]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
049ee4d52ff203198da29fb0029da8c681d8b248
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2024 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
30 #include "dwarf2/read.h"
31 #include "dwarf2/abbrev.h"
32 #include "dwarf2/aranges.h"
33 #include "dwarf2/attribute.h"
34 #include "dwarf2/comp-unit-head.h"
35 #include "dwarf2/cu.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/read-debug-names.h"
44 #include "dwarf2/read-gdb-index.h"
45 #include "dwarf2/sect-names.h"
46 #include "dwarf2/stringify.h"
47 #include "dwarf2/public.h"
48 #include "bfd.h"
49 #include "elf-bfd.h"
50 #include "event-top.h"
51 #include "symtab.h"
52 #include "gdbtypes.h"
53 #include "objfiles.h"
54 #include "dwarf2.h"
55 #include "demangle.h"
56 #include "gdb-demangle.h"
57 #include "filenames.h"
58 #include "language.h"
59 #include "complaints.h"
60 #include "dwarf2/expr.h"
61 #include "dwarf2/loc.h"
62 #include "cp-support.h"
63 #include "hashtab.h"
64 #include "command.h"
65 #include "cli/cli-cmds.h"
66 #include "block.h"
67 #include "addrmap.h"
68 #include "typeprint.h"
69 #include "c-lang.h"
70 #include "go-lang.h"
71 #include "valprint.h"
72 #include "gdbcore.h"
73 #include "gdb/gdb-index.h"
74 #include "gdb_bfd.h"
75 #include "f-lang.h"
76 #include "source.h"
77 #include "build-id.h"
78 #include "namespace.h"
79 #include "gdbsupport/function-view.h"
80 #include <optional>
81 #include "gdbsupport/underlying.h"
82 #include "gdbsupport/hash_enum.h"
83 #include "filename-seen-cache.h"
84 #include "producer.h"
85 #include <fcntl.h>
86 #include <algorithm>
87 #include <unordered_map>
88 #include "gdbsupport/selftest.h"
89 #include "rust-lang.h"
90 #include "gdbsupport/pathstuff.h"
91 #include "count-one-bits.h"
92 #include <unordered_set>
93 #include "dwarf2/abbrev-cache.h"
94 #include "cooked-index.h"
95 #include "split-name.h"
96 #include "gdbsupport/thread-pool.h"
97 #include "run-on-main-thread.h"
98 #include "dwarf2/parent-map.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 static bool producer_is_gas_ge_2_39 (struct dwarf2_cu *cu);
153
154 /* Size of .debug_loclists section header for 32-bit DWARF format. */
155 #define LOCLIST_HEADER_SIZE32 12
156
157 /* Size of .debug_loclists section header for 64-bit DWARF format. */
158 #define LOCLIST_HEADER_SIZE64 20
159
160 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
161 #define RNGLIST_HEADER_SIZE32 12
162
163 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
164 #define RNGLIST_HEADER_SIZE64 20
165
166 /* See dwarf2/read.h. */
167
168 dwarf2_per_objfile *
169 get_dwarf2_per_objfile (struct objfile *objfile)
170 {
171 return dwarf2_objfile_data_key.get (objfile);
172 }
173
174 /* Default names of the debugging sections. */
175
176 /* Note that if the debugging section has been compressed, it might
177 have a name like .zdebug_info. */
178
179 const struct dwarf2_debug_sections dwarf2_elf_names =
180 {
181 { ".debug_info", ".zdebug_info" },
182 { ".debug_abbrev", ".zdebug_abbrev" },
183 { ".debug_line", ".zdebug_line" },
184 { ".debug_loc", ".zdebug_loc" },
185 { ".debug_loclists", ".zdebug_loclists" },
186 { ".debug_macinfo", ".zdebug_macinfo" },
187 { ".debug_macro", ".zdebug_macro" },
188 { ".debug_str", ".zdebug_str" },
189 { ".debug_str_offsets", ".zdebug_str_offsets" },
190 { ".debug_line_str", ".zdebug_line_str" },
191 { ".debug_ranges", ".zdebug_ranges" },
192 { ".debug_rnglists", ".zdebug_rnglists" },
193 { ".debug_types", ".zdebug_types" },
194 { ".debug_addr", ".zdebug_addr" },
195 { ".debug_frame", ".zdebug_frame" },
196 { ".eh_frame", NULL },
197 { ".gdb_index", ".zgdb_index" },
198 { ".debug_names", ".zdebug_names" },
199 { ".debug_aranges", ".zdebug_aranges" },
200 23
201 };
202
203 /* List of DWO/DWP sections. */
204
205 static const struct dwop_section_names
206 {
207 struct dwarf2_section_names abbrev_dwo;
208 struct dwarf2_section_names info_dwo;
209 struct dwarf2_section_names line_dwo;
210 struct dwarf2_section_names loc_dwo;
211 struct dwarf2_section_names loclists_dwo;
212 struct dwarf2_section_names macinfo_dwo;
213 struct dwarf2_section_names macro_dwo;
214 struct dwarf2_section_names rnglists_dwo;
215 struct dwarf2_section_names str_dwo;
216 struct dwarf2_section_names str_offsets_dwo;
217 struct dwarf2_section_names types_dwo;
218 struct dwarf2_section_names cu_index;
219 struct dwarf2_section_names tu_index;
220 }
221 dwop_section_names =
222 {
223 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
224 { ".debug_info.dwo", ".zdebug_info.dwo" },
225 { ".debug_line.dwo", ".zdebug_line.dwo" },
226 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
227 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
228 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
229 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
230 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
231 { ".debug_str.dwo", ".zdebug_str.dwo" },
232 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
233 { ".debug_types.dwo", ".zdebug_types.dwo" },
234 { ".debug_cu_index", ".zdebug_cu_index" },
235 { ".debug_tu_index", ".zdebug_tu_index" },
236 };
237
238 /* local data types */
239
240 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
241 begin with a header, which contains the following information. */
242 struct loclists_rnglists_header
243 {
244 /* A 4-byte or 12-byte length containing the length of the
245 set of entries for this compilation unit, not including the
246 length field itself. */
247 unsigned int length;
248
249 /* A 2-byte version identifier. */
250 short version;
251
252 /* A 1-byte unsigned integer containing the size in bytes of an address on
253 the target system. */
254 unsigned char addr_size;
255
256 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
257 on the target system. */
258 unsigned char segment_collector_size;
259
260 /* A 4-byte count of the number of offsets that follow the header. */
261 unsigned int offset_entry_count;
262 };
263
264 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
265 This includes type_unit_group and quick_file_names. */
266
267 struct stmt_list_hash
268 {
269 /* The DWO unit this table is from or NULL if there is none. */
270 struct dwo_unit *dwo_unit;
271
272 /* Offset in .debug_line or .debug_line.dwo. */
273 sect_offset line_sect_off;
274 };
275
276 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
277 an object of this type. This contains elements of type unit groups
278 that can be shared across objfiles. The non-shareable parts are in
279 type_unit_group_unshareable. */
280
281 struct type_unit_group
282 {
283 /* The data used to construct the hash key. */
284 struct stmt_list_hash hash {};
285 };
286
287 /* These sections are what may appear in a (real or virtual) DWO file. */
288
289 struct dwo_sections
290 {
291 struct dwarf2_section_info abbrev;
292 struct dwarf2_section_info line;
293 struct dwarf2_section_info loc;
294 struct dwarf2_section_info loclists;
295 struct dwarf2_section_info macinfo;
296 struct dwarf2_section_info macro;
297 struct dwarf2_section_info rnglists;
298 struct dwarf2_section_info str;
299 struct dwarf2_section_info str_offsets;
300 /* In the case of a virtual DWO file, these two are unused. */
301 struct dwarf2_section_info info;
302 std::vector<dwarf2_section_info> types;
303 };
304
305 /* CUs/TUs in DWP/DWO files. */
306
307 struct dwo_unit
308 {
309 /* Backlink to the containing struct dwo_file. */
310 struct dwo_file *dwo_file;
311
312 /* The "id" that distinguishes this CU/TU.
313 .debug_info calls this "dwo_id", .debug_types calls this "signature".
314 Since signatures came first, we stick with it for consistency. */
315 ULONGEST signature;
316
317 /* The section this CU/TU lives in, in the DWO file. */
318 struct dwarf2_section_info *section;
319
320 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
321 sect_offset sect_off;
322 unsigned int length;
323
324 /* For types, offset in the type's DIE of the type defined by this TU. */
325 cu_offset type_offset_in_tu;
326 };
327
328 /* include/dwarf2.h defines the DWP section codes.
329 It defines a max value but it doesn't define a min value, which we
330 use for error checking, so provide one. */
331
332 enum dwp_v2_section_ids
333 {
334 DW_SECT_MIN = 1
335 };
336
337 /* Data for one DWO file.
338
339 This includes virtual DWO files (a virtual DWO file is a DWO file as it
340 appears in a DWP file). DWP files don't really have DWO files per se -
341 comdat folding of types "loses" the DWO file they came from, and from
342 a high level view DWP files appear to contain a mass of random types.
343 However, to maintain consistency with the non-DWP case we pretend DWP
344 files contain virtual DWO files, and we assign each TU with one virtual
345 DWO file (generally based on the line and abbrev section offsets -
346 a heuristic that seems to work in practice). */
347
348 struct dwo_file
349 {
350 dwo_file () = default;
351 DISABLE_COPY_AND_ASSIGN (dwo_file);
352
353 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
354 For virtual DWO files the name is constructed from the section offsets
355 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
356 from related CU+TUs. */
357 std::string dwo_name;
358
359 /* The DW_AT_comp_dir attribute. */
360 const char *comp_dir = nullptr;
361
362 /* The bfd, when the file is open. Otherwise this is NULL.
363 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
364 gdb_bfd_ref_ptr dbfd;
365
366 /* The sections that make up this DWO file.
367 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
368 sections (for lack of a better name). */
369 struct dwo_sections sections {};
370
371 /* The CUs in the file.
372 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
373 an extension to handle LLVM's Link Time Optimization output (where
374 multiple source files may be compiled into a single object/dwo pair). */
375 htab_up cus;
376
377 /* Table of TUs in the file.
378 Each element is a struct dwo_unit. */
379 htab_up tus;
380 };
381
382 /* These sections are what may appear in a DWP file. */
383
384 struct dwp_sections
385 {
386 /* These are used by all DWP versions (1, 2 and 5). */
387 struct dwarf2_section_info str;
388 struct dwarf2_section_info cu_index;
389 struct dwarf2_section_info tu_index;
390
391 /* These are only used by DWP version 2 and version 5 files.
392 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
393 sections are referenced by section number, and are not recorded here.
394 In DWP version 2 or 5 there is at most one copy of all these sections,
395 each section being (effectively) comprised of the concatenation of all of
396 the individual sections that exist in the version 1 format.
397 To keep the code simple we treat each of these concatenated pieces as a
398 section itself (a virtual section?). */
399 struct dwarf2_section_info abbrev;
400 struct dwarf2_section_info info;
401 struct dwarf2_section_info line;
402 struct dwarf2_section_info loc;
403 struct dwarf2_section_info loclists;
404 struct dwarf2_section_info macinfo;
405 struct dwarf2_section_info macro;
406 struct dwarf2_section_info rnglists;
407 struct dwarf2_section_info str_offsets;
408 struct dwarf2_section_info types;
409 };
410
411 /* These sections are what may appear in a virtual DWO file in DWP version 1.
412 A virtual DWO file is a DWO file as it appears in a DWP file. */
413
414 struct virtual_v1_dwo_sections
415 {
416 struct dwarf2_section_info abbrev;
417 struct dwarf2_section_info line;
418 struct dwarf2_section_info loc;
419 struct dwarf2_section_info macinfo;
420 struct dwarf2_section_info macro;
421 struct dwarf2_section_info str_offsets;
422 /* Each DWP hash table entry records one CU or one TU.
423 That is recorded here, and copied to dwo_unit.section. */
424 struct dwarf2_section_info info_or_types;
425 };
426
427 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
428 In version 2, the sections of the DWO files are concatenated together
429 and stored in one section of that name. Thus each ELF section contains
430 several "virtual" sections. */
431
432 struct virtual_v2_or_v5_dwo_sections
433 {
434 bfd_size_type abbrev_offset;
435 bfd_size_type abbrev_size;
436
437 bfd_size_type line_offset;
438 bfd_size_type line_size;
439
440 bfd_size_type loc_offset;
441 bfd_size_type loc_size;
442
443 bfd_size_type loclists_offset;
444 bfd_size_type loclists_size;
445
446 bfd_size_type macinfo_offset;
447 bfd_size_type macinfo_size;
448
449 bfd_size_type macro_offset;
450 bfd_size_type macro_size;
451
452 bfd_size_type rnglists_offset;
453 bfd_size_type rnglists_size;
454
455 bfd_size_type str_offsets_offset;
456 bfd_size_type str_offsets_size;
457
458 /* Each DWP hash table entry records one CU or one TU.
459 That is recorded here, and copied to dwo_unit.section. */
460 bfd_size_type info_or_types_offset;
461 bfd_size_type info_or_types_size;
462 };
463
464 /* Contents of DWP hash tables. */
465
466 struct dwp_hash_table
467 {
468 uint32_t version, nr_columns;
469 uint32_t nr_units, nr_slots;
470 const gdb_byte *hash_table, *unit_table;
471 union
472 {
473 struct
474 {
475 const gdb_byte *indices;
476 } v1;
477 struct
478 {
479 /* This is indexed by column number and gives the id of the section
480 in that column. */
481 #define MAX_NR_V2_DWO_SECTIONS \
482 (1 /* .debug_info or .debug_types */ \
483 + 1 /* .debug_abbrev */ \
484 + 1 /* .debug_line */ \
485 + 1 /* .debug_loc */ \
486 + 1 /* .debug_str_offsets */ \
487 + 1 /* .debug_macro or .debug_macinfo */)
488 int section_ids[MAX_NR_V2_DWO_SECTIONS];
489 const gdb_byte *offsets;
490 const gdb_byte *sizes;
491 } v2;
492 struct
493 {
494 /* This is indexed by column number and gives the id of the section
495 in that column. */
496 #define MAX_NR_V5_DWO_SECTIONS \
497 (1 /* .debug_info */ \
498 + 1 /* .debug_abbrev */ \
499 + 1 /* .debug_line */ \
500 + 1 /* .debug_loclists */ \
501 + 1 /* .debug_str_offsets */ \
502 + 1 /* .debug_macro */ \
503 + 1 /* .debug_rnglists */)
504 int section_ids[MAX_NR_V5_DWO_SECTIONS];
505 const gdb_byte *offsets;
506 const gdb_byte *sizes;
507 } v5;
508 } section_pool;
509 };
510
511 /* Data for one DWP file. */
512
513 struct dwp_file
514 {
515 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
516 : name (name_),
517 dbfd (std::move (abfd))
518 {
519 }
520
521 /* Name of the file. */
522 const char *name;
523
524 /* File format version. */
525 int version = 0;
526
527 /* The bfd. */
528 gdb_bfd_ref_ptr dbfd;
529
530 /* Section info for this file. */
531 struct dwp_sections sections {};
532
533 /* Table of CUs in the file. */
534 const struct dwp_hash_table *cus = nullptr;
535
536 /* Table of TUs in the file. */
537 const struct dwp_hash_table *tus = nullptr;
538
539 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
540 htab_up loaded_cus;
541 htab_up loaded_tus;
542
543 /* Table to map ELF section numbers to their sections.
544 This is only needed for the DWP V1 file format. */
545 unsigned int num_sections = 0;
546 asection **elf_sections = nullptr;
547 };
548
549 /* Struct used to pass misc. parameters to read_die_and_children, et
550 al. which are used for both .debug_info and .debug_types dies.
551 All parameters here are unchanging for the life of the call. This
552 struct exists to abstract away the constant parameters of die reading. */
553
554 struct die_reader_specs
555 {
556 /* The bfd of die_section. */
557 bfd *abfd;
558
559 /* The CU of the DIE we are parsing. */
560 struct dwarf2_cu *cu;
561
562 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
563 struct dwo_file *dwo_file;
564
565 /* The section the die comes from.
566 This is either .debug_info or .debug_types, or the .dwo variants. */
567 struct dwarf2_section_info *die_section;
568
569 /* die_section->buffer. */
570 const gdb_byte *buffer;
571
572 /* The end of the buffer. */
573 const gdb_byte *buffer_end;
574
575 /* The abbreviation table to use when reading the DIEs. */
576 struct abbrev_table *abbrev_table;
577 };
578
579 /* A subclass of die_reader_specs that holds storage and has complex
580 constructor and destructor behavior. */
581
582 class cutu_reader : public die_reader_specs
583 {
584 public:
585
586 cutu_reader (dwarf2_per_cu_data *this_cu,
587 dwarf2_per_objfile *per_objfile,
588 struct abbrev_table *abbrev_table,
589 dwarf2_cu *existing_cu,
590 bool skip_partial,
591 abbrev_cache *cache = nullptr);
592
593 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
594 dwarf2_per_objfile *per_objfile,
595 struct dwarf2_cu *parent_cu = nullptr,
596 struct dwo_file *dwo_file = nullptr);
597
598 DISABLE_COPY_AND_ASSIGN (cutu_reader);
599
600 cutu_reader (cutu_reader &&) = default;
601
602 const gdb_byte *info_ptr = nullptr;
603 struct die_info *comp_unit_die = nullptr;
604 bool dummy_p = false;
605
606 /* Release the new CU, putting it on the chain. This cannot be done
607 for dummy CUs. */
608 void keep ();
609
610 /* Release the abbrev table, transferring ownership to the
611 caller. */
612 abbrev_table_up release_abbrev_table ()
613 {
614 return std::move (m_abbrev_table_holder);
615 }
616
617 private:
618 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
619 dwarf2_per_objfile *per_objfile,
620 dwarf2_cu *existing_cu);
621
622 struct dwarf2_per_cu_data *m_this_cu;
623 std::unique_ptr<dwarf2_cu> m_new_cu;
624
625 /* The ordinary abbreviation table. */
626 abbrev_table_up m_abbrev_table_holder;
627
628 /* The DWO abbreviation table. */
629 abbrev_table_up m_dwo_abbrev_table;
630 };
631
632 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
633 but this would require a corresponding change in unpack_field_as_long
634 and friends. */
635 static int bits_per_byte = 8;
636
637 struct variant_part_builder;
638
639 /* When reading a variant, we track a bit more information about the
640 field, and store it in an object of this type. */
641
642 struct variant_field
643 {
644 int first_field = -1;
645 int last_field = -1;
646
647 /* A variant can contain other variant parts. */
648 std::vector<variant_part_builder> variant_parts;
649
650 /* If we see a DW_TAG_variant, then this will be set if this is the
651 default branch. */
652 bool default_branch = false;
653 /* If we see a DW_AT_discr_value, then this will be the discriminant
654 value. */
655 ULONGEST discriminant_value = 0;
656 /* If we see a DW_AT_discr_list, then this is a pointer to the list
657 data. */
658 struct dwarf_block *discr_list_data = nullptr;
659 };
660
661 /* This represents a DW_TAG_variant_part. */
662
663 struct variant_part_builder
664 {
665 /* The offset of the discriminant field. */
666 sect_offset discriminant_offset {};
667
668 /* Variants that are direct children of this variant part. */
669 std::vector<variant_field> variants;
670
671 /* True if we're currently reading a variant. */
672 bool processing_variant = false;
673 };
674
675 struct nextfield
676 {
677 /* Variant parts need to find the discriminant, which is a DIE
678 reference. We track the section offset of each field to make
679 this link. */
680 sect_offset offset;
681 struct field field {};
682 };
683
684 struct fnfieldlist
685 {
686 const char *name = nullptr;
687 std::vector<struct fn_field> fnfields;
688 };
689
690 /* The routines that read and process dies for a C struct or C++ class
691 pass lists of data member fields and lists of member function fields
692 in an instance of a field_info structure, as defined below. */
693 struct field_info
694 {
695 /* List of data member and baseclasses fields. */
696 std::vector<struct nextfield> fields;
697 std::vector<struct nextfield> baseclasses;
698
699 /* Member function fieldlist array, contains name of possibly overloaded
700 member function, number of overloaded member functions and a pointer
701 to the head of the member function field chain. */
702 std::vector<struct fnfieldlist> fnfieldlists;
703
704 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
705 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
706 std::vector<struct decl_field> typedef_field_list;
707
708 /* Nested types defined by this class and the number of elements in this
709 list. */
710 std::vector<struct decl_field> nested_types_list;
711
712 /* If non-null, this is the variant part we are currently
713 reading. */
714 variant_part_builder *current_variant_part = nullptr;
715 /* This holds all the top-level variant parts attached to the type
716 we're reading. */
717 std::vector<variant_part_builder> variant_parts;
718
719 /* Return the total number of fields (including baseclasses). */
720 int nfields () const
721 {
722 return fields.size () + baseclasses.size ();
723 }
724 };
725
726 /* Loaded secondary compilation units are kept in memory until they
727 have not been referenced for the processing of this many
728 compilation units. Set this to zero to disable caching. Cache
729 sizes of up to at least twenty will improve startup time for
730 typical inter-CU-reference binaries, at an obvious memory cost. */
731 static int dwarf_max_cache_age = 5;
732 static void
733 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
734 struct cmd_list_element *c, const char *value)
735 {
736 gdb_printf (file, _("The upper bound on the age of cached "
737 "DWARF compilation units is %s.\n"),
738 value);
739 }
740
741 /* When true, wait for DWARF reading to be complete. */
742 static bool dwarf_synchronous = false;
743
744 /* "Show" callback for "maint set dwarf synchronous". */
745 static void
746 show_dwarf_synchronous (struct ui_file *file, int from_tty,
747 struct cmd_list_element *c, const char *value)
748 {
749 gdb_printf (file, _("Whether DWARF reading is synchronous is %s.\n"),
750 value);
751 }
752 \f
753 /* local function prototypes */
754
755 static void dwarf2_find_base_address (struct die_info *die,
756 struct dwarf2_cu *cu);
757
758 static void build_type_psymtabs_reader (cutu_reader *reader,
759 cooked_index_storage *storage);
760
761 static void var_decode_location (struct attribute *attr,
762 struct symbol *sym,
763 struct dwarf2_cu *cu);
764
765 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
766
767 static const gdb_byte *read_attribute (const struct die_reader_specs *,
768 struct attribute *,
769 const struct attr_abbrev *,
770 const gdb_byte *,
771 bool allow_reprocess = true);
772
773 /* Note that the default for TAG is chosen because it only matters
774 when reading the top-level DIE, and that function is careful to
775 pass the correct tag. */
776 static void read_attribute_reprocess (const struct die_reader_specs *reader,
777 struct attribute *attr,
778 dwarf_tag tag = DW_TAG_padding);
779
780 static unrelocated_addr read_addr_index (struct dwarf2_cu *cu,
781 unsigned int addr_index);
782
783 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
784 dwarf2_section_info *, sect_offset);
785
786 static const char *read_indirect_string
787 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
788 const struct comp_unit_head *, unsigned int *);
789
790 static unrelocated_addr read_addr_index_from_leb128 (struct dwarf2_cu *,
791 const gdb_byte *,
792 unsigned int *);
793
794 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
795 ULONGEST str_index);
796
797 static const char *read_stub_str_index (struct dwarf2_cu *cu,
798 ULONGEST str_index);
799
800 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
801 struct dwarf2_cu *);
802
803 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
804 struct dwarf2_cu *cu);
805
806 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
807
808 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
809 struct dwarf2_cu *cu);
810
811 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
812
813 static struct die_info *die_specification (struct die_info *die,
814 struct dwarf2_cu **);
815
816 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
817 struct dwarf2_cu *cu,
818 const char *comp_dir);
819
820 static void dwarf_decode_lines (struct line_header *,
821 struct dwarf2_cu *,
822 unrelocated_addr, int decode_mapping);
823
824 static void dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
825 const line_header &lh);
826
827 static struct symbol *new_symbol (struct die_info *, struct type *,
828 struct dwarf2_cu *, struct symbol * = NULL);
829
830 static void dwarf2_const_value (const struct attribute *, struct symbol *,
831 struct dwarf2_cu *);
832
833 static void dwarf2_const_value_attr (const struct attribute *attr,
834 struct type *type,
835 const char *name,
836 struct obstack *obstack,
837 struct dwarf2_cu *cu, LONGEST *value,
838 const gdb_byte **bytes,
839 struct dwarf2_locexpr_baton **baton);
840
841 static struct type *read_subrange_index_type (struct die_info *die,
842 struct dwarf2_cu *cu);
843
844 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
845
846 static int need_gnat_info (struct dwarf2_cu *);
847
848 static struct type *die_descriptive_type (struct die_info *,
849 struct dwarf2_cu *);
850
851 static void set_descriptive_type (struct type *, struct die_info *,
852 struct dwarf2_cu *);
853
854 static struct type *die_containing_type (struct die_info *,
855 struct dwarf2_cu *);
856
857 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
858 struct dwarf2_cu *);
859
860 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
861
862 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
863
864 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
865
866 static char *typename_concat (struct obstack *obs, const char *prefix,
867 const char *suffix, int physname,
868 struct dwarf2_cu *cu);
869
870 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
871
872 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
873
874 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
875
876 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
877
878 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
879
880 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
881
882 /* Return the .debug_loclists section to use for cu. */
883 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
884
885 /* Return the .debug_rnglists section to use for cu. */
886 static struct dwarf2_section_info *cu_debug_rnglists_section
887 (struct dwarf2_cu *cu, dwarf_tag tag);
888
889 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
890 values. Keep the items ordered with increasing constraints compliance. */
891 enum pc_bounds_kind
892 {
893 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
894 PC_BOUNDS_NOT_PRESENT,
895
896 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
897 were present but they do not form a valid range of PC addresses. */
898 PC_BOUNDS_INVALID,
899
900 /* Discontiguous range was found - that is DW_AT_ranges was found. */
901 PC_BOUNDS_RANGES,
902
903 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
904 PC_BOUNDS_HIGH_LOW,
905 };
906
907 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
908 unrelocated_addr *,
909 unrelocated_addr *,
910 struct dwarf2_cu *,
911 addrmap_mutable *,
912 void *);
913
914 static void get_scope_pc_bounds (struct die_info *,
915 unrelocated_addr *, unrelocated_addr *,
916 struct dwarf2_cu *);
917
918 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
919 struct dwarf2_cu *);
920
921 static void dwarf2_add_field (struct field_info *, struct die_info *,
922 struct dwarf2_cu *);
923
924 static void dwarf2_attach_fields_to_type (struct field_info *,
925 struct type *, struct dwarf2_cu *);
926
927 static void dwarf2_add_member_fn (struct field_info *,
928 struct die_info *, struct type *,
929 struct dwarf2_cu *);
930
931 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
932 struct type *,
933 struct dwarf2_cu *);
934
935 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
936
937 static void read_common_block (struct die_info *, struct dwarf2_cu *);
938
939 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
940
941 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
942
943 static struct using_direct **using_directives (struct dwarf2_cu *cu);
944
945 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
946
947 static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
948
949 static struct type *read_module_type (struct die_info *die,
950 struct dwarf2_cu *cu);
951
952 static const char *namespace_name (struct die_info *die,
953 int *is_anonymous, struct dwarf2_cu *);
954
955 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
956
957 static bool decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
958 CORE_ADDR *addr);
959
960 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
961 struct dwarf2_cu *);
962
963 static struct die_info *read_die_and_siblings_1
964 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
965 struct die_info *);
966
967 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
968 const gdb_byte *info_ptr,
969 const gdb_byte **new_info_ptr,
970 struct die_info *parent);
971
972 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
973 struct die_info **, const gdb_byte *,
974 int, bool);
975
976 static const gdb_byte *read_toplevel_die (const struct die_reader_specs *,
977 struct die_info **,
978 const gdb_byte *,
979 gdb::array_view<attribute *> = {});
980
981 static void process_die (struct die_info *, struct dwarf2_cu *);
982
983 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
984 struct objfile *);
985
986 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
987
988 static const char *dwarf2_full_name (const char *name,
989 struct die_info *die,
990 struct dwarf2_cu *cu);
991
992 static const char *dwarf2_physname (const char *name, struct die_info *die,
993 struct dwarf2_cu *cu);
994
995 static struct die_info *dwarf2_extension (struct die_info *die,
996 struct dwarf2_cu **);
997
998 static void store_in_ref_table (struct die_info *,
999 struct dwarf2_cu *);
1000
1001 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1002 const struct attribute *,
1003 struct dwarf2_cu **);
1004
1005 static struct die_info *follow_die_ref (struct die_info *,
1006 const struct attribute *,
1007 struct dwarf2_cu **);
1008
1009 static struct die_info *follow_die_sig (struct die_info *,
1010 const struct attribute *,
1011 struct dwarf2_cu **);
1012
1013 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1014 struct dwarf2_cu *);
1015
1016 static struct type *get_DW_AT_signature_type (struct die_info *,
1017 const struct attribute *,
1018 struct dwarf2_cu *);
1019
1020 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1021 dwarf2_per_objfile *per_objfile);
1022
1023 static void read_signatured_type (signatured_type *sig_type,
1024 dwarf2_per_objfile *per_objfile);
1025
1026 static int attr_to_dynamic_prop (const struct attribute *attr,
1027 struct die_info *die, struct dwarf2_cu *cu,
1028 struct dynamic_prop *prop, struct type *type);
1029
1030 /* memory allocation interface */
1031
1032 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1033
1034 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1035
1036 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1037 struct dwarf2_loclist_baton *baton,
1038 const struct attribute *attr);
1039
1040 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1041 struct symbol *sym,
1042 struct dwarf2_cu *cu,
1043 int is_block);
1044
1045 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1046 const gdb_byte *info_ptr,
1047 const struct abbrev_info *abbrev,
1048 bool do_skip_children = true);
1049
1050 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1051 (sect_offset sect_off, unsigned int offset_in_dwz,
1052 dwarf2_per_bfd *per_bfd);
1053
1054 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1055 struct die_info *comp_unit_die,
1056 enum language pretend_language);
1057
1058 static struct type *set_die_type (struct die_info *, struct type *,
1059 struct dwarf2_cu *, bool = false);
1060
1061 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1062 dwarf2_per_objfile *per_objfile,
1063 dwarf2_cu *existing_cu,
1064 bool skip_partial,
1065 enum language pretend_language);
1066
1067 static void process_full_comp_unit (dwarf2_cu *cu,
1068 enum language pretend_language);
1069
1070 static void process_full_type_unit (dwarf2_cu *cu,
1071 enum language pretend_language);
1072
1073 static struct type *get_die_type_at_offset (sect_offset,
1074 dwarf2_per_cu_data *per_cu,
1075 dwarf2_per_objfile *per_objfile);
1076
1077 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1078
1079 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1080 dwarf2_per_objfile *per_objfile,
1081 enum language pretend_language);
1082
1083 static void process_queue (dwarf2_per_objfile *per_objfile);
1084
1085 static bool is_ada_import_or_export (dwarf2_cu *cu, const char *name,
1086 const char *linkagename);
1087
1088 /* Class, the destructor of which frees all allocated queue entries. This
1089 will only have work to do if an error was thrown while processing the
1090 dwarf. If no error was thrown then the queue entries should have all
1091 been processed, and freed, as we went along. */
1092
1093 class dwarf2_queue_guard
1094 {
1095 public:
1096 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1097 : m_per_objfile (per_objfile)
1098 {
1099 gdb_assert (!m_per_objfile->queue.has_value ());
1100
1101 m_per_objfile->queue.emplace ();
1102 }
1103
1104 /* Free any entries remaining on the queue. There should only be
1105 entries left if we hit an error while processing the dwarf. */
1106 ~dwarf2_queue_guard ()
1107 {
1108 gdb_assert (m_per_objfile->queue.has_value ());
1109
1110 m_per_objfile->queue.reset ();
1111 }
1112
1113 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1114
1115 private:
1116 dwarf2_per_objfile *m_per_objfile;
1117 };
1118
1119 dwarf2_queue_item::~dwarf2_queue_item ()
1120 {
1121 /* Anything still marked queued is likely to be in an
1122 inconsistent state, so discard it. */
1123 if (per_cu->queued)
1124 {
1125 per_objfile->remove_cu (per_cu);
1126 per_cu->queued = 0;
1127 }
1128 }
1129
1130 /* See dwarf2/read.h. */
1131
1132 void
1133 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1134 {
1135 if (data->is_debug_types)
1136 delete static_cast<signatured_type *> (data);
1137 else
1138 delete data;
1139 }
1140
1141 static file_and_directory &find_file_and_directory
1142 (struct die_info *die, struct dwarf2_cu *cu);
1143
1144 static const char *compute_include_file_name
1145 (const struct line_header *lh,
1146 const file_entry &fe,
1147 const file_and_directory &cu_info,
1148 std::string &name_holder);
1149
1150 static htab_up allocate_dwo_unit_table ();
1151
1152 static struct dwo_unit *lookup_dwo_unit_in_dwp
1153 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1154 const char *comp_dir, ULONGEST signature, int is_debug_types);
1155
1156 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1157
1158 static struct dwo_unit *lookup_dwo_comp_unit
1159 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1160 ULONGEST signature);
1161
1162 static struct dwo_unit *lookup_dwo_type_unit
1163 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1164
1165 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1166
1167 /* A unique pointer to a dwo_file. */
1168
1169 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1170
1171 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1172
1173 static void check_producer (struct dwarf2_cu *cu);
1174 \f
1175 /* Various complaints about symbol reading that don't abort the process. */
1176
1177 static void
1178 dwarf2_debug_line_missing_file_complaint (void)
1179 {
1180 complaint (_(".debug_line section has line data without a file"));
1181 }
1182
1183 static void
1184 dwarf2_debug_line_missing_end_sequence_complaint (void)
1185 {
1186 complaint (_(".debug_line section has line "
1187 "program sequence without an end"));
1188 }
1189
1190 static void
1191 dwarf2_complex_location_expr_complaint (void)
1192 {
1193 complaint (_("location expression too complex"));
1194 }
1195
1196 static void
1197 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1198 int arg3)
1199 {
1200 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1201 arg1, arg2, arg3);
1202 }
1203
1204 static void
1205 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1206 {
1207 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1208 arg1, arg2);
1209 }
1210
1211 /* See read.h. */
1212
1213 CORE_ADDR
1214 dwarf2_per_objfile::relocate (unrelocated_addr addr)
1215 {
1216 CORE_ADDR baseaddr = objfile->text_section_offset ();
1217 CORE_ADDR tem = (CORE_ADDR) addr + baseaddr;
1218 return gdbarch_adjust_dwarf2_addr (objfile->arch (), tem);
1219 }
1220
1221 /* Hash function for line_header_hash. */
1222
1223 static hashval_t
1224 line_header_hash (const struct line_header *ofs)
1225 {
1226 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1227 }
1228
1229 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1230
1231 static hashval_t
1232 line_header_hash_voidp (const void *item)
1233 {
1234 const struct line_header *ofs = (const struct line_header *) item;
1235
1236 return line_header_hash (ofs);
1237 }
1238
1239 /* Equality function for line_header_hash. */
1240
1241 static int
1242 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1243 {
1244 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1245 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1246
1247 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1248 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1249 }
1250
1251 /* See declaration. */
1252
1253 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1254 bool can_copy_)
1255 : obfd (obfd),
1256 can_copy (can_copy_)
1257 {
1258 if (names == NULL)
1259 names = &dwarf2_elf_names;
1260
1261 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1262 locate_sections (obfd, sec, *names);
1263 }
1264
1265 dwarf2_per_bfd::~dwarf2_per_bfd ()
1266 {
1267 /* Data from the per-BFD may be needed when finalizing the cooked
1268 index table, so wait here while this happens. */
1269 if (index_table != nullptr)
1270 index_table->wait_completely ();
1271
1272 for (auto &per_cu : all_units)
1273 per_cu->free_cached_file_names ();
1274
1275 /* Everything else should be on this->obstack. */
1276 }
1277
1278 /* See read.h. */
1279
1280 void
1281 dwarf2_per_objfile::remove_all_cus ()
1282 {
1283 gdb_assert (!queue.has_value ());
1284
1285 m_dwarf2_cus.clear ();
1286 }
1287
1288 /* A helper class that calls free_cached_comp_units on
1289 destruction. */
1290
1291 class free_cached_comp_units
1292 {
1293 public:
1294
1295 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1296 : m_per_objfile (per_objfile)
1297 {
1298 }
1299
1300 ~free_cached_comp_units ()
1301 {
1302 m_per_objfile->remove_all_cus ();
1303 }
1304
1305 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1306
1307 private:
1308
1309 dwarf2_per_objfile *m_per_objfile;
1310 };
1311
1312 /* See read.h. */
1313
1314 bool
1315 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1316 {
1317 if (per_cu->index < this->m_symtabs.size ())
1318 return this->m_symtabs[per_cu->index] != nullptr;
1319 return false;
1320 }
1321
1322 /* See read.h. */
1323
1324 compunit_symtab *
1325 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1326 {
1327 if (per_cu->index < this->m_symtabs.size ())
1328 return this->m_symtabs[per_cu->index];
1329 return nullptr;
1330 }
1331
1332 /* See read.h. */
1333
1334 void
1335 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1336 compunit_symtab *symtab)
1337 {
1338 if (per_cu->index >= this->m_symtabs.size ())
1339 this->m_symtabs.resize (per_cu->index + 1);
1340 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1341 this->m_symtabs[per_cu->index] = symtab;
1342 }
1343
1344 /* Helper function for dwarf2_initialize_objfile that creates the
1345 per-BFD object. */
1346
1347 static bool
1348 dwarf2_has_info (struct objfile *objfile,
1349 const struct dwarf2_debug_sections *names,
1350 bool can_copy)
1351 {
1352 if (objfile->flags & OBJF_READNEVER)
1353 return false;
1354
1355 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1356 bool just_created = false;
1357
1358 if (per_objfile == NULL)
1359 {
1360 dwarf2_per_bfd *per_bfd;
1361
1362 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1363 BFD doesn't require relocations.
1364
1365 We don't share with objfiles for which -readnow was requested,
1366 because it would complicate things when loading the same BFD with
1367 -readnow and then without -readnow. */
1368 if (!gdb_bfd_requires_relocations (objfile->obfd.get ())
1369 && (objfile->flags & OBJF_READNOW) == 0)
1370 {
1371 /* See if one has been created for this BFD yet. */
1372 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd.get ());
1373
1374 if (per_bfd == nullptr)
1375 {
1376 /* No, create it now. */
1377 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
1378 can_copy);
1379 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
1380 just_created = true;
1381 }
1382 }
1383 else
1384 {
1385 /* No sharing possible, create one specifically for this objfile. */
1386 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
1387 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1388 just_created = true;
1389 }
1390
1391 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1392 }
1393
1394 const bool has_info = (!per_objfile->per_bfd->info.is_virtual
1395 && per_objfile->per_bfd->info.s.section != nullptr
1396 && !per_objfile->per_bfd->abbrev.is_virtual
1397 && per_objfile->per_bfd->abbrev.s.section != nullptr);
1398
1399 if (just_created && has_info)
1400 {
1401 /* Try to fetch any potential dwz file early, while still on
1402 the main thread. Also, be sure to do it just once per
1403 BFD, to avoid races. */
1404 try
1405 {
1406 dwarf2_read_dwz_file (per_objfile);
1407 }
1408 catch (const gdb_exception_error &err)
1409 {
1410 warning (_("%s"), err.what ());
1411 }
1412 }
1413
1414 return has_info;
1415 }
1416
1417 /* See declaration. */
1418
1419 void
1420 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1421 const dwarf2_debug_sections &names)
1422 {
1423 flagword aflag = bfd_section_flags (sectp);
1424
1425 if ((aflag & SEC_HAS_CONTENTS) == 0)
1426 {
1427 }
1428 else if (elf_section_data (sectp)->this_hdr.sh_size
1429 > bfd_get_file_size (abfd))
1430 {
1431 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1432 warning (_("Discarding section %s which has a section size (%s"
1433 ") larger than the file size [in module %s]"),
1434 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1435 bfd_get_filename (abfd));
1436 }
1437 else if (names.info.matches (sectp->name))
1438 {
1439 this->info.s.section = sectp;
1440 this->info.size = bfd_section_size (sectp);
1441 }
1442 else if (names.abbrev.matches (sectp->name))
1443 {
1444 this->abbrev.s.section = sectp;
1445 this->abbrev.size = bfd_section_size (sectp);
1446 }
1447 else if (names.line.matches (sectp->name))
1448 {
1449 this->line.s.section = sectp;
1450 this->line.size = bfd_section_size (sectp);
1451 }
1452 else if (names.loc.matches (sectp->name))
1453 {
1454 this->loc.s.section = sectp;
1455 this->loc.size = bfd_section_size (sectp);
1456 }
1457 else if (names.loclists.matches (sectp->name))
1458 {
1459 this->loclists.s.section = sectp;
1460 this->loclists.size = bfd_section_size (sectp);
1461 }
1462 else if (names.macinfo.matches (sectp->name))
1463 {
1464 this->macinfo.s.section = sectp;
1465 this->macinfo.size = bfd_section_size (sectp);
1466 }
1467 else if (names.macro.matches (sectp->name))
1468 {
1469 this->macro.s.section = sectp;
1470 this->macro.size = bfd_section_size (sectp);
1471 }
1472 else if (names.str.matches (sectp->name))
1473 {
1474 this->str.s.section = sectp;
1475 this->str.size = bfd_section_size (sectp);
1476 }
1477 else if (names.str_offsets.matches (sectp->name))
1478 {
1479 this->str_offsets.s.section = sectp;
1480 this->str_offsets.size = bfd_section_size (sectp);
1481 }
1482 else if (names.line_str.matches (sectp->name))
1483 {
1484 this->line_str.s.section = sectp;
1485 this->line_str.size = bfd_section_size (sectp);
1486 }
1487 else if (names.addr.matches (sectp->name))
1488 {
1489 this->addr.s.section = sectp;
1490 this->addr.size = bfd_section_size (sectp);
1491 }
1492 else if (names.frame.matches (sectp->name))
1493 {
1494 this->frame.s.section = sectp;
1495 this->frame.size = bfd_section_size (sectp);
1496 }
1497 else if (names.eh_frame.matches (sectp->name))
1498 {
1499 this->eh_frame.s.section = sectp;
1500 this->eh_frame.size = bfd_section_size (sectp);
1501 }
1502 else if (names.ranges.matches (sectp->name))
1503 {
1504 this->ranges.s.section = sectp;
1505 this->ranges.size = bfd_section_size (sectp);
1506 }
1507 else if (names.rnglists.matches (sectp->name))
1508 {
1509 this->rnglists.s.section = sectp;
1510 this->rnglists.size = bfd_section_size (sectp);
1511 }
1512 else if (names.types.matches (sectp->name))
1513 {
1514 struct dwarf2_section_info type_section;
1515
1516 memset (&type_section, 0, sizeof (type_section));
1517 type_section.s.section = sectp;
1518 type_section.size = bfd_section_size (sectp);
1519
1520 this->types.push_back (type_section);
1521 }
1522 else if (names.gdb_index.matches (sectp->name))
1523 {
1524 this->gdb_index.s.section = sectp;
1525 this->gdb_index.size = bfd_section_size (sectp);
1526 }
1527 else if (names.debug_names.matches (sectp->name))
1528 {
1529 this->debug_names.s.section = sectp;
1530 this->debug_names.size = bfd_section_size (sectp);
1531 }
1532 else if (names.debug_aranges.matches (sectp->name))
1533 {
1534 this->debug_aranges.s.section = sectp;
1535 this->debug_aranges.size = bfd_section_size (sectp);
1536 }
1537
1538 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1539 && bfd_section_vma (sectp) == 0)
1540 this->has_section_at_zero = true;
1541 }
1542
1543 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1544 SECTION_NAME. */
1545
1546 void
1547 dwarf2_get_section_info (struct objfile *objfile,
1548 enum dwarf2_section_enum sect,
1549 asection **sectp, const gdb_byte **bufp,
1550 bfd_size_type *sizep)
1551 {
1552 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1553 struct dwarf2_section_info *info;
1554
1555 /* We may see an objfile without any DWARF, in which case we just
1556 return nothing. */
1557 if (per_objfile == NULL)
1558 {
1559 *sectp = NULL;
1560 *bufp = NULL;
1561 *sizep = 0;
1562 return;
1563 }
1564 switch (sect)
1565 {
1566 case DWARF2_DEBUG_FRAME:
1567 info = &per_objfile->per_bfd->frame;
1568 break;
1569 case DWARF2_EH_FRAME:
1570 info = &per_objfile->per_bfd->eh_frame;
1571 break;
1572 default:
1573 gdb_assert_not_reached ("unexpected section");
1574 }
1575
1576 info->read (objfile);
1577
1578 *sectp = info->get_bfd_section ();
1579 *bufp = info->buffer;
1580 *sizep = info->size;
1581 }
1582
1583 /* See dwarf2/read.h. */
1584
1585 void
1586 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1587 {
1588 info.read (objfile);
1589 abbrev.read (objfile);
1590 line.read (objfile);
1591 str.read (objfile);
1592 str_offsets.read (objfile);
1593 line_str.read (objfile);
1594 ranges.read (objfile);
1595 rnglists.read (objfile);
1596 addr.read (objfile);
1597 debug_aranges.read (objfile);
1598
1599 for (auto &section : types)
1600 section.read (objfile);
1601 }
1602
1603 \f
1604 /* DWARF quick_symbol_functions support. */
1605
1606 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1607 unique line tables, so we maintain a separate table of all .debug_line
1608 derived entries to support the sharing.
1609 All the quick functions need is the list of file names. We discard the
1610 line_header when we're done and don't need to record it here. */
1611 struct quick_file_names
1612 {
1613 /* The data used to construct the hash key. */
1614 struct stmt_list_hash hash;
1615
1616 /* The number of entries in file_names, real_names. */
1617 unsigned int num_file_names;
1618
1619 /* The CU directory, as given by DW_AT_comp_dir. May be
1620 nullptr. */
1621 const char *comp_dir;
1622
1623 /* The file names from the line table, after being run through
1624 file_full_name. */
1625 const char **file_names;
1626
1627 /* The file names from the line table after being run through
1628 gdb_realpath. These are computed lazily. */
1629 const char **real_names;
1630 };
1631
1632 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1633 It's handy in this case to have an empty implementation of the
1634 quick symbol functions, to avoid special cases in the rest of the
1635 code. */
1636
1637 struct readnow_functions : public dwarf2_base_index_functions
1638 {
1639 void dump (struct objfile *objfile) override
1640 {
1641 }
1642
1643 bool expand_symtabs_matching
1644 (struct objfile *objfile,
1645 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1646 const lookup_name_info *lookup_name,
1647 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1648 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1649 block_search_flags search_flags,
1650 domain_search_flags domain) override
1651 {
1652 return true;
1653 }
1654 };
1655
1656 /* Utility hash function for a stmt_list_hash. */
1657
1658 static hashval_t
1659 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1660 {
1661 hashval_t v = 0;
1662
1663 if (stmt_list_hash->dwo_unit != NULL)
1664 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1665 v += to_underlying (stmt_list_hash->line_sect_off);
1666 return v;
1667 }
1668
1669 /* Utility equality function for a stmt_list_hash. */
1670
1671 static int
1672 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1673 const struct stmt_list_hash *rhs)
1674 {
1675 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1676 return 0;
1677 if (lhs->dwo_unit != NULL
1678 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1679 return 0;
1680
1681 return lhs->line_sect_off == rhs->line_sect_off;
1682 }
1683
1684 /* Hash function for a quick_file_names. */
1685
1686 static hashval_t
1687 hash_file_name_entry (const void *e)
1688 {
1689 const struct quick_file_names *file_data
1690 = (const struct quick_file_names *) e;
1691
1692 return hash_stmt_list_entry (&file_data->hash);
1693 }
1694
1695 /* Equality function for a quick_file_names. */
1696
1697 static int
1698 eq_file_name_entry (const void *a, const void *b)
1699 {
1700 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1701 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1702
1703 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1704 }
1705
1706 /* See read.h. */
1707
1708 htab_up
1709 create_quick_file_names_table (unsigned int nr_initial_entries)
1710 {
1711 return htab_up (htab_create_alloc (nr_initial_entries,
1712 hash_file_name_entry, eq_file_name_entry,
1713 nullptr, xcalloc, xfree));
1714 }
1715
1716 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
1717 function is unrelated to symtabs, symtab would have to be created afterwards.
1718 You should call age_cached_comp_units after processing the CU. */
1719
1720 static dwarf2_cu *
1721 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1722 bool skip_partial)
1723 {
1724 if (per_cu->is_debug_types)
1725 load_full_type_unit (per_cu, per_objfile);
1726 else
1727 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
1728 skip_partial, language_minimal);
1729
1730 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1731 if (cu == nullptr)
1732 return nullptr; /* Dummy CU. */
1733
1734 dwarf2_find_base_address (cu->dies, cu);
1735
1736 return cu;
1737 }
1738
1739 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
1740
1741 static void
1742 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1743 dwarf2_per_objfile *per_objfile, bool skip_partial)
1744 {
1745 {
1746 /* The destructor of dwarf2_queue_guard frees any entries left on
1747 the queue. After this point we're guaranteed to leave this function
1748 with the dwarf queue empty. */
1749 dwarf2_queue_guard q_guard (per_objfile);
1750
1751 if (!per_objfile->symtab_set_p (per_cu))
1752 {
1753 queue_comp_unit (per_cu, per_objfile, language_minimal);
1754 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
1755
1756 /* If we just loaded a CU from a DWO, and we're working with an index
1757 that may badly handle TUs, load all the TUs in that DWO as well.
1758 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
1759 if (!per_cu->is_debug_types
1760 && cu != NULL
1761 && cu->dwo_unit != NULL
1762 && per_objfile->per_bfd->index_table != NULL
1763 && !per_objfile->per_bfd->index_table->version_check ()
1764 /* DWP files aren't supported yet. */
1765 && get_dwp_file (per_objfile) == NULL)
1766 queue_and_load_all_dwo_tus (cu);
1767 }
1768
1769 process_queue (per_objfile);
1770 }
1771
1772 /* Age the cache, releasing compilation units that have not
1773 been used recently. */
1774 per_objfile->age_comp_units ();
1775 }
1776
1777 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
1778 the per-objfile for which this symtab is instantiated.
1779
1780 Returns the resulting symbol table. */
1781
1782 static struct compunit_symtab *
1783 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1784 dwarf2_per_objfile *per_objfile,
1785 bool skip_partial)
1786 {
1787 if (!per_objfile->symtab_set_p (per_cu))
1788 {
1789 free_cached_comp_units freer (per_objfile);
1790 scoped_restore decrementer = increment_reading_symtab ();
1791 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
1792 process_cu_includes (per_objfile);
1793 }
1794
1795 return per_objfile->get_symtab (per_cu);
1796 }
1797
1798 /* See read.h. */
1799
1800 dwarf2_per_cu_data_up
1801 dwarf2_per_bfd::allocate_per_cu ()
1802 {
1803 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
1804 result->per_bfd = this;
1805 result->index = all_units.size ();
1806 return result;
1807 }
1808
1809 /* See read.h. */
1810
1811 signatured_type_up
1812 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
1813 {
1814 signatured_type_up result (new signatured_type (signature));
1815 result->per_bfd = this;
1816 result->index = all_units.size ();
1817 result->is_debug_types = true;
1818 tu_stats.nr_tus++;
1819 return result;
1820 }
1821
1822 /* See read.h. */
1823
1824 dwarf2_per_cu_data_up
1825 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
1826 struct dwarf2_section_info *section,
1827 int is_dwz,
1828 sect_offset sect_off, ULONGEST length)
1829 {
1830 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
1831 the_cu->sect_off = sect_off;
1832 the_cu->set_length (length);
1833 the_cu->section = section;
1834 the_cu->is_dwz = is_dwz;
1835 return the_cu;
1836 }
1837
1838 /* die_reader_func for dw2_get_file_names. */
1839
1840 static void
1841 dw2_get_file_names_reader (const struct die_reader_specs *reader,
1842 struct die_info *comp_unit_die)
1843 {
1844 struct dwarf2_cu *cu = reader->cu;
1845 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
1846 dwarf2_per_objfile *per_objfile = cu->per_objfile;
1847 struct dwarf2_per_cu_data *lh_cu;
1848 struct attribute *attr;
1849 void **slot;
1850 struct quick_file_names *qfn;
1851
1852 gdb_assert (! this_cu->is_debug_types);
1853
1854 this_cu->files_read = true;
1855 /* Our callers never want to match partial units -- instead they
1856 will match the enclosing full CU. */
1857 if (comp_unit_die->tag == DW_TAG_partial_unit)
1858 return;
1859
1860 lh_cu = this_cu;
1861 slot = NULL;
1862
1863 line_header_up lh;
1864 sect_offset line_offset {};
1865
1866 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
1867
1868 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
1869 if (attr != nullptr && attr->form_is_unsigned ())
1870 {
1871 struct quick_file_names find_entry;
1872
1873 line_offset = (sect_offset) attr->as_unsigned ();
1874
1875 /* We may have already read in this line header (TU line header sharing).
1876 If we have we're done. */
1877 find_entry.hash.dwo_unit = cu->dwo_unit;
1878 find_entry.hash.line_sect_off = line_offset;
1879 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
1880 &find_entry, INSERT);
1881 if (*slot != NULL)
1882 {
1883 lh_cu->file_names = (struct quick_file_names *) *slot;
1884 return;
1885 }
1886
1887 lh = dwarf_decode_line_header (line_offset, cu, fnd.get_comp_dir ());
1888 }
1889
1890 int offset = 0;
1891 if (!fnd.is_unknown ())
1892 ++offset;
1893 else if (lh == nullptr)
1894 return;
1895
1896 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
1897 qfn->hash.dwo_unit = cu->dwo_unit;
1898 qfn->hash.line_sect_off = line_offset;
1899 /* There may not be a DW_AT_stmt_list. */
1900 if (slot != nullptr)
1901 *slot = qfn;
1902
1903 std::vector<const char *> include_names;
1904 if (lh != nullptr)
1905 {
1906 for (const auto &entry : lh->file_names ())
1907 {
1908 std::string name_holder;
1909 const char *include_name =
1910 compute_include_file_name (lh.get (), entry, fnd, name_holder);
1911 if (include_name != nullptr)
1912 {
1913 include_name = per_objfile->objfile->intern (include_name);
1914 include_names.push_back (include_name);
1915 }
1916 }
1917 }
1918
1919 qfn->num_file_names = offset + include_names.size ();
1920 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
1921 qfn->file_names =
1922 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
1923 qfn->num_file_names);
1924 if (offset != 0)
1925 qfn->file_names[0] = per_objfile->objfile->intern (fnd.get_name ());
1926
1927 if (!include_names.empty ())
1928 memcpy (&qfn->file_names[offset], include_names.data (),
1929 include_names.size () * sizeof (const char *));
1930
1931 qfn->real_names = NULL;
1932
1933 lh_cu->file_names = qfn;
1934 }
1935
1936 /* A helper for the "quick" functions which attempts to read the line
1937 table for THIS_CU. */
1938
1939 static struct quick_file_names *
1940 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
1941 dwarf2_per_objfile *per_objfile)
1942 {
1943 /* This should never be called for TUs. */
1944 gdb_assert (! this_cu->is_debug_types);
1945
1946 if (this_cu->files_read)
1947 return this_cu->file_names;
1948
1949 cutu_reader reader (this_cu, per_objfile);
1950 if (!reader.dummy_p)
1951 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
1952
1953 return this_cu->file_names;
1954 }
1955
1956 /* A helper for the "quick" functions which computes and caches the
1957 real path for a given file name from the line table. */
1958
1959 static const char *
1960 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
1961 struct quick_file_names *qfn, int index)
1962 {
1963 if (qfn->real_names == NULL)
1964 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
1965 qfn->num_file_names, const char *);
1966
1967 if (qfn->real_names[index] == NULL)
1968 {
1969 const char *dirname = nullptr;
1970
1971 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
1972 dirname = qfn->comp_dir;
1973
1974 gdb::unique_xmalloc_ptr<char> fullname;
1975 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
1976
1977 qfn->real_names[index] = fullname.release ();
1978 }
1979
1980 return qfn->real_names[index];
1981 }
1982
1983 struct symtab *
1984 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
1985 {
1986 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1987 dwarf2_per_cu_data *dwarf_cu
1988 = per_objfile->per_bfd->all_units.back ().get ();
1989 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
1990
1991 if (cust == NULL)
1992 return NULL;
1993
1994 return cust->primary_filetab ();
1995 }
1996
1997 /* See read.h. */
1998
1999 void
2000 dwarf2_per_cu_data::free_cached_file_names ()
2001 {
2002 if (fnd != nullptr)
2003 fnd->forget_fullname ();
2004
2005 if (per_bfd == nullptr)
2006 return;
2007
2008 struct quick_file_names *file_data = file_names;
2009 if (file_data != nullptr && file_data->real_names != nullptr)
2010 {
2011 for (int i = 0; i < file_data->num_file_names; ++i)
2012 {
2013 xfree ((void *) file_data->real_names[i]);
2014 file_data->real_names[i] = nullptr;
2015 }
2016 }
2017 }
2018
2019 void
2020 dwarf2_base_index_functions::forget_cached_source_info
2021 (struct objfile *objfile)
2022 {
2023 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2024
2025 for (auto &per_cu : per_objfile->per_bfd->all_units)
2026 per_cu->free_cached_file_names ();
2027 }
2028
2029 void
2030 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
2031 bool print_bcache)
2032 {
2033 if (print_bcache)
2034 return;
2035
2036 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2037 int total = per_objfile->per_bfd->all_units.size ();
2038 int count = 0;
2039
2040 for (int i = 0; i < total; ++i)
2041 {
2042 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2043
2044 if (!per_objfile->symtab_set_p (per_cu))
2045 ++count;
2046 }
2047 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
2048 gdb_printf (_(" Number of unread CUs: %d\n"), count);
2049 }
2050
2051 void
2052 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
2053 {
2054 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2055 int total_units = per_objfile->per_bfd->all_units.size ();
2056
2057 for (int i = 0; i < total_units; ++i)
2058 {
2059 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2060
2061 /* We don't want to directly expand a partial CU, because if we
2062 read it with the wrong language, then assertion failures can
2063 be triggered later on. See PR symtab/23010. So, tell
2064 dw2_instantiate_symtab to skip partial CUs -- any important
2065 partial CU will be read via DW_TAG_imported_unit anyway. */
2066 dw2_instantiate_symtab (per_cu, per_objfile, true);
2067 }
2068 }
2069
2070
2071 /* Starting from a search name, return the string that finds the upper
2072 bound of all strings that start with SEARCH_NAME in a sorted name
2073 list. Returns the empty string to indicate that the upper bound is
2074 the end of the list. */
2075
2076 static std::string
2077 make_sort_after_prefix_name (const char *search_name)
2078 {
2079 /* When looking to complete "func", we find the upper bound of all
2080 symbols that start with "func" by looking for where we'd insert
2081 the closest string that would follow "func" in lexicographical
2082 order. Usually, that's "func"-with-last-character-incremented,
2083 i.e. "fund". Mind non-ASCII characters, though. Usually those
2084 will be UTF-8 multi-byte sequences, but we can't be certain.
2085 Especially mind the 0xff character, which is a valid character in
2086 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
2087 rule out compilers allowing it in identifiers. Note that
2088 conveniently, strcmp/strcasecmp are specified to compare
2089 characters interpreted as unsigned char. So what we do is treat
2090 the whole string as a base 256 number composed of a sequence of
2091 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
2092 to 0, and carries 1 to the following more-significant position.
2093 If the very first character in SEARCH_NAME ends up incremented
2094 and carries/overflows, then the upper bound is the end of the
2095 list. The string after the empty string is also the empty
2096 string.
2097
2098 Some examples of this operation:
2099
2100 SEARCH_NAME => "+1" RESULT
2101
2102 "abc" => "abd"
2103 "ab\xff" => "ac"
2104 "\xff" "a" "\xff" => "\xff" "b"
2105 "\xff" => ""
2106 "\xff\xff" => ""
2107 "" => ""
2108
2109 Then, with these symbols for example:
2110
2111 func
2112 func1
2113 fund
2114
2115 completing "func" looks for symbols between "func" and
2116 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
2117 which finds "func" and "func1", but not "fund".
2118
2119 And with:
2120
2121 funcÿ (Latin1 'ÿ' [0xff])
2122 funcÿ1
2123 fund
2124
2125 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
2126 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
2127
2128 And with:
2129
2130 ÿÿ (Latin1 'ÿ' [0xff])
2131 ÿÿ1
2132
2133 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
2134 the end of the list.
2135 */
2136 std::string after = search_name;
2137 while (!after.empty () && (unsigned char) after.back () == 0xff)
2138 after.pop_back ();
2139 if (!after.empty ())
2140 after.back () = (unsigned char) after.back () + 1;
2141 return after;
2142 }
2143
2144 /* See declaration. */
2145
2146 std::pair<std::vector<name_component>::const_iterator,
2147 std::vector<name_component>::const_iterator>
2148 mapped_index_base::find_name_components_bounds
2149 (const lookup_name_info &lookup_name_without_params, language lang,
2150 dwarf2_per_objfile *per_objfile) const
2151 {
2152 auto *name_cmp
2153 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
2154
2155 const char *lang_name
2156 = lookup_name_without_params.language_lookup_name (lang);
2157
2158 /* Comparison function object for lower_bound that matches against a
2159 given symbol name. */
2160 auto lookup_compare_lower = [&] (const name_component &elem,
2161 const char *name)
2162 {
2163 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
2164 const char *elem_name = elem_qualified + elem.name_offset;
2165 return name_cmp (elem_name, name) < 0;
2166 };
2167
2168 /* Comparison function object for upper_bound that matches against a
2169 given symbol name. */
2170 auto lookup_compare_upper = [&] (const char *name,
2171 const name_component &elem)
2172 {
2173 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
2174 const char *elem_name = elem_qualified + elem.name_offset;
2175 return name_cmp (name, elem_name) < 0;
2176 };
2177
2178 auto begin = this->name_components.begin ();
2179 auto end = this->name_components.end ();
2180
2181 /* Find the lower bound. */
2182 auto lower = [&] ()
2183 {
2184 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
2185 return begin;
2186 else
2187 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
2188 } ();
2189
2190 /* Find the upper bound. */
2191 auto upper = [&] ()
2192 {
2193 if (lookup_name_without_params.completion_mode ())
2194 {
2195 /* In completion mode, we want UPPER to point past all
2196 symbols names that have the same prefix. I.e., with
2197 these symbols, and completing "func":
2198
2199 function << lower bound
2200 function1
2201 other_function << upper bound
2202
2203 We find the upper bound by looking for the insertion
2204 point of "func"-with-last-character-incremented,
2205 i.e. "fund". */
2206 std::string after = make_sort_after_prefix_name (lang_name);
2207 if (after.empty ())
2208 return end;
2209 return std::lower_bound (lower, end, after.c_str (),
2210 lookup_compare_lower);
2211 }
2212 else
2213 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
2214 } ();
2215
2216 return {lower, upper};
2217 }
2218
2219 /* See declaration. */
2220
2221 void
2222 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
2223 {
2224 if (!this->name_components.empty ())
2225 return;
2226
2227 this->name_components_casing = case_sensitivity;
2228 auto *name_cmp
2229 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
2230
2231 /* The code below only knows how to break apart components of C++
2232 symbol names (and other languages that use '::' as
2233 namespace/module separator) and Ada symbol names. */
2234 auto count = this->symbol_name_count ();
2235 for (offset_type idx = 0; idx < count; idx++)
2236 {
2237 if (this->symbol_name_slot_invalid (idx))
2238 continue;
2239
2240 const char *name = this->symbol_name_at (idx, per_objfile);
2241
2242 /* Add each name component to the name component table. */
2243 unsigned int previous_len = 0;
2244
2245 if (strstr (name, "::") != nullptr)
2246 {
2247 for (unsigned int current_len = cp_find_first_component (name);
2248 name[current_len] != '\0';
2249 current_len += cp_find_first_component (name + current_len))
2250 {
2251 gdb_assert (name[current_len] == ':');
2252 this->name_components.push_back ({previous_len, idx});
2253 /* Skip the '::'. */
2254 current_len += 2;
2255 previous_len = current_len;
2256 }
2257 }
2258 else
2259 {
2260 /* Handle the Ada encoded (aka mangled) form here. */
2261 for (const char *iter = strstr (name, "__");
2262 iter != nullptr;
2263 iter = strstr (iter, "__"))
2264 {
2265 this->name_components.push_back ({previous_len, idx});
2266 iter += 2;
2267 previous_len = iter - name;
2268 }
2269 }
2270
2271 this->name_components.push_back ({previous_len, idx});
2272 }
2273
2274 /* Sort name_components elements by name. */
2275 auto name_comp_compare = [&] (const name_component &left,
2276 const name_component &right)
2277 {
2278 const char *left_qualified
2279 = this->symbol_name_at (left.idx, per_objfile);
2280 const char *right_qualified
2281 = this->symbol_name_at (right.idx, per_objfile);
2282
2283 const char *left_name = left_qualified + left.name_offset;
2284 const char *right_name = right_qualified + right.name_offset;
2285
2286 return name_cmp (left_name, right_name) < 0;
2287 };
2288
2289 std::sort (this->name_components.begin (),
2290 this->name_components.end (),
2291 name_comp_compare);
2292 }
2293
2294 /* See read.h. */
2295
2296 bool
2297 dw2_expand_symtabs_matching_symbol
2298 (mapped_index_base &index,
2299 const lookup_name_info &lookup_name_in,
2300 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2301 gdb::function_view<bool (offset_type)> match_callback,
2302 dwarf2_per_objfile *per_objfile)
2303 {
2304 lookup_name_info lookup_name_without_params
2305 = lookup_name_in.make_ignore_params ();
2306
2307 /* Build the symbol name component sorted vector, if we haven't
2308 yet. */
2309 index.build_name_components (per_objfile);
2310
2311 /* The same symbol may appear more than once in the range though.
2312 E.g., if we're looking for symbols that complete "w", and we have
2313 a symbol named "w1::w2", we'll find the two name components for
2314 that same symbol in the range. To be sure we only call the
2315 callback once per symbol, we first collect the symbol name
2316 indexes that matched in a temporary vector and ignore
2317 duplicates. */
2318 std::vector<offset_type> matches;
2319
2320 struct name_and_matcher
2321 {
2322 symbol_name_matcher_ftype *matcher;
2323 const char *name;
2324
2325 bool operator== (const name_and_matcher &other) const
2326 {
2327 return matcher == other.matcher && strcmp (name, other.name) == 0;
2328 }
2329 };
2330
2331 /* A vector holding all the different symbol name matchers, for all
2332 languages. */
2333 std::vector<name_and_matcher> matchers;
2334
2335 for (int i = 0; i < nr_languages; i++)
2336 {
2337 enum language lang_e = (enum language) i;
2338
2339 const language_defn *lang = language_def (lang_e);
2340 symbol_name_matcher_ftype *name_matcher
2341 = lang->get_symbol_name_matcher (lookup_name_without_params);
2342
2343 name_and_matcher key {
2344 name_matcher,
2345 lookup_name_without_params.language_lookup_name (lang_e)
2346 };
2347
2348 /* Don't insert the same comparison routine more than once.
2349 Note that we do this linear walk. This is not a problem in
2350 practice because the number of supported languages is
2351 low. */
2352 if (std::find (matchers.begin (), matchers.end (), key)
2353 != matchers.end ())
2354 continue;
2355 matchers.push_back (std::move (key));
2356
2357 auto bounds
2358 = index.find_name_components_bounds (lookup_name_without_params,
2359 lang_e, per_objfile);
2360
2361 /* Now for each symbol name in range, check to see if we have a name
2362 match, and if so, call the MATCH_CALLBACK callback. */
2363
2364 for (; bounds.first != bounds.second; ++bounds.first)
2365 {
2366 const char *qualified
2367 = index.symbol_name_at (bounds.first->idx, per_objfile);
2368
2369 if (!name_matcher (qualified, lookup_name_without_params, NULL)
2370 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
2371 continue;
2372
2373 matches.push_back (bounds.first->idx);
2374 }
2375 }
2376
2377 std::sort (matches.begin (), matches.end ());
2378
2379 /* Finally call the callback, once per match. */
2380 ULONGEST prev = -1;
2381 bool result = true;
2382 for (offset_type idx : matches)
2383 {
2384 if (prev != idx)
2385 {
2386 if (!match_callback (idx))
2387 {
2388 result = false;
2389 break;
2390 }
2391 prev = idx;
2392 }
2393 }
2394
2395 /* Above we use a type wider than idx's for 'prev', since 0 and
2396 (offset_type)-1 are both possible values. */
2397 static_assert (sizeof (prev) > sizeof (offset_type), "");
2398
2399 return result;
2400 }
2401
2402 #if GDB_SELF_TEST
2403
2404 namespace selftests { namespace dw2_expand_symtabs_matching {
2405
2406 /* A mock .gdb_index/.debug_names-like name index table, enough to
2407 exercise dw2_expand_symtabs_matching_symbol, which works with the
2408 mapped_index_base interface. Builds an index from the symbol list
2409 passed as parameter to the constructor. */
2410 class mock_mapped_index : public mapped_index_base
2411 {
2412 public:
2413 mock_mapped_index (gdb::array_view<const char *> symbols)
2414 : m_symbol_table (symbols)
2415 {}
2416
2417 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
2418
2419 /* Return the number of names in the symbol table. */
2420 size_t symbol_name_count () const override
2421 {
2422 return m_symbol_table.size ();
2423 }
2424
2425 /* Get the name of the symbol at IDX in the symbol table. */
2426 const char *symbol_name_at
2427 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
2428 {
2429 return m_symbol_table[idx];
2430 }
2431
2432 quick_symbol_functions_up make_quick_functions () const override
2433 {
2434 return nullptr;
2435 }
2436
2437 private:
2438 gdb::array_view<const char *> m_symbol_table;
2439 };
2440
2441 /* Convenience function that converts a NULL pointer to a "<null>"
2442 string, to pass to print routines. */
2443
2444 static const char *
2445 string_or_null (const char *str)
2446 {
2447 return str != NULL ? str : "<null>";
2448 }
2449
2450 /* Check if a lookup_name_info built from
2451 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
2452 index. EXPECTED_LIST is the list of expected matches, in expected
2453 matching order. If no match expected, then an empty list is
2454 specified. Returns true on success. On failure prints a warning
2455 indicating the file:line that failed, and returns false. */
2456
2457 static bool
2458 check_match (const char *file, int line,
2459 mock_mapped_index &mock_index,
2460 const char *name, symbol_name_match_type match_type,
2461 bool completion_mode,
2462 std::initializer_list<const char *> expected_list,
2463 dwarf2_per_objfile *per_objfile)
2464 {
2465 lookup_name_info lookup_name (name, match_type, completion_mode);
2466
2467 bool matched = true;
2468
2469 auto mismatch = [&] (const char *expected_str,
2470 const char *got)
2471 {
2472 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
2473 "expected=\"%s\", got=\"%s\"\n"),
2474 file, line,
2475 (match_type == symbol_name_match_type::FULL
2476 ? "FULL" : "WILD"),
2477 name, string_or_null (expected_str), string_or_null (got));
2478 matched = false;
2479 };
2480
2481 auto expected_it = expected_list.begin ();
2482 auto expected_end = expected_list.end ();
2483
2484 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
2485 nullptr,
2486 [&] (offset_type idx)
2487 {
2488 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
2489 const char *expected_str
2490 = expected_it == expected_end ? NULL : *expected_it++;
2491
2492 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
2493 mismatch (expected_str, matched_name);
2494 return true;
2495 }, per_objfile);
2496
2497 const char *expected_str
2498 = expected_it == expected_end ? NULL : *expected_it++;
2499 if (expected_str != NULL)
2500 mismatch (expected_str, NULL);
2501
2502 return matched;
2503 }
2504
2505 /* The symbols added to the mock mapped_index for testing (in
2506 canonical form). */
2507 static const char *test_symbols[] = {
2508 "function",
2509 "std::bar",
2510 "std::zfunction",
2511 "std::zfunction2",
2512 "w1::w2",
2513 "ns::foo<char*>",
2514 "ns::foo<int>",
2515 "ns::foo<long>",
2516 "ns2::tmpl<int>::foo2",
2517 "(anonymous namespace)::A::B::C",
2518
2519 /* These are used to check that the increment-last-char in the
2520 matching algorithm for completion doesn't match "t1_fund" when
2521 completing "t1_func". */
2522 "t1_func",
2523 "t1_func1",
2524 "t1_fund",
2525 "t1_fund1",
2526
2527 /* A UTF-8 name with multi-byte sequences to make sure that
2528 cp-name-parser understands this as a single identifier ("função"
2529 is "function" in PT). */
2530 (const char *)u8"u8função",
2531
2532 /* Test a symbol name that ends with a 0xff character, which is a
2533 valid character in non-UTF-8 source character sets (e.g. Latin1
2534 'ÿ'), and we can't rule out compilers allowing it in identifiers.
2535 We test this because the completion algorithm finds the upper
2536 bound of symbols by looking for the insertion point of
2537 "func"-with-last-character-incremented, i.e. "fund", and adding 1
2538 to 0xff should wraparound and carry to the previous character.
2539 See comments in make_sort_after_prefix_name. */
2540 "yfunc\377",
2541
2542 /* Some more symbols with \377 (0xff). See above. */
2543 "\377",
2544 "\377\377123",
2545
2546 /* A name with all sorts of complications. Starts with "z" to make
2547 it easier for the completion tests below. */
2548 #define Z_SYM_NAME \
2549 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
2550 "::tuple<(anonymous namespace)::ui*, " \
2551 "std::default_delete<(anonymous namespace)::ui>, void>"
2552
2553 Z_SYM_NAME
2554 };
2555
2556 /* Returns true if the mapped_index_base::find_name_component_bounds
2557 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
2558 in completion mode. */
2559
2560 static bool
2561 check_find_bounds_finds (mapped_index_base &index,
2562 const char *search_name,
2563 gdb::array_view<const char *> expected_syms,
2564 dwarf2_per_objfile *per_objfile)
2565 {
2566 lookup_name_info lookup_name (search_name,
2567 symbol_name_match_type::FULL, true);
2568
2569 auto bounds = index.find_name_components_bounds (lookup_name,
2570 language_cplus,
2571 per_objfile);
2572
2573 size_t distance = std::distance (bounds.first, bounds.second);
2574 if (distance != expected_syms.size ())
2575 return false;
2576
2577 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
2578 {
2579 auto nc_elem = bounds.first + exp_elem;
2580 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
2581 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
2582 return false;
2583 }
2584
2585 return true;
2586 }
2587
2588 /* Test the lower-level mapped_index::find_name_component_bounds
2589 method. */
2590
2591 static void
2592 test_mapped_index_find_name_component_bounds ()
2593 {
2594 mock_mapped_index mock_index (test_symbols);
2595
2596 mock_index.build_name_components (NULL /* per_objfile */);
2597
2598 /* Test the lower-level mapped_index::find_name_component_bounds
2599 method in completion mode. */
2600 {
2601 static const char *expected_syms[] = {
2602 "t1_func",
2603 "t1_func1",
2604 };
2605
2606 SELF_CHECK (check_find_bounds_finds
2607 (mock_index, "t1_func", expected_syms,
2608 NULL /* per_objfile */));
2609 }
2610
2611 /* Check that the increment-last-char in the name matching algorithm
2612 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
2613 make_sort_after_prefix_name. */
2614 {
2615 static const char *expected_syms1[] = {
2616 "\377",
2617 "\377\377123",
2618 };
2619 SELF_CHECK (check_find_bounds_finds
2620 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
2621
2622 static const char *expected_syms2[] = {
2623 "\377\377123",
2624 };
2625 SELF_CHECK (check_find_bounds_finds
2626 (mock_index, "\377\377", expected_syms2,
2627 NULL /* per_objfile */));
2628 }
2629 }
2630
2631 /* Test dw2_expand_symtabs_matching_symbol. */
2632
2633 static void
2634 test_dw2_expand_symtabs_matching_symbol ()
2635 {
2636 mock_mapped_index mock_index (test_symbols);
2637
2638 /* We let all tests run until the end even if some fails, for debug
2639 convenience. */
2640 bool any_mismatch = false;
2641
2642 /* Create the expected symbols list (an initializer_list). Needed
2643 because lists have commas, and we need to pass them to CHECK,
2644 which is a macro. */
2645 #define EXPECT(...) { __VA_ARGS__ }
2646
2647 /* Wrapper for check_match that passes down the current
2648 __FILE__/__LINE__. */
2649 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
2650 any_mismatch |= !check_match (__FILE__, __LINE__, \
2651 mock_index, \
2652 NAME, MATCH_TYPE, COMPLETION_MODE, \
2653 EXPECTED_LIST, NULL)
2654
2655 /* Identity checks. */
2656 for (const char *sym : test_symbols)
2657 {
2658 /* Should be able to match all existing symbols. */
2659 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
2660 EXPECT (sym));
2661
2662 /* Should be able to match all existing symbols with
2663 parameters. */
2664 std::string with_params = std::string (sym) + "(int)";
2665 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2666 EXPECT (sym));
2667
2668 /* Should be able to match all existing symbols with
2669 parameters and qualifiers. */
2670 with_params = std::string (sym) + " ( int ) const";
2671 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2672 EXPECT (sym));
2673
2674 /* This should really find sym, but cp-name-parser.y doesn't
2675 know about lvalue/rvalue qualifiers yet. */
2676 with_params = std::string (sym) + " ( int ) &&";
2677 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2678 {});
2679 }
2680
2681 /* Check that the name matching algorithm for completion doesn't get
2682 confused with Latin1 'ÿ' / 0xff. See
2683 make_sort_after_prefix_name. */
2684 {
2685 static const char str[] = "\377";
2686 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
2687 EXPECT ("\377", "\377\377123"));
2688 }
2689
2690 /* Check that the increment-last-char in the matching algorithm for
2691 completion doesn't match "t1_fund" when completing "t1_func". */
2692 {
2693 static const char str[] = "t1_func";
2694 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
2695 EXPECT ("t1_func", "t1_func1"));
2696 }
2697
2698 /* Check that completion mode works at each prefix of the expected
2699 symbol name. */
2700 {
2701 static const char str[] = "function(int)";
2702 size_t len = strlen (str);
2703 std::string lookup;
2704
2705 for (size_t i = 1; i < len; i++)
2706 {
2707 lookup.assign (str, i);
2708 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
2709 EXPECT ("function"));
2710 }
2711 }
2712
2713 /* While "w" is a prefix of both components, the match function
2714 should still only be called once. */
2715 {
2716 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
2717 EXPECT ("w1::w2"));
2718 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
2719 EXPECT ("w1::w2"));
2720 }
2721
2722 /* Same, with a "complicated" symbol. */
2723 {
2724 static const char str[] = Z_SYM_NAME;
2725 size_t len = strlen (str);
2726 std::string lookup;
2727
2728 for (size_t i = 1; i < len; i++)
2729 {
2730 lookup.assign (str, i);
2731 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
2732 EXPECT (Z_SYM_NAME));
2733 }
2734 }
2735
2736 /* In FULL mode, an incomplete symbol doesn't match. */
2737 {
2738 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
2739 {});
2740 }
2741
2742 /* A complete symbol with parameters matches any overload, since the
2743 index has no overload info. */
2744 {
2745 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
2746 EXPECT ("std::zfunction", "std::zfunction2"));
2747 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
2748 EXPECT ("std::zfunction", "std::zfunction2"));
2749 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
2750 EXPECT ("std::zfunction", "std::zfunction2"));
2751 }
2752
2753 /* Check that whitespace is ignored appropriately. A symbol with a
2754 template argument list. */
2755 {
2756 static const char expected[] = "ns::foo<int>";
2757 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
2758 EXPECT (expected));
2759 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
2760 EXPECT (expected));
2761 }
2762
2763 /* Check that whitespace is ignored appropriately. A symbol with a
2764 template argument list that includes a pointer. */
2765 {
2766 static const char expected[] = "ns::foo<char*>";
2767 /* Try both completion and non-completion modes. */
2768 static const bool completion_mode[2] = {false, true};
2769 for (size_t i = 0; i < 2; i++)
2770 {
2771 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
2772 completion_mode[i], EXPECT (expected));
2773 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
2774 completion_mode[i], EXPECT (expected));
2775
2776 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
2777 completion_mode[i], EXPECT (expected));
2778 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
2779 completion_mode[i], EXPECT (expected));
2780 }
2781 }
2782
2783 {
2784 /* Check method qualifiers are ignored. */
2785 static const char expected[] = "ns::foo<char*>";
2786 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
2787 symbol_name_match_type::FULL, true, EXPECT (expected));
2788 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
2789 symbol_name_match_type::FULL, true, EXPECT (expected));
2790 CHECK_MATCH ("foo < char * > ( int ) const",
2791 symbol_name_match_type::WILD, true, EXPECT (expected));
2792 CHECK_MATCH ("foo < char * > ( int ) &&",
2793 symbol_name_match_type::WILD, true, EXPECT (expected));
2794 }
2795
2796 /* Test lookup names that don't match anything. */
2797 {
2798 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
2799 {});
2800
2801 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
2802 {});
2803 }
2804
2805 /* Some wild matching tests, exercising "(anonymous namespace)",
2806 which should not be confused with a parameter list. */
2807 {
2808 static const char *syms[] = {
2809 "A::B::C",
2810 "B::C",
2811 "C",
2812 "A :: B :: C ( int )",
2813 "B :: C ( int )",
2814 "C ( int )",
2815 };
2816
2817 for (const char *s : syms)
2818 {
2819 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
2820 EXPECT ("(anonymous namespace)::A::B::C"));
2821 }
2822 }
2823
2824 {
2825 static const char expected[] = "ns2::tmpl<int>::foo2";
2826 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
2827 EXPECT (expected));
2828 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
2829 EXPECT (expected));
2830 }
2831
2832 SELF_CHECK (!any_mismatch);
2833
2834 #undef EXPECT
2835 #undef CHECK_MATCH
2836 }
2837
2838 static void
2839 run_test ()
2840 {
2841 test_mapped_index_find_name_component_bounds ();
2842 test_dw2_expand_symtabs_matching_symbol ();
2843 }
2844
2845 }} // namespace selftests::dw2_expand_symtabs_matching
2846
2847 #endif /* GDB_SELF_TEST */
2848
2849 /* See read.h. */
2850
2851 bool
2852 dw2_expand_symtabs_matching_one
2853 (dwarf2_per_cu_data *per_cu,
2854 dwarf2_per_objfile *per_objfile,
2855 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2856 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
2857 {
2858 if (file_matcher == NULL || per_cu->mark)
2859 {
2860 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
2861
2862 compunit_symtab *symtab
2863 = dw2_instantiate_symtab (per_cu, per_objfile, false);
2864 gdb_assert (symtab != nullptr);
2865
2866 if (expansion_notify != NULL && symtab_was_null)
2867 return expansion_notify (symtab);
2868 }
2869 return true;
2870 }
2871
2872 /* See read.h. */
2873
2874 void
2875 dw_expand_symtabs_matching_file_matcher
2876 (dwarf2_per_objfile *per_objfile,
2877 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
2878 {
2879 if (file_matcher == NULL)
2880 return;
2881
2882 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
2883 htab_eq_pointer,
2884 NULL, xcalloc, xfree));
2885 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
2886 htab_eq_pointer,
2887 NULL, xcalloc, xfree));
2888
2889 /* The rule is CUs specify all the files, including those used by
2890 any TU, so there's no need to scan TUs here. */
2891
2892 for (const auto &per_cu : per_objfile->per_bfd->all_units)
2893 {
2894 QUIT;
2895
2896 if (per_cu->is_debug_types)
2897 continue;
2898 per_cu->mark = 0;
2899
2900 /* We only need to look at symtabs not already expanded. */
2901 if (per_objfile->symtab_set_p (per_cu.get ()))
2902 continue;
2903
2904 if (per_cu->fnd != nullptr)
2905 {
2906 file_and_directory *fnd = per_cu->fnd.get ();
2907
2908 if (file_matcher (fnd->get_name (), false))
2909 {
2910 per_cu->mark = 1;
2911 continue;
2912 }
2913
2914 /* Before we invoke realpath, which can get expensive when many
2915 files are involved, do a quick comparison of the basenames. */
2916 if ((basenames_may_differ
2917 || file_matcher (lbasename (fnd->get_name ()), true))
2918 && file_matcher (fnd->get_fullname (), false))
2919 {
2920 per_cu->mark = 1;
2921 continue;
2922 }
2923 }
2924
2925 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
2926 per_objfile);
2927 if (file_data == NULL)
2928 continue;
2929
2930 if (htab_find (visited_not_found.get (), file_data) != NULL)
2931 continue;
2932 else if (htab_find (visited_found.get (), file_data) != NULL)
2933 {
2934 per_cu->mark = 1;
2935 continue;
2936 }
2937
2938 for (int j = 0; j < file_data->num_file_names; ++j)
2939 {
2940 const char *this_real_name;
2941
2942 if (file_matcher (file_data->file_names[j], false))
2943 {
2944 per_cu->mark = 1;
2945 break;
2946 }
2947
2948 /* Before we invoke realpath, which can get expensive when many
2949 files are involved, do a quick comparison of the basenames. */
2950 if (!basenames_may_differ
2951 && !file_matcher (lbasename (file_data->file_names[j]),
2952 true))
2953 continue;
2954
2955 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
2956 if (file_matcher (this_real_name, false))
2957 {
2958 per_cu->mark = 1;
2959 break;
2960 }
2961 }
2962
2963 void **slot = htab_find_slot (per_cu->mark
2964 ? visited_found.get ()
2965 : visited_not_found.get (),
2966 file_data, INSERT);
2967 *slot = file_data;
2968 }
2969 }
2970
2971
2972 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
2973 symtab. */
2974
2975 static struct compunit_symtab *
2976 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
2977 CORE_ADDR pc)
2978 {
2979 int i;
2980
2981 if (cust->blockvector () != nullptr
2982 && blockvector_contains_pc (cust->blockvector (), pc))
2983 return cust;
2984
2985 if (cust->includes == NULL)
2986 return NULL;
2987
2988 for (i = 0; cust->includes[i]; ++i)
2989 {
2990 struct compunit_symtab *s = cust->includes[i];
2991
2992 s = recursively_find_pc_sect_compunit_symtab (s, pc);
2993 if (s != NULL)
2994 return s;
2995 }
2996
2997 return NULL;
2998 }
2999
3000 dwarf2_per_cu_data *
3001 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
3002 unrelocated_addr adjusted_pc)
3003 {
3004 if (per_bfd->index_addrmap == nullptr)
3005 return nullptr;
3006
3007 void *obj = per_bfd->index_addrmap->find ((CORE_ADDR) adjusted_pc);
3008 return static_cast<dwarf2_per_cu_data *> (obj);
3009 }
3010
3011 struct compunit_symtab *
3012 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
3013 (struct objfile *objfile,
3014 struct bound_minimal_symbol msymbol,
3015 CORE_ADDR pc,
3016 struct obj_section *section,
3017 int warn_if_readin)
3018 {
3019 struct compunit_symtab *result;
3020
3021 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3022
3023 CORE_ADDR baseaddr = objfile->text_section_offset ();
3024 struct dwarf2_per_cu_data *data
3025 = find_per_cu (per_objfile->per_bfd, (unrelocated_addr) (pc - baseaddr));
3026 if (data == nullptr)
3027 return nullptr;
3028
3029 if (warn_if_readin && per_objfile->symtab_set_p (data))
3030 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3031 paddress (objfile->arch (), pc));
3032
3033 result = recursively_find_pc_sect_compunit_symtab
3034 (dw2_instantiate_symtab (data, per_objfile, false), pc);
3035
3036 if (warn_if_readin && result == nullptr)
3037 warning (_("(Error: pc %s in address map, but not in symtab.)"),
3038 paddress (objfile->arch (), pc));
3039
3040 return result;
3041 }
3042
3043 void
3044 dwarf2_base_index_functions::map_symbol_filenames
3045 (struct objfile *objfile,
3046 gdb::function_view<symbol_filename_ftype> fun,
3047 bool need_fullname)
3048 {
3049 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3050
3051 /* Use caches to ensure we only call FUN once for each filename. */
3052 filename_seen_cache filenames_cache;
3053 std::unordered_set<quick_file_names *> qfn_cache;
3054
3055 /* The rule is CUs specify all the files, including those used by any TU,
3056 so there's no need to scan TUs here. We can ignore file names coming
3057 from already-expanded CUs. It is possible that an expanded CU might
3058 reuse the file names data from a currently unexpanded CU, in this
3059 case we don't want to report the files from the unexpanded CU. */
3060
3061 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3062 {
3063 if (!per_cu->is_debug_types
3064 && per_objfile->symtab_set_p (per_cu.get ()))
3065 {
3066 if (per_cu->file_names != nullptr)
3067 qfn_cache.insert (per_cu->file_names);
3068 }
3069 }
3070
3071 for (dwarf2_per_cu_data *per_cu
3072 : all_units_range (per_objfile->per_bfd))
3073 {
3074 /* We only need to look at symtabs not already expanded. */
3075 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
3076 continue;
3077
3078 if (per_cu->fnd != nullptr)
3079 {
3080 file_and_directory *fnd = per_cu->fnd.get ();
3081
3082 const char *filename = fnd->get_name ();
3083 const char *key = filename;
3084 const char *fullname = nullptr;
3085
3086 if (need_fullname)
3087 {
3088 fullname = fnd->get_fullname ();
3089 key = fullname;
3090 }
3091
3092 if (!filenames_cache.seen (key))
3093 fun (filename, fullname);
3094 }
3095
3096 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3097 if (file_data == nullptr
3098 || qfn_cache.find (file_data) != qfn_cache.end ())
3099 continue;
3100
3101 for (int j = 0; j < file_data->num_file_names; ++j)
3102 {
3103 const char *filename = file_data->file_names[j];
3104 const char *key = filename;
3105 const char *fullname = nullptr;
3106
3107 if (need_fullname)
3108 {
3109 fullname = dw2_get_real_path (per_objfile, file_data, j);
3110 key = fullname;
3111 }
3112
3113 if (!filenames_cache.seen (key))
3114 fun (filename, fullname);
3115 }
3116 }
3117 }
3118
3119 bool
3120 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
3121 {
3122 return true;
3123 }
3124
3125 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
3126
3127 bool
3128 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
3129 {
3130 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3131
3132 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3133 {
3134 /* Is this already expanded? */
3135 if (per_objfile->symtab_set_p (per_cu.get ()))
3136 continue;
3137
3138 /* It has not yet been expanded. */
3139 return true;
3140 }
3141
3142 return false;
3143 }
3144
3145 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
3146 to either a dwarf2_per_bfd or dwz_file object. */
3147
3148 template <typename T>
3149 static gdb::array_view<const gdb_byte>
3150 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
3151 {
3152 dwarf2_section_info *section = &section_owner->gdb_index;
3153
3154 if (section->empty ())
3155 return {};
3156
3157 /* Older elfutils strip versions could keep the section in the main
3158 executable while splitting it for the separate debug info file. */
3159 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
3160 return {};
3161
3162 section->read (obj);
3163
3164 /* dwarf2_section_info::size is a bfd_size_type, while
3165 gdb::array_view works with size_t. On 32-bit hosts, with
3166 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
3167 is 32-bit. So we need an explicit narrowing conversion here.
3168 This is fine, because it's impossible to allocate or mmap an
3169 array/buffer larger than what size_t can represent. */
3170 return gdb::make_array_view (section->buffer, section->size);
3171 }
3172
3173 /* Lookup the index cache for the contents of the index associated to
3174 DWARF2_OBJ. */
3175
3176 static gdb::array_view<const gdb_byte>
3177 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
3178 {
3179 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
3180 if (build_id == nullptr)
3181 return {};
3182
3183 return global_index_cache.lookup_gdb_index (build_id,
3184 &dwarf2_per_bfd->index_cache_res);
3185 }
3186
3187 /* Same as the above, but for DWZ. */
3188
3189 static gdb::array_view<const gdb_byte>
3190 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
3191 {
3192 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
3193 if (build_id == nullptr)
3194 return {};
3195
3196 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
3197 }
3198
3199 static void start_debug_info_reader (dwarf2_per_objfile *);
3200
3201 /* See dwarf2/public.h. */
3202
3203 bool
3204 dwarf2_initialize_objfile (struct objfile *objfile,
3205 const struct dwarf2_debug_sections *names,
3206 bool can_copy)
3207 {
3208 if (!dwarf2_has_info (objfile, names, can_copy))
3209 return false;
3210
3211 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3212 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3213
3214 dwarf_read_debug_printf ("called");
3215
3216 /* If we're about to read full symbols, don't bother with the
3217 indices. In this case we also don't care if some other debug
3218 format is making psymtabs, because they are all about to be
3219 expanded anyway. */
3220 if ((objfile->flags & OBJF_READNOW))
3221 {
3222 dwarf_read_debug_printf ("readnow requested");
3223
3224 create_all_units (per_objfile);
3225 per_bfd->quick_file_names_table
3226 = create_quick_file_names_table (per_bfd->all_units.size ());
3227
3228 objfile->qf.emplace_front (new readnow_functions);
3229 }
3230 /* Was a GDB index already read when we processed an objfile sharing
3231 PER_BFD? */
3232 else if (per_bfd->index_table != nullptr)
3233 dwarf_read_debug_printf ("re-using symbols");
3234 else if (dwarf2_read_debug_names (per_objfile))
3235 dwarf_read_debug_printf ("found debug names");
3236 else if (dwarf2_read_gdb_index (per_objfile,
3237 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
3238 get_gdb_index_contents_from_section<dwz_file>))
3239 dwarf_read_debug_printf ("found gdb index from file");
3240 /* ... otherwise, try to find the index in the index cache. */
3241 else if (dwarf2_read_gdb_index (per_objfile,
3242 get_gdb_index_contents_from_cache,
3243 get_gdb_index_contents_from_cache_dwz))
3244 {
3245 dwarf_read_debug_printf ("found gdb index from cache");
3246 global_index_cache.hit ();
3247 }
3248 else
3249 {
3250 global_index_cache.miss ();
3251 start_debug_info_reader (per_objfile);
3252 }
3253
3254 if (per_bfd->index_table != nullptr)
3255 {
3256 if (dwarf_synchronous)
3257 per_bfd->index_table->wait_completely ();
3258 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3259 }
3260
3261 return true;
3262 }
3263
3264 \f
3265
3266 /* Find the base address of the compilation unit for range lists and
3267 location lists. It will normally be specified by DW_AT_low_pc.
3268 In DWARF-3 draft 4, the base address could be overridden by
3269 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3270 compilation units with discontinuous ranges. */
3271
3272 static void
3273 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3274 {
3275 struct attribute *attr;
3276
3277 cu->base_address.reset ();
3278
3279 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3280 if (attr != nullptr)
3281 cu->base_address = attr->as_address ();
3282 else
3283 {
3284 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3285 if (attr != nullptr)
3286 cu->base_address = attr->as_address ();
3287 }
3288 }
3289
3290 /* Helper function that returns the proper abbrev section for
3291 THIS_CU. */
3292
3293 static struct dwarf2_section_info *
3294 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3295 {
3296 struct dwarf2_section_info *abbrev;
3297 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
3298
3299 if (this_cu->is_dwz)
3300 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
3301 else
3302 abbrev = &per_bfd->abbrev;
3303
3304 return abbrev;
3305 }
3306
3307 /* Fetch the abbreviation table offset from a comp or type unit header. */
3308
3309 static sect_offset
3310 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
3311 struct dwarf2_section_info *section,
3312 sect_offset sect_off)
3313 {
3314 bfd *abfd = section->get_bfd_owner ();
3315 const gdb_byte *info_ptr;
3316 unsigned int initial_length_size, offset_size;
3317 uint16_t version;
3318
3319 section->read (per_objfile->objfile);
3320 info_ptr = section->buffer + to_underlying (sect_off);
3321 read_initial_length (abfd, info_ptr, &initial_length_size);
3322 offset_size = initial_length_size == 4 ? 4 : 8;
3323 info_ptr += initial_length_size;
3324
3325 version = read_2_bytes (abfd, info_ptr);
3326 info_ptr += 2;
3327 if (version >= 5)
3328 {
3329 /* Skip unit type and address size. */
3330 info_ptr += 2;
3331 }
3332
3333 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
3334 }
3335
3336 static hashval_t
3337 hash_signatured_type (const void *item)
3338 {
3339 const struct signatured_type *sig_type
3340 = (const struct signatured_type *) item;
3341
3342 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3343 return sig_type->signature;
3344 }
3345
3346 static int
3347 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3348 {
3349 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
3350 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
3351
3352 return lhs->signature == rhs->signature;
3353 }
3354
3355 /* See read.h. */
3356
3357 htab_up
3358 allocate_signatured_type_table ()
3359 {
3360 return htab_up (htab_create_alloc (41,
3361 hash_signatured_type,
3362 eq_signatured_type,
3363 NULL, xcalloc, xfree));
3364 }
3365
3366 /* A helper for create_debug_types_hash_table. Read types from SECTION
3367 and fill them into TYPES_HTAB. It will process only type units,
3368 therefore DW_UT_type. */
3369
3370 static void
3371 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
3372 struct dwo_file *dwo_file,
3373 dwarf2_section_info *section, htab_up &types_htab,
3374 rcuh_kind section_kind)
3375 {
3376 struct objfile *objfile = per_objfile->objfile;
3377 struct dwarf2_section_info *abbrev_section;
3378 bfd *abfd;
3379 const gdb_byte *info_ptr, *end_ptr;
3380
3381 abbrev_section = &dwo_file->sections.abbrev;
3382
3383 dwarf_read_debug_printf ("Reading %s for %s",
3384 section->get_name (),
3385 abbrev_section->get_file_name ());
3386
3387 section->read (objfile);
3388 info_ptr = section->buffer;
3389
3390 if (info_ptr == NULL)
3391 return;
3392
3393 /* We can't set abfd until now because the section may be empty or
3394 not present, in which case the bfd is unknown. */
3395 abfd = section->get_bfd_owner ();
3396
3397 /* We don't use cutu_reader here because we don't need to read
3398 any dies: the signature is in the header. */
3399
3400 end_ptr = info_ptr + section->size;
3401 while (info_ptr < end_ptr)
3402 {
3403 signatured_type_up sig_type;
3404 struct dwo_unit *dwo_tu;
3405 void **slot;
3406 const gdb_byte *ptr = info_ptr;
3407 struct comp_unit_head header;
3408 unsigned int length;
3409
3410 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
3411
3412 /* Initialize it due to a false compiler warning. */
3413 header.signature = -1;
3414 header.type_cu_offset_in_tu = (cu_offset) -1;
3415
3416 /* We need to read the type's signature in order to build the hash
3417 table, but we don't need anything else just yet. */
3418
3419 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
3420 abbrev_section, ptr, section_kind);
3421
3422 length = header.get_length_with_initial ();
3423
3424 /* Skip dummy type units. */
3425 if (ptr >= info_ptr + length
3426 || peek_abbrev_code (abfd, ptr) == 0
3427 || (header.unit_type != DW_UT_type
3428 && header.unit_type != DW_UT_split_type))
3429 {
3430 info_ptr += length;
3431 continue;
3432 }
3433
3434 if (types_htab == NULL)
3435 types_htab = allocate_dwo_unit_table ();
3436
3437 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
3438 dwo_tu->dwo_file = dwo_file;
3439 dwo_tu->signature = header.signature;
3440 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
3441 dwo_tu->section = section;
3442 dwo_tu->sect_off = sect_off;
3443 dwo_tu->length = length;
3444
3445 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
3446 gdb_assert (slot != NULL);
3447 if (*slot != NULL)
3448 complaint (_("debug type entry at offset %s is duplicate to"
3449 " the entry at offset %s, signature %s"),
3450 sect_offset_str (sect_off),
3451 sect_offset_str (dwo_tu->sect_off),
3452 hex_string (header.signature));
3453 *slot = dwo_tu;
3454
3455 dwarf_read_debug_printf_v (" offset %s, signature %s",
3456 sect_offset_str (sect_off),
3457 hex_string (header.signature));
3458
3459 info_ptr += length;
3460 }
3461 }
3462
3463 /* Create the hash table of all entries in the .debug_types
3464 (or .debug_types.dwo) section(s).
3465 DWO_FILE is a pointer to the DWO file object.
3466
3467 The result is a pointer to the hash table or NULL if there are no types.
3468
3469 Note: This function processes DWO files only, not DWP files. */
3470
3471 static void
3472 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
3473 struct dwo_file *dwo_file,
3474 gdb::array_view<dwarf2_section_info> type_sections,
3475 htab_up &types_htab)
3476 {
3477 for (dwarf2_section_info &section : type_sections)
3478 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
3479 rcuh_kind::TYPE);
3480 }
3481
3482 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
3483 If SLOT is non-NULL, it is the entry to use in the hash table.
3484 Otherwise we find one. */
3485
3486 static struct signatured_type *
3487 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
3488 {
3489 if (per_objfile->per_bfd->all_units.size ()
3490 == per_objfile->per_bfd->all_units.capacity ())
3491 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
3492
3493 signatured_type_up sig_type_holder
3494 = per_objfile->per_bfd->allocate_signatured_type (sig);
3495 signatured_type *sig_type = sig_type_holder.get ();
3496
3497 per_objfile->per_bfd->all_units.emplace_back
3498 (sig_type_holder.release ());
3499
3500 if (slot == NULL)
3501 {
3502 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3503 sig_type, INSERT);
3504 }
3505 gdb_assert (*slot == NULL);
3506 *slot = sig_type;
3507 /* The rest of sig_type must be filled in by the caller. */
3508 return sig_type;
3509 }
3510
3511 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
3512 Fill in SIG_ENTRY with DWO_ENTRY. */
3513
3514 static void
3515 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
3516 struct signatured_type *sig_entry,
3517 struct dwo_unit *dwo_entry)
3518 {
3519 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3520
3521 /* Make sure we're not clobbering something we don't expect to. */
3522 gdb_assert (! sig_entry->queued);
3523 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
3524 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
3525 gdb_assert (sig_entry->signature == dwo_entry->signature);
3526 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
3527 || (to_underlying (sig_entry->type_offset_in_section)
3528 == to_underlying (dwo_entry->type_offset_in_tu)));
3529 gdb_assert (sig_entry->type_unit_group == NULL);
3530 gdb_assert (sig_entry->dwo_unit == NULL
3531 || sig_entry->dwo_unit == dwo_entry);
3532
3533 sig_entry->section = dwo_entry->section;
3534 sig_entry->sect_off = dwo_entry->sect_off;
3535 sig_entry->set_length (dwo_entry->length, false);
3536 sig_entry->reading_dwo_directly = 1;
3537 sig_entry->per_bfd = per_bfd;
3538 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
3539 sig_entry->dwo_unit = dwo_entry;
3540 }
3541
3542 /* Subroutine of lookup_signatured_type.
3543 If we haven't read the TU yet, create the signatured_type data structure
3544 for a TU to be read in directly from a DWO file, bypassing the stub.
3545 This is the "Stay in DWO Optimization": When there is no DWP file and we're
3546 using .gdb_index, then when reading a CU we want to stay in the DWO file
3547 containing that CU. Otherwise we could end up reading several other DWO
3548 files (due to comdat folding) to process the transitive closure of all the
3549 mentioned TUs, and that can be slow. The current DWO file will have every
3550 type signature that it needs.
3551 We only do this for .gdb_index because in the psymtab case we already have
3552 to read all the DWOs to build the type unit groups. */
3553
3554 static struct signatured_type *
3555 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3556 {
3557 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3558 struct dwo_file *dwo_file;
3559 struct dwo_unit find_dwo_entry, *dwo_entry;
3560 void **slot;
3561
3562 gdb_assert (cu->dwo_unit);
3563
3564 /* If TU skeletons have been removed then we may not have read in any
3565 TUs yet. */
3566 if (per_objfile->per_bfd->signatured_types == NULL)
3567 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
3568
3569 /* We only ever need to read in one copy of a signatured type.
3570 Use the global signatured_types array to do our own comdat-folding
3571 of types. If this is the first time we're reading this TU, and
3572 the TU has an entry in .gdb_index, replace the recorded data from
3573 .gdb_index with this TU. */
3574
3575 signatured_type find_sig_entry (sig);
3576 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3577 &find_sig_entry, INSERT);
3578 signatured_type *sig_entry = (struct signatured_type *) *slot;
3579
3580 /* We can get here with the TU already read, *or* in the process of being
3581 read. Don't reassign the global entry to point to this DWO if that's
3582 the case. Also note that if the TU is already being read, it may not
3583 have come from a DWO, the program may be a mix of Fission-compiled
3584 code and non-Fission-compiled code. */
3585
3586 /* Have we already tried to read this TU?
3587 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3588 needn't exist in the global table yet). */
3589 if (sig_entry != NULL && sig_entry->tu_read)
3590 return sig_entry;
3591
3592 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
3593 dwo_unit of the TU itself. */
3594 dwo_file = cu->dwo_unit->dwo_file;
3595
3596 /* Ok, this is the first time we're reading this TU. */
3597 if (dwo_file->tus == NULL)
3598 return NULL;
3599 find_dwo_entry.signature = sig;
3600 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
3601 &find_dwo_entry);
3602 if (dwo_entry == NULL)
3603 return NULL;
3604
3605 /* If the global table doesn't have an entry for this TU, add one. */
3606 if (sig_entry == NULL)
3607 sig_entry = add_type_unit (per_objfile, sig, slot);
3608
3609 if (sig_entry->dwo_unit == nullptr)
3610 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
3611 sig_entry->tu_read = 1;
3612 return sig_entry;
3613 }
3614
3615 /* Subroutine of lookup_signatured_type.
3616 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
3617 then try the DWP file. If the TU stub (skeleton) has been removed then
3618 it won't be in .gdb_index. */
3619
3620 static struct signatured_type *
3621 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3622 {
3623 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3624 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
3625 struct dwo_unit *dwo_entry;
3626 void **slot;
3627
3628 gdb_assert (cu->dwo_unit);
3629 gdb_assert (dwp_file != NULL);
3630
3631 /* If TU skeletons have been removed then we may not have read in any
3632 TUs yet. */
3633 if (per_objfile->per_bfd->signatured_types == NULL)
3634 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
3635
3636 signatured_type find_sig_entry (sig);
3637 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3638 &find_sig_entry, INSERT);
3639 signatured_type *sig_entry = (struct signatured_type *) *slot;
3640
3641 /* Have we already tried to read this TU?
3642 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3643 needn't exist in the global table yet). */
3644 if (sig_entry != NULL)
3645 return sig_entry;
3646
3647 if (dwp_file->tus == NULL)
3648 return NULL;
3649 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
3650 1 /* is_debug_types */);
3651 if (dwo_entry == NULL)
3652 return NULL;
3653
3654 sig_entry = add_type_unit (per_objfile, sig, slot);
3655 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
3656
3657 return sig_entry;
3658 }
3659
3660 /* Lookup a signature based type for DW_FORM_ref_sig8.
3661 Returns NULL if signature SIG is not present in the table.
3662 It is up to the caller to complain about this. */
3663
3664 static struct signatured_type *
3665 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3666 {
3667 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3668
3669 if (cu->dwo_unit)
3670 {
3671 /* We're in a DWO/DWP file, and we're using .gdb_index.
3672 These cases require special processing. */
3673 if (get_dwp_file (per_objfile) == NULL)
3674 return lookup_dwo_signatured_type (cu, sig);
3675 else
3676 return lookup_dwp_signatured_type (cu, sig);
3677 }
3678 else
3679 {
3680 if (per_objfile->per_bfd->signatured_types == NULL)
3681 return NULL;
3682 signatured_type find_entry (sig);
3683 return ((struct signatured_type *)
3684 htab_find (per_objfile->per_bfd->signatured_types.get (),
3685 &find_entry));
3686 }
3687 }
3688
3689 /* Low level DIE reading support. */
3690
3691 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3692
3693 static void
3694 init_cu_die_reader (struct die_reader_specs *reader,
3695 struct dwarf2_cu *cu,
3696 struct dwarf2_section_info *section,
3697 struct dwo_file *dwo_file,
3698 struct abbrev_table *abbrev_table)
3699 {
3700 gdb_assert (section->readin && section->buffer != NULL);
3701 reader->abfd = section->get_bfd_owner ();
3702 reader->cu = cu;
3703 reader->dwo_file = dwo_file;
3704 reader->die_section = section;
3705 reader->buffer = section->buffer;
3706 reader->buffer_end = section->buffer + section->size;
3707 reader->abbrev_table = abbrev_table;
3708 }
3709
3710 /* Subroutine of cutu_reader to simplify it.
3711 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
3712 There's just a lot of work to do, and cutu_reader is big enough
3713 already.
3714
3715 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
3716 from it to the DIE in the DWO. If NULL we are skipping the stub.
3717 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
3718 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
3719 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
3720 STUB_COMP_DIR may be non-NULL.
3721 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
3722 are filled in with the info of the DIE from the DWO file.
3723 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
3724 from the dwo. Since *RESULT_READER references this abbrev table, it must be
3725 kept around for at least as long as *RESULT_READER.
3726
3727 The result is non-zero if a valid (non-dummy) DIE was found. */
3728
3729 static int
3730 read_cutu_die_from_dwo (dwarf2_cu *cu,
3731 struct dwo_unit *dwo_unit,
3732 struct die_info *stub_comp_unit_die,
3733 const char *stub_comp_dir,
3734 struct die_reader_specs *result_reader,
3735 const gdb_byte **result_info_ptr,
3736 struct die_info **result_comp_unit_die,
3737 abbrev_table_up *result_dwo_abbrev_table)
3738 {
3739 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3740 dwarf2_per_cu_data *per_cu = cu->per_cu;
3741 struct objfile *objfile = per_objfile->objfile;
3742 bfd *abfd;
3743 const gdb_byte *begin_info_ptr, *info_ptr;
3744 struct dwarf2_section_info *dwo_abbrev_section;
3745
3746 /* At most one of these may be provided. */
3747 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
3748
3749 /* These attributes aren't processed until later: DW_AT_stmt_list,
3750 DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_comp_dir.
3751 However, these attributes are found in the stub which we won't
3752 have later. In order to not impose this complication on the rest
3753 of the code, we read them here and copy them to the DWO CU/TU
3754 die. */
3755
3756 /* We store them all in an array. */
3757 struct attribute *attributes[5] {};
3758 /* Next available element of the attributes array. */
3759 int next_attr_idx = 0;
3760
3761 /* Push an element into ATTRIBUTES. */
3762 auto push_back = [&] (struct attribute *attr)
3763 {
3764 gdb_assert (next_attr_idx < ARRAY_SIZE (attributes));
3765 if (attr != nullptr)
3766 attributes[next_attr_idx++] = attr;
3767 };
3768
3769 if (stub_comp_unit_die != NULL)
3770 {
3771 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3772 DWO file. */
3773 if (!per_cu->is_debug_types)
3774 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu));
3775 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu));
3776 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu));
3777 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu));
3778 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu));
3779
3780 cu->addr_base = stub_comp_unit_die->addr_base ();
3781
3782 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
3783 We need the value before we can process DW_AT_ranges values from the
3784 DWO. */
3785 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
3786
3787 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
3788 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
3789 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
3790 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
3791 section. */
3792 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
3793 }
3794 else if (stub_comp_dir != NULL)
3795 {
3796 /* Reconstruct the comp_dir attribute to simplify the code below. */
3797 struct attribute *comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack,
3798 struct attribute);
3799 comp_dir->name = DW_AT_comp_dir;
3800 comp_dir->form = DW_FORM_string;
3801 comp_dir->set_string_noncanonical (stub_comp_dir);
3802 push_back (comp_dir);
3803 }
3804
3805 /* Set up for reading the DWO CU/TU. */
3806 cu->dwo_unit = dwo_unit;
3807 dwarf2_section_info *section = dwo_unit->section;
3808 section->read (objfile);
3809 abfd = section->get_bfd_owner ();
3810 begin_info_ptr = info_ptr = (section->buffer
3811 + to_underlying (dwo_unit->sect_off));
3812 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3813
3814 if (per_cu->is_debug_types)
3815 {
3816 signatured_type *sig_type = (struct signatured_type *) per_cu;
3817
3818 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3819 section, dwo_abbrev_section,
3820 info_ptr, rcuh_kind::TYPE);
3821 /* This is not an assert because it can be caused by bad debug info. */
3822 if (sig_type->signature != cu->header.signature)
3823 {
3824 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
3825 " TU at offset %s [in module %s]"),
3826 hex_string (sig_type->signature),
3827 hex_string (cu->header.signature),
3828 sect_offset_str (dwo_unit->sect_off),
3829 bfd_get_filename (abfd));
3830 }
3831 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
3832 /* For DWOs coming from DWP files, we don't know the CU length
3833 nor the type's offset in the TU until now. */
3834 dwo_unit->length = cu->header.get_length_with_initial ();
3835 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
3836
3837 /* Establish the type offset that can be used to lookup the type.
3838 For DWO files, we don't know it until now. */
3839 sig_type->type_offset_in_section
3840 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
3841 }
3842 else
3843 {
3844 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3845 section, dwo_abbrev_section,
3846 info_ptr, rcuh_kind::COMPILE);
3847 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
3848 /* For DWOs coming from DWP files, we don't know the CU length
3849 until now. */
3850 dwo_unit->length = cu->header.get_length_with_initial ();
3851 }
3852
3853 dwo_abbrev_section->read (objfile);
3854 *result_dwo_abbrev_table
3855 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
3856 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
3857 result_dwo_abbrev_table->get ());
3858
3859 /* Read in the die, filling in the attributes from the stub. This
3860 has the benefit of simplifying the rest of the code - all the
3861 work to maintain the illusion of a single
3862 DW_TAG_{compile,type}_unit DIE is done here. */
3863 info_ptr = read_toplevel_die (result_reader, result_comp_unit_die, info_ptr,
3864 gdb::make_array_view (attributes,
3865 next_attr_idx));
3866
3867 /* Skip dummy compilation units. */
3868 if (info_ptr >= begin_info_ptr + dwo_unit->length
3869 || peek_abbrev_code (abfd, info_ptr) == 0)
3870 return 0;
3871
3872 *result_info_ptr = info_ptr;
3873 return 1;
3874 }
3875
3876 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
3877 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
3878 signature is part of the header. */
3879 static std::optional<ULONGEST>
3880 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
3881 {
3882 if (cu->header.version >= 5)
3883 return cu->header.signature;
3884 struct attribute *attr;
3885 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3886 if (attr == nullptr || !attr->form_is_unsigned ())
3887 return std::optional<ULONGEST> ();
3888 return attr->as_unsigned ();
3889 }
3890
3891 /* Subroutine of cutu_reader to simplify it.
3892 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
3893 Returns NULL if the specified DWO unit cannot be found. */
3894
3895 static struct dwo_unit *
3896 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
3897 {
3898 #if CXX_STD_THREAD
3899 /* We need a lock here to handle the DWO hash table. */
3900 static std::mutex dwo_lock;
3901
3902 std::lock_guard<std::mutex> guard (dwo_lock);
3903 #endif
3904
3905 dwarf2_per_cu_data *per_cu = cu->per_cu;
3906 struct dwo_unit *dwo_unit;
3907 const char *comp_dir;
3908
3909 gdb_assert (cu != NULL);
3910
3911 /* Yeah, we look dwo_name up again, but it simplifies the code. */
3912 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
3913 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
3914
3915 if (per_cu->is_debug_types)
3916 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
3917 else
3918 {
3919 std::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
3920
3921 if (!signature.has_value ())
3922 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
3923 " [in module %s]"),
3924 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
3925
3926 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
3927 }
3928
3929 return dwo_unit;
3930 }
3931
3932 /* Subroutine of cutu_reader to simplify it.
3933 See it for a description of the parameters.
3934 Read a TU directly from a DWO file, bypassing the stub. */
3935
3936 void
3937 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
3938 dwarf2_per_objfile *per_objfile,
3939 dwarf2_cu *existing_cu)
3940 {
3941 struct signatured_type *sig_type;
3942
3943 /* Verify we can do the following downcast, and that we have the
3944 data we need. */
3945 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
3946 sig_type = (struct signatured_type *) this_cu;
3947 gdb_assert (sig_type->dwo_unit != NULL);
3948
3949 dwarf2_cu *cu;
3950
3951 if (existing_cu != nullptr)
3952 {
3953 cu = existing_cu;
3954 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
3955 /* There's no need to do the rereading_dwo_cu handling that
3956 cutu_reader does since we don't read the stub. */
3957 }
3958 else
3959 {
3960 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
3961 in per_objfile yet. */
3962 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
3963 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
3964 cu = m_new_cu.get ();
3965 }
3966
3967 /* A future optimization, if needed, would be to use an existing
3968 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
3969 could share abbrev tables. */
3970
3971 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
3972 NULL /* stub_comp_unit_die */,
3973 sig_type->dwo_unit->dwo_file->comp_dir,
3974 this, &info_ptr,
3975 &comp_unit_die,
3976 &m_dwo_abbrev_table) == 0)
3977 {
3978 /* Dummy die. */
3979 dummy_p = true;
3980 }
3981 }
3982
3983 /* Initialize a CU (or TU) and read its DIEs.
3984 If the CU defers to a DWO file, read the DWO file as well.
3985
3986 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
3987 Otherwise the table specified in the comp unit header is read in and used.
3988 This is an optimization for when we already have the abbrev table.
3989
3990 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
3991 allocated. */
3992
3993 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
3994 dwarf2_per_objfile *per_objfile,
3995 struct abbrev_table *abbrev_table,
3996 dwarf2_cu *existing_cu,
3997 bool skip_partial,
3998 abbrev_cache *cache)
3999 : die_reader_specs {},
4000 m_this_cu (this_cu)
4001 {
4002 struct objfile *objfile = per_objfile->objfile;
4003 struct dwarf2_section_info *section = this_cu->section;
4004 bfd *abfd = section->get_bfd_owner ();
4005 const gdb_byte *begin_info_ptr;
4006 struct signatured_type *sig_type = NULL;
4007 struct dwarf2_section_info *abbrev_section;
4008 /* Non-zero if CU currently points to a DWO file and we need to
4009 reread it. When this happens we need to reread the skeleton die
4010 before we can reread the DWO file (this only applies to CUs, not TUs). */
4011 int rereading_dwo_cu = 0;
4012
4013 if (dwarf_die_debug)
4014 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
4015 this_cu->is_debug_types ? "type" : "comp",
4016 sect_offset_str (this_cu->sect_off));
4017
4018 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4019 file (instead of going through the stub), short-circuit all of this. */
4020 if (this_cu->reading_dwo_directly)
4021 {
4022 /* Narrow down the scope of possibilities to have to understand. */
4023 gdb_assert (this_cu->is_debug_types);
4024 gdb_assert (abbrev_table == NULL);
4025 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
4026 return;
4027 }
4028
4029 /* This is cheap if the section is already read in. */
4030 section->read (objfile);
4031
4032 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4033
4034 abbrev_section = get_abbrev_section_for_cu (this_cu);
4035
4036 dwarf2_cu *cu;
4037
4038 if (existing_cu != nullptr)
4039 {
4040 cu = existing_cu;
4041 /* If this CU is from a DWO file we need to start over, we need to
4042 refetch the attributes from the skeleton CU.
4043 This could be optimized by retrieving those attributes from when we
4044 were here the first time: the previous comp_unit_die was stored in
4045 comp_unit_obstack. But there's no data yet that we need this
4046 optimization. */
4047 if (cu->dwo_unit != NULL)
4048 rereading_dwo_cu = 1;
4049 }
4050 else
4051 {
4052 /* If an existing_cu is provided, a dwarf2_cu must not exist for
4053 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
4054 let us know that the CU is being scanned using the parallel
4055 indexer. This assert is avoided in this case because (1) it
4056 is irrelevant, and (2) the get_cu method is not
4057 thread-safe. */
4058 gdb_assert (cache != nullptr
4059 || per_objfile->get_cu (this_cu) == nullptr);
4060 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4061 cu = m_new_cu.get ();
4062 }
4063
4064 /* Get the header. */
4065 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
4066 {
4067 /* We already have the header, there's no need to read it in again. */
4068 info_ptr += to_underlying (cu->header.first_die_cu_offset);
4069 }
4070 else
4071 {
4072 if (this_cu->is_debug_types)
4073 {
4074 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4075 section, abbrev_section,
4076 info_ptr, rcuh_kind::TYPE);
4077
4078 /* Since per_cu is the first member of struct signatured_type,
4079 we can go from a pointer to one to a pointer to the other. */
4080 sig_type = (struct signatured_type *) this_cu;
4081 gdb_assert (sig_type->signature == cu->header.signature);
4082 gdb_assert (sig_type->type_offset_in_tu
4083 == cu->header.type_cu_offset_in_tu);
4084 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4085
4086 /* LENGTH has not been set yet for type units if we're
4087 using .gdb_index. */
4088 this_cu->set_length (cu->header.get_length_with_initial ());
4089
4090 /* Establish the type offset that can be used to lookup the type. */
4091 sig_type->type_offset_in_section =
4092 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
4093
4094 this_cu->set_version (cu->header.version);
4095 }
4096 else
4097 {
4098 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4099 section, abbrev_section,
4100 info_ptr,
4101 rcuh_kind::COMPILE);
4102
4103 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4104 this_cu->set_length (cu->header.get_length_with_initial ());
4105 this_cu->set_version (cu->header.version);
4106 }
4107 }
4108
4109 /* Skip dummy compilation units. */
4110 if (info_ptr >= begin_info_ptr + this_cu->length ()
4111 || peek_abbrev_code (abfd, info_ptr) == 0)
4112 {
4113 dummy_p = true;
4114 return;
4115 }
4116
4117 /* If we don't have them yet, read the abbrevs for this compilation unit.
4118 And if we need to read them now, make sure they're freed when we're
4119 done. */
4120 if (abbrev_table != NULL)
4121 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
4122 else
4123 {
4124 if (cache != nullptr)
4125 abbrev_table = cache->find (abbrev_section,
4126 cu->header.abbrev_sect_off);
4127 if (abbrev_table == nullptr)
4128 {
4129 abbrev_section->read (objfile);
4130 m_abbrev_table_holder
4131 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
4132 abbrev_table = m_abbrev_table_holder.get ();
4133 }
4134 }
4135
4136 /* Read the top level CU/TU die. */
4137 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
4138 info_ptr = read_toplevel_die (this, &comp_unit_die, info_ptr);
4139
4140 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
4141 {
4142 dummy_p = true;
4143 return;
4144 }
4145
4146 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4147 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
4148 table from the DWO file and pass the ownership over to us. It will be
4149 referenced from READER, so we must make sure to free it after we're done
4150 with READER.
4151
4152 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4153 DWO CU, that this test will fail (the attribute will not be present). */
4154 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
4155 if (dwo_name != nullptr)
4156 {
4157 struct dwo_unit *dwo_unit;
4158 struct die_info *dwo_comp_unit_die;
4159
4160 if (comp_unit_die->has_children)
4161 {
4162 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
4163 " has children (offset %s) [in module %s]"),
4164 sect_offset_str (this_cu->sect_off),
4165 bfd_get_filename (abfd));
4166 }
4167 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
4168 if (dwo_unit != NULL)
4169 {
4170 if (read_cutu_die_from_dwo (cu, dwo_unit,
4171 comp_unit_die, NULL,
4172 this, &info_ptr,
4173 &dwo_comp_unit_die,
4174 &m_dwo_abbrev_table) == 0)
4175 {
4176 /* Dummy die. */
4177 dummy_p = true;
4178 return;
4179 }
4180 comp_unit_die = dwo_comp_unit_die;
4181 }
4182 else
4183 {
4184 /* Yikes, we couldn't find the rest of the DIE, we only have
4185 the stub. A complaint has already been logged. There's
4186 not much more we can do except pass on the stub DIE to
4187 die_reader_func. We don't want to throw an error on bad
4188 debug info. */
4189 }
4190 }
4191 }
4192
4193 void
4194 cutu_reader::keep ()
4195 {
4196 /* Done, clean up. */
4197 gdb_assert (!dummy_p);
4198 if (m_new_cu != NULL)
4199 {
4200 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
4201 now. */
4202 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
4203 per_objfile->set_cu (m_this_cu, std::move (m_new_cu));
4204 }
4205 }
4206
4207 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
4208 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
4209 assumed to have already done the lookup to find the DWO file).
4210
4211 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4212 THIS_CU->is_debug_types, but nothing else.
4213
4214 We fill in THIS_CU->length.
4215
4216 THIS_CU->cu is always freed when done.
4217 This is done in order to not leave THIS_CU->cu in a state where we have
4218 to care whether it refers to the "main" CU or the DWO CU.
4219
4220 When parent_cu is passed, it is used to provide a default value for
4221 str_offsets_base and addr_base from the parent. */
4222
4223 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
4224 dwarf2_per_objfile *per_objfile,
4225 struct dwarf2_cu *parent_cu,
4226 struct dwo_file *dwo_file)
4227 : die_reader_specs {},
4228 m_this_cu (this_cu)
4229 {
4230 struct objfile *objfile = per_objfile->objfile;
4231 struct dwarf2_section_info *section = this_cu->section;
4232 bfd *abfd = section->get_bfd_owner ();
4233 struct dwarf2_section_info *abbrev_section;
4234 const gdb_byte *begin_info_ptr, *info_ptr;
4235
4236 if (dwarf_die_debug)
4237 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
4238 this_cu->is_debug_types ? "type" : "comp",
4239 sect_offset_str (this_cu->sect_off));
4240
4241 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
4242
4243 abbrev_section = (dwo_file != NULL
4244 ? &dwo_file->sections.abbrev
4245 : get_abbrev_section_for_cu (this_cu));
4246
4247 /* This is cheap if the section is already read in. */
4248 section->read (objfile);
4249
4250 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4251
4252 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4253 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
4254 section, abbrev_section, info_ptr,
4255 (this_cu->is_debug_types
4256 ? rcuh_kind::TYPE
4257 : rcuh_kind::COMPILE));
4258
4259 if (parent_cu != nullptr)
4260 {
4261 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
4262 m_new_cu->addr_base = parent_cu->addr_base;
4263 }
4264 this_cu->set_length (m_new_cu->header.get_length_with_initial ());
4265
4266 /* Skip dummy compilation units. */
4267 if (info_ptr >= begin_info_ptr + this_cu->length ()
4268 || peek_abbrev_code (abfd, info_ptr) == 0)
4269 {
4270 dummy_p = true;
4271 return;
4272 }
4273
4274 abbrev_section->read (objfile);
4275 m_abbrev_table_holder
4276 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
4277
4278 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
4279 m_abbrev_table_holder.get ());
4280 info_ptr = read_toplevel_die (this, &comp_unit_die, info_ptr);
4281 }
4282
4283 \f
4284 /* Type Unit Groups.
4285
4286 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4287 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4288 so that all types coming from the same compilation (.o file) are grouped
4289 together. A future step could be to put the types in the same symtab as
4290 the CU the types ultimately came from. */
4291
4292 static hashval_t
4293 hash_type_unit_group (const void *item)
4294 {
4295 const struct type_unit_group *tu_group
4296 = (const struct type_unit_group *) item;
4297
4298 return hash_stmt_list_entry (&tu_group->hash);
4299 }
4300
4301 static int
4302 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4303 {
4304 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
4305 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
4306
4307 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4308 }
4309
4310 /* Allocate a hash table for type unit groups. */
4311
4312 static htab_up
4313 allocate_type_unit_groups_table ()
4314 {
4315 return htab_up (htab_create_alloc (3,
4316 hash_type_unit_group,
4317 eq_type_unit_group,
4318 htab_delete_entry<type_unit_group>,
4319 xcalloc, xfree));
4320 }
4321
4322 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4323 partial symtabs. We combine several TUs per psymtab to not let the size
4324 of any one psymtab grow too big. */
4325 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4326 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4327
4328 /* Helper routine for get_type_unit_group.
4329 Create the type_unit_group object used to hold one or more TUs. */
4330
4331 static std::unique_ptr<type_unit_group>
4332 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4333 {
4334 auto tu_group = std::make_unique<type_unit_group> ();
4335
4336 tu_group->hash.dwo_unit = cu->dwo_unit;
4337 tu_group->hash.line_sect_off = line_offset_struct;
4338
4339 return tu_group;
4340 }
4341
4342 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4343 STMT_LIST is a DW_AT_stmt_list attribute. */
4344
4345 static struct type_unit_group *
4346 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
4347 {
4348 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4349 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4350 struct type_unit_group *tu_group;
4351 void **slot;
4352 unsigned int line_offset;
4353 struct type_unit_group type_unit_group_for_lookup;
4354
4355 if (per_objfile->per_bfd->type_unit_groups == NULL)
4356 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
4357
4358 /* Do we need to create a new group, or can we use an existing one? */
4359
4360 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
4361 {
4362 line_offset = stmt_list->as_unsigned ();
4363 ++tu_stats->nr_symtab_sharers;
4364 }
4365 else
4366 {
4367 /* Ugh, no stmt_list. Rare, but we have to handle it.
4368 We can do various things here like create one group per TU or
4369 spread them over multiple groups to split up the expansion work.
4370 To avoid worst case scenarios (too many groups or too large groups)
4371 we, umm, group them in bunches. */
4372 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
4373 | (tu_stats->nr_stmt_less_type_units
4374 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
4375 ++tu_stats->nr_stmt_less_type_units;
4376 }
4377
4378 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
4379 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
4380 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
4381 &type_unit_group_for_lookup, INSERT);
4382 if (*slot == nullptr)
4383 {
4384 sect_offset line_offset_struct = (sect_offset) line_offset;
4385 std::unique_ptr<type_unit_group> grp
4386 = create_type_unit_group (cu, line_offset_struct);
4387 *slot = grp.release ();
4388 ++tu_stats->nr_symtabs;
4389 }
4390
4391 tu_group = (struct type_unit_group *) *slot;
4392 gdb_assert (tu_group != nullptr);
4393 return tu_group;
4394 }
4395 \f
4396
4397 cooked_index_storage::cooked_index_storage ()
4398 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
4399 eq_cutu_reader,
4400 htab_delete_entry<cutu_reader>,
4401 xcalloc, xfree)),
4402 m_index (new cooked_index_shard)
4403 {
4404 }
4405
4406 cutu_reader *
4407 cooked_index_storage::get_reader (dwarf2_per_cu_data *per_cu)
4408 {
4409 int index = per_cu->index;
4410 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
4411 &index, index);
4412 }
4413
4414 cutu_reader *
4415 cooked_index_storage::preserve (std::unique_ptr<cutu_reader> reader)
4416 {
4417 m_abbrev_cache.add (reader->release_abbrev_table ());
4418
4419 int index = reader->cu->per_cu->index;
4420 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
4421 index, INSERT);
4422 gdb_assert (*slot == nullptr);
4423 cutu_reader *result = reader.get ();
4424 *slot = reader.release ();
4425 return result;
4426 }
4427
4428 /* Hash function for a cutu_reader. */
4429 hashval_t
4430 cooked_index_storage::hash_cutu_reader (const void *a)
4431 {
4432 const cutu_reader *reader = (const cutu_reader *) a;
4433 return reader->cu->per_cu->index;
4434 }
4435
4436 /* Equality function for cutu_reader. */
4437 int
4438 cooked_index_storage::eq_cutu_reader (const void *a, const void *b)
4439 {
4440 const cutu_reader *ra = (const cutu_reader *) a;
4441 const int *rb = (const int *) b;
4442 return ra->cu->per_cu->index == *rb;
4443 }
4444
4445 /* An instance of this is created to index a CU. */
4446
4447 class cooked_indexer
4448 {
4449 public:
4450
4451 cooked_indexer (cooked_index_storage *storage,
4452 dwarf2_per_cu_data *per_cu,
4453 enum language language)
4454 : m_index_storage (storage),
4455 m_per_cu (per_cu),
4456 m_language (language),
4457 m_die_range_map (storage->get_parent_map ())
4458 {
4459 }
4460
4461 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
4462
4463 /* Index the given CU. */
4464 void make_index (cutu_reader *reader);
4465
4466 private:
4467
4468 /* A helper function to scan the PC bounds of READER and record them
4469 in the storage's addrmap. */
4470 void check_bounds (cutu_reader *reader);
4471
4472 /* Ensure that the indicated CU exists. The cutu_reader for it is
4473 returned. FOR_SCANNING is true if the caller intends to scan all
4474 the DIEs in the CU; when false, this use is assumed to be to look
4475 up just a single DIE. */
4476 cutu_reader *ensure_cu_exists (cutu_reader *reader,
4477 dwarf2_per_objfile *per_objfile,
4478 sect_offset sect_off,
4479 bool is_dwz,
4480 bool for_scanning);
4481
4482 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
4483 the entry for the enclosing scope (nullptr at top level). FULLY
4484 is true when a full scan must be done -- in some languages,
4485 function scopes must be fully explored in order to find nested
4486 functions. This returns a pointer to just after the spot where
4487 reading stopped. */
4488 const gdb_byte *index_dies (cutu_reader *reader,
4489 const gdb_byte *info_ptr,
4490 const cooked_index_entry *parent_entry,
4491 bool fully);
4492
4493 /* Scan the attributes for a given DIE and update the out
4494 parameters. Returns a pointer to the byte after the DIE. */
4495 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
4496 cutu_reader *reader,
4497 const gdb_byte *watermark_ptr,
4498 const gdb_byte *info_ptr,
4499 const abbrev_info *abbrev,
4500 const char **name,
4501 const char **linkage_name,
4502 cooked_index_flag *flags,
4503 sect_offset *sibling_offset,
4504 const cooked_index_entry **parent_entry,
4505 parent_map::addr_type *maybe_defer,
4506 bool *is_enum_class,
4507 bool for_specification);
4508
4509 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
4510 DW_AT_import, and then scanning the referenced CU. Returns a
4511 pointer to the byte after the DIE. */
4512 const gdb_byte *index_imported_unit (cutu_reader *reader,
4513 const gdb_byte *info_ptr,
4514 const abbrev_info *abbrev);
4515
4516 /* Recursively read DIEs, recording the section offsets in
4517 m_die_range_map and then calling index_dies. */
4518 const gdb_byte *recurse (cutu_reader *reader,
4519 const gdb_byte *info_ptr,
4520 const cooked_index_entry *parent_entry,
4521 bool fully);
4522
4523 /* The storage object, where the results are kept. */
4524 cooked_index_storage *m_index_storage;
4525 /* The CU that we are reading on behalf of. This object might be
4526 asked to index one CU but to treat the results as if they come
4527 from some including CU; in this case the including CU would be
4528 recorded here. */
4529 dwarf2_per_cu_data *m_per_cu;
4530 /* The language that we're assuming when reading. */
4531 enum language m_language;
4532
4533 /* Map from DIE ranges to newly-created entries. */
4534 parent_map *m_die_range_map;
4535 };
4536
4537 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4538 Process compilation unit THIS_CU for a psymtab. */
4539
4540 static void
4541 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
4542 dwarf2_per_objfile *per_objfile,
4543 cooked_index_storage *storage)
4544 {
4545 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
4546 storage->get_abbrev_cache ());
4547
4548 if (reader.comp_unit_die == nullptr)
4549 return;
4550
4551 if (reader.dummy_p)
4552 {
4553 /* Nothing. */
4554 }
4555 else if (this_cu->is_debug_types)
4556 build_type_psymtabs_reader (&reader, storage);
4557 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
4558 {
4559 bool nope = false;
4560 if (this_cu->scanned.compare_exchange_strong (nope, true))
4561 {
4562 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
4563 language_minimal);
4564 gdb_assert (storage != nullptr);
4565 cooked_indexer indexer (storage, this_cu, reader.cu->lang ());
4566 indexer.make_index (&reader);
4567 }
4568 }
4569 }
4570
4571 /* Reader function for build_type_psymtabs. */
4572
4573 static void
4574 build_type_psymtabs_reader (cutu_reader *reader,
4575 cooked_index_storage *storage)
4576 {
4577 struct dwarf2_cu *cu = reader->cu;
4578 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4579 struct die_info *type_unit_die = reader->comp_unit_die;
4580
4581 gdb_assert (per_cu->is_debug_types);
4582
4583 if (! type_unit_die->has_children)
4584 return;
4585
4586 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
4587
4588 gdb_assert (storage != nullptr);
4589 cooked_indexer indexer (storage, per_cu, cu->lang ());
4590 indexer.make_index (reader);
4591 }
4592
4593 /* Struct used to sort TUs by their abbreviation table offset. */
4594
4595 struct tu_abbrev_offset
4596 {
4597 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
4598 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
4599 {}
4600
4601 /* This is used when sorting. */
4602 bool operator< (const tu_abbrev_offset &other) const
4603 {
4604 return abbrev_offset < other.abbrev_offset;
4605 }
4606
4607 signatured_type *sig_type;
4608 sect_offset abbrev_offset;
4609 };
4610
4611 /* Efficiently read all the type units.
4612
4613 The efficiency is because we sort TUs by the abbrev table they use and
4614 only read each abbrev table once. In one program there are 200K TUs
4615 sharing 8K abbrev tables.
4616
4617 The main purpose of this function is to support building the
4618 dwarf2_per_objfile->per_bfd->type_unit_groups table.
4619 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
4620 can collapse the search space by grouping them by stmt_list.
4621 The savings can be significant, in the same program from above the 200K TUs
4622 share 8K stmt_list tables.
4623
4624 FUNC is expected to call get_type_unit_group, which will create the
4625 struct type_unit_group if necessary and add it to
4626 dwarf2_per_objfile->per_bfd->type_unit_groups. */
4627
4628 static void
4629 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
4630 cooked_index_storage *storage)
4631 {
4632 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4633 abbrev_table_up abbrev_table;
4634 sect_offset abbrev_offset;
4635
4636 /* It's up to the caller to not call us multiple times. */
4637 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
4638
4639 if (per_objfile->per_bfd->all_type_units.size () == 0)
4640 return;
4641
4642 /* TUs typically share abbrev tables, and there can be way more TUs than
4643 abbrev tables. Sort by abbrev table to reduce the number of times we
4644 read each abbrev table in.
4645 Alternatives are to punt or to maintain a cache of abbrev tables.
4646 This is simpler and efficient enough for now.
4647
4648 Later we group TUs by their DW_AT_stmt_list value (as this defines the
4649 symtab to use). Typically TUs with the same abbrev offset have the same
4650 stmt_list value too so in practice this should work well.
4651
4652 The basic algorithm here is:
4653
4654 sort TUs by abbrev table
4655 for each TU with same abbrev table:
4656 read abbrev table if first user
4657 read TU top level DIE
4658 [IWBN if DWO skeletons had DW_AT_stmt_list]
4659 call FUNC */
4660
4661 dwarf_read_debug_printf ("Building type unit groups ...");
4662
4663 /* Sort in a separate table to maintain the order of all_units
4664 for .gdb_index: TU indices directly index all_type_units. */
4665 std::vector<tu_abbrev_offset> sorted_by_abbrev;
4666 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
4667
4668 for (const auto &cu : per_objfile->per_bfd->all_units)
4669 {
4670 if (cu->is_debug_types)
4671 {
4672 auto sig_type = static_cast<signatured_type *> (cu.get ());
4673 sorted_by_abbrev.emplace_back
4674 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
4675 sig_type->sect_off));
4676 }
4677 }
4678
4679 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
4680
4681 abbrev_offset = (sect_offset) ~(unsigned) 0;
4682
4683 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
4684 {
4685 /* Switch to the next abbrev table if necessary. */
4686 if (abbrev_table == NULL
4687 || tu.abbrev_offset != abbrev_offset)
4688 {
4689 abbrev_offset = tu.abbrev_offset;
4690 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
4691 abbrev_table =
4692 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
4693 ++tu_stats->nr_uniq_abbrev_tables;
4694 }
4695
4696 cutu_reader reader (tu.sig_type, per_objfile,
4697 abbrev_table.get (), nullptr, false);
4698 if (!reader.dummy_p)
4699 build_type_psymtabs_reader (&reader, storage);
4700 }
4701 }
4702
4703 /* Print collected type unit statistics. */
4704
4705 static void
4706 print_tu_stats (dwarf2_per_objfile *per_objfile)
4707 {
4708 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4709
4710 dwarf_read_debug_printf ("Type unit statistics:");
4711 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
4712 dwarf_read_debug_printf (" %d uniq abbrev tables",
4713 tu_stats->nr_uniq_abbrev_tables);
4714 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
4715 tu_stats->nr_symtabs);
4716 dwarf_read_debug_printf (" %d symtab sharers",
4717 tu_stats->nr_symtab_sharers);
4718 dwarf_read_debug_printf (" %d type units without a stmt_list",
4719 tu_stats->nr_stmt_less_type_units);
4720 dwarf_read_debug_printf (" %d all_type_units reallocs",
4721 tu_stats->nr_all_type_units_reallocs);
4722 }
4723
4724 struct skeleton_data
4725 {
4726 dwarf2_per_objfile *per_objfile;
4727 cooked_index_storage *storage;
4728 };
4729
4730 /* Traversal function for process_skeletonless_type_unit.
4731 Read a TU in a DWO file and build partial symbols for it. */
4732
4733 static int
4734 process_skeletonless_type_unit (void **slot, void *info)
4735 {
4736 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
4737 skeleton_data *data = (skeleton_data *) info;
4738
4739 /* If this TU doesn't exist in the global table, add it and read it in. */
4740
4741 if (data->per_objfile->per_bfd->signatured_types == NULL)
4742 data->per_objfile->per_bfd->signatured_types
4743 = allocate_signatured_type_table ();
4744
4745 signatured_type find_entry (dwo_unit->signature);
4746 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
4747 &find_entry, INSERT);
4748 /* If we've already seen this type there's nothing to do. What's happening
4749 is we're doing our own version of comdat-folding here. */
4750 if (*slot != NULL)
4751 return 1;
4752
4753 /* This does the job that create_all_units would have done for
4754 this TU. */
4755 signatured_type *entry
4756 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
4757 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
4758 *slot = entry;
4759
4760 /* This does the job that build_type_psymtabs would have done. */
4761 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
4762 if (!reader.dummy_p)
4763 build_type_psymtabs_reader (&reader, data->storage);
4764
4765 return 1;
4766 }
4767
4768 /* Traversal function for process_skeletonless_type_units. */
4769
4770 static int
4771 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
4772 {
4773 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
4774
4775 if (dwo_file->tus != NULL)
4776 htab_traverse_noresize (dwo_file->tus.get (),
4777 process_skeletonless_type_unit, info);
4778
4779 return 1;
4780 }
4781
4782 /* Scan all TUs of DWO files, verifying we've processed them.
4783 This is needed in case a TU was emitted without its skeleton.
4784 Note: This can't be done until we know what all the DWO files are. */
4785
4786 static void
4787 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
4788 cooked_index_storage *storage)
4789 {
4790 skeleton_data data { per_objfile, storage };
4791
4792 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
4793 if (get_dwp_file (per_objfile) == NULL
4794 && per_objfile->per_bfd->dwo_files != NULL)
4795 {
4796 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
4797 process_dwo_file_for_skeletonless_type_units,
4798 &data);
4799 }
4800 }
4801
4802 /* A subclass of cooked_index_worker that handles scanning
4803 .debug_info. */
4804
4805 class cooked_index_debug_info : public cooked_index_worker
4806 {
4807 public:
4808 cooked_index_debug_info (dwarf2_per_objfile *per_objfile)
4809 : cooked_index_worker (per_objfile)
4810 {
4811 gdb_assert (is_main_thread ());
4812
4813 struct objfile *objfile = per_objfile->objfile;
4814 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4815
4816 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
4817 objfile_name (objfile));
4818
4819 per_bfd->map_info_sections (objfile);
4820 }
4821
4822 private:
4823
4824 void do_reading () override;
4825
4826 void print_stats () override
4827 {
4828 if (dwarf_read_debug > 0)
4829 print_tu_stats (m_per_objfile);
4830 }
4831
4832 /* After the last DWARF-reading task has finished, this function
4833 does the remaining work to finish the scan. */
4834 void done_reading ();
4835
4836 /* An iterator for the comp units. */
4837 typedef std::vector<dwarf2_per_cu_data_up>::iterator unit_iterator;
4838
4839 /* Process a batch of CUs. This may be called multiple times in
4840 separate threads. TASK_NUMBER indicates which task this is --
4841 the result is stored in that slot of M_RESULTS. */
4842 void process_cus (size_t task_number, unit_iterator first,
4843 unit_iterator end);
4844
4845 /* A storage object for "leftovers" -- see the 'start' method, but
4846 essentially things not parsed during the normal CU parsing
4847 passes. */
4848 cooked_index_storage m_index_storage;
4849 };
4850
4851 void
4852 cooked_index_debug_info::process_cus (size_t task_number, unit_iterator first,
4853 unit_iterator end)
4854 {
4855 SCOPE_EXIT { bfd_thread_cleanup (); };
4856
4857 /* Ensure that complaints are handled correctly. */
4858 complaint_interceptor complaint_handler;
4859
4860 std::vector<gdb_exception> errors;
4861 cooked_index_storage thread_storage;
4862 for (auto inner = first; inner != end; ++inner)
4863 {
4864 dwarf2_per_cu_data *per_cu = inner->get ();
4865 try
4866 {
4867 process_psymtab_comp_unit (per_cu, m_per_objfile, &thread_storage);
4868 }
4869 catch (gdb_exception &except)
4870 {
4871 errors.push_back (std::move (except));
4872 }
4873 }
4874
4875 m_results[task_number] = result_type (thread_storage.release (),
4876 complaint_handler.release (),
4877 std::move (errors),
4878 thread_storage.release_parent_map ());
4879 }
4880
4881 void
4882 cooked_index_debug_info::done_reading ()
4883 {
4884 /* Only handle the scanning results here. Complaints and exceptions
4885 can only be dealt with on the main thread. */
4886 std::vector<std::unique_ptr<cooked_index_shard>> indexes;
4887 for (auto &one_result : m_results)
4888 {
4889 indexes.push_back (std::move (std::get<0> (one_result)));
4890 m_all_parents_map.add_map (std::get<3> (one_result));
4891 }
4892
4893 /* This has to wait until we read the CUs, we need the list of DWOs. */
4894 process_skeletonless_type_units (m_per_objfile, &m_index_storage);
4895
4896 indexes.push_back (m_index_storage.release ());
4897 indexes.shrink_to_fit ();
4898
4899 m_all_parents_map.add_map (m_index_storage.release_parent_map ());
4900
4901 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4902 cooked_index *table
4903 = (gdb::checked_static_cast<cooked_index *>
4904 (per_bfd->index_table.get ()));
4905 table->set_contents (std::move (indexes), &m_warnings,
4906 &m_all_parents_map);
4907 }
4908
4909 void
4910 cooked_index_debug_info::do_reading ()
4911 {
4912 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4913
4914 create_all_units (m_per_objfile);
4915 build_type_psymtabs (m_per_objfile, &m_index_storage);
4916
4917 per_bfd->quick_file_names_table
4918 = create_quick_file_names_table (per_bfd->all_units.size ());
4919 if (!per_bfd->debug_aranges.empty ())
4920 read_addrmap_from_aranges (m_per_objfile, &per_bfd->debug_aranges,
4921 m_index_storage.get_addrmap (),
4922 &m_warnings);
4923
4924 /* We want to balance the load between the worker threads. This is
4925 done by using the size of each CU as a rough estimate of how
4926 difficult it will be to operate on. This isn't ideal -- for
4927 example if dwz is used, the early CUs will all tend to be
4928 "included" and won't be parsed independently. However, this
4929 heuristic works well for typical compiler output. */
4930
4931 size_t total_size = 0;
4932 for (const auto &per_cu : per_bfd->all_units)
4933 total_size += per_cu->length ();
4934
4935 /* How many worker threads we plan to use. We may not actually use
4936 this many. We use 1 as the minimum to avoid division by zero,
4937 and anyway in the N==0 case the work will be done
4938 synchronously. */
4939 const size_t n_worker_threads
4940 = std::max (gdb::thread_pool::g_thread_pool->thread_count (), (size_t) 1);
4941
4942 /* How much effort should be put into each worker. */
4943 const size_t size_per_thread
4944 = std::max (total_size / n_worker_threads, (size_t) 1);
4945
4946 /* Work is done in a task group. */
4947 gdb::task_group workers ([this] ()
4948 {
4949 this->done_reading ();
4950 });
4951
4952 auto end = per_bfd->all_units.end ();
4953 size_t task_count = 0;
4954 for (auto iter = per_bfd->all_units.begin (); iter != end; )
4955 {
4956 auto last = iter;
4957 /* Put all remaining CUs into the last task. */
4958 if (task_count == n_worker_threads - 1)
4959 last = end;
4960 else
4961 {
4962 size_t chunk_size = 0;
4963 for (; last != end && chunk_size < size_per_thread; ++last)
4964 chunk_size += (*last)->length ();
4965 }
4966
4967 gdb_assert (iter != last);
4968 workers.add_task ([=] ()
4969 {
4970 process_cus (task_count, iter, last);
4971 });
4972
4973 ++task_count;
4974 iter = last;
4975 }
4976
4977 m_results.resize (task_count);
4978 workers.start ();
4979 }
4980
4981 static void
4982 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
4983 struct dwarf2_section_info *section,
4984 struct dwarf2_section_info *abbrev_section,
4985 unsigned int is_dwz,
4986 htab_up &types_htab,
4987 rcuh_kind section_kind)
4988 {
4989 const gdb_byte *info_ptr;
4990 struct objfile *objfile = per_objfile->objfile;
4991
4992 dwarf_read_debug_printf ("Reading %s for %s",
4993 section->get_name (),
4994 section->get_file_name ());
4995
4996 section->read (objfile);
4997
4998 info_ptr = section->buffer;
4999
5000 while (info_ptr < section->buffer + section->size)
5001 {
5002 dwarf2_per_cu_data_up this_cu;
5003
5004 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
5005
5006 comp_unit_head cu_header;
5007 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
5008 abbrev_section, info_ptr,
5009 section_kind);
5010
5011 /* Save the compilation unit for later lookup. */
5012 if (cu_header.unit_type != DW_UT_type)
5013 this_cu = per_objfile->per_bfd->allocate_per_cu ();
5014 else
5015 {
5016 if (types_htab == nullptr)
5017 types_htab = allocate_signatured_type_table ();
5018
5019 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
5020 (cu_header.signature);
5021 signatured_type *sig_ptr = sig_type.get ();
5022 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
5023 this_cu.reset (sig_type.release ());
5024
5025 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
5026 gdb_assert (slot != nullptr);
5027 if (*slot != nullptr)
5028 complaint (_("debug type entry at offset %s is duplicate to"
5029 " the entry at offset %s, signature %s"),
5030 sect_offset_str (sect_off),
5031 sect_offset_str (sig_ptr->sect_off),
5032 hex_string (sig_ptr->signature));
5033 *slot = sig_ptr;
5034 }
5035 this_cu->sect_off = sect_off;
5036 this_cu->set_length (cu_header.get_length_with_initial ());
5037 this_cu->is_dwz = is_dwz;
5038 this_cu->section = section;
5039 /* Init this asap, to avoid a data race in the set_version in
5040 cutu_reader::cutu_reader (which may be run in parallel for the cooked
5041 index case). */
5042 this_cu->set_version (cu_header.version);
5043
5044 info_ptr = info_ptr + this_cu->length ();
5045 per_objfile->per_bfd->all_units.push_back (std::move (this_cu));
5046 }
5047 }
5048
5049 /* Initialize the views on all_units. */
5050
5051 void
5052 finalize_all_units (dwarf2_per_bfd *per_bfd)
5053 {
5054 size_t nr_tus = per_bfd->tu_stats.nr_tus;
5055 size_t nr_cus = per_bfd->all_units.size () - nr_tus;
5056 gdb::array_view<dwarf2_per_cu_data_up> tmp = per_bfd->all_units;
5057 per_bfd->all_comp_units = tmp.slice (0, nr_cus);
5058 per_bfd->all_type_units = tmp.slice (nr_cus, nr_tus);
5059 }
5060
5061 /* See read.h. */
5062
5063 void
5064 create_all_units (dwarf2_per_objfile *per_objfile)
5065 {
5066 htab_up types_htab;
5067 gdb_assert (per_objfile->per_bfd->all_units.empty ());
5068
5069 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
5070 &per_objfile->per_bfd->abbrev, 0,
5071 types_htab, rcuh_kind::COMPILE);
5072 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
5073 read_comp_units_from_section (per_objfile, &section,
5074 &per_objfile->per_bfd->abbrev, 0,
5075 types_htab, rcuh_kind::TYPE);
5076
5077 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
5078 if (dwz != NULL)
5079 {
5080 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
5081 types_htab, rcuh_kind::COMPILE);
5082
5083 if (!dwz->types.empty ())
5084 {
5085 per_objfile->per_bfd->all_units.clear ();
5086
5087 /* See enhancement PR symtab/30838. */
5088 error (_("Dwarf Error: .debug_types section not supported in dwz file"));
5089 }
5090 }
5091
5092 per_objfile->per_bfd->signatured_types = std::move (types_htab);
5093
5094 finalize_all_units (per_objfile->per_bfd);
5095 }
5096
5097 /* Return the initial uleb128 in the die at INFO_PTR. */
5098
5099 static unsigned int
5100 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
5101 {
5102 unsigned int bytes_read;
5103
5104 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5105 }
5106
5107 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
5108 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
5109
5110 Return the corresponding abbrev, or NULL if the number is zero (indicating
5111 an empty DIE). In either case *BYTES_READ will be set to the length of
5112 the initial number. */
5113
5114 static const struct abbrev_info *
5115 peek_die_abbrev (const die_reader_specs &reader,
5116 const gdb_byte *info_ptr, unsigned int *bytes_read)
5117 {
5118 dwarf2_cu *cu = reader.cu;
5119 bfd *abfd = reader.abfd;
5120 unsigned int abbrev_number
5121 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5122
5123 if (abbrev_number == 0)
5124 return NULL;
5125
5126 const abbrev_info *abbrev
5127 = reader.abbrev_table->lookup_abbrev (abbrev_number);
5128 if (!abbrev)
5129 {
5130 error (_("Dwarf Error: Could not find abbrev number %d in %s"
5131 " at offset %s [in module %s]"),
5132 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
5133 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
5134 }
5135
5136 return abbrev;
5137 }
5138
5139 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5140 Returns a pointer to the end of a series of DIEs, terminated by an empty
5141 DIE. Any children of the skipped DIEs will also be skipped. */
5142
5143 static const gdb_byte *
5144 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
5145 {
5146 while (1)
5147 {
5148 unsigned int bytes_read;
5149 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
5150 &bytes_read);
5151
5152 if (abbrev == NULL)
5153 return info_ptr + bytes_read;
5154 else
5155 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5156 }
5157 }
5158
5159 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5160 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5161 abbrev corresponding to that skipped uleb128 should be passed in
5162 ABBREV.
5163
5164 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
5165 returns a pointer to this DIE's sibling, skipping any children.
5166 Otherwise, returns a pointer to the DIE's first child. */
5167
5168 static const gdb_byte *
5169 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
5170 const struct abbrev_info *abbrev, bool do_skip_children)
5171 {
5172 unsigned int bytes_read;
5173 struct attribute attr;
5174 bfd *abfd = reader->abfd;
5175 struct dwarf2_cu *cu = reader->cu;
5176 const gdb_byte *buffer = reader->buffer;
5177 const gdb_byte *buffer_end = reader->buffer_end;
5178 unsigned int form, i;
5179
5180 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
5181 {
5182 /* We only handle DW_FORM_ref4 here. */
5183 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
5184 unsigned int offset = read_4_bytes (abfd, sibling_data);
5185 const gdb_byte *sibling_ptr
5186 = buffer + to_underlying (cu->header.sect_off) + offset;
5187 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
5188 return sibling_ptr;
5189 /* Fall through to the slow way. */
5190 }
5191 else if (abbrev->size_if_constant != 0)
5192 {
5193 info_ptr += abbrev->size_if_constant;
5194 if (do_skip_children && abbrev->has_children)
5195 return skip_children (reader, info_ptr);
5196 return info_ptr;
5197 }
5198
5199 for (i = 0; i < abbrev->num_attrs; i++)
5200 {
5201 /* The only abbrev we care about is DW_AT_sibling. */
5202 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
5203 {
5204 /* Note there is no need for the extra work of
5205 "reprocessing" here, so we pass false for that
5206 argument. */
5207 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr, false);
5208 if (attr.form == DW_FORM_ref_addr)
5209 complaint (_("ignoring absolute DW_AT_sibling"));
5210 else
5211 {
5212 sect_offset off = attr.get_ref_die_offset ();
5213 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
5214
5215 if (sibling_ptr < info_ptr)
5216 complaint (_("DW_AT_sibling points backwards"));
5217 else if (sibling_ptr > reader->buffer_end)
5218 reader->die_section->overflow_complaint ();
5219 else
5220 return sibling_ptr;
5221 }
5222 }
5223
5224 /* If it isn't DW_AT_sibling, skip this attribute. */
5225 form = abbrev->attrs[i].form;
5226 skip_attribute:
5227 switch (form)
5228 {
5229 case DW_FORM_ref_addr:
5230 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5231 and later it is offset sized. */
5232 if (cu->header.version == 2)
5233 info_ptr += cu->header.addr_size;
5234 else
5235 info_ptr += cu->header.offset_size;
5236 break;
5237 case DW_FORM_GNU_ref_alt:
5238 info_ptr += cu->header.offset_size;
5239 break;
5240 case DW_FORM_addr:
5241 info_ptr += cu->header.addr_size;
5242 break;
5243 case DW_FORM_data1:
5244 case DW_FORM_ref1:
5245 case DW_FORM_flag:
5246 case DW_FORM_strx1:
5247 info_ptr += 1;
5248 break;
5249 case DW_FORM_flag_present:
5250 case DW_FORM_implicit_const:
5251 break;
5252 case DW_FORM_data2:
5253 case DW_FORM_ref2:
5254 case DW_FORM_strx2:
5255 info_ptr += 2;
5256 break;
5257 case DW_FORM_strx3:
5258 info_ptr += 3;
5259 break;
5260 case DW_FORM_data4:
5261 case DW_FORM_ref4:
5262 case DW_FORM_strx4:
5263 info_ptr += 4;
5264 break;
5265 case DW_FORM_data8:
5266 case DW_FORM_ref8:
5267 case DW_FORM_ref_sig8:
5268 info_ptr += 8;
5269 break;
5270 case DW_FORM_data16:
5271 info_ptr += 16;
5272 break;
5273 case DW_FORM_string:
5274 read_direct_string (abfd, info_ptr, &bytes_read);
5275 info_ptr += bytes_read;
5276 break;
5277 case DW_FORM_sec_offset:
5278 case DW_FORM_strp:
5279 case DW_FORM_GNU_strp_alt:
5280 info_ptr += cu->header.offset_size;
5281 break;
5282 case DW_FORM_exprloc:
5283 case DW_FORM_block:
5284 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5285 info_ptr += bytes_read;
5286 break;
5287 case DW_FORM_block1:
5288 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5289 break;
5290 case DW_FORM_block2:
5291 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5292 break;
5293 case DW_FORM_block4:
5294 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5295 break;
5296 case DW_FORM_addrx:
5297 case DW_FORM_strx:
5298 case DW_FORM_sdata:
5299 case DW_FORM_udata:
5300 case DW_FORM_ref_udata:
5301 case DW_FORM_GNU_addr_index:
5302 case DW_FORM_GNU_str_index:
5303 case DW_FORM_rnglistx:
5304 case DW_FORM_loclistx:
5305 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
5306 break;
5307 case DW_FORM_indirect:
5308 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5309 info_ptr += bytes_read;
5310 /* We need to continue parsing from here, so just go back to
5311 the top. */
5312 goto skip_attribute;
5313
5314 default:
5315 error (_("Dwarf Error: Cannot handle %s "
5316 "in DWARF reader [in module %s]"),
5317 dwarf_form_name (form),
5318 bfd_get_filename (abfd));
5319 }
5320 }
5321
5322 if (do_skip_children && abbrev->has_children)
5323 return skip_children (reader, info_ptr);
5324 else
5325 return info_ptr;
5326 }
5327 \f
5328 /* Reading in full CUs. */
5329
5330 /* Add PER_CU to the queue. */
5331
5332 static void
5333 queue_comp_unit (dwarf2_per_cu_data *per_cu,
5334 dwarf2_per_objfile *per_objfile,
5335 enum language pretend_language)
5336 {
5337 per_cu->queued = 1;
5338
5339 gdb_assert (per_objfile->queue.has_value ());
5340 per_objfile->queue->emplace (per_cu, per_objfile, pretend_language);
5341 }
5342
5343 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
5344
5345 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
5346 dependency.
5347
5348 Return true if maybe_queue_comp_unit requires the caller to load the CU's
5349 DIEs, false otherwise.
5350
5351 Explanation: there is an invariant that if a CU is queued for expansion
5352 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
5353 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
5354 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
5355 are not yet loaded, the the caller must load the CU's DIEs to ensure the
5356 invariant is respected.
5357
5358 The caller is therefore not required to load the CU's DIEs (we return false)
5359 if:
5360
5361 - the CU is already expanded, and therefore does not get enqueued
5362 - the CU gets enqueued for expansion, but its DIEs are already loaded
5363
5364 Note that the caller should not use this function's return value as an
5365 indicator of whether the CU's DIEs are loaded right now, it should check
5366 that by calling `dwarf2_per_objfile::get_cu` instead. */
5367
5368 static int
5369 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
5370 dwarf2_per_cu_data *per_cu,
5371 dwarf2_per_objfile *per_objfile,
5372 enum language pretend_language)
5373 {
5374 /* Mark the dependence relation so that we don't flush PER_CU
5375 too early. */
5376 if (dependent_cu != NULL)
5377 dependent_cu->add_dependence (per_cu);
5378
5379 /* If it's already on the queue, we have nothing to do. */
5380 if (per_cu->queued)
5381 {
5382 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
5383 loaded. */
5384 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
5385
5386 /* If the CU is queued for expansion, it should not already be
5387 expanded. */
5388 gdb_assert (!per_objfile->symtab_set_p (per_cu));
5389
5390 /* The DIEs are already loaded, the caller doesn't need to do it. */
5391 return 0;
5392 }
5393
5394 bool queued = false;
5395 if (!per_objfile->symtab_set_p (per_cu))
5396 {
5397 /* Add it to the queue. */
5398 queue_comp_unit (per_cu, per_objfile, pretend_language);
5399 queued = true;
5400 }
5401
5402 /* If the compilation unit is already loaded, just mark it as
5403 used. */
5404 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
5405 if (cu != nullptr)
5406 cu->last_used = 0;
5407
5408 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
5409 and the DIEs are not already loaded. */
5410 return queued && cu == nullptr;
5411 }
5412
5413 /* Process the queue. */
5414
5415 static void
5416 process_queue (dwarf2_per_objfile *per_objfile)
5417 {
5418 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
5419 objfile_name (per_objfile->objfile));
5420
5421 /* The queue starts out with one item, but following a DIE reference
5422 may load a new CU, adding it to the end of the queue. */
5423 while (!per_objfile->queue->empty ())
5424 {
5425 dwarf2_queue_item &item = per_objfile->queue->front ();
5426 dwarf2_per_cu_data *per_cu = item.per_cu;
5427
5428 if (!per_objfile->symtab_set_p (per_cu))
5429 {
5430 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
5431
5432 /* Skip dummy CUs. */
5433 if (cu != nullptr)
5434 {
5435 unsigned int debug_print_threshold;
5436 char buf[100];
5437
5438 if (per_cu->is_debug_types)
5439 {
5440 struct signatured_type *sig_type =
5441 (struct signatured_type *) per_cu;
5442
5443 sprintf (buf, "TU %s at offset %s",
5444 hex_string (sig_type->signature),
5445 sect_offset_str (per_cu->sect_off));
5446 /* There can be 100s of TUs.
5447 Only print them in verbose mode. */
5448 debug_print_threshold = 2;
5449 }
5450 else
5451 {
5452 sprintf (buf, "CU at offset %s",
5453 sect_offset_str (per_cu->sect_off));
5454 debug_print_threshold = 1;
5455 }
5456
5457 if (dwarf_read_debug >= debug_print_threshold)
5458 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
5459
5460 if (per_cu->is_debug_types)
5461 process_full_type_unit (cu, item.pretend_language);
5462 else
5463 process_full_comp_unit (cu, item.pretend_language);
5464
5465 if (dwarf_read_debug >= debug_print_threshold)
5466 dwarf_read_debug_printf ("Done expanding %s", buf);
5467 }
5468 }
5469
5470 per_cu->queued = 0;
5471 per_objfile->queue->pop ();
5472 }
5473
5474 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
5475 objfile_name (per_objfile->objfile));
5476 }
5477
5478 /* Load the DIEs associated with PER_CU into memory.
5479
5480 In some cases, the caller, while reading partial symbols, will need to load
5481 the full symbols for the CU for some reason. It will already have a
5482 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
5483 rather than creating a new one. */
5484
5485 static void
5486 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
5487 dwarf2_per_objfile *per_objfile,
5488 dwarf2_cu *existing_cu,
5489 bool skip_partial,
5490 enum language pretend_language)
5491 {
5492 gdb_assert (! this_cu->is_debug_types);
5493
5494 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
5495 if (reader.dummy_p)
5496 return;
5497
5498 struct dwarf2_cu *cu = reader.cu;
5499 const gdb_byte *info_ptr = reader.info_ptr;
5500
5501 gdb_assert (cu->die_hash == NULL);
5502 cu->die_hash =
5503 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
5504 die_info::hash,
5505 die_info::eq,
5506 NULL,
5507 &cu->comp_unit_obstack,
5508 hashtab_obstack_allocate,
5509 dummy_obstack_deallocate);
5510
5511 if (reader.comp_unit_die->has_children)
5512 reader.comp_unit_die->child
5513 = read_die_and_siblings (&reader, reader.info_ptr,
5514 &info_ptr, reader.comp_unit_die);
5515 cu->dies = reader.comp_unit_die;
5516 /* comp_unit_die is not stored in die_hash, no need. */
5517
5518 /* We try not to read any attributes in this function, because not
5519 all CUs needed for references have been loaded yet, and symbol
5520 table processing isn't initialized. But we have to set the CU language,
5521 or we won't be able to build types correctly.
5522 Similarly, if we do not read the producer, we can not apply
5523 producer-specific interpretation. */
5524 prepare_one_comp_unit (cu, cu->dies, pretend_language);
5525
5526 reader.keep ();
5527 }
5528
5529 /* Add a DIE to the delayed physname list. */
5530
5531 static void
5532 add_to_method_list (struct type *type, int fnfield_index, int index,
5533 const char *name, struct die_info *die,
5534 struct dwarf2_cu *cu)
5535 {
5536 struct delayed_method_info mi;
5537 mi.type = type;
5538 mi.fnfield_index = fnfield_index;
5539 mi.index = index;
5540 mi.name = name;
5541 mi.die = die;
5542 cu->method_list.push_back (mi);
5543 }
5544
5545 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
5546 "const" / "volatile". If so, decrements LEN by the length of the
5547 modifier and return true. Otherwise return false. */
5548
5549 template<size_t N>
5550 static bool
5551 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
5552 {
5553 size_t mod_len = sizeof (mod) - 1;
5554 if (len > mod_len && startswith (physname + (len - mod_len), mod))
5555 {
5556 len -= mod_len;
5557 return true;
5558 }
5559 return false;
5560 }
5561
5562 /* Compute the physnames of any methods on the CU's method list.
5563
5564 The computation of method physnames is delayed in order to avoid the
5565 (bad) condition that one of the method's formal parameters is of an as yet
5566 incomplete type. */
5567
5568 static void
5569 compute_delayed_physnames (struct dwarf2_cu *cu)
5570 {
5571 /* Only C++ delays computing physnames. */
5572 if (cu->method_list.empty ())
5573 return;
5574 gdb_assert (cu->lang () == language_cplus);
5575
5576 for (const delayed_method_info &mi : cu->method_list)
5577 {
5578 const char *physname;
5579 struct fn_fieldlist *fn_flp
5580 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
5581 physname = dwarf2_physname (mi.name, mi.die, cu);
5582 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
5583 = physname ? physname : "";
5584
5585 /* Since there's no tag to indicate whether a method is a
5586 const/volatile overload, extract that information out of the
5587 demangled name. */
5588 if (physname != NULL)
5589 {
5590 size_t len = strlen (physname);
5591
5592 while (1)
5593 {
5594 if (physname[len - 1] == ')') /* shortcut */
5595 break;
5596 else if (check_modifier (physname, len, " const"))
5597 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
5598 else if (check_modifier (physname, len, " volatile"))
5599 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
5600 else
5601 break;
5602 }
5603 }
5604 }
5605
5606 /* The list is no longer needed. */
5607 cu->method_list.clear ();
5608 }
5609
5610 /* Go objects should be embedded in a DW_TAG_module DIE,
5611 and it's not clear if/how imported objects will appear.
5612 To keep Go support simple until that's worked out,
5613 go back through what we've read and create something usable.
5614 We could do this while processing each DIE, and feels kinda cleaner,
5615 but that way is more invasive.
5616 This is to, for example, allow the user to type "p var" or "b main"
5617 without having to specify the package name, and allow lookups
5618 of module.object to work in contexts that use the expression
5619 parser. */
5620
5621 static void
5622 fixup_go_packaging (struct dwarf2_cu *cu)
5623 {
5624 gdb::unique_xmalloc_ptr<char> package_name;
5625 struct pending *list;
5626 int i;
5627
5628 for (list = *cu->get_builder ()->get_global_symbols ();
5629 list != NULL;
5630 list = list->next)
5631 {
5632 for (i = 0; i < list->nsyms; ++i)
5633 {
5634 struct symbol *sym = list->symbol[i];
5635
5636 if (sym->language () == language_go
5637 && sym->aclass () == LOC_BLOCK)
5638 {
5639 gdb::unique_xmalloc_ptr<char> this_package_name
5640 = go_symbol_package_name (sym);
5641
5642 if (this_package_name == NULL)
5643 continue;
5644 if (package_name == NULL)
5645 package_name = std::move (this_package_name);
5646 else
5647 {
5648 struct objfile *objfile = cu->per_objfile->objfile;
5649 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
5650 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
5651 (sym->symtab () != NULL
5652 ? symtab_to_filename_for_display
5653 (sym->symtab ())
5654 : objfile_name (objfile)),
5655 this_package_name.get (), package_name.get ());
5656 }
5657 }
5658 }
5659 }
5660
5661 if (package_name != NULL)
5662 {
5663 struct objfile *objfile = cu->per_objfile->objfile;
5664 const char *saved_package_name = objfile->intern (package_name.get ());
5665 struct type *type
5666 = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_MODULE, 0,
5667 saved_package_name);
5668 struct symbol *sym;
5669
5670 sym = new (&objfile->objfile_obstack) symbol;
5671 sym->set_language (language_go, &objfile->objfile_obstack);
5672 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
5673 sym->set_domain (TYPE_DOMAIN);
5674 sym->set_aclass_index (LOC_TYPEDEF);
5675 sym->set_type (type);
5676
5677 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
5678 }
5679 }
5680
5681 /* Allocate a fully-qualified name consisting of the two parts on the
5682 obstack. */
5683
5684 static const char *
5685 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
5686 {
5687 return obconcat (obstack, p1, "::", p2, (char *) NULL);
5688 }
5689
5690 /* A helper that allocates a variant part to attach to a Rust enum
5691 type. OBSTACK is where the results should be allocated. TYPE is
5692 the type we're processing. DISCRIMINANT_INDEX is the index of the
5693 discriminant. It must be the index of one of the fields of TYPE,
5694 or -1 to mean there is no discriminant (univariant enum).
5695 DEFAULT_INDEX is the index of the default field; or -1 if there is
5696 no default. RANGES is indexed by "effective" field number (the
5697 field index, but omitting the discriminant and default fields) and
5698 must hold the discriminant values used by the variants. Note that
5699 RANGES must have a lifetime at least as long as OBSTACK -- either
5700 already allocated on it, or static. */
5701
5702 static void
5703 alloc_rust_variant (struct obstack *obstack, struct type *type,
5704 int discriminant_index, int default_index,
5705 gdb::array_view<discriminant_range> ranges)
5706 {
5707 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
5708 gdb_assert (discriminant_index == -1
5709 || (discriminant_index >= 0
5710 && discriminant_index < type->num_fields ()));
5711 gdb_assert (default_index == -1
5712 || (default_index >= 0 && default_index < type->num_fields ()));
5713
5714 /* We have one variant for each non-discriminant field. */
5715 int n_variants = type->num_fields ();
5716 if (discriminant_index != -1)
5717 --n_variants;
5718
5719 variant *variants = new (obstack) variant[n_variants];
5720 int var_idx = 0;
5721 int range_idx = 0;
5722 for (int i = 0; i < type->num_fields (); ++i)
5723 {
5724 if (i == discriminant_index)
5725 continue;
5726
5727 variants[var_idx].first_field = i;
5728 variants[var_idx].last_field = i + 1;
5729
5730 /* The default field does not need a range, but other fields do.
5731 We skipped the discriminant above. */
5732 if (i != default_index)
5733 {
5734 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
5735 ++range_idx;
5736 }
5737
5738 ++var_idx;
5739 }
5740
5741 gdb_assert (range_idx == ranges.size ());
5742 gdb_assert (var_idx == n_variants);
5743
5744 variant_part *part = new (obstack) variant_part;
5745 part->discriminant_index = discriminant_index;
5746 /* If there is no discriminant, then whether it is signed is of no
5747 consequence. */
5748 part->is_unsigned
5749 = (discriminant_index == -1
5750 ? false
5751 : type->field (discriminant_index).type ()->is_unsigned ());
5752 part->variants = gdb::array_view<variant> (variants, n_variants);
5753
5754 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
5755 gdb::array_view<variant_part> *prop_value
5756 = new (storage) gdb::array_view<variant_part> (part, 1);
5757
5758 struct dynamic_prop prop;
5759 prop.set_variant_parts (prop_value);
5760
5761 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
5762 }
5763
5764 /* Some versions of rustc emitted enums in an unusual way.
5765
5766 Ordinary enums were emitted as unions. The first element of each
5767 structure in the union was named "RUST$ENUM$DISR". This element
5768 held the discriminant.
5769
5770 These versions of Rust also implemented the "non-zero"
5771 optimization. When the enum had two values, and one is empty and
5772 the other holds a pointer that cannot be zero, the pointer is used
5773 as the discriminant, with a zero value meaning the empty variant.
5774 Here, the union's first member is of the form
5775 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
5776 where the fieldnos are the indices of the fields that should be
5777 traversed in order to find the field (which may be several fields deep)
5778 and the variantname is the name of the variant of the case when the
5779 field is zero.
5780
5781 This function recognizes whether TYPE is of one of these forms,
5782 and, if so, smashes it to be a variant type. */
5783
5784 static void
5785 quirk_rust_enum (struct type *type, struct objfile *objfile)
5786 {
5787 gdb_assert (type->code () == TYPE_CODE_UNION);
5788
5789 /* We don't need to deal with empty enums. */
5790 if (type->num_fields () == 0)
5791 return;
5792
5793 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
5794 if (type->num_fields () == 1
5795 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
5796 {
5797 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
5798
5799 /* Decode the field name to find the offset of the
5800 discriminant. */
5801 ULONGEST bit_offset = 0;
5802 struct type *field_type = type->field (0).type ();
5803 while (name[0] >= '0' && name[0] <= '9')
5804 {
5805 char *tail;
5806 unsigned long index = strtoul (name, &tail, 10);
5807 name = tail;
5808 if (*name != '$'
5809 || index >= field_type->num_fields ()
5810 || (field_type->field (index).loc_kind ()
5811 != FIELD_LOC_KIND_BITPOS))
5812 {
5813 complaint (_("Could not parse Rust enum encoding string \"%s\""
5814 "[in module %s]"),
5815 type->field (0).name (),
5816 objfile_name (objfile));
5817 return;
5818 }
5819 ++name;
5820
5821 bit_offset += field_type->field (index).loc_bitpos ();
5822 field_type = field_type->field (index).type ();
5823 }
5824
5825 /* Smash this type to be a structure type. We have to do this
5826 because the type has already been recorded. */
5827 type->set_code (TYPE_CODE_STRUCT);
5828 /* Save the field we care about. */
5829 struct field saved_field = type->field (0);
5830 type->alloc_fields (3);
5831
5832 /* Put the discriminant at index 0. */
5833 type->field (0).set_type (field_type);
5834 type->field (0).set_is_artificial (true);
5835 type->field (0).set_name ("<<discriminant>>");
5836 type->field (0).set_loc_bitpos (bit_offset);
5837
5838 /* The order of fields doesn't really matter, so put the real
5839 field at index 1 and the data-less field at index 2. */
5840 type->field (1) = saved_field;
5841 type->field (1).set_name
5842 (rust_last_path_segment (type->field (1).type ()->name ()));
5843 type->field (1).type ()->set_name
5844 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
5845 type->field (1).name ()));
5846
5847 const char *dataless_name
5848 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
5849 name);
5850 struct type *dataless_type
5851 = type_allocator (type).new_type (TYPE_CODE_VOID, 0,
5852 dataless_name);
5853 type->field (2).set_type (dataless_type);
5854 /* NAME points into the original discriminant name, which
5855 already has the correct lifetime. */
5856 type->field (2).set_name (name);
5857 type->field (2).set_loc_bitpos (0);
5858
5859 /* Indicate that this is a variant type. */
5860 static discriminant_range ranges[1] = { { 0, 0 } };
5861 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
5862 }
5863 /* A union with a single anonymous field is probably an old-style
5864 univariant enum. */
5865 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
5866 {
5867 /* Smash this type to be a structure type. We have to do this
5868 because the type has already been recorded. */
5869 type->set_code (TYPE_CODE_STRUCT);
5870
5871 struct type *field_type = type->field (0).type ();
5872 const char *variant_name
5873 = rust_last_path_segment (field_type->name ());
5874 type->field (0).set_name (variant_name);
5875 field_type->set_name
5876 (rust_fully_qualify (&objfile->objfile_obstack,
5877 type->name (), variant_name));
5878
5879 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
5880 }
5881 else
5882 {
5883 struct type *disr_type = nullptr;
5884 for (int i = 0; i < type->num_fields (); ++i)
5885 {
5886 disr_type = type->field (i).type ();
5887
5888 if (disr_type->code () != TYPE_CODE_STRUCT)
5889 {
5890 /* All fields of a true enum will be structs. */
5891 return;
5892 }
5893 else if (disr_type->num_fields () == 0)
5894 {
5895 /* Could be data-less variant, so keep going. */
5896 disr_type = nullptr;
5897 }
5898 else if (strcmp (disr_type->field (0).name (),
5899 "RUST$ENUM$DISR") != 0)
5900 {
5901 /* Not a Rust enum. */
5902 return;
5903 }
5904 else
5905 {
5906 /* Found one. */
5907 break;
5908 }
5909 }
5910
5911 /* If we got here without a discriminant, then it's probably
5912 just a union. */
5913 if (disr_type == nullptr)
5914 return;
5915
5916 /* Smash this type to be a structure type. We have to do this
5917 because the type has already been recorded. */
5918 type->set_code (TYPE_CODE_STRUCT);
5919
5920 /* Make space for the discriminant field. */
5921 struct field *disr_field = &disr_type->field (0);
5922 field *new_fields
5923 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
5924 * sizeof (struct field)));
5925 memcpy (new_fields + 1, type->fields (),
5926 type->num_fields () * sizeof (struct field));
5927 type->set_fields (new_fields);
5928 type->set_num_fields (type->num_fields () + 1);
5929
5930 /* Install the discriminant at index 0 in the union. */
5931 type->field (0) = *disr_field;
5932 type->field (0).set_is_artificial (true);
5933 type->field (0).set_name ("<<discriminant>>");
5934
5935 /* We need a way to find the correct discriminant given a
5936 variant name. For convenience we build a map here. */
5937 struct type *enum_type = disr_field->type ();
5938 std::unordered_map<std::string_view, ULONGEST> discriminant_map;
5939 for (int i = 0; i < enum_type->num_fields (); ++i)
5940 {
5941 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
5942 {
5943 const char *name
5944 = rust_last_path_segment (enum_type->field (i).name ());
5945 discriminant_map[name] = enum_type->field (i).loc_enumval ();
5946 }
5947 }
5948
5949 int n_fields = type->num_fields ();
5950 /* We don't need a range entry for the discriminant, but we do
5951 need one for every other field, as there is no default
5952 variant. */
5953 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
5954 discriminant_range,
5955 n_fields - 1);
5956 /* Skip the discriminant here. */
5957 for (int i = 1; i < n_fields; ++i)
5958 {
5959 /* Find the final word in the name of this variant's type.
5960 That name can be used to look up the correct
5961 discriminant. */
5962 const char *variant_name
5963 = rust_last_path_segment (type->field (i).type ()->name ());
5964
5965 auto iter = discriminant_map.find (variant_name);
5966 if (iter != discriminant_map.end ())
5967 {
5968 ranges[i - 1].low = iter->second;
5969 ranges[i - 1].high = iter->second;
5970 }
5971
5972 /* In Rust, each element should have the size of the
5973 enclosing enum. */
5974 type->field (i).type ()->set_length (type->length ());
5975
5976 /* Remove the discriminant field, if it exists. */
5977 struct type *sub_type = type->field (i).type ();
5978 if (sub_type->num_fields () > 0)
5979 {
5980 sub_type->set_num_fields (sub_type->num_fields () - 1);
5981 sub_type->set_fields (sub_type->fields () + 1);
5982 }
5983 type->field (i).set_name (variant_name);
5984 sub_type->set_name
5985 (rust_fully_qualify (&objfile->objfile_obstack,
5986 type->name (), variant_name));
5987 }
5988
5989 /* Indicate that this is a variant type. */
5990 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
5991 gdb::array_view<discriminant_range> (ranges,
5992 n_fields - 1));
5993 }
5994 }
5995
5996 /* Rewrite some Rust unions to be structures with variants parts. */
5997
5998 static void
5999 rust_union_quirks (struct dwarf2_cu *cu)
6000 {
6001 gdb_assert (cu->lang () == language_rust);
6002 for (type *type_ : cu->rust_unions)
6003 quirk_rust_enum (type_, cu->per_objfile->objfile);
6004 /* We don't need this any more. */
6005 cu->rust_unions.clear ();
6006 }
6007
6008 /* See read.h. */
6009
6010 type_unit_group_unshareable *
6011 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
6012 {
6013 auto iter = this->m_type_units.find (tu_group);
6014 if (iter != this->m_type_units.end ())
6015 return iter->second.get ();
6016
6017 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
6018 type_unit_group_unshareable *result = uniq.get ();
6019 this->m_type_units[tu_group] = std::move (uniq);
6020 return result;
6021 }
6022
6023 struct type *
6024 dwarf2_per_objfile::get_type_for_signatured_type
6025 (signatured_type *sig_type) const
6026 {
6027 auto iter = this->m_type_map.find (sig_type);
6028 if (iter == this->m_type_map.end ())
6029 return nullptr;
6030
6031 return iter->second;
6032 }
6033
6034 void dwarf2_per_objfile::set_type_for_signatured_type
6035 (signatured_type *sig_type, struct type *type)
6036 {
6037 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
6038
6039 this->m_type_map[sig_type] = type;
6040 }
6041
6042 /* A helper function for computing the list of all symbol tables
6043 included by PER_CU. */
6044
6045 static void
6046 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
6047 htab_t all_children, htab_t all_type_symtabs,
6048 dwarf2_per_cu_data *per_cu,
6049 dwarf2_per_objfile *per_objfile,
6050 struct compunit_symtab *immediate_parent)
6051 {
6052 void **slot = htab_find_slot (all_children, per_cu, INSERT);
6053 if (*slot != NULL)
6054 {
6055 /* This inclusion and its children have been processed. */
6056 return;
6057 }
6058
6059 *slot = per_cu;
6060
6061 /* Only add a CU if it has a symbol table. */
6062 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6063 if (cust != NULL)
6064 {
6065 /* If this is a type unit only add its symbol table if we haven't
6066 seen it yet (type unit per_cu's can share symtabs). */
6067 if (per_cu->is_debug_types)
6068 {
6069 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
6070 if (*slot == NULL)
6071 {
6072 *slot = cust;
6073 result->push_back (cust);
6074 if (cust->user == NULL)
6075 cust->user = immediate_parent;
6076 }
6077 }
6078 else
6079 {
6080 result->push_back (cust);
6081 if (cust->user == NULL)
6082 cust->user = immediate_parent;
6083 }
6084 }
6085
6086 for (dwarf2_per_cu_data *ptr : per_cu->imported_symtabs)
6087 recursively_compute_inclusions (result, all_children,
6088 all_type_symtabs, ptr, per_objfile,
6089 cust);
6090 }
6091
6092 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
6093 PER_CU. */
6094
6095 static void
6096 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
6097 dwarf2_per_objfile *per_objfile)
6098 {
6099 gdb_assert (! per_cu->is_debug_types);
6100
6101 if (!per_cu->imported_symtabs.empty ())
6102 {
6103 int len;
6104 std::vector<compunit_symtab *> result_symtabs;
6105 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6106
6107 /* If we don't have a symtab, we can just skip this case. */
6108 if (cust == NULL)
6109 return;
6110
6111 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
6112 htab_eq_pointer,
6113 NULL, xcalloc, xfree));
6114 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
6115 htab_eq_pointer,
6116 NULL, xcalloc, xfree));
6117
6118 for (dwarf2_per_cu_data *ptr : per_cu->imported_symtabs)
6119 recursively_compute_inclusions (&result_symtabs, all_children.get (),
6120 all_type_symtabs.get (), ptr,
6121 per_objfile, cust);
6122
6123 /* Now we have a transitive closure of all the included symtabs. */
6124 len = result_symtabs.size ();
6125 cust->includes
6126 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
6127 struct compunit_symtab *, len + 1);
6128 memcpy (cust->includes, result_symtabs.data (),
6129 len * sizeof (compunit_symtab *));
6130 cust->includes[len] = NULL;
6131 }
6132 }
6133
6134 /* Compute the 'includes' field for the symtabs of all the CUs we just
6135 read. */
6136
6137 static void
6138 process_cu_includes (dwarf2_per_objfile *per_objfile)
6139 {
6140 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
6141 {
6142 if (! iter->is_debug_types)
6143 compute_compunit_symtab_includes (iter, per_objfile);
6144 }
6145
6146 per_objfile->per_bfd->just_read_cus.clear ();
6147 }
6148
6149 /* Generate full symbol information for CU, whose DIEs have
6150 already been loaded into memory. */
6151
6152 static void
6153 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
6154 {
6155 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6156 unrelocated_addr lowpc, highpc;
6157 struct compunit_symtab *cust;
6158 struct block *static_block;
6159 CORE_ADDR addr;
6160
6161 /* Clear the list here in case something was left over. */
6162 cu->method_list.clear ();
6163
6164 dwarf2_find_base_address (cu->dies, cu);
6165
6166 /* Before we start reading the top-level DIE, ensure it has a valid tag
6167 type. */
6168 switch (cu->dies->tag)
6169 {
6170 case DW_TAG_compile_unit:
6171 case DW_TAG_partial_unit:
6172 case DW_TAG_type_unit:
6173 break;
6174 default:
6175 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6176 dwarf_tag_name (cu->dies->tag),
6177 sect_offset_str (cu->per_cu->sect_off),
6178 objfile_name (per_objfile->objfile));
6179 }
6180
6181 /* Do line number decoding in read_file_scope () */
6182 process_die (cu->dies, cu);
6183
6184 /* For now fudge the Go package. */
6185 if (cu->lang () == language_go)
6186 fixup_go_packaging (cu);
6187
6188 /* Now that we have processed all the DIEs in the CU, all the types
6189 should be complete, and it should now be safe to compute all of the
6190 physnames. */
6191 compute_delayed_physnames (cu);
6192
6193 if (cu->lang () == language_rust)
6194 rust_union_quirks (cu);
6195
6196 /* Some compilers don't define a DW_AT_high_pc attribute for the
6197 compilation unit. If the DW_AT_high_pc is missing, synthesize
6198 it, by scanning the DIE's below the compilation unit. */
6199 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6200
6201 addr = per_objfile->relocate (highpc);
6202 static_block
6203 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
6204
6205 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6206 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6207 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6208 addrmap to help ensure it has an accurate map of pc values belonging to
6209 this comp unit. */
6210 dwarf2_record_block_ranges (cu->dies, static_block, cu);
6211
6212 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
6213 (static_block, 0);
6214
6215 if (cust != NULL)
6216 {
6217 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6218
6219 /* Set symtab language to language from DW_AT_language. If the
6220 compilation is from a C file generated by language preprocessors, do
6221 not set the language if it was already deduced by start_subfile. */
6222 if (!(cu->lang () == language_c
6223 && cust->primary_filetab ()->language () != language_unknown))
6224 cust->primary_filetab ()->set_language (cu->lang ());
6225
6226 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6227 produce DW_AT_location with location lists but it can be possibly
6228 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6229 there were bugs in prologue debug info, fixed later in GCC-4.5
6230 by "unwind info for epilogues" patch (which is not directly related).
6231
6232 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6233 needed, it would be wrong due to missing DW_AT_producer there.
6234
6235 Still one can confuse GDB by using non-standard GCC compilation
6236 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6237 */
6238 if (cu->has_loclist && gcc_4_minor >= 5)
6239 cust->set_locations_valid (true);
6240
6241 int major, minor;
6242 if (cu->producer != nullptr
6243 && producer_is_gcc (cu->producer, &major, &minor)
6244 && (major < 4 || (major == 4 && minor < 5)))
6245 /* Don't trust gcc < 4.5.x. */
6246 cust->set_epilogue_unwind_valid (false);
6247 else
6248 cust->set_epilogue_unwind_valid (true);
6249
6250 cust->set_call_site_htab (cu->call_site_htab);
6251 }
6252
6253 per_objfile->set_symtab (cu->per_cu, cust);
6254
6255 /* Push it for inclusion processing later. */
6256 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
6257
6258 /* Not needed any more. */
6259 cu->reset_builder ();
6260 }
6261
6262 /* Generate full symbol information for type unit CU, whose DIEs have
6263 already been loaded into memory. */
6264
6265 static void
6266 process_full_type_unit (dwarf2_cu *cu,
6267 enum language pretend_language)
6268 {
6269 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6270 struct compunit_symtab *cust;
6271 struct signatured_type *sig_type;
6272
6273 gdb_assert (cu->per_cu->is_debug_types);
6274 sig_type = (struct signatured_type *) cu->per_cu;
6275
6276 /* Clear the list here in case something was left over. */
6277 cu->method_list.clear ();
6278
6279 /* The symbol tables are set up in read_type_unit_scope. */
6280 process_die (cu->dies, cu);
6281
6282 /* For now fudge the Go package. */
6283 if (cu->lang () == language_go)
6284 fixup_go_packaging (cu);
6285
6286 /* Now that we have processed all the DIEs in the CU, all the types
6287 should be complete, and it should now be safe to compute all of the
6288 physnames. */
6289 compute_delayed_physnames (cu);
6290
6291 if (cu->lang () == language_rust)
6292 rust_union_quirks (cu);
6293
6294 /* TUs share symbol tables.
6295 If this is the first TU to use this symtab, complete the construction
6296 of it with end_expandable_symtab. Otherwise, complete the addition of
6297 this TU's symbols to the existing symtab. */
6298 type_unit_group_unshareable *tug_unshare =
6299 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
6300 if (tug_unshare->compunit_symtab == NULL)
6301 {
6302 buildsym_compunit *builder = cu->get_builder ();
6303 cust = builder->end_expandable_symtab (0);
6304 tug_unshare->compunit_symtab = cust;
6305
6306 if (cust != NULL)
6307 {
6308 /* Set symtab language to language from DW_AT_language. If the
6309 compilation is from a C file generated by language preprocessors,
6310 do not set the language if it was already deduced by
6311 start_subfile. */
6312 if (!(cu->lang () == language_c
6313 && cust->primary_filetab ()->language () != language_c))
6314 cust->primary_filetab ()->set_language (cu->lang ());
6315 }
6316 }
6317 else
6318 {
6319 cu->get_builder ()->augment_type_symtab ();
6320 cust = tug_unshare->compunit_symtab;
6321 }
6322
6323 per_objfile->set_symtab (cu->per_cu, cust);
6324
6325 /* Not needed any more. */
6326 cu->reset_builder ();
6327 }
6328
6329 /* Process an imported unit DIE. */
6330
6331 static void
6332 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6333 {
6334 struct attribute *attr;
6335
6336 /* For now we don't handle imported units in type units. */
6337 if (cu->per_cu->is_debug_types)
6338 {
6339 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6340 " supported in type units [in module %s]"),
6341 objfile_name (cu->per_objfile->objfile));
6342 }
6343
6344 attr = dwarf2_attr (die, DW_AT_import, cu);
6345 if (attr != NULL)
6346 {
6347 sect_offset sect_off = attr->get_ref_die_offset ();
6348 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
6349 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6350 dwarf2_per_cu_data *per_cu
6351 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
6352 per_objfile->per_bfd);
6353
6354 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
6355 into another compilation unit, at root level. Regard this as a hint,
6356 and ignore it. This is a best effort, it only works if unit_type and
6357 lang are already set. */
6358 if (die->parent && die->parent->parent == NULL
6359 && per_cu->unit_type (false) == DW_UT_compile
6360 && per_cu->lang (false) == language_cplus)
6361 return;
6362
6363 /* If necessary, add it to the queue and load its DIEs. */
6364 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
6365 cu->lang ()))
6366 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
6367 false, cu->lang ());
6368
6369 cu->per_cu->imported_symtabs.push_back (per_cu);
6370 }
6371 }
6372
6373 /* RAII object that represents a process_die scope: i.e.,
6374 starts/finishes processing a DIE. */
6375 class process_die_scope
6376 {
6377 public:
6378 process_die_scope (die_info *die, dwarf2_cu *cu)
6379 : m_die (die), m_cu (cu)
6380 {
6381 /* We should only be processing DIEs not already in process. */
6382 gdb_assert (!m_die->in_process);
6383 m_die->in_process = true;
6384 }
6385
6386 ~process_die_scope ()
6387 {
6388 m_die->in_process = false;
6389
6390 /* If we're done processing the DIE for the CU that owns the line
6391 header, we don't need the line header anymore. */
6392 if (m_cu->line_header_die_owner == m_die)
6393 {
6394 delete m_cu->line_header;
6395 m_cu->line_header = NULL;
6396 m_cu->line_header_die_owner = NULL;
6397 }
6398 }
6399
6400 private:
6401 die_info *m_die;
6402 dwarf2_cu *m_cu;
6403 };
6404
6405 /* Process a die and its children. */
6406
6407 static void
6408 process_die (struct die_info *die, struct dwarf2_cu *cu)
6409 {
6410 process_die_scope scope (die, cu);
6411
6412 switch (die->tag)
6413 {
6414 case DW_TAG_padding:
6415 break;
6416 case DW_TAG_compile_unit:
6417 case DW_TAG_partial_unit:
6418 read_file_scope (die, cu);
6419 break;
6420 case DW_TAG_type_unit:
6421 read_type_unit_scope (die, cu);
6422 break;
6423 case DW_TAG_subprogram:
6424 /* Nested subprograms in Fortran get a prefix. */
6425 if (cu->lang () == language_fortran
6426 && die->parent != NULL
6427 && die->parent->tag == DW_TAG_subprogram)
6428 cu->processing_has_namespace_info = true;
6429 [[fallthrough]];
6430 /* Fall through. */
6431 case DW_TAG_entry_point:
6432 case DW_TAG_inlined_subroutine:
6433 read_func_scope (die, cu);
6434 break;
6435 case DW_TAG_lexical_block:
6436 case DW_TAG_try_block:
6437 case DW_TAG_catch_block:
6438 read_lexical_block_scope (die, cu);
6439 break;
6440 case DW_TAG_call_site:
6441 case DW_TAG_GNU_call_site:
6442 read_call_site_scope (die, cu);
6443 break;
6444 case DW_TAG_class_type:
6445 case DW_TAG_interface_type:
6446 case DW_TAG_structure_type:
6447 case DW_TAG_union_type:
6448 case DW_TAG_namelist:
6449 process_structure_scope (die, cu);
6450 break;
6451 case DW_TAG_enumeration_type:
6452 process_enumeration_scope (die, cu);
6453 break;
6454
6455 /* These dies have a type, but processing them does not create
6456 a symbol or recurse to process the children. Therefore we can
6457 read them on-demand through read_type_die. */
6458 case DW_TAG_subroutine_type:
6459 case DW_TAG_set_type:
6460 case DW_TAG_pointer_type:
6461 case DW_TAG_ptr_to_member_type:
6462 case DW_TAG_reference_type:
6463 case DW_TAG_rvalue_reference_type:
6464 case DW_TAG_string_type:
6465 break;
6466
6467 case DW_TAG_array_type:
6468 /* We only need to handle this case for Ada -- in other
6469 languages, it's normal for the compiler to emit a typedef
6470 instead. */
6471 if (cu->lang () != language_ada)
6472 break;
6473 [[fallthrough]];
6474 case DW_TAG_base_type:
6475 case DW_TAG_subrange_type:
6476 case DW_TAG_generic_subrange:
6477 case DW_TAG_typedef:
6478 case DW_TAG_unspecified_type:
6479 /* Add a typedef symbol for the type definition, if it has a
6480 DW_AT_name. */
6481 new_symbol (die, read_type_die (die, cu), cu);
6482 break;
6483 case DW_TAG_common_block:
6484 read_common_block (die, cu);
6485 break;
6486 case DW_TAG_common_inclusion:
6487 break;
6488 case DW_TAG_namespace:
6489 cu->processing_has_namespace_info = true;
6490 read_namespace (die, cu);
6491 break;
6492 case DW_TAG_module:
6493 cu->processing_has_namespace_info = true;
6494 read_module (die, cu);
6495 break;
6496 case DW_TAG_imported_declaration:
6497 cu->processing_has_namespace_info = true;
6498 if (read_alias (die, cu))
6499 break;
6500 /* The declaration is neither a global namespace nor a variable
6501 alias. */
6502 [[fallthrough]];
6503 case DW_TAG_imported_module:
6504 cu->processing_has_namespace_info = true;
6505 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6506 || cu->lang () != language_fortran))
6507 complaint (_("Tag '%s' has unexpected children"),
6508 dwarf_tag_name (die->tag));
6509 read_import_statement (die, cu);
6510 break;
6511
6512 case DW_TAG_imported_unit:
6513 process_imported_unit_die (die, cu);
6514 break;
6515
6516 case DW_TAG_variable:
6517 read_variable (die, cu);
6518 break;
6519
6520 default:
6521 new_symbol (die, NULL, cu);
6522 break;
6523 }
6524 }
6525 \f
6526 /* DWARF name computation. */
6527
6528 /* A helper function for dwarf2_compute_name which determines whether DIE
6529 needs to have the name of the scope prepended to the name listed in the
6530 die. */
6531
6532 static int
6533 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6534 {
6535 struct attribute *attr;
6536
6537 switch (die->tag)
6538 {
6539 case DW_TAG_namespace:
6540 case DW_TAG_typedef:
6541 case DW_TAG_class_type:
6542 case DW_TAG_interface_type:
6543 case DW_TAG_structure_type:
6544 case DW_TAG_union_type:
6545 case DW_TAG_enumeration_type:
6546 case DW_TAG_enumerator:
6547 case DW_TAG_subprogram:
6548 case DW_TAG_inlined_subroutine:
6549 case DW_TAG_entry_point:
6550 case DW_TAG_member:
6551 case DW_TAG_imported_declaration:
6552 return 1;
6553
6554 case DW_TAG_variable:
6555 case DW_TAG_constant:
6556 /* We only need to prefix "globally" visible variables. These include
6557 any variable marked with DW_AT_external or any variable that
6558 lives in a namespace. [Variables in anonymous namespaces
6559 require prefixing, but they are not DW_AT_external.] */
6560
6561 if (dwarf2_attr (die, DW_AT_specification, cu))
6562 {
6563 struct dwarf2_cu *spec_cu = cu;
6564
6565 return die_needs_namespace (die_specification (die, &spec_cu),
6566 spec_cu);
6567 }
6568
6569 attr = dwarf2_attr (die, DW_AT_external, cu);
6570 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6571 && die->parent->tag != DW_TAG_module)
6572 return 0;
6573 /* A variable in a lexical block of some kind does not need a
6574 namespace, even though in C++ such variables may be external
6575 and have a mangled name. */
6576 if (die->parent->tag == DW_TAG_lexical_block
6577 || die->parent->tag == DW_TAG_try_block
6578 || die->parent->tag == DW_TAG_catch_block
6579 || die->parent->tag == DW_TAG_subprogram)
6580 return 0;
6581 return 1;
6582
6583 default:
6584 return 0;
6585 }
6586 }
6587
6588 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
6589 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6590 defined for the given DIE. */
6591
6592 static struct attribute *
6593 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
6594 {
6595 struct attribute *attr;
6596
6597 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6598 if (attr == NULL)
6599 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6600
6601 return attr;
6602 }
6603
6604 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
6605 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6606 defined for the given DIE. */
6607
6608 static const char *
6609 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
6610 {
6611 const char *linkage_name;
6612
6613 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
6614 if (linkage_name == NULL)
6615 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
6616
6617 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
6618 See https://github.com/rust-lang/rust/issues/32925. */
6619 if (cu->lang () == language_rust && linkage_name != NULL
6620 && strchr (linkage_name, '{') != NULL)
6621 linkage_name = NULL;
6622
6623 return linkage_name;
6624 }
6625
6626 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6627 compute the physname for the object, which include a method's:
6628 - formal parameters (C++),
6629 - receiver type (Go),
6630
6631 The term "physname" is a bit confusing.
6632 For C++, for example, it is the demangled name.
6633 For Go, for example, it's the mangled name.
6634
6635 For Ada, return the DIE's linkage name rather than the fully qualified
6636 name. PHYSNAME is ignored..
6637
6638 The result is allocated on the objfile->per_bfd's obstack and
6639 canonicalized. */
6640
6641 static const char *
6642 dwarf2_compute_name (const char *name,
6643 struct die_info *die, struct dwarf2_cu *cu,
6644 int physname)
6645 {
6646 struct objfile *objfile = cu->per_objfile->objfile;
6647
6648 if (name == NULL)
6649 name = dwarf2_name (die, cu);
6650
6651 enum language lang = cu->lang ();
6652
6653 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
6654 but otherwise compute it by typename_concat inside GDB.
6655 FIXME: Actually this is not really true, or at least not always true.
6656 It's all very confusing. compute_and_set_names doesn't try to demangle
6657 Fortran names because there is no mangling standard. So new_symbol
6658 will set the demangled name to the result of dwarf2_full_name, and it is
6659 the demangled name that GDB uses if it exists. */
6660 if (lang == language_ada
6661 || (lang == language_fortran && physname))
6662 {
6663 /* For Ada unit, we prefer the linkage name over the name, as
6664 the former contains the exported name, which the user expects
6665 to be able to reference. Ideally, we want the user to be able
6666 to reference this entity using either natural or linkage name,
6667 but we haven't started looking at this enhancement yet. */
6668 const char *linkage_name = dw2_linkage_name (die, cu);
6669
6670 if (linkage_name != NULL)
6671 return linkage_name;
6672 }
6673
6674 /* These are the only languages we know how to qualify names in. */
6675 if (name != NULL
6676 && (lang == language_cplus
6677 || lang == language_fortran || lang == language_d
6678 || lang == language_rust))
6679 {
6680 if (die_needs_namespace (die, cu))
6681 {
6682 const char *prefix;
6683
6684 string_file buf;
6685
6686 prefix = determine_prefix (die, cu);
6687 if (*prefix != '\0')
6688 {
6689 gdb::unique_xmalloc_ptr<char> prefixed_name
6690 (typename_concat (NULL, prefix, name, physname, cu));
6691
6692 buf.puts (prefixed_name.get ());
6693 }
6694 else
6695 buf.puts (name);
6696
6697 /* Template parameters may be specified in the DIE's DW_AT_name, or
6698 as children with DW_TAG_template_type_param or
6699 DW_TAG_value_type_param. If the latter, add them to the name
6700 here. If the name already has template parameters, then
6701 skip this step; some versions of GCC emit both, and
6702 it is more efficient to use the pre-computed name.
6703
6704 Something to keep in mind about this process: it is very
6705 unlikely, or in some cases downright impossible, to produce
6706 something that will match the mangled name of a function.
6707 If the definition of the function has the same debug info,
6708 we should be able to match up with it anyway. But fallbacks
6709 using the minimal symbol, for instance to find a method
6710 implemented in a stripped copy of libstdc++, will not work.
6711 If we do not have debug info for the definition, we will have to
6712 match them up some other way.
6713
6714 When we do name matching there is a related problem with function
6715 templates; two instantiated function templates are allowed to
6716 differ only by their return types, which we do not add here. */
6717
6718 if (lang == language_cplus && strchr (name, '<') == NULL)
6719 {
6720 struct attribute *attr;
6721 struct die_info *child;
6722 int first = 1;
6723
6724 die->building_fullname = 1;
6725
6726 for (child = die->child; child != NULL; child = child->sibling)
6727 {
6728 struct type *type;
6729 LONGEST value;
6730 const gdb_byte *bytes;
6731 struct dwarf2_locexpr_baton *baton;
6732 struct value *v;
6733
6734 if (child->tag != DW_TAG_template_type_param
6735 && child->tag != DW_TAG_template_value_param)
6736 continue;
6737
6738 if (first)
6739 {
6740 buf.puts ("<");
6741 first = 0;
6742 }
6743 else
6744 buf.puts (", ");
6745
6746 attr = dwarf2_attr (child, DW_AT_type, cu);
6747 if (attr == NULL)
6748 {
6749 complaint (_("template parameter missing DW_AT_type"));
6750 buf.puts ("UNKNOWN_TYPE");
6751 continue;
6752 }
6753 type = die_type (child, cu);
6754
6755 if (child->tag == DW_TAG_template_type_param)
6756 {
6757 cu->language_defn->print_type (type, "", &buf, -1, 0,
6758 &type_print_raw_options);
6759 continue;
6760 }
6761
6762 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6763 if (attr == NULL)
6764 {
6765 complaint (_("template parameter missing "
6766 "DW_AT_const_value"));
6767 buf.puts ("UNKNOWN_VALUE");
6768 continue;
6769 }
6770
6771 dwarf2_const_value_attr (attr, type, name,
6772 &cu->comp_unit_obstack, cu,
6773 &value, &bytes, &baton);
6774
6775 if (type->has_no_signedness ())
6776 /* GDB prints characters as NUMBER 'CHAR'. If that's
6777 changed, this can use value_print instead. */
6778 cu->language_defn->printchar (value, type, &buf);
6779 else
6780 {
6781 struct value_print_options opts;
6782
6783 if (baton != NULL)
6784 v = dwarf2_evaluate_loc_desc (type, NULL,
6785 baton->data,
6786 baton->size,
6787 baton->per_cu,
6788 baton->per_objfile);
6789 else if (bytes != NULL)
6790 {
6791 v = value::allocate (type);
6792 memcpy (v->contents_writeable ().data (), bytes,
6793 type->length ());
6794 }
6795 else
6796 v = value_from_longest (type, value);
6797
6798 /* Specify decimal so that we do not depend on
6799 the radix. */
6800 get_formatted_print_options (&opts, 'd');
6801 opts.raw = true;
6802 value_print (v, &buf, &opts);
6803 release_value (v);
6804 }
6805 }
6806
6807 die->building_fullname = 0;
6808
6809 if (!first)
6810 {
6811 /* Close the argument list, with a space if necessary
6812 (nested templates). */
6813 if (!buf.empty () && buf.string ().back () == '>')
6814 buf.puts (" >");
6815 else
6816 buf.puts (">");
6817 }
6818 }
6819
6820 /* For C++ methods, append formal parameter type
6821 information, if PHYSNAME. */
6822
6823 if (physname && die->tag == DW_TAG_subprogram
6824 && lang == language_cplus)
6825 {
6826 struct type *type = read_type_die (die, cu);
6827
6828 c_type_print_args (type, &buf, 1, lang,
6829 &type_print_raw_options);
6830
6831 if (lang == language_cplus)
6832 {
6833 /* Assume that an artificial first parameter is
6834 "this", but do not crash if it is not. RealView
6835 marks unnamed (and thus unused) parameters as
6836 artificial; there is no way to differentiate
6837 the two cases. */
6838 if (type->num_fields () > 0
6839 && type->field (0).is_artificial ()
6840 && type->field (0).type ()->code () == TYPE_CODE_PTR
6841 && TYPE_CONST (type->field (0).type ()->target_type ()))
6842 buf.puts (" const");
6843 }
6844 }
6845
6846 const std::string &intermediate_name = buf.string ();
6847
6848 const char *canonical_name
6849 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
6850 objfile);
6851
6852 /* If we only computed INTERMEDIATE_NAME, or if
6853 INTERMEDIATE_NAME is already canonical, then we need to
6854 intern it. */
6855 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
6856 name = objfile->intern (intermediate_name);
6857 else
6858 name = canonical_name;
6859 }
6860 }
6861
6862 return name;
6863 }
6864
6865 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6866 If scope qualifiers are appropriate they will be added. The result
6867 will be allocated on the storage_obstack, or NULL if the DIE does
6868 not have a name. NAME may either be from a previous call to
6869 dwarf2_name or NULL.
6870
6871 The output string will be canonicalized (if C++). */
6872
6873 static const char *
6874 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
6875 {
6876 return dwarf2_compute_name (name, die, cu, 0);
6877 }
6878
6879 /* Construct a physname for the given DIE in CU. NAME may either be
6880 from a previous call to dwarf2_name or NULL. The result will be
6881 allocated on the objfile_obstack or NULL if the DIE does not have a
6882 name.
6883
6884 The output string will be canonicalized (if C++). */
6885
6886 static const char *
6887 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
6888 {
6889 struct objfile *objfile = cu->per_objfile->objfile;
6890 const char *retval, *mangled = NULL, *canon = NULL;
6891 int need_copy = 1;
6892
6893 /* In this case dwarf2_compute_name is just a shortcut not building anything
6894 on its own. */
6895 if (!die_needs_namespace (die, cu))
6896 return dwarf2_compute_name (name, die, cu, 1);
6897
6898 if (cu->lang () != language_rust)
6899 mangled = dw2_linkage_name (die, cu);
6900
6901 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6902 has computed. */
6903 gdb::unique_xmalloc_ptr<char> demangled;
6904 if (mangled != NULL)
6905 {
6906 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
6907 {
6908 /* Do nothing (do not demangle the symbol name). */
6909 }
6910 else
6911 {
6912 /* Use DMGL_RET_DROP for C++ template functions to suppress
6913 their return type. It is easier for GDB users to search
6914 for such functions as `name(params)' than `long name(params)'.
6915 In such case the minimal symbol names do not match the full
6916 symbol names but for template functions there is never a need
6917 to look up their definition from their declaration so
6918 the only disadvantage remains the minimal symbol variant
6919 `long name(params)' does not have the proper inferior type. */
6920 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
6921 | DMGL_RET_DROP));
6922 }
6923 if (demangled)
6924 canon = demangled.get ();
6925 else
6926 {
6927 canon = mangled;
6928 need_copy = 0;
6929 }
6930 }
6931
6932 if (canon == NULL || check_physname)
6933 {
6934 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6935
6936 if (canon != NULL && strcmp (physname, canon) != 0)
6937 {
6938 /* It may not mean a bug in GDB. The compiler could also
6939 compute DW_AT_linkage_name incorrectly. But in such case
6940 GDB would need to be bug-to-bug compatible. */
6941
6942 complaint (_("Computed physname <%s> does not match demangled <%s> "
6943 "(from linkage <%s>) - DIE at %s [in module %s]"),
6944 physname, canon, mangled, sect_offset_str (die->sect_off),
6945 objfile_name (objfile));
6946
6947 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6948 is available here - over computed PHYSNAME. It is safer
6949 against both buggy GDB and buggy compilers. */
6950
6951 retval = canon;
6952 }
6953 else
6954 {
6955 retval = physname;
6956 need_copy = 0;
6957 }
6958 }
6959 else
6960 retval = canon;
6961
6962 if (need_copy)
6963 retval = objfile->intern (retval);
6964
6965 return retval;
6966 }
6967
6968 /* Inspect DIE in CU for a namespace alias or a variable with alias
6969 attribute. If one exists, record a new symbol for it.
6970
6971 Returns true if an alias was recorded, false otherwise. */
6972
6973 static bool
6974 read_alias (struct die_info *die, struct dwarf2_cu *cu)
6975 {
6976 struct attribute *attr;
6977
6978 /* If the die does not have a name, this is neither a namespace
6979 alias nor a variable alias. */
6980 attr = dwarf2_attr (die, DW_AT_name, cu);
6981 if (attr != NULL)
6982 {
6983 int num;
6984 struct die_info *d = die;
6985 struct dwarf2_cu *imported_cu = cu;
6986
6987 /* If the compiler has nested DW_AT_imported_declaration DIEs,
6988 keep inspecting DIEs until we hit the underlying import. */
6989 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
6990 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
6991 {
6992 attr = dwarf2_attr (d, DW_AT_import, cu);
6993 if (attr == NULL)
6994 break;
6995
6996 d = follow_die_ref (d, attr, &imported_cu);
6997 if (d->tag != DW_TAG_imported_declaration)
6998 break;
6999 }
7000
7001 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
7002 {
7003 complaint (_("DIE at %s has too many recursively imported "
7004 "declarations"), sect_offset_str (d->sect_off));
7005 return false;
7006 }
7007
7008 if (attr != NULL)
7009 {
7010 struct type *type;
7011 if (d->tag == DW_TAG_variable)
7012 {
7013 /* This declaration is a C/C++ global variable alias.
7014 Add a symbol for it whose type is the same as the
7015 aliased variable's. */
7016 type = die_type (d, imported_cu);
7017 struct symbol *sym = new_symbol (die, type, cu);
7018 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
7019 sym->set_aclass_index (LOC_UNRESOLVED);
7020 if (attr != nullptr)
7021 var_decode_location (attr, sym, cu);
7022 return true;
7023 }
7024 else
7025 {
7026 sect_offset sect_off = attr->get_ref_die_offset ();
7027 type = get_die_type_at_offset (sect_off, cu->per_cu,
7028 cu->per_objfile);
7029 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
7030 {
7031 /* This declaration is a global namespace alias. Add
7032 a symbol for it whose type is the aliased
7033 namespace. */
7034 new_symbol (die, type, cu);
7035 return true;
7036 }
7037 }
7038 }
7039 }
7040 return false;
7041 }
7042
7043 /* Return the using directives repository (global or local?) to use in the
7044 current context for CU.
7045
7046 For Ada, imported declarations can materialize renamings, which *may* be
7047 global. However it is impossible (for now?) in DWARF to distinguish
7048 "external" imported declarations and "static" ones. As all imported
7049 declarations seem to be static in all other languages, make them all CU-wide
7050 global only in Ada. */
7051
7052 static struct using_direct **
7053 using_directives (struct dwarf2_cu *cu)
7054 {
7055 if (cu->lang () == language_ada
7056 && cu->get_builder ()->outermost_context_p ())
7057 return cu->get_builder ()->get_global_using_directives ();
7058 else
7059 return cu->get_builder ()->get_local_using_directives ();
7060 }
7061
7062 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
7063 given CU. If the format is not recognized or the attribute is
7064 not present, set it to 0. */
7065
7066 static unsigned int
7067 read_decl_line (struct die_info *die, struct dwarf2_cu *cu)
7068 {
7069 struct attribute *decl_line = dwarf2_attr (die, DW_AT_decl_line, cu);
7070 if (decl_line == nullptr)
7071 return 0;
7072 if (decl_line->form_is_constant ())
7073 {
7074 LONGEST val = decl_line->constant_value (0);
7075 if (0 <= val && val <= UINT_MAX)
7076 return (unsigned int) val;
7077
7078 complaint (_("Declared line for using directive is too large"));
7079 return 0;
7080 }
7081
7082 complaint (_("Declared line for using directive is of incorrect format"));
7083 return 0;
7084 }
7085
7086 /* Read the import statement specified by the given die and record it. */
7087
7088 static void
7089 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7090 {
7091 struct objfile *objfile = cu->per_objfile->objfile;
7092 struct attribute *import_attr;
7093 struct die_info *imported_die, *child_die;
7094 struct dwarf2_cu *imported_cu;
7095 const char *imported_name;
7096 const char *imported_name_prefix;
7097 const char *canonical_name;
7098 const char *import_alias;
7099 const char *imported_declaration = NULL;
7100 const char *import_prefix;
7101 std::vector<const char *> excludes;
7102
7103 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7104 if (import_attr == NULL)
7105 {
7106 complaint (_("Tag '%s' has no DW_AT_import"),
7107 dwarf_tag_name (die->tag));
7108 return;
7109 }
7110
7111 imported_cu = cu;
7112 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7113 imported_name = dwarf2_name (imported_die, imported_cu);
7114 if (imported_name == NULL)
7115 {
7116 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7117
7118 The import in the following code:
7119 namespace A
7120 {
7121 typedef int B;
7122 }
7123
7124 int main ()
7125 {
7126 using A::B;
7127 B b;
7128 return b;
7129 }
7130
7131 ...
7132 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7133 <52> DW_AT_decl_file : 1
7134 <53> DW_AT_decl_line : 6
7135 <54> DW_AT_import : <0x75>
7136 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7137 <59> DW_AT_name : B
7138 <5b> DW_AT_decl_file : 1
7139 <5c> DW_AT_decl_line : 2
7140 <5d> DW_AT_type : <0x6e>
7141 ...
7142 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7143 <76> DW_AT_byte_size : 4
7144 <77> DW_AT_encoding : 5 (signed)
7145
7146 imports the wrong die ( 0x75 instead of 0x58 ).
7147 This case will be ignored until the gcc bug is fixed. */
7148 return;
7149 }
7150
7151 /* Figure out the local name after import. */
7152 import_alias = dwarf2_name (die, cu);
7153
7154 /* Figure out where the statement is being imported to. */
7155 import_prefix = determine_prefix (die, cu);
7156
7157 /* Figure out what the scope of the imported die is and prepend it
7158 to the name of the imported die. */
7159 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7160
7161 if (imported_die->tag != DW_TAG_namespace
7162 && imported_die->tag != DW_TAG_module)
7163 {
7164 imported_declaration = imported_name;
7165 canonical_name = imported_name_prefix;
7166 }
7167 else if (strlen (imported_name_prefix) > 0)
7168 canonical_name = obconcat (&objfile->objfile_obstack,
7169 imported_name_prefix,
7170 (cu->lang () == language_d
7171 ? "."
7172 : "::"),
7173 imported_name, (char *) NULL);
7174 else
7175 canonical_name = imported_name;
7176
7177 if (die->tag == DW_TAG_imported_module
7178 && cu->lang () == language_fortran)
7179 for (child_die = die->child; child_die && child_die->tag;
7180 child_die = child_die->sibling)
7181 {
7182 /* DWARF-4: A Fortran use statement with a “rename list” may be
7183 represented by an imported module entry with an import attribute
7184 referring to the module and owned entries corresponding to those
7185 entities that are renamed as part of being imported. */
7186
7187 if (child_die->tag != DW_TAG_imported_declaration)
7188 {
7189 complaint (_("child DW_TAG_imported_declaration expected "
7190 "- DIE at %s [in module %s]"),
7191 sect_offset_str (child_die->sect_off),
7192 objfile_name (objfile));
7193 continue;
7194 }
7195
7196 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7197 if (import_attr == NULL)
7198 {
7199 complaint (_("Tag '%s' has no DW_AT_import"),
7200 dwarf_tag_name (child_die->tag));
7201 continue;
7202 }
7203
7204 imported_cu = cu;
7205 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7206 &imported_cu);
7207 imported_name = dwarf2_name (imported_die, imported_cu);
7208 if (imported_name == NULL)
7209 {
7210 complaint (_("child DW_TAG_imported_declaration has unknown "
7211 "imported name - DIE at %s [in module %s]"),
7212 sect_offset_str (child_die->sect_off),
7213 objfile_name (objfile));
7214 continue;
7215 }
7216
7217 excludes.push_back (imported_name);
7218
7219 process_die (child_die, cu);
7220 }
7221
7222 add_using_directive (using_directives (cu),
7223 import_prefix,
7224 canonical_name,
7225 import_alias,
7226 imported_declaration,
7227 excludes,
7228 read_decl_line (die, cu),
7229 &objfile->objfile_obstack);
7230 }
7231
7232 /* ICC<14 does not output the required DW_AT_declaration on incomplete
7233 types, but gives them a size of zero. Starting with version 14,
7234 ICC is compatible with GCC. */
7235
7236 static bool
7237 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
7238 {
7239 if (!cu->checked_producer)
7240 check_producer (cu);
7241
7242 return cu->producer_is_icc_lt_14;
7243 }
7244
7245 /* ICC generates a DW_AT_type for C void functions. This was observed on
7246 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
7247 which says that void functions should not have a DW_AT_type. */
7248
7249 static bool
7250 producer_is_icc (struct dwarf2_cu *cu)
7251 {
7252 if (!cu->checked_producer)
7253 check_producer (cu);
7254
7255 return cu->producer_is_icc;
7256 }
7257
7258 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7259 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7260 this, it was first present in GCC release 4.3.0. */
7261
7262 static bool
7263 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7264 {
7265 if (!cu->checked_producer)
7266 check_producer (cu);
7267
7268 return cu->producer_is_gcc_lt_4_3;
7269 }
7270
7271 /* See dwarf2/read.h. */
7272 bool
7273 producer_is_clang (struct dwarf2_cu *cu)
7274 {
7275 if (!cu->checked_producer)
7276 check_producer (cu);
7277
7278 return cu->producer_is_clang;
7279 }
7280
7281 static file_and_directory &
7282 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
7283 {
7284 if (cu->per_cu->fnd != nullptr)
7285 return *cu->per_cu->fnd;
7286
7287 /* Find the filename. Do not use dwarf2_name here, since the filename
7288 is not a source language identifier. */
7289 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
7290 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
7291
7292 if (res.get_comp_dir () == nullptr
7293 && producer_is_gcc_lt_4_3 (cu)
7294 && res.get_name () != nullptr
7295 && IS_ABSOLUTE_PATH (res.get_name ()))
7296 {
7297 res.set_comp_dir (ldirname (res.get_name ()));
7298 res.set_name (make_unique_xstrdup (lbasename (res.get_name ())));
7299 }
7300
7301 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
7302 return *cu->per_cu->fnd;
7303 }
7304
7305 /* Handle DW_AT_stmt_list for a compilation unit.
7306 DIE is the DW_TAG_compile_unit die for CU.
7307 COMP_DIR is the compilation directory. LOWPC is passed to
7308 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
7309
7310 static void
7311 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7312 const file_and_directory &fnd, unrelocated_addr lowpc,
7313 bool have_code) /* ARI: editCase function */
7314 {
7315 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7316 struct attribute *attr;
7317 hashval_t line_header_local_hash;
7318 void **slot;
7319 int decode_mapping;
7320
7321 gdb_assert (! cu->per_cu->is_debug_types);
7322
7323 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7324 if (attr == NULL || !attr->form_is_unsigned ())
7325 return;
7326
7327 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7328
7329 /* The line header hash table is only created if needed (it exists to
7330 prevent redundant reading of the line table for partial_units).
7331 If we're given a partial_unit, we'll need it. If we're given a
7332 compile_unit, then use the line header hash table if it's already
7333 created, but don't create one just yet. */
7334
7335 if (per_objfile->line_header_hash == NULL
7336 && die->tag == DW_TAG_partial_unit)
7337 {
7338 per_objfile->line_header_hash
7339 .reset (htab_create_alloc (127, line_header_hash_voidp,
7340 line_header_eq_voidp,
7341 htab_delete_entry<line_header>,
7342 xcalloc, xfree));
7343 }
7344
7345 line_header line_header_local (line_offset, cu->per_cu->is_dwz);
7346 line_header_local_hash = line_header_hash (&line_header_local);
7347 if (per_objfile->line_header_hash != NULL)
7348 {
7349 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7350 &line_header_local,
7351 line_header_local_hash, NO_INSERT);
7352
7353 /* For DW_TAG_compile_unit we need info like symtab::linetable which
7354 is not present in *SLOT (since if there is something in *SLOT then
7355 it will be for a partial_unit). */
7356 if (die->tag == DW_TAG_partial_unit && slot != NULL)
7357 {
7358 gdb_assert (*slot != NULL);
7359 cu->line_header = (struct line_header *) *slot;
7360 return;
7361 }
7362 }
7363
7364 /* dwarf_decode_line_header does not yet provide sufficient information.
7365 We always have to call also dwarf_decode_lines for it. */
7366 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
7367 fnd.get_comp_dir ());
7368 if (lh == NULL)
7369 return;
7370
7371 cu->line_header = lh.release ();
7372 cu->line_header_die_owner = die;
7373
7374 if (per_objfile->line_header_hash == NULL)
7375 slot = NULL;
7376 else
7377 {
7378 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7379 &line_header_local,
7380 line_header_local_hash, INSERT);
7381 gdb_assert (slot != NULL);
7382 }
7383 if (slot != NULL && *slot == NULL)
7384 {
7385 /* This newly decoded line number information unit will be owned
7386 by line_header_hash hash table. */
7387 *slot = cu->line_header;
7388 cu->line_header_die_owner = NULL;
7389 }
7390 else
7391 {
7392 /* We cannot free any current entry in (*slot) as that struct line_header
7393 may be already used by multiple CUs. Create only temporary decoded
7394 line_header for this CU - it may happen at most once for each line
7395 number information unit. And if we're not using line_header_hash
7396 then this is what we want as well. */
7397 gdb_assert (die->tag != DW_TAG_partial_unit);
7398 }
7399 decode_mapping = (die->tag != DW_TAG_partial_unit);
7400 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
7401 then there won't be any interesting code in the CU, but a check later on
7402 (in lnp_state_machine::check_line_address) will fail to properly exclude
7403 an entry that was removed via --gc-sections. */
7404 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping && have_code);
7405 }
7406
7407 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7408
7409 static void
7410 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7411 {
7412 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7413 struct objfile *objfile = per_objfile->objfile;
7414 CORE_ADDR lowpc;
7415 struct attribute *attr;
7416 struct die_info *child_die;
7417
7418 prepare_one_comp_unit (cu, die, cu->lang ());
7419
7420 unrelocated_addr unrel_low, unrel_high;
7421 get_scope_pc_bounds (die, &unrel_low, &unrel_high, cu);
7422
7423 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7424 from finish_block. */
7425 if (unrel_low == ((unrelocated_addr) -1))
7426 unrel_low = unrel_high;
7427 lowpc = per_objfile->relocate (unrel_low);
7428
7429 file_and_directory &fnd = find_file_and_directory (die, cu);
7430
7431 /* GAS supports generating dwarf-5 info starting version 2.35. Versions
7432 2.35-2.37 generate an incorrect CU name attribute: it's relative,
7433 implicitly prefixing it with the compilation dir. Work around this by
7434 prefixing it with the source dir instead. */
7435 if (cu->header.version == 5 && !IS_ABSOLUTE_PATH (fnd.get_name ())
7436 && producer_is_gas_lt_2_38 (cu))
7437 {
7438 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7439 if (attr != nullptr && attr->form_is_unsigned ())
7440 {
7441 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7442 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
7443 fnd.get_comp_dir ());
7444 if (lh->version == 5 && lh->is_valid_file_index (1))
7445 {
7446 std::string dir = lh->include_dir_at (1);
7447 fnd.set_comp_dir (std::move (dir));
7448 }
7449 }
7450 }
7451
7452 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
7453 lowpc);
7454
7455 gdb_assert (per_objfile->sym_cu == nullptr);
7456 scoped_restore restore_sym_cu
7457 = make_scoped_restore (&per_objfile->sym_cu, cu);
7458
7459 /* Decode line number information if present. We do this before
7460 processing child DIEs, so that the line header table is available
7461 for DW_AT_decl_file. */
7462 handle_DW_AT_stmt_list (die, cu, fnd, unrel_low, unrel_low != unrel_high);
7463
7464 /* Process all dies in compilation unit. */
7465 if (die->child != NULL)
7466 {
7467 child_die = die->child;
7468 while (child_die && child_die->tag)
7469 {
7470 process_die (child_die, cu);
7471 child_die = child_die->sibling;
7472 }
7473 }
7474 per_objfile->sym_cu = nullptr;
7475
7476 /* Decode macro information, if present. Dwarf 2 macro information
7477 refers to information in the line number info statement program
7478 header, so we can only read it if we've read the header
7479 successfully. */
7480 attr = dwarf2_attr (die, DW_AT_macros, cu);
7481 if (attr == NULL)
7482 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7483 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7484 {
7485 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7486 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
7487
7488 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
7489 }
7490 else
7491 {
7492 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7493 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7494 {
7495 unsigned int macro_offset = attr->as_unsigned ();
7496
7497 dwarf_decode_macros (cu, macro_offset, 0);
7498 }
7499 }
7500 }
7501
7502 void
7503 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
7504 {
7505 struct type_unit_group *tu_group;
7506 int first_time;
7507 struct attribute *attr;
7508 unsigned int i;
7509 struct signatured_type *sig_type;
7510
7511 gdb_assert (per_cu->is_debug_types);
7512 sig_type = (struct signatured_type *) per_cu;
7513
7514 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
7515
7516 /* If we're using .gdb_index (includes -readnow) then
7517 per_cu->type_unit_group may not have been set up yet. */
7518 if (sig_type->type_unit_group == NULL)
7519 sig_type->type_unit_group = get_type_unit_group (this, attr);
7520 tu_group = sig_type->type_unit_group;
7521
7522 /* If we've already processed this stmt_list there's no real need to
7523 do it again, we could fake it and just recreate the part we need
7524 (file name,index -> symtab mapping). If data shows this optimization
7525 is useful we can do it then. */
7526 type_unit_group_unshareable *tug_unshare
7527 = per_objfile->get_type_unit_group_unshareable (tu_group);
7528 first_time = tug_unshare->compunit_symtab == NULL;
7529
7530 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7531 debug info. */
7532 line_header_up lh;
7533 if (attr != NULL && attr->form_is_unsigned ())
7534 {
7535 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7536 lh = dwarf_decode_line_header (line_offset, this, nullptr);
7537 }
7538 if (lh == NULL)
7539 {
7540 if (first_time)
7541 start_compunit_symtab ("", NULL, 0);
7542 else
7543 {
7544 gdb_assert (tug_unshare->symtabs == NULL);
7545 gdb_assert (m_builder == nullptr);
7546 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7547 m_builder.reset (new struct buildsym_compunit
7548 (cust->objfile (), "",
7549 cust->dirname (),
7550 cust->language (),
7551 0, cust));
7552 list_in_scope = get_builder ()->get_file_symbols ();
7553 }
7554 return;
7555 }
7556
7557 line_header = lh.release ();
7558 line_header_die_owner = die;
7559
7560 if (first_time)
7561 {
7562 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
7563
7564 /* Note: We don't assign tu_group->compunit_symtab yet because we're
7565 still initializing it, and our caller (a few levels up)
7566 process_full_type_unit still needs to know if this is the first
7567 time. */
7568
7569 tug_unshare->symtabs
7570 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
7571 struct symtab *, line_header->file_names_size ());
7572
7573 auto &file_names = line_header->file_names ();
7574 for (i = 0; i < file_names.size (); ++i)
7575 {
7576 file_entry &fe = file_names[i];
7577 dwarf2_start_subfile (this, fe, *line_header);
7578 buildsym_compunit *b = get_builder ();
7579 subfile *sf = b->get_current_subfile ();
7580
7581 if (sf->symtab == nullptr)
7582 {
7583 /* NOTE: start_subfile will recognize when it's been
7584 passed a file it has already seen. So we can't
7585 assume there's a simple mapping from
7586 cu->line_header->file_names to subfiles, plus
7587 cu->line_header->file_names may contain dups. */
7588 const char *name = sf->name.c_str ();
7589 const char *name_for_id = sf->name_for_id.c_str ();
7590 sf->symtab = allocate_symtab (cust, name, name_for_id);
7591 }
7592
7593 fe.symtab = b->get_current_subfile ()->symtab;
7594 tug_unshare->symtabs[i] = fe.symtab;
7595 }
7596 }
7597 else
7598 {
7599 gdb_assert (m_builder == nullptr);
7600 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7601 m_builder.reset (new struct buildsym_compunit
7602 (cust->objfile (), "",
7603 cust->dirname (),
7604 cust->language (),
7605 0, cust));
7606 list_in_scope = get_builder ()->get_file_symbols ();
7607
7608 auto &file_names = line_header->file_names ();
7609 for (i = 0; i < file_names.size (); ++i)
7610 {
7611 file_entry &fe = file_names[i];
7612 fe.symtab = tug_unshare->symtabs[i];
7613 }
7614 }
7615
7616 /* The main symtab is allocated last. Type units don't have DW_AT_name
7617 so they don't have a "real" (so to speak) symtab anyway.
7618 There is later code that will assign the main symtab to all symbols
7619 that don't have one. We need to handle the case of a symbol with a
7620 missing symtab (DW_AT_decl_file) anyway. */
7621 }
7622
7623 /* Process DW_TAG_type_unit.
7624 For TUs we want to skip the first top level sibling if it's not the
7625 actual type being defined by this TU. In this case the first top
7626 level sibling is there to provide context only. */
7627
7628 static void
7629 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7630 {
7631 struct die_info *child_die;
7632
7633 prepare_one_comp_unit (cu, die, language_minimal);
7634
7635 /* Initialize (or reinitialize) the machinery for building symtabs.
7636 We do this before processing child DIEs, so that the line header table
7637 is available for DW_AT_decl_file. */
7638 cu->setup_type_unit_groups (die);
7639
7640 if (die->child != NULL)
7641 {
7642 child_die = die->child;
7643 while (child_die && child_die->tag)
7644 {
7645 process_die (child_die, cu);
7646 child_die = child_die->sibling;
7647 }
7648 }
7649 }
7650 \f
7651 /* DWO/DWP files.
7652
7653 http://gcc.gnu.org/wiki/DebugFission
7654 http://gcc.gnu.org/wiki/DebugFissionDWP
7655
7656 To simplify handling of both DWO files ("object" files with the DWARF info)
7657 and DWP files (a file with the DWOs packaged up into one file), we treat
7658 DWP files as having a collection of virtual DWO files. */
7659
7660 /* A helper function to hash two file names. This is a separate
7661 function because the hash table uses a search with a different
7662 type. The second file may be NULL. */
7663
7664 static hashval_t
7665 hash_two_files (const char *one, const char *two)
7666 {
7667 hashval_t hash = htab_hash_string (one);
7668 if (two != nullptr)
7669 hash += htab_hash_string (two);
7670 return hash;
7671 }
7672
7673 static hashval_t
7674 hash_dwo_file (const void *item)
7675 {
7676 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
7677 return hash_two_files (dwo_file->dwo_name.c_str (), dwo_file->comp_dir);
7678 }
7679
7680 /* This is used when looking up entries in the DWO hash table. */
7681
7682 struct dwo_file_search
7683 {
7684 /* Name of the DWO to look for. */
7685 const char *dwo_name;
7686 /* Compilation directory to look for. */
7687 const char *comp_dir;
7688
7689 /* Return a hash value compatible with the table. */
7690 hashval_t hash () const
7691 {
7692 return hash_two_files (dwo_name, comp_dir);
7693 }
7694 };
7695
7696 static int
7697 eq_dwo_file (const void *item_lhs, const void *item_rhs)
7698 {
7699 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
7700 const struct dwo_file_search *rhs
7701 = (const struct dwo_file_search *) item_rhs;
7702
7703 if (lhs->dwo_name != rhs->dwo_name)
7704 return 0;
7705 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
7706 return lhs->comp_dir == rhs->comp_dir;
7707 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
7708 }
7709
7710 /* Allocate a hash table for DWO files. */
7711
7712 static htab_up
7713 allocate_dwo_file_hash_table ()
7714 {
7715 return htab_up (htab_create_alloc (41,
7716 hash_dwo_file,
7717 eq_dwo_file,
7718 htab_delete_entry<dwo_file>,
7719 xcalloc, xfree));
7720 }
7721
7722 /* Lookup DWO file DWO_NAME. */
7723
7724 static void **
7725 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
7726 const char *dwo_name,
7727 const char *comp_dir)
7728 {
7729 struct dwo_file_search find_entry;
7730 void **slot;
7731
7732 if (per_objfile->per_bfd->dwo_files == NULL)
7733 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
7734
7735 find_entry.dwo_name = dwo_name;
7736 find_entry.comp_dir = comp_dir;
7737 slot = htab_find_slot_with_hash (per_objfile->per_bfd->dwo_files.get (),
7738 &find_entry, find_entry.hash (),
7739 INSERT);
7740
7741 return slot;
7742 }
7743
7744 static hashval_t
7745 hash_dwo_unit (const void *item)
7746 {
7747 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
7748
7749 /* This drops the top 32 bits of the id, but is ok for a hash. */
7750 return dwo_unit->signature;
7751 }
7752
7753 static int
7754 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7755 {
7756 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
7757 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
7758
7759 /* The signature is assumed to be unique within the DWO file.
7760 So while object file CU dwo_id's always have the value zero,
7761 that's OK, assuming each object file DWO file has only one CU,
7762 and that's the rule for now. */
7763 return lhs->signature == rhs->signature;
7764 }
7765
7766 /* Allocate a hash table for DWO CUs,TUs.
7767 There is one of these tables for each of CUs,TUs for each DWO file. */
7768
7769 static htab_up
7770 allocate_dwo_unit_table ()
7771 {
7772 /* Start out with a pretty small number.
7773 Generally DWO files contain only one CU and maybe some TUs. */
7774 return htab_up (htab_create_alloc (3,
7775 hash_dwo_unit,
7776 eq_dwo_unit,
7777 NULL, xcalloc, xfree));
7778 }
7779
7780 /* die_reader_func for create_dwo_cu. */
7781
7782 static void
7783 create_dwo_cu_reader (const struct die_reader_specs *reader,
7784 const gdb_byte *info_ptr,
7785 struct die_info *comp_unit_die,
7786 struct dwo_file *dwo_file,
7787 struct dwo_unit *dwo_unit)
7788 {
7789 struct dwarf2_cu *cu = reader->cu;
7790 sect_offset sect_off = cu->per_cu->sect_off;
7791 struct dwarf2_section_info *section = cu->per_cu->section;
7792
7793 std::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7794 if (!signature.has_value ())
7795 {
7796 complaint (_("Dwarf Error: debug entry at offset %s is missing"
7797 " its dwo_id [in module %s]"),
7798 sect_offset_str (sect_off), dwo_file->dwo_name.c_str ());
7799 return;
7800 }
7801
7802 dwo_unit->dwo_file = dwo_file;
7803 dwo_unit->signature = *signature;
7804 dwo_unit->section = section;
7805 dwo_unit->sect_off = sect_off;
7806 dwo_unit->length = cu->per_cu->length ();
7807
7808 dwarf_read_debug_printf (" offset %s, dwo_id %s",
7809 sect_offset_str (sect_off),
7810 hex_string (dwo_unit->signature));
7811 }
7812
7813 /* Create the dwo_units for the CUs in a DWO_FILE.
7814 Note: This function processes DWO files only, not DWP files. */
7815
7816 static void
7817 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
7818 dwarf2_cu *cu, struct dwo_file &dwo_file,
7819 dwarf2_section_info &section, htab_up &cus_htab)
7820 {
7821 struct objfile *objfile = per_objfile->objfile;
7822 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7823 const gdb_byte *info_ptr, *end_ptr;
7824
7825 section.read (objfile);
7826 info_ptr = section.buffer;
7827
7828 if (info_ptr == NULL)
7829 return;
7830
7831 dwarf_read_debug_printf ("Reading %s for %s:",
7832 section.get_name (),
7833 section.get_file_name ());
7834
7835 end_ptr = info_ptr + section.size;
7836 while (info_ptr < end_ptr)
7837 {
7838 struct dwarf2_per_cu_data per_cu;
7839 struct dwo_unit read_unit {};
7840 struct dwo_unit *dwo_unit;
7841 void **slot;
7842 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
7843
7844 per_cu.per_bfd = per_bfd;
7845 per_cu.is_debug_types = 0;
7846 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
7847 per_cu.section = &section;
7848
7849 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
7850 if (!reader.dummy_p)
7851 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
7852 &dwo_file, &read_unit);
7853 info_ptr += per_cu.length ();
7854
7855 // If the unit could not be parsed, skip it.
7856 if (read_unit.dwo_file == NULL)
7857 continue;
7858
7859 if (cus_htab == NULL)
7860 cus_htab = allocate_dwo_unit_table ();
7861
7862 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
7863 struct dwo_unit);
7864 *dwo_unit = read_unit;
7865 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
7866 gdb_assert (slot != NULL);
7867 if (*slot != NULL)
7868 {
7869 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
7870 sect_offset dup_sect_off = dup_cu->sect_off;
7871
7872 complaint (_("debug cu entry at offset %s is duplicate to"
7873 " the entry at offset %s, signature %s"),
7874 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7875 hex_string (dwo_unit->signature));
7876 }
7877 *slot = (void *)dwo_unit;
7878 }
7879 }
7880
7881 /* DWP file .debug_{cu,tu}_index section format:
7882 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
7883 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
7884
7885 DWP Versions 1 & 2 are older, pre-standard format versions. The first
7886 officially standard DWP format was published with DWARF v5 and is called
7887 Version 5. There are no versions 3 or 4.
7888
7889 DWP Version 1:
7890
7891 Both index sections have the same format, and serve to map a 64-bit
7892 signature to a set of section numbers. Each section begins with a header,
7893 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
7894 indexes, and a pool of 32-bit section numbers. The index sections will be
7895 aligned at 8-byte boundaries in the file.
7896
7897 The index section header consists of:
7898
7899 V, 32 bit version number
7900 -, 32 bits unused
7901 N, 32 bit number of compilation units or type units in the index
7902 M, 32 bit number of slots in the hash table
7903
7904 Numbers are recorded using the byte order of the application binary.
7905
7906 The hash table begins at offset 16 in the section, and consists of an array
7907 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
7908 order of the application binary). Unused slots in the hash table are 0.
7909 (We rely on the extreme unlikeliness of a signature being exactly 0.)
7910
7911 The parallel table begins immediately after the hash table
7912 (at offset 16 + 8 * M from the beginning of the section), and consists of an
7913 array of 32-bit indexes (using the byte order of the application binary),
7914 corresponding 1-1 with slots in the hash table. Each entry in the parallel
7915 table contains a 32-bit index into the pool of section numbers. For unused
7916 hash table slots, the corresponding entry in the parallel table will be 0.
7917
7918 The pool of section numbers begins immediately following the hash table
7919 (at offset 16 + 12 * M from the beginning of the section). The pool of
7920 section numbers consists of an array of 32-bit words (using the byte order
7921 of the application binary). Each item in the array is indexed starting
7922 from 0. The hash table entry provides the index of the first section
7923 number in the set. Additional section numbers in the set follow, and the
7924 set is terminated by a 0 entry (section number 0 is not used in ELF).
7925
7926 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
7927 section must be the first entry in the set, and the .debug_abbrev.dwo must
7928 be the second entry. Other members of the set may follow in any order.
7929
7930 ---
7931
7932 DWP Versions 2 and 5:
7933
7934 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
7935 and the entries in the index tables are now offsets into these sections.
7936 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
7937 section.
7938
7939 Index Section Contents:
7940 Header
7941 Hash Table of Signatures dwp_hash_table.hash_table
7942 Parallel Table of Indices dwp_hash_table.unit_table
7943 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
7944 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
7945
7946 The index section header consists of:
7947
7948 V, 32 bit version number
7949 L, 32 bit number of columns in the table of section offsets
7950 N, 32 bit number of compilation units or type units in the index
7951 M, 32 bit number of slots in the hash table
7952
7953 Numbers are recorded using the byte order of the application binary.
7954
7955 The hash table has the same format as version 1.
7956 The parallel table of indices has the same format as version 1,
7957 except that the entries are origin-1 indices into the table of sections
7958 offsets and the table of section sizes.
7959
7960 The table of offsets begins immediately following the parallel table
7961 (at offset 16 + 12 * M from the beginning of the section). The table is
7962 a two-dimensional array of 32-bit words (using the byte order of the
7963 application binary), with L columns and N+1 rows, in row-major order.
7964 Each row in the array is indexed starting from 0. The first row provides
7965 a key to the remaining rows: each column in this row provides an identifier
7966 for a debug section, and the offsets in the same column of subsequent rows
7967 refer to that section. The section identifiers for Version 2 are:
7968
7969 DW_SECT_INFO 1 .debug_info.dwo
7970 DW_SECT_TYPES 2 .debug_types.dwo
7971 DW_SECT_ABBREV 3 .debug_abbrev.dwo
7972 DW_SECT_LINE 4 .debug_line.dwo
7973 DW_SECT_LOC 5 .debug_loc.dwo
7974 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
7975 DW_SECT_MACINFO 7 .debug_macinfo.dwo
7976 DW_SECT_MACRO 8 .debug_macro.dwo
7977
7978 The section identifiers for Version 5 are:
7979
7980 DW_SECT_INFO_V5 1 .debug_info.dwo
7981 DW_SECT_RESERVED_V5 2 --
7982 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
7983 DW_SECT_LINE_V5 4 .debug_line.dwo
7984 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
7985 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
7986 DW_SECT_MACRO_V5 7 .debug_macro.dwo
7987 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
7988
7989 The offsets provided by the CU and TU index sections are the base offsets
7990 for the contributions made by each CU or TU to the corresponding section
7991 in the package file. Each CU and TU header contains an abbrev_offset
7992 field, used to find the abbreviations table for that CU or TU within the
7993 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
7994 be interpreted as relative to the base offset given in the index section.
7995 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
7996 should be interpreted as relative to the base offset for .debug_line.dwo,
7997 and offsets into other debug sections obtained from DWARF attributes should
7998 also be interpreted as relative to the corresponding base offset.
7999
8000 The table of sizes begins immediately following the table of offsets.
8001 Like the table of offsets, it is a two-dimensional array of 32-bit words,
8002 with L columns and N rows, in row-major order. Each row in the array is
8003 indexed starting from 1 (row 0 is shared by the two tables).
8004
8005 ---
8006
8007 Hash table lookup is handled the same in version 1 and 2:
8008
8009 We assume that N and M will not exceed 2^32 - 1.
8010 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8011
8012 Given a 64-bit compilation unit signature or a type signature S, an entry
8013 in the hash table is located as follows:
8014
8015 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8016 the low-order k bits all set to 1.
8017
8018 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8019
8020 3) If the hash table entry at index H matches the signature, use that
8021 entry. If the hash table entry at index H is unused (all zeroes),
8022 terminate the search: the signature is not present in the table.
8023
8024 4) Let H = (H + H') modulo M. Repeat at Step 3.
8025
8026 Because M > N and H' and M are relatively prime, the search is guaranteed
8027 to stop at an unused slot or find the match. */
8028
8029 /* Create a hash table to map DWO IDs to their CU/TU entry in
8030 .debug_{info,types}.dwo in DWP_FILE.
8031 Returns NULL if there isn't one.
8032 Note: This function processes DWP files only, not DWO files. */
8033
8034 static struct dwp_hash_table *
8035 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
8036 struct dwp_file *dwp_file, int is_debug_types)
8037 {
8038 struct objfile *objfile = per_objfile->objfile;
8039 bfd *dbfd = dwp_file->dbfd.get ();
8040 const gdb_byte *index_ptr, *index_end;
8041 struct dwarf2_section_info *index;
8042 uint32_t version, nr_columns, nr_units, nr_slots;
8043 struct dwp_hash_table *htab;
8044
8045 if (is_debug_types)
8046 index = &dwp_file->sections.tu_index;
8047 else
8048 index = &dwp_file->sections.cu_index;
8049
8050 if (index->empty ())
8051 return NULL;
8052 index->read (objfile);
8053
8054 index_ptr = index->buffer;
8055 index_end = index_ptr + index->size;
8056
8057 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
8058 For now it's safe to just read 4 bytes (particularly as it's difficult to
8059 tell if you're dealing with Version 5 before you've read the version). */
8060 version = read_4_bytes (dbfd, index_ptr);
8061 index_ptr += 4;
8062 if (version == 2 || version == 5)
8063 nr_columns = read_4_bytes (dbfd, index_ptr);
8064 else
8065 nr_columns = 0;
8066 index_ptr += 4;
8067 nr_units = read_4_bytes (dbfd, index_ptr);
8068 index_ptr += 4;
8069 nr_slots = read_4_bytes (dbfd, index_ptr);
8070 index_ptr += 4;
8071
8072 if (version != 1 && version != 2 && version != 5)
8073 {
8074 error (_("Dwarf Error: unsupported DWP file version (%s)"
8075 " [in module %s]"),
8076 pulongest (version), dwp_file->name);
8077 }
8078 if (nr_slots != (nr_slots & -nr_slots))
8079 {
8080 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
8081 " is not power of 2 [in module %s]"),
8082 pulongest (nr_slots), dwp_file->name);
8083 }
8084
8085 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
8086 htab->version = version;
8087 htab->nr_columns = nr_columns;
8088 htab->nr_units = nr_units;
8089 htab->nr_slots = nr_slots;
8090 htab->hash_table = index_ptr;
8091 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8092
8093 /* Exit early if the table is empty. */
8094 if (nr_slots == 0 || nr_units == 0
8095 || (version == 2 && nr_columns == 0)
8096 || (version == 5 && nr_columns == 0))
8097 {
8098 /* All must be zero. */
8099 if (nr_slots != 0 || nr_units != 0
8100 || (version == 2 && nr_columns != 0)
8101 || (version == 5 && nr_columns != 0))
8102 {
8103 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
8104 " all zero [in modules %s]"),
8105 dwp_file->name);
8106 }
8107 return htab;
8108 }
8109
8110 if (version == 1)
8111 {
8112 htab->section_pool.v1.indices =
8113 htab->unit_table + sizeof (uint32_t) * nr_slots;
8114 /* It's harder to decide whether the section is too small in v1.
8115 V1 is deprecated anyway so we punt. */
8116 }
8117 else if (version == 2)
8118 {
8119 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8120 int *ids = htab->section_pool.v2.section_ids;
8121 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
8122 /* Reverse map for error checking. */
8123 int ids_seen[DW_SECT_MAX + 1];
8124 int i;
8125
8126 if (nr_columns < 2)
8127 {
8128 error (_("Dwarf Error: bad DWP hash table, too few columns"
8129 " in section table [in module %s]"),
8130 dwp_file->name);
8131 }
8132 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
8133 {
8134 error (_("Dwarf Error: bad DWP hash table, too many columns"
8135 " in section table [in module %s]"),
8136 dwp_file->name);
8137 }
8138 memset (ids, 255, sizeof_ids);
8139 memset (ids_seen, 255, sizeof (ids_seen));
8140 for (i = 0; i < nr_columns; ++i)
8141 {
8142 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8143
8144 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
8145 {
8146 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8147 " in section table [in module %s]"),
8148 id, dwp_file->name);
8149 }
8150 if (ids_seen[id] != -1)
8151 {
8152 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8153 " id %d in section table [in module %s]"),
8154 id, dwp_file->name);
8155 }
8156 ids_seen[id] = i;
8157 ids[i] = id;
8158 }
8159 /* Must have exactly one info or types section. */
8160 if (((ids_seen[DW_SECT_INFO] != -1)
8161 + (ids_seen[DW_SECT_TYPES] != -1))
8162 != 1)
8163 {
8164 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8165 " DWO info/types section [in module %s]"),
8166 dwp_file->name);
8167 }
8168 /* Must have an abbrev section. */
8169 if (ids_seen[DW_SECT_ABBREV] == -1)
8170 {
8171 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8172 " section [in module %s]"),
8173 dwp_file->name);
8174 }
8175 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8176 htab->section_pool.v2.sizes =
8177 htab->section_pool.v2.offsets + (sizeof (uint32_t)
8178 * nr_units * nr_columns);
8179 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
8180 * nr_units * nr_columns))
8181 > index_end)
8182 {
8183 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8184 " [in module %s]"),
8185 dwp_file->name);
8186 }
8187 }
8188 else /* version == 5 */
8189 {
8190 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8191 int *ids = htab->section_pool.v5.section_ids;
8192 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
8193 /* Reverse map for error checking. */
8194 int ids_seen[DW_SECT_MAX_V5 + 1];
8195
8196 if (nr_columns < 2)
8197 {
8198 error (_("Dwarf Error: bad DWP hash table, too few columns"
8199 " in section table [in module %s]"),
8200 dwp_file->name);
8201 }
8202 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
8203 {
8204 error (_("Dwarf Error: bad DWP hash table, too many columns"
8205 " in section table [in module %s]"),
8206 dwp_file->name);
8207 }
8208 memset (ids, 255, sizeof_ids);
8209 memset (ids_seen, 255, sizeof (ids_seen));
8210 for (int i = 0; i < nr_columns; ++i)
8211 {
8212 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8213
8214 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
8215 {
8216 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8217 " in section table [in module %s]"),
8218 id, dwp_file->name);
8219 }
8220 if (ids_seen[id] != -1)
8221 {
8222 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8223 " id %d in section table [in module %s]"),
8224 id, dwp_file->name);
8225 }
8226 ids_seen[id] = i;
8227 ids[i] = id;
8228 }
8229 /* Must have seen an info section. */
8230 if (ids_seen[DW_SECT_INFO_V5] == -1)
8231 {
8232 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8233 " DWO info/types section [in module %s]"),
8234 dwp_file->name);
8235 }
8236 /* Must have an abbrev section. */
8237 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
8238 {
8239 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8240 " section [in module %s]"),
8241 dwp_file->name);
8242 }
8243 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8244 htab->section_pool.v5.sizes
8245 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
8246 * nr_units * nr_columns);
8247 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
8248 * nr_units * nr_columns))
8249 > index_end)
8250 {
8251 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8252 " [in module %s]"),
8253 dwp_file->name);
8254 }
8255 }
8256
8257 return htab;
8258 }
8259
8260 /* Update SECTIONS with the data from SECTP.
8261
8262 This function is like the other "locate" section routines, but in
8263 this context the sections to read comes from the DWP V1 hash table,
8264 not the full ELF section table.
8265
8266 The result is non-zero for success, or zero if an error was found. */
8267
8268 static int
8269 locate_v1_virtual_dwo_sections (asection *sectp,
8270 struct virtual_v1_dwo_sections *sections)
8271 {
8272 const struct dwop_section_names *names = &dwop_section_names;
8273
8274 if (names->abbrev_dwo.matches (sectp->name))
8275 {
8276 /* There can be only one. */
8277 if (sections->abbrev.s.section != NULL)
8278 return 0;
8279 sections->abbrev.s.section = sectp;
8280 sections->abbrev.size = bfd_section_size (sectp);
8281 }
8282 else if (names->info_dwo.matches (sectp->name)
8283 || names->types_dwo.matches (sectp->name))
8284 {
8285 /* There can be only one. */
8286 if (sections->info_or_types.s.section != NULL)
8287 return 0;
8288 sections->info_or_types.s.section = sectp;
8289 sections->info_or_types.size = bfd_section_size (sectp);
8290 }
8291 else if (names->line_dwo.matches (sectp->name))
8292 {
8293 /* There can be only one. */
8294 if (sections->line.s.section != NULL)
8295 return 0;
8296 sections->line.s.section = sectp;
8297 sections->line.size = bfd_section_size (sectp);
8298 }
8299 else if (names->loc_dwo.matches (sectp->name))
8300 {
8301 /* There can be only one. */
8302 if (sections->loc.s.section != NULL)
8303 return 0;
8304 sections->loc.s.section = sectp;
8305 sections->loc.size = bfd_section_size (sectp);
8306 }
8307 else if (names->macinfo_dwo.matches (sectp->name))
8308 {
8309 /* There can be only one. */
8310 if (sections->macinfo.s.section != NULL)
8311 return 0;
8312 sections->macinfo.s.section = sectp;
8313 sections->macinfo.size = bfd_section_size (sectp);
8314 }
8315 else if (names->macro_dwo.matches (sectp->name))
8316 {
8317 /* There can be only one. */
8318 if (sections->macro.s.section != NULL)
8319 return 0;
8320 sections->macro.s.section = sectp;
8321 sections->macro.size = bfd_section_size (sectp);
8322 }
8323 else if (names->str_offsets_dwo.matches (sectp->name))
8324 {
8325 /* There can be only one. */
8326 if (sections->str_offsets.s.section != NULL)
8327 return 0;
8328 sections->str_offsets.s.section = sectp;
8329 sections->str_offsets.size = bfd_section_size (sectp);
8330 }
8331 else
8332 {
8333 /* No other kind of section is valid. */
8334 return 0;
8335 }
8336
8337 return 1;
8338 }
8339
8340 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8341 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8342 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8343 This is for DWP version 1 files. */
8344
8345 static struct dwo_unit *
8346 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
8347 struct dwp_file *dwp_file,
8348 uint32_t unit_index,
8349 const char *comp_dir,
8350 ULONGEST signature, int is_debug_types)
8351 {
8352 const struct dwp_hash_table *dwp_htab =
8353 is_debug_types ? dwp_file->tus : dwp_file->cus;
8354 bfd *dbfd = dwp_file->dbfd.get ();
8355 const char *kind = is_debug_types ? "TU" : "CU";
8356 struct dwo_file *dwo_file;
8357 struct dwo_unit *dwo_unit;
8358 struct virtual_v1_dwo_sections sections;
8359 void **dwo_file_slot;
8360 int i;
8361
8362 gdb_assert (dwp_file->version == 1);
8363
8364 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
8365 kind, pulongest (unit_index), hex_string (signature),
8366 dwp_file->name);
8367
8368 /* Fetch the sections of this DWO unit.
8369 Put a limit on the number of sections we look for so that bad data
8370 doesn't cause us to loop forever. */
8371
8372 #define MAX_NR_V1_DWO_SECTIONS \
8373 (1 /* .debug_info or .debug_types */ \
8374 + 1 /* .debug_abbrev */ \
8375 + 1 /* .debug_line */ \
8376 + 1 /* .debug_loc */ \
8377 + 1 /* .debug_str_offsets */ \
8378 + 1 /* .debug_macro or .debug_macinfo */ \
8379 + 1 /* trailing zero */)
8380
8381 memset (&sections, 0, sizeof (sections));
8382
8383 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
8384 {
8385 asection *sectp;
8386 uint32_t section_nr =
8387 read_4_bytes (dbfd,
8388 dwp_htab->section_pool.v1.indices
8389 + (unit_index + i) * sizeof (uint32_t));
8390
8391 if (section_nr == 0)
8392 break;
8393 if (section_nr >= dwp_file->num_sections)
8394 {
8395 error (_("Dwarf Error: bad DWP hash table, section number too large"
8396 " [in module %s]"),
8397 dwp_file->name);
8398 }
8399
8400 sectp = dwp_file->elf_sections[section_nr];
8401 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
8402 {
8403 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8404 " [in module %s]"),
8405 dwp_file->name);
8406 }
8407 }
8408
8409 if (i < 2
8410 || sections.info_or_types.empty ()
8411 || sections.abbrev.empty ())
8412 {
8413 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8414 " [in module %s]"),
8415 dwp_file->name);
8416 }
8417 if (i == MAX_NR_V1_DWO_SECTIONS)
8418 {
8419 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8420 " [in module %s]"),
8421 dwp_file->name);
8422 }
8423
8424 /* It's easier for the rest of the code if we fake a struct dwo_file and
8425 have dwo_unit "live" in that. At least for now.
8426
8427 The DWP file can be made up of a random collection of CUs and TUs.
8428 However, for each CU + set of TUs that came from the same original DWO
8429 file, we can combine them back into a virtual DWO file to save space
8430 (fewer struct dwo_file objects to allocate). Remember that for really
8431 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8432
8433 std::string virtual_dwo_name =
8434 string_printf ("virtual-dwo/%d-%d-%d-%d",
8435 sections.abbrev.get_id (),
8436 sections.line.get_id (),
8437 sections.loc.get_id (),
8438 sections.str_offsets.get_id ());
8439 /* Can we use an existing virtual DWO file? */
8440 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8441 comp_dir);
8442 /* Create one if necessary. */
8443 if (*dwo_file_slot == NULL)
8444 {
8445 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8446 virtual_dwo_name.c_str ());
8447
8448 dwo_file = new struct dwo_file;
8449 dwo_file->dwo_name = std::move (virtual_dwo_name);
8450 dwo_file->comp_dir = comp_dir;
8451 dwo_file->sections.abbrev = sections.abbrev;
8452 dwo_file->sections.line = sections.line;
8453 dwo_file->sections.loc = sections.loc;
8454 dwo_file->sections.macinfo = sections.macinfo;
8455 dwo_file->sections.macro = sections.macro;
8456 dwo_file->sections.str_offsets = sections.str_offsets;
8457 /* The "str" section is global to the entire DWP file. */
8458 dwo_file->sections.str = dwp_file->sections.str;
8459 /* The info or types section is assigned below to dwo_unit,
8460 there's no need to record it in dwo_file.
8461 Also, we can't simply record type sections in dwo_file because
8462 we record a pointer into the vector in dwo_unit. As we collect more
8463 types we'll grow the vector and eventually have to reallocate space
8464 for it, invalidating all copies of pointers into the previous
8465 contents. */
8466 *dwo_file_slot = dwo_file;
8467 }
8468 else
8469 {
8470 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8471 virtual_dwo_name.c_str ());
8472
8473 dwo_file = (struct dwo_file *) *dwo_file_slot;
8474 }
8475
8476 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8477 dwo_unit->dwo_file = dwo_file;
8478 dwo_unit->signature = signature;
8479 dwo_unit->section =
8480 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8481 *dwo_unit->section = sections.info_or_types;
8482 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8483
8484 return dwo_unit;
8485 }
8486
8487 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
8488 simplify them. Given a pointer to the containing section SECTION, and
8489 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
8490 virtual section of just that piece. */
8491
8492 static struct dwarf2_section_info
8493 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
8494 struct dwarf2_section_info *section,
8495 bfd_size_type offset, bfd_size_type size)
8496 {
8497 struct dwarf2_section_info result;
8498 asection *sectp;
8499
8500 gdb_assert (section != NULL);
8501 gdb_assert (!section->is_virtual);
8502
8503 memset (&result, 0, sizeof (result));
8504 result.s.containing_section = section;
8505 result.is_virtual = true;
8506
8507 if (size == 0)
8508 return result;
8509
8510 sectp = section->get_bfd_section ();
8511
8512 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
8513 bounds of the real section. This is a pretty-rare event, so just
8514 flag an error (easier) instead of a warning and trying to cope. */
8515 if (sectp == NULL
8516 || offset + size > bfd_section_size (sectp))
8517 {
8518 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
8519 " in section %s [in module %s]"),
8520 sectp ? bfd_section_name (sectp) : "<unknown>",
8521 objfile_name (per_objfile->objfile));
8522 }
8523
8524 result.virtual_offset = offset;
8525 result.size = size;
8526 return result;
8527 }
8528
8529 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8530 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8531 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8532 This is for DWP version 2 files. */
8533
8534 static struct dwo_unit *
8535 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
8536 struct dwp_file *dwp_file,
8537 uint32_t unit_index,
8538 const char *comp_dir,
8539 ULONGEST signature, int is_debug_types)
8540 {
8541 const struct dwp_hash_table *dwp_htab =
8542 is_debug_types ? dwp_file->tus : dwp_file->cus;
8543 bfd *dbfd = dwp_file->dbfd.get ();
8544 const char *kind = is_debug_types ? "TU" : "CU";
8545 struct dwo_file *dwo_file;
8546 struct dwo_unit *dwo_unit;
8547 struct virtual_v2_or_v5_dwo_sections sections;
8548 void **dwo_file_slot;
8549 int i;
8550
8551 gdb_assert (dwp_file->version == 2);
8552
8553 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
8554 kind, pulongest (unit_index), hex_string (signature),
8555 dwp_file->name);
8556
8557 /* Fetch the section offsets of this DWO unit. */
8558
8559 memset (&sections, 0, sizeof (sections));
8560
8561 for (i = 0; i < dwp_htab->nr_columns; ++i)
8562 {
8563 uint32_t offset = read_4_bytes (dbfd,
8564 dwp_htab->section_pool.v2.offsets
8565 + (((unit_index - 1) * dwp_htab->nr_columns
8566 + i)
8567 * sizeof (uint32_t)));
8568 uint32_t size = read_4_bytes (dbfd,
8569 dwp_htab->section_pool.v2.sizes
8570 + (((unit_index - 1) * dwp_htab->nr_columns
8571 + i)
8572 * sizeof (uint32_t)));
8573
8574 switch (dwp_htab->section_pool.v2.section_ids[i])
8575 {
8576 case DW_SECT_INFO:
8577 case DW_SECT_TYPES:
8578 sections.info_or_types_offset = offset;
8579 sections.info_or_types_size = size;
8580 break;
8581 case DW_SECT_ABBREV:
8582 sections.abbrev_offset = offset;
8583 sections.abbrev_size = size;
8584 break;
8585 case DW_SECT_LINE:
8586 sections.line_offset = offset;
8587 sections.line_size = size;
8588 break;
8589 case DW_SECT_LOC:
8590 sections.loc_offset = offset;
8591 sections.loc_size = size;
8592 break;
8593 case DW_SECT_STR_OFFSETS:
8594 sections.str_offsets_offset = offset;
8595 sections.str_offsets_size = size;
8596 break;
8597 case DW_SECT_MACINFO:
8598 sections.macinfo_offset = offset;
8599 sections.macinfo_size = size;
8600 break;
8601 case DW_SECT_MACRO:
8602 sections.macro_offset = offset;
8603 sections.macro_size = size;
8604 break;
8605 }
8606 }
8607
8608 /* It's easier for the rest of the code if we fake a struct dwo_file and
8609 have dwo_unit "live" in that. At least for now.
8610
8611 The DWP file can be made up of a random collection of CUs and TUs.
8612 However, for each CU + set of TUs that came from the same original DWO
8613 file, we can combine them back into a virtual DWO file to save space
8614 (fewer struct dwo_file objects to allocate). Remember that for really
8615 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8616
8617 std::string virtual_dwo_name =
8618 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
8619 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8620 (long) (sections.line_size ? sections.line_offset : 0),
8621 (long) (sections.loc_size ? sections.loc_offset : 0),
8622 (long) (sections.str_offsets_size
8623 ? sections.str_offsets_offset : 0));
8624 /* Can we use an existing virtual DWO file? */
8625 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8626 comp_dir);
8627 /* Create one if necessary. */
8628 if (*dwo_file_slot == NULL)
8629 {
8630 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8631 virtual_dwo_name.c_str ());
8632
8633 dwo_file = new struct dwo_file;
8634 dwo_file->dwo_name = std::move (virtual_dwo_name);
8635 dwo_file->comp_dir = comp_dir;
8636 dwo_file->sections.abbrev =
8637 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
8638 sections.abbrev_offset,
8639 sections.abbrev_size);
8640 dwo_file->sections.line =
8641 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
8642 sections.line_offset,
8643 sections.line_size);
8644 dwo_file->sections.loc =
8645 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
8646 sections.loc_offset, sections.loc_size);
8647 dwo_file->sections.macinfo =
8648 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
8649 sections.macinfo_offset,
8650 sections.macinfo_size);
8651 dwo_file->sections.macro =
8652 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
8653 sections.macro_offset,
8654 sections.macro_size);
8655 dwo_file->sections.str_offsets =
8656 create_dwp_v2_or_v5_section (per_objfile,
8657 &dwp_file->sections.str_offsets,
8658 sections.str_offsets_offset,
8659 sections.str_offsets_size);
8660 /* The "str" section is global to the entire DWP file. */
8661 dwo_file->sections.str = dwp_file->sections.str;
8662 /* The info or types section is assigned below to dwo_unit,
8663 there's no need to record it in dwo_file.
8664 Also, we can't simply record type sections in dwo_file because
8665 we record a pointer into the vector in dwo_unit. As we collect more
8666 types we'll grow the vector and eventually have to reallocate space
8667 for it, invalidating all copies of pointers into the previous
8668 contents. */
8669 *dwo_file_slot = dwo_file;
8670 }
8671 else
8672 {
8673 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8674 virtual_dwo_name.c_str ());
8675
8676 dwo_file = (struct dwo_file *) *dwo_file_slot;
8677 }
8678
8679 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8680 dwo_unit->dwo_file = dwo_file;
8681 dwo_unit->signature = signature;
8682 dwo_unit->section =
8683 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8684 *dwo_unit->section = create_dwp_v2_or_v5_section
8685 (per_objfile,
8686 is_debug_types
8687 ? &dwp_file->sections.types
8688 : &dwp_file->sections.info,
8689 sections.info_or_types_offset,
8690 sections.info_or_types_size);
8691 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8692
8693 return dwo_unit;
8694 }
8695
8696 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8697 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8698 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8699 This is for DWP version 5 files. */
8700
8701 static struct dwo_unit *
8702 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
8703 struct dwp_file *dwp_file,
8704 uint32_t unit_index,
8705 const char *comp_dir,
8706 ULONGEST signature, int is_debug_types)
8707 {
8708 const struct dwp_hash_table *dwp_htab
8709 = is_debug_types ? dwp_file->tus : dwp_file->cus;
8710 bfd *dbfd = dwp_file->dbfd.get ();
8711 const char *kind = is_debug_types ? "TU" : "CU";
8712 struct dwo_file *dwo_file;
8713 struct dwo_unit *dwo_unit;
8714 struct virtual_v2_or_v5_dwo_sections sections {};
8715 void **dwo_file_slot;
8716
8717 gdb_assert (dwp_file->version == 5);
8718
8719 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
8720 kind, pulongest (unit_index), hex_string (signature),
8721 dwp_file->name);
8722
8723 /* Fetch the section offsets of this DWO unit. */
8724
8725 /* memset (&sections, 0, sizeof (sections)); */
8726
8727 for (int i = 0; i < dwp_htab->nr_columns; ++i)
8728 {
8729 uint32_t offset = read_4_bytes (dbfd,
8730 dwp_htab->section_pool.v5.offsets
8731 + (((unit_index - 1)
8732 * dwp_htab->nr_columns
8733 + i)
8734 * sizeof (uint32_t)));
8735 uint32_t size = read_4_bytes (dbfd,
8736 dwp_htab->section_pool.v5.sizes
8737 + (((unit_index - 1) * dwp_htab->nr_columns
8738 + i)
8739 * sizeof (uint32_t)));
8740
8741 switch (dwp_htab->section_pool.v5.section_ids[i])
8742 {
8743 case DW_SECT_ABBREV_V5:
8744 sections.abbrev_offset = offset;
8745 sections.abbrev_size = size;
8746 break;
8747 case DW_SECT_INFO_V5:
8748 sections.info_or_types_offset = offset;
8749 sections.info_or_types_size = size;
8750 break;
8751 case DW_SECT_LINE_V5:
8752 sections.line_offset = offset;
8753 sections.line_size = size;
8754 break;
8755 case DW_SECT_LOCLISTS_V5:
8756 sections.loclists_offset = offset;
8757 sections.loclists_size = size;
8758 break;
8759 case DW_SECT_MACRO_V5:
8760 sections.macro_offset = offset;
8761 sections.macro_size = size;
8762 break;
8763 case DW_SECT_RNGLISTS_V5:
8764 sections.rnglists_offset = offset;
8765 sections.rnglists_size = size;
8766 break;
8767 case DW_SECT_STR_OFFSETS_V5:
8768 sections.str_offsets_offset = offset;
8769 sections.str_offsets_size = size;
8770 break;
8771 case DW_SECT_RESERVED_V5:
8772 default:
8773 break;
8774 }
8775 }
8776
8777 /* It's easier for the rest of the code if we fake a struct dwo_file and
8778 have dwo_unit "live" in that. At least for now.
8779
8780 The DWP file can be made up of a random collection of CUs and TUs.
8781 However, for each CU + set of TUs that came from the same original DWO
8782 file, we can combine them back into a virtual DWO file to save space
8783 (fewer struct dwo_file objects to allocate). Remember that for really
8784 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8785
8786 std::string virtual_dwo_name =
8787 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
8788 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8789 (long) (sections.line_size ? sections.line_offset : 0),
8790 (long) (sections.loclists_size ? sections.loclists_offset : 0),
8791 (long) (sections.str_offsets_size
8792 ? sections.str_offsets_offset : 0),
8793 (long) (sections.macro_size ? sections.macro_offset : 0),
8794 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
8795 /* Can we use an existing virtual DWO file? */
8796 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
8797 virtual_dwo_name.c_str (),
8798 comp_dir);
8799 /* Create one if necessary. */
8800 if (*dwo_file_slot == NULL)
8801 {
8802 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8803 virtual_dwo_name.c_str ());
8804
8805 dwo_file = new struct dwo_file;
8806 dwo_file->dwo_name = std::move (virtual_dwo_name);
8807 dwo_file->comp_dir = comp_dir;
8808 dwo_file->sections.abbrev =
8809 create_dwp_v2_or_v5_section (per_objfile,
8810 &dwp_file->sections.abbrev,
8811 sections.abbrev_offset,
8812 sections.abbrev_size);
8813 dwo_file->sections.line =
8814 create_dwp_v2_or_v5_section (per_objfile,
8815 &dwp_file->sections.line,
8816 sections.line_offset, sections.line_size);
8817 dwo_file->sections.macro =
8818 create_dwp_v2_or_v5_section (per_objfile,
8819 &dwp_file->sections.macro,
8820 sections.macro_offset,
8821 sections.macro_size);
8822 dwo_file->sections.loclists =
8823 create_dwp_v2_or_v5_section (per_objfile,
8824 &dwp_file->sections.loclists,
8825 sections.loclists_offset,
8826 sections.loclists_size);
8827 dwo_file->sections.rnglists =
8828 create_dwp_v2_or_v5_section (per_objfile,
8829 &dwp_file->sections.rnglists,
8830 sections.rnglists_offset,
8831 sections.rnglists_size);
8832 dwo_file->sections.str_offsets =
8833 create_dwp_v2_or_v5_section (per_objfile,
8834 &dwp_file->sections.str_offsets,
8835 sections.str_offsets_offset,
8836 sections.str_offsets_size);
8837 /* The "str" section is global to the entire DWP file. */
8838 dwo_file->sections.str = dwp_file->sections.str;
8839 /* The info or types section is assigned below to dwo_unit,
8840 there's no need to record it in dwo_file.
8841 Also, we can't simply record type sections in dwo_file because
8842 we record a pointer into the vector in dwo_unit. As we collect more
8843 types we'll grow the vector and eventually have to reallocate space
8844 for it, invalidating all copies of pointers into the previous
8845 contents. */
8846 *dwo_file_slot = dwo_file;
8847 }
8848 else
8849 {
8850 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8851 virtual_dwo_name.c_str ());
8852
8853 dwo_file = (struct dwo_file *) *dwo_file_slot;
8854 }
8855
8856 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8857 dwo_unit->dwo_file = dwo_file;
8858 dwo_unit->signature = signature;
8859 dwo_unit->section
8860 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8861 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
8862 &dwp_file->sections.info,
8863 sections.info_or_types_offset,
8864 sections.info_or_types_size);
8865 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8866
8867 return dwo_unit;
8868 }
8869
8870 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
8871 Returns NULL if the signature isn't found. */
8872
8873 static struct dwo_unit *
8874 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
8875 struct dwp_file *dwp_file, const char *comp_dir,
8876 ULONGEST signature, int is_debug_types)
8877 {
8878 const struct dwp_hash_table *dwp_htab =
8879 is_debug_types ? dwp_file->tus : dwp_file->cus;
8880 bfd *dbfd = dwp_file->dbfd.get ();
8881 uint32_t mask = dwp_htab->nr_slots - 1;
8882 uint32_t hash = signature & mask;
8883 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8884 unsigned int i;
8885 void **slot;
8886 struct dwo_unit find_dwo_cu;
8887
8888 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8889 find_dwo_cu.signature = signature;
8890 slot = htab_find_slot (is_debug_types
8891 ? dwp_file->loaded_tus.get ()
8892 : dwp_file->loaded_cus.get (),
8893 &find_dwo_cu, INSERT);
8894
8895 if (*slot != NULL)
8896 return (struct dwo_unit *) *slot;
8897
8898 /* Use a for loop so that we don't loop forever on bad debug info. */
8899 for (i = 0; i < dwp_htab->nr_slots; ++i)
8900 {
8901 ULONGEST signature_in_table;
8902
8903 signature_in_table =
8904 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
8905 if (signature_in_table == signature)
8906 {
8907 uint32_t unit_index =
8908 read_4_bytes (dbfd,
8909 dwp_htab->unit_table + hash * sizeof (uint32_t));
8910
8911 if (dwp_file->version == 1)
8912 {
8913 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
8914 unit_index, comp_dir,
8915 signature, is_debug_types);
8916 }
8917 else if (dwp_file->version == 2)
8918 {
8919 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
8920 unit_index, comp_dir,
8921 signature, is_debug_types);
8922 }
8923 else /* version == 5 */
8924 {
8925 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
8926 unit_index, comp_dir,
8927 signature, is_debug_types);
8928 }
8929 return (struct dwo_unit *) *slot;
8930 }
8931 if (signature_in_table == 0)
8932 return NULL;
8933 hash = (hash + hash2) & mask;
8934 }
8935
8936 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8937 " [in module %s]"),
8938 dwp_file->name);
8939 }
8940
8941 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
8942 Open the file specified by FILE_NAME and hand it off to BFD for
8943 preliminary analysis. Return a newly initialized bfd *, which
8944 includes a canonicalized copy of FILE_NAME.
8945 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8946 SEARCH_CWD is true if the current directory is to be searched.
8947 It will be searched before debug-file-directory.
8948 If successful, the file is added to the bfd include table of the
8949 objfile's bfd (see gdb_bfd_record_inclusion).
8950 If unable to find/open the file, return NULL.
8951 NOTE: This function is derived from symfile_bfd_open. */
8952
8953 static gdb_bfd_ref_ptr
8954 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
8955 const char *file_name, int is_dwp, int search_cwd)
8956 {
8957 int desc;
8958 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
8959 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
8960 to debug_file_directory. */
8961 const char *search_path;
8962 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
8963
8964 gdb::unique_xmalloc_ptr<char> search_path_holder;
8965 if (search_cwd)
8966 {
8967 if (!debug_file_directory.empty ())
8968 {
8969 search_path_holder.reset (concat (".", dirname_separator_string,
8970 debug_file_directory.c_str (),
8971 (char *) NULL));
8972 search_path = search_path_holder.get ();
8973 }
8974 else
8975 search_path = ".";
8976 }
8977 else
8978 search_path = debug_file_directory.c_str ();
8979
8980 /* Add the path for the executable binary to the list of search paths. */
8981 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
8982 search_path_holder.reset (concat (objfile_dir.c_str (),
8983 dirname_separator_string,
8984 search_path, nullptr));
8985 search_path = search_path_holder.get ();
8986
8987 openp_flags flags = OPF_RETURN_REALPATH;
8988 if (is_dwp)
8989 flags |= OPF_SEARCH_IN_PATH;
8990
8991 gdb::unique_xmalloc_ptr<char> absolute_name;
8992 desc = openp (search_path, flags, file_name,
8993 O_RDONLY | O_BINARY, &absolute_name);
8994 if (desc < 0)
8995 return NULL;
8996
8997 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
8998 gnutarget, desc));
8999 if (sym_bfd == NULL)
9000 return NULL;
9001
9002 if (!bfd_check_format (sym_bfd.get (), bfd_object))
9003 return NULL;
9004
9005 /* Success. Record the bfd as having been included by the objfile's bfd.
9006 This is important because things like demangled_names_hash lives in the
9007 objfile's per_bfd space and may have references to things like symbol
9008 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
9009 gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
9010
9011 return sym_bfd;
9012 }
9013
9014 /* Try to open DWO file FILE_NAME.
9015 COMP_DIR is the DW_AT_comp_dir attribute.
9016 The result is the bfd handle of the file.
9017 If there is a problem finding or opening the file, return NULL.
9018 Upon success, the canonicalized path of the file is stored in the bfd,
9019 same as symfile_bfd_open. */
9020
9021 static gdb_bfd_ref_ptr
9022 open_dwo_file (dwarf2_per_objfile *per_objfile,
9023 const char *file_name, const char *comp_dir)
9024 {
9025 if (IS_ABSOLUTE_PATH (file_name))
9026 return try_open_dwop_file (per_objfile, file_name,
9027 0 /*is_dwp*/, 0 /*search_cwd*/);
9028
9029 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9030
9031 if (comp_dir != NULL)
9032 {
9033 std::string path_to_try = path_join (comp_dir, file_name);
9034
9035 /* NOTE: If comp_dir is a relative path, this will also try the
9036 search path, which seems useful. */
9037 gdb_bfd_ref_ptr abfd (try_open_dwop_file
9038 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
9039
9040 if (abfd != NULL)
9041 return abfd;
9042 }
9043
9044 /* That didn't work, try debug-file-directory, which, despite its name,
9045 is a list of paths. */
9046
9047 if (debug_file_directory.empty ())
9048 return NULL;
9049
9050 return try_open_dwop_file (per_objfile, file_name,
9051 0 /*is_dwp*/, 1 /*search_cwd*/);
9052 }
9053
9054 /* This function is mapped across the sections and remembers the offset and
9055 size of each of the DWO debugging sections we are interested in. */
9056
9057 static void
9058 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
9059 asection *sectp, dwo_sections *dwo_sections)
9060 {
9061 const struct dwop_section_names *names = &dwop_section_names;
9062
9063 struct dwarf2_section_info *dw_sect = nullptr;
9064
9065 if (names->abbrev_dwo.matches (sectp->name))
9066 dw_sect = &dwo_sections->abbrev;
9067 else if (names->info_dwo.matches (sectp->name))
9068 dw_sect = &dwo_sections->info;
9069 else if (names->line_dwo.matches (sectp->name))
9070 dw_sect = &dwo_sections->line;
9071 else if (names->loc_dwo.matches (sectp->name))
9072 dw_sect = &dwo_sections->loc;
9073 else if (names->loclists_dwo.matches (sectp->name))
9074 dw_sect = &dwo_sections->loclists;
9075 else if (names->macinfo_dwo.matches (sectp->name))
9076 dw_sect = &dwo_sections->macinfo;
9077 else if (names->macro_dwo.matches (sectp->name))
9078 dw_sect = &dwo_sections->macro;
9079 else if (names->rnglists_dwo.matches (sectp->name))
9080 dw_sect = &dwo_sections->rnglists;
9081 else if (names->str_dwo.matches (sectp->name))
9082 dw_sect = &dwo_sections->str;
9083 else if (names->str_offsets_dwo.matches (sectp->name))
9084 dw_sect = &dwo_sections->str_offsets;
9085 else if (names->types_dwo.matches (sectp->name))
9086 {
9087 struct dwarf2_section_info type_section;
9088
9089 memset (&type_section, 0, sizeof (type_section));
9090 dwo_sections->types.push_back (type_section);
9091 dw_sect = &dwo_sections->types.back ();
9092 }
9093
9094 if (dw_sect != nullptr)
9095 {
9096 dw_sect->s.section = sectp;
9097 dw_sect->size = bfd_section_size (sectp);
9098 dw_sect->read (objfile);
9099 }
9100 }
9101
9102 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9103 by PER_CU. This is for the non-DWP case.
9104 The result is NULL if DWO_NAME can't be found. */
9105
9106 static struct dwo_file *
9107 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
9108 const char *comp_dir)
9109 {
9110 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9111
9112 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
9113 if (dbfd == NULL)
9114 {
9115 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
9116
9117 return NULL;
9118 }
9119
9120 dwo_file_up dwo_file (new struct dwo_file);
9121 dwo_file->dwo_name = dwo_name;
9122 dwo_file->comp_dir = comp_dir;
9123 dwo_file->dbfd = std::move (dbfd);
9124
9125 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
9126 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
9127 sec, &dwo_file->sections);
9128
9129 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
9130 dwo_file->cus);
9131
9132 if (cu->per_cu->version () < 5)
9133 {
9134 create_debug_types_hash_table (per_objfile, dwo_file.get (),
9135 dwo_file->sections.types, dwo_file->tus);
9136 }
9137 else
9138 {
9139 create_debug_type_hash_table (per_objfile, dwo_file.get (),
9140 &dwo_file->sections.info, dwo_file->tus,
9141 rcuh_kind::COMPILE);
9142 }
9143
9144 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
9145
9146 bfd_cache_close (dwo_file->dbfd.get ());
9147
9148 return dwo_file.release ();
9149 }
9150
9151 /* This function is mapped across the sections and remembers the offset and
9152 size of each of the DWP debugging sections common to version 1 and 2 that
9153 we are interested in. */
9154
9155 static void
9156 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
9157 asection *sectp, dwp_file *dwp_file)
9158 {
9159 const struct dwop_section_names *names = &dwop_section_names;
9160 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9161
9162 /* Record the ELF section number for later lookup: this is what the
9163 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9164 gdb_assert (elf_section_nr < dwp_file->num_sections);
9165 dwp_file->elf_sections[elf_section_nr] = sectp;
9166
9167 /* Look for specific sections that we need. */
9168 struct dwarf2_section_info *dw_sect = nullptr;
9169 if (names->str_dwo.matches (sectp->name))
9170 dw_sect = &dwp_file->sections.str;
9171 else if (names->cu_index.matches (sectp->name))
9172 dw_sect = &dwp_file->sections.cu_index;
9173 else if (names->tu_index.matches (sectp->name))
9174 dw_sect = &dwp_file->sections.tu_index;
9175
9176 if (dw_sect != nullptr)
9177 {
9178 dw_sect->s.section = sectp;
9179 dw_sect->size = bfd_section_size (sectp);
9180 dw_sect->read (objfile);
9181 }
9182 }
9183
9184 /* This function is mapped across the sections and remembers the offset and
9185 size of each of the DWP version 2 debugging sections that we are interested
9186 in. This is split into a separate function because we don't know if we
9187 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9188
9189 static void
9190 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
9191 asection *sectp, void *dwp_file_ptr)
9192 {
9193 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9194 const struct dwop_section_names *names = &dwop_section_names;
9195 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9196
9197 /* Record the ELF section number for later lookup: this is what the
9198 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9199 gdb_assert (elf_section_nr < dwp_file->num_sections);
9200 dwp_file->elf_sections[elf_section_nr] = sectp;
9201
9202 /* Look for specific sections that we need. */
9203 struct dwarf2_section_info *dw_sect = nullptr;
9204 if (names->abbrev_dwo.matches (sectp->name))
9205 dw_sect = &dwp_file->sections.abbrev;
9206 else if (names->info_dwo.matches (sectp->name))
9207 dw_sect = &dwp_file->sections.info;
9208 else if (names->line_dwo.matches (sectp->name))
9209 dw_sect = &dwp_file->sections.line;
9210 else if (names->loc_dwo.matches (sectp->name))
9211 dw_sect = &dwp_file->sections.loc;
9212 else if (names->macinfo_dwo.matches (sectp->name))
9213 dw_sect = &dwp_file->sections.macinfo;
9214 else if (names->macro_dwo.matches (sectp->name))
9215 dw_sect = &dwp_file->sections.macro;
9216 else if (names->str_offsets_dwo.matches (sectp->name))
9217 dw_sect = &dwp_file->sections.str_offsets;
9218 else if (names->types_dwo.matches (sectp->name))
9219 dw_sect = &dwp_file->sections.types;
9220
9221 if (dw_sect != nullptr)
9222 {
9223 dw_sect->s.section = sectp;
9224 dw_sect->size = bfd_section_size (sectp);
9225 dw_sect->read (objfile);
9226 }
9227 }
9228
9229 /* This function is mapped across the sections and remembers the offset and
9230 size of each of the DWP version 5 debugging sections that we are interested
9231 in. This is split into a separate function because we don't know if we
9232 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9233
9234 static void
9235 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
9236 asection *sectp, void *dwp_file_ptr)
9237 {
9238 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9239 const struct dwop_section_names *names = &dwop_section_names;
9240 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9241
9242 /* Record the ELF section number for later lookup: this is what the
9243 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9244 gdb_assert (elf_section_nr < dwp_file->num_sections);
9245 dwp_file->elf_sections[elf_section_nr] = sectp;
9246
9247 /* Look for specific sections that we need. */
9248 struct dwarf2_section_info *dw_sect = nullptr;
9249 if (names->abbrev_dwo.matches (sectp->name))
9250 dw_sect = &dwp_file->sections.abbrev;
9251 else if (names->info_dwo.matches (sectp->name))
9252 dw_sect = &dwp_file->sections.info;
9253 else if (names->line_dwo.matches (sectp->name))
9254 dw_sect = &dwp_file->sections.line;
9255 else if (names->loclists_dwo.matches (sectp->name))
9256 dw_sect = &dwp_file->sections.loclists;
9257 else if (names->macro_dwo.matches (sectp->name))
9258 dw_sect = &dwp_file->sections.macro;
9259 else if (names->rnglists_dwo.matches (sectp->name))
9260 dw_sect = &dwp_file->sections.rnglists;
9261 else if (names->str_offsets_dwo.matches (sectp->name))
9262 dw_sect = &dwp_file->sections.str_offsets;
9263
9264 if (dw_sect != nullptr)
9265 {
9266 dw_sect->s.section = sectp;
9267 dw_sect->size = bfd_section_size (sectp);
9268 dw_sect->read (objfile);
9269 }
9270 }
9271
9272 /* Hash function for dwp_file loaded CUs/TUs. */
9273
9274 static hashval_t
9275 hash_dwp_loaded_cutus (const void *item)
9276 {
9277 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9278
9279 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9280 return dwo_unit->signature;
9281 }
9282
9283 /* Equality function for dwp_file loaded CUs/TUs. */
9284
9285 static int
9286 eq_dwp_loaded_cutus (const void *a, const void *b)
9287 {
9288 const struct dwo_unit *dua = (const struct dwo_unit *) a;
9289 const struct dwo_unit *dub = (const struct dwo_unit *) b;
9290
9291 return dua->signature == dub->signature;
9292 }
9293
9294 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9295
9296 static htab_up
9297 allocate_dwp_loaded_cutus_table ()
9298 {
9299 return htab_up (htab_create_alloc (3,
9300 hash_dwp_loaded_cutus,
9301 eq_dwp_loaded_cutus,
9302 NULL, xcalloc, xfree));
9303 }
9304
9305 /* Try to open DWP file FILE_NAME.
9306 The result is the bfd handle of the file.
9307 If there is a problem finding or opening the file, return NULL.
9308 Upon success, the canonicalized path of the file is stored in the bfd,
9309 same as symfile_bfd_open. */
9310
9311 static gdb_bfd_ref_ptr
9312 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
9313 {
9314 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
9315 1 /*is_dwp*/,
9316 1 /*search_cwd*/));
9317 if (abfd != NULL)
9318 return abfd;
9319
9320 /* Work around upstream bug 15652.
9321 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9322 [Whether that's a "bug" is debatable, but it is getting in our way.]
9323 We have no real idea where the dwp file is, because gdb's realpath-ing
9324 of the executable's path may have discarded the needed info.
9325 [IWBN if the dwp file name was recorded in the executable, akin to
9326 .gnu_debuglink, but that doesn't exist yet.]
9327 Strip the directory from FILE_NAME and search again. */
9328 if (!debug_file_directory.empty ())
9329 {
9330 /* Don't implicitly search the current directory here.
9331 If the user wants to search "." to handle this case,
9332 it must be added to debug-file-directory. */
9333 return try_open_dwop_file (per_objfile, lbasename (file_name),
9334 1 /*is_dwp*/,
9335 0 /*search_cwd*/);
9336 }
9337
9338 return NULL;
9339 }
9340
9341 /* Initialize the use of the DWP file for the current objfile.
9342 By convention the name of the DWP file is ${objfile}.dwp.
9343 The result is NULL if it can't be found. */
9344
9345 static std::unique_ptr<struct dwp_file>
9346 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
9347 {
9348 struct objfile *objfile = per_objfile->objfile;
9349
9350 /* Try to find first .dwp for the binary file before any symbolic links
9351 resolving. */
9352
9353 /* If the objfile is a debug file, find the name of the real binary
9354 file and get the name of dwp file from there. */
9355 std::string dwp_name;
9356 if (objfile->separate_debug_objfile_backlink != NULL)
9357 {
9358 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
9359 const char *backlink_basename = lbasename (backlink->original_name);
9360
9361 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
9362 }
9363 else
9364 dwp_name = objfile->original_name;
9365
9366 dwp_name += ".dwp";
9367
9368 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
9369 if (dbfd == NULL
9370 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
9371 {
9372 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9373 dwp_name = objfile_name (objfile);
9374 dwp_name += ".dwp";
9375 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
9376 }
9377
9378 if (dbfd == NULL)
9379 {
9380 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
9381
9382 return std::unique_ptr<dwp_file> ();
9383 }
9384
9385 const char *name = bfd_get_filename (dbfd.get ());
9386 std::unique_ptr<struct dwp_file> dwp_file
9387 (new struct dwp_file (name, std::move (dbfd)));
9388
9389 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
9390 dwp_file->elf_sections =
9391 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
9392 dwp_file->num_sections, asection *);
9393
9394 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9395 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9396 dwp_file.get ());
9397
9398 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
9399
9400 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
9401
9402 /* The DWP file version is stored in the hash table. Oh well. */
9403 if (dwp_file->cus && dwp_file->tus
9404 && dwp_file->cus->version != dwp_file->tus->version)
9405 {
9406 /* Technically speaking, we should try to limp along, but this is
9407 pretty bizarre. We use pulongest here because that's the established
9408 portability solution (e.g, we cannot use %u for uint32_t). */
9409 error (_("Dwarf Error: DWP file CU version %s doesn't match"
9410 " TU version %s [in DWP file %s]"),
9411 pulongest (dwp_file->cus->version),
9412 pulongest (dwp_file->tus->version), dwp_name.c_str ());
9413 }
9414
9415 if (dwp_file->cus)
9416 dwp_file->version = dwp_file->cus->version;
9417 else if (dwp_file->tus)
9418 dwp_file->version = dwp_file->tus->version;
9419 else
9420 dwp_file->version = 2;
9421
9422 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9423 {
9424 if (dwp_file->version == 2)
9425 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9426 dwp_file.get ());
9427 else
9428 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9429 dwp_file.get ());
9430 }
9431
9432 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
9433 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
9434
9435 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
9436 dwarf_read_debug_printf (" %s CUs, %s TUs",
9437 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9438 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9439
9440 bfd_cache_close (dwp_file->dbfd.get ());
9441
9442 return dwp_file;
9443 }
9444
9445 /* Wrapper around open_and_init_dwp_file, only open it once. */
9446
9447 static struct dwp_file *
9448 get_dwp_file (dwarf2_per_objfile *per_objfile)
9449 {
9450 if (!per_objfile->per_bfd->dwp_checked)
9451 {
9452 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
9453 per_objfile->per_bfd->dwp_checked = 1;
9454 }
9455 return per_objfile->per_bfd->dwp_file.get ();
9456 }
9457
9458 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9459 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9460 or in the DWP file for the objfile, referenced by THIS_UNIT.
9461 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9462 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9463
9464 This is called, for example, when wanting to read a variable with a
9465 complex location. Therefore we don't want to do file i/o for every call.
9466 Therefore we don't want to look for a DWO file on every call.
9467 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9468 then we check if we've already seen DWO_NAME, and only THEN do we check
9469 for a DWO file.
9470
9471 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9472 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9473
9474 static struct dwo_unit *
9475 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9476 ULONGEST signature, int is_debug_types)
9477 {
9478 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9479 struct objfile *objfile = per_objfile->objfile;
9480 const char *kind = is_debug_types ? "TU" : "CU";
9481 void **dwo_file_slot;
9482 struct dwo_file *dwo_file;
9483 struct dwp_file *dwp_file;
9484
9485 /* First see if there's a DWP file.
9486 If we have a DWP file but didn't find the DWO inside it, don't
9487 look for the original DWO file. It makes gdb behave differently
9488 depending on whether one is debugging in the build tree. */
9489
9490 dwp_file = get_dwp_file (per_objfile);
9491 if (dwp_file != NULL)
9492 {
9493 const struct dwp_hash_table *dwp_htab =
9494 is_debug_types ? dwp_file->tus : dwp_file->cus;
9495
9496 if (dwp_htab != NULL)
9497 {
9498 struct dwo_unit *dwo_cutu =
9499 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
9500 is_debug_types);
9501
9502 if (dwo_cutu != NULL)
9503 {
9504 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
9505 kind, hex_string (signature),
9506 host_address_to_string (dwo_cutu));
9507
9508 return dwo_cutu;
9509 }
9510 }
9511 }
9512 else
9513 {
9514 /* No DWP file, look for the DWO file. */
9515
9516 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
9517 if (*dwo_file_slot == NULL)
9518 {
9519 /* Read in the file and build a table of the CUs/TUs it contains. */
9520 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
9521 }
9522 /* NOTE: This will be NULL if unable to open the file. */
9523 dwo_file = (struct dwo_file *) *dwo_file_slot;
9524
9525 if (dwo_file != NULL)
9526 {
9527 struct dwo_unit *dwo_cutu = NULL;
9528
9529 if (is_debug_types && dwo_file->tus)
9530 {
9531 struct dwo_unit find_dwo_cutu;
9532
9533 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9534 find_dwo_cutu.signature = signature;
9535 dwo_cutu
9536 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
9537 &find_dwo_cutu);
9538 }
9539 else if (!is_debug_types && dwo_file->cus)
9540 {
9541 struct dwo_unit find_dwo_cutu;
9542
9543 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9544 find_dwo_cutu.signature = signature;
9545 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
9546 &find_dwo_cutu);
9547 }
9548
9549 if (dwo_cutu != NULL)
9550 {
9551 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
9552 kind, dwo_name, hex_string (signature),
9553 host_address_to_string (dwo_cutu));
9554
9555 return dwo_cutu;
9556 }
9557 }
9558 }
9559
9560 /* We didn't find it. This could mean a dwo_id mismatch, or
9561 someone deleted the DWO/DWP file, or the search path isn't set up
9562 correctly to find the file. */
9563
9564 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
9565 kind, dwo_name, hex_string (signature));
9566
9567 /* This is a warning and not a complaint because it can be caused by
9568 pilot error (e.g., user accidentally deleting the DWO). */
9569 {
9570 /* Print the name of the DWP file if we looked there, helps the user
9571 better diagnose the problem. */
9572 std::string dwp_text;
9573
9574 if (dwp_file != NULL)
9575 dwp_text = string_printf (" [in DWP file %s]",
9576 lbasename (dwp_file->name));
9577
9578 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
9579 " [in module %s]"),
9580 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
9581 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
9582 }
9583 return NULL;
9584 }
9585
9586 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9587 See lookup_dwo_cutu_unit for details. */
9588
9589 static struct dwo_unit *
9590 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9591 ULONGEST signature)
9592 {
9593 gdb_assert (!cu->per_cu->is_debug_types);
9594
9595 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
9596 }
9597
9598 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9599 See lookup_dwo_cutu_unit for details. */
9600
9601 static struct dwo_unit *
9602 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
9603 {
9604 gdb_assert (cu->per_cu->is_debug_types);
9605
9606 signatured_type *sig_type = (signatured_type *) cu->per_cu;
9607
9608 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
9609 }
9610
9611 /* Traversal function for queue_and_load_all_dwo_tus. */
9612
9613 static int
9614 queue_and_load_dwo_tu (void **slot, void *info)
9615 {
9616 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9617 dwarf2_cu *cu = (dwarf2_cu *) info;
9618 ULONGEST signature = dwo_unit->signature;
9619 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
9620
9621 if (sig_type != NULL)
9622 {
9623 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9624 a real dependency of PER_CU on SIG_TYPE. That is detected later
9625 while processing PER_CU. */
9626 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
9627 cu->lang ()))
9628 load_full_type_unit (sig_type, cu->per_objfile);
9629 cu->per_cu->imported_symtabs.push_back (sig_type);
9630 }
9631
9632 return 1;
9633 }
9634
9635 /* Queue all TUs contained in the DWO of CU to be read in.
9636 The DWO may have the only definition of the type, though it may not be
9637 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9638 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9639
9640 static void
9641 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
9642 {
9643 struct dwo_unit *dwo_unit;
9644 struct dwo_file *dwo_file;
9645
9646 gdb_assert (cu != nullptr);
9647 gdb_assert (!cu->per_cu->is_debug_types);
9648 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
9649
9650 dwo_unit = cu->dwo_unit;
9651 gdb_assert (dwo_unit != NULL);
9652
9653 dwo_file = dwo_unit->dwo_file;
9654 if (dwo_file->tus != NULL)
9655 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
9656 }
9657
9658 /* Read in various DIEs. */
9659
9660 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9661 Inherit only the children of the DW_AT_abstract_origin DIE not being
9662 already referenced by DW_AT_abstract_origin from the children of the
9663 current DIE. */
9664
9665 static void
9666 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9667 {
9668 attribute *attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9669 if (attr == nullptr)
9670 return;
9671
9672 /* Note that following die references may follow to a die in a
9673 different CU. */
9674 dwarf2_cu *origin_cu = cu;
9675
9676 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9677 die_info *origin_die = follow_die_ref (die, attr, &origin_cu);
9678
9679 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9680 symbols in. */
9681 struct pending **origin_previous_list_in_scope = origin_cu->list_in_scope;
9682 origin_cu->list_in_scope = cu->list_in_scope;
9683
9684 if (die->tag != origin_die->tag
9685 && !(die->tag == DW_TAG_inlined_subroutine
9686 && origin_die->tag == DW_TAG_subprogram))
9687 complaint (_("DIE %s and its abstract origin %s have different tags"),
9688 sect_offset_str (die->sect_off),
9689 sect_offset_str (origin_die->sect_off));
9690
9691 /* Find if the concrete and abstract trees are structurally the
9692 same. This is a shallow traversal and it is not bullet-proof;
9693 the compiler can trick the debugger into believing that the trees
9694 are isomorphic, whereas they actually are not. However, the
9695 likelihood of this happening is pretty low, and a full-fledged
9696 check would be an overkill. */
9697 bool are_isomorphic = true;
9698 die_info *concrete_child = die->child;
9699 die_info *abstract_child = origin_die->child;
9700 while (concrete_child != nullptr || abstract_child != nullptr)
9701 {
9702 if (concrete_child == nullptr
9703 || abstract_child == nullptr
9704 || concrete_child->tag != abstract_child->tag)
9705 {
9706 are_isomorphic = false;
9707 break;
9708 }
9709
9710 concrete_child = concrete_child->sibling;
9711 abstract_child = abstract_child->sibling;
9712 }
9713
9714 /* Walk the origin's children in parallel to the concrete children.
9715 This helps match an origin child in case the debug info misses
9716 DW_AT_abstract_origin attributes. Keep in mind that the abstract
9717 origin tree may not have the same tree structure as the concrete
9718 DIE, though. */
9719 die_info *corresponding_abstract_child
9720 = are_isomorphic ? origin_die->child : nullptr;
9721
9722 std::vector<sect_offset> offsets;
9723
9724 for (die_info *child_die = die->child;
9725 child_die && child_die->tag;
9726 child_die = child_die->sibling)
9727 {
9728 /* We are trying to process concrete instance entries:
9729 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
9730 it's not relevant to our analysis here. i.e. detecting DIEs that are
9731 present in the abstract instance but not referenced in the concrete
9732 one. */
9733 if (child_die->tag == DW_TAG_call_site
9734 || child_die->tag == DW_TAG_GNU_call_site)
9735 {
9736 if (are_isomorphic)
9737 corresponding_abstract_child
9738 = corresponding_abstract_child->sibling;
9739 continue;
9740 }
9741
9742 /* For each CHILD_DIE, find the corresponding child of
9743 ORIGIN_DIE. If there is more than one layer of
9744 DW_AT_abstract_origin, follow them all; there shouldn't be,
9745 but GCC versions at least through 4.4 generate this (GCC PR
9746 40573). */
9747 die_info *child_origin_die = child_die;
9748 dwarf2_cu *child_origin_cu = cu;
9749 while (true)
9750 {
9751 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9752 child_origin_cu);
9753 if (attr == nullptr)
9754 break;
9755
9756 die_info *prev_child_origin_die = child_origin_die;
9757 child_origin_die = follow_die_ref (child_origin_die, attr,
9758 &child_origin_cu);
9759
9760 if (prev_child_origin_die == child_origin_die)
9761 {
9762 /* Handle DIE with self-reference. */
9763 break;
9764 }
9765 }
9766
9767 /* If missing DW_AT_abstract_origin, try the corresponding child
9768 of the origin. Clang emits such lexical scopes. */
9769 if (child_origin_die == child_die
9770 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
9771 && are_isomorphic
9772 && child_die->tag == DW_TAG_lexical_block)
9773 child_origin_die = corresponding_abstract_child;
9774
9775 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9776 counterpart may exist. */
9777 if (child_origin_die != child_die)
9778 {
9779 if (child_die->tag != child_origin_die->tag
9780 && !(child_die->tag == DW_TAG_inlined_subroutine
9781 && child_origin_die->tag == DW_TAG_subprogram))
9782 complaint (_("Child DIE %s and its abstract origin %s have "
9783 "different tags"),
9784 sect_offset_str (child_die->sect_off),
9785 sect_offset_str (child_origin_die->sect_off));
9786 if (child_origin_die->parent != origin_die)
9787 complaint (_("Child DIE %s and its abstract origin %s have "
9788 "different parents"),
9789 sect_offset_str (child_die->sect_off),
9790 sect_offset_str (child_origin_die->sect_off));
9791 else
9792 offsets.push_back (child_origin_die->sect_off);
9793 }
9794
9795 if (are_isomorphic)
9796 corresponding_abstract_child = corresponding_abstract_child->sibling;
9797 }
9798
9799 if (!offsets.empty ())
9800 {
9801 std::sort (offsets.begin (), offsets.end ());
9802
9803 for (auto offsets_it = offsets.begin () + 1;
9804 offsets_it < offsets.end ();
9805 ++offsets_it)
9806 if (*(offsets_it - 1) == *offsets_it)
9807 complaint (_("Multiple children of DIE %s refer "
9808 "to DIE %s as their abstract origin"),
9809 sect_offset_str (die->sect_off),
9810 sect_offset_str (*offsets_it));
9811 }
9812
9813 auto offsets_it = offsets.begin ();
9814 die_info *origin_child_die = origin_die->child;
9815 while (origin_child_die != nullptr && origin_child_die->tag != 0)
9816 {
9817 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9818 while (offsets_it < offsets.end ()
9819 && *offsets_it < origin_child_die->sect_off)
9820 ++offsets_it;
9821
9822 if (offsets_it == offsets.end ()
9823 || *offsets_it > origin_child_die->sect_off)
9824 {
9825 /* Found that ORIGIN_CHILD_DIE is really not referenced.
9826 Check whether we're already processing ORIGIN_CHILD_DIE.
9827 This can happen with mutually referenced abstract_origins.
9828 PR 16581. */
9829 if (!origin_child_die->in_process)
9830 process_die (origin_child_die, origin_cu);
9831 }
9832
9833 origin_child_die = origin_child_die->sibling;
9834 }
9835
9836 origin_cu->list_in_scope = origin_previous_list_in_scope;
9837
9838 if (cu != origin_cu)
9839 compute_delayed_physnames (origin_cu);
9840 }
9841
9842 /* Return TRUE if the given DIE is the program's "main". DWARF 4 has
9843 defined a dedicated DW_AT_main_subprogram attribute to indicate the
9844 starting function of the program, however with older versions the
9845 DW_CC_program value of the DW_AT_calling_convention attribute was
9846 used instead as the only means available. We handle both variants. */
9847
9848 static bool
9849 dwarf2_func_is_main_p (struct die_info *die, struct dwarf2_cu *cu)
9850 {
9851 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
9852 return true;
9853 struct attribute *attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
9854 return (attr != nullptr
9855 && attr->constant_value (DW_CC_normal) == DW_CC_program);
9856 }
9857
9858 /* A helper to handle Ada's "Pragma Import" feature when it is applied
9859 to a function. */
9860
9861 static bool
9862 check_ada_pragma_import (struct die_info *die, struct dwarf2_cu *cu)
9863 {
9864 /* A Pragma Import will have both a name and a linkage name. */
9865 const char *name = dwarf2_name (die, cu);
9866 if (name == nullptr)
9867 return false;
9868
9869 const char *linkage_name = dw2_linkage_name (die, cu);
9870 /* Disallow the special Ada symbols. */
9871 if (!is_ada_import_or_export (cu, name, linkage_name))
9872 return false;
9873
9874 /* A Pragma Import will be a declaration, while a Pragma Export will
9875 not be. */
9876 if (!die_is_declaration (die, cu))
9877 return false;
9878
9879 new_symbol (die, read_type_die (die, cu), cu);
9880 return true;
9881 }
9882
9883 static void
9884 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9885 {
9886 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9887 struct objfile *objfile = per_objfile->objfile;
9888 struct gdbarch *gdbarch = objfile->arch ();
9889 struct context_stack *newobj;
9890 CORE_ADDR lowpc;
9891 CORE_ADDR highpc;
9892 struct die_info *child_die;
9893 struct attribute *attr, *call_line, *call_file;
9894 const char *name;
9895 struct block *block;
9896 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9897 std::vector<struct symbol *> template_args;
9898 struct template_symbol *templ_func = NULL;
9899
9900 if (inlined_func)
9901 {
9902 /* If we do not have call site information, we can't show the
9903 caller of this inlined function. That's too confusing, so
9904 only use the scope for local variables. */
9905 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9906 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9907 if (call_line == NULL || call_file == NULL)
9908 {
9909 read_lexical_block_scope (die, cu);
9910 return;
9911 }
9912 }
9913
9914 name = dwarf2_name (die, cu);
9915 if (name == nullptr)
9916 name = dw2_linkage_name (die, cu);
9917
9918 /* Ignore functions with missing or empty names. These are actually
9919 illegal according to the DWARF standard. */
9920 if (name == NULL)
9921 {
9922 complaint (_("missing name for subprogram DIE at %s"),
9923 sect_offset_str (die->sect_off));
9924 return;
9925 }
9926
9927 if (check_ada_pragma_import (die, cu))
9928 {
9929 /* We already made the symbol for the Pragma Import, and because
9930 it is a declaration, we know it won't have any other
9931 important information, so we can simply return. */
9932 return;
9933 }
9934
9935 /* Ignore functions with missing or invalid low and high pc attributes. */
9936 unrelocated_addr unrel_low, unrel_high;
9937 if (dwarf2_get_pc_bounds (die, &unrel_low, &unrel_high, cu, nullptr, nullptr)
9938 <= PC_BOUNDS_INVALID)
9939 {
9940 if (have_complaint ())
9941 {
9942 attr = dwarf2_attr (die, DW_AT_external, cu);
9943 bool external_p = attr != nullptr && attr->as_boolean ();
9944 attr = dwarf2_attr (die, DW_AT_inline, cu);
9945 bool inlined_p
9946 = (attr != nullptr
9947 && attr->is_nonnegative ()
9948 && (attr->as_nonnegative () == DW_INL_inlined
9949 || attr->as_nonnegative () == DW_INL_declared_inlined));
9950 attr = dwarf2_attr (die, DW_AT_declaration, cu);
9951 bool decl_p = attr != nullptr && attr->as_boolean ();
9952 if (!external_p && !inlined_p && !decl_p)
9953 complaint (_("cannot get low and high bounds "
9954 "for subprogram DIE at %s"),
9955 sect_offset_str (die->sect_off));
9956 }
9957 return;
9958 }
9959
9960 lowpc = per_objfile->relocate (unrel_low);
9961 highpc = per_objfile->relocate (unrel_high);
9962
9963 if (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_arm
9964 && producer_is_gas_ge_2_39 (cu))
9965 {
9966 /* Gas version 2.39 produces DWARF for a Thumb subprogram with a low_pc
9967 attribute with the thumb bit set (PR gas/31115). Work around this. */
9968 lowpc = gdbarch_addr_bits_remove (gdbarch, lowpc);
9969 highpc = gdbarch_addr_bits_remove (gdbarch, highpc);
9970 }
9971
9972 /* If we have any template arguments, then we must allocate a
9973 different sort of symbol. */
9974 for (child_die = die->child; child_die; child_die = child_die->sibling)
9975 {
9976 if (child_die->tag == DW_TAG_template_type_param
9977 || child_die->tag == DW_TAG_template_value_param)
9978 {
9979 templ_func = new (&objfile->objfile_obstack) template_symbol;
9980 templ_func->subclass = SYMBOL_TEMPLATE;
9981 break;
9982 }
9983 }
9984
9985 gdb_assert (cu->get_builder () != nullptr);
9986 newobj = cu->get_builder ()->push_context (0, lowpc);
9987 newobj->name = new_symbol (die, read_type_die (die, cu), cu, templ_func);
9988
9989 if (dwarf2_func_is_main_p (die, cu))
9990 set_objfile_main_name (objfile, newobj->name->linkage_name (),
9991 cu->lang ());
9992
9993 /* If there is a location expression for DW_AT_frame_base, record
9994 it. */
9995 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9996 if (attr != nullptr)
9997 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
9998
9999 /* If there is a location for the static link, record it. */
10000 newobj->static_link = NULL;
10001 attr = dwarf2_attr (die, DW_AT_static_link, cu);
10002 if (attr != nullptr)
10003 {
10004 newobj->static_link
10005 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
10006 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
10007 cu->addr_type ());
10008 }
10009
10010 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
10011
10012 if (die->child != NULL)
10013 {
10014 child_die = die->child;
10015 while (child_die && child_die->tag)
10016 {
10017 if (child_die->tag == DW_TAG_template_type_param
10018 || child_die->tag == DW_TAG_template_value_param)
10019 {
10020 struct symbol *arg = new_symbol (child_die, NULL, cu);
10021
10022 if (arg != NULL)
10023 template_args.push_back (arg);
10024 }
10025 else
10026 process_die (child_die, cu);
10027 child_die = child_die->sibling;
10028 }
10029 }
10030
10031 inherit_abstract_dies (die, cu);
10032
10033 /* If we have a DW_AT_specification, we might need to import using
10034 directives from the context of the specification DIE. See the
10035 comment in determine_prefix. */
10036 if (cu->lang () == language_cplus
10037 && dwarf2_attr (die, DW_AT_specification, cu))
10038 {
10039 struct dwarf2_cu *spec_cu = cu;
10040 struct die_info *spec_die = die_specification (die, &spec_cu);
10041
10042 while (spec_die)
10043 {
10044 child_die = spec_die->child;
10045 while (child_die && child_die->tag)
10046 {
10047 if (child_die->tag == DW_TAG_imported_module)
10048 process_die (child_die, spec_cu);
10049 child_die = child_die->sibling;
10050 }
10051
10052 /* In some cases, GCC generates specification DIEs that
10053 themselves contain DW_AT_specification attributes. */
10054 spec_die = die_specification (spec_die, &spec_cu);
10055 }
10056 }
10057
10058 struct context_stack cstk = cu->get_builder ()->pop_context ();
10059 /* Make a block for the local symbols within. */
10060 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
10061 cstk.static_link, lowpc, highpc);
10062
10063 /* For C++, set the block's scope. */
10064 if ((cu->lang () == language_cplus
10065 || cu->lang () == language_fortran
10066 || cu->lang () == language_d
10067 || cu->lang () == language_rust)
10068 && cu->processing_has_namespace_info)
10069 block->set_scope (determine_prefix (die, cu),
10070 &objfile->objfile_obstack);
10071
10072 /* If we have address ranges, record them. */
10073 dwarf2_record_block_ranges (die, block, cu);
10074
10075 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
10076
10077 /* Attach template arguments to function. */
10078 if (!template_args.empty ())
10079 {
10080 gdb_assert (templ_func != NULL);
10081
10082 templ_func->n_template_arguments = template_args.size ();
10083 templ_func->template_arguments
10084 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
10085 templ_func->n_template_arguments);
10086 memcpy (templ_func->template_arguments,
10087 template_args.data (),
10088 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10089
10090 /* Make sure that the symtab is set on the new symbols. Even
10091 though they don't appear in this symtab directly, other parts
10092 of gdb assume that symbols do, and this is reasonably
10093 true. */
10094 for (symbol *sym : template_args)
10095 sym->set_symtab (templ_func->symtab ());
10096 }
10097
10098 /* In C++, we can have functions nested inside functions (e.g., when
10099 a function declares a class that has methods). This means that
10100 when we finish processing a function scope, we may need to go
10101 back to building a containing block's symbol lists. */
10102 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10103 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10104
10105 /* If we've finished processing a top-level function, subsequent
10106 symbols go in the file symbol list. */
10107 if (cu->get_builder ()->outermost_context_p ())
10108 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
10109 }
10110
10111 /* Process all the DIES contained within a lexical block scope. Start
10112 a new scope, process the dies, and then close the scope. */
10113
10114 static void
10115 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10116 {
10117 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10118 CORE_ADDR lowpc, highpc;
10119 struct die_info *child_die;
10120
10121 /* Ignore blocks with missing or invalid low and high pc attributes. */
10122 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10123 as multiple lexical blocks? Handling children in a sane way would
10124 be nasty. Might be easier to properly extend generic blocks to
10125 describe ranges. */
10126 unrelocated_addr unrel_low, unrel_high;
10127 switch (dwarf2_get_pc_bounds (die, &unrel_low, &unrel_high, cu,
10128 nullptr, nullptr))
10129 {
10130 case PC_BOUNDS_NOT_PRESENT:
10131 /* DW_TAG_lexical_block has no attributes, process its children as if
10132 there was no wrapping by that DW_TAG_lexical_block.
10133 GCC does no longer produces such DWARF since GCC r224161. */
10134 for (child_die = die->child;
10135 child_die != NULL && child_die->tag;
10136 child_die = child_die->sibling)
10137 {
10138 /* We might already be processing this DIE. This can happen
10139 in an unusual circumstance -- where a subroutine A
10140 appears lexically in another subroutine B, but A actually
10141 inlines B. The recursion is broken here, rather than in
10142 inherit_abstract_dies, because it seems better to simply
10143 drop concrete children here. */
10144 if (!child_die->in_process)
10145 process_die (child_die, cu);
10146 }
10147 return;
10148 case PC_BOUNDS_INVALID:
10149 return;
10150 }
10151 lowpc = per_objfile->relocate (unrel_low);
10152 highpc = per_objfile->relocate (unrel_high);
10153
10154 cu->get_builder ()->push_context (0, lowpc);
10155 if (die->child != NULL)
10156 {
10157 child_die = die->child;
10158 while (child_die && child_die->tag)
10159 {
10160 process_die (child_die, cu);
10161 child_die = child_die->sibling;
10162 }
10163 }
10164 inherit_abstract_dies (die, cu);
10165 struct context_stack cstk = cu->get_builder ()->pop_context ();
10166
10167 if (*cu->get_builder ()->get_local_symbols () != NULL
10168 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
10169 {
10170 struct block *block
10171 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
10172 cstk.start_addr, highpc);
10173
10174 /* Note that recording ranges after traversing children, as we
10175 do here, means that recording a parent's ranges entails
10176 walking across all its children's ranges as they appear in
10177 the address map, which is quadratic behavior.
10178
10179 It would be nicer to record the parent's ranges before
10180 traversing its children, simply overriding whatever you find
10181 there. But since we don't even decide whether to create a
10182 block until after we've traversed its children, that's hard
10183 to do. */
10184 dwarf2_record_block_ranges (die, block, cu);
10185 }
10186 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10187 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10188 }
10189
10190 static void dwarf2_ranges_read_low_addrs
10191 (unsigned offset,
10192 struct dwarf2_cu *cu,
10193 dwarf_tag tag,
10194 std::vector<unrelocated_addr> &result);
10195
10196 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
10197
10198 static void
10199 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10200 {
10201 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10202 struct objfile *objfile = per_objfile->objfile;
10203 struct gdbarch *gdbarch = objfile->arch ();
10204 struct attribute *attr;
10205 void **slot;
10206 int nparams;
10207 struct die_info *child_die;
10208
10209 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
10210 if (attr == NULL)
10211 {
10212 /* This was a pre-DWARF-5 GNU extension alias
10213 for DW_AT_call_return_pc. */
10214 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10215 }
10216 if (!attr)
10217 {
10218 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
10219 "DIE %s [in module %s]"),
10220 sect_offset_str (die->sect_off), objfile_name (objfile));
10221 return;
10222 }
10223 unrelocated_addr pc = attr->as_address ();
10224
10225 if (cu->call_site_htab == NULL)
10226 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
10227 call_site::eq, NULL,
10228 &objfile->objfile_obstack,
10229 hashtab_obstack_allocate, NULL);
10230 struct call_site call_site_local (pc, nullptr, nullptr);
10231 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10232 if (*slot != NULL)
10233 {
10234 complaint (_("Duplicate PC %s for DW_TAG_call_site "
10235 "DIE %s [in module %s]"),
10236 paddress (gdbarch, (CORE_ADDR) pc), sect_offset_str (die->sect_off),
10237 objfile_name (objfile));
10238 return;
10239 }
10240
10241 /* Count parameters at the caller. */
10242
10243 nparams = 0;
10244 for (child_die = die->child; child_die && child_die->tag;
10245 child_die = child_die->sibling)
10246 {
10247 if (child_die->tag != DW_TAG_call_site_parameter
10248 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10249 {
10250 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
10251 "DW_TAG_call_site child DIE %s [in module %s]"),
10252 child_die->tag, sect_offset_str (child_die->sect_off),
10253 objfile_name (objfile));
10254 continue;
10255 }
10256
10257 nparams++;
10258 }
10259
10260 struct call_site *call_site
10261 = new (XOBNEWVAR (&objfile->objfile_obstack,
10262 struct call_site,
10263 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
10264 struct call_site (pc, cu->per_cu, per_objfile);
10265 *slot = call_site;
10266
10267 /* We never call the destructor of call_site, so we must ensure it is
10268 trivially destructible. */
10269 static_assert(std::is_trivially_destructible<struct call_site>::value);
10270
10271 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
10272 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10273 {
10274 struct die_info *func_die;
10275
10276 /* Skip also over DW_TAG_inlined_subroutine. */
10277 for (func_die = die->parent;
10278 func_die && func_die->tag != DW_TAG_subprogram
10279 && func_die->tag != DW_TAG_subroutine_type;
10280 func_die = func_die->parent);
10281
10282 /* DW_AT_call_all_calls is a superset
10283 of DW_AT_call_all_tail_calls. */
10284 if (func_die
10285 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
10286 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10287 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
10288 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10289 {
10290 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10291 not complete. But keep CALL_SITE for look ups via call_site_htab,
10292 both the initial caller containing the real return address PC and
10293 the final callee containing the current PC of a chain of tail
10294 calls do not need to have the tail call list complete. But any
10295 function candidate for a virtual tail call frame searched via
10296 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10297 determined unambiguously. */
10298 }
10299 else
10300 {
10301 struct type *func_type = NULL;
10302
10303 if (func_die)
10304 func_type = get_die_type (func_die, cu);
10305 if (func_type != NULL)
10306 {
10307 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
10308
10309 /* Enlist this call site to the function. */
10310 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10311 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10312 }
10313 else
10314 complaint (_("Cannot find function owning DW_TAG_call_site "
10315 "DIE %s [in module %s]"),
10316 sect_offset_str (die->sect_off), objfile_name (objfile));
10317 }
10318 }
10319
10320 attr = dwarf2_attr (die, DW_AT_call_target, cu);
10321 if (attr == NULL)
10322 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10323 if (attr == NULL)
10324 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
10325 if (attr == NULL)
10326 {
10327 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
10328 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10329 }
10330
10331 call_site->target.set_loc_dwarf_block (nullptr);
10332 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
10333 /* Keep NULL DWARF_BLOCK. */;
10334 else if (attr->form_is_block ())
10335 {
10336 struct dwarf2_locexpr_baton *dlbaton;
10337 struct dwarf_block *block = attr->as_block ();
10338
10339 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
10340 dlbaton->data = block->data;
10341 dlbaton->size = block->size;
10342 dlbaton->per_objfile = per_objfile;
10343 dlbaton->per_cu = cu->per_cu;
10344
10345 call_site->target.set_loc_dwarf_block (dlbaton);
10346 }
10347 else if (attr->form_is_ref ())
10348 {
10349 struct dwarf2_cu *target_cu = cu;
10350 struct die_info *target_die;
10351
10352 target_die = follow_die_ref (die, attr, &target_cu);
10353 gdb_assert (target_cu->per_objfile->objfile == objfile);
10354
10355 struct attribute *ranges_attr
10356 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
10357
10358 if (die_is_declaration (target_die, target_cu))
10359 {
10360 const char *target_physname;
10361
10362 /* Prefer the mangled name; otherwise compute the demangled one. */
10363 target_physname = dw2_linkage_name (target_die, target_cu);
10364 if (target_physname == NULL)
10365 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10366 if (target_physname == NULL)
10367 complaint (_("DW_AT_call_target target DIE has invalid "
10368 "physname, for referencing DIE %s [in module %s]"),
10369 sect_offset_str (die->sect_off), objfile_name (objfile));
10370 else
10371 call_site->target.set_loc_physname (target_physname);
10372 }
10373 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
10374 {
10375 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
10376 + target_cu->gnu_ranges_base);
10377 std::vector<unrelocated_addr> addresses;
10378 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
10379 target_die->tag, addresses);
10380 unrelocated_addr *saved = XOBNEWVEC (&objfile->objfile_obstack,
10381 unrelocated_addr,
10382 addresses.size ());
10383 std::copy (addresses.begin (), addresses.end (), saved);
10384 call_site->target.set_loc_array (addresses.size (), saved);
10385 }
10386 else
10387 {
10388 unrelocated_addr lowpc;
10389
10390 /* DW_AT_entry_pc should be preferred. */
10391 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
10392 nullptr, nullptr)
10393 <= PC_BOUNDS_INVALID)
10394 complaint (_("DW_AT_call_target target DIE has invalid "
10395 "low pc, for referencing DIE %s [in module %s]"),
10396 sect_offset_str (die->sect_off), objfile_name (objfile));
10397 else
10398 call_site->target.set_loc_physaddr (lowpc);
10399 }
10400 }
10401 else
10402 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
10403 "block nor reference, for DIE %s [in module %s]"),
10404 sect_offset_str (die->sect_off), objfile_name (objfile));
10405
10406 for (child_die = die->child;
10407 child_die && child_die->tag;
10408 child_die = child_die->sibling)
10409 {
10410 struct call_site_parameter *parameter;
10411 struct attribute *loc, *origin;
10412
10413 if (child_die->tag != DW_TAG_call_site_parameter
10414 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10415 {
10416 /* Already printed the complaint above. */
10417 continue;
10418 }
10419
10420 gdb_assert (call_site->parameter_count < nparams);
10421 parameter = &call_site->parameter[call_site->parameter_count];
10422
10423 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10424 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10425 register is contained in DW_AT_call_value. */
10426
10427 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10428 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
10429 if (origin == NULL)
10430 {
10431 /* This was a pre-DWARF-5 GNU extension alias
10432 for DW_AT_call_parameter. */
10433 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10434 }
10435 if (loc == NULL && origin != NULL && origin->form_is_ref ())
10436 {
10437 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10438
10439 sect_offset sect_off = origin->get_ref_die_offset ();
10440 if (!cu->header.offset_in_cu_p (sect_off))
10441 {
10442 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10443 binding can be done only inside one CU. Such referenced DIE
10444 therefore cannot be even moved to DW_TAG_partial_unit. */
10445 complaint (_("DW_AT_call_parameter offset is not in CU for "
10446 "DW_TAG_call_site child DIE %s [in module %s]"),
10447 sect_offset_str (child_die->sect_off),
10448 objfile_name (objfile));
10449 continue;
10450 }
10451 parameter->u.param_cu_off
10452 = (cu_offset) (sect_off - cu->header.sect_off);
10453 }
10454 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
10455 {
10456 complaint (_("No DW_FORM_block* DW_AT_location for "
10457 "DW_TAG_call_site child DIE %s [in module %s]"),
10458 sect_offset_str (child_die->sect_off), objfile_name (objfile));
10459 continue;
10460 }
10461 else
10462 {
10463 struct dwarf_block *block = loc->as_block ();
10464
10465 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10466 (block->data, &block->data[block->size]);
10467 if (parameter->u.dwarf_reg != -1)
10468 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10469 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
10470 &block->data[block->size],
10471 &parameter->u.fb_offset))
10472 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10473 else
10474 {
10475 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
10476 "for DW_FORM_block* DW_AT_location is supported for "
10477 "DW_TAG_call_site child DIE %s "
10478 "[in module %s]"),
10479 sect_offset_str (child_die->sect_off),
10480 objfile_name (objfile));
10481 continue;
10482 }
10483 }
10484
10485 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
10486 if (attr == NULL)
10487 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10488 if (attr == NULL || !attr->form_is_block ())
10489 {
10490 complaint (_("No DW_FORM_block* DW_AT_call_value for "
10491 "DW_TAG_call_site child DIE %s [in module %s]"),
10492 sect_offset_str (child_die->sect_off),
10493 objfile_name (objfile));
10494 continue;
10495 }
10496
10497 struct dwarf_block *block = attr->as_block ();
10498 parameter->value = block->data;
10499 parameter->value_size = block->size;
10500
10501 /* Parameters are not pre-cleared by memset above. */
10502 parameter->data_value = NULL;
10503 parameter->data_value_size = 0;
10504 call_site->parameter_count++;
10505
10506 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
10507 if (attr == NULL)
10508 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10509 if (attr != nullptr)
10510 {
10511 if (!attr->form_is_block ())
10512 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
10513 "DW_TAG_call_site child DIE %s [in module %s]"),
10514 sect_offset_str (child_die->sect_off),
10515 objfile_name (objfile));
10516 else
10517 {
10518 block = attr->as_block ();
10519 parameter->data_value = block->data;
10520 parameter->data_value_size = block->size;
10521 }
10522 }
10523 }
10524 }
10525
10526 /* Helper function for read_variable. If DIE represents a virtual
10527 table, then return the type of the concrete object that is
10528 associated with the virtual table. Otherwise, return NULL. */
10529
10530 static struct type *
10531 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10532 {
10533 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
10534 if (attr == NULL)
10535 return NULL;
10536
10537 /* Find the type DIE. */
10538 struct die_info *type_die = NULL;
10539 struct dwarf2_cu *type_cu = cu;
10540
10541 if (attr->form_is_ref ())
10542 type_die = follow_die_ref (die, attr, &type_cu);
10543 if (type_die == NULL)
10544 return NULL;
10545
10546 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
10547 return NULL;
10548 return die_containing_type (type_die, type_cu);
10549 }
10550
10551 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
10552
10553 static void
10554 read_variable (struct die_info *die, struct dwarf2_cu *cu)
10555 {
10556 struct rust_vtable_symbol *storage = NULL;
10557
10558 if (cu->lang () == language_rust)
10559 {
10560 struct type *containing_type = rust_containing_type (die, cu);
10561
10562 if (containing_type != NULL)
10563 {
10564 struct objfile *objfile = cu->per_objfile->objfile;
10565
10566 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
10567 storage->concrete_type = containing_type;
10568 storage->subclass = SYMBOL_RUST_VTABLE;
10569 }
10570 }
10571
10572 struct symbol *res = new_symbol (die, NULL, cu, storage);
10573 struct attribute *abstract_origin
10574 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10575 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
10576 if (res == NULL && loc && abstract_origin)
10577 {
10578 /* We have a variable without a name, but with a location and an abstract
10579 origin. This may be a concrete instance of an abstract variable
10580 referenced from an DW_OP_GNU_variable_value, so save it to find it back
10581 later. */
10582 struct dwarf2_cu *origin_cu = cu;
10583 struct die_info *origin_die
10584 = follow_die_ref (die, abstract_origin, &origin_cu);
10585 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10586 per_objfile->per_bfd->abstract_to_concrete
10587 [origin_die->sect_off].push_back (die->sect_off);
10588 }
10589 }
10590
10591 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
10592 reading .debug_rnglists.
10593 Callback's type should be:
10594 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10595 Return true if the attributes are present and valid, otherwise,
10596 return false. */
10597
10598 template <typename Callback>
10599 static bool
10600 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
10601 dwarf_tag tag, Callback &&callback)
10602 {
10603 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10604 struct objfile *objfile = per_objfile->objfile;
10605 bfd *obfd = objfile->obfd.get ();
10606 /* Base address selection entry. */
10607 std::optional<unrelocated_addr> base;
10608 const gdb_byte *buffer;
10609 bool overflow = false;
10610 ULONGEST addr_index;
10611 struct dwarf2_section_info *rnglists_section;
10612
10613 base = cu->base_address;
10614 rnglists_section = cu_debug_rnglists_section (cu, tag);
10615 rnglists_section->read (objfile);
10616
10617 if (offset >= rnglists_section->size)
10618 {
10619 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10620 offset);
10621 return false;
10622 }
10623 buffer = rnglists_section->buffer + offset;
10624
10625 while (1)
10626 {
10627 /* Initialize it due to a false compiler warning. */
10628 unrelocated_addr range_beginning = {}, range_end = {};
10629 const gdb_byte *buf_end = (rnglists_section->buffer
10630 + rnglists_section->size);
10631 unsigned int bytes_read;
10632
10633 if (buffer == buf_end)
10634 {
10635 overflow = true;
10636 break;
10637 }
10638 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
10639 switch (rlet)
10640 {
10641 case DW_RLE_end_of_list:
10642 break;
10643 case DW_RLE_base_address:
10644 if (buffer + cu->header.addr_size > buf_end)
10645 {
10646 overflow = true;
10647 break;
10648 }
10649 base = cu->header.read_address (obfd, buffer, &bytes_read);
10650 buffer += bytes_read;
10651 break;
10652 case DW_RLE_base_addressx:
10653 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10654 buffer += bytes_read;
10655 base = read_addr_index (cu, addr_index);
10656 break;
10657 case DW_RLE_start_length:
10658 if (buffer + cu->header.addr_size > buf_end)
10659 {
10660 overflow = true;
10661 break;
10662 }
10663 range_beginning = cu->header.read_address (obfd, buffer,
10664 &bytes_read);
10665 buffer += bytes_read;
10666 range_end
10667 = (unrelocated_addr) ((CORE_ADDR) range_beginning
10668 + read_unsigned_leb128 (obfd, buffer,
10669 &bytes_read));
10670 buffer += bytes_read;
10671 if (buffer > buf_end)
10672 {
10673 overflow = true;
10674 break;
10675 }
10676 break;
10677 case DW_RLE_startx_length:
10678 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10679 buffer += bytes_read;
10680 range_beginning = read_addr_index (cu, addr_index);
10681 if (buffer > buf_end)
10682 {
10683 overflow = true;
10684 break;
10685 }
10686 range_end
10687 = (unrelocated_addr) ((CORE_ADDR) range_beginning
10688 + read_unsigned_leb128 (obfd, buffer,
10689 &bytes_read));
10690 buffer += bytes_read;
10691 break;
10692 case DW_RLE_offset_pair:
10693 range_beginning = (unrelocated_addr) read_unsigned_leb128 (obfd, buffer,
10694 &bytes_read);
10695 buffer += bytes_read;
10696 if (buffer > buf_end)
10697 {
10698 overflow = true;
10699 break;
10700 }
10701 range_end = (unrelocated_addr) read_unsigned_leb128 (obfd, buffer,
10702 &bytes_read);
10703 buffer += bytes_read;
10704 if (buffer > buf_end)
10705 {
10706 overflow = true;
10707 break;
10708 }
10709 break;
10710 case DW_RLE_start_end:
10711 if (buffer + 2 * cu->header.addr_size > buf_end)
10712 {
10713 overflow = true;
10714 break;
10715 }
10716 range_beginning = cu->header.read_address (obfd, buffer,
10717 &bytes_read);
10718 buffer += bytes_read;
10719 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
10720 buffer += bytes_read;
10721 break;
10722 case DW_RLE_startx_endx:
10723 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10724 buffer += bytes_read;
10725 range_beginning = read_addr_index (cu, addr_index);
10726 if (buffer > buf_end)
10727 {
10728 overflow = true;
10729 break;
10730 }
10731 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10732 buffer += bytes_read;
10733 range_end = read_addr_index (cu, addr_index);
10734 break;
10735 default:
10736 complaint (_("Invalid .debug_rnglists data (no base address)"));
10737 return false;
10738 }
10739 if (rlet == DW_RLE_end_of_list || overflow)
10740 break;
10741 if (rlet == DW_RLE_base_address)
10742 continue;
10743
10744 if (range_beginning > range_end)
10745 {
10746 /* Inverted range entries are invalid. */
10747 complaint (_("Invalid .debug_rnglists data (inverted range)"));
10748 return false;
10749 }
10750
10751 /* Empty range entries have no effect. */
10752 if (range_beginning == range_end)
10753 continue;
10754
10755 /* Only DW_RLE_offset_pair needs the base address added. */
10756 if (rlet == DW_RLE_offset_pair)
10757 {
10758 if (!base.has_value ())
10759 {
10760 /* We have no valid base address for the DW_RLE_offset_pair. */
10761 complaint (_("Invalid .debug_rnglists data (no base address for "
10762 "DW_RLE_offset_pair)"));
10763 return false;
10764 }
10765
10766 range_beginning = (unrelocated_addr) ((CORE_ADDR) range_beginning
10767 + (CORE_ADDR) *base);
10768 range_end = (unrelocated_addr) ((CORE_ADDR) range_end
10769 + (CORE_ADDR) *base);
10770 }
10771
10772 /* A not-uncommon case of bad debug info.
10773 Don't pollute the addrmap with bad data. */
10774 if (range_beginning == (unrelocated_addr) 0
10775 && !per_objfile->per_bfd->has_section_at_zero)
10776 {
10777 complaint (_(".debug_rnglists entry has start address of zero"
10778 " [in module %s]"), objfile_name (objfile));
10779 continue;
10780 }
10781
10782 callback (range_beginning, range_end);
10783 }
10784
10785 if (overflow)
10786 {
10787 complaint (_("Offset %d is not terminated "
10788 "for DW_AT_ranges attribute"),
10789 offset);
10790 return false;
10791 }
10792
10793 return true;
10794 }
10795
10796 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
10797 Callback's type should be:
10798 void (unrelocated_addr range_beginning, unrelocated_addr range_end)
10799 Return 1 if the attributes are present and valid, otherwise, return 0. */
10800
10801 template <typename Callback>
10802 static int
10803 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
10804 Callback &&callback)
10805 {
10806 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10807 struct objfile *objfile = per_objfile->objfile;
10808 struct comp_unit_head *cu_header = &cu->header;
10809 bfd *obfd = objfile->obfd.get ();
10810 unsigned int addr_size = cu_header->addr_size;
10811 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10812 /* Base address selection entry. */
10813 std::optional<unrelocated_addr> base;
10814 unsigned int dummy;
10815 const gdb_byte *buffer;
10816
10817 if (cu_header->version >= 5)
10818 return dwarf2_rnglists_process (offset, cu, tag, callback);
10819
10820 base = cu->base_address;
10821
10822 per_objfile->per_bfd->ranges.read (objfile);
10823 if (offset >= per_objfile->per_bfd->ranges.size)
10824 {
10825 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10826 offset);
10827 return 0;
10828 }
10829 buffer = per_objfile->per_bfd->ranges.buffer + offset;
10830
10831 while (1)
10832 {
10833 unrelocated_addr range_beginning, range_end;
10834
10835 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
10836 buffer += addr_size;
10837 range_end = cu->header.read_address (obfd, buffer, &dummy);
10838 buffer += addr_size;
10839 offset += 2 * addr_size;
10840
10841 /* An end of list marker is a pair of zero addresses. */
10842 if (range_beginning == (unrelocated_addr) 0
10843 && range_end == (unrelocated_addr) 0)
10844 /* Found the end of list entry. */
10845 break;
10846
10847 /* Each base address selection entry is a pair of 2 values.
10848 The first is the largest possible address, the second is
10849 the base address. Check for a base address here. */
10850 if (((CORE_ADDR) range_beginning & mask) == mask)
10851 {
10852 /* If we found the largest possible address, then we already
10853 have the base address in range_end. */
10854 base = range_end;
10855 continue;
10856 }
10857
10858 if (!base.has_value ())
10859 {
10860 /* We have no valid base address for the ranges
10861 data. */
10862 complaint (_("Invalid .debug_ranges data (no base address)"));
10863 return 0;
10864 }
10865
10866 if (range_beginning > range_end)
10867 {
10868 /* Inverted range entries are invalid. */
10869 complaint (_("Invalid .debug_ranges data (inverted range)"));
10870 return 0;
10871 }
10872
10873 /* Empty range entries have no effect. */
10874 if (range_beginning == range_end)
10875 continue;
10876
10877 range_beginning = (unrelocated_addr) ((CORE_ADDR) range_beginning
10878 + (CORE_ADDR) *base);
10879 range_end = (unrelocated_addr) ((CORE_ADDR) range_end
10880 + (CORE_ADDR) *base);
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_ranges 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 return 1;
10896 }
10897
10898 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10899 Return 1 if the attributes are present and valid, otherwise, return 0.
10900 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
10901 ranges in MAP are set, using DATUM as the value. */
10902
10903 static int
10904 dwarf2_ranges_read (unsigned offset, unrelocated_addr *low_return,
10905 unrelocated_addr *high_return, struct dwarf2_cu *cu,
10906 addrmap_mutable *map, void *datum, dwarf_tag tag)
10907 {
10908 int low_set = 0;
10909 unrelocated_addr low = {};
10910 unrelocated_addr high = {};
10911 int retval;
10912
10913 retval = dwarf2_ranges_process (offset, cu, tag,
10914 [&] (unrelocated_addr range_beginning, unrelocated_addr range_end)
10915 {
10916 if (map != nullptr)
10917 {
10918 /* addrmap only accepts CORE_ADDR, so we must cast here. */
10919 map->set_empty ((CORE_ADDR) range_beginning,
10920 (CORE_ADDR) range_end - 1,
10921 datum);
10922 }
10923
10924 /* FIXME: This is recording everything as a low-high
10925 segment of consecutive addresses. We should have a
10926 data structure for discontiguous block ranges
10927 instead. */
10928 if (! low_set)
10929 {
10930 low = range_beginning;
10931 high = range_end;
10932 low_set = 1;
10933 }
10934 else
10935 {
10936 if (range_beginning < low)
10937 low = range_beginning;
10938 if (range_end > high)
10939 high = range_end;
10940 }
10941 });
10942 if (!retval)
10943 return 0;
10944
10945 if (! low_set)
10946 /* If the first entry is an end-of-list marker, the range
10947 describes an empty scope, i.e. no instructions. */
10948 return 0;
10949
10950 if (low_return)
10951 *low_return = low;
10952 if (high_return)
10953 *high_return = high;
10954 return 1;
10955 }
10956
10957 /* Process ranges and fill in a vector of the low PC values only. */
10958
10959 static void
10960 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
10961 dwarf_tag tag,
10962 std::vector<unrelocated_addr> &result)
10963 {
10964 dwarf2_ranges_process (offset, cu, tag,
10965 [&] (unrelocated_addr start, unrelocated_addr end)
10966 {
10967 result.push_back (start);
10968 });
10969 }
10970
10971 /* Determine the low and high pc of a DW_TAG_entry_point. */
10972
10973 static pc_bounds_kind
10974 dwarf2_get_pc_bounds_entry_point (die_info *die, unrelocated_addr *low,
10975 unrelocated_addr *high, dwarf2_cu *cu)
10976 {
10977 gdb_assert (low != nullptr);
10978 gdb_assert (high != nullptr);
10979
10980 if (die->parent->tag != DW_TAG_subprogram)
10981 {
10982 complaint (_("DW_TAG_entry_point not embedded in DW_TAG_subprogram"));
10983 return PC_BOUNDS_INVALID;
10984 }
10985
10986 /* A DW_TAG_entry_point is embedded in an subprogram. Therefore, we can use
10987 the highpc from its enveloping subprogram and get the lowpc from
10988 DWARF. */
10989 const enum pc_bounds_kind bounds_kind = dwarf2_get_pc_bounds (die->parent,
10990 low, high,
10991 cu, nullptr,
10992 nullptr);
10993 if (bounds_kind == PC_BOUNDS_INVALID || bounds_kind == PC_BOUNDS_NOT_PRESENT)
10994 return bounds_kind;
10995
10996 attribute *attr_low = dwarf2_attr (die, DW_AT_low_pc, cu);
10997 if (!attr_low)
10998 {
10999 complaint (_("DW_TAG_entry_point is missing DW_AT_low_pc"));
11000 return PC_BOUNDS_INVALID;
11001 }
11002 *low = attr_low->as_address ();
11003 return bounds_kind;
11004 }
11005
11006 /* Determine the low and high pc using the DW_AT_low_pc and DW_AT_high_pc or
11007 DW_AT_ranges attributes of a DIE. */
11008
11009 static pc_bounds_kind
11010 dwarf_get_pc_bounds_ranges_or_highlow_pc (die_info *die, unrelocated_addr *low,
11011 unrelocated_addr *high, dwarf2_cu *cu,
11012 addrmap_mutable *map, void *datum)
11013 {
11014 gdb_assert (low != nullptr);
11015 gdb_assert (high != nullptr);
11016
11017 struct attribute *attr;
11018 struct attribute *attr_high;
11019 enum pc_bounds_kind ret;
11020
11021 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11022 if (attr_high)
11023 {
11024 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11025 if (attr != nullptr)
11026 {
11027 *low = attr->as_address ();
11028 *high = attr_high->as_address ();
11029 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11030 *high = (unrelocated_addr) ((ULONGEST) *high + (ULONGEST) *low);
11031
11032 /* Found consecutive range of addresses. */
11033 ret = PC_BOUNDS_HIGH_LOW;
11034 }
11035 else
11036 {
11037 /* Found high w/o low attribute. */
11038 ret = PC_BOUNDS_INVALID;
11039 }
11040 }
11041 else
11042 {
11043 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11044 if (attr != nullptr && attr->form_is_unsigned ())
11045 {
11046 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11047 on DWARF version). */
11048 ULONGEST ranges_offset = attr->as_unsigned ();
11049
11050 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11051 this value. */
11052 if (die->tag != DW_TAG_compile_unit)
11053 ranges_offset += cu->gnu_ranges_base;
11054
11055 /* Value of the DW_AT_ranges attribute is the offset in the
11056 .debug_ranges section. */
11057 if (!dwarf2_ranges_read (ranges_offset, low, high, cu,
11058 map, datum, die->tag))
11059 return PC_BOUNDS_INVALID;
11060 /* Found discontinuous range of addresses. */
11061 ret = PC_BOUNDS_RANGES;
11062 }
11063 else
11064 {
11065 /* Could not find high_pc or ranges attributed and thus no bounds
11066 pair. */
11067 ret = PC_BOUNDS_NOT_PRESENT;
11068 }
11069 }
11070
11071 return ret;
11072 }
11073
11074 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
11075 definition for the return value. *LOWPC and *HIGHPC are set iff
11076 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
11077
11078 static enum pc_bounds_kind
11079 dwarf2_get_pc_bounds (struct die_info *die, unrelocated_addr *lowpc,
11080 unrelocated_addr *highpc, struct dwarf2_cu *cu,
11081 addrmap_mutable *map, void *datum)
11082 {
11083 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11084
11085 unrelocated_addr low = {};
11086 unrelocated_addr high = {};
11087 enum pc_bounds_kind ret;
11088
11089 if (die->tag == DW_TAG_entry_point)
11090 ret = dwarf2_get_pc_bounds_entry_point (die, &low, &high, cu);
11091 else
11092 ret = dwarf_get_pc_bounds_ranges_or_highlow_pc (die, &low, &high, cu, map,
11093 datum);
11094
11095 if (ret == PC_BOUNDS_NOT_PRESENT || ret == PC_BOUNDS_INVALID)
11096 return ret;
11097
11098 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
11099 if (high <= low)
11100 return PC_BOUNDS_INVALID;
11101
11102 /* When using the GNU linker, .gnu.linkonce. sections are used to
11103 eliminate duplicate copies of functions and vtables and such.
11104 The linker will arbitrarily choose one and discard the others.
11105 The AT_*_pc values for such functions refer to local labels in
11106 these sections. If the section from that file was discarded, the
11107 labels are not in the output, so the relocs get a value of 0.
11108 If this is a discarded function, mark the pc bounds as invalid,
11109 so that GDB will ignore it. */
11110 if (low == (unrelocated_addr) 0
11111 && !per_objfile->per_bfd->has_section_at_zero)
11112 return PC_BOUNDS_INVALID;
11113
11114 gdb_assert (lowpc != nullptr);
11115 *lowpc = low;
11116 if (highpc != nullptr)
11117 *highpc = high;
11118 return ret;
11119 }
11120
11121 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
11122 its low and high PC addresses. Do nothing if these addresses could not
11123 be determined. Otherwise, set LOWPC to the low address if it is smaller,
11124 and HIGHPC to the high address if greater than HIGHPC. */
11125
11126 static void
11127 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
11128 unrelocated_addr *lowpc,
11129 unrelocated_addr *highpc,
11130 struct dwarf2_cu *cu)
11131 {
11132 unrelocated_addr low, high;
11133 struct die_info *child = die->child;
11134
11135 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
11136 >= PC_BOUNDS_RANGES)
11137 {
11138 *lowpc = std::min (*lowpc, low);
11139 *highpc = std::max (*highpc, high);
11140 }
11141
11142 /* If the language does not allow nested subprograms (either inside
11143 subprograms or lexical blocks), we're done. */
11144 if (cu->lang () != language_ada)
11145 return;
11146
11147 /* Check all the children of the given DIE. If it contains nested
11148 subprograms, then check their pc bounds. Likewise, we need to
11149 check lexical blocks as well, as they may also contain subprogram
11150 definitions. */
11151 while (child && child->tag)
11152 {
11153 if (child->tag == DW_TAG_subprogram
11154 || child->tag == DW_TAG_lexical_block)
11155 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
11156 child = child->sibling;
11157 }
11158 }
11159
11160 /* Get the low and high pc's represented by the scope DIE, and store
11161 them in *LOWPC and *HIGHPC. If the correct values can't be
11162 determined, set *LOWPC to -1 and *HIGHPC to 0. */
11163
11164 static void
11165 get_scope_pc_bounds (struct die_info *die,
11166 unrelocated_addr *lowpc, unrelocated_addr *highpc,
11167 struct dwarf2_cu *cu)
11168 {
11169 unrelocated_addr best_low = (unrelocated_addr) -1;
11170 unrelocated_addr best_high = {};
11171 unrelocated_addr current_low, current_high;
11172
11173 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
11174 nullptr, nullptr)
11175 >= PC_BOUNDS_RANGES)
11176 {
11177 best_low = current_low;
11178 best_high = current_high;
11179 }
11180 else
11181 {
11182 struct die_info *child = die->child;
11183
11184 while (child && child->tag)
11185 {
11186 switch (child->tag) {
11187 case DW_TAG_subprogram:
11188 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
11189 break;
11190 case DW_TAG_namespace:
11191 case DW_TAG_module:
11192 /* FIXME: carlton/2004-01-16: Should we do this for
11193 DW_TAG_class_type/DW_TAG_structure_type, too? I think
11194 that current GCC's always emit the DIEs corresponding
11195 to definitions of methods of classes as children of a
11196 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11197 the DIEs giving the declarations, which could be
11198 anywhere). But I don't see any reason why the
11199 standards says that they have to be there. */
11200 get_scope_pc_bounds (child, &current_low, &current_high, cu);
11201
11202 if (current_low != ((unrelocated_addr) -1))
11203 {
11204 best_low = std::min (best_low, current_low);
11205 best_high = std::max (best_high, current_high);
11206 }
11207 break;
11208 default:
11209 /* Ignore. */
11210 break;
11211 }
11212
11213 child = child->sibling;
11214 }
11215 }
11216
11217 *lowpc = best_low;
11218 *highpc = best_high;
11219 }
11220
11221 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
11222 in DIE. */
11223
11224 static void
11225 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
11226 struct dwarf2_cu *cu)
11227 {
11228 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11229 struct objfile *objfile = per_objfile->objfile;
11230 struct attribute *attr;
11231 struct attribute *attr_high;
11232
11233 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11234 if (attr_high)
11235 {
11236 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11237 if (attr != nullptr)
11238 {
11239 unrelocated_addr unrel_low = attr->as_address ();
11240 unrelocated_addr unrel_high = attr_high->as_address ();
11241
11242 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11243 unrel_high = (unrelocated_addr) ((ULONGEST) unrel_high
11244 + (ULONGEST) unrel_low);
11245
11246 CORE_ADDR low = per_objfile->relocate (unrel_low);
11247 CORE_ADDR high = per_objfile->relocate (unrel_high);
11248 cu->get_builder ()->record_block_range (block, low, high - 1);
11249 }
11250 }
11251
11252 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11253 if (attr != nullptr && attr->form_is_unsigned ())
11254 {
11255 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11256 on DWARF version). */
11257 ULONGEST ranges_offset = attr->as_unsigned ();
11258
11259 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11260 this value. */
11261 if (die->tag != DW_TAG_compile_unit)
11262 ranges_offset += cu->gnu_ranges_base;
11263
11264 std::vector<blockrange> blockvec;
11265 dwarf2_ranges_process (ranges_offset, cu, die->tag,
11266 [&] (unrelocated_addr start, unrelocated_addr end)
11267 {
11268 CORE_ADDR abs_start = per_objfile->relocate (start);
11269 CORE_ADDR abs_end = per_objfile->relocate (end);
11270 cu->get_builder ()->record_block_range (block, abs_start,
11271 abs_end - 1);
11272 blockvec.emplace_back (abs_start, abs_end);
11273 });
11274
11275 block->set_ranges (make_blockranges (objfile, blockvec));
11276 }
11277 }
11278
11279 /* Check whether the producer field indicates either of GCC < 4.6, or the
11280 Intel C/C++ compiler, and cache the result in CU. */
11281
11282 static void
11283 check_producer (struct dwarf2_cu *cu)
11284 {
11285 int major, minor;
11286
11287 if (cu->producer == NULL)
11288 {
11289 /* For unknown compilers expect their behavior is DWARF version
11290 compliant.
11291
11292 GCC started to support .debug_types sections by -gdwarf-4 since
11293 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11294 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11295 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11296 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11297 }
11298 else if (producer_is_gcc (cu->producer, &major, &minor))
11299 {
11300 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11301 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11302 cu->producer_is_gcc_11 = major == 11;
11303 }
11304 else if (producer_is_icc (cu->producer, &major, &minor))
11305 {
11306 cu->producer_is_icc = true;
11307 cu->producer_is_icc_lt_14 = major < 14;
11308 }
11309 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
11310 cu->producer_is_codewarrior = true;
11311 else if (producer_is_clang (cu->producer, &major, &minor))
11312 cu->producer_is_clang = true;
11313 else if (producer_is_gas (cu->producer, &major, &minor))
11314 {
11315 cu->producer_is_gas_lt_2_38 = major < 2 || (major == 2 && minor < 38);
11316 cu->producer_is_gas_2_39 = major == 2 && minor == 39;
11317 cu->producer_is_gas_ge_2_40 = major > 2 || (major == 2 && minor >= 40);
11318 }
11319 else
11320 {
11321 /* For other non-GCC compilers, expect their behavior is DWARF version
11322 compliant. */
11323 }
11324
11325 cu->checked_producer = true;
11326 }
11327
11328 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11329 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11330 during 4.6.0 experimental. */
11331
11332 static bool
11333 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11334 {
11335 if (!cu->checked_producer)
11336 check_producer (cu);
11337
11338 return cu->producer_is_gxx_lt_4_6;
11339 }
11340
11341
11342 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
11343 with incorrect is_stmt attributes. */
11344
11345 static bool
11346 producer_is_codewarrior (struct dwarf2_cu *cu)
11347 {
11348 if (!cu->checked_producer)
11349 check_producer (cu);
11350
11351 return cu->producer_is_codewarrior;
11352 }
11353
11354 static bool
11355 producer_is_gas_lt_2_38 (struct dwarf2_cu *cu)
11356 {
11357 if (!cu->checked_producer)
11358 check_producer (cu);
11359
11360 return cu->producer_is_gas_lt_2_38;
11361 }
11362
11363 static bool
11364 producer_is_gas_2_39 (struct dwarf2_cu *cu)
11365 {
11366 if (!cu->checked_producer)
11367 check_producer (cu);
11368
11369 return cu->producer_is_gas_2_39;
11370 }
11371
11372 /* Return true if CU is produced by GAS 2.39 or later. */
11373
11374 static bool
11375 producer_is_gas_ge_2_39 (struct dwarf2_cu *cu)
11376 {
11377 if (!cu->checked_producer)
11378 check_producer (cu);
11379
11380 return cu->producer_is_gas_2_39 || cu->producer_is_gas_ge_2_40;
11381 }
11382
11383 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
11384 If that attribute is not available, return the appropriate
11385 default. */
11386
11387 static enum dwarf_access_attribute
11388 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11389 {
11390 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11391 if (attr != nullptr)
11392 {
11393 LONGEST value = attr->constant_value (-1);
11394 if (value == DW_ACCESS_public
11395 || value == DW_ACCESS_protected
11396 || value == DW_ACCESS_private)
11397 return (dwarf_access_attribute) value;
11398 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
11399 plongest (value));
11400 }
11401
11402 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11403 {
11404 /* The default DWARF 2 accessibility for members is public, the default
11405 accessibility for inheritance is private. */
11406
11407 if (die->tag != DW_TAG_inheritance)
11408 return DW_ACCESS_public;
11409 else
11410 return DW_ACCESS_private;
11411 }
11412 else
11413 {
11414 /* DWARF 3+ defines the default accessibility a different way. The same
11415 rules apply now for DW_TAG_inheritance as for the members and it only
11416 depends on the container kind. */
11417
11418 if (die->parent->tag == DW_TAG_class_type)
11419 return DW_ACCESS_private;
11420 else
11421 return DW_ACCESS_public;
11422 }
11423 }
11424
11425 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
11426 *OFFSET to the byte offset. If the attribute was not found return
11427 0, otherwise return 1. If it was found but could not properly be
11428 handled, set *OFFSET to 0. */
11429
11430 static int
11431 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11432 LONGEST *offset)
11433 {
11434 struct attribute *attr;
11435
11436 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11437 if (attr != NULL)
11438 {
11439 *offset = 0;
11440 CORE_ADDR temp;
11441
11442 /* Note that we do not check for a section offset first here.
11443 This is because DW_AT_data_member_location is new in DWARF 4,
11444 so if we see it, we can assume that a constant form is really
11445 a constant and not a section offset. */
11446 if (attr->form_is_constant ())
11447 *offset = attr->constant_value (0);
11448 else if (attr->form_is_section_offset ())
11449 dwarf2_complex_location_expr_complaint ();
11450 else if (attr->form_is_block ()
11451 && decode_locdesc (attr->as_block (), cu, &temp))
11452 {
11453 *offset = temp;
11454 }
11455 else
11456 dwarf2_complex_location_expr_complaint ();
11457
11458 return 1;
11459 }
11460 else
11461 {
11462 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11463 if (attr != nullptr)
11464 {
11465 *offset = attr->constant_value (0);
11466 return 1;
11467 }
11468 }
11469
11470 return 0;
11471 }
11472
11473 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
11474 store the results in FIELD. */
11475
11476 static void
11477 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11478 struct field *field)
11479 {
11480 struct attribute *attr;
11481
11482 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11483 if (attr != NULL)
11484 {
11485 if (attr->form_is_constant ())
11486 {
11487 LONGEST offset = attr->constant_value (0);
11488
11489 /* Work around this GCC 11 bug, where it would erroneously use -1
11490 data member locations, instead of 0:
11491
11492 Negative DW_AT_data_member_location
11493 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
11494 */
11495 if (offset == -1 && cu->producer_is_gcc_11)
11496 {
11497 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
11498 offset = 0;
11499 }
11500
11501 field->set_loc_bitpos (offset * bits_per_byte);
11502 }
11503 else if (attr->form_is_section_offset ())
11504 dwarf2_complex_location_expr_complaint ();
11505 else if (attr->form_is_block ())
11506 {
11507 CORE_ADDR offset;
11508 if (decode_locdesc (attr->as_block (), cu, &offset))
11509 field->set_loc_bitpos (offset * bits_per_byte);
11510 else
11511 {
11512 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11513 struct objfile *objfile = per_objfile->objfile;
11514 struct dwarf2_locexpr_baton *dlbaton
11515 = XOBNEW (&objfile->objfile_obstack,
11516 struct dwarf2_locexpr_baton);
11517 dlbaton->data = attr->as_block ()->data;
11518 dlbaton->size = attr->as_block ()->size;
11519 /* When using this baton, we want to compute the address
11520 of the field, not the value. This is why
11521 is_reference is set to false here. */
11522 dlbaton->is_reference = false;
11523 dlbaton->per_objfile = per_objfile;
11524 dlbaton->per_cu = cu->per_cu;
11525
11526 field->set_loc_dwarf_block (dlbaton);
11527 }
11528 }
11529 else
11530 dwarf2_complex_location_expr_complaint ();
11531 }
11532 else
11533 {
11534 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11535 if (attr != nullptr)
11536 field->set_loc_bitpos (attr->constant_value (0));
11537 }
11538 }
11539
11540 /* Add an aggregate field to the field list. */
11541
11542 static void
11543 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11544 struct dwarf2_cu *cu)
11545 {
11546 struct objfile *objfile = cu->per_objfile->objfile;
11547 struct gdbarch *gdbarch = objfile->arch ();
11548 struct nextfield *new_field;
11549 struct attribute *attr;
11550 struct field *fp;
11551 const char *fieldname = "";
11552
11553 if (die->tag == DW_TAG_inheritance)
11554 new_field = &fip->baseclasses.emplace_back ();
11555 else
11556 new_field = &fip->fields.emplace_back ();
11557
11558 new_field->offset = die->sect_off;
11559
11560 switch (dwarf2_access_attribute (die, cu))
11561 {
11562 case DW_ACCESS_public:
11563 break;
11564 case DW_ACCESS_private:
11565 new_field->field.set_accessibility (accessibility::PRIVATE);
11566 break;
11567 case DW_ACCESS_protected:
11568 new_field->field.set_accessibility (accessibility::PROTECTED);
11569 break;
11570 default:
11571 gdb_assert_not_reached ("invalid accessibility");
11572 }
11573
11574 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11575 if (attr != nullptr && attr->as_virtuality ())
11576 new_field->field.set_virtual ();
11577
11578 fp = &new_field->field;
11579
11580 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
11581 && !die_is_declaration (die, cu))
11582 {
11583 if (die->tag == DW_TAG_namelist_item)
11584 {
11585 /* Typically, DW_TAG_namelist_item are references to namelist items.
11586 If so, follow that reference. */
11587 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
11588 struct die_info *item_die = nullptr;
11589 struct dwarf2_cu *item_cu = cu;
11590 if (attr1->form_is_ref ())
11591 item_die = follow_die_ref (die, attr1, &item_cu);
11592 if (item_die != nullptr)
11593 die = item_die;
11594 }
11595 /* Data member other than a C++ static data member. */
11596
11597 /* Get type of field. */
11598 fp->set_type (die_type (die, cu));
11599
11600 fp->set_loc_bitpos (0);
11601
11602 /* Get bit size of field (zero if none). */
11603 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11604 if (attr != nullptr)
11605 fp->set_bitsize (attr->constant_value (0));
11606 else
11607 fp->set_bitsize (0);
11608
11609 /* Get bit offset of field. */
11610 handle_member_location (die, cu, fp);
11611 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11612 if (attr != nullptr && attr->form_is_constant ())
11613 {
11614 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
11615 {
11616 /* For big endian bits, the DW_AT_bit_offset gives the
11617 additional bit offset from the MSB of the containing
11618 anonymous object to the MSB of the field. We don't
11619 have to do anything special since we don't need to
11620 know the size of the anonymous object. */
11621 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
11622 }
11623 else
11624 {
11625 /* For little endian bits, compute the bit offset to the
11626 MSB of the anonymous object, subtract off the number of
11627 bits from the MSB of the field to the MSB of the
11628 object, and then subtract off the number of bits of
11629 the field itself. The result is the bit offset of
11630 the LSB of the field. */
11631 int anonymous_size;
11632 int bit_offset = attr->constant_value (0);
11633
11634 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11635 if (attr != nullptr && attr->form_is_constant ())
11636 {
11637 /* The size of the anonymous object containing
11638 the bit field is explicit, so use the
11639 indicated size (in bytes). */
11640 anonymous_size = attr->constant_value (0);
11641 }
11642 else
11643 {
11644 /* The size of the anonymous object containing
11645 the bit field must be inferred from the type
11646 attribute of the data member containing the
11647 bit field. */
11648 anonymous_size = fp->type ()->length ();
11649 }
11650 fp->set_loc_bitpos (fp->loc_bitpos ()
11651 + anonymous_size * bits_per_byte
11652 - bit_offset - fp->bitsize ());
11653 }
11654 }
11655
11656 /* Get name of field. */
11657 fieldname = dwarf2_name (die, cu);
11658 if (fieldname == NULL)
11659 fieldname = "";
11660
11661 /* The name is already allocated along with this objfile, so we don't
11662 need to duplicate it for the type. */
11663 fp->set_name (fieldname);
11664
11665 /* Change accessibility for artificial fields (e.g. virtual table
11666 pointer or virtual base class pointer) to private. */
11667 if (dwarf2_attr (die, DW_AT_artificial, cu))
11668 {
11669 fp->set_is_artificial (true);
11670 fp->set_accessibility (accessibility::PRIVATE);
11671 }
11672 }
11673 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11674 {
11675 /* C++ static member. */
11676
11677 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11678 is a declaration, but all versions of G++ as of this writing
11679 (so through at least 3.2.1) incorrectly generate
11680 DW_TAG_variable tags. */
11681
11682 const char *physname;
11683
11684 /* Get name of field. */
11685 fieldname = dwarf2_name (die, cu);
11686 if (fieldname == NULL)
11687 return;
11688
11689 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11690 if (attr
11691 /* Only create a symbol if this is an external value.
11692 new_symbol checks this and puts the value in the global symbol
11693 table, which we want. If it is not external, new_symbol
11694 will try to put the value in cu->list_in_scope which is wrong. */
11695 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11696 {
11697 /* A static const member, not much different than an enum as far as
11698 we're concerned, except that we can support more types. */
11699 new_symbol (die, NULL, cu);
11700 }
11701
11702 /* Get physical name. */
11703 physname = dwarf2_physname (fieldname, die, cu);
11704
11705 /* The name is already allocated along with this objfile, so we don't
11706 need to duplicate it for the type. */
11707 fp->set_loc_physname (physname ? physname : "");
11708 fp->set_type (die_type (die, cu));
11709 fp->set_name (fieldname);
11710 }
11711 else if (die->tag == DW_TAG_inheritance)
11712 {
11713 /* C++ base class field. */
11714 handle_member_location (die, cu, fp);
11715 fp->set_bitsize (0);
11716 fp->set_type (die_type (die, cu));
11717 fp->set_name (fp->type ()->name ());
11718 }
11719 else
11720 gdb_assert_not_reached ("missing case in dwarf2_add_field");
11721 }
11722
11723 /* Can the type given by DIE define another type? */
11724
11725 static bool
11726 type_can_define_types (const struct die_info *die)
11727 {
11728 switch (die->tag)
11729 {
11730 case DW_TAG_typedef:
11731 case DW_TAG_class_type:
11732 case DW_TAG_structure_type:
11733 case DW_TAG_union_type:
11734 case DW_TAG_enumeration_type:
11735 return true;
11736
11737 default:
11738 return false;
11739 }
11740 }
11741
11742 /* Add a type definition defined in the scope of the FIP's class. */
11743
11744 static void
11745 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
11746 struct dwarf2_cu *cu)
11747 {
11748 struct decl_field fp;
11749 memset (&fp, 0, sizeof (fp));
11750
11751 gdb_assert (type_can_define_types (die));
11752
11753 /* Get name of field. NULL is okay here, meaning an anonymous type. */
11754 fp.name = dwarf2_name (die, cu);
11755 fp.type = read_type_die (die, cu);
11756
11757 /* Save accessibility. */
11758 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
11759 switch (accessibility)
11760 {
11761 case DW_ACCESS_public:
11762 /* The assumed value if neither private nor protected. */
11763 break;
11764 case DW_ACCESS_private:
11765 fp.accessibility = accessibility::PRIVATE;
11766 break;
11767 case DW_ACCESS_protected:
11768 fp.accessibility = accessibility::PROTECTED;
11769 break;
11770 }
11771
11772 if (die->tag == DW_TAG_typedef)
11773 fip->typedef_field_list.push_back (fp);
11774 else
11775 fip->nested_types_list.push_back (fp);
11776 }
11777
11778 /* A convenience typedef that's used when finding the discriminant
11779 field for a variant part. */
11780 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
11781 offset_map_type;
11782
11783 /* Compute the discriminant range for a given variant. OBSTACK is
11784 where the results will be stored. VARIANT is the variant to
11785 process. IS_UNSIGNED indicates whether the discriminant is signed
11786 or unsigned. */
11787
11788 static const gdb::array_view<discriminant_range>
11789 convert_variant_range (struct obstack *obstack, const variant_field &variant,
11790 bool is_unsigned)
11791 {
11792 std::vector<discriminant_range> ranges;
11793
11794 if (variant.default_branch)
11795 return {};
11796
11797 if (variant.discr_list_data == nullptr)
11798 {
11799 discriminant_range r
11800 = {variant.discriminant_value, variant.discriminant_value};
11801 ranges.push_back (r);
11802 }
11803 else
11804 {
11805 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
11806 variant.discr_list_data->size);
11807 while (!data.empty ())
11808 {
11809 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
11810 {
11811 complaint (_("invalid discriminant marker: %d"), data[0]);
11812 break;
11813 }
11814 bool is_range = data[0] == DW_DSC_range;
11815 data = data.slice (1);
11816
11817 ULONGEST low, high;
11818 unsigned int bytes_read;
11819
11820 if (data.empty ())
11821 {
11822 complaint (_("DW_AT_discr_list missing low value"));
11823 break;
11824 }
11825 if (is_unsigned)
11826 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
11827 else
11828 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
11829 &bytes_read);
11830 data = data.slice (bytes_read);
11831
11832 if (is_range)
11833 {
11834 if (data.empty ())
11835 {
11836 complaint (_("DW_AT_discr_list missing high value"));
11837 break;
11838 }
11839 if (is_unsigned)
11840 high = read_unsigned_leb128 (nullptr, data.data (),
11841 &bytes_read);
11842 else
11843 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
11844 &bytes_read);
11845 data = data.slice (bytes_read);
11846 }
11847 else
11848 high = low;
11849
11850 ranges.push_back ({ low, high });
11851 }
11852 }
11853
11854 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
11855 ranges.size ());
11856 std::copy (ranges.begin (), ranges.end (), result);
11857 return gdb::array_view<discriminant_range> (result, ranges.size ());
11858 }
11859
11860 static const gdb::array_view<variant_part> create_variant_parts
11861 (struct obstack *obstack,
11862 const offset_map_type &offset_map,
11863 struct field_info *fi,
11864 const std::vector<variant_part_builder> &variant_parts);
11865
11866 /* Fill in a "struct variant" for a given variant field. RESULT is
11867 the variant to fill in. OBSTACK is where any needed allocations
11868 will be done. OFFSET_MAP holds the mapping from section offsets to
11869 fields for the type. FI describes the fields of the type we're
11870 processing. FIELD is the variant field we're converting. */
11871
11872 static void
11873 create_one_variant (variant &result, struct obstack *obstack,
11874 const offset_map_type &offset_map,
11875 struct field_info *fi, const variant_field &field)
11876 {
11877 result.discriminants = convert_variant_range (obstack, field, false);
11878 result.first_field = field.first_field + fi->baseclasses.size ();
11879 result.last_field = field.last_field + fi->baseclasses.size ();
11880 result.parts = create_variant_parts (obstack, offset_map, fi,
11881 field.variant_parts);
11882 }
11883
11884 /* Fill in a "struct variant_part" for a given variant part. RESULT
11885 is the variant part to fill in. OBSTACK is where any needed
11886 allocations will be done. OFFSET_MAP holds the mapping from
11887 section offsets to fields for the type. FI describes the fields of
11888 the type we're processing. BUILDER is the variant part to be
11889 converted. */
11890
11891 static void
11892 create_one_variant_part (variant_part &result,
11893 struct obstack *obstack,
11894 const offset_map_type &offset_map,
11895 struct field_info *fi,
11896 const variant_part_builder &builder)
11897 {
11898 auto iter = offset_map.find (builder.discriminant_offset);
11899 if (iter == offset_map.end ())
11900 {
11901 result.discriminant_index = -1;
11902 /* Doesn't matter. */
11903 result.is_unsigned = false;
11904 }
11905 else
11906 {
11907 result.discriminant_index = iter->second;
11908 result.is_unsigned
11909 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
11910 }
11911
11912 size_t n = builder.variants.size ();
11913 variant *output = new (obstack) variant[n];
11914 for (size_t i = 0; i < n; ++i)
11915 create_one_variant (output[i], obstack, offset_map, fi,
11916 builder.variants[i]);
11917
11918 result.variants = gdb::array_view<variant> (output, n);
11919 }
11920
11921 /* Create a vector of variant parts that can be attached to a type.
11922 OBSTACK is where any needed allocations will be done. OFFSET_MAP
11923 holds the mapping from section offsets to fields for the type. FI
11924 describes the fields of the type we're processing. VARIANT_PARTS
11925 is the vector to convert. */
11926
11927 static const gdb::array_view<variant_part>
11928 create_variant_parts (struct obstack *obstack,
11929 const offset_map_type &offset_map,
11930 struct field_info *fi,
11931 const std::vector<variant_part_builder> &variant_parts)
11932 {
11933 if (variant_parts.empty ())
11934 return {};
11935
11936 size_t n = variant_parts.size ();
11937 variant_part *result = new (obstack) variant_part[n];
11938 for (size_t i = 0; i < n; ++i)
11939 create_one_variant_part (result[i], obstack, offset_map, fi,
11940 variant_parts[i]);
11941
11942 return gdb::array_view<variant_part> (result, n);
11943 }
11944
11945 /* Compute the variant part vector for FIP, attaching it to TYPE when
11946 done. */
11947
11948 static void
11949 add_variant_property (struct field_info *fip, struct type *type,
11950 struct dwarf2_cu *cu)
11951 {
11952 /* Map section offsets of fields to their field index. Note the
11953 field index here does not take the number of baseclasses into
11954 account. */
11955 offset_map_type offset_map;
11956 for (int i = 0; i < fip->fields.size (); ++i)
11957 offset_map[fip->fields[i].offset] = i;
11958
11959 struct objfile *objfile = cu->per_objfile->objfile;
11960 gdb::array_view<const variant_part> parts
11961 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
11962 fip->variant_parts);
11963
11964 struct dynamic_prop prop;
11965 prop.set_variant_parts ((gdb::array_view<variant_part> *)
11966 obstack_copy (&objfile->objfile_obstack, &parts,
11967 sizeof (parts)));
11968
11969 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
11970 }
11971
11972 /* Create the vector of fields, and attach it to the type. */
11973
11974 static void
11975 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11976 struct dwarf2_cu *cu)
11977 {
11978 int nfields = fip->nfields ();
11979
11980 /* Record the field count, allocate space for the array of fields,
11981 and create blank accessibility bitfields if necessary. */
11982 type->alloc_fields (nfields);
11983
11984 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
11985 {
11986 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11987 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
11988 }
11989
11990 if (!fip->variant_parts.empty ())
11991 add_variant_property (fip, type, cu);
11992
11993 /* Copy the saved-up fields into the field vector. */
11994 for (int i = 0; i < nfields; ++i)
11995 {
11996 struct nextfield &field
11997 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
11998 : fip->fields[i - fip->baseclasses.size ()]);
11999
12000 type->field (i) = field.field;
12001 }
12002 }
12003
12004 /* Return true if this member function is a constructor, false
12005 otherwise. */
12006
12007 static int
12008 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12009 {
12010 const char *fieldname;
12011 const char *type_name;
12012 int len;
12013
12014 if (die->parent == NULL)
12015 return 0;
12016
12017 if (die->parent->tag != DW_TAG_structure_type
12018 && die->parent->tag != DW_TAG_union_type
12019 && die->parent->tag != DW_TAG_class_type)
12020 return 0;
12021
12022 fieldname = dwarf2_name (die, cu);
12023 type_name = dwarf2_name (die->parent, cu);
12024 if (fieldname == NULL || type_name == NULL)
12025 return 0;
12026
12027 len = strlen (fieldname);
12028 return (strncmp (fieldname, type_name, len) == 0
12029 && (type_name[len] == '\0' || type_name[len] == '<'));
12030 }
12031
12032 /* Add a member function to the proper fieldlist. */
12033
12034 static void
12035 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12036 struct type *type, struct dwarf2_cu *cu)
12037 {
12038 struct objfile *objfile = cu->per_objfile->objfile;
12039 struct attribute *attr;
12040 int i;
12041 struct fnfieldlist *flp = nullptr;
12042 struct fn_field *fnp;
12043 const char *fieldname;
12044 struct type *this_type;
12045
12046 if (cu->lang () == language_ada)
12047 error (_("unexpected member function in Ada type"));
12048
12049 /* Get name of member function. */
12050 fieldname = dwarf2_name (die, cu);
12051 if (fieldname == NULL)
12052 return;
12053
12054 /* Look up member function name in fieldlist. */
12055 for (i = 0; i < fip->fnfieldlists.size (); i++)
12056 {
12057 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12058 {
12059 flp = &fip->fnfieldlists[i];
12060 break;
12061 }
12062 }
12063
12064 /* Create a new fnfieldlist if necessary. */
12065 if (flp == nullptr)
12066 {
12067 flp = &fip->fnfieldlists.emplace_back ();
12068 flp->name = fieldname;
12069 i = fip->fnfieldlists.size () - 1;
12070 }
12071
12072 /* Create a new member function field and add it to the vector of
12073 fnfieldlists. */
12074 fnp = &flp->fnfields.emplace_back ();
12075
12076 /* Delay processing of the physname until later. */
12077 if (cu->lang () == language_cplus)
12078 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
12079 die, cu);
12080 else
12081 {
12082 const char *physname = dwarf2_physname (fieldname, die, cu);
12083 fnp->physname = physname ? physname : "";
12084 }
12085
12086 fnp->type = type_allocator (objfile, cu->lang ()).new_type ();
12087 this_type = read_type_die (die, cu);
12088 if (this_type && this_type->code () == TYPE_CODE_FUNC)
12089 {
12090 int nparams = this_type->num_fields ();
12091
12092 /* TYPE is the domain of this method, and THIS_TYPE is the type
12093 of the method itself (TYPE_CODE_METHOD). */
12094 smash_to_method_type (fnp->type, type,
12095 this_type->target_type (),
12096 this_type->fields (),
12097 this_type->num_fields (),
12098 this_type->has_varargs ());
12099
12100 /* Handle static member functions.
12101 Dwarf2 has no clean way to discern C++ static and non-static
12102 member functions. G++ helps GDB by marking the first
12103 parameter for non-static member functions (which is the this
12104 pointer) as artificial. We obtain this information from
12105 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12106 if (nparams == 0 || this_type->field (0).is_artificial () == 0)
12107 fnp->voffset = VOFFSET_STATIC;
12108 }
12109 else
12110 complaint (_("member function type missing for '%s'"),
12111 dwarf2_full_name (fieldname, die, cu));
12112
12113 /* Get fcontext from DW_AT_containing_type if present. */
12114 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12115 fnp->fcontext = die_containing_type (die, cu);
12116
12117 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12118 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12119
12120 /* Get accessibility. */
12121 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
12122 switch (accessibility)
12123 {
12124 case DW_ACCESS_private:
12125 fnp->accessibility = accessibility::PRIVATE;
12126 break;
12127 case DW_ACCESS_protected:
12128 fnp->accessibility = accessibility::PROTECTED;
12129 break;
12130 }
12131
12132 /* Check for artificial methods. */
12133 attr = dwarf2_attr (die, DW_AT_artificial, cu);
12134 if (attr && attr->as_boolean ())
12135 fnp->is_artificial = 1;
12136
12137 /* Check for defaulted methods. */
12138 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
12139 if (attr != nullptr)
12140 fnp->defaulted = attr->defaulted ();
12141
12142 /* Check for deleted methods. */
12143 attr = dwarf2_attr (die, DW_AT_deleted, cu);
12144 if (attr != nullptr && attr->as_boolean ())
12145 fnp->is_deleted = 1;
12146
12147 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12148
12149 /* Get index in virtual function table if it is a virtual member
12150 function. For older versions of GCC, this is an offset in the
12151 appropriate virtual table, as specified by DW_AT_containing_type.
12152 For everyone else, it is an expression to be evaluated relative
12153 to the object address. */
12154
12155 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12156 if (attr != nullptr)
12157 {
12158 if (attr->form_is_block () && attr->as_block ()->size > 0)
12159 {
12160 struct dwarf_block *block = attr->as_block ();
12161 CORE_ADDR offset;
12162
12163 if (block->data[0] == DW_OP_constu
12164 && decode_locdesc (block, cu, &offset))
12165 {
12166 /* "Old"-style GCC. See
12167 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44126
12168 for discussion. This was known and a patch available
12169 in 2010, but as of 2023, both GCC and clang still
12170 emit this. */
12171 fnp->voffset = offset + 2;
12172 }
12173 else if ((block->data[0] == DW_OP_deref
12174 || (block->size > 1
12175 && block->data[0] == DW_OP_deref_size
12176 && block->data[1] == cu->header.addr_size))
12177 && decode_locdesc (block, cu, &offset))
12178 {
12179 fnp->voffset = offset;
12180 if ((fnp->voffset % cu->header.addr_size) != 0)
12181 dwarf2_complex_location_expr_complaint ();
12182 else
12183 fnp->voffset /= cu->header.addr_size;
12184 fnp->voffset += 2;
12185 }
12186 else
12187 dwarf2_complex_location_expr_complaint ();
12188
12189 if (!fnp->fcontext)
12190 {
12191 /* If there is no `this' field and no DW_AT_containing_type,
12192 we cannot actually find a base class context for the
12193 vtable! */
12194 if (this_type->num_fields () == 0
12195 || !this_type->field (0).is_artificial ())
12196 {
12197 complaint (_("cannot determine context for virtual member "
12198 "function \"%s\" (offset %s)"),
12199 fieldname, sect_offset_str (die->sect_off));
12200 }
12201 else
12202 {
12203 fnp->fcontext = this_type->field (0).type ()->target_type ();
12204 }
12205 }
12206 }
12207 else if (attr->form_is_section_offset ())
12208 {
12209 dwarf2_complex_location_expr_complaint ();
12210 }
12211 else
12212 {
12213 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12214 fieldname);
12215 }
12216 }
12217 else
12218 {
12219 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12220 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
12221 {
12222 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12223 complaint (_("Member function \"%s\" (offset %s) is virtual "
12224 "but the vtable offset is not specified"),
12225 fieldname, sect_offset_str (die->sect_off));
12226 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12227 TYPE_CPLUS_DYNAMIC (type) = 1;
12228 }
12229 }
12230 }
12231
12232 /* Create the vector of member function fields, and attach it to the type. */
12233
12234 static void
12235 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
12236 struct dwarf2_cu *cu)
12237 {
12238 if (cu->lang () == language_ada)
12239 error (_("unexpected member functions in Ada type"));
12240
12241 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12242 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
12243 TYPE_ZALLOC (type,
12244 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
12245
12246 for (int i = 0; i < fip->fnfieldlists.size (); i++)
12247 {
12248 struct fnfieldlist &nf = fip->fnfieldlists[i];
12249 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
12250
12251 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
12252 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
12253 /* No need to zero-initialize, initialization is done by the copy in
12254 the loop below. */
12255 fn_flp->fn_fields = (struct fn_field *)
12256 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
12257
12258 for (int k = 0; k < nf.fnfields.size (); ++k)
12259 fn_flp->fn_fields[k] = nf.fnfields[k];
12260 }
12261
12262 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
12263 }
12264
12265 /* Returns non-zero if NAME is the name of a vtable member in CU's
12266 language, zero otherwise. */
12267 static int
12268 is_vtable_name (const char *name, struct dwarf2_cu *cu)
12269 {
12270 static const char vptr[] = "_vptr";
12271
12272 /* Look for the C++ form of the vtable. */
12273 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
12274 return 1;
12275
12276 return 0;
12277 }
12278
12279 /* GCC outputs unnamed structures that are really pointers to member
12280 functions, with the ABI-specified layout. If TYPE describes
12281 such a structure, smash it into a member function type.
12282
12283 GCC shouldn't do this; it should just output pointer to member DIEs.
12284 This is GCC PR debug/28767. */
12285
12286 static void
12287 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
12288 {
12289 struct type *pfn_type, *self_type, *new_type;
12290
12291 /* Check for a structure with no name and two children. */
12292 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12293 return;
12294
12295 /* Check for __pfn and __delta members. */
12296 if (type->field (0).name () == NULL
12297 || strcmp (type->field (0).name (), "__pfn") != 0
12298 || type->field (1).name () == NULL
12299 || strcmp (type->field (1).name (), "__delta") != 0)
12300 return;
12301
12302 /* Find the type of the method. */
12303 pfn_type = type->field (0).type ();
12304 if (pfn_type == NULL
12305 || pfn_type->code () != TYPE_CODE_PTR
12306 || pfn_type->target_type ()->code () != TYPE_CODE_FUNC)
12307 return;
12308
12309 /* Look for the "this" argument. */
12310 pfn_type = pfn_type->target_type ();
12311 if (pfn_type->num_fields () == 0
12312 /* || pfn_type->field (0).type () == NULL */
12313 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
12314 return;
12315
12316 self_type = pfn_type->field (0).type ()->target_type ();
12317 new_type = type_allocator (type).new_type ();
12318 smash_to_method_type (new_type, self_type, pfn_type->target_type (),
12319 pfn_type->fields (), pfn_type->num_fields (),
12320 pfn_type->has_varargs ());
12321 smash_to_methodptr_type (type, new_type);
12322 }
12323
12324 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
12325 requires rewriting, then copy it and return the updated copy.
12326 Otherwise return nullptr. */
12327
12328 static struct type *
12329 rewrite_array_type (struct type *type)
12330 {
12331 if (type->code () != TYPE_CODE_ARRAY)
12332 return nullptr;
12333
12334 struct type *index_type = type->index_type ();
12335 range_bounds *current_bounds = index_type->bounds ();
12336
12337 /* Handle multi-dimensional arrays. */
12338 struct type *new_target = rewrite_array_type (type->target_type ());
12339 if (new_target == nullptr)
12340 {
12341 /* Maybe we don't need to rewrite this array. */
12342 if (current_bounds->low.is_constant ()
12343 && current_bounds->high.is_constant ())
12344 return nullptr;
12345 }
12346
12347 /* Either the target type was rewritten, or the bounds have to be
12348 updated. Either way we want to copy the type and update
12349 everything. */
12350 struct type *copy = copy_type (type);
12351 copy->copy_fields (type);
12352 if (new_target != nullptr)
12353 copy->set_target_type (new_target);
12354
12355 struct type *index_copy = copy_type (index_type);
12356 range_bounds *bounds
12357 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
12358 sizeof (range_bounds));
12359 *bounds = *current_bounds;
12360 bounds->low.set_const_val (1);
12361 bounds->high.set_const_val (0);
12362 index_copy->set_bounds (bounds);
12363 copy->set_index_type (index_copy);
12364
12365 return copy;
12366 }
12367
12368 /* While some versions of GCC will generate complicated DWARF for an
12369 array (see quirk_ada_thick_pointer), more recent versions were
12370 modified to emit an explicit thick pointer structure. However, in
12371 this case, the array still has DWARF expressions for its ranges,
12372 and these must be ignored. */
12373
12374 static void
12375 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
12376 struct type *type)
12377 {
12378 gdb_assert (cu->lang () == language_ada);
12379
12380 /* Check for a structure with two children. */
12381 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12382 return;
12383
12384 /* Check for P_ARRAY and P_BOUNDS members. */
12385 if (type->field (0).name () == NULL
12386 || strcmp (type->field (0).name (), "P_ARRAY") != 0
12387 || type->field (1).name () == NULL
12388 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
12389 return;
12390
12391 /* Make sure we're looking at a pointer to an array. */
12392 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
12393 return;
12394
12395 /* The Ada code already knows how to handle these types, so all that
12396 we need to do is turn the bounds into static bounds. However, we
12397 don't want to rewrite existing array or index types in-place,
12398 because those may be referenced in other contexts where this
12399 rewriting is undesirable. */
12400 struct type *new_ary_type
12401 = rewrite_array_type (type->field (0).type ()->target_type ());
12402 if (new_ary_type != nullptr)
12403 type->field (0).set_type (lookup_pointer_type (new_ary_type));
12404 }
12405
12406 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
12407 appropriate error checking and issuing complaints if there is a
12408 problem. */
12409
12410 static ULONGEST
12411 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
12412 {
12413 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
12414
12415 if (attr == nullptr)
12416 return 0;
12417
12418 if (!attr->form_is_constant ())
12419 {
12420 complaint (_("DW_AT_alignment must have constant form"
12421 " - DIE at %s [in module %s]"),
12422 sect_offset_str (die->sect_off),
12423 objfile_name (cu->per_objfile->objfile));
12424 return 0;
12425 }
12426
12427 LONGEST val = attr->constant_value (0);
12428 if (val < 0)
12429 {
12430 complaint (_("DW_AT_alignment value must not be negative"
12431 " - DIE at %s [in module %s]"),
12432 sect_offset_str (die->sect_off),
12433 objfile_name (cu->per_objfile->objfile));
12434 return 0;
12435 }
12436 ULONGEST align = val;
12437
12438 if (align == 0)
12439 {
12440 complaint (_("DW_AT_alignment value must not be zero"
12441 " - DIE at %s [in module %s]"),
12442 sect_offset_str (die->sect_off),
12443 objfile_name (cu->per_objfile->objfile));
12444 return 0;
12445 }
12446 if ((align & (align - 1)) != 0)
12447 {
12448 complaint (_("DW_AT_alignment value must be a power of 2"
12449 " - DIE at %s [in module %s]"),
12450 sect_offset_str (die->sect_off),
12451 objfile_name (cu->per_objfile->objfile));
12452 return 0;
12453 }
12454
12455 return align;
12456 }
12457
12458 /* If the DIE has a DW_AT_alignment attribute, use its value to set
12459 the alignment for TYPE. */
12460
12461 static void
12462 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
12463 struct type *type)
12464 {
12465 if (!set_type_align (type, get_alignment (cu, die)))
12466 complaint (_("DW_AT_alignment value too large"
12467 " - DIE at %s [in module %s]"),
12468 sect_offset_str (die->sect_off),
12469 objfile_name (cu->per_objfile->objfile));
12470 }
12471
12472 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12473 constant for a type, according to DWARF5 spec, Table 5.5. */
12474
12475 static bool
12476 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
12477 {
12478 switch (value)
12479 {
12480 case DW_CC_normal:
12481 case DW_CC_pass_by_reference:
12482 case DW_CC_pass_by_value:
12483 return true;
12484
12485 default:
12486 complaint (_("unrecognized DW_AT_calling_convention value "
12487 "(%s) for a type"), pulongest (value));
12488 return false;
12489 }
12490 }
12491
12492 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12493 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
12494 also according to GNU-specific values (see include/dwarf2.h). */
12495
12496 static bool
12497 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
12498 {
12499 switch (value)
12500 {
12501 case DW_CC_normal:
12502 case DW_CC_program:
12503 case DW_CC_nocall:
12504 return true;
12505
12506 case DW_CC_GNU_renesas_sh:
12507 case DW_CC_GNU_borland_fastcall_i386:
12508 case DW_CC_GDB_IBM_OpenCL:
12509 return true;
12510
12511 default:
12512 complaint (_("unrecognized DW_AT_calling_convention value "
12513 "(%s) for a subroutine"), pulongest (value));
12514 return false;
12515 }
12516 }
12517
12518 /* Called when we find the DIE that starts a structure or union scope
12519 (definition) to create a type for the structure or union. Fill in
12520 the type's name and general properties; the members will not be
12521 processed until process_structure_scope. A symbol table entry for
12522 the type will also not be done until process_structure_scope (assuming
12523 the type has a name).
12524
12525 NOTE: we need to call these functions regardless of whether or not the
12526 DIE has a DW_AT_name attribute, since it might be an anonymous
12527 structure or union. This gets the type entered into our set of
12528 user defined types. */
12529
12530 static struct type *
12531 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
12532 {
12533 struct objfile *objfile = cu->per_objfile->objfile;
12534 struct type *type;
12535 struct attribute *attr;
12536 const char *name;
12537
12538 /* If the definition of this type lives in .debug_types, read that type.
12539 Don't follow DW_AT_specification though, that will take us back up
12540 the chain and we want to go down. */
12541 attr = die->attr (DW_AT_signature);
12542 if (attr != nullptr)
12543 {
12544 type = get_DW_AT_signature_type (die, attr, cu);
12545
12546 /* The type's CU may not be the same as CU.
12547 Ensure TYPE is recorded with CU in die_type_hash. */
12548 return set_die_type (die, type, cu);
12549 }
12550
12551 type = type_allocator (objfile, cu->lang ()).new_type ();
12552 INIT_CPLUS_SPECIFIC (type);
12553
12554 name = dwarf2_name (die, cu);
12555 if (name != NULL)
12556 {
12557 if (cu->lang () == language_cplus
12558 || cu->lang () == language_d
12559 || cu->lang () == language_rust)
12560 {
12561 const char *full_name = dwarf2_full_name (name, die, cu);
12562
12563 /* dwarf2_full_name might have already finished building the DIE's
12564 type. If so, there is no need to continue. */
12565 if (get_die_type (die, cu) != NULL)
12566 return get_die_type (die, cu);
12567
12568 type->set_name (full_name);
12569 }
12570 else
12571 {
12572 /* The name is already allocated along with this objfile, so
12573 we don't need to duplicate it for the type. */
12574 type->set_name (name);
12575 }
12576 }
12577
12578 if (die->tag == DW_TAG_structure_type)
12579 {
12580 type->set_code (TYPE_CODE_STRUCT);
12581 }
12582 else if (die->tag == DW_TAG_union_type)
12583 {
12584 type->set_code (TYPE_CODE_UNION);
12585 }
12586 else if (die->tag == DW_TAG_namelist)
12587 {
12588 type->set_code (TYPE_CODE_NAMELIST);
12589 }
12590 else
12591 {
12592 type->set_code (TYPE_CODE_STRUCT);
12593 }
12594
12595 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
12596 type->set_is_declared_class (true);
12597
12598 /* Store the calling convention in the type if it's available in
12599 the die. Otherwise the calling convention remains set to
12600 the default value DW_CC_normal. */
12601 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12602 if (attr != nullptr
12603 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
12604 {
12605 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12606 TYPE_CPLUS_CALLING_CONVENTION (type)
12607 = (enum dwarf_calling_convention) (attr->constant_value (0));
12608 }
12609
12610 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12611 if (attr != nullptr)
12612 {
12613 if (attr->form_is_constant ())
12614 type->set_length (attr->constant_value (0));
12615 else
12616 {
12617 struct dynamic_prop prop;
12618 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
12619 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
12620
12621 type->set_length (0);
12622 }
12623 }
12624 else
12625 type->set_length (0);
12626
12627 maybe_set_alignment (cu, die, type);
12628
12629 if (producer_is_icc_lt_14 (cu) && (type->length () == 0))
12630 {
12631 /* ICC<14 does not output the required DW_AT_declaration on
12632 incomplete types, but gives them a size of zero. */
12633 type->set_is_stub (true);
12634 }
12635 else
12636 type->set_stub_is_supported (true);
12637
12638 if (die_is_declaration (die, cu))
12639 type->set_is_stub (true);
12640 else if (attr == NULL && die->child == NULL
12641 && producer_is_realview (cu->producer))
12642 /* RealView does not output the required DW_AT_declaration
12643 on incomplete types. */
12644 type->set_is_stub (true);
12645
12646 /* We need to add the type field to the die immediately so we don't
12647 infinitely recurse when dealing with pointers to the structure
12648 type within the structure itself. */
12649 set_die_type (die, type, cu);
12650
12651 /* set_die_type should be already done. */
12652 set_descriptive_type (type, die, cu);
12653
12654 return type;
12655 }
12656
12657 static void handle_struct_member_die
12658 (struct die_info *child_die,
12659 struct type *type,
12660 struct field_info *fi,
12661 std::vector<struct symbol *> *template_args,
12662 struct dwarf2_cu *cu);
12663
12664 /* A helper for handle_struct_member_die that handles
12665 DW_TAG_variant_part. */
12666
12667 static void
12668 handle_variant_part (struct die_info *die, struct type *type,
12669 struct field_info *fi,
12670 std::vector<struct symbol *> *template_args,
12671 struct dwarf2_cu *cu)
12672 {
12673 variant_part_builder *new_part;
12674 if (fi->current_variant_part == nullptr)
12675 new_part = &fi->variant_parts.emplace_back ();
12676 else if (!fi->current_variant_part->processing_variant)
12677 {
12678 complaint (_("nested DW_TAG_variant_part seen "
12679 "- DIE at %s [in module %s]"),
12680 sect_offset_str (die->sect_off),
12681 objfile_name (cu->per_objfile->objfile));
12682 return;
12683 }
12684 else
12685 {
12686 variant_field &current = fi->current_variant_part->variants.back ();
12687 new_part = &current.variant_parts.emplace_back ();
12688 }
12689
12690 /* When we recurse, we want callees to add to this new variant
12691 part. */
12692 scoped_restore save_current_variant_part
12693 = make_scoped_restore (&fi->current_variant_part, new_part);
12694
12695 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
12696 if (discr == NULL)
12697 {
12698 /* It's a univariant form, an extension we support. */
12699 }
12700 else if (discr->form_is_ref ())
12701 {
12702 struct dwarf2_cu *target_cu = cu;
12703 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
12704
12705 new_part->discriminant_offset = target_die->sect_off;
12706 }
12707 else
12708 {
12709 complaint (_("DW_AT_discr does not have DIE reference form"
12710 " - DIE at %s [in module %s]"),
12711 sect_offset_str (die->sect_off),
12712 objfile_name (cu->per_objfile->objfile));
12713 }
12714
12715 for (die_info *child_die = die->child;
12716 child_die != NULL;
12717 child_die = child_die->sibling)
12718 handle_struct_member_die (child_die, type, fi, template_args, cu);
12719 }
12720
12721 /* A helper for handle_struct_member_die that handles
12722 DW_TAG_variant. */
12723
12724 static void
12725 handle_variant (struct die_info *die, struct type *type,
12726 struct field_info *fi,
12727 std::vector<struct symbol *> *template_args,
12728 struct dwarf2_cu *cu)
12729 {
12730 if (fi->current_variant_part == nullptr)
12731 {
12732 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
12733 "- DIE at %s [in module %s]"),
12734 sect_offset_str (die->sect_off),
12735 objfile_name (cu->per_objfile->objfile));
12736 return;
12737 }
12738 if (fi->current_variant_part->processing_variant)
12739 {
12740 complaint (_("nested DW_TAG_variant seen "
12741 "- DIE at %s [in module %s]"),
12742 sect_offset_str (die->sect_off),
12743 objfile_name (cu->per_objfile->objfile));
12744 return;
12745 }
12746
12747 scoped_restore save_processing_variant
12748 = make_scoped_restore (&fi->current_variant_part->processing_variant,
12749 true);
12750
12751 variant_field &variant = fi->current_variant_part->variants.emplace_back ();
12752 variant.first_field = fi->fields.size ();
12753
12754 /* In a variant we want to get the discriminant and also add a
12755 field for our sole member child. */
12756 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
12757 if (discr == nullptr || !discr->form_is_constant ())
12758 {
12759 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
12760 if (discr == nullptr || discr->as_block ()->size == 0)
12761 variant.default_branch = true;
12762 else
12763 variant.discr_list_data = discr->as_block ();
12764 }
12765 else
12766 variant.discriminant_value = discr->constant_value (0);
12767
12768 for (die_info *variant_child = die->child;
12769 variant_child != NULL;
12770 variant_child = variant_child->sibling)
12771 handle_struct_member_die (variant_child, type, fi, template_args, cu);
12772
12773 variant.last_field = fi->fields.size ();
12774 }
12775
12776 /* A helper for process_structure_scope that handles a single member
12777 DIE. */
12778
12779 static void
12780 handle_struct_member_die (struct die_info *child_die, struct type *type,
12781 struct field_info *fi,
12782 std::vector<struct symbol *> *template_args,
12783 struct dwarf2_cu *cu)
12784 {
12785 if (child_die->tag == DW_TAG_member
12786 || child_die->tag == DW_TAG_variable
12787 || child_die->tag == DW_TAG_namelist_item)
12788 {
12789 /* NOTE: carlton/2002-11-05: A C++ static data member
12790 should be a DW_TAG_member that is a declaration, but
12791 all versions of G++ as of this writing (so through at
12792 least 3.2.1) incorrectly generate DW_TAG_variable
12793 tags for them instead. */
12794 dwarf2_add_field (fi, child_die, cu);
12795 }
12796 else if (child_die->tag == DW_TAG_subprogram)
12797 {
12798 /* Rust doesn't have member functions in the C++ sense.
12799 However, it does emit ordinary functions as children
12800 of a struct DIE. */
12801 if (cu->lang () == language_rust)
12802 read_func_scope (child_die, cu);
12803 else
12804 {
12805 /* C++ member function. */
12806 dwarf2_add_member_fn (fi, child_die, type, cu);
12807 }
12808 }
12809 else if (child_die->tag == DW_TAG_inheritance)
12810 {
12811 /* C++ base class field. */
12812 dwarf2_add_field (fi, child_die, cu);
12813 }
12814 else if (type_can_define_types (child_die))
12815 dwarf2_add_type_defn (fi, child_die, cu);
12816 else if (child_die->tag == DW_TAG_template_type_param
12817 || child_die->tag == DW_TAG_template_value_param)
12818 {
12819 struct symbol *arg = new_symbol (child_die, NULL, cu);
12820
12821 if (arg != NULL)
12822 template_args->push_back (arg);
12823 }
12824 else if (child_die->tag == DW_TAG_variant_part)
12825 handle_variant_part (child_die, type, fi, template_args, cu);
12826 else if (child_die->tag == DW_TAG_variant)
12827 handle_variant (child_die, type, fi, template_args, cu);
12828 }
12829
12830 /* Finish creating a structure or union type, including filling in its
12831 members and creating a symbol for it. This function also handles Fortran
12832 namelist variables, their items or members and creating a symbol for
12833 them. */
12834
12835 static void
12836 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12837 {
12838 struct objfile *objfile = cu->per_objfile->objfile;
12839 struct die_info *child_die;
12840 struct type *type;
12841
12842 type = get_die_type (die, cu);
12843 if (type == NULL)
12844 type = read_structure_type (die, cu);
12845
12846 bool has_template_parameters = false;
12847 if (die->child != NULL && ! die_is_declaration (die, cu))
12848 {
12849 struct field_info fi;
12850 std::vector<struct symbol *> template_args;
12851
12852 child_die = die->child;
12853
12854 while (child_die && child_die->tag)
12855 {
12856 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
12857 child_die = child_die->sibling;
12858 }
12859
12860 /* Attach template arguments to type. */
12861 if (!template_args.empty ())
12862 {
12863 has_template_parameters = true;
12864 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12865 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
12866 TYPE_TEMPLATE_ARGUMENTS (type)
12867 = XOBNEWVEC (&objfile->objfile_obstack,
12868 struct symbol *,
12869 TYPE_N_TEMPLATE_ARGUMENTS (type));
12870 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12871 template_args.data (),
12872 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12873 * sizeof (struct symbol *)));
12874 }
12875
12876 /* Attach fields and member functions to the type. */
12877 if (fi.nfields () > 0)
12878 dwarf2_attach_fields_to_type (&fi, type, cu);
12879 if (!fi.fnfieldlists.empty ())
12880 {
12881 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12882
12883 /* Get the type which refers to the base class (possibly this
12884 class itself) which contains the vtable pointer for the current
12885 class from the DW_AT_containing_type attribute. This use of
12886 DW_AT_containing_type is a GNU extension. */
12887
12888 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12889 {
12890 struct type *t = die_containing_type (die, cu);
12891
12892 set_type_vptr_basetype (type, t);
12893 if (type == t)
12894 {
12895 int i;
12896
12897 /* Our own class provides vtbl ptr. */
12898 for (i = t->num_fields () - 1;
12899 i >= TYPE_N_BASECLASSES (t);
12900 --i)
12901 {
12902 const char *fieldname = t->field (i).name ();
12903
12904 if (is_vtable_name (fieldname, cu))
12905 {
12906 set_type_vptr_fieldno (type, i);
12907 break;
12908 }
12909 }
12910
12911 /* Complain if virtual function table field not found. */
12912 if (i < TYPE_N_BASECLASSES (t))
12913 complaint (_("virtual function table pointer "
12914 "not found when defining class '%s'"),
12915 type->name () ? type->name () : "");
12916 }
12917 else
12918 {
12919 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
12920 }
12921 }
12922 else if (cu->producer
12923 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
12924 {
12925 /* The IBM XLC compiler does not provide direct indication
12926 of the containing type, but the vtable pointer is
12927 always named __vfp. */
12928
12929 int i;
12930
12931 for (i = type->num_fields () - 1;
12932 i >= TYPE_N_BASECLASSES (type);
12933 --i)
12934 {
12935 if (strcmp (type->field (i).name (), "__vfp") == 0)
12936 {
12937 set_type_vptr_fieldno (type, i);
12938 set_type_vptr_basetype (type, type);
12939 break;
12940 }
12941 }
12942 }
12943 }
12944
12945 /* Copy fi.typedef_field_list linked list elements content into the
12946 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12947 if (!fi.typedef_field_list.empty ())
12948 {
12949 int count = fi.typedef_field_list.size ();
12950
12951 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12952 /* No zero-initialization is needed, the elements are initialized by
12953 the copy in the loop below. */
12954 TYPE_TYPEDEF_FIELD_ARRAY (type)
12955 = ((struct decl_field *)
12956 TYPE_ALLOC (type,
12957 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
12958 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
12959
12960 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
12961 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
12962 }
12963
12964 /* Copy fi.nested_types_list linked list elements content into the
12965 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
12966 if (!fi.nested_types_list.empty ()
12967 && cu->lang () != language_ada)
12968 {
12969 int count = fi.nested_types_list.size ();
12970
12971 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12972 /* No zero-initialization is needed, the elements are initialized by
12973 the copy in the loop below. */
12974 TYPE_NESTED_TYPES_ARRAY (type)
12975 = ((struct decl_field *)
12976 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
12977 TYPE_NESTED_TYPES_COUNT (type) = count;
12978
12979 for (int i = 0; i < fi.nested_types_list.size (); ++i)
12980 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
12981 }
12982 }
12983
12984 quirk_gcc_member_function_pointer (type, objfile);
12985 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
12986 cu->rust_unions.push_back (type);
12987 else if (cu->lang () == language_ada)
12988 quirk_ada_thick_pointer_struct (die, cu, type);
12989
12990 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12991 snapshots) has been known to create a die giving a declaration
12992 for a class that has, as a child, a die giving a definition for a
12993 nested class. So we have to process our children even if the
12994 current die is a declaration. Normally, of course, a declaration
12995 won't have any children at all. */
12996
12997 child_die = die->child;
12998
12999 while (child_die != NULL && child_die->tag)
13000 {
13001 if (child_die->tag == DW_TAG_member
13002 || child_die->tag == DW_TAG_variable
13003 || child_die->tag == DW_TAG_inheritance
13004 || child_die->tag == DW_TAG_template_value_param
13005 || child_die->tag == DW_TAG_template_type_param)
13006 {
13007 /* Do nothing. */
13008 }
13009 else
13010 process_die (child_die, cu);
13011
13012 child_die = child_die->sibling;
13013 }
13014
13015 /* Do not consider external references. According to the DWARF standard,
13016 these DIEs are identified by the fact that they have no byte_size
13017 attribute, and a declaration attribute. */
13018 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13019 || !die_is_declaration (die, cu)
13020 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
13021 {
13022 struct symbol *sym = new_symbol (die, type, cu);
13023
13024 if (has_template_parameters)
13025 {
13026 struct symtab *symtab;
13027 if (sym != nullptr)
13028 symtab = sym->symtab ();
13029 else if (cu->line_header != nullptr)
13030 {
13031 /* Any related symtab will do. */
13032 symtab
13033 = cu->line_header->file_names ()[0].symtab;
13034 }
13035 else
13036 {
13037 symtab = nullptr;
13038 complaint (_("could not find suitable "
13039 "symtab for template parameter"
13040 " - DIE at %s [in module %s]"),
13041 sect_offset_str (die->sect_off),
13042 objfile_name (objfile));
13043 }
13044
13045 if (symtab != nullptr)
13046 {
13047 /* Make sure that the symtab is set on the new symbols.
13048 Even though they don't appear in this symtab directly,
13049 other parts of gdb assume that symbols do, and this is
13050 reasonably true. */
13051 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
13052 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
13053 }
13054 }
13055 }
13056 }
13057
13058 /* Read DW_AT_endianity from DIE and compute the byte order that
13059 should be used. The CU's arch is used as the default. The result
13060 is true if the returned arch differs from the default, and false if
13061 they are the same. If provided, the out parameter BYTE_ORDER is
13062 also set. */
13063
13064 static bool
13065 die_byte_order (die_info *die, dwarf2_cu *cu, enum bfd_endian *byte_order)
13066 {
13067 gdbarch *arch = cu->per_objfile->objfile->arch ();
13068 enum bfd_endian arch_order = gdbarch_byte_order (arch);
13069 enum bfd_endian new_order = arch_order;
13070
13071 attribute *attr = dwarf2_attr (die, DW_AT_endianity, cu);
13072 if (attr != nullptr && attr->form_is_constant ())
13073 {
13074 int endianity = attr->constant_value (0);
13075
13076 switch (endianity)
13077 {
13078 case DW_END_big:
13079 new_order = BFD_ENDIAN_BIG;
13080 break;
13081 case DW_END_little:
13082 new_order = BFD_ENDIAN_LITTLE;
13083 break;
13084 default:
13085 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
13086 break;
13087 }
13088 }
13089
13090 if (byte_order != nullptr)
13091 *byte_order = new_order;
13092
13093 return new_order != arch_order;
13094 }
13095
13096 /* Assuming DIE is an enumeration type, and TYPE is its associated
13097 type, update TYPE using some information only available in DIE's
13098 children. In particular, the fields are computed. */
13099
13100 static void
13101 update_enumeration_type_from_children (struct die_info *die,
13102 struct type *type,
13103 struct dwarf2_cu *cu)
13104 {
13105 struct die_info *child_die;
13106 int unsigned_enum = 1;
13107 int flag_enum = 1;
13108
13109 auto_obstack obstack;
13110 std::vector<struct field> fields;
13111
13112 for (child_die = die->child;
13113 child_die != NULL && child_die->tag;
13114 child_die = child_die->sibling)
13115 {
13116 struct attribute *attr;
13117 LONGEST value;
13118 const gdb_byte *bytes;
13119 struct dwarf2_locexpr_baton *baton;
13120 const char *name;
13121
13122 if (child_die->tag != DW_TAG_enumerator)
13123 continue;
13124
13125 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13126 if (attr == NULL)
13127 continue;
13128
13129 name = dwarf2_name (child_die, cu);
13130 if (name == NULL)
13131 name = "<anonymous enumerator>";
13132
13133 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13134 &value, &bytes, &baton);
13135 if (value < 0)
13136 {
13137 unsigned_enum = 0;
13138 flag_enum = 0;
13139 }
13140 else
13141 {
13142 if (count_one_bits_ll (value) >= 2)
13143 flag_enum = 0;
13144 }
13145
13146 struct field &field = fields.emplace_back ();
13147 field.set_name (dwarf2_physname (name, child_die, cu));
13148 field.set_loc_enumval (value);
13149 }
13150
13151 if (!fields.empty ())
13152 type->copy_fields (fields);
13153 else
13154 flag_enum = 0;
13155
13156 if (unsigned_enum)
13157 type->set_is_unsigned (true);
13158
13159 if (flag_enum)
13160 type->set_is_flag_enum (true);
13161 }
13162
13163 /* Given a DW_AT_enumeration_type die, set its type. We do not
13164 complete the type's fields yet, or create any symbols. */
13165
13166 static struct type *
13167 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13168 {
13169 struct objfile *objfile = cu->per_objfile->objfile;
13170 struct type *type;
13171 struct attribute *attr;
13172 const char *name;
13173
13174 /* If the definition of this type lives in .debug_types, read that type.
13175 Don't follow DW_AT_specification though, that will take us back up
13176 the chain and we want to go down. */
13177 attr = die->attr (DW_AT_signature);
13178 if (attr != nullptr)
13179 {
13180 type = get_DW_AT_signature_type (die, attr, cu);
13181
13182 /* The type's CU may not be the same as CU.
13183 Ensure TYPE is recorded with CU in die_type_hash. */
13184 return set_die_type (die, type, cu);
13185 }
13186
13187 type = type_allocator (objfile, cu->lang ()).new_type ();
13188
13189 type->set_code (TYPE_CODE_ENUM);
13190 name = dwarf2_full_name (NULL, die, cu);
13191 if (name != NULL)
13192 type->set_name (name);
13193
13194 attr = dwarf2_attr (die, DW_AT_type, cu);
13195 if (attr != NULL)
13196 {
13197 struct type *underlying_type = die_type (die, cu);
13198
13199 type->set_target_type (underlying_type);
13200 }
13201
13202 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13203 if (attr != nullptr)
13204 type->set_length (attr->constant_value (0));
13205 else
13206 type->set_length (0);
13207
13208 maybe_set_alignment (cu, die, type);
13209
13210 /* The enumeration DIE can be incomplete. In Ada, any type can be
13211 declared as private in the package spec, and then defined only
13212 inside the package body. Such types are known as Taft Amendment
13213 Types. When another package uses such a type, an incomplete DIE
13214 may be generated by the compiler. */
13215 if (die_is_declaration (die, cu))
13216 type->set_is_stub (true);
13217
13218 /* If this type has an underlying type that is not a stub, then we
13219 may use its attributes. We always use the "unsigned" attribute
13220 in this situation, because ordinarily we guess whether the type
13221 is unsigned -- but the guess can be wrong and the underlying type
13222 can tell us the reality. However, we defer to a local size
13223 attribute if one exists, because this lets the compiler override
13224 the underlying type if needed. */
13225 if (type->target_type () != NULL && !type->target_type ()->is_stub ())
13226 {
13227 struct type *underlying_type = type->target_type ();
13228 underlying_type = check_typedef (underlying_type);
13229
13230 type->set_is_unsigned (underlying_type->is_unsigned ());
13231
13232 if (type->length () == 0)
13233 type->set_length (underlying_type->length ());
13234
13235 if (TYPE_RAW_ALIGN (type) == 0
13236 && TYPE_RAW_ALIGN (underlying_type) != 0)
13237 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
13238 }
13239
13240 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
13241
13242 type->set_endianity_is_not_default (die_byte_order (die, cu, nullptr));
13243
13244 set_die_type (die, type, cu);
13245
13246 /* Finish the creation of this type by using the enum's children.
13247 Note that, as usual, this must come after set_die_type to avoid
13248 infinite recursion when trying to compute the names of the
13249 enumerators. */
13250 update_enumeration_type_from_children (die, type, cu);
13251
13252 return type;
13253 }
13254
13255 /* Given a pointer to a die which begins an enumeration, process all
13256 the dies that define the members of the enumeration, and create the
13257 symbol for the enumeration type.
13258
13259 NOTE: We reverse the order of the element list. */
13260
13261 static void
13262 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13263 {
13264 struct type *this_type;
13265
13266 this_type = get_die_type (die, cu);
13267 if (this_type == NULL)
13268 this_type = read_enumeration_type (die, cu);
13269
13270 if (die->child != NULL)
13271 {
13272 struct die_info *child_die;
13273 const char *name;
13274
13275 child_die = die->child;
13276 while (child_die && child_die->tag)
13277 {
13278 if (child_die->tag != DW_TAG_enumerator)
13279 {
13280 process_die (child_die, cu);
13281 }
13282 else
13283 {
13284 name = dwarf2_name (child_die, cu);
13285 if (name)
13286 new_symbol (child_die, this_type, cu);
13287 }
13288
13289 child_die = child_die->sibling;
13290 }
13291 }
13292
13293 /* If we are reading an enum from a .debug_types unit, and the enum
13294 is a declaration, and the enum is not the signatured type in the
13295 unit, then we do not want to add a symbol for it. Adding a
13296 symbol would in some cases obscure the true definition of the
13297 enum, giving users an incomplete type when the definition is
13298 actually available. Note that we do not want to do this for all
13299 enums which are just declarations, because C++0x allows forward
13300 enum declarations. */
13301 if (cu->per_cu->is_debug_types
13302 && die_is_declaration (die, cu))
13303 {
13304 struct signatured_type *sig_type;
13305
13306 sig_type = (struct signatured_type *) cu->per_cu;
13307 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
13308 if (sig_type->type_offset_in_section != die->sect_off)
13309 return;
13310 }
13311
13312 new_symbol (die, this_type, cu);
13313 }
13314
13315 /* Helper function for quirk_ada_thick_pointer that examines a bounds
13316 expression for an index type and finds the corresponding field
13317 offset in the hidden "P_BOUNDS" structure. Returns true on success
13318 and updates *FIELD, false if it fails to recognize an
13319 expression. */
13320
13321 static bool
13322 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
13323 int *bounds_offset, struct field *field,
13324 struct dwarf2_cu *cu)
13325 {
13326 struct attribute *attr = dwarf2_attr (die, name, cu);
13327 if (attr == nullptr || !attr->form_is_block ())
13328 return false;
13329
13330 const struct dwarf_block *block = attr->as_block ();
13331 const gdb_byte *start = block->data;
13332 const gdb_byte *end = block->data + block->size;
13333
13334 /* The expression to recognize generally looks like:
13335
13336 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13337 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13338
13339 However, the second "plus_uconst" may be missing:
13340
13341 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13342 DW_OP_deref_size: 4)
13343
13344 This happens when the field is at the start of the structure.
13345
13346 Also, the final deref may not be sized:
13347
13348 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
13349 DW_OP_deref)
13350
13351 This happens when the size of the index type happens to be the
13352 same as the architecture's word size. This can occur with or
13353 without the second plus_uconst. */
13354
13355 if (end - start < 2)
13356 return false;
13357 if (*start++ != DW_OP_push_object_address)
13358 return false;
13359 if (*start++ != DW_OP_plus_uconst)
13360 return false;
13361
13362 uint64_t this_bound_off;
13363 start = gdb_read_uleb128 (start, end, &this_bound_off);
13364 if (start == nullptr || (int) this_bound_off != this_bound_off)
13365 return false;
13366 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
13367 is consistent among all bounds. */
13368 if (*bounds_offset == -1)
13369 *bounds_offset = this_bound_off;
13370 else if (*bounds_offset != this_bound_off)
13371 return false;
13372
13373 if (start == end || *start++ != DW_OP_deref)
13374 return false;
13375
13376 int offset = 0;
13377 if (start ==end)
13378 return false;
13379 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
13380 {
13381 /* This means an offset of 0. */
13382 }
13383 else if (*start++ != DW_OP_plus_uconst)
13384 return false;
13385 else
13386 {
13387 /* The size is the parameter to DW_OP_plus_uconst. */
13388 uint64_t val;
13389 start = gdb_read_uleb128 (start, end, &val);
13390 if (start == nullptr)
13391 return false;
13392 if ((int) val != val)
13393 return false;
13394 offset = val;
13395 }
13396
13397 if (start == end)
13398 return false;
13399
13400 uint64_t size;
13401 if (*start == DW_OP_deref_size)
13402 {
13403 start = gdb_read_uleb128 (start + 1, end, &size);
13404 if (start == nullptr)
13405 return false;
13406 }
13407 else if (*start == DW_OP_deref)
13408 {
13409 size = cu->header.addr_size;
13410 ++start;
13411 }
13412 else
13413 return false;
13414
13415 field->set_loc_bitpos (8 * offset);
13416 if (size != field->type ()->length ())
13417 field->set_bitsize (8 * size);
13418
13419 return true;
13420 }
13421
13422 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
13423 some kinds of Ada arrays:
13424
13425 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
13426 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
13427 <11e0> DW_AT_data_location: 2 byte block: 97 6
13428 (DW_OP_push_object_address; DW_OP_deref)
13429 <11e3> DW_AT_type : <0x1173>
13430 <11e7> DW_AT_sibling : <0x1201>
13431 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
13432 <11ec> DW_AT_type : <0x1206>
13433 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
13434 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13435 DW_OP_deref_size: 4)
13436 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
13437 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13438 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13439
13440 This actually represents a "thick pointer", which is a structure
13441 with two elements: one that is a pointer to the array data, and one
13442 that is a pointer to another structure; this second structure holds
13443 the array bounds.
13444
13445 This returns a new type on success, or nullptr if this didn't
13446 recognize the type. */
13447
13448 static struct type *
13449 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
13450 struct type *type)
13451 {
13452 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
13453 /* So far we've only seen this with block form. */
13454 if (attr == nullptr || !attr->form_is_block ())
13455 return nullptr;
13456
13457 /* Note that this will fail if the structure layout is changed by
13458 the compiler. However, we have no good way to recognize some
13459 other layout, because we don't know what expression the compiler
13460 might choose to emit should this happen. */
13461 struct dwarf_block *blk = attr->as_block ();
13462 if (blk->size != 2
13463 || blk->data[0] != DW_OP_push_object_address
13464 || blk->data[1] != DW_OP_deref)
13465 return nullptr;
13466
13467 int bounds_offset = -1;
13468 int max_align = -1;
13469 std::vector<struct field> range_fields;
13470 for (struct die_info *child_die = die->child;
13471 child_die;
13472 child_die = child_die->sibling)
13473 {
13474 if (child_die->tag == DW_TAG_subrange_type)
13475 {
13476 struct type *underlying = read_subrange_index_type (child_die, cu);
13477
13478 int this_align = type_align (underlying);
13479 if (this_align > max_align)
13480 max_align = this_align;
13481
13482 range_fields.emplace_back ();
13483 range_fields.emplace_back ();
13484
13485 struct field &lower = range_fields[range_fields.size () - 2];
13486 struct field &upper = range_fields[range_fields.size () - 1];
13487
13488 lower.set_type (underlying);
13489 lower.set_is_artificial (true);
13490
13491 upper.set_type (underlying);
13492 upper.set_is_artificial (true);
13493
13494 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
13495 &bounds_offset, &lower, cu)
13496 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
13497 &bounds_offset, &upper, cu))
13498 return nullptr;
13499 }
13500 }
13501
13502 /* This shouldn't really happen, but double-check that we found
13503 where the bounds are stored. */
13504 if (bounds_offset == -1)
13505 return nullptr;
13506
13507 struct objfile *objfile = cu->per_objfile->objfile;
13508 for (int i = 0; i < range_fields.size (); i += 2)
13509 {
13510 char name[20];
13511
13512 /* Set the name of each field in the bounds. */
13513 xsnprintf (name, sizeof (name), "LB%d", i / 2);
13514 range_fields[i].set_name (objfile->intern (name));
13515 xsnprintf (name, sizeof (name), "UB%d", i / 2);
13516 range_fields[i + 1].set_name (objfile->intern (name));
13517 }
13518
13519 type_allocator alloc (objfile, cu->lang ());
13520 struct type *bounds = alloc.new_type ();
13521 bounds->set_code (TYPE_CODE_STRUCT);
13522
13523 bounds->copy_fields (range_fields);
13524
13525 int last_fieldno = range_fields.size () - 1;
13526 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
13527 + bounds->field (last_fieldno).type ()->length ());
13528 bounds->set_length (align_up (bounds_size, max_align));
13529
13530 /* Rewrite the existing array type in place. Specifically, we
13531 remove any dynamic properties we might have read, and we replace
13532 the index types. */
13533 struct type *iter = type;
13534 for (int i = 0; i < range_fields.size (); i += 2)
13535 {
13536 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
13537 iter->main_type->dyn_prop_list = nullptr;
13538 iter->set_index_type
13539 (create_static_range_type (alloc, bounds->field (i).type (), 1, 0));
13540 iter = iter->target_type ();
13541 }
13542
13543 struct type *result = type_allocator (objfile, cu->lang ()).new_type ();
13544 result->set_code (TYPE_CODE_STRUCT);
13545
13546 result->alloc_fields (2);
13547
13548 /* The names are chosen to coincide with what the compiler does with
13549 -fgnat-encodings=all, which the Ada code in gdb already
13550 understands. */
13551 result->field (0).set_name ("P_ARRAY");
13552 result->field (0).set_type (lookup_pointer_type (type));
13553
13554 result->field (1).set_name ("P_BOUNDS");
13555 result->field (1).set_type (lookup_pointer_type (bounds));
13556 result->field (1).set_loc_bitpos (8 * bounds_offset);
13557
13558 result->set_name (type->name ());
13559 result->set_length (result->field (0).type ()->length ()
13560 + result->field (1).type ()->length ());
13561
13562 return result;
13563 }
13564
13565 /* Extract all information from a DW_TAG_array_type DIE and put it in
13566 the DIE's type field. For now, this only handles one dimensional
13567 arrays. */
13568
13569 static struct type *
13570 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13571 {
13572 struct objfile *objfile = cu->per_objfile->objfile;
13573 struct die_info *child_die;
13574 struct type *type;
13575 struct type *element_type, *range_type, *index_type;
13576 struct attribute *attr;
13577 const char *name;
13578 struct dynamic_prop *byte_stride_prop = NULL;
13579 unsigned int bit_stride = 0;
13580
13581 element_type = die_type (die, cu);
13582
13583 /* The die_type call above may have already set the type for this DIE. */
13584 type = get_die_type (die, cu);
13585 if (type)
13586 return type;
13587
13588 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13589 if (attr != NULL)
13590 {
13591 int stride_ok;
13592 struct type *prop_type = cu->addr_sized_int_type (false);
13593
13594 byte_stride_prop
13595 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
13596 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
13597 prop_type);
13598 if (!stride_ok)
13599 {
13600 complaint (_("unable to read array DW_AT_byte_stride "
13601 " - DIE at %s [in module %s]"),
13602 sect_offset_str (die->sect_off),
13603 objfile_name (cu->per_objfile->objfile));
13604 /* Ignore this attribute. We will likely not be able to print
13605 arrays of this type correctly, but there is little we can do
13606 to help if we cannot read the attribute's value. */
13607 byte_stride_prop = NULL;
13608 }
13609 }
13610
13611 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13612 if (attr != NULL)
13613 bit_stride = attr->constant_value (0);
13614
13615 /* Irix 6.2 native cc creates array types without children for
13616 arrays with unspecified length. */
13617 if (die->child == NULL)
13618 {
13619 index_type = builtin_type (objfile)->builtin_int;
13620 type_allocator alloc (objfile, cu->lang ());
13621 range_type = create_static_range_type (alloc, index_type, 0, -1);
13622 type = create_array_type_with_stride (alloc, element_type, range_type,
13623 byte_stride_prop, bit_stride);
13624 return set_die_type (die, type, cu);
13625 }
13626
13627 std::vector<struct type *> range_types;
13628 child_die = die->child;
13629 while (child_die && child_die->tag)
13630 {
13631 if (child_die->tag == DW_TAG_subrange_type
13632 || child_die->tag == DW_TAG_generic_subrange)
13633 {
13634 struct type *child_type = read_type_die (child_die, cu);
13635
13636 if (child_type != NULL)
13637 {
13638 /* The range type was successfully read. Save it for the
13639 array type creation. */
13640 range_types.push_back (child_type);
13641 }
13642 }
13643 child_die = child_die->sibling;
13644 }
13645
13646 if (range_types.empty ())
13647 {
13648 complaint (_("unable to find array range - DIE at %s [in module %s]"),
13649 sect_offset_str (die->sect_off),
13650 objfile_name (cu->per_objfile->objfile));
13651 return NULL;
13652 }
13653
13654 /* Dwarf2 dimensions are output from left to right, create the
13655 necessary array types in backwards order. */
13656
13657 type = element_type;
13658
13659 type_allocator alloc (cu->per_objfile->objfile, cu->lang ());
13660 if (read_array_order (die, cu) == DW_ORD_col_major)
13661 {
13662 int i = 0;
13663
13664 while (i < range_types.size ())
13665 {
13666 type = create_array_type_with_stride (alloc, type, range_types[i++],
13667 byte_stride_prop, bit_stride);
13668 type->set_is_multi_dimensional (true);
13669 bit_stride = 0;
13670 byte_stride_prop = nullptr;
13671 }
13672 }
13673 else
13674 {
13675 size_t ndim = range_types.size ();
13676 while (ndim-- > 0)
13677 {
13678 type = create_array_type_with_stride (alloc, type, range_types[ndim],
13679 byte_stride_prop, bit_stride);
13680 type->set_is_multi_dimensional (true);
13681 bit_stride = 0;
13682 byte_stride_prop = nullptr;
13683 }
13684 }
13685
13686 /* Clear the flag on the outermost array type. */
13687 type->set_is_multi_dimensional (false);
13688 gdb_assert (type != element_type);
13689
13690 /* Understand Dwarf2 support for vector types (like they occur on
13691 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13692 array type. This is not part of the Dwarf2/3 standard yet, but a
13693 custom vendor extension. The main difference between a regular
13694 array and the vector variant is that vectors are passed by value
13695 to functions. */
13696 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13697 if (attr != nullptr)
13698 make_vector_type (type);
13699
13700 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13701 implementation may choose to implement triple vectors using this
13702 attribute. */
13703 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13704 if (attr != nullptr && attr->form_is_unsigned ())
13705 {
13706 if (attr->as_unsigned () >= type->length ())
13707 type->set_length (attr->as_unsigned ());
13708 else
13709 complaint (_("DW_AT_byte_size for array type smaller "
13710 "than the total size of elements"));
13711 }
13712
13713 name = dwarf2_name (die, cu);
13714 if (name)
13715 type->set_name (name);
13716
13717 maybe_set_alignment (cu, die, type);
13718
13719 struct type *replacement_type = nullptr;
13720 if (cu->lang () == language_ada)
13721 {
13722 replacement_type = quirk_ada_thick_pointer (die, cu, type);
13723 if (replacement_type != nullptr)
13724 type = replacement_type;
13725 }
13726
13727 /* Install the type in the die. */
13728 set_die_type (die, type, cu, replacement_type != nullptr);
13729
13730 /* set_die_type should be already done. */
13731 set_descriptive_type (type, die, cu);
13732
13733 return type;
13734 }
13735
13736 static enum dwarf_array_dim_ordering
13737 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13738 {
13739 struct attribute *attr;
13740
13741 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13742
13743 if (attr != nullptr)
13744 {
13745 LONGEST val = attr->constant_value (-1);
13746 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
13747 return (enum dwarf_array_dim_ordering) val;
13748 }
13749
13750 /* GNU F77 is a special case, as at 08/2004 array type info is the
13751 opposite order to the dwarf2 specification, but data is still
13752 laid out as per normal fortran.
13753
13754 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13755 version checking. */
13756
13757 if (cu->lang () == language_fortran
13758 && cu->producer && strstr (cu->producer, "GNU F77"))
13759 {
13760 return DW_ORD_row_major;
13761 }
13762
13763 switch (cu->language_defn->array_ordering ())
13764 {
13765 case array_column_major:
13766 return DW_ORD_col_major;
13767 case array_row_major:
13768 default:
13769 return DW_ORD_row_major;
13770 };
13771 }
13772
13773 /* Extract all information from a DW_TAG_set_type DIE and put it in
13774 the DIE's type field. */
13775
13776 static struct type *
13777 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13778 {
13779 struct type *domain_type, *set_type;
13780 struct attribute *attr;
13781
13782 domain_type = die_type (die, cu);
13783
13784 /* The die_type call above may have already set the type for this DIE. */
13785 set_type = get_die_type (die, cu);
13786 if (set_type)
13787 return set_type;
13788
13789 type_allocator alloc (cu->per_objfile->objfile, cu->lang ());
13790 set_type = create_set_type (alloc, domain_type);
13791
13792 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13793 if (attr != nullptr && attr->form_is_unsigned ())
13794 set_type->set_length (attr->as_unsigned ());
13795
13796 maybe_set_alignment (cu, die, set_type);
13797
13798 return set_die_type (die, set_type, cu);
13799 }
13800
13801 /* A helper for read_common_block that creates a locexpr baton.
13802 SYM is the symbol which we are marking as computed.
13803 COMMON_DIE is the DIE for the common block.
13804 COMMON_LOC is the location expression attribute for the common
13805 block itself.
13806 MEMBER_LOC is the location expression attribute for the particular
13807 member of the common block that we are processing.
13808 CU is the CU from which the above come. */
13809
13810 static void
13811 mark_common_block_symbol_computed (struct symbol *sym,
13812 struct die_info *common_die,
13813 struct attribute *common_loc,
13814 struct attribute *member_loc,
13815 struct dwarf2_cu *cu)
13816 {
13817 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13818 struct objfile *objfile = per_objfile->objfile;
13819 struct dwarf2_locexpr_baton *baton;
13820 gdb_byte *ptr;
13821 unsigned int cu_off;
13822 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
13823 LONGEST offset = 0;
13824
13825 gdb_assert (common_loc && member_loc);
13826 gdb_assert (common_loc->form_is_block ());
13827 gdb_assert (member_loc->form_is_block ()
13828 || member_loc->form_is_constant ());
13829
13830 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13831 baton->per_objfile = per_objfile;
13832 baton->per_cu = cu->per_cu;
13833 gdb_assert (baton->per_cu);
13834
13835 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13836
13837 if (member_loc->form_is_constant ())
13838 {
13839 offset = member_loc->constant_value (0);
13840 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13841 }
13842 else
13843 baton->size += member_loc->as_block ()->size;
13844
13845 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
13846 baton->data = ptr;
13847
13848 *ptr++ = DW_OP_call4;
13849 cu_off = common_die->sect_off - cu->per_cu->sect_off;
13850 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13851 ptr += 4;
13852
13853 if (member_loc->form_is_constant ())
13854 {
13855 *ptr++ = DW_OP_addr;
13856 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13857 ptr += cu->header.addr_size;
13858 }
13859 else
13860 {
13861 /* We have to copy the data here, because DW_OP_call4 will only
13862 use a DW_AT_location attribute. */
13863 struct dwarf_block *block = member_loc->as_block ();
13864 memcpy (ptr, block->data, block->size);
13865 ptr += block->size;
13866 }
13867
13868 *ptr++ = DW_OP_plus;
13869 gdb_assert (ptr - baton->data == baton->size);
13870
13871 SYMBOL_LOCATION_BATON (sym) = baton;
13872 sym->set_aclass_index (dwarf2_locexpr_index);
13873 }
13874
13875 /* Create appropriate locally-scoped variables for all the
13876 DW_TAG_common_block entries. Also create a struct common_block
13877 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13878 is used to separate the common blocks name namespace from regular
13879 variable names. */
13880
13881 static void
13882 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13883 {
13884 struct attribute *attr;
13885
13886 attr = dwarf2_attr (die, DW_AT_location, cu);
13887 if (attr != nullptr)
13888 {
13889 /* Support the .debug_loc offsets. */
13890 if (attr->form_is_block ())
13891 {
13892 /* Ok. */
13893 }
13894 else if (attr->form_is_section_offset ())
13895 {
13896 dwarf2_complex_location_expr_complaint ();
13897 attr = NULL;
13898 }
13899 else
13900 {
13901 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13902 "common block member");
13903 attr = NULL;
13904 }
13905 }
13906
13907 if (die->child != NULL)
13908 {
13909 struct objfile *objfile = cu->per_objfile->objfile;
13910 struct die_info *child_die;
13911 size_t n_entries = 0, size;
13912 struct common_block *common_block;
13913 struct symbol *sym;
13914
13915 for (child_die = die->child;
13916 child_die && child_die->tag;
13917 child_die = child_die->sibling)
13918 ++n_entries;
13919
13920 size = (sizeof (struct common_block)
13921 + (n_entries - 1) * sizeof (struct symbol *));
13922 common_block
13923 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
13924 size);
13925 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13926 common_block->n_entries = 0;
13927
13928 for (child_die = die->child;
13929 child_die && child_die->tag;
13930 child_die = child_die->sibling)
13931 {
13932 /* Create the symbol in the DW_TAG_common_block block in the current
13933 symbol scope. */
13934 sym = new_symbol (child_die, NULL, cu);
13935 if (sym != NULL)
13936 {
13937 struct attribute *member_loc;
13938
13939 common_block->contents[common_block->n_entries++] = sym;
13940
13941 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13942 cu);
13943 if (member_loc)
13944 {
13945 /* GDB has handled this for a long time, but it is
13946 not specified by DWARF. It seems to have been
13947 emitted by gfortran at least as recently as:
13948 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
13949 complaint (_("Variable in common block has "
13950 "DW_AT_data_member_location "
13951 "- DIE at %s [in module %s]"),
13952 sect_offset_str (child_die->sect_off),
13953 objfile_name (objfile));
13954
13955 if (member_loc->form_is_section_offset ())
13956 dwarf2_complex_location_expr_complaint ();
13957 else if (member_loc->form_is_constant ()
13958 || member_loc->form_is_block ())
13959 {
13960 if (attr != nullptr)
13961 mark_common_block_symbol_computed (sym, die, attr,
13962 member_loc, cu);
13963 }
13964 else
13965 dwarf2_complex_location_expr_complaint ();
13966 }
13967 }
13968 }
13969
13970 sym = new_symbol (die, builtin_type (objfile)->builtin_void, cu);
13971 sym->set_value_common_block (common_block);
13972 }
13973 }
13974
13975 /* Create a type for a C++ namespace. */
13976
13977 static struct type *
13978 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
13979 {
13980 struct objfile *objfile = cu->per_objfile->objfile;
13981 const char *previous_prefix, *name;
13982 int is_anonymous;
13983 struct type *type;
13984
13985 /* For extensions, reuse the type of the original namespace. */
13986 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
13987 {
13988 struct die_info *ext_die;
13989 struct dwarf2_cu *ext_cu = cu;
13990
13991 ext_die = dwarf2_extension (die, &ext_cu);
13992 type = read_type_die (ext_die, ext_cu);
13993
13994 /* EXT_CU may not be the same as CU.
13995 Ensure TYPE is recorded with CU in die_type_hash. */
13996 return set_die_type (die, type, cu);
13997 }
13998
13999 name = namespace_name (die, &is_anonymous, cu);
14000
14001 /* Now build the name of the current namespace. */
14002
14003 previous_prefix = determine_prefix (die, cu);
14004 if (previous_prefix[0] != '\0')
14005 name = typename_concat (&objfile->objfile_obstack,
14006 previous_prefix, name, 0, cu);
14007
14008 /* Create the type. */
14009 type = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_NAMESPACE,
14010 0, name);
14011
14012 return set_die_type (die, type, cu);
14013 }
14014
14015 /* Read a namespace scope. */
14016
14017 static void
14018 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14019 {
14020 struct objfile *objfile = cu->per_objfile->objfile;
14021 int is_anonymous;
14022
14023 /* Add a symbol associated to this if we haven't seen the namespace
14024 before. Also, add a using directive if it's an anonymous
14025 namespace. */
14026
14027 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14028 {
14029 struct type *type;
14030
14031 type = read_type_die (die, cu);
14032 new_symbol (die, type, cu);
14033
14034 namespace_name (die, &is_anonymous, cu);
14035 if (is_anonymous)
14036 {
14037 const char *previous_prefix = determine_prefix (die, cu);
14038
14039 std::vector<const char *> excludes;
14040 add_using_directive (using_directives (cu),
14041 previous_prefix, type->name (), NULL,
14042 NULL, excludes,
14043 read_decl_line (die, cu),
14044 &objfile->objfile_obstack);
14045 }
14046 }
14047
14048 if (die->child != NULL)
14049 {
14050 struct die_info *child_die = die->child;
14051
14052 while (child_die && child_die->tag)
14053 {
14054 process_die (child_die, cu);
14055 child_die = child_die->sibling;
14056 }
14057 }
14058 }
14059
14060 /* Read a Fortran module as type. This DIE can be only a declaration used for
14061 imported module. Still we need that type as local Fortran "use ... only"
14062 declaration imports depend on the created type in determine_prefix. */
14063
14064 static struct type *
14065 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14066 {
14067 struct objfile *objfile = cu->per_objfile->objfile;
14068 const char *module_name;
14069 struct type *type;
14070
14071 module_name = dwarf2_name (die, cu);
14072 type = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_MODULE,
14073 0, module_name);
14074
14075 return set_die_type (die, type, cu);
14076 }
14077
14078 /* Read a Fortran module. */
14079
14080 static void
14081 read_module (struct die_info *die, struct dwarf2_cu *cu)
14082 {
14083 struct die_info *child_die = die->child;
14084 struct type *type;
14085
14086 type = read_type_die (die, cu);
14087 new_symbol (die, type, cu);
14088
14089 while (child_die && child_die->tag)
14090 {
14091 process_die (child_die, cu);
14092 child_die = child_die->sibling;
14093 }
14094 }
14095
14096 /* Return the name of the namespace represented by DIE. Set
14097 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14098 namespace. */
14099
14100 static const char *
14101 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14102 {
14103 struct die_info *current_die;
14104 const char *name = NULL;
14105
14106 /* Loop through the extensions until we find a name. */
14107
14108 for (current_die = die;
14109 current_die != NULL;
14110 current_die = dwarf2_extension (die, &cu))
14111 {
14112 /* We don't use dwarf2_name here so that we can detect the absence
14113 of a name -> anonymous namespace. */
14114 name = dwarf2_string_attr (die, DW_AT_name, cu);
14115
14116 if (name != NULL)
14117 break;
14118 }
14119
14120 /* Is it an anonymous namespace? */
14121
14122 *is_anonymous = (name == NULL);
14123 if (*is_anonymous)
14124 name = CP_ANONYMOUS_NAMESPACE_STR;
14125
14126 return name;
14127 }
14128
14129 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14130 the user defined type vector. */
14131
14132 static struct type *
14133 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14134 {
14135 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
14136 struct comp_unit_head *cu_header = &cu->header;
14137 struct type *type;
14138 struct attribute *attr_byte_size;
14139 struct attribute *attr_address_class;
14140 int byte_size, addr_class;
14141 struct type *target_type;
14142
14143 target_type = die_type (die, cu);
14144
14145 /* The die_type call above may have already set the type for this DIE. */
14146 type = get_die_type (die, cu);
14147 if (type)
14148 return type;
14149
14150 type = lookup_pointer_type (target_type);
14151
14152 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14153 if (attr_byte_size)
14154 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
14155 else
14156 byte_size = cu_header->addr_size;
14157
14158 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14159 if (attr_address_class)
14160 addr_class = attr_address_class->constant_value (DW_ADDR_none);
14161 else
14162 addr_class = DW_ADDR_none;
14163
14164 ULONGEST alignment = get_alignment (cu, die);
14165
14166 /* If the pointer size, alignment, or address class is different
14167 than the default, create a type variant marked as such and set
14168 the length accordingly. */
14169 if (type->length () != byte_size
14170 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
14171 && alignment != TYPE_RAW_ALIGN (type))
14172 || addr_class != DW_ADDR_none)
14173 {
14174 if (gdbarch_address_class_type_flags_p (gdbarch))
14175 {
14176 type_instance_flags type_flags
14177 = gdbarch_address_class_type_flags (gdbarch, byte_size,
14178 addr_class);
14179 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14180 == 0);
14181 type = make_type_with_address_space (type, type_flags);
14182 }
14183 else if (type->length () != byte_size)
14184 {
14185 complaint (_("invalid pointer size %d"), byte_size);
14186 }
14187 else if (TYPE_RAW_ALIGN (type) != alignment)
14188 {
14189 complaint (_("Invalid DW_AT_alignment"
14190 " - DIE at %s [in module %s]"),
14191 sect_offset_str (die->sect_off),
14192 objfile_name (cu->per_objfile->objfile));
14193 }
14194 else
14195 {
14196 /* Should we also complain about unhandled address classes? */
14197 }
14198 }
14199
14200 type->set_length (byte_size);
14201 set_type_align (type, alignment);
14202 return set_die_type (die, type, cu);
14203 }
14204
14205 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14206 the user defined type vector. */
14207
14208 static struct type *
14209 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14210 {
14211 struct type *type;
14212 struct type *to_type;
14213 struct type *domain;
14214
14215 to_type = die_type (die, cu);
14216 domain = die_containing_type (die, cu);
14217
14218 /* The calls above may have already set the type for this DIE. */
14219 type = get_die_type (die, cu);
14220 if (type)
14221 return type;
14222
14223 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
14224 type = lookup_methodptr_type (to_type);
14225 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
14226 {
14227 struct type *new_type
14228 = type_allocator (cu->per_objfile->objfile, cu->lang ()).new_type ();
14229
14230 smash_to_method_type (new_type, domain, to_type->target_type (),
14231 to_type->fields (), to_type->num_fields (),
14232 to_type->has_varargs ());
14233 type = lookup_methodptr_type (new_type);
14234 }
14235 else
14236 type = lookup_memberptr_type (to_type, domain);
14237
14238 return set_die_type (die, type, cu);
14239 }
14240
14241 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14242 the user defined type vector. */
14243
14244 static struct type *
14245 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14246 enum type_code refcode)
14247 {
14248 struct comp_unit_head *cu_header = &cu->header;
14249 struct type *type, *target_type;
14250 struct attribute *attr;
14251
14252 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14253
14254 target_type = die_type (die, cu);
14255
14256 /* The die_type call above may have already set the type for this DIE. */
14257 type = get_die_type (die, cu);
14258 if (type)
14259 return type;
14260
14261 type = lookup_reference_type (target_type, refcode);
14262 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14263 if (attr != nullptr)
14264 type->set_length (attr->constant_value (cu_header->addr_size));
14265 else
14266 type->set_length (cu_header->addr_size);
14267
14268 maybe_set_alignment (cu, die, type);
14269 return set_die_type (die, type, cu);
14270 }
14271
14272 /* Add the given cv-qualifiers to the element type of the array. GCC
14273 outputs DWARF type qualifiers that apply to an array, not the
14274 element type. But GDB relies on the array element type to carry
14275 the cv-qualifiers. This mimics section 6.7.3 of the C99
14276 specification. */
14277
14278 static struct type *
14279 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14280 struct type *base_type, int cnst, int voltl)
14281 {
14282 struct type *el_type, *inner_array;
14283
14284 base_type = copy_type (base_type);
14285 inner_array = base_type;
14286
14287 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
14288 {
14289 inner_array->set_target_type (copy_type (inner_array->target_type ()));
14290 inner_array = inner_array->target_type ();
14291 }
14292
14293 el_type = inner_array->target_type ();
14294 cnst |= TYPE_CONST (el_type);
14295 voltl |= TYPE_VOLATILE (el_type);
14296 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
14297
14298 return set_die_type (die, base_type, cu);
14299 }
14300
14301 static struct type *
14302 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14303 {
14304 struct type *base_type, *cv_type;
14305
14306 base_type = die_type (die, cu);
14307
14308 /* The die_type call above may have already set the type for this DIE. */
14309 cv_type = get_die_type (die, cu);
14310 if (cv_type)
14311 return cv_type;
14312
14313 /* In case the const qualifier is applied to an array type, the element type
14314 is so qualified, not the array type (section 6.7.3 of C99). */
14315 if (base_type->code () == TYPE_CODE_ARRAY)
14316 return add_array_cv_type (die, cu, base_type, 1, 0);
14317
14318 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14319 return set_die_type (die, cv_type, cu);
14320 }
14321
14322 static struct type *
14323 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14324 {
14325 struct type *base_type, *cv_type;
14326
14327 base_type = die_type (die, cu);
14328
14329 /* The die_type call above may have already set the type for this DIE. */
14330 cv_type = get_die_type (die, cu);
14331 if (cv_type)
14332 return cv_type;
14333
14334 /* In case the volatile qualifier is applied to an array type, the
14335 element type is so qualified, not the array type (section 6.7.3
14336 of C99). */
14337 if (base_type->code () == TYPE_CODE_ARRAY)
14338 return add_array_cv_type (die, cu, base_type, 0, 1);
14339
14340 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14341 return set_die_type (die, cv_type, cu);
14342 }
14343
14344 /* Handle DW_TAG_restrict_type. */
14345
14346 static struct type *
14347 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14348 {
14349 struct type *base_type, *cv_type;
14350
14351 base_type = die_type (die, cu);
14352
14353 /* The die_type call above may have already set the type for this DIE. */
14354 cv_type = get_die_type (die, cu);
14355 if (cv_type)
14356 return cv_type;
14357
14358 cv_type = make_restrict_type (base_type);
14359 return set_die_type (die, cv_type, cu);
14360 }
14361
14362 /* Handle DW_TAG_atomic_type. */
14363
14364 static struct type *
14365 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14366 {
14367 struct type *base_type, *cv_type;
14368
14369 base_type = die_type (die, cu);
14370
14371 /* The die_type call above may have already set the type for this DIE. */
14372 cv_type = get_die_type (die, cu);
14373 if (cv_type)
14374 return cv_type;
14375
14376 cv_type = make_atomic_type (base_type);
14377 return set_die_type (die, cv_type, cu);
14378 }
14379
14380 /* Extract all information from a DW_TAG_string_type DIE and add to
14381 the user defined type vector. It isn't really a user defined type,
14382 but it behaves like one, with other DIE's using an AT_user_def_type
14383 attribute to reference it. */
14384
14385 static struct type *
14386 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14387 {
14388 struct objfile *objfile = cu->per_objfile->objfile;
14389 struct gdbarch *gdbarch = objfile->arch ();
14390 struct type *type, *range_type, *index_type, *char_type;
14391 struct attribute *attr;
14392 struct dynamic_prop prop;
14393 bool length_is_constant = true;
14394 LONGEST length;
14395
14396 /* There are a couple of places where bit sizes might be made use of
14397 when parsing a DW_TAG_string_type, however, no producer that we know
14398 of make use of these. Handling bit sizes that are a multiple of the
14399 byte size is easy enough, but what about other bit sizes? Lets deal
14400 with that problem when we have to. Warn about these attributes being
14401 unsupported, then parse the type and ignore them like we always
14402 have. */
14403 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
14404 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
14405 {
14406 static bool warning_printed = false;
14407 if (!warning_printed)
14408 {
14409 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
14410 "currently supported on DW_TAG_string_type."));
14411 warning_printed = true;
14412 }
14413 }
14414
14415 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14416 if (attr != nullptr && !attr->form_is_constant ())
14417 {
14418 /* The string length describes the location at which the length of
14419 the string can be found. The size of the length field can be
14420 specified with one of the attributes below. */
14421 struct type *prop_type;
14422 struct attribute *len
14423 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
14424 if (len == nullptr)
14425 len = dwarf2_attr (die, DW_AT_byte_size, cu);
14426 if (len != nullptr && len->form_is_constant ())
14427 {
14428 /* Pass 0 as the default as we know this attribute is constant
14429 and the default value will not be returned. */
14430 LONGEST sz = len->constant_value (0);
14431 prop_type = objfile_int_type (objfile, sz, true);
14432 }
14433 else
14434 {
14435 /* If the size is not specified then we assume it is the size of
14436 an address on this target. */
14437 prop_type = cu->addr_sized_int_type (true);
14438 }
14439
14440 /* Convert the attribute into a dynamic property. */
14441 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
14442 length = 1;
14443 else
14444 length_is_constant = false;
14445 }
14446 else if (attr != nullptr)
14447 {
14448 /* This DW_AT_string_length just contains the length with no
14449 indirection. There's no need to create a dynamic property in this
14450 case. Pass 0 for the default value as we know it will not be
14451 returned in this case. */
14452 length = attr->constant_value (0);
14453 }
14454 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
14455 {
14456 /* We don't currently support non-constant byte sizes for strings. */
14457 length = attr->constant_value (1);
14458 }
14459 else
14460 {
14461 /* Use 1 as a fallback length if we have nothing else. */
14462 length = 1;
14463 }
14464
14465 index_type = builtin_type (objfile)->builtin_int;
14466 type_allocator alloc (objfile, cu->lang ());
14467 if (length_is_constant)
14468 range_type = create_static_range_type (alloc, index_type, 1, length);
14469 else
14470 {
14471 struct dynamic_prop low_bound;
14472
14473 low_bound.set_const_val (1);
14474 range_type = create_range_type (alloc, index_type, &low_bound, &prop, 0);
14475 }
14476 char_type = language_string_char_type (cu->language_defn, gdbarch);
14477 type = create_string_type (alloc, char_type, range_type);
14478
14479 return set_die_type (die, type, cu);
14480 }
14481
14482 /* Assuming that DIE corresponds to a function, returns nonzero
14483 if the function is prototyped. */
14484
14485 static int
14486 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14487 {
14488 struct attribute *attr;
14489
14490 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14491 if (attr && attr->as_boolean ())
14492 return 1;
14493
14494 /* The DWARF standard implies that the DW_AT_prototyped attribute
14495 is only meaningful for C, but the concept also extends to other
14496 languages that allow unprototyped functions (Eg: Objective C).
14497 For all other languages, assume that functions are always
14498 prototyped. */
14499 if (cu->lang () != language_c
14500 && cu->lang () != language_objc
14501 && cu->lang () != language_opencl)
14502 return 1;
14503
14504 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14505 prototyped and unprototyped functions; default to prototyped,
14506 since that is more common in modern code (and RealView warns
14507 about unprototyped functions). */
14508 if (producer_is_realview (cu->producer))
14509 return 1;
14510
14511 return 0;
14512 }
14513
14514 /* Handle DIES due to C code like:
14515
14516 struct foo
14517 {
14518 int (*funcp)(int a, long l);
14519 int b;
14520 };
14521
14522 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14523
14524 static struct type *
14525 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14526 {
14527 struct objfile *objfile = cu->per_objfile->objfile;
14528 struct type *type; /* Type that this function returns. */
14529 struct type *ftype; /* Function that returns above type. */
14530 struct attribute *attr;
14531
14532 type = die_type (die, cu);
14533
14534 /* PR gas/29517 occurs in 2.39, and is fixed in 2.40, but it's only fixed
14535 for dwarf version >= 3 which supports DW_TAG_unspecified_type. */
14536 if (type->code () == TYPE_CODE_VOID
14537 && !type->is_stub ()
14538 && die->child == nullptr
14539 && (cu->per_cu->version () == 2
14540 || producer_is_gas_2_39 (cu)))
14541 {
14542 /* Work around PR gas/29517, pretend we have an DW_TAG_unspecified_type
14543 return type. */
14544 type = (type_allocator (cu->per_objfile->objfile, cu->lang ())
14545 .new_type (TYPE_CODE_VOID, 0, nullptr));
14546 type->set_is_stub (true);
14547 }
14548
14549 /* The die_type call above may have already set the type for this DIE. */
14550 ftype = get_die_type (die, cu);
14551 if (ftype)
14552 return ftype;
14553
14554 ftype = lookup_function_type (type);
14555
14556 if (prototyped_function_p (die, cu))
14557 ftype->set_is_prototyped (true);
14558
14559 /* Store the calling convention in the type if it's available in
14560 the subroutine die. Otherwise set the calling convention to
14561 the default value DW_CC_normal. */
14562 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14563 if (attr != nullptr
14564 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
14565 TYPE_CALLING_CONVENTION (ftype)
14566 = (enum dwarf_calling_convention) attr->constant_value (0);
14567 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14568 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14569 else
14570 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14571
14572 /* Record whether the function returns normally to its caller or not
14573 if the DWARF producer set that information. */
14574 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14575 if (attr && attr->as_boolean ())
14576 TYPE_NO_RETURN (ftype) = 1;
14577
14578 /* We need to add the subroutine type to the die immediately so
14579 we don't infinitely recurse when dealing with parameters
14580 declared as the same subroutine type. */
14581 set_die_type (die, ftype, cu);
14582
14583 if (die->child != NULL)
14584 {
14585 struct type *void_type = builtin_type (objfile)->builtin_void;
14586 struct die_info *child_die;
14587 int nparams, iparams;
14588
14589 /* Count the number of parameters.
14590 FIXME: GDB currently ignores vararg functions, but knows about
14591 vararg member functions. */
14592 nparams = 0;
14593 child_die = die->child;
14594 while (child_die && child_die->tag)
14595 {
14596 if (child_die->tag == DW_TAG_formal_parameter)
14597 nparams++;
14598 else if (child_die->tag == DW_TAG_unspecified_parameters)
14599 ftype->set_has_varargs (true);
14600
14601 child_die = child_die->sibling;
14602 }
14603
14604 /* Allocate storage for parameters and fill them in. */
14605 ftype->alloc_fields (nparams);
14606
14607 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14608 even if we error out during the parameters reading below. */
14609 for (iparams = 0; iparams < nparams; iparams++)
14610 ftype->field (iparams).set_type (void_type);
14611
14612 iparams = 0;
14613 child_die = die->child;
14614 while (child_die && child_die->tag)
14615 {
14616 if (child_die->tag == DW_TAG_formal_parameter)
14617 {
14618 struct type *arg_type;
14619
14620 /* DWARF version 2 has no clean way to discern C++
14621 static and non-static member functions. G++ helps
14622 GDB by marking the first parameter for non-static
14623 member functions (which is the this pointer) as
14624 artificial. We pass this information to
14625 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14626
14627 DWARF version 3 added DW_AT_object_pointer, which GCC
14628 4.5 does not yet generate. */
14629 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14630 if (attr != nullptr)
14631 ftype->field (iparams).set_is_artificial (attr->as_boolean ());
14632 else
14633 ftype->field (iparams).set_is_artificial (false);
14634 arg_type = die_type (child_die, cu);
14635
14636 /* RealView does not mark THIS as const, which the testsuite
14637 expects. GCC marks THIS as const in method definitions,
14638 but not in the class specifications (GCC PR 43053). */
14639 if (cu->lang () == language_cplus
14640 && !TYPE_CONST (arg_type)
14641 && ftype->field (iparams).is_artificial ())
14642 {
14643 int is_this = 0;
14644 struct dwarf2_cu *arg_cu = cu;
14645 const char *name = dwarf2_name (child_die, cu);
14646
14647 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14648 if (attr != nullptr)
14649 {
14650 /* If the compiler emits this, use it. */
14651 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14652 is_this = 1;
14653 }
14654 else if (name && strcmp (name, "this") == 0)
14655 /* Function definitions will have the argument names. */
14656 is_this = 1;
14657 else if (name == NULL && iparams == 0)
14658 /* Declarations may not have the names, so like
14659 elsewhere in GDB, assume an artificial first
14660 argument is "this". */
14661 is_this = 1;
14662
14663 if (is_this)
14664 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14665 arg_type, 0);
14666 }
14667
14668 ftype->field (iparams).set_type (arg_type);
14669 iparams++;
14670 }
14671 child_die = child_die->sibling;
14672 }
14673 }
14674
14675 return ftype;
14676 }
14677
14678 static struct type *
14679 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14680 {
14681 struct objfile *objfile = cu->per_objfile->objfile;
14682 const char *name = dwarf2_full_name (NULL, die, cu);
14683 struct type *this_type;
14684 struct gdbarch *gdbarch = objfile->arch ();
14685 struct type *target_type = die_type (die, cu);
14686
14687 if (gdbarch_dwarf2_omit_typedef_p (gdbarch, target_type, cu->producer, name))
14688 {
14689 /* The long double is defined as a base type in C. GCC creates a long
14690 double typedef with target-type _Float128 for the long double to
14691 identify it as the IEEE Float128 value. This is a GCC hack since the
14692 DWARF doesn't distinguish between the IBM long double and IEEE
14693 128-bit float. Replace the GCC workaround for the long double
14694 typedef with the actual type information copied from the target-type
14695 with the correct long double base type name. */
14696 this_type = copy_type (target_type);
14697 this_type->set_name (name);
14698 set_die_type (die, this_type, cu);
14699 return this_type;
14700 }
14701
14702 type_allocator alloc (objfile, cu->lang ());
14703 this_type = alloc.new_type (TYPE_CODE_TYPEDEF, 0, name);
14704 this_type->set_target_is_stub (true);
14705 set_die_type (die, this_type, cu);
14706 if (target_type != this_type)
14707 this_type->set_target_type (target_type);
14708 else
14709 {
14710 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14711 spec and cause infinite loops in GDB. */
14712 complaint (_("Self-referential DW_TAG_typedef "
14713 "- DIE at %s [in module %s]"),
14714 sect_offset_str (die->sect_off), objfile_name (objfile));
14715 this_type->set_target_type (nullptr);
14716 }
14717 if (name == NULL)
14718 {
14719 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
14720 anonymous typedefs, which is, strictly speaking, invalid DWARF.
14721 Handle these by just returning the target type, rather than
14722 constructing an anonymous typedef type and trying to handle this
14723 elsewhere. */
14724 set_die_type (die, target_type, cu);
14725 return target_type;
14726 }
14727 return this_type;
14728 }
14729
14730 /* Helper for get_dwarf2_rational_constant that computes the value of
14731 a given gmp_mpz given an attribute. */
14732
14733 static void
14734 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
14735 {
14736 /* GCC will sometimes emit a 16-byte constant value as a DWARF
14737 location expression that pushes an implicit value. */
14738 if (attr->form == DW_FORM_exprloc)
14739 {
14740 dwarf_block *blk = attr->as_block ();
14741 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
14742 {
14743 uint64_t len;
14744 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
14745 blk->data + blk->size,
14746 &len);
14747 if (ptr - blk->data + len <= blk->size)
14748 {
14749 value->read (gdb::make_array_view (ptr, len),
14750 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14751 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14752 true);
14753 return;
14754 }
14755 }
14756
14757 /* On failure set it to 1. */
14758 *value = gdb_mpz (1);
14759 }
14760 else if (attr->form_is_block ())
14761 {
14762 dwarf_block *blk = attr->as_block ();
14763 value->read (gdb::make_array_view (blk->data, blk->size),
14764 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14765 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14766 true);
14767 }
14768 else if (attr->form_is_unsigned ())
14769 *value = gdb_mpz (attr->as_unsigned ());
14770 else
14771 *value = gdb_mpz (attr->constant_value (1));
14772 }
14773
14774 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
14775 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
14776
14777 If the numerator and/or numerator attribute is missing,
14778 a complaint is filed, and NUMERATOR and DENOMINATOR are left
14779 untouched. */
14780
14781 static void
14782 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
14783 gdb_mpz *numerator, gdb_mpz *denominator)
14784 {
14785 struct attribute *num_attr, *denom_attr;
14786
14787 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
14788 if (num_attr == nullptr)
14789 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
14790 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14791
14792 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
14793 if (denom_attr == nullptr)
14794 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
14795 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14796
14797 if (num_attr == nullptr || denom_attr == nullptr)
14798 return;
14799
14800 get_mpz (cu, numerator, num_attr);
14801 get_mpz (cu, denominator, denom_attr);
14802 }
14803
14804 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
14805 rational constant, rather than a signed one.
14806
14807 If the rational constant has a negative value, a complaint
14808 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
14809
14810 static void
14811 get_dwarf2_unsigned_rational_constant (struct die_info *die,
14812 struct dwarf2_cu *cu,
14813 gdb_mpz *numerator,
14814 gdb_mpz *denominator)
14815 {
14816 gdb_mpz num (1);
14817 gdb_mpz denom (1);
14818
14819 get_dwarf2_rational_constant (die, cu, &num, &denom);
14820 if (num < 0 && denom < 0)
14821 {
14822 num.negate ();
14823 denom.negate ();
14824 }
14825 else if (num < 0)
14826 {
14827 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
14828 " in DIE at %s"),
14829 sect_offset_str (die->sect_off));
14830 return;
14831 }
14832 else if (denom < 0)
14833 {
14834 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
14835 " in DIE at %s"),
14836 sect_offset_str (die->sect_off));
14837 return;
14838 }
14839
14840 *numerator = std::move (num);
14841 *denominator = std::move (denom);
14842 }
14843
14844 /* Assuming that ENCODING is a string whose contents starting at the
14845 K'th character is "_nn" where "nn" is a decimal number, scan that
14846 number and set RESULT to the value. K is updated to point to the
14847 character immediately following the number.
14848
14849 If the string does not conform to the format described above, false
14850 is returned, and K may or may not be changed. */
14851
14852 static bool
14853 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
14854 {
14855 /* The next character should be an underscore ('_') followed
14856 by a digit. */
14857 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
14858 return false;
14859
14860 /* Skip the underscore. */
14861 k++;
14862 int start = k;
14863
14864 /* Determine the number of digits for our number. */
14865 while (isdigit (encoding[k]))
14866 k++;
14867 if (k == start)
14868 return false;
14869
14870 std::string copy (&encoding[start], k - start);
14871 return result->set (copy.c_str (), 10);
14872 }
14873
14874 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
14875 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
14876 DENOM, update OFFSET, and return true on success. Return false on
14877 failure. */
14878
14879 static bool
14880 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
14881 gdb_mpz *num, gdb_mpz *denom)
14882 {
14883 if (!ada_get_gnat_encoded_number (encoding, offset, num))
14884 return false;
14885 return ada_get_gnat_encoded_number (encoding, offset, denom);
14886 }
14887
14888 /* Assuming DIE corresponds to a fixed point type, finish the creation
14889 of the corresponding TYPE by setting its type-specific data. CU is
14890 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
14891 encodings. It is nullptr if the GNAT encoding should be
14892 ignored. */
14893
14894 static void
14895 finish_fixed_point_type (struct type *type, const char *suffix,
14896 struct die_info *die, struct dwarf2_cu *cu)
14897 {
14898 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
14899 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
14900
14901 /* If GNAT encodings are preferred, don't examine the
14902 attributes. */
14903 struct attribute *attr = nullptr;
14904 if (suffix == nullptr)
14905 {
14906 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
14907 if (attr == nullptr)
14908 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
14909 if (attr == nullptr)
14910 attr = dwarf2_attr (die, DW_AT_small, cu);
14911 }
14912
14913 /* Numerator and denominator of our fixed-point type's scaling factor.
14914 The default is a scaling factor of 1, which we use as a fallback
14915 when we are not able to decode it (problem with the debugging info,
14916 unsupported forms, bug in GDB, etc...). Using that as the default
14917 allows us to at least print the unscaled value, which might still
14918 be useful to a user. */
14919 gdb_mpz scale_num (1);
14920 gdb_mpz scale_denom (1);
14921
14922 if (attr == nullptr)
14923 {
14924 int offset = 0;
14925 if (suffix != nullptr
14926 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14927 &scale_denom)
14928 /* The number might be encoded as _nn_dd_nn_dd, where the
14929 second ratio is the 'small value. In this situation, we
14930 want the second value. */
14931 && (suffix[offset] != '_'
14932 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14933 &scale_denom)))
14934 {
14935 /* Found it. */
14936 }
14937 else
14938 {
14939 /* Scaling factor not found. Assume a scaling factor of 1,
14940 and hope for the best. At least the user will be able to
14941 see the encoded value. */
14942 scale_num = 1;
14943 scale_denom = 1;
14944 complaint (_("no scale found for fixed-point type (DIE at %s)"),
14945 sect_offset_str (die->sect_off));
14946 }
14947 }
14948 else if (attr->name == DW_AT_binary_scale)
14949 {
14950 LONGEST scale_exp = attr->constant_value (0);
14951 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
14952
14953 num_or_denom <<= std::abs (scale_exp);
14954 }
14955 else if (attr->name == DW_AT_decimal_scale)
14956 {
14957 LONGEST scale_exp = attr->constant_value (0);
14958 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
14959
14960 num_or_denom = gdb_mpz::pow (10, std::abs (scale_exp));
14961 }
14962 else if (attr->name == DW_AT_small)
14963 {
14964 struct die_info *scale_die;
14965 struct dwarf2_cu *scale_cu = cu;
14966
14967 scale_die = follow_die_ref (die, attr, &scale_cu);
14968 if (scale_die->tag == DW_TAG_constant)
14969 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
14970 &scale_num, &scale_denom);
14971 else
14972 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
14973 " (DIE at %s)"),
14974 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14975 }
14976 else
14977 {
14978 complaint (_("unsupported scale attribute %s for fixed-point type"
14979 " (DIE at %s)"),
14980 dwarf_attr_name (attr->name),
14981 sect_offset_str (die->sect_off));
14982 }
14983
14984 type->fixed_point_info ().scaling_factor = gdb_mpq (scale_num, scale_denom);
14985 }
14986
14987 /* The gnat-encoding suffix for fixed point. */
14988
14989 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
14990
14991 /* If NAME encodes an Ada fixed-point type, return a pointer to the
14992 "XF" suffix of the name. The text after this is what encodes the
14993 'small and 'delta information. Otherwise, return nullptr. */
14994
14995 static const char *
14996 gnat_encoded_fixed_point_type_info (const char *name)
14997 {
14998 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
14999 }
15000
15001 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15002 (which may be different from NAME) to the architecture back-end to allow
15003 it to guess the correct format if necessary. */
15004
15005 static struct type *
15006 dwarf2_init_float_type (struct dwarf2_cu *cu, int bits, const char *name,
15007 const char *name_hint, enum bfd_endian byte_order)
15008 {
15009 struct objfile *objfile = cu->per_objfile->objfile;
15010 struct gdbarch *gdbarch = objfile->arch ();
15011 const struct floatformat **format;
15012 struct type *type;
15013
15014 type_allocator alloc (objfile, cu->lang ());
15015 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15016 if (format)
15017 type = init_float_type (alloc, bits, name, format, byte_order);
15018 else
15019 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15020
15021 return type;
15022 }
15023
15024 /* Allocate an integer type of size BITS and name NAME. */
15025
15026 static struct type *
15027 dwarf2_init_integer_type (struct dwarf2_cu *cu, int bits, int unsigned_p,
15028 const char *name)
15029 {
15030 struct type *type;
15031 struct objfile *objfile = cu->per_objfile->objfile;
15032
15033 /* Versions of Intel's C Compiler generate an integer type called "void"
15034 instead of using DW_TAG_unspecified_type. This has been seen on
15035 at least versions 14, 17, and 18. */
15036 if (bits == 0 && producer_is_icc (cu) && name != nullptr
15037 && strcmp (name, "void") == 0)
15038 type = builtin_type (objfile)->builtin_void;
15039 else
15040 {
15041 type_allocator alloc (objfile, cu->lang ());
15042 type = init_integer_type (alloc, bits, unsigned_p, name);
15043 }
15044
15045 return type;
15046 }
15047
15048 /* Return true if DIE has a DW_AT_small attribute whose value is
15049 a constant rational, where both the numerator and denominator
15050 are equal to zero.
15051
15052 CU is the DIE's Compilation Unit. */
15053
15054 static bool
15055 has_zero_over_zero_small_attribute (struct die_info *die,
15056 struct dwarf2_cu *cu)
15057 {
15058 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
15059 if (attr == nullptr)
15060 return false;
15061
15062 struct dwarf2_cu *scale_cu = cu;
15063 struct die_info *scale_die
15064 = follow_die_ref (die, attr, &scale_cu);
15065
15066 if (scale_die->tag != DW_TAG_constant)
15067 return false;
15068
15069 gdb_mpz num (1), denom (1);
15070 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
15071 return num == 0 && denom == 0;
15072 }
15073
15074 /* Initialise and return a floating point type of size BITS suitable for
15075 use as a component of a complex number. The NAME_HINT is passed through
15076 when initialising the floating point type and is the name of the complex
15077 type.
15078
15079 As DWARF doesn't currently provide an explicit name for the components
15080 of a complex number, but it can be helpful to have these components
15081 named, we try to select a suitable name based on the size of the
15082 component. */
15083 static struct type *
15084 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
15085 int bits, const char *name_hint,
15086 enum bfd_endian byte_order)
15087 {
15088 struct objfile *objfile = cu->per_objfile->objfile;
15089 gdbarch *gdbarch = objfile->arch ();
15090 struct type *tt = nullptr;
15091
15092 /* Try to find a suitable floating point builtin type of size BITS.
15093 We're going to use the name of this type as the name for the complex
15094 target type that we are about to create. */
15095 switch (cu->lang ())
15096 {
15097 case language_fortran:
15098 switch (bits)
15099 {
15100 case 32:
15101 tt = builtin_f_type (gdbarch)->builtin_real;
15102 break;
15103 case 64:
15104 tt = builtin_f_type (gdbarch)->builtin_real_s8;
15105 break;
15106 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15107 case 128:
15108 tt = builtin_f_type (gdbarch)->builtin_real_s16;
15109 break;
15110 }
15111 break;
15112 default:
15113 switch (bits)
15114 {
15115 case 32:
15116 tt = builtin_type (gdbarch)->builtin_float;
15117 break;
15118 case 64:
15119 tt = builtin_type (gdbarch)->builtin_double;
15120 break;
15121 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15122 case 128:
15123 tt = builtin_type (gdbarch)->builtin_long_double;
15124 break;
15125 }
15126 break;
15127 }
15128
15129 /* If the type we found doesn't match the size we were looking for, then
15130 pretend we didn't find a type at all, the complex target type we
15131 create will then be nameless. */
15132 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
15133 tt = nullptr;
15134
15135 const char *name = (tt == nullptr) ? nullptr : tt->name ();
15136 return dwarf2_init_float_type (cu, bits, name, name_hint, byte_order);
15137 }
15138
15139 /* Find a representation of a given base type and install
15140 it in the TYPE field of the die. */
15141
15142 static struct type *
15143 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15144 {
15145 struct objfile *objfile = cu->per_objfile->objfile;
15146 struct type *type;
15147 struct attribute *attr;
15148 int encoding = 0, bits = 0;
15149 const char *name;
15150
15151 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15152 if (attr != nullptr && attr->form_is_constant ())
15153 encoding = attr->constant_value (0);
15154 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15155 if (attr != nullptr)
15156 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
15157 name = dwarf2_name (die, cu);
15158 if (!name)
15159 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
15160
15161 enum bfd_endian byte_order;
15162 bool not_default = die_byte_order (die, cu, &byte_order);
15163
15164 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
15165 && cu->lang () == language_ada
15166 && has_zero_over_zero_small_attribute (die, cu))
15167 {
15168 /* brobecker/2018-02-24: This is a fixed point type for which
15169 the scaling factor is represented as fraction whose value
15170 does not make sense (zero divided by zero), so we should
15171 normally never see these. However, there is a small category
15172 of fixed point types for which GNAT is unable to provide
15173 the scaling factor via the standard DWARF mechanisms, and
15174 for which the info is provided via the GNAT encodings instead.
15175 This is likely what this DIE is about. */
15176 encoding = (encoding == DW_ATE_signed_fixed
15177 ? DW_ATE_signed
15178 : DW_ATE_unsigned);
15179 }
15180
15181 /* With GNAT encodings, fixed-point information will be encoded in
15182 the type name. Note that this can also occur with the above
15183 zero-over-zero case, which is why this is a separate "if" rather
15184 than an "else if". */
15185 const char *gnat_encoding_suffix = nullptr;
15186 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
15187 && cu->lang () == language_ada
15188 && name != nullptr)
15189 {
15190 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
15191 if (gnat_encoding_suffix != nullptr)
15192 {
15193 gdb_assert (startswith (gnat_encoding_suffix,
15194 GNAT_FIXED_POINT_SUFFIX));
15195 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
15196 name, gnat_encoding_suffix - name);
15197 /* Use -1 here so that SUFFIX points at the "_" after the
15198 "XF". */
15199 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
15200
15201 encoding = (encoding == DW_ATE_signed
15202 ? DW_ATE_signed_fixed
15203 : DW_ATE_unsigned_fixed);
15204 }
15205 }
15206
15207 type_allocator alloc (objfile, cu->lang ());
15208 switch (encoding)
15209 {
15210 case DW_ATE_address:
15211 /* Turn DW_ATE_address into a void * pointer. */
15212 type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
15213 type = init_pointer_type (alloc, bits, name, type);
15214 break;
15215 case DW_ATE_boolean:
15216 type = init_boolean_type (alloc, bits, 1, name);
15217 break;
15218 case DW_ATE_complex_float:
15219 type = dwarf2_init_complex_target_type (cu, bits / 2, name,
15220 byte_order);
15221 if (type->code () == TYPE_CODE_ERROR)
15222 {
15223 if (name == nullptr)
15224 {
15225 struct obstack *obstack
15226 = &cu->per_objfile->objfile->objfile_obstack;
15227 name = obconcat (obstack, "_Complex ", type->name (),
15228 nullptr);
15229 }
15230 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15231 }
15232 else
15233 type = init_complex_type (name, type);
15234 break;
15235 case DW_ATE_decimal_float:
15236 type = init_decfloat_type (alloc, bits, name);
15237 break;
15238 case DW_ATE_float:
15239 type = dwarf2_init_float_type (cu, bits, name, name, byte_order);
15240 break;
15241 case DW_ATE_signed:
15242 type = dwarf2_init_integer_type (cu, bits, 0, name);
15243 break;
15244 case DW_ATE_unsigned:
15245 if (cu->lang () == language_fortran
15246 && name
15247 && startswith (name, "character("))
15248 type = init_character_type (alloc, bits, 1, name);
15249 else
15250 type = dwarf2_init_integer_type (cu, bits, 1, name);
15251 break;
15252 case DW_ATE_signed_char:
15253 if (cu->lang () == language_ada
15254 || cu->lang () == language_m2
15255 || cu->lang () == language_pascal
15256 || cu->lang () == language_fortran)
15257 type = init_character_type (alloc, bits, 0, name);
15258 else
15259 type = dwarf2_init_integer_type (cu, bits, 0, name);
15260 break;
15261 case DW_ATE_unsigned_char:
15262 if (cu->lang () == language_ada
15263 || cu->lang () == language_m2
15264 || cu->lang () == language_pascal
15265 || cu->lang () == language_fortran
15266 || cu->lang () == language_rust)
15267 type = init_character_type (alloc, bits, 1, name);
15268 else
15269 type = dwarf2_init_integer_type (cu, bits, 1, name);
15270 break;
15271 case DW_ATE_UTF:
15272 {
15273 type = init_character_type (alloc, bits, 1, name);
15274 return set_die_type (die, type, cu);
15275 }
15276 break;
15277 case DW_ATE_signed_fixed:
15278 type = init_fixed_point_type (alloc, bits, 0, name);
15279 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15280 break;
15281 case DW_ATE_unsigned_fixed:
15282 type = init_fixed_point_type (alloc, bits, 1, name);
15283 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15284 break;
15285
15286 default:
15287 complaint (_("unsupported DW_AT_encoding: '%s'"),
15288 dwarf_type_encoding_name (encoding));
15289 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15290 break;
15291 }
15292
15293 if (type->code () == TYPE_CODE_INT
15294 && name != nullptr
15295 && strcmp (name, "char") == 0)
15296 type->set_has_no_signedness (true);
15297
15298 maybe_set_alignment (cu, die, type);
15299
15300 type->set_endianity_is_not_default (not_default);
15301
15302 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
15303 {
15304 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15305 if (attr != nullptr && attr->form_is_constant ())
15306 {
15307 unsigned real_bit_size = attr->constant_value (0);
15308 if (real_bit_size >= 0 && real_bit_size <= 8 * type->length ())
15309 {
15310 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15311 /* Only use the attributes if they make sense together. */
15312 if (attr == nullptr
15313 || (attr->form_is_constant ()
15314 && attr->constant_value (0) >= 0
15315 && (attr->constant_value (0) + real_bit_size
15316 <= 8 * type->length ())))
15317 {
15318 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
15319 = real_bit_size;
15320 if (attr != nullptr)
15321 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
15322 = attr->constant_value (0);
15323 }
15324 }
15325 }
15326 }
15327
15328 return set_die_type (die, type, cu);
15329 }
15330
15331 /* A helper function that returns the name of DIE, if it refers to a
15332 variable declaration. */
15333
15334 static const char *
15335 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
15336 {
15337 if (die->tag != DW_TAG_variable)
15338 return nullptr;
15339
15340 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
15341 if (attr == nullptr || !attr->as_boolean ())
15342 return nullptr;
15343
15344 attr = dwarf2_attr (die, DW_AT_name, cu);
15345 if (attr == nullptr)
15346 return nullptr;
15347 return attr->as_string ();
15348 }
15349
15350 /* Parse dwarf attribute if it's a block, reference or constant and put the
15351 resulting value of the attribute into struct bound_prop.
15352 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15353
15354 static int
15355 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15356 struct dwarf2_cu *cu, struct dynamic_prop *prop,
15357 struct type *default_type)
15358 {
15359 struct dwarf2_property_baton *baton;
15360 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15361 struct objfile *objfile = per_objfile->objfile;
15362 struct obstack *obstack = &objfile->objfile_obstack;
15363
15364 gdb_assert (default_type != NULL);
15365
15366 if (attr == NULL || prop == NULL)
15367 return 0;
15368
15369 if (attr->form_is_block ())
15370 {
15371 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15372 baton->property_type = default_type;
15373 baton->locexpr.per_cu = cu->per_cu;
15374 baton->locexpr.per_objfile = per_objfile;
15375
15376 struct dwarf_block block;
15377 if (attr->form == DW_FORM_data16)
15378 {
15379 size_t data_size = 16;
15380 block.size = (data_size
15381 + 2 /* Extra bytes for DW_OP and arg. */);
15382 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block.size);
15383 data[0] = DW_OP_implicit_value;
15384 data[1] = data_size;
15385 memcpy (&data[2], attr->as_block ()->data, data_size);
15386 block.data = data;
15387 }
15388 else
15389 block = *attr->as_block ();
15390
15391 baton->locexpr.size = block.size;
15392 baton->locexpr.data = block.data;
15393 switch (attr->name)
15394 {
15395 case DW_AT_string_length:
15396 baton->locexpr.is_reference = true;
15397 break;
15398 default:
15399 baton->locexpr.is_reference = false;
15400 break;
15401 }
15402
15403 prop->set_locexpr (baton);
15404 gdb_assert (prop->baton () != NULL);
15405 }
15406 else if (attr->form_is_ref ())
15407 {
15408 struct dwarf2_cu *target_cu = cu;
15409 struct die_info *target_die;
15410 struct attribute *target_attr;
15411
15412 target_die = follow_die_ref (die, attr, &target_cu);
15413 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15414 if (target_attr == NULL)
15415 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15416 target_cu);
15417 if (target_attr == nullptr)
15418 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
15419 target_cu);
15420 if (target_attr == NULL)
15421 {
15422 const char *name = var_decl_name (target_die, target_cu);
15423 if (name != nullptr)
15424 {
15425 prop->set_variable_name (name);
15426 return 1;
15427 }
15428 return 0;
15429 }
15430
15431 switch (target_attr->name)
15432 {
15433 case DW_AT_location:
15434 if (target_attr->form_is_section_offset ())
15435 {
15436 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15437 baton->property_type = die_type (target_die, target_cu);
15438 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15439 prop->set_loclist (baton);
15440 gdb_assert (prop->baton () != NULL);
15441 }
15442 else if (target_attr->form_is_block ())
15443 {
15444 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15445 baton->property_type = die_type (target_die, target_cu);
15446 baton->locexpr.per_cu = cu->per_cu;
15447 baton->locexpr.per_objfile = per_objfile;
15448 struct dwarf_block *block = target_attr->as_block ();
15449 baton->locexpr.size = block->size;
15450 baton->locexpr.data = block->data;
15451 baton->locexpr.is_reference = true;
15452 prop->set_locexpr (baton);
15453 gdb_assert (prop->baton () != NULL);
15454 }
15455 else
15456 {
15457 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15458 "dynamic property");
15459 return 0;
15460 }
15461 break;
15462 case DW_AT_data_member_location:
15463 case DW_AT_data_bit_offset:
15464 {
15465 LONGEST offset;
15466
15467 if (!handle_member_location (target_die, target_cu, &offset))
15468 return 0;
15469
15470 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15471 baton->property_type = read_type_die (target_die->parent,
15472 target_cu);
15473 baton->offset_info.offset = offset;
15474 baton->offset_info.type = die_type (target_die, target_cu);
15475 prop->set_addr_offset (baton);
15476 break;
15477 }
15478 }
15479 }
15480 else if (attr->form_is_constant ())
15481 prop->set_const_val (attr->constant_value (0));
15482 else if (attr->form_is_section_offset ())
15483 {
15484 switch (attr->name)
15485 {
15486 case DW_AT_string_length:
15487 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15488 baton->property_type = default_type;
15489 fill_in_loclist_baton (cu, &baton->loclist, attr);
15490 prop->set_loclist (baton);
15491 gdb_assert (prop->baton () != NULL);
15492 break;
15493 default:
15494 goto invalid;
15495 }
15496 }
15497 else
15498 goto invalid;
15499
15500 return 1;
15501
15502 invalid:
15503 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15504 dwarf2_name (die, cu));
15505 return 0;
15506 }
15507
15508 /* See read.h. */
15509
15510 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
15511 present (which is valid) then compute the default type based on the
15512 compilation units address size. */
15513
15514 static struct type *
15515 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
15516 {
15517 struct type *index_type = die_type (die, cu);
15518
15519 /* Dwarf-2 specifications explicitly allows to create subrange types
15520 without specifying a base type.
15521 In that case, the base type must be set to the type of
15522 the lower bound, upper bound or count, in that order, if any of these
15523 three attributes references an object that has a type.
15524 If no base type is found, the Dwarf-2 specifications say that
15525 a signed integer type of size equal to the size of an address should
15526 be used.
15527 For the following C code: `extern char gdb_int [];'
15528 GCC produces an empty range DIE.
15529 FIXME: muller/2010-05-28: Possible references to object for low bound,
15530 high bound or count are not yet handled by this code. */
15531 if (index_type->code () == TYPE_CODE_VOID)
15532 index_type = cu->addr_sized_int_type (false);
15533
15534 return index_type;
15535 }
15536
15537 /* Read the given DW_AT_subrange DIE. */
15538
15539 static struct type *
15540 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15541 {
15542 struct type *base_type, *orig_base_type;
15543 struct type *range_type;
15544 struct attribute *attr;
15545 struct dynamic_prop low, high;
15546 int low_default_is_valid;
15547 int high_bound_is_count = 0;
15548 const char *name;
15549
15550 orig_base_type = read_subrange_index_type (die, cu);
15551
15552 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15553 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15554 creating the range type, but we use the result of check_typedef
15555 when examining properties of the type. */
15556 base_type = check_typedef (orig_base_type);
15557
15558 /* The die_type call above may have already set the type for this DIE. */
15559 range_type = get_die_type (die, cu);
15560 if (range_type)
15561 return range_type;
15562
15563 high.set_const_val (0);
15564
15565 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15566 omitting DW_AT_lower_bound. */
15567 switch (cu->lang ())
15568 {
15569 case language_c:
15570 case language_cplus:
15571 low.set_const_val (0);
15572 low_default_is_valid = 1;
15573 break;
15574 case language_fortran:
15575 low.set_const_val (1);
15576 low_default_is_valid = 1;
15577 break;
15578 case language_d:
15579 case language_objc:
15580 case language_rust:
15581 low.set_const_val (0);
15582 low_default_is_valid = (cu->header.version >= 4);
15583 break;
15584 case language_ada:
15585 case language_m2:
15586 case language_pascal:
15587 low.set_const_val (1);
15588 low_default_is_valid = (cu->header.version >= 4);
15589 break;
15590 default:
15591 low.set_const_val (0);
15592 low_default_is_valid = 0;
15593 break;
15594 }
15595
15596 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15597 if (attr != nullptr)
15598 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
15599 else if (!low_default_is_valid)
15600 complaint (_("Missing DW_AT_lower_bound "
15601 "- DIE at %s [in module %s]"),
15602 sect_offset_str (die->sect_off),
15603 objfile_name (cu->per_objfile->objfile));
15604
15605 struct attribute *attr_ub, *attr_count;
15606 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
15607 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15608 {
15609 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
15610 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15611 {
15612 /* If bounds are constant do the final calculation here. */
15613 if (low.is_constant () && high.is_constant ())
15614 high.set_const_val (low.const_val () + high.const_val () - 1);
15615 else
15616 high_bound_is_count = 1;
15617 }
15618 else
15619 {
15620 if (attr_ub != NULL)
15621 complaint (_("Unresolved DW_AT_upper_bound "
15622 "- DIE at %s [in module %s]"),
15623 sect_offset_str (die->sect_off),
15624 objfile_name (cu->per_objfile->objfile));
15625 if (attr_count != NULL)
15626 complaint (_("Unresolved DW_AT_count "
15627 "- DIE at %s [in module %s]"),
15628 sect_offset_str (die->sect_off),
15629 objfile_name (cu->per_objfile->objfile));
15630 }
15631 }
15632
15633 LONGEST bias = 0;
15634 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
15635 if (bias_attr != nullptr && bias_attr->form_is_constant ())
15636 bias = bias_attr->constant_value (0);
15637
15638 /* Normally, the DWARF producers are expected to use a signed
15639 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15640 But this is unfortunately not always the case, as witnessed
15641 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15642 is used instead. To work around that ambiguity, we treat
15643 the bounds as signed, and thus sign-extend their values, when
15644 the base type is signed.
15645
15646 Skip it if the base type's length is larger than ULONGEST, to avoid
15647 the undefined behavior of a too large left shift. We don't really handle
15648 constants larger than 8 bytes anyway, at the moment. */
15649
15650 if (base_type->length () <= sizeof (ULONGEST))
15651 {
15652 ULONGEST negative_mask
15653 = -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
15654
15655 if (low.is_constant ()
15656 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
15657 low.set_const_val (low.const_val () | negative_mask);
15658
15659 if (high.is_constant ()
15660 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
15661 high.set_const_val (high.const_val () | negative_mask);
15662 }
15663
15664 /* Check for bit and byte strides. */
15665 struct dynamic_prop byte_stride_prop;
15666 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
15667 if (attr_byte_stride != nullptr)
15668 {
15669 struct type *prop_type = cu->addr_sized_int_type (false);
15670 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
15671 prop_type);
15672 }
15673
15674 struct dynamic_prop bit_stride_prop;
15675 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
15676 if (attr_bit_stride != nullptr)
15677 {
15678 /* It only makes sense to have either a bit or byte stride. */
15679 if (attr_byte_stride != nullptr)
15680 {
15681 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
15682 "- DIE at %s [in module %s]"),
15683 sect_offset_str (die->sect_off),
15684 objfile_name (cu->per_objfile->objfile));
15685 attr_bit_stride = nullptr;
15686 }
15687 else
15688 {
15689 struct type *prop_type = cu->addr_sized_int_type (false);
15690 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
15691 prop_type);
15692 }
15693 }
15694
15695 type_allocator alloc (cu->per_objfile->objfile, cu->lang ());
15696 if (attr_byte_stride != nullptr
15697 || attr_bit_stride != nullptr)
15698 {
15699 bool byte_stride_p = (attr_byte_stride != nullptr);
15700 struct dynamic_prop *stride
15701 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
15702
15703 range_type
15704 = create_range_type_with_stride (alloc, orig_base_type, &low,
15705 &high, bias, stride, byte_stride_p);
15706 }
15707 else
15708 range_type = create_range_type (alloc, orig_base_type, &low, &high, bias);
15709
15710 if (high_bound_is_count)
15711 range_type->bounds ()->flag_upper_bound_is_count = 1;
15712
15713 /* Ada expects an empty array on no boundary attributes. */
15714 if (attr == NULL && cu->lang () != language_ada)
15715 range_type->bounds ()->high.set_undefined ();
15716
15717 name = dwarf2_name (die, cu);
15718 if (name)
15719 range_type->set_name (name);
15720
15721 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15722 if (attr != nullptr)
15723 range_type->set_length (attr->constant_value (0));
15724
15725 maybe_set_alignment (cu, die, range_type);
15726
15727 set_die_type (die, range_type, cu);
15728
15729 /* set_die_type should be already done. */
15730 set_descriptive_type (range_type, die, cu);
15731
15732 return range_type;
15733 }
15734
15735 static struct type *
15736 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15737 {
15738 struct type *type;
15739
15740 type = (type_allocator (cu->per_objfile->objfile, cu->lang ())
15741 .new_type (TYPE_CODE_VOID, 0, nullptr));
15742 type->set_name (dwarf2_name (die, cu));
15743
15744 /* In Ada, an unspecified type is typically used when the description
15745 of the type is deferred to a different unit. When encountering
15746 such a type, we treat it as a stub, and try to resolve it later on,
15747 when needed.
15748 Mark this as a stub type for all languages though. */
15749 type->set_is_stub (true);
15750
15751 return set_die_type (die, type, cu);
15752 }
15753
15754 /* Read a single die and all its descendents. Set the die's sibling
15755 field to NULL; set other fields in the die correctly, and set all
15756 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15757 location of the info_ptr after reading all of those dies. PARENT
15758 is the parent of the die in question. */
15759
15760 static struct die_info *
15761 read_die_and_children (const struct die_reader_specs *reader,
15762 const gdb_byte *info_ptr,
15763 const gdb_byte **new_info_ptr,
15764 struct die_info *parent)
15765 {
15766 struct die_info *die;
15767 const gdb_byte *cur_ptr;
15768
15769 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0, true);
15770 if (die == NULL)
15771 {
15772 *new_info_ptr = cur_ptr;
15773 return NULL;
15774 }
15775 store_in_ref_table (die, reader->cu);
15776
15777 if (die->has_children)
15778 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15779 else
15780 {
15781 die->child = NULL;
15782 *new_info_ptr = cur_ptr;
15783 }
15784
15785 die->sibling = NULL;
15786 die->parent = parent;
15787 return die;
15788 }
15789
15790 /* Read a die, all of its descendents, and all of its siblings; set
15791 all of the fields of all of the dies correctly. Arguments are as
15792 in read_die_and_children. */
15793
15794 static struct die_info *
15795 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15796 const gdb_byte *info_ptr,
15797 const gdb_byte **new_info_ptr,
15798 struct die_info *parent)
15799 {
15800 struct die_info *first_die, *last_sibling;
15801 const gdb_byte *cur_ptr;
15802
15803 cur_ptr = info_ptr;
15804 first_die = last_sibling = NULL;
15805
15806 while (1)
15807 {
15808 struct die_info *die
15809 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15810
15811 if (die == NULL)
15812 {
15813 *new_info_ptr = cur_ptr;
15814 return first_die;
15815 }
15816
15817 if (!first_die)
15818 first_die = die;
15819 else
15820 last_sibling->sibling = die;
15821
15822 last_sibling = die;
15823 }
15824 }
15825
15826 /* Read a die, all of its descendents, and all of its siblings; set
15827 all of the fields of all of the dies correctly. Arguments are as
15828 in read_die_and_children.
15829 This the main entry point for reading a DIE and all its children. */
15830
15831 static struct die_info *
15832 read_die_and_siblings (const struct die_reader_specs *reader,
15833 const gdb_byte *info_ptr,
15834 const gdb_byte **new_info_ptr,
15835 struct die_info *parent)
15836 {
15837 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15838 new_info_ptr, parent);
15839
15840 if (dwarf_die_debug)
15841 {
15842 gdb_printf (gdb_stdlog,
15843 "Read die from %s@0x%x of %s:\n",
15844 reader->die_section->get_name (),
15845 (unsigned) (info_ptr - reader->die_section->buffer),
15846 bfd_get_filename (reader->abfd));
15847 die->dump (dwarf_die_debug);
15848 }
15849
15850 return die;
15851 }
15852
15853 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15854 attributes.
15855 The caller is responsible for filling in the extra attributes
15856 and updating (*DIEP)->num_attrs.
15857 Set DIEP to point to a newly allocated die with its information,
15858 except for its child, sibling, and parent fields. */
15859
15860 static const gdb_byte *
15861 read_full_die_1 (const struct die_reader_specs *reader,
15862 struct die_info **diep, const gdb_byte *info_ptr,
15863 int num_extra_attrs, bool allow_reprocess)
15864 {
15865 unsigned int abbrev_number, bytes_read, i;
15866 const struct abbrev_info *abbrev;
15867 struct die_info *die;
15868 struct dwarf2_cu *cu = reader->cu;
15869 bfd *abfd = reader->abfd;
15870
15871 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15872 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15873 info_ptr += bytes_read;
15874 if (!abbrev_number)
15875 {
15876 *diep = NULL;
15877 return info_ptr;
15878 }
15879
15880 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
15881 if (!abbrev)
15882 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15883 abbrev_number,
15884 bfd_get_filename (abfd));
15885
15886 die = die_info::allocate (&cu->comp_unit_obstack,
15887 abbrev->num_attrs + num_extra_attrs);
15888 die->sect_off = sect_off;
15889 die->tag = abbrev->tag;
15890 die->abbrev = abbrev_number;
15891 die->has_children = abbrev->has_children;
15892
15893 /* Make the result usable.
15894 The caller needs to update num_attrs after adding the extra
15895 attributes. */
15896 die->num_attrs = abbrev->num_attrs;
15897
15898 for (i = 0; i < abbrev->num_attrs; ++i)
15899 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15900 info_ptr, allow_reprocess);
15901
15902 *diep = die;
15903 return info_ptr;
15904 }
15905
15906 /* Read a die and all its attributes.
15907 Set DIEP to point to a newly allocated die with its information,
15908 except for its child, sibling, and parent fields. */
15909
15910 static const gdb_byte *
15911 read_toplevel_die (const struct die_reader_specs *reader,
15912 struct die_info **diep, const gdb_byte *info_ptr,
15913 gdb::array_view<attribute *> extra_attrs)
15914 {
15915 const gdb_byte *result;
15916 struct dwarf2_cu *cu = reader->cu;
15917
15918 result = read_full_die_1 (reader, diep, info_ptr, extra_attrs.size (),
15919 false);
15920
15921 /* Copy in the extra attributes, if any. */
15922 attribute *next = &(*diep)->attrs[(*diep)->num_attrs];
15923 for (attribute *extra : extra_attrs)
15924 *next++ = *extra;
15925
15926 struct attribute *attr = (*diep)->attr (DW_AT_str_offsets_base);
15927 if (attr != nullptr && attr->form_is_unsigned ())
15928 cu->str_offsets_base = attr->as_unsigned ();
15929
15930 attr = (*diep)->attr (DW_AT_loclists_base);
15931 if (attr != nullptr)
15932 cu->loclist_base = attr->as_unsigned ();
15933
15934 auto maybe_addr_base = (*diep)->addr_base ();
15935 if (maybe_addr_base.has_value ())
15936 cu->addr_base = *maybe_addr_base;
15937
15938 attr = (*diep)->attr (DW_AT_rnglists_base);
15939 if (attr != nullptr)
15940 cu->rnglists_base = attr->as_unsigned ();
15941
15942 for (int i = 0; i < (*diep)->num_attrs; ++i)
15943 {
15944 if ((*diep)->attrs[i].form_requires_reprocessing ())
15945 read_attribute_reprocess (reader, &(*diep)->attrs[i], (*diep)->tag);
15946 }
15947
15948 (*diep)->num_attrs += extra_attrs.size ();
15949
15950 if (dwarf_die_debug)
15951 {
15952 gdb_printf (gdb_stdlog,
15953 "Read die from %s@0x%x of %s:\n",
15954 reader->die_section->get_name (),
15955 (unsigned) (info_ptr - reader->die_section->buffer),
15956 bfd_get_filename (reader->abfd));
15957 (*diep)->dump (dwarf_die_debug);
15958 }
15959
15960 return result;
15961 }
15962 \f
15963
15964 void
15965 cooked_indexer::check_bounds (cutu_reader *reader)
15966 {
15967 if (reader->cu->per_cu->addresses_seen)
15968 return;
15969
15970 dwarf2_cu *cu = reader->cu;
15971
15972 unrelocated_addr best_lowpc = {}, best_highpc = {};
15973 /* Possibly set the default values of LOWPC and HIGHPC from
15974 `DW_AT_ranges'. */
15975 dwarf2_find_base_address (reader->comp_unit_die, cu);
15976 enum pc_bounds_kind cu_bounds_kind
15977 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
15978 cu, m_index_storage->get_addrmap (), cu->per_cu);
15979 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
15980 {
15981 /* Store the contiguous range if it is not empty; it can be
15982 empty for CUs with no code. addrmap requires CORE_ADDR, so
15983 we cast here. */
15984 m_index_storage->get_addrmap ()->set_empty ((CORE_ADDR) best_lowpc,
15985 (CORE_ADDR) best_highpc - 1,
15986 cu->per_cu);
15987
15988 cu->per_cu->addresses_seen = true;
15989 }
15990 }
15991
15992 /* Helper function that returns true if TAG can have a linkage
15993 name. */
15994
15995 static bool
15996 tag_can_have_linkage_name (enum dwarf_tag tag)
15997 {
15998 switch (tag)
15999 {
16000 case DW_TAG_variable:
16001 case DW_TAG_subprogram:
16002 return true;
16003
16004 default:
16005 return false;
16006 }
16007 }
16008
16009 cutu_reader *
16010 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
16011 dwarf2_per_objfile *per_objfile,
16012 sect_offset sect_off, bool is_dwz,
16013 bool for_scanning)
16014 {
16015 /* Lookups for type unit references are always in the CU, and
16016 cross-CU references will crash. */
16017 if (reader->cu->per_cu->is_dwz == is_dwz
16018 && reader->cu->header.offset_in_cu_p (sect_off))
16019 return reader;
16020
16021 dwarf2_per_cu_data *per_cu
16022 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
16023 per_objfile->per_bfd);
16024
16025 /* When scanning, we only want to visit a given CU a single time.
16026 Doing this check here avoids self-imports as well. */
16027 if (for_scanning)
16028 {
16029 bool nope = false;
16030 if (!per_cu->scanned.compare_exchange_strong (nope, true))
16031 return nullptr;
16032 }
16033 if (per_cu == m_per_cu)
16034 return reader;
16035
16036 cutu_reader *result = m_index_storage->get_reader (per_cu);
16037 if (result == nullptr)
16038 {
16039 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
16040 m_index_storage->get_abbrev_cache ());
16041
16042 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
16043 language_minimal);
16044 std::unique_ptr<cutu_reader> copy
16045 (new cutu_reader (std::move (new_reader)));
16046 result = m_index_storage->preserve (std::move (copy));
16047 }
16048
16049 if (result->dummy_p || !result->comp_unit_die->has_children)
16050 return nullptr;
16051
16052 if (for_scanning)
16053 check_bounds (result);
16054
16055 return result;
16056 }
16057
16058 const gdb_byte *
16059 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
16060 cutu_reader *reader,
16061 const gdb_byte *watermark_ptr,
16062 const gdb_byte *info_ptr,
16063 const abbrev_info *abbrev,
16064 const char **name,
16065 const char **linkage_name,
16066 cooked_index_flag *flags,
16067 sect_offset *sibling_offset,
16068 const cooked_index_entry **parent_entry,
16069 parent_map::addr_type *maybe_defer,
16070 bool *is_enum_class,
16071 bool for_specification)
16072 {
16073 bool origin_is_dwz = false;
16074 bool is_declaration = false;
16075 sect_offset origin_offset {};
16076
16077 std::optional<unrelocated_addr> low_pc;
16078 std::optional<unrelocated_addr> high_pc;
16079 bool high_pc_relative = false;
16080
16081 for (int i = 0; i < abbrev->num_attrs; ++i)
16082 {
16083 attribute attr;
16084 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16085
16086 /* Store the data if it is of an attribute we want to keep in a
16087 partial symbol table. */
16088 switch (attr.name)
16089 {
16090 case DW_AT_name:
16091 switch (abbrev->tag)
16092 {
16093 case DW_TAG_compile_unit:
16094 case DW_TAG_partial_unit:
16095 case DW_TAG_type_unit:
16096 /* Compilation units have a DW_AT_name that is a filename, not
16097 a source language identifier. */
16098 break;
16099
16100 default:
16101 if (*name == nullptr)
16102 *name = attr.as_string ();
16103 break;
16104 }
16105 break;
16106
16107 case DW_AT_linkage_name:
16108 case DW_AT_MIPS_linkage_name:
16109 /* Note that both forms of linkage name might appear. We
16110 assume they will be the same, and we only store the last
16111 one we see. */
16112 if (*linkage_name == nullptr)
16113 *linkage_name = attr.as_string ();
16114 break;
16115
16116 /* DWARF 4 has defined a dedicated DW_AT_main_subprogram
16117 attribute to indicate the starting function of the program... */
16118 case DW_AT_main_subprogram:
16119 if (attr.as_boolean ())
16120 *flags |= IS_MAIN;
16121 break;
16122
16123 /* ... however with older versions the DW_CC_program value of
16124 the DW_AT_calling_convention attribute was used instead as
16125 the only means available. We handle both variants then. */
16126 case DW_AT_calling_convention:
16127 if (attr.constant_value (DW_CC_normal) == DW_CC_program)
16128 *flags |= IS_MAIN;
16129 break;
16130
16131 case DW_AT_declaration:
16132 is_declaration = attr.as_boolean ();
16133 break;
16134
16135 case DW_AT_sibling:
16136 if (sibling_offset != nullptr)
16137 *sibling_offset = attr.get_ref_die_offset ();
16138 break;
16139
16140 case DW_AT_specification:
16141 case DW_AT_abstract_origin:
16142 case DW_AT_extension:
16143 origin_offset = attr.get_ref_die_offset ();
16144 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
16145 break;
16146
16147 case DW_AT_external:
16148 if (attr.as_boolean ())
16149 *flags &= ~IS_STATIC;
16150 break;
16151
16152 case DW_AT_enum_class:
16153 if (attr.as_boolean ())
16154 *is_enum_class = true;
16155 break;
16156
16157 case DW_AT_low_pc:
16158 low_pc = attr.as_address ();
16159 break;
16160
16161 case DW_AT_high_pc:
16162 high_pc = attr.as_address ();
16163 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
16164 high_pc_relative = true;
16165 break;
16166
16167 case DW_AT_location:
16168 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
16169 {
16170 struct dwarf_block *locdesc = attr.as_block ();
16171 CORE_ADDR addr;
16172 if (decode_locdesc (locdesc, reader->cu, &addr)
16173 && (addr != 0
16174 || reader->cu->per_objfile->per_bfd->has_section_at_zero))
16175 {
16176 low_pc = (unrelocated_addr) addr;
16177 /* For variables, we don't want to try decoding the
16178 type just to find the size -- for gdb's purposes
16179 we only need the address of a variable. */
16180 high_pc = (unrelocated_addr) (addr + 1);
16181 high_pc_relative = false;
16182 }
16183 }
16184 break;
16185
16186 case DW_AT_ranges:
16187 if (!scanning_per_cu->addresses_seen)
16188 {
16189 /* Offset in the .debug_ranges or .debug_rnglist section
16190 (depending on DWARF version). */
16191 ULONGEST ranges_offset = attr.as_unsigned ();
16192
16193 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
16194 want to add this value. */
16195 ranges_offset += reader->cu->gnu_ranges_base;
16196
16197 unrelocated_addr lowpc, highpc;
16198 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
16199 m_index_storage->get_addrmap (),
16200 scanning_per_cu, abbrev->tag);
16201 }
16202 break;
16203 }
16204 }
16205
16206 /* We don't want to examine declarations, but if we found a
16207 declaration when handling DW_AT_specification or the like, then
16208 that is ok. Similarly, we allow an external variable without a
16209 location; those are resolved via minimal symbols. */
16210 if (is_declaration && !for_specification
16211 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
16212 {
16213 /* We always want to recurse into some types, but we may not
16214 want to treat them as definitions. */
16215 if ((abbrev->tag == DW_TAG_class_type
16216 || abbrev->tag == DW_TAG_structure_type
16217 || abbrev->tag == DW_TAG_union_type)
16218 && abbrev->has_children)
16219 *flags |= IS_TYPE_DECLARATION;
16220 else
16221 {
16222 *linkage_name = nullptr;
16223 *name = nullptr;
16224 }
16225 }
16226 else if ((*name == nullptr
16227 || (*linkage_name == nullptr
16228 && tag_can_have_linkage_name (abbrev->tag))
16229 || (*parent_entry == nullptr && m_language != language_c))
16230 && origin_offset != sect_offset (0))
16231 {
16232 cutu_reader *new_reader
16233 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
16234 origin_is_dwz, false);
16235 if (new_reader != nullptr)
16236 {
16237 const gdb_byte *new_info_ptr = (new_reader->buffer
16238 + to_underlying (origin_offset));
16239
16240 if (*parent_entry == nullptr)
16241 {
16242 /* We only perform immediate lookups of parents for DIEs
16243 from earlier in this CU. This avoids any problem
16244 with a NULL result when when we see a reference to a
16245 DIE in another CU that we may or may not have
16246 imported locally. */
16247 parent_map::addr_type addr
16248 = parent_map::form_addr (origin_offset, origin_is_dwz);
16249 if (new_reader->cu != reader->cu || new_info_ptr > watermark_ptr)
16250 *maybe_defer = addr;
16251 else
16252 *parent_entry = m_die_range_map->find (addr);
16253 }
16254
16255 unsigned int bytes_read;
16256 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
16257 new_info_ptr,
16258 &bytes_read);
16259 new_info_ptr += bytes_read;
16260
16261 if (new_reader->cu == reader->cu && new_info_ptr == watermark_ptr)
16262 {
16263 /* Self-reference, we're done. */
16264 }
16265 else
16266 scan_attributes (scanning_per_cu, new_reader, new_info_ptr,
16267 new_info_ptr, new_abbrev, name, linkage_name,
16268 flags, nullptr, parent_entry, maybe_defer,
16269 is_enum_class, true);
16270 }
16271 }
16272
16273 if (!for_specification)
16274 {
16275 if (m_language == language_ada
16276 && *linkage_name == nullptr)
16277 *linkage_name = *name;
16278
16279 if (!scanning_per_cu->addresses_seen
16280 && low_pc.has_value ()
16281 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
16282 || *low_pc != (unrelocated_addr) 0)
16283 && high_pc.has_value ())
16284 {
16285 if (high_pc_relative)
16286 high_pc = (unrelocated_addr) ((ULONGEST) *high_pc
16287 + (ULONGEST) *low_pc);
16288
16289 if (*high_pc > *low_pc)
16290 {
16291 /* Need CORE_ADDR casts for addrmap. */
16292 m_index_storage->get_addrmap ()->set_empty
16293 ((CORE_ADDR) *low_pc, (CORE_ADDR) *high_pc - 1,
16294 scanning_per_cu);
16295 }
16296 }
16297
16298 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
16299 *flags &= ~IS_STATIC;
16300
16301 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
16302 *name = "(anonymous namespace)";
16303
16304 if (m_language == language_cplus
16305 && (abbrev->tag == DW_TAG_class_type
16306 || abbrev->tag == DW_TAG_interface_type
16307 || abbrev->tag == DW_TAG_structure_type
16308 || abbrev->tag == DW_TAG_union_type
16309 || abbrev->tag == DW_TAG_enumeration_type
16310 || abbrev->tag == DW_TAG_enumerator))
16311 *flags &= ~IS_STATIC;
16312
16313 /* Keep in sync with new_symbol. */
16314 if (abbrev->tag == DW_TAG_subprogram
16315 && (m_language == language_ada
16316 || m_language == language_fortran))
16317 *flags &= ~IS_STATIC;
16318 }
16319
16320 return info_ptr;
16321 }
16322
16323 const gdb_byte *
16324 cooked_indexer::index_imported_unit (cutu_reader *reader,
16325 const gdb_byte *info_ptr,
16326 const abbrev_info *abbrev)
16327 {
16328 sect_offset sect_off {};
16329 bool is_dwz = false;
16330
16331 for (int i = 0; i < abbrev->num_attrs; ++i)
16332 {
16333 /* Note that we never need to reprocess attributes here. */
16334 attribute attr;
16335 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16336
16337 if (attr.name == DW_AT_import)
16338 {
16339 sect_off = attr.get_ref_die_offset ();
16340 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16341 || reader->cu->per_cu->is_dwz);
16342 }
16343 }
16344
16345 /* Did not find DW_AT_import. */
16346 if (sect_off == sect_offset (0))
16347 return info_ptr;
16348
16349 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
16350 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
16351 is_dwz, true);
16352 if (new_reader != nullptr)
16353 {
16354 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
16355
16356 reader->cu->add_dependence (new_reader->cu->per_cu);
16357 }
16358
16359 return info_ptr;
16360 }
16361
16362 const gdb_byte *
16363 cooked_indexer::recurse (cutu_reader *reader,
16364 const gdb_byte *info_ptr,
16365 const cooked_index_entry *parent_entry,
16366 bool fully)
16367 {
16368 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16369
16370 if (parent_entry != nullptr)
16371 {
16372 /* Both start and end are inclusive, so use both "+ 1" and "- 1" to
16373 limit the range to the children of parent_entry. */
16374 parent_map::addr_type start
16375 = parent_map::form_addr (parent_entry->die_offset + 1,
16376 reader->cu->per_cu->is_dwz);
16377 parent_map::addr_type end
16378 = parent_map::form_addr (sect_offset (info_ptr - 1 - reader->buffer),
16379 reader->cu->per_cu->is_dwz);
16380 m_die_range_map->add_entry (start, end, parent_entry);
16381 }
16382
16383 return info_ptr;
16384 }
16385
16386 const gdb_byte *
16387 cooked_indexer::index_dies (cutu_reader *reader,
16388 const gdb_byte *info_ptr,
16389 const cooked_index_entry *parent_entry,
16390 bool fully)
16391 {
16392 const gdb_byte *end_ptr = (reader->buffer
16393 + to_underlying (reader->cu->header.sect_off)
16394 + reader->cu->header.get_length_with_initial ());
16395
16396 while (info_ptr < end_ptr)
16397 {
16398 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
16399 unsigned int bytes_read;
16400 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
16401 &bytes_read);
16402 info_ptr += bytes_read;
16403 if (abbrev == nullptr)
16404 break;
16405
16406 if (abbrev->tag == DW_TAG_imported_unit)
16407 {
16408 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
16409 continue;
16410 }
16411
16412 if (!abbrev->interesting)
16413 {
16414 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
16415 if (fully && abbrev->has_children)
16416 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16417 continue;
16418 }
16419
16420 const char *name = nullptr;
16421 const char *linkage_name = nullptr;
16422 parent_map::addr_type defer {};
16423 cooked_index_flag flags = IS_STATIC;
16424 sect_offset sibling {};
16425 const cooked_index_entry *this_parent_entry = parent_entry;
16426 bool is_enum_class = false;
16427
16428 /* The scope of a DW_TAG_entry_point cooked_index_entry is the one of
16429 its surrounding subroutine. */
16430 if (abbrev->tag == DW_TAG_entry_point)
16431 this_parent_entry = parent_entry->get_parent ();
16432 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
16433 info_ptr, abbrev, &name, &linkage_name,
16434 &flags, &sibling, &this_parent_entry,
16435 &defer, &is_enum_class, false);
16436 /* A DW_TAG_entry_point inherits its static/extern property from
16437 the enclosing subroutine. */
16438 if (abbrev->tag == DW_TAG_entry_point)
16439 {
16440 flags &= ~IS_STATIC;
16441 flags |= parent_entry->flags & IS_STATIC;
16442 }
16443 /* If the parent is an enum, but not an enum class, then use the
16444 grandparent instead. */
16445 if (this_parent_entry != nullptr
16446 && this_parent_entry->tag == DW_TAG_enumeration_type
16447 && !is_enum_class)
16448 this_parent_entry = this_parent_entry->get_parent ();
16449
16450 if (abbrev->tag == DW_TAG_namespace
16451 && m_language == language_cplus
16452 && strcmp (name, "::") == 0)
16453 {
16454 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
16455 generated bogus DW_TAG_namespace DIEs with a name of "::"
16456 for the global namespace. Work around this problem
16457 here. */
16458 name = nullptr;
16459 }
16460
16461 cooked_index_entry *this_entry = nullptr;
16462 if (name != nullptr)
16463 {
16464 if (defer != 0)
16465 this_entry
16466 = m_index_storage->add (this_die, abbrev->tag,
16467 flags | IS_PARENT_DEFERRED, name,
16468 defer, m_per_cu);
16469 else
16470 this_entry
16471 = m_index_storage->add (this_die, abbrev->tag, flags, name,
16472 this_parent_entry, m_per_cu);
16473 }
16474
16475 if (linkage_name != nullptr)
16476 {
16477 /* We only want this to be "main" if it has a linkage name
16478 but not an ordinary name. */
16479 if (name != nullptr)
16480 flags = flags & ~IS_MAIN;
16481 /* Set the IS_LINKAGE on for everything except when functions
16482 have linkage name present but name is absent. */
16483 if (name != nullptr
16484 || (abbrev->tag != DW_TAG_subprogram
16485 && abbrev->tag != DW_TAG_inlined_subroutine
16486 && abbrev->tag != DW_TAG_entry_point))
16487 flags = flags | IS_LINKAGE;
16488 m_index_storage->add (this_die, abbrev->tag, flags,
16489 linkage_name, nullptr, m_per_cu);
16490 }
16491
16492 if (abbrev->has_children)
16493 {
16494 switch (abbrev->tag)
16495 {
16496 case DW_TAG_class_type:
16497 case DW_TAG_interface_type:
16498 case DW_TAG_structure_type:
16499 case DW_TAG_union_type:
16500 if (m_language != language_c && this_entry != nullptr)
16501 {
16502 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16503 continue;
16504 }
16505 break;
16506
16507 case DW_TAG_enumeration_type:
16508 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16509 continue;
16510
16511 case DW_TAG_module:
16512 if (this_entry == nullptr)
16513 break;
16514 [[fallthrough]];
16515 case DW_TAG_namespace:
16516 /* We don't check THIS_ENTRY for a namespace, to handle
16517 the ancient G++ workaround pointed out above. */
16518 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16519 continue;
16520
16521 case DW_TAG_subprogram:
16522 if ((m_language == language_fortran
16523 || m_language == language_ada)
16524 && this_entry != nullptr)
16525 {
16526 info_ptr = recurse (reader, info_ptr, this_entry, true);
16527 continue;
16528 }
16529 break;
16530 }
16531
16532 if (sibling != sect_offset (0))
16533 {
16534 const gdb_byte *sibling_ptr
16535 = reader->buffer + to_underlying (sibling);
16536
16537 if (sibling_ptr < info_ptr)
16538 complaint (_("DW_AT_sibling points backwards"));
16539 else if (sibling_ptr > reader->buffer_end)
16540 reader->die_section->overflow_complaint ();
16541 else
16542 info_ptr = sibling_ptr;
16543 }
16544 else
16545 info_ptr = skip_children (reader, info_ptr);
16546 }
16547 }
16548
16549 return info_ptr;
16550 }
16551
16552 void
16553 cooked_indexer::make_index (cutu_reader *reader)
16554 {
16555 check_bounds (reader);
16556 find_file_and_directory (reader->comp_unit_die, reader->cu);
16557 if (!reader->comp_unit_die->has_children)
16558 return;
16559 index_dies (reader, reader->info_ptr, nullptr, false);
16560 }
16561
16562 dwarf2_per_cu_data *
16563 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
16564 unrelocated_addr adjusted_pc)
16565 {
16566 cooked_index *table
16567 = (gdb::checked_static_cast<cooked_index *>
16568 (per_bfd->index_table.get ()));
16569 return table->lookup (adjusted_pc);
16570 }
16571
16572 struct compunit_symtab *
16573 cooked_index_functions::find_compunit_symtab_by_address
16574 (struct objfile *objfile, CORE_ADDR address)
16575 {
16576 if (objfile->sect_index_data == -1)
16577 return nullptr;
16578
16579 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16580 cooked_index *table = wait (objfile, true);
16581
16582 CORE_ADDR baseaddr = objfile->data_section_offset ();
16583 dwarf2_per_cu_data *per_cu
16584 = table->lookup ((unrelocated_addr) (address - baseaddr));
16585 if (per_cu == nullptr)
16586 return nullptr;
16587
16588 return dw2_instantiate_symtab (per_cu, per_objfile, false);
16589 }
16590
16591 bool
16592 cooked_index_functions::expand_symtabs_matching
16593 (struct objfile *objfile,
16594 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16595 const lookup_name_info *lookup_name,
16596 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16597 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16598 block_search_flags search_flags,
16599 domain_search_flags domain)
16600 {
16601 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16602
16603 cooked_index *table = wait (objfile, true);
16604
16605 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
16606
16607 /* This invariant is documented in quick-functions.h. */
16608 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
16609 if (lookup_name == nullptr)
16610 {
16611 for (dwarf2_per_cu_data *per_cu
16612 : all_units_range (per_objfile->per_bfd))
16613 {
16614 QUIT;
16615
16616 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
16617 file_matcher,
16618 expansion_notify))
16619 return false;
16620 }
16621 return true;
16622 }
16623
16624 lookup_name_info lookup_name_without_params
16625 = lookup_name->make_ignore_params ();
16626 bool completing = lookup_name->completion_mode ();
16627
16628 /* Unique styles of language splitting. */
16629 static const enum language unique_styles[] =
16630 {
16631 /* No splitting is also a style. */
16632 language_c,
16633 /* This includes Rust. */
16634 language_cplus,
16635 /* This includes Go. */
16636 language_d,
16637 language_ada
16638 };
16639
16640 for (enum language lang : unique_styles)
16641 {
16642 std::vector<std::string_view> name_vec
16643 = lookup_name_without_params.split_name (lang);
16644 std::string last_name (name_vec.back ());
16645
16646 for (const cooked_index_entry *entry : table->find (last_name,
16647 completing))
16648 {
16649 QUIT;
16650
16651 /* No need to consider symbols from expanded CUs. */
16652 if (per_objfile->symtab_set_p (entry->per_cu))
16653 continue;
16654
16655 /* If file-matching was done, we don't need to consider
16656 symbols from unmarked CUs. */
16657 if (file_matcher != nullptr && !entry->per_cu->mark)
16658 continue;
16659
16660 /* See if the symbol matches the type filter. */
16661 if (!entry->matches (search_flags)
16662 || !entry->matches (domain))
16663 continue;
16664
16665 /* We've found the base name of the symbol; now walk its
16666 parentage chain, ensuring that each component
16667 matches. */
16668 bool found = true;
16669
16670 const cooked_index_entry *parent = entry->get_parent ();
16671 for (int i = name_vec.size () - 1; i > 0; --i)
16672 {
16673 /* If we ran out of entries, or if this segment doesn't
16674 match, this did not match. */
16675 if (parent == nullptr
16676 || strncmp (parent->name, name_vec[i - 1].data (),
16677 name_vec[i - 1].length ()) != 0)
16678 {
16679 found = false;
16680 break;
16681 }
16682
16683 parent = parent->get_parent ();
16684 }
16685
16686 if (!found)
16687 continue;
16688
16689 /* Might have been looking for "a::b" and found
16690 "x::a::b". */
16691 if (symbol_matcher == nullptr)
16692 {
16693 symbol_name_match_type match_type
16694 = lookup_name_without_params.match_type ();
16695 if ((match_type == symbol_name_match_type::FULL
16696 || (lang != language_ada
16697 && match_type == symbol_name_match_type::EXPRESSION))
16698 && parent != nullptr)
16699 continue;
16700 }
16701 else
16702 {
16703 auto_obstack temp_storage;
16704 const char *full_name = entry->full_name (&temp_storage);
16705 if (!symbol_matcher (full_name))
16706 continue;
16707 }
16708
16709 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
16710 file_matcher,
16711 expansion_notify))
16712 return false;
16713 }
16714 }
16715
16716 return true;
16717 }
16718
16719 /* Start reading .debug_info using the indexer. */
16720
16721 static void
16722 start_debug_info_reader (dwarf2_per_objfile *per_objfile)
16723 {
16724 /* Set the index table early so that sharing works even while
16725 scanning; and then start the scanning. */
16726 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
16727 std::unique_ptr<cooked_index_worker> worker
16728 (new cooked_index_debug_info (per_objfile));
16729 cooked_index *idx = new cooked_index (per_objfile, std::move (worker));
16730 per_bfd->index_table.reset (idx);
16731 /* Don't start reading until after 'index_table' is set. This
16732 avoids races. */
16733 idx->start_reading ();
16734 }
16735
16736 \f
16737
16738 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
16739 contents from the given SECTION in the HEADER.
16740
16741 HEADER_OFFSET is the offset of the header in the section. */
16742 static void
16743 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
16744 struct dwarf2_section_info *section,
16745 sect_offset header_offset)
16746 {
16747 unsigned int bytes_read;
16748 bfd *abfd = section->get_bfd_owner ();
16749 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
16750
16751 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
16752 info_ptr += bytes_read;
16753
16754 header->version = read_2_bytes (abfd, info_ptr);
16755 info_ptr += 2;
16756
16757 header->addr_size = read_1_byte (abfd, info_ptr);
16758 info_ptr += 1;
16759
16760 header->segment_collector_size = read_1_byte (abfd, info_ptr);
16761 info_ptr += 1;
16762
16763 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
16764 }
16765
16766 /* Return the DW_AT_loclists_base value for the CU. */
16767 static ULONGEST
16768 lookup_loclist_base (struct dwarf2_cu *cu)
16769 {
16770 /* For the .dwo unit, the loclist_base points to the first offset following
16771 the header. The header consists of the following entities-
16772 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
16773 bit format)
16774 2. version (2 bytes)
16775 3. address size (1 byte)
16776 4. segment selector size (1 byte)
16777 5. offset entry count (4 bytes)
16778 These sizes are derived as per the DWARFv5 standard. */
16779 if (cu->dwo_unit != nullptr)
16780 {
16781 if (cu->header.initial_length_size == 4)
16782 return LOCLIST_HEADER_SIZE32;
16783 return LOCLIST_HEADER_SIZE64;
16784 }
16785 return cu->loclist_base;
16786 }
16787
16788 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
16789 array of offsets in the .debug_loclists section. */
16790
16791 static sect_offset
16792 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
16793 {
16794 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16795 struct objfile *objfile = per_objfile->objfile;
16796 bfd *abfd = objfile->obfd.get ();
16797 ULONGEST loclist_header_size =
16798 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
16799 : LOCLIST_HEADER_SIZE64);
16800 ULONGEST loclist_base = lookup_loclist_base (cu);
16801
16802 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
16803 ULONGEST start_offset =
16804 loclist_base + loclist_index * cu->header.offset_size;
16805
16806 /* Get loclists section. */
16807 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16808
16809 /* Read the loclists section content. */
16810 section->read (objfile);
16811 if (section->buffer == NULL)
16812 error (_("DW_FORM_loclistx used without .debug_loclists "
16813 "section [in module %s]"), objfile_name (objfile));
16814
16815 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
16816 so if loclist_base is smaller than the header size, we have a problem. */
16817 if (loclist_base < loclist_header_size)
16818 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
16819 objfile_name (objfile));
16820
16821 /* Read the header of the loclists contribution. */
16822 struct loclists_rnglists_header header;
16823 read_loclists_rnglists_header (&header, section,
16824 (sect_offset) (loclist_base - loclist_header_size));
16825
16826 /* Verify the loclist index is valid. */
16827 if (loclist_index >= header.offset_entry_count)
16828 error (_("DW_FORM_loclistx pointing outside of "
16829 ".debug_loclists offset array [in module %s]"),
16830 objfile_name (objfile));
16831
16832 /* Validate that reading won't go beyond the end of the section. */
16833 if (start_offset + cu->header.offset_size > section->size)
16834 error (_("Reading DW_FORM_loclistx index beyond end of"
16835 ".debug_loclists section [in module %s]"),
16836 objfile_name (objfile));
16837
16838 const gdb_byte *info_ptr = section->buffer + start_offset;
16839
16840 if (cu->header.offset_size == 4)
16841 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
16842 else
16843 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
16844 }
16845
16846 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
16847 array of offsets in the .debug_rnglists section. */
16848
16849 static sect_offset
16850 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
16851 dwarf_tag tag)
16852 {
16853 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
16854 struct objfile *objfile = dwarf2_per_objfile->objfile;
16855 bfd *abfd = objfile->obfd.get ();
16856 ULONGEST rnglist_header_size =
16857 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
16858 : RNGLIST_HEADER_SIZE64);
16859
16860 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
16861 .debug_rnglists.dwo section. The rnglists base given in the skeleton
16862 doesn't apply. */
16863 ULONGEST rnglist_base =
16864 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
16865
16866 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
16867 ULONGEST start_offset =
16868 rnglist_base + rnglist_index * cu->header.offset_size;
16869
16870 /* Get rnglists section. */
16871 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
16872
16873 /* Read the rnglists section content. */
16874 section->read (objfile);
16875 if (section->buffer == nullptr)
16876 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
16877 "[in module %s]"),
16878 objfile_name (objfile));
16879
16880 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
16881 so if rnglist_base is smaller than the header size, we have a problem. */
16882 if (rnglist_base < rnglist_header_size)
16883 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
16884 objfile_name (objfile));
16885
16886 /* Read the header of the rnglists contribution. */
16887 struct loclists_rnglists_header header;
16888 read_loclists_rnglists_header (&header, section,
16889 (sect_offset) (rnglist_base - rnglist_header_size));
16890
16891 /* Verify the rnglist index is valid. */
16892 if (rnglist_index >= header.offset_entry_count)
16893 error (_("DW_FORM_rnglistx index pointing outside of "
16894 ".debug_rnglists offset array [in module %s]"),
16895 objfile_name (objfile));
16896
16897 /* Validate that reading won't go beyond the end of the section. */
16898 if (start_offset + cu->header.offset_size > section->size)
16899 error (_("Reading DW_FORM_rnglistx index beyond end of"
16900 ".debug_rnglists section [in module %s]"),
16901 objfile_name (objfile));
16902
16903 const gdb_byte *info_ptr = section->buffer + start_offset;
16904
16905 if (cu->header.offset_size == 4)
16906 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
16907 else
16908 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
16909 }
16910
16911 /* Process the attributes that had to be skipped in the first round. These
16912 attributes are the ones that need str_offsets_base or addr_base attributes.
16913 They could not have been processed in the first round, because at the time
16914 the values of str_offsets_base or addr_base may not have been known. */
16915 static void
16916 read_attribute_reprocess (const struct die_reader_specs *reader,
16917 struct attribute *attr, dwarf_tag tag)
16918 {
16919 struct dwarf2_cu *cu = reader->cu;
16920 switch (attr->form)
16921 {
16922 case DW_FORM_addrx:
16923 case DW_FORM_GNU_addr_index:
16924 attr->set_address (read_addr_index (cu,
16925 attr->as_unsigned_reprocess ()));
16926 break;
16927 case DW_FORM_loclistx:
16928 {
16929 sect_offset loclists_sect_off
16930 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
16931
16932 attr->set_unsigned (to_underlying (loclists_sect_off));
16933 }
16934 break;
16935 case DW_FORM_rnglistx:
16936 {
16937 sect_offset rnglists_sect_off
16938 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
16939
16940 attr->set_unsigned (to_underlying (rnglists_sect_off));
16941 }
16942 break;
16943 case DW_FORM_strx:
16944 case DW_FORM_strx1:
16945 case DW_FORM_strx2:
16946 case DW_FORM_strx3:
16947 case DW_FORM_strx4:
16948 case DW_FORM_GNU_str_index:
16949 {
16950 unsigned int str_index = attr->as_unsigned_reprocess ();
16951 gdb_assert (!attr->canonical_string_p ());
16952 if (reader->dwo_file != NULL)
16953 attr->set_string_noncanonical (read_dwo_str_index (reader,
16954 str_index));
16955 else
16956 attr->set_string_noncanonical (read_stub_str_index (cu,
16957 str_index));
16958 break;
16959 }
16960 default:
16961 gdb_assert_not_reached ("Unexpected DWARF form.");
16962 }
16963 }
16964
16965 /* Read an attribute value described by an attribute form. */
16966
16967 static const gdb_byte *
16968 read_attribute_value (const struct die_reader_specs *reader,
16969 struct attribute *attr, unsigned form,
16970 LONGEST implicit_const, const gdb_byte *info_ptr,
16971 bool allow_reprocess)
16972 {
16973 struct dwarf2_cu *cu = reader->cu;
16974 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16975 struct objfile *objfile = per_objfile->objfile;
16976 bfd *abfd = reader->abfd;
16977 struct comp_unit_head *cu_header = &cu->header;
16978 unsigned int bytes_read;
16979 struct dwarf_block *blk;
16980
16981 attr->form = (enum dwarf_form) form;
16982 switch (form)
16983 {
16984 case DW_FORM_ref_addr:
16985 if (cu_header->version == 2)
16986 attr->set_unsigned ((ULONGEST) cu_header->read_address (abfd, info_ptr,
16987 &bytes_read));
16988 else
16989 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
16990 &bytes_read));
16991 info_ptr += bytes_read;
16992 break;
16993 case DW_FORM_GNU_ref_alt:
16994 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
16995 &bytes_read));
16996 info_ptr += bytes_read;
16997 break;
16998 case DW_FORM_addr:
16999 {
17000 unrelocated_addr addr = cu_header->read_address (abfd, info_ptr,
17001 &bytes_read);
17002 attr->set_address (addr);
17003 info_ptr += bytes_read;
17004 }
17005 break;
17006 case DW_FORM_block2:
17007 blk = dwarf_alloc_block (cu);
17008 blk->size = read_2_bytes (abfd, info_ptr);
17009 info_ptr += 2;
17010 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17011 info_ptr += blk->size;
17012 attr->set_block (blk);
17013 break;
17014 case DW_FORM_block4:
17015 blk = dwarf_alloc_block (cu);
17016 blk->size = read_4_bytes (abfd, info_ptr);
17017 info_ptr += 4;
17018 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17019 info_ptr += blk->size;
17020 attr->set_block (blk);
17021 break;
17022 case DW_FORM_data2:
17023 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
17024 info_ptr += 2;
17025 break;
17026 case DW_FORM_data4:
17027 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
17028 info_ptr += 4;
17029 break;
17030 case DW_FORM_data8:
17031 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
17032 info_ptr += 8;
17033 break;
17034 case DW_FORM_data16:
17035 blk = dwarf_alloc_block (cu);
17036 blk->size = 16;
17037 blk->data = read_n_bytes (abfd, info_ptr, 16);
17038 info_ptr += 16;
17039 attr->set_block (blk);
17040 break;
17041 case DW_FORM_sec_offset:
17042 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17043 &bytes_read));
17044 info_ptr += bytes_read;
17045 break;
17046 case DW_FORM_loclistx:
17047 {
17048 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17049 &bytes_read));
17050 info_ptr += bytes_read;
17051 if (allow_reprocess)
17052 read_attribute_reprocess (reader, attr);
17053 }
17054 break;
17055 case DW_FORM_string:
17056 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
17057 &bytes_read));
17058 info_ptr += bytes_read;
17059 break;
17060 case DW_FORM_strp:
17061 if (!cu->per_cu->is_dwz)
17062 {
17063 attr->set_string_noncanonical
17064 (read_indirect_string (per_objfile,
17065 abfd, info_ptr, cu_header,
17066 &bytes_read));
17067 info_ptr += bytes_read;
17068 break;
17069 }
17070 [[fallthrough]];
17071 case DW_FORM_line_strp:
17072 if (!cu->per_cu->is_dwz)
17073 {
17074 attr->set_string_noncanonical
17075 (per_objfile->read_line_string (info_ptr, cu_header,
17076 &bytes_read));
17077 info_ptr += bytes_read;
17078 break;
17079 }
17080 [[fallthrough]];
17081 case DW_FORM_GNU_strp_alt:
17082 {
17083 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17084 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
17085 &bytes_read);
17086
17087 attr->set_string_noncanonical
17088 (dwz->read_string (objfile, str_offset));
17089 info_ptr += bytes_read;
17090 }
17091 break;
17092 case DW_FORM_exprloc:
17093 case DW_FORM_block:
17094 blk = dwarf_alloc_block (cu);
17095 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17096 info_ptr += bytes_read;
17097 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17098 info_ptr += blk->size;
17099 attr->set_block (blk);
17100 break;
17101 case DW_FORM_block1:
17102 blk = dwarf_alloc_block (cu);
17103 blk->size = read_1_byte (abfd, info_ptr);
17104 info_ptr += 1;
17105 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17106 info_ptr += blk->size;
17107 attr->set_block (blk);
17108 break;
17109 case DW_FORM_data1:
17110 case DW_FORM_flag:
17111 attr->set_unsigned (read_1_byte (abfd, info_ptr));
17112 info_ptr += 1;
17113 break;
17114 case DW_FORM_flag_present:
17115 attr->set_unsigned (1);
17116 break;
17117 case DW_FORM_sdata:
17118 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
17119 info_ptr += bytes_read;
17120 break;
17121 case DW_FORM_rnglistx:
17122 {
17123 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17124 &bytes_read));
17125 info_ptr += bytes_read;
17126 if (allow_reprocess)
17127 read_attribute_reprocess (reader, attr);
17128 }
17129 break;
17130 case DW_FORM_udata:
17131 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17132 info_ptr += bytes_read;
17133 break;
17134 case DW_FORM_ref1:
17135 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17136 + read_1_byte (abfd, info_ptr)));
17137 info_ptr += 1;
17138 break;
17139 case DW_FORM_ref2:
17140 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17141 + read_2_bytes (abfd, info_ptr)));
17142 info_ptr += 2;
17143 break;
17144 case DW_FORM_ref4:
17145 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17146 + read_4_bytes (abfd, info_ptr)));
17147 info_ptr += 4;
17148 break;
17149 case DW_FORM_ref8:
17150 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17151 + read_8_bytes (abfd, info_ptr)));
17152 info_ptr += 8;
17153 break;
17154 case DW_FORM_ref_sig8:
17155 attr->set_signature (read_8_bytes (abfd, info_ptr));
17156 info_ptr += 8;
17157 break;
17158 case DW_FORM_ref_udata:
17159 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17160 + read_unsigned_leb128 (abfd, info_ptr,
17161 &bytes_read)));
17162 info_ptr += bytes_read;
17163 break;
17164 case DW_FORM_indirect:
17165 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17166 info_ptr += bytes_read;
17167 if (form == DW_FORM_implicit_const)
17168 {
17169 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17170 info_ptr += bytes_read;
17171 }
17172 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17173 info_ptr, allow_reprocess);
17174 break;
17175 case DW_FORM_implicit_const:
17176 attr->set_signed (implicit_const);
17177 break;
17178 case DW_FORM_addrx:
17179 case DW_FORM_GNU_addr_index:
17180 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17181 &bytes_read));
17182 info_ptr += bytes_read;
17183 if (allow_reprocess)
17184 read_attribute_reprocess (reader, attr);
17185 break;
17186 case DW_FORM_strx:
17187 case DW_FORM_strx1:
17188 case DW_FORM_strx2:
17189 case DW_FORM_strx3:
17190 case DW_FORM_strx4:
17191 case DW_FORM_GNU_str_index:
17192 {
17193 ULONGEST str_index;
17194 if (form == DW_FORM_strx1)
17195 {
17196 str_index = read_1_byte (abfd, info_ptr);
17197 info_ptr += 1;
17198 }
17199 else if (form == DW_FORM_strx2)
17200 {
17201 str_index = read_2_bytes (abfd, info_ptr);
17202 info_ptr += 2;
17203 }
17204 else if (form == DW_FORM_strx3)
17205 {
17206 str_index = read_3_bytes (abfd, info_ptr);
17207 info_ptr += 3;
17208 }
17209 else if (form == DW_FORM_strx4)
17210 {
17211 str_index = read_4_bytes (abfd, info_ptr);
17212 info_ptr += 4;
17213 }
17214 else
17215 {
17216 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17217 info_ptr += bytes_read;
17218 }
17219 attr->set_unsigned_reprocess (str_index);
17220 if (allow_reprocess)
17221 read_attribute_reprocess (reader, attr);
17222 }
17223 break;
17224 default:
17225 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17226 dwarf_form_name (form),
17227 bfd_get_filename (abfd));
17228 }
17229
17230 /* Super hack. */
17231 if (cu->per_cu->is_dwz && attr->form_is_ref ())
17232 attr->form = DW_FORM_GNU_ref_alt;
17233
17234 /* We have seen instances where the compiler tried to emit a byte
17235 size attribute of -1 which ended up being encoded as an unsigned
17236 0xffffffff. Although 0xffffffff is technically a valid size value,
17237 an object of this size seems pretty unlikely so we can relatively
17238 safely treat these cases as if the size attribute was invalid and
17239 treat them as zero by default. */
17240 if (attr->name == DW_AT_byte_size
17241 && form == DW_FORM_data4
17242 && attr->as_unsigned () >= 0xffffffff)
17243 {
17244 complaint
17245 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17246 hex_string (attr->as_unsigned ()));
17247 attr->set_unsigned (0);
17248 }
17249
17250 return info_ptr;
17251 }
17252
17253 /* Read an attribute described by an abbreviated attribute. */
17254
17255 static const gdb_byte *
17256 read_attribute (const struct die_reader_specs *reader,
17257 struct attribute *attr, const struct attr_abbrev *abbrev,
17258 const gdb_byte *info_ptr,
17259 bool allow_reprocess)
17260 {
17261 attr->name = abbrev->name;
17262 attr->string_is_canonical = 0;
17263 return read_attribute_value (reader, attr, abbrev->form,
17264 abbrev->implicit_const, info_ptr,
17265 allow_reprocess);
17266 }
17267
17268 /* See read.h. */
17269
17270 const char *
17271 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
17272 LONGEST str_offset)
17273 {
17274 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
17275 str_offset, "DW_FORM_strp");
17276 }
17277
17278 /* Return pointer to string at .debug_str offset as read from BUF.
17279 BUF is assumed to be in a compilation unit described by CU_HEADER.
17280 Return *BYTES_READ_PTR count of bytes read from BUF. */
17281
17282 static const char *
17283 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
17284 const gdb_byte *buf,
17285 const struct comp_unit_head *cu_header,
17286 unsigned int *bytes_read_ptr)
17287 {
17288 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17289
17290 return read_indirect_string_at_offset (per_objfile, str_offset);
17291 }
17292
17293 /* See read.h. */
17294
17295 const char *
17296 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17297 unsigned int offset_size)
17298 {
17299 bfd *abfd = objfile->obfd.get ();
17300 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
17301
17302 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17303 }
17304
17305 /* See read.h. */
17306
17307 const char *
17308 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17309 const struct comp_unit_head *cu_header,
17310 unsigned int *bytes_read_ptr)
17311 {
17312 bfd *abfd = objfile->obfd.get ();
17313 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17314
17315 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17316 }
17317
17318 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17319 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
17320 ADDR_SIZE is the size of addresses from the CU header. */
17321
17322 static unrelocated_addr
17323 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
17324 std::optional<ULONGEST> addr_base, int addr_size)
17325 {
17326 struct objfile *objfile = per_objfile->objfile;
17327 bfd *abfd = objfile->obfd.get ();
17328 const gdb_byte *info_ptr;
17329 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
17330
17331 per_objfile->per_bfd->addr.read (objfile);
17332 if (per_objfile->per_bfd->addr.buffer == NULL)
17333 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17334 objfile_name (objfile));
17335 if (addr_base_or_zero + addr_index * addr_size
17336 >= per_objfile->per_bfd->addr.size)
17337 error (_("DW_FORM_addr_index pointing outside of "
17338 ".debug_addr section [in module %s]"),
17339 objfile_name (objfile));
17340 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
17341 + addr_index * addr_size);
17342 if (addr_size == 4)
17343 return (unrelocated_addr) bfd_get_32 (abfd, info_ptr);
17344 else
17345 return (unrelocated_addr) bfd_get_64 (abfd, info_ptr);
17346 }
17347
17348 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17349
17350 static unrelocated_addr
17351 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17352 {
17353 return read_addr_index_1 (cu->per_objfile, addr_index,
17354 cu->addr_base, cu->header.addr_size);
17355 }
17356
17357 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17358
17359 static unrelocated_addr
17360 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17361 unsigned int *bytes_read)
17362 {
17363 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
17364 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17365
17366 return read_addr_index (cu, addr_index);
17367 }
17368
17369 /* See read.h. */
17370
17371 unrelocated_addr
17372 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
17373 dwarf2_per_objfile *per_objfile,
17374 unsigned int addr_index)
17375 {
17376 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
17377 std::optional<ULONGEST> addr_base;
17378 int addr_size;
17379
17380 /* We need addr_base and addr_size.
17381 If we don't have PER_CU->cu, we have to get it.
17382 Nasty, but the alternative is storing the needed info in PER_CU,
17383 which at this point doesn't seem justified: it's not clear how frequently
17384 it would get used and it would increase the size of every PER_CU.
17385 Entry points like dwarf2_per_cu_addr_size do a similar thing
17386 so we're not in uncharted territory here.
17387 Alas we need to be a bit more complicated as addr_base is contained
17388 in the DIE.
17389
17390 We don't need to read the entire CU(/TU).
17391 We just need the header and top level die.
17392
17393 IWBN to use the aging mechanism to let us lazily later discard the CU.
17394 For now we skip this optimization. */
17395
17396 if (cu != NULL)
17397 {
17398 addr_base = cu->addr_base;
17399 addr_size = cu->header.addr_size;
17400 }
17401 else
17402 {
17403 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
17404 addr_base = reader.cu->addr_base;
17405 addr_size = reader.cu->header.addr_size;
17406 }
17407
17408 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
17409 }
17410
17411 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
17412 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
17413 DWO file. */
17414
17415 static const char *
17416 read_str_index (struct dwarf2_cu *cu,
17417 struct dwarf2_section_info *str_section,
17418 struct dwarf2_section_info *str_offsets_section,
17419 ULONGEST str_offsets_base, ULONGEST str_index,
17420 unsigned offset_size)
17421 {
17422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17423 struct objfile *objfile = per_objfile->objfile;
17424 const char *objf_name = objfile_name (objfile);
17425 bfd *abfd = objfile->obfd.get ();
17426 const gdb_byte *info_ptr;
17427 ULONGEST str_offset;
17428 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
17429
17430 str_section->read (objfile);
17431 str_offsets_section->read (objfile);
17432 if (str_section->buffer == NULL)
17433 error (_("%s used without %s section"
17434 " in CU at offset %s [in module %s]"),
17435 form_name, str_section->get_name (),
17436 sect_offset_str (cu->header.sect_off), objf_name);
17437 if (str_offsets_section->buffer == NULL)
17438 error (_("%s used without %s section"
17439 " in CU at offset %s [in module %s]"),
17440 form_name, str_section->get_name (),
17441 sect_offset_str (cu->header.sect_off), objf_name);
17442 info_ptr = (str_offsets_section->buffer
17443 + str_offsets_base
17444 + str_index * offset_size);
17445 if (offset_size == 4)
17446 str_offset = bfd_get_32 (abfd, info_ptr);
17447 else
17448 str_offset = bfd_get_64 (abfd, info_ptr);
17449 if (str_offset >= str_section->size)
17450 error (_("Offset from %s pointing outside of"
17451 " %s section in CU at offset %s [in module %s]"),
17452 form_name, str_section->get_name (),
17453 sect_offset_str (cu->header.sect_off), objf_name);
17454 return (const char *) (str_section->buffer + str_offset);
17455 }
17456
17457 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
17458
17459 static const char *
17460 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17461 {
17462 unsigned offset_size;
17463 ULONGEST str_offsets_base;
17464 if (reader->cu->header.version >= 5)
17465 {
17466 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
17467 so assume the .debug_str_offsets section is DWARF5 as well, and
17468 parse the header. FIXME: Parse the header only once. */
17469 unsigned int bytes_read = 0;
17470 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
17471 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
17472
17473 /* Header: Initial length. */
17474 read_initial_length (abfd, p + bytes_read, &bytes_read);
17475
17476 /* Determine offset_size based on the .debug_str_offsets header. */
17477 const bool dwarf5_is_dwarf64 = bytes_read != 4;
17478 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
17479
17480 /* Header: Version. */
17481 unsigned version = read_2_bytes (abfd, p + bytes_read);
17482 bytes_read += 2;
17483
17484 if (version <= 4)
17485 {
17486 /* We'd like one warning here about ignoring the section, but
17487 because we parse the header more than once (see FIXME above)
17488 we'd have many warnings, so use a complaint instead, which at
17489 least has a limit. */
17490 complaint (_("Section .debug_str_offsets in %s has unsupported"
17491 " version %d, use empty string."),
17492 reader->dwo_file->dwo_name.c_str (), version);
17493 return "";
17494 }
17495
17496 /* Header: Padding. */
17497 bytes_read += 2;
17498
17499 str_offsets_base = bytes_read;
17500 }
17501 else
17502 {
17503 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
17504 section, assume the .debug_str_offsets section is pre-DWARF5 as
17505 well, which doesn't have a header. */
17506 str_offsets_base = 0;
17507
17508 /* Determine offset_size based on the .debug_info header. */
17509 offset_size = reader->cu->header.offset_size;
17510 }
17511
17512 return read_str_index (reader->cu,
17513 &reader->dwo_file->sections.str,
17514 &reader->dwo_file->sections.str_offsets,
17515 str_offsets_base, str_index, offset_size);
17516 }
17517
17518 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
17519
17520 static const char *
17521 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
17522 {
17523 struct objfile *objfile = cu->per_objfile->objfile;
17524 const char *objf_name = objfile_name (objfile);
17525 static const char form_name[] = "DW_FORM_GNU_str_index";
17526 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
17527
17528 if (!cu->str_offsets_base.has_value ())
17529 error (_("%s used in Fission stub without %s"
17530 " in CU at offset 0x%lx [in module %s]"),
17531 form_name, str_offsets_attr_name,
17532 (long) cu->header.offset_size, objf_name);
17533
17534 return read_str_index (cu,
17535 &cu->per_objfile->per_bfd->str,
17536 &cu->per_objfile->per_bfd->str_offsets,
17537 *cu->str_offsets_base, str_index,
17538 cu->header.offset_size);
17539 }
17540
17541 /* Return the length of an LEB128 number in BUF. */
17542
17543 static int
17544 leb128_size (const gdb_byte *buf)
17545 {
17546 const gdb_byte *begin = buf;
17547 gdb_byte byte;
17548
17549 while (1)
17550 {
17551 byte = *buf++;
17552 if ((byte & 128) == 0)
17553 return buf - begin;
17554 }
17555 }
17556
17557 /* Converts DWARF language names to GDB language names. */
17558
17559 enum language
17560 dwarf_lang_to_enum_language (unsigned int lang)
17561 {
17562 enum language language;
17563
17564 switch (lang)
17565 {
17566 case DW_LANG_C89:
17567 case DW_LANG_C99:
17568 case DW_LANG_C11:
17569 case DW_LANG_C:
17570 case DW_LANG_UPC:
17571 language = language_c;
17572 break;
17573 case DW_LANG_Java:
17574 case DW_LANG_C_plus_plus:
17575 case DW_LANG_C_plus_plus_11:
17576 case DW_LANG_C_plus_plus_14:
17577 language = language_cplus;
17578 break;
17579 case DW_LANG_D:
17580 language = language_d;
17581 break;
17582 case DW_LANG_Fortran77:
17583 case DW_LANG_Fortran90:
17584 case DW_LANG_Fortran95:
17585 case DW_LANG_Fortran03:
17586 case DW_LANG_Fortran08:
17587 language = language_fortran;
17588 break;
17589 case DW_LANG_Go:
17590 language = language_go;
17591 break;
17592 case DW_LANG_Mips_Assembler:
17593 language = language_asm;
17594 break;
17595 case DW_LANG_Ada83:
17596 case DW_LANG_Ada95:
17597 language = language_ada;
17598 break;
17599 case DW_LANG_Modula2:
17600 language = language_m2;
17601 break;
17602 case DW_LANG_Pascal83:
17603 language = language_pascal;
17604 break;
17605 case DW_LANG_ObjC:
17606 language = language_objc;
17607 break;
17608 case DW_LANG_Rust:
17609 case DW_LANG_Rust_old:
17610 language = language_rust;
17611 break;
17612 case DW_LANG_OpenCL:
17613 language = language_opencl;
17614 break;
17615 case DW_LANG_Cobol74:
17616 case DW_LANG_Cobol85:
17617 default:
17618 language = language_minimal;
17619 break;
17620 }
17621
17622 return language;
17623 }
17624
17625 /* Return the named attribute or NULL if not there. */
17626
17627 static struct attribute *
17628 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17629 {
17630 for (;;)
17631 {
17632 unsigned int i;
17633 struct attribute *spec = NULL;
17634
17635 for (i = 0; i < die->num_attrs; ++i)
17636 {
17637 if (die->attrs[i].name == name)
17638 return &die->attrs[i];
17639 if (die->attrs[i].name == DW_AT_specification
17640 || die->attrs[i].name == DW_AT_abstract_origin)
17641 spec = &die->attrs[i];
17642 }
17643
17644 if (!spec)
17645 break;
17646
17647 struct die_info *prev_die = die;
17648 die = follow_die_ref (die, spec, &cu);
17649 if (die == prev_die)
17650 /* Self-reference, we're done. */
17651 break;
17652 }
17653
17654 return NULL;
17655 }
17656
17657 /* Return the string associated with a string-typed attribute, or NULL if it
17658 is either not found or is of an incorrect type. */
17659
17660 static const char *
17661 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17662 {
17663 struct attribute *attr;
17664 const char *str = NULL;
17665
17666 attr = dwarf2_attr (die, name, cu);
17667
17668 if (attr != NULL)
17669 {
17670 str = attr->as_string ();
17671 if (str == nullptr)
17672 complaint (_("string type expected for attribute %s for "
17673 "DIE at %s in module %s"),
17674 dwarf_attr_name (name), sect_offset_str (die->sect_off),
17675 objfile_name (cu->per_objfile->objfile));
17676 }
17677
17678 return str;
17679 }
17680
17681 /* Return the dwo name or NULL if not present. If present, it is in either
17682 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
17683 static const char *
17684 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
17685 {
17686 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
17687 if (dwo_name == nullptr)
17688 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
17689 return dwo_name;
17690 }
17691
17692 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17693 and holds a non-zero value. This function should only be used for
17694 DW_FORM_flag or DW_FORM_flag_present attributes. */
17695
17696 static int
17697 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17698 {
17699 struct attribute *attr = dwarf2_attr (die, name, cu);
17700
17701 return attr != nullptr && attr->as_boolean ();
17702 }
17703
17704 static int
17705 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17706 {
17707 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17708 which value is non-zero. However, we have to be careful with
17709 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17710 (via dwarf2_flag_true_p) follows this attribute. So we may
17711 end up accidently finding a declaration attribute that belongs
17712 to a different DIE referenced by the specification attribute,
17713 even though the given DIE does not have a declaration attribute. */
17714 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17715 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17716 }
17717
17718 /* Return the die giving the specification for DIE, if there is
17719 one. *SPEC_CU is the CU containing DIE on input, and the CU
17720 containing the return value on output. If there is no
17721 specification, but there is an abstract origin, that is
17722 returned. */
17723
17724 static struct die_info *
17725 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17726 {
17727 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17728 *spec_cu);
17729
17730 if (spec_attr == NULL)
17731 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17732
17733 if (spec_attr == NULL)
17734 return NULL;
17735 else
17736 return follow_die_ref (die, spec_attr, spec_cu);
17737 }
17738
17739 /* A convenience function to find the proper .debug_line section for a CU. */
17740
17741 static struct dwarf2_section_info *
17742 get_debug_line_section (struct dwarf2_cu *cu)
17743 {
17744 struct dwarf2_section_info *section;
17745 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17746
17747 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17748 DWO file. */
17749 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17750 section = &cu->dwo_unit->dwo_file->sections.line;
17751 else if (cu->per_cu->is_dwz)
17752 {
17753 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17754
17755 section = &dwz->line;
17756 }
17757 else
17758 section = &per_objfile->per_bfd->line;
17759
17760 return section;
17761 }
17762
17763 /* Read the statement program header starting at OFFSET in
17764 .debug_line, or .debug_line.dwo. Return a pointer
17765 to a struct line_header, allocated using xmalloc.
17766 Returns NULL if there is a problem reading the header, e.g., if it
17767 has a version we don't understand.
17768
17769 NOTE: the strings in the include directory and file name tables of
17770 the returned object point into the dwarf line section buffer,
17771 and must not be freed. */
17772
17773 static line_header_up
17774 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
17775 const char *comp_dir)
17776 {
17777 struct dwarf2_section_info *section;
17778 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17779
17780 section = get_debug_line_section (cu);
17781 section->read (per_objfile->objfile);
17782 if (section->buffer == NULL)
17783 {
17784 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17785 complaint (_("missing .debug_line.dwo section"));
17786 else
17787 complaint (_("missing .debug_line section"));
17788 return 0;
17789 }
17790
17791 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
17792 per_objfile, section, &cu->header,
17793 comp_dir);
17794 }
17795
17796 /* Subroutine of dwarf_decode_lines to simplify it.
17797 Return the file name for the given file_entry.
17798 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
17799 If space for the result is malloc'd, *NAME_HOLDER will be set.
17800 Returns NULL if FILE_INDEX should be ignored, i.e., it is
17801 equivalent to CU_INFO. */
17802
17803 static const char *
17804 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
17805 const file_and_directory &cu_info,
17806 std::string &name_holder)
17807 {
17808 const char *include_name = fe.name;
17809 const char *include_name_to_compare = include_name;
17810
17811 const char *dir_name = fe.include_dir (lh);
17812
17813 std::string hold_compare;
17814 if (!IS_ABSOLUTE_PATH (include_name)
17815 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
17816 {
17817 /* Avoid creating a duplicate name for CU_INFO.
17818 We do this by comparing INCLUDE_NAME and CU_INFO.
17819 Before we do the comparison, however, we need to account
17820 for DIR_NAME and COMP_DIR.
17821 First prepend dir_name (if non-NULL). If we still don't
17822 have an absolute path prepend comp_dir (if non-NULL).
17823 However, the directory we record in the include-file's
17824 psymtab does not contain COMP_DIR (to match the
17825 corresponding symtab(s)).
17826
17827 Example:
17828
17829 bash$ cd /tmp
17830 bash$ gcc -g ./hello.c
17831 include_name = "hello.c"
17832 dir_name = "."
17833 DW_AT_comp_dir = comp_dir = "/tmp"
17834 DW_AT_name = "./hello.c"
17835
17836 */
17837
17838 if (dir_name != NULL)
17839 {
17840 name_holder = path_join (dir_name, include_name);
17841 include_name = name_holder.c_str ();
17842 include_name_to_compare = include_name;
17843 }
17844 if (!IS_ABSOLUTE_PATH (include_name)
17845 && cu_info.get_comp_dir () != nullptr)
17846 {
17847 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
17848 include_name_to_compare = hold_compare.c_str ();
17849 }
17850 }
17851
17852 std::string copied_name;
17853 const char *cu_filename = cu_info.get_name ();
17854 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
17855 {
17856 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
17857 cu_filename = copied_name.c_str ();
17858 }
17859
17860 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
17861 return nullptr;
17862 return include_name;
17863 }
17864
17865 /* State machine to track the state of the line number program. */
17866
17867 class lnp_state_machine
17868 {
17869 public:
17870 /* Initialize a machine state for the start of a line number
17871 program. */
17872 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
17873
17874 file_entry *current_file ()
17875 {
17876 /* lh->file_names is 0-based, but the file name numbers in the
17877 statement program are 1-based. */
17878 return m_line_header->file_name_at (m_file);
17879 }
17880
17881 /* Record the line in the state machine. END_SEQUENCE is true if
17882 we're processing the end of a sequence. */
17883 void record_line (bool end_sequence);
17884
17885 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
17886 nop-out rest of the lines in this sequence. */
17887 void check_line_address (struct dwarf2_cu *cu,
17888 const gdb_byte *line_ptr,
17889 unrelocated_addr unrelocated_lowpc,
17890 unrelocated_addr address);
17891
17892 void handle_set_discriminator (unsigned int discriminator)
17893 {
17894 m_discriminator = discriminator;
17895 m_line_has_non_zero_discriminator |= discriminator != 0;
17896 }
17897
17898 /* Handle DW_LNE_set_address. */
17899 void handle_set_address (unrelocated_addr address)
17900 {
17901 m_op_index = 0;
17902 m_address
17903 = (unrelocated_addr) gdbarch_adjust_dwarf2_line (m_gdbarch,
17904 (CORE_ADDR) address,
17905 false);
17906 }
17907
17908 /* Handle DW_LNS_advance_pc. */
17909 void handle_advance_pc (CORE_ADDR adjust);
17910
17911 /* Handle a special opcode. */
17912 void handle_special_opcode (unsigned char op_code);
17913
17914 /* Handle DW_LNS_advance_line. */
17915 void handle_advance_line (int line_delta)
17916 {
17917 advance_line (line_delta);
17918 }
17919
17920 /* Handle DW_LNS_set_file. */
17921 void handle_set_file (file_name_index file);
17922
17923 /* Handle DW_LNS_negate_stmt. */
17924 void handle_negate_stmt ()
17925 {
17926 m_flags ^= LEF_IS_STMT;
17927 }
17928
17929 /* Handle DW_LNS_const_add_pc. */
17930 void handle_const_add_pc ();
17931
17932 /* Handle DW_LNS_fixed_advance_pc. */
17933 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
17934 {
17935 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
17936 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
17937 m_op_index = 0;
17938 }
17939
17940 /* Handle DW_LNS_copy. */
17941 void handle_copy ()
17942 {
17943 record_line (false);
17944 m_discriminator = 0;
17945 m_flags &= ~LEF_PROLOGUE_END;
17946 m_flags &= ~LEF_EPILOGUE_BEGIN;
17947 }
17948
17949 /* Handle DW_LNE_end_sequence. */
17950 void handle_end_sequence ()
17951 {
17952 m_currently_recording_lines = true;
17953 }
17954
17955 /* Handle DW_LNS_set_prologue_end. */
17956 void handle_set_prologue_end ()
17957 {
17958 m_flags |= LEF_PROLOGUE_END;
17959 }
17960
17961 void handle_set_epilogue_begin ()
17962 {
17963 m_flags |= LEF_EPILOGUE_BEGIN;
17964 }
17965
17966 private:
17967 /* Advance the line by LINE_DELTA. */
17968 void advance_line (int line_delta)
17969 {
17970 m_line += line_delta;
17971
17972 if (line_delta != 0)
17973 m_line_has_non_zero_discriminator = m_discriminator != 0;
17974 }
17975
17976 struct dwarf2_cu *m_cu;
17977
17978 gdbarch *m_gdbarch;
17979
17980 /* The line number header. */
17981 line_header *m_line_header;
17982
17983 /* These are part of the standard DWARF line number state machine,
17984 and initialized according to the DWARF spec. */
17985
17986 unsigned char m_op_index = 0;
17987 /* The line table index of the current file. */
17988 file_name_index m_file = 1;
17989 unsigned int m_line = 1;
17990
17991 /* These are initialized in the constructor. */
17992
17993 unrelocated_addr m_address;
17994 linetable_entry_flags m_flags;
17995 unsigned int m_discriminator = 0;
17996
17997 /* Additional bits of state we need to track. */
17998
17999 /* The last file that we called dwarf2_start_subfile for.
18000 This is only used for TLLs. */
18001 unsigned int m_last_file = 0;
18002 /* The last file a line number was recorded for. */
18003 struct subfile *m_last_subfile = NULL;
18004
18005 /* The address of the last line entry. */
18006 unrelocated_addr m_last_address;
18007
18008 /* Set to true when a previous line at the same address (using
18009 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
18010 when a line entry at a new address (m_address different to
18011 m_last_address) is processed. */
18012 bool m_stmt_at_address = false;
18013
18014 /* When true, record the lines we decode. */
18015 bool m_currently_recording_lines = true;
18016
18017 /* The last line number that was recorded, used to coalesce
18018 consecutive entries for the same line. This can happen, for
18019 example, when discriminators are present. PR 17276. */
18020 unsigned int m_last_line = 0;
18021 bool m_line_has_non_zero_discriminator = false;
18022 };
18023
18024 void
18025 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18026 {
18027 CORE_ADDR addr_adj = (((m_op_index + adjust)
18028 / m_line_header->maximum_ops_per_instruction)
18029 * m_line_header->minimum_instruction_length);
18030 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18031 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
18032 m_op_index = ((m_op_index + adjust)
18033 % m_line_header->maximum_ops_per_instruction);
18034 }
18035
18036 void
18037 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18038 {
18039 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18040 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
18041 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
18042 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
18043 / m_line_header->maximum_ops_per_instruction)
18044 * m_line_header->minimum_instruction_length);
18045 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18046 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
18047 m_op_index = ((m_op_index + adj_opcode_d)
18048 % m_line_header->maximum_ops_per_instruction);
18049
18050 int line_delta = m_line_header->line_base + adj_opcode_r;
18051 advance_line (line_delta);
18052 record_line (false);
18053 m_discriminator = 0;
18054 m_flags &= ~LEF_PROLOGUE_END;
18055 m_flags &= ~LEF_EPILOGUE_BEGIN;
18056 }
18057
18058 void
18059 lnp_state_machine::handle_set_file (file_name_index file)
18060 {
18061 m_file = file;
18062
18063 const file_entry *fe = current_file ();
18064 if (fe == NULL)
18065 dwarf2_debug_line_missing_file_complaint ();
18066 else
18067 {
18068 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18069 m_line_has_non_zero_discriminator = m_discriminator != 0;
18070 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
18071 }
18072 }
18073
18074 void
18075 lnp_state_machine::handle_const_add_pc ()
18076 {
18077 CORE_ADDR adjust
18078 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18079
18080 CORE_ADDR addr_adj
18081 = (((m_op_index + adjust)
18082 / m_line_header->maximum_ops_per_instruction)
18083 * m_line_header->minimum_instruction_length);
18084
18085 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18086 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
18087 m_op_index = ((m_op_index + adjust)
18088 % m_line_header->maximum_ops_per_instruction);
18089 }
18090
18091 /* Return non-zero if we should add LINE to the line number table.
18092 LINE is the line to add, LAST_LINE is the last line that was added,
18093 LAST_SUBFILE is the subfile for LAST_LINE.
18094 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18095 had a non-zero discriminator.
18096
18097 We have to be careful in the presence of discriminators.
18098 E.g., for this line:
18099
18100 for (i = 0; i < 100000; i++);
18101
18102 clang can emit four line number entries for that one line,
18103 each with a different discriminator.
18104 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18105
18106 However, we want gdb to coalesce all four entries into one.
18107 Otherwise the user could stepi into the middle of the line and
18108 gdb would get confused about whether the pc really was in the
18109 middle of the line.
18110
18111 Things are further complicated by the fact that two consecutive
18112 line number entries for the same line is a heuristic used by gcc
18113 to denote the end of the prologue. So we can't just discard duplicate
18114 entries, we have to be selective about it. The heuristic we use is
18115 that we only collapse consecutive entries for the same line if at least
18116 one of those entries has a non-zero discriminator. PR 17276.
18117
18118 Note: Addresses in the line number state machine can never go backwards
18119 within one sequence, thus this coalescing is ok. */
18120
18121 static int
18122 dwarf_record_line_p (struct dwarf2_cu *cu,
18123 unsigned int line, unsigned int last_line,
18124 int line_has_non_zero_discriminator,
18125 struct subfile *last_subfile)
18126 {
18127 if (cu->get_builder ()->get_current_subfile () != last_subfile)
18128 return 1;
18129 if (line != last_line)
18130 return 1;
18131 /* Same line for the same file that we've seen already.
18132 As a last check, for pr 17276, only record the line if the line
18133 has never had a non-zero discriminator. */
18134 if (!line_has_non_zero_discriminator)
18135 return 1;
18136 return 0;
18137 }
18138
18139 /* Use the CU's builder to record line number LINE beginning at
18140 address ADDRESS in the line table of subfile SUBFILE. */
18141
18142 static void
18143 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18144 unsigned int line, unrelocated_addr address,
18145 linetable_entry_flags flags,
18146 struct dwarf2_cu *cu)
18147 {
18148 unrelocated_addr addr
18149 = unrelocated_addr (gdbarch_addr_bits_remove (gdbarch,
18150 (CORE_ADDR) address));
18151
18152 if (dwarf_line_debug)
18153 {
18154 gdb_printf (gdb_stdlog,
18155 "Recording line %u, file %s, address %s\n",
18156 line, lbasename (subfile->name.c_str ()),
18157 paddress (gdbarch, (CORE_ADDR) address));
18158 }
18159
18160 if (cu != nullptr)
18161 cu->get_builder ()->record_line (subfile, line, addr, flags);
18162 }
18163
18164 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18165 Mark the end of a set of line number records.
18166 The arguments are the same as for dwarf_record_line_1.
18167 If SUBFILE is NULL the request is ignored. */
18168
18169 static void
18170 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18171 unrelocated_addr address, struct dwarf2_cu *cu)
18172 {
18173 if (subfile == NULL)
18174 return;
18175
18176 if (dwarf_line_debug)
18177 {
18178 gdb_printf (gdb_stdlog,
18179 "Finishing current line, file %s, address %s\n",
18180 lbasename (subfile->name.c_str ()),
18181 paddress (gdbarch, (CORE_ADDR) address));
18182 }
18183
18184 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
18185 }
18186
18187 void
18188 lnp_state_machine::record_line (bool end_sequence)
18189 {
18190 if (dwarf_line_debug)
18191 {
18192 gdb_printf (gdb_stdlog,
18193 "Processing actual line %u: file %u,"
18194 " address %s, is_stmt %u, prologue_end %u,"
18195 " epilogue_begin %u, discrim %u%s\n",
18196 m_line, m_file,
18197 paddress (m_gdbarch, (CORE_ADDR) m_address),
18198 (m_flags & LEF_IS_STMT) != 0,
18199 (m_flags & LEF_PROLOGUE_END) != 0,
18200 (m_flags & LEF_EPILOGUE_BEGIN) != 0,
18201 m_discriminator,
18202 (end_sequence ? "\t(end sequence)" : ""));
18203 }
18204
18205 file_entry *fe = current_file ();
18206
18207 if (fe == NULL)
18208 dwarf2_debug_line_missing_file_complaint ();
18209 /* For now we ignore lines not starting on an instruction boundary.
18210 But not when processing end_sequence for compatibility with the
18211 previous version of the code. */
18212 else if (m_op_index == 0 || end_sequence)
18213 {
18214 /* When we switch files we insert an end maker in the first file,
18215 switch to the second file and add a new line entry. The
18216 problem is that the end marker inserted in the first file will
18217 discard any previous line entries at the same address. If the
18218 line entries in the first file are marked as is-stmt, while
18219 the new line in the second file is non-stmt, then this means
18220 the end marker will discard is-stmt lines so we can have a
18221 non-stmt line. This means that there are less addresses at
18222 which the user can insert a breakpoint.
18223
18224 To improve this we track the last address in m_last_address,
18225 and whether we have seen an is-stmt at this address. Then
18226 when switching files, if we have seen a stmt at the current
18227 address, and we are switching to create a non-stmt line, then
18228 discard the new line. */
18229 bool file_changed
18230 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
18231 bool ignore_this_line
18232 = ((file_changed && !end_sequence && m_last_address == m_address
18233 && ((m_flags & LEF_IS_STMT) == 0)
18234 && m_stmt_at_address)
18235 || (!end_sequence && m_line == 0));
18236
18237 if ((file_changed && !ignore_this_line) || end_sequence)
18238 {
18239 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
18240 m_currently_recording_lines ? m_cu : nullptr);
18241 }
18242
18243 if (!end_sequence && !ignore_this_line)
18244 {
18245 linetable_entry_flags lte_flags = m_flags;
18246 if (producer_is_codewarrior (m_cu))
18247 lte_flags |= LEF_IS_STMT;
18248
18249 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
18250 m_line_has_non_zero_discriminator,
18251 m_last_subfile))
18252 {
18253 buildsym_compunit *builder = m_cu->get_builder ();
18254 dwarf_record_line_1 (m_gdbarch,
18255 builder->get_current_subfile (),
18256 m_line, m_address, lte_flags,
18257 m_currently_recording_lines ? m_cu : nullptr);
18258 }
18259 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18260 m_last_line = m_line;
18261 }
18262 }
18263
18264 /* Track whether we have seen any IS_STMT true at m_address in case we
18265 have multiple line table entries all at m_address. */
18266 if (m_last_address != m_address)
18267 {
18268 m_stmt_at_address = false;
18269 m_last_address = m_address;
18270 }
18271 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
18272 }
18273
18274 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
18275 line_header *lh)
18276 : m_cu (cu),
18277 m_gdbarch (arch),
18278 m_line_header (lh),
18279 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
18280 if there was a line entry for it so that the backend has a
18281 chance to adjust it and also record it in case it needs it.
18282 This is currently used by MIPS code,
18283 cf. `mips_adjust_dwarf2_line'. */
18284 m_address ((unrelocated_addr) gdbarch_adjust_dwarf2_line (arch, 0, 0)),
18285 m_flags (lh->default_is_stmt ? LEF_IS_STMT : (linetable_entry_flags) 0),
18286 m_last_address (m_address)
18287 {
18288 }
18289
18290 void
18291 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18292 const gdb_byte *line_ptr,
18293 unrelocated_addr unrelocated_lowpc,
18294 unrelocated_addr address)
18295 {
18296 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
18297 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
18298 located at 0x0. In this case, additionally check that if
18299 ADDRESS < UNRELOCATED_LOWPC. */
18300
18301 if ((address == (unrelocated_addr) 0 && address < unrelocated_lowpc)
18302 || address == (unrelocated_addr) -1)
18303 {
18304 /* This line table is for a function which has been
18305 GCd by the linker. Ignore it. PR gdb/12528 */
18306
18307 struct objfile *objfile = cu->per_objfile->objfile;
18308 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18309
18310 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18311 line_offset, objfile_name (objfile));
18312 m_currently_recording_lines = false;
18313 /* Note: m_currently_recording_lines is left as false until we see
18314 DW_LNE_end_sequence. */
18315 }
18316 }
18317
18318 /* Subroutine of dwarf_decode_lines to simplify it.
18319 Process the line number information in LH. */
18320
18321 static void
18322 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18323 unrelocated_addr lowpc)
18324 {
18325 const gdb_byte *line_ptr, *extended_end;
18326 const gdb_byte *line_end;
18327 unsigned int bytes_read, extended_len;
18328 unsigned char op_code, extended_op;
18329 struct objfile *objfile = cu->per_objfile->objfile;
18330 bfd *abfd = objfile->obfd.get ();
18331 struct gdbarch *gdbarch = objfile->arch ();
18332
18333 line_ptr = lh->statement_program_start;
18334 line_end = lh->statement_program_end;
18335
18336 /* Read the statement sequences until there's nothing left. */
18337 while (line_ptr < line_end)
18338 {
18339 /* The DWARF line number program state machine. Reset the state
18340 machine at the start of each sequence. */
18341 lnp_state_machine state_machine (cu, gdbarch, lh);
18342 bool end_sequence = false;
18343
18344 /* Start a subfile for the current file of the state
18345 machine. */
18346 const file_entry *fe = state_machine.current_file ();
18347
18348 if (fe != NULL)
18349 dwarf2_start_subfile (cu, *fe, *lh);
18350
18351 /* Decode the table. */
18352 while (line_ptr < line_end && !end_sequence)
18353 {
18354 op_code = read_1_byte (abfd, line_ptr);
18355 line_ptr += 1;
18356
18357 if (op_code >= lh->opcode_base)
18358 {
18359 /* Special opcode. */
18360 state_machine.handle_special_opcode (op_code);
18361 }
18362 else switch (op_code)
18363 {
18364 case DW_LNS_extended_op:
18365 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18366 &bytes_read);
18367 line_ptr += bytes_read;
18368 extended_end = line_ptr + extended_len;
18369 extended_op = read_1_byte (abfd, line_ptr);
18370 line_ptr += 1;
18371 if (DW_LNE_lo_user <= extended_op
18372 && extended_op <= DW_LNE_hi_user)
18373 {
18374 /* Vendor extension, ignore. */
18375 line_ptr = extended_end;
18376 break;
18377 }
18378 switch (extended_op)
18379 {
18380 case DW_LNE_end_sequence:
18381 state_machine.handle_end_sequence ();
18382 end_sequence = true;
18383 break;
18384 case DW_LNE_set_address:
18385 {
18386 unrelocated_addr address
18387 = cu->header.read_address (abfd, line_ptr, &bytes_read);
18388 line_ptr += bytes_read;
18389
18390 state_machine.check_line_address (cu, line_ptr, lowpc,
18391 address);
18392 state_machine.handle_set_address (address);
18393 }
18394 break;
18395 case DW_LNE_define_file:
18396 {
18397 const char *cur_file;
18398 unsigned int mod_time, length;
18399 dir_index dindex;
18400
18401 cur_file = read_direct_string (abfd, line_ptr,
18402 &bytes_read);
18403 line_ptr += bytes_read;
18404 dindex = (dir_index)
18405 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18406 line_ptr += bytes_read;
18407 mod_time =
18408 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18409 line_ptr += bytes_read;
18410 length =
18411 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18412 line_ptr += bytes_read;
18413 lh->add_file_name (cur_file, dindex, mod_time, length);
18414 }
18415 break;
18416 case DW_LNE_set_discriminator:
18417 {
18418 /* The discriminator is not interesting to the
18419 debugger; just ignore it. We still need to
18420 check its value though:
18421 if there are consecutive entries for the same
18422 (non-prologue) line we want to coalesce them.
18423 PR 17276. */
18424 unsigned int discr
18425 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18426 line_ptr += bytes_read;
18427
18428 state_machine.handle_set_discriminator (discr);
18429 }
18430 break;
18431 default:
18432 complaint (_("mangled .debug_line section"));
18433 return;
18434 }
18435 /* Make sure that we parsed the extended op correctly. If e.g.
18436 we expected a different address size than the producer used,
18437 we may have read the wrong number of bytes. */
18438 if (line_ptr != extended_end)
18439 {
18440 complaint (_("mangled .debug_line section"));
18441 return;
18442 }
18443 break;
18444 case DW_LNS_copy:
18445 state_machine.handle_copy ();
18446 break;
18447 case DW_LNS_advance_pc:
18448 {
18449 CORE_ADDR adjust
18450 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18451 line_ptr += bytes_read;
18452
18453 state_machine.handle_advance_pc (adjust);
18454 }
18455 break;
18456 case DW_LNS_advance_line:
18457 {
18458 int line_delta
18459 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18460 line_ptr += bytes_read;
18461
18462 state_machine.handle_advance_line (line_delta);
18463 }
18464 break;
18465 case DW_LNS_set_file:
18466 {
18467 file_name_index file
18468 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18469 &bytes_read);
18470 line_ptr += bytes_read;
18471
18472 state_machine.handle_set_file (file);
18473 }
18474 break;
18475 case DW_LNS_set_column:
18476 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18477 line_ptr += bytes_read;
18478 break;
18479 case DW_LNS_negate_stmt:
18480 state_machine.handle_negate_stmt ();
18481 break;
18482 case DW_LNS_set_basic_block:
18483 break;
18484 /* Add to the address register of the state machine the
18485 address increment value corresponding to special opcode
18486 255. I.e., this value is scaled by the minimum
18487 instruction length since special opcode 255 would have
18488 scaled the increment. */
18489 case DW_LNS_const_add_pc:
18490 state_machine.handle_const_add_pc ();
18491 break;
18492 case DW_LNS_fixed_advance_pc:
18493 {
18494 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18495 line_ptr += 2;
18496
18497 state_machine.handle_fixed_advance_pc (addr_adj);
18498 }
18499 break;
18500 case DW_LNS_set_prologue_end:
18501 state_machine.handle_set_prologue_end ();
18502 break;
18503 case DW_LNS_set_epilogue_begin:
18504 state_machine.handle_set_epilogue_begin ();
18505 break;
18506 default:
18507 {
18508 /* Unknown standard opcode, ignore it. */
18509 int i;
18510
18511 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18512 {
18513 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18514 line_ptr += bytes_read;
18515 }
18516 }
18517 }
18518 }
18519
18520 if (!end_sequence)
18521 dwarf2_debug_line_missing_end_sequence_complaint ();
18522
18523 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18524 in which case we still finish recording the last line). */
18525 state_machine.record_line (true);
18526 }
18527 }
18528
18529 /* Decode the Line Number Program (LNP) for the given line_header
18530 structure and CU. The actual information extracted and the type
18531 of structures created from the LNP depends on the value of PST.
18532
18533 FND holds the CU file name and directory, if known.
18534 It is used for relative paths in the line table.
18535
18536 NOTE: It is important that psymtabs have the same file name (via
18537 strcmp) as the corresponding symtab. Since the directory is not
18538 used in the name of the symtab we don't use it in the name of the
18539 psymtabs we create. E.g. expand_line_sal requires this when
18540 finding psymtabs to expand. A good testcase for this is
18541 mb-inline.exp.
18542
18543 LOWPC is the lowest address in CU (or 0 if not known).
18544
18545 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18546 for its PC<->lines mapping information. Otherwise only the filename
18547 table is read in. */
18548
18549 static void
18550 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
18551 unrelocated_addr lowpc, int decode_mapping)
18552 {
18553 if (decode_mapping)
18554 dwarf_decode_lines_1 (lh, cu, lowpc);
18555
18556 /* Make sure a symtab is created for every file, even files
18557 which contain only variables (i.e. no code with associated
18558 line numbers). */
18559 buildsym_compunit *builder = cu->get_builder ();
18560 struct compunit_symtab *cust = builder->get_compunit_symtab ();
18561
18562 for (auto &fe : lh->file_names ())
18563 {
18564 dwarf2_start_subfile (cu, fe, *lh);
18565 subfile *sf = builder->get_current_subfile ();
18566
18567 if (sf->symtab == nullptr)
18568 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
18569 sf->name_for_id.c_str ());
18570
18571 fe.symtab = sf->symtab;
18572 }
18573 }
18574
18575 /* Start a subfile for DWARF. FILENAME is the name of the file and
18576 DIRNAME the name of the source directory which contains FILENAME
18577 or NULL if not known.
18578 This routine tries to keep line numbers from identical absolute and
18579 relative file names in a common subfile.
18580
18581 Using the `list' example from the GDB testsuite, which resides in
18582 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18583 of /srcdir/list0.c yields the following debugging information for list0.c:
18584
18585 DW_AT_name: /srcdir/list0.c
18586 DW_AT_comp_dir: /compdir
18587 files.files[0].name: list0.h
18588 files.files[0].dir: /srcdir
18589 files.files[1].name: list0.c
18590 files.files[1].dir: /srcdir
18591
18592 The line number information for list0.c has to end up in a single
18593 subfile, so that `break /srcdir/list0.c:1' works as expected.
18594 start_subfile will ensure that this happens provided that we pass the
18595 concatenation of files.files[1].dir and files.files[1].name as the
18596 subfile's name. */
18597
18598 static void
18599 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
18600 const line_header &lh)
18601 {
18602 std::string filename_holder;
18603 const char *filename = fe.name;
18604 const char *dirname = lh.include_dir_at (fe.d_index);
18605
18606 /* In order not to lose the line information directory,
18607 we concatenate it to the filename when it makes sense.
18608 Note that the Dwarf3 standard says (speaking of filenames in line
18609 information): ``The directory index is ignored for file names
18610 that represent full path names''. Thus ignoring dirname in the
18611 `else' branch below isn't an issue. */
18612
18613 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18614 {
18615 filename_holder = path_join (dirname, filename);
18616 filename = filename_holder.c_str ();
18617 }
18618
18619 std::string filename_for_id = lh.file_file_name (fe);
18620 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
18621 }
18622
18623 static void
18624 var_decode_location (struct attribute *attr, struct symbol *sym,
18625 struct dwarf2_cu *cu)
18626 {
18627 struct objfile *objfile = cu->per_objfile->objfile;
18628 struct comp_unit_head *cu_header = &cu->header;
18629
18630 /* NOTE drow/2003-01-30: There used to be a comment and some special
18631 code here to turn a symbol with DW_AT_external and a
18632 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18633 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18634 with some versions of binutils) where shared libraries could have
18635 relocations against symbols in their debug information - the
18636 minimal symbol would have the right address, but the debug info
18637 would not. It's no longer necessary, because we will explicitly
18638 apply relocations when we read in the debug information now. */
18639
18640 /* A DW_AT_location attribute with no contents indicates that a
18641 variable has been optimized away. */
18642 if (attr->form_is_block () && attr->as_block ()->size == 0)
18643 {
18644 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18645 return;
18646 }
18647
18648 /* Handle one degenerate form of location expression specially, to
18649 preserve GDB's previous behavior when section offsets are
18650 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
18651 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
18652
18653 if (attr->form_is_block ())
18654 {
18655 struct dwarf_block *block = attr->as_block ();
18656
18657 if ((block->data[0] == DW_OP_addr
18658 && block->size == 1 + cu_header->addr_size)
18659 || ((block->data[0] == DW_OP_GNU_addr_index
18660 || block->data[0] == DW_OP_addrx)
18661 && (block->size
18662 == 1 + leb128_size (&block->data[1]))))
18663 {
18664 unsigned int dummy;
18665
18666 unrelocated_addr tem;
18667 if (block->data[0] == DW_OP_addr)
18668 tem = cu->header.read_address (objfile->obfd.get (),
18669 block->data + 1,
18670 &dummy);
18671 else
18672 tem = read_addr_index_from_leb128 (cu, block->data + 1, &dummy);
18673 sym->set_value_address ((CORE_ADDR) tem);
18674 sym->set_aclass_index (LOC_STATIC);
18675 fixup_symbol_section (sym, objfile);
18676 sym->set_value_address
18677 (sym->value_address ()
18678 + objfile->section_offsets[sym->section_index ()]);
18679 return;
18680 }
18681 }
18682
18683 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18684 expression evaluator, and use LOC_COMPUTED only when necessary
18685 (i.e. when the value of a register or memory location is
18686 referenced, or a thread-local block, etc.). Then again, it might
18687 not be worthwhile. I'm assuming that it isn't unless performance
18688 or memory numbers show me otherwise. */
18689
18690 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18691
18692 if (sym->computed_ops ()->location_has_loclist)
18693 cu->has_loclist = true;
18694 }
18695
18696 /* A helper function to add an "export" symbol. The new symbol starts
18697 as a clone of ORIG, but is modified to defer to the symbol named
18698 ORIG_NAME. The original symbol uses the name given in the source
18699 code, and the symbol that is created here uses the linkage name as
18700 its name. See ada-imported.c. */
18701
18702 static void
18703 add_ada_export_symbol (struct symbol *orig, const char *new_name,
18704 const char *orig_name, struct dwarf2_cu *cu,
18705 struct pending **list_to_add)
18706 {
18707 struct symbol *copy
18708 = new (&cu->per_objfile->objfile->objfile_obstack) symbol (*orig);
18709 copy->set_linkage_name (new_name);
18710 SYMBOL_LOCATION_BATON (copy) = (void *) orig_name;
18711 copy->set_aclass_index (copy->aclass () == LOC_BLOCK
18712 ? ada_block_index
18713 : ada_imported_index);
18714 add_symbol_to_list (copy, list_to_add);
18715 }
18716
18717 /* A helper function that decides if a given symbol is an Ada Pragma
18718 Import or Pragma Export. */
18719
18720 static bool
18721 is_ada_import_or_export (dwarf2_cu *cu, const char *name,
18722 const char *linkagename)
18723 {
18724 return (cu->lang () == language_ada
18725 && linkagename != nullptr
18726 && !streq (name, linkagename)
18727 /* The following exclusions are necessary because symbols
18728 with names or linkage names that match here will meet the
18729 other criteria but are not in fact caused by Pragma
18730 Import or Pragma Export, and applying the import/export
18731 treatment to them will introduce problems. Some of these
18732 checks only apply to functions, but it is simpler and
18733 harmless to always do them all. */
18734 && !startswith (name, "__builtin")
18735 && !startswith (linkagename, "___ghost_")
18736 && !startswith (linkagename, "__gnat")
18737 && !startswith (linkagename, "_ada_")
18738 && !streq (linkagename, "adainit"));
18739 }
18740
18741 /* Given a pointer to a DWARF information entry, figure out if we need
18742 to make a symbol table entry for it, and if so, create a new entry
18743 and return a pointer to it.
18744 If TYPE is NULL, determine symbol type from the die, otherwise
18745 used the passed type.
18746 If SPACE is not NULL, use it to hold the new symbol. If it is
18747 NULL, allocate a new symbol on the objfile's obstack. */
18748
18749 static struct symbol *
18750 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18751 struct symbol *space)
18752 {
18753 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18754 struct objfile *objfile = per_objfile->objfile;
18755 struct symbol *sym = NULL;
18756 const char *name;
18757 struct attribute *attr = NULL;
18758 struct attribute *attr2 = NULL;
18759 struct pending **list_to_add = NULL;
18760
18761 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18762
18763 name = dwarf2_name (die, cu);
18764 if (name == nullptr && (die->tag == DW_TAG_subprogram
18765 || die->tag == DW_TAG_inlined_subroutine
18766 || die->tag == DW_TAG_entry_point))
18767 name = dw2_linkage_name (die, cu);
18768
18769 if (name)
18770 {
18771 int suppress_add = 0;
18772
18773 if (space)
18774 sym = space;
18775 else
18776 sym = new (&objfile->objfile_obstack) symbol;
18777 OBJSTAT (objfile, n_syms++);
18778
18779 /* Cache this symbol's name and the name's demangled form (if any). */
18780 sym->set_language (cu->lang (), &objfile->objfile_obstack);
18781 /* Fortran does not have mangling standard and the mangling does differ
18782 between gfortran, iFort etc. */
18783 const char *physname
18784 = (cu->lang () == language_fortran
18785 ? dwarf2_full_name (name, die, cu)
18786 : dwarf2_physname (name, die, cu));
18787 const char *linkagename = dw2_linkage_name (die, cu);
18788
18789 if (linkagename == nullptr || cu->lang () == language_ada)
18790 sym->set_linkage_name (physname);
18791 else
18792 {
18793 sym->set_demangled_name (physname, &objfile->objfile_obstack);
18794 sym->set_linkage_name (linkagename);
18795 }
18796
18797 /* Handle DW_AT_artificial. */
18798 attr = dwarf2_attr (die, DW_AT_artificial, cu);
18799 if (attr != nullptr)
18800 sym->set_is_artificial (attr->as_boolean ());
18801
18802 /* Default assumptions.
18803 Use the passed type or decode it from the die. */
18804 sym->set_domain (UNDEF_DOMAIN);
18805 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18806 if (type != NULL)
18807 sym->set_type (type);
18808 else
18809 sym->set_type (die_type (die, cu));
18810 attr = dwarf2_attr (die,
18811 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18812 cu);
18813 if (attr != nullptr)
18814 sym->set_line (attr->constant_value (0));
18815
18816 attr = dwarf2_attr (die,
18817 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18818 cu);
18819 if (attr != nullptr && attr->is_nonnegative ())
18820 {
18821 file_name_index file_index
18822 = (file_name_index) attr->as_nonnegative ();
18823 struct file_entry *fe;
18824
18825 if (cu->line_header != NULL)
18826 fe = cu->line_header->file_name_at (file_index);
18827 else
18828 fe = NULL;
18829
18830 if (fe == NULL)
18831 complaint (_("file index out of range"));
18832 else
18833 sym->set_symtab (fe->symtab);
18834 }
18835
18836 switch (die->tag)
18837 {
18838 case DW_TAG_label:
18839 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18840 if (attr != nullptr)
18841 {
18842 CORE_ADDR addr = per_objfile->relocate (attr->as_address ());
18843 sym->set_section_index (SECT_OFF_TEXT (objfile));
18844 sym->set_value_address (addr);
18845 sym->set_aclass_index (LOC_LABEL);
18846 }
18847 else
18848 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18849 sym->set_type (builtin_type (objfile)->builtin_core_addr);
18850 sym->set_domain (LABEL_DOMAIN);
18851 list_to_add = cu->list_in_scope;
18852 break;
18853 case DW_TAG_entry_point:
18854 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18855 finish_block. */
18856 sym->set_domain (FUNCTION_DOMAIN);
18857 sym->set_aclass_index (LOC_BLOCK);
18858 /* DW_TAG_entry_point provides an additional entry_point to an
18859 existing sub_program. Therefore, we inherit the "external"
18860 attribute from the sub_program to which the entry_point
18861 belongs to. */
18862 attr2 = dwarf2_attr (die->parent, DW_AT_external, cu);
18863 if (attr2 != nullptr && attr2->as_boolean ())
18864 list_to_add = cu->get_builder ()->get_global_symbols ();
18865 else
18866 list_to_add = cu->list_in_scope;
18867 break;
18868 case DW_TAG_subprogram:
18869 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18870 finish_block. */
18871 sym->set_domain (FUNCTION_DOMAIN);
18872 sym->set_aclass_index (LOC_BLOCK);
18873 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18874 if ((attr2 != nullptr && attr2->as_boolean ())
18875 || cu->lang () == language_ada
18876 || cu->lang () == language_fortran)
18877 {
18878 /* Subprograms marked external are stored as a global symbol.
18879 Ada and Fortran subprograms, whether marked external or
18880 not, are always stored as a global symbol, because we want
18881 to be able to access them globally. For instance, we want
18882 to be able to break on a nested subprogram without having
18883 to specify the context. */
18884 list_to_add = cu->get_builder ()->get_global_symbols ();
18885 }
18886 else
18887 {
18888 list_to_add = cu->list_in_scope;
18889 }
18890
18891 if (is_ada_import_or_export (cu, name, linkagename))
18892 {
18893 /* This is either a Pragma Import or Export. They can
18894 be distinguished by the declaration flag. */
18895 sym->set_linkage_name (name);
18896 if (die_is_declaration (die, cu))
18897 {
18898 /* For Import, create a symbol using the source
18899 name, and have it refer to the linkage name. */
18900 SYMBOL_LOCATION_BATON (sym) = (void *) linkagename;
18901 sym->set_aclass_index (ada_block_index);
18902 }
18903 else
18904 {
18905 /* For Export, create a symbol using the source
18906 name, then create a second symbol that refers
18907 back to it. */
18908 add_ada_export_symbol (sym, linkagename, name, cu,
18909 list_to_add);
18910 }
18911 }
18912 break;
18913 case DW_TAG_inlined_subroutine:
18914 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18915 finish_block. */
18916 sym->set_domain (FUNCTION_DOMAIN);
18917 sym->set_aclass_index (LOC_BLOCK);
18918 sym->set_is_inlined (1);
18919 list_to_add = cu->list_in_scope;
18920 break;
18921 case DW_TAG_template_value_param:
18922 suppress_add = 1;
18923 [[fallthrough]];
18924 case DW_TAG_constant:
18925 case DW_TAG_variable:
18926 case DW_TAG_member:
18927 sym->set_domain (VAR_DOMAIN);
18928 /* Compilation with minimal debug info may result in
18929 variables with missing type entries. Change the
18930 misleading `void' type to something sensible. */
18931 if (sym->type ()->code () == TYPE_CODE_VOID)
18932 sym->set_type (builtin_type (objfile)->builtin_int);
18933
18934 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18935 /* In the case of DW_TAG_member, we should only be called for
18936 static const members. */
18937 if (die->tag == DW_TAG_member)
18938 {
18939 /* dwarf2_add_field uses die_is_declaration,
18940 so we do the same. */
18941 gdb_assert (die_is_declaration (die, cu));
18942 gdb_assert (attr);
18943 }
18944 if (attr != nullptr)
18945 {
18946 dwarf2_const_value (attr, sym, cu);
18947 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18948 if (!suppress_add)
18949 {
18950 if (attr2 != nullptr && attr2->as_boolean ())
18951 list_to_add = cu->get_builder ()->get_global_symbols ();
18952 else
18953 list_to_add = cu->list_in_scope;
18954 }
18955 break;
18956 }
18957 attr = dwarf2_attr (die, DW_AT_location, cu);
18958 if (attr != nullptr)
18959 {
18960 var_decode_location (attr, sym, cu);
18961 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18962
18963 /* Fortran explicitly imports any global symbols to the local
18964 scope by DW_TAG_common_block. */
18965 if (cu->lang () == language_fortran && die->parent
18966 && die->parent->tag == DW_TAG_common_block)
18967 attr2 = NULL;
18968
18969 if (sym->aclass () == LOC_STATIC
18970 && sym->value_address () == 0
18971 && !per_objfile->per_bfd->has_section_at_zero)
18972 {
18973 /* When a static variable is eliminated by the linker,
18974 the corresponding debug information is not stripped
18975 out, but the variable address is set to null;
18976 do not add such variables into symbol table. */
18977 }
18978 else if (attr2 != nullptr && attr2->as_boolean ())
18979 {
18980 if (sym->aclass () == LOC_STATIC
18981 && (objfile->flags & OBJF_MAINLINE) == 0
18982 && per_objfile->per_bfd->can_copy)
18983 {
18984 /* A global static variable might be subject to
18985 copy relocation. We first check for a local
18986 minsym, though, because maybe the symbol was
18987 marked hidden, in which case this would not
18988 apply. */
18989 bound_minimal_symbol found
18990 = (lookup_minimal_symbol_linkage
18991 (sym->linkage_name (), objfile));
18992 if (found.minsym != nullptr)
18993 sym->maybe_copied = 1;
18994 }
18995
18996 /* A variable with DW_AT_external is never static,
18997 but it may be block-scoped. */
18998 list_to_add
18999 = ((cu->list_in_scope
19000 == cu->get_builder ()->get_file_symbols ())
19001 ? cu->get_builder ()->get_global_symbols ()
19002 : cu->list_in_scope);
19003 }
19004 else
19005 list_to_add = cu->list_in_scope;
19006
19007 if (list_to_add != nullptr
19008 && is_ada_import_or_export (cu, name, linkagename))
19009 {
19010 /* This is a Pragma Export. A Pragma Import won't
19011 be seen here, because it will not have a location
19012 and so will be handled below. */
19013 add_ada_export_symbol (sym, name, linkagename, cu,
19014 list_to_add);
19015 }
19016 }
19017 else
19018 {
19019 /* We do not know the address of this symbol.
19020 If it is an external symbol and we have type information
19021 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19022 The address of the variable will then be determined from
19023 the minimal symbol table whenever the variable is
19024 referenced. */
19025 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19026
19027 /* Fortran explicitly imports any global symbols to the local
19028 scope by DW_TAG_common_block. */
19029 if (cu->lang () == language_fortran && die->parent
19030 && die->parent->tag == DW_TAG_common_block)
19031 {
19032 /* SYMBOL_CLASS doesn't matter here because
19033 read_common_block is going to reset it. */
19034 if (!suppress_add)
19035 list_to_add = cu->list_in_scope;
19036 }
19037 else if (is_ada_import_or_export (cu, name, linkagename))
19038 {
19039 /* This is a Pragma Import. A Pragma Export won't
19040 be seen here, because it will have a location and
19041 so will be handled above. */
19042 sym->set_linkage_name (name);
19043 list_to_add = cu->list_in_scope;
19044 SYMBOL_LOCATION_BATON (sym) = (void *) linkagename;
19045 sym->set_aclass_index (ada_imported_index);
19046 }
19047 else if (attr2 != nullptr && attr2->as_boolean ()
19048 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19049 {
19050 /* A variable with DW_AT_external is never static, but it
19051 may be block-scoped. */
19052 list_to_add
19053 = ((cu->list_in_scope
19054 == cu->get_builder ()->get_file_symbols ())
19055 ? cu->get_builder ()->get_global_symbols ()
19056 : cu->list_in_scope);
19057
19058 sym->set_aclass_index (LOC_UNRESOLVED);
19059 }
19060 else if (!die_is_declaration (die, cu))
19061 {
19062 /* Use the default LOC_OPTIMIZED_OUT class. */
19063 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
19064 if (!suppress_add)
19065 list_to_add = cu->list_in_scope;
19066 }
19067 }
19068 break;
19069 case DW_TAG_formal_parameter:
19070 {
19071 /* If we are inside a function, mark this as an argument. If
19072 not, we might be looking at an argument to an inlined function
19073 when we do not have enough information to show inlined frames;
19074 pretend it's a local variable in that case so that the user can
19075 still see it. */
19076 sym->set_domain (VAR_DOMAIN);
19077 struct context_stack *curr
19078 = cu->get_builder ()->get_current_context_stack ();
19079 if (curr != nullptr && curr->name != nullptr)
19080 sym->set_is_argument (1);
19081 attr = dwarf2_attr (die, DW_AT_location, cu);
19082 if (attr != nullptr)
19083 {
19084 var_decode_location (attr, sym, cu);
19085 }
19086 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19087 if (attr != nullptr)
19088 {
19089 dwarf2_const_value (attr, sym, cu);
19090 }
19091
19092 list_to_add = cu->list_in_scope;
19093 }
19094 break;
19095 case DW_TAG_unspecified_parameters:
19096 /* From varargs functions; gdb doesn't seem to have any
19097 interest in this information, so just ignore it for now.
19098 (FIXME?) */
19099 break;
19100 case DW_TAG_template_type_param:
19101 suppress_add = 1;
19102 [[fallthrough]];
19103 case DW_TAG_class_type:
19104 case DW_TAG_interface_type:
19105 case DW_TAG_structure_type:
19106 case DW_TAG_union_type:
19107 case DW_TAG_set_type:
19108 case DW_TAG_enumeration_type:
19109 case DW_TAG_namelist:
19110 if (die->tag == DW_TAG_namelist)
19111 {
19112 sym->set_aclass_index (LOC_STATIC);
19113 sym->set_domain (VAR_DOMAIN);
19114 }
19115 else if (cu->lang () == language_c
19116 || cu->lang () == language_cplus
19117 || cu->lang () == language_objc
19118 || cu->lang () == language_opencl
19119 || cu->lang () == language_minimal)
19120 {
19121 /* These languages have a tag namespace. Note that
19122 there's a special hack for C++ in the matching code,
19123 so we don't need to enter a separate typedef for the
19124 tag. */
19125 sym->set_aclass_index (LOC_TYPEDEF);
19126 sym->set_domain (STRUCT_DOMAIN);
19127 }
19128 else
19129 {
19130 /* Other languages don't have a tag namespace. */
19131 sym->set_aclass_index (LOC_TYPEDEF);
19132 sym->set_domain (TYPE_DOMAIN);
19133 }
19134
19135 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19136 really ever be static objects: otherwise, if you try
19137 to, say, break of a class's method and you're in a file
19138 which doesn't mention that class, it won't work unless
19139 the check for all static symbols in lookup_symbol_aux
19140 saves you. See the OtherFileClass tests in
19141 gdb.c++/namespace.exp. */
19142
19143 if (!suppress_add)
19144 {
19145 buildsym_compunit *builder = cu->get_builder ();
19146 list_to_add
19147 = (cu->list_in_scope == builder->get_file_symbols ()
19148 && cu->lang () == language_cplus
19149 ? builder->get_global_symbols ()
19150 : cu->list_in_scope);
19151
19152 /* The semantics of C++ state that "struct foo {
19153 ... }" also defines a typedef for "foo". */
19154 if (cu->lang () == language_cplus
19155 || cu->lang () == language_ada
19156 || cu->lang () == language_d
19157 || cu->lang () == language_rust)
19158 {
19159 /* The symbol's name is already allocated along
19160 with this objfile, so we don't need to
19161 duplicate it for the type. */
19162 if (sym->type ()->name () == 0)
19163 sym->type ()->set_name (sym->search_name ());
19164 }
19165 }
19166 break;
19167 case DW_TAG_unspecified_type:
19168 if (cu->lang () == language_ada)
19169 break;
19170 [[fallthrough]];
19171 case DW_TAG_typedef:
19172 case DW_TAG_array_type:
19173 case DW_TAG_base_type:
19174 case DW_TAG_subrange_type:
19175 case DW_TAG_generic_subrange:
19176 sym->set_aclass_index (LOC_TYPEDEF);
19177 sym->set_domain (TYPE_DOMAIN);
19178 list_to_add = cu->list_in_scope;
19179 break;
19180 case DW_TAG_enumerator:
19181 sym->set_domain (VAR_DOMAIN);
19182 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19183 if (attr != nullptr)
19184 {
19185 dwarf2_const_value (attr, sym, cu);
19186 }
19187
19188 /* NOTE: carlton/2003-11-10: See comment above in the
19189 DW_TAG_class_type, etc. block. */
19190
19191 list_to_add
19192 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
19193 && cu->lang () == language_cplus
19194 ? cu->get_builder ()->get_global_symbols ()
19195 : cu->list_in_scope);
19196 break;
19197 case DW_TAG_imported_declaration:
19198 case DW_TAG_namespace:
19199 sym->set_domain (TYPE_DOMAIN);
19200 sym->set_aclass_index (LOC_TYPEDEF);
19201 list_to_add = cu->get_builder ()->get_global_symbols ();
19202 break;
19203 case DW_TAG_module:
19204 sym->set_aclass_index (LOC_TYPEDEF);
19205 sym->set_domain (MODULE_DOMAIN);
19206 list_to_add = cu->get_builder ()->get_global_symbols ();
19207 break;
19208 case DW_TAG_common_block:
19209 sym->set_aclass_index (LOC_COMMON_BLOCK);
19210 sym->set_domain (COMMON_BLOCK_DOMAIN);
19211 list_to_add = cu->list_in_scope;
19212 break;
19213 default:
19214 /* Not a tag we recognize. Hopefully we aren't processing
19215 trash data, but since we must specifically ignore things
19216 we don't recognize, there is nothing else we should do at
19217 this point. */
19218 complaint (_("unsupported tag: '%s'"),
19219 dwarf_tag_name (die->tag));
19220 break;
19221 }
19222
19223 if (suppress_add)
19224 {
19225 sym->hash_next = objfile->template_symbols;
19226 objfile->template_symbols = sym;
19227 list_to_add = NULL;
19228 }
19229
19230 if (list_to_add != NULL)
19231 add_symbol_to_list (sym, list_to_add);
19232
19233 /* For the benefit of old versions of GCC, check for anonymous
19234 namespaces based on the demangled name. */
19235 if (!cu->processing_has_namespace_info
19236 && cu->lang () == language_cplus)
19237 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
19238 }
19239 return (sym);
19240 }
19241
19242 /* Given an attr with a DW_FORM_dataN value in host byte order,
19243 zero-extend it as appropriate for the symbol's type. The DWARF
19244 standard (v4) is not entirely clear about the meaning of using
19245 DW_FORM_dataN for a constant with a signed type, where the type is
19246 wider than the data. The conclusion of a discussion on the DWARF
19247 list was that this is unspecified. We choose to always zero-extend
19248 because that is the interpretation long in use by GCC. */
19249
19250 static gdb_byte *
19251 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19252 struct dwarf2_cu *cu, LONGEST *value, int bits)
19253 {
19254 struct objfile *objfile = cu->per_objfile->objfile;
19255 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
19256 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19257 LONGEST l = attr->constant_value (0);
19258
19259 if (bits < sizeof (*value) * 8)
19260 {
19261 l &= ((LONGEST) 1 << bits) - 1;
19262 *value = l;
19263 }
19264 else if (bits == sizeof (*value) * 8)
19265 *value = l;
19266 else
19267 {
19268 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19269 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19270 return bytes;
19271 }
19272
19273 return NULL;
19274 }
19275
19276 /* Read a constant value from an attribute. Either set *VALUE, or if
19277 the value does not fit in *VALUE, set *BYTES - either already
19278 allocated on the objfile obstack, or newly allocated on OBSTACK,
19279 or, set *BATON, if we translated the constant to a location
19280 expression. */
19281
19282 static void
19283 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19284 const char *name, struct obstack *obstack,
19285 struct dwarf2_cu *cu,
19286 LONGEST *value, const gdb_byte **bytes,
19287 struct dwarf2_locexpr_baton **baton)
19288 {
19289 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19290 struct objfile *objfile = per_objfile->objfile;
19291 struct comp_unit_head *cu_header = &cu->header;
19292 struct dwarf_block *blk;
19293 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
19294 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19295
19296 *value = 0;
19297 *bytes = NULL;
19298 *baton = NULL;
19299
19300 switch (attr->form)
19301 {
19302 case DW_FORM_addr:
19303 case DW_FORM_addrx:
19304 case DW_FORM_GNU_addr_index:
19305 {
19306 gdb_byte *data;
19307
19308 if (type->length () != cu_header->addr_size)
19309 dwarf2_const_value_length_mismatch_complaint (name,
19310 cu_header->addr_size,
19311 type->length ());
19312 /* Symbols of this form are reasonably rare, so we just
19313 piggyback on the existing location code rather than writing
19314 a new implementation of symbol_computed_ops. */
19315 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19316 (*baton)->per_objfile = per_objfile;
19317 (*baton)->per_cu = cu->per_cu;
19318 gdb_assert ((*baton)->per_cu);
19319
19320 (*baton)->size = 2 + cu_header->addr_size;
19321 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19322 (*baton)->data = data;
19323
19324 data[0] = DW_OP_addr;
19325 store_unsigned_integer (&data[1], cu_header->addr_size,
19326 byte_order, (ULONGEST) attr->as_address ());
19327 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19328 }
19329 break;
19330 case DW_FORM_string:
19331 case DW_FORM_strp:
19332 case DW_FORM_strx:
19333 case DW_FORM_GNU_str_index:
19334 case DW_FORM_GNU_strp_alt:
19335 /* The string is already allocated on the objfile obstack, point
19336 directly to it. */
19337 *bytes = (const gdb_byte *) attr->as_string ();
19338 break;
19339 case DW_FORM_block1:
19340 case DW_FORM_block2:
19341 case DW_FORM_block4:
19342 case DW_FORM_block:
19343 case DW_FORM_exprloc:
19344 case DW_FORM_data16:
19345 blk = attr->as_block ();
19346 if (type->length () != blk->size)
19347 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19348 type->length ());
19349 *bytes = blk->data;
19350 break;
19351
19352 /* The DW_AT_const_value attributes are supposed to carry the
19353 symbol's value "represented as it would be on the target
19354 architecture." By the time we get here, it's already been
19355 converted to host endianness, so we just need to sign- or
19356 zero-extend it as appropriate. */
19357 case DW_FORM_data1:
19358 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19359 break;
19360 case DW_FORM_data2:
19361 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19362 break;
19363 case DW_FORM_data4:
19364 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19365 break;
19366 case DW_FORM_data8:
19367 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19368 break;
19369
19370 case DW_FORM_sdata:
19371 case DW_FORM_implicit_const:
19372 *value = attr->as_signed ();
19373 break;
19374
19375 case DW_FORM_udata:
19376 *value = attr->as_unsigned ();
19377 break;
19378
19379 default:
19380 complaint (_("unsupported const value attribute form: '%s'"),
19381 dwarf_form_name (attr->form));
19382 *value = 0;
19383 break;
19384 }
19385 }
19386
19387
19388 /* Copy constant value from an attribute to a symbol. */
19389
19390 static void
19391 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19392 struct dwarf2_cu *cu)
19393 {
19394 struct objfile *objfile = cu->per_objfile->objfile;
19395 LONGEST value;
19396 const gdb_byte *bytes;
19397 struct dwarf2_locexpr_baton *baton;
19398
19399 dwarf2_const_value_attr (attr, sym->type (),
19400 sym->print_name (),
19401 &objfile->objfile_obstack, cu,
19402 &value, &bytes, &baton);
19403
19404 if (baton != NULL)
19405 {
19406 SYMBOL_LOCATION_BATON (sym) = baton;
19407 sym->set_aclass_index (dwarf2_locexpr_index);
19408 }
19409 else if (bytes != NULL)
19410 {
19411 sym->set_value_bytes (bytes);
19412 sym->set_aclass_index (LOC_CONST_BYTES);
19413 }
19414 else
19415 {
19416 sym->set_value_longest (value);
19417 sym->set_aclass_index (LOC_CONST);
19418 }
19419 }
19420
19421 /* Return the type of the die in question using its DW_AT_type attribute. */
19422
19423 static struct type *
19424 die_type (struct die_info *die, struct dwarf2_cu *cu)
19425 {
19426 struct attribute *type_attr;
19427
19428 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19429 if (!type_attr)
19430 {
19431 struct objfile *objfile = cu->per_objfile->objfile;
19432 /* A missing DW_AT_type represents a void type. */
19433 return builtin_type (objfile)->builtin_void;
19434 }
19435
19436 return lookup_die_type (die, type_attr, cu);
19437 }
19438
19439 /* True iff CU's producer generates GNAT Ada auxiliary information
19440 that allows to find parallel types through that information instead
19441 of having to do expensive parallel lookups by type name. */
19442
19443 static int
19444 need_gnat_info (struct dwarf2_cu *cu)
19445 {
19446 /* Assume that the Ada compiler was GNAT, which always produces
19447 the auxiliary information. */
19448 return (cu->lang () == language_ada);
19449 }
19450
19451 /* Return the auxiliary type of the die in question using its
19452 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19453 attribute is not present. */
19454
19455 static struct type *
19456 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19457 {
19458 struct attribute *type_attr;
19459
19460 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19461 if (!type_attr)
19462 return NULL;
19463
19464 return lookup_die_type (die, type_attr, cu);
19465 }
19466
19467 /* If DIE has a descriptive_type attribute, then set the TYPE's
19468 descriptive type accordingly. */
19469
19470 static void
19471 set_descriptive_type (struct type *type, struct die_info *die,
19472 struct dwarf2_cu *cu)
19473 {
19474 struct type *descriptive_type = die_descriptive_type (die, cu);
19475
19476 if (descriptive_type)
19477 {
19478 ALLOCATE_GNAT_AUX_TYPE (type);
19479 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19480 }
19481 }
19482
19483 /* Return the containing type of the die in question using its
19484 DW_AT_containing_type attribute. */
19485
19486 static struct type *
19487 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19488 {
19489 struct attribute *type_attr;
19490 struct objfile *objfile = cu->per_objfile->objfile;
19491
19492 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19493 if (!type_attr)
19494 error (_("Dwarf Error: Problem turning containing type into gdb type "
19495 "[in module %s]"), objfile_name (objfile));
19496
19497 return lookup_die_type (die, type_attr, cu);
19498 }
19499
19500 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19501
19502 static struct type *
19503 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19504 {
19505 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19506 struct objfile *objfile = per_objfile->objfile;
19507 char *saved;
19508
19509 std::string message
19510 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
19511 objfile_name (objfile),
19512 sect_offset_str (cu->header.sect_off),
19513 sect_offset_str (die->sect_off));
19514 saved = obstack_strdup (&objfile->objfile_obstack, message);
19515
19516 return type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_ERROR,
19517 0, saved);
19518 }
19519
19520 /* Look up the type of DIE in CU using its type attribute ATTR.
19521 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19522 DW_AT_containing_type.
19523 If there is no type substitute an error marker. */
19524
19525 static struct type *
19526 lookup_die_type (struct die_info *die, const struct attribute *attr,
19527 struct dwarf2_cu *cu)
19528 {
19529 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19530 struct objfile *objfile = per_objfile->objfile;
19531 struct type *this_type;
19532
19533 gdb_assert (attr->name == DW_AT_type
19534 || attr->name == DW_AT_GNAT_descriptive_type
19535 || attr->name == DW_AT_containing_type);
19536
19537 /* First see if we have it cached. */
19538
19539 if (attr->form == DW_FORM_GNU_ref_alt)
19540 {
19541 struct dwarf2_per_cu_data *per_cu;
19542 sect_offset sect_off = attr->get_ref_die_offset ();
19543
19544 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
19545 per_objfile->per_bfd);
19546 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
19547 }
19548 else if (attr->form_is_ref ())
19549 {
19550 sect_offset sect_off = attr->get_ref_die_offset ();
19551
19552 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
19553 }
19554 else if (attr->form == DW_FORM_ref_sig8)
19555 {
19556 ULONGEST signature = attr->as_signature ();
19557
19558 return get_signatured_type (die, signature, cu);
19559 }
19560 else
19561 {
19562 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
19563 " at %s [in module %s]"),
19564 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
19565 objfile_name (objfile));
19566 return build_error_marker_type (cu, die);
19567 }
19568
19569 /* If not cached we need to read it in. */
19570
19571 if (this_type == NULL)
19572 {
19573 struct die_info *type_die = NULL;
19574 struct dwarf2_cu *type_cu = cu;
19575
19576 if (attr->form_is_ref ())
19577 type_die = follow_die_ref (die, attr, &type_cu);
19578 if (type_die == NULL)
19579 return build_error_marker_type (cu, die);
19580 /* If we find the type now, it's probably because the type came
19581 from an inter-CU reference and the type's CU got expanded before
19582 ours. */
19583 this_type = read_type_die (type_die, type_cu);
19584 }
19585
19586 /* If we still don't have a type use an error marker. */
19587
19588 if (this_type == NULL)
19589 return build_error_marker_type (cu, die);
19590
19591 return this_type;
19592 }
19593
19594 /* Return the type in DIE, CU.
19595 Returns NULL for invalid types.
19596
19597 This first does a lookup in die_type_hash,
19598 and only reads the die in if necessary.
19599
19600 NOTE: This can be called when reading in partial or full symbols. */
19601
19602 static struct type *
19603 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19604 {
19605 struct type *this_type;
19606
19607 this_type = get_die_type (die, cu);
19608 if (this_type)
19609 return this_type;
19610
19611 return read_type_die_1 (die, cu);
19612 }
19613
19614 /* Read the type in DIE, CU.
19615 Returns NULL for invalid types. */
19616
19617 static struct type *
19618 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19619 {
19620 struct type *this_type = NULL;
19621
19622 switch (die->tag)
19623 {
19624 case DW_TAG_class_type:
19625 case DW_TAG_interface_type:
19626 case DW_TAG_structure_type:
19627 case DW_TAG_union_type:
19628 this_type = read_structure_type (die, cu);
19629 break;
19630 case DW_TAG_enumeration_type:
19631 this_type = read_enumeration_type (die, cu);
19632 break;
19633 case DW_TAG_entry_point:
19634 case DW_TAG_subprogram:
19635 case DW_TAG_subroutine_type:
19636 case DW_TAG_inlined_subroutine:
19637 this_type = read_subroutine_type (die, cu);
19638 break;
19639 case DW_TAG_array_type:
19640 this_type = read_array_type (die, cu);
19641 break;
19642 case DW_TAG_set_type:
19643 this_type = read_set_type (die, cu);
19644 break;
19645 case DW_TAG_pointer_type:
19646 this_type = read_tag_pointer_type (die, cu);
19647 break;
19648 case DW_TAG_ptr_to_member_type:
19649 this_type = read_tag_ptr_to_member_type (die, cu);
19650 break;
19651 case DW_TAG_reference_type:
19652 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19653 break;
19654 case DW_TAG_rvalue_reference_type:
19655 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19656 break;
19657 case DW_TAG_const_type:
19658 this_type = read_tag_const_type (die, cu);
19659 break;
19660 case DW_TAG_volatile_type:
19661 this_type = read_tag_volatile_type (die, cu);
19662 break;
19663 case DW_TAG_restrict_type:
19664 this_type = read_tag_restrict_type (die, cu);
19665 break;
19666 case DW_TAG_string_type:
19667 this_type = read_tag_string_type (die, cu);
19668 break;
19669 case DW_TAG_typedef:
19670 this_type = read_typedef (die, cu);
19671 break;
19672 case DW_TAG_generic_subrange:
19673 case DW_TAG_subrange_type:
19674 this_type = read_subrange_type (die, cu);
19675 break;
19676 case DW_TAG_base_type:
19677 this_type = read_base_type (die, cu);
19678 break;
19679 case DW_TAG_unspecified_type:
19680 this_type = read_unspecified_type (die, cu);
19681 break;
19682 case DW_TAG_namespace:
19683 this_type = read_namespace_type (die, cu);
19684 break;
19685 case DW_TAG_module:
19686 this_type = read_module_type (die, cu);
19687 break;
19688 case DW_TAG_atomic_type:
19689 this_type = read_tag_atomic_type (die, cu);
19690 break;
19691 default:
19692 complaint (_("unexpected tag in read_type_die: '%s'"),
19693 dwarf_tag_name (die->tag));
19694 break;
19695 }
19696
19697 return this_type;
19698 }
19699
19700 /* See if we can figure out if the class lives in a namespace. We do
19701 this by looking for a member function; its demangled name will
19702 contain namespace info, if there is any.
19703 Return the computed name or NULL.
19704 Space for the result is allocated on the objfile's obstack.
19705 This is the full-die version of guess_partial_die_structure_name.
19706 In this case we know DIE has no useful parent. */
19707
19708 static const char *
19709 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19710 {
19711 struct die_info *spec_die;
19712 struct dwarf2_cu *spec_cu;
19713 struct die_info *child;
19714 struct objfile *objfile = cu->per_objfile->objfile;
19715
19716 spec_cu = cu;
19717 spec_die = die_specification (die, &spec_cu);
19718 if (spec_die != NULL)
19719 {
19720 die = spec_die;
19721 cu = spec_cu;
19722 }
19723
19724 for (child = die->child;
19725 child != NULL;
19726 child = child->sibling)
19727 {
19728 if (child->tag == DW_TAG_subprogram)
19729 {
19730 const char *linkage_name = dw2_linkage_name (child, cu);
19731
19732 if (linkage_name != NULL)
19733 {
19734 gdb::unique_xmalloc_ptr<char> actual_name
19735 (cu->language_defn->class_name_from_physname (linkage_name));
19736 const char *name = NULL;
19737
19738 if (actual_name != NULL)
19739 {
19740 const char *die_name = dwarf2_name (die, cu);
19741
19742 if (die_name != NULL
19743 && strcmp (die_name, actual_name.get ()) != 0)
19744 {
19745 /* Strip off the class name from the full name.
19746 We want the prefix. */
19747 int die_name_len = strlen (die_name);
19748 int actual_name_len = strlen (actual_name.get ());
19749 const char *ptr = actual_name.get ();
19750
19751 /* Test for '::' as a sanity check. */
19752 if (actual_name_len > die_name_len + 2
19753 && ptr[actual_name_len - die_name_len - 1] == ':')
19754 name = obstack_strndup (
19755 &objfile->per_bfd->storage_obstack,
19756 ptr, actual_name_len - die_name_len - 2);
19757 }
19758 }
19759 return name;
19760 }
19761 }
19762 }
19763
19764 return NULL;
19765 }
19766
19767 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19768 prefix part in such case. See
19769 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19770
19771 static const char *
19772 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19773 {
19774 struct attribute *attr;
19775 const char *base;
19776
19777 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19778 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19779 return NULL;
19780
19781 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19782 return NULL;
19783
19784 attr = dw2_linkage_name_attr (die, cu);
19785 const char *attr_name = attr->as_string ();
19786 if (attr == NULL || attr_name == NULL)
19787 return NULL;
19788
19789 /* dwarf2_name had to be already called. */
19790 gdb_assert (attr->canonical_string_p ());
19791
19792 /* Strip the base name, keep any leading namespaces/classes. */
19793 base = strrchr (attr_name, ':');
19794 if (base == NULL || base == attr_name || base[-1] != ':')
19795 return "";
19796
19797 struct objfile *objfile = cu->per_objfile->objfile;
19798 return obstack_strndup (&objfile->per_bfd->storage_obstack,
19799 attr_name,
19800 &base[-1] - attr_name);
19801 }
19802
19803 /* Return the name of the namespace/class that DIE is defined within,
19804 or "" if we can't tell. The caller should not xfree the result.
19805
19806 For example, if we're within the method foo() in the following
19807 code:
19808
19809 namespace N {
19810 class C {
19811 void foo () {
19812 }
19813 };
19814 }
19815
19816 then determine_prefix on foo's die will return "N::C". */
19817
19818 static const char *
19819 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19820 {
19821 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19822 struct die_info *parent, *spec_die;
19823 struct dwarf2_cu *spec_cu;
19824 struct type *parent_type;
19825 const char *retval;
19826
19827 if (cu->lang () != language_cplus
19828 && cu->lang () != language_fortran
19829 && cu->lang () != language_d
19830 && cu->lang () != language_rust)
19831 return "";
19832
19833 retval = anonymous_struct_prefix (die, cu);
19834 if (retval)
19835 return retval;
19836
19837 /* We have to be careful in the presence of DW_AT_specification.
19838 For example, with GCC 3.4, given the code
19839
19840 namespace N {
19841 void foo() {
19842 // Definition of N::foo.
19843 }
19844 }
19845
19846 then we'll have a tree of DIEs like this:
19847
19848 1: DW_TAG_compile_unit
19849 2: DW_TAG_namespace // N
19850 3: DW_TAG_subprogram // declaration of N::foo
19851 4: DW_TAG_subprogram // definition of N::foo
19852 DW_AT_specification // refers to die #3
19853
19854 Thus, when processing die #4, we have to pretend that we're in
19855 the context of its DW_AT_specification, namely the context of die
19856 #3. */
19857 spec_cu = cu;
19858 spec_die = die_specification (die, &spec_cu);
19859 if (spec_die == NULL)
19860 parent = die->parent;
19861 else
19862 {
19863 parent = spec_die->parent;
19864 cu = spec_cu;
19865 }
19866
19867 if (parent == NULL)
19868 return "";
19869 else if (parent->building_fullname)
19870 {
19871 const char *name;
19872 const char *parent_name;
19873
19874 /* It has been seen on RealView 2.2 built binaries,
19875 DW_TAG_template_type_param types actually _defined_ as
19876 children of the parent class:
19877
19878 enum E {};
19879 template class <class Enum> Class{};
19880 Class<enum E> class_e;
19881
19882 1: DW_TAG_class_type (Class)
19883 2: DW_TAG_enumeration_type (E)
19884 3: DW_TAG_enumerator (enum1:0)
19885 3: DW_TAG_enumerator (enum2:1)
19886 ...
19887 2: DW_TAG_template_type_param
19888 DW_AT_type DW_FORM_ref_udata (E)
19889
19890 Besides being broken debug info, it can put GDB into an
19891 infinite loop. Consider:
19892
19893 When we're building the full name for Class<E>, we'll start
19894 at Class, and go look over its template type parameters,
19895 finding E. We'll then try to build the full name of E, and
19896 reach here. We're now trying to build the full name of E,
19897 and look over the parent DIE for containing scope. In the
19898 broken case, if we followed the parent DIE of E, we'd again
19899 find Class, and once again go look at its template type
19900 arguments, etc., etc. Simply don't consider such parent die
19901 as source-level parent of this die (it can't be, the language
19902 doesn't allow it), and break the loop here. */
19903 name = dwarf2_name (die, cu);
19904 parent_name = dwarf2_name (parent, cu);
19905 complaint (_("template param type '%s' defined within parent '%s'"),
19906 name ? name : "<unknown>",
19907 parent_name ? parent_name : "<unknown>");
19908 return "";
19909 }
19910 else
19911 switch (parent->tag)
19912 {
19913 case DW_TAG_namespace:
19914 parent_type = read_type_die (parent, cu);
19915 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19916 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19917 Work around this problem here. */
19918 if (cu->lang () == language_cplus
19919 && strcmp (parent_type->name (), "::") == 0)
19920 return "";
19921 /* We give a name to even anonymous namespaces. */
19922 return parent_type->name ();
19923 case DW_TAG_class_type:
19924 case DW_TAG_interface_type:
19925 case DW_TAG_structure_type:
19926 case DW_TAG_union_type:
19927 case DW_TAG_module:
19928 parent_type = read_type_die (parent, cu);
19929 if (parent_type->name () != NULL)
19930 return parent_type->name ();
19931 else
19932 /* An anonymous structure is only allowed non-static data
19933 members; no typedefs, no member functions, et cetera.
19934 So it does not need a prefix. */
19935 return "";
19936 case DW_TAG_compile_unit:
19937 case DW_TAG_partial_unit:
19938 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19939 if (cu->lang () == language_cplus
19940 && !per_objfile->per_bfd->types.empty ()
19941 && die->child != NULL
19942 && (die->tag == DW_TAG_class_type
19943 || die->tag == DW_TAG_structure_type
19944 || die->tag == DW_TAG_union_type))
19945 {
19946 const char *name = guess_full_die_structure_name (die, cu);
19947 if (name != NULL)
19948 return name;
19949 }
19950 return "";
19951 case DW_TAG_subprogram:
19952 /* Nested subroutines in Fortran get a prefix with the name
19953 of the parent's subroutine. Entry points are prefixed by the
19954 parent's namespace. */
19955 if (cu->lang () == language_fortran)
19956 {
19957 if ((die->tag == DW_TAG_subprogram)
19958 && (dwarf2_name (parent, cu) != NULL))
19959 return dwarf2_name (parent, cu);
19960 else if (die->tag == DW_TAG_entry_point)
19961 return determine_prefix (parent, cu);
19962 }
19963 return "";
19964 case DW_TAG_enumeration_type:
19965 parent_type = read_type_die (parent, cu);
19966 if (parent_type->is_declared_class ())
19967 {
19968 if (parent_type->name () != NULL)
19969 return parent_type->name ();
19970 return "";
19971 }
19972 [[fallthrough]];
19973 default:
19974 return determine_prefix (parent, cu);
19975 }
19976 }
19977
19978 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19979 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19980 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19981 an obconcat, otherwise allocate storage for the result. The CU argument is
19982 used to determine the language and hence, the appropriate separator. */
19983
19984 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19985
19986 static char *
19987 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19988 int physname, struct dwarf2_cu *cu)
19989 {
19990 const char *lead = "";
19991 const char *sep;
19992
19993 if (suffix == NULL || suffix[0] == '\0'
19994 || prefix == NULL || prefix[0] == '\0')
19995 sep = "";
19996 else if (cu->lang () == language_d)
19997 {
19998 /* For D, the 'main' function could be defined in any module, but it
19999 should never be prefixed. */
20000 if (strcmp (suffix, "D main") == 0)
20001 {
20002 prefix = "";
20003 sep = "";
20004 }
20005 else
20006 sep = ".";
20007 }
20008 else if (cu->lang () == language_fortran && physname)
20009 {
20010 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20011 DW_AT_MIPS_linkage_name is preferred and used instead. */
20012
20013 lead = "__";
20014 sep = "_MOD_";
20015 }
20016 else
20017 sep = "::";
20018
20019 if (prefix == NULL)
20020 prefix = "";
20021 if (suffix == NULL)
20022 suffix = "";
20023
20024 if (obs == NULL)
20025 {
20026 char *retval
20027 = ((char *)
20028 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
20029
20030 strcpy (retval, lead);
20031 strcat (retval, prefix);
20032 strcat (retval, sep);
20033 strcat (retval, suffix);
20034 return retval;
20035 }
20036 else
20037 {
20038 /* We have an obstack. */
20039 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20040 }
20041 }
20042
20043 /* Return a generic name for a DW_TAG_template_type_param or
20044 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
20045 per parent, so each function/class/struct template will have their own set
20046 of template parameters named <unnnamed0>, <unnamed1>, ... where the
20047 enumeration starts at 0 and represents the position of the template tag in
20048 the list of unnamed template tags for this parent, counting both, type and
20049 value tags. */
20050
20051 static const char *
20052 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
20053 {
20054 if (die->parent == nullptr)
20055 return nullptr;
20056
20057 /* Count the parent types unnamed template type and value children until, we
20058 arrive at our entry. */
20059 size_t nth_unnamed = 0;
20060
20061 die_info *child = die->parent->child;
20062 while (child != die)
20063 {
20064 gdb_assert (child != nullptr);
20065 if (child->tag == DW_TAG_template_type_param
20066 || child->tag == DW_TAG_template_value_param)
20067 {
20068 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
20069 ++nth_unnamed;
20070 }
20071 child = child->sibling;
20072 }
20073
20074 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
20075 return cu->per_objfile->objfile->intern (name_str.c_str ());
20076 }
20077
20078 /* Get name of a die, return NULL if not found. */
20079
20080 static const char *
20081 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20082 struct objfile *objfile)
20083 {
20084 if (name == nullptr)
20085 return name;
20086
20087 if (cu->lang () == language_cplus)
20088 {
20089 gdb::unique_xmalloc_ptr<char> canon_name
20090 = cp_canonicalize_string (name);
20091
20092 if (canon_name != nullptr)
20093 name = objfile->intern (canon_name.get ());
20094 }
20095 else if (cu->lang () == language_c)
20096 {
20097 gdb::unique_xmalloc_ptr<char> canon_name
20098 = c_canonicalize_name (name);
20099
20100 if (canon_name != nullptr)
20101 name = objfile->intern (canon_name.get ());
20102 }
20103
20104 return name;
20105 }
20106
20107 /* Get name of a die, return NULL if not found.
20108 Anonymous namespaces are converted to their magic string. */
20109
20110 static const char *
20111 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20112 {
20113 struct attribute *attr;
20114 struct objfile *objfile = cu->per_objfile->objfile;
20115
20116 attr = dwarf2_attr (die, DW_AT_name, cu);
20117 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
20118 if (attr_name == nullptr
20119 && die->tag != DW_TAG_namespace
20120 && die->tag != DW_TAG_class_type
20121 && die->tag != DW_TAG_interface_type
20122 && die->tag != DW_TAG_structure_type
20123 && die->tag != DW_TAG_namelist
20124 && die->tag != DW_TAG_union_type
20125 && die->tag != DW_TAG_template_type_param
20126 && die->tag != DW_TAG_template_value_param)
20127 return NULL;
20128
20129 switch (die->tag)
20130 {
20131 /* A member's name should not be canonicalized. This is a bit
20132 of a hack, in that normally it should not be possible to run
20133 into this situation; however, the dw2-unusual-field-names.exp
20134 test creates custom DWARF that does. */
20135 case DW_TAG_member:
20136 case DW_TAG_compile_unit:
20137 case DW_TAG_partial_unit:
20138 /* Compilation units have a DW_AT_name that is a filename, not
20139 a source language identifier. */
20140 case DW_TAG_enumeration_type:
20141 case DW_TAG_enumerator:
20142 /* These tags always have simple identifiers already; no need
20143 to canonicalize them. */
20144 return attr_name;
20145
20146 case DW_TAG_namespace:
20147 if (attr_name != nullptr)
20148 return attr_name;
20149 return CP_ANONYMOUS_NAMESPACE_STR;
20150
20151 /* DWARF does not actually require template tags to have a name. */
20152 case DW_TAG_template_type_param:
20153 case DW_TAG_template_value_param:
20154 if (attr_name == nullptr)
20155 return unnamed_template_tag_name (die, cu);
20156 [[fallthrough]];
20157 case DW_TAG_class_type:
20158 case DW_TAG_interface_type:
20159 case DW_TAG_structure_type:
20160 case DW_TAG_union_type:
20161 case DW_TAG_namelist:
20162 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20163 structures or unions. These were of the form "._%d" in GCC 4.1,
20164 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20165 and GCC 4.4. We work around this problem by ignoring these. */
20166 if (attr_name != nullptr
20167 && (startswith (attr_name, "._")
20168 || startswith (attr_name, "<anonymous")))
20169 return NULL;
20170
20171 /* GCC might emit a nameless typedef that has a linkage name. See
20172 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20173 if (!attr || attr_name == NULL)
20174 {
20175 attr = dw2_linkage_name_attr (die, cu);
20176 attr_name = attr == nullptr ? nullptr : attr->as_string ();
20177 if (attr == NULL || attr_name == NULL)
20178 return NULL;
20179
20180 /* Avoid demangling attr_name the second time on a second
20181 call for the same DIE. */
20182 if (!attr->canonical_string_p ())
20183 {
20184 gdb::unique_xmalloc_ptr<char> demangled
20185 (gdb_demangle (attr_name, DMGL_TYPES));
20186 if (demangled == nullptr)
20187 return nullptr;
20188
20189 attr->set_string_canonical (objfile->intern (demangled.get ()));
20190 attr_name = attr->as_string ();
20191 }
20192
20193 /* Strip any leading namespaces/classes, keep only the
20194 base name. DW_AT_name for named DIEs does not
20195 contain the prefixes. */
20196 const char *base = strrchr (attr_name, ':');
20197 if (base && base > attr_name && base[-1] == ':')
20198 return &base[1];
20199 else
20200 return attr_name;
20201 }
20202 break;
20203
20204 default:
20205 break;
20206 }
20207
20208 if (!attr->canonical_string_p ())
20209 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
20210 objfile));
20211 return attr->as_string ();
20212 }
20213
20214 /* Return the die that this die in an extension of, or NULL if there
20215 is none. *EXT_CU is the CU containing DIE on input, and the CU
20216 containing the return value on output. */
20217
20218 static struct die_info *
20219 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20220 {
20221 struct attribute *attr;
20222
20223 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20224 if (attr == NULL)
20225 return NULL;
20226
20227 return follow_die_ref (die, attr, ext_cu);
20228 }
20229
20230 static void
20231 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20232 {
20233 void **slot;
20234
20235 slot = htab_find_slot_with_hash (cu->die_hash, die,
20236 to_underlying (die->sect_off),
20237 INSERT);
20238
20239 *slot = die;
20240 }
20241
20242 /* Follow reference or signature attribute ATTR of SRC_DIE.
20243 On entry *REF_CU is the CU of SRC_DIE.
20244 On exit *REF_CU is the CU of the result. */
20245
20246 static struct die_info *
20247 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20248 struct dwarf2_cu **ref_cu)
20249 {
20250 struct die_info *die;
20251
20252 if (attr->form_is_ref ())
20253 die = follow_die_ref (src_die, attr, ref_cu);
20254 else if (attr->form == DW_FORM_ref_sig8)
20255 die = follow_die_sig (src_die, attr, ref_cu);
20256 else
20257 {
20258 src_die->error_dump ();
20259 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20260 objfile_name ((*ref_cu)->per_objfile->objfile));
20261 }
20262
20263 return die;
20264 }
20265
20266 /* Follow reference OFFSET.
20267 On entry *REF_CU is the CU of the source die referencing OFFSET.
20268 On exit *REF_CU is the CU of the result.
20269 Returns NULL if OFFSET is invalid. */
20270
20271 static struct die_info *
20272 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20273 struct dwarf2_cu **ref_cu)
20274 {
20275 struct die_info temp_die;
20276 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20277 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20278
20279 gdb_assert (cu->per_cu != NULL);
20280
20281 target_cu = cu;
20282
20283 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
20284 "source CU contains target offset: %d",
20285 sect_offset_str (cu->per_cu->sect_off),
20286 sect_offset_str (sect_off),
20287 cu->header.offset_in_cu_p (sect_off));
20288
20289 if (cu->per_cu->is_debug_types)
20290 {
20291 /* .debug_types CUs cannot reference anything outside their CU.
20292 If they need to, they have to reference a signatured type via
20293 DW_FORM_ref_sig8. */
20294 if (!cu->header.offset_in_cu_p (sect_off))
20295 return NULL;
20296 }
20297 else if (offset_in_dwz != cu->per_cu->is_dwz
20298 || !cu->header.offset_in_cu_p (sect_off))
20299 {
20300 struct dwarf2_per_cu_data *per_cu;
20301
20302 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20303 per_objfile->per_bfd);
20304
20305 dwarf_read_debug_printf_v ("target CU offset: %s, "
20306 "target CU DIEs loaded: %d",
20307 sect_offset_str (per_cu->sect_off),
20308 per_objfile->get_cu (per_cu) != nullptr);
20309
20310 /* If necessary, add it to the queue and load its DIEs.
20311
20312 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20313 it doesn't mean they are currently loaded. Since we require them
20314 to be loaded, we must check for ourselves. */
20315 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
20316 || per_objfile->get_cu (per_cu) == nullptr)
20317 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
20318 false, cu->lang ());
20319
20320 target_cu = per_objfile->get_cu (per_cu);
20321 gdb_assert (target_cu != nullptr);
20322 }
20323 else if (cu->dies == NULL)
20324 {
20325 /* We're loading full DIEs during partial symbol reading. */
20326 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
20327 language_minimal);
20328 }
20329
20330 *ref_cu = target_cu;
20331 temp_die.sect_off = sect_off;
20332
20333 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20334 &temp_die,
20335 to_underlying (sect_off));
20336 }
20337
20338 /* Follow reference attribute ATTR of SRC_DIE.
20339 On entry *REF_CU is the CU of SRC_DIE.
20340 On exit *REF_CU is the CU of the result. */
20341
20342 static struct die_info *
20343 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20344 struct dwarf2_cu **ref_cu)
20345 {
20346 sect_offset sect_off = attr->get_ref_die_offset ();
20347 struct dwarf2_cu *cu = *ref_cu;
20348 struct die_info *die;
20349
20350 if (attr->form != DW_FORM_GNU_ref_alt && src_die->sect_off == sect_off)
20351 {
20352 /* Self-reference, we're done. */
20353 return src_die;
20354 }
20355
20356 die = follow_die_offset (sect_off,
20357 (attr->form == DW_FORM_GNU_ref_alt
20358 || cu->per_cu->is_dwz),
20359 ref_cu);
20360 if (!die)
20361 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
20362 "at %s [in module %s]"),
20363 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
20364 objfile_name (cu->per_objfile->objfile));
20365
20366 return die;
20367 }
20368
20369 /* See read.h. */
20370
20371 struct dwarf2_locexpr_baton
20372 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20373 dwarf2_per_cu_data *per_cu,
20374 dwarf2_per_objfile *per_objfile,
20375 gdb::function_view<CORE_ADDR ()> get_frame_pc,
20376 bool resolve_abstract_p)
20377 {
20378 struct die_info *die;
20379 struct attribute *attr;
20380 struct dwarf2_locexpr_baton retval;
20381 struct objfile *objfile = per_objfile->objfile;
20382
20383 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20384 if (cu == nullptr)
20385 cu = load_cu (per_cu, per_objfile, false);
20386
20387 if (cu == nullptr)
20388 {
20389 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20390 Instead just throw an error, not much else we can do. */
20391 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20392 sect_offset_str (sect_off), objfile_name (objfile));
20393 }
20394
20395 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20396 if (!die)
20397 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20398 sect_offset_str (sect_off), objfile_name (objfile));
20399
20400 attr = dwarf2_attr (die, DW_AT_location, cu);
20401 if (!attr && resolve_abstract_p
20402 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
20403 != per_objfile->per_bfd->abstract_to_concrete.end ()))
20404 {
20405 CORE_ADDR pc = get_frame_pc ();
20406
20407 for (const auto &cand_off
20408 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
20409 {
20410 struct dwarf2_cu *cand_cu = cu;
20411 struct die_info *cand
20412 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
20413 if (!cand
20414 || !cand->parent
20415 || cand->parent->tag != DW_TAG_subprogram)
20416 continue;
20417
20418 unrelocated_addr unrel_low, unrel_high;
20419 get_scope_pc_bounds (cand->parent, &unrel_low, &unrel_high, cu);
20420 if (unrel_low == ((unrelocated_addr) -1))
20421 continue;
20422 CORE_ADDR pc_low = per_objfile->relocate (unrel_low);
20423 CORE_ADDR pc_high = per_objfile->relocate (unrel_high);
20424 if (!(pc_low <= pc && pc < pc_high))
20425 continue;
20426
20427 die = cand;
20428 attr = dwarf2_attr (die, DW_AT_location, cu);
20429 break;
20430 }
20431 }
20432
20433 if (!attr)
20434 {
20435 /* DWARF: "If there is no such attribute, then there is no effect.".
20436 DATA is ignored if SIZE is 0. */
20437
20438 retval.data = NULL;
20439 retval.size = 0;
20440 }
20441 else if (attr->form_is_section_offset ())
20442 {
20443 struct dwarf2_loclist_baton loclist_baton;
20444 CORE_ADDR pc = get_frame_pc ();
20445 size_t size;
20446
20447 fill_in_loclist_baton (cu, &loclist_baton, attr);
20448
20449 retval.data = dwarf2_find_location_expression (&loclist_baton,
20450 &size, pc);
20451 retval.size = size;
20452 }
20453 else
20454 {
20455 if (!attr->form_is_block ())
20456 error (_("Dwarf Error: DIE at %s referenced in module %s "
20457 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20458 sect_offset_str (sect_off), objfile_name (objfile));
20459
20460 struct dwarf_block *block = attr->as_block ();
20461 retval.data = block->data;
20462 retval.size = block->size;
20463 }
20464 retval.per_objfile = per_objfile;
20465 retval.per_cu = cu->per_cu;
20466
20467 per_objfile->age_comp_units ();
20468
20469 return retval;
20470 }
20471
20472 /* See read.h. */
20473
20474 struct dwarf2_locexpr_baton
20475 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20476 dwarf2_per_cu_data *per_cu,
20477 dwarf2_per_objfile *per_objfile,
20478 gdb::function_view<CORE_ADDR ()> get_frame_pc)
20479 {
20480 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20481
20482 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
20483 get_frame_pc);
20484 }
20485
20486 /* Write a constant of a given type as target-ordered bytes into
20487 OBSTACK. */
20488
20489 static const gdb_byte *
20490 write_constant_as_bytes (struct obstack *obstack,
20491 enum bfd_endian byte_order,
20492 struct type *type,
20493 ULONGEST value,
20494 LONGEST *len)
20495 {
20496 gdb_byte *result;
20497
20498 *len = type->length ();
20499 result = (gdb_byte *) obstack_alloc (obstack, *len);
20500 store_unsigned_integer (result, *len, byte_order, value);
20501
20502 return result;
20503 }
20504
20505 /* See read.h. */
20506
20507 const gdb_byte *
20508 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20509 dwarf2_per_cu_data *per_cu,
20510 dwarf2_per_objfile *per_objfile,
20511 obstack *obstack,
20512 LONGEST *len)
20513 {
20514 struct die_info *die;
20515 struct attribute *attr;
20516 const gdb_byte *result = NULL;
20517 struct type *type;
20518 LONGEST value;
20519 enum bfd_endian byte_order;
20520 struct objfile *objfile = per_objfile->objfile;
20521
20522 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20523 if (cu == nullptr)
20524 cu = load_cu (per_cu, per_objfile, false);
20525
20526 if (cu == nullptr)
20527 {
20528 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20529 Instead just throw an error, not much else we can do. */
20530 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20531 sect_offset_str (sect_off), objfile_name (objfile));
20532 }
20533
20534 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20535 if (!die)
20536 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20537 sect_offset_str (sect_off), objfile_name (objfile));
20538
20539 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20540 if (attr == NULL)
20541 return NULL;
20542
20543 byte_order = (bfd_big_endian (objfile->obfd.get ())
20544 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20545
20546 switch (attr->form)
20547 {
20548 case DW_FORM_addr:
20549 case DW_FORM_addrx:
20550 case DW_FORM_GNU_addr_index:
20551 {
20552 gdb_byte *tem;
20553
20554 *len = cu->header.addr_size;
20555 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20556 store_unsigned_integer (tem, *len, byte_order,
20557 (ULONGEST) attr->as_address ());
20558 result = tem;
20559 }
20560 break;
20561 case DW_FORM_string:
20562 case DW_FORM_strp:
20563 case DW_FORM_strx:
20564 case DW_FORM_GNU_str_index:
20565 case DW_FORM_GNU_strp_alt:
20566 /* The string is already allocated on the objfile obstack, point
20567 directly to it. */
20568 {
20569 const char *attr_name = attr->as_string ();
20570 result = (const gdb_byte *) attr_name;
20571 *len = strlen (attr_name);
20572 }
20573 break;
20574 case DW_FORM_block1:
20575 case DW_FORM_block2:
20576 case DW_FORM_block4:
20577 case DW_FORM_block:
20578 case DW_FORM_exprloc:
20579 case DW_FORM_data16:
20580 {
20581 struct dwarf_block *block = attr->as_block ();
20582 result = block->data;
20583 *len = block->size;
20584 }
20585 break;
20586
20587 /* The DW_AT_const_value attributes are supposed to carry the
20588 symbol's value "represented as it would be on the target
20589 architecture." By the time we get here, it's already been
20590 converted to host endianness, so we just need to sign- or
20591 zero-extend it as appropriate. */
20592 case DW_FORM_data1:
20593 type = die_type (die, cu);
20594 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20595 if (result == NULL)
20596 result = write_constant_as_bytes (obstack, byte_order,
20597 type, value, len);
20598 break;
20599 case DW_FORM_data2:
20600 type = die_type (die, cu);
20601 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20602 if (result == NULL)
20603 result = write_constant_as_bytes (obstack, byte_order,
20604 type, value, len);
20605 break;
20606 case DW_FORM_data4:
20607 type = die_type (die, cu);
20608 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20609 if (result == NULL)
20610 result = write_constant_as_bytes (obstack, byte_order,
20611 type, value, len);
20612 break;
20613 case DW_FORM_data8:
20614 type = die_type (die, cu);
20615 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20616 if (result == NULL)
20617 result = write_constant_as_bytes (obstack, byte_order,
20618 type, value, len);
20619 break;
20620
20621 case DW_FORM_sdata:
20622 case DW_FORM_implicit_const:
20623 type = die_type (die, cu);
20624 result = write_constant_as_bytes (obstack, byte_order,
20625 type, attr->as_signed (), len);
20626 break;
20627
20628 case DW_FORM_udata:
20629 type = die_type (die, cu);
20630 result = write_constant_as_bytes (obstack, byte_order,
20631 type, attr->as_unsigned (), len);
20632 break;
20633
20634 default:
20635 complaint (_("unsupported const value attribute form: '%s'"),
20636 dwarf_form_name (attr->form));
20637 break;
20638 }
20639
20640 return result;
20641 }
20642
20643 /* See read.h. */
20644
20645 struct type *
20646 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20647 dwarf2_per_cu_data *per_cu,
20648 dwarf2_per_objfile *per_objfile,
20649 const char **var_name)
20650 {
20651 struct die_info *die;
20652
20653 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20654 if (cu == nullptr)
20655 cu = load_cu (per_cu, per_objfile, false);
20656
20657 if (cu == nullptr)
20658 return nullptr;
20659
20660 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20661 if (!die)
20662 return NULL;
20663
20664 if (var_name != nullptr)
20665 *var_name = var_decl_name (die, cu);
20666 return die_type (die, cu);
20667 }
20668
20669 /* See read.h. */
20670
20671 struct type *
20672 dwarf2_get_die_type (cu_offset die_offset,
20673 dwarf2_per_cu_data *per_cu,
20674 dwarf2_per_objfile *per_objfile)
20675 {
20676 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20677 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
20678 }
20679
20680 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20681 On entry *REF_CU is the CU of SRC_DIE.
20682 On exit *REF_CU is the CU of the result.
20683 Returns NULL if the referenced DIE isn't found. */
20684
20685 static struct die_info *
20686 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20687 struct dwarf2_cu **ref_cu)
20688 {
20689 struct die_info temp_die;
20690 struct dwarf2_cu *sig_cu;
20691 struct die_info *die;
20692 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
20693
20694
20695 /* While it might be nice to assert sig_type->type == NULL here,
20696 we can get here for DW_AT_imported_declaration where we need
20697 the DIE not the type. */
20698
20699 /* If necessary, add it to the queue and load its DIEs.
20700
20701 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20702 it doesn't mean they are currently loaded. Since we require them
20703 to be loaded, we must check for ourselves. */
20704 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
20705 language_minimal)
20706 || per_objfile->get_cu (sig_type) == nullptr)
20707 read_signatured_type (sig_type, per_objfile);
20708
20709 sig_cu = per_objfile->get_cu (sig_type);
20710 gdb_assert (sig_cu != NULL);
20711 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20712 temp_die.sect_off = sig_type->type_offset_in_section;
20713 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20714 to_underlying (temp_die.sect_off));
20715 if (die)
20716 {
20717 /* For .gdb_index version 7 keep track of included TUs.
20718 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20719 if (per_objfile->per_bfd->index_table != NULL
20720 && !per_objfile->per_bfd->index_table->version_check ())
20721 (*ref_cu)->per_cu->imported_symtabs.push_back (sig_cu->per_cu);
20722
20723 *ref_cu = sig_cu;
20724 return die;
20725 }
20726
20727 return NULL;
20728 }
20729
20730 /* Follow signatured type referenced by ATTR in SRC_DIE.
20731 On entry *REF_CU is the CU of SRC_DIE.
20732 On exit *REF_CU is the CU of the result.
20733 The result is the DIE of the type.
20734 If the referenced type cannot be found an error is thrown. */
20735
20736 static struct die_info *
20737 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20738 struct dwarf2_cu **ref_cu)
20739 {
20740 ULONGEST signature = attr->as_signature ();
20741 struct signatured_type *sig_type;
20742 struct die_info *die;
20743
20744 gdb_assert (attr->form == DW_FORM_ref_sig8);
20745
20746 sig_type = lookup_signatured_type (*ref_cu, signature);
20747 /* sig_type will be NULL if the signatured type is missing from
20748 the debug info. */
20749 if (sig_type == NULL)
20750 {
20751 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20752 " from DIE at %s [in module %s]"),
20753 hex_string (signature), sect_offset_str (src_die->sect_off),
20754 objfile_name ((*ref_cu)->per_objfile->objfile));
20755 }
20756
20757 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20758 if (die == NULL)
20759 {
20760 src_die->error_dump ();
20761 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20762 " from DIE at %s [in module %s]"),
20763 hex_string (signature), sect_offset_str (src_die->sect_off),
20764 objfile_name ((*ref_cu)->per_objfile->objfile));
20765 }
20766
20767 return die;
20768 }
20769
20770 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20771 reading in and processing the type unit if necessary. */
20772
20773 static struct type *
20774 get_signatured_type (struct die_info *die, ULONGEST signature,
20775 struct dwarf2_cu *cu)
20776 {
20777 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20778 struct signatured_type *sig_type;
20779 struct dwarf2_cu *type_cu;
20780 struct die_info *type_die;
20781 struct type *type;
20782
20783 sig_type = lookup_signatured_type (cu, signature);
20784 /* sig_type will be NULL if the signatured type is missing from
20785 the debug info. */
20786 if (sig_type == NULL)
20787 {
20788 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20789 " from DIE at %s [in module %s]"),
20790 hex_string (signature), sect_offset_str (die->sect_off),
20791 objfile_name (per_objfile->objfile));
20792 return build_error_marker_type (cu, die);
20793 }
20794
20795 /* If we already know the type we're done. */
20796 type = per_objfile->get_type_for_signatured_type (sig_type);
20797 if (type != nullptr)
20798 return type;
20799
20800 type_cu = cu;
20801 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20802 if (type_die != NULL)
20803 {
20804 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20805 is created. This is important, for example, because for c++ classes
20806 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20807 type = read_type_die (type_die, type_cu);
20808 if (type == NULL)
20809 {
20810 complaint (_("Dwarf Error: Cannot build signatured type %s"
20811 " referenced from DIE at %s [in module %s]"),
20812 hex_string (signature), sect_offset_str (die->sect_off),
20813 objfile_name (per_objfile->objfile));
20814 type = build_error_marker_type (cu, die);
20815 }
20816 }
20817 else
20818 {
20819 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20820 " from DIE at %s [in module %s]"),
20821 hex_string (signature), sect_offset_str (die->sect_off),
20822 objfile_name (per_objfile->objfile));
20823 type = build_error_marker_type (cu, die);
20824 }
20825
20826 per_objfile->set_type_for_signatured_type (sig_type, type);
20827
20828 return type;
20829 }
20830
20831 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20832 reading in and processing the type unit if necessary. */
20833
20834 static struct type *
20835 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20836 struct dwarf2_cu *cu) /* ARI: editCase function */
20837 {
20838 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20839 if (attr->form_is_ref ())
20840 {
20841 struct dwarf2_cu *type_cu = cu;
20842 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20843
20844 return read_type_die (type_die, type_cu);
20845 }
20846 else if (attr->form == DW_FORM_ref_sig8)
20847 {
20848 return get_signatured_type (die, attr->as_signature (), cu);
20849 }
20850 else
20851 {
20852 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20853
20854 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20855 " at %s [in module %s]"),
20856 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
20857 objfile_name (per_objfile->objfile));
20858 return build_error_marker_type (cu, die);
20859 }
20860 }
20861
20862 /* Load the DIEs associated with type unit PER_CU into memory. */
20863
20864 static void
20865 load_full_type_unit (dwarf2_per_cu_data *per_cu,
20866 dwarf2_per_objfile *per_objfile)
20867 {
20868 struct signatured_type *sig_type;
20869
20870 /* We have the per_cu, but we need the signatured_type.
20871 Fortunately this is an easy translation. */
20872 gdb_assert (per_cu->is_debug_types);
20873 sig_type = (struct signatured_type *) per_cu;
20874
20875 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
20876
20877 read_signatured_type (sig_type, per_objfile);
20878
20879 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
20880 }
20881
20882 /* Read in a signatured type and build its CU and DIEs.
20883 If the type is a stub for the real type in a DWO file,
20884 read in the real type from the DWO file as well. */
20885
20886 static void
20887 read_signatured_type (signatured_type *sig_type,
20888 dwarf2_per_objfile *per_objfile)
20889 {
20890 gdb_assert (sig_type->is_debug_types);
20891 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
20892
20893 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
20894
20895 if (!reader.dummy_p)
20896 {
20897 struct dwarf2_cu *cu = reader.cu;
20898 const gdb_byte *info_ptr = reader.info_ptr;
20899
20900 gdb_assert (cu->die_hash == NULL);
20901 cu->die_hash =
20902 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
20903 die_info::hash,
20904 die_info::eq,
20905 NULL,
20906 &cu->comp_unit_obstack,
20907 hashtab_obstack_allocate,
20908 dummy_obstack_deallocate);
20909
20910 if (reader.comp_unit_die->has_children)
20911 reader.comp_unit_die->child
20912 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
20913 reader.comp_unit_die);
20914 cu->dies = reader.comp_unit_die;
20915 /* comp_unit_die is not stored in die_hash, no need. */
20916
20917 /* We try not to read any attributes in this function, because
20918 not all CUs needed for references have been loaded yet, and
20919 symbol table processing isn't initialized. But we have to
20920 set the CU language, or we won't be able to build types
20921 correctly. Similarly, if we do not read the producer, we can
20922 not apply producer-specific interpretation. */
20923 prepare_one_comp_unit (cu, cu->dies, language_minimal);
20924
20925 reader.keep ();
20926 }
20927
20928 sig_type->tu_read = 1;
20929 }
20930
20931 /* Decode simple location descriptions.
20932
20933 Given a pointer to a DWARF block that defines a location, compute
20934 the location. Returns true if the expression was computable by
20935 this function, false otherwise. On a true return, *RESULT is set.
20936
20937 Note that this function does not implement a full DWARF expression
20938 evaluator. Instead, it is used for a few limited purposes:
20939
20940 - Getting the address of a symbol that has a constant address. For
20941 example, if a symbol has a location like "DW_OP_addr", the address
20942 can be extracted.
20943
20944 - Getting the offset of a virtual function in its vtable. There
20945 are two forms of this, one of which involves DW_OP_deref -- so this
20946 function handles derefs in a peculiar way to make this 'work'.
20947 (Probably this area should be rewritten.)
20948
20949 - Getting the offset of a field, when it is constant.
20950
20951 Opcodes that cannot be part of a constant expression, for example
20952 those involving registers, simply result in a return of
20953 'false'.
20954
20955 This function may emit a complaint. */
20956
20957 static bool
20958 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu,
20959 CORE_ADDR *result)
20960 {
20961 struct objfile *objfile = cu->per_objfile->objfile;
20962 size_t i;
20963 size_t size = blk->size;
20964 const gdb_byte *data = blk->data;
20965 CORE_ADDR stack[64];
20966 int stacki;
20967 unsigned int bytes_read;
20968 gdb_byte op;
20969
20970 *result = 0;
20971 i = 0;
20972 stacki = 0;
20973 stack[stacki] = 0;
20974 stack[++stacki] = 0;
20975
20976 while (i < size)
20977 {
20978 op = data[i++];
20979 switch (op)
20980 {
20981 case DW_OP_lit0:
20982 case DW_OP_lit1:
20983 case DW_OP_lit2:
20984 case DW_OP_lit3:
20985 case DW_OP_lit4:
20986 case DW_OP_lit5:
20987 case DW_OP_lit6:
20988 case DW_OP_lit7:
20989 case DW_OP_lit8:
20990 case DW_OP_lit9:
20991 case DW_OP_lit10:
20992 case DW_OP_lit11:
20993 case DW_OP_lit12:
20994 case DW_OP_lit13:
20995 case DW_OP_lit14:
20996 case DW_OP_lit15:
20997 case DW_OP_lit16:
20998 case DW_OP_lit17:
20999 case DW_OP_lit18:
21000 case DW_OP_lit19:
21001 case DW_OP_lit20:
21002 case DW_OP_lit21:
21003 case DW_OP_lit22:
21004 case DW_OP_lit23:
21005 case DW_OP_lit24:
21006 case DW_OP_lit25:
21007 case DW_OP_lit26:
21008 case DW_OP_lit27:
21009 case DW_OP_lit28:
21010 case DW_OP_lit29:
21011 case DW_OP_lit30:
21012 case DW_OP_lit31:
21013 stack[++stacki] = op - DW_OP_lit0;
21014 break;
21015
21016 case DW_OP_addr:
21017 stack[++stacki]
21018 = (CORE_ADDR) cu->header.read_address (objfile->obfd.get (),
21019 &data[i],
21020 &bytes_read);
21021 i += bytes_read;
21022 break;
21023
21024 case DW_OP_const1u:
21025 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
21026 i += 1;
21027 break;
21028
21029 case DW_OP_const1s:
21030 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
21031 i += 1;
21032 break;
21033
21034 case DW_OP_const2u:
21035 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
21036 i += 2;
21037 break;
21038
21039 case DW_OP_const2s:
21040 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
21041 i += 2;
21042 break;
21043
21044 case DW_OP_const4u:
21045 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
21046 i += 4;
21047 break;
21048
21049 case DW_OP_const4s:
21050 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
21051 i += 4;
21052 break;
21053
21054 case DW_OP_const8u:
21055 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
21056 i += 8;
21057 break;
21058
21059 case DW_OP_constu:
21060 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21061 &bytes_read);
21062 i += bytes_read;
21063 break;
21064
21065 case DW_OP_consts:
21066 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21067 i += bytes_read;
21068 break;
21069
21070 case DW_OP_dup:
21071 stack[stacki + 1] = stack[stacki];
21072 stacki++;
21073 break;
21074
21075 case DW_OP_plus:
21076 stack[stacki - 1] += stack[stacki];
21077 stacki--;
21078 break;
21079
21080 case DW_OP_plus_uconst:
21081 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21082 &bytes_read);
21083 i += bytes_read;
21084 break;
21085
21086 case DW_OP_minus:
21087 stack[stacki - 1] -= stack[stacki];
21088 stacki--;
21089 break;
21090
21091 case DW_OP_deref:
21092 /* If we're not the last op, then we definitely can't encode
21093 this using GDB's address_class enum. This is valid for partial
21094 global symbols, although the variable's address will be bogus
21095 in the psymtab. */
21096 if (i < size)
21097 return false;
21098 break;
21099
21100 case DW_OP_addrx:
21101 case DW_OP_GNU_addr_index:
21102 case DW_OP_GNU_const_index:
21103 stack[++stacki]
21104 = (CORE_ADDR) read_addr_index_from_leb128 (cu, &data[i],
21105 &bytes_read);
21106 i += bytes_read;
21107 break;
21108
21109 default:
21110 return false;
21111 }
21112
21113 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21114 outside of the allocated space. Also enforce minimum>0. */
21115 if (stacki >= ARRAY_SIZE (stack) - 1)
21116 {
21117 complaint (_("location description stack overflow"));
21118 return false;
21119 }
21120
21121 if (stacki <= 0)
21122 {
21123 complaint (_("location description stack underflow"));
21124 return false;
21125 }
21126 }
21127
21128 *result = stack[stacki];
21129 return true;
21130 }
21131
21132 /* memory allocation interface */
21133
21134 static struct dwarf_block *
21135 dwarf_alloc_block (struct dwarf2_cu *cu)
21136 {
21137 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21138 }
21139
21140 \f
21141
21142 /* Macro support. */
21143
21144 /* An overload of dwarf_decode_macros that finds the correct section
21145 and ensures it is read in before calling the other overload. */
21146
21147 static void
21148 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
21149 int section_is_gnu)
21150 {
21151 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21152 struct objfile *objfile = per_objfile->objfile;
21153 const struct line_header *lh = cu->line_header;
21154 unsigned int offset_size = cu->header.offset_size;
21155 struct dwarf2_section_info *section;
21156 const char *section_name;
21157
21158 if (cu->dwo_unit != nullptr)
21159 {
21160 if (section_is_gnu)
21161 {
21162 section = &cu->dwo_unit->dwo_file->sections.macro;
21163 section_name = ".debug_macro.dwo";
21164 }
21165 else
21166 {
21167 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21168 section_name = ".debug_macinfo.dwo";
21169 }
21170 }
21171 else
21172 {
21173 if (section_is_gnu)
21174 {
21175 section = &per_objfile->per_bfd->macro;
21176 section_name = ".debug_macro";
21177 }
21178 else
21179 {
21180 section = &per_objfile->per_bfd->macinfo;
21181 section_name = ".debug_macinfo";
21182 }
21183 }
21184
21185 section->read (objfile);
21186 if (section->buffer == nullptr)
21187 {
21188 complaint (_("missing %s section"), section_name);
21189 return;
21190 }
21191
21192 buildsym_compunit *builder = cu->get_builder ();
21193
21194 struct dwarf2_section_info *str_offsets_section;
21195 struct dwarf2_section_info *str_section;
21196 std::optional<ULONGEST> str_offsets_base;
21197
21198 if (cu->dwo_unit != nullptr)
21199 {
21200 str_offsets_section = &cu->dwo_unit->dwo_file
21201 ->sections.str_offsets;
21202 str_section = &cu->dwo_unit->dwo_file->sections.str;
21203 str_offsets_base = cu->header.addr_size;
21204 }
21205 else
21206 {
21207 str_offsets_section = &per_objfile->per_bfd->str_offsets;
21208 str_section = &per_objfile->per_bfd->str;
21209 str_offsets_base = cu->str_offsets_base;
21210 }
21211
21212 dwarf_decode_macros (per_objfile, builder, section, lh,
21213 offset_size, offset, str_section, str_offsets_section,
21214 str_offsets_base, section_is_gnu, cu);
21215 }
21216
21217 /* Return the .debug_loc section to use for CU.
21218 For DWO files use .debug_loc.dwo. */
21219
21220 static struct dwarf2_section_info *
21221 cu_debug_loc_section (struct dwarf2_cu *cu)
21222 {
21223 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21224
21225 if (cu->dwo_unit)
21226 {
21227 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21228
21229 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
21230 }
21231 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
21232 : &per_objfile->per_bfd->loc);
21233 }
21234
21235 /* Return the .debug_rnglists section to use for CU. */
21236 static struct dwarf2_section_info *
21237 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
21238 {
21239 if (cu->header.version < 5)
21240 error (_(".debug_rnglists section cannot be used in DWARF %d"),
21241 cu->header.version);
21242 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21243
21244 /* Make sure we read the .debug_rnglists section from the file that
21245 contains the DW_AT_ranges attribute we are reading. Normally that
21246 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
21247 or DW_TAG_skeleton unit, we always want to read from objfile/linked
21248 program. */
21249 if (cu->dwo_unit != nullptr
21250 && tag != DW_TAG_compile_unit
21251 && tag != DW_TAG_skeleton_unit)
21252 {
21253 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21254
21255 if (sections->rnglists.size > 0)
21256 return &sections->rnglists;
21257 else
21258 error (_(".debug_rnglists section is missing from .dwo file."));
21259 }
21260 return &dwarf2_per_objfile->per_bfd->rnglists;
21261 }
21262
21263 /* A helper function that fills in a dwarf2_loclist_baton. */
21264
21265 static void
21266 fill_in_loclist_baton (struct dwarf2_cu *cu,
21267 struct dwarf2_loclist_baton *baton,
21268 const struct attribute *attr)
21269 {
21270 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21271 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21272
21273 section->read (per_objfile->objfile);
21274
21275 baton->per_objfile = per_objfile;
21276 baton->per_cu = cu->per_cu;
21277 gdb_assert (baton->per_cu);
21278 /* We don't know how long the location list is, but make sure we
21279 don't run off the edge of the section. */
21280 baton->size = section->size - attr->as_unsigned ();
21281 baton->data = section->buffer + attr->as_unsigned ();
21282 if (cu->base_address.has_value ())
21283 baton->base_address = *cu->base_address;
21284 else
21285 baton->base_address = {};
21286 baton->from_dwo = cu->dwo_unit != NULL;
21287 }
21288
21289 static void
21290 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
21291 struct dwarf2_cu *cu, int is_block)
21292 {
21293 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21294 struct objfile *objfile = per_objfile->objfile;
21295 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21296
21297 if (attr->form_is_section_offset ()
21298 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21299 the section. If so, fall through to the complaint in the
21300 other branch. */
21301 && attr->as_unsigned () < section->get_size (objfile))
21302 {
21303 struct dwarf2_loclist_baton *baton;
21304
21305 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
21306
21307 fill_in_loclist_baton (cu, baton, attr);
21308
21309 if (!cu->base_address.has_value ())
21310 complaint (_("Location list used without "
21311 "specifying the CU base address."));
21312
21313 sym->set_aclass_index ((is_block
21314 ? dwarf2_loclist_block_index
21315 : dwarf2_loclist_index));
21316 SYMBOL_LOCATION_BATON (sym) = baton;
21317 }
21318 else
21319 {
21320 struct dwarf2_locexpr_baton *baton;
21321
21322 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
21323 baton->per_objfile = per_objfile;
21324 baton->per_cu = cu->per_cu;
21325 gdb_assert (baton->per_cu);
21326
21327 if (attr->form_is_block ())
21328 {
21329 /* Note that we're just copying the block's data pointer
21330 here, not the actual data. We're still pointing into the
21331 info_buffer for SYM's objfile; right now we never release
21332 that buffer, but when we do clean up properly this may
21333 need to change. */
21334 struct dwarf_block *block = attr->as_block ();
21335 baton->size = block->size;
21336 baton->data = block->data;
21337 }
21338 else
21339 {
21340 dwarf2_invalid_attrib_class_complaint ("location description",
21341 sym->natural_name ());
21342 baton->size = 0;
21343 }
21344
21345 sym->set_aclass_index ((is_block
21346 ? dwarf2_locexpr_block_index
21347 : dwarf2_locexpr_index));
21348 SYMBOL_LOCATION_BATON (sym) = baton;
21349 }
21350 }
21351
21352 /* See read.h. */
21353
21354 const comp_unit_head *
21355 dwarf2_per_cu_data::get_header () const
21356 {
21357 if (!m_header_read_in)
21358 {
21359 const gdb_byte *info_ptr
21360 = this->section->buffer + to_underlying (this->sect_off);
21361
21362 read_comp_unit_head (&m_header, info_ptr, this->section,
21363 rcuh_kind::COMPILE);
21364
21365 m_header_read_in = true;
21366 }
21367
21368 return &m_header;
21369 }
21370
21371 /* See read.h. */
21372
21373 int
21374 dwarf2_per_cu_data::addr_size () const
21375 {
21376 return this->get_header ()->addr_size;
21377 }
21378
21379 /* See read.h. */
21380
21381 int
21382 dwarf2_per_cu_data::offset_size () const
21383 {
21384 return this->get_header ()->offset_size;
21385 }
21386
21387 /* See read.h. */
21388
21389 int
21390 dwarf2_per_cu_data::ref_addr_size () const
21391 {
21392 const comp_unit_head *header = this->get_header ();
21393
21394 if (header->version == 2)
21395 return header->addr_size;
21396 else
21397 return header->offset_size;
21398 }
21399
21400 /* See read.h. */
21401
21402 void
21403 dwarf2_per_cu_data::set_lang (enum language lang,
21404 dwarf_source_language dw_lang)
21405 {
21406 if (unit_type () == DW_UT_partial)
21407 return;
21408
21409 /* Set if not set already. */
21410 packed<language, LANGUAGE_BYTES> new_value = lang;
21411 packed<language, LANGUAGE_BYTES> old_value = m_lang.exchange (new_value);
21412 /* If already set, verify that it's the same value. */
21413 gdb_assert (old_value == language_unknown || old_value == lang);
21414
21415 packed<dwarf_source_language, 2> new_dw = dw_lang;
21416 packed<dwarf_source_language, 2> old_dw = m_dw_lang.exchange (new_dw);
21417 gdb_assert (old_dw == 0 || old_dw == dw_lang);
21418 }
21419
21420 /* A helper function for dwarf2_find_containing_comp_unit that returns
21421 the index of the result, and that searches a vector. It will
21422 return a result even if the offset in question does not actually
21423 occur in any CU. This is separate so that it can be unit
21424 tested. */
21425
21426 static int
21427 dwarf2_find_containing_comp_unit
21428 (sect_offset sect_off,
21429 unsigned int offset_in_dwz,
21430 const std::vector<dwarf2_per_cu_data_up> &all_units)
21431 {
21432 int low, high;
21433
21434 low = 0;
21435 high = all_units.size () - 1;
21436 while (high > low)
21437 {
21438 struct dwarf2_per_cu_data *mid_cu;
21439 int mid = low + (high - low) / 2;
21440
21441 mid_cu = all_units[mid].get ();
21442 if (mid_cu->is_dwz > offset_in_dwz
21443 || (mid_cu->is_dwz == offset_in_dwz
21444 && mid_cu->sect_off + mid_cu->length () > sect_off))
21445 high = mid;
21446 else
21447 low = mid + 1;
21448 }
21449 gdb_assert (low == high);
21450 return low;
21451 }
21452
21453 /* Locate the .debug_info compilation unit from CU's objfile which contains
21454 the DIE at OFFSET. Raises an error on failure. */
21455
21456 static struct dwarf2_per_cu_data *
21457 dwarf2_find_containing_comp_unit (sect_offset sect_off,
21458 unsigned int offset_in_dwz,
21459 dwarf2_per_bfd *per_bfd)
21460 {
21461 int low = dwarf2_find_containing_comp_unit
21462 (sect_off, offset_in_dwz, per_bfd->all_units);
21463 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
21464
21465 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
21466 {
21467 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
21468 error (_("Dwarf Error: could not find CU containing "
21469 "offset %s [in module %s]"),
21470 sect_offset_str (sect_off),
21471 bfd_get_filename (per_bfd->obfd));
21472
21473 gdb_assert (per_bfd->all_units[low-1]->sect_off
21474 <= sect_off);
21475 return per_bfd->all_units[low - 1].get ();
21476 }
21477 else
21478 {
21479 if (low == per_bfd->all_units.size () - 1
21480 && sect_off >= this_cu->sect_off + this_cu->length ())
21481 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
21482 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
21483 return this_cu;
21484 }
21485 }
21486
21487 #if GDB_SELF_TEST
21488
21489 namespace selftests {
21490 namespace find_containing_comp_unit {
21491
21492 static void
21493 run_test ()
21494 {
21495 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
21496 dwarf2_per_cu_data *one_ptr = one.get ();
21497 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
21498 dwarf2_per_cu_data *two_ptr = two.get ();
21499 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
21500 dwarf2_per_cu_data *three_ptr = three.get ();
21501 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
21502 dwarf2_per_cu_data *four_ptr = four.get ();
21503
21504 one->set_length (5);
21505 two->sect_off = sect_offset (one->length ());
21506 two->set_length (7);
21507
21508 three->set_length (5);
21509 three->is_dwz = 1;
21510 four->sect_off = sect_offset (three->length ());
21511 four->set_length (7);
21512 four->is_dwz = 1;
21513
21514 std::vector<dwarf2_per_cu_data_up> units;
21515 units.push_back (std::move (one));
21516 units.push_back (std::move (two));
21517 units.push_back (std::move (three));
21518 units.push_back (std::move (four));
21519
21520 int result;
21521
21522 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
21523 SELF_CHECK (units[result].get () == one_ptr);
21524 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
21525 SELF_CHECK (units[result].get () == one_ptr);
21526 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
21527 SELF_CHECK (units[result].get () == two_ptr);
21528
21529 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
21530 SELF_CHECK (units[result].get () == three_ptr);
21531 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
21532 SELF_CHECK (units[result].get () == three_ptr);
21533 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
21534 SELF_CHECK (units[result].get () == four_ptr);
21535 }
21536
21537 }
21538 }
21539
21540 #endif /* GDB_SELF_TEST */
21541
21542 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
21543
21544 static void
21545 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
21546 enum language pretend_language)
21547 {
21548 struct attribute *attr;
21549
21550 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
21551
21552 /* Set the language we're debugging. */
21553 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
21554 enum language lang;
21555 dwarf_source_language dw_lang = (dwarf_source_language) 0;
21556 if (cu->producer != nullptr
21557 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
21558 {
21559 /* The XLCL doesn't generate DW_LANG_OpenCL because this
21560 attribute is not standardised yet. As a workaround for the
21561 language detection we fall back to the DW_AT_producer
21562 string. */
21563 lang = language_opencl;
21564 dw_lang = DW_LANG_OpenCL;
21565 }
21566 else if (cu->producer != nullptr
21567 && strstr (cu->producer, "GNU Go ") != NULL)
21568 {
21569 /* Similar hack for Go. */
21570 lang = language_go;
21571 dw_lang = DW_LANG_Go;
21572 }
21573 else if (attr != nullptr)
21574 {
21575 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
21576 dw_lang = (dwarf_source_language) attr->constant_value (0);
21577 }
21578 else
21579 lang = pretend_language;
21580
21581 cu->language_defn = language_def (lang);
21582
21583 switch (comp_unit_die->tag)
21584 {
21585 case DW_TAG_compile_unit:
21586 cu->per_cu->set_unit_type (DW_UT_compile);
21587 break;
21588 case DW_TAG_partial_unit:
21589 cu->per_cu->set_unit_type (DW_UT_partial);
21590 break;
21591 case DW_TAG_type_unit:
21592 cu->per_cu->set_unit_type (DW_UT_type);
21593 break;
21594 default:
21595 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
21596 dwarf_tag_name (comp_unit_die->tag),
21597 sect_offset_str (cu->per_cu->sect_off));
21598 }
21599
21600 cu->per_cu->set_lang (lang, dw_lang);
21601 }
21602
21603 /* See read.h. */
21604
21605 dwarf2_cu *
21606 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
21607 {
21608 auto it = m_dwarf2_cus.find (per_cu);
21609 if (it == m_dwarf2_cus.end ())
21610 return nullptr;
21611
21612 return it->second.get ();
21613 }
21614
21615 /* See read.h. */
21616
21617 void
21618 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
21619 std::unique_ptr<dwarf2_cu> cu)
21620 {
21621 gdb_assert (this->get_cu (per_cu) == nullptr);
21622
21623 m_dwarf2_cus[per_cu] = std::move (cu);
21624 }
21625
21626 /* See read.h. */
21627
21628 void
21629 dwarf2_per_objfile::age_comp_units ()
21630 {
21631 dwarf_read_debug_printf_v ("running");
21632
21633 /* This is not expected to be called in the middle of CU expansion. There is
21634 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
21635 loaded in memory. Calling age_comp_units while the queue is in use could
21636 make us free the DIEs for a CU that is in the queue and therefore break
21637 that invariant. */
21638 gdb_assert (!queue.has_value ());
21639
21640 /* Start by clearing all marks. */
21641 for (const auto &pair : m_dwarf2_cus)
21642 pair.second->clear_mark ();
21643
21644 /* Traverse all CUs, mark them and their dependencies if used recently
21645 enough. */
21646 for (const auto &pair : m_dwarf2_cus)
21647 {
21648 dwarf2_cu *cu = pair.second.get ();
21649
21650 cu->last_used++;
21651 if (cu->last_used <= dwarf_max_cache_age)
21652 cu->mark ();
21653 }
21654
21655 /* Delete all CUs still not marked. */
21656 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
21657 {
21658 dwarf2_cu *cu = it->second.get ();
21659
21660 if (!cu->is_marked ())
21661 {
21662 dwarf_read_debug_printf_v ("deleting old CU %s",
21663 sect_offset_str (cu->per_cu->sect_off));
21664 it = m_dwarf2_cus.erase (it);
21665 }
21666 else
21667 it++;
21668 }
21669 }
21670
21671 /* See read.h. */
21672
21673 void
21674 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
21675 {
21676 auto it = m_dwarf2_cus.find (per_cu);
21677 if (it == m_dwarf2_cus.end ())
21678 return;
21679
21680 m_dwarf2_cus.erase (it);
21681 }
21682
21683 dwarf2_per_objfile::~dwarf2_per_objfile ()
21684 {
21685 remove_all_cus ();
21686 }
21687
21688 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21689 We store these in a hash table separate from the DIEs, and preserve them
21690 when the DIEs are flushed out of cache.
21691
21692 The CU "per_cu" pointer is needed because offset alone is not enough to
21693 uniquely identify the type. A file may have multiple .debug_types sections,
21694 or the type may come from a DWO file. Furthermore, while it's more logical
21695 to use per_cu->section+offset, with Fission the section with the data is in
21696 the DWO file but we don't know that section at the point we need it.
21697 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21698 because we can enter the lookup routine, get_die_type_at_offset, from
21699 outside this file, and thus won't necessarily have PER_CU->cu.
21700 Fortunately, PER_CU is stable for the life of the objfile. */
21701
21702 struct dwarf2_per_cu_offset_and_type
21703 {
21704 const struct dwarf2_per_cu_data *per_cu;
21705 sect_offset sect_off;
21706 struct type *type;
21707 };
21708
21709 /* Hash function for a dwarf2_per_cu_offset_and_type. */
21710
21711 static hashval_t
21712 per_cu_offset_and_type_hash (const void *item)
21713 {
21714 const struct dwarf2_per_cu_offset_and_type *ofs
21715 = (const struct dwarf2_per_cu_offset_and_type *) item;
21716
21717 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
21718 }
21719
21720 /* Equality function for a dwarf2_per_cu_offset_and_type. */
21721
21722 static int
21723 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
21724 {
21725 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
21726 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
21727 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
21728 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
21729
21730 return (ofs_lhs->per_cu == ofs_rhs->per_cu
21731 && ofs_lhs->sect_off == ofs_rhs->sect_off);
21732 }
21733
21734 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21735 table if necessary. For convenience, return TYPE.
21736
21737 The DIEs reading must have careful ordering to:
21738 * Not cause infinite loops trying to read in DIEs as a prerequisite for
21739 reading current DIE.
21740 * Not trying to dereference contents of still incompletely read in types
21741 while reading in other DIEs.
21742 * Enable referencing still incompletely read in types just by a pointer to
21743 the type without accessing its fields.
21744
21745 Therefore caller should follow these rules:
21746 * Try to fetch any prerequisite types we may need to build this DIE type
21747 before building the type and calling set_die_type.
21748 * After building type call set_die_type for current DIE as soon as
21749 possible before fetching more types to complete the current type.
21750 * Make the type as complete as possible before fetching more types. */
21751
21752 static struct type *
21753 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21754 bool skip_data_location)
21755 {
21756 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21757 struct dwarf2_per_cu_offset_and_type **slot, ofs;
21758 struct objfile *objfile = per_objfile->objfile;
21759 struct attribute *attr;
21760 struct dynamic_prop prop;
21761
21762 /* For Ada types, make sure that the gnat-specific data is always
21763 initialized (if not already set). There are a few types where
21764 we should not be doing so, because the type-specific area is
21765 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21766 where the type-specific area is used to store the floatformat).
21767 But this is not a problem, because the gnat-specific information
21768 is actually not needed for these types. */
21769 if (need_gnat_info (cu)
21770 && type->code () != TYPE_CODE_FUNC
21771 && type->code () != TYPE_CODE_FLT
21772 && type->code () != TYPE_CODE_METHODPTR
21773 && type->code () != TYPE_CODE_MEMBERPTR
21774 && type->code () != TYPE_CODE_METHOD
21775 && type->code () != TYPE_CODE_FIXED_POINT
21776 && !HAVE_GNAT_AUX_INFO (type))
21777 INIT_GNAT_SPECIFIC (type);
21778
21779 /* Read DW_AT_allocated and set in type. */
21780 attr = dwarf2_attr (die, DW_AT_allocated, cu);
21781 if (attr != NULL)
21782 {
21783 struct type *prop_type = cu->addr_sized_int_type (false);
21784 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21785 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
21786 }
21787
21788 /* Read DW_AT_associated and set in type. */
21789 attr = dwarf2_attr (die, DW_AT_associated, cu);
21790 if (attr != NULL)
21791 {
21792 struct type *prop_type = cu->addr_sized_int_type (false);
21793 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21794 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
21795 }
21796
21797 /* Read DW_AT_rank and set in type. */
21798 attr = dwarf2_attr (die, DW_AT_rank, cu);
21799 if (attr != NULL)
21800 {
21801 struct type *prop_type = cu->addr_sized_int_type (false);
21802 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21803 type->add_dyn_prop (DYN_PROP_RANK, prop);
21804 }
21805
21806 /* Read DW_AT_data_location and set in type. */
21807 if (!skip_data_location)
21808 {
21809 attr = dwarf2_attr (die, DW_AT_data_location, cu);
21810 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
21811 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
21812 }
21813
21814 if (per_objfile->die_type_hash == NULL)
21815 per_objfile->die_type_hash
21816 = htab_up (htab_create_alloc (127,
21817 per_cu_offset_and_type_hash,
21818 per_cu_offset_and_type_eq,
21819 NULL, xcalloc, xfree));
21820
21821 ofs.per_cu = cu->per_cu;
21822 ofs.sect_off = die->sect_off;
21823 ofs.type = type;
21824 slot = (struct dwarf2_per_cu_offset_and_type **)
21825 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
21826 if (*slot)
21827 complaint (_("A problem internal to GDB: DIE %s has type already set"),
21828 sect_offset_str (die->sect_off));
21829 *slot = XOBNEW (&objfile->objfile_obstack,
21830 struct dwarf2_per_cu_offset_and_type);
21831 **slot = ofs;
21832 return type;
21833 }
21834
21835 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
21836 or return NULL if the die does not have a saved type. */
21837
21838 static struct type *
21839 get_die_type_at_offset (sect_offset sect_off,
21840 dwarf2_per_cu_data *per_cu,
21841 dwarf2_per_objfile *per_objfile)
21842 {
21843 struct dwarf2_per_cu_offset_and_type *slot, ofs;
21844
21845 if (per_objfile->die_type_hash == NULL)
21846 return NULL;
21847
21848 ofs.per_cu = per_cu;
21849 ofs.sect_off = sect_off;
21850 slot = ((struct dwarf2_per_cu_offset_and_type *)
21851 htab_find (per_objfile->die_type_hash.get (), &ofs));
21852 if (slot)
21853 return slot->type;
21854 else
21855 return NULL;
21856 }
21857
21858 /* Look up the type for DIE in CU in die_type_hash,
21859 or return NULL if DIE does not have a saved type. */
21860
21861 static struct type *
21862 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
21863 {
21864 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
21865 }
21866
21867 struct cmd_list_element *set_dwarf_cmdlist;
21868 struct cmd_list_element *show_dwarf_cmdlist;
21869
21870 static void
21871 show_check_physname (struct ui_file *file, int from_tty,
21872 struct cmd_list_element *c, const char *value)
21873 {
21874 gdb_printf (file,
21875 _("Whether to check \"physname\" is %s.\n"),
21876 value);
21877 }
21878
21879 void _initialize_dwarf2_read ();
21880 void
21881 _initialize_dwarf2_read ()
21882 {
21883 add_setshow_prefix_cmd ("dwarf", class_maintenance,
21884 _("\
21885 Set DWARF specific variables.\n\
21886 Configure DWARF variables such as the cache size."),
21887 _("\
21888 Show DWARF specific variables.\n\
21889 Show DWARF variables such as the cache size."),
21890 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
21891 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
21892
21893 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21894 &dwarf_max_cache_age, _("\
21895 Set the upper bound on the age of cached DWARF compilation units."), _("\
21896 Show the upper bound on the age of cached DWARF compilation units."), _("\
21897 A higher limit means that cached compilation units will be stored\n\
21898 in memory longer, and more total memory will be used. Zero disables\n\
21899 caching, which can slow down startup."),
21900 NULL,
21901 show_dwarf_max_cache_age,
21902 &set_dwarf_cmdlist,
21903 &show_dwarf_cmdlist);
21904
21905 add_setshow_boolean_cmd ("synchronous", class_obscure,
21906 &dwarf_synchronous, _("\
21907 Set whether DWARF is read synchronously."), _("\
21908 Show whether DWARF is read synchronously."), _("\
21909 By default, DWARF information is read in worker threads,\n\
21910 and gdb will not generally wait for the reading to complete\n\
21911 before continuing with other work, for example presenting a\n\
21912 prompt to the user.\n\
21913 Enabling this setting will cause the DWARF reader to always wait\n\
21914 for debug info processing to be finished before gdb can proceed."),
21915 nullptr,
21916 show_dwarf_synchronous,
21917 &set_dwarf_cmdlist,
21918 &show_dwarf_cmdlist);
21919
21920 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
21921 Set debugging of the DWARF reader."), _("\
21922 Show debugging of the DWARF reader."), _("\
21923 When enabled (non-zero), debugging messages are printed during DWARF\n\
21924 reading and symtab expansion. A value of 1 (one) provides basic\n\
21925 information. A value greater than 1 provides more verbose information."),
21926 NULL,
21927 NULL,
21928 &setdebuglist, &showdebuglist);
21929
21930 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
21931 Set debugging of the DWARF DIE reader."), _("\
21932 Show debugging of the DWARF DIE reader."), _("\
21933 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21934 The value is the maximum depth to print."),
21935 NULL,
21936 NULL,
21937 &setdebuglist, &showdebuglist);
21938
21939 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
21940 Set debugging of the dwarf line reader."), _("\
21941 Show debugging of the dwarf line reader."), _("\
21942 When enabled (non-zero), line number entries are dumped as they are read in.\n\
21943 A value of 1 (one) provides basic information.\n\
21944 A value greater than 1 provides more verbose information."),
21945 NULL,
21946 NULL,
21947 &setdebuglist, &showdebuglist);
21948
21949 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21950 Set cross-checking of \"physname\" code against demangler."), _("\
21951 Show cross-checking of \"physname\" code against demangler."), _("\
21952 When enabled, GDB's internal \"physname\" code is checked against\n\
21953 the demangler."),
21954 NULL, show_check_physname,
21955 &setdebuglist, &showdebuglist);
21956
21957 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21958 &dwarf2_locexpr_funcs);
21959 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21960 &dwarf2_loclist_funcs);
21961 ada_imported_index = register_symbol_computed_impl (LOC_COMPUTED,
21962 &ada_imported_funcs);
21963
21964 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21965 &dwarf2_block_frame_base_locexpr_funcs);
21966 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21967 &dwarf2_block_frame_base_loclist_funcs);
21968 ada_block_index = register_symbol_block_impl (LOC_BLOCK,
21969 &ada_function_alias_funcs);
21970
21971 #if GDB_SELF_TEST
21972 selftests::register_test ("dw2_expand_symtabs_matching",
21973 selftests::dw2_expand_symtabs_matching::run_test);
21974 selftests::register_test ("dwarf2_find_containing_comp_unit",
21975 selftests::find_containing_comp_unit::run_test);
21976 #endif
21977 }