]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
gdbsupport: add path_join function
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "c-lang.h"
68 #include "go-lang.h"
69 #include "valprint.h"
70 #include "gdbcore.h" /* for gnutarget */
71 #include "gdb/gdb-index.h"
72 #include "gdb_bfd.h"
73 #include "f-lang.h"
74 #include "source.h"
75 #include "build-id.h"
76 #include "namespace.h"
77 #include "gdbsupport/function-view.h"
78 #include "gdbsupport/gdb_optional.h"
79 #include "gdbsupport/underlying.h"
80 #include "gdbsupport/hash_enum.h"
81 #include "filename-seen-cache.h"
82 #include "producer.h"
83 #include <fcntl.h>
84 #include <algorithm>
85 #include <unordered_map>
86 #include "gdbsupport/selftest.h"
87 #include "rust-lang.h"
88 #include "gdbsupport/pathstuff.h"
89 #include "count-one-bits.h"
90 #include <unordered_set>
91 #include "dwarf2/abbrev-cache.h"
92 #include "cooked-index.h"
93 #include "split-name.h"
94 #include "gdbsupport/parallel-for.h"
95 #include "gdbsupport/thread-pool.h"
96
97 /* When == 1, print basic high level tracing messages.
98 When > 1, be more verbose.
99 This is in contrast to the low level DIE reading of dwarf_die_debug. */
100 static unsigned int dwarf_read_debug = 0;
101
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
103
104 #define dwarf_read_debug_printf(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
107
108 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
109
110 #define dwarf_read_debug_printf_v(fmt, ...) \
111 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
112 ##__VA_ARGS__)
113
114 /* When non-zero, dump DIEs after they are read in. */
115 static unsigned int dwarf_die_debug = 0;
116
117 /* When non-zero, dump line number entries as they are read in. */
118 unsigned int dwarf_line_debug = 0;
119
120 /* When true, cross-check physname against demangler. */
121 static bool check_physname = false;
122
123 /* When true, do not reject deprecated .gdb_index sections. */
124 static bool use_deprecated_index_sections = false;
125
126 /* This is used to store the data that is always per objfile. */
127 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
128
129 /* These are used to store the dwarf2_per_bfd objects.
130
131 objfiles having the same BFD, which doesn't require relocations, are going to
132 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
133
134 Other objfiles are not going to share a dwarf2_per_bfd with any other
135 objfiles, so they'll have their own version kept in the _objfile_data_key
136 version. */
137 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
138 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
139
140 /* The "aclass" indices for various kinds of computed DWARF symbols. */
141
142 static int dwarf2_locexpr_index;
143 static int dwarf2_loclist_index;
144 static int dwarf2_locexpr_block_index;
145 static int dwarf2_loclist_block_index;
146
147 /* Size of .debug_loclists section header for 32-bit DWARF format. */
148 #define LOCLIST_HEADER_SIZE32 12
149
150 /* Size of .debug_loclists section header for 64-bit DWARF format. */
151 #define LOCLIST_HEADER_SIZE64 20
152
153 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
154 #define RNGLIST_HEADER_SIZE32 12
155
156 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
157 #define RNGLIST_HEADER_SIZE64 20
158
159 /* This is a view into the index that converts from bytes to an
160 offset_type, and allows indexing. Unaligned bytes are specifically
161 allowed here, and handled via unpacking. */
162
163 class offset_view
164 {
165 public:
166 offset_view () = default;
167
168 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
169 : m_bytes (bytes)
170 {
171 }
172
173 /* Extract the INDEXth offset_type from the array. */
174 offset_type operator[] (size_t index) const
175 {
176 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
177 return (offset_type) extract_unsigned_integer (bytes,
178 sizeof (offset_type),
179 BFD_ENDIAN_LITTLE);
180 }
181
182 /* Return the number of offset_types in this array. */
183 size_t size () const
184 {
185 return m_bytes.size () / sizeof (offset_type);
186 }
187
188 /* Return true if this view is empty. */
189 bool empty () const
190 {
191 return m_bytes.empty ();
192 }
193
194 private:
195 /* The underlying bytes. */
196 gdb::array_view<const gdb_byte> m_bytes;
197 };
198
199 /* A description of the mapped index. The file format is described in
200 a comment by the code that writes the index. */
201 struct mapped_index final : public mapped_index_base
202 {
203 /* Index data format version. */
204 int version = 0;
205
206 /* The address table data. */
207 gdb::array_view<const gdb_byte> address_table;
208
209 /* The symbol table, implemented as a hash table. */
210 offset_view symbol_table;
211
212 /* A pointer to the constant pool. */
213 gdb::array_view<const gdb_byte> constant_pool;
214
215 /* Return the index into the constant pool of the name of the IDXth
216 symbol in the symbol table. */
217 offset_type symbol_name_index (offset_type idx) const
218 {
219 return symbol_table[2 * idx];
220 }
221
222 /* Return the index into the constant pool of the CU vector of the
223 IDXth symbol in the symbol table. */
224 offset_type symbol_vec_index (offset_type idx) const
225 {
226 return symbol_table[2 * idx + 1];
227 }
228
229 bool symbol_name_slot_invalid (offset_type idx) const override
230 {
231 return (symbol_name_index (idx) == 0
232 && symbol_vec_index (idx) == 0);
233 }
234
235 /* Convenience method to get at the name of the symbol at IDX in the
236 symbol table. */
237 const char *symbol_name_at
238 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
239 {
240 return (const char *) (this->constant_pool.data ()
241 + symbol_name_index (idx));
242 }
243
244 size_t symbol_name_count () const override
245 { return this->symbol_table.size () / 2; }
246
247 quick_symbol_functions_up make_quick_functions () const override;
248
249 bool version_check () const override
250 {
251 return version >= 8;
252 }
253 };
254
255 /* A description of the mapped .debug_names.
256 Uninitialized map has CU_COUNT 0. */
257 struct mapped_debug_names final : public mapped_index_base
258 {
259 bfd_endian dwarf5_byte_order;
260 bool dwarf5_is_dwarf64;
261 bool augmentation_is_gdb;
262 uint8_t offset_size;
263 uint32_t cu_count = 0;
264 uint32_t tu_count, bucket_count, name_count;
265 const gdb_byte *cu_table_reordered, *tu_table_reordered;
266 const uint32_t *bucket_table_reordered, *hash_table_reordered;
267 const gdb_byte *name_table_string_offs_reordered;
268 const gdb_byte *name_table_entry_offs_reordered;
269 const gdb_byte *entry_pool;
270
271 struct index_val
272 {
273 ULONGEST dwarf_tag;
274 struct attr
275 {
276 /* Attribute name DW_IDX_*. */
277 ULONGEST dw_idx;
278
279 /* Attribute form DW_FORM_*. */
280 ULONGEST form;
281
282 /* Value if FORM is DW_FORM_implicit_const. */
283 LONGEST implicit_const;
284 };
285 std::vector<attr> attr_vec;
286 };
287
288 std::unordered_map<ULONGEST, index_val> abbrev_map;
289
290 const char *namei_to_name
291 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
292
293 /* Implementation of the mapped_index_base virtual interface, for
294 the name_components cache. */
295
296 const char *symbol_name_at
297 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
298 { return namei_to_name (idx, per_objfile); }
299
300 size_t symbol_name_count () const override
301 { return this->name_count; }
302
303 quick_symbol_functions_up make_quick_functions () const override;
304 };
305
306 /* See dwarf2/read.h. */
307
308 dwarf2_per_objfile *
309 get_dwarf2_per_objfile (struct objfile *objfile)
310 {
311 return dwarf2_objfile_data_key.get (objfile);
312 }
313
314 /* Default names of the debugging sections. */
315
316 /* Note that if the debugging section has been compressed, it might
317 have a name like .zdebug_info. */
318
319 const struct dwarf2_debug_sections dwarf2_elf_names =
320 {
321 { ".debug_info", ".zdebug_info" },
322 { ".debug_abbrev", ".zdebug_abbrev" },
323 { ".debug_line", ".zdebug_line" },
324 { ".debug_loc", ".zdebug_loc" },
325 { ".debug_loclists", ".zdebug_loclists" },
326 { ".debug_macinfo", ".zdebug_macinfo" },
327 { ".debug_macro", ".zdebug_macro" },
328 { ".debug_str", ".zdebug_str" },
329 { ".debug_str_offsets", ".zdebug_str_offsets" },
330 { ".debug_line_str", ".zdebug_line_str" },
331 { ".debug_ranges", ".zdebug_ranges" },
332 { ".debug_rnglists", ".zdebug_rnglists" },
333 { ".debug_types", ".zdebug_types" },
334 { ".debug_addr", ".zdebug_addr" },
335 { ".debug_frame", ".zdebug_frame" },
336 { ".eh_frame", NULL },
337 { ".gdb_index", ".zgdb_index" },
338 { ".debug_names", ".zdebug_names" },
339 { ".debug_aranges", ".zdebug_aranges" },
340 23
341 };
342
343 /* List of DWO/DWP sections. */
344
345 static const struct dwop_section_names
346 {
347 struct dwarf2_section_names abbrev_dwo;
348 struct dwarf2_section_names info_dwo;
349 struct dwarf2_section_names line_dwo;
350 struct dwarf2_section_names loc_dwo;
351 struct dwarf2_section_names loclists_dwo;
352 struct dwarf2_section_names macinfo_dwo;
353 struct dwarf2_section_names macro_dwo;
354 struct dwarf2_section_names rnglists_dwo;
355 struct dwarf2_section_names str_dwo;
356 struct dwarf2_section_names str_offsets_dwo;
357 struct dwarf2_section_names types_dwo;
358 struct dwarf2_section_names cu_index;
359 struct dwarf2_section_names tu_index;
360 }
361 dwop_section_names =
362 {
363 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
364 { ".debug_info.dwo", ".zdebug_info.dwo" },
365 { ".debug_line.dwo", ".zdebug_line.dwo" },
366 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
367 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
368 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
369 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
370 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
371 { ".debug_str.dwo", ".zdebug_str.dwo" },
372 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
373 { ".debug_types.dwo", ".zdebug_types.dwo" },
374 { ".debug_cu_index", ".zdebug_cu_index" },
375 { ".debug_tu_index", ".zdebug_tu_index" },
376 };
377
378 /* local data types */
379
380 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
381 begin with a header, which contains the following information. */
382 struct loclists_rnglists_header
383 {
384 /* A 4-byte or 12-byte length containing the length of the
385 set of entries for this compilation unit, not including the
386 length field itself. */
387 unsigned int length;
388
389 /* A 2-byte version identifier. */
390 short version;
391
392 /* A 1-byte unsigned integer containing the size in bytes of an address on
393 the target system. */
394 unsigned char addr_size;
395
396 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
397 on the target system. */
398 unsigned char segment_collector_size;
399
400 /* A 4-byte count of the number of offsets that follow the header. */
401 unsigned int offset_entry_count;
402 };
403
404 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
405 This includes type_unit_group and quick_file_names. */
406
407 struct stmt_list_hash
408 {
409 /* The DWO unit this table is from or NULL if there is none. */
410 struct dwo_unit *dwo_unit;
411
412 /* Offset in .debug_line or .debug_line.dwo. */
413 sect_offset line_sect_off;
414 };
415
416 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
417 an object of this type. This contains elements of type unit groups
418 that can be shared across objfiles. The non-shareable parts are in
419 type_unit_group_unshareable. */
420
421 struct type_unit_group
422 {
423 /* The data used to construct the hash key. */
424 struct stmt_list_hash hash {};
425 };
426
427 /* These sections are what may appear in a (real or virtual) DWO file. */
428
429 struct dwo_sections
430 {
431 struct dwarf2_section_info abbrev;
432 struct dwarf2_section_info line;
433 struct dwarf2_section_info loc;
434 struct dwarf2_section_info loclists;
435 struct dwarf2_section_info macinfo;
436 struct dwarf2_section_info macro;
437 struct dwarf2_section_info rnglists;
438 struct dwarf2_section_info str;
439 struct dwarf2_section_info str_offsets;
440 /* In the case of a virtual DWO file, these two are unused. */
441 struct dwarf2_section_info info;
442 std::vector<dwarf2_section_info> types;
443 };
444
445 /* CUs/TUs in DWP/DWO files. */
446
447 struct dwo_unit
448 {
449 /* Backlink to the containing struct dwo_file. */
450 struct dwo_file *dwo_file;
451
452 /* The "id" that distinguishes this CU/TU.
453 .debug_info calls this "dwo_id", .debug_types calls this "signature".
454 Since signatures came first, we stick with it for consistency. */
455 ULONGEST signature;
456
457 /* The section this CU/TU lives in, in the DWO file. */
458 struct dwarf2_section_info *section;
459
460 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
461 sect_offset sect_off;
462 unsigned int length;
463
464 /* For types, offset in the type's DIE of the type defined by this TU. */
465 cu_offset type_offset_in_tu;
466 };
467
468 /* include/dwarf2.h defines the DWP section codes.
469 It defines a max value but it doesn't define a min value, which we
470 use for error checking, so provide one. */
471
472 enum dwp_v2_section_ids
473 {
474 DW_SECT_MIN = 1
475 };
476
477 /* Data for one DWO file.
478
479 This includes virtual DWO files (a virtual DWO file is a DWO file as it
480 appears in a DWP file). DWP files don't really have DWO files per se -
481 comdat folding of types "loses" the DWO file they came from, and from
482 a high level view DWP files appear to contain a mass of random types.
483 However, to maintain consistency with the non-DWP case we pretend DWP
484 files contain virtual DWO files, and we assign each TU with one virtual
485 DWO file (generally based on the line and abbrev section offsets -
486 a heuristic that seems to work in practice). */
487
488 struct dwo_file
489 {
490 dwo_file () = default;
491 DISABLE_COPY_AND_ASSIGN (dwo_file);
492
493 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
494 For virtual DWO files the name is constructed from the section offsets
495 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
496 from related CU+TUs. */
497 const char *dwo_name = nullptr;
498
499 /* The DW_AT_comp_dir attribute. */
500 const char *comp_dir = nullptr;
501
502 /* The bfd, when the file is open. Otherwise this is NULL.
503 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
504 gdb_bfd_ref_ptr dbfd;
505
506 /* The sections that make up this DWO file.
507 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
508 sections (for lack of a better name). */
509 struct dwo_sections sections {};
510
511 /* The CUs in the file.
512 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
513 an extension to handle LLVM's Link Time Optimization output (where
514 multiple source files may be compiled into a single object/dwo pair). */
515 htab_up cus;
516
517 /* Table of TUs in the file.
518 Each element is a struct dwo_unit. */
519 htab_up tus;
520 };
521
522 /* These sections are what may appear in a DWP file. */
523
524 struct dwp_sections
525 {
526 /* These are used by all DWP versions (1, 2 and 5). */
527 struct dwarf2_section_info str;
528 struct dwarf2_section_info cu_index;
529 struct dwarf2_section_info tu_index;
530
531 /* These are only used by DWP version 2 and version 5 files.
532 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
533 sections are referenced by section number, and are not recorded here.
534 In DWP version 2 or 5 there is at most one copy of all these sections,
535 each section being (effectively) comprised of the concatenation of all of
536 the individual sections that exist in the version 1 format.
537 To keep the code simple we treat each of these concatenated pieces as a
538 section itself (a virtual section?). */
539 struct dwarf2_section_info abbrev;
540 struct dwarf2_section_info info;
541 struct dwarf2_section_info line;
542 struct dwarf2_section_info loc;
543 struct dwarf2_section_info loclists;
544 struct dwarf2_section_info macinfo;
545 struct dwarf2_section_info macro;
546 struct dwarf2_section_info rnglists;
547 struct dwarf2_section_info str_offsets;
548 struct dwarf2_section_info types;
549 };
550
551 /* These sections are what may appear in a virtual DWO file in DWP version 1.
552 A virtual DWO file is a DWO file as it appears in a DWP file. */
553
554 struct virtual_v1_dwo_sections
555 {
556 struct dwarf2_section_info abbrev;
557 struct dwarf2_section_info line;
558 struct dwarf2_section_info loc;
559 struct dwarf2_section_info macinfo;
560 struct dwarf2_section_info macro;
561 struct dwarf2_section_info str_offsets;
562 /* Each DWP hash table entry records one CU or one TU.
563 That is recorded here, and copied to dwo_unit.section. */
564 struct dwarf2_section_info info_or_types;
565 };
566
567 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
568 In version 2, the sections of the DWO files are concatenated together
569 and stored in one section of that name. Thus each ELF section contains
570 several "virtual" sections. */
571
572 struct virtual_v2_or_v5_dwo_sections
573 {
574 bfd_size_type abbrev_offset;
575 bfd_size_type abbrev_size;
576
577 bfd_size_type line_offset;
578 bfd_size_type line_size;
579
580 bfd_size_type loc_offset;
581 bfd_size_type loc_size;
582
583 bfd_size_type loclists_offset;
584 bfd_size_type loclists_size;
585
586 bfd_size_type macinfo_offset;
587 bfd_size_type macinfo_size;
588
589 bfd_size_type macro_offset;
590 bfd_size_type macro_size;
591
592 bfd_size_type rnglists_offset;
593 bfd_size_type rnglists_size;
594
595 bfd_size_type str_offsets_offset;
596 bfd_size_type str_offsets_size;
597
598 /* Each DWP hash table entry records one CU or one TU.
599 That is recorded here, and copied to dwo_unit.section. */
600 bfd_size_type info_or_types_offset;
601 bfd_size_type info_or_types_size;
602 };
603
604 /* Contents of DWP hash tables. */
605
606 struct dwp_hash_table
607 {
608 uint32_t version, nr_columns;
609 uint32_t nr_units, nr_slots;
610 const gdb_byte *hash_table, *unit_table;
611 union
612 {
613 struct
614 {
615 const gdb_byte *indices;
616 } v1;
617 struct
618 {
619 /* This is indexed by column number and gives the id of the section
620 in that column. */
621 #define MAX_NR_V2_DWO_SECTIONS \
622 (1 /* .debug_info or .debug_types */ \
623 + 1 /* .debug_abbrev */ \
624 + 1 /* .debug_line */ \
625 + 1 /* .debug_loc */ \
626 + 1 /* .debug_str_offsets */ \
627 + 1 /* .debug_macro or .debug_macinfo */)
628 int section_ids[MAX_NR_V2_DWO_SECTIONS];
629 const gdb_byte *offsets;
630 const gdb_byte *sizes;
631 } v2;
632 struct
633 {
634 /* This is indexed by column number and gives the id of the section
635 in that column. */
636 #define MAX_NR_V5_DWO_SECTIONS \
637 (1 /* .debug_info */ \
638 + 1 /* .debug_abbrev */ \
639 + 1 /* .debug_line */ \
640 + 1 /* .debug_loclists */ \
641 + 1 /* .debug_str_offsets */ \
642 + 1 /* .debug_macro */ \
643 + 1 /* .debug_rnglists */)
644 int section_ids[MAX_NR_V5_DWO_SECTIONS];
645 const gdb_byte *offsets;
646 const gdb_byte *sizes;
647 } v5;
648 } section_pool;
649 };
650
651 /* Data for one DWP file. */
652
653 struct dwp_file
654 {
655 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
656 : name (name_),
657 dbfd (std::move (abfd))
658 {
659 }
660
661 /* Name of the file. */
662 const char *name;
663
664 /* File format version. */
665 int version = 0;
666
667 /* The bfd. */
668 gdb_bfd_ref_ptr dbfd;
669
670 /* Section info for this file. */
671 struct dwp_sections sections {};
672
673 /* Table of CUs in the file. */
674 const struct dwp_hash_table *cus = nullptr;
675
676 /* Table of TUs in the file. */
677 const struct dwp_hash_table *tus = nullptr;
678
679 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
680 htab_up loaded_cus;
681 htab_up loaded_tus;
682
683 /* Table to map ELF section numbers to their sections.
684 This is only needed for the DWP V1 file format. */
685 unsigned int num_sections = 0;
686 asection **elf_sections = nullptr;
687 };
688
689 /* Struct used to pass misc. parameters to read_die_and_children, et
690 al. which are used for both .debug_info and .debug_types dies.
691 All parameters here are unchanging for the life of the call. This
692 struct exists to abstract away the constant parameters of die reading. */
693
694 struct die_reader_specs
695 {
696 /* The bfd of die_section. */
697 bfd *abfd;
698
699 /* The CU of the DIE we are parsing. */
700 struct dwarf2_cu *cu;
701
702 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
703 struct dwo_file *dwo_file;
704
705 /* The section the die comes from.
706 This is either .debug_info or .debug_types, or the .dwo variants. */
707 struct dwarf2_section_info *die_section;
708
709 /* die_section->buffer. */
710 const gdb_byte *buffer;
711
712 /* The end of the buffer. */
713 const gdb_byte *buffer_end;
714
715 /* The abbreviation table to use when reading the DIEs. */
716 struct abbrev_table *abbrev_table;
717 };
718
719 /* A subclass of die_reader_specs that holds storage and has complex
720 constructor and destructor behavior. */
721
722 class cutu_reader : public die_reader_specs
723 {
724 public:
725
726 cutu_reader (dwarf2_per_cu_data *this_cu,
727 dwarf2_per_objfile *per_objfile,
728 struct abbrev_table *abbrev_table,
729 dwarf2_cu *existing_cu,
730 bool skip_partial,
731 abbrev_cache *cache = nullptr);
732
733 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
734 dwarf2_per_objfile *per_objfile,
735 struct dwarf2_cu *parent_cu = nullptr,
736 struct dwo_file *dwo_file = nullptr);
737
738 DISABLE_COPY_AND_ASSIGN (cutu_reader);
739
740 cutu_reader (cutu_reader &&) = default;
741
742 const gdb_byte *info_ptr = nullptr;
743 struct die_info *comp_unit_die = nullptr;
744 bool dummy_p = false;
745
746 /* Release the new CU, putting it on the chain. This cannot be done
747 for dummy CUs. */
748 void keep ();
749
750 /* Release the abbrev table, transferring ownership to the
751 caller. */
752 abbrev_table_up release_abbrev_table ()
753 {
754 return std::move (m_abbrev_table_holder);
755 }
756
757 private:
758 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
759 dwarf2_per_objfile *per_objfile,
760 dwarf2_cu *existing_cu);
761
762 struct dwarf2_per_cu_data *m_this_cu;
763 std::unique_ptr<dwarf2_cu> m_new_cu;
764
765 /* The ordinary abbreviation table. */
766 abbrev_table_up m_abbrev_table_holder;
767
768 /* The DWO abbreviation table. */
769 abbrev_table_up m_dwo_abbrev_table;
770 };
771
772 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
773 but this would require a corresponding change in unpack_field_as_long
774 and friends. */
775 static int bits_per_byte = 8;
776
777 struct variant_part_builder;
778
779 /* When reading a variant, we track a bit more information about the
780 field, and store it in an object of this type. */
781
782 struct variant_field
783 {
784 int first_field = -1;
785 int last_field = -1;
786
787 /* A variant can contain other variant parts. */
788 std::vector<variant_part_builder> variant_parts;
789
790 /* If we see a DW_TAG_variant, then this will be set if this is the
791 default branch. */
792 bool default_branch = false;
793 /* If we see a DW_AT_discr_value, then this will be the discriminant
794 value. */
795 ULONGEST discriminant_value = 0;
796 /* If we see a DW_AT_discr_list, then this is a pointer to the list
797 data. */
798 struct dwarf_block *discr_list_data = nullptr;
799 };
800
801 /* This represents a DW_TAG_variant_part. */
802
803 struct variant_part_builder
804 {
805 /* The offset of the discriminant field. */
806 sect_offset discriminant_offset {};
807
808 /* Variants that are direct children of this variant part. */
809 std::vector<variant_field> variants;
810
811 /* True if we're currently reading a variant. */
812 bool processing_variant = false;
813 };
814
815 struct nextfield
816 {
817 int accessibility = 0;
818 int virtuality = 0;
819 /* Variant parts need to find the discriminant, which is a DIE
820 reference. We track the section offset of each field to make
821 this link. */
822 sect_offset offset;
823 struct field field {};
824 };
825
826 struct fnfieldlist
827 {
828 const char *name = nullptr;
829 std::vector<struct fn_field> fnfields;
830 };
831
832 /* The routines that read and process dies for a C struct or C++ class
833 pass lists of data member fields and lists of member function fields
834 in an instance of a field_info structure, as defined below. */
835 struct field_info
836 {
837 /* List of data member and baseclasses fields. */
838 std::vector<struct nextfield> fields;
839 std::vector<struct nextfield> baseclasses;
840
841 /* Set if the accessibility of one of the fields is not public. */
842 bool non_public_fields = false;
843
844 /* Member function fieldlist array, contains name of possibly overloaded
845 member function, number of overloaded member functions and a pointer
846 to the head of the member function field chain. */
847 std::vector<struct fnfieldlist> fnfieldlists;
848
849 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
850 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
851 std::vector<struct decl_field> typedef_field_list;
852
853 /* Nested types defined by this class and the number of elements in this
854 list. */
855 std::vector<struct decl_field> nested_types_list;
856
857 /* If non-null, this is the variant part we are currently
858 reading. */
859 variant_part_builder *current_variant_part = nullptr;
860 /* This holds all the top-level variant parts attached to the type
861 we're reading. */
862 std::vector<variant_part_builder> variant_parts;
863
864 /* Return the total number of fields (including baseclasses). */
865 int nfields () const
866 {
867 return fields.size () + baseclasses.size ();
868 }
869 };
870
871 /* Loaded secondary compilation units are kept in memory until they
872 have not been referenced for the processing of this many
873 compilation units. Set this to zero to disable caching. Cache
874 sizes of up to at least twenty will improve startup time for
875 typical inter-CU-reference binaries, at an obvious memory cost. */
876 static int dwarf_max_cache_age = 5;
877 static void
878 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
879 struct cmd_list_element *c, const char *value)
880 {
881 gdb_printf (file, _("The upper bound on the age of cached "
882 "DWARF compilation units is %s.\n"),
883 value);
884 }
885 \f
886 /* local function prototypes */
887
888 static void dwarf2_find_base_address (struct die_info *die,
889 struct dwarf2_cu *cu);
890
891 class cooked_index_storage;
892 static void build_type_psymtabs_reader (cutu_reader *reader,
893 cooked_index_storage *storage);
894
895 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
896
897 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
898
899 static const gdb_byte *read_attribute (const struct die_reader_specs *,
900 struct attribute *,
901 const struct attr_abbrev *,
902 const gdb_byte *);
903
904 static void read_attribute_reprocess (const struct die_reader_specs *reader,
905 struct attribute *attr, dwarf_tag tag);
906
907 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
908
909 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
910 dwarf2_section_info *, sect_offset);
911
912 static const char *read_indirect_string
913 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
914 const struct comp_unit_head *, unsigned int *);
915
916 static const char *read_indirect_string_at_offset
917 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
918
919 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
920 const gdb_byte *,
921 unsigned int *);
922
923 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
924 ULONGEST str_index);
925
926 static const char *read_stub_str_index (struct dwarf2_cu *cu,
927 ULONGEST str_index);
928
929 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
930 struct dwarf2_cu *);
931
932 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
933 struct dwarf2_cu *cu);
934
935 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
936
937 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
938 struct dwarf2_cu *cu);
939
940 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
941
942 static struct die_info *die_specification (struct die_info *die,
943 struct dwarf2_cu **);
944
945 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
946 struct dwarf2_cu *cu);
947
948 static void dwarf_decode_lines (struct line_header *,
949 struct dwarf2_cu *,
950 CORE_ADDR, int decode_mapping);
951
952 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
953 const char *);
954
955 static struct symbol *new_symbol (struct die_info *, struct type *,
956 struct dwarf2_cu *, struct symbol * = NULL);
957
958 static void dwarf2_const_value (const struct attribute *, struct symbol *,
959 struct dwarf2_cu *);
960
961 static void dwarf2_const_value_attr (const struct attribute *attr,
962 struct type *type,
963 const char *name,
964 struct obstack *obstack,
965 struct dwarf2_cu *cu, LONGEST *value,
966 const gdb_byte **bytes,
967 struct dwarf2_locexpr_baton **baton);
968
969 static struct type *read_subrange_index_type (struct die_info *die,
970 struct dwarf2_cu *cu);
971
972 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
973
974 static int need_gnat_info (struct dwarf2_cu *);
975
976 static struct type *die_descriptive_type (struct die_info *,
977 struct dwarf2_cu *);
978
979 static void set_descriptive_type (struct type *, struct die_info *,
980 struct dwarf2_cu *);
981
982 static struct type *die_containing_type (struct die_info *,
983 struct dwarf2_cu *);
984
985 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
986 struct dwarf2_cu *);
987
988 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
989
990 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
991
992 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
993
994 static char *typename_concat (struct obstack *obs, const char *prefix,
995 const char *suffix, int physname,
996 struct dwarf2_cu *cu);
997
998 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
999
1000 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1001
1002 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1003
1004 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1005
1006 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1007
1008 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1009
1010 /* Return the .debug_loclists section to use for cu. */
1011 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1012
1013 /* Return the .debug_rnglists section to use for cu. */
1014 static struct dwarf2_section_info *cu_debug_rnglists_section
1015 (struct dwarf2_cu *cu, dwarf_tag tag);
1016
1017 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1018 values. Keep the items ordered with increasing constraints compliance. */
1019 enum pc_bounds_kind
1020 {
1021 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1022 PC_BOUNDS_NOT_PRESENT,
1023
1024 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1025 were present but they do not form a valid range of PC addresses. */
1026 PC_BOUNDS_INVALID,
1027
1028 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1029 PC_BOUNDS_RANGES,
1030
1031 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1032 PC_BOUNDS_HIGH_LOW,
1033 };
1034
1035 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1036 CORE_ADDR *, CORE_ADDR *,
1037 struct dwarf2_cu *,
1038 addrmap *,
1039 void *);
1040
1041 static void get_scope_pc_bounds (struct die_info *,
1042 CORE_ADDR *, CORE_ADDR *,
1043 struct dwarf2_cu *);
1044
1045 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1046 CORE_ADDR, struct dwarf2_cu *);
1047
1048 static void dwarf2_add_field (struct field_info *, struct die_info *,
1049 struct dwarf2_cu *);
1050
1051 static void dwarf2_attach_fields_to_type (struct field_info *,
1052 struct type *, struct dwarf2_cu *);
1053
1054 static void dwarf2_add_member_fn (struct field_info *,
1055 struct die_info *, struct type *,
1056 struct dwarf2_cu *);
1057
1058 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1059 struct type *,
1060 struct dwarf2_cu *);
1061
1062 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1063
1064 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1065
1066 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1067
1068 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1069
1070 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1071
1072 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1073
1074 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1075
1076 static struct type *read_module_type (struct die_info *die,
1077 struct dwarf2_cu *cu);
1078
1079 static const char *namespace_name (struct die_info *die,
1080 int *is_anonymous, struct dwarf2_cu *);
1081
1082 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1083
1084 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1085 bool * = nullptr);
1086
1087 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1088 struct dwarf2_cu *);
1089
1090 static struct die_info *read_die_and_siblings_1
1091 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1092 struct die_info *);
1093
1094 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1095 const gdb_byte *info_ptr,
1096 const gdb_byte **new_info_ptr,
1097 struct die_info *parent);
1098
1099 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1100 struct die_info **, const gdb_byte *,
1101 int);
1102
1103 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1104 struct die_info **, const gdb_byte *);
1105
1106 static void process_die (struct die_info *, struct dwarf2_cu *);
1107
1108 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1109 struct objfile *);
1110
1111 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1112
1113 static const char *dwarf2_full_name (const char *name,
1114 struct die_info *die,
1115 struct dwarf2_cu *cu);
1116
1117 static const char *dwarf2_physname (const char *name, struct die_info *die,
1118 struct dwarf2_cu *cu);
1119
1120 static struct die_info *dwarf2_extension (struct die_info *die,
1121 struct dwarf2_cu **);
1122
1123 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1124
1125 static void dump_die_for_error (struct die_info *);
1126
1127 static void dump_die_1 (struct ui_file *, int level, int max_level,
1128 struct die_info *);
1129
1130 /*static*/ void dump_die (struct die_info *, int max_level);
1131
1132 static void store_in_ref_table (struct die_info *,
1133 struct dwarf2_cu *);
1134
1135 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1136 const struct attribute *,
1137 struct dwarf2_cu **);
1138
1139 static struct die_info *follow_die_ref (struct die_info *,
1140 const struct attribute *,
1141 struct dwarf2_cu **);
1142
1143 static struct die_info *follow_die_sig (struct die_info *,
1144 const struct attribute *,
1145 struct dwarf2_cu **);
1146
1147 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1148 struct dwarf2_cu *);
1149
1150 static struct type *get_DW_AT_signature_type (struct die_info *,
1151 const struct attribute *,
1152 struct dwarf2_cu *);
1153
1154 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1155 dwarf2_per_objfile *per_objfile);
1156
1157 static void read_signatured_type (signatured_type *sig_type,
1158 dwarf2_per_objfile *per_objfile);
1159
1160 static int attr_to_dynamic_prop (const struct attribute *attr,
1161 struct die_info *die, struct dwarf2_cu *cu,
1162 struct dynamic_prop *prop, struct type *type);
1163
1164 /* memory allocation interface */
1165
1166 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1167
1168 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1169
1170 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1171
1172 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1173 struct dwarf2_loclist_baton *baton,
1174 const struct attribute *attr);
1175
1176 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1177 struct symbol *sym,
1178 struct dwarf2_cu *cu,
1179 int is_block);
1180
1181 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1182 const gdb_byte *info_ptr,
1183 const struct abbrev_info *abbrev,
1184 bool do_skip_children = true);
1185
1186 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1187 (sect_offset sect_off, unsigned int offset_in_dwz,
1188 dwarf2_per_bfd *per_bfd);
1189
1190 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1191 struct die_info *comp_unit_die,
1192 enum language pretend_language);
1193
1194 static struct type *set_die_type (struct die_info *, struct type *,
1195 struct dwarf2_cu *, bool = false);
1196
1197 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1198
1199 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1200 dwarf2_per_objfile *per_objfile,
1201 dwarf2_cu *existing_cu,
1202 bool skip_partial,
1203 enum language pretend_language);
1204
1205 static void process_full_comp_unit (dwarf2_cu *cu,
1206 enum language pretend_language);
1207
1208 static void process_full_type_unit (dwarf2_cu *cu,
1209 enum language pretend_language);
1210
1211 static struct type *get_die_type_at_offset (sect_offset,
1212 dwarf2_per_cu_data *per_cu,
1213 dwarf2_per_objfile *per_objfile);
1214
1215 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1216
1217 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1218 dwarf2_per_objfile *per_objfile,
1219 enum language pretend_language);
1220
1221 static void process_queue (dwarf2_per_objfile *per_objfile);
1222
1223 /* Class, the destructor of which frees all allocated queue entries. This
1224 will only have work to do if an error was thrown while processing the
1225 dwarf. If no error was thrown then the queue entries should have all
1226 been processed, and freed, as we went along. */
1227
1228 class dwarf2_queue_guard
1229 {
1230 public:
1231 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1232 : m_per_objfile (per_objfile)
1233 {
1234 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1235
1236 m_per_objfile->per_bfd->queue.emplace ();
1237 }
1238
1239 /* Free any entries remaining on the queue. There should only be
1240 entries left if we hit an error while processing the dwarf. */
1241 ~dwarf2_queue_guard ()
1242 {
1243 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1244
1245 m_per_objfile->per_bfd->queue.reset ();
1246 }
1247
1248 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1249
1250 private:
1251 dwarf2_per_objfile *m_per_objfile;
1252 };
1253
1254 dwarf2_queue_item::~dwarf2_queue_item ()
1255 {
1256 /* Anything still marked queued is likely to be in an
1257 inconsistent state, so discard it. */
1258 if (per_cu->queued)
1259 {
1260 per_objfile->remove_cu (per_cu);
1261 per_cu->queued = 0;
1262 }
1263 }
1264
1265 /* See dwarf2/read.h. */
1266
1267 void
1268 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1269 {
1270 if (data->is_debug_types)
1271 delete static_cast<signatured_type *> (data);
1272 else
1273 delete data;
1274 }
1275
1276 static file_and_directory &find_file_and_directory
1277 (struct die_info *die, struct dwarf2_cu *cu);
1278
1279 static const char *compute_include_file_name
1280 (const struct line_header *lh,
1281 const file_entry &fe,
1282 const file_and_directory &cu_info,
1283 std::string &name_holder);
1284
1285 static htab_up allocate_signatured_type_table ();
1286
1287 static htab_up allocate_dwo_unit_table ();
1288
1289 static struct dwo_unit *lookup_dwo_unit_in_dwp
1290 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1291 const char *comp_dir, ULONGEST signature, int is_debug_types);
1292
1293 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1294
1295 static struct dwo_unit *lookup_dwo_comp_unit
1296 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1297 ULONGEST signature);
1298
1299 static struct dwo_unit *lookup_dwo_type_unit
1300 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1301
1302 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1303
1304 /* A unique pointer to a dwo_file. */
1305
1306 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1307
1308 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1309
1310 static void check_producer (struct dwarf2_cu *cu);
1311 \f
1312 /* Various complaints about symbol reading that don't abort the process. */
1313
1314 static void
1315 dwarf2_debug_line_missing_file_complaint (void)
1316 {
1317 complaint (_(".debug_line section has line data without a file"));
1318 }
1319
1320 static void
1321 dwarf2_debug_line_missing_end_sequence_complaint (void)
1322 {
1323 complaint (_(".debug_line section has line "
1324 "program sequence without an end"));
1325 }
1326
1327 static void
1328 dwarf2_complex_location_expr_complaint (void)
1329 {
1330 complaint (_("location expression too complex"));
1331 }
1332
1333 static void
1334 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1335 int arg3)
1336 {
1337 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1338 arg1, arg2, arg3);
1339 }
1340
1341 static void
1342 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1343 {
1344 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1345 arg1, arg2);
1346 }
1347
1348 /* Hash function for line_header_hash. */
1349
1350 static hashval_t
1351 line_header_hash (const struct line_header *ofs)
1352 {
1353 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1354 }
1355
1356 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1357
1358 static hashval_t
1359 line_header_hash_voidp (const void *item)
1360 {
1361 const struct line_header *ofs = (const struct line_header *) item;
1362
1363 return line_header_hash (ofs);
1364 }
1365
1366 /* Equality function for line_header_hash. */
1367
1368 static int
1369 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1370 {
1371 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1372 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1373
1374 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1375 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1376 }
1377
1378 \f
1379
1380 /* An iterator for all_comp_units that is based on index. This
1381 approach makes it possible to iterate over all_comp_units safely,
1382 when some caller in the loop may add new units. */
1383
1384 class all_comp_units_iterator
1385 {
1386 public:
1387
1388 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1389 : m_per_bfd (per_bfd),
1390 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1391 {
1392 }
1393
1394 all_comp_units_iterator &operator++ ()
1395 {
1396 ++m_index;
1397 return *this;
1398 }
1399
1400 dwarf2_per_cu_data *operator* () const
1401 {
1402 return m_per_bfd->get_cu (m_index);
1403 }
1404
1405 bool operator== (const all_comp_units_iterator &other) const
1406 {
1407 return m_index == other.m_index;
1408 }
1409
1410
1411 bool operator!= (const all_comp_units_iterator &other) const
1412 {
1413 return m_index != other.m_index;
1414 }
1415
1416 private:
1417
1418 dwarf2_per_bfd *m_per_bfd;
1419 size_t m_index;
1420 };
1421
1422 /* A range adapter for the all_comp_units_iterator. */
1423 class all_comp_units_range
1424 {
1425 public:
1426
1427 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1428 : m_per_bfd (per_bfd)
1429 {
1430 }
1431
1432 all_comp_units_iterator begin ()
1433 {
1434 return all_comp_units_iterator (m_per_bfd, true);
1435 }
1436
1437 all_comp_units_iterator end ()
1438 {
1439 return all_comp_units_iterator (m_per_bfd, false);
1440 }
1441
1442 private:
1443
1444 dwarf2_per_bfd *m_per_bfd;
1445 };
1446
1447 /* See declaration. */
1448
1449 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1450 bool can_copy_)
1451 : obfd (obfd),
1452 can_copy (can_copy_)
1453 {
1454 if (names == NULL)
1455 names = &dwarf2_elf_names;
1456
1457 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1458 locate_sections (obfd, sec, *names);
1459 }
1460
1461 dwarf2_per_bfd::~dwarf2_per_bfd ()
1462 {
1463 for (auto &per_cu : all_comp_units)
1464 {
1465 per_cu->imported_symtabs_free ();
1466 per_cu->free_cached_file_names ();
1467 }
1468
1469 /* Everything else should be on this->obstack. */
1470 }
1471
1472 /* See read.h. */
1473
1474 void
1475 dwarf2_per_objfile::remove_all_cus ()
1476 {
1477 gdb_assert (!this->per_bfd->queue.has_value ());
1478
1479 for (auto pair : m_dwarf2_cus)
1480 delete pair.second;
1481
1482 m_dwarf2_cus.clear ();
1483 }
1484
1485 /* A helper class that calls free_cached_comp_units on
1486 destruction. */
1487
1488 class free_cached_comp_units
1489 {
1490 public:
1491
1492 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1493 : m_per_objfile (per_objfile)
1494 {
1495 }
1496
1497 ~free_cached_comp_units ()
1498 {
1499 m_per_objfile->remove_all_cus ();
1500 }
1501
1502 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1503
1504 private:
1505
1506 dwarf2_per_objfile *m_per_objfile;
1507 };
1508
1509 /* See read.h. */
1510
1511 bool
1512 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1513 {
1514 if (per_cu->index < this->m_symtabs.size ())
1515 return this->m_symtabs[per_cu->index] != nullptr;
1516 return false;
1517 }
1518
1519 /* See read.h. */
1520
1521 compunit_symtab *
1522 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1523 {
1524 if (per_cu->index < this->m_symtabs.size ())
1525 return this->m_symtabs[per_cu->index];
1526 return nullptr;
1527 }
1528
1529 /* See read.h. */
1530
1531 void
1532 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1533 compunit_symtab *symtab)
1534 {
1535 if (per_cu->index >= this->m_symtabs.size ())
1536 this->m_symtabs.resize (per_cu->index + 1);
1537 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1538 this->m_symtabs[per_cu->index] = symtab;
1539 }
1540
1541 /* Try to locate the sections we need for DWARF 2 debugging
1542 information and return true if we have enough to do something.
1543 NAMES points to the dwarf2 section names, or is NULL if the standard
1544 ELF names are used. CAN_COPY is true for formats where symbol
1545 interposition is possible and so symbol values must follow copy
1546 relocation rules. */
1547
1548 int
1549 dwarf2_has_info (struct objfile *objfile,
1550 const struct dwarf2_debug_sections *names,
1551 bool can_copy)
1552 {
1553 if (objfile->flags & OBJF_READNEVER)
1554 return 0;
1555
1556 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1557
1558 if (per_objfile == NULL)
1559 {
1560 dwarf2_per_bfd *per_bfd;
1561
1562 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1563 BFD doesn't require relocations.
1564
1565 We don't share with objfiles for which -readnow was requested,
1566 because it would complicate things when loading the same BFD with
1567 -readnow and then without -readnow. */
1568 if (!gdb_bfd_requires_relocations (objfile->obfd)
1569 && (objfile->flags & OBJF_READNOW) == 0)
1570 {
1571 /* See if one has been created for this BFD yet. */
1572 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1573
1574 if (per_bfd == nullptr)
1575 {
1576 /* No, create it now. */
1577 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1578 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1579 }
1580 }
1581 else
1582 {
1583 /* No sharing possible, create one specifically for this objfile. */
1584 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1585 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1586 }
1587
1588 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1589 }
1590
1591 return (!per_objfile->per_bfd->info.is_virtual
1592 && per_objfile->per_bfd->info.s.section != NULL
1593 && !per_objfile->per_bfd->abbrev.is_virtual
1594 && per_objfile->per_bfd->abbrev.s.section != NULL);
1595 }
1596
1597 /* See declaration. */
1598
1599 void
1600 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1601 const dwarf2_debug_sections &names)
1602 {
1603 flagword aflag = bfd_section_flags (sectp);
1604
1605 if ((aflag & SEC_HAS_CONTENTS) == 0)
1606 {
1607 }
1608 else if (elf_section_data (sectp)->this_hdr.sh_size
1609 > bfd_get_file_size (abfd))
1610 {
1611 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1612 warning (_("Discarding section %s which has a section size (%s"
1613 ") larger than the file size [in module %s]"),
1614 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1615 bfd_get_filename (abfd));
1616 }
1617 else if (names.info.matches (sectp->name))
1618 {
1619 this->info.s.section = sectp;
1620 this->info.size = bfd_section_size (sectp);
1621 }
1622 else if (names.abbrev.matches (sectp->name))
1623 {
1624 this->abbrev.s.section = sectp;
1625 this->abbrev.size = bfd_section_size (sectp);
1626 }
1627 else if (names.line.matches (sectp->name))
1628 {
1629 this->line.s.section = sectp;
1630 this->line.size = bfd_section_size (sectp);
1631 }
1632 else if (names.loc.matches (sectp->name))
1633 {
1634 this->loc.s.section = sectp;
1635 this->loc.size = bfd_section_size (sectp);
1636 }
1637 else if (names.loclists.matches (sectp->name))
1638 {
1639 this->loclists.s.section = sectp;
1640 this->loclists.size = bfd_section_size (sectp);
1641 }
1642 else if (names.macinfo.matches (sectp->name))
1643 {
1644 this->macinfo.s.section = sectp;
1645 this->macinfo.size = bfd_section_size (sectp);
1646 }
1647 else if (names.macro.matches (sectp->name))
1648 {
1649 this->macro.s.section = sectp;
1650 this->macro.size = bfd_section_size (sectp);
1651 }
1652 else if (names.str.matches (sectp->name))
1653 {
1654 this->str.s.section = sectp;
1655 this->str.size = bfd_section_size (sectp);
1656 }
1657 else if (names.str_offsets.matches (sectp->name))
1658 {
1659 this->str_offsets.s.section = sectp;
1660 this->str_offsets.size = bfd_section_size (sectp);
1661 }
1662 else if (names.line_str.matches (sectp->name))
1663 {
1664 this->line_str.s.section = sectp;
1665 this->line_str.size = bfd_section_size (sectp);
1666 }
1667 else if (names.addr.matches (sectp->name))
1668 {
1669 this->addr.s.section = sectp;
1670 this->addr.size = bfd_section_size (sectp);
1671 }
1672 else if (names.frame.matches (sectp->name))
1673 {
1674 this->frame.s.section = sectp;
1675 this->frame.size = bfd_section_size (sectp);
1676 }
1677 else if (names.eh_frame.matches (sectp->name))
1678 {
1679 this->eh_frame.s.section = sectp;
1680 this->eh_frame.size = bfd_section_size (sectp);
1681 }
1682 else if (names.ranges.matches (sectp->name))
1683 {
1684 this->ranges.s.section = sectp;
1685 this->ranges.size = bfd_section_size (sectp);
1686 }
1687 else if (names.rnglists.matches (sectp->name))
1688 {
1689 this->rnglists.s.section = sectp;
1690 this->rnglists.size = bfd_section_size (sectp);
1691 }
1692 else if (names.types.matches (sectp->name))
1693 {
1694 struct dwarf2_section_info type_section;
1695
1696 memset (&type_section, 0, sizeof (type_section));
1697 type_section.s.section = sectp;
1698 type_section.size = bfd_section_size (sectp);
1699
1700 this->types.push_back (type_section);
1701 }
1702 else if (names.gdb_index.matches (sectp->name))
1703 {
1704 this->gdb_index.s.section = sectp;
1705 this->gdb_index.size = bfd_section_size (sectp);
1706 }
1707 else if (names.debug_names.matches (sectp->name))
1708 {
1709 this->debug_names.s.section = sectp;
1710 this->debug_names.size = bfd_section_size (sectp);
1711 }
1712 else if (names.debug_aranges.matches (sectp->name))
1713 {
1714 this->debug_aranges.s.section = sectp;
1715 this->debug_aranges.size = bfd_section_size (sectp);
1716 }
1717
1718 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1719 && bfd_section_vma (sectp) == 0)
1720 this->has_section_at_zero = true;
1721 }
1722
1723 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1724 SECTION_NAME. */
1725
1726 void
1727 dwarf2_get_section_info (struct objfile *objfile,
1728 enum dwarf2_section_enum sect,
1729 asection **sectp, const gdb_byte **bufp,
1730 bfd_size_type *sizep)
1731 {
1732 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1733 struct dwarf2_section_info *info;
1734
1735 /* We may see an objfile without any DWARF, in which case we just
1736 return nothing. */
1737 if (per_objfile == NULL)
1738 {
1739 *sectp = NULL;
1740 *bufp = NULL;
1741 *sizep = 0;
1742 return;
1743 }
1744 switch (sect)
1745 {
1746 case DWARF2_DEBUG_FRAME:
1747 info = &per_objfile->per_bfd->frame;
1748 break;
1749 case DWARF2_EH_FRAME:
1750 info = &per_objfile->per_bfd->eh_frame;
1751 break;
1752 default:
1753 gdb_assert_not_reached ("unexpected section");
1754 }
1755
1756 info->read (objfile);
1757
1758 *sectp = info->get_bfd_section ();
1759 *bufp = info->buffer;
1760 *sizep = info->size;
1761 }
1762
1763 /* See dwarf2/read.h. */
1764
1765 void
1766 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1767 {
1768 info.read (objfile);
1769 abbrev.read (objfile);
1770 line.read (objfile);
1771 str.read (objfile);
1772 str_offsets.read (objfile);
1773 line_str.read (objfile);
1774 ranges.read (objfile);
1775 rnglists.read (objfile);
1776 addr.read (objfile);
1777
1778 for (auto &section : types)
1779 section.read (objfile);
1780 }
1781
1782 \f
1783 /* DWARF quick_symbol_functions support. */
1784
1785 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1786 unique line tables, so we maintain a separate table of all .debug_line
1787 derived entries to support the sharing.
1788 All the quick functions need is the list of file names. We discard the
1789 line_header when we're done and don't need to record it here. */
1790 struct quick_file_names
1791 {
1792 /* The data used to construct the hash key. */
1793 struct stmt_list_hash hash;
1794
1795 /* The number of entries in file_names, real_names. */
1796 unsigned int num_file_names;
1797
1798 /* The CU directory, as given by DW_AT_comp_dir. May be
1799 nullptr. */
1800 const char *comp_dir;
1801
1802 /* The file names from the line table, after being run through
1803 file_full_name. */
1804 const char **file_names;
1805
1806 /* The file names from the line table after being run through
1807 gdb_realpath. These are computed lazily. */
1808 const char **real_names;
1809 };
1810
1811 struct dwarf2_base_index_functions : public quick_symbol_functions
1812 {
1813 bool has_symbols (struct objfile *objfile) override;
1814
1815 bool has_unexpanded_symtabs (struct objfile *objfile) override;
1816
1817 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
1818
1819 void forget_cached_source_info (struct objfile *objfile) override;
1820
1821 enum language lookup_global_symbol_language (struct objfile *objfile,
1822 const char *name,
1823 domain_enum domain,
1824 bool *symbol_found_p) override
1825 {
1826 *symbol_found_p = false;
1827 return language_unknown;
1828 }
1829
1830 void print_stats (struct objfile *objfile, bool print_bcache) override;
1831
1832 void expand_all_symtabs (struct objfile *objfile) override;
1833
1834 struct compunit_symtab *find_pc_sect_compunit_symtab
1835 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
1836 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
1837
1838 struct compunit_symtab *find_compunit_symtab_by_address
1839 (struct objfile *objfile, CORE_ADDR address) override
1840 {
1841 return nullptr;
1842 }
1843
1844 void map_symbol_filenames (struct objfile *objfile,
1845 gdb::function_view<symbol_filename_ftype> fun,
1846 bool need_fullname) override;
1847 };
1848
1849 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1850 It's handy in this case to have an empty implementation of the
1851 quick symbol functions, to avoid special cases in the rest of the
1852 code. */
1853
1854 struct readnow_functions : public dwarf2_base_index_functions
1855 {
1856 void dump (struct objfile *objfile) override
1857 {
1858 }
1859
1860 void expand_matching_symbols
1861 (struct objfile *,
1862 const lookup_name_info &lookup_name,
1863 domain_enum domain,
1864 int global,
1865 symbol_compare_ftype *ordered_compare) override
1866 {
1867 }
1868
1869 bool expand_symtabs_matching
1870 (struct objfile *objfile,
1871 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1872 const lookup_name_info *lookup_name,
1873 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1874 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1875 block_search_flags search_flags,
1876 domain_enum domain,
1877 enum search_domain kind) override
1878 {
1879 return true;
1880 }
1881 };
1882
1883 struct dwarf2_gdb_index : public dwarf2_base_index_functions
1884 {
1885 void dump (struct objfile *objfile) override;
1886
1887 void expand_matching_symbols
1888 (struct objfile *,
1889 const lookup_name_info &lookup_name,
1890 domain_enum domain,
1891 int global,
1892 symbol_compare_ftype *ordered_compare) override;
1893
1894 bool expand_symtabs_matching
1895 (struct objfile *objfile,
1896 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1897 const lookup_name_info *lookup_name,
1898 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1899 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1900 block_search_flags search_flags,
1901 domain_enum domain,
1902 enum search_domain kind) override;
1903 };
1904
1905 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
1906 {
1907 void dump (struct objfile *objfile) override;
1908
1909 void expand_matching_symbols
1910 (struct objfile *,
1911 const lookup_name_info &lookup_name,
1912 domain_enum domain,
1913 int global,
1914 symbol_compare_ftype *ordered_compare) override;
1915
1916 bool expand_symtabs_matching
1917 (struct objfile *objfile,
1918 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1919 const lookup_name_info *lookup_name,
1920 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1921 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1922 block_search_flags search_flags,
1923 domain_enum domain,
1924 enum search_domain kind) override;
1925 };
1926
1927 quick_symbol_functions_up
1928 mapped_index::make_quick_functions () const
1929 {
1930 return quick_symbol_functions_up (new dwarf2_gdb_index);
1931 }
1932
1933 quick_symbol_functions_up
1934 mapped_debug_names::make_quick_functions () const
1935 {
1936 return quick_symbol_functions_up (new dwarf2_debug_names_index);
1937 }
1938
1939 /* Utility hash function for a stmt_list_hash. */
1940
1941 static hashval_t
1942 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1943 {
1944 hashval_t v = 0;
1945
1946 if (stmt_list_hash->dwo_unit != NULL)
1947 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1948 v += to_underlying (stmt_list_hash->line_sect_off);
1949 return v;
1950 }
1951
1952 /* Utility equality function for a stmt_list_hash. */
1953
1954 static int
1955 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1956 const struct stmt_list_hash *rhs)
1957 {
1958 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1959 return 0;
1960 if (lhs->dwo_unit != NULL
1961 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1962 return 0;
1963
1964 return lhs->line_sect_off == rhs->line_sect_off;
1965 }
1966
1967 /* Hash function for a quick_file_names. */
1968
1969 static hashval_t
1970 hash_file_name_entry (const void *e)
1971 {
1972 const struct quick_file_names *file_data
1973 = (const struct quick_file_names *) e;
1974
1975 return hash_stmt_list_entry (&file_data->hash);
1976 }
1977
1978 /* Equality function for a quick_file_names. */
1979
1980 static int
1981 eq_file_name_entry (const void *a, const void *b)
1982 {
1983 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1984 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1985
1986 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1987 }
1988
1989 /* Create a quick_file_names hash table. */
1990
1991 static htab_up
1992 create_quick_file_names_table (unsigned int nr_initial_entries)
1993 {
1994 return htab_up (htab_create_alloc (nr_initial_entries,
1995 hash_file_name_entry, eq_file_name_entry,
1996 nullptr, xcalloc, xfree));
1997 }
1998
1999 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2000 function is unrelated to symtabs, symtab would have to be created afterwards.
2001 You should call age_cached_comp_units after processing the CU. */
2002
2003 static dwarf2_cu *
2004 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2005 bool skip_partial)
2006 {
2007 if (per_cu->is_debug_types)
2008 load_full_type_unit (per_cu, per_objfile);
2009 else
2010 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2011 skip_partial, language_minimal);
2012
2013 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2014 if (cu == nullptr)
2015 return nullptr; /* Dummy CU. */
2016
2017 dwarf2_find_base_address (cu->dies, cu);
2018
2019 return cu;
2020 }
2021
2022 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2023
2024 static void
2025 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2026 dwarf2_per_objfile *per_objfile, bool skip_partial)
2027 {
2028 {
2029 /* The destructor of dwarf2_queue_guard frees any entries left on
2030 the queue. After this point we're guaranteed to leave this function
2031 with the dwarf queue empty. */
2032 dwarf2_queue_guard q_guard (per_objfile);
2033
2034 if (!per_objfile->symtab_set_p (per_cu))
2035 {
2036 queue_comp_unit (per_cu, per_objfile, language_minimal);
2037 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2038
2039 /* If we just loaded a CU from a DWO, and we're working with an index
2040 that may badly handle TUs, load all the TUs in that DWO as well.
2041 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2042 if (!per_cu->is_debug_types
2043 && cu != NULL
2044 && cu->dwo_unit != NULL
2045 && per_objfile->per_bfd->index_table != NULL
2046 && !per_objfile->per_bfd->index_table->version_check ()
2047 /* DWP files aren't supported yet. */
2048 && get_dwp_file (per_objfile) == NULL)
2049 queue_and_load_all_dwo_tus (cu);
2050 }
2051
2052 process_queue (per_objfile);
2053 }
2054
2055 /* Age the cache, releasing compilation units that have not
2056 been used recently. */
2057 per_objfile->age_comp_units ();
2058 }
2059
2060 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2061 the per-objfile for which this symtab is instantiated.
2062
2063 Returns the resulting symbol table. */
2064
2065 static struct compunit_symtab *
2066 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2067 dwarf2_per_objfile *per_objfile,
2068 bool skip_partial)
2069 {
2070 if (!per_objfile->symtab_set_p (per_cu))
2071 {
2072 free_cached_comp_units freer (per_objfile);
2073 scoped_restore decrementer = increment_reading_symtab ();
2074 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2075 process_cu_includes (per_objfile);
2076 }
2077
2078 return per_objfile->get_symtab (per_cu);
2079 }
2080
2081 /* See read.h. */
2082
2083 dwarf2_per_cu_data_up
2084 dwarf2_per_bfd::allocate_per_cu ()
2085 {
2086 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2087 result->per_bfd = this;
2088 result->index = all_comp_units.size ();
2089 return result;
2090 }
2091
2092 /* See read.h. */
2093
2094 signatured_type_up
2095 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2096 {
2097 signatured_type_up result (new signatured_type (signature));
2098 result->per_bfd = this;
2099 result->index = all_comp_units.size ();
2100 result->is_debug_types = true;
2101 tu_stats.nr_tus++;
2102 return result;
2103 }
2104
2105 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2106 obstack, and constructed with the specified field values. */
2107
2108 static dwarf2_per_cu_data_up
2109 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2110 struct dwarf2_section_info *section,
2111 int is_dwz,
2112 sect_offset sect_off, ULONGEST length)
2113 {
2114 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2115 the_cu->sect_off = sect_off;
2116 the_cu->length = length;
2117 the_cu->section = section;
2118 the_cu->is_dwz = is_dwz;
2119 return the_cu;
2120 }
2121
2122 /* A helper for create_cus_from_index that handles a given list of
2123 CUs. */
2124
2125 static void
2126 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2127 const gdb_byte *cu_list, offset_type n_elements,
2128 struct dwarf2_section_info *section,
2129 int is_dwz)
2130 {
2131 for (offset_type i = 0; i < n_elements; i += 2)
2132 {
2133 gdb_static_assert (sizeof (ULONGEST) >= 8);
2134
2135 sect_offset sect_off
2136 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2137 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2138 cu_list += 2 * 8;
2139
2140 dwarf2_per_cu_data_up per_cu
2141 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2142 length);
2143 per_bfd->all_comp_units.push_back (std::move (per_cu));
2144 }
2145 }
2146
2147 /* Read the CU list from the mapped index, and use it to create all
2148 the CU objects for PER_BFD. */
2149
2150 static void
2151 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2152 const gdb_byte *cu_list, offset_type cu_list_elements,
2153 const gdb_byte *dwz_list, offset_type dwz_elements)
2154 {
2155 gdb_assert (per_bfd->all_comp_units.empty ());
2156 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2157
2158 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2159 &per_bfd->info, 0);
2160
2161 if (dwz_elements == 0)
2162 return;
2163
2164 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2165 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2166 &dwz->info, 1);
2167 }
2168
2169 /* Create the signatured type hash table from the index. */
2170
2171 static void
2172 create_signatured_type_table_from_index
2173 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2174 const gdb_byte *bytes, offset_type elements)
2175 {
2176 htab_up sig_types_hash = allocate_signatured_type_table ();
2177
2178 for (offset_type i = 0; i < elements; i += 3)
2179 {
2180 signatured_type_up sig_type;
2181 ULONGEST signature;
2182 void **slot;
2183 cu_offset type_offset_in_tu;
2184
2185 gdb_static_assert (sizeof (ULONGEST) >= 8);
2186 sect_offset sect_off
2187 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2188 type_offset_in_tu
2189 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2190 BFD_ENDIAN_LITTLE);
2191 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2192 bytes += 3 * 8;
2193
2194 sig_type = per_bfd->allocate_signatured_type (signature);
2195 sig_type->type_offset_in_tu = type_offset_in_tu;
2196 sig_type->section = section;
2197 sig_type->sect_off = sect_off;
2198
2199 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2200 *slot = sig_type.get ();
2201
2202 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2203 }
2204
2205 per_bfd->signatured_types = std::move (sig_types_hash);
2206 }
2207
2208 /* Create the signatured type hash table from .debug_names. */
2209
2210 static void
2211 create_signatured_type_table_from_debug_names
2212 (dwarf2_per_objfile *per_objfile,
2213 const mapped_debug_names &map,
2214 struct dwarf2_section_info *section,
2215 struct dwarf2_section_info *abbrev_section)
2216 {
2217 struct objfile *objfile = per_objfile->objfile;
2218
2219 section->read (objfile);
2220 abbrev_section->read (objfile);
2221
2222 htab_up sig_types_hash = allocate_signatured_type_table ();
2223
2224 for (uint32_t i = 0; i < map.tu_count; ++i)
2225 {
2226 signatured_type_up sig_type;
2227 void **slot;
2228
2229 sect_offset sect_off
2230 = (sect_offset) (extract_unsigned_integer
2231 (map.tu_table_reordered + i * map.offset_size,
2232 map.offset_size,
2233 map.dwarf5_byte_order));
2234
2235 comp_unit_head cu_header;
2236 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2237 abbrev_section,
2238 section->buffer + to_underlying (sect_off),
2239 rcuh_kind::TYPE);
2240
2241 sig_type = per_objfile->per_bfd->allocate_signatured_type
2242 (cu_header.signature);
2243 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2244 sig_type->section = section;
2245 sig_type->sect_off = sect_off;
2246
2247 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2248 *slot = sig_type.get ();
2249
2250 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2251 }
2252
2253 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2254 }
2255
2256 /* Read the address map data from the mapped index, and use it to
2257 populate the psymtabs_addrmap. */
2258
2259 static void
2260 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2261 struct mapped_index *index)
2262 {
2263 struct objfile *objfile = per_objfile->objfile;
2264 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2265 struct gdbarch *gdbarch = objfile->arch ();
2266 const gdb_byte *iter, *end;
2267 struct addrmap *mutable_map;
2268 CORE_ADDR baseaddr;
2269
2270 auto_obstack temp_obstack;
2271
2272 mutable_map = addrmap_create_mutable (&temp_obstack);
2273
2274 iter = index->address_table.data ();
2275 end = iter + index->address_table.size ();
2276
2277 baseaddr = objfile->text_section_offset ();
2278
2279 while (iter < end)
2280 {
2281 ULONGEST hi, lo, cu_index;
2282 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2283 iter += 8;
2284 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2285 iter += 8;
2286 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2287 iter += 4;
2288
2289 if (lo > hi)
2290 {
2291 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2292 hex_string (lo), hex_string (hi));
2293 continue;
2294 }
2295
2296 if (cu_index >= per_bfd->all_comp_units.size ())
2297 {
2298 complaint (_(".gdb_index address table has invalid CU number %u"),
2299 (unsigned) cu_index);
2300 continue;
2301 }
2302
2303 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2304 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2305 addrmap_set_empty (mutable_map, lo, hi - 1,
2306 per_bfd->get_cu (cu_index));
2307 }
2308
2309 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2310 &per_bfd->obstack);
2311 }
2312
2313 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2314 to populate given addrmap. Returns true on success, false on
2315 failure. */
2316
2317 static bool
2318 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2319 struct dwarf2_section_info *section,
2320 addrmap *mutable_map)
2321 {
2322 struct objfile *objfile = per_objfile->objfile;
2323 bfd *abfd = objfile->obfd;
2324 struct gdbarch *gdbarch = objfile->arch ();
2325 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2326 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2327
2328 std::unordered_map<sect_offset,
2329 dwarf2_per_cu_data *,
2330 gdb::hash_enum<sect_offset>>
2331 debug_info_offset_to_per_cu;
2332 for (const auto &per_cu : per_bfd->all_comp_units)
2333 {
2334 /* A TU will not need aranges, and skipping them here is an easy
2335 way of ignoring .debug_types -- and possibly seeing a
2336 duplicate section offset -- entirely. The same applies to
2337 units coming from a dwz file. */
2338 if (per_cu->is_debug_types || per_cu->is_dwz)
2339 continue;
2340
2341 const auto insertpair
2342 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2343 per_cu.get ());
2344 if (!insertpair.second)
2345 {
2346 warning (_("Section .debug_aranges in %s has duplicate "
2347 "debug_info_offset %s, ignoring .debug_aranges."),
2348 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2349 return false;
2350 }
2351 }
2352
2353 section->read (objfile);
2354
2355 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2356
2357 const gdb_byte *addr = section->buffer;
2358
2359 while (addr < section->buffer + section->size)
2360 {
2361 const gdb_byte *const entry_addr = addr;
2362 unsigned int bytes_read;
2363
2364 const LONGEST entry_length = read_initial_length (abfd, addr,
2365 &bytes_read);
2366 addr += bytes_read;
2367
2368 const gdb_byte *const entry_end = addr + entry_length;
2369 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2370 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2371 if (addr + entry_length > section->buffer + section->size)
2372 {
2373 warning (_("Section .debug_aranges in %s entry at offset %s "
2374 "length %s exceeds section length %s, "
2375 "ignoring .debug_aranges."),
2376 objfile_name (objfile),
2377 plongest (entry_addr - section->buffer),
2378 plongest (bytes_read + entry_length),
2379 pulongest (section->size));
2380 return false;
2381 }
2382
2383 /* The version number. */
2384 const uint16_t version = read_2_bytes (abfd, addr);
2385 addr += 2;
2386 if (version != 2)
2387 {
2388 warning (_("Section .debug_aranges in %s entry at offset %s "
2389 "has unsupported version %d, ignoring .debug_aranges."),
2390 objfile_name (objfile),
2391 plongest (entry_addr - section->buffer), version);
2392 return false;
2393 }
2394
2395 const uint64_t debug_info_offset
2396 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2397 addr += offset_size;
2398 const auto per_cu_it
2399 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2400 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2401 {
2402 warning (_("Section .debug_aranges in %s entry at offset %s "
2403 "debug_info_offset %s does not exists, "
2404 "ignoring .debug_aranges."),
2405 objfile_name (objfile),
2406 plongest (entry_addr - section->buffer),
2407 pulongest (debug_info_offset));
2408 return false;
2409 }
2410 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2411
2412 const uint8_t address_size = *addr++;
2413 if (address_size < 1 || address_size > 8)
2414 {
2415 warning (_("Section .debug_aranges in %s entry at offset %s "
2416 "address_size %u is invalid, ignoring .debug_aranges."),
2417 objfile_name (objfile),
2418 plongest (entry_addr - section->buffer), address_size);
2419 return false;
2420 }
2421
2422 const uint8_t segment_selector_size = *addr++;
2423 if (segment_selector_size != 0)
2424 {
2425 warning (_("Section .debug_aranges in %s entry at offset %s "
2426 "segment_selector_size %u is not supported, "
2427 "ignoring .debug_aranges."),
2428 objfile_name (objfile),
2429 plongest (entry_addr - section->buffer),
2430 segment_selector_size);
2431 return false;
2432 }
2433
2434 /* Must pad to an alignment boundary that is twice the address
2435 size. It is undocumented by the DWARF standard but GCC does
2436 use it. However, not every compiler does this. We can see
2437 whether it has happened by looking at the total length of the
2438 contents of the aranges for this CU -- it if isn't a multiple
2439 of twice the address size, then we skip any leftover
2440 bytes. */
2441 addr += (entry_end - addr) % (2 * address_size);
2442
2443 while (addr < entry_end)
2444 {
2445 if (addr + 2 * address_size > entry_end)
2446 {
2447 warning (_("Section .debug_aranges in %s entry at offset %s "
2448 "address list is not properly terminated, "
2449 "ignoring .debug_aranges."),
2450 objfile_name (objfile),
2451 plongest (entry_addr - section->buffer));
2452 return false;
2453 }
2454 ULONGEST start = extract_unsigned_integer (addr, address_size,
2455 dwarf5_byte_order);
2456 addr += address_size;
2457 ULONGEST length = extract_unsigned_integer (addr, address_size,
2458 dwarf5_byte_order);
2459 addr += address_size;
2460 if (start == 0 && length == 0)
2461 {
2462 /* This can happen on some targets with --gc-sections.
2463 This pair of values is also used to mark the end of
2464 the entries for a given CU, but we ignore it and
2465 instead handle termination using the check at the top
2466 of the loop. */
2467 continue;
2468 }
2469 if (start == 0 && !per_bfd->has_section_at_zero)
2470 {
2471 /* Symbol was eliminated due to a COMDAT group. */
2472 continue;
2473 }
2474 ULONGEST end = start + length;
2475 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2476 - baseaddr);
2477 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2478 - baseaddr);
2479 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2480 }
2481
2482 per_cu->addresses_seen = true;
2483 }
2484
2485 return true;
2486 }
2487
2488 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2489 populate the psymtabs_addrmap. */
2490
2491 static void
2492 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2493 struct dwarf2_section_info *section)
2494 {
2495 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2496
2497 auto_obstack temp_obstack;
2498 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2499
2500 if (read_addrmap_from_aranges (per_objfile, section, mutable_map))
2501 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2502 &per_bfd->obstack);
2503 }
2504
2505 /* A helper function that reads the .gdb_index from BUFFER and fills
2506 in MAP. FILENAME is the name of the file containing the data;
2507 it is used for error reporting. DEPRECATED_OK is true if it is
2508 ok to use deprecated sections.
2509
2510 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2511 out parameters that are filled in with information about the CU and
2512 TU lists in the section.
2513
2514 Returns true if all went well, false otherwise. */
2515
2516 static bool
2517 read_gdb_index_from_buffer (const char *filename,
2518 bool deprecated_ok,
2519 gdb::array_view<const gdb_byte> buffer,
2520 struct mapped_index *map,
2521 const gdb_byte **cu_list,
2522 offset_type *cu_list_elements,
2523 const gdb_byte **types_list,
2524 offset_type *types_list_elements)
2525 {
2526 const gdb_byte *addr = &buffer[0];
2527 offset_view metadata (buffer);
2528
2529 /* Version check. */
2530 offset_type version = metadata[0];
2531 /* Versions earlier than 3 emitted every copy of a psymbol. This
2532 causes the index to behave very poorly for certain requests. Version 3
2533 contained incomplete addrmap. So, it seems better to just ignore such
2534 indices. */
2535 if (version < 4)
2536 {
2537 static int warning_printed = 0;
2538 if (!warning_printed)
2539 {
2540 warning (_("Skipping obsolete .gdb_index section in %s."),
2541 filename);
2542 warning_printed = 1;
2543 }
2544 return 0;
2545 }
2546 /* Index version 4 uses a different hash function than index version
2547 5 and later.
2548
2549 Versions earlier than 6 did not emit psymbols for inlined
2550 functions. Using these files will cause GDB not to be able to
2551 set breakpoints on inlined functions by name, so we ignore these
2552 indices unless the user has done
2553 "set use-deprecated-index-sections on". */
2554 if (version < 6 && !deprecated_ok)
2555 {
2556 static int warning_printed = 0;
2557 if (!warning_printed)
2558 {
2559 warning (_("\
2560 Skipping deprecated .gdb_index section in %s.\n\
2561 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2562 to use the section anyway."),
2563 filename);
2564 warning_printed = 1;
2565 }
2566 return 0;
2567 }
2568 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2569 of the TU (for symbols coming from TUs),
2570 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2571 Plus gold-generated indices can have duplicate entries for global symbols,
2572 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2573 These are just performance bugs, and we can't distinguish gdb-generated
2574 indices from gold-generated ones, so issue no warning here. */
2575
2576 /* Indexes with higher version than the one supported by GDB may be no
2577 longer backward compatible. */
2578 if (version > 8)
2579 return 0;
2580
2581 map->version = version;
2582
2583 int i = 1;
2584 *cu_list = addr + metadata[i];
2585 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2586 ++i;
2587
2588 *types_list = addr + metadata[i];
2589 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2590 ++i;
2591
2592 const gdb_byte *address_table = addr + metadata[i];
2593 const gdb_byte *address_table_end = addr + metadata[i + 1];
2594 map->address_table
2595 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2596 ++i;
2597
2598 const gdb_byte *symbol_table = addr + metadata[i];
2599 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2600 map->symbol_table
2601 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2602 symbol_table_end));
2603
2604 ++i;
2605 map->constant_pool = buffer.slice (metadata[i]);
2606
2607 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2608 {
2609 /* An empty constant pool implies that all symbol table entries are
2610 empty. Make map->symbol_table.empty () == true. */
2611 map->symbol_table
2612 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2613 symbol_table));
2614 }
2615
2616 return 1;
2617 }
2618
2619 /* Callback types for dwarf2_read_gdb_index. */
2620
2621 typedef gdb::function_view
2622 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2623 get_gdb_index_contents_ftype;
2624 typedef gdb::function_view
2625 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2626 get_gdb_index_contents_dwz_ftype;
2627
2628 /* Read .gdb_index. If everything went ok, initialize the "quick"
2629 elements of all the CUs and return 1. Otherwise, return 0. */
2630
2631 static int
2632 dwarf2_read_gdb_index
2633 (dwarf2_per_objfile *per_objfile,
2634 get_gdb_index_contents_ftype get_gdb_index_contents,
2635 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2636 {
2637 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2638 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2639 struct dwz_file *dwz;
2640 struct objfile *objfile = per_objfile->objfile;
2641 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2642
2643 gdb::array_view<const gdb_byte> main_index_contents
2644 = get_gdb_index_contents (objfile, per_bfd);
2645
2646 if (main_index_contents.empty ())
2647 return 0;
2648
2649 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2650 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2651 use_deprecated_index_sections,
2652 main_index_contents, map.get (), &cu_list,
2653 &cu_list_elements, &types_list,
2654 &types_list_elements))
2655 return 0;
2656
2657 /* Don't use the index if it's empty. */
2658 if (map->symbol_table.empty ())
2659 return 0;
2660
2661 /* If there is a .dwz file, read it so we can get its CU list as
2662 well. */
2663 dwz = dwarf2_get_dwz_file (per_bfd);
2664 if (dwz != NULL)
2665 {
2666 struct mapped_index dwz_map;
2667 const gdb_byte *dwz_types_ignore;
2668 offset_type dwz_types_elements_ignore;
2669
2670 gdb::array_view<const gdb_byte> dwz_index_content
2671 = get_gdb_index_contents_dwz (objfile, dwz);
2672
2673 if (dwz_index_content.empty ())
2674 return 0;
2675
2676 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2677 1, dwz_index_content, &dwz_map,
2678 &dwz_list, &dwz_list_elements,
2679 &dwz_types_ignore,
2680 &dwz_types_elements_ignore))
2681 {
2682 warning (_("could not read '.gdb_index' section from %s; skipping"),
2683 bfd_get_filename (dwz->dwz_bfd.get ()));
2684 return 0;
2685 }
2686 }
2687
2688 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2689 dwz_list_elements);
2690
2691 if (types_list_elements)
2692 {
2693 /* We can only handle a single .debug_types when we have an
2694 index. */
2695 if (per_bfd->types.size () != 1)
2696 return 0;
2697
2698 dwarf2_section_info *section = &per_bfd->types[0];
2699
2700 create_signatured_type_table_from_index (per_bfd, section, types_list,
2701 types_list_elements);
2702 }
2703
2704 create_addrmap_from_index (per_objfile, map.get ());
2705
2706 per_bfd->index_table = std::move (map);
2707 per_bfd->quick_file_names_table =
2708 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2709
2710 return 1;
2711 }
2712
2713 /* die_reader_func for dw2_get_file_names. */
2714
2715 static void
2716 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2717 struct die_info *comp_unit_die)
2718 {
2719 struct dwarf2_cu *cu = reader->cu;
2720 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2721 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2722 struct dwarf2_per_cu_data *lh_cu;
2723 struct attribute *attr;
2724 void **slot;
2725 struct quick_file_names *qfn;
2726
2727 gdb_assert (! this_cu->is_debug_types);
2728
2729 this_cu->files_read = true;
2730 /* Our callers never want to match partial units -- instead they
2731 will match the enclosing full CU. */
2732 if (comp_unit_die->tag == DW_TAG_partial_unit)
2733 return;
2734
2735 lh_cu = this_cu;
2736 slot = NULL;
2737
2738 line_header_up lh;
2739 sect_offset line_offset {};
2740
2741 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2742 if (attr != nullptr && attr->form_is_unsigned ())
2743 {
2744 struct quick_file_names find_entry;
2745
2746 line_offset = (sect_offset) attr->as_unsigned ();
2747
2748 /* We may have already read in this line header (TU line header sharing).
2749 If we have we're done. */
2750 find_entry.hash.dwo_unit = cu->dwo_unit;
2751 find_entry.hash.line_sect_off = line_offset;
2752 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2753 &find_entry, INSERT);
2754 if (*slot != NULL)
2755 {
2756 lh_cu->file_names = (struct quick_file_names *) *slot;
2757 return;
2758 }
2759
2760 lh = dwarf_decode_line_header (line_offset, cu);
2761 }
2762
2763 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
2764
2765 int offset = 0;
2766 if (!fnd.is_unknown ())
2767 ++offset;
2768 else if (lh == nullptr)
2769 return;
2770
2771 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2772 qfn->hash.dwo_unit = cu->dwo_unit;
2773 qfn->hash.line_sect_off = line_offset;
2774 /* There may not be a DW_AT_stmt_list. */
2775 if (slot != nullptr)
2776 *slot = qfn;
2777
2778 std::vector<const char *> include_names;
2779 if (lh != nullptr)
2780 {
2781 for (const auto &entry : lh->file_names ())
2782 {
2783 std::string name_holder;
2784 const char *include_name =
2785 compute_include_file_name (lh.get (), entry, fnd, name_holder);
2786 if (include_name != nullptr)
2787 {
2788 include_name = per_objfile->objfile->intern (include_name);
2789 include_names.push_back (include_name);
2790 }
2791 }
2792 }
2793
2794 qfn->num_file_names = offset + include_names.size ();
2795 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
2796 qfn->file_names =
2797 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2798 qfn->num_file_names);
2799 if (offset != 0)
2800 qfn->file_names[0] = xstrdup (fnd.get_name ());
2801
2802 if (!include_names.empty ())
2803 memcpy (&qfn->file_names[offset], include_names.data (),
2804 include_names.size () * sizeof (const char *));
2805
2806 qfn->real_names = NULL;
2807
2808 lh_cu->file_names = qfn;
2809 }
2810
2811 /* A helper for the "quick" functions which attempts to read the line
2812 table for THIS_CU. */
2813
2814 static struct quick_file_names *
2815 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2816 dwarf2_per_objfile *per_objfile)
2817 {
2818 /* This should never be called for TUs. */
2819 gdb_assert (! this_cu->is_debug_types);
2820
2821 if (this_cu->files_read)
2822 return this_cu->file_names;
2823
2824 cutu_reader reader (this_cu, per_objfile);
2825 if (!reader.dummy_p)
2826 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2827
2828 return this_cu->file_names;
2829 }
2830
2831 /* A helper for the "quick" functions which computes and caches the
2832 real path for a given file name from the line table. */
2833
2834 static const char *
2835 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2836 struct quick_file_names *qfn, int index)
2837 {
2838 if (qfn->real_names == NULL)
2839 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
2840 qfn->num_file_names, const char *);
2841
2842 if (qfn->real_names[index] == NULL)
2843 {
2844 const char *dirname = nullptr;
2845
2846 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
2847 dirname = qfn->comp_dir;
2848
2849 gdb::unique_xmalloc_ptr<char> fullname;
2850 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
2851
2852 qfn->real_names[index] = fullname.release ();
2853 }
2854
2855 return qfn->real_names[index];
2856 }
2857
2858 struct symtab *
2859 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
2860 {
2861 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2862 dwarf2_per_cu_data *dwarf_cu
2863 = per_objfile->per_bfd->all_comp_units.back ().get ();
2864 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2865
2866 if (cust == NULL)
2867 return NULL;
2868
2869 return cust->primary_filetab ();
2870 }
2871
2872 /* See read.h. */
2873
2874 void
2875 dwarf2_per_cu_data::free_cached_file_names ()
2876 {
2877 if (fnd != nullptr)
2878 fnd->forget_fullname ();
2879
2880 if (per_bfd == nullptr)
2881 return;
2882
2883 struct quick_file_names *file_data = file_names;
2884 if (file_data != nullptr && file_data->real_names != nullptr)
2885 {
2886 for (int i = 0; i < file_data->num_file_names; ++i)
2887 {
2888 xfree ((void *) file_data->real_names[i]);
2889 file_data->real_names[i] = nullptr;
2890 }
2891 }
2892 }
2893
2894 void
2895 dwarf2_base_index_functions::forget_cached_source_info
2896 (struct objfile *objfile)
2897 {
2898 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2899
2900 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
2901 per_cu->free_cached_file_names ();
2902 }
2903
2904 /* Struct used to manage iterating over all CUs looking for a symbol. */
2905
2906 struct dw2_symtab_iterator
2907 {
2908 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2909 dwarf2_per_objfile *per_objfile;
2910 /* If set, only look for symbols that match that block. Valid values are
2911 GLOBAL_BLOCK and STATIC_BLOCK. */
2912 gdb::optional<block_enum> block_index;
2913 /* The kind of symbol we're looking for. */
2914 domain_enum domain;
2915 /* The list of CUs from the index entry of the symbol,
2916 or NULL if not found. */
2917 offset_view vec;
2918 /* The next element in VEC to look at. */
2919 int next;
2920 /* The number of elements in VEC, or zero if there is no match. */
2921 int length;
2922 /* Have we seen a global version of the symbol?
2923 If so we can ignore all further global instances.
2924 This is to work around gold/15646, inefficient gold-generated
2925 indices. */
2926 int global_seen;
2927 };
2928
2929 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2930
2931 static void
2932 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
2933 dwarf2_per_objfile *per_objfile,
2934 gdb::optional<block_enum> block_index,
2935 domain_enum domain, offset_type namei,
2936 mapped_index &index)
2937 {
2938 iter->per_objfile = per_objfile;
2939 iter->block_index = block_index;
2940 iter->domain = domain;
2941 iter->next = 0;
2942 iter->global_seen = 0;
2943 iter->vec = {};
2944 iter->length = 0;
2945
2946 gdb_assert (!index.symbol_name_slot_invalid (namei));
2947 offset_type vec_idx = index.symbol_vec_index (namei);
2948
2949 iter->vec = offset_view (index.constant_pool.slice (vec_idx));
2950 iter->length = iter->vec[0];
2951 }
2952
2953 /* Return the next matching CU or NULL if there are no more. */
2954
2955 static struct dwarf2_per_cu_data *
2956 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter,
2957 mapped_index &index)
2958 {
2959 dwarf2_per_objfile *per_objfile = iter->per_objfile;
2960
2961 for ( ; iter->next < iter->length; ++iter->next)
2962 {
2963 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
2964 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2965 gdb_index_symbol_kind symbol_kind =
2966 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2967 /* Only check the symbol attributes if they're present.
2968 Indices prior to version 7 don't record them,
2969 and indices >= 7 may elide them for certain symbols
2970 (gold does this). */
2971 int attrs_valid = (index.version >= 7
2972 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
2973
2974 /* Don't crash on bad data. */
2975 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2976 {
2977 complaint (_(".gdb_index entry has bad CU index"
2978 " [in module %s]"), objfile_name (per_objfile->objfile));
2979 continue;
2980 }
2981
2982 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
2983
2984 /* Skip if already read in. */
2985 if (per_objfile->symtab_set_p (per_cu))
2986 continue;
2987
2988 /* Check static vs global. */
2989 if (attrs_valid)
2990 {
2991 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2992
2993 if (iter->block_index.has_value ())
2994 {
2995 bool want_static = *iter->block_index == STATIC_BLOCK;
2996
2997 if (is_static != want_static)
2998 continue;
2999 }
3000
3001 /* Work around gold/15646. */
3002 if (!is_static
3003 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3004 {
3005 if (iter->global_seen)
3006 continue;
3007
3008 iter->global_seen = 1;
3009 }
3010 }
3011
3012 /* Only check the symbol's kind if it has one. */
3013 if (attrs_valid)
3014 {
3015 switch (iter->domain)
3016 {
3017 case VAR_DOMAIN:
3018 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3019 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3020 /* Some types are also in VAR_DOMAIN. */
3021 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3022 continue;
3023 break;
3024 case STRUCT_DOMAIN:
3025 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3026 continue;
3027 break;
3028 case LABEL_DOMAIN:
3029 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3030 continue;
3031 break;
3032 case MODULE_DOMAIN:
3033 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3034 continue;
3035 break;
3036 default:
3037 break;
3038 }
3039 }
3040
3041 ++iter->next;
3042 return per_cu;
3043 }
3044
3045 return NULL;
3046 }
3047
3048 void
3049 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3050 bool print_bcache)
3051 {
3052 if (print_bcache)
3053 return;
3054
3055 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3056 int total = per_objfile->per_bfd->all_comp_units.size ();
3057 int count = 0;
3058
3059 for (int i = 0; i < total; ++i)
3060 {
3061 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3062
3063 if (!per_objfile->symtab_set_p (per_cu))
3064 ++count;
3065 }
3066 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3067 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3068 }
3069
3070 /* This dumps minimal information about the index.
3071 It is called via "mt print objfiles".
3072 One use is to verify .gdb_index has been loaded by the
3073 gdb.dwarf2/gdb-index.exp testcase. */
3074
3075 void
3076 dwarf2_gdb_index::dump (struct objfile *objfile)
3077 {
3078 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3079
3080 mapped_index *index = (static_cast<mapped_index *>
3081 (per_objfile->per_bfd->index_table.get ()));
3082 gdb_printf (".gdb_index: version %d\n", index->version);
3083 gdb_printf ("\n");
3084 }
3085
3086 void
3087 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3088 {
3089 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3090 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3091
3092 for (int i = 0; i < total_units; ++i)
3093 {
3094 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3095
3096 /* We don't want to directly expand a partial CU, because if we
3097 read it with the wrong language, then assertion failures can
3098 be triggered later on. See PR symtab/23010. So, tell
3099 dw2_instantiate_symtab to skip partial CUs -- any important
3100 partial CU will be read via DW_TAG_imported_unit anyway. */
3101 dw2_instantiate_symtab (per_cu, per_objfile, true);
3102 }
3103 }
3104
3105 static bool
3106 dw2_expand_symtabs_matching_symbol
3107 (mapped_index_base &index,
3108 const lookup_name_info &lookup_name_in,
3109 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3110 gdb::function_view<bool (offset_type)> match_callback,
3111 dwarf2_per_objfile *per_objfile);
3112
3113 static bool
3114 dw2_expand_symtabs_matching_one
3115 (dwarf2_per_cu_data *per_cu,
3116 dwarf2_per_objfile *per_objfile,
3117 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3118 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3119
3120 void
3121 dwarf2_gdb_index::expand_matching_symbols
3122 (struct objfile *objfile,
3123 const lookup_name_info &name, domain_enum domain,
3124 int global,
3125 symbol_compare_ftype *ordered_compare)
3126 {
3127 /* Used for Ada. */
3128 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3129
3130 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3131
3132 mapped_index &index
3133 = (static_cast<mapped_index &>
3134 (*per_objfile->per_bfd->index_table.get ()));
3135
3136 const char *match_name = name.ada ().lookup_name ().c_str ();
3137 auto matcher = [&] (const char *symname)
3138 {
3139 if (ordered_compare == nullptr)
3140 return true;
3141 return ordered_compare (symname, match_name) == 0;
3142 };
3143
3144 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3145 [&] (offset_type namei)
3146 {
3147 struct dw2_symtab_iterator iter;
3148 struct dwarf2_per_cu_data *per_cu;
3149
3150 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain, namei,
3151 index);
3152 while ((per_cu = dw2_symtab_iter_next (&iter, index)) != NULL)
3153 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3154 nullptr);
3155 return true;
3156 }, per_objfile);
3157 }
3158
3159 /* Starting from a search name, return the string that finds the upper
3160 bound of all strings that start with SEARCH_NAME in a sorted name
3161 list. Returns the empty string to indicate that the upper bound is
3162 the end of the list. */
3163
3164 static std::string
3165 make_sort_after_prefix_name (const char *search_name)
3166 {
3167 /* When looking to complete "func", we find the upper bound of all
3168 symbols that start with "func" by looking for where we'd insert
3169 the closest string that would follow "func" in lexicographical
3170 order. Usually, that's "func"-with-last-character-incremented,
3171 i.e. "fund". Mind non-ASCII characters, though. Usually those
3172 will be UTF-8 multi-byte sequences, but we can't be certain.
3173 Especially mind the 0xff character, which is a valid character in
3174 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3175 rule out compilers allowing it in identifiers. Note that
3176 conveniently, strcmp/strcasecmp are specified to compare
3177 characters interpreted as unsigned char. So what we do is treat
3178 the whole string as a base 256 number composed of a sequence of
3179 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3180 to 0, and carries 1 to the following more-significant position.
3181 If the very first character in SEARCH_NAME ends up incremented
3182 and carries/overflows, then the upper bound is the end of the
3183 list. The string after the empty string is also the empty
3184 string.
3185
3186 Some examples of this operation:
3187
3188 SEARCH_NAME => "+1" RESULT
3189
3190 "abc" => "abd"
3191 "ab\xff" => "ac"
3192 "\xff" "a" "\xff" => "\xff" "b"
3193 "\xff" => ""
3194 "\xff\xff" => ""
3195 "" => ""
3196
3197 Then, with these symbols for example:
3198
3199 func
3200 func1
3201 fund
3202
3203 completing "func" looks for symbols between "func" and
3204 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3205 which finds "func" and "func1", but not "fund".
3206
3207 And with:
3208
3209 funcÿ (Latin1 'ÿ' [0xff])
3210 funcÿ1
3211 fund
3212
3213 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3214 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3215
3216 And with:
3217
3218 ÿÿ (Latin1 'ÿ' [0xff])
3219 ÿÿ1
3220
3221 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3222 the end of the list.
3223 */
3224 std::string after = search_name;
3225 while (!after.empty () && (unsigned char) after.back () == 0xff)
3226 after.pop_back ();
3227 if (!after.empty ())
3228 after.back () = (unsigned char) after.back () + 1;
3229 return after;
3230 }
3231
3232 /* See declaration. */
3233
3234 std::pair<std::vector<name_component>::const_iterator,
3235 std::vector<name_component>::const_iterator>
3236 mapped_index_base::find_name_components_bounds
3237 (const lookup_name_info &lookup_name_without_params, language lang,
3238 dwarf2_per_objfile *per_objfile) const
3239 {
3240 auto *name_cmp
3241 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3242
3243 const char *lang_name
3244 = lookup_name_without_params.language_lookup_name (lang);
3245
3246 /* Comparison function object for lower_bound that matches against a
3247 given symbol name. */
3248 auto lookup_compare_lower = [&] (const name_component &elem,
3249 const char *name)
3250 {
3251 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3252 const char *elem_name = elem_qualified + elem.name_offset;
3253 return name_cmp (elem_name, name) < 0;
3254 };
3255
3256 /* Comparison function object for upper_bound that matches against a
3257 given symbol name. */
3258 auto lookup_compare_upper = [&] (const char *name,
3259 const name_component &elem)
3260 {
3261 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3262 const char *elem_name = elem_qualified + elem.name_offset;
3263 return name_cmp (name, elem_name) < 0;
3264 };
3265
3266 auto begin = this->name_components.begin ();
3267 auto end = this->name_components.end ();
3268
3269 /* Find the lower bound. */
3270 auto lower = [&] ()
3271 {
3272 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3273 return begin;
3274 else
3275 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3276 } ();
3277
3278 /* Find the upper bound. */
3279 auto upper = [&] ()
3280 {
3281 if (lookup_name_without_params.completion_mode ())
3282 {
3283 /* In completion mode, we want UPPER to point past all
3284 symbols names that have the same prefix. I.e., with
3285 these symbols, and completing "func":
3286
3287 function << lower bound
3288 function1
3289 other_function << upper bound
3290
3291 We find the upper bound by looking for the insertion
3292 point of "func"-with-last-character-incremented,
3293 i.e. "fund". */
3294 std::string after = make_sort_after_prefix_name (lang_name);
3295 if (after.empty ())
3296 return end;
3297 return std::lower_bound (lower, end, after.c_str (),
3298 lookup_compare_lower);
3299 }
3300 else
3301 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3302 } ();
3303
3304 return {lower, upper};
3305 }
3306
3307 /* See declaration. */
3308
3309 void
3310 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3311 {
3312 if (!this->name_components.empty ())
3313 return;
3314
3315 this->name_components_casing = case_sensitivity;
3316 auto *name_cmp
3317 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3318
3319 /* The code below only knows how to break apart components of C++
3320 symbol names (and other languages that use '::' as
3321 namespace/module separator) and Ada symbol names. */
3322 auto count = this->symbol_name_count ();
3323 for (offset_type idx = 0; idx < count; idx++)
3324 {
3325 if (this->symbol_name_slot_invalid (idx))
3326 continue;
3327
3328 const char *name = this->symbol_name_at (idx, per_objfile);
3329
3330 /* Add each name component to the name component table. */
3331 unsigned int previous_len = 0;
3332
3333 if (strstr (name, "::") != nullptr)
3334 {
3335 for (unsigned int current_len = cp_find_first_component (name);
3336 name[current_len] != '\0';
3337 current_len += cp_find_first_component (name + current_len))
3338 {
3339 gdb_assert (name[current_len] == ':');
3340 this->name_components.push_back ({previous_len, idx});
3341 /* Skip the '::'. */
3342 current_len += 2;
3343 previous_len = current_len;
3344 }
3345 }
3346 else
3347 {
3348 /* Handle the Ada encoded (aka mangled) form here. */
3349 for (const char *iter = strstr (name, "__");
3350 iter != nullptr;
3351 iter = strstr (iter, "__"))
3352 {
3353 this->name_components.push_back ({previous_len, idx});
3354 iter += 2;
3355 previous_len = iter - name;
3356 }
3357 }
3358
3359 this->name_components.push_back ({previous_len, idx});
3360 }
3361
3362 /* Sort name_components elements by name. */
3363 auto name_comp_compare = [&] (const name_component &left,
3364 const name_component &right)
3365 {
3366 const char *left_qualified
3367 = this->symbol_name_at (left.idx, per_objfile);
3368 const char *right_qualified
3369 = this->symbol_name_at (right.idx, per_objfile);
3370
3371 const char *left_name = left_qualified + left.name_offset;
3372 const char *right_name = right_qualified + right.name_offset;
3373
3374 return name_cmp (left_name, right_name) < 0;
3375 };
3376
3377 std::sort (this->name_components.begin (),
3378 this->name_components.end (),
3379 name_comp_compare);
3380 }
3381
3382 /* Helper for dw2_expand_symtabs_matching that works with a
3383 mapped_index_base instead of the containing objfile. This is split
3384 to a separate function in order to be able to unit test the
3385 name_components matching using a mock mapped_index_base. For each
3386 symbol name that matches, calls MATCH_CALLBACK, passing it the
3387 symbol's index in the mapped_index_base symbol table. */
3388
3389 static bool
3390 dw2_expand_symtabs_matching_symbol
3391 (mapped_index_base &index,
3392 const lookup_name_info &lookup_name_in,
3393 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3394 gdb::function_view<bool (offset_type)> match_callback,
3395 dwarf2_per_objfile *per_objfile)
3396 {
3397 lookup_name_info lookup_name_without_params
3398 = lookup_name_in.make_ignore_params ();
3399
3400 /* Build the symbol name component sorted vector, if we haven't
3401 yet. */
3402 index.build_name_components (per_objfile);
3403
3404 /* The same symbol may appear more than once in the range though.
3405 E.g., if we're looking for symbols that complete "w", and we have
3406 a symbol named "w1::w2", we'll find the two name components for
3407 that same symbol in the range. To be sure we only call the
3408 callback once per symbol, we first collect the symbol name
3409 indexes that matched in a temporary vector and ignore
3410 duplicates. */
3411 std::vector<offset_type> matches;
3412
3413 struct name_and_matcher
3414 {
3415 symbol_name_matcher_ftype *matcher;
3416 const char *name;
3417
3418 bool operator== (const name_and_matcher &other) const
3419 {
3420 return matcher == other.matcher && strcmp (name, other.name) == 0;
3421 }
3422 };
3423
3424 /* A vector holding all the different symbol name matchers, for all
3425 languages. */
3426 std::vector<name_and_matcher> matchers;
3427
3428 for (int i = 0; i < nr_languages; i++)
3429 {
3430 enum language lang_e = (enum language) i;
3431
3432 const language_defn *lang = language_def (lang_e);
3433 symbol_name_matcher_ftype *name_matcher
3434 = lang->get_symbol_name_matcher (lookup_name_without_params);
3435
3436 name_and_matcher key {
3437 name_matcher,
3438 lookup_name_without_params.language_lookup_name (lang_e)
3439 };
3440
3441 /* Don't insert the same comparison routine more than once.
3442 Note that we do this linear walk. This is not a problem in
3443 practice because the number of supported languages is
3444 low. */
3445 if (std::find (matchers.begin (), matchers.end (), key)
3446 != matchers.end ())
3447 continue;
3448 matchers.push_back (std::move (key));
3449
3450 auto bounds
3451 = index.find_name_components_bounds (lookup_name_without_params,
3452 lang_e, per_objfile);
3453
3454 /* Now for each symbol name in range, check to see if we have a name
3455 match, and if so, call the MATCH_CALLBACK callback. */
3456
3457 for (; bounds.first != bounds.second; ++bounds.first)
3458 {
3459 const char *qualified
3460 = index.symbol_name_at (bounds.first->idx, per_objfile);
3461
3462 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3463 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3464 continue;
3465
3466 matches.push_back (bounds.first->idx);
3467 }
3468 }
3469
3470 std::sort (matches.begin (), matches.end ());
3471
3472 /* Finally call the callback, once per match. */
3473 ULONGEST prev = -1;
3474 bool result = true;
3475 for (offset_type idx : matches)
3476 {
3477 if (prev != idx)
3478 {
3479 if (!match_callback (idx))
3480 {
3481 result = false;
3482 break;
3483 }
3484 prev = idx;
3485 }
3486 }
3487
3488 /* Above we use a type wider than idx's for 'prev', since 0 and
3489 (offset_type)-1 are both possible values. */
3490 static_assert (sizeof (prev) > sizeof (offset_type), "");
3491
3492 return result;
3493 }
3494
3495 #if GDB_SELF_TEST
3496
3497 namespace selftests { namespace dw2_expand_symtabs_matching {
3498
3499 /* A mock .gdb_index/.debug_names-like name index table, enough to
3500 exercise dw2_expand_symtabs_matching_symbol, which works with the
3501 mapped_index_base interface. Builds an index from the symbol list
3502 passed as parameter to the constructor. */
3503 class mock_mapped_index : public mapped_index_base
3504 {
3505 public:
3506 mock_mapped_index (gdb::array_view<const char *> symbols)
3507 : m_symbol_table (symbols)
3508 {}
3509
3510 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3511
3512 /* Return the number of names in the symbol table. */
3513 size_t symbol_name_count () const override
3514 {
3515 return m_symbol_table.size ();
3516 }
3517
3518 /* Get the name of the symbol at IDX in the symbol table. */
3519 const char *symbol_name_at
3520 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3521 {
3522 return m_symbol_table[idx];
3523 }
3524
3525 quick_symbol_functions_up make_quick_functions () const override
3526 {
3527 return nullptr;
3528 }
3529
3530 private:
3531 gdb::array_view<const char *> m_symbol_table;
3532 };
3533
3534 /* Convenience function that converts a NULL pointer to a "<null>"
3535 string, to pass to print routines. */
3536
3537 static const char *
3538 string_or_null (const char *str)
3539 {
3540 return str != NULL ? str : "<null>";
3541 }
3542
3543 /* Check if a lookup_name_info built from
3544 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3545 index. EXPECTED_LIST is the list of expected matches, in expected
3546 matching order. If no match expected, then an empty list is
3547 specified. Returns true on success. On failure prints a warning
3548 indicating the file:line that failed, and returns false. */
3549
3550 static bool
3551 check_match (const char *file, int line,
3552 mock_mapped_index &mock_index,
3553 const char *name, symbol_name_match_type match_type,
3554 bool completion_mode,
3555 std::initializer_list<const char *> expected_list,
3556 dwarf2_per_objfile *per_objfile)
3557 {
3558 lookup_name_info lookup_name (name, match_type, completion_mode);
3559
3560 bool matched = true;
3561
3562 auto mismatch = [&] (const char *expected_str,
3563 const char *got)
3564 {
3565 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3566 "expected=\"%s\", got=\"%s\"\n"),
3567 file, line,
3568 (match_type == symbol_name_match_type::FULL
3569 ? "FULL" : "WILD"),
3570 name, string_or_null (expected_str), string_or_null (got));
3571 matched = false;
3572 };
3573
3574 auto expected_it = expected_list.begin ();
3575 auto expected_end = expected_list.end ();
3576
3577 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3578 nullptr,
3579 [&] (offset_type idx)
3580 {
3581 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3582 const char *expected_str
3583 = expected_it == expected_end ? NULL : *expected_it++;
3584
3585 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3586 mismatch (expected_str, matched_name);
3587 return true;
3588 }, per_objfile);
3589
3590 const char *expected_str
3591 = expected_it == expected_end ? NULL : *expected_it++;
3592 if (expected_str != NULL)
3593 mismatch (expected_str, NULL);
3594
3595 return matched;
3596 }
3597
3598 /* The symbols added to the mock mapped_index for testing (in
3599 canonical form). */
3600 static const char *test_symbols[] = {
3601 "function",
3602 "std::bar",
3603 "std::zfunction",
3604 "std::zfunction2",
3605 "w1::w2",
3606 "ns::foo<char*>",
3607 "ns::foo<int>",
3608 "ns::foo<long>",
3609 "ns2::tmpl<int>::foo2",
3610 "(anonymous namespace)::A::B::C",
3611
3612 /* These are used to check that the increment-last-char in the
3613 matching algorithm for completion doesn't match "t1_fund" when
3614 completing "t1_func". */
3615 "t1_func",
3616 "t1_func1",
3617 "t1_fund",
3618 "t1_fund1",
3619
3620 /* A UTF-8 name with multi-byte sequences to make sure that
3621 cp-name-parser understands this as a single identifier ("função"
3622 is "function" in PT). */
3623 u8"u8função",
3624
3625 /* \377 (0xff) is Latin1 'ÿ'. */
3626 "yfunc\377",
3627
3628 /* \377 (0xff) is Latin1 'ÿ'. */
3629 "\377",
3630 "\377\377123",
3631
3632 /* A name with all sorts of complications. Starts with "z" to make
3633 it easier for the completion tests below. */
3634 #define Z_SYM_NAME \
3635 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3636 "::tuple<(anonymous namespace)::ui*, " \
3637 "std::default_delete<(anonymous namespace)::ui>, void>"
3638
3639 Z_SYM_NAME
3640 };
3641
3642 /* Returns true if the mapped_index_base::find_name_component_bounds
3643 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3644 in completion mode. */
3645
3646 static bool
3647 check_find_bounds_finds (mapped_index_base &index,
3648 const char *search_name,
3649 gdb::array_view<const char *> expected_syms,
3650 dwarf2_per_objfile *per_objfile)
3651 {
3652 lookup_name_info lookup_name (search_name,
3653 symbol_name_match_type::FULL, true);
3654
3655 auto bounds = index.find_name_components_bounds (lookup_name,
3656 language_cplus,
3657 per_objfile);
3658
3659 size_t distance = std::distance (bounds.first, bounds.second);
3660 if (distance != expected_syms.size ())
3661 return false;
3662
3663 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3664 {
3665 auto nc_elem = bounds.first + exp_elem;
3666 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3667 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3668 return false;
3669 }
3670
3671 return true;
3672 }
3673
3674 /* Test the lower-level mapped_index::find_name_component_bounds
3675 method. */
3676
3677 static void
3678 test_mapped_index_find_name_component_bounds ()
3679 {
3680 mock_mapped_index mock_index (test_symbols);
3681
3682 mock_index.build_name_components (NULL /* per_objfile */);
3683
3684 /* Test the lower-level mapped_index::find_name_component_bounds
3685 method in completion mode. */
3686 {
3687 static const char *expected_syms[] = {
3688 "t1_func",
3689 "t1_func1",
3690 };
3691
3692 SELF_CHECK (check_find_bounds_finds
3693 (mock_index, "t1_func", expected_syms,
3694 NULL /* per_objfile */));
3695 }
3696
3697 /* Check that the increment-last-char in the name matching algorithm
3698 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3699 {
3700 static const char *expected_syms1[] = {
3701 "\377",
3702 "\377\377123",
3703 };
3704 SELF_CHECK (check_find_bounds_finds
3705 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3706
3707 static const char *expected_syms2[] = {
3708 "\377\377123",
3709 };
3710 SELF_CHECK (check_find_bounds_finds
3711 (mock_index, "\377\377", expected_syms2,
3712 NULL /* per_objfile */));
3713 }
3714 }
3715
3716 /* Test dw2_expand_symtabs_matching_symbol. */
3717
3718 static void
3719 test_dw2_expand_symtabs_matching_symbol ()
3720 {
3721 mock_mapped_index mock_index (test_symbols);
3722
3723 /* We let all tests run until the end even if some fails, for debug
3724 convenience. */
3725 bool any_mismatch = false;
3726
3727 /* Create the expected symbols list (an initializer_list). Needed
3728 because lists have commas, and we need to pass them to CHECK,
3729 which is a macro. */
3730 #define EXPECT(...) { __VA_ARGS__ }
3731
3732 /* Wrapper for check_match that passes down the current
3733 __FILE__/__LINE__. */
3734 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3735 any_mismatch |= !check_match (__FILE__, __LINE__, \
3736 mock_index, \
3737 NAME, MATCH_TYPE, COMPLETION_MODE, \
3738 EXPECTED_LIST, NULL)
3739
3740 /* Identity checks. */
3741 for (const char *sym : test_symbols)
3742 {
3743 /* Should be able to match all existing symbols. */
3744 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3745 EXPECT (sym));
3746
3747 /* Should be able to match all existing symbols with
3748 parameters. */
3749 std::string with_params = std::string (sym) + "(int)";
3750 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3751 EXPECT (sym));
3752
3753 /* Should be able to match all existing symbols with
3754 parameters and qualifiers. */
3755 with_params = std::string (sym) + " ( int ) const";
3756 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3757 EXPECT (sym));
3758
3759 /* This should really find sym, but cp-name-parser.y doesn't
3760 know about lvalue/rvalue qualifiers yet. */
3761 with_params = std::string (sym) + " ( int ) &&";
3762 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3763 {});
3764 }
3765
3766 /* Check that the name matching algorithm for completion doesn't get
3767 confused with Latin1 'ÿ' / 0xff. */
3768 {
3769 static const char str[] = "\377";
3770 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3771 EXPECT ("\377", "\377\377123"));
3772 }
3773
3774 /* Check that the increment-last-char in the matching algorithm for
3775 completion doesn't match "t1_fund" when completing "t1_func". */
3776 {
3777 static const char str[] = "t1_func";
3778 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3779 EXPECT ("t1_func", "t1_func1"));
3780 }
3781
3782 /* Check that completion mode works at each prefix of the expected
3783 symbol name. */
3784 {
3785 static const char str[] = "function(int)";
3786 size_t len = strlen (str);
3787 std::string lookup;
3788
3789 for (size_t i = 1; i < len; i++)
3790 {
3791 lookup.assign (str, i);
3792 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3793 EXPECT ("function"));
3794 }
3795 }
3796
3797 /* While "w" is a prefix of both components, the match function
3798 should still only be called once. */
3799 {
3800 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3801 EXPECT ("w1::w2"));
3802 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3803 EXPECT ("w1::w2"));
3804 }
3805
3806 /* Same, with a "complicated" symbol. */
3807 {
3808 static const char str[] = Z_SYM_NAME;
3809 size_t len = strlen (str);
3810 std::string lookup;
3811
3812 for (size_t i = 1; i < len; i++)
3813 {
3814 lookup.assign (str, i);
3815 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3816 EXPECT (Z_SYM_NAME));
3817 }
3818 }
3819
3820 /* In FULL mode, an incomplete symbol doesn't match. */
3821 {
3822 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3823 {});
3824 }
3825
3826 /* A complete symbol with parameters matches any overload, since the
3827 index has no overload info. */
3828 {
3829 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3830 EXPECT ("std::zfunction", "std::zfunction2"));
3831 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
3832 EXPECT ("std::zfunction", "std::zfunction2"));
3833 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
3834 EXPECT ("std::zfunction", "std::zfunction2"));
3835 }
3836
3837 /* Check that whitespace is ignored appropriately. A symbol with a
3838 template argument list. */
3839 {
3840 static const char expected[] = "ns::foo<int>";
3841 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
3842 EXPECT (expected));
3843 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
3844 EXPECT (expected));
3845 }
3846
3847 /* Check that whitespace is ignored appropriately. A symbol with a
3848 template argument list that includes a pointer. */
3849 {
3850 static const char expected[] = "ns::foo<char*>";
3851 /* Try both completion and non-completion modes. */
3852 static const bool completion_mode[2] = {false, true};
3853 for (size_t i = 0; i < 2; i++)
3854 {
3855 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
3856 completion_mode[i], EXPECT (expected));
3857 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
3858 completion_mode[i], EXPECT (expected));
3859
3860 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
3861 completion_mode[i], EXPECT (expected));
3862 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
3863 completion_mode[i], EXPECT (expected));
3864 }
3865 }
3866
3867 {
3868 /* Check method qualifiers are ignored. */
3869 static const char expected[] = "ns::foo<char*>";
3870 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3871 symbol_name_match_type::FULL, true, EXPECT (expected));
3872 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3873 symbol_name_match_type::FULL, true, EXPECT (expected));
3874 CHECK_MATCH ("foo < char * > ( int ) const",
3875 symbol_name_match_type::WILD, true, EXPECT (expected));
3876 CHECK_MATCH ("foo < char * > ( int ) &&",
3877 symbol_name_match_type::WILD, true, EXPECT (expected));
3878 }
3879
3880 /* Test lookup names that don't match anything. */
3881 {
3882 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
3883 {});
3884
3885 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
3886 {});
3887 }
3888
3889 /* Some wild matching tests, exercising "(anonymous namespace)",
3890 which should not be confused with a parameter list. */
3891 {
3892 static const char *syms[] = {
3893 "A::B::C",
3894 "B::C",
3895 "C",
3896 "A :: B :: C ( int )",
3897 "B :: C ( int )",
3898 "C ( int )",
3899 };
3900
3901 for (const char *s : syms)
3902 {
3903 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
3904 EXPECT ("(anonymous namespace)::A::B::C"));
3905 }
3906 }
3907
3908 {
3909 static const char expected[] = "ns2::tmpl<int>::foo2";
3910 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
3911 EXPECT (expected));
3912 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
3913 EXPECT (expected));
3914 }
3915
3916 SELF_CHECK (!any_mismatch);
3917
3918 #undef EXPECT
3919 #undef CHECK_MATCH
3920 }
3921
3922 static void
3923 run_test ()
3924 {
3925 test_mapped_index_find_name_component_bounds ();
3926 test_dw2_expand_symtabs_matching_symbol ();
3927 }
3928
3929 }} // namespace selftests::dw2_expand_symtabs_matching
3930
3931 #endif /* GDB_SELF_TEST */
3932
3933 /* If FILE_MATCHER is NULL or if PER_CU has
3934 dwarf2_per_cu_quick_data::MARK set (see
3935 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3936 EXPANSION_NOTIFY on it. */
3937
3938 static bool
3939 dw2_expand_symtabs_matching_one
3940 (dwarf2_per_cu_data *per_cu,
3941 dwarf2_per_objfile *per_objfile,
3942 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3943 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
3944 {
3945 if (file_matcher == NULL || per_cu->mark)
3946 {
3947 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
3948
3949 compunit_symtab *symtab
3950 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3951 gdb_assert (symtab != nullptr);
3952
3953 if (expansion_notify != NULL && symtab_was_null)
3954 return expansion_notify (symtab);
3955 }
3956 return true;
3957 }
3958
3959 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3960 matched, to expand corresponding CUs that were marked. IDX is the
3961 index of the symbol name that matched. */
3962
3963 static bool
3964 dw2_expand_marked_cus
3965 (dwarf2_per_objfile *per_objfile, offset_type idx,
3966 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3967 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
3968 block_search_flags search_flags,
3969 search_domain kind)
3970 {
3971 offset_type vec_len, vec_idx;
3972 bool global_seen = false;
3973 mapped_index &index
3974 = (static_cast<mapped_index &>
3975 (*per_objfile->per_bfd->index_table.get ()));
3976
3977 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
3978 vec_len = vec[0];
3979 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3980 {
3981 offset_type cu_index_and_attrs = vec[vec_idx + 1];
3982 /* This value is only valid for index versions >= 7. */
3983 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3984 gdb_index_symbol_kind symbol_kind =
3985 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3986 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3987 /* Only check the symbol attributes if they're present.
3988 Indices prior to version 7 don't record them,
3989 and indices >= 7 may elide them for certain symbols
3990 (gold does this). */
3991 int attrs_valid =
3992 (index.version >= 7
3993 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3994
3995 /* Work around gold/15646. */
3996 if (attrs_valid
3997 && !is_static
3998 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3999 {
4000 if (global_seen)
4001 continue;
4002
4003 global_seen = true;
4004 }
4005
4006 /* Only check the symbol's kind if it has one. */
4007 if (attrs_valid)
4008 {
4009 if (is_static)
4010 {
4011 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4012 continue;
4013 }
4014 else
4015 {
4016 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4017 continue;
4018 }
4019
4020 switch (kind)
4021 {
4022 case VARIABLES_DOMAIN:
4023 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4024 continue;
4025 break;
4026 case FUNCTIONS_DOMAIN:
4027 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4028 continue;
4029 break;
4030 case TYPES_DOMAIN:
4031 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4032 continue;
4033 break;
4034 case MODULES_DOMAIN:
4035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4036 continue;
4037 break;
4038 default:
4039 break;
4040 }
4041 }
4042
4043 /* Don't crash on bad data. */
4044 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4045 {
4046 complaint (_(".gdb_index entry has bad CU index"
4047 " [in module %s]"), objfile_name (per_objfile->objfile));
4048 continue;
4049 }
4050
4051 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4052 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4053 expansion_notify))
4054 return false;
4055 }
4056
4057 return true;
4058 }
4059
4060 /* If FILE_MATCHER is non-NULL, set all the
4061 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4062 that match FILE_MATCHER. */
4063
4064 static void
4065 dw_expand_symtabs_matching_file_matcher
4066 (dwarf2_per_objfile *per_objfile,
4067 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4068 {
4069 if (file_matcher == NULL)
4070 return;
4071
4072 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4073 htab_eq_pointer,
4074 NULL, xcalloc, xfree));
4075 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4076 htab_eq_pointer,
4077 NULL, xcalloc, xfree));
4078
4079 /* The rule is CUs specify all the files, including those used by
4080 any TU, so there's no need to scan TUs here. */
4081
4082 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4083 {
4084 QUIT;
4085
4086 if (per_cu->is_debug_types)
4087 continue;
4088 per_cu->mark = 0;
4089
4090 /* We only need to look at symtabs not already expanded. */
4091 if (per_objfile->symtab_set_p (per_cu.get ()))
4092 continue;
4093
4094 if (per_cu->fnd != nullptr)
4095 {
4096 file_and_directory *fnd = per_cu->fnd.get ();
4097
4098 if (file_matcher (fnd->get_name (), false))
4099 {
4100 per_cu->mark = 1;
4101 continue;
4102 }
4103
4104 /* Before we invoke realpath, which can get expensive when many
4105 files are involved, do a quick comparison of the basenames. */
4106 if ((basenames_may_differ
4107 || file_matcher (lbasename (fnd->get_name ()), true))
4108 && file_matcher (fnd->get_fullname (), false))
4109 {
4110 per_cu->mark = 1;
4111 continue;
4112 }
4113 }
4114
4115 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4116 per_objfile);
4117 if (file_data == NULL)
4118 continue;
4119
4120 if (htab_find (visited_not_found.get (), file_data) != NULL)
4121 continue;
4122 else if (htab_find (visited_found.get (), file_data) != NULL)
4123 {
4124 per_cu->mark = 1;
4125 continue;
4126 }
4127
4128 for (int j = 0; j < file_data->num_file_names; ++j)
4129 {
4130 const char *this_real_name;
4131
4132 if (file_matcher (file_data->file_names[j], false))
4133 {
4134 per_cu->mark = 1;
4135 break;
4136 }
4137
4138 /* Before we invoke realpath, which can get expensive when many
4139 files are involved, do a quick comparison of the basenames. */
4140 if (!basenames_may_differ
4141 && !file_matcher (lbasename (file_data->file_names[j]),
4142 true))
4143 continue;
4144
4145 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4146 if (file_matcher (this_real_name, false))
4147 {
4148 per_cu->mark = 1;
4149 break;
4150 }
4151 }
4152
4153 void **slot = htab_find_slot (per_cu->mark
4154 ? visited_found.get ()
4155 : visited_not_found.get (),
4156 file_data, INSERT);
4157 *slot = file_data;
4158 }
4159 }
4160
4161 bool
4162 dwarf2_gdb_index::expand_symtabs_matching
4163 (struct objfile *objfile,
4164 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4165 const lookup_name_info *lookup_name,
4166 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4167 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4168 block_search_flags search_flags,
4169 domain_enum domain,
4170 enum search_domain kind)
4171 {
4172 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4173
4174 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4175
4176 /* This invariant is documented in quick-functions.h. */
4177 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4178 if (lookup_name == nullptr)
4179 {
4180 for (dwarf2_per_cu_data *per_cu
4181 : all_comp_units_range (per_objfile->per_bfd))
4182 {
4183 QUIT;
4184
4185 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4186 file_matcher,
4187 expansion_notify))
4188 return false;
4189 }
4190 return true;
4191 }
4192
4193 mapped_index &index
4194 = (static_cast<mapped_index &>
4195 (*per_objfile->per_bfd->index_table.get ()));
4196
4197 bool result
4198 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4199 symbol_matcher,
4200 [&] (offset_type idx)
4201 {
4202 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4203 expansion_notify, search_flags, kind))
4204 return false;
4205 return true;
4206 }, per_objfile);
4207
4208 return result;
4209 }
4210
4211 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4212 symtab. */
4213
4214 static struct compunit_symtab *
4215 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4216 CORE_ADDR pc)
4217 {
4218 int i;
4219
4220 if (cust->blockvector () != nullptr
4221 && blockvector_contains_pc (cust->blockvector (), pc))
4222 return cust;
4223
4224 if (cust->includes == NULL)
4225 return NULL;
4226
4227 for (i = 0; cust->includes[i]; ++i)
4228 {
4229 struct compunit_symtab *s = cust->includes[i];
4230
4231 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4232 if (s != NULL)
4233 return s;
4234 }
4235
4236 return NULL;
4237 }
4238
4239 struct compunit_symtab *
4240 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4241 (struct objfile *objfile,
4242 struct bound_minimal_symbol msymbol,
4243 CORE_ADDR pc,
4244 struct obj_section *section,
4245 int warn_if_readin)
4246 {
4247 struct dwarf2_per_cu_data *data;
4248 struct compunit_symtab *result;
4249
4250 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4251 if (per_objfile->per_bfd->index_addrmap == nullptr)
4252 return NULL;
4253
4254 CORE_ADDR baseaddr = objfile->text_section_offset ();
4255 data = ((struct dwarf2_per_cu_data *)
4256 addrmap_find (per_objfile->per_bfd->index_addrmap,
4257 pc - baseaddr));
4258 if (!data)
4259 return NULL;
4260
4261 if (warn_if_readin && per_objfile->symtab_set_p (data))
4262 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4263 paddress (objfile->arch (), pc));
4264
4265 result = recursively_find_pc_sect_compunit_symtab
4266 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4267
4268 gdb_assert (result != NULL);
4269 return result;
4270 }
4271
4272 void
4273 dwarf2_base_index_functions::map_symbol_filenames
4274 (struct objfile *objfile,
4275 gdb::function_view<symbol_filename_ftype> fun,
4276 bool need_fullname)
4277 {
4278 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4279
4280 /* Use caches to ensure we only call FUN once for each filename. */
4281 filename_seen_cache filenames_cache;
4282 std::unordered_set<quick_file_names *> qfn_cache;
4283
4284 /* The rule is CUs specify all the files, including those used by any TU,
4285 so there's no need to scan TUs here. We can ignore file names coming
4286 from already-expanded CUs. It is possible that an expanded CU might
4287 reuse the file names data from a currently unexpanded CU, in this
4288 case we don't want to report the files from the unexpanded CU. */
4289
4290 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4291 {
4292 if (!per_cu->is_debug_types
4293 && per_objfile->symtab_set_p (per_cu.get ()))
4294 {
4295 if (per_cu->file_names != nullptr)
4296 qfn_cache.insert (per_cu->file_names);
4297 }
4298 }
4299
4300 for (dwarf2_per_cu_data *per_cu
4301 : all_comp_units_range (per_objfile->per_bfd))
4302 {
4303 /* We only need to look at symtabs not already expanded. */
4304 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4305 continue;
4306
4307 if (per_cu->fnd != nullptr)
4308 {
4309 file_and_directory *fnd = per_cu->fnd.get ();
4310
4311 const char *filename = fnd->get_name ();
4312 const char *key = filename;
4313 const char *fullname = nullptr;
4314
4315 if (need_fullname)
4316 {
4317 fullname = fnd->get_fullname ();
4318 key = fullname;
4319 }
4320
4321 if (!filenames_cache.seen (key))
4322 fun (filename, fullname);
4323 }
4324
4325 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4326 if (file_data == nullptr
4327 || qfn_cache.find (file_data) != qfn_cache.end ())
4328 continue;
4329
4330 for (int j = 0; j < file_data->num_file_names; ++j)
4331 {
4332 const char *filename = file_data->file_names[j];
4333 const char *key = filename;
4334 const char *fullname = nullptr;
4335
4336 if (need_fullname)
4337 {
4338 fullname = dw2_get_real_path (per_objfile, file_data, j);
4339 key = fullname;
4340 }
4341
4342 if (!filenames_cache.seen (key))
4343 fun (filename, fullname);
4344 }
4345 }
4346 }
4347
4348 bool
4349 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4350 {
4351 return true;
4352 }
4353
4354 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4355
4356 bool
4357 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4358 {
4359 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4360
4361 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4362 {
4363 /* Is this already expanded? */
4364 if (per_objfile->symtab_set_p (per_cu.get ()))
4365 continue;
4366
4367 /* It has not yet been expanded. */
4368 return true;
4369 }
4370
4371 return false;
4372 }
4373
4374 /* DWARF-5 debug_names reader. */
4375
4376 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4377 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4378
4379 /* A helper function that reads the .debug_names section in SECTION
4380 and fills in MAP. FILENAME is the name of the file containing the
4381 section; it is used for error reporting.
4382
4383 Returns true if all went well, false otherwise. */
4384
4385 static bool
4386 read_debug_names_from_section (struct objfile *objfile,
4387 const char *filename,
4388 struct dwarf2_section_info *section,
4389 mapped_debug_names &map)
4390 {
4391 if (section->empty ())
4392 return false;
4393
4394 /* Older elfutils strip versions could keep the section in the main
4395 executable while splitting it for the separate debug info file. */
4396 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4397 return false;
4398
4399 section->read (objfile);
4400
4401 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4402
4403 const gdb_byte *addr = section->buffer;
4404
4405 bfd *const abfd = section->get_bfd_owner ();
4406
4407 unsigned int bytes_read;
4408 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4409 addr += bytes_read;
4410
4411 map.dwarf5_is_dwarf64 = bytes_read != 4;
4412 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4413 if (bytes_read + length != section->size)
4414 {
4415 /* There may be multiple per-CU indices. */
4416 warning (_("Section .debug_names in %s length %s does not match "
4417 "section length %s, ignoring .debug_names."),
4418 filename, plongest (bytes_read + length),
4419 pulongest (section->size));
4420 return false;
4421 }
4422
4423 /* The version number. */
4424 uint16_t version = read_2_bytes (abfd, addr);
4425 addr += 2;
4426 if (version != 5)
4427 {
4428 warning (_("Section .debug_names in %s has unsupported version %d, "
4429 "ignoring .debug_names."),
4430 filename, version);
4431 return false;
4432 }
4433
4434 /* Padding. */
4435 uint16_t padding = read_2_bytes (abfd, addr);
4436 addr += 2;
4437 if (padding != 0)
4438 {
4439 warning (_("Section .debug_names in %s has unsupported padding %d, "
4440 "ignoring .debug_names."),
4441 filename, padding);
4442 return false;
4443 }
4444
4445 /* comp_unit_count - The number of CUs in the CU list. */
4446 map.cu_count = read_4_bytes (abfd, addr);
4447 addr += 4;
4448
4449 /* local_type_unit_count - The number of TUs in the local TU
4450 list. */
4451 map.tu_count = read_4_bytes (abfd, addr);
4452 addr += 4;
4453
4454 /* foreign_type_unit_count - The number of TUs in the foreign TU
4455 list. */
4456 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4457 addr += 4;
4458 if (foreign_tu_count != 0)
4459 {
4460 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4461 "ignoring .debug_names."),
4462 filename, static_cast<unsigned long> (foreign_tu_count));
4463 return false;
4464 }
4465
4466 /* bucket_count - The number of hash buckets in the hash lookup
4467 table. */
4468 map.bucket_count = read_4_bytes (abfd, addr);
4469 addr += 4;
4470
4471 /* name_count - The number of unique names in the index. */
4472 map.name_count = read_4_bytes (abfd, addr);
4473 addr += 4;
4474
4475 /* abbrev_table_size - The size in bytes of the abbreviations
4476 table. */
4477 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4478 addr += 4;
4479
4480 /* augmentation_string_size - The size in bytes of the augmentation
4481 string. This value is rounded up to a multiple of 4. */
4482 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4483 addr += 4;
4484 map.augmentation_is_gdb = ((augmentation_string_size
4485 == sizeof (dwarf5_augmentation))
4486 && memcmp (addr, dwarf5_augmentation,
4487 sizeof (dwarf5_augmentation)) == 0);
4488 augmentation_string_size += (-augmentation_string_size) & 3;
4489 addr += augmentation_string_size;
4490
4491 /* List of CUs */
4492 map.cu_table_reordered = addr;
4493 addr += map.cu_count * map.offset_size;
4494
4495 /* List of Local TUs */
4496 map.tu_table_reordered = addr;
4497 addr += map.tu_count * map.offset_size;
4498
4499 /* Hash Lookup Table */
4500 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4501 addr += map.bucket_count * 4;
4502 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4503 addr += map.name_count * 4;
4504
4505 /* Name Table */
4506 map.name_table_string_offs_reordered = addr;
4507 addr += map.name_count * map.offset_size;
4508 map.name_table_entry_offs_reordered = addr;
4509 addr += map.name_count * map.offset_size;
4510
4511 const gdb_byte *abbrev_table_start = addr;
4512 for (;;)
4513 {
4514 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4515 addr += bytes_read;
4516 if (index_num == 0)
4517 break;
4518
4519 const auto insertpair
4520 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4521 if (!insertpair.second)
4522 {
4523 warning (_("Section .debug_names in %s has duplicate index %s, "
4524 "ignoring .debug_names."),
4525 filename, pulongest (index_num));
4526 return false;
4527 }
4528 mapped_debug_names::index_val &indexval = insertpair.first->second;
4529 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4530 addr += bytes_read;
4531
4532 for (;;)
4533 {
4534 mapped_debug_names::index_val::attr attr;
4535 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4536 addr += bytes_read;
4537 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4538 addr += bytes_read;
4539 if (attr.form == DW_FORM_implicit_const)
4540 {
4541 attr.implicit_const = read_signed_leb128 (abfd, addr,
4542 &bytes_read);
4543 addr += bytes_read;
4544 }
4545 if (attr.dw_idx == 0 && attr.form == 0)
4546 break;
4547 indexval.attr_vec.push_back (std::move (attr));
4548 }
4549 }
4550 if (addr != abbrev_table_start + abbrev_table_size)
4551 {
4552 warning (_("Section .debug_names in %s has abbreviation_table "
4553 "of size %s vs. written as %u, ignoring .debug_names."),
4554 filename, plongest (addr - abbrev_table_start),
4555 abbrev_table_size);
4556 return false;
4557 }
4558 map.entry_pool = addr;
4559
4560 return true;
4561 }
4562
4563 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4564 list. */
4565
4566 static void
4567 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4568 const mapped_debug_names &map,
4569 dwarf2_section_info &section,
4570 bool is_dwz)
4571 {
4572 if (!map.augmentation_is_gdb)
4573 {
4574 for (uint32_t i = 0; i < map.cu_count; ++i)
4575 {
4576 sect_offset sect_off
4577 = (sect_offset) (extract_unsigned_integer
4578 (map.cu_table_reordered + i * map.offset_size,
4579 map.offset_size,
4580 map.dwarf5_byte_order));
4581 /* We don't know the length of the CU, because the CU list in a
4582 .debug_names index can be incomplete, so we can't use the start
4583 of the next CU as end of this CU. We create the CUs here with
4584 length 0, and in cutu_reader::cutu_reader we'll fill in the
4585 actual length. */
4586 dwarf2_per_cu_data_up per_cu
4587 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4588 sect_off, 0);
4589 per_bfd->all_comp_units.push_back (std::move (per_cu));
4590 }
4591 return;
4592 }
4593
4594 sect_offset sect_off_prev;
4595 for (uint32_t i = 0; i <= map.cu_count; ++i)
4596 {
4597 sect_offset sect_off_next;
4598 if (i < map.cu_count)
4599 {
4600 sect_off_next
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 }
4606 else
4607 sect_off_next = (sect_offset) section.size;
4608 if (i >= 1)
4609 {
4610 const ULONGEST length = sect_off_next - sect_off_prev;
4611 dwarf2_per_cu_data_up per_cu
4612 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4613 sect_off_prev, length);
4614 per_bfd->all_comp_units.push_back (std::move (per_cu));
4615 }
4616 sect_off_prev = sect_off_next;
4617 }
4618 }
4619
4620 /* Read the CU list from the mapped index, and use it to create all
4621 the CU objects for this dwarf2_per_objfile. */
4622
4623 static void
4624 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4625 const mapped_debug_names &map,
4626 const mapped_debug_names &dwz_map)
4627 {
4628 gdb_assert (per_bfd->all_comp_units.empty ());
4629 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4630
4631 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4632 false /* is_dwz */);
4633
4634 if (dwz_map.cu_count == 0)
4635 return;
4636
4637 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4638 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4639 true /* is_dwz */);
4640 }
4641
4642 /* Read .debug_names. If everything went ok, initialize the "quick"
4643 elements of all the CUs and return true. Otherwise, return false. */
4644
4645 static bool
4646 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4647 {
4648 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4649 mapped_debug_names dwz_map;
4650 struct objfile *objfile = per_objfile->objfile;
4651 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4652
4653 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4654 &per_bfd->debug_names, *map))
4655 return false;
4656
4657 /* Don't use the index if it's empty. */
4658 if (map->name_count == 0)
4659 return false;
4660
4661 /* If there is a .dwz file, read it so we can get its CU list as
4662 well. */
4663 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4664 if (dwz != NULL)
4665 {
4666 if (!read_debug_names_from_section (objfile,
4667 bfd_get_filename (dwz->dwz_bfd.get ()),
4668 &dwz->debug_names, dwz_map))
4669 {
4670 warning (_("could not read '.debug_names' section from %s; skipping"),
4671 bfd_get_filename (dwz->dwz_bfd.get ()));
4672 return false;
4673 }
4674 }
4675
4676 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4677
4678 if (map->tu_count != 0)
4679 {
4680 /* We can only handle a single .debug_types when we have an
4681 index. */
4682 if (per_bfd->types.size () != 1)
4683 return false;
4684
4685 dwarf2_section_info *section = &per_bfd->types[0];
4686
4687 create_signatured_type_table_from_debug_names
4688 (per_objfile, *map, section, &per_bfd->abbrev);
4689 }
4690
4691 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4692
4693 per_bfd->index_table = std::move (map);
4694 per_bfd->quick_file_names_table =
4695 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4696
4697 return true;
4698 }
4699
4700 /* Type used to manage iterating over all CUs looking for a symbol for
4701 .debug_names. */
4702
4703 class dw2_debug_names_iterator
4704 {
4705 public:
4706 dw2_debug_names_iterator (const mapped_debug_names &map,
4707 block_search_flags block_index,
4708 domain_enum domain,
4709 const char *name, dwarf2_per_objfile *per_objfile)
4710 : m_map (map), m_block_index (block_index), m_domain (domain),
4711 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4712 m_per_objfile (per_objfile)
4713 {}
4714
4715 dw2_debug_names_iterator (const mapped_debug_names &map,
4716 search_domain search, uint32_t namei,
4717 dwarf2_per_objfile *per_objfile,
4718 domain_enum domain = UNDEF_DOMAIN)
4719 : m_map (map),
4720 m_domain (domain),
4721 m_search (search),
4722 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4723 m_per_objfile (per_objfile)
4724 {}
4725
4726 dw2_debug_names_iterator (const mapped_debug_names &map,
4727 block_search_flags block_index, domain_enum domain,
4728 uint32_t namei, dwarf2_per_objfile *per_objfile)
4729 : m_map (map), m_block_index (block_index), m_domain (domain),
4730 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4731 m_per_objfile (per_objfile)
4732 {}
4733
4734 /* Return the next matching CU or NULL if there are no more. */
4735 dwarf2_per_cu_data *next ();
4736
4737 private:
4738 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4739 const char *name,
4740 dwarf2_per_objfile *per_objfile);
4741 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4742 uint32_t namei,
4743 dwarf2_per_objfile *per_objfile);
4744
4745 /* The internalized form of .debug_names. */
4746 const mapped_debug_names &m_map;
4747
4748 /* Restrict the search to these blocks. */
4749 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4750 | SEARCH_STATIC_BLOCK);
4751
4752 /* The kind of symbol we're looking for. */
4753 const domain_enum m_domain = UNDEF_DOMAIN;
4754 const search_domain m_search = ALL_DOMAIN;
4755
4756 /* The list of CUs from the index entry of the symbol, or NULL if
4757 not found. */
4758 const gdb_byte *m_addr;
4759
4760 dwarf2_per_objfile *m_per_objfile;
4761 };
4762
4763 const char *
4764 mapped_debug_names::namei_to_name
4765 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4766 {
4767 const ULONGEST namei_string_offs
4768 = extract_unsigned_integer ((name_table_string_offs_reordered
4769 + namei * offset_size),
4770 offset_size,
4771 dwarf5_byte_order);
4772 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4773 }
4774
4775 /* Find a slot in .debug_names for the object named NAME. If NAME is
4776 found, return pointer to its pool data. If NAME cannot be found,
4777 return NULL. */
4778
4779 const gdb_byte *
4780 dw2_debug_names_iterator::find_vec_in_debug_names
4781 (const mapped_debug_names &map, const char *name,
4782 dwarf2_per_objfile *per_objfile)
4783 {
4784 int (*cmp) (const char *, const char *);
4785
4786 gdb::unique_xmalloc_ptr<char> without_params;
4787 if (current_language->la_language == language_cplus
4788 || current_language->la_language == language_fortran
4789 || current_language->la_language == language_d)
4790 {
4791 /* NAME is already canonical. Drop any qualifiers as
4792 .debug_names does not contain any. */
4793
4794 if (strchr (name, '(') != NULL)
4795 {
4796 without_params = cp_remove_params (name);
4797 if (without_params != NULL)
4798 name = without_params.get ();
4799 }
4800 }
4801
4802 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4803
4804 const uint32_t full_hash = dwarf5_djb_hash (name);
4805 uint32_t namei
4806 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4807 (map.bucket_table_reordered
4808 + (full_hash % map.bucket_count)), 4,
4809 map.dwarf5_byte_order);
4810 if (namei == 0)
4811 return NULL;
4812 --namei;
4813 if (namei >= map.name_count)
4814 {
4815 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4816 "[in module %s]"),
4817 namei, map.name_count,
4818 objfile_name (per_objfile->objfile));
4819 return NULL;
4820 }
4821
4822 for (;;)
4823 {
4824 const uint32_t namei_full_hash
4825 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4826 (map.hash_table_reordered + namei), 4,
4827 map.dwarf5_byte_order);
4828 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4829 return NULL;
4830
4831 if (full_hash == namei_full_hash)
4832 {
4833 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4834
4835 #if 0 /* An expensive sanity check. */
4836 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4837 {
4838 complaint (_("Wrong .debug_names hash for string at index %u "
4839 "[in module %s]"),
4840 namei, objfile_name (dwarf2_per_objfile->objfile));
4841 return NULL;
4842 }
4843 #endif
4844
4845 if (cmp (namei_string, name) == 0)
4846 {
4847 const ULONGEST namei_entry_offs
4848 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4849 + namei * map.offset_size),
4850 map.offset_size, map.dwarf5_byte_order);
4851 return map.entry_pool + namei_entry_offs;
4852 }
4853 }
4854
4855 ++namei;
4856 if (namei >= map.name_count)
4857 return NULL;
4858 }
4859 }
4860
4861 const gdb_byte *
4862 dw2_debug_names_iterator::find_vec_in_debug_names
4863 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4864 {
4865 if (namei >= map.name_count)
4866 {
4867 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4868 "[in module %s]"),
4869 namei, map.name_count,
4870 objfile_name (per_objfile->objfile));
4871 return NULL;
4872 }
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 /* See dw2_debug_names_iterator. */
4882
4883 dwarf2_per_cu_data *
4884 dw2_debug_names_iterator::next ()
4885 {
4886 if (m_addr == NULL)
4887 return NULL;
4888
4889 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4890 struct objfile *objfile = m_per_objfile->objfile;
4891 bfd *const abfd = objfile->obfd;
4892
4893 again:
4894
4895 unsigned int bytes_read;
4896 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4897 m_addr += bytes_read;
4898 if (abbrev == 0)
4899 return NULL;
4900
4901 const auto indexval_it = m_map.abbrev_map.find (abbrev);
4902 if (indexval_it == m_map.abbrev_map.cend ())
4903 {
4904 complaint (_("Wrong .debug_names undefined abbrev code %s "
4905 "[in module %s]"),
4906 pulongest (abbrev), objfile_name (objfile));
4907 return NULL;
4908 }
4909 const mapped_debug_names::index_val &indexval = indexval_it->second;
4910 enum class symbol_linkage {
4911 unknown,
4912 static_,
4913 extern_,
4914 } symbol_linkage_ = symbol_linkage::unknown;
4915 dwarf2_per_cu_data *per_cu = NULL;
4916 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
4917 {
4918 ULONGEST ull;
4919 switch (attr.form)
4920 {
4921 case DW_FORM_implicit_const:
4922 ull = attr.implicit_const;
4923 break;
4924 case DW_FORM_flag_present:
4925 ull = 1;
4926 break;
4927 case DW_FORM_udata:
4928 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4929 m_addr += bytes_read;
4930 break;
4931 case DW_FORM_ref4:
4932 ull = read_4_bytes (abfd, m_addr);
4933 m_addr += 4;
4934 break;
4935 case DW_FORM_ref8:
4936 ull = read_8_bytes (abfd, m_addr);
4937 m_addr += 8;
4938 break;
4939 case DW_FORM_ref_sig8:
4940 ull = read_8_bytes (abfd, m_addr);
4941 m_addr += 8;
4942 break;
4943 default:
4944 complaint (_("Unsupported .debug_names form %s [in module %s]"),
4945 dwarf_form_name (attr.form),
4946 objfile_name (objfile));
4947 return NULL;
4948 }
4949 switch (attr.dw_idx)
4950 {
4951 case DW_IDX_compile_unit:
4952 /* Don't crash on bad data. */
4953 if (ull >= per_bfd->all_comp_units.size ())
4954 {
4955 complaint (_(".debug_names entry has bad CU index %s"
4956 " [in module %s]"),
4957 pulongest (ull),
4958 objfile_name (objfile));
4959 continue;
4960 }
4961 per_cu = per_bfd->get_cu (ull);
4962 break;
4963 case DW_IDX_type_unit:
4964 /* Don't crash on bad data. */
4965 if (ull >= per_bfd->tu_stats.nr_tus)
4966 {
4967 complaint (_(".debug_names entry has bad TU index %s"
4968 " [in module %s]"),
4969 pulongest (ull),
4970 objfile_name (objfile));
4971 continue;
4972 }
4973 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
4974 break;
4975 case DW_IDX_die_offset:
4976 /* In a per-CU index (as opposed to a per-module index), index
4977 entries without CU attribute implicitly refer to the single CU. */
4978 if (per_cu == NULL)
4979 per_cu = per_bfd->get_cu (0);
4980 break;
4981 case DW_IDX_GNU_internal:
4982 if (!m_map.augmentation_is_gdb)
4983 break;
4984 symbol_linkage_ = symbol_linkage::static_;
4985 break;
4986 case DW_IDX_GNU_external:
4987 if (!m_map.augmentation_is_gdb)
4988 break;
4989 symbol_linkage_ = symbol_linkage::extern_;
4990 break;
4991 }
4992 }
4993
4994 /* Skip if already read in. */
4995 if (m_per_objfile->symtab_set_p (per_cu))
4996 goto again;
4997
4998 /* Check static vs global. */
4999 if (symbol_linkage_ != symbol_linkage::unknown)
5000 {
5001 if (symbol_linkage_ == symbol_linkage::static_)
5002 {
5003 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5004 goto again;
5005 }
5006 else
5007 {
5008 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5009 goto again;
5010 }
5011 }
5012
5013 /* Match dw2_symtab_iter_next, symbol_kind
5014 and debug_names::psymbol_tag. */
5015 switch (m_domain)
5016 {
5017 case VAR_DOMAIN:
5018 switch (indexval.dwarf_tag)
5019 {
5020 case DW_TAG_variable:
5021 case DW_TAG_subprogram:
5022 /* Some types are also in VAR_DOMAIN. */
5023 case DW_TAG_typedef:
5024 case DW_TAG_structure_type:
5025 break;
5026 default:
5027 goto again;
5028 }
5029 break;
5030 case STRUCT_DOMAIN:
5031 switch (indexval.dwarf_tag)
5032 {
5033 case DW_TAG_typedef:
5034 case DW_TAG_structure_type:
5035 break;
5036 default:
5037 goto again;
5038 }
5039 break;
5040 case LABEL_DOMAIN:
5041 switch (indexval.dwarf_tag)
5042 {
5043 case 0:
5044 case DW_TAG_variable:
5045 break;
5046 default:
5047 goto again;
5048 }
5049 break;
5050 case MODULE_DOMAIN:
5051 switch (indexval.dwarf_tag)
5052 {
5053 case DW_TAG_module:
5054 break;
5055 default:
5056 goto again;
5057 }
5058 break;
5059 default:
5060 break;
5061 }
5062
5063 /* Match dw2_expand_symtabs_matching, symbol_kind and
5064 debug_names::psymbol_tag. */
5065 switch (m_search)
5066 {
5067 case VARIABLES_DOMAIN:
5068 switch (indexval.dwarf_tag)
5069 {
5070 case DW_TAG_variable:
5071 break;
5072 default:
5073 goto again;
5074 }
5075 break;
5076 case FUNCTIONS_DOMAIN:
5077 switch (indexval.dwarf_tag)
5078 {
5079 case DW_TAG_subprogram:
5080 break;
5081 default:
5082 goto again;
5083 }
5084 break;
5085 case TYPES_DOMAIN:
5086 switch (indexval.dwarf_tag)
5087 {
5088 case DW_TAG_typedef:
5089 case DW_TAG_structure_type:
5090 break;
5091 default:
5092 goto again;
5093 }
5094 break;
5095 case MODULES_DOMAIN:
5096 switch (indexval.dwarf_tag)
5097 {
5098 case DW_TAG_module:
5099 break;
5100 default:
5101 goto again;
5102 }
5103 default:
5104 break;
5105 }
5106
5107 return per_cu;
5108 }
5109
5110 /* This dumps minimal information about .debug_names. It is called
5111 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5112 uses this to verify that .debug_names has been loaded. */
5113
5114 void
5115 dwarf2_debug_names_index::dump (struct objfile *objfile)
5116 {
5117 gdb_printf (".debug_names: exists\n");
5118 }
5119
5120 void
5121 dwarf2_debug_names_index::expand_matching_symbols
5122 (struct objfile *objfile,
5123 const lookup_name_info &name, domain_enum domain,
5124 int global,
5125 symbol_compare_ftype *ordered_compare)
5126 {
5127 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5128
5129 mapped_debug_names &map
5130 = (static_cast<mapped_debug_names &>
5131 (*per_objfile->per_bfd->index_table.get ()));
5132 const block_search_flags block_flags
5133 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5134
5135 const char *match_name = name.ada ().lookup_name ().c_str ();
5136 auto matcher = [&] (const char *symname)
5137 {
5138 if (ordered_compare == nullptr)
5139 return true;
5140 return ordered_compare (symname, match_name) == 0;
5141 };
5142
5143 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5144 [&] (offset_type namei)
5145 {
5146 /* The name was matched, now expand corresponding CUs that were
5147 marked. */
5148 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5149 per_objfile);
5150
5151 struct dwarf2_per_cu_data *per_cu;
5152 while ((per_cu = iter.next ()) != NULL)
5153 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5154 nullptr);
5155 return true;
5156 }, per_objfile);
5157 }
5158
5159 bool
5160 dwarf2_debug_names_index::expand_symtabs_matching
5161 (struct objfile *objfile,
5162 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5163 const lookup_name_info *lookup_name,
5164 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5165 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5166 block_search_flags search_flags,
5167 domain_enum domain,
5168 enum search_domain kind)
5169 {
5170 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5171
5172 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5173
5174 /* This invariant is documented in quick-functions.h. */
5175 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5176 if (lookup_name == nullptr)
5177 {
5178 for (dwarf2_per_cu_data *per_cu
5179 : all_comp_units_range (per_objfile->per_bfd))
5180 {
5181 QUIT;
5182
5183 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5184 file_matcher,
5185 expansion_notify))
5186 return false;
5187 }
5188 return true;
5189 }
5190
5191 mapped_debug_names &map
5192 = (static_cast<mapped_debug_names &>
5193 (*per_objfile->per_bfd->index_table.get ()));
5194
5195 bool result
5196 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5197 symbol_matcher,
5198 [&] (offset_type namei)
5199 {
5200 /* The name was matched, now expand corresponding CUs that were
5201 marked. */
5202 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5203
5204 struct dwarf2_per_cu_data *per_cu;
5205 while ((per_cu = iter.next ()) != NULL)
5206 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5207 file_matcher,
5208 expansion_notify))
5209 return false;
5210 return true;
5211 }, per_objfile);
5212
5213 return result;
5214 }
5215
5216 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5217 to either a dwarf2_per_bfd or dwz_file object. */
5218
5219 template <typename T>
5220 static gdb::array_view<const gdb_byte>
5221 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5222 {
5223 dwarf2_section_info *section = &section_owner->gdb_index;
5224
5225 if (section->empty ())
5226 return {};
5227
5228 /* Older elfutils strip versions could keep the section in the main
5229 executable while splitting it for the separate debug info file. */
5230 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5231 return {};
5232
5233 section->read (obj);
5234
5235 /* dwarf2_section_info::size is a bfd_size_type, while
5236 gdb::array_view works with size_t. On 32-bit hosts, with
5237 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5238 is 32-bit. So we need an explicit narrowing conversion here.
5239 This is fine, because it's impossible to allocate or mmap an
5240 array/buffer larger than what size_t can represent. */
5241 return gdb::make_array_view (section->buffer, section->size);
5242 }
5243
5244 /* Lookup the index cache for the contents of the index associated to
5245 DWARF2_OBJ. */
5246
5247 static gdb::array_view<const gdb_byte>
5248 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5249 {
5250 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5251 if (build_id == nullptr)
5252 return {};
5253
5254 return global_index_cache.lookup_gdb_index (build_id,
5255 &dwarf2_per_bfd->index_cache_res);
5256 }
5257
5258 /* Same as the above, but for DWZ. */
5259
5260 static gdb::array_view<const gdb_byte>
5261 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5262 {
5263 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5264 if (build_id == nullptr)
5265 return {};
5266
5267 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5268 }
5269
5270 static quick_symbol_functions_up make_cooked_index_funcs ();
5271
5272 /* See dwarf2/public.h. */
5273
5274 void
5275 dwarf2_initialize_objfile (struct objfile *objfile)
5276 {
5277 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5278 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5279
5280 dwarf_read_debug_printf ("called");
5281
5282 /* If we're about to read full symbols, don't bother with the
5283 indices. In this case we also don't care if some other debug
5284 format is making psymtabs, because they are all about to be
5285 expanded anyway. */
5286 if ((objfile->flags & OBJF_READNOW))
5287 {
5288 dwarf_read_debug_printf ("readnow requested");
5289
5290 create_all_comp_units (per_objfile);
5291 per_bfd->quick_file_names_table
5292 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5293
5294 objfile->qf.emplace_front (new readnow_functions);
5295 return;
5296 }
5297
5298 /* Was a GDB index already read when we processed an objfile sharing
5299 PER_BFD? */
5300 if (per_bfd->index_table != nullptr)
5301 {
5302 dwarf_read_debug_printf ("re-using symbols");
5303 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5304 return;
5305 }
5306
5307 if (dwarf2_read_debug_names (per_objfile))
5308 {
5309 dwarf_read_debug_printf ("found debug names");
5310 objfile->qf.push_front
5311 (per_bfd->index_table->make_quick_functions ());
5312 return;
5313 }
5314
5315 if (dwarf2_read_gdb_index (per_objfile,
5316 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5317 get_gdb_index_contents_from_section<dwz_file>))
5318 {
5319 dwarf_read_debug_printf ("found gdb index from file");
5320 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5321 return;
5322 }
5323
5324 /* ... otherwise, try to find the index in the index cache. */
5325 if (dwarf2_read_gdb_index (per_objfile,
5326 get_gdb_index_contents_from_cache,
5327 get_gdb_index_contents_from_cache_dwz))
5328 {
5329 dwarf_read_debug_printf ("found gdb index from cache");
5330 global_index_cache.hit ();
5331 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5332 return;
5333 }
5334
5335 global_index_cache.miss ();
5336 objfile->qf.push_front (make_cooked_index_funcs ());
5337 }
5338
5339 \f
5340
5341 /* Build a partial symbol table. */
5342
5343 void
5344 dwarf2_build_psymtabs (struct objfile *objfile, bool already_attached)
5345 {
5346 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5347
5348 if (already_attached)
5349 {
5350 if (per_objfile->per_bfd->index_table != nullptr)
5351 return;
5352 }
5353 else
5354 objfile->qf.push_front (make_cooked_index_funcs ());
5355
5356 try
5357 {
5358 dwarf2_build_psymtabs_hard (per_objfile);
5359
5360 /* (maybe) store an index in the cache. */
5361 global_index_cache.store (per_objfile);
5362 }
5363 catch (const gdb_exception_error &except)
5364 {
5365 exception_print (gdb_stderr, except);
5366 }
5367 }
5368
5369 /* Find the base address of the compilation unit for range lists and
5370 location lists. It will normally be specified by DW_AT_low_pc.
5371 In DWARF-3 draft 4, the base address could be overridden by
5372 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5373 compilation units with discontinuous ranges. */
5374
5375 static void
5376 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5377 {
5378 struct attribute *attr;
5379
5380 cu->base_address.reset ();
5381
5382 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5383 if (attr != nullptr)
5384 cu->base_address = attr->as_address ();
5385 else
5386 {
5387 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5388 if (attr != nullptr)
5389 cu->base_address = attr->as_address ();
5390 }
5391 }
5392
5393 /* Helper function that returns the proper abbrev section for
5394 THIS_CU. */
5395
5396 static struct dwarf2_section_info *
5397 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5398 {
5399 struct dwarf2_section_info *abbrev;
5400 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5401
5402 if (this_cu->is_dwz)
5403 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5404 else
5405 abbrev = &per_bfd->abbrev;
5406
5407 return abbrev;
5408 }
5409
5410 /* Fetch the abbreviation table offset from a comp or type unit header. */
5411
5412 static sect_offset
5413 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5414 struct dwarf2_section_info *section,
5415 sect_offset sect_off)
5416 {
5417 bfd *abfd = section->get_bfd_owner ();
5418 const gdb_byte *info_ptr;
5419 unsigned int initial_length_size, offset_size;
5420 uint16_t version;
5421
5422 section->read (per_objfile->objfile);
5423 info_ptr = section->buffer + to_underlying (sect_off);
5424 read_initial_length (abfd, info_ptr, &initial_length_size);
5425 offset_size = initial_length_size == 4 ? 4 : 8;
5426 info_ptr += initial_length_size;
5427
5428 version = read_2_bytes (abfd, info_ptr);
5429 info_ptr += 2;
5430 if (version >= 5)
5431 {
5432 /* Skip unit type and address size. */
5433 info_ptr += 2;
5434 }
5435
5436 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5437 }
5438
5439 static hashval_t
5440 hash_signatured_type (const void *item)
5441 {
5442 const struct signatured_type *sig_type
5443 = (const struct signatured_type *) item;
5444
5445 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5446 return sig_type->signature;
5447 }
5448
5449 static int
5450 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5451 {
5452 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5453 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5454
5455 return lhs->signature == rhs->signature;
5456 }
5457
5458 /* Allocate a hash table for signatured types. */
5459
5460 static htab_up
5461 allocate_signatured_type_table ()
5462 {
5463 return htab_up (htab_create_alloc (41,
5464 hash_signatured_type,
5465 eq_signatured_type,
5466 NULL, xcalloc, xfree));
5467 }
5468
5469 /* A helper for create_debug_types_hash_table. Read types from SECTION
5470 and fill them into TYPES_HTAB. It will process only type units,
5471 therefore DW_UT_type. */
5472
5473 static void
5474 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5475 struct dwo_file *dwo_file,
5476 dwarf2_section_info *section, htab_up &types_htab,
5477 rcuh_kind section_kind)
5478 {
5479 struct objfile *objfile = per_objfile->objfile;
5480 struct dwarf2_section_info *abbrev_section;
5481 bfd *abfd;
5482 const gdb_byte *info_ptr, *end_ptr;
5483
5484 abbrev_section = &dwo_file->sections.abbrev;
5485
5486 dwarf_read_debug_printf ("Reading %s for %s",
5487 section->get_name (),
5488 abbrev_section->get_file_name ());
5489
5490 section->read (objfile);
5491 info_ptr = section->buffer;
5492
5493 if (info_ptr == NULL)
5494 return;
5495
5496 /* We can't set abfd until now because the section may be empty or
5497 not present, in which case the bfd is unknown. */
5498 abfd = section->get_bfd_owner ();
5499
5500 /* We don't use cutu_reader here because we don't need to read
5501 any dies: the signature is in the header. */
5502
5503 end_ptr = info_ptr + section->size;
5504 while (info_ptr < end_ptr)
5505 {
5506 signatured_type_up sig_type;
5507 struct dwo_unit *dwo_tu;
5508 void **slot;
5509 const gdb_byte *ptr = info_ptr;
5510 struct comp_unit_head header;
5511 unsigned int length;
5512
5513 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5514
5515 /* Initialize it due to a false compiler warning. */
5516 header.signature = -1;
5517 header.type_cu_offset_in_tu = (cu_offset) -1;
5518
5519 /* We need to read the type's signature in order to build the hash
5520 table, but we don't need anything else just yet. */
5521
5522 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5523 abbrev_section, ptr, section_kind);
5524
5525 length = header.get_length ();
5526
5527 /* Skip dummy type units. */
5528 if (ptr >= info_ptr + length
5529 || peek_abbrev_code (abfd, ptr) == 0
5530 || (header.unit_type != DW_UT_type
5531 && header.unit_type != DW_UT_split_type))
5532 {
5533 info_ptr += length;
5534 continue;
5535 }
5536
5537 if (types_htab == NULL)
5538 types_htab = allocate_dwo_unit_table ();
5539
5540 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5541 dwo_tu->dwo_file = dwo_file;
5542 dwo_tu->signature = header.signature;
5543 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5544 dwo_tu->section = section;
5545 dwo_tu->sect_off = sect_off;
5546 dwo_tu->length = length;
5547
5548 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5549 gdb_assert (slot != NULL);
5550 if (*slot != NULL)
5551 complaint (_("debug type entry at offset %s is duplicate to"
5552 " the entry at offset %s, signature %s"),
5553 sect_offset_str (sect_off),
5554 sect_offset_str (dwo_tu->sect_off),
5555 hex_string (header.signature));
5556 *slot = dwo_tu;
5557
5558 dwarf_read_debug_printf_v (" offset %s, signature %s",
5559 sect_offset_str (sect_off),
5560 hex_string (header.signature));
5561
5562 info_ptr += length;
5563 }
5564 }
5565
5566 /* Create the hash table of all entries in the .debug_types
5567 (or .debug_types.dwo) section(s).
5568 DWO_FILE is a pointer to the DWO file object.
5569
5570 The result is a pointer to the hash table or NULL if there are no types.
5571
5572 Note: This function processes DWO files only, not DWP files. */
5573
5574 static void
5575 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5576 struct dwo_file *dwo_file,
5577 gdb::array_view<dwarf2_section_info> type_sections,
5578 htab_up &types_htab)
5579 {
5580 for (dwarf2_section_info &section : type_sections)
5581 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5582 rcuh_kind::TYPE);
5583 }
5584
5585 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5586 If SLOT is non-NULL, it is the entry to use in the hash table.
5587 Otherwise we find one. */
5588
5589 static struct signatured_type *
5590 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5591 {
5592 if (per_objfile->per_bfd->all_comp_units.size ()
5593 == per_objfile->per_bfd->all_comp_units.capacity ())
5594 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5595
5596 signatured_type_up sig_type_holder
5597 = per_objfile->per_bfd->allocate_signatured_type (sig);
5598 signatured_type *sig_type = sig_type_holder.get ();
5599
5600 per_objfile->per_bfd->all_comp_units.emplace_back
5601 (sig_type_holder.release ());
5602
5603 if (slot == NULL)
5604 {
5605 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5606 sig_type, INSERT);
5607 }
5608 gdb_assert (*slot == NULL);
5609 *slot = sig_type;
5610 /* The rest of sig_type must be filled in by the caller. */
5611 return sig_type;
5612 }
5613
5614 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5615 Fill in SIG_ENTRY with DWO_ENTRY. */
5616
5617 static void
5618 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5619 struct signatured_type *sig_entry,
5620 struct dwo_unit *dwo_entry)
5621 {
5622 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5623
5624 /* Make sure we're not clobbering something we don't expect to. */
5625 gdb_assert (! sig_entry->queued);
5626 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5627 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5628 gdb_assert (sig_entry->signature == dwo_entry->signature);
5629 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
5630 || (to_underlying (sig_entry->type_offset_in_section)
5631 == to_underlying (dwo_entry->type_offset_in_tu)));
5632 gdb_assert (sig_entry->type_unit_group == NULL);
5633 gdb_assert (sig_entry->dwo_unit == NULL
5634 || sig_entry->dwo_unit == dwo_entry);
5635
5636 sig_entry->section = dwo_entry->section;
5637 sig_entry->sect_off = dwo_entry->sect_off;
5638 sig_entry->length = dwo_entry->length;
5639 sig_entry->reading_dwo_directly = 1;
5640 sig_entry->per_bfd = per_bfd;
5641 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5642 sig_entry->dwo_unit = dwo_entry;
5643 }
5644
5645 /* Subroutine of lookup_signatured_type.
5646 If we haven't read the TU yet, create the signatured_type data structure
5647 for a TU to be read in directly from a DWO file, bypassing the stub.
5648 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5649 using .gdb_index, then when reading a CU we want to stay in the DWO file
5650 containing that CU. Otherwise we could end up reading several other DWO
5651 files (due to comdat folding) to process the transitive closure of all the
5652 mentioned TUs, and that can be slow. The current DWO file will have every
5653 type signature that it needs.
5654 We only do this for .gdb_index because in the psymtab case we already have
5655 to read all the DWOs to build the type unit groups. */
5656
5657 static struct signatured_type *
5658 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5659 {
5660 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5661 struct dwo_file *dwo_file;
5662 struct dwo_unit find_dwo_entry, *dwo_entry;
5663 void **slot;
5664
5665 gdb_assert (cu->dwo_unit);
5666
5667 /* If TU skeletons have been removed then we may not have read in any
5668 TUs yet. */
5669 if (per_objfile->per_bfd->signatured_types == NULL)
5670 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5671
5672 /* We only ever need to read in one copy of a signatured type.
5673 Use the global signatured_types array to do our own comdat-folding
5674 of types. If this is the first time we're reading this TU, and
5675 the TU has an entry in .gdb_index, replace the recorded data from
5676 .gdb_index with this TU. */
5677
5678 signatured_type find_sig_entry (sig);
5679 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5680 &find_sig_entry, INSERT);
5681 signatured_type *sig_entry = (struct signatured_type *) *slot;
5682
5683 /* We can get here with the TU already read, *or* in the process of being
5684 read. Don't reassign the global entry to point to this DWO if that's
5685 the case. Also note that if the TU is already being read, it may not
5686 have come from a DWO, the program may be a mix of Fission-compiled
5687 code and non-Fission-compiled code. */
5688
5689 /* Have we already tried to read this TU?
5690 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5691 needn't exist in the global table yet). */
5692 if (sig_entry != NULL && sig_entry->tu_read)
5693 return sig_entry;
5694
5695 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5696 dwo_unit of the TU itself. */
5697 dwo_file = cu->dwo_unit->dwo_file;
5698
5699 /* Ok, this is the first time we're reading this TU. */
5700 if (dwo_file->tus == NULL)
5701 return NULL;
5702 find_dwo_entry.signature = sig;
5703 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
5704 &find_dwo_entry);
5705 if (dwo_entry == NULL)
5706 return NULL;
5707
5708 /* If the global table doesn't have an entry for this TU, add one. */
5709 if (sig_entry == NULL)
5710 sig_entry = add_type_unit (per_objfile, sig, slot);
5711
5712 if (sig_entry->dwo_unit == nullptr)
5713 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5714 sig_entry->tu_read = 1;
5715 return sig_entry;
5716 }
5717
5718 /* Subroutine of lookup_signatured_type.
5719 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5720 then try the DWP file. If the TU stub (skeleton) has been removed then
5721 it won't be in .gdb_index. */
5722
5723 static struct signatured_type *
5724 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5725 {
5726 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5727 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
5728 struct dwo_unit *dwo_entry;
5729 void **slot;
5730
5731 gdb_assert (cu->dwo_unit);
5732 gdb_assert (dwp_file != NULL);
5733
5734 /* If TU skeletons have been removed then we may not have read in any
5735 TUs yet. */
5736 if (per_objfile->per_bfd->signatured_types == NULL)
5737 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5738
5739 signatured_type find_sig_entry (sig);
5740 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5741 &find_sig_entry, INSERT);
5742 signatured_type *sig_entry = (struct signatured_type *) *slot;
5743
5744 /* Have we already tried to read this TU?
5745 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5746 needn't exist in the global table yet). */
5747 if (sig_entry != NULL)
5748 return sig_entry;
5749
5750 if (dwp_file->tus == NULL)
5751 return NULL;
5752 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
5753 1 /* is_debug_types */);
5754 if (dwo_entry == NULL)
5755 return NULL;
5756
5757 sig_entry = add_type_unit (per_objfile, sig, slot);
5758 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5759
5760 return sig_entry;
5761 }
5762
5763 /* Lookup a signature based type for DW_FORM_ref_sig8.
5764 Returns NULL if signature SIG is not present in the table.
5765 It is up to the caller to complain about this. */
5766
5767 static struct signatured_type *
5768 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5769 {
5770 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5771
5772 if (cu->dwo_unit)
5773 {
5774 /* We're in a DWO/DWP file, and we're using .gdb_index.
5775 These cases require special processing. */
5776 if (get_dwp_file (per_objfile) == NULL)
5777 return lookup_dwo_signatured_type (cu, sig);
5778 else
5779 return lookup_dwp_signatured_type (cu, sig);
5780 }
5781 else
5782 {
5783 if (per_objfile->per_bfd->signatured_types == NULL)
5784 return NULL;
5785 signatured_type find_entry (sig);
5786 return ((struct signatured_type *)
5787 htab_find (per_objfile->per_bfd->signatured_types.get (),
5788 &find_entry));
5789 }
5790 }
5791
5792 /* Low level DIE reading support. */
5793
5794 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5795
5796 static void
5797 init_cu_die_reader (struct die_reader_specs *reader,
5798 struct dwarf2_cu *cu,
5799 struct dwarf2_section_info *section,
5800 struct dwo_file *dwo_file,
5801 struct abbrev_table *abbrev_table)
5802 {
5803 gdb_assert (section->readin && section->buffer != NULL);
5804 reader->abfd = section->get_bfd_owner ();
5805 reader->cu = cu;
5806 reader->dwo_file = dwo_file;
5807 reader->die_section = section;
5808 reader->buffer = section->buffer;
5809 reader->buffer_end = section->buffer + section->size;
5810 reader->abbrev_table = abbrev_table;
5811 }
5812
5813 /* Subroutine of cutu_reader to simplify it.
5814 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5815 There's just a lot of work to do, and cutu_reader is big enough
5816 already.
5817
5818 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5819 from it to the DIE in the DWO. If NULL we are skipping the stub.
5820 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5821 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5822 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5823 STUB_COMP_DIR may be non-NULL.
5824 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5825 are filled in with the info of the DIE from the DWO file.
5826 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5827 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5828 kept around for at least as long as *RESULT_READER.
5829
5830 The result is non-zero if a valid (non-dummy) DIE was found. */
5831
5832 static int
5833 read_cutu_die_from_dwo (dwarf2_cu *cu,
5834 struct dwo_unit *dwo_unit,
5835 struct die_info *stub_comp_unit_die,
5836 const char *stub_comp_dir,
5837 struct die_reader_specs *result_reader,
5838 const gdb_byte **result_info_ptr,
5839 struct die_info **result_comp_unit_die,
5840 abbrev_table_up *result_dwo_abbrev_table)
5841 {
5842 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5843 dwarf2_per_cu_data *per_cu = cu->per_cu;
5844 struct objfile *objfile = per_objfile->objfile;
5845 bfd *abfd;
5846 const gdb_byte *begin_info_ptr, *info_ptr;
5847 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5848 int i,num_extra_attrs;
5849 struct dwarf2_section_info *dwo_abbrev_section;
5850 struct die_info *comp_unit_die;
5851
5852 /* At most one of these may be provided. */
5853 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5854
5855 /* These attributes aren't processed until later:
5856 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5857 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5858 referenced later. However, these attributes are found in the stub
5859 which we won't have later. In order to not impose this complication
5860 on the rest of the code, we read them here and copy them to the
5861 DWO CU/TU die. */
5862
5863 stmt_list = NULL;
5864 low_pc = NULL;
5865 high_pc = NULL;
5866 ranges = NULL;
5867 comp_dir = NULL;
5868
5869 if (stub_comp_unit_die != NULL)
5870 {
5871 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5872 DWO file. */
5873 if (!per_cu->is_debug_types)
5874 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5875 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5876 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5877 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5878 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5879
5880 cu->addr_base = stub_comp_unit_die->addr_base ();
5881
5882 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5883 We need the value before we can process DW_AT_ranges values from the
5884 DWO. */
5885 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
5886
5887 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5888 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5889 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5890 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5891 section. */
5892 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
5893 }
5894 else if (stub_comp_dir != NULL)
5895 {
5896 /* Reconstruct the comp_dir attribute to simplify the code below. */
5897 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
5898 comp_dir->name = DW_AT_comp_dir;
5899 comp_dir->form = DW_FORM_string;
5900 comp_dir->set_string_noncanonical (stub_comp_dir);
5901 }
5902
5903 /* Set up for reading the DWO CU/TU. */
5904 cu->dwo_unit = dwo_unit;
5905 dwarf2_section_info *section = dwo_unit->section;
5906 section->read (objfile);
5907 abfd = section->get_bfd_owner ();
5908 begin_info_ptr = info_ptr = (section->buffer
5909 + to_underlying (dwo_unit->sect_off));
5910 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5911
5912 if (per_cu->is_debug_types)
5913 {
5914 signatured_type *sig_type = (struct signatured_type *) per_cu;
5915
5916 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5917 section, dwo_abbrev_section,
5918 info_ptr, rcuh_kind::TYPE);
5919 /* This is not an assert because it can be caused by bad debug info. */
5920 if (sig_type->signature != cu->header.signature)
5921 {
5922 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5923 " TU at offset %s [in module %s]"),
5924 hex_string (sig_type->signature),
5925 hex_string (cu->header.signature),
5926 sect_offset_str (dwo_unit->sect_off),
5927 bfd_get_filename (abfd));
5928 }
5929 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5930 /* For DWOs coming from DWP files, we don't know the CU length
5931 nor the type's offset in the TU until now. */
5932 dwo_unit->length = cu->header.get_length ();
5933 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5934
5935 /* Establish the type offset that can be used to lookup the type.
5936 For DWO files, we don't know it until now. */
5937 sig_type->type_offset_in_section
5938 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
5939 }
5940 else
5941 {
5942 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5943 section, dwo_abbrev_section,
5944 info_ptr, rcuh_kind::COMPILE);
5945 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5946 /* For DWOs coming from DWP files, we don't know the CU length
5947 until now. */
5948 dwo_unit->length = cu->header.get_length ();
5949 }
5950
5951 dwo_abbrev_section->read (objfile);
5952 *result_dwo_abbrev_table
5953 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
5954 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
5955 result_dwo_abbrev_table->get ());
5956
5957 /* Read in the die, but leave space to copy over the attributes
5958 from the stub. This has the benefit of simplifying the rest of
5959 the code - all the work to maintain the illusion of a single
5960 DW_TAG_{compile,type}_unit DIE is done here. */
5961 num_extra_attrs = ((stmt_list != NULL)
5962 + (low_pc != NULL)
5963 + (high_pc != NULL)
5964 + (ranges != NULL)
5965 + (comp_dir != NULL));
5966 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5967 num_extra_attrs);
5968
5969 /* Copy over the attributes from the stub to the DIE we just read in. */
5970 comp_unit_die = *result_comp_unit_die;
5971 i = comp_unit_die->num_attrs;
5972 if (stmt_list != NULL)
5973 comp_unit_die->attrs[i++] = *stmt_list;
5974 if (low_pc != NULL)
5975 comp_unit_die->attrs[i++] = *low_pc;
5976 if (high_pc != NULL)
5977 comp_unit_die->attrs[i++] = *high_pc;
5978 if (ranges != NULL)
5979 comp_unit_die->attrs[i++] = *ranges;
5980 if (comp_dir != NULL)
5981 comp_unit_die->attrs[i++] = *comp_dir;
5982 comp_unit_die->num_attrs += num_extra_attrs;
5983
5984 if (dwarf_die_debug)
5985 {
5986 gdb_printf (gdb_stdlog,
5987 "Read die from %s@0x%x of %s:\n",
5988 section->get_name (),
5989 (unsigned) (begin_info_ptr - section->buffer),
5990 bfd_get_filename (abfd));
5991 dump_die (comp_unit_die, dwarf_die_debug);
5992 }
5993
5994 /* Skip dummy compilation units. */
5995 if (info_ptr >= begin_info_ptr + dwo_unit->length
5996 || peek_abbrev_code (abfd, info_ptr) == 0)
5997 return 0;
5998
5999 *result_info_ptr = info_ptr;
6000 return 1;
6001 }
6002
6003 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6004 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6005 signature is part of the header. */
6006 static gdb::optional<ULONGEST>
6007 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6008 {
6009 if (cu->header.version >= 5)
6010 return cu->header.signature;
6011 struct attribute *attr;
6012 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6013 if (attr == nullptr || !attr->form_is_unsigned ())
6014 return gdb::optional<ULONGEST> ();
6015 return attr->as_unsigned ();
6016 }
6017
6018 /* Subroutine of cutu_reader to simplify it.
6019 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6020 Returns NULL if the specified DWO unit cannot be found. */
6021
6022 static struct dwo_unit *
6023 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6024 {
6025 #if CXX_STD_THREAD
6026 /* We need a lock here both to handle the DWO hash table, and BFD,
6027 which is not thread-safe. */
6028 static std::mutex dwo_lock;
6029
6030 std::lock_guard<std::mutex> guard (dwo_lock);
6031 #endif
6032
6033 dwarf2_per_cu_data *per_cu = cu->per_cu;
6034 struct dwo_unit *dwo_unit;
6035 const char *comp_dir;
6036
6037 gdb_assert (cu != NULL);
6038
6039 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6040 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6041 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6042
6043 if (per_cu->is_debug_types)
6044 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6045 else
6046 {
6047 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6048
6049 if (!signature.has_value ())
6050 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6051 " [in module %s]"),
6052 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6053
6054 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6055 }
6056
6057 return dwo_unit;
6058 }
6059
6060 /* Subroutine of cutu_reader to simplify it.
6061 See it for a description of the parameters.
6062 Read a TU directly from a DWO file, bypassing the stub. */
6063
6064 void
6065 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6066 dwarf2_per_objfile *per_objfile,
6067 dwarf2_cu *existing_cu)
6068 {
6069 struct signatured_type *sig_type;
6070
6071 /* Verify we can do the following downcast, and that we have the
6072 data we need. */
6073 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6074 sig_type = (struct signatured_type *) this_cu;
6075 gdb_assert (sig_type->dwo_unit != NULL);
6076
6077 dwarf2_cu *cu;
6078
6079 if (existing_cu != nullptr)
6080 {
6081 cu = existing_cu;
6082 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6083 /* There's no need to do the rereading_dwo_cu handling that
6084 cutu_reader does since we don't read the stub. */
6085 }
6086 else
6087 {
6088 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6089 in per_objfile yet. */
6090 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6091 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6092 cu = m_new_cu.get ();
6093 }
6094
6095 /* A future optimization, if needed, would be to use an existing
6096 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6097 could share abbrev tables. */
6098
6099 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6100 NULL /* stub_comp_unit_die */,
6101 sig_type->dwo_unit->dwo_file->comp_dir,
6102 this, &info_ptr,
6103 &comp_unit_die,
6104 &m_dwo_abbrev_table) == 0)
6105 {
6106 /* Dummy die. */
6107 dummy_p = true;
6108 }
6109 }
6110
6111 /* Initialize a CU (or TU) and read its DIEs.
6112 If the CU defers to a DWO file, read the DWO file as well.
6113
6114 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6115 Otherwise the table specified in the comp unit header is read in and used.
6116 This is an optimization for when we already have the abbrev table.
6117
6118 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6119 allocated. */
6120
6121 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6122 dwarf2_per_objfile *per_objfile,
6123 struct abbrev_table *abbrev_table,
6124 dwarf2_cu *existing_cu,
6125 bool skip_partial,
6126 abbrev_cache *cache)
6127 : die_reader_specs {},
6128 m_this_cu (this_cu)
6129 {
6130 struct objfile *objfile = per_objfile->objfile;
6131 struct dwarf2_section_info *section = this_cu->section;
6132 bfd *abfd = section->get_bfd_owner ();
6133 const gdb_byte *begin_info_ptr;
6134 struct signatured_type *sig_type = NULL;
6135 struct dwarf2_section_info *abbrev_section;
6136 /* Non-zero if CU currently points to a DWO file and we need to
6137 reread it. When this happens we need to reread the skeleton die
6138 before we can reread the DWO file (this only applies to CUs, not TUs). */
6139 int rereading_dwo_cu = 0;
6140
6141 if (dwarf_die_debug)
6142 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6143 this_cu->is_debug_types ? "type" : "comp",
6144 sect_offset_str (this_cu->sect_off));
6145
6146 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6147 file (instead of going through the stub), short-circuit all of this. */
6148 if (this_cu->reading_dwo_directly)
6149 {
6150 /* Narrow down the scope of possibilities to have to understand. */
6151 gdb_assert (this_cu->is_debug_types);
6152 gdb_assert (abbrev_table == NULL);
6153 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6154 return;
6155 }
6156
6157 /* This is cheap if the section is already read in. */
6158 section->read (objfile);
6159
6160 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6161
6162 abbrev_section = get_abbrev_section_for_cu (this_cu);
6163
6164 dwarf2_cu *cu;
6165
6166 if (existing_cu != nullptr)
6167 {
6168 cu = existing_cu;
6169 /* If this CU is from a DWO file we need to start over, we need to
6170 refetch the attributes from the skeleton CU.
6171 This could be optimized by retrieving those attributes from when we
6172 were here the first time: the previous comp_unit_die was stored in
6173 comp_unit_obstack. But there's no data yet that we need this
6174 optimization. */
6175 if (cu->dwo_unit != NULL)
6176 rereading_dwo_cu = 1;
6177 }
6178 else
6179 {
6180 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6181 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6182 let us know that the CU is being scanned using the parallel
6183 indexer. This assert is avoided in this case because (1) it
6184 is irrelevant, and (2) the get_cu method is not
6185 thread-safe. */
6186 gdb_assert (cache != nullptr
6187 || per_objfile->get_cu (this_cu) == nullptr);
6188 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6189 cu = m_new_cu.get ();
6190 }
6191
6192 /* Get the header. */
6193 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6194 {
6195 /* We already have the header, there's no need to read it in again. */
6196 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6197 }
6198 else
6199 {
6200 if (this_cu->is_debug_types)
6201 {
6202 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6203 section, abbrev_section,
6204 info_ptr, rcuh_kind::TYPE);
6205
6206 /* Since per_cu is the first member of struct signatured_type,
6207 we can go from a pointer to one to a pointer to the other. */
6208 sig_type = (struct signatured_type *) this_cu;
6209 gdb_assert (sig_type->signature == cu->header.signature);
6210 gdb_assert (sig_type->type_offset_in_tu
6211 == cu->header.type_cu_offset_in_tu);
6212 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6213
6214 /* LENGTH has not been set yet for type units if we're
6215 using .gdb_index. */
6216 this_cu->length = cu->header.get_length ();
6217
6218 /* Establish the type offset that can be used to lookup the type. */
6219 sig_type->type_offset_in_section =
6220 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6221
6222 this_cu->dwarf_version = cu->header.version;
6223 }
6224 else
6225 {
6226 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6227 section, abbrev_section,
6228 info_ptr,
6229 rcuh_kind::COMPILE);
6230
6231 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6232 if (this_cu->length == 0)
6233 this_cu->length = cu->header.get_length ();
6234 else
6235 gdb_assert (this_cu->length == cu->header.get_length ());
6236 this_cu->dwarf_version = cu->header.version;
6237 }
6238 }
6239
6240 /* Skip dummy compilation units. */
6241 if (info_ptr >= begin_info_ptr + this_cu->length
6242 || peek_abbrev_code (abfd, info_ptr) == 0)
6243 {
6244 dummy_p = true;
6245 return;
6246 }
6247
6248 /* If we don't have them yet, read the abbrevs for this compilation unit.
6249 And if we need to read them now, make sure they're freed when we're
6250 done. */
6251 if (abbrev_table != NULL)
6252 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6253 else
6254 {
6255 if (cache != nullptr)
6256 abbrev_table = cache->find (abbrev_section,
6257 cu->header.abbrev_sect_off);
6258 if (abbrev_table == nullptr)
6259 {
6260 abbrev_section->read (objfile);
6261 m_abbrev_table_holder
6262 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6263 abbrev_table = m_abbrev_table_holder.get ();
6264 }
6265 }
6266
6267 /* Read the top level CU/TU die. */
6268 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6269 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6270
6271 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6272 {
6273 dummy_p = true;
6274 return;
6275 }
6276
6277 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6278 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6279 table from the DWO file and pass the ownership over to us. It will be
6280 referenced from READER, so we must make sure to free it after we're done
6281 with READER.
6282
6283 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6284 DWO CU, that this test will fail (the attribute will not be present). */
6285 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6286 if (dwo_name != nullptr)
6287 {
6288 struct dwo_unit *dwo_unit;
6289 struct die_info *dwo_comp_unit_die;
6290
6291 if (comp_unit_die->has_children)
6292 {
6293 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6294 " has children (offset %s) [in module %s]"),
6295 sect_offset_str (this_cu->sect_off),
6296 bfd_get_filename (abfd));
6297 }
6298 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6299 if (dwo_unit != NULL)
6300 {
6301 if (read_cutu_die_from_dwo (cu, dwo_unit,
6302 comp_unit_die, NULL,
6303 this, &info_ptr,
6304 &dwo_comp_unit_die,
6305 &m_dwo_abbrev_table) == 0)
6306 {
6307 /* Dummy die. */
6308 dummy_p = true;
6309 return;
6310 }
6311 comp_unit_die = dwo_comp_unit_die;
6312 }
6313 else
6314 {
6315 /* Yikes, we couldn't find the rest of the DIE, we only have
6316 the stub. A complaint has already been logged. There's
6317 not much more we can do except pass on the stub DIE to
6318 die_reader_func. We don't want to throw an error on bad
6319 debug info. */
6320 }
6321 }
6322 }
6323
6324 void
6325 cutu_reader::keep ()
6326 {
6327 /* Done, clean up. */
6328 gdb_assert (!dummy_p);
6329 if (m_new_cu != NULL)
6330 {
6331 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6332 now. */
6333 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6334 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6335 }
6336 }
6337
6338 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6339 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6340 assumed to have already done the lookup to find the DWO file).
6341
6342 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6343 THIS_CU->is_debug_types, but nothing else.
6344
6345 We fill in THIS_CU->length.
6346
6347 THIS_CU->cu is always freed when done.
6348 This is done in order to not leave THIS_CU->cu in a state where we have
6349 to care whether it refers to the "main" CU or the DWO CU.
6350
6351 When parent_cu is passed, it is used to provide a default value for
6352 str_offsets_base and addr_base from the parent. */
6353
6354 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6355 dwarf2_per_objfile *per_objfile,
6356 struct dwarf2_cu *parent_cu,
6357 struct dwo_file *dwo_file)
6358 : die_reader_specs {},
6359 m_this_cu (this_cu)
6360 {
6361 struct objfile *objfile = per_objfile->objfile;
6362 struct dwarf2_section_info *section = this_cu->section;
6363 bfd *abfd = section->get_bfd_owner ();
6364 struct dwarf2_section_info *abbrev_section;
6365 const gdb_byte *begin_info_ptr, *info_ptr;
6366
6367 if (dwarf_die_debug)
6368 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6369 this_cu->is_debug_types ? "type" : "comp",
6370 sect_offset_str (this_cu->sect_off));
6371
6372 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6373
6374 abbrev_section = (dwo_file != NULL
6375 ? &dwo_file->sections.abbrev
6376 : get_abbrev_section_for_cu (this_cu));
6377
6378 /* This is cheap if the section is already read in. */
6379 section->read (objfile);
6380
6381 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6382
6383 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6384 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6385 section, abbrev_section, info_ptr,
6386 (this_cu->is_debug_types
6387 ? rcuh_kind::TYPE
6388 : rcuh_kind::COMPILE));
6389
6390 if (parent_cu != nullptr)
6391 {
6392 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6393 m_new_cu->addr_base = parent_cu->addr_base;
6394 }
6395 this_cu->length = m_new_cu->header.get_length ();
6396
6397 /* Skip dummy compilation units. */
6398 if (info_ptr >= begin_info_ptr + this_cu->length
6399 || peek_abbrev_code (abfd, info_ptr) == 0)
6400 {
6401 dummy_p = true;
6402 return;
6403 }
6404
6405 abbrev_section->read (objfile);
6406 m_abbrev_table_holder
6407 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6408
6409 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6410 m_abbrev_table_holder.get ());
6411 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6412 }
6413
6414 \f
6415 /* Type Unit Groups.
6416
6417 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6418 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6419 so that all types coming from the same compilation (.o file) are grouped
6420 together. A future step could be to put the types in the same symtab as
6421 the CU the types ultimately came from. */
6422
6423 static hashval_t
6424 hash_type_unit_group (const void *item)
6425 {
6426 const struct type_unit_group *tu_group
6427 = (const struct type_unit_group *) item;
6428
6429 return hash_stmt_list_entry (&tu_group->hash);
6430 }
6431
6432 static int
6433 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6434 {
6435 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6436 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6437
6438 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6439 }
6440
6441 /* Allocate a hash table for type unit groups. */
6442
6443 static htab_up
6444 allocate_type_unit_groups_table ()
6445 {
6446 return htab_up (htab_create_alloc (3,
6447 hash_type_unit_group,
6448 eq_type_unit_group,
6449 htab_delete_entry<type_unit_group>,
6450 xcalloc, xfree));
6451 }
6452
6453 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6454 partial symtabs. We combine several TUs per psymtab to not let the size
6455 of any one psymtab grow too big. */
6456 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6457 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6458
6459 /* Helper routine for get_type_unit_group.
6460 Create the type_unit_group object used to hold one or more TUs. */
6461
6462 static std::unique_ptr<type_unit_group>
6463 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6464 {
6465 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6466
6467 tu_group->hash.dwo_unit = cu->dwo_unit;
6468 tu_group->hash.line_sect_off = line_offset_struct;
6469
6470 return tu_group;
6471 }
6472
6473 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6474 STMT_LIST is a DW_AT_stmt_list attribute. */
6475
6476 static struct type_unit_group *
6477 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6478 {
6479 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6480 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6481 struct type_unit_group *tu_group;
6482 void **slot;
6483 unsigned int line_offset;
6484 struct type_unit_group type_unit_group_for_lookup;
6485
6486 if (per_objfile->per_bfd->type_unit_groups == NULL)
6487 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6488
6489 /* Do we need to create a new group, or can we use an existing one? */
6490
6491 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6492 {
6493 line_offset = stmt_list->as_unsigned ();
6494 ++tu_stats->nr_symtab_sharers;
6495 }
6496 else
6497 {
6498 /* Ugh, no stmt_list. Rare, but we have to handle it.
6499 We can do various things here like create one group per TU or
6500 spread them over multiple groups to split up the expansion work.
6501 To avoid worst case scenarios (too many groups or too large groups)
6502 we, umm, group them in bunches. */
6503 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6504 | (tu_stats->nr_stmt_less_type_units
6505 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6506 ++tu_stats->nr_stmt_less_type_units;
6507 }
6508
6509 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6510 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6511 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6512 &type_unit_group_for_lookup, INSERT);
6513 if (*slot == nullptr)
6514 {
6515 sect_offset line_offset_struct = (sect_offset) line_offset;
6516 std::unique_ptr<type_unit_group> grp
6517 = create_type_unit_group (cu, line_offset_struct);
6518 *slot = grp.release ();
6519 ++tu_stats->nr_symtabs;
6520 }
6521
6522 tu_group = (struct type_unit_group *) *slot;
6523 gdb_assert (tu_group != nullptr);
6524 return tu_group;
6525 }
6526 \f
6527
6528 /* An instance of this is created when scanning DWARF to create a
6529 cooked index. */
6530
6531 class cooked_index_storage
6532 {
6533 public:
6534
6535 cooked_index_storage ()
6536 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
6537 eq_cutu_reader,
6538 htab_delete_entry<cutu_reader>,
6539 xcalloc, xfree)),
6540 m_index (new cooked_index),
6541 m_addrmap_storage (),
6542 m_addrmap (addrmap_create_mutable (&m_addrmap_storage))
6543 {
6544 }
6545
6546 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
6547
6548 /* Return the current abbrev cache. */
6549 abbrev_cache *get_abbrev_cache ()
6550 {
6551 return &m_abbrev_cache;
6552 }
6553
6554 /* Return the DIE reader corresponding to PER_CU. If no such reader
6555 has been registered, return NULL. */
6556 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
6557 {
6558 int index = per_cu->index;
6559 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
6560 &index, index);
6561 }
6562
6563 /* Preserve READER by storing it in the local hash table. */
6564 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
6565 {
6566 m_abbrev_cache.add (reader->release_abbrev_table ());
6567
6568 int index = reader->cu->per_cu->index;
6569 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
6570 index, INSERT);
6571 gdb_assert (*slot == nullptr);
6572 cutu_reader *result = reader.get ();
6573 *slot = reader.release ();
6574 return result;
6575 }
6576
6577 /* Add an entry to the index. The arguments describe the entry; see
6578 cooked-index.h. The new entry is returned. */
6579 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
6580 cooked_index_flag flags,
6581 const char *name,
6582 const cooked_index_entry *parent_entry,
6583 dwarf2_per_cu_data *per_cu)
6584 {
6585 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
6586 }
6587
6588 /* Install the current addrmap into the index being constructed,
6589 then transfer ownership of the index to the caller. */
6590 std::unique_ptr<cooked_index> release ()
6591 {
6592 m_index->install_addrmap (m_addrmap);
6593 return std::move (m_index);
6594 }
6595
6596 /* Return the mutable addrmap that is currently being created. */
6597 addrmap *get_addrmap ()
6598 {
6599 return m_addrmap;
6600 }
6601
6602 private:
6603
6604 /* Hash function for a cutu_reader. */
6605 static hashval_t hash_cutu_reader (const void *a)
6606 {
6607 const cutu_reader *reader = (const cutu_reader *) a;
6608 return reader->cu->per_cu->index;
6609 }
6610
6611 /* Equality function for cutu_reader. */
6612 static int eq_cutu_reader (const void *a, const void *b)
6613 {
6614 const cutu_reader *ra = (const cutu_reader *) a;
6615 const int *rb = (const int *) b;
6616 return ra->cu->per_cu->index == *rb;
6617 }
6618
6619 /* The abbrev cache used by this indexer. */
6620 abbrev_cache m_abbrev_cache;
6621 /* A hash table of cutu_reader objects. */
6622 htab_up m_reader_hash;
6623 /* The index that is being constructed. */
6624 std::unique_ptr<cooked_index> m_index;
6625
6626 /* Storage for the writeable addrmap. */
6627 auto_obstack m_addrmap_storage;
6628 /* A writeable addrmap being constructed by this scanner. */
6629 addrmap *m_addrmap;
6630 };
6631
6632 /* An instance of this is created to index a CU. */
6633
6634 class cooked_indexer
6635 {
6636 public:
6637
6638 cooked_indexer (cooked_index_storage *storage,
6639 dwarf2_per_cu_data *per_cu,
6640 enum language language)
6641 : m_index_storage (storage),
6642 m_per_cu (per_cu),
6643 m_language (language),
6644 m_obstack (),
6645 m_die_range_map (addrmap_create_mutable (&m_obstack))
6646 {
6647 }
6648
6649 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
6650
6651 /* Index the given CU. */
6652 void make_index (cutu_reader *reader);
6653
6654 private:
6655
6656 /* A helper function to turn a section offset into an address that
6657 can be used in an addrmap. */
6658 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
6659 {
6660 CORE_ADDR value = to_underlying (offset);
6661 if (is_dwz)
6662 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
6663 return value;
6664 }
6665
6666 /* A helper function to scan the PC bounds of READER and record them
6667 in the storage's addrmap. */
6668 void check_bounds (cutu_reader *reader);
6669
6670 /* Ensure that the indicated CU exists. The cutu_reader for it is
6671 returned. FOR_SCANNING is true if the caller intends to scan all
6672 the DIEs in the CU; when false, this use is assumed to be to look
6673 up just a single DIE. */
6674 cutu_reader *ensure_cu_exists (cutu_reader *reader,
6675 dwarf2_per_objfile *per_objfile,
6676 sect_offset sect_off,
6677 bool is_dwz,
6678 bool for_scanning);
6679
6680 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6681 the entry for the enclosing scope (nullptr at top level). FULLY
6682 is true when a full scan must be done -- in some languages,
6683 function scopes must be fully explored in order to find nested
6684 functions. This returns a pointer to just after the spot where
6685 reading stopped. */
6686 const gdb_byte *index_dies (cutu_reader *reader,
6687 const gdb_byte *info_ptr,
6688 const cooked_index_entry *parent_entry,
6689 bool fully);
6690
6691 /* Scan the attributes for a given DIE and update the out
6692 parameters. Returns a pointer to the byte after the DIE. */
6693 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
6694 cutu_reader *reader,
6695 const gdb_byte *watermark_ptr,
6696 const gdb_byte *info_ptr,
6697 const abbrev_info *abbrev,
6698 const char **name,
6699 const char **linkage_name,
6700 cooked_index_flag *flags,
6701 sect_offset *sibling_offset,
6702 const cooked_index_entry **parent_entry,
6703 CORE_ADDR *maybe_defer,
6704 bool for_specification);
6705
6706 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6707 DW_AT_import, and then scanning the referenced CU. Returns a
6708 pointer to the byte after the DIE. */
6709 const gdb_byte *index_imported_unit (cutu_reader *reader,
6710 const gdb_byte *info_ptr,
6711 const abbrev_info *abbrev);
6712
6713 /* Recursively read DIEs, recording the section offsets in
6714 m_die_range_map and then calling index_dies. */
6715 const gdb_byte *recurse (cutu_reader *reader,
6716 const gdb_byte *info_ptr,
6717 const cooked_index_entry *parent_entry,
6718 bool fully);
6719
6720 /* The storage object, where the results are kept. */
6721 cooked_index_storage *m_index_storage;
6722 /* The CU that we are reading on behalf of. This object might be
6723 asked to index one CU but to treat the results as if they come
6724 from some including CU; in this case the including CU would be
6725 recorded here. */
6726 dwarf2_per_cu_data *m_per_cu;
6727 /* The language that we're assuming when reading. */
6728 enum language m_language;
6729
6730 /* Temporary storage. */
6731 auto_obstack m_obstack;
6732 /* An addrmap that maps from section offsets (see the form_addr
6733 method) to newly-created entries. See m_deferred_entries to
6734 understand this. */
6735 addrmap *m_die_range_map;
6736
6737 /* A single deferred entry. */
6738 struct deferred_entry
6739 {
6740 sect_offset die_offset;
6741 const char *name;
6742 CORE_ADDR spec_offset;
6743 dwarf_tag tag;
6744 cooked_index_flag flags;
6745 };
6746
6747 /* The generated DWARF can sometimes have the declaration for a
6748 method in a class (or perhaps namespace) scope, with the
6749 definition appearing outside this scope... just one of the many
6750 bad things about DWARF. In order to handle this situation, we
6751 defer certain entries until the end of scanning, at which point
6752 we'll know the containing context of all the DIEs that we might
6753 have scanned. This vector stores these deferred entries. */
6754 std::vector<deferred_entry> m_deferred_entries;
6755 };
6756
6757 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6758 Process compilation unit THIS_CU for a psymtab. */
6759
6760 static void
6761 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
6762 dwarf2_per_objfile *per_objfile,
6763 cooked_index_storage *storage)
6764 {
6765 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
6766 storage->get_abbrev_cache ());
6767
6768 if (reader.comp_unit_die == nullptr)
6769 return;
6770
6771 switch (reader.comp_unit_die->tag)
6772 {
6773 case DW_TAG_compile_unit:
6774 this_cu->unit_type = DW_UT_compile;
6775 break;
6776 case DW_TAG_partial_unit:
6777 this_cu->unit_type = DW_UT_partial;
6778 break;
6779 case DW_TAG_type_unit:
6780 this_cu->unit_type = DW_UT_type;
6781 break;
6782 default:
6783 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6784 dwarf_tag_name (reader.comp_unit_die->tag),
6785 sect_offset_str (reader.cu->per_cu->sect_off),
6786 objfile_name (per_objfile->objfile));
6787 }
6788
6789 if (reader.dummy_p)
6790 {
6791 /* Nothing. */
6792 }
6793 else if (this_cu->is_debug_types)
6794 build_type_psymtabs_reader (&reader, storage);
6795 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
6796 {
6797 bool nope = false;
6798 if (this_cu->scanned.compare_exchange_strong (nope, true))
6799 {
6800 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
6801 language_minimal);
6802 gdb_assert (storage != nullptr);
6803 cooked_indexer indexer (storage, this_cu, reader.cu->per_cu->lang);
6804 indexer.make_index (&reader);
6805 }
6806 }
6807 }
6808
6809 /* Reader function for build_type_psymtabs. */
6810
6811 static void
6812 build_type_psymtabs_reader (cutu_reader *reader,
6813 cooked_index_storage *storage)
6814 {
6815 struct dwarf2_cu *cu = reader->cu;
6816 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6817 struct die_info *type_unit_die = reader->comp_unit_die;
6818
6819 gdb_assert (per_cu->is_debug_types);
6820
6821 if (! type_unit_die->has_children)
6822 return;
6823
6824 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6825
6826 gdb_assert (storage != nullptr);
6827 cooked_indexer indexer (storage, per_cu, cu->per_cu->lang);
6828 indexer.make_index (reader);
6829 }
6830
6831 /* Struct used to sort TUs by their abbreviation table offset. */
6832
6833 struct tu_abbrev_offset
6834 {
6835 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
6836 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
6837 {}
6838
6839 /* This is used when sorting. */
6840 bool operator< (const tu_abbrev_offset &other) const
6841 {
6842 return abbrev_offset < other.abbrev_offset;
6843 }
6844
6845 signatured_type *sig_type;
6846 sect_offset abbrev_offset;
6847 };
6848
6849 /* Efficiently read all the type units.
6850
6851 The efficiency is because we sort TUs by the abbrev table they use and
6852 only read each abbrev table once. In one program there are 200K TUs
6853 sharing 8K abbrev tables.
6854
6855 The main purpose of this function is to support building the
6856 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6857 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6858 can collapse the search space by grouping them by stmt_list.
6859 The savings can be significant, in the same program from above the 200K TUs
6860 share 8K stmt_list tables.
6861
6862 FUNC is expected to call get_type_unit_group, which will create the
6863 struct type_unit_group if necessary and add it to
6864 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6865
6866 static void
6867 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
6868 cooked_index_storage *storage)
6869 {
6870 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6871 abbrev_table_up abbrev_table;
6872 sect_offset abbrev_offset;
6873
6874 /* It's up to the caller to not call us multiple times. */
6875 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
6876
6877 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
6878 return;
6879
6880 /* TUs typically share abbrev tables, and there can be way more TUs than
6881 abbrev tables. Sort by abbrev table to reduce the number of times we
6882 read each abbrev table in.
6883 Alternatives are to punt or to maintain a cache of abbrev tables.
6884 This is simpler and efficient enough for now.
6885
6886 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6887 symtab to use). Typically TUs with the same abbrev offset have the same
6888 stmt_list value too so in practice this should work well.
6889
6890 The basic algorithm here is:
6891
6892 sort TUs by abbrev table
6893 for each TU with same abbrev table:
6894 read abbrev table if first user
6895 read TU top level DIE
6896 [IWBN if DWO skeletons had DW_AT_stmt_list]
6897 call FUNC */
6898
6899 dwarf_read_debug_printf ("Building type unit groups ...");
6900
6901 /* Sort in a separate table to maintain the order of all_comp_units
6902 for .gdb_index: TU indices directly index all_type_units. */
6903 std::vector<tu_abbrev_offset> sorted_by_abbrev;
6904 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
6905
6906 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
6907 {
6908 if (cu->is_debug_types)
6909 {
6910 auto sig_type = static_cast<signatured_type *> (cu.get ());
6911 sorted_by_abbrev.emplace_back
6912 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
6913 sig_type->sect_off));
6914 }
6915 }
6916
6917 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
6918
6919 abbrev_offset = (sect_offset) ~(unsigned) 0;
6920
6921 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
6922 {
6923 /* Switch to the next abbrev table if necessary. */
6924 if (abbrev_table == NULL
6925 || tu.abbrev_offset != abbrev_offset)
6926 {
6927 abbrev_offset = tu.abbrev_offset;
6928 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
6929 abbrev_table =
6930 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
6931 ++tu_stats->nr_uniq_abbrev_tables;
6932 }
6933
6934 cutu_reader reader (tu.sig_type, per_objfile,
6935 abbrev_table.get (), nullptr, false);
6936 if (!reader.dummy_p)
6937 build_type_psymtabs_reader (&reader, storage);
6938 }
6939 }
6940
6941 /* Print collected type unit statistics. */
6942
6943 static void
6944 print_tu_stats (dwarf2_per_objfile *per_objfile)
6945 {
6946 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6947
6948 dwarf_read_debug_printf ("Type unit statistics:");
6949 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6950 dwarf_read_debug_printf (" %d uniq abbrev tables",
6951 tu_stats->nr_uniq_abbrev_tables);
6952 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
6953 tu_stats->nr_symtabs);
6954 dwarf_read_debug_printf (" %d symtab sharers",
6955 tu_stats->nr_symtab_sharers);
6956 dwarf_read_debug_printf (" %d type units without a stmt_list",
6957 tu_stats->nr_stmt_less_type_units);
6958 dwarf_read_debug_printf (" %d all_type_units reallocs",
6959 tu_stats->nr_all_type_units_reallocs);
6960 }
6961
6962 struct skeleton_data
6963 {
6964 dwarf2_per_objfile *per_objfile;
6965 cooked_index_storage *storage;
6966 };
6967
6968 /* Traversal function for process_skeletonless_type_unit.
6969 Read a TU in a DWO file and build partial symbols for it. */
6970
6971 static int
6972 process_skeletonless_type_unit (void **slot, void *info)
6973 {
6974 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6975 skeleton_data *data = (skeleton_data *) info;
6976
6977 /* If this TU doesn't exist in the global table, add it and read it in. */
6978
6979 if (data->per_objfile->per_bfd->signatured_types == NULL)
6980 data->per_objfile->per_bfd->signatured_types
6981 = allocate_signatured_type_table ();
6982
6983 signatured_type find_entry (dwo_unit->signature);
6984 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
6985 &find_entry, INSERT);
6986 /* If we've already seen this type there's nothing to do. What's happening
6987 is we're doing our own version of comdat-folding here. */
6988 if (*slot != NULL)
6989 return 1;
6990
6991 /* This does the job that create_all_comp_units would have done for
6992 this TU. */
6993 signatured_type *entry
6994 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
6995 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
6996 *slot = entry;
6997
6998 /* This does the job that build_type_psymtabs would have done. */
6999 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
7000 if (!reader.dummy_p)
7001 build_type_psymtabs_reader (&reader, data->storage);
7002
7003 return 1;
7004 }
7005
7006 /* Traversal function for process_skeletonless_type_units. */
7007
7008 static int
7009 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7010 {
7011 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7012
7013 if (dwo_file->tus != NULL)
7014 htab_traverse_noresize (dwo_file->tus.get (),
7015 process_skeletonless_type_unit, info);
7016
7017 return 1;
7018 }
7019
7020 /* Scan all TUs of DWO files, verifying we've processed them.
7021 This is needed in case a TU was emitted without its skeleton.
7022 Note: This can't be done until we know what all the DWO files are. */
7023
7024 static void
7025 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
7026 cooked_index_storage *storage)
7027 {
7028 skeleton_data data { per_objfile, storage };
7029
7030 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7031 if (get_dwp_file (per_objfile) == NULL
7032 && per_objfile->per_bfd->dwo_files != NULL)
7033 {
7034 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7035 process_dwo_file_for_skeletonless_type_units,
7036 &data);
7037 }
7038 }
7039
7040 /* Build the partial symbol table by doing a quick pass through the
7041 .debug_info and .debug_abbrev sections. */
7042
7043 static void
7044 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7045 {
7046 struct objfile *objfile = per_objfile->objfile;
7047 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7048
7049 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7050 objfile_name (objfile));
7051
7052 per_bfd->map_info_sections (objfile);
7053
7054 cooked_index_storage index_storage;
7055 create_all_comp_units (per_objfile);
7056 build_type_psymtabs (per_objfile, &index_storage);
7057 std::vector<std::unique_ptr<cooked_index>> indexes;
7058
7059 per_bfd->quick_file_names_table
7060 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
7061 if (!per_bfd->debug_aranges.empty ())
7062 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
7063 index_storage.get_addrmap ());
7064
7065 {
7066 /* Ensure that complaints are handled correctly. */
7067 complaint_interceptor complaint_handler;
7068
7069 using iter_type = decltype (per_bfd->all_comp_units.begin ());
7070
7071 /* Each thread returns a pair holding a cooked index, and a vector
7072 of errors that should be printed. The latter is done because
7073 GDB's I/O system is not thread-safe. run_on_main_thread could be
7074 used, but that would mean the messages are printed after the
7075 prompt, which looks weird. */
7076 using result_type = std::pair<std::unique_ptr<cooked_index>,
7077 std::vector<gdb_exception>>;
7078 std::vector<result_type> results
7079 = gdb::parallel_for_each (1, per_bfd->all_comp_units.begin (),
7080 per_bfd->all_comp_units.end (),
7081 [=] (iter_type iter, iter_type end)
7082 {
7083 std::vector<gdb_exception> errors;
7084 cooked_index_storage thread_storage;
7085 for (; iter != end; ++iter)
7086 {
7087 dwarf2_per_cu_data *per_cu = iter->get ();
7088 try
7089 {
7090 process_psymtab_comp_unit (per_cu, per_objfile,
7091 &thread_storage);
7092 }
7093 catch (gdb_exception &except)
7094 {
7095 errors.push_back (std::move (except));
7096 }
7097 }
7098 return result_type (thread_storage.release (), std::move (errors));
7099 });
7100
7101 /* Only show a given exception a single time. */
7102 std::unordered_set<gdb_exception> seen_exceptions;
7103 for (auto &one_result : results)
7104 {
7105 indexes.push_back (std::move (one_result.first));
7106 for (auto &one_exc : one_result.second)
7107 if (seen_exceptions.insert (one_exc).second)
7108 exception_print (gdb_stderr, one_exc);
7109 }
7110 }
7111
7112 /* This has to wait until we read the CUs, we need the list of DWOs. */
7113 process_skeletonless_type_units (per_objfile, &index_storage);
7114
7115 if (dwarf_read_debug > 0)
7116 print_tu_stats (per_objfile);
7117
7118 indexes.push_back (index_storage.release ());
7119 /* Remove any NULL entries. This might happen if parallel-for
7120 decides to throttle the number of threads that were used. */
7121 indexes.erase
7122 (std::remove_if (indexes.begin (),
7123 indexes.end (),
7124 [] (const std::unique_ptr<cooked_index> &entry)
7125 {
7126 return entry == nullptr;
7127 }),
7128 indexes.end ());
7129 indexes.shrink_to_fit ();
7130
7131 cooked_index_vector *vec = new cooked_index_vector (std::move (indexes));
7132 per_bfd->index_table.reset (vec);
7133
7134 const cooked_index_entry *main_entry = vec->get_main ();
7135 if (main_entry != nullptr)
7136 set_objfile_main_name (objfile, main_entry->name,
7137 main_entry->per_cu->lang);
7138
7139 dwarf_read_debug_printf ("Done building psymtabs of %s",
7140 objfile_name (objfile));
7141 }
7142
7143 static void
7144 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7145 struct dwarf2_section_info *section,
7146 struct dwarf2_section_info *abbrev_section,
7147 unsigned int is_dwz,
7148 htab_up &types_htab,
7149 rcuh_kind section_kind)
7150 {
7151 const gdb_byte *info_ptr;
7152 struct objfile *objfile = per_objfile->objfile;
7153
7154 dwarf_read_debug_printf ("Reading %s for %s",
7155 section->get_name (),
7156 section->get_file_name ());
7157
7158 section->read (objfile);
7159
7160 info_ptr = section->buffer;
7161
7162 while (info_ptr < section->buffer + section->size)
7163 {
7164 dwarf2_per_cu_data_up this_cu;
7165
7166 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7167
7168 comp_unit_head cu_header;
7169 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7170 abbrev_section, info_ptr,
7171 section_kind);
7172
7173 /* Save the compilation unit for later lookup. */
7174 if (cu_header.unit_type != DW_UT_type)
7175 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7176 else
7177 {
7178 if (types_htab == nullptr)
7179 types_htab = allocate_signatured_type_table ();
7180
7181 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7182 (cu_header.signature);
7183 signatured_type *sig_ptr = sig_type.get ();
7184 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7185 this_cu.reset (sig_type.release ());
7186
7187 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7188 gdb_assert (slot != nullptr);
7189 if (*slot != nullptr)
7190 complaint (_("debug type entry at offset %s is duplicate to"
7191 " the entry at offset %s, signature %s"),
7192 sect_offset_str (sect_off),
7193 sect_offset_str (sig_ptr->sect_off),
7194 hex_string (sig_ptr->signature));
7195 *slot = sig_ptr;
7196 }
7197 this_cu->sect_off = sect_off;
7198 this_cu->length = cu_header.length + cu_header.initial_length_size;
7199 this_cu->is_dwz = is_dwz;
7200 this_cu->section = section;
7201
7202 info_ptr = info_ptr + this_cu->length;
7203 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7204 }
7205 }
7206
7207 /* Create a list of all compilation units in OBJFILE.
7208 This is only done for -readnow and building partial symtabs. */
7209
7210 static void
7211 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7212 {
7213 htab_up types_htab;
7214
7215 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7216 &per_objfile->per_bfd->abbrev, 0,
7217 types_htab, rcuh_kind::COMPILE);
7218 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7219 read_comp_units_from_section (per_objfile, &section,
7220 &per_objfile->per_bfd->abbrev, 0,
7221 types_htab, rcuh_kind::TYPE);
7222
7223 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7224 if (dwz != NULL)
7225 {
7226 /* Pre-read the sections we'll need to construct an index. */
7227 struct objfile *objfile = per_objfile->objfile;
7228 dwz->abbrev.read (objfile);
7229 dwz->info.read (objfile);
7230 dwz->str.read (objfile);
7231 dwz->line.read (objfile);
7232 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7233 types_htab, rcuh_kind::COMPILE);
7234 }
7235
7236 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7237 }
7238
7239 /* Return the initial uleb128 in the die at INFO_PTR. */
7240
7241 static unsigned int
7242 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7243 {
7244 unsigned int bytes_read;
7245
7246 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7247 }
7248
7249 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7250 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7251
7252 Return the corresponding abbrev, or NULL if the number is zero (indicating
7253 an empty DIE). In either case *BYTES_READ will be set to the length of
7254 the initial number. */
7255
7256 static const struct abbrev_info *
7257 peek_die_abbrev (const die_reader_specs &reader,
7258 const gdb_byte *info_ptr, unsigned int *bytes_read)
7259 {
7260 dwarf2_cu *cu = reader.cu;
7261 bfd *abfd = reader.abfd;
7262 unsigned int abbrev_number
7263 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7264
7265 if (abbrev_number == 0)
7266 return NULL;
7267
7268 const abbrev_info *abbrev
7269 = reader.abbrev_table->lookup_abbrev (abbrev_number);
7270 if (!abbrev)
7271 {
7272 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7273 " at offset %s [in module %s]"),
7274 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7275 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
7276 }
7277
7278 return abbrev;
7279 }
7280
7281 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7282 Returns a pointer to the end of a series of DIEs, terminated by an empty
7283 DIE. Any children of the skipped DIEs will also be skipped. */
7284
7285 static const gdb_byte *
7286 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7287 {
7288 while (1)
7289 {
7290 unsigned int bytes_read;
7291 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
7292 &bytes_read);
7293
7294 if (abbrev == NULL)
7295 return info_ptr + bytes_read;
7296 else
7297 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7298 }
7299 }
7300
7301 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7302 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7303 abbrev corresponding to that skipped uleb128 should be passed in
7304 ABBREV.
7305
7306 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7307 returns a pointer to this DIE's sibling, skipping any children.
7308 Otherwise, returns a pointer to the DIE's first child. */
7309
7310 static const gdb_byte *
7311 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7312 const struct abbrev_info *abbrev, bool do_skip_children)
7313 {
7314 unsigned int bytes_read;
7315 struct attribute attr;
7316 bfd *abfd = reader->abfd;
7317 struct dwarf2_cu *cu = reader->cu;
7318 const gdb_byte *buffer = reader->buffer;
7319 const gdb_byte *buffer_end = reader->buffer_end;
7320 unsigned int form, i;
7321
7322 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
7323 {
7324 /* We only handle DW_FORM_ref4 here. */
7325 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
7326 unsigned int offset = read_4_bytes (abfd, sibling_data);
7327 const gdb_byte *sibling_ptr
7328 = buffer + to_underlying (cu->header.sect_off) + offset;
7329 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
7330 return sibling_ptr;
7331 /* Fall through to the slow way. */
7332 }
7333 else if (abbrev->size_if_constant != 0)
7334 {
7335 info_ptr += abbrev->size_if_constant;
7336 if (do_skip_children && abbrev->has_children)
7337 return skip_children (reader, info_ptr);
7338 return info_ptr;
7339 }
7340
7341 for (i = 0; i < abbrev->num_attrs; i++)
7342 {
7343 /* The only abbrev we care about is DW_AT_sibling. */
7344 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
7345 {
7346 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7347 if (attr.form == DW_FORM_ref_addr)
7348 complaint (_("ignoring absolute DW_AT_sibling"));
7349 else
7350 {
7351 sect_offset off = attr.get_ref_die_offset ();
7352 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7353
7354 if (sibling_ptr < info_ptr)
7355 complaint (_("DW_AT_sibling points backwards"));
7356 else if (sibling_ptr > reader->buffer_end)
7357 reader->die_section->overflow_complaint ();
7358 else
7359 return sibling_ptr;
7360 }
7361 }
7362
7363 /* If it isn't DW_AT_sibling, skip this attribute. */
7364 form = abbrev->attrs[i].form;
7365 skip_attribute:
7366 switch (form)
7367 {
7368 case DW_FORM_ref_addr:
7369 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7370 and later it is offset sized. */
7371 if (cu->header.version == 2)
7372 info_ptr += cu->header.addr_size;
7373 else
7374 info_ptr += cu->header.offset_size;
7375 break;
7376 case DW_FORM_GNU_ref_alt:
7377 info_ptr += cu->header.offset_size;
7378 break;
7379 case DW_FORM_addr:
7380 info_ptr += cu->header.addr_size;
7381 break;
7382 case DW_FORM_data1:
7383 case DW_FORM_ref1:
7384 case DW_FORM_flag:
7385 case DW_FORM_strx1:
7386 info_ptr += 1;
7387 break;
7388 case DW_FORM_flag_present:
7389 case DW_FORM_implicit_const:
7390 break;
7391 case DW_FORM_data2:
7392 case DW_FORM_ref2:
7393 case DW_FORM_strx2:
7394 info_ptr += 2;
7395 break;
7396 case DW_FORM_strx3:
7397 info_ptr += 3;
7398 break;
7399 case DW_FORM_data4:
7400 case DW_FORM_ref4:
7401 case DW_FORM_strx4:
7402 info_ptr += 4;
7403 break;
7404 case DW_FORM_data8:
7405 case DW_FORM_ref8:
7406 case DW_FORM_ref_sig8:
7407 info_ptr += 8;
7408 break;
7409 case DW_FORM_data16:
7410 info_ptr += 16;
7411 break;
7412 case DW_FORM_string:
7413 read_direct_string (abfd, info_ptr, &bytes_read);
7414 info_ptr += bytes_read;
7415 break;
7416 case DW_FORM_sec_offset:
7417 case DW_FORM_strp:
7418 case DW_FORM_GNU_strp_alt:
7419 info_ptr += cu->header.offset_size;
7420 break;
7421 case DW_FORM_exprloc:
7422 case DW_FORM_block:
7423 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7424 info_ptr += bytes_read;
7425 break;
7426 case DW_FORM_block1:
7427 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7428 break;
7429 case DW_FORM_block2:
7430 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7431 break;
7432 case DW_FORM_block4:
7433 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7434 break;
7435 case DW_FORM_addrx:
7436 case DW_FORM_strx:
7437 case DW_FORM_sdata:
7438 case DW_FORM_udata:
7439 case DW_FORM_ref_udata:
7440 case DW_FORM_GNU_addr_index:
7441 case DW_FORM_GNU_str_index:
7442 case DW_FORM_rnglistx:
7443 case DW_FORM_loclistx:
7444 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7445 break;
7446 case DW_FORM_indirect:
7447 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7448 info_ptr += bytes_read;
7449 /* We need to continue parsing from here, so just go back to
7450 the top. */
7451 goto skip_attribute;
7452
7453 default:
7454 error (_("Dwarf Error: Cannot handle %s "
7455 "in DWARF reader [in module %s]"),
7456 dwarf_form_name (form),
7457 bfd_get_filename (abfd));
7458 }
7459 }
7460
7461 if (do_skip_children && abbrev->has_children)
7462 return skip_children (reader, info_ptr);
7463 else
7464 return info_ptr;
7465 }
7466 \f
7467 /* Reading in full CUs. */
7468
7469 /* Add PER_CU to the queue. */
7470
7471 static void
7472 queue_comp_unit (dwarf2_per_cu_data *per_cu,
7473 dwarf2_per_objfile *per_objfile,
7474 enum language pretend_language)
7475 {
7476 per_cu->queued = 1;
7477
7478 gdb_assert (per_objfile->per_bfd->queue.has_value ());
7479 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
7480 }
7481
7482 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7483
7484 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7485 dependency.
7486
7487 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7488 DIEs, false otherwise.
7489
7490 Explanation: there is an invariant that if a CU is queued for expansion
7491 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7492 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7493 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7494 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7495 invariant is respected.
7496
7497 The caller is therefore not required to load the CU's DIEs (we return false)
7498 if:
7499
7500 - the CU is already expanded, and therefore does not get enqueued
7501 - the CU gets enqueued for expansion, but its DIEs are already loaded
7502
7503 Note that the caller should not use this function's return value as an
7504 indicator of whether the CU's DIEs are loaded right now, it should check
7505 that by calling `dwarf2_per_objfile::get_cu` instead. */
7506
7507 static int
7508 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7509 dwarf2_per_cu_data *per_cu,
7510 dwarf2_per_objfile *per_objfile,
7511 enum language pretend_language)
7512 {
7513 /* Mark the dependence relation so that we don't flush PER_CU
7514 too early. */
7515 if (dependent_cu != NULL)
7516 dependent_cu->add_dependence (per_cu);
7517
7518 /* If it's already on the queue, we have nothing to do. */
7519 if (per_cu->queued)
7520 {
7521 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7522 loaded. */
7523 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
7524
7525 /* If the CU is queued for expansion, it should not already be
7526 expanded. */
7527 gdb_assert (!per_objfile->symtab_set_p (per_cu));
7528
7529 /* The DIEs are already loaded, the caller doesn't need to do it. */
7530 return 0;
7531 }
7532
7533 bool queued = false;
7534 if (!per_objfile->symtab_set_p (per_cu))
7535 {
7536 /* Add it to the queue. */
7537 queue_comp_unit (per_cu, per_objfile, pretend_language);
7538 queued = true;
7539 }
7540
7541 /* If the compilation unit is already loaded, just mark it as
7542 used. */
7543 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7544 if (cu != nullptr)
7545 cu->last_used = 0;
7546
7547 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7548 and the DIEs are not already loaded. */
7549 return queued && cu == nullptr;
7550 }
7551
7552 /* Process the queue. */
7553
7554 static void
7555 process_queue (dwarf2_per_objfile *per_objfile)
7556 {
7557 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7558 objfile_name (per_objfile->objfile));
7559
7560 /* The queue starts out with one item, but following a DIE reference
7561 may load a new CU, adding it to the end of the queue. */
7562 while (!per_objfile->per_bfd->queue->empty ())
7563 {
7564 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7565 dwarf2_per_cu_data *per_cu = item.per_cu;
7566
7567 if (!per_objfile->symtab_set_p (per_cu))
7568 {
7569 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7570
7571 /* Skip dummy CUs. */
7572 if (cu != nullptr)
7573 {
7574 unsigned int debug_print_threshold;
7575 char buf[100];
7576
7577 if (per_cu->is_debug_types)
7578 {
7579 struct signatured_type *sig_type =
7580 (struct signatured_type *) per_cu;
7581
7582 sprintf (buf, "TU %s at offset %s",
7583 hex_string (sig_type->signature),
7584 sect_offset_str (per_cu->sect_off));
7585 /* There can be 100s of TUs.
7586 Only print them in verbose mode. */
7587 debug_print_threshold = 2;
7588 }
7589 else
7590 {
7591 sprintf (buf, "CU at offset %s",
7592 sect_offset_str (per_cu->sect_off));
7593 debug_print_threshold = 1;
7594 }
7595
7596 if (dwarf_read_debug >= debug_print_threshold)
7597 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
7598
7599 if (per_cu->is_debug_types)
7600 process_full_type_unit (cu, item.pretend_language);
7601 else
7602 process_full_comp_unit (cu, item.pretend_language);
7603
7604 if (dwarf_read_debug >= debug_print_threshold)
7605 dwarf_read_debug_printf ("Done expanding %s", buf);
7606 }
7607 }
7608
7609 per_cu->queued = 0;
7610 per_objfile->per_bfd->queue->pop ();
7611 }
7612
7613 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7614 objfile_name (per_objfile->objfile));
7615 }
7616
7617 /* Trivial hash function for die_info: the hash value of a DIE
7618 is its offset in .debug_info for this objfile. */
7619
7620 static hashval_t
7621 die_hash (const void *item)
7622 {
7623 const struct die_info *die = (const struct die_info *) item;
7624
7625 return to_underlying (die->sect_off);
7626 }
7627
7628 /* Trivial comparison function for die_info structures: two DIEs
7629 are equal if they have the same offset. */
7630
7631 static int
7632 die_eq (const void *item_lhs, const void *item_rhs)
7633 {
7634 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7635 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7636
7637 return die_lhs->sect_off == die_rhs->sect_off;
7638 }
7639
7640 /* Load the DIEs associated with PER_CU into memory.
7641
7642 In some cases, the caller, while reading partial symbols, will need to load
7643 the full symbols for the CU for some reason. It will already have a
7644 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7645 rather than creating a new one. */
7646
7647 static void
7648 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
7649 dwarf2_per_objfile *per_objfile,
7650 dwarf2_cu *existing_cu,
7651 bool skip_partial,
7652 enum language pretend_language)
7653 {
7654 gdb_assert (! this_cu->is_debug_types);
7655
7656 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
7657 if (reader.dummy_p)
7658 return;
7659
7660 struct dwarf2_cu *cu = reader.cu;
7661 const gdb_byte *info_ptr = reader.info_ptr;
7662
7663 gdb_assert (cu->die_hash == NULL);
7664 cu->die_hash =
7665 htab_create_alloc_ex (cu->header.length / 12,
7666 die_hash,
7667 die_eq,
7668 NULL,
7669 &cu->comp_unit_obstack,
7670 hashtab_obstack_allocate,
7671 dummy_obstack_deallocate);
7672
7673 if (reader.comp_unit_die->has_children)
7674 reader.comp_unit_die->child
7675 = read_die_and_siblings (&reader, reader.info_ptr,
7676 &info_ptr, reader.comp_unit_die);
7677 cu->dies = reader.comp_unit_die;
7678 /* comp_unit_die is not stored in die_hash, no need. */
7679
7680 /* We try not to read any attributes in this function, because not
7681 all CUs needed for references have been loaded yet, and symbol
7682 table processing isn't initialized. But we have to set the CU language,
7683 or we won't be able to build types correctly.
7684 Similarly, if we do not read the producer, we can not apply
7685 producer-specific interpretation. */
7686 prepare_one_comp_unit (cu, cu->dies, pretend_language);
7687
7688 reader.keep ();
7689 }
7690
7691 /* Add a DIE to the delayed physname list. */
7692
7693 static void
7694 add_to_method_list (struct type *type, int fnfield_index, int index,
7695 const char *name, struct die_info *die,
7696 struct dwarf2_cu *cu)
7697 {
7698 struct delayed_method_info mi;
7699 mi.type = type;
7700 mi.fnfield_index = fnfield_index;
7701 mi.index = index;
7702 mi.name = name;
7703 mi.die = die;
7704 cu->method_list.push_back (mi);
7705 }
7706
7707 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7708 "const" / "volatile". If so, decrements LEN by the length of the
7709 modifier and return true. Otherwise return false. */
7710
7711 template<size_t N>
7712 static bool
7713 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
7714 {
7715 size_t mod_len = sizeof (mod) - 1;
7716 if (len > mod_len && startswith (physname + (len - mod_len), mod))
7717 {
7718 len -= mod_len;
7719 return true;
7720 }
7721 return false;
7722 }
7723
7724 /* Compute the physnames of any methods on the CU's method list.
7725
7726 The computation of method physnames is delayed in order to avoid the
7727 (bad) condition that one of the method's formal parameters is of an as yet
7728 incomplete type. */
7729
7730 static void
7731 compute_delayed_physnames (struct dwarf2_cu *cu)
7732 {
7733 /* Only C++ delays computing physnames. */
7734 if (cu->method_list.empty ())
7735 return;
7736 gdb_assert (cu->per_cu->lang == language_cplus);
7737
7738 for (const delayed_method_info &mi : cu->method_list)
7739 {
7740 const char *physname;
7741 struct fn_fieldlist *fn_flp
7742 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
7743 physname = dwarf2_physname (mi.name, mi.die, cu);
7744 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
7745 = physname ? physname : "";
7746
7747 /* Since there's no tag to indicate whether a method is a
7748 const/volatile overload, extract that information out of the
7749 demangled name. */
7750 if (physname != NULL)
7751 {
7752 size_t len = strlen (physname);
7753
7754 while (1)
7755 {
7756 if (physname[len] == ')') /* shortcut */
7757 break;
7758 else if (check_modifier (physname, len, " const"))
7759 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
7760 else if (check_modifier (physname, len, " volatile"))
7761 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
7762 else
7763 break;
7764 }
7765 }
7766 }
7767
7768 /* The list is no longer needed. */
7769 cu->method_list.clear ();
7770 }
7771
7772 /* Go objects should be embedded in a DW_TAG_module DIE,
7773 and it's not clear if/how imported objects will appear.
7774 To keep Go support simple until that's worked out,
7775 go back through what we've read and create something usable.
7776 We could do this while processing each DIE, and feels kinda cleaner,
7777 but that way is more invasive.
7778 This is to, for example, allow the user to type "p var" or "b main"
7779 without having to specify the package name, and allow lookups
7780 of module.object to work in contexts that use the expression
7781 parser. */
7782
7783 static void
7784 fixup_go_packaging (struct dwarf2_cu *cu)
7785 {
7786 gdb::unique_xmalloc_ptr<char> package_name;
7787 struct pending *list;
7788 int i;
7789
7790 for (list = *cu->get_builder ()->get_global_symbols ();
7791 list != NULL;
7792 list = list->next)
7793 {
7794 for (i = 0; i < list->nsyms; ++i)
7795 {
7796 struct symbol *sym = list->symbol[i];
7797
7798 if (sym->language () == language_go
7799 && sym->aclass () == LOC_BLOCK)
7800 {
7801 gdb::unique_xmalloc_ptr<char> this_package_name
7802 (go_symbol_package_name (sym));
7803
7804 if (this_package_name == NULL)
7805 continue;
7806 if (package_name == NULL)
7807 package_name = std::move (this_package_name);
7808 else
7809 {
7810 struct objfile *objfile = cu->per_objfile->objfile;
7811 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
7812 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7813 (sym->symtab () != NULL
7814 ? symtab_to_filename_for_display
7815 (sym->symtab ())
7816 : objfile_name (objfile)),
7817 this_package_name.get (), package_name.get ());
7818 }
7819 }
7820 }
7821 }
7822
7823 if (package_name != NULL)
7824 {
7825 struct objfile *objfile = cu->per_objfile->objfile;
7826 const char *saved_package_name = objfile->intern (package_name.get ());
7827 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7828 saved_package_name);
7829 struct symbol *sym;
7830
7831 sym = new (&objfile->objfile_obstack) symbol;
7832 sym->set_language (language_go, &objfile->objfile_obstack);
7833 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
7834 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7835 e.g., "main" finds the "main" module and not C's main(). */
7836 sym->set_domain (STRUCT_DOMAIN);
7837 sym->set_aclass_index (LOC_TYPEDEF);
7838 sym->set_type (type);
7839
7840 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
7841 }
7842 }
7843
7844 /* Allocate a fully-qualified name consisting of the two parts on the
7845 obstack. */
7846
7847 static const char *
7848 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
7849 {
7850 return obconcat (obstack, p1, "::", p2, (char *) NULL);
7851 }
7852
7853 /* A helper that allocates a variant part to attach to a Rust enum
7854 type. OBSTACK is where the results should be allocated. TYPE is
7855 the type we're processing. DISCRIMINANT_INDEX is the index of the
7856 discriminant. It must be the index of one of the fields of TYPE,
7857 or -1 to mean there is no discriminant (univariant enum).
7858 DEFAULT_INDEX is the index of the default field; or -1 if there is
7859 no default. RANGES is indexed by "effective" field number (the
7860 field index, but omitting the discriminant and default fields) and
7861 must hold the discriminant values used by the variants. Note that
7862 RANGES must have a lifetime at least as long as OBSTACK -- either
7863 already allocated on it, or static. */
7864
7865 static void
7866 alloc_rust_variant (struct obstack *obstack, struct type *type,
7867 int discriminant_index, int default_index,
7868 gdb::array_view<discriminant_range> ranges)
7869 {
7870 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7871 gdb_assert (discriminant_index == -1
7872 || (discriminant_index >= 0
7873 && discriminant_index < type->num_fields ()));
7874 gdb_assert (default_index == -1
7875 || (default_index >= 0 && default_index < type->num_fields ()));
7876
7877 /* We have one variant for each non-discriminant field. */
7878 int n_variants = type->num_fields ();
7879 if (discriminant_index != -1)
7880 --n_variants;
7881
7882 variant *variants = new (obstack) variant[n_variants];
7883 int var_idx = 0;
7884 int range_idx = 0;
7885 for (int i = 0; i < type->num_fields (); ++i)
7886 {
7887 if (i == discriminant_index)
7888 continue;
7889
7890 variants[var_idx].first_field = i;
7891 variants[var_idx].last_field = i + 1;
7892
7893 /* The default field does not need a range, but other fields do.
7894 We skipped the discriminant above. */
7895 if (i != default_index)
7896 {
7897 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
7898 ++range_idx;
7899 }
7900
7901 ++var_idx;
7902 }
7903
7904 gdb_assert (range_idx == ranges.size ());
7905 gdb_assert (var_idx == n_variants);
7906
7907 variant_part *part = new (obstack) variant_part;
7908 part->discriminant_index = discriminant_index;
7909 /* If there is no discriminant, then whether it is signed is of no
7910 consequence. */
7911 part->is_unsigned
7912 = (discriminant_index == -1
7913 ? false
7914 : type->field (discriminant_index).type ()->is_unsigned ());
7915 part->variants = gdb::array_view<variant> (variants, n_variants);
7916
7917 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
7918 gdb::array_view<variant_part> *prop_value
7919 = new (storage) gdb::array_view<variant_part> (part, 1);
7920
7921 struct dynamic_prop prop;
7922 prop.set_variant_parts (prop_value);
7923
7924 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
7925 }
7926
7927 /* Some versions of rustc emitted enums in an unusual way.
7928
7929 Ordinary enums were emitted as unions. The first element of each
7930 structure in the union was named "RUST$ENUM$DISR". This element
7931 held the discriminant.
7932
7933 These versions of Rust also implemented the "non-zero"
7934 optimization. When the enum had two values, and one is empty and
7935 the other holds a pointer that cannot be zero, the pointer is used
7936 as the discriminant, with a zero value meaning the empty variant.
7937 Here, the union's first member is of the form
7938 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
7939 where the fieldnos are the indices of the fields that should be
7940 traversed in order to find the field (which may be several fields deep)
7941 and the variantname is the name of the variant of the case when the
7942 field is zero.
7943
7944 This function recognizes whether TYPE is of one of these forms,
7945 and, if so, smashes it to be a variant type. */
7946
7947 static void
7948 quirk_rust_enum (struct type *type, struct objfile *objfile)
7949 {
7950 gdb_assert (type->code () == TYPE_CODE_UNION);
7951
7952 /* We don't need to deal with empty enums. */
7953 if (type->num_fields () == 0)
7954 return;
7955
7956 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
7957 if (type->num_fields () == 1
7958 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
7959 {
7960 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
7961
7962 /* Decode the field name to find the offset of the
7963 discriminant. */
7964 ULONGEST bit_offset = 0;
7965 struct type *field_type = type->field (0).type ();
7966 while (name[0] >= '0' && name[0] <= '9')
7967 {
7968 char *tail;
7969 unsigned long index = strtoul (name, &tail, 10);
7970 name = tail;
7971 if (*name != '$'
7972 || index >= field_type->num_fields ()
7973 || (field_type->field (index).loc_kind ()
7974 != FIELD_LOC_KIND_BITPOS))
7975 {
7976 complaint (_("Could not parse Rust enum encoding string \"%s\""
7977 "[in module %s]"),
7978 type->field (0).name (),
7979 objfile_name (objfile));
7980 return;
7981 }
7982 ++name;
7983
7984 bit_offset += field_type->field (index).loc_bitpos ();
7985 field_type = field_type->field (index).type ();
7986 }
7987
7988 /* Smash this type to be a structure type. We have to do this
7989 because the type has already been recorded. */
7990 type->set_code (TYPE_CODE_STRUCT);
7991 type->set_num_fields (3);
7992 /* Save the field we care about. */
7993 struct field saved_field = type->field (0);
7994 type->set_fields
7995 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
7996
7997 /* Put the discriminant at index 0. */
7998 type->field (0).set_type (field_type);
7999 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8000 type->field (0).set_name ("<<discriminant>>");
8001 type->field (0).set_loc_bitpos (bit_offset);
8002
8003 /* The order of fields doesn't really matter, so put the real
8004 field at index 1 and the data-less field at index 2. */
8005 type->field (1) = saved_field;
8006 type->field (1).set_name
8007 (rust_last_path_segment (type->field (1).type ()->name ()));
8008 type->field (1).type ()->set_name
8009 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8010 type->field (1).name ()));
8011
8012 const char *dataless_name
8013 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8014 name);
8015 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8016 dataless_name);
8017 type->field (2).set_type (dataless_type);
8018 /* NAME points into the original discriminant name, which
8019 already has the correct lifetime. */
8020 type->field (2).set_name (name);
8021 type->field (2).set_loc_bitpos (0);
8022
8023 /* Indicate that this is a variant type. */
8024 static discriminant_range ranges[1] = { { 0, 0 } };
8025 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8026 }
8027 /* A union with a single anonymous field is probably an old-style
8028 univariant enum. */
8029 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
8030 {
8031 /* Smash this type to be a structure type. We have to do this
8032 because the type has already been recorded. */
8033 type->set_code (TYPE_CODE_STRUCT);
8034
8035 struct type *field_type = type->field (0).type ();
8036 const char *variant_name
8037 = rust_last_path_segment (field_type->name ());
8038 type->field (0).set_name (variant_name);
8039 field_type->set_name
8040 (rust_fully_qualify (&objfile->objfile_obstack,
8041 type->name (), variant_name));
8042
8043 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
8044 }
8045 else
8046 {
8047 struct type *disr_type = nullptr;
8048 for (int i = 0; i < type->num_fields (); ++i)
8049 {
8050 disr_type = type->field (i).type ();
8051
8052 if (disr_type->code () != TYPE_CODE_STRUCT)
8053 {
8054 /* All fields of a true enum will be structs. */
8055 return;
8056 }
8057 else if (disr_type->num_fields () == 0)
8058 {
8059 /* Could be data-less variant, so keep going. */
8060 disr_type = nullptr;
8061 }
8062 else if (strcmp (disr_type->field (0).name (),
8063 "RUST$ENUM$DISR") != 0)
8064 {
8065 /* Not a Rust enum. */
8066 return;
8067 }
8068 else
8069 {
8070 /* Found one. */
8071 break;
8072 }
8073 }
8074
8075 /* If we got here without a discriminant, then it's probably
8076 just a union. */
8077 if (disr_type == nullptr)
8078 return;
8079
8080 /* Smash this type to be a structure type. We have to do this
8081 because the type has already been recorded. */
8082 type->set_code (TYPE_CODE_STRUCT);
8083
8084 /* Make space for the discriminant field. */
8085 struct field *disr_field = &disr_type->field (0);
8086 field *new_fields
8087 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
8088 * sizeof (struct field)));
8089 memcpy (new_fields + 1, type->fields (),
8090 type->num_fields () * sizeof (struct field));
8091 type->set_fields (new_fields);
8092 type->set_num_fields (type->num_fields () + 1);
8093
8094 /* Install the discriminant at index 0 in the union. */
8095 type->field (0) = *disr_field;
8096 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8097 type->field (0).set_name ("<<discriminant>>");
8098
8099 /* We need a way to find the correct discriminant given a
8100 variant name. For convenience we build a map here. */
8101 struct type *enum_type = disr_field->type ();
8102 std::unordered_map<std::string, ULONGEST> discriminant_map;
8103 for (int i = 0; i < enum_type->num_fields (); ++i)
8104 {
8105 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
8106 {
8107 const char *name
8108 = rust_last_path_segment (enum_type->field (i).name ());
8109 discriminant_map[name] = enum_type->field (i).loc_enumval ();
8110 }
8111 }
8112
8113 int n_fields = type->num_fields ();
8114 /* We don't need a range entry for the discriminant, but we do
8115 need one for every other field, as there is no default
8116 variant. */
8117 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
8118 discriminant_range,
8119 n_fields - 1);
8120 /* Skip the discriminant here. */
8121 for (int i = 1; i < n_fields; ++i)
8122 {
8123 /* Find the final word in the name of this variant's type.
8124 That name can be used to look up the correct
8125 discriminant. */
8126 const char *variant_name
8127 = rust_last_path_segment (type->field (i).type ()->name ());
8128
8129 auto iter = discriminant_map.find (variant_name);
8130 if (iter != discriminant_map.end ())
8131 {
8132 ranges[i - 1].low = iter->second;
8133 ranges[i - 1].high = iter->second;
8134 }
8135
8136 /* In Rust, each element should have the size of the
8137 enclosing enum. */
8138 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
8139
8140 /* Remove the discriminant field, if it exists. */
8141 struct type *sub_type = type->field (i).type ();
8142 if (sub_type->num_fields () > 0)
8143 {
8144 sub_type->set_num_fields (sub_type->num_fields () - 1);
8145 sub_type->set_fields (sub_type->fields () + 1);
8146 }
8147 type->field (i).set_name (variant_name);
8148 sub_type->set_name
8149 (rust_fully_qualify (&objfile->objfile_obstack,
8150 type->name (), variant_name));
8151 }
8152
8153 /* Indicate that this is a variant type. */
8154 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
8155 gdb::array_view<discriminant_range> (ranges,
8156 n_fields - 1));
8157 }
8158 }
8159
8160 /* Rewrite some Rust unions to be structures with variants parts. */
8161
8162 static void
8163 rust_union_quirks (struct dwarf2_cu *cu)
8164 {
8165 gdb_assert (cu->per_cu->lang == language_rust);
8166 for (type *type_ : cu->rust_unions)
8167 quirk_rust_enum (type_, cu->per_objfile->objfile);
8168 /* We don't need this any more. */
8169 cu->rust_unions.clear ();
8170 }
8171
8172 /* See read.h. */
8173
8174 type_unit_group_unshareable *
8175 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
8176 {
8177 auto iter = this->m_type_units.find (tu_group);
8178 if (iter != this->m_type_units.end ())
8179 return iter->second.get ();
8180
8181 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
8182 type_unit_group_unshareable *result = uniq.get ();
8183 this->m_type_units[tu_group] = std::move (uniq);
8184 return result;
8185 }
8186
8187 struct type *
8188 dwarf2_per_objfile::get_type_for_signatured_type
8189 (signatured_type *sig_type) const
8190 {
8191 auto iter = this->m_type_map.find (sig_type);
8192 if (iter == this->m_type_map.end ())
8193 return nullptr;
8194
8195 return iter->second;
8196 }
8197
8198 void dwarf2_per_objfile::set_type_for_signatured_type
8199 (signatured_type *sig_type, struct type *type)
8200 {
8201 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
8202
8203 this->m_type_map[sig_type] = type;
8204 }
8205
8206 /* A helper function for computing the list of all symbol tables
8207 included by PER_CU. */
8208
8209 static void
8210 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
8211 htab_t all_children, htab_t all_type_symtabs,
8212 dwarf2_per_cu_data *per_cu,
8213 dwarf2_per_objfile *per_objfile,
8214 struct compunit_symtab *immediate_parent)
8215 {
8216 void **slot = htab_find_slot (all_children, per_cu, INSERT);
8217 if (*slot != NULL)
8218 {
8219 /* This inclusion and its children have been processed. */
8220 return;
8221 }
8222
8223 *slot = per_cu;
8224
8225 /* Only add a CU if it has a symbol table. */
8226 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8227 if (cust != NULL)
8228 {
8229 /* If this is a type unit only add its symbol table if we haven't
8230 seen it yet (type unit per_cu's can share symtabs). */
8231 if (per_cu->is_debug_types)
8232 {
8233 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8234 if (*slot == NULL)
8235 {
8236 *slot = cust;
8237 result->push_back (cust);
8238 if (cust->user == NULL)
8239 cust->user = immediate_parent;
8240 }
8241 }
8242 else
8243 {
8244 result->push_back (cust);
8245 if (cust->user == NULL)
8246 cust->user = immediate_parent;
8247 }
8248 }
8249
8250 if (!per_cu->imported_symtabs_empty ())
8251 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8252 {
8253 recursively_compute_inclusions (result, all_children,
8254 all_type_symtabs, ptr, per_objfile,
8255 cust);
8256 }
8257 }
8258
8259 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8260 PER_CU. */
8261
8262 static void
8263 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
8264 dwarf2_per_objfile *per_objfile)
8265 {
8266 gdb_assert (! per_cu->is_debug_types);
8267
8268 if (!per_cu->imported_symtabs_empty ())
8269 {
8270 int len;
8271 std::vector<compunit_symtab *> result_symtabs;
8272 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8273
8274 /* If we don't have a symtab, we can just skip this case. */
8275 if (cust == NULL)
8276 return;
8277
8278 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
8279 htab_eq_pointer,
8280 NULL, xcalloc, xfree));
8281 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
8282 htab_eq_pointer,
8283 NULL, xcalloc, xfree));
8284
8285 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8286 {
8287 recursively_compute_inclusions (&result_symtabs, all_children.get (),
8288 all_type_symtabs.get (), ptr,
8289 per_objfile, cust);
8290 }
8291
8292 /* Now we have a transitive closure of all the included symtabs. */
8293 len = result_symtabs.size ();
8294 cust->includes
8295 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8296 struct compunit_symtab *, len + 1);
8297 memcpy (cust->includes, result_symtabs.data (),
8298 len * sizeof (compunit_symtab *));
8299 cust->includes[len] = NULL;
8300 }
8301 }
8302
8303 /* Compute the 'includes' field for the symtabs of all the CUs we just
8304 read. */
8305
8306 static void
8307 process_cu_includes (dwarf2_per_objfile *per_objfile)
8308 {
8309 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
8310 {
8311 if (! iter->is_debug_types)
8312 compute_compunit_symtab_includes (iter, per_objfile);
8313 }
8314
8315 per_objfile->per_bfd->just_read_cus.clear ();
8316 }
8317
8318 /* Generate full symbol information for CU, whose DIEs have
8319 already been loaded into memory. */
8320
8321 static void
8322 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
8323 {
8324 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8325 struct objfile *objfile = per_objfile->objfile;
8326 struct gdbarch *gdbarch = objfile->arch ();
8327 CORE_ADDR lowpc, highpc;
8328 struct compunit_symtab *cust;
8329 CORE_ADDR baseaddr;
8330 struct block *static_block;
8331 CORE_ADDR addr;
8332
8333 baseaddr = objfile->text_section_offset ();
8334
8335 /* Clear the list here in case something was left over. */
8336 cu->method_list.clear ();
8337
8338 dwarf2_find_base_address (cu->dies, cu);
8339
8340 /* Before we start reading the top-level DIE, ensure it has a valid tag
8341 type. */
8342 switch (cu->dies->tag)
8343 {
8344 case DW_TAG_compile_unit:
8345 case DW_TAG_partial_unit:
8346 case DW_TAG_type_unit:
8347 break;
8348 default:
8349 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8350 dwarf_tag_name (cu->dies->tag),
8351 sect_offset_str (cu->per_cu->sect_off),
8352 objfile_name (per_objfile->objfile));
8353 }
8354
8355 /* Do line number decoding in read_file_scope () */
8356 process_die (cu->dies, cu);
8357
8358 /* For now fudge the Go package. */
8359 if (cu->per_cu->lang == language_go)
8360 fixup_go_packaging (cu);
8361
8362 /* Now that we have processed all the DIEs in the CU, all the types
8363 should be complete, and it should now be safe to compute all of the
8364 physnames. */
8365 compute_delayed_physnames (cu);
8366
8367 if (cu->per_cu->lang == language_rust)
8368 rust_union_quirks (cu);
8369
8370 /* Some compilers don't define a DW_AT_high_pc attribute for the
8371 compilation unit. If the DW_AT_high_pc is missing, synthesize
8372 it, by scanning the DIE's below the compilation unit. */
8373 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8374
8375 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8376 static_block
8377 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
8378
8379 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8380 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8381 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8382 addrmap to help ensure it has an accurate map of pc values belonging to
8383 this comp unit. */
8384 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8385
8386 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
8387 (static_block, SECT_OFF_TEXT (objfile), 0);
8388
8389 if (cust != NULL)
8390 {
8391 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8392
8393 /* Set symtab language to language from DW_AT_language. If the
8394 compilation is from a C file generated by language preprocessors, do
8395 not set the language if it was already deduced by start_subfile. */
8396 if (!(cu->per_cu->lang == language_c
8397 && cust->primary_filetab ()->language () != language_unknown))
8398 cust->primary_filetab ()->set_language (cu->per_cu->lang);
8399
8400 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8401 produce DW_AT_location with location lists but it can be possibly
8402 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8403 there were bugs in prologue debug info, fixed later in GCC-4.5
8404 by "unwind info for epilogues" patch (which is not directly related).
8405
8406 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8407 needed, it would be wrong due to missing DW_AT_producer there.
8408
8409 Still one can confuse GDB by using non-standard GCC compilation
8410 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8411 */
8412 if (cu->has_loclist && gcc_4_minor >= 5)
8413 cust->set_locations_valid (true);
8414
8415 if (gcc_4_minor >= 5)
8416 cust->set_epilogue_unwind_valid (true);
8417
8418 cust->set_call_site_htab (cu->call_site_htab);
8419 }
8420
8421 per_objfile->set_symtab (cu->per_cu, cust);
8422
8423 /* Push it for inclusion processing later. */
8424 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
8425
8426 /* Not needed any more. */
8427 cu->reset_builder ();
8428 }
8429
8430 /* Generate full symbol information for type unit CU, whose DIEs have
8431 already been loaded into memory. */
8432
8433 static void
8434 process_full_type_unit (dwarf2_cu *cu,
8435 enum language pretend_language)
8436 {
8437 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8438 struct objfile *objfile = per_objfile->objfile;
8439 struct compunit_symtab *cust;
8440 struct signatured_type *sig_type;
8441
8442 gdb_assert (cu->per_cu->is_debug_types);
8443 sig_type = (struct signatured_type *) cu->per_cu;
8444
8445 /* Clear the list here in case something was left over. */
8446 cu->method_list.clear ();
8447
8448 /* The symbol tables are set up in read_type_unit_scope. */
8449 process_die (cu->dies, cu);
8450
8451 /* For now fudge the Go package. */
8452 if (cu->per_cu->lang == language_go)
8453 fixup_go_packaging (cu);
8454
8455 /* Now that we have processed all the DIEs in the CU, all the types
8456 should be complete, and it should now be safe to compute all of the
8457 physnames. */
8458 compute_delayed_physnames (cu);
8459
8460 if (cu->per_cu->lang == language_rust)
8461 rust_union_quirks (cu);
8462
8463 /* TUs share symbol tables.
8464 If this is the first TU to use this symtab, complete the construction
8465 of it with end_expandable_symtab. Otherwise, complete the addition of
8466 this TU's symbols to the existing symtab. */
8467 type_unit_group_unshareable *tug_unshare =
8468 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8469 if (tug_unshare->compunit_symtab == NULL)
8470 {
8471 buildsym_compunit *builder = cu->get_builder ();
8472 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8473 tug_unshare->compunit_symtab = cust;
8474
8475 if (cust != NULL)
8476 {
8477 /* Set symtab language to language from DW_AT_language. If the
8478 compilation is from a C file generated by language preprocessors,
8479 do not set the language if it was already deduced by
8480 start_subfile. */
8481 if (!(cu->per_cu->lang == language_c
8482 && cust->primary_filetab ()->language () != language_c))
8483 cust->primary_filetab ()->set_language (cu->per_cu->lang);
8484 }
8485 }
8486 else
8487 {
8488 cu->get_builder ()->augment_type_symtab ();
8489 cust = tug_unshare->compunit_symtab;
8490 }
8491
8492 per_objfile->set_symtab (cu->per_cu, cust);
8493
8494 /* Not needed any more. */
8495 cu->reset_builder ();
8496 }
8497
8498 /* Process an imported unit DIE. */
8499
8500 static void
8501 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8502 {
8503 struct attribute *attr;
8504
8505 /* For now we don't handle imported units in type units. */
8506 if (cu->per_cu->is_debug_types)
8507 {
8508 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8509 " supported in type units [in module %s]"),
8510 objfile_name (cu->per_objfile->objfile));
8511 }
8512
8513 attr = dwarf2_attr (die, DW_AT_import, cu);
8514 if (attr != NULL)
8515 {
8516 sect_offset sect_off = attr->get_ref_die_offset ();
8517 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8518 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8519 dwarf2_per_cu_data *per_cu
8520 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
8521 per_objfile->per_bfd);
8522
8523 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8524 into another compilation unit, at root level. Regard this as a hint,
8525 and ignore it. */
8526 if (die->parent && die->parent->parent == NULL
8527 && per_cu->unit_type == DW_UT_compile
8528 && per_cu->lang == language_cplus)
8529 return;
8530
8531 /* If necessary, add it to the queue and load its DIEs. */
8532 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
8533 cu->per_cu->lang))
8534 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
8535 false, cu->per_cu->lang);
8536
8537 cu->per_cu->imported_symtabs_push (per_cu);
8538 }
8539 }
8540
8541 /* RAII object that represents a process_die scope: i.e.,
8542 starts/finishes processing a DIE. */
8543 class process_die_scope
8544 {
8545 public:
8546 process_die_scope (die_info *die, dwarf2_cu *cu)
8547 : m_die (die), m_cu (cu)
8548 {
8549 /* We should only be processing DIEs not already in process. */
8550 gdb_assert (!m_die->in_process);
8551 m_die->in_process = true;
8552 }
8553
8554 ~process_die_scope ()
8555 {
8556 m_die->in_process = false;
8557
8558 /* If we're done processing the DIE for the CU that owns the line
8559 header, we don't need the line header anymore. */
8560 if (m_cu->line_header_die_owner == m_die)
8561 {
8562 delete m_cu->line_header;
8563 m_cu->line_header = NULL;
8564 m_cu->line_header_die_owner = NULL;
8565 }
8566 }
8567
8568 private:
8569 die_info *m_die;
8570 dwarf2_cu *m_cu;
8571 };
8572
8573 /* Process a die and its children. */
8574
8575 static void
8576 process_die (struct die_info *die, struct dwarf2_cu *cu)
8577 {
8578 process_die_scope scope (die, cu);
8579
8580 switch (die->tag)
8581 {
8582 case DW_TAG_padding:
8583 break;
8584 case DW_TAG_compile_unit:
8585 case DW_TAG_partial_unit:
8586 read_file_scope (die, cu);
8587 break;
8588 case DW_TAG_type_unit:
8589 read_type_unit_scope (die, cu);
8590 break;
8591 case DW_TAG_subprogram:
8592 /* Nested subprograms in Fortran get a prefix. */
8593 if (cu->per_cu->lang == language_fortran
8594 && die->parent != NULL
8595 && die->parent->tag == DW_TAG_subprogram)
8596 cu->processing_has_namespace_info = true;
8597 /* Fall through. */
8598 case DW_TAG_inlined_subroutine:
8599 read_func_scope (die, cu);
8600 break;
8601 case DW_TAG_lexical_block:
8602 case DW_TAG_try_block:
8603 case DW_TAG_catch_block:
8604 read_lexical_block_scope (die, cu);
8605 break;
8606 case DW_TAG_call_site:
8607 case DW_TAG_GNU_call_site:
8608 read_call_site_scope (die, cu);
8609 break;
8610 case DW_TAG_class_type:
8611 case DW_TAG_interface_type:
8612 case DW_TAG_structure_type:
8613 case DW_TAG_union_type:
8614 case DW_TAG_namelist:
8615 process_structure_scope (die, cu);
8616 break;
8617 case DW_TAG_enumeration_type:
8618 process_enumeration_scope (die, cu);
8619 break;
8620
8621 /* These dies have a type, but processing them does not create
8622 a symbol or recurse to process the children. Therefore we can
8623 read them on-demand through read_type_die. */
8624 case DW_TAG_subroutine_type:
8625 case DW_TAG_set_type:
8626 case DW_TAG_pointer_type:
8627 case DW_TAG_ptr_to_member_type:
8628 case DW_TAG_reference_type:
8629 case DW_TAG_rvalue_reference_type:
8630 case DW_TAG_string_type:
8631 break;
8632
8633 case DW_TAG_array_type:
8634 /* We only need to handle this case for Ada -- in other
8635 languages, it's normal for the compiler to emit a typedef
8636 instead. */
8637 if (cu->per_cu->lang != language_ada)
8638 break;
8639 /* FALLTHROUGH */
8640 case DW_TAG_base_type:
8641 case DW_TAG_subrange_type:
8642 case DW_TAG_generic_subrange:
8643 case DW_TAG_typedef:
8644 /* Add a typedef symbol for the type definition, if it has a
8645 DW_AT_name. */
8646 new_symbol (die, read_type_die (die, cu), cu);
8647 break;
8648 case DW_TAG_common_block:
8649 read_common_block (die, cu);
8650 break;
8651 case DW_TAG_common_inclusion:
8652 break;
8653 case DW_TAG_namespace:
8654 cu->processing_has_namespace_info = true;
8655 read_namespace (die, cu);
8656 break;
8657 case DW_TAG_module:
8658 cu->processing_has_namespace_info = true;
8659 read_module (die, cu);
8660 break;
8661 case DW_TAG_imported_declaration:
8662 cu->processing_has_namespace_info = true;
8663 if (read_namespace_alias (die, cu))
8664 break;
8665 /* The declaration is not a global namespace alias. */
8666 /* Fall through. */
8667 case DW_TAG_imported_module:
8668 cu->processing_has_namespace_info = true;
8669 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8670 || cu->per_cu->lang != language_fortran))
8671 complaint (_("Tag '%s' has unexpected children"),
8672 dwarf_tag_name (die->tag));
8673 read_import_statement (die, cu);
8674 break;
8675
8676 case DW_TAG_imported_unit:
8677 process_imported_unit_die (die, cu);
8678 break;
8679
8680 case DW_TAG_variable:
8681 read_variable (die, cu);
8682 break;
8683
8684 default:
8685 new_symbol (die, NULL, cu);
8686 break;
8687 }
8688 }
8689 \f
8690 /* DWARF name computation. */
8691
8692 /* A helper function for dwarf2_compute_name which determines whether DIE
8693 needs to have the name of the scope prepended to the name listed in the
8694 die. */
8695
8696 static int
8697 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8698 {
8699 struct attribute *attr;
8700
8701 switch (die->tag)
8702 {
8703 case DW_TAG_namespace:
8704 case DW_TAG_typedef:
8705 case DW_TAG_class_type:
8706 case DW_TAG_interface_type:
8707 case DW_TAG_structure_type:
8708 case DW_TAG_union_type:
8709 case DW_TAG_enumeration_type:
8710 case DW_TAG_enumerator:
8711 case DW_TAG_subprogram:
8712 case DW_TAG_inlined_subroutine:
8713 case DW_TAG_member:
8714 case DW_TAG_imported_declaration:
8715 return 1;
8716
8717 case DW_TAG_variable:
8718 case DW_TAG_constant:
8719 /* We only need to prefix "globally" visible variables. These include
8720 any variable marked with DW_AT_external or any variable that
8721 lives in a namespace. [Variables in anonymous namespaces
8722 require prefixing, but they are not DW_AT_external.] */
8723
8724 if (dwarf2_attr (die, DW_AT_specification, cu))
8725 {
8726 struct dwarf2_cu *spec_cu = cu;
8727
8728 return die_needs_namespace (die_specification (die, &spec_cu),
8729 spec_cu);
8730 }
8731
8732 attr = dwarf2_attr (die, DW_AT_external, cu);
8733 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8734 && die->parent->tag != DW_TAG_module)
8735 return 0;
8736 /* A variable in a lexical block of some kind does not need a
8737 namespace, even though in C++ such variables may be external
8738 and have a mangled name. */
8739 if (die->parent->tag == DW_TAG_lexical_block
8740 || die->parent->tag == DW_TAG_try_block
8741 || die->parent->tag == DW_TAG_catch_block
8742 || die->parent->tag == DW_TAG_subprogram)
8743 return 0;
8744 return 1;
8745
8746 default:
8747 return 0;
8748 }
8749 }
8750
8751 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8752 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8753 defined for the given DIE. */
8754
8755 static struct attribute *
8756 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8757 {
8758 struct attribute *attr;
8759
8760 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8761 if (attr == NULL)
8762 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8763
8764 return attr;
8765 }
8766
8767 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8768 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8769 defined for the given DIE. */
8770
8771 static const char *
8772 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8773 {
8774 const char *linkage_name;
8775
8776 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8777 if (linkage_name == NULL)
8778 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8779
8780 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8781 See https://github.com/rust-lang/rust/issues/32925. */
8782 if (cu->per_cu->lang == language_rust && linkage_name != NULL
8783 && strchr (linkage_name, '{') != NULL)
8784 linkage_name = NULL;
8785
8786 return linkage_name;
8787 }
8788
8789 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8790 compute the physname for the object, which include a method's:
8791 - formal parameters (C++),
8792 - receiver type (Go),
8793
8794 The term "physname" is a bit confusing.
8795 For C++, for example, it is the demangled name.
8796 For Go, for example, it's the mangled name.
8797
8798 For Ada, return the DIE's linkage name rather than the fully qualified
8799 name. PHYSNAME is ignored..
8800
8801 The result is allocated on the objfile->per_bfd's obstack and
8802 canonicalized. */
8803
8804 static const char *
8805 dwarf2_compute_name (const char *name,
8806 struct die_info *die, struct dwarf2_cu *cu,
8807 int physname)
8808 {
8809 struct objfile *objfile = cu->per_objfile->objfile;
8810
8811 if (name == NULL)
8812 name = dwarf2_name (die, cu);
8813
8814 enum language lang = cu->per_cu->lang;
8815
8816 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8817 but otherwise compute it by typename_concat inside GDB.
8818 FIXME: Actually this is not really true, or at least not always true.
8819 It's all very confusing. compute_and_set_names doesn't try to demangle
8820 Fortran names because there is no mangling standard. So new_symbol
8821 will set the demangled name to the result of dwarf2_full_name, and it is
8822 the demangled name that GDB uses if it exists. */
8823 if (lang == language_ada
8824 || (lang == language_fortran && physname))
8825 {
8826 /* For Ada unit, we prefer the linkage name over the name, as
8827 the former contains the exported name, which the user expects
8828 to be able to reference. Ideally, we want the user to be able
8829 to reference this entity using either natural or linkage name,
8830 but we haven't started looking at this enhancement yet. */
8831 const char *linkage_name = dw2_linkage_name (die, cu);
8832
8833 if (linkage_name != NULL)
8834 return linkage_name;
8835 }
8836
8837 /* These are the only languages we know how to qualify names in. */
8838 if (name != NULL
8839 && (lang == language_cplus
8840 || lang == language_fortran || lang == language_d
8841 || lang == language_rust))
8842 {
8843 if (die_needs_namespace (die, cu))
8844 {
8845 const char *prefix;
8846 const char *canonical_name = NULL;
8847
8848 string_file buf;
8849
8850 prefix = determine_prefix (die, cu);
8851 if (*prefix != '\0')
8852 {
8853 gdb::unique_xmalloc_ptr<char> prefixed_name
8854 (typename_concat (NULL, prefix, name, physname, cu));
8855
8856 buf.puts (prefixed_name.get ());
8857 }
8858 else
8859 buf.puts (name);
8860
8861 /* Template parameters may be specified in the DIE's DW_AT_name, or
8862 as children with DW_TAG_template_type_param or
8863 DW_TAG_value_type_param. If the latter, add them to the name
8864 here. If the name already has template parameters, then
8865 skip this step; some versions of GCC emit both, and
8866 it is more efficient to use the pre-computed name.
8867
8868 Something to keep in mind about this process: it is very
8869 unlikely, or in some cases downright impossible, to produce
8870 something that will match the mangled name of a function.
8871 If the definition of the function has the same debug info,
8872 we should be able to match up with it anyway. But fallbacks
8873 using the minimal symbol, for instance to find a method
8874 implemented in a stripped copy of libstdc++, will not work.
8875 If we do not have debug info for the definition, we will have to
8876 match them up some other way.
8877
8878 When we do name matching there is a related problem with function
8879 templates; two instantiated function templates are allowed to
8880 differ only by their return types, which we do not add here. */
8881
8882 if (lang == language_cplus && strchr (name, '<') == NULL)
8883 {
8884 struct attribute *attr;
8885 struct die_info *child;
8886 int first = 1;
8887
8888 die->building_fullname = 1;
8889
8890 for (child = die->child; child != NULL; child = child->sibling)
8891 {
8892 struct type *type;
8893 LONGEST value;
8894 const gdb_byte *bytes;
8895 struct dwarf2_locexpr_baton *baton;
8896 struct value *v;
8897
8898 if (child->tag != DW_TAG_template_type_param
8899 && child->tag != DW_TAG_template_value_param)
8900 continue;
8901
8902 if (first)
8903 {
8904 buf.puts ("<");
8905 first = 0;
8906 }
8907 else
8908 buf.puts (", ");
8909
8910 attr = dwarf2_attr (child, DW_AT_type, cu);
8911 if (attr == NULL)
8912 {
8913 complaint (_("template parameter missing DW_AT_type"));
8914 buf.puts ("UNKNOWN_TYPE");
8915 continue;
8916 }
8917 type = die_type (child, cu);
8918
8919 if (child->tag == DW_TAG_template_type_param)
8920 {
8921 cu->language_defn->print_type (type, "", &buf, -1, 0,
8922 &type_print_raw_options);
8923 continue;
8924 }
8925
8926 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8927 if (attr == NULL)
8928 {
8929 complaint (_("template parameter missing "
8930 "DW_AT_const_value"));
8931 buf.puts ("UNKNOWN_VALUE");
8932 continue;
8933 }
8934
8935 dwarf2_const_value_attr (attr, type, name,
8936 &cu->comp_unit_obstack, cu,
8937 &value, &bytes, &baton);
8938
8939 if (type->has_no_signedness ())
8940 /* GDB prints characters as NUMBER 'CHAR'. If that's
8941 changed, this can use value_print instead. */
8942 cu->language_defn->printchar (value, type, &buf);
8943 else
8944 {
8945 struct value_print_options opts;
8946
8947 if (baton != NULL)
8948 v = dwarf2_evaluate_loc_desc (type, NULL,
8949 baton->data,
8950 baton->size,
8951 baton->per_cu,
8952 baton->per_objfile);
8953 else if (bytes != NULL)
8954 {
8955 v = allocate_value (type);
8956 memcpy (value_contents_writeable (v).data (), bytes,
8957 TYPE_LENGTH (type));
8958 }
8959 else
8960 v = value_from_longest (type, value);
8961
8962 /* Specify decimal so that we do not depend on
8963 the radix. */
8964 get_formatted_print_options (&opts, 'd');
8965 opts.raw = 1;
8966 value_print (v, &buf, &opts);
8967 release_value (v);
8968 }
8969 }
8970
8971 die->building_fullname = 0;
8972
8973 if (!first)
8974 {
8975 /* Close the argument list, with a space if necessary
8976 (nested templates). */
8977 if (!buf.empty () && buf.string ().back () == '>')
8978 buf.puts (" >");
8979 else
8980 buf.puts (">");
8981 }
8982 }
8983
8984 /* For C++ methods, append formal parameter type
8985 information, if PHYSNAME. */
8986
8987 if (physname && die->tag == DW_TAG_subprogram
8988 && lang == language_cplus)
8989 {
8990 struct type *type = read_type_die (die, cu);
8991
8992 c_type_print_args (type, &buf, 1, lang,
8993 &type_print_raw_options);
8994
8995 if (lang == language_cplus)
8996 {
8997 /* Assume that an artificial first parameter is
8998 "this", but do not crash if it is not. RealView
8999 marks unnamed (and thus unused) parameters as
9000 artificial; there is no way to differentiate
9001 the two cases. */
9002 if (type->num_fields () > 0
9003 && TYPE_FIELD_ARTIFICIAL (type, 0)
9004 && type->field (0).type ()->code () == TYPE_CODE_PTR
9005 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
9006 buf.puts (" const");
9007 }
9008 }
9009
9010 const std::string &intermediate_name = buf.string ();
9011
9012 if (lang == language_cplus)
9013 canonical_name
9014 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9015 objfile);
9016
9017 /* If we only computed INTERMEDIATE_NAME, or if
9018 INTERMEDIATE_NAME is already canonical, then we need to
9019 intern it. */
9020 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9021 name = objfile->intern (intermediate_name);
9022 else
9023 name = canonical_name;
9024 }
9025 }
9026
9027 return name;
9028 }
9029
9030 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9031 If scope qualifiers are appropriate they will be added. The result
9032 will be allocated on the storage_obstack, or NULL if the DIE does
9033 not have a name. NAME may either be from a previous call to
9034 dwarf2_name or NULL.
9035
9036 The output string will be canonicalized (if C++). */
9037
9038 static const char *
9039 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9040 {
9041 return dwarf2_compute_name (name, die, cu, 0);
9042 }
9043
9044 /* Construct a physname for the given DIE in CU. NAME may either be
9045 from a previous call to dwarf2_name or NULL. The result will be
9046 allocated on the objfile_objstack or NULL if the DIE does not have a
9047 name.
9048
9049 The output string will be canonicalized (if C++). */
9050
9051 static const char *
9052 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9053 {
9054 struct objfile *objfile = cu->per_objfile->objfile;
9055 const char *retval, *mangled = NULL, *canon = NULL;
9056 int need_copy = 1;
9057
9058 /* In this case dwarf2_compute_name is just a shortcut not building anything
9059 on its own. */
9060 if (!die_needs_namespace (die, cu))
9061 return dwarf2_compute_name (name, die, cu, 1);
9062
9063 if (cu->per_cu->lang != language_rust)
9064 mangled = dw2_linkage_name (die, cu);
9065
9066 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9067 has computed. */
9068 gdb::unique_xmalloc_ptr<char> demangled;
9069 if (mangled != NULL)
9070 {
9071 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
9072 {
9073 /* Do nothing (do not demangle the symbol name). */
9074 }
9075 else
9076 {
9077 /* Use DMGL_RET_DROP for C++ template functions to suppress
9078 their return type. It is easier for GDB users to search
9079 for such functions as `name(params)' than `long name(params)'.
9080 In such case the minimal symbol names do not match the full
9081 symbol names but for template functions there is never a need
9082 to look up their definition from their declaration so
9083 the only disadvantage remains the minimal symbol variant
9084 `long name(params)' does not have the proper inferior type. */
9085 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
9086 | DMGL_RET_DROP));
9087 }
9088 if (demangled)
9089 canon = demangled.get ();
9090 else
9091 {
9092 canon = mangled;
9093 need_copy = 0;
9094 }
9095 }
9096
9097 if (canon == NULL || check_physname)
9098 {
9099 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9100
9101 if (canon != NULL && strcmp (physname, canon) != 0)
9102 {
9103 /* It may not mean a bug in GDB. The compiler could also
9104 compute DW_AT_linkage_name incorrectly. But in such case
9105 GDB would need to be bug-to-bug compatible. */
9106
9107 complaint (_("Computed physname <%s> does not match demangled <%s> "
9108 "(from linkage <%s>) - DIE at %s [in module %s]"),
9109 physname, canon, mangled, sect_offset_str (die->sect_off),
9110 objfile_name (objfile));
9111
9112 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9113 is available here - over computed PHYSNAME. It is safer
9114 against both buggy GDB and buggy compilers. */
9115
9116 retval = canon;
9117 }
9118 else
9119 {
9120 retval = physname;
9121 need_copy = 0;
9122 }
9123 }
9124 else
9125 retval = canon;
9126
9127 if (need_copy)
9128 retval = objfile->intern (retval);
9129
9130 return retval;
9131 }
9132
9133 /* Inspect DIE in CU for a namespace alias. If one exists, record
9134 a new symbol for it.
9135
9136 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9137
9138 static int
9139 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9140 {
9141 struct attribute *attr;
9142
9143 /* If the die does not have a name, this is not a namespace
9144 alias. */
9145 attr = dwarf2_attr (die, DW_AT_name, cu);
9146 if (attr != NULL)
9147 {
9148 int num;
9149 struct die_info *d = die;
9150 struct dwarf2_cu *imported_cu = cu;
9151
9152 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9153 keep inspecting DIEs until we hit the underlying import. */
9154 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9155 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9156 {
9157 attr = dwarf2_attr (d, DW_AT_import, cu);
9158 if (attr == NULL)
9159 break;
9160
9161 d = follow_die_ref (d, attr, &imported_cu);
9162 if (d->tag != DW_TAG_imported_declaration)
9163 break;
9164 }
9165
9166 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9167 {
9168 complaint (_("DIE at %s has too many recursively imported "
9169 "declarations"), sect_offset_str (d->sect_off));
9170 return 0;
9171 }
9172
9173 if (attr != NULL)
9174 {
9175 struct type *type;
9176 sect_offset sect_off = attr->get_ref_die_offset ();
9177
9178 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
9179 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
9180 {
9181 /* This declaration is a global namespace alias. Add
9182 a symbol for it whose type is the aliased namespace. */
9183 new_symbol (die, type, cu);
9184 return 1;
9185 }
9186 }
9187 }
9188
9189 return 0;
9190 }
9191
9192 /* Return the using directives repository (global or local?) to use in the
9193 current context for CU.
9194
9195 For Ada, imported declarations can materialize renamings, which *may* be
9196 global. However it is impossible (for now?) in DWARF to distinguish
9197 "external" imported declarations and "static" ones. As all imported
9198 declarations seem to be static in all other languages, make them all CU-wide
9199 global only in Ada. */
9200
9201 static struct using_direct **
9202 using_directives (struct dwarf2_cu *cu)
9203 {
9204 if (cu->per_cu->lang == language_ada
9205 && cu->get_builder ()->outermost_context_p ())
9206 return cu->get_builder ()->get_global_using_directives ();
9207 else
9208 return cu->get_builder ()->get_local_using_directives ();
9209 }
9210
9211 /* Read the import statement specified by the given die and record it. */
9212
9213 static void
9214 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9215 {
9216 struct objfile *objfile = cu->per_objfile->objfile;
9217 struct attribute *import_attr;
9218 struct die_info *imported_die, *child_die;
9219 struct dwarf2_cu *imported_cu;
9220 const char *imported_name;
9221 const char *imported_name_prefix;
9222 const char *canonical_name;
9223 const char *import_alias;
9224 const char *imported_declaration = NULL;
9225 const char *import_prefix;
9226 std::vector<const char *> excludes;
9227
9228 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9229 if (import_attr == NULL)
9230 {
9231 complaint (_("Tag '%s' has no DW_AT_import"),
9232 dwarf_tag_name (die->tag));
9233 return;
9234 }
9235
9236 imported_cu = cu;
9237 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9238 imported_name = dwarf2_name (imported_die, imported_cu);
9239 if (imported_name == NULL)
9240 {
9241 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9242
9243 The import in the following code:
9244 namespace A
9245 {
9246 typedef int B;
9247 }
9248
9249 int main ()
9250 {
9251 using A::B;
9252 B b;
9253 return b;
9254 }
9255
9256 ...
9257 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9258 <52> DW_AT_decl_file : 1
9259 <53> DW_AT_decl_line : 6
9260 <54> DW_AT_import : <0x75>
9261 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9262 <59> DW_AT_name : B
9263 <5b> DW_AT_decl_file : 1
9264 <5c> DW_AT_decl_line : 2
9265 <5d> DW_AT_type : <0x6e>
9266 ...
9267 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9268 <76> DW_AT_byte_size : 4
9269 <77> DW_AT_encoding : 5 (signed)
9270
9271 imports the wrong die ( 0x75 instead of 0x58 ).
9272 This case will be ignored until the gcc bug is fixed. */
9273 return;
9274 }
9275
9276 /* Figure out the local name after import. */
9277 import_alias = dwarf2_name (die, cu);
9278
9279 /* Figure out where the statement is being imported to. */
9280 import_prefix = determine_prefix (die, cu);
9281
9282 /* Figure out what the scope of the imported die is and prepend it
9283 to the name of the imported die. */
9284 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9285
9286 if (imported_die->tag != DW_TAG_namespace
9287 && imported_die->tag != DW_TAG_module)
9288 {
9289 imported_declaration = imported_name;
9290 canonical_name = imported_name_prefix;
9291 }
9292 else if (strlen (imported_name_prefix) > 0)
9293 canonical_name = obconcat (&objfile->objfile_obstack,
9294 imported_name_prefix,
9295 (cu->per_cu->lang == language_d
9296 ? "."
9297 : "::"),
9298 imported_name, (char *) NULL);
9299 else
9300 canonical_name = imported_name;
9301
9302 if (die->tag == DW_TAG_imported_module
9303 && cu->per_cu->lang == language_fortran)
9304 for (child_die = die->child; child_die && child_die->tag;
9305 child_die = child_die->sibling)
9306 {
9307 /* DWARF-4: A Fortran use statement with a “rename list” may be
9308 represented by an imported module entry with an import attribute
9309 referring to the module and owned entries corresponding to those
9310 entities that are renamed as part of being imported. */
9311
9312 if (child_die->tag != DW_TAG_imported_declaration)
9313 {
9314 complaint (_("child DW_TAG_imported_declaration expected "
9315 "- DIE at %s [in module %s]"),
9316 sect_offset_str (child_die->sect_off),
9317 objfile_name (objfile));
9318 continue;
9319 }
9320
9321 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9322 if (import_attr == NULL)
9323 {
9324 complaint (_("Tag '%s' has no DW_AT_import"),
9325 dwarf_tag_name (child_die->tag));
9326 continue;
9327 }
9328
9329 imported_cu = cu;
9330 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9331 &imported_cu);
9332 imported_name = dwarf2_name (imported_die, imported_cu);
9333 if (imported_name == NULL)
9334 {
9335 complaint (_("child DW_TAG_imported_declaration has unknown "
9336 "imported name - DIE at %s [in module %s]"),
9337 sect_offset_str (child_die->sect_off),
9338 objfile_name (objfile));
9339 continue;
9340 }
9341
9342 excludes.push_back (imported_name);
9343
9344 process_die (child_die, cu);
9345 }
9346
9347 add_using_directive (using_directives (cu),
9348 import_prefix,
9349 canonical_name,
9350 import_alias,
9351 imported_declaration,
9352 excludes,
9353 0,
9354 &objfile->objfile_obstack);
9355 }
9356
9357 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9358 types, but gives them a size of zero. Starting with version 14,
9359 ICC is compatible with GCC. */
9360
9361 static bool
9362 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9363 {
9364 if (!cu->checked_producer)
9365 check_producer (cu);
9366
9367 return cu->producer_is_icc_lt_14;
9368 }
9369
9370 /* ICC generates a DW_AT_type for C void functions. This was observed on
9371 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9372 which says that void functions should not have a DW_AT_type. */
9373
9374 static bool
9375 producer_is_icc (struct dwarf2_cu *cu)
9376 {
9377 if (!cu->checked_producer)
9378 check_producer (cu);
9379
9380 return cu->producer_is_icc;
9381 }
9382
9383 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9384 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9385 this, it was first present in GCC release 4.3.0. */
9386
9387 static bool
9388 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9389 {
9390 if (!cu->checked_producer)
9391 check_producer (cu);
9392
9393 return cu->producer_is_gcc_lt_4_3;
9394 }
9395
9396 static file_and_directory &
9397 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9398 {
9399 if (cu->per_cu->fnd != nullptr)
9400 return *cu->per_cu->fnd;
9401
9402 /* Find the filename. Do not use dwarf2_name here, since the filename
9403 is not a source language identifier. */
9404 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
9405 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
9406
9407 if (res.get_comp_dir () == nullptr
9408 && producer_is_gcc_lt_4_3 (cu)
9409 && res.get_name () != nullptr
9410 && IS_ABSOLUTE_PATH (res.get_name ()))
9411 res.set_comp_dir (ldirname (res.get_name ()));
9412
9413 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
9414 return *cu->per_cu->fnd;
9415 }
9416
9417 /* Handle DW_AT_stmt_list for a compilation unit.
9418 DIE is the DW_TAG_compile_unit die for CU.
9419 COMP_DIR is the compilation directory. LOWPC is passed to
9420 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9421
9422 static void
9423 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9424 CORE_ADDR lowpc) /* ARI: editCase function */
9425 {
9426 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9427 struct attribute *attr;
9428 struct line_header line_header_local;
9429 hashval_t line_header_local_hash;
9430 void **slot;
9431 int decode_mapping;
9432
9433 gdb_assert (! cu->per_cu->is_debug_types);
9434
9435 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9436 if (attr == NULL || !attr->form_is_unsigned ())
9437 return;
9438
9439 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9440
9441 /* The line header hash table is only created if needed (it exists to
9442 prevent redundant reading of the line table for partial_units).
9443 If we're given a partial_unit, we'll need it. If we're given a
9444 compile_unit, then use the line header hash table if it's already
9445 created, but don't create one just yet. */
9446
9447 if (per_objfile->line_header_hash == NULL
9448 && die->tag == DW_TAG_partial_unit)
9449 {
9450 per_objfile->line_header_hash
9451 .reset (htab_create_alloc (127, line_header_hash_voidp,
9452 line_header_eq_voidp,
9453 htab_delete_entry<line_header>,
9454 xcalloc, xfree));
9455 }
9456
9457 line_header_local.sect_off = line_offset;
9458 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9459 line_header_local_hash = line_header_hash (&line_header_local);
9460 if (per_objfile->line_header_hash != NULL)
9461 {
9462 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9463 &line_header_local,
9464 line_header_local_hash, NO_INSERT);
9465
9466 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9467 is not present in *SLOT (since if there is something in *SLOT then
9468 it will be for a partial_unit). */
9469 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9470 {
9471 gdb_assert (*slot != NULL);
9472 cu->line_header = (struct line_header *) *slot;
9473 return;
9474 }
9475 }
9476
9477 /* dwarf_decode_line_header does not yet provide sufficient information.
9478 We always have to call also dwarf_decode_lines for it. */
9479 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9480 if (lh == NULL)
9481 return;
9482
9483 cu->line_header = lh.release ();
9484 cu->line_header_die_owner = die;
9485
9486 if (per_objfile->line_header_hash == NULL)
9487 slot = NULL;
9488 else
9489 {
9490 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9491 &line_header_local,
9492 line_header_local_hash, INSERT);
9493 gdb_assert (slot != NULL);
9494 }
9495 if (slot != NULL && *slot == NULL)
9496 {
9497 /* This newly decoded line number information unit will be owned
9498 by line_header_hash hash table. */
9499 *slot = cu->line_header;
9500 cu->line_header_die_owner = NULL;
9501 }
9502 else
9503 {
9504 /* We cannot free any current entry in (*slot) as that struct line_header
9505 may be already used by multiple CUs. Create only temporary decoded
9506 line_header for this CU - it may happen at most once for each line
9507 number information unit. And if we're not using line_header_hash
9508 then this is what we want as well. */
9509 gdb_assert (die->tag != DW_TAG_partial_unit);
9510 }
9511 decode_mapping = (die->tag != DW_TAG_partial_unit);
9512 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
9513 }
9514
9515 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9516
9517 static void
9518 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9519 {
9520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9521 struct objfile *objfile = per_objfile->objfile;
9522 struct gdbarch *gdbarch = objfile->arch ();
9523 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9524 CORE_ADDR highpc = ((CORE_ADDR) 0);
9525 struct attribute *attr;
9526 struct die_info *child_die;
9527 CORE_ADDR baseaddr;
9528
9529 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
9530 baseaddr = objfile->text_section_offset ();
9531
9532 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9533
9534 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9535 from finish_block. */
9536 if (lowpc == ((CORE_ADDR) -1))
9537 lowpc = highpc;
9538 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9539
9540 file_and_directory &fnd = find_file_and_directory (die, cu);
9541
9542 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
9543 lowpc);
9544
9545 gdb_assert (per_objfile->sym_cu == nullptr);
9546 scoped_restore restore_sym_cu
9547 = make_scoped_restore (&per_objfile->sym_cu, cu);
9548
9549 /* Decode line number information if present. We do this before
9550 processing child DIEs, so that the line header table is available
9551 for DW_AT_decl_file. The PC check is here because, if LOWPC and
9552 HIGHPC are both 0x0, then there won't be any interesting code in
9553 the CU, but a check later on (in
9554 lnp_state_machine::check_line_address) will fail to properly
9555 exclude an entry that was removed via --gc-sections. */
9556 if (lowpc != highpc)
9557 handle_DW_AT_stmt_list (die, cu, lowpc);
9558
9559 /* Process all dies in compilation unit. */
9560 if (die->child != NULL)
9561 {
9562 child_die = die->child;
9563 while (child_die && child_die->tag)
9564 {
9565 process_die (child_die, cu);
9566 child_die = child_die->sibling;
9567 }
9568 }
9569 per_objfile->sym_cu = nullptr;
9570
9571 /* Decode macro information, if present. Dwarf 2 macro information
9572 refers to information in the line number info statement program
9573 header, so we can only read it if we've read the header
9574 successfully. */
9575 attr = dwarf2_attr (die, DW_AT_macros, cu);
9576 if (attr == NULL)
9577 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9578 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9579 {
9580 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9581 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9582
9583 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
9584 }
9585 else
9586 {
9587 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9588 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9589 {
9590 unsigned int macro_offset = attr->as_unsigned ();
9591
9592 dwarf_decode_macros (cu, macro_offset, 0);
9593 }
9594 }
9595 }
9596
9597 void
9598 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
9599 {
9600 struct type_unit_group *tu_group;
9601 int first_time;
9602 struct attribute *attr;
9603 unsigned int i;
9604 struct signatured_type *sig_type;
9605
9606 gdb_assert (per_cu->is_debug_types);
9607 sig_type = (struct signatured_type *) per_cu;
9608
9609 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
9610
9611 /* If we're using .gdb_index (includes -readnow) then
9612 per_cu->type_unit_group may not have been set up yet. */
9613 if (sig_type->type_unit_group == NULL)
9614 sig_type->type_unit_group = get_type_unit_group (this, attr);
9615 tu_group = sig_type->type_unit_group;
9616
9617 /* If we've already processed this stmt_list there's no real need to
9618 do it again, we could fake it and just recreate the part we need
9619 (file name,index -> symtab mapping). If data shows this optimization
9620 is useful we can do it then. */
9621 type_unit_group_unshareable *tug_unshare
9622 = per_objfile->get_type_unit_group_unshareable (tu_group);
9623 first_time = tug_unshare->compunit_symtab == NULL;
9624
9625 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9626 debug info. */
9627 line_header_up lh;
9628 if (attr != NULL && attr->form_is_unsigned ())
9629 {
9630 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9631 lh = dwarf_decode_line_header (line_offset, this);
9632 }
9633 if (lh == NULL)
9634 {
9635 if (first_time)
9636 start_compunit_symtab ("", NULL, 0);
9637 else
9638 {
9639 gdb_assert (tug_unshare->symtabs == NULL);
9640 gdb_assert (m_builder == nullptr);
9641 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9642 m_builder.reset (new struct buildsym_compunit
9643 (cust->objfile (), "",
9644 cust->dirname (),
9645 compunit_language (cust),
9646 0, cust));
9647 list_in_scope = get_builder ()->get_file_symbols ();
9648 }
9649 return;
9650 }
9651
9652 line_header = lh.release ();
9653 line_header_die_owner = die;
9654
9655 if (first_time)
9656 {
9657 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
9658
9659 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9660 still initializing it, and our caller (a few levels up)
9661 process_full_type_unit still needs to know if this is the first
9662 time. */
9663
9664 tug_unshare->symtabs
9665 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
9666 struct symtab *, line_header->file_names_size ());
9667
9668 auto &file_names = line_header->file_names ();
9669 for (i = 0; i < file_names.size (); ++i)
9670 {
9671 file_entry &fe = file_names[i];
9672 dwarf2_start_subfile (this, fe.name,
9673 fe.include_dir (line_header));
9674 buildsym_compunit *b = get_builder ();
9675 if (b->get_current_subfile ()->symtab == NULL)
9676 {
9677 /* NOTE: start_subfile will recognize when it's been
9678 passed a file it has already seen. So we can't
9679 assume there's a simple mapping from
9680 cu->line_header->file_names to subfiles, plus
9681 cu->line_header->file_names may contain dups. */
9682 const char *name = b->get_current_subfile ()->name.c_str ();
9683 b->get_current_subfile ()->symtab = allocate_symtab (cust, name);
9684 }
9685
9686 fe.symtab = b->get_current_subfile ()->symtab;
9687 tug_unshare->symtabs[i] = fe.symtab;
9688 }
9689 }
9690 else
9691 {
9692 gdb_assert (m_builder == nullptr);
9693 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9694 m_builder.reset (new struct buildsym_compunit
9695 (cust->objfile (), "",
9696 cust->dirname (),
9697 compunit_language (cust),
9698 0, cust));
9699 list_in_scope = get_builder ()->get_file_symbols ();
9700
9701 auto &file_names = line_header->file_names ();
9702 for (i = 0; i < file_names.size (); ++i)
9703 {
9704 file_entry &fe = file_names[i];
9705 fe.symtab = tug_unshare->symtabs[i];
9706 }
9707 }
9708
9709 /* The main symtab is allocated last. Type units don't have DW_AT_name
9710 so they don't have a "real" (so to speak) symtab anyway.
9711 There is later code that will assign the main symtab to all symbols
9712 that don't have one. We need to handle the case of a symbol with a
9713 missing symtab (DW_AT_decl_file) anyway. */
9714 }
9715
9716 /* Process DW_TAG_type_unit.
9717 For TUs we want to skip the first top level sibling if it's not the
9718 actual type being defined by this TU. In this case the first top
9719 level sibling is there to provide context only. */
9720
9721 static void
9722 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9723 {
9724 struct die_info *child_die;
9725
9726 prepare_one_comp_unit (cu, die, language_minimal);
9727
9728 /* Initialize (or reinitialize) the machinery for building symtabs.
9729 We do this before processing child DIEs, so that the line header table
9730 is available for DW_AT_decl_file. */
9731 cu->setup_type_unit_groups (die);
9732
9733 if (die->child != NULL)
9734 {
9735 child_die = die->child;
9736 while (child_die && child_die->tag)
9737 {
9738 process_die (child_die, cu);
9739 child_die = child_die->sibling;
9740 }
9741 }
9742 }
9743 \f
9744 /* DWO/DWP files.
9745
9746 http://gcc.gnu.org/wiki/DebugFission
9747 http://gcc.gnu.org/wiki/DebugFissionDWP
9748
9749 To simplify handling of both DWO files ("object" files with the DWARF info)
9750 and DWP files (a file with the DWOs packaged up into one file), we treat
9751 DWP files as having a collection of virtual DWO files. */
9752
9753 static hashval_t
9754 hash_dwo_file (const void *item)
9755 {
9756 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9757 hashval_t hash;
9758
9759 hash = htab_hash_string (dwo_file->dwo_name);
9760 if (dwo_file->comp_dir != NULL)
9761 hash += htab_hash_string (dwo_file->comp_dir);
9762 return hash;
9763 }
9764
9765 static int
9766 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9767 {
9768 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9769 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9770
9771 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9772 return 0;
9773 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9774 return lhs->comp_dir == rhs->comp_dir;
9775 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9776 }
9777
9778 /* Allocate a hash table for DWO files. */
9779
9780 static htab_up
9781 allocate_dwo_file_hash_table ()
9782 {
9783 return htab_up (htab_create_alloc (41,
9784 hash_dwo_file,
9785 eq_dwo_file,
9786 htab_delete_entry<dwo_file>,
9787 xcalloc, xfree));
9788 }
9789
9790 /* Lookup DWO file DWO_NAME. */
9791
9792 static void **
9793 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
9794 const char *dwo_name,
9795 const char *comp_dir)
9796 {
9797 struct dwo_file find_entry;
9798 void **slot;
9799
9800 if (per_objfile->per_bfd->dwo_files == NULL)
9801 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
9802
9803 find_entry.dwo_name = dwo_name;
9804 find_entry.comp_dir = comp_dir;
9805 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
9806 INSERT);
9807
9808 return slot;
9809 }
9810
9811 static hashval_t
9812 hash_dwo_unit (const void *item)
9813 {
9814 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9815
9816 /* This drops the top 32 bits of the id, but is ok for a hash. */
9817 return dwo_unit->signature;
9818 }
9819
9820 static int
9821 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9822 {
9823 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9824 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9825
9826 /* The signature is assumed to be unique within the DWO file.
9827 So while object file CU dwo_id's always have the value zero,
9828 that's OK, assuming each object file DWO file has only one CU,
9829 and that's the rule for now. */
9830 return lhs->signature == rhs->signature;
9831 }
9832
9833 /* Allocate a hash table for DWO CUs,TUs.
9834 There is one of these tables for each of CUs,TUs for each DWO file. */
9835
9836 static htab_up
9837 allocate_dwo_unit_table ()
9838 {
9839 /* Start out with a pretty small number.
9840 Generally DWO files contain only one CU and maybe some TUs. */
9841 return htab_up (htab_create_alloc (3,
9842 hash_dwo_unit,
9843 eq_dwo_unit,
9844 NULL, xcalloc, xfree));
9845 }
9846
9847 /* die_reader_func for create_dwo_cu. */
9848
9849 static void
9850 create_dwo_cu_reader (const struct die_reader_specs *reader,
9851 const gdb_byte *info_ptr,
9852 struct die_info *comp_unit_die,
9853 struct dwo_file *dwo_file,
9854 struct dwo_unit *dwo_unit)
9855 {
9856 struct dwarf2_cu *cu = reader->cu;
9857 sect_offset sect_off = cu->per_cu->sect_off;
9858 struct dwarf2_section_info *section = cu->per_cu->section;
9859
9860 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
9861 if (!signature.has_value ())
9862 {
9863 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9864 " its dwo_id [in module %s]"),
9865 sect_offset_str (sect_off), dwo_file->dwo_name);
9866 return;
9867 }
9868
9869 dwo_unit->dwo_file = dwo_file;
9870 dwo_unit->signature = *signature;
9871 dwo_unit->section = section;
9872 dwo_unit->sect_off = sect_off;
9873 dwo_unit->length = cu->per_cu->length;
9874
9875 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9876 sect_offset_str (sect_off),
9877 hex_string (dwo_unit->signature));
9878 }
9879
9880 /* Create the dwo_units for the CUs in a DWO_FILE.
9881 Note: This function processes DWO files only, not DWP files. */
9882
9883 static void
9884 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
9885 dwarf2_cu *cu, struct dwo_file &dwo_file,
9886 dwarf2_section_info &section, htab_up &cus_htab)
9887 {
9888 struct objfile *objfile = per_objfile->objfile;
9889 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
9890 const gdb_byte *info_ptr, *end_ptr;
9891
9892 section.read (objfile);
9893 info_ptr = section.buffer;
9894
9895 if (info_ptr == NULL)
9896 return;
9897
9898 dwarf_read_debug_printf ("Reading %s for %s:",
9899 section.get_name (),
9900 section.get_file_name ());
9901
9902 end_ptr = info_ptr + section.size;
9903 while (info_ptr < end_ptr)
9904 {
9905 struct dwarf2_per_cu_data per_cu;
9906 struct dwo_unit read_unit {};
9907 struct dwo_unit *dwo_unit;
9908 void **slot;
9909 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
9910
9911 per_cu.per_bfd = per_bfd;
9912 per_cu.is_debug_types = 0;
9913 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9914 per_cu.section = &section;
9915
9916 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
9917 if (!reader.dummy_p)
9918 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
9919 &dwo_file, &read_unit);
9920 info_ptr += per_cu.length;
9921
9922 // If the unit could not be parsed, skip it.
9923 if (read_unit.dwo_file == NULL)
9924 continue;
9925
9926 if (cus_htab == NULL)
9927 cus_htab = allocate_dwo_unit_table ();
9928
9929 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
9930 struct dwo_unit);
9931 *dwo_unit = read_unit;
9932 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
9933 gdb_assert (slot != NULL);
9934 if (*slot != NULL)
9935 {
9936 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9937 sect_offset dup_sect_off = dup_cu->sect_off;
9938
9939 complaint (_("debug cu entry at offset %s is duplicate to"
9940 " the entry at offset %s, signature %s"),
9941 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
9942 hex_string (dwo_unit->signature));
9943 }
9944 *slot = (void *)dwo_unit;
9945 }
9946 }
9947
9948 /* DWP file .debug_{cu,tu}_index section format:
9949 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9950 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
9951
9952 DWP Versions 1 & 2 are older, pre-standard format versions. The first
9953 officially standard DWP format was published with DWARF v5 and is called
9954 Version 5. There are no versions 3 or 4.
9955
9956 DWP Version 1:
9957
9958 Both index sections have the same format, and serve to map a 64-bit
9959 signature to a set of section numbers. Each section begins with a header,
9960 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9961 indexes, and a pool of 32-bit section numbers. The index sections will be
9962 aligned at 8-byte boundaries in the file.
9963
9964 The index section header consists of:
9965
9966 V, 32 bit version number
9967 -, 32 bits unused
9968 N, 32 bit number of compilation units or type units in the index
9969 M, 32 bit number of slots in the hash table
9970
9971 Numbers are recorded using the byte order of the application binary.
9972
9973 The hash table begins at offset 16 in the section, and consists of an array
9974 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9975 order of the application binary). Unused slots in the hash table are 0.
9976 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9977
9978 The parallel table begins immediately after the hash table
9979 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9980 array of 32-bit indexes (using the byte order of the application binary),
9981 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9982 table contains a 32-bit index into the pool of section numbers. For unused
9983 hash table slots, the corresponding entry in the parallel table will be 0.
9984
9985 The pool of section numbers begins immediately following the hash table
9986 (at offset 16 + 12 * M from the beginning of the section). The pool of
9987 section numbers consists of an array of 32-bit words (using the byte order
9988 of the application binary). Each item in the array is indexed starting
9989 from 0. The hash table entry provides the index of the first section
9990 number in the set. Additional section numbers in the set follow, and the
9991 set is terminated by a 0 entry (section number 0 is not used in ELF).
9992
9993 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9994 section must be the first entry in the set, and the .debug_abbrev.dwo must
9995 be the second entry. Other members of the set may follow in any order.
9996
9997 ---
9998
9999 DWP Versions 2 and 5:
10000
10001 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10002 and the entries in the index tables are now offsets into these sections.
10003 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10004 section.
10005
10006 Index Section Contents:
10007 Header
10008 Hash Table of Signatures dwp_hash_table.hash_table
10009 Parallel Table of Indices dwp_hash_table.unit_table
10010 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10011 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10012
10013 The index section header consists of:
10014
10015 V, 32 bit version number
10016 L, 32 bit number of columns in the table of section offsets
10017 N, 32 bit number of compilation units or type units in the index
10018 M, 32 bit number of slots in the hash table
10019
10020 Numbers are recorded using the byte order of the application binary.
10021
10022 The hash table has the same format as version 1.
10023 The parallel table of indices has the same format as version 1,
10024 except that the entries are origin-1 indices into the table of sections
10025 offsets and the table of section sizes.
10026
10027 The table of offsets begins immediately following the parallel table
10028 (at offset 16 + 12 * M from the beginning of the section). The table is
10029 a two-dimensional array of 32-bit words (using the byte order of the
10030 application binary), with L columns and N+1 rows, in row-major order.
10031 Each row in the array is indexed starting from 0. The first row provides
10032 a key to the remaining rows: each column in this row provides an identifier
10033 for a debug section, and the offsets in the same column of subsequent rows
10034 refer to that section. The section identifiers for Version 2 are:
10035
10036 DW_SECT_INFO 1 .debug_info.dwo
10037 DW_SECT_TYPES 2 .debug_types.dwo
10038 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10039 DW_SECT_LINE 4 .debug_line.dwo
10040 DW_SECT_LOC 5 .debug_loc.dwo
10041 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10042 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10043 DW_SECT_MACRO 8 .debug_macro.dwo
10044
10045 The section identifiers for Version 5 are:
10046
10047 DW_SECT_INFO_V5 1 .debug_info.dwo
10048 DW_SECT_RESERVED_V5 2 --
10049 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10050 DW_SECT_LINE_V5 4 .debug_line.dwo
10051 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10052 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10053 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10054 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10055
10056 The offsets provided by the CU and TU index sections are the base offsets
10057 for the contributions made by each CU or TU to the corresponding section
10058 in the package file. Each CU and TU header contains an abbrev_offset
10059 field, used to find the abbreviations table for that CU or TU within the
10060 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10061 be interpreted as relative to the base offset given in the index section.
10062 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10063 should be interpreted as relative to the base offset for .debug_line.dwo,
10064 and offsets into other debug sections obtained from DWARF attributes should
10065 also be interpreted as relative to the corresponding base offset.
10066
10067 The table of sizes begins immediately following the table of offsets.
10068 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10069 with L columns and N rows, in row-major order. Each row in the array is
10070 indexed starting from 1 (row 0 is shared by the two tables).
10071
10072 ---
10073
10074 Hash table lookup is handled the same in version 1 and 2:
10075
10076 We assume that N and M will not exceed 2^32 - 1.
10077 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10078
10079 Given a 64-bit compilation unit signature or a type signature S, an entry
10080 in the hash table is located as follows:
10081
10082 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10083 the low-order k bits all set to 1.
10084
10085 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10086
10087 3) If the hash table entry at index H matches the signature, use that
10088 entry. If the hash table entry at index H is unused (all zeroes),
10089 terminate the search: the signature is not present in the table.
10090
10091 4) Let H = (H + H') modulo M. Repeat at Step 3.
10092
10093 Because M > N and H' and M are relatively prime, the search is guaranteed
10094 to stop at an unused slot or find the match. */
10095
10096 /* Create a hash table to map DWO IDs to their CU/TU entry in
10097 .debug_{info,types}.dwo in DWP_FILE.
10098 Returns NULL if there isn't one.
10099 Note: This function processes DWP files only, not DWO files. */
10100
10101 static struct dwp_hash_table *
10102 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
10103 struct dwp_file *dwp_file, int is_debug_types)
10104 {
10105 struct objfile *objfile = per_objfile->objfile;
10106 bfd *dbfd = dwp_file->dbfd.get ();
10107 const gdb_byte *index_ptr, *index_end;
10108 struct dwarf2_section_info *index;
10109 uint32_t version, nr_columns, nr_units, nr_slots;
10110 struct dwp_hash_table *htab;
10111
10112 if (is_debug_types)
10113 index = &dwp_file->sections.tu_index;
10114 else
10115 index = &dwp_file->sections.cu_index;
10116
10117 if (index->empty ())
10118 return NULL;
10119 index->read (objfile);
10120
10121 index_ptr = index->buffer;
10122 index_end = index_ptr + index->size;
10123
10124 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10125 For now it's safe to just read 4 bytes (particularly as it's difficult to
10126 tell if you're dealing with Version 5 before you've read the version). */
10127 version = read_4_bytes (dbfd, index_ptr);
10128 index_ptr += 4;
10129 if (version == 2 || version == 5)
10130 nr_columns = read_4_bytes (dbfd, index_ptr);
10131 else
10132 nr_columns = 0;
10133 index_ptr += 4;
10134 nr_units = read_4_bytes (dbfd, index_ptr);
10135 index_ptr += 4;
10136 nr_slots = read_4_bytes (dbfd, index_ptr);
10137 index_ptr += 4;
10138
10139 if (version != 1 && version != 2 && version != 5)
10140 {
10141 error (_("Dwarf Error: unsupported DWP file version (%s)"
10142 " [in module %s]"),
10143 pulongest (version), dwp_file->name);
10144 }
10145 if (nr_slots != (nr_slots & -nr_slots))
10146 {
10147 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10148 " is not power of 2 [in module %s]"),
10149 pulongest (nr_slots), dwp_file->name);
10150 }
10151
10152 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
10153 htab->version = version;
10154 htab->nr_columns = nr_columns;
10155 htab->nr_units = nr_units;
10156 htab->nr_slots = nr_slots;
10157 htab->hash_table = index_ptr;
10158 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10159
10160 /* Exit early if the table is empty. */
10161 if (nr_slots == 0 || nr_units == 0
10162 || (version == 2 && nr_columns == 0)
10163 || (version == 5 && nr_columns == 0))
10164 {
10165 /* All must be zero. */
10166 if (nr_slots != 0 || nr_units != 0
10167 || (version == 2 && nr_columns != 0)
10168 || (version == 5 && nr_columns != 0))
10169 {
10170 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10171 " all zero [in modules %s]"),
10172 dwp_file->name);
10173 }
10174 return htab;
10175 }
10176
10177 if (version == 1)
10178 {
10179 htab->section_pool.v1.indices =
10180 htab->unit_table + sizeof (uint32_t) * nr_slots;
10181 /* It's harder to decide whether the section is too small in v1.
10182 V1 is deprecated anyway so we punt. */
10183 }
10184 else if (version == 2)
10185 {
10186 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10187 int *ids = htab->section_pool.v2.section_ids;
10188 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
10189 /* Reverse map for error checking. */
10190 int ids_seen[DW_SECT_MAX + 1];
10191 int i;
10192
10193 if (nr_columns < 2)
10194 {
10195 error (_("Dwarf Error: bad DWP hash table, too few columns"
10196 " in section table [in module %s]"),
10197 dwp_file->name);
10198 }
10199 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10200 {
10201 error (_("Dwarf Error: bad DWP hash table, too many columns"
10202 " in section table [in module %s]"),
10203 dwp_file->name);
10204 }
10205 memset (ids, 255, sizeof_ids);
10206 memset (ids_seen, 255, sizeof (ids_seen));
10207 for (i = 0; i < nr_columns; ++i)
10208 {
10209 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10210
10211 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10212 {
10213 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10214 " in section table [in module %s]"),
10215 id, dwp_file->name);
10216 }
10217 if (ids_seen[id] != -1)
10218 {
10219 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10220 " id %d in section table [in module %s]"),
10221 id, dwp_file->name);
10222 }
10223 ids_seen[id] = i;
10224 ids[i] = id;
10225 }
10226 /* Must have exactly one info or types section. */
10227 if (((ids_seen[DW_SECT_INFO] != -1)
10228 + (ids_seen[DW_SECT_TYPES] != -1))
10229 != 1)
10230 {
10231 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10232 " DWO info/types section [in module %s]"),
10233 dwp_file->name);
10234 }
10235 /* Must have an abbrev section. */
10236 if (ids_seen[DW_SECT_ABBREV] == -1)
10237 {
10238 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10239 " section [in module %s]"),
10240 dwp_file->name);
10241 }
10242 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10243 htab->section_pool.v2.sizes =
10244 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10245 * nr_units * nr_columns);
10246 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10247 * nr_units * nr_columns))
10248 > index_end)
10249 {
10250 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10251 " [in module %s]"),
10252 dwp_file->name);
10253 }
10254 }
10255 else /* version == 5 */
10256 {
10257 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10258 int *ids = htab->section_pool.v5.section_ids;
10259 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
10260 /* Reverse map for error checking. */
10261 int ids_seen[DW_SECT_MAX_V5 + 1];
10262
10263 if (nr_columns < 2)
10264 {
10265 error (_("Dwarf Error: bad DWP hash table, too few columns"
10266 " in section table [in module %s]"),
10267 dwp_file->name);
10268 }
10269 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
10270 {
10271 error (_("Dwarf Error: bad DWP hash table, too many columns"
10272 " in section table [in module %s]"),
10273 dwp_file->name);
10274 }
10275 memset (ids, 255, sizeof_ids);
10276 memset (ids_seen, 255, sizeof (ids_seen));
10277 for (int i = 0; i < nr_columns; ++i)
10278 {
10279 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10280
10281 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
10282 {
10283 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10284 " in section table [in module %s]"),
10285 id, dwp_file->name);
10286 }
10287 if (ids_seen[id] != -1)
10288 {
10289 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10290 " id %d in section table [in module %s]"),
10291 id, dwp_file->name);
10292 }
10293 ids_seen[id] = i;
10294 ids[i] = id;
10295 }
10296 /* Must have seen an info section. */
10297 if (ids_seen[DW_SECT_INFO_V5] == -1)
10298 {
10299 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10300 " DWO info/types section [in module %s]"),
10301 dwp_file->name);
10302 }
10303 /* Must have an abbrev section. */
10304 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
10305 {
10306 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10307 " section [in module %s]"),
10308 dwp_file->name);
10309 }
10310 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10311 htab->section_pool.v5.sizes
10312 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
10313 * nr_units * nr_columns);
10314 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
10315 * nr_units * nr_columns))
10316 > index_end)
10317 {
10318 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10319 " [in module %s]"),
10320 dwp_file->name);
10321 }
10322 }
10323
10324 return htab;
10325 }
10326
10327 /* Update SECTIONS with the data from SECTP.
10328
10329 This function is like the other "locate" section routines, but in
10330 this context the sections to read comes from the DWP V1 hash table,
10331 not the full ELF section table.
10332
10333 The result is non-zero for success, or zero if an error was found. */
10334
10335 static int
10336 locate_v1_virtual_dwo_sections (asection *sectp,
10337 struct virtual_v1_dwo_sections *sections)
10338 {
10339 const struct dwop_section_names *names = &dwop_section_names;
10340
10341 if (names->abbrev_dwo.matches (sectp->name))
10342 {
10343 /* There can be only one. */
10344 if (sections->abbrev.s.section != NULL)
10345 return 0;
10346 sections->abbrev.s.section = sectp;
10347 sections->abbrev.size = bfd_section_size (sectp);
10348 }
10349 else if (names->info_dwo.matches (sectp->name)
10350 || names->types_dwo.matches (sectp->name))
10351 {
10352 /* There can be only one. */
10353 if (sections->info_or_types.s.section != NULL)
10354 return 0;
10355 sections->info_or_types.s.section = sectp;
10356 sections->info_or_types.size = bfd_section_size (sectp);
10357 }
10358 else if (names->line_dwo.matches (sectp->name))
10359 {
10360 /* There can be only one. */
10361 if (sections->line.s.section != NULL)
10362 return 0;
10363 sections->line.s.section = sectp;
10364 sections->line.size = bfd_section_size (sectp);
10365 }
10366 else if (names->loc_dwo.matches (sectp->name))
10367 {
10368 /* There can be only one. */
10369 if (sections->loc.s.section != NULL)
10370 return 0;
10371 sections->loc.s.section = sectp;
10372 sections->loc.size = bfd_section_size (sectp);
10373 }
10374 else if (names->macinfo_dwo.matches (sectp->name))
10375 {
10376 /* There can be only one. */
10377 if (sections->macinfo.s.section != NULL)
10378 return 0;
10379 sections->macinfo.s.section = sectp;
10380 sections->macinfo.size = bfd_section_size (sectp);
10381 }
10382 else if (names->macro_dwo.matches (sectp->name))
10383 {
10384 /* There can be only one. */
10385 if (sections->macro.s.section != NULL)
10386 return 0;
10387 sections->macro.s.section = sectp;
10388 sections->macro.size = bfd_section_size (sectp);
10389 }
10390 else if (names->str_offsets_dwo.matches (sectp->name))
10391 {
10392 /* There can be only one. */
10393 if (sections->str_offsets.s.section != NULL)
10394 return 0;
10395 sections->str_offsets.s.section = sectp;
10396 sections->str_offsets.size = bfd_section_size (sectp);
10397 }
10398 else
10399 {
10400 /* No other kind of section is valid. */
10401 return 0;
10402 }
10403
10404 return 1;
10405 }
10406
10407 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10408 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10409 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10410 This is for DWP version 1 files. */
10411
10412 static struct dwo_unit *
10413 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
10414 struct dwp_file *dwp_file,
10415 uint32_t unit_index,
10416 const char *comp_dir,
10417 ULONGEST signature, int is_debug_types)
10418 {
10419 const struct dwp_hash_table *dwp_htab =
10420 is_debug_types ? dwp_file->tus : dwp_file->cus;
10421 bfd *dbfd = dwp_file->dbfd.get ();
10422 const char *kind = is_debug_types ? "TU" : "CU";
10423 struct dwo_file *dwo_file;
10424 struct dwo_unit *dwo_unit;
10425 struct virtual_v1_dwo_sections sections;
10426 void **dwo_file_slot;
10427 int i;
10428
10429 gdb_assert (dwp_file->version == 1);
10430
10431 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10432 kind, pulongest (unit_index), hex_string (signature),
10433 dwp_file->name);
10434
10435 /* Fetch the sections of this DWO unit.
10436 Put a limit on the number of sections we look for so that bad data
10437 doesn't cause us to loop forever. */
10438
10439 #define MAX_NR_V1_DWO_SECTIONS \
10440 (1 /* .debug_info or .debug_types */ \
10441 + 1 /* .debug_abbrev */ \
10442 + 1 /* .debug_line */ \
10443 + 1 /* .debug_loc */ \
10444 + 1 /* .debug_str_offsets */ \
10445 + 1 /* .debug_macro or .debug_macinfo */ \
10446 + 1 /* trailing zero */)
10447
10448 memset (&sections, 0, sizeof (sections));
10449
10450 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10451 {
10452 asection *sectp;
10453 uint32_t section_nr =
10454 read_4_bytes (dbfd,
10455 dwp_htab->section_pool.v1.indices
10456 + (unit_index + i) * sizeof (uint32_t));
10457
10458 if (section_nr == 0)
10459 break;
10460 if (section_nr >= dwp_file->num_sections)
10461 {
10462 error (_("Dwarf Error: bad DWP hash table, section number too large"
10463 " [in module %s]"),
10464 dwp_file->name);
10465 }
10466
10467 sectp = dwp_file->elf_sections[section_nr];
10468 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10469 {
10470 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10471 " [in module %s]"),
10472 dwp_file->name);
10473 }
10474 }
10475
10476 if (i < 2
10477 || sections.info_or_types.empty ()
10478 || sections.abbrev.empty ())
10479 {
10480 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10481 " [in module %s]"),
10482 dwp_file->name);
10483 }
10484 if (i == MAX_NR_V1_DWO_SECTIONS)
10485 {
10486 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10487 " [in module %s]"),
10488 dwp_file->name);
10489 }
10490
10491 /* It's easier for the rest of the code if we fake a struct dwo_file and
10492 have dwo_unit "live" in that. At least for now.
10493
10494 The DWP file can be made up of a random collection of CUs and TUs.
10495 However, for each CU + set of TUs that came from the same original DWO
10496 file, we can combine them back into a virtual DWO file to save space
10497 (fewer struct dwo_file objects to allocate). Remember that for really
10498 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10499
10500 std::string virtual_dwo_name =
10501 string_printf ("virtual-dwo/%d-%d-%d-%d",
10502 sections.abbrev.get_id (),
10503 sections.line.get_id (),
10504 sections.loc.get_id (),
10505 sections.str_offsets.get_id ());
10506 /* Can we use an existing virtual DWO file? */
10507 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10508 comp_dir);
10509 /* Create one if necessary. */
10510 if (*dwo_file_slot == NULL)
10511 {
10512 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10513 virtual_dwo_name.c_str ());
10514
10515 dwo_file = new struct dwo_file;
10516 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10517 dwo_file->comp_dir = comp_dir;
10518 dwo_file->sections.abbrev = sections.abbrev;
10519 dwo_file->sections.line = sections.line;
10520 dwo_file->sections.loc = sections.loc;
10521 dwo_file->sections.macinfo = sections.macinfo;
10522 dwo_file->sections.macro = sections.macro;
10523 dwo_file->sections.str_offsets = sections.str_offsets;
10524 /* The "str" section is global to the entire DWP file. */
10525 dwo_file->sections.str = dwp_file->sections.str;
10526 /* The info or types section is assigned below to dwo_unit,
10527 there's no need to record it in dwo_file.
10528 Also, we can't simply record type sections in dwo_file because
10529 we record a pointer into the vector in dwo_unit. As we collect more
10530 types we'll grow the vector and eventually have to reallocate space
10531 for it, invalidating all copies of pointers into the previous
10532 contents. */
10533 *dwo_file_slot = dwo_file;
10534 }
10535 else
10536 {
10537 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10538 virtual_dwo_name.c_str ());
10539
10540 dwo_file = (struct dwo_file *) *dwo_file_slot;
10541 }
10542
10543 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10544 dwo_unit->dwo_file = dwo_file;
10545 dwo_unit->signature = signature;
10546 dwo_unit->section =
10547 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10548 *dwo_unit->section = sections.info_or_types;
10549 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10550
10551 return dwo_unit;
10552 }
10553
10554 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10555 simplify them. Given a pointer to the containing section SECTION, and
10556 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10557 virtual section of just that piece. */
10558
10559 static struct dwarf2_section_info
10560 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
10561 struct dwarf2_section_info *section,
10562 bfd_size_type offset, bfd_size_type size)
10563 {
10564 struct dwarf2_section_info result;
10565 asection *sectp;
10566
10567 gdb_assert (section != NULL);
10568 gdb_assert (!section->is_virtual);
10569
10570 memset (&result, 0, sizeof (result));
10571 result.s.containing_section = section;
10572 result.is_virtual = true;
10573
10574 if (size == 0)
10575 return result;
10576
10577 sectp = section->get_bfd_section ();
10578
10579 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10580 bounds of the real section. This is a pretty-rare event, so just
10581 flag an error (easier) instead of a warning and trying to cope. */
10582 if (sectp == NULL
10583 || offset + size > bfd_section_size (sectp))
10584 {
10585 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10586 " in section %s [in module %s]"),
10587 sectp ? bfd_section_name (sectp) : "<unknown>",
10588 objfile_name (per_objfile->objfile));
10589 }
10590
10591 result.virtual_offset = offset;
10592 result.size = size;
10593 return result;
10594 }
10595
10596 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10597 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10598 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10599 This is for DWP version 2 files. */
10600
10601 static struct dwo_unit *
10602 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
10603 struct dwp_file *dwp_file,
10604 uint32_t unit_index,
10605 const char *comp_dir,
10606 ULONGEST signature, int is_debug_types)
10607 {
10608 const struct dwp_hash_table *dwp_htab =
10609 is_debug_types ? dwp_file->tus : dwp_file->cus;
10610 bfd *dbfd = dwp_file->dbfd.get ();
10611 const char *kind = is_debug_types ? "TU" : "CU";
10612 struct dwo_file *dwo_file;
10613 struct dwo_unit *dwo_unit;
10614 struct virtual_v2_or_v5_dwo_sections sections;
10615 void **dwo_file_slot;
10616 int i;
10617
10618 gdb_assert (dwp_file->version == 2);
10619
10620 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10621 kind, pulongest (unit_index), hex_string (signature),
10622 dwp_file->name);
10623
10624 /* Fetch the section offsets of this DWO unit. */
10625
10626 memset (&sections, 0, sizeof (sections));
10627
10628 for (i = 0; i < dwp_htab->nr_columns; ++i)
10629 {
10630 uint32_t offset = read_4_bytes (dbfd,
10631 dwp_htab->section_pool.v2.offsets
10632 + (((unit_index - 1) * dwp_htab->nr_columns
10633 + i)
10634 * sizeof (uint32_t)));
10635 uint32_t size = read_4_bytes (dbfd,
10636 dwp_htab->section_pool.v2.sizes
10637 + (((unit_index - 1) * dwp_htab->nr_columns
10638 + i)
10639 * sizeof (uint32_t)));
10640
10641 switch (dwp_htab->section_pool.v2.section_ids[i])
10642 {
10643 case DW_SECT_INFO:
10644 case DW_SECT_TYPES:
10645 sections.info_or_types_offset = offset;
10646 sections.info_or_types_size = size;
10647 break;
10648 case DW_SECT_ABBREV:
10649 sections.abbrev_offset = offset;
10650 sections.abbrev_size = size;
10651 break;
10652 case DW_SECT_LINE:
10653 sections.line_offset = offset;
10654 sections.line_size = size;
10655 break;
10656 case DW_SECT_LOC:
10657 sections.loc_offset = offset;
10658 sections.loc_size = size;
10659 break;
10660 case DW_SECT_STR_OFFSETS:
10661 sections.str_offsets_offset = offset;
10662 sections.str_offsets_size = size;
10663 break;
10664 case DW_SECT_MACINFO:
10665 sections.macinfo_offset = offset;
10666 sections.macinfo_size = size;
10667 break;
10668 case DW_SECT_MACRO:
10669 sections.macro_offset = offset;
10670 sections.macro_size = size;
10671 break;
10672 }
10673 }
10674
10675 /* It's easier for the rest of the code if we fake a struct dwo_file and
10676 have dwo_unit "live" in that. At least for now.
10677
10678 The DWP file can be made up of a random collection of CUs and TUs.
10679 However, for each CU + set of TUs that came from the same original DWO
10680 file, we can combine them back into a virtual DWO file to save space
10681 (fewer struct dwo_file objects to allocate). Remember that for really
10682 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10683
10684 std::string virtual_dwo_name =
10685 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10686 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10687 (long) (sections.line_size ? sections.line_offset : 0),
10688 (long) (sections.loc_size ? sections.loc_offset : 0),
10689 (long) (sections.str_offsets_size
10690 ? sections.str_offsets_offset : 0));
10691 /* Can we use an existing virtual DWO file? */
10692 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10693 comp_dir);
10694 /* Create one if necessary. */
10695 if (*dwo_file_slot == NULL)
10696 {
10697 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10698 virtual_dwo_name.c_str ());
10699
10700 dwo_file = new struct dwo_file;
10701 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10702 dwo_file->comp_dir = comp_dir;
10703 dwo_file->sections.abbrev =
10704 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
10705 sections.abbrev_offset,
10706 sections.abbrev_size);
10707 dwo_file->sections.line =
10708 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
10709 sections.line_offset,
10710 sections.line_size);
10711 dwo_file->sections.loc =
10712 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
10713 sections.loc_offset, sections.loc_size);
10714 dwo_file->sections.macinfo =
10715 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
10716 sections.macinfo_offset,
10717 sections.macinfo_size);
10718 dwo_file->sections.macro =
10719 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
10720 sections.macro_offset,
10721 sections.macro_size);
10722 dwo_file->sections.str_offsets =
10723 create_dwp_v2_or_v5_section (per_objfile,
10724 &dwp_file->sections.str_offsets,
10725 sections.str_offsets_offset,
10726 sections.str_offsets_size);
10727 /* The "str" section is global to the entire DWP file. */
10728 dwo_file->sections.str = dwp_file->sections.str;
10729 /* The info or types section is assigned below to dwo_unit,
10730 there's no need to record it in dwo_file.
10731 Also, we can't simply record type sections in dwo_file because
10732 we record a pointer into the vector in dwo_unit. As we collect more
10733 types we'll grow the vector and eventually have to reallocate space
10734 for it, invalidating all copies of pointers into the previous
10735 contents. */
10736 *dwo_file_slot = dwo_file;
10737 }
10738 else
10739 {
10740 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10741 virtual_dwo_name.c_str ());
10742
10743 dwo_file = (struct dwo_file *) *dwo_file_slot;
10744 }
10745
10746 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10747 dwo_unit->dwo_file = dwo_file;
10748 dwo_unit->signature = signature;
10749 dwo_unit->section =
10750 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10751 *dwo_unit->section = create_dwp_v2_or_v5_section
10752 (per_objfile,
10753 is_debug_types
10754 ? &dwp_file->sections.types
10755 : &dwp_file->sections.info,
10756 sections.info_or_types_offset,
10757 sections.info_or_types_size);
10758 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10759
10760 return dwo_unit;
10761 }
10762
10763 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10764 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10765 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10766 This is for DWP version 5 files. */
10767
10768 static struct dwo_unit *
10769 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
10770 struct dwp_file *dwp_file,
10771 uint32_t unit_index,
10772 const char *comp_dir,
10773 ULONGEST signature, int is_debug_types)
10774 {
10775 const struct dwp_hash_table *dwp_htab
10776 = is_debug_types ? dwp_file->tus : dwp_file->cus;
10777 bfd *dbfd = dwp_file->dbfd.get ();
10778 const char *kind = is_debug_types ? "TU" : "CU";
10779 struct dwo_file *dwo_file;
10780 struct dwo_unit *dwo_unit;
10781 struct virtual_v2_or_v5_dwo_sections sections {};
10782 void **dwo_file_slot;
10783
10784 gdb_assert (dwp_file->version == 5);
10785
10786 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10787 kind, pulongest (unit_index), hex_string (signature),
10788 dwp_file->name);
10789
10790 /* Fetch the section offsets of this DWO unit. */
10791
10792 /* memset (&sections, 0, sizeof (sections)); */
10793
10794 for (int i = 0; i < dwp_htab->nr_columns; ++i)
10795 {
10796 uint32_t offset = read_4_bytes (dbfd,
10797 dwp_htab->section_pool.v5.offsets
10798 + (((unit_index - 1)
10799 * dwp_htab->nr_columns
10800 + i)
10801 * sizeof (uint32_t)));
10802 uint32_t size = read_4_bytes (dbfd,
10803 dwp_htab->section_pool.v5.sizes
10804 + (((unit_index - 1) * dwp_htab->nr_columns
10805 + i)
10806 * sizeof (uint32_t)));
10807
10808 switch (dwp_htab->section_pool.v5.section_ids[i])
10809 {
10810 case DW_SECT_ABBREV_V5:
10811 sections.abbrev_offset = offset;
10812 sections.abbrev_size = size;
10813 break;
10814 case DW_SECT_INFO_V5:
10815 sections.info_or_types_offset = offset;
10816 sections.info_or_types_size = size;
10817 break;
10818 case DW_SECT_LINE_V5:
10819 sections.line_offset = offset;
10820 sections.line_size = size;
10821 break;
10822 case DW_SECT_LOCLISTS_V5:
10823 sections.loclists_offset = offset;
10824 sections.loclists_size = size;
10825 break;
10826 case DW_SECT_MACRO_V5:
10827 sections.macro_offset = offset;
10828 sections.macro_size = size;
10829 break;
10830 case DW_SECT_RNGLISTS_V5:
10831 sections.rnglists_offset = offset;
10832 sections.rnglists_size = size;
10833 break;
10834 case DW_SECT_STR_OFFSETS_V5:
10835 sections.str_offsets_offset = offset;
10836 sections.str_offsets_size = size;
10837 break;
10838 case DW_SECT_RESERVED_V5:
10839 default:
10840 break;
10841 }
10842 }
10843
10844 /* It's easier for the rest of the code if we fake a struct dwo_file and
10845 have dwo_unit "live" in that. At least for now.
10846
10847 The DWP file can be made up of a random collection of CUs and TUs.
10848 However, for each CU + set of TUs that came from the same original DWO
10849 file, we can combine them back into a virtual DWO file to save space
10850 (fewer struct dwo_file objects to allocate). Remember that for really
10851 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10852
10853 std::string virtual_dwo_name =
10854 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10855 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10856 (long) (sections.line_size ? sections.line_offset : 0),
10857 (long) (sections.loclists_size ? sections.loclists_offset : 0),
10858 (long) (sections.str_offsets_size
10859 ? sections.str_offsets_offset : 0),
10860 (long) (sections.macro_size ? sections.macro_offset : 0),
10861 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
10862 /* Can we use an existing virtual DWO file? */
10863 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
10864 virtual_dwo_name.c_str (),
10865 comp_dir);
10866 /* Create one if necessary. */
10867 if (*dwo_file_slot == NULL)
10868 {
10869 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10870 virtual_dwo_name.c_str ());
10871
10872 dwo_file = new struct dwo_file;
10873 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10874 dwo_file->comp_dir = comp_dir;
10875 dwo_file->sections.abbrev =
10876 create_dwp_v2_or_v5_section (per_objfile,
10877 &dwp_file->sections.abbrev,
10878 sections.abbrev_offset,
10879 sections.abbrev_size);
10880 dwo_file->sections.line =
10881 create_dwp_v2_or_v5_section (per_objfile,
10882 &dwp_file->sections.line,
10883 sections.line_offset, sections.line_size);
10884 dwo_file->sections.macro =
10885 create_dwp_v2_or_v5_section (per_objfile,
10886 &dwp_file->sections.macro,
10887 sections.macro_offset,
10888 sections.macro_size);
10889 dwo_file->sections.loclists =
10890 create_dwp_v2_or_v5_section (per_objfile,
10891 &dwp_file->sections.loclists,
10892 sections.loclists_offset,
10893 sections.loclists_size);
10894 dwo_file->sections.rnglists =
10895 create_dwp_v2_or_v5_section (per_objfile,
10896 &dwp_file->sections.rnglists,
10897 sections.rnglists_offset,
10898 sections.rnglists_size);
10899 dwo_file->sections.str_offsets =
10900 create_dwp_v2_or_v5_section (per_objfile,
10901 &dwp_file->sections.str_offsets,
10902 sections.str_offsets_offset,
10903 sections.str_offsets_size);
10904 /* The "str" section is global to the entire DWP file. */
10905 dwo_file->sections.str = dwp_file->sections.str;
10906 /* The info or types section is assigned below to dwo_unit,
10907 there's no need to record it in dwo_file.
10908 Also, we can't simply record type sections in dwo_file because
10909 we record a pointer into the vector in dwo_unit. As we collect more
10910 types we'll grow the vector and eventually have to reallocate space
10911 for it, invalidating all copies of pointers into the previous
10912 contents. */
10913 *dwo_file_slot = dwo_file;
10914 }
10915 else
10916 {
10917 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10918 virtual_dwo_name.c_str ());
10919
10920 dwo_file = (struct dwo_file *) *dwo_file_slot;
10921 }
10922
10923 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10924 dwo_unit->dwo_file = dwo_file;
10925 dwo_unit->signature = signature;
10926 dwo_unit->section
10927 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10928 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
10929 &dwp_file->sections.info,
10930 sections.info_or_types_offset,
10931 sections.info_or_types_size);
10932 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10933
10934 return dwo_unit;
10935 }
10936
10937 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10938 Returns NULL if the signature isn't found. */
10939
10940 static struct dwo_unit *
10941 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
10942 struct dwp_file *dwp_file, const char *comp_dir,
10943 ULONGEST signature, int is_debug_types)
10944 {
10945 const struct dwp_hash_table *dwp_htab =
10946 is_debug_types ? dwp_file->tus : dwp_file->cus;
10947 bfd *dbfd = dwp_file->dbfd.get ();
10948 uint32_t mask = dwp_htab->nr_slots - 1;
10949 uint32_t hash = signature & mask;
10950 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10951 unsigned int i;
10952 void **slot;
10953 struct dwo_unit find_dwo_cu;
10954
10955 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10956 find_dwo_cu.signature = signature;
10957 slot = htab_find_slot (is_debug_types
10958 ? dwp_file->loaded_tus.get ()
10959 : dwp_file->loaded_cus.get (),
10960 &find_dwo_cu, INSERT);
10961
10962 if (*slot != NULL)
10963 return (struct dwo_unit *) *slot;
10964
10965 /* Use a for loop so that we don't loop forever on bad debug info. */
10966 for (i = 0; i < dwp_htab->nr_slots; ++i)
10967 {
10968 ULONGEST signature_in_table;
10969
10970 signature_in_table =
10971 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10972 if (signature_in_table == signature)
10973 {
10974 uint32_t unit_index =
10975 read_4_bytes (dbfd,
10976 dwp_htab->unit_table + hash * sizeof (uint32_t));
10977
10978 if (dwp_file->version == 1)
10979 {
10980 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
10981 unit_index, comp_dir,
10982 signature, is_debug_types);
10983 }
10984 else if (dwp_file->version == 2)
10985 {
10986 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
10987 unit_index, comp_dir,
10988 signature, is_debug_types);
10989 }
10990 else /* version == 5 */
10991 {
10992 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
10993 unit_index, comp_dir,
10994 signature, is_debug_types);
10995 }
10996 return (struct dwo_unit *) *slot;
10997 }
10998 if (signature_in_table == 0)
10999 return NULL;
11000 hash = (hash + hash2) & mask;
11001 }
11002
11003 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11004 " [in module %s]"),
11005 dwp_file->name);
11006 }
11007
11008 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11009 Open the file specified by FILE_NAME and hand it off to BFD for
11010 preliminary analysis. Return a newly initialized bfd *, which
11011 includes a canonicalized copy of FILE_NAME.
11012 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11013 SEARCH_CWD is true if the current directory is to be searched.
11014 It will be searched before debug-file-directory.
11015 If successful, the file is added to the bfd include table of the
11016 objfile's bfd (see gdb_bfd_record_inclusion).
11017 If unable to find/open the file, return NULL.
11018 NOTE: This function is derived from symfile_bfd_open. */
11019
11020 static gdb_bfd_ref_ptr
11021 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11022 const char *file_name, int is_dwp, int search_cwd)
11023 {
11024 int desc;
11025 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11026 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11027 to debug_file_directory. */
11028 const char *search_path;
11029 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11030
11031 gdb::unique_xmalloc_ptr<char> search_path_holder;
11032 if (search_cwd)
11033 {
11034 if (!debug_file_directory.empty ())
11035 {
11036 search_path_holder.reset (concat (".", dirname_separator_string,
11037 debug_file_directory.c_str (),
11038 (char *) NULL));
11039 search_path = search_path_holder.get ();
11040 }
11041 else
11042 search_path = ".";
11043 }
11044 else
11045 search_path = debug_file_directory.c_str ();
11046
11047 /* Add the path for the executable binary to the list of search paths. */
11048 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
11049 search_path_holder.reset (concat (objfile_dir.c_str (),
11050 dirname_separator_string,
11051 search_path, nullptr));
11052 search_path = search_path_holder.get ();
11053
11054 openp_flags flags = OPF_RETURN_REALPATH;
11055 if (is_dwp)
11056 flags |= OPF_SEARCH_IN_PATH;
11057
11058 gdb::unique_xmalloc_ptr<char> absolute_name;
11059 desc = openp (search_path, flags, file_name,
11060 O_RDONLY | O_BINARY, &absolute_name);
11061 if (desc < 0)
11062 return NULL;
11063
11064 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11065 gnutarget, desc));
11066 if (sym_bfd == NULL)
11067 return NULL;
11068 bfd_set_cacheable (sym_bfd.get (), 1);
11069
11070 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11071 return NULL;
11072
11073 /* Success. Record the bfd as having been included by the objfile's bfd.
11074 This is important because things like demangled_names_hash lives in the
11075 objfile's per_bfd space and may have references to things like symbol
11076 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11077 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
11078
11079 return sym_bfd;
11080 }
11081
11082 /* Try to open DWO file FILE_NAME.
11083 COMP_DIR is the DW_AT_comp_dir attribute.
11084 The result is the bfd handle of the file.
11085 If there is a problem finding or opening the file, return NULL.
11086 Upon success, the canonicalized path of the file is stored in the bfd,
11087 same as symfile_bfd_open. */
11088
11089 static gdb_bfd_ref_ptr
11090 open_dwo_file (dwarf2_per_objfile *per_objfile,
11091 const char *file_name, const char *comp_dir)
11092 {
11093 if (IS_ABSOLUTE_PATH (file_name))
11094 return try_open_dwop_file (per_objfile, file_name,
11095 0 /*is_dwp*/, 0 /*search_cwd*/);
11096
11097 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11098
11099 if (comp_dir != NULL)
11100 {
11101 std::string path_to_try = path_join (comp_dir, file_name);
11102
11103 /* NOTE: If comp_dir is a relative path, this will also try the
11104 search path, which seems useful. */
11105 gdb_bfd_ref_ptr abfd (try_open_dwop_file
11106 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11107
11108 if (abfd != NULL)
11109 return abfd;
11110 }
11111
11112 /* That didn't work, try debug-file-directory, which, despite its name,
11113 is a list of paths. */
11114
11115 if (debug_file_directory.empty ())
11116 return NULL;
11117
11118 return try_open_dwop_file (per_objfile, file_name,
11119 0 /*is_dwp*/, 1 /*search_cwd*/);
11120 }
11121
11122 /* This function is mapped across the sections and remembers the offset and
11123 size of each of the DWO debugging sections we are interested in. */
11124
11125 static void
11126 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
11127 asection *sectp, dwo_sections *dwo_sections)
11128 {
11129 const struct dwop_section_names *names = &dwop_section_names;
11130
11131 struct dwarf2_section_info *dw_sect = nullptr;
11132
11133 if (names->abbrev_dwo.matches (sectp->name))
11134 dw_sect = &dwo_sections->abbrev;
11135 else if (names->info_dwo.matches (sectp->name))
11136 dw_sect = &dwo_sections->info;
11137 else if (names->line_dwo.matches (sectp->name))
11138 dw_sect = &dwo_sections->line;
11139 else if (names->loc_dwo.matches (sectp->name))
11140 dw_sect = &dwo_sections->loc;
11141 else if (names->loclists_dwo.matches (sectp->name))
11142 dw_sect = &dwo_sections->loclists;
11143 else if (names->macinfo_dwo.matches (sectp->name))
11144 dw_sect = &dwo_sections->macinfo;
11145 else if (names->macro_dwo.matches (sectp->name))
11146 dw_sect = &dwo_sections->macro;
11147 else if (names->rnglists_dwo.matches (sectp->name))
11148 dw_sect = &dwo_sections->rnglists;
11149 else if (names->str_dwo.matches (sectp->name))
11150 dw_sect = &dwo_sections->str;
11151 else if (names->str_offsets_dwo.matches (sectp->name))
11152 dw_sect = &dwo_sections->str_offsets;
11153 else if (names->types_dwo.matches (sectp->name))
11154 {
11155 struct dwarf2_section_info type_section;
11156
11157 memset (&type_section, 0, sizeof (type_section));
11158 dwo_sections->types.push_back (type_section);
11159 dw_sect = &dwo_sections->types.back ();
11160 }
11161
11162 if (dw_sect != nullptr)
11163 {
11164 dw_sect->s.section = sectp;
11165 dw_sect->size = bfd_section_size (sectp);
11166 dw_sect->read (objfile);
11167 }
11168 }
11169
11170 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11171 by PER_CU. This is for the non-DWP case.
11172 The result is NULL if DWO_NAME can't be found. */
11173
11174 static struct dwo_file *
11175 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
11176 const char *comp_dir)
11177 {
11178 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11179
11180 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
11181 if (dbfd == NULL)
11182 {
11183 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
11184
11185 return NULL;
11186 }
11187
11188 dwo_file_up dwo_file (new struct dwo_file);
11189 dwo_file->dwo_name = dwo_name;
11190 dwo_file->comp_dir = comp_dir;
11191 dwo_file->dbfd = std::move (dbfd);
11192
11193 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
11194 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
11195 sec, &dwo_file->sections);
11196
11197 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
11198 dwo_file->cus);
11199
11200 if (cu->per_cu->dwarf_version < 5)
11201 {
11202 create_debug_types_hash_table (per_objfile, dwo_file.get (),
11203 dwo_file->sections.types, dwo_file->tus);
11204 }
11205 else
11206 {
11207 create_debug_type_hash_table (per_objfile, dwo_file.get (),
11208 &dwo_file->sections.info, dwo_file->tus,
11209 rcuh_kind::COMPILE);
11210 }
11211
11212 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
11213
11214 return dwo_file.release ();
11215 }
11216
11217 /* This function is mapped across the sections and remembers the offset and
11218 size of each of the DWP debugging sections common to version 1 and 2 that
11219 we are interested in. */
11220
11221 static void
11222 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
11223 asection *sectp, dwp_file *dwp_file)
11224 {
11225 const struct dwop_section_names *names = &dwop_section_names;
11226 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11227
11228 /* Record the ELF section number for later lookup: this is what the
11229 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11230 gdb_assert (elf_section_nr < dwp_file->num_sections);
11231 dwp_file->elf_sections[elf_section_nr] = sectp;
11232
11233 /* Look for specific sections that we need. */
11234 struct dwarf2_section_info *dw_sect = nullptr;
11235 if (names->str_dwo.matches (sectp->name))
11236 dw_sect = &dwp_file->sections.str;
11237 else if (names->cu_index.matches (sectp->name))
11238 dw_sect = &dwp_file->sections.cu_index;
11239 else if (names->tu_index.matches (sectp->name))
11240 dw_sect = &dwp_file->sections.tu_index;
11241
11242 if (dw_sect != nullptr)
11243 {
11244 dw_sect->s.section = sectp;
11245 dw_sect->size = bfd_section_size (sectp);
11246 dw_sect->read (objfile);
11247 }
11248 }
11249
11250 /* This function is mapped across the sections and remembers the offset and
11251 size of each of the DWP version 2 debugging sections that we are interested
11252 in. This is split into a separate function because we don't know if we
11253 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11254
11255 static void
11256 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
11257 asection *sectp, void *dwp_file_ptr)
11258 {
11259 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11260 const struct dwop_section_names *names = &dwop_section_names;
11261 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11262
11263 /* Record the ELF section number for later lookup: this is what the
11264 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11265 gdb_assert (elf_section_nr < dwp_file->num_sections);
11266 dwp_file->elf_sections[elf_section_nr] = sectp;
11267
11268 /* Look for specific sections that we need. */
11269 struct dwarf2_section_info *dw_sect = nullptr;
11270 if (names->abbrev_dwo.matches (sectp->name))
11271 dw_sect = &dwp_file->sections.abbrev;
11272 else if (names->info_dwo.matches (sectp->name))
11273 dw_sect = &dwp_file->sections.info;
11274 else if (names->line_dwo.matches (sectp->name))
11275 dw_sect = &dwp_file->sections.line;
11276 else if (names->loc_dwo.matches (sectp->name))
11277 dw_sect = &dwp_file->sections.loc;
11278 else if (names->macinfo_dwo.matches (sectp->name))
11279 dw_sect = &dwp_file->sections.macinfo;
11280 else if (names->macro_dwo.matches (sectp->name))
11281 dw_sect = &dwp_file->sections.macro;
11282 else if (names->str_offsets_dwo.matches (sectp->name))
11283 dw_sect = &dwp_file->sections.str_offsets;
11284 else if (names->types_dwo.matches (sectp->name))
11285 dw_sect = &dwp_file->sections.types;
11286
11287 if (dw_sect != nullptr)
11288 {
11289 dw_sect->s.section = sectp;
11290 dw_sect->size = bfd_section_size (sectp);
11291 dw_sect->read (objfile);
11292 }
11293 }
11294
11295 /* This function is mapped across the sections and remembers the offset and
11296 size of each of the DWP version 5 debugging sections that we are interested
11297 in. This is split into a separate function because we don't know if we
11298 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11299
11300 static void
11301 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
11302 asection *sectp, void *dwp_file_ptr)
11303 {
11304 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11305 const struct dwop_section_names *names = &dwop_section_names;
11306 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11307
11308 /* Record the ELF section number for later lookup: this is what the
11309 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11310 gdb_assert (elf_section_nr < dwp_file->num_sections);
11311 dwp_file->elf_sections[elf_section_nr] = sectp;
11312
11313 /* Look for specific sections that we need. */
11314 struct dwarf2_section_info *dw_sect = nullptr;
11315 if (names->abbrev_dwo.matches (sectp->name))
11316 dw_sect = &dwp_file->sections.abbrev;
11317 else if (names->info_dwo.matches (sectp->name))
11318 dw_sect = &dwp_file->sections.info;
11319 else if (names->line_dwo.matches (sectp->name))
11320 dw_sect = &dwp_file->sections.line;
11321 else if (names->loclists_dwo.matches (sectp->name))
11322 dw_sect = &dwp_file->sections.loclists;
11323 else if (names->macro_dwo.matches (sectp->name))
11324 dw_sect = &dwp_file->sections.macro;
11325 else if (names->rnglists_dwo.matches (sectp->name))
11326 dw_sect = &dwp_file->sections.rnglists;
11327 else if (names->str_offsets_dwo.matches (sectp->name))
11328 dw_sect = &dwp_file->sections.str_offsets;
11329
11330 if (dw_sect != nullptr)
11331 {
11332 dw_sect->s.section = sectp;
11333 dw_sect->size = bfd_section_size (sectp);
11334 dw_sect->read (objfile);
11335 }
11336 }
11337
11338 /* Hash function for dwp_file loaded CUs/TUs. */
11339
11340 static hashval_t
11341 hash_dwp_loaded_cutus (const void *item)
11342 {
11343 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11344
11345 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11346 return dwo_unit->signature;
11347 }
11348
11349 /* Equality function for dwp_file loaded CUs/TUs. */
11350
11351 static int
11352 eq_dwp_loaded_cutus (const void *a, const void *b)
11353 {
11354 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11355 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11356
11357 return dua->signature == dub->signature;
11358 }
11359
11360 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11361
11362 static htab_up
11363 allocate_dwp_loaded_cutus_table ()
11364 {
11365 return htab_up (htab_create_alloc (3,
11366 hash_dwp_loaded_cutus,
11367 eq_dwp_loaded_cutus,
11368 NULL, xcalloc, xfree));
11369 }
11370
11371 /* Try to open DWP file FILE_NAME.
11372 The result is the bfd handle of the file.
11373 If there is a problem finding or opening the file, return NULL.
11374 Upon success, the canonicalized path of the file is stored in the bfd,
11375 same as symfile_bfd_open. */
11376
11377 static gdb_bfd_ref_ptr
11378 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
11379 {
11380 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
11381 1 /*is_dwp*/,
11382 1 /*search_cwd*/));
11383 if (abfd != NULL)
11384 return abfd;
11385
11386 /* Work around upstream bug 15652.
11387 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11388 [Whether that's a "bug" is debatable, but it is getting in our way.]
11389 We have no real idea where the dwp file is, because gdb's realpath-ing
11390 of the executable's path may have discarded the needed info.
11391 [IWBN if the dwp file name was recorded in the executable, akin to
11392 .gnu_debuglink, but that doesn't exist yet.]
11393 Strip the directory from FILE_NAME and search again. */
11394 if (!debug_file_directory.empty ())
11395 {
11396 /* Don't implicitly search the current directory here.
11397 If the user wants to search "." to handle this case,
11398 it must be added to debug-file-directory. */
11399 return try_open_dwop_file (per_objfile, lbasename (file_name),
11400 1 /*is_dwp*/,
11401 0 /*search_cwd*/);
11402 }
11403
11404 return NULL;
11405 }
11406
11407 /* Initialize the use of the DWP file for the current objfile.
11408 By convention the name of the DWP file is ${objfile}.dwp.
11409 The result is NULL if it can't be found. */
11410
11411 static std::unique_ptr<struct dwp_file>
11412 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
11413 {
11414 struct objfile *objfile = per_objfile->objfile;
11415
11416 /* Try to find first .dwp for the binary file before any symbolic links
11417 resolving. */
11418
11419 /* If the objfile is a debug file, find the name of the real binary
11420 file and get the name of dwp file from there. */
11421 std::string dwp_name;
11422 if (objfile->separate_debug_objfile_backlink != NULL)
11423 {
11424 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11425 const char *backlink_basename = lbasename (backlink->original_name);
11426
11427 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11428 }
11429 else
11430 dwp_name = objfile->original_name;
11431
11432 dwp_name += ".dwp";
11433
11434 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
11435 if (dbfd == NULL
11436 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11437 {
11438 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11439 dwp_name = objfile_name (objfile);
11440 dwp_name += ".dwp";
11441 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
11442 }
11443
11444 if (dbfd == NULL)
11445 {
11446 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
11447
11448 return std::unique_ptr<dwp_file> ();
11449 }
11450
11451 const char *name = bfd_get_filename (dbfd.get ());
11452 std::unique_ptr<struct dwp_file> dwp_file
11453 (new struct dwp_file (name, std::move (dbfd)));
11454
11455 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
11456 dwp_file->elf_sections =
11457 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
11458 dwp_file->num_sections, asection *);
11459
11460 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11461 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11462 dwp_file.get ());
11463
11464 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
11465
11466 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
11467
11468 /* The DWP file version is stored in the hash table. Oh well. */
11469 if (dwp_file->cus && dwp_file->tus
11470 && dwp_file->cus->version != dwp_file->tus->version)
11471 {
11472 /* Technically speaking, we should try to limp along, but this is
11473 pretty bizarre. We use pulongest here because that's the established
11474 portability solution (e.g, we cannot use %u for uint32_t). */
11475 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11476 " TU version %s [in DWP file %s]"),
11477 pulongest (dwp_file->cus->version),
11478 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11479 }
11480
11481 if (dwp_file->cus)
11482 dwp_file->version = dwp_file->cus->version;
11483 else if (dwp_file->tus)
11484 dwp_file->version = dwp_file->tus->version;
11485 else
11486 dwp_file->version = 2;
11487
11488 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11489 {
11490 if (dwp_file->version == 2)
11491 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11492 dwp_file.get ());
11493 else
11494 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11495 dwp_file.get ());
11496 }
11497
11498 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
11499 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
11500
11501 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
11502 dwarf_read_debug_printf (" %s CUs, %s TUs",
11503 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11504 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11505
11506 return dwp_file;
11507 }
11508
11509 /* Wrapper around open_and_init_dwp_file, only open it once. */
11510
11511 static struct dwp_file *
11512 get_dwp_file (dwarf2_per_objfile *per_objfile)
11513 {
11514 if (!per_objfile->per_bfd->dwp_checked)
11515 {
11516 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
11517 per_objfile->per_bfd->dwp_checked = 1;
11518 }
11519 return per_objfile->per_bfd->dwp_file.get ();
11520 }
11521
11522 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11523 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11524 or in the DWP file for the objfile, referenced by THIS_UNIT.
11525 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11526 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11527
11528 This is called, for example, when wanting to read a variable with a
11529 complex location. Therefore we don't want to do file i/o for every call.
11530 Therefore we don't want to look for a DWO file on every call.
11531 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11532 then we check if we've already seen DWO_NAME, and only THEN do we check
11533 for a DWO file.
11534
11535 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11536 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11537
11538 static struct dwo_unit *
11539 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11540 ULONGEST signature, int is_debug_types)
11541 {
11542 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11543 struct objfile *objfile = per_objfile->objfile;
11544 const char *kind = is_debug_types ? "TU" : "CU";
11545 void **dwo_file_slot;
11546 struct dwo_file *dwo_file;
11547 struct dwp_file *dwp_file;
11548
11549 /* First see if there's a DWP file.
11550 If we have a DWP file but didn't find the DWO inside it, don't
11551 look for the original DWO file. It makes gdb behave differently
11552 depending on whether one is debugging in the build tree. */
11553
11554 dwp_file = get_dwp_file (per_objfile);
11555 if (dwp_file != NULL)
11556 {
11557 const struct dwp_hash_table *dwp_htab =
11558 is_debug_types ? dwp_file->tus : dwp_file->cus;
11559
11560 if (dwp_htab != NULL)
11561 {
11562 struct dwo_unit *dwo_cutu =
11563 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
11564 is_debug_types);
11565
11566 if (dwo_cutu != NULL)
11567 {
11568 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11569 kind, hex_string (signature),
11570 host_address_to_string (dwo_cutu));
11571
11572 return dwo_cutu;
11573 }
11574 }
11575 }
11576 else
11577 {
11578 /* No DWP file, look for the DWO file. */
11579
11580 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
11581 if (*dwo_file_slot == NULL)
11582 {
11583 /* Read in the file and build a table of the CUs/TUs it contains. */
11584 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
11585 }
11586 /* NOTE: This will be NULL if unable to open the file. */
11587 dwo_file = (struct dwo_file *) *dwo_file_slot;
11588
11589 if (dwo_file != NULL)
11590 {
11591 struct dwo_unit *dwo_cutu = NULL;
11592
11593 if (is_debug_types && dwo_file->tus)
11594 {
11595 struct dwo_unit find_dwo_cutu;
11596
11597 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11598 find_dwo_cutu.signature = signature;
11599 dwo_cutu
11600 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
11601 &find_dwo_cutu);
11602 }
11603 else if (!is_debug_types && dwo_file->cus)
11604 {
11605 struct dwo_unit find_dwo_cutu;
11606
11607 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11608 find_dwo_cutu.signature = signature;
11609 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
11610 &find_dwo_cutu);
11611 }
11612
11613 if (dwo_cutu != NULL)
11614 {
11615 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11616 kind, dwo_name, hex_string (signature),
11617 host_address_to_string (dwo_cutu));
11618
11619 return dwo_cutu;
11620 }
11621 }
11622 }
11623
11624 /* We didn't find it. This could mean a dwo_id mismatch, or
11625 someone deleted the DWO/DWP file, or the search path isn't set up
11626 correctly to find the file. */
11627
11628 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11629 kind, dwo_name, hex_string (signature));
11630
11631 /* This is a warning and not a complaint because it can be caused by
11632 pilot error (e.g., user accidentally deleting the DWO). */
11633 {
11634 /* Print the name of the DWP file if we looked there, helps the user
11635 better diagnose the problem. */
11636 std::string dwp_text;
11637
11638 if (dwp_file != NULL)
11639 dwp_text = string_printf (" [in DWP file %s]",
11640 lbasename (dwp_file->name));
11641
11642 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11643 " [in module %s]"),
11644 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
11645 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
11646 }
11647 return NULL;
11648 }
11649
11650 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11651 See lookup_dwo_cutu_unit for details. */
11652
11653 static struct dwo_unit *
11654 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11655 ULONGEST signature)
11656 {
11657 gdb_assert (!cu->per_cu->is_debug_types);
11658
11659 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
11660 }
11661
11662 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11663 See lookup_dwo_cutu_unit for details. */
11664
11665 static struct dwo_unit *
11666 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
11667 {
11668 gdb_assert (cu->per_cu->is_debug_types);
11669
11670 signatured_type *sig_type = (signatured_type *) cu->per_cu;
11671
11672 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
11673 }
11674
11675 /* Traversal function for queue_and_load_all_dwo_tus. */
11676
11677 static int
11678 queue_and_load_dwo_tu (void **slot, void *info)
11679 {
11680 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11681 dwarf2_cu *cu = (dwarf2_cu *) info;
11682 ULONGEST signature = dwo_unit->signature;
11683 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
11684
11685 if (sig_type != NULL)
11686 {
11687 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11688 a real dependency of PER_CU on SIG_TYPE. That is detected later
11689 while processing PER_CU. */
11690 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
11691 cu->per_cu->lang))
11692 load_full_type_unit (sig_type, cu->per_objfile);
11693 cu->per_cu->imported_symtabs_push (sig_type);
11694 }
11695
11696 return 1;
11697 }
11698
11699 /* Queue all TUs contained in the DWO of CU to be read in.
11700 The DWO may have the only definition of the type, though it may not be
11701 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11702 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11703
11704 static void
11705 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
11706 {
11707 struct dwo_unit *dwo_unit;
11708 struct dwo_file *dwo_file;
11709
11710 gdb_assert (cu != nullptr);
11711 gdb_assert (!cu->per_cu->is_debug_types);
11712 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
11713
11714 dwo_unit = cu->dwo_unit;
11715 gdb_assert (dwo_unit != NULL);
11716
11717 dwo_file = dwo_unit->dwo_file;
11718 if (dwo_file->tus != NULL)
11719 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
11720 }
11721
11722 /* Read in various DIEs. */
11723
11724 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11725 Inherit only the children of the DW_AT_abstract_origin DIE not being
11726 already referenced by DW_AT_abstract_origin from the children of the
11727 current DIE. */
11728
11729 static void
11730 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11731 {
11732 struct die_info *child_die;
11733 sect_offset *offsetp;
11734 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11735 struct die_info *origin_die;
11736 /* Iterator of the ORIGIN_DIE children. */
11737 struct die_info *origin_child_die;
11738 struct attribute *attr;
11739 struct dwarf2_cu *origin_cu;
11740 struct pending **origin_previous_list_in_scope;
11741
11742 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11743 if (!attr)
11744 return;
11745
11746 /* Note that following die references may follow to a die in a
11747 different cu. */
11748
11749 origin_cu = cu;
11750 origin_die = follow_die_ref (die, attr, &origin_cu);
11751
11752 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11753 symbols in. */
11754 origin_previous_list_in_scope = origin_cu->list_in_scope;
11755 origin_cu->list_in_scope = cu->list_in_scope;
11756
11757 if (die->tag != origin_die->tag
11758 && !(die->tag == DW_TAG_inlined_subroutine
11759 && origin_die->tag == DW_TAG_subprogram))
11760 complaint (_("DIE %s and its abstract origin %s have different tags"),
11761 sect_offset_str (die->sect_off),
11762 sect_offset_str (origin_die->sect_off));
11763
11764 /* Find if the concrete and abstract trees are structurally the
11765 same. This is a shallow traversal and it is not bullet-proof;
11766 the compiler can trick the debugger into believing that the trees
11767 are isomorphic, whereas they actually are not. However, the
11768 likelyhood of this happening is pretty low, and a full-fledged
11769 check would be an overkill. */
11770 bool are_isomorphic = true;
11771 die_info *concrete_child = die->child;
11772 die_info *abstract_child = origin_die->child;
11773 while (concrete_child != nullptr || abstract_child != nullptr)
11774 {
11775 if (concrete_child == nullptr
11776 || abstract_child == nullptr
11777 || concrete_child->tag != abstract_child->tag)
11778 {
11779 are_isomorphic = false;
11780 break;
11781 }
11782
11783 concrete_child = concrete_child->sibling;
11784 abstract_child = abstract_child->sibling;
11785 }
11786
11787 /* Walk the origin's children in parallel to the concrete children.
11788 This helps match an origin child in case the debug info misses
11789 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11790 origin tree may not have the same tree structure as the concrete
11791 DIE, though. */
11792 die_info *corresponding_abstract_child
11793 = are_isomorphic ? origin_die->child : nullptr;
11794
11795 std::vector<sect_offset> offsets;
11796
11797 for (child_die = die->child;
11798 child_die && child_die->tag;
11799 child_die = child_die->sibling)
11800 {
11801 struct die_info *child_origin_die;
11802 struct dwarf2_cu *child_origin_cu;
11803
11804 /* We are trying to process concrete instance entries:
11805 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11806 it's not relevant to our analysis here. i.e. detecting DIEs that are
11807 present in the abstract instance but not referenced in the concrete
11808 one. */
11809 if (child_die->tag == DW_TAG_call_site
11810 || child_die->tag == DW_TAG_GNU_call_site)
11811 {
11812 if (are_isomorphic)
11813 corresponding_abstract_child
11814 = corresponding_abstract_child->sibling;
11815 continue;
11816 }
11817
11818 /* For each CHILD_DIE, find the corresponding child of
11819 ORIGIN_DIE. If there is more than one layer of
11820 DW_AT_abstract_origin, follow them all; there shouldn't be,
11821 but GCC versions at least through 4.4 generate this (GCC PR
11822 40573). */
11823 child_origin_die = child_die;
11824 child_origin_cu = cu;
11825 while (1)
11826 {
11827 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11828 child_origin_cu);
11829 if (attr == NULL)
11830 break;
11831 child_origin_die = follow_die_ref (child_origin_die, attr,
11832 &child_origin_cu);
11833 }
11834
11835 /* If missing DW_AT_abstract_origin, try the corresponding child
11836 of the origin. Clang emits such lexical scopes. */
11837 if (child_origin_die == child_die
11838 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
11839 && are_isomorphic
11840 && child_die->tag == DW_TAG_lexical_block)
11841 child_origin_die = corresponding_abstract_child;
11842
11843 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11844 counterpart may exist. */
11845 if (child_origin_die != child_die)
11846 {
11847 if (child_die->tag != child_origin_die->tag
11848 && !(child_die->tag == DW_TAG_inlined_subroutine
11849 && child_origin_die->tag == DW_TAG_subprogram))
11850 complaint (_("Child DIE %s and its abstract origin %s have "
11851 "different tags"),
11852 sect_offset_str (child_die->sect_off),
11853 sect_offset_str (child_origin_die->sect_off));
11854 if (child_origin_die->parent != origin_die)
11855 complaint (_("Child DIE %s and its abstract origin %s have "
11856 "different parents"),
11857 sect_offset_str (child_die->sect_off),
11858 sect_offset_str (child_origin_die->sect_off));
11859 else
11860 offsets.push_back (child_origin_die->sect_off);
11861 }
11862
11863 if (are_isomorphic)
11864 corresponding_abstract_child = corresponding_abstract_child->sibling;
11865 }
11866 std::sort (offsets.begin (), offsets.end ());
11867 sect_offset *offsets_end = offsets.data () + offsets.size ();
11868 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
11869 if (offsetp[-1] == *offsetp)
11870 complaint (_("Multiple children of DIE %s refer "
11871 "to DIE %s as their abstract origin"),
11872 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
11873
11874 offsetp = offsets.data ();
11875 origin_child_die = origin_die->child;
11876 while (origin_child_die && origin_child_die->tag)
11877 {
11878 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11879 while (offsetp < offsets_end
11880 && *offsetp < origin_child_die->sect_off)
11881 offsetp++;
11882 if (offsetp >= offsets_end
11883 || *offsetp > origin_child_die->sect_off)
11884 {
11885 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11886 Check whether we're already processing ORIGIN_CHILD_DIE.
11887 This can happen with mutually referenced abstract_origins.
11888 PR 16581. */
11889 if (!origin_child_die->in_process)
11890 process_die (origin_child_die, origin_cu);
11891 }
11892 origin_child_die = origin_child_die->sibling;
11893 }
11894 origin_cu->list_in_scope = origin_previous_list_in_scope;
11895
11896 if (cu != origin_cu)
11897 compute_delayed_physnames (origin_cu);
11898 }
11899
11900 static void
11901 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11902 {
11903 struct objfile *objfile = cu->per_objfile->objfile;
11904 struct gdbarch *gdbarch = objfile->arch ();
11905 struct context_stack *newobj;
11906 CORE_ADDR lowpc;
11907 CORE_ADDR highpc;
11908 struct die_info *child_die;
11909 struct attribute *attr, *call_line, *call_file;
11910 const char *name;
11911 CORE_ADDR baseaddr;
11912 struct block *block;
11913 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11914 std::vector<struct symbol *> template_args;
11915 struct template_symbol *templ_func = NULL;
11916
11917 if (inlined_func)
11918 {
11919 /* If we do not have call site information, we can't show the
11920 caller of this inlined function. That's too confusing, so
11921 only use the scope for local variables. */
11922 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11923 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11924 if (call_line == NULL || call_file == NULL)
11925 {
11926 read_lexical_block_scope (die, cu);
11927 return;
11928 }
11929 }
11930
11931 baseaddr = objfile->text_section_offset ();
11932
11933 name = dwarf2_name (die, cu);
11934
11935 /* Ignore functions with missing or empty names. These are actually
11936 illegal according to the DWARF standard. */
11937 if (name == NULL)
11938 {
11939 complaint (_("missing name for subprogram DIE at %s"),
11940 sect_offset_str (die->sect_off));
11941 return;
11942 }
11943
11944 /* Ignore functions with missing or invalid low and high pc attributes. */
11945 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
11946 <= PC_BOUNDS_INVALID)
11947 {
11948 attr = dwarf2_attr (die, DW_AT_external, cu);
11949 if (attr == nullptr || !attr->as_boolean ())
11950 complaint (_("cannot get low and high bounds "
11951 "for subprogram DIE at %s"),
11952 sect_offset_str (die->sect_off));
11953 return;
11954 }
11955
11956 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11957 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11958
11959 /* If we have any template arguments, then we must allocate a
11960 different sort of symbol. */
11961 for (child_die = die->child; child_die; child_die = child_die->sibling)
11962 {
11963 if (child_die->tag == DW_TAG_template_type_param
11964 || child_die->tag == DW_TAG_template_value_param)
11965 {
11966 templ_func = new (&objfile->objfile_obstack) template_symbol;
11967 templ_func->subclass = SYMBOL_TEMPLATE;
11968 break;
11969 }
11970 }
11971
11972 gdb_assert (cu->get_builder () != nullptr);
11973 newobj = cu->get_builder ()->push_context (0, lowpc);
11974 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
11975 (struct symbol *) templ_func);
11976
11977 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
11978 set_objfile_main_name (objfile, newobj->name->linkage_name (),
11979 cu->per_cu->lang);
11980
11981 /* If there is a location expression for DW_AT_frame_base, record
11982 it. */
11983 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11984 if (attr != nullptr)
11985 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11986
11987 /* If there is a location for the static link, record it. */
11988 newobj->static_link = NULL;
11989 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11990 if (attr != nullptr)
11991 {
11992 newobj->static_link
11993 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11994 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
11995 cu->addr_type ());
11996 }
11997
11998 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
11999
12000 if (die->child != NULL)
12001 {
12002 child_die = die->child;
12003 while (child_die && child_die->tag)
12004 {
12005 if (child_die->tag == DW_TAG_template_type_param
12006 || child_die->tag == DW_TAG_template_value_param)
12007 {
12008 struct symbol *arg = new_symbol (child_die, NULL, cu);
12009
12010 if (arg != NULL)
12011 template_args.push_back (arg);
12012 }
12013 else
12014 process_die (child_die, cu);
12015 child_die = child_die->sibling;
12016 }
12017 }
12018
12019 inherit_abstract_dies (die, cu);
12020
12021 /* If we have a DW_AT_specification, we might need to import using
12022 directives from the context of the specification DIE. See the
12023 comment in determine_prefix. */
12024 if (cu->per_cu->lang == language_cplus
12025 && dwarf2_attr (die, DW_AT_specification, cu))
12026 {
12027 struct dwarf2_cu *spec_cu = cu;
12028 struct die_info *spec_die = die_specification (die, &spec_cu);
12029
12030 while (spec_die)
12031 {
12032 child_die = spec_die->child;
12033 while (child_die && child_die->tag)
12034 {
12035 if (child_die->tag == DW_TAG_imported_module)
12036 process_die (child_die, spec_cu);
12037 child_die = child_die->sibling;
12038 }
12039
12040 /* In some cases, GCC generates specification DIEs that
12041 themselves contain DW_AT_specification attributes. */
12042 spec_die = die_specification (spec_die, &spec_cu);
12043 }
12044 }
12045
12046 struct context_stack cstk = cu->get_builder ()->pop_context ();
12047 /* Make a block for the local symbols within. */
12048 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12049 cstk.static_link, lowpc, highpc);
12050
12051 /* For C++, set the block's scope. */
12052 if ((cu->per_cu->lang == language_cplus
12053 || cu->per_cu->lang == language_fortran
12054 || cu->per_cu->lang == language_d
12055 || cu->per_cu->lang == language_rust)
12056 && cu->processing_has_namespace_info)
12057 block_set_scope (block, determine_prefix (die, cu),
12058 &objfile->objfile_obstack);
12059
12060 /* If we have address ranges, record them. */
12061 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12062
12063 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12064
12065 /* Attach template arguments to function. */
12066 if (!template_args.empty ())
12067 {
12068 gdb_assert (templ_func != NULL);
12069
12070 templ_func->n_template_arguments = template_args.size ();
12071 templ_func->template_arguments
12072 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12073 templ_func->n_template_arguments);
12074 memcpy (templ_func->template_arguments,
12075 template_args.data (),
12076 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12077
12078 /* Make sure that the symtab is set on the new symbols. Even
12079 though they don't appear in this symtab directly, other parts
12080 of gdb assume that symbols do, and this is reasonably
12081 true. */
12082 for (symbol *sym : template_args)
12083 sym->set_symtab (templ_func->symtab ());
12084 }
12085
12086 /* In C++, we can have functions nested inside functions (e.g., when
12087 a function declares a class that has methods). This means that
12088 when we finish processing a function scope, we may need to go
12089 back to building a containing block's symbol lists. */
12090 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12091 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12092
12093 /* If we've finished processing a top-level function, subsequent
12094 symbols go in the file symbol list. */
12095 if (cu->get_builder ()->outermost_context_p ())
12096 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12097 }
12098
12099 /* Process all the DIES contained within a lexical block scope. Start
12100 a new scope, process the dies, and then close the scope. */
12101
12102 static void
12103 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12104 {
12105 struct objfile *objfile = cu->per_objfile->objfile;
12106 struct gdbarch *gdbarch = objfile->arch ();
12107 CORE_ADDR lowpc, highpc;
12108 struct die_info *child_die;
12109 CORE_ADDR baseaddr;
12110
12111 baseaddr = objfile->text_section_offset ();
12112
12113 /* Ignore blocks with missing or invalid low and high pc attributes. */
12114 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12115 as multiple lexical blocks? Handling children in a sane way would
12116 be nasty. Might be easier to properly extend generic blocks to
12117 describe ranges. */
12118 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
12119 {
12120 case PC_BOUNDS_NOT_PRESENT:
12121 /* DW_TAG_lexical_block has no attributes, process its children as if
12122 there was no wrapping by that DW_TAG_lexical_block.
12123 GCC does no longer produces such DWARF since GCC r224161. */
12124 for (child_die = die->child;
12125 child_die != NULL && child_die->tag;
12126 child_die = child_die->sibling)
12127 {
12128 /* We might already be processing this DIE. This can happen
12129 in an unusual circumstance -- where a subroutine A
12130 appears lexically in another subroutine B, but A actually
12131 inlines B. The recursion is broken here, rather than in
12132 inherit_abstract_dies, because it seems better to simply
12133 drop concrete children here. */
12134 if (!child_die->in_process)
12135 process_die (child_die, cu);
12136 }
12137 return;
12138 case PC_BOUNDS_INVALID:
12139 return;
12140 }
12141 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12142 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12143
12144 cu->get_builder ()->push_context (0, lowpc);
12145 if (die->child != NULL)
12146 {
12147 child_die = die->child;
12148 while (child_die && child_die->tag)
12149 {
12150 process_die (child_die, cu);
12151 child_die = child_die->sibling;
12152 }
12153 }
12154 inherit_abstract_dies (die, cu);
12155 struct context_stack cstk = cu->get_builder ()->pop_context ();
12156
12157 if (*cu->get_builder ()->get_local_symbols () != NULL
12158 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12159 {
12160 struct block *block
12161 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12162 cstk.start_addr, highpc);
12163
12164 /* Note that recording ranges after traversing children, as we
12165 do here, means that recording a parent's ranges entails
12166 walking across all its children's ranges as they appear in
12167 the address map, which is quadratic behavior.
12168
12169 It would be nicer to record the parent's ranges before
12170 traversing its children, simply overriding whatever you find
12171 there. But since we don't even decide whether to create a
12172 block until after we've traversed its children, that's hard
12173 to do. */
12174 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12175 }
12176 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12177 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12178 }
12179
12180 static void dwarf2_ranges_read_low_addrs (unsigned offset,
12181 struct dwarf2_cu *cu,
12182 dwarf_tag tag,
12183 std::vector<CORE_ADDR> &result);
12184
12185 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12186
12187 static void
12188 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12189 {
12190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12191 struct objfile *objfile = per_objfile->objfile;
12192 struct gdbarch *gdbarch = objfile->arch ();
12193 CORE_ADDR pc, baseaddr;
12194 struct attribute *attr;
12195 void **slot;
12196 int nparams;
12197 struct die_info *child_die;
12198
12199 baseaddr = objfile->text_section_offset ();
12200
12201 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12202 if (attr == NULL)
12203 {
12204 /* This was a pre-DWARF-5 GNU extension alias
12205 for DW_AT_call_return_pc. */
12206 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12207 }
12208 if (!attr)
12209 {
12210 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12211 "DIE %s [in module %s]"),
12212 sect_offset_str (die->sect_off), objfile_name (objfile));
12213 return;
12214 }
12215 pc = attr->as_address () + baseaddr;
12216 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12217 pc -= baseaddr;
12218
12219 if (cu->call_site_htab == NULL)
12220 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
12221 call_site::eq, NULL,
12222 &objfile->objfile_obstack,
12223 hashtab_obstack_allocate, NULL);
12224 struct call_site call_site_local (pc, nullptr, nullptr);
12225 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12226 if (*slot != NULL)
12227 {
12228 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12229 "DIE %s [in module %s]"),
12230 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
12231 objfile_name (objfile));
12232 return;
12233 }
12234
12235 /* Count parameters at the caller. */
12236
12237 nparams = 0;
12238 for (child_die = die->child; child_die && child_die->tag;
12239 child_die = child_die->sibling)
12240 {
12241 if (child_die->tag != DW_TAG_call_site_parameter
12242 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12243 {
12244 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12245 "DW_TAG_call_site child DIE %s [in module %s]"),
12246 child_die->tag, sect_offset_str (child_die->sect_off),
12247 objfile_name (objfile));
12248 continue;
12249 }
12250
12251 nparams++;
12252 }
12253
12254 struct call_site *call_site
12255 = new (XOBNEWVAR (&objfile->objfile_obstack,
12256 struct call_site,
12257 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
12258 struct call_site (pc, cu->per_cu, per_objfile);
12259 *slot = call_site;
12260
12261 /* We never call the destructor of call_site, so we must ensure it is
12262 trivially destructible. */
12263 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
12264
12265 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12266 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12267 {
12268 struct die_info *func_die;
12269
12270 /* Skip also over DW_TAG_inlined_subroutine. */
12271 for (func_die = die->parent;
12272 func_die && func_die->tag != DW_TAG_subprogram
12273 && func_die->tag != DW_TAG_subroutine_type;
12274 func_die = func_die->parent);
12275
12276 /* DW_AT_call_all_calls is a superset
12277 of DW_AT_call_all_tail_calls. */
12278 if (func_die
12279 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12280 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12281 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12282 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12283 {
12284 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12285 not complete. But keep CALL_SITE for look ups via call_site_htab,
12286 both the initial caller containing the real return address PC and
12287 the final callee containing the current PC of a chain of tail
12288 calls do not need to have the tail call list complete. But any
12289 function candidate for a virtual tail call frame searched via
12290 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12291 determined unambiguously. */
12292 }
12293 else
12294 {
12295 struct type *func_type = NULL;
12296
12297 if (func_die)
12298 func_type = get_die_type (func_die, cu);
12299 if (func_type != NULL)
12300 {
12301 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
12302
12303 /* Enlist this call site to the function. */
12304 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12305 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12306 }
12307 else
12308 complaint (_("Cannot find function owning DW_TAG_call_site "
12309 "DIE %s [in module %s]"),
12310 sect_offset_str (die->sect_off), objfile_name (objfile));
12311 }
12312 }
12313
12314 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12315 if (attr == NULL)
12316 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12317 if (attr == NULL)
12318 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12319 if (attr == NULL)
12320 {
12321 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12322 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12323 }
12324
12325 call_site->target.set_loc_dwarf_block (nullptr);
12326 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
12327 /* Keep NULL DWARF_BLOCK. */;
12328 else if (attr->form_is_block ())
12329 {
12330 struct dwarf2_locexpr_baton *dlbaton;
12331 struct dwarf_block *block = attr->as_block ();
12332
12333 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12334 dlbaton->data = block->data;
12335 dlbaton->size = block->size;
12336 dlbaton->per_objfile = per_objfile;
12337 dlbaton->per_cu = cu->per_cu;
12338
12339 call_site->target.set_loc_dwarf_block (dlbaton);
12340 }
12341 else if (attr->form_is_ref ())
12342 {
12343 struct dwarf2_cu *target_cu = cu;
12344 struct die_info *target_die;
12345
12346 target_die = follow_die_ref (die, attr, &target_cu);
12347 gdb_assert (target_cu->per_objfile->objfile == objfile);
12348
12349 struct attribute *ranges_attr
12350 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
12351
12352 if (die_is_declaration (target_die, target_cu))
12353 {
12354 const char *target_physname;
12355
12356 /* Prefer the mangled name; otherwise compute the demangled one. */
12357 target_physname = dw2_linkage_name (target_die, target_cu);
12358 if (target_physname == NULL)
12359 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12360 if (target_physname == NULL)
12361 complaint (_("DW_AT_call_target target DIE has invalid "
12362 "physname, for referencing DIE %s [in module %s]"),
12363 sect_offset_str (die->sect_off), objfile_name (objfile));
12364 else
12365 call_site->target.set_loc_physname (target_physname);
12366 }
12367 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
12368 {
12369 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
12370 + target_cu->gnu_ranges_base);
12371 std::vector<CORE_ADDR> addresses;
12372 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
12373 target_die->tag, addresses);
12374 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
12375 addresses.size ());
12376 std::copy (addresses.begin (), addresses.end (), saved);
12377 call_site->target.set_loc_array (addresses.size (), saved);
12378 }
12379 else
12380 {
12381 CORE_ADDR lowpc;
12382
12383 /* DW_AT_entry_pc should be preferred. */
12384 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
12385 nullptr, nullptr)
12386 <= PC_BOUNDS_INVALID)
12387 complaint (_("DW_AT_call_target target DIE has invalid "
12388 "low pc, for referencing DIE %s [in module %s]"),
12389 sect_offset_str (die->sect_off), objfile_name (objfile));
12390 else
12391 {
12392 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
12393 - baseaddr);
12394 call_site->target.set_loc_physaddr (lowpc);
12395 }
12396 }
12397 }
12398 else
12399 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12400 "block nor reference, for DIE %s [in module %s]"),
12401 sect_offset_str (die->sect_off), objfile_name (objfile));
12402
12403 for (child_die = die->child;
12404 child_die && child_die->tag;
12405 child_die = child_die->sibling)
12406 {
12407 struct call_site_parameter *parameter;
12408 struct attribute *loc, *origin;
12409
12410 if (child_die->tag != DW_TAG_call_site_parameter
12411 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12412 {
12413 /* Already printed the complaint above. */
12414 continue;
12415 }
12416
12417 gdb_assert (call_site->parameter_count < nparams);
12418 parameter = &call_site->parameter[call_site->parameter_count];
12419
12420 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12421 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12422 register is contained in DW_AT_call_value. */
12423
12424 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12425 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12426 if (origin == NULL)
12427 {
12428 /* This was a pre-DWARF-5 GNU extension alias
12429 for DW_AT_call_parameter. */
12430 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12431 }
12432 if (loc == NULL && origin != NULL && origin->form_is_ref ())
12433 {
12434 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12435
12436 sect_offset sect_off = origin->get_ref_die_offset ();
12437 if (!cu->header.offset_in_cu_p (sect_off))
12438 {
12439 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12440 binding can be done only inside one CU. Such referenced DIE
12441 therefore cannot be even moved to DW_TAG_partial_unit. */
12442 complaint (_("DW_AT_call_parameter offset is not in CU for "
12443 "DW_TAG_call_site child DIE %s [in module %s]"),
12444 sect_offset_str (child_die->sect_off),
12445 objfile_name (objfile));
12446 continue;
12447 }
12448 parameter->u.param_cu_off
12449 = (cu_offset) (sect_off - cu->header.sect_off);
12450 }
12451 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
12452 {
12453 complaint (_("No DW_FORM_block* DW_AT_location for "
12454 "DW_TAG_call_site child DIE %s [in module %s]"),
12455 sect_offset_str (child_die->sect_off), objfile_name (objfile));
12456 continue;
12457 }
12458 else
12459 {
12460 struct dwarf_block *block = loc->as_block ();
12461
12462 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12463 (block->data, &block->data[block->size]);
12464 if (parameter->u.dwarf_reg != -1)
12465 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12466 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
12467 &block->data[block->size],
12468 &parameter->u.fb_offset))
12469 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12470 else
12471 {
12472 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12473 "for DW_FORM_block* DW_AT_location is supported for "
12474 "DW_TAG_call_site child DIE %s "
12475 "[in module %s]"),
12476 sect_offset_str (child_die->sect_off),
12477 objfile_name (objfile));
12478 continue;
12479 }
12480 }
12481
12482 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12483 if (attr == NULL)
12484 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12485 if (attr == NULL || !attr->form_is_block ())
12486 {
12487 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12488 "DW_TAG_call_site child DIE %s [in module %s]"),
12489 sect_offset_str (child_die->sect_off),
12490 objfile_name (objfile));
12491 continue;
12492 }
12493
12494 struct dwarf_block *block = attr->as_block ();
12495 parameter->value = block->data;
12496 parameter->value_size = block->size;
12497
12498 /* Parameters are not pre-cleared by memset above. */
12499 parameter->data_value = NULL;
12500 parameter->data_value_size = 0;
12501 call_site->parameter_count++;
12502
12503 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12504 if (attr == NULL)
12505 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12506 if (attr != nullptr)
12507 {
12508 if (!attr->form_is_block ())
12509 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12510 "DW_TAG_call_site child DIE %s [in module %s]"),
12511 sect_offset_str (child_die->sect_off),
12512 objfile_name (objfile));
12513 else
12514 {
12515 block = attr->as_block ();
12516 parameter->data_value = block->data;
12517 parameter->data_value_size = block->size;
12518 }
12519 }
12520 }
12521 }
12522
12523 /* Helper function for read_variable. If DIE represents a virtual
12524 table, then return the type of the concrete object that is
12525 associated with the virtual table. Otherwise, return NULL. */
12526
12527 static struct type *
12528 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12529 {
12530 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12531 if (attr == NULL)
12532 return NULL;
12533
12534 /* Find the type DIE. */
12535 struct die_info *type_die = NULL;
12536 struct dwarf2_cu *type_cu = cu;
12537
12538 if (attr->form_is_ref ())
12539 type_die = follow_die_ref (die, attr, &type_cu);
12540 if (type_die == NULL)
12541 return NULL;
12542
12543 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12544 return NULL;
12545 return die_containing_type (type_die, type_cu);
12546 }
12547
12548 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12549
12550 static void
12551 read_variable (struct die_info *die, struct dwarf2_cu *cu)
12552 {
12553 struct rust_vtable_symbol *storage = NULL;
12554
12555 if (cu->per_cu->lang == language_rust)
12556 {
12557 struct type *containing_type = rust_containing_type (die, cu);
12558
12559 if (containing_type != NULL)
12560 {
12561 struct objfile *objfile = cu->per_objfile->objfile;
12562
12563 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
12564 storage->concrete_type = containing_type;
12565 storage->subclass = SYMBOL_RUST_VTABLE;
12566 }
12567 }
12568
12569 struct symbol *res = new_symbol (die, NULL, cu, storage);
12570 struct attribute *abstract_origin
12571 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12572 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
12573 if (res == NULL && loc && abstract_origin)
12574 {
12575 /* We have a variable without a name, but with a location and an abstract
12576 origin. This may be a concrete instance of an abstract variable
12577 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12578 later. */
12579 struct dwarf2_cu *origin_cu = cu;
12580 struct die_info *origin_die
12581 = follow_die_ref (die, abstract_origin, &origin_cu);
12582 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12583 per_objfile->per_bfd->abstract_to_concrete
12584 [origin_die->sect_off].push_back (die->sect_off);
12585 }
12586 }
12587
12588 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12589 reading .debug_rnglists.
12590 Callback's type should be:
12591 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12592 Return true if the attributes are present and valid, otherwise,
12593 return false. */
12594
12595 template <typename Callback>
12596 static bool
12597 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12598 dwarf_tag tag, Callback &&callback)
12599 {
12600 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12601 struct objfile *objfile = per_objfile->objfile;
12602 bfd *obfd = objfile->obfd;
12603 /* Base address selection entry. */
12604 gdb::optional<CORE_ADDR> base;
12605 const gdb_byte *buffer;
12606 bool overflow = false;
12607 ULONGEST addr_index;
12608 struct dwarf2_section_info *rnglists_section;
12609
12610 base = cu->base_address;
12611 rnglists_section = cu_debug_rnglists_section (cu, tag);
12612 rnglists_section->read (objfile);
12613
12614 if (offset >= rnglists_section->size)
12615 {
12616 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12617 offset);
12618 return false;
12619 }
12620 buffer = rnglists_section->buffer + offset;
12621
12622 while (1)
12623 {
12624 /* Initialize it due to a false compiler warning. */
12625 CORE_ADDR range_beginning = 0, range_end = 0;
12626 const gdb_byte *buf_end = (rnglists_section->buffer
12627 + rnglists_section->size);
12628 unsigned int bytes_read;
12629
12630 if (buffer == buf_end)
12631 {
12632 overflow = true;
12633 break;
12634 }
12635 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12636 switch (rlet)
12637 {
12638 case DW_RLE_end_of_list:
12639 break;
12640 case DW_RLE_base_address:
12641 if (buffer + cu->header.addr_size > buf_end)
12642 {
12643 overflow = true;
12644 break;
12645 }
12646 base = cu->header.read_address (obfd, buffer, &bytes_read);
12647 buffer += bytes_read;
12648 break;
12649 case DW_RLE_base_addressx:
12650 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12651 buffer += bytes_read;
12652 base = read_addr_index (cu, addr_index);
12653 break;
12654 case DW_RLE_start_length:
12655 if (buffer + cu->header.addr_size > buf_end)
12656 {
12657 overflow = true;
12658 break;
12659 }
12660 range_beginning = cu->header.read_address (obfd, buffer,
12661 &bytes_read);
12662 buffer += bytes_read;
12663 range_end = (range_beginning
12664 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12665 buffer += bytes_read;
12666 if (buffer > buf_end)
12667 {
12668 overflow = true;
12669 break;
12670 }
12671 break;
12672 case DW_RLE_startx_length:
12673 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12674 buffer += bytes_read;
12675 range_beginning = read_addr_index (cu, addr_index);
12676 if (buffer > buf_end)
12677 {
12678 overflow = true;
12679 break;
12680 }
12681 range_end = (range_beginning
12682 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12683 buffer += bytes_read;
12684 break;
12685 case DW_RLE_offset_pair:
12686 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12687 buffer += bytes_read;
12688 if (buffer > buf_end)
12689 {
12690 overflow = true;
12691 break;
12692 }
12693 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12694 buffer += bytes_read;
12695 if (buffer > buf_end)
12696 {
12697 overflow = true;
12698 break;
12699 }
12700 break;
12701 case DW_RLE_start_end:
12702 if (buffer + 2 * cu->header.addr_size > buf_end)
12703 {
12704 overflow = true;
12705 break;
12706 }
12707 range_beginning = cu->header.read_address (obfd, buffer,
12708 &bytes_read);
12709 buffer += bytes_read;
12710 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
12711 buffer += bytes_read;
12712 break;
12713 case DW_RLE_startx_endx:
12714 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12715 buffer += bytes_read;
12716 range_beginning = read_addr_index (cu, addr_index);
12717 if (buffer > buf_end)
12718 {
12719 overflow = true;
12720 break;
12721 }
12722 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12723 buffer += bytes_read;
12724 range_end = read_addr_index (cu, addr_index);
12725 break;
12726 default:
12727 complaint (_("Invalid .debug_rnglists data (no base address)"));
12728 return false;
12729 }
12730 if (rlet == DW_RLE_end_of_list || overflow)
12731 break;
12732 if (rlet == DW_RLE_base_address)
12733 continue;
12734
12735 if (range_beginning > range_end)
12736 {
12737 /* Inverted range entries are invalid. */
12738 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12739 return false;
12740 }
12741
12742 /* Empty range entries have no effect. */
12743 if (range_beginning == range_end)
12744 continue;
12745
12746 /* Only DW_RLE_offset_pair needs the base address added. */
12747 if (rlet == DW_RLE_offset_pair)
12748 {
12749 if (!base.has_value ())
12750 {
12751 /* We have no valid base address for the DW_RLE_offset_pair. */
12752 complaint (_("Invalid .debug_rnglists data (no base address for "
12753 "DW_RLE_offset_pair)"));
12754 return false;
12755 }
12756
12757 range_beginning += *base;
12758 range_end += *base;
12759 }
12760
12761 /* A not-uncommon case of bad debug info.
12762 Don't pollute the addrmap with bad data. */
12763 if (range_beginning == 0
12764 && !per_objfile->per_bfd->has_section_at_zero)
12765 {
12766 complaint (_(".debug_rnglists entry has start address of zero"
12767 " [in module %s]"), objfile_name (objfile));
12768 continue;
12769 }
12770
12771 callback (range_beginning, range_end);
12772 }
12773
12774 if (overflow)
12775 {
12776 complaint (_("Offset %d is not terminated "
12777 "for DW_AT_ranges attribute"),
12778 offset);
12779 return false;
12780 }
12781
12782 return true;
12783 }
12784
12785 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12786 Callback's type should be:
12787 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12788 Return 1 if the attributes are present and valid, otherwise, return 0. */
12789
12790 template <typename Callback>
12791 static int
12792 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
12793 Callback &&callback)
12794 {
12795 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12796 struct objfile *objfile = per_objfile->objfile;
12797 struct comp_unit_head *cu_header = &cu->header;
12798 bfd *obfd = objfile->obfd;
12799 unsigned int addr_size = cu_header->addr_size;
12800 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12801 /* Base address selection entry. */
12802 gdb::optional<CORE_ADDR> base;
12803 unsigned int dummy;
12804 const gdb_byte *buffer;
12805
12806 if (cu_header->version >= 5)
12807 return dwarf2_rnglists_process (offset, cu, tag, callback);
12808
12809 base = cu->base_address;
12810
12811 per_objfile->per_bfd->ranges.read (objfile);
12812 if (offset >= per_objfile->per_bfd->ranges.size)
12813 {
12814 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12815 offset);
12816 return 0;
12817 }
12818 buffer = per_objfile->per_bfd->ranges.buffer + offset;
12819
12820 while (1)
12821 {
12822 CORE_ADDR range_beginning, range_end;
12823
12824 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
12825 buffer += addr_size;
12826 range_end = cu->header.read_address (obfd, buffer, &dummy);
12827 buffer += addr_size;
12828 offset += 2 * addr_size;
12829
12830 /* An end of list marker is a pair of zero addresses. */
12831 if (range_beginning == 0 && range_end == 0)
12832 /* Found the end of list entry. */
12833 break;
12834
12835 /* Each base address selection entry is a pair of 2 values.
12836 The first is the largest possible address, the second is
12837 the base address. Check for a base address here. */
12838 if ((range_beginning & mask) == mask)
12839 {
12840 /* If we found the largest possible address, then we already
12841 have the base address in range_end. */
12842 base = range_end;
12843 continue;
12844 }
12845
12846 if (!base.has_value ())
12847 {
12848 /* We have no valid base address for the ranges
12849 data. */
12850 complaint (_("Invalid .debug_ranges data (no base address)"));
12851 return 0;
12852 }
12853
12854 if (range_beginning > range_end)
12855 {
12856 /* Inverted range entries are invalid. */
12857 complaint (_("Invalid .debug_ranges data (inverted range)"));
12858 return 0;
12859 }
12860
12861 /* Empty range entries have no effect. */
12862 if (range_beginning == range_end)
12863 continue;
12864
12865 range_beginning += *base;
12866 range_end += *base;
12867
12868 /* A not-uncommon case of bad debug info.
12869 Don't pollute the addrmap with bad data. */
12870 if (range_beginning == 0
12871 && !per_objfile->per_bfd->has_section_at_zero)
12872 {
12873 complaint (_(".debug_ranges entry has start address of zero"
12874 " [in module %s]"), objfile_name (objfile));
12875 continue;
12876 }
12877
12878 callback (range_beginning, range_end);
12879 }
12880
12881 return 1;
12882 }
12883
12884 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12885 Return 1 if the attributes are present and valid, otherwise, return 0.
12886 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12887 ranges in MAP are set, using DATUM as the value. */
12888
12889 static int
12890 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12891 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12892 addrmap *map, void *datum, dwarf_tag tag)
12893 {
12894 struct objfile *objfile = cu->per_objfile->objfile;
12895 struct gdbarch *gdbarch = objfile->arch ();
12896 const CORE_ADDR baseaddr = objfile->text_section_offset ();
12897 int low_set = 0;
12898 CORE_ADDR low = 0;
12899 CORE_ADDR high = 0;
12900 int retval;
12901
12902 retval = dwarf2_ranges_process (offset, cu, tag,
12903 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12904 {
12905 if (map != nullptr)
12906 {
12907 CORE_ADDR lowpc;
12908 CORE_ADDR highpc;
12909
12910 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
12911 range_beginning + baseaddr)
12912 - baseaddr);
12913 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
12914 range_end + baseaddr)
12915 - baseaddr);
12916 addrmap_set_empty (map, lowpc, highpc - 1, datum);
12917 }
12918
12919 /* FIXME: This is recording everything as a low-high
12920 segment of consecutive addresses. We should have a
12921 data structure for discontiguous block ranges
12922 instead. */
12923 if (! low_set)
12924 {
12925 low = range_beginning;
12926 high = range_end;
12927 low_set = 1;
12928 }
12929 else
12930 {
12931 if (range_beginning < low)
12932 low = range_beginning;
12933 if (range_end > high)
12934 high = range_end;
12935 }
12936 });
12937 if (!retval)
12938 return 0;
12939
12940 if (! low_set)
12941 /* If the first entry is an end-of-list marker, the range
12942 describes an empty scope, i.e. no instructions. */
12943 return 0;
12944
12945 if (low_return)
12946 *low_return = low;
12947 if (high_return)
12948 *high_return = high;
12949 return 1;
12950 }
12951
12952 /* Process ranges and fill in a vector of the low PC values only. */
12953
12954 static void
12955 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
12956 dwarf_tag tag,
12957 std::vector<CORE_ADDR> &result)
12958 {
12959 dwarf2_ranges_process (offset, cu, tag,
12960 [&] (CORE_ADDR start, CORE_ADDR end)
12961 {
12962 result.push_back (start);
12963 });
12964 }
12965
12966 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12967 definition for the return value. *LOWPC and *HIGHPC are set iff
12968 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12969
12970 static enum pc_bounds_kind
12971 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12972 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12973 addrmap *map, void *datum)
12974 {
12975 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12976 struct attribute *attr;
12977 struct attribute *attr_high;
12978 CORE_ADDR low = 0;
12979 CORE_ADDR high = 0;
12980 enum pc_bounds_kind ret;
12981
12982 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12983 if (attr_high)
12984 {
12985 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12986 if (attr != nullptr)
12987 {
12988 low = attr->as_address ();
12989 high = attr_high->as_address ();
12990 if (cu->header.version >= 4 && attr_high->form_is_constant ())
12991 high += low;
12992 }
12993 else
12994 /* Found high w/o low attribute. */
12995 return PC_BOUNDS_INVALID;
12996
12997 /* Found consecutive range of addresses. */
12998 ret = PC_BOUNDS_HIGH_LOW;
12999 }
13000 else
13001 {
13002 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13003 if (attr != nullptr && attr->form_is_unsigned ())
13004 {
13005 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13006 on DWARF version). */
13007 ULONGEST ranges_offset = attr->as_unsigned ();
13008
13009 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13010 this value. */
13011 if (die->tag != DW_TAG_compile_unit)
13012 ranges_offset += cu->gnu_ranges_base;
13013
13014 /* Value of the DW_AT_ranges attribute is the offset in the
13015 .debug_ranges section. */
13016 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
13017 map, datum, die->tag))
13018 return PC_BOUNDS_INVALID;
13019 /* Found discontinuous range of addresses. */
13020 ret = PC_BOUNDS_RANGES;
13021 }
13022 else
13023 return PC_BOUNDS_NOT_PRESENT;
13024 }
13025
13026 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13027 if (high <= low)
13028 return PC_BOUNDS_INVALID;
13029
13030 /* When using the GNU linker, .gnu.linkonce. sections are used to
13031 eliminate duplicate copies of functions and vtables and such.
13032 The linker will arbitrarily choose one and discard the others.
13033 The AT_*_pc values for such functions refer to local labels in
13034 these sections. If the section from that file was discarded, the
13035 labels are not in the output, so the relocs get a value of 0.
13036 If this is a discarded function, mark the pc bounds as invalid,
13037 so that GDB will ignore it. */
13038 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
13039 return PC_BOUNDS_INVALID;
13040
13041 *lowpc = low;
13042 if (highpc)
13043 *highpc = high;
13044 return ret;
13045 }
13046
13047 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13048 its low and high PC addresses. Do nothing if these addresses could not
13049 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13050 and HIGHPC to the high address if greater than HIGHPC. */
13051
13052 static void
13053 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13054 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13055 struct dwarf2_cu *cu)
13056 {
13057 CORE_ADDR low, high;
13058 struct die_info *child = die->child;
13059
13060 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
13061 >= PC_BOUNDS_RANGES)
13062 {
13063 *lowpc = std::min (*lowpc, low);
13064 *highpc = std::max (*highpc, high);
13065 }
13066
13067 /* If the language does not allow nested subprograms (either inside
13068 subprograms or lexical blocks), we're done. */
13069 if (cu->per_cu->lang != language_ada)
13070 return;
13071
13072 /* Check all the children of the given DIE. If it contains nested
13073 subprograms, then check their pc bounds. Likewise, we need to
13074 check lexical blocks as well, as they may also contain subprogram
13075 definitions. */
13076 while (child && child->tag)
13077 {
13078 if (child->tag == DW_TAG_subprogram
13079 || child->tag == DW_TAG_lexical_block)
13080 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13081 child = child->sibling;
13082 }
13083 }
13084
13085 /* Get the low and high pc's represented by the scope DIE, and store
13086 them in *LOWPC and *HIGHPC. If the correct values can't be
13087 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13088
13089 static void
13090 get_scope_pc_bounds (struct die_info *die,
13091 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13092 struct dwarf2_cu *cu)
13093 {
13094 CORE_ADDR best_low = (CORE_ADDR) -1;
13095 CORE_ADDR best_high = (CORE_ADDR) 0;
13096 CORE_ADDR current_low, current_high;
13097
13098 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
13099 nullptr, nullptr)
13100 >= PC_BOUNDS_RANGES)
13101 {
13102 best_low = current_low;
13103 best_high = current_high;
13104 }
13105 else
13106 {
13107 struct die_info *child = die->child;
13108
13109 while (child && child->tag)
13110 {
13111 switch (child->tag) {
13112 case DW_TAG_subprogram:
13113 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13114 break;
13115 case DW_TAG_namespace:
13116 case DW_TAG_module:
13117 /* FIXME: carlton/2004-01-16: Should we do this for
13118 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13119 that current GCC's always emit the DIEs corresponding
13120 to definitions of methods of classes as children of a
13121 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13122 the DIEs giving the declarations, which could be
13123 anywhere). But I don't see any reason why the
13124 standards says that they have to be there. */
13125 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13126
13127 if (current_low != ((CORE_ADDR) -1))
13128 {
13129 best_low = std::min (best_low, current_low);
13130 best_high = std::max (best_high, current_high);
13131 }
13132 break;
13133 default:
13134 /* Ignore. */
13135 break;
13136 }
13137
13138 child = child->sibling;
13139 }
13140 }
13141
13142 *lowpc = best_low;
13143 *highpc = best_high;
13144 }
13145
13146 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13147 in DIE. */
13148
13149 static void
13150 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13151 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13152 {
13153 struct objfile *objfile = cu->per_objfile->objfile;
13154 struct gdbarch *gdbarch = objfile->arch ();
13155 struct attribute *attr;
13156 struct attribute *attr_high;
13157
13158 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13159 if (attr_high)
13160 {
13161 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13162 if (attr != nullptr)
13163 {
13164 CORE_ADDR low = attr->as_address ();
13165 CORE_ADDR high = attr_high->as_address ();
13166
13167 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13168 high += low;
13169
13170 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13171 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13172 cu->get_builder ()->record_block_range (block, low, high - 1);
13173 }
13174 }
13175
13176 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13177 if (attr != nullptr && attr->form_is_unsigned ())
13178 {
13179 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13180 on DWARF version). */
13181 ULONGEST ranges_offset = attr->as_unsigned ();
13182
13183 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13184 this value. */
13185 if (die->tag != DW_TAG_compile_unit)
13186 ranges_offset += cu->gnu_ranges_base;
13187
13188 std::vector<blockrange> blockvec;
13189 dwarf2_ranges_process (ranges_offset, cu, die->tag,
13190 [&] (CORE_ADDR start, CORE_ADDR end)
13191 {
13192 start += baseaddr;
13193 end += baseaddr;
13194 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13195 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13196 cu->get_builder ()->record_block_range (block, start, end - 1);
13197 blockvec.emplace_back (start, end);
13198 });
13199
13200 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13201 }
13202 }
13203
13204 /* Check whether the producer field indicates either of GCC < 4.6, or the
13205 Intel C/C++ compiler, and cache the result in CU. */
13206
13207 static void
13208 check_producer (struct dwarf2_cu *cu)
13209 {
13210 int major, minor;
13211
13212 if (cu->producer == NULL)
13213 {
13214 /* For unknown compilers expect their behavior is DWARF version
13215 compliant.
13216
13217 GCC started to support .debug_types sections by -gdwarf-4 since
13218 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13219 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13220 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13221 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13222 }
13223 else if (producer_is_gcc (cu->producer, &major, &minor))
13224 {
13225 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13226 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13227 cu->producer_is_gcc_11 = major == 11;
13228 }
13229 else if (producer_is_icc (cu->producer, &major, &minor))
13230 {
13231 cu->producer_is_icc = true;
13232 cu->producer_is_icc_lt_14 = major < 14;
13233 }
13234 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13235 cu->producer_is_codewarrior = true;
13236 else
13237 {
13238 /* For other non-GCC compilers, expect their behavior is DWARF version
13239 compliant. */
13240 }
13241
13242 cu->checked_producer = true;
13243 }
13244
13245 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13246 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13247 during 4.6.0 experimental. */
13248
13249 static bool
13250 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13251 {
13252 if (!cu->checked_producer)
13253 check_producer (cu);
13254
13255 return cu->producer_is_gxx_lt_4_6;
13256 }
13257
13258
13259 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13260 with incorrect is_stmt attributes. */
13261
13262 static bool
13263 producer_is_codewarrior (struct dwarf2_cu *cu)
13264 {
13265 if (!cu->checked_producer)
13266 check_producer (cu);
13267
13268 return cu->producer_is_codewarrior;
13269 }
13270
13271 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13272 If that attribute is not available, return the appropriate
13273 default. */
13274
13275 static enum dwarf_access_attribute
13276 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13277 {
13278 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13279 if (attr != nullptr)
13280 {
13281 LONGEST value = attr->constant_value (-1);
13282 if (value == DW_ACCESS_public
13283 || value == DW_ACCESS_protected
13284 || value == DW_ACCESS_private)
13285 return (dwarf_access_attribute) value;
13286 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13287 plongest (value));
13288 }
13289
13290 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13291 {
13292 /* The default DWARF 2 accessibility for members is public, the default
13293 accessibility for inheritance is private. */
13294
13295 if (die->tag != DW_TAG_inheritance)
13296 return DW_ACCESS_public;
13297 else
13298 return DW_ACCESS_private;
13299 }
13300 else
13301 {
13302 /* DWARF 3+ defines the default accessibility a different way. The same
13303 rules apply now for DW_TAG_inheritance as for the members and it only
13304 depends on the container kind. */
13305
13306 if (die->parent->tag == DW_TAG_class_type)
13307 return DW_ACCESS_private;
13308 else
13309 return DW_ACCESS_public;
13310 }
13311 }
13312
13313 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13314 *OFFSET to the byte offset. If the attribute was not found return
13315 0, otherwise return 1. If it was found but could not properly be
13316 handled, set *OFFSET to 0. */
13317
13318 static int
13319 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13320 LONGEST *offset)
13321 {
13322 struct attribute *attr;
13323
13324 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13325 if (attr != NULL)
13326 {
13327 *offset = 0;
13328
13329 /* Note that we do not check for a section offset first here.
13330 This is because DW_AT_data_member_location is new in DWARF 4,
13331 so if we see it, we can assume that a constant form is really
13332 a constant and not a section offset. */
13333 if (attr->form_is_constant ())
13334 *offset = attr->constant_value (0);
13335 else if (attr->form_is_section_offset ())
13336 dwarf2_complex_location_expr_complaint ();
13337 else if (attr->form_is_block ())
13338 *offset = decode_locdesc (attr->as_block (), cu);
13339 else
13340 dwarf2_complex_location_expr_complaint ();
13341
13342 return 1;
13343 }
13344 else
13345 {
13346 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13347 if (attr != nullptr)
13348 {
13349 *offset = attr->constant_value (0);
13350 return 1;
13351 }
13352 }
13353
13354 return 0;
13355 }
13356
13357 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13358 store the results in FIELD. */
13359
13360 static void
13361 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13362 struct field *field)
13363 {
13364 struct attribute *attr;
13365
13366 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13367 if (attr != NULL)
13368 {
13369 if (attr->form_is_constant ())
13370 {
13371 LONGEST offset = attr->constant_value (0);
13372
13373 /* Work around this GCC 11 bug, where it would erroneously use -1
13374 data member locations, instead of 0:
13375
13376 Negative DW_AT_data_member_location
13377 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13378 */
13379 if (offset == -1 && cu->producer_is_gcc_11)
13380 {
13381 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13382 offset = 0;
13383 }
13384
13385 field->set_loc_bitpos (offset * bits_per_byte);
13386 }
13387 else if (attr->form_is_section_offset ())
13388 dwarf2_complex_location_expr_complaint ();
13389 else if (attr->form_is_block ())
13390 {
13391 bool handled;
13392 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
13393 if (handled)
13394 field->set_loc_bitpos (offset * bits_per_byte);
13395 else
13396 {
13397 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13398 struct objfile *objfile = per_objfile->objfile;
13399 struct dwarf2_locexpr_baton *dlbaton
13400 = XOBNEW (&objfile->objfile_obstack,
13401 struct dwarf2_locexpr_baton);
13402 dlbaton->data = attr->as_block ()->data;
13403 dlbaton->size = attr->as_block ()->size;
13404 /* When using this baton, we want to compute the address
13405 of the field, not the value. This is why
13406 is_reference is set to false here. */
13407 dlbaton->is_reference = false;
13408 dlbaton->per_objfile = per_objfile;
13409 dlbaton->per_cu = cu->per_cu;
13410
13411 field->set_loc_dwarf_block (dlbaton);
13412 }
13413 }
13414 else
13415 dwarf2_complex_location_expr_complaint ();
13416 }
13417 else
13418 {
13419 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13420 if (attr != nullptr)
13421 field->set_loc_bitpos (attr->constant_value (0));
13422 }
13423 }
13424
13425 /* Add an aggregate field to the field list. */
13426
13427 static void
13428 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13429 struct dwarf2_cu *cu)
13430 {
13431 struct objfile *objfile = cu->per_objfile->objfile;
13432 struct gdbarch *gdbarch = objfile->arch ();
13433 struct nextfield *new_field;
13434 struct attribute *attr;
13435 struct field *fp;
13436 const char *fieldname = "";
13437
13438 if (die->tag == DW_TAG_inheritance)
13439 {
13440 fip->baseclasses.emplace_back ();
13441 new_field = &fip->baseclasses.back ();
13442 }
13443 else
13444 {
13445 fip->fields.emplace_back ();
13446 new_field = &fip->fields.back ();
13447 }
13448
13449 new_field->offset = die->sect_off;
13450
13451 new_field->accessibility = dwarf2_access_attribute (die, cu);
13452 if (new_field->accessibility != DW_ACCESS_public)
13453 fip->non_public_fields = true;
13454
13455 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13456 if (attr != nullptr)
13457 new_field->virtuality = attr->as_virtuality ();
13458 else
13459 new_field->virtuality = DW_VIRTUALITY_none;
13460
13461 fp = &new_field->field;
13462
13463 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
13464 && !die_is_declaration (die, cu))
13465 {
13466 if (die->tag == DW_TAG_namelist_item)
13467 {
13468 /* Typically, DW_TAG_namelist_item are references to namelist items.
13469 If so, follow that reference. */
13470 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
13471 struct die_info *item_die = nullptr;
13472 struct dwarf2_cu *item_cu = cu;
13473 if (attr1->form_is_ref ())
13474 item_die = follow_die_ref (die, attr1, &item_cu);
13475 if (item_die != nullptr)
13476 die = item_die;
13477 }
13478 /* Data member other than a C++ static data member. */
13479
13480 /* Get type of field. */
13481 fp->set_type (die_type (die, cu));
13482
13483 fp->set_loc_bitpos (0);
13484
13485 /* Get bit size of field (zero if none). */
13486 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13487 if (attr != nullptr)
13488 {
13489 FIELD_BITSIZE (*fp) = attr->constant_value (0);
13490 }
13491 else
13492 {
13493 FIELD_BITSIZE (*fp) = 0;
13494 }
13495
13496 /* Get bit offset of field. */
13497 handle_member_location (die, cu, fp);
13498 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13499 if (attr != nullptr && attr->form_is_constant ())
13500 {
13501 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
13502 {
13503 /* For big endian bits, the DW_AT_bit_offset gives the
13504 additional bit offset from the MSB of the containing
13505 anonymous object to the MSB of the field. We don't
13506 have to do anything special since we don't need to
13507 know the size of the anonymous object. */
13508 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
13509 }
13510 else
13511 {
13512 /* For little endian bits, compute the bit offset to the
13513 MSB of the anonymous object, subtract off the number of
13514 bits from the MSB of the field to the MSB of the
13515 object, and then subtract off the number of bits of
13516 the field itself. The result is the bit offset of
13517 the LSB of the field. */
13518 int anonymous_size;
13519 int bit_offset = attr->constant_value (0);
13520
13521 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13522 if (attr != nullptr && attr->form_is_constant ())
13523 {
13524 /* The size of the anonymous object containing
13525 the bit field is explicit, so use the
13526 indicated size (in bytes). */
13527 anonymous_size = attr->constant_value (0);
13528 }
13529 else
13530 {
13531 /* The size of the anonymous object containing
13532 the bit field must be inferred from the type
13533 attribute of the data member containing the
13534 bit field. */
13535 anonymous_size = TYPE_LENGTH (fp->type ());
13536 }
13537 fp->set_loc_bitpos (fp->loc_bitpos ()
13538 + anonymous_size * bits_per_byte
13539 - bit_offset - FIELD_BITSIZE (*fp));
13540 }
13541 }
13542
13543 /* Get name of field. */
13544 fieldname = dwarf2_name (die, cu);
13545 if (fieldname == NULL)
13546 fieldname = "";
13547
13548 /* The name is already allocated along with this objfile, so we don't
13549 need to duplicate it for the type. */
13550 fp->set_name (fieldname);
13551
13552 /* Change accessibility for artificial fields (e.g. virtual table
13553 pointer or virtual base class pointer) to private. */
13554 if (dwarf2_attr (die, DW_AT_artificial, cu))
13555 {
13556 FIELD_ARTIFICIAL (*fp) = 1;
13557 new_field->accessibility = DW_ACCESS_private;
13558 fip->non_public_fields = true;
13559 }
13560 }
13561 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13562 {
13563 /* C++ static member. */
13564
13565 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13566 is a declaration, but all versions of G++ as of this writing
13567 (so through at least 3.2.1) incorrectly generate
13568 DW_TAG_variable tags. */
13569
13570 const char *physname;
13571
13572 /* Get name of field. */
13573 fieldname = dwarf2_name (die, cu);
13574 if (fieldname == NULL)
13575 return;
13576
13577 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13578 if (attr
13579 /* Only create a symbol if this is an external value.
13580 new_symbol checks this and puts the value in the global symbol
13581 table, which we want. If it is not external, new_symbol
13582 will try to put the value in cu->list_in_scope which is wrong. */
13583 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13584 {
13585 /* A static const member, not much different than an enum as far as
13586 we're concerned, except that we can support more types. */
13587 new_symbol (die, NULL, cu);
13588 }
13589
13590 /* Get physical name. */
13591 physname = dwarf2_physname (fieldname, die, cu);
13592
13593 /* The name is already allocated along with this objfile, so we don't
13594 need to duplicate it for the type. */
13595 fp->set_loc_physname (physname ? physname : "");
13596 fp->set_type (die_type (die, cu));
13597 fp->set_name (fieldname);
13598 }
13599 else if (die->tag == DW_TAG_inheritance)
13600 {
13601 /* C++ base class field. */
13602 handle_member_location (die, cu, fp);
13603 FIELD_BITSIZE (*fp) = 0;
13604 fp->set_type (die_type (die, cu));
13605 fp->set_name (fp->type ()->name ());
13606 }
13607 else
13608 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13609 }
13610
13611 /* Can the type given by DIE define another type? */
13612
13613 static bool
13614 type_can_define_types (const struct die_info *die)
13615 {
13616 switch (die->tag)
13617 {
13618 case DW_TAG_typedef:
13619 case DW_TAG_class_type:
13620 case DW_TAG_structure_type:
13621 case DW_TAG_union_type:
13622 case DW_TAG_enumeration_type:
13623 return true;
13624
13625 default:
13626 return false;
13627 }
13628 }
13629
13630 /* Add a type definition defined in the scope of the FIP's class. */
13631
13632 static void
13633 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
13634 struct dwarf2_cu *cu)
13635 {
13636 struct decl_field fp;
13637 memset (&fp, 0, sizeof (fp));
13638
13639 gdb_assert (type_can_define_types (die));
13640
13641 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13642 fp.name = dwarf2_name (die, cu);
13643 fp.type = read_type_die (die, cu);
13644
13645 /* Save accessibility. */
13646 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
13647 switch (accessibility)
13648 {
13649 case DW_ACCESS_public:
13650 /* The assumed value if neither private nor protected. */
13651 break;
13652 case DW_ACCESS_private:
13653 fp.is_private = 1;
13654 break;
13655 case DW_ACCESS_protected:
13656 fp.is_protected = 1;
13657 break;
13658 }
13659
13660 if (die->tag == DW_TAG_typedef)
13661 fip->typedef_field_list.push_back (fp);
13662 else
13663 fip->nested_types_list.push_back (fp);
13664 }
13665
13666 /* A convenience typedef that's used when finding the discriminant
13667 field for a variant part. */
13668 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
13669 offset_map_type;
13670
13671 /* Compute the discriminant range for a given variant. OBSTACK is
13672 where the results will be stored. VARIANT is the variant to
13673 process. IS_UNSIGNED indicates whether the discriminant is signed
13674 or unsigned. */
13675
13676 static const gdb::array_view<discriminant_range>
13677 convert_variant_range (struct obstack *obstack, const variant_field &variant,
13678 bool is_unsigned)
13679 {
13680 std::vector<discriminant_range> ranges;
13681
13682 if (variant.default_branch)
13683 return {};
13684
13685 if (variant.discr_list_data == nullptr)
13686 {
13687 discriminant_range r
13688 = {variant.discriminant_value, variant.discriminant_value};
13689 ranges.push_back (r);
13690 }
13691 else
13692 {
13693 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
13694 variant.discr_list_data->size);
13695 while (!data.empty ())
13696 {
13697 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
13698 {
13699 complaint (_("invalid discriminant marker: %d"), data[0]);
13700 break;
13701 }
13702 bool is_range = data[0] == DW_DSC_range;
13703 data = data.slice (1);
13704
13705 ULONGEST low, high;
13706 unsigned int bytes_read;
13707
13708 if (data.empty ())
13709 {
13710 complaint (_("DW_AT_discr_list missing low value"));
13711 break;
13712 }
13713 if (is_unsigned)
13714 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
13715 else
13716 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
13717 &bytes_read);
13718 data = data.slice (bytes_read);
13719
13720 if (is_range)
13721 {
13722 if (data.empty ())
13723 {
13724 complaint (_("DW_AT_discr_list missing high value"));
13725 break;
13726 }
13727 if (is_unsigned)
13728 high = read_unsigned_leb128 (nullptr, data.data (),
13729 &bytes_read);
13730 else
13731 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
13732 &bytes_read);
13733 data = data.slice (bytes_read);
13734 }
13735 else
13736 high = low;
13737
13738 ranges.push_back ({ low, high });
13739 }
13740 }
13741
13742 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
13743 ranges.size ());
13744 std::copy (ranges.begin (), ranges.end (), result);
13745 return gdb::array_view<discriminant_range> (result, ranges.size ());
13746 }
13747
13748 static const gdb::array_view<variant_part> create_variant_parts
13749 (struct obstack *obstack,
13750 const offset_map_type &offset_map,
13751 struct field_info *fi,
13752 const std::vector<variant_part_builder> &variant_parts);
13753
13754 /* Fill in a "struct variant" for a given variant field. RESULT is
13755 the variant to fill in. OBSTACK is where any needed allocations
13756 will be done. OFFSET_MAP holds the mapping from section offsets to
13757 fields for the type. FI describes the fields of the type we're
13758 processing. FIELD is the variant field we're converting. */
13759
13760 static void
13761 create_one_variant (variant &result, struct obstack *obstack,
13762 const offset_map_type &offset_map,
13763 struct field_info *fi, const variant_field &field)
13764 {
13765 result.discriminants = convert_variant_range (obstack, field, false);
13766 result.first_field = field.first_field + fi->baseclasses.size ();
13767 result.last_field = field.last_field + fi->baseclasses.size ();
13768 result.parts = create_variant_parts (obstack, offset_map, fi,
13769 field.variant_parts);
13770 }
13771
13772 /* Fill in a "struct variant_part" for a given variant part. RESULT
13773 is the variant part to fill in. OBSTACK is where any needed
13774 allocations will be done. OFFSET_MAP holds the mapping from
13775 section offsets to fields for the type. FI describes the fields of
13776 the type we're processing. BUILDER is the variant part to be
13777 converted. */
13778
13779 static void
13780 create_one_variant_part (variant_part &result,
13781 struct obstack *obstack,
13782 const offset_map_type &offset_map,
13783 struct field_info *fi,
13784 const variant_part_builder &builder)
13785 {
13786 auto iter = offset_map.find (builder.discriminant_offset);
13787 if (iter == offset_map.end ())
13788 {
13789 result.discriminant_index = -1;
13790 /* Doesn't matter. */
13791 result.is_unsigned = false;
13792 }
13793 else
13794 {
13795 result.discriminant_index = iter->second;
13796 result.is_unsigned
13797 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
13798 }
13799
13800 size_t n = builder.variants.size ();
13801 variant *output = new (obstack) variant[n];
13802 for (size_t i = 0; i < n; ++i)
13803 create_one_variant (output[i], obstack, offset_map, fi,
13804 builder.variants[i]);
13805
13806 result.variants = gdb::array_view<variant> (output, n);
13807 }
13808
13809 /* Create a vector of variant parts that can be attached to a type.
13810 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13811 holds the mapping from section offsets to fields for the type. FI
13812 describes the fields of the type we're processing. VARIANT_PARTS
13813 is the vector to convert. */
13814
13815 static const gdb::array_view<variant_part>
13816 create_variant_parts (struct obstack *obstack,
13817 const offset_map_type &offset_map,
13818 struct field_info *fi,
13819 const std::vector<variant_part_builder> &variant_parts)
13820 {
13821 if (variant_parts.empty ())
13822 return {};
13823
13824 size_t n = variant_parts.size ();
13825 variant_part *result = new (obstack) variant_part[n];
13826 for (size_t i = 0; i < n; ++i)
13827 create_one_variant_part (result[i], obstack, offset_map, fi,
13828 variant_parts[i]);
13829
13830 return gdb::array_view<variant_part> (result, n);
13831 }
13832
13833 /* Compute the variant part vector for FIP, attaching it to TYPE when
13834 done. */
13835
13836 static void
13837 add_variant_property (struct field_info *fip, struct type *type,
13838 struct dwarf2_cu *cu)
13839 {
13840 /* Map section offsets of fields to their field index. Note the
13841 field index here does not take the number of baseclasses into
13842 account. */
13843 offset_map_type offset_map;
13844 for (int i = 0; i < fip->fields.size (); ++i)
13845 offset_map[fip->fields[i].offset] = i;
13846
13847 struct objfile *objfile = cu->per_objfile->objfile;
13848 gdb::array_view<const variant_part> parts
13849 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
13850 fip->variant_parts);
13851
13852 struct dynamic_prop prop;
13853 prop.set_variant_parts ((gdb::array_view<variant_part> *)
13854 obstack_copy (&objfile->objfile_obstack, &parts,
13855 sizeof (parts)));
13856
13857 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
13858 }
13859
13860 /* Create the vector of fields, and attach it to the type. */
13861
13862 static void
13863 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13864 struct dwarf2_cu *cu)
13865 {
13866 int nfields = fip->nfields ();
13867
13868 /* Record the field count, allocate space for the array of fields,
13869 and create blank accessibility bitfields if necessary. */
13870 type->set_num_fields (nfields);
13871 type->set_fields
13872 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
13873
13874 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
13875 {
13876 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13877
13878 TYPE_FIELD_PRIVATE_BITS (type) =
13879 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13880 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13881
13882 TYPE_FIELD_PROTECTED_BITS (type) =
13883 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13884 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13885
13886 TYPE_FIELD_IGNORE_BITS (type) =
13887 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13888 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13889 }
13890
13891 /* If the type has baseclasses, allocate and clear a bit vector for
13892 TYPE_FIELD_VIRTUAL_BITS. */
13893 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
13894 {
13895 int num_bytes = B_BYTES (fip->baseclasses.size ());
13896 unsigned char *pointer;
13897
13898 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13899 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13900 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13901 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
13902 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
13903 }
13904
13905 if (!fip->variant_parts.empty ())
13906 add_variant_property (fip, type, cu);
13907
13908 /* Copy the saved-up fields into the field vector. */
13909 for (int i = 0; i < nfields; ++i)
13910 {
13911 struct nextfield &field
13912 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
13913 : fip->fields[i - fip->baseclasses.size ()]);
13914
13915 type->field (i) = field.field;
13916 switch (field.accessibility)
13917 {
13918 case DW_ACCESS_private:
13919 if (cu->per_cu->lang != language_ada)
13920 SET_TYPE_FIELD_PRIVATE (type, i);
13921 break;
13922
13923 case DW_ACCESS_protected:
13924 if (cu->per_cu->lang != language_ada)
13925 SET_TYPE_FIELD_PROTECTED (type, i);
13926 break;
13927
13928 case DW_ACCESS_public:
13929 break;
13930
13931 default:
13932 /* Unknown accessibility. Complain and treat it as public. */
13933 {
13934 complaint (_("unsupported accessibility %d"),
13935 field.accessibility);
13936 }
13937 break;
13938 }
13939 if (i < fip->baseclasses.size ())
13940 {
13941 switch (field.virtuality)
13942 {
13943 case DW_VIRTUALITY_virtual:
13944 case DW_VIRTUALITY_pure_virtual:
13945 if (cu->per_cu->lang == language_ada)
13946 error (_("unexpected virtuality in component of Ada type"));
13947 SET_TYPE_FIELD_VIRTUAL (type, i);
13948 break;
13949 }
13950 }
13951 }
13952 }
13953
13954 /* Return true if this member function is a constructor, false
13955 otherwise. */
13956
13957 static int
13958 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13959 {
13960 const char *fieldname;
13961 const char *type_name;
13962 int len;
13963
13964 if (die->parent == NULL)
13965 return 0;
13966
13967 if (die->parent->tag != DW_TAG_structure_type
13968 && die->parent->tag != DW_TAG_union_type
13969 && die->parent->tag != DW_TAG_class_type)
13970 return 0;
13971
13972 fieldname = dwarf2_name (die, cu);
13973 type_name = dwarf2_name (die->parent, cu);
13974 if (fieldname == NULL || type_name == NULL)
13975 return 0;
13976
13977 len = strlen (fieldname);
13978 return (strncmp (fieldname, type_name, len) == 0
13979 && (type_name[len] == '\0' || type_name[len] == '<'));
13980 }
13981
13982 /* Add a member function to the proper fieldlist. */
13983
13984 static void
13985 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13986 struct type *type, struct dwarf2_cu *cu)
13987 {
13988 struct objfile *objfile = cu->per_objfile->objfile;
13989 struct attribute *attr;
13990 int i;
13991 struct fnfieldlist *flp = nullptr;
13992 struct fn_field *fnp;
13993 const char *fieldname;
13994 struct type *this_type;
13995
13996 if (cu->per_cu->lang == language_ada)
13997 error (_("unexpected member function in Ada type"));
13998
13999 /* Get name of member function. */
14000 fieldname = dwarf2_name (die, cu);
14001 if (fieldname == NULL)
14002 return;
14003
14004 /* Look up member function name in fieldlist. */
14005 for (i = 0; i < fip->fnfieldlists.size (); i++)
14006 {
14007 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14008 {
14009 flp = &fip->fnfieldlists[i];
14010 break;
14011 }
14012 }
14013
14014 /* Create a new fnfieldlist if necessary. */
14015 if (flp == nullptr)
14016 {
14017 fip->fnfieldlists.emplace_back ();
14018 flp = &fip->fnfieldlists.back ();
14019 flp->name = fieldname;
14020 i = fip->fnfieldlists.size () - 1;
14021 }
14022
14023 /* Create a new member function field and add it to the vector of
14024 fnfieldlists. */
14025 flp->fnfields.emplace_back ();
14026 fnp = &flp->fnfields.back ();
14027
14028 /* Delay processing of the physname until later. */
14029 if (cu->per_cu->lang == language_cplus)
14030 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14031 die, cu);
14032 else
14033 {
14034 const char *physname = dwarf2_physname (fieldname, die, cu);
14035 fnp->physname = physname ? physname : "";
14036 }
14037
14038 fnp->type = alloc_type (objfile);
14039 this_type = read_type_die (die, cu);
14040 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14041 {
14042 int nparams = this_type->num_fields ();
14043
14044 /* TYPE is the domain of this method, and THIS_TYPE is the type
14045 of the method itself (TYPE_CODE_METHOD). */
14046 smash_to_method_type (fnp->type, type,
14047 TYPE_TARGET_TYPE (this_type),
14048 this_type->fields (),
14049 this_type->num_fields (),
14050 this_type->has_varargs ());
14051
14052 /* Handle static member functions.
14053 Dwarf2 has no clean way to discern C++ static and non-static
14054 member functions. G++ helps GDB by marking the first
14055 parameter for non-static member functions (which is the this
14056 pointer) as artificial. We obtain this information from
14057 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14058 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14059 fnp->voffset = VOFFSET_STATIC;
14060 }
14061 else
14062 complaint (_("member function type missing for '%s'"),
14063 dwarf2_full_name (fieldname, die, cu));
14064
14065 /* Get fcontext from DW_AT_containing_type if present. */
14066 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14067 fnp->fcontext = die_containing_type (die, cu);
14068
14069 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14070 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14071
14072 /* Get accessibility. */
14073 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14074 switch (accessibility)
14075 {
14076 case DW_ACCESS_private:
14077 fnp->is_private = 1;
14078 break;
14079 case DW_ACCESS_protected:
14080 fnp->is_protected = 1;
14081 break;
14082 }
14083
14084 /* Check for artificial methods. */
14085 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14086 if (attr && attr->as_boolean ())
14087 fnp->is_artificial = 1;
14088
14089 /* Check for defaulted methods. */
14090 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14091 if (attr != nullptr)
14092 fnp->defaulted = attr->defaulted ();
14093
14094 /* Check for deleted methods. */
14095 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14096 if (attr != nullptr && attr->as_boolean ())
14097 fnp->is_deleted = 1;
14098
14099 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14100
14101 /* Get index in virtual function table if it is a virtual member
14102 function. For older versions of GCC, this is an offset in the
14103 appropriate virtual table, as specified by DW_AT_containing_type.
14104 For everyone else, it is an expression to be evaluated relative
14105 to the object address. */
14106
14107 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14108 if (attr != nullptr)
14109 {
14110 if (attr->form_is_block () && attr->as_block ()->size > 0)
14111 {
14112 struct dwarf_block *block = attr->as_block ();
14113
14114 if (block->data[0] == DW_OP_constu)
14115 {
14116 /* Old-style GCC. */
14117 fnp->voffset = decode_locdesc (block, cu) + 2;
14118 }
14119 else if (block->data[0] == DW_OP_deref
14120 || (block->size > 1
14121 && block->data[0] == DW_OP_deref_size
14122 && block->data[1] == cu->header.addr_size))
14123 {
14124 fnp->voffset = decode_locdesc (block, cu);
14125 if ((fnp->voffset % cu->header.addr_size) != 0)
14126 dwarf2_complex_location_expr_complaint ();
14127 else
14128 fnp->voffset /= cu->header.addr_size;
14129 fnp->voffset += 2;
14130 }
14131 else
14132 dwarf2_complex_location_expr_complaint ();
14133
14134 if (!fnp->fcontext)
14135 {
14136 /* If there is no `this' field and no DW_AT_containing_type,
14137 we cannot actually find a base class context for the
14138 vtable! */
14139 if (this_type->num_fields () == 0
14140 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14141 {
14142 complaint (_("cannot determine context for virtual member "
14143 "function \"%s\" (offset %s)"),
14144 fieldname, sect_offset_str (die->sect_off));
14145 }
14146 else
14147 {
14148 fnp->fcontext
14149 = TYPE_TARGET_TYPE (this_type->field (0).type ());
14150 }
14151 }
14152 }
14153 else if (attr->form_is_section_offset ())
14154 {
14155 dwarf2_complex_location_expr_complaint ();
14156 }
14157 else
14158 {
14159 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14160 fieldname);
14161 }
14162 }
14163 else
14164 {
14165 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14166 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
14167 {
14168 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14169 complaint (_("Member function \"%s\" (offset %s) is virtual "
14170 "but the vtable offset is not specified"),
14171 fieldname, sect_offset_str (die->sect_off));
14172 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14173 TYPE_CPLUS_DYNAMIC (type) = 1;
14174 }
14175 }
14176 }
14177
14178 /* Create the vector of member function fields, and attach it to the type. */
14179
14180 static void
14181 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14182 struct dwarf2_cu *cu)
14183 {
14184 if (cu->per_cu->lang == language_ada)
14185 error (_("unexpected member functions in Ada type"));
14186
14187 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14188 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14189 TYPE_ALLOC (type,
14190 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14191
14192 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14193 {
14194 struct fnfieldlist &nf = fip->fnfieldlists[i];
14195 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14196
14197 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14198 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14199 fn_flp->fn_fields = (struct fn_field *)
14200 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14201
14202 for (int k = 0; k < nf.fnfields.size (); ++k)
14203 fn_flp->fn_fields[k] = nf.fnfields[k];
14204 }
14205
14206 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14207 }
14208
14209 /* Returns non-zero if NAME is the name of a vtable member in CU's
14210 language, zero otherwise. */
14211 static int
14212 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14213 {
14214 static const char vptr[] = "_vptr";
14215
14216 /* Look for the C++ form of the vtable. */
14217 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14218 return 1;
14219
14220 return 0;
14221 }
14222
14223 /* GCC outputs unnamed structures that are really pointers to member
14224 functions, with the ABI-specified layout. If TYPE describes
14225 such a structure, smash it into a member function type.
14226
14227 GCC shouldn't do this; it should just output pointer to member DIEs.
14228 This is GCC PR debug/28767. */
14229
14230 static void
14231 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14232 {
14233 struct type *pfn_type, *self_type, *new_type;
14234
14235 /* Check for a structure with no name and two children. */
14236 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14237 return;
14238
14239 /* Check for __pfn and __delta members. */
14240 if (type->field (0).name () == NULL
14241 || strcmp (type->field (0).name (), "__pfn") != 0
14242 || type->field (1).name () == NULL
14243 || strcmp (type->field (1).name (), "__delta") != 0)
14244 return;
14245
14246 /* Find the type of the method. */
14247 pfn_type = type->field (0).type ();
14248 if (pfn_type == NULL
14249 || pfn_type->code () != TYPE_CODE_PTR
14250 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
14251 return;
14252
14253 /* Look for the "this" argument. */
14254 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14255 if (pfn_type->num_fields () == 0
14256 /* || pfn_type->field (0).type () == NULL */
14257 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
14258 return;
14259
14260 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
14261 new_type = alloc_type (objfile);
14262 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14263 pfn_type->fields (), pfn_type->num_fields (),
14264 pfn_type->has_varargs ());
14265 smash_to_methodptr_type (type, new_type);
14266 }
14267
14268 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14269 requires rewriting, then copy it and return the updated copy.
14270 Otherwise return nullptr. */
14271
14272 static struct type *
14273 rewrite_array_type (struct type *type)
14274 {
14275 if (type->code () != TYPE_CODE_ARRAY)
14276 return nullptr;
14277
14278 struct type *index_type = type->index_type ();
14279 range_bounds *current_bounds = index_type->bounds ();
14280
14281 /* Handle multi-dimensional arrays. */
14282 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
14283 if (new_target == nullptr)
14284 {
14285 /* Maybe we don't need to rewrite this array. */
14286 if (current_bounds->low.kind () == PROP_CONST
14287 && current_bounds->high.kind () == PROP_CONST)
14288 return nullptr;
14289 }
14290
14291 /* Either the target type was rewritten, or the bounds have to be
14292 updated. Either way we want to copy the type and update
14293 everything. */
14294 struct type *copy = copy_type (type);
14295 int nfields = copy->num_fields ();
14296 field *new_fields
14297 = ((struct field *) TYPE_ZALLOC (copy,
14298 nfields * sizeof (struct field)));
14299 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
14300 copy->set_fields (new_fields);
14301 if (new_target != nullptr)
14302 TYPE_TARGET_TYPE (copy) = new_target;
14303
14304 struct type *index_copy = copy_type (index_type);
14305 range_bounds *bounds
14306 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
14307 sizeof (range_bounds));
14308 *bounds = *current_bounds;
14309 bounds->low.set_const_val (1);
14310 bounds->high.set_const_val (0);
14311 index_copy->set_bounds (bounds);
14312 copy->set_index_type (index_copy);
14313
14314 return copy;
14315 }
14316
14317 /* While some versions of GCC will generate complicated DWARF for an
14318 array (see quirk_ada_thick_pointer), more recent versions were
14319 modified to emit an explicit thick pointer structure. However, in
14320 this case, the array still has DWARF expressions for its ranges,
14321 and these must be ignored. */
14322
14323 static void
14324 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
14325 struct type *type)
14326 {
14327 gdb_assert (cu->per_cu->lang == language_ada);
14328
14329 /* Check for a structure with two children. */
14330 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14331 return;
14332
14333 /* Check for P_ARRAY and P_BOUNDS members. */
14334 if (type->field (0).name () == NULL
14335 || strcmp (type->field (0).name (), "P_ARRAY") != 0
14336 || type->field (1).name () == NULL
14337 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
14338 return;
14339
14340 /* Make sure we're looking at a pointer to an array. */
14341 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
14342 return;
14343
14344 /* The Ada code already knows how to handle these types, so all that
14345 we need to do is turn the bounds into static bounds. However, we
14346 don't want to rewrite existing array or index types in-place,
14347 because those may be referenced in other contexts where this
14348 rewriting is undesirable. */
14349 struct type *new_ary_type
14350 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
14351 if (new_ary_type != nullptr)
14352 type->field (0).set_type (lookup_pointer_type (new_ary_type));
14353 }
14354
14355 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14356 appropriate error checking and issuing complaints if there is a
14357 problem. */
14358
14359 static ULONGEST
14360 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14361 {
14362 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14363
14364 if (attr == nullptr)
14365 return 0;
14366
14367 if (!attr->form_is_constant ())
14368 {
14369 complaint (_("DW_AT_alignment must have constant form"
14370 " - DIE at %s [in module %s]"),
14371 sect_offset_str (die->sect_off),
14372 objfile_name (cu->per_objfile->objfile));
14373 return 0;
14374 }
14375
14376 LONGEST val = attr->constant_value (0);
14377 if (val < 0)
14378 {
14379 complaint (_("DW_AT_alignment value must not be negative"
14380 " - DIE at %s [in module %s]"),
14381 sect_offset_str (die->sect_off),
14382 objfile_name (cu->per_objfile->objfile));
14383 return 0;
14384 }
14385 ULONGEST align = val;
14386
14387 if (align == 0)
14388 {
14389 complaint (_("DW_AT_alignment value must not be zero"
14390 " - DIE at %s [in module %s]"),
14391 sect_offset_str (die->sect_off),
14392 objfile_name (cu->per_objfile->objfile));
14393 return 0;
14394 }
14395 if ((align & (align - 1)) != 0)
14396 {
14397 complaint (_("DW_AT_alignment value must be a power of 2"
14398 " - DIE at %s [in module %s]"),
14399 sect_offset_str (die->sect_off),
14400 objfile_name (cu->per_objfile->objfile));
14401 return 0;
14402 }
14403
14404 return align;
14405 }
14406
14407 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14408 the alignment for TYPE. */
14409
14410 static void
14411 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14412 struct type *type)
14413 {
14414 if (!set_type_align (type, get_alignment (cu, die)))
14415 complaint (_("DW_AT_alignment value too large"
14416 " - DIE at %s [in module %s]"),
14417 sect_offset_str (die->sect_off),
14418 objfile_name (cu->per_objfile->objfile));
14419 }
14420
14421 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14422 constant for a type, according to DWARF5 spec, Table 5.5. */
14423
14424 static bool
14425 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14426 {
14427 switch (value)
14428 {
14429 case DW_CC_normal:
14430 case DW_CC_pass_by_reference:
14431 case DW_CC_pass_by_value:
14432 return true;
14433
14434 default:
14435 complaint (_("unrecognized DW_AT_calling_convention value "
14436 "(%s) for a type"), pulongest (value));
14437 return false;
14438 }
14439 }
14440
14441 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14442 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14443 also according to GNU-specific values (see include/dwarf2.h). */
14444
14445 static bool
14446 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14447 {
14448 switch (value)
14449 {
14450 case DW_CC_normal:
14451 case DW_CC_program:
14452 case DW_CC_nocall:
14453 return true;
14454
14455 case DW_CC_GNU_renesas_sh:
14456 case DW_CC_GNU_borland_fastcall_i386:
14457 case DW_CC_GDB_IBM_OpenCL:
14458 return true;
14459
14460 default:
14461 complaint (_("unrecognized DW_AT_calling_convention value "
14462 "(%s) for a subroutine"), pulongest (value));
14463 return false;
14464 }
14465 }
14466
14467 /* Called when we find the DIE that starts a structure or union scope
14468 (definition) to create a type for the structure or union. Fill in
14469 the type's name and general properties; the members will not be
14470 processed until process_structure_scope. A symbol table entry for
14471 the type will also not be done until process_structure_scope (assuming
14472 the type has a name).
14473
14474 NOTE: we need to call these functions regardless of whether or not the
14475 DIE has a DW_AT_name attribute, since it might be an anonymous
14476 structure or union. This gets the type entered into our set of
14477 user defined types. */
14478
14479 static struct type *
14480 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14481 {
14482 struct objfile *objfile = cu->per_objfile->objfile;
14483 struct type *type;
14484 struct attribute *attr;
14485 const char *name;
14486
14487 /* If the definition of this type lives in .debug_types, read that type.
14488 Don't follow DW_AT_specification though, that will take us back up
14489 the chain and we want to go down. */
14490 attr = die->attr (DW_AT_signature);
14491 if (attr != nullptr)
14492 {
14493 type = get_DW_AT_signature_type (die, attr, cu);
14494
14495 /* The type's CU may not be the same as CU.
14496 Ensure TYPE is recorded with CU in die_type_hash. */
14497 return set_die_type (die, type, cu);
14498 }
14499
14500 type = alloc_type (objfile);
14501 INIT_CPLUS_SPECIFIC (type);
14502
14503 name = dwarf2_name (die, cu);
14504 if (name != NULL)
14505 {
14506 if (cu->per_cu->lang == language_cplus
14507 || cu->per_cu->lang == language_d
14508 || cu->per_cu->lang == language_rust)
14509 {
14510 const char *full_name = dwarf2_full_name (name, die, cu);
14511
14512 /* dwarf2_full_name might have already finished building the DIE's
14513 type. If so, there is no need to continue. */
14514 if (get_die_type (die, cu) != NULL)
14515 return get_die_type (die, cu);
14516
14517 type->set_name (full_name);
14518 }
14519 else
14520 {
14521 /* The name is already allocated along with this objfile, so
14522 we don't need to duplicate it for the type. */
14523 type->set_name (name);
14524 }
14525 }
14526
14527 if (die->tag == DW_TAG_structure_type)
14528 {
14529 type->set_code (TYPE_CODE_STRUCT);
14530 }
14531 else if (die->tag == DW_TAG_union_type)
14532 {
14533 type->set_code (TYPE_CODE_UNION);
14534 }
14535 else if (die->tag == DW_TAG_namelist)
14536 {
14537 type->set_code (TYPE_CODE_NAMELIST);
14538 }
14539 else
14540 {
14541 type->set_code (TYPE_CODE_STRUCT);
14542 }
14543
14544 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
14545 type->set_is_declared_class (true);
14546
14547 /* Store the calling convention in the type if it's available in
14548 the die. Otherwise the calling convention remains set to
14549 the default value DW_CC_normal. */
14550 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14551 if (attr != nullptr
14552 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
14553 {
14554 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14555 TYPE_CPLUS_CALLING_CONVENTION (type)
14556 = (enum dwarf_calling_convention) (attr->constant_value (0));
14557 }
14558
14559 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14560 if (attr != nullptr)
14561 {
14562 if (attr->form_is_constant ())
14563 TYPE_LENGTH (type) = attr->constant_value (0);
14564 else
14565 {
14566 struct dynamic_prop prop;
14567 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
14568 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
14569 TYPE_LENGTH (type) = 0;
14570 }
14571 }
14572 else
14573 {
14574 TYPE_LENGTH (type) = 0;
14575 }
14576
14577 maybe_set_alignment (cu, die, type);
14578
14579 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
14580 {
14581 /* ICC<14 does not output the required DW_AT_declaration on
14582 incomplete types, but gives them a size of zero. */
14583 type->set_is_stub (true);
14584 }
14585 else
14586 type->set_stub_is_supported (true);
14587
14588 if (die_is_declaration (die, cu))
14589 type->set_is_stub (true);
14590 else if (attr == NULL && die->child == NULL
14591 && producer_is_realview (cu->producer))
14592 /* RealView does not output the required DW_AT_declaration
14593 on incomplete types. */
14594 type->set_is_stub (true);
14595
14596 /* We need to add the type field to the die immediately so we don't
14597 infinitely recurse when dealing with pointers to the structure
14598 type within the structure itself. */
14599 set_die_type (die, type, cu);
14600
14601 /* set_die_type should be already done. */
14602 set_descriptive_type (type, die, cu);
14603
14604 return type;
14605 }
14606
14607 static void handle_struct_member_die
14608 (struct die_info *child_die,
14609 struct type *type,
14610 struct field_info *fi,
14611 std::vector<struct symbol *> *template_args,
14612 struct dwarf2_cu *cu);
14613
14614 /* A helper for handle_struct_member_die that handles
14615 DW_TAG_variant_part. */
14616
14617 static void
14618 handle_variant_part (struct die_info *die, struct type *type,
14619 struct field_info *fi,
14620 std::vector<struct symbol *> *template_args,
14621 struct dwarf2_cu *cu)
14622 {
14623 variant_part_builder *new_part;
14624 if (fi->current_variant_part == nullptr)
14625 {
14626 fi->variant_parts.emplace_back ();
14627 new_part = &fi->variant_parts.back ();
14628 }
14629 else if (!fi->current_variant_part->processing_variant)
14630 {
14631 complaint (_("nested DW_TAG_variant_part seen "
14632 "- DIE at %s [in module %s]"),
14633 sect_offset_str (die->sect_off),
14634 objfile_name (cu->per_objfile->objfile));
14635 return;
14636 }
14637 else
14638 {
14639 variant_field &current = fi->current_variant_part->variants.back ();
14640 current.variant_parts.emplace_back ();
14641 new_part = &current.variant_parts.back ();
14642 }
14643
14644 /* When we recurse, we want callees to add to this new variant
14645 part. */
14646 scoped_restore save_current_variant_part
14647 = make_scoped_restore (&fi->current_variant_part, new_part);
14648
14649 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
14650 if (discr == NULL)
14651 {
14652 /* It's a univariant form, an extension we support. */
14653 }
14654 else if (discr->form_is_ref ())
14655 {
14656 struct dwarf2_cu *target_cu = cu;
14657 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
14658
14659 new_part->discriminant_offset = target_die->sect_off;
14660 }
14661 else
14662 {
14663 complaint (_("DW_AT_discr does not have DIE reference form"
14664 " - DIE at %s [in module %s]"),
14665 sect_offset_str (die->sect_off),
14666 objfile_name (cu->per_objfile->objfile));
14667 }
14668
14669 for (die_info *child_die = die->child;
14670 child_die != NULL;
14671 child_die = child_die->sibling)
14672 handle_struct_member_die (child_die, type, fi, template_args, cu);
14673 }
14674
14675 /* A helper for handle_struct_member_die that handles
14676 DW_TAG_variant. */
14677
14678 static void
14679 handle_variant (struct die_info *die, struct type *type,
14680 struct field_info *fi,
14681 std::vector<struct symbol *> *template_args,
14682 struct dwarf2_cu *cu)
14683 {
14684 if (fi->current_variant_part == nullptr)
14685 {
14686 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14687 "- DIE at %s [in module %s]"),
14688 sect_offset_str (die->sect_off),
14689 objfile_name (cu->per_objfile->objfile));
14690 return;
14691 }
14692 if (fi->current_variant_part->processing_variant)
14693 {
14694 complaint (_("nested DW_TAG_variant seen "
14695 "- DIE at %s [in module %s]"),
14696 sect_offset_str (die->sect_off),
14697 objfile_name (cu->per_objfile->objfile));
14698 return;
14699 }
14700
14701 scoped_restore save_processing_variant
14702 = make_scoped_restore (&fi->current_variant_part->processing_variant,
14703 true);
14704
14705 fi->current_variant_part->variants.emplace_back ();
14706 variant_field &variant = fi->current_variant_part->variants.back ();
14707 variant.first_field = fi->fields.size ();
14708
14709 /* In a variant we want to get the discriminant and also add a
14710 field for our sole member child. */
14711 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
14712 if (discr == nullptr || !discr->form_is_constant ())
14713 {
14714 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
14715 if (discr == nullptr || discr->as_block ()->size == 0)
14716 variant.default_branch = true;
14717 else
14718 variant.discr_list_data = discr->as_block ();
14719 }
14720 else
14721 variant.discriminant_value = discr->constant_value (0);
14722
14723 for (die_info *variant_child = die->child;
14724 variant_child != NULL;
14725 variant_child = variant_child->sibling)
14726 handle_struct_member_die (variant_child, type, fi, template_args, cu);
14727
14728 variant.last_field = fi->fields.size ();
14729 }
14730
14731 /* A helper for process_structure_scope that handles a single member
14732 DIE. */
14733
14734 static void
14735 handle_struct_member_die (struct die_info *child_die, struct type *type,
14736 struct field_info *fi,
14737 std::vector<struct symbol *> *template_args,
14738 struct dwarf2_cu *cu)
14739 {
14740 if (child_die->tag == DW_TAG_member
14741 || child_die->tag == DW_TAG_variable
14742 || child_die->tag == DW_TAG_namelist_item)
14743 {
14744 /* NOTE: carlton/2002-11-05: A C++ static data member
14745 should be a DW_TAG_member that is a declaration, but
14746 all versions of G++ as of this writing (so through at
14747 least 3.2.1) incorrectly generate DW_TAG_variable
14748 tags for them instead. */
14749 dwarf2_add_field (fi, child_die, cu);
14750 }
14751 else if (child_die->tag == DW_TAG_subprogram)
14752 {
14753 /* Rust doesn't have member functions in the C++ sense.
14754 However, it does emit ordinary functions as children
14755 of a struct DIE. */
14756 if (cu->per_cu->lang == language_rust)
14757 read_func_scope (child_die, cu);
14758 else
14759 {
14760 /* C++ member function. */
14761 dwarf2_add_member_fn (fi, child_die, type, cu);
14762 }
14763 }
14764 else if (child_die->tag == DW_TAG_inheritance)
14765 {
14766 /* C++ base class field. */
14767 dwarf2_add_field (fi, child_die, cu);
14768 }
14769 else if (type_can_define_types (child_die))
14770 dwarf2_add_type_defn (fi, child_die, cu);
14771 else if (child_die->tag == DW_TAG_template_type_param
14772 || child_die->tag == DW_TAG_template_value_param)
14773 {
14774 struct symbol *arg = new_symbol (child_die, NULL, cu);
14775
14776 if (arg != NULL)
14777 template_args->push_back (arg);
14778 }
14779 else if (child_die->tag == DW_TAG_variant_part)
14780 handle_variant_part (child_die, type, fi, template_args, cu);
14781 else if (child_die->tag == DW_TAG_variant)
14782 handle_variant (child_die, type, fi, template_args, cu);
14783 }
14784
14785 /* Finish creating a structure or union type, including filling in its
14786 members and creating a symbol for it. This function also handles Fortran
14787 namelist variables, their items or members and creating a symbol for
14788 them. */
14789
14790 static void
14791 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14792 {
14793 struct objfile *objfile = cu->per_objfile->objfile;
14794 struct die_info *child_die;
14795 struct type *type;
14796
14797 type = get_die_type (die, cu);
14798 if (type == NULL)
14799 type = read_structure_type (die, cu);
14800
14801 bool has_template_parameters = false;
14802 if (die->child != NULL && ! die_is_declaration (die, cu))
14803 {
14804 struct field_info fi;
14805 std::vector<struct symbol *> template_args;
14806
14807 child_die = die->child;
14808
14809 while (child_die && child_die->tag)
14810 {
14811 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
14812 child_die = child_die->sibling;
14813 }
14814
14815 /* Attach template arguments to type. */
14816 if (!template_args.empty ())
14817 {
14818 has_template_parameters = true;
14819 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14820 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14821 TYPE_TEMPLATE_ARGUMENTS (type)
14822 = XOBNEWVEC (&objfile->objfile_obstack,
14823 struct symbol *,
14824 TYPE_N_TEMPLATE_ARGUMENTS (type));
14825 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14826 template_args.data (),
14827 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14828 * sizeof (struct symbol *)));
14829 }
14830
14831 /* Attach fields and member functions to the type. */
14832 if (fi.nfields () > 0)
14833 dwarf2_attach_fields_to_type (&fi, type, cu);
14834 if (!fi.fnfieldlists.empty ())
14835 {
14836 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14837
14838 /* Get the type which refers to the base class (possibly this
14839 class itself) which contains the vtable pointer for the current
14840 class from the DW_AT_containing_type attribute. This use of
14841 DW_AT_containing_type is a GNU extension. */
14842
14843 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14844 {
14845 struct type *t = die_containing_type (die, cu);
14846
14847 set_type_vptr_basetype (type, t);
14848 if (type == t)
14849 {
14850 int i;
14851
14852 /* Our own class provides vtbl ptr. */
14853 for (i = t->num_fields () - 1;
14854 i >= TYPE_N_BASECLASSES (t);
14855 --i)
14856 {
14857 const char *fieldname = t->field (i).name ();
14858
14859 if (is_vtable_name (fieldname, cu))
14860 {
14861 set_type_vptr_fieldno (type, i);
14862 break;
14863 }
14864 }
14865
14866 /* Complain if virtual function table field not found. */
14867 if (i < TYPE_N_BASECLASSES (t))
14868 complaint (_("virtual function table pointer "
14869 "not found when defining class '%s'"),
14870 type->name () ? type->name () : "");
14871 }
14872 else
14873 {
14874 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14875 }
14876 }
14877 else if (cu->producer
14878 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14879 {
14880 /* The IBM XLC compiler does not provide direct indication
14881 of the containing type, but the vtable pointer is
14882 always named __vfp. */
14883
14884 int i;
14885
14886 for (i = type->num_fields () - 1;
14887 i >= TYPE_N_BASECLASSES (type);
14888 --i)
14889 {
14890 if (strcmp (type->field (i).name (), "__vfp") == 0)
14891 {
14892 set_type_vptr_fieldno (type, i);
14893 set_type_vptr_basetype (type, type);
14894 break;
14895 }
14896 }
14897 }
14898 }
14899
14900 /* Copy fi.typedef_field_list linked list elements content into the
14901 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14902 if (!fi.typedef_field_list.empty ())
14903 {
14904 int count = fi.typedef_field_list.size ();
14905
14906 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14907 TYPE_TYPEDEF_FIELD_ARRAY (type)
14908 = ((struct decl_field *)
14909 TYPE_ALLOC (type,
14910 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
14911 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
14912
14913 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
14914 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
14915 }
14916
14917 /* Copy fi.nested_types_list linked list elements content into the
14918 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
14919 if (!fi.nested_types_list.empty ()
14920 && cu->per_cu->lang != language_ada)
14921 {
14922 int count = fi.nested_types_list.size ();
14923
14924 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14925 TYPE_NESTED_TYPES_ARRAY (type)
14926 = ((struct decl_field *)
14927 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
14928 TYPE_NESTED_TYPES_COUNT (type) = count;
14929
14930 for (int i = 0; i < fi.nested_types_list.size (); ++i)
14931 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
14932 }
14933 }
14934
14935 quirk_gcc_member_function_pointer (type, objfile);
14936 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
14937 cu->rust_unions.push_back (type);
14938 else if (cu->per_cu->lang == language_ada)
14939 quirk_ada_thick_pointer_struct (die, cu, type);
14940
14941 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14942 snapshots) has been known to create a die giving a declaration
14943 for a class that has, as a child, a die giving a definition for a
14944 nested class. So we have to process our children even if the
14945 current die is a declaration. Normally, of course, a declaration
14946 won't have any children at all. */
14947
14948 child_die = die->child;
14949
14950 while (child_die != NULL && child_die->tag)
14951 {
14952 if (child_die->tag == DW_TAG_member
14953 || child_die->tag == DW_TAG_variable
14954 || child_die->tag == DW_TAG_inheritance
14955 || child_die->tag == DW_TAG_template_value_param
14956 || child_die->tag == DW_TAG_template_type_param)
14957 {
14958 /* Do nothing. */
14959 }
14960 else
14961 process_die (child_die, cu);
14962
14963 child_die = child_die->sibling;
14964 }
14965
14966 /* Do not consider external references. According to the DWARF standard,
14967 these DIEs are identified by the fact that they have no byte_size
14968 attribute, and a declaration attribute. */
14969 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
14970 || !die_is_declaration (die, cu)
14971 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
14972 {
14973 struct symbol *sym = new_symbol (die, type, cu);
14974
14975 if (has_template_parameters)
14976 {
14977 struct symtab *symtab;
14978 if (sym != nullptr)
14979 symtab = sym->symtab ();
14980 else if (cu->line_header != nullptr)
14981 {
14982 /* Any related symtab will do. */
14983 symtab
14984 = cu->line_header->file_names ()[0].symtab;
14985 }
14986 else
14987 {
14988 symtab = nullptr;
14989 complaint (_("could not find suitable "
14990 "symtab for template parameter"
14991 " - DIE at %s [in module %s]"),
14992 sect_offset_str (die->sect_off),
14993 objfile_name (objfile));
14994 }
14995
14996 if (symtab != nullptr)
14997 {
14998 /* Make sure that the symtab is set on the new symbols.
14999 Even though they don't appear in this symtab directly,
15000 other parts of gdb assume that symbols do, and this is
15001 reasonably true. */
15002 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15003 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
15004 }
15005 }
15006 }
15007 }
15008
15009 /* Assuming DIE is an enumeration type, and TYPE is its associated
15010 type, update TYPE using some information only available in DIE's
15011 children. In particular, the fields are computed. */
15012
15013 static void
15014 update_enumeration_type_from_children (struct die_info *die,
15015 struct type *type,
15016 struct dwarf2_cu *cu)
15017 {
15018 struct die_info *child_die;
15019 int unsigned_enum = 1;
15020 int flag_enum = 1;
15021
15022 auto_obstack obstack;
15023 std::vector<struct field> fields;
15024
15025 for (child_die = die->child;
15026 child_die != NULL && child_die->tag;
15027 child_die = child_die->sibling)
15028 {
15029 struct attribute *attr;
15030 LONGEST value;
15031 const gdb_byte *bytes;
15032 struct dwarf2_locexpr_baton *baton;
15033 const char *name;
15034
15035 if (child_die->tag != DW_TAG_enumerator)
15036 continue;
15037
15038 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15039 if (attr == NULL)
15040 continue;
15041
15042 name = dwarf2_name (child_die, cu);
15043 if (name == NULL)
15044 name = "<anonymous enumerator>";
15045
15046 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15047 &value, &bytes, &baton);
15048 if (value < 0)
15049 {
15050 unsigned_enum = 0;
15051 flag_enum = 0;
15052 }
15053 else
15054 {
15055 if (count_one_bits_ll (value) >= 2)
15056 flag_enum = 0;
15057 }
15058
15059 fields.emplace_back ();
15060 struct field &field = fields.back ();
15061 field.set_name (dwarf2_physname (name, child_die, cu));
15062 field.set_loc_enumval (value);
15063 }
15064
15065 if (!fields.empty ())
15066 {
15067 type->set_num_fields (fields.size ());
15068 type->set_fields
15069 ((struct field *)
15070 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15071 memcpy (type->fields (), fields.data (),
15072 sizeof (struct field) * fields.size ());
15073 }
15074
15075 if (unsigned_enum)
15076 type->set_is_unsigned (true);
15077
15078 if (flag_enum)
15079 type->set_is_flag_enum (true);
15080 }
15081
15082 /* Given a DW_AT_enumeration_type die, set its type. We do not
15083 complete the type's fields yet, or create any symbols. */
15084
15085 static struct type *
15086 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15087 {
15088 struct objfile *objfile = cu->per_objfile->objfile;
15089 struct type *type;
15090 struct attribute *attr;
15091 const char *name;
15092
15093 /* If the definition of this type lives in .debug_types, read that type.
15094 Don't follow DW_AT_specification though, that will take us back up
15095 the chain and we want to go down. */
15096 attr = die->attr (DW_AT_signature);
15097 if (attr != nullptr)
15098 {
15099 type = get_DW_AT_signature_type (die, attr, cu);
15100
15101 /* The type's CU may not be the same as CU.
15102 Ensure TYPE is recorded with CU in die_type_hash. */
15103 return set_die_type (die, type, cu);
15104 }
15105
15106 type = alloc_type (objfile);
15107
15108 type->set_code (TYPE_CODE_ENUM);
15109 name = dwarf2_full_name (NULL, die, cu);
15110 if (name != NULL)
15111 type->set_name (name);
15112
15113 attr = dwarf2_attr (die, DW_AT_type, cu);
15114 if (attr != NULL)
15115 {
15116 struct type *underlying_type = die_type (die, cu);
15117
15118 TYPE_TARGET_TYPE (type) = underlying_type;
15119 }
15120
15121 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15122 if (attr != nullptr)
15123 {
15124 TYPE_LENGTH (type) = attr->constant_value (0);
15125 }
15126 else
15127 {
15128 TYPE_LENGTH (type) = 0;
15129 }
15130
15131 maybe_set_alignment (cu, die, type);
15132
15133 /* The enumeration DIE can be incomplete. In Ada, any type can be
15134 declared as private in the package spec, and then defined only
15135 inside the package body. Such types are known as Taft Amendment
15136 Types. When another package uses such a type, an incomplete DIE
15137 may be generated by the compiler. */
15138 if (die_is_declaration (die, cu))
15139 type->set_is_stub (true);
15140
15141 /* If this type has an underlying type that is not a stub, then we
15142 may use its attributes. We always use the "unsigned" attribute
15143 in this situation, because ordinarily we guess whether the type
15144 is unsigned -- but the guess can be wrong and the underlying type
15145 can tell us the reality. However, we defer to a local size
15146 attribute if one exists, because this lets the compiler override
15147 the underlying type if needed. */
15148 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
15149 {
15150 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15151 underlying_type = check_typedef (underlying_type);
15152
15153 type->set_is_unsigned (underlying_type->is_unsigned ());
15154
15155 if (TYPE_LENGTH (type) == 0)
15156 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15157
15158 if (TYPE_RAW_ALIGN (type) == 0
15159 && TYPE_RAW_ALIGN (underlying_type) != 0)
15160 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15161 }
15162
15163 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
15164
15165 set_die_type (die, type, cu);
15166
15167 /* Finish the creation of this type by using the enum's children.
15168 Note that, as usual, this must come after set_die_type to avoid
15169 infinite recursion when trying to compute the names of the
15170 enumerators. */
15171 update_enumeration_type_from_children (die, type, cu);
15172
15173 return type;
15174 }
15175
15176 /* Given a pointer to a die which begins an enumeration, process all
15177 the dies that define the members of the enumeration, and create the
15178 symbol for the enumeration type.
15179
15180 NOTE: We reverse the order of the element list. */
15181
15182 static void
15183 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15184 {
15185 struct type *this_type;
15186
15187 this_type = get_die_type (die, cu);
15188 if (this_type == NULL)
15189 this_type = read_enumeration_type (die, cu);
15190
15191 if (die->child != NULL)
15192 {
15193 struct die_info *child_die;
15194 const char *name;
15195
15196 child_die = die->child;
15197 while (child_die && child_die->tag)
15198 {
15199 if (child_die->tag != DW_TAG_enumerator)
15200 {
15201 process_die (child_die, cu);
15202 }
15203 else
15204 {
15205 name = dwarf2_name (child_die, cu);
15206 if (name)
15207 new_symbol (child_die, this_type, cu);
15208 }
15209
15210 child_die = child_die->sibling;
15211 }
15212 }
15213
15214 /* If we are reading an enum from a .debug_types unit, and the enum
15215 is a declaration, and the enum is not the signatured type in the
15216 unit, then we do not want to add a symbol for it. Adding a
15217 symbol would in some cases obscure the true definition of the
15218 enum, giving users an incomplete type when the definition is
15219 actually available. Note that we do not want to do this for all
15220 enums which are just declarations, because C++0x allows forward
15221 enum declarations. */
15222 if (cu->per_cu->is_debug_types
15223 && die_is_declaration (die, cu))
15224 {
15225 struct signatured_type *sig_type;
15226
15227 sig_type = (struct signatured_type *) cu->per_cu;
15228 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15229 if (sig_type->type_offset_in_section != die->sect_off)
15230 return;
15231 }
15232
15233 new_symbol (die, this_type, cu);
15234 }
15235
15236 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15237 expression for an index type and finds the corresponding field
15238 offset in the hidden "P_BOUNDS" structure. Returns true on success
15239 and updates *FIELD, false if it fails to recognize an
15240 expression. */
15241
15242 static bool
15243 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
15244 int *bounds_offset, struct field *field,
15245 struct dwarf2_cu *cu)
15246 {
15247 struct attribute *attr = dwarf2_attr (die, name, cu);
15248 if (attr == nullptr || !attr->form_is_block ())
15249 return false;
15250
15251 const struct dwarf_block *block = attr->as_block ();
15252 const gdb_byte *start = block->data;
15253 const gdb_byte *end = block->data + block->size;
15254
15255 /* The expression to recognize generally looks like:
15256
15257 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15258 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15259
15260 However, the second "plus_uconst" may be missing:
15261
15262 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15263 DW_OP_deref_size: 4)
15264
15265 This happens when the field is at the start of the structure.
15266
15267 Also, the final deref may not be sized:
15268
15269 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15270 DW_OP_deref)
15271
15272 This happens when the size of the index type happens to be the
15273 same as the architecture's word size. This can occur with or
15274 without the second plus_uconst. */
15275
15276 if (end - start < 2)
15277 return false;
15278 if (*start++ != DW_OP_push_object_address)
15279 return false;
15280 if (*start++ != DW_OP_plus_uconst)
15281 return false;
15282
15283 uint64_t this_bound_off;
15284 start = gdb_read_uleb128 (start, end, &this_bound_off);
15285 if (start == nullptr || (int) this_bound_off != this_bound_off)
15286 return false;
15287 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15288 is consistent among all bounds. */
15289 if (*bounds_offset == -1)
15290 *bounds_offset = this_bound_off;
15291 else if (*bounds_offset != this_bound_off)
15292 return false;
15293
15294 if (start == end || *start++ != DW_OP_deref)
15295 return false;
15296
15297 int offset = 0;
15298 if (start ==end)
15299 return false;
15300 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
15301 {
15302 /* This means an offset of 0. */
15303 }
15304 else if (*start++ != DW_OP_plus_uconst)
15305 return false;
15306 else
15307 {
15308 /* The size is the parameter to DW_OP_plus_uconst. */
15309 uint64_t val;
15310 start = gdb_read_uleb128 (start, end, &val);
15311 if (start == nullptr)
15312 return false;
15313 if ((int) val != val)
15314 return false;
15315 offset = val;
15316 }
15317
15318 if (start == end)
15319 return false;
15320
15321 uint64_t size;
15322 if (*start == DW_OP_deref_size)
15323 {
15324 start = gdb_read_uleb128 (start + 1, end, &size);
15325 if (start == nullptr)
15326 return false;
15327 }
15328 else if (*start == DW_OP_deref)
15329 {
15330 size = cu->header.addr_size;
15331 ++start;
15332 }
15333 else
15334 return false;
15335
15336 field->set_loc_bitpos (8 * offset);
15337 if (size != TYPE_LENGTH (field->type ()))
15338 FIELD_BITSIZE (*field) = 8 * size;
15339
15340 return true;
15341 }
15342
15343 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15344 some kinds of Ada arrays:
15345
15346 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15347 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15348 <11e0> DW_AT_data_location: 2 byte block: 97 6
15349 (DW_OP_push_object_address; DW_OP_deref)
15350 <11e3> DW_AT_type : <0x1173>
15351 <11e7> DW_AT_sibling : <0x1201>
15352 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15353 <11ec> DW_AT_type : <0x1206>
15354 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15355 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15356 DW_OP_deref_size: 4)
15357 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15358 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15359 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15360
15361 This actually represents a "thick pointer", which is a structure
15362 with two elements: one that is a pointer to the array data, and one
15363 that is a pointer to another structure; this second structure holds
15364 the array bounds.
15365
15366 This returns a new type on success, or nullptr if this didn't
15367 recognize the type. */
15368
15369 static struct type *
15370 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
15371 struct type *type)
15372 {
15373 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
15374 /* So far we've only seen this with block form. */
15375 if (attr == nullptr || !attr->form_is_block ())
15376 return nullptr;
15377
15378 /* Note that this will fail if the structure layout is changed by
15379 the compiler. However, we have no good way to recognize some
15380 other layout, because we don't know what expression the compiler
15381 might choose to emit should this happen. */
15382 struct dwarf_block *blk = attr->as_block ();
15383 if (blk->size != 2
15384 || blk->data[0] != DW_OP_push_object_address
15385 || blk->data[1] != DW_OP_deref)
15386 return nullptr;
15387
15388 int bounds_offset = -1;
15389 int max_align = -1;
15390 std::vector<struct field> range_fields;
15391 for (struct die_info *child_die = die->child;
15392 child_die;
15393 child_die = child_die->sibling)
15394 {
15395 if (child_die->tag == DW_TAG_subrange_type)
15396 {
15397 struct type *underlying = read_subrange_index_type (child_die, cu);
15398
15399 int this_align = type_align (underlying);
15400 if (this_align > max_align)
15401 max_align = this_align;
15402
15403 range_fields.emplace_back ();
15404 range_fields.emplace_back ();
15405
15406 struct field &lower = range_fields[range_fields.size () - 2];
15407 struct field &upper = range_fields[range_fields.size () - 1];
15408
15409 lower.set_type (underlying);
15410 FIELD_ARTIFICIAL (lower) = 1;
15411
15412 upper.set_type (underlying);
15413 FIELD_ARTIFICIAL (upper) = 1;
15414
15415 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
15416 &bounds_offset, &lower, cu)
15417 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
15418 &bounds_offset, &upper, cu))
15419 return nullptr;
15420 }
15421 }
15422
15423 /* This shouldn't really happen, but double-check that we found
15424 where the bounds are stored. */
15425 if (bounds_offset == -1)
15426 return nullptr;
15427
15428 struct objfile *objfile = cu->per_objfile->objfile;
15429 for (int i = 0; i < range_fields.size (); i += 2)
15430 {
15431 char name[20];
15432
15433 /* Set the name of each field in the bounds. */
15434 xsnprintf (name, sizeof (name), "LB%d", i / 2);
15435 range_fields[i].set_name (objfile->intern (name));
15436 xsnprintf (name, sizeof (name), "UB%d", i / 2);
15437 range_fields[i + 1].set_name (objfile->intern (name));
15438 }
15439
15440 struct type *bounds = alloc_type (objfile);
15441 bounds->set_code (TYPE_CODE_STRUCT);
15442
15443 bounds->set_num_fields (range_fields.size ());
15444 bounds->set_fields
15445 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
15446 * sizeof (struct field))));
15447 memcpy (bounds->fields (), range_fields.data (),
15448 bounds->num_fields () * sizeof (struct field));
15449
15450 int last_fieldno = range_fields.size () - 1;
15451 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
15452 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
15453 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
15454
15455 /* Rewrite the existing array type in place. Specifically, we
15456 remove any dynamic properties we might have read, and we replace
15457 the index types. */
15458 struct type *iter = type;
15459 for (int i = 0; i < range_fields.size (); i += 2)
15460 {
15461 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
15462 iter->main_type->dyn_prop_list = nullptr;
15463 iter->set_index_type
15464 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
15465 iter = TYPE_TARGET_TYPE (iter);
15466 }
15467
15468 struct type *result = alloc_type (objfile);
15469 result->set_code (TYPE_CODE_STRUCT);
15470
15471 result->set_num_fields (2);
15472 result->set_fields
15473 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
15474 * sizeof (struct field))));
15475
15476 /* The names are chosen to coincide with what the compiler does with
15477 -fgnat-encodings=all, which the Ada code in gdb already
15478 understands. */
15479 result->field (0).set_name ("P_ARRAY");
15480 result->field (0).set_type (lookup_pointer_type (type));
15481
15482 result->field (1).set_name ("P_BOUNDS");
15483 result->field (1).set_type (lookup_pointer_type (bounds));
15484 result->field (1).set_loc_bitpos (8 * bounds_offset);
15485
15486 result->set_name (type->name ());
15487 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
15488 + TYPE_LENGTH (result->field (1).type ()));
15489
15490 return result;
15491 }
15492
15493 /* Extract all information from a DW_TAG_array_type DIE and put it in
15494 the DIE's type field. For now, this only handles one dimensional
15495 arrays. */
15496
15497 static struct type *
15498 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15499 {
15500 struct objfile *objfile = cu->per_objfile->objfile;
15501 struct die_info *child_die;
15502 struct type *type;
15503 struct type *element_type, *range_type, *index_type;
15504 struct attribute *attr;
15505 const char *name;
15506 struct dynamic_prop *byte_stride_prop = NULL;
15507 unsigned int bit_stride = 0;
15508
15509 element_type = die_type (die, cu);
15510
15511 /* The die_type call above may have already set the type for this DIE. */
15512 type = get_die_type (die, cu);
15513 if (type)
15514 return type;
15515
15516 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15517 if (attr != NULL)
15518 {
15519 int stride_ok;
15520 struct type *prop_type = cu->addr_sized_int_type (false);
15521
15522 byte_stride_prop
15523 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15524 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15525 prop_type);
15526 if (!stride_ok)
15527 {
15528 complaint (_("unable to read array DW_AT_byte_stride "
15529 " - DIE at %s [in module %s]"),
15530 sect_offset_str (die->sect_off),
15531 objfile_name (cu->per_objfile->objfile));
15532 /* Ignore this attribute. We will likely not be able to print
15533 arrays of this type correctly, but there is little we can do
15534 to help if we cannot read the attribute's value. */
15535 byte_stride_prop = NULL;
15536 }
15537 }
15538
15539 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15540 if (attr != NULL)
15541 bit_stride = attr->constant_value (0);
15542
15543 /* Irix 6.2 native cc creates array types without children for
15544 arrays with unspecified length. */
15545 if (die->child == NULL)
15546 {
15547 index_type = objfile_type (objfile)->builtin_int;
15548 range_type = create_static_range_type (NULL, index_type, 0, -1);
15549 type = create_array_type_with_stride (NULL, element_type, range_type,
15550 byte_stride_prop, bit_stride);
15551 return set_die_type (die, type, cu);
15552 }
15553
15554 std::vector<struct type *> range_types;
15555 child_die = die->child;
15556 while (child_die && child_die->tag)
15557 {
15558 if (child_die->tag == DW_TAG_subrange_type
15559 || child_die->tag == DW_TAG_generic_subrange)
15560 {
15561 struct type *child_type = read_type_die (child_die, cu);
15562
15563 if (child_type != NULL)
15564 {
15565 /* The range type was succesfully read. Save it for the
15566 array type creation. */
15567 range_types.push_back (child_type);
15568 }
15569 }
15570 child_die = child_die->sibling;
15571 }
15572
15573 if (range_types.empty ())
15574 {
15575 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15576 sect_offset_str (die->sect_off),
15577 objfile_name (cu->per_objfile->objfile));
15578 return NULL;
15579 }
15580
15581 /* Dwarf2 dimensions are output from left to right, create the
15582 necessary array types in backwards order. */
15583
15584 type = element_type;
15585
15586 if (read_array_order (die, cu) == DW_ORD_col_major)
15587 {
15588 int i = 0;
15589
15590 while (i < range_types.size ())
15591 {
15592 type = create_array_type_with_stride (NULL, type, range_types[i++],
15593 byte_stride_prop, bit_stride);
15594 bit_stride = 0;
15595 byte_stride_prop = nullptr;
15596 }
15597 }
15598 else
15599 {
15600 size_t ndim = range_types.size ();
15601 while (ndim-- > 0)
15602 {
15603 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15604 byte_stride_prop, bit_stride);
15605 bit_stride = 0;
15606 byte_stride_prop = nullptr;
15607 }
15608 }
15609
15610 gdb_assert (type != element_type);
15611
15612 /* Understand Dwarf2 support for vector types (like they occur on
15613 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15614 array type. This is not part of the Dwarf2/3 standard yet, but a
15615 custom vendor extension. The main difference between a regular
15616 array and the vector variant is that vectors are passed by value
15617 to functions. */
15618 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15619 if (attr != nullptr)
15620 make_vector_type (type);
15621
15622 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15623 implementation may choose to implement triple vectors using this
15624 attribute. */
15625 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15626 if (attr != nullptr && attr->form_is_unsigned ())
15627 {
15628 if (attr->as_unsigned () >= TYPE_LENGTH (type))
15629 TYPE_LENGTH (type) = attr->as_unsigned ();
15630 else
15631 complaint (_("DW_AT_byte_size for array type smaller "
15632 "than the total size of elements"));
15633 }
15634
15635 name = dwarf2_name (die, cu);
15636 if (name)
15637 type->set_name (name);
15638
15639 maybe_set_alignment (cu, die, type);
15640
15641 struct type *replacement_type = nullptr;
15642 if (cu->per_cu->lang == language_ada)
15643 {
15644 replacement_type = quirk_ada_thick_pointer (die, cu, type);
15645 if (replacement_type != nullptr)
15646 type = replacement_type;
15647 }
15648
15649 /* Install the type in the die. */
15650 set_die_type (die, type, cu, replacement_type != nullptr);
15651
15652 /* set_die_type should be already done. */
15653 set_descriptive_type (type, die, cu);
15654
15655 return type;
15656 }
15657
15658 static enum dwarf_array_dim_ordering
15659 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15660 {
15661 struct attribute *attr;
15662
15663 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15664
15665 if (attr != nullptr)
15666 {
15667 LONGEST val = attr->constant_value (-1);
15668 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
15669 return (enum dwarf_array_dim_ordering) val;
15670 }
15671
15672 /* GNU F77 is a special case, as at 08/2004 array type info is the
15673 opposite order to the dwarf2 specification, but data is still
15674 laid out as per normal fortran.
15675
15676 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15677 version checking. */
15678
15679 if (cu->per_cu->lang == language_fortran
15680 && cu->producer && strstr (cu->producer, "GNU F77"))
15681 {
15682 return DW_ORD_row_major;
15683 }
15684
15685 switch (cu->language_defn->array_ordering ())
15686 {
15687 case array_column_major:
15688 return DW_ORD_col_major;
15689 case array_row_major:
15690 default:
15691 return DW_ORD_row_major;
15692 };
15693 }
15694
15695 /* Extract all information from a DW_TAG_set_type DIE and put it in
15696 the DIE's type field. */
15697
15698 static struct type *
15699 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15700 {
15701 struct type *domain_type, *set_type;
15702 struct attribute *attr;
15703
15704 domain_type = die_type (die, cu);
15705
15706 /* The die_type call above may have already set the type for this DIE. */
15707 set_type = get_die_type (die, cu);
15708 if (set_type)
15709 return set_type;
15710
15711 set_type = create_set_type (NULL, domain_type);
15712
15713 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15714 if (attr != nullptr && attr->form_is_unsigned ())
15715 TYPE_LENGTH (set_type) = attr->as_unsigned ();
15716
15717 maybe_set_alignment (cu, die, set_type);
15718
15719 return set_die_type (die, set_type, cu);
15720 }
15721
15722 /* A helper for read_common_block that creates a locexpr baton.
15723 SYM is the symbol which we are marking as computed.
15724 COMMON_DIE is the DIE for the common block.
15725 COMMON_LOC is the location expression attribute for the common
15726 block itself.
15727 MEMBER_LOC is the location expression attribute for the particular
15728 member of the common block that we are processing.
15729 CU is the CU from which the above come. */
15730
15731 static void
15732 mark_common_block_symbol_computed (struct symbol *sym,
15733 struct die_info *common_die,
15734 struct attribute *common_loc,
15735 struct attribute *member_loc,
15736 struct dwarf2_cu *cu)
15737 {
15738 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15739 struct objfile *objfile = per_objfile->objfile;
15740 struct dwarf2_locexpr_baton *baton;
15741 gdb_byte *ptr;
15742 unsigned int cu_off;
15743 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
15744 LONGEST offset = 0;
15745
15746 gdb_assert (common_loc && member_loc);
15747 gdb_assert (common_loc->form_is_block ());
15748 gdb_assert (member_loc->form_is_block ()
15749 || member_loc->form_is_constant ());
15750
15751 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15752 baton->per_objfile = per_objfile;
15753 baton->per_cu = cu->per_cu;
15754 gdb_assert (baton->per_cu);
15755
15756 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15757
15758 if (member_loc->form_is_constant ())
15759 {
15760 offset = member_loc->constant_value (0);
15761 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15762 }
15763 else
15764 baton->size += member_loc->as_block ()->size;
15765
15766 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15767 baton->data = ptr;
15768
15769 *ptr++ = DW_OP_call4;
15770 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15771 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15772 ptr += 4;
15773
15774 if (member_loc->form_is_constant ())
15775 {
15776 *ptr++ = DW_OP_addr;
15777 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15778 ptr += cu->header.addr_size;
15779 }
15780 else
15781 {
15782 /* We have to copy the data here, because DW_OP_call4 will only
15783 use a DW_AT_location attribute. */
15784 struct dwarf_block *block = member_loc->as_block ();
15785 memcpy (ptr, block->data, block->size);
15786 ptr += block->size;
15787 }
15788
15789 *ptr++ = DW_OP_plus;
15790 gdb_assert (ptr - baton->data == baton->size);
15791
15792 SYMBOL_LOCATION_BATON (sym) = baton;
15793 sym->set_aclass_index (dwarf2_locexpr_index);
15794 }
15795
15796 /* Create appropriate locally-scoped variables for all the
15797 DW_TAG_common_block entries. Also create a struct common_block
15798 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15799 is used to separate the common blocks name namespace from regular
15800 variable names. */
15801
15802 static void
15803 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15804 {
15805 struct attribute *attr;
15806
15807 attr = dwarf2_attr (die, DW_AT_location, cu);
15808 if (attr != nullptr)
15809 {
15810 /* Support the .debug_loc offsets. */
15811 if (attr->form_is_block ())
15812 {
15813 /* Ok. */
15814 }
15815 else if (attr->form_is_section_offset ())
15816 {
15817 dwarf2_complex_location_expr_complaint ();
15818 attr = NULL;
15819 }
15820 else
15821 {
15822 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15823 "common block member");
15824 attr = NULL;
15825 }
15826 }
15827
15828 if (die->child != NULL)
15829 {
15830 struct objfile *objfile = cu->per_objfile->objfile;
15831 struct die_info *child_die;
15832 size_t n_entries = 0, size;
15833 struct common_block *common_block;
15834 struct symbol *sym;
15835
15836 for (child_die = die->child;
15837 child_die && child_die->tag;
15838 child_die = child_die->sibling)
15839 ++n_entries;
15840
15841 size = (sizeof (struct common_block)
15842 + (n_entries - 1) * sizeof (struct symbol *));
15843 common_block
15844 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15845 size);
15846 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15847 common_block->n_entries = 0;
15848
15849 for (child_die = die->child;
15850 child_die && child_die->tag;
15851 child_die = child_die->sibling)
15852 {
15853 /* Create the symbol in the DW_TAG_common_block block in the current
15854 symbol scope. */
15855 sym = new_symbol (child_die, NULL, cu);
15856 if (sym != NULL)
15857 {
15858 struct attribute *member_loc;
15859
15860 common_block->contents[common_block->n_entries++] = sym;
15861
15862 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15863 cu);
15864 if (member_loc)
15865 {
15866 /* GDB has handled this for a long time, but it is
15867 not specified by DWARF. It seems to have been
15868 emitted by gfortran at least as recently as:
15869 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15870 complaint (_("Variable in common block has "
15871 "DW_AT_data_member_location "
15872 "- DIE at %s [in module %s]"),
15873 sect_offset_str (child_die->sect_off),
15874 objfile_name (objfile));
15875
15876 if (member_loc->form_is_section_offset ())
15877 dwarf2_complex_location_expr_complaint ();
15878 else if (member_loc->form_is_constant ()
15879 || member_loc->form_is_block ())
15880 {
15881 if (attr != nullptr)
15882 mark_common_block_symbol_computed (sym, die, attr,
15883 member_loc, cu);
15884 }
15885 else
15886 dwarf2_complex_location_expr_complaint ();
15887 }
15888 }
15889 }
15890
15891 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15892 sym->set_value_common_block (common_block);
15893 }
15894 }
15895
15896 /* Create a type for a C++ namespace. */
15897
15898 static struct type *
15899 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15900 {
15901 struct objfile *objfile = cu->per_objfile->objfile;
15902 const char *previous_prefix, *name;
15903 int is_anonymous;
15904 struct type *type;
15905
15906 /* For extensions, reuse the type of the original namespace. */
15907 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15908 {
15909 struct die_info *ext_die;
15910 struct dwarf2_cu *ext_cu = cu;
15911
15912 ext_die = dwarf2_extension (die, &ext_cu);
15913 type = read_type_die (ext_die, ext_cu);
15914
15915 /* EXT_CU may not be the same as CU.
15916 Ensure TYPE is recorded with CU in die_type_hash. */
15917 return set_die_type (die, type, cu);
15918 }
15919
15920 name = namespace_name (die, &is_anonymous, cu);
15921
15922 /* Now build the name of the current namespace. */
15923
15924 previous_prefix = determine_prefix (die, cu);
15925 if (previous_prefix[0] != '\0')
15926 name = typename_concat (&objfile->objfile_obstack,
15927 previous_prefix, name, 0, cu);
15928
15929 /* Create the type. */
15930 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
15931
15932 return set_die_type (die, type, cu);
15933 }
15934
15935 /* Read a namespace scope. */
15936
15937 static void
15938 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
15939 {
15940 struct objfile *objfile = cu->per_objfile->objfile;
15941 int is_anonymous;
15942
15943 /* Add a symbol associated to this if we haven't seen the namespace
15944 before. Also, add a using directive if it's an anonymous
15945 namespace. */
15946
15947 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
15948 {
15949 struct type *type;
15950
15951 type = read_type_die (die, cu);
15952 new_symbol (die, type, cu);
15953
15954 namespace_name (die, &is_anonymous, cu);
15955 if (is_anonymous)
15956 {
15957 const char *previous_prefix = determine_prefix (die, cu);
15958
15959 std::vector<const char *> excludes;
15960 add_using_directive (using_directives (cu),
15961 previous_prefix, type->name (), NULL,
15962 NULL, excludes, 0, &objfile->objfile_obstack);
15963 }
15964 }
15965
15966 if (die->child != NULL)
15967 {
15968 struct die_info *child_die = die->child;
15969
15970 while (child_die && child_die->tag)
15971 {
15972 process_die (child_die, cu);
15973 child_die = child_die->sibling;
15974 }
15975 }
15976 }
15977
15978 /* Read a Fortran module as type. This DIE can be only a declaration used for
15979 imported module. Still we need that type as local Fortran "use ... only"
15980 declaration imports depend on the created type in determine_prefix. */
15981
15982 static struct type *
15983 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
15984 {
15985 struct objfile *objfile = cu->per_objfile->objfile;
15986 const char *module_name;
15987 struct type *type;
15988
15989 module_name = dwarf2_name (die, cu);
15990 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
15991
15992 return set_die_type (die, type, cu);
15993 }
15994
15995 /* Read a Fortran module. */
15996
15997 static void
15998 read_module (struct die_info *die, struct dwarf2_cu *cu)
15999 {
16000 struct die_info *child_die = die->child;
16001 struct type *type;
16002
16003 type = read_type_die (die, cu);
16004 new_symbol (die, type, cu);
16005
16006 while (child_die && child_die->tag)
16007 {
16008 process_die (child_die, cu);
16009 child_die = child_die->sibling;
16010 }
16011 }
16012
16013 /* Return the name of the namespace represented by DIE. Set
16014 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16015 namespace. */
16016
16017 static const char *
16018 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16019 {
16020 struct die_info *current_die;
16021 const char *name = NULL;
16022
16023 /* Loop through the extensions until we find a name. */
16024
16025 for (current_die = die;
16026 current_die != NULL;
16027 current_die = dwarf2_extension (die, &cu))
16028 {
16029 /* We don't use dwarf2_name here so that we can detect the absence
16030 of a name -> anonymous namespace. */
16031 name = dwarf2_string_attr (die, DW_AT_name, cu);
16032
16033 if (name != NULL)
16034 break;
16035 }
16036
16037 /* Is it an anonymous namespace? */
16038
16039 *is_anonymous = (name == NULL);
16040 if (*is_anonymous)
16041 name = CP_ANONYMOUS_NAMESPACE_STR;
16042
16043 return name;
16044 }
16045
16046 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16047 the user defined type vector. */
16048
16049 static struct type *
16050 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16051 {
16052 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16053 struct comp_unit_head *cu_header = &cu->header;
16054 struct type *type;
16055 struct attribute *attr_byte_size;
16056 struct attribute *attr_address_class;
16057 int byte_size, addr_class;
16058 struct type *target_type;
16059
16060 target_type = die_type (die, cu);
16061
16062 /* The die_type call above may have already set the type for this DIE. */
16063 type = get_die_type (die, cu);
16064 if (type)
16065 return type;
16066
16067 type = lookup_pointer_type (target_type);
16068
16069 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16070 if (attr_byte_size)
16071 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
16072 else
16073 byte_size = cu_header->addr_size;
16074
16075 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16076 if (attr_address_class)
16077 addr_class = attr_address_class->constant_value (DW_ADDR_none);
16078 else
16079 addr_class = DW_ADDR_none;
16080
16081 ULONGEST alignment = get_alignment (cu, die);
16082
16083 /* If the pointer size, alignment, or address class is different
16084 than the default, create a type variant marked as such and set
16085 the length accordingly. */
16086 if (TYPE_LENGTH (type) != byte_size
16087 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16088 && alignment != TYPE_RAW_ALIGN (type))
16089 || addr_class != DW_ADDR_none)
16090 {
16091 if (gdbarch_address_class_type_flags_p (gdbarch))
16092 {
16093 type_instance_flags type_flags
16094 = gdbarch_address_class_type_flags (gdbarch, byte_size,
16095 addr_class);
16096 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16097 == 0);
16098 type = make_type_with_address_space (type, type_flags);
16099 }
16100 else if (TYPE_LENGTH (type) != byte_size)
16101 {
16102 complaint (_("invalid pointer size %d"), byte_size);
16103 }
16104 else if (TYPE_RAW_ALIGN (type) != alignment)
16105 {
16106 complaint (_("Invalid DW_AT_alignment"
16107 " - DIE at %s [in module %s]"),
16108 sect_offset_str (die->sect_off),
16109 objfile_name (cu->per_objfile->objfile));
16110 }
16111 else
16112 {
16113 /* Should we also complain about unhandled address classes? */
16114 }
16115 }
16116
16117 TYPE_LENGTH (type) = byte_size;
16118 set_type_align (type, alignment);
16119 return set_die_type (die, type, cu);
16120 }
16121
16122 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16123 the user defined type vector. */
16124
16125 static struct type *
16126 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16127 {
16128 struct type *type;
16129 struct type *to_type;
16130 struct type *domain;
16131
16132 to_type = die_type (die, cu);
16133 domain = die_containing_type (die, cu);
16134
16135 /* The calls above may have already set the type for this DIE. */
16136 type = get_die_type (die, cu);
16137 if (type)
16138 return type;
16139
16140 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16141 type = lookup_methodptr_type (to_type);
16142 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16143 {
16144 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16145
16146 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16147 to_type->fields (), to_type->num_fields (),
16148 to_type->has_varargs ());
16149 type = lookup_methodptr_type (new_type);
16150 }
16151 else
16152 type = lookup_memberptr_type (to_type, domain);
16153
16154 return set_die_type (die, type, cu);
16155 }
16156
16157 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16158 the user defined type vector. */
16159
16160 static struct type *
16161 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16162 enum type_code refcode)
16163 {
16164 struct comp_unit_head *cu_header = &cu->header;
16165 struct type *type, *target_type;
16166 struct attribute *attr;
16167
16168 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16169
16170 target_type = die_type (die, cu);
16171
16172 /* The die_type call above may have already set the type for this DIE. */
16173 type = get_die_type (die, cu);
16174 if (type)
16175 return type;
16176
16177 type = lookup_reference_type (target_type, refcode);
16178 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16179 if (attr != nullptr)
16180 {
16181 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
16182 }
16183 else
16184 {
16185 TYPE_LENGTH (type) = cu_header->addr_size;
16186 }
16187 maybe_set_alignment (cu, die, type);
16188 return set_die_type (die, type, cu);
16189 }
16190
16191 /* Add the given cv-qualifiers to the element type of the array. GCC
16192 outputs DWARF type qualifiers that apply to an array, not the
16193 element type. But GDB relies on the array element type to carry
16194 the cv-qualifiers. This mimics section 6.7.3 of the C99
16195 specification. */
16196
16197 static struct type *
16198 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16199 struct type *base_type, int cnst, int voltl)
16200 {
16201 struct type *el_type, *inner_array;
16202
16203 base_type = copy_type (base_type);
16204 inner_array = base_type;
16205
16206 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16207 {
16208 TYPE_TARGET_TYPE (inner_array) =
16209 copy_type (TYPE_TARGET_TYPE (inner_array));
16210 inner_array = TYPE_TARGET_TYPE (inner_array);
16211 }
16212
16213 el_type = TYPE_TARGET_TYPE (inner_array);
16214 cnst |= TYPE_CONST (el_type);
16215 voltl |= TYPE_VOLATILE (el_type);
16216 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16217
16218 return set_die_type (die, base_type, cu);
16219 }
16220
16221 static struct type *
16222 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16223 {
16224 struct type *base_type, *cv_type;
16225
16226 base_type = die_type (die, cu);
16227
16228 /* The die_type call above may have already set the type for this DIE. */
16229 cv_type = get_die_type (die, cu);
16230 if (cv_type)
16231 return cv_type;
16232
16233 /* In case the const qualifier is applied to an array type, the element type
16234 is so qualified, not the array type (section 6.7.3 of C99). */
16235 if (base_type->code () == TYPE_CODE_ARRAY)
16236 return add_array_cv_type (die, cu, base_type, 1, 0);
16237
16238 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16239 return set_die_type (die, cv_type, cu);
16240 }
16241
16242 static struct type *
16243 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16244 {
16245 struct type *base_type, *cv_type;
16246
16247 base_type = die_type (die, cu);
16248
16249 /* The die_type call above may have already set the type for this DIE. */
16250 cv_type = get_die_type (die, cu);
16251 if (cv_type)
16252 return cv_type;
16253
16254 /* In case the volatile qualifier is applied to an array type, the
16255 element type is so qualified, not the array type (section 6.7.3
16256 of C99). */
16257 if (base_type->code () == TYPE_CODE_ARRAY)
16258 return add_array_cv_type (die, cu, base_type, 0, 1);
16259
16260 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16261 return set_die_type (die, cv_type, cu);
16262 }
16263
16264 /* Handle DW_TAG_restrict_type. */
16265
16266 static struct type *
16267 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16268 {
16269 struct type *base_type, *cv_type;
16270
16271 base_type = die_type (die, cu);
16272
16273 /* The die_type call above may have already set the type for this DIE. */
16274 cv_type = get_die_type (die, cu);
16275 if (cv_type)
16276 return cv_type;
16277
16278 cv_type = make_restrict_type (base_type);
16279 return set_die_type (die, cv_type, cu);
16280 }
16281
16282 /* Handle DW_TAG_atomic_type. */
16283
16284 static struct type *
16285 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16286 {
16287 struct type *base_type, *cv_type;
16288
16289 base_type = die_type (die, cu);
16290
16291 /* The die_type call above may have already set the type for this DIE. */
16292 cv_type = get_die_type (die, cu);
16293 if (cv_type)
16294 return cv_type;
16295
16296 cv_type = make_atomic_type (base_type);
16297 return set_die_type (die, cv_type, cu);
16298 }
16299
16300 /* Extract all information from a DW_TAG_string_type DIE and add to
16301 the user defined type vector. It isn't really a user defined type,
16302 but it behaves like one, with other DIE's using an AT_user_def_type
16303 attribute to reference it. */
16304
16305 static struct type *
16306 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16307 {
16308 struct objfile *objfile = cu->per_objfile->objfile;
16309 struct gdbarch *gdbarch = objfile->arch ();
16310 struct type *type, *range_type, *index_type, *char_type;
16311 struct attribute *attr;
16312 struct dynamic_prop prop;
16313 bool length_is_constant = true;
16314 LONGEST length;
16315
16316 /* There are a couple of places where bit sizes might be made use of
16317 when parsing a DW_TAG_string_type, however, no producer that we know
16318 of make use of these. Handling bit sizes that are a multiple of the
16319 byte size is easy enough, but what about other bit sizes? Lets deal
16320 with that problem when we have to. Warn about these attributes being
16321 unsupported, then parse the type and ignore them like we always
16322 have. */
16323 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16324 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16325 {
16326 static bool warning_printed = false;
16327 if (!warning_printed)
16328 {
16329 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16330 "currently supported on DW_TAG_string_type."));
16331 warning_printed = true;
16332 }
16333 }
16334
16335 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16336 if (attr != nullptr && !attr->form_is_constant ())
16337 {
16338 /* The string length describes the location at which the length of
16339 the string can be found. The size of the length field can be
16340 specified with one of the attributes below. */
16341 struct type *prop_type;
16342 struct attribute *len
16343 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16344 if (len == nullptr)
16345 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16346 if (len != nullptr && len->form_is_constant ())
16347 {
16348 /* Pass 0 as the default as we know this attribute is constant
16349 and the default value will not be returned. */
16350 LONGEST sz = len->constant_value (0);
16351 prop_type = objfile_int_type (objfile, sz, true);
16352 }
16353 else
16354 {
16355 /* If the size is not specified then we assume it is the size of
16356 an address on this target. */
16357 prop_type = cu->addr_sized_int_type (true);
16358 }
16359
16360 /* Convert the attribute into a dynamic property. */
16361 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16362 length = 1;
16363 else
16364 length_is_constant = false;
16365 }
16366 else if (attr != nullptr)
16367 {
16368 /* This DW_AT_string_length just contains the length with no
16369 indirection. There's no need to create a dynamic property in this
16370 case. Pass 0 for the default value as we know it will not be
16371 returned in this case. */
16372 length = attr->constant_value (0);
16373 }
16374 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16375 {
16376 /* We don't currently support non-constant byte sizes for strings. */
16377 length = attr->constant_value (1);
16378 }
16379 else
16380 {
16381 /* Use 1 as a fallback length if we have nothing else. */
16382 length = 1;
16383 }
16384
16385 index_type = objfile_type (objfile)->builtin_int;
16386 if (length_is_constant)
16387 range_type = create_static_range_type (NULL, index_type, 1, length);
16388 else
16389 {
16390 struct dynamic_prop low_bound;
16391
16392 low_bound.set_const_val (1);
16393 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16394 }
16395 char_type = language_string_char_type (cu->language_defn, gdbarch);
16396 type = create_string_type (NULL, char_type, range_type);
16397
16398 return set_die_type (die, type, cu);
16399 }
16400
16401 /* Assuming that DIE corresponds to a function, returns nonzero
16402 if the function is prototyped. */
16403
16404 static int
16405 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16406 {
16407 struct attribute *attr;
16408
16409 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16410 if (attr && attr->as_boolean ())
16411 return 1;
16412
16413 /* The DWARF standard implies that the DW_AT_prototyped attribute
16414 is only meaningful for C, but the concept also extends to other
16415 languages that allow unprototyped functions (Eg: Objective C).
16416 For all other languages, assume that functions are always
16417 prototyped. */
16418 if (cu->per_cu->lang != language_c
16419 && cu->per_cu->lang != language_objc
16420 && cu->per_cu->lang != language_opencl)
16421 return 1;
16422
16423 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16424 prototyped and unprototyped functions; default to prototyped,
16425 since that is more common in modern code (and RealView warns
16426 about unprototyped functions). */
16427 if (producer_is_realview (cu->producer))
16428 return 1;
16429
16430 return 0;
16431 }
16432
16433 /* Handle DIES due to C code like:
16434
16435 struct foo
16436 {
16437 int (*funcp)(int a, long l);
16438 int b;
16439 };
16440
16441 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16442
16443 static struct type *
16444 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16445 {
16446 struct objfile *objfile = cu->per_objfile->objfile;
16447 struct type *type; /* Type that this function returns. */
16448 struct type *ftype; /* Function that returns above type. */
16449 struct attribute *attr;
16450
16451 type = die_type (die, cu);
16452
16453 /* The die_type call above may have already set the type for this DIE. */
16454 ftype = get_die_type (die, cu);
16455 if (ftype)
16456 return ftype;
16457
16458 ftype = lookup_function_type (type);
16459
16460 if (prototyped_function_p (die, cu))
16461 ftype->set_is_prototyped (true);
16462
16463 /* Store the calling convention in the type if it's available in
16464 the subroutine die. Otherwise set the calling convention to
16465 the default value DW_CC_normal. */
16466 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16467 if (attr != nullptr
16468 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
16469 TYPE_CALLING_CONVENTION (ftype)
16470 = (enum dwarf_calling_convention) attr->constant_value (0);
16471 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16472 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16473 else
16474 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16475
16476 /* Record whether the function returns normally to its caller or not
16477 if the DWARF producer set that information. */
16478 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16479 if (attr && attr->as_boolean ())
16480 TYPE_NO_RETURN (ftype) = 1;
16481
16482 /* We need to add the subroutine type to the die immediately so
16483 we don't infinitely recurse when dealing with parameters
16484 declared as the same subroutine type. */
16485 set_die_type (die, ftype, cu);
16486
16487 if (die->child != NULL)
16488 {
16489 struct type *void_type = objfile_type (objfile)->builtin_void;
16490 struct die_info *child_die;
16491 int nparams, iparams;
16492
16493 /* Count the number of parameters.
16494 FIXME: GDB currently ignores vararg functions, but knows about
16495 vararg member functions. */
16496 nparams = 0;
16497 child_die = die->child;
16498 while (child_die && child_die->tag)
16499 {
16500 if (child_die->tag == DW_TAG_formal_parameter)
16501 nparams++;
16502 else if (child_die->tag == DW_TAG_unspecified_parameters)
16503 ftype->set_has_varargs (true);
16504
16505 child_die = child_die->sibling;
16506 }
16507
16508 /* Allocate storage for parameters and fill them in. */
16509 ftype->set_num_fields (nparams);
16510 ftype->set_fields
16511 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
16512
16513 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16514 even if we error out during the parameters reading below. */
16515 for (iparams = 0; iparams < nparams; iparams++)
16516 ftype->field (iparams).set_type (void_type);
16517
16518 iparams = 0;
16519 child_die = die->child;
16520 while (child_die && child_die->tag)
16521 {
16522 if (child_die->tag == DW_TAG_formal_parameter)
16523 {
16524 struct type *arg_type;
16525
16526 /* DWARF version 2 has no clean way to discern C++
16527 static and non-static member functions. G++ helps
16528 GDB by marking the first parameter for non-static
16529 member functions (which is the this pointer) as
16530 artificial. We pass this information to
16531 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16532
16533 DWARF version 3 added DW_AT_object_pointer, which GCC
16534 4.5 does not yet generate. */
16535 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16536 if (attr != nullptr)
16537 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
16538 else
16539 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16540 arg_type = die_type (child_die, cu);
16541
16542 /* RealView does not mark THIS as const, which the testsuite
16543 expects. GCC marks THIS as const in method definitions,
16544 but not in the class specifications (GCC PR 43053). */
16545 if (cu->per_cu->lang == language_cplus
16546 && !TYPE_CONST (arg_type)
16547 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16548 {
16549 int is_this = 0;
16550 struct dwarf2_cu *arg_cu = cu;
16551 const char *name = dwarf2_name (child_die, cu);
16552
16553 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16554 if (attr != nullptr)
16555 {
16556 /* If the compiler emits this, use it. */
16557 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16558 is_this = 1;
16559 }
16560 else if (name && strcmp (name, "this") == 0)
16561 /* Function definitions will have the argument names. */
16562 is_this = 1;
16563 else if (name == NULL && iparams == 0)
16564 /* Declarations may not have the names, so like
16565 elsewhere in GDB, assume an artificial first
16566 argument is "this". */
16567 is_this = 1;
16568
16569 if (is_this)
16570 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16571 arg_type, 0);
16572 }
16573
16574 ftype->field (iparams).set_type (arg_type);
16575 iparams++;
16576 }
16577 child_die = child_die->sibling;
16578 }
16579 }
16580
16581 return ftype;
16582 }
16583
16584 static struct type *
16585 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16586 {
16587 struct objfile *objfile = cu->per_objfile->objfile;
16588 const char *name = NULL;
16589 struct type *this_type, *target_type;
16590
16591 name = dwarf2_full_name (NULL, die, cu);
16592 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16593 this_type->set_target_is_stub (true);
16594 set_die_type (die, this_type, cu);
16595 target_type = die_type (die, cu);
16596 if (target_type != this_type)
16597 TYPE_TARGET_TYPE (this_type) = target_type;
16598 else
16599 {
16600 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16601 spec and cause infinite loops in GDB. */
16602 complaint (_("Self-referential DW_TAG_typedef "
16603 "- DIE at %s [in module %s]"),
16604 sect_offset_str (die->sect_off), objfile_name (objfile));
16605 TYPE_TARGET_TYPE (this_type) = NULL;
16606 }
16607 if (name == NULL)
16608 {
16609 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16610 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16611 Handle these by just returning the target type, rather than
16612 constructing an anonymous typedef type and trying to handle this
16613 elsewhere. */
16614 set_die_type (die, target_type, cu);
16615 return target_type;
16616 }
16617 return this_type;
16618 }
16619
16620 /* Helper for get_dwarf2_rational_constant that computes the value of
16621 a given gmp_mpz given an attribute. */
16622
16623 static void
16624 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
16625 {
16626 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16627 location expression that pushes an implicit value. */
16628 if (attr->form == DW_FORM_exprloc)
16629 {
16630 dwarf_block *blk = attr->as_block ();
16631 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
16632 {
16633 uint64_t len;
16634 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
16635 blk->data + blk->size,
16636 &len);
16637 if (ptr - blk->data + len <= blk->size)
16638 {
16639 mpz_import (value->val, len,
16640 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
16641 1, 0, 0, ptr);
16642 return;
16643 }
16644 }
16645
16646 /* On failure set it to 1. */
16647 *value = gdb_mpz (1);
16648 }
16649 else if (attr->form_is_block ())
16650 {
16651 dwarf_block *blk = attr->as_block ();
16652 mpz_import (value->val, blk->size,
16653 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
16654 1, 0, 0, blk->data);
16655 }
16656 else
16657 *value = gdb_mpz (attr->constant_value (1));
16658 }
16659
16660 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16661 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16662
16663 If the numerator and/or numerator attribute is missing,
16664 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16665 untouched. */
16666
16667 static void
16668 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
16669 gdb_mpz *numerator, gdb_mpz *denominator)
16670 {
16671 struct attribute *num_attr, *denom_attr;
16672
16673 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
16674 if (num_attr == nullptr)
16675 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16676 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16677
16678 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
16679 if (denom_attr == nullptr)
16680 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16681 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16682
16683 if (num_attr == nullptr || denom_attr == nullptr)
16684 return;
16685
16686 get_mpz (cu, numerator, num_attr);
16687 get_mpz (cu, denominator, denom_attr);
16688 }
16689
16690 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16691 rational constant, rather than a signed one.
16692
16693 If the rational constant has a negative value, a complaint
16694 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16695
16696 static void
16697 get_dwarf2_unsigned_rational_constant (struct die_info *die,
16698 struct dwarf2_cu *cu,
16699 gdb_mpz *numerator,
16700 gdb_mpz *denominator)
16701 {
16702 gdb_mpz num (1);
16703 gdb_mpz denom (1);
16704
16705 get_dwarf2_rational_constant (die, cu, &num, &denom);
16706 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
16707 {
16708 mpz_neg (num.val, num.val);
16709 mpz_neg (denom.val, denom.val);
16710 }
16711 else if (mpz_sgn (num.val) == -1)
16712 {
16713 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16714 " in DIE at %s"),
16715 sect_offset_str (die->sect_off));
16716 return;
16717 }
16718 else if (mpz_sgn (denom.val) == -1)
16719 {
16720 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16721 " in DIE at %s"),
16722 sect_offset_str (die->sect_off));
16723 return;
16724 }
16725
16726 *numerator = std::move (num);
16727 *denominator = std::move (denom);
16728 }
16729
16730 /* Assuming that ENCODING is a string whose contents starting at the
16731 K'th character is "_nn" where "nn" is a decimal number, scan that
16732 number and set RESULT to the value. K is updated to point to the
16733 character immediately following the number.
16734
16735 If the string does not conform to the format described above, false
16736 is returned, and K may or may not be changed. */
16737
16738 static bool
16739 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
16740 {
16741 /* The next character should be an underscore ('_') followed
16742 by a digit. */
16743 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
16744 return false;
16745
16746 /* Skip the underscore. */
16747 k++;
16748 int start = k;
16749
16750 /* Determine the number of digits for our number. */
16751 while (isdigit (encoding[k]))
16752 k++;
16753 if (k == start)
16754 return false;
16755
16756 std::string copy (&encoding[start], k - start);
16757 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
16758 return false;
16759
16760 return true;
16761 }
16762
16763 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16764 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16765 DENOM, update OFFSET, and return true on success. Return false on
16766 failure. */
16767
16768 static bool
16769 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
16770 gdb_mpz *num, gdb_mpz *denom)
16771 {
16772 if (!ada_get_gnat_encoded_number (encoding, offset, num))
16773 return false;
16774 return ada_get_gnat_encoded_number (encoding, offset, denom);
16775 }
16776
16777 /* Assuming DIE corresponds to a fixed point type, finish the creation
16778 of the corresponding TYPE by setting its type-specific data. CU is
16779 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16780 encodings. It is nullptr if the GNAT encoding should be
16781 ignored. */
16782
16783 static void
16784 finish_fixed_point_type (struct type *type, const char *suffix,
16785 struct die_info *die, struct dwarf2_cu *cu)
16786 {
16787 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
16788 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
16789
16790 /* If GNAT encodings are preferred, don't examine the
16791 attributes. */
16792 struct attribute *attr = nullptr;
16793 if (suffix == nullptr)
16794 {
16795 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
16796 if (attr == nullptr)
16797 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
16798 if (attr == nullptr)
16799 attr = dwarf2_attr (die, DW_AT_small, cu);
16800 }
16801
16802 /* Numerator and denominator of our fixed-point type's scaling factor.
16803 The default is a scaling factor of 1, which we use as a fallback
16804 when we are not able to decode it (problem with the debugging info,
16805 unsupported forms, bug in GDB, etc...). Using that as the default
16806 allows us to at least print the unscaled value, which might still
16807 be useful to a user. */
16808 gdb_mpz scale_num (1);
16809 gdb_mpz scale_denom (1);
16810
16811 if (attr == nullptr)
16812 {
16813 int offset = 0;
16814 if (suffix != nullptr
16815 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16816 &scale_denom)
16817 /* The number might be encoded as _nn_dd_nn_dd, where the
16818 second ratio is the 'small value. In this situation, we
16819 want the second value. */
16820 && (suffix[offset] != '_'
16821 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16822 &scale_denom)))
16823 {
16824 /* Found it. */
16825 }
16826 else
16827 {
16828 /* Scaling factor not found. Assume a scaling factor of 1,
16829 and hope for the best. At least the user will be able to
16830 see the encoded value. */
16831 scale_num = 1;
16832 scale_denom = 1;
16833 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16834 sect_offset_str (die->sect_off));
16835 }
16836 }
16837 else if (attr->name == DW_AT_binary_scale)
16838 {
16839 LONGEST scale_exp = attr->constant_value (0);
16840 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16841
16842 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
16843 }
16844 else if (attr->name == DW_AT_decimal_scale)
16845 {
16846 LONGEST scale_exp = attr->constant_value (0);
16847 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16848
16849 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
16850 }
16851 else if (attr->name == DW_AT_small)
16852 {
16853 struct die_info *scale_die;
16854 struct dwarf2_cu *scale_cu = cu;
16855
16856 scale_die = follow_die_ref (die, attr, &scale_cu);
16857 if (scale_die->tag == DW_TAG_constant)
16858 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
16859 &scale_num, &scale_denom);
16860 else
16861 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16862 " (DIE at %s)"),
16863 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16864 }
16865 else
16866 {
16867 complaint (_("unsupported scale attribute %s for fixed-point type"
16868 " (DIE at %s)"),
16869 dwarf_attr_name (attr->name),
16870 sect_offset_str (die->sect_off));
16871 }
16872
16873 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
16874 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
16875 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
16876 mpq_canonicalize (scaling_factor.val);
16877 }
16878
16879 /* The gnat-encoding suffix for fixed point. */
16880
16881 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16882
16883 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16884 "XF" suffix of the name. The text after this is what encodes the
16885 'small and 'delta information. Otherwise, return nullptr. */
16886
16887 static const char *
16888 gnat_encoded_fixed_point_type_info (const char *name)
16889 {
16890 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
16891 }
16892
16893 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16894 (which may be different from NAME) to the architecture back-end to allow
16895 it to guess the correct format if necessary. */
16896
16897 static struct type *
16898 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16899 const char *name_hint, enum bfd_endian byte_order)
16900 {
16901 struct gdbarch *gdbarch = objfile->arch ();
16902 const struct floatformat **format;
16903 struct type *type;
16904
16905 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16906 if (format)
16907 type = init_float_type (objfile, bits, name, format, byte_order);
16908 else
16909 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16910
16911 return type;
16912 }
16913
16914 /* Allocate an integer type of size BITS and name NAME. */
16915
16916 static struct type *
16917 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16918 int bits, int unsigned_p, const char *name)
16919 {
16920 struct type *type;
16921
16922 /* Versions of Intel's C Compiler generate an integer type called "void"
16923 instead of using DW_TAG_unspecified_type. This has been seen on
16924 at least versions 14, 17, and 18. */
16925 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16926 && strcmp (name, "void") == 0)
16927 type = objfile_type (objfile)->builtin_void;
16928 else
16929 type = init_integer_type (objfile, bits, unsigned_p, name);
16930
16931 return type;
16932 }
16933
16934 /* Return true if DIE has a DW_AT_small attribute whose value is
16935 a constant rational, where both the numerator and denominator
16936 are equal to zero.
16937
16938 CU is the DIE's Compilation Unit. */
16939
16940 static bool
16941 has_zero_over_zero_small_attribute (struct die_info *die,
16942 struct dwarf2_cu *cu)
16943 {
16944 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
16945 if (attr == nullptr)
16946 return false;
16947
16948 struct dwarf2_cu *scale_cu = cu;
16949 struct die_info *scale_die
16950 = follow_die_ref (die, attr, &scale_cu);
16951
16952 if (scale_die->tag != DW_TAG_constant)
16953 return false;
16954
16955 gdb_mpz num (1), denom (1);
16956 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
16957 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
16958 }
16959
16960 /* Initialise and return a floating point type of size BITS suitable for
16961 use as a component of a complex number. The NAME_HINT is passed through
16962 when initialising the floating point type and is the name of the complex
16963 type.
16964
16965 As DWARF doesn't currently provide an explicit name for the components
16966 of a complex number, but it can be helpful to have these components
16967 named, we try to select a suitable name based on the size of the
16968 component. */
16969 static struct type *
16970 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16971 struct objfile *objfile,
16972 int bits, const char *name_hint,
16973 enum bfd_endian byte_order)
16974 {
16975 gdbarch *gdbarch = objfile->arch ();
16976 struct type *tt = nullptr;
16977
16978 /* Try to find a suitable floating point builtin type of size BITS.
16979 We're going to use the name of this type as the name for the complex
16980 target type that we are about to create. */
16981 switch (cu->per_cu->lang)
16982 {
16983 case language_fortran:
16984 switch (bits)
16985 {
16986 case 32:
16987 tt = builtin_f_type (gdbarch)->builtin_real;
16988 break;
16989 case 64:
16990 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16991 break;
16992 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16993 case 128:
16994 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16995 break;
16996 }
16997 break;
16998 default:
16999 switch (bits)
17000 {
17001 case 32:
17002 tt = builtin_type (gdbarch)->builtin_float;
17003 break;
17004 case 64:
17005 tt = builtin_type (gdbarch)->builtin_double;
17006 break;
17007 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17008 case 128:
17009 tt = builtin_type (gdbarch)->builtin_long_double;
17010 break;
17011 }
17012 break;
17013 }
17014
17015 /* If the type we found doesn't match the size we were looking for, then
17016 pretend we didn't find a type at all, the complex target type we
17017 create will then be nameless. */
17018 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17019 tt = nullptr;
17020
17021 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17022 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17023 }
17024
17025 /* Find a representation of a given base type and install
17026 it in the TYPE field of the die. */
17027
17028 static struct type *
17029 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17030 {
17031 struct objfile *objfile = cu->per_objfile->objfile;
17032 struct type *type;
17033 struct attribute *attr;
17034 int encoding = 0, bits = 0;
17035 const char *name;
17036 gdbarch *arch;
17037
17038 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17039 if (attr != nullptr && attr->form_is_constant ())
17040 encoding = attr->constant_value (0);
17041 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17042 if (attr != nullptr)
17043 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17044 name = dwarf2_name (die, cu);
17045 if (!name)
17046 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17047
17048 arch = objfile->arch ();
17049 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17050
17051 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17052 if (attr != nullptr && attr->form_is_constant ())
17053 {
17054 int endianity = attr->constant_value (0);
17055
17056 switch (endianity)
17057 {
17058 case DW_END_big:
17059 byte_order = BFD_ENDIAN_BIG;
17060 break;
17061 case DW_END_little:
17062 byte_order = BFD_ENDIAN_LITTLE;
17063 break;
17064 default:
17065 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17066 break;
17067 }
17068 }
17069
17070 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
17071 && cu->per_cu->lang == language_ada
17072 && has_zero_over_zero_small_attribute (die, cu))
17073 {
17074 /* brobecker/2018-02-24: This is a fixed point type for which
17075 the scaling factor is represented as fraction whose value
17076 does not make sense (zero divided by zero), so we should
17077 normally never see these. However, there is a small category
17078 of fixed point types for which GNAT is unable to provide
17079 the scaling factor via the standard DWARF mechanisms, and
17080 for which the info is provided via the GNAT encodings instead.
17081 This is likely what this DIE is about. */
17082 encoding = (encoding == DW_ATE_signed_fixed
17083 ? DW_ATE_signed
17084 : DW_ATE_unsigned);
17085 }
17086
17087 /* With GNAT encodings, fixed-point information will be encoded in
17088 the type name. Note that this can also occur with the above
17089 zero-over-zero case, which is why this is a separate "if" rather
17090 than an "else if". */
17091 const char *gnat_encoding_suffix = nullptr;
17092 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
17093 && cu->per_cu->lang == language_ada
17094 && name != nullptr)
17095 {
17096 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
17097 if (gnat_encoding_suffix != nullptr)
17098 {
17099 gdb_assert (startswith (gnat_encoding_suffix,
17100 GNAT_FIXED_POINT_SUFFIX));
17101 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
17102 name, gnat_encoding_suffix - name);
17103 /* Use -1 here so that SUFFIX points at the "_" after the
17104 "XF". */
17105 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
17106
17107 encoding = (encoding == DW_ATE_signed
17108 ? DW_ATE_signed_fixed
17109 : DW_ATE_unsigned_fixed);
17110 }
17111 }
17112
17113 switch (encoding)
17114 {
17115 case DW_ATE_address:
17116 /* Turn DW_ATE_address into a void * pointer. */
17117 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17118 type = init_pointer_type (objfile, bits, name, type);
17119 break;
17120 case DW_ATE_boolean:
17121 type = init_boolean_type (objfile, bits, 1, name);
17122 break;
17123 case DW_ATE_complex_float:
17124 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17125 byte_order);
17126 if (type->code () == TYPE_CODE_ERROR)
17127 {
17128 if (name == nullptr)
17129 {
17130 struct obstack *obstack
17131 = &cu->per_objfile->objfile->objfile_obstack;
17132 name = obconcat (obstack, "_Complex ", type->name (),
17133 nullptr);
17134 }
17135 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17136 }
17137 else
17138 type = init_complex_type (name, type);
17139 break;
17140 case DW_ATE_decimal_float:
17141 type = init_decfloat_type (objfile, bits, name);
17142 break;
17143 case DW_ATE_float:
17144 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17145 break;
17146 case DW_ATE_signed:
17147 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17148 break;
17149 case DW_ATE_unsigned:
17150 if (cu->per_cu->lang == language_fortran
17151 && name
17152 && startswith (name, "character("))
17153 type = init_character_type (objfile, bits, 1, name);
17154 else
17155 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17156 break;
17157 case DW_ATE_signed_char:
17158 if (cu->per_cu->lang == language_ada
17159 || cu->per_cu->lang == language_m2
17160 || cu->per_cu->lang == language_pascal
17161 || cu->per_cu->lang == language_fortran)
17162 type = init_character_type (objfile, bits, 0, name);
17163 else
17164 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17165 break;
17166 case DW_ATE_unsigned_char:
17167 if (cu->per_cu->lang == language_ada
17168 || cu->per_cu->lang == language_m2
17169 || cu->per_cu->lang == language_pascal
17170 || cu->per_cu->lang == language_fortran
17171 || cu->per_cu->lang == language_rust)
17172 type = init_character_type (objfile, bits, 1, name);
17173 else
17174 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17175 break;
17176 case DW_ATE_UTF:
17177 {
17178 type = init_character_type (objfile, bits, 1, name);
17179 return set_die_type (die, type, cu);
17180 }
17181 break;
17182 case DW_ATE_signed_fixed:
17183 type = init_fixed_point_type (objfile, bits, 0, name);
17184 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17185 break;
17186 case DW_ATE_unsigned_fixed:
17187 type = init_fixed_point_type (objfile, bits, 1, name);
17188 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17189 break;
17190
17191 default:
17192 complaint (_("unsupported DW_AT_encoding: '%s'"),
17193 dwarf_type_encoding_name (encoding));
17194 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17195 break;
17196 }
17197
17198 if (type->code () == TYPE_CODE_INT
17199 && name != nullptr
17200 && strcmp (name, "char") == 0)
17201 type->set_has_no_signedness (true);
17202
17203 maybe_set_alignment (cu, die, type);
17204
17205 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
17206
17207 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
17208 {
17209 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
17210 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
17211 {
17212 unsigned real_bit_size = attr->as_unsigned ();
17213 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
17214 /* Only use the attributes if they make sense together. */
17215 if (attr == nullptr
17216 || (attr->as_unsigned () + real_bit_size
17217 <= 8 * TYPE_LENGTH (type)))
17218 {
17219 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
17220 = real_bit_size;
17221 if (attr != nullptr)
17222 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
17223 = attr->as_unsigned ();
17224 }
17225 }
17226 }
17227
17228 return set_die_type (die, type, cu);
17229 }
17230
17231 /* A helper function that returns the name of DIE, if it refers to a
17232 variable declaration. */
17233
17234 static const char *
17235 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
17236 {
17237 if (die->tag != DW_TAG_variable)
17238 return nullptr;
17239
17240 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
17241 if (attr == nullptr || !attr->as_boolean ())
17242 return nullptr;
17243
17244 attr = dwarf2_attr (die, DW_AT_name, cu);
17245 if (attr == nullptr)
17246 return nullptr;
17247 return attr->as_string ();
17248 }
17249
17250 /* Parse dwarf attribute if it's a block, reference or constant and put the
17251 resulting value of the attribute into struct bound_prop.
17252 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17253
17254 static int
17255 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17256 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17257 struct type *default_type)
17258 {
17259 struct dwarf2_property_baton *baton;
17260 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17261 struct objfile *objfile = per_objfile->objfile;
17262 struct obstack *obstack = &objfile->objfile_obstack;
17263
17264 gdb_assert (default_type != NULL);
17265
17266 if (attr == NULL || prop == NULL)
17267 return 0;
17268
17269 if (attr->form_is_block ())
17270 {
17271 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17272 baton->property_type = default_type;
17273 baton->locexpr.per_cu = cu->per_cu;
17274 baton->locexpr.per_objfile = per_objfile;
17275
17276 struct dwarf_block *block;
17277 if (attr->form == DW_FORM_data16)
17278 {
17279 size_t data_size = 16;
17280 block = XOBNEW (obstack, struct dwarf_block);
17281 block->size = (data_size
17282 + 2 /* Extra bytes for DW_OP and arg. */);
17283 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
17284 data[0] = DW_OP_implicit_value;
17285 data[1] = data_size;
17286 memcpy (&data[2], attr->as_block ()->data, data_size);
17287 block->data = data;
17288 }
17289 else
17290 block = attr->as_block ();
17291
17292 baton->locexpr.size = block->size;
17293 baton->locexpr.data = block->data;
17294 switch (attr->name)
17295 {
17296 case DW_AT_string_length:
17297 baton->locexpr.is_reference = true;
17298 break;
17299 default:
17300 baton->locexpr.is_reference = false;
17301 break;
17302 }
17303
17304 prop->set_locexpr (baton);
17305 gdb_assert (prop->baton () != NULL);
17306 }
17307 else if (attr->form_is_ref ())
17308 {
17309 struct dwarf2_cu *target_cu = cu;
17310 struct die_info *target_die;
17311 struct attribute *target_attr;
17312
17313 target_die = follow_die_ref (die, attr, &target_cu);
17314 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17315 if (target_attr == NULL)
17316 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17317 target_cu);
17318 if (target_attr == nullptr)
17319 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
17320 target_cu);
17321 if (target_attr == NULL)
17322 {
17323 const char *name = var_decl_name (target_die, target_cu);
17324 if (name != nullptr)
17325 {
17326 prop->set_variable_name (name);
17327 return 1;
17328 }
17329 return 0;
17330 }
17331
17332 switch (target_attr->name)
17333 {
17334 case DW_AT_location:
17335 if (target_attr->form_is_section_offset ())
17336 {
17337 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17338 baton->property_type = die_type (target_die, target_cu);
17339 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17340 prop->set_loclist (baton);
17341 gdb_assert (prop->baton () != NULL);
17342 }
17343 else if (target_attr->form_is_block ())
17344 {
17345 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17346 baton->property_type = die_type (target_die, target_cu);
17347 baton->locexpr.per_cu = cu->per_cu;
17348 baton->locexpr.per_objfile = per_objfile;
17349 struct dwarf_block *block = target_attr->as_block ();
17350 baton->locexpr.size = block->size;
17351 baton->locexpr.data = block->data;
17352 baton->locexpr.is_reference = true;
17353 prop->set_locexpr (baton);
17354 gdb_assert (prop->baton () != NULL);
17355 }
17356 else
17357 {
17358 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17359 "dynamic property");
17360 return 0;
17361 }
17362 break;
17363 case DW_AT_data_member_location:
17364 case DW_AT_data_bit_offset:
17365 {
17366 LONGEST offset;
17367
17368 if (!handle_member_location (target_die, target_cu, &offset))
17369 return 0;
17370
17371 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17372 baton->property_type = read_type_die (target_die->parent,
17373 target_cu);
17374 baton->offset_info.offset = offset;
17375 baton->offset_info.type = die_type (target_die, target_cu);
17376 prop->set_addr_offset (baton);
17377 break;
17378 }
17379 }
17380 }
17381 else if (attr->form_is_constant ())
17382 prop->set_const_val (attr->constant_value (0));
17383 else if (attr->form_is_section_offset ())
17384 {
17385 switch (attr->name)
17386 {
17387 case DW_AT_string_length:
17388 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17389 baton->property_type = default_type;
17390 fill_in_loclist_baton (cu, &baton->loclist, attr);
17391 prop->set_loclist (baton);
17392 gdb_assert (prop->baton () != NULL);
17393 break;
17394 default:
17395 goto invalid;
17396 }
17397 }
17398 else
17399 goto invalid;
17400
17401 return 1;
17402
17403 invalid:
17404 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17405 dwarf2_name (die, cu));
17406 return 0;
17407 }
17408
17409 /* See read.h. */
17410
17411 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17412 present (which is valid) then compute the default type based on the
17413 compilation units address size. */
17414
17415 static struct type *
17416 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17417 {
17418 struct type *index_type = die_type (die, cu);
17419
17420 /* Dwarf-2 specifications explicitly allows to create subrange types
17421 without specifying a base type.
17422 In that case, the base type must be set to the type of
17423 the lower bound, upper bound or count, in that order, if any of these
17424 three attributes references an object that has a type.
17425 If no base type is found, the Dwarf-2 specifications say that
17426 a signed integer type of size equal to the size of an address should
17427 be used.
17428 For the following C code: `extern char gdb_int [];'
17429 GCC produces an empty range DIE.
17430 FIXME: muller/2010-05-28: Possible references to object for low bound,
17431 high bound or count are not yet handled by this code. */
17432 if (index_type->code () == TYPE_CODE_VOID)
17433 index_type = cu->addr_sized_int_type (false);
17434
17435 return index_type;
17436 }
17437
17438 /* Read the given DW_AT_subrange DIE. */
17439
17440 static struct type *
17441 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17442 {
17443 struct type *base_type, *orig_base_type;
17444 struct type *range_type;
17445 struct attribute *attr;
17446 struct dynamic_prop low, high;
17447 int low_default_is_valid;
17448 int high_bound_is_count = 0;
17449 const char *name;
17450 ULONGEST negative_mask;
17451
17452 orig_base_type = read_subrange_index_type (die, cu);
17453
17454 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17455 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17456 creating the range type, but we use the result of check_typedef
17457 when examining properties of the type. */
17458 base_type = check_typedef (orig_base_type);
17459
17460 /* The die_type call above may have already set the type for this DIE. */
17461 range_type = get_die_type (die, cu);
17462 if (range_type)
17463 return range_type;
17464
17465 high.set_const_val (0);
17466
17467 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17468 omitting DW_AT_lower_bound. */
17469 switch (cu->per_cu->lang)
17470 {
17471 case language_c:
17472 case language_cplus:
17473 low.set_const_val (0);
17474 low_default_is_valid = 1;
17475 break;
17476 case language_fortran:
17477 low.set_const_val (1);
17478 low_default_is_valid = 1;
17479 break;
17480 case language_d:
17481 case language_objc:
17482 case language_rust:
17483 low.set_const_val (0);
17484 low_default_is_valid = (cu->header.version >= 4);
17485 break;
17486 case language_ada:
17487 case language_m2:
17488 case language_pascal:
17489 low.set_const_val (1);
17490 low_default_is_valid = (cu->header.version >= 4);
17491 break;
17492 default:
17493 low.set_const_val (0);
17494 low_default_is_valid = 0;
17495 break;
17496 }
17497
17498 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17499 if (attr != nullptr)
17500 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17501 else if (!low_default_is_valid)
17502 complaint (_("Missing DW_AT_lower_bound "
17503 "- DIE at %s [in module %s]"),
17504 sect_offset_str (die->sect_off),
17505 objfile_name (cu->per_objfile->objfile));
17506
17507 struct attribute *attr_ub, *attr_count;
17508 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17509 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17510 {
17511 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17512 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17513 {
17514 /* If bounds are constant do the final calculation here. */
17515 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
17516 high.set_const_val (low.const_val () + high.const_val () - 1);
17517 else
17518 high_bound_is_count = 1;
17519 }
17520 else
17521 {
17522 if (attr_ub != NULL)
17523 complaint (_("Unresolved DW_AT_upper_bound "
17524 "- DIE at %s [in module %s]"),
17525 sect_offset_str (die->sect_off),
17526 objfile_name (cu->per_objfile->objfile));
17527 if (attr_count != NULL)
17528 complaint (_("Unresolved DW_AT_count "
17529 "- DIE at %s [in module %s]"),
17530 sect_offset_str (die->sect_off),
17531 objfile_name (cu->per_objfile->objfile));
17532 }
17533 }
17534
17535 LONGEST bias = 0;
17536 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17537 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17538 bias = bias_attr->constant_value (0);
17539
17540 /* Normally, the DWARF producers are expected to use a signed
17541 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17542 But this is unfortunately not always the case, as witnessed
17543 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17544 is used instead. To work around that ambiguity, we treat
17545 the bounds as signed, and thus sign-extend their values, when
17546 the base type is signed. */
17547 negative_mask =
17548 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17549 if (low.kind () == PROP_CONST
17550 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
17551 low.set_const_val (low.const_val () | negative_mask);
17552 if (high.kind () == PROP_CONST
17553 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
17554 high.set_const_val (high.const_val () | negative_mask);
17555
17556 /* Check for bit and byte strides. */
17557 struct dynamic_prop byte_stride_prop;
17558 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17559 if (attr_byte_stride != nullptr)
17560 {
17561 struct type *prop_type = cu->addr_sized_int_type (false);
17562 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17563 prop_type);
17564 }
17565
17566 struct dynamic_prop bit_stride_prop;
17567 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17568 if (attr_bit_stride != nullptr)
17569 {
17570 /* It only makes sense to have either a bit or byte stride. */
17571 if (attr_byte_stride != nullptr)
17572 {
17573 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17574 "- DIE at %s [in module %s]"),
17575 sect_offset_str (die->sect_off),
17576 objfile_name (cu->per_objfile->objfile));
17577 attr_bit_stride = nullptr;
17578 }
17579 else
17580 {
17581 struct type *prop_type = cu->addr_sized_int_type (false);
17582 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17583 prop_type);
17584 }
17585 }
17586
17587 if (attr_byte_stride != nullptr
17588 || attr_bit_stride != nullptr)
17589 {
17590 bool byte_stride_p = (attr_byte_stride != nullptr);
17591 struct dynamic_prop *stride
17592 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17593
17594 range_type
17595 = create_range_type_with_stride (NULL, orig_base_type, &low,
17596 &high, bias, stride, byte_stride_p);
17597 }
17598 else
17599 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17600
17601 if (high_bound_is_count)
17602 range_type->bounds ()->flag_upper_bound_is_count = 1;
17603
17604 /* Ada expects an empty array on no boundary attributes. */
17605 if (attr == NULL && cu->per_cu->lang != language_ada)
17606 range_type->bounds ()->high.set_undefined ();
17607
17608 name = dwarf2_name (die, cu);
17609 if (name)
17610 range_type->set_name (name);
17611
17612 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17613 if (attr != nullptr)
17614 TYPE_LENGTH (range_type) = attr->constant_value (0);
17615
17616 maybe_set_alignment (cu, die, range_type);
17617
17618 set_die_type (die, range_type, cu);
17619
17620 /* set_die_type should be already done. */
17621 set_descriptive_type (range_type, die, cu);
17622
17623 return range_type;
17624 }
17625
17626 static struct type *
17627 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17628 {
17629 struct type *type;
17630
17631 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17632 type->set_name (dwarf2_name (die, cu));
17633
17634 /* In Ada, an unspecified type is typically used when the description
17635 of the type is deferred to a different unit. When encountering
17636 such a type, we treat it as a stub, and try to resolve it later on,
17637 when needed. */
17638 if (cu->per_cu->lang == language_ada)
17639 type->set_is_stub (true);
17640
17641 return set_die_type (die, type, cu);
17642 }
17643
17644 /* Read a single die and all its descendents. Set the die's sibling
17645 field to NULL; set other fields in the die correctly, and set all
17646 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17647 location of the info_ptr after reading all of those dies. PARENT
17648 is the parent of the die in question. */
17649
17650 static struct die_info *
17651 read_die_and_children (const struct die_reader_specs *reader,
17652 const gdb_byte *info_ptr,
17653 const gdb_byte **new_info_ptr,
17654 struct die_info *parent)
17655 {
17656 struct die_info *die;
17657 const gdb_byte *cur_ptr;
17658
17659 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17660 if (die == NULL)
17661 {
17662 *new_info_ptr = cur_ptr;
17663 return NULL;
17664 }
17665 store_in_ref_table (die, reader->cu);
17666
17667 if (die->has_children)
17668 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17669 else
17670 {
17671 die->child = NULL;
17672 *new_info_ptr = cur_ptr;
17673 }
17674
17675 die->sibling = NULL;
17676 die->parent = parent;
17677 return die;
17678 }
17679
17680 /* Read a die, all of its descendents, and all of its siblings; set
17681 all of the fields of all of the dies correctly. Arguments are as
17682 in read_die_and_children. */
17683
17684 static struct die_info *
17685 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17686 const gdb_byte *info_ptr,
17687 const gdb_byte **new_info_ptr,
17688 struct die_info *parent)
17689 {
17690 struct die_info *first_die, *last_sibling;
17691 const gdb_byte *cur_ptr;
17692
17693 cur_ptr = info_ptr;
17694 first_die = last_sibling = NULL;
17695
17696 while (1)
17697 {
17698 struct die_info *die
17699 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17700
17701 if (die == NULL)
17702 {
17703 *new_info_ptr = cur_ptr;
17704 return first_die;
17705 }
17706
17707 if (!first_die)
17708 first_die = die;
17709 else
17710 last_sibling->sibling = die;
17711
17712 last_sibling = die;
17713 }
17714 }
17715
17716 /* Read a die, all of its descendents, and all of its siblings; set
17717 all of the fields of all of the dies correctly. Arguments are as
17718 in read_die_and_children.
17719 This the main entry point for reading a DIE and all its children. */
17720
17721 static struct die_info *
17722 read_die_and_siblings (const struct die_reader_specs *reader,
17723 const gdb_byte *info_ptr,
17724 const gdb_byte **new_info_ptr,
17725 struct die_info *parent)
17726 {
17727 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17728 new_info_ptr, parent);
17729
17730 if (dwarf_die_debug)
17731 {
17732 gdb_printf (gdb_stdlog,
17733 "Read die from %s@0x%x of %s:\n",
17734 reader->die_section->get_name (),
17735 (unsigned) (info_ptr - reader->die_section->buffer),
17736 bfd_get_filename (reader->abfd));
17737 dump_die (die, dwarf_die_debug);
17738 }
17739
17740 return die;
17741 }
17742
17743 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17744 attributes.
17745 The caller is responsible for filling in the extra attributes
17746 and updating (*DIEP)->num_attrs.
17747 Set DIEP to point to a newly allocated die with its information,
17748 except for its child, sibling, and parent fields. */
17749
17750 static const gdb_byte *
17751 read_full_die_1 (const struct die_reader_specs *reader,
17752 struct die_info **diep, const gdb_byte *info_ptr,
17753 int num_extra_attrs)
17754 {
17755 unsigned int abbrev_number, bytes_read, i;
17756 const struct abbrev_info *abbrev;
17757 struct die_info *die;
17758 struct dwarf2_cu *cu = reader->cu;
17759 bfd *abfd = reader->abfd;
17760
17761 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17762 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17763 info_ptr += bytes_read;
17764 if (!abbrev_number)
17765 {
17766 *diep = NULL;
17767 return info_ptr;
17768 }
17769
17770 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17771 if (!abbrev)
17772 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17773 abbrev_number,
17774 bfd_get_filename (abfd));
17775
17776 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17777 die->sect_off = sect_off;
17778 die->tag = abbrev->tag;
17779 die->abbrev = abbrev_number;
17780 die->has_children = abbrev->has_children;
17781
17782 /* Make the result usable.
17783 The caller needs to update num_attrs after adding the extra
17784 attributes. */
17785 die->num_attrs = abbrev->num_attrs;
17786
17787 bool any_need_reprocess = false;
17788 for (i = 0; i < abbrev->num_attrs; ++i)
17789 {
17790 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17791 info_ptr);
17792 if (die->attrs[i].requires_reprocessing_p ())
17793 any_need_reprocess = true;
17794 }
17795
17796 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17797 if (attr != nullptr && attr->form_is_unsigned ())
17798 cu->str_offsets_base = attr->as_unsigned ();
17799
17800 attr = die->attr (DW_AT_loclists_base);
17801 if (attr != nullptr)
17802 cu->loclist_base = attr->as_unsigned ();
17803
17804 auto maybe_addr_base = die->addr_base ();
17805 if (maybe_addr_base.has_value ())
17806 cu->addr_base = *maybe_addr_base;
17807
17808 attr = die->attr (DW_AT_rnglists_base);
17809 if (attr != nullptr)
17810 cu->rnglists_base = attr->as_unsigned ();
17811
17812 if (any_need_reprocess)
17813 {
17814 for (i = 0; i < abbrev->num_attrs; ++i)
17815 {
17816 if (die->attrs[i].requires_reprocessing_p ())
17817 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
17818 }
17819 }
17820 *diep = die;
17821 return info_ptr;
17822 }
17823
17824 /* Read a die and all its attributes.
17825 Set DIEP to point to a newly allocated die with its information,
17826 except for its child, sibling, and parent fields. */
17827
17828 static const gdb_byte *
17829 read_full_die (const struct die_reader_specs *reader,
17830 struct die_info **diep, const gdb_byte *info_ptr)
17831 {
17832 const gdb_byte *result;
17833
17834 result = read_full_die_1 (reader, diep, info_ptr, 0);
17835
17836 if (dwarf_die_debug)
17837 {
17838 gdb_printf (gdb_stdlog,
17839 "Read die from %s@0x%x of %s:\n",
17840 reader->die_section->get_name (),
17841 (unsigned) (info_ptr - reader->die_section->buffer),
17842 bfd_get_filename (reader->abfd));
17843 dump_die (*diep, dwarf_die_debug);
17844 }
17845
17846 return result;
17847 }
17848 \f
17849
17850 void
17851 cooked_indexer::check_bounds (cutu_reader *reader)
17852 {
17853 if (reader->cu->per_cu->addresses_seen)
17854 return;
17855
17856 dwarf2_cu *cu = reader->cu;
17857
17858 CORE_ADDR best_lowpc = 0, best_highpc = 0;
17859 /* Possibly set the default values of LOWPC and HIGHPC from
17860 `DW_AT_ranges'. */
17861 dwarf2_find_base_address (reader->comp_unit_die, cu);
17862 enum pc_bounds_kind cu_bounds_kind
17863 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
17864 cu, m_index_storage->get_addrmap (), cu->per_cu);
17865 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
17866 {
17867 struct objfile *objfile = cu->per_objfile->objfile;
17868 CORE_ADDR baseaddr = objfile->text_section_offset ();
17869 struct gdbarch *gdbarch = objfile->arch ();
17870 CORE_ADDR low
17871 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
17872 - baseaddr);
17873 CORE_ADDR high
17874 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
17875 - baseaddr - 1);
17876 /* Store the contiguous range if it is not empty; it can be
17877 empty for CUs with no code. */
17878 addrmap_set_empty (m_index_storage->get_addrmap (), low, high,
17879 cu->per_cu);
17880
17881 cu->per_cu->addresses_seen = true;
17882 }
17883 }
17884
17885 /* Helper function that returns true if TAG can have a linkage
17886 name. */
17887
17888 static bool
17889 tag_can_have_linkage_name (enum dwarf_tag tag)
17890 {
17891 switch (tag)
17892 {
17893 /* We include types here because an anonymous C++ type might
17894 have a name for linkage purposes. */
17895 case DW_TAG_class_type:
17896 case DW_TAG_structure_type:
17897 case DW_TAG_union_type:
17898 case DW_TAG_variable:
17899 case DW_TAG_subprogram:
17900 return true;
17901
17902 default:
17903 return false;
17904 }
17905 }
17906
17907 cutu_reader *
17908 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
17909 dwarf2_per_objfile *per_objfile,
17910 sect_offset sect_off, bool is_dwz,
17911 bool for_scanning)
17912 {
17913 /* Lookups for type unit references are always in the CU, and
17914 cross-CU references will crash. */
17915 if (reader->cu->per_cu->is_dwz == is_dwz
17916 && reader->cu->header.offset_in_cu_p (sect_off))
17917 return reader;
17918
17919 dwarf2_per_cu_data *per_cu
17920 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
17921 per_objfile->per_bfd);
17922
17923 /* When scanning, we only want to visit a given CU a single time.
17924 Doing this check here avoids self-imports as well. */
17925 if (for_scanning)
17926 {
17927 bool nope = false;
17928 if (!per_cu->scanned.compare_exchange_strong (nope, true))
17929 return nullptr;
17930 }
17931 if (per_cu == m_per_cu)
17932 return reader;
17933
17934 cutu_reader *result = m_index_storage->get_reader (per_cu);
17935 if (result == nullptr)
17936 {
17937 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
17938 m_index_storage->get_abbrev_cache ());
17939
17940 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
17941 language_minimal);
17942 std::unique_ptr<cutu_reader> copy
17943 (new cutu_reader (std::move (new_reader)));
17944 result = m_index_storage->preserve (std::move (copy));
17945 }
17946
17947 if (result->dummy_p || !result->comp_unit_die->has_children)
17948 return nullptr;
17949
17950 if (for_scanning)
17951 check_bounds (result);
17952
17953 return result;
17954 }
17955
17956 const gdb_byte *
17957 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
17958 cutu_reader *reader,
17959 const gdb_byte *watermark_ptr,
17960 const gdb_byte *info_ptr,
17961 const abbrev_info *abbrev,
17962 const char **name,
17963 const char **linkage_name,
17964 cooked_index_flag *flags,
17965 sect_offset *sibling_offset,
17966 const cooked_index_entry **parent_entry,
17967 CORE_ADDR *maybe_defer,
17968 bool for_specification)
17969 {
17970 bool origin_is_dwz = false;
17971 bool is_declaration = false;
17972 sect_offset origin_offset {};
17973
17974 gdb::optional<CORE_ADDR> low_pc;
17975 gdb::optional<CORE_ADDR> high_pc;
17976 bool high_pc_relative = false;
17977
17978 for (int i = 0; i < abbrev->num_attrs; ++i)
17979 {
17980 attribute attr;
17981 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
17982 if (attr.requires_reprocessing_p ())
17983 read_attribute_reprocess (reader, &attr, abbrev->tag);
17984
17985 /* Store the data if it is of an attribute we want to keep in a
17986 partial symbol table. */
17987 switch (attr.name)
17988 {
17989 case DW_AT_name:
17990 switch (abbrev->tag)
17991 {
17992 case DW_TAG_compile_unit:
17993 case DW_TAG_partial_unit:
17994 case DW_TAG_type_unit:
17995 /* Compilation units have a DW_AT_name that is a filename, not
17996 a source language identifier. */
17997 break;
17998
17999 default:
18000 if (*name == nullptr)
18001 *name = attr.as_string ();
18002 break;
18003 }
18004 break;
18005
18006 case DW_AT_linkage_name:
18007 case DW_AT_MIPS_linkage_name:
18008 /* Note that both forms of linkage name might appear. We
18009 assume they will be the same, and we only store the last
18010 one we see. */
18011 if (*linkage_name == nullptr)
18012 *linkage_name = attr.as_string ();
18013 break;
18014
18015 case DW_AT_main_subprogram:
18016 if (attr.as_boolean ())
18017 *flags |= IS_MAIN;
18018 break;
18019
18020 case DW_AT_declaration:
18021 is_declaration = attr.as_boolean ();
18022 break;
18023
18024 case DW_AT_sibling:
18025 if (sibling_offset != nullptr)
18026 *sibling_offset = attr.get_ref_die_offset ();
18027 break;
18028
18029 case DW_AT_specification:
18030 case DW_AT_abstract_origin:
18031 case DW_AT_extension:
18032 origin_offset = attr.get_ref_die_offset ();
18033 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
18034 break;
18035
18036 case DW_AT_external:
18037 if (attr.as_boolean ())
18038 *flags &= ~IS_STATIC;
18039 break;
18040
18041 case DW_AT_enum_class:
18042 if (attr.as_boolean ())
18043 *flags |= IS_ENUM_CLASS;
18044 break;
18045
18046 case DW_AT_low_pc:
18047 low_pc = attr.as_address ();
18048 break;
18049
18050 case DW_AT_high_pc:
18051 high_pc = attr.as_address ();
18052 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
18053 high_pc_relative = true;
18054 break;
18055
18056 case DW_AT_location:
18057 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
18058 {
18059 struct dwarf_block *locdesc = attr.as_block ();
18060 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
18061 if (addr != 0
18062 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
18063 {
18064 low_pc = addr;
18065 /* For variables, we don't want to try decoding the
18066 type just to find the size -- for gdb's purposes
18067 we only need the address of a variable. */
18068 high_pc = addr + 1;
18069 high_pc_relative = false;
18070 }
18071 }
18072 break;
18073
18074 case DW_AT_ranges:
18075 if (!scanning_per_cu->addresses_seen)
18076 {
18077 /* Offset in the .debug_ranges or .debug_rnglist section
18078 (depending on DWARF version). */
18079 ULONGEST ranges_offset = attr.as_unsigned ();
18080
18081 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18082 want to add this value. */
18083 ranges_offset += reader->cu->gnu_ranges_base;
18084
18085 CORE_ADDR lowpc, highpc;
18086 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
18087 m_index_storage->get_addrmap (),
18088 scanning_per_cu, abbrev->tag);
18089 }
18090 break;
18091 }
18092 }
18093
18094 /* We don't want to examine declarations, but if we found a
18095 declaration when handling DW_AT_specification or the like, then
18096 that is ok. Similarly, we allow an external variable without a
18097 location; those are resolved via minimal symbols. */
18098 if (is_declaration && !for_specification
18099 && (abbrev->tag != DW_TAG_variable
18100 || (*flags & IS_STATIC) != 0))
18101 {
18102 *linkage_name = nullptr;
18103 *name = nullptr;
18104 }
18105 else if ((*name == nullptr
18106 || (*linkage_name == nullptr
18107 && tag_can_have_linkage_name (abbrev->tag))
18108 || (*parent_entry == nullptr && m_language != language_c))
18109 && origin_offset != sect_offset (0))
18110 {
18111 cutu_reader *new_reader
18112 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
18113 origin_is_dwz, false);
18114 if (new_reader != nullptr)
18115 {
18116 const gdb_byte *new_info_ptr = (new_reader->buffer
18117 + to_underlying (origin_offset));
18118
18119 if (new_reader->cu == reader->cu
18120 && new_info_ptr > watermark_ptr
18121 && maybe_defer != nullptr
18122 && *parent_entry == nullptr)
18123 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
18124 else if (*parent_entry == nullptr)
18125 {
18126 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
18127 *parent_entry
18128 = (cooked_index_entry *) addrmap_find (m_die_range_map,
18129 lookup);
18130 }
18131
18132 unsigned int bytes_read;
18133 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
18134 new_info_ptr,
18135 &bytes_read);
18136 new_info_ptr += bytes_read;
18137 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
18138 new_abbrev, name, linkage_name, flags, nullptr,
18139 parent_entry, maybe_defer, true);
18140 }
18141 }
18142
18143 if (!for_specification)
18144 {
18145 if (m_language == language_ada
18146 && *linkage_name == nullptr)
18147 *linkage_name = *name;
18148
18149 if (!scanning_per_cu->addresses_seen
18150 && low_pc.has_value ()
18151 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
18152 || *low_pc != 0)
18153 && high_pc.has_value ())
18154 {
18155 if (high_pc_relative)
18156 high_pc = *high_pc + *low_pc;
18157
18158 if (*high_pc > *low_pc)
18159 {
18160 struct objfile *objfile = reader->cu->per_objfile->objfile;
18161 CORE_ADDR baseaddr = objfile->text_section_offset ();
18162 struct gdbarch *gdbarch = objfile->arch ();
18163 CORE_ADDR lo
18164 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
18165 - baseaddr);
18166 CORE_ADDR hi
18167 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
18168 - baseaddr);
18169 addrmap_set_empty (m_index_storage->get_addrmap (), lo, hi - 1,
18170 scanning_per_cu);
18171 }
18172 }
18173
18174 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
18175 *flags &= ~IS_STATIC;
18176
18177 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
18178 *name = "(anonymous namespace)";
18179
18180 if (m_language == language_cplus
18181 && (abbrev->tag == DW_TAG_class_type
18182 || abbrev->tag == DW_TAG_interface_type
18183 || abbrev->tag == DW_TAG_structure_type
18184 || abbrev->tag == DW_TAG_union_type
18185 || abbrev->tag == DW_TAG_enumeration_type
18186 || abbrev->tag == DW_TAG_enumerator))
18187 *flags &= ~IS_STATIC;
18188 }
18189
18190 return info_ptr;
18191 }
18192
18193 const gdb_byte *
18194 cooked_indexer::index_imported_unit (cutu_reader *reader,
18195 const gdb_byte *info_ptr,
18196 const abbrev_info *abbrev)
18197 {
18198 sect_offset sect_off {};
18199 bool is_dwz = false;
18200
18201 for (int i = 0; i < abbrev->num_attrs; ++i)
18202 {
18203 /* Note that we never need to reprocess attributes here. */
18204 attribute attr;
18205 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18206
18207 if (attr.name == DW_AT_import)
18208 {
18209 sect_off = attr.get_ref_die_offset ();
18210 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18211 || reader->cu->per_cu->is_dwz);
18212 }
18213 }
18214
18215 /* Did not find DW_AT_import. */
18216 if (sect_off == sect_offset (0))
18217 return info_ptr;
18218
18219 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
18220 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
18221 is_dwz, true);
18222 if (new_reader != nullptr)
18223 {
18224 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
18225
18226 reader->cu->add_dependence (new_reader->cu->per_cu);
18227 }
18228
18229 return info_ptr;
18230 }
18231
18232 const gdb_byte *
18233 cooked_indexer::recurse (cutu_reader *reader,
18234 const gdb_byte *info_ptr,
18235 const cooked_index_entry *parent_entry,
18236 bool fully)
18237 {
18238 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18239
18240 if (parent_entry != nullptr)
18241 {
18242 CORE_ADDR start = form_addr (parent_entry->die_offset,
18243 reader->cu->per_cu->is_dwz);
18244 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
18245 reader->cu->per_cu->is_dwz);
18246 addrmap_set_empty (m_die_range_map, start, end, (void *) parent_entry);
18247 }
18248
18249 return info_ptr;
18250 }
18251
18252 const gdb_byte *
18253 cooked_indexer::index_dies (cutu_reader *reader,
18254 const gdb_byte *info_ptr,
18255 const cooked_index_entry *parent_entry,
18256 bool fully)
18257 {
18258 const gdb_byte *end_ptr = info_ptr + reader->cu->header.get_length ();
18259
18260 while (info_ptr < end_ptr)
18261 {
18262 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
18263 unsigned int bytes_read;
18264 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18265 &bytes_read);
18266 info_ptr += bytes_read;
18267 if (abbrev == nullptr)
18268 break;
18269
18270 if (abbrev->tag == DW_TAG_imported_unit)
18271 {
18272 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
18273 continue;
18274 }
18275
18276 if (!abbrev->interesting)
18277 {
18278 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
18279 if (fully && abbrev->has_children)
18280 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18281 continue;
18282 }
18283
18284 const char *name = nullptr;
18285 const char *linkage_name = nullptr;
18286 CORE_ADDR defer = 0;
18287 cooked_index_flag flags = IS_STATIC;
18288 sect_offset sibling {};
18289 const cooked_index_entry *this_parent_entry = parent_entry;
18290 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
18291 info_ptr, abbrev, &name, &linkage_name,
18292 &flags, &sibling, &this_parent_entry,
18293 &defer, false);
18294
18295 if (abbrev->tag == DW_TAG_namespace
18296 && m_language == language_cplus
18297 && strcmp (name, "::") == 0)
18298 {
18299 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18300 generated bogus DW_TAG_namespace DIEs with a name of "::"
18301 for the global namespace. Work around this problem
18302 here. */
18303 name = nullptr;
18304 }
18305
18306 const cooked_index_entry *this_entry = nullptr;
18307 if (name != nullptr)
18308 {
18309 if (defer != 0)
18310 m_deferred_entries.push_back ({
18311 this_die, name, defer, abbrev->tag, flags
18312 });
18313 else
18314 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
18315 name, this_parent_entry,
18316 m_per_cu);
18317 }
18318
18319 if (linkage_name != nullptr)
18320 {
18321 /* We only want this to be "main" if it has a linkage name
18322 but not an ordinary name. */
18323 if (name != nullptr)
18324 flags = flags & ~IS_MAIN;
18325 m_index_storage->add (this_die, abbrev->tag, flags | IS_LINKAGE,
18326 linkage_name, nullptr, m_per_cu);
18327 }
18328
18329 if (abbrev->has_children)
18330 {
18331 switch (abbrev->tag)
18332 {
18333 case DW_TAG_class_type:
18334 case DW_TAG_interface_type:
18335 case DW_TAG_structure_type:
18336 case DW_TAG_union_type:
18337 if (m_language != language_c && this_entry != nullptr)
18338 {
18339 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18340 continue;
18341 }
18342 break;
18343
18344 case DW_TAG_enumeration_type:
18345 /* We need to recurse even for an anonymous enumeration.
18346 Which scope we record as the parent scope depends on
18347 whether we're reading an "enum class". If so, we use
18348 the enum itself as the parent, yielding names like
18349 "enum_class::enumerator"; otherwise we inject the
18350 names into our own parent scope. */
18351 info_ptr = recurse (reader, info_ptr,
18352 ((flags & IS_ENUM_CLASS) == 0)
18353 ? parent_entry
18354 : this_entry,
18355 fully);
18356 continue;
18357
18358 case DW_TAG_module:
18359 if (this_entry == nullptr)
18360 break;
18361 /* FALLTHROUGH */
18362 case DW_TAG_namespace:
18363 /* We don't check THIS_ENTRY for a namespace, to handle
18364 the ancient G++ workaround pointed out above. */
18365 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18366 continue;
18367
18368 case DW_TAG_subprogram:
18369 if ((m_language == language_fortran
18370 || m_language == language_ada)
18371 && this_entry != nullptr)
18372 {
18373 info_ptr = recurse (reader, info_ptr, this_entry, true);
18374 continue;
18375 }
18376 break;
18377 }
18378
18379 if (sibling != sect_offset (0))
18380 {
18381 const gdb_byte *sibling_ptr
18382 = reader->buffer + to_underlying (sibling);
18383
18384 if (sibling_ptr < info_ptr)
18385 complaint (_("DW_AT_sibling points backwards"));
18386 else if (sibling_ptr > reader->buffer_end)
18387 reader->die_section->overflow_complaint ();
18388 else
18389 info_ptr = sibling_ptr;
18390 }
18391 else
18392 info_ptr = skip_children (reader, info_ptr);
18393 }
18394 }
18395
18396 return info_ptr;
18397 }
18398
18399 void
18400 cooked_indexer::make_index (cutu_reader *reader)
18401 {
18402 check_bounds (reader);
18403 find_file_and_directory (reader->comp_unit_die, reader->cu);
18404 if (!reader->comp_unit_die->has_children)
18405 return;
18406 index_dies (reader, reader->info_ptr, nullptr, false);
18407
18408 for (const auto &entry : m_deferred_entries)
18409 {
18410 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
18411 cooked_index_entry *parent
18412 = (cooked_index_entry *) addrmap_find (m_die_range_map, key);
18413 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
18414 entry.name, parent, m_per_cu);
18415 }
18416 }
18417
18418 /* An implementation of quick_symbol_functions for the cooked DWARF
18419 index. */
18420
18421 struct cooked_index_functions : public dwarf2_base_index_functions
18422 {
18423 struct compunit_symtab *find_pc_sect_compunit_symtab
18424 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
18425 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
18426
18427 struct compunit_symtab *find_compunit_symtab_by_address
18428 (struct objfile *objfile, CORE_ADDR address) override;
18429
18430 void dump (struct objfile *objfile) override
18431 {
18432 gdb_printf ("Cooked index in use\n");
18433 }
18434
18435 void expand_matching_symbols
18436 (struct objfile *,
18437 const lookup_name_info &lookup_name,
18438 domain_enum domain,
18439 int global,
18440 symbol_compare_ftype *ordered_compare) override;
18441
18442 bool expand_symtabs_matching
18443 (struct objfile *objfile,
18444 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18445 const lookup_name_info *lookup_name,
18446 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18447 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18448 block_search_flags search_flags,
18449 domain_enum domain,
18450 enum search_domain kind) override;
18451
18452 bool can_lazily_read_symbols () override
18453 {
18454 return true;
18455 }
18456
18457 void read_partial_symbols (struct objfile *objfile) override
18458 {
18459 if (dwarf2_has_info (objfile, nullptr))
18460 dwarf2_build_psymtabs (objfile, true);
18461 }
18462 };
18463
18464 struct compunit_symtab *
18465 cooked_index_functions::find_pc_sect_compunit_symtab
18466 (struct objfile *objfile,
18467 struct bound_minimal_symbol msymbol,
18468 CORE_ADDR pc,
18469 struct obj_section *section,
18470 int warn_if_readin)
18471 {
18472 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18473 if (per_objfile->per_bfd->index_table == nullptr)
18474 return nullptr;
18475
18476 CORE_ADDR baseaddr = objfile->text_section_offset ();
18477 cooked_index_vector *table
18478 = (static_cast<cooked_index_vector *>
18479 (per_objfile->per_bfd->index_table.get ()));
18480 dwarf2_per_cu_data *per_cu = table->lookup (pc - baseaddr);
18481 if (per_cu == nullptr)
18482 return nullptr;
18483
18484 if (warn_if_readin && per_objfile->symtab_set_p (per_cu))
18485 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
18486 paddress (objfile->arch (), pc));
18487
18488 compunit_symtab *result = (recursively_find_pc_sect_compunit_symtab
18489 (dw2_instantiate_symtab (per_cu, per_objfile,
18490 false),
18491 pc));
18492 gdb_assert (result != nullptr);
18493 return result;
18494 }
18495
18496 struct compunit_symtab *
18497 cooked_index_functions::find_compunit_symtab_by_address
18498 (struct objfile *objfile, CORE_ADDR address)
18499 {
18500 if (objfile->sect_index_data == -1)
18501 return nullptr;
18502
18503 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18504 if (per_objfile->per_bfd->index_table == nullptr)
18505 return nullptr;
18506
18507 CORE_ADDR baseaddr = objfile->data_section_offset ();
18508 cooked_index_vector *table
18509 = (static_cast<cooked_index_vector *>
18510 (per_objfile->per_bfd->index_table.get ()));
18511 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
18512 if (per_cu == nullptr)
18513 return nullptr;
18514
18515 return dw2_instantiate_symtab (per_cu, per_objfile, false);
18516 }
18517
18518 void
18519 cooked_index_functions::expand_matching_symbols
18520 (struct objfile *objfile,
18521 const lookup_name_info &lookup_name,
18522 domain_enum domain,
18523 int global,
18524 symbol_compare_ftype *ordered_compare)
18525 {
18526 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18527 if (per_objfile->per_bfd->index_table == nullptr)
18528 return;
18529 const block_search_flags search_flags = (global
18530 ? SEARCH_GLOBAL_BLOCK
18531 : SEARCH_STATIC_BLOCK);
18532 const language_defn *lang = language_def (language_ada);
18533 symbol_name_matcher_ftype *name_match
18534 = lang->get_symbol_name_matcher (lookup_name);
18535
18536 cooked_index_vector *table
18537 = (static_cast<cooked_index_vector *>
18538 (per_objfile->per_bfd->index_table.get ()));
18539 for (const cooked_index_entry *entry : table->all_entries ())
18540 {
18541 if (entry->parent_entry != nullptr)
18542 continue;
18543
18544 if (!entry->matches (search_flags)
18545 || !entry->matches (domain))
18546 continue;
18547
18548 if (name_match (entry->canonical, lookup_name, nullptr))
18549 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
18550 }
18551 }
18552
18553 bool
18554 cooked_index_functions::expand_symtabs_matching
18555 (struct objfile *objfile,
18556 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18557 const lookup_name_info *lookup_name,
18558 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18559 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18560 block_search_flags search_flags,
18561 domain_enum domain,
18562 enum search_domain kind)
18563 {
18564 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18565 if (per_objfile->per_bfd->index_table == nullptr)
18566 return true;
18567
18568 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
18569
18570 /* This invariant is documented in quick-functions.h. */
18571 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
18572 if (lookup_name == nullptr)
18573 {
18574 for (dwarf2_per_cu_data *per_cu
18575 : all_comp_units_range (per_objfile->per_bfd))
18576 {
18577 QUIT;
18578
18579 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
18580 file_matcher,
18581 expansion_notify))
18582 return false;
18583 }
18584 return true;
18585 }
18586
18587 lookup_name_info lookup_name_without_params
18588 = lookup_name->make_ignore_params ();
18589 bool completing = lookup_name->completion_mode ();
18590
18591 /* Unique styles of language splitting. */
18592 static const enum language unique_styles[] =
18593 {
18594 /* No splitting is also a style. */
18595 language_c,
18596 /* This includes Rust. */
18597 language_cplus,
18598 /* This includes Go. */
18599 language_d,
18600 language_ada
18601 };
18602
18603 cooked_index_vector *table
18604 = (static_cast<cooked_index_vector *>
18605 (per_objfile->per_bfd->index_table.get ()));
18606 for (enum language lang : unique_styles)
18607 {
18608 std::vector<gdb::string_view> name_vec
18609 = lookup_name_without_params.split_name (lang);
18610
18611 for (const cooked_index_entry *entry : table->find (name_vec.back (),
18612 completing))
18613 {
18614 /* No need to consider symbols from expanded CUs. */
18615 if (per_objfile->symtab_set_p (entry->per_cu))
18616 continue;
18617
18618 /* If file-matching was done, we don't need to consider
18619 symbols from unmarked CUs. */
18620 if (file_matcher != nullptr && !entry->per_cu->mark)
18621 continue;
18622
18623 /* See if the symbol matches the type filter. */
18624 if (!entry->matches (search_flags)
18625 || !entry->matches (domain)
18626 || !entry->matches (kind))
18627 continue;
18628
18629 /* We've found the base name of the symbol; now walk its
18630 parentage chain, ensuring that each component
18631 matches. */
18632 bool found = true;
18633
18634 const cooked_index_entry *parent = entry->parent_entry;
18635 for (int i = name_vec.size () - 1; i > 0; --i)
18636 {
18637 /* If we ran out of entries, or if this segment doesn't
18638 match, this did not match. */
18639 if (parent == nullptr
18640 || strncmp (parent->name, name_vec[i - 1].data (),
18641 name_vec[i - 1].length ()) != 0)
18642 {
18643 found = false;
18644 break;
18645 }
18646
18647 parent = parent->parent_entry;
18648 }
18649
18650 if (!found)
18651 continue;
18652
18653 /* Might have been looking for "a::b" and found
18654 "x::a::b". */
18655 if (symbol_matcher == nullptr)
18656 {
18657 symbol_name_match_type match_type
18658 = lookup_name_without_params.match_type ();
18659 if ((match_type == symbol_name_match_type::FULL
18660 || (lang != language_ada
18661 && match_type == symbol_name_match_type::EXPRESSION))
18662 && parent != nullptr)
18663 continue;
18664 }
18665 else
18666 {
18667 auto_obstack temp_storage;
18668 const char *full_name = entry->full_name (&temp_storage);
18669 if (!symbol_matcher (full_name))
18670 continue;
18671 }
18672
18673 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
18674 file_matcher,
18675 expansion_notify))
18676 return false;
18677 }
18678 }
18679
18680 return true;
18681 }
18682
18683 /* Return a new cooked_index_functions object. */
18684
18685 static quick_symbol_functions_up
18686 make_cooked_index_funcs ()
18687 {
18688 return quick_symbol_functions_up (new cooked_index_functions);
18689 }
18690
18691 quick_symbol_functions_up
18692 cooked_index_vector::make_quick_functions () const
18693 {
18694 return make_cooked_index_funcs ();
18695 }
18696
18697 \f
18698
18699 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18700 contents from the given SECTION in the HEADER.
18701
18702 HEADER_OFFSET is the offset of the header in the section. */
18703 static void
18704 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
18705 struct dwarf2_section_info *section,
18706 sect_offset header_offset)
18707 {
18708 unsigned int bytes_read;
18709 bfd *abfd = section->get_bfd_owner ();
18710 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
18711
18712 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18713 info_ptr += bytes_read;
18714
18715 header->version = read_2_bytes (abfd, info_ptr);
18716 info_ptr += 2;
18717
18718 header->addr_size = read_1_byte (abfd, info_ptr);
18719 info_ptr += 1;
18720
18721 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18722 info_ptr += 1;
18723
18724 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18725 }
18726
18727 /* Return the DW_AT_loclists_base value for the CU. */
18728 static ULONGEST
18729 lookup_loclist_base (struct dwarf2_cu *cu)
18730 {
18731 /* For the .dwo unit, the loclist_base points to the first offset following
18732 the header. The header consists of the following entities-
18733 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18734 bit format)
18735 2. version (2 bytes)
18736 3. address size (1 byte)
18737 4. segment selector size (1 byte)
18738 5. offset entry count (4 bytes)
18739 These sizes are derived as per the DWARFv5 standard. */
18740 if (cu->dwo_unit != nullptr)
18741 {
18742 if (cu->header.initial_length_size == 4)
18743 return LOCLIST_HEADER_SIZE32;
18744 return LOCLIST_HEADER_SIZE64;
18745 }
18746 return cu->loclist_base;
18747 }
18748
18749 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18750 array of offsets in the .debug_loclists section. */
18751
18752 static sect_offset
18753 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18754 {
18755 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18756 struct objfile *objfile = per_objfile->objfile;
18757 bfd *abfd = objfile->obfd;
18758 ULONGEST loclist_header_size =
18759 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
18760 : LOCLIST_HEADER_SIZE64);
18761 ULONGEST loclist_base = lookup_loclist_base (cu);
18762
18763 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18764 ULONGEST start_offset =
18765 loclist_base + loclist_index * cu->header.offset_size;
18766
18767 /* Get loclists section. */
18768 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18769
18770 /* Read the loclists section content. */
18771 section->read (objfile);
18772 if (section->buffer == NULL)
18773 error (_("DW_FORM_loclistx used without .debug_loclists "
18774 "section [in module %s]"), objfile_name (objfile));
18775
18776 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18777 so if loclist_base is smaller than the header size, we have a problem. */
18778 if (loclist_base < loclist_header_size)
18779 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18780 objfile_name (objfile));
18781
18782 /* Read the header of the loclists contribution. */
18783 struct loclists_rnglists_header header;
18784 read_loclists_rnglists_header (&header, section,
18785 (sect_offset) (loclist_base - loclist_header_size));
18786
18787 /* Verify the loclist index is valid. */
18788 if (loclist_index >= header.offset_entry_count)
18789 error (_("DW_FORM_loclistx pointing outside of "
18790 ".debug_loclists offset array [in module %s]"),
18791 objfile_name (objfile));
18792
18793 /* Validate that reading won't go beyond the end of the section. */
18794 if (start_offset + cu->header.offset_size > section->size)
18795 error (_("Reading DW_FORM_loclistx index beyond end of"
18796 ".debug_loclists section [in module %s]"),
18797 objfile_name (objfile));
18798
18799 const gdb_byte *info_ptr = section->buffer + start_offset;
18800
18801 if (cu->header.offset_size == 4)
18802 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
18803 else
18804 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
18805 }
18806
18807 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18808 array of offsets in the .debug_rnglists section. */
18809
18810 static sect_offset
18811 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
18812 dwarf_tag tag)
18813 {
18814 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18815 struct objfile *objfile = dwarf2_per_objfile->objfile;
18816 bfd *abfd = objfile->obfd;
18817 ULONGEST rnglist_header_size =
18818 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
18819 : RNGLIST_HEADER_SIZE64);
18820
18821 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18822 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18823 doesn't apply. */
18824 ULONGEST rnglist_base =
18825 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
18826
18827 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18828 ULONGEST start_offset =
18829 rnglist_base + rnglist_index * cu->header.offset_size;
18830
18831 /* Get rnglists section. */
18832 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
18833
18834 /* Read the rnglists section content. */
18835 section->read (objfile);
18836 if (section->buffer == nullptr)
18837 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18838 "[in module %s]"),
18839 objfile_name (objfile));
18840
18841 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18842 so if rnglist_base is smaller than the header size, we have a problem. */
18843 if (rnglist_base < rnglist_header_size)
18844 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18845 objfile_name (objfile));
18846
18847 /* Read the header of the rnglists contribution. */
18848 struct loclists_rnglists_header header;
18849 read_loclists_rnglists_header (&header, section,
18850 (sect_offset) (rnglist_base - rnglist_header_size));
18851
18852 /* Verify the rnglist index is valid. */
18853 if (rnglist_index >= header.offset_entry_count)
18854 error (_("DW_FORM_rnglistx index pointing outside of "
18855 ".debug_rnglists offset array [in module %s]"),
18856 objfile_name (objfile));
18857
18858 /* Validate that reading won't go beyond the end of the section. */
18859 if (start_offset + cu->header.offset_size > section->size)
18860 error (_("Reading DW_FORM_rnglistx index beyond end of"
18861 ".debug_rnglists section [in module %s]"),
18862 objfile_name (objfile));
18863
18864 const gdb_byte *info_ptr = section->buffer + start_offset;
18865
18866 if (cu->header.offset_size == 4)
18867 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
18868 else
18869 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
18870 }
18871
18872 /* Process the attributes that had to be skipped in the first round. These
18873 attributes are the ones that need str_offsets_base or addr_base attributes.
18874 They could not have been processed in the first round, because at the time
18875 the values of str_offsets_base or addr_base may not have been known. */
18876 static void
18877 read_attribute_reprocess (const struct die_reader_specs *reader,
18878 struct attribute *attr, dwarf_tag tag)
18879 {
18880 struct dwarf2_cu *cu = reader->cu;
18881 switch (attr->form)
18882 {
18883 case DW_FORM_addrx:
18884 case DW_FORM_GNU_addr_index:
18885 attr->set_address (read_addr_index (cu,
18886 attr->as_unsigned_reprocess ()));
18887 break;
18888 case DW_FORM_loclistx:
18889 {
18890 sect_offset loclists_sect_off
18891 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
18892
18893 attr->set_unsigned (to_underlying (loclists_sect_off));
18894 }
18895 break;
18896 case DW_FORM_rnglistx:
18897 {
18898 sect_offset rnglists_sect_off
18899 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
18900
18901 attr->set_unsigned (to_underlying (rnglists_sect_off));
18902 }
18903 break;
18904 case DW_FORM_strx:
18905 case DW_FORM_strx1:
18906 case DW_FORM_strx2:
18907 case DW_FORM_strx3:
18908 case DW_FORM_strx4:
18909 case DW_FORM_GNU_str_index:
18910 {
18911 unsigned int str_index = attr->as_unsigned_reprocess ();
18912 gdb_assert (!attr->canonical_string_p ());
18913 if (reader->dwo_file != NULL)
18914 attr->set_string_noncanonical (read_dwo_str_index (reader,
18915 str_index));
18916 else
18917 attr->set_string_noncanonical (read_stub_str_index (cu,
18918 str_index));
18919 break;
18920 }
18921 default:
18922 gdb_assert_not_reached ("Unexpected DWARF form.");
18923 }
18924 }
18925
18926 /* Read an attribute value described by an attribute form. */
18927
18928 static const gdb_byte *
18929 read_attribute_value (const struct die_reader_specs *reader,
18930 struct attribute *attr, unsigned form,
18931 LONGEST implicit_const, const gdb_byte *info_ptr)
18932 {
18933 struct dwarf2_cu *cu = reader->cu;
18934 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18935 struct objfile *objfile = per_objfile->objfile;
18936 bfd *abfd = reader->abfd;
18937 struct comp_unit_head *cu_header = &cu->header;
18938 unsigned int bytes_read;
18939 struct dwarf_block *blk;
18940
18941 attr->form = (enum dwarf_form) form;
18942 switch (form)
18943 {
18944 case DW_FORM_ref_addr:
18945 if (cu_header->version == 2)
18946 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
18947 &bytes_read));
18948 else
18949 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
18950 &bytes_read));
18951 info_ptr += bytes_read;
18952 break;
18953 case DW_FORM_GNU_ref_alt:
18954 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
18955 &bytes_read));
18956 info_ptr += bytes_read;
18957 break;
18958 case DW_FORM_addr:
18959 {
18960 struct gdbarch *gdbarch = objfile->arch ();
18961 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
18962 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
18963 attr->set_address (addr);
18964 info_ptr += bytes_read;
18965 }
18966 break;
18967 case DW_FORM_block2:
18968 blk = dwarf_alloc_block (cu);
18969 blk->size = read_2_bytes (abfd, info_ptr);
18970 info_ptr += 2;
18971 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18972 info_ptr += blk->size;
18973 attr->set_block (blk);
18974 break;
18975 case DW_FORM_block4:
18976 blk = dwarf_alloc_block (cu);
18977 blk->size = read_4_bytes (abfd, info_ptr);
18978 info_ptr += 4;
18979 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18980 info_ptr += blk->size;
18981 attr->set_block (blk);
18982 break;
18983 case DW_FORM_data2:
18984 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
18985 info_ptr += 2;
18986 break;
18987 case DW_FORM_data4:
18988 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
18989 info_ptr += 4;
18990 break;
18991 case DW_FORM_data8:
18992 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
18993 info_ptr += 8;
18994 break;
18995 case DW_FORM_data16:
18996 blk = dwarf_alloc_block (cu);
18997 blk->size = 16;
18998 blk->data = read_n_bytes (abfd, info_ptr, 16);
18999 info_ptr += 16;
19000 attr->set_block (blk);
19001 break;
19002 case DW_FORM_sec_offset:
19003 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19004 &bytes_read));
19005 info_ptr += bytes_read;
19006 break;
19007 case DW_FORM_loclistx:
19008 {
19009 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19010 &bytes_read));
19011 info_ptr += bytes_read;
19012 }
19013 break;
19014 case DW_FORM_string:
19015 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19016 &bytes_read));
19017 info_ptr += bytes_read;
19018 break;
19019 case DW_FORM_strp:
19020 if (!cu->per_cu->is_dwz)
19021 {
19022 attr->set_string_noncanonical
19023 (read_indirect_string (per_objfile,
19024 abfd, info_ptr, cu_header,
19025 &bytes_read));
19026 info_ptr += bytes_read;
19027 break;
19028 }
19029 /* FALLTHROUGH */
19030 case DW_FORM_line_strp:
19031 if (!cu->per_cu->is_dwz)
19032 {
19033 attr->set_string_noncanonical
19034 (per_objfile->read_line_string (info_ptr, cu_header,
19035 &bytes_read));
19036 info_ptr += bytes_read;
19037 break;
19038 }
19039 /* FALLTHROUGH */
19040 case DW_FORM_GNU_strp_alt:
19041 {
19042 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19043 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19044 &bytes_read);
19045
19046 attr->set_string_noncanonical
19047 (dwz->read_string (objfile, str_offset));
19048 info_ptr += bytes_read;
19049 }
19050 break;
19051 case DW_FORM_exprloc:
19052 case DW_FORM_block:
19053 blk = dwarf_alloc_block (cu);
19054 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19055 info_ptr += bytes_read;
19056 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19057 info_ptr += blk->size;
19058 attr->set_block (blk);
19059 break;
19060 case DW_FORM_block1:
19061 blk = dwarf_alloc_block (cu);
19062 blk->size = read_1_byte (abfd, info_ptr);
19063 info_ptr += 1;
19064 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19065 info_ptr += blk->size;
19066 attr->set_block (blk);
19067 break;
19068 case DW_FORM_data1:
19069 case DW_FORM_flag:
19070 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19071 info_ptr += 1;
19072 break;
19073 case DW_FORM_flag_present:
19074 attr->set_unsigned (1);
19075 break;
19076 case DW_FORM_sdata:
19077 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19078 info_ptr += bytes_read;
19079 break;
19080 case DW_FORM_rnglistx:
19081 {
19082 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19083 &bytes_read));
19084 info_ptr += bytes_read;
19085 }
19086 break;
19087 case DW_FORM_udata:
19088 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19089 info_ptr += bytes_read;
19090 break;
19091 case DW_FORM_ref1:
19092 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19093 + read_1_byte (abfd, info_ptr)));
19094 info_ptr += 1;
19095 break;
19096 case DW_FORM_ref2:
19097 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19098 + read_2_bytes (abfd, info_ptr)));
19099 info_ptr += 2;
19100 break;
19101 case DW_FORM_ref4:
19102 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19103 + read_4_bytes (abfd, info_ptr)));
19104 info_ptr += 4;
19105 break;
19106 case DW_FORM_ref8:
19107 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19108 + read_8_bytes (abfd, info_ptr)));
19109 info_ptr += 8;
19110 break;
19111 case DW_FORM_ref_sig8:
19112 attr->set_signature (read_8_bytes (abfd, info_ptr));
19113 info_ptr += 8;
19114 break;
19115 case DW_FORM_ref_udata:
19116 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19117 + read_unsigned_leb128 (abfd, info_ptr,
19118 &bytes_read)));
19119 info_ptr += bytes_read;
19120 break;
19121 case DW_FORM_indirect:
19122 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19123 info_ptr += bytes_read;
19124 if (form == DW_FORM_implicit_const)
19125 {
19126 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19127 info_ptr += bytes_read;
19128 }
19129 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19130 info_ptr);
19131 break;
19132 case DW_FORM_implicit_const:
19133 attr->set_signed (implicit_const);
19134 break;
19135 case DW_FORM_addrx:
19136 case DW_FORM_GNU_addr_index:
19137 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19138 &bytes_read));
19139 info_ptr += bytes_read;
19140 break;
19141 case DW_FORM_strx:
19142 case DW_FORM_strx1:
19143 case DW_FORM_strx2:
19144 case DW_FORM_strx3:
19145 case DW_FORM_strx4:
19146 case DW_FORM_GNU_str_index:
19147 {
19148 ULONGEST str_index;
19149 if (form == DW_FORM_strx1)
19150 {
19151 str_index = read_1_byte (abfd, info_ptr);
19152 info_ptr += 1;
19153 }
19154 else if (form == DW_FORM_strx2)
19155 {
19156 str_index = read_2_bytes (abfd, info_ptr);
19157 info_ptr += 2;
19158 }
19159 else if (form == DW_FORM_strx3)
19160 {
19161 str_index = read_3_bytes (abfd, info_ptr);
19162 info_ptr += 3;
19163 }
19164 else if (form == DW_FORM_strx4)
19165 {
19166 str_index = read_4_bytes (abfd, info_ptr);
19167 info_ptr += 4;
19168 }
19169 else
19170 {
19171 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19172 info_ptr += bytes_read;
19173 }
19174 attr->set_unsigned_reprocess (str_index);
19175 }
19176 break;
19177 default:
19178 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19179 dwarf_form_name (form),
19180 bfd_get_filename (abfd));
19181 }
19182
19183 /* Super hack. */
19184 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19185 attr->form = DW_FORM_GNU_ref_alt;
19186
19187 /* We have seen instances where the compiler tried to emit a byte
19188 size attribute of -1 which ended up being encoded as an unsigned
19189 0xffffffff. Although 0xffffffff is technically a valid size value,
19190 an object of this size seems pretty unlikely so we can relatively
19191 safely treat these cases as if the size attribute was invalid and
19192 treat them as zero by default. */
19193 if (attr->name == DW_AT_byte_size
19194 && form == DW_FORM_data4
19195 && attr->as_unsigned () >= 0xffffffff)
19196 {
19197 complaint
19198 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19199 hex_string (attr->as_unsigned ()));
19200 attr->set_unsigned (0);
19201 }
19202
19203 return info_ptr;
19204 }
19205
19206 /* Read an attribute described by an abbreviated attribute. */
19207
19208 static const gdb_byte *
19209 read_attribute (const struct die_reader_specs *reader,
19210 struct attribute *attr, const struct attr_abbrev *abbrev,
19211 const gdb_byte *info_ptr)
19212 {
19213 attr->name = abbrev->name;
19214 attr->string_is_canonical = 0;
19215 attr->requires_reprocessing = 0;
19216 return read_attribute_value (reader, attr, abbrev->form,
19217 abbrev->implicit_const, info_ptr);
19218 }
19219
19220 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19221
19222 static const char *
19223 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19224 LONGEST str_offset)
19225 {
19226 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19227 str_offset, "DW_FORM_strp");
19228 }
19229
19230 /* Return pointer to string at .debug_str offset as read from BUF.
19231 BUF is assumed to be in a compilation unit described by CU_HEADER.
19232 Return *BYTES_READ_PTR count of bytes read from BUF. */
19233
19234 static const char *
19235 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19236 const gdb_byte *buf,
19237 const struct comp_unit_head *cu_header,
19238 unsigned int *bytes_read_ptr)
19239 {
19240 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19241
19242 return read_indirect_string_at_offset (per_objfile, str_offset);
19243 }
19244
19245 /* See read.h. */
19246
19247 const char *
19248 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19249 unsigned int offset_size)
19250 {
19251 bfd *abfd = objfile->obfd;
19252 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
19253
19254 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19255 }
19256
19257 /* See read.h. */
19258
19259 const char *
19260 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19261 const struct comp_unit_head *cu_header,
19262 unsigned int *bytes_read_ptr)
19263 {
19264 bfd *abfd = objfile->obfd;
19265 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19266
19267 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19268 }
19269
19270 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19271 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19272 ADDR_SIZE is the size of addresses from the CU header. */
19273
19274 static CORE_ADDR
19275 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19276 gdb::optional<ULONGEST> addr_base, int addr_size)
19277 {
19278 struct objfile *objfile = per_objfile->objfile;
19279 bfd *abfd = objfile->obfd;
19280 const gdb_byte *info_ptr;
19281 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19282
19283 per_objfile->per_bfd->addr.read (objfile);
19284 if (per_objfile->per_bfd->addr.buffer == NULL)
19285 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19286 objfile_name (objfile));
19287 if (addr_base_or_zero + addr_index * addr_size
19288 >= per_objfile->per_bfd->addr.size)
19289 error (_("DW_FORM_addr_index pointing outside of "
19290 ".debug_addr section [in module %s]"),
19291 objfile_name (objfile));
19292 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19293 + addr_index * addr_size);
19294 if (addr_size == 4)
19295 return bfd_get_32 (abfd, info_ptr);
19296 else
19297 return bfd_get_64 (abfd, info_ptr);
19298 }
19299
19300 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19301
19302 static CORE_ADDR
19303 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19304 {
19305 return read_addr_index_1 (cu->per_objfile, addr_index,
19306 cu->addr_base, cu->header.addr_size);
19307 }
19308
19309 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19310
19311 static CORE_ADDR
19312 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19313 unsigned int *bytes_read)
19314 {
19315 bfd *abfd = cu->per_objfile->objfile->obfd;
19316 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19317
19318 return read_addr_index (cu, addr_index);
19319 }
19320
19321 /* See read.h. */
19322
19323 CORE_ADDR
19324 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19325 dwarf2_per_objfile *per_objfile,
19326 unsigned int addr_index)
19327 {
19328 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19329 gdb::optional<ULONGEST> addr_base;
19330 int addr_size;
19331
19332 /* We need addr_base and addr_size.
19333 If we don't have PER_CU->cu, we have to get it.
19334 Nasty, but the alternative is storing the needed info in PER_CU,
19335 which at this point doesn't seem justified: it's not clear how frequently
19336 it would get used and it would increase the size of every PER_CU.
19337 Entry points like dwarf2_per_cu_addr_size do a similar thing
19338 so we're not in uncharted territory here.
19339 Alas we need to be a bit more complicated as addr_base is contained
19340 in the DIE.
19341
19342 We don't need to read the entire CU(/TU).
19343 We just need the header and top level die.
19344
19345 IWBN to use the aging mechanism to let us lazily later discard the CU.
19346 For now we skip this optimization. */
19347
19348 if (cu != NULL)
19349 {
19350 addr_base = cu->addr_base;
19351 addr_size = cu->header.addr_size;
19352 }
19353 else
19354 {
19355 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19356 addr_base = reader.cu->addr_base;
19357 addr_size = reader.cu->header.addr_size;
19358 }
19359
19360 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19361 }
19362
19363 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19364 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19365 DWO file. */
19366
19367 static const char *
19368 read_str_index (struct dwarf2_cu *cu,
19369 struct dwarf2_section_info *str_section,
19370 struct dwarf2_section_info *str_offsets_section,
19371 ULONGEST str_offsets_base, ULONGEST str_index)
19372 {
19373 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19374 struct objfile *objfile = per_objfile->objfile;
19375 const char *objf_name = objfile_name (objfile);
19376 bfd *abfd = objfile->obfd;
19377 const gdb_byte *info_ptr;
19378 ULONGEST str_offset;
19379 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19380
19381 str_section->read (objfile);
19382 str_offsets_section->read (objfile);
19383 if (str_section->buffer == NULL)
19384 error (_("%s used without %s section"
19385 " in CU at offset %s [in module %s]"),
19386 form_name, str_section->get_name (),
19387 sect_offset_str (cu->header.sect_off), objf_name);
19388 if (str_offsets_section->buffer == NULL)
19389 error (_("%s used without %s section"
19390 " in CU at offset %s [in module %s]"),
19391 form_name, str_section->get_name (),
19392 sect_offset_str (cu->header.sect_off), objf_name);
19393 info_ptr = (str_offsets_section->buffer
19394 + str_offsets_base
19395 + str_index * cu->header.offset_size);
19396 if (cu->header.offset_size == 4)
19397 str_offset = bfd_get_32 (abfd, info_ptr);
19398 else
19399 str_offset = bfd_get_64 (abfd, info_ptr);
19400 if (str_offset >= str_section->size)
19401 error (_("Offset from %s pointing outside of"
19402 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19403 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19404 return (const char *) (str_section->buffer + str_offset);
19405 }
19406
19407 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19408
19409 static const char *
19410 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19411 {
19412 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19413 ? reader->cu->header.addr_size : 0;
19414 return read_str_index (reader->cu,
19415 &reader->dwo_file->sections.str,
19416 &reader->dwo_file->sections.str_offsets,
19417 str_offsets_base, str_index);
19418 }
19419
19420 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19421
19422 static const char *
19423 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19424 {
19425 struct objfile *objfile = cu->per_objfile->objfile;
19426 const char *objf_name = objfile_name (objfile);
19427 static const char form_name[] = "DW_FORM_GNU_str_index";
19428 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19429
19430 if (!cu->str_offsets_base.has_value ())
19431 error (_("%s used in Fission stub without %s"
19432 " in CU at offset 0x%lx [in module %s]"),
19433 form_name, str_offsets_attr_name,
19434 (long) cu->header.offset_size, objf_name);
19435
19436 return read_str_index (cu,
19437 &cu->per_objfile->per_bfd->str,
19438 &cu->per_objfile->per_bfd->str_offsets,
19439 *cu->str_offsets_base, str_index);
19440 }
19441
19442 /* Return the length of an LEB128 number in BUF. */
19443
19444 static int
19445 leb128_size (const gdb_byte *buf)
19446 {
19447 const gdb_byte *begin = buf;
19448 gdb_byte byte;
19449
19450 while (1)
19451 {
19452 byte = *buf++;
19453 if ((byte & 128) == 0)
19454 return buf - begin;
19455 }
19456 }
19457
19458 static enum language
19459 dwarf_lang_to_enum_language (unsigned int lang)
19460 {
19461 enum language language;
19462
19463 switch (lang)
19464 {
19465 case DW_LANG_C89:
19466 case DW_LANG_C99:
19467 case DW_LANG_C11:
19468 case DW_LANG_C:
19469 case DW_LANG_UPC:
19470 language = language_c;
19471 break;
19472 case DW_LANG_Java:
19473 case DW_LANG_C_plus_plus:
19474 case DW_LANG_C_plus_plus_11:
19475 case DW_LANG_C_plus_plus_14:
19476 language = language_cplus;
19477 break;
19478 case DW_LANG_D:
19479 language = language_d;
19480 break;
19481 case DW_LANG_Fortran77:
19482 case DW_LANG_Fortran90:
19483 case DW_LANG_Fortran95:
19484 case DW_LANG_Fortran03:
19485 case DW_LANG_Fortran08:
19486 language = language_fortran;
19487 break;
19488 case DW_LANG_Go:
19489 language = language_go;
19490 break;
19491 case DW_LANG_Mips_Assembler:
19492 language = language_asm;
19493 break;
19494 case DW_LANG_Ada83:
19495 case DW_LANG_Ada95:
19496 language = language_ada;
19497 break;
19498 case DW_LANG_Modula2:
19499 language = language_m2;
19500 break;
19501 case DW_LANG_Pascal83:
19502 language = language_pascal;
19503 break;
19504 case DW_LANG_ObjC:
19505 language = language_objc;
19506 break;
19507 case DW_LANG_Rust:
19508 case DW_LANG_Rust_old:
19509 language = language_rust;
19510 break;
19511 case DW_LANG_OpenCL:
19512 language = language_opencl;
19513 break;
19514 case DW_LANG_Cobol74:
19515 case DW_LANG_Cobol85:
19516 default:
19517 language = language_minimal;
19518 break;
19519 }
19520
19521 return language;
19522 }
19523
19524 /* Return the named attribute or NULL if not there. */
19525
19526 static struct attribute *
19527 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19528 {
19529 for (;;)
19530 {
19531 unsigned int i;
19532 struct attribute *spec = NULL;
19533
19534 for (i = 0; i < die->num_attrs; ++i)
19535 {
19536 if (die->attrs[i].name == name)
19537 return &die->attrs[i];
19538 if (die->attrs[i].name == DW_AT_specification
19539 || die->attrs[i].name == DW_AT_abstract_origin)
19540 spec = &die->attrs[i];
19541 }
19542
19543 if (!spec)
19544 break;
19545
19546 die = follow_die_ref (die, spec, &cu);
19547 }
19548
19549 return NULL;
19550 }
19551
19552 /* Return the string associated with a string-typed attribute, or NULL if it
19553 is either not found or is of an incorrect type. */
19554
19555 static const char *
19556 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19557 {
19558 struct attribute *attr;
19559 const char *str = NULL;
19560
19561 attr = dwarf2_attr (die, name, cu);
19562
19563 if (attr != NULL)
19564 {
19565 str = attr->as_string ();
19566 if (str == nullptr)
19567 complaint (_("string type expected for attribute %s for "
19568 "DIE at %s in module %s"),
19569 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19570 objfile_name (cu->per_objfile->objfile));
19571 }
19572
19573 return str;
19574 }
19575
19576 /* Return the dwo name or NULL if not present. If present, it is in either
19577 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19578 static const char *
19579 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19580 {
19581 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19582 if (dwo_name == nullptr)
19583 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19584 return dwo_name;
19585 }
19586
19587 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19588 and holds a non-zero value. This function should only be used for
19589 DW_FORM_flag or DW_FORM_flag_present attributes. */
19590
19591 static int
19592 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19593 {
19594 struct attribute *attr = dwarf2_attr (die, name, cu);
19595
19596 return attr != nullptr && attr->as_boolean ();
19597 }
19598
19599 static int
19600 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19601 {
19602 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19603 which value is non-zero. However, we have to be careful with
19604 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19605 (via dwarf2_flag_true_p) follows this attribute. So we may
19606 end up accidently finding a declaration attribute that belongs
19607 to a different DIE referenced by the specification attribute,
19608 even though the given DIE does not have a declaration attribute. */
19609 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19610 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19611 }
19612
19613 /* Return the die giving the specification for DIE, if there is
19614 one. *SPEC_CU is the CU containing DIE on input, and the CU
19615 containing the return value on output. If there is no
19616 specification, but there is an abstract origin, that is
19617 returned. */
19618
19619 static struct die_info *
19620 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19621 {
19622 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19623 *spec_cu);
19624
19625 if (spec_attr == NULL)
19626 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19627
19628 if (spec_attr == NULL)
19629 return NULL;
19630 else
19631 return follow_die_ref (die, spec_attr, spec_cu);
19632 }
19633
19634 /* A convenience function to find the proper .debug_line section for a CU. */
19635
19636 static struct dwarf2_section_info *
19637 get_debug_line_section (struct dwarf2_cu *cu)
19638 {
19639 struct dwarf2_section_info *section;
19640 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19641
19642 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19643 DWO file. */
19644 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19645 section = &cu->dwo_unit->dwo_file->sections.line;
19646 else if (cu->per_cu->is_dwz)
19647 {
19648 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19649
19650 section = &dwz->line;
19651 }
19652 else
19653 section = &per_objfile->per_bfd->line;
19654
19655 return section;
19656 }
19657
19658 /* Read the statement program header starting at OFFSET in
19659 .debug_line, or .debug_line.dwo. Return a pointer
19660 to a struct line_header, allocated using xmalloc.
19661 Returns NULL if there is a problem reading the header, e.g., if it
19662 has a version we don't understand.
19663
19664 NOTE: the strings in the include directory and file name tables of
19665 the returned object point into the dwarf line section buffer,
19666 and must not be freed. */
19667
19668 static line_header_up
19669 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19670 {
19671 struct dwarf2_section_info *section;
19672 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19673
19674 section = get_debug_line_section (cu);
19675 section->read (per_objfile->objfile);
19676 if (section->buffer == NULL)
19677 {
19678 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19679 complaint (_("missing .debug_line.dwo section"));
19680 else
19681 complaint (_("missing .debug_line section"));
19682 return 0;
19683 }
19684
19685 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19686 per_objfile, section, &cu->header);
19687 }
19688
19689 /* Subroutine of dwarf_decode_lines to simplify it.
19690 Return the file name for the given file_entry.
19691 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19692 If space for the result is malloc'd, *NAME_HOLDER will be set.
19693 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19694 equivalent to CU_INFO. */
19695
19696 static const char *
19697 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
19698 const file_and_directory &cu_info,
19699 std::string &name_holder)
19700 {
19701 const char *include_name = fe.name;
19702 const char *include_name_to_compare = include_name;
19703
19704 const char *dir_name = fe.include_dir (lh);
19705
19706 std::string hold_compare;
19707 if (!IS_ABSOLUTE_PATH (include_name)
19708 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
19709 {
19710 /* Avoid creating a duplicate name for CU_INFO.
19711 We do this by comparing INCLUDE_NAME and CU_INFO.
19712 Before we do the comparison, however, we need to account
19713 for DIR_NAME and COMP_DIR.
19714 First prepend dir_name (if non-NULL). If we still don't
19715 have an absolute path prepend comp_dir (if non-NULL).
19716 However, the directory we record in the include-file's
19717 psymtab does not contain COMP_DIR (to match the
19718 corresponding symtab(s)).
19719
19720 Example:
19721
19722 bash$ cd /tmp
19723 bash$ gcc -g ./hello.c
19724 include_name = "hello.c"
19725 dir_name = "."
19726 DW_AT_comp_dir = comp_dir = "/tmp"
19727 DW_AT_name = "./hello.c"
19728
19729 */
19730
19731 if (dir_name != NULL)
19732 {
19733 name_holder = path_join (dir_name, include_name);
19734 include_name = name_holder.c_str ();
19735 include_name_to_compare = include_name;
19736 }
19737 if (!IS_ABSOLUTE_PATH (include_name)
19738 && cu_info.get_comp_dir () != nullptr)
19739 {
19740 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
19741 include_name_to_compare = hold_compare.c_str ();
19742 }
19743 }
19744
19745 std::string copied_name;
19746 const char *cu_filename = cu_info.get_name ();
19747 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
19748 {
19749 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
19750 cu_filename = copied_name.c_str ();
19751 }
19752
19753 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
19754 return nullptr;
19755 return include_name;
19756 }
19757
19758 /* State machine to track the state of the line number program. */
19759
19760 class lnp_state_machine
19761 {
19762 public:
19763 /* Initialize a machine state for the start of a line number
19764 program. */
19765 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
19766
19767 file_entry *current_file ()
19768 {
19769 /* lh->file_names is 0-based, but the file name numbers in the
19770 statement program are 1-based. */
19771 return m_line_header->file_name_at (m_file);
19772 }
19773
19774 /* Record the line in the state machine. END_SEQUENCE is true if
19775 we're processing the end of a sequence. */
19776 void record_line (bool end_sequence);
19777
19778 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19779 nop-out rest of the lines in this sequence. */
19780 void check_line_address (struct dwarf2_cu *cu,
19781 const gdb_byte *line_ptr,
19782 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19783
19784 void handle_set_discriminator (unsigned int discriminator)
19785 {
19786 m_discriminator = discriminator;
19787 m_line_has_non_zero_discriminator |= discriminator != 0;
19788 }
19789
19790 /* Handle DW_LNE_set_address. */
19791 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19792 {
19793 m_op_index = 0;
19794 address += baseaddr;
19795 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19796 }
19797
19798 /* Handle DW_LNS_advance_pc. */
19799 void handle_advance_pc (CORE_ADDR adjust);
19800
19801 /* Handle a special opcode. */
19802 void handle_special_opcode (unsigned char op_code);
19803
19804 /* Handle DW_LNS_advance_line. */
19805 void handle_advance_line (int line_delta)
19806 {
19807 advance_line (line_delta);
19808 }
19809
19810 /* Handle DW_LNS_set_file. */
19811 void handle_set_file (file_name_index file);
19812
19813 /* Handle DW_LNS_negate_stmt. */
19814 void handle_negate_stmt ()
19815 {
19816 m_flags ^= LEF_IS_STMT;
19817 }
19818
19819 /* Handle DW_LNS_const_add_pc. */
19820 void handle_const_add_pc ();
19821
19822 /* Handle DW_LNS_fixed_advance_pc. */
19823 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19824 {
19825 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19826 m_op_index = 0;
19827 }
19828
19829 /* Handle DW_LNS_copy. */
19830 void handle_copy ()
19831 {
19832 record_line (false);
19833 m_discriminator = 0;
19834 m_flags &= ~LEF_PROLOGUE_END;
19835 }
19836
19837 /* Handle DW_LNE_end_sequence. */
19838 void handle_end_sequence ()
19839 {
19840 m_currently_recording_lines = true;
19841 }
19842
19843 /* Handle DW_LNS_set_prologue_end. */
19844 void handle_set_prologue_end ()
19845 {
19846 m_flags |= LEF_PROLOGUE_END;
19847 }
19848
19849 private:
19850 /* Advance the line by LINE_DELTA. */
19851 void advance_line (int line_delta)
19852 {
19853 m_line += line_delta;
19854
19855 if (line_delta != 0)
19856 m_line_has_non_zero_discriminator = m_discriminator != 0;
19857 }
19858
19859 struct dwarf2_cu *m_cu;
19860
19861 gdbarch *m_gdbarch;
19862
19863 /* The line number header. */
19864 line_header *m_line_header;
19865
19866 /* These are part of the standard DWARF line number state machine,
19867 and initialized according to the DWARF spec. */
19868
19869 unsigned char m_op_index = 0;
19870 /* The line table index of the current file. */
19871 file_name_index m_file = 1;
19872 unsigned int m_line = 1;
19873
19874 /* These are initialized in the constructor. */
19875
19876 CORE_ADDR m_address;
19877 linetable_entry_flags m_flags;
19878 unsigned int m_discriminator;
19879
19880 /* Additional bits of state we need to track. */
19881
19882 /* The last file that we called dwarf2_start_subfile for.
19883 This is only used for TLLs. */
19884 unsigned int m_last_file = 0;
19885 /* The last file a line number was recorded for. */
19886 struct subfile *m_last_subfile = NULL;
19887
19888 /* The address of the last line entry. */
19889 CORE_ADDR m_last_address;
19890
19891 /* Set to true when a previous line at the same address (using
19892 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
19893 when a line entry at a new address (m_address different to
19894 m_last_address) is processed. */
19895 bool m_stmt_at_address = false;
19896
19897 /* When true, record the lines we decode. */
19898 bool m_currently_recording_lines = false;
19899
19900 /* The last line number that was recorded, used to coalesce
19901 consecutive entries for the same line. This can happen, for
19902 example, when discriminators are present. PR 17276. */
19903 unsigned int m_last_line = 0;
19904 bool m_line_has_non_zero_discriminator = false;
19905 };
19906
19907 void
19908 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19909 {
19910 CORE_ADDR addr_adj = (((m_op_index + adjust)
19911 / m_line_header->maximum_ops_per_instruction)
19912 * m_line_header->minimum_instruction_length);
19913 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19914 m_op_index = ((m_op_index + adjust)
19915 % m_line_header->maximum_ops_per_instruction);
19916 }
19917
19918 void
19919 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19920 {
19921 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19922 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19923 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19924 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19925 / m_line_header->maximum_ops_per_instruction)
19926 * m_line_header->minimum_instruction_length);
19927 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19928 m_op_index = ((m_op_index + adj_opcode_d)
19929 % m_line_header->maximum_ops_per_instruction);
19930
19931 int line_delta = m_line_header->line_base + adj_opcode_r;
19932 advance_line (line_delta);
19933 record_line (false);
19934 m_discriminator = 0;
19935 m_flags &= ~LEF_PROLOGUE_END;
19936 }
19937
19938 void
19939 lnp_state_machine::handle_set_file (file_name_index file)
19940 {
19941 m_file = file;
19942
19943 const file_entry *fe = current_file ();
19944 if (fe == NULL)
19945 dwarf2_debug_line_missing_file_complaint ();
19946 else
19947 {
19948 const char *dir = fe->include_dir (m_line_header);
19949
19950 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19951 m_line_has_non_zero_discriminator = m_discriminator != 0;
19952 dwarf2_start_subfile (m_cu, fe->name, dir);
19953 }
19954 }
19955
19956 void
19957 lnp_state_machine::handle_const_add_pc ()
19958 {
19959 CORE_ADDR adjust
19960 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19961
19962 CORE_ADDR addr_adj
19963 = (((m_op_index + adjust)
19964 / m_line_header->maximum_ops_per_instruction)
19965 * m_line_header->minimum_instruction_length);
19966
19967 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19968 m_op_index = ((m_op_index + adjust)
19969 % m_line_header->maximum_ops_per_instruction);
19970 }
19971
19972 /* Return non-zero if we should add LINE to the line number table.
19973 LINE is the line to add, LAST_LINE is the last line that was added,
19974 LAST_SUBFILE is the subfile for LAST_LINE.
19975 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19976 had a non-zero discriminator.
19977
19978 We have to be careful in the presence of discriminators.
19979 E.g., for this line:
19980
19981 for (i = 0; i < 100000; i++);
19982
19983 clang can emit four line number entries for that one line,
19984 each with a different discriminator.
19985 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19986
19987 However, we want gdb to coalesce all four entries into one.
19988 Otherwise the user could stepi into the middle of the line and
19989 gdb would get confused about whether the pc really was in the
19990 middle of the line.
19991
19992 Things are further complicated by the fact that two consecutive
19993 line number entries for the same line is a heuristic used by gcc
19994 to denote the end of the prologue. So we can't just discard duplicate
19995 entries, we have to be selective about it. The heuristic we use is
19996 that we only collapse consecutive entries for the same line if at least
19997 one of those entries has a non-zero discriminator. PR 17276.
19998
19999 Note: Addresses in the line number state machine can never go backwards
20000 within one sequence, thus this coalescing is ok. */
20001
20002 static int
20003 dwarf_record_line_p (struct dwarf2_cu *cu,
20004 unsigned int line, unsigned int last_line,
20005 int line_has_non_zero_discriminator,
20006 struct subfile *last_subfile)
20007 {
20008 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20009 return 1;
20010 if (line != last_line)
20011 return 1;
20012 /* Same line for the same file that we've seen already.
20013 As a last check, for pr 17276, only record the line if the line
20014 has never had a non-zero discriminator. */
20015 if (!line_has_non_zero_discriminator)
20016 return 1;
20017 return 0;
20018 }
20019
20020 /* Use the CU's builder to record line number LINE beginning at
20021 address ADDRESS in the line table of subfile SUBFILE. */
20022
20023 static void
20024 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20025 unsigned int line, CORE_ADDR address,
20026 linetable_entry_flags flags,
20027 struct dwarf2_cu *cu)
20028 {
20029 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20030
20031 if (dwarf_line_debug)
20032 {
20033 gdb_printf (gdb_stdlog,
20034 "Recording line %u, file %s, address %s\n",
20035 line, lbasename (subfile->name.c_str ()),
20036 paddress (gdbarch, address));
20037 }
20038
20039 if (cu != nullptr)
20040 cu->get_builder ()->record_line (subfile, line, addr, flags);
20041 }
20042
20043 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20044 Mark the end of a set of line number records.
20045 The arguments are the same as for dwarf_record_line_1.
20046 If SUBFILE is NULL the request is ignored. */
20047
20048 static void
20049 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20050 CORE_ADDR address, struct dwarf2_cu *cu)
20051 {
20052 if (subfile == NULL)
20053 return;
20054
20055 if (dwarf_line_debug)
20056 {
20057 gdb_printf (gdb_stdlog,
20058 "Finishing current line, file %s, address %s\n",
20059 lbasename (subfile->name.c_str ()),
20060 paddress (gdbarch, address));
20061 }
20062
20063 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
20064 }
20065
20066 void
20067 lnp_state_machine::record_line (bool end_sequence)
20068 {
20069 if (dwarf_line_debug)
20070 {
20071 gdb_printf (gdb_stdlog,
20072 "Processing actual line %u: file %u,"
20073 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20074 m_line, m_file,
20075 paddress (m_gdbarch, m_address),
20076 (m_flags & LEF_IS_STMT) != 0,
20077 (m_flags & LEF_PROLOGUE_END) != 0,
20078 m_discriminator,
20079 (end_sequence ? "\t(end sequence)" : ""));
20080 }
20081
20082 file_entry *fe = current_file ();
20083
20084 if (fe == NULL)
20085 dwarf2_debug_line_missing_file_complaint ();
20086 /* For now we ignore lines not starting on an instruction boundary.
20087 But not when processing end_sequence for compatibility with the
20088 previous version of the code. */
20089 else if (m_op_index == 0 || end_sequence)
20090 {
20091 /* When we switch files we insert an end maker in the first file,
20092 switch to the second file and add a new line entry. The
20093 problem is that the end marker inserted in the first file will
20094 discard any previous line entries at the same address. If the
20095 line entries in the first file are marked as is-stmt, while
20096 the new line in the second file is non-stmt, then this means
20097 the end marker will discard is-stmt lines so we can have a
20098 non-stmt line. This means that there are less addresses at
20099 which the user can insert a breakpoint.
20100
20101 To improve this we track the last address in m_last_address,
20102 and whether we have seen an is-stmt at this address. Then
20103 when switching files, if we have seen a stmt at the current
20104 address, and we are switching to create a non-stmt line, then
20105 discard the new line. */
20106 bool file_changed
20107 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20108 bool ignore_this_line
20109 = ((file_changed && !end_sequence && m_last_address == m_address
20110 && ((m_flags & LEF_IS_STMT) == 0)
20111 && m_stmt_at_address)
20112 || (!end_sequence && m_line == 0));
20113
20114 if ((file_changed && !ignore_this_line) || end_sequence)
20115 {
20116 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20117 m_currently_recording_lines ? m_cu : nullptr);
20118 }
20119
20120 if (!end_sequence && !ignore_this_line)
20121 {
20122 linetable_entry_flags lte_flags = m_flags;
20123 if (producer_is_codewarrior (m_cu))
20124 lte_flags |= LEF_IS_STMT;
20125
20126 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20127 m_line_has_non_zero_discriminator,
20128 m_last_subfile))
20129 {
20130 buildsym_compunit *builder = m_cu->get_builder ();
20131 dwarf_record_line_1 (m_gdbarch,
20132 builder->get_current_subfile (),
20133 m_line, m_address, lte_flags,
20134 m_currently_recording_lines ? m_cu : nullptr);
20135 }
20136 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20137 m_last_line = m_line;
20138 }
20139 }
20140
20141 /* Track whether we have seen any IS_STMT true at m_address in case we
20142 have multiple line table entries all at m_address. */
20143 if (m_last_address != m_address)
20144 {
20145 m_stmt_at_address = false;
20146 m_last_address = m_address;
20147 }
20148 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
20149 }
20150
20151 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20152 line_header *lh)
20153 {
20154 m_cu = cu;
20155 m_gdbarch = arch;
20156 m_line_header = lh;
20157
20158 m_currently_recording_lines = true;
20159
20160 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20161 was a line entry for it so that the backend has a chance to adjust it
20162 and also record it in case it needs it. This is currently used by MIPS
20163 code, cf. `mips_adjust_dwarf2_line'. */
20164 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20165 m_flags = 0;
20166 if (lh->default_is_stmt)
20167 m_flags |= LEF_IS_STMT;
20168 m_discriminator = 0;
20169
20170 m_last_address = m_address;
20171 m_stmt_at_address = false;
20172 }
20173
20174 void
20175 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20176 const gdb_byte *line_ptr,
20177 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20178 {
20179 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20180 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20181 located at 0x0. In this case, additionally check that if
20182 ADDRESS < UNRELOCATED_LOWPC. */
20183
20184 if ((address == 0 && address < unrelocated_lowpc)
20185 || address == (CORE_ADDR) -1)
20186 {
20187 /* This line table is for a function which has been
20188 GCd by the linker. Ignore it. PR gdb/12528 */
20189
20190 struct objfile *objfile = cu->per_objfile->objfile;
20191 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20192
20193 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20194 line_offset, objfile_name (objfile));
20195 m_currently_recording_lines = false;
20196 /* Note: m_currently_recording_lines is left as false until we see
20197 DW_LNE_end_sequence. */
20198 }
20199 }
20200
20201 /* Subroutine of dwarf_decode_lines to simplify it.
20202 Process the line number information in LH. */
20203
20204 static void
20205 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20206 CORE_ADDR lowpc)
20207 {
20208 const gdb_byte *line_ptr, *extended_end;
20209 const gdb_byte *line_end;
20210 unsigned int bytes_read, extended_len;
20211 unsigned char op_code, extended_op;
20212 CORE_ADDR baseaddr;
20213 struct objfile *objfile = cu->per_objfile->objfile;
20214 bfd *abfd = objfile->obfd;
20215 struct gdbarch *gdbarch = objfile->arch ();
20216
20217 baseaddr = objfile->text_section_offset ();
20218
20219 line_ptr = lh->statement_program_start;
20220 line_end = lh->statement_program_end;
20221
20222 /* Read the statement sequences until there's nothing left. */
20223 while (line_ptr < line_end)
20224 {
20225 /* The DWARF line number program state machine. Reset the state
20226 machine at the start of each sequence. */
20227 lnp_state_machine state_machine (cu, gdbarch, lh);
20228 bool end_sequence = false;
20229
20230 /* Start a subfile for the current file of the state
20231 machine. */
20232 const file_entry *fe = state_machine.current_file ();
20233
20234 if (fe != NULL)
20235 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20236
20237 /* Decode the table. */
20238 while (line_ptr < line_end && !end_sequence)
20239 {
20240 op_code = read_1_byte (abfd, line_ptr);
20241 line_ptr += 1;
20242
20243 if (op_code >= lh->opcode_base)
20244 {
20245 /* Special opcode. */
20246 state_machine.handle_special_opcode (op_code);
20247 }
20248 else switch (op_code)
20249 {
20250 case DW_LNS_extended_op:
20251 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20252 &bytes_read);
20253 line_ptr += bytes_read;
20254 extended_end = line_ptr + extended_len;
20255 extended_op = read_1_byte (abfd, line_ptr);
20256 line_ptr += 1;
20257 if (DW_LNE_lo_user <= extended_op
20258 && extended_op <= DW_LNE_hi_user)
20259 {
20260 /* Vendor extension, ignore. */
20261 line_ptr = extended_end;
20262 break;
20263 }
20264 switch (extended_op)
20265 {
20266 case DW_LNE_end_sequence:
20267 state_machine.handle_end_sequence ();
20268 end_sequence = true;
20269 break;
20270 case DW_LNE_set_address:
20271 {
20272 CORE_ADDR address
20273 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20274 line_ptr += bytes_read;
20275
20276 state_machine.check_line_address (cu, line_ptr,
20277 lowpc - baseaddr, address);
20278 state_machine.handle_set_address (baseaddr, address);
20279 }
20280 break;
20281 case DW_LNE_define_file:
20282 {
20283 const char *cur_file;
20284 unsigned int mod_time, length;
20285 dir_index dindex;
20286
20287 cur_file = read_direct_string (abfd, line_ptr,
20288 &bytes_read);
20289 line_ptr += bytes_read;
20290 dindex = (dir_index)
20291 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20292 line_ptr += bytes_read;
20293 mod_time =
20294 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20295 line_ptr += bytes_read;
20296 length =
20297 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20298 line_ptr += bytes_read;
20299 lh->add_file_name (cur_file, dindex, mod_time, length);
20300 }
20301 break;
20302 case DW_LNE_set_discriminator:
20303 {
20304 /* The discriminator is not interesting to the
20305 debugger; just ignore it. We still need to
20306 check its value though:
20307 if there are consecutive entries for the same
20308 (non-prologue) line we want to coalesce them.
20309 PR 17276. */
20310 unsigned int discr
20311 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20312 line_ptr += bytes_read;
20313
20314 state_machine.handle_set_discriminator (discr);
20315 }
20316 break;
20317 default:
20318 complaint (_("mangled .debug_line section"));
20319 return;
20320 }
20321 /* Make sure that we parsed the extended op correctly. If e.g.
20322 we expected a different address size than the producer used,
20323 we may have read the wrong number of bytes. */
20324 if (line_ptr != extended_end)
20325 {
20326 complaint (_("mangled .debug_line section"));
20327 return;
20328 }
20329 break;
20330 case DW_LNS_copy:
20331 state_machine.handle_copy ();
20332 break;
20333 case DW_LNS_advance_pc:
20334 {
20335 CORE_ADDR adjust
20336 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20337 line_ptr += bytes_read;
20338
20339 state_machine.handle_advance_pc (adjust);
20340 }
20341 break;
20342 case DW_LNS_advance_line:
20343 {
20344 int line_delta
20345 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20346 line_ptr += bytes_read;
20347
20348 state_machine.handle_advance_line (line_delta);
20349 }
20350 break;
20351 case DW_LNS_set_file:
20352 {
20353 file_name_index file
20354 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20355 &bytes_read);
20356 line_ptr += bytes_read;
20357
20358 state_machine.handle_set_file (file);
20359 }
20360 break;
20361 case DW_LNS_set_column:
20362 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20363 line_ptr += bytes_read;
20364 break;
20365 case DW_LNS_negate_stmt:
20366 state_machine.handle_negate_stmt ();
20367 break;
20368 case DW_LNS_set_basic_block:
20369 break;
20370 /* Add to the address register of the state machine the
20371 address increment value corresponding to special opcode
20372 255. I.e., this value is scaled by the minimum
20373 instruction length since special opcode 255 would have
20374 scaled the increment. */
20375 case DW_LNS_const_add_pc:
20376 state_machine.handle_const_add_pc ();
20377 break;
20378 case DW_LNS_fixed_advance_pc:
20379 {
20380 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20381 line_ptr += 2;
20382
20383 state_machine.handle_fixed_advance_pc (addr_adj);
20384 }
20385 break;
20386 case DW_LNS_set_prologue_end:
20387 state_machine.handle_set_prologue_end ();
20388 break;
20389 default:
20390 {
20391 /* Unknown standard opcode, ignore it. */
20392 int i;
20393
20394 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20395 {
20396 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20397 line_ptr += bytes_read;
20398 }
20399 }
20400 }
20401 }
20402
20403 if (!end_sequence)
20404 dwarf2_debug_line_missing_end_sequence_complaint ();
20405
20406 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20407 in which case we still finish recording the last line). */
20408 state_machine.record_line (true);
20409 }
20410 }
20411
20412 /* Decode the Line Number Program (LNP) for the given line_header
20413 structure and CU. The actual information extracted and the type
20414 of structures created from the LNP depends on the value of PST.
20415
20416 FND holds the CU file name and directory, if known.
20417 It is used for relative paths in the line table.
20418
20419 NOTE: It is important that psymtabs have the same file name (via
20420 strcmp) as the corresponding symtab. Since the directory is not
20421 used in the name of the symtab we don't use it in the name of the
20422 psymtabs we create. E.g. expand_line_sal requires this when
20423 finding psymtabs to expand. A good testcase for this is
20424 mb-inline.exp.
20425
20426 LOWPC is the lowest address in CU (or 0 if not known).
20427
20428 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20429 for its PC<->lines mapping information. Otherwise only the filename
20430 table is read in. */
20431
20432 static void
20433 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
20434 CORE_ADDR lowpc, int decode_mapping)
20435 {
20436 if (decode_mapping)
20437 dwarf_decode_lines_1 (lh, cu, lowpc);
20438
20439 /* Make sure a symtab is created for every file, even files
20440 which contain only variables (i.e. no code with associated
20441 line numbers). */
20442 buildsym_compunit *builder = cu->get_builder ();
20443 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20444
20445 for (auto &fe : lh->file_names ())
20446 {
20447 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20448 if (builder->get_current_subfile ()->symtab == NULL)
20449 {
20450 builder->get_current_subfile ()->symtab
20451 = allocate_symtab (cust,
20452 builder->get_current_subfile ()->name.c_str ());
20453 }
20454 fe.symtab = builder->get_current_subfile ()->symtab;
20455 }
20456 }
20457
20458 /* Start a subfile for DWARF. FILENAME is the name of the file and
20459 DIRNAME the name of the source directory which contains FILENAME
20460 or NULL if not known.
20461 This routine tries to keep line numbers from identical absolute and
20462 relative file names in a common subfile.
20463
20464 Using the `list' example from the GDB testsuite, which resides in
20465 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20466 of /srcdir/list0.c yields the following debugging information for list0.c:
20467
20468 DW_AT_name: /srcdir/list0.c
20469 DW_AT_comp_dir: /compdir
20470 files.files[0].name: list0.h
20471 files.files[0].dir: /srcdir
20472 files.files[1].name: list0.c
20473 files.files[1].dir: /srcdir
20474
20475 The line number information for list0.c has to end up in a single
20476 subfile, so that `break /srcdir/list0.c:1' works as expected.
20477 start_subfile will ensure that this happens provided that we pass the
20478 concatenation of files.files[1].dir and files.files[1].name as the
20479 subfile's name. */
20480
20481 static void
20482 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20483 const char *dirname)
20484 {
20485 std::string copy;
20486
20487 /* In order not to lose the line information directory,
20488 we concatenate it to the filename when it makes sense.
20489 Note that the Dwarf3 standard says (speaking of filenames in line
20490 information): ``The directory index is ignored for file names
20491 that represent full path names''. Thus ignoring dirname in the
20492 `else' branch below isn't an issue. */
20493
20494 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20495 {
20496 copy = path_join (dirname, filename);
20497 filename = copy.c_str ();
20498 }
20499
20500 cu->get_builder ()->start_subfile (filename);
20501 }
20502
20503 static void
20504 var_decode_location (struct attribute *attr, struct symbol *sym,
20505 struct dwarf2_cu *cu)
20506 {
20507 struct objfile *objfile = cu->per_objfile->objfile;
20508 struct comp_unit_head *cu_header = &cu->header;
20509
20510 /* NOTE drow/2003-01-30: There used to be a comment and some special
20511 code here to turn a symbol with DW_AT_external and a
20512 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20513 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20514 with some versions of binutils) where shared libraries could have
20515 relocations against symbols in their debug information - the
20516 minimal symbol would have the right address, but the debug info
20517 would not. It's no longer necessary, because we will explicitly
20518 apply relocations when we read in the debug information now. */
20519
20520 /* A DW_AT_location attribute with no contents indicates that a
20521 variable has been optimized away. */
20522 if (attr->form_is_block () && attr->as_block ()->size == 0)
20523 {
20524 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20525 return;
20526 }
20527
20528 /* Handle one degenerate form of location expression specially, to
20529 preserve GDB's previous behavior when section offsets are
20530 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20531 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20532
20533 if (attr->form_is_block ())
20534 {
20535 struct dwarf_block *block = attr->as_block ();
20536
20537 if ((block->data[0] == DW_OP_addr
20538 && block->size == 1 + cu_header->addr_size)
20539 || ((block->data[0] == DW_OP_GNU_addr_index
20540 || block->data[0] == DW_OP_addrx)
20541 && (block->size
20542 == 1 + leb128_size (&block->data[1]))))
20543 {
20544 unsigned int dummy;
20545
20546 if (block->data[0] == DW_OP_addr)
20547 sym->set_value_address
20548 (cu->header.read_address (objfile->obfd, block->data + 1,
20549 &dummy));
20550 else
20551 sym->set_value_address
20552 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
20553 sym->set_aclass_index (LOC_STATIC);
20554 fixup_symbol_section (sym, objfile);
20555 sym->set_value_address
20556 (sym->value_address ()
20557 + objfile->section_offsets[sym->section_index ()]);
20558 return;
20559 }
20560 }
20561
20562 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20563 expression evaluator, and use LOC_COMPUTED only when necessary
20564 (i.e. when the value of a register or memory location is
20565 referenced, or a thread-local block, etc.). Then again, it might
20566 not be worthwhile. I'm assuming that it isn't unless performance
20567 or memory numbers show me otherwise. */
20568
20569 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20570
20571 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20572 cu->has_loclist = true;
20573 }
20574
20575 /* Given a pointer to a DWARF information entry, figure out if we need
20576 to make a symbol table entry for it, and if so, create a new entry
20577 and return a pointer to it.
20578 If TYPE is NULL, determine symbol type from the die, otherwise
20579 used the passed type.
20580 If SPACE is not NULL, use it to hold the new symbol. If it is
20581 NULL, allocate a new symbol on the objfile's obstack. */
20582
20583 static struct symbol *
20584 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20585 struct symbol *space)
20586 {
20587 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20588 struct objfile *objfile = per_objfile->objfile;
20589 struct gdbarch *gdbarch = objfile->arch ();
20590 struct symbol *sym = NULL;
20591 const char *name;
20592 struct attribute *attr = NULL;
20593 struct attribute *attr2 = NULL;
20594 CORE_ADDR baseaddr;
20595 struct pending **list_to_add = NULL;
20596
20597 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20598
20599 baseaddr = objfile->text_section_offset ();
20600
20601 name = dwarf2_name (die, cu);
20602 if (name)
20603 {
20604 int suppress_add = 0;
20605
20606 if (space)
20607 sym = space;
20608 else
20609 sym = new (&objfile->objfile_obstack) symbol;
20610 OBJSTAT (objfile, n_syms++);
20611
20612 /* Cache this symbol's name and the name's demangled form (if any). */
20613 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
20614 /* Fortran does not have mangling standard and the mangling does differ
20615 between gfortran, iFort etc. */
20616 const char *physname
20617 = (cu->per_cu->lang == language_fortran
20618 ? dwarf2_full_name (name, die, cu)
20619 : dwarf2_physname (name, die, cu));
20620 const char *linkagename = dw2_linkage_name (die, cu);
20621
20622 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
20623 sym->set_linkage_name (physname);
20624 else
20625 {
20626 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20627 sym->set_linkage_name (linkagename);
20628 }
20629
20630 /* Handle DW_AT_artificial. */
20631 attr = dwarf2_attr (die, DW_AT_artificial, cu);
20632 if (attr != nullptr)
20633 sym->set_is_artificial (attr->as_boolean ());
20634
20635 /* Default assumptions.
20636 Use the passed type or decode it from the die. */
20637 sym->set_domain (VAR_DOMAIN);
20638 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20639 if (type != NULL)
20640 sym->set_type (type);
20641 else
20642 sym->set_type (die_type (die, cu));
20643 attr = dwarf2_attr (die,
20644 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20645 cu);
20646 if (attr != nullptr)
20647 sym->set_line (attr->constant_value (0));
20648
20649 attr = dwarf2_attr (die,
20650 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20651 cu);
20652 if (attr != nullptr && attr->is_nonnegative ())
20653 {
20654 file_name_index file_index
20655 = (file_name_index) attr->as_nonnegative ();
20656 struct file_entry *fe;
20657
20658 if (cu->line_header != NULL)
20659 fe = cu->line_header->file_name_at (file_index);
20660 else
20661 fe = NULL;
20662
20663 if (fe == NULL)
20664 complaint (_("file index out of range"));
20665 else
20666 sym->set_symtab (fe->symtab);
20667 }
20668
20669 switch (die->tag)
20670 {
20671 case DW_TAG_label:
20672 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20673 if (attr != nullptr)
20674 {
20675 CORE_ADDR addr;
20676
20677 addr = attr->as_address ();
20678 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20679 sym->set_value_address (addr);
20680 sym->set_aclass_index (LOC_LABEL);
20681 }
20682 else
20683 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20684 sym->set_type (objfile_type (objfile)->builtin_core_addr);
20685 sym->set_domain (LABEL_DOMAIN);
20686 add_symbol_to_list (sym, cu->list_in_scope);
20687 break;
20688 case DW_TAG_subprogram:
20689 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20690 finish_block. */
20691 sym->set_aclass_index (LOC_BLOCK);
20692 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20693 if ((attr2 != nullptr && attr2->as_boolean ())
20694 || cu->per_cu->lang == language_ada
20695 || cu->per_cu->lang == language_fortran)
20696 {
20697 /* Subprograms marked external are stored as a global symbol.
20698 Ada and Fortran subprograms, whether marked external or
20699 not, are always stored as a global symbol, because we want
20700 to be able to access them globally. For instance, we want
20701 to be able to break on a nested subprogram without having
20702 to specify the context. */
20703 list_to_add = cu->get_builder ()->get_global_symbols ();
20704 }
20705 else
20706 {
20707 list_to_add = cu->list_in_scope;
20708 }
20709 break;
20710 case DW_TAG_inlined_subroutine:
20711 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20712 finish_block. */
20713 sym->set_aclass_index (LOC_BLOCK);
20714 sym->set_is_inlined (1);
20715 list_to_add = cu->list_in_scope;
20716 break;
20717 case DW_TAG_template_value_param:
20718 suppress_add = 1;
20719 /* Fall through. */
20720 case DW_TAG_constant:
20721 case DW_TAG_variable:
20722 case DW_TAG_member:
20723 /* Compilation with minimal debug info may result in
20724 variables with missing type entries. Change the
20725 misleading `void' type to something sensible. */
20726 if (sym->type ()->code () == TYPE_CODE_VOID)
20727 sym->set_type (objfile_type (objfile)->builtin_int);
20728
20729 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20730 /* In the case of DW_TAG_member, we should only be called for
20731 static const members. */
20732 if (die->tag == DW_TAG_member)
20733 {
20734 /* dwarf2_add_field uses die_is_declaration,
20735 so we do the same. */
20736 gdb_assert (die_is_declaration (die, cu));
20737 gdb_assert (attr);
20738 }
20739 if (attr != nullptr)
20740 {
20741 dwarf2_const_value (attr, sym, cu);
20742 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20743 if (!suppress_add)
20744 {
20745 if (attr2 != nullptr && attr2->as_boolean ())
20746 list_to_add = cu->get_builder ()->get_global_symbols ();
20747 else
20748 list_to_add = cu->list_in_scope;
20749 }
20750 break;
20751 }
20752 attr = dwarf2_attr (die, DW_AT_location, cu);
20753 if (attr != nullptr)
20754 {
20755 var_decode_location (attr, sym, cu);
20756 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20757
20758 /* Fortran explicitly imports any global symbols to the local
20759 scope by DW_TAG_common_block. */
20760 if (cu->per_cu->lang == language_fortran && die->parent
20761 && die->parent->tag == DW_TAG_common_block)
20762 attr2 = NULL;
20763
20764 if (sym->aclass () == LOC_STATIC
20765 && sym->value_address () == 0
20766 && !per_objfile->per_bfd->has_section_at_zero)
20767 {
20768 /* When a static variable is eliminated by the linker,
20769 the corresponding debug information is not stripped
20770 out, but the variable address is set to null;
20771 do not add such variables into symbol table. */
20772 }
20773 else if (attr2 != nullptr && attr2->as_boolean ())
20774 {
20775 if (sym->aclass () == LOC_STATIC
20776 && (objfile->flags & OBJF_MAINLINE) == 0
20777 && per_objfile->per_bfd->can_copy)
20778 {
20779 /* A global static variable might be subject to
20780 copy relocation. We first check for a local
20781 minsym, though, because maybe the symbol was
20782 marked hidden, in which case this would not
20783 apply. */
20784 bound_minimal_symbol found
20785 = (lookup_minimal_symbol_linkage
20786 (sym->linkage_name (), objfile));
20787 if (found.minsym != nullptr)
20788 sym->maybe_copied = 1;
20789 }
20790
20791 /* A variable with DW_AT_external is never static,
20792 but it may be block-scoped. */
20793 list_to_add
20794 = ((cu->list_in_scope
20795 == cu->get_builder ()->get_file_symbols ())
20796 ? cu->get_builder ()->get_global_symbols ()
20797 : cu->list_in_scope);
20798 }
20799 else
20800 list_to_add = cu->list_in_scope;
20801 }
20802 else
20803 {
20804 /* We do not know the address of this symbol.
20805 If it is an external symbol and we have type information
20806 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20807 The address of the variable will then be determined from
20808 the minimal symbol table whenever the variable is
20809 referenced. */
20810 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20811
20812 /* Fortran explicitly imports any global symbols to the local
20813 scope by DW_TAG_common_block. */
20814 if (cu->per_cu->lang == language_fortran && die->parent
20815 && die->parent->tag == DW_TAG_common_block)
20816 {
20817 /* SYMBOL_CLASS doesn't matter here because
20818 read_common_block is going to reset it. */
20819 if (!suppress_add)
20820 list_to_add = cu->list_in_scope;
20821 }
20822 else if (attr2 != nullptr && attr2->as_boolean ()
20823 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20824 {
20825 /* A variable with DW_AT_external is never static, but it
20826 may be block-scoped. */
20827 list_to_add
20828 = ((cu->list_in_scope
20829 == cu->get_builder ()->get_file_symbols ())
20830 ? cu->get_builder ()->get_global_symbols ()
20831 : cu->list_in_scope);
20832
20833 sym->set_aclass_index (LOC_UNRESOLVED);
20834 }
20835 else if (!die_is_declaration (die, cu))
20836 {
20837 /* Use the default LOC_OPTIMIZED_OUT class. */
20838 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
20839 if (!suppress_add)
20840 list_to_add = cu->list_in_scope;
20841 }
20842 }
20843 break;
20844 case DW_TAG_formal_parameter:
20845 {
20846 /* If we are inside a function, mark this as an argument. If
20847 not, we might be looking at an argument to an inlined function
20848 when we do not have enough information to show inlined frames;
20849 pretend it's a local variable in that case so that the user can
20850 still see it. */
20851 struct context_stack *curr
20852 = cu->get_builder ()->get_current_context_stack ();
20853 if (curr != nullptr && curr->name != nullptr)
20854 sym->set_is_argument (1);
20855 attr = dwarf2_attr (die, DW_AT_location, cu);
20856 if (attr != nullptr)
20857 {
20858 var_decode_location (attr, sym, cu);
20859 }
20860 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20861 if (attr != nullptr)
20862 {
20863 dwarf2_const_value (attr, sym, cu);
20864 }
20865
20866 list_to_add = cu->list_in_scope;
20867 }
20868 break;
20869 case DW_TAG_unspecified_parameters:
20870 /* From varargs functions; gdb doesn't seem to have any
20871 interest in this information, so just ignore it for now.
20872 (FIXME?) */
20873 break;
20874 case DW_TAG_template_type_param:
20875 suppress_add = 1;
20876 /* Fall through. */
20877 case DW_TAG_class_type:
20878 case DW_TAG_interface_type:
20879 case DW_TAG_structure_type:
20880 case DW_TAG_union_type:
20881 case DW_TAG_set_type:
20882 case DW_TAG_enumeration_type:
20883 case DW_TAG_namelist:
20884 if (die->tag == DW_TAG_namelist)
20885 {
20886 sym->set_aclass_index (LOC_STATIC);
20887 sym->set_domain (VAR_DOMAIN);
20888 }
20889 else
20890 {
20891 sym->set_aclass_index (LOC_TYPEDEF);
20892 sym->set_domain (STRUCT_DOMAIN);
20893 }
20894 {
20895 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20896 really ever be static objects: otherwise, if you try
20897 to, say, break of a class's method and you're in a file
20898 which doesn't mention that class, it won't work unless
20899 the check for all static symbols in lookup_symbol_aux
20900 saves you. See the OtherFileClass tests in
20901 gdb.c++/namespace.exp. */
20902
20903 if (!suppress_add)
20904 {
20905 buildsym_compunit *builder = cu->get_builder ();
20906 list_to_add
20907 = (cu->list_in_scope == builder->get_file_symbols ()
20908 && cu->per_cu->lang == language_cplus
20909 ? builder->get_global_symbols ()
20910 : cu->list_in_scope);
20911
20912 /* The semantics of C++ state that "struct foo {
20913 ... }" also defines a typedef for "foo". */
20914 if (cu->per_cu->lang == language_cplus
20915 || cu->per_cu->lang == language_ada
20916 || cu->per_cu->lang == language_d
20917 || cu->per_cu->lang == language_rust)
20918 {
20919 /* The symbol's name is already allocated along
20920 with this objfile, so we don't need to
20921 duplicate it for the type. */
20922 if (sym->type ()->name () == 0)
20923 sym->type ()->set_name (sym->search_name ());
20924 }
20925 }
20926 }
20927 break;
20928 case DW_TAG_typedef:
20929 sym->set_aclass_index (LOC_TYPEDEF);
20930 sym->set_domain (VAR_DOMAIN);
20931 list_to_add = cu->list_in_scope;
20932 break;
20933 case DW_TAG_array_type:
20934 case DW_TAG_base_type:
20935 case DW_TAG_subrange_type:
20936 case DW_TAG_generic_subrange:
20937 sym->set_aclass_index (LOC_TYPEDEF);
20938 sym->set_domain (VAR_DOMAIN);
20939 list_to_add = cu->list_in_scope;
20940 break;
20941 case DW_TAG_enumerator:
20942 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20943 if (attr != nullptr)
20944 {
20945 dwarf2_const_value (attr, sym, cu);
20946 }
20947 {
20948 /* NOTE: carlton/2003-11-10: See comment above in the
20949 DW_TAG_class_type, etc. block. */
20950
20951 list_to_add
20952 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20953 && cu->per_cu->lang == language_cplus
20954 ? cu->get_builder ()->get_global_symbols ()
20955 : cu->list_in_scope);
20956 }
20957 break;
20958 case DW_TAG_imported_declaration:
20959 case DW_TAG_namespace:
20960 sym->set_aclass_index (LOC_TYPEDEF);
20961 list_to_add = cu->get_builder ()->get_global_symbols ();
20962 break;
20963 case DW_TAG_module:
20964 sym->set_aclass_index (LOC_TYPEDEF);
20965 sym->set_domain (MODULE_DOMAIN);
20966 list_to_add = cu->get_builder ()->get_global_symbols ();
20967 break;
20968 case DW_TAG_common_block:
20969 sym->set_aclass_index (LOC_COMMON_BLOCK);
20970 sym->set_domain (COMMON_BLOCK_DOMAIN);
20971 add_symbol_to_list (sym, cu->list_in_scope);
20972 break;
20973 default:
20974 /* Not a tag we recognize. Hopefully we aren't processing
20975 trash data, but since we must specifically ignore things
20976 we don't recognize, there is nothing else we should do at
20977 this point. */
20978 complaint (_("unsupported tag: '%s'"),
20979 dwarf_tag_name (die->tag));
20980 break;
20981 }
20982
20983 if (suppress_add)
20984 {
20985 sym->hash_next = objfile->template_symbols;
20986 objfile->template_symbols = sym;
20987 list_to_add = NULL;
20988 }
20989
20990 if (list_to_add != NULL)
20991 add_symbol_to_list (sym, list_to_add);
20992
20993 /* For the benefit of old versions of GCC, check for anonymous
20994 namespaces based on the demangled name. */
20995 if (!cu->processing_has_namespace_info
20996 && cu->per_cu->lang == language_cplus)
20997 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20998 }
20999 return (sym);
21000 }
21001
21002 /* Given an attr with a DW_FORM_dataN value in host byte order,
21003 zero-extend it as appropriate for the symbol's type. The DWARF
21004 standard (v4) is not entirely clear about the meaning of using
21005 DW_FORM_dataN for a constant with a signed type, where the type is
21006 wider than the data. The conclusion of a discussion on the DWARF
21007 list was that this is unspecified. We choose to always zero-extend
21008 because that is the interpretation long in use by GCC. */
21009
21010 static gdb_byte *
21011 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21012 struct dwarf2_cu *cu, LONGEST *value, int bits)
21013 {
21014 struct objfile *objfile = cu->per_objfile->objfile;
21015 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21016 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21017 LONGEST l = attr->constant_value (0);
21018
21019 if (bits < sizeof (*value) * 8)
21020 {
21021 l &= ((LONGEST) 1 << bits) - 1;
21022 *value = l;
21023 }
21024 else if (bits == sizeof (*value) * 8)
21025 *value = l;
21026 else
21027 {
21028 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21029 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21030 return bytes;
21031 }
21032
21033 return NULL;
21034 }
21035
21036 /* Read a constant value from an attribute. Either set *VALUE, or if
21037 the value does not fit in *VALUE, set *BYTES - either already
21038 allocated on the objfile obstack, or newly allocated on OBSTACK,
21039 or, set *BATON, if we translated the constant to a location
21040 expression. */
21041
21042 static void
21043 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21044 const char *name, struct obstack *obstack,
21045 struct dwarf2_cu *cu,
21046 LONGEST *value, const gdb_byte **bytes,
21047 struct dwarf2_locexpr_baton **baton)
21048 {
21049 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21050 struct objfile *objfile = per_objfile->objfile;
21051 struct comp_unit_head *cu_header = &cu->header;
21052 struct dwarf_block *blk;
21053 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21054 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21055
21056 *value = 0;
21057 *bytes = NULL;
21058 *baton = NULL;
21059
21060 switch (attr->form)
21061 {
21062 case DW_FORM_addr:
21063 case DW_FORM_addrx:
21064 case DW_FORM_GNU_addr_index:
21065 {
21066 gdb_byte *data;
21067
21068 if (TYPE_LENGTH (type) != cu_header->addr_size)
21069 dwarf2_const_value_length_mismatch_complaint (name,
21070 cu_header->addr_size,
21071 TYPE_LENGTH (type));
21072 /* Symbols of this form are reasonably rare, so we just
21073 piggyback on the existing location code rather than writing
21074 a new implementation of symbol_computed_ops. */
21075 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21076 (*baton)->per_objfile = per_objfile;
21077 (*baton)->per_cu = cu->per_cu;
21078 gdb_assert ((*baton)->per_cu);
21079
21080 (*baton)->size = 2 + cu_header->addr_size;
21081 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21082 (*baton)->data = data;
21083
21084 data[0] = DW_OP_addr;
21085 store_unsigned_integer (&data[1], cu_header->addr_size,
21086 byte_order, attr->as_address ());
21087 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21088 }
21089 break;
21090 case DW_FORM_string:
21091 case DW_FORM_strp:
21092 case DW_FORM_strx:
21093 case DW_FORM_GNU_str_index:
21094 case DW_FORM_GNU_strp_alt:
21095 /* The string is already allocated on the objfile obstack, point
21096 directly to it. */
21097 *bytes = (const gdb_byte *) attr->as_string ();
21098 break;
21099 case DW_FORM_block1:
21100 case DW_FORM_block2:
21101 case DW_FORM_block4:
21102 case DW_FORM_block:
21103 case DW_FORM_exprloc:
21104 case DW_FORM_data16:
21105 blk = attr->as_block ();
21106 if (TYPE_LENGTH (type) != blk->size)
21107 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21108 TYPE_LENGTH (type));
21109 *bytes = blk->data;
21110 break;
21111
21112 /* The DW_AT_const_value attributes are supposed to carry the
21113 symbol's value "represented as it would be on the target
21114 architecture." By the time we get here, it's already been
21115 converted to host endianness, so we just need to sign- or
21116 zero-extend it as appropriate. */
21117 case DW_FORM_data1:
21118 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21119 break;
21120 case DW_FORM_data2:
21121 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21122 break;
21123 case DW_FORM_data4:
21124 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21125 break;
21126 case DW_FORM_data8:
21127 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21128 break;
21129
21130 case DW_FORM_sdata:
21131 case DW_FORM_implicit_const:
21132 *value = attr->as_signed ();
21133 break;
21134
21135 case DW_FORM_udata:
21136 *value = attr->as_unsigned ();
21137 break;
21138
21139 default:
21140 complaint (_("unsupported const value attribute form: '%s'"),
21141 dwarf_form_name (attr->form));
21142 *value = 0;
21143 break;
21144 }
21145 }
21146
21147
21148 /* Copy constant value from an attribute to a symbol. */
21149
21150 static void
21151 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21152 struct dwarf2_cu *cu)
21153 {
21154 struct objfile *objfile = cu->per_objfile->objfile;
21155 LONGEST value;
21156 const gdb_byte *bytes;
21157 struct dwarf2_locexpr_baton *baton;
21158
21159 dwarf2_const_value_attr (attr, sym->type (),
21160 sym->print_name (),
21161 &objfile->objfile_obstack, cu,
21162 &value, &bytes, &baton);
21163
21164 if (baton != NULL)
21165 {
21166 SYMBOL_LOCATION_BATON (sym) = baton;
21167 sym->set_aclass_index (dwarf2_locexpr_index);
21168 }
21169 else if (bytes != NULL)
21170 {
21171 sym->set_value_bytes (bytes);
21172 sym->set_aclass_index (LOC_CONST_BYTES);
21173 }
21174 else
21175 {
21176 sym->set_value_longest (value);
21177 sym->set_aclass_index (LOC_CONST);
21178 }
21179 }
21180
21181 /* Return the type of the die in question using its DW_AT_type attribute. */
21182
21183 static struct type *
21184 die_type (struct die_info *die, struct dwarf2_cu *cu)
21185 {
21186 struct attribute *type_attr;
21187
21188 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21189 if (!type_attr)
21190 {
21191 struct objfile *objfile = cu->per_objfile->objfile;
21192 /* A missing DW_AT_type represents a void type. */
21193 return objfile_type (objfile)->builtin_void;
21194 }
21195
21196 return lookup_die_type (die, type_attr, cu);
21197 }
21198
21199 /* True iff CU's producer generates GNAT Ada auxiliary information
21200 that allows to find parallel types through that information instead
21201 of having to do expensive parallel lookups by type name. */
21202
21203 static int
21204 need_gnat_info (struct dwarf2_cu *cu)
21205 {
21206 /* Assume that the Ada compiler was GNAT, which always produces
21207 the auxiliary information. */
21208 return (cu->per_cu->lang == language_ada);
21209 }
21210
21211 /* Return the auxiliary type of the die in question using its
21212 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21213 attribute is not present. */
21214
21215 static struct type *
21216 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21217 {
21218 struct attribute *type_attr;
21219
21220 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21221 if (!type_attr)
21222 return NULL;
21223
21224 return lookup_die_type (die, type_attr, cu);
21225 }
21226
21227 /* If DIE has a descriptive_type attribute, then set the TYPE's
21228 descriptive type accordingly. */
21229
21230 static void
21231 set_descriptive_type (struct type *type, struct die_info *die,
21232 struct dwarf2_cu *cu)
21233 {
21234 struct type *descriptive_type = die_descriptive_type (die, cu);
21235
21236 if (descriptive_type)
21237 {
21238 ALLOCATE_GNAT_AUX_TYPE (type);
21239 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21240 }
21241 }
21242
21243 /* Return the containing type of the die in question using its
21244 DW_AT_containing_type attribute. */
21245
21246 static struct type *
21247 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21248 {
21249 struct attribute *type_attr;
21250 struct objfile *objfile = cu->per_objfile->objfile;
21251
21252 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21253 if (!type_attr)
21254 error (_("Dwarf Error: Problem turning containing type into gdb type "
21255 "[in module %s]"), objfile_name (objfile));
21256
21257 return lookup_die_type (die, type_attr, cu);
21258 }
21259
21260 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21261
21262 static struct type *
21263 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21264 {
21265 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21266 struct objfile *objfile = per_objfile->objfile;
21267 char *saved;
21268
21269 std::string message
21270 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21271 objfile_name (objfile),
21272 sect_offset_str (cu->header.sect_off),
21273 sect_offset_str (die->sect_off));
21274 saved = obstack_strdup (&objfile->objfile_obstack, message);
21275
21276 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21277 }
21278
21279 /* Look up the type of DIE in CU using its type attribute ATTR.
21280 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21281 DW_AT_containing_type.
21282 If there is no type substitute an error marker. */
21283
21284 static struct type *
21285 lookup_die_type (struct die_info *die, const struct attribute *attr,
21286 struct dwarf2_cu *cu)
21287 {
21288 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21289 struct objfile *objfile = per_objfile->objfile;
21290 struct type *this_type;
21291
21292 gdb_assert (attr->name == DW_AT_type
21293 || attr->name == DW_AT_GNAT_descriptive_type
21294 || attr->name == DW_AT_containing_type);
21295
21296 /* First see if we have it cached. */
21297
21298 if (attr->form == DW_FORM_GNU_ref_alt)
21299 {
21300 struct dwarf2_per_cu_data *per_cu;
21301 sect_offset sect_off = attr->get_ref_die_offset ();
21302
21303 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21304 per_objfile->per_bfd);
21305 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21306 }
21307 else if (attr->form_is_ref ())
21308 {
21309 sect_offset sect_off = attr->get_ref_die_offset ();
21310
21311 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21312 }
21313 else if (attr->form == DW_FORM_ref_sig8)
21314 {
21315 ULONGEST signature = attr->as_signature ();
21316
21317 return get_signatured_type (die, signature, cu);
21318 }
21319 else
21320 {
21321 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21322 " at %s [in module %s]"),
21323 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21324 objfile_name (objfile));
21325 return build_error_marker_type (cu, die);
21326 }
21327
21328 /* If not cached we need to read it in. */
21329
21330 if (this_type == NULL)
21331 {
21332 struct die_info *type_die = NULL;
21333 struct dwarf2_cu *type_cu = cu;
21334
21335 if (attr->form_is_ref ())
21336 type_die = follow_die_ref (die, attr, &type_cu);
21337 if (type_die == NULL)
21338 return build_error_marker_type (cu, die);
21339 /* If we find the type now, it's probably because the type came
21340 from an inter-CU reference and the type's CU got expanded before
21341 ours. */
21342 this_type = read_type_die (type_die, type_cu);
21343 }
21344
21345 /* If we still don't have a type use an error marker. */
21346
21347 if (this_type == NULL)
21348 return build_error_marker_type (cu, die);
21349
21350 return this_type;
21351 }
21352
21353 /* Return the type in DIE, CU.
21354 Returns NULL for invalid types.
21355
21356 This first does a lookup in die_type_hash,
21357 and only reads the die in if necessary.
21358
21359 NOTE: This can be called when reading in partial or full symbols. */
21360
21361 static struct type *
21362 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21363 {
21364 struct type *this_type;
21365
21366 this_type = get_die_type (die, cu);
21367 if (this_type)
21368 return this_type;
21369
21370 return read_type_die_1 (die, cu);
21371 }
21372
21373 /* Read the type in DIE, CU.
21374 Returns NULL for invalid types. */
21375
21376 static struct type *
21377 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21378 {
21379 struct type *this_type = NULL;
21380
21381 switch (die->tag)
21382 {
21383 case DW_TAG_class_type:
21384 case DW_TAG_interface_type:
21385 case DW_TAG_structure_type:
21386 case DW_TAG_union_type:
21387 this_type = read_structure_type (die, cu);
21388 break;
21389 case DW_TAG_enumeration_type:
21390 this_type = read_enumeration_type (die, cu);
21391 break;
21392 case DW_TAG_subprogram:
21393 case DW_TAG_subroutine_type:
21394 case DW_TAG_inlined_subroutine:
21395 this_type = read_subroutine_type (die, cu);
21396 break;
21397 case DW_TAG_array_type:
21398 this_type = read_array_type (die, cu);
21399 break;
21400 case DW_TAG_set_type:
21401 this_type = read_set_type (die, cu);
21402 break;
21403 case DW_TAG_pointer_type:
21404 this_type = read_tag_pointer_type (die, cu);
21405 break;
21406 case DW_TAG_ptr_to_member_type:
21407 this_type = read_tag_ptr_to_member_type (die, cu);
21408 break;
21409 case DW_TAG_reference_type:
21410 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21411 break;
21412 case DW_TAG_rvalue_reference_type:
21413 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21414 break;
21415 case DW_TAG_const_type:
21416 this_type = read_tag_const_type (die, cu);
21417 break;
21418 case DW_TAG_volatile_type:
21419 this_type = read_tag_volatile_type (die, cu);
21420 break;
21421 case DW_TAG_restrict_type:
21422 this_type = read_tag_restrict_type (die, cu);
21423 break;
21424 case DW_TAG_string_type:
21425 this_type = read_tag_string_type (die, cu);
21426 break;
21427 case DW_TAG_typedef:
21428 this_type = read_typedef (die, cu);
21429 break;
21430 case DW_TAG_generic_subrange:
21431 case DW_TAG_subrange_type:
21432 this_type = read_subrange_type (die, cu);
21433 break;
21434 case DW_TAG_base_type:
21435 this_type = read_base_type (die, cu);
21436 break;
21437 case DW_TAG_unspecified_type:
21438 this_type = read_unspecified_type (die, cu);
21439 break;
21440 case DW_TAG_namespace:
21441 this_type = read_namespace_type (die, cu);
21442 break;
21443 case DW_TAG_module:
21444 this_type = read_module_type (die, cu);
21445 break;
21446 case DW_TAG_atomic_type:
21447 this_type = read_tag_atomic_type (die, cu);
21448 break;
21449 default:
21450 complaint (_("unexpected tag in read_type_die: '%s'"),
21451 dwarf_tag_name (die->tag));
21452 break;
21453 }
21454
21455 return this_type;
21456 }
21457
21458 /* See if we can figure out if the class lives in a namespace. We do
21459 this by looking for a member function; its demangled name will
21460 contain namespace info, if there is any.
21461 Return the computed name or NULL.
21462 Space for the result is allocated on the objfile's obstack.
21463 This is the full-die version of guess_partial_die_structure_name.
21464 In this case we know DIE has no useful parent. */
21465
21466 static const char *
21467 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21468 {
21469 struct die_info *spec_die;
21470 struct dwarf2_cu *spec_cu;
21471 struct die_info *child;
21472 struct objfile *objfile = cu->per_objfile->objfile;
21473
21474 spec_cu = cu;
21475 spec_die = die_specification (die, &spec_cu);
21476 if (spec_die != NULL)
21477 {
21478 die = spec_die;
21479 cu = spec_cu;
21480 }
21481
21482 for (child = die->child;
21483 child != NULL;
21484 child = child->sibling)
21485 {
21486 if (child->tag == DW_TAG_subprogram)
21487 {
21488 const char *linkage_name = dw2_linkage_name (child, cu);
21489
21490 if (linkage_name != NULL)
21491 {
21492 gdb::unique_xmalloc_ptr<char> actual_name
21493 (cu->language_defn->class_name_from_physname (linkage_name));
21494 const char *name = NULL;
21495
21496 if (actual_name != NULL)
21497 {
21498 const char *die_name = dwarf2_name (die, cu);
21499
21500 if (die_name != NULL
21501 && strcmp (die_name, actual_name.get ()) != 0)
21502 {
21503 /* Strip off the class name from the full name.
21504 We want the prefix. */
21505 int die_name_len = strlen (die_name);
21506 int actual_name_len = strlen (actual_name.get ());
21507 const char *ptr = actual_name.get ();
21508
21509 /* Test for '::' as a sanity check. */
21510 if (actual_name_len > die_name_len + 2
21511 && ptr[actual_name_len - die_name_len - 1] == ':')
21512 name = obstack_strndup (
21513 &objfile->per_bfd->storage_obstack,
21514 ptr, actual_name_len - die_name_len - 2);
21515 }
21516 }
21517 return name;
21518 }
21519 }
21520 }
21521
21522 return NULL;
21523 }
21524
21525 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21526 prefix part in such case. See
21527 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21528
21529 static const char *
21530 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21531 {
21532 struct attribute *attr;
21533 const char *base;
21534
21535 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21536 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21537 return NULL;
21538
21539 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21540 return NULL;
21541
21542 attr = dw2_linkage_name_attr (die, cu);
21543 const char *attr_name = attr->as_string ();
21544 if (attr == NULL || attr_name == NULL)
21545 return NULL;
21546
21547 /* dwarf2_name had to be already called. */
21548 gdb_assert (attr->canonical_string_p ());
21549
21550 /* Strip the base name, keep any leading namespaces/classes. */
21551 base = strrchr (attr_name, ':');
21552 if (base == NULL || base == attr_name || base[-1] != ':')
21553 return "";
21554
21555 struct objfile *objfile = cu->per_objfile->objfile;
21556 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21557 attr_name,
21558 &base[-1] - attr_name);
21559 }
21560
21561 /* Return the name of the namespace/class that DIE is defined within,
21562 or "" if we can't tell. The caller should not xfree the result.
21563
21564 For example, if we're within the method foo() in the following
21565 code:
21566
21567 namespace N {
21568 class C {
21569 void foo () {
21570 }
21571 };
21572 }
21573
21574 then determine_prefix on foo's die will return "N::C". */
21575
21576 static const char *
21577 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21578 {
21579 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21580 struct die_info *parent, *spec_die;
21581 struct dwarf2_cu *spec_cu;
21582 struct type *parent_type;
21583 const char *retval;
21584
21585 if (cu->per_cu->lang != language_cplus
21586 && cu->per_cu->lang != language_fortran
21587 && cu->per_cu->lang != language_d
21588 && cu->per_cu->lang != language_rust)
21589 return "";
21590
21591 retval = anonymous_struct_prefix (die, cu);
21592 if (retval)
21593 return retval;
21594
21595 /* We have to be careful in the presence of DW_AT_specification.
21596 For example, with GCC 3.4, given the code
21597
21598 namespace N {
21599 void foo() {
21600 // Definition of N::foo.
21601 }
21602 }
21603
21604 then we'll have a tree of DIEs like this:
21605
21606 1: DW_TAG_compile_unit
21607 2: DW_TAG_namespace // N
21608 3: DW_TAG_subprogram // declaration of N::foo
21609 4: DW_TAG_subprogram // definition of N::foo
21610 DW_AT_specification // refers to die #3
21611
21612 Thus, when processing die #4, we have to pretend that we're in
21613 the context of its DW_AT_specification, namely the contex of die
21614 #3. */
21615 spec_cu = cu;
21616 spec_die = die_specification (die, &spec_cu);
21617 if (spec_die == NULL)
21618 parent = die->parent;
21619 else
21620 {
21621 parent = spec_die->parent;
21622 cu = spec_cu;
21623 }
21624
21625 if (parent == NULL)
21626 return "";
21627 else if (parent->building_fullname)
21628 {
21629 const char *name;
21630 const char *parent_name;
21631
21632 /* It has been seen on RealView 2.2 built binaries,
21633 DW_TAG_template_type_param types actually _defined_ as
21634 children of the parent class:
21635
21636 enum E {};
21637 template class <class Enum> Class{};
21638 Class<enum E> class_e;
21639
21640 1: DW_TAG_class_type (Class)
21641 2: DW_TAG_enumeration_type (E)
21642 3: DW_TAG_enumerator (enum1:0)
21643 3: DW_TAG_enumerator (enum2:1)
21644 ...
21645 2: DW_TAG_template_type_param
21646 DW_AT_type DW_FORM_ref_udata (E)
21647
21648 Besides being broken debug info, it can put GDB into an
21649 infinite loop. Consider:
21650
21651 When we're building the full name for Class<E>, we'll start
21652 at Class, and go look over its template type parameters,
21653 finding E. We'll then try to build the full name of E, and
21654 reach here. We're now trying to build the full name of E,
21655 and look over the parent DIE for containing scope. In the
21656 broken case, if we followed the parent DIE of E, we'd again
21657 find Class, and once again go look at its template type
21658 arguments, etc., etc. Simply don't consider such parent die
21659 as source-level parent of this die (it can't be, the language
21660 doesn't allow it), and break the loop here. */
21661 name = dwarf2_name (die, cu);
21662 parent_name = dwarf2_name (parent, cu);
21663 complaint (_("template param type '%s' defined within parent '%s'"),
21664 name ? name : "<unknown>",
21665 parent_name ? parent_name : "<unknown>");
21666 return "";
21667 }
21668 else
21669 switch (parent->tag)
21670 {
21671 case DW_TAG_namespace:
21672 parent_type = read_type_die (parent, cu);
21673 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21674 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21675 Work around this problem here. */
21676 if (cu->per_cu->lang == language_cplus
21677 && strcmp (parent_type->name (), "::") == 0)
21678 return "";
21679 /* We give a name to even anonymous namespaces. */
21680 return parent_type->name ();
21681 case DW_TAG_class_type:
21682 case DW_TAG_interface_type:
21683 case DW_TAG_structure_type:
21684 case DW_TAG_union_type:
21685 case DW_TAG_module:
21686 parent_type = read_type_die (parent, cu);
21687 if (parent_type->name () != NULL)
21688 return parent_type->name ();
21689 else
21690 /* An anonymous structure is only allowed non-static data
21691 members; no typedefs, no member functions, et cetera.
21692 So it does not need a prefix. */
21693 return "";
21694 case DW_TAG_compile_unit:
21695 case DW_TAG_partial_unit:
21696 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21697 if (cu->per_cu->lang == language_cplus
21698 && !per_objfile->per_bfd->types.empty ()
21699 && die->child != NULL
21700 && (die->tag == DW_TAG_class_type
21701 || die->tag == DW_TAG_structure_type
21702 || die->tag == DW_TAG_union_type))
21703 {
21704 const char *name = guess_full_die_structure_name (die, cu);
21705 if (name != NULL)
21706 return name;
21707 }
21708 return "";
21709 case DW_TAG_subprogram:
21710 /* Nested subroutines in Fortran get a prefix with the name
21711 of the parent's subroutine. */
21712 if (cu->per_cu->lang == language_fortran)
21713 {
21714 if ((die->tag == DW_TAG_subprogram)
21715 && (dwarf2_name (parent, cu) != NULL))
21716 return dwarf2_name (parent, cu);
21717 }
21718 return "";
21719 case DW_TAG_enumeration_type:
21720 parent_type = read_type_die (parent, cu);
21721 if (parent_type->is_declared_class ())
21722 {
21723 if (parent_type->name () != NULL)
21724 return parent_type->name ();
21725 return "";
21726 }
21727 /* Fall through. */
21728 default:
21729 return determine_prefix (parent, cu);
21730 }
21731 }
21732
21733 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21734 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21735 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21736 an obconcat, otherwise allocate storage for the result. The CU argument is
21737 used to determine the language and hence, the appropriate separator. */
21738
21739 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21740
21741 static char *
21742 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21743 int physname, struct dwarf2_cu *cu)
21744 {
21745 const char *lead = "";
21746 const char *sep;
21747
21748 if (suffix == NULL || suffix[0] == '\0'
21749 || prefix == NULL || prefix[0] == '\0')
21750 sep = "";
21751 else if (cu->per_cu->lang == language_d)
21752 {
21753 /* For D, the 'main' function could be defined in any module, but it
21754 should never be prefixed. */
21755 if (strcmp (suffix, "D main") == 0)
21756 {
21757 prefix = "";
21758 sep = "";
21759 }
21760 else
21761 sep = ".";
21762 }
21763 else if (cu->per_cu->lang == language_fortran && physname)
21764 {
21765 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21766 DW_AT_MIPS_linkage_name is preferred and used instead. */
21767
21768 lead = "__";
21769 sep = "_MOD_";
21770 }
21771 else
21772 sep = "::";
21773
21774 if (prefix == NULL)
21775 prefix = "";
21776 if (suffix == NULL)
21777 suffix = "";
21778
21779 if (obs == NULL)
21780 {
21781 char *retval
21782 = ((char *)
21783 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21784
21785 strcpy (retval, lead);
21786 strcat (retval, prefix);
21787 strcat (retval, sep);
21788 strcat (retval, suffix);
21789 return retval;
21790 }
21791 else
21792 {
21793 /* We have an obstack. */
21794 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21795 }
21796 }
21797
21798 /* Get name of a die, return NULL if not found. */
21799
21800 static const char *
21801 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21802 struct objfile *objfile)
21803 {
21804 if (name && cu->per_cu->lang == language_cplus)
21805 {
21806 gdb::unique_xmalloc_ptr<char> canon_name
21807 = cp_canonicalize_string (name);
21808
21809 if (canon_name != nullptr)
21810 name = objfile->intern (canon_name.get ());
21811 }
21812
21813 return name;
21814 }
21815
21816 /* Get name of a die, return NULL if not found.
21817 Anonymous namespaces are converted to their magic string. */
21818
21819 static const char *
21820 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21821 {
21822 struct attribute *attr;
21823 struct objfile *objfile = cu->per_objfile->objfile;
21824
21825 attr = dwarf2_attr (die, DW_AT_name, cu);
21826 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
21827 if (attr_name == nullptr
21828 && die->tag != DW_TAG_namespace
21829 && die->tag != DW_TAG_class_type
21830 && die->tag != DW_TAG_interface_type
21831 && die->tag != DW_TAG_structure_type
21832 && die->tag != DW_TAG_namelist
21833 && die->tag != DW_TAG_union_type)
21834 return NULL;
21835
21836 switch (die->tag)
21837 {
21838 case DW_TAG_compile_unit:
21839 case DW_TAG_partial_unit:
21840 /* Compilation units have a DW_AT_name that is a filename, not
21841 a source language identifier. */
21842 case DW_TAG_enumeration_type:
21843 case DW_TAG_enumerator:
21844 /* These tags always have simple identifiers already; no need
21845 to canonicalize them. */
21846 return attr_name;
21847
21848 case DW_TAG_namespace:
21849 if (attr_name != nullptr)
21850 return attr_name;
21851 return CP_ANONYMOUS_NAMESPACE_STR;
21852
21853 case DW_TAG_class_type:
21854 case DW_TAG_interface_type:
21855 case DW_TAG_structure_type:
21856 case DW_TAG_union_type:
21857 case DW_TAG_namelist:
21858 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21859 structures or unions. These were of the form "._%d" in GCC 4.1,
21860 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21861 and GCC 4.4. We work around this problem by ignoring these. */
21862 if (attr_name != nullptr
21863 && (startswith (attr_name, "._")
21864 || startswith (attr_name, "<anonymous")))
21865 return NULL;
21866
21867 /* GCC might emit a nameless typedef that has a linkage name. See
21868 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21869 if (!attr || attr_name == NULL)
21870 {
21871 attr = dw2_linkage_name_attr (die, cu);
21872 attr_name = attr == nullptr ? nullptr : attr->as_string ();
21873 if (attr == NULL || attr_name == NULL)
21874 return NULL;
21875
21876 /* Avoid demangling attr_name the second time on a second
21877 call for the same DIE. */
21878 if (!attr->canonical_string_p ())
21879 {
21880 gdb::unique_xmalloc_ptr<char> demangled
21881 (gdb_demangle (attr_name, DMGL_TYPES));
21882 if (demangled == nullptr)
21883 return nullptr;
21884
21885 attr->set_string_canonical (objfile->intern (demangled.get ()));
21886 attr_name = attr->as_string ();
21887 }
21888
21889 /* Strip any leading namespaces/classes, keep only the
21890 base name. DW_AT_name for named DIEs does not
21891 contain the prefixes. */
21892 const char *base = strrchr (attr_name, ':');
21893 if (base && base > attr_name && base[-1] == ':')
21894 return &base[1];
21895 else
21896 return attr_name;
21897 }
21898 break;
21899
21900 default:
21901 break;
21902 }
21903
21904 if (!attr->canonical_string_p ())
21905 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
21906 objfile));
21907 return attr->as_string ();
21908 }
21909
21910 /* Return the die that this die in an extension of, or NULL if there
21911 is none. *EXT_CU is the CU containing DIE on input, and the CU
21912 containing the return value on output. */
21913
21914 static struct die_info *
21915 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21916 {
21917 struct attribute *attr;
21918
21919 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21920 if (attr == NULL)
21921 return NULL;
21922
21923 return follow_die_ref (die, attr, ext_cu);
21924 }
21925
21926 static void
21927 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21928 {
21929 unsigned int i;
21930
21931 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
21932 indent, "",
21933 dwarf_tag_name (die->tag), die->abbrev,
21934 sect_offset_str (die->sect_off));
21935
21936 if (die->parent != NULL)
21937 gdb_printf (f, "%*s parent at offset: %s\n",
21938 indent, "",
21939 sect_offset_str (die->parent->sect_off));
21940
21941 gdb_printf (f, "%*s has children: %s\n",
21942 indent, "",
21943 dwarf_bool_name (die->child != NULL));
21944
21945 gdb_printf (f, "%*s attributes:\n", indent, "");
21946
21947 for (i = 0; i < die->num_attrs; ++i)
21948 {
21949 gdb_printf (f, "%*s %s (%s) ",
21950 indent, "",
21951 dwarf_attr_name (die->attrs[i].name),
21952 dwarf_form_name (die->attrs[i].form));
21953
21954 switch (die->attrs[i].form)
21955 {
21956 case DW_FORM_addr:
21957 case DW_FORM_addrx:
21958 case DW_FORM_GNU_addr_index:
21959 gdb_printf (f, "address: ");
21960 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
21961 break;
21962 case DW_FORM_block2:
21963 case DW_FORM_block4:
21964 case DW_FORM_block:
21965 case DW_FORM_block1:
21966 gdb_printf (f, "block: size %s",
21967 pulongest (die->attrs[i].as_block ()->size));
21968 break;
21969 case DW_FORM_exprloc:
21970 gdb_printf (f, "expression: size %s",
21971 pulongest (die->attrs[i].as_block ()->size));
21972 break;
21973 case DW_FORM_data16:
21974 gdb_printf (f, "constant of 16 bytes");
21975 break;
21976 case DW_FORM_ref_addr:
21977 gdb_printf (f, "ref address: ");
21978 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
21979 break;
21980 case DW_FORM_GNU_ref_alt:
21981 gdb_printf (f, "alt ref address: ");
21982 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
21983 break;
21984 case DW_FORM_ref1:
21985 case DW_FORM_ref2:
21986 case DW_FORM_ref4:
21987 case DW_FORM_ref8:
21988 case DW_FORM_ref_udata:
21989 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
21990 (long) (die->attrs[i].as_unsigned ()));
21991 break;
21992 case DW_FORM_data1:
21993 case DW_FORM_data2:
21994 case DW_FORM_data4:
21995 case DW_FORM_data8:
21996 case DW_FORM_udata:
21997 gdb_printf (f, "constant: %s",
21998 pulongest (die->attrs[i].as_unsigned ()));
21999 break;
22000 case DW_FORM_sec_offset:
22001 gdb_printf (f, "section offset: %s",
22002 pulongest (die->attrs[i].as_unsigned ()));
22003 break;
22004 case DW_FORM_ref_sig8:
22005 gdb_printf (f, "signature: %s",
22006 hex_string (die->attrs[i].as_signature ()));
22007 break;
22008 case DW_FORM_string:
22009 case DW_FORM_strp:
22010 case DW_FORM_line_strp:
22011 case DW_FORM_strx:
22012 case DW_FORM_GNU_str_index:
22013 case DW_FORM_GNU_strp_alt:
22014 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
22015 die->attrs[i].as_string ()
22016 ? die->attrs[i].as_string () : "",
22017 die->attrs[i].canonical_string_p () ? "is" : "not");
22018 break;
22019 case DW_FORM_flag:
22020 if (die->attrs[i].as_boolean ())
22021 gdb_printf (f, "flag: TRUE");
22022 else
22023 gdb_printf (f, "flag: FALSE");
22024 break;
22025 case DW_FORM_flag_present:
22026 gdb_printf (f, "flag: TRUE");
22027 break;
22028 case DW_FORM_indirect:
22029 /* The reader will have reduced the indirect form to
22030 the "base form" so this form should not occur. */
22031 gdb_printf (f,
22032 "unexpected attribute form: DW_FORM_indirect");
22033 break;
22034 case DW_FORM_sdata:
22035 case DW_FORM_implicit_const:
22036 gdb_printf (f, "constant: %s",
22037 plongest (die->attrs[i].as_signed ()));
22038 break;
22039 default:
22040 gdb_printf (f, "unsupported attribute form: %d.",
22041 die->attrs[i].form);
22042 break;
22043 }
22044 gdb_printf (f, "\n");
22045 }
22046 }
22047
22048 static void
22049 dump_die_for_error (struct die_info *die)
22050 {
22051 dump_die_shallow (gdb_stderr, 0, die);
22052 }
22053
22054 static void
22055 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22056 {
22057 int indent = level * 4;
22058
22059 gdb_assert (die != NULL);
22060
22061 if (level >= max_level)
22062 return;
22063
22064 dump_die_shallow (f, indent, die);
22065
22066 if (die->child != NULL)
22067 {
22068 gdb_printf (f, "%*s Children:", indent, "");
22069 if (level + 1 < max_level)
22070 {
22071 gdb_printf (f, "\n");
22072 dump_die_1 (f, level + 1, max_level, die->child);
22073 }
22074 else
22075 {
22076 gdb_printf (f,
22077 " [not printed, max nesting level reached]\n");
22078 }
22079 }
22080
22081 if (die->sibling != NULL && level > 0)
22082 {
22083 dump_die_1 (f, level, max_level, die->sibling);
22084 }
22085 }
22086
22087 /* This is called from the pdie macro in gdbinit.in.
22088 It's not static so gcc will keep a copy callable from gdb. */
22089
22090 void
22091 dump_die (struct die_info *die, int max_level)
22092 {
22093 dump_die_1 (gdb_stdlog, 0, max_level, die);
22094 }
22095
22096 static void
22097 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22098 {
22099 void **slot;
22100
22101 slot = htab_find_slot_with_hash (cu->die_hash, die,
22102 to_underlying (die->sect_off),
22103 INSERT);
22104
22105 *slot = die;
22106 }
22107
22108 /* Follow reference or signature attribute ATTR of SRC_DIE.
22109 On entry *REF_CU is the CU of SRC_DIE.
22110 On exit *REF_CU is the CU of the result. */
22111
22112 static struct die_info *
22113 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22114 struct dwarf2_cu **ref_cu)
22115 {
22116 struct die_info *die;
22117
22118 if (attr->form_is_ref ())
22119 die = follow_die_ref (src_die, attr, ref_cu);
22120 else if (attr->form == DW_FORM_ref_sig8)
22121 die = follow_die_sig (src_die, attr, ref_cu);
22122 else
22123 {
22124 dump_die_for_error (src_die);
22125 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22126 objfile_name ((*ref_cu)->per_objfile->objfile));
22127 }
22128
22129 return die;
22130 }
22131
22132 /* Follow reference OFFSET.
22133 On entry *REF_CU is the CU of the source die referencing OFFSET.
22134 On exit *REF_CU is the CU of the result.
22135 Returns NULL if OFFSET is invalid. */
22136
22137 static struct die_info *
22138 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22139 struct dwarf2_cu **ref_cu)
22140 {
22141 struct die_info temp_die;
22142 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22143 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22144
22145 gdb_assert (cu->per_cu != NULL);
22146
22147 target_cu = cu;
22148
22149 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22150 "source CU contains target offset: %d",
22151 sect_offset_str (cu->per_cu->sect_off),
22152 sect_offset_str (sect_off),
22153 cu->header.offset_in_cu_p (sect_off));
22154
22155 if (cu->per_cu->is_debug_types)
22156 {
22157 /* .debug_types CUs cannot reference anything outside their CU.
22158 If they need to, they have to reference a signatured type via
22159 DW_FORM_ref_sig8. */
22160 if (!cu->header.offset_in_cu_p (sect_off))
22161 return NULL;
22162 }
22163 else if (offset_in_dwz != cu->per_cu->is_dwz
22164 || !cu->header.offset_in_cu_p (sect_off))
22165 {
22166 struct dwarf2_per_cu_data *per_cu;
22167
22168 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22169 per_objfile->per_bfd);
22170
22171 dwarf_read_debug_printf_v ("target CU offset: %s, "
22172 "target CU DIEs loaded: %d",
22173 sect_offset_str (per_cu->sect_off),
22174 per_objfile->get_cu (per_cu) != nullptr);
22175
22176 /* If necessary, add it to the queue and load its DIEs.
22177
22178 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22179 it doesn't mean they are currently loaded. Since we require them
22180 to be loaded, we must check for ourselves. */
22181 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
22182 || per_objfile->get_cu (per_cu) == nullptr)
22183 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
22184 false, cu->per_cu->lang);
22185
22186 target_cu = per_objfile->get_cu (per_cu);
22187 gdb_assert (target_cu != nullptr);
22188 }
22189 else if (cu->dies == NULL)
22190 {
22191 /* We're loading full DIEs during partial symbol reading. */
22192 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
22193 language_minimal);
22194 }
22195
22196 *ref_cu = target_cu;
22197 temp_die.sect_off = sect_off;
22198
22199 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22200 &temp_die,
22201 to_underlying (sect_off));
22202 }
22203
22204 /* Follow reference attribute ATTR of SRC_DIE.
22205 On entry *REF_CU is the CU of SRC_DIE.
22206 On exit *REF_CU is the CU of the result. */
22207
22208 static struct die_info *
22209 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22210 struct dwarf2_cu **ref_cu)
22211 {
22212 sect_offset sect_off = attr->get_ref_die_offset ();
22213 struct dwarf2_cu *cu = *ref_cu;
22214 struct die_info *die;
22215
22216 die = follow_die_offset (sect_off,
22217 (attr->form == DW_FORM_GNU_ref_alt
22218 || cu->per_cu->is_dwz),
22219 ref_cu);
22220 if (!die)
22221 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22222 "at %s [in module %s]"),
22223 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22224 objfile_name (cu->per_objfile->objfile));
22225
22226 return die;
22227 }
22228
22229 /* See read.h. */
22230
22231 struct dwarf2_locexpr_baton
22232 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22233 dwarf2_per_cu_data *per_cu,
22234 dwarf2_per_objfile *per_objfile,
22235 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22236 bool resolve_abstract_p)
22237 {
22238 struct die_info *die;
22239 struct attribute *attr;
22240 struct dwarf2_locexpr_baton retval;
22241 struct objfile *objfile = per_objfile->objfile;
22242
22243 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22244 if (cu == nullptr)
22245 cu = load_cu (per_cu, per_objfile, false);
22246
22247 if (cu == nullptr)
22248 {
22249 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22250 Instead just throw an error, not much else we can do. */
22251 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22252 sect_offset_str (sect_off), objfile_name (objfile));
22253 }
22254
22255 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22256 if (!die)
22257 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22258 sect_offset_str (sect_off), objfile_name (objfile));
22259
22260 attr = dwarf2_attr (die, DW_AT_location, cu);
22261 if (!attr && resolve_abstract_p
22262 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22263 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22264 {
22265 CORE_ADDR pc = get_frame_pc ();
22266 CORE_ADDR baseaddr = objfile->text_section_offset ();
22267 struct gdbarch *gdbarch = objfile->arch ();
22268
22269 for (const auto &cand_off
22270 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22271 {
22272 struct dwarf2_cu *cand_cu = cu;
22273 struct die_info *cand
22274 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22275 if (!cand
22276 || !cand->parent
22277 || cand->parent->tag != DW_TAG_subprogram)
22278 continue;
22279
22280 CORE_ADDR pc_low, pc_high;
22281 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22282 if (pc_low == ((CORE_ADDR) -1))
22283 continue;
22284 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22285 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22286 if (!(pc_low <= pc && pc < pc_high))
22287 continue;
22288
22289 die = cand;
22290 attr = dwarf2_attr (die, DW_AT_location, cu);
22291 break;
22292 }
22293 }
22294
22295 if (!attr)
22296 {
22297 /* DWARF: "If there is no such attribute, then there is no effect.".
22298 DATA is ignored if SIZE is 0. */
22299
22300 retval.data = NULL;
22301 retval.size = 0;
22302 }
22303 else if (attr->form_is_section_offset ())
22304 {
22305 struct dwarf2_loclist_baton loclist_baton;
22306 CORE_ADDR pc = get_frame_pc ();
22307 size_t size;
22308
22309 fill_in_loclist_baton (cu, &loclist_baton, attr);
22310
22311 retval.data = dwarf2_find_location_expression (&loclist_baton,
22312 &size, pc);
22313 retval.size = size;
22314 }
22315 else
22316 {
22317 if (!attr->form_is_block ())
22318 error (_("Dwarf Error: DIE at %s referenced in module %s "
22319 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22320 sect_offset_str (sect_off), objfile_name (objfile));
22321
22322 struct dwarf_block *block = attr->as_block ();
22323 retval.data = block->data;
22324 retval.size = block->size;
22325 }
22326 retval.per_objfile = per_objfile;
22327 retval.per_cu = cu->per_cu;
22328
22329 per_objfile->age_comp_units ();
22330
22331 return retval;
22332 }
22333
22334 /* See read.h. */
22335
22336 struct dwarf2_locexpr_baton
22337 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22338 dwarf2_per_cu_data *per_cu,
22339 dwarf2_per_objfile *per_objfile,
22340 gdb::function_view<CORE_ADDR ()> get_frame_pc)
22341 {
22342 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22343
22344 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22345 get_frame_pc);
22346 }
22347
22348 /* Write a constant of a given type as target-ordered bytes into
22349 OBSTACK. */
22350
22351 static const gdb_byte *
22352 write_constant_as_bytes (struct obstack *obstack,
22353 enum bfd_endian byte_order,
22354 struct type *type,
22355 ULONGEST value,
22356 LONGEST *len)
22357 {
22358 gdb_byte *result;
22359
22360 *len = TYPE_LENGTH (type);
22361 result = (gdb_byte *) obstack_alloc (obstack, *len);
22362 store_unsigned_integer (result, *len, byte_order, value);
22363
22364 return result;
22365 }
22366
22367 /* See read.h. */
22368
22369 const gdb_byte *
22370 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22371 dwarf2_per_cu_data *per_cu,
22372 dwarf2_per_objfile *per_objfile,
22373 obstack *obstack,
22374 LONGEST *len)
22375 {
22376 struct die_info *die;
22377 struct attribute *attr;
22378 const gdb_byte *result = NULL;
22379 struct type *type;
22380 LONGEST value;
22381 enum bfd_endian byte_order;
22382 struct objfile *objfile = per_objfile->objfile;
22383
22384 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22385 if (cu == nullptr)
22386 cu = load_cu (per_cu, per_objfile, false);
22387
22388 if (cu == nullptr)
22389 {
22390 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22391 Instead just throw an error, not much else we can do. */
22392 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22393 sect_offset_str (sect_off), objfile_name (objfile));
22394 }
22395
22396 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22397 if (!die)
22398 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22399 sect_offset_str (sect_off), objfile_name (objfile));
22400
22401 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22402 if (attr == NULL)
22403 return NULL;
22404
22405 byte_order = (bfd_big_endian (objfile->obfd)
22406 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22407
22408 switch (attr->form)
22409 {
22410 case DW_FORM_addr:
22411 case DW_FORM_addrx:
22412 case DW_FORM_GNU_addr_index:
22413 {
22414 gdb_byte *tem;
22415
22416 *len = cu->header.addr_size;
22417 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22418 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
22419 result = tem;
22420 }
22421 break;
22422 case DW_FORM_string:
22423 case DW_FORM_strp:
22424 case DW_FORM_strx:
22425 case DW_FORM_GNU_str_index:
22426 case DW_FORM_GNU_strp_alt:
22427 /* The string is already allocated on the objfile obstack, point
22428 directly to it. */
22429 {
22430 const char *attr_name = attr->as_string ();
22431 result = (const gdb_byte *) attr_name;
22432 *len = strlen (attr_name);
22433 }
22434 break;
22435 case DW_FORM_block1:
22436 case DW_FORM_block2:
22437 case DW_FORM_block4:
22438 case DW_FORM_block:
22439 case DW_FORM_exprloc:
22440 case DW_FORM_data16:
22441 {
22442 struct dwarf_block *block = attr->as_block ();
22443 result = block->data;
22444 *len = block->size;
22445 }
22446 break;
22447
22448 /* The DW_AT_const_value attributes are supposed to carry the
22449 symbol's value "represented as it would be on the target
22450 architecture." By the time we get here, it's already been
22451 converted to host endianness, so we just need to sign- or
22452 zero-extend it as appropriate. */
22453 case DW_FORM_data1:
22454 type = die_type (die, cu);
22455 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22456 if (result == NULL)
22457 result = write_constant_as_bytes (obstack, byte_order,
22458 type, value, len);
22459 break;
22460 case DW_FORM_data2:
22461 type = die_type (die, cu);
22462 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22463 if (result == NULL)
22464 result = write_constant_as_bytes (obstack, byte_order,
22465 type, value, len);
22466 break;
22467 case DW_FORM_data4:
22468 type = die_type (die, cu);
22469 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22470 if (result == NULL)
22471 result = write_constant_as_bytes (obstack, byte_order,
22472 type, value, len);
22473 break;
22474 case DW_FORM_data8:
22475 type = die_type (die, cu);
22476 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22477 if (result == NULL)
22478 result = write_constant_as_bytes (obstack, byte_order,
22479 type, value, len);
22480 break;
22481
22482 case DW_FORM_sdata:
22483 case DW_FORM_implicit_const:
22484 type = die_type (die, cu);
22485 result = write_constant_as_bytes (obstack, byte_order,
22486 type, attr->as_signed (), len);
22487 break;
22488
22489 case DW_FORM_udata:
22490 type = die_type (die, cu);
22491 result = write_constant_as_bytes (obstack, byte_order,
22492 type, attr->as_unsigned (), len);
22493 break;
22494
22495 default:
22496 complaint (_("unsupported const value attribute form: '%s'"),
22497 dwarf_form_name (attr->form));
22498 break;
22499 }
22500
22501 return result;
22502 }
22503
22504 /* See read.h. */
22505
22506 struct type *
22507 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22508 dwarf2_per_cu_data *per_cu,
22509 dwarf2_per_objfile *per_objfile,
22510 const char **var_name)
22511 {
22512 struct die_info *die;
22513
22514 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22515 if (cu == nullptr)
22516 cu = load_cu (per_cu, per_objfile, false);
22517
22518 if (cu == nullptr)
22519 return nullptr;
22520
22521 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22522 if (!die)
22523 return NULL;
22524
22525 if (var_name != nullptr)
22526 *var_name = var_decl_name (die, cu);
22527 return die_type (die, cu);
22528 }
22529
22530 /* See read.h. */
22531
22532 struct type *
22533 dwarf2_get_die_type (cu_offset die_offset,
22534 dwarf2_per_cu_data *per_cu,
22535 dwarf2_per_objfile *per_objfile)
22536 {
22537 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22538 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22539 }
22540
22541 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22542 On entry *REF_CU is the CU of SRC_DIE.
22543 On exit *REF_CU is the CU of the result.
22544 Returns NULL if the referenced DIE isn't found. */
22545
22546 static struct die_info *
22547 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22548 struct dwarf2_cu **ref_cu)
22549 {
22550 struct die_info temp_die;
22551 struct dwarf2_cu *sig_cu;
22552 struct die_info *die;
22553 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22554
22555
22556 /* While it might be nice to assert sig_type->type == NULL here,
22557 we can get here for DW_AT_imported_declaration where we need
22558 the DIE not the type. */
22559
22560 /* If necessary, add it to the queue and load its DIEs.
22561
22562 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22563 it doesn't mean they are currently loaded. Since we require them
22564 to be loaded, we must check for ourselves. */
22565 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
22566 language_minimal)
22567 || per_objfile->get_cu (sig_type) == nullptr)
22568 read_signatured_type (sig_type, per_objfile);
22569
22570 sig_cu = per_objfile->get_cu (sig_type);
22571 gdb_assert (sig_cu != NULL);
22572 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22573 temp_die.sect_off = sig_type->type_offset_in_section;
22574 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22575 to_underlying (temp_die.sect_off));
22576 if (die)
22577 {
22578 /* For .gdb_index version 7 keep track of included TUs.
22579 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22580 if (per_objfile->per_bfd->index_table != NULL
22581 && !per_objfile->per_bfd->index_table->version_check ())
22582 {
22583 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22584 }
22585
22586 *ref_cu = sig_cu;
22587 return die;
22588 }
22589
22590 return NULL;
22591 }
22592
22593 /* Follow signatured type referenced by ATTR in SRC_DIE.
22594 On entry *REF_CU is the CU of SRC_DIE.
22595 On exit *REF_CU is the CU of the result.
22596 The result is the DIE of the type.
22597 If the referenced type cannot be found an error is thrown. */
22598
22599 static struct die_info *
22600 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22601 struct dwarf2_cu **ref_cu)
22602 {
22603 ULONGEST signature = attr->as_signature ();
22604 struct signatured_type *sig_type;
22605 struct die_info *die;
22606
22607 gdb_assert (attr->form == DW_FORM_ref_sig8);
22608
22609 sig_type = lookup_signatured_type (*ref_cu, signature);
22610 /* sig_type will be NULL if the signatured type is missing from
22611 the debug info. */
22612 if (sig_type == NULL)
22613 {
22614 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22615 " from DIE at %s [in module %s]"),
22616 hex_string (signature), sect_offset_str (src_die->sect_off),
22617 objfile_name ((*ref_cu)->per_objfile->objfile));
22618 }
22619
22620 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22621 if (die == NULL)
22622 {
22623 dump_die_for_error (src_die);
22624 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22625 " from DIE at %s [in module %s]"),
22626 hex_string (signature), sect_offset_str (src_die->sect_off),
22627 objfile_name ((*ref_cu)->per_objfile->objfile));
22628 }
22629
22630 return die;
22631 }
22632
22633 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22634 reading in and processing the type unit if necessary. */
22635
22636 static struct type *
22637 get_signatured_type (struct die_info *die, ULONGEST signature,
22638 struct dwarf2_cu *cu)
22639 {
22640 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22641 struct signatured_type *sig_type;
22642 struct dwarf2_cu *type_cu;
22643 struct die_info *type_die;
22644 struct type *type;
22645
22646 sig_type = lookup_signatured_type (cu, signature);
22647 /* sig_type will be NULL if the signatured type is missing from
22648 the debug info. */
22649 if (sig_type == NULL)
22650 {
22651 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22652 " from DIE at %s [in module %s]"),
22653 hex_string (signature), sect_offset_str (die->sect_off),
22654 objfile_name (per_objfile->objfile));
22655 return build_error_marker_type (cu, die);
22656 }
22657
22658 /* If we already know the type we're done. */
22659 type = per_objfile->get_type_for_signatured_type (sig_type);
22660 if (type != nullptr)
22661 return type;
22662
22663 type_cu = cu;
22664 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22665 if (type_die != NULL)
22666 {
22667 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22668 is created. This is important, for example, because for c++ classes
22669 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22670 type = read_type_die (type_die, type_cu);
22671 if (type == NULL)
22672 {
22673 complaint (_("Dwarf Error: Cannot build signatured type %s"
22674 " referenced from DIE at %s [in module %s]"),
22675 hex_string (signature), sect_offset_str (die->sect_off),
22676 objfile_name (per_objfile->objfile));
22677 type = build_error_marker_type (cu, die);
22678 }
22679 }
22680 else
22681 {
22682 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22683 " from DIE at %s [in module %s]"),
22684 hex_string (signature), sect_offset_str (die->sect_off),
22685 objfile_name (per_objfile->objfile));
22686 type = build_error_marker_type (cu, die);
22687 }
22688
22689 per_objfile->set_type_for_signatured_type (sig_type, type);
22690
22691 return type;
22692 }
22693
22694 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22695 reading in and processing the type unit if necessary. */
22696
22697 static struct type *
22698 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22699 struct dwarf2_cu *cu) /* ARI: editCase function */
22700 {
22701 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22702 if (attr->form_is_ref ())
22703 {
22704 struct dwarf2_cu *type_cu = cu;
22705 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22706
22707 return read_type_die (type_die, type_cu);
22708 }
22709 else if (attr->form == DW_FORM_ref_sig8)
22710 {
22711 return get_signatured_type (die, attr->as_signature (), cu);
22712 }
22713 else
22714 {
22715 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22716
22717 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22718 " at %s [in module %s]"),
22719 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22720 objfile_name (per_objfile->objfile));
22721 return build_error_marker_type (cu, die);
22722 }
22723 }
22724
22725 /* Load the DIEs associated with type unit PER_CU into memory. */
22726
22727 static void
22728 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22729 dwarf2_per_objfile *per_objfile)
22730 {
22731 struct signatured_type *sig_type;
22732
22733 /* We have the per_cu, but we need the signatured_type.
22734 Fortunately this is an easy translation. */
22735 gdb_assert (per_cu->is_debug_types);
22736 sig_type = (struct signatured_type *) per_cu;
22737
22738 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22739
22740 read_signatured_type (sig_type, per_objfile);
22741
22742 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22743 }
22744
22745 /* Read in a signatured type and build its CU and DIEs.
22746 If the type is a stub for the real type in a DWO file,
22747 read in the real type from the DWO file as well. */
22748
22749 static void
22750 read_signatured_type (signatured_type *sig_type,
22751 dwarf2_per_objfile *per_objfile)
22752 {
22753 gdb_assert (sig_type->is_debug_types);
22754 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
22755
22756 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
22757
22758 if (!reader.dummy_p)
22759 {
22760 struct dwarf2_cu *cu = reader.cu;
22761 const gdb_byte *info_ptr = reader.info_ptr;
22762
22763 gdb_assert (cu->die_hash == NULL);
22764 cu->die_hash =
22765 htab_create_alloc_ex (cu->header.length / 12,
22766 die_hash,
22767 die_eq,
22768 NULL,
22769 &cu->comp_unit_obstack,
22770 hashtab_obstack_allocate,
22771 dummy_obstack_deallocate);
22772
22773 if (reader.comp_unit_die->has_children)
22774 reader.comp_unit_die->child
22775 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22776 reader.comp_unit_die);
22777 cu->dies = reader.comp_unit_die;
22778 /* comp_unit_die is not stored in die_hash, no need. */
22779
22780 /* We try not to read any attributes in this function, because
22781 not all CUs needed for references have been loaded yet, and
22782 symbol table processing isn't initialized. But we have to
22783 set the CU language, or we won't be able to build types
22784 correctly. Similarly, if we do not read the producer, we can
22785 not apply producer-specific interpretation. */
22786 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22787
22788 reader.keep ();
22789 }
22790
22791 sig_type->tu_read = 1;
22792 }
22793
22794 /* Decode simple location descriptions.
22795 Given a pointer to a dwarf block that defines a location, compute
22796 the location and return the value. If COMPUTED is non-null, it is
22797 set to true to indicate that decoding was successful, and false
22798 otherwise. If COMPUTED is null, then this function may emit a
22799 complaint. */
22800
22801 static CORE_ADDR
22802 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22803 {
22804 struct objfile *objfile = cu->per_objfile->objfile;
22805 size_t i;
22806 size_t size = blk->size;
22807 const gdb_byte *data = blk->data;
22808 CORE_ADDR stack[64];
22809 int stacki;
22810 unsigned int bytes_read, unsnd;
22811 gdb_byte op;
22812
22813 if (computed != nullptr)
22814 *computed = false;
22815
22816 i = 0;
22817 stacki = 0;
22818 stack[stacki] = 0;
22819 stack[++stacki] = 0;
22820
22821 while (i < size)
22822 {
22823 op = data[i++];
22824 switch (op)
22825 {
22826 case DW_OP_lit0:
22827 case DW_OP_lit1:
22828 case DW_OP_lit2:
22829 case DW_OP_lit3:
22830 case DW_OP_lit4:
22831 case DW_OP_lit5:
22832 case DW_OP_lit6:
22833 case DW_OP_lit7:
22834 case DW_OP_lit8:
22835 case DW_OP_lit9:
22836 case DW_OP_lit10:
22837 case DW_OP_lit11:
22838 case DW_OP_lit12:
22839 case DW_OP_lit13:
22840 case DW_OP_lit14:
22841 case DW_OP_lit15:
22842 case DW_OP_lit16:
22843 case DW_OP_lit17:
22844 case DW_OP_lit18:
22845 case DW_OP_lit19:
22846 case DW_OP_lit20:
22847 case DW_OP_lit21:
22848 case DW_OP_lit22:
22849 case DW_OP_lit23:
22850 case DW_OP_lit24:
22851 case DW_OP_lit25:
22852 case DW_OP_lit26:
22853 case DW_OP_lit27:
22854 case DW_OP_lit28:
22855 case DW_OP_lit29:
22856 case DW_OP_lit30:
22857 case DW_OP_lit31:
22858 stack[++stacki] = op - DW_OP_lit0;
22859 break;
22860
22861 case DW_OP_reg0:
22862 case DW_OP_reg1:
22863 case DW_OP_reg2:
22864 case DW_OP_reg3:
22865 case DW_OP_reg4:
22866 case DW_OP_reg5:
22867 case DW_OP_reg6:
22868 case DW_OP_reg7:
22869 case DW_OP_reg8:
22870 case DW_OP_reg9:
22871 case DW_OP_reg10:
22872 case DW_OP_reg11:
22873 case DW_OP_reg12:
22874 case DW_OP_reg13:
22875 case DW_OP_reg14:
22876 case DW_OP_reg15:
22877 case DW_OP_reg16:
22878 case DW_OP_reg17:
22879 case DW_OP_reg18:
22880 case DW_OP_reg19:
22881 case DW_OP_reg20:
22882 case DW_OP_reg21:
22883 case DW_OP_reg22:
22884 case DW_OP_reg23:
22885 case DW_OP_reg24:
22886 case DW_OP_reg25:
22887 case DW_OP_reg26:
22888 case DW_OP_reg27:
22889 case DW_OP_reg28:
22890 case DW_OP_reg29:
22891 case DW_OP_reg30:
22892 case DW_OP_reg31:
22893 stack[++stacki] = op - DW_OP_reg0;
22894 if (i < size)
22895 {
22896 if (computed == nullptr)
22897 dwarf2_complex_location_expr_complaint ();
22898 else
22899 return 0;
22900 }
22901 break;
22902
22903 case DW_OP_regx:
22904 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22905 i += bytes_read;
22906 stack[++stacki] = unsnd;
22907 if (i < size)
22908 {
22909 if (computed == nullptr)
22910 dwarf2_complex_location_expr_complaint ();
22911 else
22912 return 0;
22913 }
22914 break;
22915
22916 case DW_OP_addr:
22917 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22918 &bytes_read);
22919 i += bytes_read;
22920 break;
22921
22922 case DW_OP_const1u:
22923 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22924 i += 1;
22925 break;
22926
22927 case DW_OP_const1s:
22928 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22929 i += 1;
22930 break;
22931
22932 case DW_OP_const2u:
22933 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22934 i += 2;
22935 break;
22936
22937 case DW_OP_const2s:
22938 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22939 i += 2;
22940 break;
22941
22942 case DW_OP_const4u:
22943 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22944 i += 4;
22945 break;
22946
22947 case DW_OP_const4s:
22948 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22949 i += 4;
22950 break;
22951
22952 case DW_OP_const8u:
22953 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22954 i += 8;
22955 break;
22956
22957 case DW_OP_constu:
22958 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22959 &bytes_read);
22960 i += bytes_read;
22961 break;
22962
22963 case DW_OP_consts:
22964 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22965 i += bytes_read;
22966 break;
22967
22968 case DW_OP_dup:
22969 stack[stacki + 1] = stack[stacki];
22970 stacki++;
22971 break;
22972
22973 case DW_OP_plus:
22974 stack[stacki - 1] += stack[stacki];
22975 stacki--;
22976 break;
22977
22978 case DW_OP_plus_uconst:
22979 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22980 &bytes_read);
22981 i += bytes_read;
22982 break;
22983
22984 case DW_OP_minus:
22985 stack[stacki - 1] -= stack[stacki];
22986 stacki--;
22987 break;
22988
22989 case DW_OP_deref:
22990 /* If we're not the last op, then we definitely can't encode
22991 this using GDB's address_class enum. This is valid for partial
22992 global symbols, although the variable's address will be bogus
22993 in the psymtab. */
22994 if (i < size)
22995 {
22996 if (computed == nullptr)
22997 dwarf2_complex_location_expr_complaint ();
22998 else
22999 return 0;
23000 }
23001 break;
23002
23003 case DW_OP_GNU_push_tls_address:
23004 case DW_OP_form_tls_address:
23005 /* The top of the stack has the offset from the beginning
23006 of the thread control block at which the variable is located. */
23007 /* Nothing should follow this operator, so the top of stack would
23008 be returned. */
23009 /* This is valid for partial global symbols, but the variable's
23010 address will be bogus in the psymtab. Make it always at least
23011 non-zero to not look as a variable garbage collected by linker
23012 which have DW_OP_addr 0. */
23013 if (i < size)
23014 {
23015 if (computed == nullptr)
23016 dwarf2_complex_location_expr_complaint ();
23017 else
23018 return 0;
23019 }
23020 stack[stacki]++;
23021 break;
23022
23023 case DW_OP_GNU_uninit:
23024 if (computed != nullptr)
23025 return 0;
23026 break;
23027
23028 case DW_OP_addrx:
23029 case DW_OP_GNU_addr_index:
23030 case DW_OP_GNU_const_index:
23031 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23032 &bytes_read);
23033 i += bytes_read;
23034 break;
23035
23036 default:
23037 if (computed == nullptr)
23038 {
23039 const char *name = get_DW_OP_name (op);
23040
23041 if (name)
23042 complaint (_("unsupported stack op: '%s'"),
23043 name);
23044 else
23045 complaint (_("unsupported stack op: '%02x'"),
23046 op);
23047 }
23048
23049 return (stack[stacki]);
23050 }
23051
23052 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23053 outside of the allocated space. Also enforce minimum>0. */
23054 if (stacki >= ARRAY_SIZE (stack) - 1)
23055 {
23056 if (computed == nullptr)
23057 complaint (_("location description stack overflow"));
23058 return 0;
23059 }
23060
23061 if (stacki <= 0)
23062 {
23063 if (computed == nullptr)
23064 complaint (_("location description stack underflow"));
23065 return 0;
23066 }
23067 }
23068
23069 if (computed != nullptr)
23070 *computed = true;
23071 return (stack[stacki]);
23072 }
23073
23074 /* memory allocation interface */
23075
23076 static struct dwarf_block *
23077 dwarf_alloc_block (struct dwarf2_cu *cu)
23078 {
23079 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23080 }
23081
23082 static struct die_info *
23083 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23084 {
23085 struct die_info *die;
23086 size_t size = sizeof (struct die_info);
23087
23088 if (num_attrs > 1)
23089 size += (num_attrs - 1) * sizeof (struct attribute);
23090
23091 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23092 memset (die, 0, sizeof (struct die_info));
23093 return (die);
23094 }
23095
23096 \f
23097
23098 /* Macro support. */
23099
23100 /* An overload of dwarf_decode_macros that finds the correct section
23101 and ensures it is read in before calling the other overload. */
23102
23103 static void
23104 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23105 int section_is_gnu)
23106 {
23107 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23108 struct objfile *objfile = per_objfile->objfile;
23109 const struct line_header *lh = cu->line_header;
23110 unsigned int offset_size = cu->header.offset_size;
23111 struct dwarf2_section_info *section;
23112 const char *section_name;
23113
23114 if (cu->dwo_unit != nullptr)
23115 {
23116 if (section_is_gnu)
23117 {
23118 section = &cu->dwo_unit->dwo_file->sections.macro;
23119 section_name = ".debug_macro.dwo";
23120 }
23121 else
23122 {
23123 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23124 section_name = ".debug_macinfo.dwo";
23125 }
23126 }
23127 else
23128 {
23129 if (section_is_gnu)
23130 {
23131 section = &per_objfile->per_bfd->macro;
23132 section_name = ".debug_macro";
23133 }
23134 else
23135 {
23136 section = &per_objfile->per_bfd->macinfo;
23137 section_name = ".debug_macinfo";
23138 }
23139 }
23140
23141 section->read (objfile);
23142 if (section->buffer == nullptr)
23143 {
23144 complaint (_("missing %s section"), section_name);
23145 return;
23146 }
23147
23148 buildsym_compunit *builder = cu->get_builder ();
23149
23150 struct dwarf2_section_info *str_offsets_section;
23151 struct dwarf2_section_info *str_section;
23152 gdb::optional<ULONGEST> str_offsets_base;
23153
23154 if (cu->dwo_unit != nullptr)
23155 {
23156 str_offsets_section = &cu->dwo_unit->dwo_file
23157 ->sections.str_offsets;
23158 str_section = &cu->dwo_unit->dwo_file->sections.str;
23159 str_offsets_base = cu->header.addr_size;
23160 }
23161 else
23162 {
23163 str_offsets_section = &per_objfile->per_bfd->str_offsets;
23164 str_section = &per_objfile->per_bfd->str;
23165 str_offsets_base = cu->str_offsets_base;
23166 }
23167
23168 dwarf_decode_macros (per_objfile, builder, section, lh,
23169 offset_size, offset, str_section, str_offsets_section,
23170 str_offsets_base, section_is_gnu);
23171 }
23172
23173 /* Return the .debug_loc section to use for CU.
23174 For DWO files use .debug_loc.dwo. */
23175
23176 static struct dwarf2_section_info *
23177 cu_debug_loc_section (struct dwarf2_cu *cu)
23178 {
23179 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23180
23181 if (cu->dwo_unit)
23182 {
23183 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23184
23185 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23186 }
23187 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23188 : &per_objfile->per_bfd->loc);
23189 }
23190
23191 /* Return the .debug_rnglists section to use for CU. */
23192 static struct dwarf2_section_info *
23193 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23194 {
23195 if (cu->header.version < 5)
23196 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23197 cu->header.version);
23198 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23199
23200 /* Make sure we read the .debug_rnglists section from the file that
23201 contains the DW_AT_ranges attribute we are reading. Normally that
23202 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23203 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23204 program. */
23205 if (cu->dwo_unit != nullptr
23206 && tag != DW_TAG_compile_unit
23207 && tag != DW_TAG_skeleton_unit)
23208 {
23209 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23210
23211 if (sections->rnglists.size > 0)
23212 return &sections->rnglists;
23213 else
23214 error (_(".debug_rnglists section is missing from .dwo file."));
23215 }
23216 return &dwarf2_per_objfile->per_bfd->rnglists;
23217 }
23218
23219 /* A helper function that fills in a dwarf2_loclist_baton. */
23220
23221 static void
23222 fill_in_loclist_baton (struct dwarf2_cu *cu,
23223 struct dwarf2_loclist_baton *baton,
23224 const struct attribute *attr)
23225 {
23226 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23227 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23228
23229 section->read (per_objfile->objfile);
23230
23231 baton->per_objfile = per_objfile;
23232 baton->per_cu = cu->per_cu;
23233 gdb_assert (baton->per_cu);
23234 /* We don't know how long the location list is, but make sure we
23235 don't run off the edge of the section. */
23236 baton->size = section->size - attr->as_unsigned ();
23237 baton->data = section->buffer + attr->as_unsigned ();
23238 if (cu->base_address.has_value ())
23239 baton->base_address = *cu->base_address;
23240 else
23241 baton->base_address = 0;
23242 baton->from_dwo = cu->dwo_unit != NULL;
23243 }
23244
23245 static void
23246 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23247 struct dwarf2_cu *cu, int is_block)
23248 {
23249 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23250 struct objfile *objfile = per_objfile->objfile;
23251 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23252
23253 if (attr->form_is_section_offset ()
23254 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23255 the section. If so, fall through to the complaint in the
23256 other branch. */
23257 && attr->as_unsigned () < section->get_size (objfile))
23258 {
23259 struct dwarf2_loclist_baton *baton;
23260
23261 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23262
23263 fill_in_loclist_baton (cu, baton, attr);
23264
23265 if (!cu->base_address.has_value ())
23266 complaint (_("Location list used without "
23267 "specifying the CU base address."));
23268
23269 sym->set_aclass_index ((is_block
23270 ? dwarf2_loclist_block_index
23271 : dwarf2_loclist_index));
23272 SYMBOL_LOCATION_BATON (sym) = baton;
23273 }
23274 else
23275 {
23276 struct dwarf2_locexpr_baton *baton;
23277
23278 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23279 baton->per_objfile = per_objfile;
23280 baton->per_cu = cu->per_cu;
23281 gdb_assert (baton->per_cu);
23282
23283 if (attr->form_is_block ())
23284 {
23285 /* Note that we're just copying the block's data pointer
23286 here, not the actual data. We're still pointing into the
23287 info_buffer for SYM's objfile; right now we never release
23288 that buffer, but when we do clean up properly this may
23289 need to change. */
23290 struct dwarf_block *block = attr->as_block ();
23291 baton->size = block->size;
23292 baton->data = block->data;
23293 }
23294 else
23295 {
23296 dwarf2_invalid_attrib_class_complaint ("location description",
23297 sym->natural_name ());
23298 baton->size = 0;
23299 }
23300
23301 sym->set_aclass_index ((is_block
23302 ? dwarf2_locexpr_block_index
23303 : dwarf2_locexpr_index));
23304 SYMBOL_LOCATION_BATON (sym) = baton;
23305 }
23306 }
23307
23308 /* See read.h. */
23309
23310 const comp_unit_head *
23311 dwarf2_per_cu_data::get_header () const
23312 {
23313 if (!m_header_read_in)
23314 {
23315 const gdb_byte *info_ptr
23316 = this->section->buffer + to_underlying (this->sect_off);
23317
23318 memset (&m_header, 0, sizeof (m_header));
23319
23320 read_comp_unit_head (&m_header, info_ptr, this->section,
23321 rcuh_kind::COMPILE);
23322
23323 m_header_read_in = true;
23324 }
23325
23326 return &m_header;
23327 }
23328
23329 /* See read.h. */
23330
23331 int
23332 dwarf2_per_cu_data::addr_size () const
23333 {
23334 return this->get_header ()->addr_size;
23335 }
23336
23337 /* See read.h. */
23338
23339 int
23340 dwarf2_per_cu_data::offset_size () const
23341 {
23342 return this->get_header ()->offset_size;
23343 }
23344
23345 /* See read.h. */
23346
23347 int
23348 dwarf2_per_cu_data::ref_addr_size () const
23349 {
23350 const comp_unit_head *header = this->get_header ();
23351
23352 if (header->version == 2)
23353 return header->addr_size;
23354 else
23355 return header->offset_size;
23356 }
23357
23358 /* A helper function for dwarf2_find_containing_comp_unit that returns
23359 the index of the result, and that searches a vector. It will
23360 return a result even if the offset in question does not actually
23361 occur in any CU. This is separate so that it can be unit
23362 tested. */
23363
23364 static int
23365 dwarf2_find_containing_comp_unit
23366 (sect_offset sect_off,
23367 unsigned int offset_in_dwz,
23368 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
23369 {
23370 int low, high;
23371
23372 low = 0;
23373 high = all_comp_units.size () - 1;
23374 while (high > low)
23375 {
23376 struct dwarf2_per_cu_data *mid_cu;
23377 int mid = low + (high - low) / 2;
23378
23379 mid_cu = all_comp_units[mid].get ();
23380 if (mid_cu->is_dwz > offset_in_dwz
23381 || (mid_cu->is_dwz == offset_in_dwz
23382 && mid_cu->sect_off + mid_cu->length > sect_off))
23383 high = mid;
23384 else
23385 low = mid + 1;
23386 }
23387 gdb_assert (low == high);
23388 return low;
23389 }
23390
23391 /* Locate the .debug_info compilation unit from CU's objfile which contains
23392 the DIE at OFFSET. Raises an error on failure. */
23393
23394 static struct dwarf2_per_cu_data *
23395 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23396 unsigned int offset_in_dwz,
23397 dwarf2_per_bfd *per_bfd)
23398 {
23399 int low = dwarf2_find_containing_comp_unit
23400 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
23401 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
23402
23403 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23404 {
23405 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23406 error (_("Dwarf Error: could not find partial DIE containing "
23407 "offset %s [in module %s]"),
23408 sect_offset_str (sect_off),
23409 bfd_get_filename (per_bfd->obfd));
23410
23411 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
23412 <= sect_off);
23413 return per_bfd->all_comp_units[low - 1].get ();
23414 }
23415 else
23416 {
23417 if (low == per_bfd->all_comp_units.size () - 1
23418 && sect_off >= this_cu->sect_off + this_cu->length)
23419 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23420 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23421 return this_cu;
23422 }
23423 }
23424
23425 #if GDB_SELF_TEST
23426
23427 namespace selftests {
23428 namespace find_containing_comp_unit {
23429
23430 static void
23431 run_test ()
23432 {
23433 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
23434 dwarf2_per_cu_data *one_ptr = one.get ();
23435 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
23436 dwarf2_per_cu_data *two_ptr = two.get ();
23437 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
23438 dwarf2_per_cu_data *three_ptr = three.get ();
23439 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
23440 dwarf2_per_cu_data *four_ptr = four.get ();
23441
23442 one->length = 5;
23443 two->sect_off = sect_offset (one->length);
23444 two->length = 7;
23445
23446 three->length = 5;
23447 three->is_dwz = 1;
23448 four->sect_off = sect_offset (three->length);
23449 four->length = 7;
23450 four->is_dwz = 1;
23451
23452 std::vector<dwarf2_per_cu_data_up> units;
23453 units.push_back (std::move (one));
23454 units.push_back (std::move (two));
23455 units.push_back (std::move (three));
23456 units.push_back (std::move (four));
23457
23458 int result;
23459
23460 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23461 SELF_CHECK (units[result].get () == one_ptr);
23462 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23463 SELF_CHECK (units[result].get () == one_ptr);
23464 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23465 SELF_CHECK (units[result].get () == two_ptr);
23466
23467 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23468 SELF_CHECK (units[result].get () == three_ptr);
23469 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23470 SELF_CHECK (units[result].get () == three_ptr);
23471 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23472 SELF_CHECK (units[result].get () == four_ptr);
23473 }
23474
23475 }
23476 }
23477
23478 #endif /* GDB_SELF_TEST */
23479
23480 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23481
23482 static void
23483 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23484 enum language pretend_language)
23485 {
23486 struct attribute *attr;
23487
23488 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23489
23490 /* Set the language we're debugging. */
23491 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23492 if (cu->producer != nullptr
23493 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
23494 {
23495 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23496 attribute is not standardised yet. As a workaround for the
23497 language detection we fall back to the DW_AT_producer
23498 string. */
23499 cu->per_cu->lang = language_opencl;
23500 }
23501 else if (cu->producer != nullptr
23502 && strstr (cu->producer, "GNU Go ") != NULL)
23503 {
23504 /* Similar hack for Go. */
23505 cu->per_cu->lang = language_go;
23506 }
23507 else if (attr != nullptr)
23508 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
23509 else
23510 cu->per_cu->lang = pretend_language;
23511 cu->language_defn = language_def (cu->per_cu->lang);
23512 }
23513
23514 /* See read.h. */
23515
23516 dwarf2_cu *
23517 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23518 {
23519 auto it = m_dwarf2_cus.find (per_cu);
23520 if (it == m_dwarf2_cus.end ())
23521 return nullptr;
23522
23523 return it->second;
23524 }
23525
23526 /* See read.h. */
23527
23528 void
23529 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23530 {
23531 gdb_assert (this->get_cu (per_cu) == nullptr);
23532
23533 m_dwarf2_cus[per_cu] = cu;
23534 }
23535
23536 /* See read.h. */
23537
23538 void
23539 dwarf2_per_objfile::age_comp_units ()
23540 {
23541 dwarf_read_debug_printf_v ("running");
23542
23543 /* This is not expected to be called in the middle of CU expansion. There is
23544 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23545 loaded in memory. Calling age_comp_units while the queue is in use could
23546 make us free the DIEs for a CU that is in the queue and therefore break
23547 that invariant. */
23548 gdb_assert (!this->per_bfd->queue.has_value ());
23549
23550 /* Start by clearing all marks. */
23551 for (auto pair : m_dwarf2_cus)
23552 pair.second->clear_mark ();
23553
23554 /* Traverse all CUs, mark them and their dependencies if used recently
23555 enough. */
23556 for (auto pair : m_dwarf2_cus)
23557 {
23558 dwarf2_cu *cu = pair.second;
23559
23560 cu->last_used++;
23561 if (cu->last_used <= dwarf_max_cache_age)
23562 cu->mark ();
23563 }
23564
23565 /* Delete all CUs still not marked. */
23566 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23567 {
23568 dwarf2_cu *cu = it->second;
23569
23570 if (!cu->is_marked ())
23571 {
23572 dwarf_read_debug_printf_v ("deleting old CU %s",
23573 sect_offset_str (cu->per_cu->sect_off));
23574 delete cu;
23575 it = m_dwarf2_cus.erase (it);
23576 }
23577 else
23578 it++;
23579 }
23580 }
23581
23582 /* See read.h. */
23583
23584 void
23585 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23586 {
23587 auto it = m_dwarf2_cus.find (per_cu);
23588 if (it == m_dwarf2_cus.end ())
23589 return;
23590
23591 delete it->second;
23592
23593 m_dwarf2_cus.erase (it);
23594 }
23595
23596 dwarf2_per_objfile::~dwarf2_per_objfile ()
23597 {
23598 remove_all_cus ();
23599 }
23600
23601 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23602 We store these in a hash table separate from the DIEs, and preserve them
23603 when the DIEs are flushed out of cache.
23604
23605 The CU "per_cu" pointer is needed because offset alone is not enough to
23606 uniquely identify the type. A file may have multiple .debug_types sections,
23607 or the type may come from a DWO file. Furthermore, while it's more logical
23608 to use per_cu->section+offset, with Fission the section with the data is in
23609 the DWO file but we don't know that section at the point we need it.
23610 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23611 because we can enter the lookup routine, get_die_type_at_offset, from
23612 outside this file, and thus won't necessarily have PER_CU->cu.
23613 Fortunately, PER_CU is stable for the life of the objfile. */
23614
23615 struct dwarf2_per_cu_offset_and_type
23616 {
23617 const struct dwarf2_per_cu_data *per_cu;
23618 sect_offset sect_off;
23619 struct type *type;
23620 };
23621
23622 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23623
23624 static hashval_t
23625 per_cu_offset_and_type_hash (const void *item)
23626 {
23627 const struct dwarf2_per_cu_offset_and_type *ofs
23628 = (const struct dwarf2_per_cu_offset_and_type *) item;
23629
23630 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23631 }
23632
23633 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23634
23635 static int
23636 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23637 {
23638 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23639 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23640 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23641 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23642
23643 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23644 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23645 }
23646
23647 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23648 table if necessary. For convenience, return TYPE.
23649
23650 The DIEs reading must have careful ordering to:
23651 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23652 reading current DIE.
23653 * Not trying to dereference contents of still incompletely read in types
23654 while reading in other DIEs.
23655 * Enable referencing still incompletely read in types just by a pointer to
23656 the type without accessing its fields.
23657
23658 Therefore caller should follow these rules:
23659 * Try to fetch any prerequisite types we may need to build this DIE type
23660 before building the type and calling set_die_type.
23661 * After building type call set_die_type for current DIE as soon as
23662 possible before fetching more types to complete the current type.
23663 * Make the type as complete as possible before fetching more types. */
23664
23665 static struct type *
23666 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
23667 bool skip_data_location)
23668 {
23669 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23670 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23671 struct objfile *objfile = per_objfile->objfile;
23672 struct attribute *attr;
23673 struct dynamic_prop prop;
23674
23675 /* For Ada types, make sure that the gnat-specific data is always
23676 initialized (if not already set). There are a few types where
23677 we should not be doing so, because the type-specific area is
23678 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23679 where the type-specific area is used to store the floatformat).
23680 But this is not a problem, because the gnat-specific information
23681 is actually not needed for these types. */
23682 if (need_gnat_info (cu)
23683 && type->code () != TYPE_CODE_FUNC
23684 && type->code () != TYPE_CODE_FLT
23685 && type->code () != TYPE_CODE_METHODPTR
23686 && type->code () != TYPE_CODE_MEMBERPTR
23687 && type->code () != TYPE_CODE_METHOD
23688 && type->code () != TYPE_CODE_FIXED_POINT
23689 && !HAVE_GNAT_AUX_INFO (type))
23690 INIT_GNAT_SPECIFIC (type);
23691
23692 /* Read DW_AT_allocated and set in type. */
23693 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23694 if (attr != NULL)
23695 {
23696 struct type *prop_type = cu->addr_sized_int_type (false);
23697 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23698 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23699 }
23700
23701 /* Read DW_AT_associated and set in type. */
23702 attr = dwarf2_attr (die, DW_AT_associated, cu);
23703 if (attr != NULL)
23704 {
23705 struct type *prop_type = cu->addr_sized_int_type (false);
23706 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23707 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23708 }
23709
23710 /* Read DW_AT_rank and set in type. */
23711 attr = dwarf2_attr (die, DW_AT_rank, cu);
23712 if (attr != NULL)
23713 {
23714 struct type *prop_type = cu->addr_sized_int_type (false);
23715 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23716 type->add_dyn_prop (DYN_PROP_RANK, prop);
23717 }
23718
23719 /* Read DW_AT_data_location and set in type. */
23720 if (!skip_data_location)
23721 {
23722 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23723 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23724 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23725 }
23726
23727 if (per_objfile->die_type_hash == NULL)
23728 per_objfile->die_type_hash
23729 = htab_up (htab_create_alloc (127,
23730 per_cu_offset_and_type_hash,
23731 per_cu_offset_and_type_eq,
23732 NULL, xcalloc, xfree));
23733
23734 ofs.per_cu = cu->per_cu;
23735 ofs.sect_off = die->sect_off;
23736 ofs.type = type;
23737 slot = (struct dwarf2_per_cu_offset_and_type **)
23738 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23739 if (*slot)
23740 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23741 sect_offset_str (die->sect_off));
23742 *slot = XOBNEW (&objfile->objfile_obstack,
23743 struct dwarf2_per_cu_offset_and_type);
23744 **slot = ofs;
23745 return type;
23746 }
23747
23748 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23749 or return NULL if the die does not have a saved type. */
23750
23751 static struct type *
23752 get_die_type_at_offset (sect_offset sect_off,
23753 dwarf2_per_cu_data *per_cu,
23754 dwarf2_per_objfile *per_objfile)
23755 {
23756 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23757
23758 if (per_objfile->die_type_hash == NULL)
23759 return NULL;
23760
23761 ofs.per_cu = per_cu;
23762 ofs.sect_off = sect_off;
23763 slot = ((struct dwarf2_per_cu_offset_and_type *)
23764 htab_find (per_objfile->die_type_hash.get (), &ofs));
23765 if (slot)
23766 return slot->type;
23767 else
23768 return NULL;
23769 }
23770
23771 /* Look up the type for DIE in CU in die_type_hash,
23772 or return NULL if DIE does not have a saved type. */
23773
23774 static struct type *
23775 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23776 {
23777 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23778 }
23779
23780 struct cmd_list_element *set_dwarf_cmdlist;
23781 struct cmd_list_element *show_dwarf_cmdlist;
23782
23783 static void
23784 show_check_physname (struct ui_file *file, int from_tty,
23785 struct cmd_list_element *c, const char *value)
23786 {
23787 gdb_printf (file,
23788 _("Whether to check \"physname\" is %s.\n"),
23789 value);
23790 }
23791
23792 void _initialize_dwarf2_read ();
23793 void
23794 _initialize_dwarf2_read ()
23795 {
23796 add_setshow_prefix_cmd ("dwarf", class_maintenance,
23797 _("\
23798 Set DWARF specific variables.\n\
23799 Configure DWARF variables such as the cache size."),
23800 _("\
23801 Show DWARF specific variables.\n\
23802 Show DWARF variables such as the cache size."),
23803 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
23804 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
23805
23806 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23807 &dwarf_max_cache_age, _("\
23808 Set the upper bound on the age of cached DWARF compilation units."), _("\
23809 Show the upper bound on the age of cached DWARF compilation units."), _("\
23810 A higher limit means that cached compilation units will be stored\n\
23811 in memory longer, and more total memory will be used. Zero disables\n\
23812 caching, which can slow down startup."),
23813 NULL,
23814 show_dwarf_max_cache_age,
23815 &set_dwarf_cmdlist,
23816 &show_dwarf_cmdlist);
23817
23818 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23819 Set debugging of the DWARF reader."), _("\
23820 Show debugging of the DWARF reader."), _("\
23821 When enabled (non-zero), debugging messages are printed during DWARF\n\
23822 reading and symtab expansion. A value of 1 (one) provides basic\n\
23823 information. A value greater than 1 provides more verbose information."),
23824 NULL,
23825 NULL,
23826 &setdebuglist, &showdebuglist);
23827
23828 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23829 Set debugging of the DWARF DIE reader."), _("\
23830 Show debugging of the DWARF DIE reader."), _("\
23831 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23832 The value is the maximum depth to print."),
23833 NULL,
23834 NULL,
23835 &setdebuglist, &showdebuglist);
23836
23837 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23838 Set debugging of the dwarf line reader."), _("\
23839 Show debugging of the dwarf line reader."), _("\
23840 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23841 A value of 1 (one) provides basic information.\n\
23842 A value greater than 1 provides more verbose information."),
23843 NULL,
23844 NULL,
23845 &setdebuglist, &showdebuglist);
23846
23847 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23848 Set cross-checking of \"physname\" code against demangler."), _("\
23849 Show cross-checking of \"physname\" code against demangler."), _("\
23850 When enabled, GDB's internal \"physname\" code is checked against\n\
23851 the demangler."),
23852 NULL, show_check_physname,
23853 &setdebuglist, &showdebuglist);
23854
23855 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23856 no_class, &use_deprecated_index_sections, _("\
23857 Set whether to use deprecated gdb_index sections."), _("\
23858 Show whether to use deprecated gdb_index sections."), _("\
23859 When enabled, deprecated .gdb_index sections are used anyway.\n\
23860 Normally they are ignored either because of a missing feature or\n\
23861 performance issue.\n\
23862 Warning: This option must be enabled before gdb reads the file."),
23863 NULL,
23864 NULL,
23865 &setlist, &showlist);
23866
23867 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23868 &dwarf2_locexpr_funcs);
23869 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23870 &dwarf2_loclist_funcs);
23871
23872 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23873 &dwarf2_block_frame_base_locexpr_funcs);
23874 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23875 &dwarf2_block_frame_base_loclist_funcs);
23876
23877 #if GDB_SELF_TEST
23878 selftests::register_test ("dw2_expand_symtabs_matching",
23879 selftests::dw2_expand_symtabs_matching::run_test);
23880 selftests::register_test ("dwarf2_find_containing_comp_unit",
23881 selftests::find_containing_comp_unit::run_test);
23882 #endif
23883 }