Turn allocate_value into a static "constructor"
[binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2023 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 registry<objfile>::key<dwarf2_per_objfile>
128 dwarf2_objfile_data_key;
129
130 /* These are used to store the dwarf2_per_bfd objects.
131
132 objfiles having the same BFD, which doesn't require relocations, are going to
133 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
134
135 Other objfiles are not going to share a dwarf2_per_bfd with any other
136 objfiles, so they'll have their own version kept in the _objfile_data_key
137 version. */
138 static const registry<bfd>::key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
139 static const registry<objfile>::key<dwarf2_per_bfd>
140 dwarf2_per_bfd_objfile_data_key;
141
142 /* The "aclass" indices for various kinds of computed DWARF symbols. */
143
144 static int dwarf2_locexpr_index;
145 static int dwarf2_loclist_index;
146 static int dwarf2_locexpr_block_index;
147 static int dwarf2_loclist_block_index;
148
149 /* Size of .debug_loclists section header for 32-bit DWARF format. */
150 #define LOCLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_loclists section header for 64-bit DWARF format. */
153 #define LOCLIST_HEADER_SIZE64 20
154
155 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
156 #define RNGLIST_HEADER_SIZE32 12
157
158 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
159 #define RNGLIST_HEADER_SIZE64 20
160
161 /* This is a view into the index that converts from bytes to an
162 offset_type, and allows indexing. Unaligned bytes are specifically
163 allowed here, and handled via unpacking. */
164
165 class offset_view
166 {
167 public:
168 offset_view () = default;
169
170 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
171 : m_bytes (bytes)
172 {
173 }
174
175 /* Extract the INDEXth offset_type from the array. */
176 offset_type operator[] (size_t index) const
177 {
178 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
179 return (offset_type) extract_unsigned_integer (bytes,
180 sizeof (offset_type),
181 BFD_ENDIAN_LITTLE);
182 }
183
184 /* Return the number of offset_types in this array. */
185 size_t size () const
186 {
187 return m_bytes.size () / sizeof (offset_type);
188 }
189
190 /* Return true if this view is empty. */
191 bool empty () const
192 {
193 return m_bytes.empty ();
194 }
195
196 private:
197 /* The underlying bytes. */
198 gdb::array_view<const gdb_byte> m_bytes;
199 };
200
201 /* A description of the mapped index. The file format is described in
202 a comment by the code that writes the index. */
203 struct mapped_index final : public mapped_index_base
204 {
205 /* Index data format version. */
206 int version = 0;
207
208 /* The address table data. */
209 gdb::array_view<const gdb_byte> address_table;
210
211 /* The symbol table, implemented as a hash table. */
212 offset_view symbol_table;
213
214 /* A pointer to the constant pool. */
215 gdb::array_view<const gdb_byte> constant_pool;
216
217 /* Return the index into the constant pool of the name of the IDXth
218 symbol in the symbol table. */
219 offset_type symbol_name_index (offset_type idx) const
220 {
221 return symbol_table[2 * idx];
222 }
223
224 /* Return the index into the constant pool of the CU vector of the
225 IDXth symbol in the symbol table. */
226 offset_type symbol_vec_index (offset_type idx) const
227 {
228 return symbol_table[2 * idx + 1];
229 }
230
231 bool symbol_name_slot_invalid (offset_type idx) const override
232 {
233 return (symbol_name_index (idx) == 0
234 && symbol_vec_index (idx) == 0);
235 }
236
237 /* Convenience method to get at the name of the symbol at IDX in the
238 symbol table. */
239 const char *symbol_name_at
240 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
241 {
242 return (const char *) (this->constant_pool.data ()
243 + symbol_name_index (idx));
244 }
245
246 size_t symbol_name_count () const override
247 { return this->symbol_table.size () / 2; }
248
249 quick_symbol_functions_up make_quick_functions () const override;
250
251 bool version_check () const override
252 {
253 return version >= 8;
254 }
255 };
256
257 /* A description of the mapped .debug_names.
258 Uninitialized map has CU_COUNT 0. */
259 struct mapped_debug_names final : public mapped_index_base
260 {
261 bfd_endian dwarf5_byte_order;
262 bool dwarf5_is_dwarf64;
263 bool augmentation_is_gdb;
264 uint8_t offset_size;
265 uint32_t cu_count = 0;
266 uint32_t tu_count, bucket_count, name_count;
267 const gdb_byte *cu_table_reordered, *tu_table_reordered;
268 const uint32_t *bucket_table_reordered, *hash_table_reordered;
269 const gdb_byte *name_table_string_offs_reordered;
270 const gdb_byte *name_table_entry_offs_reordered;
271 const gdb_byte *entry_pool;
272
273 struct index_val
274 {
275 ULONGEST dwarf_tag;
276 struct attr
277 {
278 /* Attribute name DW_IDX_*. */
279 ULONGEST dw_idx;
280
281 /* Attribute form DW_FORM_*. */
282 ULONGEST form;
283
284 /* Value if FORM is DW_FORM_implicit_const. */
285 LONGEST implicit_const;
286 };
287 std::vector<attr> attr_vec;
288 };
289
290 std::unordered_map<ULONGEST, index_val> abbrev_map;
291
292 const char *namei_to_name
293 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
294
295 /* Implementation of the mapped_index_base virtual interface, for
296 the name_components cache. */
297
298 const char *symbol_name_at
299 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
300 { return namei_to_name (idx, per_objfile); }
301
302 size_t symbol_name_count () const override
303 { return this->name_count; }
304
305 quick_symbol_functions_up make_quick_functions () const override;
306 };
307
308 /* See dwarf2/read.h. */
309
310 dwarf2_per_objfile *
311 get_dwarf2_per_objfile (struct objfile *objfile)
312 {
313 return dwarf2_objfile_data_key.get (objfile);
314 }
315
316 /* Default names of the debugging sections. */
317
318 /* Note that if the debugging section has been compressed, it might
319 have a name like .zdebug_info. */
320
321 const struct dwarf2_debug_sections dwarf2_elf_names =
322 {
323 { ".debug_info", ".zdebug_info" },
324 { ".debug_abbrev", ".zdebug_abbrev" },
325 { ".debug_line", ".zdebug_line" },
326 { ".debug_loc", ".zdebug_loc" },
327 { ".debug_loclists", ".zdebug_loclists" },
328 { ".debug_macinfo", ".zdebug_macinfo" },
329 { ".debug_macro", ".zdebug_macro" },
330 { ".debug_str", ".zdebug_str" },
331 { ".debug_str_offsets", ".zdebug_str_offsets" },
332 { ".debug_line_str", ".zdebug_line_str" },
333 { ".debug_ranges", ".zdebug_ranges" },
334 { ".debug_rnglists", ".zdebug_rnglists" },
335 { ".debug_types", ".zdebug_types" },
336 { ".debug_addr", ".zdebug_addr" },
337 { ".debug_frame", ".zdebug_frame" },
338 { ".eh_frame", NULL },
339 { ".gdb_index", ".zgdb_index" },
340 { ".debug_names", ".zdebug_names" },
341 { ".debug_aranges", ".zdebug_aranges" },
342 23
343 };
344
345 /* List of DWO/DWP sections. */
346
347 static const struct dwop_section_names
348 {
349 struct dwarf2_section_names abbrev_dwo;
350 struct dwarf2_section_names info_dwo;
351 struct dwarf2_section_names line_dwo;
352 struct dwarf2_section_names loc_dwo;
353 struct dwarf2_section_names loclists_dwo;
354 struct dwarf2_section_names macinfo_dwo;
355 struct dwarf2_section_names macro_dwo;
356 struct dwarf2_section_names rnglists_dwo;
357 struct dwarf2_section_names str_dwo;
358 struct dwarf2_section_names str_offsets_dwo;
359 struct dwarf2_section_names types_dwo;
360 struct dwarf2_section_names cu_index;
361 struct dwarf2_section_names tu_index;
362 }
363 dwop_section_names =
364 {
365 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
366 { ".debug_info.dwo", ".zdebug_info.dwo" },
367 { ".debug_line.dwo", ".zdebug_line.dwo" },
368 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
369 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
370 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
371 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
372 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
373 { ".debug_str.dwo", ".zdebug_str.dwo" },
374 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
375 { ".debug_types.dwo", ".zdebug_types.dwo" },
376 { ".debug_cu_index", ".zdebug_cu_index" },
377 { ".debug_tu_index", ".zdebug_tu_index" },
378 };
379
380 /* local data types */
381
382 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
383 begin with a header, which contains the following information. */
384 struct loclists_rnglists_header
385 {
386 /* A 4-byte or 12-byte length containing the length of the
387 set of entries for this compilation unit, not including the
388 length field itself. */
389 unsigned int length;
390
391 /* A 2-byte version identifier. */
392 short version;
393
394 /* A 1-byte unsigned integer containing the size in bytes of an address on
395 the target system. */
396 unsigned char addr_size;
397
398 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
399 on the target system. */
400 unsigned char segment_collector_size;
401
402 /* A 4-byte count of the number of offsets that follow the header. */
403 unsigned int offset_entry_count;
404 };
405
406 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
407 This includes type_unit_group and quick_file_names. */
408
409 struct stmt_list_hash
410 {
411 /* The DWO unit this table is from or NULL if there is none. */
412 struct dwo_unit *dwo_unit;
413
414 /* Offset in .debug_line or .debug_line.dwo. */
415 sect_offset line_sect_off;
416 };
417
418 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
419 an object of this type. This contains elements of type unit groups
420 that can be shared across objfiles. The non-shareable parts are in
421 type_unit_group_unshareable. */
422
423 struct type_unit_group
424 {
425 /* The data used to construct the hash key. */
426 struct stmt_list_hash hash {};
427 };
428
429 /* These sections are what may appear in a (real or virtual) DWO file. */
430
431 struct dwo_sections
432 {
433 struct dwarf2_section_info abbrev;
434 struct dwarf2_section_info line;
435 struct dwarf2_section_info loc;
436 struct dwarf2_section_info loclists;
437 struct dwarf2_section_info macinfo;
438 struct dwarf2_section_info macro;
439 struct dwarf2_section_info rnglists;
440 struct dwarf2_section_info str;
441 struct dwarf2_section_info str_offsets;
442 /* In the case of a virtual DWO file, these two are unused. */
443 struct dwarf2_section_info info;
444 std::vector<dwarf2_section_info> types;
445 };
446
447 /* CUs/TUs in DWP/DWO files. */
448
449 struct dwo_unit
450 {
451 /* Backlink to the containing struct dwo_file. */
452 struct dwo_file *dwo_file;
453
454 /* The "id" that distinguishes this CU/TU.
455 .debug_info calls this "dwo_id", .debug_types calls this "signature".
456 Since signatures came first, we stick with it for consistency. */
457 ULONGEST signature;
458
459 /* The section this CU/TU lives in, in the DWO file. */
460 struct dwarf2_section_info *section;
461
462 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
463 sect_offset sect_off;
464 unsigned int length;
465
466 /* For types, offset in the type's DIE of the type defined by this TU. */
467 cu_offset type_offset_in_tu;
468 };
469
470 /* include/dwarf2.h defines the DWP section codes.
471 It defines a max value but it doesn't define a min value, which we
472 use for error checking, so provide one. */
473
474 enum dwp_v2_section_ids
475 {
476 DW_SECT_MIN = 1
477 };
478
479 /* Data for one DWO file.
480
481 This includes virtual DWO files (a virtual DWO file is a DWO file as it
482 appears in a DWP file). DWP files don't really have DWO files per se -
483 comdat folding of types "loses" the DWO file they came from, and from
484 a high level view DWP files appear to contain a mass of random types.
485 However, to maintain consistency with the non-DWP case we pretend DWP
486 files contain virtual DWO files, and we assign each TU with one virtual
487 DWO file (generally based on the line and abbrev section offsets -
488 a heuristic that seems to work in practice). */
489
490 struct dwo_file
491 {
492 dwo_file () = default;
493 DISABLE_COPY_AND_ASSIGN (dwo_file);
494
495 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
496 For virtual DWO files the name is constructed from the section offsets
497 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
498 from related CU+TUs. */
499 const char *dwo_name = nullptr;
500
501 /* The DW_AT_comp_dir attribute. */
502 const char *comp_dir = nullptr;
503
504 /* The bfd, when the file is open. Otherwise this is NULL.
505 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
506 gdb_bfd_ref_ptr dbfd;
507
508 /* The sections that make up this DWO file.
509 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
510 sections (for lack of a better name). */
511 struct dwo_sections sections {};
512
513 /* The CUs in the file.
514 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
515 an extension to handle LLVM's Link Time Optimization output (where
516 multiple source files may be compiled into a single object/dwo pair). */
517 htab_up cus;
518
519 /* Table of TUs in the file.
520 Each element is a struct dwo_unit. */
521 htab_up tus;
522 };
523
524 /* These sections are what may appear in a DWP file. */
525
526 struct dwp_sections
527 {
528 /* These are used by all DWP versions (1, 2 and 5). */
529 struct dwarf2_section_info str;
530 struct dwarf2_section_info cu_index;
531 struct dwarf2_section_info tu_index;
532
533 /* These are only used by DWP version 2 and version 5 files.
534 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
535 sections are referenced by section number, and are not recorded here.
536 In DWP version 2 or 5 there is at most one copy of all these sections,
537 each section being (effectively) comprised of the concatenation of all of
538 the individual sections that exist in the version 1 format.
539 To keep the code simple we treat each of these concatenated pieces as a
540 section itself (a virtual section?). */
541 struct dwarf2_section_info abbrev;
542 struct dwarf2_section_info info;
543 struct dwarf2_section_info line;
544 struct dwarf2_section_info loc;
545 struct dwarf2_section_info loclists;
546 struct dwarf2_section_info macinfo;
547 struct dwarf2_section_info macro;
548 struct dwarf2_section_info rnglists;
549 struct dwarf2_section_info str_offsets;
550 struct dwarf2_section_info types;
551 };
552
553 /* These sections are what may appear in a virtual DWO file in DWP version 1.
554 A virtual DWO file is a DWO file as it appears in a DWP file. */
555
556 struct virtual_v1_dwo_sections
557 {
558 struct dwarf2_section_info abbrev;
559 struct dwarf2_section_info line;
560 struct dwarf2_section_info loc;
561 struct dwarf2_section_info macinfo;
562 struct dwarf2_section_info macro;
563 struct dwarf2_section_info str_offsets;
564 /* Each DWP hash table entry records one CU or one TU.
565 That is recorded here, and copied to dwo_unit.section. */
566 struct dwarf2_section_info info_or_types;
567 };
568
569 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
570 In version 2, the sections of the DWO files are concatenated together
571 and stored in one section of that name. Thus each ELF section contains
572 several "virtual" sections. */
573
574 struct virtual_v2_or_v5_dwo_sections
575 {
576 bfd_size_type abbrev_offset;
577 bfd_size_type abbrev_size;
578
579 bfd_size_type line_offset;
580 bfd_size_type line_size;
581
582 bfd_size_type loc_offset;
583 bfd_size_type loc_size;
584
585 bfd_size_type loclists_offset;
586 bfd_size_type loclists_size;
587
588 bfd_size_type macinfo_offset;
589 bfd_size_type macinfo_size;
590
591 bfd_size_type macro_offset;
592 bfd_size_type macro_size;
593
594 bfd_size_type rnglists_offset;
595 bfd_size_type rnglists_size;
596
597 bfd_size_type str_offsets_offset;
598 bfd_size_type str_offsets_size;
599
600 /* Each DWP hash table entry records one CU or one TU.
601 That is recorded here, and copied to dwo_unit.section. */
602 bfd_size_type info_or_types_offset;
603 bfd_size_type info_or_types_size;
604 };
605
606 /* Contents of DWP hash tables. */
607
608 struct dwp_hash_table
609 {
610 uint32_t version, nr_columns;
611 uint32_t nr_units, nr_slots;
612 const gdb_byte *hash_table, *unit_table;
613 union
614 {
615 struct
616 {
617 const gdb_byte *indices;
618 } v1;
619 struct
620 {
621 /* This is indexed by column number and gives the id of the section
622 in that column. */
623 #define MAX_NR_V2_DWO_SECTIONS \
624 (1 /* .debug_info or .debug_types */ \
625 + 1 /* .debug_abbrev */ \
626 + 1 /* .debug_line */ \
627 + 1 /* .debug_loc */ \
628 + 1 /* .debug_str_offsets */ \
629 + 1 /* .debug_macro or .debug_macinfo */)
630 int section_ids[MAX_NR_V2_DWO_SECTIONS];
631 const gdb_byte *offsets;
632 const gdb_byte *sizes;
633 } v2;
634 struct
635 {
636 /* This is indexed by column number and gives the id of the section
637 in that column. */
638 #define MAX_NR_V5_DWO_SECTIONS \
639 (1 /* .debug_info */ \
640 + 1 /* .debug_abbrev */ \
641 + 1 /* .debug_line */ \
642 + 1 /* .debug_loclists */ \
643 + 1 /* .debug_str_offsets */ \
644 + 1 /* .debug_macro */ \
645 + 1 /* .debug_rnglists */)
646 int section_ids[MAX_NR_V5_DWO_SECTIONS];
647 const gdb_byte *offsets;
648 const gdb_byte *sizes;
649 } v5;
650 } section_pool;
651 };
652
653 /* Data for one DWP file. */
654
655 struct dwp_file
656 {
657 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
658 : name (name_),
659 dbfd (std::move (abfd))
660 {
661 }
662
663 /* Name of the file. */
664 const char *name;
665
666 /* File format version. */
667 int version = 0;
668
669 /* The bfd. */
670 gdb_bfd_ref_ptr dbfd;
671
672 /* Section info for this file. */
673 struct dwp_sections sections {};
674
675 /* Table of CUs in the file. */
676 const struct dwp_hash_table *cus = nullptr;
677
678 /* Table of TUs in the file. */
679 const struct dwp_hash_table *tus = nullptr;
680
681 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
682 htab_up loaded_cus;
683 htab_up loaded_tus;
684
685 /* Table to map ELF section numbers to their sections.
686 This is only needed for the DWP V1 file format. */
687 unsigned int num_sections = 0;
688 asection **elf_sections = nullptr;
689 };
690
691 /* Struct used to pass misc. parameters to read_die_and_children, et
692 al. which are used for both .debug_info and .debug_types dies.
693 All parameters here are unchanging for the life of the call. This
694 struct exists to abstract away the constant parameters of die reading. */
695
696 struct die_reader_specs
697 {
698 /* The bfd of die_section. */
699 bfd *abfd;
700
701 /* The CU of the DIE we are parsing. */
702 struct dwarf2_cu *cu;
703
704 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
705 struct dwo_file *dwo_file;
706
707 /* The section the die comes from.
708 This is either .debug_info or .debug_types, or the .dwo variants. */
709 struct dwarf2_section_info *die_section;
710
711 /* die_section->buffer. */
712 const gdb_byte *buffer;
713
714 /* The end of the buffer. */
715 const gdb_byte *buffer_end;
716
717 /* The abbreviation table to use when reading the DIEs. */
718 struct abbrev_table *abbrev_table;
719 };
720
721 /* A subclass of die_reader_specs that holds storage and has complex
722 constructor and destructor behavior. */
723
724 class cutu_reader : public die_reader_specs
725 {
726 public:
727
728 cutu_reader (dwarf2_per_cu_data *this_cu,
729 dwarf2_per_objfile *per_objfile,
730 struct abbrev_table *abbrev_table,
731 dwarf2_cu *existing_cu,
732 bool skip_partial,
733 abbrev_cache *cache = nullptr);
734
735 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
736 dwarf2_per_objfile *per_objfile,
737 struct dwarf2_cu *parent_cu = nullptr,
738 struct dwo_file *dwo_file = nullptr);
739
740 DISABLE_COPY_AND_ASSIGN (cutu_reader);
741
742 cutu_reader (cutu_reader &&) = default;
743
744 const gdb_byte *info_ptr = nullptr;
745 struct die_info *comp_unit_die = nullptr;
746 bool dummy_p = false;
747
748 /* Release the new CU, putting it on the chain. This cannot be done
749 for dummy CUs. */
750 void keep ();
751
752 /* Release the abbrev table, transferring ownership to the
753 caller. */
754 abbrev_table_up release_abbrev_table ()
755 {
756 return std::move (m_abbrev_table_holder);
757 }
758
759 private:
760 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
761 dwarf2_per_objfile *per_objfile,
762 dwarf2_cu *existing_cu);
763
764 struct dwarf2_per_cu_data *m_this_cu;
765 std::unique_ptr<dwarf2_cu> m_new_cu;
766
767 /* The ordinary abbreviation table. */
768 abbrev_table_up m_abbrev_table_holder;
769
770 /* The DWO abbreviation table. */
771 abbrev_table_up m_dwo_abbrev_table;
772 };
773
774 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
775 but this would require a corresponding change in unpack_field_as_long
776 and friends. */
777 static int bits_per_byte = 8;
778
779 struct variant_part_builder;
780
781 /* When reading a variant, we track a bit more information about the
782 field, and store it in an object of this type. */
783
784 struct variant_field
785 {
786 int first_field = -1;
787 int last_field = -1;
788
789 /* A variant can contain other variant parts. */
790 std::vector<variant_part_builder> variant_parts;
791
792 /* If we see a DW_TAG_variant, then this will be set if this is the
793 default branch. */
794 bool default_branch = false;
795 /* If we see a DW_AT_discr_value, then this will be the discriminant
796 value. */
797 ULONGEST discriminant_value = 0;
798 /* If we see a DW_AT_discr_list, then this is a pointer to the list
799 data. */
800 struct dwarf_block *discr_list_data = nullptr;
801 };
802
803 /* This represents a DW_TAG_variant_part. */
804
805 struct variant_part_builder
806 {
807 /* The offset of the discriminant field. */
808 sect_offset discriminant_offset {};
809
810 /* Variants that are direct children of this variant part. */
811 std::vector<variant_field> variants;
812
813 /* True if we're currently reading a variant. */
814 bool processing_variant = false;
815 };
816
817 struct nextfield
818 {
819 int accessibility = 0;
820 int virtuality = 0;
821 /* Variant parts need to find the discriminant, which is a DIE
822 reference. We track the section offset of each field to make
823 this link. */
824 sect_offset offset;
825 struct field field {};
826 };
827
828 struct fnfieldlist
829 {
830 const char *name = nullptr;
831 std::vector<struct fn_field> fnfields;
832 };
833
834 /* The routines that read and process dies for a C struct or C++ class
835 pass lists of data member fields and lists of member function fields
836 in an instance of a field_info structure, as defined below. */
837 struct field_info
838 {
839 /* List of data member and baseclasses fields. */
840 std::vector<struct nextfield> fields;
841 std::vector<struct nextfield> baseclasses;
842
843 /* Set if the accessibility of one of the fields is not public. */
844 bool non_public_fields = false;
845
846 /* Member function fieldlist array, contains name of possibly overloaded
847 member function, number of overloaded member functions and a pointer
848 to the head of the member function field chain. */
849 std::vector<struct fnfieldlist> fnfieldlists;
850
851 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
852 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
853 std::vector<struct decl_field> typedef_field_list;
854
855 /* Nested types defined by this class and the number of elements in this
856 list. */
857 std::vector<struct decl_field> nested_types_list;
858
859 /* If non-null, this is the variant part we are currently
860 reading. */
861 variant_part_builder *current_variant_part = nullptr;
862 /* This holds all the top-level variant parts attached to the type
863 we're reading. */
864 std::vector<variant_part_builder> variant_parts;
865
866 /* Return the total number of fields (including baseclasses). */
867 int nfields () const
868 {
869 return fields.size () + baseclasses.size ();
870 }
871 };
872
873 /* Loaded secondary compilation units are kept in memory until they
874 have not been referenced for the processing of this many
875 compilation units. Set this to zero to disable caching. Cache
876 sizes of up to at least twenty will improve startup time for
877 typical inter-CU-reference binaries, at an obvious memory cost. */
878 static int dwarf_max_cache_age = 5;
879 static void
880 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
881 struct cmd_list_element *c, const char *value)
882 {
883 gdb_printf (file, _("The upper bound on the age of cached "
884 "DWARF compilation units is %s.\n"),
885 value);
886 }
887 \f
888 /* local function prototypes */
889
890 static void dwarf2_find_base_address (struct die_info *die,
891 struct dwarf2_cu *cu);
892
893 class cooked_index_storage;
894 static void build_type_psymtabs_reader (cutu_reader *reader,
895 cooked_index_storage *storage);
896
897 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
898
899 static void var_decode_location (struct attribute *attr,
900 struct symbol *sym,
901 struct dwarf2_cu *cu);
902
903 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
904
905 static const gdb_byte *read_attribute (const struct die_reader_specs *,
906 struct attribute *,
907 const struct attr_abbrev *,
908 const gdb_byte *);
909
910 static void read_attribute_reprocess (const struct die_reader_specs *reader,
911 struct attribute *attr, dwarf_tag tag);
912
913 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
914
915 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
916 dwarf2_section_info *, sect_offset);
917
918 static const char *read_indirect_string
919 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
920 const struct comp_unit_head *, unsigned int *);
921
922 static const char *read_indirect_string_at_offset
923 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
924
925 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
926 const gdb_byte *,
927 unsigned int *);
928
929 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
930 ULONGEST str_index);
931
932 static const char *read_stub_str_index (struct dwarf2_cu *cu,
933 ULONGEST str_index);
934
935 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
936 struct dwarf2_cu *);
937
938 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
939 struct dwarf2_cu *cu);
940
941 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
942
943 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
944 struct dwarf2_cu *cu);
945
946 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
947
948 static struct die_info *die_specification (struct die_info *die,
949 struct dwarf2_cu **);
950
951 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
952 struct dwarf2_cu *cu,
953 const char *comp_dir);
954
955 static void dwarf_decode_lines (struct line_header *,
956 struct dwarf2_cu *,
957 CORE_ADDR, int decode_mapping);
958
959 static void dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
960 const line_header &lh);
961
962 static struct symbol *new_symbol (struct die_info *, struct type *,
963 struct dwarf2_cu *, struct symbol * = NULL);
964
965 static void dwarf2_const_value (const struct attribute *, struct symbol *,
966 struct dwarf2_cu *);
967
968 static void dwarf2_const_value_attr (const struct attribute *attr,
969 struct type *type,
970 const char *name,
971 struct obstack *obstack,
972 struct dwarf2_cu *cu, LONGEST *value,
973 const gdb_byte **bytes,
974 struct dwarf2_locexpr_baton **baton);
975
976 static struct type *read_subrange_index_type (struct die_info *die,
977 struct dwarf2_cu *cu);
978
979 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
980
981 static int need_gnat_info (struct dwarf2_cu *);
982
983 static struct type *die_descriptive_type (struct die_info *,
984 struct dwarf2_cu *);
985
986 static void set_descriptive_type (struct type *, struct die_info *,
987 struct dwarf2_cu *);
988
989 static struct type *die_containing_type (struct die_info *,
990 struct dwarf2_cu *);
991
992 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
993 struct dwarf2_cu *);
994
995 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
996
997 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
998
999 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1000
1001 static char *typename_concat (struct obstack *obs, const char *prefix,
1002 const char *suffix, int physname,
1003 struct dwarf2_cu *cu);
1004
1005 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1006
1007 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1008
1009 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1010
1011 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1012
1013 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1014
1015 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1016
1017 /* Return the .debug_loclists section to use for cu. */
1018 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1019
1020 /* Return the .debug_rnglists section to use for cu. */
1021 static struct dwarf2_section_info *cu_debug_rnglists_section
1022 (struct dwarf2_cu *cu, dwarf_tag tag);
1023
1024 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1025 values. Keep the items ordered with increasing constraints compliance. */
1026 enum pc_bounds_kind
1027 {
1028 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1029 PC_BOUNDS_NOT_PRESENT,
1030
1031 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1032 were present but they do not form a valid range of PC addresses. */
1033 PC_BOUNDS_INVALID,
1034
1035 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1036 PC_BOUNDS_RANGES,
1037
1038 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1039 PC_BOUNDS_HIGH_LOW,
1040 };
1041
1042 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1043 CORE_ADDR *, CORE_ADDR *,
1044 struct dwarf2_cu *,
1045 addrmap *,
1046 void *);
1047
1048 static void get_scope_pc_bounds (struct die_info *,
1049 CORE_ADDR *, CORE_ADDR *,
1050 struct dwarf2_cu *);
1051
1052 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1053 CORE_ADDR, struct dwarf2_cu *);
1054
1055 static void dwarf2_add_field (struct field_info *, struct die_info *,
1056 struct dwarf2_cu *);
1057
1058 static void dwarf2_attach_fields_to_type (struct field_info *,
1059 struct type *, struct dwarf2_cu *);
1060
1061 static void dwarf2_add_member_fn (struct field_info *,
1062 struct die_info *, struct type *,
1063 struct dwarf2_cu *);
1064
1065 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1066 struct type *,
1067 struct dwarf2_cu *);
1068
1069 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1070
1071 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1072
1073 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1074
1075 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1076
1077 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1078
1079 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1080
1081 static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
1082
1083 static struct type *read_module_type (struct die_info *die,
1084 struct dwarf2_cu *cu);
1085
1086 static const char *namespace_name (struct die_info *die,
1087 int *is_anonymous, struct dwarf2_cu *);
1088
1089 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1090
1091 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1092 bool * = nullptr);
1093
1094 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1095 struct dwarf2_cu *);
1096
1097 static struct die_info *read_die_and_siblings_1
1098 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1099 struct die_info *);
1100
1101 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1102 const gdb_byte *info_ptr,
1103 const gdb_byte **new_info_ptr,
1104 struct die_info *parent);
1105
1106 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1107 struct die_info **, const gdb_byte *,
1108 int);
1109
1110 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1111 struct die_info **, const gdb_byte *);
1112
1113 static void process_die (struct die_info *, struct dwarf2_cu *);
1114
1115 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1116 struct objfile *);
1117
1118 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1119
1120 static const char *dwarf2_full_name (const char *name,
1121 struct die_info *die,
1122 struct dwarf2_cu *cu);
1123
1124 static const char *dwarf2_physname (const char *name, struct die_info *die,
1125 struct dwarf2_cu *cu);
1126
1127 static struct die_info *dwarf2_extension (struct die_info *die,
1128 struct dwarf2_cu **);
1129
1130 static void store_in_ref_table (struct die_info *,
1131 struct dwarf2_cu *);
1132
1133 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1134 const struct attribute *,
1135 struct dwarf2_cu **);
1136
1137 static struct die_info *follow_die_ref (struct die_info *,
1138 const struct attribute *,
1139 struct dwarf2_cu **);
1140
1141 static struct die_info *follow_die_sig (struct die_info *,
1142 const struct attribute *,
1143 struct dwarf2_cu **);
1144
1145 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1146 struct dwarf2_cu *);
1147
1148 static struct type *get_DW_AT_signature_type (struct die_info *,
1149 const struct attribute *,
1150 struct dwarf2_cu *);
1151
1152 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1153 dwarf2_per_objfile *per_objfile);
1154
1155 static void read_signatured_type (signatured_type *sig_type,
1156 dwarf2_per_objfile *per_objfile);
1157
1158 static int attr_to_dynamic_prop (const struct attribute *attr,
1159 struct die_info *die, struct dwarf2_cu *cu,
1160 struct dynamic_prop *prop, struct type *type);
1161
1162 /* memory allocation interface */
1163
1164 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1165
1166 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1167
1168 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1169 struct dwarf2_loclist_baton *baton,
1170 const struct attribute *attr);
1171
1172 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1173 struct symbol *sym,
1174 struct dwarf2_cu *cu,
1175 int is_block);
1176
1177 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1178 const gdb_byte *info_ptr,
1179 const struct abbrev_info *abbrev,
1180 bool do_skip_children = true);
1181
1182 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1183 (sect_offset sect_off, unsigned int offset_in_dwz,
1184 dwarf2_per_bfd *per_bfd);
1185
1186 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1187 struct die_info *comp_unit_die,
1188 enum language pretend_language);
1189
1190 static struct type *set_die_type (struct die_info *, struct type *,
1191 struct dwarf2_cu *, bool = false);
1192
1193 static void create_all_units (dwarf2_per_objfile *per_objfile);
1194
1195 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1196 dwarf2_per_objfile *per_objfile,
1197 dwarf2_cu *existing_cu,
1198 bool skip_partial,
1199 enum language pretend_language);
1200
1201 static void process_full_comp_unit (dwarf2_cu *cu,
1202 enum language pretend_language);
1203
1204 static void process_full_type_unit (dwarf2_cu *cu,
1205 enum language pretend_language);
1206
1207 static struct type *get_die_type_at_offset (sect_offset,
1208 dwarf2_per_cu_data *per_cu,
1209 dwarf2_per_objfile *per_objfile);
1210
1211 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1212
1213 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1214 dwarf2_per_objfile *per_objfile,
1215 enum language pretend_language);
1216
1217 static void process_queue (dwarf2_per_objfile *per_objfile);
1218
1219 /* Class, the destructor of which frees all allocated queue entries. This
1220 will only have work to do if an error was thrown while processing the
1221 dwarf. If no error was thrown then the queue entries should have all
1222 been processed, and freed, as we went along. */
1223
1224 class dwarf2_queue_guard
1225 {
1226 public:
1227 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1228 : m_per_objfile (per_objfile)
1229 {
1230 gdb_assert (!m_per_objfile->queue.has_value ());
1231
1232 m_per_objfile->queue.emplace ();
1233 }
1234
1235 /* Free any entries remaining on the queue. There should only be
1236 entries left if we hit an error while processing the dwarf. */
1237 ~dwarf2_queue_guard ()
1238 {
1239 gdb_assert (m_per_objfile->queue.has_value ());
1240
1241 m_per_objfile->queue.reset ();
1242 }
1243
1244 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1245
1246 private:
1247 dwarf2_per_objfile *m_per_objfile;
1248 };
1249
1250 dwarf2_queue_item::~dwarf2_queue_item ()
1251 {
1252 /* Anything still marked queued is likely to be in an
1253 inconsistent state, so discard it. */
1254 if (per_cu->queued)
1255 {
1256 per_objfile->remove_cu (per_cu);
1257 per_cu->queued = 0;
1258 }
1259 }
1260
1261 /* See dwarf2/read.h. */
1262
1263 void
1264 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1265 {
1266 if (data->is_debug_types)
1267 delete static_cast<signatured_type *> (data);
1268 else
1269 delete data;
1270 }
1271
1272 static file_and_directory &find_file_and_directory
1273 (struct die_info *die, struct dwarf2_cu *cu);
1274
1275 static const char *compute_include_file_name
1276 (const struct line_header *lh,
1277 const file_entry &fe,
1278 const file_and_directory &cu_info,
1279 std::string &name_holder);
1280
1281 static htab_up allocate_signatured_type_table ();
1282
1283 static htab_up allocate_dwo_unit_table ();
1284
1285 static struct dwo_unit *lookup_dwo_unit_in_dwp
1286 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1287 const char *comp_dir, ULONGEST signature, int is_debug_types);
1288
1289 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1290
1291 static struct dwo_unit *lookup_dwo_comp_unit
1292 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1293 ULONGEST signature);
1294
1295 static struct dwo_unit *lookup_dwo_type_unit
1296 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1297
1298 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1299
1300 /* A unique pointer to a dwo_file. */
1301
1302 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1303
1304 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1305
1306 static void check_producer (struct dwarf2_cu *cu);
1307 \f
1308 /* Various complaints about symbol reading that don't abort the process. */
1309
1310 static void
1311 dwarf2_debug_line_missing_file_complaint (void)
1312 {
1313 complaint (_(".debug_line section has line data without a file"));
1314 }
1315
1316 static void
1317 dwarf2_debug_line_missing_end_sequence_complaint (void)
1318 {
1319 complaint (_(".debug_line section has line "
1320 "program sequence without an end"));
1321 }
1322
1323 static void
1324 dwarf2_complex_location_expr_complaint (void)
1325 {
1326 complaint (_("location expression too complex"));
1327 }
1328
1329 static void
1330 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1331 int arg3)
1332 {
1333 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1334 arg1, arg2, arg3);
1335 }
1336
1337 static void
1338 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1339 {
1340 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1341 arg1, arg2);
1342 }
1343
1344 /* Hash function for line_header_hash. */
1345
1346 static hashval_t
1347 line_header_hash (const struct line_header *ofs)
1348 {
1349 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1350 }
1351
1352 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1353
1354 static hashval_t
1355 line_header_hash_voidp (const void *item)
1356 {
1357 const struct line_header *ofs = (const struct line_header *) item;
1358
1359 return line_header_hash (ofs);
1360 }
1361
1362 /* Equality function for line_header_hash. */
1363
1364 static int
1365 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1366 {
1367 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1368 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1369
1370 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1371 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1372 }
1373
1374 \f
1375
1376 /* An iterator for all_units that is based on index. This
1377 approach makes it possible to iterate over all_units safely,
1378 when some caller in the loop may add new units. */
1379
1380 class all_units_iterator
1381 {
1382 public:
1383
1384 all_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1385 : m_per_bfd (per_bfd),
1386 m_index (start ? 0 : per_bfd->all_units.size ())
1387 {
1388 }
1389
1390 all_units_iterator &operator++ ()
1391 {
1392 ++m_index;
1393 return *this;
1394 }
1395
1396 dwarf2_per_cu_data *operator* () const
1397 {
1398 return m_per_bfd->get_cu (m_index);
1399 }
1400
1401 bool operator== (const all_units_iterator &other) const
1402 {
1403 return m_index == other.m_index;
1404 }
1405
1406
1407 bool operator!= (const all_units_iterator &other) const
1408 {
1409 return m_index != other.m_index;
1410 }
1411
1412 private:
1413
1414 dwarf2_per_bfd *m_per_bfd;
1415 size_t m_index;
1416 };
1417
1418 /* A range adapter for the all_units_iterator. */
1419 class all_units_range
1420 {
1421 public:
1422
1423 all_units_range (dwarf2_per_bfd *per_bfd)
1424 : m_per_bfd (per_bfd)
1425 {
1426 }
1427
1428 all_units_iterator begin ()
1429 {
1430 return all_units_iterator (m_per_bfd, true);
1431 }
1432
1433 all_units_iterator end ()
1434 {
1435 return all_units_iterator (m_per_bfd, false);
1436 }
1437
1438 private:
1439
1440 dwarf2_per_bfd *m_per_bfd;
1441 };
1442
1443 /* See declaration. */
1444
1445 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1446 bool can_copy_)
1447 : obfd (obfd),
1448 can_copy (can_copy_)
1449 {
1450 if (names == NULL)
1451 names = &dwarf2_elf_names;
1452
1453 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1454 locate_sections (obfd, sec, *names);
1455 }
1456
1457 dwarf2_per_bfd::~dwarf2_per_bfd ()
1458 {
1459 for (auto &per_cu : all_units)
1460 {
1461 per_cu->imported_symtabs_free ();
1462 per_cu->free_cached_file_names ();
1463 }
1464
1465 /* Everything else should be on this->obstack. */
1466 }
1467
1468 /* See read.h. */
1469
1470 void
1471 dwarf2_per_objfile::remove_all_cus ()
1472 {
1473 gdb_assert (!queue.has_value ());
1474
1475 m_dwarf2_cus.clear ();
1476 }
1477
1478 /* A helper class that calls free_cached_comp_units on
1479 destruction. */
1480
1481 class free_cached_comp_units
1482 {
1483 public:
1484
1485 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1486 : m_per_objfile (per_objfile)
1487 {
1488 }
1489
1490 ~free_cached_comp_units ()
1491 {
1492 m_per_objfile->remove_all_cus ();
1493 }
1494
1495 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1496
1497 private:
1498
1499 dwarf2_per_objfile *m_per_objfile;
1500 };
1501
1502 /* See read.h. */
1503
1504 bool
1505 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1506 {
1507 if (per_cu->index < this->m_symtabs.size ())
1508 return this->m_symtabs[per_cu->index] != nullptr;
1509 return false;
1510 }
1511
1512 /* See read.h. */
1513
1514 compunit_symtab *
1515 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1516 {
1517 if (per_cu->index < this->m_symtabs.size ())
1518 return this->m_symtabs[per_cu->index];
1519 return nullptr;
1520 }
1521
1522 /* See read.h. */
1523
1524 void
1525 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1526 compunit_symtab *symtab)
1527 {
1528 if (per_cu->index >= this->m_symtabs.size ())
1529 this->m_symtabs.resize (per_cu->index + 1);
1530 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1531 this->m_symtabs[per_cu->index] = symtab;
1532 }
1533
1534 /* Try to locate the sections we need for DWARF 2 debugging
1535 information and return true if we have enough to do something.
1536 NAMES points to the dwarf2 section names, or is NULL if the standard
1537 ELF names are used. CAN_COPY is true for formats where symbol
1538 interposition is possible and so symbol values must follow copy
1539 relocation rules. */
1540
1541 bool
1542 dwarf2_has_info (struct objfile *objfile,
1543 const struct dwarf2_debug_sections *names,
1544 bool can_copy)
1545 {
1546 if (objfile->flags & OBJF_READNEVER)
1547 return false;
1548
1549 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1550
1551 if (per_objfile == NULL)
1552 {
1553 dwarf2_per_bfd *per_bfd;
1554
1555 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1556 BFD doesn't require relocations.
1557
1558 We don't share with objfiles for which -readnow was requested,
1559 because it would complicate things when loading the same BFD with
1560 -readnow and then without -readnow. */
1561 if (!gdb_bfd_requires_relocations (objfile->obfd.get ())
1562 && (objfile->flags & OBJF_READNOW) == 0)
1563 {
1564 /* See if one has been created for this BFD yet. */
1565 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd.get ());
1566
1567 if (per_bfd == nullptr)
1568 {
1569 /* No, create it now. */
1570 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
1571 can_copy);
1572 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
1573 }
1574 }
1575 else
1576 {
1577 /* No sharing possible, create one specifically for this objfile. */
1578 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
1579 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1580 }
1581
1582 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1583 }
1584
1585 return (!per_objfile->per_bfd->info.is_virtual
1586 && per_objfile->per_bfd->info.s.section != NULL
1587 && !per_objfile->per_bfd->abbrev.is_virtual
1588 && per_objfile->per_bfd->abbrev.s.section != NULL);
1589 }
1590
1591 /* See declaration. */
1592
1593 void
1594 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1595 const dwarf2_debug_sections &names)
1596 {
1597 flagword aflag = bfd_section_flags (sectp);
1598
1599 if ((aflag & SEC_HAS_CONTENTS) == 0)
1600 {
1601 }
1602 else if (elf_section_data (sectp)->this_hdr.sh_size
1603 > bfd_get_file_size (abfd))
1604 {
1605 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1606 warning (_("Discarding section %s which has a section size (%s"
1607 ") larger than the file size [in module %s]"),
1608 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1609 bfd_get_filename (abfd));
1610 }
1611 else if (names.info.matches (sectp->name))
1612 {
1613 this->info.s.section = sectp;
1614 this->info.size = bfd_section_size (sectp);
1615 }
1616 else if (names.abbrev.matches (sectp->name))
1617 {
1618 this->abbrev.s.section = sectp;
1619 this->abbrev.size = bfd_section_size (sectp);
1620 }
1621 else if (names.line.matches (sectp->name))
1622 {
1623 this->line.s.section = sectp;
1624 this->line.size = bfd_section_size (sectp);
1625 }
1626 else if (names.loc.matches (sectp->name))
1627 {
1628 this->loc.s.section = sectp;
1629 this->loc.size = bfd_section_size (sectp);
1630 }
1631 else if (names.loclists.matches (sectp->name))
1632 {
1633 this->loclists.s.section = sectp;
1634 this->loclists.size = bfd_section_size (sectp);
1635 }
1636 else if (names.macinfo.matches (sectp->name))
1637 {
1638 this->macinfo.s.section = sectp;
1639 this->macinfo.size = bfd_section_size (sectp);
1640 }
1641 else if (names.macro.matches (sectp->name))
1642 {
1643 this->macro.s.section = sectp;
1644 this->macro.size = bfd_section_size (sectp);
1645 }
1646 else if (names.str.matches (sectp->name))
1647 {
1648 this->str.s.section = sectp;
1649 this->str.size = bfd_section_size (sectp);
1650 }
1651 else if (names.str_offsets.matches (sectp->name))
1652 {
1653 this->str_offsets.s.section = sectp;
1654 this->str_offsets.size = bfd_section_size (sectp);
1655 }
1656 else if (names.line_str.matches (sectp->name))
1657 {
1658 this->line_str.s.section = sectp;
1659 this->line_str.size = bfd_section_size (sectp);
1660 }
1661 else if (names.addr.matches (sectp->name))
1662 {
1663 this->addr.s.section = sectp;
1664 this->addr.size = bfd_section_size (sectp);
1665 }
1666 else if (names.frame.matches (sectp->name))
1667 {
1668 this->frame.s.section = sectp;
1669 this->frame.size = bfd_section_size (sectp);
1670 }
1671 else if (names.eh_frame.matches (sectp->name))
1672 {
1673 this->eh_frame.s.section = sectp;
1674 this->eh_frame.size = bfd_section_size (sectp);
1675 }
1676 else if (names.ranges.matches (sectp->name))
1677 {
1678 this->ranges.s.section = sectp;
1679 this->ranges.size = bfd_section_size (sectp);
1680 }
1681 else if (names.rnglists.matches (sectp->name))
1682 {
1683 this->rnglists.s.section = sectp;
1684 this->rnglists.size = bfd_section_size (sectp);
1685 }
1686 else if (names.types.matches (sectp->name))
1687 {
1688 struct dwarf2_section_info type_section;
1689
1690 memset (&type_section, 0, sizeof (type_section));
1691 type_section.s.section = sectp;
1692 type_section.size = bfd_section_size (sectp);
1693
1694 this->types.push_back (type_section);
1695 }
1696 else if (names.gdb_index.matches (sectp->name))
1697 {
1698 this->gdb_index.s.section = sectp;
1699 this->gdb_index.size = bfd_section_size (sectp);
1700 }
1701 else if (names.debug_names.matches (sectp->name))
1702 {
1703 this->debug_names.s.section = sectp;
1704 this->debug_names.size = bfd_section_size (sectp);
1705 }
1706 else if (names.debug_aranges.matches (sectp->name))
1707 {
1708 this->debug_aranges.s.section = sectp;
1709 this->debug_aranges.size = bfd_section_size (sectp);
1710 }
1711
1712 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1713 && bfd_section_vma (sectp) == 0)
1714 this->has_section_at_zero = true;
1715 }
1716
1717 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1718 SECTION_NAME. */
1719
1720 void
1721 dwarf2_get_section_info (struct objfile *objfile,
1722 enum dwarf2_section_enum sect,
1723 asection **sectp, const gdb_byte **bufp,
1724 bfd_size_type *sizep)
1725 {
1726 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1727 struct dwarf2_section_info *info;
1728
1729 /* We may see an objfile without any DWARF, in which case we just
1730 return nothing. */
1731 if (per_objfile == NULL)
1732 {
1733 *sectp = NULL;
1734 *bufp = NULL;
1735 *sizep = 0;
1736 return;
1737 }
1738 switch (sect)
1739 {
1740 case DWARF2_DEBUG_FRAME:
1741 info = &per_objfile->per_bfd->frame;
1742 break;
1743 case DWARF2_EH_FRAME:
1744 info = &per_objfile->per_bfd->eh_frame;
1745 break;
1746 default:
1747 gdb_assert_not_reached ("unexpected section");
1748 }
1749
1750 info->read (objfile);
1751
1752 *sectp = info->get_bfd_section ();
1753 *bufp = info->buffer;
1754 *sizep = info->size;
1755 }
1756
1757 /* See dwarf2/read.h. */
1758
1759 void
1760 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1761 {
1762 info.read (objfile);
1763 abbrev.read (objfile);
1764 line.read (objfile);
1765 str.read (objfile);
1766 str_offsets.read (objfile);
1767 line_str.read (objfile);
1768 ranges.read (objfile);
1769 rnglists.read (objfile);
1770 addr.read (objfile);
1771
1772 for (auto &section : types)
1773 section.read (objfile);
1774 }
1775
1776 \f
1777 /* DWARF quick_symbol_functions support. */
1778
1779 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1780 unique line tables, so we maintain a separate table of all .debug_line
1781 derived entries to support the sharing.
1782 All the quick functions need is the list of file names. We discard the
1783 line_header when we're done and don't need to record it here. */
1784 struct quick_file_names
1785 {
1786 /* The data used to construct the hash key. */
1787 struct stmt_list_hash hash;
1788
1789 /* The number of entries in file_names, real_names. */
1790 unsigned int num_file_names;
1791
1792 /* The CU directory, as given by DW_AT_comp_dir. May be
1793 nullptr. */
1794 const char *comp_dir;
1795
1796 /* The file names from the line table, after being run through
1797 file_full_name. */
1798 const char **file_names;
1799
1800 /* The file names from the line table after being run through
1801 gdb_realpath. These are computed lazily. */
1802 const char **real_names;
1803 };
1804
1805 struct dwarf2_base_index_functions : public quick_symbol_functions
1806 {
1807 bool has_symbols (struct objfile *objfile) override;
1808
1809 bool has_unexpanded_symtabs (struct objfile *objfile) override;
1810
1811 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
1812
1813 void forget_cached_source_info (struct objfile *objfile) override;
1814
1815 enum language lookup_global_symbol_language (struct objfile *objfile,
1816 const char *name,
1817 domain_enum domain,
1818 bool *symbol_found_p) override
1819 {
1820 *symbol_found_p = false;
1821 return language_unknown;
1822 }
1823
1824 void print_stats (struct objfile *objfile, bool print_bcache) override;
1825
1826 void expand_all_symtabs (struct objfile *objfile) override;
1827
1828 /* A helper function that finds the per-cu object from an "adjusted"
1829 PC -- a PC with the base text offset removed. */
1830 virtual dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
1831 CORE_ADDR adjusted_pc);
1832
1833 struct compunit_symtab *find_pc_sect_compunit_symtab
1834 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
1835 CORE_ADDR pc, struct obj_section *section, int warn_if_readin)
1836 override final;
1837
1838 struct compunit_symtab *find_compunit_symtab_by_address
1839 (struct objfile *objfile, CORE_ADDR address) override
1840 {
1841 return nullptr;
1842 }
1843
1844 void map_symbol_filenames (struct objfile *objfile,
1845 gdb::function_view<symbol_filename_ftype> fun,
1846 bool need_fullname) override;
1847 };
1848
1849 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1850 It's handy in this case to have an empty implementation of the
1851 quick symbol functions, to avoid special cases in the rest of the
1852 code. */
1853
1854 struct readnow_functions : public dwarf2_base_index_functions
1855 {
1856 void dump (struct objfile *objfile) override
1857 {
1858 }
1859
1860 void expand_matching_symbols
1861 (struct objfile *,
1862 const lookup_name_info &lookup_name,
1863 domain_enum domain,
1864 int global,
1865 symbol_compare_ftype *ordered_compare) override
1866 {
1867 }
1868
1869 bool expand_symtabs_matching
1870 (struct objfile *objfile,
1871 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1872 const lookup_name_info *lookup_name,
1873 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1874 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1875 block_search_flags search_flags,
1876 domain_enum domain,
1877 enum search_domain kind) override
1878 {
1879 return true;
1880 }
1881 };
1882
1883 struct dwarf2_gdb_index : public dwarf2_base_index_functions
1884 {
1885 void dump (struct objfile *objfile) override;
1886
1887 void expand_matching_symbols
1888 (struct objfile *,
1889 const lookup_name_info &lookup_name,
1890 domain_enum domain,
1891 int global,
1892 symbol_compare_ftype *ordered_compare) override;
1893
1894 bool expand_symtabs_matching
1895 (struct objfile *objfile,
1896 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1897 const lookup_name_info *lookup_name,
1898 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1899 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1900 block_search_flags search_flags,
1901 domain_enum domain,
1902 enum search_domain kind) override;
1903 };
1904
1905 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
1906 {
1907 void dump (struct objfile *objfile) override;
1908
1909 void expand_matching_symbols
1910 (struct objfile *,
1911 const lookup_name_info &lookup_name,
1912 domain_enum domain,
1913 int global,
1914 symbol_compare_ftype *ordered_compare) override;
1915
1916 bool expand_symtabs_matching
1917 (struct objfile *objfile,
1918 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1919 const lookup_name_info *lookup_name,
1920 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1921 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1922 block_search_flags search_flags,
1923 domain_enum domain,
1924 enum search_domain kind) override;
1925 };
1926
1927 quick_symbol_functions_up
1928 mapped_index::make_quick_functions () const
1929 {
1930 return quick_symbol_functions_up (new dwarf2_gdb_index);
1931 }
1932
1933 quick_symbol_functions_up
1934 mapped_debug_names::make_quick_functions () const
1935 {
1936 return quick_symbol_functions_up (new dwarf2_debug_names_index);
1937 }
1938
1939 /* Utility hash function for a stmt_list_hash. */
1940
1941 static hashval_t
1942 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1943 {
1944 hashval_t v = 0;
1945
1946 if (stmt_list_hash->dwo_unit != NULL)
1947 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1948 v += to_underlying (stmt_list_hash->line_sect_off);
1949 return v;
1950 }
1951
1952 /* Utility equality function for a stmt_list_hash. */
1953
1954 static int
1955 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1956 const struct stmt_list_hash *rhs)
1957 {
1958 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1959 return 0;
1960 if (lhs->dwo_unit != NULL
1961 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1962 return 0;
1963
1964 return lhs->line_sect_off == rhs->line_sect_off;
1965 }
1966
1967 /* Hash function for a quick_file_names. */
1968
1969 static hashval_t
1970 hash_file_name_entry (const void *e)
1971 {
1972 const struct quick_file_names *file_data
1973 = (const struct quick_file_names *) e;
1974
1975 return hash_stmt_list_entry (&file_data->hash);
1976 }
1977
1978 /* Equality function for a quick_file_names. */
1979
1980 static int
1981 eq_file_name_entry (const void *a, const void *b)
1982 {
1983 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1984 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1985
1986 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1987 }
1988
1989 /* Create a quick_file_names hash table. */
1990
1991 static htab_up
1992 create_quick_file_names_table (unsigned int nr_initial_entries)
1993 {
1994 return htab_up (htab_create_alloc (nr_initial_entries,
1995 hash_file_name_entry, eq_file_name_entry,
1996 nullptr, xcalloc, xfree));
1997 }
1998
1999 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2000 function is unrelated to symtabs, symtab would have to be created afterwards.
2001 You should call age_cached_comp_units after processing the CU. */
2002
2003 static dwarf2_cu *
2004 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2005 bool skip_partial)
2006 {
2007 if (per_cu->is_debug_types)
2008 load_full_type_unit (per_cu, per_objfile);
2009 else
2010 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2011 skip_partial, language_minimal);
2012
2013 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2014 if (cu == nullptr)
2015 return nullptr; /* Dummy CU. */
2016
2017 dwarf2_find_base_address (cu->dies, cu);
2018
2019 return cu;
2020 }
2021
2022 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2023
2024 static void
2025 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2026 dwarf2_per_objfile *per_objfile, bool skip_partial)
2027 {
2028 {
2029 /* The destructor of dwarf2_queue_guard frees any entries left on
2030 the queue. After this point we're guaranteed to leave this function
2031 with the dwarf queue empty. */
2032 dwarf2_queue_guard q_guard (per_objfile);
2033
2034 if (!per_objfile->symtab_set_p (per_cu))
2035 {
2036 queue_comp_unit (per_cu, per_objfile, language_minimal);
2037 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2038
2039 /* If we just loaded a CU from a DWO, and we're working with an index
2040 that may badly handle TUs, load all the TUs in that DWO as well.
2041 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2042 if (!per_cu->is_debug_types
2043 && cu != NULL
2044 && cu->dwo_unit != NULL
2045 && per_objfile->per_bfd->index_table != NULL
2046 && !per_objfile->per_bfd->index_table->version_check ()
2047 /* DWP files aren't supported yet. */
2048 && get_dwp_file (per_objfile) == NULL)
2049 queue_and_load_all_dwo_tus (cu);
2050 }
2051
2052 process_queue (per_objfile);
2053 }
2054
2055 /* Age the cache, releasing compilation units that have not
2056 been used recently. */
2057 per_objfile->age_comp_units ();
2058 }
2059
2060 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2061 the per-objfile for which this symtab is instantiated.
2062
2063 Returns the resulting symbol table. */
2064
2065 static struct compunit_symtab *
2066 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2067 dwarf2_per_objfile *per_objfile,
2068 bool skip_partial)
2069 {
2070 if (!per_objfile->symtab_set_p (per_cu))
2071 {
2072 free_cached_comp_units freer (per_objfile);
2073 scoped_restore decrementer = increment_reading_symtab ();
2074 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2075 process_cu_includes (per_objfile);
2076 }
2077
2078 return per_objfile->get_symtab (per_cu);
2079 }
2080
2081 /* See read.h. */
2082
2083 dwarf2_per_cu_data_up
2084 dwarf2_per_bfd::allocate_per_cu ()
2085 {
2086 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2087 result->per_bfd = this;
2088 result->index = all_units.size ();
2089 return result;
2090 }
2091
2092 /* See read.h. */
2093
2094 signatured_type_up
2095 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2096 {
2097 signatured_type_up result (new signatured_type (signature));
2098 result->per_bfd = this;
2099 result->index = all_units.size ();
2100 result->is_debug_types = true;
2101 tu_stats.nr_tus++;
2102 return result;
2103 }
2104
2105 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2106 obstack, and constructed with the specified field values. */
2107
2108 static dwarf2_per_cu_data_up
2109 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2110 struct dwarf2_section_info *section,
2111 int is_dwz,
2112 sect_offset sect_off, ULONGEST length)
2113 {
2114 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2115 the_cu->sect_off = sect_off;
2116 the_cu->set_length (length);
2117 the_cu->section = section;
2118 the_cu->is_dwz = is_dwz;
2119 return the_cu;
2120 }
2121
2122 /* A helper for create_cus_from_index that handles a given list of
2123 CUs. */
2124
2125 static void
2126 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2127 const gdb_byte *cu_list, offset_type n_elements,
2128 struct dwarf2_section_info *section,
2129 int is_dwz)
2130 {
2131 for (offset_type i = 0; i < n_elements; i += 2)
2132 {
2133 gdb_static_assert (sizeof (ULONGEST) >= 8);
2134
2135 sect_offset sect_off
2136 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2137 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2138 cu_list += 2 * 8;
2139
2140 dwarf2_per_cu_data_up per_cu
2141 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2142 length);
2143 per_bfd->all_units.push_back (std::move (per_cu));
2144 }
2145 }
2146
2147 /* Read the CU list from the mapped index, and use it to create all
2148 the CU objects for PER_BFD. */
2149
2150 static void
2151 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2152 const gdb_byte *cu_list, offset_type cu_list_elements,
2153 const gdb_byte *dwz_list, offset_type dwz_elements)
2154 {
2155 gdb_assert (per_bfd->all_units.empty ());
2156 per_bfd->all_units.reserve ((cu_list_elements + dwz_elements) / 2);
2157
2158 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2159 &per_bfd->info, 0);
2160
2161 if (dwz_elements == 0)
2162 return;
2163
2164 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2165 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2166 &dwz->info, 1);
2167 }
2168
2169 /* Create the signatured type hash table from the index. */
2170
2171 static void
2172 create_signatured_type_table_from_index
2173 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2174 const gdb_byte *bytes, offset_type elements)
2175 {
2176 htab_up sig_types_hash = allocate_signatured_type_table ();
2177
2178 for (offset_type i = 0; i < elements; i += 3)
2179 {
2180 signatured_type_up sig_type;
2181 ULONGEST signature;
2182 void **slot;
2183 cu_offset type_offset_in_tu;
2184
2185 gdb_static_assert (sizeof (ULONGEST) >= 8);
2186 sect_offset sect_off
2187 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2188 type_offset_in_tu
2189 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2190 BFD_ENDIAN_LITTLE);
2191 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2192 bytes += 3 * 8;
2193
2194 sig_type = per_bfd->allocate_signatured_type (signature);
2195 sig_type->type_offset_in_tu = type_offset_in_tu;
2196 sig_type->section = section;
2197 sig_type->sect_off = sect_off;
2198
2199 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2200 *slot = sig_type.get ();
2201
2202 per_bfd->all_units.emplace_back (sig_type.release ());
2203 }
2204
2205 per_bfd->signatured_types = std::move (sig_types_hash);
2206 }
2207
2208 /* Create the signatured type hash table from .debug_names. */
2209
2210 static void
2211 create_signatured_type_table_from_debug_names
2212 (dwarf2_per_objfile *per_objfile,
2213 const mapped_debug_names &map,
2214 struct dwarf2_section_info *section,
2215 struct dwarf2_section_info *abbrev_section)
2216 {
2217 struct objfile *objfile = per_objfile->objfile;
2218
2219 section->read (objfile);
2220 abbrev_section->read (objfile);
2221
2222 htab_up sig_types_hash = allocate_signatured_type_table ();
2223
2224 for (uint32_t i = 0; i < map.tu_count; ++i)
2225 {
2226 signatured_type_up sig_type;
2227 void **slot;
2228
2229 sect_offset sect_off
2230 = (sect_offset) (extract_unsigned_integer
2231 (map.tu_table_reordered + i * map.offset_size,
2232 map.offset_size,
2233 map.dwarf5_byte_order));
2234
2235 comp_unit_head cu_header;
2236 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2237 abbrev_section,
2238 section->buffer + to_underlying (sect_off),
2239 rcuh_kind::TYPE);
2240
2241 sig_type = per_objfile->per_bfd->allocate_signatured_type
2242 (cu_header.signature);
2243 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2244 sig_type->section = section;
2245 sig_type->sect_off = sect_off;
2246
2247 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2248 *slot = sig_type.get ();
2249
2250 per_objfile->per_bfd->all_units.emplace_back (sig_type.release ());
2251 }
2252
2253 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2254 }
2255
2256 /* Read the address map data from the mapped index, and use it to
2257 populate the index_addrmap. */
2258
2259 static void
2260 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2261 struct mapped_index *index)
2262 {
2263 struct objfile *objfile = per_objfile->objfile;
2264 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2265 struct gdbarch *gdbarch = objfile->arch ();
2266 const gdb_byte *iter, *end;
2267 CORE_ADDR baseaddr;
2268
2269 addrmap_mutable mutable_map;
2270
2271 iter = index->address_table.data ();
2272 end = iter + index->address_table.size ();
2273
2274 baseaddr = objfile->text_section_offset ();
2275
2276 while (iter < end)
2277 {
2278 ULONGEST hi, lo, cu_index;
2279 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2280 iter += 8;
2281 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2282 iter += 8;
2283 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2284 iter += 4;
2285
2286 if (lo > hi)
2287 {
2288 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2289 hex_string (lo), hex_string (hi));
2290 continue;
2291 }
2292
2293 if (cu_index >= per_bfd->all_units.size ())
2294 {
2295 complaint (_(".gdb_index address table has invalid CU number %u"),
2296 (unsigned) cu_index);
2297 continue;
2298 }
2299
2300 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2301 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2302 mutable_map.set_empty (lo, hi - 1, per_bfd->get_cu (cu_index));
2303 }
2304
2305 per_bfd->index_addrmap
2306 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack, &mutable_map);
2307 }
2308
2309 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2310 to populate given addrmap. Returns true on success, false on
2311 failure. */
2312
2313 static bool
2314 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2315 struct dwarf2_section_info *section,
2316 addrmap *mutable_map)
2317 {
2318 struct objfile *objfile = per_objfile->objfile;
2319 bfd *abfd = objfile->obfd.get ();
2320 struct gdbarch *gdbarch = objfile->arch ();
2321 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2322 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2323
2324 std::unordered_map<sect_offset,
2325 dwarf2_per_cu_data *,
2326 gdb::hash_enum<sect_offset>>
2327 debug_info_offset_to_per_cu;
2328 for (const auto &per_cu : per_bfd->all_units)
2329 {
2330 /* A TU will not need aranges, and skipping them here is an easy
2331 way of ignoring .debug_types -- and possibly seeing a
2332 duplicate section offset -- entirely. The same applies to
2333 units coming from a dwz file. */
2334 if (per_cu->is_debug_types || per_cu->is_dwz)
2335 continue;
2336
2337 const auto insertpair
2338 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2339 per_cu.get ());
2340
2341 /* Assume no duplicate offsets in all_units. */
2342 gdb_assert (insertpair.second);
2343 }
2344
2345 std::set<sect_offset> debug_info_offset_seen;
2346
2347 section->read (objfile);
2348
2349 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2350
2351 const gdb_byte *addr = section->buffer;
2352
2353 while (addr < section->buffer + section->size)
2354 {
2355 const gdb_byte *const entry_addr = addr;
2356 unsigned int bytes_read;
2357
2358 const LONGEST entry_length = read_initial_length (abfd, addr,
2359 &bytes_read);
2360 addr += bytes_read;
2361
2362 const gdb_byte *const entry_end = addr + entry_length;
2363 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2364 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2365 if (addr + entry_length > section->buffer + section->size)
2366 {
2367 warning (_("Section .debug_aranges in %s entry at offset %s "
2368 "length %s exceeds section length %s, "
2369 "ignoring .debug_aranges."),
2370 objfile_name (objfile),
2371 plongest (entry_addr - section->buffer),
2372 plongest (bytes_read + entry_length),
2373 pulongest (section->size));
2374 return false;
2375 }
2376
2377 /* The version number. */
2378 const uint16_t version = read_2_bytes (abfd, addr);
2379 addr += 2;
2380 if (version != 2)
2381 {
2382 warning (_("Section .debug_aranges in %s entry at offset %s "
2383 "has unsupported version %d, ignoring .debug_aranges."),
2384 objfile_name (objfile),
2385 plongest (entry_addr - section->buffer), version);
2386 return false;
2387 }
2388
2389 const uint64_t debug_info_offset
2390 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2391 addr += offset_size;
2392 const auto per_cu_it
2393 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2394 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2395 {
2396 warning (_("Section .debug_aranges in %s entry at offset %s "
2397 "debug_info_offset %s does not exists, "
2398 "ignoring .debug_aranges."),
2399 objfile_name (objfile),
2400 plongest (entry_addr - section->buffer),
2401 pulongest (debug_info_offset));
2402 return false;
2403 }
2404 const auto insertpair
2405 = debug_info_offset_seen.insert (sect_offset (debug_info_offset));
2406 if (!insertpair.second)
2407 {
2408 warning (_("Section .debug_aranges in %s has duplicate "
2409 "debug_info_offset %s, ignoring .debug_aranges."),
2410 objfile_name (objfile),
2411 sect_offset_str (sect_offset (debug_info_offset)));
2412 return false;
2413 }
2414 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2415
2416 const uint8_t address_size = *addr++;
2417 if (address_size < 1 || address_size > 8)
2418 {
2419 warning (_("Section .debug_aranges in %s entry at offset %s "
2420 "address_size %u is invalid, ignoring .debug_aranges."),
2421 objfile_name (objfile),
2422 plongest (entry_addr - section->buffer), address_size);
2423 return false;
2424 }
2425
2426 const uint8_t segment_selector_size = *addr++;
2427 if (segment_selector_size != 0)
2428 {
2429 warning (_("Section .debug_aranges in %s entry at offset %s "
2430 "segment_selector_size %u is not supported, "
2431 "ignoring .debug_aranges."),
2432 objfile_name (objfile),
2433 plongest (entry_addr - section->buffer),
2434 segment_selector_size);
2435 return false;
2436 }
2437
2438 /* Must pad to an alignment boundary that is twice the address
2439 size. It is undocumented by the DWARF standard but GCC does
2440 use it. However, not every compiler does this. We can see
2441 whether it has happened by looking at the total length of the
2442 contents of the aranges for this CU -- it if isn't a multiple
2443 of twice the address size, then we skip any leftover
2444 bytes. */
2445 addr += (entry_end - addr) % (2 * address_size);
2446
2447 while (addr < entry_end)
2448 {
2449 if (addr + 2 * address_size > entry_end)
2450 {
2451 warning (_("Section .debug_aranges in %s entry at offset %s "
2452 "address list is not properly terminated, "
2453 "ignoring .debug_aranges."),
2454 objfile_name (objfile),
2455 plongest (entry_addr - section->buffer));
2456 return false;
2457 }
2458 ULONGEST start = extract_unsigned_integer (addr, address_size,
2459 dwarf5_byte_order);
2460 addr += address_size;
2461 ULONGEST length = extract_unsigned_integer (addr, address_size,
2462 dwarf5_byte_order);
2463 addr += address_size;
2464 if (start == 0 && length == 0)
2465 {
2466 /* This can happen on some targets with --gc-sections.
2467 This pair of values is also used to mark the end of
2468 the entries for a given CU, but we ignore it and
2469 instead handle termination using the check at the top
2470 of the loop. */
2471 continue;
2472 }
2473 if (start == 0 && !per_bfd->has_section_at_zero)
2474 {
2475 /* Symbol was eliminated due to a COMDAT group. */
2476 continue;
2477 }
2478 ULONGEST end = start + length;
2479 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2480 - baseaddr);
2481 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2482 - baseaddr);
2483 mutable_map->set_empty (start, end - 1, per_cu);
2484 }
2485
2486 per_cu->addresses_seen = true;
2487 }
2488
2489 return true;
2490 }
2491
2492 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2493 populate the index_addrmap. */
2494
2495 static void
2496 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2497 struct dwarf2_section_info *section)
2498 {
2499 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2500
2501 addrmap_mutable mutable_map;
2502
2503 if (read_addrmap_from_aranges (per_objfile, section, &mutable_map))
2504 per_bfd->index_addrmap
2505 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack,
2506 &mutable_map);
2507 }
2508
2509 /* A helper function that reads the .gdb_index from BUFFER and fills
2510 in MAP. FILENAME is the name of the file containing the data;
2511 it is used for error reporting. DEPRECATED_OK is true if it is
2512 ok to use deprecated sections.
2513
2514 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2515 out parameters that are filled in with information about the CU and
2516 TU lists in the section.
2517
2518 Returns true if all went well, false otherwise. */
2519
2520 static bool
2521 read_gdb_index_from_buffer (const char *filename,
2522 bool deprecated_ok,
2523 gdb::array_view<const gdb_byte> buffer,
2524 struct mapped_index *map,
2525 const gdb_byte **cu_list,
2526 offset_type *cu_list_elements,
2527 const gdb_byte **types_list,
2528 offset_type *types_list_elements)
2529 {
2530 const gdb_byte *addr = &buffer[0];
2531 offset_view metadata (buffer);
2532
2533 /* Version check. */
2534 offset_type version = metadata[0];
2535 /* Versions earlier than 3 emitted every copy of a psymbol. This
2536 causes the index to behave very poorly for certain requests. Version 3
2537 contained incomplete addrmap. So, it seems better to just ignore such
2538 indices. */
2539 if (version < 4)
2540 {
2541 static int warning_printed = 0;
2542 if (!warning_printed)
2543 {
2544 warning (_("Skipping obsolete .gdb_index section in %s."),
2545 filename);
2546 warning_printed = 1;
2547 }
2548 return 0;
2549 }
2550 /* Index version 4 uses a different hash function than index version
2551 5 and later.
2552
2553 Versions earlier than 6 did not emit psymbols for inlined
2554 functions. Using these files will cause GDB not to be able to
2555 set breakpoints on inlined functions by name, so we ignore these
2556 indices unless the user has done
2557 "set use-deprecated-index-sections on". */
2558 if (version < 6 && !deprecated_ok)
2559 {
2560 static int warning_printed = 0;
2561 if (!warning_printed)
2562 {
2563 warning (_("\
2564 Skipping deprecated .gdb_index section in %s.\n\
2565 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2566 to use the section anyway."),
2567 filename);
2568 warning_printed = 1;
2569 }
2570 return 0;
2571 }
2572 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2573 of the TU (for symbols coming from TUs),
2574 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2575 Plus gold-generated indices can have duplicate entries for global symbols,
2576 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2577 These are just performance bugs, and we can't distinguish gdb-generated
2578 indices from gold-generated ones, so issue no warning here. */
2579
2580 /* Indexes with higher version than the one supported by GDB may be no
2581 longer backward compatible. */
2582 if (version > 8)
2583 return 0;
2584
2585 map->version = version;
2586
2587 int i = 1;
2588 *cu_list = addr + metadata[i];
2589 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2590 ++i;
2591
2592 *types_list = addr + metadata[i];
2593 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2594 ++i;
2595
2596 const gdb_byte *address_table = addr + metadata[i];
2597 const gdb_byte *address_table_end = addr + metadata[i + 1];
2598 map->address_table
2599 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2600 ++i;
2601
2602 const gdb_byte *symbol_table = addr + metadata[i];
2603 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2604 map->symbol_table
2605 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2606 symbol_table_end));
2607
2608 ++i;
2609 map->constant_pool = buffer.slice (metadata[i]);
2610
2611 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2612 {
2613 /* An empty constant pool implies that all symbol table entries are
2614 empty. Make map->symbol_table.empty () == true. */
2615 map->symbol_table
2616 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2617 symbol_table));
2618 }
2619
2620 return 1;
2621 }
2622
2623 static void finalize_all_units (dwarf2_per_bfd *per_bfd);
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 {
2703 per_bfd->all_units.clear ();
2704 return 0;
2705 }
2706
2707 dwarf2_section_info *section
2708 = (per_bfd->types.size () == 1
2709 ? &per_bfd->types[0]
2710 : &per_bfd->info);
2711
2712 create_signatured_type_table_from_index (per_bfd, section, types_list,
2713 types_list_elements);
2714 }
2715
2716 finalize_all_units (per_bfd);
2717
2718 create_addrmap_from_index (per_objfile, map.get ());
2719
2720 per_bfd->index_table = std::move (map);
2721 per_bfd->quick_file_names_table =
2722 create_quick_file_names_table (per_bfd->all_units.size ());
2723
2724 return 1;
2725 }
2726
2727 /* die_reader_func for dw2_get_file_names. */
2728
2729 static void
2730 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2731 struct die_info *comp_unit_die)
2732 {
2733 struct dwarf2_cu *cu = reader->cu;
2734 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2736 struct dwarf2_per_cu_data *lh_cu;
2737 struct attribute *attr;
2738 void **slot;
2739 struct quick_file_names *qfn;
2740
2741 gdb_assert (! this_cu->is_debug_types);
2742
2743 this_cu->files_read = true;
2744 /* Our callers never want to match partial units -- instead they
2745 will match the enclosing full CU. */
2746 if (comp_unit_die->tag == DW_TAG_partial_unit)
2747 return;
2748
2749 lh_cu = this_cu;
2750 slot = NULL;
2751
2752 line_header_up lh;
2753 sect_offset line_offset {};
2754
2755 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
2756
2757 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2758 if (attr != nullptr && attr->form_is_unsigned ())
2759 {
2760 struct quick_file_names find_entry;
2761
2762 line_offset = (sect_offset) attr->as_unsigned ();
2763
2764 /* We may have already read in this line header (TU line header sharing).
2765 If we have we're done. */
2766 find_entry.hash.dwo_unit = cu->dwo_unit;
2767 find_entry.hash.line_sect_off = line_offset;
2768 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2769 &find_entry, INSERT);
2770 if (*slot != NULL)
2771 {
2772 lh_cu->file_names = (struct quick_file_names *) *slot;
2773 return;
2774 }
2775
2776 lh = dwarf_decode_line_header (line_offset, cu, fnd.get_comp_dir ());
2777 }
2778
2779 int offset = 0;
2780 if (!fnd.is_unknown ())
2781 ++offset;
2782 else if (lh == nullptr)
2783 return;
2784
2785 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2786 qfn->hash.dwo_unit = cu->dwo_unit;
2787 qfn->hash.line_sect_off = line_offset;
2788 /* There may not be a DW_AT_stmt_list. */
2789 if (slot != nullptr)
2790 *slot = qfn;
2791
2792 std::vector<const char *> include_names;
2793 if (lh != nullptr)
2794 {
2795 for (const auto &entry : lh->file_names ())
2796 {
2797 std::string name_holder;
2798 const char *include_name =
2799 compute_include_file_name (lh.get (), entry, fnd, name_holder);
2800 if (include_name != nullptr)
2801 {
2802 include_name = per_objfile->objfile->intern (include_name);
2803 include_names.push_back (include_name);
2804 }
2805 }
2806 }
2807
2808 qfn->num_file_names = offset + include_names.size ();
2809 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
2810 qfn->file_names =
2811 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2812 qfn->num_file_names);
2813 if (offset != 0)
2814 qfn->file_names[0] = per_objfile->objfile->intern (fnd.get_name ());
2815
2816 if (!include_names.empty ())
2817 memcpy (&qfn->file_names[offset], include_names.data (),
2818 include_names.size () * sizeof (const char *));
2819
2820 qfn->real_names = NULL;
2821
2822 lh_cu->file_names = qfn;
2823 }
2824
2825 /* A helper for the "quick" functions which attempts to read the line
2826 table for THIS_CU. */
2827
2828 static struct quick_file_names *
2829 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2830 dwarf2_per_objfile *per_objfile)
2831 {
2832 /* This should never be called for TUs. */
2833 gdb_assert (! this_cu->is_debug_types);
2834
2835 if (this_cu->files_read)
2836 return this_cu->file_names;
2837
2838 cutu_reader reader (this_cu, per_objfile);
2839 if (!reader.dummy_p)
2840 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2841
2842 return this_cu->file_names;
2843 }
2844
2845 /* A helper for the "quick" functions which computes and caches the
2846 real path for a given file name from the line table. */
2847
2848 static const char *
2849 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2850 struct quick_file_names *qfn, int index)
2851 {
2852 if (qfn->real_names == NULL)
2853 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
2854 qfn->num_file_names, const char *);
2855
2856 if (qfn->real_names[index] == NULL)
2857 {
2858 const char *dirname = nullptr;
2859
2860 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
2861 dirname = qfn->comp_dir;
2862
2863 gdb::unique_xmalloc_ptr<char> fullname;
2864 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
2865
2866 qfn->real_names[index] = fullname.release ();
2867 }
2868
2869 return qfn->real_names[index];
2870 }
2871
2872 struct symtab *
2873 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
2874 {
2875 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2876 dwarf2_per_cu_data *dwarf_cu
2877 = per_objfile->per_bfd->all_units.back ().get ();
2878 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2879
2880 if (cust == NULL)
2881 return NULL;
2882
2883 return cust->primary_filetab ();
2884 }
2885
2886 /* See read.h. */
2887
2888 void
2889 dwarf2_per_cu_data::free_cached_file_names ()
2890 {
2891 if (fnd != nullptr)
2892 fnd->forget_fullname ();
2893
2894 if (per_bfd == nullptr)
2895 return;
2896
2897 struct quick_file_names *file_data = file_names;
2898 if (file_data != nullptr && file_data->real_names != nullptr)
2899 {
2900 for (int i = 0; i < file_data->num_file_names; ++i)
2901 {
2902 xfree ((void *) file_data->real_names[i]);
2903 file_data->real_names[i] = nullptr;
2904 }
2905 }
2906 }
2907
2908 void
2909 dwarf2_base_index_functions::forget_cached_source_info
2910 (struct objfile *objfile)
2911 {
2912 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2913
2914 for (auto &per_cu : per_objfile->per_bfd->all_units)
2915 per_cu->free_cached_file_names ();
2916 }
2917
2918 /* Struct used to manage iterating over all CUs looking for a symbol. */
2919
2920 struct dw2_symtab_iterator
2921 {
2922 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2923 dwarf2_per_objfile *per_objfile;
2924 /* If set, only look for symbols that match that block. Valid values are
2925 GLOBAL_BLOCK and STATIC_BLOCK. */
2926 gdb::optional<block_enum> block_index;
2927 /* The kind of symbol we're looking for. */
2928 domain_enum domain;
2929 /* The list of CUs from the index entry of the symbol,
2930 or NULL if not found. */
2931 offset_view vec;
2932 /* The next element in VEC to look at. */
2933 int next;
2934 /* The number of elements in VEC, or zero if there is no match. */
2935 int length;
2936 /* Have we seen a global version of the symbol?
2937 If so we can ignore all further global instances.
2938 This is to work around gold/15646, inefficient gold-generated
2939 indices. */
2940 int global_seen;
2941 };
2942
2943 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2944
2945 static void
2946 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
2947 dwarf2_per_objfile *per_objfile,
2948 gdb::optional<block_enum> block_index,
2949 domain_enum domain, offset_type namei,
2950 mapped_index &index)
2951 {
2952 iter->per_objfile = per_objfile;
2953 iter->block_index = block_index;
2954 iter->domain = domain;
2955 iter->next = 0;
2956 iter->global_seen = 0;
2957 iter->vec = {};
2958 iter->length = 0;
2959
2960 gdb_assert (!index.symbol_name_slot_invalid (namei));
2961 offset_type vec_idx = index.symbol_vec_index (namei);
2962
2963 iter->vec = offset_view (index.constant_pool.slice (vec_idx));
2964 iter->length = iter->vec[0];
2965 }
2966
2967 /* Return the next matching CU or NULL if there are no more. */
2968
2969 static struct dwarf2_per_cu_data *
2970 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter,
2971 mapped_index &index)
2972 {
2973 dwarf2_per_objfile *per_objfile = iter->per_objfile;
2974
2975 for ( ; iter->next < iter->length; ++iter->next)
2976 {
2977 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
2978 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2979 gdb_index_symbol_kind symbol_kind =
2980 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2981 /* Only check the symbol attributes if they're present.
2982 Indices prior to version 7 don't record them,
2983 and indices >= 7 may elide them for certain symbols
2984 (gold does this). */
2985 int attrs_valid = (index.version >= 7
2986 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
2987
2988 /* Don't crash on bad data. */
2989 if (cu_index >= per_objfile->per_bfd->all_units.size ())
2990 {
2991 complaint (_(".gdb_index entry has bad CU index"
2992 " [in module %s]"), objfile_name (per_objfile->objfile));
2993 continue;
2994 }
2995
2996 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
2997
2998 /* Skip if already read in. */
2999 if (per_objfile->symtab_set_p (per_cu))
3000 continue;
3001
3002 /* Check static vs global. */
3003 if (attrs_valid)
3004 {
3005 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3006
3007 if (iter->block_index.has_value ())
3008 {
3009 bool want_static = *iter->block_index == STATIC_BLOCK;
3010
3011 if (is_static != want_static)
3012 continue;
3013 }
3014
3015 /* Work around gold/15646. */
3016 if (!is_static
3017 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3018 {
3019 if (iter->global_seen)
3020 continue;
3021
3022 iter->global_seen = 1;
3023 }
3024 }
3025
3026 /* Only check the symbol's kind if it has one. */
3027 if (attrs_valid)
3028 {
3029 switch (iter->domain)
3030 {
3031 case VAR_DOMAIN:
3032 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3033 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3034 /* Some types are also in VAR_DOMAIN. */
3035 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3036 continue;
3037 break;
3038 case STRUCT_DOMAIN:
3039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3040 continue;
3041 break;
3042 case LABEL_DOMAIN:
3043 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3044 continue;
3045 break;
3046 case MODULE_DOMAIN:
3047 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3048 continue;
3049 break;
3050 default:
3051 break;
3052 }
3053 }
3054
3055 ++iter->next;
3056 return per_cu;
3057 }
3058
3059 return NULL;
3060 }
3061
3062 void
3063 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3064 bool print_bcache)
3065 {
3066 if (print_bcache)
3067 return;
3068
3069 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3070 int total = per_objfile->per_bfd->all_units.size ();
3071 int count = 0;
3072
3073 for (int i = 0; i < total; ++i)
3074 {
3075 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3076
3077 if (!per_objfile->symtab_set_p (per_cu))
3078 ++count;
3079 }
3080 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3081 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3082 }
3083
3084 /* This dumps minimal information about the index.
3085 It is called via "mt print objfiles".
3086 One use is to verify .gdb_index has been loaded by the
3087 gdb.dwarf2/gdb-index.exp testcase. */
3088
3089 void
3090 dwarf2_gdb_index::dump (struct objfile *objfile)
3091 {
3092 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3093
3094 mapped_index *index = (gdb::checked_static_cast<mapped_index *>
3095 (per_objfile->per_bfd->index_table.get ()));
3096 gdb_printf (".gdb_index: version %d\n", index->version);
3097 gdb_printf ("\n");
3098 }
3099
3100 void
3101 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3102 {
3103 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3104 int total_units = per_objfile->per_bfd->all_units.size ();
3105
3106 for (int i = 0; i < total_units; ++i)
3107 {
3108 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3109
3110 /* We don't want to directly expand a partial CU, because if we
3111 read it with the wrong language, then assertion failures can
3112 be triggered later on. See PR symtab/23010. So, tell
3113 dw2_instantiate_symtab to skip partial CUs -- any important
3114 partial CU will be read via DW_TAG_imported_unit anyway. */
3115 dw2_instantiate_symtab (per_cu, per_objfile, true);
3116 }
3117 }
3118
3119 static bool
3120 dw2_expand_symtabs_matching_symbol
3121 (mapped_index_base &index,
3122 const lookup_name_info &lookup_name_in,
3123 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3124 gdb::function_view<bool (offset_type)> match_callback,
3125 dwarf2_per_objfile *per_objfile);
3126
3127 static bool
3128 dw2_expand_symtabs_matching_one
3129 (dwarf2_per_cu_data *per_cu,
3130 dwarf2_per_objfile *per_objfile,
3131 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3132 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3133
3134 void
3135 dwarf2_gdb_index::expand_matching_symbols
3136 (struct objfile *objfile,
3137 const lookup_name_info &name, domain_enum domain,
3138 int global,
3139 symbol_compare_ftype *ordered_compare)
3140 {
3141 /* Used for Ada. */
3142 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3143
3144 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3145
3146 mapped_index &index
3147 = *(gdb::checked_static_cast<mapped_index *>
3148 (per_objfile->per_bfd->index_table.get ()));
3149
3150 const char *match_name = name.ada ().lookup_name ().c_str ();
3151 auto matcher = [&] (const char *symname)
3152 {
3153 if (ordered_compare == nullptr)
3154 return true;
3155 return ordered_compare (symname, match_name) == 0;
3156 };
3157
3158 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3159 [&] (offset_type namei)
3160 {
3161 struct dw2_symtab_iterator iter;
3162 struct dwarf2_per_cu_data *per_cu;
3163
3164 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain, namei,
3165 index);
3166 while ((per_cu = dw2_symtab_iter_next (&iter, index)) != NULL)
3167 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3168 nullptr);
3169 return true;
3170 }, per_objfile);
3171 }
3172
3173 /* Starting from a search name, return the string that finds the upper
3174 bound of all strings that start with SEARCH_NAME in a sorted name
3175 list. Returns the empty string to indicate that the upper bound is
3176 the end of the list. */
3177
3178 static std::string
3179 make_sort_after_prefix_name (const char *search_name)
3180 {
3181 /* When looking to complete "func", we find the upper bound of all
3182 symbols that start with "func" by looking for where we'd insert
3183 the closest string that would follow "func" in lexicographical
3184 order. Usually, that's "func"-with-last-character-incremented,
3185 i.e. "fund". Mind non-ASCII characters, though. Usually those
3186 will be UTF-8 multi-byte sequences, but we can't be certain.
3187 Especially mind the 0xff character, which is a valid character in
3188 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3189 rule out compilers allowing it in identifiers. Note that
3190 conveniently, strcmp/strcasecmp are specified to compare
3191 characters interpreted as unsigned char. So what we do is treat
3192 the whole string as a base 256 number composed of a sequence of
3193 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3194 to 0, and carries 1 to the following more-significant position.
3195 If the very first character in SEARCH_NAME ends up incremented
3196 and carries/overflows, then the upper bound is the end of the
3197 list. The string after the empty string is also the empty
3198 string.
3199
3200 Some examples of this operation:
3201
3202 SEARCH_NAME => "+1" RESULT
3203
3204 "abc" => "abd"
3205 "ab\xff" => "ac"
3206 "\xff" "a" "\xff" => "\xff" "b"
3207 "\xff" => ""
3208 "\xff\xff" => ""
3209 "" => ""
3210
3211 Then, with these symbols for example:
3212
3213 func
3214 func1
3215 fund
3216
3217 completing "func" looks for symbols between "func" and
3218 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3219 which finds "func" and "func1", but not "fund".
3220
3221 And with:
3222
3223 funcÿ (Latin1 'ÿ' [0xff])
3224 funcÿ1
3225 fund
3226
3227 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3228 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3229
3230 And with:
3231
3232 ÿÿ (Latin1 'ÿ' [0xff])
3233 ÿÿ1
3234
3235 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3236 the end of the list.
3237 */
3238 std::string after = search_name;
3239 while (!after.empty () && (unsigned char) after.back () == 0xff)
3240 after.pop_back ();
3241 if (!after.empty ())
3242 after.back () = (unsigned char) after.back () + 1;
3243 return after;
3244 }
3245
3246 /* See declaration. */
3247
3248 std::pair<std::vector<name_component>::const_iterator,
3249 std::vector<name_component>::const_iterator>
3250 mapped_index_base::find_name_components_bounds
3251 (const lookup_name_info &lookup_name_without_params, language lang,
3252 dwarf2_per_objfile *per_objfile) const
3253 {
3254 auto *name_cmp
3255 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3256
3257 const char *lang_name
3258 = lookup_name_without_params.language_lookup_name (lang);
3259
3260 /* Comparison function object for lower_bound that matches against a
3261 given symbol name. */
3262 auto lookup_compare_lower = [&] (const name_component &elem,
3263 const char *name)
3264 {
3265 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3266 const char *elem_name = elem_qualified + elem.name_offset;
3267 return name_cmp (elem_name, name) < 0;
3268 };
3269
3270 /* Comparison function object for upper_bound that matches against a
3271 given symbol name. */
3272 auto lookup_compare_upper = [&] (const char *name,
3273 const name_component &elem)
3274 {
3275 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3276 const char *elem_name = elem_qualified + elem.name_offset;
3277 return name_cmp (name, elem_name) < 0;
3278 };
3279
3280 auto begin = this->name_components.begin ();
3281 auto end = this->name_components.end ();
3282
3283 /* Find the lower bound. */
3284 auto lower = [&] ()
3285 {
3286 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3287 return begin;
3288 else
3289 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3290 } ();
3291
3292 /* Find the upper bound. */
3293 auto upper = [&] ()
3294 {
3295 if (lookup_name_without_params.completion_mode ())
3296 {
3297 /* In completion mode, we want UPPER to point past all
3298 symbols names that have the same prefix. I.e., with
3299 these symbols, and completing "func":
3300
3301 function << lower bound
3302 function1
3303 other_function << upper bound
3304
3305 We find the upper bound by looking for the insertion
3306 point of "func"-with-last-character-incremented,
3307 i.e. "fund". */
3308 std::string after = make_sort_after_prefix_name (lang_name);
3309 if (after.empty ())
3310 return end;
3311 return std::lower_bound (lower, end, after.c_str (),
3312 lookup_compare_lower);
3313 }
3314 else
3315 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3316 } ();
3317
3318 return {lower, upper};
3319 }
3320
3321 /* See declaration. */
3322
3323 void
3324 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3325 {
3326 if (!this->name_components.empty ())
3327 return;
3328
3329 this->name_components_casing = case_sensitivity;
3330 auto *name_cmp
3331 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3332
3333 /* The code below only knows how to break apart components of C++
3334 symbol names (and other languages that use '::' as
3335 namespace/module separator) and Ada symbol names. */
3336 auto count = this->symbol_name_count ();
3337 for (offset_type idx = 0; idx < count; idx++)
3338 {
3339 if (this->symbol_name_slot_invalid (idx))
3340 continue;
3341
3342 const char *name = this->symbol_name_at (idx, per_objfile);
3343
3344 /* Add each name component to the name component table. */
3345 unsigned int previous_len = 0;
3346
3347 if (strstr (name, "::") != nullptr)
3348 {
3349 for (unsigned int current_len = cp_find_first_component (name);
3350 name[current_len] != '\0';
3351 current_len += cp_find_first_component (name + current_len))
3352 {
3353 gdb_assert (name[current_len] == ':');
3354 this->name_components.push_back ({previous_len, idx});
3355 /* Skip the '::'. */
3356 current_len += 2;
3357 previous_len = current_len;
3358 }
3359 }
3360 else
3361 {
3362 /* Handle the Ada encoded (aka mangled) form here. */
3363 for (const char *iter = strstr (name, "__");
3364 iter != nullptr;
3365 iter = strstr (iter, "__"))
3366 {
3367 this->name_components.push_back ({previous_len, idx});
3368 iter += 2;
3369 previous_len = iter - name;
3370 }
3371 }
3372
3373 this->name_components.push_back ({previous_len, idx});
3374 }
3375
3376 /* Sort name_components elements by name. */
3377 auto name_comp_compare = [&] (const name_component &left,
3378 const name_component &right)
3379 {
3380 const char *left_qualified
3381 = this->symbol_name_at (left.idx, per_objfile);
3382 const char *right_qualified
3383 = this->symbol_name_at (right.idx, per_objfile);
3384
3385 const char *left_name = left_qualified + left.name_offset;
3386 const char *right_name = right_qualified + right.name_offset;
3387
3388 return name_cmp (left_name, right_name) < 0;
3389 };
3390
3391 std::sort (this->name_components.begin (),
3392 this->name_components.end (),
3393 name_comp_compare);
3394 }
3395
3396 /* Helper for dw2_expand_symtabs_matching that works with a
3397 mapped_index_base instead of the containing objfile. This is split
3398 to a separate function in order to be able to unit test the
3399 name_components matching using a mock mapped_index_base. For each
3400 symbol name that matches, calls MATCH_CALLBACK, passing it the
3401 symbol's index in the mapped_index_base symbol table. */
3402
3403 static bool
3404 dw2_expand_symtabs_matching_symbol
3405 (mapped_index_base &index,
3406 const lookup_name_info &lookup_name_in,
3407 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3408 gdb::function_view<bool (offset_type)> match_callback,
3409 dwarf2_per_objfile *per_objfile)
3410 {
3411 lookup_name_info lookup_name_without_params
3412 = lookup_name_in.make_ignore_params ();
3413
3414 /* Build the symbol name component sorted vector, if we haven't
3415 yet. */
3416 index.build_name_components (per_objfile);
3417
3418 /* The same symbol may appear more than once in the range though.
3419 E.g., if we're looking for symbols that complete "w", and we have
3420 a symbol named "w1::w2", we'll find the two name components for
3421 that same symbol in the range. To be sure we only call the
3422 callback once per symbol, we first collect the symbol name
3423 indexes that matched in a temporary vector and ignore
3424 duplicates. */
3425 std::vector<offset_type> matches;
3426
3427 struct name_and_matcher
3428 {
3429 symbol_name_matcher_ftype *matcher;
3430 const char *name;
3431
3432 bool operator== (const name_and_matcher &other) const
3433 {
3434 return matcher == other.matcher && strcmp (name, other.name) == 0;
3435 }
3436 };
3437
3438 /* A vector holding all the different symbol name matchers, for all
3439 languages. */
3440 std::vector<name_and_matcher> matchers;
3441
3442 for (int i = 0; i < nr_languages; i++)
3443 {
3444 enum language lang_e = (enum language) i;
3445
3446 const language_defn *lang = language_def (lang_e);
3447 symbol_name_matcher_ftype *name_matcher
3448 = lang->get_symbol_name_matcher (lookup_name_without_params);
3449
3450 name_and_matcher key {
3451 name_matcher,
3452 lookup_name_without_params.language_lookup_name (lang_e)
3453 };
3454
3455 /* Don't insert the same comparison routine more than once.
3456 Note that we do this linear walk. This is not a problem in
3457 practice because the number of supported languages is
3458 low. */
3459 if (std::find (matchers.begin (), matchers.end (), key)
3460 != matchers.end ())
3461 continue;
3462 matchers.push_back (std::move (key));
3463
3464 auto bounds
3465 = index.find_name_components_bounds (lookup_name_without_params,
3466 lang_e, per_objfile);
3467
3468 /* Now for each symbol name in range, check to see if we have a name
3469 match, and if so, call the MATCH_CALLBACK callback. */
3470
3471 for (; bounds.first != bounds.second; ++bounds.first)
3472 {
3473 const char *qualified
3474 = index.symbol_name_at (bounds.first->idx, per_objfile);
3475
3476 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3477 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3478 continue;
3479
3480 matches.push_back (bounds.first->idx);
3481 }
3482 }
3483
3484 std::sort (matches.begin (), matches.end ());
3485
3486 /* Finally call the callback, once per match. */
3487 ULONGEST prev = -1;
3488 bool result = true;
3489 for (offset_type idx : matches)
3490 {
3491 if (prev != idx)
3492 {
3493 if (!match_callback (idx))
3494 {
3495 result = false;
3496 break;
3497 }
3498 prev = idx;
3499 }
3500 }
3501
3502 /* Above we use a type wider than idx's for 'prev', since 0 and
3503 (offset_type)-1 are both possible values. */
3504 static_assert (sizeof (prev) > sizeof (offset_type), "");
3505
3506 return result;
3507 }
3508
3509 #if GDB_SELF_TEST
3510
3511 namespace selftests { namespace dw2_expand_symtabs_matching {
3512
3513 /* A mock .gdb_index/.debug_names-like name index table, enough to
3514 exercise dw2_expand_symtabs_matching_symbol, which works with the
3515 mapped_index_base interface. Builds an index from the symbol list
3516 passed as parameter to the constructor. */
3517 class mock_mapped_index : public mapped_index_base
3518 {
3519 public:
3520 mock_mapped_index (gdb::array_view<const char *> symbols)
3521 : m_symbol_table (symbols)
3522 {}
3523
3524 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3525
3526 /* Return the number of names in the symbol table. */
3527 size_t symbol_name_count () const override
3528 {
3529 return m_symbol_table.size ();
3530 }
3531
3532 /* Get the name of the symbol at IDX in the symbol table. */
3533 const char *symbol_name_at
3534 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3535 {
3536 return m_symbol_table[idx];
3537 }
3538
3539 quick_symbol_functions_up make_quick_functions () const override
3540 {
3541 return nullptr;
3542 }
3543
3544 private:
3545 gdb::array_view<const char *> m_symbol_table;
3546 };
3547
3548 /* Convenience function that converts a NULL pointer to a "<null>"
3549 string, to pass to print routines. */
3550
3551 static const char *
3552 string_or_null (const char *str)
3553 {
3554 return str != NULL ? str : "<null>";
3555 }
3556
3557 /* Check if a lookup_name_info built from
3558 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3559 index. EXPECTED_LIST is the list of expected matches, in expected
3560 matching order. If no match expected, then an empty list is
3561 specified. Returns true on success. On failure prints a warning
3562 indicating the file:line that failed, and returns false. */
3563
3564 static bool
3565 check_match (const char *file, int line,
3566 mock_mapped_index &mock_index,
3567 const char *name, symbol_name_match_type match_type,
3568 bool completion_mode,
3569 std::initializer_list<const char *> expected_list,
3570 dwarf2_per_objfile *per_objfile)
3571 {
3572 lookup_name_info lookup_name (name, match_type, completion_mode);
3573
3574 bool matched = true;
3575
3576 auto mismatch = [&] (const char *expected_str,
3577 const char *got)
3578 {
3579 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3580 "expected=\"%s\", got=\"%s\"\n"),
3581 file, line,
3582 (match_type == symbol_name_match_type::FULL
3583 ? "FULL" : "WILD"),
3584 name, string_or_null (expected_str), string_or_null (got));
3585 matched = false;
3586 };
3587
3588 auto expected_it = expected_list.begin ();
3589 auto expected_end = expected_list.end ();
3590
3591 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3592 nullptr,
3593 [&] (offset_type idx)
3594 {
3595 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3596 const char *expected_str
3597 = expected_it == expected_end ? NULL : *expected_it++;
3598
3599 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3600 mismatch (expected_str, matched_name);
3601 return true;
3602 }, per_objfile);
3603
3604 const char *expected_str
3605 = expected_it == expected_end ? NULL : *expected_it++;
3606 if (expected_str != NULL)
3607 mismatch (expected_str, NULL);
3608
3609 return matched;
3610 }
3611
3612 /* The symbols added to the mock mapped_index for testing (in
3613 canonical form). */
3614 static const char *test_symbols[] = {
3615 "function",
3616 "std::bar",
3617 "std::zfunction",
3618 "std::zfunction2",
3619 "w1::w2",
3620 "ns::foo<char*>",
3621 "ns::foo<int>",
3622 "ns::foo<long>",
3623 "ns2::tmpl<int>::foo2",
3624 "(anonymous namespace)::A::B::C",
3625
3626 /* These are used to check that the increment-last-char in the
3627 matching algorithm for completion doesn't match "t1_fund" when
3628 completing "t1_func". */
3629 "t1_func",
3630 "t1_func1",
3631 "t1_fund",
3632 "t1_fund1",
3633
3634 /* A UTF-8 name with multi-byte sequences to make sure that
3635 cp-name-parser understands this as a single identifier ("função"
3636 is "function" in PT). */
3637 u8"u8função",
3638
3639 /* Test a symbol name that ends with a 0xff character, which is a
3640 valid character in non-UTF-8 source character sets (e.g. Latin1
3641 'ÿ'), and we can't rule out compilers allowing it in identifiers.
3642 We test this because the completion algorithm finds the upper
3643 bound of symbols by looking for the insertion point of
3644 "func"-with-last-character-incremented, i.e. "fund", and adding 1
3645 to 0xff should wraparound and carry to the previous character.
3646 See comments in make_sort_after_prefix_name. */
3647 "yfunc\377",
3648
3649 /* Some more symbols with \377 (0xff). See above. */
3650 "\377",
3651 "\377\377123",
3652
3653 /* A name with all sorts of complications. Starts with "z" to make
3654 it easier for the completion tests below. */
3655 #define Z_SYM_NAME \
3656 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3657 "::tuple<(anonymous namespace)::ui*, " \
3658 "std::default_delete<(anonymous namespace)::ui>, void>"
3659
3660 Z_SYM_NAME
3661 };
3662
3663 /* Returns true if the mapped_index_base::find_name_component_bounds
3664 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3665 in completion mode. */
3666
3667 static bool
3668 check_find_bounds_finds (mapped_index_base &index,
3669 const char *search_name,
3670 gdb::array_view<const char *> expected_syms,
3671 dwarf2_per_objfile *per_objfile)
3672 {
3673 lookup_name_info lookup_name (search_name,
3674 symbol_name_match_type::FULL, true);
3675
3676 auto bounds = index.find_name_components_bounds (lookup_name,
3677 language_cplus,
3678 per_objfile);
3679
3680 size_t distance = std::distance (bounds.first, bounds.second);
3681 if (distance != expected_syms.size ())
3682 return false;
3683
3684 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3685 {
3686 auto nc_elem = bounds.first + exp_elem;
3687 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3688 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3689 return false;
3690 }
3691
3692 return true;
3693 }
3694
3695 /* Test the lower-level mapped_index::find_name_component_bounds
3696 method. */
3697
3698 static void
3699 test_mapped_index_find_name_component_bounds ()
3700 {
3701 mock_mapped_index mock_index (test_symbols);
3702
3703 mock_index.build_name_components (NULL /* per_objfile */);
3704
3705 /* Test the lower-level mapped_index::find_name_component_bounds
3706 method in completion mode. */
3707 {
3708 static const char *expected_syms[] = {
3709 "t1_func",
3710 "t1_func1",
3711 };
3712
3713 SELF_CHECK (check_find_bounds_finds
3714 (mock_index, "t1_func", expected_syms,
3715 NULL /* per_objfile */));
3716 }
3717
3718 /* Check that the increment-last-char in the name matching algorithm
3719 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
3720 make_sort_after_prefix_name. */
3721 {
3722 static const char *expected_syms1[] = {
3723 "\377",
3724 "\377\377123",
3725 };
3726 SELF_CHECK (check_find_bounds_finds
3727 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3728
3729 static const char *expected_syms2[] = {
3730 "\377\377123",
3731 };
3732 SELF_CHECK (check_find_bounds_finds
3733 (mock_index, "\377\377", expected_syms2,
3734 NULL /* per_objfile */));
3735 }
3736 }
3737
3738 /* Test dw2_expand_symtabs_matching_symbol. */
3739
3740 static void
3741 test_dw2_expand_symtabs_matching_symbol ()
3742 {
3743 mock_mapped_index mock_index (test_symbols);
3744
3745 /* We let all tests run until the end even if some fails, for debug
3746 convenience. */
3747 bool any_mismatch = false;
3748
3749 /* Create the expected symbols list (an initializer_list). Needed
3750 because lists have commas, and we need to pass them to CHECK,
3751 which is a macro. */
3752 #define EXPECT(...) { __VA_ARGS__ }
3753
3754 /* Wrapper for check_match that passes down the current
3755 __FILE__/__LINE__. */
3756 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3757 any_mismatch |= !check_match (__FILE__, __LINE__, \
3758 mock_index, \
3759 NAME, MATCH_TYPE, COMPLETION_MODE, \
3760 EXPECTED_LIST, NULL)
3761
3762 /* Identity checks. */
3763 for (const char *sym : test_symbols)
3764 {
3765 /* Should be able to match all existing symbols. */
3766 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3767 EXPECT (sym));
3768
3769 /* Should be able to match all existing symbols with
3770 parameters. */
3771 std::string with_params = std::string (sym) + "(int)";
3772 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3773 EXPECT (sym));
3774
3775 /* Should be able to match all existing symbols with
3776 parameters and qualifiers. */
3777 with_params = std::string (sym) + " ( int ) const";
3778 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3779 EXPECT (sym));
3780
3781 /* This should really find sym, but cp-name-parser.y doesn't
3782 know about lvalue/rvalue qualifiers yet. */
3783 with_params = std::string (sym) + " ( int ) &&";
3784 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3785 {});
3786 }
3787
3788 /* Check that the name matching algorithm for completion doesn't get
3789 confused with Latin1 'ÿ' / 0xff. See
3790 make_sort_after_prefix_name. */
3791 {
3792 static const char str[] = "\377";
3793 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3794 EXPECT ("\377", "\377\377123"));
3795 }
3796
3797 /* Check that the increment-last-char in the matching algorithm for
3798 completion doesn't match "t1_fund" when completing "t1_func". */
3799 {
3800 static const char str[] = "t1_func";
3801 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3802 EXPECT ("t1_func", "t1_func1"));
3803 }
3804
3805 /* Check that completion mode works at each prefix of the expected
3806 symbol name. */
3807 {
3808 static const char str[] = "function(int)";
3809 size_t len = strlen (str);
3810 std::string lookup;
3811
3812 for (size_t i = 1; i < len; i++)
3813 {
3814 lookup.assign (str, i);
3815 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3816 EXPECT ("function"));
3817 }
3818 }
3819
3820 /* While "w" is a prefix of both components, the match function
3821 should still only be called once. */
3822 {
3823 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3824 EXPECT ("w1::w2"));
3825 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3826 EXPECT ("w1::w2"));
3827 }
3828
3829 /* Same, with a "complicated" symbol. */
3830 {
3831 static const char str[] = Z_SYM_NAME;
3832 size_t len = strlen (str);
3833 std::string lookup;
3834
3835 for (size_t i = 1; i < len; i++)
3836 {
3837 lookup.assign (str, i);
3838 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3839 EXPECT (Z_SYM_NAME));
3840 }
3841 }
3842
3843 /* In FULL mode, an incomplete symbol doesn't match. */
3844 {
3845 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3846 {});
3847 }
3848
3849 /* A complete symbol with parameters matches any overload, since the
3850 index has no overload info. */
3851 {
3852 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3853 EXPECT ("std::zfunction", "std::zfunction2"));
3854 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
3855 EXPECT ("std::zfunction", "std::zfunction2"));
3856 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
3857 EXPECT ("std::zfunction", "std::zfunction2"));
3858 }
3859
3860 /* Check that whitespace is ignored appropriately. A symbol with a
3861 template argument list. */
3862 {
3863 static const char expected[] = "ns::foo<int>";
3864 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
3865 EXPECT (expected));
3866 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
3867 EXPECT (expected));
3868 }
3869
3870 /* Check that whitespace is ignored appropriately. A symbol with a
3871 template argument list that includes a pointer. */
3872 {
3873 static const char expected[] = "ns::foo<char*>";
3874 /* Try both completion and non-completion modes. */
3875 static const bool completion_mode[2] = {false, true};
3876 for (size_t i = 0; i < 2; i++)
3877 {
3878 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
3879 completion_mode[i], EXPECT (expected));
3880 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
3881 completion_mode[i], EXPECT (expected));
3882
3883 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
3884 completion_mode[i], EXPECT (expected));
3885 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
3886 completion_mode[i], EXPECT (expected));
3887 }
3888 }
3889
3890 {
3891 /* Check method qualifiers are ignored. */
3892 static const char expected[] = "ns::foo<char*>";
3893 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3894 symbol_name_match_type::FULL, true, EXPECT (expected));
3895 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3896 symbol_name_match_type::FULL, true, EXPECT (expected));
3897 CHECK_MATCH ("foo < char * > ( int ) const",
3898 symbol_name_match_type::WILD, true, EXPECT (expected));
3899 CHECK_MATCH ("foo < char * > ( int ) &&",
3900 symbol_name_match_type::WILD, true, EXPECT (expected));
3901 }
3902
3903 /* Test lookup names that don't match anything. */
3904 {
3905 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
3906 {});
3907
3908 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
3909 {});
3910 }
3911
3912 /* Some wild matching tests, exercising "(anonymous namespace)",
3913 which should not be confused with a parameter list. */
3914 {
3915 static const char *syms[] = {
3916 "A::B::C",
3917 "B::C",
3918 "C",
3919 "A :: B :: C ( int )",
3920 "B :: C ( int )",
3921 "C ( int )",
3922 };
3923
3924 for (const char *s : syms)
3925 {
3926 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
3927 EXPECT ("(anonymous namespace)::A::B::C"));
3928 }
3929 }
3930
3931 {
3932 static const char expected[] = "ns2::tmpl<int>::foo2";
3933 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
3934 EXPECT (expected));
3935 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
3936 EXPECT (expected));
3937 }
3938
3939 SELF_CHECK (!any_mismatch);
3940
3941 #undef EXPECT
3942 #undef CHECK_MATCH
3943 }
3944
3945 static void
3946 run_test ()
3947 {
3948 test_mapped_index_find_name_component_bounds ();
3949 test_dw2_expand_symtabs_matching_symbol ();
3950 }
3951
3952 }} // namespace selftests::dw2_expand_symtabs_matching
3953
3954 #endif /* GDB_SELF_TEST */
3955
3956 /* If FILE_MATCHER is NULL or if PER_CU has
3957 dwarf2_per_cu_quick_data::MARK set (see
3958 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3959 EXPANSION_NOTIFY on it. */
3960
3961 static bool
3962 dw2_expand_symtabs_matching_one
3963 (dwarf2_per_cu_data *per_cu,
3964 dwarf2_per_objfile *per_objfile,
3965 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3966 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
3967 {
3968 if (file_matcher == NULL || per_cu->mark)
3969 {
3970 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
3971
3972 compunit_symtab *symtab
3973 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3974 gdb_assert (symtab != nullptr);
3975
3976 if (expansion_notify != NULL && symtab_was_null)
3977 return expansion_notify (symtab);
3978 }
3979 return true;
3980 }
3981
3982 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3983 matched, to expand corresponding CUs that were marked. IDX is the
3984 index of the symbol name that matched. */
3985
3986 static bool
3987 dw2_expand_marked_cus
3988 (dwarf2_per_objfile *per_objfile, offset_type idx,
3989 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3990 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
3991 block_search_flags search_flags,
3992 search_domain kind)
3993 {
3994 offset_type vec_len, vec_idx;
3995 bool global_seen = false;
3996 mapped_index &index
3997 = *(gdb::checked_static_cast<mapped_index *>
3998 (per_objfile->per_bfd->index_table.get ()));
3999
4000 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4001 vec_len = vec[0];
4002 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4003 {
4004 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4005 /* This value is only valid for index versions >= 7. */
4006 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4007 gdb_index_symbol_kind symbol_kind =
4008 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4009 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4010 /* Only check the symbol attributes if they're present.
4011 Indices prior to version 7 don't record them,
4012 and indices >= 7 may elide them for certain symbols
4013 (gold does this). */
4014 int attrs_valid =
4015 (index.version >= 7
4016 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4017
4018 /* Work around gold/15646. */
4019 if (attrs_valid
4020 && !is_static
4021 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4022 {
4023 if (global_seen)
4024 continue;
4025
4026 global_seen = true;
4027 }
4028
4029 /* Only check the symbol's kind if it has one. */
4030 if (attrs_valid)
4031 {
4032 if (is_static)
4033 {
4034 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4035 continue;
4036 }
4037 else
4038 {
4039 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4040 continue;
4041 }
4042
4043 switch (kind)
4044 {
4045 case VARIABLES_DOMAIN:
4046 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4047 continue;
4048 break;
4049 case FUNCTIONS_DOMAIN:
4050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4051 continue;
4052 break;
4053 case TYPES_DOMAIN:
4054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4055 continue;
4056 break;
4057 case MODULES_DOMAIN:
4058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4059 continue;
4060 break;
4061 default:
4062 break;
4063 }
4064 }
4065
4066 /* Don't crash on bad data. */
4067 if (cu_index >= per_objfile->per_bfd->all_units.size ())
4068 {
4069 complaint (_(".gdb_index entry has bad CU index"
4070 " [in module %s]"), objfile_name (per_objfile->objfile));
4071 continue;
4072 }
4073
4074 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4075 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4076 expansion_notify))
4077 return false;
4078 }
4079
4080 return true;
4081 }
4082
4083 /* If FILE_MATCHER is non-NULL, set all the
4084 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4085 that match FILE_MATCHER. */
4086
4087 static void
4088 dw_expand_symtabs_matching_file_matcher
4089 (dwarf2_per_objfile *per_objfile,
4090 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4091 {
4092 if (file_matcher == NULL)
4093 return;
4094
4095 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4096 htab_eq_pointer,
4097 NULL, xcalloc, xfree));
4098 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4099 htab_eq_pointer,
4100 NULL, xcalloc, xfree));
4101
4102 /* The rule is CUs specify all the files, including those used by
4103 any TU, so there's no need to scan TUs here. */
4104
4105 for (const auto &per_cu : per_objfile->per_bfd->all_units)
4106 {
4107 QUIT;
4108
4109 if (per_cu->is_debug_types)
4110 continue;
4111 per_cu->mark = 0;
4112
4113 /* We only need to look at symtabs not already expanded. */
4114 if (per_objfile->symtab_set_p (per_cu.get ()))
4115 continue;
4116
4117 if (per_cu->fnd != nullptr)
4118 {
4119 file_and_directory *fnd = per_cu->fnd.get ();
4120
4121 if (file_matcher (fnd->get_name (), false))
4122 {
4123 per_cu->mark = 1;
4124 continue;
4125 }
4126
4127 /* Before we invoke realpath, which can get expensive when many
4128 files are involved, do a quick comparison of the basenames. */
4129 if ((basenames_may_differ
4130 || file_matcher (lbasename (fnd->get_name ()), true))
4131 && file_matcher (fnd->get_fullname (), false))
4132 {
4133 per_cu->mark = 1;
4134 continue;
4135 }
4136 }
4137
4138 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4139 per_objfile);
4140 if (file_data == NULL)
4141 continue;
4142
4143 if (htab_find (visited_not_found.get (), file_data) != NULL)
4144 continue;
4145 else if (htab_find (visited_found.get (), file_data) != NULL)
4146 {
4147 per_cu->mark = 1;
4148 continue;
4149 }
4150
4151 for (int j = 0; j < file_data->num_file_names; ++j)
4152 {
4153 const char *this_real_name;
4154
4155 if (file_matcher (file_data->file_names[j], false))
4156 {
4157 per_cu->mark = 1;
4158 break;
4159 }
4160
4161 /* Before we invoke realpath, which can get expensive when many
4162 files are involved, do a quick comparison of the basenames. */
4163 if (!basenames_may_differ
4164 && !file_matcher (lbasename (file_data->file_names[j]),
4165 true))
4166 continue;
4167
4168 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4169 if (file_matcher (this_real_name, false))
4170 {
4171 per_cu->mark = 1;
4172 break;
4173 }
4174 }
4175
4176 void **slot = htab_find_slot (per_cu->mark
4177 ? visited_found.get ()
4178 : visited_not_found.get (),
4179 file_data, INSERT);
4180 *slot = file_data;
4181 }
4182 }
4183
4184 bool
4185 dwarf2_gdb_index::expand_symtabs_matching
4186 (struct objfile *objfile,
4187 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4188 const lookup_name_info *lookup_name,
4189 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4190 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4191 block_search_flags search_flags,
4192 domain_enum domain,
4193 enum search_domain kind)
4194 {
4195 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4196
4197 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4198
4199 /* This invariant is documented in quick-functions.h. */
4200 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4201 if (lookup_name == nullptr)
4202 {
4203 for (dwarf2_per_cu_data *per_cu
4204 : all_units_range (per_objfile->per_bfd))
4205 {
4206 QUIT;
4207
4208 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4209 file_matcher,
4210 expansion_notify))
4211 return false;
4212 }
4213 return true;
4214 }
4215
4216 mapped_index &index
4217 = *(gdb::checked_static_cast<mapped_index *>
4218 (per_objfile->per_bfd->index_table.get ()));
4219
4220 bool result
4221 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4222 symbol_matcher,
4223 [&] (offset_type idx)
4224 {
4225 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4226 expansion_notify, search_flags, kind))
4227 return false;
4228 return true;
4229 }, per_objfile);
4230
4231 return result;
4232 }
4233
4234 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4235 symtab. */
4236
4237 static struct compunit_symtab *
4238 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4239 CORE_ADDR pc)
4240 {
4241 int i;
4242
4243 if (cust->blockvector () != nullptr
4244 && blockvector_contains_pc (cust->blockvector (), pc))
4245 return cust;
4246
4247 if (cust->includes == NULL)
4248 return NULL;
4249
4250 for (i = 0; cust->includes[i]; ++i)
4251 {
4252 struct compunit_symtab *s = cust->includes[i];
4253
4254 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4255 if (s != NULL)
4256 return s;
4257 }
4258
4259 return NULL;
4260 }
4261
4262 dwarf2_per_cu_data *
4263 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
4264 CORE_ADDR adjusted_pc)
4265 {
4266 if (per_bfd->index_addrmap == nullptr)
4267 return nullptr;
4268
4269 void *obj = per_bfd->index_addrmap->find (adjusted_pc);
4270 return static_cast<dwarf2_per_cu_data *> (obj);
4271 }
4272
4273 struct compunit_symtab *
4274 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4275 (struct objfile *objfile,
4276 struct bound_minimal_symbol msymbol,
4277 CORE_ADDR pc,
4278 struct obj_section *section,
4279 int warn_if_readin)
4280 {
4281 struct compunit_symtab *result;
4282
4283 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4284
4285 CORE_ADDR baseaddr = objfile->text_section_offset ();
4286 struct dwarf2_per_cu_data *data = find_per_cu (per_objfile->per_bfd,
4287 pc - baseaddr);
4288 if (data == nullptr)
4289 return nullptr;
4290
4291 if (warn_if_readin && per_objfile->symtab_set_p (data))
4292 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4293 paddress (objfile->arch (), pc));
4294
4295 result = recursively_find_pc_sect_compunit_symtab
4296 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4297
4298 if (warn_if_readin && result == nullptr)
4299 warning (_("(Error: pc %s in address map, but not in symtab.)"),
4300 paddress (objfile->arch (), pc));
4301
4302 return result;
4303 }
4304
4305 void
4306 dwarf2_base_index_functions::map_symbol_filenames
4307 (struct objfile *objfile,
4308 gdb::function_view<symbol_filename_ftype> fun,
4309 bool need_fullname)
4310 {
4311 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4312
4313 /* Use caches to ensure we only call FUN once for each filename. */
4314 filename_seen_cache filenames_cache;
4315 std::unordered_set<quick_file_names *> qfn_cache;
4316
4317 /* The rule is CUs specify all the files, including those used by any TU,
4318 so there's no need to scan TUs here. We can ignore file names coming
4319 from already-expanded CUs. It is possible that an expanded CU might
4320 reuse the file names data from a currently unexpanded CU, in this
4321 case we don't want to report the files from the unexpanded CU. */
4322
4323 for (const auto &per_cu : per_objfile->per_bfd->all_units)
4324 {
4325 if (!per_cu->is_debug_types
4326 && per_objfile->symtab_set_p (per_cu.get ()))
4327 {
4328 if (per_cu->file_names != nullptr)
4329 qfn_cache.insert (per_cu->file_names);
4330 }
4331 }
4332
4333 for (dwarf2_per_cu_data *per_cu
4334 : all_units_range (per_objfile->per_bfd))
4335 {
4336 /* We only need to look at symtabs not already expanded. */
4337 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4338 continue;
4339
4340 if (per_cu->fnd != nullptr)
4341 {
4342 file_and_directory *fnd = per_cu->fnd.get ();
4343
4344 const char *filename = fnd->get_name ();
4345 const char *key = filename;
4346 const char *fullname = nullptr;
4347
4348 if (need_fullname)
4349 {
4350 fullname = fnd->get_fullname ();
4351 key = fullname;
4352 }
4353
4354 if (!filenames_cache.seen (key))
4355 fun (filename, fullname);
4356 }
4357
4358 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4359 if (file_data == nullptr
4360 || qfn_cache.find (file_data) != qfn_cache.end ())
4361 continue;
4362
4363 for (int j = 0; j < file_data->num_file_names; ++j)
4364 {
4365 const char *filename = file_data->file_names[j];
4366 const char *key = filename;
4367 const char *fullname = nullptr;
4368
4369 if (need_fullname)
4370 {
4371 fullname = dw2_get_real_path (per_objfile, file_data, j);
4372 key = fullname;
4373 }
4374
4375 if (!filenames_cache.seen (key))
4376 fun (filename, fullname);
4377 }
4378 }
4379 }
4380
4381 bool
4382 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4383 {
4384 return true;
4385 }
4386
4387 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4388
4389 bool
4390 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4391 {
4392 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4393
4394 for (const auto &per_cu : per_objfile->per_bfd->all_units)
4395 {
4396 /* Is this already expanded? */
4397 if (per_objfile->symtab_set_p (per_cu.get ()))
4398 continue;
4399
4400 /* It has not yet been expanded. */
4401 return true;
4402 }
4403
4404 return false;
4405 }
4406
4407 /* DWARF-5 debug_names reader. */
4408
4409 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4410 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4411
4412 /* A helper function that reads the .debug_names section in SECTION
4413 and fills in MAP. FILENAME is the name of the file containing the
4414 section; it is used for error reporting.
4415
4416 Returns true if all went well, false otherwise. */
4417
4418 static bool
4419 read_debug_names_from_section (struct objfile *objfile,
4420 const char *filename,
4421 struct dwarf2_section_info *section,
4422 mapped_debug_names &map)
4423 {
4424 if (section->empty ())
4425 return false;
4426
4427 /* Older elfutils strip versions could keep the section in the main
4428 executable while splitting it for the separate debug info file. */
4429 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4430 return false;
4431
4432 section->read (objfile);
4433
4434 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4435
4436 const gdb_byte *addr = section->buffer;
4437
4438 bfd *const abfd = section->get_bfd_owner ();
4439
4440 unsigned int bytes_read;
4441 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4442 addr += bytes_read;
4443
4444 map.dwarf5_is_dwarf64 = bytes_read != 4;
4445 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4446 if (bytes_read + length != section->size)
4447 {
4448 /* There may be multiple per-CU indices. */
4449 warning (_("Section .debug_names in %s length %s does not match "
4450 "section length %s, ignoring .debug_names."),
4451 filename, plongest (bytes_read + length),
4452 pulongest (section->size));
4453 return false;
4454 }
4455
4456 /* The version number. */
4457 uint16_t version = read_2_bytes (abfd, addr);
4458 addr += 2;
4459 if (version != 5)
4460 {
4461 warning (_("Section .debug_names in %s has unsupported version %d, "
4462 "ignoring .debug_names."),
4463 filename, version);
4464 return false;
4465 }
4466
4467 /* Padding. */
4468 uint16_t padding = read_2_bytes (abfd, addr);
4469 addr += 2;
4470 if (padding != 0)
4471 {
4472 warning (_("Section .debug_names in %s has unsupported padding %d, "
4473 "ignoring .debug_names."),
4474 filename, padding);
4475 return false;
4476 }
4477
4478 /* comp_unit_count - The number of CUs in the CU list. */
4479 map.cu_count = read_4_bytes (abfd, addr);
4480 addr += 4;
4481
4482 /* local_type_unit_count - The number of TUs in the local TU
4483 list. */
4484 map.tu_count = read_4_bytes (abfd, addr);
4485 addr += 4;
4486
4487 /* foreign_type_unit_count - The number of TUs in the foreign TU
4488 list. */
4489 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4490 addr += 4;
4491 if (foreign_tu_count != 0)
4492 {
4493 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4494 "ignoring .debug_names."),
4495 filename, static_cast<unsigned long> (foreign_tu_count));
4496 return false;
4497 }
4498
4499 /* bucket_count - The number of hash buckets in the hash lookup
4500 table. */
4501 map.bucket_count = read_4_bytes (abfd, addr);
4502 addr += 4;
4503
4504 /* name_count - The number of unique names in the index. */
4505 map.name_count = read_4_bytes (abfd, addr);
4506 addr += 4;
4507
4508 /* abbrev_table_size - The size in bytes of the abbreviations
4509 table. */
4510 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4511 addr += 4;
4512
4513 /* augmentation_string_size - The size in bytes of the augmentation
4514 string. This value is rounded up to a multiple of 4. */
4515 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4516 addr += 4;
4517 map.augmentation_is_gdb = ((augmentation_string_size
4518 == sizeof (dwarf5_augmentation))
4519 && memcmp (addr, dwarf5_augmentation,
4520 sizeof (dwarf5_augmentation)) == 0);
4521 augmentation_string_size += (-augmentation_string_size) & 3;
4522 addr += augmentation_string_size;
4523
4524 /* List of CUs */
4525 map.cu_table_reordered = addr;
4526 addr += map.cu_count * map.offset_size;
4527
4528 /* List of Local TUs */
4529 map.tu_table_reordered = addr;
4530 addr += map.tu_count * map.offset_size;
4531
4532 /* Hash Lookup Table */
4533 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4534 addr += map.bucket_count * 4;
4535 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4536 addr += map.name_count * 4;
4537
4538 /* Name Table */
4539 map.name_table_string_offs_reordered = addr;
4540 addr += map.name_count * map.offset_size;
4541 map.name_table_entry_offs_reordered = addr;
4542 addr += map.name_count * map.offset_size;
4543
4544 const gdb_byte *abbrev_table_start = addr;
4545 for (;;)
4546 {
4547 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4548 addr += bytes_read;
4549 if (index_num == 0)
4550 break;
4551
4552 const auto insertpair
4553 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4554 if (!insertpair.second)
4555 {
4556 warning (_("Section .debug_names in %s has duplicate index %s, "
4557 "ignoring .debug_names."),
4558 filename, pulongest (index_num));
4559 return false;
4560 }
4561 mapped_debug_names::index_val &indexval = insertpair.first->second;
4562 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4563 addr += bytes_read;
4564
4565 for (;;)
4566 {
4567 mapped_debug_names::index_val::attr attr;
4568 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4569 addr += bytes_read;
4570 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4571 addr += bytes_read;
4572 if (attr.form == DW_FORM_implicit_const)
4573 {
4574 attr.implicit_const = read_signed_leb128 (abfd, addr,
4575 &bytes_read);
4576 addr += bytes_read;
4577 }
4578 if (attr.dw_idx == 0 && attr.form == 0)
4579 break;
4580 indexval.attr_vec.push_back (std::move (attr));
4581 }
4582 }
4583 if (addr != abbrev_table_start + abbrev_table_size)
4584 {
4585 warning (_("Section .debug_names in %s has abbreviation_table "
4586 "of size %s vs. written as %u, ignoring .debug_names."),
4587 filename, plongest (addr - abbrev_table_start),
4588 abbrev_table_size);
4589 return false;
4590 }
4591 map.entry_pool = addr;
4592
4593 return true;
4594 }
4595
4596 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4597 list. */
4598
4599 static bool
4600 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4601 const mapped_debug_names &map,
4602 dwarf2_section_info &section,
4603 bool is_dwz)
4604 {
4605 if (!map.augmentation_is_gdb)
4606 {
4607 for (uint32_t i = 0; i < map.cu_count; ++i)
4608 {
4609 sect_offset sect_off
4610 = (sect_offset) (extract_unsigned_integer
4611 (map.cu_table_reordered + i * map.offset_size,
4612 map.offset_size,
4613 map.dwarf5_byte_order));
4614 /* We don't know the length of the CU, because the CU list in a
4615 .debug_names index can be incomplete, so we can't use the start
4616 of the next CU as end of this CU. We create the CUs here with
4617 length 0, and in cutu_reader::cutu_reader we'll fill in the
4618 actual length. */
4619 dwarf2_per_cu_data_up per_cu
4620 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4621 sect_off, 0);
4622 per_bfd->all_units.push_back (std::move (per_cu));
4623 }
4624 return true;
4625 }
4626
4627 sect_offset sect_off_prev;
4628 for (uint32_t i = 0; i <= map.cu_count; ++i)
4629 {
4630 sect_offset sect_off_next;
4631 if (i < map.cu_count)
4632 {
4633 sect_off_next
4634 = (sect_offset) (extract_unsigned_integer
4635 (map.cu_table_reordered + i * map.offset_size,
4636 map.offset_size,
4637 map.dwarf5_byte_order));
4638 }
4639 else
4640 sect_off_next = (sect_offset) section.size;
4641 if (i >= 1)
4642 {
4643 if (sect_off_next == sect_off_prev)
4644 {
4645 warning (_("Section .debug_names has duplicate entry in CU table,"
4646 " ignoring .debug_names."));
4647 return false;
4648 }
4649 if (sect_off_next < sect_off_prev)
4650 {
4651 warning (_("Section .debug_names has non-ascending CU table,"
4652 " ignoring .debug_names."));
4653 return false;
4654 }
4655 /* Note: we're not using length = sect_off_next - sect_off_prev,
4656 to gracefully handle an incomplete CU list. */
4657 const ULONGEST length = 0;
4658 dwarf2_per_cu_data_up per_cu
4659 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4660 sect_off_prev, length);
4661 per_bfd->all_units.push_back (std::move (per_cu));
4662 }
4663 sect_off_prev = sect_off_next;
4664 }
4665
4666 return true;
4667 }
4668
4669 /* Read the CU list from the mapped index, and use it to create all
4670 the CU objects for this dwarf2_per_objfile. */
4671
4672 static bool
4673 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4674 const mapped_debug_names &map,
4675 const mapped_debug_names &dwz_map)
4676 {
4677 gdb_assert (per_bfd->all_units.empty ());
4678 per_bfd->all_units.reserve (map.cu_count + dwz_map.cu_count);
4679
4680 if (!create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4681 false /* is_dwz */))
4682 return false;
4683
4684 if (dwz_map.cu_count == 0)
4685 return true;
4686
4687 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4688 return create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4689 true /* is_dwz */);
4690 }
4691
4692 /* Read .debug_names. If everything went ok, initialize the "quick"
4693 elements of all the CUs and return true. Otherwise, return false. */
4694
4695 static bool
4696 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4697 {
4698 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4699 mapped_debug_names dwz_map;
4700 struct objfile *objfile = per_objfile->objfile;
4701 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4702
4703 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4704 &per_bfd->debug_names, *map))
4705 return false;
4706
4707 /* Don't use the index if it's empty. */
4708 if (map->name_count == 0)
4709 return false;
4710
4711 /* If there is a .dwz file, read it so we can get its CU list as
4712 well. */
4713 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4714 if (dwz != NULL)
4715 {
4716 if (!read_debug_names_from_section (objfile,
4717 bfd_get_filename (dwz->dwz_bfd.get ()),
4718 &dwz->debug_names, dwz_map))
4719 {
4720 warning (_("could not read '.debug_names' section from %s; skipping"),
4721 bfd_get_filename (dwz->dwz_bfd.get ()));
4722 return false;
4723 }
4724 }
4725
4726 if (!create_cus_from_debug_names (per_bfd, *map, dwz_map))
4727 {
4728 per_bfd->all_units.clear ();
4729 return false;
4730 }
4731
4732 if (map->tu_count != 0)
4733 {
4734 /* We can only handle a single .debug_types when we have an
4735 index. */
4736 if (per_bfd->types.size () > 1)
4737 {
4738 per_bfd->all_units.clear ();
4739 return false;
4740 }
4741
4742 dwarf2_section_info *section
4743 = (per_bfd->types.size () == 1
4744 ? &per_bfd->types[0]
4745 : &per_bfd->info);
4746
4747 create_signatured_type_table_from_debug_names
4748 (per_objfile, *map, section, &per_bfd->abbrev);
4749 }
4750
4751 finalize_all_units (per_bfd);
4752
4753 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4754
4755 per_bfd->index_table = std::move (map);
4756 per_bfd->quick_file_names_table =
4757 create_quick_file_names_table (per_bfd->all_units.size ());
4758
4759 return true;
4760 }
4761
4762 /* Type used to manage iterating over all CUs looking for a symbol for
4763 .debug_names. */
4764
4765 class dw2_debug_names_iterator
4766 {
4767 public:
4768 dw2_debug_names_iterator (const mapped_debug_names &map,
4769 block_search_flags block_index,
4770 domain_enum domain,
4771 const char *name, dwarf2_per_objfile *per_objfile)
4772 : m_map (map), m_block_index (block_index), m_domain (domain),
4773 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4774 m_per_objfile (per_objfile)
4775 {}
4776
4777 dw2_debug_names_iterator (const mapped_debug_names &map,
4778 search_domain search, uint32_t namei,
4779 dwarf2_per_objfile *per_objfile,
4780 domain_enum domain = UNDEF_DOMAIN)
4781 : m_map (map),
4782 m_domain (domain),
4783 m_search (search),
4784 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4785 m_per_objfile (per_objfile)
4786 {}
4787
4788 dw2_debug_names_iterator (const mapped_debug_names &map,
4789 block_search_flags block_index, domain_enum domain,
4790 uint32_t namei, dwarf2_per_objfile *per_objfile)
4791 : m_map (map), m_block_index (block_index), m_domain (domain),
4792 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4793 m_per_objfile (per_objfile)
4794 {}
4795
4796 /* Return the next matching CU or NULL if there are no more. */
4797 dwarf2_per_cu_data *next ();
4798
4799 private:
4800 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4801 const char *name,
4802 dwarf2_per_objfile *per_objfile);
4803 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4804 uint32_t namei,
4805 dwarf2_per_objfile *per_objfile);
4806
4807 /* The internalized form of .debug_names. */
4808 const mapped_debug_names &m_map;
4809
4810 /* Restrict the search to these blocks. */
4811 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4812 | SEARCH_STATIC_BLOCK);
4813
4814 /* The kind of symbol we're looking for. */
4815 const domain_enum m_domain = UNDEF_DOMAIN;
4816 const search_domain m_search = ALL_DOMAIN;
4817
4818 /* The list of CUs from the index entry of the symbol, or NULL if
4819 not found. */
4820 const gdb_byte *m_addr;
4821
4822 dwarf2_per_objfile *m_per_objfile;
4823 };
4824
4825 const char *
4826 mapped_debug_names::namei_to_name
4827 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4828 {
4829 const ULONGEST namei_string_offs
4830 = extract_unsigned_integer ((name_table_string_offs_reordered
4831 + namei * offset_size),
4832 offset_size,
4833 dwarf5_byte_order);
4834 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4835 }
4836
4837 /* Find a slot in .debug_names for the object named NAME. If NAME is
4838 found, return pointer to its pool data. If NAME cannot be found,
4839 return NULL. */
4840
4841 const gdb_byte *
4842 dw2_debug_names_iterator::find_vec_in_debug_names
4843 (const mapped_debug_names &map, const char *name,
4844 dwarf2_per_objfile *per_objfile)
4845 {
4846 int (*cmp) (const char *, const char *);
4847
4848 gdb::unique_xmalloc_ptr<char> without_params;
4849 if (current_language->la_language == language_cplus
4850 || current_language->la_language == language_fortran
4851 || current_language->la_language == language_d)
4852 {
4853 /* NAME is already canonical. Drop any qualifiers as
4854 .debug_names does not contain any. */
4855
4856 if (strchr (name, '(') != NULL)
4857 {
4858 without_params = cp_remove_params (name);
4859 if (without_params != NULL)
4860 name = without_params.get ();
4861 }
4862 }
4863
4864 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4865
4866 const uint32_t full_hash = dwarf5_djb_hash (name);
4867 uint32_t namei
4868 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4869 (map.bucket_table_reordered
4870 + (full_hash % map.bucket_count)), 4,
4871 map.dwarf5_byte_order);
4872 if (namei == 0)
4873 return NULL;
4874 --namei;
4875 if (namei >= map.name_count)
4876 {
4877 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4878 "[in module %s]"),
4879 namei, map.name_count,
4880 objfile_name (per_objfile->objfile));
4881 return NULL;
4882 }
4883
4884 for (;;)
4885 {
4886 const uint32_t namei_full_hash
4887 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4888 (map.hash_table_reordered + namei), 4,
4889 map.dwarf5_byte_order);
4890 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4891 return NULL;
4892
4893 if (full_hash == namei_full_hash)
4894 {
4895 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4896
4897 #if 0 /* An expensive sanity check. */
4898 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4899 {
4900 complaint (_("Wrong .debug_names hash for string at index %u "
4901 "[in module %s]"),
4902 namei, objfile_name (dwarf2_per_objfile->objfile));
4903 return NULL;
4904 }
4905 #endif
4906
4907 if (cmp (namei_string, name) == 0)
4908 {
4909 const ULONGEST namei_entry_offs
4910 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4911 + namei * map.offset_size),
4912 map.offset_size, map.dwarf5_byte_order);
4913 return map.entry_pool + namei_entry_offs;
4914 }
4915 }
4916
4917 ++namei;
4918 if (namei >= map.name_count)
4919 return NULL;
4920 }
4921 }
4922
4923 const gdb_byte *
4924 dw2_debug_names_iterator::find_vec_in_debug_names
4925 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4926 {
4927 if (namei >= map.name_count)
4928 {
4929 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4930 "[in module %s]"),
4931 namei, map.name_count,
4932 objfile_name (per_objfile->objfile));
4933 return NULL;
4934 }
4935
4936 const ULONGEST namei_entry_offs
4937 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4938 + namei * map.offset_size),
4939 map.offset_size, map.dwarf5_byte_order);
4940 return map.entry_pool + namei_entry_offs;
4941 }
4942
4943 /* See dw2_debug_names_iterator. */
4944
4945 dwarf2_per_cu_data *
4946 dw2_debug_names_iterator::next ()
4947 {
4948 if (m_addr == NULL)
4949 return NULL;
4950
4951 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4952 struct objfile *objfile = m_per_objfile->objfile;
4953 bfd *const abfd = objfile->obfd.get ();
4954
4955 again:
4956
4957 unsigned int bytes_read;
4958 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4959 m_addr += bytes_read;
4960 if (abbrev == 0)
4961 return NULL;
4962
4963 const auto indexval_it = m_map.abbrev_map.find (abbrev);
4964 if (indexval_it == m_map.abbrev_map.cend ())
4965 {
4966 complaint (_("Wrong .debug_names undefined abbrev code %s "
4967 "[in module %s]"),
4968 pulongest (abbrev), objfile_name (objfile));
4969 return NULL;
4970 }
4971 const mapped_debug_names::index_val &indexval = indexval_it->second;
4972 enum class symbol_linkage {
4973 unknown,
4974 static_,
4975 extern_,
4976 } symbol_linkage_ = symbol_linkage::unknown;
4977 dwarf2_per_cu_data *per_cu = NULL;
4978 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
4979 {
4980 ULONGEST ull;
4981 switch (attr.form)
4982 {
4983 case DW_FORM_implicit_const:
4984 ull = attr.implicit_const;
4985 break;
4986 case DW_FORM_flag_present:
4987 ull = 1;
4988 break;
4989 case DW_FORM_udata:
4990 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4991 m_addr += bytes_read;
4992 break;
4993 case DW_FORM_ref4:
4994 ull = read_4_bytes (abfd, m_addr);
4995 m_addr += 4;
4996 break;
4997 case DW_FORM_ref8:
4998 ull = read_8_bytes (abfd, m_addr);
4999 m_addr += 8;
5000 break;
5001 case DW_FORM_ref_sig8:
5002 ull = read_8_bytes (abfd, m_addr);
5003 m_addr += 8;
5004 break;
5005 default:
5006 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5007 dwarf_form_name (attr.form),
5008 objfile_name (objfile));
5009 return NULL;
5010 }
5011 switch (attr.dw_idx)
5012 {
5013 case DW_IDX_compile_unit:
5014 {
5015 /* Don't crash on bad data. */
5016 if (ull >= per_bfd->all_comp_units.size ())
5017 {
5018 complaint (_(".debug_names entry has bad CU index %s"
5019 " [in module %s]"),
5020 pulongest (ull),
5021 objfile_name (objfile));
5022 continue;
5023 }
5024 }
5025 per_cu = per_bfd->get_cu (ull);
5026 break;
5027 case DW_IDX_type_unit:
5028 /* Don't crash on bad data. */
5029 if (ull >= per_bfd->all_type_units.size ())
5030 {
5031 complaint (_(".debug_names entry has bad TU index %s"
5032 " [in module %s]"),
5033 pulongest (ull),
5034 objfile_name (objfile));
5035 continue;
5036 }
5037 {
5038 int nr_cus = per_bfd->all_comp_units.size ();
5039 per_cu = per_bfd->get_cu (nr_cus + ull);
5040 }
5041 break;
5042 case DW_IDX_die_offset:
5043 /* In a per-CU index (as opposed to a per-module index), index
5044 entries without CU attribute implicitly refer to the single CU. */
5045 if (per_cu == NULL)
5046 per_cu = per_bfd->get_cu (0);
5047 break;
5048 case DW_IDX_GNU_internal:
5049 if (!m_map.augmentation_is_gdb)
5050 break;
5051 symbol_linkage_ = symbol_linkage::static_;
5052 break;
5053 case DW_IDX_GNU_external:
5054 if (!m_map.augmentation_is_gdb)
5055 break;
5056 symbol_linkage_ = symbol_linkage::extern_;
5057 break;
5058 }
5059 }
5060
5061 /* Skip if we couldn't find a valid CU/TU index. */
5062 if (per_cu == nullptr)
5063 goto again;
5064
5065 /* Skip if already read in. */
5066 if (m_per_objfile->symtab_set_p (per_cu))
5067 goto again;
5068
5069 /* Check static vs global. */
5070 if (symbol_linkage_ != symbol_linkage::unknown)
5071 {
5072 if (symbol_linkage_ == symbol_linkage::static_)
5073 {
5074 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5075 goto again;
5076 }
5077 else
5078 {
5079 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5080 goto again;
5081 }
5082 }
5083
5084 /* Match dw2_symtab_iter_next, symbol_kind
5085 and debug_names::psymbol_tag. */
5086 switch (m_domain)
5087 {
5088 case VAR_DOMAIN:
5089 switch (indexval.dwarf_tag)
5090 {
5091 case DW_TAG_variable:
5092 case DW_TAG_subprogram:
5093 /* Some types are also in VAR_DOMAIN. */
5094 case DW_TAG_typedef:
5095 case DW_TAG_structure_type:
5096 break;
5097 default:
5098 goto again;
5099 }
5100 break;
5101 case STRUCT_DOMAIN:
5102 switch (indexval.dwarf_tag)
5103 {
5104 case DW_TAG_typedef:
5105 case DW_TAG_structure_type:
5106 break;
5107 default:
5108 goto again;
5109 }
5110 break;
5111 case LABEL_DOMAIN:
5112 switch (indexval.dwarf_tag)
5113 {
5114 case 0:
5115 case DW_TAG_variable:
5116 break;
5117 default:
5118 goto again;
5119 }
5120 break;
5121 case MODULE_DOMAIN:
5122 switch (indexval.dwarf_tag)
5123 {
5124 case DW_TAG_module:
5125 break;
5126 default:
5127 goto again;
5128 }
5129 break;
5130 default:
5131 break;
5132 }
5133
5134 /* Match dw2_expand_symtabs_matching, symbol_kind and
5135 debug_names::psymbol_tag. */
5136 switch (m_search)
5137 {
5138 case VARIABLES_DOMAIN:
5139 switch (indexval.dwarf_tag)
5140 {
5141 case DW_TAG_variable:
5142 break;
5143 default:
5144 goto again;
5145 }
5146 break;
5147 case FUNCTIONS_DOMAIN:
5148 switch (indexval.dwarf_tag)
5149 {
5150 case DW_TAG_subprogram:
5151 break;
5152 default:
5153 goto again;
5154 }
5155 break;
5156 case TYPES_DOMAIN:
5157 switch (indexval.dwarf_tag)
5158 {
5159 case DW_TAG_typedef:
5160 case DW_TAG_structure_type:
5161 break;
5162 default:
5163 goto again;
5164 }
5165 break;
5166 case MODULES_DOMAIN:
5167 switch (indexval.dwarf_tag)
5168 {
5169 case DW_TAG_module:
5170 break;
5171 default:
5172 goto again;
5173 }
5174 default:
5175 break;
5176 }
5177
5178 return per_cu;
5179 }
5180
5181 /* This dumps minimal information about .debug_names. It is called
5182 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5183 uses this to verify that .debug_names has been loaded. */
5184
5185 void
5186 dwarf2_debug_names_index::dump (struct objfile *objfile)
5187 {
5188 gdb_printf (".debug_names: exists\n");
5189 }
5190
5191 void
5192 dwarf2_debug_names_index::expand_matching_symbols
5193 (struct objfile *objfile,
5194 const lookup_name_info &name, domain_enum domain,
5195 int global,
5196 symbol_compare_ftype *ordered_compare)
5197 {
5198 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5199
5200 mapped_debug_names &map
5201 = *(gdb::checked_static_cast<mapped_debug_names *>
5202 (per_objfile->per_bfd->index_table.get ()));
5203 const block_search_flags block_flags
5204 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5205
5206 const char *match_name = name.ada ().lookup_name ().c_str ();
5207 auto matcher = [&] (const char *symname)
5208 {
5209 if (ordered_compare == nullptr)
5210 return true;
5211 return ordered_compare (symname, match_name) == 0;
5212 };
5213
5214 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5215 [&] (offset_type namei)
5216 {
5217 /* The name was matched, now expand corresponding CUs that were
5218 marked. */
5219 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5220 per_objfile);
5221
5222 struct dwarf2_per_cu_data *per_cu;
5223 while ((per_cu = iter.next ()) != NULL)
5224 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5225 nullptr);
5226 return true;
5227 }, per_objfile);
5228 }
5229
5230 bool
5231 dwarf2_debug_names_index::expand_symtabs_matching
5232 (struct objfile *objfile,
5233 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5234 const lookup_name_info *lookup_name,
5235 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5236 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5237 block_search_flags search_flags,
5238 domain_enum domain,
5239 enum search_domain kind)
5240 {
5241 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5242
5243 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5244
5245 /* This invariant is documented in quick-functions.h. */
5246 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5247 if (lookup_name == nullptr)
5248 {
5249 for (dwarf2_per_cu_data *per_cu
5250 : all_units_range (per_objfile->per_bfd))
5251 {
5252 QUIT;
5253
5254 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5255 file_matcher,
5256 expansion_notify))
5257 return false;
5258 }
5259 return true;
5260 }
5261
5262 mapped_debug_names &map
5263 = *(gdb::checked_static_cast<mapped_debug_names *>
5264 (per_objfile->per_bfd->index_table.get ()));
5265
5266 bool result
5267 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5268 symbol_matcher,
5269 [&] (offset_type namei)
5270 {
5271 /* The name was matched, now expand corresponding CUs that were
5272 marked. */
5273 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5274
5275 struct dwarf2_per_cu_data *per_cu;
5276 while ((per_cu = iter.next ()) != NULL)
5277 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5278 file_matcher,
5279 expansion_notify))
5280 return false;
5281 return true;
5282 }, per_objfile);
5283
5284 return result;
5285 }
5286
5287 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5288 to either a dwarf2_per_bfd or dwz_file object. */
5289
5290 template <typename T>
5291 static gdb::array_view<const gdb_byte>
5292 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5293 {
5294 dwarf2_section_info *section = &section_owner->gdb_index;
5295
5296 if (section->empty ())
5297 return {};
5298
5299 /* Older elfutils strip versions could keep the section in the main
5300 executable while splitting it for the separate debug info file. */
5301 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5302 return {};
5303
5304 section->read (obj);
5305
5306 /* dwarf2_section_info::size is a bfd_size_type, while
5307 gdb::array_view works with size_t. On 32-bit hosts, with
5308 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5309 is 32-bit. So we need an explicit narrowing conversion here.
5310 This is fine, because it's impossible to allocate or mmap an
5311 array/buffer larger than what size_t can represent. */
5312 return gdb::make_array_view (section->buffer, section->size);
5313 }
5314
5315 /* Lookup the index cache for the contents of the index associated to
5316 DWARF2_OBJ. */
5317
5318 static gdb::array_view<const gdb_byte>
5319 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5320 {
5321 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
5322 if (build_id == nullptr)
5323 return {};
5324
5325 return global_index_cache.lookup_gdb_index (build_id,
5326 &dwarf2_per_bfd->index_cache_res);
5327 }
5328
5329 /* Same as the above, but for DWZ. */
5330
5331 static gdb::array_view<const gdb_byte>
5332 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5333 {
5334 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5335 if (build_id == nullptr)
5336 return {};
5337
5338 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5339 }
5340
5341 static quick_symbol_functions_up make_cooked_index_funcs ();
5342
5343 /* See dwarf2/public.h. */
5344
5345 void
5346 dwarf2_initialize_objfile (struct objfile *objfile)
5347 {
5348 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5349 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5350
5351 dwarf_read_debug_printf ("called");
5352
5353 /* If we're about to read full symbols, don't bother with the
5354 indices. In this case we also don't care if some other debug
5355 format is making psymtabs, because they are all about to be
5356 expanded anyway. */
5357 if ((objfile->flags & OBJF_READNOW))
5358 {
5359 dwarf_read_debug_printf ("readnow requested");
5360
5361 create_all_units (per_objfile);
5362 per_bfd->quick_file_names_table
5363 = create_quick_file_names_table (per_bfd->all_units.size ());
5364
5365 objfile->qf.emplace_front (new readnow_functions);
5366 return;
5367 }
5368
5369 /* Was a GDB index already read when we processed an objfile sharing
5370 PER_BFD? */
5371 if (per_bfd->index_table != nullptr)
5372 {
5373 dwarf_read_debug_printf ("re-using symbols");
5374 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5375 return;
5376 }
5377
5378 if (dwarf2_read_debug_names (per_objfile))
5379 {
5380 dwarf_read_debug_printf ("found debug names");
5381 objfile->qf.push_front
5382 (per_bfd->index_table->make_quick_functions ());
5383 return;
5384 }
5385
5386 if (dwarf2_read_gdb_index (per_objfile,
5387 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5388 get_gdb_index_contents_from_section<dwz_file>))
5389 {
5390 dwarf_read_debug_printf ("found gdb index from file");
5391 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5392 return;
5393 }
5394
5395 /* ... otherwise, try to find the index in the index cache. */
5396 if (dwarf2_read_gdb_index (per_objfile,
5397 get_gdb_index_contents_from_cache,
5398 get_gdb_index_contents_from_cache_dwz))
5399 {
5400 dwarf_read_debug_printf ("found gdb index from cache");
5401 global_index_cache.hit ();
5402 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5403 return;
5404 }
5405
5406 global_index_cache.miss ();
5407 objfile->qf.push_front (make_cooked_index_funcs ());
5408 }
5409
5410 \f
5411
5412 /* Build a partial symbol table. */
5413
5414 static void
5415 dwarf2_build_psymtabs (struct objfile *objfile)
5416 {
5417 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5418
5419 if (per_objfile->per_bfd->index_table != nullptr)
5420 return;
5421
5422 try
5423 {
5424 dwarf2_build_psymtabs_hard (per_objfile);
5425
5426 /* (maybe) store an index in the cache. */
5427 global_index_cache.store (per_objfile);
5428 }
5429 catch (const gdb_exception_error &except)
5430 {
5431 exception_print (gdb_stderr, except);
5432 }
5433 }
5434
5435 /* Find the base address of the compilation unit for range lists and
5436 location lists. It will normally be specified by DW_AT_low_pc.
5437 In DWARF-3 draft 4, the base address could be overridden by
5438 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5439 compilation units with discontinuous ranges. */
5440
5441 static void
5442 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5443 {
5444 struct attribute *attr;
5445
5446 cu->base_address.reset ();
5447
5448 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5449 if (attr != nullptr)
5450 cu->base_address = attr->as_address ();
5451 else
5452 {
5453 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5454 if (attr != nullptr)
5455 cu->base_address = attr->as_address ();
5456 }
5457 }
5458
5459 /* Helper function that returns the proper abbrev section for
5460 THIS_CU. */
5461
5462 static struct dwarf2_section_info *
5463 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5464 {
5465 struct dwarf2_section_info *abbrev;
5466 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5467
5468 if (this_cu->is_dwz)
5469 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5470 else
5471 abbrev = &per_bfd->abbrev;
5472
5473 return abbrev;
5474 }
5475
5476 /* Fetch the abbreviation table offset from a comp or type unit header. */
5477
5478 static sect_offset
5479 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5480 struct dwarf2_section_info *section,
5481 sect_offset sect_off)
5482 {
5483 bfd *abfd = section->get_bfd_owner ();
5484 const gdb_byte *info_ptr;
5485 unsigned int initial_length_size, offset_size;
5486 uint16_t version;
5487
5488 section->read (per_objfile->objfile);
5489 info_ptr = section->buffer + to_underlying (sect_off);
5490 read_initial_length (abfd, info_ptr, &initial_length_size);
5491 offset_size = initial_length_size == 4 ? 4 : 8;
5492 info_ptr += initial_length_size;
5493
5494 version = read_2_bytes (abfd, info_ptr);
5495 info_ptr += 2;
5496 if (version >= 5)
5497 {
5498 /* Skip unit type and address size. */
5499 info_ptr += 2;
5500 }
5501
5502 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5503 }
5504
5505 static hashval_t
5506 hash_signatured_type (const void *item)
5507 {
5508 const struct signatured_type *sig_type
5509 = (const struct signatured_type *) item;
5510
5511 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5512 return sig_type->signature;
5513 }
5514
5515 static int
5516 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5517 {
5518 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5519 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5520
5521 return lhs->signature == rhs->signature;
5522 }
5523
5524 /* Allocate a hash table for signatured types. */
5525
5526 static htab_up
5527 allocate_signatured_type_table ()
5528 {
5529 return htab_up (htab_create_alloc (41,
5530 hash_signatured_type,
5531 eq_signatured_type,
5532 NULL, xcalloc, xfree));
5533 }
5534
5535 /* A helper for create_debug_types_hash_table. Read types from SECTION
5536 and fill them into TYPES_HTAB. It will process only type units,
5537 therefore DW_UT_type. */
5538
5539 static void
5540 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5541 struct dwo_file *dwo_file,
5542 dwarf2_section_info *section, htab_up &types_htab,
5543 rcuh_kind section_kind)
5544 {
5545 struct objfile *objfile = per_objfile->objfile;
5546 struct dwarf2_section_info *abbrev_section;
5547 bfd *abfd;
5548 const gdb_byte *info_ptr, *end_ptr;
5549
5550 abbrev_section = &dwo_file->sections.abbrev;
5551
5552 dwarf_read_debug_printf ("Reading %s for %s",
5553 section->get_name (),
5554 abbrev_section->get_file_name ());
5555
5556 section->read (objfile);
5557 info_ptr = section->buffer;
5558
5559 if (info_ptr == NULL)
5560 return;
5561
5562 /* We can't set abfd until now because the section may be empty or
5563 not present, in which case the bfd is unknown. */
5564 abfd = section->get_bfd_owner ();
5565
5566 /* We don't use cutu_reader here because we don't need to read
5567 any dies: the signature is in the header. */
5568
5569 end_ptr = info_ptr + section->size;
5570 while (info_ptr < end_ptr)
5571 {
5572 signatured_type_up sig_type;
5573 struct dwo_unit *dwo_tu;
5574 void **slot;
5575 const gdb_byte *ptr = info_ptr;
5576 struct comp_unit_head header;
5577 unsigned int length;
5578
5579 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5580
5581 /* Initialize it due to a false compiler warning. */
5582 header.signature = -1;
5583 header.type_cu_offset_in_tu = (cu_offset) -1;
5584
5585 /* We need to read the type's signature in order to build the hash
5586 table, but we don't need anything else just yet. */
5587
5588 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5589 abbrev_section, ptr, section_kind);
5590
5591 length = header.get_length_with_initial ();
5592
5593 /* Skip dummy type units. */
5594 if (ptr >= info_ptr + length
5595 || peek_abbrev_code (abfd, ptr) == 0
5596 || (header.unit_type != DW_UT_type
5597 && header.unit_type != DW_UT_split_type))
5598 {
5599 info_ptr += length;
5600 continue;
5601 }
5602
5603 if (types_htab == NULL)
5604 types_htab = allocate_dwo_unit_table ();
5605
5606 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5607 dwo_tu->dwo_file = dwo_file;
5608 dwo_tu->signature = header.signature;
5609 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5610 dwo_tu->section = section;
5611 dwo_tu->sect_off = sect_off;
5612 dwo_tu->length = length;
5613
5614 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5615 gdb_assert (slot != NULL);
5616 if (*slot != NULL)
5617 complaint (_("debug type entry at offset %s is duplicate to"
5618 " the entry at offset %s, signature %s"),
5619 sect_offset_str (sect_off),
5620 sect_offset_str (dwo_tu->sect_off),
5621 hex_string (header.signature));
5622 *slot = dwo_tu;
5623
5624 dwarf_read_debug_printf_v (" offset %s, signature %s",
5625 sect_offset_str (sect_off),
5626 hex_string (header.signature));
5627
5628 info_ptr += length;
5629 }
5630 }
5631
5632 /* Create the hash table of all entries in the .debug_types
5633 (or .debug_types.dwo) section(s).
5634 DWO_FILE is a pointer to the DWO file object.
5635
5636 The result is a pointer to the hash table or NULL if there are no types.
5637
5638 Note: This function processes DWO files only, not DWP files. */
5639
5640 static void
5641 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5642 struct dwo_file *dwo_file,
5643 gdb::array_view<dwarf2_section_info> type_sections,
5644 htab_up &types_htab)
5645 {
5646 for (dwarf2_section_info &section : type_sections)
5647 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5648 rcuh_kind::TYPE);
5649 }
5650
5651 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5652 If SLOT is non-NULL, it is the entry to use in the hash table.
5653 Otherwise we find one. */
5654
5655 static struct signatured_type *
5656 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5657 {
5658 if (per_objfile->per_bfd->all_units.size ()
5659 == per_objfile->per_bfd->all_units.capacity ())
5660 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5661
5662 signatured_type_up sig_type_holder
5663 = per_objfile->per_bfd->allocate_signatured_type (sig);
5664 signatured_type *sig_type = sig_type_holder.get ();
5665
5666 per_objfile->per_bfd->all_units.emplace_back
5667 (sig_type_holder.release ());
5668
5669 if (slot == NULL)
5670 {
5671 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5672 sig_type, INSERT);
5673 }
5674 gdb_assert (*slot == NULL);
5675 *slot = sig_type;
5676 /* The rest of sig_type must be filled in by the caller. */
5677 return sig_type;
5678 }
5679
5680 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5681 Fill in SIG_ENTRY with DWO_ENTRY. */
5682
5683 static void
5684 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5685 struct signatured_type *sig_entry,
5686 struct dwo_unit *dwo_entry)
5687 {
5688 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5689
5690 /* Make sure we're not clobbering something we don't expect to. */
5691 gdb_assert (! sig_entry->queued);
5692 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5693 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5694 gdb_assert (sig_entry->signature == dwo_entry->signature);
5695 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
5696 || (to_underlying (sig_entry->type_offset_in_section)
5697 == to_underlying (dwo_entry->type_offset_in_tu)));
5698 gdb_assert (sig_entry->type_unit_group == NULL);
5699 gdb_assert (sig_entry->dwo_unit == NULL
5700 || sig_entry->dwo_unit == dwo_entry);
5701
5702 sig_entry->section = dwo_entry->section;
5703 sig_entry->sect_off = dwo_entry->sect_off;
5704 sig_entry->set_length (dwo_entry->length, false);
5705 sig_entry->reading_dwo_directly = 1;
5706 sig_entry->per_bfd = per_bfd;
5707 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5708 sig_entry->dwo_unit = dwo_entry;
5709 }
5710
5711 /* Subroutine of lookup_signatured_type.
5712 If we haven't read the TU yet, create the signatured_type data structure
5713 for a TU to be read in directly from a DWO file, bypassing the stub.
5714 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5715 using .gdb_index, then when reading a CU we want to stay in the DWO file
5716 containing that CU. Otherwise we could end up reading several other DWO
5717 files (due to comdat folding) to process the transitive closure of all the
5718 mentioned TUs, and that can be slow. The current DWO file will have every
5719 type signature that it needs.
5720 We only do this for .gdb_index because in the psymtab case we already have
5721 to read all the DWOs to build the type unit groups. */
5722
5723 static struct signatured_type *
5724 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5725 {
5726 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5727 struct dwo_file *dwo_file;
5728 struct dwo_unit find_dwo_entry, *dwo_entry;
5729 void **slot;
5730
5731 gdb_assert (cu->dwo_unit);
5732
5733 /* If TU skeletons have been removed then we may not have read in any
5734 TUs yet. */
5735 if (per_objfile->per_bfd->signatured_types == NULL)
5736 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5737
5738 /* We only ever need to read in one copy of a signatured type.
5739 Use the global signatured_types array to do our own comdat-folding
5740 of types. If this is the first time we're reading this TU, and
5741 the TU has an entry in .gdb_index, replace the recorded data from
5742 .gdb_index with this TU. */
5743
5744 signatured_type find_sig_entry (sig);
5745 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5746 &find_sig_entry, INSERT);
5747 signatured_type *sig_entry = (struct signatured_type *) *slot;
5748
5749 /* We can get here with the TU already read, *or* in the process of being
5750 read. Don't reassign the global entry to point to this DWO if that's
5751 the case. Also note that if the TU is already being read, it may not
5752 have come from a DWO, the program may be a mix of Fission-compiled
5753 code and non-Fission-compiled code. */
5754
5755 /* Have we already tried to read this TU?
5756 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5757 needn't exist in the global table yet). */
5758 if (sig_entry != NULL && sig_entry->tu_read)
5759 return sig_entry;
5760
5761 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5762 dwo_unit of the TU itself. */
5763 dwo_file = cu->dwo_unit->dwo_file;
5764
5765 /* Ok, this is the first time we're reading this TU. */
5766 if (dwo_file->tus == NULL)
5767 return NULL;
5768 find_dwo_entry.signature = sig;
5769 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
5770 &find_dwo_entry);
5771 if (dwo_entry == NULL)
5772 return NULL;
5773
5774 /* If the global table doesn't have an entry for this TU, add one. */
5775 if (sig_entry == NULL)
5776 sig_entry = add_type_unit (per_objfile, sig, slot);
5777
5778 if (sig_entry->dwo_unit == nullptr)
5779 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5780 sig_entry->tu_read = 1;
5781 return sig_entry;
5782 }
5783
5784 /* Subroutine of lookup_signatured_type.
5785 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5786 then try the DWP file. If the TU stub (skeleton) has been removed then
5787 it won't be in .gdb_index. */
5788
5789 static struct signatured_type *
5790 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5791 {
5792 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5793 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
5794 struct dwo_unit *dwo_entry;
5795 void **slot;
5796
5797 gdb_assert (cu->dwo_unit);
5798 gdb_assert (dwp_file != NULL);
5799
5800 /* If TU skeletons have been removed then we may not have read in any
5801 TUs yet. */
5802 if (per_objfile->per_bfd->signatured_types == NULL)
5803 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5804
5805 signatured_type find_sig_entry (sig);
5806 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5807 &find_sig_entry, INSERT);
5808 signatured_type *sig_entry = (struct signatured_type *) *slot;
5809
5810 /* Have we already tried to read this TU?
5811 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5812 needn't exist in the global table yet). */
5813 if (sig_entry != NULL)
5814 return sig_entry;
5815
5816 if (dwp_file->tus == NULL)
5817 return NULL;
5818 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
5819 1 /* is_debug_types */);
5820 if (dwo_entry == NULL)
5821 return NULL;
5822
5823 sig_entry = add_type_unit (per_objfile, sig, slot);
5824 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5825
5826 return sig_entry;
5827 }
5828
5829 /* Lookup a signature based type for DW_FORM_ref_sig8.
5830 Returns NULL if signature SIG is not present in the table.
5831 It is up to the caller to complain about this. */
5832
5833 static struct signatured_type *
5834 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5835 {
5836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5837
5838 if (cu->dwo_unit)
5839 {
5840 /* We're in a DWO/DWP file, and we're using .gdb_index.
5841 These cases require special processing. */
5842 if (get_dwp_file (per_objfile) == NULL)
5843 return lookup_dwo_signatured_type (cu, sig);
5844 else
5845 return lookup_dwp_signatured_type (cu, sig);
5846 }
5847 else
5848 {
5849 if (per_objfile->per_bfd->signatured_types == NULL)
5850 return NULL;
5851 signatured_type find_entry (sig);
5852 return ((struct signatured_type *)
5853 htab_find (per_objfile->per_bfd->signatured_types.get (),
5854 &find_entry));
5855 }
5856 }
5857
5858 /* Low level DIE reading support. */
5859
5860 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5861
5862 static void
5863 init_cu_die_reader (struct die_reader_specs *reader,
5864 struct dwarf2_cu *cu,
5865 struct dwarf2_section_info *section,
5866 struct dwo_file *dwo_file,
5867 struct abbrev_table *abbrev_table)
5868 {
5869 gdb_assert (section->readin && section->buffer != NULL);
5870 reader->abfd = section->get_bfd_owner ();
5871 reader->cu = cu;
5872 reader->dwo_file = dwo_file;
5873 reader->die_section = section;
5874 reader->buffer = section->buffer;
5875 reader->buffer_end = section->buffer + section->size;
5876 reader->abbrev_table = abbrev_table;
5877 }
5878
5879 /* Subroutine of cutu_reader to simplify it.
5880 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5881 There's just a lot of work to do, and cutu_reader is big enough
5882 already.
5883
5884 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5885 from it to the DIE in the DWO. If NULL we are skipping the stub.
5886 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5887 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5888 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5889 STUB_COMP_DIR may be non-NULL.
5890 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5891 are filled in with the info of the DIE from the DWO file.
5892 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5893 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5894 kept around for at least as long as *RESULT_READER.
5895
5896 The result is non-zero if a valid (non-dummy) DIE was found. */
5897
5898 static int
5899 read_cutu_die_from_dwo (dwarf2_cu *cu,
5900 struct dwo_unit *dwo_unit,
5901 struct die_info *stub_comp_unit_die,
5902 const char *stub_comp_dir,
5903 struct die_reader_specs *result_reader,
5904 const gdb_byte **result_info_ptr,
5905 struct die_info **result_comp_unit_die,
5906 abbrev_table_up *result_dwo_abbrev_table)
5907 {
5908 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5909 dwarf2_per_cu_data *per_cu = cu->per_cu;
5910 struct objfile *objfile = per_objfile->objfile;
5911 bfd *abfd;
5912 const gdb_byte *begin_info_ptr, *info_ptr;
5913 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5914 int i,num_extra_attrs;
5915 struct dwarf2_section_info *dwo_abbrev_section;
5916 struct die_info *comp_unit_die;
5917
5918 /* At most one of these may be provided. */
5919 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5920
5921 /* These attributes aren't processed until later:
5922 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5923 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5924 referenced later. However, these attributes are found in the stub
5925 which we won't have later. In order to not impose this complication
5926 on the rest of the code, we read them here and copy them to the
5927 DWO CU/TU die. */
5928
5929 stmt_list = NULL;
5930 low_pc = NULL;
5931 high_pc = NULL;
5932 ranges = NULL;
5933 comp_dir = NULL;
5934
5935 if (stub_comp_unit_die != NULL)
5936 {
5937 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5938 DWO file. */
5939 if (!per_cu->is_debug_types)
5940 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5941 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5942 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5943 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5944 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5945
5946 cu->addr_base = stub_comp_unit_die->addr_base ();
5947
5948 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5949 We need the value before we can process DW_AT_ranges values from the
5950 DWO. */
5951 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
5952
5953 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5954 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5955 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5956 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5957 section. */
5958 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
5959 }
5960 else if (stub_comp_dir != NULL)
5961 {
5962 /* Reconstruct the comp_dir attribute to simplify the code below. */
5963 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
5964 comp_dir->name = DW_AT_comp_dir;
5965 comp_dir->form = DW_FORM_string;
5966 comp_dir->set_string_noncanonical (stub_comp_dir);
5967 }
5968
5969 /* Set up for reading the DWO CU/TU. */
5970 cu->dwo_unit = dwo_unit;
5971 dwarf2_section_info *section = dwo_unit->section;
5972 section->read (objfile);
5973 abfd = section->get_bfd_owner ();
5974 begin_info_ptr = info_ptr = (section->buffer
5975 + to_underlying (dwo_unit->sect_off));
5976 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5977
5978 if (per_cu->is_debug_types)
5979 {
5980 signatured_type *sig_type = (struct signatured_type *) per_cu;
5981
5982 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5983 section, dwo_abbrev_section,
5984 info_ptr, rcuh_kind::TYPE);
5985 /* This is not an assert because it can be caused by bad debug info. */
5986 if (sig_type->signature != cu->header.signature)
5987 {
5988 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5989 " TU at offset %s [in module %s]"),
5990 hex_string (sig_type->signature),
5991 hex_string (cu->header.signature),
5992 sect_offset_str (dwo_unit->sect_off),
5993 bfd_get_filename (abfd));
5994 }
5995 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5996 /* For DWOs coming from DWP files, we don't know the CU length
5997 nor the type's offset in the TU until now. */
5998 dwo_unit->length = cu->header.get_length_with_initial ();
5999 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6000
6001 /* Establish the type offset that can be used to lookup the type.
6002 For DWO files, we don't know it until now. */
6003 sig_type->type_offset_in_section
6004 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6005 }
6006 else
6007 {
6008 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6009 section, dwo_abbrev_section,
6010 info_ptr, rcuh_kind::COMPILE);
6011 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6012 /* For DWOs coming from DWP files, we don't know the CU length
6013 until now. */
6014 dwo_unit->length = cu->header.get_length_with_initial ();
6015 }
6016
6017 dwo_abbrev_section->read (objfile);
6018 *result_dwo_abbrev_table
6019 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6020 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6021 result_dwo_abbrev_table->get ());
6022
6023 /* Read in the die, but leave space to copy over the attributes
6024 from the stub. This has the benefit of simplifying the rest of
6025 the code - all the work to maintain the illusion of a single
6026 DW_TAG_{compile,type}_unit DIE is done here. */
6027 num_extra_attrs = ((stmt_list != NULL)
6028 + (low_pc != NULL)
6029 + (high_pc != NULL)
6030 + (ranges != NULL)
6031 + (comp_dir != NULL));
6032 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6033 num_extra_attrs);
6034
6035 /* Copy over the attributes from the stub to the DIE we just read in. */
6036 comp_unit_die = *result_comp_unit_die;
6037 i = comp_unit_die->num_attrs;
6038 if (stmt_list != NULL)
6039 comp_unit_die->attrs[i++] = *stmt_list;
6040 if (low_pc != NULL)
6041 comp_unit_die->attrs[i++] = *low_pc;
6042 if (high_pc != NULL)
6043 comp_unit_die->attrs[i++] = *high_pc;
6044 if (ranges != NULL)
6045 comp_unit_die->attrs[i++] = *ranges;
6046 if (comp_dir != NULL)
6047 comp_unit_die->attrs[i++] = *comp_dir;
6048 comp_unit_die->num_attrs += num_extra_attrs;
6049
6050 if (dwarf_die_debug)
6051 {
6052 gdb_printf (gdb_stdlog,
6053 "Read die from %s@0x%x of %s:\n",
6054 section->get_name (),
6055 (unsigned) (begin_info_ptr - section->buffer),
6056 bfd_get_filename (abfd));
6057 comp_unit_die->dump (dwarf_die_debug);
6058 }
6059
6060 /* Skip dummy compilation units. */
6061 if (info_ptr >= begin_info_ptr + dwo_unit->length
6062 || peek_abbrev_code (abfd, info_ptr) == 0)
6063 return 0;
6064
6065 *result_info_ptr = info_ptr;
6066 return 1;
6067 }
6068
6069 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6070 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6071 signature is part of the header. */
6072 static gdb::optional<ULONGEST>
6073 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6074 {
6075 if (cu->header.version >= 5)
6076 return cu->header.signature;
6077 struct attribute *attr;
6078 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6079 if (attr == nullptr || !attr->form_is_unsigned ())
6080 return gdb::optional<ULONGEST> ();
6081 return attr->as_unsigned ();
6082 }
6083
6084 /* Subroutine of cutu_reader to simplify it.
6085 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6086 Returns NULL if the specified DWO unit cannot be found. */
6087
6088 static struct dwo_unit *
6089 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6090 {
6091 #if CXX_STD_THREAD
6092 /* We need a lock here both to handle the DWO hash table, and BFD,
6093 which is not thread-safe. */
6094 static std::mutex dwo_lock;
6095
6096 std::lock_guard<std::mutex> guard (dwo_lock);
6097 #endif
6098
6099 dwarf2_per_cu_data *per_cu = cu->per_cu;
6100 struct dwo_unit *dwo_unit;
6101 const char *comp_dir;
6102
6103 gdb_assert (cu != NULL);
6104
6105 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6106 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6107 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6108
6109 if (per_cu->is_debug_types)
6110 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6111 else
6112 {
6113 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6114
6115 if (!signature.has_value ())
6116 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6117 " [in module %s]"),
6118 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6119
6120 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6121 }
6122
6123 return dwo_unit;
6124 }
6125
6126 /* Subroutine of cutu_reader to simplify it.
6127 See it for a description of the parameters.
6128 Read a TU directly from a DWO file, bypassing the stub. */
6129
6130 void
6131 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6132 dwarf2_per_objfile *per_objfile,
6133 dwarf2_cu *existing_cu)
6134 {
6135 struct signatured_type *sig_type;
6136
6137 /* Verify we can do the following downcast, and that we have the
6138 data we need. */
6139 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6140 sig_type = (struct signatured_type *) this_cu;
6141 gdb_assert (sig_type->dwo_unit != NULL);
6142
6143 dwarf2_cu *cu;
6144
6145 if (existing_cu != nullptr)
6146 {
6147 cu = existing_cu;
6148 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6149 /* There's no need to do the rereading_dwo_cu handling that
6150 cutu_reader does since we don't read the stub. */
6151 }
6152 else
6153 {
6154 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6155 in per_objfile yet. */
6156 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6157 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6158 cu = m_new_cu.get ();
6159 }
6160
6161 /* A future optimization, if needed, would be to use an existing
6162 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6163 could share abbrev tables. */
6164
6165 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6166 NULL /* stub_comp_unit_die */,
6167 sig_type->dwo_unit->dwo_file->comp_dir,
6168 this, &info_ptr,
6169 &comp_unit_die,
6170 &m_dwo_abbrev_table) == 0)
6171 {
6172 /* Dummy die. */
6173 dummy_p = true;
6174 }
6175 }
6176
6177 /* Initialize a CU (or TU) and read its DIEs.
6178 If the CU defers to a DWO file, read the DWO file as well.
6179
6180 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6181 Otherwise the table specified in the comp unit header is read in and used.
6182 This is an optimization for when we already have the abbrev table.
6183
6184 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6185 allocated. */
6186
6187 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6188 dwarf2_per_objfile *per_objfile,
6189 struct abbrev_table *abbrev_table,
6190 dwarf2_cu *existing_cu,
6191 bool skip_partial,
6192 abbrev_cache *cache)
6193 : die_reader_specs {},
6194 m_this_cu (this_cu)
6195 {
6196 struct objfile *objfile = per_objfile->objfile;
6197 struct dwarf2_section_info *section = this_cu->section;
6198 bfd *abfd = section->get_bfd_owner ();
6199 const gdb_byte *begin_info_ptr;
6200 struct signatured_type *sig_type = NULL;
6201 struct dwarf2_section_info *abbrev_section;
6202 /* Non-zero if CU currently points to a DWO file and we need to
6203 reread it. When this happens we need to reread the skeleton die
6204 before we can reread the DWO file (this only applies to CUs, not TUs). */
6205 int rereading_dwo_cu = 0;
6206
6207 if (dwarf_die_debug)
6208 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6209 this_cu->is_debug_types ? "type" : "comp",
6210 sect_offset_str (this_cu->sect_off));
6211
6212 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6213 file (instead of going through the stub), short-circuit all of this. */
6214 if (this_cu->reading_dwo_directly)
6215 {
6216 /* Narrow down the scope of possibilities to have to understand. */
6217 gdb_assert (this_cu->is_debug_types);
6218 gdb_assert (abbrev_table == NULL);
6219 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6220 return;
6221 }
6222
6223 /* This is cheap if the section is already read in. */
6224 section->read (objfile);
6225
6226 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6227
6228 abbrev_section = get_abbrev_section_for_cu (this_cu);
6229
6230 dwarf2_cu *cu;
6231
6232 if (existing_cu != nullptr)
6233 {
6234 cu = existing_cu;
6235 /* If this CU is from a DWO file we need to start over, we need to
6236 refetch the attributes from the skeleton CU.
6237 This could be optimized by retrieving those attributes from when we
6238 were here the first time: the previous comp_unit_die was stored in
6239 comp_unit_obstack. But there's no data yet that we need this
6240 optimization. */
6241 if (cu->dwo_unit != NULL)
6242 rereading_dwo_cu = 1;
6243 }
6244 else
6245 {
6246 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6247 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6248 let us know that the CU is being scanned using the parallel
6249 indexer. This assert is avoided in this case because (1) it
6250 is irrelevant, and (2) the get_cu method is not
6251 thread-safe. */
6252 gdb_assert (cache != nullptr
6253 || per_objfile->get_cu (this_cu) == nullptr);
6254 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6255 cu = m_new_cu.get ();
6256 }
6257
6258 /* Get the header. */
6259 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6260 {
6261 /* We already have the header, there's no need to read it in again. */
6262 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6263 }
6264 else
6265 {
6266 if (this_cu->is_debug_types)
6267 {
6268 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6269 section, abbrev_section,
6270 info_ptr, rcuh_kind::TYPE);
6271
6272 /* Since per_cu is the first member of struct signatured_type,
6273 we can go from a pointer to one to a pointer to the other. */
6274 sig_type = (struct signatured_type *) this_cu;
6275 gdb_assert (sig_type->signature == cu->header.signature);
6276 gdb_assert (sig_type->type_offset_in_tu
6277 == cu->header.type_cu_offset_in_tu);
6278 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6279
6280 /* LENGTH has not been set yet for type units if we're
6281 using .gdb_index. */
6282 this_cu->set_length (cu->header.get_length_with_initial ());
6283
6284 /* Establish the type offset that can be used to lookup the type. */
6285 sig_type->type_offset_in_section =
6286 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6287
6288 this_cu->set_version (cu->header.version);
6289 }
6290 else
6291 {
6292 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6293 section, abbrev_section,
6294 info_ptr,
6295 rcuh_kind::COMPILE);
6296
6297 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6298 this_cu->set_length (cu->header.get_length_with_initial ());
6299 this_cu->set_version (cu->header.version);
6300 }
6301 }
6302
6303 /* Skip dummy compilation units. */
6304 if (info_ptr >= begin_info_ptr + this_cu->length ()
6305 || peek_abbrev_code (abfd, info_ptr) == 0)
6306 {
6307 dummy_p = true;
6308 return;
6309 }
6310
6311 /* If we don't have them yet, read the abbrevs for this compilation unit.
6312 And if we need to read them now, make sure they're freed when we're
6313 done. */
6314 if (abbrev_table != NULL)
6315 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6316 else
6317 {
6318 if (cache != nullptr)
6319 abbrev_table = cache->find (abbrev_section,
6320 cu->header.abbrev_sect_off);
6321 if (abbrev_table == nullptr)
6322 {
6323 abbrev_section->read (objfile);
6324 m_abbrev_table_holder
6325 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6326 abbrev_table = m_abbrev_table_holder.get ();
6327 }
6328 }
6329
6330 /* Read the top level CU/TU die. */
6331 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6332 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6333
6334 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6335 {
6336 dummy_p = true;
6337 return;
6338 }
6339
6340 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6341 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6342 table from the DWO file and pass the ownership over to us. It will be
6343 referenced from READER, so we must make sure to free it after we're done
6344 with READER.
6345
6346 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6347 DWO CU, that this test will fail (the attribute will not be present). */
6348 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6349 if (dwo_name != nullptr)
6350 {
6351 struct dwo_unit *dwo_unit;
6352 struct die_info *dwo_comp_unit_die;
6353
6354 if (comp_unit_die->has_children)
6355 {
6356 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6357 " has children (offset %s) [in module %s]"),
6358 sect_offset_str (this_cu->sect_off),
6359 bfd_get_filename (abfd));
6360 }
6361 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6362 if (dwo_unit != NULL)
6363 {
6364 if (read_cutu_die_from_dwo (cu, dwo_unit,
6365 comp_unit_die, NULL,
6366 this, &info_ptr,
6367 &dwo_comp_unit_die,
6368 &m_dwo_abbrev_table) == 0)
6369 {
6370 /* Dummy die. */
6371 dummy_p = true;
6372 return;
6373 }
6374 comp_unit_die = dwo_comp_unit_die;
6375 }
6376 else
6377 {
6378 /* Yikes, we couldn't find the rest of the DIE, we only have
6379 the stub. A complaint has already been logged. There's
6380 not much more we can do except pass on the stub DIE to
6381 die_reader_func. We don't want to throw an error on bad
6382 debug info. */
6383 }
6384 }
6385 }
6386
6387 void
6388 cutu_reader::keep ()
6389 {
6390 /* Done, clean up. */
6391 gdb_assert (!dummy_p);
6392 if (m_new_cu != NULL)
6393 {
6394 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6395 now. */
6396 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6397 per_objfile->set_cu (m_this_cu, std::move (m_new_cu));
6398 }
6399 }
6400
6401 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6402 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6403 assumed to have already done the lookup to find the DWO file).
6404
6405 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6406 THIS_CU->is_debug_types, but nothing else.
6407
6408 We fill in THIS_CU->length.
6409
6410 THIS_CU->cu is always freed when done.
6411 This is done in order to not leave THIS_CU->cu in a state where we have
6412 to care whether it refers to the "main" CU or the DWO CU.
6413
6414 When parent_cu is passed, it is used to provide a default value for
6415 str_offsets_base and addr_base from the parent. */
6416
6417 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6418 dwarf2_per_objfile *per_objfile,
6419 struct dwarf2_cu *parent_cu,
6420 struct dwo_file *dwo_file)
6421 : die_reader_specs {},
6422 m_this_cu (this_cu)
6423 {
6424 struct objfile *objfile = per_objfile->objfile;
6425 struct dwarf2_section_info *section = this_cu->section;
6426 bfd *abfd = section->get_bfd_owner ();
6427 struct dwarf2_section_info *abbrev_section;
6428 const gdb_byte *begin_info_ptr, *info_ptr;
6429
6430 if (dwarf_die_debug)
6431 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6432 this_cu->is_debug_types ? "type" : "comp",
6433 sect_offset_str (this_cu->sect_off));
6434
6435 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6436
6437 abbrev_section = (dwo_file != NULL
6438 ? &dwo_file->sections.abbrev
6439 : get_abbrev_section_for_cu (this_cu));
6440
6441 /* This is cheap if the section is already read in. */
6442 section->read (objfile);
6443
6444 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6445
6446 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6447 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6448 section, abbrev_section, info_ptr,
6449 (this_cu->is_debug_types
6450 ? rcuh_kind::TYPE
6451 : rcuh_kind::COMPILE));
6452
6453 if (parent_cu != nullptr)
6454 {
6455 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6456 m_new_cu->addr_base = parent_cu->addr_base;
6457 }
6458 this_cu->set_length (m_new_cu->header.get_length_with_initial ());
6459
6460 /* Skip dummy compilation units. */
6461 if (info_ptr >= begin_info_ptr + this_cu->length ()
6462 || peek_abbrev_code (abfd, info_ptr) == 0)
6463 {
6464 dummy_p = true;
6465 return;
6466 }
6467
6468 abbrev_section->read (objfile);
6469 m_abbrev_table_holder
6470 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6471
6472 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6473 m_abbrev_table_holder.get ());
6474 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6475 }
6476
6477 \f
6478 /* Type Unit Groups.
6479
6480 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6481 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6482 so that all types coming from the same compilation (.o file) are grouped
6483 together. A future step could be to put the types in the same symtab as
6484 the CU the types ultimately came from. */
6485
6486 static hashval_t
6487 hash_type_unit_group (const void *item)
6488 {
6489 const struct type_unit_group *tu_group
6490 = (const struct type_unit_group *) item;
6491
6492 return hash_stmt_list_entry (&tu_group->hash);
6493 }
6494
6495 static int
6496 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6497 {
6498 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6499 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6500
6501 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6502 }
6503
6504 /* Allocate a hash table for type unit groups. */
6505
6506 static htab_up
6507 allocate_type_unit_groups_table ()
6508 {
6509 return htab_up (htab_create_alloc (3,
6510 hash_type_unit_group,
6511 eq_type_unit_group,
6512 htab_delete_entry<type_unit_group>,
6513 xcalloc, xfree));
6514 }
6515
6516 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6517 partial symtabs. We combine several TUs per psymtab to not let the size
6518 of any one psymtab grow too big. */
6519 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6520 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6521
6522 /* Helper routine for get_type_unit_group.
6523 Create the type_unit_group object used to hold one or more TUs. */
6524
6525 static std::unique_ptr<type_unit_group>
6526 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6527 {
6528 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6529
6530 tu_group->hash.dwo_unit = cu->dwo_unit;
6531 tu_group->hash.line_sect_off = line_offset_struct;
6532
6533 return tu_group;
6534 }
6535
6536 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6537 STMT_LIST is a DW_AT_stmt_list attribute. */
6538
6539 static struct type_unit_group *
6540 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6541 {
6542 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6543 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6544 struct type_unit_group *tu_group;
6545 void **slot;
6546 unsigned int line_offset;
6547 struct type_unit_group type_unit_group_for_lookup;
6548
6549 if (per_objfile->per_bfd->type_unit_groups == NULL)
6550 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6551
6552 /* Do we need to create a new group, or can we use an existing one? */
6553
6554 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6555 {
6556 line_offset = stmt_list->as_unsigned ();
6557 ++tu_stats->nr_symtab_sharers;
6558 }
6559 else
6560 {
6561 /* Ugh, no stmt_list. Rare, but we have to handle it.
6562 We can do various things here like create one group per TU or
6563 spread them over multiple groups to split up the expansion work.
6564 To avoid worst case scenarios (too many groups or too large groups)
6565 we, umm, group them in bunches. */
6566 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6567 | (tu_stats->nr_stmt_less_type_units
6568 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6569 ++tu_stats->nr_stmt_less_type_units;
6570 }
6571
6572 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6573 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6574 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6575 &type_unit_group_for_lookup, INSERT);
6576 if (*slot == nullptr)
6577 {
6578 sect_offset line_offset_struct = (sect_offset) line_offset;
6579 std::unique_ptr<type_unit_group> grp
6580 = create_type_unit_group (cu, line_offset_struct);
6581 *slot = grp.release ();
6582 ++tu_stats->nr_symtabs;
6583 }
6584
6585 tu_group = (struct type_unit_group *) *slot;
6586 gdb_assert (tu_group != nullptr);
6587 return tu_group;
6588 }
6589 \f
6590
6591 /* An instance of this is created when scanning DWARF to create a
6592 cooked index. */
6593
6594 class cooked_index_storage
6595 {
6596 public:
6597
6598 cooked_index_storage ()
6599 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
6600 eq_cutu_reader,
6601 htab_delete_entry<cutu_reader>,
6602 xcalloc, xfree)),
6603 m_index (new cooked_index_shard)
6604 {
6605 }
6606
6607 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
6608
6609 /* Return the current abbrev cache. */
6610 abbrev_cache *get_abbrev_cache ()
6611 {
6612 return &m_abbrev_cache;
6613 }
6614
6615 /* Return the DIE reader corresponding to PER_CU. If no such reader
6616 has been registered, return NULL. */
6617 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
6618 {
6619 int index = per_cu->index;
6620 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
6621 &index, index);
6622 }
6623
6624 /* Preserve READER by storing it in the local hash table. */
6625 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
6626 {
6627 m_abbrev_cache.add (reader->release_abbrev_table ());
6628
6629 int index = reader->cu->per_cu->index;
6630 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
6631 index, INSERT);
6632 gdb_assert (*slot == nullptr);
6633 cutu_reader *result = reader.get ();
6634 *slot = reader.release ();
6635 return result;
6636 }
6637
6638 /* Add an entry to the index. The arguments describe the entry; see
6639 cooked-index.h. The new entry is returned. */
6640 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
6641 cooked_index_flag flags,
6642 const char *name,
6643 const cooked_index_entry *parent_entry,
6644 dwarf2_per_cu_data *per_cu)
6645 {
6646 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
6647 }
6648
6649 /* Install the current addrmap into the index shard being constructed,
6650 then transfer ownership of the index to the caller. */
6651 std::unique_ptr<cooked_index_shard> release ()
6652 {
6653 m_index->install_addrmap (&m_addrmap);
6654 return std::move (m_index);
6655 }
6656
6657 /* Return the mutable addrmap that is currently being created. */
6658 addrmap_mutable *get_addrmap ()
6659 {
6660 return &m_addrmap;
6661 }
6662
6663 private:
6664
6665 /* Hash function for a cutu_reader. */
6666 static hashval_t hash_cutu_reader (const void *a)
6667 {
6668 const cutu_reader *reader = (const cutu_reader *) a;
6669 return reader->cu->per_cu->index;
6670 }
6671
6672 /* Equality function for cutu_reader. */
6673 static int eq_cutu_reader (const void *a, const void *b)
6674 {
6675 const cutu_reader *ra = (const cutu_reader *) a;
6676 const int *rb = (const int *) b;
6677 return ra->cu->per_cu->index == *rb;
6678 }
6679
6680 /* The abbrev cache used by this indexer. */
6681 abbrev_cache m_abbrev_cache;
6682 /* A hash table of cutu_reader objects. */
6683 htab_up m_reader_hash;
6684 /* The index shard that is being constructed. */
6685 std::unique_ptr<cooked_index_shard> m_index;
6686
6687 /* A writeable addrmap being constructed by this scanner. */
6688 addrmap_mutable m_addrmap;
6689 };
6690
6691 /* An instance of this is created to index a CU. */
6692
6693 class cooked_indexer
6694 {
6695 public:
6696
6697 cooked_indexer (cooked_index_storage *storage,
6698 dwarf2_per_cu_data *per_cu,
6699 enum language language)
6700 : m_index_storage (storage),
6701 m_per_cu (per_cu),
6702 m_language (language)
6703 {
6704 }
6705
6706 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
6707
6708 /* Index the given CU. */
6709 void make_index (cutu_reader *reader);
6710
6711 private:
6712
6713 /* A helper function to turn a section offset into an address that
6714 can be used in an addrmap. */
6715 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
6716 {
6717 CORE_ADDR value = to_underlying (offset);
6718 if (is_dwz)
6719 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
6720 return value;
6721 }
6722
6723 /* A helper function to scan the PC bounds of READER and record them
6724 in the storage's addrmap. */
6725 void check_bounds (cutu_reader *reader);
6726
6727 /* Ensure that the indicated CU exists. The cutu_reader for it is
6728 returned. FOR_SCANNING is true if the caller intends to scan all
6729 the DIEs in the CU; when false, this use is assumed to be to look
6730 up just a single DIE. */
6731 cutu_reader *ensure_cu_exists (cutu_reader *reader,
6732 dwarf2_per_objfile *per_objfile,
6733 sect_offset sect_off,
6734 bool is_dwz,
6735 bool for_scanning);
6736
6737 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6738 the entry for the enclosing scope (nullptr at top level). FULLY
6739 is true when a full scan must be done -- in some languages,
6740 function scopes must be fully explored in order to find nested
6741 functions. This returns a pointer to just after the spot where
6742 reading stopped. */
6743 const gdb_byte *index_dies (cutu_reader *reader,
6744 const gdb_byte *info_ptr,
6745 const cooked_index_entry *parent_entry,
6746 bool fully);
6747
6748 /* Scan the attributes for a given DIE and update the out
6749 parameters. Returns a pointer to the byte after the DIE. */
6750 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
6751 cutu_reader *reader,
6752 const gdb_byte *watermark_ptr,
6753 const gdb_byte *info_ptr,
6754 const abbrev_info *abbrev,
6755 const char **name,
6756 const char **linkage_name,
6757 cooked_index_flag *flags,
6758 sect_offset *sibling_offset,
6759 const cooked_index_entry **parent_entry,
6760 CORE_ADDR *maybe_defer,
6761 bool for_specification);
6762
6763 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6764 DW_AT_import, and then scanning the referenced CU. Returns a
6765 pointer to the byte after the DIE. */
6766 const gdb_byte *index_imported_unit (cutu_reader *reader,
6767 const gdb_byte *info_ptr,
6768 const abbrev_info *abbrev);
6769
6770 /* Recursively read DIEs, recording the section offsets in
6771 m_die_range_map and then calling index_dies. */
6772 const gdb_byte *recurse (cutu_reader *reader,
6773 const gdb_byte *info_ptr,
6774 const cooked_index_entry *parent_entry,
6775 bool fully);
6776
6777 /* The storage object, where the results are kept. */
6778 cooked_index_storage *m_index_storage;
6779 /* The CU that we are reading on behalf of. This object might be
6780 asked to index one CU but to treat the results as if they come
6781 from some including CU; in this case the including CU would be
6782 recorded here. */
6783 dwarf2_per_cu_data *m_per_cu;
6784 /* The language that we're assuming when reading. */
6785 enum language m_language;
6786
6787 /* An addrmap that maps from section offsets (see the form_addr
6788 method) to newly-created entries. See m_deferred_entries to
6789 understand this. */
6790 addrmap_mutable m_die_range_map;
6791
6792 /* A single deferred entry. */
6793 struct deferred_entry
6794 {
6795 sect_offset die_offset;
6796 const char *name;
6797 CORE_ADDR spec_offset;
6798 dwarf_tag tag;
6799 cooked_index_flag flags;
6800 };
6801
6802 /* The generated DWARF can sometimes have the declaration for a
6803 method in a class (or perhaps namespace) scope, with the
6804 definition appearing outside this scope... just one of the many
6805 bad things about DWARF. In order to handle this situation, we
6806 defer certain entries until the end of scanning, at which point
6807 we'll know the containing context of all the DIEs that we might
6808 have scanned. This vector stores these deferred entries. */
6809 std::vector<deferred_entry> m_deferred_entries;
6810 };
6811
6812 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6813 Process compilation unit THIS_CU for a psymtab. */
6814
6815 static void
6816 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
6817 dwarf2_per_objfile *per_objfile,
6818 cooked_index_storage *storage)
6819 {
6820 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
6821 storage->get_abbrev_cache ());
6822
6823 if (reader.comp_unit_die == nullptr)
6824 return;
6825
6826 if (reader.dummy_p)
6827 {
6828 /* Nothing. */
6829 }
6830 else if (this_cu->is_debug_types)
6831 build_type_psymtabs_reader (&reader, storage);
6832 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
6833 {
6834 bool nope = false;
6835 if (this_cu->scanned.compare_exchange_strong (nope, true))
6836 {
6837 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
6838 language_minimal);
6839 gdb_assert (storage != nullptr);
6840 cooked_indexer indexer (storage, this_cu, reader.cu->lang ());
6841 indexer.make_index (&reader);
6842 }
6843 }
6844 }
6845
6846 /* Reader function for build_type_psymtabs. */
6847
6848 static void
6849 build_type_psymtabs_reader (cutu_reader *reader,
6850 cooked_index_storage *storage)
6851 {
6852 struct dwarf2_cu *cu = reader->cu;
6853 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6854 struct die_info *type_unit_die = reader->comp_unit_die;
6855
6856 gdb_assert (per_cu->is_debug_types);
6857
6858 if (! type_unit_die->has_children)
6859 return;
6860
6861 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6862
6863 gdb_assert (storage != nullptr);
6864 cooked_indexer indexer (storage, per_cu, cu->lang ());
6865 indexer.make_index (reader);
6866 }
6867
6868 /* Struct used to sort TUs by their abbreviation table offset. */
6869
6870 struct tu_abbrev_offset
6871 {
6872 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
6873 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
6874 {}
6875
6876 /* This is used when sorting. */
6877 bool operator< (const tu_abbrev_offset &other) const
6878 {
6879 return abbrev_offset < other.abbrev_offset;
6880 }
6881
6882 signatured_type *sig_type;
6883 sect_offset abbrev_offset;
6884 };
6885
6886 /* Efficiently read all the type units.
6887
6888 The efficiency is because we sort TUs by the abbrev table they use and
6889 only read each abbrev table once. In one program there are 200K TUs
6890 sharing 8K abbrev tables.
6891
6892 The main purpose of this function is to support building the
6893 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6894 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6895 can collapse the search space by grouping them by stmt_list.
6896 The savings can be significant, in the same program from above the 200K TUs
6897 share 8K stmt_list tables.
6898
6899 FUNC is expected to call get_type_unit_group, which will create the
6900 struct type_unit_group if necessary and add it to
6901 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6902
6903 static void
6904 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
6905 cooked_index_storage *storage)
6906 {
6907 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6908 abbrev_table_up abbrev_table;
6909 sect_offset abbrev_offset;
6910
6911 /* It's up to the caller to not call us multiple times. */
6912 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
6913
6914 if (per_objfile->per_bfd->all_type_units.size () == 0)
6915 return;
6916
6917 /* TUs typically share abbrev tables, and there can be way more TUs than
6918 abbrev tables. Sort by abbrev table to reduce the number of times we
6919 read each abbrev table in.
6920 Alternatives are to punt or to maintain a cache of abbrev tables.
6921 This is simpler and efficient enough for now.
6922
6923 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6924 symtab to use). Typically TUs with the same abbrev offset have the same
6925 stmt_list value too so in practice this should work well.
6926
6927 The basic algorithm here is:
6928
6929 sort TUs by abbrev table
6930 for each TU with same abbrev table:
6931 read abbrev table if first user
6932 read TU top level DIE
6933 [IWBN if DWO skeletons had DW_AT_stmt_list]
6934 call FUNC */
6935
6936 dwarf_read_debug_printf ("Building type unit groups ...");
6937
6938 /* Sort in a separate table to maintain the order of all_units
6939 for .gdb_index: TU indices directly index all_type_units. */
6940 std::vector<tu_abbrev_offset> sorted_by_abbrev;
6941 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
6942
6943 for (const auto &cu : per_objfile->per_bfd->all_units)
6944 {
6945 if (cu->is_debug_types)
6946 {
6947 auto sig_type = static_cast<signatured_type *> (cu.get ());
6948 sorted_by_abbrev.emplace_back
6949 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
6950 sig_type->sect_off));
6951 }
6952 }
6953
6954 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
6955
6956 abbrev_offset = (sect_offset) ~(unsigned) 0;
6957
6958 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
6959 {
6960 /* Switch to the next abbrev table if necessary. */
6961 if (abbrev_table == NULL
6962 || tu.abbrev_offset != abbrev_offset)
6963 {
6964 abbrev_offset = tu.abbrev_offset;
6965 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
6966 abbrev_table =
6967 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
6968 ++tu_stats->nr_uniq_abbrev_tables;
6969 }
6970
6971 cutu_reader reader (tu.sig_type, per_objfile,
6972 abbrev_table.get (), nullptr, false);
6973 if (!reader.dummy_p)
6974 build_type_psymtabs_reader (&reader, storage);
6975 }
6976 }
6977
6978 /* Print collected type unit statistics. */
6979
6980 static void
6981 print_tu_stats (dwarf2_per_objfile *per_objfile)
6982 {
6983 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6984
6985 dwarf_read_debug_printf ("Type unit statistics:");
6986 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6987 dwarf_read_debug_printf (" %d uniq abbrev tables",
6988 tu_stats->nr_uniq_abbrev_tables);
6989 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
6990 tu_stats->nr_symtabs);
6991 dwarf_read_debug_printf (" %d symtab sharers",
6992 tu_stats->nr_symtab_sharers);
6993 dwarf_read_debug_printf (" %d type units without a stmt_list",
6994 tu_stats->nr_stmt_less_type_units);
6995 dwarf_read_debug_printf (" %d all_type_units reallocs",
6996 tu_stats->nr_all_type_units_reallocs);
6997 }
6998
6999 struct skeleton_data
7000 {
7001 dwarf2_per_objfile *per_objfile;
7002 cooked_index_storage *storage;
7003 };
7004
7005 /* Traversal function for process_skeletonless_type_unit.
7006 Read a TU in a DWO file and build partial symbols for it. */
7007
7008 static int
7009 process_skeletonless_type_unit (void **slot, void *info)
7010 {
7011 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7012 skeleton_data *data = (skeleton_data *) info;
7013
7014 /* If this TU doesn't exist in the global table, add it and read it in. */
7015
7016 if (data->per_objfile->per_bfd->signatured_types == NULL)
7017 data->per_objfile->per_bfd->signatured_types
7018 = allocate_signatured_type_table ();
7019
7020 signatured_type find_entry (dwo_unit->signature);
7021 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
7022 &find_entry, INSERT);
7023 /* If we've already seen this type there's nothing to do. What's happening
7024 is we're doing our own version of comdat-folding here. */
7025 if (*slot != NULL)
7026 return 1;
7027
7028 /* This does the job that create_all_units would have done for
7029 this TU. */
7030 signatured_type *entry
7031 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
7032 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
7033 *slot = entry;
7034
7035 /* This does the job that build_type_psymtabs would have done. */
7036 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
7037 if (!reader.dummy_p)
7038 build_type_psymtabs_reader (&reader, data->storage);
7039
7040 return 1;
7041 }
7042
7043 /* Traversal function for process_skeletonless_type_units. */
7044
7045 static int
7046 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7047 {
7048 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7049
7050 if (dwo_file->tus != NULL)
7051 htab_traverse_noresize (dwo_file->tus.get (),
7052 process_skeletonless_type_unit, info);
7053
7054 return 1;
7055 }
7056
7057 /* Scan all TUs of DWO files, verifying we've processed them.
7058 This is needed in case a TU was emitted without its skeleton.
7059 Note: This can't be done until we know what all the DWO files are. */
7060
7061 static void
7062 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
7063 cooked_index_storage *storage)
7064 {
7065 skeleton_data data { per_objfile, storage };
7066
7067 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7068 if (get_dwp_file (per_objfile) == NULL
7069 && per_objfile->per_bfd->dwo_files != NULL)
7070 {
7071 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7072 process_dwo_file_for_skeletonless_type_units,
7073 &data);
7074 }
7075 }
7076
7077 /* Build the partial symbol table by doing a quick pass through the
7078 .debug_info and .debug_abbrev sections. */
7079
7080 static void
7081 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7082 {
7083 struct objfile *objfile = per_objfile->objfile;
7084 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7085
7086 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7087 objfile_name (objfile));
7088
7089 per_bfd->map_info_sections (objfile);
7090
7091 cooked_index_storage index_storage;
7092 create_all_units (per_objfile);
7093 build_type_psymtabs (per_objfile, &index_storage);
7094 std::vector<std::unique_ptr<cooked_index_shard>> indexes;
7095
7096 per_bfd->quick_file_names_table
7097 = create_quick_file_names_table (per_bfd->all_units.size ());
7098 if (!per_bfd->debug_aranges.empty ())
7099 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
7100 index_storage.get_addrmap ());
7101
7102 {
7103 /* Ensure that complaints are handled correctly. */
7104 complaint_interceptor complaint_handler;
7105
7106 using iter_type = decltype (per_bfd->all_units.begin ());
7107
7108 auto task_size_ = [] (iter_type iter)
7109 {
7110 dwarf2_per_cu_data *per_cu = iter->get ();
7111 return (size_t)per_cu->length ();
7112 };
7113 auto task_size = gdb::make_function_view (task_size_);
7114
7115 /* Each thread returns a pair holding a cooked index, and a vector
7116 of errors that should be printed. The latter is done because
7117 GDB's I/O system is not thread-safe. run_on_main_thread could be
7118 used, but that would mean the messages are printed after the
7119 prompt, which looks weird. */
7120 using result_type = std::pair<std::unique_ptr<cooked_index_shard>,
7121 std::vector<gdb_exception>>;
7122 std::vector<result_type> results
7123 = gdb::parallel_for_each (1, per_bfd->all_units.begin (),
7124 per_bfd->all_units.end (),
7125 [=] (iter_type iter, iter_type end)
7126 {
7127 std::vector<gdb_exception> errors;
7128 cooked_index_storage thread_storage;
7129 for (; iter != end; ++iter)
7130 {
7131 dwarf2_per_cu_data *per_cu = iter->get ();
7132 try
7133 {
7134 process_psymtab_comp_unit (per_cu, per_objfile,
7135 &thread_storage);
7136 }
7137 catch (gdb_exception &except)
7138 {
7139 errors.push_back (std::move (except));
7140 }
7141 }
7142 return result_type (thread_storage.release (), std::move (errors));
7143 }, task_size);
7144
7145 /* Only show a given exception a single time. */
7146 std::unordered_set<gdb_exception> seen_exceptions;
7147 for (auto &one_result : results)
7148 {
7149 indexes.push_back (std::move (one_result.first));
7150 for (auto &one_exc : one_result.second)
7151 if (seen_exceptions.insert (one_exc).second)
7152 exception_print (gdb_stderr, one_exc);
7153 }
7154 }
7155
7156 /* This has to wait until we read the CUs, we need the list of DWOs. */
7157 process_skeletonless_type_units (per_objfile, &index_storage);
7158
7159 if (dwarf_read_debug > 0)
7160 print_tu_stats (per_objfile);
7161
7162 indexes.push_back (index_storage.release ());
7163 indexes.shrink_to_fit ();
7164
7165 cooked_index *vec = new cooked_index (std::move (indexes));
7166 per_bfd->index_table.reset (vec);
7167
7168 const cooked_index_entry *main_entry = vec->get_main ();
7169 if (main_entry != nullptr)
7170 set_objfile_main_name (objfile, main_entry->name,
7171 main_entry->per_cu->lang ());
7172
7173 dwarf_read_debug_printf ("Done building psymtabs of %s",
7174 objfile_name (objfile));
7175 }
7176
7177 static void
7178 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7179 struct dwarf2_section_info *section,
7180 struct dwarf2_section_info *abbrev_section,
7181 unsigned int is_dwz,
7182 htab_up &types_htab,
7183 rcuh_kind section_kind)
7184 {
7185 const gdb_byte *info_ptr;
7186 struct objfile *objfile = per_objfile->objfile;
7187
7188 dwarf_read_debug_printf ("Reading %s for %s",
7189 section->get_name (),
7190 section->get_file_name ());
7191
7192 section->read (objfile);
7193
7194 info_ptr = section->buffer;
7195
7196 while (info_ptr < section->buffer + section->size)
7197 {
7198 dwarf2_per_cu_data_up this_cu;
7199
7200 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7201
7202 comp_unit_head cu_header;
7203 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7204 abbrev_section, info_ptr,
7205 section_kind);
7206
7207 /* Save the compilation unit for later lookup. */
7208 if (cu_header.unit_type != DW_UT_type)
7209 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7210 else
7211 {
7212 if (types_htab == nullptr)
7213 types_htab = allocate_signatured_type_table ();
7214
7215 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7216 (cu_header.signature);
7217 signatured_type *sig_ptr = sig_type.get ();
7218 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7219 this_cu.reset (sig_type.release ());
7220
7221 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7222 gdb_assert (slot != nullptr);
7223 if (*slot != nullptr)
7224 complaint (_("debug type entry at offset %s is duplicate to"
7225 " the entry at offset %s, signature %s"),
7226 sect_offset_str (sect_off),
7227 sect_offset_str (sig_ptr->sect_off),
7228 hex_string (sig_ptr->signature));
7229 *slot = sig_ptr;
7230 }
7231 this_cu->sect_off = sect_off;
7232 this_cu->set_length (cu_header.get_length_with_initial ());
7233 this_cu->is_dwz = is_dwz;
7234 this_cu->section = section;
7235 /* Init this asap, to avoid a data race in the set_version in
7236 cutu_reader::cutu_reader (which may be run in parallel for the cooked
7237 index case). */
7238 this_cu->set_version (cu_header.version);
7239
7240 info_ptr = info_ptr + this_cu->length ();
7241 per_objfile->per_bfd->all_units.push_back (std::move (this_cu));
7242 }
7243 }
7244
7245 /* Initialize the views on all_units. */
7246
7247 static void
7248 finalize_all_units (dwarf2_per_bfd *per_bfd)
7249 {
7250 size_t nr_tus = per_bfd->tu_stats.nr_tus;
7251 size_t nr_cus = per_bfd->all_units.size () - nr_tus;
7252 gdb::array_view<dwarf2_per_cu_data_up> tmp = per_bfd->all_units;
7253 per_bfd->all_comp_units = tmp.slice (0, nr_cus);
7254 per_bfd->all_type_units = tmp.slice (nr_cus, nr_tus);
7255 }
7256
7257 /* Create a list of all compilation units in OBJFILE.
7258 This is only done for -readnow and building partial symtabs. */
7259
7260 static void
7261 create_all_units (dwarf2_per_objfile *per_objfile)
7262 {
7263 htab_up types_htab;
7264 gdb_assert (per_objfile->per_bfd->all_units.empty ());
7265
7266 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7267 &per_objfile->per_bfd->abbrev, 0,
7268 types_htab, rcuh_kind::COMPILE);
7269 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7270 read_comp_units_from_section (per_objfile, &section,
7271 &per_objfile->per_bfd->abbrev, 0,
7272 types_htab, rcuh_kind::TYPE);
7273
7274 dwz_file *dwz;
7275 try
7276 {
7277 dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7278 }
7279 catch (const gdb_exception_error &)
7280 {
7281 per_objfile->per_bfd->all_units.clear ();
7282 throw;
7283 }
7284 if (dwz != NULL)
7285 {
7286 /* Pre-read the sections we'll need to construct an index. */
7287 struct objfile *objfile = per_objfile->objfile;
7288 dwz->abbrev.read (objfile);
7289 dwz->info.read (objfile);
7290 dwz->str.read (objfile);
7291 dwz->line.read (objfile);
7292 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7293 types_htab, rcuh_kind::COMPILE);
7294 }
7295
7296 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7297
7298 finalize_all_units (per_objfile->per_bfd);
7299 }
7300
7301 /* Return the initial uleb128 in the die at INFO_PTR. */
7302
7303 static unsigned int
7304 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7305 {
7306 unsigned int bytes_read;
7307
7308 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7309 }
7310
7311 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7312 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7313
7314 Return the corresponding abbrev, or NULL if the number is zero (indicating
7315 an empty DIE). In either case *BYTES_READ will be set to the length of
7316 the initial number. */
7317
7318 static const struct abbrev_info *
7319 peek_die_abbrev (const die_reader_specs &reader,
7320 const gdb_byte *info_ptr, unsigned int *bytes_read)
7321 {
7322 dwarf2_cu *cu = reader.cu;
7323 bfd *abfd = reader.abfd;
7324 unsigned int abbrev_number
7325 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7326
7327 if (abbrev_number == 0)
7328 return NULL;
7329
7330 const abbrev_info *abbrev
7331 = reader.abbrev_table->lookup_abbrev (abbrev_number);
7332 if (!abbrev)
7333 {
7334 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7335 " at offset %s [in module %s]"),
7336 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7337 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
7338 }
7339
7340 return abbrev;
7341 }
7342
7343 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7344 Returns a pointer to the end of a series of DIEs, terminated by an empty
7345 DIE. Any children of the skipped DIEs will also be skipped. */
7346
7347 static const gdb_byte *
7348 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7349 {
7350 while (1)
7351 {
7352 unsigned int bytes_read;
7353 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
7354 &bytes_read);
7355
7356 if (abbrev == NULL)
7357 return info_ptr + bytes_read;
7358 else
7359 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7360 }
7361 }
7362
7363 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7364 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7365 abbrev corresponding to that skipped uleb128 should be passed in
7366 ABBREV.
7367
7368 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7369 returns a pointer to this DIE's sibling, skipping any children.
7370 Otherwise, returns a pointer to the DIE's first child. */
7371
7372 static const gdb_byte *
7373 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7374 const struct abbrev_info *abbrev, bool do_skip_children)
7375 {
7376 unsigned int bytes_read;
7377 struct attribute attr;
7378 bfd *abfd = reader->abfd;
7379 struct dwarf2_cu *cu = reader->cu;
7380 const gdb_byte *buffer = reader->buffer;
7381 const gdb_byte *buffer_end = reader->buffer_end;
7382 unsigned int form, i;
7383
7384 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
7385 {
7386 /* We only handle DW_FORM_ref4 here. */
7387 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
7388 unsigned int offset = read_4_bytes (abfd, sibling_data);
7389 const gdb_byte *sibling_ptr
7390 = buffer + to_underlying (cu->header.sect_off) + offset;
7391 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
7392 return sibling_ptr;
7393 /* Fall through to the slow way. */
7394 }
7395 else if (abbrev->size_if_constant != 0)
7396 {
7397 info_ptr += abbrev->size_if_constant;
7398 if (do_skip_children && abbrev->has_children)
7399 return skip_children (reader, info_ptr);
7400 return info_ptr;
7401 }
7402
7403 for (i = 0; i < abbrev->num_attrs; i++)
7404 {
7405 /* The only abbrev we care about is DW_AT_sibling. */
7406 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
7407 {
7408 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7409 if (attr.form == DW_FORM_ref_addr)
7410 complaint (_("ignoring absolute DW_AT_sibling"));
7411 else
7412 {
7413 sect_offset off = attr.get_ref_die_offset ();
7414 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7415
7416 if (sibling_ptr < info_ptr)
7417 complaint (_("DW_AT_sibling points backwards"));
7418 else if (sibling_ptr > reader->buffer_end)
7419 reader->die_section->overflow_complaint ();
7420 else
7421 return sibling_ptr;
7422 }
7423 }
7424
7425 /* If it isn't DW_AT_sibling, skip this attribute. */
7426 form = abbrev->attrs[i].form;
7427 skip_attribute:
7428 switch (form)
7429 {
7430 case DW_FORM_ref_addr:
7431 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7432 and later it is offset sized. */
7433 if (cu->header.version == 2)
7434 info_ptr += cu->header.addr_size;
7435 else
7436 info_ptr += cu->header.offset_size;
7437 break;
7438 case DW_FORM_GNU_ref_alt:
7439 info_ptr += cu->header.offset_size;
7440 break;
7441 case DW_FORM_addr:
7442 info_ptr += cu->header.addr_size;
7443 break;
7444 case DW_FORM_data1:
7445 case DW_FORM_ref1:
7446 case DW_FORM_flag:
7447 case DW_FORM_strx1:
7448 info_ptr += 1;
7449 break;
7450 case DW_FORM_flag_present:
7451 case DW_FORM_implicit_const:
7452 break;
7453 case DW_FORM_data2:
7454 case DW_FORM_ref2:
7455 case DW_FORM_strx2:
7456 info_ptr += 2;
7457 break;
7458 case DW_FORM_strx3:
7459 info_ptr += 3;
7460 break;
7461 case DW_FORM_data4:
7462 case DW_FORM_ref4:
7463 case DW_FORM_strx4:
7464 info_ptr += 4;
7465 break;
7466 case DW_FORM_data8:
7467 case DW_FORM_ref8:
7468 case DW_FORM_ref_sig8:
7469 info_ptr += 8;
7470 break;
7471 case DW_FORM_data16:
7472 info_ptr += 16;
7473 break;
7474 case DW_FORM_string:
7475 read_direct_string (abfd, info_ptr, &bytes_read);
7476 info_ptr += bytes_read;
7477 break;
7478 case DW_FORM_sec_offset:
7479 case DW_FORM_strp:
7480 case DW_FORM_GNU_strp_alt:
7481 info_ptr += cu->header.offset_size;
7482 break;
7483 case DW_FORM_exprloc:
7484 case DW_FORM_block:
7485 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7486 info_ptr += bytes_read;
7487 break;
7488 case DW_FORM_block1:
7489 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7490 break;
7491 case DW_FORM_block2:
7492 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7493 break;
7494 case DW_FORM_block4:
7495 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7496 break;
7497 case DW_FORM_addrx:
7498 case DW_FORM_strx:
7499 case DW_FORM_sdata:
7500 case DW_FORM_udata:
7501 case DW_FORM_ref_udata:
7502 case DW_FORM_GNU_addr_index:
7503 case DW_FORM_GNU_str_index:
7504 case DW_FORM_rnglistx:
7505 case DW_FORM_loclistx:
7506 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7507 break;
7508 case DW_FORM_indirect:
7509 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7510 info_ptr += bytes_read;
7511 /* We need to continue parsing from here, so just go back to
7512 the top. */
7513 goto skip_attribute;
7514
7515 default:
7516 error (_("Dwarf Error: Cannot handle %s "
7517 "in DWARF reader [in module %s]"),
7518 dwarf_form_name (form),
7519 bfd_get_filename (abfd));
7520 }
7521 }
7522
7523 if (do_skip_children && abbrev->has_children)
7524 return skip_children (reader, info_ptr);
7525 else
7526 return info_ptr;
7527 }
7528 \f
7529 /* Reading in full CUs. */
7530
7531 /* Add PER_CU to the queue. */
7532
7533 static void
7534 queue_comp_unit (dwarf2_per_cu_data *per_cu,
7535 dwarf2_per_objfile *per_objfile,
7536 enum language pretend_language)
7537 {
7538 per_cu->queued = 1;
7539
7540 gdb_assert (per_objfile->queue.has_value ());
7541 per_objfile->queue->emplace (per_cu, per_objfile, pretend_language);
7542 }
7543
7544 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7545
7546 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7547 dependency.
7548
7549 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7550 DIEs, false otherwise.
7551
7552 Explanation: there is an invariant that if a CU is queued for expansion
7553 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7554 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7555 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7556 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7557 invariant is respected.
7558
7559 The caller is therefore not required to load the CU's DIEs (we return false)
7560 if:
7561
7562 - the CU is already expanded, and therefore does not get enqueued
7563 - the CU gets enqueued for expansion, but its DIEs are already loaded
7564
7565 Note that the caller should not use this function's return value as an
7566 indicator of whether the CU's DIEs are loaded right now, it should check
7567 that by calling `dwarf2_per_objfile::get_cu` instead. */
7568
7569 static int
7570 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7571 dwarf2_per_cu_data *per_cu,
7572 dwarf2_per_objfile *per_objfile,
7573 enum language pretend_language)
7574 {
7575 /* Mark the dependence relation so that we don't flush PER_CU
7576 too early. */
7577 if (dependent_cu != NULL)
7578 dependent_cu->add_dependence (per_cu);
7579
7580 /* If it's already on the queue, we have nothing to do. */
7581 if (per_cu->queued)
7582 {
7583 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7584 loaded. */
7585 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
7586
7587 /* If the CU is queued for expansion, it should not already be
7588 expanded. */
7589 gdb_assert (!per_objfile->symtab_set_p (per_cu));
7590
7591 /* The DIEs are already loaded, the caller doesn't need to do it. */
7592 return 0;
7593 }
7594
7595 bool queued = false;
7596 if (!per_objfile->symtab_set_p (per_cu))
7597 {
7598 /* Add it to the queue. */
7599 queue_comp_unit (per_cu, per_objfile, pretend_language);
7600 queued = true;
7601 }
7602
7603 /* If the compilation unit is already loaded, just mark it as
7604 used. */
7605 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7606 if (cu != nullptr)
7607 cu->last_used = 0;
7608
7609 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7610 and the DIEs are not already loaded. */
7611 return queued && cu == nullptr;
7612 }
7613
7614 /* Process the queue. */
7615
7616 static void
7617 process_queue (dwarf2_per_objfile *per_objfile)
7618 {
7619 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7620 objfile_name (per_objfile->objfile));
7621
7622 /* The queue starts out with one item, but following a DIE reference
7623 may load a new CU, adding it to the end of the queue. */
7624 while (!per_objfile->queue->empty ())
7625 {
7626 dwarf2_queue_item &item = per_objfile->queue->front ();
7627 dwarf2_per_cu_data *per_cu = item.per_cu;
7628
7629 if (!per_objfile->symtab_set_p (per_cu))
7630 {
7631 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7632
7633 /* Skip dummy CUs. */
7634 if (cu != nullptr)
7635 {
7636 unsigned int debug_print_threshold;
7637 char buf[100];
7638
7639 if (per_cu->is_debug_types)
7640 {
7641 struct signatured_type *sig_type =
7642 (struct signatured_type *) per_cu;
7643
7644 sprintf (buf, "TU %s at offset %s",
7645 hex_string (sig_type->signature),
7646 sect_offset_str (per_cu->sect_off));
7647 /* There can be 100s of TUs.
7648 Only print them in verbose mode. */
7649 debug_print_threshold = 2;
7650 }
7651 else
7652 {
7653 sprintf (buf, "CU at offset %s",
7654 sect_offset_str (per_cu->sect_off));
7655 debug_print_threshold = 1;
7656 }
7657
7658 if (dwarf_read_debug >= debug_print_threshold)
7659 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
7660
7661 if (per_cu->is_debug_types)
7662 process_full_type_unit (cu, item.pretend_language);
7663 else
7664 process_full_comp_unit (cu, item.pretend_language);
7665
7666 if (dwarf_read_debug >= debug_print_threshold)
7667 dwarf_read_debug_printf ("Done expanding %s", buf);
7668 }
7669 }
7670
7671 per_cu->queued = 0;
7672 per_objfile->queue->pop ();
7673 }
7674
7675 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7676 objfile_name (per_objfile->objfile));
7677 }
7678
7679 /* Load the DIEs associated with PER_CU into memory.
7680
7681 In some cases, the caller, while reading partial symbols, will need to load
7682 the full symbols for the CU for some reason. It will already have a
7683 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7684 rather than creating a new one. */
7685
7686 static void
7687 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
7688 dwarf2_per_objfile *per_objfile,
7689 dwarf2_cu *existing_cu,
7690 bool skip_partial,
7691 enum language pretend_language)
7692 {
7693 gdb_assert (! this_cu->is_debug_types);
7694
7695 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
7696 if (reader.dummy_p)
7697 return;
7698
7699 struct dwarf2_cu *cu = reader.cu;
7700 const gdb_byte *info_ptr = reader.info_ptr;
7701
7702 gdb_assert (cu->die_hash == NULL);
7703 cu->die_hash =
7704 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
7705 die_info::hash,
7706 die_info::eq,
7707 NULL,
7708 &cu->comp_unit_obstack,
7709 hashtab_obstack_allocate,
7710 dummy_obstack_deallocate);
7711
7712 if (reader.comp_unit_die->has_children)
7713 reader.comp_unit_die->child
7714 = read_die_and_siblings (&reader, reader.info_ptr,
7715 &info_ptr, reader.comp_unit_die);
7716 cu->dies = reader.comp_unit_die;
7717 /* comp_unit_die is not stored in die_hash, no need. */
7718
7719 /* We try not to read any attributes in this function, because not
7720 all CUs needed for references have been loaded yet, and symbol
7721 table processing isn't initialized. But we have to set the CU language,
7722 or we won't be able to build types correctly.
7723 Similarly, if we do not read the producer, we can not apply
7724 producer-specific interpretation. */
7725 prepare_one_comp_unit (cu, cu->dies, pretend_language);
7726
7727 reader.keep ();
7728 }
7729
7730 /* Add a DIE to the delayed physname list. */
7731
7732 static void
7733 add_to_method_list (struct type *type, int fnfield_index, int index,
7734 const char *name, struct die_info *die,
7735 struct dwarf2_cu *cu)
7736 {
7737 struct delayed_method_info mi;
7738 mi.type = type;
7739 mi.fnfield_index = fnfield_index;
7740 mi.index = index;
7741 mi.name = name;
7742 mi.die = die;
7743 cu->method_list.push_back (mi);
7744 }
7745
7746 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7747 "const" / "volatile". If so, decrements LEN by the length of the
7748 modifier and return true. Otherwise return false. */
7749
7750 template<size_t N>
7751 static bool
7752 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
7753 {
7754 size_t mod_len = sizeof (mod) - 1;
7755 if (len > mod_len && startswith (physname + (len - mod_len), mod))
7756 {
7757 len -= mod_len;
7758 return true;
7759 }
7760 return false;
7761 }
7762
7763 /* Compute the physnames of any methods on the CU's method list.
7764
7765 The computation of method physnames is delayed in order to avoid the
7766 (bad) condition that one of the method's formal parameters is of an as yet
7767 incomplete type. */
7768
7769 static void
7770 compute_delayed_physnames (struct dwarf2_cu *cu)
7771 {
7772 /* Only C++ delays computing physnames. */
7773 if (cu->method_list.empty ())
7774 return;
7775 gdb_assert (cu->lang () == language_cplus);
7776
7777 for (const delayed_method_info &mi : cu->method_list)
7778 {
7779 const char *physname;
7780 struct fn_fieldlist *fn_flp
7781 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
7782 physname = dwarf2_physname (mi.name, mi.die, cu);
7783 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
7784 = physname ? physname : "";
7785
7786 /* Since there's no tag to indicate whether a method is a
7787 const/volatile overload, extract that information out of the
7788 demangled name. */
7789 if (physname != NULL)
7790 {
7791 size_t len = strlen (physname);
7792
7793 while (1)
7794 {
7795 if (physname[len] == ')') /* shortcut */
7796 break;
7797 else if (check_modifier (physname, len, " const"))
7798 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
7799 else if (check_modifier (physname, len, " volatile"))
7800 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
7801 else
7802 break;
7803 }
7804 }
7805 }
7806
7807 /* The list is no longer needed. */
7808 cu->method_list.clear ();
7809 }
7810
7811 /* Go objects should be embedded in a DW_TAG_module DIE,
7812 and it's not clear if/how imported objects will appear.
7813 To keep Go support simple until that's worked out,
7814 go back through what we've read and create something usable.
7815 We could do this while processing each DIE, and feels kinda cleaner,
7816 but that way is more invasive.
7817 This is to, for example, allow the user to type "p var" or "b main"
7818 without having to specify the package name, and allow lookups
7819 of module.object to work in contexts that use the expression
7820 parser. */
7821
7822 static void
7823 fixup_go_packaging (struct dwarf2_cu *cu)
7824 {
7825 gdb::unique_xmalloc_ptr<char> package_name;
7826 struct pending *list;
7827 int i;
7828
7829 for (list = *cu->get_builder ()->get_global_symbols ();
7830 list != NULL;
7831 list = list->next)
7832 {
7833 for (i = 0; i < list->nsyms; ++i)
7834 {
7835 struct symbol *sym = list->symbol[i];
7836
7837 if (sym->language () == language_go
7838 && sym->aclass () == LOC_BLOCK)
7839 {
7840 gdb::unique_xmalloc_ptr<char> this_package_name
7841 (go_symbol_package_name (sym));
7842
7843 if (this_package_name == NULL)
7844 continue;
7845 if (package_name == NULL)
7846 package_name = std::move (this_package_name);
7847 else
7848 {
7849 struct objfile *objfile = cu->per_objfile->objfile;
7850 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
7851 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7852 (sym->symtab () != NULL
7853 ? symtab_to_filename_for_display
7854 (sym->symtab ())
7855 : objfile_name (objfile)),
7856 this_package_name.get (), package_name.get ());
7857 }
7858 }
7859 }
7860 }
7861
7862 if (package_name != NULL)
7863 {
7864 struct objfile *objfile = cu->per_objfile->objfile;
7865 const char *saved_package_name = objfile->intern (package_name.get ());
7866 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7867 saved_package_name);
7868 struct symbol *sym;
7869
7870 sym = new (&objfile->objfile_obstack) symbol;
7871 sym->set_language (language_go, &objfile->objfile_obstack);
7872 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
7873 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7874 e.g., "main" finds the "main" module and not C's main(). */
7875 sym->set_domain (STRUCT_DOMAIN);
7876 sym->set_aclass_index (LOC_TYPEDEF);
7877 sym->set_type (type);
7878
7879 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
7880 }
7881 }
7882
7883 /* Allocate a fully-qualified name consisting of the two parts on the
7884 obstack. */
7885
7886 static const char *
7887 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
7888 {
7889 return obconcat (obstack, p1, "::", p2, (char *) NULL);
7890 }
7891
7892 /* A helper that allocates a variant part to attach to a Rust enum
7893 type. OBSTACK is where the results should be allocated. TYPE is
7894 the type we're processing. DISCRIMINANT_INDEX is the index of the
7895 discriminant. It must be the index of one of the fields of TYPE,
7896 or -1 to mean there is no discriminant (univariant enum).
7897 DEFAULT_INDEX is the index of the default field; or -1 if there is
7898 no default. RANGES is indexed by "effective" field number (the
7899 field index, but omitting the discriminant and default fields) and
7900 must hold the discriminant values used by the variants. Note that
7901 RANGES must have a lifetime at least as long as OBSTACK -- either
7902 already allocated on it, or static. */
7903
7904 static void
7905 alloc_rust_variant (struct obstack *obstack, struct type *type,
7906 int discriminant_index, int default_index,
7907 gdb::array_view<discriminant_range> ranges)
7908 {
7909 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7910 gdb_assert (discriminant_index == -1
7911 || (discriminant_index >= 0
7912 && discriminant_index < type->num_fields ()));
7913 gdb_assert (default_index == -1
7914 || (default_index >= 0 && default_index < type->num_fields ()));
7915
7916 /* We have one variant for each non-discriminant field. */
7917 int n_variants = type->num_fields ();
7918 if (discriminant_index != -1)
7919 --n_variants;
7920
7921 variant *variants = new (obstack) variant[n_variants];
7922 int var_idx = 0;
7923 int range_idx = 0;
7924 for (int i = 0; i < type->num_fields (); ++i)
7925 {
7926 if (i == discriminant_index)
7927 continue;
7928
7929 variants[var_idx].first_field = i;
7930 variants[var_idx].last_field = i + 1;
7931
7932 /* The default field does not need a range, but other fields do.
7933 We skipped the discriminant above. */
7934 if (i != default_index)
7935 {
7936 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
7937 ++range_idx;
7938 }
7939
7940 ++var_idx;
7941 }
7942
7943 gdb_assert (range_idx == ranges.size ());
7944 gdb_assert (var_idx == n_variants);
7945
7946 variant_part *part = new (obstack) variant_part;
7947 part->discriminant_index = discriminant_index;
7948 /* If there is no discriminant, then whether it is signed is of no
7949 consequence. */
7950 part->is_unsigned
7951 = (discriminant_index == -1
7952 ? false
7953 : type->field (discriminant_index).type ()->is_unsigned ());
7954 part->variants = gdb::array_view<variant> (variants, n_variants);
7955
7956 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
7957 gdb::array_view<variant_part> *prop_value
7958 = new (storage) gdb::array_view<variant_part> (part, 1);
7959
7960 struct dynamic_prop prop;
7961 prop.set_variant_parts (prop_value);
7962
7963 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
7964 }
7965
7966 /* Some versions of rustc emitted enums in an unusual way.
7967
7968 Ordinary enums were emitted as unions. The first element of each
7969 structure in the union was named "RUST$ENUM$DISR". This element
7970 held the discriminant.
7971
7972 These versions of Rust also implemented the "non-zero"
7973 optimization. When the enum had two values, and one is empty and
7974 the other holds a pointer that cannot be zero, the pointer is used
7975 as the discriminant, with a zero value meaning the empty variant.
7976 Here, the union's first member is of the form
7977 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
7978 where the fieldnos are the indices of the fields that should be
7979 traversed in order to find the field (which may be several fields deep)
7980 and the variantname is the name of the variant of the case when the
7981 field is zero.
7982
7983 This function recognizes whether TYPE is of one of these forms,
7984 and, if so, smashes it to be a variant type. */
7985
7986 static void
7987 quirk_rust_enum (struct type *type, struct objfile *objfile)
7988 {
7989 gdb_assert (type->code () == TYPE_CODE_UNION);
7990
7991 /* We don't need to deal with empty enums. */
7992 if (type->num_fields () == 0)
7993 return;
7994
7995 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
7996 if (type->num_fields () == 1
7997 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
7998 {
7999 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
8000
8001 /* Decode the field name to find the offset of the
8002 discriminant. */
8003 ULONGEST bit_offset = 0;
8004 struct type *field_type = type->field (0).type ();
8005 while (name[0] >= '0' && name[0] <= '9')
8006 {
8007 char *tail;
8008 unsigned long index = strtoul (name, &tail, 10);
8009 name = tail;
8010 if (*name != '$'
8011 || index >= field_type->num_fields ()
8012 || (field_type->field (index).loc_kind ()
8013 != FIELD_LOC_KIND_BITPOS))
8014 {
8015 complaint (_("Could not parse Rust enum encoding string \"%s\""
8016 "[in module %s]"),
8017 type->field (0).name (),
8018 objfile_name (objfile));
8019 return;
8020 }
8021 ++name;
8022
8023 bit_offset += field_type->field (index).loc_bitpos ();
8024 field_type = field_type->field (index).type ();
8025 }
8026
8027 /* Smash this type to be a structure type. We have to do this
8028 because the type has already been recorded. */
8029 type->set_code (TYPE_CODE_STRUCT);
8030 type->set_num_fields (3);
8031 /* Save the field we care about. */
8032 struct field saved_field = type->field (0);
8033 type->set_fields
8034 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8035
8036 /* Put the discriminant at index 0. */
8037 type->field (0).set_type (field_type);
8038 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8039 type->field (0).set_name ("<<discriminant>>");
8040 type->field (0).set_loc_bitpos (bit_offset);
8041
8042 /* The order of fields doesn't really matter, so put the real
8043 field at index 1 and the data-less field at index 2. */
8044 type->field (1) = saved_field;
8045 type->field (1).set_name
8046 (rust_last_path_segment (type->field (1).type ()->name ()));
8047 type->field (1).type ()->set_name
8048 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8049 type->field (1).name ()));
8050
8051 const char *dataless_name
8052 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8053 name);
8054 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8055 dataless_name);
8056 type->field (2).set_type (dataless_type);
8057 /* NAME points into the original discriminant name, which
8058 already has the correct lifetime. */
8059 type->field (2).set_name (name);
8060 type->field (2).set_loc_bitpos (0);
8061
8062 /* Indicate that this is a variant type. */
8063 static discriminant_range ranges[1] = { { 0, 0 } };
8064 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8065 }
8066 /* A union with a single anonymous field is probably an old-style
8067 univariant enum. */
8068 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
8069 {
8070 /* Smash this type to be a structure type. We have to do this
8071 because the type has already been recorded. */
8072 type->set_code (TYPE_CODE_STRUCT);
8073
8074 struct type *field_type = type->field (0).type ();
8075 const char *variant_name
8076 = rust_last_path_segment (field_type->name ());
8077 type->field (0).set_name (variant_name);
8078 field_type->set_name
8079 (rust_fully_qualify (&objfile->objfile_obstack,
8080 type->name (), variant_name));
8081
8082 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
8083 }
8084 else
8085 {
8086 struct type *disr_type = nullptr;
8087 for (int i = 0; i < type->num_fields (); ++i)
8088 {
8089 disr_type = type->field (i).type ();
8090
8091 if (disr_type->code () != TYPE_CODE_STRUCT)
8092 {
8093 /* All fields of a true enum will be structs. */
8094 return;
8095 }
8096 else if (disr_type->num_fields () == 0)
8097 {
8098 /* Could be data-less variant, so keep going. */
8099 disr_type = nullptr;
8100 }
8101 else if (strcmp (disr_type->field (0).name (),
8102 "RUST$ENUM$DISR") != 0)
8103 {
8104 /* Not a Rust enum. */
8105 return;
8106 }
8107 else
8108 {
8109 /* Found one. */
8110 break;
8111 }
8112 }
8113
8114 /* If we got here without a discriminant, then it's probably
8115 just a union. */
8116 if (disr_type == nullptr)
8117 return;
8118
8119 /* Smash this type to be a structure type. We have to do this
8120 because the type has already been recorded. */
8121 type->set_code (TYPE_CODE_STRUCT);
8122
8123 /* Make space for the discriminant field. */
8124 struct field *disr_field = &disr_type->field (0);
8125 field *new_fields
8126 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
8127 * sizeof (struct field)));
8128 memcpy (new_fields + 1, type->fields (),
8129 type->num_fields () * sizeof (struct field));
8130 type->set_fields (new_fields);
8131 type->set_num_fields (type->num_fields () + 1);
8132
8133 /* Install the discriminant at index 0 in the union. */
8134 type->field (0) = *disr_field;
8135 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8136 type->field (0).set_name ("<<discriminant>>");
8137
8138 /* We need a way to find the correct discriminant given a
8139 variant name. For convenience we build a map here. */
8140 struct type *enum_type = disr_field->type ();
8141 std::unordered_map<std::string, ULONGEST> discriminant_map;
8142 for (int i = 0; i < enum_type->num_fields (); ++i)
8143 {
8144 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
8145 {
8146 const char *name
8147 = rust_last_path_segment (enum_type->field (i).name ());
8148 discriminant_map[name] = enum_type->field (i).loc_enumval ();
8149 }
8150 }
8151
8152 int n_fields = type->num_fields ();
8153 /* We don't need a range entry for the discriminant, but we do
8154 need one for every other field, as there is no default
8155 variant. */
8156 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
8157 discriminant_range,
8158 n_fields - 1);
8159 /* Skip the discriminant here. */
8160 for (int i = 1; i < n_fields; ++i)
8161 {
8162 /* Find the final word in the name of this variant's type.
8163 That name can be used to look up the correct
8164 discriminant. */
8165 const char *variant_name
8166 = rust_last_path_segment (type->field (i).type ()->name ());
8167
8168 auto iter = discriminant_map.find (variant_name);
8169 if (iter != discriminant_map.end ())
8170 {
8171 ranges[i - 1].low = iter->second;
8172 ranges[i - 1].high = iter->second;
8173 }
8174
8175 /* In Rust, each element should have the size of the
8176 enclosing enum. */
8177 type->field (i).type ()->set_length (type->length ());
8178
8179 /* Remove the discriminant field, if it exists. */
8180 struct type *sub_type = type->field (i).type ();
8181 if (sub_type->num_fields () > 0)
8182 {
8183 sub_type->set_num_fields (sub_type->num_fields () - 1);
8184 sub_type->set_fields (sub_type->fields () + 1);
8185 }
8186 type->field (i).set_name (variant_name);
8187 sub_type->set_name
8188 (rust_fully_qualify (&objfile->objfile_obstack,
8189 type->name (), variant_name));
8190 }
8191
8192 /* Indicate that this is a variant type. */
8193 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
8194 gdb::array_view<discriminant_range> (ranges,
8195 n_fields - 1));
8196 }
8197 }
8198
8199 /* Rewrite some Rust unions to be structures with variants parts. */
8200
8201 static void
8202 rust_union_quirks (struct dwarf2_cu *cu)
8203 {
8204 gdb_assert (cu->lang () == language_rust);
8205 for (type *type_ : cu->rust_unions)
8206 quirk_rust_enum (type_, cu->per_objfile->objfile);
8207 /* We don't need this any more. */
8208 cu->rust_unions.clear ();
8209 }
8210
8211 /* See read.h. */
8212
8213 type_unit_group_unshareable *
8214 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
8215 {
8216 auto iter = this->m_type_units.find (tu_group);
8217 if (iter != this->m_type_units.end ())
8218 return iter->second.get ();
8219
8220 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
8221 type_unit_group_unshareable *result = uniq.get ();
8222 this->m_type_units[tu_group] = std::move (uniq);
8223 return result;
8224 }
8225
8226 struct type *
8227 dwarf2_per_objfile::get_type_for_signatured_type
8228 (signatured_type *sig_type) const
8229 {
8230 auto iter = this->m_type_map.find (sig_type);
8231 if (iter == this->m_type_map.end ())
8232 return nullptr;
8233
8234 return iter->second;
8235 }
8236
8237 void dwarf2_per_objfile::set_type_for_signatured_type
8238 (signatured_type *sig_type, struct type *type)
8239 {
8240 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
8241
8242 this->m_type_map[sig_type] = type;
8243 }
8244
8245 /* A helper function for computing the list of all symbol tables
8246 included by PER_CU. */
8247
8248 static void
8249 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
8250 htab_t all_children, htab_t all_type_symtabs,
8251 dwarf2_per_cu_data *per_cu,
8252 dwarf2_per_objfile *per_objfile,
8253 struct compunit_symtab *immediate_parent)
8254 {
8255 void **slot = htab_find_slot (all_children, per_cu, INSERT);
8256 if (*slot != NULL)
8257 {
8258 /* This inclusion and its children have been processed. */
8259 return;
8260 }
8261
8262 *slot = per_cu;
8263
8264 /* Only add a CU if it has a symbol table. */
8265 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8266 if (cust != NULL)
8267 {
8268 /* If this is a type unit only add its symbol table if we haven't
8269 seen it yet (type unit per_cu's can share symtabs). */
8270 if (per_cu->is_debug_types)
8271 {
8272 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8273 if (*slot == NULL)
8274 {
8275 *slot = cust;
8276 result->push_back (cust);
8277 if (cust->user == NULL)
8278 cust->user = immediate_parent;
8279 }
8280 }
8281 else
8282 {
8283 result->push_back (cust);
8284 if (cust->user == NULL)
8285 cust->user = immediate_parent;
8286 }
8287 }
8288
8289 if (!per_cu->imported_symtabs_empty ())
8290 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8291 {
8292 recursively_compute_inclusions (result, all_children,
8293 all_type_symtabs, ptr, per_objfile,
8294 cust);
8295 }
8296 }
8297
8298 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8299 PER_CU. */
8300
8301 static void
8302 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
8303 dwarf2_per_objfile *per_objfile)
8304 {
8305 gdb_assert (! per_cu->is_debug_types);
8306
8307 if (!per_cu->imported_symtabs_empty ())
8308 {
8309 int len;
8310 std::vector<compunit_symtab *> result_symtabs;
8311 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8312
8313 /* If we don't have a symtab, we can just skip this case. */
8314 if (cust == NULL)
8315 return;
8316
8317 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
8318 htab_eq_pointer,
8319 NULL, xcalloc, xfree));
8320 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
8321 htab_eq_pointer,
8322 NULL, xcalloc, xfree));
8323
8324 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8325 {
8326 recursively_compute_inclusions (&result_symtabs, all_children.get (),
8327 all_type_symtabs.get (), ptr,
8328 per_objfile, cust);
8329 }
8330
8331 /* Now we have a transitive closure of all the included symtabs. */
8332 len = result_symtabs.size ();
8333 cust->includes
8334 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8335 struct compunit_symtab *, len + 1);
8336 memcpy (cust->includes, result_symtabs.data (),
8337 len * sizeof (compunit_symtab *));
8338 cust->includes[len] = NULL;
8339 }
8340 }
8341
8342 /* Compute the 'includes' field for the symtabs of all the CUs we just
8343 read. */
8344
8345 static void
8346 process_cu_includes (dwarf2_per_objfile *per_objfile)
8347 {
8348 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
8349 {
8350 if (! iter->is_debug_types)
8351 compute_compunit_symtab_includes (iter, per_objfile);
8352 }
8353
8354 per_objfile->per_bfd->just_read_cus.clear ();
8355 }
8356
8357 /* Generate full symbol information for CU, whose DIEs have
8358 already been loaded into memory. */
8359
8360 static void
8361 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
8362 {
8363 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8364 struct objfile *objfile = per_objfile->objfile;
8365 struct gdbarch *gdbarch = objfile->arch ();
8366 CORE_ADDR lowpc, highpc;
8367 struct compunit_symtab *cust;
8368 CORE_ADDR baseaddr;
8369 struct block *static_block;
8370 CORE_ADDR addr;
8371
8372 baseaddr = objfile->text_section_offset ();
8373
8374 /* Clear the list here in case something was left over. */
8375 cu->method_list.clear ();
8376
8377 dwarf2_find_base_address (cu->dies, cu);
8378
8379 /* Before we start reading the top-level DIE, ensure it has a valid tag
8380 type. */
8381 switch (cu->dies->tag)
8382 {
8383 case DW_TAG_compile_unit:
8384 case DW_TAG_partial_unit:
8385 case DW_TAG_type_unit:
8386 break;
8387 default:
8388 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8389 dwarf_tag_name (cu->dies->tag),
8390 sect_offset_str (cu->per_cu->sect_off),
8391 objfile_name (per_objfile->objfile));
8392 }
8393
8394 /* Do line number decoding in read_file_scope () */
8395 process_die (cu->dies, cu);
8396
8397 /* For now fudge the Go package. */
8398 if (cu->lang () == language_go)
8399 fixup_go_packaging (cu);
8400
8401 /* Now that we have processed all the DIEs in the CU, all the types
8402 should be complete, and it should now be safe to compute all of the
8403 physnames. */
8404 compute_delayed_physnames (cu);
8405
8406 if (cu->lang () == language_rust)
8407 rust_union_quirks (cu);
8408
8409 /* Some compilers don't define a DW_AT_high_pc attribute for the
8410 compilation unit. If the DW_AT_high_pc is missing, synthesize
8411 it, by scanning the DIE's below the compilation unit. */
8412 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8413
8414 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8415 static_block
8416 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
8417
8418 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8419 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8420 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8421 addrmap to help ensure it has an accurate map of pc values belonging to
8422 this comp unit. */
8423 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8424
8425 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
8426 (static_block, 0);
8427
8428 if (cust != NULL)
8429 {
8430 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8431
8432 /* Set symtab language to language from DW_AT_language. If the
8433 compilation is from a C file generated by language preprocessors, do
8434 not set the language if it was already deduced by start_subfile. */
8435 if (!(cu->lang () == language_c
8436 && cust->primary_filetab ()->language () != language_unknown))
8437 cust->primary_filetab ()->set_language (cu->lang ());
8438
8439 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8440 produce DW_AT_location with location lists but it can be possibly
8441 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8442 there were bugs in prologue debug info, fixed later in GCC-4.5
8443 by "unwind info for epilogues" patch (which is not directly related).
8444
8445 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8446 needed, it would be wrong due to missing DW_AT_producer there.
8447
8448 Still one can confuse GDB by using non-standard GCC compilation
8449 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8450 */
8451 if (cu->has_loclist && gcc_4_minor >= 5)
8452 cust->set_locations_valid (true);
8453
8454 if (gcc_4_minor >= 5)
8455 cust->set_epilogue_unwind_valid (true);
8456
8457 cust->set_call_site_htab (cu->call_site_htab);
8458 }
8459
8460 per_objfile->set_symtab (cu->per_cu, cust);
8461
8462 /* Push it for inclusion processing later. */
8463 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
8464
8465 /* Not needed any more. */
8466 cu->reset_builder ();
8467 }
8468
8469 /* Generate full symbol information for type unit CU, whose DIEs have
8470 already been loaded into memory. */
8471
8472 static void
8473 process_full_type_unit (dwarf2_cu *cu,
8474 enum language pretend_language)
8475 {
8476 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8477 struct compunit_symtab *cust;
8478 struct signatured_type *sig_type;
8479
8480 gdb_assert (cu->per_cu->is_debug_types);
8481 sig_type = (struct signatured_type *) cu->per_cu;
8482
8483 /* Clear the list here in case something was left over. */
8484 cu->method_list.clear ();
8485
8486 /* The symbol tables are set up in read_type_unit_scope. */
8487 process_die (cu->dies, cu);
8488
8489 /* For now fudge the Go package. */
8490 if (cu->lang () == language_go)
8491 fixup_go_packaging (cu);
8492
8493 /* Now that we have processed all the DIEs in the CU, all the types
8494 should be complete, and it should now be safe to compute all of the
8495 physnames. */
8496 compute_delayed_physnames (cu);
8497
8498 if (cu->lang () == language_rust)
8499 rust_union_quirks (cu);
8500
8501 /* TUs share symbol tables.
8502 If this is the first TU to use this symtab, complete the construction
8503 of it with end_expandable_symtab. Otherwise, complete the addition of
8504 this TU's symbols to the existing symtab. */
8505 type_unit_group_unshareable *tug_unshare =
8506 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8507 if (tug_unshare->compunit_symtab == NULL)
8508 {
8509 buildsym_compunit *builder = cu->get_builder ();
8510 cust = builder->end_expandable_symtab (0);
8511 tug_unshare->compunit_symtab = cust;
8512
8513 if (cust != NULL)
8514 {
8515 /* Set symtab language to language from DW_AT_language. If the
8516 compilation is from a C file generated by language preprocessors,
8517 do not set the language if it was already deduced by
8518 start_subfile. */
8519 if (!(cu->lang () == language_c
8520 && cust->primary_filetab ()->language () != language_c))
8521 cust->primary_filetab ()->set_language (cu->lang ());
8522 }
8523 }
8524 else
8525 {
8526 cu->get_builder ()->augment_type_symtab ();
8527 cust = tug_unshare->compunit_symtab;
8528 }
8529
8530 per_objfile->set_symtab (cu->per_cu, cust);
8531
8532 /* Not needed any more. */
8533 cu->reset_builder ();
8534 }
8535
8536 /* Process an imported unit DIE. */
8537
8538 static void
8539 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8540 {
8541 struct attribute *attr;
8542
8543 /* For now we don't handle imported units in type units. */
8544 if (cu->per_cu->is_debug_types)
8545 {
8546 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8547 " supported in type units [in module %s]"),
8548 objfile_name (cu->per_objfile->objfile));
8549 }
8550
8551 attr = dwarf2_attr (die, DW_AT_import, cu);
8552 if (attr != NULL)
8553 {
8554 sect_offset sect_off = attr->get_ref_die_offset ();
8555 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8556 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8557 dwarf2_per_cu_data *per_cu
8558 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
8559 per_objfile->per_bfd);
8560
8561 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8562 into another compilation unit, at root level. Regard this as a hint,
8563 and ignore it. This is a best effort, it only works if unit_type and
8564 lang are already set. */
8565 if (die->parent && die->parent->parent == NULL
8566 && per_cu->unit_type (false) == DW_UT_compile
8567 && per_cu->lang (false) == language_cplus)
8568 return;
8569
8570 /* If necessary, add it to the queue and load its DIEs. */
8571 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
8572 cu->lang ()))
8573 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
8574 false, cu->lang ());
8575
8576 cu->per_cu->imported_symtabs_push (per_cu);
8577 }
8578 }
8579
8580 /* RAII object that represents a process_die scope: i.e.,
8581 starts/finishes processing a DIE. */
8582 class process_die_scope
8583 {
8584 public:
8585 process_die_scope (die_info *die, dwarf2_cu *cu)
8586 : m_die (die), m_cu (cu)
8587 {
8588 /* We should only be processing DIEs not already in process. */
8589 gdb_assert (!m_die->in_process);
8590 m_die->in_process = true;
8591 }
8592
8593 ~process_die_scope ()
8594 {
8595 m_die->in_process = false;
8596
8597 /* If we're done processing the DIE for the CU that owns the line
8598 header, we don't need the line header anymore. */
8599 if (m_cu->line_header_die_owner == m_die)
8600 {
8601 delete m_cu->line_header;
8602 m_cu->line_header = NULL;
8603 m_cu->line_header_die_owner = NULL;
8604 }
8605 }
8606
8607 private:
8608 die_info *m_die;
8609 dwarf2_cu *m_cu;
8610 };
8611
8612 /* Process a die and its children. */
8613
8614 static void
8615 process_die (struct die_info *die, struct dwarf2_cu *cu)
8616 {
8617 process_die_scope scope (die, cu);
8618
8619 switch (die->tag)
8620 {
8621 case DW_TAG_padding:
8622 break;
8623 case DW_TAG_compile_unit:
8624 case DW_TAG_partial_unit:
8625 read_file_scope (die, cu);
8626 break;
8627 case DW_TAG_type_unit:
8628 read_type_unit_scope (die, cu);
8629 break;
8630 case DW_TAG_subprogram:
8631 /* Nested subprograms in Fortran get a prefix. */
8632 if (cu->lang () == language_fortran
8633 && die->parent != NULL
8634 && die->parent->tag == DW_TAG_subprogram)
8635 cu->processing_has_namespace_info = true;
8636 /* Fall through. */
8637 case DW_TAG_inlined_subroutine:
8638 read_func_scope (die, cu);
8639 break;
8640 case DW_TAG_lexical_block:
8641 case DW_TAG_try_block:
8642 case DW_TAG_catch_block:
8643 read_lexical_block_scope (die, cu);
8644 break;
8645 case DW_TAG_call_site:
8646 case DW_TAG_GNU_call_site:
8647 read_call_site_scope (die, cu);
8648 break;
8649 case DW_TAG_class_type:
8650 case DW_TAG_interface_type:
8651 case DW_TAG_structure_type:
8652 case DW_TAG_union_type:
8653 case DW_TAG_namelist:
8654 process_structure_scope (die, cu);
8655 break;
8656 case DW_TAG_enumeration_type:
8657 process_enumeration_scope (die, cu);
8658 break;
8659
8660 /* These dies have a type, but processing them does not create
8661 a symbol or recurse to process the children. Therefore we can
8662 read them on-demand through read_type_die. */
8663 case DW_TAG_subroutine_type:
8664 case DW_TAG_set_type:
8665 case DW_TAG_pointer_type:
8666 case DW_TAG_ptr_to_member_type:
8667 case DW_TAG_reference_type:
8668 case DW_TAG_rvalue_reference_type:
8669 case DW_TAG_string_type:
8670 break;
8671
8672 case DW_TAG_array_type:
8673 /* We only need to handle this case for Ada -- in other
8674 languages, it's normal for the compiler to emit a typedef
8675 instead. */
8676 if (cu->lang () != language_ada)
8677 break;
8678 /* FALLTHROUGH */
8679 case DW_TAG_base_type:
8680 case DW_TAG_subrange_type:
8681 case DW_TAG_generic_subrange:
8682 case DW_TAG_typedef:
8683 /* Add a typedef symbol for the type definition, if it has a
8684 DW_AT_name. */
8685 new_symbol (die, read_type_die (die, cu), cu);
8686 break;
8687 case DW_TAG_common_block:
8688 read_common_block (die, cu);
8689 break;
8690 case DW_TAG_common_inclusion:
8691 break;
8692 case DW_TAG_namespace:
8693 cu->processing_has_namespace_info = true;
8694 read_namespace (die, cu);
8695 break;
8696 case DW_TAG_module:
8697 cu->processing_has_namespace_info = true;
8698 read_module (die, cu);
8699 break;
8700 case DW_TAG_imported_declaration:
8701 cu->processing_has_namespace_info = true;
8702 if (read_alias (die, cu))
8703 break;
8704 /* The declaration is neither a global namespace nor a variable
8705 alias. */
8706 /* Fall through. */
8707 case DW_TAG_imported_module:
8708 cu->processing_has_namespace_info = true;
8709 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8710 || cu->lang () != language_fortran))
8711 complaint (_("Tag '%s' has unexpected children"),
8712 dwarf_tag_name (die->tag));
8713 read_import_statement (die, cu);
8714 break;
8715
8716 case DW_TAG_imported_unit:
8717 process_imported_unit_die (die, cu);
8718 break;
8719
8720 case DW_TAG_variable:
8721 read_variable (die, cu);
8722 break;
8723
8724 default:
8725 new_symbol (die, NULL, cu);
8726 break;
8727 }
8728 }
8729 \f
8730 /* DWARF name computation. */
8731
8732 /* A helper function for dwarf2_compute_name which determines whether DIE
8733 needs to have the name of the scope prepended to the name listed in the
8734 die. */
8735
8736 static int
8737 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8738 {
8739 struct attribute *attr;
8740
8741 switch (die->tag)
8742 {
8743 case DW_TAG_namespace:
8744 case DW_TAG_typedef:
8745 case DW_TAG_class_type:
8746 case DW_TAG_interface_type:
8747 case DW_TAG_structure_type:
8748 case DW_TAG_union_type:
8749 case DW_TAG_enumeration_type:
8750 case DW_TAG_enumerator:
8751 case DW_TAG_subprogram:
8752 case DW_TAG_inlined_subroutine:
8753 case DW_TAG_member:
8754 case DW_TAG_imported_declaration:
8755 return 1;
8756
8757 case DW_TAG_variable:
8758 case DW_TAG_constant:
8759 /* We only need to prefix "globally" visible variables. These include
8760 any variable marked with DW_AT_external or any variable that
8761 lives in a namespace. [Variables in anonymous namespaces
8762 require prefixing, but they are not DW_AT_external.] */
8763
8764 if (dwarf2_attr (die, DW_AT_specification, cu))
8765 {
8766 struct dwarf2_cu *spec_cu = cu;
8767
8768 return die_needs_namespace (die_specification (die, &spec_cu),
8769 spec_cu);
8770 }
8771
8772 attr = dwarf2_attr (die, DW_AT_external, cu);
8773 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8774 && die->parent->tag != DW_TAG_module)
8775 return 0;
8776 /* A variable in a lexical block of some kind does not need a
8777 namespace, even though in C++ such variables may be external
8778 and have a mangled name. */
8779 if (die->parent->tag == DW_TAG_lexical_block
8780 || die->parent->tag == DW_TAG_try_block
8781 || die->parent->tag == DW_TAG_catch_block
8782 || die->parent->tag == DW_TAG_subprogram)
8783 return 0;
8784 return 1;
8785
8786 default:
8787 return 0;
8788 }
8789 }
8790
8791 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8792 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8793 defined for the given DIE. */
8794
8795 static struct attribute *
8796 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8797 {
8798 struct attribute *attr;
8799
8800 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8801 if (attr == NULL)
8802 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8803
8804 return attr;
8805 }
8806
8807 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8808 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8809 defined for the given DIE. */
8810
8811 static const char *
8812 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8813 {
8814 const char *linkage_name;
8815
8816 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8817 if (linkage_name == NULL)
8818 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8819
8820 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8821 See https://github.com/rust-lang/rust/issues/32925. */
8822 if (cu->lang () == language_rust && linkage_name != NULL
8823 && strchr (linkage_name, '{') != NULL)
8824 linkage_name = NULL;
8825
8826 return linkage_name;
8827 }
8828
8829 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8830 compute the physname for the object, which include a method's:
8831 - formal parameters (C++),
8832 - receiver type (Go),
8833
8834 The term "physname" is a bit confusing.
8835 For C++, for example, it is the demangled name.
8836 For Go, for example, it's the mangled name.
8837
8838 For Ada, return the DIE's linkage name rather than the fully qualified
8839 name. PHYSNAME is ignored..
8840
8841 The result is allocated on the objfile->per_bfd's obstack and
8842 canonicalized. */
8843
8844 static const char *
8845 dwarf2_compute_name (const char *name,
8846 struct die_info *die, struct dwarf2_cu *cu,
8847 int physname)
8848 {
8849 struct objfile *objfile = cu->per_objfile->objfile;
8850
8851 if (name == NULL)
8852 name = dwarf2_name (die, cu);
8853
8854 enum language lang = cu->lang ();
8855
8856 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8857 but otherwise compute it by typename_concat inside GDB.
8858 FIXME: Actually this is not really true, or at least not always true.
8859 It's all very confusing. compute_and_set_names doesn't try to demangle
8860 Fortran names because there is no mangling standard. So new_symbol
8861 will set the demangled name to the result of dwarf2_full_name, and it is
8862 the demangled name that GDB uses if it exists. */
8863 if (lang == language_ada
8864 || (lang == language_fortran && physname))
8865 {
8866 /* For Ada unit, we prefer the linkage name over the name, as
8867 the former contains the exported name, which the user expects
8868 to be able to reference. Ideally, we want the user to be able
8869 to reference this entity using either natural or linkage name,
8870 but we haven't started looking at this enhancement yet. */
8871 const char *linkage_name = dw2_linkage_name (die, cu);
8872
8873 if (linkage_name != NULL)
8874 return linkage_name;
8875 }
8876
8877 /* These are the only languages we know how to qualify names in. */
8878 if (name != NULL
8879 && (lang == language_cplus
8880 || lang == language_fortran || lang == language_d
8881 || lang == language_rust))
8882 {
8883 if (die_needs_namespace (die, cu))
8884 {
8885 const char *prefix;
8886
8887 string_file buf;
8888
8889 prefix = determine_prefix (die, cu);
8890 if (*prefix != '\0')
8891 {
8892 gdb::unique_xmalloc_ptr<char> prefixed_name
8893 (typename_concat (NULL, prefix, name, physname, cu));
8894
8895 buf.puts (prefixed_name.get ());
8896 }
8897 else
8898 buf.puts (name);
8899
8900 /* Template parameters may be specified in the DIE's DW_AT_name, or
8901 as children with DW_TAG_template_type_param or
8902 DW_TAG_value_type_param. If the latter, add them to the name
8903 here. If the name already has template parameters, then
8904 skip this step; some versions of GCC emit both, and
8905 it is more efficient to use the pre-computed name.
8906
8907 Something to keep in mind about this process: it is very
8908 unlikely, or in some cases downright impossible, to produce
8909 something that will match the mangled name of a function.
8910 If the definition of the function has the same debug info,
8911 we should be able to match up with it anyway. But fallbacks
8912 using the minimal symbol, for instance to find a method
8913 implemented in a stripped copy of libstdc++, will not work.
8914 If we do not have debug info for the definition, we will have to
8915 match them up some other way.
8916
8917 When we do name matching there is a related problem with function
8918 templates; two instantiated function templates are allowed to
8919 differ only by their return types, which we do not add here. */
8920
8921 if (lang == language_cplus && strchr (name, '<') == NULL)
8922 {
8923 struct attribute *attr;
8924 struct die_info *child;
8925 int first = 1;
8926
8927 die->building_fullname = 1;
8928
8929 for (child = die->child; child != NULL; child = child->sibling)
8930 {
8931 struct type *type;
8932 LONGEST value;
8933 const gdb_byte *bytes;
8934 struct dwarf2_locexpr_baton *baton;
8935 struct value *v;
8936
8937 if (child->tag != DW_TAG_template_type_param
8938 && child->tag != DW_TAG_template_value_param)
8939 continue;
8940
8941 if (first)
8942 {
8943 buf.puts ("<");
8944 first = 0;
8945 }
8946 else
8947 buf.puts (", ");
8948
8949 attr = dwarf2_attr (child, DW_AT_type, cu);
8950 if (attr == NULL)
8951 {
8952 complaint (_("template parameter missing DW_AT_type"));
8953 buf.puts ("UNKNOWN_TYPE");
8954 continue;
8955 }
8956 type = die_type (child, cu);
8957
8958 if (child->tag == DW_TAG_template_type_param)
8959 {
8960 cu->language_defn->print_type (type, "", &buf, -1, 0,
8961 &type_print_raw_options);
8962 continue;
8963 }
8964
8965 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8966 if (attr == NULL)
8967 {
8968 complaint (_("template parameter missing "
8969 "DW_AT_const_value"));
8970 buf.puts ("UNKNOWN_VALUE");
8971 continue;
8972 }
8973
8974 dwarf2_const_value_attr (attr, type, name,
8975 &cu->comp_unit_obstack, cu,
8976 &value, &bytes, &baton);
8977
8978 if (type->has_no_signedness ())
8979 /* GDB prints characters as NUMBER 'CHAR'. If that's
8980 changed, this can use value_print instead. */
8981 cu->language_defn->printchar (value, type, &buf);
8982 else
8983 {
8984 struct value_print_options opts;
8985
8986 if (baton != NULL)
8987 v = dwarf2_evaluate_loc_desc (type, NULL,
8988 baton->data,
8989 baton->size,
8990 baton->per_cu,
8991 baton->per_objfile);
8992 else if (bytes != NULL)
8993 {
8994 v = value::allocate (type);
8995 memcpy (value_contents_writeable (v).data (), bytes,
8996 type->length ());
8997 }
8998 else
8999 v = value_from_longest (type, value);
9000
9001 /* Specify decimal so that we do not depend on
9002 the radix. */
9003 get_formatted_print_options (&opts, 'd');
9004 opts.raw = true;
9005 value_print (v, &buf, &opts);
9006 release_value (v);
9007 }
9008 }
9009
9010 die->building_fullname = 0;
9011
9012 if (!first)
9013 {
9014 /* Close the argument list, with a space if necessary
9015 (nested templates). */
9016 if (!buf.empty () && buf.string ().back () == '>')
9017 buf.puts (" >");
9018 else
9019 buf.puts (">");
9020 }
9021 }
9022
9023 /* For C++ methods, append formal parameter type
9024 information, if PHYSNAME. */
9025
9026 if (physname && die->tag == DW_TAG_subprogram
9027 && lang == language_cplus)
9028 {
9029 struct type *type = read_type_die (die, cu);
9030
9031 c_type_print_args (type, &buf, 1, lang,
9032 &type_print_raw_options);
9033
9034 if (lang == language_cplus)
9035 {
9036 /* Assume that an artificial first parameter is
9037 "this", but do not crash if it is not. RealView
9038 marks unnamed (and thus unused) parameters as
9039 artificial; there is no way to differentiate
9040 the two cases. */
9041 if (type->num_fields () > 0
9042 && TYPE_FIELD_ARTIFICIAL (type, 0)
9043 && type->field (0).type ()->code () == TYPE_CODE_PTR
9044 && TYPE_CONST (type->field (0).type ()->target_type ()))
9045 buf.puts (" const");
9046 }
9047 }
9048
9049 const std::string &intermediate_name = buf.string ();
9050
9051 const char *canonical_name
9052 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9053 objfile);
9054
9055 /* If we only computed INTERMEDIATE_NAME, or if
9056 INTERMEDIATE_NAME is already canonical, then we need to
9057 intern it. */
9058 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9059 name = objfile->intern (intermediate_name);
9060 else
9061 name = canonical_name;
9062 }
9063 }
9064
9065 return name;
9066 }
9067
9068 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9069 If scope qualifiers are appropriate they will be added. The result
9070 will be allocated on the storage_obstack, or NULL if the DIE does
9071 not have a name. NAME may either be from a previous call to
9072 dwarf2_name or NULL.
9073
9074 The output string will be canonicalized (if C++). */
9075
9076 static const char *
9077 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9078 {
9079 return dwarf2_compute_name (name, die, cu, 0);
9080 }
9081
9082 /* Construct a physname for the given DIE in CU. NAME may either be
9083 from a previous call to dwarf2_name or NULL. The result will be
9084 allocated on the objfile_objstack or NULL if the DIE does not have a
9085 name.
9086
9087 The output string will be canonicalized (if C++). */
9088
9089 static const char *
9090 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9091 {
9092 struct objfile *objfile = cu->per_objfile->objfile;
9093 const char *retval, *mangled = NULL, *canon = NULL;
9094 int need_copy = 1;
9095
9096 /* In this case dwarf2_compute_name is just a shortcut not building anything
9097 on its own. */
9098 if (!die_needs_namespace (die, cu))
9099 return dwarf2_compute_name (name, die, cu, 1);
9100
9101 if (cu->lang () != language_rust)
9102 mangled = dw2_linkage_name (die, cu);
9103
9104 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9105 has computed. */
9106 gdb::unique_xmalloc_ptr<char> demangled;
9107 if (mangled != NULL)
9108 {
9109 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
9110 {
9111 /* Do nothing (do not demangle the symbol name). */
9112 }
9113 else
9114 {
9115 /* Use DMGL_RET_DROP for C++ template functions to suppress
9116 their return type. It is easier for GDB users to search
9117 for such functions as `name(params)' than `long name(params)'.
9118 In such case the minimal symbol names do not match the full
9119 symbol names but for template functions there is never a need
9120 to look up their definition from their declaration so
9121 the only disadvantage remains the minimal symbol variant
9122 `long name(params)' does not have the proper inferior type. */
9123 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
9124 | DMGL_RET_DROP));
9125 }
9126 if (demangled)
9127 canon = demangled.get ();
9128 else
9129 {
9130 canon = mangled;
9131 need_copy = 0;
9132 }
9133 }
9134
9135 if (canon == NULL || check_physname)
9136 {
9137 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9138
9139 if (canon != NULL && strcmp (physname, canon) != 0)
9140 {
9141 /* It may not mean a bug in GDB. The compiler could also
9142 compute DW_AT_linkage_name incorrectly. But in such case
9143 GDB would need to be bug-to-bug compatible. */
9144
9145 complaint (_("Computed physname <%s> does not match demangled <%s> "
9146 "(from linkage <%s>) - DIE at %s [in module %s]"),
9147 physname, canon, mangled, sect_offset_str (die->sect_off),
9148 objfile_name (objfile));
9149
9150 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9151 is available here - over computed PHYSNAME. It is safer
9152 against both buggy GDB and buggy compilers. */
9153
9154 retval = canon;
9155 }
9156 else
9157 {
9158 retval = physname;
9159 need_copy = 0;
9160 }
9161 }
9162 else
9163 retval = canon;
9164
9165 if (need_copy)
9166 retval = objfile->intern (retval);
9167
9168 return retval;
9169 }
9170
9171 /* Inspect DIE in CU for a namespace alias or a variable with alias
9172 attribute. If one exists, record a new symbol for it.
9173
9174 Returns true if an alias was recorded, false otherwise. */
9175
9176 static bool
9177 read_alias (struct die_info *die, struct dwarf2_cu *cu)
9178 {
9179 struct attribute *attr;
9180
9181 /* If the die does not have a name, this is neither a namespace
9182 alias nor a variable alias. */
9183 attr = dwarf2_attr (die, DW_AT_name, cu);
9184 if (attr != NULL)
9185 {
9186 int num;
9187 struct die_info *d = die;
9188 struct dwarf2_cu *imported_cu = cu;
9189
9190 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9191 keep inspecting DIEs until we hit the underlying import. */
9192 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9193 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9194 {
9195 attr = dwarf2_attr (d, DW_AT_import, cu);
9196 if (attr == NULL)
9197 break;
9198
9199 d = follow_die_ref (d, attr, &imported_cu);
9200 if (d->tag != DW_TAG_imported_declaration)
9201 break;
9202 }
9203
9204 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9205 {
9206 complaint (_("DIE at %s has too many recursively imported "
9207 "declarations"), sect_offset_str (d->sect_off));
9208 return false;
9209 }
9210
9211 if (attr != NULL)
9212 {
9213 struct type *type;
9214 if (d->tag == DW_TAG_variable)
9215 {
9216 /* This declaration is a C/C++ global variable alias.
9217 Add a symbol for it whose type is the same as the
9218 aliased variable's. */
9219 type = die_type (d, imported_cu);
9220 struct symbol *sym = new_symbol (die, type, cu);
9221 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
9222 sym->set_aclass_index (LOC_UNRESOLVED);
9223 if (attr != nullptr)
9224 var_decode_location (attr, sym, cu);
9225 return true;
9226 }
9227 else
9228 {
9229 sect_offset sect_off = attr->get_ref_die_offset ();
9230 type = get_die_type_at_offset (sect_off, cu->per_cu,
9231 cu->per_objfile);
9232 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
9233 {
9234 /* This declaration is a global namespace alias. Add
9235 a symbol for it whose type is the aliased
9236 namespace. */
9237 new_symbol (die, type, cu);
9238 return true;
9239 }
9240 }
9241 }
9242 }
9243 return false;
9244 }
9245
9246 /* Return the using directives repository (global or local?) to use in the
9247 current context for CU.
9248
9249 For Ada, imported declarations can materialize renamings, which *may* be
9250 global. However it is impossible (for now?) in DWARF to distinguish
9251 "external" imported declarations and "static" ones. As all imported
9252 declarations seem to be static in all other languages, make them all CU-wide
9253 global only in Ada. */
9254
9255 static struct using_direct **
9256 using_directives (struct dwarf2_cu *cu)
9257 {
9258 if (cu->lang () == language_ada
9259 && cu->get_builder ()->outermost_context_p ())
9260 return cu->get_builder ()->get_global_using_directives ();
9261 else
9262 return cu->get_builder ()->get_local_using_directives ();
9263 }
9264
9265 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
9266 given CU. If the format is not recognized or the attribute is
9267 not present, set it to 0. */
9268
9269 static unsigned int
9270 read_decl_line (struct die_info *die, struct dwarf2_cu *cu)
9271 {
9272 struct attribute *decl_line = dwarf2_attr (die, DW_AT_decl_line, cu);
9273 if (decl_line == nullptr)
9274 return 0;
9275 if (decl_line->form_is_constant ())
9276 {
9277 LONGEST val = decl_line->constant_value (0);
9278 if (0 <= val && val <= UINT_MAX)
9279 return (unsigned int) val;
9280
9281 complaint (_("Declared line for using directive is too large"));
9282 return 0;
9283 }
9284
9285 complaint (_("Declared line for using directive is of incorrect format"));
9286 return 0;
9287 }
9288
9289 /* Read the import statement specified by the given die and record it. */
9290
9291 static void
9292 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9293 {
9294 struct objfile *objfile = cu->per_objfile->objfile;
9295 struct attribute *import_attr;
9296 struct die_info *imported_die, *child_die;
9297 struct dwarf2_cu *imported_cu;
9298 const char *imported_name;
9299 const char *imported_name_prefix;
9300 const char *canonical_name;
9301 const char *import_alias;
9302 const char *imported_declaration = NULL;
9303 const char *import_prefix;
9304 std::vector<const char *> excludes;
9305
9306 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9307 if (import_attr == NULL)
9308 {
9309 complaint (_("Tag '%s' has no DW_AT_import"),
9310 dwarf_tag_name (die->tag));
9311 return;
9312 }
9313
9314 imported_cu = cu;
9315 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9316 imported_name = dwarf2_name (imported_die, imported_cu);
9317 if (imported_name == NULL)
9318 {
9319 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9320
9321 The import in the following code:
9322 namespace A
9323 {
9324 typedef int B;
9325 }
9326
9327 int main ()
9328 {
9329 using A::B;
9330 B b;
9331 return b;
9332 }
9333
9334 ...
9335 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9336 <52> DW_AT_decl_file : 1
9337 <53> DW_AT_decl_line : 6
9338 <54> DW_AT_import : <0x75>
9339 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9340 <59> DW_AT_name : B
9341 <5b> DW_AT_decl_file : 1
9342 <5c> DW_AT_decl_line : 2
9343 <5d> DW_AT_type : <0x6e>
9344 ...
9345 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9346 <76> DW_AT_byte_size : 4
9347 <77> DW_AT_encoding : 5 (signed)
9348
9349 imports the wrong die ( 0x75 instead of 0x58 ).
9350 This case will be ignored until the gcc bug is fixed. */
9351 return;
9352 }
9353
9354 /* Figure out the local name after import. */
9355 import_alias = dwarf2_name (die, cu);
9356
9357 /* Figure out where the statement is being imported to. */
9358 import_prefix = determine_prefix (die, cu);
9359
9360 /* Figure out what the scope of the imported die is and prepend it
9361 to the name of the imported die. */
9362 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9363
9364 if (imported_die->tag != DW_TAG_namespace
9365 && imported_die->tag != DW_TAG_module)
9366 {
9367 imported_declaration = imported_name;
9368 canonical_name = imported_name_prefix;
9369 }
9370 else if (strlen (imported_name_prefix) > 0)
9371 canonical_name = obconcat (&objfile->objfile_obstack,
9372 imported_name_prefix,
9373 (cu->lang () == language_d
9374 ? "."
9375 : "::"),
9376 imported_name, (char *) NULL);
9377 else
9378 canonical_name = imported_name;
9379
9380 if (die->tag == DW_TAG_imported_module
9381 && cu->lang () == language_fortran)
9382 for (child_die = die->child; child_die && child_die->tag;
9383 child_die = child_die->sibling)
9384 {
9385 /* DWARF-4: A Fortran use statement with a “rename list” may be
9386 represented by an imported module entry with an import attribute
9387 referring to the module and owned entries corresponding to those
9388 entities that are renamed as part of being imported. */
9389
9390 if (child_die->tag != DW_TAG_imported_declaration)
9391 {
9392 complaint (_("child DW_TAG_imported_declaration expected "
9393 "- DIE at %s [in module %s]"),
9394 sect_offset_str (child_die->sect_off),
9395 objfile_name (objfile));
9396 continue;
9397 }
9398
9399 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9400 if (import_attr == NULL)
9401 {
9402 complaint (_("Tag '%s' has no DW_AT_import"),
9403 dwarf_tag_name (child_die->tag));
9404 continue;
9405 }
9406
9407 imported_cu = cu;
9408 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9409 &imported_cu);
9410 imported_name = dwarf2_name (imported_die, imported_cu);
9411 if (imported_name == NULL)
9412 {
9413 complaint (_("child DW_TAG_imported_declaration has unknown "
9414 "imported name - DIE at %s [in module %s]"),
9415 sect_offset_str (child_die->sect_off),
9416 objfile_name (objfile));
9417 continue;
9418 }
9419
9420 excludes.push_back (imported_name);
9421
9422 process_die (child_die, cu);
9423 }
9424
9425 add_using_directive (using_directives (cu),
9426 import_prefix,
9427 canonical_name,
9428 import_alias,
9429 imported_declaration,
9430 excludes,
9431 read_decl_line (die, cu),
9432 0,
9433 &objfile->objfile_obstack);
9434 }
9435
9436 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9437 types, but gives them a size of zero. Starting with version 14,
9438 ICC is compatible with GCC. */
9439
9440 static bool
9441 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9442 {
9443 if (!cu->checked_producer)
9444 check_producer (cu);
9445
9446 return cu->producer_is_icc_lt_14;
9447 }
9448
9449 /* ICC generates a DW_AT_type for C void functions. This was observed on
9450 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9451 which says that void functions should not have a DW_AT_type. */
9452
9453 static bool
9454 producer_is_icc (struct dwarf2_cu *cu)
9455 {
9456 if (!cu->checked_producer)
9457 check_producer (cu);
9458
9459 return cu->producer_is_icc;
9460 }
9461
9462 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9463 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9464 this, it was first present in GCC release 4.3.0. */
9465
9466 static bool
9467 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9468 {
9469 if (!cu->checked_producer)
9470 check_producer (cu);
9471
9472 return cu->producer_is_gcc_lt_4_3;
9473 }
9474
9475 /* See dwarf2/read.h. */
9476 bool
9477 producer_is_clang (struct dwarf2_cu *cu)
9478 {
9479 if (!cu->checked_producer)
9480 check_producer (cu);
9481
9482 return cu->producer_is_clang;
9483 }
9484
9485 static file_and_directory &
9486 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9487 {
9488 if (cu->per_cu->fnd != nullptr)
9489 return *cu->per_cu->fnd;
9490
9491 /* Find the filename. Do not use dwarf2_name here, since the filename
9492 is not a source language identifier. */
9493 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
9494 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
9495
9496 if (res.get_comp_dir () == nullptr
9497 && producer_is_gcc_lt_4_3 (cu)
9498 && res.get_name () != nullptr
9499 && IS_ABSOLUTE_PATH (res.get_name ()))
9500 {
9501 res.set_comp_dir (ldirname (res.get_name ()));
9502 res.set_name (make_unique_xstrdup (lbasename (res.get_name ())));
9503 }
9504
9505 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
9506 return *cu->per_cu->fnd;
9507 }
9508
9509 /* Handle DW_AT_stmt_list for a compilation unit.
9510 DIE is the DW_TAG_compile_unit die for CU.
9511 COMP_DIR is the compilation directory. LOWPC is passed to
9512 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9513
9514 static void
9515 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9516 const file_and_directory &fnd, CORE_ADDR lowpc,
9517 bool have_code) /* ARI: editCase function */
9518 {
9519 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9520 struct attribute *attr;
9521 hashval_t line_header_local_hash;
9522 void **slot;
9523 int decode_mapping;
9524
9525 gdb_assert (! cu->per_cu->is_debug_types);
9526
9527 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9528 if (attr == NULL || !attr->form_is_unsigned ())
9529 return;
9530
9531 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9532
9533 /* The line header hash table is only created if needed (it exists to
9534 prevent redundant reading of the line table for partial_units).
9535 If we're given a partial_unit, we'll need it. If we're given a
9536 compile_unit, then use the line header hash table if it's already
9537 created, but don't create one just yet. */
9538
9539 if (per_objfile->line_header_hash == NULL
9540 && die->tag == DW_TAG_partial_unit)
9541 {
9542 per_objfile->line_header_hash
9543 .reset (htab_create_alloc (127, line_header_hash_voidp,
9544 line_header_eq_voidp,
9545 htab_delete_entry<line_header>,
9546 xcalloc, xfree));
9547 }
9548
9549 line_header line_header_local (line_offset, cu->per_cu->is_dwz);
9550 line_header_local_hash = line_header_hash (&line_header_local);
9551 if (per_objfile->line_header_hash != NULL)
9552 {
9553 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9554 &line_header_local,
9555 line_header_local_hash, NO_INSERT);
9556
9557 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9558 is not present in *SLOT (since if there is something in *SLOT then
9559 it will be for a partial_unit). */
9560 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9561 {
9562 gdb_assert (*slot != NULL);
9563 cu->line_header = (struct line_header *) *slot;
9564 return;
9565 }
9566 }
9567
9568 /* dwarf_decode_line_header does not yet provide sufficient information.
9569 We always have to call also dwarf_decode_lines for it. */
9570 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
9571 fnd.get_comp_dir ());
9572 if (lh == NULL)
9573 return;
9574
9575 cu->line_header = lh.release ();
9576 cu->line_header_die_owner = die;
9577
9578 if (per_objfile->line_header_hash == NULL)
9579 slot = NULL;
9580 else
9581 {
9582 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9583 &line_header_local,
9584 line_header_local_hash, INSERT);
9585 gdb_assert (slot != NULL);
9586 }
9587 if (slot != NULL && *slot == NULL)
9588 {
9589 /* This newly decoded line number information unit will be owned
9590 by line_header_hash hash table. */
9591 *slot = cu->line_header;
9592 cu->line_header_die_owner = NULL;
9593 }
9594 else
9595 {
9596 /* We cannot free any current entry in (*slot) as that struct line_header
9597 may be already used by multiple CUs. Create only temporary decoded
9598 line_header for this CU - it may happen at most once for each line
9599 number information unit. And if we're not using line_header_hash
9600 then this is what we want as well. */
9601 gdb_assert (die->tag != DW_TAG_partial_unit);
9602 }
9603 decode_mapping = (die->tag != DW_TAG_partial_unit);
9604 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
9605 then there won't be any interesting code in the CU, but a check later on
9606 (in lnp_state_machine::check_line_address) will fail to properly exclude
9607 an entry that was removed via --gc-sections. */
9608 if (have_code)
9609 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
9610 }
9611
9612 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9613
9614 static void
9615 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9616 {
9617 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9618 struct objfile *objfile = per_objfile->objfile;
9619 struct gdbarch *gdbarch = objfile->arch ();
9620 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9621 CORE_ADDR highpc = ((CORE_ADDR) 0);
9622 struct attribute *attr;
9623 struct die_info *child_die;
9624 CORE_ADDR baseaddr;
9625
9626 prepare_one_comp_unit (cu, die, cu->lang ());
9627 baseaddr = objfile->text_section_offset ();
9628
9629 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9630
9631 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9632 from finish_block. */
9633 if (lowpc == ((CORE_ADDR) -1))
9634 lowpc = highpc;
9635 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9636
9637 file_and_directory &fnd = find_file_and_directory (die, cu);
9638
9639 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
9640 lowpc);
9641
9642 gdb_assert (per_objfile->sym_cu == nullptr);
9643 scoped_restore restore_sym_cu
9644 = make_scoped_restore (&per_objfile->sym_cu, cu);
9645
9646 /* Decode line number information if present. We do this before
9647 processing child DIEs, so that the line header table is available
9648 for DW_AT_decl_file. */
9649 handle_DW_AT_stmt_list (die, cu, fnd, lowpc, lowpc != highpc);
9650
9651 /* Process all dies in compilation unit. */
9652 if (die->child != NULL)
9653 {
9654 child_die = die->child;
9655 while (child_die && child_die->tag)
9656 {
9657 process_die (child_die, cu);
9658 child_die = child_die->sibling;
9659 }
9660 }
9661 per_objfile->sym_cu = nullptr;
9662
9663 /* Decode macro information, if present. Dwarf 2 macro information
9664 refers to information in the line number info statement program
9665 header, so we can only read it if we've read the header
9666 successfully. */
9667 attr = dwarf2_attr (die, DW_AT_macros, cu);
9668 if (attr == NULL)
9669 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9670 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9671 {
9672 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9673 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9674
9675 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
9676 }
9677 else
9678 {
9679 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9680 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9681 {
9682 unsigned int macro_offset = attr->as_unsigned ();
9683
9684 dwarf_decode_macros (cu, macro_offset, 0);
9685 }
9686 }
9687 }
9688
9689 void
9690 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
9691 {
9692 struct type_unit_group *tu_group;
9693 int first_time;
9694 struct attribute *attr;
9695 unsigned int i;
9696 struct signatured_type *sig_type;
9697
9698 gdb_assert (per_cu->is_debug_types);
9699 sig_type = (struct signatured_type *) per_cu;
9700
9701 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
9702
9703 /* If we're using .gdb_index (includes -readnow) then
9704 per_cu->type_unit_group may not have been set up yet. */
9705 if (sig_type->type_unit_group == NULL)
9706 sig_type->type_unit_group = get_type_unit_group (this, attr);
9707 tu_group = sig_type->type_unit_group;
9708
9709 /* If we've already processed this stmt_list there's no real need to
9710 do it again, we could fake it and just recreate the part we need
9711 (file name,index -> symtab mapping). If data shows this optimization
9712 is useful we can do it then. */
9713 type_unit_group_unshareable *tug_unshare
9714 = per_objfile->get_type_unit_group_unshareable (tu_group);
9715 first_time = tug_unshare->compunit_symtab == NULL;
9716
9717 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9718 debug info. */
9719 line_header_up lh;
9720 if (attr != NULL && attr->form_is_unsigned ())
9721 {
9722 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9723 lh = dwarf_decode_line_header (line_offset, this, nullptr);
9724 }
9725 if (lh == NULL)
9726 {
9727 if (first_time)
9728 start_compunit_symtab ("", NULL, 0);
9729 else
9730 {
9731 gdb_assert (tug_unshare->symtabs == NULL);
9732 gdb_assert (m_builder == nullptr);
9733 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9734 m_builder.reset (new struct buildsym_compunit
9735 (cust->objfile (), "",
9736 cust->dirname (),
9737 cust->language (),
9738 0, cust));
9739 list_in_scope = get_builder ()->get_file_symbols ();
9740 }
9741 return;
9742 }
9743
9744 line_header = lh.release ();
9745 line_header_die_owner = die;
9746
9747 if (first_time)
9748 {
9749 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
9750
9751 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9752 still initializing it, and our caller (a few levels up)
9753 process_full_type_unit still needs to know if this is the first
9754 time. */
9755
9756 tug_unshare->symtabs
9757 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
9758 struct symtab *, line_header->file_names_size ());
9759
9760 auto &file_names = line_header->file_names ();
9761 for (i = 0; i < file_names.size (); ++i)
9762 {
9763 file_entry &fe = file_names[i];
9764 dwarf2_start_subfile (this, fe, *line_header);
9765 buildsym_compunit *b = get_builder ();
9766 subfile *sf = b->get_current_subfile ();
9767
9768 if (sf->symtab == nullptr)
9769 {
9770 /* NOTE: start_subfile will recognize when it's been
9771 passed a file it has already seen. So we can't
9772 assume there's a simple mapping from
9773 cu->line_header->file_names to subfiles, plus
9774 cu->line_header->file_names may contain dups. */
9775 const char *name = sf->name.c_str ();
9776 const char *name_for_id = sf->name_for_id.c_str ();
9777 sf->symtab = allocate_symtab (cust, name, name_for_id);
9778 }
9779
9780 fe.symtab = b->get_current_subfile ()->symtab;
9781 tug_unshare->symtabs[i] = fe.symtab;
9782 }
9783 }
9784 else
9785 {
9786 gdb_assert (m_builder == nullptr);
9787 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9788 m_builder.reset (new struct buildsym_compunit
9789 (cust->objfile (), "",
9790 cust->dirname (),
9791 cust->language (),
9792 0, cust));
9793 list_in_scope = get_builder ()->get_file_symbols ();
9794
9795 auto &file_names = line_header->file_names ();
9796 for (i = 0; i < file_names.size (); ++i)
9797 {
9798 file_entry &fe = file_names[i];
9799 fe.symtab = tug_unshare->symtabs[i];
9800 }
9801 }
9802
9803 /* The main symtab is allocated last. Type units don't have DW_AT_name
9804 so they don't have a "real" (so to speak) symtab anyway.
9805 There is later code that will assign the main symtab to all symbols
9806 that don't have one. We need to handle the case of a symbol with a
9807 missing symtab (DW_AT_decl_file) anyway. */
9808 }
9809
9810 /* Process DW_TAG_type_unit.
9811 For TUs we want to skip the first top level sibling if it's not the
9812 actual type being defined by this TU. In this case the first top
9813 level sibling is there to provide context only. */
9814
9815 static void
9816 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9817 {
9818 struct die_info *child_die;
9819
9820 prepare_one_comp_unit (cu, die, language_minimal);
9821
9822 /* Initialize (or reinitialize) the machinery for building symtabs.
9823 We do this before processing child DIEs, so that the line header table
9824 is available for DW_AT_decl_file. */
9825 cu->setup_type_unit_groups (die);
9826
9827 if (die->child != NULL)
9828 {
9829 child_die = die->child;
9830 while (child_die && child_die->tag)
9831 {
9832 process_die (child_die, cu);
9833 child_die = child_die->sibling;
9834 }
9835 }
9836 }
9837 \f
9838 /* DWO/DWP files.
9839
9840 http://gcc.gnu.org/wiki/DebugFission
9841 http://gcc.gnu.org/wiki/DebugFissionDWP
9842
9843 To simplify handling of both DWO files ("object" files with the DWARF info)
9844 and DWP files (a file with the DWOs packaged up into one file), we treat
9845 DWP files as having a collection of virtual DWO files. */
9846
9847 static hashval_t
9848 hash_dwo_file (const void *item)
9849 {
9850 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9851 hashval_t hash;
9852
9853 hash = htab_hash_string (dwo_file->dwo_name);
9854 if (dwo_file->comp_dir != NULL)
9855 hash += htab_hash_string (dwo_file->comp_dir);
9856 return hash;
9857 }
9858
9859 static int
9860 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9861 {
9862 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9863 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9864
9865 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9866 return 0;
9867 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9868 return lhs->comp_dir == rhs->comp_dir;
9869 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9870 }
9871
9872 /* Allocate a hash table for DWO files. */
9873
9874 static htab_up
9875 allocate_dwo_file_hash_table ()
9876 {
9877 return htab_up (htab_create_alloc (41,
9878 hash_dwo_file,
9879 eq_dwo_file,
9880 htab_delete_entry<dwo_file>,
9881 xcalloc, xfree));
9882 }
9883
9884 /* Lookup DWO file DWO_NAME. */
9885
9886 static void **
9887 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
9888 const char *dwo_name,
9889 const char *comp_dir)
9890 {
9891 struct dwo_file find_entry;
9892 void **slot;
9893
9894 if (per_objfile->per_bfd->dwo_files == NULL)
9895 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
9896
9897 find_entry.dwo_name = dwo_name;
9898 find_entry.comp_dir = comp_dir;
9899 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
9900 INSERT);
9901
9902 return slot;
9903 }
9904
9905 static hashval_t
9906 hash_dwo_unit (const void *item)
9907 {
9908 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9909
9910 /* This drops the top 32 bits of the id, but is ok for a hash. */
9911 return dwo_unit->signature;
9912 }
9913
9914 static int
9915 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9916 {
9917 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9918 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9919
9920 /* The signature is assumed to be unique within the DWO file.
9921 So while object file CU dwo_id's always have the value zero,
9922 that's OK, assuming each object file DWO file has only one CU,
9923 and that's the rule for now. */
9924 return lhs->signature == rhs->signature;
9925 }
9926
9927 /* Allocate a hash table for DWO CUs,TUs.
9928 There is one of these tables for each of CUs,TUs for each DWO file. */
9929
9930 static htab_up
9931 allocate_dwo_unit_table ()
9932 {
9933 /* Start out with a pretty small number.
9934 Generally DWO files contain only one CU and maybe some TUs. */
9935 return htab_up (htab_create_alloc (3,
9936 hash_dwo_unit,
9937 eq_dwo_unit,
9938 NULL, xcalloc, xfree));
9939 }
9940
9941 /* die_reader_func for create_dwo_cu. */
9942
9943 static void
9944 create_dwo_cu_reader (const struct die_reader_specs *reader,
9945 const gdb_byte *info_ptr,
9946 struct die_info *comp_unit_die,
9947 struct dwo_file *dwo_file,
9948 struct dwo_unit *dwo_unit)
9949 {
9950 struct dwarf2_cu *cu = reader->cu;
9951 sect_offset sect_off = cu->per_cu->sect_off;
9952 struct dwarf2_section_info *section = cu->per_cu->section;
9953
9954 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
9955 if (!signature.has_value ())
9956 {
9957 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9958 " its dwo_id [in module %s]"),
9959 sect_offset_str (sect_off), dwo_file->dwo_name);
9960 return;
9961 }
9962
9963 dwo_unit->dwo_file = dwo_file;
9964 dwo_unit->signature = *signature;
9965 dwo_unit->section = section;
9966 dwo_unit->sect_off = sect_off;
9967 dwo_unit->length = cu->per_cu->length ();
9968
9969 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9970 sect_offset_str (sect_off),
9971 hex_string (dwo_unit->signature));
9972 }
9973
9974 /* Create the dwo_units for the CUs in a DWO_FILE.
9975 Note: This function processes DWO files only, not DWP files. */
9976
9977 static void
9978 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
9979 dwarf2_cu *cu, struct dwo_file &dwo_file,
9980 dwarf2_section_info &section, htab_up &cus_htab)
9981 {
9982 struct objfile *objfile = per_objfile->objfile;
9983 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
9984 const gdb_byte *info_ptr, *end_ptr;
9985
9986 section.read (objfile);
9987 info_ptr = section.buffer;
9988
9989 if (info_ptr == NULL)
9990 return;
9991
9992 dwarf_read_debug_printf ("Reading %s for %s:",
9993 section.get_name (),
9994 section.get_file_name ());
9995
9996 end_ptr = info_ptr + section.size;
9997 while (info_ptr < end_ptr)
9998 {
9999 struct dwarf2_per_cu_data per_cu;
10000 struct dwo_unit read_unit {};
10001 struct dwo_unit *dwo_unit;
10002 void **slot;
10003 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10004
10005 per_cu.per_bfd = per_bfd;
10006 per_cu.is_debug_types = 0;
10007 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10008 per_cu.section = &section;
10009
10010 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10011 if (!reader.dummy_p)
10012 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
10013 &dwo_file, &read_unit);
10014 info_ptr += per_cu.length ();
10015
10016 // If the unit could not be parsed, skip it.
10017 if (read_unit.dwo_file == NULL)
10018 continue;
10019
10020 if (cus_htab == NULL)
10021 cus_htab = allocate_dwo_unit_table ();
10022
10023 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
10024 struct dwo_unit);
10025 *dwo_unit = read_unit;
10026 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
10027 gdb_assert (slot != NULL);
10028 if (*slot != NULL)
10029 {
10030 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10031 sect_offset dup_sect_off = dup_cu->sect_off;
10032
10033 complaint (_("debug cu entry at offset %s is duplicate to"
10034 " the entry at offset %s, signature %s"),
10035 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
10036 hex_string (dwo_unit->signature));
10037 }
10038 *slot = (void *)dwo_unit;
10039 }
10040 }
10041
10042 /* DWP file .debug_{cu,tu}_index section format:
10043 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10044 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10045
10046 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10047 officially standard DWP format was published with DWARF v5 and is called
10048 Version 5. There are no versions 3 or 4.
10049
10050 DWP Version 1:
10051
10052 Both index sections have the same format, and serve to map a 64-bit
10053 signature to a set of section numbers. Each section begins with a header,
10054 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10055 indexes, and a pool of 32-bit section numbers. The index sections will be
10056 aligned at 8-byte boundaries in the file.
10057
10058 The index section header consists of:
10059
10060 V, 32 bit version number
10061 -, 32 bits unused
10062 N, 32 bit number of compilation units or type units in the index
10063 M, 32 bit number of slots in the hash table
10064
10065 Numbers are recorded using the byte order of the application binary.
10066
10067 The hash table begins at offset 16 in the section, and consists of an array
10068 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10069 order of the application binary). Unused slots in the hash table are 0.
10070 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10071
10072 The parallel table begins immediately after the hash table
10073 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10074 array of 32-bit indexes (using the byte order of the application binary),
10075 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10076 table contains a 32-bit index into the pool of section numbers. For unused
10077 hash table slots, the corresponding entry in the parallel table will be 0.
10078
10079 The pool of section numbers begins immediately following the hash table
10080 (at offset 16 + 12 * M from the beginning of the section). The pool of
10081 section numbers consists of an array of 32-bit words (using the byte order
10082 of the application binary). Each item in the array is indexed starting
10083 from 0. The hash table entry provides the index of the first section
10084 number in the set. Additional section numbers in the set follow, and the
10085 set is terminated by a 0 entry (section number 0 is not used in ELF).
10086
10087 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10088 section must be the first entry in the set, and the .debug_abbrev.dwo must
10089 be the second entry. Other members of the set may follow in any order.
10090
10091 ---
10092
10093 DWP Versions 2 and 5:
10094
10095 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10096 and the entries in the index tables are now offsets into these sections.
10097 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10098 section.
10099
10100 Index Section Contents:
10101 Header
10102 Hash Table of Signatures dwp_hash_table.hash_table
10103 Parallel Table of Indices dwp_hash_table.unit_table
10104 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10105 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10106
10107 The index section header consists of:
10108
10109 V, 32 bit version number
10110 L, 32 bit number of columns in the table of section offsets
10111 N, 32 bit number of compilation units or type units in the index
10112 M, 32 bit number of slots in the hash table
10113
10114 Numbers are recorded using the byte order of the application binary.
10115
10116 The hash table has the same format as version 1.
10117 The parallel table of indices has the same format as version 1,
10118 except that the entries are origin-1 indices into the table of sections
10119 offsets and the table of section sizes.
10120
10121 The table of offsets begins immediately following the parallel table
10122 (at offset 16 + 12 * M from the beginning of the section). The table is
10123 a two-dimensional array of 32-bit words (using the byte order of the
10124 application binary), with L columns and N+1 rows, in row-major order.
10125 Each row in the array is indexed starting from 0. The first row provides
10126 a key to the remaining rows: each column in this row provides an identifier
10127 for a debug section, and the offsets in the same column of subsequent rows
10128 refer to that section. The section identifiers for Version 2 are:
10129
10130 DW_SECT_INFO 1 .debug_info.dwo
10131 DW_SECT_TYPES 2 .debug_types.dwo
10132 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10133 DW_SECT_LINE 4 .debug_line.dwo
10134 DW_SECT_LOC 5 .debug_loc.dwo
10135 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10136 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10137 DW_SECT_MACRO 8 .debug_macro.dwo
10138
10139 The section identifiers for Version 5 are:
10140
10141 DW_SECT_INFO_V5 1 .debug_info.dwo
10142 DW_SECT_RESERVED_V5 2 --
10143 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10144 DW_SECT_LINE_V5 4 .debug_line.dwo
10145 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10146 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10147 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10148 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10149
10150 The offsets provided by the CU and TU index sections are the base offsets
10151 for the contributions made by each CU or TU to the corresponding section
10152 in the package file. Each CU and TU header contains an abbrev_offset
10153 field, used to find the abbreviations table for that CU or TU within the
10154 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10155 be interpreted as relative to the base offset given in the index section.
10156 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10157 should be interpreted as relative to the base offset for .debug_line.dwo,
10158 and offsets into other debug sections obtained from DWARF attributes should
10159 also be interpreted as relative to the corresponding base offset.
10160
10161 The table of sizes begins immediately following the table of offsets.
10162 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10163 with L columns and N rows, in row-major order. Each row in the array is
10164 indexed starting from 1 (row 0 is shared by the two tables).
10165
10166 ---
10167
10168 Hash table lookup is handled the same in version 1 and 2:
10169
10170 We assume that N and M will not exceed 2^32 - 1.
10171 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10172
10173 Given a 64-bit compilation unit signature or a type signature S, an entry
10174 in the hash table is located as follows:
10175
10176 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10177 the low-order k bits all set to 1.
10178
10179 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10180
10181 3) If the hash table entry at index H matches the signature, use that
10182 entry. If the hash table entry at index H is unused (all zeroes),
10183 terminate the search: the signature is not present in the table.
10184
10185 4) Let H = (H + H') modulo M. Repeat at Step 3.
10186
10187 Because M > N and H' and M are relatively prime, the search is guaranteed
10188 to stop at an unused slot or find the match. */
10189
10190 /* Create a hash table to map DWO IDs to their CU/TU entry in
10191 .debug_{info,types}.dwo in DWP_FILE.
10192 Returns NULL if there isn't one.
10193 Note: This function processes DWP files only, not DWO files. */
10194
10195 static struct dwp_hash_table *
10196 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
10197 struct dwp_file *dwp_file, int is_debug_types)
10198 {
10199 struct objfile *objfile = per_objfile->objfile;
10200 bfd *dbfd = dwp_file->dbfd.get ();
10201 const gdb_byte *index_ptr, *index_end;
10202 struct dwarf2_section_info *index;
10203 uint32_t version, nr_columns, nr_units, nr_slots;
10204 struct dwp_hash_table *htab;
10205
10206 if (is_debug_types)
10207 index = &dwp_file->sections.tu_index;
10208 else
10209 index = &dwp_file->sections.cu_index;
10210
10211 if (index->empty ())
10212 return NULL;
10213 index->read (objfile);
10214
10215 index_ptr = index->buffer;
10216 index_end = index_ptr + index->size;
10217
10218 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10219 For now it's safe to just read 4 bytes (particularly as it's difficult to
10220 tell if you're dealing with Version 5 before you've read the version). */
10221 version = read_4_bytes (dbfd, index_ptr);
10222 index_ptr += 4;
10223 if (version == 2 || version == 5)
10224 nr_columns = read_4_bytes (dbfd, index_ptr);
10225 else
10226 nr_columns = 0;
10227 index_ptr += 4;
10228 nr_units = read_4_bytes (dbfd, index_ptr);
10229 index_ptr += 4;
10230 nr_slots = read_4_bytes (dbfd, index_ptr);
10231 index_ptr += 4;
10232
10233 if (version != 1 && version != 2 && version != 5)
10234 {
10235 error (_("Dwarf Error: unsupported DWP file version (%s)"
10236 " [in module %s]"),
10237 pulongest (version), dwp_file->name);
10238 }
10239 if (nr_slots != (nr_slots & -nr_slots))
10240 {
10241 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10242 " is not power of 2 [in module %s]"),
10243 pulongest (nr_slots), dwp_file->name);
10244 }
10245
10246 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
10247 htab->version = version;
10248 htab->nr_columns = nr_columns;
10249 htab->nr_units = nr_units;
10250 htab->nr_slots = nr_slots;
10251 htab->hash_table = index_ptr;
10252 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10253
10254 /* Exit early if the table is empty. */
10255 if (nr_slots == 0 || nr_units == 0
10256 || (version == 2 && nr_columns == 0)
10257 || (version == 5 && nr_columns == 0))
10258 {
10259 /* All must be zero. */
10260 if (nr_slots != 0 || nr_units != 0
10261 || (version == 2 && nr_columns != 0)
10262 || (version == 5 && nr_columns != 0))
10263 {
10264 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10265 " all zero [in modules %s]"),
10266 dwp_file->name);
10267 }
10268 return htab;
10269 }
10270
10271 if (version == 1)
10272 {
10273 htab->section_pool.v1.indices =
10274 htab->unit_table + sizeof (uint32_t) * nr_slots;
10275 /* It's harder to decide whether the section is too small in v1.
10276 V1 is deprecated anyway so we punt. */
10277 }
10278 else if (version == 2)
10279 {
10280 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10281 int *ids = htab->section_pool.v2.section_ids;
10282 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
10283 /* Reverse map for error checking. */
10284 int ids_seen[DW_SECT_MAX + 1];
10285 int i;
10286
10287 if (nr_columns < 2)
10288 {
10289 error (_("Dwarf Error: bad DWP hash table, too few columns"
10290 " in section table [in module %s]"),
10291 dwp_file->name);
10292 }
10293 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10294 {
10295 error (_("Dwarf Error: bad DWP hash table, too many columns"
10296 " in section table [in module %s]"),
10297 dwp_file->name);
10298 }
10299 memset (ids, 255, sizeof_ids);
10300 memset (ids_seen, 255, sizeof (ids_seen));
10301 for (i = 0; i < nr_columns; ++i)
10302 {
10303 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10304
10305 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10306 {
10307 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10308 " in section table [in module %s]"),
10309 id, dwp_file->name);
10310 }
10311 if (ids_seen[id] != -1)
10312 {
10313 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10314 " id %d in section table [in module %s]"),
10315 id, dwp_file->name);
10316 }
10317 ids_seen[id] = i;
10318 ids[i] = id;
10319 }
10320 /* Must have exactly one info or types section. */
10321 if (((ids_seen[DW_SECT_INFO] != -1)
10322 + (ids_seen[DW_SECT_TYPES] != -1))
10323 != 1)
10324 {
10325 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10326 " DWO info/types section [in module %s]"),
10327 dwp_file->name);
10328 }
10329 /* Must have an abbrev section. */
10330 if (ids_seen[DW_SECT_ABBREV] == -1)
10331 {
10332 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10333 " section [in module %s]"),
10334 dwp_file->name);
10335 }
10336 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10337 htab->section_pool.v2.sizes =
10338 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10339 * nr_units * nr_columns);
10340 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10341 * nr_units * nr_columns))
10342 > index_end)
10343 {
10344 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10345 " [in module %s]"),
10346 dwp_file->name);
10347 }
10348 }
10349 else /* version == 5 */
10350 {
10351 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10352 int *ids = htab->section_pool.v5.section_ids;
10353 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
10354 /* Reverse map for error checking. */
10355 int ids_seen[DW_SECT_MAX_V5 + 1];
10356
10357 if (nr_columns < 2)
10358 {
10359 error (_("Dwarf Error: bad DWP hash table, too few columns"
10360 " in section table [in module %s]"),
10361 dwp_file->name);
10362 }
10363 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
10364 {
10365 error (_("Dwarf Error: bad DWP hash table, too many columns"
10366 " in section table [in module %s]"),
10367 dwp_file->name);
10368 }
10369 memset (ids, 255, sizeof_ids);
10370 memset (ids_seen, 255, sizeof (ids_seen));
10371 for (int i = 0; i < nr_columns; ++i)
10372 {
10373 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10374
10375 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
10376 {
10377 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10378 " in section table [in module %s]"),
10379 id, dwp_file->name);
10380 }
10381 if (ids_seen[id] != -1)
10382 {
10383 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10384 " id %d in section table [in module %s]"),
10385 id, dwp_file->name);
10386 }
10387 ids_seen[id] = i;
10388 ids[i] = id;
10389 }
10390 /* Must have seen an info section. */
10391 if (ids_seen[DW_SECT_INFO_V5] == -1)
10392 {
10393 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10394 " DWO info/types section [in module %s]"),
10395 dwp_file->name);
10396 }
10397 /* Must have an abbrev section. */
10398 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
10399 {
10400 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10401 " section [in module %s]"),
10402 dwp_file->name);
10403 }
10404 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10405 htab->section_pool.v5.sizes
10406 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
10407 * nr_units * nr_columns);
10408 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
10409 * nr_units * nr_columns))
10410 > index_end)
10411 {
10412 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10413 " [in module %s]"),
10414 dwp_file->name);
10415 }
10416 }
10417
10418 return htab;
10419 }
10420
10421 /* Update SECTIONS with the data from SECTP.
10422
10423 This function is like the other "locate" section routines, but in
10424 this context the sections to read comes from the DWP V1 hash table,
10425 not the full ELF section table.
10426
10427 The result is non-zero for success, or zero if an error was found. */
10428
10429 static int
10430 locate_v1_virtual_dwo_sections (asection *sectp,
10431 struct virtual_v1_dwo_sections *sections)
10432 {
10433 const struct dwop_section_names *names = &dwop_section_names;
10434
10435 if (names->abbrev_dwo.matches (sectp->name))
10436 {
10437 /* There can be only one. */
10438 if (sections->abbrev.s.section != NULL)
10439 return 0;
10440 sections->abbrev.s.section = sectp;
10441 sections->abbrev.size = bfd_section_size (sectp);
10442 }
10443 else if (names->info_dwo.matches (sectp->name)
10444 || names->types_dwo.matches (sectp->name))
10445 {
10446 /* There can be only one. */
10447 if (sections->info_or_types.s.section != NULL)
10448 return 0;
10449 sections->info_or_types.s.section = sectp;
10450 sections->info_or_types.size = bfd_section_size (sectp);
10451 }
10452 else if (names->line_dwo.matches (sectp->name))
10453 {
10454 /* There can be only one. */
10455 if (sections->line.s.section != NULL)
10456 return 0;
10457 sections->line.s.section = sectp;
10458 sections->line.size = bfd_section_size (sectp);
10459 }
10460 else if (names->loc_dwo.matches (sectp->name))
10461 {
10462 /* There can be only one. */
10463 if (sections->loc.s.section != NULL)
10464 return 0;
10465 sections->loc.s.section = sectp;
10466 sections->loc.size = bfd_section_size (sectp);
10467 }
10468 else if (names->macinfo_dwo.matches (sectp->name))
10469 {
10470 /* There can be only one. */
10471 if (sections->macinfo.s.section != NULL)
10472 return 0;
10473 sections->macinfo.s.section = sectp;
10474 sections->macinfo.size = bfd_section_size (sectp);
10475 }
10476 else if (names->macro_dwo.matches (sectp->name))
10477 {
10478 /* There can be only one. */
10479 if (sections->macro.s.section != NULL)
10480 return 0;
10481 sections->macro.s.section = sectp;
10482 sections->macro.size = bfd_section_size (sectp);
10483 }
10484 else if (names->str_offsets_dwo.matches (sectp->name))
10485 {
10486 /* There can be only one. */
10487 if (sections->str_offsets.s.section != NULL)
10488 return 0;
10489 sections->str_offsets.s.section = sectp;
10490 sections->str_offsets.size = bfd_section_size (sectp);
10491 }
10492 else
10493 {
10494 /* No other kind of section is valid. */
10495 return 0;
10496 }
10497
10498 return 1;
10499 }
10500
10501 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10502 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10503 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10504 This is for DWP version 1 files. */
10505
10506 static struct dwo_unit *
10507 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
10508 struct dwp_file *dwp_file,
10509 uint32_t unit_index,
10510 const char *comp_dir,
10511 ULONGEST signature, int is_debug_types)
10512 {
10513 const struct dwp_hash_table *dwp_htab =
10514 is_debug_types ? dwp_file->tus : dwp_file->cus;
10515 bfd *dbfd = dwp_file->dbfd.get ();
10516 const char *kind = is_debug_types ? "TU" : "CU";
10517 struct dwo_file *dwo_file;
10518 struct dwo_unit *dwo_unit;
10519 struct virtual_v1_dwo_sections sections;
10520 void **dwo_file_slot;
10521 int i;
10522
10523 gdb_assert (dwp_file->version == 1);
10524
10525 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10526 kind, pulongest (unit_index), hex_string (signature),
10527 dwp_file->name);
10528
10529 /* Fetch the sections of this DWO unit.
10530 Put a limit on the number of sections we look for so that bad data
10531 doesn't cause us to loop forever. */
10532
10533 #define MAX_NR_V1_DWO_SECTIONS \
10534 (1 /* .debug_info or .debug_types */ \
10535 + 1 /* .debug_abbrev */ \
10536 + 1 /* .debug_line */ \
10537 + 1 /* .debug_loc */ \
10538 + 1 /* .debug_str_offsets */ \
10539 + 1 /* .debug_macro or .debug_macinfo */ \
10540 + 1 /* trailing zero */)
10541
10542 memset (&sections, 0, sizeof (sections));
10543
10544 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10545 {
10546 asection *sectp;
10547 uint32_t section_nr =
10548 read_4_bytes (dbfd,
10549 dwp_htab->section_pool.v1.indices
10550 + (unit_index + i) * sizeof (uint32_t));
10551
10552 if (section_nr == 0)
10553 break;
10554 if (section_nr >= dwp_file->num_sections)
10555 {
10556 error (_("Dwarf Error: bad DWP hash table, section number too large"
10557 " [in module %s]"),
10558 dwp_file->name);
10559 }
10560
10561 sectp = dwp_file->elf_sections[section_nr];
10562 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10563 {
10564 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10565 " [in module %s]"),
10566 dwp_file->name);
10567 }
10568 }
10569
10570 if (i < 2
10571 || sections.info_or_types.empty ()
10572 || sections.abbrev.empty ())
10573 {
10574 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10575 " [in module %s]"),
10576 dwp_file->name);
10577 }
10578 if (i == MAX_NR_V1_DWO_SECTIONS)
10579 {
10580 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10581 " [in module %s]"),
10582 dwp_file->name);
10583 }
10584
10585 /* It's easier for the rest of the code if we fake a struct dwo_file and
10586 have dwo_unit "live" in that. At least for now.
10587
10588 The DWP file can be made up of a random collection of CUs and TUs.
10589 However, for each CU + set of TUs that came from the same original DWO
10590 file, we can combine them back into a virtual DWO file to save space
10591 (fewer struct dwo_file objects to allocate). Remember that for really
10592 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10593
10594 std::string virtual_dwo_name =
10595 string_printf ("virtual-dwo/%d-%d-%d-%d",
10596 sections.abbrev.get_id (),
10597 sections.line.get_id (),
10598 sections.loc.get_id (),
10599 sections.str_offsets.get_id ());
10600 /* Can we use an existing virtual DWO file? */
10601 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10602 comp_dir);
10603 /* Create one if necessary. */
10604 if (*dwo_file_slot == NULL)
10605 {
10606 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10607 virtual_dwo_name.c_str ());
10608
10609 dwo_file = new struct dwo_file;
10610 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10611 dwo_file->comp_dir = comp_dir;
10612 dwo_file->sections.abbrev = sections.abbrev;
10613 dwo_file->sections.line = sections.line;
10614 dwo_file->sections.loc = sections.loc;
10615 dwo_file->sections.macinfo = sections.macinfo;
10616 dwo_file->sections.macro = sections.macro;
10617 dwo_file->sections.str_offsets = sections.str_offsets;
10618 /* The "str" section is global to the entire DWP file. */
10619 dwo_file->sections.str = dwp_file->sections.str;
10620 /* The info or types section is assigned below to dwo_unit,
10621 there's no need to record it in dwo_file.
10622 Also, we can't simply record type sections in dwo_file because
10623 we record a pointer into the vector in dwo_unit. As we collect more
10624 types we'll grow the vector and eventually have to reallocate space
10625 for it, invalidating all copies of pointers into the previous
10626 contents. */
10627 *dwo_file_slot = dwo_file;
10628 }
10629 else
10630 {
10631 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10632 virtual_dwo_name.c_str ());
10633
10634 dwo_file = (struct dwo_file *) *dwo_file_slot;
10635 }
10636
10637 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10638 dwo_unit->dwo_file = dwo_file;
10639 dwo_unit->signature = signature;
10640 dwo_unit->section =
10641 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10642 *dwo_unit->section = sections.info_or_types;
10643 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10644
10645 return dwo_unit;
10646 }
10647
10648 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10649 simplify them. Given a pointer to the containing section SECTION, and
10650 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10651 virtual section of just that piece. */
10652
10653 static struct dwarf2_section_info
10654 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
10655 struct dwarf2_section_info *section,
10656 bfd_size_type offset, bfd_size_type size)
10657 {
10658 struct dwarf2_section_info result;
10659 asection *sectp;
10660
10661 gdb_assert (section != NULL);
10662 gdb_assert (!section->is_virtual);
10663
10664 memset (&result, 0, sizeof (result));
10665 result.s.containing_section = section;
10666 result.is_virtual = true;
10667
10668 if (size == 0)
10669 return result;
10670
10671 sectp = section->get_bfd_section ();
10672
10673 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10674 bounds of the real section. This is a pretty-rare event, so just
10675 flag an error (easier) instead of a warning and trying to cope. */
10676 if (sectp == NULL
10677 || offset + size > bfd_section_size (sectp))
10678 {
10679 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10680 " in section %s [in module %s]"),
10681 sectp ? bfd_section_name (sectp) : "<unknown>",
10682 objfile_name (per_objfile->objfile));
10683 }
10684
10685 result.virtual_offset = offset;
10686 result.size = size;
10687 return result;
10688 }
10689
10690 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10691 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10692 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10693 This is for DWP version 2 files. */
10694
10695 static struct dwo_unit *
10696 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
10697 struct dwp_file *dwp_file,
10698 uint32_t unit_index,
10699 const char *comp_dir,
10700 ULONGEST signature, int is_debug_types)
10701 {
10702 const struct dwp_hash_table *dwp_htab =
10703 is_debug_types ? dwp_file->tus : dwp_file->cus;
10704 bfd *dbfd = dwp_file->dbfd.get ();
10705 const char *kind = is_debug_types ? "TU" : "CU";
10706 struct dwo_file *dwo_file;
10707 struct dwo_unit *dwo_unit;
10708 struct virtual_v2_or_v5_dwo_sections sections;
10709 void **dwo_file_slot;
10710 int i;
10711
10712 gdb_assert (dwp_file->version == 2);
10713
10714 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10715 kind, pulongest (unit_index), hex_string (signature),
10716 dwp_file->name);
10717
10718 /* Fetch the section offsets of this DWO unit. */
10719
10720 memset (&sections, 0, sizeof (sections));
10721
10722 for (i = 0; i < dwp_htab->nr_columns; ++i)
10723 {
10724 uint32_t offset = read_4_bytes (dbfd,
10725 dwp_htab->section_pool.v2.offsets
10726 + (((unit_index - 1) * dwp_htab->nr_columns
10727 + i)
10728 * sizeof (uint32_t)));
10729 uint32_t size = read_4_bytes (dbfd,
10730 dwp_htab->section_pool.v2.sizes
10731 + (((unit_index - 1) * dwp_htab->nr_columns
10732 + i)
10733 * sizeof (uint32_t)));
10734
10735 switch (dwp_htab->section_pool.v2.section_ids[i])
10736 {
10737 case DW_SECT_INFO:
10738 case DW_SECT_TYPES:
10739 sections.info_or_types_offset = offset;
10740 sections.info_or_types_size = size;
10741 break;
10742 case DW_SECT_ABBREV:
10743 sections.abbrev_offset = offset;
10744 sections.abbrev_size = size;
10745 break;
10746 case DW_SECT_LINE:
10747 sections.line_offset = offset;
10748 sections.line_size = size;
10749 break;
10750 case DW_SECT_LOC:
10751 sections.loc_offset = offset;
10752 sections.loc_size = size;
10753 break;
10754 case DW_SECT_STR_OFFSETS:
10755 sections.str_offsets_offset = offset;
10756 sections.str_offsets_size = size;
10757 break;
10758 case DW_SECT_MACINFO:
10759 sections.macinfo_offset = offset;
10760 sections.macinfo_size = size;
10761 break;
10762 case DW_SECT_MACRO:
10763 sections.macro_offset = offset;
10764 sections.macro_size = size;
10765 break;
10766 }
10767 }
10768
10769 /* It's easier for the rest of the code if we fake a struct dwo_file and
10770 have dwo_unit "live" in that. At least for now.
10771
10772 The DWP file can be made up of a random collection of CUs and TUs.
10773 However, for each CU + set of TUs that came from the same original DWO
10774 file, we can combine them back into a virtual DWO file to save space
10775 (fewer struct dwo_file objects to allocate). Remember that for really
10776 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10777
10778 std::string virtual_dwo_name =
10779 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10780 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10781 (long) (sections.line_size ? sections.line_offset : 0),
10782 (long) (sections.loc_size ? sections.loc_offset : 0),
10783 (long) (sections.str_offsets_size
10784 ? sections.str_offsets_offset : 0));
10785 /* Can we use an existing virtual DWO file? */
10786 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10787 comp_dir);
10788 /* Create one if necessary. */
10789 if (*dwo_file_slot == NULL)
10790 {
10791 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10792 virtual_dwo_name.c_str ());
10793
10794 dwo_file = new struct dwo_file;
10795 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10796 dwo_file->comp_dir = comp_dir;
10797 dwo_file->sections.abbrev =
10798 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
10799 sections.abbrev_offset,
10800 sections.abbrev_size);
10801 dwo_file->sections.line =
10802 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
10803 sections.line_offset,
10804 sections.line_size);
10805 dwo_file->sections.loc =
10806 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
10807 sections.loc_offset, sections.loc_size);
10808 dwo_file->sections.macinfo =
10809 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
10810 sections.macinfo_offset,
10811 sections.macinfo_size);
10812 dwo_file->sections.macro =
10813 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
10814 sections.macro_offset,
10815 sections.macro_size);
10816 dwo_file->sections.str_offsets =
10817 create_dwp_v2_or_v5_section (per_objfile,
10818 &dwp_file->sections.str_offsets,
10819 sections.str_offsets_offset,
10820 sections.str_offsets_size);
10821 /* The "str" section is global to the entire DWP file. */
10822 dwo_file->sections.str = dwp_file->sections.str;
10823 /* The info or types section is assigned below to dwo_unit,
10824 there's no need to record it in dwo_file.
10825 Also, we can't simply record type sections in dwo_file because
10826 we record a pointer into the vector in dwo_unit. As we collect more
10827 types we'll grow the vector and eventually have to reallocate space
10828 for it, invalidating all copies of pointers into the previous
10829 contents. */
10830 *dwo_file_slot = dwo_file;
10831 }
10832 else
10833 {
10834 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10835 virtual_dwo_name.c_str ());
10836
10837 dwo_file = (struct dwo_file *) *dwo_file_slot;
10838 }
10839
10840 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10841 dwo_unit->dwo_file = dwo_file;
10842 dwo_unit->signature = signature;
10843 dwo_unit->section =
10844 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10845 *dwo_unit->section = create_dwp_v2_or_v5_section
10846 (per_objfile,
10847 is_debug_types
10848 ? &dwp_file->sections.types
10849 : &dwp_file->sections.info,
10850 sections.info_or_types_offset,
10851 sections.info_or_types_size);
10852 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10853
10854 return dwo_unit;
10855 }
10856
10857 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10858 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10859 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10860 This is for DWP version 5 files. */
10861
10862 static struct dwo_unit *
10863 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
10864 struct dwp_file *dwp_file,
10865 uint32_t unit_index,
10866 const char *comp_dir,
10867 ULONGEST signature, int is_debug_types)
10868 {
10869 const struct dwp_hash_table *dwp_htab
10870 = is_debug_types ? dwp_file->tus : dwp_file->cus;
10871 bfd *dbfd = dwp_file->dbfd.get ();
10872 const char *kind = is_debug_types ? "TU" : "CU";
10873 struct dwo_file *dwo_file;
10874 struct dwo_unit *dwo_unit;
10875 struct virtual_v2_or_v5_dwo_sections sections {};
10876 void **dwo_file_slot;
10877
10878 gdb_assert (dwp_file->version == 5);
10879
10880 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10881 kind, pulongest (unit_index), hex_string (signature),
10882 dwp_file->name);
10883
10884 /* Fetch the section offsets of this DWO unit. */
10885
10886 /* memset (&sections, 0, sizeof (sections)); */
10887
10888 for (int i = 0; i < dwp_htab->nr_columns; ++i)
10889 {
10890 uint32_t offset = read_4_bytes (dbfd,
10891 dwp_htab->section_pool.v5.offsets
10892 + (((unit_index - 1)
10893 * dwp_htab->nr_columns
10894 + i)
10895 * sizeof (uint32_t)));
10896 uint32_t size = read_4_bytes (dbfd,
10897 dwp_htab->section_pool.v5.sizes
10898 + (((unit_index - 1) * dwp_htab->nr_columns
10899 + i)
10900 * sizeof (uint32_t)));
10901
10902 switch (dwp_htab->section_pool.v5.section_ids[i])
10903 {
10904 case DW_SECT_ABBREV_V5:
10905 sections.abbrev_offset = offset;
10906 sections.abbrev_size = size;
10907 break;
10908 case DW_SECT_INFO_V5:
10909 sections.info_or_types_offset = offset;
10910 sections.info_or_types_size = size;
10911 break;
10912 case DW_SECT_LINE_V5:
10913 sections.line_offset = offset;
10914 sections.line_size = size;
10915 break;
10916 case DW_SECT_LOCLISTS_V5:
10917 sections.loclists_offset = offset;
10918 sections.loclists_size = size;
10919 break;
10920 case DW_SECT_MACRO_V5:
10921 sections.macro_offset = offset;
10922 sections.macro_size = size;
10923 break;
10924 case DW_SECT_RNGLISTS_V5:
10925 sections.rnglists_offset = offset;
10926 sections.rnglists_size = size;
10927 break;
10928 case DW_SECT_STR_OFFSETS_V5:
10929 sections.str_offsets_offset = offset;
10930 sections.str_offsets_size = size;
10931 break;
10932 case DW_SECT_RESERVED_V5:
10933 default:
10934 break;
10935 }
10936 }
10937
10938 /* It's easier for the rest of the code if we fake a struct dwo_file and
10939 have dwo_unit "live" in that. At least for now.
10940
10941 The DWP file can be made up of a random collection of CUs and TUs.
10942 However, for each CU + set of TUs that came from the same original DWO
10943 file, we can combine them back into a virtual DWO file to save space
10944 (fewer struct dwo_file objects to allocate). Remember that for really
10945 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10946
10947 std::string virtual_dwo_name =
10948 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10949 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10950 (long) (sections.line_size ? sections.line_offset : 0),
10951 (long) (sections.loclists_size ? sections.loclists_offset : 0),
10952 (long) (sections.str_offsets_size
10953 ? sections.str_offsets_offset : 0),
10954 (long) (sections.macro_size ? sections.macro_offset : 0),
10955 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
10956 /* Can we use an existing virtual DWO file? */
10957 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
10958 virtual_dwo_name.c_str (),
10959 comp_dir);
10960 /* Create one if necessary. */
10961 if (*dwo_file_slot == NULL)
10962 {
10963 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10964 virtual_dwo_name.c_str ());
10965
10966 dwo_file = new struct dwo_file;
10967 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10968 dwo_file->comp_dir = comp_dir;
10969 dwo_file->sections.abbrev =
10970 create_dwp_v2_or_v5_section (per_objfile,
10971 &dwp_file->sections.abbrev,
10972 sections.abbrev_offset,
10973 sections.abbrev_size);
10974 dwo_file->sections.line =
10975 create_dwp_v2_or_v5_section (per_objfile,
10976 &dwp_file->sections.line,
10977 sections.line_offset, sections.line_size);
10978 dwo_file->sections.macro =
10979 create_dwp_v2_or_v5_section (per_objfile,
10980 &dwp_file->sections.macro,
10981 sections.macro_offset,
10982 sections.macro_size);
10983 dwo_file->sections.loclists =
10984 create_dwp_v2_or_v5_section (per_objfile,
10985 &dwp_file->sections.loclists,
10986 sections.loclists_offset,
10987 sections.loclists_size);
10988 dwo_file->sections.rnglists =
10989 create_dwp_v2_or_v5_section (per_objfile,
10990 &dwp_file->sections.rnglists,
10991 sections.rnglists_offset,
10992 sections.rnglists_size);
10993 dwo_file->sections.str_offsets =
10994 create_dwp_v2_or_v5_section (per_objfile,
10995 &dwp_file->sections.str_offsets,
10996 sections.str_offsets_offset,
10997 sections.str_offsets_size);
10998 /* The "str" section is global to the entire DWP file. */
10999 dwo_file->sections.str = dwp_file->sections.str;
11000 /* The info or types section is assigned below to dwo_unit,
11001 there's no need to record it in dwo_file.
11002 Also, we can't simply record type sections in dwo_file because
11003 we record a pointer into the vector in dwo_unit. As we collect more
11004 types we'll grow the vector and eventually have to reallocate space
11005 for it, invalidating all copies of pointers into the previous
11006 contents. */
11007 *dwo_file_slot = dwo_file;
11008 }
11009 else
11010 {
11011 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11012 virtual_dwo_name.c_str ());
11013
11014 dwo_file = (struct dwo_file *) *dwo_file_slot;
11015 }
11016
11017 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11018 dwo_unit->dwo_file = dwo_file;
11019 dwo_unit->signature = signature;
11020 dwo_unit->section
11021 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11022 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
11023 &dwp_file->sections.info,
11024 sections.info_or_types_offset,
11025 sections.info_or_types_size);
11026 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11027
11028 return dwo_unit;
11029 }
11030
11031 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11032 Returns NULL if the signature isn't found. */
11033
11034 static struct dwo_unit *
11035 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
11036 struct dwp_file *dwp_file, const char *comp_dir,
11037 ULONGEST signature, int is_debug_types)
11038 {
11039 const struct dwp_hash_table *dwp_htab =
11040 is_debug_types ? dwp_file->tus : dwp_file->cus;
11041 bfd *dbfd = dwp_file->dbfd.get ();
11042 uint32_t mask = dwp_htab->nr_slots - 1;
11043 uint32_t hash = signature & mask;
11044 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11045 unsigned int i;
11046 void **slot;
11047 struct dwo_unit find_dwo_cu;
11048
11049 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11050 find_dwo_cu.signature = signature;
11051 slot = htab_find_slot (is_debug_types
11052 ? dwp_file->loaded_tus.get ()
11053 : dwp_file->loaded_cus.get (),
11054 &find_dwo_cu, INSERT);
11055
11056 if (*slot != NULL)
11057 return (struct dwo_unit *) *slot;
11058
11059 /* Use a for loop so that we don't loop forever on bad debug info. */
11060 for (i = 0; i < dwp_htab->nr_slots; ++i)
11061 {
11062 ULONGEST signature_in_table;
11063
11064 signature_in_table =
11065 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11066 if (signature_in_table == signature)
11067 {
11068 uint32_t unit_index =
11069 read_4_bytes (dbfd,
11070 dwp_htab->unit_table + hash * sizeof (uint32_t));
11071
11072 if (dwp_file->version == 1)
11073 {
11074 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11075 unit_index, comp_dir,
11076 signature, is_debug_types);
11077 }
11078 else if (dwp_file->version == 2)
11079 {
11080 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11081 unit_index, comp_dir,
11082 signature, is_debug_types);
11083 }
11084 else /* version == 5 */
11085 {
11086 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11087 unit_index, comp_dir,
11088 signature, is_debug_types);
11089 }
11090 return (struct dwo_unit *) *slot;
11091 }
11092 if (signature_in_table == 0)
11093 return NULL;
11094 hash = (hash + hash2) & mask;
11095 }
11096
11097 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11098 " [in module %s]"),
11099 dwp_file->name);
11100 }
11101
11102 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11103 Open the file specified by FILE_NAME and hand it off to BFD for
11104 preliminary analysis. Return a newly initialized bfd *, which
11105 includes a canonicalized copy of FILE_NAME.
11106 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11107 SEARCH_CWD is true if the current directory is to be searched.
11108 It will be searched before debug-file-directory.
11109 If successful, the file is added to the bfd include table of the
11110 objfile's bfd (see gdb_bfd_record_inclusion).
11111 If unable to find/open the file, return NULL.
11112 NOTE: This function is derived from symfile_bfd_open. */
11113
11114 static gdb_bfd_ref_ptr
11115 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11116 const char *file_name, int is_dwp, int search_cwd)
11117 {
11118 int desc;
11119 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11120 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11121 to debug_file_directory. */
11122 const char *search_path;
11123 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11124
11125 gdb::unique_xmalloc_ptr<char> search_path_holder;
11126 if (search_cwd)
11127 {
11128 if (!debug_file_directory.empty ())
11129 {
11130 search_path_holder.reset (concat (".", dirname_separator_string,
11131 debug_file_directory.c_str (),
11132 (char *) NULL));
11133 search_path = search_path_holder.get ();
11134 }
11135 else
11136 search_path = ".";
11137 }
11138 else
11139 search_path = debug_file_directory.c_str ();
11140
11141 /* Add the path for the executable binary to the list of search paths. */
11142 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
11143 search_path_holder.reset (concat (objfile_dir.c_str (),
11144 dirname_separator_string,
11145 search_path, nullptr));
11146 search_path = search_path_holder.get ();
11147
11148 openp_flags flags = OPF_RETURN_REALPATH;
11149 if (is_dwp)
11150 flags |= OPF_SEARCH_IN_PATH;
11151
11152 gdb::unique_xmalloc_ptr<char> absolute_name;
11153 desc = openp (search_path, flags, file_name,
11154 O_RDONLY | O_BINARY, &absolute_name);
11155 if (desc < 0)
11156 return NULL;
11157
11158 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11159 gnutarget, desc));
11160 if (sym_bfd == NULL)
11161 return NULL;
11162 bfd_set_cacheable (sym_bfd.get (), 1);
11163
11164 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11165 return NULL;
11166
11167 /* Success. Record the bfd as having been included by the objfile's bfd.
11168 This is important because things like demangled_names_hash lives in the
11169 objfile's per_bfd space and may have references to things like symbol
11170 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11171 gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
11172
11173 return sym_bfd;
11174 }
11175
11176 /* Try to open DWO file FILE_NAME.
11177 COMP_DIR is the DW_AT_comp_dir attribute.
11178 The result is the bfd handle of the file.
11179 If there is a problem finding or opening the file, return NULL.
11180 Upon success, the canonicalized path of the file is stored in the bfd,
11181 same as symfile_bfd_open. */
11182
11183 static gdb_bfd_ref_ptr
11184 open_dwo_file (dwarf2_per_objfile *per_objfile,
11185 const char *file_name, const char *comp_dir)
11186 {
11187 if (IS_ABSOLUTE_PATH (file_name))
11188 return try_open_dwop_file (per_objfile, file_name,
11189 0 /*is_dwp*/, 0 /*search_cwd*/);
11190
11191 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11192
11193 if (comp_dir != NULL)
11194 {
11195 std::string path_to_try = path_join (comp_dir, file_name);
11196
11197 /* NOTE: If comp_dir is a relative path, this will also try the
11198 search path, which seems useful. */
11199 gdb_bfd_ref_ptr abfd (try_open_dwop_file
11200 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11201
11202 if (abfd != NULL)
11203 return abfd;
11204 }
11205
11206 /* That didn't work, try debug-file-directory, which, despite its name,
11207 is a list of paths. */
11208
11209 if (debug_file_directory.empty ())
11210 return NULL;
11211
11212 return try_open_dwop_file (per_objfile, file_name,
11213 0 /*is_dwp*/, 1 /*search_cwd*/);
11214 }
11215
11216 /* This function is mapped across the sections and remembers the offset and
11217 size of each of the DWO debugging sections we are interested in. */
11218
11219 static void
11220 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
11221 asection *sectp, dwo_sections *dwo_sections)
11222 {
11223 const struct dwop_section_names *names = &dwop_section_names;
11224
11225 struct dwarf2_section_info *dw_sect = nullptr;
11226
11227 if (names->abbrev_dwo.matches (sectp->name))
11228 dw_sect = &dwo_sections->abbrev;
11229 else if (names->info_dwo.matches (sectp->name))
11230 dw_sect = &dwo_sections->info;
11231 else if (names->line_dwo.matches (sectp->name))
11232 dw_sect = &dwo_sections->line;
11233 else if (names->loc_dwo.matches (sectp->name))
11234 dw_sect = &dwo_sections->loc;
11235 else if (names->loclists_dwo.matches (sectp->name))
11236 dw_sect = &dwo_sections->loclists;
11237 else if (names->macinfo_dwo.matches (sectp->name))
11238 dw_sect = &dwo_sections->macinfo;
11239 else if (names->macro_dwo.matches (sectp->name))
11240 dw_sect = &dwo_sections->macro;
11241 else if (names->rnglists_dwo.matches (sectp->name))
11242 dw_sect = &dwo_sections->rnglists;
11243 else if (names->str_dwo.matches (sectp->name))
11244 dw_sect = &dwo_sections->str;
11245 else if (names->str_offsets_dwo.matches (sectp->name))
11246 dw_sect = &dwo_sections->str_offsets;
11247 else if (names->types_dwo.matches (sectp->name))
11248 {
11249 struct dwarf2_section_info type_section;
11250
11251 memset (&type_section, 0, sizeof (type_section));
11252 dwo_sections->types.push_back (type_section);
11253 dw_sect = &dwo_sections->types.back ();
11254 }
11255
11256 if (dw_sect != nullptr)
11257 {
11258 dw_sect->s.section = sectp;
11259 dw_sect->size = bfd_section_size (sectp);
11260 dw_sect->read (objfile);
11261 }
11262 }
11263
11264 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11265 by PER_CU. This is for the non-DWP case.
11266 The result is NULL if DWO_NAME can't be found. */
11267
11268 static struct dwo_file *
11269 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
11270 const char *comp_dir)
11271 {
11272 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11273
11274 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
11275 if (dbfd == NULL)
11276 {
11277 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
11278
11279 return NULL;
11280 }
11281
11282 dwo_file_up dwo_file (new struct dwo_file);
11283 dwo_file->dwo_name = dwo_name;
11284 dwo_file->comp_dir = comp_dir;
11285 dwo_file->dbfd = std::move (dbfd);
11286
11287 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
11288 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
11289 sec, &dwo_file->sections);
11290
11291 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
11292 dwo_file->cus);
11293
11294 if (cu->per_cu->version () < 5)
11295 {
11296 create_debug_types_hash_table (per_objfile, dwo_file.get (),
11297 dwo_file->sections.types, dwo_file->tus);
11298 }
11299 else
11300 {
11301 create_debug_type_hash_table (per_objfile, dwo_file.get (),
11302 &dwo_file->sections.info, dwo_file->tus,
11303 rcuh_kind::COMPILE);
11304 }
11305
11306 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
11307
11308 return dwo_file.release ();
11309 }
11310
11311 /* This function is mapped across the sections and remembers the offset and
11312 size of each of the DWP debugging sections common to version 1 and 2 that
11313 we are interested in. */
11314
11315 static void
11316 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
11317 asection *sectp, dwp_file *dwp_file)
11318 {
11319 const struct dwop_section_names *names = &dwop_section_names;
11320 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11321
11322 /* Record the ELF section number for later lookup: this is what the
11323 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11324 gdb_assert (elf_section_nr < dwp_file->num_sections);
11325 dwp_file->elf_sections[elf_section_nr] = sectp;
11326
11327 /* Look for specific sections that we need. */
11328 struct dwarf2_section_info *dw_sect = nullptr;
11329 if (names->str_dwo.matches (sectp->name))
11330 dw_sect = &dwp_file->sections.str;
11331 else if (names->cu_index.matches (sectp->name))
11332 dw_sect = &dwp_file->sections.cu_index;
11333 else if (names->tu_index.matches (sectp->name))
11334 dw_sect = &dwp_file->sections.tu_index;
11335
11336 if (dw_sect != nullptr)
11337 {
11338 dw_sect->s.section = sectp;
11339 dw_sect->size = bfd_section_size (sectp);
11340 dw_sect->read (objfile);
11341 }
11342 }
11343
11344 /* This function is mapped across the sections and remembers the offset and
11345 size of each of the DWP version 2 debugging sections that we are interested
11346 in. This is split into a separate function because we don't know if we
11347 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11348
11349 static void
11350 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
11351 asection *sectp, void *dwp_file_ptr)
11352 {
11353 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11354 const struct dwop_section_names *names = &dwop_section_names;
11355 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11356
11357 /* Record the ELF section number for later lookup: this is what the
11358 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11359 gdb_assert (elf_section_nr < dwp_file->num_sections);
11360 dwp_file->elf_sections[elf_section_nr] = sectp;
11361
11362 /* Look for specific sections that we need. */
11363 struct dwarf2_section_info *dw_sect = nullptr;
11364 if (names->abbrev_dwo.matches (sectp->name))
11365 dw_sect = &dwp_file->sections.abbrev;
11366 else if (names->info_dwo.matches (sectp->name))
11367 dw_sect = &dwp_file->sections.info;
11368 else if (names->line_dwo.matches (sectp->name))
11369 dw_sect = &dwp_file->sections.line;
11370 else if (names->loc_dwo.matches (sectp->name))
11371 dw_sect = &dwp_file->sections.loc;
11372 else if (names->macinfo_dwo.matches (sectp->name))
11373 dw_sect = &dwp_file->sections.macinfo;
11374 else if (names->macro_dwo.matches (sectp->name))
11375 dw_sect = &dwp_file->sections.macro;
11376 else if (names->str_offsets_dwo.matches (sectp->name))
11377 dw_sect = &dwp_file->sections.str_offsets;
11378 else if (names->types_dwo.matches (sectp->name))
11379 dw_sect = &dwp_file->sections.types;
11380
11381 if (dw_sect != nullptr)
11382 {
11383 dw_sect->s.section = sectp;
11384 dw_sect->size = bfd_section_size (sectp);
11385 dw_sect->read (objfile);
11386 }
11387 }
11388
11389 /* This function is mapped across the sections and remembers the offset and
11390 size of each of the DWP version 5 debugging sections that we are interested
11391 in. This is split into a separate function because we don't know if we
11392 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11393
11394 static void
11395 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
11396 asection *sectp, void *dwp_file_ptr)
11397 {
11398 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11399 const struct dwop_section_names *names = &dwop_section_names;
11400 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11401
11402 /* Record the ELF section number for later lookup: this is what the
11403 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11404 gdb_assert (elf_section_nr < dwp_file->num_sections);
11405 dwp_file->elf_sections[elf_section_nr] = sectp;
11406
11407 /* Look for specific sections that we need. */
11408 struct dwarf2_section_info *dw_sect = nullptr;
11409 if (names->abbrev_dwo.matches (sectp->name))
11410 dw_sect = &dwp_file->sections.abbrev;
11411 else if (names->info_dwo.matches (sectp->name))
11412 dw_sect = &dwp_file->sections.info;
11413 else if (names->line_dwo.matches (sectp->name))
11414 dw_sect = &dwp_file->sections.line;
11415 else if (names->loclists_dwo.matches (sectp->name))
11416 dw_sect = &dwp_file->sections.loclists;
11417 else if (names->macro_dwo.matches (sectp->name))
11418 dw_sect = &dwp_file->sections.macro;
11419 else if (names->rnglists_dwo.matches (sectp->name))
11420 dw_sect = &dwp_file->sections.rnglists;
11421 else if (names->str_offsets_dwo.matches (sectp->name))
11422 dw_sect = &dwp_file->sections.str_offsets;
11423
11424 if (dw_sect != nullptr)
11425 {
11426 dw_sect->s.section = sectp;
11427 dw_sect->size = bfd_section_size (sectp);
11428 dw_sect->read (objfile);
11429 }
11430 }
11431
11432 /* Hash function for dwp_file loaded CUs/TUs. */
11433
11434 static hashval_t
11435 hash_dwp_loaded_cutus (const void *item)
11436 {
11437 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11438
11439 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11440 return dwo_unit->signature;
11441 }
11442
11443 /* Equality function for dwp_file loaded CUs/TUs. */
11444
11445 static int
11446 eq_dwp_loaded_cutus (const void *a, const void *b)
11447 {
11448 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11449 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11450
11451 return dua->signature == dub->signature;
11452 }
11453
11454 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11455
11456 static htab_up
11457 allocate_dwp_loaded_cutus_table ()
11458 {
11459 return htab_up (htab_create_alloc (3,
11460 hash_dwp_loaded_cutus,
11461 eq_dwp_loaded_cutus,
11462 NULL, xcalloc, xfree));
11463 }
11464
11465 /* Try to open DWP file FILE_NAME.
11466 The result is the bfd handle of the file.
11467 If there is a problem finding or opening the file, return NULL.
11468 Upon success, the canonicalized path of the file is stored in the bfd,
11469 same as symfile_bfd_open. */
11470
11471 static gdb_bfd_ref_ptr
11472 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
11473 {
11474 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
11475 1 /*is_dwp*/,
11476 1 /*search_cwd*/));
11477 if (abfd != NULL)
11478 return abfd;
11479
11480 /* Work around upstream bug 15652.
11481 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11482 [Whether that's a "bug" is debatable, but it is getting in our way.]
11483 We have no real idea where the dwp file is, because gdb's realpath-ing
11484 of the executable's path may have discarded the needed info.
11485 [IWBN if the dwp file name was recorded in the executable, akin to
11486 .gnu_debuglink, but that doesn't exist yet.]
11487 Strip the directory from FILE_NAME and search again. */
11488 if (!debug_file_directory.empty ())
11489 {
11490 /* Don't implicitly search the current directory here.
11491 If the user wants to search "." to handle this case,
11492 it must be added to debug-file-directory. */
11493 return try_open_dwop_file (per_objfile, lbasename (file_name),
11494 1 /*is_dwp*/,
11495 0 /*search_cwd*/);
11496 }
11497
11498 return NULL;
11499 }
11500
11501 /* Initialize the use of the DWP file for the current objfile.
11502 By convention the name of the DWP file is ${objfile}.dwp.
11503 The result is NULL if it can't be found. */
11504
11505 static std::unique_ptr<struct dwp_file>
11506 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
11507 {
11508 struct objfile *objfile = per_objfile->objfile;
11509
11510 /* Try to find first .dwp for the binary file before any symbolic links
11511 resolving. */
11512
11513 /* If the objfile is a debug file, find the name of the real binary
11514 file and get the name of dwp file from there. */
11515 std::string dwp_name;
11516 if (objfile->separate_debug_objfile_backlink != NULL)
11517 {
11518 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11519 const char *backlink_basename = lbasename (backlink->original_name);
11520
11521 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11522 }
11523 else
11524 dwp_name = objfile->original_name;
11525
11526 dwp_name += ".dwp";
11527
11528 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
11529 if (dbfd == NULL
11530 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11531 {
11532 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11533 dwp_name = objfile_name (objfile);
11534 dwp_name += ".dwp";
11535 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
11536 }
11537
11538 if (dbfd == NULL)
11539 {
11540 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
11541
11542 return std::unique_ptr<dwp_file> ();
11543 }
11544
11545 const char *name = bfd_get_filename (dbfd.get ());
11546 std::unique_ptr<struct dwp_file> dwp_file
11547 (new struct dwp_file (name, std::move (dbfd)));
11548
11549 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
11550 dwp_file->elf_sections =
11551 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
11552 dwp_file->num_sections, asection *);
11553
11554 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11555 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11556 dwp_file.get ());
11557
11558 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
11559
11560 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
11561
11562 /* The DWP file version is stored in the hash table. Oh well. */
11563 if (dwp_file->cus && dwp_file->tus
11564 && dwp_file->cus->version != dwp_file->tus->version)
11565 {
11566 /* Technically speaking, we should try to limp along, but this is
11567 pretty bizarre. We use pulongest here because that's the established
11568 portability solution (e.g, we cannot use %u for uint32_t). */
11569 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11570 " TU version %s [in DWP file %s]"),
11571 pulongest (dwp_file->cus->version),
11572 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11573 }
11574
11575 if (dwp_file->cus)
11576 dwp_file->version = dwp_file->cus->version;
11577 else if (dwp_file->tus)
11578 dwp_file->version = dwp_file->tus->version;
11579 else
11580 dwp_file->version = 2;
11581
11582 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11583 {
11584 if (dwp_file->version == 2)
11585 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11586 dwp_file.get ());
11587 else
11588 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11589 dwp_file.get ());
11590 }
11591
11592 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
11593 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
11594
11595 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
11596 dwarf_read_debug_printf (" %s CUs, %s TUs",
11597 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11598 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11599
11600 return dwp_file;
11601 }
11602
11603 /* Wrapper around open_and_init_dwp_file, only open it once. */
11604
11605 static struct dwp_file *
11606 get_dwp_file (dwarf2_per_objfile *per_objfile)
11607 {
11608 if (!per_objfile->per_bfd->dwp_checked)
11609 {
11610 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
11611 per_objfile->per_bfd->dwp_checked = 1;
11612 }
11613 return per_objfile->per_bfd->dwp_file.get ();
11614 }
11615
11616 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11617 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11618 or in the DWP file for the objfile, referenced by THIS_UNIT.
11619 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11620 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11621
11622 This is called, for example, when wanting to read a variable with a
11623 complex location. Therefore we don't want to do file i/o for every call.
11624 Therefore we don't want to look for a DWO file on every call.
11625 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11626 then we check if we've already seen DWO_NAME, and only THEN do we check
11627 for a DWO file.
11628
11629 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11630 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11631
11632 static struct dwo_unit *
11633 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11634 ULONGEST signature, int is_debug_types)
11635 {
11636 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11637 struct objfile *objfile = per_objfile->objfile;
11638 const char *kind = is_debug_types ? "TU" : "CU";
11639 void **dwo_file_slot;
11640 struct dwo_file *dwo_file;
11641 struct dwp_file *dwp_file;
11642
11643 /* First see if there's a DWP file.
11644 If we have a DWP file but didn't find the DWO inside it, don't
11645 look for the original DWO file. It makes gdb behave differently
11646 depending on whether one is debugging in the build tree. */
11647
11648 dwp_file = get_dwp_file (per_objfile);
11649 if (dwp_file != NULL)
11650 {
11651 const struct dwp_hash_table *dwp_htab =
11652 is_debug_types ? dwp_file->tus : dwp_file->cus;
11653
11654 if (dwp_htab != NULL)
11655 {
11656 struct dwo_unit *dwo_cutu =
11657 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
11658 is_debug_types);
11659
11660 if (dwo_cutu != NULL)
11661 {
11662 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11663 kind, hex_string (signature),
11664 host_address_to_string (dwo_cutu));
11665
11666 return dwo_cutu;
11667 }
11668 }
11669 }
11670 else
11671 {
11672 /* No DWP file, look for the DWO file. */
11673
11674 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
11675 if (*dwo_file_slot == NULL)
11676 {
11677 /* Read in the file and build a table of the CUs/TUs it contains. */
11678 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
11679 }
11680 /* NOTE: This will be NULL if unable to open the file. */
11681 dwo_file = (struct dwo_file *) *dwo_file_slot;
11682
11683 if (dwo_file != NULL)
11684 {
11685 struct dwo_unit *dwo_cutu = NULL;
11686
11687 if (is_debug_types && dwo_file->tus)
11688 {
11689 struct dwo_unit find_dwo_cutu;
11690
11691 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11692 find_dwo_cutu.signature = signature;
11693 dwo_cutu
11694 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
11695 &find_dwo_cutu);
11696 }
11697 else if (!is_debug_types && dwo_file->cus)
11698 {
11699 struct dwo_unit find_dwo_cutu;
11700
11701 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11702 find_dwo_cutu.signature = signature;
11703 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
11704 &find_dwo_cutu);
11705 }
11706
11707 if (dwo_cutu != NULL)
11708 {
11709 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11710 kind, dwo_name, hex_string (signature),
11711 host_address_to_string (dwo_cutu));
11712
11713 return dwo_cutu;
11714 }
11715 }
11716 }
11717
11718 /* We didn't find it. This could mean a dwo_id mismatch, or
11719 someone deleted the DWO/DWP file, or the search path isn't set up
11720 correctly to find the file. */
11721
11722 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11723 kind, dwo_name, hex_string (signature));
11724
11725 /* This is a warning and not a complaint because it can be caused by
11726 pilot error (e.g., user accidentally deleting the DWO). */
11727 {
11728 /* Print the name of the DWP file if we looked there, helps the user
11729 better diagnose the problem. */
11730 std::string dwp_text;
11731
11732 if (dwp_file != NULL)
11733 dwp_text = string_printf (" [in DWP file %s]",
11734 lbasename (dwp_file->name));
11735
11736 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11737 " [in module %s]"),
11738 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
11739 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
11740 }
11741 return NULL;
11742 }
11743
11744 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11745 See lookup_dwo_cutu_unit for details. */
11746
11747 static struct dwo_unit *
11748 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11749 ULONGEST signature)
11750 {
11751 gdb_assert (!cu->per_cu->is_debug_types);
11752
11753 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
11754 }
11755
11756 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11757 See lookup_dwo_cutu_unit for details. */
11758
11759 static struct dwo_unit *
11760 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
11761 {
11762 gdb_assert (cu->per_cu->is_debug_types);
11763
11764 signatured_type *sig_type = (signatured_type *) cu->per_cu;
11765
11766 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
11767 }
11768
11769 /* Traversal function for queue_and_load_all_dwo_tus. */
11770
11771 static int
11772 queue_and_load_dwo_tu (void **slot, void *info)
11773 {
11774 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11775 dwarf2_cu *cu = (dwarf2_cu *) info;
11776 ULONGEST signature = dwo_unit->signature;
11777 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
11778
11779 if (sig_type != NULL)
11780 {
11781 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11782 a real dependency of PER_CU on SIG_TYPE. That is detected later
11783 while processing PER_CU. */
11784 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
11785 cu->lang ()))
11786 load_full_type_unit (sig_type, cu->per_objfile);
11787 cu->per_cu->imported_symtabs_push (sig_type);
11788 }
11789
11790 return 1;
11791 }
11792
11793 /* Queue all TUs contained in the DWO of CU to be read in.
11794 The DWO may have the only definition of the type, though it may not be
11795 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11796 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11797
11798 static void
11799 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
11800 {
11801 struct dwo_unit *dwo_unit;
11802 struct dwo_file *dwo_file;
11803
11804 gdb_assert (cu != nullptr);
11805 gdb_assert (!cu->per_cu->is_debug_types);
11806 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
11807
11808 dwo_unit = cu->dwo_unit;
11809 gdb_assert (dwo_unit != NULL);
11810
11811 dwo_file = dwo_unit->dwo_file;
11812 if (dwo_file->tus != NULL)
11813 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
11814 }
11815
11816 /* Read in various DIEs. */
11817
11818 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11819 Inherit only the children of the DW_AT_abstract_origin DIE not being
11820 already referenced by DW_AT_abstract_origin from the children of the
11821 current DIE. */
11822
11823 static void
11824 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11825 {
11826 attribute *attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11827 if (attr == nullptr)
11828 return;
11829
11830 /* Note that following die references may follow to a die in a
11831 different CU. */
11832 dwarf2_cu *origin_cu = cu;
11833
11834 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11835 die_info *origin_die = follow_die_ref (die, attr, &origin_cu);
11836
11837 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11838 symbols in. */
11839 struct pending **origin_previous_list_in_scope = origin_cu->list_in_scope;
11840 origin_cu->list_in_scope = cu->list_in_scope;
11841
11842 if (die->tag != origin_die->tag
11843 && !(die->tag == DW_TAG_inlined_subroutine
11844 && origin_die->tag == DW_TAG_subprogram))
11845 complaint (_("DIE %s and its abstract origin %s have different tags"),
11846 sect_offset_str (die->sect_off),
11847 sect_offset_str (origin_die->sect_off));
11848
11849 /* Find if the concrete and abstract trees are structurally the
11850 same. This is a shallow traversal and it is not bullet-proof;
11851 the compiler can trick the debugger into believing that the trees
11852 are isomorphic, whereas they actually are not. However, the
11853 likelyhood of this happening is pretty low, and a full-fledged
11854 check would be an overkill. */
11855 bool are_isomorphic = true;
11856 die_info *concrete_child = die->child;
11857 die_info *abstract_child = origin_die->child;
11858 while (concrete_child != nullptr || abstract_child != nullptr)
11859 {
11860 if (concrete_child == nullptr
11861 || abstract_child == nullptr
11862 || concrete_child->tag != abstract_child->tag)
11863 {
11864 are_isomorphic = false;
11865 break;
11866 }
11867
11868 concrete_child = concrete_child->sibling;
11869 abstract_child = abstract_child->sibling;
11870 }
11871
11872 /* Walk the origin's children in parallel to the concrete children.
11873 This helps match an origin child in case the debug info misses
11874 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11875 origin tree may not have the same tree structure as the concrete
11876 DIE, though. */
11877 die_info *corresponding_abstract_child
11878 = are_isomorphic ? origin_die->child : nullptr;
11879
11880 std::vector<sect_offset> offsets;
11881
11882 for (die_info *child_die = die->child;
11883 child_die && child_die->tag;
11884 child_die = child_die->sibling)
11885 {
11886 /* We are trying to process concrete instance entries:
11887 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11888 it's not relevant to our analysis here. i.e. detecting DIEs that are
11889 present in the abstract instance but not referenced in the concrete
11890 one. */
11891 if (child_die->tag == DW_TAG_call_site
11892 || child_die->tag == DW_TAG_GNU_call_site)
11893 {
11894 if (are_isomorphic)
11895 corresponding_abstract_child
11896 = corresponding_abstract_child->sibling;
11897 continue;
11898 }
11899
11900 /* For each CHILD_DIE, find the corresponding child of
11901 ORIGIN_DIE. If there is more than one layer of
11902 DW_AT_abstract_origin, follow them all; there shouldn't be,
11903 but GCC versions at least through 4.4 generate this (GCC PR
11904 40573). */
11905 die_info *child_origin_die = child_die;
11906 dwarf2_cu *child_origin_cu = cu;
11907 while (true)
11908 {
11909 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11910 child_origin_cu);
11911 if (attr == nullptr)
11912 break;
11913
11914 child_origin_die = follow_die_ref (child_origin_die, attr,
11915 &child_origin_cu);
11916 }
11917
11918 /* If missing DW_AT_abstract_origin, try the corresponding child
11919 of the origin. Clang emits such lexical scopes. */
11920 if (child_origin_die == child_die
11921 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
11922 && are_isomorphic
11923 && child_die->tag == DW_TAG_lexical_block)
11924 child_origin_die = corresponding_abstract_child;
11925
11926 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11927 counterpart may exist. */
11928 if (child_origin_die != child_die)
11929 {
11930 if (child_die->tag != child_origin_die->tag
11931 && !(child_die->tag == DW_TAG_inlined_subroutine
11932 && child_origin_die->tag == DW_TAG_subprogram))
11933 complaint (_("Child DIE %s and its abstract origin %s have "
11934 "different tags"),
11935 sect_offset_str (child_die->sect_off),
11936 sect_offset_str (child_origin_die->sect_off));
11937 if (child_origin_die->parent != origin_die)
11938 complaint (_("Child DIE %s and its abstract origin %s have "
11939 "different parents"),
11940 sect_offset_str (child_die->sect_off),
11941 sect_offset_str (child_origin_die->sect_off));
11942 else
11943 offsets.push_back (child_origin_die->sect_off);
11944 }
11945
11946 if (are_isomorphic)
11947 corresponding_abstract_child = corresponding_abstract_child->sibling;
11948 }
11949
11950 if (!offsets.empty ())
11951 {
11952 std::sort (offsets.begin (), offsets.end ());
11953
11954 for (auto offsets_it = offsets.begin () + 1;
11955 offsets_it < offsets.end ();
11956 ++offsets_it)
11957 if (*(offsets_it - 1) == *offsets_it)
11958 complaint (_("Multiple children of DIE %s refer "
11959 "to DIE %s as their abstract origin"),
11960 sect_offset_str (die->sect_off),
11961 sect_offset_str (*offsets_it));
11962 }
11963
11964 auto offsets_it = offsets.begin ();
11965 die_info *origin_child_die = origin_die->child;
11966 while (origin_child_die != nullptr && origin_child_die->tag != 0)
11967 {
11968 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11969 while (offsets_it < offsets.end ()
11970 && *offsets_it < origin_child_die->sect_off)
11971 ++offsets_it;
11972
11973 if (offsets_it == offsets.end ()
11974 || *offsets_it > origin_child_die->sect_off)
11975 {
11976 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11977 Check whether we're already processing ORIGIN_CHILD_DIE.
11978 This can happen with mutually referenced abstract_origins.
11979 PR 16581. */
11980 if (!origin_child_die->in_process)
11981 process_die (origin_child_die, origin_cu);
11982 }
11983
11984 origin_child_die = origin_child_die->sibling;
11985 }
11986
11987 origin_cu->list_in_scope = origin_previous_list_in_scope;
11988
11989 if (cu != origin_cu)
11990 compute_delayed_physnames (origin_cu);
11991 }
11992
11993 static void
11994 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11995 {
11996 struct objfile *objfile = cu->per_objfile->objfile;
11997 struct gdbarch *gdbarch = objfile->arch ();
11998 struct context_stack *newobj;
11999 CORE_ADDR lowpc;
12000 CORE_ADDR highpc;
12001 struct die_info *child_die;
12002 struct attribute *attr, *call_line, *call_file;
12003 const char *name;
12004 CORE_ADDR baseaddr;
12005 struct block *block;
12006 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12007 std::vector<struct symbol *> template_args;
12008 struct template_symbol *templ_func = NULL;
12009
12010 if (inlined_func)
12011 {
12012 /* If we do not have call site information, we can't show the
12013 caller of this inlined function. That's too confusing, so
12014 only use the scope for local variables. */
12015 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12016 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12017 if (call_line == NULL || call_file == NULL)
12018 {
12019 read_lexical_block_scope (die, cu);
12020 return;
12021 }
12022 }
12023
12024 baseaddr = objfile->text_section_offset ();
12025
12026 name = dwarf2_name (die, cu);
12027 if (name == nullptr)
12028 name = dw2_linkage_name (die, cu);
12029
12030 /* Ignore functions with missing or empty names. These are actually
12031 illegal according to the DWARF standard. */
12032 if (name == NULL)
12033 {
12034 complaint (_("missing name for subprogram DIE at %s"),
12035 sect_offset_str (die->sect_off));
12036 return;
12037 }
12038
12039 /* Ignore functions with missing or invalid low and high pc attributes. */
12040 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
12041 <= PC_BOUNDS_INVALID)
12042 {
12043 if (have_complaint ())
12044 {
12045 attr = dwarf2_attr (die, DW_AT_external, cu);
12046 bool external_p = attr != nullptr && attr->as_boolean ();
12047 attr = dwarf2_attr (die, DW_AT_inline, cu);
12048 bool inlined_p
12049 = (attr != nullptr
12050 && attr->is_nonnegative ()
12051 && (attr->as_nonnegative () == DW_INL_inlined
12052 || attr->as_nonnegative () == DW_INL_declared_inlined));
12053 attr = dwarf2_attr (die, DW_AT_declaration, cu);
12054 bool decl_p = attr != nullptr && attr->as_boolean ();
12055 if (!external_p && !inlined_p && !decl_p)
12056 complaint (_("cannot get low and high bounds "
12057 "for subprogram DIE at %s"),
12058 sect_offset_str (die->sect_off));
12059 }
12060 return;
12061 }
12062
12063 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12064 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12065
12066 /* If we have any template arguments, then we must allocate a
12067 different sort of symbol. */
12068 for (child_die = die->child; child_die; child_die = child_die->sibling)
12069 {
12070 if (child_die->tag == DW_TAG_template_type_param
12071 || child_die->tag == DW_TAG_template_value_param)
12072 {
12073 templ_func = new (&objfile->objfile_obstack) template_symbol;
12074 templ_func->subclass = SYMBOL_TEMPLATE;
12075 break;
12076 }
12077 }
12078
12079 gdb_assert (cu->get_builder () != nullptr);
12080 newobj = cu->get_builder ()->push_context (0, lowpc);
12081 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12082 (struct symbol *) templ_func);
12083
12084 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12085 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12086 cu->lang ());
12087
12088 /* If there is a location expression for DW_AT_frame_base, record
12089 it. */
12090 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12091 if (attr != nullptr)
12092 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12093
12094 /* If there is a location for the static link, record it. */
12095 newobj->static_link = NULL;
12096 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12097 if (attr != nullptr)
12098 {
12099 newobj->static_link
12100 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12101 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12102 cu->addr_type ());
12103 }
12104
12105 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12106
12107 if (die->child != NULL)
12108 {
12109 child_die = die->child;
12110 while (child_die && child_die->tag)
12111 {
12112 if (child_die->tag == DW_TAG_template_type_param
12113 || child_die->tag == DW_TAG_template_value_param)
12114 {
12115 struct symbol *arg = new_symbol (child_die, NULL, cu);
12116
12117 if (arg != NULL)
12118 template_args.push_back (arg);
12119 }
12120 else
12121 process_die (child_die, cu);
12122 child_die = child_die->sibling;
12123 }
12124 }
12125
12126 inherit_abstract_dies (die, cu);
12127
12128 /* If we have a DW_AT_specification, we might need to import using
12129 directives from the context of the specification DIE. See the
12130 comment in determine_prefix. */
12131 if (cu->lang () == language_cplus
12132 && dwarf2_attr (die, DW_AT_specification, cu))
12133 {
12134 struct dwarf2_cu *spec_cu = cu;
12135 struct die_info *spec_die = die_specification (die, &spec_cu);
12136
12137 while (spec_die)
12138 {
12139 child_die = spec_die->child;
12140 while (child_die && child_die->tag)
12141 {
12142 if (child_die->tag == DW_TAG_imported_module)
12143 process_die (child_die, spec_cu);
12144 child_die = child_die->sibling;
12145 }
12146
12147 /* In some cases, GCC generates specification DIEs that
12148 themselves contain DW_AT_specification attributes. */
12149 spec_die = die_specification (spec_die, &spec_cu);
12150 }
12151 }
12152
12153 struct context_stack cstk = cu->get_builder ()->pop_context ();
12154 /* Make a block for the local symbols within. */
12155 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12156 cstk.static_link, lowpc, highpc);
12157
12158 /* For C++, set the block's scope. */
12159 if ((cu->lang () == language_cplus
12160 || cu->lang () == language_fortran
12161 || cu->lang () == language_d
12162 || cu->lang () == language_rust)
12163 && cu->processing_has_namespace_info)
12164 block_set_scope (block, determine_prefix (die, cu),
12165 &objfile->objfile_obstack);
12166
12167 /* If we have address ranges, record them. */
12168 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12169
12170 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12171
12172 /* Attach template arguments to function. */
12173 if (!template_args.empty ())
12174 {
12175 gdb_assert (templ_func != NULL);
12176
12177 templ_func->n_template_arguments = template_args.size ();
12178 templ_func->template_arguments
12179 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12180 templ_func->n_template_arguments);
12181 memcpy (templ_func->template_arguments,
12182 template_args.data (),
12183 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12184
12185 /* Make sure that the symtab is set on the new symbols. Even
12186 though they don't appear in this symtab directly, other parts
12187 of gdb assume that symbols do, and this is reasonably
12188 true. */
12189 for (symbol *sym : template_args)
12190 sym->set_symtab (templ_func->symtab ());
12191 }
12192
12193 /* In C++, we can have functions nested inside functions (e.g., when
12194 a function declares a class that has methods). This means that
12195 when we finish processing a function scope, we may need to go
12196 back to building a containing block's symbol lists. */
12197 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12198 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12199
12200 /* If we've finished processing a top-level function, subsequent
12201 symbols go in the file symbol list. */
12202 if (cu->get_builder ()->outermost_context_p ())
12203 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12204 }
12205
12206 /* Process all the DIES contained within a lexical block scope. Start
12207 a new scope, process the dies, and then close the scope. */
12208
12209 static void
12210 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12211 {
12212 struct objfile *objfile = cu->per_objfile->objfile;
12213 struct gdbarch *gdbarch = objfile->arch ();
12214 CORE_ADDR lowpc, highpc;
12215 struct die_info *child_die;
12216 CORE_ADDR baseaddr;
12217
12218 baseaddr = objfile->text_section_offset ();
12219
12220 /* Ignore blocks with missing or invalid low and high pc attributes. */
12221 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12222 as multiple lexical blocks? Handling children in a sane way would
12223 be nasty. Might be easier to properly extend generic blocks to
12224 describe ranges. */
12225 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
12226 {
12227 case PC_BOUNDS_NOT_PRESENT:
12228 /* DW_TAG_lexical_block has no attributes, process its children as if
12229 there was no wrapping by that DW_TAG_lexical_block.
12230 GCC does no longer produces such DWARF since GCC r224161. */
12231 for (child_die = die->child;
12232 child_die != NULL && child_die->tag;
12233 child_die = child_die->sibling)
12234 {
12235 /* We might already be processing this DIE. This can happen
12236 in an unusual circumstance -- where a subroutine A
12237 appears lexically in another subroutine B, but A actually
12238 inlines B. The recursion is broken here, rather than in
12239 inherit_abstract_dies, because it seems better to simply
12240 drop concrete children here. */
12241 if (!child_die->in_process)
12242 process_die (child_die, cu);
12243 }
12244 return;
12245 case PC_BOUNDS_INVALID:
12246 return;
12247 }
12248 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12249 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12250
12251 cu->get_builder ()->push_context (0, lowpc);
12252 if (die->child != NULL)
12253 {
12254 child_die = die->child;
12255 while (child_die && child_die->tag)
12256 {
12257 process_die (child_die, cu);
12258 child_die = child_die->sibling;
12259 }
12260 }
12261 inherit_abstract_dies (die, cu);
12262 struct context_stack cstk = cu->get_builder ()->pop_context ();
12263
12264 if (*cu->get_builder ()->get_local_symbols () != NULL
12265 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12266 {
12267 struct block *block
12268 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12269 cstk.start_addr, highpc);
12270
12271 /* Note that recording ranges after traversing children, as we
12272 do here, means that recording a parent's ranges entails
12273 walking across all its children's ranges as they appear in
12274 the address map, which is quadratic behavior.
12275
12276 It would be nicer to record the parent's ranges before
12277 traversing its children, simply overriding whatever you find
12278 there. But since we don't even decide whether to create a
12279 block until after we've traversed its children, that's hard
12280 to do. */
12281 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12282 }
12283 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12284 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12285 }
12286
12287 static void dwarf2_ranges_read_low_addrs (unsigned offset,
12288 struct dwarf2_cu *cu,
12289 dwarf_tag tag,
12290 std::vector<CORE_ADDR> &result);
12291
12292 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12293
12294 static void
12295 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12296 {
12297 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12298 struct objfile *objfile = per_objfile->objfile;
12299 struct gdbarch *gdbarch = objfile->arch ();
12300 CORE_ADDR pc, baseaddr;
12301 struct attribute *attr;
12302 void **slot;
12303 int nparams;
12304 struct die_info *child_die;
12305
12306 baseaddr = objfile->text_section_offset ();
12307
12308 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12309 if (attr == NULL)
12310 {
12311 /* This was a pre-DWARF-5 GNU extension alias
12312 for DW_AT_call_return_pc. */
12313 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12314 }
12315 if (!attr)
12316 {
12317 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12318 "DIE %s [in module %s]"),
12319 sect_offset_str (die->sect_off), objfile_name (objfile));
12320 return;
12321 }
12322 pc = attr->as_address () + baseaddr;
12323 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12324 pc -= baseaddr;
12325
12326 if (cu->call_site_htab == NULL)
12327 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
12328 call_site::eq, NULL,
12329 &objfile->objfile_obstack,
12330 hashtab_obstack_allocate, NULL);
12331 struct call_site call_site_local (pc, nullptr, nullptr);
12332 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12333 if (*slot != NULL)
12334 {
12335 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12336 "DIE %s [in module %s]"),
12337 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
12338 objfile_name (objfile));
12339 return;
12340 }
12341
12342 /* Count parameters at the caller. */
12343
12344 nparams = 0;
12345 for (child_die = die->child; child_die && child_die->tag;
12346 child_die = child_die->sibling)
12347 {
12348 if (child_die->tag != DW_TAG_call_site_parameter
12349 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12350 {
12351 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12352 "DW_TAG_call_site child DIE %s [in module %s]"),
12353 child_die->tag, sect_offset_str (child_die->sect_off),
12354 objfile_name (objfile));
12355 continue;
12356 }
12357
12358 nparams++;
12359 }
12360
12361 struct call_site *call_site
12362 = new (XOBNEWVAR (&objfile->objfile_obstack,
12363 struct call_site,
12364 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
12365 struct call_site (pc, cu->per_cu, per_objfile);
12366 *slot = call_site;
12367
12368 /* We never call the destructor of call_site, so we must ensure it is
12369 trivially destructible. */
12370 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
12371
12372 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12373 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12374 {
12375 struct die_info *func_die;
12376
12377 /* Skip also over DW_TAG_inlined_subroutine. */
12378 for (func_die = die->parent;
12379 func_die && func_die->tag != DW_TAG_subprogram
12380 && func_die->tag != DW_TAG_subroutine_type;
12381 func_die = func_die->parent);
12382
12383 /* DW_AT_call_all_calls is a superset
12384 of DW_AT_call_all_tail_calls. */
12385 if (func_die
12386 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12387 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12388 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12389 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12390 {
12391 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12392 not complete. But keep CALL_SITE for look ups via call_site_htab,
12393 both the initial caller containing the real return address PC and
12394 the final callee containing the current PC of a chain of tail
12395 calls do not need to have the tail call list complete. But any
12396 function candidate for a virtual tail call frame searched via
12397 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12398 determined unambiguously. */
12399 }
12400 else
12401 {
12402 struct type *func_type = NULL;
12403
12404 if (func_die)
12405 func_type = get_die_type (func_die, cu);
12406 if (func_type != NULL)
12407 {
12408 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
12409
12410 /* Enlist this call site to the function. */
12411 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12412 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12413 }
12414 else
12415 complaint (_("Cannot find function owning DW_TAG_call_site "
12416 "DIE %s [in module %s]"),
12417 sect_offset_str (die->sect_off), objfile_name (objfile));
12418 }
12419 }
12420
12421 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12422 if (attr == NULL)
12423 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12424 if (attr == NULL)
12425 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12426 if (attr == NULL)
12427 {
12428 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12429 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12430 }
12431
12432 call_site->target.set_loc_dwarf_block (nullptr);
12433 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
12434 /* Keep NULL DWARF_BLOCK. */;
12435 else if (attr->form_is_block ())
12436 {
12437 struct dwarf2_locexpr_baton *dlbaton;
12438 struct dwarf_block *block = attr->as_block ();
12439
12440 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12441 dlbaton->data = block->data;
12442 dlbaton->size = block->size;
12443 dlbaton->per_objfile = per_objfile;
12444 dlbaton->per_cu = cu->per_cu;
12445
12446 call_site->target.set_loc_dwarf_block (dlbaton);
12447 }
12448 else if (attr->form_is_ref ())
12449 {
12450 struct dwarf2_cu *target_cu = cu;
12451 struct die_info *target_die;
12452
12453 target_die = follow_die_ref (die, attr, &target_cu);
12454 gdb_assert (target_cu->per_objfile->objfile == objfile);
12455
12456 struct attribute *ranges_attr
12457 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
12458
12459 if (die_is_declaration (target_die, target_cu))
12460 {
12461 const char *target_physname;
12462
12463 /* Prefer the mangled name; otherwise compute the demangled one. */
12464 target_physname = dw2_linkage_name (target_die, target_cu);
12465 if (target_physname == NULL)
12466 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12467 if (target_physname == NULL)
12468 complaint (_("DW_AT_call_target target DIE has invalid "
12469 "physname, for referencing DIE %s [in module %s]"),
12470 sect_offset_str (die->sect_off), objfile_name (objfile));
12471 else
12472 call_site->target.set_loc_physname (target_physname);
12473 }
12474 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
12475 {
12476 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
12477 + target_cu->gnu_ranges_base);
12478 std::vector<CORE_ADDR> addresses;
12479 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
12480 target_die->tag, addresses);
12481 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
12482 addresses.size ());
12483 std::copy (addresses.begin (), addresses.end (), saved);
12484 call_site->target.set_loc_array (addresses.size (), saved);
12485 }
12486 else
12487 {
12488 CORE_ADDR lowpc;
12489
12490 /* DW_AT_entry_pc should be preferred. */
12491 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
12492 nullptr, nullptr)
12493 <= PC_BOUNDS_INVALID)
12494 complaint (_("DW_AT_call_target target DIE has invalid "
12495 "low pc, for referencing DIE %s [in module %s]"),
12496 sect_offset_str (die->sect_off), objfile_name (objfile));
12497 else
12498 {
12499 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
12500 - baseaddr);
12501 call_site->target.set_loc_physaddr (lowpc);
12502 }
12503 }
12504 }
12505 else
12506 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12507 "block nor reference, for DIE %s [in module %s]"),
12508 sect_offset_str (die->sect_off), objfile_name (objfile));
12509
12510 for (child_die = die->child;
12511 child_die && child_die->tag;
12512 child_die = child_die->sibling)
12513 {
12514 struct call_site_parameter *parameter;
12515 struct attribute *loc, *origin;
12516
12517 if (child_die->tag != DW_TAG_call_site_parameter
12518 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12519 {
12520 /* Already printed the complaint above. */
12521 continue;
12522 }
12523
12524 gdb_assert (call_site->parameter_count < nparams);
12525 parameter = &call_site->parameter[call_site->parameter_count];
12526
12527 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12528 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12529 register is contained in DW_AT_call_value. */
12530
12531 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12532 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12533 if (origin == NULL)
12534 {
12535 /* This was a pre-DWARF-5 GNU extension alias
12536 for DW_AT_call_parameter. */
12537 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12538 }
12539 if (loc == NULL && origin != NULL && origin->form_is_ref ())
12540 {
12541 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12542
12543 sect_offset sect_off = origin->get_ref_die_offset ();
12544 if (!cu->header.offset_in_cu_p (sect_off))
12545 {
12546 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12547 binding can be done only inside one CU. Such referenced DIE
12548 therefore cannot be even moved to DW_TAG_partial_unit. */
12549 complaint (_("DW_AT_call_parameter offset is not in CU for "
12550 "DW_TAG_call_site child DIE %s [in module %s]"),
12551 sect_offset_str (child_die->sect_off),
12552 objfile_name (objfile));
12553 continue;
12554 }
12555 parameter->u.param_cu_off
12556 = (cu_offset) (sect_off - cu->header.sect_off);
12557 }
12558 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
12559 {
12560 complaint (_("No DW_FORM_block* DW_AT_location for "
12561 "DW_TAG_call_site child DIE %s [in module %s]"),
12562 sect_offset_str (child_die->sect_off), objfile_name (objfile));
12563 continue;
12564 }
12565 else
12566 {
12567 struct dwarf_block *block = loc->as_block ();
12568
12569 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12570 (block->data, &block->data[block->size]);
12571 if (parameter->u.dwarf_reg != -1)
12572 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12573 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
12574 &block->data[block->size],
12575 &parameter->u.fb_offset))
12576 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12577 else
12578 {
12579 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12580 "for DW_FORM_block* DW_AT_location is supported for "
12581 "DW_TAG_call_site child DIE %s "
12582 "[in module %s]"),
12583 sect_offset_str (child_die->sect_off),
12584 objfile_name (objfile));
12585 continue;
12586 }
12587 }
12588
12589 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12590 if (attr == NULL)
12591 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12592 if (attr == NULL || !attr->form_is_block ())
12593 {
12594 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12595 "DW_TAG_call_site child DIE %s [in module %s]"),
12596 sect_offset_str (child_die->sect_off),
12597 objfile_name (objfile));
12598 continue;
12599 }
12600
12601 struct dwarf_block *block = attr->as_block ();
12602 parameter->value = block->data;
12603 parameter->value_size = block->size;
12604
12605 /* Parameters are not pre-cleared by memset above. */
12606 parameter->data_value = NULL;
12607 parameter->data_value_size = 0;
12608 call_site->parameter_count++;
12609
12610 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12611 if (attr == NULL)
12612 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12613 if (attr != nullptr)
12614 {
12615 if (!attr->form_is_block ())
12616 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12617 "DW_TAG_call_site child DIE %s [in module %s]"),
12618 sect_offset_str (child_die->sect_off),
12619 objfile_name (objfile));
12620 else
12621 {
12622 block = attr->as_block ();
12623 parameter->data_value = block->data;
12624 parameter->data_value_size = block->size;
12625 }
12626 }
12627 }
12628 }
12629
12630 /* Helper function for read_variable. If DIE represents a virtual
12631 table, then return the type of the concrete object that is
12632 associated with the virtual table. Otherwise, return NULL. */
12633
12634 static struct type *
12635 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12636 {
12637 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12638 if (attr == NULL)
12639 return NULL;
12640
12641 /* Find the type DIE. */
12642 struct die_info *type_die = NULL;
12643 struct dwarf2_cu *type_cu = cu;
12644
12645 if (attr->form_is_ref ())
12646 type_die = follow_die_ref (die, attr, &type_cu);
12647 if (type_die == NULL)
12648 return NULL;
12649
12650 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12651 return NULL;
12652 return die_containing_type (type_die, type_cu);
12653 }
12654
12655 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12656
12657 static void
12658 read_variable (struct die_info *die, struct dwarf2_cu *cu)
12659 {
12660 struct rust_vtable_symbol *storage = NULL;
12661
12662 if (cu->lang () == language_rust)
12663 {
12664 struct type *containing_type = rust_containing_type (die, cu);
12665
12666 if (containing_type != NULL)
12667 {
12668 struct objfile *objfile = cu->per_objfile->objfile;
12669
12670 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
12671 storage->concrete_type = containing_type;
12672 storage->subclass = SYMBOL_RUST_VTABLE;
12673 }
12674 }
12675
12676 struct symbol *res = new_symbol (die, NULL, cu, storage);
12677 struct attribute *abstract_origin
12678 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12679 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
12680 if (res == NULL && loc && abstract_origin)
12681 {
12682 /* We have a variable without a name, but with a location and an abstract
12683 origin. This may be a concrete instance of an abstract variable
12684 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12685 later. */
12686 struct dwarf2_cu *origin_cu = cu;
12687 struct die_info *origin_die
12688 = follow_die_ref (die, abstract_origin, &origin_cu);
12689 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12690 per_objfile->per_bfd->abstract_to_concrete
12691 [origin_die->sect_off].push_back (die->sect_off);
12692 }
12693 }
12694
12695 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12696 reading .debug_rnglists.
12697 Callback's type should be:
12698 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12699 Return true if the attributes are present and valid, otherwise,
12700 return false. */
12701
12702 template <typename Callback>
12703 static bool
12704 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12705 dwarf_tag tag, Callback &&callback)
12706 {
12707 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12708 struct objfile *objfile = per_objfile->objfile;
12709 bfd *obfd = objfile->obfd.get ();
12710 /* Base address selection entry. */
12711 gdb::optional<CORE_ADDR> base;
12712 const gdb_byte *buffer;
12713 bool overflow = false;
12714 ULONGEST addr_index;
12715 struct dwarf2_section_info *rnglists_section;
12716
12717 base = cu->base_address;
12718 rnglists_section = cu_debug_rnglists_section (cu, tag);
12719 rnglists_section->read (objfile);
12720
12721 if (offset >= rnglists_section->size)
12722 {
12723 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12724 offset);
12725 return false;
12726 }
12727 buffer = rnglists_section->buffer + offset;
12728
12729 while (1)
12730 {
12731 /* Initialize it due to a false compiler warning. */
12732 CORE_ADDR range_beginning = 0, range_end = 0;
12733 const gdb_byte *buf_end = (rnglists_section->buffer
12734 + rnglists_section->size);
12735 unsigned int bytes_read;
12736
12737 if (buffer == buf_end)
12738 {
12739 overflow = true;
12740 break;
12741 }
12742 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12743 switch (rlet)
12744 {
12745 case DW_RLE_end_of_list:
12746 break;
12747 case DW_RLE_base_address:
12748 if (buffer + cu->header.addr_size > buf_end)
12749 {
12750 overflow = true;
12751 break;
12752 }
12753 base = cu->header.read_address (obfd, buffer, &bytes_read);
12754 buffer += bytes_read;
12755 break;
12756 case DW_RLE_base_addressx:
12757 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12758 buffer += bytes_read;
12759 base = read_addr_index (cu, addr_index);
12760 break;
12761 case DW_RLE_start_length:
12762 if (buffer + cu->header.addr_size > buf_end)
12763 {
12764 overflow = true;
12765 break;
12766 }
12767 range_beginning = cu->header.read_address (obfd, buffer,
12768 &bytes_read);
12769 buffer += bytes_read;
12770 range_end = (range_beginning
12771 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12772 buffer += bytes_read;
12773 if (buffer > buf_end)
12774 {
12775 overflow = true;
12776 break;
12777 }
12778 break;
12779 case DW_RLE_startx_length:
12780 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12781 buffer += bytes_read;
12782 range_beginning = read_addr_index (cu, addr_index);
12783 if (buffer > buf_end)
12784 {
12785 overflow = true;
12786 break;
12787 }
12788 range_end = (range_beginning
12789 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12790 buffer += bytes_read;
12791 break;
12792 case DW_RLE_offset_pair:
12793 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12794 buffer += bytes_read;
12795 if (buffer > buf_end)
12796 {
12797 overflow = true;
12798 break;
12799 }
12800 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12801 buffer += bytes_read;
12802 if (buffer > buf_end)
12803 {
12804 overflow = true;
12805 break;
12806 }
12807 break;
12808 case DW_RLE_start_end:
12809 if (buffer + 2 * cu->header.addr_size > buf_end)
12810 {
12811 overflow = true;
12812 break;
12813 }
12814 range_beginning = cu->header.read_address (obfd, buffer,
12815 &bytes_read);
12816 buffer += bytes_read;
12817 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
12818 buffer += bytes_read;
12819 break;
12820 case DW_RLE_startx_endx:
12821 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12822 buffer += bytes_read;
12823 range_beginning = read_addr_index (cu, addr_index);
12824 if (buffer > buf_end)
12825 {
12826 overflow = true;
12827 break;
12828 }
12829 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12830 buffer += bytes_read;
12831 range_end = read_addr_index (cu, addr_index);
12832 break;
12833 default:
12834 complaint (_("Invalid .debug_rnglists data (no base address)"));
12835 return false;
12836 }
12837 if (rlet == DW_RLE_end_of_list || overflow)
12838 break;
12839 if (rlet == DW_RLE_base_address)
12840 continue;
12841
12842 if (range_beginning > range_end)
12843 {
12844 /* Inverted range entries are invalid. */
12845 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12846 return false;
12847 }
12848
12849 /* Empty range entries have no effect. */
12850 if (range_beginning == range_end)
12851 continue;
12852
12853 /* Only DW_RLE_offset_pair needs the base address added. */
12854 if (rlet == DW_RLE_offset_pair)
12855 {
12856 if (!base.has_value ())
12857 {
12858 /* We have no valid base address for the DW_RLE_offset_pair. */
12859 complaint (_("Invalid .debug_rnglists data (no base address for "
12860 "DW_RLE_offset_pair)"));
12861 return false;
12862 }
12863
12864 range_beginning += *base;
12865 range_end += *base;
12866 }
12867
12868 /* A not-uncommon case of bad debug info.
12869 Don't pollute the addrmap with bad data. */
12870 if (range_beginning == 0
12871 && !per_objfile->per_bfd->has_section_at_zero)
12872 {
12873 complaint (_(".debug_rnglists entry has start address of zero"
12874 " [in module %s]"), objfile_name (objfile));
12875 continue;
12876 }
12877
12878 callback (range_beginning, range_end);
12879 }
12880
12881 if (overflow)
12882 {
12883 complaint (_("Offset %d is not terminated "
12884 "for DW_AT_ranges attribute"),
12885 offset);
12886 return false;
12887 }
12888
12889 return true;
12890 }
12891
12892 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12893 Callback's type should be:
12894 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12895 Return 1 if the attributes are present and valid, otherwise, return 0. */
12896
12897 template <typename Callback>
12898 static int
12899 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
12900 Callback &&callback)
12901 {
12902 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12903 struct objfile *objfile = per_objfile->objfile;
12904 struct comp_unit_head *cu_header = &cu->header;
12905 bfd *obfd = objfile->obfd.get ();
12906 unsigned int addr_size = cu_header->addr_size;
12907 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12908 /* Base address selection entry. */
12909 gdb::optional<CORE_ADDR> base;
12910 unsigned int dummy;
12911 const gdb_byte *buffer;
12912
12913 if (cu_header->version >= 5)
12914 return dwarf2_rnglists_process (offset, cu, tag, callback);
12915
12916 base = cu->base_address;
12917
12918 per_objfile->per_bfd->ranges.read (objfile);
12919 if (offset >= per_objfile->per_bfd->ranges.size)
12920 {
12921 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12922 offset);
12923 return 0;
12924 }
12925 buffer = per_objfile->per_bfd->ranges.buffer + offset;
12926
12927 while (1)
12928 {
12929 CORE_ADDR range_beginning, range_end;
12930
12931 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
12932 buffer += addr_size;
12933 range_end = cu->header.read_address (obfd, buffer, &dummy);
12934 buffer += addr_size;
12935 offset += 2 * addr_size;
12936
12937 /* An end of list marker is a pair of zero addresses. */
12938 if (range_beginning == 0 && range_end == 0)
12939 /* Found the end of list entry. */
12940 break;
12941
12942 /* Each base address selection entry is a pair of 2 values.
12943 The first is the largest possible address, the second is
12944 the base address. Check for a base address here. */
12945 if ((range_beginning & mask) == mask)
12946 {
12947 /* If we found the largest possible address, then we already
12948 have the base address in range_end. */
12949 base = range_end;
12950 continue;
12951 }
12952
12953 if (!base.has_value ())
12954 {
12955 /* We have no valid base address for the ranges
12956 data. */
12957 complaint (_("Invalid .debug_ranges data (no base address)"));
12958 return 0;
12959 }
12960
12961 if (range_beginning > range_end)
12962 {
12963 /* Inverted range entries are invalid. */
12964 complaint (_("Invalid .debug_ranges data (inverted range)"));
12965 return 0;
12966 }
12967
12968 /* Empty range entries have no effect. */
12969 if (range_beginning == range_end)
12970 continue;
12971
12972 range_beginning += *base;
12973 range_end += *base;
12974
12975 /* A not-uncommon case of bad debug info.
12976 Don't pollute the addrmap with bad data. */
12977 if (range_beginning == 0
12978 && !per_objfile->per_bfd->has_section_at_zero)
12979 {
12980 complaint (_(".debug_ranges entry has start address of zero"
12981 " [in module %s]"), objfile_name (objfile));
12982 continue;
12983 }
12984
12985 callback (range_beginning, range_end);
12986 }
12987
12988 return 1;
12989 }
12990
12991 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12992 Return 1 if the attributes are present and valid, otherwise, return 0.
12993 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12994 ranges in MAP are set, using DATUM as the value. */
12995
12996 static int
12997 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12998 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12999 addrmap *map, void *datum, dwarf_tag tag)
13000 {
13001 struct objfile *objfile = cu->per_objfile->objfile;
13002 struct gdbarch *gdbarch = objfile->arch ();
13003 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13004 int low_set = 0;
13005 CORE_ADDR low = 0;
13006 CORE_ADDR high = 0;
13007 int retval;
13008
13009 retval = dwarf2_ranges_process (offset, cu, tag,
13010 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13011 {
13012 if (map != nullptr)
13013 {
13014 CORE_ADDR lowpc;
13015 CORE_ADDR highpc;
13016
13017 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13018 range_beginning + baseaddr)
13019 - baseaddr);
13020 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13021 range_end + baseaddr)
13022 - baseaddr);
13023 map->set_empty (lowpc, highpc - 1, datum);
13024 }
13025
13026 /* FIXME: This is recording everything as a low-high
13027 segment of consecutive addresses. We should have a
13028 data structure for discontiguous block ranges
13029 instead. */
13030 if (! low_set)
13031 {
13032 low = range_beginning;
13033 high = range_end;
13034 low_set = 1;
13035 }
13036 else
13037 {
13038 if (range_beginning < low)
13039 low = range_beginning;
13040 if (range_end > high)
13041 high = range_end;
13042 }
13043 });
13044 if (!retval)
13045 return 0;
13046
13047 if (! low_set)
13048 /* If the first entry is an end-of-list marker, the range
13049 describes an empty scope, i.e. no instructions. */
13050 return 0;
13051
13052 if (low_return)
13053 *low_return = low;
13054 if (high_return)
13055 *high_return = high;
13056 return 1;
13057 }
13058
13059 /* Process ranges and fill in a vector of the low PC values only. */
13060
13061 static void
13062 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
13063 dwarf_tag tag,
13064 std::vector<CORE_ADDR> &result)
13065 {
13066 dwarf2_ranges_process (offset, cu, tag,
13067 [&] (CORE_ADDR start, CORE_ADDR end)
13068 {
13069 result.push_back (start);
13070 });
13071 }
13072
13073 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13074 definition for the return value. *LOWPC and *HIGHPC are set iff
13075 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13076
13077 static enum pc_bounds_kind
13078 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13079 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13080 addrmap *map, void *datum)
13081 {
13082 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13083 struct attribute *attr;
13084 struct attribute *attr_high;
13085 CORE_ADDR low = 0;
13086 CORE_ADDR high = 0;
13087 enum pc_bounds_kind ret;
13088
13089 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13090 if (attr_high)
13091 {
13092 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13093 if (attr != nullptr)
13094 {
13095 low = attr->as_address ();
13096 high = attr_high->as_address ();
13097 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13098 high += low;
13099 }
13100 else
13101 /* Found high w/o low attribute. */
13102 return PC_BOUNDS_INVALID;
13103
13104 /* Found consecutive range of addresses. */
13105 ret = PC_BOUNDS_HIGH_LOW;
13106 }
13107 else
13108 {
13109 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13110 if (attr != nullptr && attr->form_is_unsigned ())
13111 {
13112 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13113 on DWARF version). */
13114 ULONGEST ranges_offset = attr->as_unsigned ();
13115
13116 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13117 this value. */
13118 if (die->tag != DW_TAG_compile_unit)
13119 ranges_offset += cu->gnu_ranges_base;
13120
13121 /* Value of the DW_AT_ranges attribute is the offset in the
13122 .debug_ranges section. */
13123 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
13124 map, datum, die->tag))
13125 return PC_BOUNDS_INVALID;
13126 /* Found discontinuous range of addresses. */
13127 ret = PC_BOUNDS_RANGES;
13128 }
13129 else
13130 return PC_BOUNDS_NOT_PRESENT;
13131 }
13132
13133 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13134 if (high <= low)
13135 return PC_BOUNDS_INVALID;
13136
13137 /* When using the GNU linker, .gnu.linkonce. sections are used to
13138 eliminate duplicate copies of functions and vtables and such.
13139 The linker will arbitrarily choose one and discard the others.
13140 The AT_*_pc values for such functions refer to local labels in
13141 these sections. If the section from that file was discarded, the
13142 labels are not in the output, so the relocs get a value of 0.
13143 If this is a discarded function, mark the pc bounds as invalid,
13144 so that GDB will ignore it. */
13145 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
13146 return PC_BOUNDS_INVALID;
13147
13148 *lowpc = low;
13149 if (highpc)
13150 *highpc = high;
13151 return ret;
13152 }
13153
13154 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13155 its low and high PC addresses. Do nothing if these addresses could not
13156 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13157 and HIGHPC to the high address if greater than HIGHPC. */
13158
13159 static void
13160 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13161 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13162 struct dwarf2_cu *cu)
13163 {
13164 CORE_ADDR low, high;
13165 struct die_info *child = die->child;
13166
13167 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
13168 >= PC_BOUNDS_RANGES)
13169 {
13170 *lowpc = std::min (*lowpc, low);
13171 *highpc = std::max (*highpc, high);
13172 }
13173
13174 /* If the language does not allow nested subprograms (either inside
13175 subprograms or lexical blocks), we're done. */
13176 if (cu->lang () != language_ada)
13177 return;
13178
13179 /* Check all the children of the given DIE. If it contains nested
13180 subprograms, then check their pc bounds. Likewise, we need to
13181 check lexical blocks as well, as they may also contain subprogram
13182 definitions. */
13183 while (child && child->tag)
13184 {
13185 if (child->tag == DW_TAG_subprogram
13186 || child->tag == DW_TAG_lexical_block)
13187 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13188 child = child->sibling;
13189 }
13190 }
13191
13192 /* Get the low and high pc's represented by the scope DIE, and store
13193 them in *LOWPC and *HIGHPC. If the correct values can't be
13194 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13195
13196 static void
13197 get_scope_pc_bounds (struct die_info *die,
13198 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13199 struct dwarf2_cu *cu)
13200 {
13201 CORE_ADDR best_low = (CORE_ADDR) -1;
13202 CORE_ADDR best_high = (CORE_ADDR) 0;
13203 CORE_ADDR current_low, current_high;
13204
13205 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
13206 nullptr, nullptr)
13207 >= PC_BOUNDS_RANGES)
13208 {
13209 best_low = current_low;
13210 best_high = current_high;
13211 }
13212 else
13213 {
13214 struct die_info *child = die->child;
13215
13216 while (child && child->tag)
13217 {
13218 switch (child->tag) {
13219 case DW_TAG_subprogram:
13220 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13221 break;
13222 case DW_TAG_namespace:
13223 case DW_TAG_module:
13224 /* FIXME: carlton/2004-01-16: Should we do this for
13225 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13226 that current GCC's always emit the DIEs corresponding
13227 to definitions of methods of classes as children of a
13228 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13229 the DIEs giving the declarations, which could be
13230 anywhere). But I don't see any reason why the
13231 standards says that they have to be there. */
13232 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13233
13234 if (current_low != ((CORE_ADDR) -1))
13235 {
13236 best_low = std::min (best_low, current_low);
13237 best_high = std::max (best_high, current_high);
13238 }
13239 break;
13240 default:
13241 /* Ignore. */
13242 break;
13243 }
13244
13245 child = child->sibling;
13246 }
13247 }
13248
13249 *lowpc = best_low;
13250 *highpc = best_high;
13251 }
13252
13253 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13254 in DIE. */
13255
13256 static void
13257 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13258 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13259 {
13260 struct objfile *objfile = cu->per_objfile->objfile;
13261 struct gdbarch *gdbarch = objfile->arch ();
13262 struct attribute *attr;
13263 struct attribute *attr_high;
13264
13265 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13266 if (attr_high)
13267 {
13268 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13269 if (attr != nullptr)
13270 {
13271 CORE_ADDR low = attr->as_address ();
13272 CORE_ADDR high = attr_high->as_address ();
13273
13274 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13275 high += low;
13276
13277 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13278 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13279 cu->get_builder ()->record_block_range (block, low, high - 1);
13280 }
13281 }
13282
13283 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13284 if (attr != nullptr && attr->form_is_unsigned ())
13285 {
13286 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13287 on DWARF version). */
13288 ULONGEST ranges_offset = attr->as_unsigned ();
13289
13290 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13291 this value. */
13292 if (die->tag != DW_TAG_compile_unit)
13293 ranges_offset += cu->gnu_ranges_base;
13294
13295 std::vector<blockrange> blockvec;
13296 dwarf2_ranges_process (ranges_offset, cu, die->tag,
13297 [&] (CORE_ADDR start, CORE_ADDR end)
13298 {
13299 start += baseaddr;
13300 end += baseaddr;
13301 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13302 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13303 cu->get_builder ()->record_block_range (block, start, end - 1);
13304 blockvec.emplace_back (start, end);
13305 });
13306
13307 block->set_ranges (make_blockranges (objfile, blockvec));
13308 }
13309 }
13310
13311 /* Check whether the producer field indicates either of GCC < 4.6, or the
13312 Intel C/C++ compiler, and cache the result in CU. */
13313
13314 static void
13315 check_producer (struct dwarf2_cu *cu)
13316 {
13317 int major, minor;
13318
13319 if (cu->producer == NULL)
13320 {
13321 /* For unknown compilers expect their behavior is DWARF version
13322 compliant.
13323
13324 GCC started to support .debug_types sections by -gdwarf-4 since
13325 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13326 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13327 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13328 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13329 }
13330 else if (producer_is_gcc (cu->producer, &major, &minor))
13331 {
13332 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13333 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13334 cu->producer_is_gcc_11 = major == 11;
13335 }
13336 else if (producer_is_icc (cu->producer, &major, &minor))
13337 {
13338 cu->producer_is_icc = true;
13339 cu->producer_is_icc_lt_14 = major < 14;
13340 }
13341 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13342 cu->producer_is_codewarrior = true;
13343 else if (producer_is_clang (cu->producer, &major, &minor))
13344 cu->producer_is_clang = true;
13345 else
13346 {
13347 /* For other non-GCC compilers, expect their behavior is DWARF version
13348 compliant. */
13349 }
13350
13351 cu->checked_producer = true;
13352 }
13353
13354 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13355 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13356 during 4.6.0 experimental. */
13357
13358 static bool
13359 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13360 {
13361 if (!cu->checked_producer)
13362 check_producer (cu);
13363
13364 return cu->producer_is_gxx_lt_4_6;
13365 }
13366
13367
13368 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13369 with incorrect is_stmt attributes. */
13370
13371 static bool
13372 producer_is_codewarrior (struct dwarf2_cu *cu)
13373 {
13374 if (!cu->checked_producer)
13375 check_producer (cu);
13376
13377 return cu->producer_is_codewarrior;
13378 }
13379
13380 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13381 If that attribute is not available, return the appropriate
13382 default. */
13383
13384 static enum dwarf_access_attribute
13385 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13386 {
13387 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13388 if (attr != nullptr)
13389 {
13390 LONGEST value = attr->constant_value (-1);
13391 if (value == DW_ACCESS_public
13392 || value == DW_ACCESS_protected
13393 || value == DW_ACCESS_private)
13394 return (dwarf_access_attribute) value;
13395 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13396 plongest (value));
13397 }
13398
13399 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13400 {
13401 /* The default DWARF 2 accessibility for members is public, the default
13402 accessibility for inheritance is private. */
13403
13404 if (die->tag != DW_TAG_inheritance)
13405 return DW_ACCESS_public;
13406 else
13407 return DW_ACCESS_private;
13408 }
13409 else
13410 {
13411 /* DWARF 3+ defines the default accessibility a different way. The same
13412 rules apply now for DW_TAG_inheritance as for the members and it only
13413 depends on the container kind. */
13414
13415 if (die->parent->tag == DW_TAG_class_type)
13416 return DW_ACCESS_private;
13417 else
13418 return DW_ACCESS_public;
13419 }
13420 }
13421
13422 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13423 *OFFSET to the byte offset. If the attribute was not found return
13424 0, otherwise return 1. If it was found but could not properly be
13425 handled, set *OFFSET to 0. */
13426
13427 static int
13428 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13429 LONGEST *offset)
13430 {
13431 struct attribute *attr;
13432
13433 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13434 if (attr != NULL)
13435 {
13436 *offset = 0;
13437
13438 /* Note that we do not check for a section offset first here.
13439 This is because DW_AT_data_member_location is new in DWARF 4,
13440 so if we see it, we can assume that a constant form is really
13441 a constant and not a section offset. */
13442 if (attr->form_is_constant ())
13443 *offset = attr->constant_value (0);
13444 else if (attr->form_is_section_offset ())
13445 dwarf2_complex_location_expr_complaint ();
13446 else if (attr->form_is_block ())
13447 *offset = decode_locdesc (attr->as_block (), cu);
13448 else
13449 dwarf2_complex_location_expr_complaint ();
13450
13451 return 1;
13452 }
13453 else
13454 {
13455 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13456 if (attr != nullptr)
13457 {
13458 *offset = attr->constant_value (0);
13459 return 1;
13460 }
13461 }
13462
13463 return 0;
13464 }
13465
13466 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13467 store the results in FIELD. */
13468
13469 static void
13470 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13471 struct field *field)
13472 {
13473 struct attribute *attr;
13474
13475 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13476 if (attr != NULL)
13477 {
13478 if (attr->form_is_constant ())
13479 {
13480 LONGEST offset = attr->constant_value (0);
13481
13482 /* Work around this GCC 11 bug, where it would erroneously use -1
13483 data member locations, instead of 0:
13484
13485 Negative DW_AT_data_member_location
13486 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13487 */
13488 if (offset == -1 && cu->producer_is_gcc_11)
13489 {
13490 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13491 offset = 0;
13492 }
13493
13494 field->set_loc_bitpos (offset * bits_per_byte);
13495 }
13496 else if (attr->form_is_section_offset ())
13497 dwarf2_complex_location_expr_complaint ();
13498 else if (attr->form_is_block ())
13499 {
13500 bool handled;
13501 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
13502 if (handled)
13503 field->set_loc_bitpos (offset * bits_per_byte);
13504 else
13505 {
13506 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13507 struct objfile *objfile = per_objfile->objfile;
13508 struct dwarf2_locexpr_baton *dlbaton
13509 = XOBNEW (&objfile->objfile_obstack,
13510 struct dwarf2_locexpr_baton);
13511 dlbaton->data = attr->as_block ()->data;
13512 dlbaton->size = attr->as_block ()->size;
13513 /* When using this baton, we want to compute the address
13514 of the field, not the value. This is why
13515 is_reference is set to false here. */
13516 dlbaton->is_reference = false;
13517 dlbaton->per_objfile = per_objfile;
13518 dlbaton->per_cu = cu->per_cu;
13519
13520 field->set_loc_dwarf_block (dlbaton);
13521 }
13522 }
13523 else
13524 dwarf2_complex_location_expr_complaint ();
13525 }
13526 else
13527 {
13528 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13529 if (attr != nullptr)
13530 field->set_loc_bitpos (attr->constant_value (0));
13531 }
13532 }
13533
13534 /* Add an aggregate field to the field list. */
13535
13536 static void
13537 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13538 struct dwarf2_cu *cu)
13539 {
13540 struct objfile *objfile = cu->per_objfile->objfile;
13541 struct gdbarch *gdbarch = objfile->arch ();
13542 struct nextfield *new_field;
13543 struct attribute *attr;
13544 struct field *fp;
13545 const char *fieldname = "";
13546
13547 if (die->tag == DW_TAG_inheritance)
13548 {
13549 fip->baseclasses.emplace_back ();
13550 new_field = &fip->baseclasses.back ();
13551 }
13552 else
13553 {
13554 fip->fields.emplace_back ();
13555 new_field = &fip->fields.back ();
13556 }
13557
13558 new_field->offset = die->sect_off;
13559
13560 new_field->accessibility = dwarf2_access_attribute (die, cu);
13561 if (new_field->accessibility != DW_ACCESS_public)
13562 fip->non_public_fields = true;
13563
13564 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13565 if (attr != nullptr)
13566 new_field->virtuality = attr->as_virtuality ();
13567 else
13568 new_field->virtuality = DW_VIRTUALITY_none;
13569
13570 fp = &new_field->field;
13571
13572 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
13573 && !die_is_declaration (die, cu))
13574 {
13575 if (die->tag == DW_TAG_namelist_item)
13576 {
13577 /* Typically, DW_TAG_namelist_item are references to namelist items.
13578 If so, follow that reference. */
13579 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
13580 struct die_info *item_die = nullptr;
13581 struct dwarf2_cu *item_cu = cu;
13582 if (attr1->form_is_ref ())
13583 item_die = follow_die_ref (die, attr1, &item_cu);
13584 if (item_die != nullptr)
13585 die = item_die;
13586 }
13587 /* Data member other than a C++ static data member. */
13588
13589 /* Get type of field. */
13590 fp->set_type (die_type (die, cu));
13591
13592 fp->set_loc_bitpos (0);
13593
13594 /* Get bit size of field (zero if none). */
13595 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13596 if (attr != nullptr)
13597 {
13598 FIELD_BITSIZE (*fp) = attr->constant_value (0);
13599 }
13600 else
13601 {
13602 FIELD_BITSIZE (*fp) = 0;
13603 }
13604
13605 /* Get bit offset of field. */
13606 handle_member_location (die, cu, fp);
13607 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13608 if (attr != nullptr && attr->form_is_constant ())
13609 {
13610 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
13611 {
13612 /* For big endian bits, the DW_AT_bit_offset gives the
13613 additional bit offset from the MSB of the containing
13614 anonymous object to the MSB of the field. We don't
13615 have to do anything special since we don't need to
13616 know the size of the anonymous object. */
13617 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
13618 }
13619 else
13620 {
13621 /* For little endian bits, compute the bit offset to the
13622 MSB of the anonymous object, subtract off the number of
13623 bits from the MSB of the field to the MSB of the
13624 object, and then subtract off the number of bits of
13625 the field itself. The result is the bit offset of
13626 the LSB of the field. */
13627 int anonymous_size;
13628 int bit_offset = attr->constant_value (0);
13629
13630 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13631 if (attr != nullptr && attr->form_is_constant ())
13632 {
13633 /* The size of the anonymous object containing
13634 the bit field is explicit, so use the
13635 indicated size (in bytes). */
13636 anonymous_size = attr->constant_value (0);
13637 }
13638 else
13639 {
13640 /* The size of the anonymous object containing
13641 the bit field must be inferred from the type
13642 attribute of the data member containing the
13643 bit field. */
13644 anonymous_size = fp->type ()->length ();
13645 }
13646 fp->set_loc_bitpos (fp->loc_bitpos ()
13647 + anonymous_size * bits_per_byte
13648 - bit_offset - FIELD_BITSIZE (*fp));
13649 }
13650 }
13651
13652 /* Get name of field. */
13653 fieldname = dwarf2_name (die, cu);
13654 if (fieldname == NULL)
13655 fieldname = "";
13656
13657 /* The name is already allocated along with this objfile, so we don't
13658 need to duplicate it for the type. */
13659 fp->set_name (fieldname);
13660
13661 /* Change accessibility for artificial fields (e.g. virtual table
13662 pointer or virtual base class pointer) to private. */
13663 if (dwarf2_attr (die, DW_AT_artificial, cu))
13664 {
13665 FIELD_ARTIFICIAL (*fp) = 1;
13666 new_field->accessibility = DW_ACCESS_private;
13667 fip->non_public_fields = true;
13668 }
13669 }
13670 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13671 {
13672 /* C++ static member. */
13673
13674 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13675 is a declaration, but all versions of G++ as of this writing
13676 (so through at least 3.2.1) incorrectly generate
13677 DW_TAG_variable tags. */
13678
13679 const char *physname;
13680
13681 /* Get name of field. */
13682 fieldname = dwarf2_name (die, cu);
13683 if (fieldname == NULL)
13684 return;
13685
13686 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13687 if (attr
13688 /* Only create a symbol if this is an external value.
13689 new_symbol checks this and puts the value in the global symbol
13690 table, which we want. If it is not external, new_symbol
13691 will try to put the value in cu->list_in_scope which is wrong. */
13692 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13693 {
13694 /* A static const member, not much different than an enum as far as
13695 we're concerned, except that we can support more types. */
13696 new_symbol (die, NULL, cu);
13697 }
13698
13699 /* Get physical name. */
13700 physname = dwarf2_physname (fieldname, die, cu);
13701
13702 /* The name is already allocated along with this objfile, so we don't
13703 need to duplicate it for the type. */
13704 fp->set_loc_physname (physname ? physname : "");
13705 fp->set_type (die_type (die, cu));
13706 fp->set_name (fieldname);
13707 }
13708 else if (die->tag == DW_TAG_inheritance)
13709 {
13710 /* C++ base class field. */
13711 handle_member_location (die, cu, fp);
13712 FIELD_BITSIZE (*fp) = 0;
13713 fp->set_type (die_type (die, cu));
13714 fp->set_name (fp->type ()->name ());
13715 }
13716 else
13717 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13718 }
13719
13720 /* Can the type given by DIE define another type? */
13721
13722 static bool
13723 type_can_define_types (const struct die_info *die)
13724 {
13725 switch (die->tag)
13726 {
13727 case DW_TAG_typedef:
13728 case DW_TAG_class_type:
13729 case DW_TAG_structure_type:
13730 case DW_TAG_union_type:
13731 case DW_TAG_enumeration_type:
13732 return true;
13733
13734 default:
13735 return false;
13736 }
13737 }
13738
13739 /* Add a type definition defined in the scope of the FIP's class. */
13740
13741 static void
13742 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
13743 struct dwarf2_cu *cu)
13744 {
13745 struct decl_field fp;
13746 memset (&fp, 0, sizeof (fp));
13747
13748 gdb_assert (type_can_define_types (die));
13749
13750 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13751 fp.name = dwarf2_name (die, cu);
13752 fp.type = read_type_die (die, cu);
13753
13754 /* Save accessibility. */
13755 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
13756 switch (accessibility)
13757 {
13758 case DW_ACCESS_public:
13759 /* The assumed value if neither private nor protected. */
13760 break;
13761 case DW_ACCESS_private:
13762 fp.is_private = 1;
13763 break;
13764 case DW_ACCESS_protected:
13765 fp.is_protected = 1;
13766 break;
13767 }
13768
13769 if (die->tag == DW_TAG_typedef)
13770 fip->typedef_field_list.push_back (fp);
13771 else
13772 fip->nested_types_list.push_back (fp);
13773 }
13774
13775 /* A convenience typedef that's used when finding the discriminant
13776 field for a variant part. */
13777 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
13778 offset_map_type;
13779
13780 /* Compute the discriminant range for a given variant. OBSTACK is
13781 where the results will be stored. VARIANT is the variant to
13782 process. IS_UNSIGNED indicates whether the discriminant is signed
13783 or unsigned. */
13784
13785 static const gdb::array_view<discriminant_range>
13786 convert_variant_range (struct obstack *obstack, const variant_field &variant,
13787 bool is_unsigned)
13788 {
13789 std::vector<discriminant_range> ranges;
13790
13791 if (variant.default_branch)
13792 return {};
13793
13794 if (variant.discr_list_data == nullptr)
13795 {
13796 discriminant_range r
13797 = {variant.discriminant_value, variant.discriminant_value};
13798 ranges.push_back (r);
13799 }
13800 else
13801 {
13802 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
13803 variant.discr_list_data->size);
13804 while (!data.empty ())
13805 {
13806 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
13807 {
13808 complaint (_("invalid discriminant marker: %d"), data[0]);
13809 break;
13810 }
13811 bool is_range = data[0] == DW_DSC_range;
13812 data = data.slice (1);
13813
13814 ULONGEST low, high;
13815 unsigned int bytes_read;
13816
13817 if (data.empty ())
13818 {
13819 complaint (_("DW_AT_discr_list missing low value"));
13820 break;
13821 }
13822 if (is_unsigned)
13823 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
13824 else
13825 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
13826 &bytes_read);
13827 data = data.slice (bytes_read);
13828
13829 if (is_range)
13830 {
13831 if (data.empty ())
13832 {
13833 complaint (_("DW_AT_discr_list missing high value"));
13834 break;
13835 }
13836 if (is_unsigned)
13837 high = read_unsigned_leb128 (nullptr, data.data (),
13838 &bytes_read);
13839 else
13840 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
13841 &bytes_read);
13842 data = data.slice (bytes_read);
13843 }
13844 else
13845 high = low;
13846
13847 ranges.push_back ({ low, high });
13848 }
13849 }
13850
13851 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
13852 ranges.size ());
13853 std::copy (ranges.begin (), ranges.end (), result);
13854 return gdb::array_view<discriminant_range> (result, ranges.size ());
13855 }
13856
13857 static const gdb::array_view<variant_part> create_variant_parts
13858 (struct obstack *obstack,
13859 const offset_map_type &offset_map,
13860 struct field_info *fi,
13861 const std::vector<variant_part_builder> &variant_parts);
13862
13863 /* Fill in a "struct variant" for a given variant field. RESULT is
13864 the variant to fill in. OBSTACK is where any needed allocations
13865 will be done. OFFSET_MAP holds the mapping from section offsets to
13866 fields for the type. FI describes the fields of the type we're
13867 processing. FIELD is the variant field we're converting. */
13868
13869 static void
13870 create_one_variant (variant &result, struct obstack *obstack,
13871 const offset_map_type &offset_map,
13872 struct field_info *fi, const variant_field &field)
13873 {
13874 result.discriminants = convert_variant_range (obstack, field, false);
13875 result.first_field = field.first_field + fi->baseclasses.size ();
13876 result.last_field = field.last_field + fi->baseclasses.size ();
13877 result.parts = create_variant_parts (obstack, offset_map, fi,
13878 field.variant_parts);
13879 }
13880
13881 /* Fill in a "struct variant_part" for a given variant part. RESULT
13882 is the variant part to fill in. OBSTACK is where any needed
13883 allocations will be done. OFFSET_MAP holds the mapping from
13884 section offsets to fields for the type. FI describes the fields of
13885 the type we're processing. BUILDER is the variant part to be
13886 converted. */
13887
13888 static void
13889 create_one_variant_part (variant_part &result,
13890 struct obstack *obstack,
13891 const offset_map_type &offset_map,
13892 struct field_info *fi,
13893 const variant_part_builder &builder)
13894 {
13895 auto iter = offset_map.find (builder.discriminant_offset);
13896 if (iter == offset_map.end ())
13897 {
13898 result.discriminant_index = -1;
13899 /* Doesn't matter. */
13900 result.is_unsigned = false;
13901 }
13902 else
13903 {
13904 result.discriminant_index = iter->second;
13905 result.is_unsigned
13906 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
13907 }
13908
13909 size_t n = builder.variants.size ();
13910 variant *output = new (obstack) variant[n];
13911 for (size_t i = 0; i < n; ++i)
13912 create_one_variant (output[i], obstack, offset_map, fi,
13913 builder.variants[i]);
13914
13915 result.variants = gdb::array_view<variant> (output, n);
13916 }
13917
13918 /* Create a vector of variant parts that can be attached to a type.
13919 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13920 holds the mapping from section offsets to fields for the type. FI
13921 describes the fields of the type we're processing. VARIANT_PARTS
13922 is the vector to convert. */
13923
13924 static const gdb::array_view<variant_part>
13925 create_variant_parts (struct obstack *obstack,
13926 const offset_map_type &offset_map,
13927 struct field_info *fi,
13928 const std::vector<variant_part_builder> &variant_parts)
13929 {
13930 if (variant_parts.empty ())
13931 return {};
13932
13933 size_t n = variant_parts.size ();
13934 variant_part *result = new (obstack) variant_part[n];
13935 for (size_t i = 0; i < n; ++i)
13936 create_one_variant_part (result[i], obstack, offset_map, fi,
13937 variant_parts[i]);
13938
13939 return gdb::array_view<variant_part> (result, n);
13940 }
13941
13942 /* Compute the variant part vector for FIP, attaching it to TYPE when
13943 done. */
13944
13945 static void
13946 add_variant_property (struct field_info *fip, struct type *type,
13947 struct dwarf2_cu *cu)
13948 {
13949 /* Map section offsets of fields to their field index. Note the
13950 field index here does not take the number of baseclasses into
13951 account. */
13952 offset_map_type offset_map;
13953 for (int i = 0; i < fip->fields.size (); ++i)
13954 offset_map[fip->fields[i].offset] = i;
13955
13956 struct objfile *objfile = cu->per_objfile->objfile;
13957 gdb::array_view<const variant_part> parts
13958 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
13959 fip->variant_parts);
13960
13961 struct dynamic_prop prop;
13962 prop.set_variant_parts ((gdb::array_view<variant_part> *)
13963 obstack_copy (&objfile->objfile_obstack, &parts,
13964 sizeof (parts)));
13965
13966 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
13967 }
13968
13969 /* Create the vector of fields, and attach it to the type. */
13970
13971 static void
13972 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13973 struct dwarf2_cu *cu)
13974 {
13975 int nfields = fip->nfields ();
13976
13977 /* Record the field count, allocate space for the array of fields,
13978 and create blank accessibility bitfields if necessary. */
13979 type->set_num_fields (nfields);
13980 type->set_fields
13981 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
13982
13983 if (fip->non_public_fields && cu->lang () != language_ada)
13984 {
13985 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13986
13987 TYPE_FIELD_PRIVATE_BITS (type) =
13988 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13989 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13990
13991 TYPE_FIELD_PROTECTED_BITS (type) =
13992 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13993 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13994
13995 TYPE_FIELD_IGNORE_BITS (type) =
13996 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13997 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13998 }
13999
14000 /* If the type has baseclasses, allocate and clear a bit vector for
14001 TYPE_FIELD_VIRTUAL_BITS. */
14002 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
14003 {
14004 int num_bytes = B_BYTES (fip->baseclasses.size ());
14005 unsigned char *pointer;
14006
14007 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14008 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14009 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14010 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14011 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14012 }
14013
14014 if (!fip->variant_parts.empty ())
14015 add_variant_property (fip, type, cu);
14016
14017 /* Copy the saved-up fields into the field vector. */
14018 for (int i = 0; i < nfields; ++i)
14019 {
14020 struct nextfield &field
14021 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14022 : fip->fields[i - fip->baseclasses.size ()]);
14023
14024 type->field (i) = field.field;
14025 switch (field.accessibility)
14026 {
14027 case DW_ACCESS_private:
14028 if (cu->lang () != language_ada)
14029 SET_TYPE_FIELD_PRIVATE (type, i);
14030 break;
14031
14032 case DW_ACCESS_protected:
14033 if (cu->lang () != language_ada)
14034 SET_TYPE_FIELD_PROTECTED (type, i);
14035 break;
14036
14037 case DW_ACCESS_public:
14038 break;
14039
14040 default:
14041 /* Unknown accessibility. Complain and treat it as public. */
14042 {
14043 complaint (_("unsupported accessibility %d"),
14044 field.accessibility);
14045 }
14046 break;
14047 }
14048 if (i < fip->baseclasses.size ())
14049 {
14050 switch (field.virtuality)
14051 {
14052 case DW_VIRTUALITY_virtual:
14053 case DW_VIRTUALITY_pure_virtual:
14054 if (cu->lang () == language_ada)
14055 error (_("unexpected virtuality in component of Ada type"));
14056 SET_TYPE_FIELD_VIRTUAL (type, i);
14057 break;
14058 }
14059 }
14060 }
14061 }
14062
14063 /* Return true if this member function is a constructor, false
14064 otherwise. */
14065
14066 static int
14067 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14068 {
14069 const char *fieldname;
14070 const char *type_name;
14071 int len;
14072
14073 if (die->parent == NULL)
14074 return 0;
14075
14076 if (die->parent->tag != DW_TAG_structure_type
14077 && die->parent->tag != DW_TAG_union_type
14078 && die->parent->tag != DW_TAG_class_type)
14079 return 0;
14080
14081 fieldname = dwarf2_name (die, cu);
14082 type_name = dwarf2_name (die->parent, cu);
14083 if (fieldname == NULL || type_name == NULL)
14084 return 0;
14085
14086 len = strlen (fieldname);
14087 return (strncmp (fieldname, type_name, len) == 0
14088 && (type_name[len] == '\0' || type_name[len] == '<'));
14089 }
14090
14091 /* Add a member function to the proper fieldlist. */
14092
14093 static void
14094 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14095 struct type *type, struct dwarf2_cu *cu)
14096 {
14097 struct objfile *objfile = cu->per_objfile->objfile;
14098 struct attribute *attr;
14099 int i;
14100 struct fnfieldlist *flp = nullptr;
14101 struct fn_field *fnp;
14102 const char *fieldname;
14103 struct type *this_type;
14104
14105 if (cu->lang () == language_ada)
14106 error (_("unexpected member function in Ada type"));
14107
14108 /* Get name of member function. */
14109 fieldname = dwarf2_name (die, cu);
14110 if (fieldname == NULL)
14111 return;
14112
14113 /* Look up member function name in fieldlist. */
14114 for (i = 0; i < fip->fnfieldlists.size (); i++)
14115 {
14116 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14117 {
14118 flp = &fip->fnfieldlists[i];
14119 break;
14120 }
14121 }
14122
14123 /* Create a new fnfieldlist if necessary. */
14124 if (flp == nullptr)
14125 {
14126 fip->fnfieldlists.emplace_back ();
14127 flp = &fip->fnfieldlists.back ();
14128 flp->name = fieldname;
14129 i = fip->fnfieldlists.size () - 1;
14130 }
14131
14132 /* Create a new member function field and add it to the vector of
14133 fnfieldlists. */
14134 flp->fnfields.emplace_back ();
14135 fnp = &flp->fnfields.back ();
14136
14137 /* Delay processing of the physname until later. */
14138 if (cu->lang () == language_cplus)
14139 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14140 die, cu);
14141 else
14142 {
14143 const char *physname = dwarf2_physname (fieldname, die, cu);
14144 fnp->physname = physname ? physname : "";
14145 }
14146
14147 fnp->type = alloc_type (objfile);
14148 this_type = read_type_die (die, cu);
14149 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14150 {
14151 int nparams = this_type->num_fields ();
14152
14153 /* TYPE is the domain of this method, and THIS_TYPE is the type
14154 of the method itself (TYPE_CODE_METHOD). */
14155 smash_to_method_type (fnp->type, type,
14156 this_type->target_type (),
14157 this_type->fields (),
14158 this_type->num_fields (),
14159 this_type->has_varargs ());
14160
14161 /* Handle static member functions.
14162 Dwarf2 has no clean way to discern C++ static and non-static
14163 member functions. G++ helps GDB by marking the first
14164 parameter for non-static member functions (which is the this
14165 pointer) as artificial. We obtain this information from
14166 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14167 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14168 fnp->voffset = VOFFSET_STATIC;
14169 }
14170 else
14171 complaint (_("member function type missing for '%s'"),
14172 dwarf2_full_name (fieldname, die, cu));
14173
14174 /* Get fcontext from DW_AT_containing_type if present. */
14175 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14176 fnp->fcontext = die_containing_type (die, cu);
14177
14178 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14179 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14180
14181 /* Get accessibility. */
14182 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14183 switch (accessibility)
14184 {
14185 case DW_ACCESS_private:
14186 fnp->is_private = 1;
14187 break;
14188 case DW_ACCESS_protected:
14189 fnp->is_protected = 1;
14190 break;
14191 }
14192
14193 /* Check for artificial methods. */
14194 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14195 if (attr && attr->as_boolean ())
14196 fnp->is_artificial = 1;
14197
14198 /* Check for defaulted methods. */
14199 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14200 if (attr != nullptr)
14201 fnp->defaulted = attr->defaulted ();
14202
14203 /* Check for deleted methods. */
14204 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14205 if (attr != nullptr && attr->as_boolean ())
14206 fnp->is_deleted = 1;
14207
14208 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14209
14210 /* Get index in virtual function table if it is a virtual member
14211 function. For older versions of GCC, this is an offset in the
14212 appropriate virtual table, as specified by DW_AT_containing_type.
14213 For everyone else, it is an expression to be evaluated relative
14214 to the object address. */
14215
14216 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14217 if (attr != nullptr)
14218 {
14219 if (attr->form_is_block () && attr->as_block ()->size > 0)
14220 {
14221 struct dwarf_block *block = attr->as_block ();
14222
14223 if (block->data[0] == DW_OP_constu)
14224 {
14225 /* Old-style GCC. */
14226 fnp->voffset = decode_locdesc (block, cu) + 2;
14227 }
14228 else if (block->data[0] == DW_OP_deref
14229 || (block->size > 1
14230 && block->data[0] == DW_OP_deref_size
14231 && block->data[1] == cu->header.addr_size))
14232 {
14233 fnp->voffset = decode_locdesc (block, cu);
14234 if ((fnp->voffset % cu->header.addr_size) != 0)
14235 dwarf2_complex_location_expr_complaint ();
14236 else
14237 fnp->voffset /= cu->header.addr_size;
14238 fnp->voffset += 2;
14239 }
14240 else
14241 dwarf2_complex_location_expr_complaint ();
14242
14243 if (!fnp->fcontext)
14244 {
14245 /* If there is no `this' field and no DW_AT_containing_type,
14246 we cannot actually find a base class context for the
14247 vtable! */
14248 if (this_type->num_fields () == 0
14249 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14250 {
14251 complaint (_("cannot determine context for virtual member "
14252 "function \"%s\" (offset %s)"),
14253 fieldname, sect_offset_str (die->sect_off));
14254 }
14255 else
14256 {
14257 fnp->fcontext = this_type->field (0).type ()->target_type ();
14258 }
14259 }
14260 }
14261 else if (attr->form_is_section_offset ())
14262 {
14263 dwarf2_complex_location_expr_complaint ();
14264 }
14265 else
14266 {
14267 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14268 fieldname);
14269 }
14270 }
14271 else
14272 {
14273 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14274 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
14275 {
14276 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14277 complaint (_("Member function \"%s\" (offset %s) is virtual "
14278 "but the vtable offset is not specified"),
14279 fieldname, sect_offset_str (die->sect_off));
14280 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14281 TYPE_CPLUS_DYNAMIC (type) = 1;
14282 }
14283 }
14284 }
14285
14286 /* Create the vector of member function fields, and attach it to the type. */
14287
14288 static void
14289 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14290 struct dwarf2_cu *cu)
14291 {
14292 if (cu->lang () == language_ada)
14293 error (_("unexpected member functions in Ada type"));
14294
14295 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14296 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14297 TYPE_ALLOC (type,
14298 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14299
14300 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14301 {
14302 struct fnfieldlist &nf = fip->fnfieldlists[i];
14303 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14304
14305 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14306 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14307 fn_flp->fn_fields = (struct fn_field *)
14308 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14309
14310 for (int k = 0; k < nf.fnfields.size (); ++k)
14311 fn_flp->fn_fields[k] = nf.fnfields[k];
14312 }
14313
14314 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14315 }
14316
14317 /* Returns non-zero if NAME is the name of a vtable member in CU's
14318 language, zero otherwise. */
14319 static int
14320 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14321 {
14322 static const char vptr[] = "_vptr";
14323
14324 /* Look for the C++ form of the vtable. */
14325 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14326 return 1;
14327
14328 return 0;
14329 }
14330
14331 /* GCC outputs unnamed structures that are really pointers to member
14332 functions, with the ABI-specified layout. If TYPE describes
14333 such a structure, smash it into a member function type.
14334
14335 GCC shouldn't do this; it should just output pointer to member DIEs.
14336 This is GCC PR debug/28767. */
14337
14338 static void
14339 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14340 {
14341 struct type *pfn_type, *self_type, *new_type;
14342
14343 /* Check for a structure with no name and two children. */
14344 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14345 return;
14346
14347 /* Check for __pfn and __delta members. */
14348 if (type->field (0).name () == NULL
14349 || strcmp (type->field (0).name (), "__pfn") != 0
14350 || type->field (1).name () == NULL
14351 || strcmp (type->field (1).name (), "__delta") != 0)
14352 return;
14353
14354 /* Find the type of the method. */
14355 pfn_type = type->field (0).type ();
14356 if (pfn_type == NULL
14357 || pfn_type->code () != TYPE_CODE_PTR
14358 || pfn_type->target_type ()->code () != TYPE_CODE_FUNC)
14359 return;
14360
14361 /* Look for the "this" argument. */
14362 pfn_type = pfn_type->target_type ();
14363 if (pfn_type->num_fields () == 0
14364 /* || pfn_type->field (0).type () == NULL */
14365 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
14366 return;
14367
14368 self_type = pfn_type->field (0).type ()->target_type ();
14369 new_type = alloc_type (objfile);
14370 smash_to_method_type (new_type, self_type, pfn_type->target_type (),
14371 pfn_type->fields (), pfn_type->num_fields (),
14372 pfn_type->has_varargs ());
14373 smash_to_methodptr_type (type, new_type);
14374 }
14375
14376 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14377 requires rewriting, then copy it and return the updated copy.
14378 Otherwise return nullptr. */
14379
14380 static struct type *
14381 rewrite_array_type (struct type *type)
14382 {
14383 if (type->code () != TYPE_CODE_ARRAY)
14384 return nullptr;
14385
14386 struct type *index_type = type->index_type ();
14387 range_bounds *current_bounds = index_type->bounds ();
14388
14389 /* Handle multi-dimensional arrays. */
14390 struct type *new_target = rewrite_array_type (type->target_type ());
14391 if (new_target == nullptr)
14392 {
14393 /* Maybe we don't need to rewrite this array. */
14394 if (current_bounds->low.kind () == PROP_CONST
14395 && current_bounds->high.kind () == PROP_CONST)
14396 return nullptr;
14397 }
14398
14399 /* Either the target type was rewritten, or the bounds have to be
14400 updated. Either way we want to copy the type and update
14401 everything. */
14402 struct type *copy = copy_type (type);
14403 int nfields = copy->num_fields ();
14404 field *new_fields
14405 = ((struct field *) TYPE_ZALLOC (copy,
14406 nfields * sizeof (struct field)));
14407 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
14408 copy->set_fields (new_fields);
14409 if (new_target != nullptr)
14410 copy->set_target_type (new_target);
14411
14412 struct type *index_copy = copy_type (index_type);
14413 range_bounds *bounds
14414 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
14415 sizeof (range_bounds));
14416 *bounds = *current_bounds;
14417 bounds->low.set_const_val (1);
14418 bounds->high.set_const_val (0);
14419 index_copy->set_bounds (bounds);
14420 copy->set_index_type (index_copy);
14421
14422 return copy;
14423 }
14424
14425 /* While some versions of GCC will generate complicated DWARF for an
14426 array (see quirk_ada_thick_pointer), more recent versions were
14427 modified to emit an explicit thick pointer structure. However, in
14428 this case, the array still has DWARF expressions for its ranges,
14429 and these must be ignored. */
14430
14431 static void
14432 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
14433 struct type *type)
14434 {
14435 gdb_assert (cu->lang () == language_ada);
14436
14437 /* Check for a structure with two children. */
14438 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14439 return;
14440
14441 /* Check for P_ARRAY and P_BOUNDS members. */
14442 if (type->field (0).name () == NULL
14443 || strcmp (type->field (0).name (), "P_ARRAY") != 0
14444 || type->field (1).name () == NULL
14445 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
14446 return;
14447
14448 /* Make sure we're looking at a pointer to an array. */
14449 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
14450 return;
14451
14452 /* The Ada code already knows how to handle these types, so all that
14453 we need to do is turn the bounds into static bounds. However, we
14454 don't want to rewrite existing array or index types in-place,
14455 because those may be referenced in other contexts where this
14456 rewriting is undesirable. */
14457 struct type *new_ary_type
14458 = rewrite_array_type (type->field (0).type ()->target_type ());
14459 if (new_ary_type != nullptr)
14460 type->field (0).set_type (lookup_pointer_type (new_ary_type));
14461 }
14462
14463 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14464 appropriate error checking and issuing complaints if there is a
14465 problem. */
14466
14467 static ULONGEST
14468 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14469 {
14470 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14471
14472 if (attr == nullptr)
14473 return 0;
14474
14475 if (!attr->form_is_constant ())
14476 {
14477 complaint (_("DW_AT_alignment must have constant form"
14478 " - DIE at %s [in module %s]"),
14479 sect_offset_str (die->sect_off),
14480 objfile_name (cu->per_objfile->objfile));
14481 return 0;
14482 }
14483
14484 LONGEST val = attr->constant_value (0);
14485 if (val < 0)
14486 {
14487 complaint (_("DW_AT_alignment value must not be negative"
14488 " - DIE at %s [in module %s]"),
14489 sect_offset_str (die->sect_off),
14490 objfile_name (cu->per_objfile->objfile));
14491 return 0;
14492 }
14493 ULONGEST align = val;
14494
14495 if (align == 0)
14496 {
14497 complaint (_("DW_AT_alignment value must not be zero"
14498 " - DIE at %s [in module %s]"),
14499 sect_offset_str (die->sect_off),
14500 objfile_name (cu->per_objfile->objfile));
14501 return 0;
14502 }
14503 if ((align & (align - 1)) != 0)
14504 {
14505 complaint (_("DW_AT_alignment value must be a power of 2"
14506 " - DIE at %s [in module %s]"),
14507 sect_offset_str (die->sect_off),
14508 objfile_name (cu->per_objfile->objfile));
14509 return 0;
14510 }
14511
14512 return align;
14513 }
14514
14515 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14516 the alignment for TYPE. */
14517
14518 static void
14519 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14520 struct type *type)
14521 {
14522 if (!set_type_align (type, get_alignment (cu, die)))
14523 complaint (_("DW_AT_alignment value too large"
14524 " - DIE at %s [in module %s]"),
14525 sect_offset_str (die->sect_off),
14526 objfile_name (cu->per_objfile->objfile));
14527 }
14528
14529 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14530 constant for a type, according to DWARF5 spec, Table 5.5. */
14531
14532 static bool
14533 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14534 {
14535 switch (value)
14536 {
14537 case DW_CC_normal:
14538 case DW_CC_pass_by_reference:
14539 case DW_CC_pass_by_value:
14540 return true;
14541
14542 default:
14543 complaint (_("unrecognized DW_AT_calling_convention value "
14544 "(%s) for a type"), pulongest (value));
14545 return false;
14546 }
14547 }
14548
14549 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14550 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14551 also according to GNU-specific values (see include/dwarf2.h). */
14552
14553 static bool
14554 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14555 {
14556 switch (value)
14557 {
14558 case DW_CC_normal:
14559 case DW_CC_program:
14560 case DW_CC_nocall:
14561 return true;
14562
14563 case DW_CC_GNU_renesas_sh:
14564 case DW_CC_GNU_borland_fastcall_i386:
14565 case DW_CC_GDB_IBM_OpenCL:
14566 return true;
14567
14568 default:
14569 complaint (_("unrecognized DW_AT_calling_convention value "
14570 "(%s) for a subroutine"), pulongest (value));
14571 return false;
14572 }
14573 }
14574
14575 /* Called when we find the DIE that starts a structure or union scope
14576 (definition) to create a type for the structure or union. Fill in
14577 the type's name and general properties; the members will not be
14578 processed until process_structure_scope. A symbol table entry for
14579 the type will also not be done until process_structure_scope (assuming
14580 the type has a name).
14581
14582 NOTE: we need to call these functions regardless of whether or not the
14583 DIE has a DW_AT_name attribute, since it might be an anonymous
14584 structure or union. This gets the type entered into our set of
14585 user defined types. */
14586
14587 static struct type *
14588 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14589 {
14590 struct objfile *objfile = cu->per_objfile->objfile;
14591 struct type *type;
14592 struct attribute *attr;
14593 const char *name;
14594
14595 /* If the definition of this type lives in .debug_types, read that type.
14596 Don't follow DW_AT_specification though, that will take us back up
14597 the chain and we want to go down. */
14598 attr = die->attr (DW_AT_signature);
14599 if (attr != nullptr)
14600 {
14601 type = get_DW_AT_signature_type (die, attr, cu);
14602
14603 /* The type's CU may not be the same as CU.
14604 Ensure TYPE is recorded with CU in die_type_hash. */
14605 return set_die_type (die, type, cu);
14606 }
14607
14608 type = alloc_type (objfile);
14609 INIT_CPLUS_SPECIFIC (type);
14610
14611 name = dwarf2_name (die, cu);
14612 if (name != NULL)
14613 {
14614 if (cu->lang () == language_cplus
14615 || cu->lang () == language_d
14616 || cu->lang () == language_rust)
14617 {
14618 const char *full_name = dwarf2_full_name (name, die, cu);
14619
14620 /* dwarf2_full_name might have already finished building the DIE's
14621 type. If so, there is no need to continue. */
14622 if (get_die_type (die, cu) != NULL)
14623 return get_die_type (die, cu);
14624
14625 type->set_name (full_name);
14626 }
14627 else
14628 {
14629 /* The name is already allocated along with this objfile, so
14630 we don't need to duplicate it for the type. */
14631 type->set_name (name);
14632 }
14633 }
14634
14635 if (die->tag == DW_TAG_structure_type)
14636 {
14637 type->set_code (TYPE_CODE_STRUCT);
14638 }
14639 else if (die->tag == DW_TAG_union_type)
14640 {
14641 type->set_code (TYPE_CODE_UNION);
14642 }
14643 else if (die->tag == DW_TAG_namelist)
14644 {
14645 type->set_code (TYPE_CODE_NAMELIST);
14646 }
14647 else
14648 {
14649 type->set_code (TYPE_CODE_STRUCT);
14650 }
14651
14652 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
14653 type->set_is_declared_class (true);
14654
14655 /* Store the calling convention in the type if it's available in
14656 the die. Otherwise the calling convention remains set to
14657 the default value DW_CC_normal. */
14658 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14659 if (attr != nullptr
14660 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
14661 {
14662 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14663 TYPE_CPLUS_CALLING_CONVENTION (type)
14664 = (enum dwarf_calling_convention) (attr->constant_value (0));
14665 }
14666
14667 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14668 if (attr != nullptr)
14669 {
14670 if (attr->form_is_constant ())
14671 type->set_length (attr->constant_value (0));
14672 else
14673 {
14674 struct dynamic_prop prop;
14675 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
14676 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
14677
14678 type->set_length (0);
14679 }
14680 }
14681 else
14682 type->set_length (0);
14683
14684 maybe_set_alignment (cu, die, type);
14685
14686 if (producer_is_icc_lt_14 (cu) && (type->length () == 0))
14687 {
14688 /* ICC<14 does not output the required DW_AT_declaration on
14689 incomplete types, but gives them a size of zero. */
14690 type->set_is_stub (true);
14691 }
14692 else
14693 type->set_stub_is_supported (true);
14694
14695 if (die_is_declaration (die, cu))
14696 type->set_is_stub (true);
14697 else if (attr == NULL && die->child == NULL
14698 && producer_is_realview (cu->producer))
14699 /* RealView does not output the required DW_AT_declaration
14700 on incomplete types. */
14701 type->set_is_stub (true);
14702
14703 /* We need to add the type field to the die immediately so we don't
14704 infinitely recurse when dealing with pointers to the structure
14705 type within the structure itself. */
14706 set_die_type (die, type, cu);
14707
14708 /* set_die_type should be already done. */
14709 set_descriptive_type (type, die, cu);
14710
14711 return type;
14712 }
14713
14714 static void handle_struct_member_die
14715 (struct die_info *child_die,
14716 struct type *type,
14717 struct field_info *fi,
14718 std::vector<struct symbol *> *template_args,
14719 struct dwarf2_cu *cu);
14720
14721 /* A helper for handle_struct_member_die that handles
14722 DW_TAG_variant_part. */
14723
14724 static void
14725 handle_variant_part (struct die_info *die, struct type *type,
14726 struct field_info *fi,
14727 std::vector<struct symbol *> *template_args,
14728 struct dwarf2_cu *cu)
14729 {
14730 variant_part_builder *new_part;
14731 if (fi->current_variant_part == nullptr)
14732 {
14733 fi->variant_parts.emplace_back ();
14734 new_part = &fi->variant_parts.back ();
14735 }
14736 else if (!fi->current_variant_part->processing_variant)
14737 {
14738 complaint (_("nested DW_TAG_variant_part seen "
14739 "- DIE at %s [in module %s]"),
14740 sect_offset_str (die->sect_off),
14741 objfile_name (cu->per_objfile->objfile));
14742 return;
14743 }
14744 else
14745 {
14746 variant_field &current = fi->current_variant_part->variants.back ();
14747 current.variant_parts.emplace_back ();
14748 new_part = &current.variant_parts.back ();
14749 }
14750
14751 /* When we recurse, we want callees to add to this new variant
14752 part. */
14753 scoped_restore save_current_variant_part
14754 = make_scoped_restore (&fi->current_variant_part, new_part);
14755
14756 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
14757 if (discr == NULL)
14758 {
14759 /* It's a univariant form, an extension we support. */
14760 }
14761 else if (discr->form_is_ref ())
14762 {
14763 struct dwarf2_cu *target_cu = cu;
14764 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
14765
14766 new_part->discriminant_offset = target_die->sect_off;
14767 }
14768 else
14769 {
14770 complaint (_("DW_AT_discr does not have DIE reference form"
14771 " - DIE at %s [in module %s]"),
14772 sect_offset_str (die->sect_off),
14773 objfile_name (cu->per_objfile->objfile));
14774 }
14775
14776 for (die_info *child_die = die->child;
14777 child_die != NULL;
14778 child_die = child_die->sibling)
14779 handle_struct_member_die (child_die, type, fi, template_args, cu);
14780 }
14781
14782 /* A helper for handle_struct_member_die that handles
14783 DW_TAG_variant. */
14784
14785 static void
14786 handle_variant (struct die_info *die, struct type *type,
14787 struct field_info *fi,
14788 std::vector<struct symbol *> *template_args,
14789 struct dwarf2_cu *cu)
14790 {
14791 if (fi->current_variant_part == nullptr)
14792 {
14793 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14794 "- DIE at %s [in module %s]"),
14795 sect_offset_str (die->sect_off),
14796 objfile_name (cu->per_objfile->objfile));
14797 return;
14798 }
14799 if (fi->current_variant_part->processing_variant)
14800 {
14801 complaint (_("nested DW_TAG_variant seen "
14802 "- DIE at %s [in module %s]"),
14803 sect_offset_str (die->sect_off),
14804 objfile_name (cu->per_objfile->objfile));
14805 return;
14806 }
14807
14808 scoped_restore save_processing_variant
14809 = make_scoped_restore (&fi->current_variant_part->processing_variant,
14810 true);
14811
14812 fi->current_variant_part->variants.emplace_back ();
14813 variant_field &variant = fi->current_variant_part->variants.back ();
14814 variant.first_field = fi->fields.size ();
14815
14816 /* In a variant we want to get the discriminant and also add a
14817 field for our sole member child. */
14818 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
14819 if (discr == nullptr || !discr->form_is_constant ())
14820 {
14821 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
14822 if (discr == nullptr || discr->as_block ()->size == 0)
14823 variant.default_branch = true;
14824 else
14825 variant.discr_list_data = discr->as_block ();
14826 }
14827 else
14828 variant.discriminant_value = discr->constant_value (0);
14829
14830 for (die_info *variant_child = die->child;
14831 variant_child != NULL;
14832 variant_child = variant_child->sibling)
14833 handle_struct_member_die (variant_child, type, fi, template_args, cu);
14834
14835 variant.last_field = fi->fields.size ();
14836 }
14837
14838 /* A helper for process_structure_scope that handles a single member
14839 DIE. */
14840
14841 static void
14842 handle_struct_member_die (struct die_info *child_die, struct type *type,
14843 struct field_info *fi,
14844 std::vector<struct symbol *> *template_args,
14845 struct dwarf2_cu *cu)
14846 {
14847 if (child_die->tag == DW_TAG_member
14848 || child_die->tag == DW_TAG_variable
14849 || child_die->tag == DW_TAG_namelist_item)
14850 {
14851 /* NOTE: carlton/2002-11-05: A C++ static data member
14852 should be a DW_TAG_member that is a declaration, but
14853 all versions of G++ as of this writing (so through at
14854 least 3.2.1) incorrectly generate DW_TAG_variable
14855 tags for them instead. */
14856 dwarf2_add_field (fi, child_die, cu);
14857 }
14858 else if (child_die->tag == DW_TAG_subprogram)
14859 {
14860 /* Rust doesn't have member functions in the C++ sense.
14861 However, it does emit ordinary functions as children
14862 of a struct DIE. */
14863 if (cu->lang () == language_rust)
14864 read_func_scope (child_die, cu);
14865 else
14866 {
14867 /* C++ member function. */
14868 dwarf2_add_member_fn (fi, child_die, type, cu);
14869 }
14870 }
14871 else if (child_die->tag == DW_TAG_inheritance)
14872 {
14873 /* C++ base class field. */
14874 dwarf2_add_field (fi, child_die, cu);
14875 }
14876 else if (type_can_define_types (child_die))
14877 dwarf2_add_type_defn (fi, child_die, cu);
14878 else if (child_die->tag == DW_TAG_template_type_param
14879 || child_die->tag == DW_TAG_template_value_param)
14880 {
14881 struct symbol *arg = new_symbol (child_die, NULL, cu);
14882
14883 if (arg != NULL)
14884 template_args->push_back (arg);
14885 }
14886 else if (child_die->tag == DW_TAG_variant_part)
14887 handle_variant_part (child_die, type, fi, template_args, cu);
14888 else if (child_die->tag == DW_TAG_variant)
14889 handle_variant (child_die, type, fi, template_args, cu);
14890 }
14891
14892 /* Finish creating a structure or union type, including filling in its
14893 members and creating a symbol for it. This function also handles Fortran
14894 namelist variables, their items or members and creating a symbol for
14895 them. */
14896
14897 static void
14898 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14899 {
14900 struct objfile *objfile = cu->per_objfile->objfile;
14901 struct die_info *child_die;
14902 struct type *type;
14903
14904 type = get_die_type (die, cu);
14905 if (type == NULL)
14906 type = read_structure_type (die, cu);
14907
14908 bool has_template_parameters = false;
14909 if (die->child != NULL && ! die_is_declaration (die, cu))
14910 {
14911 struct field_info fi;
14912 std::vector<struct symbol *> template_args;
14913
14914 child_die = die->child;
14915
14916 while (child_die && child_die->tag)
14917 {
14918 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
14919 child_die = child_die->sibling;
14920 }
14921
14922 /* Attach template arguments to type. */
14923 if (!template_args.empty ())
14924 {
14925 has_template_parameters = true;
14926 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14927 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14928 TYPE_TEMPLATE_ARGUMENTS (type)
14929 = XOBNEWVEC (&objfile->objfile_obstack,
14930 struct symbol *,
14931 TYPE_N_TEMPLATE_ARGUMENTS (type));
14932 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14933 template_args.data (),
14934 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14935 * sizeof (struct symbol *)));
14936 }
14937
14938 /* Attach fields and member functions to the type. */
14939 if (fi.nfields () > 0)
14940 dwarf2_attach_fields_to_type (&fi, type, cu);
14941 if (!fi.fnfieldlists.empty ())
14942 {
14943 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14944
14945 /* Get the type which refers to the base class (possibly this
14946 class itself) which contains the vtable pointer for the current
14947 class from the DW_AT_containing_type attribute. This use of
14948 DW_AT_containing_type is a GNU extension. */
14949
14950 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14951 {
14952 struct type *t = die_containing_type (die, cu);
14953
14954 set_type_vptr_basetype (type, t);
14955 if (type == t)
14956 {
14957 int i;
14958
14959 /* Our own class provides vtbl ptr. */
14960 for (i = t->num_fields () - 1;
14961 i >= TYPE_N_BASECLASSES (t);
14962 --i)
14963 {
14964 const char *fieldname = t->field (i).name ();
14965
14966 if (is_vtable_name (fieldname, cu))
14967 {
14968 set_type_vptr_fieldno (type, i);
14969 break;
14970 }
14971 }
14972
14973 /* Complain if virtual function table field not found. */
14974 if (i < TYPE_N_BASECLASSES (t))
14975 complaint (_("virtual function table pointer "
14976 "not found when defining class '%s'"),
14977 type->name () ? type->name () : "");
14978 }
14979 else
14980 {
14981 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14982 }
14983 }
14984 else if (cu->producer
14985 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14986 {
14987 /* The IBM XLC compiler does not provide direct indication
14988 of the containing type, but the vtable pointer is
14989 always named __vfp. */
14990
14991 int i;
14992
14993 for (i = type->num_fields () - 1;
14994 i >= TYPE_N_BASECLASSES (type);
14995 --i)
14996 {
14997 if (strcmp (type->field (i).name (), "__vfp") == 0)
14998 {
14999 set_type_vptr_fieldno (type, i);
15000 set_type_vptr_basetype (type, type);
15001 break;
15002 }
15003 }
15004 }
15005 }
15006
15007 /* Copy fi.typedef_field_list linked list elements content into the
15008 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15009 if (!fi.typedef_field_list.empty ())
15010 {
15011 int count = fi.typedef_field_list.size ();
15012
15013 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15014 TYPE_TYPEDEF_FIELD_ARRAY (type)
15015 = ((struct decl_field *)
15016 TYPE_ALLOC (type,
15017 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15018 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15019
15020 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15021 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15022 }
15023
15024 /* Copy fi.nested_types_list linked list elements content into the
15025 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15026 if (!fi.nested_types_list.empty ()
15027 && cu->lang () != language_ada)
15028 {
15029 int count = fi.nested_types_list.size ();
15030
15031 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15032 TYPE_NESTED_TYPES_ARRAY (type)
15033 = ((struct decl_field *)
15034 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15035 TYPE_NESTED_TYPES_COUNT (type) = count;
15036
15037 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15038 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15039 }
15040 }
15041
15042 quirk_gcc_member_function_pointer (type, objfile);
15043 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
15044 cu->rust_unions.push_back (type);
15045 else if (cu->lang () == language_ada)
15046 quirk_ada_thick_pointer_struct (die, cu, type);
15047
15048 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15049 snapshots) has been known to create a die giving a declaration
15050 for a class that has, as a child, a die giving a definition for a
15051 nested class. So we have to process our children even if the
15052 current die is a declaration. Normally, of course, a declaration
15053 won't have any children at all. */
15054
15055 child_die = die->child;
15056
15057 while (child_die != NULL && child_die->tag)
15058 {
15059 if (child_die->tag == DW_TAG_member
15060 || child_die->tag == DW_TAG_variable
15061 || child_die->tag == DW_TAG_inheritance
15062 || child_die->tag == DW_TAG_template_value_param
15063 || child_die->tag == DW_TAG_template_type_param)
15064 {
15065 /* Do nothing. */
15066 }
15067 else
15068 process_die (child_die, cu);
15069
15070 child_die = child_die->sibling;
15071 }
15072
15073 /* Do not consider external references. According to the DWARF standard,
15074 these DIEs are identified by the fact that they have no byte_size
15075 attribute, and a declaration attribute. */
15076 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15077 || !die_is_declaration (die, cu)
15078 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15079 {
15080 struct symbol *sym = new_symbol (die, type, cu);
15081
15082 if (has_template_parameters)
15083 {
15084 struct symtab *symtab;
15085 if (sym != nullptr)
15086 symtab = sym->symtab ();
15087 else if (cu->line_header != nullptr)
15088 {
15089 /* Any related symtab will do. */
15090 symtab
15091 = cu->line_header->file_names ()[0].symtab;
15092 }
15093 else
15094 {
15095 symtab = nullptr;
15096 complaint (_("could not find suitable "
15097 "symtab for template parameter"
15098 " - DIE at %s [in module %s]"),
15099 sect_offset_str (die->sect_off),
15100 objfile_name (objfile));
15101 }
15102
15103 if (symtab != nullptr)
15104 {
15105 /* Make sure that the symtab is set on the new symbols.
15106 Even though they don't appear in this symtab directly,
15107 other parts of gdb assume that symbols do, and this is
15108 reasonably true. */
15109 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15110 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
15111 }
15112 }
15113 }
15114 }
15115
15116 /* Assuming DIE is an enumeration type, and TYPE is its associated
15117 type, update TYPE using some information only available in DIE's
15118 children. In particular, the fields are computed. */
15119
15120 static void
15121 update_enumeration_type_from_children (struct die_info *die,
15122 struct type *type,
15123 struct dwarf2_cu *cu)
15124 {
15125 struct die_info *child_die;
15126 int unsigned_enum = 1;
15127 int flag_enum = 1;
15128
15129 auto_obstack obstack;
15130 std::vector<struct field> fields;
15131
15132 for (child_die = die->child;
15133 child_die != NULL && child_die->tag;
15134 child_die = child_die->sibling)
15135 {
15136 struct attribute *attr;
15137 LONGEST value;
15138 const gdb_byte *bytes;
15139 struct dwarf2_locexpr_baton *baton;
15140 const char *name;
15141
15142 if (child_die->tag != DW_TAG_enumerator)
15143 continue;
15144
15145 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15146 if (attr == NULL)
15147 continue;
15148
15149 name = dwarf2_name (child_die, cu);
15150 if (name == NULL)
15151 name = "<anonymous enumerator>";
15152
15153 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15154 &value, &bytes, &baton);
15155 if (value < 0)
15156 {
15157 unsigned_enum = 0;
15158 flag_enum = 0;
15159 }
15160 else
15161 {
15162 if (count_one_bits_ll (value) >= 2)
15163 flag_enum = 0;
15164 }
15165
15166 fields.emplace_back ();
15167 struct field &field = fields.back ();
15168 field.set_name (dwarf2_physname (name, child_die, cu));
15169 field.set_loc_enumval (value);
15170 }
15171
15172 if (!fields.empty ())
15173 {
15174 type->set_num_fields (fields.size ());
15175 type->set_fields
15176 ((struct field *)
15177 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15178 memcpy (type->fields (), fields.data (),
15179 sizeof (struct field) * fields.size ());
15180 }
15181
15182 if (unsigned_enum)
15183 type->set_is_unsigned (true);
15184
15185 if (flag_enum)
15186 type->set_is_flag_enum (true);
15187 }
15188
15189 /* Given a DW_AT_enumeration_type die, set its type. We do not
15190 complete the type's fields yet, or create any symbols. */
15191
15192 static struct type *
15193 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15194 {
15195 struct objfile *objfile = cu->per_objfile->objfile;
15196 struct type *type;
15197 struct attribute *attr;
15198 const char *name;
15199
15200 /* If the definition of this type lives in .debug_types, read that type.
15201 Don't follow DW_AT_specification though, that will take us back up
15202 the chain and we want to go down. */
15203 attr = die->attr (DW_AT_signature);
15204 if (attr != nullptr)
15205 {
15206 type = get_DW_AT_signature_type (die, attr, cu);
15207
15208 /* The type's CU may not be the same as CU.
15209 Ensure TYPE is recorded with CU in die_type_hash. */
15210 return set_die_type (die, type, cu);
15211 }
15212
15213 type = alloc_type (objfile);
15214
15215 type->set_code (TYPE_CODE_ENUM);
15216 name = dwarf2_full_name (NULL, die, cu);
15217 if (name != NULL)
15218 type->set_name (name);
15219
15220 attr = dwarf2_attr (die, DW_AT_type, cu);
15221 if (attr != NULL)
15222 {
15223 struct type *underlying_type = die_type (die, cu);
15224
15225 type->set_target_type (underlying_type);
15226 }
15227
15228 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15229 if (attr != nullptr)
15230 type->set_length (attr->constant_value (0));
15231 else
15232 type->set_length (0);
15233
15234 maybe_set_alignment (cu, die, type);
15235
15236 /* The enumeration DIE can be incomplete. In Ada, any type can be
15237 declared as private in the package spec, and then defined only
15238 inside the package body. Such types are known as Taft Amendment
15239 Types. When another package uses such a type, an incomplete DIE
15240 may be generated by the compiler. */
15241 if (die_is_declaration (die, cu))
15242 type->set_is_stub (true);
15243
15244 /* If this type has an underlying type that is not a stub, then we
15245 may use its attributes. We always use the "unsigned" attribute
15246 in this situation, because ordinarily we guess whether the type
15247 is unsigned -- but the guess can be wrong and the underlying type
15248 can tell us the reality. However, we defer to a local size
15249 attribute if one exists, because this lets the compiler override
15250 the underlying type if needed. */
15251 if (type->target_type () != NULL && !type->target_type ()->is_stub ())
15252 {
15253 struct type *underlying_type = type->target_type ();
15254 underlying_type = check_typedef (underlying_type);
15255
15256 type->set_is_unsigned (underlying_type->is_unsigned ());
15257
15258 if (type->length () == 0)
15259 type->set_length (underlying_type->length ());
15260
15261 if (TYPE_RAW_ALIGN (type) == 0
15262 && TYPE_RAW_ALIGN (underlying_type) != 0)
15263 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15264 }
15265
15266 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
15267
15268 set_die_type (die, type, cu);
15269
15270 /* Finish the creation of this type by using the enum's children.
15271 Note that, as usual, this must come after set_die_type to avoid
15272 infinite recursion when trying to compute the names of the
15273 enumerators. */
15274 update_enumeration_type_from_children (die, type, cu);
15275
15276 return type;
15277 }
15278
15279 /* Given a pointer to a die which begins an enumeration, process all
15280 the dies that define the members of the enumeration, and create the
15281 symbol for the enumeration type.
15282
15283 NOTE: We reverse the order of the element list. */
15284
15285 static void
15286 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15287 {
15288 struct type *this_type;
15289
15290 this_type = get_die_type (die, cu);
15291 if (this_type == NULL)
15292 this_type = read_enumeration_type (die, cu);
15293
15294 if (die->child != NULL)
15295 {
15296 struct die_info *child_die;
15297 const char *name;
15298
15299 child_die = die->child;
15300 while (child_die && child_die->tag)
15301 {
15302 if (child_die->tag != DW_TAG_enumerator)
15303 {
15304 process_die (child_die, cu);
15305 }
15306 else
15307 {
15308 name = dwarf2_name (child_die, cu);
15309 if (name)
15310 new_symbol (child_die, this_type, cu);
15311 }
15312
15313 child_die = child_die->sibling;
15314 }
15315 }
15316
15317 /* If we are reading an enum from a .debug_types unit, and the enum
15318 is a declaration, and the enum is not the signatured type in the
15319 unit, then we do not want to add a symbol for it. Adding a
15320 symbol would in some cases obscure the true definition of the
15321 enum, giving users an incomplete type when the definition is
15322 actually available. Note that we do not want to do this for all
15323 enums which are just declarations, because C++0x allows forward
15324 enum declarations. */
15325 if (cu->per_cu->is_debug_types
15326 && die_is_declaration (die, cu))
15327 {
15328 struct signatured_type *sig_type;
15329
15330 sig_type = (struct signatured_type *) cu->per_cu;
15331 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15332 if (sig_type->type_offset_in_section != die->sect_off)
15333 return;
15334 }
15335
15336 new_symbol (die, this_type, cu);
15337 }
15338
15339 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15340 expression for an index type and finds the corresponding field
15341 offset in the hidden "P_BOUNDS" structure. Returns true on success
15342 and updates *FIELD, false if it fails to recognize an
15343 expression. */
15344
15345 static bool
15346 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
15347 int *bounds_offset, struct field *field,
15348 struct dwarf2_cu *cu)
15349 {
15350 struct attribute *attr = dwarf2_attr (die, name, cu);
15351 if (attr == nullptr || !attr->form_is_block ())
15352 return false;
15353
15354 const struct dwarf_block *block = attr->as_block ();
15355 const gdb_byte *start = block->data;
15356 const gdb_byte *end = block->data + block->size;
15357
15358 /* The expression to recognize generally looks like:
15359
15360 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15361 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15362
15363 However, the second "plus_uconst" may be missing:
15364
15365 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15366 DW_OP_deref_size: 4)
15367
15368 This happens when the field is at the start of the structure.
15369
15370 Also, the final deref may not be sized:
15371
15372 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15373 DW_OP_deref)
15374
15375 This happens when the size of the index type happens to be the
15376 same as the architecture's word size. This can occur with or
15377 without the second plus_uconst. */
15378
15379 if (end - start < 2)
15380 return false;
15381 if (*start++ != DW_OP_push_object_address)
15382 return false;
15383 if (*start++ != DW_OP_plus_uconst)
15384 return false;
15385
15386 uint64_t this_bound_off;
15387 start = gdb_read_uleb128 (start, end, &this_bound_off);
15388 if (start == nullptr || (int) this_bound_off != this_bound_off)
15389 return false;
15390 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15391 is consistent among all bounds. */
15392 if (*bounds_offset == -1)
15393 *bounds_offset = this_bound_off;
15394 else if (*bounds_offset != this_bound_off)
15395 return false;
15396
15397 if (start == end || *start++ != DW_OP_deref)
15398 return false;
15399
15400 int offset = 0;
15401 if (start ==end)
15402 return false;
15403 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
15404 {
15405 /* This means an offset of 0. */
15406 }
15407 else if (*start++ != DW_OP_plus_uconst)
15408 return false;
15409 else
15410 {
15411 /* The size is the parameter to DW_OP_plus_uconst. */
15412 uint64_t val;
15413 start = gdb_read_uleb128 (start, end, &val);
15414 if (start == nullptr)
15415 return false;
15416 if ((int) val != val)
15417 return false;
15418 offset = val;
15419 }
15420
15421 if (start == end)
15422 return false;
15423
15424 uint64_t size;
15425 if (*start == DW_OP_deref_size)
15426 {
15427 start = gdb_read_uleb128 (start + 1, end, &size);
15428 if (start == nullptr)
15429 return false;
15430 }
15431 else if (*start == DW_OP_deref)
15432 {
15433 size = cu->header.addr_size;
15434 ++start;
15435 }
15436 else
15437 return false;
15438
15439 field->set_loc_bitpos (8 * offset);
15440 if (size != field->type ()->length ())
15441 FIELD_BITSIZE (*field) = 8 * size;
15442
15443 return true;
15444 }
15445
15446 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15447 some kinds of Ada arrays:
15448
15449 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15450 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15451 <11e0> DW_AT_data_location: 2 byte block: 97 6
15452 (DW_OP_push_object_address; DW_OP_deref)
15453 <11e3> DW_AT_type : <0x1173>
15454 <11e7> DW_AT_sibling : <0x1201>
15455 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15456 <11ec> DW_AT_type : <0x1206>
15457 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15458 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15459 DW_OP_deref_size: 4)
15460 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15461 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15462 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15463
15464 This actually represents a "thick pointer", which is a structure
15465 with two elements: one that is a pointer to the array data, and one
15466 that is a pointer to another structure; this second structure holds
15467 the array bounds.
15468
15469 This returns a new type on success, or nullptr if this didn't
15470 recognize the type. */
15471
15472 static struct type *
15473 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
15474 struct type *type)
15475 {
15476 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
15477 /* So far we've only seen this with block form. */
15478 if (attr == nullptr || !attr->form_is_block ())
15479 return nullptr;
15480
15481 /* Note that this will fail if the structure layout is changed by
15482 the compiler. However, we have no good way to recognize some
15483 other layout, because we don't know what expression the compiler
15484 might choose to emit should this happen. */
15485 struct dwarf_block *blk = attr->as_block ();
15486 if (blk->size != 2
15487 || blk->data[0] != DW_OP_push_object_address
15488 || blk->data[1] != DW_OP_deref)
15489 return nullptr;
15490
15491 int bounds_offset = -1;
15492 int max_align = -1;
15493 std::vector<struct field> range_fields;
15494 for (struct die_info *child_die = die->child;
15495 child_die;
15496 child_die = child_die->sibling)
15497 {
15498 if (child_die->tag == DW_TAG_subrange_type)
15499 {
15500 struct type *underlying = read_subrange_index_type (child_die, cu);
15501
15502 int this_align = type_align (underlying);
15503 if (this_align > max_align)
15504 max_align = this_align;
15505
15506 range_fields.emplace_back ();
15507 range_fields.emplace_back ();
15508
15509 struct field &lower = range_fields[range_fields.size () - 2];
15510 struct field &upper = range_fields[range_fields.size () - 1];
15511
15512 lower.set_type (underlying);
15513 FIELD_ARTIFICIAL (lower) = 1;
15514
15515 upper.set_type (underlying);
15516 FIELD_ARTIFICIAL (upper) = 1;
15517
15518 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
15519 &bounds_offset, &lower, cu)
15520 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
15521 &bounds_offset, &upper, cu))
15522 return nullptr;
15523 }
15524 }
15525
15526 /* This shouldn't really happen, but double-check that we found
15527 where the bounds are stored. */
15528 if (bounds_offset == -1)
15529 return nullptr;
15530
15531 struct objfile *objfile = cu->per_objfile->objfile;
15532 for (int i = 0; i < range_fields.size (); i += 2)
15533 {
15534 char name[20];
15535
15536 /* Set the name of each field in the bounds. */
15537 xsnprintf (name, sizeof (name), "LB%d", i / 2);
15538 range_fields[i].set_name (objfile->intern (name));
15539 xsnprintf (name, sizeof (name), "UB%d", i / 2);
15540 range_fields[i + 1].set_name (objfile->intern (name));
15541 }
15542
15543 struct type *bounds = alloc_type (objfile);
15544 bounds->set_code (TYPE_CODE_STRUCT);
15545
15546 bounds->set_num_fields (range_fields.size ());
15547 bounds->set_fields
15548 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
15549 * sizeof (struct field))));
15550 memcpy (bounds->fields (), range_fields.data (),
15551 bounds->num_fields () * sizeof (struct field));
15552
15553 int last_fieldno = range_fields.size () - 1;
15554 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
15555 + bounds->field (last_fieldno).type ()->length ());
15556 bounds->set_length (align_up (bounds_size, max_align));
15557
15558 /* Rewrite the existing array type in place. Specifically, we
15559 remove any dynamic properties we might have read, and we replace
15560 the index types. */
15561 struct type *iter = type;
15562 for (int i = 0; i < range_fields.size (); i += 2)
15563 {
15564 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
15565 iter->main_type->dyn_prop_list = nullptr;
15566 iter->set_index_type
15567 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
15568 iter = iter->target_type ();
15569 }
15570
15571 struct type *result = alloc_type (objfile);
15572 result->set_code (TYPE_CODE_STRUCT);
15573
15574 result->set_num_fields (2);
15575 result->set_fields
15576 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
15577 * sizeof (struct field))));
15578
15579 /* The names are chosen to coincide with what the compiler does with
15580 -fgnat-encodings=all, which the Ada code in gdb already
15581 understands. */
15582 result->field (0).set_name ("P_ARRAY");
15583 result->field (0).set_type (lookup_pointer_type (type));
15584
15585 result->field (1).set_name ("P_BOUNDS");
15586 result->field (1).set_type (lookup_pointer_type (bounds));
15587 result->field (1).set_loc_bitpos (8 * bounds_offset);
15588
15589 result->set_name (type->name ());
15590 result->set_length (result->field (0).type ()->length ()
15591 + result->field (1).type ()->length ());
15592
15593 return result;
15594 }
15595
15596 /* Extract all information from a DW_TAG_array_type DIE and put it in
15597 the DIE's type field. For now, this only handles one dimensional
15598 arrays. */
15599
15600 static struct type *
15601 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15602 {
15603 struct objfile *objfile = cu->per_objfile->objfile;
15604 struct die_info *child_die;
15605 struct type *type;
15606 struct type *element_type, *range_type, *index_type;
15607 struct attribute *attr;
15608 const char *name;
15609 struct dynamic_prop *byte_stride_prop = NULL;
15610 unsigned int bit_stride = 0;
15611
15612 element_type = die_type (die, cu);
15613
15614 /* The die_type call above may have already set the type for this DIE. */
15615 type = get_die_type (die, cu);
15616 if (type)
15617 return type;
15618
15619 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15620 if (attr != NULL)
15621 {
15622 int stride_ok;
15623 struct type *prop_type = cu->addr_sized_int_type (false);
15624
15625 byte_stride_prop
15626 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15627 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15628 prop_type);
15629 if (!stride_ok)
15630 {
15631 complaint (_("unable to read array DW_AT_byte_stride "
15632 " - DIE at %s [in module %s]"),
15633 sect_offset_str (die->sect_off),
15634 objfile_name (cu->per_objfile->objfile));
15635 /* Ignore this attribute. We will likely not be able to print
15636 arrays of this type correctly, but there is little we can do
15637 to help if we cannot read the attribute's value. */
15638 byte_stride_prop = NULL;
15639 }
15640 }
15641
15642 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15643 if (attr != NULL)
15644 bit_stride = attr->constant_value (0);
15645
15646 /* Irix 6.2 native cc creates array types without children for
15647 arrays with unspecified length. */
15648 if (die->child == NULL)
15649 {
15650 index_type = objfile_type (objfile)->builtin_int;
15651 range_type = create_static_range_type (NULL, index_type, 0, -1);
15652 type = create_array_type_with_stride (NULL, element_type, range_type,
15653 byte_stride_prop, bit_stride);
15654 return set_die_type (die, type, cu);
15655 }
15656
15657 std::vector<struct type *> range_types;
15658 child_die = die->child;
15659 while (child_die && child_die->tag)
15660 {
15661 if (child_die->tag == DW_TAG_subrange_type
15662 || child_die->tag == DW_TAG_generic_subrange)
15663 {
15664 struct type *child_type = read_type_die (child_die, cu);
15665
15666 if (child_type != NULL)
15667 {
15668 /* The range type was succesfully read. Save it for the
15669 array type creation. */
15670 range_types.push_back (child_type);
15671 }
15672 }
15673 child_die = child_die->sibling;
15674 }
15675
15676 if (range_types.empty ())
15677 {
15678 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15679 sect_offset_str (die->sect_off),
15680 objfile_name (cu->per_objfile->objfile));
15681 return NULL;
15682 }
15683
15684 /* Dwarf2 dimensions are output from left to right, create the
15685 necessary array types in backwards order. */
15686
15687 type = element_type;
15688
15689 if (read_array_order (die, cu) == DW_ORD_col_major)
15690 {
15691 int i = 0;
15692
15693 while (i < range_types.size ())
15694 {
15695 type = create_array_type_with_stride (NULL, type, range_types[i++],
15696 byte_stride_prop, bit_stride);
15697 type->set_is_multi_dimensional (true);
15698 bit_stride = 0;
15699 byte_stride_prop = nullptr;
15700 }
15701 }
15702 else
15703 {
15704 size_t ndim = range_types.size ();
15705 while (ndim-- > 0)
15706 {
15707 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15708 byte_stride_prop, bit_stride);
15709 type->set_is_multi_dimensional (true);
15710 bit_stride = 0;
15711 byte_stride_prop = nullptr;
15712 }
15713 }
15714
15715 /* Clear the flag on the outermost array type. */
15716 type->set_is_multi_dimensional (false);
15717 gdb_assert (type != element_type);
15718
15719 /* Understand Dwarf2 support for vector types (like they occur on
15720 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15721 array type. This is not part of the Dwarf2/3 standard yet, but a
15722 custom vendor extension. The main difference between a regular
15723 array and the vector variant is that vectors are passed by value
15724 to functions. */
15725 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15726 if (attr != nullptr)
15727 make_vector_type (type);
15728
15729 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15730 implementation may choose to implement triple vectors using this
15731 attribute. */
15732 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15733 if (attr != nullptr && attr->form_is_unsigned ())
15734 {
15735 if (attr->as_unsigned () >= type->length ())
15736 type->set_length (attr->as_unsigned ());
15737 else
15738 complaint (_("DW_AT_byte_size for array type smaller "
15739 "than the total size of elements"));
15740 }
15741
15742 name = dwarf2_name (die, cu);
15743 if (name)
15744 type->set_name (name);
15745
15746 maybe_set_alignment (cu, die, type);
15747
15748 struct type *replacement_type = nullptr;
15749 if (cu->lang () == language_ada)
15750 {
15751 replacement_type = quirk_ada_thick_pointer (die, cu, type);
15752 if (replacement_type != nullptr)
15753 type = replacement_type;
15754 }
15755
15756 /* Install the type in the die. */
15757 set_die_type (die, type, cu, replacement_type != nullptr);
15758
15759 /* set_die_type should be already done. */
15760 set_descriptive_type (type, die, cu);
15761
15762 return type;
15763 }
15764
15765 static enum dwarf_array_dim_ordering
15766 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15767 {
15768 struct attribute *attr;
15769
15770 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15771
15772 if (attr != nullptr)
15773 {
15774 LONGEST val = attr->constant_value (-1);
15775 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
15776 return (enum dwarf_array_dim_ordering) val;
15777 }
15778
15779 /* GNU F77 is a special case, as at 08/2004 array type info is the
15780 opposite order to the dwarf2 specification, but data is still
15781 laid out as per normal fortran.
15782
15783 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15784 version checking. */
15785
15786 if (cu->lang () == language_fortran
15787 && cu->producer && strstr (cu->producer, "GNU F77"))
15788 {
15789 return DW_ORD_row_major;
15790 }
15791
15792 switch (cu->language_defn->array_ordering ())
15793 {
15794 case array_column_major:
15795 return DW_ORD_col_major;
15796 case array_row_major:
15797 default:
15798 return DW_ORD_row_major;
15799 };
15800 }
15801
15802 /* Extract all information from a DW_TAG_set_type DIE and put it in
15803 the DIE's type field. */
15804
15805 static struct type *
15806 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15807 {
15808 struct type *domain_type, *set_type;
15809 struct attribute *attr;
15810
15811 domain_type = die_type (die, cu);
15812
15813 /* The die_type call above may have already set the type for this DIE. */
15814 set_type = get_die_type (die, cu);
15815 if (set_type)
15816 return set_type;
15817
15818 set_type = create_set_type (NULL, domain_type);
15819
15820 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15821 if (attr != nullptr && attr->form_is_unsigned ())
15822 set_type->set_length (attr->as_unsigned ());
15823
15824 maybe_set_alignment (cu, die, set_type);
15825
15826 return set_die_type (die, set_type, cu);
15827 }
15828
15829 /* A helper for read_common_block that creates a locexpr baton.
15830 SYM is the symbol which we are marking as computed.
15831 COMMON_DIE is the DIE for the common block.
15832 COMMON_LOC is the location expression attribute for the common
15833 block itself.
15834 MEMBER_LOC is the location expression attribute for the particular
15835 member of the common block that we are processing.
15836 CU is the CU from which the above come. */
15837
15838 static void
15839 mark_common_block_symbol_computed (struct symbol *sym,
15840 struct die_info *common_die,
15841 struct attribute *common_loc,
15842 struct attribute *member_loc,
15843 struct dwarf2_cu *cu)
15844 {
15845 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15846 struct objfile *objfile = per_objfile->objfile;
15847 struct dwarf2_locexpr_baton *baton;
15848 gdb_byte *ptr;
15849 unsigned int cu_off;
15850 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
15851 LONGEST offset = 0;
15852
15853 gdb_assert (common_loc && member_loc);
15854 gdb_assert (common_loc->form_is_block ());
15855 gdb_assert (member_loc->form_is_block ()
15856 || member_loc->form_is_constant ());
15857
15858 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15859 baton->per_objfile = per_objfile;
15860 baton->per_cu = cu->per_cu;
15861 gdb_assert (baton->per_cu);
15862
15863 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15864
15865 if (member_loc->form_is_constant ())
15866 {
15867 offset = member_loc->constant_value (0);
15868 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15869 }
15870 else
15871 baton->size += member_loc->as_block ()->size;
15872
15873 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15874 baton->data = ptr;
15875
15876 *ptr++ = DW_OP_call4;
15877 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15878 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15879 ptr += 4;
15880
15881 if (member_loc->form_is_constant ())
15882 {
15883 *ptr++ = DW_OP_addr;
15884 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15885 ptr += cu->header.addr_size;
15886 }
15887 else
15888 {
15889 /* We have to copy the data here, because DW_OP_call4 will only
15890 use a DW_AT_location attribute. */
15891 struct dwarf_block *block = member_loc->as_block ();
15892 memcpy (ptr, block->data, block->size);
15893 ptr += block->size;
15894 }
15895
15896 *ptr++ = DW_OP_plus;
15897 gdb_assert (ptr - baton->data == baton->size);
15898
15899 SYMBOL_LOCATION_BATON (sym) = baton;
15900 sym->set_aclass_index (dwarf2_locexpr_index);
15901 }
15902
15903 /* Create appropriate locally-scoped variables for all the
15904 DW_TAG_common_block entries. Also create a struct common_block
15905 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15906 is used to separate the common blocks name namespace from regular
15907 variable names. */
15908
15909 static void
15910 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15911 {
15912 struct attribute *attr;
15913
15914 attr = dwarf2_attr (die, DW_AT_location, cu);
15915 if (attr != nullptr)
15916 {
15917 /* Support the .debug_loc offsets. */
15918 if (attr->form_is_block ())
15919 {
15920 /* Ok. */
15921 }
15922 else if (attr->form_is_section_offset ())
15923 {
15924 dwarf2_complex_location_expr_complaint ();
15925 attr = NULL;
15926 }
15927 else
15928 {
15929 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15930 "common block member");
15931 attr = NULL;
15932 }
15933 }
15934
15935 if (die->child != NULL)
15936 {
15937 struct objfile *objfile = cu->per_objfile->objfile;
15938 struct die_info *child_die;
15939 size_t n_entries = 0, size;
15940 struct common_block *common_block;
15941 struct symbol *sym;
15942
15943 for (child_die = die->child;
15944 child_die && child_die->tag;
15945 child_die = child_die->sibling)
15946 ++n_entries;
15947
15948 size = (sizeof (struct common_block)
15949 + (n_entries - 1) * sizeof (struct symbol *));
15950 common_block
15951 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15952 size);
15953 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15954 common_block->n_entries = 0;
15955
15956 for (child_die = die->child;
15957 child_die && child_die->tag;
15958 child_die = child_die->sibling)
15959 {
15960 /* Create the symbol in the DW_TAG_common_block block in the current
15961 symbol scope. */
15962 sym = new_symbol (child_die, NULL, cu);
15963 if (sym != NULL)
15964 {
15965 struct attribute *member_loc;
15966
15967 common_block->contents[common_block->n_entries++] = sym;
15968
15969 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15970 cu);
15971 if (member_loc)
15972 {
15973 /* GDB has handled this for a long time, but it is
15974 not specified by DWARF. It seems to have been
15975 emitted by gfortran at least as recently as:
15976 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15977 complaint (_("Variable in common block has "
15978 "DW_AT_data_member_location "
15979 "- DIE at %s [in module %s]"),
15980 sect_offset_str (child_die->sect_off),
15981 objfile_name (objfile));
15982
15983 if (member_loc->form_is_section_offset ())
15984 dwarf2_complex_location_expr_complaint ();
15985 else if (member_loc->form_is_constant ()
15986 || member_loc->form_is_block ())
15987 {
15988 if (attr != nullptr)
15989 mark_common_block_symbol_computed (sym, die, attr,
15990 member_loc, cu);
15991 }
15992 else
15993 dwarf2_complex_location_expr_complaint ();
15994 }
15995 }
15996 }
15997
15998 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15999 sym->set_value_common_block (common_block);
16000 }
16001 }
16002
16003 /* Create a type for a C++ namespace. */
16004
16005 static struct type *
16006 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16007 {
16008 struct objfile *objfile = cu->per_objfile->objfile;
16009 const char *previous_prefix, *name;
16010 int is_anonymous;
16011 struct type *type;
16012
16013 /* For extensions, reuse the type of the original namespace. */
16014 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16015 {
16016 struct die_info *ext_die;
16017 struct dwarf2_cu *ext_cu = cu;
16018
16019 ext_die = dwarf2_extension (die, &ext_cu);
16020 type = read_type_die (ext_die, ext_cu);
16021
16022 /* EXT_CU may not be the same as CU.
16023 Ensure TYPE is recorded with CU in die_type_hash. */
16024 return set_die_type (die, type, cu);
16025 }
16026
16027 name = namespace_name (die, &is_anonymous, cu);
16028
16029 /* Now build the name of the current namespace. */
16030
16031 previous_prefix = determine_prefix (die, cu);
16032 if (previous_prefix[0] != '\0')
16033 name = typename_concat (&objfile->objfile_obstack,
16034 previous_prefix, name, 0, cu);
16035
16036 /* Create the type. */
16037 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16038
16039 return set_die_type (die, type, cu);
16040 }
16041
16042 /* Read a namespace scope. */
16043
16044 static void
16045 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16046 {
16047 struct objfile *objfile = cu->per_objfile->objfile;
16048 int is_anonymous;
16049
16050 /* Add a symbol associated to this if we haven't seen the namespace
16051 before. Also, add a using directive if it's an anonymous
16052 namespace. */
16053
16054 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16055 {
16056 struct type *type;
16057
16058 type = read_type_die (die, cu);
16059 new_symbol (die, type, cu);
16060
16061 namespace_name (die, &is_anonymous, cu);
16062 if (is_anonymous)
16063 {
16064 const char *previous_prefix = determine_prefix (die, cu);
16065
16066 std::vector<const char *> excludes;
16067 add_using_directive (using_directives (cu),
16068 previous_prefix, type->name (), NULL,
16069 NULL, excludes,
16070 read_decl_line (die, cu),
16071 0, &objfile->objfile_obstack);
16072 }
16073 }
16074
16075 if (die->child != NULL)
16076 {
16077 struct die_info *child_die = die->child;
16078
16079 while (child_die && child_die->tag)
16080 {
16081 process_die (child_die, cu);
16082 child_die = child_die->sibling;
16083 }
16084 }
16085 }
16086
16087 /* Read a Fortran module as type. This DIE can be only a declaration used for
16088 imported module. Still we need that type as local Fortran "use ... only"
16089 declaration imports depend on the created type in determine_prefix. */
16090
16091 static struct type *
16092 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16093 {
16094 struct objfile *objfile = cu->per_objfile->objfile;
16095 const char *module_name;
16096 struct type *type;
16097
16098 module_name = dwarf2_name (die, cu);
16099 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16100
16101 return set_die_type (die, type, cu);
16102 }
16103
16104 /* Read a Fortran module. */
16105
16106 static void
16107 read_module (struct die_info *die, struct dwarf2_cu *cu)
16108 {
16109 struct die_info *child_die = die->child;
16110 struct type *type;
16111
16112 type = read_type_die (die, cu);
16113 new_symbol (die, type, cu);
16114
16115 while (child_die && child_die->tag)
16116 {
16117 process_die (child_die, cu);
16118 child_die = child_die->sibling;
16119 }
16120 }
16121
16122 /* Return the name of the namespace represented by DIE. Set
16123 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16124 namespace. */
16125
16126 static const char *
16127 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16128 {
16129 struct die_info *current_die;
16130 const char *name = NULL;
16131
16132 /* Loop through the extensions until we find a name. */
16133
16134 for (current_die = die;
16135 current_die != NULL;
16136 current_die = dwarf2_extension (die, &cu))
16137 {
16138 /* We don't use dwarf2_name here so that we can detect the absence
16139 of a name -> anonymous namespace. */
16140 name = dwarf2_string_attr (die, DW_AT_name, cu);
16141
16142 if (name != NULL)
16143 break;
16144 }
16145
16146 /* Is it an anonymous namespace? */
16147
16148 *is_anonymous = (name == NULL);
16149 if (*is_anonymous)
16150 name = CP_ANONYMOUS_NAMESPACE_STR;
16151
16152 return name;
16153 }
16154
16155 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16156 the user defined type vector. */
16157
16158 static struct type *
16159 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16160 {
16161 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16162 struct comp_unit_head *cu_header = &cu->header;
16163 struct type *type;
16164 struct attribute *attr_byte_size;
16165 struct attribute *attr_address_class;
16166 int byte_size, addr_class;
16167 struct type *target_type;
16168
16169 target_type = die_type (die, cu);
16170
16171 /* The die_type call above may have already set the type for this DIE. */
16172 type = get_die_type (die, cu);
16173 if (type)
16174 return type;
16175
16176 type = lookup_pointer_type (target_type);
16177
16178 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16179 if (attr_byte_size)
16180 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
16181 else
16182 byte_size = cu_header->addr_size;
16183
16184 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16185 if (attr_address_class)
16186 addr_class = attr_address_class->constant_value (DW_ADDR_none);
16187 else
16188 addr_class = DW_ADDR_none;
16189
16190 ULONGEST alignment = get_alignment (cu, die);
16191
16192 /* If the pointer size, alignment, or address class is different
16193 than the default, create a type variant marked as such and set
16194 the length accordingly. */
16195 if (type->length () != byte_size
16196 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16197 && alignment != TYPE_RAW_ALIGN (type))
16198 || addr_class != DW_ADDR_none)
16199 {
16200 if (gdbarch_address_class_type_flags_p (gdbarch))
16201 {
16202 type_instance_flags type_flags
16203 = gdbarch_address_class_type_flags (gdbarch, byte_size,
16204 addr_class);
16205 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16206 == 0);
16207 type = make_type_with_address_space (type, type_flags);
16208 }
16209 else if (type->length () != byte_size)
16210 {
16211 complaint (_("invalid pointer size %d"), byte_size);
16212 }
16213 else if (TYPE_RAW_ALIGN (type) != alignment)
16214 {
16215 complaint (_("Invalid DW_AT_alignment"
16216 " - DIE at %s [in module %s]"),
16217 sect_offset_str (die->sect_off),
16218 objfile_name (cu->per_objfile->objfile));
16219 }
16220 else
16221 {
16222 /* Should we also complain about unhandled address classes? */
16223 }
16224 }
16225
16226 type->set_length (byte_size);
16227 set_type_align (type, alignment);
16228 return set_die_type (die, type, cu);
16229 }
16230
16231 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16232 the user defined type vector. */
16233
16234 static struct type *
16235 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16236 {
16237 struct type *type;
16238 struct type *to_type;
16239 struct type *domain;
16240
16241 to_type = die_type (die, cu);
16242 domain = die_containing_type (die, cu);
16243
16244 /* The calls above may have already set the type for this DIE. */
16245 type = get_die_type (die, cu);
16246 if (type)
16247 return type;
16248
16249 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16250 type = lookup_methodptr_type (to_type);
16251 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16252 {
16253 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16254
16255 smash_to_method_type (new_type, domain, to_type->target_type (),
16256 to_type->fields (), to_type->num_fields (),
16257 to_type->has_varargs ());
16258 type = lookup_methodptr_type (new_type);
16259 }
16260 else
16261 type = lookup_memberptr_type (to_type, domain);
16262
16263 return set_die_type (die, type, cu);
16264 }
16265
16266 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16267 the user defined type vector. */
16268
16269 static struct type *
16270 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16271 enum type_code refcode)
16272 {
16273 struct comp_unit_head *cu_header = &cu->header;
16274 struct type *type, *target_type;
16275 struct attribute *attr;
16276
16277 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16278
16279 target_type = die_type (die, cu);
16280
16281 /* The die_type call above may have already set the type for this DIE. */
16282 type = get_die_type (die, cu);
16283 if (type)
16284 return type;
16285
16286 type = lookup_reference_type (target_type, refcode);
16287 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16288 if (attr != nullptr)
16289 type->set_length (attr->constant_value (cu_header->addr_size));
16290 else
16291 type->set_length (cu_header->addr_size);
16292
16293 maybe_set_alignment (cu, die, type);
16294 return set_die_type (die, type, cu);
16295 }
16296
16297 /* Add the given cv-qualifiers to the element type of the array. GCC
16298 outputs DWARF type qualifiers that apply to an array, not the
16299 element type. But GDB relies on the array element type to carry
16300 the cv-qualifiers. This mimics section 6.7.3 of the C99
16301 specification. */
16302
16303 static struct type *
16304 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16305 struct type *base_type, int cnst, int voltl)
16306 {
16307 struct type *el_type, *inner_array;
16308
16309 base_type = copy_type (base_type);
16310 inner_array = base_type;
16311
16312 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
16313 {
16314 inner_array->set_target_type (copy_type (inner_array->target_type ()));
16315 inner_array = inner_array->target_type ();
16316 }
16317
16318 el_type = inner_array->target_type ();
16319 cnst |= TYPE_CONST (el_type);
16320 voltl |= TYPE_VOLATILE (el_type);
16321 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
16322
16323 return set_die_type (die, base_type, cu);
16324 }
16325
16326 static struct type *
16327 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16328 {
16329 struct type *base_type, *cv_type;
16330
16331 base_type = die_type (die, cu);
16332
16333 /* The die_type call above may have already set the type for this DIE. */
16334 cv_type = get_die_type (die, cu);
16335 if (cv_type)
16336 return cv_type;
16337
16338 /* In case the const qualifier is applied to an array type, the element type
16339 is so qualified, not the array type (section 6.7.3 of C99). */
16340 if (base_type->code () == TYPE_CODE_ARRAY)
16341 return add_array_cv_type (die, cu, base_type, 1, 0);
16342
16343 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16344 return set_die_type (die, cv_type, cu);
16345 }
16346
16347 static struct type *
16348 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16349 {
16350 struct type *base_type, *cv_type;
16351
16352 base_type = die_type (die, cu);
16353
16354 /* The die_type call above may have already set the type for this DIE. */
16355 cv_type = get_die_type (die, cu);
16356 if (cv_type)
16357 return cv_type;
16358
16359 /* In case the volatile qualifier is applied to an array type, the
16360 element type is so qualified, not the array type (section 6.7.3
16361 of C99). */
16362 if (base_type->code () == TYPE_CODE_ARRAY)
16363 return add_array_cv_type (die, cu, base_type, 0, 1);
16364
16365 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16366 return set_die_type (die, cv_type, cu);
16367 }
16368
16369 /* Handle DW_TAG_restrict_type. */
16370
16371 static struct type *
16372 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16373 {
16374 struct type *base_type, *cv_type;
16375
16376 base_type = die_type (die, cu);
16377
16378 /* The die_type call above may have already set the type for this DIE. */
16379 cv_type = get_die_type (die, cu);
16380 if (cv_type)
16381 return cv_type;
16382
16383 cv_type = make_restrict_type (base_type);
16384 return set_die_type (die, cv_type, cu);
16385 }
16386
16387 /* Handle DW_TAG_atomic_type. */
16388
16389 static struct type *
16390 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16391 {
16392 struct type *base_type, *cv_type;
16393
16394 base_type = die_type (die, cu);
16395
16396 /* The die_type call above may have already set the type for this DIE. */
16397 cv_type = get_die_type (die, cu);
16398 if (cv_type)
16399 return cv_type;
16400
16401 cv_type = make_atomic_type (base_type);
16402 return set_die_type (die, cv_type, cu);
16403 }
16404
16405 /* Extract all information from a DW_TAG_string_type DIE and add to
16406 the user defined type vector. It isn't really a user defined type,
16407 but it behaves like one, with other DIE's using an AT_user_def_type
16408 attribute to reference it. */
16409
16410 static struct type *
16411 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16412 {
16413 struct objfile *objfile = cu->per_objfile->objfile;
16414 struct gdbarch *gdbarch = objfile->arch ();
16415 struct type *type, *range_type, *index_type, *char_type;
16416 struct attribute *attr;
16417 struct dynamic_prop prop;
16418 bool length_is_constant = true;
16419 LONGEST length;
16420
16421 /* There are a couple of places where bit sizes might be made use of
16422 when parsing a DW_TAG_string_type, however, no producer that we know
16423 of make use of these. Handling bit sizes that are a multiple of the
16424 byte size is easy enough, but what about other bit sizes? Lets deal
16425 with that problem when we have to. Warn about these attributes being
16426 unsupported, then parse the type and ignore them like we always
16427 have. */
16428 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16429 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16430 {
16431 static bool warning_printed = false;
16432 if (!warning_printed)
16433 {
16434 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16435 "currently supported on DW_TAG_string_type."));
16436 warning_printed = true;
16437 }
16438 }
16439
16440 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16441 if (attr != nullptr && !attr->form_is_constant ())
16442 {
16443 /* The string length describes the location at which the length of
16444 the string can be found. The size of the length field can be
16445 specified with one of the attributes below. */
16446 struct type *prop_type;
16447 struct attribute *len
16448 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16449 if (len == nullptr)
16450 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16451 if (len != nullptr && len->form_is_constant ())
16452 {
16453 /* Pass 0 as the default as we know this attribute is constant
16454 and the default value will not be returned. */
16455 LONGEST sz = len->constant_value (0);
16456 prop_type = objfile_int_type (objfile, sz, true);
16457 }
16458 else
16459 {
16460 /* If the size is not specified then we assume it is the size of
16461 an address on this target. */
16462 prop_type = cu->addr_sized_int_type (true);
16463 }
16464
16465 /* Convert the attribute into a dynamic property. */
16466 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16467 length = 1;
16468 else
16469 length_is_constant = false;
16470 }
16471 else if (attr != nullptr)
16472 {
16473 /* This DW_AT_string_length just contains the length with no
16474 indirection. There's no need to create a dynamic property in this
16475 case. Pass 0 for the default value as we know it will not be
16476 returned in this case. */
16477 length = attr->constant_value (0);
16478 }
16479 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16480 {
16481 /* We don't currently support non-constant byte sizes for strings. */
16482 length = attr->constant_value (1);
16483 }
16484 else
16485 {
16486 /* Use 1 as a fallback length if we have nothing else. */
16487 length = 1;
16488 }
16489
16490 index_type = objfile_type (objfile)->builtin_int;
16491 if (length_is_constant)
16492 range_type = create_static_range_type (NULL, index_type, 1, length);
16493 else
16494 {
16495 struct dynamic_prop low_bound;
16496
16497 low_bound.set_const_val (1);
16498 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16499 }
16500 char_type = language_string_char_type (cu->language_defn, gdbarch);
16501 type = create_string_type (NULL, char_type, range_type);
16502
16503 return set_die_type (die, type, cu);
16504 }
16505
16506 /* Assuming that DIE corresponds to a function, returns nonzero
16507 if the function is prototyped. */
16508
16509 static int
16510 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16511 {
16512 struct attribute *attr;
16513
16514 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16515 if (attr && attr->as_boolean ())
16516 return 1;
16517
16518 /* The DWARF standard implies that the DW_AT_prototyped attribute
16519 is only meaningful for C, but the concept also extends to other
16520 languages that allow unprototyped functions (Eg: Objective C).
16521 For all other languages, assume that functions are always
16522 prototyped. */
16523 if (cu->lang () != language_c
16524 && cu->lang () != language_objc
16525 && cu->lang () != language_opencl)
16526 return 1;
16527
16528 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16529 prototyped and unprototyped functions; default to prototyped,
16530 since that is more common in modern code (and RealView warns
16531 about unprototyped functions). */
16532 if (producer_is_realview (cu->producer))
16533 return 1;
16534
16535 return 0;
16536 }
16537
16538 /* Handle DIES due to C code like:
16539
16540 struct foo
16541 {
16542 int (*funcp)(int a, long l);
16543 int b;
16544 };
16545
16546 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16547
16548 static struct type *
16549 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16550 {
16551 struct objfile *objfile = cu->per_objfile->objfile;
16552 struct type *type; /* Type that this function returns. */
16553 struct type *ftype; /* Function that returns above type. */
16554 struct attribute *attr;
16555
16556 type = die_type (die, cu);
16557
16558 /* The die_type call above may have already set the type for this DIE. */
16559 ftype = get_die_type (die, cu);
16560 if (ftype)
16561 return ftype;
16562
16563 ftype = lookup_function_type (type);
16564
16565 if (prototyped_function_p (die, cu))
16566 ftype->set_is_prototyped (true);
16567
16568 /* Store the calling convention in the type if it's available in
16569 the subroutine die. Otherwise set the calling convention to
16570 the default value DW_CC_normal. */
16571 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16572 if (attr != nullptr
16573 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
16574 TYPE_CALLING_CONVENTION (ftype)
16575 = (enum dwarf_calling_convention) attr->constant_value (0);
16576 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16577 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16578 else
16579 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16580
16581 /* Record whether the function returns normally to its caller or not
16582 if the DWARF producer set that information. */
16583 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16584 if (attr && attr->as_boolean ())
16585 TYPE_NO_RETURN (ftype) = 1;
16586
16587 /* We need to add the subroutine type to the die immediately so
16588 we don't infinitely recurse when dealing with parameters
16589 declared as the same subroutine type. */
16590 set_die_type (die, ftype, cu);
16591
16592 if (die->child != NULL)
16593 {
16594 struct type *void_type = objfile_type (objfile)->builtin_void;
16595 struct die_info *child_die;
16596 int nparams, iparams;
16597
16598 /* Count the number of parameters.
16599 FIXME: GDB currently ignores vararg functions, but knows about
16600 vararg member functions. */
16601 nparams = 0;
16602 child_die = die->child;
16603 while (child_die && child_die->tag)
16604 {
16605 if (child_die->tag == DW_TAG_formal_parameter)
16606 nparams++;
16607 else if (child_die->tag == DW_TAG_unspecified_parameters)
16608 ftype->set_has_varargs (true);
16609
16610 child_die = child_die->sibling;
16611 }
16612
16613 /* Allocate storage for parameters and fill them in. */
16614 ftype->set_num_fields (nparams);
16615 ftype->set_fields
16616 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
16617
16618 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16619 even if we error out during the parameters reading below. */
16620 for (iparams = 0; iparams < nparams; iparams++)
16621 ftype->field (iparams).set_type (void_type);
16622
16623 iparams = 0;
16624 child_die = die->child;
16625 while (child_die && child_die->tag)
16626 {
16627 if (child_die->tag == DW_TAG_formal_parameter)
16628 {
16629 struct type *arg_type;
16630
16631 /* DWARF version 2 has no clean way to discern C++
16632 static and non-static member functions. G++ helps
16633 GDB by marking the first parameter for non-static
16634 member functions (which is the this pointer) as
16635 artificial. We pass this information to
16636 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16637
16638 DWARF version 3 added DW_AT_object_pointer, which GCC
16639 4.5 does not yet generate. */
16640 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16641 if (attr != nullptr)
16642 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
16643 else
16644 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16645 arg_type = die_type (child_die, cu);
16646
16647 /* RealView does not mark THIS as const, which the testsuite
16648 expects. GCC marks THIS as const in method definitions,
16649 but not in the class specifications (GCC PR 43053). */
16650 if (cu->lang () == language_cplus
16651 && !TYPE_CONST (arg_type)
16652 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16653 {
16654 int is_this = 0;
16655 struct dwarf2_cu *arg_cu = cu;
16656 const char *name = dwarf2_name (child_die, cu);
16657
16658 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16659 if (attr != nullptr)
16660 {
16661 /* If the compiler emits this, use it. */
16662 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16663 is_this = 1;
16664 }
16665 else if (name && strcmp (name, "this") == 0)
16666 /* Function definitions will have the argument names. */
16667 is_this = 1;
16668 else if (name == NULL && iparams == 0)
16669 /* Declarations may not have the names, so like
16670 elsewhere in GDB, assume an artificial first
16671 argument is "this". */
16672 is_this = 1;
16673
16674 if (is_this)
16675 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16676 arg_type, 0);
16677 }
16678
16679 ftype->field (iparams).set_type (arg_type);
16680 iparams++;
16681 }
16682 child_die = child_die->sibling;
16683 }
16684 }
16685
16686 return ftype;
16687 }
16688
16689 static struct type *
16690 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16691 {
16692 struct objfile *objfile = cu->per_objfile->objfile;
16693 const char *name = NULL;
16694 struct type *this_type, *target_type;
16695
16696 name = dwarf2_full_name (NULL, die, cu);
16697 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16698 this_type->set_target_is_stub (true);
16699 set_die_type (die, this_type, cu);
16700 target_type = die_type (die, cu);
16701 if (target_type != this_type)
16702 this_type->set_target_type (target_type);
16703 else
16704 {
16705 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16706 spec and cause infinite loops in GDB. */
16707 complaint (_("Self-referential DW_TAG_typedef "
16708 "- DIE at %s [in module %s]"),
16709 sect_offset_str (die->sect_off), objfile_name (objfile));
16710 this_type->set_target_type (nullptr);
16711 }
16712 if (name == NULL)
16713 {
16714 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16715 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16716 Handle these by just returning the target type, rather than
16717 constructing an anonymous typedef type and trying to handle this
16718 elsewhere. */
16719 set_die_type (die, target_type, cu);
16720 return target_type;
16721 }
16722 return this_type;
16723 }
16724
16725 /* Helper for get_dwarf2_rational_constant that computes the value of
16726 a given gmp_mpz given an attribute. */
16727
16728 static void
16729 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
16730 {
16731 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16732 location expression that pushes an implicit value. */
16733 if (attr->form == DW_FORM_exprloc)
16734 {
16735 dwarf_block *blk = attr->as_block ();
16736 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
16737 {
16738 uint64_t len;
16739 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
16740 blk->data + blk->size,
16741 &len);
16742 if (ptr - blk->data + len <= blk->size)
16743 {
16744 mpz_import (value->val, len,
16745 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
16746 ? 1 : -1,
16747 1, 0, 0, ptr);
16748 return;
16749 }
16750 }
16751
16752 /* On failure set it to 1. */
16753 *value = gdb_mpz (1);
16754 }
16755 else if (attr->form_is_block ())
16756 {
16757 dwarf_block *blk = attr->as_block ();
16758 mpz_import (value->val, blk->size,
16759 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
16760 ? 1 : -1,
16761 1, 0, 0, blk->data);
16762 }
16763 else
16764 *value = gdb_mpz (attr->constant_value (1));
16765 }
16766
16767 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16768 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16769
16770 If the numerator and/or numerator attribute is missing,
16771 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16772 untouched. */
16773
16774 static void
16775 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
16776 gdb_mpz *numerator, gdb_mpz *denominator)
16777 {
16778 struct attribute *num_attr, *denom_attr;
16779
16780 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
16781 if (num_attr == nullptr)
16782 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16783 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16784
16785 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
16786 if (denom_attr == nullptr)
16787 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16788 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16789
16790 if (num_attr == nullptr || denom_attr == nullptr)
16791 return;
16792
16793 get_mpz (cu, numerator, num_attr);
16794 get_mpz (cu, denominator, denom_attr);
16795 }
16796
16797 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16798 rational constant, rather than a signed one.
16799
16800 If the rational constant has a negative value, a complaint
16801 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16802
16803 static void
16804 get_dwarf2_unsigned_rational_constant (struct die_info *die,
16805 struct dwarf2_cu *cu,
16806 gdb_mpz *numerator,
16807 gdb_mpz *denominator)
16808 {
16809 gdb_mpz num (1);
16810 gdb_mpz denom (1);
16811
16812 get_dwarf2_rational_constant (die, cu, &num, &denom);
16813 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
16814 {
16815 mpz_neg (num.val, num.val);
16816 mpz_neg (denom.val, denom.val);
16817 }
16818 else if (mpz_sgn (num.val) == -1)
16819 {
16820 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16821 " in DIE at %s"),
16822 sect_offset_str (die->sect_off));
16823 return;
16824 }
16825 else if (mpz_sgn (denom.val) == -1)
16826 {
16827 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16828 " in DIE at %s"),
16829 sect_offset_str (die->sect_off));
16830 return;
16831 }
16832
16833 *numerator = std::move (num);
16834 *denominator = std::move (denom);
16835 }
16836
16837 /* Assuming that ENCODING is a string whose contents starting at the
16838 K'th character is "_nn" where "nn" is a decimal number, scan that
16839 number and set RESULT to the value. K is updated to point to the
16840 character immediately following the number.
16841
16842 If the string does not conform to the format described above, false
16843 is returned, and K may or may not be changed. */
16844
16845 static bool
16846 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
16847 {
16848 /* The next character should be an underscore ('_') followed
16849 by a digit. */
16850 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
16851 return false;
16852
16853 /* Skip the underscore. */
16854 k++;
16855 int start = k;
16856
16857 /* Determine the number of digits for our number. */
16858 while (isdigit (encoding[k]))
16859 k++;
16860 if (k == start)
16861 return false;
16862
16863 std::string copy (&encoding[start], k - start);
16864 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
16865 return false;
16866
16867 return true;
16868 }
16869
16870 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16871 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16872 DENOM, update OFFSET, and return true on success. Return false on
16873 failure. */
16874
16875 static bool
16876 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
16877 gdb_mpz *num, gdb_mpz *denom)
16878 {
16879 if (!ada_get_gnat_encoded_number (encoding, offset, num))
16880 return false;
16881 return ada_get_gnat_encoded_number (encoding, offset, denom);
16882 }
16883
16884 /* Assuming DIE corresponds to a fixed point type, finish the creation
16885 of the corresponding TYPE by setting its type-specific data. CU is
16886 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16887 encodings. It is nullptr if the GNAT encoding should be
16888 ignored. */
16889
16890 static void
16891 finish_fixed_point_type (struct type *type, const char *suffix,
16892 struct die_info *die, struct dwarf2_cu *cu)
16893 {
16894 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
16895 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
16896
16897 /* If GNAT encodings are preferred, don't examine the
16898 attributes. */
16899 struct attribute *attr = nullptr;
16900 if (suffix == nullptr)
16901 {
16902 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
16903 if (attr == nullptr)
16904 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
16905 if (attr == nullptr)
16906 attr = dwarf2_attr (die, DW_AT_small, cu);
16907 }
16908
16909 /* Numerator and denominator of our fixed-point type's scaling factor.
16910 The default is a scaling factor of 1, which we use as a fallback
16911 when we are not able to decode it (problem with the debugging info,
16912 unsupported forms, bug in GDB, etc...). Using that as the default
16913 allows us to at least print the unscaled value, which might still
16914 be useful to a user. */
16915 gdb_mpz scale_num (1);
16916 gdb_mpz scale_denom (1);
16917
16918 if (attr == nullptr)
16919 {
16920 int offset = 0;
16921 if (suffix != nullptr
16922 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16923 &scale_denom)
16924 /* The number might be encoded as _nn_dd_nn_dd, where the
16925 second ratio is the 'small value. In this situation, we
16926 want the second value. */
16927 && (suffix[offset] != '_'
16928 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16929 &scale_denom)))
16930 {
16931 /* Found it. */
16932 }
16933 else
16934 {
16935 /* Scaling factor not found. Assume a scaling factor of 1,
16936 and hope for the best. At least the user will be able to
16937 see the encoded value. */
16938 scale_num = 1;
16939 scale_denom = 1;
16940 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16941 sect_offset_str (die->sect_off));
16942 }
16943 }
16944 else if (attr->name == DW_AT_binary_scale)
16945 {
16946 LONGEST scale_exp = attr->constant_value (0);
16947 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16948
16949 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
16950 }
16951 else if (attr->name == DW_AT_decimal_scale)
16952 {
16953 LONGEST scale_exp = attr->constant_value (0);
16954 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16955
16956 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
16957 }
16958 else if (attr->name == DW_AT_small)
16959 {
16960 struct die_info *scale_die;
16961 struct dwarf2_cu *scale_cu = cu;
16962
16963 scale_die = follow_die_ref (die, attr, &scale_cu);
16964 if (scale_die->tag == DW_TAG_constant)
16965 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
16966 &scale_num, &scale_denom);
16967 else
16968 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16969 " (DIE at %s)"),
16970 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16971 }
16972 else
16973 {
16974 complaint (_("unsupported scale attribute %s for fixed-point type"
16975 " (DIE at %s)"),
16976 dwarf_attr_name (attr->name),
16977 sect_offset_str (die->sect_off));
16978 }
16979
16980 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
16981 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
16982 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
16983 mpq_canonicalize (scaling_factor.val);
16984 }
16985
16986 /* The gnat-encoding suffix for fixed point. */
16987
16988 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16989
16990 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16991 "XF" suffix of the name. The text after this is what encodes the
16992 'small and 'delta information. Otherwise, return nullptr. */
16993
16994 static const char *
16995 gnat_encoded_fixed_point_type_info (const char *name)
16996 {
16997 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
16998 }
16999
17000 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17001 (which may be different from NAME) to the architecture back-end to allow
17002 it to guess the correct format if necessary. */
17003
17004 static struct type *
17005 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17006 const char *name_hint, enum bfd_endian byte_order)
17007 {
17008 struct gdbarch *gdbarch = objfile->arch ();
17009 const struct floatformat **format;
17010 struct type *type;
17011
17012 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17013 if (format)
17014 type = init_float_type (objfile, bits, name, format, byte_order);
17015 else
17016 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17017
17018 return type;
17019 }
17020
17021 /* Allocate an integer type of size BITS and name NAME. */
17022
17023 static struct type *
17024 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17025 int bits, int unsigned_p, const char *name)
17026 {
17027 struct type *type;
17028
17029 /* Versions of Intel's C Compiler generate an integer type called "void"
17030 instead of using DW_TAG_unspecified_type. This has been seen on
17031 at least versions 14, 17, and 18. */
17032 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17033 && strcmp (name, "void") == 0)
17034 type = objfile_type (objfile)->builtin_void;
17035 else
17036 type = init_integer_type (objfile, bits, unsigned_p, name);
17037
17038 return type;
17039 }
17040
17041 /* Return true if DIE has a DW_AT_small attribute whose value is
17042 a constant rational, where both the numerator and denominator
17043 are equal to zero.
17044
17045 CU is the DIE's Compilation Unit. */
17046
17047 static bool
17048 has_zero_over_zero_small_attribute (struct die_info *die,
17049 struct dwarf2_cu *cu)
17050 {
17051 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17052 if (attr == nullptr)
17053 return false;
17054
17055 struct dwarf2_cu *scale_cu = cu;
17056 struct die_info *scale_die
17057 = follow_die_ref (die, attr, &scale_cu);
17058
17059 if (scale_die->tag != DW_TAG_constant)
17060 return false;
17061
17062 gdb_mpz num (1), denom (1);
17063 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
17064 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
17065 }
17066
17067 /* Initialise and return a floating point type of size BITS suitable for
17068 use as a component of a complex number. The NAME_HINT is passed through
17069 when initialising the floating point type and is the name of the complex
17070 type.
17071
17072 As DWARF doesn't currently provide an explicit name for the components
17073 of a complex number, but it can be helpful to have these components
17074 named, we try to select a suitable name based on the size of the
17075 component. */
17076 static struct type *
17077 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17078 struct objfile *objfile,
17079 int bits, const char *name_hint,
17080 enum bfd_endian byte_order)
17081 {
17082 gdbarch *gdbarch = objfile->arch ();
17083 struct type *tt = nullptr;
17084
17085 /* Try to find a suitable floating point builtin type of size BITS.
17086 We're going to use the name of this type as the name for the complex
17087 target type that we are about to create. */
17088 switch (cu->lang ())
17089 {
17090 case language_fortran:
17091 switch (bits)
17092 {
17093 case 32:
17094 tt = builtin_f_type (gdbarch)->builtin_real;
17095 break;
17096 case 64:
17097 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17098 break;
17099 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17100 case 128:
17101 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17102 break;
17103 }
17104 break;
17105 default:
17106 switch (bits)
17107 {
17108 case 32:
17109 tt = builtin_type (gdbarch)->builtin_float;
17110 break;
17111 case 64:
17112 tt = builtin_type (gdbarch)->builtin_double;
17113 break;
17114 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17115 case 128:
17116 tt = builtin_type (gdbarch)->builtin_long_double;
17117 break;
17118 }
17119 break;
17120 }
17121
17122 /* If the type we found doesn't match the size we were looking for, then
17123 pretend we didn't find a type at all, the complex target type we
17124 create will then be nameless. */
17125 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
17126 tt = nullptr;
17127
17128 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17129 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17130 }
17131
17132 /* Find a representation of a given base type and install
17133 it in the TYPE field of the die. */
17134
17135 static struct type *
17136 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17137 {
17138 struct objfile *objfile = cu->per_objfile->objfile;
17139 struct type *type;
17140 struct attribute *attr;
17141 int encoding = 0, bits = 0;
17142 const char *name;
17143 gdbarch *arch;
17144
17145 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17146 if (attr != nullptr && attr->form_is_constant ())
17147 encoding = attr->constant_value (0);
17148 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17149 if (attr != nullptr)
17150 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17151 name = dwarf2_name (die, cu);
17152 if (!name)
17153 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17154
17155 arch = objfile->arch ();
17156 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17157
17158 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17159 if (attr != nullptr && attr->form_is_constant ())
17160 {
17161 int endianity = attr->constant_value (0);
17162
17163 switch (endianity)
17164 {
17165 case DW_END_big:
17166 byte_order = BFD_ENDIAN_BIG;
17167 break;
17168 case DW_END_little:
17169 byte_order = BFD_ENDIAN_LITTLE;
17170 break;
17171 default:
17172 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17173 break;
17174 }
17175 }
17176
17177 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
17178 && cu->lang () == language_ada
17179 && has_zero_over_zero_small_attribute (die, cu))
17180 {
17181 /* brobecker/2018-02-24: This is a fixed point type for which
17182 the scaling factor is represented as fraction whose value
17183 does not make sense (zero divided by zero), so we should
17184 normally never see these. However, there is a small category
17185 of fixed point types for which GNAT is unable to provide
17186 the scaling factor via the standard DWARF mechanisms, and
17187 for which the info is provided via the GNAT encodings instead.
17188 This is likely what this DIE is about. */
17189 encoding = (encoding == DW_ATE_signed_fixed
17190 ? DW_ATE_signed
17191 : DW_ATE_unsigned);
17192 }
17193
17194 /* With GNAT encodings, fixed-point information will be encoded in
17195 the type name. Note that this can also occur with the above
17196 zero-over-zero case, which is why this is a separate "if" rather
17197 than an "else if". */
17198 const char *gnat_encoding_suffix = nullptr;
17199 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
17200 && cu->lang () == language_ada
17201 && name != nullptr)
17202 {
17203 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
17204 if (gnat_encoding_suffix != nullptr)
17205 {
17206 gdb_assert (startswith (gnat_encoding_suffix,
17207 GNAT_FIXED_POINT_SUFFIX));
17208 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
17209 name, gnat_encoding_suffix - name);
17210 /* Use -1 here so that SUFFIX points at the "_" after the
17211 "XF". */
17212 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
17213
17214 encoding = (encoding == DW_ATE_signed
17215 ? DW_ATE_signed_fixed
17216 : DW_ATE_unsigned_fixed);
17217 }
17218 }
17219
17220 switch (encoding)
17221 {
17222 case DW_ATE_address:
17223 /* Turn DW_ATE_address into a void * pointer. */
17224 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17225 type = init_pointer_type (objfile, bits, name, type);
17226 break;
17227 case DW_ATE_boolean:
17228 type = init_boolean_type (objfile, bits, 1, name);
17229 break;
17230 case DW_ATE_complex_float:
17231 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17232 byte_order);
17233 if (type->code () == TYPE_CODE_ERROR)
17234 {
17235 if (name == nullptr)
17236 {
17237 struct obstack *obstack
17238 = &cu->per_objfile->objfile->objfile_obstack;
17239 name = obconcat (obstack, "_Complex ", type->name (),
17240 nullptr);
17241 }
17242 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17243 }
17244 else
17245 type = init_complex_type (name, type);
17246 break;
17247 case DW_ATE_decimal_float:
17248 type = init_decfloat_type (objfile, bits, name);
17249 break;
17250 case DW_ATE_float:
17251 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17252 break;
17253 case DW_ATE_signed:
17254 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17255 break;
17256 case DW_ATE_unsigned:
17257 if (cu->lang () == language_fortran
17258 && name
17259 && startswith (name, "character("))
17260 type = init_character_type (objfile, bits, 1, name);
17261 else
17262 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17263 break;
17264 case DW_ATE_signed_char:
17265 if (cu->lang () == language_ada
17266 || cu->lang () == language_m2
17267 || cu->lang () == language_pascal
17268 || cu->lang () == language_fortran)
17269 type = init_character_type (objfile, bits, 0, name);
17270 else
17271 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17272 break;
17273 case DW_ATE_unsigned_char:
17274 if (cu->lang () == language_ada
17275 || cu->lang () == language_m2
17276 || cu->lang () == language_pascal
17277 || cu->lang () == language_fortran
17278 || cu->lang () == language_rust)
17279 type = init_character_type (objfile, bits, 1, name);
17280 else
17281 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17282 break;
17283 case DW_ATE_UTF:
17284 {
17285 type = init_character_type (objfile, bits, 1, name);
17286 return set_die_type (die, type, cu);
17287 }
17288 break;
17289 case DW_ATE_signed_fixed:
17290 type = init_fixed_point_type (objfile, bits, 0, name);
17291 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17292 break;
17293 case DW_ATE_unsigned_fixed:
17294 type = init_fixed_point_type (objfile, bits, 1, name);
17295 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17296 break;
17297
17298 default:
17299 complaint (_("unsupported DW_AT_encoding: '%s'"),
17300 dwarf_type_encoding_name (encoding));
17301 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17302 break;
17303 }
17304
17305 if (type->code () == TYPE_CODE_INT
17306 && name != nullptr
17307 && strcmp (name, "char") == 0)
17308 type->set_has_no_signedness (true);
17309
17310 maybe_set_alignment (cu, die, type);
17311
17312 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
17313
17314 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
17315 {
17316 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
17317 if (attr != nullptr && attr->as_unsigned () <= 8 * type->length ())
17318 {
17319 unsigned real_bit_size = attr->as_unsigned ();
17320 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
17321 /* Only use the attributes if they make sense together. */
17322 if (attr == nullptr
17323 || (attr->as_unsigned () + real_bit_size
17324 <= 8 * type->length ()))
17325 {
17326 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
17327 = real_bit_size;
17328 if (attr != nullptr)
17329 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
17330 = attr->as_unsigned ();
17331 }
17332 }
17333 }
17334
17335 return set_die_type (die, type, cu);
17336 }
17337
17338 /* A helper function that returns the name of DIE, if it refers to a
17339 variable declaration. */
17340
17341 static const char *
17342 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
17343 {
17344 if (die->tag != DW_TAG_variable)
17345 return nullptr;
17346
17347 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
17348 if (attr == nullptr || !attr->as_boolean ())
17349 return nullptr;
17350
17351 attr = dwarf2_attr (die, DW_AT_name, cu);
17352 if (attr == nullptr)
17353 return nullptr;
17354 return attr->as_string ();
17355 }
17356
17357 /* Parse dwarf attribute if it's a block, reference or constant and put the
17358 resulting value of the attribute into struct bound_prop.
17359 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17360
17361 static int
17362 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17363 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17364 struct type *default_type)
17365 {
17366 struct dwarf2_property_baton *baton;
17367 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17368 struct objfile *objfile = per_objfile->objfile;
17369 struct obstack *obstack = &objfile->objfile_obstack;
17370
17371 gdb_assert (default_type != NULL);
17372
17373 if (attr == NULL || prop == NULL)
17374 return 0;
17375
17376 if (attr->form_is_block ())
17377 {
17378 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17379 baton->property_type = default_type;
17380 baton->locexpr.per_cu = cu->per_cu;
17381 baton->locexpr.per_objfile = per_objfile;
17382
17383 struct dwarf_block *block;
17384 if (attr->form == DW_FORM_data16)
17385 {
17386 size_t data_size = 16;
17387 block = XOBNEW (obstack, struct dwarf_block);
17388 block->size = (data_size
17389 + 2 /* Extra bytes for DW_OP and arg. */);
17390 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
17391 data[0] = DW_OP_implicit_value;
17392 data[1] = data_size;
17393 memcpy (&data[2], attr->as_block ()->data, data_size);
17394 block->data = data;
17395 }
17396 else
17397 block = attr->as_block ();
17398
17399 baton->locexpr.size = block->size;
17400 baton->locexpr.data = block->data;
17401 switch (attr->name)
17402 {
17403 case DW_AT_string_length:
17404 baton->locexpr.is_reference = true;
17405 break;
17406 default:
17407 baton->locexpr.is_reference = false;
17408 break;
17409 }
17410
17411 prop->set_locexpr (baton);
17412 gdb_assert (prop->baton () != NULL);
17413 }
17414 else if (attr->form_is_ref ())
17415 {
17416 struct dwarf2_cu *target_cu = cu;
17417 struct die_info *target_die;
17418 struct attribute *target_attr;
17419
17420 target_die = follow_die_ref (die, attr, &target_cu);
17421 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17422 if (target_attr == NULL)
17423 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17424 target_cu);
17425 if (target_attr == nullptr)
17426 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
17427 target_cu);
17428 if (target_attr == NULL)
17429 {
17430 const char *name = var_decl_name (target_die, target_cu);
17431 if (name != nullptr)
17432 {
17433 prop->set_variable_name (name);
17434 return 1;
17435 }
17436 return 0;
17437 }
17438
17439 switch (target_attr->name)
17440 {
17441 case DW_AT_location:
17442 if (target_attr->form_is_section_offset ())
17443 {
17444 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17445 baton->property_type = die_type (target_die, target_cu);
17446 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17447 prop->set_loclist (baton);
17448 gdb_assert (prop->baton () != NULL);
17449 }
17450 else if (target_attr->form_is_block ())
17451 {
17452 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17453 baton->property_type = die_type (target_die, target_cu);
17454 baton->locexpr.per_cu = cu->per_cu;
17455 baton->locexpr.per_objfile = per_objfile;
17456 struct dwarf_block *block = target_attr->as_block ();
17457 baton->locexpr.size = block->size;
17458 baton->locexpr.data = block->data;
17459 baton->locexpr.is_reference = true;
17460 prop->set_locexpr (baton);
17461 gdb_assert (prop->baton () != NULL);
17462 }
17463 else
17464 {
17465 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17466 "dynamic property");
17467 return 0;
17468 }
17469 break;
17470 case DW_AT_data_member_location:
17471 case DW_AT_data_bit_offset:
17472 {
17473 LONGEST offset;
17474
17475 if (!handle_member_location (target_die, target_cu, &offset))
17476 return 0;
17477
17478 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17479 baton->property_type = read_type_die (target_die->parent,
17480 target_cu);
17481 baton->offset_info.offset = offset;
17482 baton->offset_info.type = die_type (target_die, target_cu);
17483 prop->set_addr_offset (baton);
17484 break;
17485 }
17486 }
17487 }
17488 else if (attr->form_is_constant ())
17489 prop->set_const_val (attr->constant_value (0));
17490 else if (attr->form_is_section_offset ())
17491 {
17492 switch (attr->name)
17493 {
17494 case DW_AT_string_length:
17495 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17496 baton->property_type = default_type;
17497 fill_in_loclist_baton (cu, &baton->loclist, attr);
17498 prop->set_loclist (baton);
17499 gdb_assert (prop->baton () != NULL);
17500 break;
17501 default:
17502 goto invalid;
17503 }
17504 }
17505 else
17506 goto invalid;
17507
17508 return 1;
17509
17510 invalid:
17511 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17512 dwarf2_name (die, cu));
17513 return 0;
17514 }
17515
17516 /* See read.h. */
17517
17518 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17519 present (which is valid) then compute the default type based on the
17520 compilation units address size. */
17521
17522 static struct type *
17523 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17524 {
17525 struct type *index_type = die_type (die, cu);
17526
17527 /* Dwarf-2 specifications explicitly allows to create subrange types
17528 without specifying a base type.
17529 In that case, the base type must be set to the type of
17530 the lower bound, upper bound or count, in that order, if any of these
17531 three attributes references an object that has a type.
17532 If no base type is found, the Dwarf-2 specifications say that
17533 a signed integer type of size equal to the size of an address should
17534 be used.
17535 For the following C code: `extern char gdb_int [];'
17536 GCC produces an empty range DIE.
17537 FIXME: muller/2010-05-28: Possible references to object for low bound,
17538 high bound or count are not yet handled by this code. */
17539 if (index_type->code () == TYPE_CODE_VOID)
17540 index_type = cu->addr_sized_int_type (false);
17541
17542 return index_type;
17543 }
17544
17545 /* Read the given DW_AT_subrange DIE. */
17546
17547 static struct type *
17548 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17549 {
17550 struct type *base_type, *orig_base_type;
17551 struct type *range_type;
17552 struct attribute *attr;
17553 struct dynamic_prop low, high;
17554 int low_default_is_valid;
17555 int high_bound_is_count = 0;
17556 const char *name;
17557
17558 orig_base_type = read_subrange_index_type (die, cu);
17559
17560 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17561 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17562 creating the range type, but we use the result of check_typedef
17563 when examining properties of the type. */
17564 base_type = check_typedef (orig_base_type);
17565
17566 /* The die_type call above may have already set the type for this DIE. */
17567 range_type = get_die_type (die, cu);
17568 if (range_type)
17569 return range_type;
17570
17571 high.set_const_val (0);
17572
17573 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17574 omitting DW_AT_lower_bound. */
17575 switch (cu->lang ())
17576 {
17577 case language_c:
17578 case language_cplus:
17579 low.set_const_val (0);
17580 low_default_is_valid = 1;
17581 break;
17582 case language_fortran:
17583 low.set_const_val (1);
17584 low_default_is_valid = 1;
17585 break;
17586 case language_d:
17587 case language_objc:
17588 case language_rust:
17589 low.set_const_val (0);
17590 low_default_is_valid = (cu->header.version >= 4);
17591 break;
17592 case language_ada:
17593 case language_m2:
17594 case language_pascal:
17595 low.set_const_val (1);
17596 low_default_is_valid = (cu->header.version >= 4);
17597 break;
17598 default:
17599 low.set_const_val (0);
17600 low_default_is_valid = 0;
17601 break;
17602 }
17603
17604 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17605 if (attr != nullptr)
17606 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17607 else if (!low_default_is_valid)
17608 complaint (_("Missing DW_AT_lower_bound "
17609 "- DIE at %s [in module %s]"),
17610 sect_offset_str (die->sect_off),
17611 objfile_name (cu->per_objfile->objfile));
17612
17613 struct attribute *attr_ub, *attr_count;
17614 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17615 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17616 {
17617 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17618 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17619 {
17620 /* If bounds are constant do the final calculation here. */
17621 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
17622 high.set_const_val (low.const_val () + high.const_val () - 1);
17623 else
17624 high_bound_is_count = 1;
17625 }
17626 else
17627 {
17628 if (attr_ub != NULL)
17629 complaint (_("Unresolved DW_AT_upper_bound "
17630 "- DIE at %s [in module %s]"),
17631 sect_offset_str (die->sect_off),
17632 objfile_name (cu->per_objfile->objfile));
17633 if (attr_count != NULL)
17634 complaint (_("Unresolved DW_AT_count "
17635 "- DIE at %s [in module %s]"),
17636 sect_offset_str (die->sect_off),
17637 objfile_name (cu->per_objfile->objfile));
17638 }
17639 }
17640
17641 LONGEST bias = 0;
17642 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17643 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17644 bias = bias_attr->constant_value (0);
17645
17646 /* Normally, the DWARF producers are expected to use a signed
17647 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17648 But this is unfortunately not always the case, as witnessed
17649 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17650 is used instead. To work around that ambiguity, we treat
17651 the bounds as signed, and thus sign-extend their values, when
17652 the base type is signed.
17653
17654 Skip it if the base type's length is larger than ULONGEST, to avoid
17655 the undefined behavior of a too large left shift. We don't really handle
17656 constants larger than 8 bytes anyway, at the moment. */
17657
17658 if (base_type->length () <= sizeof (ULONGEST))
17659 {
17660 ULONGEST negative_mask
17661 = -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
17662
17663 if (low.kind () == PROP_CONST
17664 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
17665 low.set_const_val (low.const_val () | negative_mask);
17666
17667 if (high.kind () == PROP_CONST
17668 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
17669 high.set_const_val (high.const_val () | negative_mask);
17670 }
17671
17672 /* Check for bit and byte strides. */
17673 struct dynamic_prop byte_stride_prop;
17674 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17675 if (attr_byte_stride != nullptr)
17676 {
17677 struct type *prop_type = cu->addr_sized_int_type (false);
17678 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17679 prop_type);
17680 }
17681
17682 struct dynamic_prop bit_stride_prop;
17683 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17684 if (attr_bit_stride != nullptr)
17685 {
17686 /* It only makes sense to have either a bit or byte stride. */
17687 if (attr_byte_stride != nullptr)
17688 {
17689 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17690 "- DIE at %s [in module %s]"),
17691 sect_offset_str (die->sect_off),
17692 objfile_name (cu->per_objfile->objfile));
17693 attr_bit_stride = nullptr;
17694 }
17695 else
17696 {
17697 struct type *prop_type = cu->addr_sized_int_type (false);
17698 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17699 prop_type);
17700 }
17701 }
17702
17703 if (attr_byte_stride != nullptr
17704 || attr_bit_stride != nullptr)
17705 {
17706 bool byte_stride_p = (attr_byte_stride != nullptr);
17707 struct dynamic_prop *stride
17708 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17709
17710 range_type
17711 = create_range_type_with_stride (NULL, orig_base_type, &low,
17712 &high, bias, stride, byte_stride_p);
17713 }
17714 else
17715 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17716
17717 if (high_bound_is_count)
17718 range_type->bounds ()->flag_upper_bound_is_count = 1;
17719
17720 /* Ada expects an empty array on no boundary attributes. */
17721 if (attr == NULL && cu->lang () != language_ada)
17722 range_type->bounds ()->high.set_undefined ();
17723
17724 name = dwarf2_name (die, cu);
17725 if (name)
17726 range_type->set_name (name);
17727
17728 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17729 if (attr != nullptr)
17730 range_type->set_length (attr->constant_value (0));
17731
17732 maybe_set_alignment (cu, die, range_type);
17733
17734 set_die_type (die, range_type, cu);
17735
17736 /* set_die_type should be already done. */
17737 set_descriptive_type (range_type, die, cu);
17738
17739 return range_type;
17740 }
17741
17742 static struct type *
17743 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17744 {
17745 struct type *type;
17746
17747 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17748 type->set_name (dwarf2_name (die, cu));
17749
17750 /* In Ada, an unspecified type is typically used when the description
17751 of the type is deferred to a different unit. When encountering
17752 such a type, we treat it as a stub, and try to resolve it later on,
17753 when needed.
17754 Mark this as a stub type for all languages though. */
17755 type->set_is_stub (true);
17756
17757 return set_die_type (die, type, cu);
17758 }
17759
17760 /* Read a single die and all its descendents. Set the die's sibling
17761 field to NULL; set other fields in the die correctly, and set all
17762 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17763 location of the info_ptr after reading all of those dies. PARENT
17764 is the parent of the die in question. */
17765
17766 static struct die_info *
17767 read_die_and_children (const struct die_reader_specs *reader,
17768 const gdb_byte *info_ptr,
17769 const gdb_byte **new_info_ptr,
17770 struct die_info *parent)
17771 {
17772 struct die_info *die;
17773 const gdb_byte *cur_ptr;
17774
17775 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17776 if (die == NULL)
17777 {
17778 *new_info_ptr = cur_ptr;
17779 return NULL;
17780 }
17781 store_in_ref_table (die, reader->cu);
17782
17783 if (die->has_children)
17784 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17785 else
17786 {
17787 die->child = NULL;
17788 *new_info_ptr = cur_ptr;
17789 }
17790
17791 die->sibling = NULL;
17792 die->parent = parent;
17793 return die;
17794 }
17795
17796 /* Read a die, all of its descendents, and all of its siblings; set
17797 all of the fields of all of the dies correctly. Arguments are as
17798 in read_die_and_children. */
17799
17800 static struct die_info *
17801 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17802 const gdb_byte *info_ptr,
17803 const gdb_byte **new_info_ptr,
17804 struct die_info *parent)
17805 {
17806 struct die_info *first_die, *last_sibling;
17807 const gdb_byte *cur_ptr;
17808
17809 cur_ptr = info_ptr;
17810 first_die = last_sibling = NULL;
17811
17812 while (1)
17813 {
17814 struct die_info *die
17815 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17816
17817 if (die == NULL)
17818 {
17819 *new_info_ptr = cur_ptr;
17820 return first_die;
17821 }
17822
17823 if (!first_die)
17824 first_die = die;
17825 else
17826 last_sibling->sibling = die;
17827
17828 last_sibling = die;
17829 }
17830 }
17831
17832 /* Read a die, all of its descendents, and all of its siblings; set
17833 all of the fields of all of the dies correctly. Arguments are as
17834 in read_die_and_children.
17835 This the main entry point for reading a DIE and all its children. */
17836
17837 static struct die_info *
17838 read_die_and_siblings (const struct die_reader_specs *reader,
17839 const gdb_byte *info_ptr,
17840 const gdb_byte **new_info_ptr,
17841 struct die_info *parent)
17842 {
17843 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17844 new_info_ptr, parent);
17845
17846 if (dwarf_die_debug)
17847 {
17848 gdb_printf (gdb_stdlog,
17849 "Read die from %s@0x%x of %s:\n",
17850 reader->die_section->get_name (),
17851 (unsigned) (info_ptr - reader->die_section->buffer),
17852 bfd_get_filename (reader->abfd));
17853 die->dump (dwarf_die_debug);
17854 }
17855
17856 return die;
17857 }
17858
17859 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17860 attributes.
17861 The caller is responsible for filling in the extra attributes
17862 and updating (*DIEP)->num_attrs.
17863 Set DIEP to point to a newly allocated die with its information,
17864 except for its child, sibling, and parent fields. */
17865
17866 static const gdb_byte *
17867 read_full_die_1 (const struct die_reader_specs *reader,
17868 struct die_info **diep, const gdb_byte *info_ptr,
17869 int num_extra_attrs)
17870 {
17871 unsigned int abbrev_number, bytes_read, i;
17872 const struct abbrev_info *abbrev;
17873 struct die_info *die;
17874 struct dwarf2_cu *cu = reader->cu;
17875 bfd *abfd = reader->abfd;
17876
17877 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17878 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17879 info_ptr += bytes_read;
17880 if (!abbrev_number)
17881 {
17882 *diep = NULL;
17883 return info_ptr;
17884 }
17885
17886 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17887 if (!abbrev)
17888 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17889 abbrev_number,
17890 bfd_get_filename (abfd));
17891
17892 die = die_info::allocate (&cu->comp_unit_obstack,
17893 abbrev->num_attrs + num_extra_attrs);
17894 die->sect_off = sect_off;
17895 die->tag = abbrev->tag;
17896 die->abbrev = abbrev_number;
17897 die->has_children = abbrev->has_children;
17898
17899 /* Make the result usable.
17900 The caller needs to update num_attrs after adding the extra
17901 attributes. */
17902 die->num_attrs = abbrev->num_attrs;
17903
17904 bool any_need_reprocess = false;
17905 for (i = 0; i < abbrev->num_attrs; ++i)
17906 {
17907 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17908 info_ptr);
17909 if (die->attrs[i].requires_reprocessing_p ())
17910 any_need_reprocess = true;
17911 }
17912
17913 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17914 if (attr != nullptr && attr->form_is_unsigned ())
17915 cu->str_offsets_base = attr->as_unsigned ();
17916
17917 attr = die->attr (DW_AT_loclists_base);
17918 if (attr != nullptr)
17919 cu->loclist_base = attr->as_unsigned ();
17920
17921 auto maybe_addr_base = die->addr_base ();
17922 if (maybe_addr_base.has_value ())
17923 cu->addr_base = *maybe_addr_base;
17924
17925 attr = die->attr (DW_AT_rnglists_base);
17926 if (attr != nullptr)
17927 cu->rnglists_base = attr->as_unsigned ();
17928
17929 if (any_need_reprocess)
17930 {
17931 for (i = 0; i < abbrev->num_attrs; ++i)
17932 {
17933 if (die->attrs[i].requires_reprocessing_p ())
17934 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
17935 }
17936 }
17937 *diep = die;
17938 return info_ptr;
17939 }
17940
17941 /* Read a die and all its attributes.
17942 Set DIEP to point to a newly allocated die with its information,
17943 except for its child, sibling, and parent fields. */
17944
17945 static const gdb_byte *
17946 read_full_die (const struct die_reader_specs *reader,
17947 struct die_info **diep, const gdb_byte *info_ptr)
17948 {
17949 const gdb_byte *result;
17950
17951 result = read_full_die_1 (reader, diep, info_ptr, 0);
17952
17953 if (dwarf_die_debug)
17954 {
17955 gdb_printf (gdb_stdlog,
17956 "Read die from %s@0x%x of %s:\n",
17957 reader->die_section->get_name (),
17958 (unsigned) (info_ptr - reader->die_section->buffer),
17959 bfd_get_filename (reader->abfd));
17960 (*diep)->dump (dwarf_die_debug);
17961 }
17962
17963 return result;
17964 }
17965 \f
17966
17967 void
17968 cooked_indexer::check_bounds (cutu_reader *reader)
17969 {
17970 if (reader->cu->per_cu->addresses_seen)
17971 return;
17972
17973 dwarf2_cu *cu = reader->cu;
17974
17975 CORE_ADDR best_lowpc = 0, best_highpc = 0;
17976 /* Possibly set the default values of LOWPC and HIGHPC from
17977 `DW_AT_ranges'. */
17978 dwarf2_find_base_address (reader->comp_unit_die, cu);
17979 enum pc_bounds_kind cu_bounds_kind
17980 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
17981 cu, m_index_storage->get_addrmap (), cu->per_cu);
17982 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
17983 {
17984 struct objfile *objfile = cu->per_objfile->objfile;
17985 CORE_ADDR baseaddr = objfile->text_section_offset ();
17986 struct gdbarch *gdbarch = objfile->arch ();
17987 CORE_ADDR low
17988 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
17989 - baseaddr);
17990 CORE_ADDR high
17991 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
17992 - baseaddr - 1);
17993 /* Store the contiguous range if it is not empty; it can be
17994 empty for CUs with no code. */
17995 m_index_storage->get_addrmap ()->set_empty (low, high, cu->per_cu);
17996
17997 cu->per_cu->addresses_seen = true;
17998 }
17999 }
18000
18001 /* Helper function that returns true if TAG can have a linkage
18002 name. */
18003
18004 static bool
18005 tag_can_have_linkage_name (enum dwarf_tag tag)
18006 {
18007 switch (tag)
18008 {
18009 case DW_TAG_variable:
18010 case DW_TAG_subprogram:
18011 return true;
18012
18013 default:
18014 return false;
18015 }
18016 }
18017
18018 cutu_reader *
18019 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
18020 dwarf2_per_objfile *per_objfile,
18021 sect_offset sect_off, bool is_dwz,
18022 bool for_scanning)
18023 {
18024 /* Lookups for type unit references are always in the CU, and
18025 cross-CU references will crash. */
18026 if (reader->cu->per_cu->is_dwz == is_dwz
18027 && reader->cu->header.offset_in_cu_p (sect_off))
18028 return reader;
18029
18030 dwarf2_per_cu_data *per_cu
18031 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
18032 per_objfile->per_bfd);
18033
18034 /* When scanning, we only want to visit a given CU a single time.
18035 Doing this check here avoids self-imports as well. */
18036 if (for_scanning)
18037 {
18038 bool nope = false;
18039 if (!per_cu->scanned.compare_exchange_strong (nope, true))
18040 return nullptr;
18041 }
18042 if (per_cu == m_per_cu)
18043 return reader;
18044
18045 cutu_reader *result = m_index_storage->get_reader (per_cu);
18046 if (result == nullptr)
18047 {
18048 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
18049 m_index_storage->get_abbrev_cache ());
18050
18051 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
18052 language_minimal);
18053 std::unique_ptr<cutu_reader> copy
18054 (new cutu_reader (std::move (new_reader)));
18055 result = m_index_storage->preserve (std::move (copy));
18056 }
18057
18058 if (result->dummy_p || !result->comp_unit_die->has_children)
18059 return nullptr;
18060
18061 if (for_scanning)
18062 check_bounds (result);
18063
18064 return result;
18065 }
18066
18067 const gdb_byte *
18068 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
18069 cutu_reader *reader,
18070 const gdb_byte *watermark_ptr,
18071 const gdb_byte *info_ptr,
18072 const abbrev_info *abbrev,
18073 const char **name,
18074 const char **linkage_name,
18075 cooked_index_flag *flags,
18076 sect_offset *sibling_offset,
18077 const cooked_index_entry **parent_entry,
18078 CORE_ADDR *maybe_defer,
18079 bool for_specification)
18080 {
18081 bool origin_is_dwz = false;
18082 bool is_declaration = false;
18083 sect_offset origin_offset {};
18084
18085 gdb::optional<CORE_ADDR> low_pc;
18086 gdb::optional<CORE_ADDR> high_pc;
18087 bool high_pc_relative = false;
18088
18089 for (int i = 0; i < abbrev->num_attrs; ++i)
18090 {
18091 attribute attr;
18092 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18093 if (attr.requires_reprocessing_p ())
18094 read_attribute_reprocess (reader, &attr, abbrev->tag);
18095
18096 /* Store the data if it is of an attribute we want to keep in a
18097 partial symbol table. */
18098 switch (attr.name)
18099 {
18100 case DW_AT_name:
18101 switch (abbrev->tag)
18102 {
18103 case DW_TAG_compile_unit:
18104 case DW_TAG_partial_unit:
18105 case DW_TAG_type_unit:
18106 /* Compilation units have a DW_AT_name that is a filename, not
18107 a source language identifier. */
18108 break;
18109
18110 default:
18111 if (*name == nullptr)
18112 *name = attr.as_string ();
18113 break;
18114 }
18115 break;
18116
18117 case DW_AT_linkage_name:
18118 case DW_AT_MIPS_linkage_name:
18119 /* Note that both forms of linkage name might appear. We
18120 assume they will be the same, and we only store the last
18121 one we see. */
18122 if (*linkage_name == nullptr)
18123 *linkage_name = attr.as_string ();
18124 break;
18125
18126 case DW_AT_main_subprogram:
18127 if (attr.as_boolean ())
18128 *flags |= IS_MAIN;
18129 break;
18130
18131 case DW_AT_declaration:
18132 is_declaration = attr.as_boolean ();
18133 break;
18134
18135 case DW_AT_sibling:
18136 if (sibling_offset != nullptr)
18137 *sibling_offset = attr.get_ref_die_offset ();
18138 break;
18139
18140 case DW_AT_specification:
18141 case DW_AT_abstract_origin:
18142 case DW_AT_extension:
18143 origin_offset = attr.get_ref_die_offset ();
18144 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
18145 break;
18146
18147 case DW_AT_external:
18148 if (attr.as_boolean ())
18149 *flags &= ~IS_STATIC;
18150 break;
18151
18152 case DW_AT_enum_class:
18153 if (attr.as_boolean ())
18154 *flags |= IS_ENUM_CLASS;
18155 break;
18156
18157 case DW_AT_low_pc:
18158 low_pc = attr.as_address ();
18159 break;
18160
18161 case DW_AT_high_pc:
18162 high_pc = attr.as_address ();
18163 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
18164 high_pc_relative = true;
18165 break;
18166
18167 case DW_AT_location:
18168 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
18169 {
18170 struct dwarf_block *locdesc = attr.as_block ();
18171 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
18172 if (addr != 0
18173 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
18174 {
18175 low_pc = addr;
18176 /* For variables, we don't want to try decoding the
18177 type just to find the size -- for gdb's purposes
18178 we only need the address of a variable. */
18179 high_pc = addr + 1;
18180 high_pc_relative = false;
18181 }
18182 }
18183 break;
18184
18185 case DW_AT_ranges:
18186 if (!scanning_per_cu->addresses_seen)
18187 {
18188 /* Offset in the .debug_ranges or .debug_rnglist section
18189 (depending on DWARF version). */
18190 ULONGEST ranges_offset = attr.as_unsigned ();
18191
18192 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18193 want to add this value. */
18194 ranges_offset += reader->cu->gnu_ranges_base;
18195
18196 CORE_ADDR lowpc, highpc;
18197 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
18198 m_index_storage->get_addrmap (),
18199 scanning_per_cu, abbrev->tag);
18200 }
18201 break;
18202 }
18203 }
18204
18205 /* We don't want to examine declarations, but if we found a
18206 declaration when handling DW_AT_specification or the like, then
18207 that is ok. Similarly, we allow an external variable without a
18208 location; those are resolved via minimal symbols. */
18209 if (is_declaration && !for_specification
18210 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
18211 {
18212 /* We always want to recurse into some types, but we may not
18213 want to treat them as definitions. */
18214 if ((abbrev->tag == DW_TAG_class_type
18215 || abbrev->tag == DW_TAG_structure_type
18216 || abbrev->tag == DW_TAG_union_type)
18217 && abbrev->has_children)
18218 *flags |= IS_TYPE_DECLARATION;
18219 else
18220 {
18221 *linkage_name = nullptr;
18222 *name = nullptr;
18223 }
18224 }
18225 else if ((*name == nullptr
18226 || (*linkage_name == nullptr
18227 && tag_can_have_linkage_name (abbrev->tag))
18228 || (*parent_entry == nullptr && m_language != language_c))
18229 && origin_offset != sect_offset (0))
18230 {
18231 cutu_reader *new_reader
18232 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
18233 origin_is_dwz, false);
18234 if (new_reader != nullptr)
18235 {
18236 const gdb_byte *new_info_ptr = (new_reader->buffer
18237 + to_underlying (origin_offset));
18238
18239 if (new_reader->cu == reader->cu
18240 && new_info_ptr > watermark_ptr
18241 && *parent_entry == nullptr)
18242 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
18243 else if (*parent_entry == nullptr)
18244 {
18245 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
18246 void *obj = m_die_range_map.find (lookup);
18247 *parent_entry = static_cast <cooked_index_entry *> (obj);
18248 }
18249
18250 unsigned int bytes_read;
18251 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
18252 new_info_ptr,
18253 &bytes_read);
18254 new_info_ptr += bytes_read;
18255 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
18256 new_abbrev, name, linkage_name, flags, nullptr,
18257 parent_entry, maybe_defer, true);
18258 }
18259 }
18260
18261 if (!for_specification)
18262 {
18263 if (m_language == language_ada
18264 && *linkage_name == nullptr)
18265 *linkage_name = *name;
18266
18267 if (!scanning_per_cu->addresses_seen
18268 && low_pc.has_value ()
18269 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
18270 || *low_pc != 0)
18271 && high_pc.has_value ())
18272 {
18273 if (high_pc_relative)
18274 high_pc = *high_pc + *low_pc;
18275
18276 if (*high_pc > *low_pc)
18277 {
18278 struct objfile *objfile = reader->cu->per_objfile->objfile;
18279 CORE_ADDR baseaddr = objfile->text_section_offset ();
18280 struct gdbarch *gdbarch = objfile->arch ();
18281 CORE_ADDR lo
18282 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
18283 - baseaddr);
18284 CORE_ADDR hi
18285 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
18286 - baseaddr);
18287 m_index_storage->get_addrmap ()->set_empty (lo, hi - 1,
18288 scanning_per_cu);
18289 }
18290 }
18291
18292 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
18293 *flags &= ~IS_STATIC;
18294
18295 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
18296 *name = "(anonymous namespace)";
18297
18298 if (m_language == language_cplus
18299 && (abbrev->tag == DW_TAG_class_type
18300 || abbrev->tag == DW_TAG_interface_type
18301 || abbrev->tag == DW_TAG_structure_type
18302 || abbrev->tag == DW_TAG_union_type
18303 || abbrev->tag == DW_TAG_enumeration_type
18304 || abbrev->tag == DW_TAG_enumerator))
18305 *flags &= ~IS_STATIC;
18306 }
18307
18308 return info_ptr;
18309 }
18310
18311 const gdb_byte *
18312 cooked_indexer::index_imported_unit (cutu_reader *reader,
18313 const gdb_byte *info_ptr,
18314 const abbrev_info *abbrev)
18315 {
18316 sect_offset sect_off {};
18317 bool is_dwz = false;
18318
18319 for (int i = 0; i < abbrev->num_attrs; ++i)
18320 {
18321 /* Note that we never need to reprocess attributes here. */
18322 attribute attr;
18323 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18324
18325 if (attr.name == DW_AT_import)
18326 {
18327 sect_off = attr.get_ref_die_offset ();
18328 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18329 || reader->cu->per_cu->is_dwz);
18330 }
18331 }
18332
18333 /* Did not find DW_AT_import. */
18334 if (sect_off == sect_offset (0))
18335 return info_ptr;
18336
18337 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
18338 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
18339 is_dwz, true);
18340 if (new_reader != nullptr)
18341 {
18342 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
18343
18344 reader->cu->add_dependence (new_reader->cu->per_cu);
18345 }
18346
18347 return info_ptr;
18348 }
18349
18350 const gdb_byte *
18351 cooked_indexer::recurse (cutu_reader *reader,
18352 const gdb_byte *info_ptr,
18353 const cooked_index_entry *parent_entry,
18354 bool fully)
18355 {
18356 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18357
18358 if (parent_entry != nullptr)
18359 {
18360 CORE_ADDR start = form_addr (parent_entry->die_offset,
18361 reader->cu->per_cu->is_dwz);
18362 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
18363 reader->cu->per_cu->is_dwz);
18364 m_die_range_map.set_empty (start, end, (void *) parent_entry);
18365 }
18366
18367 return info_ptr;
18368 }
18369
18370 const gdb_byte *
18371 cooked_indexer::index_dies (cutu_reader *reader,
18372 const gdb_byte *info_ptr,
18373 const cooked_index_entry *parent_entry,
18374 bool fully)
18375 {
18376 const gdb_byte *end_ptr = (reader->buffer
18377 + to_underlying (reader->cu->header.sect_off)
18378 + reader->cu->header.get_length_with_initial ());
18379
18380 while (info_ptr < end_ptr)
18381 {
18382 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
18383 unsigned int bytes_read;
18384 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18385 &bytes_read);
18386 info_ptr += bytes_read;
18387 if (abbrev == nullptr)
18388 break;
18389
18390 if (abbrev->tag == DW_TAG_imported_unit)
18391 {
18392 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
18393 continue;
18394 }
18395
18396 if (!abbrev->interesting)
18397 {
18398 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
18399 if (fully && abbrev->has_children)
18400 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18401 continue;
18402 }
18403
18404 const char *name = nullptr;
18405 const char *linkage_name = nullptr;
18406 CORE_ADDR defer = 0;
18407 cooked_index_flag flags = IS_STATIC;
18408 sect_offset sibling {};
18409 const cooked_index_entry *this_parent_entry = parent_entry;
18410 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
18411 info_ptr, abbrev, &name, &linkage_name,
18412 &flags, &sibling, &this_parent_entry,
18413 &defer, false);
18414
18415 if (abbrev->tag == DW_TAG_namespace
18416 && m_language == language_cplus
18417 && strcmp (name, "::") == 0)
18418 {
18419 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18420 generated bogus DW_TAG_namespace DIEs with a name of "::"
18421 for the global namespace. Work around this problem
18422 here. */
18423 name = nullptr;
18424 }
18425
18426 const cooked_index_entry *this_entry = nullptr;
18427 if (name != nullptr)
18428 {
18429 if (defer != 0)
18430 m_deferred_entries.push_back ({
18431 this_die, name, defer, abbrev->tag, flags
18432 });
18433 else
18434 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
18435 name, this_parent_entry,
18436 m_per_cu);
18437 }
18438
18439 if (linkage_name != nullptr)
18440 {
18441 /* We only want this to be "main" if it has a linkage name
18442 but not an ordinary name. */
18443 if (name != nullptr)
18444 flags = flags & ~IS_MAIN;
18445 /* Set the IS_LINKAGE on for everything except when functions
18446 have linkage name present but name is absent. */
18447 if (name != nullptr
18448 || (abbrev->tag != DW_TAG_subprogram
18449 && abbrev->tag != DW_TAG_inlined_subroutine
18450 && abbrev->tag != DW_TAG_entry_point))
18451 flags = flags | IS_LINKAGE;
18452 m_index_storage->add (this_die, abbrev->tag, flags,
18453 linkage_name, nullptr, m_per_cu);
18454 }
18455
18456 if (abbrev->has_children)
18457 {
18458 switch (abbrev->tag)
18459 {
18460 case DW_TAG_class_type:
18461 case DW_TAG_interface_type:
18462 case DW_TAG_structure_type:
18463 case DW_TAG_union_type:
18464 if (m_language != language_c && this_entry != nullptr)
18465 {
18466 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18467 continue;
18468 }
18469 break;
18470
18471 case DW_TAG_enumeration_type:
18472 /* We need to recurse even for an anonymous enumeration.
18473 Which scope we record as the parent scope depends on
18474 whether we're reading an "enum class". If so, we use
18475 the enum itself as the parent, yielding names like
18476 "enum_class::enumerator"; otherwise we inject the
18477 names into our own parent scope. */
18478 info_ptr = recurse (reader, info_ptr,
18479 ((flags & IS_ENUM_CLASS) == 0)
18480 ? parent_entry
18481 : this_entry,
18482 fully);
18483 continue;
18484
18485 case DW_TAG_module:
18486 if (this_entry == nullptr)
18487 break;
18488 /* FALLTHROUGH */
18489 case DW_TAG_namespace:
18490 /* We don't check THIS_ENTRY for a namespace, to handle
18491 the ancient G++ workaround pointed out above. */
18492 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18493 continue;
18494
18495 case DW_TAG_subprogram:
18496 if ((m_language == language_fortran
18497 || m_language == language_ada)
18498 && this_entry != nullptr)
18499 {
18500 info_ptr = recurse (reader, info_ptr, this_entry, true);
18501 continue;
18502 }
18503 break;
18504 }
18505
18506 if (sibling != sect_offset (0))
18507 {
18508 const gdb_byte *sibling_ptr
18509 = reader->buffer + to_underlying (sibling);
18510
18511 if (sibling_ptr < info_ptr)
18512 complaint (_("DW_AT_sibling points backwards"));
18513 else if (sibling_ptr > reader->buffer_end)
18514 reader->die_section->overflow_complaint ();
18515 else
18516 info_ptr = sibling_ptr;
18517 }
18518 else
18519 info_ptr = skip_children (reader, info_ptr);
18520 }
18521 }
18522
18523 return info_ptr;
18524 }
18525
18526 void
18527 cooked_indexer::make_index (cutu_reader *reader)
18528 {
18529 check_bounds (reader);
18530 find_file_and_directory (reader->comp_unit_die, reader->cu);
18531 if (!reader->comp_unit_die->has_children)
18532 return;
18533 index_dies (reader, reader->info_ptr, nullptr, false);
18534
18535 for (const auto &entry : m_deferred_entries)
18536 {
18537 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
18538 void *obj = m_die_range_map.find (key);
18539 cooked_index_entry *parent = static_cast <cooked_index_entry *> (obj);
18540 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
18541 entry.name, parent, m_per_cu);
18542 }
18543 }
18544
18545 /* An implementation of quick_symbol_functions for the cooked DWARF
18546 index. */
18547
18548 struct cooked_index_functions : public dwarf2_base_index_functions
18549 {
18550 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
18551 CORE_ADDR adjusted_pc) override;
18552
18553 struct compunit_symtab *find_compunit_symtab_by_address
18554 (struct objfile *objfile, CORE_ADDR address) override;
18555
18556 void dump (struct objfile *objfile) override
18557 {
18558 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18559 cooked_index *index
18560 = (gdb::checked_static_cast<cooked_index *>
18561 (per_objfile->per_bfd->index_table.get ()));
18562 if (index == nullptr)
18563 return;
18564
18565 gdb_printf ("Cooked index in use:\n");
18566 gdb_printf ("\n");
18567 index->dump (objfile->arch ());
18568 }
18569
18570 void expand_matching_symbols
18571 (struct objfile *,
18572 const lookup_name_info &lookup_name,
18573 domain_enum domain,
18574 int global,
18575 symbol_compare_ftype *ordered_compare) override;
18576
18577 bool expand_symtabs_matching
18578 (struct objfile *objfile,
18579 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18580 const lookup_name_info *lookup_name,
18581 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18582 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18583 block_search_flags search_flags,
18584 domain_enum domain,
18585 enum search_domain kind) override;
18586
18587 bool can_lazily_read_symbols () override
18588 {
18589 return true;
18590 }
18591
18592 void read_partial_symbols (struct objfile *objfile) override
18593 {
18594 if (dwarf2_has_info (objfile, nullptr))
18595 dwarf2_build_psymtabs (objfile);
18596 }
18597 };
18598
18599 dwarf2_per_cu_data *
18600 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
18601 CORE_ADDR adjusted_pc)
18602 {
18603 cooked_index *table
18604 = (gdb::checked_static_cast<cooked_index *>
18605 (per_bfd->index_table.get ()));
18606 if (table == nullptr)
18607 return nullptr;
18608 return table->lookup (adjusted_pc);
18609 }
18610
18611 struct compunit_symtab *
18612 cooked_index_functions::find_compunit_symtab_by_address
18613 (struct objfile *objfile, CORE_ADDR address)
18614 {
18615 if (objfile->sect_index_data == -1)
18616 return nullptr;
18617
18618 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18619 cooked_index *table
18620 = (gdb::checked_static_cast<cooked_index *>
18621 (per_objfile->per_bfd->index_table.get ()));
18622 if (table == nullptr)
18623 return nullptr;
18624
18625 CORE_ADDR baseaddr = objfile->data_section_offset ();
18626 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
18627 if (per_cu == nullptr)
18628 return nullptr;
18629
18630 return dw2_instantiate_symtab (per_cu, per_objfile, false);
18631 }
18632
18633 void
18634 cooked_index_functions::expand_matching_symbols
18635 (struct objfile *objfile,
18636 const lookup_name_info &lookup_name,
18637 domain_enum domain,
18638 int global,
18639 symbol_compare_ftype *ordered_compare)
18640 {
18641 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18642 cooked_index *table
18643 = (gdb::checked_static_cast<cooked_index *>
18644 (per_objfile->per_bfd->index_table.get ()));
18645 if (table == nullptr)
18646 return;
18647
18648 const block_search_flags search_flags = (global
18649 ? SEARCH_GLOBAL_BLOCK
18650 : SEARCH_STATIC_BLOCK);
18651 const language_defn *lang = language_def (language_ada);
18652 symbol_name_matcher_ftype *name_match
18653 = lang->get_symbol_name_matcher (lookup_name);
18654
18655 for (const cooked_index_entry *entry : table->all_entries ())
18656 {
18657 QUIT;
18658
18659 if (entry->parent_entry != nullptr)
18660 continue;
18661
18662 if (!entry->matches (search_flags)
18663 || !entry->matches (domain))
18664 continue;
18665
18666 if (name_match (entry->canonical, lookup_name, nullptr))
18667 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
18668 }
18669 }
18670
18671 bool
18672 cooked_index_functions::expand_symtabs_matching
18673 (struct objfile *objfile,
18674 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18675 const lookup_name_info *lookup_name,
18676 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18677 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18678 block_search_flags search_flags,
18679 domain_enum domain,
18680 enum search_domain kind)
18681 {
18682 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18683
18684 cooked_index *table
18685 = (gdb::checked_static_cast<cooked_index *>
18686 (per_objfile->per_bfd->index_table.get ()));
18687 if (table == nullptr)
18688 return true;
18689
18690 table->wait ();
18691
18692 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
18693
18694 /* This invariant is documented in quick-functions.h. */
18695 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
18696 if (lookup_name == nullptr)
18697 {
18698 for (dwarf2_per_cu_data *per_cu
18699 : all_units_range (per_objfile->per_bfd))
18700 {
18701 QUIT;
18702
18703 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
18704 file_matcher,
18705 expansion_notify))
18706 return false;
18707 }
18708 return true;
18709 }
18710
18711 lookup_name_info lookup_name_without_params
18712 = lookup_name->make_ignore_params ();
18713 bool completing = lookup_name->completion_mode ();
18714
18715 /* Unique styles of language splitting. */
18716 static const enum language unique_styles[] =
18717 {
18718 /* No splitting is also a style. */
18719 language_c,
18720 /* This includes Rust. */
18721 language_cplus,
18722 /* This includes Go. */
18723 language_d,
18724 language_ada
18725 };
18726
18727 for (enum language lang : unique_styles)
18728 {
18729 std::vector<gdb::string_view> name_vec
18730 = lookup_name_without_params.split_name (lang);
18731 std::string last_name = gdb::to_string (name_vec.back ());
18732
18733 for (const cooked_index_entry *entry : table->find (last_name,
18734 completing))
18735 {
18736 QUIT;
18737
18738 /* No need to consider symbols from expanded CUs. */
18739 if (per_objfile->symtab_set_p (entry->per_cu))
18740 continue;
18741
18742 /* If file-matching was done, we don't need to consider
18743 symbols from unmarked CUs. */
18744 if (file_matcher != nullptr && !entry->per_cu->mark)
18745 continue;
18746
18747 /* See if the symbol matches the type filter. */
18748 if (!entry->matches (search_flags)
18749 || !entry->matches (domain)
18750 || !entry->matches (kind))
18751 continue;
18752
18753 /* We've found the base name of the symbol; now walk its
18754 parentage chain, ensuring that each component
18755 matches. */
18756 bool found = true;
18757
18758 const cooked_index_entry *parent = entry->parent_entry;
18759 for (int i = name_vec.size () - 1; i > 0; --i)
18760 {
18761 /* If we ran out of entries, or if this segment doesn't
18762 match, this did not match. */
18763 if (parent == nullptr
18764 || strncmp (parent->name, name_vec[i - 1].data (),
18765 name_vec[i - 1].length ()) != 0)
18766 {
18767 found = false;
18768 break;
18769 }
18770
18771 parent = parent->parent_entry;
18772 }
18773
18774 if (!found)
18775 continue;
18776
18777 /* Might have been looking for "a::b" and found
18778 "x::a::b". */
18779 if (symbol_matcher == nullptr)
18780 {
18781 symbol_name_match_type match_type
18782 = lookup_name_without_params.match_type ();
18783 if ((match_type == symbol_name_match_type::FULL
18784 || (lang != language_ada
18785 && match_type == symbol_name_match_type::EXPRESSION))
18786 && parent != nullptr)
18787 continue;
18788 }
18789 else
18790 {
18791 auto_obstack temp_storage;
18792 const char *full_name = entry->full_name (&temp_storage);
18793 if (!symbol_matcher (full_name))
18794 continue;
18795 }
18796
18797 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
18798 file_matcher,
18799 expansion_notify))
18800 return false;
18801 }
18802 }
18803
18804 return true;
18805 }
18806
18807 /* Return a new cooked_index_functions object. */
18808
18809 static quick_symbol_functions_up
18810 make_cooked_index_funcs ()
18811 {
18812 return quick_symbol_functions_up (new cooked_index_functions);
18813 }
18814
18815 quick_symbol_functions_up
18816 cooked_index::make_quick_functions () const
18817 {
18818 return make_cooked_index_funcs ();
18819 }
18820
18821 \f
18822
18823 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18824 contents from the given SECTION in the HEADER.
18825
18826 HEADER_OFFSET is the offset of the header in the section. */
18827 static void
18828 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
18829 struct dwarf2_section_info *section,
18830 sect_offset header_offset)
18831 {
18832 unsigned int bytes_read;
18833 bfd *abfd = section->get_bfd_owner ();
18834 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
18835
18836 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18837 info_ptr += bytes_read;
18838
18839 header->version = read_2_bytes (abfd, info_ptr);
18840 info_ptr += 2;
18841
18842 header->addr_size = read_1_byte (abfd, info_ptr);
18843 info_ptr += 1;
18844
18845 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18846 info_ptr += 1;
18847
18848 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18849 }
18850
18851 /* Return the DW_AT_loclists_base value for the CU. */
18852 static ULONGEST
18853 lookup_loclist_base (struct dwarf2_cu *cu)
18854 {
18855 /* For the .dwo unit, the loclist_base points to the first offset following
18856 the header. The header consists of the following entities-
18857 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18858 bit format)
18859 2. version (2 bytes)
18860 3. address size (1 byte)
18861 4. segment selector size (1 byte)
18862 5. offset entry count (4 bytes)
18863 These sizes are derived as per the DWARFv5 standard. */
18864 if (cu->dwo_unit != nullptr)
18865 {
18866 if (cu->header.initial_length_size == 4)
18867 return LOCLIST_HEADER_SIZE32;
18868 return LOCLIST_HEADER_SIZE64;
18869 }
18870 return cu->loclist_base;
18871 }
18872
18873 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18874 array of offsets in the .debug_loclists section. */
18875
18876 static sect_offset
18877 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18878 {
18879 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18880 struct objfile *objfile = per_objfile->objfile;
18881 bfd *abfd = objfile->obfd.get ();
18882 ULONGEST loclist_header_size =
18883 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
18884 : LOCLIST_HEADER_SIZE64);
18885 ULONGEST loclist_base = lookup_loclist_base (cu);
18886
18887 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18888 ULONGEST start_offset =
18889 loclist_base + loclist_index * cu->header.offset_size;
18890
18891 /* Get loclists section. */
18892 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18893
18894 /* Read the loclists section content. */
18895 section->read (objfile);
18896 if (section->buffer == NULL)
18897 error (_("DW_FORM_loclistx used without .debug_loclists "
18898 "section [in module %s]"), objfile_name (objfile));
18899
18900 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18901 so if loclist_base is smaller than the header size, we have a problem. */
18902 if (loclist_base < loclist_header_size)
18903 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18904 objfile_name (objfile));
18905
18906 /* Read the header of the loclists contribution. */
18907 struct loclists_rnglists_header header;
18908 read_loclists_rnglists_header (&header, section,
18909 (sect_offset) (loclist_base - loclist_header_size));
18910
18911 /* Verify the loclist index is valid. */
18912 if (loclist_index >= header.offset_entry_count)
18913 error (_("DW_FORM_loclistx pointing outside of "
18914 ".debug_loclists offset array [in module %s]"),
18915 objfile_name (objfile));
18916
18917 /* Validate that reading won't go beyond the end of the section. */
18918 if (start_offset + cu->header.offset_size > section->size)
18919 error (_("Reading DW_FORM_loclistx index beyond end of"
18920 ".debug_loclists section [in module %s]"),
18921 objfile_name (objfile));
18922
18923 const gdb_byte *info_ptr = section->buffer + start_offset;
18924
18925 if (cu->header.offset_size == 4)
18926 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
18927 else
18928 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
18929 }
18930
18931 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18932 array of offsets in the .debug_rnglists section. */
18933
18934 static sect_offset
18935 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
18936 dwarf_tag tag)
18937 {
18938 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18939 struct objfile *objfile = dwarf2_per_objfile->objfile;
18940 bfd *abfd = objfile->obfd.get ();
18941 ULONGEST rnglist_header_size =
18942 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
18943 : RNGLIST_HEADER_SIZE64);
18944
18945 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18946 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18947 doesn't apply. */
18948 ULONGEST rnglist_base =
18949 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
18950
18951 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18952 ULONGEST start_offset =
18953 rnglist_base + rnglist_index * cu->header.offset_size;
18954
18955 /* Get rnglists section. */
18956 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
18957
18958 /* Read the rnglists section content. */
18959 section->read (objfile);
18960 if (section->buffer == nullptr)
18961 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18962 "[in module %s]"),
18963 objfile_name (objfile));
18964
18965 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18966 so if rnglist_base is smaller than the header size, we have a problem. */
18967 if (rnglist_base < rnglist_header_size)
18968 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18969 objfile_name (objfile));
18970
18971 /* Read the header of the rnglists contribution. */
18972 struct loclists_rnglists_header header;
18973 read_loclists_rnglists_header (&header, section,
18974 (sect_offset) (rnglist_base - rnglist_header_size));
18975
18976 /* Verify the rnglist index is valid. */
18977 if (rnglist_index >= header.offset_entry_count)
18978 error (_("DW_FORM_rnglistx index pointing outside of "
18979 ".debug_rnglists offset array [in module %s]"),
18980 objfile_name (objfile));
18981
18982 /* Validate that reading won't go beyond the end of the section. */
18983 if (start_offset + cu->header.offset_size > section->size)
18984 error (_("Reading DW_FORM_rnglistx index beyond end of"
18985 ".debug_rnglists section [in module %s]"),
18986 objfile_name (objfile));
18987
18988 const gdb_byte *info_ptr = section->buffer + start_offset;
18989
18990 if (cu->header.offset_size == 4)
18991 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
18992 else
18993 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
18994 }
18995
18996 /* Process the attributes that had to be skipped in the first round. These
18997 attributes are the ones that need str_offsets_base or addr_base attributes.
18998 They could not have been processed in the first round, because at the time
18999 the values of str_offsets_base or addr_base may not have been known. */
19000 static void
19001 read_attribute_reprocess (const struct die_reader_specs *reader,
19002 struct attribute *attr, dwarf_tag tag)
19003 {
19004 struct dwarf2_cu *cu = reader->cu;
19005 switch (attr->form)
19006 {
19007 case DW_FORM_addrx:
19008 case DW_FORM_GNU_addr_index:
19009 attr->set_address (read_addr_index (cu,
19010 attr->as_unsigned_reprocess ()));
19011 break;
19012 case DW_FORM_loclistx:
19013 {
19014 sect_offset loclists_sect_off
19015 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19016
19017 attr->set_unsigned (to_underlying (loclists_sect_off));
19018 }
19019 break;
19020 case DW_FORM_rnglistx:
19021 {
19022 sect_offset rnglists_sect_off
19023 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19024
19025 attr->set_unsigned (to_underlying (rnglists_sect_off));
19026 }
19027 break;
19028 case DW_FORM_strx:
19029 case DW_FORM_strx1:
19030 case DW_FORM_strx2:
19031 case DW_FORM_strx3:
19032 case DW_FORM_strx4:
19033 case DW_FORM_GNU_str_index:
19034 {
19035 unsigned int str_index = attr->as_unsigned_reprocess ();
19036 gdb_assert (!attr->canonical_string_p ());
19037 if (reader->dwo_file != NULL)
19038 attr->set_string_noncanonical (read_dwo_str_index (reader,
19039 str_index));
19040 else
19041 attr->set_string_noncanonical (read_stub_str_index (cu,
19042 str_index));
19043 break;
19044 }
19045 default:
19046 gdb_assert_not_reached ("Unexpected DWARF form.");
19047 }
19048 }
19049
19050 /* Read an attribute value described by an attribute form. */
19051
19052 static const gdb_byte *
19053 read_attribute_value (const struct die_reader_specs *reader,
19054 struct attribute *attr, unsigned form,
19055 LONGEST implicit_const, const gdb_byte *info_ptr)
19056 {
19057 struct dwarf2_cu *cu = reader->cu;
19058 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19059 struct objfile *objfile = per_objfile->objfile;
19060 bfd *abfd = reader->abfd;
19061 struct comp_unit_head *cu_header = &cu->header;
19062 unsigned int bytes_read;
19063 struct dwarf_block *blk;
19064
19065 attr->form = (enum dwarf_form) form;
19066 switch (form)
19067 {
19068 case DW_FORM_ref_addr:
19069 if (cu_header->version == 2)
19070 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19071 &bytes_read));
19072 else
19073 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19074 &bytes_read));
19075 info_ptr += bytes_read;
19076 break;
19077 case DW_FORM_GNU_ref_alt:
19078 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19079 &bytes_read));
19080 info_ptr += bytes_read;
19081 break;
19082 case DW_FORM_addr:
19083 {
19084 struct gdbarch *gdbarch = objfile->arch ();
19085 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19086 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19087 attr->set_address (addr);
19088 info_ptr += bytes_read;
19089 }
19090 break;
19091 case DW_FORM_block2:
19092 blk = dwarf_alloc_block (cu);
19093 blk->size = read_2_bytes (abfd, info_ptr);
19094 info_ptr += 2;
19095 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19096 info_ptr += blk->size;
19097 attr->set_block (blk);
19098 break;
19099 case DW_FORM_block4:
19100 blk = dwarf_alloc_block (cu);
19101 blk->size = read_4_bytes (abfd, info_ptr);
19102 info_ptr += 4;
19103 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19104 info_ptr += blk->size;
19105 attr->set_block (blk);
19106 break;
19107 case DW_FORM_data2:
19108 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19109 info_ptr += 2;
19110 break;
19111 case DW_FORM_data4:
19112 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19113 info_ptr += 4;
19114 break;
19115 case DW_FORM_data8:
19116 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19117 info_ptr += 8;
19118 break;
19119 case DW_FORM_data16:
19120 blk = dwarf_alloc_block (cu);
19121 blk->size = 16;
19122 blk->data = read_n_bytes (abfd, info_ptr, 16);
19123 info_ptr += 16;
19124 attr->set_block (blk);
19125 break;
19126 case DW_FORM_sec_offset:
19127 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19128 &bytes_read));
19129 info_ptr += bytes_read;
19130 break;
19131 case DW_FORM_loclistx:
19132 {
19133 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19134 &bytes_read));
19135 info_ptr += bytes_read;
19136 }
19137 break;
19138 case DW_FORM_string:
19139 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19140 &bytes_read));
19141 info_ptr += bytes_read;
19142 break;
19143 case DW_FORM_strp:
19144 if (!cu->per_cu->is_dwz)
19145 {
19146 attr->set_string_noncanonical
19147 (read_indirect_string (per_objfile,
19148 abfd, info_ptr, cu_header,
19149 &bytes_read));
19150 info_ptr += bytes_read;
19151 break;
19152 }
19153 /* FALLTHROUGH */
19154 case DW_FORM_line_strp:
19155 if (!cu->per_cu->is_dwz)
19156 {
19157 attr->set_string_noncanonical
19158 (per_objfile->read_line_string (info_ptr, cu_header,
19159 &bytes_read));
19160 info_ptr += bytes_read;
19161 break;
19162 }
19163 /* FALLTHROUGH */
19164 case DW_FORM_GNU_strp_alt:
19165 {
19166 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19167 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19168 &bytes_read);
19169
19170 attr->set_string_noncanonical
19171 (dwz->read_string (objfile, str_offset));
19172 info_ptr += bytes_read;
19173 }
19174 break;
19175 case DW_FORM_exprloc:
19176 case DW_FORM_block:
19177 blk = dwarf_alloc_block (cu);
19178 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19179 info_ptr += bytes_read;
19180 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19181 info_ptr += blk->size;
19182 attr->set_block (blk);
19183 break;
19184 case DW_FORM_block1:
19185 blk = dwarf_alloc_block (cu);
19186 blk->size = read_1_byte (abfd, info_ptr);
19187 info_ptr += 1;
19188 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19189 info_ptr += blk->size;
19190 attr->set_block (blk);
19191 break;
19192 case DW_FORM_data1:
19193 case DW_FORM_flag:
19194 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19195 info_ptr += 1;
19196 break;
19197 case DW_FORM_flag_present:
19198 attr->set_unsigned (1);
19199 break;
19200 case DW_FORM_sdata:
19201 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19202 info_ptr += bytes_read;
19203 break;
19204 case DW_FORM_rnglistx:
19205 {
19206 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19207 &bytes_read));
19208 info_ptr += bytes_read;
19209 }
19210 break;
19211 case DW_FORM_udata:
19212 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19213 info_ptr += bytes_read;
19214 break;
19215 case DW_FORM_ref1:
19216 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19217 + read_1_byte (abfd, info_ptr)));
19218 info_ptr += 1;
19219 break;
19220 case DW_FORM_ref2:
19221 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19222 + read_2_bytes (abfd, info_ptr)));
19223 info_ptr += 2;
19224 break;
19225 case DW_FORM_ref4:
19226 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19227 + read_4_bytes (abfd, info_ptr)));
19228 info_ptr += 4;
19229 break;
19230 case DW_FORM_ref8:
19231 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19232 + read_8_bytes (abfd, info_ptr)));
19233 info_ptr += 8;
19234 break;
19235 case DW_FORM_ref_sig8:
19236 attr->set_signature (read_8_bytes (abfd, info_ptr));
19237 info_ptr += 8;
19238 break;
19239 case DW_FORM_ref_udata:
19240 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19241 + read_unsigned_leb128 (abfd, info_ptr,
19242 &bytes_read)));
19243 info_ptr += bytes_read;
19244 break;
19245 case DW_FORM_indirect:
19246 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19247 info_ptr += bytes_read;
19248 if (form == DW_FORM_implicit_const)
19249 {
19250 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19251 info_ptr += bytes_read;
19252 }
19253 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19254 info_ptr);
19255 break;
19256 case DW_FORM_implicit_const:
19257 attr->set_signed (implicit_const);
19258 break;
19259 case DW_FORM_addrx:
19260 case DW_FORM_GNU_addr_index:
19261 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19262 &bytes_read));
19263 info_ptr += bytes_read;
19264 break;
19265 case DW_FORM_strx:
19266 case DW_FORM_strx1:
19267 case DW_FORM_strx2:
19268 case DW_FORM_strx3:
19269 case DW_FORM_strx4:
19270 case DW_FORM_GNU_str_index:
19271 {
19272 ULONGEST str_index;
19273 if (form == DW_FORM_strx1)
19274 {
19275 str_index = read_1_byte (abfd, info_ptr);
19276 info_ptr += 1;
19277 }
19278 else if (form == DW_FORM_strx2)
19279 {
19280 str_index = read_2_bytes (abfd, info_ptr);
19281 info_ptr += 2;
19282 }
19283 else if (form == DW_FORM_strx3)
19284 {
19285 str_index = read_3_bytes (abfd, info_ptr);
19286 info_ptr += 3;
19287 }
19288 else if (form == DW_FORM_strx4)
19289 {
19290 str_index = read_4_bytes (abfd, info_ptr);
19291 info_ptr += 4;
19292 }
19293 else
19294 {
19295 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19296 info_ptr += bytes_read;
19297 }
19298 attr->set_unsigned_reprocess (str_index);
19299 }
19300 break;
19301 default:
19302 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19303 dwarf_form_name (form),
19304 bfd_get_filename (abfd));
19305 }
19306
19307 /* Super hack. */
19308 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19309 attr->form = DW_FORM_GNU_ref_alt;
19310
19311 /* We have seen instances where the compiler tried to emit a byte
19312 size attribute of -1 which ended up being encoded as an unsigned
19313 0xffffffff. Although 0xffffffff is technically a valid size value,
19314 an object of this size seems pretty unlikely so we can relatively
19315 safely treat these cases as if the size attribute was invalid and
19316 treat them as zero by default. */
19317 if (attr->name == DW_AT_byte_size
19318 && form == DW_FORM_data4
19319 && attr->as_unsigned () >= 0xffffffff)
19320 {
19321 complaint
19322 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19323 hex_string (attr->as_unsigned ()));
19324 attr->set_unsigned (0);
19325 }
19326
19327 return info_ptr;
19328 }
19329
19330 /* Read an attribute described by an abbreviated attribute. */
19331
19332 static const gdb_byte *
19333 read_attribute (const struct die_reader_specs *reader,
19334 struct attribute *attr, const struct attr_abbrev *abbrev,
19335 const gdb_byte *info_ptr)
19336 {
19337 attr->name = abbrev->name;
19338 attr->string_is_canonical = 0;
19339 attr->requires_reprocessing = 0;
19340 return read_attribute_value (reader, attr, abbrev->form,
19341 abbrev->implicit_const, info_ptr);
19342 }
19343
19344 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19345
19346 static const char *
19347 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19348 LONGEST str_offset)
19349 {
19350 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19351 str_offset, "DW_FORM_strp");
19352 }
19353
19354 /* Return pointer to string at .debug_str offset as read from BUF.
19355 BUF is assumed to be in a compilation unit described by CU_HEADER.
19356 Return *BYTES_READ_PTR count of bytes read from BUF. */
19357
19358 static const char *
19359 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19360 const gdb_byte *buf,
19361 const struct comp_unit_head *cu_header,
19362 unsigned int *bytes_read_ptr)
19363 {
19364 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19365
19366 return read_indirect_string_at_offset (per_objfile, str_offset);
19367 }
19368
19369 /* See read.h. */
19370
19371 const char *
19372 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19373 unsigned int offset_size)
19374 {
19375 bfd *abfd = objfile->obfd.get ();
19376 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
19377
19378 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19379 }
19380
19381 /* See read.h. */
19382
19383 const char *
19384 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19385 const struct comp_unit_head *cu_header,
19386 unsigned int *bytes_read_ptr)
19387 {
19388 bfd *abfd = objfile->obfd.get ();
19389 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19390
19391 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19392 }
19393
19394 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19395 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19396 ADDR_SIZE is the size of addresses from the CU header. */
19397
19398 static CORE_ADDR
19399 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19400 gdb::optional<ULONGEST> addr_base, int addr_size)
19401 {
19402 struct objfile *objfile = per_objfile->objfile;
19403 bfd *abfd = objfile->obfd.get ();
19404 const gdb_byte *info_ptr;
19405 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19406
19407 per_objfile->per_bfd->addr.read (objfile);
19408 if (per_objfile->per_bfd->addr.buffer == NULL)
19409 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19410 objfile_name (objfile));
19411 if (addr_base_or_zero + addr_index * addr_size
19412 >= per_objfile->per_bfd->addr.size)
19413 error (_("DW_FORM_addr_index pointing outside of "
19414 ".debug_addr section [in module %s]"),
19415 objfile_name (objfile));
19416 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19417 + addr_index * addr_size);
19418 if (addr_size == 4)
19419 return bfd_get_32 (abfd, info_ptr);
19420 else
19421 return bfd_get_64 (abfd, info_ptr);
19422 }
19423
19424 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19425
19426 static CORE_ADDR
19427 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19428 {
19429 return read_addr_index_1 (cu->per_objfile, addr_index,
19430 cu->addr_base, cu->header.addr_size);
19431 }
19432
19433 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19434
19435 static CORE_ADDR
19436 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19437 unsigned int *bytes_read)
19438 {
19439 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
19440 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19441
19442 return read_addr_index (cu, addr_index);
19443 }
19444
19445 /* See read.h. */
19446
19447 CORE_ADDR
19448 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19449 dwarf2_per_objfile *per_objfile,
19450 unsigned int addr_index)
19451 {
19452 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19453 gdb::optional<ULONGEST> addr_base;
19454 int addr_size;
19455
19456 /* We need addr_base and addr_size.
19457 If we don't have PER_CU->cu, we have to get it.
19458 Nasty, but the alternative is storing the needed info in PER_CU,
19459 which at this point doesn't seem justified: it's not clear how frequently
19460 it would get used and it would increase the size of every PER_CU.
19461 Entry points like dwarf2_per_cu_addr_size do a similar thing
19462 so we're not in uncharted territory here.
19463 Alas we need to be a bit more complicated as addr_base is contained
19464 in the DIE.
19465
19466 We don't need to read the entire CU(/TU).
19467 We just need the header and top level die.
19468
19469 IWBN to use the aging mechanism to let us lazily later discard the CU.
19470 For now we skip this optimization. */
19471
19472 if (cu != NULL)
19473 {
19474 addr_base = cu->addr_base;
19475 addr_size = cu->header.addr_size;
19476 }
19477 else
19478 {
19479 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19480 addr_base = reader.cu->addr_base;
19481 addr_size = reader.cu->header.addr_size;
19482 }
19483
19484 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19485 }
19486
19487 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19488 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19489 DWO file. */
19490
19491 static const char *
19492 read_str_index (struct dwarf2_cu *cu,
19493 struct dwarf2_section_info *str_section,
19494 struct dwarf2_section_info *str_offsets_section,
19495 ULONGEST str_offsets_base, ULONGEST str_index,
19496 unsigned offset_size)
19497 {
19498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19499 struct objfile *objfile = per_objfile->objfile;
19500 const char *objf_name = objfile_name (objfile);
19501 bfd *abfd = objfile->obfd.get ();
19502 const gdb_byte *info_ptr;
19503 ULONGEST str_offset;
19504 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19505
19506 str_section->read (objfile);
19507 str_offsets_section->read (objfile);
19508 if (str_section->buffer == NULL)
19509 error (_("%s used without %s section"
19510 " in CU at offset %s [in module %s]"),
19511 form_name, str_section->get_name (),
19512 sect_offset_str (cu->header.sect_off), objf_name);
19513 if (str_offsets_section->buffer == NULL)
19514 error (_("%s used without %s section"
19515 " in CU at offset %s [in module %s]"),
19516 form_name, str_section->get_name (),
19517 sect_offset_str (cu->header.sect_off), objf_name);
19518 info_ptr = (str_offsets_section->buffer
19519 + str_offsets_base
19520 + str_index * offset_size);
19521 if (offset_size == 4)
19522 str_offset = bfd_get_32 (abfd, info_ptr);
19523 else
19524 str_offset = bfd_get_64 (abfd, info_ptr);
19525 if (str_offset >= str_section->size)
19526 error (_("Offset from %s pointing outside of"
19527 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19528 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19529 return (const char *) (str_section->buffer + str_offset);
19530 }
19531
19532 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19533
19534 static const char *
19535 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19536 {
19537 unsigned offset_size;
19538 ULONGEST str_offsets_base;
19539 if (reader->cu->header.version >= 5)
19540 {
19541 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
19542 so assume the .debug_str_offsets section is DWARF5 as well, and
19543 parse the header. FIXME: Parse the header only once. */
19544 unsigned int bytes_read = 0;
19545 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
19546 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
19547
19548 /* Header: Initial length. */
19549 read_initial_length (abfd, p + bytes_read, &bytes_read);
19550
19551 /* Determine offset_size based on the .debug_str_offsets header. */
19552 const bool dwarf5_is_dwarf64 = bytes_read != 4;
19553 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
19554
19555 /* Header: Version. */
19556 unsigned version = read_2_bytes (abfd, p + bytes_read);
19557 bytes_read += 2;
19558
19559 if (version <= 4)
19560 {
19561 /* We'd like one warning here about ignoring the section, but
19562 because we parse the header more than once (see FIXME above)
19563 we'd have many warnings, so use a complaint instead, which at
19564 least has a limit. */
19565 complaint (_("Section .debug_str_offsets in %s has unsupported"
19566 " version %d, use empty string."),
19567 reader->dwo_file->dwo_name, version);
19568 return "";
19569 }
19570
19571 /* Header: Padding. */
19572 bytes_read += 2;
19573
19574 str_offsets_base = bytes_read;
19575 }
19576 else
19577 {
19578 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
19579 section, assume the .debug_str_offsets section is pre-DWARF5 as
19580 well, which doesn't have a header. */
19581 str_offsets_base = 0;
19582
19583 /* Determine offset_size based on the .debug_info header. */
19584 offset_size = reader->cu->header.offset_size;
19585 }
19586
19587 return read_str_index (reader->cu,
19588 &reader->dwo_file->sections.str,
19589 &reader->dwo_file->sections.str_offsets,
19590 str_offsets_base, str_index, offset_size);
19591 }
19592
19593 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19594
19595 static const char *
19596 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19597 {
19598 struct objfile *objfile = cu->per_objfile->objfile;
19599 const char *objf_name = objfile_name (objfile);
19600 static const char form_name[] = "DW_FORM_GNU_str_index";
19601 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19602
19603 if (!cu->str_offsets_base.has_value ())
19604 error (_("%s used in Fission stub without %s"
19605 " in CU at offset 0x%lx [in module %s]"),
19606 form_name, str_offsets_attr_name,
19607 (long) cu->header.offset_size, objf_name);
19608
19609 return read_str_index (cu,
19610 &cu->per_objfile->per_bfd->str,
19611 &cu->per_objfile->per_bfd->str_offsets,
19612 *cu->str_offsets_base, str_index,
19613 cu->header.offset_size);
19614 }
19615
19616 /* Return the length of an LEB128 number in BUF. */
19617
19618 static int
19619 leb128_size (const gdb_byte *buf)
19620 {
19621 const gdb_byte *begin = buf;
19622 gdb_byte byte;
19623
19624 while (1)
19625 {
19626 byte = *buf++;
19627 if ((byte & 128) == 0)
19628 return buf - begin;
19629 }
19630 }
19631
19632 static enum language
19633 dwarf_lang_to_enum_language (unsigned int lang)
19634 {
19635 enum language language;
19636
19637 switch (lang)
19638 {
19639 case DW_LANG_C89:
19640 case DW_LANG_C99:
19641 case DW_LANG_C11:
19642 case DW_LANG_C:
19643 case DW_LANG_UPC:
19644 language = language_c;
19645 break;
19646 case DW_LANG_Java:
19647 case DW_LANG_C_plus_plus:
19648 case DW_LANG_C_plus_plus_11:
19649 case DW_LANG_C_plus_plus_14:
19650 language = language_cplus;
19651 break;
19652 case DW_LANG_D:
19653 language = language_d;
19654 break;
19655 case DW_LANG_Fortran77:
19656 case DW_LANG_Fortran90:
19657 case DW_LANG_Fortran95:
19658 case DW_LANG_Fortran03:
19659 case DW_LANG_Fortran08:
19660 language = language_fortran;
19661 break;
19662 case DW_LANG_Go:
19663 language = language_go;
19664 break;
19665 case DW_LANG_Mips_Assembler:
19666 language = language_asm;
19667 break;
19668 case DW_LANG_Ada83:
19669 case DW_LANG_Ada95:
19670 language = language_ada;
19671 break;
19672 case DW_LANG_Modula2:
19673 language = language_m2;
19674 break;
19675 case DW_LANG_Pascal83:
19676 language = language_pascal;
19677 break;
19678 case DW_LANG_ObjC:
19679 language = language_objc;
19680 break;
19681 case DW_LANG_Rust:
19682 case DW_LANG_Rust_old:
19683 language = language_rust;
19684 break;
19685 case DW_LANG_OpenCL:
19686 language = language_opencl;
19687 break;
19688 case DW_LANG_Cobol74:
19689 case DW_LANG_Cobol85:
19690 default:
19691 language = language_minimal;
19692 break;
19693 }
19694
19695 return language;
19696 }
19697
19698 /* Return the named attribute or NULL if not there. */
19699
19700 static struct attribute *
19701 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19702 {
19703 for (;;)
19704 {
19705 unsigned int i;
19706 struct attribute *spec = NULL;
19707
19708 for (i = 0; i < die->num_attrs; ++i)
19709 {
19710 if (die->attrs[i].name == name)
19711 return &die->attrs[i];
19712 if (die->attrs[i].name == DW_AT_specification
19713 || die->attrs[i].name == DW_AT_abstract_origin)
19714 spec = &die->attrs[i];
19715 }
19716
19717 if (!spec)
19718 break;
19719
19720 die = follow_die_ref (die, spec, &cu);
19721 }
19722
19723 return NULL;
19724 }
19725
19726 /* Return the string associated with a string-typed attribute, or NULL if it
19727 is either not found or is of an incorrect type. */
19728
19729 static const char *
19730 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19731 {
19732 struct attribute *attr;
19733 const char *str = NULL;
19734
19735 attr = dwarf2_attr (die, name, cu);
19736
19737 if (attr != NULL)
19738 {
19739 str = attr->as_string ();
19740 if (str == nullptr)
19741 complaint (_("string type expected for attribute %s for "
19742 "DIE at %s in module %s"),
19743 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19744 objfile_name (cu->per_objfile->objfile));
19745 }
19746
19747 return str;
19748 }
19749
19750 /* Return the dwo name or NULL if not present. If present, it is in either
19751 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19752 static const char *
19753 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19754 {
19755 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19756 if (dwo_name == nullptr)
19757 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19758 return dwo_name;
19759 }
19760
19761 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19762 and holds a non-zero value. This function should only be used for
19763 DW_FORM_flag or DW_FORM_flag_present attributes. */
19764
19765 static int
19766 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19767 {
19768 struct attribute *attr = dwarf2_attr (die, name, cu);
19769
19770 return attr != nullptr && attr->as_boolean ();
19771 }
19772
19773 static int
19774 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19775 {
19776 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19777 which value is non-zero. However, we have to be careful with
19778 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19779 (via dwarf2_flag_true_p) follows this attribute. So we may
19780 end up accidently finding a declaration attribute that belongs
19781 to a different DIE referenced by the specification attribute,
19782 even though the given DIE does not have a declaration attribute. */
19783 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19784 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19785 }
19786
19787 /* Return the die giving the specification for DIE, if there is
19788 one. *SPEC_CU is the CU containing DIE on input, and the CU
19789 containing the return value on output. If there is no
19790 specification, but there is an abstract origin, that is
19791 returned. */
19792
19793 static struct die_info *
19794 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19795 {
19796 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19797 *spec_cu);
19798
19799 if (spec_attr == NULL)
19800 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19801
19802 if (spec_attr == NULL)
19803 return NULL;
19804 else
19805 return follow_die_ref (die, spec_attr, spec_cu);
19806 }
19807
19808 /* A convenience function to find the proper .debug_line section for a CU. */
19809
19810 static struct dwarf2_section_info *
19811 get_debug_line_section (struct dwarf2_cu *cu)
19812 {
19813 struct dwarf2_section_info *section;
19814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19815
19816 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19817 DWO file. */
19818 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19819 section = &cu->dwo_unit->dwo_file->sections.line;
19820 else if (cu->per_cu->is_dwz)
19821 {
19822 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19823
19824 section = &dwz->line;
19825 }
19826 else
19827 section = &per_objfile->per_bfd->line;
19828
19829 return section;
19830 }
19831
19832 /* Read the statement program header starting at OFFSET in
19833 .debug_line, or .debug_line.dwo. Return a pointer
19834 to a struct line_header, allocated using xmalloc.
19835 Returns NULL if there is a problem reading the header, e.g., if it
19836 has a version we don't understand.
19837
19838 NOTE: the strings in the include directory and file name tables of
19839 the returned object point into the dwarf line section buffer,
19840 and must not be freed. */
19841
19842 static line_header_up
19843 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
19844 const char *comp_dir)
19845 {
19846 struct dwarf2_section_info *section;
19847 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19848
19849 section = get_debug_line_section (cu);
19850 section->read (per_objfile->objfile);
19851 if (section->buffer == NULL)
19852 {
19853 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19854 complaint (_("missing .debug_line.dwo section"));
19855 else
19856 complaint (_("missing .debug_line section"));
19857 return 0;
19858 }
19859
19860 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19861 per_objfile, section, &cu->header,
19862 comp_dir);
19863 }
19864
19865 /* Subroutine of dwarf_decode_lines to simplify it.
19866 Return the file name for the given file_entry.
19867 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19868 If space for the result is malloc'd, *NAME_HOLDER will be set.
19869 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19870 equivalent to CU_INFO. */
19871
19872 static const char *
19873 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
19874 const file_and_directory &cu_info,
19875 std::string &name_holder)
19876 {
19877 const char *include_name = fe.name;
19878 const char *include_name_to_compare = include_name;
19879
19880 const char *dir_name = fe.include_dir (lh);
19881
19882 std::string hold_compare;
19883 if (!IS_ABSOLUTE_PATH (include_name)
19884 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
19885 {
19886 /* Avoid creating a duplicate name for CU_INFO.
19887 We do this by comparing INCLUDE_NAME and CU_INFO.
19888 Before we do the comparison, however, we need to account
19889 for DIR_NAME and COMP_DIR.
19890 First prepend dir_name (if non-NULL). If we still don't
19891 have an absolute path prepend comp_dir (if non-NULL).
19892 However, the directory we record in the include-file's
19893 psymtab does not contain COMP_DIR (to match the
19894 corresponding symtab(s)).
19895
19896 Example:
19897
19898 bash$ cd /tmp
19899 bash$ gcc -g ./hello.c
19900 include_name = "hello.c"
19901 dir_name = "."
19902 DW_AT_comp_dir = comp_dir = "/tmp"
19903 DW_AT_name = "./hello.c"
19904
19905 */
19906
19907 if (dir_name != NULL)
19908 {
19909 name_holder = path_join (dir_name, include_name);
19910 include_name = name_holder.c_str ();
19911 include_name_to_compare = include_name;
19912 }
19913 if (!IS_ABSOLUTE_PATH (include_name)
19914 && cu_info.get_comp_dir () != nullptr)
19915 {
19916 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
19917 include_name_to_compare = hold_compare.c_str ();
19918 }
19919 }
19920
19921 std::string copied_name;
19922 const char *cu_filename = cu_info.get_name ();
19923 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
19924 {
19925 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
19926 cu_filename = copied_name.c_str ();
19927 }
19928
19929 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
19930 return nullptr;
19931 return include_name;
19932 }
19933
19934 /* State machine to track the state of the line number program. */
19935
19936 class lnp_state_machine
19937 {
19938 public:
19939 /* Initialize a machine state for the start of a line number
19940 program. */
19941 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
19942
19943 file_entry *current_file ()
19944 {
19945 /* lh->file_names is 0-based, but the file name numbers in the
19946 statement program are 1-based. */
19947 return m_line_header->file_name_at (m_file);
19948 }
19949
19950 /* Record the line in the state machine. END_SEQUENCE is true if
19951 we're processing the end of a sequence. */
19952 void record_line (bool end_sequence);
19953
19954 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19955 nop-out rest of the lines in this sequence. */
19956 void check_line_address (struct dwarf2_cu *cu,
19957 const gdb_byte *line_ptr,
19958 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19959
19960 void handle_set_discriminator (unsigned int discriminator)
19961 {
19962 m_discriminator = discriminator;
19963 m_line_has_non_zero_discriminator |= discriminator != 0;
19964 }
19965
19966 /* Handle DW_LNE_set_address. */
19967 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19968 {
19969 m_op_index = 0;
19970 address += baseaddr;
19971 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19972 }
19973
19974 /* Handle DW_LNS_advance_pc. */
19975 void handle_advance_pc (CORE_ADDR adjust);
19976
19977 /* Handle a special opcode. */
19978 void handle_special_opcode (unsigned char op_code);
19979
19980 /* Handle DW_LNS_advance_line. */
19981 void handle_advance_line (int line_delta)
19982 {
19983 advance_line (line_delta);
19984 }
19985
19986 /* Handle DW_LNS_set_file. */
19987 void handle_set_file (file_name_index file);
19988
19989 /* Handle DW_LNS_negate_stmt. */
19990 void handle_negate_stmt ()
19991 {
19992 m_flags ^= LEF_IS_STMT;
19993 }
19994
19995 /* Handle DW_LNS_const_add_pc. */
19996 void handle_const_add_pc ();
19997
19998 /* Handle DW_LNS_fixed_advance_pc. */
19999 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20000 {
20001 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20002 m_op_index = 0;
20003 }
20004
20005 /* Handle DW_LNS_copy. */
20006 void handle_copy ()
20007 {
20008 record_line (false);
20009 m_discriminator = 0;
20010 m_flags &= ~LEF_PROLOGUE_END;
20011 }
20012
20013 /* Handle DW_LNE_end_sequence. */
20014 void handle_end_sequence ()
20015 {
20016 m_currently_recording_lines = true;
20017 }
20018
20019 /* Handle DW_LNS_set_prologue_end. */
20020 void handle_set_prologue_end ()
20021 {
20022 m_flags |= LEF_PROLOGUE_END;
20023 }
20024
20025 private:
20026 /* Advance the line by LINE_DELTA. */
20027 void advance_line (int line_delta)
20028 {
20029 m_line += line_delta;
20030
20031 if (line_delta != 0)
20032 m_line_has_non_zero_discriminator = m_discriminator != 0;
20033 }
20034
20035 struct dwarf2_cu *m_cu;
20036
20037 gdbarch *m_gdbarch;
20038
20039 /* The line number header. */
20040 line_header *m_line_header;
20041
20042 /* These are part of the standard DWARF line number state machine,
20043 and initialized according to the DWARF spec. */
20044
20045 unsigned char m_op_index = 0;
20046 /* The line table index of the current file. */
20047 file_name_index m_file = 1;
20048 unsigned int m_line = 1;
20049
20050 /* These are initialized in the constructor. */
20051
20052 CORE_ADDR m_address;
20053 linetable_entry_flags m_flags;
20054 unsigned int m_discriminator = 0;
20055
20056 /* Additional bits of state we need to track. */
20057
20058 /* The last file that we called dwarf2_start_subfile for.
20059 This is only used for TLLs. */
20060 unsigned int m_last_file = 0;
20061 /* The last file a line number was recorded for. */
20062 struct subfile *m_last_subfile = NULL;
20063
20064 /* The address of the last line entry. */
20065 CORE_ADDR m_last_address;
20066
20067 /* Set to true when a previous line at the same address (using
20068 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
20069 when a line entry at a new address (m_address different to
20070 m_last_address) is processed. */
20071 bool m_stmt_at_address = false;
20072
20073 /* When true, record the lines we decode. */
20074 bool m_currently_recording_lines = true;
20075
20076 /* The last line number that was recorded, used to coalesce
20077 consecutive entries for the same line. This can happen, for
20078 example, when discriminators are present. PR 17276. */
20079 unsigned int m_last_line = 0;
20080 bool m_line_has_non_zero_discriminator = false;
20081 };
20082
20083 void
20084 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20085 {
20086 CORE_ADDR addr_adj = (((m_op_index + adjust)
20087 / m_line_header->maximum_ops_per_instruction)
20088 * m_line_header->minimum_instruction_length);
20089 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20090 m_op_index = ((m_op_index + adjust)
20091 % m_line_header->maximum_ops_per_instruction);
20092 }
20093
20094 void
20095 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20096 {
20097 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20098 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20099 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20100 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20101 / m_line_header->maximum_ops_per_instruction)
20102 * m_line_header->minimum_instruction_length);
20103 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20104 m_op_index = ((m_op_index + adj_opcode_d)
20105 % m_line_header->maximum_ops_per_instruction);
20106
20107 int line_delta = m_line_header->line_base + adj_opcode_r;
20108 advance_line (line_delta);
20109 record_line (false);
20110 m_discriminator = 0;
20111 m_flags &= ~LEF_PROLOGUE_END;
20112 }
20113
20114 void
20115 lnp_state_machine::handle_set_file (file_name_index file)
20116 {
20117 m_file = file;
20118
20119 const file_entry *fe = current_file ();
20120 if (fe == NULL)
20121 dwarf2_debug_line_missing_file_complaint ();
20122 else
20123 {
20124 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20125 m_line_has_non_zero_discriminator = m_discriminator != 0;
20126 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
20127 }
20128 }
20129
20130 void
20131 lnp_state_machine::handle_const_add_pc ()
20132 {
20133 CORE_ADDR adjust
20134 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20135
20136 CORE_ADDR addr_adj
20137 = (((m_op_index + adjust)
20138 / m_line_header->maximum_ops_per_instruction)
20139 * m_line_header->minimum_instruction_length);
20140
20141 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20142 m_op_index = ((m_op_index + adjust)
20143 % m_line_header->maximum_ops_per_instruction);
20144 }
20145
20146 /* Return non-zero if we should add LINE to the line number table.
20147 LINE is the line to add, LAST_LINE is the last line that was added,
20148 LAST_SUBFILE is the subfile for LAST_LINE.
20149 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20150 had a non-zero discriminator.
20151
20152 We have to be careful in the presence of discriminators.
20153 E.g., for this line:
20154
20155 for (i = 0; i < 100000; i++);
20156
20157 clang can emit four line number entries for that one line,
20158 each with a different discriminator.
20159 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20160
20161 However, we want gdb to coalesce all four entries into one.
20162 Otherwise the user could stepi into the middle of the line and
20163 gdb would get confused about whether the pc really was in the
20164 middle of the line.
20165
20166 Things are further complicated by the fact that two consecutive
20167 line number entries for the same line is a heuristic used by gcc
20168 to denote the end of the prologue. So we can't just discard duplicate
20169 entries, we have to be selective about it. The heuristic we use is
20170 that we only collapse consecutive entries for the same line if at least
20171 one of those entries has a non-zero discriminator. PR 17276.
20172
20173 Note: Addresses in the line number state machine can never go backwards
20174 within one sequence, thus this coalescing is ok. */
20175
20176 static int
20177 dwarf_record_line_p (struct dwarf2_cu *cu,
20178 unsigned int line, unsigned int last_line,
20179 int line_has_non_zero_discriminator,
20180 struct subfile *last_subfile)
20181 {
20182 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20183 return 1;
20184 if (line != last_line)
20185 return 1;
20186 /* Same line for the same file that we've seen already.
20187 As a last check, for pr 17276, only record the line if the line
20188 has never had a non-zero discriminator. */
20189 if (!line_has_non_zero_discriminator)
20190 return 1;
20191 return 0;
20192 }
20193
20194 /* Use the CU's builder to record line number LINE beginning at
20195 address ADDRESS in the line table of subfile SUBFILE. */
20196
20197 static void
20198 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20199 unsigned int line, CORE_ADDR address,
20200 linetable_entry_flags flags,
20201 struct dwarf2_cu *cu)
20202 {
20203 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20204
20205 if (dwarf_line_debug)
20206 {
20207 gdb_printf (gdb_stdlog,
20208 "Recording line %u, file %s, address %s\n",
20209 line, lbasename (subfile->name.c_str ()),
20210 paddress (gdbarch, address));
20211 }
20212
20213 if (cu != nullptr)
20214 cu->get_builder ()->record_line (subfile, line, addr, flags);
20215 }
20216
20217 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20218 Mark the end of a set of line number records.
20219 The arguments are the same as for dwarf_record_line_1.
20220 If SUBFILE is NULL the request is ignored. */
20221
20222 static void
20223 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20224 CORE_ADDR address, struct dwarf2_cu *cu)
20225 {
20226 if (subfile == NULL)
20227 return;
20228
20229 if (dwarf_line_debug)
20230 {
20231 gdb_printf (gdb_stdlog,
20232 "Finishing current line, file %s, address %s\n",
20233 lbasename (subfile->name.c_str ()),
20234 paddress (gdbarch, address));
20235 }
20236
20237 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
20238 }
20239
20240 void
20241 lnp_state_machine::record_line (bool end_sequence)
20242 {
20243 if (dwarf_line_debug)
20244 {
20245 gdb_printf (gdb_stdlog,
20246 "Processing actual line %u: file %u,"
20247 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20248 m_line, m_file,
20249 paddress (m_gdbarch, m_address),
20250 (m_flags & LEF_IS_STMT) != 0,
20251 (m_flags & LEF_PROLOGUE_END) != 0,
20252 m_discriminator,
20253 (end_sequence ? "\t(end sequence)" : ""));
20254 }
20255
20256 file_entry *fe = current_file ();
20257
20258 if (fe == NULL)
20259 dwarf2_debug_line_missing_file_complaint ();
20260 /* For now we ignore lines not starting on an instruction boundary.
20261 But not when processing end_sequence for compatibility with the
20262 previous version of the code. */
20263 else if (m_op_index == 0 || end_sequence)
20264 {
20265 /* When we switch files we insert an end maker in the first file,
20266 switch to the second file and add a new line entry. The
20267 problem is that the end marker inserted in the first file will
20268 discard any previous line entries at the same address. If the
20269 line entries in the first file are marked as is-stmt, while
20270 the new line in the second file is non-stmt, then this means
20271 the end marker will discard is-stmt lines so we can have a
20272 non-stmt line. This means that there are less addresses at
20273 which the user can insert a breakpoint.
20274
20275 To improve this we track the last address in m_last_address,
20276 and whether we have seen an is-stmt at this address. Then
20277 when switching files, if we have seen a stmt at the current
20278 address, and we are switching to create a non-stmt line, then
20279 discard the new line. */
20280 bool file_changed
20281 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20282 bool ignore_this_line
20283 = ((file_changed && !end_sequence && m_last_address == m_address
20284 && ((m_flags & LEF_IS_STMT) == 0)
20285 && m_stmt_at_address)
20286 || (!end_sequence && m_line == 0));
20287
20288 if ((file_changed && !ignore_this_line) || end_sequence)
20289 {
20290 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20291 m_currently_recording_lines ? m_cu : nullptr);
20292 }
20293
20294 if (!end_sequence && !ignore_this_line)
20295 {
20296 linetable_entry_flags lte_flags = m_flags;
20297 if (producer_is_codewarrior (m_cu))
20298 lte_flags |= LEF_IS_STMT;
20299
20300 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20301 m_line_has_non_zero_discriminator,
20302 m_last_subfile))
20303 {
20304 buildsym_compunit *builder = m_cu->get_builder ();
20305 dwarf_record_line_1 (m_gdbarch,
20306 builder->get_current_subfile (),
20307 m_line, m_address, lte_flags,
20308 m_currently_recording_lines ? m_cu : nullptr);
20309 }
20310 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20311 m_last_line = m_line;
20312 }
20313 }
20314
20315 /* Track whether we have seen any IS_STMT true at m_address in case we
20316 have multiple line table entries all at m_address. */
20317 if (m_last_address != m_address)
20318 {
20319 m_stmt_at_address = false;
20320 m_last_address = m_address;
20321 }
20322 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
20323 }
20324
20325 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20326 line_header *lh)
20327 : m_cu (cu),
20328 m_gdbarch (arch),
20329 m_line_header (lh),
20330 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
20331 if there was a line entry for it so that the backend has a
20332 chance to adjust it and also record it in case it needs it.
20333 This is currently used by MIPS code,
20334 cf. `mips_adjust_dwarf2_line'. */
20335 m_address (gdbarch_adjust_dwarf2_line (arch, 0, 0)),
20336 m_flags (lh->default_is_stmt ? LEF_IS_STMT : (linetable_entry_flags) 0),
20337 m_last_address (m_address)
20338 {
20339 }
20340
20341 void
20342 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20343 const gdb_byte *line_ptr,
20344 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20345 {
20346 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20347 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20348 located at 0x0. In this case, additionally check that if
20349 ADDRESS < UNRELOCATED_LOWPC. */
20350
20351 if ((address == 0 && address < unrelocated_lowpc)
20352 || address == (CORE_ADDR) -1)
20353 {
20354 /* This line table is for a function which has been
20355 GCd by the linker. Ignore it. PR gdb/12528 */
20356
20357 struct objfile *objfile = cu->per_objfile->objfile;
20358 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20359
20360 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20361 line_offset, objfile_name (objfile));
20362 m_currently_recording_lines = false;
20363 /* Note: m_currently_recording_lines is left as false until we see
20364 DW_LNE_end_sequence. */
20365 }
20366 }
20367
20368 /* Subroutine of dwarf_decode_lines to simplify it.
20369 Process the line number information in LH. */
20370
20371 static void
20372 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20373 CORE_ADDR lowpc)
20374 {
20375 const gdb_byte *line_ptr, *extended_end;
20376 const gdb_byte *line_end;
20377 unsigned int bytes_read, extended_len;
20378 unsigned char op_code, extended_op;
20379 CORE_ADDR baseaddr;
20380 struct objfile *objfile = cu->per_objfile->objfile;
20381 bfd *abfd = objfile->obfd.get ();
20382 struct gdbarch *gdbarch = objfile->arch ();
20383
20384 baseaddr = objfile->text_section_offset ();
20385
20386 line_ptr = lh->statement_program_start;
20387 line_end = lh->statement_program_end;
20388
20389 /* Read the statement sequences until there's nothing left. */
20390 while (line_ptr < line_end)
20391 {
20392 /* The DWARF line number program state machine. Reset the state
20393 machine at the start of each sequence. */
20394 lnp_state_machine state_machine (cu, gdbarch, lh);
20395 bool end_sequence = false;
20396
20397 /* Start a subfile for the current file of the state
20398 machine. */
20399 const file_entry *fe = state_machine.current_file ();
20400
20401 if (fe != NULL)
20402 dwarf2_start_subfile (cu, *fe, *lh);
20403
20404 /* Decode the table. */
20405 while (line_ptr < line_end && !end_sequence)
20406 {
20407 op_code = read_1_byte (abfd, line_ptr);
20408 line_ptr += 1;
20409
20410 if (op_code >= lh->opcode_base)
20411 {
20412 /* Special opcode. */
20413 state_machine.handle_special_opcode (op_code);
20414 }
20415 else switch (op_code)
20416 {
20417 case DW_LNS_extended_op:
20418 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20419 &bytes_read);
20420 line_ptr += bytes_read;
20421 extended_end = line_ptr + extended_len;
20422 extended_op = read_1_byte (abfd, line_ptr);
20423 line_ptr += 1;
20424 if (DW_LNE_lo_user <= extended_op
20425 && extended_op <= DW_LNE_hi_user)
20426 {
20427 /* Vendor extension, ignore. */
20428 line_ptr = extended_end;
20429 break;
20430 }
20431 switch (extended_op)
20432 {
20433 case DW_LNE_end_sequence:
20434 state_machine.handle_end_sequence ();
20435 end_sequence = true;
20436 break;
20437 case DW_LNE_set_address:
20438 {
20439 CORE_ADDR address
20440 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20441 line_ptr += bytes_read;
20442
20443 state_machine.check_line_address (cu, line_ptr,
20444 lowpc - baseaddr, address);
20445 state_machine.handle_set_address (baseaddr, address);
20446 }
20447 break;
20448 case DW_LNE_define_file:
20449 {
20450 const char *cur_file;
20451 unsigned int mod_time, length;
20452 dir_index dindex;
20453
20454 cur_file = read_direct_string (abfd, line_ptr,
20455 &bytes_read);
20456 line_ptr += bytes_read;
20457 dindex = (dir_index)
20458 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20459 line_ptr += bytes_read;
20460 mod_time =
20461 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20462 line_ptr += bytes_read;
20463 length =
20464 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20465 line_ptr += bytes_read;
20466 lh->add_file_name (cur_file, dindex, mod_time, length);
20467 }
20468 break;
20469 case DW_LNE_set_discriminator:
20470 {
20471 /* The discriminator is not interesting to the
20472 debugger; just ignore it. We still need to
20473 check its value though:
20474 if there are consecutive entries for the same
20475 (non-prologue) line we want to coalesce them.
20476 PR 17276. */
20477 unsigned int discr
20478 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20479 line_ptr += bytes_read;
20480
20481 state_machine.handle_set_discriminator (discr);
20482 }
20483 break;
20484 default:
20485 complaint (_("mangled .debug_line section"));
20486 return;
20487 }
20488 /* Make sure that we parsed the extended op correctly. If e.g.
20489 we expected a different address size than the producer used,
20490 we may have read the wrong number of bytes. */
20491 if (line_ptr != extended_end)
20492 {
20493 complaint (_("mangled .debug_line section"));
20494 return;
20495 }
20496 break;
20497 case DW_LNS_copy:
20498 state_machine.handle_copy ();
20499 break;
20500 case DW_LNS_advance_pc:
20501 {
20502 CORE_ADDR adjust
20503 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20504 line_ptr += bytes_read;
20505
20506 state_machine.handle_advance_pc (adjust);
20507 }
20508 break;
20509 case DW_LNS_advance_line:
20510 {
20511 int line_delta
20512 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20513 line_ptr += bytes_read;
20514
20515 state_machine.handle_advance_line (line_delta);
20516 }
20517 break;
20518 case DW_LNS_set_file:
20519 {
20520 file_name_index file
20521 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20522 &bytes_read);
20523 line_ptr += bytes_read;
20524
20525 state_machine.handle_set_file (file);
20526 }
20527 break;
20528 case DW_LNS_set_column:
20529 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20530 line_ptr += bytes_read;
20531 break;
20532 case DW_LNS_negate_stmt:
20533 state_machine.handle_negate_stmt ();
20534 break;
20535 case DW_LNS_set_basic_block:
20536 break;
20537 /* Add to the address register of the state machine the
20538 address increment value corresponding to special opcode
20539 255. I.e., this value is scaled by the minimum
20540 instruction length since special opcode 255 would have
20541 scaled the increment. */
20542 case DW_LNS_const_add_pc:
20543 state_machine.handle_const_add_pc ();
20544 break;
20545 case DW_LNS_fixed_advance_pc:
20546 {
20547 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20548 line_ptr += 2;
20549
20550 state_machine.handle_fixed_advance_pc (addr_adj);
20551 }
20552 break;
20553 case DW_LNS_set_prologue_end:
20554 state_machine.handle_set_prologue_end ();
20555 break;
20556 default:
20557 {
20558 /* Unknown standard opcode, ignore it. */
20559 int i;
20560
20561 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20562 {
20563 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20564 line_ptr += bytes_read;
20565 }
20566 }
20567 }
20568 }
20569
20570 if (!end_sequence)
20571 dwarf2_debug_line_missing_end_sequence_complaint ();
20572
20573 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20574 in which case we still finish recording the last line). */
20575 state_machine.record_line (true);
20576 }
20577 }
20578
20579 /* Decode the Line Number Program (LNP) for the given line_header
20580 structure and CU. The actual information extracted and the type
20581 of structures created from the LNP depends on the value of PST.
20582
20583 FND holds the CU file name and directory, if known.
20584 It is used for relative paths in the line table.
20585
20586 NOTE: It is important that psymtabs have the same file name (via
20587 strcmp) as the corresponding symtab. Since the directory is not
20588 used in the name of the symtab we don't use it in the name of the
20589 psymtabs we create. E.g. expand_line_sal requires this when
20590 finding psymtabs to expand. A good testcase for this is
20591 mb-inline.exp.
20592
20593 LOWPC is the lowest address in CU (or 0 if not known).
20594
20595 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20596 for its PC<->lines mapping information. Otherwise only the filename
20597 table is read in. */
20598
20599 static void
20600 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
20601 CORE_ADDR lowpc, int decode_mapping)
20602 {
20603 if (decode_mapping)
20604 dwarf_decode_lines_1 (lh, cu, lowpc);
20605
20606 /* Make sure a symtab is created for every file, even files
20607 which contain only variables (i.e. no code with associated
20608 line numbers). */
20609 buildsym_compunit *builder = cu->get_builder ();
20610 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20611
20612 for (auto &fe : lh->file_names ())
20613 {
20614 dwarf2_start_subfile (cu, fe, *lh);
20615 subfile *sf = builder->get_current_subfile ();
20616
20617 if (sf->symtab == nullptr)
20618 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
20619 sf->name_for_id.c_str ());
20620
20621 fe.symtab = sf->symtab;
20622 }
20623 }
20624
20625 /* Start a subfile for DWARF. FILENAME is the name of the file and
20626 DIRNAME the name of the source directory which contains FILENAME
20627 or NULL if not known.
20628 This routine tries to keep line numbers from identical absolute and
20629 relative file names in a common subfile.
20630
20631 Using the `list' example from the GDB testsuite, which resides in
20632 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20633 of /srcdir/list0.c yields the following debugging information for list0.c:
20634
20635 DW_AT_name: /srcdir/list0.c
20636 DW_AT_comp_dir: /compdir
20637 files.files[0].name: list0.h
20638 files.files[0].dir: /srcdir
20639 files.files[1].name: list0.c
20640 files.files[1].dir: /srcdir
20641
20642 The line number information for list0.c has to end up in a single
20643 subfile, so that `break /srcdir/list0.c:1' works as expected.
20644 start_subfile will ensure that this happens provided that we pass the
20645 concatenation of files.files[1].dir and files.files[1].name as the
20646 subfile's name. */
20647
20648 static void
20649 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
20650 const line_header &lh)
20651 {
20652 std::string filename_holder;
20653 const char *filename = fe.name;
20654 const char *dirname = lh.include_dir_at (fe.d_index);
20655
20656 /* In order not to lose the line information directory,
20657 we concatenate it to the filename when it makes sense.
20658 Note that the Dwarf3 standard says (speaking of filenames in line
20659 information): ``The directory index is ignored for file names
20660 that represent full path names''. Thus ignoring dirname in the
20661 `else' branch below isn't an issue. */
20662
20663 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20664 {
20665 filename_holder = path_join (dirname, filename);
20666 filename = filename_holder.c_str ();
20667 }
20668
20669 std::string filename_for_id = lh.file_file_name (fe);
20670 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
20671 }
20672
20673 static void
20674 var_decode_location (struct attribute *attr, struct symbol *sym,
20675 struct dwarf2_cu *cu)
20676 {
20677 struct objfile *objfile = cu->per_objfile->objfile;
20678 struct comp_unit_head *cu_header = &cu->header;
20679
20680 /* NOTE drow/2003-01-30: There used to be a comment and some special
20681 code here to turn a symbol with DW_AT_external and a
20682 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20683 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20684 with some versions of binutils) where shared libraries could have
20685 relocations against symbols in their debug information - the
20686 minimal symbol would have the right address, but the debug info
20687 would not. It's no longer necessary, because we will explicitly
20688 apply relocations when we read in the debug information now. */
20689
20690 /* A DW_AT_location attribute with no contents indicates that a
20691 variable has been optimized away. */
20692 if (attr->form_is_block () && attr->as_block ()->size == 0)
20693 {
20694 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20695 return;
20696 }
20697
20698 /* Handle one degenerate form of location expression specially, to
20699 preserve GDB's previous behavior when section offsets are
20700 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20701 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20702
20703 if (attr->form_is_block ())
20704 {
20705 struct dwarf_block *block = attr->as_block ();
20706
20707 if ((block->data[0] == DW_OP_addr
20708 && block->size == 1 + cu_header->addr_size)
20709 || ((block->data[0] == DW_OP_GNU_addr_index
20710 || block->data[0] == DW_OP_addrx)
20711 && (block->size
20712 == 1 + leb128_size (&block->data[1]))))
20713 {
20714 unsigned int dummy;
20715
20716 if (block->data[0] == DW_OP_addr)
20717 sym->set_value_address
20718 (cu->header.read_address (objfile->obfd.get (), block->data + 1,
20719 &dummy));
20720 else
20721 sym->set_value_address
20722 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
20723 sym->set_aclass_index (LOC_STATIC);
20724 fixup_symbol_section (sym, objfile);
20725 sym->set_value_address
20726 (sym->value_address ()
20727 + objfile->section_offsets[sym->section_index ()]);
20728 return;
20729 }
20730 }
20731
20732 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20733 expression evaluator, and use LOC_COMPUTED only when necessary
20734 (i.e. when the value of a register or memory location is
20735 referenced, or a thread-local block, etc.). Then again, it might
20736 not be worthwhile. I'm assuming that it isn't unless performance
20737 or memory numbers show me otherwise. */
20738
20739 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20740
20741 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20742 cu->has_loclist = true;
20743 }
20744
20745 /* Given a pointer to a DWARF information entry, figure out if we need
20746 to make a symbol table entry for it, and if so, create a new entry
20747 and return a pointer to it.
20748 If TYPE is NULL, determine symbol type from the die, otherwise
20749 used the passed type.
20750 If SPACE is not NULL, use it to hold the new symbol. If it is
20751 NULL, allocate a new symbol on the objfile's obstack. */
20752
20753 static struct symbol *
20754 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20755 struct symbol *space)
20756 {
20757 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20758 struct objfile *objfile = per_objfile->objfile;
20759 struct gdbarch *gdbarch = objfile->arch ();
20760 struct symbol *sym = NULL;
20761 const char *name;
20762 struct attribute *attr = NULL;
20763 struct attribute *attr2 = NULL;
20764 CORE_ADDR baseaddr;
20765 struct pending **list_to_add = NULL;
20766
20767 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20768
20769 baseaddr = objfile->text_section_offset ();
20770
20771 name = dwarf2_name (die, cu);
20772 if (name == nullptr && (die->tag == DW_TAG_subprogram
20773 || die->tag == DW_TAG_inlined_subroutine
20774 || die->tag == DW_TAG_entry_point))
20775 name = dw2_linkage_name (die, cu);
20776
20777 if (name)
20778 {
20779 int suppress_add = 0;
20780
20781 if (space)
20782 sym = space;
20783 else
20784 sym = new (&objfile->objfile_obstack) symbol;
20785 OBJSTAT (objfile, n_syms++);
20786
20787 /* Cache this symbol's name and the name's demangled form (if any). */
20788 sym->set_language (cu->lang (), &objfile->objfile_obstack);
20789 /* Fortran does not have mangling standard and the mangling does differ
20790 between gfortran, iFort etc. */
20791 const char *physname
20792 = (cu->lang () == language_fortran
20793 ? dwarf2_full_name (name, die, cu)
20794 : dwarf2_physname (name, die, cu));
20795 const char *linkagename = dw2_linkage_name (die, cu);
20796
20797 if (linkagename == nullptr || cu->lang () == language_ada)
20798 sym->set_linkage_name (physname);
20799 else
20800 {
20801 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20802 sym->set_linkage_name (linkagename);
20803 }
20804
20805 /* Handle DW_AT_artificial. */
20806 attr = dwarf2_attr (die, DW_AT_artificial, cu);
20807 if (attr != nullptr)
20808 sym->set_is_artificial (attr->as_boolean ());
20809
20810 /* Default assumptions.
20811 Use the passed type or decode it from the die. */
20812 sym->set_domain (VAR_DOMAIN);
20813 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20814 if (type != NULL)
20815 sym->set_type (type);
20816 else
20817 sym->set_type (die_type (die, cu));
20818 attr = dwarf2_attr (die,
20819 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20820 cu);
20821 if (attr != nullptr)
20822 sym->set_line (attr->constant_value (0));
20823
20824 attr = dwarf2_attr (die,
20825 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20826 cu);
20827 if (attr != nullptr && attr->is_nonnegative ())
20828 {
20829 file_name_index file_index
20830 = (file_name_index) attr->as_nonnegative ();
20831 struct file_entry *fe;
20832
20833 if (cu->line_header != NULL)
20834 fe = cu->line_header->file_name_at (file_index);
20835 else
20836 fe = NULL;
20837
20838 if (fe == NULL)
20839 complaint (_("file index out of range"));
20840 else
20841 sym->set_symtab (fe->symtab);
20842 }
20843
20844 switch (die->tag)
20845 {
20846 case DW_TAG_label:
20847 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20848 if (attr != nullptr)
20849 {
20850 CORE_ADDR addr;
20851
20852 addr = attr->as_address ();
20853 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20854 sym->set_section_index (SECT_OFF_TEXT (objfile));
20855 sym->set_value_address (addr);
20856 sym->set_aclass_index (LOC_LABEL);
20857 }
20858 else
20859 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20860 sym->set_type (objfile_type (objfile)->builtin_core_addr);
20861 sym->set_domain (LABEL_DOMAIN);
20862 add_symbol_to_list (sym, cu->list_in_scope);
20863 break;
20864 case DW_TAG_subprogram:
20865 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20866 finish_block. */
20867 sym->set_aclass_index (LOC_BLOCK);
20868 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20869 if ((attr2 != nullptr && attr2->as_boolean ())
20870 || cu->lang () == language_ada
20871 || cu->lang () == language_fortran)
20872 {
20873 /* Subprograms marked external are stored as a global symbol.
20874 Ada and Fortran subprograms, whether marked external or
20875 not, are always stored as a global symbol, because we want
20876 to be able to access them globally. For instance, we want
20877 to be able to break on a nested subprogram without having
20878 to specify the context. */
20879 list_to_add = cu->get_builder ()->get_global_symbols ();
20880 }
20881 else
20882 {
20883 list_to_add = cu->list_in_scope;
20884 }
20885 break;
20886 case DW_TAG_inlined_subroutine:
20887 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20888 finish_block. */
20889 sym->set_aclass_index (LOC_BLOCK);
20890 sym->set_is_inlined (1);
20891 list_to_add = cu->list_in_scope;
20892 break;
20893 case DW_TAG_template_value_param:
20894 suppress_add = 1;
20895 /* Fall through. */
20896 case DW_TAG_constant:
20897 case DW_TAG_variable:
20898 case DW_TAG_member:
20899 /* Compilation with minimal debug info may result in
20900 variables with missing type entries. Change the
20901 misleading `void' type to something sensible. */
20902 if (sym->type ()->code () == TYPE_CODE_VOID)
20903 sym->set_type (objfile_type (objfile)->builtin_int);
20904
20905 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20906 /* In the case of DW_TAG_member, we should only be called for
20907 static const members. */
20908 if (die->tag == DW_TAG_member)
20909 {
20910 /* dwarf2_add_field uses die_is_declaration,
20911 so we do the same. */
20912 gdb_assert (die_is_declaration (die, cu));
20913 gdb_assert (attr);
20914 }
20915 if (attr != nullptr)
20916 {
20917 dwarf2_const_value (attr, sym, cu);
20918 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20919 if (!suppress_add)
20920 {
20921 if (attr2 != nullptr && attr2->as_boolean ())
20922 list_to_add = cu->get_builder ()->get_global_symbols ();
20923 else
20924 list_to_add = cu->list_in_scope;
20925 }
20926 break;
20927 }
20928 attr = dwarf2_attr (die, DW_AT_location, cu);
20929 if (attr != nullptr)
20930 {
20931 var_decode_location (attr, sym, cu);
20932 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20933
20934 /* Fortran explicitly imports any global symbols to the local
20935 scope by DW_TAG_common_block. */
20936 if (cu->lang () == language_fortran && die->parent
20937 && die->parent->tag == DW_TAG_common_block)
20938 attr2 = NULL;
20939
20940 if (sym->aclass () == LOC_STATIC
20941 && sym->value_address () == 0
20942 && !per_objfile->per_bfd->has_section_at_zero)
20943 {
20944 /* When a static variable is eliminated by the linker,
20945 the corresponding debug information is not stripped
20946 out, but the variable address is set to null;
20947 do not add such variables into symbol table. */
20948 }
20949 else if (attr2 != nullptr && attr2->as_boolean ())
20950 {
20951 if (sym->aclass () == LOC_STATIC
20952 && (objfile->flags & OBJF_MAINLINE) == 0
20953 && per_objfile->per_bfd->can_copy)
20954 {
20955 /* A global static variable might be subject to
20956 copy relocation. We first check for a local
20957 minsym, though, because maybe the symbol was
20958 marked hidden, in which case this would not
20959 apply. */
20960 bound_minimal_symbol found
20961 = (lookup_minimal_symbol_linkage
20962 (sym->linkage_name (), objfile));
20963 if (found.minsym != nullptr)
20964 sym->maybe_copied = 1;
20965 }
20966
20967 /* A variable with DW_AT_external is never static,
20968 but it may be block-scoped. */
20969 list_to_add
20970 = ((cu->list_in_scope
20971 == cu->get_builder ()->get_file_symbols ())
20972 ? cu->get_builder ()->get_global_symbols ()
20973 : cu->list_in_scope);
20974 }
20975 else
20976 list_to_add = cu->list_in_scope;
20977 }
20978 else
20979 {
20980 /* We do not know the address of this symbol.
20981 If it is an external symbol and we have type information
20982 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20983 The address of the variable will then be determined from
20984 the minimal symbol table whenever the variable is
20985 referenced. */
20986 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20987
20988 /* Fortran explicitly imports any global symbols to the local
20989 scope by DW_TAG_common_block. */
20990 if (cu->lang () == language_fortran && die->parent
20991 && die->parent->tag == DW_TAG_common_block)
20992 {
20993 /* SYMBOL_CLASS doesn't matter here because
20994 read_common_block is going to reset it. */
20995 if (!suppress_add)
20996 list_to_add = cu->list_in_scope;
20997 }
20998 else if (attr2 != nullptr && attr2->as_boolean ()
20999 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21000 {
21001 /* A variable with DW_AT_external is never static, but it
21002 may be block-scoped. */
21003 list_to_add
21004 = ((cu->list_in_scope
21005 == cu->get_builder ()->get_file_symbols ())
21006 ? cu->get_builder ()->get_global_symbols ()
21007 : cu->list_in_scope);
21008
21009 sym->set_aclass_index (LOC_UNRESOLVED);
21010 }
21011 else if (!die_is_declaration (die, cu))
21012 {
21013 /* Use the default LOC_OPTIMIZED_OUT class. */
21014 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
21015 if (!suppress_add)
21016 list_to_add = cu->list_in_scope;
21017 }
21018 }
21019 break;
21020 case DW_TAG_formal_parameter:
21021 {
21022 /* If we are inside a function, mark this as an argument. If
21023 not, we might be looking at an argument to an inlined function
21024 when we do not have enough information to show inlined frames;
21025 pretend it's a local variable in that case so that the user can
21026 still see it. */
21027 struct context_stack *curr
21028 = cu->get_builder ()->get_current_context_stack ();
21029 if (curr != nullptr && curr->name != nullptr)
21030 sym->set_is_argument (1);
21031 attr = dwarf2_attr (die, DW_AT_location, cu);
21032 if (attr != nullptr)
21033 {
21034 var_decode_location (attr, sym, cu);
21035 }
21036 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21037 if (attr != nullptr)
21038 {
21039 dwarf2_const_value (attr, sym, cu);
21040 }
21041
21042 list_to_add = cu->list_in_scope;
21043 }
21044 break;
21045 case DW_TAG_unspecified_parameters:
21046 /* From varargs functions; gdb doesn't seem to have any
21047 interest in this information, so just ignore it for now.
21048 (FIXME?) */
21049 break;
21050 case DW_TAG_template_type_param:
21051 suppress_add = 1;
21052 /* Fall through. */
21053 case DW_TAG_class_type:
21054 case DW_TAG_interface_type:
21055 case DW_TAG_structure_type:
21056 case DW_TAG_union_type:
21057 case DW_TAG_set_type:
21058 case DW_TAG_enumeration_type:
21059 case DW_TAG_namelist:
21060 if (die->tag == DW_TAG_namelist)
21061 {
21062 sym->set_aclass_index (LOC_STATIC);
21063 sym->set_domain (VAR_DOMAIN);
21064 }
21065 else
21066 {
21067 sym->set_aclass_index (LOC_TYPEDEF);
21068 sym->set_domain (STRUCT_DOMAIN);
21069 }
21070 {
21071 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21072 really ever be static objects: otherwise, if you try
21073 to, say, break of a class's method and you're in a file
21074 which doesn't mention that class, it won't work unless
21075 the check for all static symbols in lookup_symbol_aux
21076 saves you. See the OtherFileClass tests in
21077 gdb.c++/namespace.exp. */
21078
21079 if (!suppress_add)
21080 {
21081 buildsym_compunit *builder = cu->get_builder ();
21082 list_to_add
21083 = (cu->list_in_scope == builder->get_file_symbols ()
21084 && cu->lang () == language_cplus
21085 ? builder->get_global_symbols ()
21086 : cu->list_in_scope);
21087
21088 /* The semantics of C++ state that "struct foo {
21089 ... }" also defines a typedef for "foo". */
21090 if (cu->lang () == language_cplus
21091 || cu->lang () == language_ada
21092 || cu->lang () == language_d
21093 || cu->lang () == language_rust)
21094 {
21095 /* The symbol's name is already allocated along
21096 with this objfile, so we don't need to
21097 duplicate it for the type. */
21098 if (sym->type ()->name () == 0)
21099 sym->type ()->set_name (sym->search_name ());
21100 }
21101 }
21102 }
21103 break;
21104 case DW_TAG_typedef:
21105 sym->set_aclass_index (LOC_TYPEDEF);
21106 sym->set_domain (VAR_DOMAIN);
21107 list_to_add = cu->list_in_scope;
21108 break;
21109 case DW_TAG_array_type:
21110 case DW_TAG_base_type:
21111 case DW_TAG_subrange_type:
21112 case DW_TAG_generic_subrange:
21113 case DW_TAG_unspecified_type:
21114 sym->set_aclass_index (LOC_TYPEDEF);
21115 sym->set_domain (VAR_DOMAIN);
21116 list_to_add = cu->list_in_scope;
21117 break;
21118 case DW_TAG_enumerator:
21119 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21120 if (attr != nullptr)
21121 {
21122 dwarf2_const_value (attr, sym, cu);
21123 }
21124 {
21125 /* NOTE: carlton/2003-11-10: See comment above in the
21126 DW_TAG_class_type, etc. block. */
21127
21128 list_to_add
21129 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21130 && cu->lang () == language_cplus
21131 ? cu->get_builder ()->get_global_symbols ()
21132 : cu->list_in_scope);
21133 }
21134 break;
21135 case DW_TAG_imported_declaration:
21136 case DW_TAG_namespace:
21137 sym->set_aclass_index (LOC_TYPEDEF);
21138 list_to_add = cu->get_builder ()->get_global_symbols ();
21139 break;
21140 case DW_TAG_module:
21141 sym->set_aclass_index (LOC_TYPEDEF);
21142 sym->set_domain (MODULE_DOMAIN);
21143 list_to_add = cu->get_builder ()->get_global_symbols ();
21144 break;
21145 case DW_TAG_common_block:
21146 sym->set_aclass_index (LOC_COMMON_BLOCK);
21147 sym->set_domain (COMMON_BLOCK_DOMAIN);
21148 add_symbol_to_list (sym, cu->list_in_scope);
21149 break;
21150 default:
21151 /* Not a tag we recognize. Hopefully we aren't processing
21152 trash data, but since we must specifically ignore things
21153 we don't recognize, there is nothing else we should do at
21154 this point. */
21155 complaint (_("unsupported tag: '%s'"),
21156 dwarf_tag_name (die->tag));
21157 break;
21158 }
21159
21160 if (suppress_add)
21161 {
21162 sym->hash_next = objfile->template_symbols;
21163 objfile->template_symbols = sym;
21164 list_to_add = NULL;
21165 }
21166
21167 if (list_to_add != NULL)
21168 add_symbol_to_list (sym, list_to_add);
21169
21170 /* For the benefit of old versions of GCC, check for anonymous
21171 namespaces based on the demangled name. */
21172 if (!cu->processing_has_namespace_info
21173 && cu->lang () == language_cplus)
21174 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21175 }
21176 return (sym);
21177 }
21178
21179 /* Given an attr with a DW_FORM_dataN value in host byte order,
21180 zero-extend it as appropriate for the symbol's type. The DWARF
21181 standard (v4) is not entirely clear about the meaning of using
21182 DW_FORM_dataN for a constant with a signed type, where the type is
21183 wider than the data. The conclusion of a discussion on the DWARF
21184 list was that this is unspecified. We choose to always zero-extend
21185 because that is the interpretation long in use by GCC. */
21186
21187 static gdb_byte *
21188 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21189 struct dwarf2_cu *cu, LONGEST *value, int bits)
21190 {
21191 struct objfile *objfile = cu->per_objfile->objfile;
21192 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
21193 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21194 LONGEST l = attr->constant_value (0);
21195
21196 if (bits < sizeof (*value) * 8)
21197 {
21198 l &= ((LONGEST) 1 << bits) - 1;
21199 *value = l;
21200 }
21201 else if (bits == sizeof (*value) * 8)
21202 *value = l;
21203 else
21204 {
21205 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21206 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21207 return bytes;
21208 }
21209
21210 return NULL;
21211 }
21212
21213 /* Read a constant value from an attribute. Either set *VALUE, or if
21214 the value does not fit in *VALUE, set *BYTES - either already
21215 allocated on the objfile obstack, or newly allocated on OBSTACK,
21216 or, set *BATON, if we translated the constant to a location
21217 expression. */
21218
21219 static void
21220 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21221 const char *name, struct obstack *obstack,
21222 struct dwarf2_cu *cu,
21223 LONGEST *value, const gdb_byte **bytes,
21224 struct dwarf2_locexpr_baton **baton)
21225 {
21226 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21227 struct objfile *objfile = per_objfile->objfile;
21228 struct comp_unit_head *cu_header = &cu->header;
21229 struct dwarf_block *blk;
21230 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
21231 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21232
21233 *value = 0;
21234 *bytes = NULL;
21235 *baton = NULL;
21236
21237 switch (attr->form)
21238 {
21239 case DW_FORM_addr:
21240 case DW_FORM_addrx:
21241 case DW_FORM_GNU_addr_index:
21242 {
21243 gdb_byte *data;
21244
21245 if (type->length () != cu_header->addr_size)
21246 dwarf2_const_value_length_mismatch_complaint (name,
21247 cu_header->addr_size,
21248 type->length ());
21249 /* Symbols of this form are reasonably rare, so we just
21250 piggyback on the existing location code rather than writing
21251 a new implementation of symbol_computed_ops. */
21252 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21253 (*baton)->per_objfile = per_objfile;
21254 (*baton)->per_cu = cu->per_cu;
21255 gdb_assert ((*baton)->per_cu);
21256
21257 (*baton)->size = 2 + cu_header->addr_size;
21258 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21259 (*baton)->data = data;
21260
21261 data[0] = DW_OP_addr;
21262 store_unsigned_integer (&data[1], cu_header->addr_size,
21263 byte_order, attr->as_address ());
21264 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21265 }
21266 break;
21267 case DW_FORM_string:
21268 case DW_FORM_strp:
21269 case DW_FORM_strx:
21270 case DW_FORM_GNU_str_index:
21271 case DW_FORM_GNU_strp_alt:
21272 /* The string is already allocated on the objfile obstack, point
21273 directly to it. */
21274 *bytes = (const gdb_byte *) attr->as_string ();
21275 break;
21276 case DW_FORM_block1:
21277 case DW_FORM_block2:
21278 case DW_FORM_block4:
21279 case DW_FORM_block:
21280 case DW_FORM_exprloc:
21281 case DW_FORM_data16:
21282 blk = attr->as_block ();
21283 if (type->length () != blk->size)
21284 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21285 type->length ());
21286 *bytes = blk->data;
21287 break;
21288
21289 /* The DW_AT_const_value attributes are supposed to carry the
21290 symbol's value "represented as it would be on the target
21291 architecture." By the time we get here, it's already been
21292 converted to host endianness, so we just need to sign- or
21293 zero-extend it as appropriate. */
21294 case DW_FORM_data1:
21295 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21296 break;
21297 case DW_FORM_data2:
21298 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21299 break;
21300 case DW_FORM_data4:
21301 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21302 break;
21303 case DW_FORM_data8:
21304 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21305 break;
21306
21307 case DW_FORM_sdata:
21308 case DW_FORM_implicit_const:
21309 *value = attr->as_signed ();
21310 break;
21311
21312 case DW_FORM_udata:
21313 *value = attr->as_unsigned ();
21314 break;
21315
21316 default:
21317 complaint (_("unsupported const value attribute form: '%s'"),
21318 dwarf_form_name (attr->form));
21319 *value = 0;
21320 break;
21321 }
21322 }
21323
21324
21325 /* Copy constant value from an attribute to a symbol. */
21326
21327 static void
21328 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21329 struct dwarf2_cu *cu)
21330 {
21331 struct objfile *objfile = cu->per_objfile->objfile;
21332 LONGEST value;
21333 const gdb_byte *bytes;
21334 struct dwarf2_locexpr_baton *baton;
21335
21336 dwarf2_const_value_attr (attr, sym->type (),
21337 sym->print_name (),
21338 &objfile->objfile_obstack, cu,
21339 &value, &bytes, &baton);
21340
21341 if (baton != NULL)
21342 {
21343 SYMBOL_LOCATION_BATON (sym) = baton;
21344 sym->set_aclass_index (dwarf2_locexpr_index);
21345 }
21346 else if (bytes != NULL)
21347 {
21348 sym->set_value_bytes (bytes);
21349 sym->set_aclass_index (LOC_CONST_BYTES);
21350 }
21351 else
21352 {
21353 sym->set_value_longest (value);
21354 sym->set_aclass_index (LOC_CONST);
21355 }
21356 }
21357
21358 /* Return the type of the die in question using its DW_AT_type attribute. */
21359
21360 static struct type *
21361 die_type (struct die_info *die, struct dwarf2_cu *cu)
21362 {
21363 struct attribute *type_attr;
21364
21365 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21366 if (!type_attr)
21367 {
21368 struct objfile *objfile = cu->per_objfile->objfile;
21369 /* A missing DW_AT_type represents a void type. */
21370 return objfile_type (objfile)->builtin_void;
21371 }
21372
21373 return lookup_die_type (die, type_attr, cu);
21374 }
21375
21376 /* True iff CU's producer generates GNAT Ada auxiliary information
21377 that allows to find parallel types through that information instead
21378 of having to do expensive parallel lookups by type name. */
21379
21380 static int
21381 need_gnat_info (struct dwarf2_cu *cu)
21382 {
21383 /* Assume that the Ada compiler was GNAT, which always produces
21384 the auxiliary information. */
21385 return (cu->lang () == language_ada);
21386 }
21387
21388 /* Return the auxiliary type of the die in question using its
21389 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21390 attribute is not present. */
21391
21392 static struct type *
21393 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21394 {
21395 struct attribute *type_attr;
21396
21397 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21398 if (!type_attr)
21399 return NULL;
21400
21401 return lookup_die_type (die, type_attr, cu);
21402 }
21403
21404 /* If DIE has a descriptive_type attribute, then set the TYPE's
21405 descriptive type accordingly. */
21406
21407 static void
21408 set_descriptive_type (struct type *type, struct die_info *die,
21409 struct dwarf2_cu *cu)
21410 {
21411 struct type *descriptive_type = die_descriptive_type (die, cu);
21412
21413 if (descriptive_type)
21414 {
21415 ALLOCATE_GNAT_AUX_TYPE (type);
21416 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21417 }
21418 }
21419
21420 /* Return the containing type of the die in question using its
21421 DW_AT_containing_type attribute. */
21422
21423 static struct type *
21424 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21425 {
21426 struct attribute *type_attr;
21427 struct objfile *objfile = cu->per_objfile->objfile;
21428
21429 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21430 if (!type_attr)
21431 error (_("Dwarf Error: Problem turning containing type into gdb type "
21432 "[in module %s]"), objfile_name (objfile));
21433
21434 return lookup_die_type (die, type_attr, cu);
21435 }
21436
21437 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21438
21439 static struct type *
21440 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21441 {
21442 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21443 struct objfile *objfile = per_objfile->objfile;
21444 char *saved;
21445
21446 std::string message
21447 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21448 objfile_name (objfile),
21449 sect_offset_str (cu->header.sect_off),
21450 sect_offset_str (die->sect_off));
21451 saved = obstack_strdup (&objfile->objfile_obstack, message);
21452
21453 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21454 }
21455
21456 /* Look up the type of DIE in CU using its type attribute ATTR.
21457 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21458 DW_AT_containing_type.
21459 If there is no type substitute an error marker. */
21460
21461 static struct type *
21462 lookup_die_type (struct die_info *die, const struct attribute *attr,
21463 struct dwarf2_cu *cu)
21464 {
21465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21466 struct objfile *objfile = per_objfile->objfile;
21467 struct type *this_type;
21468
21469 gdb_assert (attr->name == DW_AT_type
21470 || attr->name == DW_AT_GNAT_descriptive_type
21471 || attr->name == DW_AT_containing_type);
21472
21473 /* First see if we have it cached. */
21474
21475 if (attr->form == DW_FORM_GNU_ref_alt)
21476 {
21477 struct dwarf2_per_cu_data *per_cu;
21478 sect_offset sect_off = attr->get_ref_die_offset ();
21479
21480 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21481 per_objfile->per_bfd);
21482 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21483 }
21484 else if (attr->form_is_ref ())
21485 {
21486 sect_offset sect_off = attr->get_ref_die_offset ();
21487
21488 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21489 }
21490 else if (attr->form == DW_FORM_ref_sig8)
21491 {
21492 ULONGEST signature = attr->as_signature ();
21493
21494 return get_signatured_type (die, signature, cu);
21495 }
21496 else
21497 {
21498 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21499 " at %s [in module %s]"),
21500 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21501 objfile_name (objfile));
21502 return build_error_marker_type (cu, die);
21503 }
21504
21505 /* If not cached we need to read it in. */
21506
21507 if (this_type == NULL)
21508 {
21509 struct die_info *type_die = NULL;
21510 struct dwarf2_cu *type_cu = cu;
21511
21512 if (attr->form_is_ref ())
21513 type_die = follow_die_ref (die, attr, &type_cu);
21514 if (type_die == NULL)
21515 return build_error_marker_type (cu, die);
21516 /* If we find the type now, it's probably because the type came
21517 from an inter-CU reference and the type's CU got expanded before
21518 ours. */
21519 this_type = read_type_die (type_die, type_cu);
21520 }
21521
21522 /* If we still don't have a type use an error marker. */
21523
21524 if (this_type == NULL)
21525 return build_error_marker_type (cu, die);
21526
21527 return this_type;
21528 }
21529
21530 /* Return the type in DIE, CU.
21531 Returns NULL for invalid types.
21532
21533 This first does a lookup in die_type_hash,
21534 and only reads the die in if necessary.
21535
21536 NOTE: This can be called when reading in partial or full symbols. */
21537
21538 static struct type *
21539 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21540 {
21541 struct type *this_type;
21542
21543 this_type = get_die_type (die, cu);
21544 if (this_type)
21545 return this_type;
21546
21547 return read_type_die_1 (die, cu);
21548 }
21549
21550 /* Read the type in DIE, CU.
21551 Returns NULL for invalid types. */
21552
21553 static struct type *
21554 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21555 {
21556 struct type *this_type = NULL;
21557
21558 switch (die->tag)
21559 {
21560 case DW_TAG_class_type:
21561 case DW_TAG_interface_type:
21562 case DW_TAG_structure_type:
21563 case DW_TAG_union_type:
21564 this_type = read_structure_type (die, cu);
21565 break;
21566 case DW_TAG_enumeration_type:
21567 this_type = read_enumeration_type (die, cu);
21568 break;
21569 case DW_TAG_subprogram:
21570 case DW_TAG_subroutine_type:
21571 case DW_TAG_inlined_subroutine:
21572 this_type = read_subroutine_type (die, cu);
21573 break;
21574 case DW_TAG_array_type:
21575 this_type = read_array_type (die, cu);
21576 break;
21577 case DW_TAG_set_type:
21578 this_type = read_set_type (die, cu);
21579 break;
21580 case DW_TAG_pointer_type:
21581 this_type = read_tag_pointer_type (die, cu);
21582 break;
21583 case DW_TAG_ptr_to_member_type:
21584 this_type = read_tag_ptr_to_member_type (die, cu);
21585 break;
21586 case DW_TAG_reference_type:
21587 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21588 break;
21589 case DW_TAG_rvalue_reference_type:
21590 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21591 break;
21592 case DW_TAG_const_type:
21593 this_type = read_tag_const_type (die, cu);
21594 break;
21595 case DW_TAG_volatile_type:
21596 this_type = read_tag_volatile_type (die, cu);
21597 break;
21598 case DW_TAG_restrict_type:
21599 this_type = read_tag_restrict_type (die, cu);
21600 break;
21601 case DW_TAG_string_type:
21602 this_type = read_tag_string_type (die, cu);
21603 break;
21604 case DW_TAG_typedef:
21605 this_type = read_typedef (die, cu);
21606 break;
21607 case DW_TAG_generic_subrange:
21608 case DW_TAG_subrange_type:
21609 this_type = read_subrange_type (die, cu);
21610 break;
21611 case DW_TAG_base_type:
21612 this_type = read_base_type (die, cu);
21613 break;
21614 case DW_TAG_unspecified_type:
21615 this_type = read_unspecified_type (die, cu);
21616 break;
21617 case DW_TAG_namespace:
21618 this_type = read_namespace_type (die, cu);
21619 break;
21620 case DW_TAG_module:
21621 this_type = read_module_type (die, cu);
21622 break;
21623 case DW_TAG_atomic_type:
21624 this_type = read_tag_atomic_type (die, cu);
21625 break;
21626 default:
21627 complaint (_("unexpected tag in read_type_die: '%s'"),
21628 dwarf_tag_name (die->tag));
21629 break;
21630 }
21631
21632 return this_type;
21633 }
21634
21635 /* See if we can figure out if the class lives in a namespace. We do
21636 this by looking for a member function; its demangled name will
21637 contain namespace info, if there is any.
21638 Return the computed name or NULL.
21639 Space for the result is allocated on the objfile's obstack.
21640 This is the full-die version of guess_partial_die_structure_name.
21641 In this case we know DIE has no useful parent. */
21642
21643 static const char *
21644 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21645 {
21646 struct die_info *spec_die;
21647 struct dwarf2_cu *spec_cu;
21648 struct die_info *child;
21649 struct objfile *objfile = cu->per_objfile->objfile;
21650
21651 spec_cu = cu;
21652 spec_die = die_specification (die, &spec_cu);
21653 if (spec_die != NULL)
21654 {
21655 die = spec_die;
21656 cu = spec_cu;
21657 }
21658
21659 for (child = die->child;
21660 child != NULL;
21661 child = child->sibling)
21662 {
21663 if (child->tag == DW_TAG_subprogram)
21664 {
21665 const char *linkage_name = dw2_linkage_name (child, cu);
21666
21667 if (linkage_name != NULL)
21668 {
21669 gdb::unique_xmalloc_ptr<char> actual_name
21670 (cu->language_defn->class_name_from_physname (linkage_name));
21671 const char *name = NULL;
21672
21673 if (actual_name != NULL)
21674 {
21675 const char *die_name = dwarf2_name (die, cu);
21676
21677 if (die_name != NULL
21678 && strcmp (die_name, actual_name.get ()) != 0)
21679 {
21680 /* Strip off the class name from the full name.
21681 We want the prefix. */
21682 int die_name_len = strlen (die_name);
21683 int actual_name_len = strlen (actual_name.get ());
21684 const char *ptr = actual_name.get ();
21685
21686 /* Test for '::' as a sanity check. */
21687 if (actual_name_len > die_name_len + 2
21688 && ptr[actual_name_len - die_name_len - 1] == ':')
21689 name = obstack_strndup (
21690 &objfile->per_bfd->storage_obstack,
21691 ptr, actual_name_len - die_name_len - 2);
21692 }
21693 }
21694 return name;
21695 }
21696 }
21697 }
21698
21699 return NULL;
21700 }
21701
21702 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21703 prefix part in such case. See
21704 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21705
21706 static const char *
21707 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21708 {
21709 struct attribute *attr;
21710 const char *base;
21711
21712 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21713 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21714 return NULL;
21715
21716 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21717 return NULL;
21718
21719 attr = dw2_linkage_name_attr (die, cu);
21720 const char *attr_name = attr->as_string ();
21721 if (attr == NULL || attr_name == NULL)
21722 return NULL;
21723
21724 /* dwarf2_name had to be already called. */
21725 gdb_assert (attr->canonical_string_p ());
21726
21727 /* Strip the base name, keep any leading namespaces/classes. */
21728 base = strrchr (attr_name, ':');
21729 if (base == NULL || base == attr_name || base[-1] != ':')
21730 return "";
21731
21732 struct objfile *objfile = cu->per_objfile->objfile;
21733 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21734 attr_name,
21735 &base[-1] - attr_name);
21736 }
21737
21738 /* Return the name of the namespace/class that DIE is defined within,
21739 or "" if we can't tell. The caller should not xfree the result.
21740
21741 For example, if we're within the method foo() in the following
21742 code:
21743
21744 namespace N {
21745 class C {
21746 void foo () {
21747 }
21748 };
21749 }
21750
21751 then determine_prefix on foo's die will return "N::C". */
21752
21753 static const char *
21754 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21755 {
21756 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21757 struct die_info *parent, *spec_die;
21758 struct dwarf2_cu *spec_cu;
21759 struct type *parent_type;
21760 const char *retval;
21761
21762 if (cu->lang () != language_cplus
21763 && cu->lang () != language_fortran
21764 && cu->lang () != language_d
21765 && cu->lang () != language_rust)
21766 return "";
21767
21768 retval = anonymous_struct_prefix (die, cu);
21769 if (retval)
21770 return retval;
21771
21772 /* We have to be careful in the presence of DW_AT_specification.
21773 For example, with GCC 3.4, given the code
21774
21775 namespace N {
21776 void foo() {
21777 // Definition of N::foo.
21778 }
21779 }
21780
21781 then we'll have a tree of DIEs like this:
21782
21783 1: DW_TAG_compile_unit
21784 2: DW_TAG_namespace // N
21785 3: DW_TAG_subprogram // declaration of N::foo
21786 4: DW_TAG_subprogram // definition of N::foo
21787 DW_AT_specification // refers to die #3
21788
21789 Thus, when processing die #4, we have to pretend that we're in
21790 the context of its DW_AT_specification, namely the contex of die
21791 #3. */
21792 spec_cu = cu;
21793 spec_die = die_specification (die, &spec_cu);
21794 if (spec_die == NULL)
21795 parent = die->parent;
21796 else
21797 {
21798 parent = spec_die->parent;
21799 cu = spec_cu;
21800 }
21801
21802 if (parent == NULL)
21803 return "";
21804 else if (parent->building_fullname)
21805 {
21806 const char *name;
21807 const char *parent_name;
21808
21809 /* It has been seen on RealView 2.2 built binaries,
21810 DW_TAG_template_type_param types actually _defined_ as
21811 children of the parent class:
21812
21813 enum E {};
21814 template class <class Enum> Class{};
21815 Class<enum E> class_e;
21816
21817 1: DW_TAG_class_type (Class)
21818 2: DW_TAG_enumeration_type (E)
21819 3: DW_TAG_enumerator (enum1:0)
21820 3: DW_TAG_enumerator (enum2:1)
21821 ...
21822 2: DW_TAG_template_type_param
21823 DW_AT_type DW_FORM_ref_udata (E)
21824
21825 Besides being broken debug info, it can put GDB into an
21826 infinite loop. Consider:
21827
21828 When we're building the full name for Class<E>, we'll start
21829 at Class, and go look over its template type parameters,
21830 finding E. We'll then try to build the full name of E, and
21831 reach here. We're now trying to build the full name of E,
21832 and look over the parent DIE for containing scope. In the
21833 broken case, if we followed the parent DIE of E, we'd again
21834 find Class, and once again go look at its template type
21835 arguments, etc., etc. Simply don't consider such parent die
21836 as source-level parent of this die (it can't be, the language
21837 doesn't allow it), and break the loop here. */
21838 name = dwarf2_name (die, cu);
21839 parent_name = dwarf2_name (parent, cu);
21840 complaint (_("template param type '%s' defined within parent '%s'"),
21841 name ? name : "<unknown>",
21842 parent_name ? parent_name : "<unknown>");
21843 return "";
21844 }
21845 else
21846 switch (parent->tag)
21847 {
21848 case DW_TAG_namespace:
21849 parent_type = read_type_die (parent, cu);
21850 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21851 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21852 Work around this problem here. */
21853 if (cu->lang () == language_cplus
21854 && strcmp (parent_type->name (), "::") == 0)
21855 return "";
21856 /* We give a name to even anonymous namespaces. */
21857 return parent_type->name ();
21858 case DW_TAG_class_type:
21859 case DW_TAG_interface_type:
21860 case DW_TAG_structure_type:
21861 case DW_TAG_union_type:
21862 case DW_TAG_module:
21863 parent_type = read_type_die (parent, cu);
21864 if (parent_type->name () != NULL)
21865 return parent_type->name ();
21866 else
21867 /* An anonymous structure is only allowed non-static data
21868 members; no typedefs, no member functions, et cetera.
21869 So it does not need a prefix. */
21870 return "";
21871 case DW_TAG_compile_unit:
21872 case DW_TAG_partial_unit:
21873 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21874 if (cu->lang () == language_cplus
21875 && !per_objfile->per_bfd->types.empty ()
21876 && die->child != NULL
21877 && (die->tag == DW_TAG_class_type
21878 || die->tag == DW_TAG_structure_type
21879 || die->tag == DW_TAG_union_type))
21880 {
21881 const char *name = guess_full_die_structure_name (die, cu);
21882 if (name != NULL)
21883 return name;
21884 }
21885 return "";
21886 case DW_TAG_subprogram:
21887 /* Nested subroutines in Fortran get a prefix with the name
21888 of the parent's subroutine. */
21889 if (cu->lang () == language_fortran)
21890 {
21891 if ((die->tag == DW_TAG_subprogram)
21892 && (dwarf2_name (parent, cu) != NULL))
21893 return dwarf2_name (parent, cu);
21894 }
21895 return "";
21896 case DW_TAG_enumeration_type:
21897 parent_type = read_type_die (parent, cu);
21898 if (parent_type->is_declared_class ())
21899 {
21900 if (parent_type->name () != NULL)
21901 return parent_type->name ();
21902 return "";
21903 }
21904 /* Fall through. */
21905 default:
21906 return determine_prefix (parent, cu);
21907 }
21908 }
21909
21910 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21911 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21912 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21913 an obconcat, otherwise allocate storage for the result. The CU argument is
21914 used to determine the language and hence, the appropriate separator. */
21915
21916 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21917
21918 static char *
21919 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21920 int physname, struct dwarf2_cu *cu)
21921 {
21922 const char *lead = "";
21923 const char *sep;
21924
21925 if (suffix == NULL || suffix[0] == '\0'
21926 || prefix == NULL || prefix[0] == '\0')
21927 sep = "";
21928 else if (cu->lang () == language_d)
21929 {
21930 /* For D, the 'main' function could be defined in any module, but it
21931 should never be prefixed. */
21932 if (strcmp (suffix, "D main") == 0)
21933 {
21934 prefix = "";
21935 sep = "";
21936 }
21937 else
21938 sep = ".";
21939 }
21940 else if (cu->lang () == language_fortran && physname)
21941 {
21942 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21943 DW_AT_MIPS_linkage_name is preferred and used instead. */
21944
21945 lead = "__";
21946 sep = "_MOD_";
21947 }
21948 else
21949 sep = "::";
21950
21951 if (prefix == NULL)
21952 prefix = "";
21953 if (suffix == NULL)
21954 suffix = "";
21955
21956 if (obs == NULL)
21957 {
21958 char *retval
21959 = ((char *)
21960 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21961
21962 strcpy (retval, lead);
21963 strcat (retval, prefix);
21964 strcat (retval, sep);
21965 strcat (retval, suffix);
21966 return retval;
21967 }
21968 else
21969 {
21970 /* We have an obstack. */
21971 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21972 }
21973 }
21974
21975 /* Return a generic name for a DW_TAG_template_type_param or
21976 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
21977 per parent, so each function/class/struct template will have their own set
21978 of template parameters named <unnnamed0>, <unnamed1>, ... where the
21979 enumeration starts at 0 and represents the position of the template tag in
21980 the list of unnamed template tags for this parent, counting both, type and
21981 value tags. */
21982
21983 static const char *
21984 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
21985 {
21986 if (die->parent == nullptr)
21987 return nullptr;
21988
21989 /* Count the parent types unnamed template type and value children until, we
21990 arrive at our entry. */
21991 size_t nth_unnamed = 0;
21992
21993 die_info *child = die->parent->child;
21994 while (child != die)
21995 {
21996 gdb_assert (child != nullptr);
21997 if (child->tag == DW_TAG_template_type_param
21998 || child->tag == DW_TAG_template_value_param)
21999 {
22000 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
22001 ++nth_unnamed;
22002 }
22003 child = child->sibling;
22004 }
22005
22006 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
22007 return cu->per_objfile->objfile->intern (name_str.c_str ());
22008 }
22009
22010 /* Get name of a die, return NULL if not found. */
22011
22012 static const char *
22013 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22014 struct objfile *objfile)
22015 {
22016 if (name == nullptr)
22017 return name;
22018
22019 if (cu->lang () == language_cplus)
22020 {
22021 gdb::unique_xmalloc_ptr<char> canon_name
22022 = cp_canonicalize_string (name);
22023
22024 if (canon_name != nullptr)
22025 name = objfile->intern (canon_name.get ());
22026 }
22027 else if (cu->lang () == language_c)
22028 {
22029 gdb::unique_xmalloc_ptr<char> canon_name
22030 = c_canonicalize_name (name);
22031
22032 if (canon_name != nullptr)
22033 name = objfile->intern (canon_name.get ());
22034 }
22035
22036 return name;
22037 }
22038
22039 /* Get name of a die, return NULL if not found.
22040 Anonymous namespaces are converted to their magic string. */
22041
22042 static const char *
22043 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22044 {
22045 struct attribute *attr;
22046 struct objfile *objfile = cu->per_objfile->objfile;
22047
22048 attr = dwarf2_attr (die, DW_AT_name, cu);
22049 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22050 if (attr_name == nullptr
22051 && die->tag != DW_TAG_namespace
22052 && die->tag != DW_TAG_class_type
22053 && die->tag != DW_TAG_interface_type
22054 && die->tag != DW_TAG_structure_type
22055 && die->tag != DW_TAG_namelist
22056 && die->tag != DW_TAG_union_type
22057 && die->tag != DW_TAG_template_type_param
22058 && die->tag != DW_TAG_template_value_param)
22059 return NULL;
22060
22061 switch (die->tag)
22062 {
22063 /* A member's name should not be canonicalized. This is a bit
22064 of a hack, in that normally it should not be possible to run
22065 into this situation; however, the dw2-unusual-field-names.exp
22066 test creates custom DWARF that does. */
22067 case DW_TAG_member:
22068 case DW_TAG_compile_unit:
22069 case DW_TAG_partial_unit:
22070 /* Compilation units have a DW_AT_name that is a filename, not
22071 a source language identifier. */
22072 case DW_TAG_enumeration_type:
22073 case DW_TAG_enumerator:
22074 /* These tags always have simple identifiers already; no need
22075 to canonicalize them. */
22076 return attr_name;
22077
22078 case DW_TAG_namespace:
22079 if (attr_name != nullptr)
22080 return attr_name;
22081 return CP_ANONYMOUS_NAMESPACE_STR;
22082
22083 /* DWARF does not actually require template tags to have a name. */
22084 case DW_TAG_template_type_param:
22085 case DW_TAG_template_value_param:
22086 if (attr_name == nullptr)
22087 return unnamed_template_tag_name (die, cu);
22088 /* FALLTHROUGH. */
22089 case DW_TAG_class_type:
22090 case DW_TAG_interface_type:
22091 case DW_TAG_structure_type:
22092 case DW_TAG_union_type:
22093 case DW_TAG_namelist:
22094 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22095 structures or unions. These were of the form "._%d" in GCC 4.1,
22096 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22097 and GCC 4.4. We work around this problem by ignoring these. */
22098 if (attr_name != nullptr
22099 && (startswith (attr_name, "._")
22100 || startswith (attr_name, "<anonymous")))
22101 return NULL;
22102
22103 /* GCC might emit a nameless typedef that has a linkage name. See
22104 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22105 if (!attr || attr_name == NULL)
22106 {
22107 attr = dw2_linkage_name_attr (die, cu);
22108 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22109 if (attr == NULL || attr_name == NULL)
22110 return NULL;
22111
22112 /* Avoid demangling attr_name the second time on a second
22113 call for the same DIE. */
22114 if (!attr->canonical_string_p ())
22115 {
22116 gdb::unique_xmalloc_ptr<char> demangled
22117 (gdb_demangle (attr_name, DMGL_TYPES));
22118 if (demangled == nullptr)
22119 return nullptr;
22120
22121 attr->set_string_canonical (objfile->intern (demangled.get ()));
22122 attr_name = attr->as_string ();
22123 }
22124
22125 /* Strip any leading namespaces/classes, keep only the
22126 base name. DW_AT_name for named DIEs does not
22127 contain the prefixes. */
22128 const char *base = strrchr (attr_name, ':');
22129 if (base && base > attr_name && base[-1] == ':')
22130 return &base[1];
22131 else
22132 return attr_name;
22133 }
22134 break;
22135
22136 default:
22137 break;
22138 }
22139
22140 if (!attr->canonical_string_p ())
22141 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22142 objfile));
22143 return attr->as_string ();
22144 }
22145
22146 /* Return the die that this die in an extension of, or NULL if there
22147 is none. *EXT_CU is the CU containing DIE on input, and the CU
22148 containing the return value on output. */
22149
22150 static struct die_info *
22151 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22152 {
22153 struct attribute *attr;
22154
22155 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22156 if (attr == NULL)
22157 return NULL;
22158
22159 return follow_die_ref (die, attr, ext_cu);
22160 }
22161
22162 static void
22163 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22164 {
22165 void **slot;
22166
22167 slot = htab_find_slot_with_hash (cu->die_hash, die,
22168 to_underlying (die->sect_off),
22169 INSERT);
22170
22171 *slot = die;
22172 }
22173
22174 /* Follow reference or signature attribute ATTR of SRC_DIE.
22175 On entry *REF_CU is the CU of SRC_DIE.
22176 On exit *REF_CU is the CU of the result. */
22177
22178 static struct die_info *
22179 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22180 struct dwarf2_cu **ref_cu)
22181 {
22182 struct die_info *die;
22183
22184 if (attr->form_is_ref ())
22185 die = follow_die_ref (src_die, attr, ref_cu);
22186 else if (attr->form == DW_FORM_ref_sig8)
22187 die = follow_die_sig (src_die, attr, ref_cu);
22188 else
22189 {
22190 src_die->error_dump ();
22191 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22192 objfile_name ((*ref_cu)->per_objfile->objfile));
22193 }
22194
22195 return die;
22196 }
22197
22198 /* Follow reference OFFSET.
22199 On entry *REF_CU is the CU of the source die referencing OFFSET.
22200 On exit *REF_CU is the CU of the result.
22201 Returns NULL if OFFSET is invalid. */
22202
22203 static struct die_info *
22204 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22205 struct dwarf2_cu **ref_cu)
22206 {
22207 struct die_info temp_die;
22208 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22209 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22210
22211 gdb_assert (cu->per_cu != NULL);
22212
22213 target_cu = cu;
22214
22215 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22216 "source CU contains target offset: %d",
22217 sect_offset_str (cu->per_cu->sect_off),
22218 sect_offset_str (sect_off),
22219 cu->header.offset_in_cu_p (sect_off));
22220
22221 if (cu->per_cu->is_debug_types)
22222 {
22223 /* .debug_types CUs cannot reference anything outside their CU.
22224 If they need to, they have to reference a signatured type via
22225 DW_FORM_ref_sig8. */
22226 if (!cu->header.offset_in_cu_p (sect_off))
22227 return NULL;
22228 }
22229 else if (offset_in_dwz != cu->per_cu->is_dwz
22230 || !cu->header.offset_in_cu_p (sect_off))
22231 {
22232 struct dwarf2_per_cu_data *per_cu;
22233
22234 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22235 per_objfile->per_bfd);
22236
22237 dwarf_read_debug_printf_v ("target CU offset: %s, "
22238 "target CU DIEs loaded: %d",
22239 sect_offset_str (per_cu->sect_off),
22240 per_objfile->get_cu (per_cu) != nullptr);
22241
22242 /* If necessary, add it to the queue and load its DIEs.
22243
22244 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22245 it doesn't mean they are currently loaded. Since we require them
22246 to be loaded, we must check for ourselves. */
22247 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
22248 || per_objfile->get_cu (per_cu) == nullptr)
22249 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
22250 false, cu->lang ());
22251
22252 target_cu = per_objfile->get_cu (per_cu);
22253 gdb_assert (target_cu != nullptr);
22254 }
22255 else if (cu->dies == NULL)
22256 {
22257 /* We're loading full DIEs during partial symbol reading. */
22258 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
22259 language_minimal);
22260 }
22261
22262 *ref_cu = target_cu;
22263 temp_die.sect_off = sect_off;
22264
22265 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22266 &temp_die,
22267 to_underlying (sect_off));
22268 }
22269
22270 /* Follow reference attribute ATTR of SRC_DIE.
22271 On entry *REF_CU is the CU of SRC_DIE.
22272 On exit *REF_CU is the CU of the result. */
22273
22274 static struct die_info *
22275 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22276 struct dwarf2_cu **ref_cu)
22277 {
22278 sect_offset sect_off = attr->get_ref_die_offset ();
22279 struct dwarf2_cu *cu = *ref_cu;
22280 struct die_info *die;
22281
22282 die = follow_die_offset (sect_off,
22283 (attr->form == DW_FORM_GNU_ref_alt
22284 || cu->per_cu->is_dwz),
22285 ref_cu);
22286 if (!die)
22287 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22288 "at %s [in module %s]"),
22289 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22290 objfile_name (cu->per_objfile->objfile));
22291
22292 return die;
22293 }
22294
22295 /* See read.h. */
22296
22297 struct dwarf2_locexpr_baton
22298 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22299 dwarf2_per_cu_data *per_cu,
22300 dwarf2_per_objfile *per_objfile,
22301 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22302 bool resolve_abstract_p)
22303 {
22304 struct die_info *die;
22305 struct attribute *attr;
22306 struct dwarf2_locexpr_baton retval;
22307 struct objfile *objfile = per_objfile->objfile;
22308
22309 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22310 if (cu == nullptr)
22311 cu = load_cu (per_cu, per_objfile, false);
22312
22313 if (cu == nullptr)
22314 {
22315 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22316 Instead just throw an error, not much else we can do. */
22317 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22318 sect_offset_str (sect_off), objfile_name (objfile));
22319 }
22320
22321 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22322 if (!die)
22323 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22324 sect_offset_str (sect_off), objfile_name (objfile));
22325
22326 attr = dwarf2_attr (die, DW_AT_location, cu);
22327 if (!attr && resolve_abstract_p
22328 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22329 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22330 {
22331 CORE_ADDR pc = get_frame_pc ();
22332 CORE_ADDR baseaddr = objfile->text_section_offset ();
22333 struct gdbarch *gdbarch = objfile->arch ();
22334
22335 for (const auto &cand_off
22336 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22337 {
22338 struct dwarf2_cu *cand_cu = cu;
22339 struct die_info *cand
22340 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22341 if (!cand
22342 || !cand->parent
22343 || cand->parent->tag != DW_TAG_subprogram)
22344 continue;
22345
22346 CORE_ADDR pc_low, pc_high;
22347 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22348 if (pc_low == ((CORE_ADDR) -1))
22349 continue;
22350 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22351 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22352 if (!(pc_low <= pc && pc < pc_high))
22353 continue;
22354
22355 die = cand;
22356 attr = dwarf2_attr (die, DW_AT_location, cu);
22357 break;
22358 }
22359 }
22360
22361 if (!attr)
22362 {
22363 /* DWARF: "If there is no such attribute, then there is no effect.".
22364 DATA is ignored if SIZE is 0. */
22365
22366 retval.data = NULL;
22367 retval.size = 0;
22368 }
22369 else if (attr->form_is_section_offset ())
22370 {
22371 struct dwarf2_loclist_baton loclist_baton;
22372 CORE_ADDR pc = get_frame_pc ();
22373 size_t size;
22374
22375 fill_in_loclist_baton (cu, &loclist_baton, attr);
22376
22377 retval.data = dwarf2_find_location_expression (&loclist_baton,
22378 &size, pc);
22379 retval.size = size;
22380 }
22381 else
22382 {
22383 if (!attr->form_is_block ())
22384 error (_("Dwarf Error: DIE at %s referenced in module %s "
22385 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22386 sect_offset_str (sect_off), objfile_name (objfile));
22387
22388 struct dwarf_block *block = attr->as_block ();
22389 retval.data = block->data;
22390 retval.size = block->size;
22391 }
22392 retval.per_objfile = per_objfile;
22393 retval.per_cu = cu->per_cu;
22394
22395 per_objfile->age_comp_units ();
22396
22397 return retval;
22398 }
22399
22400 /* See read.h. */
22401
22402 struct dwarf2_locexpr_baton
22403 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22404 dwarf2_per_cu_data *per_cu,
22405 dwarf2_per_objfile *per_objfile,
22406 gdb::function_view<CORE_ADDR ()> get_frame_pc)
22407 {
22408 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22409
22410 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22411 get_frame_pc);
22412 }
22413
22414 /* Write a constant of a given type as target-ordered bytes into
22415 OBSTACK. */
22416
22417 static const gdb_byte *
22418 write_constant_as_bytes (struct obstack *obstack,
22419 enum bfd_endian byte_order,
22420 struct type *type,
22421 ULONGEST value,
22422 LONGEST *len)
22423 {
22424 gdb_byte *result;
22425
22426 *len = type->length ();
22427 result = (gdb_byte *) obstack_alloc (obstack, *len);
22428 store_unsigned_integer (result, *len, byte_order, value);
22429
22430 return result;
22431 }
22432
22433 /* See read.h. */
22434
22435 const gdb_byte *
22436 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22437 dwarf2_per_cu_data *per_cu,
22438 dwarf2_per_objfile *per_objfile,
22439 obstack *obstack,
22440 LONGEST *len)
22441 {
22442 struct die_info *die;
22443 struct attribute *attr;
22444 const gdb_byte *result = NULL;
22445 struct type *type;
22446 LONGEST value;
22447 enum bfd_endian byte_order;
22448 struct objfile *objfile = per_objfile->objfile;
22449
22450 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22451 if (cu == nullptr)
22452 cu = load_cu (per_cu, per_objfile, false);
22453
22454 if (cu == nullptr)
22455 {
22456 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22457 Instead just throw an error, not much else we can do. */
22458 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22459 sect_offset_str (sect_off), objfile_name (objfile));
22460 }
22461
22462 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22463 if (!die)
22464 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22465 sect_offset_str (sect_off), objfile_name (objfile));
22466
22467 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22468 if (attr == NULL)
22469 return NULL;
22470
22471 byte_order = (bfd_big_endian (objfile->obfd.get ())
22472 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22473
22474 switch (attr->form)
22475 {
22476 case DW_FORM_addr:
22477 case DW_FORM_addrx:
22478 case DW_FORM_GNU_addr_index:
22479 {
22480 gdb_byte *tem;
22481
22482 *len = cu->header.addr_size;
22483 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22484 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
22485 result = tem;
22486 }
22487 break;
22488 case DW_FORM_string:
22489 case DW_FORM_strp:
22490 case DW_FORM_strx:
22491 case DW_FORM_GNU_str_index:
22492 case DW_FORM_GNU_strp_alt:
22493 /* The string is already allocated on the objfile obstack, point
22494 directly to it. */
22495 {
22496 const char *attr_name = attr->as_string ();
22497 result = (const gdb_byte *) attr_name;
22498 *len = strlen (attr_name);
22499 }
22500 break;
22501 case DW_FORM_block1:
22502 case DW_FORM_block2:
22503 case DW_FORM_block4:
22504 case DW_FORM_block:
22505 case DW_FORM_exprloc:
22506 case DW_FORM_data16:
22507 {
22508 struct dwarf_block *block = attr->as_block ();
22509 result = block->data;
22510 *len = block->size;
22511 }
22512 break;
22513
22514 /* The DW_AT_const_value attributes are supposed to carry the
22515 symbol's value "represented as it would be on the target
22516 architecture." By the time we get here, it's already been
22517 converted to host endianness, so we just need to sign- or
22518 zero-extend it as appropriate. */
22519 case DW_FORM_data1:
22520 type = die_type (die, cu);
22521 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22522 if (result == NULL)
22523 result = write_constant_as_bytes (obstack, byte_order,
22524 type, value, len);
22525 break;
22526 case DW_FORM_data2:
22527 type = die_type (die, cu);
22528 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22529 if (result == NULL)
22530 result = write_constant_as_bytes (obstack, byte_order,
22531 type, value, len);
22532 break;
22533 case DW_FORM_data4:
22534 type = die_type (die, cu);
22535 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22536 if (result == NULL)
22537 result = write_constant_as_bytes (obstack, byte_order,
22538 type, value, len);
22539 break;
22540 case DW_FORM_data8:
22541 type = die_type (die, cu);
22542 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22543 if (result == NULL)
22544 result = write_constant_as_bytes (obstack, byte_order,
22545 type, value, len);
22546 break;
22547
22548 case DW_FORM_sdata:
22549 case DW_FORM_implicit_const:
22550 type = die_type (die, cu);
22551 result = write_constant_as_bytes (obstack, byte_order,
22552 type, attr->as_signed (), len);
22553 break;
22554
22555 case DW_FORM_udata:
22556 type = die_type (die, cu);
22557 result = write_constant_as_bytes (obstack, byte_order,
22558 type, attr->as_unsigned (), len);
22559 break;
22560
22561 default:
22562 complaint (_("unsupported const value attribute form: '%s'"),
22563 dwarf_form_name (attr->form));
22564 break;
22565 }
22566
22567 return result;
22568 }
22569
22570 /* See read.h. */
22571
22572 struct type *
22573 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22574 dwarf2_per_cu_data *per_cu,
22575 dwarf2_per_objfile *per_objfile,
22576 const char **var_name)
22577 {
22578 struct die_info *die;
22579
22580 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22581 if (cu == nullptr)
22582 cu = load_cu (per_cu, per_objfile, false);
22583
22584 if (cu == nullptr)
22585 return nullptr;
22586
22587 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22588 if (!die)
22589 return NULL;
22590
22591 if (var_name != nullptr)
22592 *var_name = var_decl_name (die, cu);
22593 return die_type (die, cu);
22594 }
22595
22596 /* See read.h. */
22597
22598 struct type *
22599 dwarf2_get_die_type (cu_offset die_offset,
22600 dwarf2_per_cu_data *per_cu,
22601 dwarf2_per_objfile *per_objfile)
22602 {
22603 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22604 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22605 }
22606
22607 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22608 On entry *REF_CU is the CU of SRC_DIE.
22609 On exit *REF_CU is the CU of the result.
22610 Returns NULL if the referenced DIE isn't found. */
22611
22612 static struct die_info *
22613 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22614 struct dwarf2_cu **ref_cu)
22615 {
22616 struct die_info temp_die;
22617 struct dwarf2_cu *sig_cu;
22618 struct die_info *die;
22619 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22620
22621
22622 /* While it might be nice to assert sig_type->type == NULL here,
22623 we can get here for DW_AT_imported_declaration where we need
22624 the DIE not the type. */
22625
22626 /* If necessary, add it to the queue and load its DIEs.
22627
22628 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22629 it doesn't mean they are currently loaded. Since we require them
22630 to be loaded, we must check for ourselves. */
22631 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
22632 language_minimal)
22633 || per_objfile->get_cu (sig_type) == nullptr)
22634 read_signatured_type (sig_type, per_objfile);
22635
22636 sig_cu = per_objfile->get_cu (sig_type);
22637 gdb_assert (sig_cu != NULL);
22638 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22639 temp_die.sect_off = sig_type->type_offset_in_section;
22640 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22641 to_underlying (temp_die.sect_off));
22642 if (die)
22643 {
22644 /* For .gdb_index version 7 keep track of included TUs.
22645 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22646 if (per_objfile->per_bfd->index_table != NULL
22647 && !per_objfile->per_bfd->index_table->version_check ())
22648 {
22649 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22650 }
22651
22652 *ref_cu = sig_cu;
22653 return die;
22654 }
22655
22656 return NULL;
22657 }
22658
22659 /* Follow signatured type referenced by ATTR in SRC_DIE.
22660 On entry *REF_CU is the CU of SRC_DIE.
22661 On exit *REF_CU is the CU of the result.
22662 The result is the DIE of the type.
22663 If the referenced type cannot be found an error is thrown. */
22664
22665 static struct die_info *
22666 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22667 struct dwarf2_cu **ref_cu)
22668 {
22669 ULONGEST signature = attr->as_signature ();
22670 struct signatured_type *sig_type;
22671 struct die_info *die;
22672
22673 gdb_assert (attr->form == DW_FORM_ref_sig8);
22674
22675 sig_type = lookup_signatured_type (*ref_cu, signature);
22676 /* sig_type will be NULL if the signatured type is missing from
22677 the debug info. */
22678 if (sig_type == NULL)
22679 {
22680 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22681 " from DIE at %s [in module %s]"),
22682 hex_string (signature), sect_offset_str (src_die->sect_off),
22683 objfile_name ((*ref_cu)->per_objfile->objfile));
22684 }
22685
22686 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22687 if (die == NULL)
22688 {
22689 src_die->error_dump ();
22690 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22691 " from DIE at %s [in module %s]"),
22692 hex_string (signature), sect_offset_str (src_die->sect_off),
22693 objfile_name ((*ref_cu)->per_objfile->objfile));
22694 }
22695
22696 return die;
22697 }
22698
22699 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22700 reading in and processing the type unit if necessary. */
22701
22702 static struct type *
22703 get_signatured_type (struct die_info *die, ULONGEST signature,
22704 struct dwarf2_cu *cu)
22705 {
22706 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22707 struct signatured_type *sig_type;
22708 struct dwarf2_cu *type_cu;
22709 struct die_info *type_die;
22710 struct type *type;
22711
22712 sig_type = lookup_signatured_type (cu, signature);
22713 /* sig_type will be NULL if the signatured type is missing from
22714 the debug info. */
22715 if (sig_type == NULL)
22716 {
22717 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22718 " from DIE at %s [in module %s]"),
22719 hex_string (signature), sect_offset_str (die->sect_off),
22720 objfile_name (per_objfile->objfile));
22721 return build_error_marker_type (cu, die);
22722 }
22723
22724 /* If we already know the type we're done. */
22725 type = per_objfile->get_type_for_signatured_type (sig_type);
22726 if (type != nullptr)
22727 return type;
22728
22729 type_cu = cu;
22730 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22731 if (type_die != NULL)
22732 {
22733 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22734 is created. This is important, for example, because for c++ classes
22735 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22736 type = read_type_die (type_die, type_cu);
22737 if (type == NULL)
22738 {
22739 complaint (_("Dwarf Error: Cannot build signatured type %s"
22740 " referenced from DIE at %s [in module %s]"),
22741 hex_string (signature), sect_offset_str (die->sect_off),
22742 objfile_name (per_objfile->objfile));
22743 type = build_error_marker_type (cu, die);
22744 }
22745 }
22746 else
22747 {
22748 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22749 " from DIE at %s [in module %s]"),
22750 hex_string (signature), sect_offset_str (die->sect_off),
22751 objfile_name (per_objfile->objfile));
22752 type = build_error_marker_type (cu, die);
22753 }
22754
22755 per_objfile->set_type_for_signatured_type (sig_type, type);
22756
22757 return type;
22758 }
22759
22760 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22761 reading in and processing the type unit if necessary. */
22762
22763 static struct type *
22764 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22765 struct dwarf2_cu *cu) /* ARI: editCase function */
22766 {
22767 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22768 if (attr->form_is_ref ())
22769 {
22770 struct dwarf2_cu *type_cu = cu;
22771 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22772
22773 return read_type_die (type_die, type_cu);
22774 }
22775 else if (attr->form == DW_FORM_ref_sig8)
22776 {
22777 return get_signatured_type (die, attr->as_signature (), cu);
22778 }
22779 else
22780 {
22781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22782
22783 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22784 " at %s [in module %s]"),
22785 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22786 objfile_name (per_objfile->objfile));
22787 return build_error_marker_type (cu, die);
22788 }
22789 }
22790
22791 /* Load the DIEs associated with type unit PER_CU into memory. */
22792
22793 static void
22794 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22795 dwarf2_per_objfile *per_objfile)
22796 {
22797 struct signatured_type *sig_type;
22798
22799 /* We have the per_cu, but we need the signatured_type.
22800 Fortunately this is an easy translation. */
22801 gdb_assert (per_cu->is_debug_types);
22802 sig_type = (struct signatured_type *) per_cu;
22803
22804 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22805
22806 read_signatured_type (sig_type, per_objfile);
22807
22808 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22809 }
22810
22811 /* Read in a signatured type and build its CU and DIEs.
22812 If the type is a stub for the real type in a DWO file,
22813 read in the real type from the DWO file as well. */
22814
22815 static void
22816 read_signatured_type (signatured_type *sig_type,
22817 dwarf2_per_objfile *per_objfile)
22818 {
22819 gdb_assert (sig_type->is_debug_types);
22820 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
22821
22822 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
22823
22824 if (!reader.dummy_p)
22825 {
22826 struct dwarf2_cu *cu = reader.cu;
22827 const gdb_byte *info_ptr = reader.info_ptr;
22828
22829 gdb_assert (cu->die_hash == NULL);
22830 cu->die_hash =
22831 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
22832 die_info::hash,
22833 die_info::eq,
22834 NULL,
22835 &cu->comp_unit_obstack,
22836 hashtab_obstack_allocate,
22837 dummy_obstack_deallocate);
22838
22839 if (reader.comp_unit_die->has_children)
22840 reader.comp_unit_die->child
22841 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22842 reader.comp_unit_die);
22843 cu->dies = reader.comp_unit_die;
22844 /* comp_unit_die is not stored in die_hash, no need. */
22845
22846 /* We try not to read any attributes in this function, because
22847 not all CUs needed for references have been loaded yet, and
22848 symbol table processing isn't initialized. But we have to
22849 set the CU language, or we won't be able to build types
22850 correctly. Similarly, if we do not read the producer, we can
22851 not apply producer-specific interpretation. */
22852 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22853
22854 reader.keep ();
22855 }
22856
22857 sig_type->tu_read = 1;
22858 }
22859
22860 /* Decode simple location descriptions.
22861 Given a pointer to a dwarf block that defines a location, compute
22862 the location and return the value. If COMPUTED is non-null, it is
22863 set to true to indicate that decoding was successful, and false
22864 otherwise. If COMPUTED is null, then this function may emit a
22865 complaint. */
22866
22867 static CORE_ADDR
22868 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22869 {
22870 struct objfile *objfile = cu->per_objfile->objfile;
22871 size_t i;
22872 size_t size = blk->size;
22873 const gdb_byte *data = blk->data;
22874 CORE_ADDR stack[64];
22875 int stacki;
22876 unsigned int bytes_read, unsnd;
22877 gdb_byte op;
22878
22879 if (computed != nullptr)
22880 *computed = false;
22881
22882 i = 0;
22883 stacki = 0;
22884 stack[stacki] = 0;
22885 stack[++stacki] = 0;
22886
22887 while (i < size)
22888 {
22889 op = data[i++];
22890 switch (op)
22891 {
22892 case DW_OP_lit0:
22893 case DW_OP_lit1:
22894 case DW_OP_lit2:
22895 case DW_OP_lit3:
22896 case DW_OP_lit4:
22897 case DW_OP_lit5:
22898 case DW_OP_lit6:
22899 case DW_OP_lit7:
22900 case DW_OP_lit8:
22901 case DW_OP_lit9:
22902 case DW_OP_lit10:
22903 case DW_OP_lit11:
22904 case DW_OP_lit12:
22905 case DW_OP_lit13:
22906 case DW_OP_lit14:
22907 case DW_OP_lit15:
22908 case DW_OP_lit16:
22909 case DW_OP_lit17:
22910 case DW_OP_lit18:
22911 case DW_OP_lit19:
22912 case DW_OP_lit20:
22913 case DW_OP_lit21:
22914 case DW_OP_lit22:
22915 case DW_OP_lit23:
22916 case DW_OP_lit24:
22917 case DW_OP_lit25:
22918 case DW_OP_lit26:
22919 case DW_OP_lit27:
22920 case DW_OP_lit28:
22921 case DW_OP_lit29:
22922 case DW_OP_lit30:
22923 case DW_OP_lit31:
22924 stack[++stacki] = op - DW_OP_lit0;
22925 break;
22926
22927 case DW_OP_reg0:
22928 case DW_OP_reg1:
22929 case DW_OP_reg2:
22930 case DW_OP_reg3:
22931 case DW_OP_reg4:
22932 case DW_OP_reg5:
22933 case DW_OP_reg6:
22934 case DW_OP_reg7:
22935 case DW_OP_reg8:
22936 case DW_OP_reg9:
22937 case DW_OP_reg10:
22938 case DW_OP_reg11:
22939 case DW_OP_reg12:
22940 case DW_OP_reg13:
22941 case DW_OP_reg14:
22942 case DW_OP_reg15:
22943 case DW_OP_reg16:
22944 case DW_OP_reg17:
22945 case DW_OP_reg18:
22946 case DW_OP_reg19:
22947 case DW_OP_reg20:
22948 case DW_OP_reg21:
22949 case DW_OP_reg22:
22950 case DW_OP_reg23:
22951 case DW_OP_reg24:
22952 case DW_OP_reg25:
22953 case DW_OP_reg26:
22954 case DW_OP_reg27:
22955 case DW_OP_reg28:
22956 case DW_OP_reg29:
22957 case DW_OP_reg30:
22958 case DW_OP_reg31:
22959 stack[++stacki] = op - DW_OP_reg0;
22960 if (i < size)
22961 {
22962 if (computed == nullptr)
22963 dwarf2_complex_location_expr_complaint ();
22964 else
22965 return 0;
22966 }
22967 break;
22968
22969 case DW_OP_regx:
22970 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22971 i += bytes_read;
22972 stack[++stacki] = unsnd;
22973 if (i < size)
22974 {
22975 if (computed == nullptr)
22976 dwarf2_complex_location_expr_complaint ();
22977 else
22978 return 0;
22979 }
22980 break;
22981
22982 case DW_OP_addr:
22983 stack[++stacki] = cu->header.read_address (objfile->obfd.get (),
22984 &data[i],
22985 &bytes_read);
22986 i += bytes_read;
22987 break;
22988
22989 case DW_OP_const1u:
22990 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
22991 i += 1;
22992 break;
22993
22994 case DW_OP_const1s:
22995 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
22996 i += 1;
22997 break;
22998
22999 case DW_OP_const2u:
23000 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
23001 i += 2;
23002 break;
23003
23004 case DW_OP_const2s:
23005 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
23006 i += 2;
23007 break;
23008
23009 case DW_OP_const4u:
23010 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
23011 i += 4;
23012 break;
23013
23014 case DW_OP_const4s:
23015 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
23016 i += 4;
23017 break;
23018
23019 case DW_OP_const8u:
23020 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
23021 i += 8;
23022 break;
23023
23024 case DW_OP_constu:
23025 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23026 &bytes_read);
23027 i += bytes_read;
23028 break;
23029
23030 case DW_OP_consts:
23031 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23032 i += bytes_read;
23033 break;
23034
23035 case DW_OP_dup:
23036 stack[stacki + 1] = stack[stacki];
23037 stacki++;
23038 break;
23039
23040 case DW_OP_plus:
23041 stack[stacki - 1] += stack[stacki];
23042 stacki--;
23043 break;
23044
23045 case DW_OP_plus_uconst:
23046 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23047 &bytes_read);
23048 i += bytes_read;
23049 break;
23050
23051 case DW_OP_minus:
23052 stack[stacki - 1] -= stack[stacki];
23053 stacki--;
23054 break;
23055
23056 case DW_OP_deref:
23057 /* If we're not the last op, then we definitely can't encode
23058 this using GDB's address_class enum. This is valid for partial
23059 global symbols, although the variable's address will be bogus
23060 in the psymtab. */
23061 if (i < size)
23062 {
23063 if (computed == nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23065 else
23066 return 0;
23067 }
23068 break;
23069
23070 case DW_OP_GNU_push_tls_address:
23071 case DW_OP_form_tls_address:
23072 /* The top of the stack has the offset from the beginning
23073 of the thread control block at which the variable is located. */
23074 /* Nothing should follow this operator, so the top of stack would
23075 be returned. */
23076 /* This is valid for partial global symbols, but the variable's
23077 address will be bogus in the psymtab. Make it always at least
23078 non-zero to not look as a variable garbage collected by linker
23079 which have DW_OP_addr 0. */
23080 if (i < size)
23081 {
23082 if (computed == nullptr)
23083 dwarf2_complex_location_expr_complaint ();
23084 else
23085 return 0;
23086 }
23087 stack[stacki]++;
23088 break;
23089
23090 case DW_OP_GNU_uninit:
23091 if (computed != nullptr)
23092 return 0;
23093 break;
23094
23095 case DW_OP_addrx:
23096 case DW_OP_GNU_addr_index:
23097 case DW_OP_GNU_const_index:
23098 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23099 &bytes_read);
23100 i += bytes_read;
23101 break;
23102
23103 default:
23104 if (computed == nullptr)
23105 {
23106 const char *name = get_DW_OP_name (op);
23107
23108 if (name)
23109 complaint (_("unsupported stack op: '%s'"),
23110 name);
23111 else
23112 complaint (_("unsupported stack op: '%02x'"),
23113 op);
23114 }
23115
23116 return (stack[stacki]);
23117 }
23118
23119 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23120 outside of the allocated space. Also enforce minimum>0. */
23121 if (stacki >= ARRAY_SIZE (stack) - 1)
23122 {
23123 if (computed == nullptr)
23124 complaint (_("location description stack overflow"));
23125 return 0;
23126 }
23127
23128 if (stacki <= 0)
23129 {
23130 if (computed == nullptr)
23131 complaint (_("location description stack underflow"));
23132 return 0;
23133 }
23134 }
23135
23136 if (computed != nullptr)
23137 *computed = true;
23138 return (stack[stacki]);
23139 }
23140
23141 /* memory allocation interface */
23142
23143 static struct dwarf_block *
23144 dwarf_alloc_block (struct dwarf2_cu *cu)
23145 {
23146 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23147 }
23148
23149 \f
23150
23151 /* Macro support. */
23152
23153 /* An overload of dwarf_decode_macros that finds the correct section
23154 and ensures it is read in before calling the other overload. */
23155
23156 static void
23157 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23158 int section_is_gnu)
23159 {
23160 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23161 struct objfile *objfile = per_objfile->objfile;
23162 const struct line_header *lh = cu->line_header;
23163 unsigned int offset_size = cu->header.offset_size;
23164 struct dwarf2_section_info *section;
23165 const char *section_name;
23166
23167 if (cu->dwo_unit != nullptr)
23168 {
23169 if (section_is_gnu)
23170 {
23171 section = &cu->dwo_unit->dwo_file->sections.macro;
23172 section_name = ".debug_macro.dwo";
23173 }
23174 else
23175 {
23176 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23177 section_name = ".debug_macinfo.dwo";
23178 }
23179 }
23180 else
23181 {
23182 if (section_is_gnu)
23183 {
23184 section = &per_objfile->per_bfd->macro;
23185 section_name = ".debug_macro";
23186 }
23187 else
23188 {
23189 section = &per_objfile->per_bfd->macinfo;
23190 section_name = ".debug_macinfo";
23191 }
23192 }
23193
23194 section->read (objfile);
23195 if (section->buffer == nullptr)
23196 {
23197 complaint (_("missing %s section"), section_name);
23198 return;
23199 }
23200
23201 buildsym_compunit *builder = cu->get_builder ();
23202
23203 struct dwarf2_section_info *str_offsets_section;
23204 struct dwarf2_section_info *str_section;
23205 gdb::optional<ULONGEST> str_offsets_base;
23206
23207 if (cu->dwo_unit != nullptr)
23208 {
23209 str_offsets_section = &cu->dwo_unit->dwo_file
23210 ->sections.str_offsets;
23211 str_section = &cu->dwo_unit->dwo_file->sections.str;
23212 str_offsets_base = cu->header.addr_size;
23213 }
23214 else
23215 {
23216 str_offsets_section = &per_objfile->per_bfd->str_offsets;
23217 str_section = &per_objfile->per_bfd->str;
23218 str_offsets_base = cu->str_offsets_base;
23219 }
23220
23221 dwarf_decode_macros (per_objfile, builder, section, lh,
23222 offset_size, offset, str_section, str_offsets_section,
23223 str_offsets_base, section_is_gnu, cu);
23224 }
23225
23226 /* Return the .debug_loc section to use for CU.
23227 For DWO files use .debug_loc.dwo. */
23228
23229 static struct dwarf2_section_info *
23230 cu_debug_loc_section (struct dwarf2_cu *cu)
23231 {
23232 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23233
23234 if (cu->dwo_unit)
23235 {
23236 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23237
23238 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23239 }
23240 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23241 : &per_objfile->per_bfd->loc);
23242 }
23243
23244 /* Return the .debug_rnglists section to use for CU. */
23245 static struct dwarf2_section_info *
23246 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23247 {
23248 if (cu->header.version < 5)
23249 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23250 cu->header.version);
23251 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23252
23253 /* Make sure we read the .debug_rnglists section from the file that
23254 contains the DW_AT_ranges attribute we are reading. Normally that
23255 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23256 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23257 program. */
23258 if (cu->dwo_unit != nullptr
23259 && tag != DW_TAG_compile_unit
23260 && tag != DW_TAG_skeleton_unit)
23261 {
23262 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23263
23264 if (sections->rnglists.size > 0)
23265 return &sections->rnglists;
23266 else
23267 error (_(".debug_rnglists section is missing from .dwo file."));
23268 }
23269 return &dwarf2_per_objfile->per_bfd->rnglists;
23270 }
23271
23272 /* A helper function that fills in a dwarf2_loclist_baton. */
23273
23274 static void
23275 fill_in_loclist_baton (struct dwarf2_cu *cu,
23276 struct dwarf2_loclist_baton *baton,
23277 const struct attribute *attr)
23278 {
23279 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23280 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23281
23282 section->read (per_objfile->objfile);
23283
23284 baton->per_objfile = per_objfile;
23285 baton->per_cu = cu->per_cu;
23286 gdb_assert (baton->per_cu);
23287 /* We don't know how long the location list is, but make sure we
23288 don't run off the edge of the section. */
23289 baton->size = section->size - attr->as_unsigned ();
23290 baton->data = section->buffer + attr->as_unsigned ();
23291 if (cu->base_address.has_value ())
23292 baton->base_address = *cu->base_address;
23293 else
23294 baton->base_address = 0;
23295 baton->from_dwo = cu->dwo_unit != NULL;
23296 }
23297
23298 static void
23299 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23300 struct dwarf2_cu *cu, int is_block)
23301 {
23302 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23303 struct objfile *objfile = per_objfile->objfile;
23304 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23305
23306 if (attr->form_is_section_offset ()
23307 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23308 the section. If so, fall through to the complaint in the
23309 other branch. */
23310 && attr->as_unsigned () < section->get_size (objfile))
23311 {
23312 struct dwarf2_loclist_baton *baton;
23313
23314 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23315
23316 fill_in_loclist_baton (cu, baton, attr);
23317
23318 if (!cu->base_address.has_value ())
23319 complaint (_("Location list used without "
23320 "specifying the CU base address."));
23321
23322 sym->set_aclass_index ((is_block
23323 ? dwarf2_loclist_block_index
23324 : dwarf2_loclist_index));
23325 SYMBOL_LOCATION_BATON (sym) = baton;
23326 }
23327 else
23328 {
23329 struct dwarf2_locexpr_baton *baton;
23330
23331 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23332 baton->per_objfile = per_objfile;
23333 baton->per_cu = cu->per_cu;
23334 gdb_assert (baton->per_cu);
23335
23336 if (attr->form_is_block ())
23337 {
23338 /* Note that we're just copying the block's data pointer
23339 here, not the actual data. We're still pointing into the
23340 info_buffer for SYM's objfile; right now we never release
23341 that buffer, but when we do clean up properly this may
23342 need to change. */
23343 struct dwarf_block *block = attr->as_block ();
23344 baton->size = block->size;
23345 baton->data = block->data;
23346 }
23347 else
23348 {
23349 dwarf2_invalid_attrib_class_complaint ("location description",
23350 sym->natural_name ());
23351 baton->size = 0;
23352 }
23353
23354 sym->set_aclass_index ((is_block
23355 ? dwarf2_locexpr_block_index
23356 : dwarf2_locexpr_index));
23357 SYMBOL_LOCATION_BATON (sym) = baton;
23358 }
23359 }
23360
23361 /* See read.h. */
23362
23363 const comp_unit_head *
23364 dwarf2_per_cu_data::get_header () const
23365 {
23366 if (!m_header_read_in)
23367 {
23368 const gdb_byte *info_ptr
23369 = this->section->buffer + to_underlying (this->sect_off);
23370
23371 read_comp_unit_head (&m_header, info_ptr, this->section,
23372 rcuh_kind::COMPILE);
23373
23374 m_header_read_in = true;
23375 }
23376
23377 return &m_header;
23378 }
23379
23380 /* See read.h. */
23381
23382 int
23383 dwarf2_per_cu_data::addr_size () const
23384 {
23385 return this->get_header ()->addr_size;
23386 }
23387
23388 /* See read.h. */
23389
23390 int
23391 dwarf2_per_cu_data::offset_size () const
23392 {
23393 return this->get_header ()->offset_size;
23394 }
23395
23396 /* See read.h. */
23397
23398 int
23399 dwarf2_per_cu_data::ref_addr_size () const
23400 {
23401 const comp_unit_head *header = this->get_header ();
23402
23403 if (header->version == 2)
23404 return header->addr_size;
23405 else
23406 return header->offset_size;
23407 }
23408
23409 /* A helper function for dwarf2_find_containing_comp_unit that returns
23410 the index of the result, and that searches a vector. It will
23411 return a result even if the offset in question does not actually
23412 occur in any CU. This is separate so that it can be unit
23413 tested. */
23414
23415 static int
23416 dwarf2_find_containing_comp_unit
23417 (sect_offset sect_off,
23418 unsigned int offset_in_dwz,
23419 const std::vector<dwarf2_per_cu_data_up> &all_units)
23420 {
23421 int low, high;
23422
23423 low = 0;
23424 high = all_units.size () - 1;
23425 while (high > low)
23426 {
23427 struct dwarf2_per_cu_data *mid_cu;
23428 int mid = low + (high - low) / 2;
23429
23430 mid_cu = all_units[mid].get ();
23431 if (mid_cu->is_dwz > offset_in_dwz
23432 || (mid_cu->is_dwz == offset_in_dwz
23433 && mid_cu->sect_off + mid_cu->length () > sect_off))
23434 high = mid;
23435 else
23436 low = mid + 1;
23437 }
23438 gdb_assert (low == high);
23439 return low;
23440 }
23441
23442 /* Locate the .debug_info compilation unit from CU's objfile which contains
23443 the DIE at OFFSET. Raises an error on failure. */
23444
23445 static struct dwarf2_per_cu_data *
23446 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23447 unsigned int offset_in_dwz,
23448 dwarf2_per_bfd *per_bfd)
23449 {
23450 int low = dwarf2_find_containing_comp_unit
23451 (sect_off, offset_in_dwz, per_bfd->all_units);
23452 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
23453
23454 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23455 {
23456 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23457 error (_("Dwarf Error: could not find partial DIE containing "
23458 "offset %s [in module %s]"),
23459 sect_offset_str (sect_off),
23460 bfd_get_filename (per_bfd->obfd));
23461
23462 gdb_assert (per_bfd->all_units[low-1]->sect_off
23463 <= sect_off);
23464 return per_bfd->all_units[low - 1].get ();
23465 }
23466 else
23467 {
23468 if (low == per_bfd->all_units.size () - 1
23469 && sect_off >= this_cu->sect_off + this_cu->length ())
23470 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23471 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
23472 return this_cu;
23473 }
23474 }
23475
23476 #if GDB_SELF_TEST
23477
23478 namespace selftests {
23479 namespace find_containing_comp_unit {
23480
23481 static void
23482 run_test ()
23483 {
23484 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
23485 dwarf2_per_cu_data *one_ptr = one.get ();
23486 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
23487 dwarf2_per_cu_data *two_ptr = two.get ();
23488 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
23489 dwarf2_per_cu_data *three_ptr = three.get ();
23490 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
23491 dwarf2_per_cu_data *four_ptr = four.get ();
23492
23493 one->set_length (5);
23494 two->sect_off = sect_offset (one->length ());
23495 two->set_length (7);
23496
23497 three->set_length (5);
23498 three->is_dwz = 1;
23499 four->sect_off = sect_offset (three->length ());
23500 four->set_length (7);
23501 four->is_dwz = 1;
23502
23503 std::vector<dwarf2_per_cu_data_up> units;
23504 units.push_back (std::move (one));
23505 units.push_back (std::move (two));
23506 units.push_back (std::move (three));
23507 units.push_back (std::move (four));
23508
23509 int result;
23510
23511 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23512 SELF_CHECK (units[result].get () == one_ptr);
23513 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23514 SELF_CHECK (units[result].get () == one_ptr);
23515 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23516 SELF_CHECK (units[result].get () == two_ptr);
23517
23518 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23519 SELF_CHECK (units[result].get () == three_ptr);
23520 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23521 SELF_CHECK (units[result].get () == three_ptr);
23522 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23523 SELF_CHECK (units[result].get () == four_ptr);
23524 }
23525
23526 }
23527 }
23528
23529 #endif /* GDB_SELF_TEST */
23530
23531 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23532
23533 static void
23534 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23535 enum language pretend_language)
23536 {
23537 struct attribute *attr;
23538
23539 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23540
23541 /* Set the language we're debugging. */
23542 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23543 enum language lang;
23544 if (cu->producer != nullptr
23545 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
23546 {
23547 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23548 attribute is not standardised yet. As a workaround for the
23549 language detection we fall back to the DW_AT_producer
23550 string. */
23551 lang = language_opencl;
23552 }
23553 else if (cu->producer != nullptr
23554 && strstr (cu->producer, "GNU Go ") != NULL)
23555 {
23556 /* Similar hack for Go. */
23557 lang = language_go;
23558 }
23559 else if (attr != nullptr)
23560 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
23561 else
23562 lang = pretend_language;
23563
23564 cu->language_defn = language_def (lang);
23565
23566 switch (comp_unit_die->tag)
23567 {
23568 case DW_TAG_compile_unit:
23569 cu->per_cu->set_unit_type (DW_UT_compile);
23570 break;
23571 case DW_TAG_partial_unit:
23572 cu->per_cu->set_unit_type (DW_UT_partial);
23573 break;
23574 case DW_TAG_type_unit:
23575 cu->per_cu->set_unit_type (DW_UT_type);
23576 break;
23577 default:
23578 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
23579 dwarf_tag_name (comp_unit_die->tag),
23580 sect_offset_str (cu->per_cu->sect_off));
23581 }
23582
23583 cu->per_cu->set_lang (lang);
23584 }
23585
23586 /* See read.h. */
23587
23588 dwarf2_cu *
23589 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23590 {
23591 auto it = m_dwarf2_cus.find (per_cu);
23592 if (it == m_dwarf2_cus.end ())
23593 return nullptr;
23594
23595 return it->second.get ();
23596 }
23597
23598 /* See read.h. */
23599
23600 void
23601 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
23602 std::unique_ptr<dwarf2_cu> cu)
23603 {
23604 gdb_assert (this->get_cu (per_cu) == nullptr);
23605
23606 m_dwarf2_cus[per_cu] = std::move (cu);
23607 }
23608
23609 /* See read.h. */
23610
23611 void
23612 dwarf2_per_objfile::age_comp_units ()
23613 {
23614 dwarf_read_debug_printf_v ("running");
23615
23616 /* This is not expected to be called in the middle of CU expansion. There is
23617 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23618 loaded in memory. Calling age_comp_units while the queue is in use could
23619 make us free the DIEs for a CU that is in the queue and therefore break
23620 that invariant. */
23621 gdb_assert (!queue.has_value ());
23622
23623 /* Start by clearing all marks. */
23624 for (const auto &pair : m_dwarf2_cus)
23625 pair.second->clear_mark ();
23626
23627 /* Traverse all CUs, mark them and their dependencies if used recently
23628 enough. */
23629 for (const auto &pair : m_dwarf2_cus)
23630 {
23631 dwarf2_cu *cu = pair.second.get ();
23632
23633 cu->last_used++;
23634 if (cu->last_used <= dwarf_max_cache_age)
23635 cu->mark ();
23636 }
23637
23638 /* Delete all CUs still not marked. */
23639 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23640 {
23641 dwarf2_cu *cu = it->second.get ();
23642
23643 if (!cu->is_marked ())
23644 {
23645 dwarf_read_debug_printf_v ("deleting old CU %s",
23646 sect_offset_str (cu->per_cu->sect_off));
23647 it = m_dwarf2_cus.erase (it);
23648 }
23649 else
23650 it++;
23651 }
23652 }
23653
23654 /* See read.h. */
23655
23656 void
23657 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23658 {
23659 auto it = m_dwarf2_cus.find (per_cu);
23660 if (it == m_dwarf2_cus.end ())
23661 return;
23662
23663 m_dwarf2_cus.erase (it);
23664 }
23665
23666 dwarf2_per_objfile::~dwarf2_per_objfile ()
23667 {
23668 remove_all_cus ();
23669 }
23670
23671 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23672 We store these in a hash table separate from the DIEs, and preserve them
23673 when the DIEs are flushed out of cache.
23674
23675 The CU "per_cu" pointer is needed because offset alone is not enough to
23676 uniquely identify the type. A file may have multiple .debug_types sections,
23677 or the type may come from a DWO file. Furthermore, while it's more logical
23678 to use per_cu->section+offset, with Fission the section with the data is in
23679 the DWO file but we don't know that section at the point we need it.
23680 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23681 because we can enter the lookup routine, get_die_type_at_offset, from
23682 outside this file, and thus won't necessarily have PER_CU->cu.
23683 Fortunately, PER_CU is stable for the life of the objfile. */
23684
23685 struct dwarf2_per_cu_offset_and_type
23686 {
23687 const struct dwarf2_per_cu_data *per_cu;
23688 sect_offset sect_off;
23689 struct type *type;
23690 };
23691
23692 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23693
23694 static hashval_t
23695 per_cu_offset_and_type_hash (const void *item)
23696 {
23697 const struct dwarf2_per_cu_offset_and_type *ofs
23698 = (const struct dwarf2_per_cu_offset_and_type *) item;
23699
23700 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23701 }
23702
23703 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23704
23705 static int
23706 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23707 {
23708 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23709 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23710 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23711 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23712
23713 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23714 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23715 }
23716
23717 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23718 table if necessary. For convenience, return TYPE.
23719
23720 The DIEs reading must have careful ordering to:
23721 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23722 reading current DIE.
23723 * Not trying to dereference contents of still incompletely read in types
23724 while reading in other DIEs.
23725 * Enable referencing still incompletely read in types just by a pointer to
23726 the type without accessing its fields.
23727
23728 Therefore caller should follow these rules:
23729 * Try to fetch any prerequisite types we may need to build this DIE type
23730 before building the type and calling set_die_type.
23731 * After building type call set_die_type for current DIE as soon as
23732 possible before fetching more types to complete the current type.
23733 * Make the type as complete as possible before fetching more types. */
23734
23735 static struct type *
23736 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
23737 bool skip_data_location)
23738 {
23739 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23740 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23741 struct objfile *objfile = per_objfile->objfile;
23742 struct attribute *attr;
23743 struct dynamic_prop prop;
23744
23745 /* For Ada types, make sure that the gnat-specific data is always
23746 initialized (if not already set). There are a few types where
23747 we should not be doing so, because the type-specific area is
23748 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23749 where the type-specific area is used to store the floatformat).
23750 But this is not a problem, because the gnat-specific information
23751 is actually not needed for these types. */
23752 if (need_gnat_info (cu)
23753 && type->code () != TYPE_CODE_FUNC
23754 && type->code () != TYPE_CODE_FLT
23755 && type->code () != TYPE_CODE_METHODPTR
23756 && type->code () != TYPE_CODE_MEMBERPTR
23757 && type->code () != TYPE_CODE_METHOD
23758 && type->code () != TYPE_CODE_FIXED_POINT
23759 && !HAVE_GNAT_AUX_INFO (type))
23760 INIT_GNAT_SPECIFIC (type);
23761
23762 /* Read DW_AT_allocated and set in type. */
23763 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23764 if (attr != NULL)
23765 {
23766 struct type *prop_type = cu->addr_sized_int_type (false);
23767 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23768 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23769 }
23770
23771 /* Read DW_AT_associated and set in type. */
23772 attr = dwarf2_attr (die, DW_AT_associated, cu);
23773 if (attr != NULL)
23774 {
23775 struct type *prop_type = cu->addr_sized_int_type (false);
23776 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23777 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23778 }
23779
23780 /* Read DW_AT_rank and set in type. */
23781 attr = dwarf2_attr (die, DW_AT_rank, cu);
23782 if (attr != NULL)
23783 {
23784 struct type *prop_type = cu->addr_sized_int_type (false);
23785 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23786 type->add_dyn_prop (DYN_PROP_RANK, prop);
23787 }
23788
23789 /* Read DW_AT_data_location and set in type. */
23790 if (!skip_data_location)
23791 {
23792 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23793 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23794 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23795 }
23796
23797 if (per_objfile->die_type_hash == NULL)
23798 per_objfile->die_type_hash
23799 = htab_up (htab_create_alloc (127,
23800 per_cu_offset_and_type_hash,
23801 per_cu_offset_and_type_eq,
23802 NULL, xcalloc, xfree));
23803
23804 ofs.per_cu = cu->per_cu;
23805 ofs.sect_off = die->sect_off;
23806 ofs.type = type;
23807 slot = (struct dwarf2_per_cu_offset_and_type **)
23808 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23809 if (*slot)
23810 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23811 sect_offset_str (die->sect_off));
23812 *slot = XOBNEW (&objfile->objfile_obstack,
23813 struct dwarf2_per_cu_offset_and_type);
23814 **slot = ofs;
23815 return type;
23816 }
23817
23818 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23819 or return NULL if the die does not have a saved type. */
23820
23821 static struct type *
23822 get_die_type_at_offset (sect_offset sect_off,
23823 dwarf2_per_cu_data *per_cu,
23824 dwarf2_per_objfile *per_objfile)
23825 {
23826 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23827
23828 if (per_objfile->die_type_hash == NULL)
23829 return NULL;
23830
23831 ofs.per_cu = per_cu;
23832 ofs.sect_off = sect_off;
23833 slot = ((struct dwarf2_per_cu_offset_and_type *)
23834 htab_find (per_objfile->die_type_hash.get (), &ofs));
23835 if (slot)
23836 return slot->type;
23837 else
23838 return NULL;
23839 }
23840
23841 /* Look up the type for DIE in CU in die_type_hash,
23842 or return NULL if DIE does not have a saved type. */
23843
23844 static struct type *
23845 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23846 {
23847 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23848 }
23849
23850 struct cmd_list_element *set_dwarf_cmdlist;
23851 struct cmd_list_element *show_dwarf_cmdlist;
23852
23853 static void
23854 show_check_physname (struct ui_file *file, int from_tty,
23855 struct cmd_list_element *c, const char *value)
23856 {
23857 gdb_printf (file,
23858 _("Whether to check \"physname\" is %s.\n"),
23859 value);
23860 }
23861
23862 void _initialize_dwarf2_read ();
23863 void
23864 _initialize_dwarf2_read ()
23865 {
23866 add_setshow_prefix_cmd ("dwarf", class_maintenance,
23867 _("\
23868 Set DWARF specific variables.\n\
23869 Configure DWARF variables such as the cache size."),
23870 _("\
23871 Show DWARF specific variables.\n\
23872 Show DWARF variables such as the cache size."),
23873 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
23874 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
23875
23876 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23877 &dwarf_max_cache_age, _("\
23878 Set the upper bound on the age of cached DWARF compilation units."), _("\
23879 Show the upper bound on the age of cached DWARF compilation units."), _("\
23880 A higher limit means that cached compilation units will be stored\n\
23881 in memory longer, and more total memory will be used. Zero disables\n\
23882 caching, which can slow down startup."),
23883 NULL,
23884 show_dwarf_max_cache_age,
23885 &set_dwarf_cmdlist,
23886 &show_dwarf_cmdlist);
23887
23888 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23889 Set debugging of the DWARF reader."), _("\
23890 Show debugging of the DWARF reader."), _("\
23891 When enabled (non-zero), debugging messages are printed during DWARF\n\
23892 reading and symtab expansion. A value of 1 (one) provides basic\n\
23893 information. A value greater than 1 provides more verbose information."),
23894 NULL,
23895 NULL,
23896 &setdebuglist, &showdebuglist);
23897
23898 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23899 Set debugging of the DWARF DIE reader."), _("\
23900 Show debugging of the DWARF DIE reader."), _("\
23901 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23902 The value is the maximum depth to print."),
23903 NULL,
23904 NULL,
23905 &setdebuglist, &showdebuglist);
23906
23907 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23908 Set debugging of the dwarf line reader."), _("\
23909 Show debugging of the dwarf line reader."), _("\
23910 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23911 A value of 1 (one) provides basic information.\n\
23912 A value greater than 1 provides more verbose information."),
23913 NULL,
23914 NULL,
23915 &setdebuglist, &showdebuglist);
23916
23917 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23918 Set cross-checking of \"physname\" code against demangler."), _("\
23919 Show cross-checking of \"physname\" code against demangler."), _("\
23920 When enabled, GDB's internal \"physname\" code is checked against\n\
23921 the demangler."),
23922 NULL, show_check_physname,
23923 &setdebuglist, &showdebuglist);
23924
23925 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23926 no_class, &use_deprecated_index_sections, _("\
23927 Set whether to use deprecated gdb_index sections."), _("\
23928 Show whether to use deprecated gdb_index sections."), _("\
23929 When enabled, deprecated .gdb_index sections are used anyway.\n\
23930 Normally they are ignored either because of a missing feature or\n\
23931 performance issue.\n\
23932 Warning: This option must be enabled before gdb reads the file."),
23933 NULL,
23934 NULL,
23935 &setlist, &showlist);
23936
23937 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23938 &dwarf2_locexpr_funcs);
23939 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23940 &dwarf2_loclist_funcs);
23941
23942 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23943 &dwarf2_block_frame_base_locexpr_funcs);
23944 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23945 &dwarf2_block_frame_base_loclist_funcs);
23946
23947 #if GDB_SELF_TEST
23948 selftests::register_test ("dw2_expand_symtabs_matching",
23949 selftests::dw2_expand_symtabs_matching::run_test);
23950 selftests::register_test ("dwarf2_find_containing_comp_unit",
23951 selftests::find_containing_comp_unit::run_test);
23952 #endif
23953 }