]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Convert more block functions to methods
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2023 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/read-debug-names.h"
45 #include "dwarf2/read-gdb-index.h"
46 #include "dwarf2/sect-names.h"
47 #include "dwarf2/stringify.h"
48 #include "dwarf2/public.h"
49 #include "bfd.h"
50 #include "elf-bfd.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" /* for DOSish file names */
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 "gdbcmd.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" /* for gnutarget */
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 "gdbsupport/gdb_optional.h"
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/parallel-for.h"
97 #include "gdbsupport/thread-pool.h"
98
99 /* When == 1, print basic high level tracing messages.
100 When > 1, be more verbose.
101 This is in contrast to the low level DIE reading of dwarf_die_debug. */
102 static unsigned int dwarf_read_debug = 0;
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
105
106 #define dwarf_read_debug_printf(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
111
112 #define dwarf_read_debug_printf_v(fmt, ...) \
113 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
114 ##__VA_ARGS__)
115
116 /* When non-zero, dump DIEs after they are read in. */
117 static unsigned int dwarf_die_debug = 0;
118
119 /* When non-zero, dump line number entries as they are read in. */
120 unsigned int dwarf_line_debug = 0;
121
122 /* When true, cross-check physname against demangler. */
123 static bool check_physname = false;
124
125 /* This is used to store the data that is always per objfile. */
126 static const registry<objfile>::key<dwarf2_per_objfile>
127 dwarf2_objfile_data_key;
128
129 /* These are used to store the dwarf2_per_bfd objects.
130
131 objfiles having the same BFD, which doesn't require relocations, are going to
132 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
133
134 Other objfiles are not going to share a dwarf2_per_bfd with any other
135 objfiles, so they'll have their own version kept in the _objfile_data_key
136 version. */
137 static const registry<bfd>::key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
138 static const registry<objfile>::key<dwarf2_per_bfd>
139 dwarf2_per_bfd_objfile_data_key;
140
141 /* The "aclass" indices for various kinds of computed DWARF symbols. */
142
143 static int dwarf2_locexpr_index;
144 static int dwarf2_loclist_index;
145 static int dwarf2_locexpr_block_index;
146 static int dwarf2_loclist_block_index;
147
148 /* Size of .debug_loclists section header for 32-bit DWARF format. */
149 #define LOCLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_loclists section header for 64-bit DWARF format. */
152 #define LOCLIST_HEADER_SIZE64 20
153
154 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
155 #define RNGLIST_HEADER_SIZE32 12
156
157 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
158 #define RNGLIST_HEADER_SIZE64 20
159
160 /* See dwarf2/read.h. */
161
162 dwarf2_per_objfile *
163 get_dwarf2_per_objfile (struct objfile *objfile)
164 {
165 return dwarf2_objfile_data_key.get (objfile);
166 }
167
168 /* Default names of the debugging sections. */
169
170 /* Note that if the debugging section has been compressed, it might
171 have a name like .zdebug_info. */
172
173 const struct dwarf2_debug_sections dwarf2_elf_names =
174 {
175 { ".debug_info", ".zdebug_info" },
176 { ".debug_abbrev", ".zdebug_abbrev" },
177 { ".debug_line", ".zdebug_line" },
178 { ".debug_loc", ".zdebug_loc" },
179 { ".debug_loclists", ".zdebug_loclists" },
180 { ".debug_macinfo", ".zdebug_macinfo" },
181 { ".debug_macro", ".zdebug_macro" },
182 { ".debug_str", ".zdebug_str" },
183 { ".debug_str_offsets", ".zdebug_str_offsets" },
184 { ".debug_line_str", ".zdebug_line_str" },
185 { ".debug_ranges", ".zdebug_ranges" },
186 { ".debug_rnglists", ".zdebug_rnglists" },
187 { ".debug_types", ".zdebug_types" },
188 { ".debug_addr", ".zdebug_addr" },
189 { ".debug_frame", ".zdebug_frame" },
190 { ".eh_frame", NULL },
191 { ".gdb_index", ".zgdb_index" },
192 { ".debug_names", ".zdebug_names" },
193 { ".debug_aranges", ".zdebug_aranges" },
194 23
195 };
196
197 /* List of DWO/DWP sections. */
198
199 static const struct dwop_section_names
200 {
201 struct dwarf2_section_names abbrev_dwo;
202 struct dwarf2_section_names info_dwo;
203 struct dwarf2_section_names line_dwo;
204 struct dwarf2_section_names loc_dwo;
205 struct dwarf2_section_names loclists_dwo;
206 struct dwarf2_section_names macinfo_dwo;
207 struct dwarf2_section_names macro_dwo;
208 struct dwarf2_section_names rnglists_dwo;
209 struct dwarf2_section_names str_dwo;
210 struct dwarf2_section_names str_offsets_dwo;
211 struct dwarf2_section_names types_dwo;
212 struct dwarf2_section_names cu_index;
213 struct dwarf2_section_names tu_index;
214 }
215 dwop_section_names =
216 {
217 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
218 { ".debug_info.dwo", ".zdebug_info.dwo" },
219 { ".debug_line.dwo", ".zdebug_line.dwo" },
220 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
221 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
222 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
223 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
224 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
225 { ".debug_str.dwo", ".zdebug_str.dwo" },
226 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
227 { ".debug_types.dwo", ".zdebug_types.dwo" },
228 { ".debug_cu_index", ".zdebug_cu_index" },
229 { ".debug_tu_index", ".zdebug_tu_index" },
230 };
231
232 /* local data types */
233
234 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
235 begin with a header, which contains the following information. */
236 struct loclists_rnglists_header
237 {
238 /* A 4-byte or 12-byte length containing the length of the
239 set of entries for this compilation unit, not including the
240 length field itself. */
241 unsigned int length;
242
243 /* A 2-byte version identifier. */
244 short version;
245
246 /* A 1-byte unsigned integer containing the size in bytes of an address on
247 the target system. */
248 unsigned char addr_size;
249
250 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
251 on the target system. */
252 unsigned char segment_collector_size;
253
254 /* A 4-byte count of the number of offsets that follow the header. */
255 unsigned int offset_entry_count;
256 };
257
258 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
259 This includes type_unit_group and quick_file_names. */
260
261 struct stmt_list_hash
262 {
263 /* The DWO unit this table is from or NULL if there is none. */
264 struct dwo_unit *dwo_unit;
265
266 /* Offset in .debug_line or .debug_line.dwo. */
267 sect_offset line_sect_off;
268 };
269
270 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
271 an object of this type. This contains elements of type unit groups
272 that can be shared across objfiles. The non-shareable parts are in
273 type_unit_group_unshareable. */
274
275 struct type_unit_group
276 {
277 /* The data used to construct the hash key. */
278 struct stmt_list_hash hash {};
279 };
280
281 /* These sections are what may appear in a (real or virtual) DWO file. */
282
283 struct dwo_sections
284 {
285 struct dwarf2_section_info abbrev;
286 struct dwarf2_section_info line;
287 struct dwarf2_section_info loc;
288 struct dwarf2_section_info loclists;
289 struct dwarf2_section_info macinfo;
290 struct dwarf2_section_info macro;
291 struct dwarf2_section_info rnglists;
292 struct dwarf2_section_info str;
293 struct dwarf2_section_info str_offsets;
294 /* In the case of a virtual DWO file, these two are unused. */
295 struct dwarf2_section_info info;
296 std::vector<dwarf2_section_info> types;
297 };
298
299 /* CUs/TUs in DWP/DWO files. */
300
301 struct dwo_unit
302 {
303 /* Backlink to the containing struct dwo_file. */
304 struct dwo_file *dwo_file;
305
306 /* The "id" that distinguishes this CU/TU.
307 .debug_info calls this "dwo_id", .debug_types calls this "signature".
308 Since signatures came first, we stick with it for consistency. */
309 ULONGEST signature;
310
311 /* The section this CU/TU lives in, in the DWO file. */
312 struct dwarf2_section_info *section;
313
314 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
315 sect_offset sect_off;
316 unsigned int length;
317
318 /* For types, offset in the type's DIE of the type defined by this TU. */
319 cu_offset type_offset_in_tu;
320 };
321
322 /* include/dwarf2.h defines the DWP section codes.
323 It defines a max value but it doesn't define a min value, which we
324 use for error checking, so provide one. */
325
326 enum dwp_v2_section_ids
327 {
328 DW_SECT_MIN = 1
329 };
330
331 /* Data for one DWO file.
332
333 This includes virtual DWO files (a virtual DWO file is a DWO file as it
334 appears in a DWP file). DWP files don't really have DWO files per se -
335 comdat folding of types "loses" the DWO file they came from, and from
336 a high level view DWP files appear to contain a mass of random types.
337 However, to maintain consistency with the non-DWP case we pretend DWP
338 files contain virtual DWO files, and we assign each TU with one virtual
339 DWO file (generally based on the line and abbrev section offsets -
340 a heuristic that seems to work in practice). */
341
342 struct dwo_file
343 {
344 dwo_file () = default;
345 DISABLE_COPY_AND_ASSIGN (dwo_file);
346
347 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
348 For virtual DWO files the name is constructed from the section offsets
349 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
350 from related CU+TUs. */
351 const char *dwo_name = nullptr;
352
353 /* The DW_AT_comp_dir attribute. */
354 const char *comp_dir = nullptr;
355
356 /* The bfd, when the file is open. Otherwise this is NULL.
357 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
358 gdb_bfd_ref_ptr dbfd;
359
360 /* The sections that make up this DWO file.
361 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
362 sections (for lack of a better name). */
363 struct dwo_sections sections {};
364
365 /* The CUs in the file.
366 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
367 an extension to handle LLVM's Link Time Optimization output (where
368 multiple source files may be compiled into a single object/dwo pair). */
369 htab_up cus;
370
371 /* Table of TUs in the file.
372 Each element is a struct dwo_unit. */
373 htab_up tus;
374 };
375
376 /* These sections are what may appear in a DWP file. */
377
378 struct dwp_sections
379 {
380 /* These are used by all DWP versions (1, 2 and 5). */
381 struct dwarf2_section_info str;
382 struct dwarf2_section_info cu_index;
383 struct dwarf2_section_info tu_index;
384
385 /* These are only used by DWP version 2 and version 5 files.
386 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
387 sections are referenced by section number, and are not recorded here.
388 In DWP version 2 or 5 there is at most one copy of all these sections,
389 each section being (effectively) comprised of the concatenation of all of
390 the individual sections that exist in the version 1 format.
391 To keep the code simple we treat each of these concatenated pieces as a
392 section itself (a virtual section?). */
393 struct dwarf2_section_info abbrev;
394 struct dwarf2_section_info info;
395 struct dwarf2_section_info line;
396 struct dwarf2_section_info loc;
397 struct dwarf2_section_info loclists;
398 struct dwarf2_section_info macinfo;
399 struct dwarf2_section_info macro;
400 struct dwarf2_section_info rnglists;
401 struct dwarf2_section_info str_offsets;
402 struct dwarf2_section_info types;
403 };
404
405 /* These sections are what may appear in a virtual DWO file in DWP version 1.
406 A virtual DWO file is a DWO file as it appears in a DWP file. */
407
408 struct virtual_v1_dwo_sections
409 {
410 struct dwarf2_section_info abbrev;
411 struct dwarf2_section_info line;
412 struct dwarf2_section_info loc;
413 struct dwarf2_section_info macinfo;
414 struct dwarf2_section_info macro;
415 struct dwarf2_section_info str_offsets;
416 /* Each DWP hash table entry records one CU or one TU.
417 That is recorded here, and copied to dwo_unit.section. */
418 struct dwarf2_section_info info_or_types;
419 };
420
421 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
422 In version 2, the sections of the DWO files are concatenated together
423 and stored in one section of that name. Thus each ELF section contains
424 several "virtual" sections. */
425
426 struct virtual_v2_or_v5_dwo_sections
427 {
428 bfd_size_type abbrev_offset;
429 bfd_size_type abbrev_size;
430
431 bfd_size_type line_offset;
432 bfd_size_type line_size;
433
434 bfd_size_type loc_offset;
435 bfd_size_type loc_size;
436
437 bfd_size_type loclists_offset;
438 bfd_size_type loclists_size;
439
440 bfd_size_type macinfo_offset;
441 bfd_size_type macinfo_size;
442
443 bfd_size_type macro_offset;
444 bfd_size_type macro_size;
445
446 bfd_size_type rnglists_offset;
447 bfd_size_type rnglists_size;
448
449 bfd_size_type str_offsets_offset;
450 bfd_size_type str_offsets_size;
451
452 /* Each DWP hash table entry records one CU or one TU.
453 That is recorded here, and copied to dwo_unit.section. */
454 bfd_size_type info_or_types_offset;
455 bfd_size_type info_or_types_size;
456 };
457
458 /* Contents of DWP hash tables. */
459
460 struct dwp_hash_table
461 {
462 uint32_t version, nr_columns;
463 uint32_t nr_units, nr_slots;
464 const gdb_byte *hash_table, *unit_table;
465 union
466 {
467 struct
468 {
469 const gdb_byte *indices;
470 } v1;
471 struct
472 {
473 /* This is indexed by column number and gives the id of the section
474 in that column. */
475 #define MAX_NR_V2_DWO_SECTIONS \
476 (1 /* .debug_info or .debug_types */ \
477 + 1 /* .debug_abbrev */ \
478 + 1 /* .debug_line */ \
479 + 1 /* .debug_loc */ \
480 + 1 /* .debug_str_offsets */ \
481 + 1 /* .debug_macro or .debug_macinfo */)
482 int section_ids[MAX_NR_V2_DWO_SECTIONS];
483 const gdb_byte *offsets;
484 const gdb_byte *sizes;
485 } v2;
486 struct
487 {
488 /* This is indexed by column number and gives the id of the section
489 in that column. */
490 #define MAX_NR_V5_DWO_SECTIONS \
491 (1 /* .debug_info */ \
492 + 1 /* .debug_abbrev */ \
493 + 1 /* .debug_line */ \
494 + 1 /* .debug_loclists */ \
495 + 1 /* .debug_str_offsets */ \
496 + 1 /* .debug_macro */ \
497 + 1 /* .debug_rnglists */)
498 int section_ids[MAX_NR_V5_DWO_SECTIONS];
499 const gdb_byte *offsets;
500 const gdb_byte *sizes;
501 } v5;
502 } section_pool;
503 };
504
505 /* Data for one DWP file. */
506
507 struct dwp_file
508 {
509 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
510 : name (name_),
511 dbfd (std::move (abfd))
512 {
513 }
514
515 /* Name of the file. */
516 const char *name;
517
518 /* File format version. */
519 int version = 0;
520
521 /* The bfd. */
522 gdb_bfd_ref_ptr dbfd;
523
524 /* Section info for this file. */
525 struct dwp_sections sections {};
526
527 /* Table of CUs in the file. */
528 const struct dwp_hash_table *cus = nullptr;
529
530 /* Table of TUs in the file. */
531 const struct dwp_hash_table *tus = nullptr;
532
533 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
534 htab_up loaded_cus;
535 htab_up loaded_tus;
536
537 /* Table to map ELF section numbers to their sections.
538 This is only needed for the DWP V1 file format. */
539 unsigned int num_sections = 0;
540 asection **elf_sections = nullptr;
541 };
542
543 /* Struct used to pass misc. parameters to read_die_and_children, et
544 al. which are used for both .debug_info and .debug_types dies.
545 All parameters here are unchanging for the life of the call. This
546 struct exists to abstract away the constant parameters of die reading. */
547
548 struct die_reader_specs
549 {
550 /* The bfd of die_section. */
551 bfd *abfd;
552
553 /* The CU of the DIE we are parsing. */
554 struct dwarf2_cu *cu;
555
556 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
557 struct dwo_file *dwo_file;
558
559 /* The section the die comes from.
560 This is either .debug_info or .debug_types, or the .dwo variants. */
561 struct dwarf2_section_info *die_section;
562
563 /* die_section->buffer. */
564 const gdb_byte *buffer;
565
566 /* The end of the buffer. */
567 const gdb_byte *buffer_end;
568
569 /* The abbreviation table to use when reading the DIEs. */
570 struct abbrev_table *abbrev_table;
571 };
572
573 /* A subclass of die_reader_specs that holds storage and has complex
574 constructor and destructor behavior. */
575
576 class cutu_reader : public die_reader_specs
577 {
578 public:
579
580 cutu_reader (dwarf2_per_cu_data *this_cu,
581 dwarf2_per_objfile *per_objfile,
582 struct abbrev_table *abbrev_table,
583 dwarf2_cu *existing_cu,
584 bool skip_partial,
585 abbrev_cache *cache = nullptr);
586
587 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
588 dwarf2_per_objfile *per_objfile,
589 struct dwarf2_cu *parent_cu = nullptr,
590 struct dwo_file *dwo_file = nullptr);
591
592 DISABLE_COPY_AND_ASSIGN (cutu_reader);
593
594 cutu_reader (cutu_reader &&) = default;
595
596 const gdb_byte *info_ptr = nullptr;
597 struct die_info *comp_unit_die = nullptr;
598 bool dummy_p = false;
599
600 /* Release the new CU, putting it on the chain. This cannot be done
601 for dummy CUs. */
602 void keep ();
603
604 /* Release the abbrev table, transferring ownership to the
605 caller. */
606 abbrev_table_up release_abbrev_table ()
607 {
608 return std::move (m_abbrev_table_holder);
609 }
610
611 private:
612 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
613 dwarf2_per_objfile *per_objfile,
614 dwarf2_cu *existing_cu);
615
616 struct dwarf2_per_cu_data *m_this_cu;
617 std::unique_ptr<dwarf2_cu> m_new_cu;
618
619 /* The ordinary abbreviation table. */
620 abbrev_table_up m_abbrev_table_holder;
621
622 /* The DWO abbreviation table. */
623 abbrev_table_up m_dwo_abbrev_table;
624 };
625
626 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
627 but this would require a corresponding change in unpack_field_as_long
628 and friends. */
629 static int bits_per_byte = 8;
630
631 struct variant_part_builder;
632
633 /* When reading a variant, we track a bit more information about the
634 field, and store it in an object of this type. */
635
636 struct variant_field
637 {
638 int first_field = -1;
639 int last_field = -1;
640
641 /* A variant can contain other variant parts. */
642 std::vector<variant_part_builder> variant_parts;
643
644 /* If we see a DW_TAG_variant, then this will be set if this is the
645 default branch. */
646 bool default_branch = false;
647 /* If we see a DW_AT_discr_value, then this will be the discriminant
648 value. */
649 ULONGEST discriminant_value = 0;
650 /* If we see a DW_AT_discr_list, then this is a pointer to the list
651 data. */
652 struct dwarf_block *discr_list_data = nullptr;
653 };
654
655 /* This represents a DW_TAG_variant_part. */
656
657 struct variant_part_builder
658 {
659 /* The offset of the discriminant field. */
660 sect_offset discriminant_offset {};
661
662 /* Variants that are direct children of this variant part. */
663 std::vector<variant_field> variants;
664
665 /* True if we're currently reading a variant. */
666 bool processing_variant = false;
667 };
668
669 struct nextfield
670 {
671 int accessibility = 0;
672 int virtuality = 0;
673 /* Variant parts need to find the discriminant, which is a DIE
674 reference. We track the section offset of each field to make
675 this link. */
676 sect_offset offset;
677 struct field field {};
678 };
679
680 struct fnfieldlist
681 {
682 const char *name = nullptr;
683 std::vector<struct fn_field> fnfields;
684 };
685
686 /* The routines that read and process dies for a C struct or C++ class
687 pass lists of data member fields and lists of member function fields
688 in an instance of a field_info structure, as defined below. */
689 struct field_info
690 {
691 /* List of data member and baseclasses fields. */
692 std::vector<struct nextfield> fields;
693 std::vector<struct nextfield> baseclasses;
694
695 /* Set if the accessibility of one of the fields is not public. */
696 bool non_public_fields = false;
697
698 /* Member function fieldlist array, contains name of possibly overloaded
699 member function, number of overloaded member functions and a pointer
700 to the head of the member function field chain. */
701 std::vector<struct fnfieldlist> fnfieldlists;
702
703 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
704 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
705 std::vector<struct decl_field> typedef_field_list;
706
707 /* Nested types defined by this class and the number of elements in this
708 list. */
709 std::vector<struct decl_field> nested_types_list;
710
711 /* If non-null, this is the variant part we are currently
712 reading. */
713 variant_part_builder *current_variant_part = nullptr;
714 /* This holds all the top-level variant parts attached to the type
715 we're reading. */
716 std::vector<variant_part_builder> variant_parts;
717
718 /* Return the total number of fields (including baseclasses). */
719 int nfields () const
720 {
721 return fields.size () + baseclasses.size ();
722 }
723 };
724
725 /* Loaded secondary compilation units are kept in memory until they
726 have not been referenced for the processing of this many
727 compilation units. Set this to zero to disable caching. Cache
728 sizes of up to at least twenty will improve startup time for
729 typical inter-CU-reference binaries, at an obvious memory cost. */
730 static int dwarf_max_cache_age = 5;
731 static void
732 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
733 struct cmd_list_element *c, const char *value)
734 {
735 gdb_printf (file, _("The upper bound on the age of cached "
736 "DWARF compilation units is %s.\n"),
737 value);
738 }
739 \f
740 /* local function prototypes */
741
742 static void dwarf2_find_base_address (struct die_info *die,
743 struct dwarf2_cu *cu);
744
745 class cooked_index_storage;
746 static void build_type_psymtabs_reader (cutu_reader *reader,
747 cooked_index_storage *storage);
748
749 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
750
751 static void var_decode_location (struct attribute *attr,
752 struct symbol *sym,
753 struct dwarf2_cu *cu);
754
755 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
756
757 static const gdb_byte *read_attribute (const struct die_reader_specs *,
758 struct attribute *,
759 const struct attr_abbrev *,
760 const gdb_byte *);
761
762 static void read_attribute_reprocess (const struct die_reader_specs *reader,
763 struct attribute *attr, dwarf_tag tag);
764
765 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
766
767 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
768 dwarf2_section_info *, sect_offset);
769
770 static const char *read_indirect_string
771 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
772 const struct comp_unit_head *, unsigned int *);
773
774 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
775 const gdb_byte *,
776 unsigned int *);
777
778 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
779 ULONGEST str_index);
780
781 static const char *read_stub_str_index (struct dwarf2_cu *cu,
782 ULONGEST str_index);
783
784 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
785 struct dwarf2_cu *);
786
787 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
788 struct dwarf2_cu *cu);
789
790 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
791
792 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
793 struct dwarf2_cu *cu);
794
795 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
796
797 static struct die_info *die_specification (struct die_info *die,
798 struct dwarf2_cu **);
799
800 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
801 struct dwarf2_cu *cu,
802 const char *comp_dir);
803
804 static void dwarf_decode_lines (struct line_header *,
805 struct dwarf2_cu *,
806 CORE_ADDR, int decode_mapping);
807
808 static void dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
809 const line_header &lh);
810
811 static struct symbol *new_symbol (struct die_info *, struct type *,
812 struct dwarf2_cu *, struct symbol * = NULL);
813
814 static void dwarf2_const_value (const struct attribute *, struct symbol *,
815 struct dwarf2_cu *);
816
817 static void dwarf2_const_value_attr (const struct attribute *attr,
818 struct type *type,
819 const char *name,
820 struct obstack *obstack,
821 struct dwarf2_cu *cu, LONGEST *value,
822 const gdb_byte **bytes,
823 struct dwarf2_locexpr_baton **baton);
824
825 static struct type *read_subrange_index_type (struct die_info *die,
826 struct dwarf2_cu *cu);
827
828 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
829
830 static int need_gnat_info (struct dwarf2_cu *);
831
832 static struct type *die_descriptive_type (struct die_info *,
833 struct dwarf2_cu *);
834
835 static void set_descriptive_type (struct type *, struct die_info *,
836 struct dwarf2_cu *);
837
838 static struct type *die_containing_type (struct die_info *,
839 struct dwarf2_cu *);
840
841 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
842 struct dwarf2_cu *);
843
844 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
845
846 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
847
848 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
849
850 static char *typename_concat (struct obstack *obs, const char *prefix,
851 const char *suffix, int physname,
852 struct dwarf2_cu *cu);
853
854 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
855
856 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
857
858 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
859
860 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
861
862 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
863
864 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
865
866 /* Return the .debug_loclists section to use for cu. */
867 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
868
869 /* Return the .debug_rnglists section to use for cu. */
870 static struct dwarf2_section_info *cu_debug_rnglists_section
871 (struct dwarf2_cu *cu, dwarf_tag tag);
872
873 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
874 values. Keep the items ordered with increasing constraints compliance. */
875 enum pc_bounds_kind
876 {
877 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
878 PC_BOUNDS_NOT_PRESENT,
879
880 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
881 were present but they do not form a valid range of PC addresses. */
882 PC_BOUNDS_INVALID,
883
884 /* Discontiguous range was found - that is DW_AT_ranges was found. */
885 PC_BOUNDS_RANGES,
886
887 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
888 PC_BOUNDS_HIGH_LOW,
889 };
890
891 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
892 CORE_ADDR *, CORE_ADDR *,
893 struct dwarf2_cu *,
894 addrmap *,
895 void *);
896
897 static void get_scope_pc_bounds (struct die_info *,
898 CORE_ADDR *, CORE_ADDR *,
899 struct dwarf2_cu *);
900
901 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
902 CORE_ADDR, struct dwarf2_cu *);
903
904 static void dwarf2_add_field (struct field_info *, struct die_info *,
905 struct dwarf2_cu *);
906
907 static void dwarf2_attach_fields_to_type (struct field_info *,
908 struct type *, struct dwarf2_cu *);
909
910 static void dwarf2_add_member_fn (struct field_info *,
911 struct die_info *, struct type *,
912 struct dwarf2_cu *);
913
914 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
915 struct type *,
916 struct dwarf2_cu *);
917
918 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
919
920 static void read_common_block (struct die_info *, struct dwarf2_cu *);
921
922 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
923
924 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
925
926 static struct using_direct **using_directives (struct dwarf2_cu *cu);
927
928 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
929
930 static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
931
932 static struct type *read_module_type (struct die_info *die,
933 struct dwarf2_cu *cu);
934
935 static const char *namespace_name (struct die_info *die,
936 int *is_anonymous, struct dwarf2_cu *);
937
938 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
939
940 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
941 bool * = nullptr);
942
943 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
944 struct dwarf2_cu *);
945
946 static struct die_info *read_die_and_siblings_1
947 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
948 struct die_info *);
949
950 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
951 const gdb_byte *info_ptr,
952 const gdb_byte **new_info_ptr,
953 struct die_info *parent);
954
955 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
956 struct die_info **, const gdb_byte *,
957 int);
958
959 static const gdb_byte *read_full_die (const struct die_reader_specs *,
960 struct die_info **, const gdb_byte *);
961
962 static void process_die (struct die_info *, struct dwarf2_cu *);
963
964 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
965 struct objfile *);
966
967 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
968
969 static const char *dwarf2_full_name (const char *name,
970 struct die_info *die,
971 struct dwarf2_cu *cu);
972
973 static const char *dwarf2_physname (const char *name, struct die_info *die,
974 struct dwarf2_cu *cu);
975
976 static struct die_info *dwarf2_extension (struct die_info *die,
977 struct dwarf2_cu **);
978
979 static void store_in_ref_table (struct die_info *,
980 struct dwarf2_cu *);
981
982 static struct die_info *follow_die_ref_or_sig (struct die_info *,
983 const struct attribute *,
984 struct dwarf2_cu **);
985
986 static struct die_info *follow_die_ref (struct die_info *,
987 const struct attribute *,
988 struct dwarf2_cu **);
989
990 static struct die_info *follow_die_sig (struct die_info *,
991 const struct attribute *,
992 struct dwarf2_cu **);
993
994 static struct type *get_signatured_type (struct die_info *, ULONGEST,
995 struct dwarf2_cu *);
996
997 static struct type *get_DW_AT_signature_type (struct die_info *,
998 const struct attribute *,
999 struct dwarf2_cu *);
1000
1001 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1002 dwarf2_per_objfile *per_objfile);
1003
1004 static void read_signatured_type (signatured_type *sig_type,
1005 dwarf2_per_objfile *per_objfile);
1006
1007 static int attr_to_dynamic_prop (const struct attribute *attr,
1008 struct die_info *die, struct dwarf2_cu *cu,
1009 struct dynamic_prop *prop, struct type *type);
1010
1011 /* memory allocation interface */
1012
1013 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1014
1015 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1016
1017 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1018 struct dwarf2_loclist_baton *baton,
1019 const struct attribute *attr);
1020
1021 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1022 struct symbol *sym,
1023 struct dwarf2_cu *cu,
1024 int is_block);
1025
1026 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1027 const gdb_byte *info_ptr,
1028 const struct abbrev_info *abbrev,
1029 bool do_skip_children = true);
1030
1031 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1032 (sect_offset sect_off, unsigned int offset_in_dwz,
1033 dwarf2_per_bfd *per_bfd);
1034
1035 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1036 struct die_info *comp_unit_die,
1037 enum language pretend_language);
1038
1039 static struct type *set_die_type (struct die_info *, struct type *,
1040 struct dwarf2_cu *, bool = false);
1041
1042 static void create_all_units (dwarf2_per_objfile *per_objfile);
1043
1044 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1045 dwarf2_per_objfile *per_objfile,
1046 dwarf2_cu *existing_cu,
1047 bool skip_partial,
1048 enum language pretend_language);
1049
1050 static void process_full_comp_unit (dwarf2_cu *cu,
1051 enum language pretend_language);
1052
1053 static void process_full_type_unit (dwarf2_cu *cu,
1054 enum language pretend_language);
1055
1056 static struct type *get_die_type_at_offset (sect_offset,
1057 dwarf2_per_cu_data *per_cu,
1058 dwarf2_per_objfile *per_objfile);
1059
1060 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1061
1062 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1063 dwarf2_per_objfile *per_objfile,
1064 enum language pretend_language);
1065
1066 static void process_queue (dwarf2_per_objfile *per_objfile);
1067
1068 /* Class, the destructor of which frees all allocated queue entries. This
1069 will only have work to do if an error was thrown while processing the
1070 dwarf. If no error was thrown then the queue entries should have all
1071 been processed, and freed, as we went along. */
1072
1073 class dwarf2_queue_guard
1074 {
1075 public:
1076 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1077 : m_per_objfile (per_objfile)
1078 {
1079 gdb_assert (!m_per_objfile->queue.has_value ());
1080
1081 m_per_objfile->queue.emplace ();
1082 }
1083
1084 /* Free any entries remaining on the queue. There should only be
1085 entries left if we hit an error while processing the dwarf. */
1086 ~dwarf2_queue_guard ()
1087 {
1088 gdb_assert (m_per_objfile->queue.has_value ());
1089
1090 m_per_objfile->queue.reset ();
1091 }
1092
1093 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1094
1095 private:
1096 dwarf2_per_objfile *m_per_objfile;
1097 };
1098
1099 dwarf2_queue_item::~dwarf2_queue_item ()
1100 {
1101 /* Anything still marked queued is likely to be in an
1102 inconsistent state, so discard it. */
1103 if (per_cu->queued)
1104 {
1105 per_objfile->remove_cu (per_cu);
1106 per_cu->queued = 0;
1107 }
1108 }
1109
1110 /* See dwarf2/read.h. */
1111
1112 void
1113 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1114 {
1115 if (data->is_debug_types)
1116 delete static_cast<signatured_type *> (data);
1117 else
1118 delete data;
1119 }
1120
1121 static file_and_directory &find_file_and_directory
1122 (struct die_info *die, struct dwarf2_cu *cu);
1123
1124 static const char *compute_include_file_name
1125 (const struct line_header *lh,
1126 const file_entry &fe,
1127 const file_and_directory &cu_info,
1128 std::string &name_holder);
1129
1130 static htab_up allocate_dwo_unit_table ();
1131
1132 static struct dwo_unit *lookup_dwo_unit_in_dwp
1133 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1134 const char *comp_dir, ULONGEST signature, int is_debug_types);
1135
1136 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1137
1138 static struct dwo_unit *lookup_dwo_comp_unit
1139 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1140 ULONGEST signature);
1141
1142 static struct dwo_unit *lookup_dwo_type_unit
1143 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1144
1145 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1146
1147 /* A unique pointer to a dwo_file. */
1148
1149 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1150
1151 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1152
1153 static void check_producer (struct dwarf2_cu *cu);
1154 \f
1155 /* Various complaints about symbol reading that don't abort the process. */
1156
1157 static void
1158 dwarf2_debug_line_missing_file_complaint (void)
1159 {
1160 complaint (_(".debug_line section has line data without a file"));
1161 }
1162
1163 static void
1164 dwarf2_debug_line_missing_end_sequence_complaint (void)
1165 {
1166 complaint (_(".debug_line section has line "
1167 "program sequence without an end"));
1168 }
1169
1170 static void
1171 dwarf2_complex_location_expr_complaint (void)
1172 {
1173 complaint (_("location expression too complex"));
1174 }
1175
1176 static void
1177 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1178 int arg3)
1179 {
1180 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1181 arg1, arg2, arg3);
1182 }
1183
1184 static void
1185 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1186 {
1187 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1188 arg1, arg2);
1189 }
1190
1191 /* Hash function for line_header_hash. */
1192
1193 static hashval_t
1194 line_header_hash (const struct line_header *ofs)
1195 {
1196 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1197 }
1198
1199 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1200
1201 static hashval_t
1202 line_header_hash_voidp (const void *item)
1203 {
1204 const struct line_header *ofs = (const struct line_header *) item;
1205
1206 return line_header_hash (ofs);
1207 }
1208
1209 /* Equality function for line_header_hash. */
1210
1211 static int
1212 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1213 {
1214 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1215 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1216
1217 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1218 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1219 }
1220
1221 /* See declaration. */
1222
1223 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1224 bool can_copy_)
1225 : obfd (obfd),
1226 can_copy (can_copy_)
1227 {
1228 if (names == NULL)
1229 names = &dwarf2_elf_names;
1230
1231 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1232 locate_sections (obfd, sec, *names);
1233 }
1234
1235 dwarf2_per_bfd::~dwarf2_per_bfd ()
1236 {
1237 for (auto &per_cu : all_units)
1238 {
1239 per_cu->imported_symtabs_free ();
1240 per_cu->free_cached_file_names ();
1241 }
1242
1243 /* Everything else should be on this->obstack. */
1244 }
1245
1246 /* See read.h. */
1247
1248 void
1249 dwarf2_per_objfile::remove_all_cus ()
1250 {
1251 gdb_assert (!queue.has_value ());
1252
1253 m_dwarf2_cus.clear ();
1254 }
1255
1256 /* A helper class that calls free_cached_comp_units on
1257 destruction. */
1258
1259 class free_cached_comp_units
1260 {
1261 public:
1262
1263 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1264 : m_per_objfile (per_objfile)
1265 {
1266 }
1267
1268 ~free_cached_comp_units ()
1269 {
1270 m_per_objfile->remove_all_cus ();
1271 }
1272
1273 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1274
1275 private:
1276
1277 dwarf2_per_objfile *m_per_objfile;
1278 };
1279
1280 /* See read.h. */
1281
1282 bool
1283 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1284 {
1285 if (per_cu->index < this->m_symtabs.size ())
1286 return this->m_symtabs[per_cu->index] != nullptr;
1287 return false;
1288 }
1289
1290 /* See read.h. */
1291
1292 compunit_symtab *
1293 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1294 {
1295 if (per_cu->index < this->m_symtabs.size ())
1296 return this->m_symtabs[per_cu->index];
1297 return nullptr;
1298 }
1299
1300 /* See read.h. */
1301
1302 void
1303 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1304 compunit_symtab *symtab)
1305 {
1306 if (per_cu->index >= this->m_symtabs.size ())
1307 this->m_symtabs.resize (per_cu->index + 1);
1308 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1309 this->m_symtabs[per_cu->index] = symtab;
1310 }
1311
1312 /* Try to locate the sections we need for DWARF 2 debugging
1313 information and return true if we have enough to do something.
1314 NAMES points to the dwarf2 section names, or is NULL if the standard
1315 ELF names are used. CAN_COPY is true for formats where symbol
1316 interposition is possible and so symbol values must follow copy
1317 relocation rules. */
1318
1319 bool
1320 dwarf2_has_info (struct objfile *objfile,
1321 const struct dwarf2_debug_sections *names,
1322 bool can_copy)
1323 {
1324 if (objfile->flags & OBJF_READNEVER)
1325 return false;
1326
1327 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1328
1329 if (per_objfile == NULL)
1330 {
1331 dwarf2_per_bfd *per_bfd;
1332
1333 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1334 BFD doesn't require relocations.
1335
1336 We don't share with objfiles for which -readnow was requested,
1337 because it would complicate things when loading the same BFD with
1338 -readnow and then without -readnow. */
1339 if (!gdb_bfd_requires_relocations (objfile->obfd.get ())
1340 && (objfile->flags & OBJF_READNOW) == 0)
1341 {
1342 /* See if one has been created for this BFD yet. */
1343 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd.get ());
1344
1345 if (per_bfd == nullptr)
1346 {
1347 /* No, create it now. */
1348 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
1349 can_copy);
1350 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
1351 }
1352 }
1353 else
1354 {
1355 /* No sharing possible, create one specifically for this objfile. */
1356 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
1357 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1358 }
1359
1360 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1361 }
1362
1363 return (!per_objfile->per_bfd->info.is_virtual
1364 && per_objfile->per_bfd->info.s.section != NULL
1365 && !per_objfile->per_bfd->abbrev.is_virtual
1366 && per_objfile->per_bfd->abbrev.s.section != NULL);
1367 }
1368
1369 /* See declaration. */
1370
1371 void
1372 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1373 const dwarf2_debug_sections &names)
1374 {
1375 flagword aflag = bfd_section_flags (sectp);
1376
1377 if ((aflag & SEC_HAS_CONTENTS) == 0)
1378 {
1379 }
1380 else if (elf_section_data (sectp)->this_hdr.sh_size
1381 > bfd_get_file_size (abfd))
1382 {
1383 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1384 warning (_("Discarding section %s which has a section size (%s"
1385 ") larger than the file size [in module %s]"),
1386 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1387 bfd_get_filename (abfd));
1388 }
1389 else if (names.info.matches (sectp->name))
1390 {
1391 this->info.s.section = sectp;
1392 this->info.size = bfd_section_size (sectp);
1393 }
1394 else if (names.abbrev.matches (sectp->name))
1395 {
1396 this->abbrev.s.section = sectp;
1397 this->abbrev.size = bfd_section_size (sectp);
1398 }
1399 else if (names.line.matches (sectp->name))
1400 {
1401 this->line.s.section = sectp;
1402 this->line.size = bfd_section_size (sectp);
1403 }
1404 else if (names.loc.matches (sectp->name))
1405 {
1406 this->loc.s.section = sectp;
1407 this->loc.size = bfd_section_size (sectp);
1408 }
1409 else if (names.loclists.matches (sectp->name))
1410 {
1411 this->loclists.s.section = sectp;
1412 this->loclists.size = bfd_section_size (sectp);
1413 }
1414 else if (names.macinfo.matches (sectp->name))
1415 {
1416 this->macinfo.s.section = sectp;
1417 this->macinfo.size = bfd_section_size (sectp);
1418 }
1419 else if (names.macro.matches (sectp->name))
1420 {
1421 this->macro.s.section = sectp;
1422 this->macro.size = bfd_section_size (sectp);
1423 }
1424 else if (names.str.matches (sectp->name))
1425 {
1426 this->str.s.section = sectp;
1427 this->str.size = bfd_section_size (sectp);
1428 }
1429 else if (names.str_offsets.matches (sectp->name))
1430 {
1431 this->str_offsets.s.section = sectp;
1432 this->str_offsets.size = bfd_section_size (sectp);
1433 }
1434 else if (names.line_str.matches (sectp->name))
1435 {
1436 this->line_str.s.section = sectp;
1437 this->line_str.size = bfd_section_size (sectp);
1438 }
1439 else if (names.addr.matches (sectp->name))
1440 {
1441 this->addr.s.section = sectp;
1442 this->addr.size = bfd_section_size (sectp);
1443 }
1444 else if (names.frame.matches (sectp->name))
1445 {
1446 this->frame.s.section = sectp;
1447 this->frame.size = bfd_section_size (sectp);
1448 }
1449 else if (names.eh_frame.matches (sectp->name))
1450 {
1451 this->eh_frame.s.section = sectp;
1452 this->eh_frame.size = bfd_section_size (sectp);
1453 }
1454 else if (names.ranges.matches (sectp->name))
1455 {
1456 this->ranges.s.section = sectp;
1457 this->ranges.size = bfd_section_size (sectp);
1458 }
1459 else if (names.rnglists.matches (sectp->name))
1460 {
1461 this->rnglists.s.section = sectp;
1462 this->rnglists.size = bfd_section_size (sectp);
1463 }
1464 else if (names.types.matches (sectp->name))
1465 {
1466 struct dwarf2_section_info type_section;
1467
1468 memset (&type_section, 0, sizeof (type_section));
1469 type_section.s.section = sectp;
1470 type_section.size = bfd_section_size (sectp);
1471
1472 this->types.push_back (type_section);
1473 }
1474 else if (names.gdb_index.matches (sectp->name))
1475 {
1476 this->gdb_index.s.section = sectp;
1477 this->gdb_index.size = bfd_section_size (sectp);
1478 }
1479 else if (names.debug_names.matches (sectp->name))
1480 {
1481 this->debug_names.s.section = sectp;
1482 this->debug_names.size = bfd_section_size (sectp);
1483 }
1484 else if (names.debug_aranges.matches (sectp->name))
1485 {
1486 this->debug_aranges.s.section = sectp;
1487 this->debug_aranges.size = bfd_section_size (sectp);
1488 }
1489
1490 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1491 && bfd_section_vma (sectp) == 0)
1492 this->has_section_at_zero = true;
1493 }
1494
1495 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1496 SECTION_NAME. */
1497
1498 void
1499 dwarf2_get_section_info (struct objfile *objfile,
1500 enum dwarf2_section_enum sect,
1501 asection **sectp, const gdb_byte **bufp,
1502 bfd_size_type *sizep)
1503 {
1504 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1505 struct dwarf2_section_info *info;
1506
1507 /* We may see an objfile without any DWARF, in which case we just
1508 return nothing. */
1509 if (per_objfile == NULL)
1510 {
1511 *sectp = NULL;
1512 *bufp = NULL;
1513 *sizep = 0;
1514 return;
1515 }
1516 switch (sect)
1517 {
1518 case DWARF2_DEBUG_FRAME:
1519 info = &per_objfile->per_bfd->frame;
1520 break;
1521 case DWARF2_EH_FRAME:
1522 info = &per_objfile->per_bfd->eh_frame;
1523 break;
1524 default:
1525 gdb_assert_not_reached ("unexpected section");
1526 }
1527
1528 info->read (objfile);
1529
1530 *sectp = info->get_bfd_section ();
1531 *bufp = info->buffer;
1532 *sizep = info->size;
1533 }
1534
1535 /* See dwarf2/read.h. */
1536
1537 void
1538 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1539 {
1540 info.read (objfile);
1541 abbrev.read (objfile);
1542 line.read (objfile);
1543 str.read (objfile);
1544 str_offsets.read (objfile);
1545 line_str.read (objfile);
1546 ranges.read (objfile);
1547 rnglists.read (objfile);
1548 addr.read (objfile);
1549
1550 for (auto &section : types)
1551 section.read (objfile);
1552 }
1553
1554 \f
1555 /* DWARF quick_symbol_functions support. */
1556
1557 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1558 unique line tables, so we maintain a separate table of all .debug_line
1559 derived entries to support the sharing.
1560 All the quick functions need is the list of file names. We discard the
1561 line_header when we're done and don't need to record it here. */
1562 struct quick_file_names
1563 {
1564 /* The data used to construct the hash key. */
1565 struct stmt_list_hash hash;
1566
1567 /* The number of entries in file_names, real_names. */
1568 unsigned int num_file_names;
1569
1570 /* The CU directory, as given by DW_AT_comp_dir. May be
1571 nullptr. */
1572 const char *comp_dir;
1573
1574 /* The file names from the line table, after being run through
1575 file_full_name. */
1576 const char **file_names;
1577
1578 /* The file names from the line table after being run through
1579 gdb_realpath. These are computed lazily. */
1580 const char **real_names;
1581 };
1582
1583 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1584 It's handy in this case to have an empty implementation of the
1585 quick symbol functions, to avoid special cases in the rest of the
1586 code. */
1587
1588 struct readnow_functions : public dwarf2_base_index_functions
1589 {
1590 void dump (struct objfile *objfile) override
1591 {
1592 }
1593
1594 void expand_matching_symbols
1595 (struct objfile *,
1596 const lookup_name_info &lookup_name,
1597 domain_enum domain,
1598 int global,
1599 symbol_compare_ftype *ordered_compare) override
1600 {
1601 }
1602
1603 bool expand_symtabs_matching
1604 (struct objfile *objfile,
1605 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1606 const lookup_name_info *lookup_name,
1607 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1608 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1609 block_search_flags search_flags,
1610 domain_enum domain,
1611 enum search_domain kind) override
1612 {
1613 return true;
1614 }
1615 };
1616
1617 /* Utility hash function for a stmt_list_hash. */
1618
1619 static hashval_t
1620 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1621 {
1622 hashval_t v = 0;
1623
1624 if (stmt_list_hash->dwo_unit != NULL)
1625 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1626 v += to_underlying (stmt_list_hash->line_sect_off);
1627 return v;
1628 }
1629
1630 /* Utility equality function for a stmt_list_hash. */
1631
1632 static int
1633 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1634 const struct stmt_list_hash *rhs)
1635 {
1636 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1637 return 0;
1638 if (lhs->dwo_unit != NULL
1639 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1640 return 0;
1641
1642 return lhs->line_sect_off == rhs->line_sect_off;
1643 }
1644
1645 /* Hash function for a quick_file_names. */
1646
1647 static hashval_t
1648 hash_file_name_entry (const void *e)
1649 {
1650 const struct quick_file_names *file_data
1651 = (const struct quick_file_names *) e;
1652
1653 return hash_stmt_list_entry (&file_data->hash);
1654 }
1655
1656 /* Equality function for a quick_file_names. */
1657
1658 static int
1659 eq_file_name_entry (const void *a, const void *b)
1660 {
1661 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1662 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1663
1664 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1665 }
1666
1667 /* See read.h. */
1668
1669 htab_up
1670 create_quick_file_names_table (unsigned int nr_initial_entries)
1671 {
1672 return htab_up (htab_create_alloc (nr_initial_entries,
1673 hash_file_name_entry, eq_file_name_entry,
1674 nullptr, xcalloc, xfree));
1675 }
1676
1677 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
1678 function is unrelated to symtabs, symtab would have to be created afterwards.
1679 You should call age_cached_comp_units after processing the CU. */
1680
1681 static dwarf2_cu *
1682 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1683 bool skip_partial)
1684 {
1685 if (per_cu->is_debug_types)
1686 load_full_type_unit (per_cu, per_objfile);
1687 else
1688 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
1689 skip_partial, language_minimal);
1690
1691 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1692 if (cu == nullptr)
1693 return nullptr; /* Dummy CU. */
1694
1695 dwarf2_find_base_address (cu->dies, cu);
1696
1697 return cu;
1698 }
1699
1700 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
1701
1702 static void
1703 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1704 dwarf2_per_objfile *per_objfile, bool skip_partial)
1705 {
1706 {
1707 /* The destructor of dwarf2_queue_guard frees any entries left on
1708 the queue. After this point we're guaranteed to leave this function
1709 with the dwarf queue empty. */
1710 dwarf2_queue_guard q_guard (per_objfile);
1711
1712 if (!per_objfile->symtab_set_p (per_cu))
1713 {
1714 queue_comp_unit (per_cu, per_objfile, language_minimal);
1715 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
1716
1717 /* If we just loaded a CU from a DWO, and we're working with an index
1718 that may badly handle TUs, load all the TUs in that DWO as well.
1719 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
1720 if (!per_cu->is_debug_types
1721 && cu != NULL
1722 && cu->dwo_unit != NULL
1723 && per_objfile->per_bfd->index_table != NULL
1724 && !per_objfile->per_bfd->index_table->version_check ()
1725 /* DWP files aren't supported yet. */
1726 && get_dwp_file (per_objfile) == NULL)
1727 queue_and_load_all_dwo_tus (cu);
1728 }
1729
1730 process_queue (per_objfile);
1731 }
1732
1733 /* Age the cache, releasing compilation units that have not
1734 been used recently. */
1735 per_objfile->age_comp_units ();
1736 }
1737
1738 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
1739 the per-objfile for which this symtab is instantiated.
1740
1741 Returns the resulting symbol table. */
1742
1743 static struct compunit_symtab *
1744 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1745 dwarf2_per_objfile *per_objfile,
1746 bool skip_partial)
1747 {
1748 if (!per_objfile->symtab_set_p (per_cu))
1749 {
1750 free_cached_comp_units freer (per_objfile);
1751 scoped_restore decrementer = increment_reading_symtab ();
1752 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
1753 process_cu_includes (per_objfile);
1754 }
1755
1756 return per_objfile->get_symtab (per_cu);
1757 }
1758
1759 /* See read.h. */
1760
1761 dwarf2_per_cu_data_up
1762 dwarf2_per_bfd::allocate_per_cu ()
1763 {
1764 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
1765 result->per_bfd = this;
1766 result->index = all_units.size ();
1767 return result;
1768 }
1769
1770 /* See read.h. */
1771
1772 signatured_type_up
1773 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
1774 {
1775 signatured_type_up result (new signatured_type (signature));
1776 result->per_bfd = this;
1777 result->index = all_units.size ();
1778 result->is_debug_types = true;
1779 tu_stats.nr_tus++;
1780 return result;
1781 }
1782
1783 /* See read.h. */
1784
1785 dwarf2_per_cu_data_up
1786 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
1787 struct dwarf2_section_info *section,
1788 int is_dwz,
1789 sect_offset sect_off, ULONGEST length)
1790 {
1791 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
1792 the_cu->sect_off = sect_off;
1793 the_cu->set_length (length);
1794 the_cu->section = section;
1795 the_cu->is_dwz = is_dwz;
1796 return the_cu;
1797 }
1798
1799 /* See read.h. */
1800
1801 bool
1802 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
1803 dwarf2_section_info *section,
1804 addrmap *mutable_map)
1805 {
1806 struct objfile *objfile = per_objfile->objfile;
1807 bfd *abfd = objfile->obfd.get ();
1808 struct gdbarch *gdbarch = objfile->arch ();
1809 const CORE_ADDR baseaddr = objfile->text_section_offset ();
1810 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1811
1812 std::unordered_map<sect_offset,
1813 dwarf2_per_cu_data *,
1814 gdb::hash_enum<sect_offset>>
1815 debug_info_offset_to_per_cu;
1816 for (const auto &per_cu : per_bfd->all_units)
1817 {
1818 /* A TU will not need aranges, and skipping them here is an easy
1819 way of ignoring .debug_types -- and possibly seeing a
1820 duplicate section offset -- entirely. The same applies to
1821 units coming from a dwz file. */
1822 if (per_cu->is_debug_types || per_cu->is_dwz)
1823 continue;
1824
1825 const auto insertpair
1826 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
1827 per_cu.get ());
1828
1829 /* Assume no duplicate offsets in all_units. */
1830 gdb_assert (insertpair.second);
1831 }
1832
1833 std::set<sect_offset> debug_info_offset_seen;
1834
1835 section->read (objfile);
1836
1837 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
1838
1839 const gdb_byte *addr = section->buffer;
1840
1841 while (addr < section->buffer + section->size)
1842 {
1843 const gdb_byte *const entry_addr = addr;
1844 unsigned int bytes_read;
1845
1846 const LONGEST entry_length = read_initial_length (abfd, addr,
1847 &bytes_read);
1848 addr += bytes_read;
1849
1850 const gdb_byte *const entry_end = addr + entry_length;
1851 const bool dwarf5_is_dwarf64 = bytes_read != 4;
1852 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
1853 if (addr + entry_length > section->buffer + section->size)
1854 {
1855 warning (_("Section .debug_aranges in %s entry at offset %s "
1856 "length %s exceeds section length %s, "
1857 "ignoring .debug_aranges."),
1858 objfile_name (objfile),
1859 plongest (entry_addr - section->buffer),
1860 plongest (bytes_read + entry_length),
1861 pulongest (section->size));
1862 return false;
1863 }
1864
1865 /* The version number. */
1866 const uint16_t version = read_2_bytes (abfd, addr);
1867 addr += 2;
1868 if (version != 2)
1869 {
1870 warning (_("Section .debug_aranges in %s entry at offset %s "
1871 "has unsupported version %d, ignoring .debug_aranges."),
1872 objfile_name (objfile),
1873 plongest (entry_addr - section->buffer), version);
1874 return false;
1875 }
1876
1877 const uint64_t debug_info_offset
1878 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
1879 addr += offset_size;
1880 const auto per_cu_it
1881 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
1882 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
1883 {
1884 warning (_("Section .debug_aranges in %s entry at offset %s "
1885 "debug_info_offset %s does not exists, "
1886 "ignoring .debug_aranges."),
1887 objfile_name (objfile),
1888 plongest (entry_addr - section->buffer),
1889 pulongest (debug_info_offset));
1890 return false;
1891 }
1892 const auto insertpair
1893 = debug_info_offset_seen.insert (sect_offset (debug_info_offset));
1894 if (!insertpair.second)
1895 {
1896 warning (_("Section .debug_aranges in %s has duplicate "
1897 "debug_info_offset %s, ignoring .debug_aranges."),
1898 objfile_name (objfile),
1899 sect_offset_str (sect_offset (debug_info_offset)));
1900 return false;
1901 }
1902 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
1903
1904 const uint8_t address_size = *addr++;
1905 if (address_size < 1 || address_size > 8)
1906 {
1907 warning (_("Section .debug_aranges in %s entry at offset %s "
1908 "address_size %u is invalid, ignoring .debug_aranges."),
1909 objfile_name (objfile),
1910 plongest (entry_addr - section->buffer), address_size);
1911 return false;
1912 }
1913
1914 const uint8_t segment_selector_size = *addr++;
1915 if (segment_selector_size != 0)
1916 {
1917 warning (_("Section .debug_aranges in %s entry at offset %s "
1918 "segment_selector_size %u is not supported, "
1919 "ignoring .debug_aranges."),
1920 objfile_name (objfile),
1921 plongest (entry_addr - section->buffer),
1922 segment_selector_size);
1923 return false;
1924 }
1925
1926 /* Must pad to an alignment boundary that is twice the address
1927 size. It is undocumented by the DWARF standard but GCC does
1928 use it. However, not every compiler does this. We can see
1929 whether it has happened by looking at the total length of the
1930 contents of the aranges for this CU -- it if isn't a multiple
1931 of twice the address size, then we skip any leftover
1932 bytes. */
1933 addr += (entry_end - addr) % (2 * address_size);
1934
1935 while (addr < entry_end)
1936 {
1937 if (addr + 2 * address_size > entry_end)
1938 {
1939 warning (_("Section .debug_aranges in %s entry at offset %s "
1940 "address list is not properly terminated, "
1941 "ignoring .debug_aranges."),
1942 objfile_name (objfile),
1943 plongest (entry_addr - section->buffer));
1944 return false;
1945 }
1946 ULONGEST start = extract_unsigned_integer (addr, address_size,
1947 dwarf5_byte_order);
1948 addr += address_size;
1949 ULONGEST length = extract_unsigned_integer (addr, address_size,
1950 dwarf5_byte_order);
1951 addr += address_size;
1952 if (start == 0 && length == 0)
1953 {
1954 /* This can happen on some targets with --gc-sections.
1955 This pair of values is also used to mark the end of
1956 the entries for a given CU, but we ignore it and
1957 instead handle termination using the check at the top
1958 of the loop. */
1959 continue;
1960 }
1961 if (start == 0 && !per_bfd->has_section_at_zero)
1962 {
1963 /* Symbol was eliminated due to a COMDAT group. */
1964 continue;
1965 }
1966 ULONGEST end = start + length;
1967 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
1968 - baseaddr);
1969 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
1970 - baseaddr);
1971 mutable_map->set_empty (start, end - 1, per_cu);
1972 }
1973
1974 per_cu->addresses_seen = true;
1975 }
1976
1977 return true;
1978 }
1979
1980 /* die_reader_func for dw2_get_file_names. */
1981
1982 static void
1983 dw2_get_file_names_reader (const struct die_reader_specs *reader,
1984 struct die_info *comp_unit_die)
1985 {
1986 struct dwarf2_cu *cu = reader->cu;
1987 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
1988 dwarf2_per_objfile *per_objfile = cu->per_objfile;
1989 struct dwarf2_per_cu_data *lh_cu;
1990 struct attribute *attr;
1991 void **slot;
1992 struct quick_file_names *qfn;
1993
1994 gdb_assert (! this_cu->is_debug_types);
1995
1996 this_cu->files_read = true;
1997 /* Our callers never want to match partial units -- instead they
1998 will match the enclosing full CU. */
1999 if (comp_unit_die->tag == DW_TAG_partial_unit)
2000 return;
2001
2002 lh_cu = this_cu;
2003 slot = NULL;
2004
2005 line_header_up lh;
2006 sect_offset line_offset {};
2007
2008 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
2009
2010 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2011 if (attr != nullptr && attr->form_is_unsigned ())
2012 {
2013 struct quick_file_names find_entry;
2014
2015 line_offset = (sect_offset) attr->as_unsigned ();
2016
2017 /* We may have already read in this line header (TU line header sharing).
2018 If we have we're done. */
2019 find_entry.hash.dwo_unit = cu->dwo_unit;
2020 find_entry.hash.line_sect_off = line_offset;
2021 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2022 &find_entry, INSERT);
2023 if (*slot != NULL)
2024 {
2025 lh_cu->file_names = (struct quick_file_names *) *slot;
2026 return;
2027 }
2028
2029 lh = dwarf_decode_line_header (line_offset, cu, fnd.get_comp_dir ());
2030 }
2031
2032 int offset = 0;
2033 if (!fnd.is_unknown ())
2034 ++offset;
2035 else if (lh == nullptr)
2036 return;
2037
2038 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2039 qfn->hash.dwo_unit = cu->dwo_unit;
2040 qfn->hash.line_sect_off = line_offset;
2041 /* There may not be a DW_AT_stmt_list. */
2042 if (slot != nullptr)
2043 *slot = qfn;
2044
2045 std::vector<const char *> include_names;
2046 if (lh != nullptr)
2047 {
2048 for (const auto &entry : lh->file_names ())
2049 {
2050 std::string name_holder;
2051 const char *include_name =
2052 compute_include_file_name (lh.get (), entry, fnd, name_holder);
2053 if (include_name != nullptr)
2054 {
2055 include_name = per_objfile->objfile->intern (include_name);
2056 include_names.push_back (include_name);
2057 }
2058 }
2059 }
2060
2061 qfn->num_file_names = offset + include_names.size ();
2062 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
2063 qfn->file_names =
2064 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2065 qfn->num_file_names);
2066 if (offset != 0)
2067 qfn->file_names[0] = per_objfile->objfile->intern (fnd.get_name ());
2068
2069 if (!include_names.empty ())
2070 memcpy (&qfn->file_names[offset], include_names.data (),
2071 include_names.size () * sizeof (const char *));
2072
2073 qfn->real_names = NULL;
2074
2075 lh_cu->file_names = qfn;
2076 }
2077
2078 /* A helper for the "quick" functions which attempts to read the line
2079 table for THIS_CU. */
2080
2081 static struct quick_file_names *
2082 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2083 dwarf2_per_objfile *per_objfile)
2084 {
2085 /* This should never be called for TUs. */
2086 gdb_assert (! this_cu->is_debug_types);
2087
2088 if (this_cu->files_read)
2089 return this_cu->file_names;
2090
2091 cutu_reader reader (this_cu, per_objfile);
2092 if (!reader.dummy_p)
2093 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2094
2095 return this_cu->file_names;
2096 }
2097
2098 /* A helper for the "quick" functions which computes and caches the
2099 real path for a given file name from the line table. */
2100
2101 static const char *
2102 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2103 struct quick_file_names *qfn, int index)
2104 {
2105 if (qfn->real_names == NULL)
2106 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
2107 qfn->num_file_names, const char *);
2108
2109 if (qfn->real_names[index] == NULL)
2110 {
2111 const char *dirname = nullptr;
2112
2113 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
2114 dirname = qfn->comp_dir;
2115
2116 gdb::unique_xmalloc_ptr<char> fullname;
2117 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
2118
2119 qfn->real_names[index] = fullname.release ();
2120 }
2121
2122 return qfn->real_names[index];
2123 }
2124
2125 struct symtab *
2126 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
2127 {
2128 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2129 dwarf2_per_cu_data *dwarf_cu
2130 = per_objfile->per_bfd->all_units.back ().get ();
2131 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2132
2133 if (cust == NULL)
2134 return NULL;
2135
2136 return cust->primary_filetab ();
2137 }
2138
2139 /* See read.h. */
2140
2141 void
2142 dwarf2_per_cu_data::free_cached_file_names ()
2143 {
2144 if (fnd != nullptr)
2145 fnd->forget_fullname ();
2146
2147 if (per_bfd == nullptr)
2148 return;
2149
2150 struct quick_file_names *file_data = file_names;
2151 if (file_data != nullptr && file_data->real_names != nullptr)
2152 {
2153 for (int i = 0; i < file_data->num_file_names; ++i)
2154 {
2155 xfree ((void *) file_data->real_names[i]);
2156 file_data->real_names[i] = nullptr;
2157 }
2158 }
2159 }
2160
2161 void
2162 dwarf2_base_index_functions::forget_cached_source_info
2163 (struct objfile *objfile)
2164 {
2165 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2166
2167 for (auto &per_cu : per_objfile->per_bfd->all_units)
2168 per_cu->free_cached_file_names ();
2169 }
2170
2171 void
2172 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
2173 bool print_bcache)
2174 {
2175 if (print_bcache)
2176 return;
2177
2178 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2179 int total = per_objfile->per_bfd->all_units.size ();
2180 int count = 0;
2181
2182 for (int i = 0; i < total; ++i)
2183 {
2184 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2185
2186 if (!per_objfile->symtab_set_p (per_cu))
2187 ++count;
2188 }
2189 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
2190 gdb_printf (_(" Number of unread CUs: %d\n"), count);
2191 }
2192
2193 void
2194 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
2195 {
2196 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2197 int total_units = per_objfile->per_bfd->all_units.size ();
2198
2199 for (int i = 0; i < total_units; ++i)
2200 {
2201 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2202
2203 /* We don't want to directly expand a partial CU, because if we
2204 read it with the wrong language, then assertion failures can
2205 be triggered later on. See PR symtab/23010. So, tell
2206 dw2_instantiate_symtab to skip partial CUs -- any important
2207 partial CU will be read via DW_TAG_imported_unit anyway. */
2208 dw2_instantiate_symtab (per_cu, per_objfile, true);
2209 }
2210 }
2211
2212
2213 /* Starting from a search name, return the string that finds the upper
2214 bound of all strings that start with SEARCH_NAME in a sorted name
2215 list. Returns the empty string to indicate that the upper bound is
2216 the end of the list. */
2217
2218 static std::string
2219 make_sort_after_prefix_name (const char *search_name)
2220 {
2221 /* When looking to complete "func", we find the upper bound of all
2222 symbols that start with "func" by looking for where we'd insert
2223 the closest string that would follow "func" in lexicographical
2224 order. Usually, that's "func"-with-last-character-incremented,
2225 i.e. "fund". Mind non-ASCII characters, though. Usually those
2226 will be UTF-8 multi-byte sequences, but we can't be certain.
2227 Especially mind the 0xff character, which is a valid character in
2228 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
2229 rule out compilers allowing it in identifiers. Note that
2230 conveniently, strcmp/strcasecmp are specified to compare
2231 characters interpreted as unsigned char. So what we do is treat
2232 the whole string as a base 256 number composed of a sequence of
2233 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
2234 to 0, and carries 1 to the following more-significant position.
2235 If the very first character in SEARCH_NAME ends up incremented
2236 and carries/overflows, then the upper bound is the end of the
2237 list. The string after the empty string is also the empty
2238 string.
2239
2240 Some examples of this operation:
2241
2242 SEARCH_NAME => "+1" RESULT
2243
2244 "abc" => "abd"
2245 "ab\xff" => "ac"
2246 "\xff" "a" "\xff" => "\xff" "b"
2247 "\xff" => ""
2248 "\xff\xff" => ""
2249 "" => ""
2250
2251 Then, with these symbols for example:
2252
2253 func
2254 func1
2255 fund
2256
2257 completing "func" looks for symbols between "func" and
2258 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
2259 which finds "func" and "func1", but not "fund".
2260
2261 And with:
2262
2263 funcÿ (Latin1 'ÿ' [0xff])
2264 funcÿ1
2265 fund
2266
2267 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
2268 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
2269
2270 And with:
2271
2272 ÿÿ (Latin1 'ÿ' [0xff])
2273 ÿÿ1
2274
2275 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
2276 the end of the list.
2277 */
2278 std::string after = search_name;
2279 while (!after.empty () && (unsigned char) after.back () == 0xff)
2280 after.pop_back ();
2281 if (!after.empty ())
2282 after.back () = (unsigned char) after.back () + 1;
2283 return after;
2284 }
2285
2286 /* See declaration. */
2287
2288 std::pair<std::vector<name_component>::const_iterator,
2289 std::vector<name_component>::const_iterator>
2290 mapped_index_base::find_name_components_bounds
2291 (const lookup_name_info &lookup_name_without_params, language lang,
2292 dwarf2_per_objfile *per_objfile) const
2293 {
2294 auto *name_cmp
2295 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
2296
2297 const char *lang_name
2298 = lookup_name_without_params.language_lookup_name (lang);
2299
2300 /* Comparison function object for lower_bound that matches against a
2301 given symbol name. */
2302 auto lookup_compare_lower = [&] (const name_component &elem,
2303 const char *name)
2304 {
2305 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
2306 const char *elem_name = elem_qualified + elem.name_offset;
2307 return name_cmp (elem_name, name) < 0;
2308 };
2309
2310 /* Comparison function object for upper_bound that matches against a
2311 given symbol name. */
2312 auto lookup_compare_upper = [&] (const char *name,
2313 const name_component &elem)
2314 {
2315 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
2316 const char *elem_name = elem_qualified + elem.name_offset;
2317 return name_cmp (name, elem_name) < 0;
2318 };
2319
2320 auto begin = this->name_components.begin ();
2321 auto end = this->name_components.end ();
2322
2323 /* Find the lower bound. */
2324 auto lower = [&] ()
2325 {
2326 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
2327 return begin;
2328 else
2329 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
2330 } ();
2331
2332 /* Find the upper bound. */
2333 auto upper = [&] ()
2334 {
2335 if (lookup_name_without_params.completion_mode ())
2336 {
2337 /* In completion mode, we want UPPER to point past all
2338 symbols names that have the same prefix. I.e., with
2339 these symbols, and completing "func":
2340
2341 function << lower bound
2342 function1
2343 other_function << upper bound
2344
2345 We find the upper bound by looking for the insertion
2346 point of "func"-with-last-character-incremented,
2347 i.e. "fund". */
2348 std::string after = make_sort_after_prefix_name (lang_name);
2349 if (after.empty ())
2350 return end;
2351 return std::lower_bound (lower, end, after.c_str (),
2352 lookup_compare_lower);
2353 }
2354 else
2355 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
2356 } ();
2357
2358 return {lower, upper};
2359 }
2360
2361 /* See declaration. */
2362
2363 void
2364 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
2365 {
2366 if (!this->name_components.empty ())
2367 return;
2368
2369 this->name_components_casing = case_sensitivity;
2370 auto *name_cmp
2371 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
2372
2373 /* The code below only knows how to break apart components of C++
2374 symbol names (and other languages that use '::' as
2375 namespace/module separator) and Ada symbol names. */
2376 auto count = this->symbol_name_count ();
2377 for (offset_type idx = 0; idx < count; idx++)
2378 {
2379 if (this->symbol_name_slot_invalid (idx))
2380 continue;
2381
2382 const char *name = this->symbol_name_at (idx, per_objfile);
2383
2384 /* Add each name component to the name component table. */
2385 unsigned int previous_len = 0;
2386
2387 if (strstr (name, "::") != nullptr)
2388 {
2389 for (unsigned int current_len = cp_find_first_component (name);
2390 name[current_len] != '\0';
2391 current_len += cp_find_first_component (name + current_len))
2392 {
2393 gdb_assert (name[current_len] == ':');
2394 this->name_components.push_back ({previous_len, idx});
2395 /* Skip the '::'. */
2396 current_len += 2;
2397 previous_len = current_len;
2398 }
2399 }
2400 else
2401 {
2402 /* Handle the Ada encoded (aka mangled) form here. */
2403 for (const char *iter = strstr (name, "__");
2404 iter != nullptr;
2405 iter = strstr (iter, "__"))
2406 {
2407 this->name_components.push_back ({previous_len, idx});
2408 iter += 2;
2409 previous_len = iter - name;
2410 }
2411 }
2412
2413 this->name_components.push_back ({previous_len, idx});
2414 }
2415
2416 /* Sort name_components elements by name. */
2417 auto name_comp_compare = [&] (const name_component &left,
2418 const name_component &right)
2419 {
2420 const char *left_qualified
2421 = this->symbol_name_at (left.idx, per_objfile);
2422 const char *right_qualified
2423 = this->symbol_name_at (right.idx, per_objfile);
2424
2425 const char *left_name = left_qualified + left.name_offset;
2426 const char *right_name = right_qualified + right.name_offset;
2427
2428 return name_cmp (left_name, right_name) < 0;
2429 };
2430
2431 std::sort (this->name_components.begin (),
2432 this->name_components.end (),
2433 name_comp_compare);
2434 }
2435
2436 /* See read.h. */
2437
2438 bool
2439 dw2_expand_symtabs_matching_symbol
2440 (mapped_index_base &index,
2441 const lookup_name_info &lookup_name_in,
2442 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2443 gdb::function_view<bool (offset_type)> match_callback,
2444 dwarf2_per_objfile *per_objfile)
2445 {
2446 lookup_name_info lookup_name_without_params
2447 = lookup_name_in.make_ignore_params ();
2448
2449 /* Build the symbol name component sorted vector, if we haven't
2450 yet. */
2451 index.build_name_components (per_objfile);
2452
2453 /* The same symbol may appear more than once in the range though.
2454 E.g., if we're looking for symbols that complete "w", and we have
2455 a symbol named "w1::w2", we'll find the two name components for
2456 that same symbol in the range. To be sure we only call the
2457 callback once per symbol, we first collect the symbol name
2458 indexes that matched in a temporary vector and ignore
2459 duplicates. */
2460 std::vector<offset_type> matches;
2461
2462 struct name_and_matcher
2463 {
2464 symbol_name_matcher_ftype *matcher;
2465 const char *name;
2466
2467 bool operator== (const name_and_matcher &other) const
2468 {
2469 return matcher == other.matcher && strcmp (name, other.name) == 0;
2470 }
2471 };
2472
2473 /* A vector holding all the different symbol name matchers, for all
2474 languages. */
2475 std::vector<name_and_matcher> matchers;
2476
2477 for (int i = 0; i < nr_languages; i++)
2478 {
2479 enum language lang_e = (enum language) i;
2480
2481 const language_defn *lang = language_def (lang_e);
2482 symbol_name_matcher_ftype *name_matcher
2483 = lang->get_symbol_name_matcher (lookup_name_without_params);
2484
2485 name_and_matcher key {
2486 name_matcher,
2487 lookup_name_without_params.language_lookup_name (lang_e)
2488 };
2489
2490 /* Don't insert the same comparison routine more than once.
2491 Note that we do this linear walk. This is not a problem in
2492 practice because the number of supported languages is
2493 low. */
2494 if (std::find (matchers.begin (), matchers.end (), key)
2495 != matchers.end ())
2496 continue;
2497 matchers.push_back (std::move (key));
2498
2499 auto bounds
2500 = index.find_name_components_bounds (lookup_name_without_params,
2501 lang_e, per_objfile);
2502
2503 /* Now for each symbol name in range, check to see if we have a name
2504 match, and if so, call the MATCH_CALLBACK callback. */
2505
2506 for (; bounds.first != bounds.second; ++bounds.first)
2507 {
2508 const char *qualified
2509 = index.symbol_name_at (bounds.first->idx, per_objfile);
2510
2511 if (!name_matcher (qualified, lookup_name_without_params, NULL)
2512 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
2513 continue;
2514
2515 matches.push_back (bounds.first->idx);
2516 }
2517 }
2518
2519 std::sort (matches.begin (), matches.end ());
2520
2521 /* Finally call the callback, once per match. */
2522 ULONGEST prev = -1;
2523 bool result = true;
2524 for (offset_type idx : matches)
2525 {
2526 if (prev != idx)
2527 {
2528 if (!match_callback (idx))
2529 {
2530 result = false;
2531 break;
2532 }
2533 prev = idx;
2534 }
2535 }
2536
2537 /* Above we use a type wider than idx's for 'prev', since 0 and
2538 (offset_type)-1 are both possible values. */
2539 static_assert (sizeof (prev) > sizeof (offset_type), "");
2540
2541 return result;
2542 }
2543
2544 #if GDB_SELF_TEST
2545
2546 namespace selftests { namespace dw2_expand_symtabs_matching {
2547
2548 /* A mock .gdb_index/.debug_names-like name index table, enough to
2549 exercise dw2_expand_symtabs_matching_symbol, which works with the
2550 mapped_index_base interface. Builds an index from the symbol list
2551 passed as parameter to the constructor. */
2552 class mock_mapped_index : public mapped_index_base
2553 {
2554 public:
2555 mock_mapped_index (gdb::array_view<const char *> symbols)
2556 : m_symbol_table (symbols)
2557 {}
2558
2559 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
2560
2561 /* Return the number of names in the symbol table. */
2562 size_t symbol_name_count () const override
2563 {
2564 return m_symbol_table.size ();
2565 }
2566
2567 /* Get the name of the symbol at IDX in the symbol table. */
2568 const char *symbol_name_at
2569 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
2570 {
2571 return m_symbol_table[idx];
2572 }
2573
2574 quick_symbol_functions_up make_quick_functions () const override
2575 {
2576 return nullptr;
2577 }
2578
2579 private:
2580 gdb::array_view<const char *> m_symbol_table;
2581 };
2582
2583 /* Convenience function that converts a NULL pointer to a "<null>"
2584 string, to pass to print routines. */
2585
2586 static const char *
2587 string_or_null (const char *str)
2588 {
2589 return str != NULL ? str : "<null>";
2590 }
2591
2592 /* Check if a lookup_name_info built from
2593 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
2594 index. EXPECTED_LIST is the list of expected matches, in expected
2595 matching order. If no match expected, then an empty list is
2596 specified. Returns true on success. On failure prints a warning
2597 indicating the file:line that failed, and returns false. */
2598
2599 static bool
2600 check_match (const char *file, int line,
2601 mock_mapped_index &mock_index,
2602 const char *name, symbol_name_match_type match_type,
2603 bool completion_mode,
2604 std::initializer_list<const char *> expected_list,
2605 dwarf2_per_objfile *per_objfile)
2606 {
2607 lookup_name_info lookup_name (name, match_type, completion_mode);
2608
2609 bool matched = true;
2610
2611 auto mismatch = [&] (const char *expected_str,
2612 const char *got)
2613 {
2614 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
2615 "expected=\"%s\", got=\"%s\"\n"),
2616 file, line,
2617 (match_type == symbol_name_match_type::FULL
2618 ? "FULL" : "WILD"),
2619 name, string_or_null (expected_str), string_or_null (got));
2620 matched = false;
2621 };
2622
2623 auto expected_it = expected_list.begin ();
2624 auto expected_end = expected_list.end ();
2625
2626 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
2627 nullptr,
2628 [&] (offset_type idx)
2629 {
2630 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
2631 const char *expected_str
2632 = expected_it == expected_end ? NULL : *expected_it++;
2633
2634 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
2635 mismatch (expected_str, matched_name);
2636 return true;
2637 }, per_objfile);
2638
2639 const char *expected_str
2640 = expected_it == expected_end ? NULL : *expected_it++;
2641 if (expected_str != NULL)
2642 mismatch (expected_str, NULL);
2643
2644 return matched;
2645 }
2646
2647 /* The symbols added to the mock mapped_index for testing (in
2648 canonical form). */
2649 static const char *test_symbols[] = {
2650 "function",
2651 "std::bar",
2652 "std::zfunction",
2653 "std::zfunction2",
2654 "w1::w2",
2655 "ns::foo<char*>",
2656 "ns::foo<int>",
2657 "ns::foo<long>",
2658 "ns2::tmpl<int>::foo2",
2659 "(anonymous namespace)::A::B::C",
2660
2661 /* These are used to check that the increment-last-char in the
2662 matching algorithm for completion doesn't match "t1_fund" when
2663 completing "t1_func". */
2664 "t1_func",
2665 "t1_func1",
2666 "t1_fund",
2667 "t1_fund1",
2668
2669 /* A UTF-8 name with multi-byte sequences to make sure that
2670 cp-name-parser understands this as a single identifier ("função"
2671 is "function" in PT). */
2672 u8"u8função",
2673
2674 /* Test a symbol name that ends with a 0xff character, which is a
2675 valid character in non-UTF-8 source character sets (e.g. Latin1
2676 'ÿ'), and we can't rule out compilers allowing it in identifiers.
2677 We test this because the completion algorithm finds the upper
2678 bound of symbols by looking for the insertion point of
2679 "func"-with-last-character-incremented, i.e. "fund", and adding 1
2680 to 0xff should wraparound and carry to the previous character.
2681 See comments in make_sort_after_prefix_name. */
2682 "yfunc\377",
2683
2684 /* Some more symbols with \377 (0xff). See above. */
2685 "\377",
2686 "\377\377123",
2687
2688 /* A name with all sorts of complications. Starts with "z" to make
2689 it easier for the completion tests below. */
2690 #define Z_SYM_NAME \
2691 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
2692 "::tuple<(anonymous namespace)::ui*, " \
2693 "std::default_delete<(anonymous namespace)::ui>, void>"
2694
2695 Z_SYM_NAME
2696 };
2697
2698 /* Returns true if the mapped_index_base::find_name_component_bounds
2699 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
2700 in completion mode. */
2701
2702 static bool
2703 check_find_bounds_finds (mapped_index_base &index,
2704 const char *search_name,
2705 gdb::array_view<const char *> expected_syms,
2706 dwarf2_per_objfile *per_objfile)
2707 {
2708 lookup_name_info lookup_name (search_name,
2709 symbol_name_match_type::FULL, true);
2710
2711 auto bounds = index.find_name_components_bounds (lookup_name,
2712 language_cplus,
2713 per_objfile);
2714
2715 size_t distance = std::distance (bounds.first, bounds.second);
2716 if (distance != expected_syms.size ())
2717 return false;
2718
2719 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
2720 {
2721 auto nc_elem = bounds.first + exp_elem;
2722 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
2723 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
2724 return false;
2725 }
2726
2727 return true;
2728 }
2729
2730 /* Test the lower-level mapped_index::find_name_component_bounds
2731 method. */
2732
2733 static void
2734 test_mapped_index_find_name_component_bounds ()
2735 {
2736 mock_mapped_index mock_index (test_symbols);
2737
2738 mock_index.build_name_components (NULL /* per_objfile */);
2739
2740 /* Test the lower-level mapped_index::find_name_component_bounds
2741 method in completion mode. */
2742 {
2743 static const char *expected_syms[] = {
2744 "t1_func",
2745 "t1_func1",
2746 };
2747
2748 SELF_CHECK (check_find_bounds_finds
2749 (mock_index, "t1_func", expected_syms,
2750 NULL /* per_objfile */));
2751 }
2752
2753 /* Check that the increment-last-char in the name matching algorithm
2754 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
2755 make_sort_after_prefix_name. */
2756 {
2757 static const char *expected_syms1[] = {
2758 "\377",
2759 "\377\377123",
2760 };
2761 SELF_CHECK (check_find_bounds_finds
2762 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
2763
2764 static const char *expected_syms2[] = {
2765 "\377\377123",
2766 };
2767 SELF_CHECK (check_find_bounds_finds
2768 (mock_index, "\377\377", expected_syms2,
2769 NULL /* per_objfile */));
2770 }
2771 }
2772
2773 /* Test dw2_expand_symtabs_matching_symbol. */
2774
2775 static void
2776 test_dw2_expand_symtabs_matching_symbol ()
2777 {
2778 mock_mapped_index mock_index (test_symbols);
2779
2780 /* We let all tests run until the end even if some fails, for debug
2781 convenience. */
2782 bool any_mismatch = false;
2783
2784 /* Create the expected symbols list (an initializer_list). Needed
2785 because lists have commas, and we need to pass them to CHECK,
2786 which is a macro. */
2787 #define EXPECT(...) { __VA_ARGS__ }
2788
2789 /* Wrapper for check_match that passes down the current
2790 __FILE__/__LINE__. */
2791 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
2792 any_mismatch |= !check_match (__FILE__, __LINE__, \
2793 mock_index, \
2794 NAME, MATCH_TYPE, COMPLETION_MODE, \
2795 EXPECTED_LIST, NULL)
2796
2797 /* Identity checks. */
2798 for (const char *sym : test_symbols)
2799 {
2800 /* Should be able to match all existing symbols. */
2801 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
2802 EXPECT (sym));
2803
2804 /* Should be able to match all existing symbols with
2805 parameters. */
2806 std::string with_params = std::string (sym) + "(int)";
2807 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2808 EXPECT (sym));
2809
2810 /* Should be able to match all existing symbols with
2811 parameters and qualifiers. */
2812 with_params = std::string (sym) + " ( int ) const";
2813 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2814 EXPECT (sym));
2815
2816 /* This should really find sym, but cp-name-parser.y doesn't
2817 know about lvalue/rvalue qualifiers yet. */
2818 with_params = std::string (sym) + " ( int ) &&";
2819 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2820 {});
2821 }
2822
2823 /* Check that the name matching algorithm for completion doesn't get
2824 confused with Latin1 'ÿ' / 0xff. See
2825 make_sort_after_prefix_name. */
2826 {
2827 static const char str[] = "\377";
2828 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
2829 EXPECT ("\377", "\377\377123"));
2830 }
2831
2832 /* Check that the increment-last-char in the matching algorithm for
2833 completion doesn't match "t1_fund" when completing "t1_func". */
2834 {
2835 static const char str[] = "t1_func";
2836 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
2837 EXPECT ("t1_func", "t1_func1"));
2838 }
2839
2840 /* Check that completion mode works at each prefix of the expected
2841 symbol name. */
2842 {
2843 static const char str[] = "function(int)";
2844 size_t len = strlen (str);
2845 std::string lookup;
2846
2847 for (size_t i = 1; i < len; i++)
2848 {
2849 lookup.assign (str, i);
2850 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
2851 EXPECT ("function"));
2852 }
2853 }
2854
2855 /* While "w" is a prefix of both components, the match function
2856 should still only be called once. */
2857 {
2858 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
2859 EXPECT ("w1::w2"));
2860 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
2861 EXPECT ("w1::w2"));
2862 }
2863
2864 /* Same, with a "complicated" symbol. */
2865 {
2866 static const char str[] = Z_SYM_NAME;
2867 size_t len = strlen (str);
2868 std::string lookup;
2869
2870 for (size_t i = 1; i < len; i++)
2871 {
2872 lookup.assign (str, i);
2873 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
2874 EXPECT (Z_SYM_NAME));
2875 }
2876 }
2877
2878 /* In FULL mode, an incomplete symbol doesn't match. */
2879 {
2880 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
2881 {});
2882 }
2883
2884 /* A complete symbol with parameters matches any overload, since the
2885 index has no overload info. */
2886 {
2887 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
2888 EXPECT ("std::zfunction", "std::zfunction2"));
2889 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
2890 EXPECT ("std::zfunction", "std::zfunction2"));
2891 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
2892 EXPECT ("std::zfunction", "std::zfunction2"));
2893 }
2894
2895 /* Check that whitespace is ignored appropriately. A symbol with a
2896 template argument list. */
2897 {
2898 static const char expected[] = "ns::foo<int>";
2899 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
2900 EXPECT (expected));
2901 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
2902 EXPECT (expected));
2903 }
2904
2905 /* Check that whitespace is ignored appropriately. A symbol with a
2906 template argument list that includes a pointer. */
2907 {
2908 static const char expected[] = "ns::foo<char*>";
2909 /* Try both completion and non-completion modes. */
2910 static const bool completion_mode[2] = {false, true};
2911 for (size_t i = 0; i < 2; i++)
2912 {
2913 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
2914 completion_mode[i], EXPECT (expected));
2915 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
2916 completion_mode[i], EXPECT (expected));
2917
2918 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
2919 completion_mode[i], EXPECT (expected));
2920 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
2921 completion_mode[i], EXPECT (expected));
2922 }
2923 }
2924
2925 {
2926 /* Check method qualifiers are ignored. */
2927 static const char expected[] = "ns::foo<char*>";
2928 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
2929 symbol_name_match_type::FULL, true, EXPECT (expected));
2930 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
2931 symbol_name_match_type::FULL, true, EXPECT (expected));
2932 CHECK_MATCH ("foo < char * > ( int ) const",
2933 symbol_name_match_type::WILD, true, EXPECT (expected));
2934 CHECK_MATCH ("foo < char * > ( int ) &&",
2935 symbol_name_match_type::WILD, true, EXPECT (expected));
2936 }
2937
2938 /* Test lookup names that don't match anything. */
2939 {
2940 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
2941 {});
2942
2943 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
2944 {});
2945 }
2946
2947 /* Some wild matching tests, exercising "(anonymous namespace)",
2948 which should not be confused with a parameter list. */
2949 {
2950 static const char *syms[] = {
2951 "A::B::C",
2952 "B::C",
2953 "C",
2954 "A :: B :: C ( int )",
2955 "B :: C ( int )",
2956 "C ( int )",
2957 };
2958
2959 for (const char *s : syms)
2960 {
2961 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
2962 EXPECT ("(anonymous namespace)::A::B::C"));
2963 }
2964 }
2965
2966 {
2967 static const char expected[] = "ns2::tmpl<int>::foo2";
2968 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
2969 EXPECT (expected));
2970 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
2971 EXPECT (expected));
2972 }
2973
2974 SELF_CHECK (!any_mismatch);
2975
2976 #undef EXPECT
2977 #undef CHECK_MATCH
2978 }
2979
2980 static void
2981 run_test ()
2982 {
2983 test_mapped_index_find_name_component_bounds ();
2984 test_dw2_expand_symtabs_matching_symbol ();
2985 }
2986
2987 }} // namespace selftests::dw2_expand_symtabs_matching
2988
2989 #endif /* GDB_SELF_TEST */
2990
2991 /* See read.h. */
2992
2993 bool
2994 dw2_expand_symtabs_matching_one
2995 (dwarf2_per_cu_data *per_cu,
2996 dwarf2_per_objfile *per_objfile,
2997 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2998 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
2999 {
3000 if (file_matcher == NULL || per_cu->mark)
3001 {
3002 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
3003
3004 compunit_symtab *symtab
3005 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3006 gdb_assert (symtab != nullptr);
3007
3008 if (expansion_notify != NULL && symtab_was_null)
3009 return expansion_notify (symtab);
3010 }
3011 return true;
3012 }
3013
3014 /* See read.h. */
3015
3016 void
3017 dw_expand_symtabs_matching_file_matcher
3018 (dwarf2_per_objfile *per_objfile,
3019 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
3020 {
3021 if (file_matcher == NULL)
3022 return;
3023
3024 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
3025 htab_eq_pointer,
3026 NULL, xcalloc, xfree));
3027 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
3028 htab_eq_pointer,
3029 NULL, xcalloc, xfree));
3030
3031 /* The rule is CUs specify all the files, including those used by
3032 any TU, so there's no need to scan TUs here. */
3033
3034 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3035 {
3036 QUIT;
3037
3038 if (per_cu->is_debug_types)
3039 continue;
3040 per_cu->mark = 0;
3041
3042 /* We only need to look at symtabs not already expanded. */
3043 if (per_objfile->symtab_set_p (per_cu.get ()))
3044 continue;
3045
3046 if (per_cu->fnd != nullptr)
3047 {
3048 file_and_directory *fnd = per_cu->fnd.get ();
3049
3050 if (file_matcher (fnd->get_name (), false))
3051 {
3052 per_cu->mark = 1;
3053 continue;
3054 }
3055
3056 /* Before we invoke realpath, which can get expensive when many
3057 files are involved, do a quick comparison of the basenames. */
3058 if ((basenames_may_differ
3059 || file_matcher (lbasename (fnd->get_name ()), true))
3060 && file_matcher (fnd->get_fullname (), false))
3061 {
3062 per_cu->mark = 1;
3063 continue;
3064 }
3065 }
3066
3067 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
3068 per_objfile);
3069 if (file_data == NULL)
3070 continue;
3071
3072 if (htab_find (visited_not_found.get (), file_data) != NULL)
3073 continue;
3074 else if (htab_find (visited_found.get (), file_data) != NULL)
3075 {
3076 per_cu->mark = 1;
3077 continue;
3078 }
3079
3080 for (int j = 0; j < file_data->num_file_names; ++j)
3081 {
3082 const char *this_real_name;
3083
3084 if (file_matcher (file_data->file_names[j], false))
3085 {
3086 per_cu->mark = 1;
3087 break;
3088 }
3089
3090 /* Before we invoke realpath, which can get expensive when many
3091 files are involved, do a quick comparison of the basenames. */
3092 if (!basenames_may_differ
3093 && !file_matcher (lbasename (file_data->file_names[j]),
3094 true))
3095 continue;
3096
3097 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3098 if (file_matcher (this_real_name, false))
3099 {
3100 per_cu->mark = 1;
3101 break;
3102 }
3103 }
3104
3105 void **slot = htab_find_slot (per_cu->mark
3106 ? visited_found.get ()
3107 : visited_not_found.get (),
3108 file_data, INSERT);
3109 *slot = file_data;
3110 }
3111 }
3112
3113
3114 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
3115 symtab. */
3116
3117 static struct compunit_symtab *
3118 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
3119 CORE_ADDR pc)
3120 {
3121 int i;
3122
3123 if (cust->blockvector () != nullptr
3124 && blockvector_contains_pc (cust->blockvector (), pc))
3125 return cust;
3126
3127 if (cust->includes == NULL)
3128 return NULL;
3129
3130 for (i = 0; cust->includes[i]; ++i)
3131 {
3132 struct compunit_symtab *s = cust->includes[i];
3133
3134 s = recursively_find_pc_sect_compunit_symtab (s, pc);
3135 if (s != NULL)
3136 return s;
3137 }
3138
3139 return NULL;
3140 }
3141
3142 dwarf2_per_cu_data *
3143 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
3144 CORE_ADDR adjusted_pc)
3145 {
3146 if (per_bfd->index_addrmap == nullptr)
3147 return nullptr;
3148
3149 void *obj = per_bfd->index_addrmap->find (adjusted_pc);
3150 return static_cast<dwarf2_per_cu_data *> (obj);
3151 }
3152
3153 struct compunit_symtab *
3154 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
3155 (struct objfile *objfile,
3156 struct bound_minimal_symbol msymbol,
3157 CORE_ADDR pc,
3158 struct obj_section *section,
3159 int warn_if_readin)
3160 {
3161 struct compunit_symtab *result;
3162
3163 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3164
3165 CORE_ADDR baseaddr = objfile->text_section_offset ();
3166 struct dwarf2_per_cu_data *data = find_per_cu (per_objfile->per_bfd,
3167 pc - baseaddr);
3168 if (data == nullptr)
3169 return nullptr;
3170
3171 if (warn_if_readin && per_objfile->symtab_set_p (data))
3172 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3173 paddress (objfile->arch (), pc));
3174
3175 result = recursively_find_pc_sect_compunit_symtab
3176 (dw2_instantiate_symtab (data, per_objfile, false), pc);
3177
3178 if (warn_if_readin && result == nullptr)
3179 warning (_("(Error: pc %s in address map, but not in symtab.)"),
3180 paddress (objfile->arch (), pc));
3181
3182 return result;
3183 }
3184
3185 void
3186 dwarf2_base_index_functions::map_symbol_filenames
3187 (struct objfile *objfile,
3188 gdb::function_view<symbol_filename_ftype> fun,
3189 bool need_fullname)
3190 {
3191 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3192
3193 /* Use caches to ensure we only call FUN once for each filename. */
3194 filename_seen_cache filenames_cache;
3195 std::unordered_set<quick_file_names *> qfn_cache;
3196
3197 /* The rule is CUs specify all the files, including those used by any TU,
3198 so there's no need to scan TUs here. We can ignore file names coming
3199 from already-expanded CUs. It is possible that an expanded CU might
3200 reuse the file names data from a currently unexpanded CU, in this
3201 case we don't want to report the files from the unexpanded CU. */
3202
3203 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3204 {
3205 if (!per_cu->is_debug_types
3206 && per_objfile->symtab_set_p (per_cu.get ()))
3207 {
3208 if (per_cu->file_names != nullptr)
3209 qfn_cache.insert (per_cu->file_names);
3210 }
3211 }
3212
3213 for (dwarf2_per_cu_data *per_cu
3214 : all_units_range (per_objfile->per_bfd))
3215 {
3216 /* We only need to look at symtabs not already expanded. */
3217 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
3218 continue;
3219
3220 if (per_cu->fnd != nullptr)
3221 {
3222 file_and_directory *fnd = per_cu->fnd.get ();
3223
3224 const char *filename = fnd->get_name ();
3225 const char *key = filename;
3226 const char *fullname = nullptr;
3227
3228 if (need_fullname)
3229 {
3230 fullname = fnd->get_fullname ();
3231 key = fullname;
3232 }
3233
3234 if (!filenames_cache.seen (key))
3235 fun (filename, fullname);
3236 }
3237
3238 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3239 if (file_data == nullptr
3240 || qfn_cache.find (file_data) != qfn_cache.end ())
3241 continue;
3242
3243 for (int j = 0; j < file_data->num_file_names; ++j)
3244 {
3245 const char *filename = file_data->file_names[j];
3246 const char *key = filename;
3247 const char *fullname = nullptr;
3248
3249 if (need_fullname)
3250 {
3251 fullname = dw2_get_real_path (per_objfile, file_data, j);
3252 key = fullname;
3253 }
3254
3255 if (!filenames_cache.seen (key))
3256 fun (filename, fullname);
3257 }
3258 }
3259 }
3260
3261 bool
3262 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
3263 {
3264 return true;
3265 }
3266
3267 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
3268
3269 bool
3270 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
3271 {
3272 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3273
3274 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3275 {
3276 /* Is this already expanded? */
3277 if (per_objfile->symtab_set_p (per_cu.get ()))
3278 continue;
3279
3280 /* It has not yet been expanded. */
3281 return true;
3282 }
3283
3284 return false;
3285 }
3286
3287 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
3288 to either a dwarf2_per_bfd or dwz_file object. */
3289
3290 template <typename T>
3291 static gdb::array_view<const gdb_byte>
3292 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
3293 {
3294 dwarf2_section_info *section = &section_owner->gdb_index;
3295
3296 if (section->empty ())
3297 return {};
3298
3299 /* Older elfutils strip versions could keep the section in the main
3300 executable while splitting it for the separate debug info file. */
3301 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
3302 return {};
3303
3304 section->read (obj);
3305
3306 /* dwarf2_section_info::size is a bfd_size_type, while
3307 gdb::array_view works with size_t. On 32-bit hosts, with
3308 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
3309 is 32-bit. So we need an explicit narrowing conversion here.
3310 This is fine, because it's impossible to allocate or mmap an
3311 array/buffer larger than what size_t can represent. */
3312 return gdb::make_array_view (section->buffer, section->size);
3313 }
3314
3315 /* Lookup the index cache for the contents of the index associated to
3316 DWARF2_OBJ. */
3317
3318 static gdb::array_view<const gdb_byte>
3319 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
3320 {
3321 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
3322 if (build_id == nullptr)
3323 return {};
3324
3325 return global_index_cache.lookup_gdb_index (build_id,
3326 &dwarf2_per_bfd->index_cache_res);
3327 }
3328
3329 /* Same as the above, but for DWZ. */
3330
3331 static gdb::array_view<const gdb_byte>
3332 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
3333 {
3334 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
3335 if (build_id == nullptr)
3336 return {};
3337
3338 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
3339 }
3340
3341 static quick_symbol_functions_up make_cooked_index_funcs ();
3342
3343 /* See dwarf2/public.h. */
3344
3345 void
3346 dwarf2_initialize_objfile (struct objfile *objfile)
3347 {
3348 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3349 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3350
3351 dwarf_read_debug_printf ("called");
3352
3353 /* If we're about to read full symbols, don't bother with the
3354 indices. In this case we also don't care if some other debug
3355 format is making psymtabs, because they are all about to be
3356 expanded anyway. */
3357 if ((objfile->flags & OBJF_READNOW))
3358 {
3359 dwarf_read_debug_printf ("readnow requested");
3360
3361 create_all_units (per_objfile);
3362 per_bfd->quick_file_names_table
3363 = create_quick_file_names_table (per_bfd->all_units.size ());
3364
3365 objfile->qf.emplace_front (new readnow_functions);
3366 return;
3367 }
3368
3369 /* Was a GDB index already read when we processed an objfile sharing
3370 PER_BFD? */
3371 if (per_bfd->index_table != nullptr)
3372 {
3373 dwarf_read_debug_printf ("re-using symbols");
3374 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3375 return;
3376 }
3377
3378 if (dwarf2_read_debug_names (per_objfile))
3379 {
3380 dwarf_read_debug_printf ("found debug names");
3381 objfile->qf.push_front
3382 (per_bfd->index_table->make_quick_functions ());
3383 return;
3384 }
3385
3386 if (dwarf2_read_gdb_index (per_objfile,
3387 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
3388 get_gdb_index_contents_from_section<dwz_file>))
3389 {
3390 dwarf_read_debug_printf ("found gdb index from file");
3391 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3392 return;
3393 }
3394
3395 /* ... otherwise, try to find the index in the index cache. */
3396 if (dwarf2_read_gdb_index (per_objfile,
3397 get_gdb_index_contents_from_cache,
3398 get_gdb_index_contents_from_cache_dwz))
3399 {
3400 dwarf_read_debug_printf ("found gdb index from cache");
3401 global_index_cache.hit ();
3402 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3403 return;
3404 }
3405
3406 global_index_cache.miss ();
3407 objfile->qf.push_front (make_cooked_index_funcs ());
3408 }
3409
3410 \f
3411
3412 /* Build a partial symbol table. */
3413
3414 static void
3415 dwarf2_build_psymtabs (struct objfile *objfile)
3416 {
3417 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3418
3419 if (per_objfile->per_bfd->index_table != nullptr)
3420 return;
3421
3422 try
3423 {
3424 dwarf2_build_psymtabs_hard (per_objfile);
3425
3426 /* (maybe) store an index in the cache. */
3427 global_index_cache.store (per_objfile);
3428 }
3429 catch (const gdb_exception_error &except)
3430 {
3431 exception_print (gdb_stderr, except);
3432 }
3433 }
3434
3435 /* Find the base address of the compilation unit for range lists and
3436 location lists. It will normally be specified by DW_AT_low_pc.
3437 In DWARF-3 draft 4, the base address could be overridden by
3438 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3439 compilation units with discontinuous ranges. */
3440
3441 static void
3442 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3443 {
3444 struct attribute *attr;
3445
3446 cu->base_address.reset ();
3447
3448 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3449 if (attr != nullptr)
3450 cu->base_address = attr->as_address ();
3451 else
3452 {
3453 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3454 if (attr != nullptr)
3455 cu->base_address = attr->as_address ();
3456 }
3457 }
3458
3459 /* Helper function that returns the proper abbrev section for
3460 THIS_CU. */
3461
3462 static struct dwarf2_section_info *
3463 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3464 {
3465 struct dwarf2_section_info *abbrev;
3466 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
3467
3468 if (this_cu->is_dwz)
3469 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
3470 else
3471 abbrev = &per_bfd->abbrev;
3472
3473 return abbrev;
3474 }
3475
3476 /* Fetch the abbreviation table offset from a comp or type unit header. */
3477
3478 static sect_offset
3479 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
3480 struct dwarf2_section_info *section,
3481 sect_offset sect_off)
3482 {
3483 bfd *abfd = section->get_bfd_owner ();
3484 const gdb_byte *info_ptr;
3485 unsigned int initial_length_size, offset_size;
3486 uint16_t version;
3487
3488 section->read (per_objfile->objfile);
3489 info_ptr = section->buffer + to_underlying (sect_off);
3490 read_initial_length (abfd, info_ptr, &initial_length_size);
3491 offset_size = initial_length_size == 4 ? 4 : 8;
3492 info_ptr += initial_length_size;
3493
3494 version = read_2_bytes (abfd, info_ptr);
3495 info_ptr += 2;
3496 if (version >= 5)
3497 {
3498 /* Skip unit type and address size. */
3499 info_ptr += 2;
3500 }
3501
3502 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
3503 }
3504
3505 static hashval_t
3506 hash_signatured_type (const void *item)
3507 {
3508 const struct signatured_type *sig_type
3509 = (const struct signatured_type *) item;
3510
3511 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3512 return sig_type->signature;
3513 }
3514
3515 static int
3516 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3517 {
3518 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
3519 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
3520
3521 return lhs->signature == rhs->signature;
3522 }
3523
3524 /* See read.h. */
3525
3526 htab_up
3527 allocate_signatured_type_table ()
3528 {
3529 return htab_up (htab_create_alloc (41,
3530 hash_signatured_type,
3531 eq_signatured_type,
3532 NULL, xcalloc, xfree));
3533 }
3534
3535 /* A helper for create_debug_types_hash_table. Read types from SECTION
3536 and fill them into TYPES_HTAB. It will process only type units,
3537 therefore DW_UT_type. */
3538
3539 static void
3540 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
3541 struct dwo_file *dwo_file,
3542 dwarf2_section_info *section, htab_up &types_htab,
3543 rcuh_kind section_kind)
3544 {
3545 struct objfile *objfile = per_objfile->objfile;
3546 struct dwarf2_section_info *abbrev_section;
3547 bfd *abfd;
3548 const gdb_byte *info_ptr, *end_ptr;
3549
3550 abbrev_section = &dwo_file->sections.abbrev;
3551
3552 dwarf_read_debug_printf ("Reading %s for %s",
3553 section->get_name (),
3554 abbrev_section->get_file_name ());
3555
3556 section->read (objfile);
3557 info_ptr = section->buffer;
3558
3559 if (info_ptr == NULL)
3560 return;
3561
3562 /* We can't set abfd until now because the section may be empty or
3563 not present, in which case the bfd is unknown. */
3564 abfd = section->get_bfd_owner ();
3565
3566 /* We don't use cutu_reader here because we don't need to read
3567 any dies: the signature is in the header. */
3568
3569 end_ptr = info_ptr + section->size;
3570 while (info_ptr < end_ptr)
3571 {
3572 signatured_type_up sig_type;
3573 struct dwo_unit *dwo_tu;
3574 void **slot;
3575 const gdb_byte *ptr = info_ptr;
3576 struct comp_unit_head header;
3577 unsigned int length;
3578
3579 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
3580
3581 /* Initialize it due to a false compiler warning. */
3582 header.signature = -1;
3583 header.type_cu_offset_in_tu = (cu_offset) -1;
3584
3585 /* We need to read the type's signature in order to build the hash
3586 table, but we don't need anything else just yet. */
3587
3588 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
3589 abbrev_section, ptr, section_kind);
3590
3591 length = header.get_length_with_initial ();
3592
3593 /* Skip dummy type units. */
3594 if (ptr >= info_ptr + length
3595 || peek_abbrev_code (abfd, ptr) == 0
3596 || (header.unit_type != DW_UT_type
3597 && header.unit_type != DW_UT_split_type))
3598 {
3599 info_ptr += length;
3600 continue;
3601 }
3602
3603 if (types_htab == NULL)
3604 types_htab = allocate_dwo_unit_table ();
3605
3606 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
3607 dwo_tu->dwo_file = dwo_file;
3608 dwo_tu->signature = header.signature;
3609 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
3610 dwo_tu->section = section;
3611 dwo_tu->sect_off = sect_off;
3612 dwo_tu->length = length;
3613
3614 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
3615 gdb_assert (slot != NULL);
3616 if (*slot != NULL)
3617 complaint (_("debug type entry at offset %s is duplicate to"
3618 " the entry at offset %s, signature %s"),
3619 sect_offset_str (sect_off),
3620 sect_offset_str (dwo_tu->sect_off),
3621 hex_string (header.signature));
3622 *slot = dwo_tu;
3623
3624 dwarf_read_debug_printf_v (" offset %s, signature %s",
3625 sect_offset_str (sect_off),
3626 hex_string (header.signature));
3627
3628 info_ptr += length;
3629 }
3630 }
3631
3632 /* Create the hash table of all entries in the .debug_types
3633 (or .debug_types.dwo) section(s).
3634 DWO_FILE is a pointer to the DWO file object.
3635
3636 The result is a pointer to the hash table or NULL if there are no types.
3637
3638 Note: This function processes DWO files only, not DWP files. */
3639
3640 static void
3641 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
3642 struct dwo_file *dwo_file,
3643 gdb::array_view<dwarf2_section_info> type_sections,
3644 htab_up &types_htab)
3645 {
3646 for (dwarf2_section_info &section : type_sections)
3647 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
3648 rcuh_kind::TYPE);
3649 }
3650
3651 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
3652 If SLOT is non-NULL, it is the entry to use in the hash table.
3653 Otherwise we find one. */
3654
3655 static struct signatured_type *
3656 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
3657 {
3658 if (per_objfile->per_bfd->all_units.size ()
3659 == per_objfile->per_bfd->all_units.capacity ())
3660 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
3661
3662 signatured_type_up sig_type_holder
3663 = per_objfile->per_bfd->allocate_signatured_type (sig);
3664 signatured_type *sig_type = sig_type_holder.get ();
3665
3666 per_objfile->per_bfd->all_units.emplace_back
3667 (sig_type_holder.release ());
3668
3669 if (slot == NULL)
3670 {
3671 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3672 sig_type, INSERT);
3673 }
3674 gdb_assert (*slot == NULL);
3675 *slot = sig_type;
3676 /* The rest of sig_type must be filled in by the caller. */
3677 return sig_type;
3678 }
3679
3680 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
3681 Fill in SIG_ENTRY with DWO_ENTRY. */
3682
3683 static void
3684 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
3685 struct signatured_type *sig_entry,
3686 struct dwo_unit *dwo_entry)
3687 {
3688 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3689
3690 /* Make sure we're not clobbering something we don't expect to. */
3691 gdb_assert (! sig_entry->queued);
3692 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
3693 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
3694 gdb_assert (sig_entry->signature == dwo_entry->signature);
3695 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
3696 || (to_underlying (sig_entry->type_offset_in_section)
3697 == to_underlying (dwo_entry->type_offset_in_tu)));
3698 gdb_assert (sig_entry->type_unit_group == NULL);
3699 gdb_assert (sig_entry->dwo_unit == NULL
3700 || sig_entry->dwo_unit == dwo_entry);
3701
3702 sig_entry->section = dwo_entry->section;
3703 sig_entry->sect_off = dwo_entry->sect_off;
3704 sig_entry->set_length (dwo_entry->length, false);
3705 sig_entry->reading_dwo_directly = 1;
3706 sig_entry->per_bfd = per_bfd;
3707 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
3708 sig_entry->dwo_unit = dwo_entry;
3709 }
3710
3711 /* Subroutine of lookup_signatured_type.
3712 If we haven't read the TU yet, create the signatured_type data structure
3713 for a TU to be read in directly from a DWO file, bypassing the stub.
3714 This is the "Stay in DWO Optimization": When there is no DWP file and we're
3715 using .gdb_index, then when reading a CU we want to stay in the DWO file
3716 containing that CU. Otherwise we could end up reading several other DWO
3717 files (due to comdat folding) to process the transitive closure of all the
3718 mentioned TUs, and that can be slow. The current DWO file will have every
3719 type signature that it needs.
3720 We only do this for .gdb_index because in the psymtab case we already have
3721 to read all the DWOs to build the type unit groups. */
3722
3723 static struct signatured_type *
3724 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3725 {
3726 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3727 struct dwo_file *dwo_file;
3728 struct dwo_unit find_dwo_entry, *dwo_entry;
3729 void **slot;
3730
3731 gdb_assert (cu->dwo_unit);
3732
3733 /* If TU skeletons have been removed then we may not have read in any
3734 TUs yet. */
3735 if (per_objfile->per_bfd->signatured_types == NULL)
3736 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
3737
3738 /* We only ever need to read in one copy of a signatured type.
3739 Use the global signatured_types array to do our own comdat-folding
3740 of types. If this is the first time we're reading this TU, and
3741 the TU has an entry in .gdb_index, replace the recorded data from
3742 .gdb_index with this TU. */
3743
3744 signatured_type find_sig_entry (sig);
3745 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3746 &find_sig_entry, INSERT);
3747 signatured_type *sig_entry = (struct signatured_type *) *slot;
3748
3749 /* We can get here with the TU already read, *or* in the process of being
3750 read. Don't reassign the global entry to point to this DWO if that's
3751 the case. Also note that if the TU is already being read, it may not
3752 have come from a DWO, the program may be a mix of Fission-compiled
3753 code and non-Fission-compiled code. */
3754
3755 /* Have we already tried to read this TU?
3756 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3757 needn't exist in the global table yet). */
3758 if (sig_entry != NULL && sig_entry->tu_read)
3759 return sig_entry;
3760
3761 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
3762 dwo_unit of the TU itself. */
3763 dwo_file = cu->dwo_unit->dwo_file;
3764
3765 /* Ok, this is the first time we're reading this TU. */
3766 if (dwo_file->tus == NULL)
3767 return NULL;
3768 find_dwo_entry.signature = sig;
3769 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
3770 &find_dwo_entry);
3771 if (dwo_entry == NULL)
3772 return NULL;
3773
3774 /* If the global table doesn't have an entry for this TU, add one. */
3775 if (sig_entry == NULL)
3776 sig_entry = add_type_unit (per_objfile, sig, slot);
3777
3778 if (sig_entry->dwo_unit == nullptr)
3779 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
3780 sig_entry->tu_read = 1;
3781 return sig_entry;
3782 }
3783
3784 /* Subroutine of lookup_signatured_type.
3785 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
3786 then try the DWP file. If the TU stub (skeleton) has been removed then
3787 it won't be in .gdb_index. */
3788
3789 static struct signatured_type *
3790 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3791 {
3792 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3793 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
3794 struct dwo_unit *dwo_entry;
3795 void **slot;
3796
3797 gdb_assert (cu->dwo_unit);
3798 gdb_assert (dwp_file != NULL);
3799
3800 /* If TU skeletons have been removed then we may not have read in any
3801 TUs yet. */
3802 if (per_objfile->per_bfd->signatured_types == NULL)
3803 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
3804
3805 signatured_type find_sig_entry (sig);
3806 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3807 &find_sig_entry, INSERT);
3808 signatured_type *sig_entry = (struct signatured_type *) *slot;
3809
3810 /* Have we already tried to read this TU?
3811 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3812 needn't exist in the global table yet). */
3813 if (sig_entry != NULL)
3814 return sig_entry;
3815
3816 if (dwp_file->tus == NULL)
3817 return NULL;
3818 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
3819 1 /* is_debug_types */);
3820 if (dwo_entry == NULL)
3821 return NULL;
3822
3823 sig_entry = add_type_unit (per_objfile, sig, slot);
3824 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
3825
3826 return sig_entry;
3827 }
3828
3829 /* Lookup a signature based type for DW_FORM_ref_sig8.
3830 Returns NULL if signature SIG is not present in the table.
3831 It is up to the caller to complain about this. */
3832
3833 static struct signatured_type *
3834 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3835 {
3836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3837
3838 if (cu->dwo_unit)
3839 {
3840 /* We're in a DWO/DWP file, and we're using .gdb_index.
3841 These cases require special processing. */
3842 if (get_dwp_file (per_objfile) == NULL)
3843 return lookup_dwo_signatured_type (cu, sig);
3844 else
3845 return lookup_dwp_signatured_type (cu, sig);
3846 }
3847 else
3848 {
3849 if (per_objfile->per_bfd->signatured_types == NULL)
3850 return NULL;
3851 signatured_type find_entry (sig);
3852 return ((struct signatured_type *)
3853 htab_find (per_objfile->per_bfd->signatured_types.get (),
3854 &find_entry));
3855 }
3856 }
3857
3858 /* Low level DIE reading support. */
3859
3860 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3861
3862 static void
3863 init_cu_die_reader (struct die_reader_specs *reader,
3864 struct dwarf2_cu *cu,
3865 struct dwarf2_section_info *section,
3866 struct dwo_file *dwo_file,
3867 struct abbrev_table *abbrev_table)
3868 {
3869 gdb_assert (section->readin && section->buffer != NULL);
3870 reader->abfd = section->get_bfd_owner ();
3871 reader->cu = cu;
3872 reader->dwo_file = dwo_file;
3873 reader->die_section = section;
3874 reader->buffer = section->buffer;
3875 reader->buffer_end = section->buffer + section->size;
3876 reader->abbrev_table = abbrev_table;
3877 }
3878
3879 /* Subroutine of cutu_reader to simplify it.
3880 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
3881 There's just a lot of work to do, and cutu_reader is big enough
3882 already.
3883
3884 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
3885 from it to the DIE in the DWO. If NULL we are skipping the stub.
3886 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
3887 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
3888 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
3889 STUB_COMP_DIR may be non-NULL.
3890 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
3891 are filled in with the info of the DIE from the DWO file.
3892 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
3893 from the dwo. Since *RESULT_READER references this abbrev table, it must be
3894 kept around for at least as long as *RESULT_READER.
3895
3896 The result is non-zero if a valid (non-dummy) DIE was found. */
3897
3898 static int
3899 read_cutu_die_from_dwo (dwarf2_cu *cu,
3900 struct dwo_unit *dwo_unit,
3901 struct die_info *stub_comp_unit_die,
3902 const char *stub_comp_dir,
3903 struct die_reader_specs *result_reader,
3904 const gdb_byte **result_info_ptr,
3905 struct die_info **result_comp_unit_die,
3906 abbrev_table_up *result_dwo_abbrev_table)
3907 {
3908 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3909 dwarf2_per_cu_data *per_cu = cu->per_cu;
3910 struct objfile *objfile = per_objfile->objfile;
3911 bfd *abfd;
3912 const gdb_byte *begin_info_ptr, *info_ptr;
3913 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3914 int i,num_extra_attrs;
3915 struct dwarf2_section_info *dwo_abbrev_section;
3916 struct die_info *comp_unit_die;
3917
3918 /* At most one of these may be provided. */
3919 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
3920
3921 /* These attributes aren't processed until later:
3922 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3923 DW_AT_comp_dir is used now, to find the DWO file, but it is also
3924 referenced later. However, these attributes are found in the stub
3925 which we won't have later. In order to not impose this complication
3926 on the rest of the code, we read them here and copy them to the
3927 DWO CU/TU die. */
3928
3929 stmt_list = NULL;
3930 low_pc = NULL;
3931 high_pc = NULL;
3932 ranges = NULL;
3933 comp_dir = NULL;
3934
3935 if (stub_comp_unit_die != NULL)
3936 {
3937 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3938 DWO file. */
3939 if (!per_cu->is_debug_types)
3940 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
3941 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
3942 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
3943 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
3944 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
3945
3946 cu->addr_base = stub_comp_unit_die->addr_base ();
3947
3948 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
3949 We need the value before we can process DW_AT_ranges values from the
3950 DWO. */
3951 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
3952
3953 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
3954 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
3955 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
3956 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
3957 section. */
3958 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
3959 }
3960 else if (stub_comp_dir != NULL)
3961 {
3962 /* Reconstruct the comp_dir attribute to simplify the code below. */
3963 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
3964 comp_dir->name = DW_AT_comp_dir;
3965 comp_dir->form = DW_FORM_string;
3966 comp_dir->set_string_noncanonical (stub_comp_dir);
3967 }
3968
3969 /* Set up for reading the DWO CU/TU. */
3970 cu->dwo_unit = dwo_unit;
3971 dwarf2_section_info *section = dwo_unit->section;
3972 section->read (objfile);
3973 abfd = section->get_bfd_owner ();
3974 begin_info_ptr = info_ptr = (section->buffer
3975 + to_underlying (dwo_unit->sect_off));
3976 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3977
3978 if (per_cu->is_debug_types)
3979 {
3980 signatured_type *sig_type = (struct signatured_type *) per_cu;
3981
3982 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3983 section, dwo_abbrev_section,
3984 info_ptr, rcuh_kind::TYPE);
3985 /* This is not an assert because it can be caused by bad debug info. */
3986 if (sig_type->signature != cu->header.signature)
3987 {
3988 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
3989 " TU at offset %s [in module %s]"),
3990 hex_string (sig_type->signature),
3991 hex_string (cu->header.signature),
3992 sect_offset_str (dwo_unit->sect_off),
3993 bfd_get_filename (abfd));
3994 }
3995 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
3996 /* For DWOs coming from DWP files, we don't know the CU length
3997 nor the type's offset in the TU until now. */
3998 dwo_unit->length = cu->header.get_length_with_initial ();
3999 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
4000
4001 /* Establish the type offset that can be used to lookup the type.
4002 For DWO files, we don't know it until now. */
4003 sig_type->type_offset_in_section
4004 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
4005 }
4006 else
4007 {
4008 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4009 section, dwo_abbrev_section,
4010 info_ptr, rcuh_kind::COMPILE);
4011 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
4012 /* For DWOs coming from DWP files, we don't know the CU length
4013 until now. */
4014 dwo_unit->length = cu->header.get_length_with_initial ();
4015 }
4016
4017 dwo_abbrev_section->read (objfile);
4018 *result_dwo_abbrev_table
4019 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
4020 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
4021 result_dwo_abbrev_table->get ());
4022
4023 /* Read in the die, but leave space to copy over the attributes
4024 from the stub. This has the benefit of simplifying the rest of
4025 the code - all the work to maintain the illusion of a single
4026 DW_TAG_{compile,type}_unit DIE is done here. */
4027 num_extra_attrs = ((stmt_list != NULL)
4028 + (low_pc != NULL)
4029 + (high_pc != NULL)
4030 + (ranges != NULL)
4031 + (comp_dir != NULL));
4032 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4033 num_extra_attrs);
4034
4035 /* Copy over the attributes from the stub to the DIE we just read in. */
4036 comp_unit_die = *result_comp_unit_die;
4037 i = comp_unit_die->num_attrs;
4038 if (stmt_list != NULL)
4039 comp_unit_die->attrs[i++] = *stmt_list;
4040 if (low_pc != NULL)
4041 comp_unit_die->attrs[i++] = *low_pc;
4042 if (high_pc != NULL)
4043 comp_unit_die->attrs[i++] = *high_pc;
4044 if (ranges != NULL)
4045 comp_unit_die->attrs[i++] = *ranges;
4046 if (comp_dir != NULL)
4047 comp_unit_die->attrs[i++] = *comp_dir;
4048 comp_unit_die->num_attrs += num_extra_attrs;
4049
4050 if (dwarf_die_debug)
4051 {
4052 gdb_printf (gdb_stdlog,
4053 "Read die from %s@0x%x of %s:\n",
4054 section->get_name (),
4055 (unsigned) (begin_info_ptr - section->buffer),
4056 bfd_get_filename (abfd));
4057 comp_unit_die->dump (dwarf_die_debug);
4058 }
4059
4060 /* Skip dummy compilation units. */
4061 if (info_ptr >= begin_info_ptr + dwo_unit->length
4062 || peek_abbrev_code (abfd, info_ptr) == 0)
4063 return 0;
4064
4065 *result_info_ptr = info_ptr;
4066 return 1;
4067 }
4068
4069 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
4070 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
4071 signature is part of the header. */
4072 static gdb::optional<ULONGEST>
4073 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
4074 {
4075 if (cu->header.version >= 5)
4076 return cu->header.signature;
4077 struct attribute *attr;
4078 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4079 if (attr == nullptr || !attr->form_is_unsigned ())
4080 return gdb::optional<ULONGEST> ();
4081 return attr->as_unsigned ();
4082 }
4083
4084 /* Subroutine of cutu_reader to simplify it.
4085 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4086 Returns NULL if the specified DWO unit cannot be found. */
4087
4088 static struct dwo_unit *
4089 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
4090 {
4091 #if CXX_STD_THREAD
4092 /* We need a lock here both to handle the DWO hash table, and BFD,
4093 which is not thread-safe. */
4094 static std::mutex dwo_lock;
4095
4096 std::lock_guard<std::mutex> guard (dwo_lock);
4097 #endif
4098
4099 dwarf2_per_cu_data *per_cu = cu->per_cu;
4100 struct dwo_unit *dwo_unit;
4101 const char *comp_dir;
4102
4103 gdb_assert (cu != NULL);
4104
4105 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4106 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
4107 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
4108
4109 if (per_cu->is_debug_types)
4110 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
4111 else
4112 {
4113 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4114
4115 if (!signature.has_value ())
4116 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4117 " [in module %s]"),
4118 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
4119
4120 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
4121 }
4122
4123 return dwo_unit;
4124 }
4125
4126 /* Subroutine of cutu_reader to simplify it.
4127 See it for a description of the parameters.
4128 Read a TU directly from a DWO file, bypassing the stub. */
4129
4130 void
4131 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
4132 dwarf2_per_objfile *per_objfile,
4133 dwarf2_cu *existing_cu)
4134 {
4135 struct signatured_type *sig_type;
4136
4137 /* Verify we can do the following downcast, and that we have the
4138 data we need. */
4139 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4140 sig_type = (struct signatured_type *) this_cu;
4141 gdb_assert (sig_type->dwo_unit != NULL);
4142
4143 dwarf2_cu *cu;
4144
4145 if (existing_cu != nullptr)
4146 {
4147 cu = existing_cu;
4148 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
4149 /* There's no need to do the rereading_dwo_cu handling that
4150 cutu_reader does since we don't read the stub. */
4151 }
4152 else
4153 {
4154 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
4155 in per_objfile yet. */
4156 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
4157 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4158 cu = m_new_cu.get ();
4159 }
4160
4161 /* A future optimization, if needed, would be to use an existing
4162 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
4163 could share abbrev tables. */
4164
4165 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
4166 NULL /* stub_comp_unit_die */,
4167 sig_type->dwo_unit->dwo_file->comp_dir,
4168 this, &info_ptr,
4169 &comp_unit_die,
4170 &m_dwo_abbrev_table) == 0)
4171 {
4172 /* Dummy die. */
4173 dummy_p = true;
4174 }
4175 }
4176
4177 /* Initialize a CU (or TU) and read its DIEs.
4178 If the CU defers to a DWO file, read the DWO file as well.
4179
4180 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4181 Otherwise the table specified in the comp unit header is read in and used.
4182 This is an optimization for when we already have the abbrev table.
4183
4184 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
4185 allocated. */
4186
4187 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
4188 dwarf2_per_objfile *per_objfile,
4189 struct abbrev_table *abbrev_table,
4190 dwarf2_cu *existing_cu,
4191 bool skip_partial,
4192 abbrev_cache *cache)
4193 : die_reader_specs {},
4194 m_this_cu (this_cu)
4195 {
4196 struct objfile *objfile = per_objfile->objfile;
4197 struct dwarf2_section_info *section = this_cu->section;
4198 bfd *abfd = section->get_bfd_owner ();
4199 const gdb_byte *begin_info_ptr;
4200 struct signatured_type *sig_type = NULL;
4201 struct dwarf2_section_info *abbrev_section;
4202 /* Non-zero if CU currently points to a DWO file and we need to
4203 reread it. When this happens we need to reread the skeleton die
4204 before we can reread the DWO file (this only applies to CUs, not TUs). */
4205 int rereading_dwo_cu = 0;
4206
4207 if (dwarf_die_debug)
4208 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
4209 this_cu->is_debug_types ? "type" : "comp",
4210 sect_offset_str (this_cu->sect_off));
4211
4212 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4213 file (instead of going through the stub), short-circuit all of this. */
4214 if (this_cu->reading_dwo_directly)
4215 {
4216 /* Narrow down the scope of possibilities to have to understand. */
4217 gdb_assert (this_cu->is_debug_types);
4218 gdb_assert (abbrev_table == NULL);
4219 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
4220 return;
4221 }
4222
4223 /* This is cheap if the section is already read in. */
4224 section->read (objfile);
4225
4226 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4227
4228 abbrev_section = get_abbrev_section_for_cu (this_cu);
4229
4230 dwarf2_cu *cu;
4231
4232 if (existing_cu != nullptr)
4233 {
4234 cu = existing_cu;
4235 /* If this CU is from a DWO file we need to start over, we need to
4236 refetch the attributes from the skeleton CU.
4237 This could be optimized by retrieving those attributes from when we
4238 were here the first time: the previous comp_unit_die was stored in
4239 comp_unit_obstack. But there's no data yet that we need this
4240 optimization. */
4241 if (cu->dwo_unit != NULL)
4242 rereading_dwo_cu = 1;
4243 }
4244 else
4245 {
4246 /* If an existing_cu is provided, a dwarf2_cu must not exist for
4247 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
4248 let us know that the CU is being scanned using the parallel
4249 indexer. This assert is avoided in this case because (1) it
4250 is irrelevant, and (2) the get_cu method is not
4251 thread-safe. */
4252 gdb_assert (cache != nullptr
4253 || per_objfile->get_cu (this_cu) == nullptr);
4254 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4255 cu = m_new_cu.get ();
4256 }
4257
4258 /* Get the header. */
4259 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
4260 {
4261 /* We already have the header, there's no need to read it in again. */
4262 info_ptr += to_underlying (cu->header.first_die_cu_offset);
4263 }
4264 else
4265 {
4266 if (this_cu->is_debug_types)
4267 {
4268 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4269 section, abbrev_section,
4270 info_ptr, rcuh_kind::TYPE);
4271
4272 /* Since per_cu is the first member of struct signatured_type,
4273 we can go from a pointer to one to a pointer to the other. */
4274 sig_type = (struct signatured_type *) this_cu;
4275 gdb_assert (sig_type->signature == cu->header.signature);
4276 gdb_assert (sig_type->type_offset_in_tu
4277 == cu->header.type_cu_offset_in_tu);
4278 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4279
4280 /* LENGTH has not been set yet for type units if we're
4281 using .gdb_index. */
4282 this_cu->set_length (cu->header.get_length_with_initial ());
4283
4284 /* Establish the type offset that can be used to lookup the type. */
4285 sig_type->type_offset_in_section =
4286 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
4287
4288 this_cu->set_version (cu->header.version);
4289 }
4290 else
4291 {
4292 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4293 section, abbrev_section,
4294 info_ptr,
4295 rcuh_kind::COMPILE);
4296
4297 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4298 this_cu->set_length (cu->header.get_length_with_initial ());
4299 this_cu->set_version (cu->header.version);
4300 }
4301 }
4302
4303 /* Skip dummy compilation units. */
4304 if (info_ptr >= begin_info_ptr + this_cu->length ()
4305 || peek_abbrev_code (abfd, info_ptr) == 0)
4306 {
4307 dummy_p = true;
4308 return;
4309 }
4310
4311 /* If we don't have them yet, read the abbrevs for this compilation unit.
4312 And if we need to read them now, make sure they're freed when we're
4313 done. */
4314 if (abbrev_table != NULL)
4315 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
4316 else
4317 {
4318 if (cache != nullptr)
4319 abbrev_table = cache->find (abbrev_section,
4320 cu->header.abbrev_sect_off);
4321 if (abbrev_table == nullptr)
4322 {
4323 abbrev_section->read (objfile);
4324 m_abbrev_table_holder
4325 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
4326 abbrev_table = m_abbrev_table_holder.get ();
4327 }
4328 }
4329
4330 /* Read the top level CU/TU die. */
4331 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
4332 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
4333
4334 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
4335 {
4336 dummy_p = true;
4337 return;
4338 }
4339
4340 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4341 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
4342 table from the DWO file and pass the ownership over to us. It will be
4343 referenced from READER, so we must make sure to free it after we're done
4344 with READER.
4345
4346 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4347 DWO CU, that this test will fail (the attribute will not be present). */
4348 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
4349 if (dwo_name != nullptr)
4350 {
4351 struct dwo_unit *dwo_unit;
4352 struct die_info *dwo_comp_unit_die;
4353
4354 if (comp_unit_die->has_children)
4355 {
4356 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
4357 " has children (offset %s) [in module %s]"),
4358 sect_offset_str (this_cu->sect_off),
4359 bfd_get_filename (abfd));
4360 }
4361 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
4362 if (dwo_unit != NULL)
4363 {
4364 if (read_cutu_die_from_dwo (cu, dwo_unit,
4365 comp_unit_die, NULL,
4366 this, &info_ptr,
4367 &dwo_comp_unit_die,
4368 &m_dwo_abbrev_table) == 0)
4369 {
4370 /* Dummy die. */
4371 dummy_p = true;
4372 return;
4373 }
4374 comp_unit_die = dwo_comp_unit_die;
4375 }
4376 else
4377 {
4378 /* Yikes, we couldn't find the rest of the DIE, we only have
4379 the stub. A complaint has already been logged. There's
4380 not much more we can do except pass on the stub DIE to
4381 die_reader_func. We don't want to throw an error on bad
4382 debug info. */
4383 }
4384 }
4385 }
4386
4387 void
4388 cutu_reader::keep ()
4389 {
4390 /* Done, clean up. */
4391 gdb_assert (!dummy_p);
4392 if (m_new_cu != NULL)
4393 {
4394 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
4395 now. */
4396 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
4397 per_objfile->set_cu (m_this_cu, std::move (m_new_cu));
4398 }
4399 }
4400
4401 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
4402 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
4403 assumed to have already done the lookup to find the DWO file).
4404
4405 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4406 THIS_CU->is_debug_types, but nothing else.
4407
4408 We fill in THIS_CU->length.
4409
4410 THIS_CU->cu is always freed when done.
4411 This is done in order to not leave THIS_CU->cu in a state where we have
4412 to care whether it refers to the "main" CU or the DWO CU.
4413
4414 When parent_cu is passed, it is used to provide a default value for
4415 str_offsets_base and addr_base from the parent. */
4416
4417 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
4418 dwarf2_per_objfile *per_objfile,
4419 struct dwarf2_cu *parent_cu,
4420 struct dwo_file *dwo_file)
4421 : die_reader_specs {},
4422 m_this_cu (this_cu)
4423 {
4424 struct objfile *objfile = per_objfile->objfile;
4425 struct dwarf2_section_info *section = this_cu->section;
4426 bfd *abfd = section->get_bfd_owner ();
4427 struct dwarf2_section_info *abbrev_section;
4428 const gdb_byte *begin_info_ptr, *info_ptr;
4429
4430 if (dwarf_die_debug)
4431 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
4432 this_cu->is_debug_types ? "type" : "comp",
4433 sect_offset_str (this_cu->sect_off));
4434
4435 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
4436
4437 abbrev_section = (dwo_file != NULL
4438 ? &dwo_file->sections.abbrev
4439 : get_abbrev_section_for_cu (this_cu));
4440
4441 /* This is cheap if the section is already read in. */
4442 section->read (objfile);
4443
4444 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4445
4446 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4447 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
4448 section, abbrev_section, info_ptr,
4449 (this_cu->is_debug_types
4450 ? rcuh_kind::TYPE
4451 : rcuh_kind::COMPILE));
4452
4453 if (parent_cu != nullptr)
4454 {
4455 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
4456 m_new_cu->addr_base = parent_cu->addr_base;
4457 }
4458 this_cu->set_length (m_new_cu->header.get_length_with_initial ());
4459
4460 /* Skip dummy compilation units. */
4461 if (info_ptr >= begin_info_ptr + this_cu->length ()
4462 || peek_abbrev_code (abfd, info_ptr) == 0)
4463 {
4464 dummy_p = true;
4465 return;
4466 }
4467
4468 abbrev_section->read (objfile);
4469 m_abbrev_table_holder
4470 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
4471
4472 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
4473 m_abbrev_table_holder.get ());
4474 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
4475 }
4476
4477 \f
4478 /* Type Unit Groups.
4479
4480 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4481 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4482 so that all types coming from the same compilation (.o file) are grouped
4483 together. A future step could be to put the types in the same symtab as
4484 the CU the types ultimately came from. */
4485
4486 static hashval_t
4487 hash_type_unit_group (const void *item)
4488 {
4489 const struct type_unit_group *tu_group
4490 = (const struct type_unit_group *) item;
4491
4492 return hash_stmt_list_entry (&tu_group->hash);
4493 }
4494
4495 static int
4496 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4497 {
4498 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
4499 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
4500
4501 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4502 }
4503
4504 /* Allocate a hash table for type unit groups. */
4505
4506 static htab_up
4507 allocate_type_unit_groups_table ()
4508 {
4509 return htab_up (htab_create_alloc (3,
4510 hash_type_unit_group,
4511 eq_type_unit_group,
4512 htab_delete_entry<type_unit_group>,
4513 xcalloc, xfree));
4514 }
4515
4516 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4517 partial symtabs. We combine several TUs per psymtab to not let the size
4518 of any one psymtab grow too big. */
4519 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4520 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4521
4522 /* Helper routine for get_type_unit_group.
4523 Create the type_unit_group object used to hold one or more TUs. */
4524
4525 static std::unique_ptr<type_unit_group>
4526 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4527 {
4528 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
4529
4530 tu_group->hash.dwo_unit = cu->dwo_unit;
4531 tu_group->hash.line_sect_off = line_offset_struct;
4532
4533 return tu_group;
4534 }
4535
4536 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4537 STMT_LIST is a DW_AT_stmt_list attribute. */
4538
4539 static struct type_unit_group *
4540 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
4541 {
4542 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4543 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4544 struct type_unit_group *tu_group;
4545 void **slot;
4546 unsigned int line_offset;
4547 struct type_unit_group type_unit_group_for_lookup;
4548
4549 if (per_objfile->per_bfd->type_unit_groups == NULL)
4550 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
4551
4552 /* Do we need to create a new group, or can we use an existing one? */
4553
4554 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
4555 {
4556 line_offset = stmt_list->as_unsigned ();
4557 ++tu_stats->nr_symtab_sharers;
4558 }
4559 else
4560 {
4561 /* Ugh, no stmt_list. Rare, but we have to handle it.
4562 We can do various things here like create one group per TU or
4563 spread them over multiple groups to split up the expansion work.
4564 To avoid worst case scenarios (too many groups or too large groups)
4565 we, umm, group them in bunches. */
4566 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
4567 | (tu_stats->nr_stmt_less_type_units
4568 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
4569 ++tu_stats->nr_stmt_less_type_units;
4570 }
4571
4572 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
4573 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
4574 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
4575 &type_unit_group_for_lookup, INSERT);
4576 if (*slot == nullptr)
4577 {
4578 sect_offset line_offset_struct = (sect_offset) line_offset;
4579 std::unique_ptr<type_unit_group> grp
4580 = create_type_unit_group (cu, line_offset_struct);
4581 *slot = grp.release ();
4582 ++tu_stats->nr_symtabs;
4583 }
4584
4585 tu_group = (struct type_unit_group *) *slot;
4586 gdb_assert (tu_group != nullptr);
4587 return tu_group;
4588 }
4589 \f
4590
4591 /* An instance of this is created when scanning DWARF to create a
4592 cooked index. */
4593
4594 class cooked_index_storage
4595 {
4596 public:
4597
4598 cooked_index_storage ()
4599 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
4600 eq_cutu_reader,
4601 htab_delete_entry<cutu_reader>,
4602 xcalloc, xfree)),
4603 m_index (new cooked_index_shard)
4604 {
4605 }
4606
4607 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
4608
4609 /* Return the current abbrev cache. */
4610 abbrev_cache *get_abbrev_cache ()
4611 {
4612 return &m_abbrev_cache;
4613 }
4614
4615 /* Return the DIE reader corresponding to PER_CU. If no such reader
4616 has been registered, return NULL. */
4617 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
4618 {
4619 int index = per_cu->index;
4620 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
4621 &index, index);
4622 }
4623
4624 /* Preserve READER by storing it in the local hash table. */
4625 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
4626 {
4627 m_abbrev_cache.add (reader->release_abbrev_table ());
4628
4629 int index = reader->cu->per_cu->index;
4630 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
4631 index, INSERT);
4632 gdb_assert (*slot == nullptr);
4633 cutu_reader *result = reader.get ();
4634 *slot = reader.release ();
4635 return result;
4636 }
4637
4638 /* Add an entry to the index. The arguments describe the entry; see
4639 cooked-index.h. The new entry is returned. */
4640 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
4641 cooked_index_flag flags,
4642 const char *name,
4643 const cooked_index_entry *parent_entry,
4644 dwarf2_per_cu_data *per_cu)
4645 {
4646 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
4647 }
4648
4649 /* Install the current addrmap into the index shard being constructed,
4650 then transfer ownership of the index to the caller. */
4651 std::unique_ptr<cooked_index_shard> release ()
4652 {
4653 m_index->install_addrmap (&m_addrmap);
4654 return std::move (m_index);
4655 }
4656
4657 /* Return the mutable addrmap that is currently being created. */
4658 addrmap_mutable *get_addrmap ()
4659 {
4660 return &m_addrmap;
4661 }
4662
4663 private:
4664
4665 /* Hash function for a cutu_reader. */
4666 static hashval_t hash_cutu_reader (const void *a)
4667 {
4668 const cutu_reader *reader = (const cutu_reader *) a;
4669 return reader->cu->per_cu->index;
4670 }
4671
4672 /* Equality function for cutu_reader. */
4673 static int eq_cutu_reader (const void *a, const void *b)
4674 {
4675 const cutu_reader *ra = (const cutu_reader *) a;
4676 const int *rb = (const int *) b;
4677 return ra->cu->per_cu->index == *rb;
4678 }
4679
4680 /* The abbrev cache used by this indexer. */
4681 abbrev_cache m_abbrev_cache;
4682 /* A hash table of cutu_reader objects. */
4683 htab_up m_reader_hash;
4684 /* The index shard that is being constructed. */
4685 std::unique_ptr<cooked_index_shard> m_index;
4686
4687 /* A writeable addrmap being constructed by this scanner. */
4688 addrmap_mutable m_addrmap;
4689 };
4690
4691 /* An instance of this is created to index a CU. */
4692
4693 class cooked_indexer
4694 {
4695 public:
4696
4697 cooked_indexer (cooked_index_storage *storage,
4698 dwarf2_per_cu_data *per_cu,
4699 enum language language)
4700 : m_index_storage (storage),
4701 m_per_cu (per_cu),
4702 m_language (language)
4703 {
4704 }
4705
4706 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
4707
4708 /* Index the given CU. */
4709 void make_index (cutu_reader *reader);
4710
4711 private:
4712
4713 /* A helper function to turn a section offset into an address that
4714 can be used in an addrmap. */
4715 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
4716 {
4717 CORE_ADDR value = to_underlying (offset);
4718 if (is_dwz)
4719 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
4720 return value;
4721 }
4722
4723 /* A helper function to scan the PC bounds of READER and record them
4724 in the storage's addrmap. */
4725 void check_bounds (cutu_reader *reader);
4726
4727 /* Ensure that the indicated CU exists. The cutu_reader for it is
4728 returned. FOR_SCANNING is true if the caller intends to scan all
4729 the DIEs in the CU; when false, this use is assumed to be to look
4730 up just a single DIE. */
4731 cutu_reader *ensure_cu_exists (cutu_reader *reader,
4732 dwarf2_per_objfile *per_objfile,
4733 sect_offset sect_off,
4734 bool is_dwz,
4735 bool for_scanning);
4736
4737 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
4738 the entry for the enclosing scope (nullptr at top level). FULLY
4739 is true when a full scan must be done -- in some languages,
4740 function scopes must be fully explored in order to find nested
4741 functions. This returns a pointer to just after the spot where
4742 reading stopped. */
4743 const gdb_byte *index_dies (cutu_reader *reader,
4744 const gdb_byte *info_ptr,
4745 const cooked_index_entry *parent_entry,
4746 bool fully);
4747
4748 /* Scan the attributes for a given DIE and update the out
4749 parameters. Returns a pointer to the byte after the DIE. */
4750 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
4751 cutu_reader *reader,
4752 const gdb_byte *watermark_ptr,
4753 const gdb_byte *info_ptr,
4754 const abbrev_info *abbrev,
4755 const char **name,
4756 const char **linkage_name,
4757 cooked_index_flag *flags,
4758 sect_offset *sibling_offset,
4759 const cooked_index_entry **parent_entry,
4760 CORE_ADDR *maybe_defer,
4761 bool for_specification);
4762
4763 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
4764 DW_AT_import, and then scanning the referenced CU. Returns a
4765 pointer to the byte after the DIE. */
4766 const gdb_byte *index_imported_unit (cutu_reader *reader,
4767 const gdb_byte *info_ptr,
4768 const abbrev_info *abbrev);
4769
4770 /* Recursively read DIEs, recording the section offsets in
4771 m_die_range_map and then calling index_dies. */
4772 const gdb_byte *recurse (cutu_reader *reader,
4773 const gdb_byte *info_ptr,
4774 const cooked_index_entry *parent_entry,
4775 bool fully);
4776
4777 /* The storage object, where the results are kept. */
4778 cooked_index_storage *m_index_storage;
4779 /* The CU that we are reading on behalf of. This object might be
4780 asked to index one CU but to treat the results as if they come
4781 from some including CU; in this case the including CU would be
4782 recorded here. */
4783 dwarf2_per_cu_data *m_per_cu;
4784 /* The language that we're assuming when reading. */
4785 enum language m_language;
4786
4787 /* An addrmap that maps from section offsets (see the form_addr
4788 method) to newly-created entries. See m_deferred_entries to
4789 understand this. */
4790 addrmap_mutable m_die_range_map;
4791
4792 /* A single deferred entry. */
4793 struct deferred_entry
4794 {
4795 sect_offset die_offset;
4796 const char *name;
4797 CORE_ADDR spec_offset;
4798 dwarf_tag tag;
4799 cooked_index_flag flags;
4800 };
4801
4802 /* The generated DWARF can sometimes have the declaration for a
4803 method in a class (or perhaps namespace) scope, with the
4804 definition appearing outside this scope... just one of the many
4805 bad things about DWARF. In order to handle this situation, we
4806 defer certain entries until the end of scanning, at which point
4807 we'll know the containing context of all the DIEs that we might
4808 have scanned. This vector stores these deferred entries. */
4809 std::vector<deferred_entry> m_deferred_entries;
4810 };
4811
4812 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4813 Process compilation unit THIS_CU for a psymtab. */
4814
4815 static void
4816 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
4817 dwarf2_per_objfile *per_objfile,
4818 cooked_index_storage *storage)
4819 {
4820 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
4821 storage->get_abbrev_cache ());
4822
4823 if (reader.comp_unit_die == nullptr)
4824 return;
4825
4826 if (reader.dummy_p)
4827 {
4828 /* Nothing. */
4829 }
4830 else if (this_cu->is_debug_types)
4831 build_type_psymtabs_reader (&reader, storage);
4832 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
4833 {
4834 bool nope = false;
4835 if (this_cu->scanned.compare_exchange_strong (nope, true))
4836 {
4837 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
4838 language_minimal);
4839 gdb_assert (storage != nullptr);
4840 cooked_indexer indexer (storage, this_cu, reader.cu->lang ());
4841 indexer.make_index (&reader);
4842 }
4843 }
4844 }
4845
4846 /* Reader function for build_type_psymtabs. */
4847
4848 static void
4849 build_type_psymtabs_reader (cutu_reader *reader,
4850 cooked_index_storage *storage)
4851 {
4852 struct dwarf2_cu *cu = reader->cu;
4853 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4854 struct die_info *type_unit_die = reader->comp_unit_die;
4855
4856 gdb_assert (per_cu->is_debug_types);
4857
4858 if (! type_unit_die->has_children)
4859 return;
4860
4861 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
4862
4863 gdb_assert (storage != nullptr);
4864 cooked_indexer indexer (storage, per_cu, cu->lang ());
4865 indexer.make_index (reader);
4866 }
4867
4868 /* Struct used to sort TUs by their abbreviation table offset. */
4869
4870 struct tu_abbrev_offset
4871 {
4872 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
4873 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
4874 {}
4875
4876 /* This is used when sorting. */
4877 bool operator< (const tu_abbrev_offset &other) const
4878 {
4879 return abbrev_offset < other.abbrev_offset;
4880 }
4881
4882 signatured_type *sig_type;
4883 sect_offset abbrev_offset;
4884 };
4885
4886 /* Efficiently read all the type units.
4887
4888 The efficiency is because we sort TUs by the abbrev table they use and
4889 only read each abbrev table once. In one program there are 200K TUs
4890 sharing 8K abbrev tables.
4891
4892 The main purpose of this function is to support building the
4893 dwarf2_per_objfile->per_bfd->type_unit_groups table.
4894 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
4895 can collapse the search space by grouping them by stmt_list.
4896 The savings can be significant, in the same program from above the 200K TUs
4897 share 8K stmt_list tables.
4898
4899 FUNC is expected to call get_type_unit_group, which will create the
4900 struct type_unit_group if necessary and add it to
4901 dwarf2_per_objfile->per_bfd->type_unit_groups. */
4902
4903 static void
4904 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
4905 cooked_index_storage *storage)
4906 {
4907 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4908 abbrev_table_up abbrev_table;
4909 sect_offset abbrev_offset;
4910
4911 /* It's up to the caller to not call us multiple times. */
4912 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
4913
4914 if (per_objfile->per_bfd->all_type_units.size () == 0)
4915 return;
4916
4917 /* TUs typically share abbrev tables, and there can be way more TUs than
4918 abbrev tables. Sort by abbrev table to reduce the number of times we
4919 read each abbrev table in.
4920 Alternatives are to punt or to maintain a cache of abbrev tables.
4921 This is simpler and efficient enough for now.
4922
4923 Later we group TUs by their DW_AT_stmt_list value (as this defines the
4924 symtab to use). Typically TUs with the same abbrev offset have the same
4925 stmt_list value too so in practice this should work well.
4926
4927 The basic algorithm here is:
4928
4929 sort TUs by abbrev table
4930 for each TU with same abbrev table:
4931 read abbrev table if first user
4932 read TU top level DIE
4933 [IWBN if DWO skeletons had DW_AT_stmt_list]
4934 call FUNC */
4935
4936 dwarf_read_debug_printf ("Building type unit groups ...");
4937
4938 /* Sort in a separate table to maintain the order of all_units
4939 for .gdb_index: TU indices directly index all_type_units. */
4940 std::vector<tu_abbrev_offset> sorted_by_abbrev;
4941 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
4942
4943 for (const auto &cu : per_objfile->per_bfd->all_units)
4944 {
4945 if (cu->is_debug_types)
4946 {
4947 auto sig_type = static_cast<signatured_type *> (cu.get ());
4948 sorted_by_abbrev.emplace_back
4949 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
4950 sig_type->sect_off));
4951 }
4952 }
4953
4954 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
4955
4956 abbrev_offset = (sect_offset) ~(unsigned) 0;
4957
4958 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
4959 {
4960 /* Switch to the next abbrev table if necessary. */
4961 if (abbrev_table == NULL
4962 || tu.abbrev_offset != abbrev_offset)
4963 {
4964 abbrev_offset = tu.abbrev_offset;
4965 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
4966 abbrev_table =
4967 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
4968 ++tu_stats->nr_uniq_abbrev_tables;
4969 }
4970
4971 cutu_reader reader (tu.sig_type, per_objfile,
4972 abbrev_table.get (), nullptr, false);
4973 if (!reader.dummy_p)
4974 build_type_psymtabs_reader (&reader, storage);
4975 }
4976 }
4977
4978 /* Print collected type unit statistics. */
4979
4980 static void
4981 print_tu_stats (dwarf2_per_objfile *per_objfile)
4982 {
4983 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4984
4985 dwarf_read_debug_printf ("Type unit statistics:");
4986 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
4987 dwarf_read_debug_printf (" %d uniq abbrev tables",
4988 tu_stats->nr_uniq_abbrev_tables);
4989 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
4990 tu_stats->nr_symtabs);
4991 dwarf_read_debug_printf (" %d symtab sharers",
4992 tu_stats->nr_symtab_sharers);
4993 dwarf_read_debug_printf (" %d type units without a stmt_list",
4994 tu_stats->nr_stmt_less_type_units);
4995 dwarf_read_debug_printf (" %d all_type_units reallocs",
4996 tu_stats->nr_all_type_units_reallocs);
4997 }
4998
4999 struct skeleton_data
5000 {
5001 dwarf2_per_objfile *per_objfile;
5002 cooked_index_storage *storage;
5003 };
5004
5005 /* Traversal function for process_skeletonless_type_unit.
5006 Read a TU in a DWO file and build partial symbols for it. */
5007
5008 static int
5009 process_skeletonless_type_unit (void **slot, void *info)
5010 {
5011 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
5012 skeleton_data *data = (skeleton_data *) info;
5013
5014 /* If this TU doesn't exist in the global table, add it and read it in. */
5015
5016 if (data->per_objfile->per_bfd->signatured_types == NULL)
5017 data->per_objfile->per_bfd->signatured_types
5018 = allocate_signatured_type_table ();
5019
5020 signatured_type find_entry (dwo_unit->signature);
5021 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
5022 &find_entry, INSERT);
5023 /* If we've already seen this type there's nothing to do. What's happening
5024 is we're doing our own version of comdat-folding here. */
5025 if (*slot != NULL)
5026 return 1;
5027
5028 /* This does the job that create_all_units would have done for
5029 this TU. */
5030 signatured_type *entry
5031 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
5032 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
5033 *slot = entry;
5034
5035 /* This does the job that build_type_psymtabs would have done. */
5036 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
5037 if (!reader.dummy_p)
5038 build_type_psymtabs_reader (&reader, data->storage);
5039
5040 return 1;
5041 }
5042
5043 /* Traversal function for process_skeletonless_type_units. */
5044
5045 static int
5046 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
5047 {
5048 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
5049
5050 if (dwo_file->tus != NULL)
5051 htab_traverse_noresize (dwo_file->tus.get (),
5052 process_skeletonless_type_unit, info);
5053
5054 return 1;
5055 }
5056
5057 /* Scan all TUs of DWO files, verifying we've processed them.
5058 This is needed in case a TU was emitted without its skeleton.
5059 Note: This can't be done until we know what all the DWO files are. */
5060
5061 static void
5062 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
5063 cooked_index_storage *storage)
5064 {
5065 skeleton_data data { per_objfile, storage };
5066
5067 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
5068 if (get_dwp_file (per_objfile) == NULL
5069 && per_objfile->per_bfd->dwo_files != NULL)
5070 {
5071 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
5072 process_dwo_file_for_skeletonless_type_units,
5073 &data);
5074 }
5075 }
5076
5077 /* Build the partial symbol table by doing a quick pass through the
5078 .debug_info and .debug_abbrev sections. */
5079
5080 static void
5081 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
5082 {
5083 struct objfile *objfile = per_objfile->objfile;
5084 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5085
5086 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
5087 objfile_name (objfile));
5088
5089 per_bfd->map_info_sections (objfile);
5090
5091 cooked_index_storage index_storage;
5092 create_all_units (per_objfile);
5093 build_type_psymtabs (per_objfile, &index_storage);
5094 std::vector<std::unique_ptr<cooked_index_shard>> indexes;
5095
5096 per_bfd->quick_file_names_table
5097 = create_quick_file_names_table (per_bfd->all_units.size ());
5098 if (!per_bfd->debug_aranges.empty ())
5099 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
5100 index_storage.get_addrmap ());
5101
5102 {
5103 /* Ensure that complaints are handled correctly. */
5104 complaint_interceptor complaint_handler;
5105
5106 using iter_type = decltype (per_bfd->all_units.begin ());
5107
5108 auto task_size_ = [] (iter_type iter)
5109 {
5110 dwarf2_per_cu_data *per_cu = iter->get ();
5111 return (size_t)per_cu->length ();
5112 };
5113 auto task_size = gdb::make_function_view (task_size_);
5114
5115 /* Each thread returns a pair holding a cooked index, and a vector
5116 of errors that should be printed. The latter is done because
5117 GDB's I/O system is not thread-safe. run_on_main_thread could be
5118 used, but that would mean the messages are printed after the
5119 prompt, which looks weird. */
5120 using result_type = std::pair<std::unique_ptr<cooked_index_shard>,
5121 std::vector<gdb_exception>>;
5122 std::vector<result_type> results
5123 = gdb::parallel_for_each (1, per_bfd->all_units.begin (),
5124 per_bfd->all_units.end (),
5125 [=] (iter_type iter, iter_type end)
5126 {
5127 std::vector<gdb_exception> errors;
5128 cooked_index_storage thread_storage;
5129 for (; iter != end; ++iter)
5130 {
5131 dwarf2_per_cu_data *per_cu = iter->get ();
5132 try
5133 {
5134 process_psymtab_comp_unit (per_cu, per_objfile,
5135 &thread_storage);
5136 }
5137 catch (gdb_exception &except)
5138 {
5139 errors.push_back (std::move (except));
5140 }
5141 }
5142 return result_type (thread_storage.release (), std::move (errors));
5143 }, task_size);
5144
5145 /* Only show a given exception a single time. */
5146 std::unordered_set<gdb_exception> seen_exceptions;
5147 for (auto &one_result : results)
5148 {
5149 indexes.push_back (std::move (one_result.first));
5150 for (auto &one_exc : one_result.second)
5151 if (seen_exceptions.insert (one_exc).second)
5152 exception_print (gdb_stderr, one_exc);
5153 }
5154 }
5155
5156 /* This has to wait until we read the CUs, we need the list of DWOs. */
5157 process_skeletonless_type_units (per_objfile, &index_storage);
5158
5159 if (dwarf_read_debug > 0)
5160 print_tu_stats (per_objfile);
5161
5162 indexes.push_back (index_storage.release ());
5163 indexes.shrink_to_fit ();
5164
5165 cooked_index *vec = new cooked_index (std::move (indexes));
5166 per_bfd->index_table.reset (vec);
5167
5168 const cooked_index_entry *main_entry = vec->get_main ();
5169 if (main_entry != nullptr)
5170 {
5171 /* We only do this for names not requiring canonicalization. At
5172 this point in the process names have not been canonicalized.
5173 However, currently, languages that require this step also do
5174 not use DW_AT_main_subprogram. An assert is appropriate here
5175 because this filtering is done in get_main. */
5176 enum language lang = main_entry->per_cu->lang ();
5177 gdb_assert (!language_requires_canonicalization (lang));
5178 const char *full_name = main_entry->full_name (&per_bfd->obstack, true);
5179 set_objfile_main_name (objfile, full_name, lang);
5180 }
5181
5182 dwarf_read_debug_printf ("Done building psymtabs of %s",
5183 objfile_name (objfile));
5184 }
5185
5186 static void
5187 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
5188 struct dwarf2_section_info *section,
5189 struct dwarf2_section_info *abbrev_section,
5190 unsigned int is_dwz,
5191 htab_up &types_htab,
5192 rcuh_kind section_kind)
5193 {
5194 const gdb_byte *info_ptr;
5195 struct objfile *objfile = per_objfile->objfile;
5196
5197 dwarf_read_debug_printf ("Reading %s for %s",
5198 section->get_name (),
5199 section->get_file_name ());
5200
5201 section->read (objfile);
5202
5203 info_ptr = section->buffer;
5204
5205 while (info_ptr < section->buffer + section->size)
5206 {
5207 dwarf2_per_cu_data_up this_cu;
5208
5209 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
5210
5211 comp_unit_head cu_header;
5212 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
5213 abbrev_section, info_ptr,
5214 section_kind);
5215
5216 /* Save the compilation unit for later lookup. */
5217 if (cu_header.unit_type != DW_UT_type)
5218 this_cu = per_objfile->per_bfd->allocate_per_cu ();
5219 else
5220 {
5221 if (types_htab == nullptr)
5222 types_htab = allocate_signatured_type_table ();
5223
5224 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
5225 (cu_header.signature);
5226 signatured_type *sig_ptr = sig_type.get ();
5227 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
5228 this_cu.reset (sig_type.release ());
5229
5230 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
5231 gdb_assert (slot != nullptr);
5232 if (*slot != nullptr)
5233 complaint (_("debug type entry at offset %s is duplicate to"
5234 " the entry at offset %s, signature %s"),
5235 sect_offset_str (sect_off),
5236 sect_offset_str (sig_ptr->sect_off),
5237 hex_string (sig_ptr->signature));
5238 *slot = sig_ptr;
5239 }
5240 this_cu->sect_off = sect_off;
5241 this_cu->set_length (cu_header.get_length_with_initial ());
5242 this_cu->is_dwz = is_dwz;
5243 this_cu->section = section;
5244 /* Init this asap, to avoid a data race in the set_version in
5245 cutu_reader::cutu_reader (which may be run in parallel for the cooked
5246 index case). */
5247 this_cu->set_version (cu_header.version);
5248
5249 info_ptr = info_ptr + this_cu->length ();
5250 per_objfile->per_bfd->all_units.push_back (std::move (this_cu));
5251 }
5252 }
5253
5254 /* Initialize the views on all_units. */
5255
5256 void
5257 finalize_all_units (dwarf2_per_bfd *per_bfd)
5258 {
5259 size_t nr_tus = per_bfd->tu_stats.nr_tus;
5260 size_t nr_cus = per_bfd->all_units.size () - nr_tus;
5261 gdb::array_view<dwarf2_per_cu_data_up> tmp = per_bfd->all_units;
5262 per_bfd->all_comp_units = tmp.slice (0, nr_cus);
5263 per_bfd->all_type_units = tmp.slice (nr_cus, nr_tus);
5264 }
5265
5266 /* Create a list of all compilation units in OBJFILE.
5267 This is only done for -readnow and building partial symtabs. */
5268
5269 static void
5270 create_all_units (dwarf2_per_objfile *per_objfile)
5271 {
5272 htab_up types_htab;
5273 gdb_assert (per_objfile->per_bfd->all_units.empty ());
5274
5275 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
5276 &per_objfile->per_bfd->abbrev, 0,
5277 types_htab, rcuh_kind::COMPILE);
5278 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
5279 read_comp_units_from_section (per_objfile, &section,
5280 &per_objfile->per_bfd->abbrev, 0,
5281 types_htab, rcuh_kind::TYPE);
5282
5283 dwz_file *dwz;
5284 try
5285 {
5286 dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
5287 }
5288 catch (const gdb_exception_error &)
5289 {
5290 per_objfile->per_bfd->all_units.clear ();
5291 throw;
5292 }
5293 if (dwz != NULL)
5294 {
5295 /* Pre-read the sections we'll need to construct an index. */
5296 struct objfile *objfile = per_objfile->objfile;
5297 dwz->abbrev.read (objfile);
5298 dwz->info.read (objfile);
5299 dwz->str.read (objfile);
5300 dwz->line.read (objfile);
5301 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
5302 types_htab, rcuh_kind::COMPILE);
5303 }
5304
5305 per_objfile->per_bfd->signatured_types = std::move (types_htab);
5306
5307 finalize_all_units (per_objfile->per_bfd);
5308 }
5309
5310 /* Return the initial uleb128 in the die at INFO_PTR. */
5311
5312 static unsigned int
5313 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
5314 {
5315 unsigned int bytes_read;
5316
5317 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5318 }
5319
5320 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
5321 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
5322
5323 Return the corresponding abbrev, or NULL if the number is zero (indicating
5324 an empty DIE). In either case *BYTES_READ will be set to the length of
5325 the initial number. */
5326
5327 static const struct abbrev_info *
5328 peek_die_abbrev (const die_reader_specs &reader,
5329 const gdb_byte *info_ptr, unsigned int *bytes_read)
5330 {
5331 dwarf2_cu *cu = reader.cu;
5332 bfd *abfd = reader.abfd;
5333 unsigned int abbrev_number
5334 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5335
5336 if (abbrev_number == 0)
5337 return NULL;
5338
5339 const abbrev_info *abbrev
5340 = reader.abbrev_table->lookup_abbrev (abbrev_number);
5341 if (!abbrev)
5342 {
5343 error (_("Dwarf Error: Could not find abbrev number %d in %s"
5344 " at offset %s [in module %s]"),
5345 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
5346 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
5347 }
5348
5349 return abbrev;
5350 }
5351
5352 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5353 Returns a pointer to the end of a series of DIEs, terminated by an empty
5354 DIE. Any children of the skipped DIEs will also be skipped. */
5355
5356 static const gdb_byte *
5357 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
5358 {
5359 while (1)
5360 {
5361 unsigned int bytes_read;
5362 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
5363 &bytes_read);
5364
5365 if (abbrev == NULL)
5366 return info_ptr + bytes_read;
5367 else
5368 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5369 }
5370 }
5371
5372 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5373 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5374 abbrev corresponding to that skipped uleb128 should be passed in
5375 ABBREV.
5376
5377 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
5378 returns a pointer to this DIE's sibling, skipping any children.
5379 Otherwise, returns a pointer to the DIE's first child. */
5380
5381 static const gdb_byte *
5382 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
5383 const struct abbrev_info *abbrev, bool do_skip_children)
5384 {
5385 unsigned int bytes_read;
5386 struct attribute attr;
5387 bfd *abfd = reader->abfd;
5388 struct dwarf2_cu *cu = reader->cu;
5389 const gdb_byte *buffer = reader->buffer;
5390 const gdb_byte *buffer_end = reader->buffer_end;
5391 unsigned int form, i;
5392
5393 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
5394 {
5395 /* We only handle DW_FORM_ref4 here. */
5396 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
5397 unsigned int offset = read_4_bytes (abfd, sibling_data);
5398 const gdb_byte *sibling_ptr
5399 = buffer + to_underlying (cu->header.sect_off) + offset;
5400 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
5401 return sibling_ptr;
5402 /* Fall through to the slow way. */
5403 }
5404 else if (abbrev->size_if_constant != 0)
5405 {
5406 info_ptr += abbrev->size_if_constant;
5407 if (do_skip_children && abbrev->has_children)
5408 return skip_children (reader, info_ptr);
5409 return info_ptr;
5410 }
5411
5412 for (i = 0; i < abbrev->num_attrs; i++)
5413 {
5414 /* The only abbrev we care about is DW_AT_sibling. */
5415 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
5416 {
5417 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
5418 if (attr.form == DW_FORM_ref_addr)
5419 complaint (_("ignoring absolute DW_AT_sibling"));
5420 else
5421 {
5422 sect_offset off = attr.get_ref_die_offset ();
5423 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
5424
5425 if (sibling_ptr < info_ptr)
5426 complaint (_("DW_AT_sibling points backwards"));
5427 else if (sibling_ptr > reader->buffer_end)
5428 reader->die_section->overflow_complaint ();
5429 else
5430 return sibling_ptr;
5431 }
5432 }
5433
5434 /* If it isn't DW_AT_sibling, skip this attribute. */
5435 form = abbrev->attrs[i].form;
5436 skip_attribute:
5437 switch (form)
5438 {
5439 case DW_FORM_ref_addr:
5440 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5441 and later it is offset sized. */
5442 if (cu->header.version == 2)
5443 info_ptr += cu->header.addr_size;
5444 else
5445 info_ptr += cu->header.offset_size;
5446 break;
5447 case DW_FORM_GNU_ref_alt:
5448 info_ptr += cu->header.offset_size;
5449 break;
5450 case DW_FORM_addr:
5451 info_ptr += cu->header.addr_size;
5452 break;
5453 case DW_FORM_data1:
5454 case DW_FORM_ref1:
5455 case DW_FORM_flag:
5456 case DW_FORM_strx1:
5457 info_ptr += 1;
5458 break;
5459 case DW_FORM_flag_present:
5460 case DW_FORM_implicit_const:
5461 break;
5462 case DW_FORM_data2:
5463 case DW_FORM_ref2:
5464 case DW_FORM_strx2:
5465 info_ptr += 2;
5466 break;
5467 case DW_FORM_strx3:
5468 info_ptr += 3;
5469 break;
5470 case DW_FORM_data4:
5471 case DW_FORM_ref4:
5472 case DW_FORM_strx4:
5473 info_ptr += 4;
5474 break;
5475 case DW_FORM_data8:
5476 case DW_FORM_ref8:
5477 case DW_FORM_ref_sig8:
5478 info_ptr += 8;
5479 break;
5480 case DW_FORM_data16:
5481 info_ptr += 16;
5482 break;
5483 case DW_FORM_string:
5484 read_direct_string (abfd, info_ptr, &bytes_read);
5485 info_ptr += bytes_read;
5486 break;
5487 case DW_FORM_sec_offset:
5488 case DW_FORM_strp:
5489 case DW_FORM_GNU_strp_alt:
5490 info_ptr += cu->header.offset_size;
5491 break;
5492 case DW_FORM_exprloc:
5493 case DW_FORM_block:
5494 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5495 info_ptr += bytes_read;
5496 break;
5497 case DW_FORM_block1:
5498 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5499 break;
5500 case DW_FORM_block2:
5501 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5502 break;
5503 case DW_FORM_block4:
5504 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5505 break;
5506 case DW_FORM_addrx:
5507 case DW_FORM_strx:
5508 case DW_FORM_sdata:
5509 case DW_FORM_udata:
5510 case DW_FORM_ref_udata:
5511 case DW_FORM_GNU_addr_index:
5512 case DW_FORM_GNU_str_index:
5513 case DW_FORM_rnglistx:
5514 case DW_FORM_loclistx:
5515 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
5516 break;
5517 case DW_FORM_indirect:
5518 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5519 info_ptr += bytes_read;
5520 /* We need to continue parsing from here, so just go back to
5521 the top. */
5522 goto skip_attribute;
5523
5524 default:
5525 error (_("Dwarf Error: Cannot handle %s "
5526 "in DWARF reader [in module %s]"),
5527 dwarf_form_name (form),
5528 bfd_get_filename (abfd));
5529 }
5530 }
5531
5532 if (do_skip_children && abbrev->has_children)
5533 return skip_children (reader, info_ptr);
5534 else
5535 return info_ptr;
5536 }
5537 \f
5538 /* Reading in full CUs. */
5539
5540 /* Add PER_CU to the queue. */
5541
5542 static void
5543 queue_comp_unit (dwarf2_per_cu_data *per_cu,
5544 dwarf2_per_objfile *per_objfile,
5545 enum language pretend_language)
5546 {
5547 per_cu->queued = 1;
5548
5549 gdb_assert (per_objfile->queue.has_value ());
5550 per_objfile->queue->emplace (per_cu, per_objfile, pretend_language);
5551 }
5552
5553 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
5554
5555 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
5556 dependency.
5557
5558 Return true if maybe_queue_comp_unit requires the caller to load the CU's
5559 DIEs, false otherwise.
5560
5561 Explanation: there is an invariant that if a CU is queued for expansion
5562 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
5563 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
5564 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
5565 are not yet loaded, the the caller must load the CU's DIEs to ensure the
5566 invariant is respected.
5567
5568 The caller is therefore not required to load the CU's DIEs (we return false)
5569 if:
5570
5571 - the CU is already expanded, and therefore does not get enqueued
5572 - the CU gets enqueued for expansion, but its DIEs are already loaded
5573
5574 Note that the caller should not use this function's return value as an
5575 indicator of whether the CU's DIEs are loaded right now, it should check
5576 that by calling `dwarf2_per_objfile::get_cu` instead. */
5577
5578 static int
5579 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
5580 dwarf2_per_cu_data *per_cu,
5581 dwarf2_per_objfile *per_objfile,
5582 enum language pretend_language)
5583 {
5584 /* Mark the dependence relation so that we don't flush PER_CU
5585 too early. */
5586 if (dependent_cu != NULL)
5587 dependent_cu->add_dependence (per_cu);
5588
5589 /* If it's already on the queue, we have nothing to do. */
5590 if (per_cu->queued)
5591 {
5592 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
5593 loaded. */
5594 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
5595
5596 /* If the CU is queued for expansion, it should not already be
5597 expanded. */
5598 gdb_assert (!per_objfile->symtab_set_p (per_cu));
5599
5600 /* The DIEs are already loaded, the caller doesn't need to do it. */
5601 return 0;
5602 }
5603
5604 bool queued = false;
5605 if (!per_objfile->symtab_set_p (per_cu))
5606 {
5607 /* Add it to the queue. */
5608 queue_comp_unit (per_cu, per_objfile, pretend_language);
5609 queued = true;
5610 }
5611
5612 /* If the compilation unit is already loaded, just mark it as
5613 used. */
5614 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
5615 if (cu != nullptr)
5616 cu->last_used = 0;
5617
5618 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
5619 and the DIEs are not already loaded. */
5620 return queued && cu == nullptr;
5621 }
5622
5623 /* Process the queue. */
5624
5625 static void
5626 process_queue (dwarf2_per_objfile *per_objfile)
5627 {
5628 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
5629 objfile_name (per_objfile->objfile));
5630
5631 /* The queue starts out with one item, but following a DIE reference
5632 may load a new CU, adding it to the end of the queue. */
5633 while (!per_objfile->queue->empty ())
5634 {
5635 dwarf2_queue_item &item = per_objfile->queue->front ();
5636 dwarf2_per_cu_data *per_cu = item.per_cu;
5637
5638 if (!per_objfile->symtab_set_p (per_cu))
5639 {
5640 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
5641
5642 /* Skip dummy CUs. */
5643 if (cu != nullptr)
5644 {
5645 unsigned int debug_print_threshold;
5646 char buf[100];
5647
5648 if (per_cu->is_debug_types)
5649 {
5650 struct signatured_type *sig_type =
5651 (struct signatured_type *) per_cu;
5652
5653 sprintf (buf, "TU %s at offset %s",
5654 hex_string (sig_type->signature),
5655 sect_offset_str (per_cu->sect_off));
5656 /* There can be 100s of TUs.
5657 Only print them in verbose mode. */
5658 debug_print_threshold = 2;
5659 }
5660 else
5661 {
5662 sprintf (buf, "CU at offset %s",
5663 sect_offset_str (per_cu->sect_off));
5664 debug_print_threshold = 1;
5665 }
5666
5667 if (dwarf_read_debug >= debug_print_threshold)
5668 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
5669
5670 if (per_cu->is_debug_types)
5671 process_full_type_unit (cu, item.pretend_language);
5672 else
5673 process_full_comp_unit (cu, item.pretend_language);
5674
5675 if (dwarf_read_debug >= debug_print_threshold)
5676 dwarf_read_debug_printf ("Done expanding %s", buf);
5677 }
5678 }
5679
5680 per_cu->queued = 0;
5681 per_objfile->queue->pop ();
5682 }
5683
5684 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
5685 objfile_name (per_objfile->objfile));
5686 }
5687
5688 /* Load the DIEs associated with PER_CU into memory.
5689
5690 In some cases, the caller, while reading partial symbols, will need to load
5691 the full symbols for the CU for some reason. It will already have a
5692 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
5693 rather than creating a new one. */
5694
5695 static void
5696 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
5697 dwarf2_per_objfile *per_objfile,
5698 dwarf2_cu *existing_cu,
5699 bool skip_partial,
5700 enum language pretend_language)
5701 {
5702 gdb_assert (! this_cu->is_debug_types);
5703
5704 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
5705 if (reader.dummy_p)
5706 return;
5707
5708 struct dwarf2_cu *cu = reader.cu;
5709 const gdb_byte *info_ptr = reader.info_ptr;
5710
5711 gdb_assert (cu->die_hash == NULL);
5712 cu->die_hash =
5713 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
5714 die_info::hash,
5715 die_info::eq,
5716 NULL,
5717 &cu->comp_unit_obstack,
5718 hashtab_obstack_allocate,
5719 dummy_obstack_deallocate);
5720
5721 if (reader.comp_unit_die->has_children)
5722 reader.comp_unit_die->child
5723 = read_die_and_siblings (&reader, reader.info_ptr,
5724 &info_ptr, reader.comp_unit_die);
5725 cu->dies = reader.comp_unit_die;
5726 /* comp_unit_die is not stored in die_hash, no need. */
5727
5728 /* We try not to read any attributes in this function, because not
5729 all CUs needed for references have been loaded yet, and symbol
5730 table processing isn't initialized. But we have to set the CU language,
5731 or we won't be able to build types correctly.
5732 Similarly, if we do not read the producer, we can not apply
5733 producer-specific interpretation. */
5734 prepare_one_comp_unit (cu, cu->dies, pretend_language);
5735
5736 reader.keep ();
5737 }
5738
5739 /* Add a DIE to the delayed physname list. */
5740
5741 static void
5742 add_to_method_list (struct type *type, int fnfield_index, int index,
5743 const char *name, struct die_info *die,
5744 struct dwarf2_cu *cu)
5745 {
5746 struct delayed_method_info mi;
5747 mi.type = type;
5748 mi.fnfield_index = fnfield_index;
5749 mi.index = index;
5750 mi.name = name;
5751 mi.die = die;
5752 cu->method_list.push_back (mi);
5753 }
5754
5755 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
5756 "const" / "volatile". If so, decrements LEN by the length of the
5757 modifier and return true. Otherwise return false. */
5758
5759 template<size_t N>
5760 static bool
5761 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
5762 {
5763 size_t mod_len = sizeof (mod) - 1;
5764 if (len > mod_len && startswith (physname + (len - mod_len), mod))
5765 {
5766 len -= mod_len;
5767 return true;
5768 }
5769 return false;
5770 }
5771
5772 /* Compute the physnames of any methods on the CU's method list.
5773
5774 The computation of method physnames is delayed in order to avoid the
5775 (bad) condition that one of the method's formal parameters is of an as yet
5776 incomplete type. */
5777
5778 static void
5779 compute_delayed_physnames (struct dwarf2_cu *cu)
5780 {
5781 /* Only C++ delays computing physnames. */
5782 if (cu->method_list.empty ())
5783 return;
5784 gdb_assert (cu->lang () == language_cplus);
5785
5786 for (const delayed_method_info &mi : cu->method_list)
5787 {
5788 const char *physname;
5789 struct fn_fieldlist *fn_flp
5790 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
5791 physname = dwarf2_physname (mi.name, mi.die, cu);
5792 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
5793 = physname ? physname : "";
5794
5795 /* Since there's no tag to indicate whether a method is a
5796 const/volatile overload, extract that information out of the
5797 demangled name. */
5798 if (physname != NULL)
5799 {
5800 size_t len = strlen (physname);
5801
5802 while (1)
5803 {
5804 if (physname[len] == ')') /* shortcut */
5805 break;
5806 else if (check_modifier (physname, len, " const"))
5807 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
5808 else if (check_modifier (physname, len, " volatile"))
5809 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
5810 else
5811 break;
5812 }
5813 }
5814 }
5815
5816 /* The list is no longer needed. */
5817 cu->method_list.clear ();
5818 }
5819
5820 /* Go objects should be embedded in a DW_TAG_module DIE,
5821 and it's not clear if/how imported objects will appear.
5822 To keep Go support simple until that's worked out,
5823 go back through what we've read and create something usable.
5824 We could do this while processing each DIE, and feels kinda cleaner,
5825 but that way is more invasive.
5826 This is to, for example, allow the user to type "p var" or "b main"
5827 without having to specify the package name, and allow lookups
5828 of module.object to work in contexts that use the expression
5829 parser. */
5830
5831 static void
5832 fixup_go_packaging (struct dwarf2_cu *cu)
5833 {
5834 gdb::unique_xmalloc_ptr<char> package_name;
5835 struct pending *list;
5836 int i;
5837
5838 for (list = *cu->get_builder ()->get_global_symbols ();
5839 list != NULL;
5840 list = list->next)
5841 {
5842 for (i = 0; i < list->nsyms; ++i)
5843 {
5844 struct symbol *sym = list->symbol[i];
5845
5846 if (sym->language () == language_go
5847 && sym->aclass () == LOC_BLOCK)
5848 {
5849 gdb::unique_xmalloc_ptr<char> this_package_name
5850 = go_symbol_package_name (sym);
5851
5852 if (this_package_name == NULL)
5853 continue;
5854 if (package_name == NULL)
5855 package_name = std::move (this_package_name);
5856 else
5857 {
5858 struct objfile *objfile = cu->per_objfile->objfile;
5859 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
5860 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
5861 (sym->symtab () != NULL
5862 ? symtab_to_filename_for_display
5863 (sym->symtab ())
5864 : objfile_name (objfile)),
5865 this_package_name.get (), package_name.get ());
5866 }
5867 }
5868 }
5869 }
5870
5871 if (package_name != NULL)
5872 {
5873 struct objfile *objfile = cu->per_objfile->objfile;
5874 const char *saved_package_name = objfile->intern (package_name.get ());
5875 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
5876 saved_package_name);
5877 struct symbol *sym;
5878
5879 sym = new (&objfile->objfile_obstack) symbol;
5880 sym->set_language (language_go, &objfile->objfile_obstack);
5881 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
5882 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5883 e.g., "main" finds the "main" module and not C's main(). */
5884 sym->set_domain (STRUCT_DOMAIN);
5885 sym->set_aclass_index (LOC_TYPEDEF);
5886 sym->set_type (type);
5887
5888 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
5889 }
5890 }
5891
5892 /* Allocate a fully-qualified name consisting of the two parts on the
5893 obstack. */
5894
5895 static const char *
5896 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
5897 {
5898 return obconcat (obstack, p1, "::", p2, (char *) NULL);
5899 }
5900
5901 /* A helper that allocates a variant part to attach to a Rust enum
5902 type. OBSTACK is where the results should be allocated. TYPE is
5903 the type we're processing. DISCRIMINANT_INDEX is the index of the
5904 discriminant. It must be the index of one of the fields of TYPE,
5905 or -1 to mean there is no discriminant (univariant enum).
5906 DEFAULT_INDEX is the index of the default field; or -1 if there is
5907 no default. RANGES is indexed by "effective" field number (the
5908 field index, but omitting the discriminant and default fields) and
5909 must hold the discriminant values used by the variants. Note that
5910 RANGES must have a lifetime at least as long as OBSTACK -- either
5911 already allocated on it, or static. */
5912
5913 static void
5914 alloc_rust_variant (struct obstack *obstack, struct type *type,
5915 int discriminant_index, int default_index,
5916 gdb::array_view<discriminant_range> ranges)
5917 {
5918 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
5919 gdb_assert (discriminant_index == -1
5920 || (discriminant_index >= 0
5921 && discriminant_index < type->num_fields ()));
5922 gdb_assert (default_index == -1
5923 || (default_index >= 0 && default_index < type->num_fields ()));
5924
5925 /* We have one variant for each non-discriminant field. */
5926 int n_variants = type->num_fields ();
5927 if (discriminant_index != -1)
5928 --n_variants;
5929
5930 variant *variants = new (obstack) variant[n_variants];
5931 int var_idx = 0;
5932 int range_idx = 0;
5933 for (int i = 0; i < type->num_fields (); ++i)
5934 {
5935 if (i == discriminant_index)
5936 continue;
5937
5938 variants[var_idx].first_field = i;
5939 variants[var_idx].last_field = i + 1;
5940
5941 /* The default field does not need a range, but other fields do.
5942 We skipped the discriminant above. */
5943 if (i != default_index)
5944 {
5945 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
5946 ++range_idx;
5947 }
5948
5949 ++var_idx;
5950 }
5951
5952 gdb_assert (range_idx == ranges.size ());
5953 gdb_assert (var_idx == n_variants);
5954
5955 variant_part *part = new (obstack) variant_part;
5956 part->discriminant_index = discriminant_index;
5957 /* If there is no discriminant, then whether it is signed is of no
5958 consequence. */
5959 part->is_unsigned
5960 = (discriminant_index == -1
5961 ? false
5962 : type->field (discriminant_index).type ()->is_unsigned ());
5963 part->variants = gdb::array_view<variant> (variants, n_variants);
5964
5965 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
5966 gdb::array_view<variant_part> *prop_value
5967 = new (storage) gdb::array_view<variant_part> (part, 1);
5968
5969 struct dynamic_prop prop;
5970 prop.set_variant_parts (prop_value);
5971
5972 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
5973 }
5974
5975 /* Some versions of rustc emitted enums in an unusual way.
5976
5977 Ordinary enums were emitted as unions. The first element of each
5978 structure in the union was named "RUST$ENUM$DISR". This element
5979 held the discriminant.
5980
5981 These versions of Rust also implemented the "non-zero"
5982 optimization. When the enum had two values, and one is empty and
5983 the other holds a pointer that cannot be zero, the pointer is used
5984 as the discriminant, with a zero value meaning the empty variant.
5985 Here, the union's first member is of the form
5986 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
5987 where the fieldnos are the indices of the fields that should be
5988 traversed in order to find the field (which may be several fields deep)
5989 and the variantname is the name of the variant of the case when the
5990 field is zero.
5991
5992 This function recognizes whether TYPE is of one of these forms,
5993 and, if so, smashes it to be a variant type. */
5994
5995 static void
5996 quirk_rust_enum (struct type *type, struct objfile *objfile)
5997 {
5998 gdb_assert (type->code () == TYPE_CODE_UNION);
5999
6000 /* We don't need to deal with empty enums. */
6001 if (type->num_fields () == 0)
6002 return;
6003
6004 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
6005 if (type->num_fields () == 1
6006 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
6007 {
6008 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
6009
6010 /* Decode the field name to find the offset of the
6011 discriminant. */
6012 ULONGEST bit_offset = 0;
6013 struct type *field_type = type->field (0).type ();
6014 while (name[0] >= '0' && name[0] <= '9')
6015 {
6016 char *tail;
6017 unsigned long index = strtoul (name, &tail, 10);
6018 name = tail;
6019 if (*name != '$'
6020 || index >= field_type->num_fields ()
6021 || (field_type->field (index).loc_kind ()
6022 != FIELD_LOC_KIND_BITPOS))
6023 {
6024 complaint (_("Could not parse Rust enum encoding string \"%s\""
6025 "[in module %s]"),
6026 type->field (0).name (),
6027 objfile_name (objfile));
6028 return;
6029 }
6030 ++name;
6031
6032 bit_offset += field_type->field (index).loc_bitpos ();
6033 field_type = field_type->field (index).type ();
6034 }
6035
6036 /* Smash this type to be a structure type. We have to do this
6037 because the type has already been recorded. */
6038 type->set_code (TYPE_CODE_STRUCT);
6039 type->set_num_fields (3);
6040 /* Save the field we care about. */
6041 struct field saved_field = type->field (0);
6042 type->set_fields
6043 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
6044
6045 /* Put the discriminant at index 0. */
6046 type->field (0).set_type (field_type);
6047 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
6048 type->field (0).set_name ("<<discriminant>>");
6049 type->field (0).set_loc_bitpos (bit_offset);
6050
6051 /* The order of fields doesn't really matter, so put the real
6052 field at index 1 and the data-less field at index 2. */
6053 type->field (1) = saved_field;
6054 type->field (1).set_name
6055 (rust_last_path_segment (type->field (1).type ()->name ()));
6056 type->field (1).type ()->set_name
6057 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
6058 type->field (1).name ()));
6059
6060 const char *dataless_name
6061 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
6062 name);
6063 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
6064 dataless_name);
6065 type->field (2).set_type (dataless_type);
6066 /* NAME points into the original discriminant name, which
6067 already has the correct lifetime. */
6068 type->field (2).set_name (name);
6069 type->field (2).set_loc_bitpos (0);
6070
6071 /* Indicate that this is a variant type. */
6072 static discriminant_range ranges[1] = { { 0, 0 } };
6073 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
6074 }
6075 /* A union with a single anonymous field is probably an old-style
6076 univariant enum. */
6077 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
6078 {
6079 /* Smash this type to be a structure type. We have to do this
6080 because the type has already been recorded. */
6081 type->set_code (TYPE_CODE_STRUCT);
6082
6083 struct type *field_type = type->field (0).type ();
6084 const char *variant_name
6085 = rust_last_path_segment (field_type->name ());
6086 type->field (0).set_name (variant_name);
6087 field_type->set_name
6088 (rust_fully_qualify (&objfile->objfile_obstack,
6089 type->name (), variant_name));
6090
6091 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
6092 }
6093 else
6094 {
6095 struct type *disr_type = nullptr;
6096 for (int i = 0; i < type->num_fields (); ++i)
6097 {
6098 disr_type = type->field (i).type ();
6099
6100 if (disr_type->code () != TYPE_CODE_STRUCT)
6101 {
6102 /* All fields of a true enum will be structs. */
6103 return;
6104 }
6105 else if (disr_type->num_fields () == 0)
6106 {
6107 /* Could be data-less variant, so keep going. */
6108 disr_type = nullptr;
6109 }
6110 else if (strcmp (disr_type->field (0).name (),
6111 "RUST$ENUM$DISR") != 0)
6112 {
6113 /* Not a Rust enum. */
6114 return;
6115 }
6116 else
6117 {
6118 /* Found one. */
6119 break;
6120 }
6121 }
6122
6123 /* If we got here without a discriminant, then it's probably
6124 just a union. */
6125 if (disr_type == nullptr)
6126 return;
6127
6128 /* Smash this type to be a structure type. We have to do this
6129 because the type has already been recorded. */
6130 type->set_code (TYPE_CODE_STRUCT);
6131
6132 /* Make space for the discriminant field. */
6133 struct field *disr_field = &disr_type->field (0);
6134 field *new_fields
6135 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
6136 * sizeof (struct field)));
6137 memcpy (new_fields + 1, type->fields (),
6138 type->num_fields () * sizeof (struct field));
6139 type->set_fields (new_fields);
6140 type->set_num_fields (type->num_fields () + 1);
6141
6142 /* Install the discriminant at index 0 in the union. */
6143 type->field (0) = *disr_field;
6144 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
6145 type->field (0).set_name ("<<discriminant>>");
6146
6147 /* We need a way to find the correct discriminant given a
6148 variant name. For convenience we build a map here. */
6149 struct type *enum_type = disr_field->type ();
6150 std::unordered_map<std::string, ULONGEST> discriminant_map;
6151 for (int i = 0; i < enum_type->num_fields (); ++i)
6152 {
6153 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
6154 {
6155 const char *name
6156 = rust_last_path_segment (enum_type->field (i).name ());
6157 discriminant_map[name] = enum_type->field (i).loc_enumval ();
6158 }
6159 }
6160
6161 int n_fields = type->num_fields ();
6162 /* We don't need a range entry for the discriminant, but we do
6163 need one for every other field, as there is no default
6164 variant. */
6165 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
6166 discriminant_range,
6167 n_fields - 1);
6168 /* Skip the discriminant here. */
6169 for (int i = 1; i < n_fields; ++i)
6170 {
6171 /* Find the final word in the name of this variant's type.
6172 That name can be used to look up the correct
6173 discriminant. */
6174 const char *variant_name
6175 = rust_last_path_segment (type->field (i).type ()->name ());
6176
6177 auto iter = discriminant_map.find (variant_name);
6178 if (iter != discriminant_map.end ())
6179 {
6180 ranges[i - 1].low = iter->second;
6181 ranges[i - 1].high = iter->second;
6182 }
6183
6184 /* In Rust, each element should have the size of the
6185 enclosing enum. */
6186 type->field (i).type ()->set_length (type->length ());
6187
6188 /* Remove the discriminant field, if it exists. */
6189 struct type *sub_type = type->field (i).type ();
6190 if (sub_type->num_fields () > 0)
6191 {
6192 sub_type->set_num_fields (sub_type->num_fields () - 1);
6193 sub_type->set_fields (sub_type->fields () + 1);
6194 }
6195 type->field (i).set_name (variant_name);
6196 sub_type->set_name
6197 (rust_fully_qualify (&objfile->objfile_obstack,
6198 type->name (), variant_name));
6199 }
6200
6201 /* Indicate that this is a variant type. */
6202 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
6203 gdb::array_view<discriminant_range> (ranges,
6204 n_fields - 1));
6205 }
6206 }
6207
6208 /* Rewrite some Rust unions to be structures with variants parts. */
6209
6210 static void
6211 rust_union_quirks (struct dwarf2_cu *cu)
6212 {
6213 gdb_assert (cu->lang () == language_rust);
6214 for (type *type_ : cu->rust_unions)
6215 quirk_rust_enum (type_, cu->per_objfile->objfile);
6216 /* We don't need this any more. */
6217 cu->rust_unions.clear ();
6218 }
6219
6220 /* See read.h. */
6221
6222 type_unit_group_unshareable *
6223 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
6224 {
6225 auto iter = this->m_type_units.find (tu_group);
6226 if (iter != this->m_type_units.end ())
6227 return iter->second.get ();
6228
6229 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
6230 type_unit_group_unshareable *result = uniq.get ();
6231 this->m_type_units[tu_group] = std::move (uniq);
6232 return result;
6233 }
6234
6235 struct type *
6236 dwarf2_per_objfile::get_type_for_signatured_type
6237 (signatured_type *sig_type) const
6238 {
6239 auto iter = this->m_type_map.find (sig_type);
6240 if (iter == this->m_type_map.end ())
6241 return nullptr;
6242
6243 return iter->second;
6244 }
6245
6246 void dwarf2_per_objfile::set_type_for_signatured_type
6247 (signatured_type *sig_type, struct type *type)
6248 {
6249 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
6250
6251 this->m_type_map[sig_type] = type;
6252 }
6253
6254 /* A helper function for computing the list of all symbol tables
6255 included by PER_CU. */
6256
6257 static void
6258 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
6259 htab_t all_children, htab_t all_type_symtabs,
6260 dwarf2_per_cu_data *per_cu,
6261 dwarf2_per_objfile *per_objfile,
6262 struct compunit_symtab *immediate_parent)
6263 {
6264 void **slot = htab_find_slot (all_children, per_cu, INSERT);
6265 if (*slot != NULL)
6266 {
6267 /* This inclusion and its children have been processed. */
6268 return;
6269 }
6270
6271 *slot = per_cu;
6272
6273 /* Only add a CU if it has a symbol table. */
6274 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6275 if (cust != NULL)
6276 {
6277 /* If this is a type unit only add its symbol table if we haven't
6278 seen it yet (type unit per_cu's can share symtabs). */
6279 if (per_cu->is_debug_types)
6280 {
6281 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
6282 if (*slot == NULL)
6283 {
6284 *slot = cust;
6285 result->push_back (cust);
6286 if (cust->user == NULL)
6287 cust->user = immediate_parent;
6288 }
6289 }
6290 else
6291 {
6292 result->push_back (cust);
6293 if (cust->user == NULL)
6294 cust->user = immediate_parent;
6295 }
6296 }
6297
6298 if (!per_cu->imported_symtabs_empty ())
6299 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
6300 {
6301 recursively_compute_inclusions (result, all_children,
6302 all_type_symtabs, ptr, per_objfile,
6303 cust);
6304 }
6305 }
6306
6307 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
6308 PER_CU. */
6309
6310 static void
6311 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
6312 dwarf2_per_objfile *per_objfile)
6313 {
6314 gdb_assert (! per_cu->is_debug_types);
6315
6316 if (!per_cu->imported_symtabs_empty ())
6317 {
6318 int len;
6319 std::vector<compunit_symtab *> result_symtabs;
6320 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6321
6322 /* If we don't have a symtab, we can just skip this case. */
6323 if (cust == NULL)
6324 return;
6325
6326 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
6327 htab_eq_pointer,
6328 NULL, xcalloc, xfree));
6329 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
6330 htab_eq_pointer,
6331 NULL, xcalloc, xfree));
6332
6333 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
6334 {
6335 recursively_compute_inclusions (&result_symtabs, all_children.get (),
6336 all_type_symtabs.get (), ptr,
6337 per_objfile, cust);
6338 }
6339
6340 /* Now we have a transitive closure of all the included symtabs. */
6341 len = result_symtabs.size ();
6342 cust->includes
6343 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
6344 struct compunit_symtab *, len + 1);
6345 memcpy (cust->includes, result_symtabs.data (),
6346 len * sizeof (compunit_symtab *));
6347 cust->includes[len] = NULL;
6348 }
6349 }
6350
6351 /* Compute the 'includes' field for the symtabs of all the CUs we just
6352 read. */
6353
6354 static void
6355 process_cu_includes (dwarf2_per_objfile *per_objfile)
6356 {
6357 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
6358 {
6359 if (! iter->is_debug_types)
6360 compute_compunit_symtab_includes (iter, per_objfile);
6361 }
6362
6363 per_objfile->per_bfd->just_read_cus.clear ();
6364 }
6365
6366 /* Generate full symbol information for CU, whose DIEs have
6367 already been loaded into memory. */
6368
6369 static void
6370 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
6371 {
6372 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6373 struct objfile *objfile = per_objfile->objfile;
6374 struct gdbarch *gdbarch = objfile->arch ();
6375 CORE_ADDR lowpc, highpc;
6376 struct compunit_symtab *cust;
6377 CORE_ADDR baseaddr;
6378 struct block *static_block;
6379 CORE_ADDR addr;
6380
6381 baseaddr = objfile->text_section_offset ();
6382
6383 /* Clear the list here in case something was left over. */
6384 cu->method_list.clear ();
6385
6386 dwarf2_find_base_address (cu->dies, cu);
6387
6388 /* Before we start reading the top-level DIE, ensure it has a valid tag
6389 type. */
6390 switch (cu->dies->tag)
6391 {
6392 case DW_TAG_compile_unit:
6393 case DW_TAG_partial_unit:
6394 case DW_TAG_type_unit:
6395 break;
6396 default:
6397 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6398 dwarf_tag_name (cu->dies->tag),
6399 sect_offset_str (cu->per_cu->sect_off),
6400 objfile_name (per_objfile->objfile));
6401 }
6402
6403 /* Do line number decoding in read_file_scope () */
6404 process_die (cu->dies, cu);
6405
6406 /* For now fudge the Go package. */
6407 if (cu->lang () == language_go)
6408 fixup_go_packaging (cu);
6409
6410 /* Now that we have processed all the DIEs in the CU, all the types
6411 should be complete, and it should now be safe to compute all of the
6412 physnames. */
6413 compute_delayed_physnames (cu);
6414
6415 if (cu->lang () == language_rust)
6416 rust_union_quirks (cu);
6417
6418 /* Some compilers don't define a DW_AT_high_pc attribute for the
6419 compilation unit. If the DW_AT_high_pc is missing, synthesize
6420 it, by scanning the DIE's below the compilation unit. */
6421 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6422
6423 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
6424 static_block
6425 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
6426
6427 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6428 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6429 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6430 addrmap to help ensure it has an accurate map of pc values belonging to
6431 this comp unit. */
6432 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6433
6434 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
6435 (static_block, 0);
6436
6437 if (cust != NULL)
6438 {
6439 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6440
6441 /* Set symtab language to language from DW_AT_language. If the
6442 compilation is from a C file generated by language preprocessors, do
6443 not set the language if it was already deduced by start_subfile. */
6444 if (!(cu->lang () == language_c
6445 && cust->primary_filetab ()->language () != language_unknown))
6446 cust->primary_filetab ()->set_language (cu->lang ());
6447
6448 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6449 produce DW_AT_location with location lists but it can be possibly
6450 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6451 there were bugs in prologue debug info, fixed later in GCC-4.5
6452 by "unwind info for epilogues" patch (which is not directly related).
6453
6454 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6455 needed, it would be wrong due to missing DW_AT_producer there.
6456
6457 Still one can confuse GDB by using non-standard GCC compilation
6458 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6459 */
6460 if (cu->has_loclist && gcc_4_minor >= 5)
6461 cust->set_locations_valid (true);
6462
6463 if (gcc_4_minor >= 5)
6464 cust->set_epilogue_unwind_valid (true);
6465
6466 cust->set_call_site_htab (cu->call_site_htab);
6467 }
6468
6469 per_objfile->set_symtab (cu->per_cu, cust);
6470
6471 /* Push it for inclusion processing later. */
6472 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
6473
6474 /* Not needed any more. */
6475 cu->reset_builder ();
6476 }
6477
6478 /* Generate full symbol information for type unit CU, whose DIEs have
6479 already been loaded into memory. */
6480
6481 static void
6482 process_full_type_unit (dwarf2_cu *cu,
6483 enum language pretend_language)
6484 {
6485 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6486 struct compunit_symtab *cust;
6487 struct signatured_type *sig_type;
6488
6489 gdb_assert (cu->per_cu->is_debug_types);
6490 sig_type = (struct signatured_type *) cu->per_cu;
6491
6492 /* Clear the list here in case something was left over. */
6493 cu->method_list.clear ();
6494
6495 /* The symbol tables are set up in read_type_unit_scope. */
6496 process_die (cu->dies, cu);
6497
6498 /* For now fudge the Go package. */
6499 if (cu->lang () == language_go)
6500 fixup_go_packaging (cu);
6501
6502 /* Now that we have processed all the DIEs in the CU, all the types
6503 should be complete, and it should now be safe to compute all of the
6504 physnames. */
6505 compute_delayed_physnames (cu);
6506
6507 if (cu->lang () == language_rust)
6508 rust_union_quirks (cu);
6509
6510 /* TUs share symbol tables.
6511 If this is the first TU to use this symtab, complete the construction
6512 of it with end_expandable_symtab. Otherwise, complete the addition of
6513 this TU's symbols to the existing symtab. */
6514 type_unit_group_unshareable *tug_unshare =
6515 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
6516 if (tug_unshare->compunit_symtab == NULL)
6517 {
6518 buildsym_compunit *builder = cu->get_builder ();
6519 cust = builder->end_expandable_symtab (0);
6520 tug_unshare->compunit_symtab = cust;
6521
6522 if (cust != NULL)
6523 {
6524 /* Set symtab language to language from DW_AT_language. If the
6525 compilation is from a C file generated by language preprocessors,
6526 do not set the language if it was already deduced by
6527 start_subfile. */
6528 if (!(cu->lang () == language_c
6529 && cust->primary_filetab ()->language () != language_c))
6530 cust->primary_filetab ()->set_language (cu->lang ());
6531 }
6532 }
6533 else
6534 {
6535 cu->get_builder ()->augment_type_symtab ();
6536 cust = tug_unshare->compunit_symtab;
6537 }
6538
6539 per_objfile->set_symtab (cu->per_cu, cust);
6540
6541 /* Not needed any more. */
6542 cu->reset_builder ();
6543 }
6544
6545 /* Process an imported unit DIE. */
6546
6547 static void
6548 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6549 {
6550 struct attribute *attr;
6551
6552 /* For now we don't handle imported units in type units. */
6553 if (cu->per_cu->is_debug_types)
6554 {
6555 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6556 " supported in type units [in module %s]"),
6557 objfile_name (cu->per_objfile->objfile));
6558 }
6559
6560 attr = dwarf2_attr (die, DW_AT_import, cu);
6561 if (attr != NULL)
6562 {
6563 sect_offset sect_off = attr->get_ref_die_offset ();
6564 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
6565 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6566 dwarf2_per_cu_data *per_cu
6567 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
6568 per_objfile->per_bfd);
6569
6570 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
6571 into another compilation unit, at root level. Regard this as a hint,
6572 and ignore it. This is a best effort, it only works if unit_type and
6573 lang are already set. */
6574 if (die->parent && die->parent->parent == NULL
6575 && per_cu->unit_type (false) == DW_UT_compile
6576 && per_cu->lang (false) == language_cplus)
6577 return;
6578
6579 /* If necessary, add it to the queue and load its DIEs. */
6580 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
6581 cu->lang ()))
6582 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
6583 false, cu->lang ());
6584
6585 cu->per_cu->imported_symtabs_push (per_cu);
6586 }
6587 }
6588
6589 /* RAII object that represents a process_die scope: i.e.,
6590 starts/finishes processing a DIE. */
6591 class process_die_scope
6592 {
6593 public:
6594 process_die_scope (die_info *die, dwarf2_cu *cu)
6595 : m_die (die), m_cu (cu)
6596 {
6597 /* We should only be processing DIEs not already in process. */
6598 gdb_assert (!m_die->in_process);
6599 m_die->in_process = true;
6600 }
6601
6602 ~process_die_scope ()
6603 {
6604 m_die->in_process = false;
6605
6606 /* If we're done processing the DIE for the CU that owns the line
6607 header, we don't need the line header anymore. */
6608 if (m_cu->line_header_die_owner == m_die)
6609 {
6610 delete m_cu->line_header;
6611 m_cu->line_header = NULL;
6612 m_cu->line_header_die_owner = NULL;
6613 }
6614 }
6615
6616 private:
6617 die_info *m_die;
6618 dwarf2_cu *m_cu;
6619 };
6620
6621 /* Process a die and its children. */
6622
6623 static void
6624 process_die (struct die_info *die, struct dwarf2_cu *cu)
6625 {
6626 process_die_scope scope (die, cu);
6627
6628 switch (die->tag)
6629 {
6630 case DW_TAG_padding:
6631 break;
6632 case DW_TAG_compile_unit:
6633 case DW_TAG_partial_unit:
6634 read_file_scope (die, cu);
6635 break;
6636 case DW_TAG_type_unit:
6637 read_type_unit_scope (die, cu);
6638 break;
6639 case DW_TAG_subprogram:
6640 /* Nested subprograms in Fortran get a prefix. */
6641 if (cu->lang () == language_fortran
6642 && die->parent != NULL
6643 && die->parent->tag == DW_TAG_subprogram)
6644 cu->processing_has_namespace_info = true;
6645 /* Fall through. */
6646 case DW_TAG_inlined_subroutine:
6647 read_func_scope (die, cu);
6648 break;
6649 case DW_TAG_lexical_block:
6650 case DW_TAG_try_block:
6651 case DW_TAG_catch_block:
6652 read_lexical_block_scope (die, cu);
6653 break;
6654 case DW_TAG_call_site:
6655 case DW_TAG_GNU_call_site:
6656 read_call_site_scope (die, cu);
6657 break;
6658 case DW_TAG_class_type:
6659 case DW_TAG_interface_type:
6660 case DW_TAG_structure_type:
6661 case DW_TAG_union_type:
6662 case DW_TAG_namelist:
6663 process_structure_scope (die, cu);
6664 break;
6665 case DW_TAG_enumeration_type:
6666 process_enumeration_scope (die, cu);
6667 break;
6668
6669 /* These dies have a type, but processing them does not create
6670 a symbol or recurse to process the children. Therefore we can
6671 read them on-demand through read_type_die. */
6672 case DW_TAG_subroutine_type:
6673 case DW_TAG_set_type:
6674 case DW_TAG_pointer_type:
6675 case DW_TAG_ptr_to_member_type:
6676 case DW_TAG_reference_type:
6677 case DW_TAG_rvalue_reference_type:
6678 case DW_TAG_string_type:
6679 break;
6680
6681 case DW_TAG_array_type:
6682 /* We only need to handle this case for Ada -- in other
6683 languages, it's normal for the compiler to emit a typedef
6684 instead. */
6685 if (cu->lang () != language_ada)
6686 break;
6687 /* FALLTHROUGH */
6688 case DW_TAG_base_type:
6689 case DW_TAG_subrange_type:
6690 case DW_TAG_generic_subrange:
6691 case DW_TAG_typedef:
6692 /* Add a typedef symbol for the type definition, if it has a
6693 DW_AT_name. */
6694 new_symbol (die, read_type_die (die, cu), cu);
6695 break;
6696 case DW_TAG_common_block:
6697 read_common_block (die, cu);
6698 break;
6699 case DW_TAG_common_inclusion:
6700 break;
6701 case DW_TAG_namespace:
6702 cu->processing_has_namespace_info = true;
6703 read_namespace (die, cu);
6704 break;
6705 case DW_TAG_module:
6706 cu->processing_has_namespace_info = true;
6707 read_module (die, cu);
6708 break;
6709 case DW_TAG_imported_declaration:
6710 cu->processing_has_namespace_info = true;
6711 if (read_alias (die, cu))
6712 break;
6713 /* The declaration is neither a global namespace nor a variable
6714 alias. */
6715 /* Fall through. */
6716 case DW_TAG_imported_module:
6717 cu->processing_has_namespace_info = true;
6718 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6719 || cu->lang () != language_fortran))
6720 complaint (_("Tag '%s' has unexpected children"),
6721 dwarf_tag_name (die->tag));
6722 read_import_statement (die, cu);
6723 break;
6724
6725 case DW_TAG_imported_unit:
6726 process_imported_unit_die (die, cu);
6727 break;
6728
6729 case DW_TAG_variable:
6730 read_variable (die, cu);
6731 break;
6732
6733 default:
6734 new_symbol (die, NULL, cu);
6735 break;
6736 }
6737 }
6738 \f
6739 /* DWARF name computation. */
6740
6741 /* A helper function for dwarf2_compute_name which determines whether DIE
6742 needs to have the name of the scope prepended to the name listed in the
6743 die. */
6744
6745 static int
6746 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6747 {
6748 struct attribute *attr;
6749
6750 switch (die->tag)
6751 {
6752 case DW_TAG_namespace:
6753 case DW_TAG_typedef:
6754 case DW_TAG_class_type:
6755 case DW_TAG_interface_type:
6756 case DW_TAG_structure_type:
6757 case DW_TAG_union_type:
6758 case DW_TAG_enumeration_type:
6759 case DW_TAG_enumerator:
6760 case DW_TAG_subprogram:
6761 case DW_TAG_inlined_subroutine:
6762 case DW_TAG_member:
6763 case DW_TAG_imported_declaration:
6764 return 1;
6765
6766 case DW_TAG_variable:
6767 case DW_TAG_constant:
6768 /* We only need to prefix "globally" visible variables. These include
6769 any variable marked with DW_AT_external or any variable that
6770 lives in a namespace. [Variables in anonymous namespaces
6771 require prefixing, but they are not DW_AT_external.] */
6772
6773 if (dwarf2_attr (die, DW_AT_specification, cu))
6774 {
6775 struct dwarf2_cu *spec_cu = cu;
6776
6777 return die_needs_namespace (die_specification (die, &spec_cu),
6778 spec_cu);
6779 }
6780
6781 attr = dwarf2_attr (die, DW_AT_external, cu);
6782 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6783 && die->parent->tag != DW_TAG_module)
6784 return 0;
6785 /* A variable in a lexical block of some kind does not need a
6786 namespace, even though in C++ such variables may be external
6787 and have a mangled name. */
6788 if (die->parent->tag == DW_TAG_lexical_block
6789 || die->parent->tag == DW_TAG_try_block
6790 || die->parent->tag == DW_TAG_catch_block
6791 || die->parent->tag == DW_TAG_subprogram)
6792 return 0;
6793 return 1;
6794
6795 default:
6796 return 0;
6797 }
6798 }
6799
6800 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
6801 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6802 defined for the given DIE. */
6803
6804 static struct attribute *
6805 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
6806 {
6807 struct attribute *attr;
6808
6809 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6810 if (attr == NULL)
6811 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6812
6813 return attr;
6814 }
6815
6816 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
6817 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6818 defined for the given DIE. */
6819
6820 static const char *
6821 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
6822 {
6823 const char *linkage_name;
6824
6825 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
6826 if (linkage_name == NULL)
6827 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
6828
6829 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
6830 See https://github.com/rust-lang/rust/issues/32925. */
6831 if (cu->lang () == language_rust && linkage_name != NULL
6832 && strchr (linkage_name, '{') != NULL)
6833 linkage_name = NULL;
6834
6835 return linkage_name;
6836 }
6837
6838 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6839 compute the physname for the object, which include a method's:
6840 - formal parameters (C++),
6841 - receiver type (Go),
6842
6843 The term "physname" is a bit confusing.
6844 For C++, for example, it is the demangled name.
6845 For Go, for example, it's the mangled name.
6846
6847 For Ada, return the DIE's linkage name rather than the fully qualified
6848 name. PHYSNAME is ignored..
6849
6850 The result is allocated on the objfile->per_bfd's obstack and
6851 canonicalized. */
6852
6853 static const char *
6854 dwarf2_compute_name (const char *name,
6855 struct die_info *die, struct dwarf2_cu *cu,
6856 int physname)
6857 {
6858 struct objfile *objfile = cu->per_objfile->objfile;
6859
6860 if (name == NULL)
6861 name = dwarf2_name (die, cu);
6862
6863 enum language lang = cu->lang ();
6864
6865 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
6866 but otherwise compute it by typename_concat inside GDB.
6867 FIXME: Actually this is not really true, or at least not always true.
6868 It's all very confusing. compute_and_set_names doesn't try to demangle
6869 Fortran names because there is no mangling standard. So new_symbol
6870 will set the demangled name to the result of dwarf2_full_name, and it is
6871 the demangled name that GDB uses if it exists. */
6872 if (lang == language_ada
6873 || (lang == language_fortran && physname))
6874 {
6875 /* For Ada unit, we prefer the linkage name over the name, as
6876 the former contains the exported name, which the user expects
6877 to be able to reference. Ideally, we want the user to be able
6878 to reference this entity using either natural or linkage name,
6879 but we haven't started looking at this enhancement yet. */
6880 const char *linkage_name = dw2_linkage_name (die, cu);
6881
6882 if (linkage_name != NULL)
6883 return linkage_name;
6884 }
6885
6886 /* These are the only languages we know how to qualify names in. */
6887 if (name != NULL
6888 && (lang == language_cplus
6889 || lang == language_fortran || lang == language_d
6890 || lang == language_rust))
6891 {
6892 if (die_needs_namespace (die, cu))
6893 {
6894 const char *prefix;
6895
6896 string_file buf;
6897
6898 prefix = determine_prefix (die, cu);
6899 if (*prefix != '\0')
6900 {
6901 gdb::unique_xmalloc_ptr<char> prefixed_name
6902 (typename_concat (NULL, prefix, name, physname, cu));
6903
6904 buf.puts (prefixed_name.get ());
6905 }
6906 else
6907 buf.puts (name);
6908
6909 /* Template parameters may be specified in the DIE's DW_AT_name, or
6910 as children with DW_TAG_template_type_param or
6911 DW_TAG_value_type_param. If the latter, add them to the name
6912 here. If the name already has template parameters, then
6913 skip this step; some versions of GCC emit both, and
6914 it is more efficient to use the pre-computed name.
6915
6916 Something to keep in mind about this process: it is very
6917 unlikely, or in some cases downright impossible, to produce
6918 something that will match the mangled name of a function.
6919 If the definition of the function has the same debug info,
6920 we should be able to match up with it anyway. But fallbacks
6921 using the minimal symbol, for instance to find a method
6922 implemented in a stripped copy of libstdc++, will not work.
6923 If we do not have debug info for the definition, we will have to
6924 match them up some other way.
6925
6926 When we do name matching there is a related problem with function
6927 templates; two instantiated function templates are allowed to
6928 differ only by their return types, which we do not add here. */
6929
6930 if (lang == language_cplus && strchr (name, '<') == NULL)
6931 {
6932 struct attribute *attr;
6933 struct die_info *child;
6934 int first = 1;
6935
6936 die->building_fullname = 1;
6937
6938 for (child = die->child; child != NULL; child = child->sibling)
6939 {
6940 struct type *type;
6941 LONGEST value;
6942 const gdb_byte *bytes;
6943 struct dwarf2_locexpr_baton *baton;
6944 struct value *v;
6945
6946 if (child->tag != DW_TAG_template_type_param
6947 && child->tag != DW_TAG_template_value_param)
6948 continue;
6949
6950 if (first)
6951 {
6952 buf.puts ("<");
6953 first = 0;
6954 }
6955 else
6956 buf.puts (", ");
6957
6958 attr = dwarf2_attr (child, DW_AT_type, cu);
6959 if (attr == NULL)
6960 {
6961 complaint (_("template parameter missing DW_AT_type"));
6962 buf.puts ("UNKNOWN_TYPE");
6963 continue;
6964 }
6965 type = die_type (child, cu);
6966
6967 if (child->tag == DW_TAG_template_type_param)
6968 {
6969 cu->language_defn->print_type (type, "", &buf, -1, 0,
6970 &type_print_raw_options);
6971 continue;
6972 }
6973
6974 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6975 if (attr == NULL)
6976 {
6977 complaint (_("template parameter missing "
6978 "DW_AT_const_value"));
6979 buf.puts ("UNKNOWN_VALUE");
6980 continue;
6981 }
6982
6983 dwarf2_const_value_attr (attr, type, name,
6984 &cu->comp_unit_obstack, cu,
6985 &value, &bytes, &baton);
6986
6987 if (type->has_no_signedness ())
6988 /* GDB prints characters as NUMBER 'CHAR'. If that's
6989 changed, this can use value_print instead. */
6990 cu->language_defn->printchar (value, type, &buf);
6991 else
6992 {
6993 struct value_print_options opts;
6994
6995 if (baton != NULL)
6996 v = dwarf2_evaluate_loc_desc (type, NULL,
6997 baton->data,
6998 baton->size,
6999 baton->per_cu,
7000 baton->per_objfile);
7001 else if (bytes != NULL)
7002 {
7003 v = value::allocate (type);
7004 memcpy (v->contents_writeable ().data (), bytes,
7005 type->length ());
7006 }
7007 else
7008 v = value_from_longest (type, value);
7009
7010 /* Specify decimal so that we do not depend on
7011 the radix. */
7012 get_formatted_print_options (&opts, 'd');
7013 opts.raw = true;
7014 value_print (v, &buf, &opts);
7015 release_value (v);
7016 }
7017 }
7018
7019 die->building_fullname = 0;
7020
7021 if (!first)
7022 {
7023 /* Close the argument list, with a space if necessary
7024 (nested templates). */
7025 if (!buf.empty () && buf.string ().back () == '>')
7026 buf.puts (" >");
7027 else
7028 buf.puts (">");
7029 }
7030 }
7031
7032 /* For C++ methods, append formal parameter type
7033 information, if PHYSNAME. */
7034
7035 if (physname && die->tag == DW_TAG_subprogram
7036 && lang == language_cplus)
7037 {
7038 struct type *type = read_type_die (die, cu);
7039
7040 c_type_print_args (type, &buf, 1, lang,
7041 &type_print_raw_options);
7042
7043 if (lang == language_cplus)
7044 {
7045 /* Assume that an artificial first parameter is
7046 "this", but do not crash if it is not. RealView
7047 marks unnamed (and thus unused) parameters as
7048 artificial; there is no way to differentiate
7049 the two cases. */
7050 if (type->num_fields () > 0
7051 && TYPE_FIELD_ARTIFICIAL (type, 0)
7052 && type->field (0).type ()->code () == TYPE_CODE_PTR
7053 && TYPE_CONST (type->field (0).type ()->target_type ()))
7054 buf.puts (" const");
7055 }
7056 }
7057
7058 const std::string &intermediate_name = buf.string ();
7059
7060 const char *canonical_name
7061 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
7062 objfile);
7063
7064 /* If we only computed INTERMEDIATE_NAME, or if
7065 INTERMEDIATE_NAME is already canonical, then we need to
7066 intern it. */
7067 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
7068 name = objfile->intern (intermediate_name);
7069 else
7070 name = canonical_name;
7071 }
7072 }
7073
7074 return name;
7075 }
7076
7077 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7078 If scope qualifiers are appropriate they will be added. The result
7079 will be allocated on the storage_obstack, or NULL if the DIE does
7080 not have a name. NAME may either be from a previous call to
7081 dwarf2_name or NULL.
7082
7083 The output string will be canonicalized (if C++). */
7084
7085 static const char *
7086 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7087 {
7088 return dwarf2_compute_name (name, die, cu, 0);
7089 }
7090
7091 /* Construct a physname for the given DIE in CU. NAME may either be
7092 from a previous call to dwarf2_name or NULL. The result will be
7093 allocated on the objfile_objstack or NULL if the DIE does not have a
7094 name.
7095
7096 The output string will be canonicalized (if C++). */
7097
7098 static const char *
7099 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7100 {
7101 struct objfile *objfile = cu->per_objfile->objfile;
7102 const char *retval, *mangled = NULL, *canon = NULL;
7103 int need_copy = 1;
7104
7105 /* In this case dwarf2_compute_name is just a shortcut not building anything
7106 on its own. */
7107 if (!die_needs_namespace (die, cu))
7108 return dwarf2_compute_name (name, die, cu, 1);
7109
7110 if (cu->lang () != language_rust)
7111 mangled = dw2_linkage_name (die, cu);
7112
7113 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7114 has computed. */
7115 gdb::unique_xmalloc_ptr<char> demangled;
7116 if (mangled != NULL)
7117 {
7118 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
7119 {
7120 /* Do nothing (do not demangle the symbol name). */
7121 }
7122 else
7123 {
7124 /* Use DMGL_RET_DROP for C++ template functions to suppress
7125 their return type. It is easier for GDB users to search
7126 for such functions as `name(params)' than `long name(params)'.
7127 In such case the minimal symbol names do not match the full
7128 symbol names but for template functions there is never a need
7129 to look up their definition from their declaration so
7130 the only disadvantage remains the minimal symbol variant
7131 `long name(params)' does not have the proper inferior type. */
7132 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
7133 | DMGL_RET_DROP));
7134 }
7135 if (demangled)
7136 canon = demangled.get ();
7137 else
7138 {
7139 canon = mangled;
7140 need_copy = 0;
7141 }
7142 }
7143
7144 if (canon == NULL || check_physname)
7145 {
7146 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7147
7148 if (canon != NULL && strcmp (physname, canon) != 0)
7149 {
7150 /* It may not mean a bug in GDB. The compiler could also
7151 compute DW_AT_linkage_name incorrectly. But in such case
7152 GDB would need to be bug-to-bug compatible. */
7153
7154 complaint (_("Computed physname <%s> does not match demangled <%s> "
7155 "(from linkage <%s>) - DIE at %s [in module %s]"),
7156 physname, canon, mangled, sect_offset_str (die->sect_off),
7157 objfile_name (objfile));
7158
7159 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7160 is available here - over computed PHYSNAME. It is safer
7161 against both buggy GDB and buggy compilers. */
7162
7163 retval = canon;
7164 }
7165 else
7166 {
7167 retval = physname;
7168 need_copy = 0;
7169 }
7170 }
7171 else
7172 retval = canon;
7173
7174 if (need_copy)
7175 retval = objfile->intern (retval);
7176
7177 return retval;
7178 }
7179
7180 /* Inspect DIE in CU for a namespace alias or a variable with alias
7181 attribute. If one exists, record a new symbol for it.
7182
7183 Returns true if an alias was recorded, false otherwise. */
7184
7185 static bool
7186 read_alias (struct die_info *die, struct dwarf2_cu *cu)
7187 {
7188 struct attribute *attr;
7189
7190 /* If the die does not have a name, this is neither a namespace
7191 alias nor a variable alias. */
7192 attr = dwarf2_attr (die, DW_AT_name, cu);
7193 if (attr != NULL)
7194 {
7195 int num;
7196 struct die_info *d = die;
7197 struct dwarf2_cu *imported_cu = cu;
7198
7199 /* If the compiler has nested DW_AT_imported_declaration DIEs,
7200 keep inspecting DIEs until we hit the underlying import. */
7201 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
7202 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
7203 {
7204 attr = dwarf2_attr (d, DW_AT_import, cu);
7205 if (attr == NULL)
7206 break;
7207
7208 d = follow_die_ref (d, attr, &imported_cu);
7209 if (d->tag != DW_TAG_imported_declaration)
7210 break;
7211 }
7212
7213 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
7214 {
7215 complaint (_("DIE at %s has too many recursively imported "
7216 "declarations"), sect_offset_str (d->sect_off));
7217 return false;
7218 }
7219
7220 if (attr != NULL)
7221 {
7222 struct type *type;
7223 if (d->tag == DW_TAG_variable)
7224 {
7225 /* This declaration is a C/C++ global variable alias.
7226 Add a symbol for it whose type is the same as the
7227 aliased variable's. */
7228 type = die_type (d, imported_cu);
7229 struct symbol *sym = new_symbol (die, type, cu);
7230 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
7231 sym->set_aclass_index (LOC_UNRESOLVED);
7232 if (attr != nullptr)
7233 var_decode_location (attr, sym, cu);
7234 return true;
7235 }
7236 else
7237 {
7238 sect_offset sect_off = attr->get_ref_die_offset ();
7239 type = get_die_type_at_offset (sect_off, cu->per_cu,
7240 cu->per_objfile);
7241 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
7242 {
7243 /* This declaration is a global namespace alias. Add
7244 a symbol for it whose type is the aliased
7245 namespace. */
7246 new_symbol (die, type, cu);
7247 return true;
7248 }
7249 }
7250 }
7251 }
7252 return false;
7253 }
7254
7255 /* Return the using directives repository (global or local?) to use in the
7256 current context for CU.
7257
7258 For Ada, imported declarations can materialize renamings, which *may* be
7259 global. However it is impossible (for now?) in DWARF to distinguish
7260 "external" imported declarations and "static" ones. As all imported
7261 declarations seem to be static in all other languages, make them all CU-wide
7262 global only in Ada. */
7263
7264 static struct using_direct **
7265 using_directives (struct dwarf2_cu *cu)
7266 {
7267 if (cu->lang () == language_ada
7268 && cu->get_builder ()->outermost_context_p ())
7269 return cu->get_builder ()->get_global_using_directives ();
7270 else
7271 return cu->get_builder ()->get_local_using_directives ();
7272 }
7273
7274 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
7275 given CU. If the format is not recognized or the attribute is
7276 not present, set it to 0. */
7277
7278 static unsigned int
7279 read_decl_line (struct die_info *die, struct dwarf2_cu *cu)
7280 {
7281 struct attribute *decl_line = dwarf2_attr (die, DW_AT_decl_line, cu);
7282 if (decl_line == nullptr)
7283 return 0;
7284 if (decl_line->form_is_constant ())
7285 {
7286 LONGEST val = decl_line->constant_value (0);
7287 if (0 <= val && val <= UINT_MAX)
7288 return (unsigned int) val;
7289
7290 complaint (_("Declared line for using directive is too large"));
7291 return 0;
7292 }
7293
7294 complaint (_("Declared line for using directive is of incorrect format"));
7295 return 0;
7296 }
7297
7298 /* Read the import statement specified by the given die and record it. */
7299
7300 static void
7301 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7302 {
7303 struct objfile *objfile = cu->per_objfile->objfile;
7304 struct attribute *import_attr;
7305 struct die_info *imported_die, *child_die;
7306 struct dwarf2_cu *imported_cu;
7307 const char *imported_name;
7308 const char *imported_name_prefix;
7309 const char *canonical_name;
7310 const char *import_alias;
7311 const char *imported_declaration = NULL;
7312 const char *import_prefix;
7313 std::vector<const char *> excludes;
7314
7315 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7316 if (import_attr == NULL)
7317 {
7318 complaint (_("Tag '%s' has no DW_AT_import"),
7319 dwarf_tag_name (die->tag));
7320 return;
7321 }
7322
7323 imported_cu = cu;
7324 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7325 imported_name = dwarf2_name (imported_die, imported_cu);
7326 if (imported_name == NULL)
7327 {
7328 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7329
7330 The import in the following code:
7331 namespace A
7332 {
7333 typedef int B;
7334 }
7335
7336 int main ()
7337 {
7338 using A::B;
7339 B b;
7340 return b;
7341 }
7342
7343 ...
7344 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7345 <52> DW_AT_decl_file : 1
7346 <53> DW_AT_decl_line : 6
7347 <54> DW_AT_import : <0x75>
7348 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7349 <59> DW_AT_name : B
7350 <5b> DW_AT_decl_file : 1
7351 <5c> DW_AT_decl_line : 2
7352 <5d> DW_AT_type : <0x6e>
7353 ...
7354 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7355 <76> DW_AT_byte_size : 4
7356 <77> DW_AT_encoding : 5 (signed)
7357
7358 imports the wrong die ( 0x75 instead of 0x58 ).
7359 This case will be ignored until the gcc bug is fixed. */
7360 return;
7361 }
7362
7363 /* Figure out the local name after import. */
7364 import_alias = dwarf2_name (die, cu);
7365
7366 /* Figure out where the statement is being imported to. */
7367 import_prefix = determine_prefix (die, cu);
7368
7369 /* Figure out what the scope of the imported die is and prepend it
7370 to the name of the imported die. */
7371 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7372
7373 if (imported_die->tag != DW_TAG_namespace
7374 && imported_die->tag != DW_TAG_module)
7375 {
7376 imported_declaration = imported_name;
7377 canonical_name = imported_name_prefix;
7378 }
7379 else if (strlen (imported_name_prefix) > 0)
7380 canonical_name = obconcat (&objfile->objfile_obstack,
7381 imported_name_prefix,
7382 (cu->lang () == language_d
7383 ? "."
7384 : "::"),
7385 imported_name, (char *) NULL);
7386 else
7387 canonical_name = imported_name;
7388
7389 if (die->tag == DW_TAG_imported_module
7390 && cu->lang () == language_fortran)
7391 for (child_die = die->child; child_die && child_die->tag;
7392 child_die = child_die->sibling)
7393 {
7394 /* DWARF-4: A Fortran use statement with a “rename list” may be
7395 represented by an imported module entry with an import attribute
7396 referring to the module and owned entries corresponding to those
7397 entities that are renamed as part of being imported. */
7398
7399 if (child_die->tag != DW_TAG_imported_declaration)
7400 {
7401 complaint (_("child DW_TAG_imported_declaration expected "
7402 "- DIE at %s [in module %s]"),
7403 sect_offset_str (child_die->sect_off),
7404 objfile_name (objfile));
7405 continue;
7406 }
7407
7408 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7409 if (import_attr == NULL)
7410 {
7411 complaint (_("Tag '%s' has no DW_AT_import"),
7412 dwarf_tag_name (child_die->tag));
7413 continue;
7414 }
7415
7416 imported_cu = cu;
7417 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7418 &imported_cu);
7419 imported_name = dwarf2_name (imported_die, imported_cu);
7420 if (imported_name == NULL)
7421 {
7422 complaint (_("child DW_TAG_imported_declaration has unknown "
7423 "imported name - DIE at %s [in module %s]"),
7424 sect_offset_str (child_die->sect_off),
7425 objfile_name (objfile));
7426 continue;
7427 }
7428
7429 excludes.push_back (imported_name);
7430
7431 process_die (child_die, cu);
7432 }
7433
7434 add_using_directive (using_directives (cu),
7435 import_prefix,
7436 canonical_name,
7437 import_alias,
7438 imported_declaration,
7439 excludes,
7440 read_decl_line (die, cu),
7441 0,
7442 &objfile->objfile_obstack);
7443 }
7444
7445 /* ICC<14 does not output the required DW_AT_declaration on incomplete
7446 types, but gives them a size of zero. Starting with version 14,
7447 ICC is compatible with GCC. */
7448
7449 static bool
7450 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
7451 {
7452 if (!cu->checked_producer)
7453 check_producer (cu);
7454
7455 return cu->producer_is_icc_lt_14;
7456 }
7457
7458 /* ICC generates a DW_AT_type for C void functions. This was observed on
7459 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
7460 which says that void functions should not have a DW_AT_type. */
7461
7462 static bool
7463 producer_is_icc (struct dwarf2_cu *cu)
7464 {
7465 if (!cu->checked_producer)
7466 check_producer (cu);
7467
7468 return cu->producer_is_icc;
7469 }
7470
7471 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7472 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7473 this, it was first present in GCC release 4.3.0. */
7474
7475 static bool
7476 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7477 {
7478 if (!cu->checked_producer)
7479 check_producer (cu);
7480
7481 return cu->producer_is_gcc_lt_4_3;
7482 }
7483
7484 /* See dwarf2/read.h. */
7485 bool
7486 producer_is_clang (struct dwarf2_cu *cu)
7487 {
7488 if (!cu->checked_producer)
7489 check_producer (cu);
7490
7491 return cu->producer_is_clang;
7492 }
7493
7494 static file_and_directory &
7495 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
7496 {
7497 if (cu->per_cu->fnd != nullptr)
7498 return *cu->per_cu->fnd;
7499
7500 /* Find the filename. Do not use dwarf2_name here, since the filename
7501 is not a source language identifier. */
7502 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
7503 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
7504
7505 if (res.get_comp_dir () == nullptr
7506 && producer_is_gcc_lt_4_3 (cu)
7507 && res.get_name () != nullptr
7508 && IS_ABSOLUTE_PATH (res.get_name ()))
7509 {
7510 res.set_comp_dir (ldirname (res.get_name ()));
7511 res.set_name (make_unique_xstrdup (lbasename (res.get_name ())));
7512 }
7513
7514 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
7515 return *cu->per_cu->fnd;
7516 }
7517
7518 /* Handle DW_AT_stmt_list for a compilation unit.
7519 DIE is the DW_TAG_compile_unit die for CU.
7520 COMP_DIR is the compilation directory. LOWPC is passed to
7521 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
7522
7523 static void
7524 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7525 const file_and_directory &fnd, CORE_ADDR lowpc,
7526 bool have_code) /* ARI: editCase function */
7527 {
7528 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7529 struct attribute *attr;
7530 hashval_t line_header_local_hash;
7531 void **slot;
7532 int decode_mapping;
7533
7534 gdb_assert (! cu->per_cu->is_debug_types);
7535
7536 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7537 if (attr == NULL || !attr->form_is_unsigned ())
7538 return;
7539
7540 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7541
7542 /* The line header hash table is only created if needed (it exists to
7543 prevent redundant reading of the line table for partial_units).
7544 If we're given a partial_unit, we'll need it. If we're given a
7545 compile_unit, then use the line header hash table if it's already
7546 created, but don't create one just yet. */
7547
7548 if (per_objfile->line_header_hash == NULL
7549 && die->tag == DW_TAG_partial_unit)
7550 {
7551 per_objfile->line_header_hash
7552 .reset (htab_create_alloc (127, line_header_hash_voidp,
7553 line_header_eq_voidp,
7554 htab_delete_entry<line_header>,
7555 xcalloc, xfree));
7556 }
7557
7558 line_header line_header_local (line_offset, cu->per_cu->is_dwz);
7559 line_header_local_hash = line_header_hash (&line_header_local);
7560 if (per_objfile->line_header_hash != NULL)
7561 {
7562 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7563 &line_header_local,
7564 line_header_local_hash, NO_INSERT);
7565
7566 /* For DW_TAG_compile_unit we need info like symtab::linetable which
7567 is not present in *SLOT (since if there is something in *SLOT then
7568 it will be for a partial_unit). */
7569 if (die->tag == DW_TAG_partial_unit && slot != NULL)
7570 {
7571 gdb_assert (*slot != NULL);
7572 cu->line_header = (struct line_header *) *slot;
7573 return;
7574 }
7575 }
7576
7577 /* dwarf_decode_line_header does not yet provide sufficient information.
7578 We always have to call also dwarf_decode_lines for it. */
7579 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
7580 fnd.get_comp_dir ());
7581 if (lh == NULL)
7582 return;
7583
7584 cu->line_header = lh.release ();
7585 cu->line_header_die_owner = die;
7586
7587 if (per_objfile->line_header_hash == NULL)
7588 slot = NULL;
7589 else
7590 {
7591 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7592 &line_header_local,
7593 line_header_local_hash, INSERT);
7594 gdb_assert (slot != NULL);
7595 }
7596 if (slot != NULL && *slot == NULL)
7597 {
7598 /* This newly decoded line number information unit will be owned
7599 by line_header_hash hash table. */
7600 *slot = cu->line_header;
7601 cu->line_header_die_owner = NULL;
7602 }
7603 else
7604 {
7605 /* We cannot free any current entry in (*slot) as that struct line_header
7606 may be already used by multiple CUs. Create only temporary decoded
7607 line_header for this CU - it may happen at most once for each line
7608 number information unit. And if we're not using line_header_hash
7609 then this is what we want as well. */
7610 gdb_assert (die->tag != DW_TAG_partial_unit);
7611 }
7612 decode_mapping = (die->tag != DW_TAG_partial_unit);
7613 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
7614 then there won't be any interesting code in the CU, but a check later on
7615 (in lnp_state_machine::check_line_address) will fail to properly exclude
7616 an entry that was removed via --gc-sections. */
7617 if (have_code)
7618 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
7619 }
7620
7621 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7622
7623 static void
7624 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7625 {
7626 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7627 struct objfile *objfile = per_objfile->objfile;
7628 struct gdbarch *gdbarch = objfile->arch ();
7629 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7630 CORE_ADDR highpc = ((CORE_ADDR) 0);
7631 struct attribute *attr;
7632 struct die_info *child_die;
7633 CORE_ADDR baseaddr;
7634
7635 prepare_one_comp_unit (cu, die, cu->lang ());
7636 baseaddr = objfile->text_section_offset ();
7637
7638 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7639
7640 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7641 from finish_block. */
7642 if (lowpc == ((CORE_ADDR) -1))
7643 lowpc = highpc;
7644 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
7645
7646 file_and_directory &fnd = find_file_and_directory (die, cu);
7647
7648 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
7649 lowpc);
7650
7651 gdb_assert (per_objfile->sym_cu == nullptr);
7652 scoped_restore restore_sym_cu
7653 = make_scoped_restore (&per_objfile->sym_cu, cu);
7654
7655 /* Decode line number information if present. We do this before
7656 processing child DIEs, so that the line header table is available
7657 for DW_AT_decl_file. */
7658 handle_DW_AT_stmt_list (die, cu, fnd, lowpc, lowpc != highpc);
7659
7660 /* Process all dies in compilation unit. */
7661 if (die->child != NULL)
7662 {
7663 child_die = die->child;
7664 while (child_die && child_die->tag)
7665 {
7666 process_die (child_die, cu);
7667 child_die = child_die->sibling;
7668 }
7669 }
7670 per_objfile->sym_cu = nullptr;
7671
7672 /* Decode macro information, if present. Dwarf 2 macro information
7673 refers to information in the line number info statement program
7674 header, so we can only read it if we've read the header
7675 successfully. */
7676 attr = dwarf2_attr (die, DW_AT_macros, cu);
7677 if (attr == NULL)
7678 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7679 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7680 {
7681 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7682 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
7683
7684 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
7685 }
7686 else
7687 {
7688 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7689 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7690 {
7691 unsigned int macro_offset = attr->as_unsigned ();
7692
7693 dwarf_decode_macros (cu, macro_offset, 0);
7694 }
7695 }
7696 }
7697
7698 void
7699 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
7700 {
7701 struct type_unit_group *tu_group;
7702 int first_time;
7703 struct attribute *attr;
7704 unsigned int i;
7705 struct signatured_type *sig_type;
7706
7707 gdb_assert (per_cu->is_debug_types);
7708 sig_type = (struct signatured_type *) per_cu;
7709
7710 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
7711
7712 /* If we're using .gdb_index (includes -readnow) then
7713 per_cu->type_unit_group may not have been set up yet. */
7714 if (sig_type->type_unit_group == NULL)
7715 sig_type->type_unit_group = get_type_unit_group (this, attr);
7716 tu_group = sig_type->type_unit_group;
7717
7718 /* If we've already processed this stmt_list there's no real need to
7719 do it again, we could fake it and just recreate the part we need
7720 (file name,index -> symtab mapping). If data shows this optimization
7721 is useful we can do it then. */
7722 type_unit_group_unshareable *tug_unshare
7723 = per_objfile->get_type_unit_group_unshareable (tu_group);
7724 first_time = tug_unshare->compunit_symtab == NULL;
7725
7726 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7727 debug info. */
7728 line_header_up lh;
7729 if (attr != NULL && attr->form_is_unsigned ())
7730 {
7731 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7732 lh = dwarf_decode_line_header (line_offset, this, nullptr);
7733 }
7734 if (lh == NULL)
7735 {
7736 if (first_time)
7737 start_compunit_symtab ("", NULL, 0);
7738 else
7739 {
7740 gdb_assert (tug_unshare->symtabs == NULL);
7741 gdb_assert (m_builder == nullptr);
7742 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7743 m_builder.reset (new struct buildsym_compunit
7744 (cust->objfile (), "",
7745 cust->dirname (),
7746 cust->language (),
7747 0, cust));
7748 list_in_scope = get_builder ()->get_file_symbols ();
7749 }
7750 return;
7751 }
7752
7753 line_header = lh.release ();
7754 line_header_die_owner = die;
7755
7756 if (first_time)
7757 {
7758 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
7759
7760 /* Note: We don't assign tu_group->compunit_symtab yet because we're
7761 still initializing it, and our caller (a few levels up)
7762 process_full_type_unit still needs to know if this is the first
7763 time. */
7764
7765 tug_unshare->symtabs
7766 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
7767 struct symtab *, line_header->file_names_size ());
7768
7769 auto &file_names = line_header->file_names ();
7770 for (i = 0; i < file_names.size (); ++i)
7771 {
7772 file_entry &fe = file_names[i];
7773 dwarf2_start_subfile (this, fe, *line_header);
7774 buildsym_compunit *b = get_builder ();
7775 subfile *sf = b->get_current_subfile ();
7776
7777 if (sf->symtab == nullptr)
7778 {
7779 /* NOTE: start_subfile will recognize when it's been
7780 passed a file it has already seen. So we can't
7781 assume there's a simple mapping from
7782 cu->line_header->file_names to subfiles, plus
7783 cu->line_header->file_names may contain dups. */
7784 const char *name = sf->name.c_str ();
7785 const char *name_for_id = sf->name_for_id.c_str ();
7786 sf->symtab = allocate_symtab (cust, name, name_for_id);
7787 }
7788
7789 fe.symtab = b->get_current_subfile ()->symtab;
7790 tug_unshare->symtabs[i] = fe.symtab;
7791 }
7792 }
7793 else
7794 {
7795 gdb_assert (m_builder == nullptr);
7796 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7797 m_builder.reset (new struct buildsym_compunit
7798 (cust->objfile (), "",
7799 cust->dirname (),
7800 cust->language (),
7801 0, cust));
7802 list_in_scope = get_builder ()->get_file_symbols ();
7803
7804 auto &file_names = line_header->file_names ();
7805 for (i = 0; i < file_names.size (); ++i)
7806 {
7807 file_entry &fe = file_names[i];
7808 fe.symtab = tug_unshare->symtabs[i];
7809 }
7810 }
7811
7812 /* The main symtab is allocated last. Type units don't have DW_AT_name
7813 so they don't have a "real" (so to speak) symtab anyway.
7814 There is later code that will assign the main symtab to all symbols
7815 that don't have one. We need to handle the case of a symbol with a
7816 missing symtab (DW_AT_decl_file) anyway. */
7817 }
7818
7819 /* Process DW_TAG_type_unit.
7820 For TUs we want to skip the first top level sibling if it's not the
7821 actual type being defined by this TU. In this case the first top
7822 level sibling is there to provide context only. */
7823
7824 static void
7825 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7826 {
7827 struct die_info *child_die;
7828
7829 prepare_one_comp_unit (cu, die, language_minimal);
7830
7831 /* Initialize (or reinitialize) the machinery for building symtabs.
7832 We do this before processing child DIEs, so that the line header table
7833 is available for DW_AT_decl_file. */
7834 cu->setup_type_unit_groups (die);
7835
7836 if (die->child != NULL)
7837 {
7838 child_die = die->child;
7839 while (child_die && child_die->tag)
7840 {
7841 process_die (child_die, cu);
7842 child_die = child_die->sibling;
7843 }
7844 }
7845 }
7846 \f
7847 /* DWO/DWP files.
7848
7849 http://gcc.gnu.org/wiki/DebugFission
7850 http://gcc.gnu.org/wiki/DebugFissionDWP
7851
7852 To simplify handling of both DWO files ("object" files with the DWARF info)
7853 and DWP files (a file with the DWOs packaged up into one file), we treat
7854 DWP files as having a collection of virtual DWO files. */
7855
7856 static hashval_t
7857 hash_dwo_file (const void *item)
7858 {
7859 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
7860 hashval_t hash;
7861
7862 hash = htab_hash_string (dwo_file->dwo_name);
7863 if (dwo_file->comp_dir != NULL)
7864 hash += htab_hash_string (dwo_file->comp_dir);
7865 return hash;
7866 }
7867
7868 static int
7869 eq_dwo_file (const void *item_lhs, const void *item_rhs)
7870 {
7871 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
7872 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
7873
7874 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
7875 return 0;
7876 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
7877 return lhs->comp_dir == rhs->comp_dir;
7878 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
7879 }
7880
7881 /* Allocate a hash table for DWO files. */
7882
7883 static htab_up
7884 allocate_dwo_file_hash_table ()
7885 {
7886 return htab_up (htab_create_alloc (41,
7887 hash_dwo_file,
7888 eq_dwo_file,
7889 htab_delete_entry<dwo_file>,
7890 xcalloc, xfree));
7891 }
7892
7893 /* Lookup DWO file DWO_NAME. */
7894
7895 static void **
7896 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
7897 const char *dwo_name,
7898 const char *comp_dir)
7899 {
7900 struct dwo_file find_entry;
7901 void **slot;
7902
7903 if (per_objfile->per_bfd->dwo_files == NULL)
7904 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
7905
7906 find_entry.dwo_name = dwo_name;
7907 find_entry.comp_dir = comp_dir;
7908 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
7909 INSERT);
7910
7911 return slot;
7912 }
7913
7914 static hashval_t
7915 hash_dwo_unit (const void *item)
7916 {
7917 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
7918
7919 /* This drops the top 32 bits of the id, but is ok for a hash. */
7920 return dwo_unit->signature;
7921 }
7922
7923 static int
7924 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7925 {
7926 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
7927 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
7928
7929 /* The signature is assumed to be unique within the DWO file.
7930 So while object file CU dwo_id's always have the value zero,
7931 that's OK, assuming each object file DWO file has only one CU,
7932 and that's the rule for now. */
7933 return lhs->signature == rhs->signature;
7934 }
7935
7936 /* Allocate a hash table for DWO CUs,TUs.
7937 There is one of these tables for each of CUs,TUs for each DWO file. */
7938
7939 static htab_up
7940 allocate_dwo_unit_table ()
7941 {
7942 /* Start out with a pretty small number.
7943 Generally DWO files contain only one CU and maybe some TUs. */
7944 return htab_up (htab_create_alloc (3,
7945 hash_dwo_unit,
7946 eq_dwo_unit,
7947 NULL, xcalloc, xfree));
7948 }
7949
7950 /* die_reader_func for create_dwo_cu. */
7951
7952 static void
7953 create_dwo_cu_reader (const struct die_reader_specs *reader,
7954 const gdb_byte *info_ptr,
7955 struct die_info *comp_unit_die,
7956 struct dwo_file *dwo_file,
7957 struct dwo_unit *dwo_unit)
7958 {
7959 struct dwarf2_cu *cu = reader->cu;
7960 sect_offset sect_off = cu->per_cu->sect_off;
7961 struct dwarf2_section_info *section = cu->per_cu->section;
7962
7963 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7964 if (!signature.has_value ())
7965 {
7966 complaint (_("Dwarf Error: debug entry at offset %s is missing"
7967 " its dwo_id [in module %s]"),
7968 sect_offset_str (sect_off), dwo_file->dwo_name);
7969 return;
7970 }
7971
7972 dwo_unit->dwo_file = dwo_file;
7973 dwo_unit->signature = *signature;
7974 dwo_unit->section = section;
7975 dwo_unit->sect_off = sect_off;
7976 dwo_unit->length = cu->per_cu->length ();
7977
7978 dwarf_read_debug_printf (" offset %s, dwo_id %s",
7979 sect_offset_str (sect_off),
7980 hex_string (dwo_unit->signature));
7981 }
7982
7983 /* Create the dwo_units for the CUs in a DWO_FILE.
7984 Note: This function processes DWO files only, not DWP files. */
7985
7986 static void
7987 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
7988 dwarf2_cu *cu, struct dwo_file &dwo_file,
7989 dwarf2_section_info &section, htab_up &cus_htab)
7990 {
7991 struct objfile *objfile = per_objfile->objfile;
7992 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7993 const gdb_byte *info_ptr, *end_ptr;
7994
7995 section.read (objfile);
7996 info_ptr = section.buffer;
7997
7998 if (info_ptr == NULL)
7999 return;
8000
8001 dwarf_read_debug_printf ("Reading %s for %s:",
8002 section.get_name (),
8003 section.get_file_name ());
8004
8005 end_ptr = info_ptr + section.size;
8006 while (info_ptr < end_ptr)
8007 {
8008 struct dwarf2_per_cu_data per_cu;
8009 struct dwo_unit read_unit {};
8010 struct dwo_unit *dwo_unit;
8011 void **slot;
8012 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
8013
8014 per_cu.per_bfd = per_bfd;
8015 per_cu.is_debug_types = 0;
8016 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
8017 per_cu.section = &section;
8018
8019 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
8020 if (!reader.dummy_p)
8021 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8022 &dwo_file, &read_unit);
8023 info_ptr += per_cu.length ();
8024
8025 // If the unit could not be parsed, skip it.
8026 if (read_unit.dwo_file == NULL)
8027 continue;
8028
8029 if (cus_htab == NULL)
8030 cus_htab = allocate_dwo_unit_table ();
8031
8032 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
8033 struct dwo_unit);
8034 *dwo_unit = read_unit;
8035 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
8036 gdb_assert (slot != NULL);
8037 if (*slot != NULL)
8038 {
8039 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
8040 sect_offset dup_sect_off = dup_cu->sect_off;
8041
8042 complaint (_("debug cu entry at offset %s is duplicate to"
8043 " the entry at offset %s, signature %s"),
8044 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
8045 hex_string (dwo_unit->signature));
8046 }
8047 *slot = (void *)dwo_unit;
8048 }
8049 }
8050
8051 /* DWP file .debug_{cu,tu}_index section format:
8052 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8053 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
8054
8055 DWP Versions 1 & 2 are older, pre-standard format versions. The first
8056 officially standard DWP format was published with DWARF v5 and is called
8057 Version 5. There are no versions 3 or 4.
8058
8059 DWP Version 1:
8060
8061 Both index sections have the same format, and serve to map a 64-bit
8062 signature to a set of section numbers. Each section begins with a header,
8063 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8064 indexes, and a pool of 32-bit section numbers. The index sections will be
8065 aligned at 8-byte boundaries in the file.
8066
8067 The index section header consists of:
8068
8069 V, 32 bit version number
8070 -, 32 bits unused
8071 N, 32 bit number of compilation units or type units in the index
8072 M, 32 bit number of slots in the hash table
8073
8074 Numbers are recorded using the byte order of the application binary.
8075
8076 The hash table begins at offset 16 in the section, and consists of an array
8077 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8078 order of the application binary). Unused slots in the hash table are 0.
8079 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8080
8081 The parallel table begins immediately after the hash table
8082 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8083 array of 32-bit indexes (using the byte order of the application binary),
8084 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8085 table contains a 32-bit index into the pool of section numbers. For unused
8086 hash table slots, the corresponding entry in the parallel table will be 0.
8087
8088 The pool of section numbers begins immediately following the hash table
8089 (at offset 16 + 12 * M from the beginning of the section). The pool of
8090 section numbers consists of an array of 32-bit words (using the byte order
8091 of the application binary). Each item in the array is indexed starting
8092 from 0. The hash table entry provides the index of the first section
8093 number in the set. Additional section numbers in the set follow, and the
8094 set is terminated by a 0 entry (section number 0 is not used in ELF).
8095
8096 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8097 section must be the first entry in the set, and the .debug_abbrev.dwo must
8098 be the second entry. Other members of the set may follow in any order.
8099
8100 ---
8101
8102 DWP Versions 2 and 5:
8103
8104 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
8105 and the entries in the index tables are now offsets into these sections.
8106 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
8107 section.
8108
8109 Index Section Contents:
8110 Header
8111 Hash Table of Signatures dwp_hash_table.hash_table
8112 Parallel Table of Indices dwp_hash_table.unit_table
8113 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
8114 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
8115
8116 The index section header consists of:
8117
8118 V, 32 bit version number
8119 L, 32 bit number of columns in the table of section offsets
8120 N, 32 bit number of compilation units or type units in the index
8121 M, 32 bit number of slots in the hash table
8122
8123 Numbers are recorded using the byte order of the application binary.
8124
8125 The hash table has the same format as version 1.
8126 The parallel table of indices has the same format as version 1,
8127 except that the entries are origin-1 indices into the table of sections
8128 offsets and the table of section sizes.
8129
8130 The table of offsets begins immediately following the parallel table
8131 (at offset 16 + 12 * M from the beginning of the section). The table is
8132 a two-dimensional array of 32-bit words (using the byte order of the
8133 application binary), with L columns and N+1 rows, in row-major order.
8134 Each row in the array is indexed starting from 0. The first row provides
8135 a key to the remaining rows: each column in this row provides an identifier
8136 for a debug section, and the offsets in the same column of subsequent rows
8137 refer to that section. The section identifiers for Version 2 are:
8138
8139 DW_SECT_INFO 1 .debug_info.dwo
8140 DW_SECT_TYPES 2 .debug_types.dwo
8141 DW_SECT_ABBREV 3 .debug_abbrev.dwo
8142 DW_SECT_LINE 4 .debug_line.dwo
8143 DW_SECT_LOC 5 .debug_loc.dwo
8144 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
8145 DW_SECT_MACINFO 7 .debug_macinfo.dwo
8146 DW_SECT_MACRO 8 .debug_macro.dwo
8147
8148 The section identifiers for Version 5 are:
8149
8150 DW_SECT_INFO_V5 1 .debug_info.dwo
8151 DW_SECT_RESERVED_V5 2 --
8152 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
8153 DW_SECT_LINE_V5 4 .debug_line.dwo
8154 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
8155 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
8156 DW_SECT_MACRO_V5 7 .debug_macro.dwo
8157 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
8158
8159 The offsets provided by the CU and TU index sections are the base offsets
8160 for the contributions made by each CU or TU to the corresponding section
8161 in the package file. Each CU and TU header contains an abbrev_offset
8162 field, used to find the abbreviations table for that CU or TU within the
8163 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
8164 be interpreted as relative to the base offset given in the index section.
8165 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
8166 should be interpreted as relative to the base offset for .debug_line.dwo,
8167 and offsets into other debug sections obtained from DWARF attributes should
8168 also be interpreted as relative to the corresponding base offset.
8169
8170 The table of sizes begins immediately following the table of offsets.
8171 Like the table of offsets, it is a two-dimensional array of 32-bit words,
8172 with L columns and N rows, in row-major order. Each row in the array is
8173 indexed starting from 1 (row 0 is shared by the two tables).
8174
8175 ---
8176
8177 Hash table lookup is handled the same in version 1 and 2:
8178
8179 We assume that N and M will not exceed 2^32 - 1.
8180 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8181
8182 Given a 64-bit compilation unit signature or a type signature S, an entry
8183 in the hash table is located as follows:
8184
8185 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8186 the low-order k bits all set to 1.
8187
8188 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8189
8190 3) If the hash table entry at index H matches the signature, use that
8191 entry. If the hash table entry at index H is unused (all zeroes),
8192 terminate the search: the signature is not present in the table.
8193
8194 4) Let H = (H + H') modulo M. Repeat at Step 3.
8195
8196 Because M > N and H' and M are relatively prime, the search is guaranteed
8197 to stop at an unused slot or find the match. */
8198
8199 /* Create a hash table to map DWO IDs to their CU/TU entry in
8200 .debug_{info,types}.dwo in DWP_FILE.
8201 Returns NULL if there isn't one.
8202 Note: This function processes DWP files only, not DWO files. */
8203
8204 static struct dwp_hash_table *
8205 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
8206 struct dwp_file *dwp_file, int is_debug_types)
8207 {
8208 struct objfile *objfile = per_objfile->objfile;
8209 bfd *dbfd = dwp_file->dbfd.get ();
8210 const gdb_byte *index_ptr, *index_end;
8211 struct dwarf2_section_info *index;
8212 uint32_t version, nr_columns, nr_units, nr_slots;
8213 struct dwp_hash_table *htab;
8214
8215 if (is_debug_types)
8216 index = &dwp_file->sections.tu_index;
8217 else
8218 index = &dwp_file->sections.cu_index;
8219
8220 if (index->empty ())
8221 return NULL;
8222 index->read (objfile);
8223
8224 index_ptr = index->buffer;
8225 index_end = index_ptr + index->size;
8226
8227 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
8228 For now it's safe to just read 4 bytes (particularly as it's difficult to
8229 tell if you're dealing with Version 5 before you've read the version). */
8230 version = read_4_bytes (dbfd, index_ptr);
8231 index_ptr += 4;
8232 if (version == 2 || version == 5)
8233 nr_columns = read_4_bytes (dbfd, index_ptr);
8234 else
8235 nr_columns = 0;
8236 index_ptr += 4;
8237 nr_units = read_4_bytes (dbfd, index_ptr);
8238 index_ptr += 4;
8239 nr_slots = read_4_bytes (dbfd, index_ptr);
8240 index_ptr += 4;
8241
8242 if (version != 1 && version != 2 && version != 5)
8243 {
8244 error (_("Dwarf Error: unsupported DWP file version (%s)"
8245 " [in module %s]"),
8246 pulongest (version), dwp_file->name);
8247 }
8248 if (nr_slots != (nr_slots & -nr_slots))
8249 {
8250 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
8251 " is not power of 2 [in module %s]"),
8252 pulongest (nr_slots), dwp_file->name);
8253 }
8254
8255 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
8256 htab->version = version;
8257 htab->nr_columns = nr_columns;
8258 htab->nr_units = nr_units;
8259 htab->nr_slots = nr_slots;
8260 htab->hash_table = index_ptr;
8261 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8262
8263 /* Exit early if the table is empty. */
8264 if (nr_slots == 0 || nr_units == 0
8265 || (version == 2 && nr_columns == 0)
8266 || (version == 5 && nr_columns == 0))
8267 {
8268 /* All must be zero. */
8269 if (nr_slots != 0 || nr_units != 0
8270 || (version == 2 && nr_columns != 0)
8271 || (version == 5 && nr_columns != 0))
8272 {
8273 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
8274 " all zero [in modules %s]"),
8275 dwp_file->name);
8276 }
8277 return htab;
8278 }
8279
8280 if (version == 1)
8281 {
8282 htab->section_pool.v1.indices =
8283 htab->unit_table + sizeof (uint32_t) * nr_slots;
8284 /* It's harder to decide whether the section is too small in v1.
8285 V1 is deprecated anyway so we punt. */
8286 }
8287 else if (version == 2)
8288 {
8289 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8290 int *ids = htab->section_pool.v2.section_ids;
8291 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
8292 /* Reverse map for error checking. */
8293 int ids_seen[DW_SECT_MAX + 1];
8294 int i;
8295
8296 if (nr_columns < 2)
8297 {
8298 error (_("Dwarf Error: bad DWP hash table, too few columns"
8299 " in section table [in module %s]"),
8300 dwp_file->name);
8301 }
8302 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
8303 {
8304 error (_("Dwarf Error: bad DWP hash table, too many columns"
8305 " in section table [in module %s]"),
8306 dwp_file->name);
8307 }
8308 memset (ids, 255, sizeof_ids);
8309 memset (ids_seen, 255, sizeof (ids_seen));
8310 for (i = 0; i < nr_columns; ++i)
8311 {
8312 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8313
8314 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
8315 {
8316 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8317 " in section table [in module %s]"),
8318 id, dwp_file->name);
8319 }
8320 if (ids_seen[id] != -1)
8321 {
8322 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8323 " id %d in section table [in module %s]"),
8324 id, dwp_file->name);
8325 }
8326 ids_seen[id] = i;
8327 ids[i] = id;
8328 }
8329 /* Must have exactly one info or types section. */
8330 if (((ids_seen[DW_SECT_INFO] != -1)
8331 + (ids_seen[DW_SECT_TYPES] != -1))
8332 != 1)
8333 {
8334 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8335 " DWO info/types section [in module %s]"),
8336 dwp_file->name);
8337 }
8338 /* Must have an abbrev section. */
8339 if (ids_seen[DW_SECT_ABBREV] == -1)
8340 {
8341 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8342 " section [in module %s]"),
8343 dwp_file->name);
8344 }
8345 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8346 htab->section_pool.v2.sizes =
8347 htab->section_pool.v2.offsets + (sizeof (uint32_t)
8348 * nr_units * nr_columns);
8349 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
8350 * nr_units * nr_columns))
8351 > index_end)
8352 {
8353 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8354 " [in module %s]"),
8355 dwp_file->name);
8356 }
8357 }
8358 else /* version == 5 */
8359 {
8360 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8361 int *ids = htab->section_pool.v5.section_ids;
8362 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
8363 /* Reverse map for error checking. */
8364 int ids_seen[DW_SECT_MAX_V5 + 1];
8365
8366 if (nr_columns < 2)
8367 {
8368 error (_("Dwarf Error: bad DWP hash table, too few columns"
8369 " in section table [in module %s]"),
8370 dwp_file->name);
8371 }
8372 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
8373 {
8374 error (_("Dwarf Error: bad DWP hash table, too many columns"
8375 " in section table [in module %s]"),
8376 dwp_file->name);
8377 }
8378 memset (ids, 255, sizeof_ids);
8379 memset (ids_seen, 255, sizeof (ids_seen));
8380 for (int i = 0; i < nr_columns; ++i)
8381 {
8382 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8383
8384 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
8385 {
8386 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8387 " in section table [in module %s]"),
8388 id, dwp_file->name);
8389 }
8390 if (ids_seen[id] != -1)
8391 {
8392 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8393 " id %d in section table [in module %s]"),
8394 id, dwp_file->name);
8395 }
8396 ids_seen[id] = i;
8397 ids[i] = id;
8398 }
8399 /* Must have seen an info section. */
8400 if (ids_seen[DW_SECT_INFO_V5] == -1)
8401 {
8402 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8403 " DWO info/types section [in module %s]"),
8404 dwp_file->name);
8405 }
8406 /* Must have an abbrev section. */
8407 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
8408 {
8409 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8410 " section [in module %s]"),
8411 dwp_file->name);
8412 }
8413 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8414 htab->section_pool.v5.sizes
8415 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
8416 * nr_units * nr_columns);
8417 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
8418 * nr_units * nr_columns))
8419 > index_end)
8420 {
8421 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8422 " [in module %s]"),
8423 dwp_file->name);
8424 }
8425 }
8426
8427 return htab;
8428 }
8429
8430 /* Update SECTIONS with the data from SECTP.
8431
8432 This function is like the other "locate" section routines, but in
8433 this context the sections to read comes from the DWP V1 hash table,
8434 not the full ELF section table.
8435
8436 The result is non-zero for success, or zero if an error was found. */
8437
8438 static int
8439 locate_v1_virtual_dwo_sections (asection *sectp,
8440 struct virtual_v1_dwo_sections *sections)
8441 {
8442 const struct dwop_section_names *names = &dwop_section_names;
8443
8444 if (names->abbrev_dwo.matches (sectp->name))
8445 {
8446 /* There can be only one. */
8447 if (sections->abbrev.s.section != NULL)
8448 return 0;
8449 sections->abbrev.s.section = sectp;
8450 sections->abbrev.size = bfd_section_size (sectp);
8451 }
8452 else if (names->info_dwo.matches (sectp->name)
8453 || names->types_dwo.matches (sectp->name))
8454 {
8455 /* There can be only one. */
8456 if (sections->info_or_types.s.section != NULL)
8457 return 0;
8458 sections->info_or_types.s.section = sectp;
8459 sections->info_or_types.size = bfd_section_size (sectp);
8460 }
8461 else if (names->line_dwo.matches (sectp->name))
8462 {
8463 /* There can be only one. */
8464 if (sections->line.s.section != NULL)
8465 return 0;
8466 sections->line.s.section = sectp;
8467 sections->line.size = bfd_section_size (sectp);
8468 }
8469 else if (names->loc_dwo.matches (sectp->name))
8470 {
8471 /* There can be only one. */
8472 if (sections->loc.s.section != NULL)
8473 return 0;
8474 sections->loc.s.section = sectp;
8475 sections->loc.size = bfd_section_size (sectp);
8476 }
8477 else if (names->macinfo_dwo.matches (sectp->name))
8478 {
8479 /* There can be only one. */
8480 if (sections->macinfo.s.section != NULL)
8481 return 0;
8482 sections->macinfo.s.section = sectp;
8483 sections->macinfo.size = bfd_section_size (sectp);
8484 }
8485 else if (names->macro_dwo.matches (sectp->name))
8486 {
8487 /* There can be only one. */
8488 if (sections->macro.s.section != NULL)
8489 return 0;
8490 sections->macro.s.section = sectp;
8491 sections->macro.size = bfd_section_size (sectp);
8492 }
8493 else if (names->str_offsets_dwo.matches (sectp->name))
8494 {
8495 /* There can be only one. */
8496 if (sections->str_offsets.s.section != NULL)
8497 return 0;
8498 sections->str_offsets.s.section = sectp;
8499 sections->str_offsets.size = bfd_section_size (sectp);
8500 }
8501 else
8502 {
8503 /* No other kind of section is valid. */
8504 return 0;
8505 }
8506
8507 return 1;
8508 }
8509
8510 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8511 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8512 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8513 This is for DWP version 1 files. */
8514
8515 static struct dwo_unit *
8516 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
8517 struct dwp_file *dwp_file,
8518 uint32_t unit_index,
8519 const char *comp_dir,
8520 ULONGEST signature, int is_debug_types)
8521 {
8522 const struct dwp_hash_table *dwp_htab =
8523 is_debug_types ? dwp_file->tus : dwp_file->cus;
8524 bfd *dbfd = dwp_file->dbfd.get ();
8525 const char *kind = is_debug_types ? "TU" : "CU";
8526 struct dwo_file *dwo_file;
8527 struct dwo_unit *dwo_unit;
8528 struct virtual_v1_dwo_sections sections;
8529 void **dwo_file_slot;
8530 int i;
8531
8532 gdb_assert (dwp_file->version == 1);
8533
8534 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
8535 kind, pulongest (unit_index), hex_string (signature),
8536 dwp_file->name);
8537
8538 /* Fetch the sections of this DWO unit.
8539 Put a limit on the number of sections we look for so that bad data
8540 doesn't cause us to loop forever. */
8541
8542 #define MAX_NR_V1_DWO_SECTIONS \
8543 (1 /* .debug_info or .debug_types */ \
8544 + 1 /* .debug_abbrev */ \
8545 + 1 /* .debug_line */ \
8546 + 1 /* .debug_loc */ \
8547 + 1 /* .debug_str_offsets */ \
8548 + 1 /* .debug_macro or .debug_macinfo */ \
8549 + 1 /* trailing zero */)
8550
8551 memset (&sections, 0, sizeof (sections));
8552
8553 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
8554 {
8555 asection *sectp;
8556 uint32_t section_nr =
8557 read_4_bytes (dbfd,
8558 dwp_htab->section_pool.v1.indices
8559 + (unit_index + i) * sizeof (uint32_t));
8560
8561 if (section_nr == 0)
8562 break;
8563 if (section_nr >= dwp_file->num_sections)
8564 {
8565 error (_("Dwarf Error: bad DWP hash table, section number too large"
8566 " [in module %s]"),
8567 dwp_file->name);
8568 }
8569
8570 sectp = dwp_file->elf_sections[section_nr];
8571 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
8572 {
8573 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8574 " [in module %s]"),
8575 dwp_file->name);
8576 }
8577 }
8578
8579 if (i < 2
8580 || sections.info_or_types.empty ()
8581 || sections.abbrev.empty ())
8582 {
8583 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8584 " [in module %s]"),
8585 dwp_file->name);
8586 }
8587 if (i == MAX_NR_V1_DWO_SECTIONS)
8588 {
8589 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8590 " [in module %s]"),
8591 dwp_file->name);
8592 }
8593
8594 /* It's easier for the rest of the code if we fake a struct dwo_file and
8595 have dwo_unit "live" in that. At least for now.
8596
8597 The DWP file can be made up of a random collection of CUs and TUs.
8598 However, for each CU + set of TUs that came from the same original DWO
8599 file, we can combine them back into a virtual DWO file to save space
8600 (fewer struct dwo_file objects to allocate). Remember that for really
8601 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8602
8603 std::string virtual_dwo_name =
8604 string_printf ("virtual-dwo/%d-%d-%d-%d",
8605 sections.abbrev.get_id (),
8606 sections.line.get_id (),
8607 sections.loc.get_id (),
8608 sections.str_offsets.get_id ());
8609 /* Can we use an existing virtual DWO file? */
8610 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8611 comp_dir);
8612 /* Create one if necessary. */
8613 if (*dwo_file_slot == NULL)
8614 {
8615 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8616 virtual_dwo_name.c_str ());
8617
8618 dwo_file = new struct dwo_file;
8619 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
8620 dwo_file->comp_dir = comp_dir;
8621 dwo_file->sections.abbrev = sections.abbrev;
8622 dwo_file->sections.line = sections.line;
8623 dwo_file->sections.loc = sections.loc;
8624 dwo_file->sections.macinfo = sections.macinfo;
8625 dwo_file->sections.macro = sections.macro;
8626 dwo_file->sections.str_offsets = sections.str_offsets;
8627 /* The "str" section is global to the entire DWP file. */
8628 dwo_file->sections.str = dwp_file->sections.str;
8629 /* The info or types section is assigned below to dwo_unit,
8630 there's no need to record it in dwo_file.
8631 Also, we can't simply record type sections in dwo_file because
8632 we record a pointer into the vector in dwo_unit. As we collect more
8633 types we'll grow the vector and eventually have to reallocate space
8634 for it, invalidating all copies of pointers into the previous
8635 contents. */
8636 *dwo_file_slot = dwo_file;
8637 }
8638 else
8639 {
8640 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8641 virtual_dwo_name.c_str ());
8642
8643 dwo_file = (struct dwo_file *) *dwo_file_slot;
8644 }
8645
8646 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8647 dwo_unit->dwo_file = dwo_file;
8648 dwo_unit->signature = signature;
8649 dwo_unit->section =
8650 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8651 *dwo_unit->section = sections.info_or_types;
8652 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8653
8654 return dwo_unit;
8655 }
8656
8657 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
8658 simplify them. Given a pointer to the containing section SECTION, and
8659 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
8660 virtual section of just that piece. */
8661
8662 static struct dwarf2_section_info
8663 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
8664 struct dwarf2_section_info *section,
8665 bfd_size_type offset, bfd_size_type size)
8666 {
8667 struct dwarf2_section_info result;
8668 asection *sectp;
8669
8670 gdb_assert (section != NULL);
8671 gdb_assert (!section->is_virtual);
8672
8673 memset (&result, 0, sizeof (result));
8674 result.s.containing_section = section;
8675 result.is_virtual = true;
8676
8677 if (size == 0)
8678 return result;
8679
8680 sectp = section->get_bfd_section ();
8681
8682 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
8683 bounds of the real section. This is a pretty-rare event, so just
8684 flag an error (easier) instead of a warning and trying to cope. */
8685 if (sectp == NULL
8686 || offset + size > bfd_section_size (sectp))
8687 {
8688 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
8689 " in section %s [in module %s]"),
8690 sectp ? bfd_section_name (sectp) : "<unknown>",
8691 objfile_name (per_objfile->objfile));
8692 }
8693
8694 result.virtual_offset = offset;
8695 result.size = size;
8696 return result;
8697 }
8698
8699 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8700 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8701 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8702 This is for DWP version 2 files. */
8703
8704 static struct dwo_unit *
8705 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
8706 struct dwp_file *dwp_file,
8707 uint32_t unit_index,
8708 const char *comp_dir,
8709 ULONGEST signature, int is_debug_types)
8710 {
8711 const struct dwp_hash_table *dwp_htab =
8712 is_debug_types ? dwp_file->tus : dwp_file->cus;
8713 bfd *dbfd = dwp_file->dbfd.get ();
8714 const char *kind = is_debug_types ? "TU" : "CU";
8715 struct dwo_file *dwo_file;
8716 struct dwo_unit *dwo_unit;
8717 struct virtual_v2_or_v5_dwo_sections sections;
8718 void **dwo_file_slot;
8719 int i;
8720
8721 gdb_assert (dwp_file->version == 2);
8722
8723 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
8724 kind, pulongest (unit_index), hex_string (signature),
8725 dwp_file->name);
8726
8727 /* Fetch the section offsets of this DWO unit. */
8728
8729 memset (&sections, 0, sizeof (sections));
8730
8731 for (i = 0; i < dwp_htab->nr_columns; ++i)
8732 {
8733 uint32_t offset = read_4_bytes (dbfd,
8734 dwp_htab->section_pool.v2.offsets
8735 + (((unit_index - 1) * dwp_htab->nr_columns
8736 + i)
8737 * sizeof (uint32_t)));
8738 uint32_t size = read_4_bytes (dbfd,
8739 dwp_htab->section_pool.v2.sizes
8740 + (((unit_index - 1) * dwp_htab->nr_columns
8741 + i)
8742 * sizeof (uint32_t)));
8743
8744 switch (dwp_htab->section_pool.v2.section_ids[i])
8745 {
8746 case DW_SECT_INFO:
8747 case DW_SECT_TYPES:
8748 sections.info_or_types_offset = offset;
8749 sections.info_or_types_size = size;
8750 break;
8751 case DW_SECT_ABBREV:
8752 sections.abbrev_offset = offset;
8753 sections.abbrev_size = size;
8754 break;
8755 case DW_SECT_LINE:
8756 sections.line_offset = offset;
8757 sections.line_size = size;
8758 break;
8759 case DW_SECT_LOC:
8760 sections.loc_offset = offset;
8761 sections.loc_size = size;
8762 break;
8763 case DW_SECT_STR_OFFSETS:
8764 sections.str_offsets_offset = offset;
8765 sections.str_offsets_size = size;
8766 break;
8767 case DW_SECT_MACINFO:
8768 sections.macinfo_offset = offset;
8769 sections.macinfo_size = size;
8770 break;
8771 case DW_SECT_MACRO:
8772 sections.macro_offset = offset;
8773 sections.macro_size = size;
8774 break;
8775 }
8776 }
8777
8778 /* It's easier for the rest of the code if we fake a struct dwo_file and
8779 have dwo_unit "live" in that. At least for now.
8780
8781 The DWP file can be made up of a random collection of CUs and TUs.
8782 However, for each CU + set of TUs that came from the same original DWO
8783 file, we can combine them back into a virtual DWO file to save space
8784 (fewer struct dwo_file objects to allocate). Remember that for really
8785 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8786
8787 std::string virtual_dwo_name =
8788 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
8789 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8790 (long) (sections.line_size ? sections.line_offset : 0),
8791 (long) (sections.loc_size ? sections.loc_offset : 0),
8792 (long) (sections.str_offsets_size
8793 ? sections.str_offsets_offset : 0));
8794 /* Can we use an existing virtual DWO file? */
8795 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8796 comp_dir);
8797 /* Create one if necessary. */
8798 if (*dwo_file_slot == NULL)
8799 {
8800 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8801 virtual_dwo_name.c_str ());
8802
8803 dwo_file = new struct dwo_file;
8804 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
8805 dwo_file->comp_dir = comp_dir;
8806 dwo_file->sections.abbrev =
8807 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
8808 sections.abbrev_offset,
8809 sections.abbrev_size);
8810 dwo_file->sections.line =
8811 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
8812 sections.line_offset,
8813 sections.line_size);
8814 dwo_file->sections.loc =
8815 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
8816 sections.loc_offset, sections.loc_size);
8817 dwo_file->sections.macinfo =
8818 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
8819 sections.macinfo_offset,
8820 sections.macinfo_size);
8821 dwo_file->sections.macro =
8822 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
8823 sections.macro_offset,
8824 sections.macro_size);
8825 dwo_file->sections.str_offsets =
8826 create_dwp_v2_or_v5_section (per_objfile,
8827 &dwp_file->sections.str_offsets,
8828 sections.str_offsets_offset,
8829 sections.str_offsets_size);
8830 /* The "str" section is global to the entire DWP file. */
8831 dwo_file->sections.str = dwp_file->sections.str;
8832 /* The info or types section is assigned below to dwo_unit,
8833 there's no need to record it in dwo_file.
8834 Also, we can't simply record type sections in dwo_file because
8835 we record a pointer into the vector in dwo_unit. As we collect more
8836 types we'll grow the vector and eventually have to reallocate space
8837 for it, invalidating all copies of pointers into the previous
8838 contents. */
8839 *dwo_file_slot = dwo_file;
8840 }
8841 else
8842 {
8843 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8844 virtual_dwo_name.c_str ());
8845
8846 dwo_file = (struct dwo_file *) *dwo_file_slot;
8847 }
8848
8849 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8850 dwo_unit->dwo_file = dwo_file;
8851 dwo_unit->signature = signature;
8852 dwo_unit->section =
8853 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8854 *dwo_unit->section = create_dwp_v2_or_v5_section
8855 (per_objfile,
8856 is_debug_types
8857 ? &dwp_file->sections.types
8858 : &dwp_file->sections.info,
8859 sections.info_or_types_offset,
8860 sections.info_or_types_size);
8861 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8862
8863 return dwo_unit;
8864 }
8865
8866 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8867 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8868 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8869 This is for DWP version 5 files. */
8870
8871 static struct dwo_unit *
8872 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
8873 struct dwp_file *dwp_file,
8874 uint32_t unit_index,
8875 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 const char *kind = is_debug_types ? "TU" : "CU";
8882 struct dwo_file *dwo_file;
8883 struct dwo_unit *dwo_unit;
8884 struct virtual_v2_or_v5_dwo_sections sections {};
8885 void **dwo_file_slot;
8886
8887 gdb_assert (dwp_file->version == 5);
8888
8889 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
8890 kind, pulongest (unit_index), hex_string (signature),
8891 dwp_file->name);
8892
8893 /* Fetch the section offsets of this DWO unit. */
8894
8895 /* memset (&sections, 0, sizeof (sections)); */
8896
8897 for (int i = 0; i < dwp_htab->nr_columns; ++i)
8898 {
8899 uint32_t offset = read_4_bytes (dbfd,
8900 dwp_htab->section_pool.v5.offsets
8901 + (((unit_index - 1)
8902 * dwp_htab->nr_columns
8903 + i)
8904 * sizeof (uint32_t)));
8905 uint32_t size = read_4_bytes (dbfd,
8906 dwp_htab->section_pool.v5.sizes
8907 + (((unit_index - 1) * dwp_htab->nr_columns
8908 + i)
8909 * sizeof (uint32_t)));
8910
8911 switch (dwp_htab->section_pool.v5.section_ids[i])
8912 {
8913 case DW_SECT_ABBREV_V5:
8914 sections.abbrev_offset = offset;
8915 sections.abbrev_size = size;
8916 break;
8917 case DW_SECT_INFO_V5:
8918 sections.info_or_types_offset = offset;
8919 sections.info_or_types_size = size;
8920 break;
8921 case DW_SECT_LINE_V5:
8922 sections.line_offset = offset;
8923 sections.line_size = size;
8924 break;
8925 case DW_SECT_LOCLISTS_V5:
8926 sections.loclists_offset = offset;
8927 sections.loclists_size = size;
8928 break;
8929 case DW_SECT_MACRO_V5:
8930 sections.macro_offset = offset;
8931 sections.macro_size = size;
8932 break;
8933 case DW_SECT_RNGLISTS_V5:
8934 sections.rnglists_offset = offset;
8935 sections.rnglists_size = size;
8936 break;
8937 case DW_SECT_STR_OFFSETS_V5:
8938 sections.str_offsets_offset = offset;
8939 sections.str_offsets_size = size;
8940 break;
8941 case DW_SECT_RESERVED_V5:
8942 default:
8943 break;
8944 }
8945 }
8946
8947 /* It's easier for the rest of the code if we fake a struct dwo_file and
8948 have dwo_unit "live" in that. At least for now.
8949
8950 The DWP file can be made up of a random collection of CUs and TUs.
8951 However, for each CU + set of TUs that came from the same original DWO
8952 file, we can combine them back into a virtual DWO file to save space
8953 (fewer struct dwo_file objects to allocate). Remember that for really
8954 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8955
8956 std::string virtual_dwo_name =
8957 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
8958 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8959 (long) (sections.line_size ? sections.line_offset : 0),
8960 (long) (sections.loclists_size ? sections.loclists_offset : 0),
8961 (long) (sections.str_offsets_size
8962 ? sections.str_offsets_offset : 0),
8963 (long) (sections.macro_size ? sections.macro_offset : 0),
8964 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
8965 /* Can we use an existing virtual DWO file? */
8966 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
8967 virtual_dwo_name.c_str (),
8968 comp_dir);
8969 /* Create one if necessary. */
8970 if (*dwo_file_slot == NULL)
8971 {
8972 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8973 virtual_dwo_name.c_str ());
8974
8975 dwo_file = new struct dwo_file;
8976 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
8977 dwo_file->comp_dir = comp_dir;
8978 dwo_file->sections.abbrev =
8979 create_dwp_v2_or_v5_section (per_objfile,
8980 &dwp_file->sections.abbrev,
8981 sections.abbrev_offset,
8982 sections.abbrev_size);
8983 dwo_file->sections.line =
8984 create_dwp_v2_or_v5_section (per_objfile,
8985 &dwp_file->sections.line,
8986 sections.line_offset, sections.line_size);
8987 dwo_file->sections.macro =
8988 create_dwp_v2_or_v5_section (per_objfile,
8989 &dwp_file->sections.macro,
8990 sections.macro_offset,
8991 sections.macro_size);
8992 dwo_file->sections.loclists =
8993 create_dwp_v2_or_v5_section (per_objfile,
8994 &dwp_file->sections.loclists,
8995 sections.loclists_offset,
8996 sections.loclists_size);
8997 dwo_file->sections.rnglists =
8998 create_dwp_v2_or_v5_section (per_objfile,
8999 &dwp_file->sections.rnglists,
9000 sections.rnglists_offset,
9001 sections.rnglists_size);
9002 dwo_file->sections.str_offsets =
9003 create_dwp_v2_or_v5_section (per_objfile,
9004 &dwp_file->sections.str_offsets,
9005 sections.str_offsets_offset,
9006 sections.str_offsets_size);
9007 /* The "str" section is global to the entire DWP file. */
9008 dwo_file->sections.str = dwp_file->sections.str;
9009 /* The info or types section is assigned below to dwo_unit,
9010 there's no need to record it in dwo_file.
9011 Also, we can't simply record type sections in dwo_file because
9012 we record a pointer into the vector in dwo_unit. As we collect more
9013 types we'll grow the vector and eventually have to reallocate space
9014 for it, invalidating all copies of pointers into the previous
9015 contents. */
9016 *dwo_file_slot = dwo_file;
9017 }
9018 else
9019 {
9020 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
9021 virtual_dwo_name.c_str ());
9022
9023 dwo_file = (struct dwo_file *) *dwo_file_slot;
9024 }
9025
9026 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
9027 dwo_unit->dwo_file = dwo_file;
9028 dwo_unit->signature = signature;
9029 dwo_unit->section
9030 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
9031 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
9032 &dwp_file->sections.info,
9033 sections.info_or_types_offset,
9034 sections.info_or_types_size);
9035 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
9036
9037 return dwo_unit;
9038 }
9039
9040 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
9041 Returns NULL if the signature isn't found. */
9042
9043 static struct dwo_unit *
9044 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
9045 struct dwp_file *dwp_file, const char *comp_dir,
9046 ULONGEST signature, int is_debug_types)
9047 {
9048 const struct dwp_hash_table *dwp_htab =
9049 is_debug_types ? dwp_file->tus : dwp_file->cus;
9050 bfd *dbfd = dwp_file->dbfd.get ();
9051 uint32_t mask = dwp_htab->nr_slots - 1;
9052 uint32_t hash = signature & mask;
9053 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9054 unsigned int i;
9055 void **slot;
9056 struct dwo_unit find_dwo_cu;
9057
9058 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9059 find_dwo_cu.signature = signature;
9060 slot = htab_find_slot (is_debug_types
9061 ? dwp_file->loaded_tus.get ()
9062 : dwp_file->loaded_cus.get (),
9063 &find_dwo_cu, INSERT);
9064
9065 if (*slot != NULL)
9066 return (struct dwo_unit *) *slot;
9067
9068 /* Use a for loop so that we don't loop forever on bad debug info. */
9069 for (i = 0; i < dwp_htab->nr_slots; ++i)
9070 {
9071 ULONGEST signature_in_table;
9072
9073 signature_in_table =
9074 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
9075 if (signature_in_table == signature)
9076 {
9077 uint32_t unit_index =
9078 read_4_bytes (dbfd,
9079 dwp_htab->unit_table + hash * sizeof (uint32_t));
9080
9081 if (dwp_file->version == 1)
9082 {
9083 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
9084 unit_index, comp_dir,
9085 signature, is_debug_types);
9086 }
9087 else if (dwp_file->version == 2)
9088 {
9089 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
9090 unit_index, comp_dir,
9091 signature, is_debug_types);
9092 }
9093 else /* version == 5 */
9094 {
9095 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
9096 unit_index, comp_dir,
9097 signature, is_debug_types);
9098 }
9099 return (struct dwo_unit *) *slot;
9100 }
9101 if (signature_in_table == 0)
9102 return NULL;
9103 hash = (hash + hash2) & mask;
9104 }
9105
9106 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9107 " [in module %s]"),
9108 dwp_file->name);
9109 }
9110
9111 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9112 Open the file specified by FILE_NAME and hand it off to BFD for
9113 preliminary analysis. Return a newly initialized bfd *, which
9114 includes a canonicalized copy of FILE_NAME.
9115 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9116 SEARCH_CWD is true if the current directory is to be searched.
9117 It will be searched before debug-file-directory.
9118 If successful, the file is added to the bfd include table of the
9119 objfile's bfd (see gdb_bfd_record_inclusion).
9120 If unable to find/open the file, return NULL.
9121 NOTE: This function is derived from symfile_bfd_open. */
9122
9123 static gdb_bfd_ref_ptr
9124 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
9125 const char *file_name, int is_dwp, int search_cwd)
9126 {
9127 int desc;
9128 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9129 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9130 to debug_file_directory. */
9131 const char *search_path;
9132 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9133
9134 gdb::unique_xmalloc_ptr<char> search_path_holder;
9135 if (search_cwd)
9136 {
9137 if (!debug_file_directory.empty ())
9138 {
9139 search_path_holder.reset (concat (".", dirname_separator_string,
9140 debug_file_directory.c_str (),
9141 (char *) NULL));
9142 search_path = search_path_holder.get ();
9143 }
9144 else
9145 search_path = ".";
9146 }
9147 else
9148 search_path = debug_file_directory.c_str ();
9149
9150 /* Add the path for the executable binary to the list of search paths. */
9151 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
9152 search_path_holder.reset (concat (objfile_dir.c_str (),
9153 dirname_separator_string,
9154 search_path, nullptr));
9155 search_path = search_path_holder.get ();
9156
9157 openp_flags flags = OPF_RETURN_REALPATH;
9158 if (is_dwp)
9159 flags |= OPF_SEARCH_IN_PATH;
9160
9161 gdb::unique_xmalloc_ptr<char> absolute_name;
9162 desc = openp (search_path, flags, file_name,
9163 O_RDONLY | O_BINARY, &absolute_name);
9164 if (desc < 0)
9165 return NULL;
9166
9167 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
9168 gnutarget, desc));
9169 if (sym_bfd == NULL)
9170 return NULL;
9171 bfd_set_cacheable (sym_bfd.get (), 1);
9172
9173 if (!bfd_check_format (sym_bfd.get (), bfd_object))
9174 return NULL;
9175
9176 /* Success. Record the bfd as having been included by the objfile's bfd.
9177 This is important because things like demangled_names_hash lives in the
9178 objfile's per_bfd space and may have references to things like symbol
9179 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
9180 gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
9181
9182 return sym_bfd;
9183 }
9184
9185 /* Try to open DWO file FILE_NAME.
9186 COMP_DIR is the DW_AT_comp_dir attribute.
9187 The result is the bfd handle of the file.
9188 If there is a problem finding or opening the file, return NULL.
9189 Upon success, the canonicalized path of the file is stored in the bfd,
9190 same as symfile_bfd_open. */
9191
9192 static gdb_bfd_ref_ptr
9193 open_dwo_file (dwarf2_per_objfile *per_objfile,
9194 const char *file_name, const char *comp_dir)
9195 {
9196 if (IS_ABSOLUTE_PATH (file_name))
9197 return try_open_dwop_file (per_objfile, file_name,
9198 0 /*is_dwp*/, 0 /*search_cwd*/);
9199
9200 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9201
9202 if (comp_dir != NULL)
9203 {
9204 std::string path_to_try = path_join (comp_dir, file_name);
9205
9206 /* NOTE: If comp_dir is a relative path, this will also try the
9207 search path, which seems useful. */
9208 gdb_bfd_ref_ptr abfd (try_open_dwop_file
9209 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
9210
9211 if (abfd != NULL)
9212 return abfd;
9213 }
9214
9215 /* That didn't work, try debug-file-directory, which, despite its name,
9216 is a list of paths. */
9217
9218 if (debug_file_directory.empty ())
9219 return NULL;
9220
9221 return try_open_dwop_file (per_objfile, file_name,
9222 0 /*is_dwp*/, 1 /*search_cwd*/);
9223 }
9224
9225 /* This function is mapped across the sections and remembers the offset and
9226 size of each of the DWO debugging sections we are interested in. */
9227
9228 static void
9229 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
9230 asection *sectp, dwo_sections *dwo_sections)
9231 {
9232 const struct dwop_section_names *names = &dwop_section_names;
9233
9234 struct dwarf2_section_info *dw_sect = nullptr;
9235
9236 if (names->abbrev_dwo.matches (sectp->name))
9237 dw_sect = &dwo_sections->abbrev;
9238 else if (names->info_dwo.matches (sectp->name))
9239 dw_sect = &dwo_sections->info;
9240 else if (names->line_dwo.matches (sectp->name))
9241 dw_sect = &dwo_sections->line;
9242 else if (names->loc_dwo.matches (sectp->name))
9243 dw_sect = &dwo_sections->loc;
9244 else if (names->loclists_dwo.matches (sectp->name))
9245 dw_sect = &dwo_sections->loclists;
9246 else if (names->macinfo_dwo.matches (sectp->name))
9247 dw_sect = &dwo_sections->macinfo;
9248 else if (names->macro_dwo.matches (sectp->name))
9249 dw_sect = &dwo_sections->macro;
9250 else if (names->rnglists_dwo.matches (sectp->name))
9251 dw_sect = &dwo_sections->rnglists;
9252 else if (names->str_dwo.matches (sectp->name))
9253 dw_sect = &dwo_sections->str;
9254 else if (names->str_offsets_dwo.matches (sectp->name))
9255 dw_sect = &dwo_sections->str_offsets;
9256 else if (names->types_dwo.matches (sectp->name))
9257 {
9258 struct dwarf2_section_info type_section;
9259
9260 memset (&type_section, 0, sizeof (type_section));
9261 dwo_sections->types.push_back (type_section);
9262 dw_sect = &dwo_sections->types.back ();
9263 }
9264
9265 if (dw_sect != nullptr)
9266 {
9267 dw_sect->s.section = sectp;
9268 dw_sect->size = bfd_section_size (sectp);
9269 dw_sect->read (objfile);
9270 }
9271 }
9272
9273 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9274 by PER_CU. This is for the non-DWP case.
9275 The result is NULL if DWO_NAME can't be found. */
9276
9277 static struct dwo_file *
9278 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
9279 const char *comp_dir)
9280 {
9281 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9282
9283 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
9284 if (dbfd == NULL)
9285 {
9286 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
9287
9288 return NULL;
9289 }
9290
9291 dwo_file_up dwo_file (new struct dwo_file);
9292 dwo_file->dwo_name = dwo_name;
9293 dwo_file->comp_dir = comp_dir;
9294 dwo_file->dbfd = std::move (dbfd);
9295
9296 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
9297 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
9298 sec, &dwo_file->sections);
9299
9300 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
9301 dwo_file->cus);
9302
9303 if (cu->per_cu->version () < 5)
9304 {
9305 create_debug_types_hash_table (per_objfile, dwo_file.get (),
9306 dwo_file->sections.types, dwo_file->tus);
9307 }
9308 else
9309 {
9310 create_debug_type_hash_table (per_objfile, dwo_file.get (),
9311 &dwo_file->sections.info, dwo_file->tus,
9312 rcuh_kind::COMPILE);
9313 }
9314
9315 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
9316
9317 return dwo_file.release ();
9318 }
9319
9320 /* This function is mapped across the sections and remembers the offset and
9321 size of each of the DWP debugging sections common to version 1 and 2 that
9322 we are interested in. */
9323
9324 static void
9325 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
9326 asection *sectp, dwp_file *dwp_file)
9327 {
9328 const struct dwop_section_names *names = &dwop_section_names;
9329 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9330
9331 /* Record the ELF section number for later lookup: this is what the
9332 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9333 gdb_assert (elf_section_nr < dwp_file->num_sections);
9334 dwp_file->elf_sections[elf_section_nr] = sectp;
9335
9336 /* Look for specific sections that we need. */
9337 struct dwarf2_section_info *dw_sect = nullptr;
9338 if (names->str_dwo.matches (sectp->name))
9339 dw_sect = &dwp_file->sections.str;
9340 else if (names->cu_index.matches (sectp->name))
9341 dw_sect = &dwp_file->sections.cu_index;
9342 else if (names->tu_index.matches (sectp->name))
9343 dw_sect = &dwp_file->sections.tu_index;
9344
9345 if (dw_sect != nullptr)
9346 {
9347 dw_sect->s.section = sectp;
9348 dw_sect->size = bfd_section_size (sectp);
9349 dw_sect->read (objfile);
9350 }
9351 }
9352
9353 /* This function is mapped across the sections and remembers the offset and
9354 size of each of the DWP version 2 debugging sections that we are interested
9355 in. This is split into a separate function because we don't know if we
9356 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9357
9358 static void
9359 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
9360 asection *sectp, void *dwp_file_ptr)
9361 {
9362 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9363 const struct dwop_section_names *names = &dwop_section_names;
9364 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9365
9366 /* Record the ELF section number for later lookup: this is what the
9367 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9368 gdb_assert (elf_section_nr < dwp_file->num_sections);
9369 dwp_file->elf_sections[elf_section_nr] = sectp;
9370
9371 /* Look for specific sections that we need. */
9372 struct dwarf2_section_info *dw_sect = nullptr;
9373 if (names->abbrev_dwo.matches (sectp->name))
9374 dw_sect = &dwp_file->sections.abbrev;
9375 else if (names->info_dwo.matches (sectp->name))
9376 dw_sect = &dwp_file->sections.info;
9377 else if (names->line_dwo.matches (sectp->name))
9378 dw_sect = &dwp_file->sections.line;
9379 else if (names->loc_dwo.matches (sectp->name))
9380 dw_sect = &dwp_file->sections.loc;
9381 else if (names->macinfo_dwo.matches (sectp->name))
9382 dw_sect = &dwp_file->sections.macinfo;
9383 else if (names->macro_dwo.matches (sectp->name))
9384 dw_sect = &dwp_file->sections.macro;
9385 else if (names->str_offsets_dwo.matches (sectp->name))
9386 dw_sect = &dwp_file->sections.str_offsets;
9387 else if (names->types_dwo.matches (sectp->name))
9388 dw_sect = &dwp_file->sections.types;
9389
9390 if (dw_sect != nullptr)
9391 {
9392 dw_sect->s.section = sectp;
9393 dw_sect->size = bfd_section_size (sectp);
9394 dw_sect->read (objfile);
9395 }
9396 }
9397
9398 /* This function is mapped across the sections and remembers the offset and
9399 size of each of the DWP version 5 debugging sections that we are interested
9400 in. This is split into a separate function because we don't know if we
9401 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9402
9403 static void
9404 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
9405 asection *sectp, void *dwp_file_ptr)
9406 {
9407 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9408 const struct dwop_section_names *names = &dwop_section_names;
9409 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9410
9411 /* Record the ELF section number for later lookup: this is what the
9412 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9413 gdb_assert (elf_section_nr < dwp_file->num_sections);
9414 dwp_file->elf_sections[elf_section_nr] = sectp;
9415
9416 /* Look for specific sections that we need. */
9417 struct dwarf2_section_info *dw_sect = nullptr;
9418 if (names->abbrev_dwo.matches (sectp->name))
9419 dw_sect = &dwp_file->sections.abbrev;
9420 else if (names->info_dwo.matches (sectp->name))
9421 dw_sect = &dwp_file->sections.info;
9422 else if (names->line_dwo.matches (sectp->name))
9423 dw_sect = &dwp_file->sections.line;
9424 else if (names->loclists_dwo.matches (sectp->name))
9425 dw_sect = &dwp_file->sections.loclists;
9426 else if (names->macro_dwo.matches (sectp->name))
9427 dw_sect = &dwp_file->sections.macro;
9428 else if (names->rnglists_dwo.matches (sectp->name))
9429 dw_sect = &dwp_file->sections.rnglists;
9430 else if (names->str_offsets_dwo.matches (sectp->name))
9431 dw_sect = &dwp_file->sections.str_offsets;
9432
9433 if (dw_sect != nullptr)
9434 {
9435 dw_sect->s.section = sectp;
9436 dw_sect->size = bfd_section_size (sectp);
9437 dw_sect->read (objfile);
9438 }
9439 }
9440
9441 /* Hash function for dwp_file loaded CUs/TUs. */
9442
9443 static hashval_t
9444 hash_dwp_loaded_cutus (const void *item)
9445 {
9446 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9447
9448 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9449 return dwo_unit->signature;
9450 }
9451
9452 /* Equality function for dwp_file loaded CUs/TUs. */
9453
9454 static int
9455 eq_dwp_loaded_cutus (const void *a, const void *b)
9456 {
9457 const struct dwo_unit *dua = (const struct dwo_unit *) a;
9458 const struct dwo_unit *dub = (const struct dwo_unit *) b;
9459
9460 return dua->signature == dub->signature;
9461 }
9462
9463 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9464
9465 static htab_up
9466 allocate_dwp_loaded_cutus_table ()
9467 {
9468 return htab_up (htab_create_alloc (3,
9469 hash_dwp_loaded_cutus,
9470 eq_dwp_loaded_cutus,
9471 NULL, xcalloc, xfree));
9472 }
9473
9474 /* Try to open DWP file FILE_NAME.
9475 The result is the bfd handle of the file.
9476 If there is a problem finding or opening the file, return NULL.
9477 Upon success, the canonicalized path of the file is stored in the bfd,
9478 same as symfile_bfd_open. */
9479
9480 static gdb_bfd_ref_ptr
9481 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
9482 {
9483 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
9484 1 /*is_dwp*/,
9485 1 /*search_cwd*/));
9486 if (abfd != NULL)
9487 return abfd;
9488
9489 /* Work around upstream bug 15652.
9490 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9491 [Whether that's a "bug" is debatable, but it is getting in our way.]
9492 We have no real idea where the dwp file is, because gdb's realpath-ing
9493 of the executable's path may have discarded the needed info.
9494 [IWBN if the dwp file name was recorded in the executable, akin to
9495 .gnu_debuglink, but that doesn't exist yet.]
9496 Strip the directory from FILE_NAME and search again. */
9497 if (!debug_file_directory.empty ())
9498 {
9499 /* Don't implicitly search the current directory here.
9500 If the user wants to search "." to handle this case,
9501 it must be added to debug-file-directory. */
9502 return try_open_dwop_file (per_objfile, lbasename (file_name),
9503 1 /*is_dwp*/,
9504 0 /*search_cwd*/);
9505 }
9506
9507 return NULL;
9508 }
9509
9510 /* Initialize the use of the DWP file for the current objfile.
9511 By convention the name of the DWP file is ${objfile}.dwp.
9512 The result is NULL if it can't be found. */
9513
9514 static std::unique_ptr<struct dwp_file>
9515 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
9516 {
9517 struct objfile *objfile = per_objfile->objfile;
9518
9519 /* Try to find first .dwp for the binary file before any symbolic links
9520 resolving. */
9521
9522 /* If the objfile is a debug file, find the name of the real binary
9523 file and get the name of dwp file from there. */
9524 std::string dwp_name;
9525 if (objfile->separate_debug_objfile_backlink != NULL)
9526 {
9527 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
9528 const char *backlink_basename = lbasename (backlink->original_name);
9529
9530 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
9531 }
9532 else
9533 dwp_name = objfile->original_name;
9534
9535 dwp_name += ".dwp";
9536
9537 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
9538 if (dbfd == NULL
9539 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
9540 {
9541 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9542 dwp_name = objfile_name (objfile);
9543 dwp_name += ".dwp";
9544 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
9545 }
9546
9547 if (dbfd == NULL)
9548 {
9549 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
9550
9551 return std::unique_ptr<dwp_file> ();
9552 }
9553
9554 const char *name = bfd_get_filename (dbfd.get ());
9555 std::unique_ptr<struct dwp_file> dwp_file
9556 (new struct dwp_file (name, std::move (dbfd)));
9557
9558 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
9559 dwp_file->elf_sections =
9560 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
9561 dwp_file->num_sections, asection *);
9562
9563 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9564 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9565 dwp_file.get ());
9566
9567 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
9568
9569 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
9570
9571 /* The DWP file version is stored in the hash table. Oh well. */
9572 if (dwp_file->cus && dwp_file->tus
9573 && dwp_file->cus->version != dwp_file->tus->version)
9574 {
9575 /* Technically speaking, we should try to limp along, but this is
9576 pretty bizarre. We use pulongest here because that's the established
9577 portability solution (e.g, we cannot use %u for uint32_t). */
9578 error (_("Dwarf Error: DWP file CU version %s doesn't match"
9579 " TU version %s [in DWP file %s]"),
9580 pulongest (dwp_file->cus->version),
9581 pulongest (dwp_file->tus->version), dwp_name.c_str ());
9582 }
9583
9584 if (dwp_file->cus)
9585 dwp_file->version = dwp_file->cus->version;
9586 else if (dwp_file->tus)
9587 dwp_file->version = dwp_file->tus->version;
9588 else
9589 dwp_file->version = 2;
9590
9591 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9592 {
9593 if (dwp_file->version == 2)
9594 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9595 dwp_file.get ());
9596 else
9597 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9598 dwp_file.get ());
9599 }
9600
9601 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
9602 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
9603
9604 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
9605 dwarf_read_debug_printf (" %s CUs, %s TUs",
9606 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9607 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9608
9609 return dwp_file;
9610 }
9611
9612 /* Wrapper around open_and_init_dwp_file, only open it once. */
9613
9614 static struct dwp_file *
9615 get_dwp_file (dwarf2_per_objfile *per_objfile)
9616 {
9617 if (!per_objfile->per_bfd->dwp_checked)
9618 {
9619 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
9620 per_objfile->per_bfd->dwp_checked = 1;
9621 }
9622 return per_objfile->per_bfd->dwp_file.get ();
9623 }
9624
9625 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9626 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9627 or in the DWP file for the objfile, referenced by THIS_UNIT.
9628 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9629 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9630
9631 This is called, for example, when wanting to read a variable with a
9632 complex location. Therefore we don't want to do file i/o for every call.
9633 Therefore we don't want to look for a DWO file on every call.
9634 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9635 then we check if we've already seen DWO_NAME, and only THEN do we check
9636 for a DWO file.
9637
9638 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9639 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9640
9641 static struct dwo_unit *
9642 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9643 ULONGEST signature, int is_debug_types)
9644 {
9645 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9646 struct objfile *objfile = per_objfile->objfile;
9647 const char *kind = is_debug_types ? "TU" : "CU";
9648 void **dwo_file_slot;
9649 struct dwo_file *dwo_file;
9650 struct dwp_file *dwp_file;
9651
9652 /* First see if there's a DWP file.
9653 If we have a DWP file but didn't find the DWO inside it, don't
9654 look for the original DWO file. It makes gdb behave differently
9655 depending on whether one is debugging in the build tree. */
9656
9657 dwp_file = get_dwp_file (per_objfile);
9658 if (dwp_file != NULL)
9659 {
9660 const struct dwp_hash_table *dwp_htab =
9661 is_debug_types ? dwp_file->tus : dwp_file->cus;
9662
9663 if (dwp_htab != NULL)
9664 {
9665 struct dwo_unit *dwo_cutu =
9666 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
9667 is_debug_types);
9668
9669 if (dwo_cutu != NULL)
9670 {
9671 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
9672 kind, hex_string (signature),
9673 host_address_to_string (dwo_cutu));
9674
9675 return dwo_cutu;
9676 }
9677 }
9678 }
9679 else
9680 {
9681 /* No DWP file, look for the DWO file. */
9682
9683 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
9684 if (*dwo_file_slot == NULL)
9685 {
9686 /* Read in the file and build a table of the CUs/TUs it contains. */
9687 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
9688 }
9689 /* NOTE: This will be NULL if unable to open the file. */
9690 dwo_file = (struct dwo_file *) *dwo_file_slot;
9691
9692 if (dwo_file != NULL)
9693 {
9694 struct dwo_unit *dwo_cutu = NULL;
9695
9696 if (is_debug_types && dwo_file->tus)
9697 {
9698 struct dwo_unit find_dwo_cutu;
9699
9700 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9701 find_dwo_cutu.signature = signature;
9702 dwo_cutu
9703 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
9704 &find_dwo_cutu);
9705 }
9706 else if (!is_debug_types && dwo_file->cus)
9707 {
9708 struct dwo_unit find_dwo_cutu;
9709
9710 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9711 find_dwo_cutu.signature = signature;
9712 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
9713 &find_dwo_cutu);
9714 }
9715
9716 if (dwo_cutu != NULL)
9717 {
9718 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
9719 kind, dwo_name, hex_string (signature),
9720 host_address_to_string (dwo_cutu));
9721
9722 return dwo_cutu;
9723 }
9724 }
9725 }
9726
9727 /* We didn't find it. This could mean a dwo_id mismatch, or
9728 someone deleted the DWO/DWP file, or the search path isn't set up
9729 correctly to find the file. */
9730
9731 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
9732 kind, dwo_name, hex_string (signature));
9733
9734 /* This is a warning and not a complaint because it can be caused by
9735 pilot error (e.g., user accidentally deleting the DWO). */
9736 {
9737 /* Print the name of the DWP file if we looked there, helps the user
9738 better diagnose the problem. */
9739 std::string dwp_text;
9740
9741 if (dwp_file != NULL)
9742 dwp_text = string_printf (" [in DWP file %s]",
9743 lbasename (dwp_file->name));
9744
9745 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
9746 " [in module %s]"),
9747 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
9748 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
9749 }
9750 return NULL;
9751 }
9752
9753 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9754 See lookup_dwo_cutu_unit for details. */
9755
9756 static struct dwo_unit *
9757 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9758 ULONGEST signature)
9759 {
9760 gdb_assert (!cu->per_cu->is_debug_types);
9761
9762 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
9763 }
9764
9765 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9766 See lookup_dwo_cutu_unit for details. */
9767
9768 static struct dwo_unit *
9769 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
9770 {
9771 gdb_assert (cu->per_cu->is_debug_types);
9772
9773 signatured_type *sig_type = (signatured_type *) cu->per_cu;
9774
9775 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
9776 }
9777
9778 /* Traversal function for queue_and_load_all_dwo_tus. */
9779
9780 static int
9781 queue_and_load_dwo_tu (void **slot, void *info)
9782 {
9783 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9784 dwarf2_cu *cu = (dwarf2_cu *) info;
9785 ULONGEST signature = dwo_unit->signature;
9786 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
9787
9788 if (sig_type != NULL)
9789 {
9790 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9791 a real dependency of PER_CU on SIG_TYPE. That is detected later
9792 while processing PER_CU. */
9793 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
9794 cu->lang ()))
9795 load_full_type_unit (sig_type, cu->per_objfile);
9796 cu->per_cu->imported_symtabs_push (sig_type);
9797 }
9798
9799 return 1;
9800 }
9801
9802 /* Queue all TUs contained in the DWO of CU to be read in.
9803 The DWO may have the only definition of the type, though it may not be
9804 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9805 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9806
9807 static void
9808 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
9809 {
9810 struct dwo_unit *dwo_unit;
9811 struct dwo_file *dwo_file;
9812
9813 gdb_assert (cu != nullptr);
9814 gdb_assert (!cu->per_cu->is_debug_types);
9815 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
9816
9817 dwo_unit = cu->dwo_unit;
9818 gdb_assert (dwo_unit != NULL);
9819
9820 dwo_file = dwo_unit->dwo_file;
9821 if (dwo_file->tus != NULL)
9822 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
9823 }
9824
9825 /* Read in various DIEs. */
9826
9827 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9828 Inherit only the children of the DW_AT_abstract_origin DIE not being
9829 already referenced by DW_AT_abstract_origin from the children of the
9830 current DIE. */
9831
9832 static void
9833 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9834 {
9835 attribute *attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9836 if (attr == nullptr)
9837 return;
9838
9839 /* Note that following die references may follow to a die in a
9840 different CU. */
9841 dwarf2_cu *origin_cu = cu;
9842
9843 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9844 die_info *origin_die = follow_die_ref (die, attr, &origin_cu);
9845
9846 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9847 symbols in. */
9848 struct pending **origin_previous_list_in_scope = origin_cu->list_in_scope;
9849 origin_cu->list_in_scope = cu->list_in_scope;
9850
9851 if (die->tag != origin_die->tag
9852 && !(die->tag == DW_TAG_inlined_subroutine
9853 && origin_die->tag == DW_TAG_subprogram))
9854 complaint (_("DIE %s and its abstract origin %s have different tags"),
9855 sect_offset_str (die->sect_off),
9856 sect_offset_str (origin_die->sect_off));
9857
9858 /* Find if the concrete and abstract trees are structurally the
9859 same. This is a shallow traversal and it is not bullet-proof;
9860 the compiler can trick the debugger into believing that the trees
9861 are isomorphic, whereas they actually are not. However, the
9862 likelyhood of this happening is pretty low, and a full-fledged
9863 check would be an overkill. */
9864 bool are_isomorphic = true;
9865 die_info *concrete_child = die->child;
9866 die_info *abstract_child = origin_die->child;
9867 while (concrete_child != nullptr || abstract_child != nullptr)
9868 {
9869 if (concrete_child == nullptr
9870 || abstract_child == nullptr
9871 || concrete_child->tag != abstract_child->tag)
9872 {
9873 are_isomorphic = false;
9874 break;
9875 }
9876
9877 concrete_child = concrete_child->sibling;
9878 abstract_child = abstract_child->sibling;
9879 }
9880
9881 /* Walk the origin's children in parallel to the concrete children.
9882 This helps match an origin child in case the debug info misses
9883 DW_AT_abstract_origin attributes. Keep in mind that the abstract
9884 origin tree may not have the same tree structure as the concrete
9885 DIE, though. */
9886 die_info *corresponding_abstract_child
9887 = are_isomorphic ? origin_die->child : nullptr;
9888
9889 std::vector<sect_offset> offsets;
9890
9891 for (die_info *child_die = die->child;
9892 child_die && child_die->tag;
9893 child_die = child_die->sibling)
9894 {
9895 /* We are trying to process concrete instance entries:
9896 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
9897 it's not relevant to our analysis here. i.e. detecting DIEs that are
9898 present in the abstract instance but not referenced in the concrete
9899 one. */
9900 if (child_die->tag == DW_TAG_call_site
9901 || child_die->tag == DW_TAG_GNU_call_site)
9902 {
9903 if (are_isomorphic)
9904 corresponding_abstract_child
9905 = corresponding_abstract_child->sibling;
9906 continue;
9907 }
9908
9909 /* For each CHILD_DIE, find the corresponding child of
9910 ORIGIN_DIE. If there is more than one layer of
9911 DW_AT_abstract_origin, follow them all; there shouldn't be,
9912 but GCC versions at least through 4.4 generate this (GCC PR
9913 40573). */
9914 die_info *child_origin_die = child_die;
9915 dwarf2_cu *child_origin_cu = cu;
9916 while (true)
9917 {
9918 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9919 child_origin_cu);
9920 if (attr == nullptr)
9921 break;
9922
9923 child_origin_die = follow_die_ref (child_origin_die, attr,
9924 &child_origin_cu);
9925 }
9926
9927 /* If missing DW_AT_abstract_origin, try the corresponding child
9928 of the origin. Clang emits such lexical scopes. */
9929 if (child_origin_die == child_die
9930 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
9931 && are_isomorphic
9932 && child_die->tag == DW_TAG_lexical_block)
9933 child_origin_die = corresponding_abstract_child;
9934
9935 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9936 counterpart may exist. */
9937 if (child_origin_die != child_die)
9938 {
9939 if (child_die->tag != child_origin_die->tag
9940 && !(child_die->tag == DW_TAG_inlined_subroutine
9941 && child_origin_die->tag == DW_TAG_subprogram))
9942 complaint (_("Child DIE %s and its abstract origin %s have "
9943 "different tags"),
9944 sect_offset_str (child_die->sect_off),
9945 sect_offset_str (child_origin_die->sect_off));
9946 if (child_origin_die->parent != origin_die)
9947 complaint (_("Child DIE %s and its abstract origin %s have "
9948 "different parents"),
9949 sect_offset_str (child_die->sect_off),
9950 sect_offset_str (child_origin_die->sect_off));
9951 else
9952 offsets.push_back (child_origin_die->sect_off);
9953 }
9954
9955 if (are_isomorphic)
9956 corresponding_abstract_child = corresponding_abstract_child->sibling;
9957 }
9958
9959 if (!offsets.empty ())
9960 {
9961 std::sort (offsets.begin (), offsets.end ());
9962
9963 for (auto offsets_it = offsets.begin () + 1;
9964 offsets_it < offsets.end ();
9965 ++offsets_it)
9966 if (*(offsets_it - 1) == *offsets_it)
9967 complaint (_("Multiple children of DIE %s refer "
9968 "to DIE %s as their abstract origin"),
9969 sect_offset_str (die->sect_off),
9970 sect_offset_str (*offsets_it));
9971 }
9972
9973 auto offsets_it = offsets.begin ();
9974 die_info *origin_child_die = origin_die->child;
9975 while (origin_child_die != nullptr && origin_child_die->tag != 0)
9976 {
9977 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9978 while (offsets_it < offsets.end ()
9979 && *offsets_it < origin_child_die->sect_off)
9980 ++offsets_it;
9981
9982 if (offsets_it == offsets.end ()
9983 || *offsets_it > origin_child_die->sect_off)
9984 {
9985 /* Found that ORIGIN_CHILD_DIE is really not referenced.
9986 Check whether we're already processing ORIGIN_CHILD_DIE.
9987 This can happen with mutually referenced abstract_origins.
9988 PR 16581. */
9989 if (!origin_child_die->in_process)
9990 process_die (origin_child_die, origin_cu);
9991 }
9992
9993 origin_child_die = origin_child_die->sibling;
9994 }
9995
9996 origin_cu->list_in_scope = origin_previous_list_in_scope;
9997
9998 if (cu != origin_cu)
9999 compute_delayed_physnames (origin_cu);
10000 }
10001
10002 static void
10003 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
10004 {
10005 struct objfile *objfile = cu->per_objfile->objfile;
10006 struct gdbarch *gdbarch = objfile->arch ();
10007 struct context_stack *newobj;
10008 CORE_ADDR lowpc;
10009 CORE_ADDR highpc;
10010 struct die_info *child_die;
10011 struct attribute *attr, *call_line, *call_file;
10012 const char *name;
10013 CORE_ADDR baseaddr;
10014 struct block *block;
10015 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10016 std::vector<struct symbol *> template_args;
10017 struct template_symbol *templ_func = NULL;
10018
10019 if (inlined_func)
10020 {
10021 /* If we do not have call site information, we can't show the
10022 caller of this inlined function. That's too confusing, so
10023 only use the scope for local variables. */
10024 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10025 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10026 if (call_line == NULL || call_file == NULL)
10027 {
10028 read_lexical_block_scope (die, cu);
10029 return;
10030 }
10031 }
10032
10033 baseaddr = objfile->text_section_offset ();
10034
10035 name = dwarf2_name (die, cu);
10036 if (name == nullptr)
10037 name = dw2_linkage_name (die, cu);
10038
10039 /* Ignore functions with missing or empty names. These are actually
10040 illegal according to the DWARF standard. */
10041 if (name == NULL)
10042 {
10043 complaint (_("missing name for subprogram DIE at %s"),
10044 sect_offset_str (die->sect_off));
10045 return;
10046 }
10047
10048 /* Ignore functions with missing or invalid low and high pc attributes. */
10049 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
10050 <= PC_BOUNDS_INVALID)
10051 {
10052 if (have_complaint ())
10053 {
10054 attr = dwarf2_attr (die, DW_AT_external, cu);
10055 bool external_p = attr != nullptr && attr->as_boolean ();
10056 attr = dwarf2_attr (die, DW_AT_inline, cu);
10057 bool inlined_p
10058 = (attr != nullptr
10059 && attr->is_nonnegative ()
10060 && (attr->as_nonnegative () == DW_INL_inlined
10061 || attr->as_nonnegative () == DW_INL_declared_inlined));
10062 attr = dwarf2_attr (die, DW_AT_declaration, cu);
10063 bool decl_p = attr != nullptr && attr->as_boolean ();
10064 if (!external_p && !inlined_p && !decl_p)
10065 complaint (_("cannot get low and high bounds "
10066 "for subprogram DIE at %s"),
10067 sect_offset_str (die->sect_off));
10068 }
10069 return;
10070 }
10071
10072 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10073 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10074
10075 /* If we have any template arguments, then we must allocate a
10076 different sort of symbol. */
10077 for (child_die = die->child; child_die; child_die = child_die->sibling)
10078 {
10079 if (child_die->tag == DW_TAG_template_type_param
10080 || child_die->tag == DW_TAG_template_value_param)
10081 {
10082 templ_func = new (&objfile->objfile_obstack) template_symbol;
10083 templ_func->subclass = SYMBOL_TEMPLATE;
10084 break;
10085 }
10086 }
10087
10088 gdb_assert (cu->get_builder () != nullptr);
10089 newobj = cu->get_builder ()->push_context (0, lowpc);
10090 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
10091 (struct symbol *) templ_func);
10092
10093 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
10094 set_objfile_main_name (objfile, newobj->name->linkage_name (),
10095 cu->lang ());
10096
10097 /* If there is a location expression for DW_AT_frame_base, record
10098 it. */
10099 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10100 if (attr != nullptr)
10101 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
10102
10103 /* If there is a location for the static link, record it. */
10104 newobj->static_link = NULL;
10105 attr = dwarf2_attr (die, DW_AT_static_link, cu);
10106 if (attr != nullptr)
10107 {
10108 newobj->static_link
10109 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
10110 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
10111 cu->addr_type ());
10112 }
10113
10114 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
10115
10116 if (die->child != NULL)
10117 {
10118 child_die = die->child;
10119 while (child_die && child_die->tag)
10120 {
10121 if (child_die->tag == DW_TAG_template_type_param
10122 || child_die->tag == DW_TAG_template_value_param)
10123 {
10124 struct symbol *arg = new_symbol (child_die, NULL, cu);
10125
10126 if (arg != NULL)
10127 template_args.push_back (arg);
10128 }
10129 else
10130 process_die (child_die, cu);
10131 child_die = child_die->sibling;
10132 }
10133 }
10134
10135 inherit_abstract_dies (die, cu);
10136
10137 /* If we have a DW_AT_specification, we might need to import using
10138 directives from the context of the specification DIE. See the
10139 comment in determine_prefix. */
10140 if (cu->lang () == language_cplus
10141 && dwarf2_attr (die, DW_AT_specification, cu))
10142 {
10143 struct dwarf2_cu *spec_cu = cu;
10144 struct die_info *spec_die = die_specification (die, &spec_cu);
10145
10146 while (spec_die)
10147 {
10148 child_die = spec_die->child;
10149 while (child_die && child_die->tag)
10150 {
10151 if (child_die->tag == DW_TAG_imported_module)
10152 process_die (child_die, spec_cu);
10153 child_die = child_die->sibling;
10154 }
10155
10156 /* In some cases, GCC generates specification DIEs that
10157 themselves contain DW_AT_specification attributes. */
10158 spec_die = die_specification (spec_die, &spec_cu);
10159 }
10160 }
10161
10162 struct context_stack cstk = cu->get_builder ()->pop_context ();
10163 /* Make a block for the local symbols within. */
10164 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
10165 cstk.static_link, lowpc, highpc);
10166
10167 /* For C++, set the block's scope. */
10168 if ((cu->lang () == language_cplus
10169 || cu->lang () == language_fortran
10170 || cu->lang () == language_d
10171 || cu->lang () == language_rust)
10172 && cu->processing_has_namespace_info)
10173 block->set_scope (determine_prefix (die, cu),
10174 &objfile->objfile_obstack);
10175
10176 /* If we have address ranges, record them. */
10177 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10178
10179 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
10180
10181 /* Attach template arguments to function. */
10182 if (!template_args.empty ())
10183 {
10184 gdb_assert (templ_func != NULL);
10185
10186 templ_func->n_template_arguments = template_args.size ();
10187 templ_func->template_arguments
10188 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
10189 templ_func->n_template_arguments);
10190 memcpy (templ_func->template_arguments,
10191 template_args.data (),
10192 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10193
10194 /* Make sure that the symtab is set on the new symbols. Even
10195 though they don't appear in this symtab directly, other parts
10196 of gdb assume that symbols do, and this is reasonably
10197 true. */
10198 for (symbol *sym : template_args)
10199 sym->set_symtab (templ_func->symtab ());
10200 }
10201
10202 /* In C++, we can have functions nested inside functions (e.g., when
10203 a function declares a class that has methods). This means that
10204 when we finish processing a function scope, we may need to go
10205 back to building a containing block's symbol lists. */
10206 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10207 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10208
10209 /* If we've finished processing a top-level function, subsequent
10210 symbols go in the file symbol list. */
10211 if (cu->get_builder ()->outermost_context_p ())
10212 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
10213 }
10214
10215 /* Process all the DIES contained within a lexical block scope. Start
10216 a new scope, process the dies, and then close the scope. */
10217
10218 static void
10219 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10220 {
10221 struct objfile *objfile = cu->per_objfile->objfile;
10222 struct gdbarch *gdbarch = objfile->arch ();
10223 CORE_ADDR lowpc, highpc;
10224 struct die_info *child_die;
10225 CORE_ADDR baseaddr;
10226
10227 baseaddr = objfile->text_section_offset ();
10228
10229 /* Ignore blocks with missing or invalid low and high pc attributes. */
10230 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10231 as multiple lexical blocks? Handling children in a sane way would
10232 be nasty. Might be easier to properly extend generic blocks to
10233 describe ranges. */
10234 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
10235 {
10236 case PC_BOUNDS_NOT_PRESENT:
10237 /* DW_TAG_lexical_block has no attributes, process its children as if
10238 there was no wrapping by that DW_TAG_lexical_block.
10239 GCC does no longer produces such DWARF since GCC r224161. */
10240 for (child_die = die->child;
10241 child_die != NULL && child_die->tag;
10242 child_die = child_die->sibling)
10243 {
10244 /* We might already be processing this DIE. This can happen
10245 in an unusual circumstance -- where a subroutine A
10246 appears lexically in another subroutine B, but A actually
10247 inlines B. The recursion is broken here, rather than in
10248 inherit_abstract_dies, because it seems better to simply
10249 drop concrete children here. */
10250 if (!child_die->in_process)
10251 process_die (child_die, cu);
10252 }
10253 return;
10254 case PC_BOUNDS_INVALID:
10255 return;
10256 }
10257 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10258 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10259
10260 cu->get_builder ()->push_context (0, lowpc);
10261 if (die->child != NULL)
10262 {
10263 child_die = die->child;
10264 while (child_die && child_die->tag)
10265 {
10266 process_die (child_die, cu);
10267 child_die = child_die->sibling;
10268 }
10269 }
10270 inherit_abstract_dies (die, cu);
10271 struct context_stack cstk = cu->get_builder ()->pop_context ();
10272
10273 if (*cu->get_builder ()->get_local_symbols () != NULL
10274 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
10275 {
10276 struct block *block
10277 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
10278 cstk.start_addr, highpc);
10279
10280 /* Note that recording ranges after traversing children, as we
10281 do here, means that recording a parent's ranges entails
10282 walking across all its children's ranges as they appear in
10283 the address map, which is quadratic behavior.
10284
10285 It would be nicer to record the parent's ranges before
10286 traversing its children, simply overriding whatever you find
10287 there. But since we don't even decide whether to create a
10288 block until after we've traversed its children, that's hard
10289 to do. */
10290 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10291 }
10292 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10293 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10294 }
10295
10296 static void dwarf2_ranges_read_low_addrs (unsigned offset,
10297 struct dwarf2_cu *cu,
10298 dwarf_tag tag,
10299 std::vector<CORE_ADDR> &result);
10300
10301 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
10302
10303 static void
10304 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10305 {
10306 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10307 struct objfile *objfile = per_objfile->objfile;
10308 struct gdbarch *gdbarch = objfile->arch ();
10309 CORE_ADDR pc, baseaddr;
10310 struct attribute *attr;
10311 void **slot;
10312 int nparams;
10313 struct die_info *child_die;
10314
10315 baseaddr = objfile->text_section_offset ();
10316
10317 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
10318 if (attr == NULL)
10319 {
10320 /* This was a pre-DWARF-5 GNU extension alias
10321 for DW_AT_call_return_pc. */
10322 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10323 }
10324 if (!attr)
10325 {
10326 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
10327 "DIE %s [in module %s]"),
10328 sect_offset_str (die->sect_off), objfile_name (objfile));
10329 return;
10330 }
10331 pc = attr->as_address () + baseaddr;
10332 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
10333 pc -= baseaddr;
10334
10335 if (cu->call_site_htab == NULL)
10336 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
10337 call_site::eq, NULL,
10338 &objfile->objfile_obstack,
10339 hashtab_obstack_allocate, NULL);
10340 struct call_site call_site_local (pc, nullptr, nullptr);
10341 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10342 if (*slot != NULL)
10343 {
10344 complaint (_("Duplicate PC %s for DW_TAG_call_site "
10345 "DIE %s [in module %s]"),
10346 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
10347 objfile_name (objfile));
10348 return;
10349 }
10350
10351 /* Count parameters at the caller. */
10352
10353 nparams = 0;
10354 for (child_die = die->child; child_die && child_die->tag;
10355 child_die = child_die->sibling)
10356 {
10357 if (child_die->tag != DW_TAG_call_site_parameter
10358 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10359 {
10360 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
10361 "DW_TAG_call_site child DIE %s [in module %s]"),
10362 child_die->tag, sect_offset_str (child_die->sect_off),
10363 objfile_name (objfile));
10364 continue;
10365 }
10366
10367 nparams++;
10368 }
10369
10370 struct call_site *call_site
10371 = new (XOBNEWVAR (&objfile->objfile_obstack,
10372 struct call_site,
10373 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
10374 struct call_site (pc, cu->per_cu, per_objfile);
10375 *slot = call_site;
10376
10377 /* We never call the destructor of call_site, so we must ensure it is
10378 trivially destructible. */
10379 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
10380
10381 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
10382 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10383 {
10384 struct die_info *func_die;
10385
10386 /* Skip also over DW_TAG_inlined_subroutine. */
10387 for (func_die = die->parent;
10388 func_die && func_die->tag != DW_TAG_subprogram
10389 && func_die->tag != DW_TAG_subroutine_type;
10390 func_die = func_die->parent);
10391
10392 /* DW_AT_call_all_calls is a superset
10393 of DW_AT_call_all_tail_calls. */
10394 if (func_die
10395 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
10396 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10397 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
10398 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10399 {
10400 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10401 not complete. But keep CALL_SITE for look ups via call_site_htab,
10402 both the initial caller containing the real return address PC and
10403 the final callee containing the current PC of a chain of tail
10404 calls do not need to have the tail call list complete. But any
10405 function candidate for a virtual tail call frame searched via
10406 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10407 determined unambiguously. */
10408 }
10409 else
10410 {
10411 struct type *func_type = NULL;
10412
10413 if (func_die)
10414 func_type = get_die_type (func_die, cu);
10415 if (func_type != NULL)
10416 {
10417 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
10418
10419 /* Enlist this call site to the function. */
10420 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10421 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10422 }
10423 else
10424 complaint (_("Cannot find function owning DW_TAG_call_site "
10425 "DIE %s [in module %s]"),
10426 sect_offset_str (die->sect_off), objfile_name (objfile));
10427 }
10428 }
10429
10430 attr = dwarf2_attr (die, DW_AT_call_target, cu);
10431 if (attr == NULL)
10432 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10433 if (attr == NULL)
10434 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
10435 if (attr == NULL)
10436 {
10437 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
10438 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10439 }
10440
10441 call_site->target.set_loc_dwarf_block (nullptr);
10442 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
10443 /* Keep NULL DWARF_BLOCK. */;
10444 else if (attr->form_is_block ())
10445 {
10446 struct dwarf2_locexpr_baton *dlbaton;
10447 struct dwarf_block *block = attr->as_block ();
10448
10449 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
10450 dlbaton->data = block->data;
10451 dlbaton->size = block->size;
10452 dlbaton->per_objfile = per_objfile;
10453 dlbaton->per_cu = cu->per_cu;
10454
10455 call_site->target.set_loc_dwarf_block (dlbaton);
10456 }
10457 else if (attr->form_is_ref ())
10458 {
10459 struct dwarf2_cu *target_cu = cu;
10460 struct die_info *target_die;
10461
10462 target_die = follow_die_ref (die, attr, &target_cu);
10463 gdb_assert (target_cu->per_objfile->objfile == objfile);
10464
10465 struct attribute *ranges_attr
10466 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
10467
10468 if (die_is_declaration (target_die, target_cu))
10469 {
10470 const char *target_physname;
10471
10472 /* Prefer the mangled name; otherwise compute the demangled one. */
10473 target_physname = dw2_linkage_name (target_die, target_cu);
10474 if (target_physname == NULL)
10475 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10476 if (target_physname == NULL)
10477 complaint (_("DW_AT_call_target target DIE has invalid "
10478 "physname, for referencing DIE %s [in module %s]"),
10479 sect_offset_str (die->sect_off), objfile_name (objfile));
10480 else
10481 call_site->target.set_loc_physname (target_physname);
10482 }
10483 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
10484 {
10485 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
10486 + target_cu->gnu_ranges_base);
10487 std::vector<CORE_ADDR> addresses;
10488 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
10489 target_die->tag, addresses);
10490 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
10491 addresses.size ());
10492 std::copy (addresses.begin (), addresses.end (), saved);
10493 call_site->target.set_loc_array (addresses.size (), saved);
10494 }
10495 else
10496 {
10497 CORE_ADDR lowpc;
10498
10499 /* DW_AT_entry_pc should be preferred. */
10500 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
10501 nullptr, nullptr)
10502 <= PC_BOUNDS_INVALID)
10503 complaint (_("DW_AT_call_target target DIE has invalid "
10504 "low pc, for referencing DIE %s [in module %s]"),
10505 sect_offset_str (die->sect_off), objfile_name (objfile));
10506 else
10507 {
10508 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
10509 - baseaddr);
10510 call_site->target.set_loc_physaddr (lowpc);
10511 }
10512 }
10513 }
10514 else
10515 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
10516 "block nor reference, for DIE %s [in module %s]"),
10517 sect_offset_str (die->sect_off), objfile_name (objfile));
10518
10519 for (child_die = die->child;
10520 child_die && child_die->tag;
10521 child_die = child_die->sibling)
10522 {
10523 struct call_site_parameter *parameter;
10524 struct attribute *loc, *origin;
10525
10526 if (child_die->tag != DW_TAG_call_site_parameter
10527 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10528 {
10529 /* Already printed the complaint above. */
10530 continue;
10531 }
10532
10533 gdb_assert (call_site->parameter_count < nparams);
10534 parameter = &call_site->parameter[call_site->parameter_count];
10535
10536 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10537 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10538 register is contained in DW_AT_call_value. */
10539
10540 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10541 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
10542 if (origin == NULL)
10543 {
10544 /* This was a pre-DWARF-5 GNU extension alias
10545 for DW_AT_call_parameter. */
10546 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10547 }
10548 if (loc == NULL && origin != NULL && origin->form_is_ref ())
10549 {
10550 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10551
10552 sect_offset sect_off = origin->get_ref_die_offset ();
10553 if (!cu->header.offset_in_cu_p (sect_off))
10554 {
10555 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10556 binding can be done only inside one CU. Such referenced DIE
10557 therefore cannot be even moved to DW_TAG_partial_unit. */
10558 complaint (_("DW_AT_call_parameter offset is not in CU for "
10559 "DW_TAG_call_site child DIE %s [in module %s]"),
10560 sect_offset_str (child_die->sect_off),
10561 objfile_name (objfile));
10562 continue;
10563 }
10564 parameter->u.param_cu_off
10565 = (cu_offset) (sect_off - cu->header.sect_off);
10566 }
10567 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
10568 {
10569 complaint (_("No DW_FORM_block* DW_AT_location for "
10570 "DW_TAG_call_site child DIE %s [in module %s]"),
10571 sect_offset_str (child_die->sect_off), objfile_name (objfile));
10572 continue;
10573 }
10574 else
10575 {
10576 struct dwarf_block *block = loc->as_block ();
10577
10578 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10579 (block->data, &block->data[block->size]);
10580 if (parameter->u.dwarf_reg != -1)
10581 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10582 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
10583 &block->data[block->size],
10584 &parameter->u.fb_offset))
10585 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10586 else
10587 {
10588 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
10589 "for DW_FORM_block* DW_AT_location is supported for "
10590 "DW_TAG_call_site child DIE %s "
10591 "[in module %s]"),
10592 sect_offset_str (child_die->sect_off),
10593 objfile_name (objfile));
10594 continue;
10595 }
10596 }
10597
10598 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
10599 if (attr == NULL)
10600 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10601 if (attr == NULL || !attr->form_is_block ())
10602 {
10603 complaint (_("No DW_FORM_block* DW_AT_call_value for "
10604 "DW_TAG_call_site child DIE %s [in module %s]"),
10605 sect_offset_str (child_die->sect_off),
10606 objfile_name (objfile));
10607 continue;
10608 }
10609
10610 struct dwarf_block *block = attr->as_block ();
10611 parameter->value = block->data;
10612 parameter->value_size = block->size;
10613
10614 /* Parameters are not pre-cleared by memset above. */
10615 parameter->data_value = NULL;
10616 parameter->data_value_size = 0;
10617 call_site->parameter_count++;
10618
10619 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
10620 if (attr == NULL)
10621 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10622 if (attr != nullptr)
10623 {
10624 if (!attr->form_is_block ())
10625 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
10626 "DW_TAG_call_site child DIE %s [in module %s]"),
10627 sect_offset_str (child_die->sect_off),
10628 objfile_name (objfile));
10629 else
10630 {
10631 block = attr->as_block ();
10632 parameter->data_value = block->data;
10633 parameter->data_value_size = block->size;
10634 }
10635 }
10636 }
10637 }
10638
10639 /* Helper function for read_variable. If DIE represents a virtual
10640 table, then return the type of the concrete object that is
10641 associated with the virtual table. Otherwise, return NULL. */
10642
10643 static struct type *
10644 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10645 {
10646 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
10647 if (attr == NULL)
10648 return NULL;
10649
10650 /* Find the type DIE. */
10651 struct die_info *type_die = NULL;
10652 struct dwarf2_cu *type_cu = cu;
10653
10654 if (attr->form_is_ref ())
10655 type_die = follow_die_ref (die, attr, &type_cu);
10656 if (type_die == NULL)
10657 return NULL;
10658
10659 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
10660 return NULL;
10661 return die_containing_type (type_die, type_cu);
10662 }
10663
10664 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
10665
10666 static void
10667 read_variable (struct die_info *die, struct dwarf2_cu *cu)
10668 {
10669 struct rust_vtable_symbol *storage = NULL;
10670
10671 if (cu->lang () == language_rust)
10672 {
10673 struct type *containing_type = rust_containing_type (die, cu);
10674
10675 if (containing_type != NULL)
10676 {
10677 struct objfile *objfile = cu->per_objfile->objfile;
10678
10679 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
10680 storage->concrete_type = containing_type;
10681 storage->subclass = SYMBOL_RUST_VTABLE;
10682 }
10683 }
10684
10685 struct symbol *res = new_symbol (die, NULL, cu, storage);
10686 struct attribute *abstract_origin
10687 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10688 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
10689 if (res == NULL && loc && abstract_origin)
10690 {
10691 /* We have a variable without a name, but with a location and an abstract
10692 origin. This may be a concrete instance of an abstract variable
10693 referenced from an DW_OP_GNU_variable_value, so save it to find it back
10694 later. */
10695 struct dwarf2_cu *origin_cu = cu;
10696 struct die_info *origin_die
10697 = follow_die_ref (die, abstract_origin, &origin_cu);
10698 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10699 per_objfile->per_bfd->abstract_to_concrete
10700 [origin_die->sect_off].push_back (die->sect_off);
10701 }
10702 }
10703
10704 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
10705 reading .debug_rnglists.
10706 Callback's type should be:
10707 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10708 Return true if the attributes are present and valid, otherwise,
10709 return false. */
10710
10711 template <typename Callback>
10712 static bool
10713 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
10714 dwarf_tag tag, Callback &&callback)
10715 {
10716 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10717 struct objfile *objfile = per_objfile->objfile;
10718 bfd *obfd = objfile->obfd.get ();
10719 /* Base address selection entry. */
10720 gdb::optional<CORE_ADDR> base;
10721 const gdb_byte *buffer;
10722 bool overflow = false;
10723 ULONGEST addr_index;
10724 struct dwarf2_section_info *rnglists_section;
10725
10726 base = cu->base_address;
10727 rnglists_section = cu_debug_rnglists_section (cu, tag);
10728 rnglists_section->read (objfile);
10729
10730 if (offset >= rnglists_section->size)
10731 {
10732 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10733 offset);
10734 return false;
10735 }
10736 buffer = rnglists_section->buffer + offset;
10737
10738 while (1)
10739 {
10740 /* Initialize it due to a false compiler warning. */
10741 CORE_ADDR range_beginning = 0, range_end = 0;
10742 const gdb_byte *buf_end = (rnglists_section->buffer
10743 + rnglists_section->size);
10744 unsigned int bytes_read;
10745
10746 if (buffer == buf_end)
10747 {
10748 overflow = true;
10749 break;
10750 }
10751 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
10752 switch (rlet)
10753 {
10754 case DW_RLE_end_of_list:
10755 break;
10756 case DW_RLE_base_address:
10757 if (buffer + cu->header.addr_size > buf_end)
10758 {
10759 overflow = true;
10760 break;
10761 }
10762 base = cu->header.read_address (obfd, buffer, &bytes_read);
10763 buffer += bytes_read;
10764 break;
10765 case DW_RLE_base_addressx:
10766 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10767 buffer += bytes_read;
10768 base = read_addr_index (cu, addr_index);
10769 break;
10770 case DW_RLE_start_length:
10771 if (buffer + cu->header.addr_size > buf_end)
10772 {
10773 overflow = true;
10774 break;
10775 }
10776 range_beginning = cu->header.read_address (obfd, buffer,
10777 &bytes_read);
10778 buffer += bytes_read;
10779 range_end = (range_beginning
10780 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
10781 buffer += bytes_read;
10782 if (buffer > buf_end)
10783 {
10784 overflow = true;
10785 break;
10786 }
10787 break;
10788 case DW_RLE_startx_length:
10789 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10790 buffer += bytes_read;
10791 range_beginning = read_addr_index (cu, addr_index);
10792 if (buffer > buf_end)
10793 {
10794 overflow = true;
10795 break;
10796 }
10797 range_end = (range_beginning
10798 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
10799 buffer += bytes_read;
10800 break;
10801 case DW_RLE_offset_pair:
10802 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10803 buffer += bytes_read;
10804 if (buffer > buf_end)
10805 {
10806 overflow = true;
10807 break;
10808 }
10809 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10810 buffer += bytes_read;
10811 if (buffer > buf_end)
10812 {
10813 overflow = true;
10814 break;
10815 }
10816 break;
10817 case DW_RLE_start_end:
10818 if (buffer + 2 * cu->header.addr_size > buf_end)
10819 {
10820 overflow = true;
10821 break;
10822 }
10823 range_beginning = cu->header.read_address (obfd, buffer,
10824 &bytes_read);
10825 buffer += bytes_read;
10826 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
10827 buffer += bytes_read;
10828 break;
10829 case DW_RLE_startx_endx:
10830 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10831 buffer += bytes_read;
10832 range_beginning = read_addr_index (cu, addr_index);
10833 if (buffer > buf_end)
10834 {
10835 overflow = true;
10836 break;
10837 }
10838 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10839 buffer += bytes_read;
10840 range_end = read_addr_index (cu, addr_index);
10841 break;
10842 default:
10843 complaint (_("Invalid .debug_rnglists data (no base address)"));
10844 return false;
10845 }
10846 if (rlet == DW_RLE_end_of_list || overflow)
10847 break;
10848 if (rlet == DW_RLE_base_address)
10849 continue;
10850
10851 if (range_beginning > range_end)
10852 {
10853 /* Inverted range entries are invalid. */
10854 complaint (_("Invalid .debug_rnglists data (inverted range)"));
10855 return false;
10856 }
10857
10858 /* Empty range entries have no effect. */
10859 if (range_beginning == range_end)
10860 continue;
10861
10862 /* Only DW_RLE_offset_pair needs the base address added. */
10863 if (rlet == DW_RLE_offset_pair)
10864 {
10865 if (!base.has_value ())
10866 {
10867 /* We have no valid base address for the DW_RLE_offset_pair. */
10868 complaint (_("Invalid .debug_rnglists data (no base address for "
10869 "DW_RLE_offset_pair)"));
10870 return false;
10871 }
10872
10873 range_beginning += *base;
10874 range_end += *base;
10875 }
10876
10877 /* A not-uncommon case of bad debug info.
10878 Don't pollute the addrmap with bad data. */
10879 if (range_beginning == 0
10880 && !per_objfile->per_bfd->has_section_at_zero)
10881 {
10882 complaint (_(".debug_rnglists entry has start address of zero"
10883 " [in module %s]"), objfile_name (objfile));
10884 continue;
10885 }
10886
10887 callback (range_beginning, range_end);
10888 }
10889
10890 if (overflow)
10891 {
10892 complaint (_("Offset %d is not terminated "
10893 "for DW_AT_ranges attribute"),
10894 offset);
10895 return false;
10896 }
10897
10898 return true;
10899 }
10900
10901 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
10902 Callback's type should be:
10903 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10904 Return 1 if the attributes are present and valid, otherwise, return 0. */
10905
10906 template <typename Callback>
10907 static int
10908 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
10909 Callback &&callback)
10910 {
10911 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10912 struct objfile *objfile = per_objfile->objfile;
10913 struct comp_unit_head *cu_header = &cu->header;
10914 bfd *obfd = objfile->obfd.get ();
10915 unsigned int addr_size = cu_header->addr_size;
10916 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10917 /* Base address selection entry. */
10918 gdb::optional<CORE_ADDR> base;
10919 unsigned int dummy;
10920 const gdb_byte *buffer;
10921
10922 if (cu_header->version >= 5)
10923 return dwarf2_rnglists_process (offset, cu, tag, callback);
10924
10925 base = cu->base_address;
10926
10927 per_objfile->per_bfd->ranges.read (objfile);
10928 if (offset >= per_objfile->per_bfd->ranges.size)
10929 {
10930 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10931 offset);
10932 return 0;
10933 }
10934 buffer = per_objfile->per_bfd->ranges.buffer + offset;
10935
10936 while (1)
10937 {
10938 CORE_ADDR range_beginning, range_end;
10939
10940 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
10941 buffer += addr_size;
10942 range_end = cu->header.read_address (obfd, buffer, &dummy);
10943 buffer += addr_size;
10944 offset += 2 * addr_size;
10945
10946 /* An end of list marker is a pair of zero addresses. */
10947 if (range_beginning == 0 && range_end == 0)
10948 /* Found the end of list entry. */
10949 break;
10950
10951 /* Each base address selection entry is a pair of 2 values.
10952 The first is the largest possible address, the second is
10953 the base address. Check for a base address here. */
10954 if ((range_beginning & mask) == mask)
10955 {
10956 /* If we found the largest possible address, then we already
10957 have the base address in range_end. */
10958 base = range_end;
10959 continue;
10960 }
10961
10962 if (!base.has_value ())
10963 {
10964 /* We have no valid base address for the ranges
10965 data. */
10966 complaint (_("Invalid .debug_ranges data (no base address)"));
10967 return 0;
10968 }
10969
10970 if (range_beginning > range_end)
10971 {
10972 /* Inverted range entries are invalid. */
10973 complaint (_("Invalid .debug_ranges data (inverted range)"));
10974 return 0;
10975 }
10976
10977 /* Empty range entries have no effect. */
10978 if (range_beginning == range_end)
10979 continue;
10980
10981 range_beginning += *base;
10982 range_end += *base;
10983
10984 /* A not-uncommon case of bad debug info.
10985 Don't pollute the addrmap with bad data. */
10986 if (range_beginning == 0
10987 && !per_objfile->per_bfd->has_section_at_zero)
10988 {
10989 complaint (_(".debug_ranges entry has start address of zero"
10990 " [in module %s]"), objfile_name (objfile));
10991 continue;
10992 }
10993
10994 callback (range_beginning, range_end);
10995 }
10996
10997 return 1;
10998 }
10999
11000 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
11001 Return 1 if the attributes are present and valid, otherwise, return 0.
11002 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
11003 ranges in MAP are set, using DATUM as the value. */
11004
11005 static int
11006 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
11007 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11008 addrmap *map, void *datum, dwarf_tag tag)
11009 {
11010 struct objfile *objfile = cu->per_objfile->objfile;
11011 struct gdbarch *gdbarch = objfile->arch ();
11012 const CORE_ADDR baseaddr = objfile->text_section_offset ();
11013 int low_set = 0;
11014 CORE_ADDR low = 0;
11015 CORE_ADDR high = 0;
11016 int retval;
11017
11018 retval = dwarf2_ranges_process (offset, cu, tag,
11019 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
11020 {
11021 if (map != nullptr)
11022 {
11023 CORE_ADDR lowpc;
11024 CORE_ADDR highpc;
11025
11026 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
11027 range_beginning + baseaddr)
11028 - baseaddr);
11029 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
11030 range_end + baseaddr)
11031 - baseaddr);
11032 map->set_empty (lowpc, highpc - 1, datum);
11033 }
11034
11035 /* FIXME: This is recording everything as a low-high
11036 segment of consecutive addresses. We should have a
11037 data structure for discontiguous block ranges
11038 instead. */
11039 if (! low_set)
11040 {
11041 low = range_beginning;
11042 high = range_end;
11043 low_set = 1;
11044 }
11045 else
11046 {
11047 if (range_beginning < low)
11048 low = range_beginning;
11049 if (range_end > high)
11050 high = range_end;
11051 }
11052 });
11053 if (!retval)
11054 return 0;
11055
11056 if (! low_set)
11057 /* If the first entry is an end-of-list marker, the range
11058 describes an empty scope, i.e. no instructions. */
11059 return 0;
11060
11061 if (low_return)
11062 *low_return = low;
11063 if (high_return)
11064 *high_return = high;
11065 return 1;
11066 }
11067
11068 /* Process ranges and fill in a vector of the low PC values only. */
11069
11070 static void
11071 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
11072 dwarf_tag tag,
11073 std::vector<CORE_ADDR> &result)
11074 {
11075 dwarf2_ranges_process (offset, cu, tag,
11076 [&] (CORE_ADDR start, CORE_ADDR end)
11077 {
11078 result.push_back (start);
11079 });
11080 }
11081
11082 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
11083 definition for the return value. *LOWPC and *HIGHPC are set iff
11084 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
11085
11086 static enum pc_bounds_kind
11087 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
11088 CORE_ADDR *highpc, struct dwarf2_cu *cu,
11089 addrmap *map, void *datum)
11090 {
11091 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11092 struct attribute *attr;
11093 struct attribute *attr_high;
11094 CORE_ADDR low = 0;
11095 CORE_ADDR high = 0;
11096 enum pc_bounds_kind ret;
11097
11098 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11099 if (attr_high)
11100 {
11101 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11102 if (attr != nullptr)
11103 {
11104 low = attr->as_address ();
11105 high = attr_high->as_address ();
11106 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11107 high += low;
11108 }
11109 else
11110 /* Found high w/o low attribute. */
11111 return PC_BOUNDS_INVALID;
11112
11113 /* Found consecutive range of addresses. */
11114 ret = PC_BOUNDS_HIGH_LOW;
11115 }
11116 else
11117 {
11118 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11119 if (attr != nullptr && attr->form_is_unsigned ())
11120 {
11121 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11122 on DWARF version). */
11123 ULONGEST ranges_offset = attr->as_unsigned ();
11124
11125 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11126 this value. */
11127 if (die->tag != DW_TAG_compile_unit)
11128 ranges_offset += cu->gnu_ranges_base;
11129
11130 /* Value of the DW_AT_ranges attribute is the offset in the
11131 .debug_ranges section. */
11132 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
11133 map, datum, die->tag))
11134 return PC_BOUNDS_INVALID;
11135 /* Found discontinuous range of addresses. */
11136 ret = PC_BOUNDS_RANGES;
11137 }
11138 else
11139 return PC_BOUNDS_NOT_PRESENT;
11140 }
11141
11142 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
11143 if (high <= low)
11144 return PC_BOUNDS_INVALID;
11145
11146 /* When using the GNU linker, .gnu.linkonce. sections are used to
11147 eliminate duplicate copies of functions and vtables and such.
11148 The linker will arbitrarily choose one and discard the others.
11149 The AT_*_pc values for such functions refer to local labels in
11150 these sections. If the section from that file was discarded, the
11151 labels are not in the output, so the relocs get a value of 0.
11152 If this is a discarded function, mark the pc bounds as invalid,
11153 so that GDB will ignore it. */
11154 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
11155 return PC_BOUNDS_INVALID;
11156
11157 *lowpc = low;
11158 if (highpc)
11159 *highpc = high;
11160 return ret;
11161 }
11162
11163 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
11164 its low and high PC addresses. Do nothing if these addresses could not
11165 be determined. Otherwise, set LOWPC to the low address if it is smaller,
11166 and HIGHPC to the high address if greater than HIGHPC. */
11167
11168 static void
11169 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
11170 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11171 struct dwarf2_cu *cu)
11172 {
11173 CORE_ADDR low, high;
11174 struct die_info *child = die->child;
11175
11176 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
11177 >= PC_BOUNDS_RANGES)
11178 {
11179 *lowpc = std::min (*lowpc, low);
11180 *highpc = std::max (*highpc, high);
11181 }
11182
11183 /* If the language does not allow nested subprograms (either inside
11184 subprograms or lexical blocks), we're done. */
11185 if (cu->lang () != language_ada)
11186 return;
11187
11188 /* Check all the children of the given DIE. If it contains nested
11189 subprograms, then check their pc bounds. Likewise, we need to
11190 check lexical blocks as well, as they may also contain subprogram
11191 definitions. */
11192 while (child && child->tag)
11193 {
11194 if (child->tag == DW_TAG_subprogram
11195 || child->tag == DW_TAG_lexical_block)
11196 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
11197 child = child->sibling;
11198 }
11199 }
11200
11201 /* Get the low and high pc's represented by the scope DIE, and store
11202 them in *LOWPC and *HIGHPC. If the correct values can't be
11203 determined, set *LOWPC to -1 and *HIGHPC to 0. */
11204
11205 static void
11206 get_scope_pc_bounds (struct die_info *die,
11207 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11208 struct dwarf2_cu *cu)
11209 {
11210 CORE_ADDR best_low = (CORE_ADDR) -1;
11211 CORE_ADDR best_high = (CORE_ADDR) 0;
11212 CORE_ADDR current_low, current_high;
11213
11214 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
11215 nullptr, nullptr)
11216 >= PC_BOUNDS_RANGES)
11217 {
11218 best_low = current_low;
11219 best_high = current_high;
11220 }
11221 else
11222 {
11223 struct die_info *child = die->child;
11224
11225 while (child && child->tag)
11226 {
11227 switch (child->tag) {
11228 case DW_TAG_subprogram:
11229 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
11230 break;
11231 case DW_TAG_namespace:
11232 case DW_TAG_module:
11233 /* FIXME: carlton/2004-01-16: Should we do this for
11234 DW_TAG_class_type/DW_TAG_structure_type, too? I think
11235 that current GCC's always emit the DIEs corresponding
11236 to definitions of methods of classes as children of a
11237 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11238 the DIEs giving the declarations, which could be
11239 anywhere). But I don't see any reason why the
11240 standards says that they have to be there. */
11241 get_scope_pc_bounds (child, &current_low, &current_high, cu);
11242
11243 if (current_low != ((CORE_ADDR) -1))
11244 {
11245 best_low = std::min (best_low, current_low);
11246 best_high = std::max (best_high, current_high);
11247 }
11248 break;
11249 default:
11250 /* Ignore. */
11251 break;
11252 }
11253
11254 child = child->sibling;
11255 }
11256 }
11257
11258 *lowpc = best_low;
11259 *highpc = best_high;
11260 }
11261
11262 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
11263 in DIE. */
11264
11265 static void
11266 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
11267 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
11268 {
11269 struct objfile *objfile = cu->per_objfile->objfile;
11270 struct gdbarch *gdbarch = objfile->arch ();
11271 struct attribute *attr;
11272 struct attribute *attr_high;
11273
11274 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11275 if (attr_high)
11276 {
11277 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11278 if (attr != nullptr)
11279 {
11280 CORE_ADDR low = attr->as_address ();
11281 CORE_ADDR high = attr_high->as_address ();
11282
11283 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11284 high += low;
11285
11286 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
11287 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
11288 cu->get_builder ()->record_block_range (block, low, high - 1);
11289 }
11290 }
11291
11292 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11293 if (attr != nullptr && attr->form_is_unsigned ())
11294 {
11295 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11296 on DWARF version). */
11297 ULONGEST ranges_offset = attr->as_unsigned ();
11298
11299 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11300 this value. */
11301 if (die->tag != DW_TAG_compile_unit)
11302 ranges_offset += cu->gnu_ranges_base;
11303
11304 std::vector<blockrange> blockvec;
11305 dwarf2_ranges_process (ranges_offset, cu, die->tag,
11306 [&] (CORE_ADDR start, CORE_ADDR end)
11307 {
11308 start += baseaddr;
11309 end += baseaddr;
11310 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
11311 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
11312 cu->get_builder ()->record_block_range (block, start, end - 1);
11313 blockvec.emplace_back (start, end);
11314 });
11315
11316 block->set_ranges (make_blockranges (objfile, blockvec));
11317 }
11318 }
11319
11320 /* Check whether the producer field indicates either of GCC < 4.6, or the
11321 Intel C/C++ compiler, and cache the result in CU. */
11322
11323 static void
11324 check_producer (struct dwarf2_cu *cu)
11325 {
11326 int major, minor;
11327
11328 if (cu->producer == NULL)
11329 {
11330 /* For unknown compilers expect their behavior is DWARF version
11331 compliant.
11332
11333 GCC started to support .debug_types sections by -gdwarf-4 since
11334 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11335 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11336 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11337 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11338 }
11339 else if (producer_is_gcc (cu->producer, &major, &minor))
11340 {
11341 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11342 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11343 cu->producer_is_gcc_11 = major == 11;
11344 }
11345 else if (producer_is_icc (cu->producer, &major, &minor))
11346 {
11347 cu->producer_is_icc = true;
11348 cu->producer_is_icc_lt_14 = major < 14;
11349 }
11350 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
11351 cu->producer_is_codewarrior = true;
11352 else if (producer_is_clang (cu->producer, &major, &minor))
11353 cu->producer_is_clang = true;
11354 else
11355 {
11356 /* For other non-GCC compilers, expect their behavior is DWARF version
11357 compliant. */
11358 }
11359
11360 cu->checked_producer = true;
11361 }
11362
11363 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11364 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11365 during 4.6.0 experimental. */
11366
11367 static bool
11368 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11369 {
11370 if (!cu->checked_producer)
11371 check_producer (cu);
11372
11373 return cu->producer_is_gxx_lt_4_6;
11374 }
11375
11376
11377 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
11378 with incorrect is_stmt attributes. */
11379
11380 static bool
11381 producer_is_codewarrior (struct dwarf2_cu *cu)
11382 {
11383 if (!cu->checked_producer)
11384 check_producer (cu);
11385
11386 return cu->producer_is_codewarrior;
11387 }
11388
11389 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
11390 If that attribute is not available, return the appropriate
11391 default. */
11392
11393 static enum dwarf_access_attribute
11394 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11395 {
11396 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11397 if (attr != nullptr)
11398 {
11399 LONGEST value = attr->constant_value (-1);
11400 if (value == DW_ACCESS_public
11401 || value == DW_ACCESS_protected
11402 || value == DW_ACCESS_private)
11403 return (dwarf_access_attribute) value;
11404 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
11405 plongest (value));
11406 }
11407
11408 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11409 {
11410 /* The default DWARF 2 accessibility for members is public, the default
11411 accessibility for inheritance is private. */
11412
11413 if (die->tag != DW_TAG_inheritance)
11414 return DW_ACCESS_public;
11415 else
11416 return DW_ACCESS_private;
11417 }
11418 else
11419 {
11420 /* DWARF 3+ defines the default accessibility a different way. The same
11421 rules apply now for DW_TAG_inheritance as for the members and it only
11422 depends on the container kind. */
11423
11424 if (die->parent->tag == DW_TAG_class_type)
11425 return DW_ACCESS_private;
11426 else
11427 return DW_ACCESS_public;
11428 }
11429 }
11430
11431 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
11432 *OFFSET to the byte offset. If the attribute was not found return
11433 0, otherwise return 1. If it was found but could not properly be
11434 handled, set *OFFSET to 0. */
11435
11436 static int
11437 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11438 LONGEST *offset)
11439 {
11440 struct attribute *attr;
11441
11442 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11443 if (attr != NULL)
11444 {
11445 *offset = 0;
11446
11447 /* Note that we do not check for a section offset first here.
11448 This is because DW_AT_data_member_location is new in DWARF 4,
11449 so if we see it, we can assume that a constant form is really
11450 a constant and not a section offset. */
11451 if (attr->form_is_constant ())
11452 *offset = attr->constant_value (0);
11453 else if (attr->form_is_section_offset ())
11454 dwarf2_complex_location_expr_complaint ();
11455 else if (attr->form_is_block ())
11456 *offset = decode_locdesc (attr->as_block (), cu);
11457 else
11458 dwarf2_complex_location_expr_complaint ();
11459
11460 return 1;
11461 }
11462 else
11463 {
11464 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11465 if (attr != nullptr)
11466 {
11467 *offset = attr->constant_value (0);
11468 return 1;
11469 }
11470 }
11471
11472 return 0;
11473 }
11474
11475 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
11476 store the results in FIELD. */
11477
11478 static void
11479 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11480 struct field *field)
11481 {
11482 struct attribute *attr;
11483
11484 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11485 if (attr != NULL)
11486 {
11487 if (attr->form_is_constant ())
11488 {
11489 LONGEST offset = attr->constant_value (0);
11490
11491 /* Work around this GCC 11 bug, where it would erroneously use -1
11492 data member locations, instead of 0:
11493
11494 Negative DW_AT_data_member_location
11495 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
11496 */
11497 if (offset == -1 && cu->producer_is_gcc_11)
11498 {
11499 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
11500 offset = 0;
11501 }
11502
11503 field->set_loc_bitpos (offset * bits_per_byte);
11504 }
11505 else if (attr->form_is_section_offset ())
11506 dwarf2_complex_location_expr_complaint ();
11507 else if (attr->form_is_block ())
11508 {
11509 bool handled;
11510 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
11511 if (handled)
11512 field->set_loc_bitpos (offset * bits_per_byte);
11513 else
11514 {
11515 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11516 struct objfile *objfile = per_objfile->objfile;
11517 struct dwarf2_locexpr_baton *dlbaton
11518 = XOBNEW (&objfile->objfile_obstack,
11519 struct dwarf2_locexpr_baton);
11520 dlbaton->data = attr->as_block ()->data;
11521 dlbaton->size = attr->as_block ()->size;
11522 /* When using this baton, we want to compute the address
11523 of the field, not the value. This is why
11524 is_reference is set to false here. */
11525 dlbaton->is_reference = false;
11526 dlbaton->per_objfile = per_objfile;
11527 dlbaton->per_cu = cu->per_cu;
11528
11529 field->set_loc_dwarf_block (dlbaton);
11530 }
11531 }
11532 else
11533 dwarf2_complex_location_expr_complaint ();
11534 }
11535 else
11536 {
11537 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11538 if (attr != nullptr)
11539 field->set_loc_bitpos (attr->constant_value (0));
11540 }
11541 }
11542
11543 /* Add an aggregate field to the field list. */
11544
11545 static void
11546 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11547 struct dwarf2_cu *cu)
11548 {
11549 struct objfile *objfile = cu->per_objfile->objfile;
11550 struct gdbarch *gdbarch = objfile->arch ();
11551 struct nextfield *new_field;
11552 struct attribute *attr;
11553 struct field *fp;
11554 const char *fieldname = "";
11555
11556 if (die->tag == DW_TAG_inheritance)
11557 {
11558 fip->baseclasses.emplace_back ();
11559 new_field = &fip->baseclasses.back ();
11560 }
11561 else
11562 {
11563 fip->fields.emplace_back ();
11564 new_field = &fip->fields.back ();
11565 }
11566
11567 new_field->offset = die->sect_off;
11568
11569 new_field->accessibility = dwarf2_access_attribute (die, cu);
11570 if (new_field->accessibility != DW_ACCESS_public)
11571 fip->non_public_fields = true;
11572
11573 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11574 if (attr != nullptr)
11575 new_field->virtuality = attr->as_virtuality ();
11576 else
11577 new_field->virtuality = DW_VIRTUALITY_none;
11578
11579 fp = &new_field->field;
11580
11581 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
11582 && !die_is_declaration (die, cu))
11583 {
11584 if (die->tag == DW_TAG_namelist_item)
11585 {
11586 /* Typically, DW_TAG_namelist_item are references to namelist items.
11587 If so, follow that reference. */
11588 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
11589 struct die_info *item_die = nullptr;
11590 struct dwarf2_cu *item_cu = cu;
11591 if (attr1->form_is_ref ())
11592 item_die = follow_die_ref (die, attr1, &item_cu);
11593 if (item_die != nullptr)
11594 die = item_die;
11595 }
11596 /* Data member other than a C++ static data member. */
11597
11598 /* Get type of field. */
11599 fp->set_type (die_type (die, cu));
11600
11601 fp->set_loc_bitpos (0);
11602
11603 /* Get bit size of field (zero if none). */
11604 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11605 if (attr != nullptr)
11606 {
11607 FIELD_BITSIZE (*fp) = attr->constant_value (0);
11608 }
11609 else
11610 {
11611 FIELD_BITSIZE (*fp) = 0;
11612 }
11613
11614 /* Get bit offset of field. */
11615 handle_member_location (die, cu, fp);
11616 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11617 if (attr != nullptr && attr->form_is_constant ())
11618 {
11619 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
11620 {
11621 /* For big endian bits, the DW_AT_bit_offset gives the
11622 additional bit offset from the MSB of the containing
11623 anonymous object to the MSB of the field. We don't
11624 have to do anything special since we don't need to
11625 know the size of the anonymous object. */
11626 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
11627 }
11628 else
11629 {
11630 /* For little endian bits, compute the bit offset to the
11631 MSB of the anonymous object, subtract off the number of
11632 bits from the MSB of the field to the MSB of the
11633 object, and then subtract off the number of bits of
11634 the field itself. The result is the bit offset of
11635 the LSB of the field. */
11636 int anonymous_size;
11637 int bit_offset = attr->constant_value (0);
11638
11639 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11640 if (attr != nullptr && attr->form_is_constant ())
11641 {
11642 /* The size of the anonymous object containing
11643 the bit field is explicit, so use the
11644 indicated size (in bytes). */
11645 anonymous_size = attr->constant_value (0);
11646 }
11647 else
11648 {
11649 /* The size of the anonymous object containing
11650 the bit field must be inferred from the type
11651 attribute of the data member containing the
11652 bit field. */
11653 anonymous_size = fp->type ()->length ();
11654 }
11655 fp->set_loc_bitpos (fp->loc_bitpos ()
11656 + anonymous_size * bits_per_byte
11657 - bit_offset - FIELD_BITSIZE (*fp));
11658 }
11659 }
11660
11661 /* Get name of field. */
11662 fieldname = dwarf2_name (die, cu);
11663 if (fieldname == NULL)
11664 fieldname = "";
11665
11666 /* The name is already allocated along with this objfile, so we don't
11667 need to duplicate it for the type. */
11668 fp->set_name (fieldname);
11669
11670 /* Change accessibility for artificial fields (e.g. virtual table
11671 pointer or virtual base class pointer) to private. */
11672 if (dwarf2_attr (die, DW_AT_artificial, cu))
11673 {
11674 FIELD_ARTIFICIAL (*fp) = 1;
11675 new_field->accessibility = DW_ACCESS_private;
11676 fip->non_public_fields = true;
11677 }
11678 }
11679 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11680 {
11681 /* C++ static member. */
11682
11683 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11684 is a declaration, but all versions of G++ as of this writing
11685 (so through at least 3.2.1) incorrectly generate
11686 DW_TAG_variable tags. */
11687
11688 const char *physname;
11689
11690 /* Get name of field. */
11691 fieldname = dwarf2_name (die, cu);
11692 if (fieldname == NULL)
11693 return;
11694
11695 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11696 if (attr
11697 /* Only create a symbol if this is an external value.
11698 new_symbol checks this and puts the value in the global symbol
11699 table, which we want. If it is not external, new_symbol
11700 will try to put the value in cu->list_in_scope which is wrong. */
11701 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11702 {
11703 /* A static const member, not much different than an enum as far as
11704 we're concerned, except that we can support more types. */
11705 new_symbol (die, NULL, cu);
11706 }
11707
11708 /* Get physical name. */
11709 physname = dwarf2_physname (fieldname, die, cu);
11710
11711 /* The name is already allocated along with this objfile, so we don't
11712 need to duplicate it for the type. */
11713 fp->set_loc_physname (physname ? physname : "");
11714 fp->set_type (die_type (die, cu));
11715 fp->set_name (fieldname);
11716 }
11717 else if (die->tag == DW_TAG_inheritance)
11718 {
11719 /* C++ base class field. */
11720 handle_member_location (die, cu, fp);
11721 FIELD_BITSIZE (*fp) = 0;
11722 fp->set_type (die_type (die, cu));
11723 fp->set_name (fp->type ()->name ());
11724 }
11725 else
11726 gdb_assert_not_reached ("missing case in dwarf2_add_field");
11727 }
11728
11729 /* Can the type given by DIE define another type? */
11730
11731 static bool
11732 type_can_define_types (const struct die_info *die)
11733 {
11734 switch (die->tag)
11735 {
11736 case DW_TAG_typedef:
11737 case DW_TAG_class_type:
11738 case DW_TAG_structure_type:
11739 case DW_TAG_union_type:
11740 case DW_TAG_enumeration_type:
11741 return true;
11742
11743 default:
11744 return false;
11745 }
11746 }
11747
11748 /* Add a type definition defined in the scope of the FIP's class. */
11749
11750 static void
11751 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
11752 struct dwarf2_cu *cu)
11753 {
11754 struct decl_field fp;
11755 memset (&fp, 0, sizeof (fp));
11756
11757 gdb_assert (type_can_define_types (die));
11758
11759 /* Get name of field. NULL is okay here, meaning an anonymous type. */
11760 fp.name = dwarf2_name (die, cu);
11761 fp.type = read_type_die (die, cu);
11762
11763 /* Save accessibility. */
11764 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
11765 switch (accessibility)
11766 {
11767 case DW_ACCESS_public:
11768 /* The assumed value if neither private nor protected. */
11769 break;
11770 case DW_ACCESS_private:
11771 fp.is_private = 1;
11772 break;
11773 case DW_ACCESS_protected:
11774 fp.is_protected = 1;
11775 break;
11776 }
11777
11778 if (die->tag == DW_TAG_typedef)
11779 fip->typedef_field_list.push_back (fp);
11780 else
11781 fip->nested_types_list.push_back (fp);
11782 }
11783
11784 /* A convenience typedef that's used when finding the discriminant
11785 field for a variant part. */
11786 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
11787 offset_map_type;
11788
11789 /* Compute the discriminant range for a given variant. OBSTACK is
11790 where the results will be stored. VARIANT is the variant to
11791 process. IS_UNSIGNED indicates whether the discriminant is signed
11792 or unsigned. */
11793
11794 static const gdb::array_view<discriminant_range>
11795 convert_variant_range (struct obstack *obstack, const variant_field &variant,
11796 bool is_unsigned)
11797 {
11798 std::vector<discriminant_range> ranges;
11799
11800 if (variant.default_branch)
11801 return {};
11802
11803 if (variant.discr_list_data == nullptr)
11804 {
11805 discriminant_range r
11806 = {variant.discriminant_value, variant.discriminant_value};
11807 ranges.push_back (r);
11808 }
11809 else
11810 {
11811 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
11812 variant.discr_list_data->size);
11813 while (!data.empty ())
11814 {
11815 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
11816 {
11817 complaint (_("invalid discriminant marker: %d"), data[0]);
11818 break;
11819 }
11820 bool is_range = data[0] == DW_DSC_range;
11821 data = data.slice (1);
11822
11823 ULONGEST low, high;
11824 unsigned int bytes_read;
11825
11826 if (data.empty ())
11827 {
11828 complaint (_("DW_AT_discr_list missing low value"));
11829 break;
11830 }
11831 if (is_unsigned)
11832 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
11833 else
11834 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
11835 &bytes_read);
11836 data = data.slice (bytes_read);
11837
11838 if (is_range)
11839 {
11840 if (data.empty ())
11841 {
11842 complaint (_("DW_AT_discr_list missing high value"));
11843 break;
11844 }
11845 if (is_unsigned)
11846 high = read_unsigned_leb128 (nullptr, data.data (),
11847 &bytes_read);
11848 else
11849 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
11850 &bytes_read);
11851 data = data.slice (bytes_read);
11852 }
11853 else
11854 high = low;
11855
11856 ranges.push_back ({ low, high });
11857 }
11858 }
11859
11860 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
11861 ranges.size ());
11862 std::copy (ranges.begin (), ranges.end (), result);
11863 return gdb::array_view<discriminant_range> (result, ranges.size ());
11864 }
11865
11866 static const gdb::array_view<variant_part> create_variant_parts
11867 (struct obstack *obstack,
11868 const offset_map_type &offset_map,
11869 struct field_info *fi,
11870 const std::vector<variant_part_builder> &variant_parts);
11871
11872 /* Fill in a "struct variant" for a given variant field. RESULT is
11873 the variant to fill in. OBSTACK is where any needed allocations
11874 will be done. OFFSET_MAP holds the mapping from section offsets to
11875 fields for the type. FI describes the fields of the type we're
11876 processing. FIELD is the variant field we're converting. */
11877
11878 static void
11879 create_one_variant (variant &result, struct obstack *obstack,
11880 const offset_map_type &offset_map,
11881 struct field_info *fi, const variant_field &field)
11882 {
11883 result.discriminants = convert_variant_range (obstack, field, false);
11884 result.first_field = field.first_field + fi->baseclasses.size ();
11885 result.last_field = field.last_field + fi->baseclasses.size ();
11886 result.parts = create_variant_parts (obstack, offset_map, fi,
11887 field.variant_parts);
11888 }
11889
11890 /* Fill in a "struct variant_part" for a given variant part. RESULT
11891 is the variant part to fill in. OBSTACK is where any needed
11892 allocations will be done. OFFSET_MAP holds the mapping from
11893 section offsets to fields for the type. FI describes the fields of
11894 the type we're processing. BUILDER is the variant part to be
11895 converted. */
11896
11897 static void
11898 create_one_variant_part (variant_part &result,
11899 struct obstack *obstack,
11900 const offset_map_type &offset_map,
11901 struct field_info *fi,
11902 const variant_part_builder &builder)
11903 {
11904 auto iter = offset_map.find (builder.discriminant_offset);
11905 if (iter == offset_map.end ())
11906 {
11907 result.discriminant_index = -1;
11908 /* Doesn't matter. */
11909 result.is_unsigned = false;
11910 }
11911 else
11912 {
11913 result.discriminant_index = iter->second;
11914 result.is_unsigned
11915 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
11916 }
11917
11918 size_t n = builder.variants.size ();
11919 variant *output = new (obstack) variant[n];
11920 for (size_t i = 0; i < n; ++i)
11921 create_one_variant (output[i], obstack, offset_map, fi,
11922 builder.variants[i]);
11923
11924 result.variants = gdb::array_view<variant> (output, n);
11925 }
11926
11927 /* Create a vector of variant parts that can be attached to a type.
11928 OBSTACK is where any needed allocations will be done. OFFSET_MAP
11929 holds the mapping from section offsets to fields for the type. FI
11930 describes the fields of the type we're processing. VARIANT_PARTS
11931 is the vector to convert. */
11932
11933 static const gdb::array_view<variant_part>
11934 create_variant_parts (struct obstack *obstack,
11935 const offset_map_type &offset_map,
11936 struct field_info *fi,
11937 const std::vector<variant_part_builder> &variant_parts)
11938 {
11939 if (variant_parts.empty ())
11940 return {};
11941
11942 size_t n = variant_parts.size ();
11943 variant_part *result = new (obstack) variant_part[n];
11944 for (size_t i = 0; i < n; ++i)
11945 create_one_variant_part (result[i], obstack, offset_map, fi,
11946 variant_parts[i]);
11947
11948 return gdb::array_view<variant_part> (result, n);
11949 }
11950
11951 /* Compute the variant part vector for FIP, attaching it to TYPE when
11952 done. */
11953
11954 static void
11955 add_variant_property (struct field_info *fip, struct type *type,
11956 struct dwarf2_cu *cu)
11957 {
11958 /* Map section offsets of fields to their field index. Note the
11959 field index here does not take the number of baseclasses into
11960 account. */
11961 offset_map_type offset_map;
11962 for (int i = 0; i < fip->fields.size (); ++i)
11963 offset_map[fip->fields[i].offset] = i;
11964
11965 struct objfile *objfile = cu->per_objfile->objfile;
11966 gdb::array_view<const variant_part> parts
11967 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
11968 fip->variant_parts);
11969
11970 struct dynamic_prop prop;
11971 prop.set_variant_parts ((gdb::array_view<variant_part> *)
11972 obstack_copy (&objfile->objfile_obstack, &parts,
11973 sizeof (parts)));
11974
11975 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
11976 }
11977
11978 /* Create the vector of fields, and attach it to the type. */
11979
11980 static void
11981 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11982 struct dwarf2_cu *cu)
11983 {
11984 int nfields = fip->nfields ();
11985
11986 /* Record the field count, allocate space for the array of fields,
11987 and create blank accessibility bitfields if necessary. */
11988 type->set_num_fields (nfields);
11989 type->set_fields
11990 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
11991
11992 if (fip->non_public_fields && cu->lang () != language_ada)
11993 {
11994 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11995
11996 TYPE_FIELD_PRIVATE_BITS (type) =
11997 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11998 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11999
12000 TYPE_FIELD_PROTECTED_BITS (type) =
12001 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12002 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12003
12004 TYPE_FIELD_IGNORE_BITS (type) =
12005 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12006 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12007 }
12008
12009 /* If the type has baseclasses, allocate and clear a bit vector for
12010 TYPE_FIELD_VIRTUAL_BITS. */
12011 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
12012 {
12013 int num_bytes = B_BYTES (fip->baseclasses.size ());
12014 unsigned char *pointer;
12015
12016 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12017 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
12018 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12019 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
12020 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
12021 }
12022
12023 if (!fip->variant_parts.empty ())
12024 add_variant_property (fip, type, cu);
12025
12026 /* Copy the saved-up fields into the field vector. */
12027 for (int i = 0; i < nfields; ++i)
12028 {
12029 struct nextfield &field
12030 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
12031 : fip->fields[i - fip->baseclasses.size ()]);
12032
12033 type->field (i) = field.field;
12034 switch (field.accessibility)
12035 {
12036 case DW_ACCESS_private:
12037 if (cu->lang () != language_ada)
12038 SET_TYPE_FIELD_PRIVATE (type, i);
12039 break;
12040
12041 case DW_ACCESS_protected:
12042 if (cu->lang () != language_ada)
12043 SET_TYPE_FIELD_PROTECTED (type, i);
12044 break;
12045
12046 case DW_ACCESS_public:
12047 break;
12048
12049 default:
12050 /* Unknown accessibility. Complain and treat it as public. */
12051 {
12052 complaint (_("unsupported accessibility %d"),
12053 field.accessibility);
12054 }
12055 break;
12056 }
12057 if (i < fip->baseclasses.size ())
12058 {
12059 switch (field.virtuality)
12060 {
12061 case DW_VIRTUALITY_virtual:
12062 case DW_VIRTUALITY_pure_virtual:
12063 if (cu->lang () == language_ada)
12064 error (_("unexpected virtuality in component of Ada type"));
12065 SET_TYPE_FIELD_VIRTUAL (type, i);
12066 break;
12067 }
12068 }
12069 }
12070 }
12071
12072 /* Return true if this member function is a constructor, false
12073 otherwise. */
12074
12075 static int
12076 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12077 {
12078 const char *fieldname;
12079 const char *type_name;
12080 int len;
12081
12082 if (die->parent == NULL)
12083 return 0;
12084
12085 if (die->parent->tag != DW_TAG_structure_type
12086 && die->parent->tag != DW_TAG_union_type
12087 && die->parent->tag != DW_TAG_class_type)
12088 return 0;
12089
12090 fieldname = dwarf2_name (die, cu);
12091 type_name = dwarf2_name (die->parent, cu);
12092 if (fieldname == NULL || type_name == NULL)
12093 return 0;
12094
12095 len = strlen (fieldname);
12096 return (strncmp (fieldname, type_name, len) == 0
12097 && (type_name[len] == '\0' || type_name[len] == '<'));
12098 }
12099
12100 /* Add a member function to the proper fieldlist. */
12101
12102 static void
12103 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12104 struct type *type, struct dwarf2_cu *cu)
12105 {
12106 struct objfile *objfile = cu->per_objfile->objfile;
12107 struct attribute *attr;
12108 int i;
12109 struct fnfieldlist *flp = nullptr;
12110 struct fn_field *fnp;
12111 const char *fieldname;
12112 struct type *this_type;
12113
12114 if (cu->lang () == language_ada)
12115 error (_("unexpected member function in Ada type"));
12116
12117 /* Get name of member function. */
12118 fieldname = dwarf2_name (die, cu);
12119 if (fieldname == NULL)
12120 return;
12121
12122 /* Look up member function name in fieldlist. */
12123 for (i = 0; i < fip->fnfieldlists.size (); i++)
12124 {
12125 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12126 {
12127 flp = &fip->fnfieldlists[i];
12128 break;
12129 }
12130 }
12131
12132 /* Create a new fnfieldlist if necessary. */
12133 if (flp == nullptr)
12134 {
12135 fip->fnfieldlists.emplace_back ();
12136 flp = &fip->fnfieldlists.back ();
12137 flp->name = fieldname;
12138 i = fip->fnfieldlists.size () - 1;
12139 }
12140
12141 /* Create a new member function field and add it to the vector of
12142 fnfieldlists. */
12143 flp->fnfields.emplace_back ();
12144 fnp = &flp->fnfields.back ();
12145
12146 /* Delay processing of the physname until later. */
12147 if (cu->lang () == language_cplus)
12148 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
12149 die, cu);
12150 else
12151 {
12152 const char *physname = dwarf2_physname (fieldname, die, cu);
12153 fnp->physname = physname ? physname : "";
12154 }
12155
12156 fnp->type = alloc_type (objfile);
12157 this_type = read_type_die (die, cu);
12158 if (this_type && this_type->code () == TYPE_CODE_FUNC)
12159 {
12160 int nparams = this_type->num_fields ();
12161
12162 /* TYPE is the domain of this method, and THIS_TYPE is the type
12163 of the method itself (TYPE_CODE_METHOD). */
12164 smash_to_method_type (fnp->type, type,
12165 this_type->target_type (),
12166 this_type->fields (),
12167 this_type->num_fields (),
12168 this_type->has_varargs ());
12169
12170 /* Handle static member functions.
12171 Dwarf2 has no clean way to discern C++ static and non-static
12172 member functions. G++ helps GDB by marking the first
12173 parameter for non-static member functions (which is the this
12174 pointer) as artificial. We obtain this information from
12175 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12176 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
12177 fnp->voffset = VOFFSET_STATIC;
12178 }
12179 else
12180 complaint (_("member function type missing for '%s'"),
12181 dwarf2_full_name (fieldname, die, cu));
12182
12183 /* Get fcontext from DW_AT_containing_type if present. */
12184 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12185 fnp->fcontext = die_containing_type (die, cu);
12186
12187 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12188 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12189
12190 /* Get accessibility. */
12191 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
12192 switch (accessibility)
12193 {
12194 case DW_ACCESS_private:
12195 fnp->is_private = 1;
12196 break;
12197 case DW_ACCESS_protected:
12198 fnp->is_protected = 1;
12199 break;
12200 }
12201
12202 /* Check for artificial methods. */
12203 attr = dwarf2_attr (die, DW_AT_artificial, cu);
12204 if (attr && attr->as_boolean ())
12205 fnp->is_artificial = 1;
12206
12207 /* Check for defaulted methods. */
12208 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
12209 if (attr != nullptr)
12210 fnp->defaulted = attr->defaulted ();
12211
12212 /* Check for deleted methods. */
12213 attr = dwarf2_attr (die, DW_AT_deleted, cu);
12214 if (attr != nullptr && attr->as_boolean ())
12215 fnp->is_deleted = 1;
12216
12217 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12218
12219 /* Get index in virtual function table if it is a virtual member
12220 function. For older versions of GCC, this is an offset in the
12221 appropriate virtual table, as specified by DW_AT_containing_type.
12222 For everyone else, it is an expression to be evaluated relative
12223 to the object address. */
12224
12225 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12226 if (attr != nullptr)
12227 {
12228 if (attr->form_is_block () && attr->as_block ()->size > 0)
12229 {
12230 struct dwarf_block *block = attr->as_block ();
12231
12232 if (block->data[0] == DW_OP_constu)
12233 {
12234 /* Old-style GCC. */
12235 fnp->voffset = decode_locdesc (block, cu) + 2;
12236 }
12237 else if (block->data[0] == DW_OP_deref
12238 || (block->size > 1
12239 && block->data[0] == DW_OP_deref_size
12240 && block->data[1] == cu->header.addr_size))
12241 {
12242 fnp->voffset = decode_locdesc (block, cu);
12243 if ((fnp->voffset % cu->header.addr_size) != 0)
12244 dwarf2_complex_location_expr_complaint ();
12245 else
12246 fnp->voffset /= cu->header.addr_size;
12247 fnp->voffset += 2;
12248 }
12249 else
12250 dwarf2_complex_location_expr_complaint ();
12251
12252 if (!fnp->fcontext)
12253 {
12254 /* If there is no `this' field and no DW_AT_containing_type,
12255 we cannot actually find a base class context for the
12256 vtable! */
12257 if (this_type->num_fields () == 0
12258 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
12259 {
12260 complaint (_("cannot determine context for virtual member "
12261 "function \"%s\" (offset %s)"),
12262 fieldname, sect_offset_str (die->sect_off));
12263 }
12264 else
12265 {
12266 fnp->fcontext = this_type->field (0).type ()->target_type ();
12267 }
12268 }
12269 }
12270 else if (attr->form_is_section_offset ())
12271 {
12272 dwarf2_complex_location_expr_complaint ();
12273 }
12274 else
12275 {
12276 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12277 fieldname);
12278 }
12279 }
12280 else
12281 {
12282 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12283 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
12284 {
12285 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12286 complaint (_("Member function \"%s\" (offset %s) is virtual "
12287 "but the vtable offset is not specified"),
12288 fieldname, sect_offset_str (die->sect_off));
12289 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12290 TYPE_CPLUS_DYNAMIC (type) = 1;
12291 }
12292 }
12293 }
12294
12295 /* Create the vector of member function fields, and attach it to the type. */
12296
12297 static void
12298 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
12299 struct dwarf2_cu *cu)
12300 {
12301 if (cu->lang () == language_ada)
12302 error (_("unexpected member functions in Ada type"));
12303
12304 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12305 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
12306 TYPE_ALLOC (type,
12307 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
12308
12309 for (int i = 0; i < fip->fnfieldlists.size (); i++)
12310 {
12311 struct fnfieldlist &nf = fip->fnfieldlists[i];
12312 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
12313
12314 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
12315 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
12316 fn_flp->fn_fields = (struct fn_field *)
12317 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
12318
12319 for (int k = 0; k < nf.fnfields.size (); ++k)
12320 fn_flp->fn_fields[k] = nf.fnfields[k];
12321 }
12322
12323 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
12324 }
12325
12326 /* Returns non-zero if NAME is the name of a vtable member in CU's
12327 language, zero otherwise. */
12328 static int
12329 is_vtable_name (const char *name, struct dwarf2_cu *cu)
12330 {
12331 static const char vptr[] = "_vptr";
12332
12333 /* Look for the C++ form of the vtable. */
12334 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
12335 return 1;
12336
12337 return 0;
12338 }
12339
12340 /* GCC outputs unnamed structures that are really pointers to member
12341 functions, with the ABI-specified layout. If TYPE describes
12342 such a structure, smash it into a member function type.
12343
12344 GCC shouldn't do this; it should just output pointer to member DIEs.
12345 This is GCC PR debug/28767. */
12346
12347 static void
12348 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
12349 {
12350 struct type *pfn_type, *self_type, *new_type;
12351
12352 /* Check for a structure with no name and two children. */
12353 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12354 return;
12355
12356 /* Check for __pfn and __delta members. */
12357 if (type->field (0).name () == NULL
12358 || strcmp (type->field (0).name (), "__pfn") != 0
12359 || type->field (1).name () == NULL
12360 || strcmp (type->field (1).name (), "__delta") != 0)
12361 return;
12362
12363 /* Find the type of the method. */
12364 pfn_type = type->field (0).type ();
12365 if (pfn_type == NULL
12366 || pfn_type->code () != TYPE_CODE_PTR
12367 || pfn_type->target_type ()->code () != TYPE_CODE_FUNC)
12368 return;
12369
12370 /* Look for the "this" argument. */
12371 pfn_type = pfn_type->target_type ();
12372 if (pfn_type->num_fields () == 0
12373 /* || pfn_type->field (0).type () == NULL */
12374 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
12375 return;
12376
12377 self_type = pfn_type->field (0).type ()->target_type ();
12378 new_type = alloc_type (objfile);
12379 smash_to_method_type (new_type, self_type, pfn_type->target_type (),
12380 pfn_type->fields (), pfn_type->num_fields (),
12381 pfn_type->has_varargs ());
12382 smash_to_methodptr_type (type, new_type);
12383 }
12384
12385 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
12386 requires rewriting, then copy it and return the updated copy.
12387 Otherwise return nullptr. */
12388
12389 static struct type *
12390 rewrite_array_type (struct type *type)
12391 {
12392 if (type->code () != TYPE_CODE_ARRAY)
12393 return nullptr;
12394
12395 struct type *index_type = type->index_type ();
12396 range_bounds *current_bounds = index_type->bounds ();
12397
12398 /* Handle multi-dimensional arrays. */
12399 struct type *new_target = rewrite_array_type (type->target_type ());
12400 if (new_target == nullptr)
12401 {
12402 /* Maybe we don't need to rewrite this array. */
12403 if (current_bounds->low.kind () == PROP_CONST
12404 && current_bounds->high.kind () == PROP_CONST)
12405 return nullptr;
12406 }
12407
12408 /* Either the target type was rewritten, or the bounds have to be
12409 updated. Either way we want to copy the type and update
12410 everything. */
12411 struct type *copy = copy_type (type);
12412 int nfields = copy->num_fields ();
12413 field *new_fields
12414 = ((struct field *) TYPE_ZALLOC (copy,
12415 nfields * sizeof (struct field)));
12416 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
12417 copy->set_fields (new_fields);
12418 if (new_target != nullptr)
12419 copy->set_target_type (new_target);
12420
12421 struct type *index_copy = copy_type (index_type);
12422 range_bounds *bounds
12423 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
12424 sizeof (range_bounds));
12425 *bounds = *current_bounds;
12426 bounds->low.set_const_val (1);
12427 bounds->high.set_const_val (0);
12428 index_copy->set_bounds (bounds);
12429 copy->set_index_type (index_copy);
12430
12431 return copy;
12432 }
12433
12434 /* While some versions of GCC will generate complicated DWARF for an
12435 array (see quirk_ada_thick_pointer), more recent versions were
12436 modified to emit an explicit thick pointer structure. However, in
12437 this case, the array still has DWARF expressions for its ranges,
12438 and these must be ignored. */
12439
12440 static void
12441 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
12442 struct type *type)
12443 {
12444 gdb_assert (cu->lang () == language_ada);
12445
12446 /* Check for a structure with two children. */
12447 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12448 return;
12449
12450 /* Check for P_ARRAY and P_BOUNDS members. */
12451 if (type->field (0).name () == NULL
12452 || strcmp (type->field (0).name (), "P_ARRAY") != 0
12453 || type->field (1).name () == NULL
12454 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
12455 return;
12456
12457 /* Make sure we're looking at a pointer to an array. */
12458 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
12459 return;
12460
12461 /* The Ada code already knows how to handle these types, so all that
12462 we need to do is turn the bounds into static bounds. However, we
12463 don't want to rewrite existing array or index types in-place,
12464 because those may be referenced in other contexts where this
12465 rewriting is undesirable. */
12466 struct type *new_ary_type
12467 = rewrite_array_type (type->field (0).type ()->target_type ());
12468 if (new_ary_type != nullptr)
12469 type->field (0).set_type (lookup_pointer_type (new_ary_type));
12470 }
12471
12472 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
12473 appropriate error checking and issuing complaints if there is a
12474 problem. */
12475
12476 static ULONGEST
12477 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
12478 {
12479 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
12480
12481 if (attr == nullptr)
12482 return 0;
12483
12484 if (!attr->form_is_constant ())
12485 {
12486 complaint (_("DW_AT_alignment must have constant form"
12487 " - DIE at %s [in module %s]"),
12488 sect_offset_str (die->sect_off),
12489 objfile_name (cu->per_objfile->objfile));
12490 return 0;
12491 }
12492
12493 LONGEST val = attr->constant_value (0);
12494 if (val < 0)
12495 {
12496 complaint (_("DW_AT_alignment value must not be negative"
12497 " - DIE at %s [in module %s]"),
12498 sect_offset_str (die->sect_off),
12499 objfile_name (cu->per_objfile->objfile));
12500 return 0;
12501 }
12502 ULONGEST align = val;
12503
12504 if (align == 0)
12505 {
12506 complaint (_("DW_AT_alignment value must not be zero"
12507 " - DIE at %s [in module %s]"),
12508 sect_offset_str (die->sect_off),
12509 objfile_name (cu->per_objfile->objfile));
12510 return 0;
12511 }
12512 if ((align & (align - 1)) != 0)
12513 {
12514 complaint (_("DW_AT_alignment value must be a power of 2"
12515 " - DIE at %s [in module %s]"),
12516 sect_offset_str (die->sect_off),
12517 objfile_name (cu->per_objfile->objfile));
12518 return 0;
12519 }
12520
12521 return align;
12522 }
12523
12524 /* If the DIE has a DW_AT_alignment attribute, use its value to set
12525 the alignment for TYPE. */
12526
12527 static void
12528 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
12529 struct type *type)
12530 {
12531 if (!set_type_align (type, get_alignment (cu, die)))
12532 complaint (_("DW_AT_alignment value too large"
12533 " - DIE at %s [in module %s]"),
12534 sect_offset_str (die->sect_off),
12535 objfile_name (cu->per_objfile->objfile));
12536 }
12537
12538 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12539 constant for a type, according to DWARF5 spec, Table 5.5. */
12540
12541 static bool
12542 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
12543 {
12544 switch (value)
12545 {
12546 case DW_CC_normal:
12547 case DW_CC_pass_by_reference:
12548 case DW_CC_pass_by_value:
12549 return true;
12550
12551 default:
12552 complaint (_("unrecognized DW_AT_calling_convention value "
12553 "(%s) for a type"), pulongest (value));
12554 return false;
12555 }
12556 }
12557
12558 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12559 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
12560 also according to GNU-specific values (see include/dwarf2.h). */
12561
12562 static bool
12563 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
12564 {
12565 switch (value)
12566 {
12567 case DW_CC_normal:
12568 case DW_CC_program:
12569 case DW_CC_nocall:
12570 return true;
12571
12572 case DW_CC_GNU_renesas_sh:
12573 case DW_CC_GNU_borland_fastcall_i386:
12574 case DW_CC_GDB_IBM_OpenCL:
12575 return true;
12576
12577 default:
12578 complaint (_("unrecognized DW_AT_calling_convention value "
12579 "(%s) for a subroutine"), pulongest (value));
12580 return false;
12581 }
12582 }
12583
12584 /* Called when we find the DIE that starts a structure or union scope
12585 (definition) to create a type for the structure or union. Fill in
12586 the type's name and general properties; the members will not be
12587 processed until process_structure_scope. A symbol table entry for
12588 the type will also not be done until process_structure_scope (assuming
12589 the type has a name).
12590
12591 NOTE: we need to call these functions regardless of whether or not the
12592 DIE has a DW_AT_name attribute, since it might be an anonymous
12593 structure or union. This gets the type entered into our set of
12594 user defined types. */
12595
12596 static struct type *
12597 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
12598 {
12599 struct objfile *objfile = cu->per_objfile->objfile;
12600 struct type *type;
12601 struct attribute *attr;
12602 const char *name;
12603
12604 /* If the definition of this type lives in .debug_types, read that type.
12605 Don't follow DW_AT_specification though, that will take us back up
12606 the chain and we want to go down. */
12607 attr = die->attr (DW_AT_signature);
12608 if (attr != nullptr)
12609 {
12610 type = get_DW_AT_signature_type (die, attr, cu);
12611
12612 /* The type's CU may not be the same as CU.
12613 Ensure TYPE is recorded with CU in die_type_hash. */
12614 return set_die_type (die, type, cu);
12615 }
12616
12617 type = alloc_type (objfile);
12618 INIT_CPLUS_SPECIFIC (type);
12619
12620 name = dwarf2_name (die, cu);
12621 if (name != NULL)
12622 {
12623 if (cu->lang () == language_cplus
12624 || cu->lang () == language_d
12625 || cu->lang () == language_rust)
12626 {
12627 const char *full_name = dwarf2_full_name (name, die, cu);
12628
12629 /* dwarf2_full_name might have already finished building the DIE's
12630 type. If so, there is no need to continue. */
12631 if (get_die_type (die, cu) != NULL)
12632 return get_die_type (die, cu);
12633
12634 type->set_name (full_name);
12635 }
12636 else
12637 {
12638 /* The name is already allocated along with this objfile, so
12639 we don't need to duplicate it for the type. */
12640 type->set_name (name);
12641 }
12642 }
12643
12644 if (die->tag == DW_TAG_structure_type)
12645 {
12646 type->set_code (TYPE_CODE_STRUCT);
12647 }
12648 else if (die->tag == DW_TAG_union_type)
12649 {
12650 type->set_code (TYPE_CODE_UNION);
12651 }
12652 else if (die->tag == DW_TAG_namelist)
12653 {
12654 type->set_code (TYPE_CODE_NAMELIST);
12655 }
12656 else
12657 {
12658 type->set_code (TYPE_CODE_STRUCT);
12659 }
12660
12661 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
12662 type->set_is_declared_class (true);
12663
12664 /* Store the calling convention in the type if it's available in
12665 the die. Otherwise the calling convention remains set to
12666 the default value DW_CC_normal. */
12667 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12668 if (attr != nullptr
12669 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
12670 {
12671 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12672 TYPE_CPLUS_CALLING_CONVENTION (type)
12673 = (enum dwarf_calling_convention) (attr->constant_value (0));
12674 }
12675
12676 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12677 if (attr != nullptr)
12678 {
12679 if (attr->form_is_constant ())
12680 type->set_length (attr->constant_value (0));
12681 else
12682 {
12683 struct dynamic_prop prop;
12684 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
12685 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
12686
12687 type->set_length (0);
12688 }
12689 }
12690 else
12691 type->set_length (0);
12692
12693 maybe_set_alignment (cu, die, type);
12694
12695 if (producer_is_icc_lt_14 (cu) && (type->length () == 0))
12696 {
12697 /* ICC<14 does not output the required DW_AT_declaration on
12698 incomplete types, but gives them a size of zero. */
12699 type->set_is_stub (true);
12700 }
12701 else
12702 type->set_stub_is_supported (true);
12703
12704 if (die_is_declaration (die, cu))
12705 type->set_is_stub (true);
12706 else if (attr == NULL && die->child == NULL
12707 && producer_is_realview (cu->producer))
12708 /* RealView does not output the required DW_AT_declaration
12709 on incomplete types. */
12710 type->set_is_stub (true);
12711
12712 /* We need to add the type field to the die immediately so we don't
12713 infinitely recurse when dealing with pointers to the structure
12714 type within the structure itself. */
12715 set_die_type (die, type, cu);
12716
12717 /* set_die_type should be already done. */
12718 set_descriptive_type (type, die, cu);
12719
12720 return type;
12721 }
12722
12723 static void handle_struct_member_die
12724 (struct die_info *child_die,
12725 struct type *type,
12726 struct field_info *fi,
12727 std::vector<struct symbol *> *template_args,
12728 struct dwarf2_cu *cu);
12729
12730 /* A helper for handle_struct_member_die that handles
12731 DW_TAG_variant_part. */
12732
12733 static void
12734 handle_variant_part (struct die_info *die, struct type *type,
12735 struct field_info *fi,
12736 std::vector<struct symbol *> *template_args,
12737 struct dwarf2_cu *cu)
12738 {
12739 variant_part_builder *new_part;
12740 if (fi->current_variant_part == nullptr)
12741 {
12742 fi->variant_parts.emplace_back ();
12743 new_part = &fi->variant_parts.back ();
12744 }
12745 else if (!fi->current_variant_part->processing_variant)
12746 {
12747 complaint (_("nested DW_TAG_variant_part seen "
12748 "- DIE at %s [in module %s]"),
12749 sect_offset_str (die->sect_off),
12750 objfile_name (cu->per_objfile->objfile));
12751 return;
12752 }
12753 else
12754 {
12755 variant_field &current = fi->current_variant_part->variants.back ();
12756 current.variant_parts.emplace_back ();
12757 new_part = &current.variant_parts.back ();
12758 }
12759
12760 /* When we recurse, we want callees to add to this new variant
12761 part. */
12762 scoped_restore save_current_variant_part
12763 = make_scoped_restore (&fi->current_variant_part, new_part);
12764
12765 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
12766 if (discr == NULL)
12767 {
12768 /* It's a univariant form, an extension we support. */
12769 }
12770 else if (discr->form_is_ref ())
12771 {
12772 struct dwarf2_cu *target_cu = cu;
12773 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
12774
12775 new_part->discriminant_offset = target_die->sect_off;
12776 }
12777 else
12778 {
12779 complaint (_("DW_AT_discr does not have DIE reference form"
12780 " - DIE at %s [in module %s]"),
12781 sect_offset_str (die->sect_off),
12782 objfile_name (cu->per_objfile->objfile));
12783 }
12784
12785 for (die_info *child_die = die->child;
12786 child_die != NULL;
12787 child_die = child_die->sibling)
12788 handle_struct_member_die (child_die, type, fi, template_args, cu);
12789 }
12790
12791 /* A helper for handle_struct_member_die that handles
12792 DW_TAG_variant. */
12793
12794 static void
12795 handle_variant (struct die_info *die, struct type *type,
12796 struct field_info *fi,
12797 std::vector<struct symbol *> *template_args,
12798 struct dwarf2_cu *cu)
12799 {
12800 if (fi->current_variant_part == nullptr)
12801 {
12802 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
12803 "- DIE at %s [in module %s]"),
12804 sect_offset_str (die->sect_off),
12805 objfile_name (cu->per_objfile->objfile));
12806 return;
12807 }
12808 if (fi->current_variant_part->processing_variant)
12809 {
12810 complaint (_("nested DW_TAG_variant seen "
12811 "- DIE at %s [in module %s]"),
12812 sect_offset_str (die->sect_off),
12813 objfile_name (cu->per_objfile->objfile));
12814 return;
12815 }
12816
12817 scoped_restore save_processing_variant
12818 = make_scoped_restore (&fi->current_variant_part->processing_variant,
12819 true);
12820
12821 fi->current_variant_part->variants.emplace_back ();
12822 variant_field &variant = fi->current_variant_part->variants.back ();
12823 variant.first_field = fi->fields.size ();
12824
12825 /* In a variant we want to get the discriminant and also add a
12826 field for our sole member child. */
12827 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
12828 if (discr == nullptr || !discr->form_is_constant ())
12829 {
12830 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
12831 if (discr == nullptr || discr->as_block ()->size == 0)
12832 variant.default_branch = true;
12833 else
12834 variant.discr_list_data = discr->as_block ();
12835 }
12836 else
12837 variant.discriminant_value = discr->constant_value (0);
12838
12839 for (die_info *variant_child = die->child;
12840 variant_child != NULL;
12841 variant_child = variant_child->sibling)
12842 handle_struct_member_die (variant_child, type, fi, template_args, cu);
12843
12844 variant.last_field = fi->fields.size ();
12845 }
12846
12847 /* A helper for process_structure_scope that handles a single member
12848 DIE. */
12849
12850 static void
12851 handle_struct_member_die (struct die_info *child_die, struct type *type,
12852 struct field_info *fi,
12853 std::vector<struct symbol *> *template_args,
12854 struct dwarf2_cu *cu)
12855 {
12856 if (child_die->tag == DW_TAG_member
12857 || child_die->tag == DW_TAG_variable
12858 || child_die->tag == DW_TAG_namelist_item)
12859 {
12860 /* NOTE: carlton/2002-11-05: A C++ static data member
12861 should be a DW_TAG_member that is a declaration, but
12862 all versions of G++ as of this writing (so through at
12863 least 3.2.1) incorrectly generate DW_TAG_variable
12864 tags for them instead. */
12865 dwarf2_add_field (fi, child_die, cu);
12866 }
12867 else if (child_die->tag == DW_TAG_subprogram)
12868 {
12869 /* Rust doesn't have member functions in the C++ sense.
12870 However, it does emit ordinary functions as children
12871 of a struct DIE. */
12872 if (cu->lang () == language_rust)
12873 read_func_scope (child_die, cu);
12874 else
12875 {
12876 /* C++ member function. */
12877 dwarf2_add_member_fn (fi, child_die, type, cu);
12878 }
12879 }
12880 else if (child_die->tag == DW_TAG_inheritance)
12881 {
12882 /* C++ base class field. */
12883 dwarf2_add_field (fi, child_die, cu);
12884 }
12885 else if (type_can_define_types (child_die))
12886 dwarf2_add_type_defn (fi, child_die, cu);
12887 else if (child_die->tag == DW_TAG_template_type_param
12888 || child_die->tag == DW_TAG_template_value_param)
12889 {
12890 struct symbol *arg = new_symbol (child_die, NULL, cu);
12891
12892 if (arg != NULL)
12893 template_args->push_back (arg);
12894 }
12895 else if (child_die->tag == DW_TAG_variant_part)
12896 handle_variant_part (child_die, type, fi, template_args, cu);
12897 else if (child_die->tag == DW_TAG_variant)
12898 handle_variant (child_die, type, fi, template_args, cu);
12899 }
12900
12901 /* Finish creating a structure or union type, including filling in its
12902 members and creating a symbol for it. This function also handles Fortran
12903 namelist variables, their items or members and creating a symbol for
12904 them. */
12905
12906 static void
12907 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12908 {
12909 struct objfile *objfile = cu->per_objfile->objfile;
12910 struct die_info *child_die;
12911 struct type *type;
12912
12913 type = get_die_type (die, cu);
12914 if (type == NULL)
12915 type = read_structure_type (die, cu);
12916
12917 bool has_template_parameters = false;
12918 if (die->child != NULL && ! die_is_declaration (die, cu))
12919 {
12920 struct field_info fi;
12921 std::vector<struct symbol *> template_args;
12922
12923 child_die = die->child;
12924
12925 while (child_die && child_die->tag)
12926 {
12927 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
12928 child_die = child_die->sibling;
12929 }
12930
12931 /* Attach template arguments to type. */
12932 if (!template_args.empty ())
12933 {
12934 has_template_parameters = true;
12935 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12936 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
12937 TYPE_TEMPLATE_ARGUMENTS (type)
12938 = XOBNEWVEC (&objfile->objfile_obstack,
12939 struct symbol *,
12940 TYPE_N_TEMPLATE_ARGUMENTS (type));
12941 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12942 template_args.data (),
12943 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12944 * sizeof (struct symbol *)));
12945 }
12946
12947 /* Attach fields and member functions to the type. */
12948 if (fi.nfields () > 0)
12949 dwarf2_attach_fields_to_type (&fi, type, cu);
12950 if (!fi.fnfieldlists.empty ())
12951 {
12952 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12953
12954 /* Get the type which refers to the base class (possibly this
12955 class itself) which contains the vtable pointer for the current
12956 class from the DW_AT_containing_type attribute. This use of
12957 DW_AT_containing_type is a GNU extension. */
12958
12959 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12960 {
12961 struct type *t = die_containing_type (die, cu);
12962
12963 set_type_vptr_basetype (type, t);
12964 if (type == t)
12965 {
12966 int i;
12967
12968 /* Our own class provides vtbl ptr. */
12969 for (i = t->num_fields () - 1;
12970 i >= TYPE_N_BASECLASSES (t);
12971 --i)
12972 {
12973 const char *fieldname = t->field (i).name ();
12974
12975 if (is_vtable_name (fieldname, cu))
12976 {
12977 set_type_vptr_fieldno (type, i);
12978 break;
12979 }
12980 }
12981
12982 /* Complain if virtual function table field not found. */
12983 if (i < TYPE_N_BASECLASSES (t))
12984 complaint (_("virtual function table pointer "
12985 "not found when defining class '%s'"),
12986 type->name () ? type->name () : "");
12987 }
12988 else
12989 {
12990 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
12991 }
12992 }
12993 else if (cu->producer
12994 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
12995 {
12996 /* The IBM XLC compiler does not provide direct indication
12997 of the containing type, but the vtable pointer is
12998 always named __vfp. */
12999
13000 int i;
13001
13002 for (i = type->num_fields () - 1;
13003 i >= TYPE_N_BASECLASSES (type);
13004 --i)
13005 {
13006 if (strcmp (type->field (i).name (), "__vfp") == 0)
13007 {
13008 set_type_vptr_fieldno (type, i);
13009 set_type_vptr_basetype (type, type);
13010 break;
13011 }
13012 }
13013 }
13014 }
13015
13016 /* Copy fi.typedef_field_list linked list elements content into the
13017 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13018 if (!fi.typedef_field_list.empty ())
13019 {
13020 int count = fi.typedef_field_list.size ();
13021
13022 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13023 TYPE_TYPEDEF_FIELD_ARRAY (type)
13024 = ((struct decl_field *)
13025 TYPE_ALLOC (type,
13026 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
13027 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
13028
13029 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
13030 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
13031 }
13032
13033 /* Copy fi.nested_types_list linked list elements content into the
13034 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
13035 if (!fi.nested_types_list.empty ()
13036 && cu->lang () != language_ada)
13037 {
13038 int count = fi.nested_types_list.size ();
13039
13040 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13041 TYPE_NESTED_TYPES_ARRAY (type)
13042 = ((struct decl_field *)
13043 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
13044 TYPE_NESTED_TYPES_COUNT (type) = count;
13045
13046 for (int i = 0; i < fi.nested_types_list.size (); ++i)
13047 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
13048 }
13049 }
13050
13051 quirk_gcc_member_function_pointer (type, objfile);
13052 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
13053 cu->rust_unions.push_back (type);
13054 else if (cu->lang () == language_ada)
13055 quirk_ada_thick_pointer_struct (die, cu, type);
13056
13057 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13058 snapshots) has been known to create a die giving a declaration
13059 for a class that has, as a child, a die giving a definition for a
13060 nested class. So we have to process our children even if the
13061 current die is a declaration. Normally, of course, a declaration
13062 won't have any children at all. */
13063
13064 child_die = die->child;
13065
13066 while (child_die != NULL && child_die->tag)
13067 {
13068 if (child_die->tag == DW_TAG_member
13069 || child_die->tag == DW_TAG_variable
13070 || child_die->tag == DW_TAG_inheritance
13071 || child_die->tag == DW_TAG_template_value_param
13072 || child_die->tag == DW_TAG_template_type_param)
13073 {
13074 /* Do nothing. */
13075 }
13076 else
13077 process_die (child_die, cu);
13078
13079 child_die = child_die->sibling;
13080 }
13081
13082 /* Do not consider external references. According to the DWARF standard,
13083 these DIEs are identified by the fact that they have no byte_size
13084 attribute, and a declaration attribute. */
13085 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13086 || !die_is_declaration (die, cu)
13087 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
13088 {
13089 struct symbol *sym = new_symbol (die, type, cu);
13090
13091 if (has_template_parameters)
13092 {
13093 struct symtab *symtab;
13094 if (sym != nullptr)
13095 symtab = sym->symtab ();
13096 else if (cu->line_header != nullptr)
13097 {
13098 /* Any related symtab will do. */
13099 symtab
13100 = cu->line_header->file_names ()[0].symtab;
13101 }
13102 else
13103 {
13104 symtab = nullptr;
13105 complaint (_("could not find suitable "
13106 "symtab for template parameter"
13107 " - DIE at %s [in module %s]"),
13108 sect_offset_str (die->sect_off),
13109 objfile_name (objfile));
13110 }
13111
13112 if (symtab != nullptr)
13113 {
13114 /* Make sure that the symtab is set on the new symbols.
13115 Even though they don't appear in this symtab directly,
13116 other parts of gdb assume that symbols do, and this is
13117 reasonably true. */
13118 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
13119 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
13120 }
13121 }
13122 }
13123 }
13124
13125 /* Assuming DIE is an enumeration type, and TYPE is its associated
13126 type, update TYPE using some information only available in DIE's
13127 children. In particular, the fields are computed. */
13128
13129 static void
13130 update_enumeration_type_from_children (struct die_info *die,
13131 struct type *type,
13132 struct dwarf2_cu *cu)
13133 {
13134 struct die_info *child_die;
13135 int unsigned_enum = 1;
13136 int flag_enum = 1;
13137
13138 auto_obstack obstack;
13139 std::vector<struct field> fields;
13140
13141 for (child_die = die->child;
13142 child_die != NULL && child_die->tag;
13143 child_die = child_die->sibling)
13144 {
13145 struct attribute *attr;
13146 LONGEST value;
13147 const gdb_byte *bytes;
13148 struct dwarf2_locexpr_baton *baton;
13149 const char *name;
13150
13151 if (child_die->tag != DW_TAG_enumerator)
13152 continue;
13153
13154 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13155 if (attr == NULL)
13156 continue;
13157
13158 name = dwarf2_name (child_die, cu);
13159 if (name == NULL)
13160 name = "<anonymous enumerator>";
13161
13162 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13163 &value, &bytes, &baton);
13164 if (value < 0)
13165 {
13166 unsigned_enum = 0;
13167 flag_enum = 0;
13168 }
13169 else
13170 {
13171 if (count_one_bits_ll (value) >= 2)
13172 flag_enum = 0;
13173 }
13174
13175 fields.emplace_back ();
13176 struct field &field = fields.back ();
13177 field.set_name (dwarf2_physname (name, child_die, cu));
13178 field.set_loc_enumval (value);
13179 }
13180
13181 if (!fields.empty ())
13182 {
13183 type->set_num_fields (fields.size ());
13184 type->set_fields
13185 ((struct field *)
13186 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
13187 memcpy (type->fields (), fields.data (),
13188 sizeof (struct field) * fields.size ());
13189 }
13190
13191 if (unsigned_enum)
13192 type->set_is_unsigned (true);
13193
13194 if (flag_enum)
13195 type->set_is_flag_enum (true);
13196 }
13197
13198 /* Given a DW_AT_enumeration_type die, set its type. We do not
13199 complete the type's fields yet, or create any symbols. */
13200
13201 static struct type *
13202 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13203 {
13204 struct objfile *objfile = cu->per_objfile->objfile;
13205 struct type *type;
13206 struct attribute *attr;
13207 const char *name;
13208
13209 /* If the definition of this type lives in .debug_types, read that type.
13210 Don't follow DW_AT_specification though, that will take us back up
13211 the chain and we want to go down. */
13212 attr = die->attr (DW_AT_signature);
13213 if (attr != nullptr)
13214 {
13215 type = get_DW_AT_signature_type (die, attr, cu);
13216
13217 /* The type's CU may not be the same as CU.
13218 Ensure TYPE is recorded with CU in die_type_hash. */
13219 return set_die_type (die, type, cu);
13220 }
13221
13222 type = alloc_type (objfile);
13223
13224 type->set_code (TYPE_CODE_ENUM);
13225 name = dwarf2_full_name (NULL, die, cu);
13226 if (name != NULL)
13227 type->set_name (name);
13228
13229 attr = dwarf2_attr (die, DW_AT_type, cu);
13230 if (attr != NULL)
13231 {
13232 struct type *underlying_type = die_type (die, cu);
13233
13234 type->set_target_type (underlying_type);
13235 }
13236
13237 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13238 if (attr != nullptr)
13239 type->set_length (attr->constant_value (0));
13240 else
13241 type->set_length (0);
13242
13243 maybe_set_alignment (cu, die, type);
13244
13245 /* The enumeration DIE can be incomplete. In Ada, any type can be
13246 declared as private in the package spec, and then defined only
13247 inside the package body. Such types are known as Taft Amendment
13248 Types. When another package uses such a type, an incomplete DIE
13249 may be generated by the compiler. */
13250 if (die_is_declaration (die, cu))
13251 type->set_is_stub (true);
13252
13253 /* If this type has an underlying type that is not a stub, then we
13254 may use its attributes. We always use the "unsigned" attribute
13255 in this situation, because ordinarily we guess whether the type
13256 is unsigned -- but the guess can be wrong and the underlying type
13257 can tell us the reality. However, we defer to a local size
13258 attribute if one exists, because this lets the compiler override
13259 the underlying type if needed. */
13260 if (type->target_type () != NULL && !type->target_type ()->is_stub ())
13261 {
13262 struct type *underlying_type = type->target_type ();
13263 underlying_type = check_typedef (underlying_type);
13264
13265 type->set_is_unsigned (underlying_type->is_unsigned ());
13266
13267 if (type->length () == 0)
13268 type->set_length (underlying_type->length ());
13269
13270 if (TYPE_RAW_ALIGN (type) == 0
13271 && TYPE_RAW_ALIGN (underlying_type) != 0)
13272 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
13273 }
13274
13275 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
13276
13277 set_die_type (die, type, cu);
13278
13279 /* Finish the creation of this type by using the enum's children.
13280 Note that, as usual, this must come after set_die_type to avoid
13281 infinite recursion when trying to compute the names of the
13282 enumerators. */
13283 update_enumeration_type_from_children (die, type, cu);
13284
13285 return type;
13286 }
13287
13288 /* Given a pointer to a die which begins an enumeration, process all
13289 the dies that define the members of the enumeration, and create the
13290 symbol for the enumeration type.
13291
13292 NOTE: We reverse the order of the element list. */
13293
13294 static void
13295 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13296 {
13297 struct type *this_type;
13298
13299 this_type = get_die_type (die, cu);
13300 if (this_type == NULL)
13301 this_type = read_enumeration_type (die, cu);
13302
13303 if (die->child != NULL)
13304 {
13305 struct die_info *child_die;
13306 const char *name;
13307
13308 child_die = die->child;
13309 while (child_die && child_die->tag)
13310 {
13311 if (child_die->tag != DW_TAG_enumerator)
13312 {
13313 process_die (child_die, cu);
13314 }
13315 else
13316 {
13317 name = dwarf2_name (child_die, cu);
13318 if (name)
13319 new_symbol (child_die, this_type, cu);
13320 }
13321
13322 child_die = child_die->sibling;
13323 }
13324 }
13325
13326 /* If we are reading an enum from a .debug_types unit, and the enum
13327 is a declaration, and the enum is not the signatured type in the
13328 unit, then we do not want to add a symbol for it. Adding a
13329 symbol would in some cases obscure the true definition of the
13330 enum, giving users an incomplete type when the definition is
13331 actually available. Note that we do not want to do this for all
13332 enums which are just declarations, because C++0x allows forward
13333 enum declarations. */
13334 if (cu->per_cu->is_debug_types
13335 && die_is_declaration (die, cu))
13336 {
13337 struct signatured_type *sig_type;
13338
13339 sig_type = (struct signatured_type *) cu->per_cu;
13340 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
13341 if (sig_type->type_offset_in_section != die->sect_off)
13342 return;
13343 }
13344
13345 new_symbol (die, this_type, cu);
13346 }
13347
13348 /* Helper function for quirk_ada_thick_pointer that examines a bounds
13349 expression for an index type and finds the corresponding field
13350 offset in the hidden "P_BOUNDS" structure. Returns true on success
13351 and updates *FIELD, false if it fails to recognize an
13352 expression. */
13353
13354 static bool
13355 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
13356 int *bounds_offset, struct field *field,
13357 struct dwarf2_cu *cu)
13358 {
13359 struct attribute *attr = dwarf2_attr (die, name, cu);
13360 if (attr == nullptr || !attr->form_is_block ())
13361 return false;
13362
13363 const struct dwarf_block *block = attr->as_block ();
13364 const gdb_byte *start = block->data;
13365 const gdb_byte *end = block->data + block->size;
13366
13367 /* The expression to recognize generally looks like:
13368
13369 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13370 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13371
13372 However, the second "plus_uconst" may be missing:
13373
13374 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13375 DW_OP_deref_size: 4)
13376
13377 This happens when the field is at the start of the structure.
13378
13379 Also, the final deref may not be sized:
13380
13381 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
13382 DW_OP_deref)
13383
13384 This happens when the size of the index type happens to be the
13385 same as the architecture's word size. This can occur with or
13386 without the second plus_uconst. */
13387
13388 if (end - start < 2)
13389 return false;
13390 if (*start++ != DW_OP_push_object_address)
13391 return false;
13392 if (*start++ != DW_OP_plus_uconst)
13393 return false;
13394
13395 uint64_t this_bound_off;
13396 start = gdb_read_uleb128 (start, end, &this_bound_off);
13397 if (start == nullptr || (int) this_bound_off != this_bound_off)
13398 return false;
13399 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
13400 is consistent among all bounds. */
13401 if (*bounds_offset == -1)
13402 *bounds_offset = this_bound_off;
13403 else if (*bounds_offset != this_bound_off)
13404 return false;
13405
13406 if (start == end || *start++ != DW_OP_deref)
13407 return false;
13408
13409 int offset = 0;
13410 if (start ==end)
13411 return false;
13412 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
13413 {
13414 /* This means an offset of 0. */
13415 }
13416 else if (*start++ != DW_OP_plus_uconst)
13417 return false;
13418 else
13419 {
13420 /* The size is the parameter to DW_OP_plus_uconst. */
13421 uint64_t val;
13422 start = gdb_read_uleb128 (start, end, &val);
13423 if (start == nullptr)
13424 return false;
13425 if ((int) val != val)
13426 return false;
13427 offset = val;
13428 }
13429
13430 if (start == end)
13431 return false;
13432
13433 uint64_t size;
13434 if (*start == DW_OP_deref_size)
13435 {
13436 start = gdb_read_uleb128 (start + 1, end, &size);
13437 if (start == nullptr)
13438 return false;
13439 }
13440 else if (*start == DW_OP_deref)
13441 {
13442 size = cu->header.addr_size;
13443 ++start;
13444 }
13445 else
13446 return false;
13447
13448 field->set_loc_bitpos (8 * offset);
13449 if (size != field->type ()->length ())
13450 FIELD_BITSIZE (*field) = 8 * size;
13451
13452 return true;
13453 }
13454
13455 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
13456 some kinds of Ada arrays:
13457
13458 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
13459 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
13460 <11e0> DW_AT_data_location: 2 byte block: 97 6
13461 (DW_OP_push_object_address; DW_OP_deref)
13462 <11e3> DW_AT_type : <0x1173>
13463 <11e7> DW_AT_sibling : <0x1201>
13464 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
13465 <11ec> DW_AT_type : <0x1206>
13466 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
13467 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13468 DW_OP_deref_size: 4)
13469 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
13470 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13471 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13472
13473 This actually represents a "thick pointer", which is a structure
13474 with two elements: one that is a pointer to the array data, and one
13475 that is a pointer to another structure; this second structure holds
13476 the array bounds.
13477
13478 This returns a new type on success, or nullptr if this didn't
13479 recognize the type. */
13480
13481 static struct type *
13482 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
13483 struct type *type)
13484 {
13485 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
13486 /* So far we've only seen this with block form. */
13487 if (attr == nullptr || !attr->form_is_block ())
13488 return nullptr;
13489
13490 /* Note that this will fail if the structure layout is changed by
13491 the compiler. However, we have no good way to recognize some
13492 other layout, because we don't know what expression the compiler
13493 might choose to emit should this happen. */
13494 struct dwarf_block *blk = attr->as_block ();
13495 if (blk->size != 2
13496 || blk->data[0] != DW_OP_push_object_address
13497 || blk->data[1] != DW_OP_deref)
13498 return nullptr;
13499
13500 int bounds_offset = -1;
13501 int max_align = -1;
13502 std::vector<struct field> range_fields;
13503 for (struct die_info *child_die = die->child;
13504 child_die;
13505 child_die = child_die->sibling)
13506 {
13507 if (child_die->tag == DW_TAG_subrange_type)
13508 {
13509 struct type *underlying = read_subrange_index_type (child_die, cu);
13510
13511 int this_align = type_align (underlying);
13512 if (this_align > max_align)
13513 max_align = this_align;
13514
13515 range_fields.emplace_back ();
13516 range_fields.emplace_back ();
13517
13518 struct field &lower = range_fields[range_fields.size () - 2];
13519 struct field &upper = range_fields[range_fields.size () - 1];
13520
13521 lower.set_type (underlying);
13522 FIELD_ARTIFICIAL (lower) = 1;
13523
13524 upper.set_type (underlying);
13525 FIELD_ARTIFICIAL (upper) = 1;
13526
13527 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
13528 &bounds_offset, &lower, cu)
13529 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
13530 &bounds_offset, &upper, cu))
13531 return nullptr;
13532 }
13533 }
13534
13535 /* This shouldn't really happen, but double-check that we found
13536 where the bounds are stored. */
13537 if (bounds_offset == -1)
13538 return nullptr;
13539
13540 struct objfile *objfile = cu->per_objfile->objfile;
13541 for (int i = 0; i < range_fields.size (); i += 2)
13542 {
13543 char name[20];
13544
13545 /* Set the name of each field in the bounds. */
13546 xsnprintf (name, sizeof (name), "LB%d", i / 2);
13547 range_fields[i].set_name (objfile->intern (name));
13548 xsnprintf (name, sizeof (name), "UB%d", i / 2);
13549 range_fields[i + 1].set_name (objfile->intern (name));
13550 }
13551
13552 struct type *bounds = alloc_type (objfile);
13553 bounds->set_code (TYPE_CODE_STRUCT);
13554
13555 bounds->set_num_fields (range_fields.size ());
13556 bounds->set_fields
13557 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
13558 * sizeof (struct field))));
13559 memcpy (bounds->fields (), range_fields.data (),
13560 bounds->num_fields () * sizeof (struct field));
13561
13562 int last_fieldno = range_fields.size () - 1;
13563 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
13564 + bounds->field (last_fieldno).type ()->length ());
13565 bounds->set_length (align_up (bounds_size, max_align));
13566
13567 /* Rewrite the existing array type in place. Specifically, we
13568 remove any dynamic properties we might have read, and we replace
13569 the index types. */
13570 struct type *iter = type;
13571 for (int i = 0; i < range_fields.size (); i += 2)
13572 {
13573 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
13574 iter->main_type->dyn_prop_list = nullptr;
13575 iter->set_index_type
13576 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
13577 iter = iter->target_type ();
13578 }
13579
13580 struct type *result = alloc_type (objfile);
13581 result->set_code (TYPE_CODE_STRUCT);
13582
13583 result->set_num_fields (2);
13584 result->set_fields
13585 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
13586 * sizeof (struct field))));
13587
13588 /* The names are chosen to coincide with what the compiler does with
13589 -fgnat-encodings=all, which the Ada code in gdb already
13590 understands. */
13591 result->field (0).set_name ("P_ARRAY");
13592 result->field (0).set_type (lookup_pointer_type (type));
13593
13594 result->field (1).set_name ("P_BOUNDS");
13595 result->field (1).set_type (lookup_pointer_type (bounds));
13596 result->field (1).set_loc_bitpos (8 * bounds_offset);
13597
13598 result->set_name (type->name ());
13599 result->set_length (result->field (0).type ()->length ()
13600 + result->field (1).type ()->length ());
13601
13602 return result;
13603 }
13604
13605 /* Extract all information from a DW_TAG_array_type DIE and put it in
13606 the DIE's type field. For now, this only handles one dimensional
13607 arrays. */
13608
13609 static struct type *
13610 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13611 {
13612 struct objfile *objfile = cu->per_objfile->objfile;
13613 struct die_info *child_die;
13614 struct type *type;
13615 struct type *element_type, *range_type, *index_type;
13616 struct attribute *attr;
13617 const char *name;
13618 struct dynamic_prop *byte_stride_prop = NULL;
13619 unsigned int bit_stride = 0;
13620
13621 element_type = die_type (die, cu);
13622
13623 /* The die_type call above may have already set the type for this DIE. */
13624 type = get_die_type (die, cu);
13625 if (type)
13626 return type;
13627
13628 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13629 if (attr != NULL)
13630 {
13631 int stride_ok;
13632 struct type *prop_type = cu->addr_sized_int_type (false);
13633
13634 byte_stride_prop
13635 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
13636 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
13637 prop_type);
13638 if (!stride_ok)
13639 {
13640 complaint (_("unable to read array DW_AT_byte_stride "
13641 " - DIE at %s [in module %s]"),
13642 sect_offset_str (die->sect_off),
13643 objfile_name (cu->per_objfile->objfile));
13644 /* Ignore this attribute. We will likely not be able to print
13645 arrays of this type correctly, but there is little we can do
13646 to help if we cannot read the attribute's value. */
13647 byte_stride_prop = NULL;
13648 }
13649 }
13650
13651 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13652 if (attr != NULL)
13653 bit_stride = attr->constant_value (0);
13654
13655 /* Irix 6.2 native cc creates array types without children for
13656 arrays with unspecified length. */
13657 if (die->child == NULL)
13658 {
13659 index_type = objfile_type (objfile)->builtin_int;
13660 range_type = create_static_range_type (NULL, index_type, 0, -1);
13661 type = create_array_type_with_stride (NULL, element_type, range_type,
13662 byte_stride_prop, bit_stride);
13663 return set_die_type (die, type, cu);
13664 }
13665
13666 std::vector<struct type *> range_types;
13667 child_die = die->child;
13668 while (child_die && child_die->tag)
13669 {
13670 if (child_die->tag == DW_TAG_subrange_type
13671 || child_die->tag == DW_TAG_generic_subrange)
13672 {
13673 struct type *child_type = read_type_die (child_die, cu);
13674
13675 if (child_type != NULL)
13676 {
13677 /* The range type was succesfully read. Save it for the
13678 array type creation. */
13679 range_types.push_back (child_type);
13680 }
13681 }
13682 child_die = child_die->sibling;
13683 }
13684
13685 if (range_types.empty ())
13686 {
13687 complaint (_("unable to find array range - DIE at %s [in module %s]"),
13688 sect_offset_str (die->sect_off),
13689 objfile_name (cu->per_objfile->objfile));
13690 return NULL;
13691 }
13692
13693 /* Dwarf2 dimensions are output from left to right, create the
13694 necessary array types in backwards order. */
13695
13696 type = element_type;
13697
13698 if (read_array_order (die, cu) == DW_ORD_col_major)
13699 {
13700 int i = 0;
13701
13702 while (i < range_types.size ())
13703 {
13704 type = create_array_type_with_stride (NULL, type, range_types[i++],
13705 byte_stride_prop, bit_stride);
13706 type->set_is_multi_dimensional (true);
13707 bit_stride = 0;
13708 byte_stride_prop = nullptr;
13709 }
13710 }
13711 else
13712 {
13713 size_t ndim = range_types.size ();
13714 while (ndim-- > 0)
13715 {
13716 type = create_array_type_with_stride (NULL, type, range_types[ndim],
13717 byte_stride_prop, bit_stride);
13718 type->set_is_multi_dimensional (true);
13719 bit_stride = 0;
13720 byte_stride_prop = nullptr;
13721 }
13722 }
13723
13724 /* Clear the flag on the outermost array type. */
13725 type->set_is_multi_dimensional (false);
13726 gdb_assert (type != element_type);
13727
13728 /* Understand Dwarf2 support for vector types (like they occur on
13729 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13730 array type. This is not part of the Dwarf2/3 standard yet, but a
13731 custom vendor extension. The main difference between a regular
13732 array and the vector variant is that vectors are passed by value
13733 to functions. */
13734 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13735 if (attr != nullptr)
13736 make_vector_type (type);
13737
13738 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13739 implementation may choose to implement triple vectors using this
13740 attribute. */
13741 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13742 if (attr != nullptr && attr->form_is_unsigned ())
13743 {
13744 if (attr->as_unsigned () >= type->length ())
13745 type->set_length (attr->as_unsigned ());
13746 else
13747 complaint (_("DW_AT_byte_size for array type smaller "
13748 "than the total size of elements"));
13749 }
13750
13751 name = dwarf2_name (die, cu);
13752 if (name)
13753 type->set_name (name);
13754
13755 maybe_set_alignment (cu, die, type);
13756
13757 struct type *replacement_type = nullptr;
13758 if (cu->lang () == language_ada)
13759 {
13760 replacement_type = quirk_ada_thick_pointer (die, cu, type);
13761 if (replacement_type != nullptr)
13762 type = replacement_type;
13763 }
13764
13765 /* Install the type in the die. */
13766 set_die_type (die, type, cu, replacement_type != nullptr);
13767
13768 /* set_die_type should be already done. */
13769 set_descriptive_type (type, die, cu);
13770
13771 return type;
13772 }
13773
13774 static enum dwarf_array_dim_ordering
13775 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13776 {
13777 struct attribute *attr;
13778
13779 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13780
13781 if (attr != nullptr)
13782 {
13783 LONGEST val = attr->constant_value (-1);
13784 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
13785 return (enum dwarf_array_dim_ordering) val;
13786 }
13787
13788 /* GNU F77 is a special case, as at 08/2004 array type info is the
13789 opposite order to the dwarf2 specification, but data is still
13790 laid out as per normal fortran.
13791
13792 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13793 version checking. */
13794
13795 if (cu->lang () == language_fortran
13796 && cu->producer && strstr (cu->producer, "GNU F77"))
13797 {
13798 return DW_ORD_row_major;
13799 }
13800
13801 switch (cu->language_defn->array_ordering ())
13802 {
13803 case array_column_major:
13804 return DW_ORD_col_major;
13805 case array_row_major:
13806 default:
13807 return DW_ORD_row_major;
13808 };
13809 }
13810
13811 /* Extract all information from a DW_TAG_set_type DIE and put it in
13812 the DIE's type field. */
13813
13814 static struct type *
13815 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13816 {
13817 struct type *domain_type, *set_type;
13818 struct attribute *attr;
13819
13820 domain_type = die_type (die, cu);
13821
13822 /* The die_type call above may have already set the type for this DIE. */
13823 set_type = get_die_type (die, cu);
13824 if (set_type)
13825 return set_type;
13826
13827 set_type = create_set_type (NULL, domain_type);
13828
13829 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13830 if (attr != nullptr && attr->form_is_unsigned ())
13831 set_type->set_length (attr->as_unsigned ());
13832
13833 maybe_set_alignment (cu, die, set_type);
13834
13835 return set_die_type (die, set_type, cu);
13836 }
13837
13838 /* A helper for read_common_block that creates a locexpr baton.
13839 SYM is the symbol which we are marking as computed.
13840 COMMON_DIE is the DIE for the common block.
13841 COMMON_LOC is the location expression attribute for the common
13842 block itself.
13843 MEMBER_LOC is the location expression attribute for the particular
13844 member of the common block that we are processing.
13845 CU is the CU from which the above come. */
13846
13847 static void
13848 mark_common_block_symbol_computed (struct symbol *sym,
13849 struct die_info *common_die,
13850 struct attribute *common_loc,
13851 struct attribute *member_loc,
13852 struct dwarf2_cu *cu)
13853 {
13854 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13855 struct objfile *objfile = per_objfile->objfile;
13856 struct dwarf2_locexpr_baton *baton;
13857 gdb_byte *ptr;
13858 unsigned int cu_off;
13859 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
13860 LONGEST offset = 0;
13861
13862 gdb_assert (common_loc && member_loc);
13863 gdb_assert (common_loc->form_is_block ());
13864 gdb_assert (member_loc->form_is_block ()
13865 || member_loc->form_is_constant ());
13866
13867 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13868 baton->per_objfile = per_objfile;
13869 baton->per_cu = cu->per_cu;
13870 gdb_assert (baton->per_cu);
13871
13872 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13873
13874 if (member_loc->form_is_constant ())
13875 {
13876 offset = member_loc->constant_value (0);
13877 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13878 }
13879 else
13880 baton->size += member_loc->as_block ()->size;
13881
13882 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
13883 baton->data = ptr;
13884
13885 *ptr++ = DW_OP_call4;
13886 cu_off = common_die->sect_off - cu->per_cu->sect_off;
13887 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13888 ptr += 4;
13889
13890 if (member_loc->form_is_constant ())
13891 {
13892 *ptr++ = DW_OP_addr;
13893 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13894 ptr += cu->header.addr_size;
13895 }
13896 else
13897 {
13898 /* We have to copy the data here, because DW_OP_call4 will only
13899 use a DW_AT_location attribute. */
13900 struct dwarf_block *block = member_loc->as_block ();
13901 memcpy (ptr, block->data, block->size);
13902 ptr += block->size;
13903 }
13904
13905 *ptr++ = DW_OP_plus;
13906 gdb_assert (ptr - baton->data == baton->size);
13907
13908 SYMBOL_LOCATION_BATON (sym) = baton;
13909 sym->set_aclass_index (dwarf2_locexpr_index);
13910 }
13911
13912 /* Create appropriate locally-scoped variables for all the
13913 DW_TAG_common_block entries. Also create a struct common_block
13914 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13915 is used to separate the common blocks name namespace from regular
13916 variable names. */
13917
13918 static void
13919 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13920 {
13921 struct attribute *attr;
13922
13923 attr = dwarf2_attr (die, DW_AT_location, cu);
13924 if (attr != nullptr)
13925 {
13926 /* Support the .debug_loc offsets. */
13927 if (attr->form_is_block ())
13928 {
13929 /* Ok. */
13930 }
13931 else if (attr->form_is_section_offset ())
13932 {
13933 dwarf2_complex_location_expr_complaint ();
13934 attr = NULL;
13935 }
13936 else
13937 {
13938 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13939 "common block member");
13940 attr = NULL;
13941 }
13942 }
13943
13944 if (die->child != NULL)
13945 {
13946 struct objfile *objfile = cu->per_objfile->objfile;
13947 struct die_info *child_die;
13948 size_t n_entries = 0, size;
13949 struct common_block *common_block;
13950 struct symbol *sym;
13951
13952 for (child_die = die->child;
13953 child_die && child_die->tag;
13954 child_die = child_die->sibling)
13955 ++n_entries;
13956
13957 size = (sizeof (struct common_block)
13958 + (n_entries - 1) * sizeof (struct symbol *));
13959 common_block
13960 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
13961 size);
13962 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13963 common_block->n_entries = 0;
13964
13965 for (child_die = die->child;
13966 child_die && child_die->tag;
13967 child_die = child_die->sibling)
13968 {
13969 /* Create the symbol in the DW_TAG_common_block block in the current
13970 symbol scope. */
13971 sym = new_symbol (child_die, NULL, cu);
13972 if (sym != NULL)
13973 {
13974 struct attribute *member_loc;
13975
13976 common_block->contents[common_block->n_entries++] = sym;
13977
13978 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13979 cu);
13980 if (member_loc)
13981 {
13982 /* GDB has handled this for a long time, but it is
13983 not specified by DWARF. It seems to have been
13984 emitted by gfortran at least as recently as:
13985 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
13986 complaint (_("Variable in common block has "
13987 "DW_AT_data_member_location "
13988 "- DIE at %s [in module %s]"),
13989 sect_offset_str (child_die->sect_off),
13990 objfile_name (objfile));
13991
13992 if (member_loc->form_is_section_offset ())
13993 dwarf2_complex_location_expr_complaint ();
13994 else if (member_loc->form_is_constant ()
13995 || member_loc->form_is_block ())
13996 {
13997 if (attr != nullptr)
13998 mark_common_block_symbol_computed (sym, die, attr,
13999 member_loc, cu);
14000 }
14001 else
14002 dwarf2_complex_location_expr_complaint ();
14003 }
14004 }
14005 }
14006
14007 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14008 sym->set_value_common_block (common_block);
14009 }
14010 }
14011
14012 /* Create a type for a C++ namespace. */
14013
14014 static struct type *
14015 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14016 {
14017 struct objfile *objfile = cu->per_objfile->objfile;
14018 const char *previous_prefix, *name;
14019 int is_anonymous;
14020 struct type *type;
14021
14022 /* For extensions, reuse the type of the original namespace. */
14023 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14024 {
14025 struct die_info *ext_die;
14026 struct dwarf2_cu *ext_cu = cu;
14027
14028 ext_die = dwarf2_extension (die, &ext_cu);
14029 type = read_type_die (ext_die, ext_cu);
14030
14031 /* EXT_CU may not be the same as CU.
14032 Ensure TYPE is recorded with CU in die_type_hash. */
14033 return set_die_type (die, type, cu);
14034 }
14035
14036 name = namespace_name (die, &is_anonymous, cu);
14037
14038 /* Now build the name of the current namespace. */
14039
14040 previous_prefix = determine_prefix (die, cu);
14041 if (previous_prefix[0] != '\0')
14042 name = typename_concat (&objfile->objfile_obstack,
14043 previous_prefix, name, 0, cu);
14044
14045 /* Create the type. */
14046 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14047
14048 return set_die_type (die, type, cu);
14049 }
14050
14051 /* Read a namespace scope. */
14052
14053 static void
14054 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14055 {
14056 struct objfile *objfile = cu->per_objfile->objfile;
14057 int is_anonymous;
14058
14059 /* Add a symbol associated to this if we haven't seen the namespace
14060 before. Also, add a using directive if it's an anonymous
14061 namespace. */
14062
14063 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14064 {
14065 struct type *type;
14066
14067 type = read_type_die (die, cu);
14068 new_symbol (die, type, cu);
14069
14070 namespace_name (die, &is_anonymous, cu);
14071 if (is_anonymous)
14072 {
14073 const char *previous_prefix = determine_prefix (die, cu);
14074
14075 std::vector<const char *> excludes;
14076 add_using_directive (using_directives (cu),
14077 previous_prefix, type->name (), NULL,
14078 NULL, excludes,
14079 read_decl_line (die, cu),
14080 0, &objfile->objfile_obstack);
14081 }
14082 }
14083
14084 if (die->child != NULL)
14085 {
14086 struct die_info *child_die = die->child;
14087
14088 while (child_die && child_die->tag)
14089 {
14090 process_die (child_die, cu);
14091 child_die = child_die->sibling;
14092 }
14093 }
14094 }
14095
14096 /* Read a Fortran module as type. This DIE can be only a declaration used for
14097 imported module. Still we need that type as local Fortran "use ... only"
14098 declaration imports depend on the created type in determine_prefix. */
14099
14100 static struct type *
14101 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14102 {
14103 struct objfile *objfile = cu->per_objfile->objfile;
14104 const char *module_name;
14105 struct type *type;
14106
14107 module_name = dwarf2_name (die, cu);
14108 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14109
14110 return set_die_type (die, type, cu);
14111 }
14112
14113 /* Read a Fortran module. */
14114
14115 static void
14116 read_module (struct die_info *die, struct dwarf2_cu *cu)
14117 {
14118 struct die_info *child_die = die->child;
14119 struct type *type;
14120
14121 type = read_type_die (die, cu);
14122 new_symbol (die, type, cu);
14123
14124 while (child_die && child_die->tag)
14125 {
14126 process_die (child_die, cu);
14127 child_die = child_die->sibling;
14128 }
14129 }
14130
14131 /* Return the name of the namespace represented by DIE. Set
14132 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14133 namespace. */
14134
14135 static const char *
14136 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14137 {
14138 struct die_info *current_die;
14139 const char *name = NULL;
14140
14141 /* Loop through the extensions until we find a name. */
14142
14143 for (current_die = die;
14144 current_die != NULL;
14145 current_die = dwarf2_extension (die, &cu))
14146 {
14147 /* We don't use dwarf2_name here so that we can detect the absence
14148 of a name -> anonymous namespace. */
14149 name = dwarf2_string_attr (die, DW_AT_name, cu);
14150
14151 if (name != NULL)
14152 break;
14153 }
14154
14155 /* Is it an anonymous namespace? */
14156
14157 *is_anonymous = (name == NULL);
14158 if (*is_anonymous)
14159 name = CP_ANONYMOUS_NAMESPACE_STR;
14160
14161 return name;
14162 }
14163
14164 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14165 the user defined type vector. */
14166
14167 static struct type *
14168 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14169 {
14170 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
14171 struct comp_unit_head *cu_header = &cu->header;
14172 struct type *type;
14173 struct attribute *attr_byte_size;
14174 struct attribute *attr_address_class;
14175 int byte_size, addr_class;
14176 struct type *target_type;
14177
14178 target_type = die_type (die, cu);
14179
14180 /* The die_type call above may have already set the type for this DIE. */
14181 type = get_die_type (die, cu);
14182 if (type)
14183 return type;
14184
14185 type = lookup_pointer_type (target_type);
14186
14187 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14188 if (attr_byte_size)
14189 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
14190 else
14191 byte_size = cu_header->addr_size;
14192
14193 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14194 if (attr_address_class)
14195 addr_class = attr_address_class->constant_value (DW_ADDR_none);
14196 else
14197 addr_class = DW_ADDR_none;
14198
14199 ULONGEST alignment = get_alignment (cu, die);
14200
14201 /* If the pointer size, alignment, or address class is different
14202 than the default, create a type variant marked as such and set
14203 the length accordingly. */
14204 if (type->length () != byte_size
14205 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
14206 && alignment != TYPE_RAW_ALIGN (type))
14207 || addr_class != DW_ADDR_none)
14208 {
14209 if (gdbarch_address_class_type_flags_p (gdbarch))
14210 {
14211 type_instance_flags type_flags
14212 = gdbarch_address_class_type_flags (gdbarch, byte_size,
14213 addr_class);
14214 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14215 == 0);
14216 type = make_type_with_address_space (type, type_flags);
14217 }
14218 else if (type->length () != byte_size)
14219 {
14220 complaint (_("invalid pointer size %d"), byte_size);
14221 }
14222 else if (TYPE_RAW_ALIGN (type) != alignment)
14223 {
14224 complaint (_("Invalid DW_AT_alignment"
14225 " - DIE at %s [in module %s]"),
14226 sect_offset_str (die->sect_off),
14227 objfile_name (cu->per_objfile->objfile));
14228 }
14229 else
14230 {
14231 /* Should we also complain about unhandled address classes? */
14232 }
14233 }
14234
14235 type->set_length (byte_size);
14236 set_type_align (type, alignment);
14237 return set_die_type (die, type, cu);
14238 }
14239
14240 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14241 the user defined type vector. */
14242
14243 static struct type *
14244 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14245 {
14246 struct type *type;
14247 struct type *to_type;
14248 struct type *domain;
14249
14250 to_type = die_type (die, cu);
14251 domain = die_containing_type (die, cu);
14252
14253 /* The calls above may have already set the type for this DIE. */
14254 type = get_die_type (die, cu);
14255 if (type)
14256 return type;
14257
14258 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
14259 type = lookup_methodptr_type (to_type);
14260 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
14261 {
14262 struct type *new_type = alloc_type (cu->per_objfile->objfile);
14263
14264 smash_to_method_type (new_type, domain, to_type->target_type (),
14265 to_type->fields (), to_type->num_fields (),
14266 to_type->has_varargs ());
14267 type = lookup_methodptr_type (new_type);
14268 }
14269 else
14270 type = lookup_memberptr_type (to_type, domain);
14271
14272 return set_die_type (die, type, cu);
14273 }
14274
14275 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14276 the user defined type vector. */
14277
14278 static struct type *
14279 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14280 enum type_code refcode)
14281 {
14282 struct comp_unit_head *cu_header = &cu->header;
14283 struct type *type, *target_type;
14284 struct attribute *attr;
14285
14286 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14287
14288 target_type = die_type (die, cu);
14289
14290 /* The die_type call above may have already set the type for this DIE. */
14291 type = get_die_type (die, cu);
14292 if (type)
14293 return type;
14294
14295 type = lookup_reference_type (target_type, refcode);
14296 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14297 if (attr != nullptr)
14298 type->set_length (attr->constant_value (cu_header->addr_size));
14299 else
14300 type->set_length (cu_header->addr_size);
14301
14302 maybe_set_alignment (cu, die, type);
14303 return set_die_type (die, type, cu);
14304 }
14305
14306 /* Add the given cv-qualifiers to the element type of the array. GCC
14307 outputs DWARF type qualifiers that apply to an array, not the
14308 element type. But GDB relies on the array element type to carry
14309 the cv-qualifiers. This mimics section 6.7.3 of the C99
14310 specification. */
14311
14312 static struct type *
14313 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14314 struct type *base_type, int cnst, int voltl)
14315 {
14316 struct type *el_type, *inner_array;
14317
14318 base_type = copy_type (base_type);
14319 inner_array = base_type;
14320
14321 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
14322 {
14323 inner_array->set_target_type (copy_type (inner_array->target_type ()));
14324 inner_array = inner_array->target_type ();
14325 }
14326
14327 el_type = inner_array->target_type ();
14328 cnst |= TYPE_CONST (el_type);
14329 voltl |= TYPE_VOLATILE (el_type);
14330 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
14331
14332 return set_die_type (die, base_type, cu);
14333 }
14334
14335 static struct type *
14336 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14337 {
14338 struct type *base_type, *cv_type;
14339
14340 base_type = die_type (die, cu);
14341
14342 /* The die_type call above may have already set the type for this DIE. */
14343 cv_type = get_die_type (die, cu);
14344 if (cv_type)
14345 return cv_type;
14346
14347 /* In case the const qualifier is applied to an array type, the element type
14348 is so qualified, not the array type (section 6.7.3 of C99). */
14349 if (base_type->code () == TYPE_CODE_ARRAY)
14350 return add_array_cv_type (die, cu, base_type, 1, 0);
14351
14352 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14353 return set_die_type (die, cv_type, cu);
14354 }
14355
14356 static struct type *
14357 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14358 {
14359 struct type *base_type, *cv_type;
14360
14361 base_type = die_type (die, cu);
14362
14363 /* The die_type call above may have already set the type for this DIE. */
14364 cv_type = get_die_type (die, cu);
14365 if (cv_type)
14366 return cv_type;
14367
14368 /* In case the volatile qualifier is applied to an array type, the
14369 element type is so qualified, not the array type (section 6.7.3
14370 of C99). */
14371 if (base_type->code () == TYPE_CODE_ARRAY)
14372 return add_array_cv_type (die, cu, base_type, 0, 1);
14373
14374 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14375 return set_die_type (die, cv_type, cu);
14376 }
14377
14378 /* Handle DW_TAG_restrict_type. */
14379
14380 static struct type *
14381 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14382 {
14383 struct type *base_type, *cv_type;
14384
14385 base_type = die_type (die, cu);
14386
14387 /* The die_type call above may have already set the type for this DIE. */
14388 cv_type = get_die_type (die, cu);
14389 if (cv_type)
14390 return cv_type;
14391
14392 cv_type = make_restrict_type (base_type);
14393 return set_die_type (die, cv_type, cu);
14394 }
14395
14396 /* Handle DW_TAG_atomic_type. */
14397
14398 static struct type *
14399 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14400 {
14401 struct type *base_type, *cv_type;
14402
14403 base_type = die_type (die, cu);
14404
14405 /* The die_type call above may have already set the type for this DIE. */
14406 cv_type = get_die_type (die, cu);
14407 if (cv_type)
14408 return cv_type;
14409
14410 cv_type = make_atomic_type (base_type);
14411 return set_die_type (die, cv_type, cu);
14412 }
14413
14414 /* Extract all information from a DW_TAG_string_type DIE and add to
14415 the user defined type vector. It isn't really a user defined type,
14416 but it behaves like one, with other DIE's using an AT_user_def_type
14417 attribute to reference it. */
14418
14419 static struct type *
14420 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14421 {
14422 struct objfile *objfile = cu->per_objfile->objfile;
14423 struct gdbarch *gdbarch = objfile->arch ();
14424 struct type *type, *range_type, *index_type, *char_type;
14425 struct attribute *attr;
14426 struct dynamic_prop prop;
14427 bool length_is_constant = true;
14428 LONGEST length;
14429
14430 /* There are a couple of places where bit sizes might be made use of
14431 when parsing a DW_TAG_string_type, however, no producer that we know
14432 of make use of these. Handling bit sizes that are a multiple of the
14433 byte size is easy enough, but what about other bit sizes? Lets deal
14434 with that problem when we have to. Warn about these attributes being
14435 unsupported, then parse the type and ignore them like we always
14436 have. */
14437 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
14438 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
14439 {
14440 static bool warning_printed = false;
14441 if (!warning_printed)
14442 {
14443 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
14444 "currently supported on DW_TAG_string_type."));
14445 warning_printed = true;
14446 }
14447 }
14448
14449 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14450 if (attr != nullptr && !attr->form_is_constant ())
14451 {
14452 /* The string length describes the location at which the length of
14453 the string can be found. The size of the length field can be
14454 specified with one of the attributes below. */
14455 struct type *prop_type;
14456 struct attribute *len
14457 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
14458 if (len == nullptr)
14459 len = dwarf2_attr (die, DW_AT_byte_size, cu);
14460 if (len != nullptr && len->form_is_constant ())
14461 {
14462 /* Pass 0 as the default as we know this attribute is constant
14463 and the default value will not be returned. */
14464 LONGEST sz = len->constant_value (0);
14465 prop_type = objfile_int_type (objfile, sz, true);
14466 }
14467 else
14468 {
14469 /* If the size is not specified then we assume it is the size of
14470 an address on this target. */
14471 prop_type = cu->addr_sized_int_type (true);
14472 }
14473
14474 /* Convert the attribute into a dynamic property. */
14475 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
14476 length = 1;
14477 else
14478 length_is_constant = false;
14479 }
14480 else if (attr != nullptr)
14481 {
14482 /* This DW_AT_string_length just contains the length with no
14483 indirection. There's no need to create a dynamic property in this
14484 case. Pass 0 for the default value as we know it will not be
14485 returned in this case. */
14486 length = attr->constant_value (0);
14487 }
14488 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
14489 {
14490 /* We don't currently support non-constant byte sizes for strings. */
14491 length = attr->constant_value (1);
14492 }
14493 else
14494 {
14495 /* Use 1 as a fallback length if we have nothing else. */
14496 length = 1;
14497 }
14498
14499 index_type = objfile_type (objfile)->builtin_int;
14500 if (length_is_constant)
14501 range_type = create_static_range_type (NULL, index_type, 1, length);
14502 else
14503 {
14504 struct dynamic_prop low_bound;
14505
14506 low_bound.set_const_val (1);
14507 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
14508 }
14509 char_type = language_string_char_type (cu->language_defn, gdbarch);
14510 type = create_string_type (NULL, char_type, range_type);
14511
14512 return set_die_type (die, type, cu);
14513 }
14514
14515 /* Assuming that DIE corresponds to a function, returns nonzero
14516 if the function is prototyped. */
14517
14518 static int
14519 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14520 {
14521 struct attribute *attr;
14522
14523 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14524 if (attr && attr->as_boolean ())
14525 return 1;
14526
14527 /* The DWARF standard implies that the DW_AT_prototyped attribute
14528 is only meaningful for C, but the concept also extends to other
14529 languages that allow unprototyped functions (Eg: Objective C).
14530 For all other languages, assume that functions are always
14531 prototyped. */
14532 if (cu->lang () != language_c
14533 && cu->lang () != language_objc
14534 && cu->lang () != language_opencl)
14535 return 1;
14536
14537 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14538 prototyped and unprototyped functions; default to prototyped,
14539 since that is more common in modern code (and RealView warns
14540 about unprototyped functions). */
14541 if (producer_is_realview (cu->producer))
14542 return 1;
14543
14544 return 0;
14545 }
14546
14547 /* Handle DIES due to C code like:
14548
14549 struct foo
14550 {
14551 int (*funcp)(int a, long l);
14552 int b;
14553 };
14554
14555 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14556
14557 static struct type *
14558 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14559 {
14560 struct objfile *objfile = cu->per_objfile->objfile;
14561 struct type *type; /* Type that this function returns. */
14562 struct type *ftype; /* Function that returns above type. */
14563 struct attribute *attr;
14564
14565 type = die_type (die, cu);
14566
14567 /* The die_type call above may have already set the type for this DIE. */
14568 ftype = get_die_type (die, cu);
14569 if (ftype)
14570 return ftype;
14571
14572 ftype = lookup_function_type (type);
14573
14574 if (prototyped_function_p (die, cu))
14575 ftype->set_is_prototyped (true);
14576
14577 /* Store the calling convention in the type if it's available in
14578 the subroutine die. Otherwise set the calling convention to
14579 the default value DW_CC_normal. */
14580 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14581 if (attr != nullptr
14582 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
14583 TYPE_CALLING_CONVENTION (ftype)
14584 = (enum dwarf_calling_convention) attr->constant_value (0);
14585 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14586 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14587 else
14588 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14589
14590 /* Record whether the function returns normally to its caller or not
14591 if the DWARF producer set that information. */
14592 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14593 if (attr && attr->as_boolean ())
14594 TYPE_NO_RETURN (ftype) = 1;
14595
14596 /* We need to add the subroutine type to the die immediately so
14597 we don't infinitely recurse when dealing with parameters
14598 declared as the same subroutine type. */
14599 set_die_type (die, ftype, cu);
14600
14601 if (die->child != NULL)
14602 {
14603 struct type *void_type = objfile_type (objfile)->builtin_void;
14604 struct die_info *child_die;
14605 int nparams, iparams;
14606
14607 /* Count the number of parameters.
14608 FIXME: GDB currently ignores vararg functions, but knows about
14609 vararg member functions. */
14610 nparams = 0;
14611 child_die = die->child;
14612 while (child_die && child_die->tag)
14613 {
14614 if (child_die->tag == DW_TAG_formal_parameter)
14615 nparams++;
14616 else if (child_die->tag == DW_TAG_unspecified_parameters)
14617 ftype->set_has_varargs (true);
14618
14619 child_die = child_die->sibling;
14620 }
14621
14622 /* Allocate storage for parameters and fill them in. */
14623 ftype->set_num_fields (nparams);
14624 ftype->set_fields
14625 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
14626
14627 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14628 even if we error out during the parameters reading below. */
14629 for (iparams = 0; iparams < nparams; iparams++)
14630 ftype->field (iparams).set_type (void_type);
14631
14632 iparams = 0;
14633 child_die = die->child;
14634 while (child_die && child_die->tag)
14635 {
14636 if (child_die->tag == DW_TAG_formal_parameter)
14637 {
14638 struct type *arg_type;
14639
14640 /* DWARF version 2 has no clean way to discern C++
14641 static and non-static member functions. G++ helps
14642 GDB by marking the first parameter for non-static
14643 member functions (which is the this pointer) as
14644 artificial. We pass this information to
14645 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14646
14647 DWARF version 3 added DW_AT_object_pointer, which GCC
14648 4.5 does not yet generate. */
14649 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14650 if (attr != nullptr)
14651 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
14652 else
14653 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14654 arg_type = die_type (child_die, cu);
14655
14656 /* RealView does not mark THIS as const, which the testsuite
14657 expects. GCC marks THIS as const in method definitions,
14658 but not in the class specifications (GCC PR 43053). */
14659 if (cu->lang () == language_cplus
14660 && !TYPE_CONST (arg_type)
14661 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14662 {
14663 int is_this = 0;
14664 struct dwarf2_cu *arg_cu = cu;
14665 const char *name = dwarf2_name (child_die, cu);
14666
14667 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14668 if (attr != nullptr)
14669 {
14670 /* If the compiler emits this, use it. */
14671 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14672 is_this = 1;
14673 }
14674 else if (name && strcmp (name, "this") == 0)
14675 /* Function definitions will have the argument names. */
14676 is_this = 1;
14677 else if (name == NULL && iparams == 0)
14678 /* Declarations may not have the names, so like
14679 elsewhere in GDB, assume an artificial first
14680 argument is "this". */
14681 is_this = 1;
14682
14683 if (is_this)
14684 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14685 arg_type, 0);
14686 }
14687
14688 ftype->field (iparams).set_type (arg_type);
14689 iparams++;
14690 }
14691 child_die = child_die->sibling;
14692 }
14693 }
14694
14695 return ftype;
14696 }
14697
14698 static struct type *
14699 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14700 {
14701 struct objfile *objfile = cu->per_objfile->objfile;
14702 const char *name = NULL;
14703 struct type *this_type, *target_type;
14704
14705 name = dwarf2_full_name (NULL, die, cu);
14706 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14707 this_type->set_target_is_stub (true);
14708 set_die_type (die, this_type, cu);
14709 target_type = die_type (die, cu);
14710 if (target_type != this_type)
14711 this_type->set_target_type (target_type);
14712 else
14713 {
14714 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14715 spec and cause infinite loops in GDB. */
14716 complaint (_("Self-referential DW_TAG_typedef "
14717 "- DIE at %s [in module %s]"),
14718 sect_offset_str (die->sect_off), objfile_name (objfile));
14719 this_type->set_target_type (nullptr);
14720 }
14721 if (name == NULL)
14722 {
14723 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
14724 anonymous typedefs, which is, strictly speaking, invalid DWARF.
14725 Handle these by just returning the target type, rather than
14726 constructing an anonymous typedef type and trying to handle this
14727 elsewhere. */
14728 set_die_type (die, target_type, cu);
14729 return target_type;
14730 }
14731 return this_type;
14732 }
14733
14734 /* Helper for get_dwarf2_rational_constant that computes the value of
14735 a given gmp_mpz given an attribute. */
14736
14737 static void
14738 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
14739 {
14740 /* GCC will sometimes emit a 16-byte constant value as a DWARF
14741 location expression that pushes an implicit value. */
14742 if (attr->form == DW_FORM_exprloc)
14743 {
14744 dwarf_block *blk = attr->as_block ();
14745 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
14746 {
14747 uint64_t len;
14748 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
14749 blk->data + blk->size,
14750 &len);
14751 if (ptr - blk->data + len <= blk->size)
14752 {
14753 mpz_import (value->val, len,
14754 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14755 ? 1 : -1,
14756 1, 0, 0, ptr);
14757 return;
14758 }
14759 }
14760
14761 /* On failure set it to 1. */
14762 *value = gdb_mpz (1);
14763 }
14764 else if (attr->form_is_block ())
14765 {
14766 dwarf_block *blk = attr->as_block ();
14767 mpz_import (value->val, blk->size,
14768 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14769 ? 1 : -1,
14770 1, 0, 0, blk->data);
14771 }
14772 else
14773 *value = gdb_mpz (attr->constant_value (1));
14774 }
14775
14776 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
14777 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
14778
14779 If the numerator and/or numerator attribute is missing,
14780 a complaint is filed, and NUMERATOR and DENOMINATOR are left
14781 untouched. */
14782
14783 static void
14784 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
14785 gdb_mpz *numerator, gdb_mpz *denominator)
14786 {
14787 struct attribute *num_attr, *denom_attr;
14788
14789 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
14790 if (num_attr == nullptr)
14791 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
14792 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14793
14794 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
14795 if (denom_attr == nullptr)
14796 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
14797 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14798
14799 if (num_attr == nullptr || denom_attr == nullptr)
14800 return;
14801
14802 get_mpz (cu, numerator, num_attr);
14803 get_mpz (cu, denominator, denom_attr);
14804 }
14805
14806 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
14807 rational constant, rather than a signed one.
14808
14809 If the rational constant has a negative value, a complaint
14810 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
14811
14812 static void
14813 get_dwarf2_unsigned_rational_constant (struct die_info *die,
14814 struct dwarf2_cu *cu,
14815 gdb_mpz *numerator,
14816 gdb_mpz *denominator)
14817 {
14818 gdb_mpz num (1);
14819 gdb_mpz denom (1);
14820
14821 get_dwarf2_rational_constant (die, cu, &num, &denom);
14822 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
14823 {
14824 mpz_neg (num.val, num.val);
14825 mpz_neg (denom.val, denom.val);
14826 }
14827 else if (mpz_sgn (num.val) == -1)
14828 {
14829 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
14830 " in DIE at %s"),
14831 sect_offset_str (die->sect_off));
14832 return;
14833 }
14834 else if (mpz_sgn (denom.val) == -1)
14835 {
14836 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
14837 " in DIE at %s"),
14838 sect_offset_str (die->sect_off));
14839 return;
14840 }
14841
14842 *numerator = std::move (num);
14843 *denominator = std::move (denom);
14844 }
14845
14846 /* Assuming that ENCODING is a string whose contents starting at the
14847 K'th character is "_nn" where "nn" is a decimal number, scan that
14848 number and set RESULT to the value. K is updated to point to the
14849 character immediately following the number.
14850
14851 If the string does not conform to the format described above, false
14852 is returned, and K may or may not be changed. */
14853
14854 static bool
14855 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
14856 {
14857 /* The next character should be an underscore ('_') followed
14858 by a digit. */
14859 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
14860 return false;
14861
14862 /* Skip the underscore. */
14863 k++;
14864 int start = k;
14865
14866 /* Determine the number of digits for our number. */
14867 while (isdigit (encoding[k]))
14868 k++;
14869 if (k == start)
14870 return false;
14871
14872 std::string copy (&encoding[start], k - start);
14873 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
14874 return false;
14875
14876 return true;
14877 }
14878
14879 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
14880 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
14881 DENOM, update OFFSET, and return true on success. Return false on
14882 failure. */
14883
14884 static bool
14885 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
14886 gdb_mpz *num, gdb_mpz *denom)
14887 {
14888 if (!ada_get_gnat_encoded_number (encoding, offset, num))
14889 return false;
14890 return ada_get_gnat_encoded_number (encoding, offset, denom);
14891 }
14892
14893 /* Assuming DIE corresponds to a fixed point type, finish the creation
14894 of the corresponding TYPE by setting its type-specific data. CU is
14895 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
14896 encodings. It is nullptr if the GNAT encoding should be
14897 ignored. */
14898
14899 static void
14900 finish_fixed_point_type (struct type *type, const char *suffix,
14901 struct die_info *die, struct dwarf2_cu *cu)
14902 {
14903 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
14904 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
14905
14906 /* If GNAT encodings are preferred, don't examine the
14907 attributes. */
14908 struct attribute *attr = nullptr;
14909 if (suffix == nullptr)
14910 {
14911 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
14912 if (attr == nullptr)
14913 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
14914 if (attr == nullptr)
14915 attr = dwarf2_attr (die, DW_AT_small, cu);
14916 }
14917
14918 /* Numerator and denominator of our fixed-point type's scaling factor.
14919 The default is a scaling factor of 1, which we use as a fallback
14920 when we are not able to decode it (problem with the debugging info,
14921 unsupported forms, bug in GDB, etc...). Using that as the default
14922 allows us to at least print the unscaled value, which might still
14923 be useful to a user. */
14924 gdb_mpz scale_num (1);
14925 gdb_mpz scale_denom (1);
14926
14927 if (attr == nullptr)
14928 {
14929 int offset = 0;
14930 if (suffix != nullptr
14931 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14932 &scale_denom)
14933 /* The number might be encoded as _nn_dd_nn_dd, where the
14934 second ratio is the 'small value. In this situation, we
14935 want the second value. */
14936 && (suffix[offset] != '_'
14937 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14938 &scale_denom)))
14939 {
14940 /* Found it. */
14941 }
14942 else
14943 {
14944 /* Scaling factor not found. Assume a scaling factor of 1,
14945 and hope for the best. At least the user will be able to
14946 see the encoded value. */
14947 scale_num = 1;
14948 scale_denom = 1;
14949 complaint (_("no scale found for fixed-point type (DIE at %s)"),
14950 sect_offset_str (die->sect_off));
14951 }
14952 }
14953 else if (attr->name == DW_AT_binary_scale)
14954 {
14955 LONGEST scale_exp = attr->constant_value (0);
14956 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
14957
14958 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
14959 }
14960 else if (attr->name == DW_AT_decimal_scale)
14961 {
14962 LONGEST scale_exp = attr->constant_value (0);
14963 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
14964
14965 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
14966 }
14967 else if (attr->name == DW_AT_small)
14968 {
14969 struct die_info *scale_die;
14970 struct dwarf2_cu *scale_cu = cu;
14971
14972 scale_die = follow_die_ref (die, attr, &scale_cu);
14973 if (scale_die->tag == DW_TAG_constant)
14974 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
14975 &scale_num, &scale_denom);
14976 else
14977 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
14978 " (DIE at %s)"),
14979 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14980 }
14981 else
14982 {
14983 complaint (_("unsupported scale attribute %s for fixed-point type"
14984 " (DIE at %s)"),
14985 dwarf_attr_name (attr->name),
14986 sect_offset_str (die->sect_off));
14987 }
14988
14989 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
14990 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
14991 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
14992 mpq_canonicalize (scaling_factor.val);
14993 }
14994
14995 /* The gnat-encoding suffix for fixed point. */
14996
14997 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
14998
14999 /* If NAME encodes an Ada fixed-point type, return a pointer to the
15000 "XF" suffix of the name. The text after this is what encodes the
15001 'small and 'delta information. Otherwise, return nullptr. */
15002
15003 static const char *
15004 gnat_encoded_fixed_point_type_info (const char *name)
15005 {
15006 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
15007 }
15008
15009 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15010 (which may be different from NAME) to the architecture back-end to allow
15011 it to guess the correct format if necessary. */
15012
15013 static struct type *
15014 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15015 const char *name_hint, enum bfd_endian byte_order)
15016 {
15017 struct gdbarch *gdbarch = objfile->arch ();
15018 const struct floatformat **format;
15019 struct type *type;
15020
15021 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15022 if (format)
15023 type = init_float_type (objfile, bits, name, format, byte_order);
15024 else
15025 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15026
15027 return type;
15028 }
15029
15030 /* Allocate an integer type of size BITS and name NAME. */
15031
15032 static struct type *
15033 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
15034 int bits, int unsigned_p, const char *name)
15035 {
15036 struct type *type;
15037
15038 /* Versions of Intel's C Compiler generate an integer type called "void"
15039 instead of using DW_TAG_unspecified_type. This has been seen on
15040 at least versions 14, 17, and 18. */
15041 if (bits == 0 && producer_is_icc (cu) && name != nullptr
15042 && strcmp (name, "void") == 0)
15043 type = objfile_type (objfile)->builtin_void;
15044 else
15045 type = init_integer_type (objfile, bits, unsigned_p, name);
15046
15047 return type;
15048 }
15049
15050 /* Return true if DIE has a DW_AT_small attribute whose value is
15051 a constant rational, where both the numerator and denominator
15052 are equal to zero.
15053
15054 CU is the DIE's Compilation Unit. */
15055
15056 static bool
15057 has_zero_over_zero_small_attribute (struct die_info *die,
15058 struct dwarf2_cu *cu)
15059 {
15060 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
15061 if (attr == nullptr)
15062 return false;
15063
15064 struct dwarf2_cu *scale_cu = cu;
15065 struct die_info *scale_die
15066 = follow_die_ref (die, attr, &scale_cu);
15067
15068 if (scale_die->tag != DW_TAG_constant)
15069 return false;
15070
15071 gdb_mpz num (1), denom (1);
15072 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
15073 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
15074 }
15075
15076 /* Initialise and return a floating point type of size BITS suitable for
15077 use as a component of a complex number. The NAME_HINT is passed through
15078 when initialising the floating point type and is the name of the complex
15079 type.
15080
15081 As DWARF doesn't currently provide an explicit name for the components
15082 of a complex number, but it can be helpful to have these components
15083 named, we try to select a suitable name based on the size of the
15084 component. */
15085 static struct type *
15086 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
15087 struct objfile *objfile,
15088 int bits, const char *name_hint,
15089 enum bfd_endian byte_order)
15090 {
15091 gdbarch *gdbarch = objfile->arch ();
15092 struct type *tt = nullptr;
15093
15094 /* Try to find a suitable floating point builtin type of size BITS.
15095 We're going to use the name of this type as the name for the complex
15096 target type that we are about to create. */
15097 switch (cu->lang ())
15098 {
15099 case language_fortran:
15100 switch (bits)
15101 {
15102 case 32:
15103 tt = builtin_f_type (gdbarch)->builtin_real;
15104 break;
15105 case 64:
15106 tt = builtin_f_type (gdbarch)->builtin_real_s8;
15107 break;
15108 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15109 case 128:
15110 tt = builtin_f_type (gdbarch)->builtin_real_s16;
15111 break;
15112 }
15113 break;
15114 default:
15115 switch (bits)
15116 {
15117 case 32:
15118 tt = builtin_type (gdbarch)->builtin_float;
15119 break;
15120 case 64:
15121 tt = builtin_type (gdbarch)->builtin_double;
15122 break;
15123 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15124 case 128:
15125 tt = builtin_type (gdbarch)->builtin_long_double;
15126 break;
15127 }
15128 break;
15129 }
15130
15131 /* If the type we found doesn't match the size we were looking for, then
15132 pretend we didn't find a type at all, the complex target type we
15133 create will then be nameless. */
15134 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
15135 tt = nullptr;
15136
15137 const char *name = (tt == nullptr) ? nullptr : tt->name ();
15138 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
15139 }
15140
15141 /* Find a representation of a given base type and install
15142 it in the TYPE field of the die. */
15143
15144 static struct type *
15145 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15146 {
15147 struct objfile *objfile = cu->per_objfile->objfile;
15148 struct type *type;
15149 struct attribute *attr;
15150 int encoding = 0, bits = 0;
15151 const char *name;
15152 gdbarch *arch;
15153
15154 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15155 if (attr != nullptr && attr->form_is_constant ())
15156 encoding = attr->constant_value (0);
15157 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15158 if (attr != nullptr)
15159 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
15160 name = dwarf2_name (die, cu);
15161 if (!name)
15162 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
15163
15164 arch = objfile->arch ();
15165 enum bfd_endian byte_order = gdbarch_byte_order (arch);
15166
15167 attr = dwarf2_attr (die, DW_AT_endianity, cu);
15168 if (attr != nullptr && attr->form_is_constant ())
15169 {
15170 int endianity = attr->constant_value (0);
15171
15172 switch (endianity)
15173 {
15174 case DW_END_big:
15175 byte_order = BFD_ENDIAN_BIG;
15176 break;
15177 case DW_END_little:
15178 byte_order = BFD_ENDIAN_LITTLE;
15179 break;
15180 default:
15181 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
15182 break;
15183 }
15184 }
15185
15186 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
15187 && cu->lang () == language_ada
15188 && has_zero_over_zero_small_attribute (die, cu))
15189 {
15190 /* brobecker/2018-02-24: This is a fixed point type for which
15191 the scaling factor is represented as fraction whose value
15192 does not make sense (zero divided by zero), so we should
15193 normally never see these. However, there is a small category
15194 of fixed point types for which GNAT is unable to provide
15195 the scaling factor via the standard DWARF mechanisms, and
15196 for which the info is provided via the GNAT encodings instead.
15197 This is likely what this DIE is about. */
15198 encoding = (encoding == DW_ATE_signed_fixed
15199 ? DW_ATE_signed
15200 : DW_ATE_unsigned);
15201 }
15202
15203 /* With GNAT encodings, fixed-point information will be encoded in
15204 the type name. Note that this can also occur with the above
15205 zero-over-zero case, which is why this is a separate "if" rather
15206 than an "else if". */
15207 const char *gnat_encoding_suffix = nullptr;
15208 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
15209 && cu->lang () == language_ada
15210 && name != nullptr)
15211 {
15212 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
15213 if (gnat_encoding_suffix != nullptr)
15214 {
15215 gdb_assert (startswith (gnat_encoding_suffix,
15216 GNAT_FIXED_POINT_SUFFIX));
15217 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
15218 name, gnat_encoding_suffix - name);
15219 /* Use -1 here so that SUFFIX points at the "_" after the
15220 "XF". */
15221 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
15222
15223 encoding = (encoding == DW_ATE_signed
15224 ? DW_ATE_signed_fixed
15225 : DW_ATE_unsigned_fixed);
15226 }
15227 }
15228
15229 switch (encoding)
15230 {
15231 case DW_ATE_address:
15232 /* Turn DW_ATE_address into a void * pointer. */
15233 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
15234 type = init_pointer_type (objfile, bits, name, type);
15235 break;
15236 case DW_ATE_boolean:
15237 type = init_boolean_type (objfile, bits, 1, name);
15238 break;
15239 case DW_ATE_complex_float:
15240 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
15241 byte_order);
15242 if (type->code () == TYPE_CODE_ERROR)
15243 {
15244 if (name == nullptr)
15245 {
15246 struct obstack *obstack
15247 = &cu->per_objfile->objfile->objfile_obstack;
15248 name = obconcat (obstack, "_Complex ", type->name (),
15249 nullptr);
15250 }
15251 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15252 }
15253 else
15254 type = init_complex_type (name, type);
15255 break;
15256 case DW_ATE_decimal_float:
15257 type = init_decfloat_type (objfile, bits, name);
15258 break;
15259 case DW_ATE_float:
15260 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
15261 break;
15262 case DW_ATE_signed:
15263 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
15264 break;
15265 case DW_ATE_unsigned:
15266 if (cu->lang () == language_fortran
15267 && name
15268 && startswith (name, "character("))
15269 type = init_character_type (objfile, bits, 1, name);
15270 else
15271 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
15272 break;
15273 case DW_ATE_signed_char:
15274 if (cu->lang () == language_ada
15275 || cu->lang () == language_m2
15276 || cu->lang () == language_pascal
15277 || cu->lang () == language_fortran)
15278 type = init_character_type (objfile, bits, 0, name);
15279 else
15280 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
15281 break;
15282 case DW_ATE_unsigned_char:
15283 if (cu->lang () == language_ada
15284 || cu->lang () == language_m2
15285 || cu->lang () == language_pascal
15286 || cu->lang () == language_fortran
15287 || cu->lang () == language_rust)
15288 type = init_character_type (objfile, bits, 1, name);
15289 else
15290 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
15291 break;
15292 case DW_ATE_UTF:
15293 {
15294 type = init_character_type (objfile, bits, 1, name);
15295 return set_die_type (die, type, cu);
15296 }
15297 break;
15298 case DW_ATE_signed_fixed:
15299 type = init_fixed_point_type (objfile, bits, 0, name);
15300 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15301 break;
15302 case DW_ATE_unsigned_fixed:
15303 type = init_fixed_point_type (objfile, bits, 1, name);
15304 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15305 break;
15306
15307 default:
15308 complaint (_("unsupported DW_AT_encoding: '%s'"),
15309 dwarf_type_encoding_name (encoding));
15310 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15311 break;
15312 }
15313
15314 if (type->code () == TYPE_CODE_INT
15315 && name != nullptr
15316 && strcmp (name, "char") == 0)
15317 type->set_has_no_signedness (true);
15318
15319 maybe_set_alignment (cu, die, type);
15320
15321 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
15322
15323 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
15324 {
15325 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15326 if (attr != nullptr && attr->as_unsigned () <= 8 * type->length ())
15327 {
15328 unsigned real_bit_size = attr->as_unsigned ();
15329 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15330 /* Only use the attributes if they make sense together. */
15331 if (attr == nullptr
15332 || (attr->as_unsigned () + real_bit_size
15333 <= 8 * type->length ()))
15334 {
15335 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
15336 = real_bit_size;
15337 if (attr != nullptr)
15338 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
15339 = attr->as_unsigned ();
15340 }
15341 }
15342 }
15343
15344 return set_die_type (die, type, cu);
15345 }
15346
15347 /* A helper function that returns the name of DIE, if it refers to a
15348 variable declaration. */
15349
15350 static const char *
15351 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
15352 {
15353 if (die->tag != DW_TAG_variable)
15354 return nullptr;
15355
15356 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
15357 if (attr == nullptr || !attr->as_boolean ())
15358 return nullptr;
15359
15360 attr = dwarf2_attr (die, DW_AT_name, cu);
15361 if (attr == nullptr)
15362 return nullptr;
15363 return attr->as_string ();
15364 }
15365
15366 /* Parse dwarf attribute if it's a block, reference or constant and put the
15367 resulting value of the attribute into struct bound_prop.
15368 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15369
15370 static int
15371 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15372 struct dwarf2_cu *cu, struct dynamic_prop *prop,
15373 struct type *default_type)
15374 {
15375 struct dwarf2_property_baton *baton;
15376 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15377 struct objfile *objfile = per_objfile->objfile;
15378 struct obstack *obstack = &objfile->objfile_obstack;
15379
15380 gdb_assert (default_type != NULL);
15381
15382 if (attr == NULL || prop == NULL)
15383 return 0;
15384
15385 if (attr->form_is_block ())
15386 {
15387 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15388 baton->property_type = default_type;
15389 baton->locexpr.per_cu = cu->per_cu;
15390 baton->locexpr.per_objfile = per_objfile;
15391
15392 struct dwarf_block *block;
15393 if (attr->form == DW_FORM_data16)
15394 {
15395 size_t data_size = 16;
15396 block = XOBNEW (obstack, struct dwarf_block);
15397 block->size = (data_size
15398 + 2 /* Extra bytes for DW_OP and arg. */);
15399 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
15400 data[0] = DW_OP_implicit_value;
15401 data[1] = data_size;
15402 memcpy (&data[2], attr->as_block ()->data, data_size);
15403 block->data = data;
15404 }
15405 else
15406 block = attr->as_block ();
15407
15408 baton->locexpr.size = block->size;
15409 baton->locexpr.data = block->data;
15410 switch (attr->name)
15411 {
15412 case DW_AT_string_length:
15413 baton->locexpr.is_reference = true;
15414 break;
15415 default:
15416 baton->locexpr.is_reference = false;
15417 break;
15418 }
15419
15420 prop->set_locexpr (baton);
15421 gdb_assert (prop->baton () != NULL);
15422 }
15423 else if (attr->form_is_ref ())
15424 {
15425 struct dwarf2_cu *target_cu = cu;
15426 struct die_info *target_die;
15427 struct attribute *target_attr;
15428
15429 target_die = follow_die_ref (die, attr, &target_cu);
15430 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15431 if (target_attr == NULL)
15432 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15433 target_cu);
15434 if (target_attr == nullptr)
15435 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
15436 target_cu);
15437 if (target_attr == NULL)
15438 {
15439 const char *name = var_decl_name (target_die, target_cu);
15440 if (name != nullptr)
15441 {
15442 prop->set_variable_name (name);
15443 return 1;
15444 }
15445 return 0;
15446 }
15447
15448 switch (target_attr->name)
15449 {
15450 case DW_AT_location:
15451 if (target_attr->form_is_section_offset ())
15452 {
15453 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15454 baton->property_type = die_type (target_die, target_cu);
15455 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15456 prop->set_loclist (baton);
15457 gdb_assert (prop->baton () != NULL);
15458 }
15459 else if (target_attr->form_is_block ())
15460 {
15461 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15462 baton->property_type = die_type (target_die, target_cu);
15463 baton->locexpr.per_cu = cu->per_cu;
15464 baton->locexpr.per_objfile = per_objfile;
15465 struct dwarf_block *block = target_attr->as_block ();
15466 baton->locexpr.size = block->size;
15467 baton->locexpr.data = block->data;
15468 baton->locexpr.is_reference = true;
15469 prop->set_locexpr (baton);
15470 gdb_assert (prop->baton () != NULL);
15471 }
15472 else
15473 {
15474 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15475 "dynamic property");
15476 return 0;
15477 }
15478 break;
15479 case DW_AT_data_member_location:
15480 case DW_AT_data_bit_offset:
15481 {
15482 LONGEST offset;
15483
15484 if (!handle_member_location (target_die, target_cu, &offset))
15485 return 0;
15486
15487 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15488 baton->property_type = read_type_die (target_die->parent,
15489 target_cu);
15490 baton->offset_info.offset = offset;
15491 baton->offset_info.type = die_type (target_die, target_cu);
15492 prop->set_addr_offset (baton);
15493 break;
15494 }
15495 }
15496 }
15497 else if (attr->form_is_constant ())
15498 prop->set_const_val (attr->constant_value (0));
15499 else if (attr->form_is_section_offset ())
15500 {
15501 switch (attr->name)
15502 {
15503 case DW_AT_string_length:
15504 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15505 baton->property_type = default_type;
15506 fill_in_loclist_baton (cu, &baton->loclist, attr);
15507 prop->set_loclist (baton);
15508 gdb_assert (prop->baton () != NULL);
15509 break;
15510 default:
15511 goto invalid;
15512 }
15513 }
15514 else
15515 goto invalid;
15516
15517 return 1;
15518
15519 invalid:
15520 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15521 dwarf2_name (die, cu));
15522 return 0;
15523 }
15524
15525 /* See read.h. */
15526
15527 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
15528 present (which is valid) then compute the default type based on the
15529 compilation units address size. */
15530
15531 static struct type *
15532 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
15533 {
15534 struct type *index_type = die_type (die, cu);
15535
15536 /* Dwarf-2 specifications explicitly allows to create subrange types
15537 without specifying a base type.
15538 In that case, the base type must be set to the type of
15539 the lower bound, upper bound or count, in that order, if any of these
15540 three attributes references an object that has a type.
15541 If no base type is found, the Dwarf-2 specifications say that
15542 a signed integer type of size equal to the size of an address should
15543 be used.
15544 For the following C code: `extern char gdb_int [];'
15545 GCC produces an empty range DIE.
15546 FIXME: muller/2010-05-28: Possible references to object for low bound,
15547 high bound or count are not yet handled by this code. */
15548 if (index_type->code () == TYPE_CODE_VOID)
15549 index_type = cu->addr_sized_int_type (false);
15550
15551 return index_type;
15552 }
15553
15554 /* Read the given DW_AT_subrange DIE. */
15555
15556 static struct type *
15557 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15558 {
15559 struct type *base_type, *orig_base_type;
15560 struct type *range_type;
15561 struct attribute *attr;
15562 struct dynamic_prop low, high;
15563 int low_default_is_valid;
15564 int high_bound_is_count = 0;
15565 const char *name;
15566
15567 orig_base_type = read_subrange_index_type (die, cu);
15568
15569 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15570 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15571 creating the range type, but we use the result of check_typedef
15572 when examining properties of the type. */
15573 base_type = check_typedef (orig_base_type);
15574
15575 /* The die_type call above may have already set the type for this DIE. */
15576 range_type = get_die_type (die, cu);
15577 if (range_type)
15578 return range_type;
15579
15580 high.set_const_val (0);
15581
15582 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15583 omitting DW_AT_lower_bound. */
15584 switch (cu->lang ())
15585 {
15586 case language_c:
15587 case language_cplus:
15588 low.set_const_val (0);
15589 low_default_is_valid = 1;
15590 break;
15591 case language_fortran:
15592 low.set_const_val (1);
15593 low_default_is_valid = 1;
15594 break;
15595 case language_d:
15596 case language_objc:
15597 case language_rust:
15598 low.set_const_val (0);
15599 low_default_is_valid = (cu->header.version >= 4);
15600 break;
15601 case language_ada:
15602 case language_m2:
15603 case language_pascal:
15604 low.set_const_val (1);
15605 low_default_is_valid = (cu->header.version >= 4);
15606 break;
15607 default:
15608 low.set_const_val (0);
15609 low_default_is_valid = 0;
15610 break;
15611 }
15612
15613 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15614 if (attr != nullptr)
15615 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
15616 else if (!low_default_is_valid)
15617 complaint (_("Missing DW_AT_lower_bound "
15618 "- DIE at %s [in module %s]"),
15619 sect_offset_str (die->sect_off),
15620 objfile_name (cu->per_objfile->objfile));
15621
15622 struct attribute *attr_ub, *attr_count;
15623 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
15624 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15625 {
15626 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
15627 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15628 {
15629 /* If bounds are constant do the final calculation here. */
15630 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
15631 high.set_const_val (low.const_val () + high.const_val () - 1);
15632 else
15633 high_bound_is_count = 1;
15634 }
15635 else
15636 {
15637 if (attr_ub != NULL)
15638 complaint (_("Unresolved DW_AT_upper_bound "
15639 "- DIE at %s [in module %s]"),
15640 sect_offset_str (die->sect_off),
15641 objfile_name (cu->per_objfile->objfile));
15642 if (attr_count != NULL)
15643 complaint (_("Unresolved DW_AT_count "
15644 "- DIE at %s [in module %s]"),
15645 sect_offset_str (die->sect_off),
15646 objfile_name (cu->per_objfile->objfile));
15647 }
15648 }
15649
15650 LONGEST bias = 0;
15651 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
15652 if (bias_attr != nullptr && bias_attr->form_is_constant ())
15653 bias = bias_attr->constant_value (0);
15654
15655 /* Normally, the DWARF producers are expected to use a signed
15656 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15657 But this is unfortunately not always the case, as witnessed
15658 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15659 is used instead. To work around that ambiguity, we treat
15660 the bounds as signed, and thus sign-extend their values, when
15661 the base type is signed.
15662
15663 Skip it if the base type's length is larger than ULONGEST, to avoid
15664 the undefined behavior of a too large left shift. We don't really handle
15665 constants larger than 8 bytes anyway, at the moment. */
15666
15667 if (base_type->length () <= sizeof (ULONGEST))
15668 {
15669 ULONGEST negative_mask
15670 = -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
15671
15672 if (low.kind () == PROP_CONST
15673 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
15674 low.set_const_val (low.const_val () | negative_mask);
15675
15676 if (high.kind () == PROP_CONST
15677 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
15678 high.set_const_val (high.const_val () | negative_mask);
15679 }
15680
15681 /* Check for bit and byte strides. */
15682 struct dynamic_prop byte_stride_prop;
15683 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
15684 if (attr_byte_stride != nullptr)
15685 {
15686 struct type *prop_type = cu->addr_sized_int_type (false);
15687 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
15688 prop_type);
15689 }
15690
15691 struct dynamic_prop bit_stride_prop;
15692 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
15693 if (attr_bit_stride != nullptr)
15694 {
15695 /* It only makes sense to have either a bit or byte stride. */
15696 if (attr_byte_stride != nullptr)
15697 {
15698 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
15699 "- DIE at %s [in module %s]"),
15700 sect_offset_str (die->sect_off),
15701 objfile_name (cu->per_objfile->objfile));
15702 attr_bit_stride = nullptr;
15703 }
15704 else
15705 {
15706 struct type *prop_type = cu->addr_sized_int_type (false);
15707 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
15708 prop_type);
15709 }
15710 }
15711
15712 if (attr_byte_stride != nullptr
15713 || attr_bit_stride != nullptr)
15714 {
15715 bool byte_stride_p = (attr_byte_stride != nullptr);
15716 struct dynamic_prop *stride
15717 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
15718
15719 range_type
15720 = create_range_type_with_stride (NULL, orig_base_type, &low,
15721 &high, bias, stride, byte_stride_p);
15722 }
15723 else
15724 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
15725
15726 if (high_bound_is_count)
15727 range_type->bounds ()->flag_upper_bound_is_count = 1;
15728
15729 /* Ada expects an empty array on no boundary attributes. */
15730 if (attr == NULL && cu->lang () != language_ada)
15731 range_type->bounds ()->high.set_undefined ();
15732
15733 name = dwarf2_name (die, cu);
15734 if (name)
15735 range_type->set_name (name);
15736
15737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15738 if (attr != nullptr)
15739 range_type->set_length (attr->constant_value (0));
15740
15741 maybe_set_alignment (cu, die, range_type);
15742
15743 set_die_type (die, range_type, cu);
15744
15745 /* set_die_type should be already done. */
15746 set_descriptive_type (range_type, die, cu);
15747
15748 return range_type;
15749 }
15750
15751 static struct type *
15752 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15753 {
15754 struct type *type;
15755
15756 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
15757 type->set_name (dwarf2_name (die, cu));
15758
15759 /* In Ada, an unspecified type is typically used when the description
15760 of the type is deferred to a different unit. When encountering
15761 such a type, we treat it as a stub, and try to resolve it later on,
15762 when needed.
15763 Mark this as a stub type for all languages though. */
15764 type->set_is_stub (true);
15765
15766 return set_die_type (die, type, cu);
15767 }
15768
15769 /* Read a single die and all its descendents. Set the die's sibling
15770 field to NULL; set other fields in the die correctly, and set all
15771 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15772 location of the info_ptr after reading all of those dies. PARENT
15773 is the parent of the die in question. */
15774
15775 static struct die_info *
15776 read_die_and_children (const struct die_reader_specs *reader,
15777 const gdb_byte *info_ptr,
15778 const gdb_byte **new_info_ptr,
15779 struct die_info *parent)
15780 {
15781 struct die_info *die;
15782 const gdb_byte *cur_ptr;
15783
15784 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
15785 if (die == NULL)
15786 {
15787 *new_info_ptr = cur_ptr;
15788 return NULL;
15789 }
15790 store_in_ref_table (die, reader->cu);
15791
15792 if (die->has_children)
15793 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15794 else
15795 {
15796 die->child = NULL;
15797 *new_info_ptr = cur_ptr;
15798 }
15799
15800 die->sibling = NULL;
15801 die->parent = parent;
15802 return die;
15803 }
15804
15805 /* Read a die, all of its descendents, and all of its siblings; set
15806 all of the fields of all of the dies correctly. Arguments are as
15807 in read_die_and_children. */
15808
15809 static struct die_info *
15810 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15811 const gdb_byte *info_ptr,
15812 const gdb_byte **new_info_ptr,
15813 struct die_info *parent)
15814 {
15815 struct die_info *first_die, *last_sibling;
15816 const gdb_byte *cur_ptr;
15817
15818 cur_ptr = info_ptr;
15819 first_die = last_sibling = NULL;
15820
15821 while (1)
15822 {
15823 struct die_info *die
15824 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15825
15826 if (die == NULL)
15827 {
15828 *new_info_ptr = cur_ptr;
15829 return first_die;
15830 }
15831
15832 if (!first_die)
15833 first_die = die;
15834 else
15835 last_sibling->sibling = die;
15836
15837 last_sibling = die;
15838 }
15839 }
15840
15841 /* Read a die, all of its descendents, and all of its siblings; set
15842 all of the fields of all of the dies correctly. Arguments are as
15843 in read_die_and_children.
15844 This the main entry point for reading a DIE and all its children. */
15845
15846 static struct die_info *
15847 read_die_and_siblings (const struct die_reader_specs *reader,
15848 const gdb_byte *info_ptr,
15849 const gdb_byte **new_info_ptr,
15850 struct die_info *parent)
15851 {
15852 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15853 new_info_ptr, parent);
15854
15855 if (dwarf_die_debug)
15856 {
15857 gdb_printf (gdb_stdlog,
15858 "Read die from %s@0x%x of %s:\n",
15859 reader->die_section->get_name (),
15860 (unsigned) (info_ptr - reader->die_section->buffer),
15861 bfd_get_filename (reader->abfd));
15862 die->dump (dwarf_die_debug);
15863 }
15864
15865 return die;
15866 }
15867
15868 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15869 attributes.
15870 The caller is responsible for filling in the extra attributes
15871 and updating (*DIEP)->num_attrs.
15872 Set DIEP to point to a newly allocated die with its information,
15873 except for its child, sibling, and parent fields. */
15874
15875 static const gdb_byte *
15876 read_full_die_1 (const struct die_reader_specs *reader,
15877 struct die_info **diep, const gdb_byte *info_ptr,
15878 int num_extra_attrs)
15879 {
15880 unsigned int abbrev_number, bytes_read, i;
15881 const struct abbrev_info *abbrev;
15882 struct die_info *die;
15883 struct dwarf2_cu *cu = reader->cu;
15884 bfd *abfd = reader->abfd;
15885
15886 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15887 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15888 info_ptr += bytes_read;
15889 if (!abbrev_number)
15890 {
15891 *diep = NULL;
15892 return info_ptr;
15893 }
15894
15895 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
15896 if (!abbrev)
15897 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15898 abbrev_number,
15899 bfd_get_filename (abfd));
15900
15901 die = die_info::allocate (&cu->comp_unit_obstack,
15902 abbrev->num_attrs + num_extra_attrs);
15903 die->sect_off = sect_off;
15904 die->tag = abbrev->tag;
15905 die->abbrev = abbrev_number;
15906 die->has_children = abbrev->has_children;
15907
15908 /* Make the result usable.
15909 The caller needs to update num_attrs after adding the extra
15910 attributes. */
15911 die->num_attrs = abbrev->num_attrs;
15912
15913 bool any_need_reprocess = false;
15914 for (i = 0; i < abbrev->num_attrs; ++i)
15915 {
15916 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15917 info_ptr);
15918 if (die->attrs[i].requires_reprocessing_p ())
15919 any_need_reprocess = true;
15920 }
15921
15922 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
15923 if (attr != nullptr && attr->form_is_unsigned ())
15924 cu->str_offsets_base = attr->as_unsigned ();
15925
15926 attr = die->attr (DW_AT_loclists_base);
15927 if (attr != nullptr)
15928 cu->loclist_base = attr->as_unsigned ();
15929
15930 auto maybe_addr_base = die->addr_base ();
15931 if (maybe_addr_base.has_value ())
15932 cu->addr_base = *maybe_addr_base;
15933
15934 attr = die->attr (DW_AT_rnglists_base);
15935 if (attr != nullptr)
15936 cu->rnglists_base = attr->as_unsigned ();
15937
15938 if (any_need_reprocess)
15939 {
15940 for (i = 0; i < abbrev->num_attrs; ++i)
15941 {
15942 if (die->attrs[i].requires_reprocessing_p ())
15943 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
15944 }
15945 }
15946 *diep = die;
15947 return info_ptr;
15948 }
15949
15950 /* Read a die and all its attributes.
15951 Set DIEP to point to a newly allocated die with its information,
15952 except for its child, sibling, and parent fields. */
15953
15954 static const gdb_byte *
15955 read_full_die (const struct die_reader_specs *reader,
15956 struct die_info **diep, const gdb_byte *info_ptr)
15957 {
15958 const gdb_byte *result;
15959
15960 result = read_full_die_1 (reader, diep, info_ptr, 0);
15961
15962 if (dwarf_die_debug)
15963 {
15964 gdb_printf (gdb_stdlog,
15965 "Read die from %s@0x%x of %s:\n",
15966 reader->die_section->get_name (),
15967 (unsigned) (info_ptr - reader->die_section->buffer),
15968 bfd_get_filename (reader->abfd));
15969 (*diep)->dump (dwarf_die_debug);
15970 }
15971
15972 return result;
15973 }
15974 \f
15975
15976 void
15977 cooked_indexer::check_bounds (cutu_reader *reader)
15978 {
15979 if (reader->cu->per_cu->addresses_seen)
15980 return;
15981
15982 dwarf2_cu *cu = reader->cu;
15983
15984 CORE_ADDR best_lowpc = 0, best_highpc = 0;
15985 /* Possibly set the default values of LOWPC and HIGHPC from
15986 `DW_AT_ranges'. */
15987 dwarf2_find_base_address (reader->comp_unit_die, cu);
15988 enum pc_bounds_kind cu_bounds_kind
15989 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
15990 cu, m_index_storage->get_addrmap (), cu->per_cu);
15991 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
15992 {
15993 struct objfile *objfile = cu->per_objfile->objfile;
15994 CORE_ADDR baseaddr = objfile->text_section_offset ();
15995 struct gdbarch *gdbarch = objfile->arch ();
15996 CORE_ADDR low
15997 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
15998 - baseaddr);
15999 CORE_ADDR high
16000 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
16001 - baseaddr - 1);
16002 /* Store the contiguous range if it is not empty; it can be
16003 empty for CUs with no code. */
16004 m_index_storage->get_addrmap ()->set_empty (low, high, cu->per_cu);
16005
16006 cu->per_cu->addresses_seen = true;
16007 }
16008 }
16009
16010 /* Helper function that returns true if TAG can have a linkage
16011 name. */
16012
16013 static bool
16014 tag_can_have_linkage_name (enum dwarf_tag tag)
16015 {
16016 switch (tag)
16017 {
16018 case DW_TAG_variable:
16019 case DW_TAG_subprogram:
16020 return true;
16021
16022 default:
16023 return false;
16024 }
16025 }
16026
16027 cutu_reader *
16028 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
16029 dwarf2_per_objfile *per_objfile,
16030 sect_offset sect_off, bool is_dwz,
16031 bool for_scanning)
16032 {
16033 /* Lookups for type unit references are always in the CU, and
16034 cross-CU references will crash. */
16035 if (reader->cu->per_cu->is_dwz == is_dwz
16036 && reader->cu->header.offset_in_cu_p (sect_off))
16037 return reader;
16038
16039 dwarf2_per_cu_data *per_cu
16040 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
16041 per_objfile->per_bfd);
16042
16043 /* When scanning, we only want to visit a given CU a single time.
16044 Doing this check here avoids self-imports as well. */
16045 if (for_scanning)
16046 {
16047 bool nope = false;
16048 if (!per_cu->scanned.compare_exchange_strong (nope, true))
16049 return nullptr;
16050 }
16051 if (per_cu == m_per_cu)
16052 return reader;
16053
16054 cutu_reader *result = m_index_storage->get_reader (per_cu);
16055 if (result == nullptr)
16056 {
16057 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
16058 m_index_storage->get_abbrev_cache ());
16059
16060 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
16061 language_minimal);
16062 std::unique_ptr<cutu_reader> copy
16063 (new cutu_reader (std::move (new_reader)));
16064 result = m_index_storage->preserve (std::move (copy));
16065 }
16066
16067 if (result->dummy_p || !result->comp_unit_die->has_children)
16068 return nullptr;
16069
16070 if (for_scanning)
16071 check_bounds (result);
16072
16073 return result;
16074 }
16075
16076 const gdb_byte *
16077 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
16078 cutu_reader *reader,
16079 const gdb_byte *watermark_ptr,
16080 const gdb_byte *info_ptr,
16081 const abbrev_info *abbrev,
16082 const char **name,
16083 const char **linkage_name,
16084 cooked_index_flag *flags,
16085 sect_offset *sibling_offset,
16086 const cooked_index_entry **parent_entry,
16087 CORE_ADDR *maybe_defer,
16088 bool for_specification)
16089 {
16090 bool origin_is_dwz = false;
16091 bool is_declaration = false;
16092 sect_offset origin_offset {};
16093
16094 gdb::optional<CORE_ADDR> low_pc;
16095 gdb::optional<CORE_ADDR> high_pc;
16096 bool high_pc_relative = false;
16097
16098 for (int i = 0; i < abbrev->num_attrs; ++i)
16099 {
16100 attribute attr;
16101 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16102 if (attr.requires_reprocessing_p ())
16103 read_attribute_reprocess (reader, &attr, abbrev->tag);
16104
16105 /* Store the data if it is of an attribute we want to keep in a
16106 partial symbol table. */
16107 switch (attr.name)
16108 {
16109 case DW_AT_name:
16110 switch (abbrev->tag)
16111 {
16112 case DW_TAG_compile_unit:
16113 case DW_TAG_partial_unit:
16114 case DW_TAG_type_unit:
16115 /* Compilation units have a DW_AT_name that is a filename, not
16116 a source language identifier. */
16117 break;
16118
16119 default:
16120 if (*name == nullptr)
16121 *name = attr.as_string ();
16122 break;
16123 }
16124 break;
16125
16126 case DW_AT_linkage_name:
16127 case DW_AT_MIPS_linkage_name:
16128 /* Note that both forms of linkage name might appear. We
16129 assume they will be the same, and we only store the last
16130 one we see. */
16131 if (*linkage_name == nullptr)
16132 *linkage_name = attr.as_string ();
16133 break;
16134
16135 case DW_AT_main_subprogram:
16136 if (attr.as_boolean ())
16137 *flags |= IS_MAIN;
16138 break;
16139
16140 case DW_AT_declaration:
16141 is_declaration = attr.as_boolean ();
16142 break;
16143
16144 case DW_AT_sibling:
16145 if (sibling_offset != nullptr)
16146 *sibling_offset = attr.get_ref_die_offset ();
16147 break;
16148
16149 case DW_AT_specification:
16150 case DW_AT_abstract_origin:
16151 case DW_AT_extension:
16152 origin_offset = attr.get_ref_die_offset ();
16153 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
16154 break;
16155
16156 case DW_AT_external:
16157 if (attr.as_boolean ())
16158 *flags &= ~IS_STATIC;
16159 break;
16160
16161 case DW_AT_enum_class:
16162 if (attr.as_boolean ())
16163 *flags |= IS_ENUM_CLASS;
16164 break;
16165
16166 case DW_AT_low_pc:
16167 low_pc = attr.as_address ();
16168 break;
16169
16170 case DW_AT_high_pc:
16171 high_pc = attr.as_address ();
16172 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
16173 high_pc_relative = true;
16174 break;
16175
16176 case DW_AT_location:
16177 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
16178 {
16179 struct dwarf_block *locdesc = attr.as_block ();
16180 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
16181 if (addr != 0
16182 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
16183 {
16184 low_pc = addr;
16185 /* For variables, we don't want to try decoding the
16186 type just to find the size -- for gdb's purposes
16187 we only need the address of a variable. */
16188 high_pc = addr + 1;
16189 high_pc_relative = false;
16190 }
16191 }
16192 break;
16193
16194 case DW_AT_ranges:
16195 if (!scanning_per_cu->addresses_seen)
16196 {
16197 /* Offset in the .debug_ranges or .debug_rnglist section
16198 (depending on DWARF version). */
16199 ULONGEST ranges_offset = attr.as_unsigned ();
16200
16201 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
16202 want to add this value. */
16203 ranges_offset += reader->cu->gnu_ranges_base;
16204
16205 CORE_ADDR lowpc, highpc;
16206 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
16207 m_index_storage->get_addrmap (),
16208 scanning_per_cu, abbrev->tag);
16209 }
16210 break;
16211 }
16212 }
16213
16214 /* We don't want to examine declarations, but if we found a
16215 declaration when handling DW_AT_specification or the like, then
16216 that is ok. Similarly, we allow an external variable without a
16217 location; those are resolved via minimal symbols. */
16218 if (is_declaration && !for_specification
16219 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
16220 {
16221 /* We always want to recurse into some types, but we may not
16222 want to treat them as definitions. */
16223 if ((abbrev->tag == DW_TAG_class_type
16224 || abbrev->tag == DW_TAG_structure_type
16225 || abbrev->tag == DW_TAG_union_type)
16226 && abbrev->has_children)
16227 *flags |= IS_TYPE_DECLARATION;
16228 else
16229 {
16230 *linkage_name = nullptr;
16231 *name = nullptr;
16232 }
16233 }
16234 else if ((*name == nullptr
16235 || (*linkage_name == nullptr
16236 && tag_can_have_linkage_name (abbrev->tag))
16237 || (*parent_entry == nullptr && m_language != language_c))
16238 && origin_offset != sect_offset (0))
16239 {
16240 cutu_reader *new_reader
16241 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
16242 origin_is_dwz, false);
16243 if (new_reader != nullptr)
16244 {
16245 const gdb_byte *new_info_ptr = (new_reader->buffer
16246 + to_underlying (origin_offset));
16247
16248 if (new_reader->cu == reader->cu
16249 && new_info_ptr > watermark_ptr
16250 && *parent_entry == nullptr)
16251 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
16252 else if (*parent_entry == nullptr)
16253 {
16254 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
16255 void *obj = m_die_range_map.find (lookup);
16256 *parent_entry = static_cast <cooked_index_entry *> (obj);
16257 }
16258
16259 unsigned int bytes_read;
16260 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
16261 new_info_ptr,
16262 &bytes_read);
16263 new_info_ptr += bytes_read;
16264 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
16265 new_abbrev, name, linkage_name, flags, nullptr,
16266 parent_entry, maybe_defer, true);
16267 }
16268 }
16269
16270 if (!for_specification)
16271 {
16272 if (m_language == language_ada
16273 && *linkage_name == nullptr)
16274 *linkage_name = *name;
16275
16276 if (!scanning_per_cu->addresses_seen
16277 && low_pc.has_value ()
16278 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
16279 || *low_pc != 0)
16280 && high_pc.has_value ())
16281 {
16282 if (high_pc_relative)
16283 high_pc = *high_pc + *low_pc;
16284
16285 if (*high_pc > *low_pc)
16286 {
16287 struct objfile *objfile = reader->cu->per_objfile->objfile;
16288 CORE_ADDR baseaddr = objfile->text_section_offset ();
16289 struct gdbarch *gdbarch = objfile->arch ();
16290 CORE_ADDR lo
16291 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
16292 - baseaddr);
16293 CORE_ADDR hi
16294 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
16295 - baseaddr);
16296 m_index_storage->get_addrmap ()->set_empty (lo, hi - 1,
16297 scanning_per_cu);
16298 }
16299 }
16300
16301 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
16302 *flags &= ~IS_STATIC;
16303
16304 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
16305 *name = "(anonymous namespace)";
16306
16307 if (m_language == language_cplus
16308 && (abbrev->tag == DW_TAG_class_type
16309 || abbrev->tag == DW_TAG_interface_type
16310 || abbrev->tag == DW_TAG_structure_type
16311 || abbrev->tag == DW_TAG_union_type
16312 || abbrev->tag == DW_TAG_enumeration_type
16313 || abbrev->tag == DW_TAG_enumerator))
16314 *flags &= ~IS_STATIC;
16315 }
16316
16317 return info_ptr;
16318 }
16319
16320 const gdb_byte *
16321 cooked_indexer::index_imported_unit (cutu_reader *reader,
16322 const gdb_byte *info_ptr,
16323 const abbrev_info *abbrev)
16324 {
16325 sect_offset sect_off {};
16326 bool is_dwz = false;
16327
16328 for (int i = 0; i < abbrev->num_attrs; ++i)
16329 {
16330 /* Note that we never need to reprocess attributes here. */
16331 attribute attr;
16332 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16333
16334 if (attr.name == DW_AT_import)
16335 {
16336 sect_off = attr.get_ref_die_offset ();
16337 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16338 || reader->cu->per_cu->is_dwz);
16339 }
16340 }
16341
16342 /* Did not find DW_AT_import. */
16343 if (sect_off == sect_offset (0))
16344 return info_ptr;
16345
16346 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
16347 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
16348 is_dwz, true);
16349 if (new_reader != nullptr)
16350 {
16351 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
16352
16353 reader->cu->add_dependence (new_reader->cu->per_cu);
16354 }
16355
16356 return info_ptr;
16357 }
16358
16359 const gdb_byte *
16360 cooked_indexer::recurse (cutu_reader *reader,
16361 const gdb_byte *info_ptr,
16362 const cooked_index_entry *parent_entry,
16363 bool fully)
16364 {
16365 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16366
16367 if (parent_entry != nullptr)
16368 {
16369 CORE_ADDR start = form_addr (parent_entry->die_offset,
16370 reader->cu->per_cu->is_dwz);
16371 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
16372 reader->cu->per_cu->is_dwz);
16373 m_die_range_map.set_empty (start, end, (void *) parent_entry);
16374 }
16375
16376 return info_ptr;
16377 }
16378
16379 const gdb_byte *
16380 cooked_indexer::index_dies (cutu_reader *reader,
16381 const gdb_byte *info_ptr,
16382 const cooked_index_entry *parent_entry,
16383 bool fully)
16384 {
16385 const gdb_byte *end_ptr = (reader->buffer
16386 + to_underlying (reader->cu->header.sect_off)
16387 + reader->cu->header.get_length_with_initial ());
16388
16389 while (info_ptr < end_ptr)
16390 {
16391 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
16392 unsigned int bytes_read;
16393 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
16394 &bytes_read);
16395 info_ptr += bytes_read;
16396 if (abbrev == nullptr)
16397 break;
16398
16399 if (abbrev->tag == DW_TAG_imported_unit)
16400 {
16401 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
16402 continue;
16403 }
16404
16405 if (!abbrev->interesting)
16406 {
16407 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
16408 if (fully && abbrev->has_children)
16409 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16410 continue;
16411 }
16412
16413 const char *name = nullptr;
16414 const char *linkage_name = nullptr;
16415 CORE_ADDR defer = 0;
16416 cooked_index_flag flags = IS_STATIC;
16417 sect_offset sibling {};
16418 const cooked_index_entry *this_parent_entry = parent_entry;
16419 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
16420 info_ptr, abbrev, &name, &linkage_name,
16421 &flags, &sibling, &this_parent_entry,
16422 &defer, false);
16423
16424 if (abbrev->tag == DW_TAG_namespace
16425 && m_language == language_cplus
16426 && strcmp (name, "::") == 0)
16427 {
16428 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
16429 generated bogus DW_TAG_namespace DIEs with a name of "::"
16430 for the global namespace. Work around this problem
16431 here. */
16432 name = nullptr;
16433 }
16434
16435 const cooked_index_entry *this_entry = nullptr;
16436 if (name != nullptr)
16437 {
16438 if (defer != 0)
16439 m_deferred_entries.push_back ({
16440 this_die, name, defer, abbrev->tag, flags
16441 });
16442 else
16443 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
16444 name, this_parent_entry,
16445 m_per_cu);
16446 }
16447
16448 if (linkage_name != nullptr)
16449 {
16450 /* We only want this to be "main" if it has a linkage name
16451 but not an ordinary name. */
16452 if (name != nullptr)
16453 flags = flags & ~IS_MAIN;
16454 /* Set the IS_LINKAGE on for everything except when functions
16455 have linkage name present but name is absent. */
16456 if (name != nullptr
16457 || (abbrev->tag != DW_TAG_subprogram
16458 && abbrev->tag != DW_TAG_inlined_subroutine
16459 && abbrev->tag != DW_TAG_entry_point))
16460 flags = flags | IS_LINKAGE;
16461 m_index_storage->add (this_die, abbrev->tag, flags,
16462 linkage_name, nullptr, m_per_cu);
16463 }
16464
16465 if (abbrev->has_children)
16466 {
16467 switch (abbrev->tag)
16468 {
16469 case DW_TAG_class_type:
16470 case DW_TAG_interface_type:
16471 case DW_TAG_structure_type:
16472 case DW_TAG_union_type:
16473 if (m_language != language_c && this_entry != nullptr)
16474 {
16475 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16476 continue;
16477 }
16478 break;
16479
16480 case DW_TAG_enumeration_type:
16481 /* We need to recurse even for an anonymous enumeration.
16482 Which scope we record as the parent scope depends on
16483 whether we're reading an "enum class". If so, we use
16484 the enum itself as the parent, yielding names like
16485 "enum_class::enumerator"; otherwise we inject the
16486 names into our own parent scope. */
16487 info_ptr = recurse (reader, info_ptr,
16488 ((flags & IS_ENUM_CLASS) == 0)
16489 ? parent_entry
16490 : this_entry,
16491 fully);
16492 continue;
16493
16494 case DW_TAG_module:
16495 if (this_entry == nullptr)
16496 break;
16497 /* FALLTHROUGH */
16498 case DW_TAG_namespace:
16499 /* We don't check THIS_ENTRY for a namespace, to handle
16500 the ancient G++ workaround pointed out above. */
16501 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16502 continue;
16503
16504 case DW_TAG_subprogram:
16505 if ((m_language == language_fortran
16506 || m_language == language_ada)
16507 && this_entry != nullptr)
16508 {
16509 info_ptr = recurse (reader, info_ptr, this_entry, true);
16510 continue;
16511 }
16512 break;
16513 }
16514
16515 if (sibling != sect_offset (0))
16516 {
16517 const gdb_byte *sibling_ptr
16518 = reader->buffer + to_underlying (sibling);
16519
16520 if (sibling_ptr < info_ptr)
16521 complaint (_("DW_AT_sibling points backwards"));
16522 else if (sibling_ptr > reader->buffer_end)
16523 reader->die_section->overflow_complaint ();
16524 else
16525 info_ptr = sibling_ptr;
16526 }
16527 else
16528 info_ptr = skip_children (reader, info_ptr);
16529 }
16530 }
16531
16532 return info_ptr;
16533 }
16534
16535 void
16536 cooked_indexer::make_index (cutu_reader *reader)
16537 {
16538 check_bounds (reader);
16539 find_file_and_directory (reader->comp_unit_die, reader->cu);
16540 if (!reader->comp_unit_die->has_children)
16541 return;
16542 index_dies (reader, reader->info_ptr, nullptr, false);
16543
16544 for (const auto &entry : m_deferred_entries)
16545 {
16546 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
16547 void *obj = m_die_range_map.find (key);
16548 cooked_index_entry *parent = static_cast <cooked_index_entry *> (obj);
16549 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
16550 entry.name, parent, m_per_cu);
16551 }
16552 }
16553
16554 /* An implementation of quick_symbol_functions for the cooked DWARF
16555 index. */
16556
16557 struct cooked_index_functions : public dwarf2_base_index_functions
16558 {
16559 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
16560 CORE_ADDR adjusted_pc) override;
16561
16562 struct compunit_symtab *find_compunit_symtab_by_address
16563 (struct objfile *objfile, CORE_ADDR address) override;
16564
16565 void dump (struct objfile *objfile) override
16566 {
16567 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16568 cooked_index *index
16569 = (gdb::checked_static_cast<cooked_index *>
16570 (per_objfile->per_bfd->index_table.get ()));
16571 if (index == nullptr)
16572 return;
16573
16574 gdb_printf ("Cooked index in use:\n");
16575 gdb_printf ("\n");
16576 index->dump (objfile->arch ());
16577 }
16578
16579 void expand_matching_symbols
16580 (struct objfile *,
16581 const lookup_name_info &lookup_name,
16582 domain_enum domain,
16583 int global,
16584 symbol_compare_ftype *ordered_compare) override;
16585
16586 bool expand_symtabs_matching
16587 (struct objfile *objfile,
16588 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16589 const lookup_name_info *lookup_name,
16590 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16591 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16592 block_search_flags search_flags,
16593 domain_enum domain,
16594 enum search_domain kind) override;
16595
16596 bool can_lazily_read_symbols () override
16597 {
16598 return true;
16599 }
16600
16601 void read_partial_symbols (struct objfile *objfile) override
16602 {
16603 if (dwarf2_has_info (objfile, nullptr))
16604 dwarf2_build_psymtabs (objfile);
16605 }
16606 };
16607
16608 dwarf2_per_cu_data *
16609 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
16610 CORE_ADDR adjusted_pc)
16611 {
16612 cooked_index *table
16613 = (gdb::checked_static_cast<cooked_index *>
16614 (per_bfd->index_table.get ()));
16615 if (table == nullptr)
16616 return nullptr;
16617 return table->lookup (adjusted_pc);
16618 }
16619
16620 struct compunit_symtab *
16621 cooked_index_functions::find_compunit_symtab_by_address
16622 (struct objfile *objfile, CORE_ADDR address)
16623 {
16624 if (objfile->sect_index_data == -1)
16625 return nullptr;
16626
16627 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16628 cooked_index *table
16629 = (gdb::checked_static_cast<cooked_index *>
16630 (per_objfile->per_bfd->index_table.get ()));
16631 if (table == nullptr)
16632 return nullptr;
16633
16634 CORE_ADDR baseaddr = objfile->data_section_offset ();
16635 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
16636 if (per_cu == nullptr)
16637 return nullptr;
16638
16639 return dw2_instantiate_symtab (per_cu, per_objfile, false);
16640 }
16641
16642 void
16643 cooked_index_functions::expand_matching_symbols
16644 (struct objfile *objfile,
16645 const lookup_name_info &lookup_name,
16646 domain_enum domain,
16647 int global,
16648 symbol_compare_ftype *ordered_compare)
16649 {
16650 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16651 cooked_index *table
16652 = (gdb::checked_static_cast<cooked_index *>
16653 (per_objfile->per_bfd->index_table.get ()));
16654 if (table == nullptr)
16655 return;
16656
16657 const block_search_flags search_flags = (global
16658 ? SEARCH_GLOBAL_BLOCK
16659 : SEARCH_STATIC_BLOCK);
16660 const language_defn *lang = language_def (language_ada);
16661 symbol_name_matcher_ftype *name_match
16662 = lang->get_symbol_name_matcher (lookup_name);
16663
16664 for (const cooked_index_entry *entry : table->all_entries ())
16665 {
16666 QUIT;
16667
16668 if (entry->parent_entry != nullptr)
16669 continue;
16670
16671 if (!entry->matches (search_flags)
16672 || !entry->matches (domain))
16673 continue;
16674
16675 if (name_match (entry->canonical, lookup_name, nullptr))
16676 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
16677 }
16678 }
16679
16680 bool
16681 cooked_index_functions::expand_symtabs_matching
16682 (struct objfile *objfile,
16683 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16684 const lookup_name_info *lookup_name,
16685 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16686 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16687 block_search_flags search_flags,
16688 domain_enum domain,
16689 enum search_domain kind)
16690 {
16691 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16692
16693 cooked_index *table
16694 = (gdb::checked_static_cast<cooked_index *>
16695 (per_objfile->per_bfd->index_table.get ()));
16696 if (table == nullptr)
16697 return true;
16698
16699 table->wait ();
16700
16701 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
16702
16703 /* This invariant is documented in quick-functions.h. */
16704 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
16705 if (lookup_name == nullptr)
16706 {
16707 for (dwarf2_per_cu_data *per_cu
16708 : all_units_range (per_objfile->per_bfd))
16709 {
16710 QUIT;
16711
16712 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
16713 file_matcher,
16714 expansion_notify))
16715 return false;
16716 }
16717 return true;
16718 }
16719
16720 lookup_name_info lookup_name_without_params
16721 = lookup_name->make_ignore_params ();
16722 bool completing = lookup_name->completion_mode ();
16723
16724 /* Unique styles of language splitting. */
16725 static const enum language unique_styles[] =
16726 {
16727 /* No splitting is also a style. */
16728 language_c,
16729 /* This includes Rust. */
16730 language_cplus,
16731 /* This includes Go. */
16732 language_d,
16733 language_ada
16734 };
16735
16736 for (enum language lang : unique_styles)
16737 {
16738 std::vector<gdb::string_view> name_vec
16739 = lookup_name_without_params.split_name (lang);
16740 std::string last_name = gdb::to_string (name_vec.back ());
16741
16742 for (const cooked_index_entry *entry : table->find (last_name,
16743 completing))
16744 {
16745 QUIT;
16746
16747 /* No need to consider symbols from expanded CUs. */
16748 if (per_objfile->symtab_set_p (entry->per_cu))
16749 continue;
16750
16751 /* If file-matching was done, we don't need to consider
16752 symbols from unmarked CUs. */
16753 if (file_matcher != nullptr && !entry->per_cu->mark)
16754 continue;
16755
16756 /* See if the symbol matches the type filter. */
16757 if (!entry->matches (search_flags)
16758 || !entry->matches (domain)
16759 || !entry->matches (kind))
16760 continue;
16761
16762 /* We've found the base name of the symbol; now walk its
16763 parentage chain, ensuring that each component
16764 matches. */
16765 bool found = true;
16766
16767 const cooked_index_entry *parent = entry->parent_entry;
16768 for (int i = name_vec.size () - 1; i > 0; --i)
16769 {
16770 /* If we ran out of entries, or if this segment doesn't
16771 match, this did not match. */
16772 if (parent == nullptr
16773 || strncmp (parent->name, name_vec[i - 1].data (),
16774 name_vec[i - 1].length ()) != 0)
16775 {
16776 found = false;
16777 break;
16778 }
16779
16780 parent = parent->parent_entry;
16781 }
16782
16783 if (!found)
16784 continue;
16785
16786 /* Might have been looking for "a::b" and found
16787 "x::a::b". */
16788 if (symbol_matcher == nullptr)
16789 {
16790 symbol_name_match_type match_type
16791 = lookup_name_without_params.match_type ();
16792 if ((match_type == symbol_name_match_type::FULL
16793 || (lang != language_ada
16794 && match_type == symbol_name_match_type::EXPRESSION))
16795 && parent != nullptr)
16796 continue;
16797 }
16798 else
16799 {
16800 auto_obstack temp_storage;
16801 const char *full_name = entry->full_name (&temp_storage);
16802 if (!symbol_matcher (full_name))
16803 continue;
16804 }
16805
16806 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
16807 file_matcher,
16808 expansion_notify))
16809 return false;
16810 }
16811 }
16812
16813 return true;
16814 }
16815
16816 /* Return a new cooked_index_functions object. */
16817
16818 static quick_symbol_functions_up
16819 make_cooked_index_funcs ()
16820 {
16821 return quick_symbol_functions_up (new cooked_index_functions);
16822 }
16823
16824 quick_symbol_functions_up
16825 cooked_index::make_quick_functions () const
16826 {
16827 return make_cooked_index_funcs ();
16828 }
16829
16830 \f
16831
16832 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
16833 contents from the given SECTION in the HEADER.
16834
16835 HEADER_OFFSET is the offset of the header in the section. */
16836 static void
16837 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
16838 struct dwarf2_section_info *section,
16839 sect_offset header_offset)
16840 {
16841 unsigned int bytes_read;
16842 bfd *abfd = section->get_bfd_owner ();
16843 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
16844
16845 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
16846 info_ptr += bytes_read;
16847
16848 header->version = read_2_bytes (abfd, info_ptr);
16849 info_ptr += 2;
16850
16851 header->addr_size = read_1_byte (abfd, info_ptr);
16852 info_ptr += 1;
16853
16854 header->segment_collector_size = read_1_byte (abfd, info_ptr);
16855 info_ptr += 1;
16856
16857 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
16858 }
16859
16860 /* Return the DW_AT_loclists_base value for the CU. */
16861 static ULONGEST
16862 lookup_loclist_base (struct dwarf2_cu *cu)
16863 {
16864 /* For the .dwo unit, the loclist_base points to the first offset following
16865 the header. The header consists of the following entities-
16866 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
16867 bit format)
16868 2. version (2 bytes)
16869 3. address size (1 byte)
16870 4. segment selector size (1 byte)
16871 5. offset entry count (4 bytes)
16872 These sizes are derived as per the DWARFv5 standard. */
16873 if (cu->dwo_unit != nullptr)
16874 {
16875 if (cu->header.initial_length_size == 4)
16876 return LOCLIST_HEADER_SIZE32;
16877 return LOCLIST_HEADER_SIZE64;
16878 }
16879 return cu->loclist_base;
16880 }
16881
16882 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
16883 array of offsets in the .debug_loclists section. */
16884
16885 static sect_offset
16886 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
16887 {
16888 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16889 struct objfile *objfile = per_objfile->objfile;
16890 bfd *abfd = objfile->obfd.get ();
16891 ULONGEST loclist_header_size =
16892 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
16893 : LOCLIST_HEADER_SIZE64);
16894 ULONGEST loclist_base = lookup_loclist_base (cu);
16895
16896 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
16897 ULONGEST start_offset =
16898 loclist_base + loclist_index * cu->header.offset_size;
16899
16900 /* Get loclists section. */
16901 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16902
16903 /* Read the loclists section content. */
16904 section->read (objfile);
16905 if (section->buffer == NULL)
16906 error (_("DW_FORM_loclistx used without .debug_loclists "
16907 "section [in module %s]"), objfile_name (objfile));
16908
16909 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
16910 so if loclist_base is smaller than the header size, we have a problem. */
16911 if (loclist_base < loclist_header_size)
16912 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
16913 objfile_name (objfile));
16914
16915 /* Read the header of the loclists contribution. */
16916 struct loclists_rnglists_header header;
16917 read_loclists_rnglists_header (&header, section,
16918 (sect_offset) (loclist_base - loclist_header_size));
16919
16920 /* Verify the loclist index is valid. */
16921 if (loclist_index >= header.offset_entry_count)
16922 error (_("DW_FORM_loclistx pointing outside of "
16923 ".debug_loclists offset array [in module %s]"),
16924 objfile_name (objfile));
16925
16926 /* Validate that reading won't go beyond the end of the section. */
16927 if (start_offset + cu->header.offset_size > section->size)
16928 error (_("Reading DW_FORM_loclistx index beyond end of"
16929 ".debug_loclists section [in module %s]"),
16930 objfile_name (objfile));
16931
16932 const gdb_byte *info_ptr = section->buffer + start_offset;
16933
16934 if (cu->header.offset_size == 4)
16935 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
16936 else
16937 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
16938 }
16939
16940 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
16941 array of offsets in the .debug_rnglists section. */
16942
16943 static sect_offset
16944 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
16945 dwarf_tag tag)
16946 {
16947 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
16948 struct objfile *objfile = dwarf2_per_objfile->objfile;
16949 bfd *abfd = objfile->obfd.get ();
16950 ULONGEST rnglist_header_size =
16951 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
16952 : RNGLIST_HEADER_SIZE64);
16953
16954 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
16955 .debug_rnglists.dwo section. The rnglists base given in the skeleton
16956 doesn't apply. */
16957 ULONGEST rnglist_base =
16958 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
16959
16960 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
16961 ULONGEST start_offset =
16962 rnglist_base + rnglist_index * cu->header.offset_size;
16963
16964 /* Get rnglists section. */
16965 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
16966
16967 /* Read the rnglists section content. */
16968 section->read (objfile);
16969 if (section->buffer == nullptr)
16970 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
16971 "[in module %s]"),
16972 objfile_name (objfile));
16973
16974 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
16975 so if rnglist_base is smaller than the header size, we have a problem. */
16976 if (rnglist_base < rnglist_header_size)
16977 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
16978 objfile_name (objfile));
16979
16980 /* Read the header of the rnglists contribution. */
16981 struct loclists_rnglists_header header;
16982 read_loclists_rnglists_header (&header, section,
16983 (sect_offset) (rnglist_base - rnglist_header_size));
16984
16985 /* Verify the rnglist index is valid. */
16986 if (rnglist_index >= header.offset_entry_count)
16987 error (_("DW_FORM_rnglistx index pointing outside of "
16988 ".debug_rnglists offset array [in module %s]"),
16989 objfile_name (objfile));
16990
16991 /* Validate that reading won't go beyond the end of the section. */
16992 if (start_offset + cu->header.offset_size > section->size)
16993 error (_("Reading DW_FORM_rnglistx index beyond end of"
16994 ".debug_rnglists section [in module %s]"),
16995 objfile_name (objfile));
16996
16997 const gdb_byte *info_ptr = section->buffer + start_offset;
16998
16999 if (cu->header.offset_size == 4)
17000 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
17001 else
17002 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
17003 }
17004
17005 /* Process the attributes that had to be skipped in the first round. These
17006 attributes are the ones that need str_offsets_base or addr_base attributes.
17007 They could not have been processed in the first round, because at the time
17008 the values of str_offsets_base or addr_base may not have been known. */
17009 static void
17010 read_attribute_reprocess (const struct die_reader_specs *reader,
17011 struct attribute *attr, dwarf_tag tag)
17012 {
17013 struct dwarf2_cu *cu = reader->cu;
17014 switch (attr->form)
17015 {
17016 case DW_FORM_addrx:
17017 case DW_FORM_GNU_addr_index:
17018 attr->set_address (read_addr_index (cu,
17019 attr->as_unsigned_reprocess ()));
17020 break;
17021 case DW_FORM_loclistx:
17022 {
17023 sect_offset loclists_sect_off
17024 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
17025
17026 attr->set_unsigned (to_underlying (loclists_sect_off));
17027 }
17028 break;
17029 case DW_FORM_rnglistx:
17030 {
17031 sect_offset rnglists_sect_off
17032 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
17033
17034 attr->set_unsigned (to_underlying (rnglists_sect_off));
17035 }
17036 break;
17037 case DW_FORM_strx:
17038 case DW_FORM_strx1:
17039 case DW_FORM_strx2:
17040 case DW_FORM_strx3:
17041 case DW_FORM_strx4:
17042 case DW_FORM_GNU_str_index:
17043 {
17044 unsigned int str_index = attr->as_unsigned_reprocess ();
17045 gdb_assert (!attr->canonical_string_p ());
17046 if (reader->dwo_file != NULL)
17047 attr->set_string_noncanonical (read_dwo_str_index (reader,
17048 str_index));
17049 else
17050 attr->set_string_noncanonical (read_stub_str_index (cu,
17051 str_index));
17052 break;
17053 }
17054 default:
17055 gdb_assert_not_reached ("Unexpected DWARF form.");
17056 }
17057 }
17058
17059 /* Read an attribute value described by an attribute form. */
17060
17061 static const gdb_byte *
17062 read_attribute_value (const struct die_reader_specs *reader,
17063 struct attribute *attr, unsigned form,
17064 LONGEST implicit_const, const gdb_byte *info_ptr)
17065 {
17066 struct dwarf2_cu *cu = reader->cu;
17067 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17068 struct objfile *objfile = per_objfile->objfile;
17069 bfd *abfd = reader->abfd;
17070 struct comp_unit_head *cu_header = &cu->header;
17071 unsigned int bytes_read;
17072 struct dwarf_block *blk;
17073
17074 attr->form = (enum dwarf_form) form;
17075 switch (form)
17076 {
17077 case DW_FORM_ref_addr:
17078 if (cu_header->version == 2)
17079 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
17080 &bytes_read));
17081 else
17082 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17083 &bytes_read));
17084 info_ptr += bytes_read;
17085 break;
17086 case DW_FORM_GNU_ref_alt:
17087 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17088 &bytes_read));
17089 info_ptr += bytes_read;
17090 break;
17091 case DW_FORM_addr:
17092 {
17093 struct gdbarch *gdbarch = objfile->arch ();
17094 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
17095 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
17096 attr->set_address (addr);
17097 info_ptr += bytes_read;
17098 }
17099 break;
17100 case DW_FORM_block2:
17101 blk = dwarf_alloc_block (cu);
17102 blk->size = read_2_bytes (abfd, info_ptr);
17103 info_ptr += 2;
17104 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17105 info_ptr += blk->size;
17106 attr->set_block (blk);
17107 break;
17108 case DW_FORM_block4:
17109 blk = dwarf_alloc_block (cu);
17110 blk->size = read_4_bytes (abfd, info_ptr);
17111 info_ptr += 4;
17112 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17113 info_ptr += blk->size;
17114 attr->set_block (blk);
17115 break;
17116 case DW_FORM_data2:
17117 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
17118 info_ptr += 2;
17119 break;
17120 case DW_FORM_data4:
17121 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
17122 info_ptr += 4;
17123 break;
17124 case DW_FORM_data8:
17125 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
17126 info_ptr += 8;
17127 break;
17128 case DW_FORM_data16:
17129 blk = dwarf_alloc_block (cu);
17130 blk->size = 16;
17131 blk->data = read_n_bytes (abfd, info_ptr, 16);
17132 info_ptr += 16;
17133 attr->set_block (blk);
17134 break;
17135 case DW_FORM_sec_offset:
17136 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17137 &bytes_read));
17138 info_ptr += bytes_read;
17139 break;
17140 case DW_FORM_loclistx:
17141 {
17142 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17143 &bytes_read));
17144 info_ptr += bytes_read;
17145 }
17146 break;
17147 case DW_FORM_string:
17148 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
17149 &bytes_read));
17150 info_ptr += bytes_read;
17151 break;
17152 case DW_FORM_strp:
17153 if (!cu->per_cu->is_dwz)
17154 {
17155 attr->set_string_noncanonical
17156 (read_indirect_string (per_objfile,
17157 abfd, info_ptr, cu_header,
17158 &bytes_read));
17159 info_ptr += bytes_read;
17160 break;
17161 }
17162 /* FALLTHROUGH */
17163 case DW_FORM_line_strp:
17164 if (!cu->per_cu->is_dwz)
17165 {
17166 attr->set_string_noncanonical
17167 (per_objfile->read_line_string (info_ptr, cu_header,
17168 &bytes_read));
17169 info_ptr += bytes_read;
17170 break;
17171 }
17172 /* FALLTHROUGH */
17173 case DW_FORM_GNU_strp_alt:
17174 {
17175 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17176 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
17177 &bytes_read);
17178
17179 attr->set_string_noncanonical
17180 (dwz->read_string (objfile, str_offset));
17181 info_ptr += bytes_read;
17182 }
17183 break;
17184 case DW_FORM_exprloc:
17185 case DW_FORM_block:
17186 blk = dwarf_alloc_block (cu);
17187 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17188 info_ptr += bytes_read;
17189 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17190 info_ptr += blk->size;
17191 attr->set_block (blk);
17192 break;
17193 case DW_FORM_block1:
17194 blk = dwarf_alloc_block (cu);
17195 blk->size = read_1_byte (abfd, info_ptr);
17196 info_ptr += 1;
17197 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17198 info_ptr += blk->size;
17199 attr->set_block (blk);
17200 break;
17201 case DW_FORM_data1:
17202 case DW_FORM_flag:
17203 attr->set_unsigned (read_1_byte (abfd, info_ptr));
17204 info_ptr += 1;
17205 break;
17206 case DW_FORM_flag_present:
17207 attr->set_unsigned (1);
17208 break;
17209 case DW_FORM_sdata:
17210 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
17211 info_ptr += bytes_read;
17212 break;
17213 case DW_FORM_rnglistx:
17214 {
17215 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17216 &bytes_read));
17217 info_ptr += bytes_read;
17218 }
17219 break;
17220 case DW_FORM_udata:
17221 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17222 info_ptr += bytes_read;
17223 break;
17224 case DW_FORM_ref1:
17225 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17226 + read_1_byte (abfd, info_ptr)));
17227 info_ptr += 1;
17228 break;
17229 case DW_FORM_ref2:
17230 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17231 + read_2_bytes (abfd, info_ptr)));
17232 info_ptr += 2;
17233 break;
17234 case DW_FORM_ref4:
17235 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17236 + read_4_bytes (abfd, info_ptr)));
17237 info_ptr += 4;
17238 break;
17239 case DW_FORM_ref8:
17240 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17241 + read_8_bytes (abfd, info_ptr)));
17242 info_ptr += 8;
17243 break;
17244 case DW_FORM_ref_sig8:
17245 attr->set_signature (read_8_bytes (abfd, info_ptr));
17246 info_ptr += 8;
17247 break;
17248 case DW_FORM_ref_udata:
17249 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17250 + read_unsigned_leb128 (abfd, info_ptr,
17251 &bytes_read)));
17252 info_ptr += bytes_read;
17253 break;
17254 case DW_FORM_indirect:
17255 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17256 info_ptr += bytes_read;
17257 if (form == DW_FORM_implicit_const)
17258 {
17259 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17260 info_ptr += bytes_read;
17261 }
17262 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17263 info_ptr);
17264 break;
17265 case DW_FORM_implicit_const:
17266 attr->set_signed (implicit_const);
17267 break;
17268 case DW_FORM_addrx:
17269 case DW_FORM_GNU_addr_index:
17270 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17271 &bytes_read));
17272 info_ptr += bytes_read;
17273 break;
17274 case DW_FORM_strx:
17275 case DW_FORM_strx1:
17276 case DW_FORM_strx2:
17277 case DW_FORM_strx3:
17278 case DW_FORM_strx4:
17279 case DW_FORM_GNU_str_index:
17280 {
17281 ULONGEST str_index;
17282 if (form == DW_FORM_strx1)
17283 {
17284 str_index = read_1_byte (abfd, info_ptr);
17285 info_ptr += 1;
17286 }
17287 else if (form == DW_FORM_strx2)
17288 {
17289 str_index = read_2_bytes (abfd, info_ptr);
17290 info_ptr += 2;
17291 }
17292 else if (form == DW_FORM_strx3)
17293 {
17294 str_index = read_3_bytes (abfd, info_ptr);
17295 info_ptr += 3;
17296 }
17297 else if (form == DW_FORM_strx4)
17298 {
17299 str_index = read_4_bytes (abfd, info_ptr);
17300 info_ptr += 4;
17301 }
17302 else
17303 {
17304 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17305 info_ptr += bytes_read;
17306 }
17307 attr->set_unsigned_reprocess (str_index);
17308 }
17309 break;
17310 default:
17311 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17312 dwarf_form_name (form),
17313 bfd_get_filename (abfd));
17314 }
17315
17316 /* Super hack. */
17317 if (cu->per_cu->is_dwz && attr->form_is_ref ())
17318 attr->form = DW_FORM_GNU_ref_alt;
17319
17320 /* We have seen instances where the compiler tried to emit a byte
17321 size attribute of -1 which ended up being encoded as an unsigned
17322 0xffffffff. Although 0xffffffff is technically a valid size value,
17323 an object of this size seems pretty unlikely so we can relatively
17324 safely treat these cases as if the size attribute was invalid and
17325 treat them as zero by default. */
17326 if (attr->name == DW_AT_byte_size
17327 && form == DW_FORM_data4
17328 && attr->as_unsigned () >= 0xffffffff)
17329 {
17330 complaint
17331 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17332 hex_string (attr->as_unsigned ()));
17333 attr->set_unsigned (0);
17334 }
17335
17336 return info_ptr;
17337 }
17338
17339 /* Read an attribute described by an abbreviated attribute. */
17340
17341 static const gdb_byte *
17342 read_attribute (const struct die_reader_specs *reader,
17343 struct attribute *attr, const struct attr_abbrev *abbrev,
17344 const gdb_byte *info_ptr)
17345 {
17346 attr->name = abbrev->name;
17347 attr->string_is_canonical = 0;
17348 attr->requires_reprocessing = 0;
17349 return read_attribute_value (reader, attr, abbrev->form,
17350 abbrev->implicit_const, info_ptr);
17351 }
17352
17353 /* See read.h. */
17354
17355 const char *
17356 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
17357 LONGEST str_offset)
17358 {
17359 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
17360 str_offset, "DW_FORM_strp");
17361 }
17362
17363 /* Return pointer to string at .debug_str offset as read from BUF.
17364 BUF is assumed to be in a compilation unit described by CU_HEADER.
17365 Return *BYTES_READ_PTR count of bytes read from BUF. */
17366
17367 static const char *
17368 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
17369 const gdb_byte *buf,
17370 const struct comp_unit_head *cu_header,
17371 unsigned int *bytes_read_ptr)
17372 {
17373 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17374
17375 return read_indirect_string_at_offset (per_objfile, str_offset);
17376 }
17377
17378 /* See read.h. */
17379
17380 const char *
17381 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17382 unsigned int offset_size)
17383 {
17384 bfd *abfd = objfile->obfd.get ();
17385 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
17386
17387 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17388 }
17389
17390 /* See read.h. */
17391
17392 const char *
17393 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17394 const struct comp_unit_head *cu_header,
17395 unsigned int *bytes_read_ptr)
17396 {
17397 bfd *abfd = objfile->obfd.get ();
17398 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17399
17400 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17401 }
17402
17403 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17404 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
17405 ADDR_SIZE is the size of addresses from the CU header. */
17406
17407 static CORE_ADDR
17408 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
17409 gdb::optional<ULONGEST> addr_base, int addr_size)
17410 {
17411 struct objfile *objfile = per_objfile->objfile;
17412 bfd *abfd = objfile->obfd.get ();
17413 const gdb_byte *info_ptr;
17414 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
17415
17416 per_objfile->per_bfd->addr.read (objfile);
17417 if (per_objfile->per_bfd->addr.buffer == NULL)
17418 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17419 objfile_name (objfile));
17420 if (addr_base_or_zero + addr_index * addr_size
17421 >= per_objfile->per_bfd->addr.size)
17422 error (_("DW_FORM_addr_index pointing outside of "
17423 ".debug_addr section [in module %s]"),
17424 objfile_name (objfile));
17425 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
17426 + addr_index * addr_size);
17427 if (addr_size == 4)
17428 return bfd_get_32 (abfd, info_ptr);
17429 else
17430 return bfd_get_64 (abfd, info_ptr);
17431 }
17432
17433 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17434
17435 static CORE_ADDR
17436 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17437 {
17438 return read_addr_index_1 (cu->per_objfile, addr_index,
17439 cu->addr_base, cu->header.addr_size);
17440 }
17441
17442 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17443
17444 static CORE_ADDR
17445 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17446 unsigned int *bytes_read)
17447 {
17448 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
17449 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17450
17451 return read_addr_index (cu, addr_index);
17452 }
17453
17454 /* See read.h. */
17455
17456 CORE_ADDR
17457 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
17458 dwarf2_per_objfile *per_objfile,
17459 unsigned int addr_index)
17460 {
17461 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
17462 gdb::optional<ULONGEST> addr_base;
17463 int addr_size;
17464
17465 /* We need addr_base and addr_size.
17466 If we don't have PER_CU->cu, we have to get it.
17467 Nasty, but the alternative is storing the needed info in PER_CU,
17468 which at this point doesn't seem justified: it's not clear how frequently
17469 it would get used and it would increase the size of every PER_CU.
17470 Entry points like dwarf2_per_cu_addr_size do a similar thing
17471 so we're not in uncharted territory here.
17472 Alas we need to be a bit more complicated as addr_base is contained
17473 in the DIE.
17474
17475 We don't need to read the entire CU(/TU).
17476 We just need the header and top level die.
17477
17478 IWBN to use the aging mechanism to let us lazily later discard the CU.
17479 For now we skip this optimization. */
17480
17481 if (cu != NULL)
17482 {
17483 addr_base = cu->addr_base;
17484 addr_size = cu->header.addr_size;
17485 }
17486 else
17487 {
17488 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
17489 addr_base = reader.cu->addr_base;
17490 addr_size = reader.cu->header.addr_size;
17491 }
17492
17493 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
17494 }
17495
17496 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
17497 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
17498 DWO file. */
17499
17500 static const char *
17501 read_str_index (struct dwarf2_cu *cu,
17502 struct dwarf2_section_info *str_section,
17503 struct dwarf2_section_info *str_offsets_section,
17504 ULONGEST str_offsets_base, ULONGEST str_index,
17505 unsigned offset_size)
17506 {
17507 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17508 struct objfile *objfile = per_objfile->objfile;
17509 const char *objf_name = objfile_name (objfile);
17510 bfd *abfd = objfile->obfd.get ();
17511 const gdb_byte *info_ptr;
17512 ULONGEST str_offset;
17513 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
17514
17515 str_section->read (objfile);
17516 str_offsets_section->read (objfile);
17517 if (str_section->buffer == NULL)
17518 error (_("%s used without %s section"
17519 " in CU at offset %s [in module %s]"),
17520 form_name, str_section->get_name (),
17521 sect_offset_str (cu->header.sect_off), objf_name);
17522 if (str_offsets_section->buffer == NULL)
17523 error (_("%s used without %s section"
17524 " in CU at offset %s [in module %s]"),
17525 form_name, str_section->get_name (),
17526 sect_offset_str (cu->header.sect_off), objf_name);
17527 info_ptr = (str_offsets_section->buffer
17528 + str_offsets_base
17529 + str_index * offset_size);
17530 if (offset_size == 4)
17531 str_offset = bfd_get_32 (abfd, info_ptr);
17532 else
17533 str_offset = bfd_get_64 (abfd, info_ptr);
17534 if (str_offset >= str_section->size)
17535 error (_("Offset from %s pointing outside of"
17536 " .debug_str.dwo section in CU at offset %s [in module %s]"),
17537 form_name, sect_offset_str (cu->header.sect_off), objf_name);
17538 return (const char *) (str_section->buffer + str_offset);
17539 }
17540
17541 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
17542
17543 static const char *
17544 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17545 {
17546 unsigned offset_size;
17547 ULONGEST str_offsets_base;
17548 if (reader->cu->header.version >= 5)
17549 {
17550 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
17551 so assume the .debug_str_offsets section is DWARF5 as well, and
17552 parse the header. FIXME: Parse the header only once. */
17553 unsigned int bytes_read = 0;
17554 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
17555 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
17556
17557 /* Header: Initial length. */
17558 read_initial_length (abfd, p + bytes_read, &bytes_read);
17559
17560 /* Determine offset_size based on the .debug_str_offsets header. */
17561 const bool dwarf5_is_dwarf64 = bytes_read != 4;
17562 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
17563
17564 /* Header: Version. */
17565 unsigned version = read_2_bytes (abfd, p + bytes_read);
17566 bytes_read += 2;
17567
17568 if (version <= 4)
17569 {
17570 /* We'd like one warning here about ignoring the section, but
17571 because we parse the header more than once (see FIXME above)
17572 we'd have many warnings, so use a complaint instead, which at
17573 least has a limit. */
17574 complaint (_("Section .debug_str_offsets in %s has unsupported"
17575 " version %d, use empty string."),
17576 reader->dwo_file->dwo_name, version);
17577 return "";
17578 }
17579
17580 /* Header: Padding. */
17581 bytes_read += 2;
17582
17583 str_offsets_base = bytes_read;
17584 }
17585 else
17586 {
17587 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
17588 section, assume the .debug_str_offsets section is pre-DWARF5 as
17589 well, which doesn't have a header. */
17590 str_offsets_base = 0;
17591
17592 /* Determine offset_size based on the .debug_info header. */
17593 offset_size = reader->cu->header.offset_size;
17594 }
17595
17596 return read_str_index (reader->cu,
17597 &reader->dwo_file->sections.str,
17598 &reader->dwo_file->sections.str_offsets,
17599 str_offsets_base, str_index, offset_size);
17600 }
17601
17602 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
17603
17604 static const char *
17605 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
17606 {
17607 struct objfile *objfile = cu->per_objfile->objfile;
17608 const char *objf_name = objfile_name (objfile);
17609 static const char form_name[] = "DW_FORM_GNU_str_index";
17610 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
17611
17612 if (!cu->str_offsets_base.has_value ())
17613 error (_("%s used in Fission stub without %s"
17614 " in CU at offset 0x%lx [in module %s]"),
17615 form_name, str_offsets_attr_name,
17616 (long) cu->header.offset_size, objf_name);
17617
17618 return read_str_index (cu,
17619 &cu->per_objfile->per_bfd->str,
17620 &cu->per_objfile->per_bfd->str_offsets,
17621 *cu->str_offsets_base, str_index,
17622 cu->header.offset_size);
17623 }
17624
17625 /* Return the length of an LEB128 number in BUF. */
17626
17627 static int
17628 leb128_size (const gdb_byte *buf)
17629 {
17630 const gdb_byte *begin = buf;
17631 gdb_byte byte;
17632
17633 while (1)
17634 {
17635 byte = *buf++;
17636 if ((byte & 128) == 0)
17637 return buf - begin;
17638 }
17639 }
17640
17641 static enum language
17642 dwarf_lang_to_enum_language (unsigned int lang)
17643 {
17644 enum language language;
17645
17646 switch (lang)
17647 {
17648 case DW_LANG_C89:
17649 case DW_LANG_C99:
17650 case DW_LANG_C11:
17651 case DW_LANG_C:
17652 case DW_LANG_UPC:
17653 language = language_c;
17654 break;
17655 case DW_LANG_Java:
17656 case DW_LANG_C_plus_plus:
17657 case DW_LANG_C_plus_plus_11:
17658 case DW_LANG_C_plus_plus_14:
17659 language = language_cplus;
17660 break;
17661 case DW_LANG_D:
17662 language = language_d;
17663 break;
17664 case DW_LANG_Fortran77:
17665 case DW_LANG_Fortran90:
17666 case DW_LANG_Fortran95:
17667 case DW_LANG_Fortran03:
17668 case DW_LANG_Fortran08:
17669 language = language_fortran;
17670 break;
17671 case DW_LANG_Go:
17672 language = language_go;
17673 break;
17674 case DW_LANG_Mips_Assembler:
17675 language = language_asm;
17676 break;
17677 case DW_LANG_Ada83:
17678 case DW_LANG_Ada95:
17679 language = language_ada;
17680 break;
17681 case DW_LANG_Modula2:
17682 language = language_m2;
17683 break;
17684 case DW_LANG_Pascal83:
17685 language = language_pascal;
17686 break;
17687 case DW_LANG_ObjC:
17688 language = language_objc;
17689 break;
17690 case DW_LANG_Rust:
17691 case DW_LANG_Rust_old:
17692 language = language_rust;
17693 break;
17694 case DW_LANG_OpenCL:
17695 language = language_opencl;
17696 break;
17697 case DW_LANG_Cobol74:
17698 case DW_LANG_Cobol85:
17699 default:
17700 language = language_minimal;
17701 break;
17702 }
17703
17704 return language;
17705 }
17706
17707 /* Return the named attribute or NULL if not there. */
17708
17709 static struct attribute *
17710 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17711 {
17712 for (;;)
17713 {
17714 unsigned int i;
17715 struct attribute *spec = NULL;
17716
17717 for (i = 0; i < die->num_attrs; ++i)
17718 {
17719 if (die->attrs[i].name == name)
17720 return &die->attrs[i];
17721 if (die->attrs[i].name == DW_AT_specification
17722 || die->attrs[i].name == DW_AT_abstract_origin)
17723 spec = &die->attrs[i];
17724 }
17725
17726 if (!spec)
17727 break;
17728
17729 die = follow_die_ref (die, spec, &cu);
17730 }
17731
17732 return NULL;
17733 }
17734
17735 /* Return the string associated with a string-typed attribute, or NULL if it
17736 is either not found or is of an incorrect type. */
17737
17738 static const char *
17739 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17740 {
17741 struct attribute *attr;
17742 const char *str = NULL;
17743
17744 attr = dwarf2_attr (die, name, cu);
17745
17746 if (attr != NULL)
17747 {
17748 str = attr->as_string ();
17749 if (str == nullptr)
17750 complaint (_("string type expected for attribute %s for "
17751 "DIE at %s in module %s"),
17752 dwarf_attr_name (name), sect_offset_str (die->sect_off),
17753 objfile_name (cu->per_objfile->objfile));
17754 }
17755
17756 return str;
17757 }
17758
17759 /* Return the dwo name or NULL if not present. If present, it is in either
17760 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
17761 static const char *
17762 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
17763 {
17764 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
17765 if (dwo_name == nullptr)
17766 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
17767 return dwo_name;
17768 }
17769
17770 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17771 and holds a non-zero value. This function should only be used for
17772 DW_FORM_flag or DW_FORM_flag_present attributes. */
17773
17774 static int
17775 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17776 {
17777 struct attribute *attr = dwarf2_attr (die, name, cu);
17778
17779 return attr != nullptr && attr->as_boolean ();
17780 }
17781
17782 static int
17783 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17784 {
17785 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17786 which value is non-zero. However, we have to be careful with
17787 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17788 (via dwarf2_flag_true_p) follows this attribute. So we may
17789 end up accidently finding a declaration attribute that belongs
17790 to a different DIE referenced by the specification attribute,
17791 even though the given DIE does not have a declaration attribute. */
17792 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17793 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17794 }
17795
17796 /* Return the die giving the specification for DIE, if there is
17797 one. *SPEC_CU is the CU containing DIE on input, and the CU
17798 containing the return value on output. If there is no
17799 specification, but there is an abstract origin, that is
17800 returned. */
17801
17802 static struct die_info *
17803 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17804 {
17805 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17806 *spec_cu);
17807
17808 if (spec_attr == NULL)
17809 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17810
17811 if (spec_attr == NULL)
17812 return NULL;
17813 else
17814 return follow_die_ref (die, spec_attr, spec_cu);
17815 }
17816
17817 /* A convenience function to find the proper .debug_line section for a CU. */
17818
17819 static struct dwarf2_section_info *
17820 get_debug_line_section (struct dwarf2_cu *cu)
17821 {
17822 struct dwarf2_section_info *section;
17823 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17824
17825 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17826 DWO file. */
17827 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17828 section = &cu->dwo_unit->dwo_file->sections.line;
17829 else if (cu->per_cu->is_dwz)
17830 {
17831 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17832
17833 section = &dwz->line;
17834 }
17835 else
17836 section = &per_objfile->per_bfd->line;
17837
17838 return section;
17839 }
17840
17841 /* Read the statement program header starting at OFFSET in
17842 .debug_line, or .debug_line.dwo. Return a pointer
17843 to a struct line_header, allocated using xmalloc.
17844 Returns NULL if there is a problem reading the header, e.g., if it
17845 has a version we don't understand.
17846
17847 NOTE: the strings in the include directory and file name tables of
17848 the returned object point into the dwarf line section buffer,
17849 and must not be freed. */
17850
17851 static line_header_up
17852 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
17853 const char *comp_dir)
17854 {
17855 struct dwarf2_section_info *section;
17856 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17857
17858 section = get_debug_line_section (cu);
17859 section->read (per_objfile->objfile);
17860 if (section->buffer == NULL)
17861 {
17862 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17863 complaint (_("missing .debug_line.dwo section"));
17864 else
17865 complaint (_("missing .debug_line section"));
17866 return 0;
17867 }
17868
17869 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
17870 per_objfile, section, &cu->header,
17871 comp_dir);
17872 }
17873
17874 /* Subroutine of dwarf_decode_lines to simplify it.
17875 Return the file name for the given file_entry.
17876 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
17877 If space for the result is malloc'd, *NAME_HOLDER will be set.
17878 Returns NULL if FILE_INDEX should be ignored, i.e., it is
17879 equivalent to CU_INFO. */
17880
17881 static const char *
17882 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
17883 const file_and_directory &cu_info,
17884 std::string &name_holder)
17885 {
17886 const char *include_name = fe.name;
17887 const char *include_name_to_compare = include_name;
17888
17889 const char *dir_name = fe.include_dir (lh);
17890
17891 std::string hold_compare;
17892 if (!IS_ABSOLUTE_PATH (include_name)
17893 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
17894 {
17895 /* Avoid creating a duplicate name for CU_INFO.
17896 We do this by comparing INCLUDE_NAME and CU_INFO.
17897 Before we do the comparison, however, we need to account
17898 for DIR_NAME and COMP_DIR.
17899 First prepend dir_name (if non-NULL). If we still don't
17900 have an absolute path prepend comp_dir (if non-NULL).
17901 However, the directory we record in the include-file's
17902 psymtab does not contain COMP_DIR (to match the
17903 corresponding symtab(s)).
17904
17905 Example:
17906
17907 bash$ cd /tmp
17908 bash$ gcc -g ./hello.c
17909 include_name = "hello.c"
17910 dir_name = "."
17911 DW_AT_comp_dir = comp_dir = "/tmp"
17912 DW_AT_name = "./hello.c"
17913
17914 */
17915
17916 if (dir_name != NULL)
17917 {
17918 name_holder = path_join (dir_name, include_name);
17919 include_name = name_holder.c_str ();
17920 include_name_to_compare = include_name;
17921 }
17922 if (!IS_ABSOLUTE_PATH (include_name)
17923 && cu_info.get_comp_dir () != nullptr)
17924 {
17925 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
17926 include_name_to_compare = hold_compare.c_str ();
17927 }
17928 }
17929
17930 std::string copied_name;
17931 const char *cu_filename = cu_info.get_name ();
17932 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
17933 {
17934 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
17935 cu_filename = copied_name.c_str ();
17936 }
17937
17938 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
17939 return nullptr;
17940 return include_name;
17941 }
17942
17943 /* State machine to track the state of the line number program. */
17944
17945 class lnp_state_machine
17946 {
17947 public:
17948 /* Initialize a machine state for the start of a line number
17949 program. */
17950 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
17951
17952 file_entry *current_file ()
17953 {
17954 /* lh->file_names is 0-based, but the file name numbers in the
17955 statement program are 1-based. */
17956 return m_line_header->file_name_at (m_file);
17957 }
17958
17959 /* Record the line in the state machine. END_SEQUENCE is true if
17960 we're processing the end of a sequence. */
17961 void record_line (bool end_sequence);
17962
17963 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
17964 nop-out rest of the lines in this sequence. */
17965 void check_line_address (struct dwarf2_cu *cu,
17966 const gdb_byte *line_ptr,
17967 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
17968
17969 void handle_set_discriminator (unsigned int discriminator)
17970 {
17971 m_discriminator = discriminator;
17972 m_line_has_non_zero_discriminator |= discriminator != 0;
17973 }
17974
17975 /* Handle DW_LNE_set_address. */
17976 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
17977 {
17978 m_op_index = 0;
17979 address += baseaddr;
17980 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
17981 }
17982
17983 /* Handle DW_LNS_advance_pc. */
17984 void handle_advance_pc (CORE_ADDR adjust);
17985
17986 /* Handle a special opcode. */
17987 void handle_special_opcode (unsigned char op_code);
17988
17989 /* Handle DW_LNS_advance_line. */
17990 void handle_advance_line (int line_delta)
17991 {
17992 advance_line (line_delta);
17993 }
17994
17995 /* Handle DW_LNS_set_file. */
17996 void handle_set_file (file_name_index file);
17997
17998 /* Handle DW_LNS_negate_stmt. */
17999 void handle_negate_stmt ()
18000 {
18001 m_flags ^= LEF_IS_STMT;
18002 }
18003
18004 /* Handle DW_LNS_const_add_pc. */
18005 void handle_const_add_pc ();
18006
18007 /* Handle DW_LNS_fixed_advance_pc. */
18008 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18009 {
18010 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18011 m_op_index = 0;
18012 }
18013
18014 /* Handle DW_LNS_copy. */
18015 void handle_copy ()
18016 {
18017 record_line (false);
18018 m_discriminator = 0;
18019 m_flags &= ~LEF_PROLOGUE_END;
18020 }
18021
18022 /* Handle DW_LNE_end_sequence. */
18023 void handle_end_sequence ()
18024 {
18025 m_currently_recording_lines = true;
18026 }
18027
18028 /* Handle DW_LNS_set_prologue_end. */
18029 void handle_set_prologue_end ()
18030 {
18031 m_flags |= LEF_PROLOGUE_END;
18032 }
18033
18034 private:
18035 /* Advance the line by LINE_DELTA. */
18036 void advance_line (int line_delta)
18037 {
18038 m_line += line_delta;
18039
18040 if (line_delta != 0)
18041 m_line_has_non_zero_discriminator = m_discriminator != 0;
18042 }
18043
18044 struct dwarf2_cu *m_cu;
18045
18046 gdbarch *m_gdbarch;
18047
18048 /* The line number header. */
18049 line_header *m_line_header;
18050
18051 /* These are part of the standard DWARF line number state machine,
18052 and initialized according to the DWARF spec. */
18053
18054 unsigned char m_op_index = 0;
18055 /* The line table index of the current file. */
18056 file_name_index m_file = 1;
18057 unsigned int m_line = 1;
18058
18059 /* These are initialized in the constructor. */
18060
18061 CORE_ADDR m_address;
18062 linetable_entry_flags m_flags;
18063 unsigned int m_discriminator = 0;
18064
18065 /* Additional bits of state we need to track. */
18066
18067 /* The last file that we called dwarf2_start_subfile for.
18068 This is only used for TLLs. */
18069 unsigned int m_last_file = 0;
18070 /* The last file a line number was recorded for. */
18071 struct subfile *m_last_subfile = NULL;
18072
18073 /* The address of the last line entry. */
18074 CORE_ADDR m_last_address;
18075
18076 /* Set to true when a previous line at the same address (using
18077 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
18078 when a line entry at a new address (m_address different to
18079 m_last_address) is processed. */
18080 bool m_stmt_at_address = false;
18081
18082 /* When true, record the lines we decode. */
18083 bool m_currently_recording_lines = true;
18084
18085 /* The last line number that was recorded, used to coalesce
18086 consecutive entries for the same line. This can happen, for
18087 example, when discriminators are present. PR 17276. */
18088 unsigned int m_last_line = 0;
18089 bool m_line_has_non_zero_discriminator = false;
18090 };
18091
18092 void
18093 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18094 {
18095 CORE_ADDR addr_adj = (((m_op_index + adjust)
18096 / m_line_header->maximum_ops_per_instruction)
18097 * m_line_header->minimum_instruction_length);
18098 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18099 m_op_index = ((m_op_index + adjust)
18100 % m_line_header->maximum_ops_per_instruction);
18101 }
18102
18103 void
18104 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18105 {
18106 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18107 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
18108 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
18109 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
18110 / m_line_header->maximum_ops_per_instruction)
18111 * m_line_header->minimum_instruction_length);
18112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18113 m_op_index = ((m_op_index + adj_opcode_d)
18114 % m_line_header->maximum_ops_per_instruction);
18115
18116 int line_delta = m_line_header->line_base + adj_opcode_r;
18117 advance_line (line_delta);
18118 record_line (false);
18119 m_discriminator = 0;
18120 m_flags &= ~LEF_PROLOGUE_END;
18121 }
18122
18123 void
18124 lnp_state_machine::handle_set_file (file_name_index file)
18125 {
18126 m_file = file;
18127
18128 const file_entry *fe = current_file ();
18129 if (fe == NULL)
18130 dwarf2_debug_line_missing_file_complaint ();
18131 else
18132 {
18133 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18134 m_line_has_non_zero_discriminator = m_discriminator != 0;
18135 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
18136 }
18137 }
18138
18139 void
18140 lnp_state_machine::handle_const_add_pc ()
18141 {
18142 CORE_ADDR adjust
18143 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18144
18145 CORE_ADDR addr_adj
18146 = (((m_op_index + adjust)
18147 / m_line_header->maximum_ops_per_instruction)
18148 * m_line_header->minimum_instruction_length);
18149
18150 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18151 m_op_index = ((m_op_index + adjust)
18152 % m_line_header->maximum_ops_per_instruction);
18153 }
18154
18155 /* Return non-zero if we should add LINE to the line number table.
18156 LINE is the line to add, LAST_LINE is the last line that was added,
18157 LAST_SUBFILE is the subfile for LAST_LINE.
18158 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18159 had a non-zero discriminator.
18160
18161 We have to be careful in the presence of discriminators.
18162 E.g., for this line:
18163
18164 for (i = 0; i < 100000; i++);
18165
18166 clang can emit four line number entries for that one line,
18167 each with a different discriminator.
18168 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18169
18170 However, we want gdb to coalesce all four entries into one.
18171 Otherwise the user could stepi into the middle of the line and
18172 gdb would get confused about whether the pc really was in the
18173 middle of the line.
18174
18175 Things are further complicated by the fact that two consecutive
18176 line number entries for the same line is a heuristic used by gcc
18177 to denote the end of the prologue. So we can't just discard duplicate
18178 entries, we have to be selective about it. The heuristic we use is
18179 that we only collapse consecutive entries for the same line if at least
18180 one of those entries has a non-zero discriminator. PR 17276.
18181
18182 Note: Addresses in the line number state machine can never go backwards
18183 within one sequence, thus this coalescing is ok. */
18184
18185 static int
18186 dwarf_record_line_p (struct dwarf2_cu *cu,
18187 unsigned int line, unsigned int last_line,
18188 int line_has_non_zero_discriminator,
18189 struct subfile *last_subfile)
18190 {
18191 if (cu->get_builder ()->get_current_subfile () != last_subfile)
18192 return 1;
18193 if (line != last_line)
18194 return 1;
18195 /* Same line for the same file that we've seen already.
18196 As a last check, for pr 17276, only record the line if the line
18197 has never had a non-zero discriminator. */
18198 if (!line_has_non_zero_discriminator)
18199 return 1;
18200 return 0;
18201 }
18202
18203 /* Use the CU's builder to record line number LINE beginning at
18204 address ADDRESS in the line table of subfile SUBFILE. */
18205
18206 static void
18207 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18208 unsigned int line, CORE_ADDR address,
18209 linetable_entry_flags flags,
18210 struct dwarf2_cu *cu)
18211 {
18212 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18213
18214 if (dwarf_line_debug)
18215 {
18216 gdb_printf (gdb_stdlog,
18217 "Recording line %u, file %s, address %s\n",
18218 line, lbasename (subfile->name.c_str ()),
18219 paddress (gdbarch, address));
18220 }
18221
18222 if (cu != nullptr)
18223 cu->get_builder ()->record_line (subfile, line, addr, flags);
18224 }
18225
18226 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18227 Mark the end of a set of line number records.
18228 The arguments are the same as for dwarf_record_line_1.
18229 If SUBFILE is NULL the request is ignored. */
18230
18231 static void
18232 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18233 CORE_ADDR address, struct dwarf2_cu *cu)
18234 {
18235 if (subfile == NULL)
18236 return;
18237
18238 if (dwarf_line_debug)
18239 {
18240 gdb_printf (gdb_stdlog,
18241 "Finishing current line, file %s, address %s\n",
18242 lbasename (subfile->name.c_str ()),
18243 paddress (gdbarch, address));
18244 }
18245
18246 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
18247 }
18248
18249 void
18250 lnp_state_machine::record_line (bool end_sequence)
18251 {
18252 if (dwarf_line_debug)
18253 {
18254 gdb_printf (gdb_stdlog,
18255 "Processing actual line %u: file %u,"
18256 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
18257 m_line, m_file,
18258 paddress (m_gdbarch, m_address),
18259 (m_flags & LEF_IS_STMT) != 0,
18260 (m_flags & LEF_PROLOGUE_END) != 0,
18261 m_discriminator,
18262 (end_sequence ? "\t(end sequence)" : ""));
18263 }
18264
18265 file_entry *fe = current_file ();
18266
18267 if (fe == NULL)
18268 dwarf2_debug_line_missing_file_complaint ();
18269 /* For now we ignore lines not starting on an instruction boundary.
18270 But not when processing end_sequence for compatibility with the
18271 previous version of the code. */
18272 else if (m_op_index == 0 || end_sequence)
18273 {
18274 /* When we switch files we insert an end maker in the first file,
18275 switch to the second file and add a new line entry. The
18276 problem is that the end marker inserted in the first file will
18277 discard any previous line entries at the same address. If the
18278 line entries in the first file are marked as is-stmt, while
18279 the new line in the second file is non-stmt, then this means
18280 the end marker will discard is-stmt lines so we can have a
18281 non-stmt line. This means that there are less addresses at
18282 which the user can insert a breakpoint.
18283
18284 To improve this we track the last address in m_last_address,
18285 and whether we have seen an is-stmt at this address. Then
18286 when switching files, if we have seen a stmt at the current
18287 address, and we are switching to create a non-stmt line, then
18288 discard the new line. */
18289 bool file_changed
18290 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
18291 bool ignore_this_line
18292 = ((file_changed && !end_sequence && m_last_address == m_address
18293 && ((m_flags & LEF_IS_STMT) == 0)
18294 && m_stmt_at_address)
18295 || (!end_sequence && m_line == 0));
18296
18297 if ((file_changed && !ignore_this_line) || end_sequence)
18298 {
18299 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
18300 m_currently_recording_lines ? m_cu : nullptr);
18301 }
18302
18303 if (!end_sequence && !ignore_this_line)
18304 {
18305 linetable_entry_flags lte_flags = m_flags;
18306 if (producer_is_codewarrior (m_cu))
18307 lte_flags |= LEF_IS_STMT;
18308
18309 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
18310 m_line_has_non_zero_discriminator,
18311 m_last_subfile))
18312 {
18313 buildsym_compunit *builder = m_cu->get_builder ();
18314 dwarf_record_line_1 (m_gdbarch,
18315 builder->get_current_subfile (),
18316 m_line, m_address, lte_flags,
18317 m_currently_recording_lines ? m_cu : nullptr);
18318 }
18319 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18320 m_last_line = m_line;
18321 }
18322 }
18323
18324 /* Track whether we have seen any IS_STMT true at m_address in case we
18325 have multiple line table entries all at m_address. */
18326 if (m_last_address != m_address)
18327 {
18328 m_stmt_at_address = false;
18329 m_last_address = m_address;
18330 }
18331 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
18332 }
18333
18334 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
18335 line_header *lh)
18336 : m_cu (cu),
18337 m_gdbarch (arch),
18338 m_line_header (lh),
18339 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
18340 if there was a line entry for it so that the backend has a
18341 chance to adjust it and also record it in case it needs it.
18342 This is currently used by MIPS code,
18343 cf. `mips_adjust_dwarf2_line'. */
18344 m_address (gdbarch_adjust_dwarf2_line (arch, 0, 0)),
18345 m_flags (lh->default_is_stmt ? LEF_IS_STMT : (linetable_entry_flags) 0),
18346 m_last_address (m_address)
18347 {
18348 }
18349
18350 void
18351 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18352 const gdb_byte *line_ptr,
18353 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
18354 {
18355 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
18356 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
18357 located at 0x0. In this case, additionally check that if
18358 ADDRESS < UNRELOCATED_LOWPC. */
18359
18360 if ((address == 0 && address < unrelocated_lowpc)
18361 || address == (CORE_ADDR) -1)
18362 {
18363 /* This line table is for a function which has been
18364 GCd by the linker. Ignore it. PR gdb/12528 */
18365
18366 struct objfile *objfile = cu->per_objfile->objfile;
18367 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18368
18369 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18370 line_offset, objfile_name (objfile));
18371 m_currently_recording_lines = false;
18372 /* Note: m_currently_recording_lines is left as false until we see
18373 DW_LNE_end_sequence. */
18374 }
18375 }
18376
18377 /* Subroutine of dwarf_decode_lines to simplify it.
18378 Process the line number information in LH. */
18379
18380 static void
18381 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18382 CORE_ADDR lowpc)
18383 {
18384 const gdb_byte *line_ptr, *extended_end;
18385 const gdb_byte *line_end;
18386 unsigned int bytes_read, extended_len;
18387 unsigned char op_code, extended_op;
18388 CORE_ADDR baseaddr;
18389 struct objfile *objfile = cu->per_objfile->objfile;
18390 bfd *abfd = objfile->obfd.get ();
18391 struct gdbarch *gdbarch = objfile->arch ();
18392
18393 baseaddr = objfile->text_section_offset ();
18394
18395 line_ptr = lh->statement_program_start;
18396 line_end = lh->statement_program_end;
18397
18398 /* Read the statement sequences until there's nothing left. */
18399 while (line_ptr < line_end)
18400 {
18401 /* The DWARF line number program state machine. Reset the state
18402 machine at the start of each sequence. */
18403 lnp_state_machine state_machine (cu, gdbarch, lh);
18404 bool end_sequence = false;
18405
18406 /* Start a subfile for the current file of the state
18407 machine. */
18408 const file_entry *fe = state_machine.current_file ();
18409
18410 if (fe != NULL)
18411 dwarf2_start_subfile (cu, *fe, *lh);
18412
18413 /* Decode the table. */
18414 while (line_ptr < line_end && !end_sequence)
18415 {
18416 op_code = read_1_byte (abfd, line_ptr);
18417 line_ptr += 1;
18418
18419 if (op_code >= lh->opcode_base)
18420 {
18421 /* Special opcode. */
18422 state_machine.handle_special_opcode (op_code);
18423 }
18424 else switch (op_code)
18425 {
18426 case DW_LNS_extended_op:
18427 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18428 &bytes_read);
18429 line_ptr += bytes_read;
18430 extended_end = line_ptr + extended_len;
18431 extended_op = read_1_byte (abfd, line_ptr);
18432 line_ptr += 1;
18433 if (DW_LNE_lo_user <= extended_op
18434 && extended_op <= DW_LNE_hi_user)
18435 {
18436 /* Vendor extension, ignore. */
18437 line_ptr = extended_end;
18438 break;
18439 }
18440 switch (extended_op)
18441 {
18442 case DW_LNE_end_sequence:
18443 state_machine.handle_end_sequence ();
18444 end_sequence = true;
18445 break;
18446 case DW_LNE_set_address:
18447 {
18448 CORE_ADDR address
18449 = cu->header.read_address (abfd, line_ptr, &bytes_read);
18450 line_ptr += bytes_read;
18451
18452 state_machine.check_line_address (cu, line_ptr,
18453 lowpc - baseaddr, address);
18454 state_machine.handle_set_address (baseaddr, address);
18455 }
18456 break;
18457 case DW_LNE_define_file:
18458 {
18459 const char *cur_file;
18460 unsigned int mod_time, length;
18461 dir_index dindex;
18462
18463 cur_file = read_direct_string (abfd, line_ptr,
18464 &bytes_read);
18465 line_ptr += bytes_read;
18466 dindex = (dir_index)
18467 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18468 line_ptr += bytes_read;
18469 mod_time =
18470 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18471 line_ptr += bytes_read;
18472 length =
18473 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18474 line_ptr += bytes_read;
18475 lh->add_file_name (cur_file, dindex, mod_time, length);
18476 }
18477 break;
18478 case DW_LNE_set_discriminator:
18479 {
18480 /* The discriminator is not interesting to the
18481 debugger; just ignore it. We still need to
18482 check its value though:
18483 if there are consecutive entries for the same
18484 (non-prologue) line we want to coalesce them.
18485 PR 17276. */
18486 unsigned int discr
18487 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18488 line_ptr += bytes_read;
18489
18490 state_machine.handle_set_discriminator (discr);
18491 }
18492 break;
18493 default:
18494 complaint (_("mangled .debug_line section"));
18495 return;
18496 }
18497 /* Make sure that we parsed the extended op correctly. If e.g.
18498 we expected a different address size than the producer used,
18499 we may have read the wrong number of bytes. */
18500 if (line_ptr != extended_end)
18501 {
18502 complaint (_("mangled .debug_line section"));
18503 return;
18504 }
18505 break;
18506 case DW_LNS_copy:
18507 state_machine.handle_copy ();
18508 break;
18509 case DW_LNS_advance_pc:
18510 {
18511 CORE_ADDR adjust
18512 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18513 line_ptr += bytes_read;
18514
18515 state_machine.handle_advance_pc (adjust);
18516 }
18517 break;
18518 case DW_LNS_advance_line:
18519 {
18520 int line_delta
18521 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18522 line_ptr += bytes_read;
18523
18524 state_machine.handle_advance_line (line_delta);
18525 }
18526 break;
18527 case DW_LNS_set_file:
18528 {
18529 file_name_index file
18530 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18531 &bytes_read);
18532 line_ptr += bytes_read;
18533
18534 state_machine.handle_set_file (file);
18535 }
18536 break;
18537 case DW_LNS_set_column:
18538 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18539 line_ptr += bytes_read;
18540 break;
18541 case DW_LNS_negate_stmt:
18542 state_machine.handle_negate_stmt ();
18543 break;
18544 case DW_LNS_set_basic_block:
18545 break;
18546 /* Add to the address register of the state machine the
18547 address increment value corresponding to special opcode
18548 255. I.e., this value is scaled by the minimum
18549 instruction length since special opcode 255 would have
18550 scaled the increment. */
18551 case DW_LNS_const_add_pc:
18552 state_machine.handle_const_add_pc ();
18553 break;
18554 case DW_LNS_fixed_advance_pc:
18555 {
18556 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18557 line_ptr += 2;
18558
18559 state_machine.handle_fixed_advance_pc (addr_adj);
18560 }
18561 break;
18562 case DW_LNS_set_prologue_end:
18563 state_machine.handle_set_prologue_end ();
18564 break;
18565 default:
18566 {
18567 /* Unknown standard opcode, ignore it. */
18568 int i;
18569
18570 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18571 {
18572 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18573 line_ptr += bytes_read;
18574 }
18575 }
18576 }
18577 }
18578
18579 if (!end_sequence)
18580 dwarf2_debug_line_missing_end_sequence_complaint ();
18581
18582 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18583 in which case we still finish recording the last line). */
18584 state_machine.record_line (true);
18585 }
18586 }
18587
18588 /* Decode the Line Number Program (LNP) for the given line_header
18589 structure and CU. The actual information extracted and the type
18590 of structures created from the LNP depends on the value of PST.
18591
18592 FND holds the CU file name and directory, if known.
18593 It is used for relative paths in the line table.
18594
18595 NOTE: It is important that psymtabs have the same file name (via
18596 strcmp) as the corresponding symtab. Since the directory is not
18597 used in the name of the symtab we don't use it in the name of the
18598 psymtabs we create. E.g. expand_line_sal requires this when
18599 finding psymtabs to expand. A good testcase for this is
18600 mb-inline.exp.
18601
18602 LOWPC is the lowest address in CU (or 0 if not known).
18603
18604 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18605 for its PC<->lines mapping information. Otherwise only the filename
18606 table is read in. */
18607
18608 static void
18609 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
18610 CORE_ADDR lowpc, int decode_mapping)
18611 {
18612 if (decode_mapping)
18613 dwarf_decode_lines_1 (lh, cu, lowpc);
18614
18615 /* Make sure a symtab is created for every file, even files
18616 which contain only variables (i.e. no code with associated
18617 line numbers). */
18618 buildsym_compunit *builder = cu->get_builder ();
18619 struct compunit_symtab *cust = builder->get_compunit_symtab ();
18620
18621 for (auto &fe : lh->file_names ())
18622 {
18623 dwarf2_start_subfile (cu, fe, *lh);
18624 subfile *sf = builder->get_current_subfile ();
18625
18626 if (sf->symtab == nullptr)
18627 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
18628 sf->name_for_id.c_str ());
18629
18630 fe.symtab = sf->symtab;
18631 }
18632 }
18633
18634 /* Start a subfile for DWARF. FILENAME is the name of the file and
18635 DIRNAME the name of the source directory which contains FILENAME
18636 or NULL if not known.
18637 This routine tries to keep line numbers from identical absolute and
18638 relative file names in a common subfile.
18639
18640 Using the `list' example from the GDB testsuite, which resides in
18641 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18642 of /srcdir/list0.c yields the following debugging information for list0.c:
18643
18644 DW_AT_name: /srcdir/list0.c
18645 DW_AT_comp_dir: /compdir
18646 files.files[0].name: list0.h
18647 files.files[0].dir: /srcdir
18648 files.files[1].name: list0.c
18649 files.files[1].dir: /srcdir
18650
18651 The line number information for list0.c has to end up in a single
18652 subfile, so that `break /srcdir/list0.c:1' works as expected.
18653 start_subfile will ensure that this happens provided that we pass the
18654 concatenation of files.files[1].dir and files.files[1].name as the
18655 subfile's name. */
18656
18657 static void
18658 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
18659 const line_header &lh)
18660 {
18661 std::string filename_holder;
18662 const char *filename = fe.name;
18663 const char *dirname = lh.include_dir_at (fe.d_index);
18664
18665 /* In order not to lose the line information directory,
18666 we concatenate it to the filename when it makes sense.
18667 Note that the Dwarf3 standard says (speaking of filenames in line
18668 information): ``The directory index is ignored for file names
18669 that represent full path names''. Thus ignoring dirname in the
18670 `else' branch below isn't an issue. */
18671
18672 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18673 {
18674 filename_holder = path_join (dirname, filename);
18675 filename = filename_holder.c_str ();
18676 }
18677
18678 std::string filename_for_id = lh.file_file_name (fe);
18679 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
18680 }
18681
18682 static void
18683 var_decode_location (struct attribute *attr, struct symbol *sym,
18684 struct dwarf2_cu *cu)
18685 {
18686 struct objfile *objfile = cu->per_objfile->objfile;
18687 struct comp_unit_head *cu_header = &cu->header;
18688
18689 /* NOTE drow/2003-01-30: There used to be a comment and some special
18690 code here to turn a symbol with DW_AT_external and a
18691 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18692 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18693 with some versions of binutils) where shared libraries could have
18694 relocations against symbols in their debug information - the
18695 minimal symbol would have the right address, but the debug info
18696 would not. It's no longer necessary, because we will explicitly
18697 apply relocations when we read in the debug information now. */
18698
18699 /* A DW_AT_location attribute with no contents indicates that a
18700 variable has been optimized away. */
18701 if (attr->form_is_block () && attr->as_block ()->size == 0)
18702 {
18703 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18704 return;
18705 }
18706
18707 /* Handle one degenerate form of location expression specially, to
18708 preserve GDB's previous behavior when section offsets are
18709 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
18710 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
18711
18712 if (attr->form_is_block ())
18713 {
18714 struct dwarf_block *block = attr->as_block ();
18715
18716 if ((block->data[0] == DW_OP_addr
18717 && block->size == 1 + cu_header->addr_size)
18718 || ((block->data[0] == DW_OP_GNU_addr_index
18719 || block->data[0] == DW_OP_addrx)
18720 && (block->size
18721 == 1 + leb128_size (&block->data[1]))))
18722 {
18723 unsigned int dummy;
18724
18725 if (block->data[0] == DW_OP_addr)
18726 sym->set_value_address
18727 (cu->header.read_address (objfile->obfd.get (), block->data + 1,
18728 &dummy));
18729 else
18730 sym->set_value_address
18731 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
18732 sym->set_aclass_index (LOC_STATIC);
18733 fixup_symbol_section (sym, objfile);
18734 sym->set_value_address
18735 (sym->value_address ()
18736 + objfile->section_offsets[sym->section_index ()]);
18737 return;
18738 }
18739 }
18740
18741 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18742 expression evaluator, and use LOC_COMPUTED only when necessary
18743 (i.e. when the value of a register or memory location is
18744 referenced, or a thread-local block, etc.). Then again, it might
18745 not be worthwhile. I'm assuming that it isn't unless performance
18746 or memory numbers show me otherwise. */
18747
18748 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18749
18750 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18751 cu->has_loclist = true;
18752 }
18753
18754 /* Given a pointer to a DWARF information entry, figure out if we need
18755 to make a symbol table entry for it, and if so, create a new entry
18756 and return a pointer to it.
18757 If TYPE is NULL, determine symbol type from the die, otherwise
18758 used the passed type.
18759 If SPACE is not NULL, use it to hold the new symbol. If it is
18760 NULL, allocate a new symbol on the objfile's obstack. */
18761
18762 static struct symbol *
18763 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18764 struct symbol *space)
18765 {
18766 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18767 struct objfile *objfile = per_objfile->objfile;
18768 struct gdbarch *gdbarch = objfile->arch ();
18769 struct symbol *sym = NULL;
18770 const char *name;
18771 struct attribute *attr = NULL;
18772 struct attribute *attr2 = NULL;
18773 CORE_ADDR baseaddr;
18774 struct pending **list_to_add = NULL;
18775
18776 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18777
18778 baseaddr = objfile->text_section_offset ();
18779
18780 name = dwarf2_name (die, cu);
18781 if (name == nullptr && (die->tag == DW_TAG_subprogram
18782 || die->tag == DW_TAG_inlined_subroutine
18783 || die->tag == DW_TAG_entry_point))
18784 name = dw2_linkage_name (die, cu);
18785
18786 if (name)
18787 {
18788 int suppress_add = 0;
18789
18790 if (space)
18791 sym = space;
18792 else
18793 sym = new (&objfile->objfile_obstack) symbol;
18794 OBJSTAT (objfile, n_syms++);
18795
18796 /* Cache this symbol's name and the name's demangled form (if any). */
18797 sym->set_language (cu->lang (), &objfile->objfile_obstack);
18798 /* Fortran does not have mangling standard and the mangling does differ
18799 between gfortran, iFort etc. */
18800 const char *physname
18801 = (cu->lang () == language_fortran
18802 ? dwarf2_full_name (name, die, cu)
18803 : dwarf2_physname (name, die, cu));
18804 const char *linkagename = dw2_linkage_name (die, cu);
18805
18806 if (linkagename == nullptr || cu->lang () == language_ada)
18807 sym->set_linkage_name (physname);
18808 else
18809 {
18810 sym->set_demangled_name (physname, &objfile->objfile_obstack);
18811 sym->set_linkage_name (linkagename);
18812 }
18813
18814 /* Handle DW_AT_artificial. */
18815 attr = dwarf2_attr (die, DW_AT_artificial, cu);
18816 if (attr != nullptr)
18817 sym->set_is_artificial (attr->as_boolean ());
18818
18819 /* Default assumptions.
18820 Use the passed type or decode it from the die. */
18821 sym->set_domain (VAR_DOMAIN);
18822 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18823 if (type != NULL)
18824 sym->set_type (type);
18825 else
18826 sym->set_type (die_type (die, cu));
18827 attr = dwarf2_attr (die,
18828 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18829 cu);
18830 if (attr != nullptr)
18831 sym->set_line (attr->constant_value (0));
18832
18833 attr = dwarf2_attr (die,
18834 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18835 cu);
18836 if (attr != nullptr && attr->is_nonnegative ())
18837 {
18838 file_name_index file_index
18839 = (file_name_index) attr->as_nonnegative ();
18840 struct file_entry *fe;
18841
18842 if (cu->line_header != NULL)
18843 fe = cu->line_header->file_name_at (file_index);
18844 else
18845 fe = NULL;
18846
18847 if (fe == NULL)
18848 complaint (_("file index out of range"));
18849 else
18850 sym->set_symtab (fe->symtab);
18851 }
18852
18853 switch (die->tag)
18854 {
18855 case DW_TAG_label:
18856 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18857 if (attr != nullptr)
18858 {
18859 CORE_ADDR addr;
18860
18861 addr = attr->as_address ();
18862 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18863 sym->set_section_index (SECT_OFF_TEXT (objfile));
18864 sym->set_value_address (addr);
18865 sym->set_aclass_index (LOC_LABEL);
18866 }
18867 else
18868 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18869 sym->set_type (objfile_type (objfile)->builtin_core_addr);
18870 sym->set_domain (LABEL_DOMAIN);
18871 add_symbol_to_list (sym, cu->list_in_scope);
18872 break;
18873 case DW_TAG_subprogram:
18874 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18875 finish_block. */
18876 sym->set_aclass_index (LOC_BLOCK);
18877 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18878 if ((attr2 != nullptr && attr2->as_boolean ())
18879 || cu->lang () == language_ada
18880 || cu->lang () == language_fortran)
18881 {
18882 /* Subprograms marked external are stored as a global symbol.
18883 Ada and Fortran subprograms, whether marked external or
18884 not, are always stored as a global symbol, because we want
18885 to be able to access them globally. For instance, we want
18886 to be able to break on a nested subprogram without having
18887 to specify the context. */
18888 list_to_add = cu->get_builder ()->get_global_symbols ();
18889 }
18890 else
18891 {
18892 list_to_add = cu->list_in_scope;
18893 }
18894 break;
18895 case DW_TAG_inlined_subroutine:
18896 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18897 finish_block. */
18898 sym->set_aclass_index (LOC_BLOCK);
18899 sym->set_is_inlined (1);
18900 list_to_add = cu->list_in_scope;
18901 break;
18902 case DW_TAG_template_value_param:
18903 suppress_add = 1;
18904 /* Fall through. */
18905 case DW_TAG_constant:
18906 case DW_TAG_variable:
18907 case DW_TAG_member:
18908 /* Compilation with minimal debug info may result in
18909 variables with missing type entries. Change the
18910 misleading `void' type to something sensible. */
18911 if (sym->type ()->code () == TYPE_CODE_VOID)
18912 sym->set_type (objfile_type (objfile)->builtin_int);
18913
18914 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18915 /* In the case of DW_TAG_member, we should only be called for
18916 static const members. */
18917 if (die->tag == DW_TAG_member)
18918 {
18919 /* dwarf2_add_field uses die_is_declaration,
18920 so we do the same. */
18921 gdb_assert (die_is_declaration (die, cu));
18922 gdb_assert (attr);
18923 }
18924 if (attr != nullptr)
18925 {
18926 dwarf2_const_value (attr, sym, cu);
18927 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18928 if (!suppress_add)
18929 {
18930 if (attr2 != nullptr && attr2->as_boolean ())
18931 list_to_add = cu->get_builder ()->get_global_symbols ();
18932 else
18933 list_to_add = cu->list_in_scope;
18934 }
18935 break;
18936 }
18937 attr = dwarf2_attr (die, DW_AT_location, cu);
18938 if (attr != nullptr)
18939 {
18940 var_decode_location (attr, sym, cu);
18941 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18942
18943 /* Fortran explicitly imports any global symbols to the local
18944 scope by DW_TAG_common_block. */
18945 if (cu->lang () == language_fortran && die->parent
18946 && die->parent->tag == DW_TAG_common_block)
18947 attr2 = NULL;
18948
18949 if (sym->aclass () == LOC_STATIC
18950 && sym->value_address () == 0
18951 && !per_objfile->per_bfd->has_section_at_zero)
18952 {
18953 /* When a static variable is eliminated by the linker,
18954 the corresponding debug information is not stripped
18955 out, but the variable address is set to null;
18956 do not add such variables into symbol table. */
18957 }
18958 else if (attr2 != nullptr && attr2->as_boolean ())
18959 {
18960 if (sym->aclass () == LOC_STATIC
18961 && (objfile->flags & OBJF_MAINLINE) == 0
18962 && per_objfile->per_bfd->can_copy)
18963 {
18964 /* A global static variable might be subject to
18965 copy relocation. We first check for a local
18966 minsym, though, because maybe the symbol was
18967 marked hidden, in which case this would not
18968 apply. */
18969 bound_minimal_symbol found
18970 = (lookup_minimal_symbol_linkage
18971 (sym->linkage_name (), objfile));
18972 if (found.minsym != nullptr)
18973 sym->maybe_copied = 1;
18974 }
18975
18976 /* A variable with DW_AT_external is never static,
18977 but it may be block-scoped. */
18978 list_to_add
18979 = ((cu->list_in_scope
18980 == cu->get_builder ()->get_file_symbols ())
18981 ? cu->get_builder ()->get_global_symbols ()
18982 : cu->list_in_scope);
18983 }
18984 else
18985 list_to_add = cu->list_in_scope;
18986 }
18987 else
18988 {
18989 /* We do not know the address of this symbol.
18990 If it is an external symbol and we have type information
18991 for it, enter the symbol as a LOC_UNRESOLVED symbol.
18992 The address of the variable will then be determined from
18993 the minimal symbol table whenever the variable is
18994 referenced. */
18995 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18996
18997 /* Fortran explicitly imports any global symbols to the local
18998 scope by DW_TAG_common_block. */
18999 if (cu->lang () == language_fortran && die->parent
19000 && die->parent->tag == DW_TAG_common_block)
19001 {
19002 /* SYMBOL_CLASS doesn't matter here because
19003 read_common_block is going to reset it. */
19004 if (!suppress_add)
19005 list_to_add = cu->list_in_scope;
19006 }
19007 else if (attr2 != nullptr && attr2->as_boolean ()
19008 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19009 {
19010 /* A variable with DW_AT_external is never static, but it
19011 may be block-scoped. */
19012 list_to_add
19013 = ((cu->list_in_scope
19014 == cu->get_builder ()->get_file_symbols ())
19015 ? cu->get_builder ()->get_global_symbols ()
19016 : cu->list_in_scope);
19017
19018 sym->set_aclass_index (LOC_UNRESOLVED);
19019 }
19020 else if (!die_is_declaration (die, cu))
19021 {
19022 /* Use the default LOC_OPTIMIZED_OUT class. */
19023 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
19024 if (!suppress_add)
19025 list_to_add = cu->list_in_scope;
19026 }
19027 }
19028 break;
19029 case DW_TAG_formal_parameter:
19030 {
19031 /* If we are inside a function, mark this as an argument. If
19032 not, we might be looking at an argument to an inlined function
19033 when we do not have enough information to show inlined frames;
19034 pretend it's a local variable in that case so that the user can
19035 still see it. */
19036 struct context_stack *curr
19037 = cu->get_builder ()->get_current_context_stack ();
19038 if (curr != nullptr && curr->name != nullptr)
19039 sym->set_is_argument (1);
19040 attr = dwarf2_attr (die, DW_AT_location, cu);
19041 if (attr != nullptr)
19042 {
19043 var_decode_location (attr, sym, cu);
19044 }
19045 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19046 if (attr != nullptr)
19047 {
19048 dwarf2_const_value (attr, sym, cu);
19049 }
19050
19051 list_to_add = cu->list_in_scope;
19052 }
19053 break;
19054 case DW_TAG_unspecified_parameters:
19055 /* From varargs functions; gdb doesn't seem to have any
19056 interest in this information, so just ignore it for now.
19057 (FIXME?) */
19058 break;
19059 case DW_TAG_template_type_param:
19060 suppress_add = 1;
19061 /* Fall through. */
19062 case DW_TAG_class_type:
19063 case DW_TAG_interface_type:
19064 case DW_TAG_structure_type:
19065 case DW_TAG_union_type:
19066 case DW_TAG_set_type:
19067 case DW_TAG_enumeration_type:
19068 case DW_TAG_namelist:
19069 if (die->tag == DW_TAG_namelist)
19070 {
19071 sym->set_aclass_index (LOC_STATIC);
19072 sym->set_domain (VAR_DOMAIN);
19073 }
19074 else
19075 {
19076 sym->set_aclass_index (LOC_TYPEDEF);
19077 sym->set_domain (STRUCT_DOMAIN);
19078 }
19079 {
19080 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19081 really ever be static objects: otherwise, if you try
19082 to, say, break of a class's method and you're in a file
19083 which doesn't mention that class, it won't work unless
19084 the check for all static symbols in lookup_symbol_aux
19085 saves you. See the OtherFileClass tests in
19086 gdb.c++/namespace.exp. */
19087
19088 if (!suppress_add)
19089 {
19090 buildsym_compunit *builder = cu->get_builder ();
19091 list_to_add
19092 = (cu->list_in_scope == builder->get_file_symbols ()
19093 && cu->lang () == language_cplus
19094 ? builder->get_global_symbols ()
19095 : cu->list_in_scope);
19096
19097 /* The semantics of C++ state that "struct foo {
19098 ... }" also defines a typedef for "foo". */
19099 if (cu->lang () == language_cplus
19100 || cu->lang () == language_ada
19101 || cu->lang () == language_d
19102 || cu->lang () == language_rust)
19103 {
19104 /* The symbol's name is already allocated along
19105 with this objfile, so we don't need to
19106 duplicate it for the type. */
19107 if (sym->type ()->name () == 0)
19108 sym->type ()->set_name (sym->search_name ());
19109 }
19110 }
19111 }
19112 break;
19113 case DW_TAG_typedef:
19114 sym->set_aclass_index (LOC_TYPEDEF);
19115 sym->set_domain (VAR_DOMAIN);
19116 list_to_add = cu->list_in_scope;
19117 break;
19118 case DW_TAG_array_type:
19119 case DW_TAG_base_type:
19120 case DW_TAG_subrange_type:
19121 case DW_TAG_generic_subrange:
19122 case DW_TAG_unspecified_type:
19123 sym->set_aclass_index (LOC_TYPEDEF);
19124 sym->set_domain (VAR_DOMAIN);
19125 list_to_add = cu->list_in_scope;
19126 break;
19127 case DW_TAG_enumerator:
19128 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19129 if (attr != nullptr)
19130 {
19131 dwarf2_const_value (attr, sym, cu);
19132 }
19133 {
19134 /* NOTE: carlton/2003-11-10: See comment above in the
19135 DW_TAG_class_type, etc. block. */
19136
19137 list_to_add
19138 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
19139 && cu->lang () == language_cplus
19140 ? cu->get_builder ()->get_global_symbols ()
19141 : cu->list_in_scope);
19142 }
19143 break;
19144 case DW_TAG_imported_declaration:
19145 case DW_TAG_namespace:
19146 sym->set_aclass_index (LOC_TYPEDEF);
19147 list_to_add = cu->get_builder ()->get_global_symbols ();
19148 break;
19149 case DW_TAG_module:
19150 sym->set_aclass_index (LOC_TYPEDEF);
19151 sym->set_domain (MODULE_DOMAIN);
19152 list_to_add = cu->get_builder ()->get_global_symbols ();
19153 break;
19154 case DW_TAG_common_block:
19155 sym->set_aclass_index (LOC_COMMON_BLOCK);
19156 sym->set_domain (COMMON_BLOCK_DOMAIN);
19157 add_symbol_to_list (sym, cu->list_in_scope);
19158 break;
19159 default:
19160 /* Not a tag we recognize. Hopefully we aren't processing
19161 trash data, but since we must specifically ignore things
19162 we don't recognize, there is nothing else we should do at
19163 this point. */
19164 complaint (_("unsupported tag: '%s'"),
19165 dwarf_tag_name (die->tag));
19166 break;
19167 }
19168
19169 if (suppress_add)
19170 {
19171 sym->hash_next = objfile->template_symbols;
19172 objfile->template_symbols = sym;
19173 list_to_add = NULL;
19174 }
19175
19176 if (list_to_add != NULL)
19177 add_symbol_to_list (sym, list_to_add);
19178
19179 /* For the benefit of old versions of GCC, check for anonymous
19180 namespaces based on the demangled name. */
19181 if (!cu->processing_has_namespace_info
19182 && cu->lang () == language_cplus)
19183 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
19184 }
19185 return (sym);
19186 }
19187
19188 /* Given an attr with a DW_FORM_dataN value in host byte order,
19189 zero-extend it as appropriate for the symbol's type. The DWARF
19190 standard (v4) is not entirely clear about the meaning of using
19191 DW_FORM_dataN for a constant with a signed type, where the type is
19192 wider than the data. The conclusion of a discussion on the DWARF
19193 list was that this is unspecified. We choose to always zero-extend
19194 because that is the interpretation long in use by GCC. */
19195
19196 static gdb_byte *
19197 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19198 struct dwarf2_cu *cu, LONGEST *value, int bits)
19199 {
19200 struct objfile *objfile = cu->per_objfile->objfile;
19201 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
19202 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19203 LONGEST l = attr->constant_value (0);
19204
19205 if (bits < sizeof (*value) * 8)
19206 {
19207 l &= ((LONGEST) 1 << bits) - 1;
19208 *value = l;
19209 }
19210 else if (bits == sizeof (*value) * 8)
19211 *value = l;
19212 else
19213 {
19214 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19215 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19216 return bytes;
19217 }
19218
19219 return NULL;
19220 }
19221
19222 /* Read a constant value from an attribute. Either set *VALUE, or if
19223 the value does not fit in *VALUE, set *BYTES - either already
19224 allocated on the objfile obstack, or newly allocated on OBSTACK,
19225 or, set *BATON, if we translated the constant to a location
19226 expression. */
19227
19228 static void
19229 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19230 const char *name, struct obstack *obstack,
19231 struct dwarf2_cu *cu,
19232 LONGEST *value, const gdb_byte **bytes,
19233 struct dwarf2_locexpr_baton **baton)
19234 {
19235 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19236 struct objfile *objfile = per_objfile->objfile;
19237 struct comp_unit_head *cu_header = &cu->header;
19238 struct dwarf_block *blk;
19239 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
19240 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19241
19242 *value = 0;
19243 *bytes = NULL;
19244 *baton = NULL;
19245
19246 switch (attr->form)
19247 {
19248 case DW_FORM_addr:
19249 case DW_FORM_addrx:
19250 case DW_FORM_GNU_addr_index:
19251 {
19252 gdb_byte *data;
19253
19254 if (type->length () != cu_header->addr_size)
19255 dwarf2_const_value_length_mismatch_complaint (name,
19256 cu_header->addr_size,
19257 type->length ());
19258 /* Symbols of this form are reasonably rare, so we just
19259 piggyback on the existing location code rather than writing
19260 a new implementation of symbol_computed_ops. */
19261 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19262 (*baton)->per_objfile = per_objfile;
19263 (*baton)->per_cu = cu->per_cu;
19264 gdb_assert ((*baton)->per_cu);
19265
19266 (*baton)->size = 2 + cu_header->addr_size;
19267 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19268 (*baton)->data = data;
19269
19270 data[0] = DW_OP_addr;
19271 store_unsigned_integer (&data[1], cu_header->addr_size,
19272 byte_order, attr->as_address ());
19273 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19274 }
19275 break;
19276 case DW_FORM_string:
19277 case DW_FORM_strp:
19278 case DW_FORM_strx:
19279 case DW_FORM_GNU_str_index:
19280 case DW_FORM_GNU_strp_alt:
19281 /* The string is already allocated on the objfile obstack, point
19282 directly to it. */
19283 *bytes = (const gdb_byte *) attr->as_string ();
19284 break;
19285 case DW_FORM_block1:
19286 case DW_FORM_block2:
19287 case DW_FORM_block4:
19288 case DW_FORM_block:
19289 case DW_FORM_exprloc:
19290 case DW_FORM_data16:
19291 blk = attr->as_block ();
19292 if (type->length () != blk->size)
19293 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19294 type->length ());
19295 *bytes = blk->data;
19296 break;
19297
19298 /* The DW_AT_const_value attributes are supposed to carry the
19299 symbol's value "represented as it would be on the target
19300 architecture." By the time we get here, it's already been
19301 converted to host endianness, so we just need to sign- or
19302 zero-extend it as appropriate. */
19303 case DW_FORM_data1:
19304 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19305 break;
19306 case DW_FORM_data2:
19307 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19308 break;
19309 case DW_FORM_data4:
19310 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19311 break;
19312 case DW_FORM_data8:
19313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19314 break;
19315
19316 case DW_FORM_sdata:
19317 case DW_FORM_implicit_const:
19318 *value = attr->as_signed ();
19319 break;
19320
19321 case DW_FORM_udata:
19322 *value = attr->as_unsigned ();
19323 break;
19324
19325 default:
19326 complaint (_("unsupported const value attribute form: '%s'"),
19327 dwarf_form_name (attr->form));
19328 *value = 0;
19329 break;
19330 }
19331 }
19332
19333
19334 /* Copy constant value from an attribute to a symbol. */
19335
19336 static void
19337 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19338 struct dwarf2_cu *cu)
19339 {
19340 struct objfile *objfile = cu->per_objfile->objfile;
19341 LONGEST value;
19342 const gdb_byte *bytes;
19343 struct dwarf2_locexpr_baton *baton;
19344
19345 dwarf2_const_value_attr (attr, sym->type (),
19346 sym->print_name (),
19347 &objfile->objfile_obstack, cu,
19348 &value, &bytes, &baton);
19349
19350 if (baton != NULL)
19351 {
19352 SYMBOL_LOCATION_BATON (sym) = baton;
19353 sym->set_aclass_index (dwarf2_locexpr_index);
19354 }
19355 else if (bytes != NULL)
19356 {
19357 sym->set_value_bytes (bytes);
19358 sym->set_aclass_index (LOC_CONST_BYTES);
19359 }
19360 else
19361 {
19362 sym->set_value_longest (value);
19363 sym->set_aclass_index (LOC_CONST);
19364 }
19365 }
19366
19367 /* Return the type of the die in question using its DW_AT_type attribute. */
19368
19369 static struct type *
19370 die_type (struct die_info *die, struct dwarf2_cu *cu)
19371 {
19372 struct attribute *type_attr;
19373
19374 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19375 if (!type_attr)
19376 {
19377 struct objfile *objfile = cu->per_objfile->objfile;
19378 /* A missing DW_AT_type represents a void type. */
19379 return objfile_type (objfile)->builtin_void;
19380 }
19381
19382 return lookup_die_type (die, type_attr, cu);
19383 }
19384
19385 /* True iff CU's producer generates GNAT Ada auxiliary information
19386 that allows to find parallel types through that information instead
19387 of having to do expensive parallel lookups by type name. */
19388
19389 static int
19390 need_gnat_info (struct dwarf2_cu *cu)
19391 {
19392 /* Assume that the Ada compiler was GNAT, which always produces
19393 the auxiliary information. */
19394 return (cu->lang () == language_ada);
19395 }
19396
19397 /* Return the auxiliary type of the die in question using its
19398 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19399 attribute is not present. */
19400
19401 static struct type *
19402 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19403 {
19404 struct attribute *type_attr;
19405
19406 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19407 if (!type_attr)
19408 return NULL;
19409
19410 return lookup_die_type (die, type_attr, cu);
19411 }
19412
19413 /* If DIE has a descriptive_type attribute, then set the TYPE's
19414 descriptive type accordingly. */
19415
19416 static void
19417 set_descriptive_type (struct type *type, struct die_info *die,
19418 struct dwarf2_cu *cu)
19419 {
19420 struct type *descriptive_type = die_descriptive_type (die, cu);
19421
19422 if (descriptive_type)
19423 {
19424 ALLOCATE_GNAT_AUX_TYPE (type);
19425 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19426 }
19427 }
19428
19429 /* Return the containing type of the die in question using its
19430 DW_AT_containing_type attribute. */
19431
19432 static struct type *
19433 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19434 {
19435 struct attribute *type_attr;
19436 struct objfile *objfile = cu->per_objfile->objfile;
19437
19438 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19439 if (!type_attr)
19440 error (_("Dwarf Error: Problem turning containing type into gdb type "
19441 "[in module %s]"), objfile_name (objfile));
19442
19443 return lookup_die_type (die, type_attr, cu);
19444 }
19445
19446 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19447
19448 static struct type *
19449 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19450 {
19451 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19452 struct objfile *objfile = per_objfile->objfile;
19453 char *saved;
19454
19455 std::string message
19456 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
19457 objfile_name (objfile),
19458 sect_offset_str (cu->header.sect_off),
19459 sect_offset_str (die->sect_off));
19460 saved = obstack_strdup (&objfile->objfile_obstack, message);
19461
19462 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19463 }
19464
19465 /* Look up the type of DIE in CU using its type attribute ATTR.
19466 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19467 DW_AT_containing_type.
19468 If there is no type substitute an error marker. */
19469
19470 static struct type *
19471 lookup_die_type (struct die_info *die, const struct attribute *attr,
19472 struct dwarf2_cu *cu)
19473 {
19474 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19475 struct objfile *objfile = per_objfile->objfile;
19476 struct type *this_type;
19477
19478 gdb_assert (attr->name == DW_AT_type
19479 || attr->name == DW_AT_GNAT_descriptive_type
19480 || attr->name == DW_AT_containing_type);
19481
19482 /* First see if we have it cached. */
19483
19484 if (attr->form == DW_FORM_GNU_ref_alt)
19485 {
19486 struct dwarf2_per_cu_data *per_cu;
19487 sect_offset sect_off = attr->get_ref_die_offset ();
19488
19489 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
19490 per_objfile->per_bfd);
19491 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
19492 }
19493 else if (attr->form_is_ref ())
19494 {
19495 sect_offset sect_off = attr->get_ref_die_offset ();
19496
19497 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
19498 }
19499 else if (attr->form == DW_FORM_ref_sig8)
19500 {
19501 ULONGEST signature = attr->as_signature ();
19502
19503 return get_signatured_type (die, signature, cu);
19504 }
19505 else
19506 {
19507 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
19508 " at %s [in module %s]"),
19509 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
19510 objfile_name (objfile));
19511 return build_error_marker_type (cu, die);
19512 }
19513
19514 /* If not cached we need to read it in. */
19515
19516 if (this_type == NULL)
19517 {
19518 struct die_info *type_die = NULL;
19519 struct dwarf2_cu *type_cu = cu;
19520
19521 if (attr->form_is_ref ())
19522 type_die = follow_die_ref (die, attr, &type_cu);
19523 if (type_die == NULL)
19524 return build_error_marker_type (cu, die);
19525 /* If we find the type now, it's probably because the type came
19526 from an inter-CU reference and the type's CU got expanded before
19527 ours. */
19528 this_type = read_type_die (type_die, type_cu);
19529 }
19530
19531 /* If we still don't have a type use an error marker. */
19532
19533 if (this_type == NULL)
19534 return build_error_marker_type (cu, die);
19535
19536 return this_type;
19537 }
19538
19539 /* Return the type in DIE, CU.
19540 Returns NULL for invalid types.
19541
19542 This first does a lookup in die_type_hash,
19543 and only reads the die in if necessary.
19544
19545 NOTE: This can be called when reading in partial or full symbols. */
19546
19547 static struct type *
19548 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19549 {
19550 struct type *this_type;
19551
19552 this_type = get_die_type (die, cu);
19553 if (this_type)
19554 return this_type;
19555
19556 return read_type_die_1 (die, cu);
19557 }
19558
19559 /* Read the type in DIE, CU.
19560 Returns NULL for invalid types. */
19561
19562 static struct type *
19563 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19564 {
19565 struct type *this_type = NULL;
19566
19567 switch (die->tag)
19568 {
19569 case DW_TAG_class_type:
19570 case DW_TAG_interface_type:
19571 case DW_TAG_structure_type:
19572 case DW_TAG_union_type:
19573 this_type = read_structure_type (die, cu);
19574 break;
19575 case DW_TAG_enumeration_type:
19576 this_type = read_enumeration_type (die, cu);
19577 break;
19578 case DW_TAG_subprogram:
19579 case DW_TAG_subroutine_type:
19580 case DW_TAG_inlined_subroutine:
19581 this_type = read_subroutine_type (die, cu);
19582 break;
19583 case DW_TAG_array_type:
19584 this_type = read_array_type (die, cu);
19585 break;
19586 case DW_TAG_set_type:
19587 this_type = read_set_type (die, cu);
19588 break;
19589 case DW_TAG_pointer_type:
19590 this_type = read_tag_pointer_type (die, cu);
19591 break;
19592 case DW_TAG_ptr_to_member_type:
19593 this_type = read_tag_ptr_to_member_type (die, cu);
19594 break;
19595 case DW_TAG_reference_type:
19596 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19597 break;
19598 case DW_TAG_rvalue_reference_type:
19599 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19600 break;
19601 case DW_TAG_const_type:
19602 this_type = read_tag_const_type (die, cu);
19603 break;
19604 case DW_TAG_volatile_type:
19605 this_type = read_tag_volatile_type (die, cu);
19606 break;
19607 case DW_TAG_restrict_type:
19608 this_type = read_tag_restrict_type (die, cu);
19609 break;
19610 case DW_TAG_string_type:
19611 this_type = read_tag_string_type (die, cu);
19612 break;
19613 case DW_TAG_typedef:
19614 this_type = read_typedef (die, cu);
19615 break;
19616 case DW_TAG_generic_subrange:
19617 case DW_TAG_subrange_type:
19618 this_type = read_subrange_type (die, cu);
19619 break;
19620 case DW_TAG_base_type:
19621 this_type = read_base_type (die, cu);
19622 break;
19623 case DW_TAG_unspecified_type:
19624 this_type = read_unspecified_type (die, cu);
19625 break;
19626 case DW_TAG_namespace:
19627 this_type = read_namespace_type (die, cu);
19628 break;
19629 case DW_TAG_module:
19630 this_type = read_module_type (die, cu);
19631 break;
19632 case DW_TAG_atomic_type:
19633 this_type = read_tag_atomic_type (die, cu);
19634 break;
19635 default:
19636 complaint (_("unexpected tag in read_type_die: '%s'"),
19637 dwarf_tag_name (die->tag));
19638 break;
19639 }
19640
19641 return this_type;
19642 }
19643
19644 /* See if we can figure out if the class lives in a namespace. We do
19645 this by looking for a member function; its demangled name will
19646 contain namespace info, if there is any.
19647 Return the computed name or NULL.
19648 Space for the result is allocated on the objfile's obstack.
19649 This is the full-die version of guess_partial_die_structure_name.
19650 In this case we know DIE has no useful parent. */
19651
19652 static const char *
19653 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19654 {
19655 struct die_info *spec_die;
19656 struct dwarf2_cu *spec_cu;
19657 struct die_info *child;
19658 struct objfile *objfile = cu->per_objfile->objfile;
19659
19660 spec_cu = cu;
19661 spec_die = die_specification (die, &spec_cu);
19662 if (spec_die != NULL)
19663 {
19664 die = spec_die;
19665 cu = spec_cu;
19666 }
19667
19668 for (child = die->child;
19669 child != NULL;
19670 child = child->sibling)
19671 {
19672 if (child->tag == DW_TAG_subprogram)
19673 {
19674 const char *linkage_name = dw2_linkage_name (child, cu);
19675
19676 if (linkage_name != NULL)
19677 {
19678 gdb::unique_xmalloc_ptr<char> actual_name
19679 (cu->language_defn->class_name_from_physname (linkage_name));
19680 const char *name = NULL;
19681
19682 if (actual_name != NULL)
19683 {
19684 const char *die_name = dwarf2_name (die, cu);
19685
19686 if (die_name != NULL
19687 && strcmp (die_name, actual_name.get ()) != 0)
19688 {
19689 /* Strip off the class name from the full name.
19690 We want the prefix. */
19691 int die_name_len = strlen (die_name);
19692 int actual_name_len = strlen (actual_name.get ());
19693 const char *ptr = actual_name.get ();
19694
19695 /* Test for '::' as a sanity check. */
19696 if (actual_name_len > die_name_len + 2
19697 && ptr[actual_name_len - die_name_len - 1] == ':')
19698 name = obstack_strndup (
19699 &objfile->per_bfd->storage_obstack,
19700 ptr, actual_name_len - die_name_len - 2);
19701 }
19702 }
19703 return name;
19704 }
19705 }
19706 }
19707
19708 return NULL;
19709 }
19710
19711 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19712 prefix part in such case. See
19713 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19714
19715 static const char *
19716 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19717 {
19718 struct attribute *attr;
19719 const char *base;
19720
19721 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19722 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19723 return NULL;
19724
19725 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19726 return NULL;
19727
19728 attr = dw2_linkage_name_attr (die, cu);
19729 const char *attr_name = attr->as_string ();
19730 if (attr == NULL || attr_name == NULL)
19731 return NULL;
19732
19733 /* dwarf2_name had to be already called. */
19734 gdb_assert (attr->canonical_string_p ());
19735
19736 /* Strip the base name, keep any leading namespaces/classes. */
19737 base = strrchr (attr_name, ':');
19738 if (base == NULL || base == attr_name || base[-1] != ':')
19739 return "";
19740
19741 struct objfile *objfile = cu->per_objfile->objfile;
19742 return obstack_strndup (&objfile->per_bfd->storage_obstack,
19743 attr_name,
19744 &base[-1] - attr_name);
19745 }
19746
19747 /* Return the name of the namespace/class that DIE is defined within,
19748 or "" if we can't tell. The caller should not xfree the result.
19749
19750 For example, if we're within the method foo() in the following
19751 code:
19752
19753 namespace N {
19754 class C {
19755 void foo () {
19756 }
19757 };
19758 }
19759
19760 then determine_prefix on foo's die will return "N::C". */
19761
19762 static const char *
19763 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19764 {
19765 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19766 struct die_info *parent, *spec_die;
19767 struct dwarf2_cu *spec_cu;
19768 struct type *parent_type;
19769 const char *retval;
19770
19771 if (cu->lang () != language_cplus
19772 && cu->lang () != language_fortran
19773 && cu->lang () != language_d
19774 && cu->lang () != language_rust)
19775 return "";
19776
19777 retval = anonymous_struct_prefix (die, cu);
19778 if (retval)
19779 return retval;
19780
19781 /* We have to be careful in the presence of DW_AT_specification.
19782 For example, with GCC 3.4, given the code
19783
19784 namespace N {
19785 void foo() {
19786 // Definition of N::foo.
19787 }
19788 }
19789
19790 then we'll have a tree of DIEs like this:
19791
19792 1: DW_TAG_compile_unit
19793 2: DW_TAG_namespace // N
19794 3: DW_TAG_subprogram // declaration of N::foo
19795 4: DW_TAG_subprogram // definition of N::foo
19796 DW_AT_specification // refers to die #3
19797
19798 Thus, when processing die #4, we have to pretend that we're in
19799 the context of its DW_AT_specification, namely the contex of die
19800 #3. */
19801 spec_cu = cu;
19802 spec_die = die_specification (die, &spec_cu);
19803 if (spec_die == NULL)
19804 parent = die->parent;
19805 else
19806 {
19807 parent = spec_die->parent;
19808 cu = spec_cu;
19809 }
19810
19811 if (parent == NULL)
19812 return "";
19813 else if (parent->building_fullname)
19814 {
19815 const char *name;
19816 const char *parent_name;
19817
19818 /* It has been seen on RealView 2.2 built binaries,
19819 DW_TAG_template_type_param types actually _defined_ as
19820 children of the parent class:
19821
19822 enum E {};
19823 template class <class Enum> Class{};
19824 Class<enum E> class_e;
19825
19826 1: DW_TAG_class_type (Class)
19827 2: DW_TAG_enumeration_type (E)
19828 3: DW_TAG_enumerator (enum1:0)
19829 3: DW_TAG_enumerator (enum2:1)
19830 ...
19831 2: DW_TAG_template_type_param
19832 DW_AT_type DW_FORM_ref_udata (E)
19833
19834 Besides being broken debug info, it can put GDB into an
19835 infinite loop. Consider:
19836
19837 When we're building the full name for Class<E>, we'll start
19838 at Class, and go look over its template type parameters,
19839 finding E. We'll then try to build the full name of E, and
19840 reach here. We're now trying to build the full name of E,
19841 and look over the parent DIE for containing scope. In the
19842 broken case, if we followed the parent DIE of E, we'd again
19843 find Class, and once again go look at its template type
19844 arguments, etc., etc. Simply don't consider such parent die
19845 as source-level parent of this die (it can't be, the language
19846 doesn't allow it), and break the loop here. */
19847 name = dwarf2_name (die, cu);
19848 parent_name = dwarf2_name (parent, cu);
19849 complaint (_("template param type '%s' defined within parent '%s'"),
19850 name ? name : "<unknown>",
19851 parent_name ? parent_name : "<unknown>");
19852 return "";
19853 }
19854 else
19855 switch (parent->tag)
19856 {
19857 case DW_TAG_namespace:
19858 parent_type = read_type_die (parent, cu);
19859 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19860 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19861 Work around this problem here. */
19862 if (cu->lang () == language_cplus
19863 && strcmp (parent_type->name (), "::") == 0)
19864 return "";
19865 /* We give a name to even anonymous namespaces. */
19866 return parent_type->name ();
19867 case DW_TAG_class_type:
19868 case DW_TAG_interface_type:
19869 case DW_TAG_structure_type:
19870 case DW_TAG_union_type:
19871 case DW_TAG_module:
19872 parent_type = read_type_die (parent, cu);
19873 if (parent_type->name () != NULL)
19874 return parent_type->name ();
19875 else
19876 /* An anonymous structure is only allowed non-static data
19877 members; no typedefs, no member functions, et cetera.
19878 So it does not need a prefix. */
19879 return "";
19880 case DW_TAG_compile_unit:
19881 case DW_TAG_partial_unit:
19882 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19883 if (cu->lang () == language_cplus
19884 && !per_objfile->per_bfd->types.empty ()
19885 && die->child != NULL
19886 && (die->tag == DW_TAG_class_type
19887 || die->tag == DW_TAG_structure_type
19888 || die->tag == DW_TAG_union_type))
19889 {
19890 const char *name = guess_full_die_structure_name (die, cu);
19891 if (name != NULL)
19892 return name;
19893 }
19894 return "";
19895 case DW_TAG_subprogram:
19896 /* Nested subroutines in Fortran get a prefix with the name
19897 of the parent's subroutine. */
19898 if (cu->lang () == language_fortran)
19899 {
19900 if ((die->tag == DW_TAG_subprogram)
19901 && (dwarf2_name (parent, cu) != NULL))
19902 return dwarf2_name (parent, cu);
19903 }
19904 return "";
19905 case DW_TAG_enumeration_type:
19906 parent_type = read_type_die (parent, cu);
19907 if (parent_type->is_declared_class ())
19908 {
19909 if (parent_type->name () != NULL)
19910 return parent_type->name ();
19911 return "";
19912 }
19913 /* Fall through. */
19914 default:
19915 return determine_prefix (parent, cu);
19916 }
19917 }
19918
19919 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19920 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19921 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19922 an obconcat, otherwise allocate storage for the result. The CU argument is
19923 used to determine the language and hence, the appropriate separator. */
19924
19925 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19926
19927 static char *
19928 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19929 int physname, struct dwarf2_cu *cu)
19930 {
19931 const char *lead = "";
19932 const char *sep;
19933
19934 if (suffix == NULL || suffix[0] == '\0'
19935 || prefix == NULL || prefix[0] == '\0')
19936 sep = "";
19937 else if (cu->lang () == language_d)
19938 {
19939 /* For D, the 'main' function could be defined in any module, but it
19940 should never be prefixed. */
19941 if (strcmp (suffix, "D main") == 0)
19942 {
19943 prefix = "";
19944 sep = "";
19945 }
19946 else
19947 sep = ".";
19948 }
19949 else if (cu->lang () == language_fortran && physname)
19950 {
19951 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19952 DW_AT_MIPS_linkage_name is preferred and used instead. */
19953
19954 lead = "__";
19955 sep = "_MOD_";
19956 }
19957 else
19958 sep = "::";
19959
19960 if (prefix == NULL)
19961 prefix = "";
19962 if (suffix == NULL)
19963 suffix = "";
19964
19965 if (obs == NULL)
19966 {
19967 char *retval
19968 = ((char *)
19969 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19970
19971 strcpy (retval, lead);
19972 strcat (retval, prefix);
19973 strcat (retval, sep);
19974 strcat (retval, suffix);
19975 return retval;
19976 }
19977 else
19978 {
19979 /* We have an obstack. */
19980 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19981 }
19982 }
19983
19984 /* Return a generic name for a DW_TAG_template_type_param or
19985 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
19986 per parent, so each function/class/struct template will have their own set
19987 of template parameters named <unnnamed0>, <unnamed1>, ... where the
19988 enumeration starts at 0 and represents the position of the template tag in
19989 the list of unnamed template tags for this parent, counting both, type and
19990 value tags. */
19991
19992 static const char *
19993 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
19994 {
19995 if (die->parent == nullptr)
19996 return nullptr;
19997
19998 /* Count the parent types unnamed template type and value children until, we
19999 arrive at our entry. */
20000 size_t nth_unnamed = 0;
20001
20002 die_info *child = die->parent->child;
20003 while (child != die)
20004 {
20005 gdb_assert (child != nullptr);
20006 if (child->tag == DW_TAG_template_type_param
20007 || child->tag == DW_TAG_template_value_param)
20008 {
20009 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
20010 ++nth_unnamed;
20011 }
20012 child = child->sibling;
20013 }
20014
20015 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
20016 return cu->per_objfile->objfile->intern (name_str.c_str ());
20017 }
20018
20019 /* Get name of a die, return NULL if not found. */
20020
20021 static const char *
20022 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20023 struct objfile *objfile)
20024 {
20025 if (name == nullptr)
20026 return name;
20027
20028 if (cu->lang () == language_cplus)
20029 {
20030 gdb::unique_xmalloc_ptr<char> canon_name
20031 = cp_canonicalize_string (name);
20032
20033 if (canon_name != nullptr)
20034 name = objfile->intern (canon_name.get ());
20035 }
20036 else if (cu->lang () == language_c)
20037 {
20038 gdb::unique_xmalloc_ptr<char> canon_name
20039 = c_canonicalize_name (name);
20040
20041 if (canon_name != nullptr)
20042 name = objfile->intern (canon_name.get ());
20043 }
20044
20045 return name;
20046 }
20047
20048 /* Get name of a die, return NULL if not found.
20049 Anonymous namespaces are converted to their magic string. */
20050
20051 static const char *
20052 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20053 {
20054 struct attribute *attr;
20055 struct objfile *objfile = cu->per_objfile->objfile;
20056
20057 attr = dwarf2_attr (die, DW_AT_name, cu);
20058 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
20059 if (attr_name == nullptr
20060 && die->tag != DW_TAG_namespace
20061 && die->tag != DW_TAG_class_type
20062 && die->tag != DW_TAG_interface_type
20063 && die->tag != DW_TAG_structure_type
20064 && die->tag != DW_TAG_namelist
20065 && die->tag != DW_TAG_union_type
20066 && die->tag != DW_TAG_template_type_param
20067 && die->tag != DW_TAG_template_value_param)
20068 return NULL;
20069
20070 switch (die->tag)
20071 {
20072 /* A member's name should not be canonicalized. This is a bit
20073 of a hack, in that normally it should not be possible to run
20074 into this situation; however, the dw2-unusual-field-names.exp
20075 test creates custom DWARF that does. */
20076 case DW_TAG_member:
20077 case DW_TAG_compile_unit:
20078 case DW_TAG_partial_unit:
20079 /* Compilation units have a DW_AT_name that is a filename, not
20080 a source language identifier. */
20081 case DW_TAG_enumeration_type:
20082 case DW_TAG_enumerator:
20083 /* These tags always have simple identifiers already; no need
20084 to canonicalize them. */
20085 return attr_name;
20086
20087 case DW_TAG_namespace:
20088 if (attr_name != nullptr)
20089 return attr_name;
20090 return CP_ANONYMOUS_NAMESPACE_STR;
20091
20092 /* DWARF does not actually require template tags to have a name. */
20093 case DW_TAG_template_type_param:
20094 case DW_TAG_template_value_param:
20095 if (attr_name == nullptr)
20096 return unnamed_template_tag_name (die, cu);
20097 /* FALLTHROUGH. */
20098 case DW_TAG_class_type:
20099 case DW_TAG_interface_type:
20100 case DW_TAG_structure_type:
20101 case DW_TAG_union_type:
20102 case DW_TAG_namelist:
20103 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20104 structures or unions. These were of the form "._%d" in GCC 4.1,
20105 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20106 and GCC 4.4. We work around this problem by ignoring these. */
20107 if (attr_name != nullptr
20108 && (startswith (attr_name, "._")
20109 || startswith (attr_name, "<anonymous")))
20110 return NULL;
20111
20112 /* GCC might emit a nameless typedef that has a linkage name. See
20113 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20114 if (!attr || attr_name == NULL)
20115 {
20116 attr = dw2_linkage_name_attr (die, cu);
20117 attr_name = attr == nullptr ? nullptr : attr->as_string ();
20118 if (attr == NULL || attr_name == NULL)
20119 return NULL;
20120
20121 /* Avoid demangling attr_name the second time on a second
20122 call for the same DIE. */
20123 if (!attr->canonical_string_p ())
20124 {
20125 gdb::unique_xmalloc_ptr<char> demangled
20126 (gdb_demangle (attr_name, DMGL_TYPES));
20127 if (demangled == nullptr)
20128 return nullptr;
20129
20130 attr->set_string_canonical (objfile->intern (demangled.get ()));
20131 attr_name = attr->as_string ();
20132 }
20133
20134 /* Strip any leading namespaces/classes, keep only the
20135 base name. DW_AT_name for named DIEs does not
20136 contain the prefixes. */
20137 const char *base = strrchr (attr_name, ':');
20138 if (base && base > attr_name && base[-1] == ':')
20139 return &base[1];
20140 else
20141 return attr_name;
20142 }
20143 break;
20144
20145 default:
20146 break;
20147 }
20148
20149 if (!attr->canonical_string_p ())
20150 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
20151 objfile));
20152 return attr->as_string ();
20153 }
20154
20155 /* Return the die that this die in an extension of, or NULL if there
20156 is none. *EXT_CU is the CU containing DIE on input, and the CU
20157 containing the return value on output. */
20158
20159 static struct die_info *
20160 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20161 {
20162 struct attribute *attr;
20163
20164 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20165 if (attr == NULL)
20166 return NULL;
20167
20168 return follow_die_ref (die, attr, ext_cu);
20169 }
20170
20171 static void
20172 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20173 {
20174 void **slot;
20175
20176 slot = htab_find_slot_with_hash (cu->die_hash, die,
20177 to_underlying (die->sect_off),
20178 INSERT);
20179
20180 *slot = die;
20181 }
20182
20183 /* Follow reference or signature attribute ATTR of SRC_DIE.
20184 On entry *REF_CU is the CU of SRC_DIE.
20185 On exit *REF_CU is the CU of the result. */
20186
20187 static struct die_info *
20188 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20189 struct dwarf2_cu **ref_cu)
20190 {
20191 struct die_info *die;
20192
20193 if (attr->form_is_ref ())
20194 die = follow_die_ref (src_die, attr, ref_cu);
20195 else if (attr->form == DW_FORM_ref_sig8)
20196 die = follow_die_sig (src_die, attr, ref_cu);
20197 else
20198 {
20199 src_die->error_dump ();
20200 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20201 objfile_name ((*ref_cu)->per_objfile->objfile));
20202 }
20203
20204 return die;
20205 }
20206
20207 /* Follow reference OFFSET.
20208 On entry *REF_CU is the CU of the source die referencing OFFSET.
20209 On exit *REF_CU is the CU of the result.
20210 Returns NULL if OFFSET is invalid. */
20211
20212 static struct die_info *
20213 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20214 struct dwarf2_cu **ref_cu)
20215 {
20216 struct die_info temp_die;
20217 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20218 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20219
20220 gdb_assert (cu->per_cu != NULL);
20221
20222 target_cu = cu;
20223
20224 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
20225 "source CU contains target offset: %d",
20226 sect_offset_str (cu->per_cu->sect_off),
20227 sect_offset_str (sect_off),
20228 cu->header.offset_in_cu_p (sect_off));
20229
20230 if (cu->per_cu->is_debug_types)
20231 {
20232 /* .debug_types CUs cannot reference anything outside their CU.
20233 If they need to, they have to reference a signatured type via
20234 DW_FORM_ref_sig8. */
20235 if (!cu->header.offset_in_cu_p (sect_off))
20236 return NULL;
20237 }
20238 else if (offset_in_dwz != cu->per_cu->is_dwz
20239 || !cu->header.offset_in_cu_p (sect_off))
20240 {
20241 struct dwarf2_per_cu_data *per_cu;
20242
20243 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20244 per_objfile->per_bfd);
20245
20246 dwarf_read_debug_printf_v ("target CU offset: %s, "
20247 "target CU DIEs loaded: %d",
20248 sect_offset_str (per_cu->sect_off),
20249 per_objfile->get_cu (per_cu) != nullptr);
20250
20251 /* If necessary, add it to the queue and load its DIEs.
20252
20253 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20254 it doesn't mean they are currently loaded. Since we require them
20255 to be loaded, we must check for ourselves. */
20256 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
20257 || per_objfile->get_cu (per_cu) == nullptr)
20258 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
20259 false, cu->lang ());
20260
20261 target_cu = per_objfile->get_cu (per_cu);
20262 gdb_assert (target_cu != nullptr);
20263 }
20264 else if (cu->dies == NULL)
20265 {
20266 /* We're loading full DIEs during partial symbol reading. */
20267 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
20268 language_minimal);
20269 }
20270
20271 *ref_cu = target_cu;
20272 temp_die.sect_off = sect_off;
20273
20274 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20275 &temp_die,
20276 to_underlying (sect_off));
20277 }
20278
20279 /* Follow reference attribute ATTR of SRC_DIE.
20280 On entry *REF_CU is the CU of SRC_DIE.
20281 On exit *REF_CU is the CU of the result. */
20282
20283 static struct die_info *
20284 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20285 struct dwarf2_cu **ref_cu)
20286 {
20287 sect_offset sect_off = attr->get_ref_die_offset ();
20288 struct dwarf2_cu *cu = *ref_cu;
20289 struct die_info *die;
20290
20291 die = follow_die_offset (sect_off,
20292 (attr->form == DW_FORM_GNU_ref_alt
20293 || cu->per_cu->is_dwz),
20294 ref_cu);
20295 if (!die)
20296 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
20297 "at %s [in module %s]"),
20298 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
20299 objfile_name (cu->per_objfile->objfile));
20300
20301 return die;
20302 }
20303
20304 /* See read.h. */
20305
20306 struct dwarf2_locexpr_baton
20307 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20308 dwarf2_per_cu_data *per_cu,
20309 dwarf2_per_objfile *per_objfile,
20310 gdb::function_view<CORE_ADDR ()> get_frame_pc,
20311 bool resolve_abstract_p)
20312 {
20313 struct die_info *die;
20314 struct attribute *attr;
20315 struct dwarf2_locexpr_baton retval;
20316 struct objfile *objfile = per_objfile->objfile;
20317
20318 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20319 if (cu == nullptr)
20320 cu = load_cu (per_cu, per_objfile, false);
20321
20322 if (cu == nullptr)
20323 {
20324 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20325 Instead just throw an error, not much else we can do. */
20326 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20327 sect_offset_str (sect_off), objfile_name (objfile));
20328 }
20329
20330 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20331 if (!die)
20332 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20333 sect_offset_str (sect_off), objfile_name (objfile));
20334
20335 attr = dwarf2_attr (die, DW_AT_location, cu);
20336 if (!attr && resolve_abstract_p
20337 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
20338 != per_objfile->per_bfd->abstract_to_concrete.end ()))
20339 {
20340 CORE_ADDR pc = get_frame_pc ();
20341 CORE_ADDR baseaddr = objfile->text_section_offset ();
20342 struct gdbarch *gdbarch = objfile->arch ();
20343
20344 for (const auto &cand_off
20345 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
20346 {
20347 struct dwarf2_cu *cand_cu = cu;
20348 struct die_info *cand
20349 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
20350 if (!cand
20351 || !cand->parent
20352 || cand->parent->tag != DW_TAG_subprogram)
20353 continue;
20354
20355 CORE_ADDR pc_low, pc_high;
20356 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
20357 if (pc_low == ((CORE_ADDR) -1))
20358 continue;
20359 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
20360 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
20361 if (!(pc_low <= pc && pc < pc_high))
20362 continue;
20363
20364 die = cand;
20365 attr = dwarf2_attr (die, DW_AT_location, cu);
20366 break;
20367 }
20368 }
20369
20370 if (!attr)
20371 {
20372 /* DWARF: "If there is no such attribute, then there is no effect.".
20373 DATA is ignored if SIZE is 0. */
20374
20375 retval.data = NULL;
20376 retval.size = 0;
20377 }
20378 else if (attr->form_is_section_offset ())
20379 {
20380 struct dwarf2_loclist_baton loclist_baton;
20381 CORE_ADDR pc = get_frame_pc ();
20382 size_t size;
20383
20384 fill_in_loclist_baton (cu, &loclist_baton, attr);
20385
20386 retval.data = dwarf2_find_location_expression (&loclist_baton,
20387 &size, pc);
20388 retval.size = size;
20389 }
20390 else
20391 {
20392 if (!attr->form_is_block ())
20393 error (_("Dwarf Error: DIE at %s referenced in module %s "
20394 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20395 sect_offset_str (sect_off), objfile_name (objfile));
20396
20397 struct dwarf_block *block = attr->as_block ();
20398 retval.data = block->data;
20399 retval.size = block->size;
20400 }
20401 retval.per_objfile = per_objfile;
20402 retval.per_cu = cu->per_cu;
20403
20404 per_objfile->age_comp_units ();
20405
20406 return retval;
20407 }
20408
20409 /* See read.h. */
20410
20411 struct dwarf2_locexpr_baton
20412 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20413 dwarf2_per_cu_data *per_cu,
20414 dwarf2_per_objfile *per_objfile,
20415 gdb::function_view<CORE_ADDR ()> get_frame_pc)
20416 {
20417 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20418
20419 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
20420 get_frame_pc);
20421 }
20422
20423 /* Write a constant of a given type as target-ordered bytes into
20424 OBSTACK. */
20425
20426 static const gdb_byte *
20427 write_constant_as_bytes (struct obstack *obstack,
20428 enum bfd_endian byte_order,
20429 struct type *type,
20430 ULONGEST value,
20431 LONGEST *len)
20432 {
20433 gdb_byte *result;
20434
20435 *len = type->length ();
20436 result = (gdb_byte *) obstack_alloc (obstack, *len);
20437 store_unsigned_integer (result, *len, byte_order, value);
20438
20439 return result;
20440 }
20441
20442 /* See read.h. */
20443
20444 const gdb_byte *
20445 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20446 dwarf2_per_cu_data *per_cu,
20447 dwarf2_per_objfile *per_objfile,
20448 obstack *obstack,
20449 LONGEST *len)
20450 {
20451 struct die_info *die;
20452 struct attribute *attr;
20453 const gdb_byte *result = NULL;
20454 struct type *type;
20455 LONGEST value;
20456 enum bfd_endian byte_order;
20457 struct objfile *objfile = per_objfile->objfile;
20458
20459 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20460 if (cu == nullptr)
20461 cu = load_cu (per_cu, per_objfile, false);
20462
20463 if (cu == nullptr)
20464 {
20465 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20466 Instead just throw an error, not much else we can do. */
20467 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20468 sect_offset_str (sect_off), objfile_name (objfile));
20469 }
20470
20471 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20472 if (!die)
20473 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20474 sect_offset_str (sect_off), objfile_name (objfile));
20475
20476 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20477 if (attr == NULL)
20478 return NULL;
20479
20480 byte_order = (bfd_big_endian (objfile->obfd.get ())
20481 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20482
20483 switch (attr->form)
20484 {
20485 case DW_FORM_addr:
20486 case DW_FORM_addrx:
20487 case DW_FORM_GNU_addr_index:
20488 {
20489 gdb_byte *tem;
20490
20491 *len = cu->header.addr_size;
20492 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20493 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
20494 result = tem;
20495 }
20496 break;
20497 case DW_FORM_string:
20498 case DW_FORM_strp:
20499 case DW_FORM_strx:
20500 case DW_FORM_GNU_str_index:
20501 case DW_FORM_GNU_strp_alt:
20502 /* The string is already allocated on the objfile obstack, point
20503 directly to it. */
20504 {
20505 const char *attr_name = attr->as_string ();
20506 result = (const gdb_byte *) attr_name;
20507 *len = strlen (attr_name);
20508 }
20509 break;
20510 case DW_FORM_block1:
20511 case DW_FORM_block2:
20512 case DW_FORM_block4:
20513 case DW_FORM_block:
20514 case DW_FORM_exprloc:
20515 case DW_FORM_data16:
20516 {
20517 struct dwarf_block *block = attr->as_block ();
20518 result = block->data;
20519 *len = block->size;
20520 }
20521 break;
20522
20523 /* The DW_AT_const_value attributes are supposed to carry the
20524 symbol's value "represented as it would be on the target
20525 architecture." By the time we get here, it's already been
20526 converted to host endianness, so we just need to sign- or
20527 zero-extend it as appropriate. */
20528 case DW_FORM_data1:
20529 type = die_type (die, cu);
20530 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20531 if (result == NULL)
20532 result = write_constant_as_bytes (obstack, byte_order,
20533 type, value, len);
20534 break;
20535 case DW_FORM_data2:
20536 type = die_type (die, cu);
20537 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20538 if (result == NULL)
20539 result = write_constant_as_bytes (obstack, byte_order,
20540 type, value, len);
20541 break;
20542 case DW_FORM_data4:
20543 type = die_type (die, cu);
20544 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20545 if (result == NULL)
20546 result = write_constant_as_bytes (obstack, byte_order,
20547 type, value, len);
20548 break;
20549 case DW_FORM_data8:
20550 type = die_type (die, cu);
20551 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20552 if (result == NULL)
20553 result = write_constant_as_bytes (obstack, byte_order,
20554 type, value, len);
20555 break;
20556
20557 case DW_FORM_sdata:
20558 case DW_FORM_implicit_const:
20559 type = die_type (die, cu);
20560 result = write_constant_as_bytes (obstack, byte_order,
20561 type, attr->as_signed (), len);
20562 break;
20563
20564 case DW_FORM_udata:
20565 type = die_type (die, cu);
20566 result = write_constant_as_bytes (obstack, byte_order,
20567 type, attr->as_unsigned (), len);
20568 break;
20569
20570 default:
20571 complaint (_("unsupported const value attribute form: '%s'"),
20572 dwarf_form_name (attr->form));
20573 break;
20574 }
20575
20576 return result;
20577 }
20578
20579 /* See read.h. */
20580
20581 struct type *
20582 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20583 dwarf2_per_cu_data *per_cu,
20584 dwarf2_per_objfile *per_objfile,
20585 const char **var_name)
20586 {
20587 struct die_info *die;
20588
20589 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20590 if (cu == nullptr)
20591 cu = load_cu (per_cu, per_objfile, false);
20592
20593 if (cu == nullptr)
20594 return nullptr;
20595
20596 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20597 if (!die)
20598 return NULL;
20599
20600 if (var_name != nullptr)
20601 *var_name = var_decl_name (die, cu);
20602 return die_type (die, cu);
20603 }
20604
20605 /* See read.h. */
20606
20607 struct type *
20608 dwarf2_get_die_type (cu_offset die_offset,
20609 dwarf2_per_cu_data *per_cu,
20610 dwarf2_per_objfile *per_objfile)
20611 {
20612 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20613 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
20614 }
20615
20616 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20617 On entry *REF_CU is the CU of SRC_DIE.
20618 On exit *REF_CU is the CU of the result.
20619 Returns NULL if the referenced DIE isn't found. */
20620
20621 static struct die_info *
20622 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20623 struct dwarf2_cu **ref_cu)
20624 {
20625 struct die_info temp_die;
20626 struct dwarf2_cu *sig_cu;
20627 struct die_info *die;
20628 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
20629
20630
20631 /* While it might be nice to assert sig_type->type == NULL here,
20632 we can get here for DW_AT_imported_declaration where we need
20633 the DIE not the type. */
20634
20635 /* If necessary, add it to the queue and load its DIEs.
20636
20637 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20638 it doesn't mean they are currently loaded. Since we require them
20639 to be loaded, we must check for ourselves. */
20640 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
20641 language_minimal)
20642 || per_objfile->get_cu (sig_type) == nullptr)
20643 read_signatured_type (sig_type, per_objfile);
20644
20645 sig_cu = per_objfile->get_cu (sig_type);
20646 gdb_assert (sig_cu != NULL);
20647 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20648 temp_die.sect_off = sig_type->type_offset_in_section;
20649 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20650 to_underlying (temp_die.sect_off));
20651 if (die)
20652 {
20653 /* For .gdb_index version 7 keep track of included TUs.
20654 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20655 if (per_objfile->per_bfd->index_table != NULL
20656 && !per_objfile->per_bfd->index_table->version_check ())
20657 {
20658 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
20659 }
20660
20661 *ref_cu = sig_cu;
20662 return die;
20663 }
20664
20665 return NULL;
20666 }
20667
20668 /* Follow signatured type referenced by ATTR in SRC_DIE.
20669 On entry *REF_CU is the CU of SRC_DIE.
20670 On exit *REF_CU is the CU of the result.
20671 The result is the DIE of the type.
20672 If the referenced type cannot be found an error is thrown. */
20673
20674 static struct die_info *
20675 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20676 struct dwarf2_cu **ref_cu)
20677 {
20678 ULONGEST signature = attr->as_signature ();
20679 struct signatured_type *sig_type;
20680 struct die_info *die;
20681
20682 gdb_assert (attr->form == DW_FORM_ref_sig8);
20683
20684 sig_type = lookup_signatured_type (*ref_cu, signature);
20685 /* sig_type will be NULL if the signatured type is missing from
20686 the debug info. */
20687 if (sig_type == NULL)
20688 {
20689 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20690 " from DIE at %s [in module %s]"),
20691 hex_string (signature), sect_offset_str (src_die->sect_off),
20692 objfile_name ((*ref_cu)->per_objfile->objfile));
20693 }
20694
20695 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20696 if (die == NULL)
20697 {
20698 src_die->error_dump ();
20699 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20700 " from DIE at %s [in module %s]"),
20701 hex_string (signature), sect_offset_str (src_die->sect_off),
20702 objfile_name ((*ref_cu)->per_objfile->objfile));
20703 }
20704
20705 return die;
20706 }
20707
20708 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20709 reading in and processing the type unit if necessary. */
20710
20711 static struct type *
20712 get_signatured_type (struct die_info *die, ULONGEST signature,
20713 struct dwarf2_cu *cu)
20714 {
20715 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20716 struct signatured_type *sig_type;
20717 struct dwarf2_cu *type_cu;
20718 struct die_info *type_die;
20719 struct type *type;
20720
20721 sig_type = lookup_signatured_type (cu, signature);
20722 /* sig_type will be NULL if the signatured type is missing from
20723 the debug info. */
20724 if (sig_type == NULL)
20725 {
20726 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20727 " from DIE at %s [in module %s]"),
20728 hex_string (signature), sect_offset_str (die->sect_off),
20729 objfile_name (per_objfile->objfile));
20730 return build_error_marker_type (cu, die);
20731 }
20732
20733 /* If we already know the type we're done. */
20734 type = per_objfile->get_type_for_signatured_type (sig_type);
20735 if (type != nullptr)
20736 return type;
20737
20738 type_cu = cu;
20739 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20740 if (type_die != NULL)
20741 {
20742 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20743 is created. This is important, for example, because for c++ classes
20744 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20745 type = read_type_die (type_die, type_cu);
20746 if (type == NULL)
20747 {
20748 complaint (_("Dwarf Error: Cannot build signatured type %s"
20749 " referenced from DIE at %s [in module %s]"),
20750 hex_string (signature), sect_offset_str (die->sect_off),
20751 objfile_name (per_objfile->objfile));
20752 type = build_error_marker_type (cu, die);
20753 }
20754 }
20755 else
20756 {
20757 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20758 " from DIE at %s [in module %s]"),
20759 hex_string (signature), sect_offset_str (die->sect_off),
20760 objfile_name (per_objfile->objfile));
20761 type = build_error_marker_type (cu, die);
20762 }
20763
20764 per_objfile->set_type_for_signatured_type (sig_type, type);
20765
20766 return type;
20767 }
20768
20769 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20770 reading in and processing the type unit if necessary. */
20771
20772 static struct type *
20773 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20774 struct dwarf2_cu *cu) /* ARI: editCase function */
20775 {
20776 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20777 if (attr->form_is_ref ())
20778 {
20779 struct dwarf2_cu *type_cu = cu;
20780 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20781
20782 return read_type_die (type_die, type_cu);
20783 }
20784 else if (attr->form == DW_FORM_ref_sig8)
20785 {
20786 return get_signatured_type (die, attr->as_signature (), cu);
20787 }
20788 else
20789 {
20790 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20791
20792 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20793 " at %s [in module %s]"),
20794 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
20795 objfile_name (per_objfile->objfile));
20796 return build_error_marker_type (cu, die);
20797 }
20798 }
20799
20800 /* Load the DIEs associated with type unit PER_CU into memory. */
20801
20802 static void
20803 load_full_type_unit (dwarf2_per_cu_data *per_cu,
20804 dwarf2_per_objfile *per_objfile)
20805 {
20806 struct signatured_type *sig_type;
20807
20808 /* We have the per_cu, but we need the signatured_type.
20809 Fortunately this is an easy translation. */
20810 gdb_assert (per_cu->is_debug_types);
20811 sig_type = (struct signatured_type *) per_cu;
20812
20813 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
20814
20815 read_signatured_type (sig_type, per_objfile);
20816
20817 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
20818 }
20819
20820 /* Read in a signatured type and build its CU and DIEs.
20821 If the type is a stub for the real type in a DWO file,
20822 read in the real type from the DWO file as well. */
20823
20824 static void
20825 read_signatured_type (signatured_type *sig_type,
20826 dwarf2_per_objfile *per_objfile)
20827 {
20828 gdb_assert (sig_type->is_debug_types);
20829 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
20830
20831 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
20832
20833 if (!reader.dummy_p)
20834 {
20835 struct dwarf2_cu *cu = reader.cu;
20836 const gdb_byte *info_ptr = reader.info_ptr;
20837
20838 gdb_assert (cu->die_hash == NULL);
20839 cu->die_hash =
20840 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
20841 die_info::hash,
20842 die_info::eq,
20843 NULL,
20844 &cu->comp_unit_obstack,
20845 hashtab_obstack_allocate,
20846 dummy_obstack_deallocate);
20847
20848 if (reader.comp_unit_die->has_children)
20849 reader.comp_unit_die->child
20850 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
20851 reader.comp_unit_die);
20852 cu->dies = reader.comp_unit_die;
20853 /* comp_unit_die is not stored in die_hash, no need. */
20854
20855 /* We try not to read any attributes in this function, because
20856 not all CUs needed for references have been loaded yet, and
20857 symbol table processing isn't initialized. But we have to
20858 set the CU language, or we won't be able to build types
20859 correctly. Similarly, if we do not read the producer, we can
20860 not apply producer-specific interpretation. */
20861 prepare_one_comp_unit (cu, cu->dies, language_minimal);
20862
20863 reader.keep ();
20864 }
20865
20866 sig_type->tu_read = 1;
20867 }
20868
20869 /* Decode simple location descriptions.
20870 Given a pointer to a dwarf block that defines a location, compute
20871 the location and return the value. If COMPUTED is non-null, it is
20872 set to true to indicate that decoding was successful, and false
20873 otherwise. If COMPUTED is null, then this function may emit a
20874 complaint. */
20875
20876 static CORE_ADDR
20877 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
20878 {
20879 struct objfile *objfile = cu->per_objfile->objfile;
20880 size_t i;
20881 size_t size = blk->size;
20882 const gdb_byte *data = blk->data;
20883 CORE_ADDR stack[64];
20884 int stacki;
20885 unsigned int bytes_read, unsnd;
20886 gdb_byte op;
20887
20888 if (computed != nullptr)
20889 *computed = false;
20890
20891 i = 0;
20892 stacki = 0;
20893 stack[stacki] = 0;
20894 stack[++stacki] = 0;
20895
20896 while (i < size)
20897 {
20898 op = data[i++];
20899 switch (op)
20900 {
20901 case DW_OP_lit0:
20902 case DW_OP_lit1:
20903 case DW_OP_lit2:
20904 case DW_OP_lit3:
20905 case DW_OP_lit4:
20906 case DW_OP_lit5:
20907 case DW_OP_lit6:
20908 case DW_OP_lit7:
20909 case DW_OP_lit8:
20910 case DW_OP_lit9:
20911 case DW_OP_lit10:
20912 case DW_OP_lit11:
20913 case DW_OP_lit12:
20914 case DW_OP_lit13:
20915 case DW_OP_lit14:
20916 case DW_OP_lit15:
20917 case DW_OP_lit16:
20918 case DW_OP_lit17:
20919 case DW_OP_lit18:
20920 case DW_OP_lit19:
20921 case DW_OP_lit20:
20922 case DW_OP_lit21:
20923 case DW_OP_lit22:
20924 case DW_OP_lit23:
20925 case DW_OP_lit24:
20926 case DW_OP_lit25:
20927 case DW_OP_lit26:
20928 case DW_OP_lit27:
20929 case DW_OP_lit28:
20930 case DW_OP_lit29:
20931 case DW_OP_lit30:
20932 case DW_OP_lit31:
20933 stack[++stacki] = op - DW_OP_lit0;
20934 break;
20935
20936 case DW_OP_reg0:
20937 case DW_OP_reg1:
20938 case DW_OP_reg2:
20939 case DW_OP_reg3:
20940 case DW_OP_reg4:
20941 case DW_OP_reg5:
20942 case DW_OP_reg6:
20943 case DW_OP_reg7:
20944 case DW_OP_reg8:
20945 case DW_OP_reg9:
20946 case DW_OP_reg10:
20947 case DW_OP_reg11:
20948 case DW_OP_reg12:
20949 case DW_OP_reg13:
20950 case DW_OP_reg14:
20951 case DW_OP_reg15:
20952 case DW_OP_reg16:
20953 case DW_OP_reg17:
20954 case DW_OP_reg18:
20955 case DW_OP_reg19:
20956 case DW_OP_reg20:
20957 case DW_OP_reg21:
20958 case DW_OP_reg22:
20959 case DW_OP_reg23:
20960 case DW_OP_reg24:
20961 case DW_OP_reg25:
20962 case DW_OP_reg26:
20963 case DW_OP_reg27:
20964 case DW_OP_reg28:
20965 case DW_OP_reg29:
20966 case DW_OP_reg30:
20967 case DW_OP_reg31:
20968 stack[++stacki] = op - DW_OP_reg0;
20969 if (i < size)
20970 {
20971 if (computed == nullptr)
20972 dwarf2_complex_location_expr_complaint ();
20973 else
20974 return 0;
20975 }
20976 break;
20977
20978 case DW_OP_regx:
20979 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
20980 i += bytes_read;
20981 stack[++stacki] = unsnd;
20982 if (i < size)
20983 {
20984 if (computed == nullptr)
20985 dwarf2_complex_location_expr_complaint ();
20986 else
20987 return 0;
20988 }
20989 break;
20990
20991 case DW_OP_addr:
20992 stack[++stacki] = cu->header.read_address (objfile->obfd.get (),
20993 &data[i],
20994 &bytes_read);
20995 i += bytes_read;
20996 break;
20997
20998 case DW_OP_const1u:
20999 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
21000 i += 1;
21001 break;
21002
21003 case DW_OP_const1s:
21004 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
21005 i += 1;
21006 break;
21007
21008 case DW_OP_const2u:
21009 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
21010 i += 2;
21011 break;
21012
21013 case DW_OP_const2s:
21014 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
21015 i += 2;
21016 break;
21017
21018 case DW_OP_const4u:
21019 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
21020 i += 4;
21021 break;
21022
21023 case DW_OP_const4s:
21024 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
21025 i += 4;
21026 break;
21027
21028 case DW_OP_const8u:
21029 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
21030 i += 8;
21031 break;
21032
21033 case DW_OP_constu:
21034 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21035 &bytes_read);
21036 i += bytes_read;
21037 break;
21038
21039 case DW_OP_consts:
21040 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21041 i += bytes_read;
21042 break;
21043
21044 case DW_OP_dup:
21045 stack[stacki + 1] = stack[stacki];
21046 stacki++;
21047 break;
21048
21049 case DW_OP_plus:
21050 stack[stacki - 1] += stack[stacki];
21051 stacki--;
21052 break;
21053
21054 case DW_OP_plus_uconst:
21055 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21056 &bytes_read);
21057 i += bytes_read;
21058 break;
21059
21060 case DW_OP_minus:
21061 stack[stacki - 1] -= stack[stacki];
21062 stacki--;
21063 break;
21064
21065 case DW_OP_deref:
21066 /* If we're not the last op, then we definitely can't encode
21067 this using GDB's address_class enum. This is valid for partial
21068 global symbols, although the variable's address will be bogus
21069 in the psymtab. */
21070 if (i < size)
21071 {
21072 if (computed == nullptr)
21073 dwarf2_complex_location_expr_complaint ();
21074 else
21075 return 0;
21076 }
21077 break;
21078
21079 case DW_OP_GNU_push_tls_address:
21080 case DW_OP_form_tls_address:
21081 /* The top of the stack has the offset from the beginning
21082 of the thread control block at which the variable is located. */
21083 /* Nothing should follow this operator, so the top of stack would
21084 be returned. */
21085 /* This is valid for partial global symbols, but the variable's
21086 address will be bogus in the psymtab. Make it always at least
21087 non-zero to not look as a variable garbage collected by linker
21088 which have DW_OP_addr 0. */
21089 if (i < size)
21090 {
21091 if (computed == nullptr)
21092 dwarf2_complex_location_expr_complaint ();
21093 else
21094 return 0;
21095 }
21096 stack[stacki]++;
21097 break;
21098
21099 case DW_OP_GNU_uninit:
21100 if (computed != nullptr)
21101 return 0;
21102 break;
21103
21104 case DW_OP_addrx:
21105 case DW_OP_GNU_addr_index:
21106 case DW_OP_GNU_const_index:
21107 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21108 &bytes_read);
21109 i += bytes_read;
21110 break;
21111
21112 default:
21113 if (computed == nullptr)
21114 {
21115 const char *name = get_DW_OP_name (op);
21116
21117 if (name)
21118 complaint (_("unsupported stack op: '%s'"),
21119 name);
21120 else
21121 complaint (_("unsupported stack op: '%02x'"),
21122 op);
21123 }
21124
21125 return (stack[stacki]);
21126 }
21127
21128 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21129 outside of the allocated space. Also enforce minimum>0. */
21130 if (stacki >= ARRAY_SIZE (stack) - 1)
21131 {
21132 if (computed == nullptr)
21133 complaint (_("location description stack overflow"));
21134 return 0;
21135 }
21136
21137 if (stacki <= 0)
21138 {
21139 if (computed == nullptr)
21140 complaint (_("location description stack underflow"));
21141 return 0;
21142 }
21143 }
21144
21145 if (computed != nullptr)
21146 *computed = true;
21147 return (stack[stacki]);
21148 }
21149
21150 /* memory allocation interface */
21151
21152 static struct dwarf_block *
21153 dwarf_alloc_block (struct dwarf2_cu *cu)
21154 {
21155 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21156 }
21157
21158 \f
21159
21160 /* Macro support. */
21161
21162 /* An overload of dwarf_decode_macros that finds the correct section
21163 and ensures it is read in before calling the other overload. */
21164
21165 static void
21166 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
21167 int section_is_gnu)
21168 {
21169 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21170 struct objfile *objfile = per_objfile->objfile;
21171 const struct line_header *lh = cu->line_header;
21172 unsigned int offset_size = cu->header.offset_size;
21173 struct dwarf2_section_info *section;
21174 const char *section_name;
21175
21176 if (cu->dwo_unit != nullptr)
21177 {
21178 if (section_is_gnu)
21179 {
21180 section = &cu->dwo_unit->dwo_file->sections.macro;
21181 section_name = ".debug_macro.dwo";
21182 }
21183 else
21184 {
21185 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21186 section_name = ".debug_macinfo.dwo";
21187 }
21188 }
21189 else
21190 {
21191 if (section_is_gnu)
21192 {
21193 section = &per_objfile->per_bfd->macro;
21194 section_name = ".debug_macro";
21195 }
21196 else
21197 {
21198 section = &per_objfile->per_bfd->macinfo;
21199 section_name = ".debug_macinfo";
21200 }
21201 }
21202
21203 section->read (objfile);
21204 if (section->buffer == nullptr)
21205 {
21206 complaint (_("missing %s section"), section_name);
21207 return;
21208 }
21209
21210 buildsym_compunit *builder = cu->get_builder ();
21211
21212 struct dwarf2_section_info *str_offsets_section;
21213 struct dwarf2_section_info *str_section;
21214 gdb::optional<ULONGEST> str_offsets_base;
21215
21216 if (cu->dwo_unit != nullptr)
21217 {
21218 str_offsets_section = &cu->dwo_unit->dwo_file
21219 ->sections.str_offsets;
21220 str_section = &cu->dwo_unit->dwo_file->sections.str;
21221 str_offsets_base = cu->header.addr_size;
21222 }
21223 else
21224 {
21225 str_offsets_section = &per_objfile->per_bfd->str_offsets;
21226 str_section = &per_objfile->per_bfd->str;
21227 str_offsets_base = cu->str_offsets_base;
21228 }
21229
21230 dwarf_decode_macros (per_objfile, builder, section, lh,
21231 offset_size, offset, str_section, str_offsets_section,
21232 str_offsets_base, section_is_gnu, cu);
21233 }
21234
21235 /* Return the .debug_loc section to use for CU.
21236 For DWO files use .debug_loc.dwo. */
21237
21238 static struct dwarf2_section_info *
21239 cu_debug_loc_section (struct dwarf2_cu *cu)
21240 {
21241 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21242
21243 if (cu->dwo_unit)
21244 {
21245 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21246
21247 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
21248 }
21249 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
21250 : &per_objfile->per_bfd->loc);
21251 }
21252
21253 /* Return the .debug_rnglists section to use for CU. */
21254 static struct dwarf2_section_info *
21255 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
21256 {
21257 if (cu->header.version < 5)
21258 error (_(".debug_rnglists section cannot be used in DWARF %d"),
21259 cu->header.version);
21260 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21261
21262 /* Make sure we read the .debug_rnglists section from the file that
21263 contains the DW_AT_ranges attribute we are reading. Normally that
21264 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
21265 or DW_TAG_skeleton unit, we always want to read from objfile/linked
21266 program. */
21267 if (cu->dwo_unit != nullptr
21268 && tag != DW_TAG_compile_unit
21269 && tag != DW_TAG_skeleton_unit)
21270 {
21271 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21272
21273 if (sections->rnglists.size > 0)
21274 return &sections->rnglists;
21275 else
21276 error (_(".debug_rnglists section is missing from .dwo file."));
21277 }
21278 return &dwarf2_per_objfile->per_bfd->rnglists;
21279 }
21280
21281 /* A helper function that fills in a dwarf2_loclist_baton. */
21282
21283 static void
21284 fill_in_loclist_baton (struct dwarf2_cu *cu,
21285 struct dwarf2_loclist_baton *baton,
21286 const struct attribute *attr)
21287 {
21288 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21289 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21290
21291 section->read (per_objfile->objfile);
21292
21293 baton->per_objfile = per_objfile;
21294 baton->per_cu = cu->per_cu;
21295 gdb_assert (baton->per_cu);
21296 /* We don't know how long the location list is, but make sure we
21297 don't run off the edge of the section. */
21298 baton->size = section->size - attr->as_unsigned ();
21299 baton->data = section->buffer + attr->as_unsigned ();
21300 if (cu->base_address.has_value ())
21301 baton->base_address = *cu->base_address;
21302 else
21303 baton->base_address = 0;
21304 baton->from_dwo = cu->dwo_unit != NULL;
21305 }
21306
21307 static void
21308 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
21309 struct dwarf2_cu *cu, int is_block)
21310 {
21311 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21312 struct objfile *objfile = per_objfile->objfile;
21313 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21314
21315 if (attr->form_is_section_offset ()
21316 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21317 the section. If so, fall through to the complaint in the
21318 other branch. */
21319 && attr->as_unsigned () < section->get_size (objfile))
21320 {
21321 struct dwarf2_loclist_baton *baton;
21322
21323 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
21324
21325 fill_in_loclist_baton (cu, baton, attr);
21326
21327 if (!cu->base_address.has_value ())
21328 complaint (_("Location list used without "
21329 "specifying the CU base address."));
21330
21331 sym->set_aclass_index ((is_block
21332 ? dwarf2_loclist_block_index
21333 : dwarf2_loclist_index));
21334 SYMBOL_LOCATION_BATON (sym) = baton;
21335 }
21336 else
21337 {
21338 struct dwarf2_locexpr_baton *baton;
21339
21340 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
21341 baton->per_objfile = per_objfile;
21342 baton->per_cu = cu->per_cu;
21343 gdb_assert (baton->per_cu);
21344
21345 if (attr->form_is_block ())
21346 {
21347 /* Note that we're just copying the block's data pointer
21348 here, not the actual data. We're still pointing into the
21349 info_buffer for SYM's objfile; right now we never release
21350 that buffer, but when we do clean up properly this may
21351 need to change. */
21352 struct dwarf_block *block = attr->as_block ();
21353 baton->size = block->size;
21354 baton->data = block->data;
21355 }
21356 else
21357 {
21358 dwarf2_invalid_attrib_class_complaint ("location description",
21359 sym->natural_name ());
21360 baton->size = 0;
21361 }
21362
21363 sym->set_aclass_index ((is_block
21364 ? dwarf2_locexpr_block_index
21365 : dwarf2_locexpr_index));
21366 SYMBOL_LOCATION_BATON (sym) = baton;
21367 }
21368 }
21369
21370 /* See read.h. */
21371
21372 const comp_unit_head *
21373 dwarf2_per_cu_data::get_header () const
21374 {
21375 if (!m_header_read_in)
21376 {
21377 const gdb_byte *info_ptr
21378 = this->section->buffer + to_underlying (this->sect_off);
21379
21380 read_comp_unit_head (&m_header, info_ptr, this->section,
21381 rcuh_kind::COMPILE);
21382
21383 m_header_read_in = true;
21384 }
21385
21386 return &m_header;
21387 }
21388
21389 /* See read.h. */
21390
21391 int
21392 dwarf2_per_cu_data::addr_size () const
21393 {
21394 return this->get_header ()->addr_size;
21395 }
21396
21397 /* See read.h. */
21398
21399 int
21400 dwarf2_per_cu_data::offset_size () const
21401 {
21402 return this->get_header ()->offset_size;
21403 }
21404
21405 /* See read.h. */
21406
21407 int
21408 dwarf2_per_cu_data::ref_addr_size () const
21409 {
21410 const comp_unit_head *header = this->get_header ();
21411
21412 if (header->version == 2)
21413 return header->addr_size;
21414 else
21415 return header->offset_size;
21416 }
21417
21418 /* A helper function for dwarf2_find_containing_comp_unit that returns
21419 the index of the result, and that searches a vector. It will
21420 return a result even if the offset in question does not actually
21421 occur in any CU. This is separate so that it can be unit
21422 tested. */
21423
21424 static int
21425 dwarf2_find_containing_comp_unit
21426 (sect_offset sect_off,
21427 unsigned int offset_in_dwz,
21428 const std::vector<dwarf2_per_cu_data_up> &all_units)
21429 {
21430 int low, high;
21431
21432 low = 0;
21433 high = all_units.size () - 1;
21434 while (high > low)
21435 {
21436 struct dwarf2_per_cu_data *mid_cu;
21437 int mid = low + (high - low) / 2;
21438
21439 mid_cu = all_units[mid].get ();
21440 if (mid_cu->is_dwz > offset_in_dwz
21441 || (mid_cu->is_dwz == offset_in_dwz
21442 && mid_cu->sect_off + mid_cu->length () > sect_off))
21443 high = mid;
21444 else
21445 low = mid + 1;
21446 }
21447 gdb_assert (low == high);
21448 return low;
21449 }
21450
21451 /* Locate the .debug_info compilation unit from CU's objfile which contains
21452 the DIE at OFFSET. Raises an error on failure. */
21453
21454 static struct dwarf2_per_cu_data *
21455 dwarf2_find_containing_comp_unit (sect_offset sect_off,
21456 unsigned int offset_in_dwz,
21457 dwarf2_per_bfd *per_bfd)
21458 {
21459 int low = dwarf2_find_containing_comp_unit
21460 (sect_off, offset_in_dwz, per_bfd->all_units);
21461 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
21462
21463 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
21464 {
21465 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
21466 error (_("Dwarf Error: could not find partial DIE containing "
21467 "offset %s [in module %s]"),
21468 sect_offset_str (sect_off),
21469 bfd_get_filename (per_bfd->obfd));
21470
21471 gdb_assert (per_bfd->all_units[low-1]->sect_off
21472 <= sect_off);
21473 return per_bfd->all_units[low - 1].get ();
21474 }
21475 else
21476 {
21477 if (low == per_bfd->all_units.size () - 1
21478 && sect_off >= this_cu->sect_off + this_cu->length ())
21479 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
21480 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
21481 return this_cu;
21482 }
21483 }
21484
21485 #if GDB_SELF_TEST
21486
21487 namespace selftests {
21488 namespace find_containing_comp_unit {
21489
21490 static void
21491 run_test ()
21492 {
21493 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
21494 dwarf2_per_cu_data *one_ptr = one.get ();
21495 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
21496 dwarf2_per_cu_data *two_ptr = two.get ();
21497 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
21498 dwarf2_per_cu_data *three_ptr = three.get ();
21499 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
21500 dwarf2_per_cu_data *four_ptr = four.get ();
21501
21502 one->set_length (5);
21503 two->sect_off = sect_offset (one->length ());
21504 two->set_length (7);
21505
21506 three->set_length (5);
21507 three->is_dwz = 1;
21508 four->sect_off = sect_offset (three->length ());
21509 four->set_length (7);
21510 four->is_dwz = 1;
21511
21512 std::vector<dwarf2_per_cu_data_up> units;
21513 units.push_back (std::move (one));
21514 units.push_back (std::move (two));
21515 units.push_back (std::move (three));
21516 units.push_back (std::move (four));
21517
21518 int result;
21519
21520 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
21521 SELF_CHECK (units[result].get () == one_ptr);
21522 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
21523 SELF_CHECK (units[result].get () == one_ptr);
21524 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
21525 SELF_CHECK (units[result].get () == two_ptr);
21526
21527 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
21528 SELF_CHECK (units[result].get () == three_ptr);
21529 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
21530 SELF_CHECK (units[result].get () == three_ptr);
21531 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
21532 SELF_CHECK (units[result].get () == four_ptr);
21533 }
21534
21535 }
21536 }
21537
21538 #endif /* GDB_SELF_TEST */
21539
21540 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
21541
21542 static void
21543 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
21544 enum language pretend_language)
21545 {
21546 struct attribute *attr;
21547
21548 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
21549
21550 /* Set the language we're debugging. */
21551 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
21552 enum language lang;
21553 if (cu->producer != nullptr
21554 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
21555 {
21556 /* The XLCL doesn't generate DW_LANG_OpenCL because this
21557 attribute is not standardised yet. As a workaround for the
21558 language detection we fall back to the DW_AT_producer
21559 string. */
21560 lang = language_opencl;
21561 }
21562 else if (cu->producer != nullptr
21563 && strstr (cu->producer, "GNU Go ") != NULL)
21564 {
21565 /* Similar hack for Go. */
21566 lang = language_go;
21567 }
21568 else if (attr != nullptr)
21569 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
21570 else
21571 lang = pretend_language;
21572
21573 cu->language_defn = language_def (lang);
21574
21575 switch (comp_unit_die->tag)
21576 {
21577 case DW_TAG_compile_unit:
21578 cu->per_cu->set_unit_type (DW_UT_compile);
21579 break;
21580 case DW_TAG_partial_unit:
21581 cu->per_cu->set_unit_type (DW_UT_partial);
21582 break;
21583 case DW_TAG_type_unit:
21584 cu->per_cu->set_unit_type (DW_UT_type);
21585 break;
21586 default:
21587 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
21588 dwarf_tag_name (comp_unit_die->tag),
21589 sect_offset_str (cu->per_cu->sect_off));
21590 }
21591
21592 cu->per_cu->set_lang (lang);
21593 }
21594
21595 /* See read.h. */
21596
21597 dwarf2_cu *
21598 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
21599 {
21600 auto it = m_dwarf2_cus.find (per_cu);
21601 if (it == m_dwarf2_cus.end ())
21602 return nullptr;
21603
21604 return it->second.get ();
21605 }
21606
21607 /* See read.h. */
21608
21609 void
21610 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
21611 std::unique_ptr<dwarf2_cu> cu)
21612 {
21613 gdb_assert (this->get_cu (per_cu) == nullptr);
21614
21615 m_dwarf2_cus[per_cu] = std::move (cu);
21616 }
21617
21618 /* See read.h. */
21619
21620 void
21621 dwarf2_per_objfile::age_comp_units ()
21622 {
21623 dwarf_read_debug_printf_v ("running");
21624
21625 /* This is not expected to be called in the middle of CU expansion. There is
21626 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
21627 loaded in memory. Calling age_comp_units while the queue is in use could
21628 make us free the DIEs for a CU that is in the queue and therefore break
21629 that invariant. */
21630 gdb_assert (!queue.has_value ());
21631
21632 /* Start by clearing all marks. */
21633 for (const auto &pair : m_dwarf2_cus)
21634 pair.second->clear_mark ();
21635
21636 /* Traverse all CUs, mark them and their dependencies if used recently
21637 enough. */
21638 for (const auto &pair : m_dwarf2_cus)
21639 {
21640 dwarf2_cu *cu = pair.second.get ();
21641
21642 cu->last_used++;
21643 if (cu->last_used <= dwarf_max_cache_age)
21644 cu->mark ();
21645 }
21646
21647 /* Delete all CUs still not marked. */
21648 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
21649 {
21650 dwarf2_cu *cu = it->second.get ();
21651
21652 if (!cu->is_marked ())
21653 {
21654 dwarf_read_debug_printf_v ("deleting old CU %s",
21655 sect_offset_str (cu->per_cu->sect_off));
21656 it = m_dwarf2_cus.erase (it);
21657 }
21658 else
21659 it++;
21660 }
21661 }
21662
21663 /* See read.h. */
21664
21665 void
21666 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
21667 {
21668 auto it = m_dwarf2_cus.find (per_cu);
21669 if (it == m_dwarf2_cus.end ())
21670 return;
21671
21672 m_dwarf2_cus.erase (it);
21673 }
21674
21675 dwarf2_per_objfile::~dwarf2_per_objfile ()
21676 {
21677 remove_all_cus ();
21678 }
21679
21680 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21681 We store these in a hash table separate from the DIEs, and preserve them
21682 when the DIEs are flushed out of cache.
21683
21684 The CU "per_cu" pointer is needed because offset alone is not enough to
21685 uniquely identify the type. A file may have multiple .debug_types sections,
21686 or the type may come from a DWO file. Furthermore, while it's more logical
21687 to use per_cu->section+offset, with Fission the section with the data is in
21688 the DWO file but we don't know that section at the point we need it.
21689 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21690 because we can enter the lookup routine, get_die_type_at_offset, from
21691 outside this file, and thus won't necessarily have PER_CU->cu.
21692 Fortunately, PER_CU is stable for the life of the objfile. */
21693
21694 struct dwarf2_per_cu_offset_and_type
21695 {
21696 const struct dwarf2_per_cu_data *per_cu;
21697 sect_offset sect_off;
21698 struct type *type;
21699 };
21700
21701 /* Hash function for a dwarf2_per_cu_offset_and_type. */
21702
21703 static hashval_t
21704 per_cu_offset_and_type_hash (const void *item)
21705 {
21706 const struct dwarf2_per_cu_offset_and_type *ofs
21707 = (const struct dwarf2_per_cu_offset_and_type *) item;
21708
21709 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
21710 }
21711
21712 /* Equality function for a dwarf2_per_cu_offset_and_type. */
21713
21714 static int
21715 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
21716 {
21717 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
21718 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
21719 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
21720 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
21721
21722 return (ofs_lhs->per_cu == ofs_rhs->per_cu
21723 && ofs_lhs->sect_off == ofs_rhs->sect_off);
21724 }
21725
21726 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21727 table if necessary. For convenience, return TYPE.
21728
21729 The DIEs reading must have careful ordering to:
21730 * Not cause infinite loops trying to read in DIEs as a prerequisite for
21731 reading current DIE.
21732 * Not trying to dereference contents of still incompletely read in types
21733 while reading in other DIEs.
21734 * Enable referencing still incompletely read in types just by a pointer to
21735 the type without accessing its fields.
21736
21737 Therefore caller should follow these rules:
21738 * Try to fetch any prerequisite types we may need to build this DIE type
21739 before building the type and calling set_die_type.
21740 * After building type call set_die_type for current DIE as soon as
21741 possible before fetching more types to complete the current type.
21742 * Make the type as complete as possible before fetching more types. */
21743
21744 static struct type *
21745 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21746 bool skip_data_location)
21747 {
21748 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21749 struct dwarf2_per_cu_offset_and_type **slot, ofs;
21750 struct objfile *objfile = per_objfile->objfile;
21751 struct attribute *attr;
21752 struct dynamic_prop prop;
21753
21754 /* For Ada types, make sure that the gnat-specific data is always
21755 initialized (if not already set). There are a few types where
21756 we should not be doing so, because the type-specific area is
21757 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21758 where the type-specific area is used to store the floatformat).
21759 But this is not a problem, because the gnat-specific information
21760 is actually not needed for these types. */
21761 if (need_gnat_info (cu)
21762 && type->code () != TYPE_CODE_FUNC
21763 && type->code () != TYPE_CODE_FLT
21764 && type->code () != TYPE_CODE_METHODPTR
21765 && type->code () != TYPE_CODE_MEMBERPTR
21766 && type->code () != TYPE_CODE_METHOD
21767 && type->code () != TYPE_CODE_FIXED_POINT
21768 && !HAVE_GNAT_AUX_INFO (type))
21769 INIT_GNAT_SPECIFIC (type);
21770
21771 /* Read DW_AT_allocated and set in type. */
21772 attr = dwarf2_attr (die, DW_AT_allocated, cu);
21773 if (attr != NULL)
21774 {
21775 struct type *prop_type = cu->addr_sized_int_type (false);
21776 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21777 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
21778 }
21779
21780 /* Read DW_AT_associated and set in type. */
21781 attr = dwarf2_attr (die, DW_AT_associated, cu);
21782 if (attr != NULL)
21783 {
21784 struct type *prop_type = cu->addr_sized_int_type (false);
21785 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21786 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
21787 }
21788
21789 /* Read DW_AT_rank and set in type. */
21790 attr = dwarf2_attr (die, DW_AT_rank, cu);
21791 if (attr != NULL)
21792 {
21793 struct type *prop_type = cu->addr_sized_int_type (false);
21794 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21795 type->add_dyn_prop (DYN_PROP_RANK, prop);
21796 }
21797
21798 /* Read DW_AT_data_location and set in type. */
21799 if (!skip_data_location)
21800 {
21801 attr = dwarf2_attr (die, DW_AT_data_location, cu);
21802 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
21803 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
21804 }
21805
21806 if (per_objfile->die_type_hash == NULL)
21807 per_objfile->die_type_hash
21808 = htab_up (htab_create_alloc (127,
21809 per_cu_offset_and_type_hash,
21810 per_cu_offset_and_type_eq,
21811 NULL, xcalloc, xfree));
21812
21813 ofs.per_cu = cu->per_cu;
21814 ofs.sect_off = die->sect_off;
21815 ofs.type = type;
21816 slot = (struct dwarf2_per_cu_offset_and_type **)
21817 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
21818 if (*slot)
21819 complaint (_("A problem internal to GDB: DIE %s has type already set"),
21820 sect_offset_str (die->sect_off));
21821 *slot = XOBNEW (&objfile->objfile_obstack,
21822 struct dwarf2_per_cu_offset_and_type);
21823 **slot = ofs;
21824 return type;
21825 }
21826
21827 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
21828 or return NULL if the die does not have a saved type. */
21829
21830 static struct type *
21831 get_die_type_at_offset (sect_offset sect_off,
21832 dwarf2_per_cu_data *per_cu,
21833 dwarf2_per_objfile *per_objfile)
21834 {
21835 struct dwarf2_per_cu_offset_and_type *slot, ofs;
21836
21837 if (per_objfile->die_type_hash == NULL)
21838 return NULL;
21839
21840 ofs.per_cu = per_cu;
21841 ofs.sect_off = sect_off;
21842 slot = ((struct dwarf2_per_cu_offset_and_type *)
21843 htab_find (per_objfile->die_type_hash.get (), &ofs));
21844 if (slot)
21845 return slot->type;
21846 else
21847 return NULL;
21848 }
21849
21850 /* Look up the type for DIE in CU in die_type_hash,
21851 or return NULL if DIE does not have a saved type. */
21852
21853 static struct type *
21854 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
21855 {
21856 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
21857 }
21858
21859 struct cmd_list_element *set_dwarf_cmdlist;
21860 struct cmd_list_element *show_dwarf_cmdlist;
21861
21862 static void
21863 show_check_physname (struct ui_file *file, int from_tty,
21864 struct cmd_list_element *c, const char *value)
21865 {
21866 gdb_printf (file,
21867 _("Whether to check \"physname\" is %s.\n"),
21868 value);
21869 }
21870
21871 void _initialize_dwarf2_read ();
21872 void
21873 _initialize_dwarf2_read ()
21874 {
21875 add_setshow_prefix_cmd ("dwarf", class_maintenance,
21876 _("\
21877 Set DWARF specific variables.\n\
21878 Configure DWARF variables such as the cache size."),
21879 _("\
21880 Show DWARF specific variables.\n\
21881 Show DWARF variables such as the cache size."),
21882 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
21883 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
21884
21885 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21886 &dwarf_max_cache_age, _("\
21887 Set the upper bound on the age of cached DWARF compilation units."), _("\
21888 Show the upper bound on the age of cached DWARF compilation units."), _("\
21889 A higher limit means that cached compilation units will be stored\n\
21890 in memory longer, and more total memory will be used. Zero disables\n\
21891 caching, which can slow down startup."),
21892 NULL,
21893 show_dwarf_max_cache_age,
21894 &set_dwarf_cmdlist,
21895 &show_dwarf_cmdlist);
21896
21897 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
21898 Set debugging of the DWARF reader."), _("\
21899 Show debugging of the DWARF reader."), _("\
21900 When enabled (non-zero), debugging messages are printed during DWARF\n\
21901 reading and symtab expansion. A value of 1 (one) provides basic\n\
21902 information. A value greater than 1 provides more verbose information."),
21903 NULL,
21904 NULL,
21905 &setdebuglist, &showdebuglist);
21906
21907 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
21908 Set debugging of the DWARF DIE reader."), _("\
21909 Show debugging of the DWARF DIE reader."), _("\
21910 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21911 The value is the maximum depth to print."),
21912 NULL,
21913 NULL,
21914 &setdebuglist, &showdebuglist);
21915
21916 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
21917 Set debugging of the dwarf line reader."), _("\
21918 Show debugging of the dwarf line reader."), _("\
21919 When enabled (non-zero), line number entries are dumped as they are read in.\n\
21920 A value of 1 (one) provides basic information.\n\
21921 A value greater than 1 provides more verbose information."),
21922 NULL,
21923 NULL,
21924 &setdebuglist, &showdebuglist);
21925
21926 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21927 Set cross-checking of \"physname\" code against demangler."), _("\
21928 Show cross-checking of \"physname\" code against demangler."), _("\
21929 When enabled, GDB's internal \"physname\" code is checked against\n\
21930 the demangler."),
21931 NULL, show_check_physname,
21932 &setdebuglist, &showdebuglist);
21933
21934 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21935 &dwarf2_locexpr_funcs);
21936 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21937 &dwarf2_loclist_funcs);
21938
21939 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21940 &dwarf2_block_frame_base_locexpr_funcs);
21941 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21942 &dwarf2_block_frame_base_loclist_funcs);
21943
21944 #if GDB_SELF_TEST
21945 selftests::register_test ("dw2_expand_symtabs_matching",
21946 selftests::dw2_expand_symtabs_matching::run_test);
21947 selftests::register_test ("dwarf2_find_containing_comp_unit",
21948 selftests::find_containing_comp_unit::run_test);
21949 #endif
21950 }