]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
76dbeb8d536a139325c68b37ace4927722eac67c
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2023 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/aranges.h"
35 #include "dwarf2/attribute.h"
36 #include "dwarf2/comp-unit-head.h"
37 #include "dwarf2/cu.h"
38 #include "dwarf2/index-cache.h"
39 #include "dwarf2/index-common.h"
40 #include "dwarf2/leb.h"
41 #include "dwarf2/line-header.h"
42 #include "dwarf2/dwz.h"
43 #include "dwarf2/macro.h"
44 #include "dwarf2/die.h"
45 #include "dwarf2/read-debug-names.h"
46 #include "dwarf2/read-gdb-index.h"
47 #include "dwarf2/sect-names.h"
48 #include "dwarf2/stringify.h"
49 #include "dwarf2/public.h"
50 #include "bfd.h"
51 #include "elf-bfd.h"
52 #include "symtab.h"
53 #include "gdbtypes.h"
54 #include "objfiles.h"
55 #include "dwarf2.h"
56 #include "demangle.h"
57 #include "gdb-demangle.h"
58 #include "filenames.h"
59 #include "language.h"
60 #include "complaints.h"
61 #include "dwarf2/expr.h"
62 #include "dwarf2/loc.h"
63 #include "cp-support.h"
64 #include "hashtab.h"
65 #include "command.h"
66 #include "gdbcmd.h"
67 #include "block.h"
68 #include "addrmap.h"
69 #include "typeprint.h"
70 #include "c-lang.h"
71 #include "go-lang.h"
72 #include "valprint.h"
73 #include "gdbcore.h"
74 #include "gdb/gdb-index.h"
75 #include "gdb_bfd.h"
76 #include "f-lang.h"
77 #include "source.h"
78 #include "build-id.h"
79 #include "namespace.h"
80 #include "gdbsupport/function-view.h"
81 #include <optional>
82 #include "gdbsupport/underlying.h"
83 #include "gdbsupport/hash_enum.h"
84 #include "filename-seen-cache.h"
85 #include "producer.h"
86 #include <fcntl.h>
87 #include <algorithm>
88 #include <unordered_map>
89 #include "gdbsupport/selftest.h"
90 #include "rust-lang.h"
91 #include "gdbsupport/pathstuff.h"
92 #include "count-one-bits.h"
93 #include <unordered_set>
94 #include "dwarf2/abbrev-cache.h"
95 #include "cooked-index.h"
96 #include "split-name.h"
97 #include "gdbsupport/thread-pool.h"
98 #include "run-on-main-thread.h"
99
100 /* When == 1, print basic high level tracing messages.
101 When > 1, be more verbose.
102 This is in contrast to the low level DIE reading of dwarf_die_debug. */
103 static unsigned int dwarf_read_debug = 0;
104
105 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
106
107 #define dwarf_read_debug_printf(fmt, ...) \
108 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
109 ##__VA_ARGS__)
110
111 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
112
113 #define dwarf_read_debug_printf_v(fmt, ...) \
114 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
115 ##__VA_ARGS__)
116
117 /* When non-zero, dump DIEs after they are read in. */
118 static unsigned int dwarf_die_debug = 0;
119
120 /* When non-zero, dump line number entries as they are read in. */
121 unsigned int dwarf_line_debug = 0;
122
123 /* When true, cross-check physname against demangler. */
124 static bool check_physname = false;
125
126 /* This is used to store the data that is always per objfile. */
127 static const registry<objfile>::key<dwarf2_per_objfile>
128 dwarf2_objfile_data_key;
129
130 /* These are used to store the dwarf2_per_bfd objects.
131
132 objfiles having the same BFD, which doesn't require relocations, are going to
133 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
134
135 Other objfiles are not going to share a dwarf2_per_bfd with any other
136 objfiles, so they'll have their own version kept in the _objfile_data_key
137 version. */
138 static const registry<bfd>::key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
139 static const registry<objfile>::key<dwarf2_per_bfd>
140 dwarf2_per_bfd_objfile_data_key;
141
142 /* The "aclass" indices for various kinds of computed DWARF symbols. */
143
144 static int dwarf2_locexpr_index;
145 static int dwarf2_loclist_index;
146 static int ada_imported_index;
147 static int dwarf2_locexpr_block_index;
148 static int dwarf2_loclist_block_index;
149 static int ada_block_index;
150
151 static bool producer_is_gas_lt_2_38 (struct dwarf2_cu *cu);
152
153 /* Size of .debug_loclists section header for 32-bit DWARF format. */
154 #define LOCLIST_HEADER_SIZE32 12
155
156 /* Size of .debug_loclists section header for 64-bit DWARF format. */
157 #define LOCLIST_HEADER_SIZE64 20
158
159 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
160 #define RNGLIST_HEADER_SIZE32 12
161
162 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
163 #define RNGLIST_HEADER_SIZE64 20
164
165 /* See dwarf2/read.h. */
166
167 dwarf2_per_objfile *
168 get_dwarf2_per_objfile (struct objfile *objfile)
169 {
170 return dwarf2_objfile_data_key.get (objfile);
171 }
172
173 /* Default names of the debugging sections. */
174
175 /* Note that if the debugging section has been compressed, it might
176 have a name like .zdebug_info. */
177
178 const struct dwarf2_debug_sections dwarf2_elf_names =
179 {
180 { ".debug_info", ".zdebug_info" },
181 { ".debug_abbrev", ".zdebug_abbrev" },
182 { ".debug_line", ".zdebug_line" },
183 { ".debug_loc", ".zdebug_loc" },
184 { ".debug_loclists", ".zdebug_loclists" },
185 { ".debug_macinfo", ".zdebug_macinfo" },
186 { ".debug_macro", ".zdebug_macro" },
187 { ".debug_str", ".zdebug_str" },
188 { ".debug_str_offsets", ".zdebug_str_offsets" },
189 { ".debug_line_str", ".zdebug_line_str" },
190 { ".debug_ranges", ".zdebug_ranges" },
191 { ".debug_rnglists", ".zdebug_rnglists" },
192 { ".debug_types", ".zdebug_types" },
193 { ".debug_addr", ".zdebug_addr" },
194 { ".debug_frame", ".zdebug_frame" },
195 { ".eh_frame", NULL },
196 { ".gdb_index", ".zgdb_index" },
197 { ".debug_names", ".zdebug_names" },
198 { ".debug_aranges", ".zdebug_aranges" },
199 23
200 };
201
202 /* List of DWO/DWP sections. */
203
204 static const struct dwop_section_names
205 {
206 struct dwarf2_section_names abbrev_dwo;
207 struct dwarf2_section_names info_dwo;
208 struct dwarf2_section_names line_dwo;
209 struct dwarf2_section_names loc_dwo;
210 struct dwarf2_section_names loclists_dwo;
211 struct dwarf2_section_names macinfo_dwo;
212 struct dwarf2_section_names macro_dwo;
213 struct dwarf2_section_names rnglists_dwo;
214 struct dwarf2_section_names str_dwo;
215 struct dwarf2_section_names str_offsets_dwo;
216 struct dwarf2_section_names types_dwo;
217 struct dwarf2_section_names cu_index;
218 struct dwarf2_section_names tu_index;
219 }
220 dwop_section_names =
221 {
222 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
223 { ".debug_info.dwo", ".zdebug_info.dwo" },
224 { ".debug_line.dwo", ".zdebug_line.dwo" },
225 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
226 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
227 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
228 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
229 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
230 { ".debug_str.dwo", ".zdebug_str.dwo" },
231 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
232 { ".debug_types.dwo", ".zdebug_types.dwo" },
233 { ".debug_cu_index", ".zdebug_cu_index" },
234 { ".debug_tu_index", ".zdebug_tu_index" },
235 };
236
237 /* local data types */
238
239 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
240 begin with a header, which contains the following information. */
241 struct loclists_rnglists_header
242 {
243 /* A 4-byte or 12-byte length containing the length of the
244 set of entries for this compilation unit, not including the
245 length field itself. */
246 unsigned int length;
247
248 /* A 2-byte version identifier. */
249 short version;
250
251 /* A 1-byte unsigned integer containing the size in bytes of an address on
252 the target system. */
253 unsigned char addr_size;
254
255 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
256 on the target system. */
257 unsigned char segment_collector_size;
258
259 /* A 4-byte count of the number of offsets that follow the header. */
260 unsigned int offset_entry_count;
261 };
262
263 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
264 This includes type_unit_group and quick_file_names. */
265
266 struct stmt_list_hash
267 {
268 /* The DWO unit this table is from or NULL if there is none. */
269 struct dwo_unit *dwo_unit;
270
271 /* Offset in .debug_line or .debug_line.dwo. */
272 sect_offset line_sect_off;
273 };
274
275 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
276 an object of this type. This contains elements of type unit groups
277 that can be shared across objfiles. The non-shareable parts are in
278 type_unit_group_unshareable. */
279
280 struct type_unit_group
281 {
282 /* The data used to construct the hash key. */
283 struct stmt_list_hash hash {};
284 };
285
286 /* These sections are what may appear in a (real or virtual) DWO file. */
287
288 struct dwo_sections
289 {
290 struct dwarf2_section_info abbrev;
291 struct dwarf2_section_info line;
292 struct dwarf2_section_info loc;
293 struct dwarf2_section_info loclists;
294 struct dwarf2_section_info macinfo;
295 struct dwarf2_section_info macro;
296 struct dwarf2_section_info rnglists;
297 struct dwarf2_section_info str;
298 struct dwarf2_section_info str_offsets;
299 /* In the case of a virtual DWO file, these two are unused. */
300 struct dwarf2_section_info info;
301 std::vector<dwarf2_section_info> types;
302 };
303
304 /* CUs/TUs in DWP/DWO files. */
305
306 struct dwo_unit
307 {
308 /* Backlink to the containing struct dwo_file. */
309 struct dwo_file *dwo_file;
310
311 /* The "id" that distinguishes this CU/TU.
312 .debug_info calls this "dwo_id", .debug_types calls this "signature".
313 Since signatures came first, we stick with it for consistency. */
314 ULONGEST signature;
315
316 /* The section this CU/TU lives in, in the DWO file. */
317 struct dwarf2_section_info *section;
318
319 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
320 sect_offset sect_off;
321 unsigned int length;
322
323 /* For types, offset in the type's DIE of the type defined by this TU. */
324 cu_offset type_offset_in_tu;
325 };
326
327 /* include/dwarf2.h defines the DWP section codes.
328 It defines a max value but it doesn't define a min value, which we
329 use for error checking, so provide one. */
330
331 enum dwp_v2_section_ids
332 {
333 DW_SECT_MIN = 1
334 };
335
336 /* Data for one DWO file.
337
338 This includes virtual DWO files (a virtual DWO file is a DWO file as it
339 appears in a DWP file). DWP files don't really have DWO files per se -
340 comdat folding of types "loses" the DWO file they came from, and from
341 a high level view DWP files appear to contain a mass of random types.
342 However, to maintain consistency with the non-DWP case we pretend DWP
343 files contain virtual DWO files, and we assign each TU with one virtual
344 DWO file (generally based on the line and abbrev section offsets -
345 a heuristic that seems to work in practice). */
346
347 struct dwo_file
348 {
349 dwo_file () = default;
350 DISABLE_COPY_AND_ASSIGN (dwo_file);
351
352 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
353 For virtual DWO files the name is constructed from the section offsets
354 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
355 from related CU+TUs. */
356 std::string dwo_name;
357
358 /* The DW_AT_comp_dir attribute. */
359 const char *comp_dir = nullptr;
360
361 /* The bfd, when the file is open. Otherwise this is NULL.
362 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
363 gdb_bfd_ref_ptr dbfd;
364
365 /* The sections that make up this DWO file.
366 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
367 sections (for lack of a better name). */
368 struct dwo_sections sections {};
369
370 /* The CUs in the file.
371 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
372 an extension to handle LLVM's Link Time Optimization output (where
373 multiple source files may be compiled into a single object/dwo pair). */
374 htab_up cus;
375
376 /* Table of TUs in the file.
377 Each element is a struct dwo_unit. */
378 htab_up tus;
379 };
380
381 /* These sections are what may appear in a DWP file. */
382
383 struct dwp_sections
384 {
385 /* These are used by all DWP versions (1, 2 and 5). */
386 struct dwarf2_section_info str;
387 struct dwarf2_section_info cu_index;
388 struct dwarf2_section_info tu_index;
389
390 /* These are only used by DWP version 2 and version 5 files.
391 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
392 sections are referenced by section number, and are not recorded here.
393 In DWP version 2 or 5 there is at most one copy of all these sections,
394 each section being (effectively) comprised of the concatenation of all of
395 the individual sections that exist in the version 1 format.
396 To keep the code simple we treat each of these concatenated pieces as a
397 section itself (a virtual section?). */
398 struct dwarf2_section_info abbrev;
399 struct dwarf2_section_info info;
400 struct dwarf2_section_info line;
401 struct dwarf2_section_info loc;
402 struct dwarf2_section_info loclists;
403 struct dwarf2_section_info macinfo;
404 struct dwarf2_section_info macro;
405 struct dwarf2_section_info rnglists;
406 struct dwarf2_section_info str_offsets;
407 struct dwarf2_section_info types;
408 };
409
410 /* These sections are what may appear in a virtual DWO file in DWP version 1.
411 A virtual DWO file is a DWO file as it appears in a DWP file. */
412
413 struct virtual_v1_dwo_sections
414 {
415 struct dwarf2_section_info abbrev;
416 struct dwarf2_section_info line;
417 struct dwarf2_section_info loc;
418 struct dwarf2_section_info macinfo;
419 struct dwarf2_section_info macro;
420 struct dwarf2_section_info str_offsets;
421 /* Each DWP hash table entry records one CU or one TU.
422 That is recorded here, and copied to dwo_unit.section. */
423 struct dwarf2_section_info info_or_types;
424 };
425
426 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
427 In version 2, the sections of the DWO files are concatenated together
428 and stored in one section of that name. Thus each ELF section contains
429 several "virtual" sections. */
430
431 struct virtual_v2_or_v5_dwo_sections
432 {
433 bfd_size_type abbrev_offset;
434 bfd_size_type abbrev_size;
435
436 bfd_size_type line_offset;
437 bfd_size_type line_size;
438
439 bfd_size_type loc_offset;
440 bfd_size_type loc_size;
441
442 bfd_size_type loclists_offset;
443 bfd_size_type loclists_size;
444
445 bfd_size_type macinfo_offset;
446 bfd_size_type macinfo_size;
447
448 bfd_size_type macro_offset;
449 bfd_size_type macro_size;
450
451 bfd_size_type rnglists_offset;
452 bfd_size_type rnglists_size;
453
454 bfd_size_type str_offsets_offset;
455 bfd_size_type str_offsets_size;
456
457 /* Each DWP hash table entry records one CU or one TU.
458 That is recorded here, and copied to dwo_unit.section. */
459 bfd_size_type info_or_types_offset;
460 bfd_size_type info_or_types_size;
461 };
462
463 /* Contents of DWP hash tables. */
464
465 struct dwp_hash_table
466 {
467 uint32_t version, nr_columns;
468 uint32_t nr_units, nr_slots;
469 const gdb_byte *hash_table, *unit_table;
470 union
471 {
472 struct
473 {
474 const gdb_byte *indices;
475 } v1;
476 struct
477 {
478 /* This is indexed by column number and gives the id of the section
479 in that column. */
480 #define MAX_NR_V2_DWO_SECTIONS \
481 (1 /* .debug_info or .debug_types */ \
482 + 1 /* .debug_abbrev */ \
483 + 1 /* .debug_line */ \
484 + 1 /* .debug_loc */ \
485 + 1 /* .debug_str_offsets */ \
486 + 1 /* .debug_macro or .debug_macinfo */)
487 int section_ids[MAX_NR_V2_DWO_SECTIONS];
488 const gdb_byte *offsets;
489 const gdb_byte *sizes;
490 } v2;
491 struct
492 {
493 /* This is indexed by column number and gives the id of the section
494 in that column. */
495 #define MAX_NR_V5_DWO_SECTIONS \
496 (1 /* .debug_info */ \
497 + 1 /* .debug_abbrev */ \
498 + 1 /* .debug_line */ \
499 + 1 /* .debug_loclists */ \
500 + 1 /* .debug_str_offsets */ \
501 + 1 /* .debug_macro */ \
502 + 1 /* .debug_rnglists */)
503 int section_ids[MAX_NR_V5_DWO_SECTIONS];
504 const gdb_byte *offsets;
505 const gdb_byte *sizes;
506 } v5;
507 } section_pool;
508 };
509
510 /* Data for one DWP file. */
511
512 struct dwp_file
513 {
514 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
515 : name (name_),
516 dbfd (std::move (abfd))
517 {
518 }
519
520 /* Name of the file. */
521 const char *name;
522
523 /* File format version. */
524 int version = 0;
525
526 /* The bfd. */
527 gdb_bfd_ref_ptr dbfd;
528
529 /* Section info for this file. */
530 struct dwp_sections sections {};
531
532 /* Table of CUs in the file. */
533 const struct dwp_hash_table *cus = nullptr;
534
535 /* Table of TUs in the file. */
536 const struct dwp_hash_table *tus = nullptr;
537
538 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
539 htab_up loaded_cus;
540 htab_up loaded_tus;
541
542 /* Table to map ELF section numbers to their sections.
543 This is only needed for the DWP V1 file format. */
544 unsigned int num_sections = 0;
545 asection **elf_sections = nullptr;
546 };
547
548 /* Struct used to pass misc. parameters to read_die_and_children, et
549 al. which are used for both .debug_info and .debug_types dies.
550 All parameters here are unchanging for the life of the call. This
551 struct exists to abstract away the constant parameters of die reading. */
552
553 struct die_reader_specs
554 {
555 /* The bfd of die_section. */
556 bfd *abfd;
557
558 /* The CU of the DIE we are parsing. */
559 struct dwarf2_cu *cu;
560
561 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
562 struct dwo_file *dwo_file;
563
564 /* The section the die comes from.
565 This is either .debug_info or .debug_types, or the .dwo variants. */
566 struct dwarf2_section_info *die_section;
567
568 /* die_section->buffer. */
569 const gdb_byte *buffer;
570
571 /* The end of the buffer. */
572 const gdb_byte *buffer_end;
573
574 /* The abbreviation table to use when reading the DIEs. */
575 struct abbrev_table *abbrev_table;
576 };
577
578 /* A subclass of die_reader_specs that holds storage and has complex
579 constructor and destructor behavior. */
580
581 class cutu_reader : public die_reader_specs
582 {
583 public:
584
585 cutu_reader (dwarf2_per_cu_data *this_cu,
586 dwarf2_per_objfile *per_objfile,
587 struct abbrev_table *abbrev_table,
588 dwarf2_cu *existing_cu,
589 bool skip_partial,
590 abbrev_cache *cache = nullptr);
591
592 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
593 dwarf2_per_objfile *per_objfile,
594 struct dwarf2_cu *parent_cu = nullptr,
595 struct dwo_file *dwo_file = nullptr);
596
597 DISABLE_COPY_AND_ASSIGN (cutu_reader);
598
599 cutu_reader (cutu_reader &&) = default;
600
601 const gdb_byte *info_ptr = nullptr;
602 struct die_info *comp_unit_die = nullptr;
603 bool dummy_p = false;
604
605 /* Release the new CU, putting it on the chain. This cannot be done
606 for dummy CUs. */
607 void keep ();
608
609 /* Release the abbrev table, transferring ownership to the
610 caller. */
611 abbrev_table_up release_abbrev_table ()
612 {
613 return std::move (m_abbrev_table_holder);
614 }
615
616 private:
617 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
618 dwarf2_per_objfile *per_objfile,
619 dwarf2_cu *existing_cu);
620
621 struct dwarf2_per_cu_data *m_this_cu;
622 std::unique_ptr<dwarf2_cu> m_new_cu;
623
624 /* The ordinary abbreviation table. */
625 abbrev_table_up m_abbrev_table_holder;
626
627 /* The DWO abbreviation table. */
628 abbrev_table_up m_dwo_abbrev_table;
629 };
630
631 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
632 but this would require a corresponding change in unpack_field_as_long
633 and friends. */
634 static int bits_per_byte = 8;
635
636 struct variant_part_builder;
637
638 /* When reading a variant, we track a bit more information about the
639 field, and store it in an object of this type. */
640
641 struct variant_field
642 {
643 int first_field = -1;
644 int last_field = -1;
645
646 /* A variant can contain other variant parts. */
647 std::vector<variant_part_builder> variant_parts;
648
649 /* If we see a DW_TAG_variant, then this will be set if this is the
650 default branch. */
651 bool default_branch = false;
652 /* If we see a DW_AT_discr_value, then this will be the discriminant
653 value. */
654 ULONGEST discriminant_value = 0;
655 /* If we see a DW_AT_discr_list, then this is a pointer to the list
656 data. */
657 struct dwarf_block *discr_list_data = nullptr;
658 };
659
660 /* This represents a DW_TAG_variant_part. */
661
662 struct variant_part_builder
663 {
664 /* The offset of the discriminant field. */
665 sect_offset discriminant_offset {};
666
667 /* Variants that are direct children of this variant part. */
668 std::vector<variant_field> variants;
669
670 /* True if we're currently reading a variant. */
671 bool processing_variant = false;
672 };
673
674 struct nextfield
675 {
676 /* Variant parts need to find the discriminant, which is a DIE
677 reference. We track the section offset of each field to make
678 this link. */
679 sect_offset offset;
680 struct field field {};
681 };
682
683 struct fnfieldlist
684 {
685 const char *name = nullptr;
686 std::vector<struct fn_field> fnfields;
687 };
688
689 /* The routines that read and process dies for a C struct or C++ class
690 pass lists of data member fields and lists of member function fields
691 in an instance of a field_info structure, as defined below. */
692 struct field_info
693 {
694 /* List of data member and baseclasses fields. */
695 std::vector<struct nextfield> fields;
696 std::vector<struct nextfield> baseclasses;
697
698 /* Member function fieldlist array, contains name of possibly overloaded
699 member function, number of overloaded member functions and a pointer
700 to the head of the member function field chain. */
701 std::vector<struct fnfieldlist> fnfieldlists;
702
703 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
704 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
705 std::vector<struct decl_field> typedef_field_list;
706
707 /* Nested types defined by this class and the number of elements in this
708 list. */
709 std::vector<struct decl_field> nested_types_list;
710
711 /* If non-null, this is the variant part we are currently
712 reading. */
713 variant_part_builder *current_variant_part = nullptr;
714 /* This holds all the top-level variant parts attached to the type
715 we're reading. */
716 std::vector<variant_part_builder> variant_parts;
717
718 /* Return the total number of fields (including baseclasses). */
719 int nfields () const
720 {
721 return fields.size () + baseclasses.size ();
722 }
723 };
724
725 /* Loaded secondary compilation units are kept in memory until they
726 have not been referenced for the processing of this many
727 compilation units. Set this to zero to disable caching. Cache
728 sizes of up to at least twenty will improve startup time for
729 typical inter-CU-reference binaries, at an obvious memory cost. */
730 static int dwarf_max_cache_age = 5;
731 static void
732 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
733 struct cmd_list_element *c, const char *value)
734 {
735 gdb_printf (file, _("The upper bound on the age of cached "
736 "DWARF compilation units is %s.\n"),
737 value);
738 }
739
740 /* When true, wait for DWARF reading to be complete. */
741 static bool dwarf_synchronous = false;
742
743 /* "Show" callback for "maint set dwarf synchronous". */
744 static void
745 show_dwarf_synchronous (struct ui_file *file, int from_tty,
746 struct cmd_list_element *c, const char *value)
747 {
748 gdb_printf (file, _("Whether DWARF reading is synchronous is %s.\n"),
749 value);
750 }
751 \f
752 /* local function prototypes */
753
754 static void dwarf2_find_base_address (struct die_info *die,
755 struct dwarf2_cu *cu);
756
757 static void build_type_psymtabs_reader (cutu_reader *reader,
758 cooked_index_storage *storage);
759
760 static void var_decode_location (struct attribute *attr,
761 struct symbol *sym,
762 struct dwarf2_cu *cu);
763
764 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
765
766 static const gdb_byte *read_attribute (const struct die_reader_specs *,
767 struct attribute *,
768 const struct attr_abbrev *,
769 const gdb_byte *,
770 bool allow_reprocess = true);
771
772 /* Note that the default for TAG is chosen because it only matters
773 when reading the top-level DIE, and that function is careful to
774 pass the correct tag. */
775 static void read_attribute_reprocess (const struct die_reader_specs *reader,
776 struct attribute *attr,
777 dwarf_tag tag = DW_TAG_padding);
778
779 static unrelocated_addr read_addr_index (struct dwarf2_cu *cu,
780 unsigned int addr_index);
781
782 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
783 dwarf2_section_info *, sect_offset);
784
785 static const char *read_indirect_string
786 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
787 const struct comp_unit_head *, unsigned int *);
788
789 static unrelocated_addr read_addr_index_from_leb128 (struct dwarf2_cu *,
790 const gdb_byte *,
791 unsigned int *);
792
793 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
794 ULONGEST str_index);
795
796 static const char *read_stub_str_index (struct dwarf2_cu *cu,
797 ULONGEST str_index);
798
799 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
800 struct dwarf2_cu *);
801
802 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
803 struct dwarf2_cu *cu);
804
805 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
806
807 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
808 struct dwarf2_cu *cu);
809
810 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
811
812 static struct die_info *die_specification (struct die_info *die,
813 struct dwarf2_cu **);
814
815 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
816 struct dwarf2_cu *cu,
817 const char *comp_dir);
818
819 static void dwarf_decode_lines (struct line_header *,
820 struct dwarf2_cu *,
821 unrelocated_addr, int decode_mapping);
822
823 static void dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
824 const line_header &lh);
825
826 static struct symbol *new_symbol (struct die_info *, struct type *,
827 struct dwarf2_cu *, struct symbol * = NULL);
828
829 static void dwarf2_const_value (const struct attribute *, struct symbol *,
830 struct dwarf2_cu *);
831
832 static void dwarf2_const_value_attr (const struct attribute *attr,
833 struct type *type,
834 const char *name,
835 struct obstack *obstack,
836 struct dwarf2_cu *cu, LONGEST *value,
837 const gdb_byte **bytes,
838 struct dwarf2_locexpr_baton **baton);
839
840 static struct type *read_subrange_index_type (struct die_info *die,
841 struct dwarf2_cu *cu);
842
843 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
844
845 static int need_gnat_info (struct dwarf2_cu *);
846
847 static struct type *die_descriptive_type (struct die_info *,
848 struct dwarf2_cu *);
849
850 static void set_descriptive_type (struct type *, struct die_info *,
851 struct dwarf2_cu *);
852
853 static struct type *die_containing_type (struct die_info *,
854 struct dwarf2_cu *);
855
856 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
857 struct dwarf2_cu *);
858
859 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
860
861 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
862
863 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
864
865 static char *typename_concat (struct obstack *obs, const char *prefix,
866 const char *suffix, int physname,
867 struct dwarf2_cu *cu);
868
869 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
870
871 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
872
873 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
874
875 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
876
877 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
878
879 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
880
881 /* Return the .debug_loclists section to use for cu. */
882 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
883
884 /* Return the .debug_rnglists section to use for cu. */
885 static struct dwarf2_section_info *cu_debug_rnglists_section
886 (struct dwarf2_cu *cu, dwarf_tag tag);
887
888 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
889 values. Keep the items ordered with increasing constraints compliance. */
890 enum pc_bounds_kind
891 {
892 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
893 PC_BOUNDS_NOT_PRESENT,
894
895 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
896 were present but they do not form a valid range of PC addresses. */
897 PC_BOUNDS_INVALID,
898
899 /* Discontiguous range was found - that is DW_AT_ranges was found. */
900 PC_BOUNDS_RANGES,
901
902 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
903 PC_BOUNDS_HIGH_LOW,
904 };
905
906 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
907 unrelocated_addr *,
908 unrelocated_addr *,
909 struct dwarf2_cu *,
910 addrmap *,
911 void *);
912
913 static void get_scope_pc_bounds (struct die_info *,
914 unrelocated_addr *, unrelocated_addr *,
915 struct dwarf2_cu *);
916
917 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
918 struct dwarf2_cu *);
919
920 static void dwarf2_add_field (struct field_info *, struct die_info *,
921 struct dwarf2_cu *);
922
923 static void dwarf2_attach_fields_to_type (struct field_info *,
924 struct type *, struct dwarf2_cu *);
925
926 static void dwarf2_add_member_fn (struct field_info *,
927 struct die_info *, struct type *,
928 struct dwarf2_cu *);
929
930 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
931 struct type *,
932 struct dwarf2_cu *);
933
934 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
935
936 static void read_common_block (struct die_info *, struct dwarf2_cu *);
937
938 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
939
940 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
941
942 static struct using_direct **using_directives (struct dwarf2_cu *cu);
943
944 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
945
946 static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
947
948 static struct type *read_module_type (struct die_info *die,
949 struct dwarf2_cu *cu);
950
951 static const char *namespace_name (struct die_info *die,
952 int *is_anonymous, struct dwarf2_cu *);
953
954 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
955
956 static bool decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
957 CORE_ADDR *addr);
958
959 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
960 struct dwarf2_cu *);
961
962 static struct die_info *read_die_and_siblings_1
963 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
964 struct die_info *);
965
966 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
967 const gdb_byte *info_ptr,
968 const gdb_byte **new_info_ptr,
969 struct die_info *parent);
970
971 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
972 struct die_info **, const gdb_byte *,
973 int, bool);
974
975 static const gdb_byte *read_toplevel_die (const struct die_reader_specs *,
976 struct die_info **,
977 const gdb_byte *,
978 gdb::array_view<attribute *> = {});
979
980 static void process_die (struct die_info *, struct dwarf2_cu *);
981
982 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
983 struct objfile *);
984
985 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
986
987 static const char *dwarf2_full_name (const char *name,
988 struct die_info *die,
989 struct dwarf2_cu *cu);
990
991 static const char *dwarf2_physname (const char *name, struct die_info *die,
992 struct dwarf2_cu *cu);
993
994 static struct die_info *dwarf2_extension (struct die_info *die,
995 struct dwarf2_cu **);
996
997 static void store_in_ref_table (struct die_info *,
998 struct dwarf2_cu *);
999
1000 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1001 const struct attribute *,
1002 struct dwarf2_cu **);
1003
1004 static struct die_info *follow_die_ref (struct die_info *,
1005 const struct attribute *,
1006 struct dwarf2_cu **);
1007
1008 static struct die_info *follow_die_sig (struct die_info *,
1009 const struct attribute *,
1010 struct dwarf2_cu **);
1011
1012 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1013 struct dwarf2_cu *);
1014
1015 static struct type *get_DW_AT_signature_type (struct die_info *,
1016 const struct attribute *,
1017 struct dwarf2_cu *);
1018
1019 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1020 dwarf2_per_objfile *per_objfile);
1021
1022 static void read_signatured_type (signatured_type *sig_type,
1023 dwarf2_per_objfile *per_objfile);
1024
1025 static int attr_to_dynamic_prop (const struct attribute *attr,
1026 struct die_info *die, struct dwarf2_cu *cu,
1027 struct dynamic_prop *prop, struct type *type);
1028
1029 /* memory allocation interface */
1030
1031 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1032
1033 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1034
1035 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1036 struct dwarf2_loclist_baton *baton,
1037 const struct attribute *attr);
1038
1039 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1040 struct symbol *sym,
1041 struct dwarf2_cu *cu,
1042 int is_block);
1043
1044 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1045 const gdb_byte *info_ptr,
1046 const struct abbrev_info *abbrev,
1047 bool do_skip_children = true);
1048
1049 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1050 (sect_offset sect_off, unsigned int offset_in_dwz,
1051 dwarf2_per_bfd *per_bfd);
1052
1053 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1054 struct die_info *comp_unit_die,
1055 enum language pretend_language);
1056
1057 static struct type *set_die_type (struct die_info *, struct type *,
1058 struct dwarf2_cu *, bool = false);
1059
1060 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1061 dwarf2_per_objfile *per_objfile,
1062 dwarf2_cu *existing_cu,
1063 bool skip_partial,
1064 enum language pretend_language);
1065
1066 static void process_full_comp_unit (dwarf2_cu *cu,
1067 enum language pretend_language);
1068
1069 static void process_full_type_unit (dwarf2_cu *cu,
1070 enum language pretend_language);
1071
1072 static struct type *get_die_type_at_offset (sect_offset,
1073 dwarf2_per_cu_data *per_cu,
1074 dwarf2_per_objfile *per_objfile);
1075
1076 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1077
1078 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1079 dwarf2_per_objfile *per_objfile,
1080 enum language pretend_language);
1081
1082 static void process_queue (dwarf2_per_objfile *per_objfile);
1083
1084 static bool is_ada_import_or_export (dwarf2_cu *cu, const char *name,
1085 const char *linkagename);
1086
1087 /* Class, the destructor of which frees all allocated queue entries. This
1088 will only have work to do if an error was thrown while processing the
1089 dwarf. If no error was thrown then the queue entries should have all
1090 been processed, and freed, as we went along. */
1091
1092 class dwarf2_queue_guard
1093 {
1094 public:
1095 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1096 : m_per_objfile (per_objfile)
1097 {
1098 gdb_assert (!m_per_objfile->queue.has_value ());
1099
1100 m_per_objfile->queue.emplace ();
1101 }
1102
1103 /* Free any entries remaining on the queue. There should only be
1104 entries left if we hit an error while processing the dwarf. */
1105 ~dwarf2_queue_guard ()
1106 {
1107 gdb_assert (m_per_objfile->queue.has_value ());
1108
1109 m_per_objfile->queue.reset ();
1110 }
1111
1112 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1113
1114 private:
1115 dwarf2_per_objfile *m_per_objfile;
1116 };
1117
1118 dwarf2_queue_item::~dwarf2_queue_item ()
1119 {
1120 /* Anything still marked queued is likely to be in an
1121 inconsistent state, so discard it. */
1122 if (per_cu->queued)
1123 {
1124 per_objfile->remove_cu (per_cu);
1125 per_cu->queued = 0;
1126 }
1127 }
1128
1129 /* See dwarf2/read.h. */
1130
1131 void
1132 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1133 {
1134 if (data->is_debug_types)
1135 delete static_cast<signatured_type *> (data);
1136 else
1137 delete data;
1138 }
1139
1140 static file_and_directory &find_file_and_directory
1141 (struct die_info *die, struct dwarf2_cu *cu);
1142
1143 static const char *compute_include_file_name
1144 (const struct line_header *lh,
1145 const file_entry &fe,
1146 const file_and_directory &cu_info,
1147 std::string &name_holder);
1148
1149 static htab_up allocate_dwo_unit_table ();
1150
1151 static struct dwo_unit *lookup_dwo_unit_in_dwp
1152 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1153 const char *comp_dir, ULONGEST signature, int is_debug_types);
1154
1155 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1156
1157 static struct dwo_unit *lookup_dwo_comp_unit
1158 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1159 ULONGEST signature);
1160
1161 static struct dwo_unit *lookup_dwo_type_unit
1162 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1163
1164 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1165
1166 /* A unique pointer to a dwo_file. */
1167
1168 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1169
1170 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1171
1172 static void check_producer (struct dwarf2_cu *cu);
1173 \f
1174 /* Various complaints about symbol reading that don't abort the process. */
1175
1176 static void
1177 dwarf2_debug_line_missing_file_complaint (void)
1178 {
1179 complaint (_(".debug_line section has line data without a file"));
1180 }
1181
1182 static void
1183 dwarf2_debug_line_missing_end_sequence_complaint (void)
1184 {
1185 complaint (_(".debug_line section has line "
1186 "program sequence without an end"));
1187 }
1188
1189 static void
1190 dwarf2_complex_location_expr_complaint (void)
1191 {
1192 complaint (_("location expression too complex"));
1193 }
1194
1195 static void
1196 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1197 int arg3)
1198 {
1199 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1200 arg1, arg2, arg3);
1201 }
1202
1203 static void
1204 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1205 {
1206 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1207 arg1, arg2);
1208 }
1209
1210 /* See read.h. */
1211
1212 unrelocated_addr
1213 dwarf2_per_objfile::adjust (unrelocated_addr addr)
1214 {
1215 CORE_ADDR baseaddr = objfile->text_section_offset ();
1216 CORE_ADDR tem = (CORE_ADDR) addr + baseaddr;
1217 tem = gdbarch_adjust_dwarf2_addr (objfile->arch (), tem);
1218 return (unrelocated_addr) (tem - baseaddr);
1219 }
1220
1221 /* See read.h. */
1222
1223 CORE_ADDR
1224 dwarf2_per_objfile::relocate (unrelocated_addr addr)
1225 {
1226 CORE_ADDR baseaddr = objfile->text_section_offset ();
1227 CORE_ADDR tem = (CORE_ADDR) addr + baseaddr;
1228 return gdbarch_adjust_dwarf2_addr (objfile->arch (), tem);
1229 }
1230
1231 /* Hash function for line_header_hash. */
1232
1233 static hashval_t
1234 line_header_hash (const struct line_header *ofs)
1235 {
1236 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1237 }
1238
1239 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1240
1241 static hashval_t
1242 line_header_hash_voidp (const void *item)
1243 {
1244 const struct line_header *ofs = (const struct line_header *) item;
1245
1246 return line_header_hash (ofs);
1247 }
1248
1249 /* Equality function for line_header_hash. */
1250
1251 static int
1252 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1253 {
1254 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1255 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1256
1257 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1258 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1259 }
1260
1261 /* See declaration. */
1262
1263 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1264 bool can_copy_)
1265 : obfd (obfd),
1266 can_copy (can_copy_)
1267 {
1268 if (names == NULL)
1269 names = &dwarf2_elf_names;
1270
1271 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1272 locate_sections (obfd, sec, *names);
1273 }
1274
1275 dwarf2_per_bfd::~dwarf2_per_bfd ()
1276 {
1277 /* Data from the per-BFD may be needed when finalizing the cooked
1278 index table, so wait here while this happens. */
1279 if (index_table != nullptr)
1280 index_table->wait_completely ();
1281
1282 for (auto &per_cu : all_units)
1283 {
1284 per_cu->imported_symtabs_free ();
1285 per_cu->free_cached_file_names ();
1286 }
1287
1288 /* Everything else should be on this->obstack. */
1289 }
1290
1291 /* See read.h. */
1292
1293 void
1294 dwarf2_per_objfile::remove_all_cus ()
1295 {
1296 gdb_assert (!queue.has_value ());
1297
1298 m_dwarf2_cus.clear ();
1299 }
1300
1301 /* A helper class that calls free_cached_comp_units on
1302 destruction. */
1303
1304 class free_cached_comp_units
1305 {
1306 public:
1307
1308 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1309 : m_per_objfile (per_objfile)
1310 {
1311 }
1312
1313 ~free_cached_comp_units ()
1314 {
1315 m_per_objfile->remove_all_cus ();
1316 }
1317
1318 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1319
1320 private:
1321
1322 dwarf2_per_objfile *m_per_objfile;
1323 };
1324
1325 /* See read.h. */
1326
1327 bool
1328 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1329 {
1330 if (per_cu->index < this->m_symtabs.size ())
1331 return this->m_symtabs[per_cu->index] != nullptr;
1332 return false;
1333 }
1334
1335 /* See read.h. */
1336
1337 compunit_symtab *
1338 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1339 {
1340 if (per_cu->index < this->m_symtabs.size ())
1341 return this->m_symtabs[per_cu->index];
1342 return nullptr;
1343 }
1344
1345 /* See read.h. */
1346
1347 void
1348 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1349 compunit_symtab *symtab)
1350 {
1351 if (per_cu->index >= this->m_symtabs.size ())
1352 this->m_symtabs.resize (per_cu->index + 1);
1353 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1354 this->m_symtabs[per_cu->index] = symtab;
1355 }
1356
1357 /* Helper function for dwarf2_initialize_objfile that creates the
1358 per-BFD object. */
1359
1360 static bool
1361 dwarf2_has_info (struct objfile *objfile,
1362 const struct dwarf2_debug_sections *names,
1363 bool can_copy)
1364 {
1365 if (objfile->flags & OBJF_READNEVER)
1366 return false;
1367
1368 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1369
1370 if (per_objfile == NULL)
1371 {
1372 dwarf2_per_bfd *per_bfd;
1373
1374 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1375 BFD doesn't require relocations.
1376
1377 We don't share with objfiles for which -readnow was requested,
1378 because it would complicate things when loading the same BFD with
1379 -readnow and then without -readnow. */
1380 if (!gdb_bfd_requires_relocations (objfile->obfd.get ())
1381 && (objfile->flags & OBJF_READNOW) == 0)
1382 {
1383 /* See if one has been created for this BFD yet. */
1384 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd.get ());
1385
1386 if (per_bfd == nullptr)
1387 {
1388 /* No, create it now. */
1389 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
1390 can_copy);
1391 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
1392 }
1393 }
1394 else
1395 {
1396 /* No sharing possible, create one specifically for this objfile. */
1397 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
1398 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1399 }
1400
1401 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1402 }
1403
1404 return (!per_objfile->per_bfd->info.is_virtual
1405 && per_objfile->per_bfd->info.s.section != NULL
1406 && !per_objfile->per_bfd->abbrev.is_virtual
1407 && per_objfile->per_bfd->abbrev.s.section != NULL);
1408 }
1409
1410 /* See declaration. */
1411
1412 void
1413 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1414 const dwarf2_debug_sections &names)
1415 {
1416 flagword aflag = bfd_section_flags (sectp);
1417
1418 if ((aflag & SEC_HAS_CONTENTS) == 0)
1419 {
1420 }
1421 else if (elf_section_data (sectp)->this_hdr.sh_size
1422 > bfd_get_file_size (abfd))
1423 {
1424 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1425 warning (_("Discarding section %s which has a section size (%s"
1426 ") larger than the file size [in module %s]"),
1427 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1428 bfd_get_filename (abfd));
1429 }
1430 else if (names.info.matches (sectp->name))
1431 {
1432 this->info.s.section = sectp;
1433 this->info.size = bfd_section_size (sectp);
1434 }
1435 else if (names.abbrev.matches (sectp->name))
1436 {
1437 this->abbrev.s.section = sectp;
1438 this->abbrev.size = bfd_section_size (sectp);
1439 }
1440 else if (names.line.matches (sectp->name))
1441 {
1442 this->line.s.section = sectp;
1443 this->line.size = bfd_section_size (sectp);
1444 }
1445 else if (names.loc.matches (sectp->name))
1446 {
1447 this->loc.s.section = sectp;
1448 this->loc.size = bfd_section_size (sectp);
1449 }
1450 else if (names.loclists.matches (sectp->name))
1451 {
1452 this->loclists.s.section = sectp;
1453 this->loclists.size = bfd_section_size (sectp);
1454 }
1455 else if (names.macinfo.matches (sectp->name))
1456 {
1457 this->macinfo.s.section = sectp;
1458 this->macinfo.size = bfd_section_size (sectp);
1459 }
1460 else if (names.macro.matches (sectp->name))
1461 {
1462 this->macro.s.section = sectp;
1463 this->macro.size = bfd_section_size (sectp);
1464 }
1465 else if (names.str.matches (sectp->name))
1466 {
1467 this->str.s.section = sectp;
1468 this->str.size = bfd_section_size (sectp);
1469 }
1470 else if (names.str_offsets.matches (sectp->name))
1471 {
1472 this->str_offsets.s.section = sectp;
1473 this->str_offsets.size = bfd_section_size (sectp);
1474 }
1475 else if (names.line_str.matches (sectp->name))
1476 {
1477 this->line_str.s.section = sectp;
1478 this->line_str.size = bfd_section_size (sectp);
1479 }
1480 else if (names.addr.matches (sectp->name))
1481 {
1482 this->addr.s.section = sectp;
1483 this->addr.size = bfd_section_size (sectp);
1484 }
1485 else if (names.frame.matches (sectp->name))
1486 {
1487 this->frame.s.section = sectp;
1488 this->frame.size = bfd_section_size (sectp);
1489 }
1490 else if (names.eh_frame.matches (sectp->name))
1491 {
1492 this->eh_frame.s.section = sectp;
1493 this->eh_frame.size = bfd_section_size (sectp);
1494 }
1495 else if (names.ranges.matches (sectp->name))
1496 {
1497 this->ranges.s.section = sectp;
1498 this->ranges.size = bfd_section_size (sectp);
1499 }
1500 else if (names.rnglists.matches (sectp->name))
1501 {
1502 this->rnglists.s.section = sectp;
1503 this->rnglists.size = bfd_section_size (sectp);
1504 }
1505 else if (names.types.matches (sectp->name))
1506 {
1507 struct dwarf2_section_info type_section;
1508
1509 memset (&type_section, 0, sizeof (type_section));
1510 type_section.s.section = sectp;
1511 type_section.size = bfd_section_size (sectp);
1512
1513 this->types.push_back (type_section);
1514 }
1515 else if (names.gdb_index.matches (sectp->name))
1516 {
1517 this->gdb_index.s.section = sectp;
1518 this->gdb_index.size = bfd_section_size (sectp);
1519 }
1520 else if (names.debug_names.matches (sectp->name))
1521 {
1522 this->debug_names.s.section = sectp;
1523 this->debug_names.size = bfd_section_size (sectp);
1524 }
1525 else if (names.debug_aranges.matches (sectp->name))
1526 {
1527 this->debug_aranges.s.section = sectp;
1528 this->debug_aranges.size = bfd_section_size (sectp);
1529 }
1530
1531 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1532 && bfd_section_vma (sectp) == 0)
1533 this->has_section_at_zero = true;
1534 }
1535
1536 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1537 SECTION_NAME. */
1538
1539 void
1540 dwarf2_get_section_info (struct objfile *objfile,
1541 enum dwarf2_section_enum sect,
1542 asection **sectp, const gdb_byte **bufp,
1543 bfd_size_type *sizep)
1544 {
1545 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1546 struct dwarf2_section_info *info;
1547
1548 /* We may see an objfile without any DWARF, in which case we just
1549 return nothing. */
1550 if (per_objfile == NULL)
1551 {
1552 *sectp = NULL;
1553 *bufp = NULL;
1554 *sizep = 0;
1555 return;
1556 }
1557 switch (sect)
1558 {
1559 case DWARF2_DEBUG_FRAME:
1560 info = &per_objfile->per_bfd->frame;
1561 break;
1562 case DWARF2_EH_FRAME:
1563 info = &per_objfile->per_bfd->eh_frame;
1564 break;
1565 default:
1566 gdb_assert_not_reached ("unexpected section");
1567 }
1568
1569 info->read (objfile);
1570
1571 *sectp = info->get_bfd_section ();
1572 *bufp = info->buffer;
1573 *sizep = info->size;
1574 }
1575
1576 /* See dwarf2/read.h. */
1577
1578 void
1579 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1580 {
1581 info.read (objfile);
1582 abbrev.read (objfile);
1583 line.read (objfile);
1584 str.read (objfile);
1585 str_offsets.read (objfile);
1586 line_str.read (objfile);
1587 ranges.read (objfile);
1588 rnglists.read (objfile);
1589 addr.read (objfile);
1590 debug_aranges.read (objfile);
1591
1592 for (auto &section : types)
1593 section.read (objfile);
1594 }
1595
1596 \f
1597 /* DWARF quick_symbol_functions support. */
1598
1599 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1600 unique line tables, so we maintain a separate table of all .debug_line
1601 derived entries to support the sharing.
1602 All the quick functions need is the list of file names. We discard the
1603 line_header when we're done and don't need to record it here. */
1604 struct quick_file_names
1605 {
1606 /* The data used to construct the hash key. */
1607 struct stmt_list_hash hash;
1608
1609 /* The number of entries in file_names, real_names. */
1610 unsigned int num_file_names;
1611
1612 /* The CU directory, as given by DW_AT_comp_dir. May be
1613 nullptr. */
1614 const char *comp_dir;
1615
1616 /* The file names from the line table, after being run through
1617 file_full_name. */
1618 const char **file_names;
1619
1620 /* The file names from the line table after being run through
1621 gdb_realpath. These are computed lazily. */
1622 const char **real_names;
1623 };
1624
1625 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1626 It's handy in this case to have an empty implementation of the
1627 quick symbol functions, to avoid special cases in the rest of the
1628 code. */
1629
1630 struct readnow_functions : public dwarf2_base_index_functions
1631 {
1632 void dump (struct objfile *objfile) override
1633 {
1634 }
1635
1636 bool expand_symtabs_matching
1637 (struct objfile *objfile,
1638 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1639 const lookup_name_info *lookup_name,
1640 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1641 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1642 block_search_flags search_flags,
1643 domain_enum domain,
1644 enum search_domain kind) override
1645 {
1646 return true;
1647 }
1648 };
1649
1650 /* Utility hash function for a stmt_list_hash. */
1651
1652 static hashval_t
1653 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1654 {
1655 hashval_t v = 0;
1656
1657 if (stmt_list_hash->dwo_unit != NULL)
1658 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1659 v += to_underlying (stmt_list_hash->line_sect_off);
1660 return v;
1661 }
1662
1663 /* Utility equality function for a stmt_list_hash. */
1664
1665 static int
1666 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1667 const struct stmt_list_hash *rhs)
1668 {
1669 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1670 return 0;
1671 if (lhs->dwo_unit != NULL
1672 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1673 return 0;
1674
1675 return lhs->line_sect_off == rhs->line_sect_off;
1676 }
1677
1678 /* Hash function for a quick_file_names. */
1679
1680 static hashval_t
1681 hash_file_name_entry (const void *e)
1682 {
1683 const struct quick_file_names *file_data
1684 = (const struct quick_file_names *) e;
1685
1686 return hash_stmt_list_entry (&file_data->hash);
1687 }
1688
1689 /* Equality function for a quick_file_names. */
1690
1691 static int
1692 eq_file_name_entry (const void *a, const void *b)
1693 {
1694 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1695 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1696
1697 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1698 }
1699
1700 /* See read.h. */
1701
1702 htab_up
1703 create_quick_file_names_table (unsigned int nr_initial_entries)
1704 {
1705 return htab_up (htab_create_alloc (nr_initial_entries,
1706 hash_file_name_entry, eq_file_name_entry,
1707 nullptr, xcalloc, xfree));
1708 }
1709
1710 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
1711 function is unrelated to symtabs, symtab would have to be created afterwards.
1712 You should call age_cached_comp_units after processing the CU. */
1713
1714 static dwarf2_cu *
1715 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1716 bool skip_partial)
1717 {
1718 if (per_cu->is_debug_types)
1719 load_full_type_unit (per_cu, per_objfile);
1720 else
1721 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
1722 skip_partial, language_minimal);
1723
1724 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1725 if (cu == nullptr)
1726 return nullptr; /* Dummy CU. */
1727
1728 dwarf2_find_base_address (cu->dies, cu);
1729
1730 return cu;
1731 }
1732
1733 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
1734
1735 static void
1736 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1737 dwarf2_per_objfile *per_objfile, bool skip_partial)
1738 {
1739 {
1740 /* The destructor of dwarf2_queue_guard frees any entries left on
1741 the queue. After this point we're guaranteed to leave this function
1742 with the dwarf queue empty. */
1743 dwarf2_queue_guard q_guard (per_objfile);
1744
1745 if (!per_objfile->symtab_set_p (per_cu))
1746 {
1747 queue_comp_unit (per_cu, per_objfile, language_minimal);
1748 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
1749
1750 /* If we just loaded a CU from a DWO, and we're working with an index
1751 that may badly handle TUs, load all the TUs in that DWO as well.
1752 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
1753 if (!per_cu->is_debug_types
1754 && cu != NULL
1755 && cu->dwo_unit != NULL
1756 && per_objfile->per_bfd->index_table != NULL
1757 && !per_objfile->per_bfd->index_table->version_check ()
1758 /* DWP files aren't supported yet. */
1759 && get_dwp_file (per_objfile) == NULL)
1760 queue_and_load_all_dwo_tus (cu);
1761 }
1762
1763 process_queue (per_objfile);
1764 }
1765
1766 /* Age the cache, releasing compilation units that have not
1767 been used recently. */
1768 per_objfile->age_comp_units ();
1769 }
1770
1771 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
1772 the per-objfile for which this symtab is instantiated.
1773
1774 Returns the resulting symbol table. */
1775
1776 static struct compunit_symtab *
1777 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1778 dwarf2_per_objfile *per_objfile,
1779 bool skip_partial)
1780 {
1781 if (!per_objfile->symtab_set_p (per_cu))
1782 {
1783 free_cached_comp_units freer (per_objfile);
1784 scoped_restore decrementer = increment_reading_symtab ();
1785 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
1786 process_cu_includes (per_objfile);
1787 }
1788
1789 return per_objfile->get_symtab (per_cu);
1790 }
1791
1792 /* See read.h. */
1793
1794 dwarf2_per_cu_data_up
1795 dwarf2_per_bfd::allocate_per_cu ()
1796 {
1797 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
1798 result->per_bfd = this;
1799 result->index = all_units.size ();
1800 return result;
1801 }
1802
1803 /* See read.h. */
1804
1805 signatured_type_up
1806 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
1807 {
1808 signatured_type_up result (new signatured_type (signature));
1809 result->per_bfd = this;
1810 result->index = all_units.size ();
1811 result->is_debug_types = true;
1812 tu_stats.nr_tus++;
1813 return result;
1814 }
1815
1816 /* See read.h. */
1817
1818 dwarf2_per_cu_data_up
1819 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
1820 struct dwarf2_section_info *section,
1821 int is_dwz,
1822 sect_offset sect_off, ULONGEST length)
1823 {
1824 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
1825 the_cu->sect_off = sect_off;
1826 the_cu->set_length (length);
1827 the_cu->section = section;
1828 the_cu->is_dwz = is_dwz;
1829 return the_cu;
1830 }
1831
1832 /* die_reader_func for dw2_get_file_names. */
1833
1834 static void
1835 dw2_get_file_names_reader (const struct die_reader_specs *reader,
1836 struct die_info *comp_unit_die)
1837 {
1838 struct dwarf2_cu *cu = reader->cu;
1839 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
1840 dwarf2_per_objfile *per_objfile = cu->per_objfile;
1841 struct dwarf2_per_cu_data *lh_cu;
1842 struct attribute *attr;
1843 void **slot;
1844 struct quick_file_names *qfn;
1845
1846 gdb_assert (! this_cu->is_debug_types);
1847
1848 this_cu->files_read = true;
1849 /* Our callers never want to match partial units -- instead they
1850 will match the enclosing full CU. */
1851 if (comp_unit_die->tag == DW_TAG_partial_unit)
1852 return;
1853
1854 lh_cu = this_cu;
1855 slot = NULL;
1856
1857 line_header_up lh;
1858 sect_offset line_offset {};
1859
1860 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
1861
1862 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
1863 if (attr != nullptr && attr->form_is_unsigned ())
1864 {
1865 struct quick_file_names find_entry;
1866
1867 line_offset = (sect_offset) attr->as_unsigned ();
1868
1869 /* We may have already read in this line header (TU line header sharing).
1870 If we have we're done. */
1871 find_entry.hash.dwo_unit = cu->dwo_unit;
1872 find_entry.hash.line_sect_off = line_offset;
1873 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
1874 &find_entry, INSERT);
1875 if (*slot != NULL)
1876 {
1877 lh_cu->file_names = (struct quick_file_names *) *slot;
1878 return;
1879 }
1880
1881 lh = dwarf_decode_line_header (line_offset, cu, fnd.get_comp_dir ());
1882 }
1883
1884 int offset = 0;
1885 if (!fnd.is_unknown ())
1886 ++offset;
1887 else if (lh == nullptr)
1888 return;
1889
1890 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
1891 qfn->hash.dwo_unit = cu->dwo_unit;
1892 qfn->hash.line_sect_off = line_offset;
1893 /* There may not be a DW_AT_stmt_list. */
1894 if (slot != nullptr)
1895 *slot = qfn;
1896
1897 std::vector<const char *> include_names;
1898 if (lh != nullptr)
1899 {
1900 for (const auto &entry : lh->file_names ())
1901 {
1902 std::string name_holder;
1903 const char *include_name =
1904 compute_include_file_name (lh.get (), entry, fnd, name_holder);
1905 if (include_name != nullptr)
1906 {
1907 include_name = per_objfile->objfile->intern (include_name);
1908 include_names.push_back (include_name);
1909 }
1910 }
1911 }
1912
1913 qfn->num_file_names = offset + include_names.size ();
1914 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
1915 qfn->file_names =
1916 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
1917 qfn->num_file_names);
1918 if (offset != 0)
1919 qfn->file_names[0] = per_objfile->objfile->intern (fnd.get_name ());
1920
1921 if (!include_names.empty ())
1922 memcpy (&qfn->file_names[offset], include_names.data (),
1923 include_names.size () * sizeof (const char *));
1924
1925 qfn->real_names = NULL;
1926
1927 lh_cu->file_names = qfn;
1928 }
1929
1930 /* A helper for the "quick" functions which attempts to read the line
1931 table for THIS_CU. */
1932
1933 static struct quick_file_names *
1934 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
1935 dwarf2_per_objfile *per_objfile)
1936 {
1937 /* This should never be called for TUs. */
1938 gdb_assert (! this_cu->is_debug_types);
1939
1940 if (this_cu->files_read)
1941 return this_cu->file_names;
1942
1943 cutu_reader reader (this_cu, per_objfile);
1944 if (!reader.dummy_p)
1945 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
1946
1947 return this_cu->file_names;
1948 }
1949
1950 /* A helper for the "quick" functions which computes and caches the
1951 real path for a given file name from the line table. */
1952
1953 static const char *
1954 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
1955 struct quick_file_names *qfn, int index)
1956 {
1957 if (qfn->real_names == NULL)
1958 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
1959 qfn->num_file_names, const char *);
1960
1961 if (qfn->real_names[index] == NULL)
1962 {
1963 const char *dirname = nullptr;
1964
1965 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
1966 dirname = qfn->comp_dir;
1967
1968 gdb::unique_xmalloc_ptr<char> fullname;
1969 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
1970
1971 qfn->real_names[index] = fullname.release ();
1972 }
1973
1974 return qfn->real_names[index];
1975 }
1976
1977 struct symtab *
1978 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
1979 {
1980 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1981 dwarf2_per_cu_data *dwarf_cu
1982 = per_objfile->per_bfd->all_units.back ().get ();
1983 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
1984
1985 if (cust == NULL)
1986 return NULL;
1987
1988 return cust->primary_filetab ();
1989 }
1990
1991 /* See read.h. */
1992
1993 void
1994 dwarf2_per_cu_data::free_cached_file_names ()
1995 {
1996 if (fnd != nullptr)
1997 fnd->forget_fullname ();
1998
1999 if (per_bfd == nullptr)
2000 return;
2001
2002 struct quick_file_names *file_data = file_names;
2003 if (file_data != nullptr && file_data->real_names != nullptr)
2004 {
2005 for (int i = 0; i < file_data->num_file_names; ++i)
2006 {
2007 xfree ((void *) file_data->real_names[i]);
2008 file_data->real_names[i] = nullptr;
2009 }
2010 }
2011 }
2012
2013 void
2014 dwarf2_base_index_functions::forget_cached_source_info
2015 (struct objfile *objfile)
2016 {
2017 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2018
2019 for (auto &per_cu : per_objfile->per_bfd->all_units)
2020 per_cu->free_cached_file_names ();
2021 }
2022
2023 void
2024 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
2025 bool print_bcache)
2026 {
2027 if (print_bcache)
2028 return;
2029
2030 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2031 int total = per_objfile->per_bfd->all_units.size ();
2032 int count = 0;
2033
2034 for (int i = 0; i < total; ++i)
2035 {
2036 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2037
2038 if (!per_objfile->symtab_set_p (per_cu))
2039 ++count;
2040 }
2041 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
2042 gdb_printf (_(" Number of unread CUs: %d\n"), count);
2043 }
2044
2045 void
2046 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
2047 {
2048 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2049 int total_units = per_objfile->per_bfd->all_units.size ();
2050
2051 for (int i = 0; i < total_units; ++i)
2052 {
2053 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2054
2055 /* We don't want to directly expand a partial CU, because if we
2056 read it with the wrong language, then assertion failures can
2057 be triggered later on. See PR symtab/23010. So, tell
2058 dw2_instantiate_symtab to skip partial CUs -- any important
2059 partial CU will be read via DW_TAG_imported_unit anyway. */
2060 dw2_instantiate_symtab (per_cu, per_objfile, true);
2061 }
2062 }
2063
2064
2065 /* Starting from a search name, return the string that finds the upper
2066 bound of all strings that start with SEARCH_NAME in a sorted name
2067 list. Returns the empty string to indicate that the upper bound is
2068 the end of the list. */
2069
2070 static std::string
2071 make_sort_after_prefix_name (const char *search_name)
2072 {
2073 /* When looking to complete "func", we find the upper bound of all
2074 symbols that start with "func" by looking for where we'd insert
2075 the closest string that would follow "func" in lexicographical
2076 order. Usually, that's "func"-with-last-character-incremented,
2077 i.e. "fund". Mind non-ASCII characters, though. Usually those
2078 will be UTF-8 multi-byte sequences, but we can't be certain.
2079 Especially mind the 0xff character, which is a valid character in
2080 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
2081 rule out compilers allowing it in identifiers. Note that
2082 conveniently, strcmp/strcasecmp are specified to compare
2083 characters interpreted as unsigned char. So what we do is treat
2084 the whole string as a base 256 number composed of a sequence of
2085 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
2086 to 0, and carries 1 to the following more-significant position.
2087 If the very first character in SEARCH_NAME ends up incremented
2088 and carries/overflows, then the upper bound is the end of the
2089 list. The string after the empty string is also the empty
2090 string.
2091
2092 Some examples of this operation:
2093
2094 SEARCH_NAME => "+1" RESULT
2095
2096 "abc" => "abd"
2097 "ab\xff" => "ac"
2098 "\xff" "a" "\xff" => "\xff" "b"
2099 "\xff" => ""
2100 "\xff\xff" => ""
2101 "" => ""
2102
2103 Then, with these symbols for example:
2104
2105 func
2106 func1
2107 fund
2108
2109 completing "func" looks for symbols between "func" and
2110 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
2111 which finds "func" and "func1", but not "fund".
2112
2113 And with:
2114
2115 funcÿ (Latin1 'ÿ' [0xff])
2116 funcÿ1
2117 fund
2118
2119 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
2120 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
2121
2122 And with:
2123
2124 ÿÿ (Latin1 'ÿ' [0xff])
2125 ÿÿ1
2126
2127 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
2128 the end of the list.
2129 */
2130 std::string after = search_name;
2131 while (!after.empty () && (unsigned char) after.back () == 0xff)
2132 after.pop_back ();
2133 if (!after.empty ())
2134 after.back () = (unsigned char) after.back () + 1;
2135 return after;
2136 }
2137
2138 /* See declaration. */
2139
2140 std::pair<std::vector<name_component>::const_iterator,
2141 std::vector<name_component>::const_iterator>
2142 mapped_index_base::find_name_components_bounds
2143 (const lookup_name_info &lookup_name_without_params, language lang,
2144 dwarf2_per_objfile *per_objfile) const
2145 {
2146 auto *name_cmp
2147 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
2148
2149 const char *lang_name
2150 = lookup_name_without_params.language_lookup_name (lang);
2151
2152 /* Comparison function object for lower_bound that matches against a
2153 given symbol name. */
2154 auto lookup_compare_lower = [&] (const name_component &elem,
2155 const char *name)
2156 {
2157 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
2158 const char *elem_name = elem_qualified + elem.name_offset;
2159 return name_cmp (elem_name, name) < 0;
2160 };
2161
2162 /* Comparison function object for upper_bound that matches against a
2163 given symbol name. */
2164 auto lookup_compare_upper = [&] (const char *name,
2165 const name_component &elem)
2166 {
2167 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
2168 const char *elem_name = elem_qualified + elem.name_offset;
2169 return name_cmp (name, elem_name) < 0;
2170 };
2171
2172 auto begin = this->name_components.begin ();
2173 auto end = this->name_components.end ();
2174
2175 /* Find the lower bound. */
2176 auto lower = [&] ()
2177 {
2178 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
2179 return begin;
2180 else
2181 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
2182 } ();
2183
2184 /* Find the upper bound. */
2185 auto upper = [&] ()
2186 {
2187 if (lookup_name_without_params.completion_mode ())
2188 {
2189 /* In completion mode, we want UPPER to point past all
2190 symbols names that have the same prefix. I.e., with
2191 these symbols, and completing "func":
2192
2193 function << lower bound
2194 function1
2195 other_function << upper bound
2196
2197 We find the upper bound by looking for the insertion
2198 point of "func"-with-last-character-incremented,
2199 i.e. "fund". */
2200 std::string after = make_sort_after_prefix_name (lang_name);
2201 if (after.empty ())
2202 return end;
2203 return std::lower_bound (lower, end, after.c_str (),
2204 lookup_compare_lower);
2205 }
2206 else
2207 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
2208 } ();
2209
2210 return {lower, upper};
2211 }
2212
2213 /* See declaration. */
2214
2215 void
2216 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
2217 {
2218 if (!this->name_components.empty ())
2219 return;
2220
2221 this->name_components_casing = case_sensitivity;
2222 auto *name_cmp
2223 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
2224
2225 /* The code below only knows how to break apart components of C++
2226 symbol names (and other languages that use '::' as
2227 namespace/module separator) and Ada symbol names. */
2228 auto count = this->symbol_name_count ();
2229 for (offset_type idx = 0; idx < count; idx++)
2230 {
2231 if (this->symbol_name_slot_invalid (idx))
2232 continue;
2233
2234 const char *name = this->symbol_name_at (idx, per_objfile);
2235
2236 /* Add each name component to the name component table. */
2237 unsigned int previous_len = 0;
2238
2239 if (strstr (name, "::") != nullptr)
2240 {
2241 for (unsigned int current_len = cp_find_first_component (name);
2242 name[current_len] != '\0';
2243 current_len += cp_find_first_component (name + current_len))
2244 {
2245 gdb_assert (name[current_len] == ':');
2246 this->name_components.push_back ({previous_len, idx});
2247 /* Skip the '::'. */
2248 current_len += 2;
2249 previous_len = current_len;
2250 }
2251 }
2252 else
2253 {
2254 /* Handle the Ada encoded (aka mangled) form here. */
2255 for (const char *iter = strstr (name, "__");
2256 iter != nullptr;
2257 iter = strstr (iter, "__"))
2258 {
2259 this->name_components.push_back ({previous_len, idx});
2260 iter += 2;
2261 previous_len = iter - name;
2262 }
2263 }
2264
2265 this->name_components.push_back ({previous_len, idx});
2266 }
2267
2268 /* Sort name_components elements by name. */
2269 auto name_comp_compare = [&] (const name_component &left,
2270 const name_component &right)
2271 {
2272 const char *left_qualified
2273 = this->symbol_name_at (left.idx, per_objfile);
2274 const char *right_qualified
2275 = this->symbol_name_at (right.idx, per_objfile);
2276
2277 const char *left_name = left_qualified + left.name_offset;
2278 const char *right_name = right_qualified + right.name_offset;
2279
2280 return name_cmp (left_name, right_name) < 0;
2281 };
2282
2283 std::sort (this->name_components.begin (),
2284 this->name_components.end (),
2285 name_comp_compare);
2286 }
2287
2288 /* See read.h. */
2289
2290 bool
2291 dw2_expand_symtabs_matching_symbol
2292 (mapped_index_base &index,
2293 const lookup_name_info &lookup_name_in,
2294 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2295 gdb::function_view<bool (offset_type)> match_callback,
2296 dwarf2_per_objfile *per_objfile)
2297 {
2298 lookup_name_info lookup_name_without_params
2299 = lookup_name_in.make_ignore_params ();
2300
2301 /* Build the symbol name component sorted vector, if we haven't
2302 yet. */
2303 index.build_name_components (per_objfile);
2304
2305 /* The same symbol may appear more than once in the range though.
2306 E.g., if we're looking for symbols that complete "w", and we have
2307 a symbol named "w1::w2", we'll find the two name components for
2308 that same symbol in the range. To be sure we only call the
2309 callback once per symbol, we first collect the symbol name
2310 indexes that matched in a temporary vector and ignore
2311 duplicates. */
2312 std::vector<offset_type> matches;
2313
2314 struct name_and_matcher
2315 {
2316 symbol_name_matcher_ftype *matcher;
2317 const char *name;
2318
2319 bool operator== (const name_and_matcher &other) const
2320 {
2321 return matcher == other.matcher && strcmp (name, other.name) == 0;
2322 }
2323 };
2324
2325 /* A vector holding all the different symbol name matchers, for all
2326 languages. */
2327 std::vector<name_and_matcher> matchers;
2328
2329 for (int i = 0; i < nr_languages; i++)
2330 {
2331 enum language lang_e = (enum language) i;
2332
2333 const language_defn *lang = language_def (lang_e);
2334 symbol_name_matcher_ftype *name_matcher
2335 = lang->get_symbol_name_matcher (lookup_name_without_params);
2336
2337 name_and_matcher key {
2338 name_matcher,
2339 lookup_name_without_params.language_lookup_name (lang_e)
2340 };
2341
2342 /* Don't insert the same comparison routine more than once.
2343 Note that we do this linear walk. This is not a problem in
2344 practice because the number of supported languages is
2345 low. */
2346 if (std::find (matchers.begin (), matchers.end (), key)
2347 != matchers.end ())
2348 continue;
2349 matchers.push_back (std::move (key));
2350
2351 auto bounds
2352 = index.find_name_components_bounds (lookup_name_without_params,
2353 lang_e, per_objfile);
2354
2355 /* Now for each symbol name in range, check to see if we have a name
2356 match, and if so, call the MATCH_CALLBACK callback. */
2357
2358 for (; bounds.first != bounds.second; ++bounds.first)
2359 {
2360 const char *qualified
2361 = index.symbol_name_at (bounds.first->idx, per_objfile);
2362
2363 if (!name_matcher (qualified, lookup_name_without_params, NULL)
2364 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
2365 continue;
2366
2367 matches.push_back (bounds.first->idx);
2368 }
2369 }
2370
2371 std::sort (matches.begin (), matches.end ());
2372
2373 /* Finally call the callback, once per match. */
2374 ULONGEST prev = -1;
2375 bool result = true;
2376 for (offset_type idx : matches)
2377 {
2378 if (prev != idx)
2379 {
2380 if (!match_callback (idx))
2381 {
2382 result = false;
2383 break;
2384 }
2385 prev = idx;
2386 }
2387 }
2388
2389 /* Above we use a type wider than idx's for 'prev', since 0 and
2390 (offset_type)-1 are both possible values. */
2391 static_assert (sizeof (prev) > sizeof (offset_type), "");
2392
2393 return result;
2394 }
2395
2396 #if GDB_SELF_TEST
2397
2398 namespace selftests { namespace dw2_expand_symtabs_matching {
2399
2400 /* A mock .gdb_index/.debug_names-like name index table, enough to
2401 exercise dw2_expand_symtabs_matching_symbol, which works with the
2402 mapped_index_base interface. Builds an index from the symbol list
2403 passed as parameter to the constructor. */
2404 class mock_mapped_index : public mapped_index_base
2405 {
2406 public:
2407 mock_mapped_index (gdb::array_view<const char *> symbols)
2408 : m_symbol_table (symbols)
2409 {}
2410
2411 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
2412
2413 /* Return the number of names in the symbol table. */
2414 size_t symbol_name_count () const override
2415 {
2416 return m_symbol_table.size ();
2417 }
2418
2419 /* Get the name of the symbol at IDX in the symbol table. */
2420 const char *symbol_name_at
2421 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
2422 {
2423 return m_symbol_table[idx];
2424 }
2425
2426 quick_symbol_functions_up make_quick_functions () const override
2427 {
2428 return nullptr;
2429 }
2430
2431 private:
2432 gdb::array_view<const char *> m_symbol_table;
2433 };
2434
2435 /* Convenience function that converts a NULL pointer to a "<null>"
2436 string, to pass to print routines. */
2437
2438 static const char *
2439 string_or_null (const char *str)
2440 {
2441 return str != NULL ? str : "<null>";
2442 }
2443
2444 /* Check if a lookup_name_info built from
2445 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
2446 index. EXPECTED_LIST is the list of expected matches, in expected
2447 matching order. If no match expected, then an empty list is
2448 specified. Returns true on success. On failure prints a warning
2449 indicating the file:line that failed, and returns false. */
2450
2451 static bool
2452 check_match (const char *file, int line,
2453 mock_mapped_index &mock_index,
2454 const char *name, symbol_name_match_type match_type,
2455 bool completion_mode,
2456 std::initializer_list<const char *> expected_list,
2457 dwarf2_per_objfile *per_objfile)
2458 {
2459 lookup_name_info lookup_name (name, match_type, completion_mode);
2460
2461 bool matched = true;
2462
2463 auto mismatch = [&] (const char *expected_str,
2464 const char *got)
2465 {
2466 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
2467 "expected=\"%s\", got=\"%s\"\n"),
2468 file, line,
2469 (match_type == symbol_name_match_type::FULL
2470 ? "FULL" : "WILD"),
2471 name, string_or_null (expected_str), string_or_null (got));
2472 matched = false;
2473 };
2474
2475 auto expected_it = expected_list.begin ();
2476 auto expected_end = expected_list.end ();
2477
2478 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
2479 nullptr,
2480 [&] (offset_type idx)
2481 {
2482 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
2483 const char *expected_str
2484 = expected_it == expected_end ? NULL : *expected_it++;
2485
2486 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
2487 mismatch (expected_str, matched_name);
2488 return true;
2489 }, per_objfile);
2490
2491 const char *expected_str
2492 = expected_it == expected_end ? NULL : *expected_it++;
2493 if (expected_str != NULL)
2494 mismatch (expected_str, NULL);
2495
2496 return matched;
2497 }
2498
2499 /* The symbols added to the mock mapped_index for testing (in
2500 canonical form). */
2501 static const char *test_symbols[] = {
2502 "function",
2503 "std::bar",
2504 "std::zfunction",
2505 "std::zfunction2",
2506 "w1::w2",
2507 "ns::foo<char*>",
2508 "ns::foo<int>",
2509 "ns::foo<long>",
2510 "ns2::tmpl<int>::foo2",
2511 "(anonymous namespace)::A::B::C",
2512
2513 /* These are used to check that the increment-last-char in the
2514 matching algorithm for completion doesn't match "t1_fund" when
2515 completing "t1_func". */
2516 "t1_func",
2517 "t1_func1",
2518 "t1_fund",
2519 "t1_fund1",
2520
2521 /* A UTF-8 name with multi-byte sequences to make sure that
2522 cp-name-parser understands this as a single identifier ("função"
2523 is "function" in PT). */
2524 (const char *)u8"u8função",
2525
2526 /* Test a symbol name that ends with a 0xff character, which is a
2527 valid character in non-UTF-8 source character sets (e.g. Latin1
2528 'ÿ'), and we can't rule out compilers allowing it in identifiers.
2529 We test this because the completion algorithm finds the upper
2530 bound of symbols by looking for the insertion point of
2531 "func"-with-last-character-incremented, i.e. "fund", and adding 1
2532 to 0xff should wraparound and carry to the previous character.
2533 See comments in make_sort_after_prefix_name. */
2534 "yfunc\377",
2535
2536 /* Some more symbols with \377 (0xff). See above. */
2537 "\377",
2538 "\377\377123",
2539
2540 /* A name with all sorts of complications. Starts with "z" to make
2541 it easier for the completion tests below. */
2542 #define Z_SYM_NAME \
2543 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
2544 "::tuple<(anonymous namespace)::ui*, " \
2545 "std::default_delete<(anonymous namespace)::ui>, void>"
2546
2547 Z_SYM_NAME
2548 };
2549
2550 /* Returns true if the mapped_index_base::find_name_component_bounds
2551 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
2552 in completion mode. */
2553
2554 static bool
2555 check_find_bounds_finds (mapped_index_base &index,
2556 const char *search_name,
2557 gdb::array_view<const char *> expected_syms,
2558 dwarf2_per_objfile *per_objfile)
2559 {
2560 lookup_name_info lookup_name (search_name,
2561 symbol_name_match_type::FULL, true);
2562
2563 auto bounds = index.find_name_components_bounds (lookup_name,
2564 language_cplus,
2565 per_objfile);
2566
2567 size_t distance = std::distance (bounds.first, bounds.second);
2568 if (distance != expected_syms.size ())
2569 return false;
2570
2571 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
2572 {
2573 auto nc_elem = bounds.first + exp_elem;
2574 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
2575 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
2576 return false;
2577 }
2578
2579 return true;
2580 }
2581
2582 /* Test the lower-level mapped_index::find_name_component_bounds
2583 method. */
2584
2585 static void
2586 test_mapped_index_find_name_component_bounds ()
2587 {
2588 mock_mapped_index mock_index (test_symbols);
2589
2590 mock_index.build_name_components (NULL /* per_objfile */);
2591
2592 /* Test the lower-level mapped_index::find_name_component_bounds
2593 method in completion mode. */
2594 {
2595 static const char *expected_syms[] = {
2596 "t1_func",
2597 "t1_func1",
2598 };
2599
2600 SELF_CHECK (check_find_bounds_finds
2601 (mock_index, "t1_func", expected_syms,
2602 NULL /* per_objfile */));
2603 }
2604
2605 /* Check that the increment-last-char in the name matching algorithm
2606 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
2607 make_sort_after_prefix_name. */
2608 {
2609 static const char *expected_syms1[] = {
2610 "\377",
2611 "\377\377123",
2612 };
2613 SELF_CHECK (check_find_bounds_finds
2614 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
2615
2616 static const char *expected_syms2[] = {
2617 "\377\377123",
2618 };
2619 SELF_CHECK (check_find_bounds_finds
2620 (mock_index, "\377\377", expected_syms2,
2621 NULL /* per_objfile */));
2622 }
2623 }
2624
2625 /* Test dw2_expand_symtabs_matching_symbol. */
2626
2627 static void
2628 test_dw2_expand_symtabs_matching_symbol ()
2629 {
2630 mock_mapped_index mock_index (test_symbols);
2631
2632 /* We let all tests run until the end even if some fails, for debug
2633 convenience. */
2634 bool any_mismatch = false;
2635
2636 /* Create the expected symbols list (an initializer_list). Needed
2637 because lists have commas, and we need to pass them to CHECK,
2638 which is a macro. */
2639 #define EXPECT(...) { __VA_ARGS__ }
2640
2641 /* Wrapper for check_match that passes down the current
2642 __FILE__/__LINE__. */
2643 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
2644 any_mismatch |= !check_match (__FILE__, __LINE__, \
2645 mock_index, \
2646 NAME, MATCH_TYPE, COMPLETION_MODE, \
2647 EXPECTED_LIST, NULL)
2648
2649 /* Identity checks. */
2650 for (const char *sym : test_symbols)
2651 {
2652 /* Should be able to match all existing symbols. */
2653 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
2654 EXPECT (sym));
2655
2656 /* Should be able to match all existing symbols with
2657 parameters. */
2658 std::string with_params = std::string (sym) + "(int)";
2659 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2660 EXPECT (sym));
2661
2662 /* Should be able to match all existing symbols with
2663 parameters and qualifiers. */
2664 with_params = std::string (sym) + " ( int ) const";
2665 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2666 EXPECT (sym));
2667
2668 /* This should really find sym, but cp-name-parser.y doesn't
2669 know about lvalue/rvalue qualifiers yet. */
2670 with_params = std::string (sym) + " ( int ) &&";
2671 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2672 {});
2673 }
2674
2675 /* Check that the name matching algorithm for completion doesn't get
2676 confused with Latin1 'ÿ' / 0xff. See
2677 make_sort_after_prefix_name. */
2678 {
2679 static const char str[] = "\377";
2680 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
2681 EXPECT ("\377", "\377\377123"));
2682 }
2683
2684 /* Check that the increment-last-char in the matching algorithm for
2685 completion doesn't match "t1_fund" when completing "t1_func". */
2686 {
2687 static const char str[] = "t1_func";
2688 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
2689 EXPECT ("t1_func", "t1_func1"));
2690 }
2691
2692 /* Check that completion mode works at each prefix of the expected
2693 symbol name. */
2694 {
2695 static const char str[] = "function(int)";
2696 size_t len = strlen (str);
2697 std::string lookup;
2698
2699 for (size_t i = 1; i < len; i++)
2700 {
2701 lookup.assign (str, i);
2702 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
2703 EXPECT ("function"));
2704 }
2705 }
2706
2707 /* While "w" is a prefix of both components, the match function
2708 should still only be called once. */
2709 {
2710 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
2711 EXPECT ("w1::w2"));
2712 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
2713 EXPECT ("w1::w2"));
2714 }
2715
2716 /* Same, with a "complicated" symbol. */
2717 {
2718 static const char str[] = Z_SYM_NAME;
2719 size_t len = strlen (str);
2720 std::string lookup;
2721
2722 for (size_t i = 1; i < len; i++)
2723 {
2724 lookup.assign (str, i);
2725 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
2726 EXPECT (Z_SYM_NAME));
2727 }
2728 }
2729
2730 /* In FULL mode, an incomplete symbol doesn't match. */
2731 {
2732 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
2733 {});
2734 }
2735
2736 /* A complete symbol with parameters matches any overload, since the
2737 index has no overload info. */
2738 {
2739 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
2740 EXPECT ("std::zfunction", "std::zfunction2"));
2741 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
2742 EXPECT ("std::zfunction", "std::zfunction2"));
2743 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
2744 EXPECT ("std::zfunction", "std::zfunction2"));
2745 }
2746
2747 /* Check that whitespace is ignored appropriately. A symbol with a
2748 template argument list. */
2749 {
2750 static const char expected[] = "ns::foo<int>";
2751 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
2752 EXPECT (expected));
2753 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
2754 EXPECT (expected));
2755 }
2756
2757 /* Check that whitespace is ignored appropriately. A symbol with a
2758 template argument list that includes a pointer. */
2759 {
2760 static const char expected[] = "ns::foo<char*>";
2761 /* Try both completion and non-completion modes. */
2762 static const bool completion_mode[2] = {false, true};
2763 for (size_t i = 0; i < 2; i++)
2764 {
2765 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
2766 completion_mode[i], EXPECT (expected));
2767 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
2768 completion_mode[i], EXPECT (expected));
2769
2770 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
2771 completion_mode[i], EXPECT (expected));
2772 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
2773 completion_mode[i], EXPECT (expected));
2774 }
2775 }
2776
2777 {
2778 /* Check method qualifiers are ignored. */
2779 static const char expected[] = "ns::foo<char*>";
2780 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
2781 symbol_name_match_type::FULL, true, EXPECT (expected));
2782 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
2783 symbol_name_match_type::FULL, true, EXPECT (expected));
2784 CHECK_MATCH ("foo < char * > ( int ) const",
2785 symbol_name_match_type::WILD, true, EXPECT (expected));
2786 CHECK_MATCH ("foo < char * > ( int ) &&",
2787 symbol_name_match_type::WILD, true, EXPECT (expected));
2788 }
2789
2790 /* Test lookup names that don't match anything. */
2791 {
2792 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
2793 {});
2794
2795 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
2796 {});
2797 }
2798
2799 /* Some wild matching tests, exercising "(anonymous namespace)",
2800 which should not be confused with a parameter list. */
2801 {
2802 static const char *syms[] = {
2803 "A::B::C",
2804 "B::C",
2805 "C",
2806 "A :: B :: C ( int )",
2807 "B :: C ( int )",
2808 "C ( int )",
2809 };
2810
2811 for (const char *s : syms)
2812 {
2813 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
2814 EXPECT ("(anonymous namespace)::A::B::C"));
2815 }
2816 }
2817
2818 {
2819 static const char expected[] = "ns2::tmpl<int>::foo2";
2820 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
2821 EXPECT (expected));
2822 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
2823 EXPECT (expected));
2824 }
2825
2826 SELF_CHECK (!any_mismatch);
2827
2828 #undef EXPECT
2829 #undef CHECK_MATCH
2830 }
2831
2832 static void
2833 run_test ()
2834 {
2835 test_mapped_index_find_name_component_bounds ();
2836 test_dw2_expand_symtabs_matching_symbol ();
2837 }
2838
2839 }} // namespace selftests::dw2_expand_symtabs_matching
2840
2841 #endif /* GDB_SELF_TEST */
2842
2843 /* See read.h. */
2844
2845 bool
2846 dw2_expand_symtabs_matching_one
2847 (dwarf2_per_cu_data *per_cu,
2848 dwarf2_per_objfile *per_objfile,
2849 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2850 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
2851 {
2852 if (file_matcher == NULL || per_cu->mark)
2853 {
2854 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
2855
2856 compunit_symtab *symtab
2857 = dw2_instantiate_symtab (per_cu, per_objfile, false);
2858 gdb_assert (symtab != nullptr);
2859
2860 if (expansion_notify != NULL && symtab_was_null)
2861 return expansion_notify (symtab);
2862 }
2863 return true;
2864 }
2865
2866 /* See read.h. */
2867
2868 void
2869 dw_expand_symtabs_matching_file_matcher
2870 (dwarf2_per_objfile *per_objfile,
2871 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
2872 {
2873 if (file_matcher == NULL)
2874 return;
2875
2876 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
2877 htab_eq_pointer,
2878 NULL, xcalloc, xfree));
2879 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
2880 htab_eq_pointer,
2881 NULL, xcalloc, xfree));
2882
2883 /* The rule is CUs specify all the files, including those used by
2884 any TU, so there's no need to scan TUs here. */
2885
2886 for (const auto &per_cu : per_objfile->per_bfd->all_units)
2887 {
2888 QUIT;
2889
2890 if (per_cu->is_debug_types)
2891 continue;
2892 per_cu->mark = 0;
2893
2894 /* We only need to look at symtabs not already expanded. */
2895 if (per_objfile->symtab_set_p (per_cu.get ()))
2896 continue;
2897
2898 if (per_cu->fnd != nullptr)
2899 {
2900 file_and_directory *fnd = per_cu->fnd.get ();
2901
2902 if (file_matcher (fnd->get_name (), false))
2903 {
2904 per_cu->mark = 1;
2905 continue;
2906 }
2907
2908 /* Before we invoke realpath, which can get expensive when many
2909 files are involved, do a quick comparison of the basenames. */
2910 if ((basenames_may_differ
2911 || file_matcher (lbasename (fnd->get_name ()), true))
2912 && file_matcher (fnd->get_fullname (), false))
2913 {
2914 per_cu->mark = 1;
2915 continue;
2916 }
2917 }
2918
2919 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
2920 per_objfile);
2921 if (file_data == NULL)
2922 continue;
2923
2924 if (htab_find (visited_not_found.get (), file_data) != NULL)
2925 continue;
2926 else if (htab_find (visited_found.get (), file_data) != NULL)
2927 {
2928 per_cu->mark = 1;
2929 continue;
2930 }
2931
2932 for (int j = 0; j < file_data->num_file_names; ++j)
2933 {
2934 const char *this_real_name;
2935
2936 if (file_matcher (file_data->file_names[j], false))
2937 {
2938 per_cu->mark = 1;
2939 break;
2940 }
2941
2942 /* Before we invoke realpath, which can get expensive when many
2943 files are involved, do a quick comparison of the basenames. */
2944 if (!basenames_may_differ
2945 && !file_matcher (lbasename (file_data->file_names[j]),
2946 true))
2947 continue;
2948
2949 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
2950 if (file_matcher (this_real_name, false))
2951 {
2952 per_cu->mark = 1;
2953 break;
2954 }
2955 }
2956
2957 void **slot = htab_find_slot (per_cu->mark
2958 ? visited_found.get ()
2959 : visited_not_found.get (),
2960 file_data, INSERT);
2961 *slot = file_data;
2962 }
2963 }
2964
2965
2966 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
2967 symtab. */
2968
2969 static struct compunit_symtab *
2970 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
2971 CORE_ADDR pc)
2972 {
2973 int i;
2974
2975 if (cust->blockvector () != nullptr
2976 && blockvector_contains_pc (cust->blockvector (), pc))
2977 return cust;
2978
2979 if (cust->includes == NULL)
2980 return NULL;
2981
2982 for (i = 0; cust->includes[i]; ++i)
2983 {
2984 struct compunit_symtab *s = cust->includes[i];
2985
2986 s = recursively_find_pc_sect_compunit_symtab (s, pc);
2987 if (s != NULL)
2988 return s;
2989 }
2990
2991 return NULL;
2992 }
2993
2994 dwarf2_per_cu_data *
2995 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
2996 unrelocated_addr adjusted_pc)
2997 {
2998 if (per_bfd->index_addrmap == nullptr)
2999 return nullptr;
3000
3001 void *obj = per_bfd->index_addrmap->find ((CORE_ADDR) adjusted_pc);
3002 return static_cast<dwarf2_per_cu_data *> (obj);
3003 }
3004
3005 struct compunit_symtab *
3006 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
3007 (struct objfile *objfile,
3008 struct bound_minimal_symbol msymbol,
3009 CORE_ADDR pc,
3010 struct obj_section *section,
3011 int warn_if_readin)
3012 {
3013 struct compunit_symtab *result;
3014
3015 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3016
3017 CORE_ADDR baseaddr = objfile->text_section_offset ();
3018 struct dwarf2_per_cu_data *data
3019 = find_per_cu (per_objfile->per_bfd, (unrelocated_addr) (pc - baseaddr));
3020 if (data == nullptr)
3021 return nullptr;
3022
3023 if (warn_if_readin && per_objfile->symtab_set_p (data))
3024 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3025 paddress (objfile->arch (), pc));
3026
3027 result = recursively_find_pc_sect_compunit_symtab
3028 (dw2_instantiate_symtab (data, per_objfile, false), pc);
3029
3030 if (warn_if_readin && result == nullptr)
3031 warning (_("(Error: pc %s in address map, but not in symtab.)"),
3032 paddress (objfile->arch (), pc));
3033
3034 return result;
3035 }
3036
3037 void
3038 dwarf2_base_index_functions::map_symbol_filenames
3039 (struct objfile *objfile,
3040 gdb::function_view<symbol_filename_ftype> fun,
3041 bool need_fullname)
3042 {
3043 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3044
3045 /* Use caches to ensure we only call FUN once for each filename. */
3046 filename_seen_cache filenames_cache;
3047 std::unordered_set<quick_file_names *> qfn_cache;
3048
3049 /* The rule is CUs specify all the files, including those used by any TU,
3050 so there's no need to scan TUs here. We can ignore file names coming
3051 from already-expanded CUs. It is possible that an expanded CU might
3052 reuse the file names data from a currently unexpanded CU, in this
3053 case we don't want to report the files from the unexpanded CU. */
3054
3055 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3056 {
3057 if (!per_cu->is_debug_types
3058 && per_objfile->symtab_set_p (per_cu.get ()))
3059 {
3060 if (per_cu->file_names != nullptr)
3061 qfn_cache.insert (per_cu->file_names);
3062 }
3063 }
3064
3065 for (dwarf2_per_cu_data *per_cu
3066 : all_units_range (per_objfile->per_bfd))
3067 {
3068 /* We only need to look at symtabs not already expanded. */
3069 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
3070 continue;
3071
3072 if (per_cu->fnd != nullptr)
3073 {
3074 file_and_directory *fnd = per_cu->fnd.get ();
3075
3076 const char *filename = fnd->get_name ();
3077 const char *key = filename;
3078 const char *fullname = nullptr;
3079
3080 if (need_fullname)
3081 {
3082 fullname = fnd->get_fullname ();
3083 key = fullname;
3084 }
3085
3086 if (!filenames_cache.seen (key))
3087 fun (filename, fullname);
3088 }
3089
3090 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3091 if (file_data == nullptr
3092 || qfn_cache.find (file_data) != qfn_cache.end ())
3093 continue;
3094
3095 for (int j = 0; j < file_data->num_file_names; ++j)
3096 {
3097 const char *filename = file_data->file_names[j];
3098 const char *key = filename;
3099 const char *fullname = nullptr;
3100
3101 if (need_fullname)
3102 {
3103 fullname = dw2_get_real_path (per_objfile, file_data, j);
3104 key = fullname;
3105 }
3106
3107 if (!filenames_cache.seen (key))
3108 fun (filename, fullname);
3109 }
3110 }
3111 }
3112
3113 bool
3114 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
3115 {
3116 return true;
3117 }
3118
3119 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
3120
3121 bool
3122 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
3123 {
3124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3125
3126 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3127 {
3128 /* Is this already expanded? */
3129 if (per_objfile->symtab_set_p (per_cu.get ()))
3130 continue;
3131
3132 /* It has not yet been expanded. */
3133 return true;
3134 }
3135
3136 return false;
3137 }
3138
3139 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
3140 to either a dwarf2_per_bfd or dwz_file object. */
3141
3142 template <typename T>
3143 static gdb::array_view<const gdb_byte>
3144 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
3145 {
3146 dwarf2_section_info *section = &section_owner->gdb_index;
3147
3148 if (section->empty ())
3149 return {};
3150
3151 /* Older elfutils strip versions could keep the section in the main
3152 executable while splitting it for the separate debug info file. */
3153 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
3154 return {};
3155
3156 section->read (obj);
3157
3158 /* dwarf2_section_info::size is a bfd_size_type, while
3159 gdb::array_view works with size_t. On 32-bit hosts, with
3160 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
3161 is 32-bit. So we need an explicit narrowing conversion here.
3162 This is fine, because it's impossible to allocate or mmap an
3163 array/buffer larger than what size_t can represent. */
3164 return gdb::make_array_view (section->buffer, section->size);
3165 }
3166
3167 /* Lookup the index cache for the contents of the index associated to
3168 DWARF2_OBJ. */
3169
3170 static gdb::array_view<const gdb_byte>
3171 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
3172 {
3173 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
3174 if (build_id == nullptr)
3175 return {};
3176
3177 return global_index_cache.lookup_gdb_index (build_id,
3178 &dwarf2_per_bfd->index_cache_res);
3179 }
3180
3181 /* Same as the above, but for DWZ. */
3182
3183 static gdb::array_view<const gdb_byte>
3184 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
3185 {
3186 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
3187 if (build_id == nullptr)
3188 return {};
3189
3190 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
3191 }
3192
3193 static quick_symbol_functions_up make_cooked_index_funcs
3194 (dwarf2_per_objfile *);
3195
3196 /* See dwarf2/public.h. */
3197
3198 bool
3199 dwarf2_initialize_objfile (struct objfile *objfile,
3200 const struct dwarf2_debug_sections *names,
3201 bool can_copy)
3202 {
3203 if (!dwarf2_has_info (objfile, names, can_copy))
3204 return false;
3205
3206 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3207 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3208
3209 dwarf_read_debug_printf ("called");
3210
3211 /* Try to fetch any potential dwz file early, while still on the
3212 main thread. */
3213 try
3214 {
3215 dwarf2_read_dwz_file (per_objfile);
3216 }
3217 catch (const gdb_exception_error &err)
3218 {
3219 warning (_("%s"), err.what ());
3220 }
3221
3222 /* If we're about to read full symbols, don't bother with the
3223 indices. In this case we also don't care if some other debug
3224 format is making psymtabs, because they are all about to be
3225 expanded anyway. */
3226 if ((objfile->flags & OBJF_READNOW))
3227 {
3228 dwarf_read_debug_printf ("readnow requested");
3229
3230 create_all_units (per_objfile);
3231 per_bfd->quick_file_names_table
3232 = create_quick_file_names_table (per_bfd->all_units.size ());
3233
3234 objfile->qf.emplace_front (new readnow_functions);
3235 }
3236 /* Was a GDB index already read when we processed an objfile sharing
3237 PER_BFD? */
3238 else if (per_bfd->index_table != nullptr)
3239 {
3240 dwarf_read_debug_printf ("re-using symbols");
3241 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3242 }
3243 else if (dwarf2_read_debug_names (per_objfile))
3244 {
3245 dwarf_read_debug_printf ("found debug names");
3246 objfile->qf.push_front
3247 (per_bfd->index_table->make_quick_functions ());
3248 }
3249 else if (dwarf2_read_gdb_index (per_objfile,
3250 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
3251 get_gdb_index_contents_from_section<dwz_file>))
3252 {
3253 dwarf_read_debug_printf ("found gdb index from file");
3254 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3255 }
3256 /* ... otherwise, try to find the index in the index cache. */
3257 else if (dwarf2_read_gdb_index (per_objfile,
3258 get_gdb_index_contents_from_cache,
3259 get_gdb_index_contents_from_cache_dwz))
3260 {
3261 dwarf_read_debug_printf ("found gdb index from cache");
3262 global_index_cache.hit ();
3263 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3264 }
3265 else
3266 {
3267 global_index_cache.miss ();
3268 objfile->qf.push_front (make_cooked_index_funcs (per_objfile));
3269 }
3270 return true;
3271 }
3272
3273 \f
3274
3275 /* Find the base address of the compilation unit for range lists and
3276 location lists. It will normally be specified by DW_AT_low_pc.
3277 In DWARF-3 draft 4, the base address could be overridden by
3278 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3279 compilation units with discontinuous ranges. */
3280
3281 static void
3282 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3283 {
3284 struct attribute *attr;
3285
3286 cu->base_address.reset ();
3287
3288 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3289 if (attr != nullptr)
3290 cu->base_address = attr->as_address ();
3291 else
3292 {
3293 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3294 if (attr != nullptr)
3295 cu->base_address = attr->as_address ();
3296 }
3297 }
3298
3299 /* Helper function that returns the proper abbrev section for
3300 THIS_CU. */
3301
3302 static struct dwarf2_section_info *
3303 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3304 {
3305 struct dwarf2_section_info *abbrev;
3306 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
3307
3308 if (this_cu->is_dwz)
3309 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
3310 else
3311 abbrev = &per_bfd->abbrev;
3312
3313 return abbrev;
3314 }
3315
3316 /* Fetch the abbreviation table offset from a comp or type unit header. */
3317
3318 static sect_offset
3319 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
3320 struct dwarf2_section_info *section,
3321 sect_offset sect_off)
3322 {
3323 bfd *abfd = section->get_bfd_owner ();
3324 const gdb_byte *info_ptr;
3325 unsigned int initial_length_size, offset_size;
3326 uint16_t version;
3327
3328 section->read (per_objfile->objfile);
3329 info_ptr = section->buffer + to_underlying (sect_off);
3330 read_initial_length (abfd, info_ptr, &initial_length_size);
3331 offset_size = initial_length_size == 4 ? 4 : 8;
3332 info_ptr += initial_length_size;
3333
3334 version = read_2_bytes (abfd, info_ptr);
3335 info_ptr += 2;
3336 if (version >= 5)
3337 {
3338 /* Skip unit type and address size. */
3339 info_ptr += 2;
3340 }
3341
3342 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
3343 }
3344
3345 static hashval_t
3346 hash_signatured_type (const void *item)
3347 {
3348 const struct signatured_type *sig_type
3349 = (const struct signatured_type *) item;
3350
3351 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3352 return sig_type->signature;
3353 }
3354
3355 static int
3356 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3357 {
3358 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
3359 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
3360
3361 return lhs->signature == rhs->signature;
3362 }
3363
3364 /* See read.h. */
3365
3366 htab_up
3367 allocate_signatured_type_table ()
3368 {
3369 return htab_up (htab_create_alloc (41,
3370 hash_signatured_type,
3371 eq_signatured_type,
3372 NULL, xcalloc, xfree));
3373 }
3374
3375 /* A helper for create_debug_types_hash_table. Read types from SECTION
3376 and fill them into TYPES_HTAB. It will process only type units,
3377 therefore DW_UT_type. */
3378
3379 static void
3380 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
3381 struct dwo_file *dwo_file,
3382 dwarf2_section_info *section, htab_up &types_htab,
3383 rcuh_kind section_kind)
3384 {
3385 struct objfile *objfile = per_objfile->objfile;
3386 struct dwarf2_section_info *abbrev_section;
3387 bfd *abfd;
3388 const gdb_byte *info_ptr, *end_ptr;
3389
3390 abbrev_section = &dwo_file->sections.abbrev;
3391
3392 dwarf_read_debug_printf ("Reading %s for %s",
3393 section->get_name (),
3394 abbrev_section->get_file_name ());
3395
3396 section->read (objfile);
3397 info_ptr = section->buffer;
3398
3399 if (info_ptr == NULL)
3400 return;
3401
3402 /* We can't set abfd until now because the section may be empty or
3403 not present, in which case the bfd is unknown. */
3404 abfd = section->get_bfd_owner ();
3405
3406 /* We don't use cutu_reader here because we don't need to read
3407 any dies: the signature is in the header. */
3408
3409 end_ptr = info_ptr + section->size;
3410 while (info_ptr < end_ptr)
3411 {
3412 signatured_type_up sig_type;
3413 struct dwo_unit *dwo_tu;
3414 void **slot;
3415 const gdb_byte *ptr = info_ptr;
3416 struct comp_unit_head header;
3417 unsigned int length;
3418
3419 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
3420
3421 /* Initialize it due to a false compiler warning. */
3422 header.signature = -1;
3423 header.type_cu_offset_in_tu = (cu_offset) -1;
3424
3425 /* We need to read the type's signature in order to build the hash
3426 table, but we don't need anything else just yet. */
3427
3428 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
3429 abbrev_section, ptr, section_kind);
3430
3431 length = header.get_length_with_initial ();
3432
3433 /* Skip dummy type units. */
3434 if (ptr >= info_ptr + length
3435 || peek_abbrev_code (abfd, ptr) == 0
3436 || (header.unit_type != DW_UT_type
3437 && header.unit_type != DW_UT_split_type))
3438 {
3439 info_ptr += length;
3440 continue;
3441 }
3442
3443 if (types_htab == NULL)
3444 types_htab = allocate_dwo_unit_table ();
3445
3446 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
3447 dwo_tu->dwo_file = dwo_file;
3448 dwo_tu->signature = header.signature;
3449 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
3450 dwo_tu->section = section;
3451 dwo_tu->sect_off = sect_off;
3452 dwo_tu->length = length;
3453
3454 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
3455 gdb_assert (slot != NULL);
3456 if (*slot != NULL)
3457 complaint (_("debug type entry at offset %s is duplicate to"
3458 " the entry at offset %s, signature %s"),
3459 sect_offset_str (sect_off),
3460 sect_offset_str (dwo_tu->sect_off),
3461 hex_string (header.signature));
3462 *slot = dwo_tu;
3463
3464 dwarf_read_debug_printf_v (" offset %s, signature %s",
3465 sect_offset_str (sect_off),
3466 hex_string (header.signature));
3467
3468 info_ptr += length;
3469 }
3470 }
3471
3472 /* Create the hash table of all entries in the .debug_types
3473 (or .debug_types.dwo) section(s).
3474 DWO_FILE is a pointer to the DWO file object.
3475
3476 The result is a pointer to the hash table or NULL if there are no types.
3477
3478 Note: This function processes DWO files only, not DWP files. */
3479
3480 static void
3481 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
3482 struct dwo_file *dwo_file,
3483 gdb::array_view<dwarf2_section_info> type_sections,
3484 htab_up &types_htab)
3485 {
3486 for (dwarf2_section_info &section : type_sections)
3487 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
3488 rcuh_kind::TYPE);
3489 }
3490
3491 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
3492 If SLOT is non-NULL, it is the entry to use in the hash table.
3493 Otherwise we find one. */
3494
3495 static struct signatured_type *
3496 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
3497 {
3498 if (per_objfile->per_bfd->all_units.size ()
3499 == per_objfile->per_bfd->all_units.capacity ())
3500 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
3501
3502 signatured_type_up sig_type_holder
3503 = per_objfile->per_bfd->allocate_signatured_type (sig);
3504 signatured_type *sig_type = sig_type_holder.get ();
3505
3506 per_objfile->per_bfd->all_units.emplace_back
3507 (sig_type_holder.release ());
3508
3509 if (slot == NULL)
3510 {
3511 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3512 sig_type, INSERT);
3513 }
3514 gdb_assert (*slot == NULL);
3515 *slot = sig_type;
3516 /* The rest of sig_type must be filled in by the caller. */
3517 return sig_type;
3518 }
3519
3520 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
3521 Fill in SIG_ENTRY with DWO_ENTRY. */
3522
3523 static void
3524 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
3525 struct signatured_type *sig_entry,
3526 struct dwo_unit *dwo_entry)
3527 {
3528 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3529
3530 /* Make sure we're not clobbering something we don't expect to. */
3531 gdb_assert (! sig_entry->queued);
3532 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
3533 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
3534 gdb_assert (sig_entry->signature == dwo_entry->signature);
3535 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
3536 || (to_underlying (sig_entry->type_offset_in_section)
3537 == to_underlying (dwo_entry->type_offset_in_tu)));
3538 gdb_assert (sig_entry->type_unit_group == NULL);
3539 gdb_assert (sig_entry->dwo_unit == NULL
3540 || sig_entry->dwo_unit == dwo_entry);
3541
3542 sig_entry->section = dwo_entry->section;
3543 sig_entry->sect_off = dwo_entry->sect_off;
3544 sig_entry->set_length (dwo_entry->length, false);
3545 sig_entry->reading_dwo_directly = 1;
3546 sig_entry->per_bfd = per_bfd;
3547 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
3548 sig_entry->dwo_unit = dwo_entry;
3549 }
3550
3551 /* Subroutine of lookup_signatured_type.
3552 If we haven't read the TU yet, create the signatured_type data structure
3553 for a TU to be read in directly from a DWO file, bypassing the stub.
3554 This is the "Stay in DWO Optimization": When there is no DWP file and we're
3555 using .gdb_index, then when reading a CU we want to stay in the DWO file
3556 containing that CU. Otherwise we could end up reading several other DWO
3557 files (due to comdat folding) to process the transitive closure of all the
3558 mentioned TUs, and that can be slow. The current DWO file will have every
3559 type signature that it needs.
3560 We only do this for .gdb_index because in the psymtab case we already have
3561 to read all the DWOs to build the type unit groups. */
3562
3563 static struct signatured_type *
3564 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3565 {
3566 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3567 struct dwo_file *dwo_file;
3568 struct dwo_unit find_dwo_entry, *dwo_entry;
3569 void **slot;
3570
3571 gdb_assert (cu->dwo_unit);
3572
3573 /* If TU skeletons have been removed then we may not have read in any
3574 TUs yet. */
3575 if (per_objfile->per_bfd->signatured_types == NULL)
3576 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
3577
3578 /* We only ever need to read in one copy of a signatured type.
3579 Use the global signatured_types array to do our own comdat-folding
3580 of types. If this is the first time we're reading this TU, and
3581 the TU has an entry in .gdb_index, replace the recorded data from
3582 .gdb_index with this TU. */
3583
3584 signatured_type find_sig_entry (sig);
3585 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3586 &find_sig_entry, INSERT);
3587 signatured_type *sig_entry = (struct signatured_type *) *slot;
3588
3589 /* We can get here with the TU already read, *or* in the process of being
3590 read. Don't reassign the global entry to point to this DWO if that's
3591 the case. Also note that if the TU is already being read, it may not
3592 have come from a DWO, the program may be a mix of Fission-compiled
3593 code and non-Fission-compiled code. */
3594
3595 /* Have we already tried to read this TU?
3596 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3597 needn't exist in the global table yet). */
3598 if (sig_entry != NULL && sig_entry->tu_read)
3599 return sig_entry;
3600
3601 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
3602 dwo_unit of the TU itself. */
3603 dwo_file = cu->dwo_unit->dwo_file;
3604
3605 /* Ok, this is the first time we're reading this TU. */
3606 if (dwo_file->tus == NULL)
3607 return NULL;
3608 find_dwo_entry.signature = sig;
3609 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
3610 &find_dwo_entry);
3611 if (dwo_entry == NULL)
3612 return NULL;
3613
3614 /* If the global table doesn't have an entry for this TU, add one. */
3615 if (sig_entry == NULL)
3616 sig_entry = add_type_unit (per_objfile, sig, slot);
3617
3618 if (sig_entry->dwo_unit == nullptr)
3619 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
3620 sig_entry->tu_read = 1;
3621 return sig_entry;
3622 }
3623
3624 /* Subroutine of lookup_signatured_type.
3625 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
3626 then try the DWP file. If the TU stub (skeleton) has been removed then
3627 it won't be in .gdb_index. */
3628
3629 static struct signatured_type *
3630 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3631 {
3632 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3633 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
3634 struct dwo_unit *dwo_entry;
3635 void **slot;
3636
3637 gdb_assert (cu->dwo_unit);
3638 gdb_assert (dwp_file != NULL);
3639
3640 /* If TU skeletons have been removed then we may not have read in any
3641 TUs yet. */
3642 if (per_objfile->per_bfd->signatured_types == NULL)
3643 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
3644
3645 signatured_type find_sig_entry (sig);
3646 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3647 &find_sig_entry, INSERT);
3648 signatured_type *sig_entry = (struct signatured_type *) *slot;
3649
3650 /* Have we already tried to read this TU?
3651 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3652 needn't exist in the global table yet). */
3653 if (sig_entry != NULL)
3654 return sig_entry;
3655
3656 if (dwp_file->tus == NULL)
3657 return NULL;
3658 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
3659 1 /* is_debug_types */);
3660 if (dwo_entry == NULL)
3661 return NULL;
3662
3663 sig_entry = add_type_unit (per_objfile, sig, slot);
3664 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
3665
3666 return sig_entry;
3667 }
3668
3669 /* Lookup a signature based type for DW_FORM_ref_sig8.
3670 Returns NULL if signature SIG is not present in the table.
3671 It is up to the caller to complain about this. */
3672
3673 static struct signatured_type *
3674 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3675 {
3676 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3677
3678 if (cu->dwo_unit)
3679 {
3680 /* We're in a DWO/DWP file, and we're using .gdb_index.
3681 These cases require special processing. */
3682 if (get_dwp_file (per_objfile) == NULL)
3683 return lookup_dwo_signatured_type (cu, sig);
3684 else
3685 return lookup_dwp_signatured_type (cu, sig);
3686 }
3687 else
3688 {
3689 if (per_objfile->per_bfd->signatured_types == NULL)
3690 return NULL;
3691 signatured_type find_entry (sig);
3692 return ((struct signatured_type *)
3693 htab_find (per_objfile->per_bfd->signatured_types.get (),
3694 &find_entry));
3695 }
3696 }
3697
3698 /* Low level DIE reading support. */
3699
3700 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3701
3702 static void
3703 init_cu_die_reader (struct die_reader_specs *reader,
3704 struct dwarf2_cu *cu,
3705 struct dwarf2_section_info *section,
3706 struct dwo_file *dwo_file,
3707 struct abbrev_table *abbrev_table)
3708 {
3709 gdb_assert (section->readin && section->buffer != NULL);
3710 reader->abfd = section->get_bfd_owner ();
3711 reader->cu = cu;
3712 reader->dwo_file = dwo_file;
3713 reader->die_section = section;
3714 reader->buffer = section->buffer;
3715 reader->buffer_end = section->buffer + section->size;
3716 reader->abbrev_table = abbrev_table;
3717 }
3718
3719 /* Subroutine of cutu_reader to simplify it.
3720 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
3721 There's just a lot of work to do, and cutu_reader is big enough
3722 already.
3723
3724 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
3725 from it to the DIE in the DWO. If NULL we are skipping the stub.
3726 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
3727 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
3728 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
3729 STUB_COMP_DIR may be non-NULL.
3730 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
3731 are filled in with the info of the DIE from the DWO file.
3732 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
3733 from the dwo. Since *RESULT_READER references this abbrev table, it must be
3734 kept around for at least as long as *RESULT_READER.
3735
3736 The result is non-zero if a valid (non-dummy) DIE was found. */
3737
3738 static int
3739 read_cutu_die_from_dwo (dwarf2_cu *cu,
3740 struct dwo_unit *dwo_unit,
3741 struct die_info *stub_comp_unit_die,
3742 const char *stub_comp_dir,
3743 struct die_reader_specs *result_reader,
3744 const gdb_byte **result_info_ptr,
3745 struct die_info **result_comp_unit_die,
3746 abbrev_table_up *result_dwo_abbrev_table)
3747 {
3748 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3749 dwarf2_per_cu_data *per_cu = cu->per_cu;
3750 struct objfile *objfile = per_objfile->objfile;
3751 bfd *abfd;
3752 const gdb_byte *begin_info_ptr, *info_ptr;
3753 struct dwarf2_section_info *dwo_abbrev_section;
3754
3755 /* At most one of these may be provided. */
3756 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
3757
3758 /* These attributes aren't processed until later: DW_AT_stmt_list,
3759 DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_comp_dir.
3760 However, these attributes are found in the stub which we won't
3761 have later. In order to not impose this complication on the rest
3762 of the code, we read them here and copy them to the DWO CU/TU
3763 die. */
3764
3765 /* We store them all in an array. */
3766 struct attribute *attributes[5] {};
3767 /* Next available element of the attributes array. */
3768 int next_attr_idx = 0;
3769
3770 /* Push an element into ATTRIBUTES. */
3771 auto push_back = [&] (struct attribute *attr)
3772 {
3773 gdb_assert (next_attr_idx < ARRAY_SIZE (attributes));
3774 if (attr != nullptr)
3775 attributes[next_attr_idx++] = attr;
3776 };
3777
3778 if (stub_comp_unit_die != NULL)
3779 {
3780 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3781 DWO file. */
3782 if (!per_cu->is_debug_types)
3783 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu));
3784 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu));
3785 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu));
3786 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu));
3787 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu));
3788
3789 cu->addr_base = stub_comp_unit_die->addr_base ();
3790
3791 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
3792 We need the value before we can process DW_AT_ranges values from the
3793 DWO. */
3794 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
3795
3796 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
3797 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
3798 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
3799 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
3800 section. */
3801 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
3802 }
3803 else if (stub_comp_dir != NULL)
3804 {
3805 /* Reconstruct the comp_dir attribute to simplify the code below. */
3806 struct attribute *comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack,
3807 struct attribute);
3808 comp_dir->name = DW_AT_comp_dir;
3809 comp_dir->form = DW_FORM_string;
3810 comp_dir->set_string_noncanonical (stub_comp_dir);
3811 push_back (comp_dir);
3812 }
3813
3814 /* Set up for reading the DWO CU/TU. */
3815 cu->dwo_unit = dwo_unit;
3816 dwarf2_section_info *section = dwo_unit->section;
3817 section->read (objfile);
3818 abfd = section->get_bfd_owner ();
3819 begin_info_ptr = info_ptr = (section->buffer
3820 + to_underlying (dwo_unit->sect_off));
3821 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3822
3823 if (per_cu->is_debug_types)
3824 {
3825 signatured_type *sig_type = (struct signatured_type *) per_cu;
3826
3827 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3828 section, dwo_abbrev_section,
3829 info_ptr, rcuh_kind::TYPE);
3830 /* This is not an assert because it can be caused by bad debug info. */
3831 if (sig_type->signature != cu->header.signature)
3832 {
3833 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
3834 " TU at offset %s [in module %s]"),
3835 hex_string (sig_type->signature),
3836 hex_string (cu->header.signature),
3837 sect_offset_str (dwo_unit->sect_off),
3838 bfd_get_filename (abfd));
3839 }
3840 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
3841 /* For DWOs coming from DWP files, we don't know the CU length
3842 nor the type's offset in the TU until now. */
3843 dwo_unit->length = cu->header.get_length_with_initial ();
3844 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
3845
3846 /* Establish the type offset that can be used to lookup the type.
3847 For DWO files, we don't know it until now. */
3848 sig_type->type_offset_in_section
3849 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
3850 }
3851 else
3852 {
3853 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3854 section, dwo_abbrev_section,
3855 info_ptr, rcuh_kind::COMPILE);
3856 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
3857 /* For DWOs coming from DWP files, we don't know the CU length
3858 until now. */
3859 dwo_unit->length = cu->header.get_length_with_initial ();
3860 }
3861
3862 dwo_abbrev_section->read (objfile);
3863 *result_dwo_abbrev_table
3864 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
3865 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
3866 result_dwo_abbrev_table->get ());
3867
3868 /* Read in the die, filling in the attributes from the stub. This
3869 has the benefit of simplifying the rest of the code - all the
3870 work to maintain the illusion of a single
3871 DW_TAG_{compile,type}_unit DIE is done here. */
3872 info_ptr = read_toplevel_die (result_reader, result_comp_unit_die, info_ptr,
3873 gdb::make_array_view (attributes,
3874 next_attr_idx));
3875
3876 /* Skip dummy compilation units. */
3877 if (info_ptr >= begin_info_ptr + dwo_unit->length
3878 || peek_abbrev_code (abfd, info_ptr) == 0)
3879 return 0;
3880
3881 *result_info_ptr = info_ptr;
3882 return 1;
3883 }
3884
3885 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
3886 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
3887 signature is part of the header. */
3888 static std::optional<ULONGEST>
3889 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
3890 {
3891 if (cu->header.version >= 5)
3892 return cu->header.signature;
3893 struct attribute *attr;
3894 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3895 if (attr == nullptr || !attr->form_is_unsigned ())
3896 return std::optional<ULONGEST> ();
3897 return attr->as_unsigned ();
3898 }
3899
3900 /* Subroutine of cutu_reader to simplify it.
3901 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
3902 Returns NULL if the specified DWO unit cannot be found. */
3903
3904 static struct dwo_unit *
3905 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
3906 {
3907 #if CXX_STD_THREAD
3908 /* We need a lock here to handle the DWO hash table. */
3909 static std::mutex dwo_lock;
3910
3911 std::lock_guard<std::mutex> guard (dwo_lock);
3912 #endif
3913
3914 dwarf2_per_cu_data *per_cu = cu->per_cu;
3915 struct dwo_unit *dwo_unit;
3916 const char *comp_dir;
3917
3918 gdb_assert (cu != NULL);
3919
3920 /* Yeah, we look dwo_name up again, but it simplifies the code. */
3921 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
3922 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
3923
3924 if (per_cu->is_debug_types)
3925 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
3926 else
3927 {
3928 std::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
3929
3930 if (!signature.has_value ())
3931 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
3932 " [in module %s]"),
3933 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
3934
3935 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
3936 }
3937
3938 return dwo_unit;
3939 }
3940
3941 /* Subroutine of cutu_reader to simplify it.
3942 See it for a description of the parameters.
3943 Read a TU directly from a DWO file, bypassing the stub. */
3944
3945 void
3946 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
3947 dwarf2_per_objfile *per_objfile,
3948 dwarf2_cu *existing_cu)
3949 {
3950 struct signatured_type *sig_type;
3951
3952 /* Verify we can do the following downcast, and that we have the
3953 data we need. */
3954 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
3955 sig_type = (struct signatured_type *) this_cu;
3956 gdb_assert (sig_type->dwo_unit != NULL);
3957
3958 dwarf2_cu *cu;
3959
3960 if (existing_cu != nullptr)
3961 {
3962 cu = existing_cu;
3963 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
3964 /* There's no need to do the rereading_dwo_cu handling that
3965 cutu_reader does since we don't read the stub. */
3966 }
3967 else
3968 {
3969 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
3970 in per_objfile yet. */
3971 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
3972 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
3973 cu = m_new_cu.get ();
3974 }
3975
3976 /* A future optimization, if needed, would be to use an existing
3977 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
3978 could share abbrev tables. */
3979
3980 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
3981 NULL /* stub_comp_unit_die */,
3982 sig_type->dwo_unit->dwo_file->comp_dir,
3983 this, &info_ptr,
3984 &comp_unit_die,
3985 &m_dwo_abbrev_table) == 0)
3986 {
3987 /* Dummy die. */
3988 dummy_p = true;
3989 }
3990 }
3991
3992 /* Initialize a CU (or TU) and read its DIEs.
3993 If the CU defers to a DWO file, read the DWO file as well.
3994
3995 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
3996 Otherwise the table specified in the comp unit header is read in and used.
3997 This is an optimization for when we already have the abbrev table.
3998
3999 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
4000 allocated. */
4001
4002 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
4003 dwarf2_per_objfile *per_objfile,
4004 struct abbrev_table *abbrev_table,
4005 dwarf2_cu *existing_cu,
4006 bool skip_partial,
4007 abbrev_cache *cache)
4008 : die_reader_specs {},
4009 m_this_cu (this_cu)
4010 {
4011 struct objfile *objfile = per_objfile->objfile;
4012 struct dwarf2_section_info *section = this_cu->section;
4013 bfd *abfd = section->get_bfd_owner ();
4014 const gdb_byte *begin_info_ptr;
4015 struct signatured_type *sig_type = NULL;
4016 struct dwarf2_section_info *abbrev_section;
4017 /* Non-zero if CU currently points to a DWO file and we need to
4018 reread it. When this happens we need to reread the skeleton die
4019 before we can reread the DWO file (this only applies to CUs, not TUs). */
4020 int rereading_dwo_cu = 0;
4021
4022 if (dwarf_die_debug)
4023 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
4024 this_cu->is_debug_types ? "type" : "comp",
4025 sect_offset_str (this_cu->sect_off));
4026
4027 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4028 file (instead of going through the stub), short-circuit all of this. */
4029 if (this_cu->reading_dwo_directly)
4030 {
4031 /* Narrow down the scope of possibilities to have to understand. */
4032 gdb_assert (this_cu->is_debug_types);
4033 gdb_assert (abbrev_table == NULL);
4034 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
4035 return;
4036 }
4037
4038 /* This is cheap if the section is already read in. */
4039 section->read (objfile);
4040
4041 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4042
4043 abbrev_section = get_abbrev_section_for_cu (this_cu);
4044
4045 dwarf2_cu *cu;
4046
4047 if (existing_cu != nullptr)
4048 {
4049 cu = existing_cu;
4050 /* If this CU is from a DWO file we need to start over, we need to
4051 refetch the attributes from the skeleton CU.
4052 This could be optimized by retrieving those attributes from when we
4053 were here the first time: the previous comp_unit_die was stored in
4054 comp_unit_obstack. But there's no data yet that we need this
4055 optimization. */
4056 if (cu->dwo_unit != NULL)
4057 rereading_dwo_cu = 1;
4058 }
4059 else
4060 {
4061 /* If an existing_cu is provided, a dwarf2_cu must not exist for
4062 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
4063 let us know that the CU is being scanned using the parallel
4064 indexer. This assert is avoided in this case because (1) it
4065 is irrelevant, and (2) the get_cu method is not
4066 thread-safe. */
4067 gdb_assert (cache != nullptr
4068 || per_objfile->get_cu (this_cu) == nullptr);
4069 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4070 cu = m_new_cu.get ();
4071 }
4072
4073 /* Get the header. */
4074 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
4075 {
4076 /* We already have the header, there's no need to read it in again. */
4077 info_ptr += to_underlying (cu->header.first_die_cu_offset);
4078 }
4079 else
4080 {
4081 if (this_cu->is_debug_types)
4082 {
4083 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4084 section, abbrev_section,
4085 info_ptr, rcuh_kind::TYPE);
4086
4087 /* Since per_cu is the first member of struct signatured_type,
4088 we can go from a pointer to one to a pointer to the other. */
4089 sig_type = (struct signatured_type *) this_cu;
4090 gdb_assert (sig_type->signature == cu->header.signature);
4091 gdb_assert (sig_type->type_offset_in_tu
4092 == cu->header.type_cu_offset_in_tu);
4093 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4094
4095 /* LENGTH has not been set yet for type units if we're
4096 using .gdb_index. */
4097 this_cu->set_length (cu->header.get_length_with_initial ());
4098
4099 /* Establish the type offset that can be used to lookup the type. */
4100 sig_type->type_offset_in_section =
4101 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
4102
4103 this_cu->set_version (cu->header.version);
4104 }
4105 else
4106 {
4107 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4108 section, abbrev_section,
4109 info_ptr,
4110 rcuh_kind::COMPILE);
4111
4112 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4113 this_cu->set_length (cu->header.get_length_with_initial ());
4114 this_cu->set_version (cu->header.version);
4115 }
4116 }
4117
4118 /* Skip dummy compilation units. */
4119 if (info_ptr >= begin_info_ptr + this_cu->length ()
4120 || peek_abbrev_code (abfd, info_ptr) == 0)
4121 {
4122 dummy_p = true;
4123 return;
4124 }
4125
4126 /* If we don't have them yet, read the abbrevs for this compilation unit.
4127 And if we need to read them now, make sure they're freed when we're
4128 done. */
4129 if (abbrev_table != NULL)
4130 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
4131 else
4132 {
4133 if (cache != nullptr)
4134 abbrev_table = cache->find (abbrev_section,
4135 cu->header.abbrev_sect_off);
4136 if (abbrev_table == nullptr)
4137 {
4138 abbrev_section->read (objfile);
4139 m_abbrev_table_holder
4140 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
4141 abbrev_table = m_abbrev_table_holder.get ();
4142 }
4143 }
4144
4145 /* Read the top level CU/TU die. */
4146 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
4147 info_ptr = read_toplevel_die (this, &comp_unit_die, info_ptr);
4148
4149 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
4150 {
4151 dummy_p = true;
4152 return;
4153 }
4154
4155 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4156 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
4157 table from the DWO file and pass the ownership over to us. It will be
4158 referenced from READER, so we must make sure to free it after we're done
4159 with READER.
4160
4161 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4162 DWO CU, that this test will fail (the attribute will not be present). */
4163 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
4164 if (dwo_name != nullptr)
4165 {
4166 struct dwo_unit *dwo_unit;
4167 struct die_info *dwo_comp_unit_die;
4168
4169 if (comp_unit_die->has_children)
4170 {
4171 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
4172 " has children (offset %s) [in module %s]"),
4173 sect_offset_str (this_cu->sect_off),
4174 bfd_get_filename (abfd));
4175 }
4176 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
4177 if (dwo_unit != NULL)
4178 {
4179 if (read_cutu_die_from_dwo (cu, dwo_unit,
4180 comp_unit_die, NULL,
4181 this, &info_ptr,
4182 &dwo_comp_unit_die,
4183 &m_dwo_abbrev_table) == 0)
4184 {
4185 /* Dummy die. */
4186 dummy_p = true;
4187 return;
4188 }
4189 comp_unit_die = dwo_comp_unit_die;
4190 }
4191 else
4192 {
4193 /* Yikes, we couldn't find the rest of the DIE, we only have
4194 the stub. A complaint has already been logged. There's
4195 not much more we can do except pass on the stub DIE to
4196 die_reader_func. We don't want to throw an error on bad
4197 debug info. */
4198 }
4199 }
4200 }
4201
4202 void
4203 cutu_reader::keep ()
4204 {
4205 /* Done, clean up. */
4206 gdb_assert (!dummy_p);
4207 if (m_new_cu != NULL)
4208 {
4209 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
4210 now. */
4211 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
4212 per_objfile->set_cu (m_this_cu, std::move (m_new_cu));
4213 }
4214 }
4215
4216 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
4217 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
4218 assumed to have already done the lookup to find the DWO file).
4219
4220 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4221 THIS_CU->is_debug_types, but nothing else.
4222
4223 We fill in THIS_CU->length.
4224
4225 THIS_CU->cu is always freed when done.
4226 This is done in order to not leave THIS_CU->cu in a state where we have
4227 to care whether it refers to the "main" CU or the DWO CU.
4228
4229 When parent_cu is passed, it is used to provide a default value for
4230 str_offsets_base and addr_base from the parent. */
4231
4232 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
4233 dwarf2_per_objfile *per_objfile,
4234 struct dwarf2_cu *parent_cu,
4235 struct dwo_file *dwo_file)
4236 : die_reader_specs {},
4237 m_this_cu (this_cu)
4238 {
4239 struct objfile *objfile = per_objfile->objfile;
4240 struct dwarf2_section_info *section = this_cu->section;
4241 bfd *abfd = section->get_bfd_owner ();
4242 struct dwarf2_section_info *abbrev_section;
4243 const gdb_byte *begin_info_ptr, *info_ptr;
4244
4245 if (dwarf_die_debug)
4246 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
4247 this_cu->is_debug_types ? "type" : "comp",
4248 sect_offset_str (this_cu->sect_off));
4249
4250 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
4251
4252 abbrev_section = (dwo_file != NULL
4253 ? &dwo_file->sections.abbrev
4254 : get_abbrev_section_for_cu (this_cu));
4255
4256 /* This is cheap if the section is already read in. */
4257 section->read (objfile);
4258
4259 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4260
4261 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4262 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
4263 section, abbrev_section, info_ptr,
4264 (this_cu->is_debug_types
4265 ? rcuh_kind::TYPE
4266 : rcuh_kind::COMPILE));
4267
4268 if (parent_cu != nullptr)
4269 {
4270 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
4271 m_new_cu->addr_base = parent_cu->addr_base;
4272 }
4273 this_cu->set_length (m_new_cu->header.get_length_with_initial ());
4274
4275 /* Skip dummy compilation units. */
4276 if (info_ptr >= begin_info_ptr + this_cu->length ()
4277 || peek_abbrev_code (abfd, info_ptr) == 0)
4278 {
4279 dummy_p = true;
4280 return;
4281 }
4282
4283 abbrev_section->read (objfile);
4284 m_abbrev_table_holder
4285 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
4286
4287 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
4288 m_abbrev_table_holder.get ());
4289 info_ptr = read_toplevel_die (this, &comp_unit_die, info_ptr);
4290 }
4291
4292 \f
4293 /* Type Unit Groups.
4294
4295 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4296 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4297 so that all types coming from the same compilation (.o file) are grouped
4298 together. A future step could be to put the types in the same symtab as
4299 the CU the types ultimately came from. */
4300
4301 static hashval_t
4302 hash_type_unit_group (const void *item)
4303 {
4304 const struct type_unit_group *tu_group
4305 = (const struct type_unit_group *) item;
4306
4307 return hash_stmt_list_entry (&tu_group->hash);
4308 }
4309
4310 static int
4311 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4312 {
4313 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
4314 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
4315
4316 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4317 }
4318
4319 /* Allocate a hash table for type unit groups. */
4320
4321 static htab_up
4322 allocate_type_unit_groups_table ()
4323 {
4324 return htab_up (htab_create_alloc (3,
4325 hash_type_unit_group,
4326 eq_type_unit_group,
4327 htab_delete_entry<type_unit_group>,
4328 xcalloc, xfree));
4329 }
4330
4331 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4332 partial symtabs. We combine several TUs per psymtab to not let the size
4333 of any one psymtab grow too big. */
4334 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4335 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4336
4337 /* Helper routine for get_type_unit_group.
4338 Create the type_unit_group object used to hold one or more TUs. */
4339
4340 static std::unique_ptr<type_unit_group>
4341 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4342 {
4343 auto tu_group = std::make_unique<type_unit_group> ();
4344
4345 tu_group->hash.dwo_unit = cu->dwo_unit;
4346 tu_group->hash.line_sect_off = line_offset_struct;
4347
4348 return tu_group;
4349 }
4350
4351 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4352 STMT_LIST is a DW_AT_stmt_list attribute. */
4353
4354 static struct type_unit_group *
4355 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
4356 {
4357 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4358 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4359 struct type_unit_group *tu_group;
4360 void **slot;
4361 unsigned int line_offset;
4362 struct type_unit_group type_unit_group_for_lookup;
4363
4364 if (per_objfile->per_bfd->type_unit_groups == NULL)
4365 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
4366
4367 /* Do we need to create a new group, or can we use an existing one? */
4368
4369 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
4370 {
4371 line_offset = stmt_list->as_unsigned ();
4372 ++tu_stats->nr_symtab_sharers;
4373 }
4374 else
4375 {
4376 /* Ugh, no stmt_list. Rare, but we have to handle it.
4377 We can do various things here like create one group per TU or
4378 spread them over multiple groups to split up the expansion work.
4379 To avoid worst case scenarios (too many groups or too large groups)
4380 we, umm, group them in bunches. */
4381 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
4382 | (tu_stats->nr_stmt_less_type_units
4383 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
4384 ++tu_stats->nr_stmt_less_type_units;
4385 }
4386
4387 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
4388 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
4389 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
4390 &type_unit_group_for_lookup, INSERT);
4391 if (*slot == nullptr)
4392 {
4393 sect_offset line_offset_struct = (sect_offset) line_offset;
4394 std::unique_ptr<type_unit_group> grp
4395 = create_type_unit_group (cu, line_offset_struct);
4396 *slot = grp.release ();
4397 ++tu_stats->nr_symtabs;
4398 }
4399
4400 tu_group = (struct type_unit_group *) *slot;
4401 gdb_assert (tu_group != nullptr);
4402 return tu_group;
4403 }
4404 \f
4405
4406 cooked_index_storage::cooked_index_storage ()
4407 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
4408 eq_cutu_reader,
4409 htab_delete_entry<cutu_reader>,
4410 xcalloc, xfree)),
4411 m_index (new cooked_index_shard)
4412 {
4413 }
4414
4415 cutu_reader *
4416 cooked_index_storage::get_reader (dwarf2_per_cu_data *per_cu)
4417 {
4418 int index = per_cu->index;
4419 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
4420 &index, index);
4421 }
4422
4423 cutu_reader *
4424 cooked_index_storage::preserve (std::unique_ptr<cutu_reader> reader)
4425 {
4426 m_abbrev_cache.add (reader->release_abbrev_table ());
4427
4428 int index = reader->cu->per_cu->index;
4429 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
4430 index, INSERT);
4431 gdb_assert (*slot == nullptr);
4432 cutu_reader *result = reader.get ();
4433 *slot = reader.release ();
4434 return result;
4435 }
4436
4437 /* Hash function for a cutu_reader. */
4438 hashval_t
4439 cooked_index_storage::hash_cutu_reader (const void *a)
4440 {
4441 const cutu_reader *reader = (const cutu_reader *) a;
4442 return reader->cu->per_cu->index;
4443 }
4444
4445 /* Equality function for cutu_reader. */
4446 int
4447 cooked_index_storage::eq_cutu_reader (const void *a, const void *b)
4448 {
4449 const cutu_reader *ra = (const cutu_reader *) a;
4450 const int *rb = (const int *) b;
4451 return ra->cu->per_cu->index == *rb;
4452 }
4453
4454 /* An instance of this is created to index a CU. */
4455
4456 class cooked_indexer
4457 {
4458 public:
4459
4460 cooked_indexer (cooked_index_storage *storage,
4461 dwarf2_per_cu_data *per_cu,
4462 enum language language)
4463 : m_index_storage (storage),
4464 m_per_cu (per_cu),
4465 m_language (language)
4466 {
4467 }
4468
4469 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
4470
4471 /* Index the given CU. */
4472 void make_index (cutu_reader *reader);
4473
4474 private:
4475
4476 /* A helper function to turn a section offset into an address that
4477 can be used in an addrmap. */
4478 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
4479 {
4480 CORE_ADDR value = to_underlying (offset);
4481 if (is_dwz)
4482 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
4483 return value;
4484 }
4485
4486 /* A helper function to scan the PC bounds of READER and record them
4487 in the storage's addrmap. */
4488 void check_bounds (cutu_reader *reader);
4489
4490 /* Ensure that the indicated CU exists. The cutu_reader for it is
4491 returned. FOR_SCANNING is true if the caller intends to scan all
4492 the DIEs in the CU; when false, this use is assumed to be to look
4493 up just a single DIE. */
4494 cutu_reader *ensure_cu_exists (cutu_reader *reader,
4495 dwarf2_per_objfile *per_objfile,
4496 sect_offset sect_off,
4497 bool is_dwz,
4498 bool for_scanning);
4499
4500 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
4501 the entry for the enclosing scope (nullptr at top level). FULLY
4502 is true when a full scan must be done -- in some languages,
4503 function scopes must be fully explored in order to find nested
4504 functions. This returns a pointer to just after the spot where
4505 reading stopped. */
4506 const gdb_byte *index_dies (cutu_reader *reader,
4507 const gdb_byte *info_ptr,
4508 const cooked_index_entry *parent_entry,
4509 bool fully);
4510
4511 /* Scan the attributes for a given DIE and update the out
4512 parameters. Returns a pointer to the byte after the DIE. */
4513 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
4514 cutu_reader *reader,
4515 const gdb_byte *watermark_ptr,
4516 const gdb_byte *info_ptr,
4517 const abbrev_info *abbrev,
4518 const char **name,
4519 const char **linkage_name,
4520 cooked_index_flag *flags,
4521 sect_offset *sibling_offset,
4522 const cooked_index_entry **parent_entry,
4523 CORE_ADDR *maybe_defer,
4524 bool for_specification);
4525
4526 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
4527 DW_AT_import, and then scanning the referenced CU. Returns a
4528 pointer to the byte after the DIE. */
4529 const gdb_byte *index_imported_unit (cutu_reader *reader,
4530 const gdb_byte *info_ptr,
4531 const abbrev_info *abbrev);
4532
4533 /* Recursively read DIEs, recording the section offsets in
4534 m_die_range_map and then calling index_dies. */
4535 const gdb_byte *recurse (cutu_reader *reader,
4536 const gdb_byte *info_ptr,
4537 const cooked_index_entry *parent_entry,
4538 bool fully);
4539
4540 /* The storage object, where the results are kept. */
4541 cooked_index_storage *m_index_storage;
4542 /* The CU that we are reading on behalf of. This object might be
4543 asked to index one CU but to treat the results as if they come
4544 from some including CU; in this case the including CU would be
4545 recorded here. */
4546 dwarf2_per_cu_data *m_per_cu;
4547 /* The language that we're assuming when reading. */
4548 enum language m_language;
4549
4550 /* An addrmap that maps from section offsets (see the form_addr
4551 method) to newly-created entries. See m_deferred_entries to
4552 understand this. */
4553 addrmap_mutable m_die_range_map;
4554
4555 /* The generated DWARF can sometimes have the declaration for a
4556 method in a class (or perhaps namespace) scope, with the
4557 definition appearing outside this scope... just one of the many
4558 bad things about DWARF. In order to handle this situation, we
4559 defer certain entries until the end of scanning, at which point
4560 we'll know the containing context of all the DIEs that we might
4561 have scanned. This vector stores these deferred entries. */
4562 std::vector<cooked_index_entry *> m_deferred_entries;
4563 };
4564
4565 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4566 Process compilation unit THIS_CU for a psymtab. */
4567
4568 static void
4569 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
4570 dwarf2_per_objfile *per_objfile,
4571 cooked_index_storage *storage)
4572 {
4573 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
4574 storage->get_abbrev_cache ());
4575
4576 if (reader.comp_unit_die == nullptr)
4577 return;
4578
4579 if (reader.dummy_p)
4580 {
4581 /* Nothing. */
4582 }
4583 else if (this_cu->is_debug_types)
4584 build_type_psymtabs_reader (&reader, storage);
4585 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
4586 {
4587 bool nope = false;
4588 if (this_cu->scanned.compare_exchange_strong (nope, true))
4589 {
4590 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
4591 language_minimal);
4592 gdb_assert (storage != nullptr);
4593 cooked_indexer indexer (storage, this_cu, reader.cu->lang ());
4594 indexer.make_index (&reader);
4595 }
4596 }
4597 }
4598
4599 /* Reader function for build_type_psymtabs. */
4600
4601 static void
4602 build_type_psymtabs_reader (cutu_reader *reader,
4603 cooked_index_storage *storage)
4604 {
4605 struct dwarf2_cu *cu = reader->cu;
4606 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4607 struct die_info *type_unit_die = reader->comp_unit_die;
4608
4609 gdb_assert (per_cu->is_debug_types);
4610
4611 if (! type_unit_die->has_children)
4612 return;
4613
4614 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
4615
4616 gdb_assert (storage != nullptr);
4617 cooked_indexer indexer (storage, per_cu, cu->lang ());
4618 indexer.make_index (reader);
4619 }
4620
4621 /* Struct used to sort TUs by their abbreviation table offset. */
4622
4623 struct tu_abbrev_offset
4624 {
4625 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
4626 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
4627 {}
4628
4629 /* This is used when sorting. */
4630 bool operator< (const tu_abbrev_offset &other) const
4631 {
4632 return abbrev_offset < other.abbrev_offset;
4633 }
4634
4635 signatured_type *sig_type;
4636 sect_offset abbrev_offset;
4637 };
4638
4639 /* Efficiently read all the type units.
4640
4641 The efficiency is because we sort TUs by the abbrev table they use and
4642 only read each abbrev table once. In one program there are 200K TUs
4643 sharing 8K abbrev tables.
4644
4645 The main purpose of this function is to support building the
4646 dwarf2_per_objfile->per_bfd->type_unit_groups table.
4647 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
4648 can collapse the search space by grouping them by stmt_list.
4649 The savings can be significant, in the same program from above the 200K TUs
4650 share 8K stmt_list tables.
4651
4652 FUNC is expected to call get_type_unit_group, which will create the
4653 struct type_unit_group if necessary and add it to
4654 dwarf2_per_objfile->per_bfd->type_unit_groups. */
4655
4656 static void
4657 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
4658 cooked_index_storage *storage)
4659 {
4660 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4661 abbrev_table_up abbrev_table;
4662 sect_offset abbrev_offset;
4663
4664 /* It's up to the caller to not call us multiple times. */
4665 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
4666
4667 if (per_objfile->per_bfd->all_type_units.size () == 0)
4668 return;
4669
4670 /* TUs typically share abbrev tables, and there can be way more TUs than
4671 abbrev tables. Sort by abbrev table to reduce the number of times we
4672 read each abbrev table in.
4673 Alternatives are to punt or to maintain a cache of abbrev tables.
4674 This is simpler and efficient enough for now.
4675
4676 Later we group TUs by their DW_AT_stmt_list value (as this defines the
4677 symtab to use). Typically TUs with the same abbrev offset have the same
4678 stmt_list value too so in practice this should work well.
4679
4680 The basic algorithm here is:
4681
4682 sort TUs by abbrev table
4683 for each TU with same abbrev table:
4684 read abbrev table if first user
4685 read TU top level DIE
4686 [IWBN if DWO skeletons had DW_AT_stmt_list]
4687 call FUNC */
4688
4689 dwarf_read_debug_printf ("Building type unit groups ...");
4690
4691 /* Sort in a separate table to maintain the order of all_units
4692 for .gdb_index: TU indices directly index all_type_units. */
4693 std::vector<tu_abbrev_offset> sorted_by_abbrev;
4694 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
4695
4696 for (const auto &cu : per_objfile->per_bfd->all_units)
4697 {
4698 if (cu->is_debug_types)
4699 {
4700 auto sig_type = static_cast<signatured_type *> (cu.get ());
4701 sorted_by_abbrev.emplace_back
4702 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
4703 sig_type->sect_off));
4704 }
4705 }
4706
4707 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
4708
4709 abbrev_offset = (sect_offset) ~(unsigned) 0;
4710
4711 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
4712 {
4713 /* Switch to the next abbrev table if necessary. */
4714 if (abbrev_table == NULL
4715 || tu.abbrev_offset != abbrev_offset)
4716 {
4717 abbrev_offset = tu.abbrev_offset;
4718 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
4719 abbrev_table =
4720 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
4721 ++tu_stats->nr_uniq_abbrev_tables;
4722 }
4723
4724 cutu_reader reader (tu.sig_type, per_objfile,
4725 abbrev_table.get (), nullptr, false);
4726 if (!reader.dummy_p)
4727 build_type_psymtabs_reader (&reader, storage);
4728 }
4729 }
4730
4731 /* Print collected type unit statistics. */
4732
4733 static void
4734 print_tu_stats (dwarf2_per_objfile *per_objfile)
4735 {
4736 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4737
4738 dwarf_read_debug_printf ("Type unit statistics:");
4739 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
4740 dwarf_read_debug_printf (" %d uniq abbrev tables",
4741 tu_stats->nr_uniq_abbrev_tables);
4742 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
4743 tu_stats->nr_symtabs);
4744 dwarf_read_debug_printf (" %d symtab sharers",
4745 tu_stats->nr_symtab_sharers);
4746 dwarf_read_debug_printf (" %d type units without a stmt_list",
4747 tu_stats->nr_stmt_less_type_units);
4748 dwarf_read_debug_printf (" %d all_type_units reallocs",
4749 tu_stats->nr_all_type_units_reallocs);
4750 }
4751
4752 struct skeleton_data
4753 {
4754 dwarf2_per_objfile *per_objfile;
4755 cooked_index_storage *storage;
4756 };
4757
4758 /* Traversal function for process_skeletonless_type_unit.
4759 Read a TU in a DWO file and build partial symbols for it. */
4760
4761 static int
4762 process_skeletonless_type_unit (void **slot, void *info)
4763 {
4764 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
4765 skeleton_data *data = (skeleton_data *) info;
4766
4767 /* If this TU doesn't exist in the global table, add it and read it in. */
4768
4769 if (data->per_objfile->per_bfd->signatured_types == NULL)
4770 data->per_objfile->per_bfd->signatured_types
4771 = allocate_signatured_type_table ();
4772
4773 signatured_type find_entry (dwo_unit->signature);
4774 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
4775 &find_entry, INSERT);
4776 /* If we've already seen this type there's nothing to do. What's happening
4777 is we're doing our own version of comdat-folding here. */
4778 if (*slot != NULL)
4779 return 1;
4780
4781 /* This does the job that create_all_units would have done for
4782 this TU. */
4783 signatured_type *entry
4784 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
4785 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
4786 *slot = entry;
4787
4788 /* This does the job that build_type_psymtabs would have done. */
4789 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
4790 if (!reader.dummy_p)
4791 build_type_psymtabs_reader (&reader, data->storage);
4792
4793 return 1;
4794 }
4795
4796 /* Traversal function for process_skeletonless_type_units. */
4797
4798 static int
4799 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
4800 {
4801 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
4802
4803 if (dwo_file->tus != NULL)
4804 htab_traverse_noresize (dwo_file->tus.get (),
4805 process_skeletonless_type_unit, info);
4806
4807 return 1;
4808 }
4809
4810 /* Scan all TUs of DWO files, verifying we've processed them.
4811 This is needed in case a TU was emitted without its skeleton.
4812 Note: This can't be done until we know what all the DWO files are. */
4813
4814 static void
4815 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
4816 cooked_index_storage *storage)
4817 {
4818 skeleton_data data { per_objfile, storage };
4819
4820 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
4821 if (get_dwp_file (per_objfile) == NULL
4822 && per_objfile->per_bfd->dwo_files != NULL)
4823 {
4824 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
4825 process_dwo_file_for_skeletonless_type_units,
4826 &data);
4827 }
4828 }
4829
4830 cooked_index_worker::cooked_index_worker (dwarf2_per_objfile *per_objfile)
4831 : m_per_objfile (per_objfile)
4832 {
4833 gdb_assert (is_main_thread ());
4834
4835 struct objfile *objfile = per_objfile->objfile;
4836 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4837
4838 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
4839 objfile_name (objfile));
4840
4841 per_bfd->map_info_sections (objfile);
4842 }
4843
4844 void
4845 cooked_index_worker::start ()
4846 {
4847 gdb::thread_pool::g_thread_pool->post_task ([=] ()
4848 {
4849 this->start_reading ();
4850 });
4851 }
4852
4853 void
4854 cooked_index_worker::process_cus (size_t task_number, unit_iterator first,
4855 unit_iterator end)
4856 {
4857 SCOPE_EXIT { bfd_thread_cleanup (); };
4858
4859 /* Ensure that complaints are handled correctly. */
4860 complaint_interceptor complaint_handler;
4861
4862 std::vector<gdb_exception> errors;
4863 cooked_index_storage thread_storage;
4864 for (auto inner = first; inner != end; ++inner)
4865 {
4866 dwarf2_per_cu_data *per_cu = inner->get ();
4867 try
4868 {
4869 process_psymtab_comp_unit (per_cu, m_per_objfile, &thread_storage);
4870 }
4871 catch (gdb_exception &except)
4872 {
4873 errors.push_back (std::move (except));
4874 }
4875 }
4876
4877 m_results[task_number] = result_type (thread_storage.release (),
4878 complaint_handler.release (),
4879 std::move (errors));
4880 }
4881
4882 void
4883 cooked_index_worker::done_reading ()
4884 {
4885 /* Only handle the scanning results here. Complaints and exceptions
4886 can only be dealt with on the main thread. */
4887 std::vector<std::unique_ptr<cooked_index_shard>> indexes;
4888 for (auto &one_result : m_results)
4889 indexes.push_back (std::move (std::get<0> (one_result)));
4890
4891 /* This has to wait until we read the CUs, we need the list of DWOs. */
4892 process_skeletonless_type_units (m_per_objfile, &m_index_storage);
4893
4894 indexes.push_back (m_index_storage.release ());
4895 indexes.shrink_to_fit ();
4896
4897 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4898 cooked_index *table
4899 = (gdb::checked_static_cast<cooked_index *>
4900 (per_bfd->index_table.get ()));
4901 table->set_contents (std::move (indexes));
4902 }
4903
4904 void
4905 cooked_index_worker::start_reading ()
4906 {
4907 SCOPE_EXIT { bfd_thread_cleanup (); };
4908
4909 try
4910 {
4911 do_reading ();
4912 }
4913 catch (const gdb_exception &exc)
4914 {
4915 m_failed = exc;
4916 set (cooked_state::CACHE_DONE);
4917 }
4918 }
4919
4920 void
4921 cooked_index_worker::do_reading ()
4922 {
4923 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4924
4925 create_all_units (m_per_objfile);
4926 build_type_psymtabs (m_per_objfile, &m_index_storage);
4927
4928 per_bfd->quick_file_names_table
4929 = create_quick_file_names_table (per_bfd->all_units.size ());
4930 if (!per_bfd->debug_aranges.empty ())
4931 read_addrmap_from_aranges (m_per_objfile, &per_bfd->debug_aranges,
4932 m_index_storage.get_addrmap (),
4933 &m_warnings);
4934
4935 /* We want to balance the load between the worker threads. This is
4936 done by using the size of each CU as a rough estimate of how
4937 difficult it will be to operate on. This isn't ideal -- for
4938 example if dwz is used, the early CUs will all tend to be
4939 "included" and won't be parsed independently. However, this
4940 heuristic works well for typical compiler output. */
4941
4942 size_t total_size = 0;
4943 for (const auto &per_cu : per_bfd->all_units)
4944 total_size += per_cu->length ();
4945
4946 /* How many worker threads we plan to use. We may not actually use
4947 this many. We use 1 as the minimum to avoid division by zero,
4948 and anyway in the N==0 case the work will be done
4949 synchronously. */
4950 const size_t n_worker_threads
4951 = std::max (gdb::thread_pool::g_thread_pool->thread_count (), (size_t) 1);
4952
4953 /* How much effort should be put into each worker. */
4954 const size_t size_per_thread
4955 = std::max (total_size / n_worker_threads, (size_t) 1);
4956
4957 /* Work is done in a task group. */
4958 gdb::task_group workers ([this] ()
4959 {
4960 this->done_reading ();
4961 });
4962
4963 auto end = per_bfd->all_units.end ();
4964 size_t task_count = 0;
4965 for (auto iter = per_bfd->all_units.begin (); iter != end; )
4966 {
4967 auto last = iter;
4968 /* Put all remaining CUs into the last task. */
4969 if (task_count == n_worker_threads - 1)
4970 last = end;
4971 else
4972 {
4973 size_t chunk_size = 0;
4974 for (; last != end && chunk_size < size_per_thread; ++last)
4975 chunk_size += (*last)->length ();
4976 }
4977
4978 gdb_assert (iter != last);
4979 workers.add_task ([=] ()
4980 {
4981 process_cus (task_count, iter, last);
4982 });
4983
4984 ++task_count;
4985 iter = last;
4986 }
4987
4988 m_results.resize (task_count);
4989 workers.start ();
4990 }
4991
4992 bool
4993 cooked_index_worker::wait (cooked_state desired_state, bool allow_quit)
4994 {
4995 bool done;
4996 #if CXX_STD_THREAD
4997 {
4998 std::unique_lock<std::mutex> lock (m_mutex);
4999
5000 /* This may be called from a non-main thread -- this functionality
5001 is needed for the index cache -- but in this case we require
5002 that the desired state already have been attained. */
5003 gdb_assert (is_main_thread () || desired_state <= m_state);
5004
5005 while (desired_state > m_state)
5006 {
5007 if (allow_quit)
5008 {
5009 std::chrono::milliseconds duration { 15 };
5010 if (m_cond.wait_for (lock, duration) == std::cv_status::timeout)
5011 QUIT;
5012 }
5013 else
5014 m_cond.wait (lock);
5015 }
5016 done = m_state == cooked_state::CACHE_DONE;
5017 }
5018 #else
5019 /* Without threads, all the work is done immediately on the main
5020 thread, and there is never anything to wait for. */
5021 done = true;
5022 #endif /* CXX_STD_THREAD */
5023
5024 /* Only the main thread is allowed to report complaints and the
5025 like. */
5026 if (!is_main_thread ())
5027 return false;
5028
5029 if (m_reported)
5030 return done;
5031 m_reported = true;
5032
5033 /* Emit warnings first, maybe they were emitted before an exception
5034 (if any) was thrown. */
5035 m_warnings.emit ();
5036
5037 if (m_failed.has_value ())
5038 {
5039 /* start_reading failed -- report it. */
5040 exception_print (gdb_stderr, *m_failed);
5041 m_failed.reset ();
5042 return done;
5043 }
5044
5045 /* Only show a given exception a single time. */
5046 std::unordered_set<gdb_exception> seen_exceptions;
5047 for (auto &one_result : m_results)
5048 {
5049 re_emit_complaints (std::get<1> (one_result));
5050 for (auto &one_exc : std::get<2> (one_result))
5051 if (seen_exceptions.insert (one_exc).second)
5052 exception_print (gdb_stderr, one_exc);
5053 }
5054
5055 if (dwarf_read_debug > 0)
5056 print_tu_stats (m_per_objfile);
5057
5058 struct objfile *objfile = m_per_objfile->objfile;
5059 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5060 cooked_index *table
5061 = (gdb::checked_static_cast<cooked_index *>
5062 (per_bfd->index_table.get ()));
5063
5064 auto_obstack temp_storage;
5065 enum language lang = language_unknown;
5066 const char *main_name = table->get_main_name (&temp_storage, &lang);
5067 if (main_name != nullptr)
5068 set_objfile_main_name (objfile, main_name, lang);
5069
5070 dwarf_read_debug_printf ("Done building psymtabs of %s",
5071 objfile_name (objfile));
5072
5073 return done;
5074 }
5075
5076 void
5077 cooked_index_worker::set (cooked_state desired_state)
5078 {
5079 gdb_assert (desired_state != cooked_state::INITIAL);
5080
5081 #if CXX_STD_THREAD
5082 std::lock_guard<std::mutex> guard (m_mutex);
5083 gdb_assert (desired_state > m_state);
5084 m_state = desired_state;
5085 m_cond.notify_one ();
5086 #else
5087 /* Without threads, all the work is done immediately on the main
5088 thread, and there is never anything to do. */
5089 #endif /* CXX_STD_THREAD */
5090 }
5091
5092 static void
5093 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
5094 struct dwarf2_section_info *section,
5095 struct dwarf2_section_info *abbrev_section,
5096 unsigned int is_dwz,
5097 htab_up &types_htab,
5098 rcuh_kind section_kind)
5099 {
5100 const gdb_byte *info_ptr;
5101 struct objfile *objfile = per_objfile->objfile;
5102
5103 dwarf_read_debug_printf ("Reading %s for %s",
5104 section->get_name (),
5105 section->get_file_name ());
5106
5107 section->read (objfile);
5108
5109 info_ptr = section->buffer;
5110
5111 while (info_ptr < section->buffer + section->size)
5112 {
5113 dwarf2_per_cu_data_up this_cu;
5114
5115 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
5116
5117 comp_unit_head cu_header;
5118 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
5119 abbrev_section, info_ptr,
5120 section_kind);
5121
5122 /* Save the compilation unit for later lookup. */
5123 if (cu_header.unit_type != DW_UT_type)
5124 this_cu = per_objfile->per_bfd->allocate_per_cu ();
5125 else
5126 {
5127 if (types_htab == nullptr)
5128 types_htab = allocate_signatured_type_table ();
5129
5130 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
5131 (cu_header.signature);
5132 signatured_type *sig_ptr = sig_type.get ();
5133 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
5134 this_cu.reset (sig_type.release ());
5135
5136 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
5137 gdb_assert (slot != nullptr);
5138 if (*slot != nullptr)
5139 complaint (_("debug type entry at offset %s is duplicate to"
5140 " the entry at offset %s, signature %s"),
5141 sect_offset_str (sect_off),
5142 sect_offset_str (sig_ptr->sect_off),
5143 hex_string (sig_ptr->signature));
5144 *slot = sig_ptr;
5145 }
5146 this_cu->sect_off = sect_off;
5147 this_cu->set_length (cu_header.get_length_with_initial ());
5148 this_cu->is_dwz = is_dwz;
5149 this_cu->section = section;
5150 /* Init this asap, to avoid a data race in the set_version in
5151 cutu_reader::cutu_reader (which may be run in parallel for the cooked
5152 index case). */
5153 this_cu->set_version (cu_header.version);
5154
5155 info_ptr = info_ptr + this_cu->length ();
5156 per_objfile->per_bfd->all_units.push_back (std::move (this_cu));
5157 }
5158 }
5159
5160 /* Initialize the views on all_units. */
5161
5162 void
5163 finalize_all_units (dwarf2_per_bfd *per_bfd)
5164 {
5165 size_t nr_tus = per_bfd->tu_stats.nr_tus;
5166 size_t nr_cus = per_bfd->all_units.size () - nr_tus;
5167 gdb::array_view<dwarf2_per_cu_data_up> tmp = per_bfd->all_units;
5168 per_bfd->all_comp_units = tmp.slice (0, nr_cus);
5169 per_bfd->all_type_units = tmp.slice (nr_cus, nr_tus);
5170 }
5171
5172 /* See read.h. */
5173
5174 void
5175 create_all_units (dwarf2_per_objfile *per_objfile)
5176 {
5177 htab_up types_htab;
5178 gdb_assert (per_objfile->per_bfd->all_units.empty ());
5179
5180 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
5181 &per_objfile->per_bfd->abbrev, 0,
5182 types_htab, rcuh_kind::COMPILE);
5183 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
5184 read_comp_units_from_section (per_objfile, &section,
5185 &per_objfile->per_bfd->abbrev, 0,
5186 types_htab, rcuh_kind::TYPE);
5187
5188 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
5189 if (dwz != NULL)
5190 {
5191 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
5192 types_htab, rcuh_kind::COMPILE);
5193
5194 if (!dwz->types.empty ())
5195 {
5196 /* See enhancement PR symtab/30838. */
5197 error (_("Dwarf Error: .debug_types section not supported in dwz file"));
5198 }
5199 }
5200
5201 per_objfile->per_bfd->signatured_types = std::move (types_htab);
5202
5203 finalize_all_units (per_objfile->per_bfd);
5204 }
5205
5206 /* Return the initial uleb128 in the die at INFO_PTR. */
5207
5208 static unsigned int
5209 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
5210 {
5211 unsigned int bytes_read;
5212
5213 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5214 }
5215
5216 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
5217 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
5218
5219 Return the corresponding abbrev, or NULL if the number is zero (indicating
5220 an empty DIE). In either case *BYTES_READ will be set to the length of
5221 the initial number. */
5222
5223 static const struct abbrev_info *
5224 peek_die_abbrev (const die_reader_specs &reader,
5225 const gdb_byte *info_ptr, unsigned int *bytes_read)
5226 {
5227 dwarf2_cu *cu = reader.cu;
5228 bfd *abfd = reader.abfd;
5229 unsigned int abbrev_number
5230 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5231
5232 if (abbrev_number == 0)
5233 return NULL;
5234
5235 const abbrev_info *abbrev
5236 = reader.abbrev_table->lookup_abbrev (abbrev_number);
5237 if (!abbrev)
5238 {
5239 error (_("Dwarf Error: Could not find abbrev number %d in %s"
5240 " at offset %s [in module %s]"),
5241 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
5242 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
5243 }
5244
5245 return abbrev;
5246 }
5247
5248 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5249 Returns a pointer to the end of a series of DIEs, terminated by an empty
5250 DIE. Any children of the skipped DIEs will also be skipped. */
5251
5252 static const gdb_byte *
5253 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
5254 {
5255 while (1)
5256 {
5257 unsigned int bytes_read;
5258 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
5259 &bytes_read);
5260
5261 if (abbrev == NULL)
5262 return info_ptr + bytes_read;
5263 else
5264 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5265 }
5266 }
5267
5268 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5269 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5270 abbrev corresponding to that skipped uleb128 should be passed in
5271 ABBREV.
5272
5273 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
5274 returns a pointer to this DIE's sibling, skipping any children.
5275 Otherwise, returns a pointer to the DIE's first child. */
5276
5277 static const gdb_byte *
5278 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
5279 const struct abbrev_info *abbrev, bool do_skip_children)
5280 {
5281 unsigned int bytes_read;
5282 struct attribute attr;
5283 bfd *abfd = reader->abfd;
5284 struct dwarf2_cu *cu = reader->cu;
5285 const gdb_byte *buffer = reader->buffer;
5286 const gdb_byte *buffer_end = reader->buffer_end;
5287 unsigned int form, i;
5288
5289 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
5290 {
5291 /* We only handle DW_FORM_ref4 here. */
5292 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
5293 unsigned int offset = read_4_bytes (abfd, sibling_data);
5294 const gdb_byte *sibling_ptr
5295 = buffer + to_underlying (cu->header.sect_off) + offset;
5296 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
5297 return sibling_ptr;
5298 /* Fall through to the slow way. */
5299 }
5300 else if (abbrev->size_if_constant != 0)
5301 {
5302 info_ptr += abbrev->size_if_constant;
5303 if (do_skip_children && abbrev->has_children)
5304 return skip_children (reader, info_ptr);
5305 return info_ptr;
5306 }
5307
5308 for (i = 0; i < abbrev->num_attrs; i++)
5309 {
5310 /* The only abbrev we care about is DW_AT_sibling. */
5311 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
5312 {
5313 /* Note there is no need for the extra work of
5314 "reprocessing" here, so we pass false for that
5315 argument. */
5316 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr, false);
5317 if (attr.form == DW_FORM_ref_addr)
5318 complaint (_("ignoring absolute DW_AT_sibling"));
5319 else
5320 {
5321 sect_offset off = attr.get_ref_die_offset ();
5322 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
5323
5324 if (sibling_ptr < info_ptr)
5325 complaint (_("DW_AT_sibling points backwards"));
5326 else if (sibling_ptr > reader->buffer_end)
5327 reader->die_section->overflow_complaint ();
5328 else
5329 return sibling_ptr;
5330 }
5331 }
5332
5333 /* If it isn't DW_AT_sibling, skip this attribute. */
5334 form = abbrev->attrs[i].form;
5335 skip_attribute:
5336 switch (form)
5337 {
5338 case DW_FORM_ref_addr:
5339 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5340 and later it is offset sized. */
5341 if (cu->header.version == 2)
5342 info_ptr += cu->header.addr_size;
5343 else
5344 info_ptr += cu->header.offset_size;
5345 break;
5346 case DW_FORM_GNU_ref_alt:
5347 info_ptr += cu->header.offset_size;
5348 break;
5349 case DW_FORM_addr:
5350 info_ptr += cu->header.addr_size;
5351 break;
5352 case DW_FORM_data1:
5353 case DW_FORM_ref1:
5354 case DW_FORM_flag:
5355 case DW_FORM_strx1:
5356 info_ptr += 1;
5357 break;
5358 case DW_FORM_flag_present:
5359 case DW_FORM_implicit_const:
5360 break;
5361 case DW_FORM_data2:
5362 case DW_FORM_ref2:
5363 case DW_FORM_strx2:
5364 info_ptr += 2;
5365 break;
5366 case DW_FORM_strx3:
5367 info_ptr += 3;
5368 break;
5369 case DW_FORM_data4:
5370 case DW_FORM_ref4:
5371 case DW_FORM_strx4:
5372 info_ptr += 4;
5373 break;
5374 case DW_FORM_data8:
5375 case DW_FORM_ref8:
5376 case DW_FORM_ref_sig8:
5377 info_ptr += 8;
5378 break;
5379 case DW_FORM_data16:
5380 info_ptr += 16;
5381 break;
5382 case DW_FORM_string:
5383 read_direct_string (abfd, info_ptr, &bytes_read);
5384 info_ptr += bytes_read;
5385 break;
5386 case DW_FORM_sec_offset:
5387 case DW_FORM_strp:
5388 case DW_FORM_GNU_strp_alt:
5389 info_ptr += cu->header.offset_size;
5390 break;
5391 case DW_FORM_exprloc:
5392 case DW_FORM_block:
5393 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5394 info_ptr += bytes_read;
5395 break;
5396 case DW_FORM_block1:
5397 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5398 break;
5399 case DW_FORM_block2:
5400 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5401 break;
5402 case DW_FORM_block4:
5403 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5404 break;
5405 case DW_FORM_addrx:
5406 case DW_FORM_strx:
5407 case DW_FORM_sdata:
5408 case DW_FORM_udata:
5409 case DW_FORM_ref_udata:
5410 case DW_FORM_GNU_addr_index:
5411 case DW_FORM_GNU_str_index:
5412 case DW_FORM_rnglistx:
5413 case DW_FORM_loclistx:
5414 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
5415 break;
5416 case DW_FORM_indirect:
5417 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5418 info_ptr += bytes_read;
5419 /* We need to continue parsing from here, so just go back to
5420 the top. */
5421 goto skip_attribute;
5422
5423 default:
5424 error (_("Dwarf Error: Cannot handle %s "
5425 "in DWARF reader [in module %s]"),
5426 dwarf_form_name (form),
5427 bfd_get_filename (abfd));
5428 }
5429 }
5430
5431 if (do_skip_children && abbrev->has_children)
5432 return skip_children (reader, info_ptr);
5433 else
5434 return info_ptr;
5435 }
5436 \f
5437 /* Reading in full CUs. */
5438
5439 /* Add PER_CU to the queue. */
5440
5441 static void
5442 queue_comp_unit (dwarf2_per_cu_data *per_cu,
5443 dwarf2_per_objfile *per_objfile,
5444 enum language pretend_language)
5445 {
5446 per_cu->queued = 1;
5447
5448 gdb_assert (per_objfile->queue.has_value ());
5449 per_objfile->queue->emplace (per_cu, per_objfile, pretend_language);
5450 }
5451
5452 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
5453
5454 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
5455 dependency.
5456
5457 Return true if maybe_queue_comp_unit requires the caller to load the CU's
5458 DIEs, false otherwise.
5459
5460 Explanation: there is an invariant that if a CU is queued for expansion
5461 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
5462 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
5463 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
5464 are not yet loaded, the the caller must load the CU's DIEs to ensure the
5465 invariant is respected.
5466
5467 The caller is therefore not required to load the CU's DIEs (we return false)
5468 if:
5469
5470 - the CU is already expanded, and therefore does not get enqueued
5471 - the CU gets enqueued for expansion, but its DIEs are already loaded
5472
5473 Note that the caller should not use this function's return value as an
5474 indicator of whether the CU's DIEs are loaded right now, it should check
5475 that by calling `dwarf2_per_objfile::get_cu` instead. */
5476
5477 static int
5478 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
5479 dwarf2_per_cu_data *per_cu,
5480 dwarf2_per_objfile *per_objfile,
5481 enum language pretend_language)
5482 {
5483 /* Mark the dependence relation so that we don't flush PER_CU
5484 too early. */
5485 if (dependent_cu != NULL)
5486 dependent_cu->add_dependence (per_cu);
5487
5488 /* If it's already on the queue, we have nothing to do. */
5489 if (per_cu->queued)
5490 {
5491 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
5492 loaded. */
5493 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
5494
5495 /* If the CU is queued for expansion, it should not already be
5496 expanded. */
5497 gdb_assert (!per_objfile->symtab_set_p (per_cu));
5498
5499 /* The DIEs are already loaded, the caller doesn't need to do it. */
5500 return 0;
5501 }
5502
5503 bool queued = false;
5504 if (!per_objfile->symtab_set_p (per_cu))
5505 {
5506 /* Add it to the queue. */
5507 queue_comp_unit (per_cu, per_objfile, pretend_language);
5508 queued = true;
5509 }
5510
5511 /* If the compilation unit is already loaded, just mark it as
5512 used. */
5513 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
5514 if (cu != nullptr)
5515 cu->last_used = 0;
5516
5517 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
5518 and the DIEs are not already loaded. */
5519 return queued && cu == nullptr;
5520 }
5521
5522 /* Process the queue. */
5523
5524 static void
5525 process_queue (dwarf2_per_objfile *per_objfile)
5526 {
5527 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
5528 objfile_name (per_objfile->objfile));
5529
5530 /* The queue starts out with one item, but following a DIE reference
5531 may load a new CU, adding it to the end of the queue. */
5532 while (!per_objfile->queue->empty ())
5533 {
5534 dwarf2_queue_item &item = per_objfile->queue->front ();
5535 dwarf2_per_cu_data *per_cu = item.per_cu;
5536
5537 if (!per_objfile->symtab_set_p (per_cu))
5538 {
5539 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
5540
5541 /* Skip dummy CUs. */
5542 if (cu != nullptr)
5543 {
5544 unsigned int debug_print_threshold;
5545 char buf[100];
5546
5547 if (per_cu->is_debug_types)
5548 {
5549 struct signatured_type *sig_type =
5550 (struct signatured_type *) per_cu;
5551
5552 sprintf (buf, "TU %s at offset %s",
5553 hex_string (sig_type->signature),
5554 sect_offset_str (per_cu->sect_off));
5555 /* There can be 100s of TUs.
5556 Only print them in verbose mode. */
5557 debug_print_threshold = 2;
5558 }
5559 else
5560 {
5561 sprintf (buf, "CU at offset %s",
5562 sect_offset_str (per_cu->sect_off));
5563 debug_print_threshold = 1;
5564 }
5565
5566 if (dwarf_read_debug >= debug_print_threshold)
5567 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
5568
5569 if (per_cu->is_debug_types)
5570 process_full_type_unit (cu, item.pretend_language);
5571 else
5572 process_full_comp_unit (cu, item.pretend_language);
5573
5574 if (dwarf_read_debug >= debug_print_threshold)
5575 dwarf_read_debug_printf ("Done expanding %s", buf);
5576 }
5577 }
5578
5579 per_cu->queued = 0;
5580 per_objfile->queue->pop ();
5581 }
5582
5583 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
5584 objfile_name (per_objfile->objfile));
5585 }
5586
5587 /* Load the DIEs associated with PER_CU into memory.
5588
5589 In some cases, the caller, while reading partial symbols, will need to load
5590 the full symbols for the CU for some reason. It will already have a
5591 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
5592 rather than creating a new one. */
5593
5594 static void
5595 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
5596 dwarf2_per_objfile *per_objfile,
5597 dwarf2_cu *existing_cu,
5598 bool skip_partial,
5599 enum language pretend_language)
5600 {
5601 gdb_assert (! this_cu->is_debug_types);
5602
5603 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
5604 if (reader.dummy_p)
5605 return;
5606
5607 struct dwarf2_cu *cu = reader.cu;
5608 const gdb_byte *info_ptr = reader.info_ptr;
5609
5610 gdb_assert (cu->die_hash == NULL);
5611 cu->die_hash =
5612 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
5613 die_info::hash,
5614 die_info::eq,
5615 NULL,
5616 &cu->comp_unit_obstack,
5617 hashtab_obstack_allocate,
5618 dummy_obstack_deallocate);
5619
5620 if (reader.comp_unit_die->has_children)
5621 reader.comp_unit_die->child
5622 = read_die_and_siblings (&reader, reader.info_ptr,
5623 &info_ptr, reader.comp_unit_die);
5624 cu->dies = reader.comp_unit_die;
5625 /* comp_unit_die is not stored in die_hash, no need. */
5626
5627 /* We try not to read any attributes in this function, because not
5628 all CUs needed for references have been loaded yet, and symbol
5629 table processing isn't initialized. But we have to set the CU language,
5630 or we won't be able to build types correctly.
5631 Similarly, if we do not read the producer, we can not apply
5632 producer-specific interpretation. */
5633 prepare_one_comp_unit (cu, cu->dies, pretend_language);
5634
5635 reader.keep ();
5636 }
5637
5638 /* Add a DIE to the delayed physname list. */
5639
5640 static void
5641 add_to_method_list (struct type *type, int fnfield_index, int index,
5642 const char *name, struct die_info *die,
5643 struct dwarf2_cu *cu)
5644 {
5645 struct delayed_method_info mi;
5646 mi.type = type;
5647 mi.fnfield_index = fnfield_index;
5648 mi.index = index;
5649 mi.name = name;
5650 mi.die = die;
5651 cu->method_list.push_back (mi);
5652 }
5653
5654 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
5655 "const" / "volatile". If so, decrements LEN by the length of the
5656 modifier and return true. Otherwise return false. */
5657
5658 template<size_t N>
5659 static bool
5660 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
5661 {
5662 size_t mod_len = sizeof (mod) - 1;
5663 if (len > mod_len && startswith (physname + (len - mod_len), mod))
5664 {
5665 len -= mod_len;
5666 return true;
5667 }
5668 return false;
5669 }
5670
5671 /* Compute the physnames of any methods on the CU's method list.
5672
5673 The computation of method physnames is delayed in order to avoid the
5674 (bad) condition that one of the method's formal parameters is of an as yet
5675 incomplete type. */
5676
5677 static void
5678 compute_delayed_physnames (struct dwarf2_cu *cu)
5679 {
5680 /* Only C++ delays computing physnames. */
5681 if (cu->method_list.empty ())
5682 return;
5683 gdb_assert (cu->lang () == language_cplus);
5684
5685 for (const delayed_method_info &mi : cu->method_list)
5686 {
5687 const char *physname;
5688 struct fn_fieldlist *fn_flp
5689 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
5690 physname = dwarf2_physname (mi.name, mi.die, cu);
5691 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
5692 = physname ? physname : "";
5693
5694 /* Since there's no tag to indicate whether a method is a
5695 const/volatile overload, extract that information out of the
5696 demangled name. */
5697 if (physname != NULL)
5698 {
5699 size_t len = strlen (physname);
5700
5701 while (1)
5702 {
5703 if (physname[len - 1] == ')') /* shortcut */
5704 break;
5705 else if (check_modifier (physname, len, " const"))
5706 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
5707 else if (check_modifier (physname, len, " volatile"))
5708 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
5709 else
5710 break;
5711 }
5712 }
5713 }
5714
5715 /* The list is no longer needed. */
5716 cu->method_list.clear ();
5717 }
5718
5719 /* Go objects should be embedded in a DW_TAG_module DIE,
5720 and it's not clear if/how imported objects will appear.
5721 To keep Go support simple until that's worked out,
5722 go back through what we've read and create something usable.
5723 We could do this while processing each DIE, and feels kinda cleaner,
5724 but that way is more invasive.
5725 This is to, for example, allow the user to type "p var" or "b main"
5726 without having to specify the package name, and allow lookups
5727 of module.object to work in contexts that use the expression
5728 parser. */
5729
5730 static void
5731 fixup_go_packaging (struct dwarf2_cu *cu)
5732 {
5733 gdb::unique_xmalloc_ptr<char> package_name;
5734 struct pending *list;
5735 int i;
5736
5737 for (list = *cu->get_builder ()->get_global_symbols ();
5738 list != NULL;
5739 list = list->next)
5740 {
5741 for (i = 0; i < list->nsyms; ++i)
5742 {
5743 struct symbol *sym = list->symbol[i];
5744
5745 if (sym->language () == language_go
5746 && sym->aclass () == LOC_BLOCK)
5747 {
5748 gdb::unique_xmalloc_ptr<char> this_package_name
5749 = go_symbol_package_name (sym);
5750
5751 if (this_package_name == NULL)
5752 continue;
5753 if (package_name == NULL)
5754 package_name = std::move (this_package_name);
5755 else
5756 {
5757 struct objfile *objfile = cu->per_objfile->objfile;
5758 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
5759 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
5760 (sym->symtab () != NULL
5761 ? symtab_to_filename_for_display
5762 (sym->symtab ())
5763 : objfile_name (objfile)),
5764 this_package_name.get (), package_name.get ());
5765 }
5766 }
5767 }
5768 }
5769
5770 if (package_name != NULL)
5771 {
5772 struct objfile *objfile = cu->per_objfile->objfile;
5773 const char *saved_package_name = objfile->intern (package_name.get ());
5774 struct type *type
5775 = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_MODULE, 0,
5776 saved_package_name);
5777 struct symbol *sym;
5778
5779 sym = new (&objfile->objfile_obstack) symbol;
5780 sym->set_language (language_go, &objfile->objfile_obstack);
5781 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
5782 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5783 e.g., "main" finds the "main" module and not C's main(). */
5784 sym->set_domain (STRUCT_DOMAIN);
5785 sym->set_aclass_index (LOC_TYPEDEF);
5786 sym->set_type (type);
5787
5788 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
5789 }
5790 }
5791
5792 /* Allocate a fully-qualified name consisting of the two parts on the
5793 obstack. */
5794
5795 static const char *
5796 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
5797 {
5798 return obconcat (obstack, p1, "::", p2, (char *) NULL);
5799 }
5800
5801 /* A helper that allocates a variant part to attach to a Rust enum
5802 type. OBSTACK is where the results should be allocated. TYPE is
5803 the type we're processing. DISCRIMINANT_INDEX is the index of the
5804 discriminant. It must be the index of one of the fields of TYPE,
5805 or -1 to mean there is no discriminant (univariant enum).
5806 DEFAULT_INDEX is the index of the default field; or -1 if there is
5807 no default. RANGES is indexed by "effective" field number (the
5808 field index, but omitting the discriminant and default fields) and
5809 must hold the discriminant values used by the variants. Note that
5810 RANGES must have a lifetime at least as long as OBSTACK -- either
5811 already allocated on it, or static. */
5812
5813 static void
5814 alloc_rust_variant (struct obstack *obstack, struct type *type,
5815 int discriminant_index, int default_index,
5816 gdb::array_view<discriminant_range> ranges)
5817 {
5818 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
5819 gdb_assert (discriminant_index == -1
5820 || (discriminant_index >= 0
5821 && discriminant_index < type->num_fields ()));
5822 gdb_assert (default_index == -1
5823 || (default_index >= 0 && default_index < type->num_fields ()));
5824
5825 /* We have one variant for each non-discriminant field. */
5826 int n_variants = type->num_fields ();
5827 if (discriminant_index != -1)
5828 --n_variants;
5829
5830 variant *variants = new (obstack) variant[n_variants];
5831 int var_idx = 0;
5832 int range_idx = 0;
5833 for (int i = 0; i < type->num_fields (); ++i)
5834 {
5835 if (i == discriminant_index)
5836 continue;
5837
5838 variants[var_idx].first_field = i;
5839 variants[var_idx].last_field = i + 1;
5840
5841 /* The default field does not need a range, but other fields do.
5842 We skipped the discriminant above. */
5843 if (i != default_index)
5844 {
5845 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
5846 ++range_idx;
5847 }
5848
5849 ++var_idx;
5850 }
5851
5852 gdb_assert (range_idx == ranges.size ());
5853 gdb_assert (var_idx == n_variants);
5854
5855 variant_part *part = new (obstack) variant_part;
5856 part->discriminant_index = discriminant_index;
5857 /* If there is no discriminant, then whether it is signed is of no
5858 consequence. */
5859 part->is_unsigned
5860 = (discriminant_index == -1
5861 ? false
5862 : type->field (discriminant_index).type ()->is_unsigned ());
5863 part->variants = gdb::array_view<variant> (variants, n_variants);
5864
5865 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
5866 gdb::array_view<variant_part> *prop_value
5867 = new (storage) gdb::array_view<variant_part> (part, 1);
5868
5869 struct dynamic_prop prop;
5870 prop.set_variant_parts (prop_value);
5871
5872 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
5873 }
5874
5875 /* Some versions of rustc emitted enums in an unusual way.
5876
5877 Ordinary enums were emitted as unions. The first element of each
5878 structure in the union was named "RUST$ENUM$DISR". This element
5879 held the discriminant.
5880
5881 These versions of Rust also implemented the "non-zero"
5882 optimization. When the enum had two values, and one is empty and
5883 the other holds a pointer that cannot be zero, the pointer is used
5884 as the discriminant, with a zero value meaning the empty variant.
5885 Here, the union's first member is of the form
5886 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
5887 where the fieldnos are the indices of the fields that should be
5888 traversed in order to find the field (which may be several fields deep)
5889 and the variantname is the name of the variant of the case when the
5890 field is zero.
5891
5892 This function recognizes whether TYPE is of one of these forms,
5893 and, if so, smashes it to be a variant type. */
5894
5895 static void
5896 quirk_rust_enum (struct type *type, struct objfile *objfile)
5897 {
5898 gdb_assert (type->code () == TYPE_CODE_UNION);
5899
5900 /* We don't need to deal with empty enums. */
5901 if (type->num_fields () == 0)
5902 return;
5903
5904 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
5905 if (type->num_fields () == 1
5906 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
5907 {
5908 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
5909
5910 /* Decode the field name to find the offset of the
5911 discriminant. */
5912 ULONGEST bit_offset = 0;
5913 struct type *field_type = type->field (0).type ();
5914 while (name[0] >= '0' && name[0] <= '9')
5915 {
5916 char *tail;
5917 unsigned long index = strtoul (name, &tail, 10);
5918 name = tail;
5919 if (*name != '$'
5920 || index >= field_type->num_fields ()
5921 || (field_type->field (index).loc_kind ()
5922 != FIELD_LOC_KIND_BITPOS))
5923 {
5924 complaint (_("Could not parse Rust enum encoding string \"%s\""
5925 "[in module %s]"),
5926 type->field (0).name (),
5927 objfile_name (objfile));
5928 return;
5929 }
5930 ++name;
5931
5932 bit_offset += field_type->field (index).loc_bitpos ();
5933 field_type = field_type->field (index).type ();
5934 }
5935
5936 /* Smash this type to be a structure type. We have to do this
5937 because the type has already been recorded. */
5938 type->set_code (TYPE_CODE_STRUCT);
5939 /* Save the field we care about. */
5940 struct field saved_field = type->field (0);
5941 type->alloc_fields (3);
5942
5943 /* Put the discriminant at index 0. */
5944 type->field (0).set_type (field_type);
5945 type->field (0).set_is_artificial (true);
5946 type->field (0).set_name ("<<discriminant>>");
5947 type->field (0).set_loc_bitpos (bit_offset);
5948
5949 /* The order of fields doesn't really matter, so put the real
5950 field at index 1 and the data-less field at index 2. */
5951 type->field (1) = saved_field;
5952 type->field (1).set_name
5953 (rust_last_path_segment (type->field (1).type ()->name ()));
5954 type->field (1).type ()->set_name
5955 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
5956 type->field (1).name ()));
5957
5958 const char *dataless_name
5959 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
5960 name);
5961 struct type *dataless_type
5962 = type_allocator (type).new_type (TYPE_CODE_VOID, 0,
5963 dataless_name);
5964 type->field (2).set_type (dataless_type);
5965 /* NAME points into the original discriminant name, which
5966 already has the correct lifetime. */
5967 type->field (2).set_name (name);
5968 type->field (2).set_loc_bitpos (0);
5969
5970 /* Indicate that this is a variant type. */
5971 static discriminant_range ranges[1] = { { 0, 0 } };
5972 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
5973 }
5974 /* A union with a single anonymous field is probably an old-style
5975 univariant enum. */
5976 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
5977 {
5978 /* Smash this type to be a structure type. We have to do this
5979 because the type has already been recorded. */
5980 type->set_code (TYPE_CODE_STRUCT);
5981
5982 struct type *field_type = type->field (0).type ();
5983 const char *variant_name
5984 = rust_last_path_segment (field_type->name ());
5985 type->field (0).set_name (variant_name);
5986 field_type->set_name
5987 (rust_fully_qualify (&objfile->objfile_obstack,
5988 type->name (), variant_name));
5989
5990 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
5991 }
5992 else
5993 {
5994 struct type *disr_type = nullptr;
5995 for (int i = 0; i < type->num_fields (); ++i)
5996 {
5997 disr_type = type->field (i).type ();
5998
5999 if (disr_type->code () != TYPE_CODE_STRUCT)
6000 {
6001 /* All fields of a true enum will be structs. */
6002 return;
6003 }
6004 else if (disr_type->num_fields () == 0)
6005 {
6006 /* Could be data-less variant, so keep going. */
6007 disr_type = nullptr;
6008 }
6009 else if (strcmp (disr_type->field (0).name (),
6010 "RUST$ENUM$DISR") != 0)
6011 {
6012 /* Not a Rust enum. */
6013 return;
6014 }
6015 else
6016 {
6017 /* Found one. */
6018 break;
6019 }
6020 }
6021
6022 /* If we got here without a discriminant, then it's probably
6023 just a union. */
6024 if (disr_type == nullptr)
6025 return;
6026
6027 /* Smash this type to be a structure type. We have to do this
6028 because the type has already been recorded. */
6029 type->set_code (TYPE_CODE_STRUCT);
6030
6031 /* Make space for the discriminant field. */
6032 struct field *disr_field = &disr_type->field (0);
6033 field *new_fields
6034 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
6035 * sizeof (struct field)));
6036 memcpy (new_fields + 1, type->fields (),
6037 type->num_fields () * sizeof (struct field));
6038 type->set_fields (new_fields);
6039 type->set_num_fields (type->num_fields () + 1);
6040
6041 /* Install the discriminant at index 0 in the union. */
6042 type->field (0) = *disr_field;
6043 type->field (0).set_is_artificial (true);
6044 type->field (0).set_name ("<<discriminant>>");
6045
6046 /* We need a way to find the correct discriminant given a
6047 variant name. For convenience we build a map here. */
6048 struct type *enum_type = disr_field->type ();
6049 std::unordered_map<std::string, ULONGEST> discriminant_map;
6050 for (int i = 0; i < enum_type->num_fields (); ++i)
6051 {
6052 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
6053 {
6054 const char *name
6055 = rust_last_path_segment (enum_type->field (i).name ());
6056 discriminant_map[name] = enum_type->field (i).loc_enumval ();
6057 }
6058 }
6059
6060 int n_fields = type->num_fields ();
6061 /* We don't need a range entry for the discriminant, but we do
6062 need one for every other field, as there is no default
6063 variant. */
6064 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
6065 discriminant_range,
6066 n_fields - 1);
6067 /* Skip the discriminant here. */
6068 for (int i = 1; i < n_fields; ++i)
6069 {
6070 /* Find the final word in the name of this variant's type.
6071 That name can be used to look up the correct
6072 discriminant. */
6073 const char *variant_name
6074 = rust_last_path_segment (type->field (i).type ()->name ());
6075
6076 auto iter = discriminant_map.find (variant_name);
6077 if (iter != discriminant_map.end ())
6078 {
6079 ranges[i - 1].low = iter->second;
6080 ranges[i - 1].high = iter->second;
6081 }
6082
6083 /* In Rust, each element should have the size of the
6084 enclosing enum. */
6085 type->field (i).type ()->set_length (type->length ());
6086
6087 /* Remove the discriminant field, if it exists. */
6088 struct type *sub_type = type->field (i).type ();
6089 if (sub_type->num_fields () > 0)
6090 {
6091 sub_type->set_num_fields (sub_type->num_fields () - 1);
6092 sub_type->set_fields (sub_type->fields () + 1);
6093 }
6094 type->field (i).set_name (variant_name);
6095 sub_type->set_name
6096 (rust_fully_qualify (&objfile->objfile_obstack,
6097 type->name (), variant_name));
6098 }
6099
6100 /* Indicate that this is a variant type. */
6101 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
6102 gdb::array_view<discriminant_range> (ranges,
6103 n_fields - 1));
6104 }
6105 }
6106
6107 /* Rewrite some Rust unions to be structures with variants parts. */
6108
6109 static void
6110 rust_union_quirks (struct dwarf2_cu *cu)
6111 {
6112 gdb_assert (cu->lang () == language_rust);
6113 for (type *type_ : cu->rust_unions)
6114 quirk_rust_enum (type_, cu->per_objfile->objfile);
6115 /* We don't need this any more. */
6116 cu->rust_unions.clear ();
6117 }
6118
6119 /* See read.h. */
6120
6121 type_unit_group_unshareable *
6122 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
6123 {
6124 auto iter = this->m_type_units.find (tu_group);
6125 if (iter != this->m_type_units.end ())
6126 return iter->second.get ();
6127
6128 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
6129 type_unit_group_unshareable *result = uniq.get ();
6130 this->m_type_units[tu_group] = std::move (uniq);
6131 return result;
6132 }
6133
6134 struct type *
6135 dwarf2_per_objfile::get_type_for_signatured_type
6136 (signatured_type *sig_type) const
6137 {
6138 auto iter = this->m_type_map.find (sig_type);
6139 if (iter == this->m_type_map.end ())
6140 return nullptr;
6141
6142 return iter->second;
6143 }
6144
6145 void dwarf2_per_objfile::set_type_for_signatured_type
6146 (signatured_type *sig_type, struct type *type)
6147 {
6148 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
6149
6150 this->m_type_map[sig_type] = type;
6151 }
6152
6153 /* A helper function for computing the list of all symbol tables
6154 included by PER_CU. */
6155
6156 static void
6157 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
6158 htab_t all_children, htab_t all_type_symtabs,
6159 dwarf2_per_cu_data *per_cu,
6160 dwarf2_per_objfile *per_objfile,
6161 struct compunit_symtab *immediate_parent)
6162 {
6163 void **slot = htab_find_slot (all_children, per_cu, INSERT);
6164 if (*slot != NULL)
6165 {
6166 /* This inclusion and its children have been processed. */
6167 return;
6168 }
6169
6170 *slot = per_cu;
6171
6172 /* Only add a CU if it has a symbol table. */
6173 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6174 if (cust != NULL)
6175 {
6176 /* If this is a type unit only add its symbol table if we haven't
6177 seen it yet (type unit per_cu's can share symtabs). */
6178 if (per_cu->is_debug_types)
6179 {
6180 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
6181 if (*slot == NULL)
6182 {
6183 *slot = cust;
6184 result->push_back (cust);
6185 if (cust->user == NULL)
6186 cust->user = immediate_parent;
6187 }
6188 }
6189 else
6190 {
6191 result->push_back (cust);
6192 if (cust->user == NULL)
6193 cust->user = immediate_parent;
6194 }
6195 }
6196
6197 if (!per_cu->imported_symtabs_empty ())
6198 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
6199 {
6200 recursively_compute_inclusions (result, all_children,
6201 all_type_symtabs, ptr, per_objfile,
6202 cust);
6203 }
6204 }
6205
6206 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
6207 PER_CU. */
6208
6209 static void
6210 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
6211 dwarf2_per_objfile *per_objfile)
6212 {
6213 gdb_assert (! per_cu->is_debug_types);
6214
6215 if (!per_cu->imported_symtabs_empty ())
6216 {
6217 int len;
6218 std::vector<compunit_symtab *> result_symtabs;
6219 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6220
6221 /* If we don't have a symtab, we can just skip this case. */
6222 if (cust == NULL)
6223 return;
6224
6225 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
6226 htab_eq_pointer,
6227 NULL, xcalloc, xfree));
6228 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
6229 htab_eq_pointer,
6230 NULL, xcalloc, xfree));
6231
6232 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
6233 {
6234 recursively_compute_inclusions (&result_symtabs, all_children.get (),
6235 all_type_symtabs.get (), ptr,
6236 per_objfile, cust);
6237 }
6238
6239 /* Now we have a transitive closure of all the included symtabs. */
6240 len = result_symtabs.size ();
6241 cust->includes
6242 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
6243 struct compunit_symtab *, len + 1);
6244 memcpy (cust->includes, result_symtabs.data (),
6245 len * sizeof (compunit_symtab *));
6246 cust->includes[len] = NULL;
6247 }
6248 }
6249
6250 /* Compute the 'includes' field for the symtabs of all the CUs we just
6251 read. */
6252
6253 static void
6254 process_cu_includes (dwarf2_per_objfile *per_objfile)
6255 {
6256 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
6257 {
6258 if (! iter->is_debug_types)
6259 compute_compunit_symtab_includes (iter, per_objfile);
6260 }
6261
6262 per_objfile->per_bfd->just_read_cus.clear ();
6263 }
6264
6265 /* Generate full symbol information for CU, whose DIEs have
6266 already been loaded into memory. */
6267
6268 static void
6269 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
6270 {
6271 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6272 unrelocated_addr lowpc, highpc;
6273 struct compunit_symtab *cust;
6274 struct block *static_block;
6275 CORE_ADDR addr;
6276
6277 /* Clear the list here in case something was left over. */
6278 cu->method_list.clear ();
6279
6280 dwarf2_find_base_address (cu->dies, cu);
6281
6282 /* Before we start reading the top-level DIE, ensure it has a valid tag
6283 type. */
6284 switch (cu->dies->tag)
6285 {
6286 case DW_TAG_compile_unit:
6287 case DW_TAG_partial_unit:
6288 case DW_TAG_type_unit:
6289 break;
6290 default:
6291 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6292 dwarf_tag_name (cu->dies->tag),
6293 sect_offset_str (cu->per_cu->sect_off),
6294 objfile_name (per_objfile->objfile));
6295 }
6296
6297 /* Do line number decoding in read_file_scope () */
6298 process_die (cu->dies, cu);
6299
6300 /* For now fudge the Go package. */
6301 if (cu->lang () == language_go)
6302 fixup_go_packaging (cu);
6303
6304 /* Now that we have processed all the DIEs in the CU, all the types
6305 should be complete, and it should now be safe to compute all of the
6306 physnames. */
6307 compute_delayed_physnames (cu);
6308
6309 if (cu->lang () == language_rust)
6310 rust_union_quirks (cu);
6311
6312 /* Some compilers don't define a DW_AT_high_pc attribute for the
6313 compilation unit. If the DW_AT_high_pc is missing, synthesize
6314 it, by scanning the DIE's below the compilation unit. */
6315 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6316
6317 addr = per_objfile->relocate (highpc);
6318 static_block
6319 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
6320
6321 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6322 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6323 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6324 addrmap to help ensure it has an accurate map of pc values belonging to
6325 this comp unit. */
6326 dwarf2_record_block_ranges (cu->dies, static_block, cu);
6327
6328 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
6329 (static_block, 0);
6330
6331 if (cust != NULL)
6332 {
6333 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6334
6335 /* Set symtab language to language from DW_AT_language. If the
6336 compilation is from a C file generated by language preprocessors, do
6337 not set the language if it was already deduced by start_subfile. */
6338 if (!(cu->lang () == language_c
6339 && cust->primary_filetab ()->language () != language_unknown))
6340 cust->primary_filetab ()->set_language (cu->lang ());
6341
6342 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6343 produce DW_AT_location with location lists but it can be possibly
6344 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6345 there were bugs in prologue debug info, fixed later in GCC-4.5
6346 by "unwind info for epilogues" patch (which is not directly related).
6347
6348 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6349 needed, it would be wrong due to missing DW_AT_producer there.
6350
6351 Still one can confuse GDB by using non-standard GCC compilation
6352 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6353 */
6354 if (cu->has_loclist && gcc_4_minor >= 5)
6355 cust->set_locations_valid (true);
6356
6357 int major, minor;
6358 if (cu->producer != nullptr
6359 && producer_is_gcc (cu->producer, &major, &minor)
6360 && (major < 4 || (major == 4 && minor < 5)))
6361 /* Don't trust gcc < 4.5.x. */
6362 cust->set_epilogue_unwind_valid (false);
6363 else
6364 cust->set_epilogue_unwind_valid (true);
6365
6366 cust->set_call_site_htab (cu->call_site_htab);
6367 }
6368
6369 per_objfile->set_symtab (cu->per_cu, cust);
6370
6371 /* Push it for inclusion processing later. */
6372 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
6373
6374 /* Not needed any more. */
6375 cu->reset_builder ();
6376 }
6377
6378 /* Generate full symbol information for type unit CU, whose DIEs have
6379 already been loaded into memory. */
6380
6381 static void
6382 process_full_type_unit (dwarf2_cu *cu,
6383 enum language pretend_language)
6384 {
6385 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6386 struct compunit_symtab *cust;
6387 struct signatured_type *sig_type;
6388
6389 gdb_assert (cu->per_cu->is_debug_types);
6390 sig_type = (struct signatured_type *) cu->per_cu;
6391
6392 /* Clear the list here in case something was left over. */
6393 cu->method_list.clear ();
6394
6395 /* The symbol tables are set up in read_type_unit_scope. */
6396 process_die (cu->dies, cu);
6397
6398 /* For now fudge the Go package. */
6399 if (cu->lang () == language_go)
6400 fixup_go_packaging (cu);
6401
6402 /* Now that we have processed all the DIEs in the CU, all the types
6403 should be complete, and it should now be safe to compute all of the
6404 physnames. */
6405 compute_delayed_physnames (cu);
6406
6407 if (cu->lang () == language_rust)
6408 rust_union_quirks (cu);
6409
6410 /* TUs share symbol tables.
6411 If this is the first TU to use this symtab, complete the construction
6412 of it with end_expandable_symtab. Otherwise, complete the addition of
6413 this TU's symbols to the existing symtab. */
6414 type_unit_group_unshareable *tug_unshare =
6415 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
6416 if (tug_unshare->compunit_symtab == NULL)
6417 {
6418 buildsym_compunit *builder = cu->get_builder ();
6419 cust = builder->end_expandable_symtab (0);
6420 tug_unshare->compunit_symtab = cust;
6421
6422 if (cust != NULL)
6423 {
6424 /* Set symtab language to language from DW_AT_language. If the
6425 compilation is from a C file generated by language preprocessors,
6426 do not set the language if it was already deduced by
6427 start_subfile. */
6428 if (!(cu->lang () == language_c
6429 && cust->primary_filetab ()->language () != language_c))
6430 cust->primary_filetab ()->set_language (cu->lang ());
6431 }
6432 }
6433 else
6434 {
6435 cu->get_builder ()->augment_type_symtab ();
6436 cust = tug_unshare->compunit_symtab;
6437 }
6438
6439 per_objfile->set_symtab (cu->per_cu, cust);
6440
6441 /* Not needed any more. */
6442 cu->reset_builder ();
6443 }
6444
6445 /* Process an imported unit DIE. */
6446
6447 static void
6448 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6449 {
6450 struct attribute *attr;
6451
6452 /* For now we don't handle imported units in type units. */
6453 if (cu->per_cu->is_debug_types)
6454 {
6455 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6456 " supported in type units [in module %s]"),
6457 objfile_name (cu->per_objfile->objfile));
6458 }
6459
6460 attr = dwarf2_attr (die, DW_AT_import, cu);
6461 if (attr != NULL)
6462 {
6463 sect_offset sect_off = attr->get_ref_die_offset ();
6464 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
6465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6466 dwarf2_per_cu_data *per_cu
6467 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
6468 per_objfile->per_bfd);
6469
6470 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
6471 into another compilation unit, at root level. Regard this as a hint,
6472 and ignore it. This is a best effort, it only works if unit_type and
6473 lang are already set. */
6474 if (die->parent && die->parent->parent == NULL
6475 && per_cu->unit_type (false) == DW_UT_compile
6476 && per_cu->lang (false) == language_cplus)
6477 return;
6478
6479 /* If necessary, add it to the queue and load its DIEs. */
6480 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
6481 cu->lang ()))
6482 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
6483 false, cu->lang ());
6484
6485 cu->per_cu->imported_symtabs_push (per_cu);
6486 }
6487 }
6488
6489 /* RAII object that represents a process_die scope: i.e.,
6490 starts/finishes processing a DIE. */
6491 class process_die_scope
6492 {
6493 public:
6494 process_die_scope (die_info *die, dwarf2_cu *cu)
6495 : m_die (die), m_cu (cu)
6496 {
6497 /* We should only be processing DIEs not already in process. */
6498 gdb_assert (!m_die->in_process);
6499 m_die->in_process = true;
6500 }
6501
6502 ~process_die_scope ()
6503 {
6504 m_die->in_process = false;
6505
6506 /* If we're done processing the DIE for the CU that owns the line
6507 header, we don't need the line header anymore. */
6508 if (m_cu->line_header_die_owner == m_die)
6509 {
6510 delete m_cu->line_header;
6511 m_cu->line_header = NULL;
6512 m_cu->line_header_die_owner = NULL;
6513 }
6514 }
6515
6516 private:
6517 die_info *m_die;
6518 dwarf2_cu *m_cu;
6519 };
6520
6521 /* Process a die and its children. */
6522
6523 static void
6524 process_die (struct die_info *die, struct dwarf2_cu *cu)
6525 {
6526 process_die_scope scope (die, cu);
6527
6528 switch (die->tag)
6529 {
6530 case DW_TAG_padding:
6531 break;
6532 case DW_TAG_compile_unit:
6533 case DW_TAG_partial_unit:
6534 read_file_scope (die, cu);
6535 break;
6536 case DW_TAG_type_unit:
6537 read_type_unit_scope (die, cu);
6538 break;
6539 case DW_TAG_subprogram:
6540 /* Nested subprograms in Fortran get a prefix. */
6541 if (cu->lang () == language_fortran
6542 && die->parent != NULL
6543 && die->parent->tag == DW_TAG_subprogram)
6544 cu->processing_has_namespace_info = true;
6545 [[fallthrough]];
6546 /* Fall through. */
6547 case DW_TAG_entry_point:
6548 case DW_TAG_inlined_subroutine:
6549 read_func_scope (die, cu);
6550 break;
6551 case DW_TAG_lexical_block:
6552 case DW_TAG_try_block:
6553 case DW_TAG_catch_block:
6554 read_lexical_block_scope (die, cu);
6555 break;
6556 case DW_TAG_call_site:
6557 case DW_TAG_GNU_call_site:
6558 read_call_site_scope (die, cu);
6559 break;
6560 case DW_TAG_class_type:
6561 case DW_TAG_interface_type:
6562 case DW_TAG_structure_type:
6563 case DW_TAG_union_type:
6564 case DW_TAG_namelist:
6565 process_structure_scope (die, cu);
6566 break;
6567 case DW_TAG_enumeration_type:
6568 process_enumeration_scope (die, cu);
6569 break;
6570
6571 /* These dies have a type, but processing them does not create
6572 a symbol or recurse to process the children. Therefore we can
6573 read them on-demand through read_type_die. */
6574 case DW_TAG_subroutine_type:
6575 case DW_TAG_set_type:
6576 case DW_TAG_pointer_type:
6577 case DW_TAG_ptr_to_member_type:
6578 case DW_TAG_reference_type:
6579 case DW_TAG_rvalue_reference_type:
6580 case DW_TAG_string_type:
6581 break;
6582
6583 case DW_TAG_array_type:
6584 /* We only need to handle this case for Ada -- in other
6585 languages, it's normal for the compiler to emit a typedef
6586 instead. */
6587 if (cu->lang () != language_ada)
6588 break;
6589 [[fallthrough]];
6590 case DW_TAG_base_type:
6591 case DW_TAG_subrange_type:
6592 case DW_TAG_generic_subrange:
6593 case DW_TAG_typedef:
6594 /* Add a typedef symbol for the type definition, if it has a
6595 DW_AT_name. */
6596 new_symbol (die, read_type_die (die, cu), cu);
6597 break;
6598 case DW_TAG_common_block:
6599 read_common_block (die, cu);
6600 break;
6601 case DW_TAG_common_inclusion:
6602 break;
6603 case DW_TAG_namespace:
6604 cu->processing_has_namespace_info = true;
6605 read_namespace (die, cu);
6606 break;
6607 case DW_TAG_module:
6608 cu->processing_has_namespace_info = true;
6609 read_module (die, cu);
6610 break;
6611 case DW_TAG_imported_declaration:
6612 cu->processing_has_namespace_info = true;
6613 if (read_alias (die, cu))
6614 break;
6615 /* The declaration is neither a global namespace nor a variable
6616 alias. */
6617 [[fallthrough]];
6618 case DW_TAG_imported_module:
6619 cu->processing_has_namespace_info = true;
6620 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6621 || cu->lang () != language_fortran))
6622 complaint (_("Tag '%s' has unexpected children"),
6623 dwarf_tag_name (die->tag));
6624 read_import_statement (die, cu);
6625 break;
6626
6627 case DW_TAG_imported_unit:
6628 process_imported_unit_die (die, cu);
6629 break;
6630
6631 case DW_TAG_variable:
6632 read_variable (die, cu);
6633 break;
6634
6635 default:
6636 new_symbol (die, NULL, cu);
6637 break;
6638 }
6639 }
6640 \f
6641 /* DWARF name computation. */
6642
6643 /* A helper function for dwarf2_compute_name which determines whether DIE
6644 needs to have the name of the scope prepended to the name listed in the
6645 die. */
6646
6647 static int
6648 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6649 {
6650 struct attribute *attr;
6651
6652 switch (die->tag)
6653 {
6654 case DW_TAG_namespace:
6655 case DW_TAG_typedef:
6656 case DW_TAG_class_type:
6657 case DW_TAG_interface_type:
6658 case DW_TAG_structure_type:
6659 case DW_TAG_union_type:
6660 case DW_TAG_enumeration_type:
6661 case DW_TAG_enumerator:
6662 case DW_TAG_subprogram:
6663 case DW_TAG_inlined_subroutine:
6664 case DW_TAG_entry_point:
6665 case DW_TAG_member:
6666 case DW_TAG_imported_declaration:
6667 return 1;
6668
6669 case DW_TAG_variable:
6670 case DW_TAG_constant:
6671 /* We only need to prefix "globally" visible variables. These include
6672 any variable marked with DW_AT_external or any variable that
6673 lives in a namespace. [Variables in anonymous namespaces
6674 require prefixing, but they are not DW_AT_external.] */
6675
6676 if (dwarf2_attr (die, DW_AT_specification, cu))
6677 {
6678 struct dwarf2_cu *spec_cu = cu;
6679
6680 return die_needs_namespace (die_specification (die, &spec_cu),
6681 spec_cu);
6682 }
6683
6684 attr = dwarf2_attr (die, DW_AT_external, cu);
6685 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6686 && die->parent->tag != DW_TAG_module)
6687 return 0;
6688 /* A variable in a lexical block of some kind does not need a
6689 namespace, even though in C++ such variables may be external
6690 and have a mangled name. */
6691 if (die->parent->tag == DW_TAG_lexical_block
6692 || die->parent->tag == DW_TAG_try_block
6693 || die->parent->tag == DW_TAG_catch_block
6694 || die->parent->tag == DW_TAG_subprogram)
6695 return 0;
6696 return 1;
6697
6698 default:
6699 return 0;
6700 }
6701 }
6702
6703 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
6704 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6705 defined for the given DIE. */
6706
6707 static struct attribute *
6708 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
6709 {
6710 struct attribute *attr;
6711
6712 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6713 if (attr == NULL)
6714 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6715
6716 return attr;
6717 }
6718
6719 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
6720 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6721 defined for the given DIE. */
6722
6723 static const char *
6724 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
6725 {
6726 const char *linkage_name;
6727
6728 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
6729 if (linkage_name == NULL)
6730 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
6731
6732 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
6733 See https://github.com/rust-lang/rust/issues/32925. */
6734 if (cu->lang () == language_rust && linkage_name != NULL
6735 && strchr (linkage_name, '{') != NULL)
6736 linkage_name = NULL;
6737
6738 return linkage_name;
6739 }
6740
6741 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6742 compute the physname for the object, which include a method's:
6743 - formal parameters (C++),
6744 - receiver type (Go),
6745
6746 The term "physname" is a bit confusing.
6747 For C++, for example, it is the demangled name.
6748 For Go, for example, it's the mangled name.
6749
6750 For Ada, return the DIE's linkage name rather than the fully qualified
6751 name. PHYSNAME is ignored..
6752
6753 The result is allocated on the objfile->per_bfd's obstack and
6754 canonicalized. */
6755
6756 static const char *
6757 dwarf2_compute_name (const char *name,
6758 struct die_info *die, struct dwarf2_cu *cu,
6759 int physname)
6760 {
6761 struct objfile *objfile = cu->per_objfile->objfile;
6762
6763 if (name == NULL)
6764 name = dwarf2_name (die, cu);
6765
6766 enum language lang = cu->lang ();
6767
6768 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
6769 but otherwise compute it by typename_concat inside GDB.
6770 FIXME: Actually this is not really true, or at least not always true.
6771 It's all very confusing. compute_and_set_names doesn't try to demangle
6772 Fortran names because there is no mangling standard. So new_symbol
6773 will set the demangled name to the result of dwarf2_full_name, and it is
6774 the demangled name that GDB uses if it exists. */
6775 if (lang == language_ada
6776 || (lang == language_fortran && physname))
6777 {
6778 /* For Ada unit, we prefer the linkage name over the name, as
6779 the former contains the exported name, which the user expects
6780 to be able to reference. Ideally, we want the user to be able
6781 to reference this entity using either natural or linkage name,
6782 but we haven't started looking at this enhancement yet. */
6783 const char *linkage_name = dw2_linkage_name (die, cu);
6784
6785 if (linkage_name != NULL)
6786 return linkage_name;
6787 }
6788
6789 /* These are the only languages we know how to qualify names in. */
6790 if (name != NULL
6791 && (lang == language_cplus
6792 || lang == language_fortran || lang == language_d
6793 || lang == language_rust))
6794 {
6795 if (die_needs_namespace (die, cu))
6796 {
6797 const char *prefix;
6798
6799 string_file buf;
6800
6801 prefix = determine_prefix (die, cu);
6802 if (*prefix != '\0')
6803 {
6804 gdb::unique_xmalloc_ptr<char> prefixed_name
6805 (typename_concat (NULL, prefix, name, physname, cu));
6806
6807 buf.puts (prefixed_name.get ());
6808 }
6809 else
6810 buf.puts (name);
6811
6812 /* Template parameters may be specified in the DIE's DW_AT_name, or
6813 as children with DW_TAG_template_type_param or
6814 DW_TAG_value_type_param. If the latter, add them to the name
6815 here. If the name already has template parameters, then
6816 skip this step; some versions of GCC emit both, and
6817 it is more efficient to use the pre-computed name.
6818
6819 Something to keep in mind about this process: it is very
6820 unlikely, or in some cases downright impossible, to produce
6821 something that will match the mangled name of a function.
6822 If the definition of the function has the same debug info,
6823 we should be able to match up with it anyway. But fallbacks
6824 using the minimal symbol, for instance to find a method
6825 implemented in a stripped copy of libstdc++, will not work.
6826 If we do not have debug info for the definition, we will have to
6827 match them up some other way.
6828
6829 When we do name matching there is a related problem with function
6830 templates; two instantiated function templates are allowed to
6831 differ only by their return types, which we do not add here. */
6832
6833 if (lang == language_cplus && strchr (name, '<') == NULL)
6834 {
6835 struct attribute *attr;
6836 struct die_info *child;
6837 int first = 1;
6838
6839 die->building_fullname = 1;
6840
6841 for (child = die->child; child != NULL; child = child->sibling)
6842 {
6843 struct type *type;
6844 LONGEST value;
6845 const gdb_byte *bytes;
6846 struct dwarf2_locexpr_baton *baton;
6847 struct value *v;
6848
6849 if (child->tag != DW_TAG_template_type_param
6850 && child->tag != DW_TAG_template_value_param)
6851 continue;
6852
6853 if (first)
6854 {
6855 buf.puts ("<");
6856 first = 0;
6857 }
6858 else
6859 buf.puts (", ");
6860
6861 attr = dwarf2_attr (child, DW_AT_type, cu);
6862 if (attr == NULL)
6863 {
6864 complaint (_("template parameter missing DW_AT_type"));
6865 buf.puts ("UNKNOWN_TYPE");
6866 continue;
6867 }
6868 type = die_type (child, cu);
6869
6870 if (child->tag == DW_TAG_template_type_param)
6871 {
6872 cu->language_defn->print_type (type, "", &buf, -1, 0,
6873 &type_print_raw_options);
6874 continue;
6875 }
6876
6877 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6878 if (attr == NULL)
6879 {
6880 complaint (_("template parameter missing "
6881 "DW_AT_const_value"));
6882 buf.puts ("UNKNOWN_VALUE");
6883 continue;
6884 }
6885
6886 dwarf2_const_value_attr (attr, type, name,
6887 &cu->comp_unit_obstack, cu,
6888 &value, &bytes, &baton);
6889
6890 if (type->has_no_signedness ())
6891 /* GDB prints characters as NUMBER 'CHAR'. If that's
6892 changed, this can use value_print instead. */
6893 cu->language_defn->printchar (value, type, &buf);
6894 else
6895 {
6896 struct value_print_options opts;
6897
6898 if (baton != NULL)
6899 v = dwarf2_evaluate_loc_desc (type, NULL,
6900 baton->data,
6901 baton->size,
6902 baton->per_cu,
6903 baton->per_objfile);
6904 else if (bytes != NULL)
6905 {
6906 v = value::allocate (type);
6907 memcpy (v->contents_writeable ().data (), bytes,
6908 type->length ());
6909 }
6910 else
6911 v = value_from_longest (type, value);
6912
6913 /* Specify decimal so that we do not depend on
6914 the radix. */
6915 get_formatted_print_options (&opts, 'd');
6916 opts.raw = true;
6917 value_print (v, &buf, &opts);
6918 release_value (v);
6919 }
6920 }
6921
6922 die->building_fullname = 0;
6923
6924 if (!first)
6925 {
6926 /* Close the argument list, with a space if necessary
6927 (nested templates). */
6928 if (!buf.empty () && buf.string ().back () == '>')
6929 buf.puts (" >");
6930 else
6931 buf.puts (">");
6932 }
6933 }
6934
6935 /* For C++ methods, append formal parameter type
6936 information, if PHYSNAME. */
6937
6938 if (physname && die->tag == DW_TAG_subprogram
6939 && lang == language_cplus)
6940 {
6941 struct type *type = read_type_die (die, cu);
6942
6943 c_type_print_args (type, &buf, 1, lang,
6944 &type_print_raw_options);
6945
6946 if (lang == language_cplus)
6947 {
6948 /* Assume that an artificial first parameter is
6949 "this", but do not crash if it is not. RealView
6950 marks unnamed (and thus unused) parameters as
6951 artificial; there is no way to differentiate
6952 the two cases. */
6953 if (type->num_fields () > 0
6954 && type->field (0).is_artificial ()
6955 && type->field (0).type ()->code () == TYPE_CODE_PTR
6956 && TYPE_CONST (type->field (0).type ()->target_type ()))
6957 buf.puts (" const");
6958 }
6959 }
6960
6961 const std::string &intermediate_name = buf.string ();
6962
6963 const char *canonical_name
6964 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
6965 objfile);
6966
6967 /* If we only computed INTERMEDIATE_NAME, or if
6968 INTERMEDIATE_NAME is already canonical, then we need to
6969 intern it. */
6970 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
6971 name = objfile->intern (intermediate_name);
6972 else
6973 name = canonical_name;
6974 }
6975 }
6976
6977 return name;
6978 }
6979
6980 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6981 If scope qualifiers are appropriate they will be added. The result
6982 will be allocated on the storage_obstack, or NULL if the DIE does
6983 not have a name. NAME may either be from a previous call to
6984 dwarf2_name or NULL.
6985
6986 The output string will be canonicalized (if C++). */
6987
6988 static const char *
6989 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
6990 {
6991 return dwarf2_compute_name (name, die, cu, 0);
6992 }
6993
6994 /* Construct a physname for the given DIE in CU. NAME may either be
6995 from a previous call to dwarf2_name or NULL. The result will be
6996 allocated on the objfile_objstack or NULL if the DIE does not have a
6997 name.
6998
6999 The output string will be canonicalized (if C++). */
7000
7001 static const char *
7002 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7003 {
7004 struct objfile *objfile = cu->per_objfile->objfile;
7005 const char *retval, *mangled = NULL, *canon = NULL;
7006 int need_copy = 1;
7007
7008 /* In this case dwarf2_compute_name is just a shortcut not building anything
7009 on its own. */
7010 if (!die_needs_namespace (die, cu))
7011 return dwarf2_compute_name (name, die, cu, 1);
7012
7013 if (cu->lang () != language_rust)
7014 mangled = dw2_linkage_name (die, cu);
7015
7016 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7017 has computed. */
7018 gdb::unique_xmalloc_ptr<char> demangled;
7019 if (mangled != NULL)
7020 {
7021 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
7022 {
7023 /* Do nothing (do not demangle the symbol name). */
7024 }
7025 else
7026 {
7027 /* Use DMGL_RET_DROP for C++ template functions to suppress
7028 their return type. It is easier for GDB users to search
7029 for such functions as `name(params)' than `long name(params)'.
7030 In such case the minimal symbol names do not match the full
7031 symbol names but for template functions there is never a need
7032 to look up their definition from their declaration so
7033 the only disadvantage remains the minimal symbol variant
7034 `long name(params)' does not have the proper inferior type. */
7035 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
7036 | DMGL_RET_DROP));
7037 }
7038 if (demangled)
7039 canon = demangled.get ();
7040 else
7041 {
7042 canon = mangled;
7043 need_copy = 0;
7044 }
7045 }
7046
7047 if (canon == NULL || check_physname)
7048 {
7049 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7050
7051 if (canon != NULL && strcmp (physname, canon) != 0)
7052 {
7053 /* It may not mean a bug in GDB. The compiler could also
7054 compute DW_AT_linkage_name incorrectly. But in such case
7055 GDB would need to be bug-to-bug compatible. */
7056
7057 complaint (_("Computed physname <%s> does not match demangled <%s> "
7058 "(from linkage <%s>) - DIE at %s [in module %s]"),
7059 physname, canon, mangled, sect_offset_str (die->sect_off),
7060 objfile_name (objfile));
7061
7062 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7063 is available here - over computed PHYSNAME. It is safer
7064 against both buggy GDB and buggy compilers. */
7065
7066 retval = canon;
7067 }
7068 else
7069 {
7070 retval = physname;
7071 need_copy = 0;
7072 }
7073 }
7074 else
7075 retval = canon;
7076
7077 if (need_copy)
7078 retval = objfile->intern (retval);
7079
7080 return retval;
7081 }
7082
7083 /* Inspect DIE in CU for a namespace alias or a variable with alias
7084 attribute. If one exists, record a new symbol for it.
7085
7086 Returns true if an alias was recorded, false otherwise. */
7087
7088 static bool
7089 read_alias (struct die_info *die, struct dwarf2_cu *cu)
7090 {
7091 struct attribute *attr;
7092
7093 /* If the die does not have a name, this is neither a namespace
7094 alias nor a variable alias. */
7095 attr = dwarf2_attr (die, DW_AT_name, cu);
7096 if (attr != NULL)
7097 {
7098 int num;
7099 struct die_info *d = die;
7100 struct dwarf2_cu *imported_cu = cu;
7101
7102 /* If the compiler has nested DW_AT_imported_declaration DIEs,
7103 keep inspecting DIEs until we hit the underlying import. */
7104 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
7105 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
7106 {
7107 attr = dwarf2_attr (d, DW_AT_import, cu);
7108 if (attr == NULL)
7109 break;
7110
7111 d = follow_die_ref (d, attr, &imported_cu);
7112 if (d->tag != DW_TAG_imported_declaration)
7113 break;
7114 }
7115
7116 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
7117 {
7118 complaint (_("DIE at %s has too many recursively imported "
7119 "declarations"), sect_offset_str (d->sect_off));
7120 return false;
7121 }
7122
7123 if (attr != NULL)
7124 {
7125 struct type *type;
7126 if (d->tag == DW_TAG_variable)
7127 {
7128 /* This declaration is a C/C++ global variable alias.
7129 Add a symbol for it whose type is the same as the
7130 aliased variable's. */
7131 type = die_type (d, imported_cu);
7132 struct symbol *sym = new_symbol (die, type, cu);
7133 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
7134 sym->set_aclass_index (LOC_UNRESOLVED);
7135 if (attr != nullptr)
7136 var_decode_location (attr, sym, cu);
7137 return true;
7138 }
7139 else
7140 {
7141 sect_offset sect_off = attr->get_ref_die_offset ();
7142 type = get_die_type_at_offset (sect_off, cu->per_cu,
7143 cu->per_objfile);
7144 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
7145 {
7146 /* This declaration is a global namespace alias. Add
7147 a symbol for it whose type is the aliased
7148 namespace. */
7149 new_symbol (die, type, cu);
7150 return true;
7151 }
7152 }
7153 }
7154 }
7155 return false;
7156 }
7157
7158 /* Return the using directives repository (global or local?) to use in the
7159 current context for CU.
7160
7161 For Ada, imported declarations can materialize renamings, which *may* be
7162 global. However it is impossible (for now?) in DWARF to distinguish
7163 "external" imported declarations and "static" ones. As all imported
7164 declarations seem to be static in all other languages, make them all CU-wide
7165 global only in Ada. */
7166
7167 static struct using_direct **
7168 using_directives (struct dwarf2_cu *cu)
7169 {
7170 if (cu->lang () == language_ada
7171 && cu->get_builder ()->outermost_context_p ())
7172 return cu->get_builder ()->get_global_using_directives ();
7173 else
7174 return cu->get_builder ()->get_local_using_directives ();
7175 }
7176
7177 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
7178 given CU. If the format is not recognized or the attribute is
7179 not present, set it to 0. */
7180
7181 static unsigned int
7182 read_decl_line (struct die_info *die, struct dwarf2_cu *cu)
7183 {
7184 struct attribute *decl_line = dwarf2_attr (die, DW_AT_decl_line, cu);
7185 if (decl_line == nullptr)
7186 return 0;
7187 if (decl_line->form_is_constant ())
7188 {
7189 LONGEST val = decl_line->constant_value (0);
7190 if (0 <= val && val <= UINT_MAX)
7191 return (unsigned int) val;
7192
7193 complaint (_("Declared line for using directive is too large"));
7194 return 0;
7195 }
7196
7197 complaint (_("Declared line for using directive is of incorrect format"));
7198 return 0;
7199 }
7200
7201 /* Read the import statement specified by the given die and record it. */
7202
7203 static void
7204 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7205 {
7206 struct objfile *objfile = cu->per_objfile->objfile;
7207 struct attribute *import_attr;
7208 struct die_info *imported_die, *child_die;
7209 struct dwarf2_cu *imported_cu;
7210 const char *imported_name;
7211 const char *imported_name_prefix;
7212 const char *canonical_name;
7213 const char *import_alias;
7214 const char *imported_declaration = NULL;
7215 const char *import_prefix;
7216 std::vector<const char *> excludes;
7217
7218 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7219 if (import_attr == NULL)
7220 {
7221 complaint (_("Tag '%s' has no DW_AT_import"),
7222 dwarf_tag_name (die->tag));
7223 return;
7224 }
7225
7226 imported_cu = cu;
7227 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7228 imported_name = dwarf2_name (imported_die, imported_cu);
7229 if (imported_name == NULL)
7230 {
7231 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7232
7233 The import in the following code:
7234 namespace A
7235 {
7236 typedef int B;
7237 }
7238
7239 int main ()
7240 {
7241 using A::B;
7242 B b;
7243 return b;
7244 }
7245
7246 ...
7247 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7248 <52> DW_AT_decl_file : 1
7249 <53> DW_AT_decl_line : 6
7250 <54> DW_AT_import : <0x75>
7251 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7252 <59> DW_AT_name : B
7253 <5b> DW_AT_decl_file : 1
7254 <5c> DW_AT_decl_line : 2
7255 <5d> DW_AT_type : <0x6e>
7256 ...
7257 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7258 <76> DW_AT_byte_size : 4
7259 <77> DW_AT_encoding : 5 (signed)
7260
7261 imports the wrong die ( 0x75 instead of 0x58 ).
7262 This case will be ignored until the gcc bug is fixed. */
7263 return;
7264 }
7265
7266 /* Figure out the local name after import. */
7267 import_alias = dwarf2_name (die, cu);
7268
7269 /* Figure out where the statement is being imported to. */
7270 import_prefix = determine_prefix (die, cu);
7271
7272 /* Figure out what the scope of the imported die is and prepend it
7273 to the name of the imported die. */
7274 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7275
7276 if (imported_die->tag != DW_TAG_namespace
7277 && imported_die->tag != DW_TAG_module)
7278 {
7279 imported_declaration = imported_name;
7280 canonical_name = imported_name_prefix;
7281 }
7282 else if (strlen (imported_name_prefix) > 0)
7283 canonical_name = obconcat (&objfile->objfile_obstack,
7284 imported_name_prefix,
7285 (cu->lang () == language_d
7286 ? "."
7287 : "::"),
7288 imported_name, (char *) NULL);
7289 else
7290 canonical_name = imported_name;
7291
7292 if (die->tag == DW_TAG_imported_module
7293 && cu->lang () == language_fortran)
7294 for (child_die = die->child; child_die && child_die->tag;
7295 child_die = child_die->sibling)
7296 {
7297 /* DWARF-4: A Fortran use statement with a “rename list” may be
7298 represented by an imported module entry with an import attribute
7299 referring to the module and owned entries corresponding to those
7300 entities that are renamed as part of being imported. */
7301
7302 if (child_die->tag != DW_TAG_imported_declaration)
7303 {
7304 complaint (_("child DW_TAG_imported_declaration expected "
7305 "- DIE at %s [in module %s]"),
7306 sect_offset_str (child_die->sect_off),
7307 objfile_name (objfile));
7308 continue;
7309 }
7310
7311 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7312 if (import_attr == NULL)
7313 {
7314 complaint (_("Tag '%s' has no DW_AT_import"),
7315 dwarf_tag_name (child_die->tag));
7316 continue;
7317 }
7318
7319 imported_cu = cu;
7320 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7321 &imported_cu);
7322 imported_name = dwarf2_name (imported_die, imported_cu);
7323 if (imported_name == NULL)
7324 {
7325 complaint (_("child DW_TAG_imported_declaration has unknown "
7326 "imported name - DIE at %s [in module %s]"),
7327 sect_offset_str (child_die->sect_off),
7328 objfile_name (objfile));
7329 continue;
7330 }
7331
7332 excludes.push_back (imported_name);
7333
7334 process_die (child_die, cu);
7335 }
7336
7337 add_using_directive (using_directives (cu),
7338 import_prefix,
7339 canonical_name,
7340 import_alias,
7341 imported_declaration,
7342 excludes,
7343 read_decl_line (die, cu),
7344 0,
7345 &objfile->objfile_obstack);
7346 }
7347
7348 /* ICC<14 does not output the required DW_AT_declaration on incomplete
7349 types, but gives them a size of zero. Starting with version 14,
7350 ICC is compatible with GCC. */
7351
7352 static bool
7353 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
7354 {
7355 if (!cu->checked_producer)
7356 check_producer (cu);
7357
7358 return cu->producer_is_icc_lt_14;
7359 }
7360
7361 /* ICC generates a DW_AT_type for C void functions. This was observed on
7362 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
7363 which says that void functions should not have a DW_AT_type. */
7364
7365 static bool
7366 producer_is_icc (struct dwarf2_cu *cu)
7367 {
7368 if (!cu->checked_producer)
7369 check_producer (cu);
7370
7371 return cu->producer_is_icc;
7372 }
7373
7374 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7375 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7376 this, it was first present in GCC release 4.3.0. */
7377
7378 static bool
7379 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7380 {
7381 if (!cu->checked_producer)
7382 check_producer (cu);
7383
7384 return cu->producer_is_gcc_lt_4_3;
7385 }
7386
7387 /* See dwarf2/read.h. */
7388 bool
7389 producer_is_clang (struct dwarf2_cu *cu)
7390 {
7391 if (!cu->checked_producer)
7392 check_producer (cu);
7393
7394 return cu->producer_is_clang;
7395 }
7396
7397 static file_and_directory &
7398 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
7399 {
7400 if (cu->per_cu->fnd != nullptr)
7401 return *cu->per_cu->fnd;
7402
7403 /* Find the filename. Do not use dwarf2_name here, since the filename
7404 is not a source language identifier. */
7405 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
7406 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
7407
7408 if (res.get_comp_dir () == nullptr
7409 && producer_is_gcc_lt_4_3 (cu)
7410 && res.get_name () != nullptr
7411 && IS_ABSOLUTE_PATH (res.get_name ()))
7412 {
7413 res.set_comp_dir (ldirname (res.get_name ()));
7414 res.set_name (make_unique_xstrdup (lbasename (res.get_name ())));
7415 }
7416
7417 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
7418 return *cu->per_cu->fnd;
7419 }
7420
7421 /* Handle DW_AT_stmt_list for a compilation unit.
7422 DIE is the DW_TAG_compile_unit die for CU.
7423 COMP_DIR is the compilation directory. LOWPC is passed to
7424 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
7425
7426 static void
7427 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7428 const file_and_directory &fnd, unrelocated_addr lowpc,
7429 bool have_code) /* ARI: editCase function */
7430 {
7431 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7432 struct attribute *attr;
7433 hashval_t line_header_local_hash;
7434 void **slot;
7435 int decode_mapping;
7436
7437 gdb_assert (! cu->per_cu->is_debug_types);
7438
7439 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7440 if (attr == NULL || !attr->form_is_unsigned ())
7441 return;
7442
7443 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7444
7445 /* The line header hash table is only created if needed (it exists to
7446 prevent redundant reading of the line table for partial_units).
7447 If we're given a partial_unit, we'll need it. If we're given a
7448 compile_unit, then use the line header hash table if it's already
7449 created, but don't create one just yet. */
7450
7451 if (per_objfile->line_header_hash == NULL
7452 && die->tag == DW_TAG_partial_unit)
7453 {
7454 per_objfile->line_header_hash
7455 .reset (htab_create_alloc (127, line_header_hash_voidp,
7456 line_header_eq_voidp,
7457 htab_delete_entry<line_header>,
7458 xcalloc, xfree));
7459 }
7460
7461 line_header line_header_local (line_offset, cu->per_cu->is_dwz);
7462 line_header_local_hash = line_header_hash (&line_header_local);
7463 if (per_objfile->line_header_hash != NULL)
7464 {
7465 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7466 &line_header_local,
7467 line_header_local_hash, NO_INSERT);
7468
7469 /* For DW_TAG_compile_unit we need info like symtab::linetable which
7470 is not present in *SLOT (since if there is something in *SLOT then
7471 it will be for a partial_unit). */
7472 if (die->tag == DW_TAG_partial_unit && slot != NULL)
7473 {
7474 gdb_assert (*slot != NULL);
7475 cu->line_header = (struct line_header *) *slot;
7476 return;
7477 }
7478 }
7479
7480 /* dwarf_decode_line_header does not yet provide sufficient information.
7481 We always have to call also dwarf_decode_lines for it. */
7482 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
7483 fnd.get_comp_dir ());
7484 if (lh == NULL)
7485 return;
7486
7487 cu->line_header = lh.release ();
7488 cu->line_header_die_owner = die;
7489
7490 if (per_objfile->line_header_hash == NULL)
7491 slot = NULL;
7492 else
7493 {
7494 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7495 &line_header_local,
7496 line_header_local_hash, INSERT);
7497 gdb_assert (slot != NULL);
7498 }
7499 if (slot != NULL && *slot == NULL)
7500 {
7501 /* This newly decoded line number information unit will be owned
7502 by line_header_hash hash table. */
7503 *slot = cu->line_header;
7504 cu->line_header_die_owner = NULL;
7505 }
7506 else
7507 {
7508 /* We cannot free any current entry in (*slot) as that struct line_header
7509 may be already used by multiple CUs. Create only temporary decoded
7510 line_header for this CU - it may happen at most once for each line
7511 number information unit. And if we're not using line_header_hash
7512 then this is what we want as well. */
7513 gdb_assert (die->tag != DW_TAG_partial_unit);
7514 }
7515 decode_mapping = (die->tag != DW_TAG_partial_unit);
7516 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
7517 then there won't be any interesting code in the CU, but a check later on
7518 (in lnp_state_machine::check_line_address) will fail to properly exclude
7519 an entry that was removed via --gc-sections. */
7520 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping && have_code);
7521 }
7522
7523 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7524
7525 static void
7526 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7527 {
7528 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7529 struct objfile *objfile = per_objfile->objfile;
7530 CORE_ADDR lowpc;
7531 struct attribute *attr;
7532 struct die_info *child_die;
7533
7534 prepare_one_comp_unit (cu, die, cu->lang ());
7535
7536 unrelocated_addr unrel_low, unrel_high;
7537 get_scope_pc_bounds (die, &unrel_low, &unrel_high, cu);
7538
7539 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7540 from finish_block. */
7541 if (unrel_low == ((unrelocated_addr) -1))
7542 unrel_low = unrel_high;
7543 lowpc = per_objfile->relocate (unrel_low);
7544
7545 file_and_directory &fnd = find_file_and_directory (die, cu);
7546
7547 /* GAS supports generating dwarf-5 info starting version 2.35. Versions
7548 2.35-2.37 generate an incorrect CU name attribute: it's relative,
7549 implicitly prefixing it with the compilation dir. Work around this by
7550 prefixing it with the source dir instead. */
7551 if (cu->header.version == 5 && !IS_ABSOLUTE_PATH (fnd.get_name ())
7552 && producer_is_gas_lt_2_38 (cu))
7553 {
7554 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7555 if (attr != nullptr && attr->form_is_unsigned ())
7556 {
7557 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7558 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
7559 fnd.get_comp_dir ());
7560 if (lh->version == 5 && lh->is_valid_file_index (1))
7561 {
7562 std::string dir = lh->include_dir_at (1);
7563 fnd.set_comp_dir (std::move (dir));
7564 }
7565 }
7566 }
7567
7568 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
7569 lowpc);
7570
7571 gdb_assert (per_objfile->sym_cu == nullptr);
7572 scoped_restore restore_sym_cu
7573 = make_scoped_restore (&per_objfile->sym_cu, cu);
7574
7575 /* Decode line number information if present. We do this before
7576 processing child DIEs, so that the line header table is available
7577 for DW_AT_decl_file. */
7578 handle_DW_AT_stmt_list (die, cu, fnd, unrel_low, unrel_low != unrel_high);
7579
7580 /* Process all dies in compilation unit. */
7581 if (die->child != NULL)
7582 {
7583 child_die = die->child;
7584 while (child_die && child_die->tag)
7585 {
7586 process_die (child_die, cu);
7587 child_die = child_die->sibling;
7588 }
7589 }
7590 per_objfile->sym_cu = nullptr;
7591
7592 /* Decode macro information, if present. Dwarf 2 macro information
7593 refers to information in the line number info statement program
7594 header, so we can only read it if we've read the header
7595 successfully. */
7596 attr = dwarf2_attr (die, DW_AT_macros, cu);
7597 if (attr == NULL)
7598 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7599 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7600 {
7601 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7602 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
7603
7604 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
7605 }
7606 else
7607 {
7608 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7609 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7610 {
7611 unsigned int macro_offset = attr->as_unsigned ();
7612
7613 dwarf_decode_macros (cu, macro_offset, 0);
7614 }
7615 }
7616 }
7617
7618 void
7619 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
7620 {
7621 struct type_unit_group *tu_group;
7622 int first_time;
7623 struct attribute *attr;
7624 unsigned int i;
7625 struct signatured_type *sig_type;
7626
7627 gdb_assert (per_cu->is_debug_types);
7628 sig_type = (struct signatured_type *) per_cu;
7629
7630 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
7631
7632 /* If we're using .gdb_index (includes -readnow) then
7633 per_cu->type_unit_group may not have been set up yet. */
7634 if (sig_type->type_unit_group == NULL)
7635 sig_type->type_unit_group = get_type_unit_group (this, attr);
7636 tu_group = sig_type->type_unit_group;
7637
7638 /* If we've already processed this stmt_list there's no real need to
7639 do it again, we could fake it and just recreate the part we need
7640 (file name,index -> symtab mapping). If data shows this optimization
7641 is useful we can do it then. */
7642 type_unit_group_unshareable *tug_unshare
7643 = per_objfile->get_type_unit_group_unshareable (tu_group);
7644 first_time = tug_unshare->compunit_symtab == NULL;
7645
7646 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7647 debug info. */
7648 line_header_up lh;
7649 if (attr != NULL && attr->form_is_unsigned ())
7650 {
7651 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7652 lh = dwarf_decode_line_header (line_offset, this, nullptr);
7653 }
7654 if (lh == NULL)
7655 {
7656 if (first_time)
7657 start_compunit_symtab ("", NULL, 0);
7658 else
7659 {
7660 gdb_assert (tug_unshare->symtabs == NULL);
7661 gdb_assert (m_builder == nullptr);
7662 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7663 m_builder.reset (new struct buildsym_compunit
7664 (cust->objfile (), "",
7665 cust->dirname (),
7666 cust->language (),
7667 0, cust));
7668 list_in_scope = get_builder ()->get_file_symbols ();
7669 }
7670 return;
7671 }
7672
7673 line_header = lh.release ();
7674 line_header_die_owner = die;
7675
7676 if (first_time)
7677 {
7678 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
7679
7680 /* Note: We don't assign tu_group->compunit_symtab yet because we're
7681 still initializing it, and our caller (a few levels up)
7682 process_full_type_unit still needs to know if this is the first
7683 time. */
7684
7685 tug_unshare->symtabs
7686 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
7687 struct symtab *, line_header->file_names_size ());
7688
7689 auto &file_names = line_header->file_names ();
7690 for (i = 0; i < file_names.size (); ++i)
7691 {
7692 file_entry &fe = file_names[i];
7693 dwarf2_start_subfile (this, fe, *line_header);
7694 buildsym_compunit *b = get_builder ();
7695 subfile *sf = b->get_current_subfile ();
7696
7697 if (sf->symtab == nullptr)
7698 {
7699 /* NOTE: start_subfile will recognize when it's been
7700 passed a file it has already seen. So we can't
7701 assume there's a simple mapping from
7702 cu->line_header->file_names to subfiles, plus
7703 cu->line_header->file_names may contain dups. */
7704 const char *name = sf->name.c_str ();
7705 const char *name_for_id = sf->name_for_id.c_str ();
7706 sf->symtab = allocate_symtab (cust, name, name_for_id);
7707 }
7708
7709 fe.symtab = b->get_current_subfile ()->symtab;
7710 tug_unshare->symtabs[i] = fe.symtab;
7711 }
7712 }
7713 else
7714 {
7715 gdb_assert (m_builder == nullptr);
7716 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7717 m_builder.reset (new struct buildsym_compunit
7718 (cust->objfile (), "",
7719 cust->dirname (),
7720 cust->language (),
7721 0, cust));
7722 list_in_scope = get_builder ()->get_file_symbols ();
7723
7724 auto &file_names = line_header->file_names ();
7725 for (i = 0; i < file_names.size (); ++i)
7726 {
7727 file_entry &fe = file_names[i];
7728 fe.symtab = tug_unshare->symtabs[i];
7729 }
7730 }
7731
7732 /* The main symtab is allocated last. Type units don't have DW_AT_name
7733 so they don't have a "real" (so to speak) symtab anyway.
7734 There is later code that will assign the main symtab to all symbols
7735 that don't have one. We need to handle the case of a symbol with a
7736 missing symtab (DW_AT_decl_file) anyway. */
7737 }
7738
7739 /* Process DW_TAG_type_unit.
7740 For TUs we want to skip the first top level sibling if it's not the
7741 actual type being defined by this TU. In this case the first top
7742 level sibling is there to provide context only. */
7743
7744 static void
7745 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7746 {
7747 struct die_info *child_die;
7748
7749 prepare_one_comp_unit (cu, die, language_minimal);
7750
7751 /* Initialize (or reinitialize) the machinery for building symtabs.
7752 We do this before processing child DIEs, so that the line header table
7753 is available for DW_AT_decl_file. */
7754 cu->setup_type_unit_groups (die);
7755
7756 if (die->child != NULL)
7757 {
7758 child_die = die->child;
7759 while (child_die && child_die->tag)
7760 {
7761 process_die (child_die, cu);
7762 child_die = child_die->sibling;
7763 }
7764 }
7765 }
7766 \f
7767 /* DWO/DWP files.
7768
7769 http://gcc.gnu.org/wiki/DebugFission
7770 http://gcc.gnu.org/wiki/DebugFissionDWP
7771
7772 To simplify handling of both DWO files ("object" files with the DWARF info)
7773 and DWP files (a file with the DWOs packaged up into one file), we treat
7774 DWP files as having a collection of virtual DWO files. */
7775
7776 /* A helper function to hash two file names. This is a separate
7777 function because the hash table uses a search with a different
7778 type. The second file may be NULL. */
7779
7780 static hashval_t
7781 hash_two_files (const char *one, const char *two)
7782 {
7783 hashval_t hash = htab_hash_string (one);
7784 if (two != nullptr)
7785 hash += htab_hash_string (two);
7786 return hash;
7787 }
7788
7789 static hashval_t
7790 hash_dwo_file (const void *item)
7791 {
7792 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
7793 return hash_two_files (dwo_file->dwo_name.c_str (), dwo_file->comp_dir);
7794 }
7795
7796 /* This is used when looking up entries in the DWO hash table. */
7797
7798 struct dwo_file_search
7799 {
7800 /* Name of the DWO to look for. */
7801 const char *dwo_name;
7802 /* Compilation directory to look for. */
7803 const char *comp_dir;
7804
7805 /* Return a hash value compatible with the table. */
7806 hashval_t hash () const
7807 {
7808 return hash_two_files (dwo_name, comp_dir);
7809 }
7810 };
7811
7812 static int
7813 eq_dwo_file (const void *item_lhs, const void *item_rhs)
7814 {
7815 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
7816 const struct dwo_file_search *rhs
7817 = (const struct dwo_file_search *) item_rhs;
7818
7819 if (lhs->dwo_name != rhs->dwo_name)
7820 return 0;
7821 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
7822 return lhs->comp_dir == rhs->comp_dir;
7823 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
7824 }
7825
7826 /* Allocate a hash table for DWO files. */
7827
7828 static htab_up
7829 allocate_dwo_file_hash_table ()
7830 {
7831 return htab_up (htab_create_alloc (41,
7832 hash_dwo_file,
7833 eq_dwo_file,
7834 htab_delete_entry<dwo_file>,
7835 xcalloc, xfree));
7836 }
7837
7838 /* Lookup DWO file DWO_NAME. */
7839
7840 static void **
7841 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
7842 const char *dwo_name,
7843 const char *comp_dir)
7844 {
7845 struct dwo_file_search find_entry;
7846 void **slot;
7847
7848 if (per_objfile->per_bfd->dwo_files == NULL)
7849 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
7850
7851 find_entry.dwo_name = dwo_name;
7852 find_entry.comp_dir = comp_dir;
7853 slot = htab_find_slot_with_hash (per_objfile->per_bfd->dwo_files.get (),
7854 &find_entry, find_entry.hash (),
7855 INSERT);
7856
7857 return slot;
7858 }
7859
7860 static hashval_t
7861 hash_dwo_unit (const void *item)
7862 {
7863 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
7864
7865 /* This drops the top 32 bits of the id, but is ok for a hash. */
7866 return dwo_unit->signature;
7867 }
7868
7869 static int
7870 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7871 {
7872 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
7873 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
7874
7875 /* The signature is assumed to be unique within the DWO file.
7876 So while object file CU dwo_id's always have the value zero,
7877 that's OK, assuming each object file DWO file has only one CU,
7878 and that's the rule for now. */
7879 return lhs->signature == rhs->signature;
7880 }
7881
7882 /* Allocate a hash table for DWO CUs,TUs.
7883 There is one of these tables for each of CUs,TUs for each DWO file. */
7884
7885 static htab_up
7886 allocate_dwo_unit_table ()
7887 {
7888 /* Start out with a pretty small number.
7889 Generally DWO files contain only one CU and maybe some TUs. */
7890 return htab_up (htab_create_alloc (3,
7891 hash_dwo_unit,
7892 eq_dwo_unit,
7893 NULL, xcalloc, xfree));
7894 }
7895
7896 /* die_reader_func for create_dwo_cu. */
7897
7898 static void
7899 create_dwo_cu_reader (const struct die_reader_specs *reader,
7900 const gdb_byte *info_ptr,
7901 struct die_info *comp_unit_die,
7902 struct dwo_file *dwo_file,
7903 struct dwo_unit *dwo_unit)
7904 {
7905 struct dwarf2_cu *cu = reader->cu;
7906 sect_offset sect_off = cu->per_cu->sect_off;
7907 struct dwarf2_section_info *section = cu->per_cu->section;
7908
7909 std::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7910 if (!signature.has_value ())
7911 {
7912 complaint (_("Dwarf Error: debug entry at offset %s is missing"
7913 " its dwo_id [in module %s]"),
7914 sect_offset_str (sect_off), dwo_file->dwo_name.c_str ());
7915 return;
7916 }
7917
7918 dwo_unit->dwo_file = dwo_file;
7919 dwo_unit->signature = *signature;
7920 dwo_unit->section = section;
7921 dwo_unit->sect_off = sect_off;
7922 dwo_unit->length = cu->per_cu->length ();
7923
7924 dwarf_read_debug_printf (" offset %s, dwo_id %s",
7925 sect_offset_str (sect_off),
7926 hex_string (dwo_unit->signature));
7927 }
7928
7929 /* Create the dwo_units for the CUs in a DWO_FILE.
7930 Note: This function processes DWO files only, not DWP files. */
7931
7932 static void
7933 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
7934 dwarf2_cu *cu, struct dwo_file &dwo_file,
7935 dwarf2_section_info &section, htab_up &cus_htab)
7936 {
7937 struct objfile *objfile = per_objfile->objfile;
7938 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7939 const gdb_byte *info_ptr, *end_ptr;
7940
7941 section.read (objfile);
7942 info_ptr = section.buffer;
7943
7944 if (info_ptr == NULL)
7945 return;
7946
7947 dwarf_read_debug_printf ("Reading %s for %s:",
7948 section.get_name (),
7949 section.get_file_name ());
7950
7951 end_ptr = info_ptr + section.size;
7952 while (info_ptr < end_ptr)
7953 {
7954 struct dwarf2_per_cu_data per_cu;
7955 struct dwo_unit read_unit {};
7956 struct dwo_unit *dwo_unit;
7957 void **slot;
7958 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
7959
7960 per_cu.per_bfd = per_bfd;
7961 per_cu.is_debug_types = 0;
7962 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
7963 per_cu.section = &section;
7964
7965 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
7966 if (!reader.dummy_p)
7967 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
7968 &dwo_file, &read_unit);
7969 info_ptr += per_cu.length ();
7970
7971 // If the unit could not be parsed, skip it.
7972 if (read_unit.dwo_file == NULL)
7973 continue;
7974
7975 if (cus_htab == NULL)
7976 cus_htab = allocate_dwo_unit_table ();
7977
7978 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
7979 struct dwo_unit);
7980 *dwo_unit = read_unit;
7981 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
7982 gdb_assert (slot != NULL);
7983 if (*slot != NULL)
7984 {
7985 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
7986 sect_offset dup_sect_off = dup_cu->sect_off;
7987
7988 complaint (_("debug cu entry at offset %s is duplicate to"
7989 " the entry at offset %s, signature %s"),
7990 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7991 hex_string (dwo_unit->signature));
7992 }
7993 *slot = (void *)dwo_unit;
7994 }
7995 }
7996
7997 /* DWP file .debug_{cu,tu}_index section format:
7998 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
7999 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
8000
8001 DWP Versions 1 & 2 are older, pre-standard format versions. The first
8002 officially standard DWP format was published with DWARF v5 and is called
8003 Version 5. There are no versions 3 or 4.
8004
8005 DWP Version 1:
8006
8007 Both index sections have the same format, and serve to map a 64-bit
8008 signature to a set of section numbers. Each section begins with a header,
8009 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8010 indexes, and a pool of 32-bit section numbers. The index sections will be
8011 aligned at 8-byte boundaries in the file.
8012
8013 The index section header consists of:
8014
8015 V, 32 bit version number
8016 -, 32 bits unused
8017 N, 32 bit number of compilation units or type units in the index
8018 M, 32 bit number of slots in the hash table
8019
8020 Numbers are recorded using the byte order of the application binary.
8021
8022 The hash table begins at offset 16 in the section, and consists of an array
8023 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8024 order of the application binary). Unused slots in the hash table are 0.
8025 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8026
8027 The parallel table begins immediately after the hash table
8028 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8029 array of 32-bit indexes (using the byte order of the application binary),
8030 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8031 table contains a 32-bit index into the pool of section numbers. For unused
8032 hash table slots, the corresponding entry in the parallel table will be 0.
8033
8034 The pool of section numbers begins immediately following the hash table
8035 (at offset 16 + 12 * M from the beginning of the section). The pool of
8036 section numbers consists of an array of 32-bit words (using the byte order
8037 of the application binary). Each item in the array is indexed starting
8038 from 0. The hash table entry provides the index of the first section
8039 number in the set. Additional section numbers in the set follow, and the
8040 set is terminated by a 0 entry (section number 0 is not used in ELF).
8041
8042 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8043 section must be the first entry in the set, and the .debug_abbrev.dwo must
8044 be the second entry. Other members of the set may follow in any order.
8045
8046 ---
8047
8048 DWP Versions 2 and 5:
8049
8050 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
8051 and the entries in the index tables are now offsets into these sections.
8052 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
8053 section.
8054
8055 Index Section Contents:
8056 Header
8057 Hash Table of Signatures dwp_hash_table.hash_table
8058 Parallel Table of Indices dwp_hash_table.unit_table
8059 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
8060 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
8061
8062 The index section header consists of:
8063
8064 V, 32 bit version number
8065 L, 32 bit number of columns in the table of section offsets
8066 N, 32 bit number of compilation units or type units in the index
8067 M, 32 bit number of slots in the hash table
8068
8069 Numbers are recorded using the byte order of the application binary.
8070
8071 The hash table has the same format as version 1.
8072 The parallel table of indices has the same format as version 1,
8073 except that the entries are origin-1 indices into the table of sections
8074 offsets and the table of section sizes.
8075
8076 The table of offsets begins immediately following the parallel table
8077 (at offset 16 + 12 * M from the beginning of the section). The table is
8078 a two-dimensional array of 32-bit words (using the byte order of the
8079 application binary), with L columns and N+1 rows, in row-major order.
8080 Each row in the array is indexed starting from 0. The first row provides
8081 a key to the remaining rows: each column in this row provides an identifier
8082 for a debug section, and the offsets in the same column of subsequent rows
8083 refer to that section. The section identifiers for Version 2 are:
8084
8085 DW_SECT_INFO 1 .debug_info.dwo
8086 DW_SECT_TYPES 2 .debug_types.dwo
8087 DW_SECT_ABBREV 3 .debug_abbrev.dwo
8088 DW_SECT_LINE 4 .debug_line.dwo
8089 DW_SECT_LOC 5 .debug_loc.dwo
8090 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
8091 DW_SECT_MACINFO 7 .debug_macinfo.dwo
8092 DW_SECT_MACRO 8 .debug_macro.dwo
8093
8094 The section identifiers for Version 5 are:
8095
8096 DW_SECT_INFO_V5 1 .debug_info.dwo
8097 DW_SECT_RESERVED_V5 2 --
8098 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
8099 DW_SECT_LINE_V5 4 .debug_line.dwo
8100 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
8101 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
8102 DW_SECT_MACRO_V5 7 .debug_macro.dwo
8103 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
8104
8105 The offsets provided by the CU and TU index sections are the base offsets
8106 for the contributions made by each CU or TU to the corresponding section
8107 in the package file. Each CU and TU header contains an abbrev_offset
8108 field, used to find the abbreviations table for that CU or TU within the
8109 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
8110 be interpreted as relative to the base offset given in the index section.
8111 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
8112 should be interpreted as relative to the base offset for .debug_line.dwo,
8113 and offsets into other debug sections obtained from DWARF attributes should
8114 also be interpreted as relative to the corresponding base offset.
8115
8116 The table of sizes begins immediately following the table of offsets.
8117 Like the table of offsets, it is a two-dimensional array of 32-bit words,
8118 with L columns and N rows, in row-major order. Each row in the array is
8119 indexed starting from 1 (row 0 is shared by the two tables).
8120
8121 ---
8122
8123 Hash table lookup is handled the same in version 1 and 2:
8124
8125 We assume that N and M will not exceed 2^32 - 1.
8126 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8127
8128 Given a 64-bit compilation unit signature or a type signature S, an entry
8129 in the hash table is located as follows:
8130
8131 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8132 the low-order k bits all set to 1.
8133
8134 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8135
8136 3) If the hash table entry at index H matches the signature, use that
8137 entry. If the hash table entry at index H is unused (all zeroes),
8138 terminate the search: the signature is not present in the table.
8139
8140 4) Let H = (H + H') modulo M. Repeat at Step 3.
8141
8142 Because M > N and H' and M are relatively prime, the search is guaranteed
8143 to stop at an unused slot or find the match. */
8144
8145 /* Create a hash table to map DWO IDs to their CU/TU entry in
8146 .debug_{info,types}.dwo in DWP_FILE.
8147 Returns NULL if there isn't one.
8148 Note: This function processes DWP files only, not DWO files. */
8149
8150 static struct dwp_hash_table *
8151 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
8152 struct dwp_file *dwp_file, int is_debug_types)
8153 {
8154 struct objfile *objfile = per_objfile->objfile;
8155 bfd *dbfd = dwp_file->dbfd.get ();
8156 const gdb_byte *index_ptr, *index_end;
8157 struct dwarf2_section_info *index;
8158 uint32_t version, nr_columns, nr_units, nr_slots;
8159 struct dwp_hash_table *htab;
8160
8161 if (is_debug_types)
8162 index = &dwp_file->sections.tu_index;
8163 else
8164 index = &dwp_file->sections.cu_index;
8165
8166 if (index->empty ())
8167 return NULL;
8168 index->read (objfile);
8169
8170 index_ptr = index->buffer;
8171 index_end = index_ptr + index->size;
8172
8173 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
8174 For now it's safe to just read 4 bytes (particularly as it's difficult to
8175 tell if you're dealing with Version 5 before you've read the version). */
8176 version = read_4_bytes (dbfd, index_ptr);
8177 index_ptr += 4;
8178 if (version == 2 || version == 5)
8179 nr_columns = read_4_bytes (dbfd, index_ptr);
8180 else
8181 nr_columns = 0;
8182 index_ptr += 4;
8183 nr_units = read_4_bytes (dbfd, index_ptr);
8184 index_ptr += 4;
8185 nr_slots = read_4_bytes (dbfd, index_ptr);
8186 index_ptr += 4;
8187
8188 if (version != 1 && version != 2 && version != 5)
8189 {
8190 error (_("Dwarf Error: unsupported DWP file version (%s)"
8191 " [in module %s]"),
8192 pulongest (version), dwp_file->name);
8193 }
8194 if (nr_slots != (nr_slots & -nr_slots))
8195 {
8196 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
8197 " is not power of 2 [in module %s]"),
8198 pulongest (nr_slots), dwp_file->name);
8199 }
8200
8201 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
8202 htab->version = version;
8203 htab->nr_columns = nr_columns;
8204 htab->nr_units = nr_units;
8205 htab->nr_slots = nr_slots;
8206 htab->hash_table = index_ptr;
8207 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8208
8209 /* Exit early if the table is empty. */
8210 if (nr_slots == 0 || nr_units == 0
8211 || (version == 2 && nr_columns == 0)
8212 || (version == 5 && nr_columns == 0))
8213 {
8214 /* All must be zero. */
8215 if (nr_slots != 0 || nr_units != 0
8216 || (version == 2 && nr_columns != 0)
8217 || (version == 5 && nr_columns != 0))
8218 {
8219 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
8220 " all zero [in modules %s]"),
8221 dwp_file->name);
8222 }
8223 return htab;
8224 }
8225
8226 if (version == 1)
8227 {
8228 htab->section_pool.v1.indices =
8229 htab->unit_table + sizeof (uint32_t) * nr_slots;
8230 /* It's harder to decide whether the section is too small in v1.
8231 V1 is deprecated anyway so we punt. */
8232 }
8233 else if (version == 2)
8234 {
8235 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8236 int *ids = htab->section_pool.v2.section_ids;
8237 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
8238 /* Reverse map for error checking. */
8239 int ids_seen[DW_SECT_MAX + 1];
8240 int i;
8241
8242 if (nr_columns < 2)
8243 {
8244 error (_("Dwarf Error: bad DWP hash table, too few columns"
8245 " in section table [in module %s]"),
8246 dwp_file->name);
8247 }
8248 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
8249 {
8250 error (_("Dwarf Error: bad DWP hash table, too many columns"
8251 " in section table [in module %s]"),
8252 dwp_file->name);
8253 }
8254 memset (ids, 255, sizeof_ids);
8255 memset (ids_seen, 255, sizeof (ids_seen));
8256 for (i = 0; i < nr_columns; ++i)
8257 {
8258 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8259
8260 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
8261 {
8262 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8263 " in section table [in module %s]"),
8264 id, dwp_file->name);
8265 }
8266 if (ids_seen[id] != -1)
8267 {
8268 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8269 " id %d in section table [in module %s]"),
8270 id, dwp_file->name);
8271 }
8272 ids_seen[id] = i;
8273 ids[i] = id;
8274 }
8275 /* Must have exactly one info or types section. */
8276 if (((ids_seen[DW_SECT_INFO] != -1)
8277 + (ids_seen[DW_SECT_TYPES] != -1))
8278 != 1)
8279 {
8280 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8281 " DWO info/types section [in module %s]"),
8282 dwp_file->name);
8283 }
8284 /* Must have an abbrev section. */
8285 if (ids_seen[DW_SECT_ABBREV] == -1)
8286 {
8287 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8288 " section [in module %s]"),
8289 dwp_file->name);
8290 }
8291 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8292 htab->section_pool.v2.sizes =
8293 htab->section_pool.v2.offsets + (sizeof (uint32_t)
8294 * nr_units * nr_columns);
8295 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
8296 * nr_units * nr_columns))
8297 > index_end)
8298 {
8299 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8300 " [in module %s]"),
8301 dwp_file->name);
8302 }
8303 }
8304 else /* version == 5 */
8305 {
8306 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8307 int *ids = htab->section_pool.v5.section_ids;
8308 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
8309 /* Reverse map for error checking. */
8310 int ids_seen[DW_SECT_MAX_V5 + 1];
8311
8312 if (nr_columns < 2)
8313 {
8314 error (_("Dwarf Error: bad DWP hash table, too few columns"
8315 " in section table [in module %s]"),
8316 dwp_file->name);
8317 }
8318 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
8319 {
8320 error (_("Dwarf Error: bad DWP hash table, too many columns"
8321 " in section table [in module %s]"),
8322 dwp_file->name);
8323 }
8324 memset (ids, 255, sizeof_ids);
8325 memset (ids_seen, 255, sizeof (ids_seen));
8326 for (int i = 0; i < nr_columns; ++i)
8327 {
8328 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8329
8330 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
8331 {
8332 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8333 " in section table [in module %s]"),
8334 id, dwp_file->name);
8335 }
8336 if (ids_seen[id] != -1)
8337 {
8338 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8339 " id %d in section table [in module %s]"),
8340 id, dwp_file->name);
8341 }
8342 ids_seen[id] = i;
8343 ids[i] = id;
8344 }
8345 /* Must have seen an info section. */
8346 if (ids_seen[DW_SECT_INFO_V5] == -1)
8347 {
8348 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8349 " DWO info/types section [in module %s]"),
8350 dwp_file->name);
8351 }
8352 /* Must have an abbrev section. */
8353 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
8354 {
8355 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8356 " section [in module %s]"),
8357 dwp_file->name);
8358 }
8359 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8360 htab->section_pool.v5.sizes
8361 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
8362 * nr_units * nr_columns);
8363 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
8364 * nr_units * nr_columns))
8365 > index_end)
8366 {
8367 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8368 " [in module %s]"),
8369 dwp_file->name);
8370 }
8371 }
8372
8373 return htab;
8374 }
8375
8376 /* Update SECTIONS with the data from SECTP.
8377
8378 This function is like the other "locate" section routines, but in
8379 this context the sections to read comes from the DWP V1 hash table,
8380 not the full ELF section table.
8381
8382 The result is non-zero for success, or zero if an error was found. */
8383
8384 static int
8385 locate_v1_virtual_dwo_sections (asection *sectp,
8386 struct virtual_v1_dwo_sections *sections)
8387 {
8388 const struct dwop_section_names *names = &dwop_section_names;
8389
8390 if (names->abbrev_dwo.matches (sectp->name))
8391 {
8392 /* There can be only one. */
8393 if (sections->abbrev.s.section != NULL)
8394 return 0;
8395 sections->abbrev.s.section = sectp;
8396 sections->abbrev.size = bfd_section_size (sectp);
8397 }
8398 else if (names->info_dwo.matches (sectp->name)
8399 || names->types_dwo.matches (sectp->name))
8400 {
8401 /* There can be only one. */
8402 if (sections->info_or_types.s.section != NULL)
8403 return 0;
8404 sections->info_or_types.s.section = sectp;
8405 sections->info_or_types.size = bfd_section_size (sectp);
8406 }
8407 else if (names->line_dwo.matches (sectp->name))
8408 {
8409 /* There can be only one. */
8410 if (sections->line.s.section != NULL)
8411 return 0;
8412 sections->line.s.section = sectp;
8413 sections->line.size = bfd_section_size (sectp);
8414 }
8415 else if (names->loc_dwo.matches (sectp->name))
8416 {
8417 /* There can be only one. */
8418 if (sections->loc.s.section != NULL)
8419 return 0;
8420 sections->loc.s.section = sectp;
8421 sections->loc.size = bfd_section_size (sectp);
8422 }
8423 else if (names->macinfo_dwo.matches (sectp->name))
8424 {
8425 /* There can be only one. */
8426 if (sections->macinfo.s.section != NULL)
8427 return 0;
8428 sections->macinfo.s.section = sectp;
8429 sections->macinfo.size = bfd_section_size (sectp);
8430 }
8431 else if (names->macro_dwo.matches (sectp->name))
8432 {
8433 /* There can be only one. */
8434 if (sections->macro.s.section != NULL)
8435 return 0;
8436 sections->macro.s.section = sectp;
8437 sections->macro.size = bfd_section_size (sectp);
8438 }
8439 else if (names->str_offsets_dwo.matches (sectp->name))
8440 {
8441 /* There can be only one. */
8442 if (sections->str_offsets.s.section != NULL)
8443 return 0;
8444 sections->str_offsets.s.section = sectp;
8445 sections->str_offsets.size = bfd_section_size (sectp);
8446 }
8447 else
8448 {
8449 /* No other kind of section is valid. */
8450 return 0;
8451 }
8452
8453 return 1;
8454 }
8455
8456 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8457 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8458 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8459 This is for DWP version 1 files. */
8460
8461 static struct dwo_unit *
8462 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
8463 struct dwp_file *dwp_file,
8464 uint32_t unit_index,
8465 const char *comp_dir,
8466 ULONGEST signature, int is_debug_types)
8467 {
8468 const struct dwp_hash_table *dwp_htab =
8469 is_debug_types ? dwp_file->tus : dwp_file->cus;
8470 bfd *dbfd = dwp_file->dbfd.get ();
8471 const char *kind = is_debug_types ? "TU" : "CU";
8472 struct dwo_file *dwo_file;
8473 struct dwo_unit *dwo_unit;
8474 struct virtual_v1_dwo_sections sections;
8475 void **dwo_file_slot;
8476 int i;
8477
8478 gdb_assert (dwp_file->version == 1);
8479
8480 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
8481 kind, pulongest (unit_index), hex_string (signature),
8482 dwp_file->name);
8483
8484 /* Fetch the sections of this DWO unit.
8485 Put a limit on the number of sections we look for so that bad data
8486 doesn't cause us to loop forever. */
8487
8488 #define MAX_NR_V1_DWO_SECTIONS \
8489 (1 /* .debug_info or .debug_types */ \
8490 + 1 /* .debug_abbrev */ \
8491 + 1 /* .debug_line */ \
8492 + 1 /* .debug_loc */ \
8493 + 1 /* .debug_str_offsets */ \
8494 + 1 /* .debug_macro or .debug_macinfo */ \
8495 + 1 /* trailing zero */)
8496
8497 memset (&sections, 0, sizeof (sections));
8498
8499 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
8500 {
8501 asection *sectp;
8502 uint32_t section_nr =
8503 read_4_bytes (dbfd,
8504 dwp_htab->section_pool.v1.indices
8505 + (unit_index + i) * sizeof (uint32_t));
8506
8507 if (section_nr == 0)
8508 break;
8509 if (section_nr >= dwp_file->num_sections)
8510 {
8511 error (_("Dwarf Error: bad DWP hash table, section number too large"
8512 " [in module %s]"),
8513 dwp_file->name);
8514 }
8515
8516 sectp = dwp_file->elf_sections[section_nr];
8517 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
8518 {
8519 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8520 " [in module %s]"),
8521 dwp_file->name);
8522 }
8523 }
8524
8525 if (i < 2
8526 || sections.info_or_types.empty ()
8527 || sections.abbrev.empty ())
8528 {
8529 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8530 " [in module %s]"),
8531 dwp_file->name);
8532 }
8533 if (i == MAX_NR_V1_DWO_SECTIONS)
8534 {
8535 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8536 " [in module %s]"),
8537 dwp_file->name);
8538 }
8539
8540 /* It's easier for the rest of the code if we fake a struct dwo_file and
8541 have dwo_unit "live" in that. At least for now.
8542
8543 The DWP file can be made up of a random collection of CUs and TUs.
8544 However, for each CU + set of TUs that came from the same original DWO
8545 file, we can combine them back into a virtual DWO file to save space
8546 (fewer struct dwo_file objects to allocate). Remember that for really
8547 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8548
8549 std::string virtual_dwo_name =
8550 string_printf ("virtual-dwo/%d-%d-%d-%d",
8551 sections.abbrev.get_id (),
8552 sections.line.get_id (),
8553 sections.loc.get_id (),
8554 sections.str_offsets.get_id ());
8555 /* Can we use an existing virtual DWO file? */
8556 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8557 comp_dir);
8558 /* Create one if necessary. */
8559 if (*dwo_file_slot == NULL)
8560 {
8561 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8562 virtual_dwo_name.c_str ());
8563
8564 dwo_file = new struct dwo_file;
8565 dwo_file->dwo_name = std::move (virtual_dwo_name);
8566 dwo_file->comp_dir = comp_dir;
8567 dwo_file->sections.abbrev = sections.abbrev;
8568 dwo_file->sections.line = sections.line;
8569 dwo_file->sections.loc = sections.loc;
8570 dwo_file->sections.macinfo = sections.macinfo;
8571 dwo_file->sections.macro = sections.macro;
8572 dwo_file->sections.str_offsets = sections.str_offsets;
8573 /* The "str" section is global to the entire DWP file. */
8574 dwo_file->sections.str = dwp_file->sections.str;
8575 /* The info or types section is assigned below to dwo_unit,
8576 there's no need to record it in dwo_file.
8577 Also, we can't simply record type sections in dwo_file because
8578 we record a pointer into the vector in dwo_unit. As we collect more
8579 types we'll grow the vector and eventually have to reallocate space
8580 for it, invalidating all copies of pointers into the previous
8581 contents. */
8582 *dwo_file_slot = dwo_file;
8583 }
8584 else
8585 {
8586 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8587 virtual_dwo_name.c_str ());
8588
8589 dwo_file = (struct dwo_file *) *dwo_file_slot;
8590 }
8591
8592 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8593 dwo_unit->dwo_file = dwo_file;
8594 dwo_unit->signature = signature;
8595 dwo_unit->section =
8596 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8597 *dwo_unit->section = sections.info_or_types;
8598 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8599
8600 return dwo_unit;
8601 }
8602
8603 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
8604 simplify them. Given a pointer to the containing section SECTION, and
8605 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
8606 virtual section of just that piece. */
8607
8608 static struct dwarf2_section_info
8609 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
8610 struct dwarf2_section_info *section,
8611 bfd_size_type offset, bfd_size_type size)
8612 {
8613 struct dwarf2_section_info result;
8614 asection *sectp;
8615
8616 gdb_assert (section != NULL);
8617 gdb_assert (!section->is_virtual);
8618
8619 memset (&result, 0, sizeof (result));
8620 result.s.containing_section = section;
8621 result.is_virtual = true;
8622
8623 if (size == 0)
8624 return result;
8625
8626 sectp = section->get_bfd_section ();
8627
8628 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
8629 bounds of the real section. This is a pretty-rare event, so just
8630 flag an error (easier) instead of a warning and trying to cope. */
8631 if (sectp == NULL
8632 || offset + size > bfd_section_size (sectp))
8633 {
8634 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
8635 " in section %s [in module %s]"),
8636 sectp ? bfd_section_name (sectp) : "<unknown>",
8637 objfile_name (per_objfile->objfile));
8638 }
8639
8640 result.virtual_offset = offset;
8641 result.size = size;
8642 return result;
8643 }
8644
8645 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8646 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8647 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8648 This is for DWP version 2 files. */
8649
8650 static struct dwo_unit *
8651 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
8652 struct dwp_file *dwp_file,
8653 uint32_t unit_index,
8654 const char *comp_dir,
8655 ULONGEST signature, int is_debug_types)
8656 {
8657 const struct dwp_hash_table *dwp_htab =
8658 is_debug_types ? dwp_file->tus : dwp_file->cus;
8659 bfd *dbfd = dwp_file->dbfd.get ();
8660 const char *kind = is_debug_types ? "TU" : "CU";
8661 struct dwo_file *dwo_file;
8662 struct dwo_unit *dwo_unit;
8663 struct virtual_v2_or_v5_dwo_sections sections;
8664 void **dwo_file_slot;
8665 int i;
8666
8667 gdb_assert (dwp_file->version == 2);
8668
8669 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
8670 kind, pulongest (unit_index), hex_string (signature),
8671 dwp_file->name);
8672
8673 /* Fetch the section offsets of this DWO unit. */
8674
8675 memset (&sections, 0, sizeof (sections));
8676
8677 for (i = 0; i < dwp_htab->nr_columns; ++i)
8678 {
8679 uint32_t offset = read_4_bytes (dbfd,
8680 dwp_htab->section_pool.v2.offsets
8681 + (((unit_index - 1) * dwp_htab->nr_columns
8682 + i)
8683 * sizeof (uint32_t)));
8684 uint32_t size = read_4_bytes (dbfd,
8685 dwp_htab->section_pool.v2.sizes
8686 + (((unit_index - 1) * dwp_htab->nr_columns
8687 + i)
8688 * sizeof (uint32_t)));
8689
8690 switch (dwp_htab->section_pool.v2.section_ids[i])
8691 {
8692 case DW_SECT_INFO:
8693 case DW_SECT_TYPES:
8694 sections.info_or_types_offset = offset;
8695 sections.info_or_types_size = size;
8696 break;
8697 case DW_SECT_ABBREV:
8698 sections.abbrev_offset = offset;
8699 sections.abbrev_size = size;
8700 break;
8701 case DW_SECT_LINE:
8702 sections.line_offset = offset;
8703 sections.line_size = size;
8704 break;
8705 case DW_SECT_LOC:
8706 sections.loc_offset = offset;
8707 sections.loc_size = size;
8708 break;
8709 case DW_SECT_STR_OFFSETS:
8710 sections.str_offsets_offset = offset;
8711 sections.str_offsets_size = size;
8712 break;
8713 case DW_SECT_MACINFO:
8714 sections.macinfo_offset = offset;
8715 sections.macinfo_size = size;
8716 break;
8717 case DW_SECT_MACRO:
8718 sections.macro_offset = offset;
8719 sections.macro_size = size;
8720 break;
8721 }
8722 }
8723
8724 /* It's easier for the rest of the code if we fake a struct dwo_file and
8725 have dwo_unit "live" in that. At least for now.
8726
8727 The DWP file can be made up of a random collection of CUs and TUs.
8728 However, for each CU + set of TUs that came from the same original DWO
8729 file, we can combine them back into a virtual DWO file to save space
8730 (fewer struct dwo_file objects to allocate). Remember that for really
8731 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8732
8733 std::string virtual_dwo_name =
8734 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
8735 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8736 (long) (sections.line_size ? sections.line_offset : 0),
8737 (long) (sections.loc_size ? sections.loc_offset : 0),
8738 (long) (sections.str_offsets_size
8739 ? sections.str_offsets_offset : 0));
8740 /* Can we use an existing virtual DWO file? */
8741 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8742 comp_dir);
8743 /* Create one if necessary. */
8744 if (*dwo_file_slot == NULL)
8745 {
8746 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8747 virtual_dwo_name.c_str ());
8748
8749 dwo_file = new struct dwo_file;
8750 dwo_file->dwo_name = std::move (virtual_dwo_name);
8751 dwo_file->comp_dir = comp_dir;
8752 dwo_file->sections.abbrev =
8753 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
8754 sections.abbrev_offset,
8755 sections.abbrev_size);
8756 dwo_file->sections.line =
8757 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
8758 sections.line_offset,
8759 sections.line_size);
8760 dwo_file->sections.loc =
8761 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
8762 sections.loc_offset, sections.loc_size);
8763 dwo_file->sections.macinfo =
8764 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
8765 sections.macinfo_offset,
8766 sections.macinfo_size);
8767 dwo_file->sections.macro =
8768 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
8769 sections.macro_offset,
8770 sections.macro_size);
8771 dwo_file->sections.str_offsets =
8772 create_dwp_v2_or_v5_section (per_objfile,
8773 &dwp_file->sections.str_offsets,
8774 sections.str_offsets_offset,
8775 sections.str_offsets_size);
8776 /* The "str" section is global to the entire DWP file. */
8777 dwo_file->sections.str = dwp_file->sections.str;
8778 /* The info or types section is assigned below to dwo_unit,
8779 there's no need to record it in dwo_file.
8780 Also, we can't simply record type sections in dwo_file because
8781 we record a pointer into the vector in dwo_unit. As we collect more
8782 types we'll grow the vector and eventually have to reallocate space
8783 for it, invalidating all copies of pointers into the previous
8784 contents. */
8785 *dwo_file_slot = dwo_file;
8786 }
8787 else
8788 {
8789 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8790 virtual_dwo_name.c_str ());
8791
8792 dwo_file = (struct dwo_file *) *dwo_file_slot;
8793 }
8794
8795 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8796 dwo_unit->dwo_file = dwo_file;
8797 dwo_unit->signature = signature;
8798 dwo_unit->section =
8799 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8800 *dwo_unit->section = create_dwp_v2_or_v5_section
8801 (per_objfile,
8802 is_debug_types
8803 ? &dwp_file->sections.types
8804 : &dwp_file->sections.info,
8805 sections.info_or_types_offset,
8806 sections.info_or_types_size);
8807 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8808
8809 return dwo_unit;
8810 }
8811
8812 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8813 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8814 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8815 This is for DWP version 5 files. */
8816
8817 static struct dwo_unit *
8818 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
8819 struct dwp_file *dwp_file,
8820 uint32_t unit_index,
8821 const char *comp_dir,
8822 ULONGEST signature, int is_debug_types)
8823 {
8824 const struct dwp_hash_table *dwp_htab
8825 = is_debug_types ? dwp_file->tus : dwp_file->cus;
8826 bfd *dbfd = dwp_file->dbfd.get ();
8827 const char *kind = is_debug_types ? "TU" : "CU";
8828 struct dwo_file *dwo_file;
8829 struct dwo_unit *dwo_unit;
8830 struct virtual_v2_or_v5_dwo_sections sections {};
8831 void **dwo_file_slot;
8832
8833 gdb_assert (dwp_file->version == 5);
8834
8835 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
8836 kind, pulongest (unit_index), hex_string (signature),
8837 dwp_file->name);
8838
8839 /* Fetch the section offsets of this DWO unit. */
8840
8841 /* memset (&sections, 0, sizeof (sections)); */
8842
8843 for (int i = 0; i < dwp_htab->nr_columns; ++i)
8844 {
8845 uint32_t offset = read_4_bytes (dbfd,
8846 dwp_htab->section_pool.v5.offsets
8847 + (((unit_index - 1)
8848 * dwp_htab->nr_columns
8849 + i)
8850 * sizeof (uint32_t)));
8851 uint32_t size = read_4_bytes (dbfd,
8852 dwp_htab->section_pool.v5.sizes
8853 + (((unit_index - 1) * dwp_htab->nr_columns
8854 + i)
8855 * sizeof (uint32_t)));
8856
8857 switch (dwp_htab->section_pool.v5.section_ids[i])
8858 {
8859 case DW_SECT_ABBREV_V5:
8860 sections.abbrev_offset = offset;
8861 sections.abbrev_size = size;
8862 break;
8863 case DW_SECT_INFO_V5:
8864 sections.info_or_types_offset = offset;
8865 sections.info_or_types_size = size;
8866 break;
8867 case DW_SECT_LINE_V5:
8868 sections.line_offset = offset;
8869 sections.line_size = size;
8870 break;
8871 case DW_SECT_LOCLISTS_V5:
8872 sections.loclists_offset = offset;
8873 sections.loclists_size = size;
8874 break;
8875 case DW_SECT_MACRO_V5:
8876 sections.macro_offset = offset;
8877 sections.macro_size = size;
8878 break;
8879 case DW_SECT_RNGLISTS_V5:
8880 sections.rnglists_offset = offset;
8881 sections.rnglists_size = size;
8882 break;
8883 case DW_SECT_STR_OFFSETS_V5:
8884 sections.str_offsets_offset = offset;
8885 sections.str_offsets_size = size;
8886 break;
8887 case DW_SECT_RESERVED_V5:
8888 default:
8889 break;
8890 }
8891 }
8892
8893 /* It's easier for the rest of the code if we fake a struct dwo_file and
8894 have dwo_unit "live" in that. At least for now.
8895
8896 The DWP file can be made up of a random collection of CUs and TUs.
8897 However, for each CU + set of TUs that came from the same original DWO
8898 file, we can combine them back into a virtual DWO file to save space
8899 (fewer struct dwo_file objects to allocate). Remember that for really
8900 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8901
8902 std::string virtual_dwo_name =
8903 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
8904 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8905 (long) (sections.line_size ? sections.line_offset : 0),
8906 (long) (sections.loclists_size ? sections.loclists_offset : 0),
8907 (long) (sections.str_offsets_size
8908 ? sections.str_offsets_offset : 0),
8909 (long) (sections.macro_size ? sections.macro_offset : 0),
8910 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
8911 /* Can we use an existing virtual DWO file? */
8912 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
8913 virtual_dwo_name.c_str (),
8914 comp_dir);
8915 /* Create one if necessary. */
8916 if (*dwo_file_slot == NULL)
8917 {
8918 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8919 virtual_dwo_name.c_str ());
8920
8921 dwo_file = new struct dwo_file;
8922 dwo_file->dwo_name = std::move (virtual_dwo_name);
8923 dwo_file->comp_dir = comp_dir;
8924 dwo_file->sections.abbrev =
8925 create_dwp_v2_or_v5_section (per_objfile,
8926 &dwp_file->sections.abbrev,
8927 sections.abbrev_offset,
8928 sections.abbrev_size);
8929 dwo_file->sections.line =
8930 create_dwp_v2_or_v5_section (per_objfile,
8931 &dwp_file->sections.line,
8932 sections.line_offset, sections.line_size);
8933 dwo_file->sections.macro =
8934 create_dwp_v2_or_v5_section (per_objfile,
8935 &dwp_file->sections.macro,
8936 sections.macro_offset,
8937 sections.macro_size);
8938 dwo_file->sections.loclists =
8939 create_dwp_v2_or_v5_section (per_objfile,
8940 &dwp_file->sections.loclists,
8941 sections.loclists_offset,
8942 sections.loclists_size);
8943 dwo_file->sections.rnglists =
8944 create_dwp_v2_or_v5_section (per_objfile,
8945 &dwp_file->sections.rnglists,
8946 sections.rnglists_offset,
8947 sections.rnglists_size);
8948 dwo_file->sections.str_offsets =
8949 create_dwp_v2_or_v5_section (per_objfile,
8950 &dwp_file->sections.str_offsets,
8951 sections.str_offsets_offset,
8952 sections.str_offsets_size);
8953 /* The "str" section is global to the entire DWP file. */
8954 dwo_file->sections.str = dwp_file->sections.str;
8955 /* The info or types section is assigned below to dwo_unit,
8956 there's no need to record it in dwo_file.
8957 Also, we can't simply record type sections in dwo_file because
8958 we record a pointer into the vector in dwo_unit. As we collect more
8959 types we'll grow the vector and eventually have to reallocate space
8960 for it, invalidating all copies of pointers into the previous
8961 contents. */
8962 *dwo_file_slot = dwo_file;
8963 }
8964 else
8965 {
8966 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8967 virtual_dwo_name.c_str ());
8968
8969 dwo_file = (struct dwo_file *) *dwo_file_slot;
8970 }
8971
8972 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8973 dwo_unit->dwo_file = dwo_file;
8974 dwo_unit->signature = signature;
8975 dwo_unit->section
8976 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8977 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
8978 &dwp_file->sections.info,
8979 sections.info_or_types_offset,
8980 sections.info_or_types_size);
8981 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8982
8983 return dwo_unit;
8984 }
8985
8986 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
8987 Returns NULL if the signature isn't found. */
8988
8989 static struct dwo_unit *
8990 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
8991 struct dwp_file *dwp_file, const char *comp_dir,
8992 ULONGEST signature, int is_debug_types)
8993 {
8994 const struct dwp_hash_table *dwp_htab =
8995 is_debug_types ? dwp_file->tus : dwp_file->cus;
8996 bfd *dbfd = dwp_file->dbfd.get ();
8997 uint32_t mask = dwp_htab->nr_slots - 1;
8998 uint32_t hash = signature & mask;
8999 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9000 unsigned int i;
9001 void **slot;
9002 struct dwo_unit find_dwo_cu;
9003
9004 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9005 find_dwo_cu.signature = signature;
9006 slot = htab_find_slot (is_debug_types
9007 ? dwp_file->loaded_tus.get ()
9008 : dwp_file->loaded_cus.get (),
9009 &find_dwo_cu, INSERT);
9010
9011 if (*slot != NULL)
9012 return (struct dwo_unit *) *slot;
9013
9014 /* Use a for loop so that we don't loop forever on bad debug info. */
9015 for (i = 0; i < dwp_htab->nr_slots; ++i)
9016 {
9017 ULONGEST signature_in_table;
9018
9019 signature_in_table =
9020 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
9021 if (signature_in_table == signature)
9022 {
9023 uint32_t unit_index =
9024 read_4_bytes (dbfd,
9025 dwp_htab->unit_table + hash * sizeof (uint32_t));
9026
9027 if (dwp_file->version == 1)
9028 {
9029 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
9030 unit_index, comp_dir,
9031 signature, is_debug_types);
9032 }
9033 else if (dwp_file->version == 2)
9034 {
9035 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
9036 unit_index, comp_dir,
9037 signature, is_debug_types);
9038 }
9039 else /* version == 5 */
9040 {
9041 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
9042 unit_index, comp_dir,
9043 signature, is_debug_types);
9044 }
9045 return (struct dwo_unit *) *slot;
9046 }
9047 if (signature_in_table == 0)
9048 return NULL;
9049 hash = (hash + hash2) & mask;
9050 }
9051
9052 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9053 " [in module %s]"),
9054 dwp_file->name);
9055 }
9056
9057 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9058 Open the file specified by FILE_NAME and hand it off to BFD for
9059 preliminary analysis. Return a newly initialized bfd *, which
9060 includes a canonicalized copy of FILE_NAME.
9061 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9062 SEARCH_CWD is true if the current directory is to be searched.
9063 It will be searched before debug-file-directory.
9064 If successful, the file is added to the bfd include table of the
9065 objfile's bfd (see gdb_bfd_record_inclusion).
9066 If unable to find/open the file, return NULL.
9067 NOTE: This function is derived from symfile_bfd_open. */
9068
9069 static gdb_bfd_ref_ptr
9070 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
9071 const char *file_name, int is_dwp, int search_cwd)
9072 {
9073 int desc;
9074 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9075 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9076 to debug_file_directory. */
9077 const char *search_path;
9078 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9079
9080 gdb::unique_xmalloc_ptr<char> search_path_holder;
9081 if (search_cwd)
9082 {
9083 if (!debug_file_directory.empty ())
9084 {
9085 search_path_holder.reset (concat (".", dirname_separator_string,
9086 debug_file_directory.c_str (),
9087 (char *) NULL));
9088 search_path = search_path_holder.get ();
9089 }
9090 else
9091 search_path = ".";
9092 }
9093 else
9094 search_path = debug_file_directory.c_str ();
9095
9096 /* Add the path for the executable binary to the list of search paths. */
9097 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
9098 search_path_holder.reset (concat (objfile_dir.c_str (),
9099 dirname_separator_string,
9100 search_path, nullptr));
9101 search_path = search_path_holder.get ();
9102
9103 openp_flags flags = OPF_RETURN_REALPATH;
9104 if (is_dwp)
9105 flags |= OPF_SEARCH_IN_PATH;
9106
9107 gdb::unique_xmalloc_ptr<char> absolute_name;
9108 desc = openp (search_path, flags, file_name,
9109 O_RDONLY | O_BINARY, &absolute_name);
9110 if (desc < 0)
9111 return NULL;
9112
9113 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
9114 gnutarget, desc));
9115 if (sym_bfd == NULL)
9116 return NULL;
9117
9118 if (!bfd_check_format (sym_bfd.get (), bfd_object))
9119 return NULL;
9120
9121 /* Success. Record the bfd as having been included by the objfile's bfd.
9122 This is important because things like demangled_names_hash lives in the
9123 objfile's per_bfd space and may have references to things like symbol
9124 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
9125 gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
9126
9127 return sym_bfd;
9128 }
9129
9130 /* Try to open DWO file FILE_NAME.
9131 COMP_DIR is the DW_AT_comp_dir attribute.
9132 The result is the bfd handle of the file.
9133 If there is a problem finding or opening the file, return NULL.
9134 Upon success, the canonicalized path of the file is stored in the bfd,
9135 same as symfile_bfd_open. */
9136
9137 static gdb_bfd_ref_ptr
9138 open_dwo_file (dwarf2_per_objfile *per_objfile,
9139 const char *file_name, const char *comp_dir)
9140 {
9141 if (IS_ABSOLUTE_PATH (file_name))
9142 return try_open_dwop_file (per_objfile, file_name,
9143 0 /*is_dwp*/, 0 /*search_cwd*/);
9144
9145 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9146
9147 if (comp_dir != NULL)
9148 {
9149 std::string path_to_try = path_join (comp_dir, file_name);
9150
9151 /* NOTE: If comp_dir is a relative path, this will also try the
9152 search path, which seems useful. */
9153 gdb_bfd_ref_ptr abfd (try_open_dwop_file
9154 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
9155
9156 if (abfd != NULL)
9157 return abfd;
9158 }
9159
9160 /* That didn't work, try debug-file-directory, which, despite its name,
9161 is a list of paths. */
9162
9163 if (debug_file_directory.empty ())
9164 return NULL;
9165
9166 return try_open_dwop_file (per_objfile, file_name,
9167 0 /*is_dwp*/, 1 /*search_cwd*/);
9168 }
9169
9170 /* This function is mapped across the sections and remembers the offset and
9171 size of each of the DWO debugging sections we are interested in. */
9172
9173 static void
9174 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
9175 asection *sectp, dwo_sections *dwo_sections)
9176 {
9177 const struct dwop_section_names *names = &dwop_section_names;
9178
9179 struct dwarf2_section_info *dw_sect = nullptr;
9180
9181 if (names->abbrev_dwo.matches (sectp->name))
9182 dw_sect = &dwo_sections->abbrev;
9183 else if (names->info_dwo.matches (sectp->name))
9184 dw_sect = &dwo_sections->info;
9185 else if (names->line_dwo.matches (sectp->name))
9186 dw_sect = &dwo_sections->line;
9187 else if (names->loc_dwo.matches (sectp->name))
9188 dw_sect = &dwo_sections->loc;
9189 else if (names->loclists_dwo.matches (sectp->name))
9190 dw_sect = &dwo_sections->loclists;
9191 else if (names->macinfo_dwo.matches (sectp->name))
9192 dw_sect = &dwo_sections->macinfo;
9193 else if (names->macro_dwo.matches (sectp->name))
9194 dw_sect = &dwo_sections->macro;
9195 else if (names->rnglists_dwo.matches (sectp->name))
9196 dw_sect = &dwo_sections->rnglists;
9197 else if (names->str_dwo.matches (sectp->name))
9198 dw_sect = &dwo_sections->str;
9199 else if (names->str_offsets_dwo.matches (sectp->name))
9200 dw_sect = &dwo_sections->str_offsets;
9201 else if (names->types_dwo.matches (sectp->name))
9202 {
9203 struct dwarf2_section_info type_section;
9204
9205 memset (&type_section, 0, sizeof (type_section));
9206 dwo_sections->types.push_back (type_section);
9207 dw_sect = &dwo_sections->types.back ();
9208 }
9209
9210 if (dw_sect != nullptr)
9211 {
9212 dw_sect->s.section = sectp;
9213 dw_sect->size = bfd_section_size (sectp);
9214 dw_sect->read (objfile);
9215 }
9216 }
9217
9218 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9219 by PER_CU. This is for the non-DWP case.
9220 The result is NULL if DWO_NAME can't be found. */
9221
9222 static struct dwo_file *
9223 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
9224 const char *comp_dir)
9225 {
9226 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9227
9228 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
9229 if (dbfd == NULL)
9230 {
9231 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
9232
9233 return NULL;
9234 }
9235
9236 dwo_file_up dwo_file (new struct dwo_file);
9237 dwo_file->dwo_name = dwo_name;
9238 dwo_file->comp_dir = comp_dir;
9239 dwo_file->dbfd = std::move (dbfd);
9240
9241 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
9242 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
9243 sec, &dwo_file->sections);
9244
9245 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
9246 dwo_file->cus);
9247
9248 if (cu->per_cu->version () < 5)
9249 {
9250 create_debug_types_hash_table (per_objfile, dwo_file.get (),
9251 dwo_file->sections.types, dwo_file->tus);
9252 }
9253 else
9254 {
9255 create_debug_type_hash_table (per_objfile, dwo_file.get (),
9256 &dwo_file->sections.info, dwo_file->tus,
9257 rcuh_kind::COMPILE);
9258 }
9259
9260 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
9261
9262 bfd_cache_close (dwo_file->dbfd.get ());
9263
9264 return dwo_file.release ();
9265 }
9266
9267 /* This function is mapped across the sections and remembers the offset and
9268 size of each of the DWP debugging sections common to version 1 and 2 that
9269 we are interested in. */
9270
9271 static void
9272 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
9273 asection *sectp, dwp_file *dwp_file)
9274 {
9275 const struct dwop_section_names *names = &dwop_section_names;
9276 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9277
9278 /* Record the ELF section number for later lookup: this is what the
9279 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9280 gdb_assert (elf_section_nr < dwp_file->num_sections);
9281 dwp_file->elf_sections[elf_section_nr] = sectp;
9282
9283 /* Look for specific sections that we need. */
9284 struct dwarf2_section_info *dw_sect = nullptr;
9285 if (names->str_dwo.matches (sectp->name))
9286 dw_sect = &dwp_file->sections.str;
9287 else if (names->cu_index.matches (sectp->name))
9288 dw_sect = &dwp_file->sections.cu_index;
9289 else if (names->tu_index.matches (sectp->name))
9290 dw_sect = &dwp_file->sections.tu_index;
9291
9292 if (dw_sect != nullptr)
9293 {
9294 dw_sect->s.section = sectp;
9295 dw_sect->size = bfd_section_size (sectp);
9296 dw_sect->read (objfile);
9297 }
9298 }
9299
9300 /* This function is mapped across the sections and remembers the offset and
9301 size of each of the DWP version 2 debugging sections that we are interested
9302 in. This is split into a separate function because we don't know if we
9303 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9304
9305 static void
9306 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
9307 asection *sectp, void *dwp_file_ptr)
9308 {
9309 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9310 const struct dwop_section_names *names = &dwop_section_names;
9311 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9312
9313 /* Record the ELF section number for later lookup: this is what the
9314 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9315 gdb_assert (elf_section_nr < dwp_file->num_sections);
9316 dwp_file->elf_sections[elf_section_nr] = sectp;
9317
9318 /* Look for specific sections that we need. */
9319 struct dwarf2_section_info *dw_sect = nullptr;
9320 if (names->abbrev_dwo.matches (sectp->name))
9321 dw_sect = &dwp_file->sections.abbrev;
9322 else if (names->info_dwo.matches (sectp->name))
9323 dw_sect = &dwp_file->sections.info;
9324 else if (names->line_dwo.matches (sectp->name))
9325 dw_sect = &dwp_file->sections.line;
9326 else if (names->loc_dwo.matches (sectp->name))
9327 dw_sect = &dwp_file->sections.loc;
9328 else if (names->macinfo_dwo.matches (sectp->name))
9329 dw_sect = &dwp_file->sections.macinfo;
9330 else if (names->macro_dwo.matches (sectp->name))
9331 dw_sect = &dwp_file->sections.macro;
9332 else if (names->str_offsets_dwo.matches (sectp->name))
9333 dw_sect = &dwp_file->sections.str_offsets;
9334 else if (names->types_dwo.matches (sectp->name))
9335 dw_sect = &dwp_file->sections.types;
9336
9337 if (dw_sect != nullptr)
9338 {
9339 dw_sect->s.section = sectp;
9340 dw_sect->size = bfd_section_size (sectp);
9341 dw_sect->read (objfile);
9342 }
9343 }
9344
9345 /* This function is mapped across the sections and remembers the offset and
9346 size of each of the DWP version 5 debugging sections that we are interested
9347 in. This is split into a separate function because we don't know if we
9348 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9349
9350 static void
9351 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
9352 asection *sectp, void *dwp_file_ptr)
9353 {
9354 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9355 const struct dwop_section_names *names = &dwop_section_names;
9356 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9357
9358 /* Record the ELF section number for later lookup: this is what the
9359 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9360 gdb_assert (elf_section_nr < dwp_file->num_sections);
9361 dwp_file->elf_sections[elf_section_nr] = sectp;
9362
9363 /* Look for specific sections that we need. */
9364 struct dwarf2_section_info *dw_sect = nullptr;
9365 if (names->abbrev_dwo.matches (sectp->name))
9366 dw_sect = &dwp_file->sections.abbrev;
9367 else if (names->info_dwo.matches (sectp->name))
9368 dw_sect = &dwp_file->sections.info;
9369 else if (names->line_dwo.matches (sectp->name))
9370 dw_sect = &dwp_file->sections.line;
9371 else if (names->loclists_dwo.matches (sectp->name))
9372 dw_sect = &dwp_file->sections.loclists;
9373 else if (names->macro_dwo.matches (sectp->name))
9374 dw_sect = &dwp_file->sections.macro;
9375 else if (names->rnglists_dwo.matches (sectp->name))
9376 dw_sect = &dwp_file->sections.rnglists;
9377 else if (names->str_offsets_dwo.matches (sectp->name))
9378 dw_sect = &dwp_file->sections.str_offsets;
9379
9380 if (dw_sect != nullptr)
9381 {
9382 dw_sect->s.section = sectp;
9383 dw_sect->size = bfd_section_size (sectp);
9384 dw_sect->read (objfile);
9385 }
9386 }
9387
9388 /* Hash function for dwp_file loaded CUs/TUs. */
9389
9390 static hashval_t
9391 hash_dwp_loaded_cutus (const void *item)
9392 {
9393 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9394
9395 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9396 return dwo_unit->signature;
9397 }
9398
9399 /* Equality function for dwp_file loaded CUs/TUs. */
9400
9401 static int
9402 eq_dwp_loaded_cutus (const void *a, const void *b)
9403 {
9404 const struct dwo_unit *dua = (const struct dwo_unit *) a;
9405 const struct dwo_unit *dub = (const struct dwo_unit *) b;
9406
9407 return dua->signature == dub->signature;
9408 }
9409
9410 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9411
9412 static htab_up
9413 allocate_dwp_loaded_cutus_table ()
9414 {
9415 return htab_up (htab_create_alloc (3,
9416 hash_dwp_loaded_cutus,
9417 eq_dwp_loaded_cutus,
9418 NULL, xcalloc, xfree));
9419 }
9420
9421 /* Try to open DWP file FILE_NAME.
9422 The result is the bfd handle of the file.
9423 If there is a problem finding or opening the file, return NULL.
9424 Upon success, the canonicalized path of the file is stored in the bfd,
9425 same as symfile_bfd_open. */
9426
9427 static gdb_bfd_ref_ptr
9428 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
9429 {
9430 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
9431 1 /*is_dwp*/,
9432 1 /*search_cwd*/));
9433 if (abfd != NULL)
9434 return abfd;
9435
9436 /* Work around upstream bug 15652.
9437 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9438 [Whether that's a "bug" is debatable, but it is getting in our way.]
9439 We have no real idea where the dwp file is, because gdb's realpath-ing
9440 of the executable's path may have discarded the needed info.
9441 [IWBN if the dwp file name was recorded in the executable, akin to
9442 .gnu_debuglink, but that doesn't exist yet.]
9443 Strip the directory from FILE_NAME and search again. */
9444 if (!debug_file_directory.empty ())
9445 {
9446 /* Don't implicitly search the current directory here.
9447 If the user wants to search "." to handle this case,
9448 it must be added to debug-file-directory. */
9449 return try_open_dwop_file (per_objfile, lbasename (file_name),
9450 1 /*is_dwp*/,
9451 0 /*search_cwd*/);
9452 }
9453
9454 return NULL;
9455 }
9456
9457 /* Initialize the use of the DWP file for the current objfile.
9458 By convention the name of the DWP file is ${objfile}.dwp.
9459 The result is NULL if it can't be found. */
9460
9461 static std::unique_ptr<struct dwp_file>
9462 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
9463 {
9464 struct objfile *objfile = per_objfile->objfile;
9465
9466 /* Try to find first .dwp for the binary file before any symbolic links
9467 resolving. */
9468
9469 /* If the objfile is a debug file, find the name of the real binary
9470 file and get the name of dwp file from there. */
9471 std::string dwp_name;
9472 if (objfile->separate_debug_objfile_backlink != NULL)
9473 {
9474 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
9475 const char *backlink_basename = lbasename (backlink->original_name);
9476
9477 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
9478 }
9479 else
9480 dwp_name = objfile->original_name;
9481
9482 dwp_name += ".dwp";
9483
9484 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
9485 if (dbfd == NULL
9486 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
9487 {
9488 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9489 dwp_name = objfile_name (objfile);
9490 dwp_name += ".dwp";
9491 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
9492 }
9493
9494 if (dbfd == NULL)
9495 {
9496 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
9497
9498 return std::unique_ptr<dwp_file> ();
9499 }
9500
9501 const char *name = bfd_get_filename (dbfd.get ());
9502 std::unique_ptr<struct dwp_file> dwp_file
9503 (new struct dwp_file (name, std::move (dbfd)));
9504
9505 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
9506 dwp_file->elf_sections =
9507 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
9508 dwp_file->num_sections, asection *);
9509
9510 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9511 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9512 dwp_file.get ());
9513
9514 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
9515
9516 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
9517
9518 /* The DWP file version is stored in the hash table. Oh well. */
9519 if (dwp_file->cus && dwp_file->tus
9520 && dwp_file->cus->version != dwp_file->tus->version)
9521 {
9522 /* Technically speaking, we should try to limp along, but this is
9523 pretty bizarre. We use pulongest here because that's the established
9524 portability solution (e.g, we cannot use %u for uint32_t). */
9525 error (_("Dwarf Error: DWP file CU version %s doesn't match"
9526 " TU version %s [in DWP file %s]"),
9527 pulongest (dwp_file->cus->version),
9528 pulongest (dwp_file->tus->version), dwp_name.c_str ());
9529 }
9530
9531 if (dwp_file->cus)
9532 dwp_file->version = dwp_file->cus->version;
9533 else if (dwp_file->tus)
9534 dwp_file->version = dwp_file->tus->version;
9535 else
9536 dwp_file->version = 2;
9537
9538 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9539 {
9540 if (dwp_file->version == 2)
9541 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9542 dwp_file.get ());
9543 else
9544 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9545 dwp_file.get ());
9546 }
9547
9548 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
9549 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
9550
9551 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
9552 dwarf_read_debug_printf (" %s CUs, %s TUs",
9553 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9554 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9555
9556 bfd_cache_close (dwp_file->dbfd.get ());
9557
9558 return dwp_file;
9559 }
9560
9561 /* Wrapper around open_and_init_dwp_file, only open it once. */
9562
9563 static struct dwp_file *
9564 get_dwp_file (dwarf2_per_objfile *per_objfile)
9565 {
9566 if (!per_objfile->per_bfd->dwp_checked)
9567 {
9568 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
9569 per_objfile->per_bfd->dwp_checked = 1;
9570 }
9571 return per_objfile->per_bfd->dwp_file.get ();
9572 }
9573
9574 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9575 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9576 or in the DWP file for the objfile, referenced by THIS_UNIT.
9577 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9578 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9579
9580 This is called, for example, when wanting to read a variable with a
9581 complex location. Therefore we don't want to do file i/o for every call.
9582 Therefore we don't want to look for a DWO file on every call.
9583 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9584 then we check if we've already seen DWO_NAME, and only THEN do we check
9585 for a DWO file.
9586
9587 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9588 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9589
9590 static struct dwo_unit *
9591 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9592 ULONGEST signature, int is_debug_types)
9593 {
9594 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9595 struct objfile *objfile = per_objfile->objfile;
9596 const char *kind = is_debug_types ? "TU" : "CU";
9597 void **dwo_file_slot;
9598 struct dwo_file *dwo_file;
9599 struct dwp_file *dwp_file;
9600
9601 /* First see if there's a DWP file.
9602 If we have a DWP file but didn't find the DWO inside it, don't
9603 look for the original DWO file. It makes gdb behave differently
9604 depending on whether one is debugging in the build tree. */
9605
9606 dwp_file = get_dwp_file (per_objfile);
9607 if (dwp_file != NULL)
9608 {
9609 const struct dwp_hash_table *dwp_htab =
9610 is_debug_types ? dwp_file->tus : dwp_file->cus;
9611
9612 if (dwp_htab != NULL)
9613 {
9614 struct dwo_unit *dwo_cutu =
9615 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
9616 is_debug_types);
9617
9618 if (dwo_cutu != NULL)
9619 {
9620 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
9621 kind, hex_string (signature),
9622 host_address_to_string (dwo_cutu));
9623
9624 return dwo_cutu;
9625 }
9626 }
9627 }
9628 else
9629 {
9630 /* No DWP file, look for the DWO file. */
9631
9632 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
9633 if (*dwo_file_slot == NULL)
9634 {
9635 /* Read in the file and build a table of the CUs/TUs it contains. */
9636 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
9637 }
9638 /* NOTE: This will be NULL if unable to open the file. */
9639 dwo_file = (struct dwo_file *) *dwo_file_slot;
9640
9641 if (dwo_file != NULL)
9642 {
9643 struct dwo_unit *dwo_cutu = NULL;
9644
9645 if (is_debug_types && dwo_file->tus)
9646 {
9647 struct dwo_unit find_dwo_cutu;
9648
9649 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9650 find_dwo_cutu.signature = signature;
9651 dwo_cutu
9652 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
9653 &find_dwo_cutu);
9654 }
9655 else if (!is_debug_types && dwo_file->cus)
9656 {
9657 struct dwo_unit find_dwo_cutu;
9658
9659 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9660 find_dwo_cutu.signature = signature;
9661 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
9662 &find_dwo_cutu);
9663 }
9664
9665 if (dwo_cutu != NULL)
9666 {
9667 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
9668 kind, dwo_name, hex_string (signature),
9669 host_address_to_string (dwo_cutu));
9670
9671 return dwo_cutu;
9672 }
9673 }
9674 }
9675
9676 /* We didn't find it. This could mean a dwo_id mismatch, or
9677 someone deleted the DWO/DWP file, or the search path isn't set up
9678 correctly to find the file. */
9679
9680 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
9681 kind, dwo_name, hex_string (signature));
9682
9683 /* This is a warning and not a complaint because it can be caused by
9684 pilot error (e.g., user accidentally deleting the DWO). */
9685 {
9686 /* Print the name of the DWP file if we looked there, helps the user
9687 better diagnose the problem. */
9688 std::string dwp_text;
9689
9690 if (dwp_file != NULL)
9691 dwp_text = string_printf (" [in DWP file %s]",
9692 lbasename (dwp_file->name));
9693
9694 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
9695 " [in module %s]"),
9696 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
9697 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
9698 }
9699 return NULL;
9700 }
9701
9702 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9703 See lookup_dwo_cutu_unit for details. */
9704
9705 static struct dwo_unit *
9706 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9707 ULONGEST signature)
9708 {
9709 gdb_assert (!cu->per_cu->is_debug_types);
9710
9711 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
9712 }
9713
9714 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9715 See lookup_dwo_cutu_unit for details. */
9716
9717 static struct dwo_unit *
9718 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
9719 {
9720 gdb_assert (cu->per_cu->is_debug_types);
9721
9722 signatured_type *sig_type = (signatured_type *) cu->per_cu;
9723
9724 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
9725 }
9726
9727 /* Traversal function for queue_and_load_all_dwo_tus. */
9728
9729 static int
9730 queue_and_load_dwo_tu (void **slot, void *info)
9731 {
9732 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9733 dwarf2_cu *cu = (dwarf2_cu *) info;
9734 ULONGEST signature = dwo_unit->signature;
9735 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
9736
9737 if (sig_type != NULL)
9738 {
9739 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9740 a real dependency of PER_CU on SIG_TYPE. That is detected later
9741 while processing PER_CU. */
9742 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
9743 cu->lang ()))
9744 load_full_type_unit (sig_type, cu->per_objfile);
9745 cu->per_cu->imported_symtabs_push (sig_type);
9746 }
9747
9748 return 1;
9749 }
9750
9751 /* Queue all TUs contained in the DWO of CU to be read in.
9752 The DWO may have the only definition of the type, though it may not be
9753 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9754 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9755
9756 static void
9757 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
9758 {
9759 struct dwo_unit *dwo_unit;
9760 struct dwo_file *dwo_file;
9761
9762 gdb_assert (cu != nullptr);
9763 gdb_assert (!cu->per_cu->is_debug_types);
9764 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
9765
9766 dwo_unit = cu->dwo_unit;
9767 gdb_assert (dwo_unit != NULL);
9768
9769 dwo_file = dwo_unit->dwo_file;
9770 if (dwo_file->tus != NULL)
9771 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
9772 }
9773
9774 /* Read in various DIEs. */
9775
9776 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9777 Inherit only the children of the DW_AT_abstract_origin DIE not being
9778 already referenced by DW_AT_abstract_origin from the children of the
9779 current DIE. */
9780
9781 static void
9782 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9783 {
9784 attribute *attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9785 if (attr == nullptr)
9786 return;
9787
9788 /* Note that following die references may follow to a die in a
9789 different CU. */
9790 dwarf2_cu *origin_cu = cu;
9791
9792 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9793 die_info *origin_die = follow_die_ref (die, attr, &origin_cu);
9794
9795 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9796 symbols in. */
9797 struct pending **origin_previous_list_in_scope = origin_cu->list_in_scope;
9798 origin_cu->list_in_scope = cu->list_in_scope;
9799
9800 if (die->tag != origin_die->tag
9801 && !(die->tag == DW_TAG_inlined_subroutine
9802 && origin_die->tag == DW_TAG_subprogram))
9803 complaint (_("DIE %s and its abstract origin %s have different tags"),
9804 sect_offset_str (die->sect_off),
9805 sect_offset_str (origin_die->sect_off));
9806
9807 /* Find if the concrete and abstract trees are structurally the
9808 same. This is a shallow traversal and it is not bullet-proof;
9809 the compiler can trick the debugger into believing that the trees
9810 are isomorphic, whereas they actually are not. However, the
9811 likelihood of this happening is pretty low, and a full-fledged
9812 check would be an overkill. */
9813 bool are_isomorphic = true;
9814 die_info *concrete_child = die->child;
9815 die_info *abstract_child = origin_die->child;
9816 while (concrete_child != nullptr || abstract_child != nullptr)
9817 {
9818 if (concrete_child == nullptr
9819 || abstract_child == nullptr
9820 || concrete_child->tag != abstract_child->tag)
9821 {
9822 are_isomorphic = false;
9823 break;
9824 }
9825
9826 concrete_child = concrete_child->sibling;
9827 abstract_child = abstract_child->sibling;
9828 }
9829
9830 /* Walk the origin's children in parallel to the concrete children.
9831 This helps match an origin child in case the debug info misses
9832 DW_AT_abstract_origin attributes. Keep in mind that the abstract
9833 origin tree may not have the same tree structure as the concrete
9834 DIE, though. */
9835 die_info *corresponding_abstract_child
9836 = are_isomorphic ? origin_die->child : nullptr;
9837
9838 std::vector<sect_offset> offsets;
9839
9840 for (die_info *child_die = die->child;
9841 child_die && child_die->tag;
9842 child_die = child_die->sibling)
9843 {
9844 /* We are trying to process concrete instance entries:
9845 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
9846 it's not relevant to our analysis here. i.e. detecting DIEs that are
9847 present in the abstract instance but not referenced in the concrete
9848 one. */
9849 if (child_die->tag == DW_TAG_call_site
9850 || child_die->tag == DW_TAG_GNU_call_site)
9851 {
9852 if (are_isomorphic)
9853 corresponding_abstract_child
9854 = corresponding_abstract_child->sibling;
9855 continue;
9856 }
9857
9858 /* For each CHILD_DIE, find the corresponding child of
9859 ORIGIN_DIE. If there is more than one layer of
9860 DW_AT_abstract_origin, follow them all; there shouldn't be,
9861 but GCC versions at least through 4.4 generate this (GCC PR
9862 40573). */
9863 die_info *child_origin_die = child_die;
9864 dwarf2_cu *child_origin_cu = cu;
9865 while (true)
9866 {
9867 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9868 child_origin_cu);
9869 if (attr == nullptr)
9870 break;
9871
9872 die_info *prev_child_origin_die = child_origin_die;
9873 child_origin_die = follow_die_ref (child_origin_die, attr,
9874 &child_origin_cu);
9875
9876 if (prev_child_origin_die == child_origin_die)
9877 {
9878 /* Handle DIE with self-reference. */
9879 break;
9880 }
9881 }
9882
9883 /* If missing DW_AT_abstract_origin, try the corresponding child
9884 of the origin. Clang emits such lexical scopes. */
9885 if (child_origin_die == child_die
9886 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
9887 && are_isomorphic
9888 && child_die->tag == DW_TAG_lexical_block)
9889 child_origin_die = corresponding_abstract_child;
9890
9891 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9892 counterpart may exist. */
9893 if (child_origin_die != child_die)
9894 {
9895 if (child_die->tag != child_origin_die->tag
9896 && !(child_die->tag == DW_TAG_inlined_subroutine
9897 && child_origin_die->tag == DW_TAG_subprogram))
9898 complaint (_("Child DIE %s and its abstract origin %s have "
9899 "different tags"),
9900 sect_offset_str (child_die->sect_off),
9901 sect_offset_str (child_origin_die->sect_off));
9902 if (child_origin_die->parent != origin_die)
9903 complaint (_("Child DIE %s and its abstract origin %s have "
9904 "different parents"),
9905 sect_offset_str (child_die->sect_off),
9906 sect_offset_str (child_origin_die->sect_off));
9907 else
9908 offsets.push_back (child_origin_die->sect_off);
9909 }
9910
9911 if (are_isomorphic)
9912 corresponding_abstract_child = corresponding_abstract_child->sibling;
9913 }
9914
9915 if (!offsets.empty ())
9916 {
9917 std::sort (offsets.begin (), offsets.end ());
9918
9919 for (auto offsets_it = offsets.begin () + 1;
9920 offsets_it < offsets.end ();
9921 ++offsets_it)
9922 if (*(offsets_it - 1) == *offsets_it)
9923 complaint (_("Multiple children of DIE %s refer "
9924 "to DIE %s as their abstract origin"),
9925 sect_offset_str (die->sect_off),
9926 sect_offset_str (*offsets_it));
9927 }
9928
9929 auto offsets_it = offsets.begin ();
9930 die_info *origin_child_die = origin_die->child;
9931 while (origin_child_die != nullptr && origin_child_die->tag != 0)
9932 {
9933 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9934 while (offsets_it < offsets.end ()
9935 && *offsets_it < origin_child_die->sect_off)
9936 ++offsets_it;
9937
9938 if (offsets_it == offsets.end ()
9939 || *offsets_it > origin_child_die->sect_off)
9940 {
9941 /* Found that ORIGIN_CHILD_DIE is really not referenced.
9942 Check whether we're already processing ORIGIN_CHILD_DIE.
9943 This can happen with mutually referenced abstract_origins.
9944 PR 16581. */
9945 if (!origin_child_die->in_process)
9946 process_die (origin_child_die, origin_cu);
9947 }
9948
9949 origin_child_die = origin_child_die->sibling;
9950 }
9951
9952 origin_cu->list_in_scope = origin_previous_list_in_scope;
9953
9954 if (cu != origin_cu)
9955 compute_delayed_physnames (origin_cu);
9956 }
9957
9958 /* Return TRUE if the given DIE is the program's "main". DWARF 4 has
9959 defined a dedicated DW_AT_main_subprogram attribute to indicate the
9960 starting function of the program, however with older versions the
9961 DW_CC_program value of the DW_AT_calling_convention attribute was
9962 used instead as the only means available. We handle both variants. */
9963
9964 static bool
9965 dwarf2_func_is_main_p (struct die_info *die, struct dwarf2_cu *cu)
9966 {
9967 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
9968 return true;
9969 struct attribute *attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
9970 return (attr != nullptr
9971 && attr->constant_value (DW_CC_normal) == DW_CC_program);
9972 }
9973
9974 /* A helper to handle Ada's "Pragma Import" feature when it is applied
9975 to a function. */
9976
9977 static bool
9978 check_ada_pragma_import (struct die_info *die, struct dwarf2_cu *cu)
9979 {
9980 /* A Pragma Import will have both a name and a linkage name. */
9981 const char *name = dwarf2_name (die, cu);
9982 if (name == nullptr)
9983 return false;
9984
9985 const char *linkage_name = dw2_linkage_name (die, cu);
9986 /* Disallow the special Ada symbols. */
9987 if (!is_ada_import_or_export (cu, name, linkage_name))
9988 return false;
9989
9990 /* A Pragma Import will be a declaration, while a Pragma Export will
9991 not be. */
9992 if (!die_is_declaration (die, cu))
9993 return false;
9994
9995 new_symbol (die, read_type_die (die, cu), cu);
9996 return true;
9997 }
9998
9999 static void
10000 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
10001 {
10002 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10003 struct objfile *objfile = per_objfile->objfile;
10004 struct gdbarch *gdbarch = objfile->arch ();
10005 struct context_stack *newobj;
10006 CORE_ADDR lowpc;
10007 CORE_ADDR highpc;
10008 struct die_info *child_die;
10009 struct attribute *attr, *call_line, *call_file;
10010 const char *name;
10011 struct block *block;
10012 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10013 std::vector<struct symbol *> template_args;
10014 struct template_symbol *templ_func = NULL;
10015
10016 if (inlined_func)
10017 {
10018 /* If we do not have call site information, we can't show the
10019 caller of this inlined function. That's too confusing, so
10020 only use the scope for local variables. */
10021 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10022 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10023 if (call_line == NULL || call_file == NULL)
10024 {
10025 read_lexical_block_scope (die, cu);
10026 return;
10027 }
10028 }
10029
10030 name = dwarf2_name (die, cu);
10031 if (name == nullptr)
10032 name = dw2_linkage_name (die, cu);
10033
10034 /* Ignore functions with missing or empty names. These are actually
10035 illegal according to the DWARF standard. */
10036 if (name == NULL)
10037 {
10038 complaint (_("missing name for subprogram DIE at %s"),
10039 sect_offset_str (die->sect_off));
10040 return;
10041 }
10042
10043 if (check_ada_pragma_import (die, cu))
10044 {
10045 /* We already made the symbol for the Pragma Import, and because
10046 it is a declaration, we know it won't have any other
10047 important information, so we can simply return. */
10048 return;
10049 }
10050
10051 /* Ignore functions with missing or invalid low and high pc attributes. */
10052 unrelocated_addr unrel_low, unrel_high;
10053 if (dwarf2_get_pc_bounds (die, &unrel_low, &unrel_high, cu, nullptr, nullptr)
10054 <= PC_BOUNDS_INVALID)
10055 {
10056 if (have_complaint ())
10057 {
10058 attr = dwarf2_attr (die, DW_AT_external, cu);
10059 bool external_p = attr != nullptr && attr->as_boolean ();
10060 attr = dwarf2_attr (die, DW_AT_inline, cu);
10061 bool inlined_p
10062 = (attr != nullptr
10063 && attr->is_nonnegative ()
10064 && (attr->as_nonnegative () == DW_INL_inlined
10065 || attr->as_nonnegative () == DW_INL_declared_inlined));
10066 attr = dwarf2_attr (die, DW_AT_declaration, cu);
10067 bool decl_p = attr != nullptr && attr->as_boolean ();
10068 if (!external_p && !inlined_p && !decl_p)
10069 complaint (_("cannot get low and high bounds "
10070 "for subprogram DIE at %s"),
10071 sect_offset_str (die->sect_off));
10072 }
10073 return;
10074 }
10075
10076 lowpc = per_objfile->relocate (unrel_low);
10077 highpc = per_objfile->relocate (unrel_high);
10078
10079 /* If we have any template arguments, then we must allocate a
10080 different sort of symbol. */
10081 for (child_die = die->child; child_die; child_die = child_die->sibling)
10082 {
10083 if (child_die->tag == DW_TAG_template_type_param
10084 || child_die->tag == DW_TAG_template_value_param)
10085 {
10086 templ_func = new (&objfile->objfile_obstack) template_symbol;
10087 templ_func->subclass = SYMBOL_TEMPLATE;
10088 break;
10089 }
10090 }
10091
10092 gdb_assert (cu->get_builder () != nullptr);
10093 newobj = cu->get_builder ()->push_context (0, lowpc);
10094 newobj->name = new_symbol (die, read_type_die (die, cu), cu, templ_func);
10095
10096 if (dwarf2_func_is_main_p (die, cu))
10097 set_objfile_main_name (objfile, newobj->name->linkage_name (),
10098 cu->lang ());
10099
10100 /* If there is a location expression for DW_AT_frame_base, record
10101 it. */
10102 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10103 if (attr != nullptr)
10104 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
10105
10106 /* If there is a location for the static link, record it. */
10107 newobj->static_link = NULL;
10108 attr = dwarf2_attr (die, DW_AT_static_link, cu);
10109 if (attr != nullptr)
10110 {
10111 newobj->static_link
10112 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
10113 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
10114 cu->addr_type ());
10115 }
10116
10117 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
10118
10119 if (die->child != NULL)
10120 {
10121 child_die = die->child;
10122 while (child_die && child_die->tag)
10123 {
10124 if (child_die->tag == DW_TAG_template_type_param
10125 || child_die->tag == DW_TAG_template_value_param)
10126 {
10127 struct symbol *arg = new_symbol (child_die, NULL, cu);
10128
10129 if (arg != NULL)
10130 template_args.push_back (arg);
10131 }
10132 else
10133 process_die (child_die, cu);
10134 child_die = child_die->sibling;
10135 }
10136 }
10137
10138 inherit_abstract_dies (die, cu);
10139
10140 /* If we have a DW_AT_specification, we might need to import using
10141 directives from the context of the specification DIE. See the
10142 comment in determine_prefix. */
10143 if (cu->lang () == language_cplus
10144 && dwarf2_attr (die, DW_AT_specification, cu))
10145 {
10146 struct dwarf2_cu *spec_cu = cu;
10147 struct die_info *spec_die = die_specification (die, &spec_cu);
10148
10149 while (spec_die)
10150 {
10151 child_die = spec_die->child;
10152 while (child_die && child_die->tag)
10153 {
10154 if (child_die->tag == DW_TAG_imported_module)
10155 process_die (child_die, spec_cu);
10156 child_die = child_die->sibling;
10157 }
10158
10159 /* In some cases, GCC generates specification DIEs that
10160 themselves contain DW_AT_specification attributes. */
10161 spec_die = die_specification (spec_die, &spec_cu);
10162 }
10163 }
10164
10165 struct context_stack cstk = cu->get_builder ()->pop_context ();
10166 /* Make a block for the local symbols within. */
10167 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
10168 cstk.static_link, lowpc, highpc);
10169
10170 /* For C++, set the block's scope. */
10171 if ((cu->lang () == language_cplus
10172 || cu->lang () == language_fortran
10173 || cu->lang () == language_d
10174 || cu->lang () == language_rust)
10175 && cu->processing_has_namespace_info)
10176 block->set_scope (determine_prefix (die, cu),
10177 &objfile->objfile_obstack);
10178
10179 /* If we have address ranges, record them. */
10180 dwarf2_record_block_ranges (die, block, cu);
10181
10182 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
10183
10184 /* Attach template arguments to function. */
10185 if (!template_args.empty ())
10186 {
10187 gdb_assert (templ_func != NULL);
10188
10189 templ_func->n_template_arguments = template_args.size ();
10190 templ_func->template_arguments
10191 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
10192 templ_func->n_template_arguments);
10193 memcpy (templ_func->template_arguments,
10194 template_args.data (),
10195 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10196
10197 /* Make sure that the symtab is set on the new symbols. Even
10198 though they don't appear in this symtab directly, other parts
10199 of gdb assume that symbols do, and this is reasonably
10200 true. */
10201 for (symbol *sym : template_args)
10202 sym->set_symtab (templ_func->symtab ());
10203 }
10204
10205 /* In C++, we can have functions nested inside functions (e.g., when
10206 a function declares a class that has methods). This means that
10207 when we finish processing a function scope, we may need to go
10208 back to building a containing block's symbol lists. */
10209 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10210 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10211
10212 /* If we've finished processing a top-level function, subsequent
10213 symbols go in the file symbol list. */
10214 if (cu->get_builder ()->outermost_context_p ())
10215 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
10216 }
10217
10218 /* Process all the DIES contained within a lexical block scope. Start
10219 a new scope, process the dies, and then close the scope. */
10220
10221 static void
10222 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10223 {
10224 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10225 CORE_ADDR lowpc, highpc;
10226 struct die_info *child_die;
10227
10228 /* Ignore blocks with missing or invalid low and high pc attributes. */
10229 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10230 as multiple lexical blocks? Handling children in a sane way would
10231 be nasty. Might be easier to properly extend generic blocks to
10232 describe ranges. */
10233 unrelocated_addr unrel_low, unrel_high;
10234 switch (dwarf2_get_pc_bounds (die, &unrel_low, &unrel_high, cu,
10235 nullptr, nullptr))
10236 {
10237 case PC_BOUNDS_NOT_PRESENT:
10238 /* DW_TAG_lexical_block has no attributes, process its children as if
10239 there was no wrapping by that DW_TAG_lexical_block.
10240 GCC does no longer produces such DWARF since GCC r224161. */
10241 for (child_die = die->child;
10242 child_die != NULL && child_die->tag;
10243 child_die = child_die->sibling)
10244 {
10245 /* We might already be processing this DIE. This can happen
10246 in an unusual circumstance -- where a subroutine A
10247 appears lexically in another subroutine B, but A actually
10248 inlines B. The recursion is broken here, rather than in
10249 inherit_abstract_dies, because it seems better to simply
10250 drop concrete children here. */
10251 if (!child_die->in_process)
10252 process_die (child_die, cu);
10253 }
10254 return;
10255 case PC_BOUNDS_INVALID:
10256 return;
10257 }
10258 lowpc = per_objfile->relocate (unrel_low);
10259 highpc = per_objfile->relocate (unrel_high);
10260
10261 cu->get_builder ()->push_context (0, lowpc);
10262 if (die->child != NULL)
10263 {
10264 child_die = die->child;
10265 while (child_die && child_die->tag)
10266 {
10267 process_die (child_die, cu);
10268 child_die = child_die->sibling;
10269 }
10270 }
10271 inherit_abstract_dies (die, cu);
10272 struct context_stack cstk = cu->get_builder ()->pop_context ();
10273
10274 if (*cu->get_builder ()->get_local_symbols () != NULL
10275 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
10276 {
10277 struct block *block
10278 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
10279 cstk.start_addr, highpc);
10280
10281 /* Note that recording ranges after traversing children, as we
10282 do here, means that recording a parent's ranges entails
10283 walking across all its children's ranges as they appear in
10284 the address map, which is quadratic behavior.
10285
10286 It would be nicer to record the parent's ranges before
10287 traversing its children, simply overriding whatever you find
10288 there. But since we don't even decide whether to create a
10289 block until after we've traversed its children, that's hard
10290 to do. */
10291 dwarf2_record_block_ranges (die, block, cu);
10292 }
10293 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10294 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10295 }
10296
10297 static void dwarf2_ranges_read_low_addrs
10298 (unsigned offset,
10299 struct dwarf2_cu *cu,
10300 dwarf_tag tag,
10301 std::vector<unrelocated_addr> &result);
10302
10303 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
10304
10305 static void
10306 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10307 {
10308 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10309 struct objfile *objfile = per_objfile->objfile;
10310 struct gdbarch *gdbarch = objfile->arch ();
10311 struct attribute *attr;
10312 void **slot;
10313 int nparams;
10314 struct die_info *child_die;
10315
10316 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
10317 if (attr == NULL)
10318 {
10319 /* This was a pre-DWARF-5 GNU extension alias
10320 for DW_AT_call_return_pc. */
10321 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10322 }
10323 if (!attr)
10324 {
10325 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
10326 "DIE %s [in module %s]"),
10327 sect_offset_str (die->sect_off), objfile_name (objfile));
10328 return;
10329 }
10330 unrelocated_addr pc = per_objfile->adjust (attr->as_address ());
10331
10332 if (cu->call_site_htab == NULL)
10333 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
10334 call_site::eq, NULL,
10335 &objfile->objfile_obstack,
10336 hashtab_obstack_allocate, NULL);
10337 struct call_site call_site_local (pc, nullptr, nullptr);
10338 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10339 if (*slot != NULL)
10340 {
10341 complaint (_("Duplicate PC %s for DW_TAG_call_site "
10342 "DIE %s [in module %s]"),
10343 paddress (gdbarch, (CORE_ADDR) pc), sect_offset_str (die->sect_off),
10344 objfile_name (objfile));
10345 return;
10346 }
10347
10348 /* Count parameters at the caller. */
10349
10350 nparams = 0;
10351 for (child_die = die->child; child_die && child_die->tag;
10352 child_die = child_die->sibling)
10353 {
10354 if (child_die->tag != DW_TAG_call_site_parameter
10355 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10356 {
10357 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
10358 "DW_TAG_call_site child DIE %s [in module %s]"),
10359 child_die->tag, sect_offset_str (child_die->sect_off),
10360 objfile_name (objfile));
10361 continue;
10362 }
10363
10364 nparams++;
10365 }
10366
10367 struct call_site *call_site
10368 = new (XOBNEWVAR (&objfile->objfile_obstack,
10369 struct call_site,
10370 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
10371 struct call_site (pc, cu->per_cu, per_objfile);
10372 *slot = call_site;
10373
10374 /* We never call the destructor of call_site, so we must ensure it is
10375 trivially destructible. */
10376 static_assert(std::is_trivially_destructible<struct call_site>::value);
10377
10378 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
10379 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10380 {
10381 struct die_info *func_die;
10382
10383 /* Skip also over DW_TAG_inlined_subroutine. */
10384 for (func_die = die->parent;
10385 func_die && func_die->tag != DW_TAG_subprogram
10386 && func_die->tag != DW_TAG_subroutine_type;
10387 func_die = func_die->parent);
10388
10389 /* DW_AT_call_all_calls is a superset
10390 of DW_AT_call_all_tail_calls. */
10391 if (func_die
10392 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
10393 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10394 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
10395 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10396 {
10397 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10398 not complete. But keep CALL_SITE for look ups via call_site_htab,
10399 both the initial caller containing the real return address PC and
10400 the final callee containing the current PC of a chain of tail
10401 calls do not need to have the tail call list complete. But any
10402 function candidate for a virtual tail call frame searched via
10403 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10404 determined unambiguously. */
10405 }
10406 else
10407 {
10408 struct type *func_type = NULL;
10409
10410 if (func_die)
10411 func_type = get_die_type (func_die, cu);
10412 if (func_type != NULL)
10413 {
10414 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
10415
10416 /* Enlist this call site to the function. */
10417 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10418 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10419 }
10420 else
10421 complaint (_("Cannot find function owning DW_TAG_call_site "
10422 "DIE %s [in module %s]"),
10423 sect_offset_str (die->sect_off), objfile_name (objfile));
10424 }
10425 }
10426
10427 attr = dwarf2_attr (die, DW_AT_call_target, cu);
10428 if (attr == NULL)
10429 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10430 if (attr == NULL)
10431 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
10432 if (attr == NULL)
10433 {
10434 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
10435 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10436 }
10437
10438 call_site->target.set_loc_dwarf_block (nullptr);
10439 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
10440 /* Keep NULL DWARF_BLOCK. */;
10441 else if (attr->form_is_block ())
10442 {
10443 struct dwarf2_locexpr_baton *dlbaton;
10444 struct dwarf_block *block = attr->as_block ();
10445
10446 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
10447 dlbaton->data = block->data;
10448 dlbaton->size = block->size;
10449 dlbaton->per_objfile = per_objfile;
10450 dlbaton->per_cu = cu->per_cu;
10451
10452 call_site->target.set_loc_dwarf_block (dlbaton);
10453 }
10454 else if (attr->form_is_ref ())
10455 {
10456 struct dwarf2_cu *target_cu = cu;
10457 struct die_info *target_die;
10458
10459 target_die = follow_die_ref (die, attr, &target_cu);
10460 gdb_assert (target_cu->per_objfile->objfile == objfile);
10461
10462 struct attribute *ranges_attr
10463 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
10464
10465 if (die_is_declaration (target_die, target_cu))
10466 {
10467 const char *target_physname;
10468
10469 /* Prefer the mangled name; otherwise compute the demangled one. */
10470 target_physname = dw2_linkage_name (target_die, target_cu);
10471 if (target_physname == NULL)
10472 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10473 if (target_physname == NULL)
10474 complaint (_("DW_AT_call_target target DIE has invalid "
10475 "physname, for referencing DIE %s [in module %s]"),
10476 sect_offset_str (die->sect_off), objfile_name (objfile));
10477 else
10478 call_site->target.set_loc_physname (target_physname);
10479 }
10480 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
10481 {
10482 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
10483 + target_cu->gnu_ranges_base);
10484 std::vector<unrelocated_addr> addresses;
10485 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
10486 target_die->tag, addresses);
10487 unrelocated_addr *saved = XOBNEWVEC (&objfile->objfile_obstack,
10488 unrelocated_addr,
10489 addresses.size ());
10490 std::copy (addresses.begin (), addresses.end (), saved);
10491 call_site->target.set_loc_array (addresses.size (), saved);
10492 }
10493 else
10494 {
10495 unrelocated_addr lowpc;
10496
10497 /* DW_AT_entry_pc should be preferred. */
10498 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
10499 nullptr, nullptr)
10500 <= PC_BOUNDS_INVALID)
10501 complaint (_("DW_AT_call_target target DIE has invalid "
10502 "low pc, for referencing DIE %s [in module %s]"),
10503 sect_offset_str (die->sect_off), objfile_name (objfile));
10504 else
10505 {
10506 lowpc = per_objfile->adjust (lowpc);
10507 call_site->target.set_loc_physaddr (lowpc);
10508 }
10509 }
10510 }
10511 else
10512 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
10513 "block nor reference, for DIE %s [in module %s]"),
10514 sect_offset_str (die->sect_off), objfile_name (objfile));
10515
10516 for (child_die = die->child;
10517 child_die && child_die->tag;
10518 child_die = child_die->sibling)
10519 {
10520 struct call_site_parameter *parameter;
10521 struct attribute *loc, *origin;
10522
10523 if (child_die->tag != DW_TAG_call_site_parameter
10524 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10525 {
10526 /* Already printed the complaint above. */
10527 continue;
10528 }
10529
10530 gdb_assert (call_site->parameter_count < nparams);
10531 parameter = &call_site->parameter[call_site->parameter_count];
10532
10533 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10534 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10535 register is contained in DW_AT_call_value. */
10536
10537 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10538 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
10539 if (origin == NULL)
10540 {
10541 /* This was a pre-DWARF-5 GNU extension alias
10542 for DW_AT_call_parameter. */
10543 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10544 }
10545 if (loc == NULL && origin != NULL && origin->form_is_ref ())
10546 {
10547 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10548
10549 sect_offset sect_off = origin->get_ref_die_offset ();
10550 if (!cu->header.offset_in_cu_p (sect_off))
10551 {
10552 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10553 binding can be done only inside one CU. Such referenced DIE
10554 therefore cannot be even moved to DW_TAG_partial_unit. */
10555 complaint (_("DW_AT_call_parameter offset is not in CU for "
10556 "DW_TAG_call_site child DIE %s [in module %s]"),
10557 sect_offset_str (child_die->sect_off),
10558 objfile_name (objfile));
10559 continue;
10560 }
10561 parameter->u.param_cu_off
10562 = (cu_offset) (sect_off - cu->header.sect_off);
10563 }
10564 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
10565 {
10566 complaint (_("No DW_FORM_block* DW_AT_location for "
10567 "DW_TAG_call_site child DIE %s [in module %s]"),
10568 sect_offset_str (child_die->sect_off), objfile_name (objfile));
10569 continue;
10570 }
10571 else
10572 {
10573 struct dwarf_block *block = loc->as_block ();
10574
10575 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10576 (block->data, &block->data[block->size]);
10577 if (parameter->u.dwarf_reg != -1)
10578 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10579 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
10580 &block->data[block->size],
10581 &parameter->u.fb_offset))
10582 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10583 else
10584 {
10585 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
10586 "for DW_FORM_block* DW_AT_location is supported for "
10587 "DW_TAG_call_site child DIE %s "
10588 "[in module %s]"),
10589 sect_offset_str (child_die->sect_off),
10590 objfile_name (objfile));
10591 continue;
10592 }
10593 }
10594
10595 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
10596 if (attr == NULL)
10597 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10598 if (attr == NULL || !attr->form_is_block ())
10599 {
10600 complaint (_("No DW_FORM_block* DW_AT_call_value for "
10601 "DW_TAG_call_site child DIE %s [in module %s]"),
10602 sect_offset_str (child_die->sect_off),
10603 objfile_name (objfile));
10604 continue;
10605 }
10606
10607 struct dwarf_block *block = attr->as_block ();
10608 parameter->value = block->data;
10609 parameter->value_size = block->size;
10610
10611 /* Parameters are not pre-cleared by memset above. */
10612 parameter->data_value = NULL;
10613 parameter->data_value_size = 0;
10614 call_site->parameter_count++;
10615
10616 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
10617 if (attr == NULL)
10618 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10619 if (attr != nullptr)
10620 {
10621 if (!attr->form_is_block ())
10622 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
10623 "DW_TAG_call_site child DIE %s [in module %s]"),
10624 sect_offset_str (child_die->sect_off),
10625 objfile_name (objfile));
10626 else
10627 {
10628 block = attr->as_block ();
10629 parameter->data_value = block->data;
10630 parameter->data_value_size = block->size;
10631 }
10632 }
10633 }
10634 }
10635
10636 /* Helper function for read_variable. If DIE represents a virtual
10637 table, then return the type of the concrete object that is
10638 associated with the virtual table. Otherwise, return NULL. */
10639
10640 static struct type *
10641 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10642 {
10643 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
10644 if (attr == NULL)
10645 return NULL;
10646
10647 /* Find the type DIE. */
10648 struct die_info *type_die = NULL;
10649 struct dwarf2_cu *type_cu = cu;
10650
10651 if (attr->form_is_ref ())
10652 type_die = follow_die_ref (die, attr, &type_cu);
10653 if (type_die == NULL)
10654 return NULL;
10655
10656 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
10657 return NULL;
10658 return die_containing_type (type_die, type_cu);
10659 }
10660
10661 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
10662
10663 static void
10664 read_variable (struct die_info *die, struct dwarf2_cu *cu)
10665 {
10666 struct rust_vtable_symbol *storage = NULL;
10667
10668 if (cu->lang () == language_rust)
10669 {
10670 struct type *containing_type = rust_containing_type (die, cu);
10671
10672 if (containing_type != NULL)
10673 {
10674 struct objfile *objfile = cu->per_objfile->objfile;
10675
10676 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
10677 storage->concrete_type = containing_type;
10678 storage->subclass = SYMBOL_RUST_VTABLE;
10679 }
10680 }
10681
10682 struct symbol *res = new_symbol (die, NULL, cu, storage);
10683 struct attribute *abstract_origin
10684 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10685 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
10686 if (res == NULL && loc && abstract_origin)
10687 {
10688 /* We have a variable without a name, but with a location and an abstract
10689 origin. This may be a concrete instance of an abstract variable
10690 referenced from an DW_OP_GNU_variable_value, so save it to find it back
10691 later. */
10692 struct dwarf2_cu *origin_cu = cu;
10693 struct die_info *origin_die
10694 = follow_die_ref (die, abstract_origin, &origin_cu);
10695 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10696 per_objfile->per_bfd->abstract_to_concrete
10697 [origin_die->sect_off].push_back (die->sect_off);
10698 }
10699 }
10700
10701 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
10702 reading .debug_rnglists.
10703 Callback's type should be:
10704 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10705 Return true if the attributes are present and valid, otherwise,
10706 return false. */
10707
10708 template <typename Callback>
10709 static bool
10710 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
10711 dwarf_tag tag, Callback &&callback)
10712 {
10713 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10714 struct objfile *objfile = per_objfile->objfile;
10715 bfd *obfd = objfile->obfd.get ();
10716 /* Base address selection entry. */
10717 std::optional<unrelocated_addr> base;
10718 const gdb_byte *buffer;
10719 bool overflow = false;
10720 ULONGEST addr_index;
10721 struct dwarf2_section_info *rnglists_section;
10722
10723 base = cu->base_address;
10724 rnglists_section = cu_debug_rnglists_section (cu, tag);
10725 rnglists_section->read (objfile);
10726
10727 if (offset >= rnglists_section->size)
10728 {
10729 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10730 offset);
10731 return false;
10732 }
10733 buffer = rnglists_section->buffer + offset;
10734
10735 while (1)
10736 {
10737 /* Initialize it due to a false compiler warning. */
10738 unrelocated_addr range_beginning = {}, range_end = {};
10739 const gdb_byte *buf_end = (rnglists_section->buffer
10740 + rnglists_section->size);
10741 unsigned int bytes_read;
10742
10743 if (buffer == buf_end)
10744 {
10745 overflow = true;
10746 break;
10747 }
10748 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
10749 switch (rlet)
10750 {
10751 case DW_RLE_end_of_list:
10752 break;
10753 case DW_RLE_base_address:
10754 if (buffer + cu->header.addr_size > buf_end)
10755 {
10756 overflow = true;
10757 break;
10758 }
10759 base = cu->header.read_address (obfd, buffer, &bytes_read);
10760 buffer += bytes_read;
10761 break;
10762 case DW_RLE_base_addressx:
10763 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10764 buffer += bytes_read;
10765 base = read_addr_index (cu, addr_index);
10766 break;
10767 case DW_RLE_start_length:
10768 if (buffer + cu->header.addr_size > buf_end)
10769 {
10770 overflow = true;
10771 break;
10772 }
10773 range_beginning = cu->header.read_address (obfd, buffer,
10774 &bytes_read);
10775 buffer += bytes_read;
10776 range_end
10777 = (unrelocated_addr) ((CORE_ADDR) range_beginning
10778 + read_unsigned_leb128 (obfd, buffer,
10779 &bytes_read));
10780 buffer += bytes_read;
10781 if (buffer > buf_end)
10782 {
10783 overflow = true;
10784 break;
10785 }
10786 break;
10787 case DW_RLE_startx_length:
10788 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10789 buffer += bytes_read;
10790 range_beginning = read_addr_index (cu, addr_index);
10791 if (buffer > buf_end)
10792 {
10793 overflow = true;
10794 break;
10795 }
10796 range_end
10797 = (unrelocated_addr) ((CORE_ADDR) range_beginning
10798 + read_unsigned_leb128 (obfd, buffer,
10799 &bytes_read));
10800 buffer += bytes_read;
10801 break;
10802 case DW_RLE_offset_pair:
10803 range_beginning = (unrelocated_addr) read_unsigned_leb128 (obfd, buffer,
10804 &bytes_read);
10805 buffer += bytes_read;
10806 if (buffer > buf_end)
10807 {
10808 overflow = true;
10809 break;
10810 }
10811 range_end = (unrelocated_addr) read_unsigned_leb128 (obfd, buffer,
10812 &bytes_read);
10813 buffer += bytes_read;
10814 if (buffer > buf_end)
10815 {
10816 overflow = true;
10817 break;
10818 }
10819 break;
10820 case DW_RLE_start_end:
10821 if (buffer + 2 * cu->header.addr_size > buf_end)
10822 {
10823 overflow = true;
10824 break;
10825 }
10826 range_beginning = cu->header.read_address (obfd, buffer,
10827 &bytes_read);
10828 buffer += bytes_read;
10829 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
10830 buffer += bytes_read;
10831 break;
10832 case DW_RLE_startx_endx:
10833 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10834 buffer += bytes_read;
10835 range_beginning = read_addr_index (cu, addr_index);
10836 if (buffer > buf_end)
10837 {
10838 overflow = true;
10839 break;
10840 }
10841 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10842 buffer += bytes_read;
10843 range_end = read_addr_index (cu, addr_index);
10844 break;
10845 default:
10846 complaint (_("Invalid .debug_rnglists data (no base address)"));
10847 return false;
10848 }
10849 if (rlet == DW_RLE_end_of_list || overflow)
10850 break;
10851 if (rlet == DW_RLE_base_address)
10852 continue;
10853
10854 if (range_beginning > range_end)
10855 {
10856 /* Inverted range entries are invalid. */
10857 complaint (_("Invalid .debug_rnglists data (inverted range)"));
10858 return false;
10859 }
10860
10861 /* Empty range entries have no effect. */
10862 if (range_beginning == range_end)
10863 continue;
10864
10865 /* Only DW_RLE_offset_pair needs the base address added. */
10866 if (rlet == DW_RLE_offset_pair)
10867 {
10868 if (!base.has_value ())
10869 {
10870 /* We have no valid base address for the DW_RLE_offset_pair. */
10871 complaint (_("Invalid .debug_rnglists data (no base address for "
10872 "DW_RLE_offset_pair)"));
10873 return false;
10874 }
10875
10876 range_beginning = (unrelocated_addr) ((CORE_ADDR) range_beginning
10877 + (CORE_ADDR) *base);
10878 range_end = (unrelocated_addr) ((CORE_ADDR) range_end
10879 + (CORE_ADDR) *base);
10880 }
10881
10882 /* A not-uncommon case of bad debug info.
10883 Don't pollute the addrmap with bad data. */
10884 if (range_beginning == (unrelocated_addr) 0
10885 && !per_objfile->per_bfd->has_section_at_zero)
10886 {
10887 complaint (_(".debug_rnglists entry has start address of zero"
10888 " [in module %s]"), objfile_name (objfile));
10889 continue;
10890 }
10891
10892 callback (range_beginning, range_end);
10893 }
10894
10895 if (overflow)
10896 {
10897 complaint (_("Offset %d is not terminated "
10898 "for DW_AT_ranges attribute"),
10899 offset);
10900 return false;
10901 }
10902
10903 return true;
10904 }
10905
10906 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
10907 Callback's type should be:
10908 void (unrelocated_addr range_beginning, unrelocated_addr range_end)
10909 Return 1 if the attributes are present and valid, otherwise, return 0. */
10910
10911 template <typename Callback>
10912 static int
10913 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
10914 Callback &&callback)
10915 {
10916 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10917 struct objfile *objfile = per_objfile->objfile;
10918 struct comp_unit_head *cu_header = &cu->header;
10919 bfd *obfd = objfile->obfd.get ();
10920 unsigned int addr_size = cu_header->addr_size;
10921 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10922 /* Base address selection entry. */
10923 std::optional<unrelocated_addr> base;
10924 unsigned int dummy;
10925 const gdb_byte *buffer;
10926
10927 if (cu_header->version >= 5)
10928 return dwarf2_rnglists_process (offset, cu, tag, callback);
10929
10930 base = cu->base_address;
10931
10932 per_objfile->per_bfd->ranges.read (objfile);
10933 if (offset >= per_objfile->per_bfd->ranges.size)
10934 {
10935 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10936 offset);
10937 return 0;
10938 }
10939 buffer = per_objfile->per_bfd->ranges.buffer + offset;
10940
10941 while (1)
10942 {
10943 unrelocated_addr range_beginning, range_end;
10944
10945 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
10946 buffer += addr_size;
10947 range_end = cu->header.read_address (obfd, buffer, &dummy);
10948 buffer += addr_size;
10949 offset += 2 * addr_size;
10950
10951 /* An end of list marker is a pair of zero addresses. */
10952 if (range_beginning == (unrelocated_addr) 0
10953 && range_end == (unrelocated_addr) 0)
10954 /* Found the end of list entry. */
10955 break;
10956
10957 /* Each base address selection entry is a pair of 2 values.
10958 The first is the largest possible address, the second is
10959 the base address. Check for a base address here. */
10960 if (((CORE_ADDR) range_beginning & mask) == mask)
10961 {
10962 /* If we found the largest possible address, then we already
10963 have the base address in range_end. */
10964 base = range_end;
10965 continue;
10966 }
10967
10968 if (!base.has_value ())
10969 {
10970 /* We have no valid base address for the ranges
10971 data. */
10972 complaint (_("Invalid .debug_ranges data (no base address)"));
10973 return 0;
10974 }
10975
10976 if (range_beginning > range_end)
10977 {
10978 /* Inverted range entries are invalid. */
10979 complaint (_("Invalid .debug_ranges data (inverted range)"));
10980 return 0;
10981 }
10982
10983 /* Empty range entries have no effect. */
10984 if (range_beginning == range_end)
10985 continue;
10986
10987 range_beginning = (unrelocated_addr) ((CORE_ADDR) range_beginning
10988 + (CORE_ADDR) *base);
10989 range_end = (unrelocated_addr) ((CORE_ADDR) range_end
10990 + (CORE_ADDR) *base);
10991
10992 /* A not-uncommon case of bad debug info.
10993 Don't pollute the addrmap with bad data. */
10994 if (range_beginning == (unrelocated_addr) 0
10995 && !per_objfile->per_bfd->has_section_at_zero)
10996 {
10997 complaint (_(".debug_ranges entry has start address of zero"
10998 " [in module %s]"), objfile_name (objfile));
10999 continue;
11000 }
11001
11002 callback (range_beginning, range_end);
11003 }
11004
11005 return 1;
11006 }
11007
11008 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
11009 Return 1 if the attributes are present and valid, otherwise, return 0.
11010 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
11011 ranges in MAP are set, using DATUM as the value. */
11012
11013 static int
11014 dwarf2_ranges_read (unsigned offset, unrelocated_addr *low_return,
11015 unrelocated_addr *high_return, struct dwarf2_cu *cu,
11016 addrmap *map, void *datum, dwarf_tag tag)
11017 {
11018 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11019 int low_set = 0;
11020 unrelocated_addr low = {};
11021 unrelocated_addr high = {};
11022 int retval;
11023
11024 retval = dwarf2_ranges_process (offset, cu, tag,
11025 [&] (unrelocated_addr range_beginning, unrelocated_addr range_end)
11026 {
11027 if (map != nullptr)
11028 {
11029 unrelocated_addr lowpc;
11030 unrelocated_addr highpc;
11031
11032 lowpc = per_objfile->adjust (range_beginning);
11033 highpc = per_objfile->adjust (range_end);
11034 /* addrmap only accepts CORE_ADDR, so we must cast here. */
11035 map->set_empty ((CORE_ADDR) lowpc, (CORE_ADDR) highpc - 1, datum);
11036 }
11037
11038 /* FIXME: This is recording everything as a low-high
11039 segment of consecutive addresses. We should have a
11040 data structure for discontiguous block ranges
11041 instead. */
11042 if (! low_set)
11043 {
11044 low = range_beginning;
11045 high = range_end;
11046 low_set = 1;
11047 }
11048 else
11049 {
11050 if (range_beginning < low)
11051 low = range_beginning;
11052 if (range_end > high)
11053 high = range_end;
11054 }
11055 });
11056 if (!retval)
11057 return 0;
11058
11059 if (! low_set)
11060 /* If the first entry is an end-of-list marker, the range
11061 describes an empty scope, i.e. no instructions. */
11062 return 0;
11063
11064 if (low_return)
11065 *low_return = low;
11066 if (high_return)
11067 *high_return = high;
11068 return 1;
11069 }
11070
11071 /* Process ranges and fill in a vector of the low PC values only. */
11072
11073 static void
11074 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
11075 dwarf_tag tag,
11076 std::vector<unrelocated_addr> &result)
11077 {
11078 dwarf2_ranges_process (offset, cu, tag,
11079 [&] (unrelocated_addr start, unrelocated_addr end)
11080 {
11081 result.push_back (start);
11082 });
11083 }
11084
11085 /* Determine the low and high pc of a DW_TAG_entry_point. */
11086
11087 static pc_bounds_kind
11088 dwarf2_get_pc_bounds_entry_point (die_info *die, unrelocated_addr *low,
11089 unrelocated_addr *high, dwarf2_cu *cu)
11090 {
11091 gdb_assert (low != nullptr);
11092 gdb_assert (high != nullptr);
11093
11094 if (die->parent->tag != DW_TAG_subprogram)
11095 {
11096 complaint (_("DW_TAG_entry_point not embedded in DW_TAG_subprogram"));
11097 return PC_BOUNDS_INVALID;
11098 }
11099
11100 /* A DW_TAG_entry_point is embedded in an subprogram. Therefore, we can use
11101 the highpc from its enveloping subprogram and get the lowpc from
11102 DWARF. */
11103 const enum pc_bounds_kind bounds_kind = dwarf2_get_pc_bounds (die->parent,
11104 low, high,
11105 cu, nullptr,
11106 nullptr);
11107 if (bounds_kind == PC_BOUNDS_INVALID || bounds_kind == PC_BOUNDS_NOT_PRESENT)
11108 return bounds_kind;
11109
11110 attribute *attr_low = dwarf2_attr (die, DW_AT_low_pc, cu);
11111 if (!attr_low)
11112 {
11113 complaint (_("DW_TAG_entry_point is missing DW_AT_low_pc"));
11114 return PC_BOUNDS_INVALID;
11115 }
11116 *low = attr_low->as_address ();
11117 return bounds_kind;
11118 }
11119
11120 /* Determine the low and high pc using the DW_AT_low_pc and DW_AT_high_pc or
11121 DW_AT_ranges attributes of a DIE. */
11122
11123 static pc_bounds_kind
11124 dwarf_get_pc_bounds_ranges_or_highlow_pc (die_info *die, unrelocated_addr *low,
11125 unrelocated_addr *high, dwarf2_cu *cu,
11126 addrmap *map, void *datum)
11127 {
11128 gdb_assert (low != nullptr);
11129 gdb_assert (high != nullptr);
11130
11131 struct attribute *attr;
11132 struct attribute *attr_high;
11133 enum pc_bounds_kind ret;
11134
11135 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11136 if (attr_high)
11137 {
11138 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11139 if (attr != nullptr)
11140 {
11141 *low = attr->as_address ();
11142 *high = attr_high->as_address ();
11143 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11144 *high = (unrelocated_addr) ((ULONGEST) *high + (ULONGEST) *low);
11145
11146 /* Found consecutive range of addresses. */
11147 ret = PC_BOUNDS_HIGH_LOW;
11148 }
11149 else
11150 {
11151 /* Found high w/o low attribute. */
11152 ret = PC_BOUNDS_INVALID;
11153 }
11154 }
11155 else
11156 {
11157 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11158 if (attr != nullptr && attr->form_is_unsigned ())
11159 {
11160 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11161 on DWARF version). */
11162 ULONGEST ranges_offset = attr->as_unsigned ();
11163
11164 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11165 this value. */
11166 if (die->tag != DW_TAG_compile_unit)
11167 ranges_offset += cu->gnu_ranges_base;
11168
11169 /* Value of the DW_AT_ranges attribute is the offset in the
11170 .debug_ranges section. */
11171 if (!dwarf2_ranges_read (ranges_offset, low, high, cu,
11172 map, datum, die->tag))
11173 return PC_BOUNDS_INVALID;
11174 /* Found discontinuous range of addresses. */
11175 ret = PC_BOUNDS_RANGES;
11176 }
11177 else
11178 {
11179 /* Could not find high_pc or ranges attributed and thus no bounds
11180 pair. */
11181 ret = PC_BOUNDS_NOT_PRESENT;
11182 }
11183 }
11184
11185 return ret;
11186 }
11187
11188 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
11189 definition for the return value. *LOWPC and *HIGHPC are set iff
11190 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
11191
11192 static enum pc_bounds_kind
11193 dwarf2_get_pc_bounds (struct die_info *die, unrelocated_addr *lowpc,
11194 unrelocated_addr *highpc, struct dwarf2_cu *cu,
11195 addrmap *map, void *datum)
11196 {
11197 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11198
11199 unrelocated_addr low = {};
11200 unrelocated_addr high = {};
11201 enum pc_bounds_kind ret;
11202
11203 if (die->tag == DW_TAG_entry_point)
11204 ret = dwarf2_get_pc_bounds_entry_point (die, &low, &high, cu);
11205 else
11206 ret = dwarf_get_pc_bounds_ranges_or_highlow_pc (die, &low, &high, cu, map,
11207 datum);
11208
11209 if (ret == PC_BOUNDS_NOT_PRESENT || ret == PC_BOUNDS_INVALID)
11210 return ret;
11211
11212 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
11213 if (high <= low)
11214 return PC_BOUNDS_INVALID;
11215
11216 /* When using the GNU linker, .gnu.linkonce. sections are used to
11217 eliminate duplicate copies of functions and vtables and such.
11218 The linker will arbitrarily choose one and discard the others.
11219 The AT_*_pc values for such functions refer to local labels in
11220 these sections. If the section from that file was discarded, the
11221 labels are not in the output, so the relocs get a value of 0.
11222 If this is a discarded function, mark the pc bounds as invalid,
11223 so that GDB will ignore it. */
11224 if (low == (unrelocated_addr) 0
11225 && !per_objfile->per_bfd->has_section_at_zero)
11226 return PC_BOUNDS_INVALID;
11227
11228 gdb_assert (lowpc != nullptr);
11229 *lowpc = low;
11230 if (highpc != nullptr)
11231 *highpc = high;
11232 return ret;
11233 }
11234
11235 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
11236 its low and high PC addresses. Do nothing if these addresses could not
11237 be determined. Otherwise, set LOWPC to the low address if it is smaller,
11238 and HIGHPC to the high address if greater than HIGHPC. */
11239
11240 static void
11241 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
11242 unrelocated_addr *lowpc,
11243 unrelocated_addr *highpc,
11244 struct dwarf2_cu *cu)
11245 {
11246 unrelocated_addr low, high;
11247 struct die_info *child = die->child;
11248
11249 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
11250 >= PC_BOUNDS_RANGES)
11251 {
11252 *lowpc = std::min (*lowpc, low);
11253 *highpc = std::max (*highpc, high);
11254 }
11255
11256 /* If the language does not allow nested subprograms (either inside
11257 subprograms or lexical blocks), we're done. */
11258 if (cu->lang () != language_ada)
11259 return;
11260
11261 /* Check all the children of the given DIE. If it contains nested
11262 subprograms, then check their pc bounds. Likewise, we need to
11263 check lexical blocks as well, as they may also contain subprogram
11264 definitions. */
11265 while (child && child->tag)
11266 {
11267 if (child->tag == DW_TAG_subprogram
11268 || child->tag == DW_TAG_lexical_block)
11269 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
11270 child = child->sibling;
11271 }
11272 }
11273
11274 /* Get the low and high pc's represented by the scope DIE, and store
11275 them in *LOWPC and *HIGHPC. If the correct values can't be
11276 determined, set *LOWPC to -1 and *HIGHPC to 0. */
11277
11278 static void
11279 get_scope_pc_bounds (struct die_info *die,
11280 unrelocated_addr *lowpc, unrelocated_addr *highpc,
11281 struct dwarf2_cu *cu)
11282 {
11283 unrelocated_addr best_low = (unrelocated_addr) -1;
11284 unrelocated_addr best_high = {};
11285 unrelocated_addr current_low, current_high;
11286
11287 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
11288 nullptr, nullptr)
11289 >= PC_BOUNDS_RANGES)
11290 {
11291 best_low = current_low;
11292 best_high = current_high;
11293 }
11294 else
11295 {
11296 struct die_info *child = die->child;
11297
11298 while (child && child->tag)
11299 {
11300 switch (child->tag) {
11301 case DW_TAG_subprogram:
11302 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
11303 break;
11304 case DW_TAG_namespace:
11305 case DW_TAG_module:
11306 /* FIXME: carlton/2004-01-16: Should we do this for
11307 DW_TAG_class_type/DW_TAG_structure_type, too? I think
11308 that current GCC's always emit the DIEs corresponding
11309 to definitions of methods of classes as children of a
11310 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11311 the DIEs giving the declarations, which could be
11312 anywhere). But I don't see any reason why the
11313 standards says that they have to be there. */
11314 get_scope_pc_bounds (child, &current_low, &current_high, cu);
11315
11316 if (current_low != ((unrelocated_addr) -1))
11317 {
11318 best_low = std::min (best_low, current_low);
11319 best_high = std::max (best_high, current_high);
11320 }
11321 break;
11322 default:
11323 /* Ignore. */
11324 break;
11325 }
11326
11327 child = child->sibling;
11328 }
11329 }
11330
11331 *lowpc = best_low;
11332 *highpc = best_high;
11333 }
11334
11335 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
11336 in DIE. */
11337
11338 static void
11339 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
11340 struct dwarf2_cu *cu)
11341 {
11342 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11343 struct objfile *objfile = per_objfile->objfile;
11344 struct attribute *attr;
11345 struct attribute *attr_high;
11346
11347 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11348 if (attr_high)
11349 {
11350 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11351 if (attr != nullptr)
11352 {
11353 unrelocated_addr unrel_low = attr->as_address ();
11354 unrelocated_addr unrel_high = attr_high->as_address ();
11355
11356 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11357 unrel_high = (unrelocated_addr) ((ULONGEST) unrel_high
11358 + (ULONGEST) unrel_low);
11359
11360 CORE_ADDR low = per_objfile->relocate (unrel_low);
11361 CORE_ADDR high = per_objfile->relocate (unrel_high);
11362 cu->get_builder ()->record_block_range (block, low, high - 1);
11363 }
11364 }
11365
11366 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11367 if (attr != nullptr && attr->form_is_unsigned ())
11368 {
11369 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11370 on DWARF version). */
11371 ULONGEST ranges_offset = attr->as_unsigned ();
11372
11373 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11374 this value. */
11375 if (die->tag != DW_TAG_compile_unit)
11376 ranges_offset += cu->gnu_ranges_base;
11377
11378 std::vector<blockrange> blockvec;
11379 dwarf2_ranges_process (ranges_offset, cu, die->tag,
11380 [&] (unrelocated_addr start, unrelocated_addr end)
11381 {
11382 CORE_ADDR abs_start = per_objfile->relocate (start);
11383 CORE_ADDR abs_end = per_objfile->relocate (end);
11384 cu->get_builder ()->record_block_range (block, abs_start,
11385 abs_end - 1);
11386 blockvec.emplace_back (abs_start, abs_end);
11387 });
11388
11389 block->set_ranges (make_blockranges (objfile, blockvec));
11390 }
11391 }
11392
11393 /* Check whether the producer field indicates either of GCC < 4.6, or the
11394 Intel C/C++ compiler, and cache the result in CU. */
11395
11396 static void
11397 check_producer (struct dwarf2_cu *cu)
11398 {
11399 int major, minor;
11400
11401 if (cu->producer == NULL)
11402 {
11403 /* For unknown compilers expect their behavior is DWARF version
11404 compliant.
11405
11406 GCC started to support .debug_types sections by -gdwarf-4 since
11407 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11408 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11409 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11410 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11411 }
11412 else if (producer_is_gcc (cu->producer, &major, &minor))
11413 {
11414 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11415 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11416 cu->producer_is_gcc_11 = major == 11;
11417 }
11418 else if (producer_is_icc (cu->producer, &major, &minor))
11419 {
11420 cu->producer_is_icc = true;
11421 cu->producer_is_icc_lt_14 = major < 14;
11422 }
11423 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
11424 cu->producer_is_codewarrior = true;
11425 else if (producer_is_clang (cu->producer, &major, &minor))
11426 cu->producer_is_clang = true;
11427 else if (producer_is_gas (cu->producer, &major, &minor))
11428 {
11429 cu->producer_is_gas_lt_2_38 = major < 2 || (major == 2 && minor < 38);
11430 cu->producer_is_gas_2_39 = major == 2 && minor == 39;
11431 }
11432 else
11433 {
11434 /* For other non-GCC compilers, expect their behavior is DWARF version
11435 compliant. */
11436 }
11437
11438 cu->checked_producer = true;
11439 }
11440
11441 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11442 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11443 during 4.6.0 experimental. */
11444
11445 static bool
11446 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11447 {
11448 if (!cu->checked_producer)
11449 check_producer (cu);
11450
11451 return cu->producer_is_gxx_lt_4_6;
11452 }
11453
11454
11455 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
11456 with incorrect is_stmt attributes. */
11457
11458 static bool
11459 producer_is_codewarrior (struct dwarf2_cu *cu)
11460 {
11461 if (!cu->checked_producer)
11462 check_producer (cu);
11463
11464 return cu->producer_is_codewarrior;
11465 }
11466
11467 static bool
11468 producer_is_gas_lt_2_38 (struct dwarf2_cu *cu)
11469 {
11470 if (!cu->checked_producer)
11471 check_producer (cu);
11472
11473 return cu->producer_is_gas_lt_2_38;
11474 }
11475
11476 static bool
11477 producer_is_gas_2_39 (struct dwarf2_cu *cu)
11478 {
11479 if (!cu->checked_producer)
11480 check_producer (cu);
11481
11482 return cu->producer_is_gas_2_39;
11483 }
11484
11485 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
11486 If that attribute is not available, return the appropriate
11487 default. */
11488
11489 static enum dwarf_access_attribute
11490 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11491 {
11492 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11493 if (attr != nullptr)
11494 {
11495 LONGEST value = attr->constant_value (-1);
11496 if (value == DW_ACCESS_public
11497 || value == DW_ACCESS_protected
11498 || value == DW_ACCESS_private)
11499 return (dwarf_access_attribute) value;
11500 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
11501 plongest (value));
11502 }
11503
11504 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11505 {
11506 /* The default DWARF 2 accessibility for members is public, the default
11507 accessibility for inheritance is private. */
11508
11509 if (die->tag != DW_TAG_inheritance)
11510 return DW_ACCESS_public;
11511 else
11512 return DW_ACCESS_private;
11513 }
11514 else
11515 {
11516 /* DWARF 3+ defines the default accessibility a different way. The same
11517 rules apply now for DW_TAG_inheritance as for the members and it only
11518 depends on the container kind. */
11519
11520 if (die->parent->tag == DW_TAG_class_type)
11521 return DW_ACCESS_private;
11522 else
11523 return DW_ACCESS_public;
11524 }
11525 }
11526
11527 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
11528 *OFFSET to the byte offset. If the attribute was not found return
11529 0, otherwise return 1. If it was found but could not properly be
11530 handled, set *OFFSET to 0. */
11531
11532 static int
11533 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11534 LONGEST *offset)
11535 {
11536 struct attribute *attr;
11537
11538 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11539 if (attr != NULL)
11540 {
11541 *offset = 0;
11542 CORE_ADDR temp;
11543
11544 /* Note that we do not check for a section offset first here.
11545 This is because DW_AT_data_member_location is new in DWARF 4,
11546 so if we see it, we can assume that a constant form is really
11547 a constant and not a section offset. */
11548 if (attr->form_is_constant ())
11549 *offset = attr->constant_value (0);
11550 else if (attr->form_is_section_offset ())
11551 dwarf2_complex_location_expr_complaint ();
11552 else if (attr->form_is_block ()
11553 && decode_locdesc (attr->as_block (), cu, &temp))
11554 {
11555 *offset = temp;
11556 }
11557 else
11558 dwarf2_complex_location_expr_complaint ();
11559
11560 return 1;
11561 }
11562 else
11563 {
11564 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11565 if (attr != nullptr)
11566 {
11567 *offset = attr->constant_value (0);
11568 return 1;
11569 }
11570 }
11571
11572 return 0;
11573 }
11574
11575 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
11576 store the results in FIELD. */
11577
11578 static void
11579 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11580 struct field *field)
11581 {
11582 struct attribute *attr;
11583
11584 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11585 if (attr != NULL)
11586 {
11587 if (attr->form_is_constant ())
11588 {
11589 LONGEST offset = attr->constant_value (0);
11590
11591 /* Work around this GCC 11 bug, where it would erroneously use -1
11592 data member locations, instead of 0:
11593
11594 Negative DW_AT_data_member_location
11595 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
11596 */
11597 if (offset == -1 && cu->producer_is_gcc_11)
11598 {
11599 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
11600 offset = 0;
11601 }
11602
11603 field->set_loc_bitpos (offset * bits_per_byte);
11604 }
11605 else if (attr->form_is_section_offset ())
11606 dwarf2_complex_location_expr_complaint ();
11607 else if (attr->form_is_block ())
11608 {
11609 CORE_ADDR offset;
11610 if (decode_locdesc (attr->as_block (), cu, &offset))
11611 field->set_loc_bitpos (offset * bits_per_byte);
11612 else
11613 {
11614 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11615 struct objfile *objfile = per_objfile->objfile;
11616 struct dwarf2_locexpr_baton *dlbaton
11617 = XOBNEW (&objfile->objfile_obstack,
11618 struct dwarf2_locexpr_baton);
11619 dlbaton->data = attr->as_block ()->data;
11620 dlbaton->size = attr->as_block ()->size;
11621 /* When using this baton, we want to compute the address
11622 of the field, not the value. This is why
11623 is_reference is set to false here. */
11624 dlbaton->is_reference = false;
11625 dlbaton->per_objfile = per_objfile;
11626 dlbaton->per_cu = cu->per_cu;
11627
11628 field->set_loc_dwarf_block (dlbaton);
11629 }
11630 }
11631 else
11632 dwarf2_complex_location_expr_complaint ();
11633 }
11634 else
11635 {
11636 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11637 if (attr != nullptr)
11638 field->set_loc_bitpos (attr->constant_value (0));
11639 }
11640 }
11641
11642 /* Add an aggregate field to the field list. */
11643
11644 static void
11645 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11646 struct dwarf2_cu *cu)
11647 {
11648 struct objfile *objfile = cu->per_objfile->objfile;
11649 struct gdbarch *gdbarch = objfile->arch ();
11650 struct nextfield *new_field;
11651 struct attribute *attr;
11652 struct field *fp;
11653 const char *fieldname = "";
11654
11655 if (die->tag == DW_TAG_inheritance)
11656 {
11657 fip->baseclasses.emplace_back ();
11658 new_field = &fip->baseclasses.back ();
11659 }
11660 else
11661 {
11662 fip->fields.emplace_back ();
11663 new_field = &fip->fields.back ();
11664 }
11665
11666 new_field->offset = die->sect_off;
11667
11668 switch (dwarf2_access_attribute (die, cu))
11669 {
11670 case DW_ACCESS_public:
11671 break;
11672 case DW_ACCESS_private:
11673 new_field->field.set_accessibility (accessibility::PRIVATE);
11674 break;
11675 case DW_ACCESS_protected:
11676 new_field->field.set_accessibility (accessibility::PROTECTED);
11677 break;
11678 default:
11679 gdb_assert_not_reached ("invalid accessibility");
11680 }
11681
11682 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11683 if (attr != nullptr && attr->as_virtuality ())
11684 new_field->field.set_virtual ();
11685
11686 fp = &new_field->field;
11687
11688 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
11689 && !die_is_declaration (die, cu))
11690 {
11691 if (die->tag == DW_TAG_namelist_item)
11692 {
11693 /* Typically, DW_TAG_namelist_item are references to namelist items.
11694 If so, follow that reference. */
11695 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
11696 struct die_info *item_die = nullptr;
11697 struct dwarf2_cu *item_cu = cu;
11698 if (attr1->form_is_ref ())
11699 item_die = follow_die_ref (die, attr1, &item_cu);
11700 if (item_die != nullptr)
11701 die = item_die;
11702 }
11703 /* Data member other than a C++ static data member. */
11704
11705 /* Get type of field. */
11706 fp->set_type (die_type (die, cu));
11707
11708 fp->set_loc_bitpos (0);
11709
11710 /* Get bit size of field (zero if none). */
11711 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11712 if (attr != nullptr)
11713 fp->set_bitsize (attr->constant_value (0));
11714 else
11715 fp->set_bitsize (0);
11716
11717 /* Get bit offset of field. */
11718 handle_member_location (die, cu, fp);
11719 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11720 if (attr != nullptr && attr->form_is_constant ())
11721 {
11722 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
11723 {
11724 /* For big endian bits, the DW_AT_bit_offset gives the
11725 additional bit offset from the MSB of the containing
11726 anonymous object to the MSB of the field. We don't
11727 have to do anything special since we don't need to
11728 know the size of the anonymous object. */
11729 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
11730 }
11731 else
11732 {
11733 /* For little endian bits, compute the bit offset to the
11734 MSB of the anonymous object, subtract off the number of
11735 bits from the MSB of the field to the MSB of the
11736 object, and then subtract off the number of bits of
11737 the field itself. The result is the bit offset of
11738 the LSB of the field. */
11739 int anonymous_size;
11740 int bit_offset = attr->constant_value (0);
11741
11742 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11743 if (attr != nullptr && attr->form_is_constant ())
11744 {
11745 /* The size of the anonymous object containing
11746 the bit field is explicit, so use the
11747 indicated size (in bytes). */
11748 anonymous_size = attr->constant_value (0);
11749 }
11750 else
11751 {
11752 /* The size of the anonymous object containing
11753 the bit field must be inferred from the type
11754 attribute of the data member containing the
11755 bit field. */
11756 anonymous_size = fp->type ()->length ();
11757 }
11758 fp->set_loc_bitpos (fp->loc_bitpos ()
11759 + anonymous_size * bits_per_byte
11760 - bit_offset - fp->bitsize ());
11761 }
11762 }
11763
11764 /* Get name of field. */
11765 fieldname = dwarf2_name (die, cu);
11766 if (fieldname == NULL)
11767 fieldname = "";
11768
11769 /* The name is already allocated along with this objfile, so we don't
11770 need to duplicate it for the type. */
11771 fp->set_name (fieldname);
11772
11773 /* Change accessibility for artificial fields (e.g. virtual table
11774 pointer or virtual base class pointer) to private. */
11775 if (dwarf2_attr (die, DW_AT_artificial, cu))
11776 {
11777 fp->set_is_artificial (true);
11778 fp->set_accessibility (accessibility::PRIVATE);
11779 }
11780 }
11781 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11782 {
11783 /* C++ static member. */
11784
11785 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11786 is a declaration, but all versions of G++ as of this writing
11787 (so through at least 3.2.1) incorrectly generate
11788 DW_TAG_variable tags. */
11789
11790 const char *physname;
11791
11792 /* Get name of field. */
11793 fieldname = dwarf2_name (die, cu);
11794 if (fieldname == NULL)
11795 return;
11796
11797 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11798 if (attr
11799 /* Only create a symbol if this is an external value.
11800 new_symbol checks this and puts the value in the global symbol
11801 table, which we want. If it is not external, new_symbol
11802 will try to put the value in cu->list_in_scope which is wrong. */
11803 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11804 {
11805 /* A static const member, not much different than an enum as far as
11806 we're concerned, except that we can support more types. */
11807 new_symbol (die, NULL, cu);
11808 }
11809
11810 /* Get physical name. */
11811 physname = dwarf2_physname (fieldname, die, cu);
11812
11813 /* The name is already allocated along with this objfile, so we don't
11814 need to duplicate it for the type. */
11815 fp->set_loc_physname (physname ? physname : "");
11816 fp->set_type (die_type (die, cu));
11817 fp->set_name (fieldname);
11818 }
11819 else if (die->tag == DW_TAG_inheritance)
11820 {
11821 /* C++ base class field. */
11822 handle_member_location (die, cu, fp);
11823 fp->set_bitsize (0);
11824 fp->set_type (die_type (die, cu));
11825 fp->set_name (fp->type ()->name ());
11826 }
11827 else
11828 gdb_assert_not_reached ("missing case in dwarf2_add_field");
11829 }
11830
11831 /* Can the type given by DIE define another type? */
11832
11833 static bool
11834 type_can_define_types (const struct die_info *die)
11835 {
11836 switch (die->tag)
11837 {
11838 case DW_TAG_typedef:
11839 case DW_TAG_class_type:
11840 case DW_TAG_structure_type:
11841 case DW_TAG_union_type:
11842 case DW_TAG_enumeration_type:
11843 return true;
11844
11845 default:
11846 return false;
11847 }
11848 }
11849
11850 /* Add a type definition defined in the scope of the FIP's class. */
11851
11852 static void
11853 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
11854 struct dwarf2_cu *cu)
11855 {
11856 struct decl_field fp;
11857 memset (&fp, 0, sizeof (fp));
11858
11859 gdb_assert (type_can_define_types (die));
11860
11861 /* Get name of field. NULL is okay here, meaning an anonymous type. */
11862 fp.name = dwarf2_name (die, cu);
11863 fp.type = read_type_die (die, cu);
11864
11865 /* Save accessibility. */
11866 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
11867 switch (accessibility)
11868 {
11869 case DW_ACCESS_public:
11870 /* The assumed value if neither private nor protected. */
11871 break;
11872 case DW_ACCESS_private:
11873 fp.accessibility = accessibility::PRIVATE;
11874 break;
11875 case DW_ACCESS_protected:
11876 fp.accessibility = accessibility::PROTECTED;
11877 break;
11878 }
11879
11880 if (die->tag == DW_TAG_typedef)
11881 fip->typedef_field_list.push_back (fp);
11882 else
11883 fip->nested_types_list.push_back (fp);
11884 }
11885
11886 /* A convenience typedef that's used when finding the discriminant
11887 field for a variant part. */
11888 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
11889 offset_map_type;
11890
11891 /* Compute the discriminant range for a given variant. OBSTACK is
11892 where the results will be stored. VARIANT is the variant to
11893 process. IS_UNSIGNED indicates whether the discriminant is signed
11894 or unsigned. */
11895
11896 static const gdb::array_view<discriminant_range>
11897 convert_variant_range (struct obstack *obstack, const variant_field &variant,
11898 bool is_unsigned)
11899 {
11900 std::vector<discriminant_range> ranges;
11901
11902 if (variant.default_branch)
11903 return {};
11904
11905 if (variant.discr_list_data == nullptr)
11906 {
11907 discriminant_range r
11908 = {variant.discriminant_value, variant.discriminant_value};
11909 ranges.push_back (r);
11910 }
11911 else
11912 {
11913 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
11914 variant.discr_list_data->size);
11915 while (!data.empty ())
11916 {
11917 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
11918 {
11919 complaint (_("invalid discriminant marker: %d"), data[0]);
11920 break;
11921 }
11922 bool is_range = data[0] == DW_DSC_range;
11923 data = data.slice (1);
11924
11925 ULONGEST low, high;
11926 unsigned int bytes_read;
11927
11928 if (data.empty ())
11929 {
11930 complaint (_("DW_AT_discr_list missing low value"));
11931 break;
11932 }
11933 if (is_unsigned)
11934 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
11935 else
11936 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
11937 &bytes_read);
11938 data = data.slice (bytes_read);
11939
11940 if (is_range)
11941 {
11942 if (data.empty ())
11943 {
11944 complaint (_("DW_AT_discr_list missing high value"));
11945 break;
11946 }
11947 if (is_unsigned)
11948 high = read_unsigned_leb128 (nullptr, data.data (),
11949 &bytes_read);
11950 else
11951 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
11952 &bytes_read);
11953 data = data.slice (bytes_read);
11954 }
11955 else
11956 high = low;
11957
11958 ranges.push_back ({ low, high });
11959 }
11960 }
11961
11962 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
11963 ranges.size ());
11964 std::copy (ranges.begin (), ranges.end (), result);
11965 return gdb::array_view<discriminant_range> (result, ranges.size ());
11966 }
11967
11968 static const gdb::array_view<variant_part> create_variant_parts
11969 (struct obstack *obstack,
11970 const offset_map_type &offset_map,
11971 struct field_info *fi,
11972 const std::vector<variant_part_builder> &variant_parts);
11973
11974 /* Fill in a "struct variant" for a given variant field. RESULT is
11975 the variant to fill in. OBSTACK is where any needed allocations
11976 will be done. OFFSET_MAP holds the mapping from section offsets to
11977 fields for the type. FI describes the fields of the type we're
11978 processing. FIELD is the variant field we're converting. */
11979
11980 static void
11981 create_one_variant (variant &result, struct obstack *obstack,
11982 const offset_map_type &offset_map,
11983 struct field_info *fi, const variant_field &field)
11984 {
11985 result.discriminants = convert_variant_range (obstack, field, false);
11986 result.first_field = field.first_field + fi->baseclasses.size ();
11987 result.last_field = field.last_field + fi->baseclasses.size ();
11988 result.parts = create_variant_parts (obstack, offset_map, fi,
11989 field.variant_parts);
11990 }
11991
11992 /* Fill in a "struct variant_part" for a given variant part. RESULT
11993 is the variant part to fill in. OBSTACK is where any needed
11994 allocations will be done. OFFSET_MAP holds the mapping from
11995 section offsets to fields for the type. FI describes the fields of
11996 the type we're processing. BUILDER is the variant part to be
11997 converted. */
11998
11999 static void
12000 create_one_variant_part (variant_part &result,
12001 struct obstack *obstack,
12002 const offset_map_type &offset_map,
12003 struct field_info *fi,
12004 const variant_part_builder &builder)
12005 {
12006 auto iter = offset_map.find (builder.discriminant_offset);
12007 if (iter == offset_map.end ())
12008 {
12009 result.discriminant_index = -1;
12010 /* Doesn't matter. */
12011 result.is_unsigned = false;
12012 }
12013 else
12014 {
12015 result.discriminant_index = iter->second;
12016 result.is_unsigned
12017 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
12018 }
12019
12020 size_t n = builder.variants.size ();
12021 variant *output = new (obstack) variant[n];
12022 for (size_t i = 0; i < n; ++i)
12023 create_one_variant (output[i], obstack, offset_map, fi,
12024 builder.variants[i]);
12025
12026 result.variants = gdb::array_view<variant> (output, n);
12027 }
12028
12029 /* Create a vector of variant parts that can be attached to a type.
12030 OBSTACK is where any needed allocations will be done. OFFSET_MAP
12031 holds the mapping from section offsets to fields for the type. FI
12032 describes the fields of the type we're processing. VARIANT_PARTS
12033 is the vector to convert. */
12034
12035 static const gdb::array_view<variant_part>
12036 create_variant_parts (struct obstack *obstack,
12037 const offset_map_type &offset_map,
12038 struct field_info *fi,
12039 const std::vector<variant_part_builder> &variant_parts)
12040 {
12041 if (variant_parts.empty ())
12042 return {};
12043
12044 size_t n = variant_parts.size ();
12045 variant_part *result = new (obstack) variant_part[n];
12046 for (size_t i = 0; i < n; ++i)
12047 create_one_variant_part (result[i], obstack, offset_map, fi,
12048 variant_parts[i]);
12049
12050 return gdb::array_view<variant_part> (result, n);
12051 }
12052
12053 /* Compute the variant part vector for FIP, attaching it to TYPE when
12054 done. */
12055
12056 static void
12057 add_variant_property (struct field_info *fip, struct type *type,
12058 struct dwarf2_cu *cu)
12059 {
12060 /* Map section offsets of fields to their field index. Note the
12061 field index here does not take the number of baseclasses into
12062 account. */
12063 offset_map_type offset_map;
12064 for (int i = 0; i < fip->fields.size (); ++i)
12065 offset_map[fip->fields[i].offset] = i;
12066
12067 struct objfile *objfile = cu->per_objfile->objfile;
12068 gdb::array_view<const variant_part> parts
12069 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
12070 fip->variant_parts);
12071
12072 struct dynamic_prop prop;
12073 prop.set_variant_parts ((gdb::array_view<variant_part> *)
12074 obstack_copy (&objfile->objfile_obstack, &parts,
12075 sizeof (parts)));
12076
12077 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
12078 }
12079
12080 /* Create the vector of fields, and attach it to the type. */
12081
12082 static void
12083 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12084 struct dwarf2_cu *cu)
12085 {
12086 int nfields = fip->nfields ();
12087
12088 /* Record the field count, allocate space for the array of fields,
12089 and create blank accessibility bitfields if necessary. */
12090 type->alloc_fields (nfields);
12091
12092 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
12093 {
12094 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12095 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
12096 }
12097
12098 if (!fip->variant_parts.empty ())
12099 add_variant_property (fip, type, cu);
12100
12101 /* Copy the saved-up fields into the field vector. */
12102 for (int i = 0; i < nfields; ++i)
12103 {
12104 struct nextfield &field
12105 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
12106 : fip->fields[i - fip->baseclasses.size ()]);
12107
12108 type->field (i) = field.field;
12109 }
12110 }
12111
12112 /* Return true if this member function is a constructor, false
12113 otherwise. */
12114
12115 static int
12116 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12117 {
12118 const char *fieldname;
12119 const char *type_name;
12120 int len;
12121
12122 if (die->parent == NULL)
12123 return 0;
12124
12125 if (die->parent->tag != DW_TAG_structure_type
12126 && die->parent->tag != DW_TAG_union_type
12127 && die->parent->tag != DW_TAG_class_type)
12128 return 0;
12129
12130 fieldname = dwarf2_name (die, cu);
12131 type_name = dwarf2_name (die->parent, cu);
12132 if (fieldname == NULL || type_name == NULL)
12133 return 0;
12134
12135 len = strlen (fieldname);
12136 return (strncmp (fieldname, type_name, len) == 0
12137 && (type_name[len] == '\0' || type_name[len] == '<'));
12138 }
12139
12140 /* Add a member function to the proper fieldlist. */
12141
12142 static void
12143 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12144 struct type *type, struct dwarf2_cu *cu)
12145 {
12146 struct objfile *objfile = cu->per_objfile->objfile;
12147 struct attribute *attr;
12148 int i;
12149 struct fnfieldlist *flp = nullptr;
12150 struct fn_field *fnp;
12151 const char *fieldname;
12152 struct type *this_type;
12153
12154 if (cu->lang () == language_ada)
12155 error (_("unexpected member function in Ada type"));
12156
12157 /* Get name of member function. */
12158 fieldname = dwarf2_name (die, cu);
12159 if (fieldname == NULL)
12160 return;
12161
12162 /* Look up member function name in fieldlist. */
12163 for (i = 0; i < fip->fnfieldlists.size (); i++)
12164 {
12165 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12166 {
12167 flp = &fip->fnfieldlists[i];
12168 break;
12169 }
12170 }
12171
12172 /* Create a new fnfieldlist if necessary. */
12173 if (flp == nullptr)
12174 {
12175 fip->fnfieldlists.emplace_back ();
12176 flp = &fip->fnfieldlists.back ();
12177 flp->name = fieldname;
12178 i = fip->fnfieldlists.size () - 1;
12179 }
12180
12181 /* Create a new member function field and add it to the vector of
12182 fnfieldlists. */
12183 flp->fnfields.emplace_back ();
12184 fnp = &flp->fnfields.back ();
12185
12186 /* Delay processing of the physname until later. */
12187 if (cu->lang () == language_cplus)
12188 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
12189 die, cu);
12190 else
12191 {
12192 const char *physname = dwarf2_physname (fieldname, die, cu);
12193 fnp->physname = physname ? physname : "";
12194 }
12195
12196 fnp->type = type_allocator (objfile, cu->lang ()).new_type ();
12197 this_type = read_type_die (die, cu);
12198 if (this_type && this_type->code () == TYPE_CODE_FUNC)
12199 {
12200 int nparams = this_type->num_fields ();
12201
12202 /* TYPE is the domain of this method, and THIS_TYPE is the type
12203 of the method itself (TYPE_CODE_METHOD). */
12204 smash_to_method_type (fnp->type, type,
12205 this_type->target_type (),
12206 this_type->fields (),
12207 this_type->num_fields (),
12208 this_type->has_varargs ());
12209
12210 /* Handle static member functions.
12211 Dwarf2 has no clean way to discern C++ static and non-static
12212 member functions. G++ helps GDB by marking the first
12213 parameter for non-static member functions (which is the this
12214 pointer) as artificial. We obtain this information from
12215 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12216 if (nparams == 0 || this_type->field (0).is_artificial () == 0)
12217 fnp->voffset = VOFFSET_STATIC;
12218 }
12219 else
12220 complaint (_("member function type missing for '%s'"),
12221 dwarf2_full_name (fieldname, die, cu));
12222
12223 /* Get fcontext from DW_AT_containing_type if present. */
12224 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12225 fnp->fcontext = die_containing_type (die, cu);
12226
12227 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12228 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12229
12230 /* Get accessibility. */
12231 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
12232 switch (accessibility)
12233 {
12234 case DW_ACCESS_private:
12235 fnp->accessibility = accessibility::PRIVATE;
12236 break;
12237 case DW_ACCESS_protected:
12238 fnp->accessibility = accessibility::PROTECTED;
12239 break;
12240 }
12241
12242 /* Check for artificial methods. */
12243 attr = dwarf2_attr (die, DW_AT_artificial, cu);
12244 if (attr && attr->as_boolean ())
12245 fnp->is_artificial = 1;
12246
12247 /* Check for defaulted methods. */
12248 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
12249 if (attr != nullptr)
12250 fnp->defaulted = attr->defaulted ();
12251
12252 /* Check for deleted methods. */
12253 attr = dwarf2_attr (die, DW_AT_deleted, cu);
12254 if (attr != nullptr && attr->as_boolean ())
12255 fnp->is_deleted = 1;
12256
12257 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12258
12259 /* Get index in virtual function table if it is a virtual member
12260 function. For older versions of GCC, this is an offset in the
12261 appropriate virtual table, as specified by DW_AT_containing_type.
12262 For everyone else, it is an expression to be evaluated relative
12263 to the object address. */
12264
12265 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12266 if (attr != nullptr)
12267 {
12268 if (attr->form_is_block () && attr->as_block ()->size > 0)
12269 {
12270 struct dwarf_block *block = attr->as_block ();
12271 CORE_ADDR offset;
12272
12273 if (block->data[0] == DW_OP_constu
12274 && decode_locdesc (block, cu, &offset))
12275 {
12276 /* "Old"-style GCC. See
12277 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44126
12278 for discussion. This was known and a patch available
12279 in 2010, but as of 2023, both GCC and clang still
12280 emit this. */
12281 fnp->voffset = offset + 2;
12282 }
12283 else if ((block->data[0] == DW_OP_deref
12284 || (block->size > 1
12285 && block->data[0] == DW_OP_deref_size
12286 && block->data[1] == cu->header.addr_size))
12287 && decode_locdesc (block, cu, &offset))
12288 {
12289 fnp->voffset = offset;
12290 if ((fnp->voffset % cu->header.addr_size) != 0)
12291 dwarf2_complex_location_expr_complaint ();
12292 else
12293 fnp->voffset /= cu->header.addr_size;
12294 fnp->voffset += 2;
12295 }
12296 else
12297 dwarf2_complex_location_expr_complaint ();
12298
12299 if (!fnp->fcontext)
12300 {
12301 /* If there is no `this' field and no DW_AT_containing_type,
12302 we cannot actually find a base class context for the
12303 vtable! */
12304 if (this_type->num_fields () == 0
12305 || !this_type->field (0).is_artificial ())
12306 {
12307 complaint (_("cannot determine context for virtual member "
12308 "function \"%s\" (offset %s)"),
12309 fieldname, sect_offset_str (die->sect_off));
12310 }
12311 else
12312 {
12313 fnp->fcontext = this_type->field (0).type ()->target_type ();
12314 }
12315 }
12316 }
12317 else if (attr->form_is_section_offset ())
12318 {
12319 dwarf2_complex_location_expr_complaint ();
12320 }
12321 else
12322 {
12323 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12324 fieldname);
12325 }
12326 }
12327 else
12328 {
12329 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12330 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
12331 {
12332 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12333 complaint (_("Member function \"%s\" (offset %s) is virtual "
12334 "but the vtable offset is not specified"),
12335 fieldname, sect_offset_str (die->sect_off));
12336 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12337 TYPE_CPLUS_DYNAMIC (type) = 1;
12338 }
12339 }
12340 }
12341
12342 /* Create the vector of member function fields, and attach it to the type. */
12343
12344 static void
12345 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
12346 struct dwarf2_cu *cu)
12347 {
12348 if (cu->lang () == language_ada)
12349 error (_("unexpected member functions in Ada type"));
12350
12351 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12352 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
12353 TYPE_ZALLOC (type,
12354 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
12355
12356 for (int i = 0; i < fip->fnfieldlists.size (); i++)
12357 {
12358 struct fnfieldlist &nf = fip->fnfieldlists[i];
12359 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
12360
12361 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
12362 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
12363 /* No need to zero-initialize, initialization is done by the copy in
12364 the loop below. */
12365 fn_flp->fn_fields = (struct fn_field *)
12366 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
12367
12368 for (int k = 0; k < nf.fnfields.size (); ++k)
12369 fn_flp->fn_fields[k] = nf.fnfields[k];
12370 }
12371
12372 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
12373 }
12374
12375 /* Returns non-zero if NAME is the name of a vtable member in CU's
12376 language, zero otherwise. */
12377 static int
12378 is_vtable_name (const char *name, struct dwarf2_cu *cu)
12379 {
12380 static const char vptr[] = "_vptr";
12381
12382 /* Look for the C++ form of the vtable. */
12383 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
12384 return 1;
12385
12386 return 0;
12387 }
12388
12389 /* GCC outputs unnamed structures that are really pointers to member
12390 functions, with the ABI-specified layout. If TYPE describes
12391 such a structure, smash it into a member function type.
12392
12393 GCC shouldn't do this; it should just output pointer to member DIEs.
12394 This is GCC PR debug/28767. */
12395
12396 static void
12397 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
12398 {
12399 struct type *pfn_type, *self_type, *new_type;
12400
12401 /* Check for a structure with no name and two children. */
12402 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12403 return;
12404
12405 /* Check for __pfn and __delta members. */
12406 if (type->field (0).name () == NULL
12407 || strcmp (type->field (0).name (), "__pfn") != 0
12408 || type->field (1).name () == NULL
12409 || strcmp (type->field (1).name (), "__delta") != 0)
12410 return;
12411
12412 /* Find the type of the method. */
12413 pfn_type = type->field (0).type ();
12414 if (pfn_type == NULL
12415 || pfn_type->code () != TYPE_CODE_PTR
12416 || pfn_type->target_type ()->code () != TYPE_CODE_FUNC)
12417 return;
12418
12419 /* Look for the "this" argument. */
12420 pfn_type = pfn_type->target_type ();
12421 if (pfn_type->num_fields () == 0
12422 /* || pfn_type->field (0).type () == NULL */
12423 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
12424 return;
12425
12426 self_type = pfn_type->field (0).type ()->target_type ();
12427 new_type = type_allocator (type).new_type ();
12428 smash_to_method_type (new_type, self_type, pfn_type->target_type (),
12429 pfn_type->fields (), pfn_type->num_fields (),
12430 pfn_type->has_varargs ());
12431 smash_to_methodptr_type (type, new_type);
12432 }
12433
12434 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
12435 requires rewriting, then copy it and return the updated copy.
12436 Otherwise return nullptr. */
12437
12438 static struct type *
12439 rewrite_array_type (struct type *type)
12440 {
12441 if (type->code () != TYPE_CODE_ARRAY)
12442 return nullptr;
12443
12444 struct type *index_type = type->index_type ();
12445 range_bounds *current_bounds = index_type->bounds ();
12446
12447 /* Handle multi-dimensional arrays. */
12448 struct type *new_target = rewrite_array_type (type->target_type ());
12449 if (new_target == nullptr)
12450 {
12451 /* Maybe we don't need to rewrite this array. */
12452 if (current_bounds->low.is_constant ()
12453 && current_bounds->high.is_constant ())
12454 return nullptr;
12455 }
12456
12457 /* Either the target type was rewritten, or the bounds have to be
12458 updated. Either way we want to copy the type and update
12459 everything. */
12460 struct type *copy = copy_type (type);
12461 copy->copy_fields (type);
12462 if (new_target != nullptr)
12463 copy->set_target_type (new_target);
12464
12465 struct type *index_copy = copy_type (index_type);
12466 range_bounds *bounds
12467 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
12468 sizeof (range_bounds));
12469 *bounds = *current_bounds;
12470 bounds->low.set_const_val (1);
12471 bounds->high.set_const_val (0);
12472 index_copy->set_bounds (bounds);
12473 copy->set_index_type (index_copy);
12474
12475 return copy;
12476 }
12477
12478 /* While some versions of GCC will generate complicated DWARF for an
12479 array (see quirk_ada_thick_pointer), more recent versions were
12480 modified to emit an explicit thick pointer structure. However, in
12481 this case, the array still has DWARF expressions for its ranges,
12482 and these must be ignored. */
12483
12484 static void
12485 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
12486 struct type *type)
12487 {
12488 gdb_assert (cu->lang () == language_ada);
12489
12490 /* Check for a structure with two children. */
12491 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12492 return;
12493
12494 /* Check for P_ARRAY and P_BOUNDS members. */
12495 if (type->field (0).name () == NULL
12496 || strcmp (type->field (0).name (), "P_ARRAY") != 0
12497 || type->field (1).name () == NULL
12498 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
12499 return;
12500
12501 /* Make sure we're looking at a pointer to an array. */
12502 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
12503 return;
12504
12505 /* The Ada code already knows how to handle these types, so all that
12506 we need to do is turn the bounds into static bounds. However, we
12507 don't want to rewrite existing array or index types in-place,
12508 because those may be referenced in other contexts where this
12509 rewriting is undesirable. */
12510 struct type *new_ary_type
12511 = rewrite_array_type (type->field (0).type ()->target_type ());
12512 if (new_ary_type != nullptr)
12513 type->field (0).set_type (lookup_pointer_type (new_ary_type));
12514 }
12515
12516 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
12517 appropriate error checking and issuing complaints if there is a
12518 problem. */
12519
12520 static ULONGEST
12521 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
12522 {
12523 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
12524
12525 if (attr == nullptr)
12526 return 0;
12527
12528 if (!attr->form_is_constant ())
12529 {
12530 complaint (_("DW_AT_alignment must have constant form"
12531 " - DIE at %s [in module %s]"),
12532 sect_offset_str (die->sect_off),
12533 objfile_name (cu->per_objfile->objfile));
12534 return 0;
12535 }
12536
12537 LONGEST val = attr->constant_value (0);
12538 if (val < 0)
12539 {
12540 complaint (_("DW_AT_alignment value must not be negative"
12541 " - DIE at %s [in module %s]"),
12542 sect_offset_str (die->sect_off),
12543 objfile_name (cu->per_objfile->objfile));
12544 return 0;
12545 }
12546 ULONGEST align = val;
12547
12548 if (align == 0)
12549 {
12550 complaint (_("DW_AT_alignment value must not be zero"
12551 " - DIE at %s [in module %s]"),
12552 sect_offset_str (die->sect_off),
12553 objfile_name (cu->per_objfile->objfile));
12554 return 0;
12555 }
12556 if ((align & (align - 1)) != 0)
12557 {
12558 complaint (_("DW_AT_alignment value must be a power of 2"
12559 " - DIE at %s [in module %s]"),
12560 sect_offset_str (die->sect_off),
12561 objfile_name (cu->per_objfile->objfile));
12562 return 0;
12563 }
12564
12565 return align;
12566 }
12567
12568 /* If the DIE has a DW_AT_alignment attribute, use its value to set
12569 the alignment for TYPE. */
12570
12571 static void
12572 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
12573 struct type *type)
12574 {
12575 if (!set_type_align (type, get_alignment (cu, die)))
12576 complaint (_("DW_AT_alignment value too large"
12577 " - DIE at %s [in module %s]"),
12578 sect_offset_str (die->sect_off),
12579 objfile_name (cu->per_objfile->objfile));
12580 }
12581
12582 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12583 constant for a type, according to DWARF5 spec, Table 5.5. */
12584
12585 static bool
12586 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
12587 {
12588 switch (value)
12589 {
12590 case DW_CC_normal:
12591 case DW_CC_pass_by_reference:
12592 case DW_CC_pass_by_value:
12593 return true;
12594
12595 default:
12596 complaint (_("unrecognized DW_AT_calling_convention value "
12597 "(%s) for a type"), pulongest (value));
12598 return false;
12599 }
12600 }
12601
12602 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12603 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
12604 also according to GNU-specific values (see include/dwarf2.h). */
12605
12606 static bool
12607 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
12608 {
12609 switch (value)
12610 {
12611 case DW_CC_normal:
12612 case DW_CC_program:
12613 case DW_CC_nocall:
12614 return true;
12615
12616 case DW_CC_GNU_renesas_sh:
12617 case DW_CC_GNU_borland_fastcall_i386:
12618 case DW_CC_GDB_IBM_OpenCL:
12619 return true;
12620
12621 default:
12622 complaint (_("unrecognized DW_AT_calling_convention value "
12623 "(%s) for a subroutine"), pulongest (value));
12624 return false;
12625 }
12626 }
12627
12628 /* Called when we find the DIE that starts a structure or union scope
12629 (definition) to create a type for the structure or union. Fill in
12630 the type's name and general properties; the members will not be
12631 processed until process_structure_scope. A symbol table entry for
12632 the type will also not be done until process_structure_scope (assuming
12633 the type has a name).
12634
12635 NOTE: we need to call these functions regardless of whether or not the
12636 DIE has a DW_AT_name attribute, since it might be an anonymous
12637 structure or union. This gets the type entered into our set of
12638 user defined types. */
12639
12640 static struct type *
12641 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
12642 {
12643 struct objfile *objfile = cu->per_objfile->objfile;
12644 struct type *type;
12645 struct attribute *attr;
12646 const char *name;
12647
12648 /* If the definition of this type lives in .debug_types, read that type.
12649 Don't follow DW_AT_specification though, that will take us back up
12650 the chain and we want to go down. */
12651 attr = die->attr (DW_AT_signature);
12652 if (attr != nullptr)
12653 {
12654 type = get_DW_AT_signature_type (die, attr, cu);
12655
12656 /* The type's CU may not be the same as CU.
12657 Ensure TYPE is recorded with CU in die_type_hash. */
12658 return set_die_type (die, type, cu);
12659 }
12660
12661 type = type_allocator (objfile, cu->lang ()).new_type ();
12662 INIT_CPLUS_SPECIFIC (type);
12663
12664 name = dwarf2_name (die, cu);
12665 if (name != NULL)
12666 {
12667 if (cu->lang () == language_cplus
12668 || cu->lang () == language_d
12669 || cu->lang () == language_rust)
12670 {
12671 const char *full_name = dwarf2_full_name (name, die, cu);
12672
12673 /* dwarf2_full_name might have already finished building the DIE's
12674 type. If so, there is no need to continue. */
12675 if (get_die_type (die, cu) != NULL)
12676 return get_die_type (die, cu);
12677
12678 type->set_name (full_name);
12679 }
12680 else
12681 {
12682 /* The name is already allocated along with this objfile, so
12683 we don't need to duplicate it for the type. */
12684 type->set_name (name);
12685 }
12686 }
12687
12688 if (die->tag == DW_TAG_structure_type)
12689 {
12690 type->set_code (TYPE_CODE_STRUCT);
12691 }
12692 else if (die->tag == DW_TAG_union_type)
12693 {
12694 type->set_code (TYPE_CODE_UNION);
12695 }
12696 else if (die->tag == DW_TAG_namelist)
12697 {
12698 type->set_code (TYPE_CODE_NAMELIST);
12699 }
12700 else
12701 {
12702 type->set_code (TYPE_CODE_STRUCT);
12703 }
12704
12705 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
12706 type->set_is_declared_class (true);
12707
12708 /* Store the calling convention in the type if it's available in
12709 the die. Otherwise the calling convention remains set to
12710 the default value DW_CC_normal. */
12711 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12712 if (attr != nullptr
12713 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
12714 {
12715 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12716 TYPE_CPLUS_CALLING_CONVENTION (type)
12717 = (enum dwarf_calling_convention) (attr->constant_value (0));
12718 }
12719
12720 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12721 if (attr != nullptr)
12722 {
12723 if (attr->form_is_constant ())
12724 type->set_length (attr->constant_value (0));
12725 else
12726 {
12727 struct dynamic_prop prop;
12728 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
12729 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
12730
12731 type->set_length (0);
12732 }
12733 }
12734 else
12735 type->set_length (0);
12736
12737 maybe_set_alignment (cu, die, type);
12738
12739 if (producer_is_icc_lt_14 (cu) && (type->length () == 0))
12740 {
12741 /* ICC<14 does not output the required DW_AT_declaration on
12742 incomplete types, but gives them a size of zero. */
12743 type->set_is_stub (true);
12744 }
12745 else
12746 type->set_stub_is_supported (true);
12747
12748 if (die_is_declaration (die, cu))
12749 type->set_is_stub (true);
12750 else if (attr == NULL && die->child == NULL
12751 && producer_is_realview (cu->producer))
12752 /* RealView does not output the required DW_AT_declaration
12753 on incomplete types. */
12754 type->set_is_stub (true);
12755
12756 /* We need to add the type field to the die immediately so we don't
12757 infinitely recurse when dealing with pointers to the structure
12758 type within the structure itself. */
12759 set_die_type (die, type, cu);
12760
12761 /* set_die_type should be already done. */
12762 set_descriptive_type (type, die, cu);
12763
12764 return type;
12765 }
12766
12767 static void handle_struct_member_die
12768 (struct die_info *child_die,
12769 struct type *type,
12770 struct field_info *fi,
12771 std::vector<struct symbol *> *template_args,
12772 struct dwarf2_cu *cu);
12773
12774 /* A helper for handle_struct_member_die that handles
12775 DW_TAG_variant_part. */
12776
12777 static void
12778 handle_variant_part (struct die_info *die, struct type *type,
12779 struct field_info *fi,
12780 std::vector<struct symbol *> *template_args,
12781 struct dwarf2_cu *cu)
12782 {
12783 variant_part_builder *new_part;
12784 if (fi->current_variant_part == nullptr)
12785 {
12786 fi->variant_parts.emplace_back ();
12787 new_part = &fi->variant_parts.back ();
12788 }
12789 else if (!fi->current_variant_part->processing_variant)
12790 {
12791 complaint (_("nested DW_TAG_variant_part seen "
12792 "- DIE at %s [in module %s]"),
12793 sect_offset_str (die->sect_off),
12794 objfile_name (cu->per_objfile->objfile));
12795 return;
12796 }
12797 else
12798 {
12799 variant_field &current = fi->current_variant_part->variants.back ();
12800 current.variant_parts.emplace_back ();
12801 new_part = &current.variant_parts.back ();
12802 }
12803
12804 /* When we recurse, we want callees to add to this new variant
12805 part. */
12806 scoped_restore save_current_variant_part
12807 = make_scoped_restore (&fi->current_variant_part, new_part);
12808
12809 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
12810 if (discr == NULL)
12811 {
12812 /* It's a univariant form, an extension we support. */
12813 }
12814 else if (discr->form_is_ref ())
12815 {
12816 struct dwarf2_cu *target_cu = cu;
12817 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
12818
12819 new_part->discriminant_offset = target_die->sect_off;
12820 }
12821 else
12822 {
12823 complaint (_("DW_AT_discr does not have DIE reference form"
12824 " - DIE at %s [in module %s]"),
12825 sect_offset_str (die->sect_off),
12826 objfile_name (cu->per_objfile->objfile));
12827 }
12828
12829 for (die_info *child_die = die->child;
12830 child_die != NULL;
12831 child_die = child_die->sibling)
12832 handle_struct_member_die (child_die, type, fi, template_args, cu);
12833 }
12834
12835 /* A helper for handle_struct_member_die that handles
12836 DW_TAG_variant. */
12837
12838 static void
12839 handle_variant (struct die_info *die, struct type *type,
12840 struct field_info *fi,
12841 std::vector<struct symbol *> *template_args,
12842 struct dwarf2_cu *cu)
12843 {
12844 if (fi->current_variant_part == nullptr)
12845 {
12846 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
12847 "- DIE at %s [in module %s]"),
12848 sect_offset_str (die->sect_off),
12849 objfile_name (cu->per_objfile->objfile));
12850 return;
12851 }
12852 if (fi->current_variant_part->processing_variant)
12853 {
12854 complaint (_("nested DW_TAG_variant seen "
12855 "- DIE at %s [in module %s]"),
12856 sect_offset_str (die->sect_off),
12857 objfile_name (cu->per_objfile->objfile));
12858 return;
12859 }
12860
12861 scoped_restore save_processing_variant
12862 = make_scoped_restore (&fi->current_variant_part->processing_variant,
12863 true);
12864
12865 fi->current_variant_part->variants.emplace_back ();
12866 variant_field &variant = fi->current_variant_part->variants.back ();
12867 variant.first_field = fi->fields.size ();
12868
12869 /* In a variant we want to get the discriminant and also add a
12870 field for our sole member child. */
12871 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
12872 if (discr == nullptr || !discr->form_is_constant ())
12873 {
12874 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
12875 if (discr == nullptr || discr->as_block ()->size == 0)
12876 variant.default_branch = true;
12877 else
12878 variant.discr_list_data = discr->as_block ();
12879 }
12880 else
12881 variant.discriminant_value = discr->constant_value (0);
12882
12883 for (die_info *variant_child = die->child;
12884 variant_child != NULL;
12885 variant_child = variant_child->sibling)
12886 handle_struct_member_die (variant_child, type, fi, template_args, cu);
12887
12888 variant.last_field = fi->fields.size ();
12889 }
12890
12891 /* A helper for process_structure_scope that handles a single member
12892 DIE. */
12893
12894 static void
12895 handle_struct_member_die (struct die_info *child_die, struct type *type,
12896 struct field_info *fi,
12897 std::vector<struct symbol *> *template_args,
12898 struct dwarf2_cu *cu)
12899 {
12900 if (child_die->tag == DW_TAG_member
12901 || child_die->tag == DW_TAG_variable
12902 || child_die->tag == DW_TAG_namelist_item)
12903 {
12904 /* NOTE: carlton/2002-11-05: A C++ static data member
12905 should be a DW_TAG_member that is a declaration, but
12906 all versions of G++ as of this writing (so through at
12907 least 3.2.1) incorrectly generate DW_TAG_variable
12908 tags for them instead. */
12909 dwarf2_add_field (fi, child_die, cu);
12910 }
12911 else if (child_die->tag == DW_TAG_subprogram)
12912 {
12913 /* Rust doesn't have member functions in the C++ sense.
12914 However, it does emit ordinary functions as children
12915 of a struct DIE. */
12916 if (cu->lang () == language_rust)
12917 read_func_scope (child_die, cu);
12918 else
12919 {
12920 /* C++ member function. */
12921 dwarf2_add_member_fn (fi, child_die, type, cu);
12922 }
12923 }
12924 else if (child_die->tag == DW_TAG_inheritance)
12925 {
12926 /* C++ base class field. */
12927 dwarf2_add_field (fi, child_die, cu);
12928 }
12929 else if (type_can_define_types (child_die))
12930 dwarf2_add_type_defn (fi, child_die, cu);
12931 else if (child_die->tag == DW_TAG_template_type_param
12932 || child_die->tag == DW_TAG_template_value_param)
12933 {
12934 struct symbol *arg = new_symbol (child_die, NULL, cu);
12935
12936 if (arg != NULL)
12937 template_args->push_back (arg);
12938 }
12939 else if (child_die->tag == DW_TAG_variant_part)
12940 handle_variant_part (child_die, type, fi, template_args, cu);
12941 else if (child_die->tag == DW_TAG_variant)
12942 handle_variant (child_die, type, fi, template_args, cu);
12943 }
12944
12945 /* Finish creating a structure or union type, including filling in its
12946 members and creating a symbol for it. This function also handles Fortran
12947 namelist variables, their items or members and creating a symbol for
12948 them. */
12949
12950 static void
12951 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12952 {
12953 struct objfile *objfile = cu->per_objfile->objfile;
12954 struct die_info *child_die;
12955 struct type *type;
12956
12957 type = get_die_type (die, cu);
12958 if (type == NULL)
12959 type = read_structure_type (die, cu);
12960
12961 bool has_template_parameters = false;
12962 if (die->child != NULL && ! die_is_declaration (die, cu))
12963 {
12964 struct field_info fi;
12965 std::vector<struct symbol *> template_args;
12966
12967 child_die = die->child;
12968
12969 while (child_die && child_die->tag)
12970 {
12971 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
12972 child_die = child_die->sibling;
12973 }
12974
12975 /* Attach template arguments to type. */
12976 if (!template_args.empty ())
12977 {
12978 has_template_parameters = true;
12979 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12980 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
12981 TYPE_TEMPLATE_ARGUMENTS (type)
12982 = XOBNEWVEC (&objfile->objfile_obstack,
12983 struct symbol *,
12984 TYPE_N_TEMPLATE_ARGUMENTS (type));
12985 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12986 template_args.data (),
12987 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12988 * sizeof (struct symbol *)));
12989 }
12990
12991 /* Attach fields and member functions to the type. */
12992 if (fi.nfields () > 0)
12993 dwarf2_attach_fields_to_type (&fi, type, cu);
12994 if (!fi.fnfieldlists.empty ())
12995 {
12996 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12997
12998 /* Get the type which refers to the base class (possibly this
12999 class itself) which contains the vtable pointer for the current
13000 class from the DW_AT_containing_type attribute. This use of
13001 DW_AT_containing_type is a GNU extension. */
13002
13003 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13004 {
13005 struct type *t = die_containing_type (die, cu);
13006
13007 set_type_vptr_basetype (type, t);
13008 if (type == t)
13009 {
13010 int i;
13011
13012 /* Our own class provides vtbl ptr. */
13013 for (i = t->num_fields () - 1;
13014 i >= TYPE_N_BASECLASSES (t);
13015 --i)
13016 {
13017 const char *fieldname = t->field (i).name ();
13018
13019 if (is_vtable_name (fieldname, cu))
13020 {
13021 set_type_vptr_fieldno (type, i);
13022 break;
13023 }
13024 }
13025
13026 /* Complain if virtual function table field not found. */
13027 if (i < TYPE_N_BASECLASSES (t))
13028 complaint (_("virtual function table pointer "
13029 "not found when defining class '%s'"),
13030 type->name () ? type->name () : "");
13031 }
13032 else
13033 {
13034 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13035 }
13036 }
13037 else if (cu->producer
13038 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13039 {
13040 /* The IBM XLC compiler does not provide direct indication
13041 of the containing type, but the vtable pointer is
13042 always named __vfp. */
13043
13044 int i;
13045
13046 for (i = type->num_fields () - 1;
13047 i >= TYPE_N_BASECLASSES (type);
13048 --i)
13049 {
13050 if (strcmp (type->field (i).name (), "__vfp") == 0)
13051 {
13052 set_type_vptr_fieldno (type, i);
13053 set_type_vptr_basetype (type, type);
13054 break;
13055 }
13056 }
13057 }
13058 }
13059
13060 /* Copy fi.typedef_field_list linked list elements content into the
13061 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13062 if (!fi.typedef_field_list.empty ())
13063 {
13064 int count = fi.typedef_field_list.size ();
13065
13066 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13067 /* No zero-initialization is needed, the elements are initialized by
13068 the copy in the loop below. */
13069 TYPE_TYPEDEF_FIELD_ARRAY (type)
13070 = ((struct decl_field *)
13071 TYPE_ALLOC (type,
13072 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
13073 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
13074
13075 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
13076 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
13077 }
13078
13079 /* Copy fi.nested_types_list linked list elements content into the
13080 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
13081 if (!fi.nested_types_list.empty ()
13082 && cu->lang () != language_ada)
13083 {
13084 int count = fi.nested_types_list.size ();
13085
13086 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13087 /* No zero-initialization is needed, the elements are initialized by
13088 the copy in the loop below. */
13089 TYPE_NESTED_TYPES_ARRAY (type)
13090 = ((struct decl_field *)
13091 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
13092 TYPE_NESTED_TYPES_COUNT (type) = count;
13093
13094 for (int i = 0; i < fi.nested_types_list.size (); ++i)
13095 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
13096 }
13097 }
13098
13099 quirk_gcc_member_function_pointer (type, objfile);
13100 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
13101 cu->rust_unions.push_back (type);
13102 else if (cu->lang () == language_ada)
13103 quirk_ada_thick_pointer_struct (die, cu, type);
13104
13105 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13106 snapshots) has been known to create a die giving a declaration
13107 for a class that has, as a child, a die giving a definition for a
13108 nested class. So we have to process our children even if the
13109 current die is a declaration. Normally, of course, a declaration
13110 won't have any children at all. */
13111
13112 child_die = die->child;
13113
13114 while (child_die != NULL && child_die->tag)
13115 {
13116 if (child_die->tag == DW_TAG_member
13117 || child_die->tag == DW_TAG_variable
13118 || child_die->tag == DW_TAG_inheritance
13119 || child_die->tag == DW_TAG_template_value_param
13120 || child_die->tag == DW_TAG_template_type_param)
13121 {
13122 /* Do nothing. */
13123 }
13124 else
13125 process_die (child_die, cu);
13126
13127 child_die = child_die->sibling;
13128 }
13129
13130 /* Do not consider external references. According to the DWARF standard,
13131 these DIEs are identified by the fact that they have no byte_size
13132 attribute, and a declaration attribute. */
13133 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13134 || !die_is_declaration (die, cu)
13135 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
13136 {
13137 struct symbol *sym = new_symbol (die, type, cu);
13138
13139 if (has_template_parameters)
13140 {
13141 struct symtab *symtab;
13142 if (sym != nullptr)
13143 symtab = sym->symtab ();
13144 else if (cu->line_header != nullptr)
13145 {
13146 /* Any related symtab will do. */
13147 symtab
13148 = cu->line_header->file_names ()[0].symtab;
13149 }
13150 else
13151 {
13152 symtab = nullptr;
13153 complaint (_("could not find suitable "
13154 "symtab for template parameter"
13155 " - DIE at %s [in module %s]"),
13156 sect_offset_str (die->sect_off),
13157 objfile_name (objfile));
13158 }
13159
13160 if (symtab != nullptr)
13161 {
13162 /* Make sure that the symtab is set on the new symbols.
13163 Even though they don't appear in this symtab directly,
13164 other parts of gdb assume that symbols do, and this is
13165 reasonably true. */
13166 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
13167 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
13168 }
13169 }
13170 }
13171 }
13172
13173 /* Assuming DIE is an enumeration type, and TYPE is its associated
13174 type, update TYPE using some information only available in DIE's
13175 children. In particular, the fields are computed. */
13176
13177 static void
13178 update_enumeration_type_from_children (struct die_info *die,
13179 struct type *type,
13180 struct dwarf2_cu *cu)
13181 {
13182 struct die_info *child_die;
13183 int unsigned_enum = 1;
13184 int flag_enum = 1;
13185
13186 auto_obstack obstack;
13187 std::vector<struct field> fields;
13188
13189 for (child_die = die->child;
13190 child_die != NULL && child_die->tag;
13191 child_die = child_die->sibling)
13192 {
13193 struct attribute *attr;
13194 LONGEST value;
13195 const gdb_byte *bytes;
13196 struct dwarf2_locexpr_baton *baton;
13197 const char *name;
13198
13199 if (child_die->tag != DW_TAG_enumerator)
13200 continue;
13201
13202 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13203 if (attr == NULL)
13204 continue;
13205
13206 name = dwarf2_name (child_die, cu);
13207 if (name == NULL)
13208 name = "<anonymous enumerator>";
13209
13210 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13211 &value, &bytes, &baton);
13212 if (value < 0)
13213 {
13214 unsigned_enum = 0;
13215 flag_enum = 0;
13216 }
13217 else
13218 {
13219 if (count_one_bits_ll (value) >= 2)
13220 flag_enum = 0;
13221 }
13222
13223 fields.emplace_back ();
13224 struct field &field = fields.back ();
13225 field.set_name (dwarf2_physname (name, child_die, cu));
13226 field.set_loc_enumval (value);
13227 }
13228
13229 if (!fields.empty ())
13230 type->copy_fields (fields);
13231 else
13232 flag_enum = 0;
13233
13234 if (unsigned_enum)
13235 type->set_is_unsigned (true);
13236
13237 if (flag_enum)
13238 type->set_is_flag_enum (true);
13239 }
13240
13241 /* Given a DW_AT_enumeration_type die, set its type. We do not
13242 complete the type's fields yet, or create any symbols. */
13243
13244 static struct type *
13245 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13246 {
13247 struct objfile *objfile = cu->per_objfile->objfile;
13248 struct type *type;
13249 struct attribute *attr;
13250 const char *name;
13251
13252 /* If the definition of this type lives in .debug_types, read that type.
13253 Don't follow DW_AT_specification though, that will take us back up
13254 the chain and we want to go down. */
13255 attr = die->attr (DW_AT_signature);
13256 if (attr != nullptr)
13257 {
13258 type = get_DW_AT_signature_type (die, attr, cu);
13259
13260 /* The type's CU may not be the same as CU.
13261 Ensure TYPE is recorded with CU in die_type_hash. */
13262 return set_die_type (die, type, cu);
13263 }
13264
13265 type = type_allocator (objfile, cu->lang ()).new_type ();
13266
13267 type->set_code (TYPE_CODE_ENUM);
13268 name = dwarf2_full_name (NULL, die, cu);
13269 if (name != NULL)
13270 type->set_name (name);
13271
13272 attr = dwarf2_attr (die, DW_AT_type, cu);
13273 if (attr != NULL)
13274 {
13275 struct type *underlying_type = die_type (die, cu);
13276
13277 type->set_target_type (underlying_type);
13278 }
13279
13280 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13281 if (attr != nullptr)
13282 type->set_length (attr->constant_value (0));
13283 else
13284 type->set_length (0);
13285
13286 maybe_set_alignment (cu, die, type);
13287
13288 /* The enumeration DIE can be incomplete. In Ada, any type can be
13289 declared as private in the package spec, and then defined only
13290 inside the package body. Such types are known as Taft Amendment
13291 Types. When another package uses such a type, an incomplete DIE
13292 may be generated by the compiler. */
13293 if (die_is_declaration (die, cu))
13294 type->set_is_stub (true);
13295
13296 /* If this type has an underlying type that is not a stub, then we
13297 may use its attributes. We always use the "unsigned" attribute
13298 in this situation, because ordinarily we guess whether the type
13299 is unsigned -- but the guess can be wrong and the underlying type
13300 can tell us the reality. However, we defer to a local size
13301 attribute if one exists, because this lets the compiler override
13302 the underlying type if needed. */
13303 if (type->target_type () != NULL && !type->target_type ()->is_stub ())
13304 {
13305 struct type *underlying_type = type->target_type ();
13306 underlying_type = check_typedef (underlying_type);
13307
13308 type->set_is_unsigned (underlying_type->is_unsigned ());
13309
13310 if (type->length () == 0)
13311 type->set_length (underlying_type->length ());
13312
13313 if (TYPE_RAW_ALIGN (type) == 0
13314 && TYPE_RAW_ALIGN (underlying_type) != 0)
13315 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
13316 }
13317
13318 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
13319
13320 set_die_type (die, type, cu);
13321
13322 /* Finish the creation of this type by using the enum's children.
13323 Note that, as usual, this must come after set_die_type to avoid
13324 infinite recursion when trying to compute the names of the
13325 enumerators. */
13326 update_enumeration_type_from_children (die, type, cu);
13327
13328 return type;
13329 }
13330
13331 /* Given a pointer to a die which begins an enumeration, process all
13332 the dies that define the members of the enumeration, and create the
13333 symbol for the enumeration type.
13334
13335 NOTE: We reverse the order of the element list. */
13336
13337 static void
13338 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13339 {
13340 struct type *this_type;
13341
13342 this_type = get_die_type (die, cu);
13343 if (this_type == NULL)
13344 this_type = read_enumeration_type (die, cu);
13345
13346 if (die->child != NULL)
13347 {
13348 struct die_info *child_die;
13349 const char *name;
13350
13351 child_die = die->child;
13352 while (child_die && child_die->tag)
13353 {
13354 if (child_die->tag != DW_TAG_enumerator)
13355 {
13356 process_die (child_die, cu);
13357 }
13358 else
13359 {
13360 name = dwarf2_name (child_die, cu);
13361 if (name)
13362 new_symbol (child_die, this_type, cu);
13363 }
13364
13365 child_die = child_die->sibling;
13366 }
13367 }
13368
13369 /* If we are reading an enum from a .debug_types unit, and the enum
13370 is a declaration, and the enum is not the signatured type in the
13371 unit, then we do not want to add a symbol for it. Adding a
13372 symbol would in some cases obscure the true definition of the
13373 enum, giving users an incomplete type when the definition is
13374 actually available. Note that we do not want to do this for all
13375 enums which are just declarations, because C++0x allows forward
13376 enum declarations. */
13377 if (cu->per_cu->is_debug_types
13378 && die_is_declaration (die, cu))
13379 {
13380 struct signatured_type *sig_type;
13381
13382 sig_type = (struct signatured_type *) cu->per_cu;
13383 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
13384 if (sig_type->type_offset_in_section != die->sect_off)
13385 return;
13386 }
13387
13388 new_symbol (die, this_type, cu);
13389 }
13390
13391 /* Helper function for quirk_ada_thick_pointer that examines a bounds
13392 expression for an index type and finds the corresponding field
13393 offset in the hidden "P_BOUNDS" structure. Returns true on success
13394 and updates *FIELD, false if it fails to recognize an
13395 expression. */
13396
13397 static bool
13398 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
13399 int *bounds_offset, struct field *field,
13400 struct dwarf2_cu *cu)
13401 {
13402 struct attribute *attr = dwarf2_attr (die, name, cu);
13403 if (attr == nullptr || !attr->form_is_block ())
13404 return false;
13405
13406 const struct dwarf_block *block = attr->as_block ();
13407 const gdb_byte *start = block->data;
13408 const gdb_byte *end = block->data + block->size;
13409
13410 /* The expression to recognize generally looks like:
13411
13412 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13413 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13414
13415 However, the second "plus_uconst" may be missing:
13416
13417 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13418 DW_OP_deref_size: 4)
13419
13420 This happens when the field is at the start of the structure.
13421
13422 Also, the final deref may not be sized:
13423
13424 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
13425 DW_OP_deref)
13426
13427 This happens when the size of the index type happens to be the
13428 same as the architecture's word size. This can occur with or
13429 without the second plus_uconst. */
13430
13431 if (end - start < 2)
13432 return false;
13433 if (*start++ != DW_OP_push_object_address)
13434 return false;
13435 if (*start++ != DW_OP_plus_uconst)
13436 return false;
13437
13438 uint64_t this_bound_off;
13439 start = gdb_read_uleb128 (start, end, &this_bound_off);
13440 if (start == nullptr || (int) this_bound_off != this_bound_off)
13441 return false;
13442 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
13443 is consistent among all bounds. */
13444 if (*bounds_offset == -1)
13445 *bounds_offset = this_bound_off;
13446 else if (*bounds_offset != this_bound_off)
13447 return false;
13448
13449 if (start == end || *start++ != DW_OP_deref)
13450 return false;
13451
13452 int offset = 0;
13453 if (start ==end)
13454 return false;
13455 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
13456 {
13457 /* This means an offset of 0. */
13458 }
13459 else if (*start++ != DW_OP_plus_uconst)
13460 return false;
13461 else
13462 {
13463 /* The size is the parameter to DW_OP_plus_uconst. */
13464 uint64_t val;
13465 start = gdb_read_uleb128 (start, end, &val);
13466 if (start == nullptr)
13467 return false;
13468 if ((int) val != val)
13469 return false;
13470 offset = val;
13471 }
13472
13473 if (start == end)
13474 return false;
13475
13476 uint64_t size;
13477 if (*start == DW_OP_deref_size)
13478 {
13479 start = gdb_read_uleb128 (start + 1, end, &size);
13480 if (start == nullptr)
13481 return false;
13482 }
13483 else if (*start == DW_OP_deref)
13484 {
13485 size = cu->header.addr_size;
13486 ++start;
13487 }
13488 else
13489 return false;
13490
13491 field->set_loc_bitpos (8 * offset);
13492 if (size != field->type ()->length ())
13493 field->set_bitsize (8 * size);
13494
13495 return true;
13496 }
13497
13498 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
13499 some kinds of Ada arrays:
13500
13501 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
13502 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
13503 <11e0> DW_AT_data_location: 2 byte block: 97 6
13504 (DW_OP_push_object_address; DW_OP_deref)
13505 <11e3> DW_AT_type : <0x1173>
13506 <11e7> DW_AT_sibling : <0x1201>
13507 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
13508 <11ec> DW_AT_type : <0x1206>
13509 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
13510 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13511 DW_OP_deref_size: 4)
13512 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
13513 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13514 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13515
13516 This actually represents a "thick pointer", which is a structure
13517 with two elements: one that is a pointer to the array data, and one
13518 that is a pointer to another structure; this second structure holds
13519 the array bounds.
13520
13521 This returns a new type on success, or nullptr if this didn't
13522 recognize the type. */
13523
13524 static struct type *
13525 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
13526 struct type *type)
13527 {
13528 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
13529 /* So far we've only seen this with block form. */
13530 if (attr == nullptr || !attr->form_is_block ())
13531 return nullptr;
13532
13533 /* Note that this will fail if the structure layout is changed by
13534 the compiler. However, we have no good way to recognize some
13535 other layout, because we don't know what expression the compiler
13536 might choose to emit should this happen. */
13537 struct dwarf_block *blk = attr->as_block ();
13538 if (blk->size != 2
13539 || blk->data[0] != DW_OP_push_object_address
13540 || blk->data[1] != DW_OP_deref)
13541 return nullptr;
13542
13543 int bounds_offset = -1;
13544 int max_align = -1;
13545 std::vector<struct field> range_fields;
13546 for (struct die_info *child_die = die->child;
13547 child_die;
13548 child_die = child_die->sibling)
13549 {
13550 if (child_die->tag == DW_TAG_subrange_type)
13551 {
13552 struct type *underlying = read_subrange_index_type (child_die, cu);
13553
13554 int this_align = type_align (underlying);
13555 if (this_align > max_align)
13556 max_align = this_align;
13557
13558 range_fields.emplace_back ();
13559 range_fields.emplace_back ();
13560
13561 struct field &lower = range_fields[range_fields.size () - 2];
13562 struct field &upper = range_fields[range_fields.size () - 1];
13563
13564 lower.set_type (underlying);
13565 lower.set_is_artificial (true);
13566
13567 upper.set_type (underlying);
13568 upper.set_is_artificial (true);
13569
13570 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
13571 &bounds_offset, &lower, cu)
13572 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
13573 &bounds_offset, &upper, cu))
13574 return nullptr;
13575 }
13576 }
13577
13578 /* This shouldn't really happen, but double-check that we found
13579 where the bounds are stored. */
13580 if (bounds_offset == -1)
13581 return nullptr;
13582
13583 struct objfile *objfile = cu->per_objfile->objfile;
13584 for (int i = 0; i < range_fields.size (); i += 2)
13585 {
13586 char name[20];
13587
13588 /* Set the name of each field in the bounds. */
13589 xsnprintf (name, sizeof (name), "LB%d", i / 2);
13590 range_fields[i].set_name (objfile->intern (name));
13591 xsnprintf (name, sizeof (name), "UB%d", i / 2);
13592 range_fields[i + 1].set_name (objfile->intern (name));
13593 }
13594
13595 type_allocator alloc (objfile, cu->lang ());
13596 struct type *bounds = alloc.new_type ();
13597 bounds->set_code (TYPE_CODE_STRUCT);
13598
13599 bounds->copy_fields (range_fields);
13600
13601 int last_fieldno = range_fields.size () - 1;
13602 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
13603 + bounds->field (last_fieldno).type ()->length ());
13604 bounds->set_length (align_up (bounds_size, max_align));
13605
13606 /* Rewrite the existing array type in place. Specifically, we
13607 remove any dynamic properties we might have read, and we replace
13608 the index types. */
13609 struct type *iter = type;
13610 for (int i = 0; i < range_fields.size (); i += 2)
13611 {
13612 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
13613 iter->main_type->dyn_prop_list = nullptr;
13614 iter->set_index_type
13615 (create_static_range_type (alloc, bounds->field (i).type (), 1, 0));
13616 iter = iter->target_type ();
13617 }
13618
13619 struct type *result = type_allocator (objfile, cu->lang ()).new_type ();
13620 result->set_code (TYPE_CODE_STRUCT);
13621
13622 result->alloc_fields (2);
13623
13624 /* The names are chosen to coincide with what the compiler does with
13625 -fgnat-encodings=all, which the Ada code in gdb already
13626 understands. */
13627 result->field (0).set_name ("P_ARRAY");
13628 result->field (0).set_type (lookup_pointer_type (type));
13629
13630 result->field (1).set_name ("P_BOUNDS");
13631 result->field (1).set_type (lookup_pointer_type (bounds));
13632 result->field (1).set_loc_bitpos (8 * bounds_offset);
13633
13634 result->set_name (type->name ());
13635 result->set_length (result->field (0).type ()->length ()
13636 + result->field (1).type ()->length ());
13637
13638 return result;
13639 }
13640
13641 /* Extract all information from a DW_TAG_array_type DIE and put it in
13642 the DIE's type field. For now, this only handles one dimensional
13643 arrays. */
13644
13645 static struct type *
13646 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13647 {
13648 struct objfile *objfile = cu->per_objfile->objfile;
13649 struct die_info *child_die;
13650 struct type *type;
13651 struct type *element_type, *range_type, *index_type;
13652 struct attribute *attr;
13653 const char *name;
13654 struct dynamic_prop *byte_stride_prop = NULL;
13655 unsigned int bit_stride = 0;
13656
13657 element_type = die_type (die, cu);
13658
13659 /* The die_type call above may have already set the type for this DIE. */
13660 type = get_die_type (die, cu);
13661 if (type)
13662 return type;
13663
13664 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13665 if (attr != NULL)
13666 {
13667 int stride_ok;
13668 struct type *prop_type = cu->addr_sized_int_type (false);
13669
13670 byte_stride_prop
13671 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
13672 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
13673 prop_type);
13674 if (!stride_ok)
13675 {
13676 complaint (_("unable to read array DW_AT_byte_stride "
13677 " - DIE at %s [in module %s]"),
13678 sect_offset_str (die->sect_off),
13679 objfile_name (cu->per_objfile->objfile));
13680 /* Ignore this attribute. We will likely not be able to print
13681 arrays of this type correctly, but there is little we can do
13682 to help if we cannot read the attribute's value. */
13683 byte_stride_prop = NULL;
13684 }
13685 }
13686
13687 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13688 if (attr != NULL)
13689 bit_stride = attr->constant_value (0);
13690
13691 /* Irix 6.2 native cc creates array types without children for
13692 arrays with unspecified length. */
13693 if (die->child == NULL)
13694 {
13695 index_type = builtin_type (objfile)->builtin_int;
13696 type_allocator alloc (objfile, cu->lang ());
13697 range_type = create_static_range_type (alloc, index_type, 0, -1);
13698 type = create_array_type_with_stride (alloc, element_type, range_type,
13699 byte_stride_prop, bit_stride);
13700 return set_die_type (die, type, cu);
13701 }
13702
13703 std::vector<struct type *> range_types;
13704 child_die = die->child;
13705 while (child_die && child_die->tag)
13706 {
13707 if (child_die->tag == DW_TAG_subrange_type
13708 || child_die->tag == DW_TAG_generic_subrange)
13709 {
13710 struct type *child_type = read_type_die (child_die, cu);
13711
13712 if (child_type != NULL)
13713 {
13714 /* The range type was successfully read. Save it for the
13715 array type creation. */
13716 range_types.push_back (child_type);
13717 }
13718 }
13719 child_die = child_die->sibling;
13720 }
13721
13722 if (range_types.empty ())
13723 {
13724 complaint (_("unable to find array range - DIE at %s [in module %s]"),
13725 sect_offset_str (die->sect_off),
13726 objfile_name (cu->per_objfile->objfile));
13727 return NULL;
13728 }
13729
13730 /* Dwarf2 dimensions are output from left to right, create the
13731 necessary array types in backwards order. */
13732
13733 type = element_type;
13734
13735 type_allocator alloc (cu->per_objfile->objfile, cu->lang ());
13736 if (read_array_order (die, cu) == DW_ORD_col_major)
13737 {
13738 int i = 0;
13739
13740 while (i < range_types.size ())
13741 {
13742 type = create_array_type_with_stride (alloc, type, range_types[i++],
13743 byte_stride_prop, bit_stride);
13744 type->set_is_multi_dimensional (true);
13745 bit_stride = 0;
13746 byte_stride_prop = nullptr;
13747 }
13748 }
13749 else
13750 {
13751 size_t ndim = range_types.size ();
13752 while (ndim-- > 0)
13753 {
13754 type = create_array_type_with_stride (alloc, type, range_types[ndim],
13755 byte_stride_prop, bit_stride);
13756 type->set_is_multi_dimensional (true);
13757 bit_stride = 0;
13758 byte_stride_prop = nullptr;
13759 }
13760 }
13761
13762 /* Clear the flag on the outermost array type. */
13763 type->set_is_multi_dimensional (false);
13764 gdb_assert (type != element_type);
13765
13766 /* Understand Dwarf2 support for vector types (like they occur on
13767 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13768 array type. This is not part of the Dwarf2/3 standard yet, but a
13769 custom vendor extension. The main difference between a regular
13770 array and the vector variant is that vectors are passed by value
13771 to functions. */
13772 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13773 if (attr != nullptr)
13774 make_vector_type (type);
13775
13776 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13777 implementation may choose to implement triple vectors using this
13778 attribute. */
13779 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13780 if (attr != nullptr && attr->form_is_unsigned ())
13781 {
13782 if (attr->as_unsigned () >= type->length ())
13783 type->set_length (attr->as_unsigned ());
13784 else
13785 complaint (_("DW_AT_byte_size for array type smaller "
13786 "than the total size of elements"));
13787 }
13788
13789 name = dwarf2_name (die, cu);
13790 if (name)
13791 type->set_name (name);
13792
13793 maybe_set_alignment (cu, die, type);
13794
13795 struct type *replacement_type = nullptr;
13796 if (cu->lang () == language_ada)
13797 {
13798 replacement_type = quirk_ada_thick_pointer (die, cu, type);
13799 if (replacement_type != nullptr)
13800 type = replacement_type;
13801 }
13802
13803 /* Install the type in the die. */
13804 set_die_type (die, type, cu, replacement_type != nullptr);
13805
13806 /* set_die_type should be already done. */
13807 set_descriptive_type (type, die, cu);
13808
13809 return type;
13810 }
13811
13812 static enum dwarf_array_dim_ordering
13813 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13814 {
13815 struct attribute *attr;
13816
13817 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13818
13819 if (attr != nullptr)
13820 {
13821 LONGEST val = attr->constant_value (-1);
13822 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
13823 return (enum dwarf_array_dim_ordering) val;
13824 }
13825
13826 /* GNU F77 is a special case, as at 08/2004 array type info is the
13827 opposite order to the dwarf2 specification, but data is still
13828 laid out as per normal fortran.
13829
13830 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13831 version checking. */
13832
13833 if (cu->lang () == language_fortran
13834 && cu->producer && strstr (cu->producer, "GNU F77"))
13835 {
13836 return DW_ORD_row_major;
13837 }
13838
13839 switch (cu->language_defn->array_ordering ())
13840 {
13841 case array_column_major:
13842 return DW_ORD_col_major;
13843 case array_row_major:
13844 default:
13845 return DW_ORD_row_major;
13846 };
13847 }
13848
13849 /* Extract all information from a DW_TAG_set_type DIE and put it in
13850 the DIE's type field. */
13851
13852 static struct type *
13853 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13854 {
13855 struct type *domain_type, *set_type;
13856 struct attribute *attr;
13857
13858 domain_type = die_type (die, cu);
13859
13860 /* The die_type call above may have already set the type for this DIE. */
13861 set_type = get_die_type (die, cu);
13862 if (set_type)
13863 return set_type;
13864
13865 type_allocator alloc (cu->per_objfile->objfile, cu->lang ());
13866 set_type = create_set_type (alloc, domain_type);
13867
13868 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13869 if (attr != nullptr && attr->form_is_unsigned ())
13870 set_type->set_length (attr->as_unsigned ());
13871
13872 maybe_set_alignment (cu, die, set_type);
13873
13874 return set_die_type (die, set_type, cu);
13875 }
13876
13877 /* A helper for read_common_block that creates a locexpr baton.
13878 SYM is the symbol which we are marking as computed.
13879 COMMON_DIE is the DIE for the common block.
13880 COMMON_LOC is the location expression attribute for the common
13881 block itself.
13882 MEMBER_LOC is the location expression attribute for the particular
13883 member of the common block that we are processing.
13884 CU is the CU from which the above come. */
13885
13886 static void
13887 mark_common_block_symbol_computed (struct symbol *sym,
13888 struct die_info *common_die,
13889 struct attribute *common_loc,
13890 struct attribute *member_loc,
13891 struct dwarf2_cu *cu)
13892 {
13893 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13894 struct objfile *objfile = per_objfile->objfile;
13895 struct dwarf2_locexpr_baton *baton;
13896 gdb_byte *ptr;
13897 unsigned int cu_off;
13898 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
13899 LONGEST offset = 0;
13900
13901 gdb_assert (common_loc && member_loc);
13902 gdb_assert (common_loc->form_is_block ());
13903 gdb_assert (member_loc->form_is_block ()
13904 || member_loc->form_is_constant ());
13905
13906 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13907 baton->per_objfile = per_objfile;
13908 baton->per_cu = cu->per_cu;
13909 gdb_assert (baton->per_cu);
13910
13911 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13912
13913 if (member_loc->form_is_constant ())
13914 {
13915 offset = member_loc->constant_value (0);
13916 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13917 }
13918 else
13919 baton->size += member_loc->as_block ()->size;
13920
13921 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
13922 baton->data = ptr;
13923
13924 *ptr++ = DW_OP_call4;
13925 cu_off = common_die->sect_off - cu->per_cu->sect_off;
13926 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13927 ptr += 4;
13928
13929 if (member_loc->form_is_constant ())
13930 {
13931 *ptr++ = DW_OP_addr;
13932 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13933 ptr += cu->header.addr_size;
13934 }
13935 else
13936 {
13937 /* We have to copy the data here, because DW_OP_call4 will only
13938 use a DW_AT_location attribute. */
13939 struct dwarf_block *block = member_loc->as_block ();
13940 memcpy (ptr, block->data, block->size);
13941 ptr += block->size;
13942 }
13943
13944 *ptr++ = DW_OP_plus;
13945 gdb_assert (ptr - baton->data == baton->size);
13946
13947 SYMBOL_LOCATION_BATON (sym) = baton;
13948 sym->set_aclass_index (dwarf2_locexpr_index);
13949 }
13950
13951 /* Create appropriate locally-scoped variables for all the
13952 DW_TAG_common_block entries. Also create a struct common_block
13953 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13954 is used to separate the common blocks name namespace from regular
13955 variable names. */
13956
13957 static void
13958 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13959 {
13960 struct attribute *attr;
13961
13962 attr = dwarf2_attr (die, DW_AT_location, cu);
13963 if (attr != nullptr)
13964 {
13965 /* Support the .debug_loc offsets. */
13966 if (attr->form_is_block ())
13967 {
13968 /* Ok. */
13969 }
13970 else if (attr->form_is_section_offset ())
13971 {
13972 dwarf2_complex_location_expr_complaint ();
13973 attr = NULL;
13974 }
13975 else
13976 {
13977 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13978 "common block member");
13979 attr = NULL;
13980 }
13981 }
13982
13983 if (die->child != NULL)
13984 {
13985 struct objfile *objfile = cu->per_objfile->objfile;
13986 struct die_info *child_die;
13987 size_t n_entries = 0, size;
13988 struct common_block *common_block;
13989 struct symbol *sym;
13990
13991 for (child_die = die->child;
13992 child_die && child_die->tag;
13993 child_die = child_die->sibling)
13994 ++n_entries;
13995
13996 size = (sizeof (struct common_block)
13997 + (n_entries - 1) * sizeof (struct symbol *));
13998 common_block
13999 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14000 size);
14001 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14002 common_block->n_entries = 0;
14003
14004 for (child_die = die->child;
14005 child_die && child_die->tag;
14006 child_die = child_die->sibling)
14007 {
14008 /* Create the symbol in the DW_TAG_common_block block in the current
14009 symbol scope. */
14010 sym = new_symbol (child_die, NULL, cu);
14011 if (sym != NULL)
14012 {
14013 struct attribute *member_loc;
14014
14015 common_block->contents[common_block->n_entries++] = sym;
14016
14017 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14018 cu);
14019 if (member_loc)
14020 {
14021 /* GDB has handled this for a long time, but it is
14022 not specified by DWARF. It seems to have been
14023 emitted by gfortran at least as recently as:
14024 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14025 complaint (_("Variable in common block has "
14026 "DW_AT_data_member_location "
14027 "- DIE at %s [in module %s]"),
14028 sect_offset_str (child_die->sect_off),
14029 objfile_name (objfile));
14030
14031 if (member_loc->form_is_section_offset ())
14032 dwarf2_complex_location_expr_complaint ();
14033 else if (member_loc->form_is_constant ()
14034 || member_loc->form_is_block ())
14035 {
14036 if (attr != nullptr)
14037 mark_common_block_symbol_computed (sym, die, attr,
14038 member_loc, cu);
14039 }
14040 else
14041 dwarf2_complex_location_expr_complaint ();
14042 }
14043 }
14044 }
14045
14046 sym = new_symbol (die, builtin_type (objfile)->builtin_void, cu);
14047 sym->set_value_common_block (common_block);
14048 }
14049 }
14050
14051 /* Create a type for a C++ namespace. */
14052
14053 static struct type *
14054 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14055 {
14056 struct objfile *objfile = cu->per_objfile->objfile;
14057 const char *previous_prefix, *name;
14058 int is_anonymous;
14059 struct type *type;
14060
14061 /* For extensions, reuse the type of the original namespace. */
14062 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14063 {
14064 struct die_info *ext_die;
14065 struct dwarf2_cu *ext_cu = cu;
14066
14067 ext_die = dwarf2_extension (die, &ext_cu);
14068 type = read_type_die (ext_die, ext_cu);
14069
14070 /* EXT_CU may not be the same as CU.
14071 Ensure TYPE is recorded with CU in die_type_hash. */
14072 return set_die_type (die, type, cu);
14073 }
14074
14075 name = namespace_name (die, &is_anonymous, cu);
14076
14077 /* Now build the name of the current namespace. */
14078
14079 previous_prefix = determine_prefix (die, cu);
14080 if (previous_prefix[0] != '\0')
14081 name = typename_concat (&objfile->objfile_obstack,
14082 previous_prefix, name, 0, cu);
14083
14084 /* Create the type. */
14085 type = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_NAMESPACE,
14086 0, name);
14087
14088 return set_die_type (die, type, cu);
14089 }
14090
14091 /* Read a namespace scope. */
14092
14093 static void
14094 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14095 {
14096 struct objfile *objfile = cu->per_objfile->objfile;
14097 int is_anonymous;
14098
14099 /* Add a symbol associated to this if we haven't seen the namespace
14100 before. Also, add a using directive if it's an anonymous
14101 namespace. */
14102
14103 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14104 {
14105 struct type *type;
14106
14107 type = read_type_die (die, cu);
14108 new_symbol (die, type, cu);
14109
14110 namespace_name (die, &is_anonymous, cu);
14111 if (is_anonymous)
14112 {
14113 const char *previous_prefix = determine_prefix (die, cu);
14114
14115 std::vector<const char *> excludes;
14116 add_using_directive (using_directives (cu),
14117 previous_prefix, type->name (), NULL,
14118 NULL, excludes,
14119 read_decl_line (die, cu),
14120 0, &objfile->objfile_obstack);
14121 }
14122 }
14123
14124 if (die->child != NULL)
14125 {
14126 struct die_info *child_die = die->child;
14127
14128 while (child_die && child_die->tag)
14129 {
14130 process_die (child_die, cu);
14131 child_die = child_die->sibling;
14132 }
14133 }
14134 }
14135
14136 /* Read a Fortran module as type. This DIE can be only a declaration used for
14137 imported module. Still we need that type as local Fortran "use ... only"
14138 declaration imports depend on the created type in determine_prefix. */
14139
14140 static struct type *
14141 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14142 {
14143 struct objfile *objfile = cu->per_objfile->objfile;
14144 const char *module_name;
14145 struct type *type;
14146
14147 module_name = dwarf2_name (die, cu);
14148 type = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_MODULE,
14149 0, module_name);
14150
14151 return set_die_type (die, type, cu);
14152 }
14153
14154 /* Read a Fortran module. */
14155
14156 static void
14157 read_module (struct die_info *die, struct dwarf2_cu *cu)
14158 {
14159 struct die_info *child_die = die->child;
14160 struct type *type;
14161
14162 type = read_type_die (die, cu);
14163 new_symbol (die, type, cu);
14164
14165 while (child_die && child_die->tag)
14166 {
14167 process_die (child_die, cu);
14168 child_die = child_die->sibling;
14169 }
14170 }
14171
14172 /* Return the name of the namespace represented by DIE. Set
14173 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14174 namespace. */
14175
14176 static const char *
14177 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14178 {
14179 struct die_info *current_die;
14180 const char *name = NULL;
14181
14182 /* Loop through the extensions until we find a name. */
14183
14184 for (current_die = die;
14185 current_die != NULL;
14186 current_die = dwarf2_extension (die, &cu))
14187 {
14188 /* We don't use dwarf2_name here so that we can detect the absence
14189 of a name -> anonymous namespace. */
14190 name = dwarf2_string_attr (die, DW_AT_name, cu);
14191
14192 if (name != NULL)
14193 break;
14194 }
14195
14196 /* Is it an anonymous namespace? */
14197
14198 *is_anonymous = (name == NULL);
14199 if (*is_anonymous)
14200 name = CP_ANONYMOUS_NAMESPACE_STR;
14201
14202 return name;
14203 }
14204
14205 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14206 the user defined type vector. */
14207
14208 static struct type *
14209 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14210 {
14211 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
14212 struct comp_unit_head *cu_header = &cu->header;
14213 struct type *type;
14214 struct attribute *attr_byte_size;
14215 struct attribute *attr_address_class;
14216 int byte_size, addr_class;
14217 struct type *target_type;
14218
14219 target_type = die_type (die, cu);
14220
14221 /* The die_type call above may have already set the type for this DIE. */
14222 type = get_die_type (die, cu);
14223 if (type)
14224 return type;
14225
14226 type = lookup_pointer_type (target_type);
14227
14228 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14229 if (attr_byte_size)
14230 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
14231 else
14232 byte_size = cu_header->addr_size;
14233
14234 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14235 if (attr_address_class)
14236 addr_class = attr_address_class->constant_value (DW_ADDR_none);
14237 else
14238 addr_class = DW_ADDR_none;
14239
14240 ULONGEST alignment = get_alignment (cu, die);
14241
14242 /* If the pointer size, alignment, or address class is different
14243 than the default, create a type variant marked as such and set
14244 the length accordingly. */
14245 if (type->length () != byte_size
14246 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
14247 && alignment != TYPE_RAW_ALIGN (type))
14248 || addr_class != DW_ADDR_none)
14249 {
14250 if (gdbarch_address_class_type_flags_p (gdbarch))
14251 {
14252 type_instance_flags type_flags
14253 = gdbarch_address_class_type_flags (gdbarch, byte_size,
14254 addr_class);
14255 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14256 == 0);
14257 type = make_type_with_address_space (type, type_flags);
14258 }
14259 else if (type->length () != byte_size)
14260 {
14261 complaint (_("invalid pointer size %d"), byte_size);
14262 }
14263 else if (TYPE_RAW_ALIGN (type) != alignment)
14264 {
14265 complaint (_("Invalid DW_AT_alignment"
14266 " - DIE at %s [in module %s]"),
14267 sect_offset_str (die->sect_off),
14268 objfile_name (cu->per_objfile->objfile));
14269 }
14270 else
14271 {
14272 /* Should we also complain about unhandled address classes? */
14273 }
14274 }
14275
14276 type->set_length (byte_size);
14277 set_type_align (type, alignment);
14278 return set_die_type (die, type, cu);
14279 }
14280
14281 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14282 the user defined type vector. */
14283
14284 static struct type *
14285 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14286 {
14287 struct type *type;
14288 struct type *to_type;
14289 struct type *domain;
14290
14291 to_type = die_type (die, cu);
14292 domain = die_containing_type (die, cu);
14293
14294 /* The calls above may have already set the type for this DIE. */
14295 type = get_die_type (die, cu);
14296 if (type)
14297 return type;
14298
14299 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
14300 type = lookup_methodptr_type (to_type);
14301 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
14302 {
14303 struct type *new_type
14304 = type_allocator (cu->per_objfile->objfile, cu->lang ()).new_type ();
14305
14306 smash_to_method_type (new_type, domain, to_type->target_type (),
14307 to_type->fields (), to_type->num_fields (),
14308 to_type->has_varargs ());
14309 type = lookup_methodptr_type (new_type);
14310 }
14311 else
14312 type = lookup_memberptr_type (to_type, domain);
14313
14314 return set_die_type (die, type, cu);
14315 }
14316
14317 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14318 the user defined type vector. */
14319
14320 static struct type *
14321 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14322 enum type_code refcode)
14323 {
14324 struct comp_unit_head *cu_header = &cu->header;
14325 struct type *type, *target_type;
14326 struct attribute *attr;
14327
14328 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14329
14330 target_type = die_type (die, cu);
14331
14332 /* The die_type call above may have already set the type for this DIE. */
14333 type = get_die_type (die, cu);
14334 if (type)
14335 return type;
14336
14337 type = lookup_reference_type (target_type, refcode);
14338 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14339 if (attr != nullptr)
14340 type->set_length (attr->constant_value (cu_header->addr_size));
14341 else
14342 type->set_length (cu_header->addr_size);
14343
14344 maybe_set_alignment (cu, die, type);
14345 return set_die_type (die, type, cu);
14346 }
14347
14348 /* Add the given cv-qualifiers to the element type of the array. GCC
14349 outputs DWARF type qualifiers that apply to an array, not the
14350 element type. But GDB relies on the array element type to carry
14351 the cv-qualifiers. This mimics section 6.7.3 of the C99
14352 specification. */
14353
14354 static struct type *
14355 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14356 struct type *base_type, int cnst, int voltl)
14357 {
14358 struct type *el_type, *inner_array;
14359
14360 base_type = copy_type (base_type);
14361 inner_array = base_type;
14362
14363 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
14364 {
14365 inner_array->set_target_type (copy_type (inner_array->target_type ()));
14366 inner_array = inner_array->target_type ();
14367 }
14368
14369 el_type = inner_array->target_type ();
14370 cnst |= TYPE_CONST (el_type);
14371 voltl |= TYPE_VOLATILE (el_type);
14372 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
14373
14374 return set_die_type (die, base_type, cu);
14375 }
14376
14377 static struct type *
14378 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14379 {
14380 struct type *base_type, *cv_type;
14381
14382 base_type = die_type (die, cu);
14383
14384 /* The die_type call above may have already set the type for this DIE. */
14385 cv_type = get_die_type (die, cu);
14386 if (cv_type)
14387 return cv_type;
14388
14389 /* In case the const qualifier is applied to an array type, the element type
14390 is so qualified, not the array type (section 6.7.3 of C99). */
14391 if (base_type->code () == TYPE_CODE_ARRAY)
14392 return add_array_cv_type (die, cu, base_type, 1, 0);
14393
14394 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14395 return set_die_type (die, cv_type, cu);
14396 }
14397
14398 static struct type *
14399 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14400 {
14401 struct type *base_type, *cv_type;
14402
14403 base_type = die_type (die, cu);
14404
14405 /* The die_type call above may have already set the type for this DIE. */
14406 cv_type = get_die_type (die, cu);
14407 if (cv_type)
14408 return cv_type;
14409
14410 /* In case the volatile qualifier is applied to an array type, the
14411 element type is so qualified, not the array type (section 6.7.3
14412 of C99). */
14413 if (base_type->code () == TYPE_CODE_ARRAY)
14414 return add_array_cv_type (die, cu, base_type, 0, 1);
14415
14416 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14417 return set_die_type (die, cv_type, cu);
14418 }
14419
14420 /* Handle DW_TAG_restrict_type. */
14421
14422 static struct type *
14423 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14424 {
14425 struct type *base_type, *cv_type;
14426
14427 base_type = die_type (die, cu);
14428
14429 /* The die_type call above may have already set the type for this DIE. */
14430 cv_type = get_die_type (die, cu);
14431 if (cv_type)
14432 return cv_type;
14433
14434 cv_type = make_restrict_type (base_type);
14435 return set_die_type (die, cv_type, cu);
14436 }
14437
14438 /* Handle DW_TAG_atomic_type. */
14439
14440 static struct type *
14441 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14442 {
14443 struct type *base_type, *cv_type;
14444
14445 base_type = die_type (die, cu);
14446
14447 /* The die_type call above may have already set the type for this DIE. */
14448 cv_type = get_die_type (die, cu);
14449 if (cv_type)
14450 return cv_type;
14451
14452 cv_type = make_atomic_type (base_type);
14453 return set_die_type (die, cv_type, cu);
14454 }
14455
14456 /* Extract all information from a DW_TAG_string_type DIE and add to
14457 the user defined type vector. It isn't really a user defined type,
14458 but it behaves like one, with other DIE's using an AT_user_def_type
14459 attribute to reference it. */
14460
14461 static struct type *
14462 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14463 {
14464 struct objfile *objfile = cu->per_objfile->objfile;
14465 struct gdbarch *gdbarch = objfile->arch ();
14466 struct type *type, *range_type, *index_type, *char_type;
14467 struct attribute *attr;
14468 struct dynamic_prop prop;
14469 bool length_is_constant = true;
14470 LONGEST length;
14471
14472 /* There are a couple of places where bit sizes might be made use of
14473 when parsing a DW_TAG_string_type, however, no producer that we know
14474 of make use of these. Handling bit sizes that are a multiple of the
14475 byte size is easy enough, but what about other bit sizes? Lets deal
14476 with that problem when we have to. Warn about these attributes being
14477 unsupported, then parse the type and ignore them like we always
14478 have. */
14479 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
14480 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
14481 {
14482 static bool warning_printed = false;
14483 if (!warning_printed)
14484 {
14485 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
14486 "currently supported on DW_TAG_string_type."));
14487 warning_printed = true;
14488 }
14489 }
14490
14491 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14492 if (attr != nullptr && !attr->form_is_constant ())
14493 {
14494 /* The string length describes the location at which the length of
14495 the string can be found. The size of the length field can be
14496 specified with one of the attributes below. */
14497 struct type *prop_type;
14498 struct attribute *len
14499 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
14500 if (len == nullptr)
14501 len = dwarf2_attr (die, DW_AT_byte_size, cu);
14502 if (len != nullptr && len->form_is_constant ())
14503 {
14504 /* Pass 0 as the default as we know this attribute is constant
14505 and the default value will not be returned. */
14506 LONGEST sz = len->constant_value (0);
14507 prop_type = objfile_int_type (objfile, sz, true);
14508 }
14509 else
14510 {
14511 /* If the size is not specified then we assume it is the size of
14512 an address on this target. */
14513 prop_type = cu->addr_sized_int_type (true);
14514 }
14515
14516 /* Convert the attribute into a dynamic property. */
14517 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
14518 length = 1;
14519 else
14520 length_is_constant = false;
14521 }
14522 else if (attr != nullptr)
14523 {
14524 /* This DW_AT_string_length just contains the length with no
14525 indirection. There's no need to create a dynamic property in this
14526 case. Pass 0 for the default value as we know it will not be
14527 returned in this case. */
14528 length = attr->constant_value (0);
14529 }
14530 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
14531 {
14532 /* We don't currently support non-constant byte sizes for strings. */
14533 length = attr->constant_value (1);
14534 }
14535 else
14536 {
14537 /* Use 1 as a fallback length if we have nothing else. */
14538 length = 1;
14539 }
14540
14541 index_type = builtin_type (objfile)->builtin_int;
14542 type_allocator alloc (objfile, cu->lang ());
14543 if (length_is_constant)
14544 range_type = create_static_range_type (alloc, index_type, 1, length);
14545 else
14546 {
14547 struct dynamic_prop low_bound;
14548
14549 low_bound.set_const_val (1);
14550 range_type = create_range_type (alloc, index_type, &low_bound, &prop, 0);
14551 }
14552 char_type = language_string_char_type (cu->language_defn, gdbarch);
14553 type = create_string_type (alloc, char_type, range_type);
14554
14555 return set_die_type (die, type, cu);
14556 }
14557
14558 /* Assuming that DIE corresponds to a function, returns nonzero
14559 if the function is prototyped. */
14560
14561 static int
14562 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14563 {
14564 struct attribute *attr;
14565
14566 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14567 if (attr && attr->as_boolean ())
14568 return 1;
14569
14570 /* The DWARF standard implies that the DW_AT_prototyped attribute
14571 is only meaningful for C, but the concept also extends to other
14572 languages that allow unprototyped functions (Eg: Objective C).
14573 For all other languages, assume that functions are always
14574 prototyped. */
14575 if (cu->lang () != language_c
14576 && cu->lang () != language_objc
14577 && cu->lang () != language_opencl)
14578 return 1;
14579
14580 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14581 prototyped and unprototyped functions; default to prototyped,
14582 since that is more common in modern code (and RealView warns
14583 about unprototyped functions). */
14584 if (producer_is_realview (cu->producer))
14585 return 1;
14586
14587 return 0;
14588 }
14589
14590 /* Handle DIES due to C code like:
14591
14592 struct foo
14593 {
14594 int (*funcp)(int a, long l);
14595 int b;
14596 };
14597
14598 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14599
14600 static struct type *
14601 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14602 {
14603 struct objfile *objfile = cu->per_objfile->objfile;
14604 struct type *type; /* Type that this function returns. */
14605 struct type *ftype; /* Function that returns above type. */
14606 struct attribute *attr;
14607
14608 type = die_type (die, cu);
14609
14610 if (type->code () == TYPE_CODE_VOID
14611 && !type->is_stub ()
14612 && die->child == nullptr
14613 && producer_is_gas_2_39 (cu))
14614 {
14615 /* Work around PR gas/29517, pretend we have an DW_TAG_unspecified_type
14616 return type. */
14617 type = (type_allocator (cu->per_objfile->objfile, cu->lang ())
14618 .new_type (TYPE_CODE_VOID, 0, nullptr));
14619 type->set_is_stub (true);
14620 }
14621
14622 /* The die_type call above may have already set the type for this DIE. */
14623 ftype = get_die_type (die, cu);
14624 if (ftype)
14625 return ftype;
14626
14627 ftype = lookup_function_type (type);
14628
14629 if (prototyped_function_p (die, cu))
14630 ftype->set_is_prototyped (true);
14631
14632 /* Store the calling convention in the type if it's available in
14633 the subroutine die. Otherwise set the calling convention to
14634 the default value DW_CC_normal. */
14635 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14636 if (attr != nullptr
14637 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
14638 TYPE_CALLING_CONVENTION (ftype)
14639 = (enum dwarf_calling_convention) attr->constant_value (0);
14640 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14641 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14642 else
14643 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14644
14645 /* Record whether the function returns normally to its caller or not
14646 if the DWARF producer set that information. */
14647 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14648 if (attr && attr->as_boolean ())
14649 TYPE_NO_RETURN (ftype) = 1;
14650
14651 /* We need to add the subroutine type to the die immediately so
14652 we don't infinitely recurse when dealing with parameters
14653 declared as the same subroutine type. */
14654 set_die_type (die, ftype, cu);
14655
14656 if (die->child != NULL)
14657 {
14658 struct type *void_type = builtin_type (objfile)->builtin_void;
14659 struct die_info *child_die;
14660 int nparams, iparams;
14661
14662 /* Count the number of parameters.
14663 FIXME: GDB currently ignores vararg functions, but knows about
14664 vararg member functions. */
14665 nparams = 0;
14666 child_die = die->child;
14667 while (child_die && child_die->tag)
14668 {
14669 if (child_die->tag == DW_TAG_formal_parameter)
14670 nparams++;
14671 else if (child_die->tag == DW_TAG_unspecified_parameters)
14672 ftype->set_has_varargs (true);
14673
14674 child_die = child_die->sibling;
14675 }
14676
14677 /* Allocate storage for parameters and fill them in. */
14678 ftype->alloc_fields (nparams);
14679
14680 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14681 even if we error out during the parameters reading below. */
14682 for (iparams = 0; iparams < nparams; iparams++)
14683 ftype->field (iparams).set_type (void_type);
14684
14685 iparams = 0;
14686 child_die = die->child;
14687 while (child_die && child_die->tag)
14688 {
14689 if (child_die->tag == DW_TAG_formal_parameter)
14690 {
14691 struct type *arg_type;
14692
14693 /* DWARF version 2 has no clean way to discern C++
14694 static and non-static member functions. G++ helps
14695 GDB by marking the first parameter for non-static
14696 member functions (which is the this pointer) as
14697 artificial. We pass this information to
14698 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14699
14700 DWARF version 3 added DW_AT_object_pointer, which GCC
14701 4.5 does not yet generate. */
14702 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14703 if (attr != nullptr)
14704 ftype->field (iparams).set_is_artificial (attr->as_boolean ());
14705 else
14706 ftype->field (iparams).set_is_artificial (false);
14707 arg_type = die_type (child_die, cu);
14708
14709 /* RealView does not mark THIS as const, which the testsuite
14710 expects. GCC marks THIS as const in method definitions,
14711 but not in the class specifications (GCC PR 43053). */
14712 if (cu->lang () == language_cplus
14713 && !TYPE_CONST (arg_type)
14714 && ftype->field (iparams).is_artificial ())
14715 {
14716 int is_this = 0;
14717 struct dwarf2_cu *arg_cu = cu;
14718 const char *name = dwarf2_name (child_die, cu);
14719
14720 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14721 if (attr != nullptr)
14722 {
14723 /* If the compiler emits this, use it. */
14724 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14725 is_this = 1;
14726 }
14727 else if (name && strcmp (name, "this") == 0)
14728 /* Function definitions will have the argument names. */
14729 is_this = 1;
14730 else if (name == NULL && iparams == 0)
14731 /* Declarations may not have the names, so like
14732 elsewhere in GDB, assume an artificial first
14733 argument is "this". */
14734 is_this = 1;
14735
14736 if (is_this)
14737 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14738 arg_type, 0);
14739 }
14740
14741 ftype->field (iparams).set_type (arg_type);
14742 iparams++;
14743 }
14744 child_die = child_die->sibling;
14745 }
14746 }
14747
14748 return ftype;
14749 }
14750
14751 static struct type *
14752 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14753 {
14754 struct objfile *objfile = cu->per_objfile->objfile;
14755 const char *name = dwarf2_full_name (NULL, die, cu);
14756 struct type *this_type;
14757 struct gdbarch *gdbarch = objfile->arch ();
14758 struct type *target_type = die_type (die, cu);
14759
14760 if (gdbarch_dwarf2_omit_typedef_p (gdbarch, target_type, cu->producer, name))
14761 {
14762 /* The long double is defined as a base type in C. GCC creates a long
14763 double typedef with target-type _Float128 for the long double to
14764 identify it as the IEEE Float128 value. This is a GCC hack since the
14765 DWARF doesn't distinguish between the IBM long double and IEEE
14766 128-bit float. Replace the GCC workaround for the long double
14767 typedef with the actual type information copied from the target-type
14768 with the correct long double base type name. */
14769 this_type = copy_type (target_type);
14770 this_type->set_name (name);
14771 set_die_type (die, this_type, cu);
14772 return this_type;
14773 }
14774
14775 type_allocator alloc (objfile, cu->lang ());
14776 this_type = alloc.new_type (TYPE_CODE_TYPEDEF, 0, name);
14777 this_type->set_target_is_stub (true);
14778 set_die_type (die, this_type, cu);
14779 if (target_type != this_type)
14780 this_type->set_target_type (target_type);
14781 else
14782 {
14783 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14784 spec and cause infinite loops in GDB. */
14785 complaint (_("Self-referential DW_TAG_typedef "
14786 "- DIE at %s [in module %s]"),
14787 sect_offset_str (die->sect_off), objfile_name (objfile));
14788 this_type->set_target_type (nullptr);
14789 }
14790 if (name == NULL)
14791 {
14792 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
14793 anonymous typedefs, which is, strictly speaking, invalid DWARF.
14794 Handle these by just returning the target type, rather than
14795 constructing an anonymous typedef type and trying to handle this
14796 elsewhere. */
14797 set_die_type (die, target_type, cu);
14798 return target_type;
14799 }
14800 return this_type;
14801 }
14802
14803 /* Helper for get_dwarf2_rational_constant that computes the value of
14804 a given gmp_mpz given an attribute. */
14805
14806 static void
14807 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
14808 {
14809 /* GCC will sometimes emit a 16-byte constant value as a DWARF
14810 location expression that pushes an implicit value. */
14811 if (attr->form == DW_FORM_exprloc)
14812 {
14813 dwarf_block *blk = attr->as_block ();
14814 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
14815 {
14816 uint64_t len;
14817 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
14818 blk->data + blk->size,
14819 &len);
14820 if (ptr - blk->data + len <= blk->size)
14821 {
14822 value->read (gdb::make_array_view (ptr, len),
14823 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14824 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14825 true);
14826 return;
14827 }
14828 }
14829
14830 /* On failure set it to 1. */
14831 *value = gdb_mpz (1);
14832 }
14833 else if (attr->form_is_block ())
14834 {
14835 dwarf_block *blk = attr->as_block ();
14836 value->read (gdb::make_array_view (blk->data, blk->size),
14837 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14838 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14839 true);
14840 }
14841 else if (attr->form_is_unsigned ())
14842 *value = gdb_mpz (attr->as_unsigned ());
14843 else
14844 *value = gdb_mpz (attr->constant_value (1));
14845 }
14846
14847 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
14848 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
14849
14850 If the numerator and/or numerator attribute is missing,
14851 a complaint is filed, and NUMERATOR and DENOMINATOR are left
14852 untouched. */
14853
14854 static void
14855 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
14856 gdb_mpz *numerator, gdb_mpz *denominator)
14857 {
14858 struct attribute *num_attr, *denom_attr;
14859
14860 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
14861 if (num_attr == nullptr)
14862 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
14863 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14864
14865 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
14866 if (denom_attr == nullptr)
14867 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
14868 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14869
14870 if (num_attr == nullptr || denom_attr == nullptr)
14871 return;
14872
14873 get_mpz (cu, numerator, num_attr);
14874 get_mpz (cu, denominator, denom_attr);
14875 }
14876
14877 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
14878 rational constant, rather than a signed one.
14879
14880 If the rational constant has a negative value, a complaint
14881 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
14882
14883 static void
14884 get_dwarf2_unsigned_rational_constant (struct die_info *die,
14885 struct dwarf2_cu *cu,
14886 gdb_mpz *numerator,
14887 gdb_mpz *denominator)
14888 {
14889 gdb_mpz num (1);
14890 gdb_mpz denom (1);
14891
14892 get_dwarf2_rational_constant (die, cu, &num, &denom);
14893 if (num < 0 && denom < 0)
14894 {
14895 num.negate ();
14896 denom.negate ();
14897 }
14898 else if (num < 0)
14899 {
14900 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
14901 " in DIE at %s"),
14902 sect_offset_str (die->sect_off));
14903 return;
14904 }
14905 else if (denom < 0)
14906 {
14907 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
14908 " in DIE at %s"),
14909 sect_offset_str (die->sect_off));
14910 return;
14911 }
14912
14913 *numerator = std::move (num);
14914 *denominator = std::move (denom);
14915 }
14916
14917 /* Assuming that ENCODING is a string whose contents starting at the
14918 K'th character is "_nn" where "nn" is a decimal number, scan that
14919 number and set RESULT to the value. K is updated to point to the
14920 character immediately following the number.
14921
14922 If the string does not conform to the format described above, false
14923 is returned, and K may or may not be changed. */
14924
14925 static bool
14926 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
14927 {
14928 /* The next character should be an underscore ('_') followed
14929 by a digit. */
14930 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
14931 return false;
14932
14933 /* Skip the underscore. */
14934 k++;
14935 int start = k;
14936
14937 /* Determine the number of digits for our number. */
14938 while (isdigit (encoding[k]))
14939 k++;
14940 if (k == start)
14941 return false;
14942
14943 std::string copy (&encoding[start], k - start);
14944 return result->set (copy.c_str (), 10);
14945 }
14946
14947 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
14948 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
14949 DENOM, update OFFSET, and return true on success. Return false on
14950 failure. */
14951
14952 static bool
14953 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
14954 gdb_mpz *num, gdb_mpz *denom)
14955 {
14956 if (!ada_get_gnat_encoded_number (encoding, offset, num))
14957 return false;
14958 return ada_get_gnat_encoded_number (encoding, offset, denom);
14959 }
14960
14961 /* Assuming DIE corresponds to a fixed point type, finish the creation
14962 of the corresponding TYPE by setting its type-specific data. CU is
14963 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
14964 encodings. It is nullptr if the GNAT encoding should be
14965 ignored. */
14966
14967 static void
14968 finish_fixed_point_type (struct type *type, const char *suffix,
14969 struct die_info *die, struct dwarf2_cu *cu)
14970 {
14971 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
14972 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
14973
14974 /* If GNAT encodings are preferred, don't examine the
14975 attributes. */
14976 struct attribute *attr = nullptr;
14977 if (suffix == nullptr)
14978 {
14979 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
14980 if (attr == nullptr)
14981 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
14982 if (attr == nullptr)
14983 attr = dwarf2_attr (die, DW_AT_small, cu);
14984 }
14985
14986 /* Numerator and denominator of our fixed-point type's scaling factor.
14987 The default is a scaling factor of 1, which we use as a fallback
14988 when we are not able to decode it (problem with the debugging info,
14989 unsupported forms, bug in GDB, etc...). Using that as the default
14990 allows us to at least print the unscaled value, which might still
14991 be useful to a user. */
14992 gdb_mpz scale_num (1);
14993 gdb_mpz scale_denom (1);
14994
14995 if (attr == nullptr)
14996 {
14997 int offset = 0;
14998 if (suffix != nullptr
14999 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
15000 &scale_denom)
15001 /* The number might be encoded as _nn_dd_nn_dd, where the
15002 second ratio is the 'small value. In this situation, we
15003 want the second value. */
15004 && (suffix[offset] != '_'
15005 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
15006 &scale_denom)))
15007 {
15008 /* Found it. */
15009 }
15010 else
15011 {
15012 /* Scaling factor not found. Assume a scaling factor of 1,
15013 and hope for the best. At least the user will be able to
15014 see the encoded value. */
15015 scale_num = 1;
15016 scale_denom = 1;
15017 complaint (_("no scale found for fixed-point type (DIE at %s)"),
15018 sect_offset_str (die->sect_off));
15019 }
15020 }
15021 else if (attr->name == DW_AT_binary_scale)
15022 {
15023 LONGEST scale_exp = attr->constant_value (0);
15024 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
15025
15026 num_or_denom <<= std::abs (scale_exp);
15027 }
15028 else if (attr->name == DW_AT_decimal_scale)
15029 {
15030 LONGEST scale_exp = attr->constant_value (0);
15031 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
15032
15033 num_or_denom = gdb_mpz::pow (10, std::abs (scale_exp));
15034 }
15035 else if (attr->name == DW_AT_small)
15036 {
15037 struct die_info *scale_die;
15038 struct dwarf2_cu *scale_cu = cu;
15039
15040 scale_die = follow_die_ref (die, attr, &scale_cu);
15041 if (scale_die->tag == DW_TAG_constant)
15042 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
15043 &scale_num, &scale_denom);
15044 else
15045 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
15046 " (DIE at %s)"),
15047 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
15048 }
15049 else
15050 {
15051 complaint (_("unsupported scale attribute %s for fixed-point type"
15052 " (DIE at %s)"),
15053 dwarf_attr_name (attr->name),
15054 sect_offset_str (die->sect_off));
15055 }
15056
15057 type->fixed_point_info ().scaling_factor = gdb_mpq (scale_num, scale_denom);
15058 }
15059
15060 /* The gnat-encoding suffix for fixed point. */
15061
15062 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
15063
15064 /* If NAME encodes an Ada fixed-point type, return a pointer to the
15065 "XF" suffix of the name. The text after this is what encodes the
15066 'small and 'delta information. Otherwise, return nullptr. */
15067
15068 static const char *
15069 gnat_encoded_fixed_point_type_info (const char *name)
15070 {
15071 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
15072 }
15073
15074 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15075 (which may be different from NAME) to the architecture back-end to allow
15076 it to guess the correct format if necessary. */
15077
15078 static struct type *
15079 dwarf2_init_float_type (struct dwarf2_cu *cu, int bits, const char *name,
15080 const char *name_hint, enum bfd_endian byte_order)
15081 {
15082 struct objfile *objfile = cu->per_objfile->objfile;
15083 struct gdbarch *gdbarch = objfile->arch ();
15084 const struct floatformat **format;
15085 struct type *type;
15086
15087 type_allocator alloc (objfile, cu->lang ());
15088 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15089 if (format)
15090 type = init_float_type (alloc, bits, name, format, byte_order);
15091 else
15092 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15093
15094 return type;
15095 }
15096
15097 /* Allocate an integer type of size BITS and name NAME. */
15098
15099 static struct type *
15100 dwarf2_init_integer_type (struct dwarf2_cu *cu, int bits, int unsigned_p,
15101 const char *name)
15102 {
15103 struct type *type;
15104 struct objfile *objfile = cu->per_objfile->objfile;
15105
15106 /* Versions of Intel's C Compiler generate an integer type called "void"
15107 instead of using DW_TAG_unspecified_type. This has been seen on
15108 at least versions 14, 17, and 18. */
15109 if (bits == 0 && producer_is_icc (cu) && name != nullptr
15110 && strcmp (name, "void") == 0)
15111 type = builtin_type (objfile)->builtin_void;
15112 else
15113 {
15114 type_allocator alloc (objfile, cu->lang ());
15115 type = init_integer_type (alloc, bits, unsigned_p, name);
15116 }
15117
15118 return type;
15119 }
15120
15121 /* Return true if DIE has a DW_AT_small attribute whose value is
15122 a constant rational, where both the numerator and denominator
15123 are equal to zero.
15124
15125 CU is the DIE's Compilation Unit. */
15126
15127 static bool
15128 has_zero_over_zero_small_attribute (struct die_info *die,
15129 struct dwarf2_cu *cu)
15130 {
15131 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
15132 if (attr == nullptr)
15133 return false;
15134
15135 struct dwarf2_cu *scale_cu = cu;
15136 struct die_info *scale_die
15137 = follow_die_ref (die, attr, &scale_cu);
15138
15139 if (scale_die->tag != DW_TAG_constant)
15140 return false;
15141
15142 gdb_mpz num (1), denom (1);
15143 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
15144 return num == 0 && denom == 0;
15145 }
15146
15147 /* Initialise and return a floating point type of size BITS suitable for
15148 use as a component of a complex number. The NAME_HINT is passed through
15149 when initialising the floating point type and is the name of the complex
15150 type.
15151
15152 As DWARF doesn't currently provide an explicit name for the components
15153 of a complex number, but it can be helpful to have these components
15154 named, we try to select a suitable name based on the size of the
15155 component. */
15156 static struct type *
15157 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
15158 int bits, const char *name_hint,
15159 enum bfd_endian byte_order)
15160 {
15161 struct objfile *objfile = cu->per_objfile->objfile;
15162 gdbarch *gdbarch = objfile->arch ();
15163 struct type *tt = nullptr;
15164
15165 /* Try to find a suitable floating point builtin type of size BITS.
15166 We're going to use the name of this type as the name for the complex
15167 target type that we are about to create. */
15168 switch (cu->lang ())
15169 {
15170 case language_fortran:
15171 switch (bits)
15172 {
15173 case 32:
15174 tt = builtin_f_type (gdbarch)->builtin_real;
15175 break;
15176 case 64:
15177 tt = builtin_f_type (gdbarch)->builtin_real_s8;
15178 break;
15179 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15180 case 128:
15181 tt = builtin_f_type (gdbarch)->builtin_real_s16;
15182 break;
15183 }
15184 break;
15185 default:
15186 switch (bits)
15187 {
15188 case 32:
15189 tt = builtin_type (gdbarch)->builtin_float;
15190 break;
15191 case 64:
15192 tt = builtin_type (gdbarch)->builtin_double;
15193 break;
15194 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15195 case 128:
15196 tt = builtin_type (gdbarch)->builtin_long_double;
15197 break;
15198 }
15199 break;
15200 }
15201
15202 /* If the type we found doesn't match the size we were looking for, then
15203 pretend we didn't find a type at all, the complex target type we
15204 create will then be nameless. */
15205 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
15206 tt = nullptr;
15207
15208 const char *name = (tt == nullptr) ? nullptr : tt->name ();
15209 return dwarf2_init_float_type (cu, bits, name, name_hint, byte_order);
15210 }
15211
15212 /* Find a representation of a given base type and install
15213 it in the TYPE field of the die. */
15214
15215 static struct type *
15216 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15217 {
15218 struct objfile *objfile = cu->per_objfile->objfile;
15219 struct type *type;
15220 struct attribute *attr;
15221 int encoding = 0, bits = 0;
15222 const char *name;
15223 gdbarch *arch;
15224
15225 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15226 if (attr != nullptr && attr->form_is_constant ())
15227 encoding = attr->constant_value (0);
15228 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15229 if (attr != nullptr)
15230 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
15231 name = dwarf2_name (die, cu);
15232 if (!name)
15233 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
15234
15235 arch = objfile->arch ();
15236 enum bfd_endian byte_order = gdbarch_byte_order (arch);
15237
15238 attr = dwarf2_attr (die, DW_AT_endianity, cu);
15239 if (attr != nullptr && attr->form_is_constant ())
15240 {
15241 int endianity = attr->constant_value (0);
15242
15243 switch (endianity)
15244 {
15245 case DW_END_big:
15246 byte_order = BFD_ENDIAN_BIG;
15247 break;
15248 case DW_END_little:
15249 byte_order = BFD_ENDIAN_LITTLE;
15250 break;
15251 default:
15252 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
15253 break;
15254 }
15255 }
15256
15257 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
15258 && cu->lang () == language_ada
15259 && has_zero_over_zero_small_attribute (die, cu))
15260 {
15261 /* brobecker/2018-02-24: This is a fixed point type for which
15262 the scaling factor is represented as fraction whose value
15263 does not make sense (zero divided by zero), so we should
15264 normally never see these. However, there is a small category
15265 of fixed point types for which GNAT is unable to provide
15266 the scaling factor via the standard DWARF mechanisms, and
15267 for which the info is provided via the GNAT encodings instead.
15268 This is likely what this DIE is about. */
15269 encoding = (encoding == DW_ATE_signed_fixed
15270 ? DW_ATE_signed
15271 : DW_ATE_unsigned);
15272 }
15273
15274 /* With GNAT encodings, fixed-point information will be encoded in
15275 the type name. Note that this can also occur with the above
15276 zero-over-zero case, which is why this is a separate "if" rather
15277 than an "else if". */
15278 const char *gnat_encoding_suffix = nullptr;
15279 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
15280 && cu->lang () == language_ada
15281 && name != nullptr)
15282 {
15283 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
15284 if (gnat_encoding_suffix != nullptr)
15285 {
15286 gdb_assert (startswith (gnat_encoding_suffix,
15287 GNAT_FIXED_POINT_SUFFIX));
15288 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
15289 name, gnat_encoding_suffix - name);
15290 /* Use -1 here so that SUFFIX points at the "_" after the
15291 "XF". */
15292 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
15293
15294 encoding = (encoding == DW_ATE_signed
15295 ? DW_ATE_signed_fixed
15296 : DW_ATE_unsigned_fixed);
15297 }
15298 }
15299
15300 type_allocator alloc (objfile, cu->lang ());
15301 switch (encoding)
15302 {
15303 case DW_ATE_address:
15304 /* Turn DW_ATE_address into a void * pointer. */
15305 type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
15306 type = init_pointer_type (alloc, bits, name, type);
15307 break;
15308 case DW_ATE_boolean:
15309 type = init_boolean_type (alloc, bits, 1, name);
15310 break;
15311 case DW_ATE_complex_float:
15312 type = dwarf2_init_complex_target_type (cu, bits / 2, name,
15313 byte_order);
15314 if (type->code () == TYPE_CODE_ERROR)
15315 {
15316 if (name == nullptr)
15317 {
15318 struct obstack *obstack
15319 = &cu->per_objfile->objfile->objfile_obstack;
15320 name = obconcat (obstack, "_Complex ", type->name (),
15321 nullptr);
15322 }
15323 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15324 }
15325 else
15326 type = init_complex_type (name, type);
15327 break;
15328 case DW_ATE_decimal_float:
15329 type = init_decfloat_type (alloc, bits, name);
15330 break;
15331 case DW_ATE_float:
15332 type = dwarf2_init_float_type (cu, bits, name, name, byte_order);
15333 break;
15334 case DW_ATE_signed:
15335 type = dwarf2_init_integer_type (cu, bits, 0, name);
15336 break;
15337 case DW_ATE_unsigned:
15338 if (cu->lang () == language_fortran
15339 && name
15340 && startswith (name, "character("))
15341 type = init_character_type (alloc, bits, 1, name);
15342 else
15343 type = dwarf2_init_integer_type (cu, bits, 1, name);
15344 break;
15345 case DW_ATE_signed_char:
15346 if (cu->lang () == language_ada
15347 || cu->lang () == language_m2
15348 || cu->lang () == language_pascal
15349 || cu->lang () == language_fortran)
15350 type = init_character_type (alloc, bits, 0, name);
15351 else
15352 type = dwarf2_init_integer_type (cu, bits, 0, name);
15353 break;
15354 case DW_ATE_unsigned_char:
15355 if (cu->lang () == language_ada
15356 || cu->lang () == language_m2
15357 || cu->lang () == language_pascal
15358 || cu->lang () == language_fortran
15359 || cu->lang () == language_rust)
15360 type = init_character_type (alloc, bits, 1, name);
15361 else
15362 type = dwarf2_init_integer_type (cu, bits, 1, name);
15363 break;
15364 case DW_ATE_UTF:
15365 {
15366 type = init_character_type (alloc, bits, 1, name);
15367 return set_die_type (die, type, cu);
15368 }
15369 break;
15370 case DW_ATE_signed_fixed:
15371 type = init_fixed_point_type (alloc, bits, 0, name);
15372 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15373 break;
15374 case DW_ATE_unsigned_fixed:
15375 type = init_fixed_point_type (alloc, bits, 1, name);
15376 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15377 break;
15378
15379 default:
15380 complaint (_("unsupported DW_AT_encoding: '%s'"),
15381 dwarf_type_encoding_name (encoding));
15382 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15383 break;
15384 }
15385
15386 if (type->code () == TYPE_CODE_INT
15387 && name != nullptr
15388 && strcmp (name, "char") == 0)
15389 type->set_has_no_signedness (true);
15390
15391 maybe_set_alignment (cu, die, type);
15392
15393 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
15394
15395 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
15396 {
15397 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15398 if (attr != nullptr && attr->form_is_constant ())
15399 {
15400 unsigned real_bit_size = attr->constant_value (0);
15401 if (real_bit_size >= 0 && real_bit_size <= 8 * type->length ())
15402 {
15403 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15404 /* Only use the attributes if they make sense together. */
15405 if (attr == nullptr
15406 || (attr->form_is_constant ()
15407 && attr->constant_value (0) >= 0
15408 && (attr->constant_value (0) + real_bit_size
15409 <= 8 * type->length ())))
15410 {
15411 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
15412 = real_bit_size;
15413 if (attr != nullptr)
15414 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
15415 = attr->constant_value (0);
15416 }
15417 }
15418 }
15419 }
15420
15421 return set_die_type (die, type, cu);
15422 }
15423
15424 /* A helper function that returns the name of DIE, if it refers to a
15425 variable declaration. */
15426
15427 static const char *
15428 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
15429 {
15430 if (die->tag != DW_TAG_variable)
15431 return nullptr;
15432
15433 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
15434 if (attr == nullptr || !attr->as_boolean ())
15435 return nullptr;
15436
15437 attr = dwarf2_attr (die, DW_AT_name, cu);
15438 if (attr == nullptr)
15439 return nullptr;
15440 return attr->as_string ();
15441 }
15442
15443 /* Parse dwarf attribute if it's a block, reference or constant and put the
15444 resulting value of the attribute into struct bound_prop.
15445 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15446
15447 static int
15448 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15449 struct dwarf2_cu *cu, struct dynamic_prop *prop,
15450 struct type *default_type)
15451 {
15452 struct dwarf2_property_baton *baton;
15453 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15454 struct objfile *objfile = per_objfile->objfile;
15455 struct obstack *obstack = &objfile->objfile_obstack;
15456
15457 gdb_assert (default_type != NULL);
15458
15459 if (attr == NULL || prop == NULL)
15460 return 0;
15461
15462 if (attr->form_is_block ())
15463 {
15464 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15465 baton->property_type = default_type;
15466 baton->locexpr.per_cu = cu->per_cu;
15467 baton->locexpr.per_objfile = per_objfile;
15468
15469 struct dwarf_block *block;
15470 if (attr->form == DW_FORM_data16)
15471 {
15472 size_t data_size = 16;
15473 block = XOBNEW (obstack, struct dwarf_block);
15474 block->size = (data_size
15475 + 2 /* Extra bytes for DW_OP and arg. */);
15476 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
15477 data[0] = DW_OP_implicit_value;
15478 data[1] = data_size;
15479 memcpy (&data[2], attr->as_block ()->data, data_size);
15480 block->data = data;
15481 }
15482 else
15483 block = attr->as_block ();
15484
15485 baton->locexpr.size = block->size;
15486 baton->locexpr.data = block->data;
15487 switch (attr->name)
15488 {
15489 case DW_AT_string_length:
15490 baton->locexpr.is_reference = true;
15491 break;
15492 default:
15493 baton->locexpr.is_reference = false;
15494 break;
15495 }
15496
15497 prop->set_locexpr (baton);
15498 gdb_assert (prop->baton () != NULL);
15499 }
15500 else if (attr->form_is_ref ())
15501 {
15502 struct dwarf2_cu *target_cu = cu;
15503 struct die_info *target_die;
15504 struct attribute *target_attr;
15505
15506 target_die = follow_die_ref (die, attr, &target_cu);
15507 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15508 if (target_attr == NULL)
15509 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15510 target_cu);
15511 if (target_attr == nullptr)
15512 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
15513 target_cu);
15514 if (target_attr == NULL)
15515 {
15516 const char *name = var_decl_name (target_die, target_cu);
15517 if (name != nullptr)
15518 {
15519 prop->set_variable_name (name);
15520 return 1;
15521 }
15522 return 0;
15523 }
15524
15525 switch (target_attr->name)
15526 {
15527 case DW_AT_location:
15528 if (target_attr->form_is_section_offset ())
15529 {
15530 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15531 baton->property_type = die_type (target_die, target_cu);
15532 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15533 prop->set_loclist (baton);
15534 gdb_assert (prop->baton () != NULL);
15535 }
15536 else if (target_attr->form_is_block ())
15537 {
15538 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15539 baton->property_type = die_type (target_die, target_cu);
15540 baton->locexpr.per_cu = cu->per_cu;
15541 baton->locexpr.per_objfile = per_objfile;
15542 struct dwarf_block *block = target_attr->as_block ();
15543 baton->locexpr.size = block->size;
15544 baton->locexpr.data = block->data;
15545 baton->locexpr.is_reference = true;
15546 prop->set_locexpr (baton);
15547 gdb_assert (prop->baton () != NULL);
15548 }
15549 else
15550 {
15551 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15552 "dynamic property");
15553 return 0;
15554 }
15555 break;
15556 case DW_AT_data_member_location:
15557 case DW_AT_data_bit_offset:
15558 {
15559 LONGEST offset;
15560
15561 if (!handle_member_location (target_die, target_cu, &offset))
15562 return 0;
15563
15564 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15565 baton->property_type = read_type_die (target_die->parent,
15566 target_cu);
15567 baton->offset_info.offset = offset;
15568 baton->offset_info.type = die_type (target_die, target_cu);
15569 prop->set_addr_offset (baton);
15570 break;
15571 }
15572 }
15573 }
15574 else if (attr->form_is_constant ())
15575 prop->set_const_val (attr->constant_value (0));
15576 else if (attr->form_is_section_offset ())
15577 {
15578 switch (attr->name)
15579 {
15580 case DW_AT_string_length:
15581 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15582 baton->property_type = default_type;
15583 fill_in_loclist_baton (cu, &baton->loclist, attr);
15584 prop->set_loclist (baton);
15585 gdb_assert (prop->baton () != NULL);
15586 break;
15587 default:
15588 goto invalid;
15589 }
15590 }
15591 else
15592 goto invalid;
15593
15594 return 1;
15595
15596 invalid:
15597 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15598 dwarf2_name (die, cu));
15599 return 0;
15600 }
15601
15602 /* See read.h. */
15603
15604 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
15605 present (which is valid) then compute the default type based on the
15606 compilation units address size. */
15607
15608 static struct type *
15609 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
15610 {
15611 struct type *index_type = die_type (die, cu);
15612
15613 /* Dwarf-2 specifications explicitly allows to create subrange types
15614 without specifying a base type.
15615 In that case, the base type must be set to the type of
15616 the lower bound, upper bound or count, in that order, if any of these
15617 three attributes references an object that has a type.
15618 If no base type is found, the Dwarf-2 specifications say that
15619 a signed integer type of size equal to the size of an address should
15620 be used.
15621 For the following C code: `extern char gdb_int [];'
15622 GCC produces an empty range DIE.
15623 FIXME: muller/2010-05-28: Possible references to object for low bound,
15624 high bound or count are not yet handled by this code. */
15625 if (index_type->code () == TYPE_CODE_VOID)
15626 index_type = cu->addr_sized_int_type (false);
15627
15628 return index_type;
15629 }
15630
15631 /* Read the given DW_AT_subrange DIE. */
15632
15633 static struct type *
15634 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15635 {
15636 struct type *base_type, *orig_base_type;
15637 struct type *range_type;
15638 struct attribute *attr;
15639 struct dynamic_prop low, high;
15640 int low_default_is_valid;
15641 int high_bound_is_count = 0;
15642 const char *name;
15643
15644 orig_base_type = read_subrange_index_type (die, cu);
15645
15646 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15647 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15648 creating the range type, but we use the result of check_typedef
15649 when examining properties of the type. */
15650 base_type = check_typedef (orig_base_type);
15651
15652 /* The die_type call above may have already set the type for this DIE. */
15653 range_type = get_die_type (die, cu);
15654 if (range_type)
15655 return range_type;
15656
15657 high.set_const_val (0);
15658
15659 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15660 omitting DW_AT_lower_bound. */
15661 switch (cu->lang ())
15662 {
15663 case language_c:
15664 case language_cplus:
15665 low.set_const_val (0);
15666 low_default_is_valid = 1;
15667 break;
15668 case language_fortran:
15669 low.set_const_val (1);
15670 low_default_is_valid = 1;
15671 break;
15672 case language_d:
15673 case language_objc:
15674 case language_rust:
15675 low.set_const_val (0);
15676 low_default_is_valid = (cu->header.version >= 4);
15677 break;
15678 case language_ada:
15679 case language_m2:
15680 case language_pascal:
15681 low.set_const_val (1);
15682 low_default_is_valid = (cu->header.version >= 4);
15683 break;
15684 default:
15685 low.set_const_val (0);
15686 low_default_is_valid = 0;
15687 break;
15688 }
15689
15690 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15691 if (attr != nullptr)
15692 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
15693 else if (!low_default_is_valid)
15694 complaint (_("Missing DW_AT_lower_bound "
15695 "- DIE at %s [in module %s]"),
15696 sect_offset_str (die->sect_off),
15697 objfile_name (cu->per_objfile->objfile));
15698
15699 struct attribute *attr_ub, *attr_count;
15700 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
15701 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15702 {
15703 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
15704 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15705 {
15706 /* If bounds are constant do the final calculation here. */
15707 if (low.is_constant () && high.is_constant ())
15708 high.set_const_val (low.const_val () + high.const_val () - 1);
15709 else
15710 high_bound_is_count = 1;
15711 }
15712 else
15713 {
15714 if (attr_ub != NULL)
15715 complaint (_("Unresolved DW_AT_upper_bound "
15716 "- DIE at %s [in module %s]"),
15717 sect_offset_str (die->sect_off),
15718 objfile_name (cu->per_objfile->objfile));
15719 if (attr_count != NULL)
15720 complaint (_("Unresolved DW_AT_count "
15721 "- DIE at %s [in module %s]"),
15722 sect_offset_str (die->sect_off),
15723 objfile_name (cu->per_objfile->objfile));
15724 }
15725 }
15726
15727 LONGEST bias = 0;
15728 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
15729 if (bias_attr != nullptr && bias_attr->form_is_constant ())
15730 bias = bias_attr->constant_value (0);
15731
15732 /* Normally, the DWARF producers are expected to use a signed
15733 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15734 But this is unfortunately not always the case, as witnessed
15735 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15736 is used instead. To work around that ambiguity, we treat
15737 the bounds as signed, and thus sign-extend their values, when
15738 the base type is signed.
15739
15740 Skip it if the base type's length is larger than ULONGEST, to avoid
15741 the undefined behavior of a too large left shift. We don't really handle
15742 constants larger than 8 bytes anyway, at the moment. */
15743
15744 if (base_type->length () <= sizeof (ULONGEST))
15745 {
15746 ULONGEST negative_mask
15747 = -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
15748
15749 if (low.is_constant ()
15750 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
15751 low.set_const_val (low.const_val () | negative_mask);
15752
15753 if (high.is_constant ()
15754 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
15755 high.set_const_val (high.const_val () | negative_mask);
15756 }
15757
15758 /* Check for bit and byte strides. */
15759 struct dynamic_prop byte_stride_prop;
15760 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
15761 if (attr_byte_stride != nullptr)
15762 {
15763 struct type *prop_type = cu->addr_sized_int_type (false);
15764 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
15765 prop_type);
15766 }
15767
15768 struct dynamic_prop bit_stride_prop;
15769 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
15770 if (attr_bit_stride != nullptr)
15771 {
15772 /* It only makes sense to have either a bit or byte stride. */
15773 if (attr_byte_stride != nullptr)
15774 {
15775 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
15776 "- DIE at %s [in module %s]"),
15777 sect_offset_str (die->sect_off),
15778 objfile_name (cu->per_objfile->objfile));
15779 attr_bit_stride = nullptr;
15780 }
15781 else
15782 {
15783 struct type *prop_type = cu->addr_sized_int_type (false);
15784 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
15785 prop_type);
15786 }
15787 }
15788
15789 type_allocator alloc (cu->per_objfile->objfile, cu->lang ());
15790 if (attr_byte_stride != nullptr
15791 || attr_bit_stride != nullptr)
15792 {
15793 bool byte_stride_p = (attr_byte_stride != nullptr);
15794 struct dynamic_prop *stride
15795 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
15796
15797 range_type
15798 = create_range_type_with_stride (alloc, orig_base_type, &low,
15799 &high, bias, stride, byte_stride_p);
15800 }
15801 else
15802 range_type = create_range_type (alloc, orig_base_type, &low, &high, bias);
15803
15804 if (high_bound_is_count)
15805 range_type->bounds ()->flag_upper_bound_is_count = 1;
15806
15807 /* Ada expects an empty array on no boundary attributes. */
15808 if (attr == NULL && cu->lang () != language_ada)
15809 range_type->bounds ()->high.set_undefined ();
15810
15811 name = dwarf2_name (die, cu);
15812 if (name)
15813 range_type->set_name (name);
15814
15815 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15816 if (attr != nullptr)
15817 range_type->set_length (attr->constant_value (0));
15818
15819 maybe_set_alignment (cu, die, range_type);
15820
15821 set_die_type (die, range_type, cu);
15822
15823 /* set_die_type should be already done. */
15824 set_descriptive_type (range_type, die, cu);
15825
15826 return range_type;
15827 }
15828
15829 static struct type *
15830 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15831 {
15832 struct type *type;
15833
15834 type = (type_allocator (cu->per_objfile->objfile, cu->lang ())
15835 .new_type (TYPE_CODE_VOID, 0, nullptr));
15836 type->set_name (dwarf2_name (die, cu));
15837
15838 /* In Ada, an unspecified type is typically used when the description
15839 of the type is deferred to a different unit. When encountering
15840 such a type, we treat it as a stub, and try to resolve it later on,
15841 when needed.
15842 Mark this as a stub type for all languages though. */
15843 type->set_is_stub (true);
15844
15845 return set_die_type (die, type, cu);
15846 }
15847
15848 /* Read a single die and all its descendents. Set the die's sibling
15849 field to NULL; set other fields in the die correctly, and set all
15850 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15851 location of the info_ptr after reading all of those dies. PARENT
15852 is the parent of the die in question. */
15853
15854 static struct die_info *
15855 read_die_and_children (const struct die_reader_specs *reader,
15856 const gdb_byte *info_ptr,
15857 const gdb_byte **new_info_ptr,
15858 struct die_info *parent)
15859 {
15860 struct die_info *die;
15861 const gdb_byte *cur_ptr;
15862
15863 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0, true);
15864 if (die == NULL)
15865 {
15866 *new_info_ptr = cur_ptr;
15867 return NULL;
15868 }
15869 store_in_ref_table (die, reader->cu);
15870
15871 if (die->has_children)
15872 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15873 else
15874 {
15875 die->child = NULL;
15876 *new_info_ptr = cur_ptr;
15877 }
15878
15879 die->sibling = NULL;
15880 die->parent = parent;
15881 return die;
15882 }
15883
15884 /* Read a die, all of its descendents, and all of its siblings; set
15885 all of the fields of all of the dies correctly. Arguments are as
15886 in read_die_and_children. */
15887
15888 static struct die_info *
15889 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15890 const gdb_byte *info_ptr,
15891 const gdb_byte **new_info_ptr,
15892 struct die_info *parent)
15893 {
15894 struct die_info *first_die, *last_sibling;
15895 const gdb_byte *cur_ptr;
15896
15897 cur_ptr = info_ptr;
15898 first_die = last_sibling = NULL;
15899
15900 while (1)
15901 {
15902 struct die_info *die
15903 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15904
15905 if (die == NULL)
15906 {
15907 *new_info_ptr = cur_ptr;
15908 return first_die;
15909 }
15910
15911 if (!first_die)
15912 first_die = die;
15913 else
15914 last_sibling->sibling = die;
15915
15916 last_sibling = die;
15917 }
15918 }
15919
15920 /* Read a die, all of its descendents, and all of its siblings; set
15921 all of the fields of all of the dies correctly. Arguments are as
15922 in read_die_and_children.
15923 This the main entry point for reading a DIE and all its children. */
15924
15925 static struct die_info *
15926 read_die_and_siblings (const struct die_reader_specs *reader,
15927 const gdb_byte *info_ptr,
15928 const gdb_byte **new_info_ptr,
15929 struct die_info *parent)
15930 {
15931 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15932 new_info_ptr, parent);
15933
15934 if (dwarf_die_debug)
15935 {
15936 gdb_printf (gdb_stdlog,
15937 "Read die from %s@0x%x of %s:\n",
15938 reader->die_section->get_name (),
15939 (unsigned) (info_ptr - reader->die_section->buffer),
15940 bfd_get_filename (reader->abfd));
15941 die->dump (dwarf_die_debug);
15942 }
15943
15944 return die;
15945 }
15946
15947 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15948 attributes.
15949 The caller is responsible for filling in the extra attributes
15950 and updating (*DIEP)->num_attrs.
15951 Set DIEP to point to a newly allocated die with its information,
15952 except for its child, sibling, and parent fields. */
15953
15954 static const gdb_byte *
15955 read_full_die_1 (const struct die_reader_specs *reader,
15956 struct die_info **diep, const gdb_byte *info_ptr,
15957 int num_extra_attrs, bool allow_reprocess)
15958 {
15959 unsigned int abbrev_number, bytes_read, i;
15960 const struct abbrev_info *abbrev;
15961 struct die_info *die;
15962 struct dwarf2_cu *cu = reader->cu;
15963 bfd *abfd = reader->abfd;
15964
15965 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15966 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15967 info_ptr += bytes_read;
15968 if (!abbrev_number)
15969 {
15970 *diep = NULL;
15971 return info_ptr;
15972 }
15973
15974 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
15975 if (!abbrev)
15976 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15977 abbrev_number,
15978 bfd_get_filename (abfd));
15979
15980 die = die_info::allocate (&cu->comp_unit_obstack,
15981 abbrev->num_attrs + num_extra_attrs);
15982 die->sect_off = sect_off;
15983 die->tag = abbrev->tag;
15984 die->abbrev = abbrev_number;
15985 die->has_children = abbrev->has_children;
15986
15987 /* Make the result usable.
15988 The caller needs to update num_attrs after adding the extra
15989 attributes. */
15990 die->num_attrs = abbrev->num_attrs;
15991
15992 for (i = 0; i < abbrev->num_attrs; ++i)
15993 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15994 info_ptr, allow_reprocess);
15995
15996 *diep = die;
15997 return info_ptr;
15998 }
15999
16000 /* Read a die and all its attributes.
16001 Set DIEP to point to a newly allocated die with its information,
16002 except for its child, sibling, and parent fields. */
16003
16004 static const gdb_byte *
16005 read_toplevel_die (const struct die_reader_specs *reader,
16006 struct die_info **diep, const gdb_byte *info_ptr,
16007 gdb::array_view<attribute *> extra_attrs)
16008 {
16009 const gdb_byte *result;
16010 struct dwarf2_cu *cu = reader->cu;
16011
16012 result = read_full_die_1 (reader, diep, info_ptr, extra_attrs.size (),
16013 false);
16014
16015 /* Copy in the extra attributes, if any. */
16016 attribute *next = &(*diep)->attrs[(*diep)->num_attrs];
16017 for (attribute *extra : extra_attrs)
16018 *next++ = *extra;
16019
16020 struct attribute *attr = (*diep)->attr (DW_AT_str_offsets_base);
16021 if (attr != nullptr && attr->form_is_unsigned ())
16022 cu->str_offsets_base = attr->as_unsigned ();
16023
16024 attr = (*diep)->attr (DW_AT_loclists_base);
16025 if (attr != nullptr)
16026 cu->loclist_base = attr->as_unsigned ();
16027
16028 auto maybe_addr_base = (*diep)->addr_base ();
16029 if (maybe_addr_base.has_value ())
16030 cu->addr_base = *maybe_addr_base;
16031
16032 attr = (*diep)->attr (DW_AT_rnglists_base);
16033 if (attr != nullptr)
16034 cu->rnglists_base = attr->as_unsigned ();
16035
16036 for (int i = 0; i < (*diep)->num_attrs; ++i)
16037 {
16038 if ((*diep)->attrs[i].form_requires_reprocessing ())
16039 read_attribute_reprocess (reader, &(*diep)->attrs[i], (*diep)->tag);
16040 }
16041
16042 (*diep)->num_attrs += extra_attrs.size ();
16043
16044 if (dwarf_die_debug)
16045 {
16046 gdb_printf (gdb_stdlog,
16047 "Read die from %s@0x%x of %s:\n",
16048 reader->die_section->get_name (),
16049 (unsigned) (info_ptr - reader->die_section->buffer),
16050 bfd_get_filename (reader->abfd));
16051 (*diep)->dump (dwarf_die_debug);
16052 }
16053
16054 return result;
16055 }
16056 \f
16057
16058 void
16059 cooked_indexer::check_bounds (cutu_reader *reader)
16060 {
16061 if (reader->cu->per_cu->addresses_seen)
16062 return;
16063
16064 dwarf2_cu *cu = reader->cu;
16065
16066 unrelocated_addr best_lowpc = {}, best_highpc = {};
16067 /* Possibly set the default values of LOWPC and HIGHPC from
16068 `DW_AT_ranges'. */
16069 dwarf2_find_base_address (reader->comp_unit_die, cu);
16070 enum pc_bounds_kind cu_bounds_kind
16071 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
16072 cu, m_index_storage->get_addrmap (), cu->per_cu);
16073 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
16074 {
16075 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16076 unrelocated_addr low = per_objfile->adjust (best_lowpc);
16077 unrelocated_addr high = per_objfile->adjust (best_highpc);
16078 /* Store the contiguous range if it is not empty; it can be
16079 empty for CUs with no code. addrmap requires CORE_ADDR, so
16080 we cast here. */
16081 m_index_storage->get_addrmap ()->set_empty ((CORE_ADDR) low,
16082 (CORE_ADDR) high - 1,
16083 cu->per_cu);
16084
16085 cu->per_cu->addresses_seen = true;
16086 }
16087 }
16088
16089 /* Helper function that returns true if TAG can have a linkage
16090 name. */
16091
16092 static bool
16093 tag_can_have_linkage_name (enum dwarf_tag tag)
16094 {
16095 switch (tag)
16096 {
16097 case DW_TAG_variable:
16098 case DW_TAG_subprogram:
16099 return true;
16100
16101 default:
16102 return false;
16103 }
16104 }
16105
16106 cutu_reader *
16107 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
16108 dwarf2_per_objfile *per_objfile,
16109 sect_offset sect_off, bool is_dwz,
16110 bool for_scanning)
16111 {
16112 /* Lookups for type unit references are always in the CU, and
16113 cross-CU references will crash. */
16114 if (reader->cu->per_cu->is_dwz == is_dwz
16115 && reader->cu->header.offset_in_cu_p (sect_off))
16116 return reader;
16117
16118 dwarf2_per_cu_data *per_cu
16119 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
16120 per_objfile->per_bfd);
16121
16122 /* When scanning, we only want to visit a given CU a single time.
16123 Doing this check here avoids self-imports as well. */
16124 if (for_scanning)
16125 {
16126 bool nope = false;
16127 if (!per_cu->scanned.compare_exchange_strong (nope, true))
16128 return nullptr;
16129 }
16130 if (per_cu == m_per_cu)
16131 return reader;
16132
16133 cutu_reader *result = m_index_storage->get_reader (per_cu);
16134 if (result == nullptr)
16135 {
16136 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
16137 m_index_storage->get_abbrev_cache ());
16138
16139 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
16140 language_minimal);
16141 std::unique_ptr<cutu_reader> copy
16142 (new cutu_reader (std::move (new_reader)));
16143 result = m_index_storage->preserve (std::move (copy));
16144 }
16145
16146 if (result->dummy_p || !result->comp_unit_die->has_children)
16147 return nullptr;
16148
16149 if (for_scanning)
16150 check_bounds (result);
16151
16152 return result;
16153 }
16154
16155 const gdb_byte *
16156 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
16157 cutu_reader *reader,
16158 const gdb_byte *watermark_ptr,
16159 const gdb_byte *info_ptr,
16160 const abbrev_info *abbrev,
16161 const char **name,
16162 const char **linkage_name,
16163 cooked_index_flag *flags,
16164 sect_offset *sibling_offset,
16165 const cooked_index_entry **parent_entry,
16166 CORE_ADDR *maybe_defer,
16167 bool for_specification)
16168 {
16169 bool origin_is_dwz = false;
16170 bool is_declaration = false;
16171 sect_offset origin_offset {};
16172
16173 std::optional<unrelocated_addr> low_pc;
16174 std::optional<unrelocated_addr> high_pc;
16175 bool high_pc_relative = false;
16176
16177 for (int i = 0; i < abbrev->num_attrs; ++i)
16178 {
16179 attribute attr;
16180 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16181
16182 /* Store the data if it is of an attribute we want to keep in a
16183 partial symbol table. */
16184 switch (attr.name)
16185 {
16186 case DW_AT_name:
16187 switch (abbrev->tag)
16188 {
16189 case DW_TAG_compile_unit:
16190 case DW_TAG_partial_unit:
16191 case DW_TAG_type_unit:
16192 /* Compilation units have a DW_AT_name that is a filename, not
16193 a source language identifier. */
16194 break;
16195
16196 default:
16197 if (*name == nullptr)
16198 *name = attr.as_string ();
16199 break;
16200 }
16201 break;
16202
16203 case DW_AT_linkage_name:
16204 case DW_AT_MIPS_linkage_name:
16205 /* Note that both forms of linkage name might appear. We
16206 assume they will be the same, and we only store the last
16207 one we see. */
16208 if (*linkage_name == nullptr)
16209 *linkage_name = attr.as_string ();
16210 break;
16211
16212 /* DWARF 4 has defined a dedicated DW_AT_main_subprogram
16213 attribute to indicate the starting function of the program... */
16214 case DW_AT_main_subprogram:
16215 if (attr.as_boolean ())
16216 *flags |= IS_MAIN;
16217 break;
16218
16219 /* ... however with older versions the DW_CC_program value of
16220 the DW_AT_calling_convention attribute was used instead as
16221 the only means available. We handle both variants then. */
16222 case DW_AT_calling_convention:
16223 if (attr.constant_value (DW_CC_normal) == DW_CC_program)
16224 *flags |= IS_MAIN;
16225 break;
16226
16227 case DW_AT_declaration:
16228 is_declaration = attr.as_boolean ();
16229 break;
16230
16231 case DW_AT_sibling:
16232 if (sibling_offset != nullptr)
16233 *sibling_offset = attr.get_ref_die_offset ();
16234 break;
16235
16236 case DW_AT_specification:
16237 case DW_AT_abstract_origin:
16238 case DW_AT_extension:
16239 origin_offset = attr.get_ref_die_offset ();
16240 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
16241 break;
16242
16243 case DW_AT_external:
16244 if (attr.as_boolean ())
16245 *flags &= ~IS_STATIC;
16246 break;
16247
16248 case DW_AT_enum_class:
16249 if (attr.as_boolean ())
16250 *flags |= IS_ENUM_CLASS;
16251 break;
16252
16253 case DW_AT_low_pc:
16254 low_pc = attr.as_address ();
16255 break;
16256
16257 case DW_AT_high_pc:
16258 high_pc = attr.as_address ();
16259 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
16260 high_pc_relative = true;
16261 break;
16262
16263 case DW_AT_location:
16264 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
16265 {
16266 struct dwarf_block *locdesc = attr.as_block ();
16267 CORE_ADDR addr;
16268 if (decode_locdesc (locdesc, reader->cu, &addr)
16269 && (addr != 0
16270 || reader->cu->per_objfile->per_bfd->has_section_at_zero))
16271 {
16272 low_pc = (unrelocated_addr) addr;
16273 /* For variables, we don't want to try decoding the
16274 type just to find the size -- for gdb's purposes
16275 we only need the address of a variable. */
16276 high_pc = (unrelocated_addr) (addr + 1);
16277 high_pc_relative = false;
16278 }
16279 }
16280 break;
16281
16282 case DW_AT_ranges:
16283 if (!scanning_per_cu->addresses_seen)
16284 {
16285 /* Offset in the .debug_ranges or .debug_rnglist section
16286 (depending on DWARF version). */
16287 ULONGEST ranges_offset = attr.as_unsigned ();
16288
16289 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
16290 want to add this value. */
16291 ranges_offset += reader->cu->gnu_ranges_base;
16292
16293 unrelocated_addr lowpc, highpc;
16294 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
16295 m_index_storage->get_addrmap (),
16296 scanning_per_cu, abbrev->tag);
16297 }
16298 break;
16299 }
16300 }
16301
16302 /* We don't want to examine declarations, but if we found a
16303 declaration when handling DW_AT_specification or the like, then
16304 that is ok. Similarly, we allow an external variable without a
16305 location; those are resolved via minimal symbols. */
16306 if (is_declaration && !for_specification
16307 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
16308 {
16309 /* We always want to recurse into some types, but we may not
16310 want to treat them as definitions. */
16311 if ((abbrev->tag == DW_TAG_class_type
16312 || abbrev->tag == DW_TAG_structure_type
16313 || abbrev->tag == DW_TAG_union_type)
16314 && abbrev->has_children)
16315 *flags |= IS_TYPE_DECLARATION;
16316 else
16317 {
16318 *linkage_name = nullptr;
16319 *name = nullptr;
16320 }
16321 }
16322 else if ((*name == nullptr
16323 || (*linkage_name == nullptr
16324 && tag_can_have_linkage_name (abbrev->tag))
16325 || (*parent_entry == nullptr && m_language != language_c))
16326 && origin_offset != sect_offset (0))
16327 {
16328 cutu_reader *new_reader
16329 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
16330 origin_is_dwz, false);
16331 if (new_reader != nullptr)
16332 {
16333 const gdb_byte *new_info_ptr = (new_reader->buffer
16334 + to_underlying (origin_offset));
16335
16336 if (new_reader->cu == reader->cu
16337 && new_info_ptr > watermark_ptr
16338 && *parent_entry == nullptr)
16339 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
16340 else if (*parent_entry == nullptr)
16341 {
16342 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
16343 void *obj = m_die_range_map.find (lookup);
16344 *parent_entry = static_cast <cooked_index_entry *> (obj);
16345 }
16346
16347 unsigned int bytes_read;
16348 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
16349 new_info_ptr,
16350 &bytes_read);
16351 new_info_ptr += bytes_read;
16352
16353 if (new_reader->cu == reader->cu && new_info_ptr == watermark_ptr)
16354 {
16355 /* Self-reference, we're done. */
16356 }
16357 else
16358 scan_attributes (scanning_per_cu, new_reader, new_info_ptr,
16359 new_info_ptr, new_abbrev, name, linkage_name,
16360 flags, nullptr, parent_entry, maybe_defer, true);
16361 }
16362 }
16363
16364 if (!for_specification)
16365 {
16366 if (m_language == language_ada
16367 && *linkage_name == nullptr)
16368 *linkage_name = *name;
16369
16370 if (!scanning_per_cu->addresses_seen
16371 && low_pc.has_value ()
16372 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
16373 || *low_pc != (unrelocated_addr) 0)
16374 && high_pc.has_value ())
16375 {
16376 if (high_pc_relative)
16377 high_pc = (unrelocated_addr) ((ULONGEST) *high_pc
16378 + (ULONGEST) *low_pc);
16379
16380 if (*high_pc > *low_pc)
16381 {
16382 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
16383 unrelocated_addr lo = per_objfile->adjust (*low_pc);
16384 unrelocated_addr hi = per_objfile->adjust (*high_pc);
16385 /* Need CORE_ADDR casts for addrmap. */
16386 m_index_storage->get_addrmap ()->set_empty ((CORE_ADDR) lo,
16387 (CORE_ADDR) hi - 1,
16388 scanning_per_cu);
16389 }
16390 }
16391
16392 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
16393 *flags &= ~IS_STATIC;
16394
16395 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
16396 *name = "(anonymous namespace)";
16397
16398 if (m_language == language_cplus
16399 && (abbrev->tag == DW_TAG_class_type
16400 || abbrev->tag == DW_TAG_interface_type
16401 || abbrev->tag == DW_TAG_structure_type
16402 || abbrev->tag == DW_TAG_union_type
16403 || abbrev->tag == DW_TAG_enumeration_type
16404 || abbrev->tag == DW_TAG_enumerator))
16405 *flags &= ~IS_STATIC;
16406 }
16407
16408 return info_ptr;
16409 }
16410
16411 const gdb_byte *
16412 cooked_indexer::index_imported_unit (cutu_reader *reader,
16413 const gdb_byte *info_ptr,
16414 const abbrev_info *abbrev)
16415 {
16416 sect_offset sect_off {};
16417 bool is_dwz = false;
16418
16419 for (int i = 0; i < abbrev->num_attrs; ++i)
16420 {
16421 /* Note that we never need to reprocess attributes here. */
16422 attribute attr;
16423 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16424
16425 if (attr.name == DW_AT_import)
16426 {
16427 sect_off = attr.get_ref_die_offset ();
16428 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16429 || reader->cu->per_cu->is_dwz);
16430 }
16431 }
16432
16433 /* Did not find DW_AT_import. */
16434 if (sect_off == sect_offset (0))
16435 return info_ptr;
16436
16437 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
16438 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
16439 is_dwz, true);
16440 if (new_reader != nullptr)
16441 {
16442 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
16443
16444 reader->cu->add_dependence (new_reader->cu->per_cu);
16445 }
16446
16447 return info_ptr;
16448 }
16449
16450 const gdb_byte *
16451 cooked_indexer::recurse (cutu_reader *reader,
16452 const gdb_byte *info_ptr,
16453 const cooked_index_entry *parent_entry,
16454 bool fully)
16455 {
16456 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16457
16458 if (parent_entry != nullptr)
16459 {
16460 /* Both start and end are inclusive, so use both "+ 1" and "- 1" to
16461 limit the range to the children of parent_entry. */
16462 CORE_ADDR start = form_addr (parent_entry->die_offset + 1,
16463 reader->cu->per_cu->is_dwz);
16464 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
16465 reader->cu->per_cu->is_dwz);
16466 m_die_range_map.set_empty (start, end, (void *) parent_entry);
16467 }
16468
16469 return info_ptr;
16470 }
16471
16472 const gdb_byte *
16473 cooked_indexer::index_dies (cutu_reader *reader,
16474 const gdb_byte *info_ptr,
16475 const cooked_index_entry *parent_entry,
16476 bool fully)
16477 {
16478 const gdb_byte *end_ptr = (reader->buffer
16479 + to_underlying (reader->cu->header.sect_off)
16480 + reader->cu->header.get_length_with_initial ());
16481
16482 while (info_ptr < end_ptr)
16483 {
16484 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
16485 unsigned int bytes_read;
16486 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
16487 &bytes_read);
16488 info_ptr += bytes_read;
16489 if (abbrev == nullptr)
16490 break;
16491
16492 if (abbrev->tag == DW_TAG_imported_unit)
16493 {
16494 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
16495 continue;
16496 }
16497
16498 if (!abbrev->interesting)
16499 {
16500 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
16501 if (fully && abbrev->has_children)
16502 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16503 continue;
16504 }
16505
16506 const char *name = nullptr;
16507 const char *linkage_name = nullptr;
16508 CORE_ADDR defer = 0;
16509 cooked_index_flag flags = IS_STATIC;
16510 sect_offset sibling {};
16511 const cooked_index_entry *this_parent_entry = parent_entry;
16512
16513 /* The scope of a DW_TAG_entry_point cooked_index_entry is the one of
16514 its surrounding subroutine. */
16515 if (abbrev->tag == DW_TAG_entry_point)
16516 this_parent_entry = parent_entry->get_parent ();
16517 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
16518 info_ptr, abbrev, &name, &linkage_name,
16519 &flags, &sibling, &this_parent_entry,
16520 &defer, false);
16521
16522 if (abbrev->tag == DW_TAG_namespace
16523 && m_language == language_cplus
16524 && strcmp (name, "::") == 0)
16525 {
16526 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
16527 generated bogus DW_TAG_namespace DIEs with a name of "::"
16528 for the global namespace. Work around this problem
16529 here. */
16530 name = nullptr;
16531 }
16532
16533 cooked_index_entry *this_entry = nullptr;
16534 if (name != nullptr)
16535 {
16536 if (defer != 0)
16537 {
16538 this_entry
16539 = m_index_storage->add (this_die, abbrev->tag,
16540 flags | IS_PARENT_DEFERRED, name,
16541 defer, m_per_cu);
16542 m_deferred_entries.push_back (this_entry);
16543 }
16544 else
16545 this_entry
16546 = m_index_storage->add (this_die, abbrev->tag, flags, name,
16547 this_parent_entry, m_per_cu);
16548 }
16549
16550 if (linkage_name != nullptr)
16551 {
16552 /* We only want this to be "main" if it has a linkage name
16553 but not an ordinary name. */
16554 if (name != nullptr)
16555 flags = flags & ~IS_MAIN;
16556 /* Set the IS_LINKAGE on for everything except when functions
16557 have linkage name present but name is absent. */
16558 if (name != nullptr
16559 || (abbrev->tag != DW_TAG_subprogram
16560 && abbrev->tag != DW_TAG_inlined_subroutine
16561 && abbrev->tag != DW_TAG_entry_point))
16562 flags = flags | IS_LINKAGE;
16563 m_index_storage->add (this_die, abbrev->tag, flags,
16564 linkage_name, nullptr, m_per_cu);
16565 }
16566
16567 if (abbrev->has_children)
16568 {
16569 switch (abbrev->tag)
16570 {
16571 case DW_TAG_class_type:
16572 case DW_TAG_interface_type:
16573 case DW_TAG_structure_type:
16574 case DW_TAG_union_type:
16575 if (m_language != language_c && this_entry != nullptr)
16576 {
16577 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16578 continue;
16579 }
16580 break;
16581
16582 case DW_TAG_enumeration_type:
16583 /* We need to recurse even for an anonymous enumeration.
16584 Which scope we record as the parent scope depends on
16585 whether we're reading an "enum class". If so, we use
16586 the enum itself as the parent, yielding names like
16587 "enum_class::enumerator"; otherwise we inject the
16588 names into our own parent scope. */
16589 info_ptr = recurse (reader, info_ptr,
16590 ((flags & IS_ENUM_CLASS) == 0)
16591 ? parent_entry
16592 : this_entry,
16593 fully);
16594 continue;
16595
16596 case DW_TAG_module:
16597 if (this_entry == nullptr)
16598 break;
16599 [[fallthrough]];
16600 case DW_TAG_namespace:
16601 /* We don't check THIS_ENTRY for a namespace, to handle
16602 the ancient G++ workaround pointed out above. */
16603 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16604 continue;
16605
16606 case DW_TAG_subprogram:
16607 if ((m_language == language_fortran
16608 || m_language == language_ada)
16609 && this_entry != nullptr)
16610 {
16611 info_ptr = recurse (reader, info_ptr, this_entry, true);
16612 continue;
16613 }
16614 break;
16615 }
16616
16617 if (sibling != sect_offset (0))
16618 {
16619 const gdb_byte *sibling_ptr
16620 = reader->buffer + to_underlying (sibling);
16621
16622 if (sibling_ptr < info_ptr)
16623 complaint (_("DW_AT_sibling points backwards"));
16624 else if (sibling_ptr > reader->buffer_end)
16625 reader->die_section->overflow_complaint ();
16626 else
16627 info_ptr = sibling_ptr;
16628 }
16629 else
16630 info_ptr = skip_children (reader, info_ptr);
16631 }
16632 }
16633
16634 return info_ptr;
16635 }
16636
16637 void
16638 cooked_indexer::make_index (cutu_reader *reader)
16639 {
16640 check_bounds (reader);
16641 find_file_and_directory (reader->comp_unit_die, reader->cu);
16642 if (!reader->comp_unit_die->has_children)
16643 return;
16644 index_dies (reader, reader->info_ptr, nullptr, false);
16645
16646 for (const auto &entry : m_deferred_entries)
16647 {
16648 void *obj = m_die_range_map.find (entry->get_deferred_parent ());
16649 cooked_index_entry *parent = static_cast<cooked_index_entry *> (obj);
16650 entry->resolve_parent (parent);
16651 }
16652 }
16653
16654 /* An implementation of quick_symbol_functions for the cooked DWARF
16655 index. */
16656
16657 struct cooked_index_functions : public dwarf2_base_index_functions
16658 {
16659 cooked_index *wait (struct objfile *objfile, bool allow_quit)
16660 {
16661 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16662 cooked_index *table
16663 = (gdb::checked_static_cast<cooked_index *>
16664 (per_objfile->per_bfd->index_table.get ()));
16665 table->wait (cooked_state::MAIN_AVAILABLE, allow_quit);
16666 return table;
16667 }
16668
16669 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
16670 unrelocated_addr adjusted_pc) override;
16671
16672 struct compunit_symtab *find_compunit_symtab_by_address
16673 (struct objfile *objfile, CORE_ADDR address) override;
16674
16675 bool has_unexpanded_symtabs (struct objfile *objfile) override
16676 {
16677 wait (objfile, true);
16678 return dwarf2_base_index_functions::has_unexpanded_symtabs (objfile);
16679 }
16680
16681 struct symtab *find_last_source_symtab (struct objfile *objfile) override
16682 {
16683 wait (objfile, true);
16684 return dwarf2_base_index_functions::find_last_source_symtab (objfile);
16685 }
16686
16687 void forget_cached_source_info (struct objfile *objfile) override
16688 {
16689 wait (objfile, true);
16690 dwarf2_base_index_functions::forget_cached_source_info (objfile);
16691 }
16692
16693 void print_stats (struct objfile *objfile, bool print_bcache) override
16694 {
16695 wait (objfile, true);
16696 dwarf2_base_index_functions::print_stats (objfile, print_bcache);
16697 }
16698
16699 void dump (struct objfile *objfile) override
16700 {
16701 cooked_index *index = wait (objfile, true);
16702 gdb_printf ("Cooked index in use:\n");
16703 gdb_printf ("\n");
16704 index->dump (objfile->arch ());
16705 }
16706
16707 void expand_all_symtabs (struct objfile *objfile) override
16708 {
16709 wait (objfile, true);
16710 dwarf2_base_index_functions::expand_all_symtabs (objfile);
16711 }
16712
16713 bool expand_symtabs_matching
16714 (struct objfile *objfile,
16715 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16716 const lookup_name_info *lookup_name,
16717 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16718 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16719 block_search_flags search_flags,
16720 domain_enum domain,
16721 enum search_domain kind) override;
16722
16723 struct compunit_symtab *find_pc_sect_compunit_symtab
16724 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
16725 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override
16726 {
16727 wait (objfile, true);
16728 return (dwarf2_base_index_functions::find_pc_sect_compunit_symtab
16729 (objfile, msymbol, pc, section, warn_if_readin));
16730 }
16731
16732 void map_symbol_filenames
16733 (struct objfile *objfile,
16734 gdb::function_view<symbol_filename_ftype> fun,
16735 bool need_fullname) override
16736 {
16737 wait (objfile, true);
16738 return (dwarf2_base_index_functions::map_symbol_filenames
16739 (objfile, fun, need_fullname));
16740 }
16741
16742 void compute_main_name (struct objfile *objfile) override
16743 {
16744 wait (objfile, false);
16745 }
16746 };
16747
16748 dwarf2_per_cu_data *
16749 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
16750 unrelocated_addr adjusted_pc)
16751 {
16752 cooked_index *table
16753 = (gdb::checked_static_cast<cooked_index *>
16754 (per_bfd->index_table.get ()));
16755 return table->lookup (adjusted_pc);
16756 }
16757
16758 struct compunit_symtab *
16759 cooked_index_functions::find_compunit_symtab_by_address
16760 (struct objfile *objfile, CORE_ADDR address)
16761 {
16762 if (objfile->sect_index_data == -1)
16763 return nullptr;
16764
16765 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16766 cooked_index *table = wait (objfile, true);
16767
16768 CORE_ADDR baseaddr = objfile->data_section_offset ();
16769 dwarf2_per_cu_data *per_cu
16770 = table->lookup ((unrelocated_addr) (address - baseaddr));
16771 if (per_cu == nullptr)
16772 return nullptr;
16773
16774 return dw2_instantiate_symtab (per_cu, per_objfile, false);
16775 }
16776
16777 bool
16778 cooked_index_functions::expand_symtabs_matching
16779 (struct objfile *objfile,
16780 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16781 const lookup_name_info *lookup_name,
16782 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16783 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16784 block_search_flags search_flags,
16785 domain_enum domain,
16786 enum search_domain kind)
16787 {
16788 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16789
16790 cooked_index *table = wait (objfile, true);
16791
16792 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
16793
16794 /* This invariant is documented in quick-functions.h. */
16795 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
16796 if (lookup_name == nullptr)
16797 {
16798 for (dwarf2_per_cu_data *per_cu
16799 : all_units_range (per_objfile->per_bfd))
16800 {
16801 QUIT;
16802
16803 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
16804 file_matcher,
16805 expansion_notify))
16806 return false;
16807 }
16808 return true;
16809 }
16810
16811 lookup_name_info lookup_name_without_params
16812 = lookup_name->make_ignore_params ();
16813 bool completing = lookup_name->completion_mode ();
16814
16815 /* Unique styles of language splitting. */
16816 static const enum language unique_styles[] =
16817 {
16818 /* No splitting is also a style. */
16819 language_c,
16820 /* This includes Rust. */
16821 language_cplus,
16822 /* This includes Go. */
16823 language_d,
16824 language_ada
16825 };
16826
16827 for (enum language lang : unique_styles)
16828 {
16829 std::vector<std::string_view> name_vec
16830 = lookup_name_without_params.split_name (lang);
16831 std::string last_name (name_vec.back ());
16832
16833 for (const cooked_index_entry *entry : table->find (last_name,
16834 completing))
16835 {
16836 QUIT;
16837
16838 /* No need to consider symbols from expanded CUs. */
16839 if (per_objfile->symtab_set_p (entry->per_cu))
16840 continue;
16841
16842 /* If file-matching was done, we don't need to consider
16843 symbols from unmarked CUs. */
16844 if (file_matcher != nullptr && !entry->per_cu->mark)
16845 continue;
16846
16847 /* See if the symbol matches the type filter. */
16848 if (!entry->matches (search_flags)
16849 || !entry->matches (domain)
16850 || !entry->matches (kind))
16851 continue;
16852
16853 /* We've found the base name of the symbol; now walk its
16854 parentage chain, ensuring that each component
16855 matches. */
16856 bool found = true;
16857
16858 const cooked_index_entry *parent = entry->get_parent ();
16859 for (int i = name_vec.size () - 1; i > 0; --i)
16860 {
16861 /* If we ran out of entries, or if this segment doesn't
16862 match, this did not match. */
16863 if (parent == nullptr
16864 || strncmp (parent->name, name_vec[i - 1].data (),
16865 name_vec[i - 1].length ()) != 0)
16866 {
16867 found = false;
16868 break;
16869 }
16870
16871 parent = parent->get_parent ();
16872 }
16873
16874 if (!found)
16875 continue;
16876
16877 /* Might have been looking for "a::b" and found
16878 "x::a::b". */
16879 if (symbol_matcher == nullptr)
16880 {
16881 symbol_name_match_type match_type
16882 = lookup_name_without_params.match_type ();
16883 if ((match_type == symbol_name_match_type::FULL
16884 || (lang != language_ada
16885 && match_type == symbol_name_match_type::EXPRESSION))
16886 && parent != nullptr)
16887 continue;
16888 }
16889 else
16890 {
16891 auto_obstack temp_storage;
16892 const char *full_name = entry->full_name (&temp_storage);
16893 if (!symbol_matcher (full_name))
16894 continue;
16895 }
16896
16897 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
16898 file_matcher,
16899 expansion_notify))
16900 return false;
16901 }
16902 }
16903
16904 return true;
16905 }
16906
16907 /* Return a new cooked_index_functions object. */
16908
16909 static quick_symbol_functions_up
16910 make_cooked_index_funcs (dwarf2_per_objfile *per_objfile)
16911 {
16912 /* Set the index table early so that sharing works even while
16913 scanning; and then start the scanning. */
16914 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
16915 cooked_index *idx = new cooked_index (per_objfile);
16916 per_bfd->index_table.reset (idx);
16917 /* Don't start reading until after 'index_table' is set. This
16918 avoids races. */
16919 idx->start_reading ();
16920
16921 if (dwarf_synchronous)
16922 idx->wait_completely ();
16923
16924 return quick_symbol_functions_up (new cooked_index_functions);
16925 }
16926
16927 quick_symbol_functions_up
16928 cooked_index::make_quick_functions () const
16929 {
16930 return quick_symbol_functions_up (new cooked_index_functions);
16931 }
16932
16933 \f
16934
16935 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
16936 contents from the given SECTION in the HEADER.
16937
16938 HEADER_OFFSET is the offset of the header in the section. */
16939 static void
16940 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
16941 struct dwarf2_section_info *section,
16942 sect_offset header_offset)
16943 {
16944 unsigned int bytes_read;
16945 bfd *abfd = section->get_bfd_owner ();
16946 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
16947
16948 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
16949 info_ptr += bytes_read;
16950
16951 header->version = read_2_bytes (abfd, info_ptr);
16952 info_ptr += 2;
16953
16954 header->addr_size = read_1_byte (abfd, info_ptr);
16955 info_ptr += 1;
16956
16957 header->segment_collector_size = read_1_byte (abfd, info_ptr);
16958 info_ptr += 1;
16959
16960 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
16961 }
16962
16963 /* Return the DW_AT_loclists_base value for the CU. */
16964 static ULONGEST
16965 lookup_loclist_base (struct dwarf2_cu *cu)
16966 {
16967 /* For the .dwo unit, the loclist_base points to the first offset following
16968 the header. The header consists of the following entities-
16969 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
16970 bit format)
16971 2. version (2 bytes)
16972 3. address size (1 byte)
16973 4. segment selector size (1 byte)
16974 5. offset entry count (4 bytes)
16975 These sizes are derived as per the DWARFv5 standard. */
16976 if (cu->dwo_unit != nullptr)
16977 {
16978 if (cu->header.initial_length_size == 4)
16979 return LOCLIST_HEADER_SIZE32;
16980 return LOCLIST_HEADER_SIZE64;
16981 }
16982 return cu->loclist_base;
16983 }
16984
16985 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
16986 array of offsets in the .debug_loclists section. */
16987
16988 static sect_offset
16989 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
16990 {
16991 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16992 struct objfile *objfile = per_objfile->objfile;
16993 bfd *abfd = objfile->obfd.get ();
16994 ULONGEST loclist_header_size =
16995 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
16996 : LOCLIST_HEADER_SIZE64);
16997 ULONGEST loclist_base = lookup_loclist_base (cu);
16998
16999 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
17000 ULONGEST start_offset =
17001 loclist_base + loclist_index * cu->header.offset_size;
17002
17003 /* Get loclists section. */
17004 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
17005
17006 /* Read the loclists section content. */
17007 section->read (objfile);
17008 if (section->buffer == NULL)
17009 error (_("DW_FORM_loclistx used without .debug_loclists "
17010 "section [in module %s]"), objfile_name (objfile));
17011
17012 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
17013 so if loclist_base is smaller than the header size, we have a problem. */
17014 if (loclist_base < loclist_header_size)
17015 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
17016 objfile_name (objfile));
17017
17018 /* Read the header of the loclists contribution. */
17019 struct loclists_rnglists_header header;
17020 read_loclists_rnglists_header (&header, section,
17021 (sect_offset) (loclist_base - loclist_header_size));
17022
17023 /* Verify the loclist index is valid. */
17024 if (loclist_index >= header.offset_entry_count)
17025 error (_("DW_FORM_loclistx pointing outside of "
17026 ".debug_loclists offset array [in module %s]"),
17027 objfile_name (objfile));
17028
17029 /* Validate that reading won't go beyond the end of the section. */
17030 if (start_offset + cu->header.offset_size > section->size)
17031 error (_("Reading DW_FORM_loclistx index beyond end of"
17032 ".debug_loclists section [in module %s]"),
17033 objfile_name (objfile));
17034
17035 const gdb_byte *info_ptr = section->buffer + start_offset;
17036
17037 if (cu->header.offset_size == 4)
17038 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
17039 else
17040 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
17041 }
17042
17043 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
17044 array of offsets in the .debug_rnglists section. */
17045
17046 static sect_offset
17047 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
17048 dwarf_tag tag)
17049 {
17050 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
17051 struct objfile *objfile = dwarf2_per_objfile->objfile;
17052 bfd *abfd = objfile->obfd.get ();
17053 ULONGEST rnglist_header_size =
17054 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
17055 : RNGLIST_HEADER_SIZE64);
17056
17057 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
17058 .debug_rnglists.dwo section. The rnglists base given in the skeleton
17059 doesn't apply. */
17060 ULONGEST rnglist_base =
17061 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
17062
17063 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
17064 ULONGEST start_offset =
17065 rnglist_base + rnglist_index * cu->header.offset_size;
17066
17067 /* Get rnglists section. */
17068 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
17069
17070 /* Read the rnglists section content. */
17071 section->read (objfile);
17072 if (section->buffer == nullptr)
17073 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
17074 "[in module %s]"),
17075 objfile_name (objfile));
17076
17077 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
17078 so if rnglist_base is smaller than the header size, we have a problem. */
17079 if (rnglist_base < rnglist_header_size)
17080 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
17081 objfile_name (objfile));
17082
17083 /* Read the header of the rnglists contribution. */
17084 struct loclists_rnglists_header header;
17085 read_loclists_rnglists_header (&header, section,
17086 (sect_offset) (rnglist_base - rnglist_header_size));
17087
17088 /* Verify the rnglist index is valid. */
17089 if (rnglist_index >= header.offset_entry_count)
17090 error (_("DW_FORM_rnglistx index pointing outside of "
17091 ".debug_rnglists offset array [in module %s]"),
17092 objfile_name (objfile));
17093
17094 /* Validate that reading won't go beyond the end of the section. */
17095 if (start_offset + cu->header.offset_size > section->size)
17096 error (_("Reading DW_FORM_rnglistx index beyond end of"
17097 ".debug_rnglists section [in module %s]"),
17098 objfile_name (objfile));
17099
17100 const gdb_byte *info_ptr = section->buffer + start_offset;
17101
17102 if (cu->header.offset_size == 4)
17103 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
17104 else
17105 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
17106 }
17107
17108 /* Process the attributes that had to be skipped in the first round. These
17109 attributes are the ones that need str_offsets_base or addr_base attributes.
17110 They could not have been processed in the first round, because at the time
17111 the values of str_offsets_base or addr_base may not have been known. */
17112 static void
17113 read_attribute_reprocess (const struct die_reader_specs *reader,
17114 struct attribute *attr, dwarf_tag tag)
17115 {
17116 struct dwarf2_cu *cu = reader->cu;
17117 switch (attr->form)
17118 {
17119 case DW_FORM_addrx:
17120 case DW_FORM_GNU_addr_index:
17121 attr->set_address (read_addr_index (cu,
17122 attr->as_unsigned_reprocess ()));
17123 break;
17124 case DW_FORM_loclistx:
17125 {
17126 sect_offset loclists_sect_off
17127 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
17128
17129 attr->set_unsigned (to_underlying (loclists_sect_off));
17130 }
17131 break;
17132 case DW_FORM_rnglistx:
17133 {
17134 sect_offset rnglists_sect_off
17135 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
17136
17137 attr->set_unsigned (to_underlying (rnglists_sect_off));
17138 }
17139 break;
17140 case DW_FORM_strx:
17141 case DW_FORM_strx1:
17142 case DW_FORM_strx2:
17143 case DW_FORM_strx3:
17144 case DW_FORM_strx4:
17145 case DW_FORM_GNU_str_index:
17146 {
17147 unsigned int str_index = attr->as_unsigned_reprocess ();
17148 gdb_assert (!attr->canonical_string_p ());
17149 if (reader->dwo_file != NULL)
17150 attr->set_string_noncanonical (read_dwo_str_index (reader,
17151 str_index));
17152 else
17153 attr->set_string_noncanonical (read_stub_str_index (cu,
17154 str_index));
17155 break;
17156 }
17157 default:
17158 gdb_assert_not_reached ("Unexpected DWARF form.");
17159 }
17160 }
17161
17162 /* Read an attribute value described by an attribute form. */
17163
17164 static const gdb_byte *
17165 read_attribute_value (const struct die_reader_specs *reader,
17166 struct attribute *attr, unsigned form,
17167 LONGEST implicit_const, const gdb_byte *info_ptr,
17168 bool allow_reprocess)
17169 {
17170 struct dwarf2_cu *cu = reader->cu;
17171 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17172 struct objfile *objfile = per_objfile->objfile;
17173 bfd *abfd = reader->abfd;
17174 struct comp_unit_head *cu_header = &cu->header;
17175 unsigned int bytes_read;
17176 struct dwarf_block *blk;
17177
17178 attr->form = (enum dwarf_form) form;
17179 switch (form)
17180 {
17181 case DW_FORM_ref_addr:
17182 if (cu_header->version == 2)
17183 attr->set_unsigned ((ULONGEST) cu_header->read_address (abfd, info_ptr,
17184 &bytes_read));
17185 else
17186 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17187 &bytes_read));
17188 info_ptr += bytes_read;
17189 break;
17190 case DW_FORM_GNU_ref_alt:
17191 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17192 &bytes_read));
17193 info_ptr += bytes_read;
17194 break;
17195 case DW_FORM_addr:
17196 {
17197 unrelocated_addr addr = cu_header->read_address (abfd, info_ptr,
17198 &bytes_read);
17199 addr = per_objfile->adjust (addr);
17200 attr->set_address (addr);
17201 info_ptr += bytes_read;
17202 }
17203 break;
17204 case DW_FORM_block2:
17205 blk = dwarf_alloc_block (cu);
17206 blk->size = read_2_bytes (abfd, info_ptr);
17207 info_ptr += 2;
17208 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17209 info_ptr += blk->size;
17210 attr->set_block (blk);
17211 break;
17212 case DW_FORM_block4:
17213 blk = dwarf_alloc_block (cu);
17214 blk->size = read_4_bytes (abfd, info_ptr);
17215 info_ptr += 4;
17216 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17217 info_ptr += blk->size;
17218 attr->set_block (blk);
17219 break;
17220 case DW_FORM_data2:
17221 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
17222 info_ptr += 2;
17223 break;
17224 case DW_FORM_data4:
17225 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
17226 info_ptr += 4;
17227 break;
17228 case DW_FORM_data8:
17229 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
17230 info_ptr += 8;
17231 break;
17232 case DW_FORM_data16:
17233 blk = dwarf_alloc_block (cu);
17234 blk->size = 16;
17235 blk->data = read_n_bytes (abfd, info_ptr, 16);
17236 info_ptr += 16;
17237 attr->set_block (blk);
17238 break;
17239 case DW_FORM_sec_offset:
17240 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17241 &bytes_read));
17242 info_ptr += bytes_read;
17243 break;
17244 case DW_FORM_loclistx:
17245 {
17246 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17247 &bytes_read));
17248 info_ptr += bytes_read;
17249 if (allow_reprocess)
17250 read_attribute_reprocess (reader, attr);
17251 }
17252 break;
17253 case DW_FORM_string:
17254 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
17255 &bytes_read));
17256 info_ptr += bytes_read;
17257 break;
17258 case DW_FORM_strp:
17259 if (!cu->per_cu->is_dwz)
17260 {
17261 attr->set_string_noncanonical
17262 (read_indirect_string (per_objfile,
17263 abfd, info_ptr, cu_header,
17264 &bytes_read));
17265 info_ptr += bytes_read;
17266 break;
17267 }
17268 [[fallthrough]];
17269 case DW_FORM_line_strp:
17270 if (!cu->per_cu->is_dwz)
17271 {
17272 attr->set_string_noncanonical
17273 (per_objfile->read_line_string (info_ptr, cu_header,
17274 &bytes_read));
17275 info_ptr += bytes_read;
17276 break;
17277 }
17278 [[fallthrough]];
17279 case DW_FORM_GNU_strp_alt:
17280 {
17281 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17282 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
17283 &bytes_read);
17284
17285 attr->set_string_noncanonical
17286 (dwz->read_string (objfile, str_offset));
17287 info_ptr += bytes_read;
17288 }
17289 break;
17290 case DW_FORM_exprloc:
17291 case DW_FORM_block:
17292 blk = dwarf_alloc_block (cu);
17293 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17294 info_ptr += bytes_read;
17295 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17296 info_ptr += blk->size;
17297 attr->set_block (blk);
17298 break;
17299 case DW_FORM_block1:
17300 blk = dwarf_alloc_block (cu);
17301 blk->size = read_1_byte (abfd, info_ptr);
17302 info_ptr += 1;
17303 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17304 info_ptr += blk->size;
17305 attr->set_block (blk);
17306 break;
17307 case DW_FORM_data1:
17308 case DW_FORM_flag:
17309 attr->set_unsigned (read_1_byte (abfd, info_ptr));
17310 info_ptr += 1;
17311 break;
17312 case DW_FORM_flag_present:
17313 attr->set_unsigned (1);
17314 break;
17315 case DW_FORM_sdata:
17316 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
17317 info_ptr += bytes_read;
17318 break;
17319 case DW_FORM_rnglistx:
17320 {
17321 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17322 &bytes_read));
17323 info_ptr += bytes_read;
17324 if (allow_reprocess)
17325 read_attribute_reprocess (reader, attr);
17326 }
17327 break;
17328 case DW_FORM_udata:
17329 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17330 info_ptr += bytes_read;
17331 break;
17332 case DW_FORM_ref1:
17333 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17334 + read_1_byte (abfd, info_ptr)));
17335 info_ptr += 1;
17336 break;
17337 case DW_FORM_ref2:
17338 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17339 + read_2_bytes (abfd, info_ptr)));
17340 info_ptr += 2;
17341 break;
17342 case DW_FORM_ref4:
17343 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17344 + read_4_bytes (abfd, info_ptr)));
17345 info_ptr += 4;
17346 break;
17347 case DW_FORM_ref8:
17348 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17349 + read_8_bytes (abfd, info_ptr)));
17350 info_ptr += 8;
17351 break;
17352 case DW_FORM_ref_sig8:
17353 attr->set_signature (read_8_bytes (abfd, info_ptr));
17354 info_ptr += 8;
17355 break;
17356 case DW_FORM_ref_udata:
17357 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17358 + read_unsigned_leb128 (abfd, info_ptr,
17359 &bytes_read)));
17360 info_ptr += bytes_read;
17361 break;
17362 case DW_FORM_indirect:
17363 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17364 info_ptr += bytes_read;
17365 if (form == DW_FORM_implicit_const)
17366 {
17367 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17368 info_ptr += bytes_read;
17369 }
17370 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17371 info_ptr, allow_reprocess);
17372 break;
17373 case DW_FORM_implicit_const:
17374 attr->set_signed (implicit_const);
17375 break;
17376 case DW_FORM_addrx:
17377 case DW_FORM_GNU_addr_index:
17378 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17379 &bytes_read));
17380 info_ptr += bytes_read;
17381 if (allow_reprocess)
17382 read_attribute_reprocess (reader, attr);
17383 break;
17384 case DW_FORM_strx:
17385 case DW_FORM_strx1:
17386 case DW_FORM_strx2:
17387 case DW_FORM_strx3:
17388 case DW_FORM_strx4:
17389 case DW_FORM_GNU_str_index:
17390 {
17391 ULONGEST str_index;
17392 if (form == DW_FORM_strx1)
17393 {
17394 str_index = read_1_byte (abfd, info_ptr);
17395 info_ptr += 1;
17396 }
17397 else if (form == DW_FORM_strx2)
17398 {
17399 str_index = read_2_bytes (abfd, info_ptr);
17400 info_ptr += 2;
17401 }
17402 else if (form == DW_FORM_strx3)
17403 {
17404 str_index = read_3_bytes (abfd, info_ptr);
17405 info_ptr += 3;
17406 }
17407 else if (form == DW_FORM_strx4)
17408 {
17409 str_index = read_4_bytes (abfd, info_ptr);
17410 info_ptr += 4;
17411 }
17412 else
17413 {
17414 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17415 info_ptr += bytes_read;
17416 }
17417 attr->set_unsigned_reprocess (str_index);
17418 if (allow_reprocess)
17419 read_attribute_reprocess (reader, attr);
17420 }
17421 break;
17422 default:
17423 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17424 dwarf_form_name (form),
17425 bfd_get_filename (abfd));
17426 }
17427
17428 /* Super hack. */
17429 if (cu->per_cu->is_dwz && attr->form_is_ref ())
17430 attr->form = DW_FORM_GNU_ref_alt;
17431
17432 /* We have seen instances where the compiler tried to emit a byte
17433 size attribute of -1 which ended up being encoded as an unsigned
17434 0xffffffff. Although 0xffffffff is technically a valid size value,
17435 an object of this size seems pretty unlikely so we can relatively
17436 safely treat these cases as if the size attribute was invalid and
17437 treat them as zero by default. */
17438 if (attr->name == DW_AT_byte_size
17439 && form == DW_FORM_data4
17440 && attr->as_unsigned () >= 0xffffffff)
17441 {
17442 complaint
17443 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17444 hex_string (attr->as_unsigned ()));
17445 attr->set_unsigned (0);
17446 }
17447
17448 return info_ptr;
17449 }
17450
17451 /* Read an attribute described by an abbreviated attribute. */
17452
17453 static const gdb_byte *
17454 read_attribute (const struct die_reader_specs *reader,
17455 struct attribute *attr, const struct attr_abbrev *abbrev,
17456 const gdb_byte *info_ptr,
17457 bool allow_reprocess)
17458 {
17459 attr->name = abbrev->name;
17460 attr->string_is_canonical = 0;
17461 return read_attribute_value (reader, attr, abbrev->form,
17462 abbrev->implicit_const, info_ptr,
17463 allow_reprocess);
17464 }
17465
17466 /* See read.h. */
17467
17468 const char *
17469 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
17470 LONGEST str_offset)
17471 {
17472 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
17473 str_offset, "DW_FORM_strp");
17474 }
17475
17476 /* Return pointer to string at .debug_str offset as read from BUF.
17477 BUF is assumed to be in a compilation unit described by CU_HEADER.
17478 Return *BYTES_READ_PTR count of bytes read from BUF. */
17479
17480 static const char *
17481 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
17482 const gdb_byte *buf,
17483 const struct comp_unit_head *cu_header,
17484 unsigned int *bytes_read_ptr)
17485 {
17486 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17487
17488 return read_indirect_string_at_offset (per_objfile, str_offset);
17489 }
17490
17491 /* See read.h. */
17492
17493 const char *
17494 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17495 unsigned int offset_size)
17496 {
17497 bfd *abfd = objfile->obfd.get ();
17498 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
17499
17500 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17501 }
17502
17503 /* See read.h. */
17504
17505 const char *
17506 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17507 const struct comp_unit_head *cu_header,
17508 unsigned int *bytes_read_ptr)
17509 {
17510 bfd *abfd = objfile->obfd.get ();
17511 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17512
17513 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17514 }
17515
17516 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17517 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
17518 ADDR_SIZE is the size of addresses from the CU header. */
17519
17520 static unrelocated_addr
17521 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
17522 std::optional<ULONGEST> addr_base, int addr_size)
17523 {
17524 struct objfile *objfile = per_objfile->objfile;
17525 bfd *abfd = objfile->obfd.get ();
17526 const gdb_byte *info_ptr;
17527 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
17528
17529 per_objfile->per_bfd->addr.read (objfile);
17530 if (per_objfile->per_bfd->addr.buffer == NULL)
17531 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17532 objfile_name (objfile));
17533 if (addr_base_or_zero + addr_index * addr_size
17534 >= per_objfile->per_bfd->addr.size)
17535 error (_("DW_FORM_addr_index pointing outside of "
17536 ".debug_addr section [in module %s]"),
17537 objfile_name (objfile));
17538 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
17539 + addr_index * addr_size);
17540 if (addr_size == 4)
17541 return (unrelocated_addr) bfd_get_32 (abfd, info_ptr);
17542 else
17543 return (unrelocated_addr) bfd_get_64 (abfd, info_ptr);
17544 }
17545
17546 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17547
17548 static unrelocated_addr
17549 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17550 {
17551 return read_addr_index_1 (cu->per_objfile, addr_index,
17552 cu->addr_base, cu->header.addr_size);
17553 }
17554
17555 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17556
17557 static unrelocated_addr
17558 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17559 unsigned int *bytes_read)
17560 {
17561 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
17562 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17563
17564 return read_addr_index (cu, addr_index);
17565 }
17566
17567 /* See read.h. */
17568
17569 unrelocated_addr
17570 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
17571 dwarf2_per_objfile *per_objfile,
17572 unsigned int addr_index)
17573 {
17574 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
17575 std::optional<ULONGEST> addr_base;
17576 int addr_size;
17577
17578 /* We need addr_base and addr_size.
17579 If we don't have PER_CU->cu, we have to get it.
17580 Nasty, but the alternative is storing the needed info in PER_CU,
17581 which at this point doesn't seem justified: it's not clear how frequently
17582 it would get used and it would increase the size of every PER_CU.
17583 Entry points like dwarf2_per_cu_addr_size do a similar thing
17584 so we're not in uncharted territory here.
17585 Alas we need to be a bit more complicated as addr_base is contained
17586 in the DIE.
17587
17588 We don't need to read the entire CU(/TU).
17589 We just need the header and top level die.
17590
17591 IWBN to use the aging mechanism to let us lazily later discard the CU.
17592 For now we skip this optimization. */
17593
17594 if (cu != NULL)
17595 {
17596 addr_base = cu->addr_base;
17597 addr_size = cu->header.addr_size;
17598 }
17599 else
17600 {
17601 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
17602 addr_base = reader.cu->addr_base;
17603 addr_size = reader.cu->header.addr_size;
17604 }
17605
17606 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
17607 }
17608
17609 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
17610 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
17611 DWO file. */
17612
17613 static const char *
17614 read_str_index (struct dwarf2_cu *cu,
17615 struct dwarf2_section_info *str_section,
17616 struct dwarf2_section_info *str_offsets_section,
17617 ULONGEST str_offsets_base, ULONGEST str_index,
17618 unsigned offset_size)
17619 {
17620 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17621 struct objfile *objfile = per_objfile->objfile;
17622 const char *objf_name = objfile_name (objfile);
17623 bfd *abfd = objfile->obfd.get ();
17624 const gdb_byte *info_ptr;
17625 ULONGEST str_offset;
17626 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
17627
17628 str_section->read (objfile);
17629 str_offsets_section->read (objfile);
17630 if (str_section->buffer == NULL)
17631 error (_("%s used without %s section"
17632 " in CU at offset %s [in module %s]"),
17633 form_name, str_section->get_name (),
17634 sect_offset_str (cu->header.sect_off), objf_name);
17635 if (str_offsets_section->buffer == NULL)
17636 error (_("%s used without %s section"
17637 " in CU at offset %s [in module %s]"),
17638 form_name, str_section->get_name (),
17639 sect_offset_str (cu->header.sect_off), objf_name);
17640 info_ptr = (str_offsets_section->buffer
17641 + str_offsets_base
17642 + str_index * offset_size);
17643 if (offset_size == 4)
17644 str_offset = bfd_get_32 (abfd, info_ptr);
17645 else
17646 str_offset = bfd_get_64 (abfd, info_ptr);
17647 if (str_offset >= str_section->size)
17648 error (_("Offset from %s pointing outside of"
17649 " .debug_str.dwo section in CU at offset %s [in module %s]"),
17650 form_name, sect_offset_str (cu->header.sect_off), objf_name);
17651 return (const char *) (str_section->buffer + str_offset);
17652 }
17653
17654 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
17655
17656 static const char *
17657 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17658 {
17659 unsigned offset_size;
17660 ULONGEST str_offsets_base;
17661 if (reader->cu->header.version >= 5)
17662 {
17663 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
17664 so assume the .debug_str_offsets section is DWARF5 as well, and
17665 parse the header. FIXME: Parse the header only once. */
17666 unsigned int bytes_read = 0;
17667 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
17668 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
17669
17670 /* Header: Initial length. */
17671 read_initial_length (abfd, p + bytes_read, &bytes_read);
17672
17673 /* Determine offset_size based on the .debug_str_offsets header. */
17674 const bool dwarf5_is_dwarf64 = bytes_read != 4;
17675 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
17676
17677 /* Header: Version. */
17678 unsigned version = read_2_bytes (abfd, p + bytes_read);
17679 bytes_read += 2;
17680
17681 if (version <= 4)
17682 {
17683 /* We'd like one warning here about ignoring the section, but
17684 because we parse the header more than once (see FIXME above)
17685 we'd have many warnings, so use a complaint instead, which at
17686 least has a limit. */
17687 complaint (_("Section .debug_str_offsets in %s has unsupported"
17688 " version %d, use empty string."),
17689 reader->dwo_file->dwo_name.c_str (), version);
17690 return "";
17691 }
17692
17693 /* Header: Padding. */
17694 bytes_read += 2;
17695
17696 str_offsets_base = bytes_read;
17697 }
17698 else
17699 {
17700 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
17701 section, assume the .debug_str_offsets section is pre-DWARF5 as
17702 well, which doesn't have a header. */
17703 str_offsets_base = 0;
17704
17705 /* Determine offset_size based on the .debug_info header. */
17706 offset_size = reader->cu->header.offset_size;
17707 }
17708
17709 return read_str_index (reader->cu,
17710 &reader->dwo_file->sections.str,
17711 &reader->dwo_file->sections.str_offsets,
17712 str_offsets_base, str_index, offset_size);
17713 }
17714
17715 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
17716
17717 static const char *
17718 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
17719 {
17720 struct objfile *objfile = cu->per_objfile->objfile;
17721 const char *objf_name = objfile_name (objfile);
17722 static const char form_name[] = "DW_FORM_GNU_str_index";
17723 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
17724
17725 if (!cu->str_offsets_base.has_value ())
17726 error (_("%s used in Fission stub without %s"
17727 " in CU at offset 0x%lx [in module %s]"),
17728 form_name, str_offsets_attr_name,
17729 (long) cu->header.offset_size, objf_name);
17730
17731 return read_str_index (cu,
17732 &cu->per_objfile->per_bfd->str,
17733 &cu->per_objfile->per_bfd->str_offsets,
17734 *cu->str_offsets_base, str_index,
17735 cu->header.offset_size);
17736 }
17737
17738 /* Return the length of an LEB128 number in BUF. */
17739
17740 static int
17741 leb128_size (const gdb_byte *buf)
17742 {
17743 const gdb_byte *begin = buf;
17744 gdb_byte byte;
17745
17746 while (1)
17747 {
17748 byte = *buf++;
17749 if ((byte & 128) == 0)
17750 return buf - begin;
17751 }
17752 }
17753
17754 /* Converts DWARF language names to GDB language names. */
17755
17756 enum language
17757 dwarf_lang_to_enum_language (unsigned int lang)
17758 {
17759 enum language language;
17760
17761 switch (lang)
17762 {
17763 case DW_LANG_C89:
17764 case DW_LANG_C99:
17765 case DW_LANG_C11:
17766 case DW_LANG_C:
17767 case DW_LANG_UPC:
17768 language = language_c;
17769 break;
17770 case DW_LANG_Java:
17771 case DW_LANG_C_plus_plus:
17772 case DW_LANG_C_plus_plus_11:
17773 case DW_LANG_C_plus_plus_14:
17774 language = language_cplus;
17775 break;
17776 case DW_LANG_D:
17777 language = language_d;
17778 break;
17779 case DW_LANG_Fortran77:
17780 case DW_LANG_Fortran90:
17781 case DW_LANG_Fortran95:
17782 case DW_LANG_Fortran03:
17783 case DW_LANG_Fortran08:
17784 language = language_fortran;
17785 break;
17786 case DW_LANG_Go:
17787 language = language_go;
17788 break;
17789 case DW_LANG_Mips_Assembler:
17790 language = language_asm;
17791 break;
17792 case DW_LANG_Ada83:
17793 case DW_LANG_Ada95:
17794 language = language_ada;
17795 break;
17796 case DW_LANG_Modula2:
17797 language = language_m2;
17798 break;
17799 case DW_LANG_Pascal83:
17800 language = language_pascal;
17801 break;
17802 case DW_LANG_ObjC:
17803 language = language_objc;
17804 break;
17805 case DW_LANG_Rust:
17806 case DW_LANG_Rust_old:
17807 language = language_rust;
17808 break;
17809 case DW_LANG_OpenCL:
17810 language = language_opencl;
17811 break;
17812 case DW_LANG_Cobol74:
17813 case DW_LANG_Cobol85:
17814 default:
17815 language = language_minimal;
17816 break;
17817 }
17818
17819 return language;
17820 }
17821
17822 /* Return the named attribute or NULL if not there. */
17823
17824 static struct attribute *
17825 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17826 {
17827 for (;;)
17828 {
17829 unsigned int i;
17830 struct attribute *spec = NULL;
17831
17832 for (i = 0; i < die->num_attrs; ++i)
17833 {
17834 if (die->attrs[i].name == name)
17835 return &die->attrs[i];
17836 if (die->attrs[i].name == DW_AT_specification
17837 || die->attrs[i].name == DW_AT_abstract_origin)
17838 spec = &die->attrs[i];
17839 }
17840
17841 if (!spec)
17842 break;
17843
17844 struct die_info *prev_die = die;
17845 die = follow_die_ref (die, spec, &cu);
17846 if (die == prev_die)
17847 /* Self-reference, we're done. */
17848 break;
17849 }
17850
17851 return NULL;
17852 }
17853
17854 /* Return the string associated with a string-typed attribute, or NULL if it
17855 is either not found or is of an incorrect type. */
17856
17857 static const char *
17858 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17859 {
17860 struct attribute *attr;
17861 const char *str = NULL;
17862
17863 attr = dwarf2_attr (die, name, cu);
17864
17865 if (attr != NULL)
17866 {
17867 str = attr->as_string ();
17868 if (str == nullptr)
17869 complaint (_("string type expected for attribute %s for "
17870 "DIE at %s in module %s"),
17871 dwarf_attr_name (name), sect_offset_str (die->sect_off),
17872 objfile_name (cu->per_objfile->objfile));
17873 }
17874
17875 return str;
17876 }
17877
17878 /* Return the dwo name or NULL if not present. If present, it is in either
17879 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
17880 static const char *
17881 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
17882 {
17883 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
17884 if (dwo_name == nullptr)
17885 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
17886 return dwo_name;
17887 }
17888
17889 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17890 and holds a non-zero value. This function should only be used for
17891 DW_FORM_flag or DW_FORM_flag_present attributes. */
17892
17893 static int
17894 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17895 {
17896 struct attribute *attr = dwarf2_attr (die, name, cu);
17897
17898 return attr != nullptr && attr->as_boolean ();
17899 }
17900
17901 static int
17902 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17903 {
17904 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17905 which value is non-zero. However, we have to be careful with
17906 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17907 (via dwarf2_flag_true_p) follows this attribute. So we may
17908 end up accidently finding a declaration attribute that belongs
17909 to a different DIE referenced by the specification attribute,
17910 even though the given DIE does not have a declaration attribute. */
17911 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17912 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17913 }
17914
17915 /* Return the die giving the specification for DIE, if there is
17916 one. *SPEC_CU is the CU containing DIE on input, and the CU
17917 containing the return value on output. If there is no
17918 specification, but there is an abstract origin, that is
17919 returned. */
17920
17921 static struct die_info *
17922 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17923 {
17924 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17925 *spec_cu);
17926
17927 if (spec_attr == NULL)
17928 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17929
17930 if (spec_attr == NULL)
17931 return NULL;
17932 else
17933 return follow_die_ref (die, spec_attr, spec_cu);
17934 }
17935
17936 /* A convenience function to find the proper .debug_line section for a CU. */
17937
17938 static struct dwarf2_section_info *
17939 get_debug_line_section (struct dwarf2_cu *cu)
17940 {
17941 struct dwarf2_section_info *section;
17942 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17943
17944 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17945 DWO file. */
17946 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17947 section = &cu->dwo_unit->dwo_file->sections.line;
17948 else if (cu->per_cu->is_dwz)
17949 {
17950 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17951
17952 section = &dwz->line;
17953 }
17954 else
17955 section = &per_objfile->per_bfd->line;
17956
17957 return section;
17958 }
17959
17960 /* Read the statement program header starting at OFFSET in
17961 .debug_line, or .debug_line.dwo. Return a pointer
17962 to a struct line_header, allocated using xmalloc.
17963 Returns NULL if there is a problem reading the header, e.g., if it
17964 has a version we don't understand.
17965
17966 NOTE: the strings in the include directory and file name tables of
17967 the returned object point into the dwarf line section buffer,
17968 and must not be freed. */
17969
17970 static line_header_up
17971 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
17972 const char *comp_dir)
17973 {
17974 struct dwarf2_section_info *section;
17975 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17976
17977 section = get_debug_line_section (cu);
17978 section->read (per_objfile->objfile);
17979 if (section->buffer == NULL)
17980 {
17981 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17982 complaint (_("missing .debug_line.dwo section"));
17983 else
17984 complaint (_("missing .debug_line section"));
17985 return 0;
17986 }
17987
17988 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
17989 per_objfile, section, &cu->header,
17990 comp_dir);
17991 }
17992
17993 /* Subroutine of dwarf_decode_lines to simplify it.
17994 Return the file name for the given file_entry.
17995 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
17996 If space for the result is malloc'd, *NAME_HOLDER will be set.
17997 Returns NULL if FILE_INDEX should be ignored, i.e., it is
17998 equivalent to CU_INFO. */
17999
18000 static const char *
18001 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
18002 const file_and_directory &cu_info,
18003 std::string &name_holder)
18004 {
18005 const char *include_name = fe.name;
18006 const char *include_name_to_compare = include_name;
18007
18008 const char *dir_name = fe.include_dir (lh);
18009
18010 std::string hold_compare;
18011 if (!IS_ABSOLUTE_PATH (include_name)
18012 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
18013 {
18014 /* Avoid creating a duplicate name for CU_INFO.
18015 We do this by comparing INCLUDE_NAME and CU_INFO.
18016 Before we do the comparison, however, we need to account
18017 for DIR_NAME and COMP_DIR.
18018 First prepend dir_name (if non-NULL). If we still don't
18019 have an absolute path prepend comp_dir (if non-NULL).
18020 However, the directory we record in the include-file's
18021 psymtab does not contain COMP_DIR (to match the
18022 corresponding symtab(s)).
18023
18024 Example:
18025
18026 bash$ cd /tmp
18027 bash$ gcc -g ./hello.c
18028 include_name = "hello.c"
18029 dir_name = "."
18030 DW_AT_comp_dir = comp_dir = "/tmp"
18031 DW_AT_name = "./hello.c"
18032
18033 */
18034
18035 if (dir_name != NULL)
18036 {
18037 name_holder = path_join (dir_name, include_name);
18038 include_name = name_holder.c_str ();
18039 include_name_to_compare = include_name;
18040 }
18041 if (!IS_ABSOLUTE_PATH (include_name)
18042 && cu_info.get_comp_dir () != nullptr)
18043 {
18044 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
18045 include_name_to_compare = hold_compare.c_str ();
18046 }
18047 }
18048
18049 std::string copied_name;
18050 const char *cu_filename = cu_info.get_name ();
18051 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
18052 {
18053 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
18054 cu_filename = copied_name.c_str ();
18055 }
18056
18057 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
18058 return nullptr;
18059 return include_name;
18060 }
18061
18062 /* State machine to track the state of the line number program. */
18063
18064 class lnp_state_machine
18065 {
18066 public:
18067 /* Initialize a machine state for the start of a line number
18068 program. */
18069 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
18070
18071 file_entry *current_file ()
18072 {
18073 /* lh->file_names is 0-based, but the file name numbers in the
18074 statement program are 1-based. */
18075 return m_line_header->file_name_at (m_file);
18076 }
18077
18078 /* Record the line in the state machine. END_SEQUENCE is true if
18079 we're processing the end of a sequence. */
18080 void record_line (bool end_sequence);
18081
18082 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
18083 nop-out rest of the lines in this sequence. */
18084 void check_line_address (struct dwarf2_cu *cu,
18085 const gdb_byte *line_ptr,
18086 unrelocated_addr unrelocated_lowpc,
18087 unrelocated_addr address);
18088
18089 void handle_set_discriminator (unsigned int discriminator)
18090 {
18091 m_discriminator = discriminator;
18092 m_line_has_non_zero_discriminator |= discriminator != 0;
18093 }
18094
18095 /* Handle DW_LNE_set_address. */
18096 void handle_set_address (unrelocated_addr address)
18097 {
18098 m_op_index = 0;
18099 m_address
18100 = (unrelocated_addr) gdbarch_adjust_dwarf2_line (m_gdbarch,
18101 (CORE_ADDR) address,
18102 false);
18103 }
18104
18105 /* Handle DW_LNS_advance_pc. */
18106 void handle_advance_pc (CORE_ADDR adjust);
18107
18108 /* Handle a special opcode. */
18109 void handle_special_opcode (unsigned char op_code);
18110
18111 /* Handle DW_LNS_advance_line. */
18112 void handle_advance_line (int line_delta)
18113 {
18114 advance_line (line_delta);
18115 }
18116
18117 /* Handle DW_LNS_set_file. */
18118 void handle_set_file (file_name_index file);
18119
18120 /* Handle DW_LNS_negate_stmt. */
18121 void handle_negate_stmt ()
18122 {
18123 m_flags ^= LEF_IS_STMT;
18124 }
18125
18126 /* Handle DW_LNS_const_add_pc. */
18127 void handle_const_add_pc ();
18128
18129 /* Handle DW_LNS_fixed_advance_pc. */
18130 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18131 {
18132 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18133 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
18134 m_op_index = 0;
18135 }
18136
18137 /* Handle DW_LNS_copy. */
18138 void handle_copy ()
18139 {
18140 record_line (false);
18141 m_discriminator = 0;
18142 m_flags &= ~LEF_PROLOGUE_END;
18143 m_flags &= ~LEF_EPILOGUE_BEGIN;
18144 }
18145
18146 /* Handle DW_LNE_end_sequence. */
18147 void handle_end_sequence ()
18148 {
18149 m_currently_recording_lines = true;
18150 }
18151
18152 /* Handle DW_LNS_set_prologue_end. */
18153 void handle_set_prologue_end ()
18154 {
18155 m_flags |= LEF_PROLOGUE_END;
18156 }
18157
18158 void handle_set_epilogue_begin ()
18159 {
18160 m_flags |= LEF_EPILOGUE_BEGIN;
18161 }
18162
18163 private:
18164 /* Advance the line by LINE_DELTA. */
18165 void advance_line (int line_delta)
18166 {
18167 m_line += line_delta;
18168
18169 if (line_delta != 0)
18170 m_line_has_non_zero_discriminator = m_discriminator != 0;
18171 }
18172
18173 struct dwarf2_cu *m_cu;
18174
18175 gdbarch *m_gdbarch;
18176
18177 /* The line number header. */
18178 line_header *m_line_header;
18179
18180 /* These are part of the standard DWARF line number state machine,
18181 and initialized according to the DWARF spec. */
18182
18183 unsigned char m_op_index = 0;
18184 /* The line table index of the current file. */
18185 file_name_index m_file = 1;
18186 unsigned int m_line = 1;
18187
18188 /* These are initialized in the constructor. */
18189
18190 unrelocated_addr m_address;
18191 linetable_entry_flags m_flags;
18192 unsigned int m_discriminator = 0;
18193
18194 /* Additional bits of state we need to track. */
18195
18196 /* The last file that we called dwarf2_start_subfile for.
18197 This is only used for TLLs. */
18198 unsigned int m_last_file = 0;
18199 /* The last file a line number was recorded for. */
18200 struct subfile *m_last_subfile = NULL;
18201
18202 /* The address of the last line entry. */
18203 unrelocated_addr m_last_address;
18204
18205 /* Set to true when a previous line at the same address (using
18206 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
18207 when a line entry at a new address (m_address different to
18208 m_last_address) is processed. */
18209 bool m_stmt_at_address = false;
18210
18211 /* When true, record the lines we decode. */
18212 bool m_currently_recording_lines = true;
18213
18214 /* The last line number that was recorded, used to coalesce
18215 consecutive entries for the same line. This can happen, for
18216 example, when discriminators are present. PR 17276. */
18217 unsigned int m_last_line = 0;
18218 bool m_line_has_non_zero_discriminator = false;
18219 };
18220
18221 void
18222 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18223 {
18224 CORE_ADDR addr_adj = (((m_op_index + adjust)
18225 / m_line_header->maximum_ops_per_instruction)
18226 * m_line_header->minimum_instruction_length);
18227 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18228 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
18229 m_op_index = ((m_op_index + adjust)
18230 % m_line_header->maximum_ops_per_instruction);
18231 }
18232
18233 void
18234 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18235 {
18236 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18237 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
18238 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
18239 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
18240 / m_line_header->maximum_ops_per_instruction)
18241 * m_line_header->minimum_instruction_length);
18242 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18243 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
18244 m_op_index = ((m_op_index + adj_opcode_d)
18245 % m_line_header->maximum_ops_per_instruction);
18246
18247 int line_delta = m_line_header->line_base + adj_opcode_r;
18248 advance_line (line_delta);
18249 record_line (false);
18250 m_discriminator = 0;
18251 m_flags &= ~LEF_PROLOGUE_END;
18252 m_flags &= ~LEF_EPILOGUE_BEGIN;
18253 }
18254
18255 void
18256 lnp_state_machine::handle_set_file (file_name_index file)
18257 {
18258 m_file = file;
18259
18260 const file_entry *fe = current_file ();
18261 if (fe == NULL)
18262 dwarf2_debug_line_missing_file_complaint ();
18263 else
18264 {
18265 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18266 m_line_has_non_zero_discriminator = m_discriminator != 0;
18267 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
18268 }
18269 }
18270
18271 void
18272 lnp_state_machine::handle_const_add_pc ()
18273 {
18274 CORE_ADDR adjust
18275 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18276
18277 CORE_ADDR addr_adj
18278 = (((m_op_index + adjust)
18279 / m_line_header->maximum_ops_per_instruction)
18280 * m_line_header->minimum_instruction_length);
18281
18282 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18283 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
18284 m_op_index = ((m_op_index + adjust)
18285 % m_line_header->maximum_ops_per_instruction);
18286 }
18287
18288 /* Return non-zero if we should add LINE to the line number table.
18289 LINE is the line to add, LAST_LINE is the last line that was added,
18290 LAST_SUBFILE is the subfile for LAST_LINE.
18291 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18292 had a non-zero discriminator.
18293
18294 We have to be careful in the presence of discriminators.
18295 E.g., for this line:
18296
18297 for (i = 0; i < 100000; i++);
18298
18299 clang can emit four line number entries for that one line,
18300 each with a different discriminator.
18301 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18302
18303 However, we want gdb to coalesce all four entries into one.
18304 Otherwise the user could stepi into the middle of the line and
18305 gdb would get confused about whether the pc really was in the
18306 middle of the line.
18307
18308 Things are further complicated by the fact that two consecutive
18309 line number entries for the same line is a heuristic used by gcc
18310 to denote the end of the prologue. So we can't just discard duplicate
18311 entries, we have to be selective about it. The heuristic we use is
18312 that we only collapse consecutive entries for the same line if at least
18313 one of those entries has a non-zero discriminator. PR 17276.
18314
18315 Note: Addresses in the line number state machine can never go backwards
18316 within one sequence, thus this coalescing is ok. */
18317
18318 static int
18319 dwarf_record_line_p (struct dwarf2_cu *cu,
18320 unsigned int line, unsigned int last_line,
18321 int line_has_non_zero_discriminator,
18322 struct subfile *last_subfile)
18323 {
18324 if (cu->get_builder ()->get_current_subfile () != last_subfile)
18325 return 1;
18326 if (line != last_line)
18327 return 1;
18328 /* Same line for the same file that we've seen already.
18329 As a last check, for pr 17276, only record the line if the line
18330 has never had a non-zero discriminator. */
18331 if (!line_has_non_zero_discriminator)
18332 return 1;
18333 return 0;
18334 }
18335
18336 /* Use the CU's builder to record line number LINE beginning at
18337 address ADDRESS in the line table of subfile SUBFILE. */
18338
18339 static void
18340 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18341 unsigned int line, unrelocated_addr address,
18342 linetable_entry_flags flags,
18343 struct dwarf2_cu *cu)
18344 {
18345 unrelocated_addr addr
18346 = unrelocated_addr (gdbarch_addr_bits_remove (gdbarch,
18347 (CORE_ADDR) address));
18348
18349 if (dwarf_line_debug)
18350 {
18351 gdb_printf (gdb_stdlog,
18352 "Recording line %u, file %s, address %s\n",
18353 line, lbasename (subfile->name.c_str ()),
18354 paddress (gdbarch, (CORE_ADDR) address));
18355 }
18356
18357 if (cu != nullptr)
18358 cu->get_builder ()->record_line (subfile, line, addr, flags);
18359 }
18360
18361 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18362 Mark the end of a set of line number records.
18363 The arguments are the same as for dwarf_record_line_1.
18364 If SUBFILE is NULL the request is ignored. */
18365
18366 static void
18367 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18368 unrelocated_addr address, struct dwarf2_cu *cu)
18369 {
18370 if (subfile == NULL)
18371 return;
18372
18373 if (dwarf_line_debug)
18374 {
18375 gdb_printf (gdb_stdlog,
18376 "Finishing current line, file %s, address %s\n",
18377 lbasename (subfile->name.c_str ()),
18378 paddress (gdbarch, (CORE_ADDR) address));
18379 }
18380
18381 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
18382 }
18383
18384 void
18385 lnp_state_machine::record_line (bool end_sequence)
18386 {
18387 if (dwarf_line_debug)
18388 {
18389 gdb_printf (gdb_stdlog,
18390 "Processing actual line %u: file %u,"
18391 " address %s, is_stmt %u, prologue_end %u,"
18392 " epilogue_begin %u, discrim %u%s\n",
18393 m_line, m_file,
18394 paddress (m_gdbarch, (CORE_ADDR) m_address),
18395 (m_flags & LEF_IS_STMT) != 0,
18396 (m_flags & LEF_PROLOGUE_END) != 0,
18397 (m_flags & LEF_EPILOGUE_BEGIN) != 0,
18398 m_discriminator,
18399 (end_sequence ? "\t(end sequence)" : ""));
18400 }
18401
18402 file_entry *fe = current_file ();
18403
18404 if (fe == NULL)
18405 dwarf2_debug_line_missing_file_complaint ();
18406 /* For now we ignore lines not starting on an instruction boundary.
18407 But not when processing end_sequence for compatibility with the
18408 previous version of the code. */
18409 else if (m_op_index == 0 || end_sequence)
18410 {
18411 /* When we switch files we insert an end maker in the first file,
18412 switch to the second file and add a new line entry. The
18413 problem is that the end marker inserted in the first file will
18414 discard any previous line entries at the same address. If the
18415 line entries in the first file are marked as is-stmt, while
18416 the new line in the second file is non-stmt, then this means
18417 the end marker will discard is-stmt lines so we can have a
18418 non-stmt line. This means that there are less addresses at
18419 which the user can insert a breakpoint.
18420
18421 To improve this we track the last address in m_last_address,
18422 and whether we have seen an is-stmt at this address. Then
18423 when switching files, if we have seen a stmt at the current
18424 address, and we are switching to create a non-stmt line, then
18425 discard the new line. */
18426 bool file_changed
18427 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
18428 bool ignore_this_line
18429 = ((file_changed && !end_sequence && m_last_address == m_address
18430 && ((m_flags & LEF_IS_STMT) == 0)
18431 && m_stmt_at_address)
18432 || (!end_sequence && m_line == 0));
18433
18434 if ((file_changed && !ignore_this_line) || end_sequence)
18435 {
18436 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
18437 m_currently_recording_lines ? m_cu : nullptr);
18438 }
18439
18440 if (!end_sequence && !ignore_this_line)
18441 {
18442 linetable_entry_flags lte_flags = m_flags;
18443 if (producer_is_codewarrior (m_cu))
18444 lte_flags |= LEF_IS_STMT;
18445
18446 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
18447 m_line_has_non_zero_discriminator,
18448 m_last_subfile))
18449 {
18450 buildsym_compunit *builder = m_cu->get_builder ();
18451 dwarf_record_line_1 (m_gdbarch,
18452 builder->get_current_subfile (),
18453 m_line, m_address, lte_flags,
18454 m_currently_recording_lines ? m_cu : nullptr);
18455 }
18456 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18457 m_last_line = m_line;
18458 }
18459 }
18460
18461 /* Track whether we have seen any IS_STMT true at m_address in case we
18462 have multiple line table entries all at m_address. */
18463 if (m_last_address != m_address)
18464 {
18465 m_stmt_at_address = false;
18466 m_last_address = m_address;
18467 }
18468 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
18469 }
18470
18471 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
18472 line_header *lh)
18473 : m_cu (cu),
18474 m_gdbarch (arch),
18475 m_line_header (lh),
18476 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
18477 if there was a line entry for it so that the backend has a
18478 chance to adjust it and also record it in case it needs it.
18479 This is currently used by MIPS code,
18480 cf. `mips_adjust_dwarf2_line'. */
18481 m_address ((unrelocated_addr) gdbarch_adjust_dwarf2_line (arch, 0, 0)),
18482 m_flags (lh->default_is_stmt ? LEF_IS_STMT : (linetable_entry_flags) 0),
18483 m_last_address (m_address)
18484 {
18485 }
18486
18487 void
18488 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18489 const gdb_byte *line_ptr,
18490 unrelocated_addr unrelocated_lowpc,
18491 unrelocated_addr address)
18492 {
18493 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
18494 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
18495 located at 0x0. In this case, additionally check that if
18496 ADDRESS < UNRELOCATED_LOWPC. */
18497
18498 if ((address == (unrelocated_addr) 0 && address < unrelocated_lowpc)
18499 || address == (unrelocated_addr) -1)
18500 {
18501 /* This line table is for a function which has been
18502 GCd by the linker. Ignore it. PR gdb/12528 */
18503
18504 struct objfile *objfile = cu->per_objfile->objfile;
18505 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18506
18507 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18508 line_offset, objfile_name (objfile));
18509 m_currently_recording_lines = false;
18510 /* Note: m_currently_recording_lines is left as false until we see
18511 DW_LNE_end_sequence. */
18512 }
18513 }
18514
18515 /* Subroutine of dwarf_decode_lines to simplify it.
18516 Process the line number information in LH. */
18517
18518 static void
18519 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18520 unrelocated_addr lowpc)
18521 {
18522 const gdb_byte *line_ptr, *extended_end;
18523 const gdb_byte *line_end;
18524 unsigned int bytes_read, extended_len;
18525 unsigned char op_code, extended_op;
18526 struct objfile *objfile = cu->per_objfile->objfile;
18527 bfd *abfd = objfile->obfd.get ();
18528 struct gdbarch *gdbarch = objfile->arch ();
18529
18530 line_ptr = lh->statement_program_start;
18531 line_end = lh->statement_program_end;
18532
18533 /* Read the statement sequences until there's nothing left. */
18534 while (line_ptr < line_end)
18535 {
18536 /* The DWARF line number program state machine. Reset the state
18537 machine at the start of each sequence. */
18538 lnp_state_machine state_machine (cu, gdbarch, lh);
18539 bool end_sequence = false;
18540
18541 /* Start a subfile for the current file of the state
18542 machine. */
18543 const file_entry *fe = state_machine.current_file ();
18544
18545 if (fe != NULL)
18546 dwarf2_start_subfile (cu, *fe, *lh);
18547
18548 /* Decode the table. */
18549 while (line_ptr < line_end && !end_sequence)
18550 {
18551 op_code = read_1_byte (abfd, line_ptr);
18552 line_ptr += 1;
18553
18554 if (op_code >= lh->opcode_base)
18555 {
18556 /* Special opcode. */
18557 state_machine.handle_special_opcode (op_code);
18558 }
18559 else switch (op_code)
18560 {
18561 case DW_LNS_extended_op:
18562 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18563 &bytes_read);
18564 line_ptr += bytes_read;
18565 extended_end = line_ptr + extended_len;
18566 extended_op = read_1_byte (abfd, line_ptr);
18567 line_ptr += 1;
18568 if (DW_LNE_lo_user <= extended_op
18569 && extended_op <= DW_LNE_hi_user)
18570 {
18571 /* Vendor extension, ignore. */
18572 line_ptr = extended_end;
18573 break;
18574 }
18575 switch (extended_op)
18576 {
18577 case DW_LNE_end_sequence:
18578 state_machine.handle_end_sequence ();
18579 end_sequence = true;
18580 break;
18581 case DW_LNE_set_address:
18582 {
18583 unrelocated_addr address
18584 = cu->header.read_address (abfd, line_ptr, &bytes_read);
18585 line_ptr += bytes_read;
18586
18587 state_machine.check_line_address (cu, line_ptr, lowpc,
18588 address);
18589 state_machine.handle_set_address (address);
18590 }
18591 break;
18592 case DW_LNE_define_file:
18593 {
18594 const char *cur_file;
18595 unsigned int mod_time, length;
18596 dir_index dindex;
18597
18598 cur_file = read_direct_string (abfd, line_ptr,
18599 &bytes_read);
18600 line_ptr += bytes_read;
18601 dindex = (dir_index)
18602 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18603 line_ptr += bytes_read;
18604 mod_time =
18605 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18606 line_ptr += bytes_read;
18607 length =
18608 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18609 line_ptr += bytes_read;
18610 lh->add_file_name (cur_file, dindex, mod_time, length);
18611 }
18612 break;
18613 case DW_LNE_set_discriminator:
18614 {
18615 /* The discriminator is not interesting to the
18616 debugger; just ignore it. We still need to
18617 check its value though:
18618 if there are consecutive entries for the same
18619 (non-prologue) line we want to coalesce them.
18620 PR 17276. */
18621 unsigned int discr
18622 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18623 line_ptr += bytes_read;
18624
18625 state_machine.handle_set_discriminator (discr);
18626 }
18627 break;
18628 default:
18629 complaint (_("mangled .debug_line section"));
18630 return;
18631 }
18632 /* Make sure that we parsed the extended op correctly. If e.g.
18633 we expected a different address size than the producer used,
18634 we may have read the wrong number of bytes. */
18635 if (line_ptr != extended_end)
18636 {
18637 complaint (_("mangled .debug_line section"));
18638 return;
18639 }
18640 break;
18641 case DW_LNS_copy:
18642 state_machine.handle_copy ();
18643 break;
18644 case DW_LNS_advance_pc:
18645 {
18646 CORE_ADDR adjust
18647 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18648 line_ptr += bytes_read;
18649
18650 state_machine.handle_advance_pc (adjust);
18651 }
18652 break;
18653 case DW_LNS_advance_line:
18654 {
18655 int line_delta
18656 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18657 line_ptr += bytes_read;
18658
18659 state_machine.handle_advance_line (line_delta);
18660 }
18661 break;
18662 case DW_LNS_set_file:
18663 {
18664 file_name_index file
18665 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18666 &bytes_read);
18667 line_ptr += bytes_read;
18668
18669 state_machine.handle_set_file (file);
18670 }
18671 break;
18672 case DW_LNS_set_column:
18673 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18674 line_ptr += bytes_read;
18675 break;
18676 case DW_LNS_negate_stmt:
18677 state_machine.handle_negate_stmt ();
18678 break;
18679 case DW_LNS_set_basic_block:
18680 break;
18681 /* Add to the address register of the state machine the
18682 address increment value corresponding to special opcode
18683 255. I.e., this value is scaled by the minimum
18684 instruction length since special opcode 255 would have
18685 scaled the increment. */
18686 case DW_LNS_const_add_pc:
18687 state_machine.handle_const_add_pc ();
18688 break;
18689 case DW_LNS_fixed_advance_pc:
18690 {
18691 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18692 line_ptr += 2;
18693
18694 state_machine.handle_fixed_advance_pc (addr_adj);
18695 }
18696 break;
18697 case DW_LNS_set_prologue_end:
18698 state_machine.handle_set_prologue_end ();
18699 break;
18700 case DW_LNS_set_epilogue_begin:
18701 state_machine.handle_set_epilogue_begin ();
18702 break;
18703 default:
18704 {
18705 /* Unknown standard opcode, ignore it. */
18706 int i;
18707
18708 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18709 {
18710 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18711 line_ptr += bytes_read;
18712 }
18713 }
18714 }
18715 }
18716
18717 if (!end_sequence)
18718 dwarf2_debug_line_missing_end_sequence_complaint ();
18719
18720 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18721 in which case we still finish recording the last line). */
18722 state_machine.record_line (true);
18723 }
18724 }
18725
18726 /* Decode the Line Number Program (LNP) for the given line_header
18727 structure and CU. The actual information extracted and the type
18728 of structures created from the LNP depends on the value of PST.
18729
18730 FND holds the CU file name and directory, if known.
18731 It is used for relative paths in the line table.
18732
18733 NOTE: It is important that psymtabs have the same file name (via
18734 strcmp) as the corresponding symtab. Since the directory is not
18735 used in the name of the symtab we don't use it in the name of the
18736 psymtabs we create. E.g. expand_line_sal requires this when
18737 finding psymtabs to expand. A good testcase for this is
18738 mb-inline.exp.
18739
18740 LOWPC is the lowest address in CU (or 0 if not known).
18741
18742 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18743 for its PC<->lines mapping information. Otherwise only the filename
18744 table is read in. */
18745
18746 static void
18747 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
18748 unrelocated_addr lowpc, int decode_mapping)
18749 {
18750 if (decode_mapping)
18751 dwarf_decode_lines_1 (lh, cu, lowpc);
18752
18753 /* Make sure a symtab is created for every file, even files
18754 which contain only variables (i.e. no code with associated
18755 line numbers). */
18756 buildsym_compunit *builder = cu->get_builder ();
18757 struct compunit_symtab *cust = builder->get_compunit_symtab ();
18758
18759 for (auto &fe : lh->file_names ())
18760 {
18761 dwarf2_start_subfile (cu, fe, *lh);
18762 subfile *sf = builder->get_current_subfile ();
18763
18764 if (sf->symtab == nullptr)
18765 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
18766 sf->name_for_id.c_str ());
18767
18768 fe.symtab = sf->symtab;
18769 }
18770 }
18771
18772 /* Start a subfile for DWARF. FILENAME is the name of the file and
18773 DIRNAME the name of the source directory which contains FILENAME
18774 or NULL if not known.
18775 This routine tries to keep line numbers from identical absolute and
18776 relative file names in a common subfile.
18777
18778 Using the `list' example from the GDB testsuite, which resides in
18779 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18780 of /srcdir/list0.c yields the following debugging information for list0.c:
18781
18782 DW_AT_name: /srcdir/list0.c
18783 DW_AT_comp_dir: /compdir
18784 files.files[0].name: list0.h
18785 files.files[0].dir: /srcdir
18786 files.files[1].name: list0.c
18787 files.files[1].dir: /srcdir
18788
18789 The line number information for list0.c has to end up in a single
18790 subfile, so that `break /srcdir/list0.c:1' works as expected.
18791 start_subfile will ensure that this happens provided that we pass the
18792 concatenation of files.files[1].dir and files.files[1].name as the
18793 subfile's name. */
18794
18795 static void
18796 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
18797 const line_header &lh)
18798 {
18799 std::string filename_holder;
18800 const char *filename = fe.name;
18801 const char *dirname = lh.include_dir_at (fe.d_index);
18802
18803 /* In order not to lose the line information directory,
18804 we concatenate it to the filename when it makes sense.
18805 Note that the Dwarf3 standard says (speaking of filenames in line
18806 information): ``The directory index is ignored for file names
18807 that represent full path names''. Thus ignoring dirname in the
18808 `else' branch below isn't an issue. */
18809
18810 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18811 {
18812 filename_holder = path_join (dirname, filename);
18813 filename = filename_holder.c_str ();
18814 }
18815
18816 std::string filename_for_id = lh.file_file_name (fe);
18817 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
18818 }
18819
18820 static void
18821 var_decode_location (struct attribute *attr, struct symbol *sym,
18822 struct dwarf2_cu *cu)
18823 {
18824 struct objfile *objfile = cu->per_objfile->objfile;
18825 struct comp_unit_head *cu_header = &cu->header;
18826
18827 /* NOTE drow/2003-01-30: There used to be a comment and some special
18828 code here to turn a symbol with DW_AT_external and a
18829 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18830 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18831 with some versions of binutils) where shared libraries could have
18832 relocations against symbols in their debug information - the
18833 minimal symbol would have the right address, but the debug info
18834 would not. It's no longer necessary, because we will explicitly
18835 apply relocations when we read in the debug information now. */
18836
18837 /* A DW_AT_location attribute with no contents indicates that a
18838 variable has been optimized away. */
18839 if (attr->form_is_block () && attr->as_block ()->size == 0)
18840 {
18841 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18842 return;
18843 }
18844
18845 /* Handle one degenerate form of location expression specially, to
18846 preserve GDB's previous behavior when section offsets are
18847 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
18848 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
18849
18850 if (attr->form_is_block ())
18851 {
18852 struct dwarf_block *block = attr->as_block ();
18853
18854 if ((block->data[0] == DW_OP_addr
18855 && block->size == 1 + cu_header->addr_size)
18856 || ((block->data[0] == DW_OP_GNU_addr_index
18857 || block->data[0] == DW_OP_addrx)
18858 && (block->size
18859 == 1 + leb128_size (&block->data[1]))))
18860 {
18861 unsigned int dummy;
18862
18863 unrelocated_addr tem;
18864 if (block->data[0] == DW_OP_addr)
18865 tem = cu->header.read_address (objfile->obfd.get (),
18866 block->data + 1,
18867 &dummy);
18868 else
18869 tem = read_addr_index_from_leb128 (cu, block->data + 1, &dummy);
18870 sym->set_value_address ((CORE_ADDR) tem);
18871 sym->set_aclass_index (LOC_STATIC);
18872 fixup_symbol_section (sym, objfile);
18873 sym->set_value_address
18874 (sym->value_address ()
18875 + objfile->section_offsets[sym->section_index ()]);
18876 return;
18877 }
18878 }
18879
18880 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18881 expression evaluator, and use LOC_COMPUTED only when necessary
18882 (i.e. when the value of a register or memory location is
18883 referenced, or a thread-local block, etc.). Then again, it might
18884 not be worthwhile. I'm assuming that it isn't unless performance
18885 or memory numbers show me otherwise. */
18886
18887 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18888
18889 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18890 cu->has_loclist = true;
18891 }
18892
18893 /* A helper function to add an "export" symbol. The new symbol starts
18894 as a clone of ORIG, but is modified to defer to the symbol named
18895 ORIG_NAME. The original symbol uses the name given in the source
18896 code, and the symbol that is created here uses the linkage name as
18897 its name. See ada-imported.c. */
18898
18899 static void
18900 add_ada_export_symbol (struct symbol *orig, const char *new_name,
18901 const char *orig_name, struct dwarf2_cu *cu,
18902 struct pending **list_to_add)
18903 {
18904 struct symbol *copy
18905 = new (&cu->per_objfile->objfile->objfile_obstack) symbol (*orig);
18906 copy->set_linkage_name (new_name);
18907 SYMBOL_LOCATION_BATON (copy) = (void *) orig_name;
18908 copy->set_aclass_index (copy->aclass () == LOC_BLOCK
18909 ? ada_block_index
18910 : ada_imported_index);
18911 add_symbol_to_list (copy, list_to_add);
18912 }
18913
18914 /* A helper function that decides if a given symbol is an Ada Pragma
18915 Import or Pragma Export. */
18916
18917 static bool
18918 is_ada_import_or_export (dwarf2_cu *cu, const char *name,
18919 const char *linkagename)
18920 {
18921 return (cu->lang () == language_ada
18922 && linkagename != nullptr
18923 && !streq (name, linkagename)
18924 /* The following exclusions are necessary because symbols
18925 with names or linkage names that match here will meet the
18926 other criteria but are not in fact caused by Pragma
18927 Import or Pragma Export, and applying the import/export
18928 treatment to them will introduce problems. Some of these
18929 checks only apply to functions, but it is simpler and
18930 harmless to always do them all. */
18931 && !startswith (name, "__builtin")
18932 && !startswith (linkagename, "___ghost_")
18933 && !startswith (linkagename, "__gnat")
18934 && !startswith (linkagename, "_ada_")
18935 && !streq (linkagename, "adainit"));
18936 }
18937
18938 /* Given a pointer to a DWARF information entry, figure out if we need
18939 to make a symbol table entry for it, and if so, create a new entry
18940 and return a pointer to it.
18941 If TYPE is NULL, determine symbol type from the die, otherwise
18942 used the passed type.
18943 If SPACE is not NULL, use it to hold the new symbol. If it is
18944 NULL, allocate a new symbol on the objfile's obstack. */
18945
18946 static struct symbol *
18947 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18948 struct symbol *space)
18949 {
18950 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18951 struct objfile *objfile = per_objfile->objfile;
18952 struct symbol *sym = NULL;
18953 const char *name;
18954 struct attribute *attr = NULL;
18955 struct attribute *attr2 = NULL;
18956 struct pending **list_to_add = NULL;
18957
18958 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18959
18960 name = dwarf2_name (die, cu);
18961 if (name == nullptr && (die->tag == DW_TAG_subprogram
18962 || die->tag == DW_TAG_inlined_subroutine
18963 || die->tag == DW_TAG_entry_point))
18964 name = dw2_linkage_name (die, cu);
18965
18966 if (name)
18967 {
18968 int suppress_add = 0;
18969
18970 if (space)
18971 sym = space;
18972 else
18973 sym = new (&objfile->objfile_obstack) symbol;
18974 OBJSTAT (objfile, n_syms++);
18975
18976 /* Cache this symbol's name and the name's demangled form (if any). */
18977 sym->set_language (cu->lang (), &objfile->objfile_obstack);
18978 /* Fortran does not have mangling standard and the mangling does differ
18979 between gfortran, iFort etc. */
18980 const char *physname
18981 = (cu->lang () == language_fortran
18982 ? dwarf2_full_name (name, die, cu)
18983 : dwarf2_physname (name, die, cu));
18984 const char *linkagename = dw2_linkage_name (die, cu);
18985
18986 if (linkagename == nullptr || cu->lang () == language_ada)
18987 sym->set_linkage_name (physname);
18988 else
18989 {
18990 sym->set_demangled_name (physname, &objfile->objfile_obstack);
18991 sym->set_linkage_name (linkagename);
18992 }
18993
18994 /* Handle DW_AT_artificial. */
18995 attr = dwarf2_attr (die, DW_AT_artificial, cu);
18996 if (attr != nullptr)
18997 sym->set_is_artificial (attr->as_boolean ());
18998
18999 /* Default assumptions.
19000 Use the passed type or decode it from the die. */
19001 sym->set_domain (VAR_DOMAIN);
19002 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
19003 if (type != NULL)
19004 sym->set_type (type);
19005 else
19006 sym->set_type (die_type (die, cu));
19007 attr = dwarf2_attr (die,
19008 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
19009 cu);
19010 if (attr != nullptr)
19011 sym->set_line (attr->constant_value (0));
19012
19013 attr = dwarf2_attr (die,
19014 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
19015 cu);
19016 if (attr != nullptr && attr->is_nonnegative ())
19017 {
19018 file_name_index file_index
19019 = (file_name_index) attr->as_nonnegative ();
19020 struct file_entry *fe;
19021
19022 if (cu->line_header != NULL)
19023 fe = cu->line_header->file_name_at (file_index);
19024 else
19025 fe = NULL;
19026
19027 if (fe == NULL)
19028 complaint (_("file index out of range"));
19029 else
19030 sym->set_symtab (fe->symtab);
19031 }
19032
19033 switch (die->tag)
19034 {
19035 case DW_TAG_label:
19036 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
19037 if (attr != nullptr)
19038 {
19039 CORE_ADDR addr = per_objfile->relocate (attr->as_address ());
19040 sym->set_section_index (SECT_OFF_TEXT (objfile));
19041 sym->set_value_address (addr);
19042 sym->set_aclass_index (LOC_LABEL);
19043 }
19044 else
19045 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
19046 sym->set_type (builtin_type (objfile)->builtin_core_addr);
19047 sym->set_domain (LABEL_DOMAIN);
19048 add_symbol_to_list (sym, cu->list_in_scope);
19049 break;
19050 case DW_TAG_entry_point:
19051 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19052 finish_block. */
19053 sym->set_aclass_index (LOC_BLOCK);
19054 /* DW_TAG_entry_point provides an additional entry_point to an
19055 existing sub_program. Therefore, we inherit the "external"
19056 attribute from the sub_program to which the entry_point
19057 belongs to. */
19058 attr2 = dwarf2_attr (die->parent, DW_AT_external, cu);
19059 if (attr2 != nullptr && attr2->as_boolean ())
19060 list_to_add = cu->get_builder ()->get_global_symbols ();
19061 else
19062 list_to_add = cu->list_in_scope;
19063 break;
19064 case DW_TAG_subprogram:
19065 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19066 finish_block. */
19067 sym->set_aclass_index (LOC_BLOCK);
19068 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19069 if ((attr2 != nullptr && attr2->as_boolean ())
19070 || cu->lang () == language_ada
19071 || cu->lang () == language_fortran)
19072 {
19073 /* Subprograms marked external are stored as a global symbol.
19074 Ada and Fortran subprograms, whether marked external or
19075 not, are always stored as a global symbol, because we want
19076 to be able to access them globally. For instance, we want
19077 to be able to break on a nested subprogram without having
19078 to specify the context. */
19079 list_to_add = cu->get_builder ()->get_global_symbols ();
19080 }
19081 else
19082 {
19083 list_to_add = cu->list_in_scope;
19084 }
19085
19086 if (is_ada_import_or_export (cu, name, linkagename))
19087 {
19088 /* This is either a Pragma Import or Export. They can
19089 be distinguished by the declaration flag. */
19090 sym->set_linkage_name (name);
19091 if (die_is_declaration (die, cu))
19092 {
19093 /* For Import, create a symbol using the source
19094 name, and have it refer to the linkage name. */
19095 SYMBOL_LOCATION_BATON (sym) = (void *) linkagename;
19096 sym->set_aclass_index (ada_block_index);
19097 }
19098 else
19099 {
19100 /* For Export, create a symbol using the source
19101 name, then create a second symbol that refers
19102 back to it. */
19103 add_ada_export_symbol (sym, linkagename, name, cu,
19104 list_to_add);
19105 }
19106 }
19107 break;
19108 case DW_TAG_inlined_subroutine:
19109 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19110 finish_block. */
19111 sym->set_aclass_index (LOC_BLOCK);
19112 sym->set_is_inlined (1);
19113 list_to_add = cu->list_in_scope;
19114 break;
19115 case DW_TAG_template_value_param:
19116 suppress_add = 1;
19117 [[fallthrough]];
19118 case DW_TAG_constant:
19119 case DW_TAG_variable:
19120 case DW_TAG_member:
19121 /* Compilation with minimal debug info may result in
19122 variables with missing type entries. Change the
19123 misleading `void' type to something sensible. */
19124 if (sym->type ()->code () == TYPE_CODE_VOID)
19125 sym->set_type (builtin_type (objfile)->builtin_int);
19126
19127 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19128 /* In the case of DW_TAG_member, we should only be called for
19129 static const members. */
19130 if (die->tag == DW_TAG_member)
19131 {
19132 /* dwarf2_add_field uses die_is_declaration,
19133 so we do the same. */
19134 gdb_assert (die_is_declaration (die, cu));
19135 gdb_assert (attr);
19136 }
19137 if (attr != nullptr)
19138 {
19139 dwarf2_const_value (attr, sym, cu);
19140 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19141 if (!suppress_add)
19142 {
19143 if (attr2 != nullptr && attr2->as_boolean ())
19144 list_to_add = cu->get_builder ()->get_global_symbols ();
19145 else
19146 list_to_add = cu->list_in_scope;
19147 }
19148 break;
19149 }
19150 attr = dwarf2_attr (die, DW_AT_location, cu);
19151 if (attr != nullptr)
19152 {
19153 var_decode_location (attr, sym, cu);
19154 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19155
19156 /* Fortran explicitly imports any global symbols to the local
19157 scope by DW_TAG_common_block. */
19158 if (cu->lang () == language_fortran && die->parent
19159 && die->parent->tag == DW_TAG_common_block)
19160 attr2 = NULL;
19161
19162 if (sym->aclass () == LOC_STATIC
19163 && sym->value_address () == 0
19164 && !per_objfile->per_bfd->has_section_at_zero)
19165 {
19166 /* When a static variable is eliminated by the linker,
19167 the corresponding debug information is not stripped
19168 out, but the variable address is set to null;
19169 do not add such variables into symbol table. */
19170 }
19171 else if (attr2 != nullptr && attr2->as_boolean ())
19172 {
19173 if (sym->aclass () == LOC_STATIC
19174 && (objfile->flags & OBJF_MAINLINE) == 0
19175 && per_objfile->per_bfd->can_copy)
19176 {
19177 /* A global static variable might be subject to
19178 copy relocation. We first check for a local
19179 minsym, though, because maybe the symbol was
19180 marked hidden, in which case this would not
19181 apply. */
19182 bound_minimal_symbol found
19183 = (lookup_minimal_symbol_linkage
19184 (sym->linkage_name (), objfile));
19185 if (found.minsym != nullptr)
19186 sym->maybe_copied = 1;
19187 }
19188
19189 /* A variable with DW_AT_external is never static,
19190 but it may be block-scoped. */
19191 list_to_add
19192 = ((cu->list_in_scope
19193 == cu->get_builder ()->get_file_symbols ())
19194 ? cu->get_builder ()->get_global_symbols ()
19195 : cu->list_in_scope);
19196 }
19197 else
19198 list_to_add = cu->list_in_scope;
19199
19200 if (list_to_add != nullptr
19201 && is_ada_import_or_export (cu, name, linkagename))
19202 {
19203 /* This is a Pragma Export. A Pragma Import won't
19204 be seen here, because it will not have a location
19205 and so will be handled below. */
19206 add_ada_export_symbol (sym, name, linkagename, cu,
19207 list_to_add);
19208 }
19209 }
19210 else
19211 {
19212 /* We do not know the address of this symbol.
19213 If it is an external symbol and we have type information
19214 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19215 The address of the variable will then be determined from
19216 the minimal symbol table whenever the variable is
19217 referenced. */
19218 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19219
19220 /* Fortran explicitly imports any global symbols to the local
19221 scope by DW_TAG_common_block. */
19222 if (cu->lang () == language_fortran && die->parent
19223 && die->parent->tag == DW_TAG_common_block)
19224 {
19225 /* SYMBOL_CLASS doesn't matter here because
19226 read_common_block is going to reset it. */
19227 if (!suppress_add)
19228 list_to_add = cu->list_in_scope;
19229 }
19230 else if (is_ada_import_or_export (cu, name, linkagename))
19231 {
19232 /* This is a Pragma Import. A Pragma Export won't
19233 be seen here, because it will have a location and
19234 so will be handled above. */
19235 sym->set_linkage_name (name);
19236 list_to_add = cu->list_in_scope;
19237 SYMBOL_LOCATION_BATON (sym) = (void *) linkagename;
19238 sym->set_aclass_index (ada_imported_index);
19239 }
19240 else if (attr2 != nullptr && attr2->as_boolean ()
19241 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19242 {
19243 /* A variable with DW_AT_external is never static, but it
19244 may be block-scoped. */
19245 list_to_add
19246 = ((cu->list_in_scope
19247 == cu->get_builder ()->get_file_symbols ())
19248 ? cu->get_builder ()->get_global_symbols ()
19249 : cu->list_in_scope);
19250
19251 sym->set_aclass_index (LOC_UNRESOLVED);
19252 }
19253 else if (!die_is_declaration (die, cu))
19254 {
19255 /* Use the default LOC_OPTIMIZED_OUT class. */
19256 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
19257 if (!suppress_add)
19258 list_to_add = cu->list_in_scope;
19259 }
19260 }
19261 break;
19262 case DW_TAG_formal_parameter:
19263 {
19264 /* If we are inside a function, mark this as an argument. If
19265 not, we might be looking at an argument to an inlined function
19266 when we do not have enough information to show inlined frames;
19267 pretend it's a local variable in that case so that the user can
19268 still see it. */
19269 struct context_stack *curr
19270 = cu->get_builder ()->get_current_context_stack ();
19271 if (curr != nullptr && curr->name != nullptr)
19272 sym->set_is_argument (1);
19273 attr = dwarf2_attr (die, DW_AT_location, cu);
19274 if (attr != nullptr)
19275 {
19276 var_decode_location (attr, sym, cu);
19277 }
19278 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19279 if (attr != nullptr)
19280 {
19281 dwarf2_const_value (attr, sym, cu);
19282 }
19283
19284 list_to_add = cu->list_in_scope;
19285 }
19286 break;
19287 case DW_TAG_unspecified_parameters:
19288 /* From varargs functions; gdb doesn't seem to have any
19289 interest in this information, so just ignore it for now.
19290 (FIXME?) */
19291 break;
19292 case DW_TAG_template_type_param:
19293 suppress_add = 1;
19294 [[fallthrough]];
19295 case DW_TAG_class_type:
19296 case DW_TAG_interface_type:
19297 case DW_TAG_structure_type:
19298 case DW_TAG_union_type:
19299 case DW_TAG_set_type:
19300 case DW_TAG_enumeration_type:
19301 case DW_TAG_namelist:
19302 if (die->tag == DW_TAG_namelist)
19303 {
19304 sym->set_aclass_index (LOC_STATIC);
19305 sym->set_domain (VAR_DOMAIN);
19306 }
19307 else
19308 {
19309 sym->set_aclass_index (LOC_TYPEDEF);
19310 sym->set_domain (STRUCT_DOMAIN);
19311 }
19312
19313 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19314 really ever be static objects: otherwise, if you try
19315 to, say, break of a class's method and you're in a file
19316 which doesn't mention that class, it won't work unless
19317 the check for all static symbols in lookup_symbol_aux
19318 saves you. See the OtherFileClass tests in
19319 gdb.c++/namespace.exp. */
19320
19321 if (!suppress_add)
19322 {
19323 buildsym_compunit *builder = cu->get_builder ();
19324 list_to_add
19325 = (cu->list_in_scope == builder->get_file_symbols ()
19326 && cu->lang () == language_cplus
19327 ? builder->get_global_symbols ()
19328 : cu->list_in_scope);
19329
19330 /* The semantics of C++ state that "struct foo {
19331 ... }" also defines a typedef for "foo". */
19332 if (cu->lang () == language_cplus
19333 || cu->lang () == language_ada
19334 || cu->lang () == language_d
19335 || cu->lang () == language_rust)
19336 {
19337 /* The symbol's name is already allocated along
19338 with this objfile, so we don't need to
19339 duplicate it for the type. */
19340 if (sym->type ()->name () == 0)
19341 sym->type ()->set_name (sym->search_name ());
19342 }
19343 }
19344 break;
19345 case DW_TAG_unspecified_type:
19346 if (cu->lang () == language_ada)
19347 break;
19348 [[fallthrough]];
19349 case DW_TAG_typedef:
19350 case DW_TAG_array_type:
19351 case DW_TAG_base_type:
19352 case DW_TAG_subrange_type:
19353 case DW_TAG_generic_subrange:
19354 sym->set_aclass_index (LOC_TYPEDEF);
19355 sym->set_domain (VAR_DOMAIN);
19356 list_to_add = cu->list_in_scope;
19357 break;
19358 case DW_TAG_enumerator:
19359 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19360 if (attr != nullptr)
19361 {
19362 dwarf2_const_value (attr, sym, cu);
19363 }
19364
19365 /* NOTE: carlton/2003-11-10: See comment above in the
19366 DW_TAG_class_type, etc. block. */
19367
19368 list_to_add
19369 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
19370 && cu->lang () == language_cplus
19371 ? cu->get_builder ()->get_global_symbols ()
19372 : cu->list_in_scope);
19373 break;
19374 case DW_TAG_imported_declaration:
19375 case DW_TAG_namespace:
19376 sym->set_aclass_index (LOC_TYPEDEF);
19377 list_to_add = cu->get_builder ()->get_global_symbols ();
19378 break;
19379 case DW_TAG_module:
19380 sym->set_aclass_index (LOC_TYPEDEF);
19381 sym->set_domain (MODULE_DOMAIN);
19382 list_to_add = cu->get_builder ()->get_global_symbols ();
19383 break;
19384 case DW_TAG_common_block:
19385 sym->set_aclass_index (LOC_COMMON_BLOCK);
19386 sym->set_domain (COMMON_BLOCK_DOMAIN);
19387 add_symbol_to_list (sym, cu->list_in_scope);
19388 break;
19389 default:
19390 /* Not a tag we recognize. Hopefully we aren't processing
19391 trash data, but since we must specifically ignore things
19392 we don't recognize, there is nothing else we should do at
19393 this point. */
19394 complaint (_("unsupported tag: '%s'"),
19395 dwarf_tag_name (die->tag));
19396 break;
19397 }
19398
19399 if (suppress_add)
19400 {
19401 sym->hash_next = objfile->template_symbols;
19402 objfile->template_symbols = sym;
19403 list_to_add = NULL;
19404 }
19405
19406 if (list_to_add != NULL)
19407 add_symbol_to_list (sym, list_to_add);
19408
19409 /* For the benefit of old versions of GCC, check for anonymous
19410 namespaces based on the demangled name. */
19411 if (!cu->processing_has_namespace_info
19412 && cu->lang () == language_cplus)
19413 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
19414 }
19415 return (sym);
19416 }
19417
19418 /* Given an attr with a DW_FORM_dataN value in host byte order,
19419 zero-extend it as appropriate for the symbol's type. The DWARF
19420 standard (v4) is not entirely clear about the meaning of using
19421 DW_FORM_dataN for a constant with a signed type, where the type is
19422 wider than the data. The conclusion of a discussion on the DWARF
19423 list was that this is unspecified. We choose to always zero-extend
19424 because that is the interpretation long in use by GCC. */
19425
19426 static gdb_byte *
19427 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19428 struct dwarf2_cu *cu, LONGEST *value, int bits)
19429 {
19430 struct objfile *objfile = cu->per_objfile->objfile;
19431 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
19432 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19433 LONGEST l = attr->constant_value (0);
19434
19435 if (bits < sizeof (*value) * 8)
19436 {
19437 l &= ((LONGEST) 1 << bits) - 1;
19438 *value = l;
19439 }
19440 else if (bits == sizeof (*value) * 8)
19441 *value = l;
19442 else
19443 {
19444 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19445 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19446 return bytes;
19447 }
19448
19449 return NULL;
19450 }
19451
19452 /* Read a constant value from an attribute. Either set *VALUE, or if
19453 the value does not fit in *VALUE, set *BYTES - either already
19454 allocated on the objfile obstack, or newly allocated on OBSTACK,
19455 or, set *BATON, if we translated the constant to a location
19456 expression. */
19457
19458 static void
19459 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19460 const char *name, struct obstack *obstack,
19461 struct dwarf2_cu *cu,
19462 LONGEST *value, const gdb_byte **bytes,
19463 struct dwarf2_locexpr_baton **baton)
19464 {
19465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19466 struct objfile *objfile = per_objfile->objfile;
19467 struct comp_unit_head *cu_header = &cu->header;
19468 struct dwarf_block *blk;
19469 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
19470 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19471
19472 *value = 0;
19473 *bytes = NULL;
19474 *baton = NULL;
19475
19476 switch (attr->form)
19477 {
19478 case DW_FORM_addr:
19479 case DW_FORM_addrx:
19480 case DW_FORM_GNU_addr_index:
19481 {
19482 gdb_byte *data;
19483
19484 if (type->length () != cu_header->addr_size)
19485 dwarf2_const_value_length_mismatch_complaint (name,
19486 cu_header->addr_size,
19487 type->length ());
19488 /* Symbols of this form are reasonably rare, so we just
19489 piggyback on the existing location code rather than writing
19490 a new implementation of symbol_computed_ops. */
19491 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19492 (*baton)->per_objfile = per_objfile;
19493 (*baton)->per_cu = cu->per_cu;
19494 gdb_assert ((*baton)->per_cu);
19495
19496 (*baton)->size = 2 + cu_header->addr_size;
19497 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19498 (*baton)->data = data;
19499
19500 data[0] = DW_OP_addr;
19501 store_unsigned_integer (&data[1], cu_header->addr_size,
19502 byte_order, (ULONGEST) attr->as_address ());
19503 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19504 }
19505 break;
19506 case DW_FORM_string:
19507 case DW_FORM_strp:
19508 case DW_FORM_strx:
19509 case DW_FORM_GNU_str_index:
19510 case DW_FORM_GNU_strp_alt:
19511 /* The string is already allocated on the objfile obstack, point
19512 directly to it. */
19513 *bytes = (const gdb_byte *) attr->as_string ();
19514 break;
19515 case DW_FORM_block1:
19516 case DW_FORM_block2:
19517 case DW_FORM_block4:
19518 case DW_FORM_block:
19519 case DW_FORM_exprloc:
19520 case DW_FORM_data16:
19521 blk = attr->as_block ();
19522 if (type->length () != blk->size)
19523 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19524 type->length ());
19525 *bytes = blk->data;
19526 break;
19527
19528 /* The DW_AT_const_value attributes are supposed to carry the
19529 symbol's value "represented as it would be on the target
19530 architecture." By the time we get here, it's already been
19531 converted to host endianness, so we just need to sign- or
19532 zero-extend it as appropriate. */
19533 case DW_FORM_data1:
19534 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19535 break;
19536 case DW_FORM_data2:
19537 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19538 break;
19539 case DW_FORM_data4:
19540 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19541 break;
19542 case DW_FORM_data8:
19543 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19544 break;
19545
19546 case DW_FORM_sdata:
19547 case DW_FORM_implicit_const:
19548 *value = attr->as_signed ();
19549 break;
19550
19551 case DW_FORM_udata:
19552 *value = attr->as_unsigned ();
19553 break;
19554
19555 default:
19556 complaint (_("unsupported const value attribute form: '%s'"),
19557 dwarf_form_name (attr->form));
19558 *value = 0;
19559 break;
19560 }
19561 }
19562
19563
19564 /* Copy constant value from an attribute to a symbol. */
19565
19566 static void
19567 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19568 struct dwarf2_cu *cu)
19569 {
19570 struct objfile *objfile = cu->per_objfile->objfile;
19571 LONGEST value;
19572 const gdb_byte *bytes;
19573 struct dwarf2_locexpr_baton *baton;
19574
19575 dwarf2_const_value_attr (attr, sym->type (),
19576 sym->print_name (),
19577 &objfile->objfile_obstack, cu,
19578 &value, &bytes, &baton);
19579
19580 if (baton != NULL)
19581 {
19582 SYMBOL_LOCATION_BATON (sym) = baton;
19583 sym->set_aclass_index (dwarf2_locexpr_index);
19584 }
19585 else if (bytes != NULL)
19586 {
19587 sym->set_value_bytes (bytes);
19588 sym->set_aclass_index (LOC_CONST_BYTES);
19589 }
19590 else
19591 {
19592 sym->set_value_longest (value);
19593 sym->set_aclass_index (LOC_CONST);
19594 }
19595 }
19596
19597 /* Return the type of the die in question using its DW_AT_type attribute. */
19598
19599 static struct type *
19600 die_type (struct die_info *die, struct dwarf2_cu *cu)
19601 {
19602 struct attribute *type_attr;
19603
19604 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19605 if (!type_attr)
19606 {
19607 struct objfile *objfile = cu->per_objfile->objfile;
19608 /* A missing DW_AT_type represents a void type. */
19609 return builtin_type (objfile)->builtin_void;
19610 }
19611
19612 return lookup_die_type (die, type_attr, cu);
19613 }
19614
19615 /* True iff CU's producer generates GNAT Ada auxiliary information
19616 that allows to find parallel types through that information instead
19617 of having to do expensive parallel lookups by type name. */
19618
19619 static int
19620 need_gnat_info (struct dwarf2_cu *cu)
19621 {
19622 /* Assume that the Ada compiler was GNAT, which always produces
19623 the auxiliary information. */
19624 return (cu->lang () == language_ada);
19625 }
19626
19627 /* Return the auxiliary type of the die in question using its
19628 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19629 attribute is not present. */
19630
19631 static struct type *
19632 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19633 {
19634 struct attribute *type_attr;
19635
19636 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19637 if (!type_attr)
19638 return NULL;
19639
19640 return lookup_die_type (die, type_attr, cu);
19641 }
19642
19643 /* If DIE has a descriptive_type attribute, then set the TYPE's
19644 descriptive type accordingly. */
19645
19646 static void
19647 set_descriptive_type (struct type *type, struct die_info *die,
19648 struct dwarf2_cu *cu)
19649 {
19650 struct type *descriptive_type = die_descriptive_type (die, cu);
19651
19652 if (descriptive_type)
19653 {
19654 ALLOCATE_GNAT_AUX_TYPE (type);
19655 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19656 }
19657 }
19658
19659 /* Return the containing type of the die in question using its
19660 DW_AT_containing_type attribute. */
19661
19662 static struct type *
19663 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19664 {
19665 struct attribute *type_attr;
19666 struct objfile *objfile = cu->per_objfile->objfile;
19667
19668 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19669 if (!type_attr)
19670 error (_("Dwarf Error: Problem turning containing type into gdb type "
19671 "[in module %s]"), objfile_name (objfile));
19672
19673 return lookup_die_type (die, type_attr, cu);
19674 }
19675
19676 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19677
19678 static struct type *
19679 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19680 {
19681 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19682 struct objfile *objfile = per_objfile->objfile;
19683 char *saved;
19684
19685 std::string message
19686 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
19687 objfile_name (objfile),
19688 sect_offset_str (cu->header.sect_off),
19689 sect_offset_str (die->sect_off));
19690 saved = obstack_strdup (&objfile->objfile_obstack, message);
19691
19692 return type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_ERROR,
19693 0, saved);
19694 }
19695
19696 /* Look up the type of DIE in CU using its type attribute ATTR.
19697 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19698 DW_AT_containing_type.
19699 If there is no type substitute an error marker. */
19700
19701 static struct type *
19702 lookup_die_type (struct die_info *die, const struct attribute *attr,
19703 struct dwarf2_cu *cu)
19704 {
19705 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19706 struct objfile *objfile = per_objfile->objfile;
19707 struct type *this_type;
19708
19709 gdb_assert (attr->name == DW_AT_type
19710 || attr->name == DW_AT_GNAT_descriptive_type
19711 || attr->name == DW_AT_containing_type);
19712
19713 /* First see if we have it cached. */
19714
19715 if (attr->form == DW_FORM_GNU_ref_alt)
19716 {
19717 struct dwarf2_per_cu_data *per_cu;
19718 sect_offset sect_off = attr->get_ref_die_offset ();
19719
19720 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
19721 per_objfile->per_bfd);
19722 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
19723 }
19724 else if (attr->form_is_ref ())
19725 {
19726 sect_offset sect_off = attr->get_ref_die_offset ();
19727
19728 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
19729 }
19730 else if (attr->form == DW_FORM_ref_sig8)
19731 {
19732 ULONGEST signature = attr->as_signature ();
19733
19734 return get_signatured_type (die, signature, cu);
19735 }
19736 else
19737 {
19738 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
19739 " at %s [in module %s]"),
19740 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
19741 objfile_name (objfile));
19742 return build_error_marker_type (cu, die);
19743 }
19744
19745 /* If not cached we need to read it in. */
19746
19747 if (this_type == NULL)
19748 {
19749 struct die_info *type_die = NULL;
19750 struct dwarf2_cu *type_cu = cu;
19751
19752 if (attr->form_is_ref ())
19753 type_die = follow_die_ref (die, attr, &type_cu);
19754 if (type_die == NULL)
19755 return build_error_marker_type (cu, die);
19756 /* If we find the type now, it's probably because the type came
19757 from an inter-CU reference and the type's CU got expanded before
19758 ours. */
19759 this_type = read_type_die (type_die, type_cu);
19760 }
19761
19762 /* If we still don't have a type use an error marker. */
19763
19764 if (this_type == NULL)
19765 return build_error_marker_type (cu, die);
19766
19767 return this_type;
19768 }
19769
19770 /* Return the type in DIE, CU.
19771 Returns NULL for invalid types.
19772
19773 This first does a lookup in die_type_hash,
19774 and only reads the die in if necessary.
19775
19776 NOTE: This can be called when reading in partial or full symbols. */
19777
19778 static struct type *
19779 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19780 {
19781 struct type *this_type;
19782
19783 this_type = get_die_type (die, cu);
19784 if (this_type)
19785 return this_type;
19786
19787 return read_type_die_1 (die, cu);
19788 }
19789
19790 /* Read the type in DIE, CU.
19791 Returns NULL for invalid types. */
19792
19793 static struct type *
19794 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19795 {
19796 struct type *this_type = NULL;
19797
19798 switch (die->tag)
19799 {
19800 case DW_TAG_class_type:
19801 case DW_TAG_interface_type:
19802 case DW_TAG_structure_type:
19803 case DW_TAG_union_type:
19804 this_type = read_structure_type (die, cu);
19805 break;
19806 case DW_TAG_enumeration_type:
19807 this_type = read_enumeration_type (die, cu);
19808 break;
19809 case DW_TAG_entry_point:
19810 case DW_TAG_subprogram:
19811 case DW_TAG_subroutine_type:
19812 case DW_TAG_inlined_subroutine:
19813 this_type = read_subroutine_type (die, cu);
19814 break;
19815 case DW_TAG_array_type:
19816 this_type = read_array_type (die, cu);
19817 break;
19818 case DW_TAG_set_type:
19819 this_type = read_set_type (die, cu);
19820 break;
19821 case DW_TAG_pointer_type:
19822 this_type = read_tag_pointer_type (die, cu);
19823 break;
19824 case DW_TAG_ptr_to_member_type:
19825 this_type = read_tag_ptr_to_member_type (die, cu);
19826 break;
19827 case DW_TAG_reference_type:
19828 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19829 break;
19830 case DW_TAG_rvalue_reference_type:
19831 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19832 break;
19833 case DW_TAG_const_type:
19834 this_type = read_tag_const_type (die, cu);
19835 break;
19836 case DW_TAG_volatile_type:
19837 this_type = read_tag_volatile_type (die, cu);
19838 break;
19839 case DW_TAG_restrict_type:
19840 this_type = read_tag_restrict_type (die, cu);
19841 break;
19842 case DW_TAG_string_type:
19843 this_type = read_tag_string_type (die, cu);
19844 break;
19845 case DW_TAG_typedef:
19846 this_type = read_typedef (die, cu);
19847 break;
19848 case DW_TAG_generic_subrange:
19849 case DW_TAG_subrange_type:
19850 this_type = read_subrange_type (die, cu);
19851 break;
19852 case DW_TAG_base_type:
19853 this_type = read_base_type (die, cu);
19854 break;
19855 case DW_TAG_unspecified_type:
19856 this_type = read_unspecified_type (die, cu);
19857 break;
19858 case DW_TAG_namespace:
19859 this_type = read_namespace_type (die, cu);
19860 break;
19861 case DW_TAG_module:
19862 this_type = read_module_type (die, cu);
19863 break;
19864 case DW_TAG_atomic_type:
19865 this_type = read_tag_atomic_type (die, cu);
19866 break;
19867 default:
19868 complaint (_("unexpected tag in read_type_die: '%s'"),
19869 dwarf_tag_name (die->tag));
19870 break;
19871 }
19872
19873 return this_type;
19874 }
19875
19876 /* See if we can figure out if the class lives in a namespace. We do
19877 this by looking for a member function; its demangled name will
19878 contain namespace info, if there is any.
19879 Return the computed name or NULL.
19880 Space for the result is allocated on the objfile's obstack.
19881 This is the full-die version of guess_partial_die_structure_name.
19882 In this case we know DIE has no useful parent. */
19883
19884 static const char *
19885 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19886 {
19887 struct die_info *spec_die;
19888 struct dwarf2_cu *spec_cu;
19889 struct die_info *child;
19890 struct objfile *objfile = cu->per_objfile->objfile;
19891
19892 spec_cu = cu;
19893 spec_die = die_specification (die, &spec_cu);
19894 if (spec_die != NULL)
19895 {
19896 die = spec_die;
19897 cu = spec_cu;
19898 }
19899
19900 for (child = die->child;
19901 child != NULL;
19902 child = child->sibling)
19903 {
19904 if (child->tag == DW_TAG_subprogram)
19905 {
19906 const char *linkage_name = dw2_linkage_name (child, cu);
19907
19908 if (linkage_name != NULL)
19909 {
19910 gdb::unique_xmalloc_ptr<char> actual_name
19911 (cu->language_defn->class_name_from_physname (linkage_name));
19912 const char *name = NULL;
19913
19914 if (actual_name != NULL)
19915 {
19916 const char *die_name = dwarf2_name (die, cu);
19917
19918 if (die_name != NULL
19919 && strcmp (die_name, actual_name.get ()) != 0)
19920 {
19921 /* Strip off the class name from the full name.
19922 We want the prefix. */
19923 int die_name_len = strlen (die_name);
19924 int actual_name_len = strlen (actual_name.get ());
19925 const char *ptr = actual_name.get ();
19926
19927 /* Test for '::' as a sanity check. */
19928 if (actual_name_len > die_name_len + 2
19929 && ptr[actual_name_len - die_name_len - 1] == ':')
19930 name = obstack_strndup (
19931 &objfile->per_bfd->storage_obstack,
19932 ptr, actual_name_len - die_name_len - 2);
19933 }
19934 }
19935 return name;
19936 }
19937 }
19938 }
19939
19940 return NULL;
19941 }
19942
19943 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19944 prefix part in such case. See
19945 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19946
19947 static const char *
19948 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19949 {
19950 struct attribute *attr;
19951 const char *base;
19952
19953 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19954 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19955 return NULL;
19956
19957 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19958 return NULL;
19959
19960 attr = dw2_linkage_name_attr (die, cu);
19961 const char *attr_name = attr->as_string ();
19962 if (attr == NULL || attr_name == NULL)
19963 return NULL;
19964
19965 /* dwarf2_name had to be already called. */
19966 gdb_assert (attr->canonical_string_p ());
19967
19968 /* Strip the base name, keep any leading namespaces/classes. */
19969 base = strrchr (attr_name, ':');
19970 if (base == NULL || base == attr_name || base[-1] != ':')
19971 return "";
19972
19973 struct objfile *objfile = cu->per_objfile->objfile;
19974 return obstack_strndup (&objfile->per_bfd->storage_obstack,
19975 attr_name,
19976 &base[-1] - attr_name);
19977 }
19978
19979 /* Return the name of the namespace/class that DIE is defined within,
19980 or "" if we can't tell. The caller should not xfree the result.
19981
19982 For example, if we're within the method foo() in the following
19983 code:
19984
19985 namespace N {
19986 class C {
19987 void foo () {
19988 }
19989 };
19990 }
19991
19992 then determine_prefix on foo's die will return "N::C". */
19993
19994 static const char *
19995 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19996 {
19997 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19998 struct die_info *parent, *spec_die;
19999 struct dwarf2_cu *spec_cu;
20000 struct type *parent_type;
20001 const char *retval;
20002
20003 if (cu->lang () != language_cplus
20004 && cu->lang () != language_fortran
20005 && cu->lang () != language_d
20006 && cu->lang () != language_rust)
20007 return "";
20008
20009 retval = anonymous_struct_prefix (die, cu);
20010 if (retval)
20011 return retval;
20012
20013 /* We have to be careful in the presence of DW_AT_specification.
20014 For example, with GCC 3.4, given the code
20015
20016 namespace N {
20017 void foo() {
20018 // Definition of N::foo.
20019 }
20020 }
20021
20022 then we'll have a tree of DIEs like this:
20023
20024 1: DW_TAG_compile_unit
20025 2: DW_TAG_namespace // N
20026 3: DW_TAG_subprogram // declaration of N::foo
20027 4: DW_TAG_subprogram // definition of N::foo
20028 DW_AT_specification // refers to die #3
20029
20030 Thus, when processing die #4, we have to pretend that we're in
20031 the context of its DW_AT_specification, namely the context of die
20032 #3. */
20033 spec_cu = cu;
20034 spec_die = die_specification (die, &spec_cu);
20035 if (spec_die == NULL)
20036 parent = die->parent;
20037 else
20038 {
20039 parent = spec_die->parent;
20040 cu = spec_cu;
20041 }
20042
20043 if (parent == NULL)
20044 return "";
20045 else if (parent->building_fullname)
20046 {
20047 const char *name;
20048 const char *parent_name;
20049
20050 /* It has been seen on RealView 2.2 built binaries,
20051 DW_TAG_template_type_param types actually _defined_ as
20052 children of the parent class:
20053
20054 enum E {};
20055 template class <class Enum> Class{};
20056 Class<enum E> class_e;
20057
20058 1: DW_TAG_class_type (Class)
20059 2: DW_TAG_enumeration_type (E)
20060 3: DW_TAG_enumerator (enum1:0)
20061 3: DW_TAG_enumerator (enum2:1)
20062 ...
20063 2: DW_TAG_template_type_param
20064 DW_AT_type DW_FORM_ref_udata (E)
20065
20066 Besides being broken debug info, it can put GDB into an
20067 infinite loop. Consider:
20068
20069 When we're building the full name for Class<E>, we'll start
20070 at Class, and go look over its template type parameters,
20071 finding E. We'll then try to build the full name of E, and
20072 reach here. We're now trying to build the full name of E,
20073 and look over the parent DIE for containing scope. In the
20074 broken case, if we followed the parent DIE of E, we'd again
20075 find Class, and once again go look at its template type
20076 arguments, etc., etc. Simply don't consider such parent die
20077 as source-level parent of this die (it can't be, the language
20078 doesn't allow it), and break the loop here. */
20079 name = dwarf2_name (die, cu);
20080 parent_name = dwarf2_name (parent, cu);
20081 complaint (_("template param type '%s' defined within parent '%s'"),
20082 name ? name : "<unknown>",
20083 parent_name ? parent_name : "<unknown>");
20084 return "";
20085 }
20086 else
20087 switch (parent->tag)
20088 {
20089 case DW_TAG_namespace:
20090 parent_type = read_type_die (parent, cu);
20091 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20092 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20093 Work around this problem here. */
20094 if (cu->lang () == language_cplus
20095 && strcmp (parent_type->name (), "::") == 0)
20096 return "";
20097 /* We give a name to even anonymous namespaces. */
20098 return parent_type->name ();
20099 case DW_TAG_class_type:
20100 case DW_TAG_interface_type:
20101 case DW_TAG_structure_type:
20102 case DW_TAG_union_type:
20103 case DW_TAG_module:
20104 parent_type = read_type_die (parent, cu);
20105 if (parent_type->name () != NULL)
20106 return parent_type->name ();
20107 else
20108 /* An anonymous structure is only allowed non-static data
20109 members; no typedefs, no member functions, et cetera.
20110 So it does not need a prefix. */
20111 return "";
20112 case DW_TAG_compile_unit:
20113 case DW_TAG_partial_unit:
20114 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20115 if (cu->lang () == language_cplus
20116 && !per_objfile->per_bfd->types.empty ()
20117 && die->child != NULL
20118 && (die->tag == DW_TAG_class_type
20119 || die->tag == DW_TAG_structure_type
20120 || die->tag == DW_TAG_union_type))
20121 {
20122 const char *name = guess_full_die_structure_name (die, cu);
20123 if (name != NULL)
20124 return name;
20125 }
20126 return "";
20127 case DW_TAG_subprogram:
20128 /* Nested subroutines in Fortran get a prefix with the name
20129 of the parent's subroutine. Entry points are prefixed by the
20130 parent's namespace. */
20131 if (cu->lang () == language_fortran)
20132 {
20133 if ((die->tag == DW_TAG_subprogram)
20134 && (dwarf2_name (parent, cu) != NULL))
20135 return dwarf2_name (parent, cu);
20136 else if (die->tag == DW_TAG_entry_point)
20137 return determine_prefix (parent, cu);
20138 }
20139 return "";
20140 case DW_TAG_enumeration_type:
20141 parent_type = read_type_die (parent, cu);
20142 if (parent_type->is_declared_class ())
20143 {
20144 if (parent_type->name () != NULL)
20145 return parent_type->name ();
20146 return "";
20147 }
20148 [[fallthrough]];
20149 default:
20150 return determine_prefix (parent, cu);
20151 }
20152 }
20153
20154 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20155 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20156 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20157 an obconcat, otherwise allocate storage for the result. The CU argument is
20158 used to determine the language and hence, the appropriate separator. */
20159
20160 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20161
20162 static char *
20163 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
20164 int physname, struct dwarf2_cu *cu)
20165 {
20166 const char *lead = "";
20167 const char *sep;
20168
20169 if (suffix == NULL || suffix[0] == '\0'
20170 || prefix == NULL || prefix[0] == '\0')
20171 sep = "";
20172 else if (cu->lang () == language_d)
20173 {
20174 /* For D, the 'main' function could be defined in any module, but it
20175 should never be prefixed. */
20176 if (strcmp (suffix, "D main") == 0)
20177 {
20178 prefix = "";
20179 sep = "";
20180 }
20181 else
20182 sep = ".";
20183 }
20184 else if (cu->lang () == language_fortran && physname)
20185 {
20186 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20187 DW_AT_MIPS_linkage_name is preferred and used instead. */
20188
20189 lead = "__";
20190 sep = "_MOD_";
20191 }
20192 else
20193 sep = "::";
20194
20195 if (prefix == NULL)
20196 prefix = "";
20197 if (suffix == NULL)
20198 suffix = "";
20199
20200 if (obs == NULL)
20201 {
20202 char *retval
20203 = ((char *)
20204 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20205
20206 strcpy (retval, lead);
20207 strcat (retval, prefix);
20208 strcat (retval, sep);
20209 strcat (retval, suffix);
20210 return retval;
20211 }
20212 else
20213 {
20214 /* We have an obstack. */
20215 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20216 }
20217 }
20218
20219 /* Return a generic name for a DW_TAG_template_type_param or
20220 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
20221 per parent, so each function/class/struct template will have their own set
20222 of template parameters named <unnnamed0>, <unnamed1>, ... where the
20223 enumeration starts at 0 and represents the position of the template tag in
20224 the list of unnamed template tags for this parent, counting both, type and
20225 value tags. */
20226
20227 static const char *
20228 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
20229 {
20230 if (die->parent == nullptr)
20231 return nullptr;
20232
20233 /* Count the parent types unnamed template type and value children until, we
20234 arrive at our entry. */
20235 size_t nth_unnamed = 0;
20236
20237 die_info *child = die->parent->child;
20238 while (child != die)
20239 {
20240 gdb_assert (child != nullptr);
20241 if (child->tag == DW_TAG_template_type_param
20242 || child->tag == DW_TAG_template_value_param)
20243 {
20244 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
20245 ++nth_unnamed;
20246 }
20247 child = child->sibling;
20248 }
20249
20250 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
20251 return cu->per_objfile->objfile->intern (name_str.c_str ());
20252 }
20253
20254 /* Get name of a die, return NULL if not found. */
20255
20256 static const char *
20257 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20258 struct objfile *objfile)
20259 {
20260 if (name == nullptr)
20261 return name;
20262
20263 if (cu->lang () == language_cplus)
20264 {
20265 gdb::unique_xmalloc_ptr<char> canon_name
20266 = cp_canonicalize_string (name);
20267
20268 if (canon_name != nullptr)
20269 name = objfile->intern (canon_name.get ());
20270 }
20271 else if (cu->lang () == language_c)
20272 {
20273 gdb::unique_xmalloc_ptr<char> canon_name
20274 = c_canonicalize_name (name);
20275
20276 if (canon_name != nullptr)
20277 name = objfile->intern (canon_name.get ());
20278 }
20279
20280 return name;
20281 }
20282
20283 /* Get name of a die, return NULL if not found.
20284 Anonymous namespaces are converted to their magic string. */
20285
20286 static const char *
20287 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20288 {
20289 struct attribute *attr;
20290 struct objfile *objfile = cu->per_objfile->objfile;
20291
20292 attr = dwarf2_attr (die, DW_AT_name, cu);
20293 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
20294 if (attr_name == nullptr
20295 && die->tag != DW_TAG_namespace
20296 && die->tag != DW_TAG_class_type
20297 && die->tag != DW_TAG_interface_type
20298 && die->tag != DW_TAG_structure_type
20299 && die->tag != DW_TAG_namelist
20300 && die->tag != DW_TAG_union_type
20301 && die->tag != DW_TAG_template_type_param
20302 && die->tag != DW_TAG_template_value_param)
20303 return NULL;
20304
20305 switch (die->tag)
20306 {
20307 /* A member's name should not be canonicalized. This is a bit
20308 of a hack, in that normally it should not be possible to run
20309 into this situation; however, the dw2-unusual-field-names.exp
20310 test creates custom DWARF that does. */
20311 case DW_TAG_member:
20312 case DW_TAG_compile_unit:
20313 case DW_TAG_partial_unit:
20314 /* Compilation units have a DW_AT_name that is a filename, not
20315 a source language identifier. */
20316 case DW_TAG_enumeration_type:
20317 case DW_TAG_enumerator:
20318 /* These tags always have simple identifiers already; no need
20319 to canonicalize them. */
20320 return attr_name;
20321
20322 case DW_TAG_namespace:
20323 if (attr_name != nullptr)
20324 return attr_name;
20325 return CP_ANONYMOUS_NAMESPACE_STR;
20326
20327 /* DWARF does not actually require template tags to have a name. */
20328 case DW_TAG_template_type_param:
20329 case DW_TAG_template_value_param:
20330 if (attr_name == nullptr)
20331 return unnamed_template_tag_name (die, cu);
20332 [[fallthrough]];
20333 case DW_TAG_class_type:
20334 case DW_TAG_interface_type:
20335 case DW_TAG_structure_type:
20336 case DW_TAG_union_type:
20337 case DW_TAG_namelist:
20338 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20339 structures or unions. These were of the form "._%d" in GCC 4.1,
20340 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20341 and GCC 4.4. We work around this problem by ignoring these. */
20342 if (attr_name != nullptr
20343 && (startswith (attr_name, "._")
20344 || startswith (attr_name, "<anonymous")))
20345 return NULL;
20346
20347 /* GCC might emit a nameless typedef that has a linkage name. See
20348 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20349 if (!attr || attr_name == NULL)
20350 {
20351 attr = dw2_linkage_name_attr (die, cu);
20352 attr_name = attr == nullptr ? nullptr : attr->as_string ();
20353 if (attr == NULL || attr_name == NULL)
20354 return NULL;
20355
20356 /* Avoid demangling attr_name the second time on a second
20357 call for the same DIE. */
20358 if (!attr->canonical_string_p ())
20359 {
20360 gdb::unique_xmalloc_ptr<char> demangled
20361 (gdb_demangle (attr_name, DMGL_TYPES));
20362 if (demangled == nullptr)
20363 return nullptr;
20364
20365 attr->set_string_canonical (objfile->intern (demangled.get ()));
20366 attr_name = attr->as_string ();
20367 }
20368
20369 /* Strip any leading namespaces/classes, keep only the
20370 base name. DW_AT_name for named DIEs does not
20371 contain the prefixes. */
20372 const char *base = strrchr (attr_name, ':');
20373 if (base && base > attr_name && base[-1] == ':')
20374 return &base[1];
20375 else
20376 return attr_name;
20377 }
20378 break;
20379
20380 default:
20381 break;
20382 }
20383
20384 if (!attr->canonical_string_p ())
20385 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
20386 objfile));
20387 return attr->as_string ();
20388 }
20389
20390 /* Return the die that this die in an extension of, or NULL if there
20391 is none. *EXT_CU is the CU containing DIE on input, and the CU
20392 containing the return value on output. */
20393
20394 static struct die_info *
20395 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20396 {
20397 struct attribute *attr;
20398
20399 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20400 if (attr == NULL)
20401 return NULL;
20402
20403 return follow_die_ref (die, attr, ext_cu);
20404 }
20405
20406 static void
20407 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20408 {
20409 void **slot;
20410
20411 slot = htab_find_slot_with_hash (cu->die_hash, die,
20412 to_underlying (die->sect_off),
20413 INSERT);
20414
20415 *slot = die;
20416 }
20417
20418 /* Follow reference or signature attribute ATTR of SRC_DIE.
20419 On entry *REF_CU is the CU of SRC_DIE.
20420 On exit *REF_CU is the CU of the result. */
20421
20422 static struct die_info *
20423 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20424 struct dwarf2_cu **ref_cu)
20425 {
20426 struct die_info *die;
20427
20428 if (attr->form_is_ref ())
20429 die = follow_die_ref (src_die, attr, ref_cu);
20430 else if (attr->form == DW_FORM_ref_sig8)
20431 die = follow_die_sig (src_die, attr, ref_cu);
20432 else
20433 {
20434 src_die->error_dump ();
20435 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20436 objfile_name ((*ref_cu)->per_objfile->objfile));
20437 }
20438
20439 return die;
20440 }
20441
20442 /* Follow reference OFFSET.
20443 On entry *REF_CU is the CU of the source die referencing OFFSET.
20444 On exit *REF_CU is the CU of the result.
20445 Returns NULL if OFFSET is invalid. */
20446
20447 static struct die_info *
20448 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20449 struct dwarf2_cu **ref_cu)
20450 {
20451 struct die_info temp_die;
20452 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20453 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20454
20455 gdb_assert (cu->per_cu != NULL);
20456
20457 target_cu = cu;
20458
20459 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
20460 "source CU contains target offset: %d",
20461 sect_offset_str (cu->per_cu->sect_off),
20462 sect_offset_str (sect_off),
20463 cu->header.offset_in_cu_p (sect_off));
20464
20465 if (cu->per_cu->is_debug_types)
20466 {
20467 /* .debug_types CUs cannot reference anything outside their CU.
20468 If they need to, they have to reference a signatured type via
20469 DW_FORM_ref_sig8. */
20470 if (!cu->header.offset_in_cu_p (sect_off))
20471 return NULL;
20472 }
20473 else if (offset_in_dwz != cu->per_cu->is_dwz
20474 || !cu->header.offset_in_cu_p (sect_off))
20475 {
20476 struct dwarf2_per_cu_data *per_cu;
20477
20478 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20479 per_objfile->per_bfd);
20480
20481 dwarf_read_debug_printf_v ("target CU offset: %s, "
20482 "target CU DIEs loaded: %d",
20483 sect_offset_str (per_cu->sect_off),
20484 per_objfile->get_cu (per_cu) != nullptr);
20485
20486 /* If necessary, add it to the queue and load its DIEs.
20487
20488 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20489 it doesn't mean they are currently loaded. Since we require them
20490 to be loaded, we must check for ourselves. */
20491 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
20492 || per_objfile->get_cu (per_cu) == nullptr)
20493 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
20494 false, cu->lang ());
20495
20496 target_cu = per_objfile->get_cu (per_cu);
20497 gdb_assert (target_cu != nullptr);
20498 }
20499 else if (cu->dies == NULL)
20500 {
20501 /* We're loading full DIEs during partial symbol reading. */
20502 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
20503 language_minimal);
20504 }
20505
20506 *ref_cu = target_cu;
20507 temp_die.sect_off = sect_off;
20508
20509 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20510 &temp_die,
20511 to_underlying (sect_off));
20512 }
20513
20514 /* Follow reference attribute ATTR of SRC_DIE.
20515 On entry *REF_CU is the CU of SRC_DIE.
20516 On exit *REF_CU is the CU of the result. */
20517
20518 static struct die_info *
20519 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20520 struct dwarf2_cu **ref_cu)
20521 {
20522 sect_offset sect_off = attr->get_ref_die_offset ();
20523 struct dwarf2_cu *cu = *ref_cu;
20524 struct die_info *die;
20525
20526 if (attr->form != DW_FORM_GNU_ref_alt && src_die->sect_off == sect_off)
20527 {
20528 /* Self-reference, we're done. */
20529 return src_die;
20530 }
20531
20532 die = follow_die_offset (sect_off,
20533 (attr->form == DW_FORM_GNU_ref_alt
20534 || cu->per_cu->is_dwz),
20535 ref_cu);
20536 if (!die)
20537 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
20538 "at %s [in module %s]"),
20539 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
20540 objfile_name (cu->per_objfile->objfile));
20541
20542 return die;
20543 }
20544
20545 /* See read.h. */
20546
20547 struct dwarf2_locexpr_baton
20548 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20549 dwarf2_per_cu_data *per_cu,
20550 dwarf2_per_objfile *per_objfile,
20551 gdb::function_view<CORE_ADDR ()> get_frame_pc,
20552 bool resolve_abstract_p)
20553 {
20554 struct die_info *die;
20555 struct attribute *attr;
20556 struct dwarf2_locexpr_baton retval;
20557 struct objfile *objfile = per_objfile->objfile;
20558
20559 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20560 if (cu == nullptr)
20561 cu = load_cu (per_cu, per_objfile, false);
20562
20563 if (cu == nullptr)
20564 {
20565 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20566 Instead just throw an error, not much else we can do. */
20567 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20568 sect_offset_str (sect_off), objfile_name (objfile));
20569 }
20570
20571 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20572 if (!die)
20573 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20574 sect_offset_str (sect_off), objfile_name (objfile));
20575
20576 attr = dwarf2_attr (die, DW_AT_location, cu);
20577 if (!attr && resolve_abstract_p
20578 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
20579 != per_objfile->per_bfd->abstract_to_concrete.end ()))
20580 {
20581 CORE_ADDR pc = get_frame_pc ();
20582
20583 for (const auto &cand_off
20584 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
20585 {
20586 struct dwarf2_cu *cand_cu = cu;
20587 struct die_info *cand
20588 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
20589 if (!cand
20590 || !cand->parent
20591 || cand->parent->tag != DW_TAG_subprogram)
20592 continue;
20593
20594 unrelocated_addr unrel_low, unrel_high;
20595 get_scope_pc_bounds (cand->parent, &unrel_low, &unrel_high, cu);
20596 if (unrel_low == ((unrelocated_addr) -1))
20597 continue;
20598 CORE_ADDR pc_low = per_objfile->relocate (unrel_low);
20599 CORE_ADDR pc_high = per_objfile->relocate (unrel_high);
20600 if (!(pc_low <= pc && pc < pc_high))
20601 continue;
20602
20603 die = cand;
20604 attr = dwarf2_attr (die, DW_AT_location, cu);
20605 break;
20606 }
20607 }
20608
20609 if (!attr)
20610 {
20611 /* DWARF: "If there is no such attribute, then there is no effect.".
20612 DATA is ignored if SIZE is 0. */
20613
20614 retval.data = NULL;
20615 retval.size = 0;
20616 }
20617 else if (attr->form_is_section_offset ())
20618 {
20619 struct dwarf2_loclist_baton loclist_baton;
20620 CORE_ADDR pc = get_frame_pc ();
20621 size_t size;
20622
20623 fill_in_loclist_baton (cu, &loclist_baton, attr);
20624
20625 retval.data = dwarf2_find_location_expression (&loclist_baton,
20626 &size, pc);
20627 retval.size = size;
20628 }
20629 else
20630 {
20631 if (!attr->form_is_block ())
20632 error (_("Dwarf Error: DIE at %s referenced in module %s "
20633 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20634 sect_offset_str (sect_off), objfile_name (objfile));
20635
20636 struct dwarf_block *block = attr->as_block ();
20637 retval.data = block->data;
20638 retval.size = block->size;
20639 }
20640 retval.per_objfile = per_objfile;
20641 retval.per_cu = cu->per_cu;
20642
20643 per_objfile->age_comp_units ();
20644
20645 return retval;
20646 }
20647
20648 /* See read.h. */
20649
20650 struct dwarf2_locexpr_baton
20651 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20652 dwarf2_per_cu_data *per_cu,
20653 dwarf2_per_objfile *per_objfile,
20654 gdb::function_view<CORE_ADDR ()> get_frame_pc)
20655 {
20656 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20657
20658 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
20659 get_frame_pc);
20660 }
20661
20662 /* Write a constant of a given type as target-ordered bytes into
20663 OBSTACK. */
20664
20665 static const gdb_byte *
20666 write_constant_as_bytes (struct obstack *obstack,
20667 enum bfd_endian byte_order,
20668 struct type *type,
20669 ULONGEST value,
20670 LONGEST *len)
20671 {
20672 gdb_byte *result;
20673
20674 *len = type->length ();
20675 result = (gdb_byte *) obstack_alloc (obstack, *len);
20676 store_unsigned_integer (result, *len, byte_order, value);
20677
20678 return result;
20679 }
20680
20681 /* See read.h. */
20682
20683 const gdb_byte *
20684 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20685 dwarf2_per_cu_data *per_cu,
20686 dwarf2_per_objfile *per_objfile,
20687 obstack *obstack,
20688 LONGEST *len)
20689 {
20690 struct die_info *die;
20691 struct attribute *attr;
20692 const gdb_byte *result = NULL;
20693 struct type *type;
20694 LONGEST value;
20695 enum bfd_endian byte_order;
20696 struct objfile *objfile = per_objfile->objfile;
20697
20698 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20699 if (cu == nullptr)
20700 cu = load_cu (per_cu, per_objfile, false);
20701
20702 if (cu == nullptr)
20703 {
20704 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20705 Instead just throw an error, not much else we can do. */
20706 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20707 sect_offset_str (sect_off), objfile_name (objfile));
20708 }
20709
20710 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20711 if (!die)
20712 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20713 sect_offset_str (sect_off), objfile_name (objfile));
20714
20715 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20716 if (attr == NULL)
20717 return NULL;
20718
20719 byte_order = (bfd_big_endian (objfile->obfd.get ())
20720 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20721
20722 switch (attr->form)
20723 {
20724 case DW_FORM_addr:
20725 case DW_FORM_addrx:
20726 case DW_FORM_GNU_addr_index:
20727 {
20728 gdb_byte *tem;
20729
20730 *len = cu->header.addr_size;
20731 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20732 store_unsigned_integer (tem, *len, byte_order,
20733 (ULONGEST) attr->as_address ());
20734 result = tem;
20735 }
20736 break;
20737 case DW_FORM_string:
20738 case DW_FORM_strp:
20739 case DW_FORM_strx:
20740 case DW_FORM_GNU_str_index:
20741 case DW_FORM_GNU_strp_alt:
20742 /* The string is already allocated on the objfile obstack, point
20743 directly to it. */
20744 {
20745 const char *attr_name = attr->as_string ();
20746 result = (const gdb_byte *) attr_name;
20747 *len = strlen (attr_name);
20748 }
20749 break;
20750 case DW_FORM_block1:
20751 case DW_FORM_block2:
20752 case DW_FORM_block4:
20753 case DW_FORM_block:
20754 case DW_FORM_exprloc:
20755 case DW_FORM_data16:
20756 {
20757 struct dwarf_block *block = attr->as_block ();
20758 result = block->data;
20759 *len = block->size;
20760 }
20761 break;
20762
20763 /* The DW_AT_const_value attributes are supposed to carry the
20764 symbol's value "represented as it would be on the target
20765 architecture." By the time we get here, it's already been
20766 converted to host endianness, so we just need to sign- or
20767 zero-extend it as appropriate. */
20768 case DW_FORM_data1:
20769 type = die_type (die, cu);
20770 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20771 if (result == NULL)
20772 result = write_constant_as_bytes (obstack, byte_order,
20773 type, value, len);
20774 break;
20775 case DW_FORM_data2:
20776 type = die_type (die, cu);
20777 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20778 if (result == NULL)
20779 result = write_constant_as_bytes (obstack, byte_order,
20780 type, value, len);
20781 break;
20782 case DW_FORM_data4:
20783 type = die_type (die, cu);
20784 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20785 if (result == NULL)
20786 result = write_constant_as_bytes (obstack, byte_order,
20787 type, value, len);
20788 break;
20789 case DW_FORM_data8:
20790 type = die_type (die, cu);
20791 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20792 if (result == NULL)
20793 result = write_constant_as_bytes (obstack, byte_order,
20794 type, value, len);
20795 break;
20796
20797 case DW_FORM_sdata:
20798 case DW_FORM_implicit_const:
20799 type = die_type (die, cu);
20800 result = write_constant_as_bytes (obstack, byte_order,
20801 type, attr->as_signed (), len);
20802 break;
20803
20804 case DW_FORM_udata:
20805 type = die_type (die, cu);
20806 result = write_constant_as_bytes (obstack, byte_order,
20807 type, attr->as_unsigned (), len);
20808 break;
20809
20810 default:
20811 complaint (_("unsupported const value attribute form: '%s'"),
20812 dwarf_form_name (attr->form));
20813 break;
20814 }
20815
20816 return result;
20817 }
20818
20819 /* See read.h. */
20820
20821 struct type *
20822 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20823 dwarf2_per_cu_data *per_cu,
20824 dwarf2_per_objfile *per_objfile,
20825 const char **var_name)
20826 {
20827 struct die_info *die;
20828
20829 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20830 if (cu == nullptr)
20831 cu = load_cu (per_cu, per_objfile, false);
20832
20833 if (cu == nullptr)
20834 return nullptr;
20835
20836 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20837 if (!die)
20838 return NULL;
20839
20840 if (var_name != nullptr)
20841 *var_name = var_decl_name (die, cu);
20842 return die_type (die, cu);
20843 }
20844
20845 /* See read.h. */
20846
20847 struct type *
20848 dwarf2_get_die_type (cu_offset die_offset,
20849 dwarf2_per_cu_data *per_cu,
20850 dwarf2_per_objfile *per_objfile)
20851 {
20852 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20853 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
20854 }
20855
20856 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20857 On entry *REF_CU is the CU of SRC_DIE.
20858 On exit *REF_CU is the CU of the result.
20859 Returns NULL if the referenced DIE isn't found. */
20860
20861 static struct die_info *
20862 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20863 struct dwarf2_cu **ref_cu)
20864 {
20865 struct die_info temp_die;
20866 struct dwarf2_cu *sig_cu;
20867 struct die_info *die;
20868 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
20869
20870
20871 /* While it might be nice to assert sig_type->type == NULL here,
20872 we can get here for DW_AT_imported_declaration where we need
20873 the DIE not the type. */
20874
20875 /* If necessary, add it to the queue and load its DIEs.
20876
20877 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20878 it doesn't mean they are currently loaded. Since we require them
20879 to be loaded, we must check for ourselves. */
20880 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
20881 language_minimal)
20882 || per_objfile->get_cu (sig_type) == nullptr)
20883 read_signatured_type (sig_type, per_objfile);
20884
20885 sig_cu = per_objfile->get_cu (sig_type);
20886 gdb_assert (sig_cu != NULL);
20887 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20888 temp_die.sect_off = sig_type->type_offset_in_section;
20889 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20890 to_underlying (temp_die.sect_off));
20891 if (die)
20892 {
20893 /* For .gdb_index version 7 keep track of included TUs.
20894 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20895 if (per_objfile->per_bfd->index_table != NULL
20896 && !per_objfile->per_bfd->index_table->version_check ())
20897 {
20898 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
20899 }
20900
20901 *ref_cu = sig_cu;
20902 return die;
20903 }
20904
20905 return NULL;
20906 }
20907
20908 /* Follow signatured type referenced by ATTR in SRC_DIE.
20909 On entry *REF_CU is the CU of SRC_DIE.
20910 On exit *REF_CU is the CU of the result.
20911 The result is the DIE of the type.
20912 If the referenced type cannot be found an error is thrown. */
20913
20914 static struct die_info *
20915 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20916 struct dwarf2_cu **ref_cu)
20917 {
20918 ULONGEST signature = attr->as_signature ();
20919 struct signatured_type *sig_type;
20920 struct die_info *die;
20921
20922 gdb_assert (attr->form == DW_FORM_ref_sig8);
20923
20924 sig_type = lookup_signatured_type (*ref_cu, signature);
20925 /* sig_type will be NULL if the signatured type is missing from
20926 the debug info. */
20927 if (sig_type == NULL)
20928 {
20929 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20930 " from DIE at %s [in module %s]"),
20931 hex_string (signature), sect_offset_str (src_die->sect_off),
20932 objfile_name ((*ref_cu)->per_objfile->objfile));
20933 }
20934
20935 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20936 if (die == NULL)
20937 {
20938 src_die->error_dump ();
20939 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20940 " from DIE at %s [in module %s]"),
20941 hex_string (signature), sect_offset_str (src_die->sect_off),
20942 objfile_name ((*ref_cu)->per_objfile->objfile));
20943 }
20944
20945 return die;
20946 }
20947
20948 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20949 reading in and processing the type unit if necessary. */
20950
20951 static struct type *
20952 get_signatured_type (struct die_info *die, ULONGEST signature,
20953 struct dwarf2_cu *cu)
20954 {
20955 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20956 struct signatured_type *sig_type;
20957 struct dwarf2_cu *type_cu;
20958 struct die_info *type_die;
20959 struct type *type;
20960
20961 sig_type = lookup_signatured_type (cu, signature);
20962 /* sig_type will be NULL if the signatured type is missing from
20963 the debug info. */
20964 if (sig_type == NULL)
20965 {
20966 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20967 " from DIE at %s [in module %s]"),
20968 hex_string (signature), sect_offset_str (die->sect_off),
20969 objfile_name (per_objfile->objfile));
20970 return build_error_marker_type (cu, die);
20971 }
20972
20973 /* If we already know the type we're done. */
20974 type = per_objfile->get_type_for_signatured_type (sig_type);
20975 if (type != nullptr)
20976 return type;
20977
20978 type_cu = cu;
20979 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20980 if (type_die != NULL)
20981 {
20982 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20983 is created. This is important, for example, because for c++ classes
20984 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20985 type = read_type_die (type_die, type_cu);
20986 if (type == NULL)
20987 {
20988 complaint (_("Dwarf Error: Cannot build signatured type %s"
20989 " referenced from DIE at %s [in module %s]"),
20990 hex_string (signature), sect_offset_str (die->sect_off),
20991 objfile_name (per_objfile->objfile));
20992 type = build_error_marker_type (cu, die);
20993 }
20994 }
20995 else
20996 {
20997 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20998 " from DIE at %s [in module %s]"),
20999 hex_string (signature), sect_offset_str (die->sect_off),
21000 objfile_name (per_objfile->objfile));
21001 type = build_error_marker_type (cu, die);
21002 }
21003
21004 per_objfile->set_type_for_signatured_type (sig_type, type);
21005
21006 return type;
21007 }
21008
21009 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21010 reading in and processing the type unit if necessary. */
21011
21012 static struct type *
21013 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
21014 struct dwarf2_cu *cu) /* ARI: editCase function */
21015 {
21016 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21017 if (attr->form_is_ref ())
21018 {
21019 struct dwarf2_cu *type_cu = cu;
21020 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
21021
21022 return read_type_die (type_die, type_cu);
21023 }
21024 else if (attr->form == DW_FORM_ref_sig8)
21025 {
21026 return get_signatured_type (die, attr->as_signature (), cu);
21027 }
21028 else
21029 {
21030 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21031
21032 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21033 " at %s [in module %s]"),
21034 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
21035 objfile_name (per_objfile->objfile));
21036 return build_error_marker_type (cu, die);
21037 }
21038 }
21039
21040 /* Load the DIEs associated with type unit PER_CU into memory. */
21041
21042 static void
21043 load_full_type_unit (dwarf2_per_cu_data *per_cu,
21044 dwarf2_per_objfile *per_objfile)
21045 {
21046 struct signatured_type *sig_type;
21047
21048 /* We have the per_cu, but we need the signatured_type.
21049 Fortunately this is an easy translation. */
21050 gdb_assert (per_cu->is_debug_types);
21051 sig_type = (struct signatured_type *) per_cu;
21052
21053 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
21054
21055 read_signatured_type (sig_type, per_objfile);
21056
21057 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
21058 }
21059
21060 /* Read in a signatured type and build its CU and DIEs.
21061 If the type is a stub for the real type in a DWO file,
21062 read in the real type from the DWO file as well. */
21063
21064 static void
21065 read_signatured_type (signatured_type *sig_type,
21066 dwarf2_per_objfile *per_objfile)
21067 {
21068 gdb_assert (sig_type->is_debug_types);
21069 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
21070
21071 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
21072
21073 if (!reader.dummy_p)
21074 {
21075 struct dwarf2_cu *cu = reader.cu;
21076 const gdb_byte *info_ptr = reader.info_ptr;
21077
21078 gdb_assert (cu->die_hash == NULL);
21079 cu->die_hash =
21080 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
21081 die_info::hash,
21082 die_info::eq,
21083 NULL,
21084 &cu->comp_unit_obstack,
21085 hashtab_obstack_allocate,
21086 dummy_obstack_deallocate);
21087
21088 if (reader.comp_unit_die->has_children)
21089 reader.comp_unit_die->child
21090 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
21091 reader.comp_unit_die);
21092 cu->dies = reader.comp_unit_die;
21093 /* comp_unit_die is not stored in die_hash, no need. */
21094
21095 /* We try not to read any attributes in this function, because
21096 not all CUs needed for references have been loaded yet, and
21097 symbol table processing isn't initialized. But we have to
21098 set the CU language, or we won't be able to build types
21099 correctly. Similarly, if we do not read the producer, we can
21100 not apply producer-specific interpretation. */
21101 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21102
21103 reader.keep ();
21104 }
21105
21106 sig_type->tu_read = 1;
21107 }
21108
21109 /* Decode simple location descriptions.
21110
21111 Given a pointer to a DWARF block that defines a location, compute
21112 the location. Returns true if the expression was computable by
21113 this function, false otherwise. On a true return, *RESULT is set.
21114
21115 Note that this function does not implement a full DWARF expression
21116 evaluator. Instead, it is used for a few limited purposes:
21117
21118 - Getting the address of a symbol that has a constant address. For
21119 example, if a symbol has a location like "DW_OP_addr", the address
21120 can be extracted.
21121
21122 - Getting the offset of a virtual function in its vtable. There
21123 are two forms of this, one of which involves DW_OP_deref -- so this
21124 function handles derefs in a peculiar way to make this 'work'.
21125 (Probably this area should be rewritten.)
21126
21127 - Getting the offset of a field, when it is constant.
21128
21129 Opcodes that cannot be part of a constant expression, for example
21130 those involving registers, simply result in a return of
21131 'false'.
21132
21133 This function may emit a complaint. */
21134
21135 static bool
21136 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu,
21137 CORE_ADDR *result)
21138 {
21139 struct objfile *objfile = cu->per_objfile->objfile;
21140 size_t i;
21141 size_t size = blk->size;
21142 const gdb_byte *data = blk->data;
21143 CORE_ADDR stack[64];
21144 int stacki;
21145 unsigned int bytes_read;
21146 gdb_byte op;
21147
21148 *result = 0;
21149 i = 0;
21150 stacki = 0;
21151 stack[stacki] = 0;
21152 stack[++stacki] = 0;
21153
21154 while (i < size)
21155 {
21156 op = data[i++];
21157 switch (op)
21158 {
21159 case DW_OP_lit0:
21160 case DW_OP_lit1:
21161 case DW_OP_lit2:
21162 case DW_OP_lit3:
21163 case DW_OP_lit4:
21164 case DW_OP_lit5:
21165 case DW_OP_lit6:
21166 case DW_OP_lit7:
21167 case DW_OP_lit8:
21168 case DW_OP_lit9:
21169 case DW_OP_lit10:
21170 case DW_OP_lit11:
21171 case DW_OP_lit12:
21172 case DW_OP_lit13:
21173 case DW_OP_lit14:
21174 case DW_OP_lit15:
21175 case DW_OP_lit16:
21176 case DW_OP_lit17:
21177 case DW_OP_lit18:
21178 case DW_OP_lit19:
21179 case DW_OP_lit20:
21180 case DW_OP_lit21:
21181 case DW_OP_lit22:
21182 case DW_OP_lit23:
21183 case DW_OP_lit24:
21184 case DW_OP_lit25:
21185 case DW_OP_lit26:
21186 case DW_OP_lit27:
21187 case DW_OP_lit28:
21188 case DW_OP_lit29:
21189 case DW_OP_lit30:
21190 case DW_OP_lit31:
21191 stack[++stacki] = op - DW_OP_lit0;
21192 break;
21193
21194 case DW_OP_addr:
21195 stack[++stacki]
21196 = (CORE_ADDR) cu->header.read_address (objfile->obfd.get (),
21197 &data[i],
21198 &bytes_read);
21199 i += bytes_read;
21200 break;
21201
21202 case DW_OP_const1u:
21203 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
21204 i += 1;
21205 break;
21206
21207 case DW_OP_const1s:
21208 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
21209 i += 1;
21210 break;
21211
21212 case DW_OP_const2u:
21213 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
21214 i += 2;
21215 break;
21216
21217 case DW_OP_const2s:
21218 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
21219 i += 2;
21220 break;
21221
21222 case DW_OP_const4u:
21223 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
21224 i += 4;
21225 break;
21226
21227 case DW_OP_const4s:
21228 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
21229 i += 4;
21230 break;
21231
21232 case DW_OP_const8u:
21233 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
21234 i += 8;
21235 break;
21236
21237 case DW_OP_constu:
21238 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21239 &bytes_read);
21240 i += bytes_read;
21241 break;
21242
21243 case DW_OP_consts:
21244 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21245 i += bytes_read;
21246 break;
21247
21248 case DW_OP_dup:
21249 stack[stacki + 1] = stack[stacki];
21250 stacki++;
21251 break;
21252
21253 case DW_OP_plus:
21254 stack[stacki - 1] += stack[stacki];
21255 stacki--;
21256 break;
21257
21258 case DW_OP_plus_uconst:
21259 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21260 &bytes_read);
21261 i += bytes_read;
21262 break;
21263
21264 case DW_OP_minus:
21265 stack[stacki - 1] -= stack[stacki];
21266 stacki--;
21267 break;
21268
21269 case DW_OP_deref:
21270 /* If we're not the last op, then we definitely can't encode
21271 this using GDB's address_class enum. This is valid for partial
21272 global symbols, although the variable's address will be bogus
21273 in the psymtab. */
21274 if (i < size)
21275 return false;
21276 break;
21277
21278 case DW_OP_addrx:
21279 case DW_OP_GNU_addr_index:
21280 case DW_OP_GNU_const_index:
21281 stack[++stacki]
21282 = (CORE_ADDR) read_addr_index_from_leb128 (cu, &data[i],
21283 &bytes_read);
21284 i += bytes_read;
21285 break;
21286
21287 default:
21288 return false;
21289 }
21290
21291 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21292 outside of the allocated space. Also enforce minimum>0. */
21293 if (stacki >= ARRAY_SIZE (stack) - 1)
21294 {
21295 complaint (_("location description stack overflow"));
21296 return false;
21297 }
21298
21299 if (stacki <= 0)
21300 {
21301 complaint (_("location description stack underflow"));
21302 return false;
21303 }
21304 }
21305
21306 *result = stack[stacki];
21307 return true;
21308 }
21309
21310 /* memory allocation interface */
21311
21312 static struct dwarf_block *
21313 dwarf_alloc_block (struct dwarf2_cu *cu)
21314 {
21315 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21316 }
21317
21318 \f
21319
21320 /* Macro support. */
21321
21322 /* An overload of dwarf_decode_macros that finds the correct section
21323 and ensures it is read in before calling the other overload. */
21324
21325 static void
21326 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
21327 int section_is_gnu)
21328 {
21329 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21330 struct objfile *objfile = per_objfile->objfile;
21331 const struct line_header *lh = cu->line_header;
21332 unsigned int offset_size = cu->header.offset_size;
21333 struct dwarf2_section_info *section;
21334 const char *section_name;
21335
21336 if (cu->dwo_unit != nullptr)
21337 {
21338 if (section_is_gnu)
21339 {
21340 section = &cu->dwo_unit->dwo_file->sections.macro;
21341 section_name = ".debug_macro.dwo";
21342 }
21343 else
21344 {
21345 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21346 section_name = ".debug_macinfo.dwo";
21347 }
21348 }
21349 else
21350 {
21351 if (section_is_gnu)
21352 {
21353 section = &per_objfile->per_bfd->macro;
21354 section_name = ".debug_macro";
21355 }
21356 else
21357 {
21358 section = &per_objfile->per_bfd->macinfo;
21359 section_name = ".debug_macinfo";
21360 }
21361 }
21362
21363 section->read (objfile);
21364 if (section->buffer == nullptr)
21365 {
21366 complaint (_("missing %s section"), section_name);
21367 return;
21368 }
21369
21370 buildsym_compunit *builder = cu->get_builder ();
21371
21372 struct dwarf2_section_info *str_offsets_section;
21373 struct dwarf2_section_info *str_section;
21374 std::optional<ULONGEST> str_offsets_base;
21375
21376 if (cu->dwo_unit != nullptr)
21377 {
21378 str_offsets_section = &cu->dwo_unit->dwo_file
21379 ->sections.str_offsets;
21380 str_section = &cu->dwo_unit->dwo_file->sections.str;
21381 str_offsets_base = cu->header.addr_size;
21382 }
21383 else
21384 {
21385 str_offsets_section = &per_objfile->per_bfd->str_offsets;
21386 str_section = &per_objfile->per_bfd->str;
21387 str_offsets_base = cu->str_offsets_base;
21388 }
21389
21390 dwarf_decode_macros (per_objfile, builder, section, lh,
21391 offset_size, offset, str_section, str_offsets_section,
21392 str_offsets_base, section_is_gnu, cu);
21393 }
21394
21395 /* Return the .debug_loc section to use for CU.
21396 For DWO files use .debug_loc.dwo. */
21397
21398 static struct dwarf2_section_info *
21399 cu_debug_loc_section (struct dwarf2_cu *cu)
21400 {
21401 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21402
21403 if (cu->dwo_unit)
21404 {
21405 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21406
21407 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
21408 }
21409 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
21410 : &per_objfile->per_bfd->loc);
21411 }
21412
21413 /* Return the .debug_rnglists section to use for CU. */
21414 static struct dwarf2_section_info *
21415 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
21416 {
21417 if (cu->header.version < 5)
21418 error (_(".debug_rnglists section cannot be used in DWARF %d"),
21419 cu->header.version);
21420 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21421
21422 /* Make sure we read the .debug_rnglists section from the file that
21423 contains the DW_AT_ranges attribute we are reading. Normally that
21424 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
21425 or DW_TAG_skeleton unit, we always want to read from objfile/linked
21426 program. */
21427 if (cu->dwo_unit != nullptr
21428 && tag != DW_TAG_compile_unit
21429 && tag != DW_TAG_skeleton_unit)
21430 {
21431 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21432
21433 if (sections->rnglists.size > 0)
21434 return &sections->rnglists;
21435 else
21436 error (_(".debug_rnglists section is missing from .dwo file."));
21437 }
21438 return &dwarf2_per_objfile->per_bfd->rnglists;
21439 }
21440
21441 /* A helper function that fills in a dwarf2_loclist_baton. */
21442
21443 static void
21444 fill_in_loclist_baton (struct dwarf2_cu *cu,
21445 struct dwarf2_loclist_baton *baton,
21446 const struct attribute *attr)
21447 {
21448 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21449 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21450
21451 section->read (per_objfile->objfile);
21452
21453 baton->per_objfile = per_objfile;
21454 baton->per_cu = cu->per_cu;
21455 gdb_assert (baton->per_cu);
21456 /* We don't know how long the location list is, but make sure we
21457 don't run off the edge of the section. */
21458 baton->size = section->size - attr->as_unsigned ();
21459 baton->data = section->buffer + attr->as_unsigned ();
21460 if (cu->base_address.has_value ())
21461 baton->base_address = *cu->base_address;
21462 else
21463 baton->base_address = {};
21464 baton->from_dwo = cu->dwo_unit != NULL;
21465 }
21466
21467 static void
21468 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
21469 struct dwarf2_cu *cu, int is_block)
21470 {
21471 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21472 struct objfile *objfile = per_objfile->objfile;
21473 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21474
21475 if (attr->form_is_section_offset ()
21476 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21477 the section. If so, fall through to the complaint in the
21478 other branch. */
21479 && attr->as_unsigned () < section->get_size (objfile))
21480 {
21481 struct dwarf2_loclist_baton *baton;
21482
21483 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
21484
21485 fill_in_loclist_baton (cu, baton, attr);
21486
21487 if (!cu->base_address.has_value ())
21488 complaint (_("Location list used without "
21489 "specifying the CU base address."));
21490
21491 sym->set_aclass_index ((is_block
21492 ? dwarf2_loclist_block_index
21493 : dwarf2_loclist_index));
21494 SYMBOL_LOCATION_BATON (sym) = baton;
21495 }
21496 else
21497 {
21498 struct dwarf2_locexpr_baton *baton;
21499
21500 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
21501 baton->per_objfile = per_objfile;
21502 baton->per_cu = cu->per_cu;
21503 gdb_assert (baton->per_cu);
21504
21505 if (attr->form_is_block ())
21506 {
21507 /* Note that we're just copying the block's data pointer
21508 here, not the actual data. We're still pointing into the
21509 info_buffer for SYM's objfile; right now we never release
21510 that buffer, but when we do clean up properly this may
21511 need to change. */
21512 struct dwarf_block *block = attr->as_block ();
21513 baton->size = block->size;
21514 baton->data = block->data;
21515 }
21516 else
21517 {
21518 dwarf2_invalid_attrib_class_complaint ("location description",
21519 sym->natural_name ());
21520 baton->size = 0;
21521 }
21522
21523 sym->set_aclass_index ((is_block
21524 ? dwarf2_locexpr_block_index
21525 : dwarf2_locexpr_index));
21526 SYMBOL_LOCATION_BATON (sym) = baton;
21527 }
21528 }
21529
21530 /* See read.h. */
21531
21532 const comp_unit_head *
21533 dwarf2_per_cu_data::get_header () const
21534 {
21535 if (!m_header_read_in)
21536 {
21537 const gdb_byte *info_ptr
21538 = this->section->buffer + to_underlying (this->sect_off);
21539
21540 read_comp_unit_head (&m_header, info_ptr, this->section,
21541 rcuh_kind::COMPILE);
21542
21543 m_header_read_in = true;
21544 }
21545
21546 return &m_header;
21547 }
21548
21549 /* See read.h. */
21550
21551 int
21552 dwarf2_per_cu_data::addr_size () const
21553 {
21554 return this->get_header ()->addr_size;
21555 }
21556
21557 /* See read.h. */
21558
21559 int
21560 dwarf2_per_cu_data::offset_size () const
21561 {
21562 return this->get_header ()->offset_size;
21563 }
21564
21565 /* See read.h. */
21566
21567 int
21568 dwarf2_per_cu_data::ref_addr_size () const
21569 {
21570 const comp_unit_head *header = this->get_header ();
21571
21572 if (header->version == 2)
21573 return header->addr_size;
21574 else
21575 return header->offset_size;
21576 }
21577
21578 /* See read.h. */
21579
21580 void
21581 dwarf2_per_cu_data::set_lang (enum language lang,
21582 dwarf_source_language dw_lang)
21583 {
21584 if (unit_type () == DW_UT_partial)
21585 return;
21586
21587 /* Set if not set already. */
21588 packed<language, LANGUAGE_BYTES> new_value = lang;
21589 packed<language, LANGUAGE_BYTES> old_value = m_lang.exchange (new_value);
21590 /* If already set, verify that it's the same value. */
21591 gdb_assert (old_value == language_unknown || old_value == lang);
21592
21593 packed<dwarf_source_language, 2> new_dw = dw_lang;
21594 packed<dwarf_source_language, 2> old_dw = m_dw_lang.exchange (new_dw);
21595 gdb_assert (old_dw == 0 || old_dw == dw_lang);
21596 }
21597
21598 /* A helper function for dwarf2_find_containing_comp_unit that returns
21599 the index of the result, and that searches a vector. It will
21600 return a result even if the offset in question does not actually
21601 occur in any CU. This is separate so that it can be unit
21602 tested. */
21603
21604 static int
21605 dwarf2_find_containing_comp_unit
21606 (sect_offset sect_off,
21607 unsigned int offset_in_dwz,
21608 const std::vector<dwarf2_per_cu_data_up> &all_units)
21609 {
21610 int low, high;
21611
21612 low = 0;
21613 high = all_units.size () - 1;
21614 while (high > low)
21615 {
21616 struct dwarf2_per_cu_data *mid_cu;
21617 int mid = low + (high - low) / 2;
21618
21619 mid_cu = all_units[mid].get ();
21620 if (mid_cu->is_dwz > offset_in_dwz
21621 || (mid_cu->is_dwz == offset_in_dwz
21622 && mid_cu->sect_off + mid_cu->length () > sect_off))
21623 high = mid;
21624 else
21625 low = mid + 1;
21626 }
21627 gdb_assert (low == high);
21628 return low;
21629 }
21630
21631 /* Locate the .debug_info compilation unit from CU's objfile which contains
21632 the DIE at OFFSET. Raises an error on failure. */
21633
21634 static struct dwarf2_per_cu_data *
21635 dwarf2_find_containing_comp_unit (sect_offset sect_off,
21636 unsigned int offset_in_dwz,
21637 dwarf2_per_bfd *per_bfd)
21638 {
21639 int low = dwarf2_find_containing_comp_unit
21640 (sect_off, offset_in_dwz, per_bfd->all_units);
21641 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
21642
21643 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
21644 {
21645 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
21646 error (_("Dwarf Error: could not find partial DIE containing "
21647 "offset %s [in module %s]"),
21648 sect_offset_str (sect_off),
21649 bfd_get_filename (per_bfd->obfd));
21650
21651 gdb_assert (per_bfd->all_units[low-1]->sect_off
21652 <= sect_off);
21653 return per_bfd->all_units[low - 1].get ();
21654 }
21655 else
21656 {
21657 if (low == per_bfd->all_units.size () - 1
21658 && sect_off >= this_cu->sect_off + this_cu->length ())
21659 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
21660 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
21661 return this_cu;
21662 }
21663 }
21664
21665 #if GDB_SELF_TEST
21666
21667 namespace selftests {
21668 namespace find_containing_comp_unit {
21669
21670 static void
21671 run_test ()
21672 {
21673 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
21674 dwarf2_per_cu_data *one_ptr = one.get ();
21675 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
21676 dwarf2_per_cu_data *two_ptr = two.get ();
21677 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
21678 dwarf2_per_cu_data *three_ptr = three.get ();
21679 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
21680 dwarf2_per_cu_data *four_ptr = four.get ();
21681
21682 one->set_length (5);
21683 two->sect_off = sect_offset (one->length ());
21684 two->set_length (7);
21685
21686 three->set_length (5);
21687 three->is_dwz = 1;
21688 four->sect_off = sect_offset (three->length ());
21689 four->set_length (7);
21690 four->is_dwz = 1;
21691
21692 std::vector<dwarf2_per_cu_data_up> units;
21693 units.push_back (std::move (one));
21694 units.push_back (std::move (two));
21695 units.push_back (std::move (three));
21696 units.push_back (std::move (four));
21697
21698 int result;
21699
21700 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
21701 SELF_CHECK (units[result].get () == one_ptr);
21702 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
21703 SELF_CHECK (units[result].get () == one_ptr);
21704 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
21705 SELF_CHECK (units[result].get () == two_ptr);
21706
21707 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
21708 SELF_CHECK (units[result].get () == three_ptr);
21709 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
21710 SELF_CHECK (units[result].get () == three_ptr);
21711 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
21712 SELF_CHECK (units[result].get () == four_ptr);
21713 }
21714
21715 }
21716 }
21717
21718 #endif /* GDB_SELF_TEST */
21719
21720 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
21721
21722 static void
21723 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
21724 enum language pretend_language)
21725 {
21726 struct attribute *attr;
21727
21728 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
21729
21730 /* Set the language we're debugging. */
21731 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
21732 enum language lang;
21733 dwarf_source_language dw_lang = (dwarf_source_language) 0;
21734 if (cu->producer != nullptr
21735 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
21736 {
21737 /* The XLCL doesn't generate DW_LANG_OpenCL because this
21738 attribute is not standardised yet. As a workaround for the
21739 language detection we fall back to the DW_AT_producer
21740 string. */
21741 lang = language_opencl;
21742 dw_lang = DW_LANG_OpenCL;
21743 }
21744 else if (cu->producer != nullptr
21745 && strstr (cu->producer, "GNU Go ") != NULL)
21746 {
21747 /* Similar hack for Go. */
21748 lang = language_go;
21749 dw_lang = DW_LANG_Go;
21750 }
21751 else if (attr != nullptr)
21752 {
21753 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
21754 dw_lang = (dwarf_source_language) attr->constant_value (0);
21755 }
21756 else
21757 lang = pretend_language;
21758
21759 cu->language_defn = language_def (lang);
21760
21761 switch (comp_unit_die->tag)
21762 {
21763 case DW_TAG_compile_unit:
21764 cu->per_cu->set_unit_type (DW_UT_compile);
21765 break;
21766 case DW_TAG_partial_unit:
21767 cu->per_cu->set_unit_type (DW_UT_partial);
21768 break;
21769 case DW_TAG_type_unit:
21770 cu->per_cu->set_unit_type (DW_UT_type);
21771 break;
21772 default:
21773 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
21774 dwarf_tag_name (comp_unit_die->tag),
21775 sect_offset_str (cu->per_cu->sect_off));
21776 }
21777
21778 cu->per_cu->set_lang (lang, dw_lang);
21779 }
21780
21781 /* See read.h. */
21782
21783 dwarf2_cu *
21784 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
21785 {
21786 auto it = m_dwarf2_cus.find (per_cu);
21787 if (it == m_dwarf2_cus.end ())
21788 return nullptr;
21789
21790 return it->second.get ();
21791 }
21792
21793 /* See read.h. */
21794
21795 void
21796 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
21797 std::unique_ptr<dwarf2_cu> cu)
21798 {
21799 gdb_assert (this->get_cu (per_cu) == nullptr);
21800
21801 m_dwarf2_cus[per_cu] = std::move (cu);
21802 }
21803
21804 /* See read.h. */
21805
21806 void
21807 dwarf2_per_objfile::age_comp_units ()
21808 {
21809 dwarf_read_debug_printf_v ("running");
21810
21811 /* This is not expected to be called in the middle of CU expansion. There is
21812 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
21813 loaded in memory. Calling age_comp_units while the queue is in use could
21814 make us free the DIEs for a CU that is in the queue and therefore break
21815 that invariant. */
21816 gdb_assert (!queue.has_value ());
21817
21818 /* Start by clearing all marks. */
21819 for (const auto &pair : m_dwarf2_cus)
21820 pair.second->clear_mark ();
21821
21822 /* Traverse all CUs, mark them and their dependencies if used recently
21823 enough. */
21824 for (const auto &pair : m_dwarf2_cus)
21825 {
21826 dwarf2_cu *cu = pair.second.get ();
21827
21828 cu->last_used++;
21829 if (cu->last_used <= dwarf_max_cache_age)
21830 cu->mark ();
21831 }
21832
21833 /* Delete all CUs still not marked. */
21834 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
21835 {
21836 dwarf2_cu *cu = it->second.get ();
21837
21838 if (!cu->is_marked ())
21839 {
21840 dwarf_read_debug_printf_v ("deleting old CU %s",
21841 sect_offset_str (cu->per_cu->sect_off));
21842 it = m_dwarf2_cus.erase (it);
21843 }
21844 else
21845 it++;
21846 }
21847 }
21848
21849 /* See read.h. */
21850
21851 void
21852 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
21853 {
21854 auto it = m_dwarf2_cus.find (per_cu);
21855 if (it == m_dwarf2_cus.end ())
21856 return;
21857
21858 m_dwarf2_cus.erase (it);
21859 }
21860
21861 dwarf2_per_objfile::~dwarf2_per_objfile ()
21862 {
21863 remove_all_cus ();
21864 }
21865
21866 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21867 We store these in a hash table separate from the DIEs, and preserve them
21868 when the DIEs are flushed out of cache.
21869
21870 The CU "per_cu" pointer is needed because offset alone is not enough to
21871 uniquely identify the type. A file may have multiple .debug_types sections,
21872 or the type may come from a DWO file. Furthermore, while it's more logical
21873 to use per_cu->section+offset, with Fission the section with the data is in
21874 the DWO file but we don't know that section at the point we need it.
21875 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21876 because we can enter the lookup routine, get_die_type_at_offset, from
21877 outside this file, and thus won't necessarily have PER_CU->cu.
21878 Fortunately, PER_CU is stable for the life of the objfile. */
21879
21880 struct dwarf2_per_cu_offset_and_type
21881 {
21882 const struct dwarf2_per_cu_data *per_cu;
21883 sect_offset sect_off;
21884 struct type *type;
21885 };
21886
21887 /* Hash function for a dwarf2_per_cu_offset_and_type. */
21888
21889 static hashval_t
21890 per_cu_offset_and_type_hash (const void *item)
21891 {
21892 const struct dwarf2_per_cu_offset_and_type *ofs
21893 = (const struct dwarf2_per_cu_offset_and_type *) item;
21894
21895 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
21896 }
21897
21898 /* Equality function for a dwarf2_per_cu_offset_and_type. */
21899
21900 static int
21901 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
21902 {
21903 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
21904 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
21905 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
21906 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
21907
21908 return (ofs_lhs->per_cu == ofs_rhs->per_cu
21909 && ofs_lhs->sect_off == ofs_rhs->sect_off);
21910 }
21911
21912 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21913 table if necessary. For convenience, return TYPE.
21914
21915 The DIEs reading must have careful ordering to:
21916 * Not cause infinite loops trying to read in DIEs as a prerequisite for
21917 reading current DIE.
21918 * Not trying to dereference contents of still incompletely read in types
21919 while reading in other DIEs.
21920 * Enable referencing still incompletely read in types just by a pointer to
21921 the type without accessing its fields.
21922
21923 Therefore caller should follow these rules:
21924 * Try to fetch any prerequisite types we may need to build this DIE type
21925 before building the type and calling set_die_type.
21926 * After building type call set_die_type for current DIE as soon as
21927 possible before fetching more types to complete the current type.
21928 * Make the type as complete as possible before fetching more types. */
21929
21930 static struct type *
21931 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21932 bool skip_data_location)
21933 {
21934 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21935 struct dwarf2_per_cu_offset_and_type **slot, ofs;
21936 struct objfile *objfile = per_objfile->objfile;
21937 struct attribute *attr;
21938 struct dynamic_prop prop;
21939
21940 /* For Ada types, make sure that the gnat-specific data is always
21941 initialized (if not already set). There are a few types where
21942 we should not be doing so, because the type-specific area is
21943 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21944 where the type-specific area is used to store the floatformat).
21945 But this is not a problem, because the gnat-specific information
21946 is actually not needed for these types. */
21947 if (need_gnat_info (cu)
21948 && type->code () != TYPE_CODE_FUNC
21949 && type->code () != TYPE_CODE_FLT
21950 && type->code () != TYPE_CODE_METHODPTR
21951 && type->code () != TYPE_CODE_MEMBERPTR
21952 && type->code () != TYPE_CODE_METHOD
21953 && type->code () != TYPE_CODE_FIXED_POINT
21954 && !HAVE_GNAT_AUX_INFO (type))
21955 INIT_GNAT_SPECIFIC (type);
21956
21957 /* Read DW_AT_allocated and set in type. */
21958 attr = dwarf2_attr (die, DW_AT_allocated, cu);
21959 if (attr != NULL)
21960 {
21961 struct type *prop_type = cu->addr_sized_int_type (false);
21962 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21963 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
21964 }
21965
21966 /* Read DW_AT_associated and set in type. */
21967 attr = dwarf2_attr (die, DW_AT_associated, cu);
21968 if (attr != NULL)
21969 {
21970 struct type *prop_type = cu->addr_sized_int_type (false);
21971 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21972 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
21973 }
21974
21975 /* Read DW_AT_rank and set in type. */
21976 attr = dwarf2_attr (die, DW_AT_rank, cu);
21977 if (attr != NULL)
21978 {
21979 struct type *prop_type = cu->addr_sized_int_type (false);
21980 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21981 type->add_dyn_prop (DYN_PROP_RANK, prop);
21982 }
21983
21984 /* Read DW_AT_data_location and set in type. */
21985 if (!skip_data_location)
21986 {
21987 attr = dwarf2_attr (die, DW_AT_data_location, cu);
21988 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
21989 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
21990 }
21991
21992 if (per_objfile->die_type_hash == NULL)
21993 per_objfile->die_type_hash
21994 = htab_up (htab_create_alloc (127,
21995 per_cu_offset_and_type_hash,
21996 per_cu_offset_and_type_eq,
21997 NULL, xcalloc, xfree));
21998
21999 ofs.per_cu = cu->per_cu;
22000 ofs.sect_off = die->sect_off;
22001 ofs.type = type;
22002 slot = (struct dwarf2_per_cu_offset_and_type **)
22003 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
22004 if (*slot)
22005 complaint (_("A problem internal to GDB: DIE %s has type already set"),
22006 sect_offset_str (die->sect_off));
22007 *slot = XOBNEW (&objfile->objfile_obstack,
22008 struct dwarf2_per_cu_offset_and_type);
22009 **slot = ofs;
22010 return type;
22011 }
22012
22013 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
22014 or return NULL if the die does not have a saved type. */
22015
22016 static struct type *
22017 get_die_type_at_offset (sect_offset sect_off,
22018 dwarf2_per_cu_data *per_cu,
22019 dwarf2_per_objfile *per_objfile)
22020 {
22021 struct dwarf2_per_cu_offset_and_type *slot, ofs;
22022
22023 if (per_objfile->die_type_hash == NULL)
22024 return NULL;
22025
22026 ofs.per_cu = per_cu;
22027 ofs.sect_off = sect_off;
22028 slot = ((struct dwarf2_per_cu_offset_and_type *)
22029 htab_find (per_objfile->die_type_hash.get (), &ofs));
22030 if (slot)
22031 return slot->type;
22032 else
22033 return NULL;
22034 }
22035
22036 /* Look up the type for DIE in CU in die_type_hash,
22037 or return NULL if DIE does not have a saved type. */
22038
22039 static struct type *
22040 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22041 {
22042 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
22043 }
22044
22045 struct cmd_list_element *set_dwarf_cmdlist;
22046 struct cmd_list_element *show_dwarf_cmdlist;
22047
22048 static void
22049 show_check_physname (struct ui_file *file, int from_tty,
22050 struct cmd_list_element *c, const char *value)
22051 {
22052 gdb_printf (file,
22053 _("Whether to check \"physname\" is %s.\n"),
22054 value);
22055 }
22056
22057 void _initialize_dwarf2_read ();
22058 void
22059 _initialize_dwarf2_read ()
22060 {
22061 add_setshow_prefix_cmd ("dwarf", class_maintenance,
22062 _("\
22063 Set DWARF specific variables.\n\
22064 Configure DWARF variables such as the cache size."),
22065 _("\
22066 Show DWARF specific variables.\n\
22067 Show DWARF variables such as the cache size."),
22068 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
22069 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
22070
22071 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
22072 &dwarf_max_cache_age, _("\
22073 Set the upper bound on the age of cached DWARF compilation units."), _("\
22074 Show the upper bound on the age of cached DWARF compilation units."), _("\
22075 A higher limit means that cached compilation units will be stored\n\
22076 in memory longer, and more total memory will be used. Zero disables\n\
22077 caching, which can slow down startup."),
22078 NULL,
22079 show_dwarf_max_cache_age,
22080 &set_dwarf_cmdlist,
22081 &show_dwarf_cmdlist);
22082
22083 add_setshow_boolean_cmd ("synchronous", class_obscure,
22084 &dwarf_synchronous, _("\
22085 Set whether DWARF is read synchronously."), _("\
22086 Show whether DWARF is read synchronously."), _("\
22087 By default, DWARF information is read in worker threads,\n\
22088 and gdb will not generally wait for the reading to complete\n\
22089 before continuing with other work, for example presenting a\n\
22090 prompt to the user.\n\
22091 Enabling this setting will cause the DWARF reader to always wait\n\
22092 for debug info processing to be finished before gdb can proceed."),
22093 nullptr,
22094 show_dwarf_synchronous,
22095 &set_dwarf_cmdlist,
22096 &show_dwarf_cmdlist);
22097
22098 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
22099 Set debugging of the DWARF reader."), _("\
22100 Show debugging of the DWARF reader."), _("\
22101 When enabled (non-zero), debugging messages are printed during DWARF\n\
22102 reading and symtab expansion. A value of 1 (one) provides basic\n\
22103 information. A value greater than 1 provides more verbose information."),
22104 NULL,
22105 NULL,
22106 &setdebuglist, &showdebuglist);
22107
22108 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
22109 Set debugging of the DWARF DIE reader."), _("\
22110 Show debugging of the DWARF DIE reader."), _("\
22111 When enabled (non-zero), DIEs are dumped after they are read in.\n\
22112 The value is the maximum depth to print."),
22113 NULL,
22114 NULL,
22115 &setdebuglist, &showdebuglist);
22116
22117 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
22118 Set debugging of the dwarf line reader."), _("\
22119 Show debugging of the dwarf line reader."), _("\
22120 When enabled (non-zero), line number entries are dumped as they are read in.\n\
22121 A value of 1 (one) provides basic information.\n\
22122 A value greater than 1 provides more verbose information."),
22123 NULL,
22124 NULL,
22125 &setdebuglist, &showdebuglist);
22126
22127 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
22128 Set cross-checking of \"physname\" code against demangler."), _("\
22129 Show cross-checking of \"physname\" code against demangler."), _("\
22130 When enabled, GDB's internal \"physname\" code is checked against\n\
22131 the demangler."),
22132 NULL, show_check_physname,
22133 &setdebuglist, &showdebuglist);
22134
22135 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
22136 &dwarf2_locexpr_funcs);
22137 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
22138 &dwarf2_loclist_funcs);
22139 ada_imported_index = register_symbol_computed_impl (LOC_COMPUTED,
22140 &ada_imported_funcs);
22141
22142 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
22143 &dwarf2_block_frame_base_locexpr_funcs);
22144 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
22145 &dwarf2_block_frame_base_loclist_funcs);
22146 ada_block_index = register_symbol_block_impl (LOC_BLOCK,
22147 &ada_function_alias_funcs);
22148
22149 #if GDB_SELF_TEST
22150 selftests::register_test ("dw2_expand_symtabs_matching",
22151 selftests::dw2_expand_symtabs_matching::run_test);
22152 selftests::register_test ("dwarf2_find_containing_comp_unit",
22153 selftests::find_containing_comp_unit::run_test);
22154 #endif
22155 }