[gdb/symtab] Fix duplicate CUs in all_comp_units
[binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "c-lang.h"
68 #include "go-lang.h"
69 #include "valprint.h"
70 #include "gdbcore.h" /* for gnutarget */
71 #include "gdb/gdb-index.h"
72 #include "gdb_bfd.h"
73 #include "f-lang.h"
74 #include "source.h"
75 #include "build-id.h"
76 #include "namespace.h"
77 #include "gdbsupport/function-view.h"
78 #include "gdbsupport/gdb_optional.h"
79 #include "gdbsupport/underlying.h"
80 #include "gdbsupport/hash_enum.h"
81 #include "filename-seen-cache.h"
82 #include "producer.h"
83 #include <fcntl.h>
84 #include <algorithm>
85 #include <unordered_map>
86 #include "gdbsupport/selftest.h"
87 #include "rust-lang.h"
88 #include "gdbsupport/pathstuff.h"
89 #include "count-one-bits.h"
90 #include <unordered_set>
91 #include "dwarf2/abbrev-cache.h"
92 #include "cooked-index.h"
93 #include "split-name.h"
94 #include "gdbsupport/parallel-for.h"
95 #include "gdbsupport/thread-pool.h"
96
97 /* When == 1, print basic high level tracing messages.
98 When > 1, be more verbose.
99 This is in contrast to the low level DIE reading of dwarf_die_debug. */
100 static unsigned int dwarf_read_debug = 0;
101
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
103
104 #define dwarf_read_debug_printf(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
107
108 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
109
110 #define dwarf_read_debug_printf_v(fmt, ...) \
111 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
112 ##__VA_ARGS__)
113
114 /* When non-zero, dump DIEs after they are read in. */
115 static unsigned int dwarf_die_debug = 0;
116
117 /* When non-zero, dump line number entries as they are read in. */
118 unsigned int dwarf_line_debug = 0;
119
120 /* When true, cross-check physname against demangler. */
121 static bool check_physname = false;
122
123 /* When true, do not reject deprecated .gdb_index sections. */
124 static bool use_deprecated_index_sections = false;
125
126 /* This is used to store the data that is always per objfile. */
127 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
128
129 /* These are used to store the dwarf2_per_bfd objects.
130
131 objfiles having the same BFD, which doesn't require relocations, are going to
132 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
133
134 Other objfiles are not going to share a dwarf2_per_bfd with any other
135 objfiles, so they'll have their own version kept in the _objfile_data_key
136 version. */
137 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
138 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
139
140 /* The "aclass" indices for various kinds of computed DWARF symbols. */
141
142 static int dwarf2_locexpr_index;
143 static int dwarf2_loclist_index;
144 static int dwarf2_locexpr_block_index;
145 static int dwarf2_loclist_block_index;
146
147 /* Size of .debug_loclists section header for 32-bit DWARF format. */
148 #define LOCLIST_HEADER_SIZE32 12
149
150 /* Size of .debug_loclists section header for 64-bit DWARF format. */
151 #define LOCLIST_HEADER_SIZE64 20
152
153 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
154 #define RNGLIST_HEADER_SIZE32 12
155
156 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
157 #define RNGLIST_HEADER_SIZE64 20
158
159 /* This is a view into the index that converts from bytes to an
160 offset_type, and allows indexing. Unaligned bytes are specifically
161 allowed here, and handled via unpacking. */
162
163 class offset_view
164 {
165 public:
166 offset_view () = default;
167
168 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
169 : m_bytes (bytes)
170 {
171 }
172
173 /* Extract the INDEXth offset_type from the array. */
174 offset_type operator[] (size_t index) const
175 {
176 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
177 return (offset_type) extract_unsigned_integer (bytes,
178 sizeof (offset_type),
179 BFD_ENDIAN_LITTLE);
180 }
181
182 /* Return the number of offset_types in this array. */
183 size_t size () const
184 {
185 return m_bytes.size () / sizeof (offset_type);
186 }
187
188 /* Return true if this view is empty. */
189 bool empty () const
190 {
191 return m_bytes.empty ();
192 }
193
194 private:
195 /* The underlying bytes. */
196 gdb::array_view<const gdb_byte> m_bytes;
197 };
198
199 /* A description of the mapped index. The file format is described in
200 a comment by the code that writes the index. */
201 struct mapped_index final : public mapped_index_base
202 {
203 /* Index data format version. */
204 int version = 0;
205
206 /* The address table data. */
207 gdb::array_view<const gdb_byte> address_table;
208
209 /* The symbol table, implemented as a hash table. */
210 offset_view symbol_table;
211
212 /* A pointer to the constant pool. */
213 gdb::array_view<const gdb_byte> constant_pool;
214
215 /* Return the index into the constant pool of the name of the IDXth
216 symbol in the symbol table. */
217 offset_type symbol_name_index (offset_type idx) const
218 {
219 return symbol_table[2 * idx];
220 }
221
222 /* Return the index into the constant pool of the CU vector of the
223 IDXth symbol in the symbol table. */
224 offset_type symbol_vec_index (offset_type idx) const
225 {
226 return symbol_table[2 * idx + 1];
227 }
228
229 bool symbol_name_slot_invalid (offset_type idx) const override
230 {
231 return (symbol_name_index (idx) == 0
232 && symbol_vec_index (idx) == 0);
233 }
234
235 /* Convenience method to get at the name of the symbol at IDX in the
236 symbol table. */
237 const char *symbol_name_at
238 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
239 {
240 return (const char *) (this->constant_pool.data ()
241 + symbol_name_index (idx));
242 }
243
244 size_t symbol_name_count () const override
245 { return this->symbol_table.size () / 2; }
246
247 quick_symbol_functions_up make_quick_functions () const override;
248
249 bool version_check () const override
250 {
251 return version >= 8;
252 }
253 };
254
255 /* A description of the mapped .debug_names.
256 Uninitialized map has CU_COUNT 0. */
257 struct mapped_debug_names final : public mapped_index_base
258 {
259 bfd_endian dwarf5_byte_order;
260 bool dwarf5_is_dwarf64;
261 bool augmentation_is_gdb;
262 uint8_t offset_size;
263 uint32_t cu_count = 0;
264 uint32_t tu_count, bucket_count, name_count;
265 const gdb_byte *cu_table_reordered, *tu_table_reordered;
266 const uint32_t *bucket_table_reordered, *hash_table_reordered;
267 const gdb_byte *name_table_string_offs_reordered;
268 const gdb_byte *name_table_entry_offs_reordered;
269 const gdb_byte *entry_pool;
270
271 struct index_val
272 {
273 ULONGEST dwarf_tag;
274 struct attr
275 {
276 /* Attribute name DW_IDX_*. */
277 ULONGEST dw_idx;
278
279 /* Attribute form DW_FORM_*. */
280 ULONGEST form;
281
282 /* Value if FORM is DW_FORM_implicit_const. */
283 LONGEST implicit_const;
284 };
285 std::vector<attr> attr_vec;
286 };
287
288 std::unordered_map<ULONGEST, index_val> abbrev_map;
289
290 const char *namei_to_name
291 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
292
293 /* Implementation of the mapped_index_base virtual interface, for
294 the name_components cache. */
295
296 const char *symbol_name_at
297 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
298 { return namei_to_name (idx, per_objfile); }
299
300 size_t symbol_name_count () const override
301 { return this->name_count; }
302
303 quick_symbol_functions_up make_quick_functions () const override;
304 };
305
306 /* See dwarf2/read.h. */
307
308 dwarf2_per_objfile *
309 get_dwarf2_per_objfile (struct objfile *objfile)
310 {
311 return dwarf2_objfile_data_key.get (objfile);
312 }
313
314 /* Default names of the debugging sections. */
315
316 /* Note that if the debugging section has been compressed, it might
317 have a name like .zdebug_info. */
318
319 const struct dwarf2_debug_sections dwarf2_elf_names =
320 {
321 { ".debug_info", ".zdebug_info" },
322 { ".debug_abbrev", ".zdebug_abbrev" },
323 { ".debug_line", ".zdebug_line" },
324 { ".debug_loc", ".zdebug_loc" },
325 { ".debug_loclists", ".zdebug_loclists" },
326 { ".debug_macinfo", ".zdebug_macinfo" },
327 { ".debug_macro", ".zdebug_macro" },
328 { ".debug_str", ".zdebug_str" },
329 { ".debug_str_offsets", ".zdebug_str_offsets" },
330 { ".debug_line_str", ".zdebug_line_str" },
331 { ".debug_ranges", ".zdebug_ranges" },
332 { ".debug_rnglists", ".zdebug_rnglists" },
333 { ".debug_types", ".zdebug_types" },
334 { ".debug_addr", ".zdebug_addr" },
335 { ".debug_frame", ".zdebug_frame" },
336 { ".eh_frame", NULL },
337 { ".gdb_index", ".zgdb_index" },
338 { ".debug_names", ".zdebug_names" },
339 { ".debug_aranges", ".zdebug_aranges" },
340 23
341 };
342
343 /* List of DWO/DWP sections. */
344
345 static const struct dwop_section_names
346 {
347 struct dwarf2_section_names abbrev_dwo;
348 struct dwarf2_section_names info_dwo;
349 struct dwarf2_section_names line_dwo;
350 struct dwarf2_section_names loc_dwo;
351 struct dwarf2_section_names loclists_dwo;
352 struct dwarf2_section_names macinfo_dwo;
353 struct dwarf2_section_names macro_dwo;
354 struct dwarf2_section_names rnglists_dwo;
355 struct dwarf2_section_names str_dwo;
356 struct dwarf2_section_names str_offsets_dwo;
357 struct dwarf2_section_names types_dwo;
358 struct dwarf2_section_names cu_index;
359 struct dwarf2_section_names tu_index;
360 }
361 dwop_section_names =
362 {
363 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
364 { ".debug_info.dwo", ".zdebug_info.dwo" },
365 { ".debug_line.dwo", ".zdebug_line.dwo" },
366 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
367 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
368 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
369 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
370 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
371 { ".debug_str.dwo", ".zdebug_str.dwo" },
372 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
373 { ".debug_types.dwo", ".zdebug_types.dwo" },
374 { ".debug_cu_index", ".zdebug_cu_index" },
375 { ".debug_tu_index", ".zdebug_tu_index" },
376 };
377
378 /* local data types */
379
380 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
381 begin with a header, which contains the following information. */
382 struct loclists_rnglists_header
383 {
384 /* A 4-byte or 12-byte length containing the length of the
385 set of entries for this compilation unit, not including the
386 length field itself. */
387 unsigned int length;
388
389 /* A 2-byte version identifier. */
390 short version;
391
392 /* A 1-byte unsigned integer containing the size in bytes of an address on
393 the target system. */
394 unsigned char addr_size;
395
396 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
397 on the target system. */
398 unsigned char segment_collector_size;
399
400 /* A 4-byte count of the number of offsets that follow the header. */
401 unsigned int offset_entry_count;
402 };
403
404 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
405 This includes type_unit_group and quick_file_names. */
406
407 struct stmt_list_hash
408 {
409 /* The DWO unit this table is from or NULL if there is none. */
410 struct dwo_unit *dwo_unit;
411
412 /* Offset in .debug_line or .debug_line.dwo. */
413 sect_offset line_sect_off;
414 };
415
416 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
417 an object of this type. This contains elements of type unit groups
418 that can be shared across objfiles. The non-shareable parts are in
419 type_unit_group_unshareable. */
420
421 struct type_unit_group
422 {
423 /* The data used to construct the hash key. */
424 struct stmt_list_hash hash {};
425 };
426
427 /* These sections are what may appear in a (real or virtual) DWO file. */
428
429 struct dwo_sections
430 {
431 struct dwarf2_section_info abbrev;
432 struct dwarf2_section_info line;
433 struct dwarf2_section_info loc;
434 struct dwarf2_section_info loclists;
435 struct dwarf2_section_info macinfo;
436 struct dwarf2_section_info macro;
437 struct dwarf2_section_info rnglists;
438 struct dwarf2_section_info str;
439 struct dwarf2_section_info str_offsets;
440 /* In the case of a virtual DWO file, these two are unused. */
441 struct dwarf2_section_info info;
442 std::vector<dwarf2_section_info> types;
443 };
444
445 /* CUs/TUs in DWP/DWO files. */
446
447 struct dwo_unit
448 {
449 /* Backlink to the containing struct dwo_file. */
450 struct dwo_file *dwo_file;
451
452 /* The "id" that distinguishes this CU/TU.
453 .debug_info calls this "dwo_id", .debug_types calls this "signature".
454 Since signatures came first, we stick with it for consistency. */
455 ULONGEST signature;
456
457 /* The section this CU/TU lives in, in the DWO file. */
458 struct dwarf2_section_info *section;
459
460 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
461 sect_offset sect_off;
462 unsigned int length;
463
464 /* For types, offset in the type's DIE of the type defined by this TU. */
465 cu_offset type_offset_in_tu;
466 };
467
468 /* include/dwarf2.h defines the DWP section codes.
469 It defines a max value but it doesn't define a min value, which we
470 use for error checking, so provide one. */
471
472 enum dwp_v2_section_ids
473 {
474 DW_SECT_MIN = 1
475 };
476
477 /* Data for one DWO file.
478
479 This includes virtual DWO files (a virtual DWO file is a DWO file as it
480 appears in a DWP file). DWP files don't really have DWO files per se -
481 comdat folding of types "loses" the DWO file they came from, and from
482 a high level view DWP files appear to contain a mass of random types.
483 However, to maintain consistency with the non-DWP case we pretend DWP
484 files contain virtual DWO files, and we assign each TU with one virtual
485 DWO file (generally based on the line and abbrev section offsets -
486 a heuristic that seems to work in practice). */
487
488 struct dwo_file
489 {
490 dwo_file () = default;
491 DISABLE_COPY_AND_ASSIGN (dwo_file);
492
493 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
494 For virtual DWO files the name is constructed from the section offsets
495 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
496 from related CU+TUs. */
497 const char *dwo_name = nullptr;
498
499 /* The DW_AT_comp_dir attribute. */
500 const char *comp_dir = nullptr;
501
502 /* The bfd, when the file is open. Otherwise this is NULL.
503 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
504 gdb_bfd_ref_ptr dbfd;
505
506 /* The sections that make up this DWO file.
507 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
508 sections (for lack of a better name). */
509 struct dwo_sections sections {};
510
511 /* The CUs in the file.
512 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
513 an extension to handle LLVM's Link Time Optimization output (where
514 multiple source files may be compiled into a single object/dwo pair). */
515 htab_up cus;
516
517 /* Table of TUs in the file.
518 Each element is a struct dwo_unit. */
519 htab_up tus;
520 };
521
522 /* These sections are what may appear in a DWP file. */
523
524 struct dwp_sections
525 {
526 /* These are used by all DWP versions (1, 2 and 5). */
527 struct dwarf2_section_info str;
528 struct dwarf2_section_info cu_index;
529 struct dwarf2_section_info tu_index;
530
531 /* These are only used by DWP version 2 and version 5 files.
532 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
533 sections are referenced by section number, and are not recorded here.
534 In DWP version 2 or 5 there is at most one copy of all these sections,
535 each section being (effectively) comprised of the concatenation of all of
536 the individual sections that exist in the version 1 format.
537 To keep the code simple we treat each of these concatenated pieces as a
538 section itself (a virtual section?). */
539 struct dwarf2_section_info abbrev;
540 struct dwarf2_section_info info;
541 struct dwarf2_section_info line;
542 struct dwarf2_section_info loc;
543 struct dwarf2_section_info loclists;
544 struct dwarf2_section_info macinfo;
545 struct dwarf2_section_info macro;
546 struct dwarf2_section_info rnglists;
547 struct dwarf2_section_info str_offsets;
548 struct dwarf2_section_info types;
549 };
550
551 /* These sections are what may appear in a virtual DWO file in DWP version 1.
552 A virtual DWO file is a DWO file as it appears in a DWP file. */
553
554 struct virtual_v1_dwo_sections
555 {
556 struct dwarf2_section_info abbrev;
557 struct dwarf2_section_info line;
558 struct dwarf2_section_info loc;
559 struct dwarf2_section_info macinfo;
560 struct dwarf2_section_info macro;
561 struct dwarf2_section_info str_offsets;
562 /* Each DWP hash table entry records one CU or one TU.
563 That is recorded here, and copied to dwo_unit.section. */
564 struct dwarf2_section_info info_or_types;
565 };
566
567 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
568 In version 2, the sections of the DWO files are concatenated together
569 and stored in one section of that name. Thus each ELF section contains
570 several "virtual" sections. */
571
572 struct virtual_v2_or_v5_dwo_sections
573 {
574 bfd_size_type abbrev_offset;
575 bfd_size_type abbrev_size;
576
577 bfd_size_type line_offset;
578 bfd_size_type line_size;
579
580 bfd_size_type loc_offset;
581 bfd_size_type loc_size;
582
583 bfd_size_type loclists_offset;
584 bfd_size_type loclists_size;
585
586 bfd_size_type macinfo_offset;
587 bfd_size_type macinfo_size;
588
589 bfd_size_type macro_offset;
590 bfd_size_type macro_size;
591
592 bfd_size_type rnglists_offset;
593 bfd_size_type rnglists_size;
594
595 bfd_size_type str_offsets_offset;
596 bfd_size_type str_offsets_size;
597
598 /* Each DWP hash table entry records one CU or one TU.
599 That is recorded here, and copied to dwo_unit.section. */
600 bfd_size_type info_or_types_offset;
601 bfd_size_type info_or_types_size;
602 };
603
604 /* Contents of DWP hash tables. */
605
606 struct dwp_hash_table
607 {
608 uint32_t version, nr_columns;
609 uint32_t nr_units, nr_slots;
610 const gdb_byte *hash_table, *unit_table;
611 union
612 {
613 struct
614 {
615 const gdb_byte *indices;
616 } v1;
617 struct
618 {
619 /* This is indexed by column number and gives the id of the section
620 in that column. */
621 #define MAX_NR_V2_DWO_SECTIONS \
622 (1 /* .debug_info or .debug_types */ \
623 + 1 /* .debug_abbrev */ \
624 + 1 /* .debug_line */ \
625 + 1 /* .debug_loc */ \
626 + 1 /* .debug_str_offsets */ \
627 + 1 /* .debug_macro or .debug_macinfo */)
628 int section_ids[MAX_NR_V2_DWO_SECTIONS];
629 const gdb_byte *offsets;
630 const gdb_byte *sizes;
631 } v2;
632 struct
633 {
634 /* This is indexed by column number and gives the id of the section
635 in that column. */
636 #define MAX_NR_V5_DWO_SECTIONS \
637 (1 /* .debug_info */ \
638 + 1 /* .debug_abbrev */ \
639 + 1 /* .debug_line */ \
640 + 1 /* .debug_loclists */ \
641 + 1 /* .debug_str_offsets */ \
642 + 1 /* .debug_macro */ \
643 + 1 /* .debug_rnglists */)
644 int section_ids[MAX_NR_V5_DWO_SECTIONS];
645 const gdb_byte *offsets;
646 const gdb_byte *sizes;
647 } v5;
648 } section_pool;
649 };
650
651 /* Data for one DWP file. */
652
653 struct dwp_file
654 {
655 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
656 : name (name_),
657 dbfd (std::move (abfd))
658 {
659 }
660
661 /* Name of the file. */
662 const char *name;
663
664 /* File format version. */
665 int version = 0;
666
667 /* The bfd. */
668 gdb_bfd_ref_ptr dbfd;
669
670 /* Section info for this file. */
671 struct dwp_sections sections {};
672
673 /* Table of CUs in the file. */
674 const struct dwp_hash_table *cus = nullptr;
675
676 /* Table of TUs in the file. */
677 const struct dwp_hash_table *tus = nullptr;
678
679 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
680 htab_up loaded_cus;
681 htab_up loaded_tus;
682
683 /* Table to map ELF section numbers to their sections.
684 This is only needed for the DWP V1 file format. */
685 unsigned int num_sections = 0;
686 asection **elf_sections = nullptr;
687 };
688
689 /* Struct used to pass misc. parameters to read_die_and_children, et
690 al. which are used for both .debug_info and .debug_types dies.
691 All parameters here are unchanging for the life of the call. This
692 struct exists to abstract away the constant parameters of die reading. */
693
694 struct die_reader_specs
695 {
696 /* The bfd of die_section. */
697 bfd *abfd;
698
699 /* The CU of the DIE we are parsing. */
700 struct dwarf2_cu *cu;
701
702 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
703 struct dwo_file *dwo_file;
704
705 /* The section the die comes from.
706 This is either .debug_info or .debug_types, or the .dwo variants. */
707 struct dwarf2_section_info *die_section;
708
709 /* die_section->buffer. */
710 const gdb_byte *buffer;
711
712 /* The end of the buffer. */
713 const gdb_byte *buffer_end;
714
715 /* The abbreviation table to use when reading the DIEs. */
716 struct abbrev_table *abbrev_table;
717 };
718
719 /* A subclass of die_reader_specs that holds storage and has complex
720 constructor and destructor behavior. */
721
722 class cutu_reader : public die_reader_specs
723 {
724 public:
725
726 cutu_reader (dwarf2_per_cu_data *this_cu,
727 dwarf2_per_objfile *per_objfile,
728 struct abbrev_table *abbrev_table,
729 dwarf2_cu *existing_cu,
730 bool skip_partial,
731 abbrev_cache *cache = nullptr);
732
733 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
734 dwarf2_per_objfile *per_objfile,
735 struct dwarf2_cu *parent_cu = nullptr,
736 struct dwo_file *dwo_file = nullptr);
737
738 DISABLE_COPY_AND_ASSIGN (cutu_reader);
739
740 cutu_reader (cutu_reader &&) = default;
741
742 const gdb_byte *info_ptr = nullptr;
743 struct die_info *comp_unit_die = nullptr;
744 bool dummy_p = false;
745
746 /* Release the new CU, putting it on the chain. This cannot be done
747 for dummy CUs. */
748 void keep ();
749
750 /* Release the abbrev table, transferring ownership to the
751 caller. */
752 abbrev_table_up release_abbrev_table ()
753 {
754 return std::move (m_abbrev_table_holder);
755 }
756
757 private:
758 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
759 dwarf2_per_objfile *per_objfile,
760 dwarf2_cu *existing_cu);
761
762 struct dwarf2_per_cu_data *m_this_cu;
763 std::unique_ptr<dwarf2_cu> m_new_cu;
764
765 /* The ordinary abbreviation table. */
766 abbrev_table_up m_abbrev_table_holder;
767
768 /* The DWO abbreviation table. */
769 abbrev_table_up m_dwo_abbrev_table;
770 };
771
772 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
773 but this would require a corresponding change in unpack_field_as_long
774 and friends. */
775 static int bits_per_byte = 8;
776
777 struct variant_part_builder;
778
779 /* When reading a variant, we track a bit more information about the
780 field, and store it in an object of this type. */
781
782 struct variant_field
783 {
784 int first_field = -1;
785 int last_field = -1;
786
787 /* A variant can contain other variant parts. */
788 std::vector<variant_part_builder> variant_parts;
789
790 /* If we see a DW_TAG_variant, then this will be set if this is the
791 default branch. */
792 bool default_branch = false;
793 /* If we see a DW_AT_discr_value, then this will be the discriminant
794 value. */
795 ULONGEST discriminant_value = 0;
796 /* If we see a DW_AT_discr_list, then this is a pointer to the list
797 data. */
798 struct dwarf_block *discr_list_data = nullptr;
799 };
800
801 /* This represents a DW_TAG_variant_part. */
802
803 struct variant_part_builder
804 {
805 /* The offset of the discriminant field. */
806 sect_offset discriminant_offset {};
807
808 /* Variants that are direct children of this variant part. */
809 std::vector<variant_field> variants;
810
811 /* True if we're currently reading a variant. */
812 bool processing_variant = false;
813 };
814
815 struct nextfield
816 {
817 int accessibility = 0;
818 int virtuality = 0;
819 /* Variant parts need to find the discriminant, which is a DIE
820 reference. We track the section offset of each field to make
821 this link. */
822 sect_offset offset;
823 struct field field {};
824 };
825
826 struct fnfieldlist
827 {
828 const char *name = nullptr;
829 std::vector<struct fn_field> fnfields;
830 };
831
832 /* The routines that read and process dies for a C struct or C++ class
833 pass lists of data member fields and lists of member function fields
834 in an instance of a field_info structure, as defined below. */
835 struct field_info
836 {
837 /* List of data member and baseclasses fields. */
838 std::vector<struct nextfield> fields;
839 std::vector<struct nextfield> baseclasses;
840
841 /* Set if the accessibility of one of the fields is not public. */
842 bool non_public_fields = false;
843
844 /* Member function fieldlist array, contains name of possibly overloaded
845 member function, number of overloaded member functions and a pointer
846 to the head of the member function field chain. */
847 std::vector<struct fnfieldlist> fnfieldlists;
848
849 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
850 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
851 std::vector<struct decl_field> typedef_field_list;
852
853 /* Nested types defined by this class and the number of elements in this
854 list. */
855 std::vector<struct decl_field> nested_types_list;
856
857 /* If non-null, this is the variant part we are currently
858 reading. */
859 variant_part_builder *current_variant_part = nullptr;
860 /* This holds all the top-level variant parts attached to the type
861 we're reading. */
862 std::vector<variant_part_builder> variant_parts;
863
864 /* Return the total number of fields (including baseclasses). */
865 int nfields () const
866 {
867 return fields.size () + baseclasses.size ();
868 }
869 };
870
871 /* Loaded secondary compilation units are kept in memory until they
872 have not been referenced for the processing of this many
873 compilation units. Set this to zero to disable caching. Cache
874 sizes of up to at least twenty will improve startup time for
875 typical inter-CU-reference binaries, at an obvious memory cost. */
876 static int dwarf_max_cache_age = 5;
877 static void
878 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
879 struct cmd_list_element *c, const char *value)
880 {
881 gdb_printf (file, _("The upper bound on the age of cached "
882 "DWARF compilation units is %s.\n"),
883 value);
884 }
885 \f
886 /* local function prototypes */
887
888 static void dwarf2_find_base_address (struct die_info *die,
889 struct dwarf2_cu *cu);
890
891 class cooked_index_storage;
892 static void build_type_psymtabs_reader (cutu_reader *reader,
893 cooked_index_storage *storage);
894
895 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
896
897 static void var_decode_location (struct attribute *attr,
898 struct symbol *sym,
899 struct dwarf2_cu *cu);
900
901 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
902
903 static const gdb_byte *read_attribute (const struct die_reader_specs *,
904 struct attribute *,
905 const struct attr_abbrev *,
906 const gdb_byte *);
907
908 static void read_attribute_reprocess (const struct die_reader_specs *reader,
909 struct attribute *attr, dwarf_tag tag);
910
911 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
912
913 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
914 dwarf2_section_info *, sect_offset);
915
916 static const char *read_indirect_string
917 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
918 const struct comp_unit_head *, unsigned int *);
919
920 static const char *read_indirect_string_at_offset
921 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
922
923 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
924 const gdb_byte *,
925 unsigned int *);
926
927 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
928 ULONGEST str_index);
929
930 static const char *read_stub_str_index (struct dwarf2_cu *cu,
931 ULONGEST str_index);
932
933 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
934 struct dwarf2_cu *);
935
936 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
937 struct dwarf2_cu *cu);
938
939 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
940
941 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
942 struct dwarf2_cu *cu);
943
944 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
945
946 static struct die_info *die_specification (struct die_info *die,
947 struct dwarf2_cu **);
948
949 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
950 struct dwarf2_cu *cu);
951
952 static void dwarf_decode_lines (struct line_header *,
953 struct dwarf2_cu *,
954 CORE_ADDR, int decode_mapping);
955
956 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
957 const char *);
958
959 static struct symbol *new_symbol (struct die_info *, struct type *,
960 struct dwarf2_cu *, struct symbol * = NULL);
961
962 static void dwarf2_const_value (const struct attribute *, struct symbol *,
963 struct dwarf2_cu *);
964
965 static void dwarf2_const_value_attr (const struct attribute *attr,
966 struct type *type,
967 const char *name,
968 struct obstack *obstack,
969 struct dwarf2_cu *cu, LONGEST *value,
970 const gdb_byte **bytes,
971 struct dwarf2_locexpr_baton **baton);
972
973 static struct type *read_subrange_index_type (struct die_info *die,
974 struct dwarf2_cu *cu);
975
976 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
977
978 static int need_gnat_info (struct dwarf2_cu *);
979
980 static struct type *die_descriptive_type (struct die_info *,
981 struct dwarf2_cu *);
982
983 static void set_descriptive_type (struct type *, struct die_info *,
984 struct dwarf2_cu *);
985
986 static struct type *die_containing_type (struct die_info *,
987 struct dwarf2_cu *);
988
989 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
990 struct dwarf2_cu *);
991
992 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
993
994 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
995
996 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
997
998 static char *typename_concat (struct obstack *obs, const char *prefix,
999 const char *suffix, int physname,
1000 struct dwarf2_cu *cu);
1001
1002 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1003
1004 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1005
1006 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1007
1008 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1009
1010 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1011
1012 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1013
1014 /* Return the .debug_loclists section to use for cu. */
1015 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1016
1017 /* Return the .debug_rnglists section to use for cu. */
1018 static struct dwarf2_section_info *cu_debug_rnglists_section
1019 (struct dwarf2_cu *cu, dwarf_tag tag);
1020
1021 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1022 values. Keep the items ordered with increasing constraints compliance. */
1023 enum pc_bounds_kind
1024 {
1025 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1026 PC_BOUNDS_NOT_PRESENT,
1027
1028 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1029 were present but they do not form a valid range of PC addresses. */
1030 PC_BOUNDS_INVALID,
1031
1032 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1033 PC_BOUNDS_RANGES,
1034
1035 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1036 PC_BOUNDS_HIGH_LOW,
1037 };
1038
1039 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1040 CORE_ADDR *, CORE_ADDR *,
1041 struct dwarf2_cu *,
1042 addrmap *,
1043 void *);
1044
1045 static void get_scope_pc_bounds (struct die_info *,
1046 CORE_ADDR *, CORE_ADDR *,
1047 struct dwarf2_cu *);
1048
1049 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1050 CORE_ADDR, struct dwarf2_cu *);
1051
1052 static void dwarf2_add_field (struct field_info *, struct die_info *,
1053 struct dwarf2_cu *);
1054
1055 static void dwarf2_attach_fields_to_type (struct field_info *,
1056 struct type *, struct dwarf2_cu *);
1057
1058 static void dwarf2_add_member_fn (struct field_info *,
1059 struct die_info *, struct type *,
1060 struct dwarf2_cu *);
1061
1062 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1063 struct type *,
1064 struct dwarf2_cu *);
1065
1066 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1067
1068 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1069
1070 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1071
1072 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1073
1074 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1075
1076 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1077
1078 static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
1079
1080 static struct type *read_module_type (struct die_info *die,
1081 struct dwarf2_cu *cu);
1082
1083 static const char *namespace_name (struct die_info *die,
1084 int *is_anonymous, struct dwarf2_cu *);
1085
1086 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1087
1088 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1089 bool * = nullptr);
1090
1091 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1092 struct dwarf2_cu *);
1093
1094 static struct die_info *read_die_and_siblings_1
1095 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1096 struct die_info *);
1097
1098 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1099 const gdb_byte *info_ptr,
1100 const gdb_byte **new_info_ptr,
1101 struct die_info *parent);
1102
1103 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1104 struct die_info **, const gdb_byte *,
1105 int);
1106
1107 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1108 struct die_info **, const gdb_byte *);
1109
1110 static void process_die (struct die_info *, struct dwarf2_cu *);
1111
1112 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1113 struct objfile *);
1114
1115 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1116
1117 static const char *dwarf2_full_name (const char *name,
1118 struct die_info *die,
1119 struct dwarf2_cu *cu);
1120
1121 static const char *dwarf2_physname (const char *name, struct die_info *die,
1122 struct dwarf2_cu *cu);
1123
1124 static struct die_info *dwarf2_extension (struct die_info *die,
1125 struct dwarf2_cu **);
1126
1127 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1128
1129 static void dump_die_for_error (struct die_info *);
1130
1131 static void dump_die_1 (struct ui_file *, int level, int max_level,
1132 struct die_info *);
1133
1134 /*static*/ void dump_die (struct die_info *, int max_level);
1135
1136 static void store_in_ref_table (struct die_info *,
1137 struct dwarf2_cu *);
1138
1139 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1140 const struct attribute *,
1141 struct dwarf2_cu **);
1142
1143 static struct die_info *follow_die_ref (struct die_info *,
1144 const struct attribute *,
1145 struct dwarf2_cu **);
1146
1147 static struct die_info *follow_die_sig (struct die_info *,
1148 const struct attribute *,
1149 struct dwarf2_cu **);
1150
1151 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1152 struct dwarf2_cu *);
1153
1154 static struct type *get_DW_AT_signature_type (struct die_info *,
1155 const struct attribute *,
1156 struct dwarf2_cu *);
1157
1158 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1159 dwarf2_per_objfile *per_objfile);
1160
1161 static void read_signatured_type (signatured_type *sig_type,
1162 dwarf2_per_objfile *per_objfile);
1163
1164 static int attr_to_dynamic_prop (const struct attribute *attr,
1165 struct die_info *die, struct dwarf2_cu *cu,
1166 struct dynamic_prop *prop, struct type *type);
1167
1168 /* memory allocation interface */
1169
1170 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1171
1172 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1173
1174 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1175
1176 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1177 struct dwarf2_loclist_baton *baton,
1178 const struct attribute *attr);
1179
1180 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1181 struct symbol *sym,
1182 struct dwarf2_cu *cu,
1183 int is_block);
1184
1185 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1186 const gdb_byte *info_ptr,
1187 const struct abbrev_info *abbrev,
1188 bool do_skip_children = true);
1189
1190 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1191 (sect_offset sect_off, unsigned int offset_in_dwz,
1192 dwarf2_per_bfd *per_bfd);
1193
1194 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1195 struct die_info *comp_unit_die,
1196 enum language pretend_language);
1197
1198 static struct type *set_die_type (struct die_info *, struct type *,
1199 struct dwarf2_cu *, bool = false);
1200
1201 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1202
1203 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1204 dwarf2_per_objfile *per_objfile,
1205 dwarf2_cu *existing_cu,
1206 bool skip_partial,
1207 enum language pretend_language);
1208
1209 static void process_full_comp_unit (dwarf2_cu *cu,
1210 enum language pretend_language);
1211
1212 static void process_full_type_unit (dwarf2_cu *cu,
1213 enum language pretend_language);
1214
1215 static struct type *get_die_type_at_offset (sect_offset,
1216 dwarf2_per_cu_data *per_cu,
1217 dwarf2_per_objfile *per_objfile);
1218
1219 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1220
1221 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1222 dwarf2_per_objfile *per_objfile,
1223 enum language pretend_language);
1224
1225 static void process_queue (dwarf2_per_objfile *per_objfile);
1226
1227 /* Class, the destructor of which frees all allocated queue entries. This
1228 will only have work to do if an error was thrown while processing the
1229 dwarf. If no error was thrown then the queue entries should have all
1230 been processed, and freed, as we went along. */
1231
1232 class dwarf2_queue_guard
1233 {
1234 public:
1235 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1236 : m_per_objfile (per_objfile)
1237 {
1238 gdb_assert (!m_per_objfile->queue.has_value ());
1239
1240 m_per_objfile->queue.emplace ();
1241 }
1242
1243 /* Free any entries remaining on the queue. There should only be
1244 entries left if we hit an error while processing the dwarf. */
1245 ~dwarf2_queue_guard ()
1246 {
1247 gdb_assert (m_per_objfile->queue.has_value ());
1248
1249 m_per_objfile->queue.reset ();
1250 }
1251
1252 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1253
1254 private:
1255 dwarf2_per_objfile *m_per_objfile;
1256 };
1257
1258 dwarf2_queue_item::~dwarf2_queue_item ()
1259 {
1260 /* Anything still marked queued is likely to be in an
1261 inconsistent state, so discard it. */
1262 if (per_cu->queued)
1263 {
1264 per_objfile->remove_cu (per_cu);
1265 per_cu->queued = 0;
1266 }
1267 }
1268
1269 /* See dwarf2/read.h. */
1270
1271 void
1272 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1273 {
1274 if (data->is_debug_types)
1275 delete static_cast<signatured_type *> (data);
1276 else
1277 delete data;
1278 }
1279
1280 static file_and_directory &find_file_and_directory
1281 (struct die_info *die, struct dwarf2_cu *cu);
1282
1283 static const char *compute_include_file_name
1284 (const struct line_header *lh,
1285 const file_entry &fe,
1286 const file_and_directory &cu_info,
1287 std::string &name_holder);
1288
1289 static htab_up allocate_signatured_type_table ();
1290
1291 static htab_up allocate_dwo_unit_table ();
1292
1293 static struct dwo_unit *lookup_dwo_unit_in_dwp
1294 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1295 const char *comp_dir, ULONGEST signature, int is_debug_types);
1296
1297 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1298
1299 static struct dwo_unit *lookup_dwo_comp_unit
1300 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1301 ULONGEST signature);
1302
1303 static struct dwo_unit *lookup_dwo_type_unit
1304 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1305
1306 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1307
1308 /* A unique pointer to a dwo_file. */
1309
1310 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1311
1312 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1313
1314 static void check_producer (struct dwarf2_cu *cu);
1315 \f
1316 /* Various complaints about symbol reading that don't abort the process. */
1317
1318 static void
1319 dwarf2_debug_line_missing_file_complaint (void)
1320 {
1321 complaint (_(".debug_line section has line data without a file"));
1322 }
1323
1324 static void
1325 dwarf2_debug_line_missing_end_sequence_complaint (void)
1326 {
1327 complaint (_(".debug_line section has line "
1328 "program sequence without an end"));
1329 }
1330
1331 static void
1332 dwarf2_complex_location_expr_complaint (void)
1333 {
1334 complaint (_("location expression too complex"));
1335 }
1336
1337 static void
1338 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1339 int arg3)
1340 {
1341 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1342 arg1, arg2, arg3);
1343 }
1344
1345 static void
1346 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1347 {
1348 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1349 arg1, arg2);
1350 }
1351
1352 /* Hash function for line_header_hash. */
1353
1354 static hashval_t
1355 line_header_hash (const struct line_header *ofs)
1356 {
1357 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1358 }
1359
1360 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1361
1362 static hashval_t
1363 line_header_hash_voidp (const void *item)
1364 {
1365 const struct line_header *ofs = (const struct line_header *) item;
1366
1367 return line_header_hash (ofs);
1368 }
1369
1370 /* Equality function for line_header_hash. */
1371
1372 static int
1373 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1374 {
1375 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1376 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1377
1378 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1379 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1380 }
1381
1382 \f
1383
1384 /* An iterator for all_comp_units that is based on index. This
1385 approach makes it possible to iterate over all_comp_units safely,
1386 when some caller in the loop may add new units. */
1387
1388 class all_comp_units_iterator
1389 {
1390 public:
1391
1392 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1393 : m_per_bfd (per_bfd),
1394 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1395 {
1396 }
1397
1398 all_comp_units_iterator &operator++ ()
1399 {
1400 ++m_index;
1401 return *this;
1402 }
1403
1404 dwarf2_per_cu_data *operator* () const
1405 {
1406 return m_per_bfd->get_cu (m_index);
1407 }
1408
1409 bool operator== (const all_comp_units_iterator &other) const
1410 {
1411 return m_index == other.m_index;
1412 }
1413
1414
1415 bool operator!= (const all_comp_units_iterator &other) const
1416 {
1417 return m_index != other.m_index;
1418 }
1419
1420 private:
1421
1422 dwarf2_per_bfd *m_per_bfd;
1423 size_t m_index;
1424 };
1425
1426 /* A range adapter for the all_comp_units_iterator. */
1427 class all_comp_units_range
1428 {
1429 public:
1430
1431 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1432 : m_per_bfd (per_bfd)
1433 {
1434 }
1435
1436 all_comp_units_iterator begin ()
1437 {
1438 return all_comp_units_iterator (m_per_bfd, true);
1439 }
1440
1441 all_comp_units_iterator end ()
1442 {
1443 return all_comp_units_iterator (m_per_bfd, false);
1444 }
1445
1446 private:
1447
1448 dwarf2_per_bfd *m_per_bfd;
1449 };
1450
1451 /* See declaration. */
1452
1453 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1454 bool can_copy_)
1455 : obfd (obfd),
1456 can_copy (can_copy_)
1457 {
1458 if (names == NULL)
1459 names = &dwarf2_elf_names;
1460
1461 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1462 locate_sections (obfd, sec, *names);
1463 }
1464
1465 dwarf2_per_bfd::~dwarf2_per_bfd ()
1466 {
1467 for (auto &per_cu : all_comp_units)
1468 {
1469 per_cu->imported_symtabs_free ();
1470 per_cu->free_cached_file_names ();
1471 }
1472
1473 /* Everything else should be on this->obstack. */
1474 }
1475
1476 /* See read.h. */
1477
1478 void
1479 dwarf2_per_objfile::remove_all_cus ()
1480 {
1481 gdb_assert (!queue.has_value ());
1482
1483 m_dwarf2_cus.clear ();
1484 }
1485
1486 /* A helper class that calls free_cached_comp_units on
1487 destruction. */
1488
1489 class free_cached_comp_units
1490 {
1491 public:
1492
1493 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1494 : m_per_objfile (per_objfile)
1495 {
1496 }
1497
1498 ~free_cached_comp_units ()
1499 {
1500 m_per_objfile->remove_all_cus ();
1501 }
1502
1503 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1504
1505 private:
1506
1507 dwarf2_per_objfile *m_per_objfile;
1508 };
1509
1510 /* See read.h. */
1511
1512 bool
1513 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1514 {
1515 if (per_cu->index < this->m_symtabs.size ())
1516 return this->m_symtabs[per_cu->index] != nullptr;
1517 return false;
1518 }
1519
1520 /* See read.h. */
1521
1522 compunit_symtab *
1523 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1524 {
1525 if (per_cu->index < this->m_symtabs.size ())
1526 return this->m_symtabs[per_cu->index];
1527 return nullptr;
1528 }
1529
1530 /* See read.h. */
1531
1532 void
1533 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1534 compunit_symtab *symtab)
1535 {
1536 if (per_cu->index >= this->m_symtabs.size ())
1537 this->m_symtabs.resize (per_cu->index + 1);
1538 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1539 this->m_symtabs[per_cu->index] = symtab;
1540 }
1541
1542 /* Try to locate the sections we need for DWARF 2 debugging
1543 information and return true if we have enough to do something.
1544 NAMES points to the dwarf2 section names, or is NULL if the standard
1545 ELF names are used. CAN_COPY is true for formats where symbol
1546 interposition is possible and so symbol values must follow copy
1547 relocation rules. */
1548
1549 int
1550 dwarf2_has_info (struct objfile *objfile,
1551 const struct dwarf2_debug_sections *names,
1552 bool can_copy)
1553 {
1554 if (objfile->flags & OBJF_READNEVER)
1555 return 0;
1556
1557 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1558
1559 if (per_objfile == NULL)
1560 {
1561 dwarf2_per_bfd *per_bfd;
1562
1563 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1564 BFD doesn't require relocations.
1565
1566 We don't share with objfiles for which -readnow was requested,
1567 because it would complicate things when loading the same BFD with
1568 -readnow and then without -readnow. */
1569 if (!gdb_bfd_requires_relocations (objfile->obfd)
1570 && (objfile->flags & OBJF_READNOW) == 0)
1571 {
1572 /* See if one has been created for this BFD yet. */
1573 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1574
1575 if (per_bfd == nullptr)
1576 {
1577 /* No, create it now. */
1578 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1579 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1580 }
1581 }
1582 else
1583 {
1584 /* No sharing possible, create one specifically for this objfile. */
1585 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1586 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1587 }
1588
1589 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1590 }
1591
1592 return (!per_objfile->per_bfd->info.is_virtual
1593 && per_objfile->per_bfd->info.s.section != NULL
1594 && !per_objfile->per_bfd->abbrev.is_virtual
1595 && per_objfile->per_bfd->abbrev.s.section != NULL);
1596 }
1597
1598 /* See declaration. */
1599
1600 void
1601 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1602 const dwarf2_debug_sections &names)
1603 {
1604 flagword aflag = bfd_section_flags (sectp);
1605
1606 if ((aflag & SEC_HAS_CONTENTS) == 0)
1607 {
1608 }
1609 else if (elf_section_data (sectp)->this_hdr.sh_size
1610 > bfd_get_file_size (abfd))
1611 {
1612 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1613 warning (_("Discarding section %s which has a section size (%s"
1614 ") larger than the file size [in module %s]"),
1615 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1616 bfd_get_filename (abfd));
1617 }
1618 else if (names.info.matches (sectp->name))
1619 {
1620 this->info.s.section = sectp;
1621 this->info.size = bfd_section_size (sectp);
1622 }
1623 else if (names.abbrev.matches (sectp->name))
1624 {
1625 this->abbrev.s.section = sectp;
1626 this->abbrev.size = bfd_section_size (sectp);
1627 }
1628 else if (names.line.matches (sectp->name))
1629 {
1630 this->line.s.section = sectp;
1631 this->line.size = bfd_section_size (sectp);
1632 }
1633 else if (names.loc.matches (sectp->name))
1634 {
1635 this->loc.s.section = sectp;
1636 this->loc.size = bfd_section_size (sectp);
1637 }
1638 else if (names.loclists.matches (sectp->name))
1639 {
1640 this->loclists.s.section = sectp;
1641 this->loclists.size = bfd_section_size (sectp);
1642 }
1643 else if (names.macinfo.matches (sectp->name))
1644 {
1645 this->macinfo.s.section = sectp;
1646 this->macinfo.size = bfd_section_size (sectp);
1647 }
1648 else if (names.macro.matches (sectp->name))
1649 {
1650 this->macro.s.section = sectp;
1651 this->macro.size = bfd_section_size (sectp);
1652 }
1653 else if (names.str.matches (sectp->name))
1654 {
1655 this->str.s.section = sectp;
1656 this->str.size = bfd_section_size (sectp);
1657 }
1658 else if (names.str_offsets.matches (sectp->name))
1659 {
1660 this->str_offsets.s.section = sectp;
1661 this->str_offsets.size = bfd_section_size (sectp);
1662 }
1663 else if (names.line_str.matches (sectp->name))
1664 {
1665 this->line_str.s.section = sectp;
1666 this->line_str.size = bfd_section_size (sectp);
1667 }
1668 else if (names.addr.matches (sectp->name))
1669 {
1670 this->addr.s.section = sectp;
1671 this->addr.size = bfd_section_size (sectp);
1672 }
1673 else if (names.frame.matches (sectp->name))
1674 {
1675 this->frame.s.section = sectp;
1676 this->frame.size = bfd_section_size (sectp);
1677 }
1678 else if (names.eh_frame.matches (sectp->name))
1679 {
1680 this->eh_frame.s.section = sectp;
1681 this->eh_frame.size = bfd_section_size (sectp);
1682 }
1683 else if (names.ranges.matches (sectp->name))
1684 {
1685 this->ranges.s.section = sectp;
1686 this->ranges.size = bfd_section_size (sectp);
1687 }
1688 else if (names.rnglists.matches (sectp->name))
1689 {
1690 this->rnglists.s.section = sectp;
1691 this->rnglists.size = bfd_section_size (sectp);
1692 }
1693 else if (names.types.matches (sectp->name))
1694 {
1695 struct dwarf2_section_info type_section;
1696
1697 memset (&type_section, 0, sizeof (type_section));
1698 type_section.s.section = sectp;
1699 type_section.size = bfd_section_size (sectp);
1700
1701 this->types.push_back (type_section);
1702 }
1703 else if (names.gdb_index.matches (sectp->name))
1704 {
1705 this->gdb_index.s.section = sectp;
1706 this->gdb_index.size = bfd_section_size (sectp);
1707 }
1708 else if (names.debug_names.matches (sectp->name))
1709 {
1710 this->debug_names.s.section = sectp;
1711 this->debug_names.size = bfd_section_size (sectp);
1712 }
1713 else if (names.debug_aranges.matches (sectp->name))
1714 {
1715 this->debug_aranges.s.section = sectp;
1716 this->debug_aranges.size = bfd_section_size (sectp);
1717 }
1718
1719 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1720 && bfd_section_vma (sectp) == 0)
1721 this->has_section_at_zero = true;
1722 }
1723
1724 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1725 SECTION_NAME. */
1726
1727 void
1728 dwarf2_get_section_info (struct objfile *objfile,
1729 enum dwarf2_section_enum sect,
1730 asection **sectp, const gdb_byte **bufp,
1731 bfd_size_type *sizep)
1732 {
1733 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1734 struct dwarf2_section_info *info;
1735
1736 /* We may see an objfile without any DWARF, in which case we just
1737 return nothing. */
1738 if (per_objfile == NULL)
1739 {
1740 *sectp = NULL;
1741 *bufp = NULL;
1742 *sizep = 0;
1743 return;
1744 }
1745 switch (sect)
1746 {
1747 case DWARF2_DEBUG_FRAME:
1748 info = &per_objfile->per_bfd->frame;
1749 break;
1750 case DWARF2_EH_FRAME:
1751 info = &per_objfile->per_bfd->eh_frame;
1752 break;
1753 default:
1754 gdb_assert_not_reached ("unexpected section");
1755 }
1756
1757 info->read (objfile);
1758
1759 *sectp = info->get_bfd_section ();
1760 *bufp = info->buffer;
1761 *sizep = info->size;
1762 }
1763
1764 /* See dwarf2/read.h. */
1765
1766 void
1767 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1768 {
1769 info.read (objfile);
1770 abbrev.read (objfile);
1771 line.read (objfile);
1772 str.read (objfile);
1773 str_offsets.read (objfile);
1774 line_str.read (objfile);
1775 ranges.read (objfile);
1776 rnglists.read (objfile);
1777 addr.read (objfile);
1778
1779 for (auto &section : types)
1780 section.read (objfile);
1781 }
1782
1783 \f
1784 /* DWARF quick_symbol_functions support. */
1785
1786 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1787 unique line tables, so we maintain a separate table of all .debug_line
1788 derived entries to support the sharing.
1789 All the quick functions need is the list of file names. We discard the
1790 line_header when we're done and don't need to record it here. */
1791 struct quick_file_names
1792 {
1793 /* The data used to construct the hash key. */
1794 struct stmt_list_hash hash;
1795
1796 /* The number of entries in file_names, real_names. */
1797 unsigned int num_file_names;
1798
1799 /* The CU directory, as given by DW_AT_comp_dir. May be
1800 nullptr. */
1801 const char *comp_dir;
1802
1803 /* The file names from the line table, after being run through
1804 file_full_name. */
1805 const char **file_names;
1806
1807 /* The file names from the line table after being run through
1808 gdb_realpath. These are computed lazily. */
1809 const char **real_names;
1810 };
1811
1812 struct dwarf2_base_index_functions : public quick_symbol_functions
1813 {
1814 bool has_symbols (struct objfile *objfile) override;
1815
1816 bool has_unexpanded_symtabs (struct objfile *objfile) override;
1817
1818 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
1819
1820 void forget_cached_source_info (struct objfile *objfile) override;
1821
1822 enum language lookup_global_symbol_language (struct objfile *objfile,
1823 const char *name,
1824 domain_enum domain,
1825 bool *symbol_found_p) override
1826 {
1827 *symbol_found_p = false;
1828 return language_unknown;
1829 }
1830
1831 void print_stats (struct objfile *objfile, bool print_bcache) override;
1832
1833 void expand_all_symtabs (struct objfile *objfile) override;
1834
1835 /* A helper function that finds the per-cu object from an "adjusted"
1836 PC -- a PC with the base text offset removed. */
1837 virtual dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
1838 CORE_ADDR adjusted_pc);
1839
1840 struct compunit_symtab *find_pc_sect_compunit_symtab
1841 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
1842 CORE_ADDR pc, struct obj_section *section, int warn_if_readin)
1843 override final;
1844
1845 struct compunit_symtab *find_compunit_symtab_by_address
1846 (struct objfile *objfile, CORE_ADDR address) override
1847 {
1848 return nullptr;
1849 }
1850
1851 void map_symbol_filenames (struct objfile *objfile,
1852 gdb::function_view<symbol_filename_ftype> fun,
1853 bool need_fullname) override;
1854 };
1855
1856 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1857 It's handy in this case to have an empty implementation of the
1858 quick symbol functions, to avoid special cases in the rest of the
1859 code. */
1860
1861 struct readnow_functions : public dwarf2_base_index_functions
1862 {
1863 void dump (struct objfile *objfile) override
1864 {
1865 }
1866
1867 void expand_matching_symbols
1868 (struct objfile *,
1869 const lookup_name_info &lookup_name,
1870 domain_enum domain,
1871 int global,
1872 symbol_compare_ftype *ordered_compare) override
1873 {
1874 }
1875
1876 bool expand_symtabs_matching
1877 (struct objfile *objfile,
1878 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1879 const lookup_name_info *lookup_name,
1880 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1881 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1882 block_search_flags search_flags,
1883 domain_enum domain,
1884 enum search_domain kind) override
1885 {
1886 return true;
1887 }
1888 };
1889
1890 struct dwarf2_gdb_index : public dwarf2_base_index_functions
1891 {
1892 void dump (struct objfile *objfile) override;
1893
1894 void expand_matching_symbols
1895 (struct objfile *,
1896 const lookup_name_info &lookup_name,
1897 domain_enum domain,
1898 int global,
1899 symbol_compare_ftype *ordered_compare) override;
1900
1901 bool expand_symtabs_matching
1902 (struct objfile *objfile,
1903 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1904 const lookup_name_info *lookup_name,
1905 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1906 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1907 block_search_flags search_flags,
1908 domain_enum domain,
1909 enum search_domain kind) override;
1910 };
1911
1912 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
1913 {
1914 void dump (struct objfile *objfile) override;
1915
1916 void expand_matching_symbols
1917 (struct objfile *,
1918 const lookup_name_info &lookup_name,
1919 domain_enum domain,
1920 int global,
1921 symbol_compare_ftype *ordered_compare) override;
1922
1923 bool expand_symtabs_matching
1924 (struct objfile *objfile,
1925 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1926 const lookup_name_info *lookup_name,
1927 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1928 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1929 block_search_flags search_flags,
1930 domain_enum domain,
1931 enum search_domain kind) override;
1932 };
1933
1934 quick_symbol_functions_up
1935 mapped_index::make_quick_functions () const
1936 {
1937 return quick_symbol_functions_up (new dwarf2_gdb_index);
1938 }
1939
1940 quick_symbol_functions_up
1941 mapped_debug_names::make_quick_functions () const
1942 {
1943 return quick_symbol_functions_up (new dwarf2_debug_names_index);
1944 }
1945
1946 /* Utility hash function for a stmt_list_hash. */
1947
1948 static hashval_t
1949 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1950 {
1951 hashval_t v = 0;
1952
1953 if (stmt_list_hash->dwo_unit != NULL)
1954 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1955 v += to_underlying (stmt_list_hash->line_sect_off);
1956 return v;
1957 }
1958
1959 /* Utility equality function for a stmt_list_hash. */
1960
1961 static int
1962 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1963 const struct stmt_list_hash *rhs)
1964 {
1965 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1966 return 0;
1967 if (lhs->dwo_unit != NULL
1968 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1969 return 0;
1970
1971 return lhs->line_sect_off == rhs->line_sect_off;
1972 }
1973
1974 /* Hash function for a quick_file_names. */
1975
1976 static hashval_t
1977 hash_file_name_entry (const void *e)
1978 {
1979 const struct quick_file_names *file_data
1980 = (const struct quick_file_names *) e;
1981
1982 return hash_stmt_list_entry (&file_data->hash);
1983 }
1984
1985 /* Equality function for a quick_file_names. */
1986
1987 static int
1988 eq_file_name_entry (const void *a, const void *b)
1989 {
1990 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1991 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1992
1993 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1994 }
1995
1996 /* Create a quick_file_names hash table. */
1997
1998 static htab_up
1999 create_quick_file_names_table (unsigned int nr_initial_entries)
2000 {
2001 return htab_up (htab_create_alloc (nr_initial_entries,
2002 hash_file_name_entry, eq_file_name_entry,
2003 nullptr, xcalloc, xfree));
2004 }
2005
2006 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2007 function is unrelated to symtabs, symtab would have to be created afterwards.
2008 You should call age_cached_comp_units after processing the CU. */
2009
2010 static dwarf2_cu *
2011 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2012 bool skip_partial)
2013 {
2014 if (per_cu->is_debug_types)
2015 load_full_type_unit (per_cu, per_objfile);
2016 else
2017 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2018 skip_partial, language_minimal);
2019
2020 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2021 if (cu == nullptr)
2022 return nullptr; /* Dummy CU. */
2023
2024 dwarf2_find_base_address (cu->dies, cu);
2025
2026 return cu;
2027 }
2028
2029 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2030
2031 static void
2032 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2033 dwarf2_per_objfile *per_objfile, bool skip_partial)
2034 {
2035 {
2036 /* The destructor of dwarf2_queue_guard frees any entries left on
2037 the queue. After this point we're guaranteed to leave this function
2038 with the dwarf queue empty. */
2039 dwarf2_queue_guard q_guard (per_objfile);
2040
2041 if (!per_objfile->symtab_set_p (per_cu))
2042 {
2043 queue_comp_unit (per_cu, per_objfile, language_minimal);
2044 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2045
2046 /* If we just loaded a CU from a DWO, and we're working with an index
2047 that may badly handle TUs, load all the TUs in that DWO as well.
2048 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2049 if (!per_cu->is_debug_types
2050 && cu != NULL
2051 && cu->dwo_unit != NULL
2052 && per_objfile->per_bfd->index_table != NULL
2053 && !per_objfile->per_bfd->index_table->version_check ()
2054 /* DWP files aren't supported yet. */
2055 && get_dwp_file (per_objfile) == NULL)
2056 queue_and_load_all_dwo_tus (cu);
2057 }
2058
2059 process_queue (per_objfile);
2060 }
2061
2062 /* Age the cache, releasing compilation units that have not
2063 been used recently. */
2064 per_objfile->age_comp_units ();
2065 }
2066
2067 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2068 the per-objfile for which this symtab is instantiated.
2069
2070 Returns the resulting symbol table. */
2071
2072 static struct compunit_symtab *
2073 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2074 dwarf2_per_objfile *per_objfile,
2075 bool skip_partial)
2076 {
2077 if (!per_objfile->symtab_set_p (per_cu))
2078 {
2079 free_cached_comp_units freer (per_objfile);
2080 scoped_restore decrementer = increment_reading_symtab ();
2081 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2082 process_cu_includes (per_objfile);
2083 }
2084
2085 return per_objfile->get_symtab (per_cu);
2086 }
2087
2088 /* See read.h. */
2089
2090 dwarf2_per_cu_data_up
2091 dwarf2_per_bfd::allocate_per_cu ()
2092 {
2093 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2094 result->per_bfd = this;
2095 result->index = all_comp_units.size ();
2096 return result;
2097 }
2098
2099 /* See read.h. */
2100
2101 signatured_type_up
2102 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2103 {
2104 signatured_type_up result (new signatured_type (signature));
2105 result->per_bfd = this;
2106 result->index = all_comp_units.size ();
2107 result->is_debug_types = true;
2108 tu_stats.nr_tus++;
2109 return result;
2110 }
2111
2112 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2113 obstack, and constructed with the specified field values. */
2114
2115 static dwarf2_per_cu_data_up
2116 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2117 struct dwarf2_section_info *section,
2118 int is_dwz,
2119 sect_offset sect_off, ULONGEST length)
2120 {
2121 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2122 the_cu->sect_off = sect_off;
2123 the_cu->set_length (length);
2124 the_cu->section = section;
2125 the_cu->is_dwz = is_dwz;
2126 return the_cu;
2127 }
2128
2129 /* A helper for create_cus_from_index that handles a given list of
2130 CUs. */
2131
2132 static void
2133 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2134 const gdb_byte *cu_list, offset_type n_elements,
2135 struct dwarf2_section_info *section,
2136 int is_dwz)
2137 {
2138 for (offset_type i = 0; i < n_elements; i += 2)
2139 {
2140 gdb_static_assert (sizeof (ULONGEST) >= 8);
2141
2142 sect_offset sect_off
2143 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2144 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2145 cu_list += 2 * 8;
2146
2147 dwarf2_per_cu_data_up per_cu
2148 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2149 length);
2150 per_bfd->all_comp_units.push_back (std::move (per_cu));
2151 }
2152 }
2153
2154 /* Read the CU list from the mapped index, and use it to create all
2155 the CU objects for PER_BFD. */
2156
2157 static void
2158 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2159 const gdb_byte *cu_list, offset_type cu_list_elements,
2160 const gdb_byte *dwz_list, offset_type dwz_elements)
2161 {
2162 gdb_assert (per_bfd->all_comp_units.empty ());
2163 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2164
2165 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2166 &per_bfd->info, 0);
2167
2168 if (dwz_elements == 0)
2169 return;
2170
2171 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2172 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2173 &dwz->info, 1);
2174 }
2175
2176 /* Create the signatured type hash table from the index. */
2177
2178 static void
2179 create_signatured_type_table_from_index
2180 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2181 const gdb_byte *bytes, offset_type elements)
2182 {
2183 htab_up sig_types_hash = allocate_signatured_type_table ();
2184
2185 for (offset_type i = 0; i < elements; i += 3)
2186 {
2187 signatured_type_up sig_type;
2188 ULONGEST signature;
2189 void **slot;
2190 cu_offset type_offset_in_tu;
2191
2192 gdb_static_assert (sizeof (ULONGEST) >= 8);
2193 sect_offset sect_off
2194 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2195 type_offset_in_tu
2196 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2197 BFD_ENDIAN_LITTLE);
2198 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2199 bytes += 3 * 8;
2200
2201 sig_type = per_bfd->allocate_signatured_type (signature);
2202 sig_type->type_offset_in_tu = type_offset_in_tu;
2203 sig_type->section = section;
2204 sig_type->sect_off = sect_off;
2205
2206 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2207 *slot = sig_type.get ();
2208
2209 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2210 }
2211
2212 per_bfd->signatured_types = std::move (sig_types_hash);
2213 }
2214
2215 /* Create the signatured type hash table from .debug_names. */
2216
2217 static void
2218 create_signatured_type_table_from_debug_names
2219 (dwarf2_per_objfile *per_objfile,
2220 const mapped_debug_names &map,
2221 struct dwarf2_section_info *section,
2222 struct dwarf2_section_info *abbrev_section)
2223 {
2224 struct objfile *objfile = per_objfile->objfile;
2225
2226 section->read (objfile);
2227 abbrev_section->read (objfile);
2228
2229 htab_up sig_types_hash = allocate_signatured_type_table ();
2230
2231 for (uint32_t i = 0; i < map.tu_count; ++i)
2232 {
2233 signatured_type_up sig_type;
2234 void **slot;
2235
2236 sect_offset sect_off
2237 = (sect_offset) (extract_unsigned_integer
2238 (map.tu_table_reordered + i * map.offset_size,
2239 map.offset_size,
2240 map.dwarf5_byte_order));
2241
2242 comp_unit_head cu_header;
2243 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2244 abbrev_section,
2245 section->buffer + to_underlying (sect_off),
2246 rcuh_kind::TYPE);
2247
2248 sig_type = per_objfile->per_bfd->allocate_signatured_type
2249 (cu_header.signature);
2250 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2251 sig_type->section = section;
2252 sig_type->sect_off = sect_off;
2253
2254 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2255 *slot = sig_type.get ();
2256
2257 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2258 }
2259
2260 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2261 }
2262
2263 /* Read the address map data from the mapped index, and use it to
2264 populate the index_addrmap. */
2265
2266 static void
2267 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2268 struct mapped_index *index)
2269 {
2270 struct objfile *objfile = per_objfile->objfile;
2271 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2272 struct gdbarch *gdbarch = objfile->arch ();
2273 const gdb_byte *iter, *end;
2274 CORE_ADDR baseaddr;
2275
2276 addrmap_mutable mutable_map;
2277
2278 iter = index->address_table.data ();
2279 end = iter + index->address_table.size ();
2280
2281 baseaddr = objfile->text_section_offset ();
2282
2283 while (iter < end)
2284 {
2285 ULONGEST hi, lo, cu_index;
2286 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2287 iter += 8;
2288 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2289 iter += 8;
2290 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2291 iter += 4;
2292
2293 if (lo > hi)
2294 {
2295 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2296 hex_string (lo), hex_string (hi));
2297 continue;
2298 }
2299
2300 if (cu_index >= per_bfd->all_comp_units.size ())
2301 {
2302 complaint (_(".gdb_index address table has invalid CU number %u"),
2303 (unsigned) cu_index);
2304 continue;
2305 }
2306
2307 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2308 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2309 mutable_map.set_empty (lo, hi - 1, per_bfd->get_cu (cu_index));
2310 }
2311
2312 per_bfd->index_addrmap
2313 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack, &mutable_map);
2314 }
2315
2316 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2317 to populate given addrmap. Returns true on success, false on
2318 failure. */
2319
2320 static bool
2321 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2322 struct dwarf2_section_info *section,
2323 addrmap *mutable_map)
2324 {
2325 struct objfile *objfile = per_objfile->objfile;
2326 bfd *abfd = objfile->obfd;
2327 struct gdbarch *gdbarch = objfile->arch ();
2328 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2329 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2330
2331 std::unordered_map<sect_offset,
2332 dwarf2_per_cu_data *,
2333 gdb::hash_enum<sect_offset>>
2334 debug_info_offset_to_per_cu;
2335 for (const auto &per_cu : per_bfd->all_comp_units)
2336 {
2337 /* A TU will not need aranges, and skipping them here is an easy
2338 way of ignoring .debug_types -- and possibly seeing a
2339 duplicate section offset -- entirely. The same applies to
2340 units coming from a dwz file. */
2341 if (per_cu->is_debug_types || per_cu->is_dwz)
2342 continue;
2343
2344 const auto insertpair
2345 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2346 per_cu.get ());
2347 if (!insertpair.second)
2348 {
2349 warning (_("Section .debug_aranges in %s has duplicate "
2350 "debug_info_offset %s, ignoring .debug_aranges."),
2351 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2352 return false;
2353 }
2354 }
2355
2356 section->read (objfile);
2357
2358 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2359
2360 const gdb_byte *addr = section->buffer;
2361
2362 while (addr < section->buffer + section->size)
2363 {
2364 const gdb_byte *const entry_addr = addr;
2365 unsigned int bytes_read;
2366
2367 const LONGEST entry_length = read_initial_length (abfd, addr,
2368 &bytes_read);
2369 addr += bytes_read;
2370
2371 const gdb_byte *const entry_end = addr + entry_length;
2372 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2373 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2374 if (addr + entry_length > section->buffer + section->size)
2375 {
2376 warning (_("Section .debug_aranges in %s entry at offset %s "
2377 "length %s exceeds section length %s, "
2378 "ignoring .debug_aranges."),
2379 objfile_name (objfile),
2380 plongest (entry_addr - section->buffer),
2381 plongest (bytes_read + entry_length),
2382 pulongest (section->size));
2383 return false;
2384 }
2385
2386 /* The version number. */
2387 const uint16_t version = read_2_bytes (abfd, addr);
2388 addr += 2;
2389 if (version != 2)
2390 {
2391 warning (_("Section .debug_aranges in %s entry at offset %s "
2392 "has unsupported version %d, ignoring .debug_aranges."),
2393 objfile_name (objfile),
2394 plongest (entry_addr - section->buffer), version);
2395 return false;
2396 }
2397
2398 const uint64_t debug_info_offset
2399 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2400 addr += offset_size;
2401 const auto per_cu_it
2402 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2403 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2404 {
2405 warning (_("Section .debug_aranges in %s entry at offset %s "
2406 "debug_info_offset %s does not exists, "
2407 "ignoring .debug_aranges."),
2408 objfile_name (objfile),
2409 plongest (entry_addr - section->buffer),
2410 pulongest (debug_info_offset));
2411 return false;
2412 }
2413 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2414
2415 const uint8_t address_size = *addr++;
2416 if (address_size < 1 || address_size > 8)
2417 {
2418 warning (_("Section .debug_aranges in %s entry at offset %s "
2419 "address_size %u is invalid, ignoring .debug_aranges."),
2420 objfile_name (objfile),
2421 plongest (entry_addr - section->buffer), address_size);
2422 return false;
2423 }
2424
2425 const uint8_t segment_selector_size = *addr++;
2426 if (segment_selector_size != 0)
2427 {
2428 warning (_("Section .debug_aranges in %s entry at offset %s "
2429 "segment_selector_size %u is not supported, "
2430 "ignoring .debug_aranges."),
2431 objfile_name (objfile),
2432 plongest (entry_addr - section->buffer),
2433 segment_selector_size);
2434 return false;
2435 }
2436
2437 /* Must pad to an alignment boundary that is twice the address
2438 size. It is undocumented by the DWARF standard but GCC does
2439 use it. However, not every compiler does this. We can see
2440 whether it has happened by looking at the total length of the
2441 contents of the aranges for this CU -- it if isn't a multiple
2442 of twice the address size, then we skip any leftover
2443 bytes. */
2444 addr += (entry_end - addr) % (2 * address_size);
2445
2446 while (addr < entry_end)
2447 {
2448 if (addr + 2 * address_size > entry_end)
2449 {
2450 warning (_("Section .debug_aranges in %s entry at offset %s "
2451 "address list is not properly terminated, "
2452 "ignoring .debug_aranges."),
2453 objfile_name (objfile),
2454 plongest (entry_addr - section->buffer));
2455 return false;
2456 }
2457 ULONGEST start = extract_unsigned_integer (addr, address_size,
2458 dwarf5_byte_order);
2459 addr += address_size;
2460 ULONGEST length = extract_unsigned_integer (addr, address_size,
2461 dwarf5_byte_order);
2462 addr += address_size;
2463 if (start == 0 && length == 0)
2464 {
2465 /* This can happen on some targets with --gc-sections.
2466 This pair of values is also used to mark the end of
2467 the entries for a given CU, but we ignore it and
2468 instead handle termination using the check at the top
2469 of the loop. */
2470 continue;
2471 }
2472 if (start == 0 && !per_bfd->has_section_at_zero)
2473 {
2474 /* Symbol was eliminated due to a COMDAT group. */
2475 continue;
2476 }
2477 ULONGEST end = start + length;
2478 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2479 - baseaddr);
2480 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2481 - baseaddr);
2482 mutable_map->set_empty (start, end - 1, per_cu);
2483 }
2484
2485 per_cu->addresses_seen = true;
2486 }
2487
2488 return true;
2489 }
2490
2491 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2492 populate the index_addrmap. */
2493
2494 static void
2495 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2496 struct dwarf2_section_info *section)
2497 {
2498 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2499
2500 addrmap_mutable mutable_map;
2501
2502 if (read_addrmap_from_aranges (per_objfile, section, &mutable_map))
2503 per_bfd->index_addrmap
2504 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack,
2505 &mutable_map);
2506 }
2507
2508 /* A helper function that reads the .gdb_index from BUFFER and fills
2509 in MAP. FILENAME is the name of the file containing the data;
2510 it is used for error reporting. DEPRECATED_OK is true if it is
2511 ok to use deprecated sections.
2512
2513 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2514 out parameters that are filled in with information about the CU and
2515 TU lists in the section.
2516
2517 Returns true if all went well, false otherwise. */
2518
2519 static bool
2520 read_gdb_index_from_buffer (const char *filename,
2521 bool deprecated_ok,
2522 gdb::array_view<const gdb_byte> buffer,
2523 struct mapped_index *map,
2524 const gdb_byte **cu_list,
2525 offset_type *cu_list_elements,
2526 const gdb_byte **types_list,
2527 offset_type *types_list_elements)
2528 {
2529 const gdb_byte *addr = &buffer[0];
2530 offset_view metadata (buffer);
2531
2532 /* Version check. */
2533 offset_type version = metadata[0];
2534 /* Versions earlier than 3 emitted every copy of a psymbol. This
2535 causes the index to behave very poorly for certain requests. Version 3
2536 contained incomplete addrmap. So, it seems better to just ignore such
2537 indices. */
2538 if (version < 4)
2539 {
2540 static int warning_printed = 0;
2541 if (!warning_printed)
2542 {
2543 warning (_("Skipping obsolete .gdb_index section in %s."),
2544 filename);
2545 warning_printed = 1;
2546 }
2547 return 0;
2548 }
2549 /* Index version 4 uses a different hash function than index version
2550 5 and later.
2551
2552 Versions earlier than 6 did not emit psymbols for inlined
2553 functions. Using these files will cause GDB not to be able to
2554 set breakpoints on inlined functions by name, so we ignore these
2555 indices unless the user has done
2556 "set use-deprecated-index-sections on". */
2557 if (version < 6 && !deprecated_ok)
2558 {
2559 static int warning_printed = 0;
2560 if (!warning_printed)
2561 {
2562 warning (_("\
2563 Skipping deprecated .gdb_index section in %s.\n\
2564 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2565 to use the section anyway."),
2566 filename);
2567 warning_printed = 1;
2568 }
2569 return 0;
2570 }
2571 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2572 of the TU (for symbols coming from TUs),
2573 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2574 Plus gold-generated indices can have duplicate entries for global symbols,
2575 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2576 These are just performance bugs, and we can't distinguish gdb-generated
2577 indices from gold-generated ones, so issue no warning here. */
2578
2579 /* Indexes with higher version than the one supported by GDB may be no
2580 longer backward compatible. */
2581 if (version > 8)
2582 return 0;
2583
2584 map->version = version;
2585
2586 int i = 1;
2587 *cu_list = addr + metadata[i];
2588 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2589 ++i;
2590
2591 *types_list = addr + metadata[i];
2592 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2593 ++i;
2594
2595 const gdb_byte *address_table = addr + metadata[i];
2596 const gdb_byte *address_table_end = addr + metadata[i + 1];
2597 map->address_table
2598 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2599 ++i;
2600
2601 const gdb_byte *symbol_table = addr + metadata[i];
2602 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2603 map->symbol_table
2604 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2605 symbol_table_end));
2606
2607 ++i;
2608 map->constant_pool = buffer.slice (metadata[i]);
2609
2610 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2611 {
2612 /* An empty constant pool implies that all symbol table entries are
2613 empty. Make map->symbol_table.empty () == true. */
2614 map->symbol_table
2615 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2616 symbol_table));
2617 }
2618
2619 return 1;
2620 }
2621
2622 /* Callback types for dwarf2_read_gdb_index. */
2623
2624 typedef gdb::function_view
2625 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2626 get_gdb_index_contents_ftype;
2627 typedef gdb::function_view
2628 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2629 get_gdb_index_contents_dwz_ftype;
2630
2631 /* Read .gdb_index. If everything went ok, initialize the "quick"
2632 elements of all the CUs and return 1. Otherwise, return 0. */
2633
2634 static int
2635 dwarf2_read_gdb_index
2636 (dwarf2_per_objfile *per_objfile,
2637 get_gdb_index_contents_ftype get_gdb_index_contents,
2638 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2639 {
2640 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2641 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2642 struct dwz_file *dwz;
2643 struct objfile *objfile = per_objfile->objfile;
2644 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2645
2646 gdb::array_view<const gdb_byte> main_index_contents
2647 = get_gdb_index_contents (objfile, per_bfd);
2648
2649 if (main_index_contents.empty ())
2650 return 0;
2651
2652 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2653 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2654 use_deprecated_index_sections,
2655 main_index_contents, map.get (), &cu_list,
2656 &cu_list_elements, &types_list,
2657 &types_list_elements))
2658 return 0;
2659
2660 /* Don't use the index if it's empty. */
2661 if (map->symbol_table.empty ())
2662 return 0;
2663
2664 /* If there is a .dwz file, read it so we can get its CU list as
2665 well. */
2666 dwz = dwarf2_get_dwz_file (per_bfd);
2667 if (dwz != NULL)
2668 {
2669 struct mapped_index dwz_map;
2670 const gdb_byte *dwz_types_ignore;
2671 offset_type dwz_types_elements_ignore;
2672
2673 gdb::array_view<const gdb_byte> dwz_index_content
2674 = get_gdb_index_contents_dwz (objfile, dwz);
2675
2676 if (dwz_index_content.empty ())
2677 return 0;
2678
2679 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2680 1, dwz_index_content, &dwz_map,
2681 &dwz_list, &dwz_list_elements,
2682 &dwz_types_ignore,
2683 &dwz_types_elements_ignore))
2684 {
2685 warning (_("could not read '.gdb_index' section from %s; skipping"),
2686 bfd_get_filename (dwz->dwz_bfd.get ()));
2687 return 0;
2688 }
2689 }
2690
2691 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2692 dwz_list_elements);
2693
2694 if (types_list_elements)
2695 {
2696 /* We can only handle a single .debug_types when we have an
2697 index. */
2698 if (per_bfd->types.size () != 1)
2699 {
2700 per_bfd->all_comp_units.clear ();
2701 return 0;
2702 }
2703
2704 dwarf2_section_info *section = &per_bfd->types[0];
2705
2706 create_signatured_type_table_from_index (per_bfd, section, types_list,
2707 types_list_elements);
2708 }
2709
2710 create_addrmap_from_index (per_objfile, map.get ());
2711
2712 per_bfd->index_table = std::move (map);
2713 per_bfd->quick_file_names_table =
2714 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2715
2716 return 1;
2717 }
2718
2719 /* die_reader_func for dw2_get_file_names. */
2720
2721 static void
2722 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2723 struct die_info *comp_unit_die)
2724 {
2725 struct dwarf2_cu *cu = reader->cu;
2726 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2727 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2728 struct dwarf2_per_cu_data *lh_cu;
2729 struct attribute *attr;
2730 void **slot;
2731 struct quick_file_names *qfn;
2732
2733 gdb_assert (! this_cu->is_debug_types);
2734
2735 this_cu->files_read = true;
2736 /* Our callers never want to match partial units -- instead they
2737 will match the enclosing full CU. */
2738 if (comp_unit_die->tag == DW_TAG_partial_unit)
2739 return;
2740
2741 lh_cu = this_cu;
2742 slot = NULL;
2743
2744 line_header_up lh;
2745 sect_offset line_offset {};
2746
2747 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2748 if (attr != nullptr && attr->form_is_unsigned ())
2749 {
2750 struct quick_file_names find_entry;
2751
2752 line_offset = (sect_offset) attr->as_unsigned ();
2753
2754 /* We may have already read in this line header (TU line header sharing).
2755 If we have we're done. */
2756 find_entry.hash.dwo_unit = cu->dwo_unit;
2757 find_entry.hash.line_sect_off = line_offset;
2758 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2759 &find_entry, INSERT);
2760 if (*slot != NULL)
2761 {
2762 lh_cu->file_names = (struct quick_file_names *) *slot;
2763 return;
2764 }
2765
2766 lh = dwarf_decode_line_header (line_offset, cu);
2767 }
2768
2769 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
2770
2771 int offset = 0;
2772 if (!fnd.is_unknown ())
2773 ++offset;
2774 else if (lh == nullptr)
2775 return;
2776
2777 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2778 qfn->hash.dwo_unit = cu->dwo_unit;
2779 qfn->hash.line_sect_off = line_offset;
2780 /* There may not be a DW_AT_stmt_list. */
2781 if (slot != nullptr)
2782 *slot = qfn;
2783
2784 std::vector<const char *> include_names;
2785 if (lh != nullptr)
2786 {
2787 for (const auto &entry : lh->file_names ())
2788 {
2789 std::string name_holder;
2790 const char *include_name =
2791 compute_include_file_name (lh.get (), entry, fnd, name_holder);
2792 if (include_name != nullptr)
2793 {
2794 include_name = per_objfile->objfile->intern (include_name);
2795 include_names.push_back (include_name);
2796 }
2797 }
2798 }
2799
2800 qfn->num_file_names = offset + include_names.size ();
2801 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
2802 qfn->file_names =
2803 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2804 qfn->num_file_names);
2805 if (offset != 0)
2806 qfn->file_names[0] = xstrdup (fnd.get_name ());
2807
2808 if (!include_names.empty ())
2809 memcpy (&qfn->file_names[offset], include_names.data (),
2810 include_names.size () * sizeof (const char *));
2811
2812 qfn->real_names = NULL;
2813
2814 lh_cu->file_names = qfn;
2815 }
2816
2817 /* A helper for the "quick" functions which attempts to read the line
2818 table for THIS_CU. */
2819
2820 static struct quick_file_names *
2821 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2822 dwarf2_per_objfile *per_objfile)
2823 {
2824 /* This should never be called for TUs. */
2825 gdb_assert (! this_cu->is_debug_types);
2826
2827 if (this_cu->files_read)
2828 return this_cu->file_names;
2829
2830 cutu_reader reader (this_cu, per_objfile);
2831 if (!reader.dummy_p)
2832 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2833
2834 return this_cu->file_names;
2835 }
2836
2837 /* A helper for the "quick" functions which computes and caches the
2838 real path for a given file name from the line table. */
2839
2840 static const char *
2841 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2842 struct quick_file_names *qfn, int index)
2843 {
2844 if (qfn->real_names == NULL)
2845 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
2846 qfn->num_file_names, const char *);
2847
2848 if (qfn->real_names[index] == NULL)
2849 {
2850 const char *dirname = nullptr;
2851
2852 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
2853 dirname = qfn->comp_dir;
2854
2855 gdb::unique_xmalloc_ptr<char> fullname;
2856 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
2857
2858 qfn->real_names[index] = fullname.release ();
2859 }
2860
2861 return qfn->real_names[index];
2862 }
2863
2864 struct symtab *
2865 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
2866 {
2867 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2868 dwarf2_per_cu_data *dwarf_cu
2869 = per_objfile->per_bfd->all_comp_units.back ().get ();
2870 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2871
2872 if (cust == NULL)
2873 return NULL;
2874
2875 return cust->primary_filetab ();
2876 }
2877
2878 /* See read.h. */
2879
2880 void
2881 dwarf2_per_cu_data::free_cached_file_names ()
2882 {
2883 if (fnd != nullptr)
2884 fnd->forget_fullname ();
2885
2886 if (per_bfd == nullptr)
2887 return;
2888
2889 struct quick_file_names *file_data = file_names;
2890 if (file_data != nullptr && file_data->real_names != nullptr)
2891 {
2892 for (int i = 0; i < file_data->num_file_names; ++i)
2893 {
2894 xfree ((void *) file_data->real_names[i]);
2895 file_data->real_names[i] = nullptr;
2896 }
2897 }
2898 }
2899
2900 void
2901 dwarf2_base_index_functions::forget_cached_source_info
2902 (struct objfile *objfile)
2903 {
2904 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2905
2906 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
2907 per_cu->free_cached_file_names ();
2908 }
2909
2910 /* Struct used to manage iterating over all CUs looking for a symbol. */
2911
2912 struct dw2_symtab_iterator
2913 {
2914 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2915 dwarf2_per_objfile *per_objfile;
2916 /* If set, only look for symbols that match that block. Valid values are
2917 GLOBAL_BLOCK and STATIC_BLOCK. */
2918 gdb::optional<block_enum> block_index;
2919 /* The kind of symbol we're looking for. */
2920 domain_enum domain;
2921 /* The list of CUs from the index entry of the symbol,
2922 or NULL if not found. */
2923 offset_view vec;
2924 /* The next element in VEC to look at. */
2925 int next;
2926 /* The number of elements in VEC, or zero if there is no match. */
2927 int length;
2928 /* Have we seen a global version of the symbol?
2929 If so we can ignore all further global instances.
2930 This is to work around gold/15646, inefficient gold-generated
2931 indices. */
2932 int global_seen;
2933 };
2934
2935 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2936
2937 static void
2938 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
2939 dwarf2_per_objfile *per_objfile,
2940 gdb::optional<block_enum> block_index,
2941 domain_enum domain, offset_type namei,
2942 mapped_index &index)
2943 {
2944 iter->per_objfile = per_objfile;
2945 iter->block_index = block_index;
2946 iter->domain = domain;
2947 iter->next = 0;
2948 iter->global_seen = 0;
2949 iter->vec = {};
2950 iter->length = 0;
2951
2952 gdb_assert (!index.symbol_name_slot_invalid (namei));
2953 offset_type vec_idx = index.symbol_vec_index (namei);
2954
2955 iter->vec = offset_view (index.constant_pool.slice (vec_idx));
2956 iter->length = iter->vec[0];
2957 }
2958
2959 /* Return the next matching CU or NULL if there are no more. */
2960
2961 static struct dwarf2_per_cu_data *
2962 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter,
2963 mapped_index &index)
2964 {
2965 dwarf2_per_objfile *per_objfile = iter->per_objfile;
2966
2967 for ( ; iter->next < iter->length; ++iter->next)
2968 {
2969 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
2970 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2971 gdb_index_symbol_kind symbol_kind =
2972 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2973 /* Only check the symbol attributes if they're present.
2974 Indices prior to version 7 don't record them,
2975 and indices >= 7 may elide them for certain symbols
2976 (gold does this). */
2977 int attrs_valid = (index.version >= 7
2978 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
2979
2980 /* Don't crash on bad data. */
2981 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2982 {
2983 complaint (_(".gdb_index entry has bad CU index"
2984 " [in module %s]"), objfile_name (per_objfile->objfile));
2985 continue;
2986 }
2987
2988 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
2989
2990 /* Skip if already read in. */
2991 if (per_objfile->symtab_set_p (per_cu))
2992 continue;
2993
2994 /* Check static vs global. */
2995 if (attrs_valid)
2996 {
2997 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2998
2999 if (iter->block_index.has_value ())
3000 {
3001 bool want_static = *iter->block_index == STATIC_BLOCK;
3002
3003 if (is_static != want_static)
3004 continue;
3005 }
3006
3007 /* Work around gold/15646. */
3008 if (!is_static
3009 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3010 {
3011 if (iter->global_seen)
3012 continue;
3013
3014 iter->global_seen = 1;
3015 }
3016 }
3017
3018 /* Only check the symbol's kind if it has one. */
3019 if (attrs_valid)
3020 {
3021 switch (iter->domain)
3022 {
3023 case VAR_DOMAIN:
3024 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3025 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3026 /* Some types are also in VAR_DOMAIN. */
3027 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3028 continue;
3029 break;
3030 case STRUCT_DOMAIN:
3031 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3032 continue;
3033 break;
3034 case LABEL_DOMAIN:
3035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3036 continue;
3037 break;
3038 case MODULE_DOMAIN:
3039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3040 continue;
3041 break;
3042 default:
3043 break;
3044 }
3045 }
3046
3047 ++iter->next;
3048 return per_cu;
3049 }
3050
3051 return NULL;
3052 }
3053
3054 void
3055 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3056 bool print_bcache)
3057 {
3058 if (print_bcache)
3059 return;
3060
3061 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3062 int total = per_objfile->per_bfd->all_comp_units.size ();
3063 int count = 0;
3064
3065 for (int i = 0; i < total; ++i)
3066 {
3067 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3068
3069 if (!per_objfile->symtab_set_p (per_cu))
3070 ++count;
3071 }
3072 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3073 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3074 }
3075
3076 /* This dumps minimal information about the index.
3077 It is called via "mt print objfiles".
3078 One use is to verify .gdb_index has been loaded by the
3079 gdb.dwarf2/gdb-index.exp testcase. */
3080
3081 void
3082 dwarf2_gdb_index::dump (struct objfile *objfile)
3083 {
3084 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3085
3086 mapped_index *index = (static_cast<mapped_index *>
3087 (per_objfile->per_bfd->index_table.get ()));
3088 gdb_printf (".gdb_index: version %d\n", index->version);
3089 gdb_printf ("\n");
3090 }
3091
3092 void
3093 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3094 {
3095 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3096 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3097
3098 for (int i = 0; i < total_units; ++i)
3099 {
3100 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3101
3102 /* We don't want to directly expand a partial CU, because if we
3103 read it with the wrong language, then assertion failures can
3104 be triggered later on. See PR symtab/23010. So, tell
3105 dw2_instantiate_symtab to skip partial CUs -- any important
3106 partial CU will be read via DW_TAG_imported_unit anyway. */
3107 dw2_instantiate_symtab (per_cu, per_objfile, true);
3108 }
3109 }
3110
3111 static bool
3112 dw2_expand_symtabs_matching_symbol
3113 (mapped_index_base &index,
3114 const lookup_name_info &lookup_name_in,
3115 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3116 gdb::function_view<bool (offset_type)> match_callback,
3117 dwarf2_per_objfile *per_objfile);
3118
3119 static bool
3120 dw2_expand_symtabs_matching_one
3121 (dwarf2_per_cu_data *per_cu,
3122 dwarf2_per_objfile *per_objfile,
3123 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3124 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3125
3126 void
3127 dwarf2_gdb_index::expand_matching_symbols
3128 (struct objfile *objfile,
3129 const lookup_name_info &name, domain_enum domain,
3130 int global,
3131 symbol_compare_ftype *ordered_compare)
3132 {
3133 /* Used for Ada. */
3134 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3135
3136 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3137
3138 mapped_index &index
3139 = (static_cast<mapped_index &>
3140 (*per_objfile->per_bfd->index_table.get ()));
3141
3142 const char *match_name = name.ada ().lookup_name ().c_str ();
3143 auto matcher = [&] (const char *symname)
3144 {
3145 if (ordered_compare == nullptr)
3146 return true;
3147 return ordered_compare (symname, match_name) == 0;
3148 };
3149
3150 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3151 [&] (offset_type namei)
3152 {
3153 struct dw2_symtab_iterator iter;
3154 struct dwarf2_per_cu_data *per_cu;
3155
3156 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain, namei,
3157 index);
3158 while ((per_cu = dw2_symtab_iter_next (&iter, index)) != NULL)
3159 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3160 nullptr);
3161 return true;
3162 }, per_objfile);
3163 }
3164
3165 /* Starting from a search name, return the string that finds the upper
3166 bound of all strings that start with SEARCH_NAME in a sorted name
3167 list. Returns the empty string to indicate that the upper bound is
3168 the end of the list. */
3169
3170 static std::string
3171 make_sort_after_prefix_name (const char *search_name)
3172 {
3173 /* When looking to complete "func", we find the upper bound of all
3174 symbols that start with "func" by looking for where we'd insert
3175 the closest string that would follow "func" in lexicographical
3176 order. Usually, that's "func"-with-last-character-incremented,
3177 i.e. "fund". Mind non-ASCII characters, though. Usually those
3178 will be UTF-8 multi-byte sequences, but we can't be certain.
3179 Especially mind the 0xff character, which is a valid character in
3180 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3181 rule out compilers allowing it in identifiers. Note that
3182 conveniently, strcmp/strcasecmp are specified to compare
3183 characters interpreted as unsigned char. So what we do is treat
3184 the whole string as a base 256 number composed of a sequence of
3185 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3186 to 0, and carries 1 to the following more-significant position.
3187 If the very first character in SEARCH_NAME ends up incremented
3188 and carries/overflows, then the upper bound is the end of the
3189 list. The string after the empty string is also the empty
3190 string.
3191
3192 Some examples of this operation:
3193
3194 SEARCH_NAME => "+1" RESULT
3195
3196 "abc" => "abd"
3197 "ab\xff" => "ac"
3198 "\xff" "a" "\xff" => "\xff" "b"
3199 "\xff" => ""
3200 "\xff\xff" => ""
3201 "" => ""
3202
3203 Then, with these symbols for example:
3204
3205 func
3206 func1
3207 fund
3208
3209 completing "func" looks for symbols between "func" and
3210 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3211 which finds "func" and "func1", but not "fund".
3212
3213 And with:
3214
3215 funcÿ (Latin1 'ÿ' [0xff])
3216 funcÿ1
3217 fund
3218
3219 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3220 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3221
3222 And with:
3223
3224 ÿÿ (Latin1 'ÿ' [0xff])
3225 ÿÿ1
3226
3227 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3228 the end of the list.
3229 */
3230 std::string after = search_name;
3231 while (!after.empty () && (unsigned char) after.back () == 0xff)
3232 after.pop_back ();
3233 if (!after.empty ())
3234 after.back () = (unsigned char) after.back () + 1;
3235 return after;
3236 }
3237
3238 /* See declaration. */
3239
3240 std::pair<std::vector<name_component>::const_iterator,
3241 std::vector<name_component>::const_iterator>
3242 mapped_index_base::find_name_components_bounds
3243 (const lookup_name_info &lookup_name_without_params, language lang,
3244 dwarf2_per_objfile *per_objfile) const
3245 {
3246 auto *name_cmp
3247 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3248
3249 const char *lang_name
3250 = lookup_name_without_params.language_lookup_name (lang);
3251
3252 /* Comparison function object for lower_bound that matches against a
3253 given symbol name. */
3254 auto lookup_compare_lower = [&] (const name_component &elem,
3255 const char *name)
3256 {
3257 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3258 const char *elem_name = elem_qualified + elem.name_offset;
3259 return name_cmp (elem_name, name) < 0;
3260 };
3261
3262 /* Comparison function object for upper_bound that matches against a
3263 given symbol name. */
3264 auto lookup_compare_upper = [&] (const char *name,
3265 const name_component &elem)
3266 {
3267 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3268 const char *elem_name = elem_qualified + elem.name_offset;
3269 return name_cmp (name, elem_name) < 0;
3270 };
3271
3272 auto begin = this->name_components.begin ();
3273 auto end = this->name_components.end ();
3274
3275 /* Find the lower bound. */
3276 auto lower = [&] ()
3277 {
3278 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3279 return begin;
3280 else
3281 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3282 } ();
3283
3284 /* Find the upper bound. */
3285 auto upper = [&] ()
3286 {
3287 if (lookup_name_without_params.completion_mode ())
3288 {
3289 /* In completion mode, we want UPPER to point past all
3290 symbols names that have the same prefix. I.e., with
3291 these symbols, and completing "func":
3292
3293 function << lower bound
3294 function1
3295 other_function << upper bound
3296
3297 We find the upper bound by looking for the insertion
3298 point of "func"-with-last-character-incremented,
3299 i.e. "fund". */
3300 std::string after = make_sort_after_prefix_name (lang_name);
3301 if (after.empty ())
3302 return end;
3303 return std::lower_bound (lower, end, after.c_str (),
3304 lookup_compare_lower);
3305 }
3306 else
3307 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3308 } ();
3309
3310 return {lower, upper};
3311 }
3312
3313 /* See declaration. */
3314
3315 void
3316 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3317 {
3318 if (!this->name_components.empty ())
3319 return;
3320
3321 this->name_components_casing = case_sensitivity;
3322 auto *name_cmp
3323 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3324
3325 /* The code below only knows how to break apart components of C++
3326 symbol names (and other languages that use '::' as
3327 namespace/module separator) and Ada symbol names. */
3328 auto count = this->symbol_name_count ();
3329 for (offset_type idx = 0; idx < count; idx++)
3330 {
3331 if (this->symbol_name_slot_invalid (idx))
3332 continue;
3333
3334 const char *name = this->symbol_name_at (idx, per_objfile);
3335
3336 /* Add each name component to the name component table. */
3337 unsigned int previous_len = 0;
3338
3339 if (strstr (name, "::") != nullptr)
3340 {
3341 for (unsigned int current_len = cp_find_first_component (name);
3342 name[current_len] != '\0';
3343 current_len += cp_find_first_component (name + current_len))
3344 {
3345 gdb_assert (name[current_len] == ':');
3346 this->name_components.push_back ({previous_len, idx});
3347 /* Skip the '::'. */
3348 current_len += 2;
3349 previous_len = current_len;
3350 }
3351 }
3352 else
3353 {
3354 /* Handle the Ada encoded (aka mangled) form here. */
3355 for (const char *iter = strstr (name, "__");
3356 iter != nullptr;
3357 iter = strstr (iter, "__"))
3358 {
3359 this->name_components.push_back ({previous_len, idx});
3360 iter += 2;
3361 previous_len = iter - name;
3362 }
3363 }
3364
3365 this->name_components.push_back ({previous_len, idx});
3366 }
3367
3368 /* Sort name_components elements by name. */
3369 auto name_comp_compare = [&] (const name_component &left,
3370 const name_component &right)
3371 {
3372 const char *left_qualified
3373 = this->symbol_name_at (left.idx, per_objfile);
3374 const char *right_qualified
3375 = this->symbol_name_at (right.idx, per_objfile);
3376
3377 const char *left_name = left_qualified + left.name_offset;
3378 const char *right_name = right_qualified + right.name_offset;
3379
3380 return name_cmp (left_name, right_name) < 0;
3381 };
3382
3383 std::sort (this->name_components.begin (),
3384 this->name_components.end (),
3385 name_comp_compare);
3386 }
3387
3388 /* Helper for dw2_expand_symtabs_matching that works with a
3389 mapped_index_base instead of the containing objfile. This is split
3390 to a separate function in order to be able to unit test the
3391 name_components matching using a mock mapped_index_base. For each
3392 symbol name that matches, calls MATCH_CALLBACK, passing it the
3393 symbol's index in the mapped_index_base symbol table. */
3394
3395 static bool
3396 dw2_expand_symtabs_matching_symbol
3397 (mapped_index_base &index,
3398 const lookup_name_info &lookup_name_in,
3399 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3400 gdb::function_view<bool (offset_type)> match_callback,
3401 dwarf2_per_objfile *per_objfile)
3402 {
3403 lookup_name_info lookup_name_without_params
3404 = lookup_name_in.make_ignore_params ();
3405
3406 /* Build the symbol name component sorted vector, if we haven't
3407 yet. */
3408 index.build_name_components (per_objfile);
3409
3410 /* The same symbol may appear more than once in the range though.
3411 E.g., if we're looking for symbols that complete "w", and we have
3412 a symbol named "w1::w2", we'll find the two name components for
3413 that same symbol in the range. To be sure we only call the
3414 callback once per symbol, we first collect the symbol name
3415 indexes that matched in a temporary vector and ignore
3416 duplicates. */
3417 std::vector<offset_type> matches;
3418
3419 struct name_and_matcher
3420 {
3421 symbol_name_matcher_ftype *matcher;
3422 const char *name;
3423
3424 bool operator== (const name_and_matcher &other) const
3425 {
3426 return matcher == other.matcher && strcmp (name, other.name) == 0;
3427 }
3428 };
3429
3430 /* A vector holding all the different symbol name matchers, for all
3431 languages. */
3432 std::vector<name_and_matcher> matchers;
3433
3434 for (int i = 0; i < nr_languages; i++)
3435 {
3436 enum language lang_e = (enum language) i;
3437
3438 const language_defn *lang = language_def (lang_e);
3439 symbol_name_matcher_ftype *name_matcher
3440 = lang->get_symbol_name_matcher (lookup_name_without_params);
3441
3442 name_and_matcher key {
3443 name_matcher,
3444 lookup_name_without_params.language_lookup_name (lang_e)
3445 };
3446
3447 /* Don't insert the same comparison routine more than once.
3448 Note that we do this linear walk. This is not a problem in
3449 practice because the number of supported languages is
3450 low. */
3451 if (std::find (matchers.begin (), matchers.end (), key)
3452 != matchers.end ())
3453 continue;
3454 matchers.push_back (std::move (key));
3455
3456 auto bounds
3457 = index.find_name_components_bounds (lookup_name_without_params,
3458 lang_e, per_objfile);
3459
3460 /* Now for each symbol name in range, check to see if we have a name
3461 match, and if so, call the MATCH_CALLBACK callback. */
3462
3463 for (; bounds.first != bounds.second; ++bounds.first)
3464 {
3465 const char *qualified
3466 = index.symbol_name_at (bounds.first->idx, per_objfile);
3467
3468 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3469 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3470 continue;
3471
3472 matches.push_back (bounds.first->idx);
3473 }
3474 }
3475
3476 std::sort (matches.begin (), matches.end ());
3477
3478 /* Finally call the callback, once per match. */
3479 ULONGEST prev = -1;
3480 bool result = true;
3481 for (offset_type idx : matches)
3482 {
3483 if (prev != idx)
3484 {
3485 if (!match_callback (idx))
3486 {
3487 result = false;
3488 break;
3489 }
3490 prev = idx;
3491 }
3492 }
3493
3494 /* Above we use a type wider than idx's for 'prev', since 0 and
3495 (offset_type)-1 are both possible values. */
3496 static_assert (sizeof (prev) > sizeof (offset_type), "");
3497
3498 return result;
3499 }
3500
3501 #if GDB_SELF_TEST
3502
3503 namespace selftests { namespace dw2_expand_symtabs_matching {
3504
3505 /* A mock .gdb_index/.debug_names-like name index table, enough to
3506 exercise dw2_expand_symtabs_matching_symbol, which works with the
3507 mapped_index_base interface. Builds an index from the symbol list
3508 passed as parameter to the constructor. */
3509 class mock_mapped_index : public mapped_index_base
3510 {
3511 public:
3512 mock_mapped_index (gdb::array_view<const char *> symbols)
3513 : m_symbol_table (symbols)
3514 {}
3515
3516 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3517
3518 /* Return the number of names in the symbol table. */
3519 size_t symbol_name_count () const override
3520 {
3521 return m_symbol_table.size ();
3522 }
3523
3524 /* Get the name of the symbol at IDX in the symbol table. */
3525 const char *symbol_name_at
3526 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3527 {
3528 return m_symbol_table[idx];
3529 }
3530
3531 quick_symbol_functions_up make_quick_functions () const override
3532 {
3533 return nullptr;
3534 }
3535
3536 private:
3537 gdb::array_view<const char *> m_symbol_table;
3538 };
3539
3540 /* Convenience function that converts a NULL pointer to a "<null>"
3541 string, to pass to print routines. */
3542
3543 static const char *
3544 string_or_null (const char *str)
3545 {
3546 return str != NULL ? str : "<null>";
3547 }
3548
3549 /* Check if a lookup_name_info built from
3550 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3551 index. EXPECTED_LIST is the list of expected matches, in expected
3552 matching order. If no match expected, then an empty list is
3553 specified. Returns true on success. On failure prints a warning
3554 indicating the file:line that failed, and returns false. */
3555
3556 static bool
3557 check_match (const char *file, int line,
3558 mock_mapped_index &mock_index,
3559 const char *name, symbol_name_match_type match_type,
3560 bool completion_mode,
3561 std::initializer_list<const char *> expected_list,
3562 dwarf2_per_objfile *per_objfile)
3563 {
3564 lookup_name_info lookup_name (name, match_type, completion_mode);
3565
3566 bool matched = true;
3567
3568 auto mismatch = [&] (const char *expected_str,
3569 const char *got)
3570 {
3571 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3572 "expected=\"%s\", got=\"%s\"\n"),
3573 file, line,
3574 (match_type == symbol_name_match_type::FULL
3575 ? "FULL" : "WILD"),
3576 name, string_or_null (expected_str), string_or_null (got));
3577 matched = false;
3578 };
3579
3580 auto expected_it = expected_list.begin ();
3581 auto expected_end = expected_list.end ();
3582
3583 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3584 nullptr,
3585 [&] (offset_type idx)
3586 {
3587 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3588 const char *expected_str
3589 = expected_it == expected_end ? NULL : *expected_it++;
3590
3591 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3592 mismatch (expected_str, matched_name);
3593 return true;
3594 }, per_objfile);
3595
3596 const char *expected_str
3597 = expected_it == expected_end ? NULL : *expected_it++;
3598 if (expected_str != NULL)
3599 mismatch (expected_str, NULL);
3600
3601 return matched;
3602 }
3603
3604 /* The symbols added to the mock mapped_index for testing (in
3605 canonical form). */
3606 static const char *test_symbols[] = {
3607 "function",
3608 "std::bar",
3609 "std::zfunction",
3610 "std::zfunction2",
3611 "w1::w2",
3612 "ns::foo<char*>",
3613 "ns::foo<int>",
3614 "ns::foo<long>",
3615 "ns2::tmpl<int>::foo2",
3616 "(anonymous namespace)::A::B::C",
3617
3618 /* These are used to check that the increment-last-char in the
3619 matching algorithm for completion doesn't match "t1_fund" when
3620 completing "t1_func". */
3621 "t1_func",
3622 "t1_func1",
3623 "t1_fund",
3624 "t1_fund1",
3625
3626 /* A UTF-8 name with multi-byte sequences to make sure that
3627 cp-name-parser understands this as a single identifier ("função"
3628 is "function" in PT). */
3629 u8"u8função",
3630
3631 /* Test a symbol name that ends with a 0xff character, which is a
3632 valid character in non-UTF-8 source character sets (e.g. Latin1
3633 'ÿ'), and we can't rule out compilers allowing it in identifiers.
3634 We test this because the completion algorithm finds the upper
3635 bound of symbols by looking for the insertion point of
3636 "func"-with-last-character-incremented, i.e. "fund", and adding 1
3637 to 0xff should wraparound and carry to the previous character.
3638 See comments in make_sort_after_prefix_name. */
3639 "yfunc\377",
3640
3641 /* Some more symbols with \377 (0xff). See above. */
3642 "\377",
3643 "\377\377123",
3644
3645 /* A name with all sorts of complications. Starts with "z" to make
3646 it easier for the completion tests below. */
3647 #define Z_SYM_NAME \
3648 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3649 "::tuple<(anonymous namespace)::ui*, " \
3650 "std::default_delete<(anonymous namespace)::ui>, void>"
3651
3652 Z_SYM_NAME
3653 };
3654
3655 /* Returns true if the mapped_index_base::find_name_component_bounds
3656 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3657 in completion mode. */
3658
3659 static bool
3660 check_find_bounds_finds (mapped_index_base &index,
3661 const char *search_name,
3662 gdb::array_view<const char *> expected_syms,
3663 dwarf2_per_objfile *per_objfile)
3664 {
3665 lookup_name_info lookup_name (search_name,
3666 symbol_name_match_type::FULL, true);
3667
3668 auto bounds = index.find_name_components_bounds (lookup_name,
3669 language_cplus,
3670 per_objfile);
3671
3672 size_t distance = std::distance (bounds.first, bounds.second);
3673 if (distance != expected_syms.size ())
3674 return false;
3675
3676 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3677 {
3678 auto nc_elem = bounds.first + exp_elem;
3679 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3680 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3681 return false;
3682 }
3683
3684 return true;
3685 }
3686
3687 /* Test the lower-level mapped_index::find_name_component_bounds
3688 method. */
3689
3690 static void
3691 test_mapped_index_find_name_component_bounds ()
3692 {
3693 mock_mapped_index mock_index (test_symbols);
3694
3695 mock_index.build_name_components (NULL /* per_objfile */);
3696
3697 /* Test the lower-level mapped_index::find_name_component_bounds
3698 method in completion mode. */
3699 {
3700 static const char *expected_syms[] = {
3701 "t1_func",
3702 "t1_func1",
3703 };
3704
3705 SELF_CHECK (check_find_bounds_finds
3706 (mock_index, "t1_func", expected_syms,
3707 NULL /* per_objfile */));
3708 }
3709
3710 /* Check that the increment-last-char in the name matching algorithm
3711 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
3712 make_sort_after_prefix_name. */
3713 {
3714 static const char *expected_syms1[] = {
3715 "\377",
3716 "\377\377123",
3717 };
3718 SELF_CHECK (check_find_bounds_finds
3719 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3720
3721 static const char *expected_syms2[] = {
3722 "\377\377123",
3723 };
3724 SELF_CHECK (check_find_bounds_finds
3725 (mock_index, "\377\377", expected_syms2,
3726 NULL /* per_objfile */));
3727 }
3728 }
3729
3730 /* Test dw2_expand_symtabs_matching_symbol. */
3731
3732 static void
3733 test_dw2_expand_symtabs_matching_symbol ()
3734 {
3735 mock_mapped_index mock_index (test_symbols);
3736
3737 /* We let all tests run until the end even if some fails, for debug
3738 convenience. */
3739 bool any_mismatch = false;
3740
3741 /* Create the expected symbols list (an initializer_list). Needed
3742 because lists have commas, and we need to pass them to CHECK,
3743 which is a macro. */
3744 #define EXPECT(...) { __VA_ARGS__ }
3745
3746 /* Wrapper for check_match that passes down the current
3747 __FILE__/__LINE__. */
3748 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3749 any_mismatch |= !check_match (__FILE__, __LINE__, \
3750 mock_index, \
3751 NAME, MATCH_TYPE, COMPLETION_MODE, \
3752 EXPECTED_LIST, NULL)
3753
3754 /* Identity checks. */
3755 for (const char *sym : test_symbols)
3756 {
3757 /* Should be able to match all existing symbols. */
3758 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3759 EXPECT (sym));
3760
3761 /* Should be able to match all existing symbols with
3762 parameters. */
3763 std::string with_params = std::string (sym) + "(int)";
3764 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3765 EXPECT (sym));
3766
3767 /* Should be able to match all existing symbols with
3768 parameters and qualifiers. */
3769 with_params = std::string (sym) + " ( int ) const";
3770 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3771 EXPECT (sym));
3772
3773 /* This should really find sym, but cp-name-parser.y doesn't
3774 know about lvalue/rvalue qualifiers yet. */
3775 with_params = std::string (sym) + " ( int ) &&";
3776 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3777 {});
3778 }
3779
3780 /* Check that the name matching algorithm for completion doesn't get
3781 confused with Latin1 'ÿ' / 0xff. See
3782 make_sort_after_prefix_name. */
3783 {
3784 static const char str[] = "\377";
3785 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3786 EXPECT ("\377", "\377\377123"));
3787 }
3788
3789 /* Check that the increment-last-char in the matching algorithm for
3790 completion doesn't match "t1_fund" when completing "t1_func". */
3791 {
3792 static const char str[] = "t1_func";
3793 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3794 EXPECT ("t1_func", "t1_func1"));
3795 }
3796
3797 /* Check that completion mode works at each prefix of the expected
3798 symbol name. */
3799 {
3800 static const char str[] = "function(int)";
3801 size_t len = strlen (str);
3802 std::string lookup;
3803
3804 for (size_t i = 1; i < len; i++)
3805 {
3806 lookup.assign (str, i);
3807 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3808 EXPECT ("function"));
3809 }
3810 }
3811
3812 /* While "w" is a prefix of both components, the match function
3813 should still only be called once. */
3814 {
3815 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3816 EXPECT ("w1::w2"));
3817 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3818 EXPECT ("w1::w2"));
3819 }
3820
3821 /* Same, with a "complicated" symbol. */
3822 {
3823 static const char str[] = Z_SYM_NAME;
3824 size_t len = strlen (str);
3825 std::string lookup;
3826
3827 for (size_t i = 1; i < len; i++)
3828 {
3829 lookup.assign (str, i);
3830 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3831 EXPECT (Z_SYM_NAME));
3832 }
3833 }
3834
3835 /* In FULL mode, an incomplete symbol doesn't match. */
3836 {
3837 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3838 {});
3839 }
3840
3841 /* A complete symbol with parameters matches any overload, since the
3842 index has no overload info. */
3843 {
3844 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3845 EXPECT ("std::zfunction", "std::zfunction2"));
3846 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
3847 EXPECT ("std::zfunction", "std::zfunction2"));
3848 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
3849 EXPECT ("std::zfunction", "std::zfunction2"));
3850 }
3851
3852 /* Check that whitespace is ignored appropriately. A symbol with a
3853 template argument list. */
3854 {
3855 static const char expected[] = "ns::foo<int>";
3856 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
3857 EXPECT (expected));
3858 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
3859 EXPECT (expected));
3860 }
3861
3862 /* Check that whitespace is ignored appropriately. A symbol with a
3863 template argument list that includes a pointer. */
3864 {
3865 static const char expected[] = "ns::foo<char*>";
3866 /* Try both completion and non-completion modes. */
3867 static const bool completion_mode[2] = {false, true};
3868 for (size_t i = 0; i < 2; i++)
3869 {
3870 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
3871 completion_mode[i], EXPECT (expected));
3872 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
3873 completion_mode[i], EXPECT (expected));
3874
3875 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
3876 completion_mode[i], EXPECT (expected));
3877 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
3878 completion_mode[i], EXPECT (expected));
3879 }
3880 }
3881
3882 {
3883 /* Check method qualifiers are ignored. */
3884 static const char expected[] = "ns::foo<char*>";
3885 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3886 symbol_name_match_type::FULL, true, EXPECT (expected));
3887 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3888 symbol_name_match_type::FULL, true, EXPECT (expected));
3889 CHECK_MATCH ("foo < char * > ( int ) const",
3890 symbol_name_match_type::WILD, true, EXPECT (expected));
3891 CHECK_MATCH ("foo < char * > ( int ) &&",
3892 symbol_name_match_type::WILD, true, EXPECT (expected));
3893 }
3894
3895 /* Test lookup names that don't match anything. */
3896 {
3897 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
3898 {});
3899
3900 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
3901 {});
3902 }
3903
3904 /* Some wild matching tests, exercising "(anonymous namespace)",
3905 which should not be confused with a parameter list. */
3906 {
3907 static const char *syms[] = {
3908 "A::B::C",
3909 "B::C",
3910 "C",
3911 "A :: B :: C ( int )",
3912 "B :: C ( int )",
3913 "C ( int )",
3914 };
3915
3916 for (const char *s : syms)
3917 {
3918 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
3919 EXPECT ("(anonymous namespace)::A::B::C"));
3920 }
3921 }
3922
3923 {
3924 static const char expected[] = "ns2::tmpl<int>::foo2";
3925 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
3926 EXPECT (expected));
3927 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
3928 EXPECT (expected));
3929 }
3930
3931 SELF_CHECK (!any_mismatch);
3932
3933 #undef EXPECT
3934 #undef CHECK_MATCH
3935 }
3936
3937 static void
3938 run_test ()
3939 {
3940 test_mapped_index_find_name_component_bounds ();
3941 test_dw2_expand_symtabs_matching_symbol ();
3942 }
3943
3944 }} // namespace selftests::dw2_expand_symtabs_matching
3945
3946 #endif /* GDB_SELF_TEST */
3947
3948 /* If FILE_MATCHER is NULL or if PER_CU has
3949 dwarf2_per_cu_quick_data::MARK set (see
3950 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3951 EXPANSION_NOTIFY on it. */
3952
3953 static bool
3954 dw2_expand_symtabs_matching_one
3955 (dwarf2_per_cu_data *per_cu,
3956 dwarf2_per_objfile *per_objfile,
3957 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3958 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
3959 {
3960 if (file_matcher == NULL || per_cu->mark)
3961 {
3962 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
3963
3964 compunit_symtab *symtab
3965 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3966 gdb_assert (symtab != nullptr);
3967
3968 if (expansion_notify != NULL && symtab_was_null)
3969 return expansion_notify (symtab);
3970 }
3971 return true;
3972 }
3973
3974 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3975 matched, to expand corresponding CUs that were marked. IDX is the
3976 index of the symbol name that matched. */
3977
3978 static bool
3979 dw2_expand_marked_cus
3980 (dwarf2_per_objfile *per_objfile, offset_type idx,
3981 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3982 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
3983 block_search_flags search_flags,
3984 search_domain kind)
3985 {
3986 offset_type vec_len, vec_idx;
3987 bool global_seen = false;
3988 mapped_index &index
3989 = (static_cast<mapped_index &>
3990 (*per_objfile->per_bfd->index_table.get ()));
3991
3992 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
3993 vec_len = vec[0];
3994 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3995 {
3996 offset_type cu_index_and_attrs = vec[vec_idx + 1];
3997 /* This value is only valid for index versions >= 7. */
3998 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3999 gdb_index_symbol_kind symbol_kind =
4000 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4001 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4002 /* Only check the symbol attributes if they're present.
4003 Indices prior to version 7 don't record them,
4004 and indices >= 7 may elide them for certain symbols
4005 (gold does this). */
4006 int attrs_valid =
4007 (index.version >= 7
4008 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4009
4010 /* Work around gold/15646. */
4011 if (attrs_valid
4012 && !is_static
4013 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4014 {
4015 if (global_seen)
4016 continue;
4017
4018 global_seen = true;
4019 }
4020
4021 /* Only check the symbol's kind if it has one. */
4022 if (attrs_valid)
4023 {
4024 if (is_static)
4025 {
4026 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4027 continue;
4028 }
4029 else
4030 {
4031 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4032 continue;
4033 }
4034
4035 switch (kind)
4036 {
4037 case VARIABLES_DOMAIN:
4038 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4039 continue;
4040 break;
4041 case FUNCTIONS_DOMAIN:
4042 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4043 continue;
4044 break;
4045 case TYPES_DOMAIN:
4046 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4047 continue;
4048 break;
4049 case MODULES_DOMAIN:
4050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4051 continue;
4052 break;
4053 default:
4054 break;
4055 }
4056 }
4057
4058 /* Don't crash on bad data. */
4059 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4060 {
4061 complaint (_(".gdb_index entry has bad CU index"
4062 " [in module %s]"), objfile_name (per_objfile->objfile));
4063 continue;
4064 }
4065
4066 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4067 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4068 expansion_notify))
4069 return false;
4070 }
4071
4072 return true;
4073 }
4074
4075 /* If FILE_MATCHER is non-NULL, set all the
4076 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4077 that match FILE_MATCHER. */
4078
4079 static void
4080 dw_expand_symtabs_matching_file_matcher
4081 (dwarf2_per_objfile *per_objfile,
4082 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4083 {
4084 if (file_matcher == NULL)
4085 return;
4086
4087 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4088 htab_eq_pointer,
4089 NULL, xcalloc, xfree));
4090 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4091 htab_eq_pointer,
4092 NULL, xcalloc, xfree));
4093
4094 /* The rule is CUs specify all the files, including those used by
4095 any TU, so there's no need to scan TUs here. */
4096
4097 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4098 {
4099 QUIT;
4100
4101 if (per_cu->is_debug_types)
4102 continue;
4103 per_cu->mark = 0;
4104
4105 /* We only need to look at symtabs not already expanded. */
4106 if (per_objfile->symtab_set_p (per_cu.get ()))
4107 continue;
4108
4109 if (per_cu->fnd != nullptr)
4110 {
4111 file_and_directory *fnd = per_cu->fnd.get ();
4112
4113 if (file_matcher (fnd->get_name (), false))
4114 {
4115 per_cu->mark = 1;
4116 continue;
4117 }
4118
4119 /* Before we invoke realpath, which can get expensive when many
4120 files are involved, do a quick comparison of the basenames. */
4121 if ((basenames_may_differ
4122 || file_matcher (lbasename (fnd->get_name ()), true))
4123 && file_matcher (fnd->get_fullname (), false))
4124 {
4125 per_cu->mark = 1;
4126 continue;
4127 }
4128 }
4129
4130 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4131 per_objfile);
4132 if (file_data == NULL)
4133 continue;
4134
4135 if (htab_find (visited_not_found.get (), file_data) != NULL)
4136 continue;
4137 else if (htab_find (visited_found.get (), file_data) != NULL)
4138 {
4139 per_cu->mark = 1;
4140 continue;
4141 }
4142
4143 for (int j = 0; j < file_data->num_file_names; ++j)
4144 {
4145 const char *this_real_name;
4146
4147 if (file_matcher (file_data->file_names[j], false))
4148 {
4149 per_cu->mark = 1;
4150 break;
4151 }
4152
4153 /* Before we invoke realpath, which can get expensive when many
4154 files are involved, do a quick comparison of the basenames. */
4155 if (!basenames_may_differ
4156 && !file_matcher (lbasename (file_data->file_names[j]),
4157 true))
4158 continue;
4159
4160 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4161 if (file_matcher (this_real_name, false))
4162 {
4163 per_cu->mark = 1;
4164 break;
4165 }
4166 }
4167
4168 void **slot = htab_find_slot (per_cu->mark
4169 ? visited_found.get ()
4170 : visited_not_found.get (),
4171 file_data, INSERT);
4172 *slot = file_data;
4173 }
4174 }
4175
4176 bool
4177 dwarf2_gdb_index::expand_symtabs_matching
4178 (struct objfile *objfile,
4179 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4180 const lookup_name_info *lookup_name,
4181 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4182 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4183 block_search_flags search_flags,
4184 domain_enum domain,
4185 enum search_domain kind)
4186 {
4187 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4188
4189 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4190
4191 /* This invariant is documented in quick-functions.h. */
4192 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4193 if (lookup_name == nullptr)
4194 {
4195 for (dwarf2_per_cu_data *per_cu
4196 : all_comp_units_range (per_objfile->per_bfd))
4197 {
4198 QUIT;
4199
4200 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4201 file_matcher,
4202 expansion_notify))
4203 return false;
4204 }
4205 return true;
4206 }
4207
4208 mapped_index &index
4209 = (static_cast<mapped_index &>
4210 (*per_objfile->per_bfd->index_table.get ()));
4211
4212 bool result
4213 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4214 symbol_matcher,
4215 [&] (offset_type idx)
4216 {
4217 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4218 expansion_notify, search_flags, kind))
4219 return false;
4220 return true;
4221 }, per_objfile);
4222
4223 return result;
4224 }
4225
4226 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4227 symtab. */
4228
4229 static struct compunit_symtab *
4230 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4231 CORE_ADDR pc)
4232 {
4233 int i;
4234
4235 if (cust->blockvector () != nullptr
4236 && blockvector_contains_pc (cust->blockvector (), pc))
4237 return cust;
4238
4239 if (cust->includes == NULL)
4240 return NULL;
4241
4242 for (i = 0; cust->includes[i]; ++i)
4243 {
4244 struct compunit_symtab *s = cust->includes[i];
4245
4246 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4247 if (s != NULL)
4248 return s;
4249 }
4250
4251 return NULL;
4252 }
4253
4254 dwarf2_per_cu_data *
4255 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
4256 CORE_ADDR adjusted_pc)
4257 {
4258 if (per_bfd->index_addrmap == nullptr)
4259 return nullptr;
4260 return ((struct dwarf2_per_cu_data *)
4261 per_bfd->index_addrmap->find (adjusted_pc));
4262 }
4263
4264 struct compunit_symtab *
4265 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4266 (struct objfile *objfile,
4267 struct bound_minimal_symbol msymbol,
4268 CORE_ADDR pc,
4269 struct obj_section *section,
4270 int warn_if_readin)
4271 {
4272 struct compunit_symtab *result;
4273
4274 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4275
4276 CORE_ADDR baseaddr = objfile->text_section_offset ();
4277 struct dwarf2_per_cu_data *data = find_per_cu (per_objfile->per_bfd,
4278 pc - baseaddr);
4279 if (data == nullptr)
4280 return nullptr;
4281
4282 if (warn_if_readin && per_objfile->symtab_set_p (data))
4283 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4284 paddress (objfile->arch (), pc));
4285
4286 result = recursively_find_pc_sect_compunit_symtab
4287 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4288
4289 if (warn_if_readin && result == nullptr)
4290 warning (_("(Error: pc %s in address map, but not in symtab.)"),
4291 paddress (objfile->arch (), pc));
4292
4293 return result;
4294 }
4295
4296 void
4297 dwarf2_base_index_functions::map_symbol_filenames
4298 (struct objfile *objfile,
4299 gdb::function_view<symbol_filename_ftype> fun,
4300 bool need_fullname)
4301 {
4302 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4303
4304 /* Use caches to ensure we only call FUN once for each filename. */
4305 filename_seen_cache filenames_cache;
4306 std::unordered_set<quick_file_names *> qfn_cache;
4307
4308 /* The rule is CUs specify all the files, including those used by any TU,
4309 so there's no need to scan TUs here. We can ignore file names coming
4310 from already-expanded CUs. It is possible that an expanded CU might
4311 reuse the file names data from a currently unexpanded CU, in this
4312 case we don't want to report the files from the unexpanded CU. */
4313
4314 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4315 {
4316 if (!per_cu->is_debug_types
4317 && per_objfile->symtab_set_p (per_cu.get ()))
4318 {
4319 if (per_cu->file_names != nullptr)
4320 qfn_cache.insert (per_cu->file_names);
4321 }
4322 }
4323
4324 for (dwarf2_per_cu_data *per_cu
4325 : all_comp_units_range (per_objfile->per_bfd))
4326 {
4327 /* We only need to look at symtabs not already expanded. */
4328 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4329 continue;
4330
4331 if (per_cu->fnd != nullptr)
4332 {
4333 file_and_directory *fnd = per_cu->fnd.get ();
4334
4335 const char *filename = fnd->get_name ();
4336 const char *key = filename;
4337 const char *fullname = nullptr;
4338
4339 if (need_fullname)
4340 {
4341 fullname = fnd->get_fullname ();
4342 key = fullname;
4343 }
4344
4345 if (!filenames_cache.seen (key))
4346 fun (filename, fullname);
4347 }
4348
4349 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4350 if (file_data == nullptr
4351 || qfn_cache.find (file_data) != qfn_cache.end ())
4352 continue;
4353
4354 for (int j = 0; j < file_data->num_file_names; ++j)
4355 {
4356 const char *filename = file_data->file_names[j];
4357 const char *key = filename;
4358 const char *fullname = nullptr;
4359
4360 if (need_fullname)
4361 {
4362 fullname = dw2_get_real_path (per_objfile, file_data, j);
4363 key = fullname;
4364 }
4365
4366 if (!filenames_cache.seen (key))
4367 fun (filename, fullname);
4368 }
4369 }
4370 }
4371
4372 bool
4373 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4374 {
4375 return true;
4376 }
4377
4378 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4379
4380 bool
4381 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4382 {
4383 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4384
4385 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4386 {
4387 /* Is this already expanded? */
4388 if (per_objfile->symtab_set_p (per_cu.get ()))
4389 continue;
4390
4391 /* It has not yet been expanded. */
4392 return true;
4393 }
4394
4395 return false;
4396 }
4397
4398 /* DWARF-5 debug_names reader. */
4399
4400 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4401 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4402
4403 /* A helper function that reads the .debug_names section in SECTION
4404 and fills in MAP. FILENAME is the name of the file containing the
4405 section; it is used for error reporting.
4406
4407 Returns true if all went well, false otherwise. */
4408
4409 static bool
4410 read_debug_names_from_section (struct objfile *objfile,
4411 const char *filename,
4412 struct dwarf2_section_info *section,
4413 mapped_debug_names &map)
4414 {
4415 if (section->empty ())
4416 return false;
4417
4418 /* Older elfutils strip versions could keep the section in the main
4419 executable while splitting it for the separate debug info file. */
4420 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4421 return false;
4422
4423 section->read (objfile);
4424
4425 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4426
4427 const gdb_byte *addr = section->buffer;
4428
4429 bfd *const abfd = section->get_bfd_owner ();
4430
4431 unsigned int bytes_read;
4432 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4433 addr += bytes_read;
4434
4435 map.dwarf5_is_dwarf64 = bytes_read != 4;
4436 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4437 if (bytes_read + length != section->size)
4438 {
4439 /* There may be multiple per-CU indices. */
4440 warning (_("Section .debug_names in %s length %s does not match "
4441 "section length %s, ignoring .debug_names."),
4442 filename, plongest (bytes_read + length),
4443 pulongest (section->size));
4444 return false;
4445 }
4446
4447 /* The version number. */
4448 uint16_t version = read_2_bytes (abfd, addr);
4449 addr += 2;
4450 if (version != 5)
4451 {
4452 warning (_("Section .debug_names in %s has unsupported version %d, "
4453 "ignoring .debug_names."),
4454 filename, version);
4455 return false;
4456 }
4457
4458 /* Padding. */
4459 uint16_t padding = read_2_bytes (abfd, addr);
4460 addr += 2;
4461 if (padding != 0)
4462 {
4463 warning (_("Section .debug_names in %s has unsupported padding %d, "
4464 "ignoring .debug_names."),
4465 filename, padding);
4466 return false;
4467 }
4468
4469 /* comp_unit_count - The number of CUs in the CU list. */
4470 map.cu_count = read_4_bytes (abfd, addr);
4471 addr += 4;
4472
4473 /* local_type_unit_count - The number of TUs in the local TU
4474 list. */
4475 map.tu_count = read_4_bytes (abfd, addr);
4476 addr += 4;
4477
4478 /* foreign_type_unit_count - The number of TUs in the foreign TU
4479 list. */
4480 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4481 addr += 4;
4482 if (foreign_tu_count != 0)
4483 {
4484 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4485 "ignoring .debug_names."),
4486 filename, static_cast<unsigned long> (foreign_tu_count));
4487 return false;
4488 }
4489
4490 /* bucket_count - The number of hash buckets in the hash lookup
4491 table. */
4492 map.bucket_count = read_4_bytes (abfd, addr);
4493 addr += 4;
4494
4495 /* name_count - The number of unique names in the index. */
4496 map.name_count = read_4_bytes (abfd, addr);
4497 addr += 4;
4498
4499 /* abbrev_table_size - The size in bytes of the abbreviations
4500 table. */
4501 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4502 addr += 4;
4503
4504 /* augmentation_string_size - The size in bytes of the augmentation
4505 string. This value is rounded up to a multiple of 4. */
4506 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4507 addr += 4;
4508 map.augmentation_is_gdb = ((augmentation_string_size
4509 == sizeof (dwarf5_augmentation))
4510 && memcmp (addr, dwarf5_augmentation,
4511 sizeof (dwarf5_augmentation)) == 0);
4512 augmentation_string_size += (-augmentation_string_size) & 3;
4513 addr += augmentation_string_size;
4514
4515 /* List of CUs */
4516 map.cu_table_reordered = addr;
4517 addr += map.cu_count * map.offset_size;
4518
4519 /* List of Local TUs */
4520 map.tu_table_reordered = addr;
4521 addr += map.tu_count * map.offset_size;
4522
4523 /* Hash Lookup Table */
4524 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4525 addr += map.bucket_count * 4;
4526 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4527 addr += map.name_count * 4;
4528
4529 /* Name Table */
4530 map.name_table_string_offs_reordered = addr;
4531 addr += map.name_count * map.offset_size;
4532 map.name_table_entry_offs_reordered = addr;
4533 addr += map.name_count * map.offset_size;
4534
4535 const gdb_byte *abbrev_table_start = addr;
4536 for (;;)
4537 {
4538 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4539 addr += bytes_read;
4540 if (index_num == 0)
4541 break;
4542
4543 const auto insertpair
4544 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4545 if (!insertpair.second)
4546 {
4547 warning (_("Section .debug_names in %s has duplicate index %s, "
4548 "ignoring .debug_names."),
4549 filename, pulongest (index_num));
4550 return false;
4551 }
4552 mapped_debug_names::index_val &indexval = insertpair.first->second;
4553 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4554 addr += bytes_read;
4555
4556 for (;;)
4557 {
4558 mapped_debug_names::index_val::attr attr;
4559 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4560 addr += bytes_read;
4561 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4562 addr += bytes_read;
4563 if (attr.form == DW_FORM_implicit_const)
4564 {
4565 attr.implicit_const = read_signed_leb128 (abfd, addr,
4566 &bytes_read);
4567 addr += bytes_read;
4568 }
4569 if (attr.dw_idx == 0 && attr.form == 0)
4570 break;
4571 indexval.attr_vec.push_back (std::move (attr));
4572 }
4573 }
4574 if (addr != abbrev_table_start + abbrev_table_size)
4575 {
4576 warning (_("Section .debug_names in %s has abbreviation_table "
4577 "of size %s vs. written as %u, ignoring .debug_names."),
4578 filename, plongest (addr - abbrev_table_start),
4579 abbrev_table_size);
4580 return false;
4581 }
4582 map.entry_pool = addr;
4583
4584 return true;
4585 }
4586
4587 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4588 list. */
4589
4590 static void
4591 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4592 const mapped_debug_names &map,
4593 dwarf2_section_info &section,
4594 bool is_dwz)
4595 {
4596 if (!map.augmentation_is_gdb)
4597 {
4598 for (uint32_t i = 0; i < map.cu_count; ++i)
4599 {
4600 sect_offset sect_off
4601 = (sect_offset) (extract_unsigned_integer
4602 (map.cu_table_reordered + i * map.offset_size,
4603 map.offset_size,
4604 map.dwarf5_byte_order));
4605 /* We don't know the length of the CU, because the CU list in a
4606 .debug_names index can be incomplete, so we can't use the start
4607 of the next CU as end of this CU. We create the CUs here with
4608 length 0, and in cutu_reader::cutu_reader we'll fill in the
4609 actual length. */
4610 dwarf2_per_cu_data_up per_cu
4611 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4612 sect_off, 0);
4613 per_bfd->all_comp_units.push_back (std::move (per_cu));
4614 }
4615 return;
4616 }
4617
4618 sect_offset sect_off_prev;
4619 for (uint32_t i = 0; i <= map.cu_count; ++i)
4620 {
4621 sect_offset sect_off_next;
4622 if (i < map.cu_count)
4623 {
4624 sect_off_next
4625 = (sect_offset) (extract_unsigned_integer
4626 (map.cu_table_reordered + i * map.offset_size,
4627 map.offset_size,
4628 map.dwarf5_byte_order));
4629 }
4630 else
4631 sect_off_next = (sect_offset) section.size;
4632 if (i >= 1)
4633 {
4634 const ULONGEST length = sect_off_next - sect_off_prev;
4635 dwarf2_per_cu_data_up per_cu
4636 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4637 sect_off_prev, length);
4638 per_bfd->all_comp_units.push_back (std::move (per_cu));
4639 }
4640 sect_off_prev = sect_off_next;
4641 }
4642 }
4643
4644 /* Read the CU list from the mapped index, and use it to create all
4645 the CU objects for this dwarf2_per_objfile. */
4646
4647 static void
4648 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4649 const mapped_debug_names &map,
4650 const mapped_debug_names &dwz_map)
4651 {
4652 gdb_assert (per_bfd->all_comp_units.empty ());
4653 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4654
4655 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4656 false /* is_dwz */);
4657
4658 if (dwz_map.cu_count == 0)
4659 return;
4660
4661 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4662 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4663 true /* is_dwz */);
4664 }
4665
4666 /* Read .debug_names. If everything went ok, initialize the "quick"
4667 elements of all the CUs and return true. Otherwise, return false. */
4668
4669 static bool
4670 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4671 {
4672 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4673 mapped_debug_names dwz_map;
4674 struct objfile *objfile = per_objfile->objfile;
4675 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4676
4677 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4678 &per_bfd->debug_names, *map))
4679 return false;
4680
4681 /* Don't use the index if it's empty. */
4682 if (map->name_count == 0)
4683 return false;
4684
4685 /* If there is a .dwz file, read it so we can get its CU list as
4686 well. */
4687 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4688 if (dwz != NULL)
4689 {
4690 if (!read_debug_names_from_section (objfile,
4691 bfd_get_filename (dwz->dwz_bfd.get ()),
4692 &dwz->debug_names, dwz_map))
4693 {
4694 warning (_("could not read '.debug_names' section from %s; skipping"),
4695 bfd_get_filename (dwz->dwz_bfd.get ()));
4696 return false;
4697 }
4698 }
4699
4700 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4701
4702 if (map->tu_count != 0)
4703 {
4704 /* We can only handle a single .debug_types when we have an
4705 index. */
4706 if (per_bfd->types.size () != 1)
4707 {
4708 per_bfd->all_comp_units.clear ();
4709 return false;
4710 }
4711
4712 dwarf2_section_info *section = &per_bfd->types[0];
4713
4714 create_signatured_type_table_from_debug_names
4715 (per_objfile, *map, section, &per_bfd->abbrev);
4716 }
4717
4718 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4719
4720 per_bfd->index_table = std::move (map);
4721 per_bfd->quick_file_names_table =
4722 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4723
4724 return true;
4725 }
4726
4727 /* Type used to manage iterating over all CUs looking for a symbol for
4728 .debug_names. */
4729
4730 class dw2_debug_names_iterator
4731 {
4732 public:
4733 dw2_debug_names_iterator (const mapped_debug_names &map,
4734 block_search_flags block_index,
4735 domain_enum domain,
4736 const char *name, dwarf2_per_objfile *per_objfile)
4737 : m_map (map), m_block_index (block_index), m_domain (domain),
4738 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4739 m_per_objfile (per_objfile)
4740 {}
4741
4742 dw2_debug_names_iterator (const mapped_debug_names &map,
4743 search_domain search, uint32_t namei,
4744 dwarf2_per_objfile *per_objfile,
4745 domain_enum domain = UNDEF_DOMAIN)
4746 : m_map (map),
4747 m_domain (domain),
4748 m_search (search),
4749 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4750 m_per_objfile (per_objfile)
4751 {}
4752
4753 dw2_debug_names_iterator (const mapped_debug_names &map,
4754 block_search_flags block_index, domain_enum domain,
4755 uint32_t namei, dwarf2_per_objfile *per_objfile)
4756 : m_map (map), m_block_index (block_index), m_domain (domain),
4757 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4758 m_per_objfile (per_objfile)
4759 {}
4760
4761 /* Return the next matching CU or NULL if there are no more. */
4762 dwarf2_per_cu_data *next ();
4763
4764 private:
4765 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4766 const char *name,
4767 dwarf2_per_objfile *per_objfile);
4768 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4769 uint32_t namei,
4770 dwarf2_per_objfile *per_objfile);
4771
4772 /* The internalized form of .debug_names. */
4773 const mapped_debug_names &m_map;
4774
4775 /* Restrict the search to these blocks. */
4776 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4777 | SEARCH_STATIC_BLOCK);
4778
4779 /* The kind of symbol we're looking for. */
4780 const domain_enum m_domain = UNDEF_DOMAIN;
4781 const search_domain m_search = ALL_DOMAIN;
4782
4783 /* The list of CUs from the index entry of the symbol, or NULL if
4784 not found. */
4785 const gdb_byte *m_addr;
4786
4787 dwarf2_per_objfile *m_per_objfile;
4788 };
4789
4790 const char *
4791 mapped_debug_names::namei_to_name
4792 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4793 {
4794 const ULONGEST namei_string_offs
4795 = extract_unsigned_integer ((name_table_string_offs_reordered
4796 + namei * offset_size),
4797 offset_size,
4798 dwarf5_byte_order);
4799 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4800 }
4801
4802 /* Find a slot in .debug_names for the object named NAME. If NAME is
4803 found, return pointer to its pool data. If NAME cannot be found,
4804 return NULL. */
4805
4806 const gdb_byte *
4807 dw2_debug_names_iterator::find_vec_in_debug_names
4808 (const mapped_debug_names &map, const char *name,
4809 dwarf2_per_objfile *per_objfile)
4810 {
4811 int (*cmp) (const char *, const char *);
4812
4813 gdb::unique_xmalloc_ptr<char> without_params;
4814 if (current_language->la_language == language_cplus
4815 || current_language->la_language == language_fortran
4816 || current_language->la_language == language_d)
4817 {
4818 /* NAME is already canonical. Drop any qualifiers as
4819 .debug_names does not contain any. */
4820
4821 if (strchr (name, '(') != NULL)
4822 {
4823 without_params = cp_remove_params (name);
4824 if (without_params != NULL)
4825 name = without_params.get ();
4826 }
4827 }
4828
4829 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4830
4831 const uint32_t full_hash = dwarf5_djb_hash (name);
4832 uint32_t namei
4833 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4834 (map.bucket_table_reordered
4835 + (full_hash % map.bucket_count)), 4,
4836 map.dwarf5_byte_order);
4837 if (namei == 0)
4838 return NULL;
4839 --namei;
4840 if (namei >= map.name_count)
4841 {
4842 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4843 "[in module %s]"),
4844 namei, map.name_count,
4845 objfile_name (per_objfile->objfile));
4846 return NULL;
4847 }
4848
4849 for (;;)
4850 {
4851 const uint32_t namei_full_hash
4852 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4853 (map.hash_table_reordered + namei), 4,
4854 map.dwarf5_byte_order);
4855 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4856 return NULL;
4857
4858 if (full_hash == namei_full_hash)
4859 {
4860 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4861
4862 #if 0 /* An expensive sanity check. */
4863 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4864 {
4865 complaint (_("Wrong .debug_names hash for string at index %u "
4866 "[in module %s]"),
4867 namei, objfile_name (dwarf2_per_objfile->objfile));
4868 return NULL;
4869 }
4870 #endif
4871
4872 if (cmp (namei_string, name) == 0)
4873 {
4874 const ULONGEST namei_entry_offs
4875 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4876 + namei * map.offset_size),
4877 map.offset_size, map.dwarf5_byte_order);
4878 return map.entry_pool + namei_entry_offs;
4879 }
4880 }
4881
4882 ++namei;
4883 if (namei >= map.name_count)
4884 return NULL;
4885 }
4886 }
4887
4888 const gdb_byte *
4889 dw2_debug_names_iterator::find_vec_in_debug_names
4890 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4891 {
4892 if (namei >= map.name_count)
4893 {
4894 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4895 "[in module %s]"),
4896 namei, map.name_count,
4897 objfile_name (per_objfile->objfile));
4898 return NULL;
4899 }
4900
4901 const ULONGEST namei_entry_offs
4902 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4903 + namei * map.offset_size),
4904 map.offset_size, map.dwarf5_byte_order);
4905 return map.entry_pool + namei_entry_offs;
4906 }
4907
4908 /* See dw2_debug_names_iterator. */
4909
4910 dwarf2_per_cu_data *
4911 dw2_debug_names_iterator::next ()
4912 {
4913 if (m_addr == NULL)
4914 return NULL;
4915
4916 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4917 struct objfile *objfile = m_per_objfile->objfile;
4918 bfd *const abfd = objfile->obfd;
4919
4920 again:
4921
4922 unsigned int bytes_read;
4923 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4924 m_addr += bytes_read;
4925 if (abbrev == 0)
4926 return NULL;
4927
4928 const auto indexval_it = m_map.abbrev_map.find (abbrev);
4929 if (indexval_it == m_map.abbrev_map.cend ())
4930 {
4931 complaint (_("Wrong .debug_names undefined abbrev code %s "
4932 "[in module %s]"),
4933 pulongest (abbrev), objfile_name (objfile));
4934 return NULL;
4935 }
4936 const mapped_debug_names::index_val &indexval = indexval_it->second;
4937 enum class symbol_linkage {
4938 unknown,
4939 static_,
4940 extern_,
4941 } symbol_linkage_ = symbol_linkage::unknown;
4942 dwarf2_per_cu_data *per_cu = NULL;
4943 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
4944 {
4945 ULONGEST ull;
4946 switch (attr.form)
4947 {
4948 case DW_FORM_implicit_const:
4949 ull = attr.implicit_const;
4950 break;
4951 case DW_FORM_flag_present:
4952 ull = 1;
4953 break;
4954 case DW_FORM_udata:
4955 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4956 m_addr += bytes_read;
4957 break;
4958 case DW_FORM_ref4:
4959 ull = read_4_bytes (abfd, m_addr);
4960 m_addr += 4;
4961 break;
4962 case DW_FORM_ref8:
4963 ull = read_8_bytes (abfd, m_addr);
4964 m_addr += 8;
4965 break;
4966 case DW_FORM_ref_sig8:
4967 ull = read_8_bytes (abfd, m_addr);
4968 m_addr += 8;
4969 break;
4970 default:
4971 complaint (_("Unsupported .debug_names form %s [in module %s]"),
4972 dwarf_form_name (attr.form),
4973 objfile_name (objfile));
4974 return NULL;
4975 }
4976 switch (attr.dw_idx)
4977 {
4978 case DW_IDX_compile_unit:
4979 {
4980 /* Don't crash on bad data. */
4981 int nr_cus = (per_bfd->all_comp_units.size ()
4982 - per_bfd->tu_stats.nr_tus);
4983 if (ull >= nr_cus)
4984 {
4985 complaint (_(".debug_names entry has bad CU index %s"
4986 " [in module %s]"),
4987 pulongest (ull),
4988 objfile_name (objfile));
4989 continue;
4990 }
4991 }
4992 per_cu = per_bfd->get_cu (ull);
4993 break;
4994 case DW_IDX_type_unit:
4995 /* Don't crash on bad data. */
4996 if (ull >= per_bfd->tu_stats.nr_tus)
4997 {
4998 complaint (_(".debug_names entry has bad TU index %s"
4999 " [in module %s]"),
5000 pulongest (ull),
5001 objfile_name (objfile));
5002 continue;
5003 }
5004 {
5005 int nr_cus = (per_bfd->all_comp_units.size ()
5006 - per_bfd->tu_stats.nr_tus);
5007 per_cu = per_bfd->get_cu (nr_cus + ull);
5008 }
5009 break;
5010 case DW_IDX_die_offset:
5011 /* In a per-CU index (as opposed to a per-module index), index
5012 entries without CU attribute implicitly refer to the single CU. */
5013 if (per_cu == NULL)
5014 per_cu = per_bfd->get_cu (0);
5015 break;
5016 case DW_IDX_GNU_internal:
5017 if (!m_map.augmentation_is_gdb)
5018 break;
5019 symbol_linkage_ = symbol_linkage::static_;
5020 break;
5021 case DW_IDX_GNU_external:
5022 if (!m_map.augmentation_is_gdb)
5023 break;
5024 symbol_linkage_ = symbol_linkage::extern_;
5025 break;
5026 }
5027 }
5028
5029 /* Skip if we couldn't find a valid CU/TU index. */
5030 if (per_cu == nullptr)
5031 goto again;
5032
5033 /* Skip if already read in. */
5034 if (m_per_objfile->symtab_set_p (per_cu))
5035 goto again;
5036
5037 /* Check static vs global. */
5038 if (symbol_linkage_ != symbol_linkage::unknown)
5039 {
5040 if (symbol_linkage_ == symbol_linkage::static_)
5041 {
5042 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5043 goto again;
5044 }
5045 else
5046 {
5047 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5048 goto again;
5049 }
5050 }
5051
5052 /* Match dw2_symtab_iter_next, symbol_kind
5053 and debug_names::psymbol_tag. */
5054 switch (m_domain)
5055 {
5056 case VAR_DOMAIN:
5057 switch (indexval.dwarf_tag)
5058 {
5059 case DW_TAG_variable:
5060 case DW_TAG_subprogram:
5061 /* Some types are also in VAR_DOMAIN. */
5062 case DW_TAG_typedef:
5063 case DW_TAG_structure_type:
5064 break;
5065 default:
5066 goto again;
5067 }
5068 break;
5069 case STRUCT_DOMAIN:
5070 switch (indexval.dwarf_tag)
5071 {
5072 case DW_TAG_typedef:
5073 case DW_TAG_structure_type:
5074 break;
5075 default:
5076 goto again;
5077 }
5078 break;
5079 case LABEL_DOMAIN:
5080 switch (indexval.dwarf_tag)
5081 {
5082 case 0:
5083 case DW_TAG_variable:
5084 break;
5085 default:
5086 goto again;
5087 }
5088 break;
5089 case MODULE_DOMAIN:
5090 switch (indexval.dwarf_tag)
5091 {
5092 case DW_TAG_module:
5093 break;
5094 default:
5095 goto again;
5096 }
5097 break;
5098 default:
5099 break;
5100 }
5101
5102 /* Match dw2_expand_symtabs_matching, symbol_kind and
5103 debug_names::psymbol_tag. */
5104 switch (m_search)
5105 {
5106 case VARIABLES_DOMAIN:
5107 switch (indexval.dwarf_tag)
5108 {
5109 case DW_TAG_variable:
5110 break;
5111 default:
5112 goto again;
5113 }
5114 break;
5115 case FUNCTIONS_DOMAIN:
5116 switch (indexval.dwarf_tag)
5117 {
5118 case DW_TAG_subprogram:
5119 break;
5120 default:
5121 goto again;
5122 }
5123 break;
5124 case TYPES_DOMAIN:
5125 switch (indexval.dwarf_tag)
5126 {
5127 case DW_TAG_typedef:
5128 case DW_TAG_structure_type:
5129 break;
5130 default:
5131 goto again;
5132 }
5133 break;
5134 case MODULES_DOMAIN:
5135 switch (indexval.dwarf_tag)
5136 {
5137 case DW_TAG_module:
5138 break;
5139 default:
5140 goto again;
5141 }
5142 default:
5143 break;
5144 }
5145
5146 return per_cu;
5147 }
5148
5149 /* This dumps minimal information about .debug_names. It is called
5150 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5151 uses this to verify that .debug_names has been loaded. */
5152
5153 void
5154 dwarf2_debug_names_index::dump (struct objfile *objfile)
5155 {
5156 gdb_printf (".debug_names: exists\n");
5157 }
5158
5159 void
5160 dwarf2_debug_names_index::expand_matching_symbols
5161 (struct objfile *objfile,
5162 const lookup_name_info &name, domain_enum domain,
5163 int global,
5164 symbol_compare_ftype *ordered_compare)
5165 {
5166 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5167
5168 mapped_debug_names &map
5169 = (static_cast<mapped_debug_names &>
5170 (*per_objfile->per_bfd->index_table.get ()));
5171 const block_search_flags block_flags
5172 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5173
5174 const char *match_name = name.ada ().lookup_name ().c_str ();
5175 auto matcher = [&] (const char *symname)
5176 {
5177 if (ordered_compare == nullptr)
5178 return true;
5179 return ordered_compare (symname, match_name) == 0;
5180 };
5181
5182 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5183 [&] (offset_type namei)
5184 {
5185 /* The name was matched, now expand corresponding CUs that were
5186 marked. */
5187 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5188 per_objfile);
5189
5190 struct dwarf2_per_cu_data *per_cu;
5191 while ((per_cu = iter.next ()) != NULL)
5192 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5193 nullptr);
5194 return true;
5195 }, per_objfile);
5196 }
5197
5198 bool
5199 dwarf2_debug_names_index::expand_symtabs_matching
5200 (struct objfile *objfile,
5201 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5202 const lookup_name_info *lookup_name,
5203 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5204 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5205 block_search_flags search_flags,
5206 domain_enum domain,
5207 enum search_domain kind)
5208 {
5209 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5210
5211 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5212
5213 /* This invariant is documented in quick-functions.h. */
5214 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5215 if (lookup_name == nullptr)
5216 {
5217 for (dwarf2_per_cu_data *per_cu
5218 : all_comp_units_range (per_objfile->per_bfd))
5219 {
5220 QUIT;
5221
5222 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5223 file_matcher,
5224 expansion_notify))
5225 return false;
5226 }
5227 return true;
5228 }
5229
5230 mapped_debug_names &map
5231 = (static_cast<mapped_debug_names &>
5232 (*per_objfile->per_bfd->index_table.get ()));
5233
5234 bool result
5235 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5236 symbol_matcher,
5237 [&] (offset_type namei)
5238 {
5239 /* The name was matched, now expand corresponding CUs that were
5240 marked. */
5241 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5242
5243 struct dwarf2_per_cu_data *per_cu;
5244 while ((per_cu = iter.next ()) != NULL)
5245 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5246 file_matcher,
5247 expansion_notify))
5248 return false;
5249 return true;
5250 }, per_objfile);
5251
5252 return result;
5253 }
5254
5255 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5256 to either a dwarf2_per_bfd or dwz_file object. */
5257
5258 template <typename T>
5259 static gdb::array_view<const gdb_byte>
5260 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5261 {
5262 dwarf2_section_info *section = &section_owner->gdb_index;
5263
5264 if (section->empty ())
5265 return {};
5266
5267 /* Older elfutils strip versions could keep the section in the main
5268 executable while splitting it for the separate debug info file. */
5269 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5270 return {};
5271
5272 section->read (obj);
5273
5274 /* dwarf2_section_info::size is a bfd_size_type, while
5275 gdb::array_view works with size_t. On 32-bit hosts, with
5276 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5277 is 32-bit. So we need an explicit narrowing conversion here.
5278 This is fine, because it's impossible to allocate or mmap an
5279 array/buffer larger than what size_t can represent. */
5280 return gdb::make_array_view (section->buffer, section->size);
5281 }
5282
5283 /* Lookup the index cache for the contents of the index associated to
5284 DWARF2_OBJ. */
5285
5286 static gdb::array_view<const gdb_byte>
5287 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5288 {
5289 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5290 if (build_id == nullptr)
5291 return {};
5292
5293 return global_index_cache.lookup_gdb_index (build_id,
5294 &dwarf2_per_bfd->index_cache_res);
5295 }
5296
5297 /* Same as the above, but for DWZ. */
5298
5299 static gdb::array_view<const gdb_byte>
5300 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5301 {
5302 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5303 if (build_id == nullptr)
5304 return {};
5305
5306 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5307 }
5308
5309 static quick_symbol_functions_up make_cooked_index_funcs ();
5310
5311 /* See dwarf2/public.h. */
5312
5313 void
5314 dwarf2_initialize_objfile (struct objfile *objfile)
5315 {
5316 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5317 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5318
5319 dwarf_read_debug_printf ("called");
5320
5321 /* If we're about to read full symbols, don't bother with the
5322 indices. In this case we also don't care if some other debug
5323 format is making psymtabs, because they are all about to be
5324 expanded anyway. */
5325 if ((objfile->flags & OBJF_READNOW))
5326 {
5327 dwarf_read_debug_printf ("readnow requested");
5328
5329 create_all_comp_units (per_objfile);
5330 per_bfd->quick_file_names_table
5331 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5332
5333 objfile->qf.emplace_front (new readnow_functions);
5334 return;
5335 }
5336
5337 /* Was a GDB index already read when we processed an objfile sharing
5338 PER_BFD? */
5339 if (per_bfd->index_table != nullptr)
5340 {
5341 dwarf_read_debug_printf ("re-using symbols");
5342 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5343 return;
5344 }
5345
5346 if (dwarf2_read_debug_names (per_objfile))
5347 {
5348 dwarf_read_debug_printf ("found debug names");
5349 objfile->qf.push_front
5350 (per_bfd->index_table->make_quick_functions ());
5351 return;
5352 }
5353
5354 if (dwarf2_read_gdb_index (per_objfile,
5355 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5356 get_gdb_index_contents_from_section<dwz_file>))
5357 {
5358 dwarf_read_debug_printf ("found gdb index from file");
5359 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5360 return;
5361 }
5362
5363 /* ... otherwise, try to find the index in the index cache. */
5364 if (dwarf2_read_gdb_index (per_objfile,
5365 get_gdb_index_contents_from_cache,
5366 get_gdb_index_contents_from_cache_dwz))
5367 {
5368 dwarf_read_debug_printf ("found gdb index from cache");
5369 global_index_cache.hit ();
5370 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5371 return;
5372 }
5373
5374 global_index_cache.miss ();
5375 objfile->qf.push_front (make_cooked_index_funcs ());
5376 }
5377
5378 \f
5379
5380 /* Build a partial symbol table. */
5381
5382 static void
5383 dwarf2_build_psymtabs (struct objfile *objfile)
5384 {
5385 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5386
5387 if (per_objfile->per_bfd->index_table != nullptr)
5388 return;
5389
5390 try
5391 {
5392 dwarf2_build_psymtabs_hard (per_objfile);
5393
5394 /* (maybe) store an index in the cache. */
5395 global_index_cache.store (per_objfile);
5396 }
5397 catch (const gdb_exception_error &except)
5398 {
5399 exception_print (gdb_stderr, except);
5400 }
5401 }
5402
5403 /* Find the base address of the compilation unit for range lists and
5404 location lists. It will normally be specified by DW_AT_low_pc.
5405 In DWARF-3 draft 4, the base address could be overridden by
5406 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5407 compilation units with discontinuous ranges. */
5408
5409 static void
5410 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5411 {
5412 struct attribute *attr;
5413
5414 cu->base_address.reset ();
5415
5416 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5417 if (attr != nullptr)
5418 cu->base_address = attr->as_address ();
5419 else
5420 {
5421 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5422 if (attr != nullptr)
5423 cu->base_address = attr->as_address ();
5424 }
5425 }
5426
5427 /* Helper function that returns the proper abbrev section for
5428 THIS_CU. */
5429
5430 static struct dwarf2_section_info *
5431 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5432 {
5433 struct dwarf2_section_info *abbrev;
5434 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5435
5436 if (this_cu->is_dwz)
5437 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5438 else
5439 abbrev = &per_bfd->abbrev;
5440
5441 return abbrev;
5442 }
5443
5444 /* Fetch the abbreviation table offset from a comp or type unit header. */
5445
5446 static sect_offset
5447 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5448 struct dwarf2_section_info *section,
5449 sect_offset sect_off)
5450 {
5451 bfd *abfd = section->get_bfd_owner ();
5452 const gdb_byte *info_ptr;
5453 unsigned int initial_length_size, offset_size;
5454 uint16_t version;
5455
5456 section->read (per_objfile->objfile);
5457 info_ptr = section->buffer + to_underlying (sect_off);
5458 read_initial_length (abfd, info_ptr, &initial_length_size);
5459 offset_size = initial_length_size == 4 ? 4 : 8;
5460 info_ptr += initial_length_size;
5461
5462 version = read_2_bytes (abfd, info_ptr);
5463 info_ptr += 2;
5464 if (version >= 5)
5465 {
5466 /* Skip unit type and address size. */
5467 info_ptr += 2;
5468 }
5469
5470 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5471 }
5472
5473 static hashval_t
5474 hash_signatured_type (const void *item)
5475 {
5476 const struct signatured_type *sig_type
5477 = (const struct signatured_type *) item;
5478
5479 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5480 return sig_type->signature;
5481 }
5482
5483 static int
5484 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5485 {
5486 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5487 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5488
5489 return lhs->signature == rhs->signature;
5490 }
5491
5492 /* Allocate a hash table for signatured types. */
5493
5494 static htab_up
5495 allocate_signatured_type_table ()
5496 {
5497 return htab_up (htab_create_alloc (41,
5498 hash_signatured_type,
5499 eq_signatured_type,
5500 NULL, xcalloc, xfree));
5501 }
5502
5503 /* A helper for create_debug_types_hash_table. Read types from SECTION
5504 and fill them into TYPES_HTAB. It will process only type units,
5505 therefore DW_UT_type. */
5506
5507 static void
5508 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5509 struct dwo_file *dwo_file,
5510 dwarf2_section_info *section, htab_up &types_htab,
5511 rcuh_kind section_kind)
5512 {
5513 struct objfile *objfile = per_objfile->objfile;
5514 struct dwarf2_section_info *abbrev_section;
5515 bfd *abfd;
5516 const gdb_byte *info_ptr, *end_ptr;
5517
5518 abbrev_section = &dwo_file->sections.abbrev;
5519
5520 dwarf_read_debug_printf ("Reading %s for %s",
5521 section->get_name (),
5522 abbrev_section->get_file_name ());
5523
5524 section->read (objfile);
5525 info_ptr = section->buffer;
5526
5527 if (info_ptr == NULL)
5528 return;
5529
5530 /* We can't set abfd until now because the section may be empty or
5531 not present, in which case the bfd is unknown. */
5532 abfd = section->get_bfd_owner ();
5533
5534 /* We don't use cutu_reader here because we don't need to read
5535 any dies: the signature is in the header. */
5536
5537 end_ptr = info_ptr + section->size;
5538 while (info_ptr < end_ptr)
5539 {
5540 signatured_type_up sig_type;
5541 struct dwo_unit *dwo_tu;
5542 void **slot;
5543 const gdb_byte *ptr = info_ptr;
5544 struct comp_unit_head header;
5545 unsigned int length;
5546
5547 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5548
5549 /* Initialize it due to a false compiler warning. */
5550 header.signature = -1;
5551 header.type_cu_offset_in_tu = (cu_offset) -1;
5552
5553 /* We need to read the type's signature in order to build the hash
5554 table, but we don't need anything else just yet. */
5555
5556 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5557 abbrev_section, ptr, section_kind);
5558
5559 length = header.get_length ();
5560
5561 /* Skip dummy type units. */
5562 if (ptr >= info_ptr + length
5563 || peek_abbrev_code (abfd, ptr) == 0
5564 || (header.unit_type != DW_UT_type
5565 && header.unit_type != DW_UT_split_type))
5566 {
5567 info_ptr += length;
5568 continue;
5569 }
5570
5571 if (types_htab == NULL)
5572 types_htab = allocate_dwo_unit_table ();
5573
5574 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5575 dwo_tu->dwo_file = dwo_file;
5576 dwo_tu->signature = header.signature;
5577 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5578 dwo_tu->section = section;
5579 dwo_tu->sect_off = sect_off;
5580 dwo_tu->length = length;
5581
5582 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5583 gdb_assert (slot != NULL);
5584 if (*slot != NULL)
5585 complaint (_("debug type entry at offset %s is duplicate to"
5586 " the entry at offset %s, signature %s"),
5587 sect_offset_str (sect_off),
5588 sect_offset_str (dwo_tu->sect_off),
5589 hex_string (header.signature));
5590 *slot = dwo_tu;
5591
5592 dwarf_read_debug_printf_v (" offset %s, signature %s",
5593 sect_offset_str (sect_off),
5594 hex_string (header.signature));
5595
5596 info_ptr += length;
5597 }
5598 }
5599
5600 /* Create the hash table of all entries in the .debug_types
5601 (or .debug_types.dwo) section(s).
5602 DWO_FILE is a pointer to the DWO file object.
5603
5604 The result is a pointer to the hash table or NULL if there are no types.
5605
5606 Note: This function processes DWO files only, not DWP files. */
5607
5608 static void
5609 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5610 struct dwo_file *dwo_file,
5611 gdb::array_view<dwarf2_section_info> type_sections,
5612 htab_up &types_htab)
5613 {
5614 for (dwarf2_section_info &section : type_sections)
5615 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5616 rcuh_kind::TYPE);
5617 }
5618
5619 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5620 If SLOT is non-NULL, it is the entry to use in the hash table.
5621 Otherwise we find one. */
5622
5623 static struct signatured_type *
5624 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5625 {
5626 if (per_objfile->per_bfd->all_comp_units.size ()
5627 == per_objfile->per_bfd->all_comp_units.capacity ())
5628 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5629
5630 signatured_type_up sig_type_holder
5631 = per_objfile->per_bfd->allocate_signatured_type (sig);
5632 signatured_type *sig_type = sig_type_holder.get ();
5633
5634 per_objfile->per_bfd->all_comp_units.emplace_back
5635 (sig_type_holder.release ());
5636
5637 if (slot == NULL)
5638 {
5639 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5640 sig_type, INSERT);
5641 }
5642 gdb_assert (*slot == NULL);
5643 *slot = sig_type;
5644 /* The rest of sig_type must be filled in by the caller. */
5645 return sig_type;
5646 }
5647
5648 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5649 Fill in SIG_ENTRY with DWO_ENTRY. */
5650
5651 static void
5652 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5653 struct signatured_type *sig_entry,
5654 struct dwo_unit *dwo_entry)
5655 {
5656 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5657
5658 /* Make sure we're not clobbering something we don't expect to. */
5659 gdb_assert (! sig_entry->queued);
5660 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5661 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5662 gdb_assert (sig_entry->signature == dwo_entry->signature);
5663 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
5664 || (to_underlying (sig_entry->type_offset_in_section)
5665 == to_underlying (dwo_entry->type_offset_in_tu)));
5666 gdb_assert (sig_entry->type_unit_group == NULL);
5667 gdb_assert (sig_entry->dwo_unit == NULL
5668 || sig_entry->dwo_unit == dwo_entry);
5669
5670 sig_entry->section = dwo_entry->section;
5671 sig_entry->sect_off = dwo_entry->sect_off;
5672 sig_entry->set_length (dwo_entry->length, false);
5673 sig_entry->reading_dwo_directly = 1;
5674 sig_entry->per_bfd = per_bfd;
5675 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5676 sig_entry->dwo_unit = dwo_entry;
5677 }
5678
5679 /* Subroutine of lookup_signatured_type.
5680 If we haven't read the TU yet, create the signatured_type data structure
5681 for a TU to be read in directly from a DWO file, bypassing the stub.
5682 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5683 using .gdb_index, then when reading a CU we want to stay in the DWO file
5684 containing that CU. Otherwise we could end up reading several other DWO
5685 files (due to comdat folding) to process the transitive closure of all the
5686 mentioned TUs, and that can be slow. The current DWO file will have every
5687 type signature that it needs.
5688 We only do this for .gdb_index because in the psymtab case we already have
5689 to read all the DWOs to build the type unit groups. */
5690
5691 static struct signatured_type *
5692 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5693 {
5694 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5695 struct dwo_file *dwo_file;
5696 struct dwo_unit find_dwo_entry, *dwo_entry;
5697 void **slot;
5698
5699 gdb_assert (cu->dwo_unit);
5700
5701 /* If TU skeletons have been removed then we may not have read in any
5702 TUs yet. */
5703 if (per_objfile->per_bfd->signatured_types == NULL)
5704 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5705
5706 /* We only ever need to read in one copy of a signatured type.
5707 Use the global signatured_types array to do our own comdat-folding
5708 of types. If this is the first time we're reading this TU, and
5709 the TU has an entry in .gdb_index, replace the recorded data from
5710 .gdb_index with this TU. */
5711
5712 signatured_type find_sig_entry (sig);
5713 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5714 &find_sig_entry, INSERT);
5715 signatured_type *sig_entry = (struct signatured_type *) *slot;
5716
5717 /* We can get here with the TU already read, *or* in the process of being
5718 read. Don't reassign the global entry to point to this DWO if that's
5719 the case. Also note that if the TU is already being read, it may not
5720 have come from a DWO, the program may be a mix of Fission-compiled
5721 code and non-Fission-compiled code. */
5722
5723 /* Have we already tried to read this TU?
5724 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5725 needn't exist in the global table yet). */
5726 if (sig_entry != NULL && sig_entry->tu_read)
5727 return sig_entry;
5728
5729 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5730 dwo_unit of the TU itself. */
5731 dwo_file = cu->dwo_unit->dwo_file;
5732
5733 /* Ok, this is the first time we're reading this TU. */
5734 if (dwo_file->tus == NULL)
5735 return NULL;
5736 find_dwo_entry.signature = sig;
5737 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
5738 &find_dwo_entry);
5739 if (dwo_entry == NULL)
5740 return NULL;
5741
5742 /* If the global table doesn't have an entry for this TU, add one. */
5743 if (sig_entry == NULL)
5744 sig_entry = add_type_unit (per_objfile, sig, slot);
5745
5746 if (sig_entry->dwo_unit == nullptr)
5747 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5748 sig_entry->tu_read = 1;
5749 return sig_entry;
5750 }
5751
5752 /* Subroutine of lookup_signatured_type.
5753 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5754 then try the DWP file. If the TU stub (skeleton) has been removed then
5755 it won't be in .gdb_index. */
5756
5757 static struct signatured_type *
5758 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5759 {
5760 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5761 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
5762 struct dwo_unit *dwo_entry;
5763 void **slot;
5764
5765 gdb_assert (cu->dwo_unit);
5766 gdb_assert (dwp_file != NULL);
5767
5768 /* If TU skeletons have been removed then we may not have read in any
5769 TUs yet. */
5770 if (per_objfile->per_bfd->signatured_types == NULL)
5771 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5772
5773 signatured_type find_sig_entry (sig);
5774 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5775 &find_sig_entry, INSERT);
5776 signatured_type *sig_entry = (struct signatured_type *) *slot;
5777
5778 /* Have we already tried to read this TU?
5779 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5780 needn't exist in the global table yet). */
5781 if (sig_entry != NULL)
5782 return sig_entry;
5783
5784 if (dwp_file->tus == NULL)
5785 return NULL;
5786 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
5787 1 /* is_debug_types */);
5788 if (dwo_entry == NULL)
5789 return NULL;
5790
5791 sig_entry = add_type_unit (per_objfile, sig, slot);
5792 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5793
5794 return sig_entry;
5795 }
5796
5797 /* Lookup a signature based type for DW_FORM_ref_sig8.
5798 Returns NULL if signature SIG is not present in the table.
5799 It is up to the caller to complain about this. */
5800
5801 static struct signatured_type *
5802 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5803 {
5804 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5805
5806 if (cu->dwo_unit)
5807 {
5808 /* We're in a DWO/DWP file, and we're using .gdb_index.
5809 These cases require special processing. */
5810 if (get_dwp_file (per_objfile) == NULL)
5811 return lookup_dwo_signatured_type (cu, sig);
5812 else
5813 return lookup_dwp_signatured_type (cu, sig);
5814 }
5815 else
5816 {
5817 if (per_objfile->per_bfd->signatured_types == NULL)
5818 return NULL;
5819 signatured_type find_entry (sig);
5820 return ((struct signatured_type *)
5821 htab_find (per_objfile->per_bfd->signatured_types.get (),
5822 &find_entry));
5823 }
5824 }
5825
5826 /* Low level DIE reading support. */
5827
5828 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5829
5830 static void
5831 init_cu_die_reader (struct die_reader_specs *reader,
5832 struct dwarf2_cu *cu,
5833 struct dwarf2_section_info *section,
5834 struct dwo_file *dwo_file,
5835 struct abbrev_table *abbrev_table)
5836 {
5837 gdb_assert (section->readin && section->buffer != NULL);
5838 reader->abfd = section->get_bfd_owner ();
5839 reader->cu = cu;
5840 reader->dwo_file = dwo_file;
5841 reader->die_section = section;
5842 reader->buffer = section->buffer;
5843 reader->buffer_end = section->buffer + section->size;
5844 reader->abbrev_table = abbrev_table;
5845 }
5846
5847 /* Subroutine of cutu_reader to simplify it.
5848 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5849 There's just a lot of work to do, and cutu_reader is big enough
5850 already.
5851
5852 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5853 from it to the DIE in the DWO. If NULL we are skipping the stub.
5854 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5855 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5856 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5857 STUB_COMP_DIR may be non-NULL.
5858 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5859 are filled in with the info of the DIE from the DWO file.
5860 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5861 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5862 kept around for at least as long as *RESULT_READER.
5863
5864 The result is non-zero if a valid (non-dummy) DIE was found. */
5865
5866 static int
5867 read_cutu_die_from_dwo (dwarf2_cu *cu,
5868 struct dwo_unit *dwo_unit,
5869 struct die_info *stub_comp_unit_die,
5870 const char *stub_comp_dir,
5871 struct die_reader_specs *result_reader,
5872 const gdb_byte **result_info_ptr,
5873 struct die_info **result_comp_unit_die,
5874 abbrev_table_up *result_dwo_abbrev_table)
5875 {
5876 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5877 dwarf2_per_cu_data *per_cu = cu->per_cu;
5878 struct objfile *objfile = per_objfile->objfile;
5879 bfd *abfd;
5880 const gdb_byte *begin_info_ptr, *info_ptr;
5881 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5882 int i,num_extra_attrs;
5883 struct dwarf2_section_info *dwo_abbrev_section;
5884 struct die_info *comp_unit_die;
5885
5886 /* At most one of these may be provided. */
5887 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5888
5889 /* These attributes aren't processed until later:
5890 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5891 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5892 referenced later. However, these attributes are found in the stub
5893 which we won't have later. In order to not impose this complication
5894 on the rest of the code, we read them here and copy them to the
5895 DWO CU/TU die. */
5896
5897 stmt_list = NULL;
5898 low_pc = NULL;
5899 high_pc = NULL;
5900 ranges = NULL;
5901 comp_dir = NULL;
5902
5903 if (stub_comp_unit_die != NULL)
5904 {
5905 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5906 DWO file. */
5907 if (!per_cu->is_debug_types)
5908 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5909 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5910 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5911 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5912 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5913
5914 cu->addr_base = stub_comp_unit_die->addr_base ();
5915
5916 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5917 We need the value before we can process DW_AT_ranges values from the
5918 DWO. */
5919 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
5920
5921 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5922 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5923 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5924 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5925 section. */
5926 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
5927 }
5928 else if (stub_comp_dir != NULL)
5929 {
5930 /* Reconstruct the comp_dir attribute to simplify the code below. */
5931 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
5932 comp_dir->name = DW_AT_comp_dir;
5933 comp_dir->form = DW_FORM_string;
5934 comp_dir->set_string_noncanonical (stub_comp_dir);
5935 }
5936
5937 /* Set up for reading the DWO CU/TU. */
5938 cu->dwo_unit = dwo_unit;
5939 dwarf2_section_info *section = dwo_unit->section;
5940 section->read (objfile);
5941 abfd = section->get_bfd_owner ();
5942 begin_info_ptr = info_ptr = (section->buffer
5943 + to_underlying (dwo_unit->sect_off));
5944 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5945
5946 if (per_cu->is_debug_types)
5947 {
5948 signatured_type *sig_type = (struct signatured_type *) per_cu;
5949
5950 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5951 section, dwo_abbrev_section,
5952 info_ptr, rcuh_kind::TYPE);
5953 /* This is not an assert because it can be caused by bad debug info. */
5954 if (sig_type->signature != cu->header.signature)
5955 {
5956 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5957 " TU at offset %s [in module %s]"),
5958 hex_string (sig_type->signature),
5959 hex_string (cu->header.signature),
5960 sect_offset_str (dwo_unit->sect_off),
5961 bfd_get_filename (abfd));
5962 }
5963 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5964 /* For DWOs coming from DWP files, we don't know the CU length
5965 nor the type's offset in the TU until now. */
5966 dwo_unit->length = cu->header.get_length ();
5967 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5968
5969 /* Establish the type offset that can be used to lookup the type.
5970 For DWO files, we don't know it until now. */
5971 sig_type->type_offset_in_section
5972 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
5973 }
5974 else
5975 {
5976 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5977 section, dwo_abbrev_section,
5978 info_ptr, rcuh_kind::COMPILE);
5979 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5980 /* For DWOs coming from DWP files, we don't know the CU length
5981 until now. */
5982 dwo_unit->length = cu->header.get_length ();
5983 }
5984
5985 dwo_abbrev_section->read (objfile);
5986 *result_dwo_abbrev_table
5987 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
5988 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
5989 result_dwo_abbrev_table->get ());
5990
5991 /* Read in the die, but leave space to copy over the attributes
5992 from the stub. This has the benefit of simplifying the rest of
5993 the code - all the work to maintain the illusion of a single
5994 DW_TAG_{compile,type}_unit DIE is done here. */
5995 num_extra_attrs = ((stmt_list != NULL)
5996 + (low_pc != NULL)
5997 + (high_pc != NULL)
5998 + (ranges != NULL)
5999 + (comp_dir != NULL));
6000 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6001 num_extra_attrs);
6002
6003 /* Copy over the attributes from the stub to the DIE we just read in. */
6004 comp_unit_die = *result_comp_unit_die;
6005 i = comp_unit_die->num_attrs;
6006 if (stmt_list != NULL)
6007 comp_unit_die->attrs[i++] = *stmt_list;
6008 if (low_pc != NULL)
6009 comp_unit_die->attrs[i++] = *low_pc;
6010 if (high_pc != NULL)
6011 comp_unit_die->attrs[i++] = *high_pc;
6012 if (ranges != NULL)
6013 comp_unit_die->attrs[i++] = *ranges;
6014 if (comp_dir != NULL)
6015 comp_unit_die->attrs[i++] = *comp_dir;
6016 comp_unit_die->num_attrs += num_extra_attrs;
6017
6018 if (dwarf_die_debug)
6019 {
6020 gdb_printf (gdb_stdlog,
6021 "Read die from %s@0x%x of %s:\n",
6022 section->get_name (),
6023 (unsigned) (begin_info_ptr - section->buffer),
6024 bfd_get_filename (abfd));
6025 dump_die (comp_unit_die, dwarf_die_debug);
6026 }
6027
6028 /* Skip dummy compilation units. */
6029 if (info_ptr >= begin_info_ptr + dwo_unit->length
6030 || peek_abbrev_code (abfd, info_ptr) == 0)
6031 return 0;
6032
6033 *result_info_ptr = info_ptr;
6034 return 1;
6035 }
6036
6037 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6038 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6039 signature is part of the header. */
6040 static gdb::optional<ULONGEST>
6041 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6042 {
6043 if (cu->header.version >= 5)
6044 return cu->header.signature;
6045 struct attribute *attr;
6046 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6047 if (attr == nullptr || !attr->form_is_unsigned ())
6048 return gdb::optional<ULONGEST> ();
6049 return attr->as_unsigned ();
6050 }
6051
6052 /* Subroutine of cutu_reader to simplify it.
6053 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6054 Returns NULL if the specified DWO unit cannot be found. */
6055
6056 static struct dwo_unit *
6057 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6058 {
6059 #if CXX_STD_THREAD
6060 /* We need a lock here both to handle the DWO hash table, and BFD,
6061 which is not thread-safe. */
6062 static std::mutex dwo_lock;
6063
6064 std::lock_guard<std::mutex> guard (dwo_lock);
6065 #endif
6066
6067 dwarf2_per_cu_data *per_cu = cu->per_cu;
6068 struct dwo_unit *dwo_unit;
6069 const char *comp_dir;
6070
6071 gdb_assert (cu != NULL);
6072
6073 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6074 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6075 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6076
6077 if (per_cu->is_debug_types)
6078 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6079 else
6080 {
6081 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6082
6083 if (!signature.has_value ())
6084 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6085 " [in module %s]"),
6086 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6087
6088 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6089 }
6090
6091 return dwo_unit;
6092 }
6093
6094 /* Subroutine of cutu_reader to simplify it.
6095 See it for a description of the parameters.
6096 Read a TU directly from a DWO file, bypassing the stub. */
6097
6098 void
6099 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6100 dwarf2_per_objfile *per_objfile,
6101 dwarf2_cu *existing_cu)
6102 {
6103 struct signatured_type *sig_type;
6104
6105 /* Verify we can do the following downcast, and that we have the
6106 data we need. */
6107 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6108 sig_type = (struct signatured_type *) this_cu;
6109 gdb_assert (sig_type->dwo_unit != NULL);
6110
6111 dwarf2_cu *cu;
6112
6113 if (existing_cu != nullptr)
6114 {
6115 cu = existing_cu;
6116 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6117 /* There's no need to do the rereading_dwo_cu handling that
6118 cutu_reader does since we don't read the stub. */
6119 }
6120 else
6121 {
6122 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6123 in per_objfile yet. */
6124 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6125 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6126 cu = m_new_cu.get ();
6127 }
6128
6129 /* A future optimization, if needed, would be to use an existing
6130 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6131 could share abbrev tables. */
6132
6133 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6134 NULL /* stub_comp_unit_die */,
6135 sig_type->dwo_unit->dwo_file->comp_dir,
6136 this, &info_ptr,
6137 &comp_unit_die,
6138 &m_dwo_abbrev_table) == 0)
6139 {
6140 /* Dummy die. */
6141 dummy_p = true;
6142 }
6143 }
6144
6145 /* Initialize a CU (or TU) and read its DIEs.
6146 If the CU defers to a DWO file, read the DWO file as well.
6147
6148 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6149 Otherwise the table specified in the comp unit header is read in and used.
6150 This is an optimization for when we already have the abbrev table.
6151
6152 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6153 allocated. */
6154
6155 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6156 dwarf2_per_objfile *per_objfile,
6157 struct abbrev_table *abbrev_table,
6158 dwarf2_cu *existing_cu,
6159 bool skip_partial,
6160 abbrev_cache *cache)
6161 : die_reader_specs {},
6162 m_this_cu (this_cu)
6163 {
6164 struct objfile *objfile = per_objfile->objfile;
6165 struct dwarf2_section_info *section = this_cu->section;
6166 bfd *abfd = section->get_bfd_owner ();
6167 const gdb_byte *begin_info_ptr;
6168 struct signatured_type *sig_type = NULL;
6169 struct dwarf2_section_info *abbrev_section;
6170 /* Non-zero if CU currently points to a DWO file and we need to
6171 reread it. When this happens we need to reread the skeleton die
6172 before we can reread the DWO file (this only applies to CUs, not TUs). */
6173 int rereading_dwo_cu = 0;
6174
6175 if (dwarf_die_debug)
6176 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6177 this_cu->is_debug_types ? "type" : "comp",
6178 sect_offset_str (this_cu->sect_off));
6179
6180 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6181 file (instead of going through the stub), short-circuit all of this. */
6182 if (this_cu->reading_dwo_directly)
6183 {
6184 /* Narrow down the scope of possibilities to have to understand. */
6185 gdb_assert (this_cu->is_debug_types);
6186 gdb_assert (abbrev_table == NULL);
6187 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6188 return;
6189 }
6190
6191 /* This is cheap if the section is already read in. */
6192 section->read (objfile);
6193
6194 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6195
6196 abbrev_section = get_abbrev_section_for_cu (this_cu);
6197
6198 dwarf2_cu *cu;
6199
6200 if (existing_cu != nullptr)
6201 {
6202 cu = existing_cu;
6203 /* If this CU is from a DWO file we need to start over, we need to
6204 refetch the attributes from the skeleton CU.
6205 This could be optimized by retrieving those attributes from when we
6206 were here the first time: the previous comp_unit_die was stored in
6207 comp_unit_obstack. But there's no data yet that we need this
6208 optimization. */
6209 if (cu->dwo_unit != NULL)
6210 rereading_dwo_cu = 1;
6211 }
6212 else
6213 {
6214 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6215 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6216 let us know that the CU is being scanned using the parallel
6217 indexer. This assert is avoided in this case because (1) it
6218 is irrelevant, and (2) the get_cu method is not
6219 thread-safe. */
6220 gdb_assert (cache != nullptr
6221 || per_objfile->get_cu (this_cu) == nullptr);
6222 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6223 cu = m_new_cu.get ();
6224 }
6225
6226 /* Get the header. */
6227 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6228 {
6229 /* We already have the header, there's no need to read it in again. */
6230 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6231 }
6232 else
6233 {
6234 if (this_cu->is_debug_types)
6235 {
6236 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6237 section, abbrev_section,
6238 info_ptr, rcuh_kind::TYPE);
6239
6240 /* Since per_cu is the first member of struct signatured_type,
6241 we can go from a pointer to one to a pointer to the other. */
6242 sig_type = (struct signatured_type *) this_cu;
6243 gdb_assert (sig_type->signature == cu->header.signature);
6244 gdb_assert (sig_type->type_offset_in_tu
6245 == cu->header.type_cu_offset_in_tu);
6246 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6247
6248 /* LENGTH has not been set yet for type units if we're
6249 using .gdb_index. */
6250 this_cu->set_length (cu->header.get_length ());
6251
6252 /* Establish the type offset that can be used to lookup the type. */
6253 sig_type->type_offset_in_section =
6254 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6255
6256 this_cu->set_version (cu->header.version);
6257 }
6258 else
6259 {
6260 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6261 section, abbrev_section,
6262 info_ptr,
6263 rcuh_kind::COMPILE);
6264
6265 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6266 this_cu->set_length (cu->header.get_length ());
6267 this_cu->set_version (cu->header.version);
6268 }
6269 }
6270
6271 /* Skip dummy compilation units. */
6272 if (info_ptr >= begin_info_ptr + this_cu->length ()
6273 || peek_abbrev_code (abfd, info_ptr) == 0)
6274 {
6275 dummy_p = true;
6276 return;
6277 }
6278
6279 /* If we don't have them yet, read the abbrevs for this compilation unit.
6280 And if we need to read them now, make sure they're freed when we're
6281 done. */
6282 if (abbrev_table != NULL)
6283 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6284 else
6285 {
6286 if (cache != nullptr)
6287 abbrev_table = cache->find (abbrev_section,
6288 cu->header.abbrev_sect_off);
6289 if (abbrev_table == nullptr)
6290 {
6291 abbrev_section->read (objfile);
6292 m_abbrev_table_holder
6293 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6294 abbrev_table = m_abbrev_table_holder.get ();
6295 }
6296 }
6297
6298 /* Read the top level CU/TU die. */
6299 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6300 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6301
6302 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6303 {
6304 dummy_p = true;
6305 return;
6306 }
6307
6308 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6309 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6310 table from the DWO file and pass the ownership over to us. It will be
6311 referenced from READER, so we must make sure to free it after we're done
6312 with READER.
6313
6314 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6315 DWO CU, that this test will fail (the attribute will not be present). */
6316 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6317 if (dwo_name != nullptr)
6318 {
6319 struct dwo_unit *dwo_unit;
6320 struct die_info *dwo_comp_unit_die;
6321
6322 if (comp_unit_die->has_children)
6323 {
6324 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6325 " has children (offset %s) [in module %s]"),
6326 sect_offset_str (this_cu->sect_off),
6327 bfd_get_filename (abfd));
6328 }
6329 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6330 if (dwo_unit != NULL)
6331 {
6332 if (read_cutu_die_from_dwo (cu, dwo_unit,
6333 comp_unit_die, NULL,
6334 this, &info_ptr,
6335 &dwo_comp_unit_die,
6336 &m_dwo_abbrev_table) == 0)
6337 {
6338 /* Dummy die. */
6339 dummy_p = true;
6340 return;
6341 }
6342 comp_unit_die = dwo_comp_unit_die;
6343 }
6344 else
6345 {
6346 /* Yikes, we couldn't find the rest of the DIE, we only have
6347 the stub. A complaint has already been logged. There's
6348 not much more we can do except pass on the stub DIE to
6349 die_reader_func. We don't want to throw an error on bad
6350 debug info. */
6351 }
6352 }
6353 }
6354
6355 void
6356 cutu_reader::keep ()
6357 {
6358 /* Done, clean up. */
6359 gdb_assert (!dummy_p);
6360 if (m_new_cu != NULL)
6361 {
6362 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6363 now. */
6364 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6365 per_objfile->set_cu (m_this_cu, std::move (m_new_cu));
6366 }
6367 }
6368
6369 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6370 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6371 assumed to have already done the lookup to find the DWO file).
6372
6373 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6374 THIS_CU->is_debug_types, but nothing else.
6375
6376 We fill in THIS_CU->length.
6377
6378 THIS_CU->cu is always freed when done.
6379 This is done in order to not leave THIS_CU->cu in a state where we have
6380 to care whether it refers to the "main" CU or the DWO CU.
6381
6382 When parent_cu is passed, it is used to provide a default value for
6383 str_offsets_base and addr_base from the parent. */
6384
6385 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6386 dwarf2_per_objfile *per_objfile,
6387 struct dwarf2_cu *parent_cu,
6388 struct dwo_file *dwo_file)
6389 : die_reader_specs {},
6390 m_this_cu (this_cu)
6391 {
6392 struct objfile *objfile = per_objfile->objfile;
6393 struct dwarf2_section_info *section = this_cu->section;
6394 bfd *abfd = section->get_bfd_owner ();
6395 struct dwarf2_section_info *abbrev_section;
6396 const gdb_byte *begin_info_ptr, *info_ptr;
6397
6398 if (dwarf_die_debug)
6399 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6400 this_cu->is_debug_types ? "type" : "comp",
6401 sect_offset_str (this_cu->sect_off));
6402
6403 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6404
6405 abbrev_section = (dwo_file != NULL
6406 ? &dwo_file->sections.abbrev
6407 : get_abbrev_section_for_cu (this_cu));
6408
6409 /* This is cheap if the section is already read in. */
6410 section->read (objfile);
6411
6412 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6413
6414 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6415 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6416 section, abbrev_section, info_ptr,
6417 (this_cu->is_debug_types
6418 ? rcuh_kind::TYPE
6419 : rcuh_kind::COMPILE));
6420
6421 if (parent_cu != nullptr)
6422 {
6423 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6424 m_new_cu->addr_base = parent_cu->addr_base;
6425 }
6426 this_cu->set_length (m_new_cu->header.get_length ());
6427
6428 /* Skip dummy compilation units. */
6429 if (info_ptr >= begin_info_ptr + this_cu->length ()
6430 || peek_abbrev_code (abfd, info_ptr) == 0)
6431 {
6432 dummy_p = true;
6433 return;
6434 }
6435
6436 abbrev_section->read (objfile);
6437 m_abbrev_table_holder
6438 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6439
6440 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6441 m_abbrev_table_holder.get ());
6442 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6443 }
6444
6445 \f
6446 /* Type Unit Groups.
6447
6448 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6449 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6450 so that all types coming from the same compilation (.o file) are grouped
6451 together. A future step could be to put the types in the same symtab as
6452 the CU the types ultimately came from. */
6453
6454 static hashval_t
6455 hash_type_unit_group (const void *item)
6456 {
6457 const struct type_unit_group *tu_group
6458 = (const struct type_unit_group *) item;
6459
6460 return hash_stmt_list_entry (&tu_group->hash);
6461 }
6462
6463 static int
6464 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6465 {
6466 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6467 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6468
6469 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6470 }
6471
6472 /* Allocate a hash table for type unit groups. */
6473
6474 static htab_up
6475 allocate_type_unit_groups_table ()
6476 {
6477 return htab_up (htab_create_alloc (3,
6478 hash_type_unit_group,
6479 eq_type_unit_group,
6480 htab_delete_entry<type_unit_group>,
6481 xcalloc, xfree));
6482 }
6483
6484 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6485 partial symtabs. We combine several TUs per psymtab to not let the size
6486 of any one psymtab grow too big. */
6487 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6488 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6489
6490 /* Helper routine for get_type_unit_group.
6491 Create the type_unit_group object used to hold one or more TUs. */
6492
6493 static std::unique_ptr<type_unit_group>
6494 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6495 {
6496 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6497
6498 tu_group->hash.dwo_unit = cu->dwo_unit;
6499 tu_group->hash.line_sect_off = line_offset_struct;
6500
6501 return tu_group;
6502 }
6503
6504 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6505 STMT_LIST is a DW_AT_stmt_list attribute. */
6506
6507 static struct type_unit_group *
6508 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6509 {
6510 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6511 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6512 struct type_unit_group *tu_group;
6513 void **slot;
6514 unsigned int line_offset;
6515 struct type_unit_group type_unit_group_for_lookup;
6516
6517 if (per_objfile->per_bfd->type_unit_groups == NULL)
6518 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6519
6520 /* Do we need to create a new group, or can we use an existing one? */
6521
6522 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6523 {
6524 line_offset = stmt_list->as_unsigned ();
6525 ++tu_stats->nr_symtab_sharers;
6526 }
6527 else
6528 {
6529 /* Ugh, no stmt_list. Rare, but we have to handle it.
6530 We can do various things here like create one group per TU or
6531 spread them over multiple groups to split up the expansion work.
6532 To avoid worst case scenarios (too many groups or too large groups)
6533 we, umm, group them in bunches. */
6534 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6535 | (tu_stats->nr_stmt_less_type_units
6536 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6537 ++tu_stats->nr_stmt_less_type_units;
6538 }
6539
6540 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6541 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6542 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6543 &type_unit_group_for_lookup, INSERT);
6544 if (*slot == nullptr)
6545 {
6546 sect_offset line_offset_struct = (sect_offset) line_offset;
6547 std::unique_ptr<type_unit_group> grp
6548 = create_type_unit_group (cu, line_offset_struct);
6549 *slot = grp.release ();
6550 ++tu_stats->nr_symtabs;
6551 }
6552
6553 tu_group = (struct type_unit_group *) *slot;
6554 gdb_assert (tu_group != nullptr);
6555 return tu_group;
6556 }
6557 \f
6558
6559 /* An instance of this is created when scanning DWARF to create a
6560 cooked index. */
6561
6562 class cooked_index_storage
6563 {
6564 public:
6565
6566 cooked_index_storage ()
6567 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
6568 eq_cutu_reader,
6569 htab_delete_entry<cutu_reader>,
6570 xcalloc, xfree)),
6571 m_index (new cooked_index)
6572 {
6573 }
6574
6575 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
6576
6577 /* Return the current abbrev cache. */
6578 abbrev_cache *get_abbrev_cache ()
6579 {
6580 return &m_abbrev_cache;
6581 }
6582
6583 /* Return the DIE reader corresponding to PER_CU. If no such reader
6584 has been registered, return NULL. */
6585 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
6586 {
6587 int index = per_cu->index;
6588 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
6589 &index, index);
6590 }
6591
6592 /* Preserve READER by storing it in the local hash table. */
6593 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
6594 {
6595 m_abbrev_cache.add (reader->release_abbrev_table ());
6596
6597 int index = reader->cu->per_cu->index;
6598 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
6599 index, INSERT);
6600 gdb_assert (*slot == nullptr);
6601 cutu_reader *result = reader.get ();
6602 *slot = reader.release ();
6603 return result;
6604 }
6605
6606 /* Add an entry to the index. The arguments describe the entry; see
6607 cooked-index.h. The new entry is returned. */
6608 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
6609 cooked_index_flag flags,
6610 const char *name,
6611 const cooked_index_entry *parent_entry,
6612 dwarf2_per_cu_data *per_cu)
6613 {
6614 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
6615 }
6616
6617 /* Install the current addrmap into the index being constructed,
6618 then transfer ownership of the index to the caller. */
6619 std::unique_ptr<cooked_index> release ()
6620 {
6621 m_index->install_addrmap (&m_addrmap);
6622 return std::move (m_index);
6623 }
6624
6625 /* Return the mutable addrmap that is currently being created. */
6626 addrmap_mutable *get_addrmap ()
6627 {
6628 return &m_addrmap;
6629 }
6630
6631 private:
6632
6633 /* Hash function for a cutu_reader. */
6634 static hashval_t hash_cutu_reader (const void *a)
6635 {
6636 const cutu_reader *reader = (const cutu_reader *) a;
6637 return reader->cu->per_cu->index;
6638 }
6639
6640 /* Equality function for cutu_reader. */
6641 static int eq_cutu_reader (const void *a, const void *b)
6642 {
6643 const cutu_reader *ra = (const cutu_reader *) a;
6644 const int *rb = (const int *) b;
6645 return ra->cu->per_cu->index == *rb;
6646 }
6647
6648 /* The abbrev cache used by this indexer. */
6649 abbrev_cache m_abbrev_cache;
6650 /* A hash table of cutu_reader objects. */
6651 htab_up m_reader_hash;
6652 /* The index that is being constructed. */
6653 std::unique_ptr<cooked_index> m_index;
6654
6655 /* A writeable addrmap being constructed by this scanner. */
6656 addrmap_mutable m_addrmap;
6657 };
6658
6659 /* An instance of this is created to index a CU. */
6660
6661 class cooked_indexer
6662 {
6663 public:
6664
6665 cooked_indexer (cooked_index_storage *storage,
6666 dwarf2_per_cu_data *per_cu,
6667 enum language language)
6668 : m_index_storage (storage),
6669 m_per_cu (per_cu),
6670 m_language (language)
6671 {
6672 }
6673
6674 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
6675
6676 /* Index the given CU. */
6677 void make_index (cutu_reader *reader);
6678
6679 private:
6680
6681 /* A helper function to turn a section offset into an address that
6682 can be used in an addrmap. */
6683 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
6684 {
6685 CORE_ADDR value = to_underlying (offset);
6686 if (is_dwz)
6687 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
6688 return value;
6689 }
6690
6691 /* A helper function to scan the PC bounds of READER and record them
6692 in the storage's addrmap. */
6693 void check_bounds (cutu_reader *reader);
6694
6695 /* Ensure that the indicated CU exists. The cutu_reader for it is
6696 returned. FOR_SCANNING is true if the caller intends to scan all
6697 the DIEs in the CU; when false, this use is assumed to be to look
6698 up just a single DIE. */
6699 cutu_reader *ensure_cu_exists (cutu_reader *reader,
6700 dwarf2_per_objfile *per_objfile,
6701 sect_offset sect_off,
6702 bool is_dwz,
6703 bool for_scanning);
6704
6705 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6706 the entry for the enclosing scope (nullptr at top level). FULLY
6707 is true when a full scan must be done -- in some languages,
6708 function scopes must be fully explored in order to find nested
6709 functions. This returns a pointer to just after the spot where
6710 reading stopped. */
6711 const gdb_byte *index_dies (cutu_reader *reader,
6712 const gdb_byte *info_ptr,
6713 const cooked_index_entry *parent_entry,
6714 bool fully);
6715
6716 /* Scan the attributes for a given DIE and update the out
6717 parameters. Returns a pointer to the byte after the DIE. */
6718 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
6719 cutu_reader *reader,
6720 const gdb_byte *watermark_ptr,
6721 const gdb_byte *info_ptr,
6722 const abbrev_info *abbrev,
6723 const char **name,
6724 const char **linkage_name,
6725 cooked_index_flag *flags,
6726 sect_offset *sibling_offset,
6727 const cooked_index_entry **parent_entry,
6728 CORE_ADDR *maybe_defer,
6729 bool for_specification);
6730
6731 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6732 DW_AT_import, and then scanning the referenced CU. Returns a
6733 pointer to the byte after the DIE. */
6734 const gdb_byte *index_imported_unit (cutu_reader *reader,
6735 const gdb_byte *info_ptr,
6736 const abbrev_info *abbrev);
6737
6738 /* Recursively read DIEs, recording the section offsets in
6739 m_die_range_map and then calling index_dies. */
6740 const gdb_byte *recurse (cutu_reader *reader,
6741 const gdb_byte *info_ptr,
6742 const cooked_index_entry *parent_entry,
6743 bool fully);
6744
6745 /* The storage object, where the results are kept. */
6746 cooked_index_storage *m_index_storage;
6747 /* The CU that we are reading on behalf of. This object might be
6748 asked to index one CU but to treat the results as if they come
6749 from some including CU; in this case the including CU would be
6750 recorded here. */
6751 dwarf2_per_cu_data *m_per_cu;
6752 /* The language that we're assuming when reading. */
6753 enum language m_language;
6754
6755 /* An addrmap that maps from section offsets (see the form_addr
6756 method) to newly-created entries. See m_deferred_entries to
6757 understand this. */
6758 addrmap_mutable m_die_range_map;
6759
6760 /* A single deferred entry. */
6761 struct deferred_entry
6762 {
6763 sect_offset die_offset;
6764 const char *name;
6765 CORE_ADDR spec_offset;
6766 dwarf_tag tag;
6767 cooked_index_flag flags;
6768 };
6769
6770 /* The generated DWARF can sometimes have the declaration for a
6771 method in a class (or perhaps namespace) scope, with the
6772 definition appearing outside this scope... just one of the many
6773 bad things about DWARF. In order to handle this situation, we
6774 defer certain entries until the end of scanning, at which point
6775 we'll know the containing context of all the DIEs that we might
6776 have scanned. This vector stores these deferred entries. */
6777 std::vector<deferred_entry> m_deferred_entries;
6778 };
6779
6780 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6781 Process compilation unit THIS_CU for a psymtab. */
6782
6783 static void
6784 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
6785 dwarf2_per_objfile *per_objfile,
6786 cooked_index_storage *storage)
6787 {
6788 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
6789 storage->get_abbrev_cache ());
6790
6791 if (reader.comp_unit_die == nullptr)
6792 return;
6793
6794 if (reader.dummy_p)
6795 {
6796 /* Nothing. */
6797 }
6798 else if (this_cu->is_debug_types)
6799 build_type_psymtabs_reader (&reader, storage);
6800 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
6801 {
6802 bool nope = false;
6803 if (this_cu->scanned.compare_exchange_strong (nope, true))
6804 {
6805 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
6806 language_minimal);
6807 gdb_assert (storage != nullptr);
6808 cooked_indexer indexer (storage, this_cu, reader.cu->lang ());
6809 indexer.make_index (&reader);
6810 }
6811 }
6812 }
6813
6814 /* Reader function for build_type_psymtabs. */
6815
6816 static void
6817 build_type_psymtabs_reader (cutu_reader *reader,
6818 cooked_index_storage *storage)
6819 {
6820 struct dwarf2_cu *cu = reader->cu;
6821 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6822 struct die_info *type_unit_die = reader->comp_unit_die;
6823
6824 gdb_assert (per_cu->is_debug_types);
6825
6826 if (! type_unit_die->has_children)
6827 return;
6828
6829 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6830
6831 gdb_assert (storage != nullptr);
6832 cooked_indexer indexer (storage, per_cu, cu->lang ());
6833 indexer.make_index (reader);
6834 }
6835
6836 /* Struct used to sort TUs by their abbreviation table offset. */
6837
6838 struct tu_abbrev_offset
6839 {
6840 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
6841 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
6842 {}
6843
6844 /* This is used when sorting. */
6845 bool operator< (const tu_abbrev_offset &other) const
6846 {
6847 return abbrev_offset < other.abbrev_offset;
6848 }
6849
6850 signatured_type *sig_type;
6851 sect_offset abbrev_offset;
6852 };
6853
6854 /* Efficiently read all the type units.
6855
6856 The efficiency is because we sort TUs by the abbrev table they use and
6857 only read each abbrev table once. In one program there are 200K TUs
6858 sharing 8K abbrev tables.
6859
6860 The main purpose of this function is to support building the
6861 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6862 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6863 can collapse the search space by grouping them by stmt_list.
6864 The savings can be significant, in the same program from above the 200K TUs
6865 share 8K stmt_list tables.
6866
6867 FUNC is expected to call get_type_unit_group, which will create the
6868 struct type_unit_group if necessary and add it to
6869 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6870
6871 static void
6872 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
6873 cooked_index_storage *storage)
6874 {
6875 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6876 abbrev_table_up abbrev_table;
6877 sect_offset abbrev_offset;
6878
6879 /* It's up to the caller to not call us multiple times. */
6880 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
6881
6882 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
6883 return;
6884
6885 /* TUs typically share abbrev tables, and there can be way more TUs than
6886 abbrev tables. Sort by abbrev table to reduce the number of times we
6887 read each abbrev table in.
6888 Alternatives are to punt or to maintain a cache of abbrev tables.
6889 This is simpler and efficient enough for now.
6890
6891 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6892 symtab to use). Typically TUs with the same abbrev offset have the same
6893 stmt_list value too so in practice this should work well.
6894
6895 The basic algorithm here is:
6896
6897 sort TUs by abbrev table
6898 for each TU with same abbrev table:
6899 read abbrev table if first user
6900 read TU top level DIE
6901 [IWBN if DWO skeletons had DW_AT_stmt_list]
6902 call FUNC */
6903
6904 dwarf_read_debug_printf ("Building type unit groups ...");
6905
6906 /* Sort in a separate table to maintain the order of all_comp_units
6907 for .gdb_index: TU indices directly index all_type_units. */
6908 std::vector<tu_abbrev_offset> sorted_by_abbrev;
6909 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
6910
6911 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
6912 {
6913 if (cu->is_debug_types)
6914 {
6915 auto sig_type = static_cast<signatured_type *> (cu.get ());
6916 sorted_by_abbrev.emplace_back
6917 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
6918 sig_type->sect_off));
6919 }
6920 }
6921
6922 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
6923
6924 abbrev_offset = (sect_offset) ~(unsigned) 0;
6925
6926 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
6927 {
6928 /* Switch to the next abbrev table if necessary. */
6929 if (abbrev_table == NULL
6930 || tu.abbrev_offset != abbrev_offset)
6931 {
6932 abbrev_offset = tu.abbrev_offset;
6933 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
6934 abbrev_table =
6935 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
6936 ++tu_stats->nr_uniq_abbrev_tables;
6937 }
6938
6939 cutu_reader reader (tu.sig_type, per_objfile,
6940 abbrev_table.get (), nullptr, false);
6941 if (!reader.dummy_p)
6942 build_type_psymtabs_reader (&reader, storage);
6943 }
6944 }
6945
6946 /* Print collected type unit statistics. */
6947
6948 static void
6949 print_tu_stats (dwarf2_per_objfile *per_objfile)
6950 {
6951 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6952
6953 dwarf_read_debug_printf ("Type unit statistics:");
6954 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6955 dwarf_read_debug_printf (" %d uniq abbrev tables",
6956 tu_stats->nr_uniq_abbrev_tables);
6957 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
6958 tu_stats->nr_symtabs);
6959 dwarf_read_debug_printf (" %d symtab sharers",
6960 tu_stats->nr_symtab_sharers);
6961 dwarf_read_debug_printf (" %d type units without a stmt_list",
6962 tu_stats->nr_stmt_less_type_units);
6963 dwarf_read_debug_printf (" %d all_type_units reallocs",
6964 tu_stats->nr_all_type_units_reallocs);
6965 }
6966
6967 struct skeleton_data
6968 {
6969 dwarf2_per_objfile *per_objfile;
6970 cooked_index_storage *storage;
6971 };
6972
6973 /* Traversal function for process_skeletonless_type_unit.
6974 Read a TU in a DWO file and build partial symbols for it. */
6975
6976 static int
6977 process_skeletonless_type_unit (void **slot, void *info)
6978 {
6979 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6980 skeleton_data *data = (skeleton_data *) info;
6981
6982 /* If this TU doesn't exist in the global table, add it and read it in. */
6983
6984 if (data->per_objfile->per_bfd->signatured_types == NULL)
6985 data->per_objfile->per_bfd->signatured_types
6986 = allocate_signatured_type_table ();
6987
6988 signatured_type find_entry (dwo_unit->signature);
6989 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
6990 &find_entry, INSERT);
6991 /* If we've already seen this type there's nothing to do. What's happening
6992 is we're doing our own version of comdat-folding here. */
6993 if (*slot != NULL)
6994 return 1;
6995
6996 /* This does the job that create_all_comp_units would have done for
6997 this TU. */
6998 signatured_type *entry
6999 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
7000 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
7001 *slot = entry;
7002
7003 /* This does the job that build_type_psymtabs would have done. */
7004 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
7005 if (!reader.dummy_p)
7006 build_type_psymtabs_reader (&reader, data->storage);
7007
7008 return 1;
7009 }
7010
7011 /* Traversal function for process_skeletonless_type_units. */
7012
7013 static int
7014 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7015 {
7016 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7017
7018 if (dwo_file->tus != NULL)
7019 htab_traverse_noresize (dwo_file->tus.get (),
7020 process_skeletonless_type_unit, info);
7021
7022 return 1;
7023 }
7024
7025 /* Scan all TUs of DWO files, verifying we've processed them.
7026 This is needed in case a TU was emitted without its skeleton.
7027 Note: This can't be done until we know what all the DWO files are. */
7028
7029 static void
7030 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
7031 cooked_index_storage *storage)
7032 {
7033 skeleton_data data { per_objfile, storage };
7034
7035 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7036 if (get_dwp_file (per_objfile) == NULL
7037 && per_objfile->per_bfd->dwo_files != NULL)
7038 {
7039 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7040 process_dwo_file_for_skeletonless_type_units,
7041 &data);
7042 }
7043 }
7044
7045 /* Build the partial symbol table by doing a quick pass through the
7046 .debug_info and .debug_abbrev sections. */
7047
7048 static void
7049 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7050 {
7051 struct objfile *objfile = per_objfile->objfile;
7052 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7053
7054 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7055 objfile_name (objfile));
7056
7057 per_bfd->map_info_sections (objfile);
7058
7059 cooked_index_storage index_storage;
7060 create_all_comp_units (per_objfile);
7061 build_type_psymtabs (per_objfile, &index_storage);
7062 std::vector<std::unique_ptr<cooked_index>> indexes;
7063
7064 per_bfd->quick_file_names_table
7065 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
7066 if (!per_bfd->debug_aranges.empty ())
7067 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
7068 index_storage.get_addrmap ());
7069
7070 {
7071 /* Ensure that complaints are handled correctly. */
7072 complaint_interceptor complaint_handler;
7073
7074 using iter_type = decltype (per_bfd->all_comp_units.begin ());
7075
7076 /* Each thread returns a pair holding a cooked index, and a vector
7077 of errors that should be printed. The latter is done because
7078 GDB's I/O system is not thread-safe. run_on_main_thread could be
7079 used, but that would mean the messages are printed after the
7080 prompt, which looks weird. */
7081 using result_type = std::pair<std::unique_ptr<cooked_index>,
7082 std::vector<gdb_exception>>;
7083 std::vector<result_type> results
7084 = gdb::parallel_for_each (1, per_bfd->all_comp_units.begin (),
7085 per_bfd->all_comp_units.end (),
7086 [=] (iter_type iter, iter_type end)
7087 {
7088 std::vector<gdb_exception> errors;
7089 cooked_index_storage thread_storage;
7090 for (; iter != end; ++iter)
7091 {
7092 dwarf2_per_cu_data *per_cu = iter->get ();
7093 try
7094 {
7095 process_psymtab_comp_unit (per_cu, per_objfile,
7096 &thread_storage);
7097 }
7098 catch (gdb_exception &except)
7099 {
7100 errors.push_back (std::move (except));
7101 }
7102 }
7103 return result_type (thread_storage.release (), std::move (errors));
7104 });
7105
7106 /* Only show a given exception a single time. */
7107 std::unordered_set<gdb_exception> seen_exceptions;
7108 for (auto &one_result : results)
7109 {
7110 indexes.push_back (std::move (one_result.first));
7111 for (auto &one_exc : one_result.second)
7112 if (seen_exceptions.insert (one_exc).second)
7113 exception_print (gdb_stderr, one_exc);
7114 }
7115 }
7116
7117 /* This has to wait until we read the CUs, we need the list of DWOs. */
7118 process_skeletonless_type_units (per_objfile, &index_storage);
7119
7120 if (dwarf_read_debug > 0)
7121 print_tu_stats (per_objfile);
7122
7123 indexes.push_back (index_storage.release ());
7124 /* Remove any NULL entries. This might happen if parallel-for
7125 decides to throttle the number of threads that were used. */
7126 indexes.erase
7127 (std::remove_if (indexes.begin (),
7128 indexes.end (),
7129 [] (const std::unique_ptr<cooked_index> &entry)
7130 {
7131 return entry == nullptr;
7132 }),
7133 indexes.end ());
7134 indexes.shrink_to_fit ();
7135
7136 cooked_index_vector *vec = new cooked_index_vector (std::move (indexes));
7137 per_bfd->index_table.reset (vec);
7138
7139 const cooked_index_entry *main_entry = vec->get_main ();
7140 if (main_entry != nullptr)
7141 set_objfile_main_name (objfile, main_entry->name,
7142 main_entry->per_cu->lang ());
7143
7144 dwarf_read_debug_printf ("Done building psymtabs of %s",
7145 objfile_name (objfile));
7146 }
7147
7148 static void
7149 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7150 struct dwarf2_section_info *section,
7151 struct dwarf2_section_info *abbrev_section,
7152 unsigned int is_dwz,
7153 htab_up &types_htab,
7154 rcuh_kind section_kind)
7155 {
7156 const gdb_byte *info_ptr;
7157 struct objfile *objfile = per_objfile->objfile;
7158
7159 dwarf_read_debug_printf ("Reading %s for %s",
7160 section->get_name (),
7161 section->get_file_name ());
7162
7163 section->read (objfile);
7164
7165 info_ptr = section->buffer;
7166
7167 while (info_ptr < section->buffer + section->size)
7168 {
7169 dwarf2_per_cu_data_up this_cu;
7170
7171 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7172
7173 comp_unit_head cu_header;
7174 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7175 abbrev_section, info_ptr,
7176 section_kind);
7177
7178 /* Save the compilation unit for later lookup. */
7179 if (cu_header.unit_type != DW_UT_type)
7180 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7181 else
7182 {
7183 if (types_htab == nullptr)
7184 types_htab = allocate_signatured_type_table ();
7185
7186 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7187 (cu_header.signature);
7188 signatured_type *sig_ptr = sig_type.get ();
7189 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7190 this_cu.reset (sig_type.release ());
7191
7192 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7193 gdb_assert (slot != nullptr);
7194 if (*slot != nullptr)
7195 complaint (_("debug type entry at offset %s is duplicate to"
7196 " the entry at offset %s, signature %s"),
7197 sect_offset_str (sect_off),
7198 sect_offset_str (sig_ptr->sect_off),
7199 hex_string (sig_ptr->signature));
7200 *slot = sig_ptr;
7201 }
7202 this_cu->sect_off = sect_off;
7203 this_cu->set_length (cu_header.get_length ());
7204 this_cu->is_dwz = is_dwz;
7205 this_cu->section = section;
7206 /* Init this asap, to avoid a data race in the set_version in
7207 cutu_reader::cutu_reader (which may be run in parallel for the cooked
7208 index case). */
7209 this_cu->set_version (cu_header.version);
7210
7211 info_ptr = info_ptr + this_cu->length ();
7212 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7213 }
7214 }
7215
7216 /* Create a list of all compilation units in OBJFILE.
7217 This is only done for -readnow and building partial symtabs. */
7218
7219 static void
7220 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7221 {
7222 htab_up types_htab;
7223 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
7224
7225 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7226 &per_objfile->per_bfd->abbrev, 0,
7227 types_htab, rcuh_kind::COMPILE);
7228 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7229 read_comp_units_from_section (per_objfile, &section,
7230 &per_objfile->per_bfd->abbrev, 0,
7231 types_htab, rcuh_kind::TYPE);
7232
7233 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7234 if (dwz != NULL)
7235 {
7236 /* Pre-read the sections we'll need to construct an index. */
7237 struct objfile *objfile = per_objfile->objfile;
7238 dwz->abbrev.read (objfile);
7239 dwz->info.read (objfile);
7240 dwz->str.read (objfile);
7241 dwz->line.read (objfile);
7242 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7243 types_htab, rcuh_kind::COMPILE);
7244 }
7245
7246 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7247 }
7248
7249 /* Return the initial uleb128 in the die at INFO_PTR. */
7250
7251 static unsigned int
7252 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7253 {
7254 unsigned int bytes_read;
7255
7256 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7257 }
7258
7259 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7260 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7261
7262 Return the corresponding abbrev, or NULL if the number is zero (indicating
7263 an empty DIE). In either case *BYTES_READ will be set to the length of
7264 the initial number. */
7265
7266 static const struct abbrev_info *
7267 peek_die_abbrev (const die_reader_specs &reader,
7268 const gdb_byte *info_ptr, unsigned int *bytes_read)
7269 {
7270 dwarf2_cu *cu = reader.cu;
7271 bfd *abfd = reader.abfd;
7272 unsigned int abbrev_number
7273 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7274
7275 if (abbrev_number == 0)
7276 return NULL;
7277
7278 const abbrev_info *abbrev
7279 = reader.abbrev_table->lookup_abbrev (abbrev_number);
7280 if (!abbrev)
7281 {
7282 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7283 " at offset %s [in module %s]"),
7284 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7285 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
7286 }
7287
7288 return abbrev;
7289 }
7290
7291 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7292 Returns a pointer to the end of a series of DIEs, terminated by an empty
7293 DIE. Any children of the skipped DIEs will also be skipped. */
7294
7295 static const gdb_byte *
7296 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7297 {
7298 while (1)
7299 {
7300 unsigned int bytes_read;
7301 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
7302 &bytes_read);
7303
7304 if (abbrev == NULL)
7305 return info_ptr + bytes_read;
7306 else
7307 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7308 }
7309 }
7310
7311 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7312 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7313 abbrev corresponding to that skipped uleb128 should be passed in
7314 ABBREV.
7315
7316 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7317 returns a pointer to this DIE's sibling, skipping any children.
7318 Otherwise, returns a pointer to the DIE's first child. */
7319
7320 static const gdb_byte *
7321 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7322 const struct abbrev_info *abbrev, bool do_skip_children)
7323 {
7324 unsigned int bytes_read;
7325 struct attribute attr;
7326 bfd *abfd = reader->abfd;
7327 struct dwarf2_cu *cu = reader->cu;
7328 const gdb_byte *buffer = reader->buffer;
7329 const gdb_byte *buffer_end = reader->buffer_end;
7330 unsigned int form, i;
7331
7332 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
7333 {
7334 /* We only handle DW_FORM_ref4 here. */
7335 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
7336 unsigned int offset = read_4_bytes (abfd, sibling_data);
7337 const gdb_byte *sibling_ptr
7338 = buffer + to_underlying (cu->header.sect_off) + offset;
7339 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
7340 return sibling_ptr;
7341 /* Fall through to the slow way. */
7342 }
7343 else if (abbrev->size_if_constant != 0)
7344 {
7345 info_ptr += abbrev->size_if_constant;
7346 if (do_skip_children && abbrev->has_children)
7347 return skip_children (reader, info_ptr);
7348 return info_ptr;
7349 }
7350
7351 for (i = 0; i < abbrev->num_attrs; i++)
7352 {
7353 /* The only abbrev we care about is DW_AT_sibling. */
7354 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
7355 {
7356 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7357 if (attr.form == DW_FORM_ref_addr)
7358 complaint (_("ignoring absolute DW_AT_sibling"));
7359 else
7360 {
7361 sect_offset off = attr.get_ref_die_offset ();
7362 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7363
7364 if (sibling_ptr < info_ptr)
7365 complaint (_("DW_AT_sibling points backwards"));
7366 else if (sibling_ptr > reader->buffer_end)
7367 reader->die_section->overflow_complaint ();
7368 else
7369 return sibling_ptr;
7370 }
7371 }
7372
7373 /* If it isn't DW_AT_sibling, skip this attribute. */
7374 form = abbrev->attrs[i].form;
7375 skip_attribute:
7376 switch (form)
7377 {
7378 case DW_FORM_ref_addr:
7379 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7380 and later it is offset sized. */
7381 if (cu->header.version == 2)
7382 info_ptr += cu->header.addr_size;
7383 else
7384 info_ptr += cu->header.offset_size;
7385 break;
7386 case DW_FORM_GNU_ref_alt:
7387 info_ptr += cu->header.offset_size;
7388 break;
7389 case DW_FORM_addr:
7390 info_ptr += cu->header.addr_size;
7391 break;
7392 case DW_FORM_data1:
7393 case DW_FORM_ref1:
7394 case DW_FORM_flag:
7395 case DW_FORM_strx1:
7396 info_ptr += 1;
7397 break;
7398 case DW_FORM_flag_present:
7399 case DW_FORM_implicit_const:
7400 break;
7401 case DW_FORM_data2:
7402 case DW_FORM_ref2:
7403 case DW_FORM_strx2:
7404 info_ptr += 2;
7405 break;
7406 case DW_FORM_strx3:
7407 info_ptr += 3;
7408 break;
7409 case DW_FORM_data4:
7410 case DW_FORM_ref4:
7411 case DW_FORM_strx4:
7412 info_ptr += 4;
7413 break;
7414 case DW_FORM_data8:
7415 case DW_FORM_ref8:
7416 case DW_FORM_ref_sig8:
7417 info_ptr += 8;
7418 break;
7419 case DW_FORM_data16:
7420 info_ptr += 16;
7421 break;
7422 case DW_FORM_string:
7423 read_direct_string (abfd, info_ptr, &bytes_read);
7424 info_ptr += bytes_read;
7425 break;
7426 case DW_FORM_sec_offset:
7427 case DW_FORM_strp:
7428 case DW_FORM_GNU_strp_alt:
7429 info_ptr += cu->header.offset_size;
7430 break;
7431 case DW_FORM_exprloc:
7432 case DW_FORM_block:
7433 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7434 info_ptr += bytes_read;
7435 break;
7436 case DW_FORM_block1:
7437 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7438 break;
7439 case DW_FORM_block2:
7440 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7441 break;
7442 case DW_FORM_block4:
7443 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7444 break;
7445 case DW_FORM_addrx:
7446 case DW_FORM_strx:
7447 case DW_FORM_sdata:
7448 case DW_FORM_udata:
7449 case DW_FORM_ref_udata:
7450 case DW_FORM_GNU_addr_index:
7451 case DW_FORM_GNU_str_index:
7452 case DW_FORM_rnglistx:
7453 case DW_FORM_loclistx:
7454 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7455 break;
7456 case DW_FORM_indirect:
7457 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7458 info_ptr += bytes_read;
7459 /* We need to continue parsing from here, so just go back to
7460 the top. */
7461 goto skip_attribute;
7462
7463 default:
7464 error (_("Dwarf Error: Cannot handle %s "
7465 "in DWARF reader [in module %s]"),
7466 dwarf_form_name (form),
7467 bfd_get_filename (abfd));
7468 }
7469 }
7470
7471 if (do_skip_children && abbrev->has_children)
7472 return skip_children (reader, info_ptr);
7473 else
7474 return info_ptr;
7475 }
7476 \f
7477 /* Reading in full CUs. */
7478
7479 /* Add PER_CU to the queue. */
7480
7481 static void
7482 queue_comp_unit (dwarf2_per_cu_data *per_cu,
7483 dwarf2_per_objfile *per_objfile,
7484 enum language pretend_language)
7485 {
7486 per_cu->queued = 1;
7487
7488 gdb_assert (per_objfile->queue.has_value ());
7489 per_objfile->queue->emplace (per_cu, per_objfile, pretend_language);
7490 }
7491
7492 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7493
7494 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7495 dependency.
7496
7497 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7498 DIEs, false otherwise.
7499
7500 Explanation: there is an invariant that if a CU is queued for expansion
7501 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7502 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7503 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7504 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7505 invariant is respected.
7506
7507 The caller is therefore not required to load the CU's DIEs (we return false)
7508 if:
7509
7510 - the CU is already expanded, and therefore does not get enqueued
7511 - the CU gets enqueued for expansion, but its DIEs are already loaded
7512
7513 Note that the caller should not use this function's return value as an
7514 indicator of whether the CU's DIEs are loaded right now, it should check
7515 that by calling `dwarf2_per_objfile::get_cu` instead. */
7516
7517 static int
7518 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7519 dwarf2_per_cu_data *per_cu,
7520 dwarf2_per_objfile *per_objfile,
7521 enum language pretend_language)
7522 {
7523 /* Mark the dependence relation so that we don't flush PER_CU
7524 too early. */
7525 if (dependent_cu != NULL)
7526 dependent_cu->add_dependence (per_cu);
7527
7528 /* If it's already on the queue, we have nothing to do. */
7529 if (per_cu->queued)
7530 {
7531 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7532 loaded. */
7533 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
7534
7535 /* If the CU is queued for expansion, it should not already be
7536 expanded. */
7537 gdb_assert (!per_objfile->symtab_set_p (per_cu));
7538
7539 /* The DIEs are already loaded, the caller doesn't need to do it. */
7540 return 0;
7541 }
7542
7543 bool queued = false;
7544 if (!per_objfile->symtab_set_p (per_cu))
7545 {
7546 /* Add it to the queue. */
7547 queue_comp_unit (per_cu, per_objfile, pretend_language);
7548 queued = true;
7549 }
7550
7551 /* If the compilation unit is already loaded, just mark it as
7552 used. */
7553 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7554 if (cu != nullptr)
7555 cu->last_used = 0;
7556
7557 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7558 and the DIEs are not already loaded. */
7559 return queued && cu == nullptr;
7560 }
7561
7562 /* Process the queue. */
7563
7564 static void
7565 process_queue (dwarf2_per_objfile *per_objfile)
7566 {
7567 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7568 objfile_name (per_objfile->objfile));
7569
7570 /* The queue starts out with one item, but following a DIE reference
7571 may load a new CU, adding it to the end of the queue. */
7572 while (!per_objfile->queue->empty ())
7573 {
7574 dwarf2_queue_item &item = per_objfile->queue->front ();
7575 dwarf2_per_cu_data *per_cu = item.per_cu;
7576
7577 if (!per_objfile->symtab_set_p (per_cu))
7578 {
7579 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7580
7581 /* Skip dummy CUs. */
7582 if (cu != nullptr)
7583 {
7584 unsigned int debug_print_threshold;
7585 char buf[100];
7586
7587 if (per_cu->is_debug_types)
7588 {
7589 struct signatured_type *sig_type =
7590 (struct signatured_type *) per_cu;
7591
7592 sprintf (buf, "TU %s at offset %s",
7593 hex_string (sig_type->signature),
7594 sect_offset_str (per_cu->sect_off));
7595 /* There can be 100s of TUs.
7596 Only print them in verbose mode. */
7597 debug_print_threshold = 2;
7598 }
7599 else
7600 {
7601 sprintf (buf, "CU at offset %s",
7602 sect_offset_str (per_cu->sect_off));
7603 debug_print_threshold = 1;
7604 }
7605
7606 if (dwarf_read_debug >= debug_print_threshold)
7607 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
7608
7609 if (per_cu->is_debug_types)
7610 process_full_type_unit (cu, item.pretend_language);
7611 else
7612 process_full_comp_unit (cu, item.pretend_language);
7613
7614 if (dwarf_read_debug >= debug_print_threshold)
7615 dwarf_read_debug_printf ("Done expanding %s", buf);
7616 }
7617 }
7618
7619 per_cu->queued = 0;
7620 per_objfile->queue->pop ();
7621 }
7622
7623 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7624 objfile_name (per_objfile->objfile));
7625 }
7626
7627 /* Trivial hash function for die_info: the hash value of a DIE
7628 is its offset in .debug_info for this objfile. */
7629
7630 static hashval_t
7631 die_hash (const void *item)
7632 {
7633 const struct die_info *die = (const struct die_info *) item;
7634
7635 return to_underlying (die->sect_off);
7636 }
7637
7638 /* Trivial comparison function for die_info structures: two DIEs
7639 are equal if they have the same offset. */
7640
7641 static int
7642 die_eq (const void *item_lhs, const void *item_rhs)
7643 {
7644 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7645 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7646
7647 return die_lhs->sect_off == die_rhs->sect_off;
7648 }
7649
7650 /* Load the DIEs associated with PER_CU into memory.
7651
7652 In some cases, the caller, while reading partial symbols, will need to load
7653 the full symbols for the CU for some reason. It will already have a
7654 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7655 rather than creating a new one. */
7656
7657 static void
7658 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
7659 dwarf2_per_objfile *per_objfile,
7660 dwarf2_cu *existing_cu,
7661 bool skip_partial,
7662 enum language pretend_language)
7663 {
7664 gdb_assert (! this_cu->is_debug_types);
7665
7666 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
7667 if (reader.dummy_p)
7668 return;
7669
7670 struct dwarf2_cu *cu = reader.cu;
7671 const gdb_byte *info_ptr = reader.info_ptr;
7672
7673 gdb_assert (cu->die_hash == NULL);
7674 cu->die_hash =
7675 htab_create_alloc_ex (cu->header.length / 12,
7676 die_hash,
7677 die_eq,
7678 NULL,
7679 &cu->comp_unit_obstack,
7680 hashtab_obstack_allocate,
7681 dummy_obstack_deallocate);
7682
7683 if (reader.comp_unit_die->has_children)
7684 reader.comp_unit_die->child
7685 = read_die_and_siblings (&reader, reader.info_ptr,
7686 &info_ptr, reader.comp_unit_die);
7687 cu->dies = reader.comp_unit_die;
7688 /* comp_unit_die is not stored in die_hash, no need. */
7689
7690 /* We try not to read any attributes in this function, because not
7691 all CUs needed for references have been loaded yet, and symbol
7692 table processing isn't initialized. But we have to set the CU language,
7693 or we won't be able to build types correctly.
7694 Similarly, if we do not read the producer, we can not apply
7695 producer-specific interpretation. */
7696 prepare_one_comp_unit (cu, cu->dies, pretend_language);
7697
7698 reader.keep ();
7699 }
7700
7701 /* Add a DIE to the delayed physname list. */
7702
7703 static void
7704 add_to_method_list (struct type *type, int fnfield_index, int index,
7705 const char *name, struct die_info *die,
7706 struct dwarf2_cu *cu)
7707 {
7708 struct delayed_method_info mi;
7709 mi.type = type;
7710 mi.fnfield_index = fnfield_index;
7711 mi.index = index;
7712 mi.name = name;
7713 mi.die = die;
7714 cu->method_list.push_back (mi);
7715 }
7716
7717 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7718 "const" / "volatile". If so, decrements LEN by the length of the
7719 modifier and return true. Otherwise return false. */
7720
7721 template<size_t N>
7722 static bool
7723 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
7724 {
7725 size_t mod_len = sizeof (mod) - 1;
7726 if (len > mod_len && startswith (physname + (len - mod_len), mod))
7727 {
7728 len -= mod_len;
7729 return true;
7730 }
7731 return false;
7732 }
7733
7734 /* Compute the physnames of any methods on the CU's method list.
7735
7736 The computation of method physnames is delayed in order to avoid the
7737 (bad) condition that one of the method's formal parameters is of an as yet
7738 incomplete type. */
7739
7740 static void
7741 compute_delayed_physnames (struct dwarf2_cu *cu)
7742 {
7743 /* Only C++ delays computing physnames. */
7744 if (cu->method_list.empty ())
7745 return;
7746 gdb_assert (cu->lang () == language_cplus);
7747
7748 for (const delayed_method_info &mi : cu->method_list)
7749 {
7750 const char *physname;
7751 struct fn_fieldlist *fn_flp
7752 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
7753 physname = dwarf2_physname (mi.name, mi.die, cu);
7754 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
7755 = physname ? physname : "";
7756
7757 /* Since there's no tag to indicate whether a method is a
7758 const/volatile overload, extract that information out of the
7759 demangled name. */
7760 if (physname != NULL)
7761 {
7762 size_t len = strlen (physname);
7763
7764 while (1)
7765 {
7766 if (physname[len] == ')') /* shortcut */
7767 break;
7768 else if (check_modifier (physname, len, " const"))
7769 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
7770 else if (check_modifier (physname, len, " volatile"))
7771 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
7772 else
7773 break;
7774 }
7775 }
7776 }
7777
7778 /* The list is no longer needed. */
7779 cu->method_list.clear ();
7780 }
7781
7782 /* Go objects should be embedded in a DW_TAG_module DIE,
7783 and it's not clear if/how imported objects will appear.
7784 To keep Go support simple until that's worked out,
7785 go back through what we've read and create something usable.
7786 We could do this while processing each DIE, and feels kinda cleaner,
7787 but that way is more invasive.
7788 This is to, for example, allow the user to type "p var" or "b main"
7789 without having to specify the package name, and allow lookups
7790 of module.object to work in contexts that use the expression
7791 parser. */
7792
7793 static void
7794 fixup_go_packaging (struct dwarf2_cu *cu)
7795 {
7796 gdb::unique_xmalloc_ptr<char> package_name;
7797 struct pending *list;
7798 int i;
7799
7800 for (list = *cu->get_builder ()->get_global_symbols ();
7801 list != NULL;
7802 list = list->next)
7803 {
7804 for (i = 0; i < list->nsyms; ++i)
7805 {
7806 struct symbol *sym = list->symbol[i];
7807
7808 if (sym->language () == language_go
7809 && sym->aclass () == LOC_BLOCK)
7810 {
7811 gdb::unique_xmalloc_ptr<char> this_package_name
7812 (go_symbol_package_name (sym));
7813
7814 if (this_package_name == NULL)
7815 continue;
7816 if (package_name == NULL)
7817 package_name = std::move (this_package_name);
7818 else
7819 {
7820 struct objfile *objfile = cu->per_objfile->objfile;
7821 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
7822 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7823 (sym->symtab () != NULL
7824 ? symtab_to_filename_for_display
7825 (sym->symtab ())
7826 : objfile_name (objfile)),
7827 this_package_name.get (), package_name.get ());
7828 }
7829 }
7830 }
7831 }
7832
7833 if (package_name != NULL)
7834 {
7835 struct objfile *objfile = cu->per_objfile->objfile;
7836 const char *saved_package_name = objfile->intern (package_name.get ());
7837 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7838 saved_package_name);
7839 struct symbol *sym;
7840
7841 sym = new (&objfile->objfile_obstack) symbol;
7842 sym->set_language (language_go, &objfile->objfile_obstack);
7843 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
7844 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7845 e.g., "main" finds the "main" module and not C's main(). */
7846 sym->set_domain (STRUCT_DOMAIN);
7847 sym->set_aclass_index (LOC_TYPEDEF);
7848 sym->set_type (type);
7849
7850 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
7851 }
7852 }
7853
7854 /* Allocate a fully-qualified name consisting of the two parts on the
7855 obstack. */
7856
7857 static const char *
7858 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
7859 {
7860 return obconcat (obstack, p1, "::", p2, (char *) NULL);
7861 }
7862
7863 /* A helper that allocates a variant part to attach to a Rust enum
7864 type. OBSTACK is where the results should be allocated. TYPE is
7865 the type we're processing. DISCRIMINANT_INDEX is the index of the
7866 discriminant. It must be the index of one of the fields of TYPE,
7867 or -1 to mean there is no discriminant (univariant enum).
7868 DEFAULT_INDEX is the index of the default field; or -1 if there is
7869 no default. RANGES is indexed by "effective" field number (the
7870 field index, but omitting the discriminant and default fields) and
7871 must hold the discriminant values used by the variants. Note that
7872 RANGES must have a lifetime at least as long as OBSTACK -- either
7873 already allocated on it, or static. */
7874
7875 static void
7876 alloc_rust_variant (struct obstack *obstack, struct type *type,
7877 int discriminant_index, int default_index,
7878 gdb::array_view<discriminant_range> ranges)
7879 {
7880 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7881 gdb_assert (discriminant_index == -1
7882 || (discriminant_index >= 0
7883 && discriminant_index < type->num_fields ()));
7884 gdb_assert (default_index == -1
7885 || (default_index >= 0 && default_index < type->num_fields ()));
7886
7887 /* We have one variant for each non-discriminant field. */
7888 int n_variants = type->num_fields ();
7889 if (discriminant_index != -1)
7890 --n_variants;
7891
7892 variant *variants = new (obstack) variant[n_variants];
7893 int var_idx = 0;
7894 int range_idx = 0;
7895 for (int i = 0; i < type->num_fields (); ++i)
7896 {
7897 if (i == discriminant_index)
7898 continue;
7899
7900 variants[var_idx].first_field = i;
7901 variants[var_idx].last_field = i + 1;
7902
7903 /* The default field does not need a range, but other fields do.
7904 We skipped the discriminant above. */
7905 if (i != default_index)
7906 {
7907 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
7908 ++range_idx;
7909 }
7910
7911 ++var_idx;
7912 }
7913
7914 gdb_assert (range_idx == ranges.size ());
7915 gdb_assert (var_idx == n_variants);
7916
7917 variant_part *part = new (obstack) variant_part;
7918 part->discriminant_index = discriminant_index;
7919 /* If there is no discriminant, then whether it is signed is of no
7920 consequence. */
7921 part->is_unsigned
7922 = (discriminant_index == -1
7923 ? false
7924 : type->field (discriminant_index).type ()->is_unsigned ());
7925 part->variants = gdb::array_view<variant> (variants, n_variants);
7926
7927 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
7928 gdb::array_view<variant_part> *prop_value
7929 = new (storage) gdb::array_view<variant_part> (part, 1);
7930
7931 struct dynamic_prop prop;
7932 prop.set_variant_parts (prop_value);
7933
7934 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
7935 }
7936
7937 /* Some versions of rustc emitted enums in an unusual way.
7938
7939 Ordinary enums were emitted as unions. The first element of each
7940 structure in the union was named "RUST$ENUM$DISR". This element
7941 held the discriminant.
7942
7943 These versions of Rust also implemented the "non-zero"
7944 optimization. When the enum had two values, and one is empty and
7945 the other holds a pointer that cannot be zero, the pointer is used
7946 as the discriminant, with a zero value meaning the empty variant.
7947 Here, the union's first member is of the form
7948 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
7949 where the fieldnos are the indices of the fields that should be
7950 traversed in order to find the field (which may be several fields deep)
7951 and the variantname is the name of the variant of the case when the
7952 field is zero.
7953
7954 This function recognizes whether TYPE is of one of these forms,
7955 and, if so, smashes it to be a variant type. */
7956
7957 static void
7958 quirk_rust_enum (struct type *type, struct objfile *objfile)
7959 {
7960 gdb_assert (type->code () == TYPE_CODE_UNION);
7961
7962 /* We don't need to deal with empty enums. */
7963 if (type->num_fields () == 0)
7964 return;
7965
7966 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
7967 if (type->num_fields () == 1
7968 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
7969 {
7970 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
7971
7972 /* Decode the field name to find the offset of the
7973 discriminant. */
7974 ULONGEST bit_offset = 0;
7975 struct type *field_type = type->field (0).type ();
7976 while (name[0] >= '0' && name[0] <= '9')
7977 {
7978 char *tail;
7979 unsigned long index = strtoul (name, &tail, 10);
7980 name = tail;
7981 if (*name != '$'
7982 || index >= field_type->num_fields ()
7983 || (field_type->field (index).loc_kind ()
7984 != FIELD_LOC_KIND_BITPOS))
7985 {
7986 complaint (_("Could not parse Rust enum encoding string \"%s\""
7987 "[in module %s]"),
7988 type->field (0).name (),
7989 objfile_name (objfile));
7990 return;
7991 }
7992 ++name;
7993
7994 bit_offset += field_type->field (index).loc_bitpos ();
7995 field_type = field_type->field (index).type ();
7996 }
7997
7998 /* Smash this type to be a structure type. We have to do this
7999 because the type has already been recorded. */
8000 type->set_code (TYPE_CODE_STRUCT);
8001 type->set_num_fields (3);
8002 /* Save the field we care about. */
8003 struct field saved_field = type->field (0);
8004 type->set_fields
8005 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8006
8007 /* Put the discriminant at index 0. */
8008 type->field (0).set_type (field_type);
8009 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8010 type->field (0).set_name ("<<discriminant>>");
8011 type->field (0).set_loc_bitpos (bit_offset);
8012
8013 /* The order of fields doesn't really matter, so put the real
8014 field at index 1 and the data-less field at index 2. */
8015 type->field (1) = saved_field;
8016 type->field (1).set_name
8017 (rust_last_path_segment (type->field (1).type ()->name ()));
8018 type->field (1).type ()->set_name
8019 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8020 type->field (1).name ()));
8021
8022 const char *dataless_name
8023 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8024 name);
8025 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8026 dataless_name);
8027 type->field (2).set_type (dataless_type);
8028 /* NAME points into the original discriminant name, which
8029 already has the correct lifetime. */
8030 type->field (2).set_name (name);
8031 type->field (2).set_loc_bitpos (0);
8032
8033 /* Indicate that this is a variant type. */
8034 static discriminant_range ranges[1] = { { 0, 0 } };
8035 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8036 }
8037 /* A union with a single anonymous field is probably an old-style
8038 univariant enum. */
8039 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
8040 {
8041 /* Smash this type to be a structure type. We have to do this
8042 because the type has already been recorded. */
8043 type->set_code (TYPE_CODE_STRUCT);
8044
8045 struct type *field_type = type->field (0).type ();
8046 const char *variant_name
8047 = rust_last_path_segment (field_type->name ());
8048 type->field (0).set_name (variant_name);
8049 field_type->set_name
8050 (rust_fully_qualify (&objfile->objfile_obstack,
8051 type->name (), variant_name));
8052
8053 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
8054 }
8055 else
8056 {
8057 struct type *disr_type = nullptr;
8058 for (int i = 0; i < type->num_fields (); ++i)
8059 {
8060 disr_type = type->field (i).type ();
8061
8062 if (disr_type->code () != TYPE_CODE_STRUCT)
8063 {
8064 /* All fields of a true enum will be structs. */
8065 return;
8066 }
8067 else if (disr_type->num_fields () == 0)
8068 {
8069 /* Could be data-less variant, so keep going. */
8070 disr_type = nullptr;
8071 }
8072 else if (strcmp (disr_type->field (0).name (),
8073 "RUST$ENUM$DISR") != 0)
8074 {
8075 /* Not a Rust enum. */
8076 return;
8077 }
8078 else
8079 {
8080 /* Found one. */
8081 break;
8082 }
8083 }
8084
8085 /* If we got here without a discriminant, then it's probably
8086 just a union. */
8087 if (disr_type == nullptr)
8088 return;
8089
8090 /* Smash this type to be a structure type. We have to do this
8091 because the type has already been recorded. */
8092 type->set_code (TYPE_CODE_STRUCT);
8093
8094 /* Make space for the discriminant field. */
8095 struct field *disr_field = &disr_type->field (0);
8096 field *new_fields
8097 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
8098 * sizeof (struct field)));
8099 memcpy (new_fields + 1, type->fields (),
8100 type->num_fields () * sizeof (struct field));
8101 type->set_fields (new_fields);
8102 type->set_num_fields (type->num_fields () + 1);
8103
8104 /* Install the discriminant at index 0 in the union. */
8105 type->field (0) = *disr_field;
8106 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8107 type->field (0).set_name ("<<discriminant>>");
8108
8109 /* We need a way to find the correct discriminant given a
8110 variant name. For convenience we build a map here. */
8111 struct type *enum_type = disr_field->type ();
8112 std::unordered_map<std::string, ULONGEST> discriminant_map;
8113 for (int i = 0; i < enum_type->num_fields (); ++i)
8114 {
8115 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
8116 {
8117 const char *name
8118 = rust_last_path_segment (enum_type->field (i).name ());
8119 discriminant_map[name] = enum_type->field (i).loc_enumval ();
8120 }
8121 }
8122
8123 int n_fields = type->num_fields ();
8124 /* We don't need a range entry for the discriminant, but we do
8125 need one for every other field, as there is no default
8126 variant. */
8127 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
8128 discriminant_range,
8129 n_fields - 1);
8130 /* Skip the discriminant here. */
8131 for (int i = 1; i < n_fields; ++i)
8132 {
8133 /* Find the final word in the name of this variant's type.
8134 That name can be used to look up the correct
8135 discriminant. */
8136 const char *variant_name
8137 = rust_last_path_segment (type->field (i).type ()->name ());
8138
8139 auto iter = discriminant_map.find (variant_name);
8140 if (iter != discriminant_map.end ())
8141 {
8142 ranges[i - 1].low = iter->second;
8143 ranges[i - 1].high = iter->second;
8144 }
8145
8146 /* In Rust, each element should have the size of the
8147 enclosing enum. */
8148 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
8149
8150 /* Remove the discriminant field, if it exists. */
8151 struct type *sub_type = type->field (i).type ();
8152 if (sub_type->num_fields () > 0)
8153 {
8154 sub_type->set_num_fields (sub_type->num_fields () - 1);
8155 sub_type->set_fields (sub_type->fields () + 1);
8156 }
8157 type->field (i).set_name (variant_name);
8158 sub_type->set_name
8159 (rust_fully_qualify (&objfile->objfile_obstack,
8160 type->name (), variant_name));
8161 }
8162
8163 /* Indicate that this is a variant type. */
8164 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
8165 gdb::array_view<discriminant_range> (ranges,
8166 n_fields - 1));
8167 }
8168 }
8169
8170 /* Rewrite some Rust unions to be structures with variants parts. */
8171
8172 static void
8173 rust_union_quirks (struct dwarf2_cu *cu)
8174 {
8175 gdb_assert (cu->lang () == language_rust);
8176 for (type *type_ : cu->rust_unions)
8177 quirk_rust_enum (type_, cu->per_objfile->objfile);
8178 /* We don't need this any more. */
8179 cu->rust_unions.clear ();
8180 }
8181
8182 /* See read.h. */
8183
8184 type_unit_group_unshareable *
8185 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
8186 {
8187 auto iter = this->m_type_units.find (tu_group);
8188 if (iter != this->m_type_units.end ())
8189 return iter->second.get ();
8190
8191 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
8192 type_unit_group_unshareable *result = uniq.get ();
8193 this->m_type_units[tu_group] = std::move (uniq);
8194 return result;
8195 }
8196
8197 struct type *
8198 dwarf2_per_objfile::get_type_for_signatured_type
8199 (signatured_type *sig_type) const
8200 {
8201 auto iter = this->m_type_map.find (sig_type);
8202 if (iter == this->m_type_map.end ())
8203 return nullptr;
8204
8205 return iter->second;
8206 }
8207
8208 void dwarf2_per_objfile::set_type_for_signatured_type
8209 (signatured_type *sig_type, struct type *type)
8210 {
8211 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
8212
8213 this->m_type_map[sig_type] = type;
8214 }
8215
8216 /* A helper function for computing the list of all symbol tables
8217 included by PER_CU. */
8218
8219 static void
8220 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
8221 htab_t all_children, htab_t all_type_symtabs,
8222 dwarf2_per_cu_data *per_cu,
8223 dwarf2_per_objfile *per_objfile,
8224 struct compunit_symtab *immediate_parent)
8225 {
8226 void **slot = htab_find_slot (all_children, per_cu, INSERT);
8227 if (*slot != NULL)
8228 {
8229 /* This inclusion and its children have been processed. */
8230 return;
8231 }
8232
8233 *slot = per_cu;
8234
8235 /* Only add a CU if it has a symbol table. */
8236 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8237 if (cust != NULL)
8238 {
8239 /* If this is a type unit only add its symbol table if we haven't
8240 seen it yet (type unit per_cu's can share symtabs). */
8241 if (per_cu->is_debug_types)
8242 {
8243 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8244 if (*slot == NULL)
8245 {
8246 *slot = cust;
8247 result->push_back (cust);
8248 if (cust->user == NULL)
8249 cust->user = immediate_parent;
8250 }
8251 }
8252 else
8253 {
8254 result->push_back (cust);
8255 if (cust->user == NULL)
8256 cust->user = immediate_parent;
8257 }
8258 }
8259
8260 if (!per_cu->imported_symtabs_empty ())
8261 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8262 {
8263 recursively_compute_inclusions (result, all_children,
8264 all_type_symtabs, ptr, per_objfile,
8265 cust);
8266 }
8267 }
8268
8269 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8270 PER_CU. */
8271
8272 static void
8273 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
8274 dwarf2_per_objfile *per_objfile)
8275 {
8276 gdb_assert (! per_cu->is_debug_types);
8277
8278 if (!per_cu->imported_symtabs_empty ())
8279 {
8280 int len;
8281 std::vector<compunit_symtab *> result_symtabs;
8282 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8283
8284 /* If we don't have a symtab, we can just skip this case. */
8285 if (cust == NULL)
8286 return;
8287
8288 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
8289 htab_eq_pointer,
8290 NULL, xcalloc, xfree));
8291 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
8292 htab_eq_pointer,
8293 NULL, xcalloc, xfree));
8294
8295 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8296 {
8297 recursively_compute_inclusions (&result_symtabs, all_children.get (),
8298 all_type_symtabs.get (), ptr,
8299 per_objfile, cust);
8300 }
8301
8302 /* Now we have a transitive closure of all the included symtabs. */
8303 len = result_symtabs.size ();
8304 cust->includes
8305 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8306 struct compunit_symtab *, len + 1);
8307 memcpy (cust->includes, result_symtabs.data (),
8308 len * sizeof (compunit_symtab *));
8309 cust->includes[len] = NULL;
8310 }
8311 }
8312
8313 /* Compute the 'includes' field for the symtabs of all the CUs we just
8314 read. */
8315
8316 static void
8317 process_cu_includes (dwarf2_per_objfile *per_objfile)
8318 {
8319 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
8320 {
8321 if (! iter->is_debug_types)
8322 compute_compunit_symtab_includes (iter, per_objfile);
8323 }
8324
8325 per_objfile->per_bfd->just_read_cus.clear ();
8326 }
8327
8328 /* Generate full symbol information for CU, whose DIEs have
8329 already been loaded into memory. */
8330
8331 static void
8332 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
8333 {
8334 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8335 struct objfile *objfile = per_objfile->objfile;
8336 struct gdbarch *gdbarch = objfile->arch ();
8337 CORE_ADDR lowpc, highpc;
8338 struct compunit_symtab *cust;
8339 CORE_ADDR baseaddr;
8340 struct block *static_block;
8341 CORE_ADDR addr;
8342
8343 baseaddr = objfile->text_section_offset ();
8344
8345 /* Clear the list here in case something was left over. */
8346 cu->method_list.clear ();
8347
8348 dwarf2_find_base_address (cu->dies, cu);
8349
8350 /* Before we start reading the top-level DIE, ensure it has a valid tag
8351 type. */
8352 switch (cu->dies->tag)
8353 {
8354 case DW_TAG_compile_unit:
8355 case DW_TAG_partial_unit:
8356 case DW_TAG_type_unit:
8357 break;
8358 default:
8359 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8360 dwarf_tag_name (cu->dies->tag),
8361 sect_offset_str (cu->per_cu->sect_off),
8362 objfile_name (per_objfile->objfile));
8363 }
8364
8365 /* Do line number decoding in read_file_scope () */
8366 process_die (cu->dies, cu);
8367
8368 /* For now fudge the Go package. */
8369 if (cu->lang () == language_go)
8370 fixup_go_packaging (cu);
8371
8372 /* Now that we have processed all the DIEs in the CU, all the types
8373 should be complete, and it should now be safe to compute all of the
8374 physnames. */
8375 compute_delayed_physnames (cu);
8376
8377 if (cu->lang () == language_rust)
8378 rust_union_quirks (cu);
8379
8380 /* Some compilers don't define a DW_AT_high_pc attribute for the
8381 compilation unit. If the DW_AT_high_pc is missing, synthesize
8382 it, by scanning the DIE's below the compilation unit. */
8383 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8384
8385 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8386 static_block
8387 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
8388
8389 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8390 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8391 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8392 addrmap to help ensure it has an accurate map of pc values belonging to
8393 this comp unit. */
8394 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8395
8396 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
8397 (static_block, SECT_OFF_TEXT (objfile), 0);
8398
8399 if (cust != NULL)
8400 {
8401 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8402
8403 /* Set symtab language to language from DW_AT_language. If the
8404 compilation is from a C file generated by language preprocessors, do
8405 not set the language if it was already deduced by start_subfile. */
8406 if (!(cu->lang () == language_c
8407 && cust->primary_filetab ()->language () != language_unknown))
8408 cust->primary_filetab ()->set_language (cu->lang ());
8409
8410 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8411 produce DW_AT_location with location lists but it can be possibly
8412 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8413 there were bugs in prologue debug info, fixed later in GCC-4.5
8414 by "unwind info for epilogues" patch (which is not directly related).
8415
8416 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8417 needed, it would be wrong due to missing DW_AT_producer there.
8418
8419 Still one can confuse GDB by using non-standard GCC compilation
8420 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8421 */
8422 if (cu->has_loclist && gcc_4_minor >= 5)
8423 cust->set_locations_valid (true);
8424
8425 if (gcc_4_minor >= 5)
8426 cust->set_epilogue_unwind_valid (true);
8427
8428 cust->set_call_site_htab (cu->call_site_htab);
8429 }
8430
8431 per_objfile->set_symtab (cu->per_cu, cust);
8432
8433 /* Push it for inclusion processing later. */
8434 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
8435
8436 /* Not needed any more. */
8437 cu->reset_builder ();
8438 }
8439
8440 /* Generate full symbol information for type unit CU, whose DIEs have
8441 already been loaded into memory. */
8442
8443 static void
8444 process_full_type_unit (dwarf2_cu *cu,
8445 enum language pretend_language)
8446 {
8447 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8448 struct objfile *objfile = per_objfile->objfile;
8449 struct compunit_symtab *cust;
8450 struct signatured_type *sig_type;
8451
8452 gdb_assert (cu->per_cu->is_debug_types);
8453 sig_type = (struct signatured_type *) cu->per_cu;
8454
8455 /* Clear the list here in case something was left over. */
8456 cu->method_list.clear ();
8457
8458 /* The symbol tables are set up in read_type_unit_scope. */
8459 process_die (cu->dies, cu);
8460
8461 /* For now fudge the Go package. */
8462 if (cu->lang () == language_go)
8463 fixup_go_packaging (cu);
8464
8465 /* Now that we have processed all the DIEs in the CU, all the types
8466 should be complete, and it should now be safe to compute all of the
8467 physnames. */
8468 compute_delayed_physnames (cu);
8469
8470 if (cu->lang () == language_rust)
8471 rust_union_quirks (cu);
8472
8473 /* TUs share symbol tables.
8474 If this is the first TU to use this symtab, complete the construction
8475 of it with end_expandable_symtab. Otherwise, complete the addition of
8476 this TU's symbols to the existing symtab. */
8477 type_unit_group_unshareable *tug_unshare =
8478 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8479 if (tug_unshare->compunit_symtab == NULL)
8480 {
8481 buildsym_compunit *builder = cu->get_builder ();
8482 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8483 tug_unshare->compunit_symtab = cust;
8484
8485 if (cust != NULL)
8486 {
8487 /* Set symtab language to language from DW_AT_language. If the
8488 compilation is from a C file generated by language preprocessors,
8489 do not set the language if it was already deduced by
8490 start_subfile. */
8491 if (!(cu->lang () == language_c
8492 && cust->primary_filetab ()->language () != language_c))
8493 cust->primary_filetab ()->set_language (cu->lang ());
8494 }
8495 }
8496 else
8497 {
8498 cu->get_builder ()->augment_type_symtab ();
8499 cust = tug_unshare->compunit_symtab;
8500 }
8501
8502 per_objfile->set_symtab (cu->per_cu, cust);
8503
8504 /* Not needed any more. */
8505 cu->reset_builder ();
8506 }
8507
8508 /* Process an imported unit DIE. */
8509
8510 static void
8511 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8512 {
8513 struct attribute *attr;
8514
8515 /* For now we don't handle imported units in type units. */
8516 if (cu->per_cu->is_debug_types)
8517 {
8518 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8519 " supported in type units [in module %s]"),
8520 objfile_name (cu->per_objfile->objfile));
8521 }
8522
8523 attr = dwarf2_attr (die, DW_AT_import, cu);
8524 if (attr != NULL)
8525 {
8526 sect_offset sect_off = attr->get_ref_die_offset ();
8527 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8528 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8529 dwarf2_per_cu_data *per_cu
8530 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
8531 per_objfile->per_bfd);
8532
8533 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8534 into another compilation unit, at root level. Regard this as a hint,
8535 and ignore it. This is a best effort, it only works if unit_type and
8536 lang are already set. */
8537 if (die->parent && die->parent->parent == NULL
8538 && per_cu->unit_type (false) == DW_UT_compile
8539 && per_cu->lang (false) == language_cplus)
8540 return;
8541
8542 /* If necessary, add it to the queue and load its DIEs. */
8543 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
8544 cu->lang ()))
8545 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
8546 false, cu->lang ());
8547
8548 cu->per_cu->imported_symtabs_push (per_cu);
8549 }
8550 }
8551
8552 /* RAII object that represents a process_die scope: i.e.,
8553 starts/finishes processing a DIE. */
8554 class process_die_scope
8555 {
8556 public:
8557 process_die_scope (die_info *die, dwarf2_cu *cu)
8558 : m_die (die), m_cu (cu)
8559 {
8560 /* We should only be processing DIEs not already in process. */
8561 gdb_assert (!m_die->in_process);
8562 m_die->in_process = true;
8563 }
8564
8565 ~process_die_scope ()
8566 {
8567 m_die->in_process = false;
8568
8569 /* If we're done processing the DIE for the CU that owns the line
8570 header, we don't need the line header anymore. */
8571 if (m_cu->line_header_die_owner == m_die)
8572 {
8573 delete m_cu->line_header;
8574 m_cu->line_header = NULL;
8575 m_cu->line_header_die_owner = NULL;
8576 }
8577 }
8578
8579 private:
8580 die_info *m_die;
8581 dwarf2_cu *m_cu;
8582 };
8583
8584 /* Process a die and its children. */
8585
8586 static void
8587 process_die (struct die_info *die, struct dwarf2_cu *cu)
8588 {
8589 process_die_scope scope (die, cu);
8590
8591 switch (die->tag)
8592 {
8593 case DW_TAG_padding:
8594 break;
8595 case DW_TAG_compile_unit:
8596 case DW_TAG_partial_unit:
8597 read_file_scope (die, cu);
8598 break;
8599 case DW_TAG_type_unit:
8600 read_type_unit_scope (die, cu);
8601 break;
8602 case DW_TAG_subprogram:
8603 /* Nested subprograms in Fortran get a prefix. */
8604 if (cu->lang () == language_fortran
8605 && die->parent != NULL
8606 && die->parent->tag == DW_TAG_subprogram)
8607 cu->processing_has_namespace_info = true;
8608 /* Fall through. */
8609 case DW_TAG_inlined_subroutine:
8610 read_func_scope (die, cu);
8611 break;
8612 case DW_TAG_lexical_block:
8613 case DW_TAG_try_block:
8614 case DW_TAG_catch_block:
8615 read_lexical_block_scope (die, cu);
8616 break;
8617 case DW_TAG_call_site:
8618 case DW_TAG_GNU_call_site:
8619 read_call_site_scope (die, cu);
8620 break;
8621 case DW_TAG_class_type:
8622 case DW_TAG_interface_type:
8623 case DW_TAG_structure_type:
8624 case DW_TAG_union_type:
8625 case DW_TAG_namelist:
8626 process_structure_scope (die, cu);
8627 break;
8628 case DW_TAG_enumeration_type:
8629 process_enumeration_scope (die, cu);
8630 break;
8631
8632 /* These dies have a type, but processing them does not create
8633 a symbol or recurse to process the children. Therefore we can
8634 read them on-demand through read_type_die. */
8635 case DW_TAG_subroutine_type:
8636 case DW_TAG_set_type:
8637 case DW_TAG_pointer_type:
8638 case DW_TAG_ptr_to_member_type:
8639 case DW_TAG_reference_type:
8640 case DW_TAG_rvalue_reference_type:
8641 case DW_TAG_string_type:
8642 break;
8643
8644 case DW_TAG_array_type:
8645 /* We only need to handle this case for Ada -- in other
8646 languages, it's normal for the compiler to emit a typedef
8647 instead. */
8648 if (cu->lang () != language_ada)
8649 break;
8650 /* FALLTHROUGH */
8651 case DW_TAG_base_type:
8652 case DW_TAG_subrange_type:
8653 case DW_TAG_generic_subrange:
8654 case DW_TAG_typedef:
8655 /* Add a typedef symbol for the type definition, if it has a
8656 DW_AT_name. */
8657 new_symbol (die, read_type_die (die, cu), cu);
8658 break;
8659 case DW_TAG_common_block:
8660 read_common_block (die, cu);
8661 break;
8662 case DW_TAG_common_inclusion:
8663 break;
8664 case DW_TAG_namespace:
8665 cu->processing_has_namespace_info = true;
8666 read_namespace (die, cu);
8667 break;
8668 case DW_TAG_module:
8669 cu->processing_has_namespace_info = true;
8670 read_module (die, cu);
8671 break;
8672 case DW_TAG_imported_declaration:
8673 cu->processing_has_namespace_info = true;
8674 if (read_alias (die, cu))
8675 break;
8676 /* The declaration is neither a global namespace nor a variable
8677 alias. */
8678 /* Fall through. */
8679 case DW_TAG_imported_module:
8680 cu->processing_has_namespace_info = true;
8681 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8682 || cu->lang () != language_fortran))
8683 complaint (_("Tag '%s' has unexpected children"),
8684 dwarf_tag_name (die->tag));
8685 read_import_statement (die, cu);
8686 break;
8687
8688 case DW_TAG_imported_unit:
8689 process_imported_unit_die (die, cu);
8690 break;
8691
8692 case DW_TAG_variable:
8693 read_variable (die, cu);
8694 break;
8695
8696 default:
8697 new_symbol (die, NULL, cu);
8698 break;
8699 }
8700 }
8701 \f
8702 /* DWARF name computation. */
8703
8704 /* A helper function for dwarf2_compute_name which determines whether DIE
8705 needs to have the name of the scope prepended to the name listed in the
8706 die. */
8707
8708 static int
8709 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8710 {
8711 struct attribute *attr;
8712
8713 switch (die->tag)
8714 {
8715 case DW_TAG_namespace:
8716 case DW_TAG_typedef:
8717 case DW_TAG_class_type:
8718 case DW_TAG_interface_type:
8719 case DW_TAG_structure_type:
8720 case DW_TAG_union_type:
8721 case DW_TAG_enumeration_type:
8722 case DW_TAG_enumerator:
8723 case DW_TAG_subprogram:
8724 case DW_TAG_inlined_subroutine:
8725 case DW_TAG_member:
8726 case DW_TAG_imported_declaration:
8727 return 1;
8728
8729 case DW_TAG_variable:
8730 case DW_TAG_constant:
8731 /* We only need to prefix "globally" visible variables. These include
8732 any variable marked with DW_AT_external or any variable that
8733 lives in a namespace. [Variables in anonymous namespaces
8734 require prefixing, but they are not DW_AT_external.] */
8735
8736 if (dwarf2_attr (die, DW_AT_specification, cu))
8737 {
8738 struct dwarf2_cu *spec_cu = cu;
8739
8740 return die_needs_namespace (die_specification (die, &spec_cu),
8741 spec_cu);
8742 }
8743
8744 attr = dwarf2_attr (die, DW_AT_external, cu);
8745 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8746 && die->parent->tag != DW_TAG_module)
8747 return 0;
8748 /* A variable in a lexical block of some kind does not need a
8749 namespace, even though in C++ such variables may be external
8750 and have a mangled name. */
8751 if (die->parent->tag == DW_TAG_lexical_block
8752 || die->parent->tag == DW_TAG_try_block
8753 || die->parent->tag == DW_TAG_catch_block
8754 || die->parent->tag == DW_TAG_subprogram)
8755 return 0;
8756 return 1;
8757
8758 default:
8759 return 0;
8760 }
8761 }
8762
8763 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8764 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8765 defined for the given DIE. */
8766
8767 static struct attribute *
8768 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8769 {
8770 struct attribute *attr;
8771
8772 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8773 if (attr == NULL)
8774 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8775
8776 return attr;
8777 }
8778
8779 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8780 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8781 defined for the given DIE. */
8782
8783 static const char *
8784 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8785 {
8786 const char *linkage_name;
8787
8788 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8789 if (linkage_name == NULL)
8790 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8791
8792 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8793 See https://github.com/rust-lang/rust/issues/32925. */
8794 if (cu->lang () == language_rust && linkage_name != NULL
8795 && strchr (linkage_name, '{') != NULL)
8796 linkage_name = NULL;
8797
8798 return linkage_name;
8799 }
8800
8801 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8802 compute the physname for the object, which include a method's:
8803 - formal parameters (C++),
8804 - receiver type (Go),
8805
8806 The term "physname" is a bit confusing.
8807 For C++, for example, it is the demangled name.
8808 For Go, for example, it's the mangled name.
8809
8810 For Ada, return the DIE's linkage name rather than the fully qualified
8811 name. PHYSNAME is ignored..
8812
8813 The result is allocated on the objfile->per_bfd's obstack and
8814 canonicalized. */
8815
8816 static const char *
8817 dwarf2_compute_name (const char *name,
8818 struct die_info *die, struct dwarf2_cu *cu,
8819 int physname)
8820 {
8821 struct objfile *objfile = cu->per_objfile->objfile;
8822
8823 if (name == NULL)
8824 name = dwarf2_name (die, cu);
8825
8826 enum language lang = cu->lang ();
8827
8828 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8829 but otherwise compute it by typename_concat inside GDB.
8830 FIXME: Actually this is not really true, or at least not always true.
8831 It's all very confusing. compute_and_set_names doesn't try to demangle
8832 Fortran names because there is no mangling standard. So new_symbol
8833 will set the demangled name to the result of dwarf2_full_name, and it is
8834 the demangled name that GDB uses if it exists. */
8835 if (lang == language_ada
8836 || (lang == language_fortran && physname))
8837 {
8838 /* For Ada unit, we prefer the linkage name over the name, as
8839 the former contains the exported name, which the user expects
8840 to be able to reference. Ideally, we want the user to be able
8841 to reference this entity using either natural or linkage name,
8842 but we haven't started looking at this enhancement yet. */
8843 const char *linkage_name = dw2_linkage_name (die, cu);
8844
8845 if (linkage_name != NULL)
8846 return linkage_name;
8847 }
8848
8849 /* These are the only languages we know how to qualify names in. */
8850 if (name != NULL
8851 && (lang == language_cplus
8852 || lang == language_fortran || lang == language_d
8853 || lang == language_rust))
8854 {
8855 if (die_needs_namespace (die, cu))
8856 {
8857 const char *prefix;
8858 const char *canonical_name = NULL;
8859
8860 string_file buf;
8861
8862 prefix = determine_prefix (die, cu);
8863 if (*prefix != '\0')
8864 {
8865 gdb::unique_xmalloc_ptr<char> prefixed_name
8866 (typename_concat (NULL, prefix, name, physname, cu));
8867
8868 buf.puts (prefixed_name.get ());
8869 }
8870 else
8871 buf.puts (name);
8872
8873 /* Template parameters may be specified in the DIE's DW_AT_name, or
8874 as children with DW_TAG_template_type_param or
8875 DW_TAG_value_type_param. If the latter, add them to the name
8876 here. If the name already has template parameters, then
8877 skip this step; some versions of GCC emit both, and
8878 it is more efficient to use the pre-computed name.
8879
8880 Something to keep in mind about this process: it is very
8881 unlikely, or in some cases downright impossible, to produce
8882 something that will match the mangled name of a function.
8883 If the definition of the function has the same debug info,
8884 we should be able to match up with it anyway. But fallbacks
8885 using the minimal symbol, for instance to find a method
8886 implemented in a stripped copy of libstdc++, will not work.
8887 If we do not have debug info for the definition, we will have to
8888 match them up some other way.
8889
8890 When we do name matching there is a related problem with function
8891 templates; two instantiated function templates are allowed to
8892 differ only by their return types, which we do not add here. */
8893
8894 if (lang == language_cplus && strchr (name, '<') == NULL)
8895 {
8896 struct attribute *attr;
8897 struct die_info *child;
8898 int first = 1;
8899
8900 die->building_fullname = 1;
8901
8902 for (child = die->child; child != NULL; child = child->sibling)
8903 {
8904 struct type *type;
8905 LONGEST value;
8906 const gdb_byte *bytes;
8907 struct dwarf2_locexpr_baton *baton;
8908 struct value *v;
8909
8910 if (child->tag != DW_TAG_template_type_param
8911 && child->tag != DW_TAG_template_value_param)
8912 continue;
8913
8914 if (first)
8915 {
8916 buf.puts ("<");
8917 first = 0;
8918 }
8919 else
8920 buf.puts (", ");
8921
8922 attr = dwarf2_attr (child, DW_AT_type, cu);
8923 if (attr == NULL)
8924 {
8925 complaint (_("template parameter missing DW_AT_type"));
8926 buf.puts ("UNKNOWN_TYPE");
8927 continue;
8928 }
8929 type = die_type (child, cu);
8930
8931 if (child->tag == DW_TAG_template_type_param)
8932 {
8933 cu->language_defn->print_type (type, "", &buf, -1, 0,
8934 &type_print_raw_options);
8935 continue;
8936 }
8937
8938 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8939 if (attr == NULL)
8940 {
8941 complaint (_("template parameter missing "
8942 "DW_AT_const_value"));
8943 buf.puts ("UNKNOWN_VALUE");
8944 continue;
8945 }
8946
8947 dwarf2_const_value_attr (attr, type, name,
8948 &cu->comp_unit_obstack, cu,
8949 &value, &bytes, &baton);
8950
8951 if (type->has_no_signedness ())
8952 /* GDB prints characters as NUMBER 'CHAR'. If that's
8953 changed, this can use value_print instead. */
8954 cu->language_defn->printchar (value, type, &buf);
8955 else
8956 {
8957 struct value_print_options opts;
8958
8959 if (baton != NULL)
8960 v = dwarf2_evaluate_loc_desc (type, NULL,
8961 baton->data,
8962 baton->size,
8963 baton->per_cu,
8964 baton->per_objfile);
8965 else if (bytes != NULL)
8966 {
8967 v = allocate_value (type);
8968 memcpy (value_contents_writeable (v).data (), bytes,
8969 TYPE_LENGTH (type));
8970 }
8971 else
8972 v = value_from_longest (type, value);
8973
8974 /* Specify decimal so that we do not depend on
8975 the radix. */
8976 get_formatted_print_options (&opts, 'd');
8977 opts.raw = 1;
8978 value_print (v, &buf, &opts);
8979 release_value (v);
8980 }
8981 }
8982
8983 die->building_fullname = 0;
8984
8985 if (!first)
8986 {
8987 /* Close the argument list, with a space if necessary
8988 (nested templates). */
8989 if (!buf.empty () && buf.string ().back () == '>')
8990 buf.puts (" >");
8991 else
8992 buf.puts (">");
8993 }
8994 }
8995
8996 /* For C++ methods, append formal parameter type
8997 information, if PHYSNAME. */
8998
8999 if (physname && die->tag == DW_TAG_subprogram
9000 && lang == language_cplus)
9001 {
9002 struct type *type = read_type_die (die, cu);
9003
9004 c_type_print_args (type, &buf, 1, lang,
9005 &type_print_raw_options);
9006
9007 if (lang == language_cplus)
9008 {
9009 /* Assume that an artificial first parameter is
9010 "this", but do not crash if it is not. RealView
9011 marks unnamed (and thus unused) parameters as
9012 artificial; there is no way to differentiate
9013 the two cases. */
9014 if (type->num_fields () > 0
9015 && TYPE_FIELD_ARTIFICIAL (type, 0)
9016 && type->field (0).type ()->code () == TYPE_CODE_PTR
9017 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
9018 buf.puts (" const");
9019 }
9020 }
9021
9022 const std::string &intermediate_name = buf.string ();
9023
9024 if (lang == language_cplus)
9025 canonical_name
9026 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9027 objfile);
9028
9029 /* If we only computed INTERMEDIATE_NAME, or if
9030 INTERMEDIATE_NAME is already canonical, then we need to
9031 intern it. */
9032 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9033 name = objfile->intern (intermediate_name);
9034 else
9035 name = canonical_name;
9036 }
9037 }
9038
9039 return name;
9040 }
9041
9042 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9043 If scope qualifiers are appropriate they will be added. The result
9044 will be allocated on the storage_obstack, or NULL if the DIE does
9045 not have a name. NAME may either be from a previous call to
9046 dwarf2_name or NULL.
9047
9048 The output string will be canonicalized (if C++). */
9049
9050 static const char *
9051 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9052 {
9053 return dwarf2_compute_name (name, die, cu, 0);
9054 }
9055
9056 /* Construct a physname for the given DIE in CU. NAME may either be
9057 from a previous call to dwarf2_name or NULL. The result will be
9058 allocated on the objfile_objstack or NULL if the DIE does not have a
9059 name.
9060
9061 The output string will be canonicalized (if C++). */
9062
9063 static const char *
9064 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9065 {
9066 struct objfile *objfile = cu->per_objfile->objfile;
9067 const char *retval, *mangled = NULL, *canon = NULL;
9068 int need_copy = 1;
9069
9070 /* In this case dwarf2_compute_name is just a shortcut not building anything
9071 on its own. */
9072 if (!die_needs_namespace (die, cu))
9073 return dwarf2_compute_name (name, die, cu, 1);
9074
9075 if (cu->lang () != language_rust)
9076 mangled = dw2_linkage_name (die, cu);
9077
9078 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9079 has computed. */
9080 gdb::unique_xmalloc_ptr<char> demangled;
9081 if (mangled != NULL)
9082 {
9083 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
9084 {
9085 /* Do nothing (do not demangle the symbol name). */
9086 }
9087 else
9088 {
9089 /* Use DMGL_RET_DROP for C++ template functions to suppress
9090 their return type. It is easier for GDB users to search
9091 for such functions as `name(params)' than `long name(params)'.
9092 In such case the minimal symbol names do not match the full
9093 symbol names but for template functions there is never a need
9094 to look up their definition from their declaration so
9095 the only disadvantage remains the minimal symbol variant
9096 `long name(params)' does not have the proper inferior type. */
9097 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
9098 | DMGL_RET_DROP));
9099 }
9100 if (demangled)
9101 canon = demangled.get ();
9102 else
9103 {
9104 canon = mangled;
9105 need_copy = 0;
9106 }
9107 }
9108
9109 if (canon == NULL || check_physname)
9110 {
9111 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9112
9113 if (canon != NULL && strcmp (physname, canon) != 0)
9114 {
9115 /* It may not mean a bug in GDB. The compiler could also
9116 compute DW_AT_linkage_name incorrectly. But in such case
9117 GDB would need to be bug-to-bug compatible. */
9118
9119 complaint (_("Computed physname <%s> does not match demangled <%s> "
9120 "(from linkage <%s>) - DIE at %s [in module %s]"),
9121 physname, canon, mangled, sect_offset_str (die->sect_off),
9122 objfile_name (objfile));
9123
9124 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9125 is available here - over computed PHYSNAME. It is safer
9126 against both buggy GDB and buggy compilers. */
9127
9128 retval = canon;
9129 }
9130 else
9131 {
9132 retval = physname;
9133 need_copy = 0;
9134 }
9135 }
9136 else
9137 retval = canon;
9138
9139 if (need_copy)
9140 retval = objfile->intern (retval);
9141
9142 return retval;
9143 }
9144
9145 /* Inspect DIE in CU for a namespace alias or a variable with alias
9146 attribute. If one exists, record a new symbol for it.
9147
9148 Returns true if an alias was recorded, false otherwise. */
9149
9150 static bool
9151 read_alias (struct die_info *die, struct dwarf2_cu *cu)
9152 {
9153 struct attribute *attr;
9154
9155 /* If the die does not have a name, this is neither a namespace
9156 alias nor a variable alias. */
9157 attr = dwarf2_attr (die, DW_AT_name, cu);
9158 if (attr != NULL)
9159 {
9160 int num;
9161 struct die_info *d = die;
9162 struct dwarf2_cu *imported_cu = cu;
9163
9164 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9165 keep inspecting DIEs until we hit the underlying import. */
9166 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9167 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9168 {
9169 attr = dwarf2_attr (d, DW_AT_import, cu);
9170 if (attr == NULL)
9171 break;
9172
9173 d = follow_die_ref (d, attr, &imported_cu);
9174 if (d->tag != DW_TAG_imported_declaration)
9175 break;
9176 }
9177
9178 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9179 {
9180 complaint (_("DIE at %s has too many recursively imported "
9181 "declarations"), sect_offset_str (d->sect_off));
9182 return false;
9183 }
9184
9185 if (attr != NULL)
9186 {
9187 struct type *type;
9188 if (d->tag == DW_TAG_variable)
9189 {
9190 /* This declaration is a C/C++ global variable alias.
9191 Add a symbol for it whose type is the same as the
9192 aliased variable's. */
9193 type = die_type (d, imported_cu);
9194 struct symbol *sym = new_symbol (die, type, cu);
9195 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
9196 sym->set_aclass_index (LOC_UNRESOLVED);
9197 if (attr != nullptr)
9198 var_decode_location (attr, sym, cu);
9199 return true;
9200 }
9201 else
9202 {
9203 sect_offset sect_off = attr->get_ref_die_offset ();
9204 type = get_die_type_at_offset (sect_off, cu->per_cu,
9205 cu->per_objfile);
9206 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
9207 {
9208 /* This declaration is a global namespace alias. Add
9209 a symbol for it whose type is the aliased
9210 namespace. */
9211 new_symbol (die, type, cu);
9212 return true;
9213 }
9214 }
9215 }
9216 }
9217 return false;
9218 }
9219
9220 /* Return the using directives repository (global or local?) to use in the
9221 current context for CU.
9222
9223 For Ada, imported declarations can materialize renamings, which *may* be
9224 global. However it is impossible (for now?) in DWARF to distinguish
9225 "external" imported declarations and "static" ones. As all imported
9226 declarations seem to be static in all other languages, make them all CU-wide
9227 global only in Ada. */
9228
9229 static struct using_direct **
9230 using_directives (struct dwarf2_cu *cu)
9231 {
9232 if (cu->lang () == language_ada
9233 && cu->get_builder ()->outermost_context_p ())
9234 return cu->get_builder ()->get_global_using_directives ();
9235 else
9236 return cu->get_builder ()->get_local_using_directives ();
9237 }
9238
9239 /* Read the import statement specified by the given die and record it. */
9240
9241 static void
9242 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9243 {
9244 struct objfile *objfile = cu->per_objfile->objfile;
9245 struct attribute *import_attr;
9246 struct die_info *imported_die, *child_die;
9247 struct dwarf2_cu *imported_cu;
9248 const char *imported_name;
9249 const char *imported_name_prefix;
9250 const char *canonical_name;
9251 const char *import_alias;
9252 const char *imported_declaration = NULL;
9253 const char *import_prefix;
9254 std::vector<const char *> excludes;
9255
9256 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9257 if (import_attr == NULL)
9258 {
9259 complaint (_("Tag '%s' has no DW_AT_import"),
9260 dwarf_tag_name (die->tag));
9261 return;
9262 }
9263
9264 imported_cu = cu;
9265 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9266 imported_name = dwarf2_name (imported_die, imported_cu);
9267 if (imported_name == NULL)
9268 {
9269 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9270
9271 The import in the following code:
9272 namespace A
9273 {
9274 typedef int B;
9275 }
9276
9277 int main ()
9278 {
9279 using A::B;
9280 B b;
9281 return b;
9282 }
9283
9284 ...
9285 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9286 <52> DW_AT_decl_file : 1
9287 <53> DW_AT_decl_line : 6
9288 <54> DW_AT_import : <0x75>
9289 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9290 <59> DW_AT_name : B
9291 <5b> DW_AT_decl_file : 1
9292 <5c> DW_AT_decl_line : 2
9293 <5d> DW_AT_type : <0x6e>
9294 ...
9295 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9296 <76> DW_AT_byte_size : 4
9297 <77> DW_AT_encoding : 5 (signed)
9298
9299 imports the wrong die ( 0x75 instead of 0x58 ).
9300 This case will be ignored until the gcc bug is fixed. */
9301 return;
9302 }
9303
9304 /* Figure out the local name after import. */
9305 import_alias = dwarf2_name (die, cu);
9306
9307 /* Figure out where the statement is being imported to. */
9308 import_prefix = determine_prefix (die, cu);
9309
9310 /* Figure out what the scope of the imported die is and prepend it
9311 to the name of the imported die. */
9312 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9313
9314 if (imported_die->tag != DW_TAG_namespace
9315 && imported_die->tag != DW_TAG_module)
9316 {
9317 imported_declaration = imported_name;
9318 canonical_name = imported_name_prefix;
9319 }
9320 else if (strlen (imported_name_prefix) > 0)
9321 canonical_name = obconcat (&objfile->objfile_obstack,
9322 imported_name_prefix,
9323 (cu->lang () == language_d
9324 ? "."
9325 : "::"),
9326 imported_name, (char *) NULL);
9327 else
9328 canonical_name = imported_name;
9329
9330 if (die->tag == DW_TAG_imported_module
9331 && cu->lang () == language_fortran)
9332 for (child_die = die->child; child_die && child_die->tag;
9333 child_die = child_die->sibling)
9334 {
9335 /* DWARF-4: A Fortran use statement with a “rename list” may be
9336 represented by an imported module entry with an import attribute
9337 referring to the module and owned entries corresponding to those
9338 entities that are renamed as part of being imported. */
9339
9340 if (child_die->tag != DW_TAG_imported_declaration)
9341 {
9342 complaint (_("child DW_TAG_imported_declaration expected "
9343 "- DIE at %s [in module %s]"),
9344 sect_offset_str (child_die->sect_off),
9345 objfile_name (objfile));
9346 continue;
9347 }
9348
9349 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9350 if (import_attr == NULL)
9351 {
9352 complaint (_("Tag '%s' has no DW_AT_import"),
9353 dwarf_tag_name (child_die->tag));
9354 continue;
9355 }
9356
9357 imported_cu = cu;
9358 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9359 &imported_cu);
9360 imported_name = dwarf2_name (imported_die, imported_cu);
9361 if (imported_name == NULL)
9362 {
9363 complaint (_("child DW_TAG_imported_declaration has unknown "
9364 "imported name - DIE at %s [in module %s]"),
9365 sect_offset_str (child_die->sect_off),
9366 objfile_name (objfile));
9367 continue;
9368 }
9369
9370 excludes.push_back (imported_name);
9371
9372 process_die (child_die, cu);
9373 }
9374
9375 add_using_directive (using_directives (cu),
9376 import_prefix,
9377 canonical_name,
9378 import_alias,
9379 imported_declaration,
9380 excludes,
9381 0,
9382 &objfile->objfile_obstack);
9383 }
9384
9385 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9386 types, but gives them a size of zero. Starting with version 14,
9387 ICC is compatible with GCC. */
9388
9389 static bool
9390 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9391 {
9392 if (!cu->checked_producer)
9393 check_producer (cu);
9394
9395 return cu->producer_is_icc_lt_14;
9396 }
9397
9398 /* ICC generates a DW_AT_type for C void functions. This was observed on
9399 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9400 which says that void functions should not have a DW_AT_type. */
9401
9402 static bool
9403 producer_is_icc (struct dwarf2_cu *cu)
9404 {
9405 if (!cu->checked_producer)
9406 check_producer (cu);
9407
9408 return cu->producer_is_icc;
9409 }
9410
9411 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9412 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9413 this, it was first present in GCC release 4.3.0. */
9414
9415 static bool
9416 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9417 {
9418 if (!cu->checked_producer)
9419 check_producer (cu);
9420
9421 return cu->producer_is_gcc_lt_4_3;
9422 }
9423
9424 static file_and_directory &
9425 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9426 {
9427 if (cu->per_cu->fnd != nullptr)
9428 return *cu->per_cu->fnd;
9429
9430 /* Find the filename. Do not use dwarf2_name here, since the filename
9431 is not a source language identifier. */
9432 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
9433 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
9434
9435 if (res.get_comp_dir () == nullptr
9436 && producer_is_gcc_lt_4_3 (cu)
9437 && res.get_name () != nullptr
9438 && IS_ABSOLUTE_PATH (res.get_name ()))
9439 res.set_comp_dir (ldirname (res.get_name ()));
9440
9441 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
9442 return *cu->per_cu->fnd;
9443 }
9444
9445 /* Handle DW_AT_stmt_list for a compilation unit.
9446 DIE is the DW_TAG_compile_unit die for CU.
9447 COMP_DIR is the compilation directory. LOWPC is passed to
9448 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9449
9450 static void
9451 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9452 CORE_ADDR lowpc) /* ARI: editCase function */
9453 {
9454 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9455 struct attribute *attr;
9456 struct line_header line_header_local;
9457 hashval_t line_header_local_hash;
9458 void **slot;
9459 int decode_mapping;
9460
9461 gdb_assert (! cu->per_cu->is_debug_types);
9462
9463 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9464 if (attr == NULL || !attr->form_is_unsigned ())
9465 return;
9466
9467 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9468
9469 /* The line header hash table is only created if needed (it exists to
9470 prevent redundant reading of the line table for partial_units).
9471 If we're given a partial_unit, we'll need it. If we're given a
9472 compile_unit, then use the line header hash table if it's already
9473 created, but don't create one just yet. */
9474
9475 if (per_objfile->line_header_hash == NULL
9476 && die->tag == DW_TAG_partial_unit)
9477 {
9478 per_objfile->line_header_hash
9479 .reset (htab_create_alloc (127, line_header_hash_voidp,
9480 line_header_eq_voidp,
9481 htab_delete_entry<line_header>,
9482 xcalloc, xfree));
9483 }
9484
9485 line_header_local.sect_off = line_offset;
9486 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9487 line_header_local_hash = line_header_hash (&line_header_local);
9488 if (per_objfile->line_header_hash != NULL)
9489 {
9490 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9491 &line_header_local,
9492 line_header_local_hash, NO_INSERT);
9493
9494 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9495 is not present in *SLOT (since if there is something in *SLOT then
9496 it will be for a partial_unit). */
9497 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9498 {
9499 gdb_assert (*slot != NULL);
9500 cu->line_header = (struct line_header *) *slot;
9501 return;
9502 }
9503 }
9504
9505 /* dwarf_decode_line_header does not yet provide sufficient information.
9506 We always have to call also dwarf_decode_lines for it. */
9507 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9508 if (lh == NULL)
9509 return;
9510
9511 cu->line_header = lh.release ();
9512 cu->line_header_die_owner = die;
9513
9514 if (per_objfile->line_header_hash == NULL)
9515 slot = NULL;
9516 else
9517 {
9518 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9519 &line_header_local,
9520 line_header_local_hash, INSERT);
9521 gdb_assert (slot != NULL);
9522 }
9523 if (slot != NULL && *slot == NULL)
9524 {
9525 /* This newly decoded line number information unit will be owned
9526 by line_header_hash hash table. */
9527 *slot = cu->line_header;
9528 cu->line_header_die_owner = NULL;
9529 }
9530 else
9531 {
9532 /* We cannot free any current entry in (*slot) as that struct line_header
9533 may be already used by multiple CUs. Create only temporary decoded
9534 line_header for this CU - it may happen at most once for each line
9535 number information unit. And if we're not using line_header_hash
9536 then this is what we want as well. */
9537 gdb_assert (die->tag != DW_TAG_partial_unit);
9538 }
9539 decode_mapping = (die->tag != DW_TAG_partial_unit);
9540 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
9541 }
9542
9543 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9544
9545 static void
9546 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9547 {
9548 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9549 struct objfile *objfile = per_objfile->objfile;
9550 struct gdbarch *gdbarch = objfile->arch ();
9551 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9552 CORE_ADDR highpc = ((CORE_ADDR) 0);
9553 struct attribute *attr;
9554 struct die_info *child_die;
9555 CORE_ADDR baseaddr;
9556
9557 prepare_one_comp_unit (cu, die, cu->lang ());
9558 baseaddr = objfile->text_section_offset ();
9559
9560 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9561
9562 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9563 from finish_block. */
9564 if (lowpc == ((CORE_ADDR) -1))
9565 lowpc = highpc;
9566 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9567
9568 file_and_directory &fnd = find_file_and_directory (die, cu);
9569
9570 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
9571 lowpc);
9572
9573 gdb_assert (per_objfile->sym_cu == nullptr);
9574 scoped_restore restore_sym_cu
9575 = make_scoped_restore (&per_objfile->sym_cu, cu);
9576
9577 /* Decode line number information if present. We do this before
9578 processing child DIEs, so that the line header table is available
9579 for DW_AT_decl_file. The PC check is here because, if LOWPC and
9580 HIGHPC are both 0x0, then there won't be any interesting code in
9581 the CU, but a check later on (in
9582 lnp_state_machine::check_line_address) will fail to properly
9583 exclude an entry that was removed via --gc-sections. */
9584 if (lowpc != highpc)
9585 handle_DW_AT_stmt_list (die, cu, lowpc);
9586
9587 /* Process all dies in compilation unit. */
9588 if (die->child != NULL)
9589 {
9590 child_die = die->child;
9591 while (child_die && child_die->tag)
9592 {
9593 process_die (child_die, cu);
9594 child_die = child_die->sibling;
9595 }
9596 }
9597 per_objfile->sym_cu = nullptr;
9598
9599 /* Decode macro information, if present. Dwarf 2 macro information
9600 refers to information in the line number info statement program
9601 header, so we can only read it if we've read the header
9602 successfully. */
9603 attr = dwarf2_attr (die, DW_AT_macros, cu);
9604 if (attr == NULL)
9605 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9606 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9607 {
9608 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9609 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9610
9611 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
9612 }
9613 else
9614 {
9615 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9616 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9617 {
9618 unsigned int macro_offset = attr->as_unsigned ();
9619
9620 dwarf_decode_macros (cu, macro_offset, 0);
9621 }
9622 }
9623 }
9624
9625 void
9626 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
9627 {
9628 struct type_unit_group *tu_group;
9629 int first_time;
9630 struct attribute *attr;
9631 unsigned int i;
9632 struct signatured_type *sig_type;
9633
9634 gdb_assert (per_cu->is_debug_types);
9635 sig_type = (struct signatured_type *) per_cu;
9636
9637 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
9638
9639 /* If we're using .gdb_index (includes -readnow) then
9640 per_cu->type_unit_group may not have been set up yet. */
9641 if (sig_type->type_unit_group == NULL)
9642 sig_type->type_unit_group = get_type_unit_group (this, attr);
9643 tu_group = sig_type->type_unit_group;
9644
9645 /* If we've already processed this stmt_list there's no real need to
9646 do it again, we could fake it and just recreate the part we need
9647 (file name,index -> symtab mapping). If data shows this optimization
9648 is useful we can do it then. */
9649 type_unit_group_unshareable *tug_unshare
9650 = per_objfile->get_type_unit_group_unshareable (tu_group);
9651 first_time = tug_unshare->compunit_symtab == NULL;
9652
9653 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9654 debug info. */
9655 line_header_up lh;
9656 if (attr != NULL && attr->form_is_unsigned ())
9657 {
9658 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9659 lh = dwarf_decode_line_header (line_offset, this);
9660 }
9661 if (lh == NULL)
9662 {
9663 if (first_time)
9664 start_compunit_symtab ("", NULL, 0);
9665 else
9666 {
9667 gdb_assert (tug_unshare->symtabs == NULL);
9668 gdb_assert (m_builder == nullptr);
9669 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9670 m_builder.reset (new struct buildsym_compunit
9671 (cust->objfile (), "",
9672 cust->dirname (),
9673 compunit_language (cust),
9674 0, cust));
9675 list_in_scope = get_builder ()->get_file_symbols ();
9676 }
9677 return;
9678 }
9679
9680 line_header = lh.release ();
9681 line_header_die_owner = die;
9682
9683 if (first_time)
9684 {
9685 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
9686
9687 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9688 still initializing it, and our caller (a few levels up)
9689 process_full_type_unit still needs to know if this is the first
9690 time. */
9691
9692 tug_unshare->symtabs
9693 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
9694 struct symtab *, line_header->file_names_size ());
9695
9696 auto &file_names = line_header->file_names ();
9697 for (i = 0; i < file_names.size (); ++i)
9698 {
9699 file_entry &fe = file_names[i];
9700 dwarf2_start_subfile (this, fe.name,
9701 fe.include_dir (line_header));
9702 buildsym_compunit *b = get_builder ();
9703 if (b->get_current_subfile ()->symtab == NULL)
9704 {
9705 /* NOTE: start_subfile will recognize when it's been
9706 passed a file it has already seen. So we can't
9707 assume there's a simple mapping from
9708 cu->line_header->file_names to subfiles, plus
9709 cu->line_header->file_names may contain dups. */
9710 const char *name = b->get_current_subfile ()->name.c_str ();
9711 b->get_current_subfile ()->symtab = allocate_symtab (cust, name);
9712 }
9713
9714 fe.symtab = b->get_current_subfile ()->symtab;
9715 tug_unshare->symtabs[i] = fe.symtab;
9716 }
9717 }
9718 else
9719 {
9720 gdb_assert (m_builder == nullptr);
9721 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9722 m_builder.reset (new struct buildsym_compunit
9723 (cust->objfile (), "",
9724 cust->dirname (),
9725 compunit_language (cust),
9726 0, cust));
9727 list_in_scope = get_builder ()->get_file_symbols ();
9728
9729 auto &file_names = line_header->file_names ();
9730 for (i = 0; i < file_names.size (); ++i)
9731 {
9732 file_entry &fe = file_names[i];
9733 fe.symtab = tug_unshare->symtabs[i];
9734 }
9735 }
9736
9737 /* The main symtab is allocated last. Type units don't have DW_AT_name
9738 so they don't have a "real" (so to speak) symtab anyway.
9739 There is later code that will assign the main symtab to all symbols
9740 that don't have one. We need to handle the case of a symbol with a
9741 missing symtab (DW_AT_decl_file) anyway. */
9742 }
9743
9744 /* Process DW_TAG_type_unit.
9745 For TUs we want to skip the first top level sibling if it's not the
9746 actual type being defined by this TU. In this case the first top
9747 level sibling is there to provide context only. */
9748
9749 static void
9750 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9751 {
9752 struct die_info *child_die;
9753
9754 prepare_one_comp_unit (cu, die, language_minimal);
9755
9756 /* Initialize (or reinitialize) the machinery for building symtabs.
9757 We do this before processing child DIEs, so that the line header table
9758 is available for DW_AT_decl_file. */
9759 cu->setup_type_unit_groups (die);
9760
9761 if (die->child != NULL)
9762 {
9763 child_die = die->child;
9764 while (child_die && child_die->tag)
9765 {
9766 process_die (child_die, cu);
9767 child_die = child_die->sibling;
9768 }
9769 }
9770 }
9771 \f
9772 /* DWO/DWP files.
9773
9774 http://gcc.gnu.org/wiki/DebugFission
9775 http://gcc.gnu.org/wiki/DebugFissionDWP
9776
9777 To simplify handling of both DWO files ("object" files with the DWARF info)
9778 and DWP files (a file with the DWOs packaged up into one file), we treat
9779 DWP files as having a collection of virtual DWO files. */
9780
9781 static hashval_t
9782 hash_dwo_file (const void *item)
9783 {
9784 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9785 hashval_t hash;
9786
9787 hash = htab_hash_string (dwo_file->dwo_name);
9788 if (dwo_file->comp_dir != NULL)
9789 hash += htab_hash_string (dwo_file->comp_dir);
9790 return hash;
9791 }
9792
9793 static int
9794 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9795 {
9796 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9797 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9798
9799 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9800 return 0;
9801 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9802 return lhs->comp_dir == rhs->comp_dir;
9803 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9804 }
9805
9806 /* Allocate a hash table for DWO files. */
9807
9808 static htab_up
9809 allocate_dwo_file_hash_table ()
9810 {
9811 return htab_up (htab_create_alloc (41,
9812 hash_dwo_file,
9813 eq_dwo_file,
9814 htab_delete_entry<dwo_file>,
9815 xcalloc, xfree));
9816 }
9817
9818 /* Lookup DWO file DWO_NAME. */
9819
9820 static void **
9821 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
9822 const char *dwo_name,
9823 const char *comp_dir)
9824 {
9825 struct dwo_file find_entry;
9826 void **slot;
9827
9828 if (per_objfile->per_bfd->dwo_files == NULL)
9829 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
9830
9831 find_entry.dwo_name = dwo_name;
9832 find_entry.comp_dir = comp_dir;
9833 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
9834 INSERT);
9835
9836 return slot;
9837 }
9838
9839 static hashval_t
9840 hash_dwo_unit (const void *item)
9841 {
9842 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9843
9844 /* This drops the top 32 bits of the id, but is ok for a hash. */
9845 return dwo_unit->signature;
9846 }
9847
9848 static int
9849 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9850 {
9851 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9852 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9853
9854 /* The signature is assumed to be unique within the DWO file.
9855 So while object file CU dwo_id's always have the value zero,
9856 that's OK, assuming each object file DWO file has only one CU,
9857 and that's the rule for now. */
9858 return lhs->signature == rhs->signature;
9859 }
9860
9861 /* Allocate a hash table for DWO CUs,TUs.
9862 There is one of these tables for each of CUs,TUs for each DWO file. */
9863
9864 static htab_up
9865 allocate_dwo_unit_table ()
9866 {
9867 /* Start out with a pretty small number.
9868 Generally DWO files contain only one CU and maybe some TUs. */
9869 return htab_up (htab_create_alloc (3,
9870 hash_dwo_unit,
9871 eq_dwo_unit,
9872 NULL, xcalloc, xfree));
9873 }
9874
9875 /* die_reader_func for create_dwo_cu. */
9876
9877 static void
9878 create_dwo_cu_reader (const struct die_reader_specs *reader,
9879 const gdb_byte *info_ptr,
9880 struct die_info *comp_unit_die,
9881 struct dwo_file *dwo_file,
9882 struct dwo_unit *dwo_unit)
9883 {
9884 struct dwarf2_cu *cu = reader->cu;
9885 sect_offset sect_off = cu->per_cu->sect_off;
9886 struct dwarf2_section_info *section = cu->per_cu->section;
9887
9888 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
9889 if (!signature.has_value ())
9890 {
9891 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9892 " its dwo_id [in module %s]"),
9893 sect_offset_str (sect_off), dwo_file->dwo_name);
9894 return;
9895 }
9896
9897 dwo_unit->dwo_file = dwo_file;
9898 dwo_unit->signature = *signature;
9899 dwo_unit->section = section;
9900 dwo_unit->sect_off = sect_off;
9901 dwo_unit->length = cu->per_cu->length ();
9902
9903 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9904 sect_offset_str (sect_off),
9905 hex_string (dwo_unit->signature));
9906 }
9907
9908 /* Create the dwo_units for the CUs in a DWO_FILE.
9909 Note: This function processes DWO files only, not DWP files. */
9910
9911 static void
9912 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
9913 dwarf2_cu *cu, struct dwo_file &dwo_file,
9914 dwarf2_section_info &section, htab_up &cus_htab)
9915 {
9916 struct objfile *objfile = per_objfile->objfile;
9917 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
9918 const gdb_byte *info_ptr, *end_ptr;
9919
9920 section.read (objfile);
9921 info_ptr = section.buffer;
9922
9923 if (info_ptr == NULL)
9924 return;
9925
9926 dwarf_read_debug_printf ("Reading %s for %s:",
9927 section.get_name (),
9928 section.get_file_name ());
9929
9930 end_ptr = info_ptr + section.size;
9931 while (info_ptr < end_ptr)
9932 {
9933 struct dwarf2_per_cu_data per_cu;
9934 struct dwo_unit read_unit {};
9935 struct dwo_unit *dwo_unit;
9936 void **slot;
9937 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
9938
9939 per_cu.per_bfd = per_bfd;
9940 per_cu.is_debug_types = 0;
9941 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9942 per_cu.section = &section;
9943
9944 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
9945 if (!reader.dummy_p)
9946 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
9947 &dwo_file, &read_unit);
9948 info_ptr += per_cu.length ();
9949
9950 // If the unit could not be parsed, skip it.
9951 if (read_unit.dwo_file == NULL)
9952 continue;
9953
9954 if (cus_htab == NULL)
9955 cus_htab = allocate_dwo_unit_table ();
9956
9957 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
9958 struct dwo_unit);
9959 *dwo_unit = read_unit;
9960 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
9961 gdb_assert (slot != NULL);
9962 if (*slot != NULL)
9963 {
9964 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9965 sect_offset dup_sect_off = dup_cu->sect_off;
9966
9967 complaint (_("debug cu entry at offset %s is duplicate to"
9968 " the entry at offset %s, signature %s"),
9969 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
9970 hex_string (dwo_unit->signature));
9971 }
9972 *slot = (void *)dwo_unit;
9973 }
9974 }
9975
9976 /* DWP file .debug_{cu,tu}_index section format:
9977 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9978 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
9979
9980 DWP Versions 1 & 2 are older, pre-standard format versions. The first
9981 officially standard DWP format was published with DWARF v5 and is called
9982 Version 5. There are no versions 3 or 4.
9983
9984 DWP Version 1:
9985
9986 Both index sections have the same format, and serve to map a 64-bit
9987 signature to a set of section numbers. Each section begins with a header,
9988 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9989 indexes, and a pool of 32-bit section numbers. The index sections will be
9990 aligned at 8-byte boundaries in the file.
9991
9992 The index section header consists of:
9993
9994 V, 32 bit version number
9995 -, 32 bits unused
9996 N, 32 bit number of compilation units or type units in the index
9997 M, 32 bit number of slots in the hash table
9998
9999 Numbers are recorded using the byte order of the application binary.
10000
10001 The hash table begins at offset 16 in the section, and consists of an array
10002 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10003 order of the application binary). Unused slots in the hash table are 0.
10004 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10005
10006 The parallel table begins immediately after the hash table
10007 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10008 array of 32-bit indexes (using the byte order of the application binary),
10009 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10010 table contains a 32-bit index into the pool of section numbers. For unused
10011 hash table slots, the corresponding entry in the parallel table will be 0.
10012
10013 The pool of section numbers begins immediately following the hash table
10014 (at offset 16 + 12 * M from the beginning of the section). The pool of
10015 section numbers consists of an array of 32-bit words (using the byte order
10016 of the application binary). Each item in the array is indexed starting
10017 from 0. The hash table entry provides the index of the first section
10018 number in the set. Additional section numbers in the set follow, and the
10019 set is terminated by a 0 entry (section number 0 is not used in ELF).
10020
10021 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10022 section must be the first entry in the set, and the .debug_abbrev.dwo must
10023 be the second entry. Other members of the set may follow in any order.
10024
10025 ---
10026
10027 DWP Versions 2 and 5:
10028
10029 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10030 and the entries in the index tables are now offsets into these sections.
10031 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10032 section.
10033
10034 Index Section Contents:
10035 Header
10036 Hash Table of Signatures dwp_hash_table.hash_table
10037 Parallel Table of Indices dwp_hash_table.unit_table
10038 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10039 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10040
10041 The index section header consists of:
10042
10043 V, 32 bit version number
10044 L, 32 bit number of columns in the table of section offsets
10045 N, 32 bit number of compilation units or type units in the index
10046 M, 32 bit number of slots in the hash table
10047
10048 Numbers are recorded using the byte order of the application binary.
10049
10050 The hash table has the same format as version 1.
10051 The parallel table of indices has the same format as version 1,
10052 except that the entries are origin-1 indices into the table of sections
10053 offsets and the table of section sizes.
10054
10055 The table of offsets begins immediately following the parallel table
10056 (at offset 16 + 12 * M from the beginning of the section). The table is
10057 a two-dimensional array of 32-bit words (using the byte order of the
10058 application binary), with L columns and N+1 rows, in row-major order.
10059 Each row in the array is indexed starting from 0. The first row provides
10060 a key to the remaining rows: each column in this row provides an identifier
10061 for a debug section, and the offsets in the same column of subsequent rows
10062 refer to that section. The section identifiers for Version 2 are:
10063
10064 DW_SECT_INFO 1 .debug_info.dwo
10065 DW_SECT_TYPES 2 .debug_types.dwo
10066 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10067 DW_SECT_LINE 4 .debug_line.dwo
10068 DW_SECT_LOC 5 .debug_loc.dwo
10069 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10070 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10071 DW_SECT_MACRO 8 .debug_macro.dwo
10072
10073 The section identifiers for Version 5 are:
10074
10075 DW_SECT_INFO_V5 1 .debug_info.dwo
10076 DW_SECT_RESERVED_V5 2 --
10077 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10078 DW_SECT_LINE_V5 4 .debug_line.dwo
10079 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10080 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10081 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10082 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10083
10084 The offsets provided by the CU and TU index sections are the base offsets
10085 for the contributions made by each CU or TU to the corresponding section
10086 in the package file. Each CU and TU header contains an abbrev_offset
10087 field, used to find the abbreviations table for that CU or TU within the
10088 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10089 be interpreted as relative to the base offset given in the index section.
10090 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10091 should be interpreted as relative to the base offset for .debug_line.dwo,
10092 and offsets into other debug sections obtained from DWARF attributes should
10093 also be interpreted as relative to the corresponding base offset.
10094
10095 The table of sizes begins immediately following the table of offsets.
10096 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10097 with L columns and N rows, in row-major order. Each row in the array is
10098 indexed starting from 1 (row 0 is shared by the two tables).
10099
10100 ---
10101
10102 Hash table lookup is handled the same in version 1 and 2:
10103
10104 We assume that N and M will not exceed 2^32 - 1.
10105 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10106
10107 Given a 64-bit compilation unit signature or a type signature S, an entry
10108 in the hash table is located as follows:
10109
10110 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10111 the low-order k bits all set to 1.
10112
10113 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10114
10115 3) If the hash table entry at index H matches the signature, use that
10116 entry. If the hash table entry at index H is unused (all zeroes),
10117 terminate the search: the signature is not present in the table.
10118
10119 4) Let H = (H + H') modulo M. Repeat at Step 3.
10120
10121 Because M > N and H' and M are relatively prime, the search is guaranteed
10122 to stop at an unused slot or find the match. */
10123
10124 /* Create a hash table to map DWO IDs to their CU/TU entry in
10125 .debug_{info,types}.dwo in DWP_FILE.
10126 Returns NULL if there isn't one.
10127 Note: This function processes DWP files only, not DWO files. */
10128
10129 static struct dwp_hash_table *
10130 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
10131 struct dwp_file *dwp_file, int is_debug_types)
10132 {
10133 struct objfile *objfile = per_objfile->objfile;
10134 bfd *dbfd = dwp_file->dbfd.get ();
10135 const gdb_byte *index_ptr, *index_end;
10136 struct dwarf2_section_info *index;
10137 uint32_t version, nr_columns, nr_units, nr_slots;
10138 struct dwp_hash_table *htab;
10139
10140 if (is_debug_types)
10141 index = &dwp_file->sections.tu_index;
10142 else
10143 index = &dwp_file->sections.cu_index;
10144
10145 if (index->empty ())
10146 return NULL;
10147 index->read (objfile);
10148
10149 index_ptr = index->buffer;
10150 index_end = index_ptr + index->size;
10151
10152 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10153 For now it's safe to just read 4 bytes (particularly as it's difficult to
10154 tell if you're dealing with Version 5 before you've read the version). */
10155 version = read_4_bytes (dbfd, index_ptr);
10156 index_ptr += 4;
10157 if (version == 2 || version == 5)
10158 nr_columns = read_4_bytes (dbfd, index_ptr);
10159 else
10160 nr_columns = 0;
10161 index_ptr += 4;
10162 nr_units = read_4_bytes (dbfd, index_ptr);
10163 index_ptr += 4;
10164 nr_slots = read_4_bytes (dbfd, index_ptr);
10165 index_ptr += 4;
10166
10167 if (version != 1 && version != 2 && version != 5)
10168 {
10169 error (_("Dwarf Error: unsupported DWP file version (%s)"
10170 " [in module %s]"),
10171 pulongest (version), dwp_file->name);
10172 }
10173 if (nr_slots != (nr_slots & -nr_slots))
10174 {
10175 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10176 " is not power of 2 [in module %s]"),
10177 pulongest (nr_slots), dwp_file->name);
10178 }
10179
10180 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
10181 htab->version = version;
10182 htab->nr_columns = nr_columns;
10183 htab->nr_units = nr_units;
10184 htab->nr_slots = nr_slots;
10185 htab->hash_table = index_ptr;
10186 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10187
10188 /* Exit early if the table is empty. */
10189 if (nr_slots == 0 || nr_units == 0
10190 || (version == 2 && nr_columns == 0)
10191 || (version == 5 && nr_columns == 0))
10192 {
10193 /* All must be zero. */
10194 if (nr_slots != 0 || nr_units != 0
10195 || (version == 2 && nr_columns != 0)
10196 || (version == 5 && nr_columns != 0))
10197 {
10198 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10199 " all zero [in modules %s]"),
10200 dwp_file->name);
10201 }
10202 return htab;
10203 }
10204
10205 if (version == 1)
10206 {
10207 htab->section_pool.v1.indices =
10208 htab->unit_table + sizeof (uint32_t) * nr_slots;
10209 /* It's harder to decide whether the section is too small in v1.
10210 V1 is deprecated anyway so we punt. */
10211 }
10212 else if (version == 2)
10213 {
10214 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10215 int *ids = htab->section_pool.v2.section_ids;
10216 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
10217 /* Reverse map for error checking. */
10218 int ids_seen[DW_SECT_MAX + 1];
10219 int i;
10220
10221 if (nr_columns < 2)
10222 {
10223 error (_("Dwarf Error: bad DWP hash table, too few columns"
10224 " in section table [in module %s]"),
10225 dwp_file->name);
10226 }
10227 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10228 {
10229 error (_("Dwarf Error: bad DWP hash table, too many columns"
10230 " in section table [in module %s]"),
10231 dwp_file->name);
10232 }
10233 memset (ids, 255, sizeof_ids);
10234 memset (ids_seen, 255, sizeof (ids_seen));
10235 for (i = 0; i < nr_columns; ++i)
10236 {
10237 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10238
10239 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10240 {
10241 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10242 " in section table [in module %s]"),
10243 id, dwp_file->name);
10244 }
10245 if (ids_seen[id] != -1)
10246 {
10247 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10248 " id %d in section table [in module %s]"),
10249 id, dwp_file->name);
10250 }
10251 ids_seen[id] = i;
10252 ids[i] = id;
10253 }
10254 /* Must have exactly one info or types section. */
10255 if (((ids_seen[DW_SECT_INFO] != -1)
10256 + (ids_seen[DW_SECT_TYPES] != -1))
10257 != 1)
10258 {
10259 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10260 " DWO info/types section [in module %s]"),
10261 dwp_file->name);
10262 }
10263 /* Must have an abbrev section. */
10264 if (ids_seen[DW_SECT_ABBREV] == -1)
10265 {
10266 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10267 " section [in module %s]"),
10268 dwp_file->name);
10269 }
10270 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10271 htab->section_pool.v2.sizes =
10272 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10273 * nr_units * nr_columns);
10274 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10275 * nr_units * nr_columns))
10276 > index_end)
10277 {
10278 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10279 " [in module %s]"),
10280 dwp_file->name);
10281 }
10282 }
10283 else /* version == 5 */
10284 {
10285 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10286 int *ids = htab->section_pool.v5.section_ids;
10287 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
10288 /* Reverse map for error checking. */
10289 int ids_seen[DW_SECT_MAX_V5 + 1];
10290
10291 if (nr_columns < 2)
10292 {
10293 error (_("Dwarf Error: bad DWP hash table, too few columns"
10294 " in section table [in module %s]"),
10295 dwp_file->name);
10296 }
10297 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
10298 {
10299 error (_("Dwarf Error: bad DWP hash table, too many columns"
10300 " in section table [in module %s]"),
10301 dwp_file->name);
10302 }
10303 memset (ids, 255, sizeof_ids);
10304 memset (ids_seen, 255, sizeof (ids_seen));
10305 for (int i = 0; i < nr_columns; ++i)
10306 {
10307 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10308
10309 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
10310 {
10311 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10312 " in section table [in module %s]"),
10313 id, dwp_file->name);
10314 }
10315 if (ids_seen[id] != -1)
10316 {
10317 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10318 " id %d in section table [in module %s]"),
10319 id, dwp_file->name);
10320 }
10321 ids_seen[id] = i;
10322 ids[i] = id;
10323 }
10324 /* Must have seen an info section. */
10325 if (ids_seen[DW_SECT_INFO_V5] == -1)
10326 {
10327 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10328 " DWO info/types section [in module %s]"),
10329 dwp_file->name);
10330 }
10331 /* Must have an abbrev section. */
10332 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
10333 {
10334 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10335 " section [in module %s]"),
10336 dwp_file->name);
10337 }
10338 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10339 htab->section_pool.v5.sizes
10340 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
10341 * nr_units * nr_columns);
10342 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
10343 * nr_units * nr_columns))
10344 > index_end)
10345 {
10346 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10347 " [in module %s]"),
10348 dwp_file->name);
10349 }
10350 }
10351
10352 return htab;
10353 }
10354
10355 /* Update SECTIONS with the data from SECTP.
10356
10357 This function is like the other "locate" section routines, but in
10358 this context the sections to read comes from the DWP V1 hash table,
10359 not the full ELF section table.
10360
10361 The result is non-zero for success, or zero if an error was found. */
10362
10363 static int
10364 locate_v1_virtual_dwo_sections (asection *sectp,
10365 struct virtual_v1_dwo_sections *sections)
10366 {
10367 const struct dwop_section_names *names = &dwop_section_names;
10368
10369 if (names->abbrev_dwo.matches (sectp->name))
10370 {
10371 /* There can be only one. */
10372 if (sections->abbrev.s.section != NULL)
10373 return 0;
10374 sections->abbrev.s.section = sectp;
10375 sections->abbrev.size = bfd_section_size (sectp);
10376 }
10377 else if (names->info_dwo.matches (sectp->name)
10378 || names->types_dwo.matches (sectp->name))
10379 {
10380 /* There can be only one. */
10381 if (sections->info_or_types.s.section != NULL)
10382 return 0;
10383 sections->info_or_types.s.section = sectp;
10384 sections->info_or_types.size = bfd_section_size (sectp);
10385 }
10386 else if (names->line_dwo.matches (sectp->name))
10387 {
10388 /* There can be only one. */
10389 if (sections->line.s.section != NULL)
10390 return 0;
10391 sections->line.s.section = sectp;
10392 sections->line.size = bfd_section_size (sectp);
10393 }
10394 else if (names->loc_dwo.matches (sectp->name))
10395 {
10396 /* There can be only one. */
10397 if (sections->loc.s.section != NULL)
10398 return 0;
10399 sections->loc.s.section = sectp;
10400 sections->loc.size = bfd_section_size (sectp);
10401 }
10402 else if (names->macinfo_dwo.matches (sectp->name))
10403 {
10404 /* There can be only one. */
10405 if (sections->macinfo.s.section != NULL)
10406 return 0;
10407 sections->macinfo.s.section = sectp;
10408 sections->macinfo.size = bfd_section_size (sectp);
10409 }
10410 else if (names->macro_dwo.matches (sectp->name))
10411 {
10412 /* There can be only one. */
10413 if (sections->macro.s.section != NULL)
10414 return 0;
10415 sections->macro.s.section = sectp;
10416 sections->macro.size = bfd_section_size (sectp);
10417 }
10418 else if (names->str_offsets_dwo.matches (sectp->name))
10419 {
10420 /* There can be only one. */
10421 if (sections->str_offsets.s.section != NULL)
10422 return 0;
10423 sections->str_offsets.s.section = sectp;
10424 sections->str_offsets.size = bfd_section_size (sectp);
10425 }
10426 else
10427 {
10428 /* No other kind of section is valid. */
10429 return 0;
10430 }
10431
10432 return 1;
10433 }
10434
10435 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10436 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10437 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10438 This is for DWP version 1 files. */
10439
10440 static struct dwo_unit *
10441 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
10442 struct dwp_file *dwp_file,
10443 uint32_t unit_index,
10444 const char *comp_dir,
10445 ULONGEST signature, int is_debug_types)
10446 {
10447 const struct dwp_hash_table *dwp_htab =
10448 is_debug_types ? dwp_file->tus : dwp_file->cus;
10449 bfd *dbfd = dwp_file->dbfd.get ();
10450 const char *kind = is_debug_types ? "TU" : "CU";
10451 struct dwo_file *dwo_file;
10452 struct dwo_unit *dwo_unit;
10453 struct virtual_v1_dwo_sections sections;
10454 void **dwo_file_slot;
10455 int i;
10456
10457 gdb_assert (dwp_file->version == 1);
10458
10459 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10460 kind, pulongest (unit_index), hex_string (signature),
10461 dwp_file->name);
10462
10463 /* Fetch the sections of this DWO unit.
10464 Put a limit on the number of sections we look for so that bad data
10465 doesn't cause us to loop forever. */
10466
10467 #define MAX_NR_V1_DWO_SECTIONS \
10468 (1 /* .debug_info or .debug_types */ \
10469 + 1 /* .debug_abbrev */ \
10470 + 1 /* .debug_line */ \
10471 + 1 /* .debug_loc */ \
10472 + 1 /* .debug_str_offsets */ \
10473 + 1 /* .debug_macro or .debug_macinfo */ \
10474 + 1 /* trailing zero */)
10475
10476 memset (&sections, 0, sizeof (sections));
10477
10478 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10479 {
10480 asection *sectp;
10481 uint32_t section_nr =
10482 read_4_bytes (dbfd,
10483 dwp_htab->section_pool.v1.indices
10484 + (unit_index + i) * sizeof (uint32_t));
10485
10486 if (section_nr == 0)
10487 break;
10488 if (section_nr >= dwp_file->num_sections)
10489 {
10490 error (_("Dwarf Error: bad DWP hash table, section number too large"
10491 " [in module %s]"),
10492 dwp_file->name);
10493 }
10494
10495 sectp = dwp_file->elf_sections[section_nr];
10496 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10497 {
10498 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10499 " [in module %s]"),
10500 dwp_file->name);
10501 }
10502 }
10503
10504 if (i < 2
10505 || sections.info_or_types.empty ()
10506 || sections.abbrev.empty ())
10507 {
10508 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10509 " [in module %s]"),
10510 dwp_file->name);
10511 }
10512 if (i == MAX_NR_V1_DWO_SECTIONS)
10513 {
10514 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10515 " [in module %s]"),
10516 dwp_file->name);
10517 }
10518
10519 /* It's easier for the rest of the code if we fake a struct dwo_file and
10520 have dwo_unit "live" in that. At least for now.
10521
10522 The DWP file can be made up of a random collection of CUs and TUs.
10523 However, for each CU + set of TUs that came from the same original DWO
10524 file, we can combine them back into a virtual DWO file to save space
10525 (fewer struct dwo_file objects to allocate). Remember that for really
10526 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10527
10528 std::string virtual_dwo_name =
10529 string_printf ("virtual-dwo/%d-%d-%d-%d",
10530 sections.abbrev.get_id (),
10531 sections.line.get_id (),
10532 sections.loc.get_id (),
10533 sections.str_offsets.get_id ());
10534 /* Can we use an existing virtual DWO file? */
10535 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10536 comp_dir);
10537 /* Create one if necessary. */
10538 if (*dwo_file_slot == NULL)
10539 {
10540 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10541 virtual_dwo_name.c_str ());
10542
10543 dwo_file = new struct dwo_file;
10544 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10545 dwo_file->comp_dir = comp_dir;
10546 dwo_file->sections.abbrev = sections.abbrev;
10547 dwo_file->sections.line = sections.line;
10548 dwo_file->sections.loc = sections.loc;
10549 dwo_file->sections.macinfo = sections.macinfo;
10550 dwo_file->sections.macro = sections.macro;
10551 dwo_file->sections.str_offsets = sections.str_offsets;
10552 /* The "str" section is global to the entire DWP file. */
10553 dwo_file->sections.str = dwp_file->sections.str;
10554 /* The info or types section is assigned below to dwo_unit,
10555 there's no need to record it in dwo_file.
10556 Also, we can't simply record type sections in dwo_file because
10557 we record a pointer into the vector in dwo_unit. As we collect more
10558 types we'll grow the vector and eventually have to reallocate space
10559 for it, invalidating all copies of pointers into the previous
10560 contents. */
10561 *dwo_file_slot = dwo_file;
10562 }
10563 else
10564 {
10565 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10566 virtual_dwo_name.c_str ());
10567
10568 dwo_file = (struct dwo_file *) *dwo_file_slot;
10569 }
10570
10571 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10572 dwo_unit->dwo_file = dwo_file;
10573 dwo_unit->signature = signature;
10574 dwo_unit->section =
10575 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10576 *dwo_unit->section = sections.info_or_types;
10577 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10578
10579 return dwo_unit;
10580 }
10581
10582 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10583 simplify them. Given a pointer to the containing section SECTION, and
10584 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10585 virtual section of just that piece. */
10586
10587 static struct dwarf2_section_info
10588 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
10589 struct dwarf2_section_info *section,
10590 bfd_size_type offset, bfd_size_type size)
10591 {
10592 struct dwarf2_section_info result;
10593 asection *sectp;
10594
10595 gdb_assert (section != NULL);
10596 gdb_assert (!section->is_virtual);
10597
10598 memset (&result, 0, sizeof (result));
10599 result.s.containing_section = section;
10600 result.is_virtual = true;
10601
10602 if (size == 0)
10603 return result;
10604
10605 sectp = section->get_bfd_section ();
10606
10607 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10608 bounds of the real section. This is a pretty-rare event, so just
10609 flag an error (easier) instead of a warning and trying to cope. */
10610 if (sectp == NULL
10611 || offset + size > bfd_section_size (sectp))
10612 {
10613 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10614 " in section %s [in module %s]"),
10615 sectp ? bfd_section_name (sectp) : "<unknown>",
10616 objfile_name (per_objfile->objfile));
10617 }
10618
10619 result.virtual_offset = offset;
10620 result.size = size;
10621 return result;
10622 }
10623
10624 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10625 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10626 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10627 This is for DWP version 2 files. */
10628
10629 static struct dwo_unit *
10630 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
10631 struct dwp_file *dwp_file,
10632 uint32_t unit_index,
10633 const char *comp_dir,
10634 ULONGEST signature, int is_debug_types)
10635 {
10636 const struct dwp_hash_table *dwp_htab =
10637 is_debug_types ? dwp_file->tus : dwp_file->cus;
10638 bfd *dbfd = dwp_file->dbfd.get ();
10639 const char *kind = is_debug_types ? "TU" : "CU";
10640 struct dwo_file *dwo_file;
10641 struct dwo_unit *dwo_unit;
10642 struct virtual_v2_or_v5_dwo_sections sections;
10643 void **dwo_file_slot;
10644 int i;
10645
10646 gdb_assert (dwp_file->version == 2);
10647
10648 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10649 kind, pulongest (unit_index), hex_string (signature),
10650 dwp_file->name);
10651
10652 /* Fetch the section offsets of this DWO unit. */
10653
10654 memset (&sections, 0, sizeof (sections));
10655
10656 for (i = 0; i < dwp_htab->nr_columns; ++i)
10657 {
10658 uint32_t offset = read_4_bytes (dbfd,
10659 dwp_htab->section_pool.v2.offsets
10660 + (((unit_index - 1) * dwp_htab->nr_columns
10661 + i)
10662 * sizeof (uint32_t)));
10663 uint32_t size = read_4_bytes (dbfd,
10664 dwp_htab->section_pool.v2.sizes
10665 + (((unit_index - 1) * dwp_htab->nr_columns
10666 + i)
10667 * sizeof (uint32_t)));
10668
10669 switch (dwp_htab->section_pool.v2.section_ids[i])
10670 {
10671 case DW_SECT_INFO:
10672 case DW_SECT_TYPES:
10673 sections.info_or_types_offset = offset;
10674 sections.info_or_types_size = size;
10675 break;
10676 case DW_SECT_ABBREV:
10677 sections.abbrev_offset = offset;
10678 sections.abbrev_size = size;
10679 break;
10680 case DW_SECT_LINE:
10681 sections.line_offset = offset;
10682 sections.line_size = size;
10683 break;
10684 case DW_SECT_LOC:
10685 sections.loc_offset = offset;
10686 sections.loc_size = size;
10687 break;
10688 case DW_SECT_STR_OFFSETS:
10689 sections.str_offsets_offset = offset;
10690 sections.str_offsets_size = size;
10691 break;
10692 case DW_SECT_MACINFO:
10693 sections.macinfo_offset = offset;
10694 sections.macinfo_size = size;
10695 break;
10696 case DW_SECT_MACRO:
10697 sections.macro_offset = offset;
10698 sections.macro_size = size;
10699 break;
10700 }
10701 }
10702
10703 /* It's easier for the rest of the code if we fake a struct dwo_file and
10704 have dwo_unit "live" in that. At least for now.
10705
10706 The DWP file can be made up of a random collection of CUs and TUs.
10707 However, for each CU + set of TUs that came from the same original DWO
10708 file, we can combine them back into a virtual DWO file to save space
10709 (fewer struct dwo_file objects to allocate). Remember that for really
10710 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10711
10712 std::string virtual_dwo_name =
10713 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10714 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10715 (long) (sections.line_size ? sections.line_offset : 0),
10716 (long) (sections.loc_size ? sections.loc_offset : 0),
10717 (long) (sections.str_offsets_size
10718 ? sections.str_offsets_offset : 0));
10719 /* Can we use an existing virtual DWO file? */
10720 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10721 comp_dir);
10722 /* Create one if necessary. */
10723 if (*dwo_file_slot == NULL)
10724 {
10725 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10726 virtual_dwo_name.c_str ());
10727
10728 dwo_file = new struct dwo_file;
10729 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10730 dwo_file->comp_dir = comp_dir;
10731 dwo_file->sections.abbrev =
10732 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
10733 sections.abbrev_offset,
10734 sections.abbrev_size);
10735 dwo_file->sections.line =
10736 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
10737 sections.line_offset,
10738 sections.line_size);
10739 dwo_file->sections.loc =
10740 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
10741 sections.loc_offset, sections.loc_size);
10742 dwo_file->sections.macinfo =
10743 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
10744 sections.macinfo_offset,
10745 sections.macinfo_size);
10746 dwo_file->sections.macro =
10747 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
10748 sections.macro_offset,
10749 sections.macro_size);
10750 dwo_file->sections.str_offsets =
10751 create_dwp_v2_or_v5_section (per_objfile,
10752 &dwp_file->sections.str_offsets,
10753 sections.str_offsets_offset,
10754 sections.str_offsets_size);
10755 /* The "str" section is global to the entire DWP file. */
10756 dwo_file->sections.str = dwp_file->sections.str;
10757 /* The info or types section is assigned below to dwo_unit,
10758 there's no need to record it in dwo_file.
10759 Also, we can't simply record type sections in dwo_file because
10760 we record a pointer into the vector in dwo_unit. As we collect more
10761 types we'll grow the vector and eventually have to reallocate space
10762 for it, invalidating all copies of pointers into the previous
10763 contents. */
10764 *dwo_file_slot = dwo_file;
10765 }
10766 else
10767 {
10768 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10769 virtual_dwo_name.c_str ());
10770
10771 dwo_file = (struct dwo_file *) *dwo_file_slot;
10772 }
10773
10774 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10775 dwo_unit->dwo_file = dwo_file;
10776 dwo_unit->signature = signature;
10777 dwo_unit->section =
10778 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10779 *dwo_unit->section = create_dwp_v2_or_v5_section
10780 (per_objfile,
10781 is_debug_types
10782 ? &dwp_file->sections.types
10783 : &dwp_file->sections.info,
10784 sections.info_or_types_offset,
10785 sections.info_or_types_size);
10786 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10787
10788 return dwo_unit;
10789 }
10790
10791 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10792 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10793 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10794 This is for DWP version 5 files. */
10795
10796 static struct dwo_unit *
10797 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
10798 struct dwp_file *dwp_file,
10799 uint32_t unit_index,
10800 const char *comp_dir,
10801 ULONGEST signature, int is_debug_types)
10802 {
10803 const struct dwp_hash_table *dwp_htab
10804 = is_debug_types ? dwp_file->tus : dwp_file->cus;
10805 bfd *dbfd = dwp_file->dbfd.get ();
10806 const char *kind = is_debug_types ? "TU" : "CU";
10807 struct dwo_file *dwo_file;
10808 struct dwo_unit *dwo_unit;
10809 struct virtual_v2_or_v5_dwo_sections sections {};
10810 void **dwo_file_slot;
10811
10812 gdb_assert (dwp_file->version == 5);
10813
10814 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10815 kind, pulongest (unit_index), hex_string (signature),
10816 dwp_file->name);
10817
10818 /* Fetch the section offsets of this DWO unit. */
10819
10820 /* memset (&sections, 0, sizeof (sections)); */
10821
10822 for (int i = 0; i < dwp_htab->nr_columns; ++i)
10823 {
10824 uint32_t offset = read_4_bytes (dbfd,
10825 dwp_htab->section_pool.v5.offsets
10826 + (((unit_index - 1)
10827 * dwp_htab->nr_columns
10828 + i)
10829 * sizeof (uint32_t)));
10830 uint32_t size = read_4_bytes (dbfd,
10831 dwp_htab->section_pool.v5.sizes
10832 + (((unit_index - 1) * dwp_htab->nr_columns
10833 + i)
10834 * sizeof (uint32_t)));
10835
10836 switch (dwp_htab->section_pool.v5.section_ids[i])
10837 {
10838 case DW_SECT_ABBREV_V5:
10839 sections.abbrev_offset = offset;
10840 sections.abbrev_size = size;
10841 break;
10842 case DW_SECT_INFO_V5:
10843 sections.info_or_types_offset = offset;
10844 sections.info_or_types_size = size;
10845 break;
10846 case DW_SECT_LINE_V5:
10847 sections.line_offset = offset;
10848 sections.line_size = size;
10849 break;
10850 case DW_SECT_LOCLISTS_V5:
10851 sections.loclists_offset = offset;
10852 sections.loclists_size = size;
10853 break;
10854 case DW_SECT_MACRO_V5:
10855 sections.macro_offset = offset;
10856 sections.macro_size = size;
10857 break;
10858 case DW_SECT_RNGLISTS_V5:
10859 sections.rnglists_offset = offset;
10860 sections.rnglists_size = size;
10861 break;
10862 case DW_SECT_STR_OFFSETS_V5:
10863 sections.str_offsets_offset = offset;
10864 sections.str_offsets_size = size;
10865 break;
10866 case DW_SECT_RESERVED_V5:
10867 default:
10868 break;
10869 }
10870 }
10871
10872 /* It's easier for the rest of the code if we fake a struct dwo_file and
10873 have dwo_unit "live" in that. At least for now.
10874
10875 The DWP file can be made up of a random collection of CUs and TUs.
10876 However, for each CU + set of TUs that came from the same original DWO
10877 file, we can combine them back into a virtual DWO file to save space
10878 (fewer struct dwo_file objects to allocate). Remember that for really
10879 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10880
10881 std::string virtual_dwo_name =
10882 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10883 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10884 (long) (sections.line_size ? sections.line_offset : 0),
10885 (long) (sections.loclists_size ? sections.loclists_offset : 0),
10886 (long) (sections.str_offsets_size
10887 ? sections.str_offsets_offset : 0),
10888 (long) (sections.macro_size ? sections.macro_offset : 0),
10889 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
10890 /* Can we use an existing virtual DWO file? */
10891 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
10892 virtual_dwo_name.c_str (),
10893 comp_dir);
10894 /* Create one if necessary. */
10895 if (*dwo_file_slot == NULL)
10896 {
10897 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10898 virtual_dwo_name.c_str ());
10899
10900 dwo_file = new struct dwo_file;
10901 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10902 dwo_file->comp_dir = comp_dir;
10903 dwo_file->sections.abbrev =
10904 create_dwp_v2_or_v5_section (per_objfile,
10905 &dwp_file->sections.abbrev,
10906 sections.abbrev_offset,
10907 sections.abbrev_size);
10908 dwo_file->sections.line =
10909 create_dwp_v2_or_v5_section (per_objfile,
10910 &dwp_file->sections.line,
10911 sections.line_offset, sections.line_size);
10912 dwo_file->sections.macro =
10913 create_dwp_v2_or_v5_section (per_objfile,
10914 &dwp_file->sections.macro,
10915 sections.macro_offset,
10916 sections.macro_size);
10917 dwo_file->sections.loclists =
10918 create_dwp_v2_or_v5_section (per_objfile,
10919 &dwp_file->sections.loclists,
10920 sections.loclists_offset,
10921 sections.loclists_size);
10922 dwo_file->sections.rnglists =
10923 create_dwp_v2_or_v5_section (per_objfile,
10924 &dwp_file->sections.rnglists,
10925 sections.rnglists_offset,
10926 sections.rnglists_size);
10927 dwo_file->sections.str_offsets =
10928 create_dwp_v2_or_v5_section (per_objfile,
10929 &dwp_file->sections.str_offsets,
10930 sections.str_offsets_offset,
10931 sections.str_offsets_size);
10932 /* The "str" section is global to the entire DWP file. */
10933 dwo_file->sections.str = dwp_file->sections.str;
10934 /* The info or types section is assigned below to dwo_unit,
10935 there's no need to record it in dwo_file.
10936 Also, we can't simply record type sections in dwo_file because
10937 we record a pointer into the vector in dwo_unit. As we collect more
10938 types we'll grow the vector and eventually have to reallocate space
10939 for it, invalidating all copies of pointers into the previous
10940 contents. */
10941 *dwo_file_slot = dwo_file;
10942 }
10943 else
10944 {
10945 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10946 virtual_dwo_name.c_str ());
10947
10948 dwo_file = (struct dwo_file *) *dwo_file_slot;
10949 }
10950
10951 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10952 dwo_unit->dwo_file = dwo_file;
10953 dwo_unit->signature = signature;
10954 dwo_unit->section
10955 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10956 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
10957 &dwp_file->sections.info,
10958 sections.info_or_types_offset,
10959 sections.info_or_types_size);
10960 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10961
10962 return dwo_unit;
10963 }
10964
10965 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10966 Returns NULL if the signature isn't found. */
10967
10968 static struct dwo_unit *
10969 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
10970 struct dwp_file *dwp_file, const char *comp_dir,
10971 ULONGEST signature, int is_debug_types)
10972 {
10973 const struct dwp_hash_table *dwp_htab =
10974 is_debug_types ? dwp_file->tus : dwp_file->cus;
10975 bfd *dbfd = dwp_file->dbfd.get ();
10976 uint32_t mask = dwp_htab->nr_slots - 1;
10977 uint32_t hash = signature & mask;
10978 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10979 unsigned int i;
10980 void **slot;
10981 struct dwo_unit find_dwo_cu;
10982
10983 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10984 find_dwo_cu.signature = signature;
10985 slot = htab_find_slot (is_debug_types
10986 ? dwp_file->loaded_tus.get ()
10987 : dwp_file->loaded_cus.get (),
10988 &find_dwo_cu, INSERT);
10989
10990 if (*slot != NULL)
10991 return (struct dwo_unit *) *slot;
10992
10993 /* Use a for loop so that we don't loop forever on bad debug info. */
10994 for (i = 0; i < dwp_htab->nr_slots; ++i)
10995 {
10996 ULONGEST signature_in_table;
10997
10998 signature_in_table =
10999 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11000 if (signature_in_table == signature)
11001 {
11002 uint32_t unit_index =
11003 read_4_bytes (dbfd,
11004 dwp_htab->unit_table + hash * sizeof (uint32_t));
11005
11006 if (dwp_file->version == 1)
11007 {
11008 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11009 unit_index, comp_dir,
11010 signature, is_debug_types);
11011 }
11012 else if (dwp_file->version == 2)
11013 {
11014 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11015 unit_index, comp_dir,
11016 signature, is_debug_types);
11017 }
11018 else /* version == 5 */
11019 {
11020 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11021 unit_index, comp_dir,
11022 signature, is_debug_types);
11023 }
11024 return (struct dwo_unit *) *slot;
11025 }
11026 if (signature_in_table == 0)
11027 return NULL;
11028 hash = (hash + hash2) & mask;
11029 }
11030
11031 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11032 " [in module %s]"),
11033 dwp_file->name);
11034 }
11035
11036 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11037 Open the file specified by FILE_NAME and hand it off to BFD for
11038 preliminary analysis. Return a newly initialized bfd *, which
11039 includes a canonicalized copy of FILE_NAME.
11040 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11041 SEARCH_CWD is true if the current directory is to be searched.
11042 It will be searched before debug-file-directory.
11043 If successful, the file is added to the bfd include table of the
11044 objfile's bfd (see gdb_bfd_record_inclusion).
11045 If unable to find/open the file, return NULL.
11046 NOTE: This function is derived from symfile_bfd_open. */
11047
11048 static gdb_bfd_ref_ptr
11049 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11050 const char *file_name, int is_dwp, int search_cwd)
11051 {
11052 int desc;
11053 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11054 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11055 to debug_file_directory. */
11056 const char *search_path;
11057 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11058
11059 gdb::unique_xmalloc_ptr<char> search_path_holder;
11060 if (search_cwd)
11061 {
11062 if (!debug_file_directory.empty ())
11063 {
11064 search_path_holder.reset (concat (".", dirname_separator_string,
11065 debug_file_directory.c_str (),
11066 (char *) NULL));
11067 search_path = search_path_holder.get ();
11068 }
11069 else
11070 search_path = ".";
11071 }
11072 else
11073 search_path = debug_file_directory.c_str ();
11074
11075 /* Add the path for the executable binary to the list of search paths. */
11076 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
11077 search_path_holder.reset (concat (objfile_dir.c_str (),
11078 dirname_separator_string,
11079 search_path, nullptr));
11080 search_path = search_path_holder.get ();
11081
11082 openp_flags flags = OPF_RETURN_REALPATH;
11083 if (is_dwp)
11084 flags |= OPF_SEARCH_IN_PATH;
11085
11086 gdb::unique_xmalloc_ptr<char> absolute_name;
11087 desc = openp (search_path, flags, file_name,
11088 O_RDONLY | O_BINARY, &absolute_name);
11089 if (desc < 0)
11090 return NULL;
11091
11092 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11093 gnutarget, desc));
11094 if (sym_bfd == NULL)
11095 return NULL;
11096 bfd_set_cacheable (sym_bfd.get (), 1);
11097
11098 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11099 return NULL;
11100
11101 /* Success. Record the bfd as having been included by the objfile's bfd.
11102 This is important because things like demangled_names_hash lives in the
11103 objfile's per_bfd space and may have references to things like symbol
11104 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11105 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
11106
11107 return sym_bfd;
11108 }
11109
11110 /* Try to open DWO file FILE_NAME.
11111 COMP_DIR is the DW_AT_comp_dir attribute.
11112 The result is the bfd handle of the file.
11113 If there is a problem finding or opening the file, return NULL.
11114 Upon success, the canonicalized path of the file is stored in the bfd,
11115 same as symfile_bfd_open. */
11116
11117 static gdb_bfd_ref_ptr
11118 open_dwo_file (dwarf2_per_objfile *per_objfile,
11119 const char *file_name, const char *comp_dir)
11120 {
11121 if (IS_ABSOLUTE_PATH (file_name))
11122 return try_open_dwop_file (per_objfile, file_name,
11123 0 /*is_dwp*/, 0 /*search_cwd*/);
11124
11125 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11126
11127 if (comp_dir != NULL)
11128 {
11129 std::string path_to_try = path_join (comp_dir, file_name);
11130
11131 /* NOTE: If comp_dir is a relative path, this will also try the
11132 search path, which seems useful. */
11133 gdb_bfd_ref_ptr abfd (try_open_dwop_file
11134 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11135
11136 if (abfd != NULL)
11137 return abfd;
11138 }
11139
11140 /* That didn't work, try debug-file-directory, which, despite its name,
11141 is a list of paths. */
11142
11143 if (debug_file_directory.empty ())
11144 return NULL;
11145
11146 return try_open_dwop_file (per_objfile, file_name,
11147 0 /*is_dwp*/, 1 /*search_cwd*/);
11148 }
11149
11150 /* This function is mapped across the sections and remembers the offset and
11151 size of each of the DWO debugging sections we are interested in. */
11152
11153 static void
11154 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
11155 asection *sectp, dwo_sections *dwo_sections)
11156 {
11157 const struct dwop_section_names *names = &dwop_section_names;
11158
11159 struct dwarf2_section_info *dw_sect = nullptr;
11160
11161 if (names->abbrev_dwo.matches (sectp->name))
11162 dw_sect = &dwo_sections->abbrev;
11163 else if (names->info_dwo.matches (sectp->name))
11164 dw_sect = &dwo_sections->info;
11165 else if (names->line_dwo.matches (sectp->name))
11166 dw_sect = &dwo_sections->line;
11167 else if (names->loc_dwo.matches (sectp->name))
11168 dw_sect = &dwo_sections->loc;
11169 else if (names->loclists_dwo.matches (sectp->name))
11170 dw_sect = &dwo_sections->loclists;
11171 else if (names->macinfo_dwo.matches (sectp->name))
11172 dw_sect = &dwo_sections->macinfo;
11173 else if (names->macro_dwo.matches (sectp->name))
11174 dw_sect = &dwo_sections->macro;
11175 else if (names->rnglists_dwo.matches (sectp->name))
11176 dw_sect = &dwo_sections->rnglists;
11177 else if (names->str_dwo.matches (sectp->name))
11178 dw_sect = &dwo_sections->str;
11179 else if (names->str_offsets_dwo.matches (sectp->name))
11180 dw_sect = &dwo_sections->str_offsets;
11181 else if (names->types_dwo.matches (sectp->name))
11182 {
11183 struct dwarf2_section_info type_section;
11184
11185 memset (&type_section, 0, sizeof (type_section));
11186 dwo_sections->types.push_back (type_section);
11187 dw_sect = &dwo_sections->types.back ();
11188 }
11189
11190 if (dw_sect != nullptr)
11191 {
11192 dw_sect->s.section = sectp;
11193 dw_sect->size = bfd_section_size (sectp);
11194 dw_sect->read (objfile);
11195 }
11196 }
11197
11198 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11199 by PER_CU. This is for the non-DWP case.
11200 The result is NULL if DWO_NAME can't be found. */
11201
11202 static struct dwo_file *
11203 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
11204 const char *comp_dir)
11205 {
11206 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11207
11208 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
11209 if (dbfd == NULL)
11210 {
11211 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
11212
11213 return NULL;
11214 }
11215
11216 dwo_file_up dwo_file (new struct dwo_file);
11217 dwo_file->dwo_name = dwo_name;
11218 dwo_file->comp_dir = comp_dir;
11219 dwo_file->dbfd = std::move (dbfd);
11220
11221 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
11222 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
11223 sec, &dwo_file->sections);
11224
11225 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
11226 dwo_file->cus);
11227
11228 if (cu->per_cu->version () < 5)
11229 {
11230 create_debug_types_hash_table (per_objfile, dwo_file.get (),
11231 dwo_file->sections.types, dwo_file->tus);
11232 }
11233 else
11234 {
11235 create_debug_type_hash_table (per_objfile, dwo_file.get (),
11236 &dwo_file->sections.info, dwo_file->tus,
11237 rcuh_kind::COMPILE);
11238 }
11239
11240 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
11241
11242 return dwo_file.release ();
11243 }
11244
11245 /* This function is mapped across the sections and remembers the offset and
11246 size of each of the DWP debugging sections common to version 1 and 2 that
11247 we are interested in. */
11248
11249 static void
11250 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
11251 asection *sectp, dwp_file *dwp_file)
11252 {
11253 const struct dwop_section_names *names = &dwop_section_names;
11254 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11255
11256 /* Record the ELF section number for later lookup: this is what the
11257 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11258 gdb_assert (elf_section_nr < dwp_file->num_sections);
11259 dwp_file->elf_sections[elf_section_nr] = sectp;
11260
11261 /* Look for specific sections that we need. */
11262 struct dwarf2_section_info *dw_sect = nullptr;
11263 if (names->str_dwo.matches (sectp->name))
11264 dw_sect = &dwp_file->sections.str;
11265 else if (names->cu_index.matches (sectp->name))
11266 dw_sect = &dwp_file->sections.cu_index;
11267 else if (names->tu_index.matches (sectp->name))
11268 dw_sect = &dwp_file->sections.tu_index;
11269
11270 if (dw_sect != nullptr)
11271 {
11272 dw_sect->s.section = sectp;
11273 dw_sect->size = bfd_section_size (sectp);
11274 dw_sect->read (objfile);
11275 }
11276 }
11277
11278 /* This function is mapped across the sections and remembers the offset and
11279 size of each of the DWP version 2 debugging sections that we are interested
11280 in. This is split into a separate function because we don't know if we
11281 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11282
11283 static void
11284 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
11285 asection *sectp, void *dwp_file_ptr)
11286 {
11287 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11288 const struct dwop_section_names *names = &dwop_section_names;
11289 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11290
11291 /* Record the ELF section number for later lookup: this is what the
11292 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11293 gdb_assert (elf_section_nr < dwp_file->num_sections);
11294 dwp_file->elf_sections[elf_section_nr] = sectp;
11295
11296 /* Look for specific sections that we need. */
11297 struct dwarf2_section_info *dw_sect = nullptr;
11298 if (names->abbrev_dwo.matches (sectp->name))
11299 dw_sect = &dwp_file->sections.abbrev;
11300 else if (names->info_dwo.matches (sectp->name))
11301 dw_sect = &dwp_file->sections.info;
11302 else if (names->line_dwo.matches (sectp->name))
11303 dw_sect = &dwp_file->sections.line;
11304 else if (names->loc_dwo.matches (sectp->name))
11305 dw_sect = &dwp_file->sections.loc;
11306 else if (names->macinfo_dwo.matches (sectp->name))
11307 dw_sect = &dwp_file->sections.macinfo;
11308 else if (names->macro_dwo.matches (sectp->name))
11309 dw_sect = &dwp_file->sections.macro;
11310 else if (names->str_offsets_dwo.matches (sectp->name))
11311 dw_sect = &dwp_file->sections.str_offsets;
11312 else if (names->types_dwo.matches (sectp->name))
11313 dw_sect = &dwp_file->sections.types;
11314
11315 if (dw_sect != nullptr)
11316 {
11317 dw_sect->s.section = sectp;
11318 dw_sect->size = bfd_section_size (sectp);
11319 dw_sect->read (objfile);
11320 }
11321 }
11322
11323 /* This function is mapped across the sections and remembers the offset and
11324 size of each of the DWP version 5 debugging sections that we are interested
11325 in. This is split into a separate function because we don't know if we
11326 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11327
11328 static void
11329 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
11330 asection *sectp, void *dwp_file_ptr)
11331 {
11332 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11333 const struct dwop_section_names *names = &dwop_section_names;
11334 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11335
11336 /* Record the ELF section number for later lookup: this is what the
11337 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11338 gdb_assert (elf_section_nr < dwp_file->num_sections);
11339 dwp_file->elf_sections[elf_section_nr] = sectp;
11340
11341 /* Look for specific sections that we need. */
11342 struct dwarf2_section_info *dw_sect = nullptr;
11343 if (names->abbrev_dwo.matches (sectp->name))
11344 dw_sect = &dwp_file->sections.abbrev;
11345 else if (names->info_dwo.matches (sectp->name))
11346 dw_sect = &dwp_file->sections.info;
11347 else if (names->line_dwo.matches (sectp->name))
11348 dw_sect = &dwp_file->sections.line;
11349 else if (names->loclists_dwo.matches (sectp->name))
11350 dw_sect = &dwp_file->sections.loclists;
11351 else if (names->macro_dwo.matches (sectp->name))
11352 dw_sect = &dwp_file->sections.macro;
11353 else if (names->rnglists_dwo.matches (sectp->name))
11354 dw_sect = &dwp_file->sections.rnglists;
11355 else if (names->str_offsets_dwo.matches (sectp->name))
11356 dw_sect = &dwp_file->sections.str_offsets;
11357
11358 if (dw_sect != nullptr)
11359 {
11360 dw_sect->s.section = sectp;
11361 dw_sect->size = bfd_section_size (sectp);
11362 dw_sect->read (objfile);
11363 }
11364 }
11365
11366 /* Hash function for dwp_file loaded CUs/TUs. */
11367
11368 static hashval_t
11369 hash_dwp_loaded_cutus (const void *item)
11370 {
11371 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11372
11373 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11374 return dwo_unit->signature;
11375 }
11376
11377 /* Equality function for dwp_file loaded CUs/TUs. */
11378
11379 static int
11380 eq_dwp_loaded_cutus (const void *a, const void *b)
11381 {
11382 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11383 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11384
11385 return dua->signature == dub->signature;
11386 }
11387
11388 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11389
11390 static htab_up
11391 allocate_dwp_loaded_cutus_table ()
11392 {
11393 return htab_up (htab_create_alloc (3,
11394 hash_dwp_loaded_cutus,
11395 eq_dwp_loaded_cutus,
11396 NULL, xcalloc, xfree));
11397 }
11398
11399 /* Try to open DWP file FILE_NAME.
11400 The result is the bfd handle of the file.
11401 If there is a problem finding or opening the file, return NULL.
11402 Upon success, the canonicalized path of the file is stored in the bfd,
11403 same as symfile_bfd_open. */
11404
11405 static gdb_bfd_ref_ptr
11406 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
11407 {
11408 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
11409 1 /*is_dwp*/,
11410 1 /*search_cwd*/));
11411 if (abfd != NULL)
11412 return abfd;
11413
11414 /* Work around upstream bug 15652.
11415 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11416 [Whether that's a "bug" is debatable, but it is getting in our way.]
11417 We have no real idea where the dwp file is, because gdb's realpath-ing
11418 of the executable's path may have discarded the needed info.
11419 [IWBN if the dwp file name was recorded in the executable, akin to
11420 .gnu_debuglink, but that doesn't exist yet.]
11421 Strip the directory from FILE_NAME and search again. */
11422 if (!debug_file_directory.empty ())
11423 {
11424 /* Don't implicitly search the current directory here.
11425 If the user wants to search "." to handle this case,
11426 it must be added to debug-file-directory. */
11427 return try_open_dwop_file (per_objfile, lbasename (file_name),
11428 1 /*is_dwp*/,
11429 0 /*search_cwd*/);
11430 }
11431
11432 return NULL;
11433 }
11434
11435 /* Initialize the use of the DWP file for the current objfile.
11436 By convention the name of the DWP file is ${objfile}.dwp.
11437 The result is NULL if it can't be found. */
11438
11439 static std::unique_ptr<struct dwp_file>
11440 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
11441 {
11442 struct objfile *objfile = per_objfile->objfile;
11443
11444 /* Try to find first .dwp for the binary file before any symbolic links
11445 resolving. */
11446
11447 /* If the objfile is a debug file, find the name of the real binary
11448 file and get the name of dwp file from there. */
11449 std::string dwp_name;
11450 if (objfile->separate_debug_objfile_backlink != NULL)
11451 {
11452 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11453 const char *backlink_basename = lbasename (backlink->original_name);
11454
11455 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11456 }
11457 else
11458 dwp_name = objfile->original_name;
11459
11460 dwp_name += ".dwp";
11461
11462 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
11463 if (dbfd == NULL
11464 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11465 {
11466 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11467 dwp_name = objfile_name (objfile);
11468 dwp_name += ".dwp";
11469 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
11470 }
11471
11472 if (dbfd == NULL)
11473 {
11474 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
11475
11476 return std::unique_ptr<dwp_file> ();
11477 }
11478
11479 const char *name = bfd_get_filename (dbfd.get ());
11480 std::unique_ptr<struct dwp_file> dwp_file
11481 (new struct dwp_file (name, std::move (dbfd)));
11482
11483 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
11484 dwp_file->elf_sections =
11485 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
11486 dwp_file->num_sections, asection *);
11487
11488 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11489 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11490 dwp_file.get ());
11491
11492 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
11493
11494 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
11495
11496 /* The DWP file version is stored in the hash table. Oh well. */
11497 if (dwp_file->cus && dwp_file->tus
11498 && dwp_file->cus->version != dwp_file->tus->version)
11499 {
11500 /* Technically speaking, we should try to limp along, but this is
11501 pretty bizarre. We use pulongest here because that's the established
11502 portability solution (e.g, we cannot use %u for uint32_t). */
11503 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11504 " TU version %s [in DWP file %s]"),
11505 pulongest (dwp_file->cus->version),
11506 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11507 }
11508
11509 if (dwp_file->cus)
11510 dwp_file->version = dwp_file->cus->version;
11511 else if (dwp_file->tus)
11512 dwp_file->version = dwp_file->tus->version;
11513 else
11514 dwp_file->version = 2;
11515
11516 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11517 {
11518 if (dwp_file->version == 2)
11519 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11520 dwp_file.get ());
11521 else
11522 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11523 dwp_file.get ());
11524 }
11525
11526 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
11527 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
11528
11529 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
11530 dwarf_read_debug_printf (" %s CUs, %s TUs",
11531 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11532 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11533
11534 return dwp_file;
11535 }
11536
11537 /* Wrapper around open_and_init_dwp_file, only open it once. */
11538
11539 static struct dwp_file *
11540 get_dwp_file (dwarf2_per_objfile *per_objfile)
11541 {
11542 if (!per_objfile->per_bfd->dwp_checked)
11543 {
11544 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
11545 per_objfile->per_bfd->dwp_checked = 1;
11546 }
11547 return per_objfile->per_bfd->dwp_file.get ();
11548 }
11549
11550 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11551 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11552 or in the DWP file for the objfile, referenced by THIS_UNIT.
11553 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11554 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11555
11556 This is called, for example, when wanting to read a variable with a
11557 complex location. Therefore we don't want to do file i/o for every call.
11558 Therefore we don't want to look for a DWO file on every call.
11559 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11560 then we check if we've already seen DWO_NAME, and only THEN do we check
11561 for a DWO file.
11562
11563 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11564 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11565
11566 static struct dwo_unit *
11567 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11568 ULONGEST signature, int is_debug_types)
11569 {
11570 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11571 struct objfile *objfile = per_objfile->objfile;
11572 const char *kind = is_debug_types ? "TU" : "CU";
11573 void **dwo_file_slot;
11574 struct dwo_file *dwo_file;
11575 struct dwp_file *dwp_file;
11576
11577 /* First see if there's a DWP file.
11578 If we have a DWP file but didn't find the DWO inside it, don't
11579 look for the original DWO file. It makes gdb behave differently
11580 depending on whether one is debugging in the build tree. */
11581
11582 dwp_file = get_dwp_file (per_objfile);
11583 if (dwp_file != NULL)
11584 {
11585 const struct dwp_hash_table *dwp_htab =
11586 is_debug_types ? dwp_file->tus : dwp_file->cus;
11587
11588 if (dwp_htab != NULL)
11589 {
11590 struct dwo_unit *dwo_cutu =
11591 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
11592 is_debug_types);
11593
11594 if (dwo_cutu != NULL)
11595 {
11596 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11597 kind, hex_string (signature),
11598 host_address_to_string (dwo_cutu));
11599
11600 return dwo_cutu;
11601 }
11602 }
11603 }
11604 else
11605 {
11606 /* No DWP file, look for the DWO file. */
11607
11608 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
11609 if (*dwo_file_slot == NULL)
11610 {
11611 /* Read in the file and build a table of the CUs/TUs it contains. */
11612 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
11613 }
11614 /* NOTE: This will be NULL if unable to open the file. */
11615 dwo_file = (struct dwo_file *) *dwo_file_slot;
11616
11617 if (dwo_file != NULL)
11618 {
11619 struct dwo_unit *dwo_cutu = NULL;
11620
11621 if (is_debug_types && dwo_file->tus)
11622 {
11623 struct dwo_unit find_dwo_cutu;
11624
11625 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11626 find_dwo_cutu.signature = signature;
11627 dwo_cutu
11628 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
11629 &find_dwo_cutu);
11630 }
11631 else if (!is_debug_types && dwo_file->cus)
11632 {
11633 struct dwo_unit find_dwo_cutu;
11634
11635 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11636 find_dwo_cutu.signature = signature;
11637 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
11638 &find_dwo_cutu);
11639 }
11640
11641 if (dwo_cutu != NULL)
11642 {
11643 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11644 kind, dwo_name, hex_string (signature),
11645 host_address_to_string (dwo_cutu));
11646
11647 return dwo_cutu;
11648 }
11649 }
11650 }
11651
11652 /* We didn't find it. This could mean a dwo_id mismatch, or
11653 someone deleted the DWO/DWP file, or the search path isn't set up
11654 correctly to find the file. */
11655
11656 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11657 kind, dwo_name, hex_string (signature));
11658
11659 /* This is a warning and not a complaint because it can be caused by
11660 pilot error (e.g., user accidentally deleting the DWO). */
11661 {
11662 /* Print the name of the DWP file if we looked there, helps the user
11663 better diagnose the problem. */
11664 std::string dwp_text;
11665
11666 if (dwp_file != NULL)
11667 dwp_text = string_printf (" [in DWP file %s]",
11668 lbasename (dwp_file->name));
11669
11670 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11671 " [in module %s]"),
11672 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
11673 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
11674 }
11675 return NULL;
11676 }
11677
11678 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11679 See lookup_dwo_cutu_unit for details. */
11680
11681 static struct dwo_unit *
11682 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11683 ULONGEST signature)
11684 {
11685 gdb_assert (!cu->per_cu->is_debug_types);
11686
11687 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
11688 }
11689
11690 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11691 See lookup_dwo_cutu_unit for details. */
11692
11693 static struct dwo_unit *
11694 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
11695 {
11696 gdb_assert (cu->per_cu->is_debug_types);
11697
11698 signatured_type *sig_type = (signatured_type *) cu->per_cu;
11699
11700 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
11701 }
11702
11703 /* Traversal function for queue_and_load_all_dwo_tus. */
11704
11705 static int
11706 queue_and_load_dwo_tu (void **slot, void *info)
11707 {
11708 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11709 dwarf2_cu *cu = (dwarf2_cu *) info;
11710 ULONGEST signature = dwo_unit->signature;
11711 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
11712
11713 if (sig_type != NULL)
11714 {
11715 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11716 a real dependency of PER_CU on SIG_TYPE. That is detected later
11717 while processing PER_CU. */
11718 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
11719 cu->lang ()))
11720 load_full_type_unit (sig_type, cu->per_objfile);
11721 cu->per_cu->imported_symtabs_push (sig_type);
11722 }
11723
11724 return 1;
11725 }
11726
11727 /* Queue all TUs contained in the DWO of CU to be read in.
11728 The DWO may have the only definition of the type, though it may not be
11729 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11730 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11731
11732 static void
11733 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
11734 {
11735 struct dwo_unit *dwo_unit;
11736 struct dwo_file *dwo_file;
11737
11738 gdb_assert (cu != nullptr);
11739 gdb_assert (!cu->per_cu->is_debug_types);
11740 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
11741
11742 dwo_unit = cu->dwo_unit;
11743 gdb_assert (dwo_unit != NULL);
11744
11745 dwo_file = dwo_unit->dwo_file;
11746 if (dwo_file->tus != NULL)
11747 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
11748 }
11749
11750 /* Read in various DIEs. */
11751
11752 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11753 Inherit only the children of the DW_AT_abstract_origin DIE not being
11754 already referenced by DW_AT_abstract_origin from the children of the
11755 current DIE. */
11756
11757 static void
11758 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11759 {
11760 struct die_info *child_die;
11761 sect_offset *offsetp;
11762 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11763 struct die_info *origin_die;
11764 /* Iterator of the ORIGIN_DIE children. */
11765 struct die_info *origin_child_die;
11766 struct attribute *attr;
11767 struct dwarf2_cu *origin_cu;
11768 struct pending **origin_previous_list_in_scope;
11769
11770 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11771 if (!attr)
11772 return;
11773
11774 /* Note that following die references may follow to a die in a
11775 different cu. */
11776
11777 origin_cu = cu;
11778 origin_die = follow_die_ref (die, attr, &origin_cu);
11779
11780 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11781 symbols in. */
11782 origin_previous_list_in_scope = origin_cu->list_in_scope;
11783 origin_cu->list_in_scope = cu->list_in_scope;
11784
11785 if (die->tag != origin_die->tag
11786 && !(die->tag == DW_TAG_inlined_subroutine
11787 && origin_die->tag == DW_TAG_subprogram))
11788 complaint (_("DIE %s and its abstract origin %s have different tags"),
11789 sect_offset_str (die->sect_off),
11790 sect_offset_str (origin_die->sect_off));
11791
11792 /* Find if the concrete and abstract trees are structurally the
11793 same. This is a shallow traversal and it is not bullet-proof;
11794 the compiler can trick the debugger into believing that the trees
11795 are isomorphic, whereas they actually are not. However, the
11796 likelyhood of this happening is pretty low, and a full-fledged
11797 check would be an overkill. */
11798 bool are_isomorphic = true;
11799 die_info *concrete_child = die->child;
11800 die_info *abstract_child = origin_die->child;
11801 while (concrete_child != nullptr || abstract_child != nullptr)
11802 {
11803 if (concrete_child == nullptr
11804 || abstract_child == nullptr
11805 || concrete_child->tag != abstract_child->tag)
11806 {
11807 are_isomorphic = false;
11808 break;
11809 }
11810
11811 concrete_child = concrete_child->sibling;
11812 abstract_child = abstract_child->sibling;
11813 }
11814
11815 /* Walk the origin's children in parallel to the concrete children.
11816 This helps match an origin child in case the debug info misses
11817 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11818 origin tree may not have the same tree structure as the concrete
11819 DIE, though. */
11820 die_info *corresponding_abstract_child
11821 = are_isomorphic ? origin_die->child : nullptr;
11822
11823 std::vector<sect_offset> offsets;
11824
11825 for (child_die = die->child;
11826 child_die && child_die->tag;
11827 child_die = child_die->sibling)
11828 {
11829 struct die_info *child_origin_die;
11830 struct dwarf2_cu *child_origin_cu;
11831
11832 /* We are trying to process concrete instance entries:
11833 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11834 it's not relevant to our analysis here. i.e. detecting DIEs that are
11835 present in the abstract instance but not referenced in the concrete
11836 one. */
11837 if (child_die->tag == DW_TAG_call_site
11838 || child_die->tag == DW_TAG_GNU_call_site)
11839 {
11840 if (are_isomorphic)
11841 corresponding_abstract_child
11842 = corresponding_abstract_child->sibling;
11843 continue;
11844 }
11845
11846 /* For each CHILD_DIE, find the corresponding child of
11847 ORIGIN_DIE. If there is more than one layer of
11848 DW_AT_abstract_origin, follow them all; there shouldn't be,
11849 but GCC versions at least through 4.4 generate this (GCC PR
11850 40573). */
11851 child_origin_die = child_die;
11852 child_origin_cu = cu;
11853 while (1)
11854 {
11855 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11856 child_origin_cu);
11857 if (attr == NULL)
11858 break;
11859 child_origin_die = follow_die_ref (child_origin_die, attr,
11860 &child_origin_cu);
11861 }
11862
11863 /* If missing DW_AT_abstract_origin, try the corresponding child
11864 of the origin. Clang emits such lexical scopes. */
11865 if (child_origin_die == child_die
11866 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
11867 && are_isomorphic
11868 && child_die->tag == DW_TAG_lexical_block)
11869 child_origin_die = corresponding_abstract_child;
11870
11871 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11872 counterpart may exist. */
11873 if (child_origin_die != child_die)
11874 {
11875 if (child_die->tag != child_origin_die->tag
11876 && !(child_die->tag == DW_TAG_inlined_subroutine
11877 && child_origin_die->tag == DW_TAG_subprogram))
11878 complaint (_("Child DIE %s and its abstract origin %s have "
11879 "different tags"),
11880 sect_offset_str (child_die->sect_off),
11881 sect_offset_str (child_origin_die->sect_off));
11882 if (child_origin_die->parent != origin_die)
11883 complaint (_("Child DIE %s and its abstract origin %s have "
11884 "different parents"),
11885 sect_offset_str (child_die->sect_off),
11886 sect_offset_str (child_origin_die->sect_off));
11887 else
11888 offsets.push_back (child_origin_die->sect_off);
11889 }
11890
11891 if (are_isomorphic)
11892 corresponding_abstract_child = corresponding_abstract_child->sibling;
11893 }
11894 std::sort (offsets.begin (), offsets.end ());
11895 sect_offset *offsets_end = offsets.data () + offsets.size ();
11896 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
11897 if (offsetp[-1] == *offsetp)
11898 complaint (_("Multiple children of DIE %s refer "
11899 "to DIE %s as their abstract origin"),
11900 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
11901
11902 offsetp = offsets.data ();
11903 origin_child_die = origin_die->child;
11904 while (origin_child_die && origin_child_die->tag)
11905 {
11906 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11907 while (offsetp < offsets_end
11908 && *offsetp < origin_child_die->sect_off)
11909 offsetp++;
11910 if (offsetp >= offsets_end
11911 || *offsetp > origin_child_die->sect_off)
11912 {
11913 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11914 Check whether we're already processing ORIGIN_CHILD_DIE.
11915 This can happen with mutually referenced abstract_origins.
11916 PR 16581. */
11917 if (!origin_child_die->in_process)
11918 process_die (origin_child_die, origin_cu);
11919 }
11920 origin_child_die = origin_child_die->sibling;
11921 }
11922 origin_cu->list_in_scope = origin_previous_list_in_scope;
11923
11924 if (cu != origin_cu)
11925 compute_delayed_physnames (origin_cu);
11926 }
11927
11928 static void
11929 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11930 {
11931 struct objfile *objfile = cu->per_objfile->objfile;
11932 struct gdbarch *gdbarch = objfile->arch ();
11933 struct context_stack *newobj;
11934 CORE_ADDR lowpc;
11935 CORE_ADDR highpc;
11936 struct die_info *child_die;
11937 struct attribute *attr, *call_line, *call_file;
11938 const char *name;
11939 CORE_ADDR baseaddr;
11940 struct block *block;
11941 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11942 std::vector<struct symbol *> template_args;
11943 struct template_symbol *templ_func = NULL;
11944
11945 if (inlined_func)
11946 {
11947 /* If we do not have call site information, we can't show the
11948 caller of this inlined function. That's too confusing, so
11949 only use the scope for local variables. */
11950 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11951 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11952 if (call_line == NULL || call_file == NULL)
11953 {
11954 read_lexical_block_scope (die, cu);
11955 return;
11956 }
11957 }
11958
11959 baseaddr = objfile->text_section_offset ();
11960
11961 name = dwarf2_name (die, cu);
11962 if (name == nullptr)
11963 name = dw2_linkage_name (die, cu);
11964
11965 /* Ignore functions with missing or empty names. These are actually
11966 illegal according to the DWARF standard. */
11967 if (name == NULL)
11968 {
11969 complaint (_("missing name for subprogram DIE at %s"),
11970 sect_offset_str (die->sect_off));
11971 return;
11972 }
11973
11974 /* Ignore functions with missing or invalid low and high pc attributes. */
11975 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
11976 <= PC_BOUNDS_INVALID)
11977 {
11978 attr = dwarf2_attr (die, DW_AT_external, cu);
11979 if (attr == nullptr || !attr->as_boolean ())
11980 complaint (_("cannot get low and high bounds "
11981 "for subprogram DIE at %s"),
11982 sect_offset_str (die->sect_off));
11983 return;
11984 }
11985
11986 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11987 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11988
11989 /* If we have any template arguments, then we must allocate a
11990 different sort of symbol. */
11991 for (child_die = die->child; child_die; child_die = child_die->sibling)
11992 {
11993 if (child_die->tag == DW_TAG_template_type_param
11994 || child_die->tag == DW_TAG_template_value_param)
11995 {
11996 templ_func = new (&objfile->objfile_obstack) template_symbol;
11997 templ_func->subclass = SYMBOL_TEMPLATE;
11998 break;
11999 }
12000 }
12001
12002 gdb_assert (cu->get_builder () != nullptr);
12003 newobj = cu->get_builder ()->push_context (0, lowpc);
12004 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12005 (struct symbol *) templ_func);
12006
12007 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12008 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12009 cu->lang ());
12010
12011 /* If there is a location expression for DW_AT_frame_base, record
12012 it. */
12013 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12014 if (attr != nullptr)
12015 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12016
12017 /* If there is a location for the static link, record it. */
12018 newobj->static_link = NULL;
12019 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12020 if (attr != nullptr)
12021 {
12022 newobj->static_link
12023 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12024 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12025 cu->addr_type ());
12026 }
12027
12028 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12029
12030 if (die->child != NULL)
12031 {
12032 child_die = die->child;
12033 while (child_die && child_die->tag)
12034 {
12035 if (child_die->tag == DW_TAG_template_type_param
12036 || child_die->tag == DW_TAG_template_value_param)
12037 {
12038 struct symbol *arg = new_symbol (child_die, NULL, cu);
12039
12040 if (arg != NULL)
12041 template_args.push_back (arg);
12042 }
12043 else
12044 process_die (child_die, cu);
12045 child_die = child_die->sibling;
12046 }
12047 }
12048
12049 inherit_abstract_dies (die, cu);
12050
12051 /* If we have a DW_AT_specification, we might need to import using
12052 directives from the context of the specification DIE. See the
12053 comment in determine_prefix. */
12054 if (cu->lang () == language_cplus
12055 && dwarf2_attr (die, DW_AT_specification, cu))
12056 {
12057 struct dwarf2_cu *spec_cu = cu;
12058 struct die_info *spec_die = die_specification (die, &spec_cu);
12059
12060 while (spec_die)
12061 {
12062 child_die = spec_die->child;
12063 while (child_die && child_die->tag)
12064 {
12065 if (child_die->tag == DW_TAG_imported_module)
12066 process_die (child_die, spec_cu);
12067 child_die = child_die->sibling;
12068 }
12069
12070 /* In some cases, GCC generates specification DIEs that
12071 themselves contain DW_AT_specification attributes. */
12072 spec_die = die_specification (spec_die, &spec_cu);
12073 }
12074 }
12075
12076 struct context_stack cstk = cu->get_builder ()->pop_context ();
12077 /* Make a block for the local symbols within. */
12078 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12079 cstk.static_link, lowpc, highpc);
12080
12081 /* For C++, set the block's scope. */
12082 if ((cu->lang () == language_cplus
12083 || cu->lang () == language_fortran
12084 || cu->lang () == language_d
12085 || cu->lang () == language_rust)
12086 && cu->processing_has_namespace_info)
12087 block_set_scope (block, determine_prefix (die, cu),
12088 &objfile->objfile_obstack);
12089
12090 /* If we have address ranges, record them. */
12091 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12092
12093 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12094
12095 /* Attach template arguments to function. */
12096 if (!template_args.empty ())
12097 {
12098 gdb_assert (templ_func != NULL);
12099
12100 templ_func->n_template_arguments = template_args.size ();
12101 templ_func->template_arguments
12102 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12103 templ_func->n_template_arguments);
12104 memcpy (templ_func->template_arguments,
12105 template_args.data (),
12106 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12107
12108 /* Make sure that the symtab is set on the new symbols. Even
12109 though they don't appear in this symtab directly, other parts
12110 of gdb assume that symbols do, and this is reasonably
12111 true. */
12112 for (symbol *sym : template_args)
12113 sym->set_symtab (templ_func->symtab ());
12114 }
12115
12116 /* In C++, we can have functions nested inside functions (e.g., when
12117 a function declares a class that has methods). This means that
12118 when we finish processing a function scope, we may need to go
12119 back to building a containing block's symbol lists. */
12120 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12121 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12122
12123 /* If we've finished processing a top-level function, subsequent
12124 symbols go in the file symbol list. */
12125 if (cu->get_builder ()->outermost_context_p ())
12126 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12127 }
12128
12129 /* Process all the DIES contained within a lexical block scope. Start
12130 a new scope, process the dies, and then close the scope. */
12131
12132 static void
12133 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12134 {
12135 struct objfile *objfile = cu->per_objfile->objfile;
12136 struct gdbarch *gdbarch = objfile->arch ();
12137 CORE_ADDR lowpc, highpc;
12138 struct die_info *child_die;
12139 CORE_ADDR baseaddr;
12140
12141 baseaddr = objfile->text_section_offset ();
12142
12143 /* Ignore blocks with missing or invalid low and high pc attributes. */
12144 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12145 as multiple lexical blocks? Handling children in a sane way would
12146 be nasty. Might be easier to properly extend generic blocks to
12147 describe ranges. */
12148 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
12149 {
12150 case PC_BOUNDS_NOT_PRESENT:
12151 /* DW_TAG_lexical_block has no attributes, process its children as if
12152 there was no wrapping by that DW_TAG_lexical_block.
12153 GCC does no longer produces such DWARF since GCC r224161. */
12154 for (child_die = die->child;
12155 child_die != NULL && child_die->tag;
12156 child_die = child_die->sibling)
12157 {
12158 /* We might already be processing this DIE. This can happen
12159 in an unusual circumstance -- where a subroutine A
12160 appears lexically in another subroutine B, but A actually
12161 inlines B. The recursion is broken here, rather than in
12162 inherit_abstract_dies, because it seems better to simply
12163 drop concrete children here. */
12164 if (!child_die->in_process)
12165 process_die (child_die, cu);
12166 }
12167 return;
12168 case PC_BOUNDS_INVALID:
12169 return;
12170 }
12171 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12172 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12173
12174 cu->get_builder ()->push_context (0, lowpc);
12175 if (die->child != NULL)
12176 {
12177 child_die = die->child;
12178 while (child_die && child_die->tag)
12179 {
12180 process_die (child_die, cu);
12181 child_die = child_die->sibling;
12182 }
12183 }
12184 inherit_abstract_dies (die, cu);
12185 struct context_stack cstk = cu->get_builder ()->pop_context ();
12186
12187 if (*cu->get_builder ()->get_local_symbols () != NULL
12188 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12189 {
12190 struct block *block
12191 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12192 cstk.start_addr, highpc);
12193
12194 /* Note that recording ranges after traversing children, as we
12195 do here, means that recording a parent's ranges entails
12196 walking across all its children's ranges as they appear in
12197 the address map, which is quadratic behavior.
12198
12199 It would be nicer to record the parent's ranges before
12200 traversing its children, simply overriding whatever you find
12201 there. But since we don't even decide whether to create a
12202 block until after we've traversed its children, that's hard
12203 to do. */
12204 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12205 }
12206 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12207 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12208 }
12209
12210 static void dwarf2_ranges_read_low_addrs (unsigned offset,
12211 struct dwarf2_cu *cu,
12212 dwarf_tag tag,
12213 std::vector<CORE_ADDR> &result);
12214
12215 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12216
12217 static void
12218 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12219 {
12220 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12221 struct objfile *objfile = per_objfile->objfile;
12222 struct gdbarch *gdbarch = objfile->arch ();
12223 CORE_ADDR pc, baseaddr;
12224 struct attribute *attr;
12225 void **slot;
12226 int nparams;
12227 struct die_info *child_die;
12228
12229 baseaddr = objfile->text_section_offset ();
12230
12231 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12232 if (attr == NULL)
12233 {
12234 /* This was a pre-DWARF-5 GNU extension alias
12235 for DW_AT_call_return_pc. */
12236 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12237 }
12238 if (!attr)
12239 {
12240 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12241 "DIE %s [in module %s]"),
12242 sect_offset_str (die->sect_off), objfile_name (objfile));
12243 return;
12244 }
12245 pc = attr->as_address () + baseaddr;
12246 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12247 pc -= baseaddr;
12248
12249 if (cu->call_site_htab == NULL)
12250 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
12251 call_site::eq, NULL,
12252 &objfile->objfile_obstack,
12253 hashtab_obstack_allocate, NULL);
12254 struct call_site call_site_local (pc, nullptr, nullptr);
12255 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12256 if (*slot != NULL)
12257 {
12258 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12259 "DIE %s [in module %s]"),
12260 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
12261 objfile_name (objfile));
12262 return;
12263 }
12264
12265 /* Count parameters at the caller. */
12266
12267 nparams = 0;
12268 for (child_die = die->child; child_die && child_die->tag;
12269 child_die = child_die->sibling)
12270 {
12271 if (child_die->tag != DW_TAG_call_site_parameter
12272 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12273 {
12274 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12275 "DW_TAG_call_site child DIE %s [in module %s]"),
12276 child_die->tag, sect_offset_str (child_die->sect_off),
12277 objfile_name (objfile));
12278 continue;
12279 }
12280
12281 nparams++;
12282 }
12283
12284 struct call_site *call_site
12285 = new (XOBNEWVAR (&objfile->objfile_obstack,
12286 struct call_site,
12287 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
12288 struct call_site (pc, cu->per_cu, per_objfile);
12289 *slot = call_site;
12290
12291 /* We never call the destructor of call_site, so we must ensure it is
12292 trivially destructible. */
12293 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
12294
12295 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12296 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12297 {
12298 struct die_info *func_die;
12299
12300 /* Skip also over DW_TAG_inlined_subroutine. */
12301 for (func_die = die->parent;
12302 func_die && func_die->tag != DW_TAG_subprogram
12303 && func_die->tag != DW_TAG_subroutine_type;
12304 func_die = func_die->parent);
12305
12306 /* DW_AT_call_all_calls is a superset
12307 of DW_AT_call_all_tail_calls. */
12308 if (func_die
12309 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12310 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12311 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12312 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12313 {
12314 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12315 not complete. But keep CALL_SITE for look ups via call_site_htab,
12316 both the initial caller containing the real return address PC and
12317 the final callee containing the current PC of a chain of tail
12318 calls do not need to have the tail call list complete. But any
12319 function candidate for a virtual tail call frame searched via
12320 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12321 determined unambiguously. */
12322 }
12323 else
12324 {
12325 struct type *func_type = NULL;
12326
12327 if (func_die)
12328 func_type = get_die_type (func_die, cu);
12329 if (func_type != NULL)
12330 {
12331 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
12332
12333 /* Enlist this call site to the function. */
12334 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12335 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12336 }
12337 else
12338 complaint (_("Cannot find function owning DW_TAG_call_site "
12339 "DIE %s [in module %s]"),
12340 sect_offset_str (die->sect_off), objfile_name (objfile));
12341 }
12342 }
12343
12344 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12345 if (attr == NULL)
12346 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12347 if (attr == NULL)
12348 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12349 if (attr == NULL)
12350 {
12351 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12352 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12353 }
12354
12355 call_site->target.set_loc_dwarf_block (nullptr);
12356 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
12357 /* Keep NULL DWARF_BLOCK. */;
12358 else if (attr->form_is_block ())
12359 {
12360 struct dwarf2_locexpr_baton *dlbaton;
12361 struct dwarf_block *block = attr->as_block ();
12362
12363 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12364 dlbaton->data = block->data;
12365 dlbaton->size = block->size;
12366 dlbaton->per_objfile = per_objfile;
12367 dlbaton->per_cu = cu->per_cu;
12368
12369 call_site->target.set_loc_dwarf_block (dlbaton);
12370 }
12371 else if (attr->form_is_ref ())
12372 {
12373 struct dwarf2_cu *target_cu = cu;
12374 struct die_info *target_die;
12375
12376 target_die = follow_die_ref (die, attr, &target_cu);
12377 gdb_assert (target_cu->per_objfile->objfile == objfile);
12378
12379 struct attribute *ranges_attr
12380 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
12381
12382 if (die_is_declaration (target_die, target_cu))
12383 {
12384 const char *target_physname;
12385
12386 /* Prefer the mangled name; otherwise compute the demangled one. */
12387 target_physname = dw2_linkage_name (target_die, target_cu);
12388 if (target_physname == NULL)
12389 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12390 if (target_physname == NULL)
12391 complaint (_("DW_AT_call_target target DIE has invalid "
12392 "physname, for referencing DIE %s [in module %s]"),
12393 sect_offset_str (die->sect_off), objfile_name (objfile));
12394 else
12395 call_site->target.set_loc_physname (target_physname);
12396 }
12397 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
12398 {
12399 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
12400 + target_cu->gnu_ranges_base);
12401 std::vector<CORE_ADDR> addresses;
12402 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
12403 target_die->tag, addresses);
12404 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
12405 addresses.size ());
12406 std::copy (addresses.begin (), addresses.end (), saved);
12407 call_site->target.set_loc_array (addresses.size (), saved);
12408 }
12409 else
12410 {
12411 CORE_ADDR lowpc;
12412
12413 /* DW_AT_entry_pc should be preferred. */
12414 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
12415 nullptr, nullptr)
12416 <= PC_BOUNDS_INVALID)
12417 complaint (_("DW_AT_call_target target DIE has invalid "
12418 "low pc, for referencing DIE %s [in module %s]"),
12419 sect_offset_str (die->sect_off), objfile_name (objfile));
12420 else
12421 {
12422 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
12423 - baseaddr);
12424 call_site->target.set_loc_physaddr (lowpc);
12425 }
12426 }
12427 }
12428 else
12429 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12430 "block nor reference, for DIE %s [in module %s]"),
12431 sect_offset_str (die->sect_off), objfile_name (objfile));
12432
12433 for (child_die = die->child;
12434 child_die && child_die->tag;
12435 child_die = child_die->sibling)
12436 {
12437 struct call_site_parameter *parameter;
12438 struct attribute *loc, *origin;
12439
12440 if (child_die->tag != DW_TAG_call_site_parameter
12441 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12442 {
12443 /* Already printed the complaint above. */
12444 continue;
12445 }
12446
12447 gdb_assert (call_site->parameter_count < nparams);
12448 parameter = &call_site->parameter[call_site->parameter_count];
12449
12450 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12451 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12452 register is contained in DW_AT_call_value. */
12453
12454 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12455 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12456 if (origin == NULL)
12457 {
12458 /* This was a pre-DWARF-5 GNU extension alias
12459 for DW_AT_call_parameter. */
12460 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12461 }
12462 if (loc == NULL && origin != NULL && origin->form_is_ref ())
12463 {
12464 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12465
12466 sect_offset sect_off = origin->get_ref_die_offset ();
12467 if (!cu->header.offset_in_cu_p (sect_off))
12468 {
12469 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12470 binding can be done only inside one CU. Such referenced DIE
12471 therefore cannot be even moved to DW_TAG_partial_unit. */
12472 complaint (_("DW_AT_call_parameter offset is not in CU for "
12473 "DW_TAG_call_site child DIE %s [in module %s]"),
12474 sect_offset_str (child_die->sect_off),
12475 objfile_name (objfile));
12476 continue;
12477 }
12478 parameter->u.param_cu_off
12479 = (cu_offset) (sect_off - cu->header.sect_off);
12480 }
12481 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
12482 {
12483 complaint (_("No DW_FORM_block* DW_AT_location for "
12484 "DW_TAG_call_site child DIE %s [in module %s]"),
12485 sect_offset_str (child_die->sect_off), objfile_name (objfile));
12486 continue;
12487 }
12488 else
12489 {
12490 struct dwarf_block *block = loc->as_block ();
12491
12492 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12493 (block->data, &block->data[block->size]);
12494 if (parameter->u.dwarf_reg != -1)
12495 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12496 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
12497 &block->data[block->size],
12498 &parameter->u.fb_offset))
12499 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12500 else
12501 {
12502 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12503 "for DW_FORM_block* DW_AT_location is supported for "
12504 "DW_TAG_call_site child DIE %s "
12505 "[in module %s]"),
12506 sect_offset_str (child_die->sect_off),
12507 objfile_name (objfile));
12508 continue;
12509 }
12510 }
12511
12512 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12513 if (attr == NULL)
12514 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12515 if (attr == NULL || !attr->form_is_block ())
12516 {
12517 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12518 "DW_TAG_call_site child DIE %s [in module %s]"),
12519 sect_offset_str (child_die->sect_off),
12520 objfile_name (objfile));
12521 continue;
12522 }
12523
12524 struct dwarf_block *block = attr->as_block ();
12525 parameter->value = block->data;
12526 parameter->value_size = block->size;
12527
12528 /* Parameters are not pre-cleared by memset above. */
12529 parameter->data_value = NULL;
12530 parameter->data_value_size = 0;
12531 call_site->parameter_count++;
12532
12533 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12534 if (attr == NULL)
12535 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12536 if (attr != nullptr)
12537 {
12538 if (!attr->form_is_block ())
12539 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12540 "DW_TAG_call_site child DIE %s [in module %s]"),
12541 sect_offset_str (child_die->sect_off),
12542 objfile_name (objfile));
12543 else
12544 {
12545 block = attr->as_block ();
12546 parameter->data_value = block->data;
12547 parameter->data_value_size = block->size;
12548 }
12549 }
12550 }
12551 }
12552
12553 /* Helper function for read_variable. If DIE represents a virtual
12554 table, then return the type of the concrete object that is
12555 associated with the virtual table. Otherwise, return NULL. */
12556
12557 static struct type *
12558 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12559 {
12560 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12561 if (attr == NULL)
12562 return NULL;
12563
12564 /* Find the type DIE. */
12565 struct die_info *type_die = NULL;
12566 struct dwarf2_cu *type_cu = cu;
12567
12568 if (attr->form_is_ref ())
12569 type_die = follow_die_ref (die, attr, &type_cu);
12570 if (type_die == NULL)
12571 return NULL;
12572
12573 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12574 return NULL;
12575 return die_containing_type (type_die, type_cu);
12576 }
12577
12578 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12579
12580 static void
12581 read_variable (struct die_info *die, struct dwarf2_cu *cu)
12582 {
12583 struct rust_vtable_symbol *storage = NULL;
12584
12585 if (cu->lang () == language_rust)
12586 {
12587 struct type *containing_type = rust_containing_type (die, cu);
12588
12589 if (containing_type != NULL)
12590 {
12591 struct objfile *objfile = cu->per_objfile->objfile;
12592
12593 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
12594 storage->concrete_type = containing_type;
12595 storage->subclass = SYMBOL_RUST_VTABLE;
12596 }
12597 }
12598
12599 struct symbol *res = new_symbol (die, NULL, cu, storage);
12600 struct attribute *abstract_origin
12601 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12602 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
12603 if (res == NULL && loc && abstract_origin)
12604 {
12605 /* We have a variable without a name, but with a location and an abstract
12606 origin. This may be a concrete instance of an abstract variable
12607 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12608 later. */
12609 struct dwarf2_cu *origin_cu = cu;
12610 struct die_info *origin_die
12611 = follow_die_ref (die, abstract_origin, &origin_cu);
12612 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12613 per_objfile->per_bfd->abstract_to_concrete
12614 [origin_die->sect_off].push_back (die->sect_off);
12615 }
12616 }
12617
12618 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12619 reading .debug_rnglists.
12620 Callback's type should be:
12621 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12622 Return true if the attributes are present and valid, otherwise,
12623 return false. */
12624
12625 template <typename Callback>
12626 static bool
12627 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12628 dwarf_tag tag, Callback &&callback)
12629 {
12630 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12631 struct objfile *objfile = per_objfile->objfile;
12632 bfd *obfd = objfile->obfd;
12633 /* Base address selection entry. */
12634 gdb::optional<CORE_ADDR> base;
12635 const gdb_byte *buffer;
12636 bool overflow = false;
12637 ULONGEST addr_index;
12638 struct dwarf2_section_info *rnglists_section;
12639
12640 base = cu->base_address;
12641 rnglists_section = cu_debug_rnglists_section (cu, tag);
12642 rnglists_section->read (objfile);
12643
12644 if (offset >= rnglists_section->size)
12645 {
12646 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12647 offset);
12648 return false;
12649 }
12650 buffer = rnglists_section->buffer + offset;
12651
12652 while (1)
12653 {
12654 /* Initialize it due to a false compiler warning. */
12655 CORE_ADDR range_beginning = 0, range_end = 0;
12656 const gdb_byte *buf_end = (rnglists_section->buffer
12657 + rnglists_section->size);
12658 unsigned int bytes_read;
12659
12660 if (buffer == buf_end)
12661 {
12662 overflow = true;
12663 break;
12664 }
12665 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12666 switch (rlet)
12667 {
12668 case DW_RLE_end_of_list:
12669 break;
12670 case DW_RLE_base_address:
12671 if (buffer + cu->header.addr_size > buf_end)
12672 {
12673 overflow = true;
12674 break;
12675 }
12676 base = cu->header.read_address (obfd, buffer, &bytes_read);
12677 buffer += bytes_read;
12678 break;
12679 case DW_RLE_base_addressx:
12680 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12681 buffer += bytes_read;
12682 base = read_addr_index (cu, addr_index);
12683 break;
12684 case DW_RLE_start_length:
12685 if (buffer + cu->header.addr_size > buf_end)
12686 {
12687 overflow = true;
12688 break;
12689 }
12690 range_beginning = cu->header.read_address (obfd, buffer,
12691 &bytes_read);
12692 buffer += bytes_read;
12693 range_end = (range_beginning
12694 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12695 buffer += bytes_read;
12696 if (buffer > buf_end)
12697 {
12698 overflow = true;
12699 break;
12700 }
12701 break;
12702 case DW_RLE_startx_length:
12703 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12704 buffer += bytes_read;
12705 range_beginning = read_addr_index (cu, addr_index);
12706 if (buffer > buf_end)
12707 {
12708 overflow = true;
12709 break;
12710 }
12711 range_end = (range_beginning
12712 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12713 buffer += bytes_read;
12714 break;
12715 case DW_RLE_offset_pair:
12716 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12717 buffer += bytes_read;
12718 if (buffer > buf_end)
12719 {
12720 overflow = true;
12721 break;
12722 }
12723 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12724 buffer += bytes_read;
12725 if (buffer > buf_end)
12726 {
12727 overflow = true;
12728 break;
12729 }
12730 break;
12731 case DW_RLE_start_end:
12732 if (buffer + 2 * cu->header.addr_size > buf_end)
12733 {
12734 overflow = true;
12735 break;
12736 }
12737 range_beginning = cu->header.read_address (obfd, buffer,
12738 &bytes_read);
12739 buffer += bytes_read;
12740 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
12741 buffer += bytes_read;
12742 break;
12743 case DW_RLE_startx_endx:
12744 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12745 buffer += bytes_read;
12746 range_beginning = read_addr_index (cu, addr_index);
12747 if (buffer > buf_end)
12748 {
12749 overflow = true;
12750 break;
12751 }
12752 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12753 buffer += bytes_read;
12754 range_end = read_addr_index (cu, addr_index);
12755 break;
12756 default:
12757 complaint (_("Invalid .debug_rnglists data (no base address)"));
12758 return false;
12759 }
12760 if (rlet == DW_RLE_end_of_list || overflow)
12761 break;
12762 if (rlet == DW_RLE_base_address)
12763 continue;
12764
12765 if (range_beginning > range_end)
12766 {
12767 /* Inverted range entries are invalid. */
12768 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12769 return false;
12770 }
12771
12772 /* Empty range entries have no effect. */
12773 if (range_beginning == range_end)
12774 continue;
12775
12776 /* Only DW_RLE_offset_pair needs the base address added. */
12777 if (rlet == DW_RLE_offset_pair)
12778 {
12779 if (!base.has_value ())
12780 {
12781 /* We have no valid base address for the DW_RLE_offset_pair. */
12782 complaint (_("Invalid .debug_rnglists data (no base address for "
12783 "DW_RLE_offset_pair)"));
12784 return false;
12785 }
12786
12787 range_beginning += *base;
12788 range_end += *base;
12789 }
12790
12791 /* A not-uncommon case of bad debug info.
12792 Don't pollute the addrmap with bad data. */
12793 if (range_beginning == 0
12794 && !per_objfile->per_bfd->has_section_at_zero)
12795 {
12796 complaint (_(".debug_rnglists entry has start address of zero"
12797 " [in module %s]"), objfile_name (objfile));
12798 continue;
12799 }
12800
12801 callback (range_beginning, range_end);
12802 }
12803
12804 if (overflow)
12805 {
12806 complaint (_("Offset %d is not terminated "
12807 "for DW_AT_ranges attribute"),
12808 offset);
12809 return false;
12810 }
12811
12812 return true;
12813 }
12814
12815 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12816 Callback's type should be:
12817 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12818 Return 1 if the attributes are present and valid, otherwise, return 0. */
12819
12820 template <typename Callback>
12821 static int
12822 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
12823 Callback &&callback)
12824 {
12825 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12826 struct objfile *objfile = per_objfile->objfile;
12827 struct comp_unit_head *cu_header = &cu->header;
12828 bfd *obfd = objfile->obfd;
12829 unsigned int addr_size = cu_header->addr_size;
12830 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12831 /* Base address selection entry. */
12832 gdb::optional<CORE_ADDR> base;
12833 unsigned int dummy;
12834 const gdb_byte *buffer;
12835
12836 if (cu_header->version >= 5)
12837 return dwarf2_rnglists_process (offset, cu, tag, callback);
12838
12839 base = cu->base_address;
12840
12841 per_objfile->per_bfd->ranges.read (objfile);
12842 if (offset >= per_objfile->per_bfd->ranges.size)
12843 {
12844 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12845 offset);
12846 return 0;
12847 }
12848 buffer = per_objfile->per_bfd->ranges.buffer + offset;
12849
12850 while (1)
12851 {
12852 CORE_ADDR range_beginning, range_end;
12853
12854 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
12855 buffer += addr_size;
12856 range_end = cu->header.read_address (obfd, buffer, &dummy);
12857 buffer += addr_size;
12858 offset += 2 * addr_size;
12859
12860 /* An end of list marker is a pair of zero addresses. */
12861 if (range_beginning == 0 && range_end == 0)
12862 /* Found the end of list entry. */
12863 break;
12864
12865 /* Each base address selection entry is a pair of 2 values.
12866 The first is the largest possible address, the second is
12867 the base address. Check for a base address here. */
12868 if ((range_beginning & mask) == mask)
12869 {
12870 /* If we found the largest possible address, then we already
12871 have the base address in range_end. */
12872 base = range_end;
12873 continue;
12874 }
12875
12876 if (!base.has_value ())
12877 {
12878 /* We have no valid base address for the ranges
12879 data. */
12880 complaint (_("Invalid .debug_ranges data (no base address)"));
12881 return 0;
12882 }
12883
12884 if (range_beginning > range_end)
12885 {
12886 /* Inverted range entries are invalid. */
12887 complaint (_("Invalid .debug_ranges data (inverted range)"));
12888 return 0;
12889 }
12890
12891 /* Empty range entries have no effect. */
12892 if (range_beginning == range_end)
12893 continue;
12894
12895 range_beginning += *base;
12896 range_end += *base;
12897
12898 /* A not-uncommon case of bad debug info.
12899 Don't pollute the addrmap with bad data. */
12900 if (range_beginning == 0
12901 && !per_objfile->per_bfd->has_section_at_zero)
12902 {
12903 complaint (_(".debug_ranges entry has start address of zero"
12904 " [in module %s]"), objfile_name (objfile));
12905 continue;
12906 }
12907
12908 callback (range_beginning, range_end);
12909 }
12910
12911 return 1;
12912 }
12913
12914 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12915 Return 1 if the attributes are present and valid, otherwise, return 0.
12916 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12917 ranges in MAP are set, using DATUM as the value. */
12918
12919 static int
12920 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12921 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12922 addrmap *map, void *datum, dwarf_tag tag)
12923 {
12924 struct objfile *objfile = cu->per_objfile->objfile;
12925 struct gdbarch *gdbarch = objfile->arch ();
12926 const CORE_ADDR baseaddr = objfile->text_section_offset ();
12927 int low_set = 0;
12928 CORE_ADDR low = 0;
12929 CORE_ADDR high = 0;
12930 int retval;
12931
12932 retval = dwarf2_ranges_process (offset, cu, tag,
12933 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12934 {
12935 if (map != nullptr)
12936 {
12937 CORE_ADDR lowpc;
12938 CORE_ADDR highpc;
12939
12940 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
12941 range_beginning + baseaddr)
12942 - baseaddr);
12943 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
12944 range_end + baseaddr)
12945 - baseaddr);
12946 map->set_empty (lowpc, highpc - 1, datum);
12947 }
12948
12949 /* FIXME: This is recording everything as a low-high
12950 segment of consecutive addresses. We should have a
12951 data structure for discontiguous block ranges
12952 instead. */
12953 if (! low_set)
12954 {
12955 low = range_beginning;
12956 high = range_end;
12957 low_set = 1;
12958 }
12959 else
12960 {
12961 if (range_beginning < low)
12962 low = range_beginning;
12963 if (range_end > high)
12964 high = range_end;
12965 }
12966 });
12967 if (!retval)
12968 return 0;
12969
12970 if (! low_set)
12971 /* If the first entry is an end-of-list marker, the range
12972 describes an empty scope, i.e. no instructions. */
12973 return 0;
12974
12975 if (low_return)
12976 *low_return = low;
12977 if (high_return)
12978 *high_return = high;
12979 return 1;
12980 }
12981
12982 /* Process ranges and fill in a vector of the low PC values only. */
12983
12984 static void
12985 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
12986 dwarf_tag tag,
12987 std::vector<CORE_ADDR> &result)
12988 {
12989 dwarf2_ranges_process (offset, cu, tag,
12990 [&] (CORE_ADDR start, CORE_ADDR end)
12991 {
12992 result.push_back (start);
12993 });
12994 }
12995
12996 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12997 definition for the return value. *LOWPC and *HIGHPC are set iff
12998 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12999
13000 static enum pc_bounds_kind
13001 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13002 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13003 addrmap *map, void *datum)
13004 {
13005 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13006 struct attribute *attr;
13007 struct attribute *attr_high;
13008 CORE_ADDR low = 0;
13009 CORE_ADDR high = 0;
13010 enum pc_bounds_kind ret;
13011
13012 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13013 if (attr_high)
13014 {
13015 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13016 if (attr != nullptr)
13017 {
13018 low = attr->as_address ();
13019 high = attr_high->as_address ();
13020 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13021 high += low;
13022 }
13023 else
13024 /* Found high w/o low attribute. */
13025 return PC_BOUNDS_INVALID;
13026
13027 /* Found consecutive range of addresses. */
13028 ret = PC_BOUNDS_HIGH_LOW;
13029 }
13030 else
13031 {
13032 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13033 if (attr != nullptr && attr->form_is_unsigned ())
13034 {
13035 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13036 on DWARF version). */
13037 ULONGEST ranges_offset = attr->as_unsigned ();
13038
13039 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13040 this value. */
13041 if (die->tag != DW_TAG_compile_unit)
13042 ranges_offset += cu->gnu_ranges_base;
13043
13044 /* Value of the DW_AT_ranges attribute is the offset in the
13045 .debug_ranges section. */
13046 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
13047 map, datum, die->tag))
13048 return PC_BOUNDS_INVALID;
13049 /* Found discontinuous range of addresses. */
13050 ret = PC_BOUNDS_RANGES;
13051 }
13052 else
13053 return PC_BOUNDS_NOT_PRESENT;
13054 }
13055
13056 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13057 if (high <= low)
13058 return PC_BOUNDS_INVALID;
13059
13060 /* When using the GNU linker, .gnu.linkonce. sections are used to
13061 eliminate duplicate copies of functions and vtables and such.
13062 The linker will arbitrarily choose one and discard the others.
13063 The AT_*_pc values for such functions refer to local labels in
13064 these sections. If the section from that file was discarded, the
13065 labels are not in the output, so the relocs get a value of 0.
13066 If this is a discarded function, mark the pc bounds as invalid,
13067 so that GDB will ignore it. */
13068 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
13069 return PC_BOUNDS_INVALID;
13070
13071 *lowpc = low;
13072 if (highpc)
13073 *highpc = high;
13074 return ret;
13075 }
13076
13077 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13078 its low and high PC addresses. Do nothing if these addresses could not
13079 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13080 and HIGHPC to the high address if greater than HIGHPC. */
13081
13082 static void
13083 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13084 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13085 struct dwarf2_cu *cu)
13086 {
13087 CORE_ADDR low, high;
13088 struct die_info *child = die->child;
13089
13090 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
13091 >= PC_BOUNDS_RANGES)
13092 {
13093 *lowpc = std::min (*lowpc, low);
13094 *highpc = std::max (*highpc, high);
13095 }
13096
13097 /* If the language does not allow nested subprograms (either inside
13098 subprograms or lexical blocks), we're done. */
13099 if (cu->lang () != language_ada)
13100 return;
13101
13102 /* Check all the children of the given DIE. If it contains nested
13103 subprograms, then check their pc bounds. Likewise, we need to
13104 check lexical blocks as well, as they may also contain subprogram
13105 definitions. */
13106 while (child && child->tag)
13107 {
13108 if (child->tag == DW_TAG_subprogram
13109 || child->tag == DW_TAG_lexical_block)
13110 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13111 child = child->sibling;
13112 }
13113 }
13114
13115 /* Get the low and high pc's represented by the scope DIE, and store
13116 them in *LOWPC and *HIGHPC. If the correct values can't be
13117 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13118
13119 static void
13120 get_scope_pc_bounds (struct die_info *die,
13121 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13122 struct dwarf2_cu *cu)
13123 {
13124 CORE_ADDR best_low = (CORE_ADDR) -1;
13125 CORE_ADDR best_high = (CORE_ADDR) 0;
13126 CORE_ADDR current_low, current_high;
13127
13128 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
13129 nullptr, nullptr)
13130 >= PC_BOUNDS_RANGES)
13131 {
13132 best_low = current_low;
13133 best_high = current_high;
13134 }
13135 else
13136 {
13137 struct die_info *child = die->child;
13138
13139 while (child && child->tag)
13140 {
13141 switch (child->tag) {
13142 case DW_TAG_subprogram:
13143 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13144 break;
13145 case DW_TAG_namespace:
13146 case DW_TAG_module:
13147 /* FIXME: carlton/2004-01-16: Should we do this for
13148 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13149 that current GCC's always emit the DIEs corresponding
13150 to definitions of methods of classes as children of a
13151 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13152 the DIEs giving the declarations, which could be
13153 anywhere). But I don't see any reason why the
13154 standards says that they have to be there. */
13155 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13156
13157 if (current_low != ((CORE_ADDR) -1))
13158 {
13159 best_low = std::min (best_low, current_low);
13160 best_high = std::max (best_high, current_high);
13161 }
13162 break;
13163 default:
13164 /* Ignore. */
13165 break;
13166 }
13167
13168 child = child->sibling;
13169 }
13170 }
13171
13172 *lowpc = best_low;
13173 *highpc = best_high;
13174 }
13175
13176 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13177 in DIE. */
13178
13179 static void
13180 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13181 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13182 {
13183 struct objfile *objfile = cu->per_objfile->objfile;
13184 struct gdbarch *gdbarch = objfile->arch ();
13185 struct attribute *attr;
13186 struct attribute *attr_high;
13187
13188 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13189 if (attr_high)
13190 {
13191 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13192 if (attr != nullptr)
13193 {
13194 CORE_ADDR low = attr->as_address ();
13195 CORE_ADDR high = attr_high->as_address ();
13196
13197 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13198 high += low;
13199
13200 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13201 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13202 cu->get_builder ()->record_block_range (block, low, high - 1);
13203 }
13204 }
13205
13206 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13207 if (attr != nullptr && attr->form_is_unsigned ())
13208 {
13209 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13210 on DWARF version). */
13211 ULONGEST ranges_offset = attr->as_unsigned ();
13212
13213 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13214 this value. */
13215 if (die->tag != DW_TAG_compile_unit)
13216 ranges_offset += cu->gnu_ranges_base;
13217
13218 std::vector<blockrange> blockvec;
13219 dwarf2_ranges_process (ranges_offset, cu, die->tag,
13220 [&] (CORE_ADDR start, CORE_ADDR end)
13221 {
13222 start += baseaddr;
13223 end += baseaddr;
13224 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13225 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13226 cu->get_builder ()->record_block_range (block, start, end - 1);
13227 blockvec.emplace_back (start, end);
13228 });
13229
13230 block->set_ranges (make_blockranges (objfile, blockvec));
13231 }
13232 }
13233
13234 /* Check whether the producer field indicates either of GCC < 4.6, or the
13235 Intel C/C++ compiler, and cache the result in CU. */
13236
13237 static void
13238 check_producer (struct dwarf2_cu *cu)
13239 {
13240 int major, minor;
13241
13242 if (cu->producer == NULL)
13243 {
13244 /* For unknown compilers expect their behavior is DWARF version
13245 compliant.
13246
13247 GCC started to support .debug_types sections by -gdwarf-4 since
13248 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13249 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13250 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13251 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13252 }
13253 else if (producer_is_gcc (cu->producer, &major, &minor))
13254 {
13255 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13256 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13257 cu->producer_is_gcc_11 = major == 11;
13258 }
13259 else if (producer_is_icc (cu->producer, &major, &minor))
13260 {
13261 cu->producer_is_icc = true;
13262 cu->producer_is_icc_lt_14 = major < 14;
13263 }
13264 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13265 cu->producer_is_codewarrior = true;
13266 else
13267 {
13268 /* For other non-GCC compilers, expect their behavior is DWARF version
13269 compliant. */
13270 }
13271
13272 cu->checked_producer = true;
13273 }
13274
13275 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13276 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13277 during 4.6.0 experimental. */
13278
13279 static bool
13280 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13281 {
13282 if (!cu->checked_producer)
13283 check_producer (cu);
13284
13285 return cu->producer_is_gxx_lt_4_6;
13286 }
13287
13288
13289 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13290 with incorrect is_stmt attributes. */
13291
13292 static bool
13293 producer_is_codewarrior (struct dwarf2_cu *cu)
13294 {
13295 if (!cu->checked_producer)
13296 check_producer (cu);
13297
13298 return cu->producer_is_codewarrior;
13299 }
13300
13301 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13302 If that attribute is not available, return the appropriate
13303 default. */
13304
13305 static enum dwarf_access_attribute
13306 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13307 {
13308 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13309 if (attr != nullptr)
13310 {
13311 LONGEST value = attr->constant_value (-1);
13312 if (value == DW_ACCESS_public
13313 || value == DW_ACCESS_protected
13314 || value == DW_ACCESS_private)
13315 return (dwarf_access_attribute) value;
13316 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13317 plongest (value));
13318 }
13319
13320 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13321 {
13322 /* The default DWARF 2 accessibility for members is public, the default
13323 accessibility for inheritance is private. */
13324
13325 if (die->tag != DW_TAG_inheritance)
13326 return DW_ACCESS_public;
13327 else
13328 return DW_ACCESS_private;
13329 }
13330 else
13331 {
13332 /* DWARF 3+ defines the default accessibility a different way. The same
13333 rules apply now for DW_TAG_inheritance as for the members and it only
13334 depends on the container kind. */
13335
13336 if (die->parent->tag == DW_TAG_class_type)
13337 return DW_ACCESS_private;
13338 else
13339 return DW_ACCESS_public;
13340 }
13341 }
13342
13343 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13344 *OFFSET to the byte offset. If the attribute was not found return
13345 0, otherwise return 1. If it was found but could not properly be
13346 handled, set *OFFSET to 0. */
13347
13348 static int
13349 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13350 LONGEST *offset)
13351 {
13352 struct attribute *attr;
13353
13354 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13355 if (attr != NULL)
13356 {
13357 *offset = 0;
13358
13359 /* Note that we do not check for a section offset first here.
13360 This is because DW_AT_data_member_location is new in DWARF 4,
13361 so if we see it, we can assume that a constant form is really
13362 a constant and not a section offset. */
13363 if (attr->form_is_constant ())
13364 *offset = attr->constant_value (0);
13365 else if (attr->form_is_section_offset ())
13366 dwarf2_complex_location_expr_complaint ();
13367 else if (attr->form_is_block ())
13368 *offset = decode_locdesc (attr->as_block (), cu);
13369 else
13370 dwarf2_complex_location_expr_complaint ();
13371
13372 return 1;
13373 }
13374 else
13375 {
13376 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13377 if (attr != nullptr)
13378 {
13379 *offset = attr->constant_value (0);
13380 return 1;
13381 }
13382 }
13383
13384 return 0;
13385 }
13386
13387 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13388 store the results in FIELD. */
13389
13390 static void
13391 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13392 struct field *field)
13393 {
13394 struct attribute *attr;
13395
13396 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13397 if (attr != NULL)
13398 {
13399 if (attr->form_is_constant ())
13400 {
13401 LONGEST offset = attr->constant_value (0);
13402
13403 /* Work around this GCC 11 bug, where it would erroneously use -1
13404 data member locations, instead of 0:
13405
13406 Negative DW_AT_data_member_location
13407 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13408 */
13409 if (offset == -1 && cu->producer_is_gcc_11)
13410 {
13411 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13412 offset = 0;
13413 }
13414
13415 field->set_loc_bitpos (offset * bits_per_byte);
13416 }
13417 else if (attr->form_is_section_offset ())
13418 dwarf2_complex_location_expr_complaint ();
13419 else if (attr->form_is_block ())
13420 {
13421 bool handled;
13422 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
13423 if (handled)
13424 field->set_loc_bitpos (offset * bits_per_byte);
13425 else
13426 {
13427 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13428 struct objfile *objfile = per_objfile->objfile;
13429 struct dwarf2_locexpr_baton *dlbaton
13430 = XOBNEW (&objfile->objfile_obstack,
13431 struct dwarf2_locexpr_baton);
13432 dlbaton->data = attr->as_block ()->data;
13433 dlbaton->size = attr->as_block ()->size;
13434 /* When using this baton, we want to compute the address
13435 of the field, not the value. This is why
13436 is_reference is set to false here. */
13437 dlbaton->is_reference = false;
13438 dlbaton->per_objfile = per_objfile;
13439 dlbaton->per_cu = cu->per_cu;
13440
13441 field->set_loc_dwarf_block (dlbaton);
13442 }
13443 }
13444 else
13445 dwarf2_complex_location_expr_complaint ();
13446 }
13447 else
13448 {
13449 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13450 if (attr != nullptr)
13451 field->set_loc_bitpos (attr->constant_value (0));
13452 }
13453 }
13454
13455 /* Add an aggregate field to the field list. */
13456
13457 static void
13458 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13459 struct dwarf2_cu *cu)
13460 {
13461 struct objfile *objfile = cu->per_objfile->objfile;
13462 struct gdbarch *gdbarch = objfile->arch ();
13463 struct nextfield *new_field;
13464 struct attribute *attr;
13465 struct field *fp;
13466 const char *fieldname = "";
13467
13468 if (die->tag == DW_TAG_inheritance)
13469 {
13470 fip->baseclasses.emplace_back ();
13471 new_field = &fip->baseclasses.back ();
13472 }
13473 else
13474 {
13475 fip->fields.emplace_back ();
13476 new_field = &fip->fields.back ();
13477 }
13478
13479 new_field->offset = die->sect_off;
13480
13481 new_field->accessibility = dwarf2_access_attribute (die, cu);
13482 if (new_field->accessibility != DW_ACCESS_public)
13483 fip->non_public_fields = true;
13484
13485 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13486 if (attr != nullptr)
13487 new_field->virtuality = attr->as_virtuality ();
13488 else
13489 new_field->virtuality = DW_VIRTUALITY_none;
13490
13491 fp = &new_field->field;
13492
13493 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
13494 && !die_is_declaration (die, cu))
13495 {
13496 if (die->tag == DW_TAG_namelist_item)
13497 {
13498 /* Typically, DW_TAG_namelist_item are references to namelist items.
13499 If so, follow that reference. */
13500 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
13501 struct die_info *item_die = nullptr;
13502 struct dwarf2_cu *item_cu = cu;
13503 if (attr1->form_is_ref ())
13504 item_die = follow_die_ref (die, attr1, &item_cu);
13505 if (item_die != nullptr)
13506 die = item_die;
13507 }
13508 /* Data member other than a C++ static data member. */
13509
13510 /* Get type of field. */
13511 fp->set_type (die_type (die, cu));
13512
13513 fp->set_loc_bitpos (0);
13514
13515 /* Get bit size of field (zero if none). */
13516 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13517 if (attr != nullptr)
13518 {
13519 FIELD_BITSIZE (*fp) = attr->constant_value (0);
13520 }
13521 else
13522 {
13523 FIELD_BITSIZE (*fp) = 0;
13524 }
13525
13526 /* Get bit offset of field. */
13527 handle_member_location (die, cu, fp);
13528 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13529 if (attr != nullptr && attr->form_is_constant ())
13530 {
13531 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
13532 {
13533 /* For big endian bits, the DW_AT_bit_offset gives the
13534 additional bit offset from the MSB of the containing
13535 anonymous object to the MSB of the field. We don't
13536 have to do anything special since we don't need to
13537 know the size of the anonymous object. */
13538 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
13539 }
13540 else
13541 {
13542 /* For little endian bits, compute the bit offset to the
13543 MSB of the anonymous object, subtract off the number of
13544 bits from the MSB of the field to the MSB of the
13545 object, and then subtract off the number of bits of
13546 the field itself. The result is the bit offset of
13547 the LSB of the field. */
13548 int anonymous_size;
13549 int bit_offset = attr->constant_value (0);
13550
13551 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13552 if (attr != nullptr && attr->form_is_constant ())
13553 {
13554 /* The size of the anonymous object containing
13555 the bit field is explicit, so use the
13556 indicated size (in bytes). */
13557 anonymous_size = attr->constant_value (0);
13558 }
13559 else
13560 {
13561 /* The size of the anonymous object containing
13562 the bit field must be inferred from the type
13563 attribute of the data member containing the
13564 bit field. */
13565 anonymous_size = TYPE_LENGTH (fp->type ());
13566 }
13567 fp->set_loc_bitpos (fp->loc_bitpos ()
13568 + anonymous_size * bits_per_byte
13569 - bit_offset - FIELD_BITSIZE (*fp));
13570 }
13571 }
13572
13573 /* Get name of field. */
13574 fieldname = dwarf2_name (die, cu);
13575 if (fieldname == NULL)
13576 fieldname = "";
13577
13578 /* The name is already allocated along with this objfile, so we don't
13579 need to duplicate it for the type. */
13580 fp->set_name (fieldname);
13581
13582 /* Change accessibility for artificial fields (e.g. virtual table
13583 pointer or virtual base class pointer) to private. */
13584 if (dwarf2_attr (die, DW_AT_artificial, cu))
13585 {
13586 FIELD_ARTIFICIAL (*fp) = 1;
13587 new_field->accessibility = DW_ACCESS_private;
13588 fip->non_public_fields = true;
13589 }
13590 }
13591 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13592 {
13593 /* C++ static member. */
13594
13595 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13596 is a declaration, but all versions of G++ as of this writing
13597 (so through at least 3.2.1) incorrectly generate
13598 DW_TAG_variable tags. */
13599
13600 const char *physname;
13601
13602 /* Get name of field. */
13603 fieldname = dwarf2_name (die, cu);
13604 if (fieldname == NULL)
13605 return;
13606
13607 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13608 if (attr
13609 /* Only create a symbol if this is an external value.
13610 new_symbol checks this and puts the value in the global symbol
13611 table, which we want. If it is not external, new_symbol
13612 will try to put the value in cu->list_in_scope which is wrong. */
13613 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13614 {
13615 /* A static const member, not much different than an enum as far as
13616 we're concerned, except that we can support more types. */
13617 new_symbol (die, NULL, cu);
13618 }
13619
13620 /* Get physical name. */
13621 physname = dwarf2_physname (fieldname, die, cu);
13622
13623 /* The name is already allocated along with this objfile, so we don't
13624 need to duplicate it for the type. */
13625 fp->set_loc_physname (physname ? physname : "");
13626 fp->set_type (die_type (die, cu));
13627 fp->set_name (fieldname);
13628 }
13629 else if (die->tag == DW_TAG_inheritance)
13630 {
13631 /* C++ base class field. */
13632 handle_member_location (die, cu, fp);
13633 FIELD_BITSIZE (*fp) = 0;
13634 fp->set_type (die_type (die, cu));
13635 fp->set_name (fp->type ()->name ());
13636 }
13637 else
13638 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13639 }
13640
13641 /* Can the type given by DIE define another type? */
13642
13643 static bool
13644 type_can_define_types (const struct die_info *die)
13645 {
13646 switch (die->tag)
13647 {
13648 case DW_TAG_typedef:
13649 case DW_TAG_class_type:
13650 case DW_TAG_structure_type:
13651 case DW_TAG_union_type:
13652 case DW_TAG_enumeration_type:
13653 return true;
13654
13655 default:
13656 return false;
13657 }
13658 }
13659
13660 /* Add a type definition defined in the scope of the FIP's class. */
13661
13662 static void
13663 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
13664 struct dwarf2_cu *cu)
13665 {
13666 struct decl_field fp;
13667 memset (&fp, 0, sizeof (fp));
13668
13669 gdb_assert (type_can_define_types (die));
13670
13671 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13672 fp.name = dwarf2_name (die, cu);
13673 fp.type = read_type_die (die, cu);
13674
13675 /* Save accessibility. */
13676 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
13677 switch (accessibility)
13678 {
13679 case DW_ACCESS_public:
13680 /* The assumed value if neither private nor protected. */
13681 break;
13682 case DW_ACCESS_private:
13683 fp.is_private = 1;
13684 break;
13685 case DW_ACCESS_protected:
13686 fp.is_protected = 1;
13687 break;
13688 }
13689
13690 if (die->tag == DW_TAG_typedef)
13691 fip->typedef_field_list.push_back (fp);
13692 else
13693 fip->nested_types_list.push_back (fp);
13694 }
13695
13696 /* A convenience typedef that's used when finding the discriminant
13697 field for a variant part. */
13698 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
13699 offset_map_type;
13700
13701 /* Compute the discriminant range for a given variant. OBSTACK is
13702 where the results will be stored. VARIANT is the variant to
13703 process. IS_UNSIGNED indicates whether the discriminant is signed
13704 or unsigned. */
13705
13706 static const gdb::array_view<discriminant_range>
13707 convert_variant_range (struct obstack *obstack, const variant_field &variant,
13708 bool is_unsigned)
13709 {
13710 std::vector<discriminant_range> ranges;
13711
13712 if (variant.default_branch)
13713 return {};
13714
13715 if (variant.discr_list_data == nullptr)
13716 {
13717 discriminant_range r
13718 = {variant.discriminant_value, variant.discriminant_value};
13719 ranges.push_back (r);
13720 }
13721 else
13722 {
13723 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
13724 variant.discr_list_data->size);
13725 while (!data.empty ())
13726 {
13727 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
13728 {
13729 complaint (_("invalid discriminant marker: %d"), data[0]);
13730 break;
13731 }
13732 bool is_range = data[0] == DW_DSC_range;
13733 data = data.slice (1);
13734
13735 ULONGEST low, high;
13736 unsigned int bytes_read;
13737
13738 if (data.empty ())
13739 {
13740 complaint (_("DW_AT_discr_list missing low value"));
13741 break;
13742 }
13743 if (is_unsigned)
13744 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
13745 else
13746 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
13747 &bytes_read);
13748 data = data.slice (bytes_read);
13749
13750 if (is_range)
13751 {
13752 if (data.empty ())
13753 {
13754 complaint (_("DW_AT_discr_list missing high value"));
13755 break;
13756 }
13757 if (is_unsigned)
13758 high = read_unsigned_leb128 (nullptr, data.data (),
13759 &bytes_read);
13760 else
13761 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
13762 &bytes_read);
13763 data = data.slice (bytes_read);
13764 }
13765 else
13766 high = low;
13767
13768 ranges.push_back ({ low, high });
13769 }
13770 }
13771
13772 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
13773 ranges.size ());
13774 std::copy (ranges.begin (), ranges.end (), result);
13775 return gdb::array_view<discriminant_range> (result, ranges.size ());
13776 }
13777
13778 static const gdb::array_view<variant_part> create_variant_parts
13779 (struct obstack *obstack,
13780 const offset_map_type &offset_map,
13781 struct field_info *fi,
13782 const std::vector<variant_part_builder> &variant_parts);
13783
13784 /* Fill in a "struct variant" for a given variant field. RESULT is
13785 the variant to fill in. OBSTACK is where any needed allocations
13786 will be done. OFFSET_MAP holds the mapping from section offsets to
13787 fields for the type. FI describes the fields of the type we're
13788 processing. FIELD is the variant field we're converting. */
13789
13790 static void
13791 create_one_variant (variant &result, struct obstack *obstack,
13792 const offset_map_type &offset_map,
13793 struct field_info *fi, const variant_field &field)
13794 {
13795 result.discriminants = convert_variant_range (obstack, field, false);
13796 result.first_field = field.first_field + fi->baseclasses.size ();
13797 result.last_field = field.last_field + fi->baseclasses.size ();
13798 result.parts = create_variant_parts (obstack, offset_map, fi,
13799 field.variant_parts);
13800 }
13801
13802 /* Fill in a "struct variant_part" for a given variant part. RESULT
13803 is the variant part to fill in. OBSTACK is where any needed
13804 allocations will be done. OFFSET_MAP holds the mapping from
13805 section offsets to fields for the type. FI describes the fields of
13806 the type we're processing. BUILDER is the variant part to be
13807 converted. */
13808
13809 static void
13810 create_one_variant_part (variant_part &result,
13811 struct obstack *obstack,
13812 const offset_map_type &offset_map,
13813 struct field_info *fi,
13814 const variant_part_builder &builder)
13815 {
13816 auto iter = offset_map.find (builder.discriminant_offset);
13817 if (iter == offset_map.end ())
13818 {
13819 result.discriminant_index = -1;
13820 /* Doesn't matter. */
13821 result.is_unsigned = false;
13822 }
13823 else
13824 {
13825 result.discriminant_index = iter->second;
13826 result.is_unsigned
13827 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
13828 }
13829
13830 size_t n = builder.variants.size ();
13831 variant *output = new (obstack) variant[n];
13832 for (size_t i = 0; i < n; ++i)
13833 create_one_variant (output[i], obstack, offset_map, fi,
13834 builder.variants[i]);
13835
13836 result.variants = gdb::array_view<variant> (output, n);
13837 }
13838
13839 /* Create a vector of variant parts that can be attached to a type.
13840 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13841 holds the mapping from section offsets to fields for the type. FI
13842 describes the fields of the type we're processing. VARIANT_PARTS
13843 is the vector to convert. */
13844
13845 static const gdb::array_view<variant_part>
13846 create_variant_parts (struct obstack *obstack,
13847 const offset_map_type &offset_map,
13848 struct field_info *fi,
13849 const std::vector<variant_part_builder> &variant_parts)
13850 {
13851 if (variant_parts.empty ())
13852 return {};
13853
13854 size_t n = variant_parts.size ();
13855 variant_part *result = new (obstack) variant_part[n];
13856 for (size_t i = 0; i < n; ++i)
13857 create_one_variant_part (result[i], obstack, offset_map, fi,
13858 variant_parts[i]);
13859
13860 return gdb::array_view<variant_part> (result, n);
13861 }
13862
13863 /* Compute the variant part vector for FIP, attaching it to TYPE when
13864 done. */
13865
13866 static void
13867 add_variant_property (struct field_info *fip, struct type *type,
13868 struct dwarf2_cu *cu)
13869 {
13870 /* Map section offsets of fields to their field index. Note the
13871 field index here does not take the number of baseclasses into
13872 account. */
13873 offset_map_type offset_map;
13874 for (int i = 0; i < fip->fields.size (); ++i)
13875 offset_map[fip->fields[i].offset] = i;
13876
13877 struct objfile *objfile = cu->per_objfile->objfile;
13878 gdb::array_view<const variant_part> parts
13879 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
13880 fip->variant_parts);
13881
13882 struct dynamic_prop prop;
13883 prop.set_variant_parts ((gdb::array_view<variant_part> *)
13884 obstack_copy (&objfile->objfile_obstack, &parts,
13885 sizeof (parts)));
13886
13887 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
13888 }
13889
13890 /* Create the vector of fields, and attach it to the type. */
13891
13892 static void
13893 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13894 struct dwarf2_cu *cu)
13895 {
13896 int nfields = fip->nfields ();
13897
13898 /* Record the field count, allocate space for the array of fields,
13899 and create blank accessibility bitfields if necessary. */
13900 type->set_num_fields (nfields);
13901 type->set_fields
13902 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
13903
13904 if (fip->non_public_fields && cu->lang () != language_ada)
13905 {
13906 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13907
13908 TYPE_FIELD_PRIVATE_BITS (type) =
13909 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13910 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13911
13912 TYPE_FIELD_PROTECTED_BITS (type) =
13913 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13914 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13915
13916 TYPE_FIELD_IGNORE_BITS (type) =
13917 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13918 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13919 }
13920
13921 /* If the type has baseclasses, allocate and clear a bit vector for
13922 TYPE_FIELD_VIRTUAL_BITS. */
13923 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
13924 {
13925 int num_bytes = B_BYTES (fip->baseclasses.size ());
13926 unsigned char *pointer;
13927
13928 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13929 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13930 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13931 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
13932 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
13933 }
13934
13935 if (!fip->variant_parts.empty ())
13936 add_variant_property (fip, type, cu);
13937
13938 /* Copy the saved-up fields into the field vector. */
13939 for (int i = 0; i < nfields; ++i)
13940 {
13941 struct nextfield &field
13942 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
13943 : fip->fields[i - fip->baseclasses.size ()]);
13944
13945 type->field (i) = field.field;
13946 switch (field.accessibility)
13947 {
13948 case DW_ACCESS_private:
13949 if (cu->lang () != language_ada)
13950 SET_TYPE_FIELD_PRIVATE (type, i);
13951 break;
13952
13953 case DW_ACCESS_protected:
13954 if (cu->lang () != language_ada)
13955 SET_TYPE_FIELD_PROTECTED (type, i);
13956 break;
13957
13958 case DW_ACCESS_public:
13959 break;
13960
13961 default:
13962 /* Unknown accessibility. Complain and treat it as public. */
13963 {
13964 complaint (_("unsupported accessibility %d"),
13965 field.accessibility);
13966 }
13967 break;
13968 }
13969 if (i < fip->baseclasses.size ())
13970 {
13971 switch (field.virtuality)
13972 {
13973 case DW_VIRTUALITY_virtual:
13974 case DW_VIRTUALITY_pure_virtual:
13975 if (cu->lang () == language_ada)
13976 error (_("unexpected virtuality in component of Ada type"));
13977 SET_TYPE_FIELD_VIRTUAL (type, i);
13978 break;
13979 }
13980 }
13981 }
13982 }
13983
13984 /* Return true if this member function is a constructor, false
13985 otherwise. */
13986
13987 static int
13988 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13989 {
13990 const char *fieldname;
13991 const char *type_name;
13992 int len;
13993
13994 if (die->parent == NULL)
13995 return 0;
13996
13997 if (die->parent->tag != DW_TAG_structure_type
13998 && die->parent->tag != DW_TAG_union_type
13999 && die->parent->tag != DW_TAG_class_type)
14000 return 0;
14001
14002 fieldname = dwarf2_name (die, cu);
14003 type_name = dwarf2_name (die->parent, cu);
14004 if (fieldname == NULL || type_name == NULL)
14005 return 0;
14006
14007 len = strlen (fieldname);
14008 return (strncmp (fieldname, type_name, len) == 0
14009 && (type_name[len] == '\0' || type_name[len] == '<'));
14010 }
14011
14012 /* Add a member function to the proper fieldlist. */
14013
14014 static void
14015 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14016 struct type *type, struct dwarf2_cu *cu)
14017 {
14018 struct objfile *objfile = cu->per_objfile->objfile;
14019 struct attribute *attr;
14020 int i;
14021 struct fnfieldlist *flp = nullptr;
14022 struct fn_field *fnp;
14023 const char *fieldname;
14024 struct type *this_type;
14025
14026 if (cu->lang () == language_ada)
14027 error (_("unexpected member function in Ada type"));
14028
14029 /* Get name of member function. */
14030 fieldname = dwarf2_name (die, cu);
14031 if (fieldname == NULL)
14032 return;
14033
14034 /* Look up member function name in fieldlist. */
14035 for (i = 0; i < fip->fnfieldlists.size (); i++)
14036 {
14037 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14038 {
14039 flp = &fip->fnfieldlists[i];
14040 break;
14041 }
14042 }
14043
14044 /* Create a new fnfieldlist if necessary. */
14045 if (flp == nullptr)
14046 {
14047 fip->fnfieldlists.emplace_back ();
14048 flp = &fip->fnfieldlists.back ();
14049 flp->name = fieldname;
14050 i = fip->fnfieldlists.size () - 1;
14051 }
14052
14053 /* Create a new member function field and add it to the vector of
14054 fnfieldlists. */
14055 flp->fnfields.emplace_back ();
14056 fnp = &flp->fnfields.back ();
14057
14058 /* Delay processing of the physname until later. */
14059 if (cu->lang () == language_cplus)
14060 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14061 die, cu);
14062 else
14063 {
14064 const char *physname = dwarf2_physname (fieldname, die, cu);
14065 fnp->physname = physname ? physname : "";
14066 }
14067
14068 fnp->type = alloc_type (objfile);
14069 this_type = read_type_die (die, cu);
14070 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14071 {
14072 int nparams = this_type->num_fields ();
14073
14074 /* TYPE is the domain of this method, and THIS_TYPE is the type
14075 of the method itself (TYPE_CODE_METHOD). */
14076 smash_to_method_type (fnp->type, type,
14077 TYPE_TARGET_TYPE (this_type),
14078 this_type->fields (),
14079 this_type->num_fields (),
14080 this_type->has_varargs ());
14081
14082 /* Handle static member functions.
14083 Dwarf2 has no clean way to discern C++ static and non-static
14084 member functions. G++ helps GDB by marking the first
14085 parameter for non-static member functions (which is the this
14086 pointer) as artificial. We obtain this information from
14087 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14088 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14089 fnp->voffset = VOFFSET_STATIC;
14090 }
14091 else
14092 complaint (_("member function type missing for '%s'"),
14093 dwarf2_full_name (fieldname, die, cu));
14094
14095 /* Get fcontext from DW_AT_containing_type if present. */
14096 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14097 fnp->fcontext = die_containing_type (die, cu);
14098
14099 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14100 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14101
14102 /* Get accessibility. */
14103 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14104 switch (accessibility)
14105 {
14106 case DW_ACCESS_private:
14107 fnp->is_private = 1;
14108 break;
14109 case DW_ACCESS_protected:
14110 fnp->is_protected = 1;
14111 break;
14112 }
14113
14114 /* Check for artificial methods. */
14115 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14116 if (attr && attr->as_boolean ())
14117 fnp->is_artificial = 1;
14118
14119 /* Check for defaulted methods. */
14120 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14121 if (attr != nullptr)
14122 fnp->defaulted = attr->defaulted ();
14123
14124 /* Check for deleted methods. */
14125 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14126 if (attr != nullptr && attr->as_boolean ())
14127 fnp->is_deleted = 1;
14128
14129 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14130
14131 /* Get index in virtual function table if it is a virtual member
14132 function. For older versions of GCC, this is an offset in the
14133 appropriate virtual table, as specified by DW_AT_containing_type.
14134 For everyone else, it is an expression to be evaluated relative
14135 to the object address. */
14136
14137 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14138 if (attr != nullptr)
14139 {
14140 if (attr->form_is_block () && attr->as_block ()->size > 0)
14141 {
14142 struct dwarf_block *block = attr->as_block ();
14143
14144 if (block->data[0] == DW_OP_constu)
14145 {
14146 /* Old-style GCC. */
14147 fnp->voffset = decode_locdesc (block, cu) + 2;
14148 }
14149 else if (block->data[0] == DW_OP_deref
14150 || (block->size > 1
14151 && block->data[0] == DW_OP_deref_size
14152 && block->data[1] == cu->header.addr_size))
14153 {
14154 fnp->voffset = decode_locdesc (block, cu);
14155 if ((fnp->voffset % cu->header.addr_size) != 0)
14156 dwarf2_complex_location_expr_complaint ();
14157 else
14158 fnp->voffset /= cu->header.addr_size;
14159 fnp->voffset += 2;
14160 }
14161 else
14162 dwarf2_complex_location_expr_complaint ();
14163
14164 if (!fnp->fcontext)
14165 {
14166 /* If there is no `this' field and no DW_AT_containing_type,
14167 we cannot actually find a base class context for the
14168 vtable! */
14169 if (this_type->num_fields () == 0
14170 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14171 {
14172 complaint (_("cannot determine context for virtual member "
14173 "function \"%s\" (offset %s)"),
14174 fieldname, sect_offset_str (die->sect_off));
14175 }
14176 else
14177 {
14178 fnp->fcontext
14179 = TYPE_TARGET_TYPE (this_type->field (0).type ());
14180 }
14181 }
14182 }
14183 else if (attr->form_is_section_offset ())
14184 {
14185 dwarf2_complex_location_expr_complaint ();
14186 }
14187 else
14188 {
14189 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14190 fieldname);
14191 }
14192 }
14193 else
14194 {
14195 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14196 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
14197 {
14198 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14199 complaint (_("Member function \"%s\" (offset %s) is virtual "
14200 "but the vtable offset is not specified"),
14201 fieldname, sect_offset_str (die->sect_off));
14202 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14203 TYPE_CPLUS_DYNAMIC (type) = 1;
14204 }
14205 }
14206 }
14207
14208 /* Create the vector of member function fields, and attach it to the type. */
14209
14210 static void
14211 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14212 struct dwarf2_cu *cu)
14213 {
14214 if (cu->lang () == language_ada)
14215 error (_("unexpected member functions in Ada type"));
14216
14217 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14218 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14219 TYPE_ALLOC (type,
14220 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14221
14222 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14223 {
14224 struct fnfieldlist &nf = fip->fnfieldlists[i];
14225 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14226
14227 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14228 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14229 fn_flp->fn_fields = (struct fn_field *)
14230 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14231
14232 for (int k = 0; k < nf.fnfields.size (); ++k)
14233 fn_flp->fn_fields[k] = nf.fnfields[k];
14234 }
14235
14236 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14237 }
14238
14239 /* Returns non-zero if NAME is the name of a vtable member in CU's
14240 language, zero otherwise. */
14241 static int
14242 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14243 {
14244 static const char vptr[] = "_vptr";
14245
14246 /* Look for the C++ form of the vtable. */
14247 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14248 return 1;
14249
14250 return 0;
14251 }
14252
14253 /* GCC outputs unnamed structures that are really pointers to member
14254 functions, with the ABI-specified layout. If TYPE describes
14255 such a structure, smash it into a member function type.
14256
14257 GCC shouldn't do this; it should just output pointer to member DIEs.
14258 This is GCC PR debug/28767. */
14259
14260 static void
14261 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14262 {
14263 struct type *pfn_type, *self_type, *new_type;
14264
14265 /* Check for a structure with no name and two children. */
14266 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14267 return;
14268
14269 /* Check for __pfn and __delta members. */
14270 if (type->field (0).name () == NULL
14271 || strcmp (type->field (0).name (), "__pfn") != 0
14272 || type->field (1).name () == NULL
14273 || strcmp (type->field (1).name (), "__delta") != 0)
14274 return;
14275
14276 /* Find the type of the method. */
14277 pfn_type = type->field (0).type ();
14278 if (pfn_type == NULL
14279 || pfn_type->code () != TYPE_CODE_PTR
14280 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
14281 return;
14282
14283 /* Look for the "this" argument. */
14284 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14285 if (pfn_type->num_fields () == 0
14286 /* || pfn_type->field (0).type () == NULL */
14287 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
14288 return;
14289
14290 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
14291 new_type = alloc_type (objfile);
14292 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14293 pfn_type->fields (), pfn_type->num_fields (),
14294 pfn_type->has_varargs ());
14295 smash_to_methodptr_type (type, new_type);
14296 }
14297
14298 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14299 requires rewriting, then copy it and return the updated copy.
14300 Otherwise return nullptr. */
14301
14302 static struct type *
14303 rewrite_array_type (struct type *type)
14304 {
14305 if (type->code () != TYPE_CODE_ARRAY)
14306 return nullptr;
14307
14308 struct type *index_type = type->index_type ();
14309 range_bounds *current_bounds = index_type->bounds ();
14310
14311 /* Handle multi-dimensional arrays. */
14312 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
14313 if (new_target == nullptr)
14314 {
14315 /* Maybe we don't need to rewrite this array. */
14316 if (current_bounds->low.kind () == PROP_CONST
14317 && current_bounds->high.kind () == PROP_CONST)
14318 return nullptr;
14319 }
14320
14321 /* Either the target type was rewritten, or the bounds have to be
14322 updated. Either way we want to copy the type and update
14323 everything. */
14324 struct type *copy = copy_type (type);
14325 int nfields = copy->num_fields ();
14326 field *new_fields
14327 = ((struct field *) TYPE_ZALLOC (copy,
14328 nfields * sizeof (struct field)));
14329 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
14330 copy->set_fields (new_fields);
14331 if (new_target != nullptr)
14332 TYPE_TARGET_TYPE (copy) = new_target;
14333
14334 struct type *index_copy = copy_type (index_type);
14335 range_bounds *bounds
14336 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
14337 sizeof (range_bounds));
14338 *bounds = *current_bounds;
14339 bounds->low.set_const_val (1);
14340 bounds->high.set_const_val (0);
14341 index_copy->set_bounds (bounds);
14342 copy->set_index_type (index_copy);
14343
14344 return copy;
14345 }
14346
14347 /* While some versions of GCC will generate complicated DWARF for an
14348 array (see quirk_ada_thick_pointer), more recent versions were
14349 modified to emit an explicit thick pointer structure. However, in
14350 this case, the array still has DWARF expressions for its ranges,
14351 and these must be ignored. */
14352
14353 static void
14354 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
14355 struct type *type)
14356 {
14357 gdb_assert (cu->lang () == language_ada);
14358
14359 /* Check for a structure with two children. */
14360 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14361 return;
14362
14363 /* Check for P_ARRAY and P_BOUNDS members. */
14364 if (type->field (0).name () == NULL
14365 || strcmp (type->field (0).name (), "P_ARRAY") != 0
14366 || type->field (1).name () == NULL
14367 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
14368 return;
14369
14370 /* Make sure we're looking at a pointer to an array. */
14371 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
14372 return;
14373
14374 /* The Ada code already knows how to handle these types, so all that
14375 we need to do is turn the bounds into static bounds. However, we
14376 don't want to rewrite existing array or index types in-place,
14377 because those may be referenced in other contexts where this
14378 rewriting is undesirable. */
14379 struct type *new_ary_type
14380 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
14381 if (new_ary_type != nullptr)
14382 type->field (0).set_type (lookup_pointer_type (new_ary_type));
14383 }
14384
14385 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14386 appropriate error checking and issuing complaints if there is a
14387 problem. */
14388
14389 static ULONGEST
14390 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14391 {
14392 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14393
14394 if (attr == nullptr)
14395 return 0;
14396
14397 if (!attr->form_is_constant ())
14398 {
14399 complaint (_("DW_AT_alignment must have constant form"
14400 " - DIE at %s [in module %s]"),
14401 sect_offset_str (die->sect_off),
14402 objfile_name (cu->per_objfile->objfile));
14403 return 0;
14404 }
14405
14406 LONGEST val = attr->constant_value (0);
14407 if (val < 0)
14408 {
14409 complaint (_("DW_AT_alignment value must not be negative"
14410 " - DIE at %s [in module %s]"),
14411 sect_offset_str (die->sect_off),
14412 objfile_name (cu->per_objfile->objfile));
14413 return 0;
14414 }
14415 ULONGEST align = val;
14416
14417 if (align == 0)
14418 {
14419 complaint (_("DW_AT_alignment value must not be zero"
14420 " - DIE at %s [in module %s]"),
14421 sect_offset_str (die->sect_off),
14422 objfile_name (cu->per_objfile->objfile));
14423 return 0;
14424 }
14425 if ((align & (align - 1)) != 0)
14426 {
14427 complaint (_("DW_AT_alignment value must be a power of 2"
14428 " - DIE at %s [in module %s]"),
14429 sect_offset_str (die->sect_off),
14430 objfile_name (cu->per_objfile->objfile));
14431 return 0;
14432 }
14433
14434 return align;
14435 }
14436
14437 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14438 the alignment for TYPE. */
14439
14440 static void
14441 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14442 struct type *type)
14443 {
14444 if (!set_type_align (type, get_alignment (cu, die)))
14445 complaint (_("DW_AT_alignment value too large"
14446 " - DIE at %s [in module %s]"),
14447 sect_offset_str (die->sect_off),
14448 objfile_name (cu->per_objfile->objfile));
14449 }
14450
14451 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14452 constant for a type, according to DWARF5 spec, Table 5.5. */
14453
14454 static bool
14455 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14456 {
14457 switch (value)
14458 {
14459 case DW_CC_normal:
14460 case DW_CC_pass_by_reference:
14461 case DW_CC_pass_by_value:
14462 return true;
14463
14464 default:
14465 complaint (_("unrecognized DW_AT_calling_convention value "
14466 "(%s) for a type"), pulongest (value));
14467 return false;
14468 }
14469 }
14470
14471 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14472 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14473 also according to GNU-specific values (see include/dwarf2.h). */
14474
14475 static bool
14476 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14477 {
14478 switch (value)
14479 {
14480 case DW_CC_normal:
14481 case DW_CC_program:
14482 case DW_CC_nocall:
14483 return true;
14484
14485 case DW_CC_GNU_renesas_sh:
14486 case DW_CC_GNU_borland_fastcall_i386:
14487 case DW_CC_GDB_IBM_OpenCL:
14488 return true;
14489
14490 default:
14491 complaint (_("unrecognized DW_AT_calling_convention value "
14492 "(%s) for a subroutine"), pulongest (value));
14493 return false;
14494 }
14495 }
14496
14497 /* Called when we find the DIE that starts a structure or union scope
14498 (definition) to create a type for the structure or union. Fill in
14499 the type's name and general properties; the members will not be
14500 processed until process_structure_scope. A symbol table entry for
14501 the type will also not be done until process_structure_scope (assuming
14502 the type has a name).
14503
14504 NOTE: we need to call these functions regardless of whether or not the
14505 DIE has a DW_AT_name attribute, since it might be an anonymous
14506 structure or union. This gets the type entered into our set of
14507 user defined types. */
14508
14509 static struct type *
14510 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14511 {
14512 struct objfile *objfile = cu->per_objfile->objfile;
14513 struct type *type;
14514 struct attribute *attr;
14515 const char *name;
14516
14517 /* If the definition of this type lives in .debug_types, read that type.
14518 Don't follow DW_AT_specification though, that will take us back up
14519 the chain and we want to go down. */
14520 attr = die->attr (DW_AT_signature);
14521 if (attr != nullptr)
14522 {
14523 type = get_DW_AT_signature_type (die, attr, cu);
14524
14525 /* The type's CU may not be the same as CU.
14526 Ensure TYPE is recorded with CU in die_type_hash. */
14527 return set_die_type (die, type, cu);
14528 }
14529
14530 type = alloc_type (objfile);
14531 INIT_CPLUS_SPECIFIC (type);
14532
14533 name = dwarf2_name (die, cu);
14534 if (name != NULL)
14535 {
14536 if (cu->lang () == language_cplus
14537 || cu->lang () == language_d
14538 || cu->lang () == language_rust)
14539 {
14540 const char *full_name = dwarf2_full_name (name, die, cu);
14541
14542 /* dwarf2_full_name might have already finished building the DIE's
14543 type. If so, there is no need to continue. */
14544 if (get_die_type (die, cu) != NULL)
14545 return get_die_type (die, cu);
14546
14547 type->set_name (full_name);
14548 }
14549 else
14550 {
14551 /* The name is already allocated along with this objfile, so
14552 we don't need to duplicate it for the type. */
14553 type->set_name (name);
14554 }
14555 }
14556
14557 if (die->tag == DW_TAG_structure_type)
14558 {
14559 type->set_code (TYPE_CODE_STRUCT);
14560 }
14561 else if (die->tag == DW_TAG_union_type)
14562 {
14563 type->set_code (TYPE_CODE_UNION);
14564 }
14565 else if (die->tag == DW_TAG_namelist)
14566 {
14567 type->set_code (TYPE_CODE_NAMELIST);
14568 }
14569 else
14570 {
14571 type->set_code (TYPE_CODE_STRUCT);
14572 }
14573
14574 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
14575 type->set_is_declared_class (true);
14576
14577 /* Store the calling convention in the type if it's available in
14578 the die. Otherwise the calling convention remains set to
14579 the default value DW_CC_normal. */
14580 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14581 if (attr != nullptr
14582 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
14583 {
14584 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14585 TYPE_CPLUS_CALLING_CONVENTION (type)
14586 = (enum dwarf_calling_convention) (attr->constant_value (0));
14587 }
14588
14589 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14590 if (attr != nullptr)
14591 {
14592 if (attr->form_is_constant ())
14593 TYPE_LENGTH (type) = attr->constant_value (0);
14594 else
14595 {
14596 struct dynamic_prop prop;
14597 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
14598 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
14599 TYPE_LENGTH (type) = 0;
14600 }
14601 }
14602 else
14603 {
14604 TYPE_LENGTH (type) = 0;
14605 }
14606
14607 maybe_set_alignment (cu, die, type);
14608
14609 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
14610 {
14611 /* ICC<14 does not output the required DW_AT_declaration on
14612 incomplete types, but gives them a size of zero. */
14613 type->set_is_stub (true);
14614 }
14615 else
14616 type->set_stub_is_supported (true);
14617
14618 if (die_is_declaration (die, cu))
14619 type->set_is_stub (true);
14620 else if (attr == NULL && die->child == NULL
14621 && producer_is_realview (cu->producer))
14622 /* RealView does not output the required DW_AT_declaration
14623 on incomplete types. */
14624 type->set_is_stub (true);
14625
14626 /* We need to add the type field to the die immediately so we don't
14627 infinitely recurse when dealing with pointers to the structure
14628 type within the structure itself. */
14629 set_die_type (die, type, cu);
14630
14631 /* set_die_type should be already done. */
14632 set_descriptive_type (type, die, cu);
14633
14634 return type;
14635 }
14636
14637 static void handle_struct_member_die
14638 (struct die_info *child_die,
14639 struct type *type,
14640 struct field_info *fi,
14641 std::vector<struct symbol *> *template_args,
14642 struct dwarf2_cu *cu);
14643
14644 /* A helper for handle_struct_member_die that handles
14645 DW_TAG_variant_part. */
14646
14647 static void
14648 handle_variant_part (struct die_info *die, struct type *type,
14649 struct field_info *fi,
14650 std::vector<struct symbol *> *template_args,
14651 struct dwarf2_cu *cu)
14652 {
14653 variant_part_builder *new_part;
14654 if (fi->current_variant_part == nullptr)
14655 {
14656 fi->variant_parts.emplace_back ();
14657 new_part = &fi->variant_parts.back ();
14658 }
14659 else if (!fi->current_variant_part->processing_variant)
14660 {
14661 complaint (_("nested DW_TAG_variant_part seen "
14662 "- DIE at %s [in module %s]"),
14663 sect_offset_str (die->sect_off),
14664 objfile_name (cu->per_objfile->objfile));
14665 return;
14666 }
14667 else
14668 {
14669 variant_field &current = fi->current_variant_part->variants.back ();
14670 current.variant_parts.emplace_back ();
14671 new_part = &current.variant_parts.back ();
14672 }
14673
14674 /* When we recurse, we want callees to add to this new variant
14675 part. */
14676 scoped_restore save_current_variant_part
14677 = make_scoped_restore (&fi->current_variant_part, new_part);
14678
14679 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
14680 if (discr == NULL)
14681 {
14682 /* It's a univariant form, an extension we support. */
14683 }
14684 else if (discr->form_is_ref ())
14685 {
14686 struct dwarf2_cu *target_cu = cu;
14687 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
14688
14689 new_part->discriminant_offset = target_die->sect_off;
14690 }
14691 else
14692 {
14693 complaint (_("DW_AT_discr does not have DIE reference form"
14694 " - DIE at %s [in module %s]"),
14695 sect_offset_str (die->sect_off),
14696 objfile_name (cu->per_objfile->objfile));
14697 }
14698
14699 for (die_info *child_die = die->child;
14700 child_die != NULL;
14701 child_die = child_die->sibling)
14702 handle_struct_member_die (child_die, type, fi, template_args, cu);
14703 }
14704
14705 /* A helper for handle_struct_member_die that handles
14706 DW_TAG_variant. */
14707
14708 static void
14709 handle_variant (struct die_info *die, struct type *type,
14710 struct field_info *fi,
14711 std::vector<struct symbol *> *template_args,
14712 struct dwarf2_cu *cu)
14713 {
14714 if (fi->current_variant_part == nullptr)
14715 {
14716 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14717 "- DIE at %s [in module %s]"),
14718 sect_offset_str (die->sect_off),
14719 objfile_name (cu->per_objfile->objfile));
14720 return;
14721 }
14722 if (fi->current_variant_part->processing_variant)
14723 {
14724 complaint (_("nested DW_TAG_variant seen "
14725 "- DIE at %s [in module %s]"),
14726 sect_offset_str (die->sect_off),
14727 objfile_name (cu->per_objfile->objfile));
14728 return;
14729 }
14730
14731 scoped_restore save_processing_variant
14732 = make_scoped_restore (&fi->current_variant_part->processing_variant,
14733 true);
14734
14735 fi->current_variant_part->variants.emplace_back ();
14736 variant_field &variant = fi->current_variant_part->variants.back ();
14737 variant.first_field = fi->fields.size ();
14738
14739 /* In a variant we want to get the discriminant and also add a
14740 field for our sole member child. */
14741 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
14742 if (discr == nullptr || !discr->form_is_constant ())
14743 {
14744 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
14745 if (discr == nullptr || discr->as_block ()->size == 0)
14746 variant.default_branch = true;
14747 else
14748 variant.discr_list_data = discr->as_block ();
14749 }
14750 else
14751 variant.discriminant_value = discr->constant_value (0);
14752
14753 for (die_info *variant_child = die->child;
14754 variant_child != NULL;
14755 variant_child = variant_child->sibling)
14756 handle_struct_member_die (variant_child, type, fi, template_args, cu);
14757
14758 variant.last_field = fi->fields.size ();
14759 }
14760
14761 /* A helper for process_structure_scope that handles a single member
14762 DIE. */
14763
14764 static void
14765 handle_struct_member_die (struct die_info *child_die, struct type *type,
14766 struct field_info *fi,
14767 std::vector<struct symbol *> *template_args,
14768 struct dwarf2_cu *cu)
14769 {
14770 if (child_die->tag == DW_TAG_member
14771 || child_die->tag == DW_TAG_variable
14772 || child_die->tag == DW_TAG_namelist_item)
14773 {
14774 /* NOTE: carlton/2002-11-05: A C++ static data member
14775 should be a DW_TAG_member that is a declaration, but
14776 all versions of G++ as of this writing (so through at
14777 least 3.2.1) incorrectly generate DW_TAG_variable
14778 tags for them instead. */
14779 dwarf2_add_field (fi, child_die, cu);
14780 }
14781 else if (child_die->tag == DW_TAG_subprogram)
14782 {
14783 /* Rust doesn't have member functions in the C++ sense.
14784 However, it does emit ordinary functions as children
14785 of a struct DIE. */
14786 if (cu->lang () == language_rust)
14787 read_func_scope (child_die, cu);
14788 else
14789 {
14790 /* C++ member function. */
14791 dwarf2_add_member_fn (fi, child_die, type, cu);
14792 }
14793 }
14794 else if (child_die->tag == DW_TAG_inheritance)
14795 {
14796 /* C++ base class field. */
14797 dwarf2_add_field (fi, child_die, cu);
14798 }
14799 else if (type_can_define_types (child_die))
14800 dwarf2_add_type_defn (fi, child_die, cu);
14801 else if (child_die->tag == DW_TAG_template_type_param
14802 || child_die->tag == DW_TAG_template_value_param)
14803 {
14804 struct symbol *arg = new_symbol (child_die, NULL, cu);
14805
14806 if (arg != NULL)
14807 template_args->push_back (arg);
14808 }
14809 else if (child_die->tag == DW_TAG_variant_part)
14810 handle_variant_part (child_die, type, fi, template_args, cu);
14811 else if (child_die->tag == DW_TAG_variant)
14812 handle_variant (child_die, type, fi, template_args, cu);
14813 }
14814
14815 /* Finish creating a structure or union type, including filling in its
14816 members and creating a symbol for it. This function also handles Fortran
14817 namelist variables, their items or members and creating a symbol for
14818 them. */
14819
14820 static void
14821 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14822 {
14823 struct objfile *objfile = cu->per_objfile->objfile;
14824 struct die_info *child_die;
14825 struct type *type;
14826
14827 type = get_die_type (die, cu);
14828 if (type == NULL)
14829 type = read_structure_type (die, cu);
14830
14831 bool has_template_parameters = false;
14832 if (die->child != NULL && ! die_is_declaration (die, cu))
14833 {
14834 struct field_info fi;
14835 std::vector<struct symbol *> template_args;
14836
14837 child_die = die->child;
14838
14839 while (child_die && child_die->tag)
14840 {
14841 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
14842 child_die = child_die->sibling;
14843 }
14844
14845 /* Attach template arguments to type. */
14846 if (!template_args.empty ())
14847 {
14848 has_template_parameters = true;
14849 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14850 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14851 TYPE_TEMPLATE_ARGUMENTS (type)
14852 = XOBNEWVEC (&objfile->objfile_obstack,
14853 struct symbol *,
14854 TYPE_N_TEMPLATE_ARGUMENTS (type));
14855 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14856 template_args.data (),
14857 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14858 * sizeof (struct symbol *)));
14859 }
14860
14861 /* Attach fields and member functions to the type. */
14862 if (fi.nfields () > 0)
14863 dwarf2_attach_fields_to_type (&fi, type, cu);
14864 if (!fi.fnfieldlists.empty ())
14865 {
14866 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14867
14868 /* Get the type which refers to the base class (possibly this
14869 class itself) which contains the vtable pointer for the current
14870 class from the DW_AT_containing_type attribute. This use of
14871 DW_AT_containing_type is a GNU extension. */
14872
14873 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14874 {
14875 struct type *t = die_containing_type (die, cu);
14876
14877 set_type_vptr_basetype (type, t);
14878 if (type == t)
14879 {
14880 int i;
14881
14882 /* Our own class provides vtbl ptr. */
14883 for (i = t->num_fields () - 1;
14884 i >= TYPE_N_BASECLASSES (t);
14885 --i)
14886 {
14887 const char *fieldname = t->field (i).name ();
14888
14889 if (is_vtable_name (fieldname, cu))
14890 {
14891 set_type_vptr_fieldno (type, i);
14892 break;
14893 }
14894 }
14895
14896 /* Complain if virtual function table field not found. */
14897 if (i < TYPE_N_BASECLASSES (t))
14898 complaint (_("virtual function table pointer "
14899 "not found when defining class '%s'"),
14900 type->name () ? type->name () : "");
14901 }
14902 else
14903 {
14904 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14905 }
14906 }
14907 else if (cu->producer
14908 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14909 {
14910 /* The IBM XLC compiler does not provide direct indication
14911 of the containing type, but the vtable pointer is
14912 always named __vfp. */
14913
14914 int i;
14915
14916 for (i = type->num_fields () - 1;
14917 i >= TYPE_N_BASECLASSES (type);
14918 --i)
14919 {
14920 if (strcmp (type->field (i).name (), "__vfp") == 0)
14921 {
14922 set_type_vptr_fieldno (type, i);
14923 set_type_vptr_basetype (type, type);
14924 break;
14925 }
14926 }
14927 }
14928 }
14929
14930 /* Copy fi.typedef_field_list linked list elements content into the
14931 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14932 if (!fi.typedef_field_list.empty ())
14933 {
14934 int count = fi.typedef_field_list.size ();
14935
14936 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14937 TYPE_TYPEDEF_FIELD_ARRAY (type)
14938 = ((struct decl_field *)
14939 TYPE_ALLOC (type,
14940 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
14941 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
14942
14943 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
14944 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
14945 }
14946
14947 /* Copy fi.nested_types_list linked list elements content into the
14948 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
14949 if (!fi.nested_types_list.empty ()
14950 && cu->lang () != language_ada)
14951 {
14952 int count = fi.nested_types_list.size ();
14953
14954 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14955 TYPE_NESTED_TYPES_ARRAY (type)
14956 = ((struct decl_field *)
14957 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
14958 TYPE_NESTED_TYPES_COUNT (type) = count;
14959
14960 for (int i = 0; i < fi.nested_types_list.size (); ++i)
14961 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
14962 }
14963 }
14964
14965 quirk_gcc_member_function_pointer (type, objfile);
14966 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
14967 cu->rust_unions.push_back (type);
14968 else if (cu->lang () == language_ada)
14969 quirk_ada_thick_pointer_struct (die, cu, type);
14970
14971 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14972 snapshots) has been known to create a die giving a declaration
14973 for a class that has, as a child, a die giving a definition for a
14974 nested class. So we have to process our children even if the
14975 current die is a declaration. Normally, of course, a declaration
14976 won't have any children at all. */
14977
14978 child_die = die->child;
14979
14980 while (child_die != NULL && child_die->tag)
14981 {
14982 if (child_die->tag == DW_TAG_member
14983 || child_die->tag == DW_TAG_variable
14984 || child_die->tag == DW_TAG_inheritance
14985 || child_die->tag == DW_TAG_template_value_param
14986 || child_die->tag == DW_TAG_template_type_param)
14987 {
14988 /* Do nothing. */
14989 }
14990 else
14991 process_die (child_die, cu);
14992
14993 child_die = child_die->sibling;
14994 }
14995
14996 /* Do not consider external references. According to the DWARF standard,
14997 these DIEs are identified by the fact that they have no byte_size
14998 attribute, and a declaration attribute. */
14999 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15000 || !die_is_declaration (die, cu)
15001 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15002 {
15003 struct symbol *sym = new_symbol (die, type, cu);
15004
15005 if (has_template_parameters)
15006 {
15007 struct symtab *symtab;
15008 if (sym != nullptr)
15009 symtab = sym->symtab ();
15010 else if (cu->line_header != nullptr)
15011 {
15012 /* Any related symtab will do. */
15013 symtab
15014 = cu->line_header->file_names ()[0].symtab;
15015 }
15016 else
15017 {
15018 symtab = nullptr;
15019 complaint (_("could not find suitable "
15020 "symtab for template parameter"
15021 " - DIE at %s [in module %s]"),
15022 sect_offset_str (die->sect_off),
15023 objfile_name (objfile));
15024 }
15025
15026 if (symtab != nullptr)
15027 {
15028 /* Make sure that the symtab is set on the new symbols.
15029 Even though they don't appear in this symtab directly,
15030 other parts of gdb assume that symbols do, and this is
15031 reasonably true. */
15032 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15033 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
15034 }
15035 }
15036 }
15037 }
15038
15039 /* Assuming DIE is an enumeration type, and TYPE is its associated
15040 type, update TYPE using some information only available in DIE's
15041 children. In particular, the fields are computed. */
15042
15043 static void
15044 update_enumeration_type_from_children (struct die_info *die,
15045 struct type *type,
15046 struct dwarf2_cu *cu)
15047 {
15048 struct die_info *child_die;
15049 int unsigned_enum = 1;
15050 int flag_enum = 1;
15051
15052 auto_obstack obstack;
15053 std::vector<struct field> fields;
15054
15055 for (child_die = die->child;
15056 child_die != NULL && child_die->tag;
15057 child_die = child_die->sibling)
15058 {
15059 struct attribute *attr;
15060 LONGEST value;
15061 const gdb_byte *bytes;
15062 struct dwarf2_locexpr_baton *baton;
15063 const char *name;
15064
15065 if (child_die->tag != DW_TAG_enumerator)
15066 continue;
15067
15068 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15069 if (attr == NULL)
15070 continue;
15071
15072 name = dwarf2_name (child_die, cu);
15073 if (name == NULL)
15074 name = "<anonymous enumerator>";
15075
15076 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15077 &value, &bytes, &baton);
15078 if (value < 0)
15079 {
15080 unsigned_enum = 0;
15081 flag_enum = 0;
15082 }
15083 else
15084 {
15085 if (count_one_bits_ll (value) >= 2)
15086 flag_enum = 0;
15087 }
15088
15089 fields.emplace_back ();
15090 struct field &field = fields.back ();
15091 field.set_name (dwarf2_physname (name, child_die, cu));
15092 field.set_loc_enumval (value);
15093 }
15094
15095 if (!fields.empty ())
15096 {
15097 type->set_num_fields (fields.size ());
15098 type->set_fields
15099 ((struct field *)
15100 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15101 memcpy (type->fields (), fields.data (),
15102 sizeof (struct field) * fields.size ());
15103 }
15104
15105 if (unsigned_enum)
15106 type->set_is_unsigned (true);
15107
15108 if (flag_enum)
15109 type->set_is_flag_enum (true);
15110 }
15111
15112 /* Given a DW_AT_enumeration_type die, set its type. We do not
15113 complete the type's fields yet, or create any symbols. */
15114
15115 static struct type *
15116 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15117 {
15118 struct objfile *objfile = cu->per_objfile->objfile;
15119 struct type *type;
15120 struct attribute *attr;
15121 const char *name;
15122
15123 /* If the definition of this type lives in .debug_types, read that type.
15124 Don't follow DW_AT_specification though, that will take us back up
15125 the chain and we want to go down. */
15126 attr = die->attr (DW_AT_signature);
15127 if (attr != nullptr)
15128 {
15129 type = get_DW_AT_signature_type (die, attr, cu);
15130
15131 /* The type's CU may not be the same as CU.
15132 Ensure TYPE is recorded with CU in die_type_hash. */
15133 return set_die_type (die, type, cu);
15134 }
15135
15136 type = alloc_type (objfile);
15137
15138 type->set_code (TYPE_CODE_ENUM);
15139 name = dwarf2_full_name (NULL, die, cu);
15140 if (name != NULL)
15141 type->set_name (name);
15142
15143 attr = dwarf2_attr (die, DW_AT_type, cu);
15144 if (attr != NULL)
15145 {
15146 struct type *underlying_type = die_type (die, cu);
15147
15148 TYPE_TARGET_TYPE (type) = underlying_type;
15149 }
15150
15151 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15152 if (attr != nullptr)
15153 {
15154 TYPE_LENGTH (type) = attr->constant_value (0);
15155 }
15156 else
15157 {
15158 TYPE_LENGTH (type) = 0;
15159 }
15160
15161 maybe_set_alignment (cu, die, type);
15162
15163 /* The enumeration DIE can be incomplete. In Ada, any type can be
15164 declared as private in the package spec, and then defined only
15165 inside the package body. Such types are known as Taft Amendment
15166 Types. When another package uses such a type, an incomplete DIE
15167 may be generated by the compiler. */
15168 if (die_is_declaration (die, cu))
15169 type->set_is_stub (true);
15170
15171 /* If this type has an underlying type that is not a stub, then we
15172 may use its attributes. We always use the "unsigned" attribute
15173 in this situation, because ordinarily we guess whether the type
15174 is unsigned -- but the guess can be wrong and the underlying type
15175 can tell us the reality. However, we defer to a local size
15176 attribute if one exists, because this lets the compiler override
15177 the underlying type if needed. */
15178 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
15179 {
15180 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15181 underlying_type = check_typedef (underlying_type);
15182
15183 type->set_is_unsigned (underlying_type->is_unsigned ());
15184
15185 if (TYPE_LENGTH (type) == 0)
15186 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15187
15188 if (TYPE_RAW_ALIGN (type) == 0
15189 && TYPE_RAW_ALIGN (underlying_type) != 0)
15190 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15191 }
15192
15193 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
15194
15195 set_die_type (die, type, cu);
15196
15197 /* Finish the creation of this type by using the enum's children.
15198 Note that, as usual, this must come after set_die_type to avoid
15199 infinite recursion when trying to compute the names of the
15200 enumerators. */
15201 update_enumeration_type_from_children (die, type, cu);
15202
15203 return type;
15204 }
15205
15206 /* Given a pointer to a die which begins an enumeration, process all
15207 the dies that define the members of the enumeration, and create the
15208 symbol for the enumeration type.
15209
15210 NOTE: We reverse the order of the element list. */
15211
15212 static void
15213 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15214 {
15215 struct type *this_type;
15216
15217 this_type = get_die_type (die, cu);
15218 if (this_type == NULL)
15219 this_type = read_enumeration_type (die, cu);
15220
15221 if (die->child != NULL)
15222 {
15223 struct die_info *child_die;
15224 const char *name;
15225
15226 child_die = die->child;
15227 while (child_die && child_die->tag)
15228 {
15229 if (child_die->tag != DW_TAG_enumerator)
15230 {
15231 process_die (child_die, cu);
15232 }
15233 else
15234 {
15235 name = dwarf2_name (child_die, cu);
15236 if (name)
15237 new_symbol (child_die, this_type, cu);
15238 }
15239
15240 child_die = child_die->sibling;
15241 }
15242 }
15243
15244 /* If we are reading an enum from a .debug_types unit, and the enum
15245 is a declaration, and the enum is not the signatured type in the
15246 unit, then we do not want to add a symbol for it. Adding a
15247 symbol would in some cases obscure the true definition of the
15248 enum, giving users an incomplete type when the definition is
15249 actually available. Note that we do not want to do this for all
15250 enums which are just declarations, because C++0x allows forward
15251 enum declarations. */
15252 if (cu->per_cu->is_debug_types
15253 && die_is_declaration (die, cu))
15254 {
15255 struct signatured_type *sig_type;
15256
15257 sig_type = (struct signatured_type *) cu->per_cu;
15258 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15259 if (sig_type->type_offset_in_section != die->sect_off)
15260 return;
15261 }
15262
15263 new_symbol (die, this_type, cu);
15264 }
15265
15266 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15267 expression for an index type and finds the corresponding field
15268 offset in the hidden "P_BOUNDS" structure. Returns true on success
15269 and updates *FIELD, false if it fails to recognize an
15270 expression. */
15271
15272 static bool
15273 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
15274 int *bounds_offset, struct field *field,
15275 struct dwarf2_cu *cu)
15276 {
15277 struct attribute *attr = dwarf2_attr (die, name, cu);
15278 if (attr == nullptr || !attr->form_is_block ())
15279 return false;
15280
15281 const struct dwarf_block *block = attr->as_block ();
15282 const gdb_byte *start = block->data;
15283 const gdb_byte *end = block->data + block->size;
15284
15285 /* The expression to recognize generally looks like:
15286
15287 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15288 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15289
15290 However, the second "plus_uconst" may be missing:
15291
15292 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15293 DW_OP_deref_size: 4)
15294
15295 This happens when the field is at the start of the structure.
15296
15297 Also, the final deref may not be sized:
15298
15299 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15300 DW_OP_deref)
15301
15302 This happens when the size of the index type happens to be the
15303 same as the architecture's word size. This can occur with or
15304 without the second plus_uconst. */
15305
15306 if (end - start < 2)
15307 return false;
15308 if (*start++ != DW_OP_push_object_address)
15309 return false;
15310 if (*start++ != DW_OP_plus_uconst)
15311 return false;
15312
15313 uint64_t this_bound_off;
15314 start = gdb_read_uleb128 (start, end, &this_bound_off);
15315 if (start == nullptr || (int) this_bound_off != this_bound_off)
15316 return false;
15317 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15318 is consistent among all bounds. */
15319 if (*bounds_offset == -1)
15320 *bounds_offset = this_bound_off;
15321 else if (*bounds_offset != this_bound_off)
15322 return false;
15323
15324 if (start == end || *start++ != DW_OP_deref)
15325 return false;
15326
15327 int offset = 0;
15328 if (start ==end)
15329 return false;
15330 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
15331 {
15332 /* This means an offset of 0. */
15333 }
15334 else if (*start++ != DW_OP_plus_uconst)
15335 return false;
15336 else
15337 {
15338 /* The size is the parameter to DW_OP_plus_uconst. */
15339 uint64_t val;
15340 start = gdb_read_uleb128 (start, end, &val);
15341 if (start == nullptr)
15342 return false;
15343 if ((int) val != val)
15344 return false;
15345 offset = val;
15346 }
15347
15348 if (start == end)
15349 return false;
15350
15351 uint64_t size;
15352 if (*start == DW_OP_deref_size)
15353 {
15354 start = gdb_read_uleb128 (start + 1, end, &size);
15355 if (start == nullptr)
15356 return false;
15357 }
15358 else if (*start == DW_OP_deref)
15359 {
15360 size = cu->header.addr_size;
15361 ++start;
15362 }
15363 else
15364 return false;
15365
15366 field->set_loc_bitpos (8 * offset);
15367 if (size != TYPE_LENGTH (field->type ()))
15368 FIELD_BITSIZE (*field) = 8 * size;
15369
15370 return true;
15371 }
15372
15373 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15374 some kinds of Ada arrays:
15375
15376 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15377 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15378 <11e0> DW_AT_data_location: 2 byte block: 97 6
15379 (DW_OP_push_object_address; DW_OP_deref)
15380 <11e3> DW_AT_type : <0x1173>
15381 <11e7> DW_AT_sibling : <0x1201>
15382 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15383 <11ec> DW_AT_type : <0x1206>
15384 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15385 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15386 DW_OP_deref_size: 4)
15387 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15388 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15389 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15390
15391 This actually represents a "thick pointer", which is a structure
15392 with two elements: one that is a pointer to the array data, and one
15393 that is a pointer to another structure; this second structure holds
15394 the array bounds.
15395
15396 This returns a new type on success, or nullptr if this didn't
15397 recognize the type. */
15398
15399 static struct type *
15400 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
15401 struct type *type)
15402 {
15403 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
15404 /* So far we've only seen this with block form. */
15405 if (attr == nullptr || !attr->form_is_block ())
15406 return nullptr;
15407
15408 /* Note that this will fail if the structure layout is changed by
15409 the compiler. However, we have no good way to recognize some
15410 other layout, because we don't know what expression the compiler
15411 might choose to emit should this happen. */
15412 struct dwarf_block *blk = attr->as_block ();
15413 if (blk->size != 2
15414 || blk->data[0] != DW_OP_push_object_address
15415 || blk->data[1] != DW_OP_deref)
15416 return nullptr;
15417
15418 int bounds_offset = -1;
15419 int max_align = -1;
15420 std::vector<struct field> range_fields;
15421 for (struct die_info *child_die = die->child;
15422 child_die;
15423 child_die = child_die->sibling)
15424 {
15425 if (child_die->tag == DW_TAG_subrange_type)
15426 {
15427 struct type *underlying = read_subrange_index_type (child_die, cu);
15428
15429 int this_align = type_align (underlying);
15430 if (this_align > max_align)
15431 max_align = this_align;
15432
15433 range_fields.emplace_back ();
15434 range_fields.emplace_back ();
15435
15436 struct field &lower = range_fields[range_fields.size () - 2];
15437 struct field &upper = range_fields[range_fields.size () - 1];
15438
15439 lower.set_type (underlying);
15440 FIELD_ARTIFICIAL (lower) = 1;
15441
15442 upper.set_type (underlying);
15443 FIELD_ARTIFICIAL (upper) = 1;
15444
15445 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
15446 &bounds_offset, &lower, cu)
15447 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
15448 &bounds_offset, &upper, cu))
15449 return nullptr;
15450 }
15451 }
15452
15453 /* This shouldn't really happen, but double-check that we found
15454 where the bounds are stored. */
15455 if (bounds_offset == -1)
15456 return nullptr;
15457
15458 struct objfile *objfile = cu->per_objfile->objfile;
15459 for (int i = 0; i < range_fields.size (); i += 2)
15460 {
15461 char name[20];
15462
15463 /* Set the name of each field in the bounds. */
15464 xsnprintf (name, sizeof (name), "LB%d", i / 2);
15465 range_fields[i].set_name (objfile->intern (name));
15466 xsnprintf (name, sizeof (name), "UB%d", i / 2);
15467 range_fields[i + 1].set_name (objfile->intern (name));
15468 }
15469
15470 struct type *bounds = alloc_type (objfile);
15471 bounds->set_code (TYPE_CODE_STRUCT);
15472
15473 bounds->set_num_fields (range_fields.size ());
15474 bounds->set_fields
15475 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
15476 * sizeof (struct field))));
15477 memcpy (bounds->fields (), range_fields.data (),
15478 bounds->num_fields () * sizeof (struct field));
15479
15480 int last_fieldno = range_fields.size () - 1;
15481 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
15482 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
15483 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
15484
15485 /* Rewrite the existing array type in place. Specifically, we
15486 remove any dynamic properties we might have read, and we replace
15487 the index types. */
15488 struct type *iter = type;
15489 for (int i = 0; i < range_fields.size (); i += 2)
15490 {
15491 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
15492 iter->main_type->dyn_prop_list = nullptr;
15493 iter->set_index_type
15494 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
15495 iter = TYPE_TARGET_TYPE (iter);
15496 }
15497
15498 struct type *result = alloc_type (objfile);
15499 result->set_code (TYPE_CODE_STRUCT);
15500
15501 result->set_num_fields (2);
15502 result->set_fields
15503 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
15504 * sizeof (struct field))));
15505
15506 /* The names are chosen to coincide with what the compiler does with
15507 -fgnat-encodings=all, which the Ada code in gdb already
15508 understands. */
15509 result->field (0).set_name ("P_ARRAY");
15510 result->field (0).set_type (lookup_pointer_type (type));
15511
15512 result->field (1).set_name ("P_BOUNDS");
15513 result->field (1).set_type (lookup_pointer_type (bounds));
15514 result->field (1).set_loc_bitpos (8 * bounds_offset);
15515
15516 result->set_name (type->name ());
15517 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
15518 + TYPE_LENGTH (result->field (1).type ()));
15519
15520 return result;
15521 }
15522
15523 /* Extract all information from a DW_TAG_array_type DIE and put it in
15524 the DIE's type field. For now, this only handles one dimensional
15525 arrays. */
15526
15527 static struct type *
15528 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15529 {
15530 struct objfile *objfile = cu->per_objfile->objfile;
15531 struct die_info *child_die;
15532 struct type *type;
15533 struct type *element_type, *range_type, *index_type;
15534 struct attribute *attr;
15535 const char *name;
15536 struct dynamic_prop *byte_stride_prop = NULL;
15537 unsigned int bit_stride = 0;
15538
15539 element_type = die_type (die, cu);
15540
15541 /* The die_type call above may have already set the type for this DIE. */
15542 type = get_die_type (die, cu);
15543 if (type)
15544 return type;
15545
15546 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15547 if (attr != NULL)
15548 {
15549 int stride_ok;
15550 struct type *prop_type = cu->addr_sized_int_type (false);
15551
15552 byte_stride_prop
15553 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15554 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15555 prop_type);
15556 if (!stride_ok)
15557 {
15558 complaint (_("unable to read array DW_AT_byte_stride "
15559 " - DIE at %s [in module %s]"),
15560 sect_offset_str (die->sect_off),
15561 objfile_name (cu->per_objfile->objfile));
15562 /* Ignore this attribute. We will likely not be able to print
15563 arrays of this type correctly, but there is little we can do
15564 to help if we cannot read the attribute's value. */
15565 byte_stride_prop = NULL;
15566 }
15567 }
15568
15569 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15570 if (attr != NULL)
15571 bit_stride = attr->constant_value (0);
15572
15573 /* Irix 6.2 native cc creates array types without children for
15574 arrays with unspecified length. */
15575 if (die->child == NULL)
15576 {
15577 index_type = objfile_type (objfile)->builtin_int;
15578 range_type = create_static_range_type (NULL, index_type, 0, -1);
15579 type = create_array_type_with_stride (NULL, element_type, range_type,
15580 byte_stride_prop, bit_stride);
15581 return set_die_type (die, type, cu);
15582 }
15583
15584 std::vector<struct type *> range_types;
15585 child_die = die->child;
15586 while (child_die && child_die->tag)
15587 {
15588 if (child_die->tag == DW_TAG_subrange_type
15589 || child_die->tag == DW_TAG_generic_subrange)
15590 {
15591 struct type *child_type = read_type_die (child_die, cu);
15592
15593 if (child_type != NULL)
15594 {
15595 /* The range type was succesfully read. Save it for the
15596 array type creation. */
15597 range_types.push_back (child_type);
15598 }
15599 }
15600 child_die = child_die->sibling;
15601 }
15602
15603 if (range_types.empty ())
15604 {
15605 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15606 sect_offset_str (die->sect_off),
15607 objfile_name (cu->per_objfile->objfile));
15608 return NULL;
15609 }
15610
15611 /* Dwarf2 dimensions are output from left to right, create the
15612 necessary array types in backwards order. */
15613
15614 type = element_type;
15615
15616 if (read_array_order (die, cu) == DW_ORD_col_major)
15617 {
15618 int i = 0;
15619
15620 while (i < range_types.size ())
15621 {
15622 type = create_array_type_with_stride (NULL, type, range_types[i++],
15623 byte_stride_prop, bit_stride);
15624 bit_stride = 0;
15625 byte_stride_prop = nullptr;
15626 }
15627 }
15628 else
15629 {
15630 size_t ndim = range_types.size ();
15631 while (ndim-- > 0)
15632 {
15633 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15634 byte_stride_prop, bit_stride);
15635 bit_stride = 0;
15636 byte_stride_prop = nullptr;
15637 }
15638 }
15639
15640 gdb_assert (type != element_type);
15641
15642 /* Understand Dwarf2 support for vector types (like they occur on
15643 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15644 array type. This is not part of the Dwarf2/3 standard yet, but a
15645 custom vendor extension. The main difference between a regular
15646 array and the vector variant is that vectors are passed by value
15647 to functions. */
15648 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15649 if (attr != nullptr)
15650 make_vector_type (type);
15651
15652 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15653 implementation may choose to implement triple vectors using this
15654 attribute. */
15655 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15656 if (attr != nullptr && attr->form_is_unsigned ())
15657 {
15658 if (attr->as_unsigned () >= TYPE_LENGTH (type))
15659 TYPE_LENGTH (type) = attr->as_unsigned ();
15660 else
15661 complaint (_("DW_AT_byte_size for array type smaller "
15662 "than the total size of elements"));
15663 }
15664
15665 name = dwarf2_name (die, cu);
15666 if (name)
15667 type->set_name (name);
15668
15669 maybe_set_alignment (cu, die, type);
15670
15671 struct type *replacement_type = nullptr;
15672 if (cu->lang () == language_ada)
15673 {
15674 replacement_type = quirk_ada_thick_pointer (die, cu, type);
15675 if (replacement_type != nullptr)
15676 type = replacement_type;
15677 }
15678
15679 /* Install the type in the die. */
15680 set_die_type (die, type, cu, replacement_type != nullptr);
15681
15682 /* set_die_type should be already done. */
15683 set_descriptive_type (type, die, cu);
15684
15685 return type;
15686 }
15687
15688 static enum dwarf_array_dim_ordering
15689 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15690 {
15691 struct attribute *attr;
15692
15693 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15694
15695 if (attr != nullptr)
15696 {
15697 LONGEST val = attr->constant_value (-1);
15698 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
15699 return (enum dwarf_array_dim_ordering) val;
15700 }
15701
15702 /* GNU F77 is a special case, as at 08/2004 array type info is the
15703 opposite order to the dwarf2 specification, but data is still
15704 laid out as per normal fortran.
15705
15706 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15707 version checking. */
15708
15709 if (cu->lang () == language_fortran
15710 && cu->producer && strstr (cu->producer, "GNU F77"))
15711 {
15712 return DW_ORD_row_major;
15713 }
15714
15715 switch (cu->language_defn->array_ordering ())
15716 {
15717 case array_column_major:
15718 return DW_ORD_col_major;
15719 case array_row_major:
15720 default:
15721 return DW_ORD_row_major;
15722 };
15723 }
15724
15725 /* Extract all information from a DW_TAG_set_type DIE and put it in
15726 the DIE's type field. */
15727
15728 static struct type *
15729 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15730 {
15731 struct type *domain_type, *set_type;
15732 struct attribute *attr;
15733
15734 domain_type = die_type (die, cu);
15735
15736 /* The die_type call above may have already set the type for this DIE. */
15737 set_type = get_die_type (die, cu);
15738 if (set_type)
15739 return set_type;
15740
15741 set_type = create_set_type (NULL, domain_type);
15742
15743 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15744 if (attr != nullptr && attr->form_is_unsigned ())
15745 TYPE_LENGTH (set_type) = attr->as_unsigned ();
15746
15747 maybe_set_alignment (cu, die, set_type);
15748
15749 return set_die_type (die, set_type, cu);
15750 }
15751
15752 /* A helper for read_common_block that creates a locexpr baton.
15753 SYM is the symbol which we are marking as computed.
15754 COMMON_DIE is the DIE for the common block.
15755 COMMON_LOC is the location expression attribute for the common
15756 block itself.
15757 MEMBER_LOC is the location expression attribute for the particular
15758 member of the common block that we are processing.
15759 CU is the CU from which the above come. */
15760
15761 static void
15762 mark_common_block_symbol_computed (struct symbol *sym,
15763 struct die_info *common_die,
15764 struct attribute *common_loc,
15765 struct attribute *member_loc,
15766 struct dwarf2_cu *cu)
15767 {
15768 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15769 struct objfile *objfile = per_objfile->objfile;
15770 struct dwarf2_locexpr_baton *baton;
15771 gdb_byte *ptr;
15772 unsigned int cu_off;
15773 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
15774 LONGEST offset = 0;
15775
15776 gdb_assert (common_loc && member_loc);
15777 gdb_assert (common_loc->form_is_block ());
15778 gdb_assert (member_loc->form_is_block ()
15779 || member_loc->form_is_constant ());
15780
15781 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15782 baton->per_objfile = per_objfile;
15783 baton->per_cu = cu->per_cu;
15784 gdb_assert (baton->per_cu);
15785
15786 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15787
15788 if (member_loc->form_is_constant ())
15789 {
15790 offset = member_loc->constant_value (0);
15791 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15792 }
15793 else
15794 baton->size += member_loc->as_block ()->size;
15795
15796 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15797 baton->data = ptr;
15798
15799 *ptr++ = DW_OP_call4;
15800 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15801 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15802 ptr += 4;
15803
15804 if (member_loc->form_is_constant ())
15805 {
15806 *ptr++ = DW_OP_addr;
15807 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15808 ptr += cu->header.addr_size;
15809 }
15810 else
15811 {
15812 /* We have to copy the data here, because DW_OP_call4 will only
15813 use a DW_AT_location attribute. */
15814 struct dwarf_block *block = member_loc->as_block ();
15815 memcpy (ptr, block->data, block->size);
15816 ptr += block->size;
15817 }
15818
15819 *ptr++ = DW_OP_plus;
15820 gdb_assert (ptr - baton->data == baton->size);
15821
15822 SYMBOL_LOCATION_BATON (sym) = baton;
15823 sym->set_aclass_index (dwarf2_locexpr_index);
15824 }
15825
15826 /* Create appropriate locally-scoped variables for all the
15827 DW_TAG_common_block entries. Also create a struct common_block
15828 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15829 is used to separate the common blocks name namespace from regular
15830 variable names. */
15831
15832 static void
15833 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15834 {
15835 struct attribute *attr;
15836
15837 attr = dwarf2_attr (die, DW_AT_location, cu);
15838 if (attr != nullptr)
15839 {
15840 /* Support the .debug_loc offsets. */
15841 if (attr->form_is_block ())
15842 {
15843 /* Ok. */
15844 }
15845 else if (attr->form_is_section_offset ())
15846 {
15847 dwarf2_complex_location_expr_complaint ();
15848 attr = NULL;
15849 }
15850 else
15851 {
15852 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15853 "common block member");
15854 attr = NULL;
15855 }
15856 }
15857
15858 if (die->child != NULL)
15859 {
15860 struct objfile *objfile = cu->per_objfile->objfile;
15861 struct die_info *child_die;
15862 size_t n_entries = 0, size;
15863 struct common_block *common_block;
15864 struct symbol *sym;
15865
15866 for (child_die = die->child;
15867 child_die && child_die->tag;
15868 child_die = child_die->sibling)
15869 ++n_entries;
15870
15871 size = (sizeof (struct common_block)
15872 + (n_entries - 1) * sizeof (struct symbol *));
15873 common_block
15874 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15875 size);
15876 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15877 common_block->n_entries = 0;
15878
15879 for (child_die = die->child;
15880 child_die && child_die->tag;
15881 child_die = child_die->sibling)
15882 {
15883 /* Create the symbol in the DW_TAG_common_block block in the current
15884 symbol scope. */
15885 sym = new_symbol (child_die, NULL, cu);
15886 if (sym != NULL)
15887 {
15888 struct attribute *member_loc;
15889
15890 common_block->contents[common_block->n_entries++] = sym;
15891
15892 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15893 cu);
15894 if (member_loc)
15895 {
15896 /* GDB has handled this for a long time, but it is
15897 not specified by DWARF. It seems to have been
15898 emitted by gfortran at least as recently as:
15899 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15900 complaint (_("Variable in common block has "
15901 "DW_AT_data_member_location "
15902 "- DIE at %s [in module %s]"),
15903 sect_offset_str (child_die->sect_off),
15904 objfile_name (objfile));
15905
15906 if (member_loc->form_is_section_offset ())
15907 dwarf2_complex_location_expr_complaint ();
15908 else if (member_loc->form_is_constant ()
15909 || member_loc->form_is_block ())
15910 {
15911 if (attr != nullptr)
15912 mark_common_block_symbol_computed (sym, die, attr,
15913 member_loc, cu);
15914 }
15915 else
15916 dwarf2_complex_location_expr_complaint ();
15917 }
15918 }
15919 }
15920
15921 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15922 sym->set_value_common_block (common_block);
15923 }
15924 }
15925
15926 /* Create a type for a C++ namespace. */
15927
15928 static struct type *
15929 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15930 {
15931 struct objfile *objfile = cu->per_objfile->objfile;
15932 const char *previous_prefix, *name;
15933 int is_anonymous;
15934 struct type *type;
15935
15936 /* For extensions, reuse the type of the original namespace. */
15937 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15938 {
15939 struct die_info *ext_die;
15940 struct dwarf2_cu *ext_cu = cu;
15941
15942 ext_die = dwarf2_extension (die, &ext_cu);
15943 type = read_type_die (ext_die, ext_cu);
15944
15945 /* EXT_CU may not be the same as CU.
15946 Ensure TYPE is recorded with CU in die_type_hash. */
15947 return set_die_type (die, type, cu);
15948 }
15949
15950 name = namespace_name (die, &is_anonymous, cu);
15951
15952 /* Now build the name of the current namespace. */
15953
15954 previous_prefix = determine_prefix (die, cu);
15955 if (previous_prefix[0] != '\0')
15956 name = typename_concat (&objfile->objfile_obstack,
15957 previous_prefix, name, 0, cu);
15958
15959 /* Create the type. */
15960 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
15961
15962 return set_die_type (die, type, cu);
15963 }
15964
15965 /* Read a namespace scope. */
15966
15967 static void
15968 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15969 {
15970 struct objfile *objfile = cu->per_objfile->objfile;
15971 int is_anonymous;
15972
15973 /* Add a symbol associated to this if we haven't seen the namespace
15974 before. Also, add a using directive if it's an anonymous
15975 namespace. */
15976
15977 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
15978 {
15979 struct type *type;
15980
15981 type = read_type_die (die, cu);
15982 new_symbol (die, type, cu);
15983
15984 namespace_name (die, &is_anonymous, cu);
15985 if (is_anonymous)
15986 {
15987 const char *previous_prefix = determine_prefix (die, cu);
15988
15989 std::vector<const char *> excludes;
15990 add_using_directive (using_directives (cu),
15991 previous_prefix, type->name (), NULL,
15992 NULL, excludes, 0, &objfile->objfile_obstack);
15993 }
15994 }
15995
15996 if (die->child != NULL)
15997 {
15998 struct die_info *child_die = die->child;
15999
16000 while (child_die && child_die->tag)
16001 {
16002 process_die (child_die, cu);
16003 child_die = child_die->sibling;
16004 }
16005 }
16006 }
16007
16008 /* Read a Fortran module as type. This DIE can be only a declaration used for
16009 imported module. Still we need that type as local Fortran "use ... only"
16010 declaration imports depend on the created type in determine_prefix. */
16011
16012 static struct type *
16013 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16014 {
16015 struct objfile *objfile = cu->per_objfile->objfile;
16016 const char *module_name;
16017 struct type *type;
16018
16019 module_name = dwarf2_name (die, cu);
16020 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16021
16022 return set_die_type (die, type, cu);
16023 }
16024
16025 /* Read a Fortran module. */
16026
16027 static void
16028 read_module (struct die_info *die, struct dwarf2_cu *cu)
16029 {
16030 struct die_info *child_die = die->child;
16031 struct type *type;
16032
16033 type = read_type_die (die, cu);
16034 new_symbol (die, type, cu);
16035
16036 while (child_die && child_die->tag)
16037 {
16038 process_die (child_die, cu);
16039 child_die = child_die->sibling;
16040 }
16041 }
16042
16043 /* Return the name of the namespace represented by DIE. Set
16044 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16045 namespace. */
16046
16047 static const char *
16048 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16049 {
16050 struct die_info *current_die;
16051 const char *name = NULL;
16052
16053 /* Loop through the extensions until we find a name. */
16054
16055 for (current_die = die;
16056 current_die != NULL;
16057 current_die = dwarf2_extension (die, &cu))
16058 {
16059 /* We don't use dwarf2_name here so that we can detect the absence
16060 of a name -> anonymous namespace. */
16061 name = dwarf2_string_attr (die, DW_AT_name, cu);
16062
16063 if (name != NULL)
16064 break;
16065 }
16066
16067 /* Is it an anonymous namespace? */
16068
16069 *is_anonymous = (name == NULL);
16070 if (*is_anonymous)
16071 name = CP_ANONYMOUS_NAMESPACE_STR;
16072
16073 return name;
16074 }
16075
16076 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16077 the user defined type vector. */
16078
16079 static struct type *
16080 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16081 {
16082 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16083 struct comp_unit_head *cu_header = &cu->header;
16084 struct type *type;
16085 struct attribute *attr_byte_size;
16086 struct attribute *attr_address_class;
16087 int byte_size, addr_class;
16088 struct type *target_type;
16089
16090 target_type = die_type (die, cu);
16091
16092 /* The die_type call above may have already set the type for this DIE. */
16093 type = get_die_type (die, cu);
16094 if (type)
16095 return type;
16096
16097 type = lookup_pointer_type (target_type);
16098
16099 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16100 if (attr_byte_size)
16101 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
16102 else
16103 byte_size = cu_header->addr_size;
16104
16105 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16106 if (attr_address_class)
16107 addr_class = attr_address_class->constant_value (DW_ADDR_none);
16108 else
16109 addr_class = DW_ADDR_none;
16110
16111 ULONGEST alignment = get_alignment (cu, die);
16112
16113 /* If the pointer size, alignment, or address class is different
16114 than the default, create a type variant marked as such and set
16115 the length accordingly. */
16116 if (TYPE_LENGTH (type) != byte_size
16117 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16118 && alignment != TYPE_RAW_ALIGN (type))
16119 || addr_class != DW_ADDR_none)
16120 {
16121 if (gdbarch_address_class_type_flags_p (gdbarch))
16122 {
16123 type_instance_flags type_flags
16124 = gdbarch_address_class_type_flags (gdbarch, byte_size,
16125 addr_class);
16126 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16127 == 0);
16128 type = make_type_with_address_space (type, type_flags);
16129 }
16130 else if (TYPE_LENGTH (type) != byte_size)
16131 {
16132 complaint (_("invalid pointer size %d"), byte_size);
16133 }
16134 else if (TYPE_RAW_ALIGN (type) != alignment)
16135 {
16136 complaint (_("Invalid DW_AT_alignment"
16137 " - DIE at %s [in module %s]"),
16138 sect_offset_str (die->sect_off),
16139 objfile_name (cu->per_objfile->objfile));
16140 }
16141 else
16142 {
16143 /* Should we also complain about unhandled address classes? */
16144 }
16145 }
16146
16147 TYPE_LENGTH (type) = byte_size;
16148 set_type_align (type, alignment);
16149 return set_die_type (die, type, cu);
16150 }
16151
16152 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16153 the user defined type vector. */
16154
16155 static struct type *
16156 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16157 {
16158 struct type *type;
16159 struct type *to_type;
16160 struct type *domain;
16161
16162 to_type = die_type (die, cu);
16163 domain = die_containing_type (die, cu);
16164
16165 /* The calls above may have already set the type for this DIE. */
16166 type = get_die_type (die, cu);
16167 if (type)
16168 return type;
16169
16170 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16171 type = lookup_methodptr_type (to_type);
16172 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16173 {
16174 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16175
16176 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16177 to_type->fields (), to_type->num_fields (),
16178 to_type->has_varargs ());
16179 type = lookup_methodptr_type (new_type);
16180 }
16181 else
16182 type = lookup_memberptr_type (to_type, domain);
16183
16184 return set_die_type (die, type, cu);
16185 }
16186
16187 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16188 the user defined type vector. */
16189
16190 static struct type *
16191 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16192 enum type_code refcode)
16193 {
16194 struct comp_unit_head *cu_header = &cu->header;
16195 struct type *type, *target_type;
16196 struct attribute *attr;
16197
16198 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16199
16200 target_type = die_type (die, cu);
16201
16202 /* The die_type call above may have already set the type for this DIE. */
16203 type = get_die_type (die, cu);
16204 if (type)
16205 return type;
16206
16207 type = lookup_reference_type (target_type, refcode);
16208 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16209 if (attr != nullptr)
16210 {
16211 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
16212 }
16213 else
16214 {
16215 TYPE_LENGTH (type) = cu_header->addr_size;
16216 }
16217 maybe_set_alignment (cu, die, type);
16218 return set_die_type (die, type, cu);
16219 }
16220
16221 /* Add the given cv-qualifiers to the element type of the array. GCC
16222 outputs DWARF type qualifiers that apply to an array, not the
16223 element type. But GDB relies on the array element type to carry
16224 the cv-qualifiers. This mimics section 6.7.3 of the C99
16225 specification. */
16226
16227 static struct type *
16228 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16229 struct type *base_type, int cnst, int voltl)
16230 {
16231 struct type *el_type, *inner_array;
16232
16233 base_type = copy_type (base_type);
16234 inner_array = base_type;
16235
16236 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16237 {
16238 TYPE_TARGET_TYPE (inner_array) =
16239 copy_type (TYPE_TARGET_TYPE (inner_array));
16240 inner_array = TYPE_TARGET_TYPE (inner_array);
16241 }
16242
16243 el_type = TYPE_TARGET_TYPE (inner_array);
16244 cnst |= TYPE_CONST (el_type);
16245 voltl |= TYPE_VOLATILE (el_type);
16246 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16247
16248 return set_die_type (die, base_type, cu);
16249 }
16250
16251 static struct type *
16252 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16253 {
16254 struct type *base_type, *cv_type;
16255
16256 base_type = die_type (die, cu);
16257
16258 /* The die_type call above may have already set the type for this DIE. */
16259 cv_type = get_die_type (die, cu);
16260 if (cv_type)
16261 return cv_type;
16262
16263 /* In case the const qualifier is applied to an array type, the element type
16264 is so qualified, not the array type (section 6.7.3 of C99). */
16265 if (base_type->code () == TYPE_CODE_ARRAY)
16266 return add_array_cv_type (die, cu, base_type, 1, 0);
16267
16268 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16269 return set_die_type (die, cv_type, cu);
16270 }
16271
16272 static struct type *
16273 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16274 {
16275 struct type *base_type, *cv_type;
16276
16277 base_type = die_type (die, cu);
16278
16279 /* The die_type call above may have already set the type for this DIE. */
16280 cv_type = get_die_type (die, cu);
16281 if (cv_type)
16282 return cv_type;
16283
16284 /* In case the volatile qualifier is applied to an array type, the
16285 element type is so qualified, not the array type (section 6.7.3
16286 of C99). */
16287 if (base_type->code () == TYPE_CODE_ARRAY)
16288 return add_array_cv_type (die, cu, base_type, 0, 1);
16289
16290 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16291 return set_die_type (die, cv_type, cu);
16292 }
16293
16294 /* Handle DW_TAG_restrict_type. */
16295
16296 static struct type *
16297 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16298 {
16299 struct type *base_type, *cv_type;
16300
16301 base_type = die_type (die, cu);
16302
16303 /* The die_type call above may have already set the type for this DIE. */
16304 cv_type = get_die_type (die, cu);
16305 if (cv_type)
16306 return cv_type;
16307
16308 cv_type = make_restrict_type (base_type);
16309 return set_die_type (die, cv_type, cu);
16310 }
16311
16312 /* Handle DW_TAG_atomic_type. */
16313
16314 static struct type *
16315 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16316 {
16317 struct type *base_type, *cv_type;
16318
16319 base_type = die_type (die, cu);
16320
16321 /* The die_type call above may have already set the type for this DIE. */
16322 cv_type = get_die_type (die, cu);
16323 if (cv_type)
16324 return cv_type;
16325
16326 cv_type = make_atomic_type (base_type);
16327 return set_die_type (die, cv_type, cu);
16328 }
16329
16330 /* Extract all information from a DW_TAG_string_type DIE and add to
16331 the user defined type vector. It isn't really a user defined type,
16332 but it behaves like one, with other DIE's using an AT_user_def_type
16333 attribute to reference it. */
16334
16335 static struct type *
16336 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16337 {
16338 struct objfile *objfile = cu->per_objfile->objfile;
16339 struct gdbarch *gdbarch = objfile->arch ();
16340 struct type *type, *range_type, *index_type, *char_type;
16341 struct attribute *attr;
16342 struct dynamic_prop prop;
16343 bool length_is_constant = true;
16344 LONGEST length;
16345
16346 /* There are a couple of places where bit sizes might be made use of
16347 when parsing a DW_TAG_string_type, however, no producer that we know
16348 of make use of these. Handling bit sizes that are a multiple of the
16349 byte size is easy enough, but what about other bit sizes? Lets deal
16350 with that problem when we have to. Warn about these attributes being
16351 unsupported, then parse the type and ignore them like we always
16352 have. */
16353 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16354 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16355 {
16356 static bool warning_printed = false;
16357 if (!warning_printed)
16358 {
16359 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16360 "currently supported on DW_TAG_string_type."));
16361 warning_printed = true;
16362 }
16363 }
16364
16365 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16366 if (attr != nullptr && !attr->form_is_constant ())
16367 {
16368 /* The string length describes the location at which the length of
16369 the string can be found. The size of the length field can be
16370 specified with one of the attributes below. */
16371 struct type *prop_type;
16372 struct attribute *len
16373 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16374 if (len == nullptr)
16375 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16376 if (len != nullptr && len->form_is_constant ())
16377 {
16378 /* Pass 0 as the default as we know this attribute is constant
16379 and the default value will not be returned. */
16380 LONGEST sz = len->constant_value (0);
16381 prop_type = objfile_int_type (objfile, sz, true);
16382 }
16383 else
16384 {
16385 /* If the size is not specified then we assume it is the size of
16386 an address on this target. */
16387 prop_type = cu->addr_sized_int_type (true);
16388 }
16389
16390 /* Convert the attribute into a dynamic property. */
16391 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16392 length = 1;
16393 else
16394 length_is_constant = false;
16395 }
16396 else if (attr != nullptr)
16397 {
16398 /* This DW_AT_string_length just contains the length with no
16399 indirection. There's no need to create a dynamic property in this
16400 case. Pass 0 for the default value as we know it will not be
16401 returned in this case. */
16402 length = attr->constant_value (0);
16403 }
16404 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16405 {
16406 /* We don't currently support non-constant byte sizes for strings. */
16407 length = attr->constant_value (1);
16408 }
16409 else
16410 {
16411 /* Use 1 as a fallback length if we have nothing else. */
16412 length = 1;
16413 }
16414
16415 index_type = objfile_type (objfile)->builtin_int;
16416 if (length_is_constant)
16417 range_type = create_static_range_type (NULL, index_type, 1, length);
16418 else
16419 {
16420 struct dynamic_prop low_bound;
16421
16422 low_bound.set_const_val (1);
16423 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16424 }
16425 char_type = language_string_char_type (cu->language_defn, gdbarch);
16426 type = create_string_type (NULL, char_type, range_type);
16427
16428 return set_die_type (die, type, cu);
16429 }
16430
16431 /* Assuming that DIE corresponds to a function, returns nonzero
16432 if the function is prototyped. */
16433
16434 static int
16435 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16436 {
16437 struct attribute *attr;
16438
16439 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16440 if (attr && attr->as_boolean ())
16441 return 1;
16442
16443 /* The DWARF standard implies that the DW_AT_prototyped attribute
16444 is only meaningful for C, but the concept also extends to other
16445 languages that allow unprototyped functions (Eg: Objective C).
16446 For all other languages, assume that functions are always
16447 prototyped. */
16448 if (cu->lang () != language_c
16449 && cu->lang () != language_objc
16450 && cu->lang () != language_opencl)
16451 return 1;
16452
16453 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16454 prototyped and unprototyped functions; default to prototyped,
16455 since that is more common in modern code (and RealView warns
16456 about unprototyped functions). */
16457 if (producer_is_realview (cu->producer))
16458 return 1;
16459
16460 return 0;
16461 }
16462
16463 /* Handle DIES due to C code like:
16464
16465 struct foo
16466 {
16467 int (*funcp)(int a, long l);
16468 int b;
16469 };
16470
16471 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16472
16473 static struct type *
16474 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16475 {
16476 struct objfile *objfile = cu->per_objfile->objfile;
16477 struct type *type; /* Type that this function returns. */
16478 struct type *ftype; /* Function that returns above type. */
16479 struct attribute *attr;
16480
16481 type = die_type (die, cu);
16482
16483 /* The die_type call above may have already set the type for this DIE. */
16484 ftype = get_die_type (die, cu);
16485 if (ftype)
16486 return ftype;
16487
16488 ftype = lookup_function_type (type);
16489
16490 if (prototyped_function_p (die, cu))
16491 ftype->set_is_prototyped (true);
16492
16493 /* Store the calling convention in the type if it's available in
16494 the subroutine die. Otherwise set the calling convention to
16495 the default value DW_CC_normal. */
16496 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16497 if (attr != nullptr
16498 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
16499 TYPE_CALLING_CONVENTION (ftype)
16500 = (enum dwarf_calling_convention) attr->constant_value (0);
16501 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16502 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16503 else
16504 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16505
16506 /* Record whether the function returns normally to its caller or not
16507 if the DWARF producer set that information. */
16508 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16509 if (attr && attr->as_boolean ())
16510 TYPE_NO_RETURN (ftype) = 1;
16511
16512 /* We need to add the subroutine type to the die immediately so
16513 we don't infinitely recurse when dealing with parameters
16514 declared as the same subroutine type. */
16515 set_die_type (die, ftype, cu);
16516
16517 if (die->child != NULL)
16518 {
16519 struct type *void_type = objfile_type (objfile)->builtin_void;
16520 struct die_info *child_die;
16521 int nparams, iparams;
16522
16523 /* Count the number of parameters.
16524 FIXME: GDB currently ignores vararg functions, but knows about
16525 vararg member functions. */
16526 nparams = 0;
16527 child_die = die->child;
16528 while (child_die && child_die->tag)
16529 {
16530 if (child_die->tag == DW_TAG_formal_parameter)
16531 nparams++;
16532 else if (child_die->tag == DW_TAG_unspecified_parameters)
16533 ftype->set_has_varargs (true);
16534
16535 child_die = child_die->sibling;
16536 }
16537
16538 /* Allocate storage for parameters and fill them in. */
16539 ftype->set_num_fields (nparams);
16540 ftype->set_fields
16541 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
16542
16543 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16544 even if we error out during the parameters reading below. */
16545 for (iparams = 0; iparams < nparams; iparams++)
16546 ftype->field (iparams).set_type (void_type);
16547
16548 iparams = 0;
16549 child_die = die->child;
16550 while (child_die && child_die->tag)
16551 {
16552 if (child_die->tag == DW_TAG_formal_parameter)
16553 {
16554 struct type *arg_type;
16555
16556 /* DWARF version 2 has no clean way to discern C++
16557 static and non-static member functions. G++ helps
16558 GDB by marking the first parameter for non-static
16559 member functions (which is the this pointer) as
16560 artificial. We pass this information to
16561 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16562
16563 DWARF version 3 added DW_AT_object_pointer, which GCC
16564 4.5 does not yet generate. */
16565 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16566 if (attr != nullptr)
16567 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
16568 else
16569 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16570 arg_type = die_type (child_die, cu);
16571
16572 /* RealView does not mark THIS as const, which the testsuite
16573 expects. GCC marks THIS as const in method definitions,
16574 but not in the class specifications (GCC PR 43053). */
16575 if (cu->lang () == language_cplus
16576 && !TYPE_CONST (arg_type)
16577 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16578 {
16579 int is_this = 0;
16580 struct dwarf2_cu *arg_cu = cu;
16581 const char *name = dwarf2_name (child_die, cu);
16582
16583 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16584 if (attr != nullptr)
16585 {
16586 /* If the compiler emits this, use it. */
16587 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16588 is_this = 1;
16589 }
16590 else if (name && strcmp (name, "this") == 0)
16591 /* Function definitions will have the argument names. */
16592 is_this = 1;
16593 else if (name == NULL && iparams == 0)
16594 /* Declarations may not have the names, so like
16595 elsewhere in GDB, assume an artificial first
16596 argument is "this". */
16597 is_this = 1;
16598
16599 if (is_this)
16600 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16601 arg_type, 0);
16602 }
16603
16604 ftype->field (iparams).set_type (arg_type);
16605 iparams++;
16606 }
16607 child_die = child_die->sibling;
16608 }
16609 }
16610
16611 return ftype;
16612 }
16613
16614 static struct type *
16615 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16616 {
16617 struct objfile *objfile = cu->per_objfile->objfile;
16618 const char *name = NULL;
16619 struct type *this_type, *target_type;
16620
16621 name = dwarf2_full_name (NULL, die, cu);
16622 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16623 this_type->set_target_is_stub (true);
16624 set_die_type (die, this_type, cu);
16625 target_type = die_type (die, cu);
16626 if (target_type != this_type)
16627 TYPE_TARGET_TYPE (this_type) = target_type;
16628 else
16629 {
16630 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16631 spec and cause infinite loops in GDB. */
16632 complaint (_("Self-referential DW_TAG_typedef "
16633 "- DIE at %s [in module %s]"),
16634 sect_offset_str (die->sect_off), objfile_name (objfile));
16635 TYPE_TARGET_TYPE (this_type) = NULL;
16636 }
16637 if (name == NULL)
16638 {
16639 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16640 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16641 Handle these by just returning the target type, rather than
16642 constructing an anonymous typedef type and trying to handle this
16643 elsewhere. */
16644 set_die_type (die, target_type, cu);
16645 return target_type;
16646 }
16647 return this_type;
16648 }
16649
16650 /* Helper for get_dwarf2_rational_constant that computes the value of
16651 a given gmp_mpz given an attribute. */
16652
16653 static void
16654 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
16655 {
16656 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16657 location expression that pushes an implicit value. */
16658 if (attr->form == DW_FORM_exprloc)
16659 {
16660 dwarf_block *blk = attr->as_block ();
16661 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
16662 {
16663 uint64_t len;
16664 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
16665 blk->data + blk->size,
16666 &len);
16667 if (ptr - blk->data + len <= blk->size)
16668 {
16669 mpz_import (value->val, len,
16670 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
16671 1, 0, 0, ptr);
16672 return;
16673 }
16674 }
16675
16676 /* On failure set it to 1. */
16677 *value = gdb_mpz (1);
16678 }
16679 else if (attr->form_is_block ())
16680 {
16681 dwarf_block *blk = attr->as_block ();
16682 mpz_import (value->val, blk->size,
16683 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
16684 1, 0, 0, blk->data);
16685 }
16686 else
16687 *value = gdb_mpz (attr->constant_value (1));
16688 }
16689
16690 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16691 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16692
16693 If the numerator and/or numerator attribute is missing,
16694 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16695 untouched. */
16696
16697 static void
16698 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
16699 gdb_mpz *numerator, gdb_mpz *denominator)
16700 {
16701 struct attribute *num_attr, *denom_attr;
16702
16703 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
16704 if (num_attr == nullptr)
16705 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16706 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16707
16708 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
16709 if (denom_attr == nullptr)
16710 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16711 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16712
16713 if (num_attr == nullptr || denom_attr == nullptr)
16714 return;
16715
16716 get_mpz (cu, numerator, num_attr);
16717 get_mpz (cu, denominator, denom_attr);
16718 }
16719
16720 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16721 rational constant, rather than a signed one.
16722
16723 If the rational constant has a negative value, a complaint
16724 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16725
16726 static void
16727 get_dwarf2_unsigned_rational_constant (struct die_info *die,
16728 struct dwarf2_cu *cu,
16729 gdb_mpz *numerator,
16730 gdb_mpz *denominator)
16731 {
16732 gdb_mpz num (1);
16733 gdb_mpz denom (1);
16734
16735 get_dwarf2_rational_constant (die, cu, &num, &denom);
16736 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
16737 {
16738 mpz_neg (num.val, num.val);
16739 mpz_neg (denom.val, denom.val);
16740 }
16741 else if (mpz_sgn (num.val) == -1)
16742 {
16743 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16744 " in DIE at %s"),
16745 sect_offset_str (die->sect_off));
16746 return;
16747 }
16748 else if (mpz_sgn (denom.val) == -1)
16749 {
16750 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16751 " in DIE at %s"),
16752 sect_offset_str (die->sect_off));
16753 return;
16754 }
16755
16756 *numerator = std::move (num);
16757 *denominator = std::move (denom);
16758 }
16759
16760 /* Assuming that ENCODING is a string whose contents starting at the
16761 K'th character is "_nn" where "nn" is a decimal number, scan that
16762 number and set RESULT to the value. K is updated to point to the
16763 character immediately following the number.
16764
16765 If the string does not conform to the format described above, false
16766 is returned, and K may or may not be changed. */
16767
16768 static bool
16769 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
16770 {
16771 /* The next character should be an underscore ('_') followed
16772 by a digit. */
16773 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
16774 return false;
16775
16776 /* Skip the underscore. */
16777 k++;
16778 int start = k;
16779
16780 /* Determine the number of digits for our number. */
16781 while (isdigit (encoding[k]))
16782 k++;
16783 if (k == start)
16784 return false;
16785
16786 std::string copy (&encoding[start], k - start);
16787 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
16788 return false;
16789
16790 return true;
16791 }
16792
16793 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16794 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16795 DENOM, update OFFSET, and return true on success. Return false on
16796 failure. */
16797
16798 static bool
16799 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
16800 gdb_mpz *num, gdb_mpz *denom)
16801 {
16802 if (!ada_get_gnat_encoded_number (encoding, offset, num))
16803 return false;
16804 return ada_get_gnat_encoded_number (encoding, offset, denom);
16805 }
16806
16807 /* Assuming DIE corresponds to a fixed point type, finish the creation
16808 of the corresponding TYPE by setting its type-specific data. CU is
16809 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16810 encodings. It is nullptr if the GNAT encoding should be
16811 ignored. */
16812
16813 static void
16814 finish_fixed_point_type (struct type *type, const char *suffix,
16815 struct die_info *die, struct dwarf2_cu *cu)
16816 {
16817 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
16818 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
16819
16820 /* If GNAT encodings are preferred, don't examine the
16821 attributes. */
16822 struct attribute *attr = nullptr;
16823 if (suffix == nullptr)
16824 {
16825 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
16826 if (attr == nullptr)
16827 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
16828 if (attr == nullptr)
16829 attr = dwarf2_attr (die, DW_AT_small, cu);
16830 }
16831
16832 /* Numerator and denominator of our fixed-point type's scaling factor.
16833 The default is a scaling factor of 1, which we use as a fallback
16834 when we are not able to decode it (problem with the debugging info,
16835 unsupported forms, bug in GDB, etc...). Using that as the default
16836 allows us to at least print the unscaled value, which might still
16837 be useful to a user. */
16838 gdb_mpz scale_num (1);
16839 gdb_mpz scale_denom (1);
16840
16841 if (attr == nullptr)
16842 {
16843 int offset = 0;
16844 if (suffix != nullptr
16845 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16846 &scale_denom)
16847 /* The number might be encoded as _nn_dd_nn_dd, where the
16848 second ratio is the 'small value. In this situation, we
16849 want the second value. */
16850 && (suffix[offset] != '_'
16851 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16852 &scale_denom)))
16853 {
16854 /* Found it. */
16855 }
16856 else
16857 {
16858 /* Scaling factor not found. Assume a scaling factor of 1,
16859 and hope for the best. At least the user will be able to
16860 see the encoded value. */
16861 scale_num = 1;
16862 scale_denom = 1;
16863 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16864 sect_offset_str (die->sect_off));
16865 }
16866 }
16867 else if (attr->name == DW_AT_binary_scale)
16868 {
16869 LONGEST scale_exp = attr->constant_value (0);
16870 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16871
16872 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
16873 }
16874 else if (attr->name == DW_AT_decimal_scale)
16875 {
16876 LONGEST scale_exp = attr->constant_value (0);
16877 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16878
16879 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
16880 }
16881 else if (attr->name == DW_AT_small)
16882 {
16883 struct die_info *scale_die;
16884 struct dwarf2_cu *scale_cu = cu;
16885
16886 scale_die = follow_die_ref (die, attr, &scale_cu);
16887 if (scale_die->tag == DW_TAG_constant)
16888 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
16889 &scale_num, &scale_denom);
16890 else
16891 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16892 " (DIE at %s)"),
16893 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16894 }
16895 else
16896 {
16897 complaint (_("unsupported scale attribute %s for fixed-point type"
16898 " (DIE at %s)"),
16899 dwarf_attr_name (attr->name),
16900 sect_offset_str (die->sect_off));
16901 }
16902
16903 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
16904 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
16905 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
16906 mpq_canonicalize (scaling_factor.val);
16907 }
16908
16909 /* The gnat-encoding suffix for fixed point. */
16910
16911 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16912
16913 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16914 "XF" suffix of the name. The text after this is what encodes the
16915 'small and 'delta information. Otherwise, return nullptr. */
16916
16917 static const char *
16918 gnat_encoded_fixed_point_type_info (const char *name)
16919 {
16920 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
16921 }
16922
16923 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16924 (which may be different from NAME) to the architecture back-end to allow
16925 it to guess the correct format if necessary. */
16926
16927 static struct type *
16928 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16929 const char *name_hint, enum bfd_endian byte_order)
16930 {
16931 struct gdbarch *gdbarch = objfile->arch ();
16932 const struct floatformat **format;
16933 struct type *type;
16934
16935 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16936 if (format)
16937 type = init_float_type (objfile, bits, name, format, byte_order);
16938 else
16939 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16940
16941 return type;
16942 }
16943
16944 /* Allocate an integer type of size BITS and name NAME. */
16945
16946 static struct type *
16947 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16948 int bits, int unsigned_p, const char *name)
16949 {
16950 struct type *type;
16951
16952 /* Versions of Intel's C Compiler generate an integer type called "void"
16953 instead of using DW_TAG_unspecified_type. This has been seen on
16954 at least versions 14, 17, and 18. */
16955 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16956 && strcmp (name, "void") == 0)
16957 type = objfile_type (objfile)->builtin_void;
16958 else
16959 type = init_integer_type (objfile, bits, unsigned_p, name);
16960
16961 return type;
16962 }
16963
16964 /* Return true if DIE has a DW_AT_small attribute whose value is
16965 a constant rational, where both the numerator and denominator
16966 are equal to zero.
16967
16968 CU is the DIE's Compilation Unit. */
16969
16970 static bool
16971 has_zero_over_zero_small_attribute (struct die_info *die,
16972 struct dwarf2_cu *cu)
16973 {
16974 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
16975 if (attr == nullptr)
16976 return false;
16977
16978 struct dwarf2_cu *scale_cu = cu;
16979 struct die_info *scale_die
16980 = follow_die_ref (die, attr, &scale_cu);
16981
16982 if (scale_die->tag != DW_TAG_constant)
16983 return false;
16984
16985 gdb_mpz num (1), denom (1);
16986 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
16987 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
16988 }
16989
16990 /* Initialise and return a floating point type of size BITS suitable for
16991 use as a component of a complex number. The NAME_HINT is passed through
16992 when initialising the floating point type and is the name of the complex
16993 type.
16994
16995 As DWARF doesn't currently provide an explicit name for the components
16996 of a complex number, but it can be helpful to have these components
16997 named, we try to select a suitable name based on the size of the
16998 component. */
16999 static struct type *
17000 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17001 struct objfile *objfile,
17002 int bits, const char *name_hint,
17003 enum bfd_endian byte_order)
17004 {
17005 gdbarch *gdbarch = objfile->arch ();
17006 struct type *tt = nullptr;
17007
17008 /* Try to find a suitable floating point builtin type of size BITS.
17009 We're going to use the name of this type as the name for the complex
17010 target type that we are about to create. */
17011 switch (cu->lang ())
17012 {
17013 case language_fortran:
17014 switch (bits)
17015 {
17016 case 32:
17017 tt = builtin_f_type (gdbarch)->builtin_real;
17018 break;
17019 case 64:
17020 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17021 break;
17022 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17023 case 128:
17024 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17025 break;
17026 }
17027 break;
17028 default:
17029 switch (bits)
17030 {
17031 case 32:
17032 tt = builtin_type (gdbarch)->builtin_float;
17033 break;
17034 case 64:
17035 tt = builtin_type (gdbarch)->builtin_double;
17036 break;
17037 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17038 case 128:
17039 tt = builtin_type (gdbarch)->builtin_long_double;
17040 break;
17041 }
17042 break;
17043 }
17044
17045 /* If the type we found doesn't match the size we were looking for, then
17046 pretend we didn't find a type at all, the complex target type we
17047 create will then be nameless. */
17048 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17049 tt = nullptr;
17050
17051 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17052 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17053 }
17054
17055 /* Find a representation of a given base type and install
17056 it in the TYPE field of the die. */
17057
17058 static struct type *
17059 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17060 {
17061 struct objfile *objfile = cu->per_objfile->objfile;
17062 struct type *type;
17063 struct attribute *attr;
17064 int encoding = 0, bits = 0;
17065 const char *name;
17066 gdbarch *arch;
17067
17068 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17069 if (attr != nullptr && attr->form_is_constant ())
17070 encoding = attr->constant_value (0);
17071 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17072 if (attr != nullptr)
17073 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17074 name = dwarf2_name (die, cu);
17075 if (!name)
17076 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17077
17078 arch = objfile->arch ();
17079 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17080
17081 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17082 if (attr != nullptr && attr->form_is_constant ())
17083 {
17084 int endianity = attr->constant_value (0);
17085
17086 switch (endianity)
17087 {
17088 case DW_END_big:
17089 byte_order = BFD_ENDIAN_BIG;
17090 break;
17091 case DW_END_little:
17092 byte_order = BFD_ENDIAN_LITTLE;
17093 break;
17094 default:
17095 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17096 break;
17097 }
17098 }
17099
17100 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
17101 && cu->lang () == language_ada
17102 && has_zero_over_zero_small_attribute (die, cu))
17103 {
17104 /* brobecker/2018-02-24: This is a fixed point type for which
17105 the scaling factor is represented as fraction whose value
17106 does not make sense (zero divided by zero), so we should
17107 normally never see these. However, there is a small category
17108 of fixed point types for which GNAT is unable to provide
17109 the scaling factor via the standard DWARF mechanisms, and
17110 for which the info is provided via the GNAT encodings instead.
17111 This is likely what this DIE is about. */
17112 encoding = (encoding == DW_ATE_signed_fixed
17113 ? DW_ATE_signed
17114 : DW_ATE_unsigned);
17115 }
17116
17117 /* With GNAT encodings, fixed-point information will be encoded in
17118 the type name. Note that this can also occur with the above
17119 zero-over-zero case, which is why this is a separate "if" rather
17120 than an "else if". */
17121 const char *gnat_encoding_suffix = nullptr;
17122 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
17123 && cu->lang () == language_ada
17124 && name != nullptr)
17125 {
17126 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
17127 if (gnat_encoding_suffix != nullptr)
17128 {
17129 gdb_assert (startswith (gnat_encoding_suffix,
17130 GNAT_FIXED_POINT_SUFFIX));
17131 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
17132 name, gnat_encoding_suffix - name);
17133 /* Use -1 here so that SUFFIX points at the "_" after the
17134 "XF". */
17135 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
17136
17137 encoding = (encoding == DW_ATE_signed
17138 ? DW_ATE_signed_fixed
17139 : DW_ATE_unsigned_fixed);
17140 }
17141 }
17142
17143 switch (encoding)
17144 {
17145 case DW_ATE_address:
17146 /* Turn DW_ATE_address into a void * pointer. */
17147 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17148 type = init_pointer_type (objfile, bits, name, type);
17149 break;
17150 case DW_ATE_boolean:
17151 type = init_boolean_type (objfile, bits, 1, name);
17152 break;
17153 case DW_ATE_complex_float:
17154 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17155 byte_order);
17156 if (type->code () == TYPE_CODE_ERROR)
17157 {
17158 if (name == nullptr)
17159 {
17160 struct obstack *obstack
17161 = &cu->per_objfile->objfile->objfile_obstack;
17162 name = obconcat (obstack, "_Complex ", type->name (),
17163 nullptr);
17164 }
17165 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17166 }
17167 else
17168 type = init_complex_type (name, type);
17169 break;
17170 case DW_ATE_decimal_float:
17171 type = init_decfloat_type (objfile, bits, name);
17172 break;
17173 case DW_ATE_float:
17174 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17175 break;
17176 case DW_ATE_signed:
17177 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17178 break;
17179 case DW_ATE_unsigned:
17180 if (cu->lang () == language_fortran
17181 && name
17182 && startswith (name, "character("))
17183 type = init_character_type (objfile, bits, 1, name);
17184 else
17185 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17186 break;
17187 case DW_ATE_signed_char:
17188 if (cu->lang () == language_ada
17189 || cu->lang () == language_m2
17190 || cu->lang () == language_pascal
17191 || cu->lang () == language_fortran)
17192 type = init_character_type (objfile, bits, 0, name);
17193 else
17194 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17195 break;
17196 case DW_ATE_unsigned_char:
17197 if (cu->lang () == language_ada
17198 || cu->lang () == language_m2
17199 || cu->lang () == language_pascal
17200 || cu->lang () == language_fortran
17201 || cu->lang () == language_rust)
17202 type = init_character_type (objfile, bits, 1, name);
17203 else
17204 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17205 break;
17206 case DW_ATE_UTF:
17207 {
17208 type = init_character_type (objfile, bits, 1, name);
17209 return set_die_type (die, type, cu);
17210 }
17211 break;
17212 case DW_ATE_signed_fixed:
17213 type = init_fixed_point_type (objfile, bits, 0, name);
17214 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17215 break;
17216 case DW_ATE_unsigned_fixed:
17217 type = init_fixed_point_type (objfile, bits, 1, name);
17218 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17219 break;
17220
17221 default:
17222 complaint (_("unsupported DW_AT_encoding: '%s'"),
17223 dwarf_type_encoding_name (encoding));
17224 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17225 break;
17226 }
17227
17228 if (type->code () == TYPE_CODE_INT
17229 && name != nullptr
17230 && strcmp (name, "char") == 0)
17231 type->set_has_no_signedness (true);
17232
17233 maybe_set_alignment (cu, die, type);
17234
17235 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
17236
17237 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
17238 {
17239 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
17240 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
17241 {
17242 unsigned real_bit_size = attr->as_unsigned ();
17243 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
17244 /* Only use the attributes if they make sense together. */
17245 if (attr == nullptr
17246 || (attr->as_unsigned () + real_bit_size
17247 <= 8 * TYPE_LENGTH (type)))
17248 {
17249 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
17250 = real_bit_size;
17251 if (attr != nullptr)
17252 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
17253 = attr->as_unsigned ();
17254 }
17255 }
17256 }
17257
17258 return set_die_type (die, type, cu);
17259 }
17260
17261 /* A helper function that returns the name of DIE, if it refers to a
17262 variable declaration. */
17263
17264 static const char *
17265 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
17266 {
17267 if (die->tag != DW_TAG_variable)
17268 return nullptr;
17269
17270 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
17271 if (attr == nullptr || !attr->as_boolean ())
17272 return nullptr;
17273
17274 attr = dwarf2_attr (die, DW_AT_name, cu);
17275 if (attr == nullptr)
17276 return nullptr;
17277 return attr->as_string ();
17278 }
17279
17280 /* Parse dwarf attribute if it's a block, reference or constant and put the
17281 resulting value of the attribute into struct bound_prop.
17282 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17283
17284 static int
17285 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17286 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17287 struct type *default_type)
17288 {
17289 struct dwarf2_property_baton *baton;
17290 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17291 struct objfile *objfile = per_objfile->objfile;
17292 struct obstack *obstack = &objfile->objfile_obstack;
17293
17294 gdb_assert (default_type != NULL);
17295
17296 if (attr == NULL || prop == NULL)
17297 return 0;
17298
17299 if (attr->form_is_block ())
17300 {
17301 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17302 baton->property_type = default_type;
17303 baton->locexpr.per_cu = cu->per_cu;
17304 baton->locexpr.per_objfile = per_objfile;
17305
17306 struct dwarf_block *block;
17307 if (attr->form == DW_FORM_data16)
17308 {
17309 size_t data_size = 16;
17310 block = XOBNEW (obstack, struct dwarf_block);
17311 block->size = (data_size
17312 + 2 /* Extra bytes for DW_OP and arg. */);
17313 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
17314 data[0] = DW_OP_implicit_value;
17315 data[1] = data_size;
17316 memcpy (&data[2], attr->as_block ()->data, data_size);
17317 block->data = data;
17318 }
17319 else
17320 block = attr->as_block ();
17321
17322 baton->locexpr.size = block->size;
17323 baton->locexpr.data = block->data;
17324 switch (attr->name)
17325 {
17326 case DW_AT_string_length:
17327 baton->locexpr.is_reference = true;
17328 break;
17329 default:
17330 baton->locexpr.is_reference = false;
17331 break;
17332 }
17333
17334 prop->set_locexpr (baton);
17335 gdb_assert (prop->baton () != NULL);
17336 }
17337 else if (attr->form_is_ref ())
17338 {
17339 struct dwarf2_cu *target_cu = cu;
17340 struct die_info *target_die;
17341 struct attribute *target_attr;
17342
17343 target_die = follow_die_ref (die, attr, &target_cu);
17344 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17345 if (target_attr == NULL)
17346 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17347 target_cu);
17348 if (target_attr == nullptr)
17349 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
17350 target_cu);
17351 if (target_attr == NULL)
17352 {
17353 const char *name = var_decl_name (target_die, target_cu);
17354 if (name != nullptr)
17355 {
17356 prop->set_variable_name (name);
17357 return 1;
17358 }
17359 return 0;
17360 }
17361
17362 switch (target_attr->name)
17363 {
17364 case DW_AT_location:
17365 if (target_attr->form_is_section_offset ())
17366 {
17367 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17368 baton->property_type = die_type (target_die, target_cu);
17369 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17370 prop->set_loclist (baton);
17371 gdb_assert (prop->baton () != NULL);
17372 }
17373 else if (target_attr->form_is_block ())
17374 {
17375 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17376 baton->property_type = die_type (target_die, target_cu);
17377 baton->locexpr.per_cu = cu->per_cu;
17378 baton->locexpr.per_objfile = per_objfile;
17379 struct dwarf_block *block = target_attr->as_block ();
17380 baton->locexpr.size = block->size;
17381 baton->locexpr.data = block->data;
17382 baton->locexpr.is_reference = true;
17383 prop->set_locexpr (baton);
17384 gdb_assert (prop->baton () != NULL);
17385 }
17386 else
17387 {
17388 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17389 "dynamic property");
17390 return 0;
17391 }
17392 break;
17393 case DW_AT_data_member_location:
17394 case DW_AT_data_bit_offset:
17395 {
17396 LONGEST offset;
17397
17398 if (!handle_member_location (target_die, target_cu, &offset))
17399 return 0;
17400
17401 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17402 baton->property_type = read_type_die (target_die->parent,
17403 target_cu);
17404 baton->offset_info.offset = offset;
17405 baton->offset_info.type = die_type (target_die, target_cu);
17406 prop->set_addr_offset (baton);
17407 break;
17408 }
17409 }
17410 }
17411 else if (attr->form_is_constant ())
17412 prop->set_const_val (attr->constant_value (0));
17413 else if (attr->form_is_section_offset ())
17414 {
17415 switch (attr->name)
17416 {
17417 case DW_AT_string_length:
17418 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17419 baton->property_type = default_type;
17420 fill_in_loclist_baton (cu, &baton->loclist, attr);
17421 prop->set_loclist (baton);
17422 gdb_assert (prop->baton () != NULL);
17423 break;
17424 default:
17425 goto invalid;
17426 }
17427 }
17428 else
17429 goto invalid;
17430
17431 return 1;
17432
17433 invalid:
17434 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17435 dwarf2_name (die, cu));
17436 return 0;
17437 }
17438
17439 /* See read.h. */
17440
17441 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17442 present (which is valid) then compute the default type based on the
17443 compilation units address size. */
17444
17445 static struct type *
17446 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17447 {
17448 struct type *index_type = die_type (die, cu);
17449
17450 /* Dwarf-2 specifications explicitly allows to create subrange types
17451 without specifying a base type.
17452 In that case, the base type must be set to the type of
17453 the lower bound, upper bound or count, in that order, if any of these
17454 three attributes references an object that has a type.
17455 If no base type is found, the Dwarf-2 specifications say that
17456 a signed integer type of size equal to the size of an address should
17457 be used.
17458 For the following C code: `extern char gdb_int [];'
17459 GCC produces an empty range DIE.
17460 FIXME: muller/2010-05-28: Possible references to object for low bound,
17461 high bound or count are not yet handled by this code. */
17462 if (index_type->code () == TYPE_CODE_VOID)
17463 index_type = cu->addr_sized_int_type (false);
17464
17465 return index_type;
17466 }
17467
17468 /* Read the given DW_AT_subrange DIE. */
17469
17470 static struct type *
17471 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17472 {
17473 struct type *base_type, *orig_base_type;
17474 struct type *range_type;
17475 struct attribute *attr;
17476 struct dynamic_prop low, high;
17477 int low_default_is_valid;
17478 int high_bound_is_count = 0;
17479 const char *name;
17480 ULONGEST negative_mask;
17481
17482 orig_base_type = read_subrange_index_type (die, cu);
17483
17484 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17485 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17486 creating the range type, but we use the result of check_typedef
17487 when examining properties of the type. */
17488 base_type = check_typedef (orig_base_type);
17489
17490 /* The die_type call above may have already set the type for this DIE. */
17491 range_type = get_die_type (die, cu);
17492 if (range_type)
17493 return range_type;
17494
17495 high.set_const_val (0);
17496
17497 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17498 omitting DW_AT_lower_bound. */
17499 switch (cu->lang ())
17500 {
17501 case language_c:
17502 case language_cplus:
17503 low.set_const_val (0);
17504 low_default_is_valid = 1;
17505 break;
17506 case language_fortran:
17507 low.set_const_val (1);
17508 low_default_is_valid = 1;
17509 break;
17510 case language_d:
17511 case language_objc:
17512 case language_rust:
17513 low.set_const_val (0);
17514 low_default_is_valid = (cu->header.version >= 4);
17515 break;
17516 case language_ada:
17517 case language_m2:
17518 case language_pascal:
17519 low.set_const_val (1);
17520 low_default_is_valid = (cu->header.version >= 4);
17521 break;
17522 default:
17523 low.set_const_val (0);
17524 low_default_is_valid = 0;
17525 break;
17526 }
17527
17528 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17529 if (attr != nullptr)
17530 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17531 else if (!low_default_is_valid)
17532 complaint (_("Missing DW_AT_lower_bound "
17533 "- DIE at %s [in module %s]"),
17534 sect_offset_str (die->sect_off),
17535 objfile_name (cu->per_objfile->objfile));
17536
17537 struct attribute *attr_ub, *attr_count;
17538 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17539 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17540 {
17541 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17542 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17543 {
17544 /* If bounds are constant do the final calculation here. */
17545 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
17546 high.set_const_val (low.const_val () + high.const_val () - 1);
17547 else
17548 high_bound_is_count = 1;
17549 }
17550 else
17551 {
17552 if (attr_ub != NULL)
17553 complaint (_("Unresolved DW_AT_upper_bound "
17554 "- DIE at %s [in module %s]"),
17555 sect_offset_str (die->sect_off),
17556 objfile_name (cu->per_objfile->objfile));
17557 if (attr_count != NULL)
17558 complaint (_("Unresolved DW_AT_count "
17559 "- DIE at %s [in module %s]"),
17560 sect_offset_str (die->sect_off),
17561 objfile_name (cu->per_objfile->objfile));
17562 }
17563 }
17564
17565 LONGEST bias = 0;
17566 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17567 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17568 bias = bias_attr->constant_value (0);
17569
17570 /* Normally, the DWARF producers are expected to use a signed
17571 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17572 But this is unfortunately not always the case, as witnessed
17573 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17574 is used instead. To work around that ambiguity, we treat
17575 the bounds as signed, and thus sign-extend their values, when
17576 the base type is signed. */
17577 negative_mask =
17578 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17579 if (low.kind () == PROP_CONST
17580 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
17581 low.set_const_val (low.const_val () | negative_mask);
17582 if (high.kind () == PROP_CONST
17583 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
17584 high.set_const_val (high.const_val () | negative_mask);
17585
17586 /* Check for bit and byte strides. */
17587 struct dynamic_prop byte_stride_prop;
17588 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17589 if (attr_byte_stride != nullptr)
17590 {
17591 struct type *prop_type = cu->addr_sized_int_type (false);
17592 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17593 prop_type);
17594 }
17595
17596 struct dynamic_prop bit_stride_prop;
17597 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17598 if (attr_bit_stride != nullptr)
17599 {
17600 /* It only makes sense to have either a bit or byte stride. */
17601 if (attr_byte_stride != nullptr)
17602 {
17603 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17604 "- DIE at %s [in module %s]"),
17605 sect_offset_str (die->sect_off),
17606 objfile_name (cu->per_objfile->objfile));
17607 attr_bit_stride = nullptr;
17608 }
17609 else
17610 {
17611 struct type *prop_type = cu->addr_sized_int_type (false);
17612 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17613 prop_type);
17614 }
17615 }
17616
17617 if (attr_byte_stride != nullptr
17618 || attr_bit_stride != nullptr)
17619 {
17620 bool byte_stride_p = (attr_byte_stride != nullptr);
17621 struct dynamic_prop *stride
17622 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17623
17624 range_type
17625 = create_range_type_with_stride (NULL, orig_base_type, &low,
17626 &high, bias, stride, byte_stride_p);
17627 }
17628 else
17629 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17630
17631 if (high_bound_is_count)
17632 range_type->bounds ()->flag_upper_bound_is_count = 1;
17633
17634 /* Ada expects an empty array on no boundary attributes. */
17635 if (attr == NULL && cu->lang () != language_ada)
17636 range_type->bounds ()->high.set_undefined ();
17637
17638 name = dwarf2_name (die, cu);
17639 if (name)
17640 range_type->set_name (name);
17641
17642 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17643 if (attr != nullptr)
17644 TYPE_LENGTH (range_type) = attr->constant_value (0);
17645
17646 maybe_set_alignment (cu, die, range_type);
17647
17648 set_die_type (die, range_type, cu);
17649
17650 /* set_die_type should be already done. */
17651 set_descriptive_type (range_type, die, cu);
17652
17653 return range_type;
17654 }
17655
17656 static struct type *
17657 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17658 {
17659 struct type *type;
17660
17661 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17662 type->set_name (dwarf2_name (die, cu));
17663
17664 /* In Ada, an unspecified type is typically used when the description
17665 of the type is deferred to a different unit. When encountering
17666 such a type, we treat it as a stub, and try to resolve it later on,
17667 when needed. */
17668 if (cu->lang () == language_ada)
17669 type->set_is_stub (true);
17670
17671 return set_die_type (die, type, cu);
17672 }
17673
17674 /* Read a single die and all its descendents. Set the die's sibling
17675 field to NULL; set other fields in the die correctly, and set all
17676 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17677 location of the info_ptr after reading all of those dies. PARENT
17678 is the parent of the die in question. */
17679
17680 static struct die_info *
17681 read_die_and_children (const struct die_reader_specs *reader,
17682 const gdb_byte *info_ptr,
17683 const gdb_byte **new_info_ptr,
17684 struct die_info *parent)
17685 {
17686 struct die_info *die;
17687 const gdb_byte *cur_ptr;
17688
17689 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17690 if (die == NULL)
17691 {
17692 *new_info_ptr = cur_ptr;
17693 return NULL;
17694 }
17695 store_in_ref_table (die, reader->cu);
17696
17697 if (die->has_children)
17698 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17699 else
17700 {
17701 die->child = NULL;
17702 *new_info_ptr = cur_ptr;
17703 }
17704
17705 die->sibling = NULL;
17706 die->parent = parent;
17707 return die;
17708 }
17709
17710 /* Read a die, all of its descendents, and all of its siblings; set
17711 all of the fields of all of the dies correctly. Arguments are as
17712 in read_die_and_children. */
17713
17714 static struct die_info *
17715 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17716 const gdb_byte *info_ptr,
17717 const gdb_byte **new_info_ptr,
17718 struct die_info *parent)
17719 {
17720 struct die_info *first_die, *last_sibling;
17721 const gdb_byte *cur_ptr;
17722
17723 cur_ptr = info_ptr;
17724 first_die = last_sibling = NULL;
17725
17726 while (1)
17727 {
17728 struct die_info *die
17729 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17730
17731 if (die == NULL)
17732 {
17733 *new_info_ptr = cur_ptr;
17734 return first_die;
17735 }
17736
17737 if (!first_die)
17738 first_die = die;
17739 else
17740 last_sibling->sibling = die;
17741
17742 last_sibling = die;
17743 }
17744 }
17745
17746 /* Read a die, all of its descendents, and all of its siblings; set
17747 all of the fields of all of the dies correctly. Arguments are as
17748 in read_die_and_children.
17749 This the main entry point for reading a DIE and all its children. */
17750
17751 static struct die_info *
17752 read_die_and_siblings (const struct die_reader_specs *reader,
17753 const gdb_byte *info_ptr,
17754 const gdb_byte **new_info_ptr,
17755 struct die_info *parent)
17756 {
17757 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17758 new_info_ptr, parent);
17759
17760 if (dwarf_die_debug)
17761 {
17762 gdb_printf (gdb_stdlog,
17763 "Read die from %s@0x%x of %s:\n",
17764 reader->die_section->get_name (),
17765 (unsigned) (info_ptr - reader->die_section->buffer),
17766 bfd_get_filename (reader->abfd));
17767 dump_die (die, dwarf_die_debug);
17768 }
17769
17770 return die;
17771 }
17772
17773 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17774 attributes.
17775 The caller is responsible for filling in the extra attributes
17776 and updating (*DIEP)->num_attrs.
17777 Set DIEP to point to a newly allocated die with its information,
17778 except for its child, sibling, and parent fields. */
17779
17780 static const gdb_byte *
17781 read_full_die_1 (const struct die_reader_specs *reader,
17782 struct die_info **diep, const gdb_byte *info_ptr,
17783 int num_extra_attrs)
17784 {
17785 unsigned int abbrev_number, bytes_read, i;
17786 const struct abbrev_info *abbrev;
17787 struct die_info *die;
17788 struct dwarf2_cu *cu = reader->cu;
17789 bfd *abfd = reader->abfd;
17790
17791 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17792 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17793 info_ptr += bytes_read;
17794 if (!abbrev_number)
17795 {
17796 *diep = NULL;
17797 return info_ptr;
17798 }
17799
17800 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17801 if (!abbrev)
17802 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17803 abbrev_number,
17804 bfd_get_filename (abfd));
17805
17806 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17807 die->sect_off = sect_off;
17808 die->tag = abbrev->tag;
17809 die->abbrev = abbrev_number;
17810 die->has_children = abbrev->has_children;
17811
17812 /* Make the result usable.
17813 The caller needs to update num_attrs after adding the extra
17814 attributes. */
17815 die->num_attrs = abbrev->num_attrs;
17816
17817 bool any_need_reprocess = false;
17818 for (i = 0; i < abbrev->num_attrs; ++i)
17819 {
17820 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17821 info_ptr);
17822 if (die->attrs[i].requires_reprocessing_p ())
17823 any_need_reprocess = true;
17824 }
17825
17826 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17827 if (attr != nullptr && attr->form_is_unsigned ())
17828 cu->str_offsets_base = attr->as_unsigned ();
17829
17830 attr = die->attr (DW_AT_loclists_base);
17831 if (attr != nullptr)
17832 cu->loclist_base = attr->as_unsigned ();
17833
17834 auto maybe_addr_base = die->addr_base ();
17835 if (maybe_addr_base.has_value ())
17836 cu->addr_base = *maybe_addr_base;
17837
17838 attr = die->attr (DW_AT_rnglists_base);
17839 if (attr != nullptr)
17840 cu->rnglists_base = attr->as_unsigned ();
17841
17842 if (any_need_reprocess)
17843 {
17844 for (i = 0; i < abbrev->num_attrs; ++i)
17845 {
17846 if (die->attrs[i].requires_reprocessing_p ())
17847 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
17848 }
17849 }
17850 *diep = die;
17851 return info_ptr;
17852 }
17853
17854 /* Read a die and all its attributes.
17855 Set DIEP to point to a newly allocated die with its information,
17856 except for its child, sibling, and parent fields. */
17857
17858 static const gdb_byte *
17859 read_full_die (const struct die_reader_specs *reader,
17860 struct die_info **diep, const gdb_byte *info_ptr)
17861 {
17862 const gdb_byte *result;
17863
17864 result = read_full_die_1 (reader, diep, info_ptr, 0);
17865
17866 if (dwarf_die_debug)
17867 {
17868 gdb_printf (gdb_stdlog,
17869 "Read die from %s@0x%x of %s:\n",
17870 reader->die_section->get_name (),
17871 (unsigned) (info_ptr - reader->die_section->buffer),
17872 bfd_get_filename (reader->abfd));
17873 dump_die (*diep, dwarf_die_debug);
17874 }
17875
17876 return result;
17877 }
17878 \f
17879
17880 void
17881 cooked_indexer::check_bounds (cutu_reader *reader)
17882 {
17883 if (reader->cu->per_cu->addresses_seen)
17884 return;
17885
17886 dwarf2_cu *cu = reader->cu;
17887
17888 CORE_ADDR best_lowpc = 0, best_highpc = 0;
17889 /* Possibly set the default values of LOWPC and HIGHPC from
17890 `DW_AT_ranges'. */
17891 dwarf2_find_base_address (reader->comp_unit_die, cu);
17892 enum pc_bounds_kind cu_bounds_kind
17893 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
17894 cu, m_index_storage->get_addrmap (), cu->per_cu);
17895 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
17896 {
17897 struct objfile *objfile = cu->per_objfile->objfile;
17898 CORE_ADDR baseaddr = objfile->text_section_offset ();
17899 struct gdbarch *gdbarch = objfile->arch ();
17900 CORE_ADDR low
17901 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
17902 - baseaddr);
17903 CORE_ADDR high
17904 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
17905 - baseaddr - 1);
17906 /* Store the contiguous range if it is not empty; it can be
17907 empty for CUs with no code. */
17908 m_index_storage->get_addrmap ()->set_empty (low, high, cu->per_cu);
17909
17910 cu->per_cu->addresses_seen = true;
17911 }
17912 }
17913
17914 /* Helper function that returns true if TAG can have a linkage
17915 name. */
17916
17917 static bool
17918 tag_can_have_linkage_name (enum dwarf_tag tag)
17919 {
17920 switch (tag)
17921 {
17922 /* We include types here because an anonymous C++ type might
17923 have a name for linkage purposes. */
17924 case DW_TAG_class_type:
17925 case DW_TAG_structure_type:
17926 case DW_TAG_union_type:
17927 case DW_TAG_variable:
17928 case DW_TAG_subprogram:
17929 return true;
17930
17931 default:
17932 return false;
17933 }
17934 }
17935
17936 cutu_reader *
17937 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
17938 dwarf2_per_objfile *per_objfile,
17939 sect_offset sect_off, bool is_dwz,
17940 bool for_scanning)
17941 {
17942 /* Lookups for type unit references are always in the CU, and
17943 cross-CU references will crash. */
17944 if (reader->cu->per_cu->is_dwz == is_dwz
17945 && reader->cu->header.offset_in_cu_p (sect_off))
17946 return reader;
17947
17948 dwarf2_per_cu_data *per_cu
17949 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
17950 per_objfile->per_bfd);
17951
17952 /* When scanning, we only want to visit a given CU a single time.
17953 Doing this check here avoids self-imports as well. */
17954 if (for_scanning)
17955 {
17956 bool nope = false;
17957 if (!per_cu->scanned.compare_exchange_strong (nope, true))
17958 return nullptr;
17959 }
17960 if (per_cu == m_per_cu)
17961 return reader;
17962
17963 cutu_reader *result = m_index_storage->get_reader (per_cu);
17964 if (result == nullptr)
17965 {
17966 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
17967 m_index_storage->get_abbrev_cache ());
17968
17969 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
17970 language_minimal);
17971 std::unique_ptr<cutu_reader> copy
17972 (new cutu_reader (std::move (new_reader)));
17973 result = m_index_storage->preserve (std::move (copy));
17974 }
17975
17976 if (result->dummy_p || !result->comp_unit_die->has_children)
17977 return nullptr;
17978
17979 if (for_scanning)
17980 check_bounds (result);
17981
17982 return result;
17983 }
17984
17985 const gdb_byte *
17986 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
17987 cutu_reader *reader,
17988 const gdb_byte *watermark_ptr,
17989 const gdb_byte *info_ptr,
17990 const abbrev_info *abbrev,
17991 const char **name,
17992 const char **linkage_name,
17993 cooked_index_flag *flags,
17994 sect_offset *sibling_offset,
17995 const cooked_index_entry **parent_entry,
17996 CORE_ADDR *maybe_defer,
17997 bool for_specification)
17998 {
17999 bool origin_is_dwz = false;
18000 bool is_declaration = false;
18001 sect_offset origin_offset {};
18002
18003 gdb::optional<CORE_ADDR> low_pc;
18004 gdb::optional<CORE_ADDR> high_pc;
18005 bool high_pc_relative = false;
18006
18007 for (int i = 0; i < abbrev->num_attrs; ++i)
18008 {
18009 attribute attr;
18010 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18011 if (attr.requires_reprocessing_p ())
18012 read_attribute_reprocess (reader, &attr, abbrev->tag);
18013
18014 /* Store the data if it is of an attribute we want to keep in a
18015 partial symbol table. */
18016 switch (attr.name)
18017 {
18018 case DW_AT_name:
18019 switch (abbrev->tag)
18020 {
18021 case DW_TAG_compile_unit:
18022 case DW_TAG_partial_unit:
18023 case DW_TAG_type_unit:
18024 /* Compilation units have a DW_AT_name that is a filename, not
18025 a source language identifier. */
18026 break;
18027
18028 default:
18029 if (*name == nullptr)
18030 *name = attr.as_string ();
18031 break;
18032 }
18033 break;
18034
18035 case DW_AT_linkage_name:
18036 case DW_AT_MIPS_linkage_name:
18037 /* Note that both forms of linkage name might appear. We
18038 assume they will be the same, and we only store the last
18039 one we see. */
18040 if (*linkage_name == nullptr)
18041 *linkage_name = attr.as_string ();
18042 break;
18043
18044 case DW_AT_main_subprogram:
18045 if (attr.as_boolean ())
18046 *flags |= IS_MAIN;
18047 break;
18048
18049 case DW_AT_declaration:
18050 is_declaration = attr.as_boolean ();
18051 break;
18052
18053 case DW_AT_sibling:
18054 if (sibling_offset != nullptr)
18055 *sibling_offset = attr.get_ref_die_offset ();
18056 break;
18057
18058 case DW_AT_specification:
18059 case DW_AT_abstract_origin:
18060 case DW_AT_extension:
18061 origin_offset = attr.get_ref_die_offset ();
18062 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
18063 break;
18064
18065 case DW_AT_external:
18066 if (attr.as_boolean ())
18067 *flags &= ~IS_STATIC;
18068 break;
18069
18070 case DW_AT_enum_class:
18071 if (attr.as_boolean ())
18072 *flags |= IS_ENUM_CLASS;
18073 break;
18074
18075 case DW_AT_low_pc:
18076 low_pc = attr.as_address ();
18077 break;
18078
18079 case DW_AT_high_pc:
18080 high_pc = attr.as_address ();
18081 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
18082 high_pc_relative = true;
18083 break;
18084
18085 case DW_AT_location:
18086 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
18087 {
18088 struct dwarf_block *locdesc = attr.as_block ();
18089 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
18090 if (addr != 0
18091 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
18092 {
18093 low_pc = addr;
18094 /* For variables, we don't want to try decoding the
18095 type just to find the size -- for gdb's purposes
18096 we only need the address of a variable. */
18097 high_pc = addr + 1;
18098 high_pc_relative = false;
18099 }
18100 }
18101 break;
18102
18103 case DW_AT_ranges:
18104 if (!scanning_per_cu->addresses_seen)
18105 {
18106 /* Offset in the .debug_ranges or .debug_rnglist section
18107 (depending on DWARF version). */
18108 ULONGEST ranges_offset = attr.as_unsigned ();
18109
18110 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18111 want to add this value. */
18112 ranges_offset += reader->cu->gnu_ranges_base;
18113
18114 CORE_ADDR lowpc, highpc;
18115 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
18116 m_index_storage->get_addrmap (),
18117 scanning_per_cu, abbrev->tag);
18118 }
18119 break;
18120 }
18121 }
18122
18123 /* We don't want to examine declarations, but if we found a
18124 declaration when handling DW_AT_specification or the like, then
18125 that is ok. Similarly, we allow an external variable without a
18126 location; those are resolved via minimal symbols. */
18127 if (is_declaration && !for_specification
18128 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0)
18129 && !((abbrev->tag == DW_TAG_class_type
18130 || abbrev->tag == DW_TAG_structure_type
18131 || abbrev->tag == DW_TAG_union_type)
18132 && abbrev->has_children))
18133 {
18134 *linkage_name = nullptr;
18135 *name = nullptr;
18136 }
18137 else if ((*name == nullptr
18138 || (*linkage_name == nullptr
18139 && tag_can_have_linkage_name (abbrev->tag))
18140 || (*parent_entry == nullptr && m_language != language_c))
18141 && origin_offset != sect_offset (0))
18142 {
18143 cutu_reader *new_reader
18144 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
18145 origin_is_dwz, false);
18146 if (new_reader != nullptr)
18147 {
18148 const gdb_byte *new_info_ptr = (new_reader->buffer
18149 + to_underlying (origin_offset));
18150
18151 if (new_reader->cu == reader->cu
18152 && new_info_ptr > watermark_ptr
18153 && maybe_defer != nullptr
18154 && *parent_entry == nullptr)
18155 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
18156 else if (*parent_entry == nullptr)
18157 {
18158 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
18159 *parent_entry
18160 = (cooked_index_entry *) m_die_range_map.find (lookup);
18161 }
18162
18163 unsigned int bytes_read;
18164 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
18165 new_info_ptr,
18166 &bytes_read);
18167 new_info_ptr += bytes_read;
18168 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
18169 new_abbrev, name, linkage_name, flags, nullptr,
18170 parent_entry, maybe_defer, true);
18171 }
18172 }
18173
18174 if (!for_specification)
18175 {
18176 if (m_language == language_ada
18177 && *linkage_name == nullptr)
18178 *linkage_name = *name;
18179
18180 if (!scanning_per_cu->addresses_seen
18181 && low_pc.has_value ()
18182 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
18183 || *low_pc != 0)
18184 && high_pc.has_value ())
18185 {
18186 if (high_pc_relative)
18187 high_pc = *high_pc + *low_pc;
18188
18189 if (*high_pc > *low_pc)
18190 {
18191 struct objfile *objfile = reader->cu->per_objfile->objfile;
18192 CORE_ADDR baseaddr = objfile->text_section_offset ();
18193 struct gdbarch *gdbarch = objfile->arch ();
18194 CORE_ADDR lo
18195 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
18196 - baseaddr);
18197 CORE_ADDR hi
18198 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
18199 - baseaddr);
18200 m_index_storage->get_addrmap ()->set_empty (lo, hi - 1,
18201 scanning_per_cu);
18202 }
18203 }
18204
18205 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
18206 *flags &= ~IS_STATIC;
18207
18208 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
18209 *name = "(anonymous namespace)";
18210
18211 if (m_language == language_cplus
18212 && (abbrev->tag == DW_TAG_class_type
18213 || abbrev->tag == DW_TAG_interface_type
18214 || abbrev->tag == DW_TAG_structure_type
18215 || abbrev->tag == DW_TAG_union_type
18216 || abbrev->tag == DW_TAG_enumeration_type
18217 || abbrev->tag == DW_TAG_enumerator))
18218 *flags &= ~IS_STATIC;
18219 }
18220
18221 return info_ptr;
18222 }
18223
18224 const gdb_byte *
18225 cooked_indexer::index_imported_unit (cutu_reader *reader,
18226 const gdb_byte *info_ptr,
18227 const abbrev_info *abbrev)
18228 {
18229 sect_offset sect_off {};
18230 bool is_dwz = false;
18231
18232 for (int i = 0; i < abbrev->num_attrs; ++i)
18233 {
18234 /* Note that we never need to reprocess attributes here. */
18235 attribute attr;
18236 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18237
18238 if (attr.name == DW_AT_import)
18239 {
18240 sect_off = attr.get_ref_die_offset ();
18241 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18242 || reader->cu->per_cu->is_dwz);
18243 }
18244 }
18245
18246 /* Did not find DW_AT_import. */
18247 if (sect_off == sect_offset (0))
18248 return info_ptr;
18249
18250 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
18251 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
18252 is_dwz, true);
18253 if (new_reader != nullptr)
18254 {
18255 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
18256
18257 reader->cu->add_dependence (new_reader->cu->per_cu);
18258 }
18259
18260 return info_ptr;
18261 }
18262
18263 const gdb_byte *
18264 cooked_indexer::recurse (cutu_reader *reader,
18265 const gdb_byte *info_ptr,
18266 const cooked_index_entry *parent_entry,
18267 bool fully)
18268 {
18269 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18270
18271 if (parent_entry != nullptr)
18272 {
18273 CORE_ADDR start = form_addr (parent_entry->die_offset,
18274 reader->cu->per_cu->is_dwz);
18275 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
18276 reader->cu->per_cu->is_dwz);
18277 m_die_range_map.set_empty (start, end, (void *) parent_entry);
18278 }
18279
18280 return info_ptr;
18281 }
18282
18283 const gdb_byte *
18284 cooked_indexer::index_dies (cutu_reader *reader,
18285 const gdb_byte *info_ptr,
18286 const cooked_index_entry *parent_entry,
18287 bool fully)
18288 {
18289 const gdb_byte *end_ptr = (reader->buffer
18290 + to_underlying (reader->cu->header.sect_off)
18291 + reader->cu->header.get_length ());
18292
18293 while (info_ptr < end_ptr)
18294 {
18295 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
18296 unsigned int bytes_read;
18297 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18298 &bytes_read);
18299 info_ptr += bytes_read;
18300 if (abbrev == nullptr)
18301 break;
18302
18303 if (abbrev->tag == DW_TAG_imported_unit)
18304 {
18305 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
18306 continue;
18307 }
18308
18309 if (!abbrev->interesting)
18310 {
18311 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
18312 if (fully && abbrev->has_children)
18313 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18314 continue;
18315 }
18316
18317 const char *name = nullptr;
18318 const char *linkage_name = nullptr;
18319 CORE_ADDR defer = 0;
18320 cooked_index_flag flags = IS_STATIC;
18321 sect_offset sibling {};
18322 const cooked_index_entry *this_parent_entry = parent_entry;
18323 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
18324 info_ptr, abbrev, &name, &linkage_name,
18325 &flags, &sibling, &this_parent_entry,
18326 &defer, false);
18327
18328 if (abbrev->tag == DW_TAG_namespace
18329 && m_language == language_cplus
18330 && strcmp (name, "::") == 0)
18331 {
18332 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18333 generated bogus DW_TAG_namespace DIEs with a name of "::"
18334 for the global namespace. Work around this problem
18335 here. */
18336 name = nullptr;
18337 }
18338
18339 const cooked_index_entry *this_entry = nullptr;
18340 if (name != nullptr)
18341 {
18342 if (defer != 0)
18343 m_deferred_entries.push_back ({
18344 this_die, name, defer, abbrev->tag, flags
18345 });
18346 else
18347 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
18348 name, this_parent_entry,
18349 m_per_cu);
18350 }
18351
18352 if (linkage_name != nullptr)
18353 {
18354 /* We only want this to be "main" if it has a linkage name
18355 but not an ordinary name. */
18356 if (name != nullptr)
18357 flags = flags & ~IS_MAIN;
18358 /* Set the IS_LINKAGE on for everything except when functions
18359 have linkage name present but name is absent. */
18360 if (name != nullptr
18361 || (abbrev->tag != DW_TAG_subprogram
18362 && abbrev->tag != DW_TAG_inlined_subroutine
18363 && abbrev->tag != DW_TAG_entry_point))
18364 flags = flags | IS_LINKAGE;
18365 m_index_storage->add (this_die, abbrev->tag, flags,
18366 linkage_name, nullptr, m_per_cu);
18367 }
18368
18369 if (abbrev->has_children)
18370 {
18371 switch (abbrev->tag)
18372 {
18373 case DW_TAG_class_type:
18374 case DW_TAG_interface_type:
18375 case DW_TAG_structure_type:
18376 case DW_TAG_union_type:
18377 if (m_language != language_c && this_entry != nullptr)
18378 {
18379 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18380 continue;
18381 }
18382 break;
18383
18384 case DW_TAG_enumeration_type:
18385 /* We need to recurse even for an anonymous enumeration.
18386 Which scope we record as the parent scope depends on
18387 whether we're reading an "enum class". If so, we use
18388 the enum itself as the parent, yielding names like
18389 "enum_class::enumerator"; otherwise we inject the
18390 names into our own parent scope. */
18391 info_ptr = recurse (reader, info_ptr,
18392 ((flags & IS_ENUM_CLASS) == 0)
18393 ? parent_entry
18394 : this_entry,
18395 fully);
18396 continue;
18397
18398 case DW_TAG_module:
18399 if (this_entry == nullptr)
18400 break;
18401 /* FALLTHROUGH */
18402 case DW_TAG_namespace:
18403 /* We don't check THIS_ENTRY for a namespace, to handle
18404 the ancient G++ workaround pointed out above. */
18405 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18406 continue;
18407
18408 case DW_TAG_subprogram:
18409 if ((m_language == language_fortran
18410 || m_language == language_ada)
18411 && this_entry != nullptr)
18412 {
18413 info_ptr = recurse (reader, info_ptr, this_entry, true);
18414 continue;
18415 }
18416 break;
18417 }
18418
18419 if (sibling != sect_offset (0))
18420 {
18421 const gdb_byte *sibling_ptr
18422 = reader->buffer + to_underlying (sibling);
18423
18424 if (sibling_ptr < info_ptr)
18425 complaint (_("DW_AT_sibling points backwards"));
18426 else if (sibling_ptr > reader->buffer_end)
18427 reader->die_section->overflow_complaint ();
18428 else
18429 info_ptr = sibling_ptr;
18430 }
18431 else
18432 info_ptr = skip_children (reader, info_ptr);
18433 }
18434 }
18435
18436 return info_ptr;
18437 }
18438
18439 void
18440 cooked_indexer::make_index (cutu_reader *reader)
18441 {
18442 check_bounds (reader);
18443 find_file_and_directory (reader->comp_unit_die, reader->cu);
18444 if (!reader->comp_unit_die->has_children)
18445 return;
18446 index_dies (reader, reader->info_ptr, nullptr, false);
18447
18448 for (const auto &entry : m_deferred_entries)
18449 {
18450 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
18451 cooked_index_entry *parent
18452 = (cooked_index_entry *) m_die_range_map.find (key);
18453 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
18454 entry.name, parent, m_per_cu);
18455 }
18456 }
18457
18458 /* An implementation of quick_symbol_functions for the cooked DWARF
18459 index. */
18460
18461 struct cooked_index_functions : public dwarf2_base_index_functions
18462 {
18463 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
18464 CORE_ADDR adjusted_pc) override;
18465
18466 struct compunit_symtab *find_compunit_symtab_by_address
18467 (struct objfile *objfile, CORE_ADDR address) override;
18468
18469 void dump (struct objfile *objfile) override
18470 {
18471 gdb_printf ("Cooked index in use\n");
18472 }
18473
18474 void expand_matching_symbols
18475 (struct objfile *,
18476 const lookup_name_info &lookup_name,
18477 domain_enum domain,
18478 int global,
18479 symbol_compare_ftype *ordered_compare) override;
18480
18481 bool expand_symtabs_matching
18482 (struct objfile *objfile,
18483 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18484 const lookup_name_info *lookup_name,
18485 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18486 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18487 block_search_flags search_flags,
18488 domain_enum domain,
18489 enum search_domain kind) override;
18490
18491 bool can_lazily_read_symbols () override
18492 {
18493 return true;
18494 }
18495
18496 void read_partial_symbols (struct objfile *objfile) override
18497 {
18498 if (dwarf2_has_info (objfile, nullptr))
18499 dwarf2_build_psymtabs (objfile);
18500 }
18501 };
18502
18503 dwarf2_per_cu_data *
18504 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
18505 CORE_ADDR adjusted_pc)
18506 {
18507 cooked_index_vector *table
18508 = (static_cast<cooked_index_vector *>
18509 (per_bfd->index_table.get ()));
18510 if (table == nullptr)
18511 return nullptr;
18512 return table->lookup (adjusted_pc);
18513 }
18514
18515 struct compunit_symtab *
18516 cooked_index_functions::find_compunit_symtab_by_address
18517 (struct objfile *objfile, CORE_ADDR address)
18518 {
18519 if (objfile->sect_index_data == -1)
18520 return nullptr;
18521
18522 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18523 if (per_objfile->per_bfd->index_table == nullptr)
18524 return nullptr;
18525
18526 CORE_ADDR baseaddr = objfile->data_section_offset ();
18527 cooked_index_vector *table
18528 = (static_cast<cooked_index_vector *>
18529 (per_objfile->per_bfd->index_table.get ()));
18530 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
18531 if (per_cu == nullptr)
18532 return nullptr;
18533
18534 return dw2_instantiate_symtab (per_cu, per_objfile, false);
18535 }
18536
18537 void
18538 cooked_index_functions::expand_matching_symbols
18539 (struct objfile *objfile,
18540 const lookup_name_info &lookup_name,
18541 domain_enum domain,
18542 int global,
18543 symbol_compare_ftype *ordered_compare)
18544 {
18545 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18546 if (per_objfile->per_bfd->index_table == nullptr)
18547 return;
18548 const block_search_flags search_flags = (global
18549 ? SEARCH_GLOBAL_BLOCK
18550 : SEARCH_STATIC_BLOCK);
18551 const language_defn *lang = language_def (language_ada);
18552 symbol_name_matcher_ftype *name_match
18553 = lang->get_symbol_name_matcher (lookup_name);
18554
18555 cooked_index_vector *table
18556 = (static_cast<cooked_index_vector *>
18557 (per_objfile->per_bfd->index_table.get ()));
18558 for (const cooked_index_entry *entry : table->all_entries ())
18559 {
18560 if (entry->parent_entry != nullptr)
18561 continue;
18562
18563 if (!entry->matches (search_flags)
18564 || !entry->matches (domain))
18565 continue;
18566
18567 if (name_match (entry->canonical, lookup_name, nullptr))
18568 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
18569 }
18570 }
18571
18572 bool
18573 cooked_index_functions::expand_symtabs_matching
18574 (struct objfile *objfile,
18575 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18576 const lookup_name_info *lookup_name,
18577 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18578 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18579 block_search_flags search_flags,
18580 domain_enum domain,
18581 enum search_domain kind)
18582 {
18583 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18584 if (per_objfile->per_bfd->index_table == nullptr)
18585 return true;
18586
18587 cooked_index_vector *table
18588 = (static_cast<cooked_index_vector *>
18589 (per_objfile->per_bfd->index_table.get ()));
18590 table->wait ();
18591
18592 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
18593
18594 /* This invariant is documented in quick-functions.h. */
18595 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
18596 if (lookup_name == nullptr)
18597 {
18598 for (dwarf2_per_cu_data *per_cu
18599 : all_comp_units_range (per_objfile->per_bfd))
18600 {
18601 QUIT;
18602
18603 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
18604 file_matcher,
18605 expansion_notify))
18606 return false;
18607 }
18608 return true;
18609 }
18610
18611 lookup_name_info lookup_name_without_params
18612 = lookup_name->make_ignore_params ();
18613 bool completing = lookup_name->completion_mode ();
18614
18615 /* Unique styles of language splitting. */
18616 static const enum language unique_styles[] =
18617 {
18618 /* No splitting is also a style. */
18619 language_c,
18620 /* This includes Rust. */
18621 language_cplus,
18622 /* This includes Go. */
18623 language_d,
18624 language_ada
18625 };
18626
18627 for (enum language lang : unique_styles)
18628 {
18629 std::vector<gdb::string_view> name_vec
18630 = lookup_name_without_params.split_name (lang);
18631
18632 for (const cooked_index_entry *entry : table->find (name_vec.back (),
18633 completing))
18634 {
18635 /* No need to consider symbols from expanded CUs. */
18636 if (per_objfile->symtab_set_p (entry->per_cu))
18637 continue;
18638
18639 /* If file-matching was done, we don't need to consider
18640 symbols from unmarked CUs. */
18641 if (file_matcher != nullptr && !entry->per_cu->mark)
18642 continue;
18643
18644 /* See if the symbol matches the type filter. */
18645 if (!entry->matches (search_flags)
18646 || !entry->matches (domain)
18647 || !entry->matches (kind))
18648 continue;
18649
18650 /* We've found the base name of the symbol; now walk its
18651 parentage chain, ensuring that each component
18652 matches. */
18653 bool found = true;
18654
18655 const cooked_index_entry *parent = entry->parent_entry;
18656 for (int i = name_vec.size () - 1; i > 0; --i)
18657 {
18658 /* If we ran out of entries, or if this segment doesn't
18659 match, this did not match. */
18660 if (parent == nullptr
18661 || strncmp (parent->name, name_vec[i - 1].data (),
18662 name_vec[i - 1].length ()) != 0)
18663 {
18664 found = false;
18665 break;
18666 }
18667
18668 parent = parent->parent_entry;
18669 }
18670
18671 if (!found)
18672 continue;
18673
18674 /* Might have been looking for "a::b" and found
18675 "x::a::b". */
18676 if (symbol_matcher == nullptr)
18677 {
18678 symbol_name_match_type match_type
18679 = lookup_name_without_params.match_type ();
18680 if ((match_type == symbol_name_match_type::FULL
18681 || (lang != language_ada
18682 && match_type == symbol_name_match_type::EXPRESSION))
18683 && parent != nullptr)
18684 continue;
18685 }
18686 else
18687 {
18688 auto_obstack temp_storage;
18689 const char *full_name = entry->full_name (&temp_storage);
18690 if (!symbol_matcher (full_name))
18691 continue;
18692 }
18693
18694 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
18695 file_matcher,
18696 expansion_notify))
18697 return false;
18698 }
18699 }
18700
18701 return true;
18702 }
18703
18704 /* Return a new cooked_index_functions object. */
18705
18706 static quick_symbol_functions_up
18707 make_cooked_index_funcs ()
18708 {
18709 return quick_symbol_functions_up (new cooked_index_functions);
18710 }
18711
18712 quick_symbol_functions_up
18713 cooked_index_vector::make_quick_functions () const
18714 {
18715 return make_cooked_index_funcs ();
18716 }
18717
18718 \f
18719
18720 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18721 contents from the given SECTION in the HEADER.
18722
18723 HEADER_OFFSET is the offset of the header in the section. */
18724 static void
18725 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
18726 struct dwarf2_section_info *section,
18727 sect_offset header_offset)
18728 {
18729 unsigned int bytes_read;
18730 bfd *abfd = section->get_bfd_owner ();
18731 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
18732
18733 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18734 info_ptr += bytes_read;
18735
18736 header->version = read_2_bytes (abfd, info_ptr);
18737 info_ptr += 2;
18738
18739 header->addr_size = read_1_byte (abfd, info_ptr);
18740 info_ptr += 1;
18741
18742 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18743 info_ptr += 1;
18744
18745 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18746 }
18747
18748 /* Return the DW_AT_loclists_base value for the CU. */
18749 static ULONGEST
18750 lookup_loclist_base (struct dwarf2_cu *cu)
18751 {
18752 /* For the .dwo unit, the loclist_base points to the first offset following
18753 the header. The header consists of the following entities-
18754 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18755 bit format)
18756 2. version (2 bytes)
18757 3. address size (1 byte)
18758 4. segment selector size (1 byte)
18759 5. offset entry count (4 bytes)
18760 These sizes are derived as per the DWARFv5 standard. */
18761 if (cu->dwo_unit != nullptr)
18762 {
18763 if (cu->header.initial_length_size == 4)
18764 return LOCLIST_HEADER_SIZE32;
18765 return LOCLIST_HEADER_SIZE64;
18766 }
18767 return cu->loclist_base;
18768 }
18769
18770 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18771 array of offsets in the .debug_loclists section. */
18772
18773 static sect_offset
18774 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18775 {
18776 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18777 struct objfile *objfile = per_objfile->objfile;
18778 bfd *abfd = objfile->obfd;
18779 ULONGEST loclist_header_size =
18780 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
18781 : LOCLIST_HEADER_SIZE64);
18782 ULONGEST loclist_base = lookup_loclist_base (cu);
18783
18784 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18785 ULONGEST start_offset =
18786 loclist_base + loclist_index * cu->header.offset_size;
18787
18788 /* Get loclists section. */
18789 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18790
18791 /* Read the loclists section content. */
18792 section->read (objfile);
18793 if (section->buffer == NULL)
18794 error (_("DW_FORM_loclistx used without .debug_loclists "
18795 "section [in module %s]"), objfile_name (objfile));
18796
18797 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18798 so if loclist_base is smaller than the header size, we have a problem. */
18799 if (loclist_base < loclist_header_size)
18800 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18801 objfile_name (objfile));
18802
18803 /* Read the header of the loclists contribution. */
18804 struct loclists_rnglists_header header;
18805 read_loclists_rnglists_header (&header, section,
18806 (sect_offset) (loclist_base - loclist_header_size));
18807
18808 /* Verify the loclist index is valid. */
18809 if (loclist_index >= header.offset_entry_count)
18810 error (_("DW_FORM_loclistx pointing outside of "
18811 ".debug_loclists offset array [in module %s]"),
18812 objfile_name (objfile));
18813
18814 /* Validate that reading won't go beyond the end of the section. */
18815 if (start_offset + cu->header.offset_size > section->size)
18816 error (_("Reading DW_FORM_loclistx index beyond end of"
18817 ".debug_loclists section [in module %s]"),
18818 objfile_name (objfile));
18819
18820 const gdb_byte *info_ptr = section->buffer + start_offset;
18821
18822 if (cu->header.offset_size == 4)
18823 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
18824 else
18825 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
18826 }
18827
18828 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18829 array of offsets in the .debug_rnglists section. */
18830
18831 static sect_offset
18832 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
18833 dwarf_tag tag)
18834 {
18835 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18836 struct objfile *objfile = dwarf2_per_objfile->objfile;
18837 bfd *abfd = objfile->obfd;
18838 ULONGEST rnglist_header_size =
18839 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
18840 : RNGLIST_HEADER_SIZE64);
18841
18842 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18843 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18844 doesn't apply. */
18845 ULONGEST rnglist_base =
18846 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
18847
18848 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18849 ULONGEST start_offset =
18850 rnglist_base + rnglist_index * cu->header.offset_size;
18851
18852 /* Get rnglists section. */
18853 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
18854
18855 /* Read the rnglists section content. */
18856 section->read (objfile);
18857 if (section->buffer == nullptr)
18858 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18859 "[in module %s]"),
18860 objfile_name (objfile));
18861
18862 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18863 so if rnglist_base is smaller than the header size, we have a problem. */
18864 if (rnglist_base < rnglist_header_size)
18865 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18866 objfile_name (objfile));
18867
18868 /* Read the header of the rnglists contribution. */
18869 struct loclists_rnglists_header header;
18870 read_loclists_rnglists_header (&header, section,
18871 (sect_offset) (rnglist_base - rnglist_header_size));
18872
18873 /* Verify the rnglist index is valid. */
18874 if (rnglist_index >= header.offset_entry_count)
18875 error (_("DW_FORM_rnglistx index pointing outside of "
18876 ".debug_rnglists offset array [in module %s]"),
18877 objfile_name (objfile));
18878
18879 /* Validate that reading won't go beyond the end of the section. */
18880 if (start_offset + cu->header.offset_size > section->size)
18881 error (_("Reading DW_FORM_rnglistx index beyond end of"
18882 ".debug_rnglists section [in module %s]"),
18883 objfile_name (objfile));
18884
18885 const gdb_byte *info_ptr = section->buffer + start_offset;
18886
18887 if (cu->header.offset_size == 4)
18888 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
18889 else
18890 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
18891 }
18892
18893 /* Process the attributes that had to be skipped in the first round. These
18894 attributes are the ones that need str_offsets_base or addr_base attributes.
18895 They could not have been processed in the first round, because at the time
18896 the values of str_offsets_base or addr_base may not have been known. */
18897 static void
18898 read_attribute_reprocess (const struct die_reader_specs *reader,
18899 struct attribute *attr, dwarf_tag tag)
18900 {
18901 struct dwarf2_cu *cu = reader->cu;
18902 switch (attr->form)
18903 {
18904 case DW_FORM_addrx:
18905 case DW_FORM_GNU_addr_index:
18906 attr->set_address (read_addr_index (cu,
18907 attr->as_unsigned_reprocess ()));
18908 break;
18909 case DW_FORM_loclistx:
18910 {
18911 sect_offset loclists_sect_off
18912 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
18913
18914 attr->set_unsigned (to_underlying (loclists_sect_off));
18915 }
18916 break;
18917 case DW_FORM_rnglistx:
18918 {
18919 sect_offset rnglists_sect_off
18920 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
18921
18922 attr->set_unsigned (to_underlying (rnglists_sect_off));
18923 }
18924 break;
18925 case DW_FORM_strx:
18926 case DW_FORM_strx1:
18927 case DW_FORM_strx2:
18928 case DW_FORM_strx3:
18929 case DW_FORM_strx4:
18930 case DW_FORM_GNU_str_index:
18931 {
18932 unsigned int str_index = attr->as_unsigned_reprocess ();
18933 gdb_assert (!attr->canonical_string_p ());
18934 if (reader->dwo_file != NULL)
18935 attr->set_string_noncanonical (read_dwo_str_index (reader,
18936 str_index));
18937 else
18938 attr->set_string_noncanonical (read_stub_str_index (cu,
18939 str_index));
18940 break;
18941 }
18942 default:
18943 gdb_assert_not_reached ("Unexpected DWARF form.");
18944 }
18945 }
18946
18947 /* Read an attribute value described by an attribute form. */
18948
18949 static const gdb_byte *
18950 read_attribute_value (const struct die_reader_specs *reader,
18951 struct attribute *attr, unsigned form,
18952 LONGEST implicit_const, const gdb_byte *info_ptr)
18953 {
18954 struct dwarf2_cu *cu = reader->cu;
18955 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18956 struct objfile *objfile = per_objfile->objfile;
18957 bfd *abfd = reader->abfd;
18958 struct comp_unit_head *cu_header = &cu->header;
18959 unsigned int bytes_read;
18960 struct dwarf_block *blk;
18961
18962 attr->form = (enum dwarf_form) form;
18963 switch (form)
18964 {
18965 case DW_FORM_ref_addr:
18966 if (cu_header->version == 2)
18967 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
18968 &bytes_read));
18969 else
18970 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
18971 &bytes_read));
18972 info_ptr += bytes_read;
18973 break;
18974 case DW_FORM_GNU_ref_alt:
18975 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
18976 &bytes_read));
18977 info_ptr += bytes_read;
18978 break;
18979 case DW_FORM_addr:
18980 {
18981 struct gdbarch *gdbarch = objfile->arch ();
18982 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
18983 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
18984 attr->set_address (addr);
18985 info_ptr += bytes_read;
18986 }
18987 break;
18988 case DW_FORM_block2:
18989 blk = dwarf_alloc_block (cu);
18990 blk->size = read_2_bytes (abfd, info_ptr);
18991 info_ptr += 2;
18992 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18993 info_ptr += blk->size;
18994 attr->set_block (blk);
18995 break;
18996 case DW_FORM_block4:
18997 blk = dwarf_alloc_block (cu);
18998 blk->size = read_4_bytes (abfd, info_ptr);
18999 info_ptr += 4;
19000 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19001 info_ptr += blk->size;
19002 attr->set_block (blk);
19003 break;
19004 case DW_FORM_data2:
19005 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19006 info_ptr += 2;
19007 break;
19008 case DW_FORM_data4:
19009 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19010 info_ptr += 4;
19011 break;
19012 case DW_FORM_data8:
19013 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19014 info_ptr += 8;
19015 break;
19016 case DW_FORM_data16:
19017 blk = dwarf_alloc_block (cu);
19018 blk->size = 16;
19019 blk->data = read_n_bytes (abfd, info_ptr, 16);
19020 info_ptr += 16;
19021 attr->set_block (blk);
19022 break;
19023 case DW_FORM_sec_offset:
19024 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19025 &bytes_read));
19026 info_ptr += bytes_read;
19027 break;
19028 case DW_FORM_loclistx:
19029 {
19030 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19031 &bytes_read));
19032 info_ptr += bytes_read;
19033 }
19034 break;
19035 case DW_FORM_string:
19036 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19037 &bytes_read));
19038 info_ptr += bytes_read;
19039 break;
19040 case DW_FORM_strp:
19041 if (!cu->per_cu->is_dwz)
19042 {
19043 attr->set_string_noncanonical
19044 (read_indirect_string (per_objfile,
19045 abfd, info_ptr, cu_header,
19046 &bytes_read));
19047 info_ptr += bytes_read;
19048 break;
19049 }
19050 /* FALLTHROUGH */
19051 case DW_FORM_line_strp:
19052 if (!cu->per_cu->is_dwz)
19053 {
19054 attr->set_string_noncanonical
19055 (per_objfile->read_line_string (info_ptr, cu_header,
19056 &bytes_read));
19057 info_ptr += bytes_read;
19058 break;
19059 }
19060 /* FALLTHROUGH */
19061 case DW_FORM_GNU_strp_alt:
19062 {
19063 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19064 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19065 &bytes_read);
19066
19067 attr->set_string_noncanonical
19068 (dwz->read_string (objfile, str_offset));
19069 info_ptr += bytes_read;
19070 }
19071 break;
19072 case DW_FORM_exprloc:
19073 case DW_FORM_block:
19074 blk = dwarf_alloc_block (cu);
19075 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19076 info_ptr += bytes_read;
19077 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19078 info_ptr += blk->size;
19079 attr->set_block (blk);
19080 break;
19081 case DW_FORM_block1:
19082 blk = dwarf_alloc_block (cu);
19083 blk->size = read_1_byte (abfd, info_ptr);
19084 info_ptr += 1;
19085 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19086 info_ptr += blk->size;
19087 attr->set_block (blk);
19088 break;
19089 case DW_FORM_data1:
19090 case DW_FORM_flag:
19091 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19092 info_ptr += 1;
19093 break;
19094 case DW_FORM_flag_present:
19095 attr->set_unsigned (1);
19096 break;
19097 case DW_FORM_sdata:
19098 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19099 info_ptr += bytes_read;
19100 break;
19101 case DW_FORM_rnglistx:
19102 {
19103 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19104 &bytes_read));
19105 info_ptr += bytes_read;
19106 }
19107 break;
19108 case DW_FORM_udata:
19109 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19110 info_ptr += bytes_read;
19111 break;
19112 case DW_FORM_ref1:
19113 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19114 + read_1_byte (abfd, info_ptr)));
19115 info_ptr += 1;
19116 break;
19117 case DW_FORM_ref2:
19118 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19119 + read_2_bytes (abfd, info_ptr)));
19120 info_ptr += 2;
19121 break;
19122 case DW_FORM_ref4:
19123 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19124 + read_4_bytes (abfd, info_ptr)));
19125 info_ptr += 4;
19126 break;
19127 case DW_FORM_ref8:
19128 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19129 + read_8_bytes (abfd, info_ptr)));
19130 info_ptr += 8;
19131 break;
19132 case DW_FORM_ref_sig8:
19133 attr->set_signature (read_8_bytes (abfd, info_ptr));
19134 info_ptr += 8;
19135 break;
19136 case DW_FORM_ref_udata:
19137 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19138 + read_unsigned_leb128 (abfd, info_ptr,
19139 &bytes_read)));
19140 info_ptr += bytes_read;
19141 break;
19142 case DW_FORM_indirect:
19143 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19144 info_ptr += bytes_read;
19145 if (form == DW_FORM_implicit_const)
19146 {
19147 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19148 info_ptr += bytes_read;
19149 }
19150 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19151 info_ptr);
19152 break;
19153 case DW_FORM_implicit_const:
19154 attr->set_signed (implicit_const);
19155 break;
19156 case DW_FORM_addrx:
19157 case DW_FORM_GNU_addr_index:
19158 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19159 &bytes_read));
19160 info_ptr += bytes_read;
19161 break;
19162 case DW_FORM_strx:
19163 case DW_FORM_strx1:
19164 case DW_FORM_strx2:
19165 case DW_FORM_strx3:
19166 case DW_FORM_strx4:
19167 case DW_FORM_GNU_str_index:
19168 {
19169 ULONGEST str_index;
19170 if (form == DW_FORM_strx1)
19171 {
19172 str_index = read_1_byte (abfd, info_ptr);
19173 info_ptr += 1;
19174 }
19175 else if (form == DW_FORM_strx2)
19176 {
19177 str_index = read_2_bytes (abfd, info_ptr);
19178 info_ptr += 2;
19179 }
19180 else if (form == DW_FORM_strx3)
19181 {
19182 str_index = read_3_bytes (abfd, info_ptr);
19183 info_ptr += 3;
19184 }
19185 else if (form == DW_FORM_strx4)
19186 {
19187 str_index = read_4_bytes (abfd, info_ptr);
19188 info_ptr += 4;
19189 }
19190 else
19191 {
19192 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19193 info_ptr += bytes_read;
19194 }
19195 attr->set_unsigned_reprocess (str_index);
19196 }
19197 break;
19198 default:
19199 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19200 dwarf_form_name (form),
19201 bfd_get_filename (abfd));
19202 }
19203
19204 /* Super hack. */
19205 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19206 attr->form = DW_FORM_GNU_ref_alt;
19207
19208 /* We have seen instances where the compiler tried to emit a byte
19209 size attribute of -1 which ended up being encoded as an unsigned
19210 0xffffffff. Although 0xffffffff is technically a valid size value,
19211 an object of this size seems pretty unlikely so we can relatively
19212 safely treat these cases as if the size attribute was invalid and
19213 treat them as zero by default. */
19214 if (attr->name == DW_AT_byte_size
19215 && form == DW_FORM_data4
19216 && attr->as_unsigned () >= 0xffffffff)
19217 {
19218 complaint
19219 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19220 hex_string (attr->as_unsigned ()));
19221 attr->set_unsigned (0);
19222 }
19223
19224 return info_ptr;
19225 }
19226
19227 /* Read an attribute described by an abbreviated attribute. */
19228
19229 static const gdb_byte *
19230 read_attribute (const struct die_reader_specs *reader,
19231 struct attribute *attr, const struct attr_abbrev *abbrev,
19232 const gdb_byte *info_ptr)
19233 {
19234 attr->name = abbrev->name;
19235 attr->string_is_canonical = 0;
19236 attr->requires_reprocessing = 0;
19237 return read_attribute_value (reader, attr, abbrev->form,
19238 abbrev->implicit_const, info_ptr);
19239 }
19240
19241 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19242
19243 static const char *
19244 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19245 LONGEST str_offset)
19246 {
19247 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19248 str_offset, "DW_FORM_strp");
19249 }
19250
19251 /* Return pointer to string at .debug_str offset as read from BUF.
19252 BUF is assumed to be in a compilation unit described by CU_HEADER.
19253 Return *BYTES_READ_PTR count of bytes read from BUF. */
19254
19255 static const char *
19256 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19257 const gdb_byte *buf,
19258 const struct comp_unit_head *cu_header,
19259 unsigned int *bytes_read_ptr)
19260 {
19261 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19262
19263 return read_indirect_string_at_offset (per_objfile, str_offset);
19264 }
19265
19266 /* See read.h. */
19267
19268 const char *
19269 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19270 unsigned int offset_size)
19271 {
19272 bfd *abfd = objfile->obfd;
19273 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
19274
19275 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19276 }
19277
19278 /* See read.h. */
19279
19280 const char *
19281 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19282 const struct comp_unit_head *cu_header,
19283 unsigned int *bytes_read_ptr)
19284 {
19285 bfd *abfd = objfile->obfd;
19286 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19287
19288 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19289 }
19290
19291 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19292 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19293 ADDR_SIZE is the size of addresses from the CU header. */
19294
19295 static CORE_ADDR
19296 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19297 gdb::optional<ULONGEST> addr_base, int addr_size)
19298 {
19299 struct objfile *objfile = per_objfile->objfile;
19300 bfd *abfd = objfile->obfd;
19301 const gdb_byte *info_ptr;
19302 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19303
19304 per_objfile->per_bfd->addr.read (objfile);
19305 if (per_objfile->per_bfd->addr.buffer == NULL)
19306 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19307 objfile_name (objfile));
19308 if (addr_base_or_zero + addr_index * addr_size
19309 >= per_objfile->per_bfd->addr.size)
19310 error (_("DW_FORM_addr_index pointing outside of "
19311 ".debug_addr section [in module %s]"),
19312 objfile_name (objfile));
19313 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19314 + addr_index * addr_size);
19315 if (addr_size == 4)
19316 return bfd_get_32 (abfd, info_ptr);
19317 else
19318 return bfd_get_64 (abfd, info_ptr);
19319 }
19320
19321 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19322
19323 static CORE_ADDR
19324 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19325 {
19326 return read_addr_index_1 (cu->per_objfile, addr_index,
19327 cu->addr_base, cu->header.addr_size);
19328 }
19329
19330 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19331
19332 static CORE_ADDR
19333 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19334 unsigned int *bytes_read)
19335 {
19336 bfd *abfd = cu->per_objfile->objfile->obfd;
19337 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19338
19339 return read_addr_index (cu, addr_index);
19340 }
19341
19342 /* See read.h. */
19343
19344 CORE_ADDR
19345 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19346 dwarf2_per_objfile *per_objfile,
19347 unsigned int addr_index)
19348 {
19349 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19350 gdb::optional<ULONGEST> addr_base;
19351 int addr_size;
19352
19353 /* We need addr_base and addr_size.
19354 If we don't have PER_CU->cu, we have to get it.
19355 Nasty, but the alternative is storing the needed info in PER_CU,
19356 which at this point doesn't seem justified: it's not clear how frequently
19357 it would get used and it would increase the size of every PER_CU.
19358 Entry points like dwarf2_per_cu_addr_size do a similar thing
19359 so we're not in uncharted territory here.
19360 Alas we need to be a bit more complicated as addr_base is contained
19361 in the DIE.
19362
19363 We don't need to read the entire CU(/TU).
19364 We just need the header and top level die.
19365
19366 IWBN to use the aging mechanism to let us lazily later discard the CU.
19367 For now we skip this optimization. */
19368
19369 if (cu != NULL)
19370 {
19371 addr_base = cu->addr_base;
19372 addr_size = cu->header.addr_size;
19373 }
19374 else
19375 {
19376 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19377 addr_base = reader.cu->addr_base;
19378 addr_size = reader.cu->header.addr_size;
19379 }
19380
19381 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19382 }
19383
19384 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19385 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19386 DWO file. */
19387
19388 static const char *
19389 read_str_index (struct dwarf2_cu *cu,
19390 struct dwarf2_section_info *str_section,
19391 struct dwarf2_section_info *str_offsets_section,
19392 ULONGEST str_offsets_base, ULONGEST str_index,
19393 unsigned offset_size)
19394 {
19395 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19396 struct objfile *objfile = per_objfile->objfile;
19397 const char *objf_name = objfile_name (objfile);
19398 bfd *abfd = objfile->obfd;
19399 const gdb_byte *info_ptr;
19400 ULONGEST str_offset;
19401 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19402
19403 str_section->read (objfile);
19404 str_offsets_section->read (objfile);
19405 if (str_section->buffer == NULL)
19406 error (_("%s used without %s section"
19407 " in CU at offset %s [in module %s]"),
19408 form_name, str_section->get_name (),
19409 sect_offset_str (cu->header.sect_off), objf_name);
19410 if (str_offsets_section->buffer == NULL)
19411 error (_("%s used without %s section"
19412 " in CU at offset %s [in module %s]"),
19413 form_name, str_section->get_name (),
19414 sect_offset_str (cu->header.sect_off), objf_name);
19415 info_ptr = (str_offsets_section->buffer
19416 + str_offsets_base
19417 + str_index * offset_size);
19418 if (offset_size == 4)
19419 str_offset = bfd_get_32 (abfd, info_ptr);
19420 else
19421 str_offset = bfd_get_64 (abfd, info_ptr);
19422 if (str_offset >= str_section->size)
19423 error (_("Offset from %s pointing outside of"
19424 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19425 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19426 return (const char *) (str_section->buffer + str_offset);
19427 }
19428
19429 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19430
19431 static const char *
19432 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19433 {
19434 unsigned offset_size;
19435 ULONGEST str_offsets_base;
19436 if (reader->cu->header.version >= 5)
19437 {
19438 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
19439 so assume the .debug_str_offsets section is DWARF5 as well, and
19440 parse the header. FIXME: Parse the header only once. */
19441 unsigned int bytes_read = 0;
19442 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
19443 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
19444
19445 /* Header: Initial length. */
19446 read_initial_length (abfd, p + bytes_read, &bytes_read);
19447
19448 /* Determine offset_size based on the .debug_str_offsets header. */
19449 const bool dwarf5_is_dwarf64 = bytes_read != 4;
19450 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
19451
19452 /* Header: Version. */
19453 unsigned version = read_2_bytes (abfd, p + bytes_read);
19454 bytes_read += 2;
19455
19456 if (version <= 4)
19457 {
19458 /* We'd like one warning here about ignoring the section, but
19459 because we parse the header more than once (see FIXME above)
19460 we'd have many warnings, so use a complaint instead, which at
19461 least has a limit. */
19462 complaint (_("Section .debug_str_offsets in %s has unsupported"
19463 " version %d, use empty string."),
19464 reader->dwo_file->dwo_name, version);
19465 return "";
19466 }
19467
19468 /* Header: Padding. */
19469 bytes_read += 2;
19470
19471 str_offsets_base = bytes_read;
19472 }
19473 else
19474 {
19475 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
19476 section, assume the .debug_str_offsets section is pre-DWARF5 as
19477 well, which doesn't have a header. */
19478 str_offsets_base = 0;
19479
19480 /* Determine offset_size based on the .debug_info header. */
19481 offset_size = reader->cu->header.offset_size;
19482 }
19483
19484 return read_str_index (reader->cu,
19485 &reader->dwo_file->sections.str,
19486 &reader->dwo_file->sections.str_offsets,
19487 str_offsets_base, str_index, offset_size);
19488 }
19489
19490 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19491
19492 static const char *
19493 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19494 {
19495 struct objfile *objfile = cu->per_objfile->objfile;
19496 const char *objf_name = objfile_name (objfile);
19497 static const char form_name[] = "DW_FORM_GNU_str_index";
19498 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19499
19500 if (!cu->str_offsets_base.has_value ())
19501 error (_("%s used in Fission stub without %s"
19502 " in CU at offset 0x%lx [in module %s]"),
19503 form_name, str_offsets_attr_name,
19504 (long) cu->header.offset_size, objf_name);
19505
19506 return read_str_index (cu,
19507 &cu->per_objfile->per_bfd->str,
19508 &cu->per_objfile->per_bfd->str_offsets,
19509 *cu->str_offsets_base, str_index,
19510 cu->header.offset_size);
19511 }
19512
19513 /* Return the length of an LEB128 number in BUF. */
19514
19515 static int
19516 leb128_size (const gdb_byte *buf)
19517 {
19518 const gdb_byte *begin = buf;
19519 gdb_byte byte;
19520
19521 while (1)
19522 {
19523 byte = *buf++;
19524 if ((byte & 128) == 0)
19525 return buf - begin;
19526 }
19527 }
19528
19529 static enum language
19530 dwarf_lang_to_enum_language (unsigned int lang)
19531 {
19532 enum language language;
19533
19534 switch (lang)
19535 {
19536 case DW_LANG_C89:
19537 case DW_LANG_C99:
19538 case DW_LANG_C11:
19539 case DW_LANG_C:
19540 case DW_LANG_UPC:
19541 language = language_c;
19542 break;
19543 case DW_LANG_Java:
19544 case DW_LANG_C_plus_plus:
19545 case DW_LANG_C_plus_plus_11:
19546 case DW_LANG_C_plus_plus_14:
19547 language = language_cplus;
19548 break;
19549 case DW_LANG_D:
19550 language = language_d;
19551 break;
19552 case DW_LANG_Fortran77:
19553 case DW_LANG_Fortran90:
19554 case DW_LANG_Fortran95:
19555 case DW_LANG_Fortran03:
19556 case DW_LANG_Fortran08:
19557 language = language_fortran;
19558 break;
19559 case DW_LANG_Go:
19560 language = language_go;
19561 break;
19562 case DW_LANG_Mips_Assembler:
19563 language = language_asm;
19564 break;
19565 case DW_LANG_Ada83:
19566 case DW_LANG_Ada95:
19567 language = language_ada;
19568 break;
19569 case DW_LANG_Modula2:
19570 language = language_m2;
19571 break;
19572 case DW_LANG_Pascal83:
19573 language = language_pascal;
19574 break;
19575 case DW_LANG_ObjC:
19576 language = language_objc;
19577 break;
19578 case DW_LANG_Rust:
19579 case DW_LANG_Rust_old:
19580 language = language_rust;
19581 break;
19582 case DW_LANG_OpenCL:
19583 language = language_opencl;
19584 break;
19585 case DW_LANG_Cobol74:
19586 case DW_LANG_Cobol85:
19587 default:
19588 language = language_minimal;
19589 break;
19590 }
19591
19592 return language;
19593 }
19594
19595 /* Return the named attribute or NULL if not there. */
19596
19597 static struct attribute *
19598 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19599 {
19600 for (;;)
19601 {
19602 unsigned int i;
19603 struct attribute *spec = NULL;
19604
19605 for (i = 0; i < die->num_attrs; ++i)
19606 {
19607 if (die->attrs[i].name == name)
19608 return &die->attrs[i];
19609 if (die->attrs[i].name == DW_AT_specification
19610 || die->attrs[i].name == DW_AT_abstract_origin)
19611 spec = &die->attrs[i];
19612 }
19613
19614 if (!spec)
19615 break;
19616
19617 die = follow_die_ref (die, spec, &cu);
19618 }
19619
19620 return NULL;
19621 }
19622
19623 /* Return the string associated with a string-typed attribute, or NULL if it
19624 is either not found or is of an incorrect type. */
19625
19626 static const char *
19627 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19628 {
19629 struct attribute *attr;
19630 const char *str = NULL;
19631
19632 attr = dwarf2_attr (die, name, cu);
19633
19634 if (attr != NULL)
19635 {
19636 str = attr->as_string ();
19637 if (str == nullptr)
19638 complaint (_("string type expected for attribute %s for "
19639 "DIE at %s in module %s"),
19640 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19641 objfile_name (cu->per_objfile->objfile));
19642 }
19643
19644 return str;
19645 }
19646
19647 /* Return the dwo name or NULL if not present. If present, it is in either
19648 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19649 static const char *
19650 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19651 {
19652 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19653 if (dwo_name == nullptr)
19654 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19655 return dwo_name;
19656 }
19657
19658 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19659 and holds a non-zero value. This function should only be used for
19660 DW_FORM_flag or DW_FORM_flag_present attributes. */
19661
19662 static int
19663 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19664 {
19665 struct attribute *attr = dwarf2_attr (die, name, cu);
19666
19667 return attr != nullptr && attr->as_boolean ();
19668 }
19669
19670 static int
19671 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19672 {
19673 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19674 which value is non-zero. However, we have to be careful with
19675 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19676 (via dwarf2_flag_true_p) follows this attribute. So we may
19677 end up accidently finding a declaration attribute that belongs
19678 to a different DIE referenced by the specification attribute,
19679 even though the given DIE does not have a declaration attribute. */
19680 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19681 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19682 }
19683
19684 /* Return the die giving the specification for DIE, if there is
19685 one. *SPEC_CU is the CU containing DIE on input, and the CU
19686 containing the return value on output. If there is no
19687 specification, but there is an abstract origin, that is
19688 returned. */
19689
19690 static struct die_info *
19691 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19692 {
19693 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19694 *spec_cu);
19695
19696 if (spec_attr == NULL)
19697 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19698
19699 if (spec_attr == NULL)
19700 return NULL;
19701 else
19702 return follow_die_ref (die, spec_attr, spec_cu);
19703 }
19704
19705 /* A convenience function to find the proper .debug_line section for a CU. */
19706
19707 static struct dwarf2_section_info *
19708 get_debug_line_section (struct dwarf2_cu *cu)
19709 {
19710 struct dwarf2_section_info *section;
19711 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19712
19713 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19714 DWO file. */
19715 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19716 section = &cu->dwo_unit->dwo_file->sections.line;
19717 else if (cu->per_cu->is_dwz)
19718 {
19719 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19720
19721 section = &dwz->line;
19722 }
19723 else
19724 section = &per_objfile->per_bfd->line;
19725
19726 return section;
19727 }
19728
19729 /* Read the statement program header starting at OFFSET in
19730 .debug_line, or .debug_line.dwo. Return a pointer
19731 to a struct line_header, allocated using xmalloc.
19732 Returns NULL if there is a problem reading the header, e.g., if it
19733 has a version we don't understand.
19734
19735 NOTE: the strings in the include directory and file name tables of
19736 the returned object point into the dwarf line section buffer,
19737 and must not be freed. */
19738
19739 static line_header_up
19740 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19741 {
19742 struct dwarf2_section_info *section;
19743 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19744
19745 section = get_debug_line_section (cu);
19746 section->read (per_objfile->objfile);
19747 if (section->buffer == NULL)
19748 {
19749 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19750 complaint (_("missing .debug_line.dwo section"));
19751 else
19752 complaint (_("missing .debug_line section"));
19753 return 0;
19754 }
19755
19756 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19757 per_objfile, section, &cu->header);
19758 }
19759
19760 /* Subroutine of dwarf_decode_lines to simplify it.
19761 Return the file name for the given file_entry.
19762 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19763 If space for the result is malloc'd, *NAME_HOLDER will be set.
19764 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19765 equivalent to CU_INFO. */
19766
19767 static const char *
19768 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
19769 const file_and_directory &cu_info,
19770 std::string &name_holder)
19771 {
19772 const char *include_name = fe.name;
19773 const char *include_name_to_compare = include_name;
19774
19775 const char *dir_name = fe.include_dir (lh);
19776
19777 std::string hold_compare;
19778 if (!IS_ABSOLUTE_PATH (include_name)
19779 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
19780 {
19781 /* Avoid creating a duplicate name for CU_INFO.
19782 We do this by comparing INCLUDE_NAME and CU_INFO.
19783 Before we do the comparison, however, we need to account
19784 for DIR_NAME and COMP_DIR.
19785 First prepend dir_name (if non-NULL). If we still don't
19786 have an absolute path prepend comp_dir (if non-NULL).
19787 However, the directory we record in the include-file's
19788 psymtab does not contain COMP_DIR (to match the
19789 corresponding symtab(s)).
19790
19791 Example:
19792
19793 bash$ cd /tmp
19794 bash$ gcc -g ./hello.c
19795 include_name = "hello.c"
19796 dir_name = "."
19797 DW_AT_comp_dir = comp_dir = "/tmp"
19798 DW_AT_name = "./hello.c"
19799
19800 */
19801
19802 if (dir_name != NULL)
19803 {
19804 name_holder = path_join (dir_name, include_name);
19805 include_name = name_holder.c_str ();
19806 include_name_to_compare = include_name;
19807 }
19808 if (!IS_ABSOLUTE_PATH (include_name)
19809 && cu_info.get_comp_dir () != nullptr)
19810 {
19811 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
19812 include_name_to_compare = hold_compare.c_str ();
19813 }
19814 }
19815
19816 std::string copied_name;
19817 const char *cu_filename = cu_info.get_name ();
19818 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
19819 {
19820 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
19821 cu_filename = copied_name.c_str ();
19822 }
19823
19824 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
19825 return nullptr;
19826 return include_name;
19827 }
19828
19829 /* State machine to track the state of the line number program. */
19830
19831 class lnp_state_machine
19832 {
19833 public:
19834 /* Initialize a machine state for the start of a line number
19835 program. */
19836 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
19837
19838 file_entry *current_file ()
19839 {
19840 /* lh->file_names is 0-based, but the file name numbers in the
19841 statement program are 1-based. */
19842 return m_line_header->file_name_at (m_file);
19843 }
19844
19845 /* Record the line in the state machine. END_SEQUENCE is true if
19846 we're processing the end of a sequence. */
19847 void record_line (bool end_sequence);
19848
19849 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19850 nop-out rest of the lines in this sequence. */
19851 void check_line_address (struct dwarf2_cu *cu,
19852 const gdb_byte *line_ptr,
19853 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19854
19855 void handle_set_discriminator (unsigned int discriminator)
19856 {
19857 m_discriminator = discriminator;
19858 m_line_has_non_zero_discriminator |= discriminator != 0;
19859 }
19860
19861 /* Handle DW_LNE_set_address. */
19862 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19863 {
19864 m_op_index = 0;
19865 address += baseaddr;
19866 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19867 }
19868
19869 /* Handle DW_LNS_advance_pc. */
19870 void handle_advance_pc (CORE_ADDR adjust);
19871
19872 /* Handle a special opcode. */
19873 void handle_special_opcode (unsigned char op_code);
19874
19875 /* Handle DW_LNS_advance_line. */
19876 void handle_advance_line (int line_delta)
19877 {
19878 advance_line (line_delta);
19879 }
19880
19881 /* Handle DW_LNS_set_file. */
19882 void handle_set_file (file_name_index file);
19883
19884 /* Handle DW_LNS_negate_stmt. */
19885 void handle_negate_stmt ()
19886 {
19887 m_flags ^= LEF_IS_STMT;
19888 }
19889
19890 /* Handle DW_LNS_const_add_pc. */
19891 void handle_const_add_pc ();
19892
19893 /* Handle DW_LNS_fixed_advance_pc. */
19894 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19895 {
19896 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19897 m_op_index = 0;
19898 }
19899
19900 /* Handle DW_LNS_copy. */
19901 void handle_copy ()
19902 {
19903 record_line (false);
19904 m_discriminator = 0;
19905 m_flags &= ~LEF_PROLOGUE_END;
19906 }
19907
19908 /* Handle DW_LNE_end_sequence. */
19909 void handle_end_sequence ()
19910 {
19911 m_currently_recording_lines = true;
19912 }
19913
19914 /* Handle DW_LNS_set_prologue_end. */
19915 void handle_set_prologue_end ()
19916 {
19917 m_flags |= LEF_PROLOGUE_END;
19918 }
19919
19920 private:
19921 /* Advance the line by LINE_DELTA. */
19922 void advance_line (int line_delta)
19923 {
19924 m_line += line_delta;
19925
19926 if (line_delta != 0)
19927 m_line_has_non_zero_discriminator = m_discriminator != 0;
19928 }
19929
19930 struct dwarf2_cu *m_cu;
19931
19932 gdbarch *m_gdbarch;
19933
19934 /* The line number header. */
19935 line_header *m_line_header;
19936
19937 /* These are part of the standard DWARF line number state machine,
19938 and initialized according to the DWARF spec. */
19939
19940 unsigned char m_op_index = 0;
19941 /* The line table index of the current file. */
19942 file_name_index m_file = 1;
19943 unsigned int m_line = 1;
19944
19945 /* These are initialized in the constructor. */
19946
19947 CORE_ADDR m_address;
19948 linetable_entry_flags m_flags;
19949 unsigned int m_discriminator;
19950
19951 /* Additional bits of state we need to track. */
19952
19953 /* The last file that we called dwarf2_start_subfile for.
19954 This is only used for TLLs. */
19955 unsigned int m_last_file = 0;
19956 /* The last file a line number was recorded for. */
19957 struct subfile *m_last_subfile = NULL;
19958
19959 /* The address of the last line entry. */
19960 CORE_ADDR m_last_address;
19961
19962 /* Set to true when a previous line at the same address (using
19963 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
19964 when a line entry at a new address (m_address different to
19965 m_last_address) is processed. */
19966 bool m_stmt_at_address = false;
19967
19968 /* When true, record the lines we decode. */
19969 bool m_currently_recording_lines = false;
19970
19971 /* The last line number that was recorded, used to coalesce
19972 consecutive entries for the same line. This can happen, for
19973 example, when discriminators are present. PR 17276. */
19974 unsigned int m_last_line = 0;
19975 bool m_line_has_non_zero_discriminator = false;
19976 };
19977
19978 void
19979 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19980 {
19981 CORE_ADDR addr_adj = (((m_op_index + adjust)
19982 / m_line_header->maximum_ops_per_instruction)
19983 * m_line_header->minimum_instruction_length);
19984 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19985 m_op_index = ((m_op_index + adjust)
19986 % m_line_header->maximum_ops_per_instruction);
19987 }
19988
19989 void
19990 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19991 {
19992 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19993 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19994 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19995 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19996 / m_line_header->maximum_ops_per_instruction)
19997 * m_line_header->minimum_instruction_length);
19998 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19999 m_op_index = ((m_op_index + adj_opcode_d)
20000 % m_line_header->maximum_ops_per_instruction);
20001
20002 int line_delta = m_line_header->line_base + adj_opcode_r;
20003 advance_line (line_delta);
20004 record_line (false);
20005 m_discriminator = 0;
20006 m_flags &= ~LEF_PROLOGUE_END;
20007 }
20008
20009 void
20010 lnp_state_machine::handle_set_file (file_name_index file)
20011 {
20012 m_file = file;
20013
20014 const file_entry *fe = current_file ();
20015 if (fe == NULL)
20016 dwarf2_debug_line_missing_file_complaint ();
20017 else
20018 {
20019 const char *dir = fe->include_dir (m_line_header);
20020
20021 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20022 m_line_has_non_zero_discriminator = m_discriminator != 0;
20023 dwarf2_start_subfile (m_cu, fe->name, dir);
20024 }
20025 }
20026
20027 void
20028 lnp_state_machine::handle_const_add_pc ()
20029 {
20030 CORE_ADDR adjust
20031 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20032
20033 CORE_ADDR addr_adj
20034 = (((m_op_index + adjust)
20035 / m_line_header->maximum_ops_per_instruction)
20036 * m_line_header->minimum_instruction_length);
20037
20038 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20039 m_op_index = ((m_op_index + adjust)
20040 % m_line_header->maximum_ops_per_instruction);
20041 }
20042
20043 /* Return non-zero if we should add LINE to the line number table.
20044 LINE is the line to add, LAST_LINE is the last line that was added,
20045 LAST_SUBFILE is the subfile for LAST_LINE.
20046 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20047 had a non-zero discriminator.
20048
20049 We have to be careful in the presence of discriminators.
20050 E.g., for this line:
20051
20052 for (i = 0; i < 100000; i++);
20053
20054 clang can emit four line number entries for that one line,
20055 each with a different discriminator.
20056 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20057
20058 However, we want gdb to coalesce all four entries into one.
20059 Otherwise the user could stepi into the middle of the line and
20060 gdb would get confused about whether the pc really was in the
20061 middle of the line.
20062
20063 Things are further complicated by the fact that two consecutive
20064 line number entries for the same line is a heuristic used by gcc
20065 to denote the end of the prologue. So we can't just discard duplicate
20066 entries, we have to be selective about it. The heuristic we use is
20067 that we only collapse consecutive entries for the same line if at least
20068 one of those entries has a non-zero discriminator. PR 17276.
20069
20070 Note: Addresses in the line number state machine can never go backwards
20071 within one sequence, thus this coalescing is ok. */
20072
20073 static int
20074 dwarf_record_line_p (struct dwarf2_cu *cu,
20075 unsigned int line, unsigned int last_line,
20076 int line_has_non_zero_discriminator,
20077 struct subfile *last_subfile)
20078 {
20079 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20080 return 1;
20081 if (line != last_line)
20082 return 1;
20083 /* Same line for the same file that we've seen already.
20084 As a last check, for pr 17276, only record the line if the line
20085 has never had a non-zero discriminator. */
20086 if (!line_has_non_zero_discriminator)
20087 return 1;
20088 return 0;
20089 }
20090
20091 /* Use the CU's builder to record line number LINE beginning at
20092 address ADDRESS in the line table of subfile SUBFILE. */
20093
20094 static void
20095 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20096 unsigned int line, CORE_ADDR address,
20097 linetable_entry_flags flags,
20098 struct dwarf2_cu *cu)
20099 {
20100 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20101
20102 if (dwarf_line_debug)
20103 {
20104 gdb_printf (gdb_stdlog,
20105 "Recording line %u, file %s, address %s\n",
20106 line, lbasename (subfile->name.c_str ()),
20107 paddress (gdbarch, address));
20108 }
20109
20110 if (cu != nullptr)
20111 cu->get_builder ()->record_line (subfile, line, addr, flags);
20112 }
20113
20114 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20115 Mark the end of a set of line number records.
20116 The arguments are the same as for dwarf_record_line_1.
20117 If SUBFILE is NULL the request is ignored. */
20118
20119 static void
20120 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20121 CORE_ADDR address, struct dwarf2_cu *cu)
20122 {
20123 if (subfile == NULL)
20124 return;
20125
20126 if (dwarf_line_debug)
20127 {
20128 gdb_printf (gdb_stdlog,
20129 "Finishing current line, file %s, address %s\n",
20130 lbasename (subfile->name.c_str ()),
20131 paddress (gdbarch, address));
20132 }
20133
20134 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
20135 }
20136
20137 void
20138 lnp_state_machine::record_line (bool end_sequence)
20139 {
20140 if (dwarf_line_debug)
20141 {
20142 gdb_printf (gdb_stdlog,
20143 "Processing actual line %u: file %u,"
20144 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20145 m_line, m_file,
20146 paddress (m_gdbarch, m_address),
20147 (m_flags & LEF_IS_STMT) != 0,
20148 (m_flags & LEF_PROLOGUE_END) != 0,
20149 m_discriminator,
20150 (end_sequence ? "\t(end sequence)" : ""));
20151 }
20152
20153 file_entry *fe = current_file ();
20154
20155 if (fe == NULL)
20156 dwarf2_debug_line_missing_file_complaint ();
20157 /* For now we ignore lines not starting on an instruction boundary.
20158 But not when processing end_sequence for compatibility with the
20159 previous version of the code. */
20160 else if (m_op_index == 0 || end_sequence)
20161 {
20162 /* When we switch files we insert an end maker in the first file,
20163 switch to the second file and add a new line entry. The
20164 problem is that the end marker inserted in the first file will
20165 discard any previous line entries at the same address. If the
20166 line entries in the first file are marked as is-stmt, while
20167 the new line in the second file is non-stmt, then this means
20168 the end marker will discard is-stmt lines so we can have a
20169 non-stmt line. This means that there are less addresses at
20170 which the user can insert a breakpoint.
20171
20172 To improve this we track the last address in m_last_address,
20173 and whether we have seen an is-stmt at this address. Then
20174 when switching files, if we have seen a stmt at the current
20175 address, and we are switching to create a non-stmt line, then
20176 discard the new line. */
20177 bool file_changed
20178 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20179 bool ignore_this_line
20180 = ((file_changed && !end_sequence && m_last_address == m_address
20181 && ((m_flags & LEF_IS_STMT) == 0)
20182 && m_stmt_at_address)
20183 || (!end_sequence && m_line == 0));
20184
20185 if ((file_changed && !ignore_this_line) || end_sequence)
20186 {
20187 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20188 m_currently_recording_lines ? m_cu : nullptr);
20189 }
20190
20191 if (!end_sequence && !ignore_this_line)
20192 {
20193 linetable_entry_flags lte_flags = m_flags;
20194 if (producer_is_codewarrior (m_cu))
20195 lte_flags |= LEF_IS_STMT;
20196
20197 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20198 m_line_has_non_zero_discriminator,
20199 m_last_subfile))
20200 {
20201 buildsym_compunit *builder = m_cu->get_builder ();
20202 dwarf_record_line_1 (m_gdbarch,
20203 builder->get_current_subfile (),
20204 m_line, m_address, lte_flags,
20205 m_currently_recording_lines ? m_cu : nullptr);
20206 }
20207 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20208 m_last_line = m_line;
20209 }
20210 }
20211
20212 /* Track whether we have seen any IS_STMT true at m_address in case we
20213 have multiple line table entries all at m_address. */
20214 if (m_last_address != m_address)
20215 {
20216 m_stmt_at_address = false;
20217 m_last_address = m_address;
20218 }
20219 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
20220 }
20221
20222 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20223 line_header *lh)
20224 {
20225 m_cu = cu;
20226 m_gdbarch = arch;
20227 m_line_header = lh;
20228
20229 m_currently_recording_lines = true;
20230
20231 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20232 was a line entry for it so that the backend has a chance to adjust it
20233 and also record it in case it needs it. This is currently used by MIPS
20234 code, cf. `mips_adjust_dwarf2_line'. */
20235 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20236 m_flags = 0;
20237 if (lh->default_is_stmt)
20238 m_flags |= LEF_IS_STMT;
20239 m_discriminator = 0;
20240
20241 m_last_address = m_address;
20242 m_stmt_at_address = false;
20243 }
20244
20245 void
20246 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20247 const gdb_byte *line_ptr,
20248 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20249 {
20250 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20251 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20252 located at 0x0. In this case, additionally check that if
20253 ADDRESS < UNRELOCATED_LOWPC. */
20254
20255 if ((address == 0 && address < unrelocated_lowpc)
20256 || address == (CORE_ADDR) -1)
20257 {
20258 /* This line table is for a function which has been
20259 GCd by the linker. Ignore it. PR gdb/12528 */
20260
20261 struct objfile *objfile = cu->per_objfile->objfile;
20262 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20263
20264 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20265 line_offset, objfile_name (objfile));
20266 m_currently_recording_lines = false;
20267 /* Note: m_currently_recording_lines is left as false until we see
20268 DW_LNE_end_sequence. */
20269 }
20270 }
20271
20272 /* Subroutine of dwarf_decode_lines to simplify it.
20273 Process the line number information in LH. */
20274
20275 static void
20276 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20277 CORE_ADDR lowpc)
20278 {
20279 const gdb_byte *line_ptr, *extended_end;
20280 const gdb_byte *line_end;
20281 unsigned int bytes_read, extended_len;
20282 unsigned char op_code, extended_op;
20283 CORE_ADDR baseaddr;
20284 struct objfile *objfile = cu->per_objfile->objfile;
20285 bfd *abfd = objfile->obfd;
20286 struct gdbarch *gdbarch = objfile->arch ();
20287
20288 baseaddr = objfile->text_section_offset ();
20289
20290 line_ptr = lh->statement_program_start;
20291 line_end = lh->statement_program_end;
20292
20293 /* Read the statement sequences until there's nothing left. */
20294 while (line_ptr < line_end)
20295 {
20296 /* The DWARF line number program state machine. Reset the state
20297 machine at the start of each sequence. */
20298 lnp_state_machine state_machine (cu, gdbarch, lh);
20299 bool end_sequence = false;
20300
20301 /* Start a subfile for the current file of the state
20302 machine. */
20303 const file_entry *fe = state_machine.current_file ();
20304
20305 if (fe != NULL)
20306 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20307
20308 /* Decode the table. */
20309 while (line_ptr < line_end && !end_sequence)
20310 {
20311 op_code = read_1_byte (abfd, line_ptr);
20312 line_ptr += 1;
20313
20314 if (op_code >= lh->opcode_base)
20315 {
20316 /* Special opcode. */
20317 state_machine.handle_special_opcode (op_code);
20318 }
20319 else switch (op_code)
20320 {
20321 case DW_LNS_extended_op:
20322 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20323 &bytes_read);
20324 line_ptr += bytes_read;
20325 extended_end = line_ptr + extended_len;
20326 extended_op = read_1_byte (abfd, line_ptr);
20327 line_ptr += 1;
20328 if (DW_LNE_lo_user <= extended_op
20329 && extended_op <= DW_LNE_hi_user)
20330 {
20331 /* Vendor extension, ignore. */
20332 line_ptr = extended_end;
20333 break;
20334 }
20335 switch (extended_op)
20336 {
20337 case DW_LNE_end_sequence:
20338 state_machine.handle_end_sequence ();
20339 end_sequence = true;
20340 break;
20341 case DW_LNE_set_address:
20342 {
20343 CORE_ADDR address
20344 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20345 line_ptr += bytes_read;
20346
20347 state_machine.check_line_address (cu, line_ptr,
20348 lowpc - baseaddr, address);
20349 state_machine.handle_set_address (baseaddr, address);
20350 }
20351 break;
20352 case DW_LNE_define_file:
20353 {
20354 const char *cur_file;
20355 unsigned int mod_time, length;
20356 dir_index dindex;
20357
20358 cur_file = read_direct_string (abfd, line_ptr,
20359 &bytes_read);
20360 line_ptr += bytes_read;
20361 dindex = (dir_index)
20362 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20363 line_ptr += bytes_read;
20364 mod_time =
20365 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20366 line_ptr += bytes_read;
20367 length =
20368 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20369 line_ptr += bytes_read;
20370 lh->add_file_name (cur_file, dindex, mod_time, length);
20371 }
20372 break;
20373 case DW_LNE_set_discriminator:
20374 {
20375 /* The discriminator is not interesting to the
20376 debugger; just ignore it. We still need to
20377 check its value though:
20378 if there are consecutive entries for the same
20379 (non-prologue) line we want to coalesce them.
20380 PR 17276. */
20381 unsigned int discr
20382 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20383 line_ptr += bytes_read;
20384
20385 state_machine.handle_set_discriminator (discr);
20386 }
20387 break;
20388 default:
20389 complaint (_("mangled .debug_line section"));
20390 return;
20391 }
20392 /* Make sure that we parsed the extended op correctly. If e.g.
20393 we expected a different address size than the producer used,
20394 we may have read the wrong number of bytes. */
20395 if (line_ptr != extended_end)
20396 {
20397 complaint (_("mangled .debug_line section"));
20398 return;
20399 }
20400 break;
20401 case DW_LNS_copy:
20402 state_machine.handle_copy ();
20403 break;
20404 case DW_LNS_advance_pc:
20405 {
20406 CORE_ADDR adjust
20407 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20408 line_ptr += bytes_read;
20409
20410 state_machine.handle_advance_pc (adjust);
20411 }
20412 break;
20413 case DW_LNS_advance_line:
20414 {
20415 int line_delta
20416 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20417 line_ptr += bytes_read;
20418
20419 state_machine.handle_advance_line (line_delta);
20420 }
20421 break;
20422 case DW_LNS_set_file:
20423 {
20424 file_name_index file
20425 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20426 &bytes_read);
20427 line_ptr += bytes_read;
20428
20429 state_machine.handle_set_file (file);
20430 }
20431 break;
20432 case DW_LNS_set_column:
20433 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20434 line_ptr += bytes_read;
20435 break;
20436 case DW_LNS_negate_stmt:
20437 state_machine.handle_negate_stmt ();
20438 break;
20439 case DW_LNS_set_basic_block:
20440 break;
20441 /* Add to the address register of the state machine the
20442 address increment value corresponding to special opcode
20443 255. I.e., this value is scaled by the minimum
20444 instruction length since special opcode 255 would have
20445 scaled the increment. */
20446 case DW_LNS_const_add_pc:
20447 state_machine.handle_const_add_pc ();
20448 break;
20449 case DW_LNS_fixed_advance_pc:
20450 {
20451 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20452 line_ptr += 2;
20453
20454 state_machine.handle_fixed_advance_pc (addr_adj);
20455 }
20456 break;
20457 case DW_LNS_set_prologue_end:
20458 state_machine.handle_set_prologue_end ();
20459 break;
20460 default:
20461 {
20462 /* Unknown standard opcode, ignore it. */
20463 int i;
20464
20465 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20466 {
20467 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20468 line_ptr += bytes_read;
20469 }
20470 }
20471 }
20472 }
20473
20474 if (!end_sequence)
20475 dwarf2_debug_line_missing_end_sequence_complaint ();
20476
20477 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20478 in which case we still finish recording the last line). */
20479 state_machine.record_line (true);
20480 }
20481 }
20482
20483 /* Decode the Line Number Program (LNP) for the given line_header
20484 structure and CU. The actual information extracted and the type
20485 of structures created from the LNP depends on the value of PST.
20486
20487 FND holds the CU file name and directory, if known.
20488 It is used for relative paths in the line table.
20489
20490 NOTE: It is important that psymtabs have the same file name (via
20491 strcmp) as the corresponding symtab. Since the directory is not
20492 used in the name of the symtab we don't use it in the name of the
20493 psymtabs we create. E.g. expand_line_sal requires this when
20494 finding psymtabs to expand. A good testcase for this is
20495 mb-inline.exp.
20496
20497 LOWPC is the lowest address in CU (or 0 if not known).
20498
20499 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20500 for its PC<->lines mapping information. Otherwise only the filename
20501 table is read in. */
20502
20503 static void
20504 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
20505 CORE_ADDR lowpc, int decode_mapping)
20506 {
20507 if (decode_mapping)
20508 dwarf_decode_lines_1 (lh, cu, lowpc);
20509
20510 /* Make sure a symtab is created for every file, even files
20511 which contain only variables (i.e. no code with associated
20512 line numbers). */
20513 buildsym_compunit *builder = cu->get_builder ();
20514 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20515
20516 for (auto &fe : lh->file_names ())
20517 {
20518 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20519 if (builder->get_current_subfile ()->symtab == NULL)
20520 {
20521 builder->get_current_subfile ()->symtab
20522 = allocate_symtab (cust,
20523 builder->get_current_subfile ()->name.c_str ());
20524 }
20525 fe.symtab = builder->get_current_subfile ()->symtab;
20526 }
20527 }
20528
20529 /* Start a subfile for DWARF. FILENAME is the name of the file and
20530 DIRNAME the name of the source directory which contains FILENAME
20531 or NULL if not known.
20532 This routine tries to keep line numbers from identical absolute and
20533 relative file names in a common subfile.
20534
20535 Using the `list' example from the GDB testsuite, which resides in
20536 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20537 of /srcdir/list0.c yields the following debugging information for list0.c:
20538
20539 DW_AT_name: /srcdir/list0.c
20540 DW_AT_comp_dir: /compdir
20541 files.files[0].name: list0.h
20542 files.files[0].dir: /srcdir
20543 files.files[1].name: list0.c
20544 files.files[1].dir: /srcdir
20545
20546 The line number information for list0.c has to end up in a single
20547 subfile, so that `break /srcdir/list0.c:1' works as expected.
20548 start_subfile will ensure that this happens provided that we pass the
20549 concatenation of files.files[1].dir and files.files[1].name as the
20550 subfile's name. */
20551
20552 static void
20553 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20554 const char *dirname)
20555 {
20556 std::string copy;
20557
20558 /* In order not to lose the line information directory,
20559 we concatenate it to the filename when it makes sense.
20560 Note that the Dwarf3 standard says (speaking of filenames in line
20561 information): ``The directory index is ignored for file names
20562 that represent full path names''. Thus ignoring dirname in the
20563 `else' branch below isn't an issue. */
20564
20565 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20566 {
20567 copy = path_join (dirname, filename);
20568 filename = copy.c_str ();
20569 }
20570
20571 cu->get_builder ()->start_subfile (filename);
20572 }
20573
20574 static void
20575 var_decode_location (struct attribute *attr, struct symbol *sym,
20576 struct dwarf2_cu *cu)
20577 {
20578 struct objfile *objfile = cu->per_objfile->objfile;
20579 struct comp_unit_head *cu_header = &cu->header;
20580
20581 /* NOTE drow/2003-01-30: There used to be a comment and some special
20582 code here to turn a symbol with DW_AT_external and a
20583 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20584 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20585 with some versions of binutils) where shared libraries could have
20586 relocations against symbols in their debug information - the
20587 minimal symbol would have the right address, but the debug info
20588 would not. It's no longer necessary, because we will explicitly
20589 apply relocations when we read in the debug information now. */
20590
20591 /* A DW_AT_location attribute with no contents indicates that a
20592 variable has been optimized away. */
20593 if (attr->form_is_block () && attr->as_block ()->size == 0)
20594 {
20595 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20596 return;
20597 }
20598
20599 /* Handle one degenerate form of location expression specially, to
20600 preserve GDB's previous behavior when section offsets are
20601 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20602 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20603
20604 if (attr->form_is_block ())
20605 {
20606 struct dwarf_block *block = attr->as_block ();
20607
20608 if ((block->data[0] == DW_OP_addr
20609 && block->size == 1 + cu_header->addr_size)
20610 || ((block->data[0] == DW_OP_GNU_addr_index
20611 || block->data[0] == DW_OP_addrx)
20612 && (block->size
20613 == 1 + leb128_size (&block->data[1]))))
20614 {
20615 unsigned int dummy;
20616
20617 if (block->data[0] == DW_OP_addr)
20618 sym->set_value_address
20619 (cu->header.read_address (objfile->obfd, block->data + 1,
20620 &dummy));
20621 else
20622 sym->set_value_address
20623 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
20624 sym->set_aclass_index (LOC_STATIC);
20625 fixup_symbol_section (sym, objfile);
20626 sym->set_value_address
20627 (sym->value_address ()
20628 + objfile->section_offsets[sym->section_index ()]);
20629 return;
20630 }
20631 }
20632
20633 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20634 expression evaluator, and use LOC_COMPUTED only when necessary
20635 (i.e. when the value of a register or memory location is
20636 referenced, or a thread-local block, etc.). Then again, it might
20637 not be worthwhile. I'm assuming that it isn't unless performance
20638 or memory numbers show me otherwise. */
20639
20640 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20641
20642 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20643 cu->has_loclist = true;
20644 }
20645
20646 /* Given a pointer to a DWARF information entry, figure out if we need
20647 to make a symbol table entry for it, and if so, create a new entry
20648 and return a pointer to it.
20649 If TYPE is NULL, determine symbol type from the die, otherwise
20650 used the passed type.
20651 If SPACE is not NULL, use it to hold the new symbol. If it is
20652 NULL, allocate a new symbol on the objfile's obstack. */
20653
20654 static struct symbol *
20655 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20656 struct symbol *space)
20657 {
20658 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20659 struct objfile *objfile = per_objfile->objfile;
20660 struct gdbarch *gdbarch = objfile->arch ();
20661 struct symbol *sym = NULL;
20662 const char *name;
20663 struct attribute *attr = NULL;
20664 struct attribute *attr2 = NULL;
20665 CORE_ADDR baseaddr;
20666 struct pending **list_to_add = NULL;
20667
20668 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20669
20670 baseaddr = objfile->text_section_offset ();
20671
20672 name = dwarf2_name (die, cu);
20673 if (name == nullptr && (die->tag == DW_TAG_subprogram
20674 || die->tag == DW_TAG_inlined_subroutine
20675 || die->tag == DW_TAG_entry_point))
20676 name = dw2_linkage_name (die, cu);
20677
20678 if (name)
20679 {
20680 int suppress_add = 0;
20681
20682 if (space)
20683 sym = space;
20684 else
20685 sym = new (&objfile->objfile_obstack) symbol;
20686 OBJSTAT (objfile, n_syms++);
20687
20688 /* Cache this symbol's name and the name's demangled form (if any). */
20689 sym->set_language (cu->lang (), &objfile->objfile_obstack);
20690 /* Fortran does not have mangling standard and the mangling does differ
20691 between gfortran, iFort etc. */
20692 const char *physname
20693 = (cu->lang () == language_fortran
20694 ? dwarf2_full_name (name, die, cu)
20695 : dwarf2_physname (name, die, cu));
20696 const char *linkagename = dw2_linkage_name (die, cu);
20697
20698 if (linkagename == nullptr || cu->lang () == language_ada)
20699 sym->set_linkage_name (physname);
20700 else
20701 {
20702 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20703 sym->set_linkage_name (linkagename);
20704 }
20705
20706 /* Handle DW_AT_artificial. */
20707 attr = dwarf2_attr (die, DW_AT_artificial, cu);
20708 if (attr != nullptr)
20709 sym->set_is_artificial (attr->as_boolean ());
20710
20711 /* Default assumptions.
20712 Use the passed type or decode it from the die. */
20713 sym->set_domain (VAR_DOMAIN);
20714 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20715 if (type != NULL)
20716 sym->set_type (type);
20717 else
20718 sym->set_type (die_type (die, cu));
20719 attr = dwarf2_attr (die,
20720 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20721 cu);
20722 if (attr != nullptr)
20723 sym->set_line (attr->constant_value (0));
20724
20725 attr = dwarf2_attr (die,
20726 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20727 cu);
20728 if (attr != nullptr && attr->is_nonnegative ())
20729 {
20730 file_name_index file_index
20731 = (file_name_index) attr->as_nonnegative ();
20732 struct file_entry *fe;
20733
20734 if (cu->line_header != NULL)
20735 fe = cu->line_header->file_name_at (file_index);
20736 else
20737 fe = NULL;
20738
20739 if (fe == NULL)
20740 complaint (_("file index out of range"));
20741 else
20742 sym->set_symtab (fe->symtab);
20743 }
20744
20745 switch (die->tag)
20746 {
20747 case DW_TAG_label:
20748 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20749 if (attr != nullptr)
20750 {
20751 CORE_ADDR addr;
20752
20753 addr = attr->as_address ();
20754 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20755 sym->set_value_address (addr);
20756 sym->set_aclass_index (LOC_LABEL);
20757 }
20758 else
20759 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20760 sym->set_type (objfile_type (objfile)->builtin_core_addr);
20761 sym->set_domain (LABEL_DOMAIN);
20762 add_symbol_to_list (sym, cu->list_in_scope);
20763 break;
20764 case DW_TAG_subprogram:
20765 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20766 finish_block. */
20767 sym->set_aclass_index (LOC_BLOCK);
20768 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20769 if ((attr2 != nullptr && attr2->as_boolean ())
20770 || cu->lang () == language_ada
20771 || cu->lang () == language_fortran)
20772 {
20773 /* Subprograms marked external are stored as a global symbol.
20774 Ada and Fortran subprograms, whether marked external or
20775 not, are always stored as a global symbol, because we want
20776 to be able to access them globally. For instance, we want
20777 to be able to break on a nested subprogram without having
20778 to specify the context. */
20779 list_to_add = cu->get_builder ()->get_global_symbols ();
20780 }
20781 else
20782 {
20783 list_to_add = cu->list_in_scope;
20784 }
20785 break;
20786 case DW_TAG_inlined_subroutine:
20787 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20788 finish_block. */
20789 sym->set_aclass_index (LOC_BLOCK);
20790 sym->set_is_inlined (1);
20791 list_to_add = cu->list_in_scope;
20792 break;
20793 case DW_TAG_template_value_param:
20794 suppress_add = 1;
20795 /* Fall through. */
20796 case DW_TAG_constant:
20797 case DW_TAG_variable:
20798 case DW_TAG_member:
20799 /* Compilation with minimal debug info may result in
20800 variables with missing type entries. Change the
20801 misleading `void' type to something sensible. */
20802 if (sym->type ()->code () == TYPE_CODE_VOID)
20803 sym->set_type (objfile_type (objfile)->builtin_int);
20804
20805 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20806 /* In the case of DW_TAG_member, we should only be called for
20807 static const members. */
20808 if (die->tag == DW_TAG_member)
20809 {
20810 /* dwarf2_add_field uses die_is_declaration,
20811 so we do the same. */
20812 gdb_assert (die_is_declaration (die, cu));
20813 gdb_assert (attr);
20814 }
20815 if (attr != nullptr)
20816 {
20817 dwarf2_const_value (attr, sym, cu);
20818 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20819 if (!suppress_add)
20820 {
20821 if (attr2 != nullptr && attr2->as_boolean ())
20822 list_to_add = cu->get_builder ()->get_global_symbols ();
20823 else
20824 list_to_add = cu->list_in_scope;
20825 }
20826 break;
20827 }
20828 attr = dwarf2_attr (die, DW_AT_location, cu);
20829 if (attr != nullptr)
20830 {
20831 var_decode_location (attr, sym, cu);
20832 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20833
20834 /* Fortran explicitly imports any global symbols to the local
20835 scope by DW_TAG_common_block. */
20836 if (cu->lang () == language_fortran && die->parent
20837 && die->parent->tag == DW_TAG_common_block)
20838 attr2 = NULL;
20839
20840 if (sym->aclass () == LOC_STATIC
20841 && sym->value_address () == 0
20842 && !per_objfile->per_bfd->has_section_at_zero)
20843 {
20844 /* When a static variable is eliminated by the linker,
20845 the corresponding debug information is not stripped
20846 out, but the variable address is set to null;
20847 do not add such variables into symbol table. */
20848 }
20849 else if (attr2 != nullptr && attr2->as_boolean ())
20850 {
20851 if (sym->aclass () == LOC_STATIC
20852 && (objfile->flags & OBJF_MAINLINE) == 0
20853 && per_objfile->per_bfd->can_copy)
20854 {
20855 /* A global static variable might be subject to
20856 copy relocation. We first check for a local
20857 minsym, though, because maybe the symbol was
20858 marked hidden, in which case this would not
20859 apply. */
20860 bound_minimal_symbol found
20861 = (lookup_minimal_symbol_linkage
20862 (sym->linkage_name (), objfile));
20863 if (found.minsym != nullptr)
20864 sym->maybe_copied = 1;
20865 }
20866
20867 /* A variable with DW_AT_external is never static,
20868 but it may be block-scoped. */
20869 list_to_add
20870 = ((cu->list_in_scope
20871 == cu->get_builder ()->get_file_symbols ())
20872 ? cu->get_builder ()->get_global_symbols ()
20873 : cu->list_in_scope);
20874 }
20875 else
20876 list_to_add = cu->list_in_scope;
20877 }
20878 else
20879 {
20880 /* We do not know the address of this symbol.
20881 If it is an external symbol and we have type information
20882 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20883 The address of the variable will then be determined from
20884 the minimal symbol table whenever the variable is
20885 referenced. */
20886 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20887
20888 /* Fortran explicitly imports any global symbols to the local
20889 scope by DW_TAG_common_block. */
20890 if (cu->lang () == language_fortran && die->parent
20891 && die->parent->tag == DW_TAG_common_block)
20892 {
20893 /* SYMBOL_CLASS doesn't matter here because
20894 read_common_block is going to reset it. */
20895 if (!suppress_add)
20896 list_to_add = cu->list_in_scope;
20897 }
20898 else if (attr2 != nullptr && attr2->as_boolean ()
20899 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20900 {
20901 /* A variable with DW_AT_external is never static, but it
20902 may be block-scoped. */
20903 list_to_add
20904 = ((cu->list_in_scope
20905 == cu->get_builder ()->get_file_symbols ())
20906 ? cu->get_builder ()->get_global_symbols ()
20907 : cu->list_in_scope);
20908
20909 sym->set_aclass_index (LOC_UNRESOLVED);
20910 }
20911 else if (!die_is_declaration (die, cu))
20912 {
20913 /* Use the default LOC_OPTIMIZED_OUT class. */
20914 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
20915 if (!suppress_add)
20916 list_to_add = cu->list_in_scope;
20917 }
20918 }
20919 break;
20920 case DW_TAG_formal_parameter:
20921 {
20922 /* If we are inside a function, mark this as an argument. If
20923 not, we might be looking at an argument to an inlined function
20924 when we do not have enough information to show inlined frames;
20925 pretend it's a local variable in that case so that the user can
20926 still see it. */
20927 struct context_stack *curr
20928 = cu->get_builder ()->get_current_context_stack ();
20929 if (curr != nullptr && curr->name != nullptr)
20930 sym->set_is_argument (1);
20931 attr = dwarf2_attr (die, DW_AT_location, cu);
20932 if (attr != nullptr)
20933 {
20934 var_decode_location (attr, sym, cu);
20935 }
20936 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20937 if (attr != nullptr)
20938 {
20939 dwarf2_const_value (attr, sym, cu);
20940 }
20941
20942 list_to_add = cu->list_in_scope;
20943 }
20944 break;
20945 case DW_TAG_unspecified_parameters:
20946 /* From varargs functions; gdb doesn't seem to have any
20947 interest in this information, so just ignore it for now.
20948 (FIXME?) */
20949 break;
20950 case DW_TAG_template_type_param:
20951 suppress_add = 1;
20952 /* Fall through. */
20953 case DW_TAG_class_type:
20954 case DW_TAG_interface_type:
20955 case DW_TAG_structure_type:
20956 case DW_TAG_union_type:
20957 case DW_TAG_set_type:
20958 case DW_TAG_enumeration_type:
20959 case DW_TAG_namelist:
20960 if (die->tag == DW_TAG_namelist)
20961 {
20962 sym->set_aclass_index (LOC_STATIC);
20963 sym->set_domain (VAR_DOMAIN);
20964 }
20965 else
20966 {
20967 sym->set_aclass_index (LOC_TYPEDEF);
20968 sym->set_domain (STRUCT_DOMAIN);
20969 }
20970 {
20971 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20972 really ever be static objects: otherwise, if you try
20973 to, say, break of a class's method and you're in a file
20974 which doesn't mention that class, it won't work unless
20975 the check for all static symbols in lookup_symbol_aux
20976 saves you. See the OtherFileClass tests in
20977 gdb.c++/namespace.exp. */
20978
20979 if (!suppress_add)
20980 {
20981 buildsym_compunit *builder = cu->get_builder ();
20982 list_to_add
20983 = (cu->list_in_scope == builder->get_file_symbols ()
20984 && cu->lang () == language_cplus
20985 ? builder->get_global_symbols ()
20986 : cu->list_in_scope);
20987
20988 /* The semantics of C++ state that "struct foo {
20989 ... }" also defines a typedef for "foo". */
20990 if (cu->lang () == language_cplus
20991 || cu->lang () == language_ada
20992 || cu->lang () == language_d
20993 || cu->lang () == language_rust)
20994 {
20995 /* The symbol's name is already allocated along
20996 with this objfile, so we don't need to
20997 duplicate it for the type. */
20998 if (sym->type ()->name () == 0)
20999 sym->type ()->set_name (sym->search_name ());
21000 }
21001 }
21002 }
21003 break;
21004 case DW_TAG_typedef:
21005 sym->set_aclass_index (LOC_TYPEDEF);
21006 sym->set_domain (VAR_DOMAIN);
21007 list_to_add = cu->list_in_scope;
21008 break;
21009 case DW_TAG_array_type:
21010 case DW_TAG_base_type:
21011 case DW_TAG_subrange_type:
21012 case DW_TAG_generic_subrange:
21013 sym->set_aclass_index (LOC_TYPEDEF);
21014 sym->set_domain (VAR_DOMAIN);
21015 list_to_add = cu->list_in_scope;
21016 break;
21017 case DW_TAG_enumerator:
21018 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21019 if (attr != nullptr)
21020 {
21021 dwarf2_const_value (attr, sym, cu);
21022 }
21023 {
21024 /* NOTE: carlton/2003-11-10: See comment above in the
21025 DW_TAG_class_type, etc. block. */
21026
21027 list_to_add
21028 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21029 && cu->lang () == language_cplus
21030 ? cu->get_builder ()->get_global_symbols ()
21031 : cu->list_in_scope);
21032 }
21033 break;
21034 case DW_TAG_imported_declaration:
21035 case DW_TAG_namespace:
21036 sym->set_aclass_index (LOC_TYPEDEF);
21037 list_to_add = cu->get_builder ()->get_global_symbols ();
21038 break;
21039 case DW_TAG_module:
21040 sym->set_aclass_index (LOC_TYPEDEF);
21041 sym->set_domain (MODULE_DOMAIN);
21042 list_to_add = cu->get_builder ()->get_global_symbols ();
21043 break;
21044 case DW_TAG_common_block:
21045 sym->set_aclass_index (LOC_COMMON_BLOCK);
21046 sym->set_domain (COMMON_BLOCK_DOMAIN);
21047 add_symbol_to_list (sym, cu->list_in_scope);
21048 break;
21049 default:
21050 /* Not a tag we recognize. Hopefully we aren't processing
21051 trash data, but since we must specifically ignore things
21052 we don't recognize, there is nothing else we should do at
21053 this point. */
21054 complaint (_("unsupported tag: '%s'"),
21055 dwarf_tag_name (die->tag));
21056 break;
21057 }
21058
21059 if (suppress_add)
21060 {
21061 sym->hash_next = objfile->template_symbols;
21062 objfile->template_symbols = sym;
21063 list_to_add = NULL;
21064 }
21065
21066 if (list_to_add != NULL)
21067 add_symbol_to_list (sym, list_to_add);
21068
21069 /* For the benefit of old versions of GCC, check for anonymous
21070 namespaces based on the demangled name. */
21071 if (!cu->processing_has_namespace_info
21072 && cu->lang () == language_cplus)
21073 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21074 }
21075 return (sym);
21076 }
21077
21078 /* Given an attr with a DW_FORM_dataN value in host byte order,
21079 zero-extend it as appropriate for the symbol's type. The DWARF
21080 standard (v4) is not entirely clear about the meaning of using
21081 DW_FORM_dataN for a constant with a signed type, where the type is
21082 wider than the data. The conclusion of a discussion on the DWARF
21083 list was that this is unspecified. We choose to always zero-extend
21084 because that is the interpretation long in use by GCC. */
21085
21086 static gdb_byte *
21087 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21088 struct dwarf2_cu *cu, LONGEST *value, int bits)
21089 {
21090 struct objfile *objfile = cu->per_objfile->objfile;
21091 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21092 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21093 LONGEST l = attr->constant_value (0);
21094
21095 if (bits < sizeof (*value) * 8)
21096 {
21097 l &= ((LONGEST) 1 << bits) - 1;
21098 *value = l;
21099 }
21100 else if (bits == sizeof (*value) * 8)
21101 *value = l;
21102 else
21103 {
21104 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21105 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21106 return bytes;
21107 }
21108
21109 return NULL;
21110 }
21111
21112 /* Read a constant value from an attribute. Either set *VALUE, or if
21113 the value does not fit in *VALUE, set *BYTES - either already
21114 allocated on the objfile obstack, or newly allocated on OBSTACK,
21115 or, set *BATON, if we translated the constant to a location
21116 expression. */
21117
21118 static void
21119 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21120 const char *name, struct obstack *obstack,
21121 struct dwarf2_cu *cu,
21122 LONGEST *value, const gdb_byte **bytes,
21123 struct dwarf2_locexpr_baton **baton)
21124 {
21125 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21126 struct objfile *objfile = per_objfile->objfile;
21127 struct comp_unit_head *cu_header = &cu->header;
21128 struct dwarf_block *blk;
21129 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21130 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21131
21132 *value = 0;
21133 *bytes = NULL;
21134 *baton = NULL;
21135
21136 switch (attr->form)
21137 {
21138 case DW_FORM_addr:
21139 case DW_FORM_addrx:
21140 case DW_FORM_GNU_addr_index:
21141 {
21142 gdb_byte *data;
21143
21144 if (TYPE_LENGTH (type) != cu_header->addr_size)
21145 dwarf2_const_value_length_mismatch_complaint (name,
21146 cu_header->addr_size,
21147 TYPE_LENGTH (type));
21148 /* Symbols of this form are reasonably rare, so we just
21149 piggyback on the existing location code rather than writing
21150 a new implementation of symbol_computed_ops. */
21151 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21152 (*baton)->per_objfile = per_objfile;
21153 (*baton)->per_cu = cu->per_cu;
21154 gdb_assert ((*baton)->per_cu);
21155
21156 (*baton)->size = 2 + cu_header->addr_size;
21157 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21158 (*baton)->data = data;
21159
21160 data[0] = DW_OP_addr;
21161 store_unsigned_integer (&data[1], cu_header->addr_size,
21162 byte_order, attr->as_address ());
21163 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21164 }
21165 break;
21166 case DW_FORM_string:
21167 case DW_FORM_strp:
21168 case DW_FORM_strx:
21169 case DW_FORM_GNU_str_index:
21170 case DW_FORM_GNU_strp_alt:
21171 /* The string is already allocated on the objfile obstack, point
21172 directly to it. */
21173 *bytes = (const gdb_byte *) attr->as_string ();
21174 break;
21175 case DW_FORM_block1:
21176 case DW_FORM_block2:
21177 case DW_FORM_block4:
21178 case DW_FORM_block:
21179 case DW_FORM_exprloc:
21180 case DW_FORM_data16:
21181 blk = attr->as_block ();
21182 if (TYPE_LENGTH (type) != blk->size)
21183 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21184 TYPE_LENGTH (type));
21185 *bytes = blk->data;
21186 break;
21187
21188 /* The DW_AT_const_value attributes are supposed to carry the
21189 symbol's value "represented as it would be on the target
21190 architecture." By the time we get here, it's already been
21191 converted to host endianness, so we just need to sign- or
21192 zero-extend it as appropriate. */
21193 case DW_FORM_data1:
21194 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21195 break;
21196 case DW_FORM_data2:
21197 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21198 break;
21199 case DW_FORM_data4:
21200 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21201 break;
21202 case DW_FORM_data8:
21203 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21204 break;
21205
21206 case DW_FORM_sdata:
21207 case DW_FORM_implicit_const:
21208 *value = attr->as_signed ();
21209 break;
21210
21211 case DW_FORM_udata:
21212 *value = attr->as_unsigned ();
21213 break;
21214
21215 default:
21216 complaint (_("unsupported const value attribute form: '%s'"),
21217 dwarf_form_name (attr->form));
21218 *value = 0;
21219 break;
21220 }
21221 }
21222
21223
21224 /* Copy constant value from an attribute to a symbol. */
21225
21226 static void
21227 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21228 struct dwarf2_cu *cu)
21229 {
21230 struct objfile *objfile = cu->per_objfile->objfile;
21231 LONGEST value;
21232 const gdb_byte *bytes;
21233 struct dwarf2_locexpr_baton *baton;
21234
21235 dwarf2_const_value_attr (attr, sym->type (),
21236 sym->print_name (),
21237 &objfile->objfile_obstack, cu,
21238 &value, &bytes, &baton);
21239
21240 if (baton != NULL)
21241 {
21242 SYMBOL_LOCATION_BATON (sym) = baton;
21243 sym->set_aclass_index (dwarf2_locexpr_index);
21244 }
21245 else if (bytes != NULL)
21246 {
21247 sym->set_value_bytes (bytes);
21248 sym->set_aclass_index (LOC_CONST_BYTES);
21249 }
21250 else
21251 {
21252 sym->set_value_longest (value);
21253 sym->set_aclass_index (LOC_CONST);
21254 }
21255 }
21256
21257 /* Return the type of the die in question using its DW_AT_type attribute. */
21258
21259 static struct type *
21260 die_type (struct die_info *die, struct dwarf2_cu *cu)
21261 {
21262 struct attribute *type_attr;
21263
21264 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21265 if (!type_attr)
21266 {
21267 struct objfile *objfile = cu->per_objfile->objfile;
21268 /* A missing DW_AT_type represents a void type. */
21269 return objfile_type (objfile)->builtin_void;
21270 }
21271
21272 return lookup_die_type (die, type_attr, cu);
21273 }
21274
21275 /* True iff CU's producer generates GNAT Ada auxiliary information
21276 that allows to find parallel types through that information instead
21277 of having to do expensive parallel lookups by type name. */
21278
21279 static int
21280 need_gnat_info (struct dwarf2_cu *cu)
21281 {
21282 /* Assume that the Ada compiler was GNAT, which always produces
21283 the auxiliary information. */
21284 return (cu->lang () == language_ada);
21285 }
21286
21287 /* Return the auxiliary type of the die in question using its
21288 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21289 attribute is not present. */
21290
21291 static struct type *
21292 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21293 {
21294 struct attribute *type_attr;
21295
21296 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21297 if (!type_attr)
21298 return NULL;
21299
21300 return lookup_die_type (die, type_attr, cu);
21301 }
21302
21303 /* If DIE has a descriptive_type attribute, then set the TYPE's
21304 descriptive type accordingly. */
21305
21306 static void
21307 set_descriptive_type (struct type *type, struct die_info *die,
21308 struct dwarf2_cu *cu)
21309 {
21310 struct type *descriptive_type = die_descriptive_type (die, cu);
21311
21312 if (descriptive_type)
21313 {
21314 ALLOCATE_GNAT_AUX_TYPE (type);
21315 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21316 }
21317 }
21318
21319 /* Return the containing type of the die in question using its
21320 DW_AT_containing_type attribute. */
21321
21322 static struct type *
21323 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21324 {
21325 struct attribute *type_attr;
21326 struct objfile *objfile = cu->per_objfile->objfile;
21327
21328 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21329 if (!type_attr)
21330 error (_("Dwarf Error: Problem turning containing type into gdb type "
21331 "[in module %s]"), objfile_name (objfile));
21332
21333 return lookup_die_type (die, type_attr, cu);
21334 }
21335
21336 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21337
21338 static struct type *
21339 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21340 {
21341 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21342 struct objfile *objfile = per_objfile->objfile;
21343 char *saved;
21344
21345 std::string message
21346 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21347 objfile_name (objfile),
21348 sect_offset_str (cu->header.sect_off),
21349 sect_offset_str (die->sect_off));
21350 saved = obstack_strdup (&objfile->objfile_obstack, message);
21351
21352 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21353 }
21354
21355 /* Look up the type of DIE in CU using its type attribute ATTR.
21356 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21357 DW_AT_containing_type.
21358 If there is no type substitute an error marker. */
21359
21360 static struct type *
21361 lookup_die_type (struct die_info *die, const struct attribute *attr,
21362 struct dwarf2_cu *cu)
21363 {
21364 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21365 struct objfile *objfile = per_objfile->objfile;
21366 struct type *this_type;
21367
21368 gdb_assert (attr->name == DW_AT_type
21369 || attr->name == DW_AT_GNAT_descriptive_type
21370 || attr->name == DW_AT_containing_type);
21371
21372 /* First see if we have it cached. */
21373
21374 if (attr->form == DW_FORM_GNU_ref_alt)
21375 {
21376 struct dwarf2_per_cu_data *per_cu;
21377 sect_offset sect_off = attr->get_ref_die_offset ();
21378
21379 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21380 per_objfile->per_bfd);
21381 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21382 }
21383 else if (attr->form_is_ref ())
21384 {
21385 sect_offset sect_off = attr->get_ref_die_offset ();
21386
21387 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21388 }
21389 else if (attr->form == DW_FORM_ref_sig8)
21390 {
21391 ULONGEST signature = attr->as_signature ();
21392
21393 return get_signatured_type (die, signature, cu);
21394 }
21395 else
21396 {
21397 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21398 " at %s [in module %s]"),
21399 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21400 objfile_name (objfile));
21401 return build_error_marker_type (cu, die);
21402 }
21403
21404 /* If not cached we need to read it in. */
21405
21406 if (this_type == NULL)
21407 {
21408 struct die_info *type_die = NULL;
21409 struct dwarf2_cu *type_cu = cu;
21410
21411 if (attr->form_is_ref ())
21412 type_die = follow_die_ref (die, attr, &type_cu);
21413 if (type_die == NULL)
21414 return build_error_marker_type (cu, die);
21415 /* If we find the type now, it's probably because the type came
21416 from an inter-CU reference and the type's CU got expanded before
21417 ours. */
21418 this_type = read_type_die (type_die, type_cu);
21419 }
21420
21421 /* If we still don't have a type use an error marker. */
21422
21423 if (this_type == NULL)
21424 return build_error_marker_type (cu, die);
21425
21426 return this_type;
21427 }
21428
21429 /* Return the type in DIE, CU.
21430 Returns NULL for invalid types.
21431
21432 This first does a lookup in die_type_hash,
21433 and only reads the die in if necessary.
21434
21435 NOTE: This can be called when reading in partial or full symbols. */
21436
21437 static struct type *
21438 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21439 {
21440 struct type *this_type;
21441
21442 this_type = get_die_type (die, cu);
21443 if (this_type)
21444 return this_type;
21445
21446 return read_type_die_1 (die, cu);
21447 }
21448
21449 /* Read the type in DIE, CU.
21450 Returns NULL for invalid types. */
21451
21452 static struct type *
21453 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21454 {
21455 struct type *this_type = NULL;
21456
21457 switch (die->tag)
21458 {
21459 case DW_TAG_class_type:
21460 case DW_TAG_interface_type:
21461 case DW_TAG_structure_type:
21462 case DW_TAG_union_type:
21463 this_type = read_structure_type (die, cu);
21464 break;
21465 case DW_TAG_enumeration_type:
21466 this_type = read_enumeration_type (die, cu);
21467 break;
21468 case DW_TAG_subprogram:
21469 case DW_TAG_subroutine_type:
21470 case DW_TAG_inlined_subroutine:
21471 this_type = read_subroutine_type (die, cu);
21472 break;
21473 case DW_TAG_array_type:
21474 this_type = read_array_type (die, cu);
21475 break;
21476 case DW_TAG_set_type:
21477 this_type = read_set_type (die, cu);
21478 break;
21479 case DW_TAG_pointer_type:
21480 this_type = read_tag_pointer_type (die, cu);
21481 break;
21482 case DW_TAG_ptr_to_member_type:
21483 this_type = read_tag_ptr_to_member_type (die, cu);
21484 break;
21485 case DW_TAG_reference_type:
21486 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21487 break;
21488 case DW_TAG_rvalue_reference_type:
21489 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21490 break;
21491 case DW_TAG_const_type:
21492 this_type = read_tag_const_type (die, cu);
21493 break;
21494 case DW_TAG_volatile_type:
21495 this_type = read_tag_volatile_type (die, cu);
21496 break;
21497 case DW_TAG_restrict_type:
21498 this_type = read_tag_restrict_type (die, cu);
21499 break;
21500 case DW_TAG_string_type:
21501 this_type = read_tag_string_type (die, cu);
21502 break;
21503 case DW_TAG_typedef:
21504 this_type = read_typedef (die, cu);
21505 break;
21506 case DW_TAG_generic_subrange:
21507 case DW_TAG_subrange_type:
21508 this_type = read_subrange_type (die, cu);
21509 break;
21510 case DW_TAG_base_type:
21511 this_type = read_base_type (die, cu);
21512 break;
21513 case DW_TAG_unspecified_type:
21514 this_type = read_unspecified_type (die, cu);
21515 break;
21516 case DW_TAG_namespace:
21517 this_type = read_namespace_type (die, cu);
21518 break;
21519 case DW_TAG_module:
21520 this_type = read_module_type (die, cu);
21521 break;
21522 case DW_TAG_atomic_type:
21523 this_type = read_tag_atomic_type (die, cu);
21524 break;
21525 default:
21526 complaint (_("unexpected tag in read_type_die: '%s'"),
21527 dwarf_tag_name (die->tag));
21528 break;
21529 }
21530
21531 return this_type;
21532 }
21533
21534 /* See if we can figure out if the class lives in a namespace. We do
21535 this by looking for a member function; its demangled name will
21536 contain namespace info, if there is any.
21537 Return the computed name or NULL.
21538 Space for the result is allocated on the objfile's obstack.
21539 This is the full-die version of guess_partial_die_structure_name.
21540 In this case we know DIE has no useful parent. */
21541
21542 static const char *
21543 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21544 {
21545 struct die_info *spec_die;
21546 struct dwarf2_cu *spec_cu;
21547 struct die_info *child;
21548 struct objfile *objfile = cu->per_objfile->objfile;
21549
21550 spec_cu = cu;
21551 spec_die = die_specification (die, &spec_cu);
21552 if (spec_die != NULL)
21553 {
21554 die = spec_die;
21555 cu = spec_cu;
21556 }
21557
21558 for (child = die->child;
21559 child != NULL;
21560 child = child->sibling)
21561 {
21562 if (child->tag == DW_TAG_subprogram)
21563 {
21564 const char *linkage_name = dw2_linkage_name (child, cu);
21565
21566 if (linkage_name != NULL)
21567 {
21568 gdb::unique_xmalloc_ptr<char> actual_name
21569 (cu->language_defn->class_name_from_physname (linkage_name));
21570 const char *name = NULL;
21571
21572 if (actual_name != NULL)
21573 {
21574 const char *die_name = dwarf2_name (die, cu);
21575
21576 if (die_name != NULL
21577 && strcmp (die_name, actual_name.get ()) != 0)
21578 {
21579 /* Strip off the class name from the full name.
21580 We want the prefix. */
21581 int die_name_len = strlen (die_name);
21582 int actual_name_len = strlen (actual_name.get ());
21583 const char *ptr = actual_name.get ();
21584
21585 /* Test for '::' as a sanity check. */
21586 if (actual_name_len > die_name_len + 2
21587 && ptr[actual_name_len - die_name_len - 1] == ':')
21588 name = obstack_strndup (
21589 &objfile->per_bfd->storage_obstack,
21590 ptr, actual_name_len - die_name_len - 2);
21591 }
21592 }
21593 return name;
21594 }
21595 }
21596 }
21597
21598 return NULL;
21599 }
21600
21601 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21602 prefix part in such case. See
21603 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21604
21605 static const char *
21606 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21607 {
21608 struct attribute *attr;
21609 const char *base;
21610
21611 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21612 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21613 return NULL;
21614
21615 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21616 return NULL;
21617
21618 attr = dw2_linkage_name_attr (die, cu);
21619 const char *attr_name = attr->as_string ();
21620 if (attr == NULL || attr_name == NULL)
21621 return NULL;
21622
21623 /* dwarf2_name had to be already called. */
21624 gdb_assert (attr->canonical_string_p ());
21625
21626 /* Strip the base name, keep any leading namespaces/classes. */
21627 base = strrchr (attr_name, ':');
21628 if (base == NULL || base == attr_name || base[-1] != ':')
21629 return "";
21630
21631 struct objfile *objfile = cu->per_objfile->objfile;
21632 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21633 attr_name,
21634 &base[-1] - attr_name);
21635 }
21636
21637 /* Return the name of the namespace/class that DIE is defined within,
21638 or "" if we can't tell. The caller should not xfree the result.
21639
21640 For example, if we're within the method foo() in the following
21641 code:
21642
21643 namespace N {
21644 class C {
21645 void foo () {
21646 }
21647 };
21648 }
21649
21650 then determine_prefix on foo's die will return "N::C". */
21651
21652 static const char *
21653 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21654 {
21655 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21656 struct die_info *parent, *spec_die;
21657 struct dwarf2_cu *spec_cu;
21658 struct type *parent_type;
21659 const char *retval;
21660
21661 if (cu->lang () != language_cplus
21662 && cu->lang () != language_fortran
21663 && cu->lang () != language_d
21664 && cu->lang () != language_rust)
21665 return "";
21666
21667 retval = anonymous_struct_prefix (die, cu);
21668 if (retval)
21669 return retval;
21670
21671 /* We have to be careful in the presence of DW_AT_specification.
21672 For example, with GCC 3.4, given the code
21673
21674 namespace N {
21675 void foo() {
21676 // Definition of N::foo.
21677 }
21678 }
21679
21680 then we'll have a tree of DIEs like this:
21681
21682 1: DW_TAG_compile_unit
21683 2: DW_TAG_namespace // N
21684 3: DW_TAG_subprogram // declaration of N::foo
21685 4: DW_TAG_subprogram // definition of N::foo
21686 DW_AT_specification // refers to die #3
21687
21688 Thus, when processing die #4, we have to pretend that we're in
21689 the context of its DW_AT_specification, namely the contex of die
21690 #3. */
21691 spec_cu = cu;
21692 spec_die = die_specification (die, &spec_cu);
21693 if (spec_die == NULL)
21694 parent = die->parent;
21695 else
21696 {
21697 parent = spec_die->parent;
21698 cu = spec_cu;
21699 }
21700
21701 if (parent == NULL)
21702 return "";
21703 else if (parent->building_fullname)
21704 {
21705 const char *name;
21706 const char *parent_name;
21707
21708 /* It has been seen on RealView 2.2 built binaries,
21709 DW_TAG_template_type_param types actually _defined_ as
21710 children of the parent class:
21711
21712 enum E {};
21713 template class <class Enum> Class{};
21714 Class<enum E> class_e;
21715
21716 1: DW_TAG_class_type (Class)
21717 2: DW_TAG_enumeration_type (E)
21718 3: DW_TAG_enumerator (enum1:0)
21719 3: DW_TAG_enumerator (enum2:1)
21720 ...
21721 2: DW_TAG_template_type_param
21722 DW_AT_type DW_FORM_ref_udata (E)
21723
21724 Besides being broken debug info, it can put GDB into an
21725 infinite loop. Consider:
21726
21727 When we're building the full name for Class<E>, we'll start
21728 at Class, and go look over its template type parameters,
21729 finding E. We'll then try to build the full name of E, and
21730 reach here. We're now trying to build the full name of E,
21731 and look over the parent DIE for containing scope. In the
21732 broken case, if we followed the parent DIE of E, we'd again
21733 find Class, and once again go look at its template type
21734 arguments, etc., etc. Simply don't consider such parent die
21735 as source-level parent of this die (it can't be, the language
21736 doesn't allow it), and break the loop here. */
21737 name = dwarf2_name (die, cu);
21738 parent_name = dwarf2_name (parent, cu);
21739 complaint (_("template param type '%s' defined within parent '%s'"),
21740 name ? name : "<unknown>",
21741 parent_name ? parent_name : "<unknown>");
21742 return "";
21743 }
21744 else
21745 switch (parent->tag)
21746 {
21747 case DW_TAG_namespace:
21748 parent_type = read_type_die (parent, cu);
21749 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21750 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21751 Work around this problem here. */
21752 if (cu->lang () == language_cplus
21753 && strcmp (parent_type->name (), "::") == 0)
21754 return "";
21755 /* We give a name to even anonymous namespaces. */
21756 return parent_type->name ();
21757 case DW_TAG_class_type:
21758 case DW_TAG_interface_type:
21759 case DW_TAG_structure_type:
21760 case DW_TAG_union_type:
21761 case DW_TAG_module:
21762 parent_type = read_type_die (parent, cu);
21763 if (parent_type->name () != NULL)
21764 return parent_type->name ();
21765 else
21766 /* An anonymous structure is only allowed non-static data
21767 members; no typedefs, no member functions, et cetera.
21768 So it does not need a prefix. */
21769 return "";
21770 case DW_TAG_compile_unit:
21771 case DW_TAG_partial_unit:
21772 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21773 if (cu->lang () == language_cplus
21774 && !per_objfile->per_bfd->types.empty ()
21775 && die->child != NULL
21776 && (die->tag == DW_TAG_class_type
21777 || die->tag == DW_TAG_structure_type
21778 || die->tag == DW_TAG_union_type))
21779 {
21780 const char *name = guess_full_die_structure_name (die, cu);
21781 if (name != NULL)
21782 return name;
21783 }
21784 return "";
21785 case DW_TAG_subprogram:
21786 /* Nested subroutines in Fortran get a prefix with the name
21787 of the parent's subroutine. */
21788 if (cu->lang () == language_fortran)
21789 {
21790 if ((die->tag == DW_TAG_subprogram)
21791 && (dwarf2_name (parent, cu) != NULL))
21792 return dwarf2_name (parent, cu);
21793 }
21794 return "";
21795 case DW_TAG_enumeration_type:
21796 parent_type = read_type_die (parent, cu);
21797 if (parent_type->is_declared_class ())
21798 {
21799 if (parent_type->name () != NULL)
21800 return parent_type->name ();
21801 return "";
21802 }
21803 /* Fall through. */
21804 default:
21805 return determine_prefix (parent, cu);
21806 }
21807 }
21808
21809 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21810 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21811 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21812 an obconcat, otherwise allocate storage for the result. The CU argument is
21813 used to determine the language and hence, the appropriate separator. */
21814
21815 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21816
21817 static char *
21818 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21819 int physname, struct dwarf2_cu *cu)
21820 {
21821 const char *lead = "";
21822 const char *sep;
21823
21824 if (suffix == NULL || suffix[0] == '\0'
21825 || prefix == NULL || prefix[0] == '\0')
21826 sep = "";
21827 else if (cu->lang () == language_d)
21828 {
21829 /* For D, the 'main' function could be defined in any module, but it
21830 should never be prefixed. */
21831 if (strcmp (suffix, "D main") == 0)
21832 {
21833 prefix = "";
21834 sep = "";
21835 }
21836 else
21837 sep = ".";
21838 }
21839 else if (cu->lang () == language_fortran && physname)
21840 {
21841 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21842 DW_AT_MIPS_linkage_name is preferred and used instead. */
21843
21844 lead = "__";
21845 sep = "_MOD_";
21846 }
21847 else
21848 sep = "::";
21849
21850 if (prefix == NULL)
21851 prefix = "";
21852 if (suffix == NULL)
21853 suffix = "";
21854
21855 if (obs == NULL)
21856 {
21857 char *retval
21858 = ((char *)
21859 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21860
21861 strcpy (retval, lead);
21862 strcat (retval, prefix);
21863 strcat (retval, sep);
21864 strcat (retval, suffix);
21865 return retval;
21866 }
21867 else
21868 {
21869 /* We have an obstack. */
21870 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21871 }
21872 }
21873
21874 /* Get name of a die, return NULL if not found. */
21875
21876 static const char *
21877 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21878 struct objfile *objfile)
21879 {
21880 if (name && cu->lang () == language_cplus)
21881 {
21882 gdb::unique_xmalloc_ptr<char> canon_name
21883 = cp_canonicalize_string (name);
21884
21885 if (canon_name != nullptr)
21886 name = objfile->intern (canon_name.get ());
21887 }
21888
21889 return name;
21890 }
21891
21892 /* Get name of a die, return NULL if not found.
21893 Anonymous namespaces are converted to their magic string. */
21894
21895 static const char *
21896 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21897 {
21898 struct attribute *attr;
21899 struct objfile *objfile = cu->per_objfile->objfile;
21900
21901 attr = dwarf2_attr (die, DW_AT_name, cu);
21902 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
21903 if (attr_name == nullptr
21904 && die->tag != DW_TAG_namespace
21905 && die->tag != DW_TAG_class_type
21906 && die->tag != DW_TAG_interface_type
21907 && die->tag != DW_TAG_structure_type
21908 && die->tag != DW_TAG_namelist
21909 && die->tag != DW_TAG_union_type)
21910 return NULL;
21911
21912 switch (die->tag)
21913 {
21914 case DW_TAG_compile_unit:
21915 case DW_TAG_partial_unit:
21916 /* Compilation units have a DW_AT_name that is a filename, not
21917 a source language identifier. */
21918 case DW_TAG_enumeration_type:
21919 case DW_TAG_enumerator:
21920 /* These tags always have simple identifiers already; no need
21921 to canonicalize them. */
21922 return attr_name;
21923
21924 case DW_TAG_namespace:
21925 if (attr_name != nullptr)
21926 return attr_name;
21927 return CP_ANONYMOUS_NAMESPACE_STR;
21928
21929 case DW_TAG_class_type:
21930 case DW_TAG_interface_type:
21931 case DW_TAG_structure_type:
21932 case DW_TAG_union_type:
21933 case DW_TAG_namelist:
21934 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21935 structures or unions. These were of the form "._%d" in GCC 4.1,
21936 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21937 and GCC 4.4. We work around this problem by ignoring these. */
21938 if (attr_name != nullptr
21939 && (startswith (attr_name, "._")
21940 || startswith (attr_name, "<anonymous")))
21941 return NULL;
21942
21943 /* GCC might emit a nameless typedef that has a linkage name. See
21944 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21945 if (!attr || attr_name == NULL)
21946 {
21947 attr = dw2_linkage_name_attr (die, cu);
21948 attr_name = attr == nullptr ? nullptr : attr->as_string ();
21949 if (attr == NULL || attr_name == NULL)
21950 return NULL;
21951
21952 /* Avoid demangling attr_name the second time on a second
21953 call for the same DIE. */
21954 if (!attr->canonical_string_p ())
21955 {
21956 gdb::unique_xmalloc_ptr<char> demangled
21957 (gdb_demangle (attr_name, DMGL_TYPES));
21958 if (demangled == nullptr)
21959 return nullptr;
21960
21961 attr->set_string_canonical (objfile->intern (demangled.get ()));
21962 attr_name = attr->as_string ();
21963 }
21964
21965 /* Strip any leading namespaces/classes, keep only the
21966 base name. DW_AT_name for named DIEs does not
21967 contain the prefixes. */
21968 const char *base = strrchr (attr_name, ':');
21969 if (base && base > attr_name && base[-1] == ':')
21970 return &base[1];
21971 else
21972 return attr_name;
21973 }
21974 break;
21975
21976 default:
21977 break;
21978 }
21979
21980 if (!attr->canonical_string_p ())
21981 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
21982 objfile));
21983 return attr->as_string ();
21984 }
21985
21986 /* Return the die that this die in an extension of, or NULL if there
21987 is none. *EXT_CU is the CU containing DIE on input, and the CU
21988 containing the return value on output. */
21989
21990 static struct die_info *
21991 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21992 {
21993 struct attribute *attr;
21994
21995 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21996 if (attr == NULL)
21997 return NULL;
21998
21999 return follow_die_ref (die, attr, ext_cu);
22000 }
22001
22002 static void
22003 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22004 {
22005 unsigned int i;
22006
22007 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
22008 indent, "",
22009 dwarf_tag_name (die->tag), die->abbrev,
22010 sect_offset_str (die->sect_off));
22011
22012 if (die->parent != NULL)
22013 gdb_printf (f, "%*s parent at offset: %s\n",
22014 indent, "",
22015 sect_offset_str (die->parent->sect_off));
22016
22017 gdb_printf (f, "%*s has children: %s\n",
22018 indent, "",
22019 dwarf_bool_name (die->child != NULL));
22020
22021 gdb_printf (f, "%*s attributes:\n", indent, "");
22022
22023 for (i = 0; i < die->num_attrs; ++i)
22024 {
22025 gdb_printf (f, "%*s %s (%s) ",
22026 indent, "",
22027 dwarf_attr_name (die->attrs[i].name),
22028 dwarf_form_name (die->attrs[i].form));
22029
22030 switch (die->attrs[i].form)
22031 {
22032 case DW_FORM_addr:
22033 case DW_FORM_addrx:
22034 case DW_FORM_GNU_addr_index:
22035 gdb_printf (f, "address: ");
22036 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
22037 break;
22038 case DW_FORM_block2:
22039 case DW_FORM_block4:
22040 case DW_FORM_block:
22041 case DW_FORM_block1:
22042 gdb_printf (f, "block: size %s",
22043 pulongest (die->attrs[i].as_block ()->size));
22044 break;
22045 case DW_FORM_exprloc:
22046 gdb_printf (f, "expression: size %s",
22047 pulongest (die->attrs[i].as_block ()->size));
22048 break;
22049 case DW_FORM_data16:
22050 gdb_printf (f, "constant of 16 bytes");
22051 break;
22052 case DW_FORM_ref_addr:
22053 gdb_printf (f, "ref address: ");
22054 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
22055 break;
22056 case DW_FORM_GNU_ref_alt:
22057 gdb_printf (f, "alt ref address: ");
22058 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
22059 break;
22060 case DW_FORM_ref1:
22061 case DW_FORM_ref2:
22062 case DW_FORM_ref4:
22063 case DW_FORM_ref8:
22064 case DW_FORM_ref_udata:
22065 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
22066 (long) (die->attrs[i].as_unsigned ()));
22067 break;
22068 case DW_FORM_data1:
22069 case DW_FORM_data2:
22070 case DW_FORM_data4:
22071 case DW_FORM_data8:
22072 case DW_FORM_udata:
22073 gdb_printf (f, "constant: %s",
22074 pulongest (die->attrs[i].as_unsigned ()));
22075 break;
22076 case DW_FORM_sec_offset:
22077 gdb_printf (f, "section offset: %s",
22078 pulongest (die->attrs[i].as_unsigned ()));
22079 break;
22080 case DW_FORM_ref_sig8:
22081 gdb_printf (f, "signature: %s",
22082 hex_string (die->attrs[i].as_signature ()));
22083 break;
22084 case DW_FORM_string:
22085 case DW_FORM_strp:
22086 case DW_FORM_line_strp:
22087 case DW_FORM_strx:
22088 case DW_FORM_GNU_str_index:
22089 case DW_FORM_GNU_strp_alt:
22090 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
22091 die->attrs[i].as_string ()
22092 ? die->attrs[i].as_string () : "",
22093 die->attrs[i].canonical_string_p () ? "is" : "not");
22094 break;
22095 case DW_FORM_flag:
22096 if (die->attrs[i].as_boolean ())
22097 gdb_printf (f, "flag: TRUE");
22098 else
22099 gdb_printf (f, "flag: FALSE");
22100 break;
22101 case DW_FORM_flag_present:
22102 gdb_printf (f, "flag: TRUE");
22103 break;
22104 case DW_FORM_indirect:
22105 /* The reader will have reduced the indirect form to
22106 the "base form" so this form should not occur. */
22107 gdb_printf (f,
22108 "unexpected attribute form: DW_FORM_indirect");
22109 break;
22110 case DW_FORM_sdata:
22111 case DW_FORM_implicit_const:
22112 gdb_printf (f, "constant: %s",
22113 plongest (die->attrs[i].as_signed ()));
22114 break;
22115 default:
22116 gdb_printf (f, "unsupported attribute form: %d.",
22117 die->attrs[i].form);
22118 break;
22119 }
22120 gdb_printf (f, "\n");
22121 }
22122 }
22123
22124 static void
22125 dump_die_for_error (struct die_info *die)
22126 {
22127 dump_die_shallow (gdb_stderr, 0, die);
22128 }
22129
22130 static void
22131 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22132 {
22133 int indent = level * 4;
22134
22135 gdb_assert (die != NULL);
22136
22137 if (level >= max_level)
22138 return;
22139
22140 dump_die_shallow (f, indent, die);
22141
22142 if (die->child != NULL)
22143 {
22144 gdb_printf (f, "%*s Children:", indent, "");
22145 if (level + 1 < max_level)
22146 {
22147 gdb_printf (f, "\n");
22148 dump_die_1 (f, level + 1, max_level, die->child);
22149 }
22150 else
22151 {
22152 gdb_printf (f,
22153 " [not printed, max nesting level reached]\n");
22154 }
22155 }
22156
22157 if (die->sibling != NULL && level > 0)
22158 {
22159 dump_die_1 (f, level, max_level, die->sibling);
22160 }
22161 }
22162
22163 /* This is called from the pdie macro in gdbinit.in.
22164 It's not static so gcc will keep a copy callable from gdb. */
22165
22166 void
22167 dump_die (struct die_info *die, int max_level)
22168 {
22169 dump_die_1 (gdb_stdlog, 0, max_level, die);
22170 }
22171
22172 static void
22173 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22174 {
22175 void **slot;
22176
22177 slot = htab_find_slot_with_hash (cu->die_hash, die,
22178 to_underlying (die->sect_off),
22179 INSERT);
22180
22181 *slot = die;
22182 }
22183
22184 /* Follow reference or signature attribute ATTR of SRC_DIE.
22185 On entry *REF_CU is the CU of SRC_DIE.
22186 On exit *REF_CU is the CU of the result. */
22187
22188 static struct die_info *
22189 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22190 struct dwarf2_cu **ref_cu)
22191 {
22192 struct die_info *die;
22193
22194 if (attr->form_is_ref ())
22195 die = follow_die_ref (src_die, attr, ref_cu);
22196 else if (attr->form == DW_FORM_ref_sig8)
22197 die = follow_die_sig (src_die, attr, ref_cu);
22198 else
22199 {
22200 dump_die_for_error (src_die);
22201 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22202 objfile_name ((*ref_cu)->per_objfile->objfile));
22203 }
22204
22205 return die;
22206 }
22207
22208 /* Follow reference OFFSET.
22209 On entry *REF_CU is the CU of the source die referencing OFFSET.
22210 On exit *REF_CU is the CU of the result.
22211 Returns NULL if OFFSET is invalid. */
22212
22213 static struct die_info *
22214 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22215 struct dwarf2_cu **ref_cu)
22216 {
22217 struct die_info temp_die;
22218 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22219 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22220
22221 gdb_assert (cu->per_cu != NULL);
22222
22223 target_cu = cu;
22224
22225 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22226 "source CU contains target offset: %d",
22227 sect_offset_str (cu->per_cu->sect_off),
22228 sect_offset_str (sect_off),
22229 cu->header.offset_in_cu_p (sect_off));
22230
22231 if (cu->per_cu->is_debug_types)
22232 {
22233 /* .debug_types CUs cannot reference anything outside their CU.
22234 If they need to, they have to reference a signatured type via
22235 DW_FORM_ref_sig8. */
22236 if (!cu->header.offset_in_cu_p (sect_off))
22237 return NULL;
22238 }
22239 else if (offset_in_dwz != cu->per_cu->is_dwz
22240 || !cu->header.offset_in_cu_p (sect_off))
22241 {
22242 struct dwarf2_per_cu_data *per_cu;
22243
22244 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22245 per_objfile->per_bfd);
22246
22247 dwarf_read_debug_printf_v ("target CU offset: %s, "
22248 "target CU DIEs loaded: %d",
22249 sect_offset_str (per_cu->sect_off),
22250 per_objfile->get_cu (per_cu) != nullptr);
22251
22252 /* If necessary, add it to the queue and load its DIEs.
22253
22254 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22255 it doesn't mean they are currently loaded. Since we require them
22256 to be loaded, we must check for ourselves. */
22257 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
22258 || per_objfile->get_cu (per_cu) == nullptr)
22259 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
22260 false, cu->lang ());
22261
22262 target_cu = per_objfile->get_cu (per_cu);
22263 gdb_assert (target_cu != nullptr);
22264 }
22265 else if (cu->dies == NULL)
22266 {
22267 /* We're loading full DIEs during partial symbol reading. */
22268 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
22269 language_minimal);
22270 }
22271
22272 *ref_cu = target_cu;
22273 temp_die.sect_off = sect_off;
22274
22275 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22276 &temp_die,
22277 to_underlying (sect_off));
22278 }
22279
22280 /* Follow reference attribute ATTR of SRC_DIE.
22281 On entry *REF_CU is the CU of SRC_DIE.
22282 On exit *REF_CU is the CU of the result. */
22283
22284 static struct die_info *
22285 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22286 struct dwarf2_cu **ref_cu)
22287 {
22288 sect_offset sect_off = attr->get_ref_die_offset ();
22289 struct dwarf2_cu *cu = *ref_cu;
22290 struct die_info *die;
22291
22292 die = follow_die_offset (sect_off,
22293 (attr->form == DW_FORM_GNU_ref_alt
22294 || cu->per_cu->is_dwz),
22295 ref_cu);
22296 if (!die)
22297 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22298 "at %s [in module %s]"),
22299 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22300 objfile_name (cu->per_objfile->objfile));
22301
22302 return die;
22303 }
22304
22305 /* See read.h. */
22306
22307 struct dwarf2_locexpr_baton
22308 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22309 dwarf2_per_cu_data *per_cu,
22310 dwarf2_per_objfile *per_objfile,
22311 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22312 bool resolve_abstract_p)
22313 {
22314 struct die_info *die;
22315 struct attribute *attr;
22316 struct dwarf2_locexpr_baton retval;
22317 struct objfile *objfile = per_objfile->objfile;
22318
22319 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22320 if (cu == nullptr)
22321 cu = load_cu (per_cu, per_objfile, false);
22322
22323 if (cu == nullptr)
22324 {
22325 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22326 Instead just throw an error, not much else we can do. */
22327 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22328 sect_offset_str (sect_off), objfile_name (objfile));
22329 }
22330
22331 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22332 if (!die)
22333 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22334 sect_offset_str (sect_off), objfile_name (objfile));
22335
22336 attr = dwarf2_attr (die, DW_AT_location, cu);
22337 if (!attr && resolve_abstract_p
22338 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22339 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22340 {
22341 CORE_ADDR pc = get_frame_pc ();
22342 CORE_ADDR baseaddr = objfile->text_section_offset ();
22343 struct gdbarch *gdbarch = objfile->arch ();
22344
22345 for (const auto &cand_off
22346 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22347 {
22348 struct dwarf2_cu *cand_cu = cu;
22349 struct die_info *cand
22350 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22351 if (!cand
22352 || !cand->parent
22353 || cand->parent->tag != DW_TAG_subprogram)
22354 continue;
22355
22356 CORE_ADDR pc_low, pc_high;
22357 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22358 if (pc_low == ((CORE_ADDR) -1))
22359 continue;
22360 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22361 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22362 if (!(pc_low <= pc && pc < pc_high))
22363 continue;
22364
22365 die = cand;
22366 attr = dwarf2_attr (die, DW_AT_location, cu);
22367 break;
22368 }
22369 }
22370
22371 if (!attr)
22372 {
22373 /* DWARF: "If there is no such attribute, then there is no effect.".
22374 DATA is ignored if SIZE is 0. */
22375
22376 retval.data = NULL;
22377 retval.size = 0;
22378 }
22379 else if (attr->form_is_section_offset ())
22380 {
22381 struct dwarf2_loclist_baton loclist_baton;
22382 CORE_ADDR pc = get_frame_pc ();
22383 size_t size;
22384
22385 fill_in_loclist_baton (cu, &loclist_baton, attr);
22386
22387 retval.data = dwarf2_find_location_expression (&loclist_baton,
22388 &size, pc);
22389 retval.size = size;
22390 }
22391 else
22392 {
22393 if (!attr->form_is_block ())
22394 error (_("Dwarf Error: DIE at %s referenced in module %s "
22395 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22396 sect_offset_str (sect_off), objfile_name (objfile));
22397
22398 struct dwarf_block *block = attr->as_block ();
22399 retval.data = block->data;
22400 retval.size = block->size;
22401 }
22402 retval.per_objfile = per_objfile;
22403 retval.per_cu = cu->per_cu;
22404
22405 per_objfile->age_comp_units ();
22406
22407 return retval;
22408 }
22409
22410 /* See read.h. */
22411
22412 struct dwarf2_locexpr_baton
22413 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22414 dwarf2_per_cu_data *per_cu,
22415 dwarf2_per_objfile *per_objfile,
22416 gdb::function_view<CORE_ADDR ()> get_frame_pc)
22417 {
22418 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22419
22420 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22421 get_frame_pc);
22422 }
22423
22424 /* Write a constant of a given type as target-ordered bytes into
22425 OBSTACK. */
22426
22427 static const gdb_byte *
22428 write_constant_as_bytes (struct obstack *obstack,
22429 enum bfd_endian byte_order,
22430 struct type *type,
22431 ULONGEST value,
22432 LONGEST *len)
22433 {
22434 gdb_byte *result;
22435
22436 *len = TYPE_LENGTH (type);
22437 result = (gdb_byte *) obstack_alloc (obstack, *len);
22438 store_unsigned_integer (result, *len, byte_order, value);
22439
22440 return result;
22441 }
22442
22443 /* See read.h. */
22444
22445 const gdb_byte *
22446 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22447 dwarf2_per_cu_data *per_cu,
22448 dwarf2_per_objfile *per_objfile,
22449 obstack *obstack,
22450 LONGEST *len)
22451 {
22452 struct die_info *die;
22453 struct attribute *attr;
22454 const gdb_byte *result = NULL;
22455 struct type *type;
22456 LONGEST value;
22457 enum bfd_endian byte_order;
22458 struct objfile *objfile = per_objfile->objfile;
22459
22460 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22461 if (cu == nullptr)
22462 cu = load_cu (per_cu, per_objfile, false);
22463
22464 if (cu == nullptr)
22465 {
22466 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22467 Instead just throw an error, not much else we can do. */
22468 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22469 sect_offset_str (sect_off), objfile_name (objfile));
22470 }
22471
22472 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22473 if (!die)
22474 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22475 sect_offset_str (sect_off), objfile_name (objfile));
22476
22477 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22478 if (attr == NULL)
22479 return NULL;
22480
22481 byte_order = (bfd_big_endian (objfile->obfd)
22482 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22483
22484 switch (attr->form)
22485 {
22486 case DW_FORM_addr:
22487 case DW_FORM_addrx:
22488 case DW_FORM_GNU_addr_index:
22489 {
22490 gdb_byte *tem;
22491
22492 *len = cu->header.addr_size;
22493 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22494 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
22495 result = tem;
22496 }
22497 break;
22498 case DW_FORM_string:
22499 case DW_FORM_strp:
22500 case DW_FORM_strx:
22501 case DW_FORM_GNU_str_index:
22502 case DW_FORM_GNU_strp_alt:
22503 /* The string is already allocated on the objfile obstack, point
22504 directly to it. */
22505 {
22506 const char *attr_name = attr->as_string ();
22507 result = (const gdb_byte *) attr_name;
22508 *len = strlen (attr_name);
22509 }
22510 break;
22511 case DW_FORM_block1:
22512 case DW_FORM_block2:
22513 case DW_FORM_block4:
22514 case DW_FORM_block:
22515 case DW_FORM_exprloc:
22516 case DW_FORM_data16:
22517 {
22518 struct dwarf_block *block = attr->as_block ();
22519 result = block->data;
22520 *len = block->size;
22521 }
22522 break;
22523
22524 /* The DW_AT_const_value attributes are supposed to carry the
22525 symbol's value "represented as it would be on the target
22526 architecture." By the time we get here, it's already been
22527 converted to host endianness, so we just need to sign- or
22528 zero-extend it as appropriate. */
22529 case DW_FORM_data1:
22530 type = die_type (die, cu);
22531 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22532 if (result == NULL)
22533 result = write_constant_as_bytes (obstack, byte_order,
22534 type, value, len);
22535 break;
22536 case DW_FORM_data2:
22537 type = die_type (die, cu);
22538 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22539 if (result == NULL)
22540 result = write_constant_as_bytes (obstack, byte_order,
22541 type, value, len);
22542 break;
22543 case DW_FORM_data4:
22544 type = die_type (die, cu);
22545 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22546 if (result == NULL)
22547 result = write_constant_as_bytes (obstack, byte_order,
22548 type, value, len);
22549 break;
22550 case DW_FORM_data8:
22551 type = die_type (die, cu);
22552 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22553 if (result == NULL)
22554 result = write_constant_as_bytes (obstack, byte_order,
22555 type, value, len);
22556 break;
22557
22558 case DW_FORM_sdata:
22559 case DW_FORM_implicit_const:
22560 type = die_type (die, cu);
22561 result = write_constant_as_bytes (obstack, byte_order,
22562 type, attr->as_signed (), len);
22563 break;
22564
22565 case DW_FORM_udata:
22566 type = die_type (die, cu);
22567 result = write_constant_as_bytes (obstack, byte_order,
22568 type, attr->as_unsigned (), len);
22569 break;
22570
22571 default:
22572 complaint (_("unsupported const value attribute form: '%s'"),
22573 dwarf_form_name (attr->form));
22574 break;
22575 }
22576
22577 return result;
22578 }
22579
22580 /* See read.h. */
22581
22582 struct type *
22583 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22584 dwarf2_per_cu_data *per_cu,
22585 dwarf2_per_objfile *per_objfile,
22586 const char **var_name)
22587 {
22588 struct die_info *die;
22589
22590 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22591 if (cu == nullptr)
22592 cu = load_cu (per_cu, per_objfile, false);
22593
22594 if (cu == nullptr)
22595 return nullptr;
22596
22597 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22598 if (!die)
22599 return NULL;
22600
22601 if (var_name != nullptr)
22602 *var_name = var_decl_name (die, cu);
22603 return die_type (die, cu);
22604 }
22605
22606 /* See read.h. */
22607
22608 struct type *
22609 dwarf2_get_die_type (cu_offset die_offset,
22610 dwarf2_per_cu_data *per_cu,
22611 dwarf2_per_objfile *per_objfile)
22612 {
22613 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22614 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22615 }
22616
22617 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22618 On entry *REF_CU is the CU of SRC_DIE.
22619 On exit *REF_CU is the CU of the result.
22620 Returns NULL if the referenced DIE isn't found. */
22621
22622 static struct die_info *
22623 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22624 struct dwarf2_cu **ref_cu)
22625 {
22626 struct die_info temp_die;
22627 struct dwarf2_cu *sig_cu;
22628 struct die_info *die;
22629 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22630
22631
22632 /* While it might be nice to assert sig_type->type == NULL here,
22633 we can get here for DW_AT_imported_declaration where we need
22634 the DIE not the type. */
22635
22636 /* If necessary, add it to the queue and load its DIEs.
22637
22638 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22639 it doesn't mean they are currently loaded. Since we require them
22640 to be loaded, we must check for ourselves. */
22641 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
22642 language_minimal)
22643 || per_objfile->get_cu (sig_type) == nullptr)
22644 read_signatured_type (sig_type, per_objfile);
22645
22646 sig_cu = per_objfile->get_cu (sig_type);
22647 gdb_assert (sig_cu != NULL);
22648 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22649 temp_die.sect_off = sig_type->type_offset_in_section;
22650 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22651 to_underlying (temp_die.sect_off));
22652 if (die)
22653 {
22654 /* For .gdb_index version 7 keep track of included TUs.
22655 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22656 if (per_objfile->per_bfd->index_table != NULL
22657 && !per_objfile->per_bfd->index_table->version_check ())
22658 {
22659 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22660 }
22661
22662 *ref_cu = sig_cu;
22663 return die;
22664 }
22665
22666 return NULL;
22667 }
22668
22669 /* Follow signatured type referenced by ATTR in SRC_DIE.
22670 On entry *REF_CU is the CU of SRC_DIE.
22671 On exit *REF_CU is the CU of the result.
22672 The result is the DIE of the type.
22673 If the referenced type cannot be found an error is thrown. */
22674
22675 static struct die_info *
22676 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22677 struct dwarf2_cu **ref_cu)
22678 {
22679 ULONGEST signature = attr->as_signature ();
22680 struct signatured_type *sig_type;
22681 struct die_info *die;
22682
22683 gdb_assert (attr->form == DW_FORM_ref_sig8);
22684
22685 sig_type = lookup_signatured_type (*ref_cu, signature);
22686 /* sig_type will be NULL if the signatured type is missing from
22687 the debug info. */
22688 if (sig_type == NULL)
22689 {
22690 error (_("Dwarf Error: Cannot find 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 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22697 if (die == NULL)
22698 {
22699 dump_die_for_error (src_die);
22700 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22701 " from DIE at %s [in module %s]"),
22702 hex_string (signature), sect_offset_str (src_die->sect_off),
22703 objfile_name ((*ref_cu)->per_objfile->objfile));
22704 }
22705
22706 return die;
22707 }
22708
22709 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22710 reading in and processing the type unit if necessary. */
22711
22712 static struct type *
22713 get_signatured_type (struct die_info *die, ULONGEST signature,
22714 struct dwarf2_cu *cu)
22715 {
22716 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22717 struct signatured_type *sig_type;
22718 struct dwarf2_cu *type_cu;
22719 struct die_info *type_die;
22720 struct type *type;
22721
22722 sig_type = lookup_signatured_type (cu, signature);
22723 /* sig_type will be NULL if the signatured type is missing from
22724 the debug info. */
22725 if (sig_type == NULL)
22726 {
22727 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22728 " from DIE at %s [in module %s]"),
22729 hex_string (signature), sect_offset_str (die->sect_off),
22730 objfile_name (per_objfile->objfile));
22731 return build_error_marker_type (cu, die);
22732 }
22733
22734 /* If we already know the type we're done. */
22735 type = per_objfile->get_type_for_signatured_type (sig_type);
22736 if (type != nullptr)
22737 return type;
22738
22739 type_cu = cu;
22740 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22741 if (type_die != NULL)
22742 {
22743 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22744 is created. This is important, for example, because for c++ classes
22745 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22746 type = read_type_die (type_die, type_cu);
22747 if (type == NULL)
22748 {
22749 complaint (_("Dwarf Error: Cannot build signatured type %s"
22750 " referenced from DIE at %s [in module %s]"),
22751 hex_string (signature), sect_offset_str (die->sect_off),
22752 objfile_name (per_objfile->objfile));
22753 type = build_error_marker_type (cu, die);
22754 }
22755 }
22756 else
22757 {
22758 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22759 " from DIE at %s [in module %s]"),
22760 hex_string (signature), sect_offset_str (die->sect_off),
22761 objfile_name (per_objfile->objfile));
22762 type = build_error_marker_type (cu, die);
22763 }
22764
22765 per_objfile->set_type_for_signatured_type (sig_type, type);
22766
22767 return type;
22768 }
22769
22770 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22771 reading in and processing the type unit if necessary. */
22772
22773 static struct type *
22774 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22775 struct dwarf2_cu *cu) /* ARI: editCase function */
22776 {
22777 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22778 if (attr->form_is_ref ())
22779 {
22780 struct dwarf2_cu *type_cu = cu;
22781 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22782
22783 return read_type_die (type_die, type_cu);
22784 }
22785 else if (attr->form == DW_FORM_ref_sig8)
22786 {
22787 return get_signatured_type (die, attr->as_signature (), cu);
22788 }
22789 else
22790 {
22791 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22792
22793 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22794 " at %s [in module %s]"),
22795 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22796 objfile_name (per_objfile->objfile));
22797 return build_error_marker_type (cu, die);
22798 }
22799 }
22800
22801 /* Load the DIEs associated with type unit PER_CU into memory. */
22802
22803 static void
22804 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22805 dwarf2_per_objfile *per_objfile)
22806 {
22807 struct signatured_type *sig_type;
22808
22809 /* We have the per_cu, but we need the signatured_type.
22810 Fortunately this is an easy translation. */
22811 gdb_assert (per_cu->is_debug_types);
22812 sig_type = (struct signatured_type *) per_cu;
22813
22814 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22815
22816 read_signatured_type (sig_type, per_objfile);
22817
22818 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22819 }
22820
22821 /* Read in a signatured type and build its CU and DIEs.
22822 If the type is a stub for the real type in a DWO file,
22823 read in the real type from the DWO file as well. */
22824
22825 static void
22826 read_signatured_type (signatured_type *sig_type,
22827 dwarf2_per_objfile *per_objfile)
22828 {
22829 gdb_assert (sig_type->is_debug_types);
22830 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
22831
22832 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
22833
22834 if (!reader.dummy_p)
22835 {
22836 struct dwarf2_cu *cu = reader.cu;
22837 const gdb_byte *info_ptr = reader.info_ptr;
22838
22839 gdb_assert (cu->die_hash == NULL);
22840 cu->die_hash =
22841 htab_create_alloc_ex (cu->header.length / 12,
22842 die_hash,
22843 die_eq,
22844 NULL,
22845 &cu->comp_unit_obstack,
22846 hashtab_obstack_allocate,
22847 dummy_obstack_deallocate);
22848
22849 if (reader.comp_unit_die->has_children)
22850 reader.comp_unit_die->child
22851 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22852 reader.comp_unit_die);
22853 cu->dies = reader.comp_unit_die;
22854 /* comp_unit_die is not stored in die_hash, no need. */
22855
22856 /* We try not to read any attributes in this function, because
22857 not all CUs needed for references have been loaded yet, and
22858 symbol table processing isn't initialized. But we have to
22859 set the CU language, or we won't be able to build types
22860 correctly. Similarly, if we do not read the producer, we can
22861 not apply producer-specific interpretation. */
22862 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22863
22864 reader.keep ();
22865 }
22866
22867 sig_type->tu_read = 1;
22868 }
22869
22870 /* Decode simple location descriptions.
22871 Given a pointer to a dwarf block that defines a location, compute
22872 the location and return the value. If COMPUTED is non-null, it is
22873 set to true to indicate that decoding was successful, and false
22874 otherwise. If COMPUTED is null, then this function may emit a
22875 complaint. */
22876
22877 static CORE_ADDR
22878 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22879 {
22880 struct objfile *objfile = cu->per_objfile->objfile;
22881 size_t i;
22882 size_t size = blk->size;
22883 const gdb_byte *data = blk->data;
22884 CORE_ADDR stack[64];
22885 int stacki;
22886 unsigned int bytes_read, unsnd;
22887 gdb_byte op;
22888
22889 if (computed != nullptr)
22890 *computed = false;
22891
22892 i = 0;
22893 stacki = 0;
22894 stack[stacki] = 0;
22895 stack[++stacki] = 0;
22896
22897 while (i < size)
22898 {
22899 op = data[i++];
22900 switch (op)
22901 {
22902 case DW_OP_lit0:
22903 case DW_OP_lit1:
22904 case DW_OP_lit2:
22905 case DW_OP_lit3:
22906 case DW_OP_lit4:
22907 case DW_OP_lit5:
22908 case DW_OP_lit6:
22909 case DW_OP_lit7:
22910 case DW_OP_lit8:
22911 case DW_OP_lit9:
22912 case DW_OP_lit10:
22913 case DW_OP_lit11:
22914 case DW_OP_lit12:
22915 case DW_OP_lit13:
22916 case DW_OP_lit14:
22917 case DW_OP_lit15:
22918 case DW_OP_lit16:
22919 case DW_OP_lit17:
22920 case DW_OP_lit18:
22921 case DW_OP_lit19:
22922 case DW_OP_lit20:
22923 case DW_OP_lit21:
22924 case DW_OP_lit22:
22925 case DW_OP_lit23:
22926 case DW_OP_lit24:
22927 case DW_OP_lit25:
22928 case DW_OP_lit26:
22929 case DW_OP_lit27:
22930 case DW_OP_lit28:
22931 case DW_OP_lit29:
22932 case DW_OP_lit30:
22933 case DW_OP_lit31:
22934 stack[++stacki] = op - DW_OP_lit0;
22935 break;
22936
22937 case DW_OP_reg0:
22938 case DW_OP_reg1:
22939 case DW_OP_reg2:
22940 case DW_OP_reg3:
22941 case DW_OP_reg4:
22942 case DW_OP_reg5:
22943 case DW_OP_reg6:
22944 case DW_OP_reg7:
22945 case DW_OP_reg8:
22946 case DW_OP_reg9:
22947 case DW_OP_reg10:
22948 case DW_OP_reg11:
22949 case DW_OP_reg12:
22950 case DW_OP_reg13:
22951 case DW_OP_reg14:
22952 case DW_OP_reg15:
22953 case DW_OP_reg16:
22954 case DW_OP_reg17:
22955 case DW_OP_reg18:
22956 case DW_OP_reg19:
22957 case DW_OP_reg20:
22958 case DW_OP_reg21:
22959 case DW_OP_reg22:
22960 case DW_OP_reg23:
22961 case DW_OP_reg24:
22962 case DW_OP_reg25:
22963 case DW_OP_reg26:
22964 case DW_OP_reg27:
22965 case DW_OP_reg28:
22966 case DW_OP_reg29:
22967 case DW_OP_reg30:
22968 case DW_OP_reg31:
22969 stack[++stacki] = op - DW_OP_reg0;
22970 if (i < size)
22971 {
22972 if (computed == nullptr)
22973 dwarf2_complex_location_expr_complaint ();
22974 else
22975 return 0;
22976 }
22977 break;
22978
22979 case DW_OP_regx:
22980 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22981 i += bytes_read;
22982 stack[++stacki] = unsnd;
22983 if (i < size)
22984 {
22985 if (computed == nullptr)
22986 dwarf2_complex_location_expr_complaint ();
22987 else
22988 return 0;
22989 }
22990 break;
22991
22992 case DW_OP_addr:
22993 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22994 &bytes_read);
22995 i += bytes_read;
22996 break;
22997
22998 case DW_OP_const1u:
22999 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23000 i += 1;
23001 break;
23002
23003 case DW_OP_const1s:
23004 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23005 i += 1;
23006 break;
23007
23008 case DW_OP_const2u:
23009 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23010 i += 2;
23011 break;
23012
23013 case DW_OP_const2s:
23014 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23015 i += 2;
23016 break;
23017
23018 case DW_OP_const4u:
23019 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23020 i += 4;
23021 break;
23022
23023 case DW_OP_const4s:
23024 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23025 i += 4;
23026 break;
23027
23028 case DW_OP_const8u:
23029 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23030 i += 8;
23031 break;
23032
23033 case DW_OP_constu:
23034 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23035 &bytes_read);
23036 i += bytes_read;
23037 break;
23038
23039 case DW_OP_consts:
23040 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23041 i += bytes_read;
23042 break;
23043
23044 case DW_OP_dup:
23045 stack[stacki + 1] = stack[stacki];
23046 stacki++;
23047 break;
23048
23049 case DW_OP_plus:
23050 stack[stacki - 1] += stack[stacki];
23051 stacki--;
23052 break;
23053
23054 case DW_OP_plus_uconst:
23055 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23056 &bytes_read);
23057 i += bytes_read;
23058 break;
23059
23060 case DW_OP_minus:
23061 stack[stacki - 1] -= stack[stacki];
23062 stacki--;
23063 break;
23064
23065 case DW_OP_deref:
23066 /* If we're not the last op, then we definitely can't encode
23067 this using GDB's address_class enum. This is valid for partial
23068 global symbols, although the variable's address will be bogus
23069 in the psymtab. */
23070 if (i < size)
23071 {
23072 if (computed == nullptr)
23073 dwarf2_complex_location_expr_complaint ();
23074 else
23075 return 0;
23076 }
23077 break;
23078
23079 case DW_OP_GNU_push_tls_address:
23080 case DW_OP_form_tls_address:
23081 /* The top of the stack has the offset from the beginning
23082 of the thread control block at which the variable is located. */
23083 /* Nothing should follow this operator, so the top of stack would
23084 be returned. */
23085 /* This is valid for partial global symbols, but the variable's
23086 address will be bogus in the psymtab. Make it always at least
23087 non-zero to not look as a variable garbage collected by linker
23088 which have DW_OP_addr 0. */
23089 if (i < size)
23090 {
23091 if (computed == nullptr)
23092 dwarf2_complex_location_expr_complaint ();
23093 else
23094 return 0;
23095 }
23096 stack[stacki]++;
23097 break;
23098
23099 case DW_OP_GNU_uninit:
23100 if (computed != nullptr)
23101 return 0;
23102 break;
23103
23104 case DW_OP_addrx:
23105 case DW_OP_GNU_addr_index:
23106 case DW_OP_GNU_const_index:
23107 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23108 &bytes_read);
23109 i += bytes_read;
23110 break;
23111
23112 default:
23113 if (computed == nullptr)
23114 {
23115 const char *name = get_DW_OP_name (op);
23116
23117 if (name)
23118 complaint (_("unsupported stack op: '%s'"),
23119 name);
23120 else
23121 complaint (_("unsupported stack op: '%02x'"),
23122 op);
23123 }
23124
23125 return (stack[stacki]);
23126 }
23127
23128 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23129 outside of the allocated space. Also enforce minimum>0. */
23130 if (stacki >= ARRAY_SIZE (stack) - 1)
23131 {
23132 if (computed == nullptr)
23133 complaint (_("location description stack overflow"));
23134 return 0;
23135 }
23136
23137 if (stacki <= 0)
23138 {
23139 if (computed == nullptr)
23140 complaint (_("location description stack underflow"));
23141 return 0;
23142 }
23143 }
23144
23145 if (computed != nullptr)
23146 *computed = true;
23147 return (stack[stacki]);
23148 }
23149
23150 /* memory allocation interface */
23151
23152 static struct dwarf_block *
23153 dwarf_alloc_block (struct dwarf2_cu *cu)
23154 {
23155 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23156 }
23157
23158 static struct die_info *
23159 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23160 {
23161 struct die_info *die;
23162 size_t size = sizeof (struct die_info);
23163
23164 if (num_attrs > 1)
23165 size += (num_attrs - 1) * sizeof (struct attribute);
23166
23167 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23168 memset (die, 0, sizeof (struct die_info));
23169 return (die);
23170 }
23171
23172 \f
23173
23174 /* Macro support. */
23175
23176 /* An overload of dwarf_decode_macros that finds the correct section
23177 and ensures it is read in before calling the other overload. */
23178
23179 static void
23180 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23181 int section_is_gnu)
23182 {
23183 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23184 struct objfile *objfile = per_objfile->objfile;
23185 const struct line_header *lh = cu->line_header;
23186 unsigned int offset_size = cu->header.offset_size;
23187 struct dwarf2_section_info *section;
23188 const char *section_name;
23189
23190 if (cu->dwo_unit != nullptr)
23191 {
23192 if (section_is_gnu)
23193 {
23194 section = &cu->dwo_unit->dwo_file->sections.macro;
23195 section_name = ".debug_macro.dwo";
23196 }
23197 else
23198 {
23199 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23200 section_name = ".debug_macinfo.dwo";
23201 }
23202 }
23203 else
23204 {
23205 if (section_is_gnu)
23206 {
23207 section = &per_objfile->per_bfd->macro;
23208 section_name = ".debug_macro";
23209 }
23210 else
23211 {
23212 section = &per_objfile->per_bfd->macinfo;
23213 section_name = ".debug_macinfo";
23214 }
23215 }
23216
23217 section->read (objfile);
23218 if (section->buffer == nullptr)
23219 {
23220 complaint (_("missing %s section"), section_name);
23221 return;
23222 }
23223
23224 buildsym_compunit *builder = cu->get_builder ();
23225
23226 struct dwarf2_section_info *str_offsets_section;
23227 struct dwarf2_section_info *str_section;
23228 gdb::optional<ULONGEST> str_offsets_base;
23229
23230 if (cu->dwo_unit != nullptr)
23231 {
23232 str_offsets_section = &cu->dwo_unit->dwo_file
23233 ->sections.str_offsets;
23234 str_section = &cu->dwo_unit->dwo_file->sections.str;
23235 str_offsets_base = cu->header.addr_size;
23236 }
23237 else
23238 {
23239 str_offsets_section = &per_objfile->per_bfd->str_offsets;
23240 str_section = &per_objfile->per_bfd->str;
23241 str_offsets_base = cu->str_offsets_base;
23242 }
23243
23244 dwarf_decode_macros (per_objfile, builder, section, lh,
23245 offset_size, offset, str_section, str_offsets_section,
23246 str_offsets_base, section_is_gnu);
23247 }
23248
23249 /* Return the .debug_loc section to use for CU.
23250 For DWO files use .debug_loc.dwo. */
23251
23252 static struct dwarf2_section_info *
23253 cu_debug_loc_section (struct dwarf2_cu *cu)
23254 {
23255 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23256
23257 if (cu->dwo_unit)
23258 {
23259 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23260
23261 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23262 }
23263 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23264 : &per_objfile->per_bfd->loc);
23265 }
23266
23267 /* Return the .debug_rnglists section to use for CU. */
23268 static struct dwarf2_section_info *
23269 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23270 {
23271 if (cu->header.version < 5)
23272 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23273 cu->header.version);
23274 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23275
23276 /* Make sure we read the .debug_rnglists section from the file that
23277 contains the DW_AT_ranges attribute we are reading. Normally that
23278 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23279 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23280 program. */
23281 if (cu->dwo_unit != nullptr
23282 && tag != DW_TAG_compile_unit
23283 && tag != DW_TAG_skeleton_unit)
23284 {
23285 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23286
23287 if (sections->rnglists.size > 0)
23288 return &sections->rnglists;
23289 else
23290 error (_(".debug_rnglists section is missing from .dwo file."));
23291 }
23292 return &dwarf2_per_objfile->per_bfd->rnglists;
23293 }
23294
23295 /* A helper function that fills in a dwarf2_loclist_baton. */
23296
23297 static void
23298 fill_in_loclist_baton (struct dwarf2_cu *cu,
23299 struct dwarf2_loclist_baton *baton,
23300 const struct attribute *attr)
23301 {
23302 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23303 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23304
23305 section->read (per_objfile->objfile);
23306
23307 baton->per_objfile = per_objfile;
23308 baton->per_cu = cu->per_cu;
23309 gdb_assert (baton->per_cu);
23310 /* We don't know how long the location list is, but make sure we
23311 don't run off the edge of the section. */
23312 baton->size = section->size - attr->as_unsigned ();
23313 baton->data = section->buffer + attr->as_unsigned ();
23314 if (cu->base_address.has_value ())
23315 baton->base_address = *cu->base_address;
23316 else
23317 baton->base_address = 0;
23318 baton->from_dwo = cu->dwo_unit != NULL;
23319 }
23320
23321 static void
23322 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23323 struct dwarf2_cu *cu, int is_block)
23324 {
23325 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23326 struct objfile *objfile = per_objfile->objfile;
23327 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23328
23329 if (attr->form_is_section_offset ()
23330 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23331 the section. If so, fall through to the complaint in the
23332 other branch. */
23333 && attr->as_unsigned () < section->get_size (objfile))
23334 {
23335 struct dwarf2_loclist_baton *baton;
23336
23337 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23338
23339 fill_in_loclist_baton (cu, baton, attr);
23340
23341 if (!cu->base_address.has_value ())
23342 complaint (_("Location list used without "
23343 "specifying the CU base address."));
23344
23345 sym->set_aclass_index ((is_block
23346 ? dwarf2_loclist_block_index
23347 : dwarf2_loclist_index));
23348 SYMBOL_LOCATION_BATON (sym) = baton;
23349 }
23350 else
23351 {
23352 struct dwarf2_locexpr_baton *baton;
23353
23354 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23355 baton->per_objfile = per_objfile;
23356 baton->per_cu = cu->per_cu;
23357 gdb_assert (baton->per_cu);
23358
23359 if (attr->form_is_block ())
23360 {
23361 /* Note that we're just copying the block's data pointer
23362 here, not the actual data. We're still pointing into the
23363 info_buffer for SYM's objfile; right now we never release
23364 that buffer, but when we do clean up properly this may
23365 need to change. */
23366 struct dwarf_block *block = attr->as_block ();
23367 baton->size = block->size;
23368 baton->data = block->data;
23369 }
23370 else
23371 {
23372 dwarf2_invalid_attrib_class_complaint ("location description",
23373 sym->natural_name ());
23374 baton->size = 0;
23375 }
23376
23377 sym->set_aclass_index ((is_block
23378 ? dwarf2_locexpr_block_index
23379 : dwarf2_locexpr_index));
23380 SYMBOL_LOCATION_BATON (sym) = baton;
23381 }
23382 }
23383
23384 /* See read.h. */
23385
23386 const comp_unit_head *
23387 dwarf2_per_cu_data::get_header () const
23388 {
23389 if (!m_header_read_in)
23390 {
23391 const gdb_byte *info_ptr
23392 = this->section->buffer + to_underlying (this->sect_off);
23393
23394 memset (&m_header, 0, sizeof (m_header));
23395
23396 read_comp_unit_head (&m_header, info_ptr, this->section,
23397 rcuh_kind::COMPILE);
23398
23399 m_header_read_in = true;
23400 }
23401
23402 return &m_header;
23403 }
23404
23405 /* See read.h. */
23406
23407 int
23408 dwarf2_per_cu_data::addr_size () const
23409 {
23410 return this->get_header ()->addr_size;
23411 }
23412
23413 /* See read.h. */
23414
23415 int
23416 dwarf2_per_cu_data::offset_size () const
23417 {
23418 return this->get_header ()->offset_size;
23419 }
23420
23421 /* See read.h. */
23422
23423 int
23424 dwarf2_per_cu_data::ref_addr_size () const
23425 {
23426 const comp_unit_head *header = this->get_header ();
23427
23428 if (header->version == 2)
23429 return header->addr_size;
23430 else
23431 return header->offset_size;
23432 }
23433
23434 /* A helper function for dwarf2_find_containing_comp_unit that returns
23435 the index of the result, and that searches a vector. It will
23436 return a result even if the offset in question does not actually
23437 occur in any CU. This is separate so that it can be unit
23438 tested. */
23439
23440 static int
23441 dwarf2_find_containing_comp_unit
23442 (sect_offset sect_off,
23443 unsigned int offset_in_dwz,
23444 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
23445 {
23446 int low, high;
23447
23448 low = 0;
23449 high = all_comp_units.size () - 1;
23450 while (high > low)
23451 {
23452 struct dwarf2_per_cu_data *mid_cu;
23453 int mid = low + (high - low) / 2;
23454
23455 mid_cu = all_comp_units[mid].get ();
23456 if (mid_cu->is_dwz > offset_in_dwz
23457 || (mid_cu->is_dwz == offset_in_dwz
23458 && mid_cu->sect_off + mid_cu->length () > sect_off))
23459 high = mid;
23460 else
23461 low = mid + 1;
23462 }
23463 gdb_assert (low == high);
23464 return low;
23465 }
23466
23467 /* Locate the .debug_info compilation unit from CU's objfile which contains
23468 the DIE at OFFSET. Raises an error on failure. */
23469
23470 static struct dwarf2_per_cu_data *
23471 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23472 unsigned int offset_in_dwz,
23473 dwarf2_per_bfd *per_bfd)
23474 {
23475 int low = dwarf2_find_containing_comp_unit
23476 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
23477 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
23478
23479 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23480 {
23481 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23482 error (_("Dwarf Error: could not find partial DIE containing "
23483 "offset %s [in module %s]"),
23484 sect_offset_str (sect_off),
23485 bfd_get_filename (per_bfd->obfd));
23486
23487 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
23488 <= sect_off);
23489 return per_bfd->all_comp_units[low - 1].get ();
23490 }
23491 else
23492 {
23493 if (low == per_bfd->all_comp_units.size () - 1
23494 && sect_off >= this_cu->sect_off + this_cu->length ())
23495 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23496 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
23497 return this_cu;
23498 }
23499 }
23500
23501 #if GDB_SELF_TEST
23502
23503 namespace selftests {
23504 namespace find_containing_comp_unit {
23505
23506 static void
23507 run_test ()
23508 {
23509 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
23510 dwarf2_per_cu_data *one_ptr = one.get ();
23511 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
23512 dwarf2_per_cu_data *two_ptr = two.get ();
23513 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
23514 dwarf2_per_cu_data *three_ptr = three.get ();
23515 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
23516 dwarf2_per_cu_data *four_ptr = four.get ();
23517
23518 one->set_length (5);
23519 two->sect_off = sect_offset (one->length ());
23520 two->set_length (7);
23521
23522 three->set_length (5);
23523 three->is_dwz = 1;
23524 four->sect_off = sect_offset (three->length ());
23525 four->set_length (7);
23526 four->is_dwz = 1;
23527
23528 std::vector<dwarf2_per_cu_data_up> units;
23529 units.push_back (std::move (one));
23530 units.push_back (std::move (two));
23531 units.push_back (std::move (three));
23532 units.push_back (std::move (four));
23533
23534 int result;
23535
23536 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23537 SELF_CHECK (units[result].get () == one_ptr);
23538 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23539 SELF_CHECK (units[result].get () == one_ptr);
23540 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23541 SELF_CHECK (units[result].get () == two_ptr);
23542
23543 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23544 SELF_CHECK (units[result].get () == three_ptr);
23545 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23546 SELF_CHECK (units[result].get () == three_ptr);
23547 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23548 SELF_CHECK (units[result].get () == four_ptr);
23549 }
23550
23551 }
23552 }
23553
23554 #endif /* GDB_SELF_TEST */
23555
23556 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23557
23558 static void
23559 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23560 enum language pretend_language)
23561 {
23562 struct attribute *attr;
23563
23564 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23565
23566 /* Set the language we're debugging. */
23567 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23568 enum language lang;
23569 if (cu->producer != nullptr
23570 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
23571 {
23572 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23573 attribute is not standardised yet. As a workaround for the
23574 language detection we fall back to the DW_AT_producer
23575 string. */
23576 lang = language_opencl;
23577 }
23578 else if (cu->producer != nullptr
23579 && strstr (cu->producer, "GNU Go ") != NULL)
23580 {
23581 /* Similar hack for Go. */
23582 lang = language_go;
23583 }
23584 else if (attr != nullptr)
23585 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
23586 else
23587 lang = pretend_language;
23588
23589 cu->language_defn = language_def (lang);
23590
23591 switch (comp_unit_die->tag)
23592 {
23593 case DW_TAG_compile_unit:
23594 cu->per_cu->set_unit_type (DW_UT_compile);
23595 break;
23596 case DW_TAG_partial_unit:
23597 cu->per_cu->set_unit_type (DW_UT_partial);
23598 break;
23599 case DW_TAG_type_unit:
23600 cu->per_cu->set_unit_type (DW_UT_type);
23601 break;
23602 default:
23603 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
23604 dwarf_tag_name (comp_unit_die->tag),
23605 sect_offset_str (cu->per_cu->sect_off));
23606 }
23607
23608 cu->per_cu->set_lang (lang);
23609 }
23610
23611 /* See read.h. */
23612
23613 dwarf2_cu *
23614 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23615 {
23616 auto it = m_dwarf2_cus.find (per_cu);
23617 if (it == m_dwarf2_cus.end ())
23618 return nullptr;
23619
23620 return it->second.get ();
23621 }
23622
23623 /* See read.h. */
23624
23625 void
23626 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
23627 std::unique_ptr<dwarf2_cu> cu)
23628 {
23629 gdb_assert (this->get_cu (per_cu) == nullptr);
23630
23631 m_dwarf2_cus[per_cu] = std::move (cu);
23632 }
23633
23634 /* See read.h. */
23635
23636 void
23637 dwarf2_per_objfile::age_comp_units ()
23638 {
23639 dwarf_read_debug_printf_v ("running");
23640
23641 /* This is not expected to be called in the middle of CU expansion. There is
23642 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23643 loaded in memory. Calling age_comp_units while the queue is in use could
23644 make us free the DIEs for a CU that is in the queue and therefore break
23645 that invariant. */
23646 gdb_assert (!queue.has_value ());
23647
23648 /* Start by clearing all marks. */
23649 for (const auto &pair : m_dwarf2_cus)
23650 pair.second->clear_mark ();
23651
23652 /* Traverse all CUs, mark them and their dependencies if used recently
23653 enough. */
23654 for (const auto &pair : m_dwarf2_cus)
23655 {
23656 dwarf2_cu *cu = pair.second.get ();
23657
23658 cu->last_used++;
23659 if (cu->last_used <= dwarf_max_cache_age)
23660 cu->mark ();
23661 }
23662
23663 /* Delete all CUs still not marked. */
23664 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23665 {
23666 dwarf2_cu *cu = it->second.get ();
23667
23668 if (!cu->is_marked ())
23669 {
23670 dwarf_read_debug_printf_v ("deleting old CU %s",
23671 sect_offset_str (cu->per_cu->sect_off));
23672 it = m_dwarf2_cus.erase (it);
23673 }
23674 else
23675 it++;
23676 }
23677 }
23678
23679 /* See read.h. */
23680
23681 void
23682 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23683 {
23684 auto it = m_dwarf2_cus.find (per_cu);
23685 if (it == m_dwarf2_cus.end ())
23686 return;
23687
23688 m_dwarf2_cus.erase (it);
23689 }
23690
23691 dwarf2_per_objfile::~dwarf2_per_objfile ()
23692 {
23693 remove_all_cus ();
23694 }
23695
23696 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23697 We store these in a hash table separate from the DIEs, and preserve them
23698 when the DIEs are flushed out of cache.
23699
23700 The CU "per_cu" pointer is needed because offset alone is not enough to
23701 uniquely identify the type. A file may have multiple .debug_types sections,
23702 or the type may come from a DWO file. Furthermore, while it's more logical
23703 to use per_cu->section+offset, with Fission the section with the data is in
23704 the DWO file but we don't know that section at the point we need it.
23705 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23706 because we can enter the lookup routine, get_die_type_at_offset, from
23707 outside this file, and thus won't necessarily have PER_CU->cu.
23708 Fortunately, PER_CU is stable for the life of the objfile. */
23709
23710 struct dwarf2_per_cu_offset_and_type
23711 {
23712 const struct dwarf2_per_cu_data *per_cu;
23713 sect_offset sect_off;
23714 struct type *type;
23715 };
23716
23717 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23718
23719 static hashval_t
23720 per_cu_offset_and_type_hash (const void *item)
23721 {
23722 const struct dwarf2_per_cu_offset_and_type *ofs
23723 = (const struct dwarf2_per_cu_offset_and_type *) item;
23724
23725 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23726 }
23727
23728 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23729
23730 static int
23731 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23732 {
23733 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23734 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23735 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23736 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23737
23738 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23739 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23740 }
23741
23742 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23743 table if necessary. For convenience, return TYPE.
23744
23745 The DIEs reading must have careful ordering to:
23746 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23747 reading current DIE.
23748 * Not trying to dereference contents of still incompletely read in types
23749 while reading in other DIEs.
23750 * Enable referencing still incompletely read in types just by a pointer to
23751 the type without accessing its fields.
23752
23753 Therefore caller should follow these rules:
23754 * Try to fetch any prerequisite types we may need to build this DIE type
23755 before building the type and calling set_die_type.
23756 * After building type call set_die_type for current DIE as soon as
23757 possible before fetching more types to complete the current type.
23758 * Make the type as complete as possible before fetching more types. */
23759
23760 static struct type *
23761 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
23762 bool skip_data_location)
23763 {
23764 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23765 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23766 struct objfile *objfile = per_objfile->objfile;
23767 struct attribute *attr;
23768 struct dynamic_prop prop;
23769
23770 /* For Ada types, make sure that the gnat-specific data is always
23771 initialized (if not already set). There are a few types where
23772 we should not be doing so, because the type-specific area is
23773 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23774 where the type-specific area is used to store the floatformat).
23775 But this is not a problem, because the gnat-specific information
23776 is actually not needed for these types. */
23777 if (need_gnat_info (cu)
23778 && type->code () != TYPE_CODE_FUNC
23779 && type->code () != TYPE_CODE_FLT
23780 && type->code () != TYPE_CODE_METHODPTR
23781 && type->code () != TYPE_CODE_MEMBERPTR
23782 && type->code () != TYPE_CODE_METHOD
23783 && type->code () != TYPE_CODE_FIXED_POINT
23784 && !HAVE_GNAT_AUX_INFO (type))
23785 INIT_GNAT_SPECIFIC (type);
23786
23787 /* Read DW_AT_allocated and set in type. */
23788 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23789 if (attr != NULL)
23790 {
23791 struct type *prop_type = cu->addr_sized_int_type (false);
23792 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23793 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23794 }
23795
23796 /* Read DW_AT_associated and set in type. */
23797 attr = dwarf2_attr (die, DW_AT_associated, cu);
23798 if (attr != NULL)
23799 {
23800 struct type *prop_type = cu->addr_sized_int_type (false);
23801 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23802 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23803 }
23804
23805 /* Read DW_AT_rank and set in type. */
23806 attr = dwarf2_attr (die, DW_AT_rank, cu);
23807 if (attr != NULL)
23808 {
23809 struct type *prop_type = cu->addr_sized_int_type (false);
23810 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23811 type->add_dyn_prop (DYN_PROP_RANK, prop);
23812 }
23813
23814 /* Read DW_AT_data_location and set in type. */
23815 if (!skip_data_location)
23816 {
23817 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23818 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23819 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23820 }
23821
23822 if (per_objfile->die_type_hash == NULL)
23823 per_objfile->die_type_hash
23824 = htab_up (htab_create_alloc (127,
23825 per_cu_offset_and_type_hash,
23826 per_cu_offset_and_type_eq,
23827 NULL, xcalloc, xfree));
23828
23829 ofs.per_cu = cu->per_cu;
23830 ofs.sect_off = die->sect_off;
23831 ofs.type = type;
23832 slot = (struct dwarf2_per_cu_offset_and_type **)
23833 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23834 if (*slot)
23835 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23836 sect_offset_str (die->sect_off));
23837 *slot = XOBNEW (&objfile->objfile_obstack,
23838 struct dwarf2_per_cu_offset_and_type);
23839 **slot = ofs;
23840 return type;
23841 }
23842
23843 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23844 or return NULL if the die does not have a saved type. */
23845
23846 static struct type *
23847 get_die_type_at_offset (sect_offset sect_off,
23848 dwarf2_per_cu_data *per_cu,
23849 dwarf2_per_objfile *per_objfile)
23850 {
23851 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23852
23853 if (per_objfile->die_type_hash == NULL)
23854 return NULL;
23855
23856 ofs.per_cu = per_cu;
23857 ofs.sect_off = sect_off;
23858 slot = ((struct dwarf2_per_cu_offset_and_type *)
23859 htab_find (per_objfile->die_type_hash.get (), &ofs));
23860 if (slot)
23861 return slot->type;
23862 else
23863 return NULL;
23864 }
23865
23866 /* Look up the type for DIE in CU in die_type_hash,
23867 or return NULL if DIE does not have a saved type. */
23868
23869 static struct type *
23870 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23871 {
23872 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23873 }
23874
23875 struct cmd_list_element *set_dwarf_cmdlist;
23876 struct cmd_list_element *show_dwarf_cmdlist;
23877
23878 static void
23879 show_check_physname (struct ui_file *file, int from_tty,
23880 struct cmd_list_element *c, const char *value)
23881 {
23882 gdb_printf (file,
23883 _("Whether to check \"physname\" is %s.\n"),
23884 value);
23885 }
23886
23887 void _initialize_dwarf2_read ();
23888 void
23889 _initialize_dwarf2_read ()
23890 {
23891 add_setshow_prefix_cmd ("dwarf", class_maintenance,
23892 _("\
23893 Set DWARF specific variables.\n\
23894 Configure DWARF variables such as the cache size."),
23895 _("\
23896 Show DWARF specific variables.\n\
23897 Show DWARF variables such as the cache size."),
23898 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
23899 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
23900
23901 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23902 &dwarf_max_cache_age, _("\
23903 Set the upper bound on the age of cached DWARF compilation units."), _("\
23904 Show the upper bound on the age of cached DWARF compilation units."), _("\
23905 A higher limit means that cached compilation units will be stored\n\
23906 in memory longer, and more total memory will be used. Zero disables\n\
23907 caching, which can slow down startup."),
23908 NULL,
23909 show_dwarf_max_cache_age,
23910 &set_dwarf_cmdlist,
23911 &show_dwarf_cmdlist);
23912
23913 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23914 Set debugging of the DWARF reader."), _("\
23915 Show debugging of the DWARF reader."), _("\
23916 When enabled (non-zero), debugging messages are printed during DWARF\n\
23917 reading and symtab expansion. A value of 1 (one) provides basic\n\
23918 information. A value greater than 1 provides more verbose information."),
23919 NULL,
23920 NULL,
23921 &setdebuglist, &showdebuglist);
23922
23923 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23924 Set debugging of the DWARF DIE reader."), _("\
23925 Show debugging of the DWARF DIE reader."), _("\
23926 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23927 The value is the maximum depth to print."),
23928 NULL,
23929 NULL,
23930 &setdebuglist, &showdebuglist);
23931
23932 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23933 Set debugging of the dwarf line reader."), _("\
23934 Show debugging of the dwarf line reader."), _("\
23935 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23936 A value of 1 (one) provides basic information.\n\
23937 A value greater than 1 provides more verbose information."),
23938 NULL,
23939 NULL,
23940 &setdebuglist, &showdebuglist);
23941
23942 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23943 Set cross-checking of \"physname\" code against demangler."), _("\
23944 Show cross-checking of \"physname\" code against demangler."), _("\
23945 When enabled, GDB's internal \"physname\" code is checked against\n\
23946 the demangler."),
23947 NULL, show_check_physname,
23948 &setdebuglist, &showdebuglist);
23949
23950 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23951 no_class, &use_deprecated_index_sections, _("\
23952 Set whether to use deprecated gdb_index sections."), _("\
23953 Show whether to use deprecated gdb_index sections."), _("\
23954 When enabled, deprecated .gdb_index sections are used anyway.\n\
23955 Normally they are ignored either because of a missing feature or\n\
23956 performance issue.\n\
23957 Warning: This option must be enabled before gdb reads the file."),
23958 NULL,
23959 NULL,
23960 &setlist, &showlist);
23961
23962 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23963 &dwarf2_locexpr_funcs);
23964 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23965 &dwarf2_loclist_funcs);
23966
23967 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23968 &dwarf2_block_frame_base_locexpr_funcs);
23969 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23970 &dwarf2_block_frame_base_loclist_funcs);
23971
23972 #if GDB_SELF_TEST
23973 selftests::register_test ("dw2_expand_symtabs_matching",
23974 selftests::dw2_expand_symtabs_matching::run_test);
23975 selftests::register_test ("dwarf2_find_containing_comp_unit",
23976 selftests::find_containing_comp_unit::run_test);
23977 #endif
23978 }