7ff090225e055cbe17b6643448235e58ffb7766a
[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 bool allow_reprocess = true);
762
763 /* Note that the default for TAG is chosen because it only matters
764 when reading the top-level DIE, and that function is careful to
765 pass the correct tag. */
766 static void read_attribute_reprocess (const struct die_reader_specs *reader,
767 struct attribute *attr,
768 dwarf_tag tag = DW_TAG_padding);
769
770 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
771
772 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
773 dwarf2_section_info *, sect_offset);
774
775 static const char *read_indirect_string
776 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
777 const struct comp_unit_head *, unsigned int *);
778
779 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
780 const gdb_byte *,
781 unsigned int *);
782
783 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
784 ULONGEST str_index);
785
786 static const char *read_stub_str_index (struct dwarf2_cu *cu,
787 ULONGEST str_index);
788
789 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
790 struct dwarf2_cu *);
791
792 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
793 struct dwarf2_cu *cu);
794
795 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
796
797 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
798 struct dwarf2_cu *cu);
799
800 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
801
802 static struct die_info *die_specification (struct die_info *die,
803 struct dwarf2_cu **);
804
805 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
806 struct dwarf2_cu *cu,
807 const char *comp_dir);
808
809 static void dwarf_decode_lines (struct line_header *,
810 struct dwarf2_cu *,
811 CORE_ADDR, int decode_mapping);
812
813 static void dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
814 const line_header &lh);
815
816 static struct symbol *new_symbol (struct die_info *, struct type *,
817 struct dwarf2_cu *, struct symbol * = NULL);
818
819 static void dwarf2_const_value (const struct attribute *, struct symbol *,
820 struct dwarf2_cu *);
821
822 static void dwarf2_const_value_attr (const struct attribute *attr,
823 struct type *type,
824 const char *name,
825 struct obstack *obstack,
826 struct dwarf2_cu *cu, LONGEST *value,
827 const gdb_byte **bytes,
828 struct dwarf2_locexpr_baton **baton);
829
830 static struct type *read_subrange_index_type (struct die_info *die,
831 struct dwarf2_cu *cu);
832
833 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
834
835 static int need_gnat_info (struct dwarf2_cu *);
836
837 static struct type *die_descriptive_type (struct die_info *,
838 struct dwarf2_cu *);
839
840 static void set_descriptive_type (struct type *, struct die_info *,
841 struct dwarf2_cu *);
842
843 static struct type *die_containing_type (struct die_info *,
844 struct dwarf2_cu *);
845
846 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
847 struct dwarf2_cu *);
848
849 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
850
851 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
852
853 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
854
855 static char *typename_concat (struct obstack *obs, const char *prefix,
856 const char *suffix, int physname,
857 struct dwarf2_cu *cu);
858
859 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
860
861 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
862
863 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
864
865 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
866
867 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
868
869 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
870
871 /* Return the .debug_loclists section to use for cu. */
872 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
873
874 /* Return the .debug_rnglists section to use for cu. */
875 static struct dwarf2_section_info *cu_debug_rnglists_section
876 (struct dwarf2_cu *cu, dwarf_tag tag);
877
878 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
879 values. Keep the items ordered with increasing constraints compliance. */
880 enum pc_bounds_kind
881 {
882 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
883 PC_BOUNDS_NOT_PRESENT,
884
885 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
886 were present but they do not form a valid range of PC addresses. */
887 PC_BOUNDS_INVALID,
888
889 /* Discontiguous range was found - that is DW_AT_ranges was found. */
890 PC_BOUNDS_RANGES,
891
892 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
893 PC_BOUNDS_HIGH_LOW,
894 };
895
896 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
897 CORE_ADDR *, CORE_ADDR *,
898 struct dwarf2_cu *,
899 addrmap *,
900 void *);
901
902 static void get_scope_pc_bounds (struct die_info *,
903 CORE_ADDR *, CORE_ADDR *,
904 struct dwarf2_cu *);
905
906 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
907 CORE_ADDR, struct dwarf2_cu *);
908
909 static void dwarf2_add_field (struct field_info *, struct die_info *,
910 struct dwarf2_cu *);
911
912 static void dwarf2_attach_fields_to_type (struct field_info *,
913 struct type *, struct dwarf2_cu *);
914
915 static void dwarf2_add_member_fn (struct field_info *,
916 struct die_info *, struct type *,
917 struct dwarf2_cu *);
918
919 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
920 struct type *,
921 struct dwarf2_cu *);
922
923 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
924
925 static void read_common_block (struct die_info *, struct dwarf2_cu *);
926
927 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
928
929 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
930
931 static struct using_direct **using_directives (struct dwarf2_cu *cu);
932
933 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
934
935 static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
936
937 static struct type *read_module_type (struct die_info *die,
938 struct dwarf2_cu *cu);
939
940 static const char *namespace_name (struct die_info *die,
941 int *is_anonymous, struct dwarf2_cu *);
942
943 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
944
945 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
946 bool * = nullptr);
947
948 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
949 struct dwarf2_cu *);
950
951 static struct die_info *read_die_and_siblings_1
952 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
953 struct die_info *);
954
955 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
956 const gdb_byte *info_ptr,
957 const gdb_byte **new_info_ptr,
958 struct die_info *parent);
959
960 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
961 struct die_info **, const gdb_byte *,
962 int, bool);
963
964 static const gdb_byte *read_toplevel_die (const struct die_reader_specs *,
965 struct die_info **,
966 const gdb_byte *,
967 gdb::array_view<attribute *> = {});
968
969 static void process_die (struct die_info *, struct dwarf2_cu *);
970
971 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
972 struct objfile *);
973
974 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
975
976 static const char *dwarf2_full_name (const char *name,
977 struct die_info *die,
978 struct dwarf2_cu *cu);
979
980 static const char *dwarf2_physname (const char *name, struct die_info *die,
981 struct dwarf2_cu *cu);
982
983 static struct die_info *dwarf2_extension (struct die_info *die,
984 struct dwarf2_cu **);
985
986 static void store_in_ref_table (struct die_info *,
987 struct dwarf2_cu *);
988
989 static struct die_info *follow_die_ref_or_sig (struct die_info *,
990 const struct attribute *,
991 struct dwarf2_cu **);
992
993 static struct die_info *follow_die_ref (struct die_info *,
994 const struct attribute *,
995 struct dwarf2_cu **);
996
997 static struct die_info *follow_die_sig (struct die_info *,
998 const struct attribute *,
999 struct dwarf2_cu **);
1000
1001 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1002 struct dwarf2_cu *);
1003
1004 static struct type *get_DW_AT_signature_type (struct die_info *,
1005 const struct attribute *,
1006 struct dwarf2_cu *);
1007
1008 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1009 dwarf2_per_objfile *per_objfile);
1010
1011 static void read_signatured_type (signatured_type *sig_type,
1012 dwarf2_per_objfile *per_objfile);
1013
1014 static int attr_to_dynamic_prop (const struct attribute *attr,
1015 struct die_info *die, struct dwarf2_cu *cu,
1016 struct dynamic_prop *prop, struct type *type);
1017
1018 /* memory allocation interface */
1019
1020 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1021
1022 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1023
1024 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1025 struct dwarf2_loclist_baton *baton,
1026 const struct attribute *attr);
1027
1028 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1029 struct symbol *sym,
1030 struct dwarf2_cu *cu,
1031 int is_block);
1032
1033 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1034 const gdb_byte *info_ptr,
1035 const struct abbrev_info *abbrev,
1036 bool do_skip_children = true);
1037
1038 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1039 (sect_offset sect_off, unsigned int offset_in_dwz,
1040 dwarf2_per_bfd *per_bfd);
1041
1042 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1043 struct die_info *comp_unit_die,
1044 enum language pretend_language);
1045
1046 static struct type *set_die_type (struct die_info *, struct type *,
1047 struct dwarf2_cu *, bool = false);
1048
1049 static void create_all_units (dwarf2_per_objfile *per_objfile);
1050
1051 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1052 dwarf2_per_objfile *per_objfile,
1053 dwarf2_cu *existing_cu,
1054 bool skip_partial,
1055 enum language pretend_language);
1056
1057 static void process_full_comp_unit (dwarf2_cu *cu,
1058 enum language pretend_language);
1059
1060 static void process_full_type_unit (dwarf2_cu *cu,
1061 enum language pretend_language);
1062
1063 static struct type *get_die_type_at_offset (sect_offset,
1064 dwarf2_per_cu_data *per_cu,
1065 dwarf2_per_objfile *per_objfile);
1066
1067 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1068
1069 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1070 dwarf2_per_objfile *per_objfile,
1071 enum language pretend_language);
1072
1073 static void process_queue (dwarf2_per_objfile *per_objfile);
1074
1075 /* Class, the destructor of which frees all allocated queue entries. This
1076 will only have work to do if an error was thrown while processing the
1077 dwarf. If no error was thrown then the queue entries should have all
1078 been processed, and freed, as we went along. */
1079
1080 class dwarf2_queue_guard
1081 {
1082 public:
1083 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1084 : m_per_objfile (per_objfile)
1085 {
1086 gdb_assert (!m_per_objfile->queue.has_value ());
1087
1088 m_per_objfile->queue.emplace ();
1089 }
1090
1091 /* Free any entries remaining on the queue. There should only be
1092 entries left if we hit an error while processing the dwarf. */
1093 ~dwarf2_queue_guard ()
1094 {
1095 gdb_assert (m_per_objfile->queue.has_value ());
1096
1097 m_per_objfile->queue.reset ();
1098 }
1099
1100 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1101
1102 private:
1103 dwarf2_per_objfile *m_per_objfile;
1104 };
1105
1106 dwarf2_queue_item::~dwarf2_queue_item ()
1107 {
1108 /* Anything still marked queued is likely to be in an
1109 inconsistent state, so discard it. */
1110 if (per_cu->queued)
1111 {
1112 per_objfile->remove_cu (per_cu);
1113 per_cu->queued = 0;
1114 }
1115 }
1116
1117 /* See dwarf2/read.h. */
1118
1119 void
1120 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1121 {
1122 if (data->is_debug_types)
1123 delete static_cast<signatured_type *> (data);
1124 else
1125 delete data;
1126 }
1127
1128 static file_and_directory &find_file_and_directory
1129 (struct die_info *die, struct dwarf2_cu *cu);
1130
1131 static const char *compute_include_file_name
1132 (const struct line_header *lh,
1133 const file_entry &fe,
1134 const file_and_directory &cu_info,
1135 std::string &name_holder);
1136
1137 static htab_up allocate_dwo_unit_table ();
1138
1139 static struct dwo_unit *lookup_dwo_unit_in_dwp
1140 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1141 const char *comp_dir, ULONGEST signature, int is_debug_types);
1142
1143 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1144
1145 static struct dwo_unit *lookup_dwo_comp_unit
1146 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1147 ULONGEST signature);
1148
1149 static struct dwo_unit *lookup_dwo_type_unit
1150 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1151
1152 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1153
1154 /* A unique pointer to a dwo_file. */
1155
1156 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1157
1158 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1159
1160 static void check_producer (struct dwarf2_cu *cu);
1161 \f
1162 /* Various complaints about symbol reading that don't abort the process. */
1163
1164 static void
1165 dwarf2_debug_line_missing_file_complaint (void)
1166 {
1167 complaint (_(".debug_line section has line data without a file"));
1168 }
1169
1170 static void
1171 dwarf2_debug_line_missing_end_sequence_complaint (void)
1172 {
1173 complaint (_(".debug_line section has line "
1174 "program sequence without an end"));
1175 }
1176
1177 static void
1178 dwarf2_complex_location_expr_complaint (void)
1179 {
1180 complaint (_("location expression too complex"));
1181 }
1182
1183 static void
1184 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1185 int arg3)
1186 {
1187 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1188 arg1, arg2, arg3);
1189 }
1190
1191 static void
1192 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1193 {
1194 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1195 arg1, arg2);
1196 }
1197
1198 /* Hash function for line_header_hash. */
1199
1200 static hashval_t
1201 line_header_hash (const struct line_header *ofs)
1202 {
1203 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1204 }
1205
1206 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1207
1208 static hashval_t
1209 line_header_hash_voidp (const void *item)
1210 {
1211 const struct line_header *ofs = (const struct line_header *) item;
1212
1213 return line_header_hash (ofs);
1214 }
1215
1216 /* Equality function for line_header_hash. */
1217
1218 static int
1219 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1220 {
1221 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1222 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1223
1224 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1225 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1226 }
1227
1228 /* See declaration. */
1229
1230 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1231 bool can_copy_)
1232 : obfd (obfd),
1233 can_copy (can_copy_)
1234 {
1235 if (names == NULL)
1236 names = &dwarf2_elf_names;
1237
1238 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1239 locate_sections (obfd, sec, *names);
1240 }
1241
1242 dwarf2_per_bfd::~dwarf2_per_bfd ()
1243 {
1244 /* Data from the per-BFD may be needed when finalizing the cooked
1245 index table, so wait here while this happens. */
1246 if (index_table != nullptr)
1247 index_table->wait_completely ();
1248
1249 for (auto &per_cu : all_units)
1250 {
1251 per_cu->imported_symtabs_free ();
1252 per_cu->free_cached_file_names ();
1253 }
1254
1255 /* Everything else should be on this->obstack. */
1256 }
1257
1258 /* See read.h. */
1259
1260 void
1261 dwarf2_per_objfile::remove_all_cus ()
1262 {
1263 gdb_assert (!queue.has_value ());
1264
1265 m_dwarf2_cus.clear ();
1266 }
1267
1268 /* A helper class that calls free_cached_comp_units on
1269 destruction. */
1270
1271 class free_cached_comp_units
1272 {
1273 public:
1274
1275 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1276 : m_per_objfile (per_objfile)
1277 {
1278 }
1279
1280 ~free_cached_comp_units ()
1281 {
1282 m_per_objfile->remove_all_cus ();
1283 }
1284
1285 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1286
1287 private:
1288
1289 dwarf2_per_objfile *m_per_objfile;
1290 };
1291
1292 /* See read.h. */
1293
1294 bool
1295 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1296 {
1297 if (per_cu->index < this->m_symtabs.size ())
1298 return this->m_symtabs[per_cu->index] != nullptr;
1299 return false;
1300 }
1301
1302 /* See read.h. */
1303
1304 compunit_symtab *
1305 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1306 {
1307 if (per_cu->index < this->m_symtabs.size ())
1308 return this->m_symtabs[per_cu->index];
1309 return nullptr;
1310 }
1311
1312 /* See read.h. */
1313
1314 void
1315 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1316 compunit_symtab *symtab)
1317 {
1318 if (per_cu->index >= this->m_symtabs.size ())
1319 this->m_symtabs.resize (per_cu->index + 1);
1320 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1321 this->m_symtabs[per_cu->index] = symtab;
1322 }
1323
1324 /* Try to locate the sections we need for DWARF 2 debugging
1325 information and return true if we have enough to do something.
1326 NAMES points to the dwarf2 section names, or is NULL if the standard
1327 ELF names are used. CAN_COPY is true for formats where symbol
1328 interposition is possible and so symbol values must follow copy
1329 relocation rules. */
1330
1331 bool
1332 dwarf2_has_info (struct objfile *objfile,
1333 const struct dwarf2_debug_sections *names,
1334 bool can_copy)
1335 {
1336 if (objfile->flags & OBJF_READNEVER)
1337 return false;
1338
1339 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1340
1341 if (per_objfile == NULL)
1342 {
1343 dwarf2_per_bfd *per_bfd;
1344
1345 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1346 BFD doesn't require relocations.
1347
1348 We don't share with objfiles for which -readnow was requested,
1349 because it would complicate things when loading the same BFD with
1350 -readnow and then without -readnow. */
1351 if (!gdb_bfd_requires_relocations (objfile->obfd.get ())
1352 && (objfile->flags & OBJF_READNOW) == 0)
1353 {
1354 /* See if one has been created for this BFD yet. */
1355 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd.get ());
1356
1357 if (per_bfd == nullptr)
1358 {
1359 /* No, create it now. */
1360 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
1361 can_copy);
1362 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
1363 }
1364 }
1365 else
1366 {
1367 /* No sharing possible, create one specifically for this objfile. */
1368 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
1369 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1370 }
1371
1372 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1373 }
1374
1375 return (!per_objfile->per_bfd->info.is_virtual
1376 && per_objfile->per_bfd->info.s.section != NULL
1377 && !per_objfile->per_bfd->abbrev.is_virtual
1378 && per_objfile->per_bfd->abbrev.s.section != NULL);
1379 }
1380
1381 /* See declaration. */
1382
1383 void
1384 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1385 const dwarf2_debug_sections &names)
1386 {
1387 flagword aflag = bfd_section_flags (sectp);
1388
1389 if ((aflag & SEC_HAS_CONTENTS) == 0)
1390 {
1391 }
1392 else if (elf_section_data (sectp)->this_hdr.sh_size
1393 > bfd_get_file_size (abfd))
1394 {
1395 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1396 warning (_("Discarding section %s which has a section size (%s"
1397 ") larger than the file size [in module %s]"),
1398 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1399 bfd_get_filename (abfd));
1400 }
1401 else if (names.info.matches (sectp->name))
1402 {
1403 this->info.s.section = sectp;
1404 this->info.size = bfd_section_size (sectp);
1405 }
1406 else if (names.abbrev.matches (sectp->name))
1407 {
1408 this->abbrev.s.section = sectp;
1409 this->abbrev.size = bfd_section_size (sectp);
1410 }
1411 else if (names.line.matches (sectp->name))
1412 {
1413 this->line.s.section = sectp;
1414 this->line.size = bfd_section_size (sectp);
1415 }
1416 else if (names.loc.matches (sectp->name))
1417 {
1418 this->loc.s.section = sectp;
1419 this->loc.size = bfd_section_size (sectp);
1420 }
1421 else if (names.loclists.matches (sectp->name))
1422 {
1423 this->loclists.s.section = sectp;
1424 this->loclists.size = bfd_section_size (sectp);
1425 }
1426 else if (names.macinfo.matches (sectp->name))
1427 {
1428 this->macinfo.s.section = sectp;
1429 this->macinfo.size = bfd_section_size (sectp);
1430 }
1431 else if (names.macro.matches (sectp->name))
1432 {
1433 this->macro.s.section = sectp;
1434 this->macro.size = bfd_section_size (sectp);
1435 }
1436 else if (names.str.matches (sectp->name))
1437 {
1438 this->str.s.section = sectp;
1439 this->str.size = bfd_section_size (sectp);
1440 }
1441 else if (names.str_offsets.matches (sectp->name))
1442 {
1443 this->str_offsets.s.section = sectp;
1444 this->str_offsets.size = bfd_section_size (sectp);
1445 }
1446 else if (names.line_str.matches (sectp->name))
1447 {
1448 this->line_str.s.section = sectp;
1449 this->line_str.size = bfd_section_size (sectp);
1450 }
1451 else if (names.addr.matches (sectp->name))
1452 {
1453 this->addr.s.section = sectp;
1454 this->addr.size = bfd_section_size (sectp);
1455 }
1456 else if (names.frame.matches (sectp->name))
1457 {
1458 this->frame.s.section = sectp;
1459 this->frame.size = bfd_section_size (sectp);
1460 }
1461 else if (names.eh_frame.matches (sectp->name))
1462 {
1463 this->eh_frame.s.section = sectp;
1464 this->eh_frame.size = bfd_section_size (sectp);
1465 }
1466 else if (names.ranges.matches (sectp->name))
1467 {
1468 this->ranges.s.section = sectp;
1469 this->ranges.size = bfd_section_size (sectp);
1470 }
1471 else if (names.rnglists.matches (sectp->name))
1472 {
1473 this->rnglists.s.section = sectp;
1474 this->rnglists.size = bfd_section_size (sectp);
1475 }
1476 else if (names.types.matches (sectp->name))
1477 {
1478 struct dwarf2_section_info type_section;
1479
1480 memset (&type_section, 0, sizeof (type_section));
1481 type_section.s.section = sectp;
1482 type_section.size = bfd_section_size (sectp);
1483
1484 this->types.push_back (type_section);
1485 }
1486 else if (names.gdb_index.matches (sectp->name))
1487 {
1488 this->gdb_index.s.section = sectp;
1489 this->gdb_index.size = bfd_section_size (sectp);
1490 }
1491 else if (names.debug_names.matches (sectp->name))
1492 {
1493 this->debug_names.s.section = sectp;
1494 this->debug_names.size = bfd_section_size (sectp);
1495 }
1496 else if (names.debug_aranges.matches (sectp->name))
1497 {
1498 this->debug_aranges.s.section = sectp;
1499 this->debug_aranges.size = bfd_section_size (sectp);
1500 }
1501
1502 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1503 && bfd_section_vma (sectp) == 0)
1504 this->has_section_at_zero = true;
1505 }
1506
1507 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1508 SECTION_NAME. */
1509
1510 void
1511 dwarf2_get_section_info (struct objfile *objfile,
1512 enum dwarf2_section_enum sect,
1513 asection **sectp, const gdb_byte **bufp,
1514 bfd_size_type *sizep)
1515 {
1516 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1517 struct dwarf2_section_info *info;
1518
1519 /* We may see an objfile without any DWARF, in which case we just
1520 return nothing. */
1521 if (per_objfile == NULL)
1522 {
1523 *sectp = NULL;
1524 *bufp = NULL;
1525 *sizep = 0;
1526 return;
1527 }
1528 switch (sect)
1529 {
1530 case DWARF2_DEBUG_FRAME:
1531 info = &per_objfile->per_bfd->frame;
1532 break;
1533 case DWARF2_EH_FRAME:
1534 info = &per_objfile->per_bfd->eh_frame;
1535 break;
1536 default:
1537 gdb_assert_not_reached ("unexpected section");
1538 }
1539
1540 info->read (objfile);
1541
1542 *sectp = info->get_bfd_section ();
1543 *bufp = info->buffer;
1544 *sizep = info->size;
1545 }
1546
1547 /* See dwarf2/read.h. */
1548
1549 void
1550 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1551 {
1552 info.read (objfile);
1553 abbrev.read (objfile);
1554 line.read (objfile);
1555 str.read (objfile);
1556 str_offsets.read (objfile);
1557 line_str.read (objfile);
1558 ranges.read (objfile);
1559 rnglists.read (objfile);
1560 addr.read (objfile);
1561
1562 for (auto &section : types)
1563 section.read (objfile);
1564 }
1565
1566 \f
1567 /* DWARF quick_symbol_functions support. */
1568
1569 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1570 unique line tables, so we maintain a separate table of all .debug_line
1571 derived entries to support the sharing.
1572 All the quick functions need is the list of file names. We discard the
1573 line_header when we're done and don't need to record it here. */
1574 struct quick_file_names
1575 {
1576 /* The data used to construct the hash key. */
1577 struct stmt_list_hash hash;
1578
1579 /* The number of entries in file_names, real_names. */
1580 unsigned int num_file_names;
1581
1582 /* The CU directory, as given by DW_AT_comp_dir. May be
1583 nullptr. */
1584 const char *comp_dir;
1585
1586 /* The file names from the line table, after being run through
1587 file_full_name. */
1588 const char **file_names;
1589
1590 /* The file names from the line table after being run through
1591 gdb_realpath. These are computed lazily. */
1592 const char **real_names;
1593 };
1594
1595 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1596 It's handy in this case to have an empty implementation of the
1597 quick symbol functions, to avoid special cases in the rest of the
1598 code. */
1599
1600 struct readnow_functions : public dwarf2_base_index_functions
1601 {
1602 void dump (struct objfile *objfile) override
1603 {
1604 }
1605
1606 void expand_matching_symbols
1607 (struct objfile *,
1608 const lookup_name_info &lookup_name,
1609 domain_enum domain,
1610 int global,
1611 symbol_compare_ftype *ordered_compare) override
1612 {
1613 }
1614
1615 bool expand_symtabs_matching
1616 (struct objfile *objfile,
1617 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1618 const lookup_name_info *lookup_name,
1619 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1620 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1621 block_search_flags search_flags,
1622 domain_enum domain,
1623 enum search_domain kind) override
1624 {
1625 return true;
1626 }
1627 };
1628
1629 /* Utility hash function for a stmt_list_hash. */
1630
1631 static hashval_t
1632 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1633 {
1634 hashval_t v = 0;
1635
1636 if (stmt_list_hash->dwo_unit != NULL)
1637 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1638 v += to_underlying (stmt_list_hash->line_sect_off);
1639 return v;
1640 }
1641
1642 /* Utility equality function for a stmt_list_hash. */
1643
1644 static int
1645 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1646 const struct stmt_list_hash *rhs)
1647 {
1648 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1649 return 0;
1650 if (lhs->dwo_unit != NULL
1651 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1652 return 0;
1653
1654 return lhs->line_sect_off == rhs->line_sect_off;
1655 }
1656
1657 /* Hash function for a quick_file_names. */
1658
1659 static hashval_t
1660 hash_file_name_entry (const void *e)
1661 {
1662 const struct quick_file_names *file_data
1663 = (const struct quick_file_names *) e;
1664
1665 return hash_stmt_list_entry (&file_data->hash);
1666 }
1667
1668 /* Equality function for a quick_file_names. */
1669
1670 static int
1671 eq_file_name_entry (const void *a, const void *b)
1672 {
1673 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1674 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1675
1676 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1677 }
1678
1679 /* See read.h. */
1680
1681 htab_up
1682 create_quick_file_names_table (unsigned int nr_initial_entries)
1683 {
1684 return htab_up (htab_create_alloc (nr_initial_entries,
1685 hash_file_name_entry, eq_file_name_entry,
1686 nullptr, xcalloc, xfree));
1687 }
1688
1689 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
1690 function is unrelated to symtabs, symtab would have to be created afterwards.
1691 You should call age_cached_comp_units after processing the CU. */
1692
1693 static dwarf2_cu *
1694 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1695 bool skip_partial)
1696 {
1697 if (per_cu->is_debug_types)
1698 load_full_type_unit (per_cu, per_objfile);
1699 else
1700 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
1701 skip_partial, language_minimal);
1702
1703 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1704 if (cu == nullptr)
1705 return nullptr; /* Dummy CU. */
1706
1707 dwarf2_find_base_address (cu->dies, cu);
1708
1709 return cu;
1710 }
1711
1712 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
1713
1714 static void
1715 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1716 dwarf2_per_objfile *per_objfile, bool skip_partial)
1717 {
1718 {
1719 /* The destructor of dwarf2_queue_guard frees any entries left on
1720 the queue. After this point we're guaranteed to leave this function
1721 with the dwarf queue empty. */
1722 dwarf2_queue_guard q_guard (per_objfile);
1723
1724 if (!per_objfile->symtab_set_p (per_cu))
1725 {
1726 queue_comp_unit (per_cu, per_objfile, language_minimal);
1727 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
1728
1729 /* If we just loaded a CU from a DWO, and we're working with an index
1730 that may badly handle TUs, load all the TUs in that DWO as well.
1731 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
1732 if (!per_cu->is_debug_types
1733 && cu != NULL
1734 && cu->dwo_unit != NULL
1735 && per_objfile->per_bfd->index_table != NULL
1736 && !per_objfile->per_bfd->index_table->version_check ()
1737 /* DWP files aren't supported yet. */
1738 && get_dwp_file (per_objfile) == NULL)
1739 queue_and_load_all_dwo_tus (cu);
1740 }
1741
1742 process_queue (per_objfile);
1743 }
1744
1745 /* Age the cache, releasing compilation units that have not
1746 been used recently. */
1747 per_objfile->age_comp_units ();
1748 }
1749
1750 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
1751 the per-objfile for which this symtab is instantiated.
1752
1753 Returns the resulting symbol table. */
1754
1755 static struct compunit_symtab *
1756 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1757 dwarf2_per_objfile *per_objfile,
1758 bool skip_partial)
1759 {
1760 if (!per_objfile->symtab_set_p (per_cu))
1761 {
1762 free_cached_comp_units freer (per_objfile);
1763 scoped_restore decrementer = increment_reading_symtab ();
1764 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
1765 process_cu_includes (per_objfile);
1766 }
1767
1768 return per_objfile->get_symtab (per_cu);
1769 }
1770
1771 /* See read.h. */
1772
1773 dwarf2_per_cu_data_up
1774 dwarf2_per_bfd::allocate_per_cu ()
1775 {
1776 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
1777 result->per_bfd = this;
1778 result->index = all_units.size ();
1779 return result;
1780 }
1781
1782 /* See read.h. */
1783
1784 signatured_type_up
1785 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
1786 {
1787 signatured_type_up result (new signatured_type (signature));
1788 result->per_bfd = this;
1789 result->index = all_units.size ();
1790 result->is_debug_types = true;
1791 tu_stats.nr_tus++;
1792 return result;
1793 }
1794
1795 /* See read.h. */
1796
1797 dwarf2_per_cu_data_up
1798 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
1799 struct dwarf2_section_info *section,
1800 int is_dwz,
1801 sect_offset sect_off, ULONGEST length)
1802 {
1803 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
1804 the_cu->sect_off = sect_off;
1805 the_cu->set_length (length);
1806 the_cu->section = section;
1807 the_cu->is_dwz = is_dwz;
1808 return the_cu;
1809 }
1810
1811 /* See read.h. */
1812
1813 bool
1814 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
1815 dwarf2_section_info *section,
1816 addrmap *mutable_map)
1817 {
1818 struct objfile *objfile = per_objfile->objfile;
1819 bfd *abfd = objfile->obfd.get ();
1820 struct gdbarch *gdbarch = objfile->arch ();
1821 const CORE_ADDR baseaddr = objfile->text_section_offset ();
1822 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1823
1824 std::unordered_map<sect_offset,
1825 dwarf2_per_cu_data *,
1826 gdb::hash_enum<sect_offset>>
1827 debug_info_offset_to_per_cu;
1828 for (const auto &per_cu : per_bfd->all_units)
1829 {
1830 /* A TU will not need aranges, and skipping them here is an easy
1831 way of ignoring .debug_types -- and possibly seeing a
1832 duplicate section offset -- entirely. The same applies to
1833 units coming from a dwz file. */
1834 if (per_cu->is_debug_types || per_cu->is_dwz)
1835 continue;
1836
1837 const auto insertpair
1838 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
1839 per_cu.get ());
1840
1841 /* Assume no duplicate offsets in all_units. */
1842 gdb_assert (insertpair.second);
1843 }
1844
1845 std::set<sect_offset> debug_info_offset_seen;
1846
1847 section->read (objfile);
1848
1849 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
1850
1851 const gdb_byte *addr = section->buffer;
1852
1853 while (addr < section->buffer + section->size)
1854 {
1855 const gdb_byte *const entry_addr = addr;
1856 unsigned int bytes_read;
1857
1858 const LONGEST entry_length = read_initial_length (abfd, addr,
1859 &bytes_read);
1860 addr += bytes_read;
1861
1862 const gdb_byte *const entry_end = addr + entry_length;
1863 const bool dwarf5_is_dwarf64 = bytes_read != 4;
1864 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
1865 if (addr + entry_length > section->buffer + section->size)
1866 {
1867 warning (_("Section .debug_aranges in %s entry at offset %s "
1868 "length %s exceeds section length %s, "
1869 "ignoring .debug_aranges."),
1870 objfile_name (objfile),
1871 plongest (entry_addr - section->buffer),
1872 plongest (bytes_read + entry_length),
1873 pulongest (section->size));
1874 return false;
1875 }
1876
1877 /* The version number. */
1878 const uint16_t version = read_2_bytes (abfd, addr);
1879 addr += 2;
1880 if (version != 2)
1881 {
1882 warning (_("Section .debug_aranges in %s entry at offset %s "
1883 "has unsupported version %d, ignoring .debug_aranges."),
1884 objfile_name (objfile),
1885 plongest (entry_addr - section->buffer), version);
1886 return false;
1887 }
1888
1889 const uint64_t debug_info_offset
1890 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
1891 addr += offset_size;
1892 const auto per_cu_it
1893 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
1894 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
1895 {
1896 warning (_("Section .debug_aranges in %s entry at offset %s "
1897 "debug_info_offset %s does not exists, "
1898 "ignoring .debug_aranges."),
1899 objfile_name (objfile),
1900 plongest (entry_addr - section->buffer),
1901 pulongest (debug_info_offset));
1902 return false;
1903 }
1904 const auto insertpair
1905 = debug_info_offset_seen.insert (sect_offset (debug_info_offset));
1906 if (!insertpair.second)
1907 {
1908 warning (_("Section .debug_aranges in %s has duplicate "
1909 "debug_info_offset %s, ignoring .debug_aranges."),
1910 objfile_name (objfile),
1911 sect_offset_str (sect_offset (debug_info_offset)));
1912 return false;
1913 }
1914 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
1915
1916 const uint8_t address_size = *addr++;
1917 if (address_size < 1 || address_size > 8)
1918 {
1919 warning (_("Section .debug_aranges in %s entry at offset %s "
1920 "address_size %u is invalid, ignoring .debug_aranges."),
1921 objfile_name (objfile),
1922 plongest (entry_addr - section->buffer), address_size);
1923 return false;
1924 }
1925
1926 const uint8_t segment_selector_size = *addr++;
1927 if (segment_selector_size != 0)
1928 {
1929 warning (_("Section .debug_aranges in %s entry at offset %s "
1930 "segment_selector_size %u is not supported, "
1931 "ignoring .debug_aranges."),
1932 objfile_name (objfile),
1933 plongest (entry_addr - section->buffer),
1934 segment_selector_size);
1935 return false;
1936 }
1937
1938 /* Must pad to an alignment boundary that is twice the address
1939 size. It is undocumented by the DWARF standard but GCC does
1940 use it. However, not every compiler does this. We can see
1941 whether it has happened by looking at the total length of the
1942 contents of the aranges for this CU -- it if isn't a multiple
1943 of twice the address size, then we skip any leftover
1944 bytes. */
1945 addr += (entry_end - addr) % (2 * address_size);
1946
1947 while (addr < entry_end)
1948 {
1949 if (addr + 2 * address_size > entry_end)
1950 {
1951 warning (_("Section .debug_aranges in %s entry at offset %s "
1952 "address list is not properly terminated, "
1953 "ignoring .debug_aranges."),
1954 objfile_name (objfile),
1955 plongest (entry_addr - section->buffer));
1956 return false;
1957 }
1958 ULONGEST start = extract_unsigned_integer (addr, address_size,
1959 dwarf5_byte_order);
1960 addr += address_size;
1961 ULONGEST length = extract_unsigned_integer (addr, address_size,
1962 dwarf5_byte_order);
1963 addr += address_size;
1964 if (start == 0 && length == 0)
1965 {
1966 /* This can happen on some targets with --gc-sections.
1967 This pair of values is also used to mark the end of
1968 the entries for a given CU, but we ignore it and
1969 instead handle termination using the check at the top
1970 of the loop. */
1971 continue;
1972 }
1973 if (start == 0 && !per_bfd->has_section_at_zero)
1974 {
1975 /* Symbol was eliminated due to a COMDAT group. */
1976 continue;
1977 }
1978 ULONGEST end = start + length;
1979 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
1980 - baseaddr);
1981 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
1982 - baseaddr);
1983 mutable_map->set_empty (start, end - 1, per_cu);
1984 }
1985
1986 per_cu->addresses_seen = true;
1987 }
1988
1989 return true;
1990 }
1991
1992 /* die_reader_func for dw2_get_file_names. */
1993
1994 static void
1995 dw2_get_file_names_reader (const struct die_reader_specs *reader,
1996 struct die_info *comp_unit_die)
1997 {
1998 struct dwarf2_cu *cu = reader->cu;
1999 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2000 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2001 struct dwarf2_per_cu_data *lh_cu;
2002 struct attribute *attr;
2003 void **slot;
2004 struct quick_file_names *qfn;
2005
2006 gdb_assert (! this_cu->is_debug_types);
2007
2008 this_cu->files_read = true;
2009 /* Our callers never want to match partial units -- instead they
2010 will match the enclosing full CU. */
2011 if (comp_unit_die->tag == DW_TAG_partial_unit)
2012 return;
2013
2014 lh_cu = this_cu;
2015 slot = NULL;
2016
2017 line_header_up lh;
2018 sect_offset line_offset {};
2019
2020 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
2021
2022 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2023 if (attr != nullptr && attr->form_is_unsigned ())
2024 {
2025 struct quick_file_names find_entry;
2026
2027 line_offset = (sect_offset) attr->as_unsigned ();
2028
2029 /* We may have already read in this line header (TU line header sharing).
2030 If we have we're done. */
2031 find_entry.hash.dwo_unit = cu->dwo_unit;
2032 find_entry.hash.line_sect_off = line_offset;
2033 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2034 &find_entry, INSERT);
2035 if (*slot != NULL)
2036 {
2037 lh_cu->file_names = (struct quick_file_names *) *slot;
2038 return;
2039 }
2040
2041 lh = dwarf_decode_line_header (line_offset, cu, fnd.get_comp_dir ());
2042 }
2043
2044 int offset = 0;
2045 if (!fnd.is_unknown ())
2046 ++offset;
2047 else if (lh == nullptr)
2048 return;
2049
2050 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2051 qfn->hash.dwo_unit = cu->dwo_unit;
2052 qfn->hash.line_sect_off = line_offset;
2053 /* There may not be a DW_AT_stmt_list. */
2054 if (slot != nullptr)
2055 *slot = qfn;
2056
2057 std::vector<const char *> include_names;
2058 if (lh != nullptr)
2059 {
2060 for (const auto &entry : lh->file_names ())
2061 {
2062 std::string name_holder;
2063 const char *include_name =
2064 compute_include_file_name (lh.get (), entry, fnd, name_holder);
2065 if (include_name != nullptr)
2066 {
2067 include_name = per_objfile->objfile->intern (include_name);
2068 include_names.push_back (include_name);
2069 }
2070 }
2071 }
2072
2073 qfn->num_file_names = offset + include_names.size ();
2074 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
2075 qfn->file_names =
2076 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2077 qfn->num_file_names);
2078 if (offset != 0)
2079 qfn->file_names[0] = per_objfile->objfile->intern (fnd.get_name ());
2080
2081 if (!include_names.empty ())
2082 memcpy (&qfn->file_names[offset], include_names.data (),
2083 include_names.size () * sizeof (const char *));
2084
2085 qfn->real_names = NULL;
2086
2087 lh_cu->file_names = qfn;
2088 }
2089
2090 /* A helper for the "quick" functions which attempts to read the line
2091 table for THIS_CU. */
2092
2093 static struct quick_file_names *
2094 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2095 dwarf2_per_objfile *per_objfile)
2096 {
2097 /* This should never be called for TUs. */
2098 gdb_assert (! this_cu->is_debug_types);
2099
2100 if (this_cu->files_read)
2101 return this_cu->file_names;
2102
2103 cutu_reader reader (this_cu, per_objfile);
2104 if (!reader.dummy_p)
2105 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2106
2107 return this_cu->file_names;
2108 }
2109
2110 /* A helper for the "quick" functions which computes and caches the
2111 real path for a given file name from the line table. */
2112
2113 static const char *
2114 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2115 struct quick_file_names *qfn, int index)
2116 {
2117 if (qfn->real_names == NULL)
2118 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
2119 qfn->num_file_names, const char *);
2120
2121 if (qfn->real_names[index] == NULL)
2122 {
2123 const char *dirname = nullptr;
2124
2125 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
2126 dirname = qfn->comp_dir;
2127
2128 gdb::unique_xmalloc_ptr<char> fullname;
2129 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
2130
2131 qfn->real_names[index] = fullname.release ();
2132 }
2133
2134 return qfn->real_names[index];
2135 }
2136
2137 struct symtab *
2138 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
2139 {
2140 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2141 dwarf2_per_cu_data *dwarf_cu
2142 = per_objfile->per_bfd->all_units.back ().get ();
2143 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2144
2145 if (cust == NULL)
2146 return NULL;
2147
2148 return cust->primary_filetab ();
2149 }
2150
2151 /* See read.h. */
2152
2153 void
2154 dwarf2_per_cu_data::free_cached_file_names ()
2155 {
2156 if (fnd != nullptr)
2157 fnd->forget_fullname ();
2158
2159 if (per_bfd == nullptr)
2160 return;
2161
2162 struct quick_file_names *file_data = file_names;
2163 if (file_data != nullptr && file_data->real_names != nullptr)
2164 {
2165 for (int i = 0; i < file_data->num_file_names; ++i)
2166 {
2167 xfree ((void *) file_data->real_names[i]);
2168 file_data->real_names[i] = nullptr;
2169 }
2170 }
2171 }
2172
2173 void
2174 dwarf2_base_index_functions::forget_cached_source_info
2175 (struct objfile *objfile)
2176 {
2177 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2178
2179 for (auto &per_cu : per_objfile->per_bfd->all_units)
2180 per_cu->free_cached_file_names ();
2181 }
2182
2183 void
2184 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
2185 bool print_bcache)
2186 {
2187 if (print_bcache)
2188 return;
2189
2190 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2191 int total = per_objfile->per_bfd->all_units.size ();
2192 int count = 0;
2193
2194 for (int i = 0; i < total; ++i)
2195 {
2196 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2197
2198 if (!per_objfile->symtab_set_p (per_cu))
2199 ++count;
2200 }
2201 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
2202 gdb_printf (_(" Number of unread CUs: %d\n"), count);
2203 }
2204
2205 void
2206 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
2207 {
2208 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2209 int total_units = per_objfile->per_bfd->all_units.size ();
2210
2211 for (int i = 0; i < total_units; ++i)
2212 {
2213 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2214
2215 /* We don't want to directly expand a partial CU, because if we
2216 read it with the wrong language, then assertion failures can
2217 be triggered later on. See PR symtab/23010. So, tell
2218 dw2_instantiate_symtab to skip partial CUs -- any important
2219 partial CU will be read via DW_TAG_imported_unit anyway. */
2220 dw2_instantiate_symtab (per_cu, per_objfile, true);
2221 }
2222 }
2223
2224
2225 /* Starting from a search name, return the string that finds the upper
2226 bound of all strings that start with SEARCH_NAME in a sorted name
2227 list. Returns the empty string to indicate that the upper bound is
2228 the end of the list. */
2229
2230 static std::string
2231 make_sort_after_prefix_name (const char *search_name)
2232 {
2233 /* When looking to complete "func", we find the upper bound of all
2234 symbols that start with "func" by looking for where we'd insert
2235 the closest string that would follow "func" in lexicographical
2236 order. Usually, that's "func"-with-last-character-incremented,
2237 i.e. "fund". Mind non-ASCII characters, though. Usually those
2238 will be UTF-8 multi-byte sequences, but we can't be certain.
2239 Especially mind the 0xff character, which is a valid character in
2240 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
2241 rule out compilers allowing it in identifiers. Note that
2242 conveniently, strcmp/strcasecmp are specified to compare
2243 characters interpreted as unsigned char. So what we do is treat
2244 the whole string as a base 256 number composed of a sequence of
2245 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
2246 to 0, and carries 1 to the following more-significant position.
2247 If the very first character in SEARCH_NAME ends up incremented
2248 and carries/overflows, then the upper bound is the end of the
2249 list. The string after the empty string is also the empty
2250 string.
2251
2252 Some examples of this operation:
2253
2254 SEARCH_NAME => "+1" RESULT
2255
2256 "abc" => "abd"
2257 "ab\xff" => "ac"
2258 "\xff" "a" "\xff" => "\xff" "b"
2259 "\xff" => ""
2260 "\xff\xff" => ""
2261 "" => ""
2262
2263 Then, with these symbols for example:
2264
2265 func
2266 func1
2267 fund
2268
2269 completing "func" looks for symbols between "func" and
2270 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
2271 which finds "func" and "func1", but not "fund".
2272
2273 And with:
2274
2275 funcÿ (Latin1 'ÿ' [0xff])
2276 funcÿ1
2277 fund
2278
2279 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
2280 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
2281
2282 And with:
2283
2284 ÿÿ (Latin1 'ÿ' [0xff])
2285 ÿÿ1
2286
2287 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
2288 the end of the list.
2289 */
2290 std::string after = search_name;
2291 while (!after.empty () && (unsigned char) after.back () == 0xff)
2292 after.pop_back ();
2293 if (!after.empty ())
2294 after.back () = (unsigned char) after.back () + 1;
2295 return after;
2296 }
2297
2298 /* See declaration. */
2299
2300 std::pair<std::vector<name_component>::const_iterator,
2301 std::vector<name_component>::const_iterator>
2302 mapped_index_base::find_name_components_bounds
2303 (const lookup_name_info &lookup_name_without_params, language lang,
2304 dwarf2_per_objfile *per_objfile) const
2305 {
2306 auto *name_cmp
2307 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
2308
2309 const char *lang_name
2310 = lookup_name_without_params.language_lookup_name (lang);
2311
2312 /* Comparison function object for lower_bound that matches against a
2313 given symbol name. */
2314 auto lookup_compare_lower = [&] (const name_component &elem,
2315 const char *name)
2316 {
2317 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
2318 const char *elem_name = elem_qualified + elem.name_offset;
2319 return name_cmp (elem_name, name) < 0;
2320 };
2321
2322 /* Comparison function object for upper_bound that matches against a
2323 given symbol name. */
2324 auto lookup_compare_upper = [&] (const char *name,
2325 const name_component &elem)
2326 {
2327 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
2328 const char *elem_name = elem_qualified + elem.name_offset;
2329 return name_cmp (name, elem_name) < 0;
2330 };
2331
2332 auto begin = this->name_components.begin ();
2333 auto end = this->name_components.end ();
2334
2335 /* Find the lower bound. */
2336 auto lower = [&] ()
2337 {
2338 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
2339 return begin;
2340 else
2341 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
2342 } ();
2343
2344 /* Find the upper bound. */
2345 auto upper = [&] ()
2346 {
2347 if (lookup_name_without_params.completion_mode ())
2348 {
2349 /* In completion mode, we want UPPER to point past all
2350 symbols names that have the same prefix. I.e., with
2351 these symbols, and completing "func":
2352
2353 function << lower bound
2354 function1
2355 other_function << upper bound
2356
2357 We find the upper bound by looking for the insertion
2358 point of "func"-with-last-character-incremented,
2359 i.e. "fund". */
2360 std::string after = make_sort_after_prefix_name (lang_name);
2361 if (after.empty ())
2362 return end;
2363 return std::lower_bound (lower, end, after.c_str (),
2364 lookup_compare_lower);
2365 }
2366 else
2367 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
2368 } ();
2369
2370 return {lower, upper};
2371 }
2372
2373 /* See declaration. */
2374
2375 void
2376 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
2377 {
2378 if (!this->name_components.empty ())
2379 return;
2380
2381 this->name_components_casing = case_sensitivity;
2382 auto *name_cmp
2383 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
2384
2385 /* The code below only knows how to break apart components of C++
2386 symbol names (and other languages that use '::' as
2387 namespace/module separator) and Ada symbol names. */
2388 auto count = this->symbol_name_count ();
2389 for (offset_type idx = 0; idx < count; idx++)
2390 {
2391 if (this->symbol_name_slot_invalid (idx))
2392 continue;
2393
2394 const char *name = this->symbol_name_at (idx, per_objfile);
2395
2396 /* Add each name component to the name component table. */
2397 unsigned int previous_len = 0;
2398
2399 if (strstr (name, "::") != nullptr)
2400 {
2401 for (unsigned int current_len = cp_find_first_component (name);
2402 name[current_len] != '\0';
2403 current_len += cp_find_first_component (name + current_len))
2404 {
2405 gdb_assert (name[current_len] == ':');
2406 this->name_components.push_back ({previous_len, idx});
2407 /* Skip the '::'. */
2408 current_len += 2;
2409 previous_len = current_len;
2410 }
2411 }
2412 else
2413 {
2414 /* Handle the Ada encoded (aka mangled) form here. */
2415 for (const char *iter = strstr (name, "__");
2416 iter != nullptr;
2417 iter = strstr (iter, "__"))
2418 {
2419 this->name_components.push_back ({previous_len, idx});
2420 iter += 2;
2421 previous_len = iter - name;
2422 }
2423 }
2424
2425 this->name_components.push_back ({previous_len, idx});
2426 }
2427
2428 /* Sort name_components elements by name. */
2429 auto name_comp_compare = [&] (const name_component &left,
2430 const name_component &right)
2431 {
2432 const char *left_qualified
2433 = this->symbol_name_at (left.idx, per_objfile);
2434 const char *right_qualified
2435 = this->symbol_name_at (right.idx, per_objfile);
2436
2437 const char *left_name = left_qualified + left.name_offset;
2438 const char *right_name = right_qualified + right.name_offset;
2439
2440 return name_cmp (left_name, right_name) < 0;
2441 };
2442
2443 std::sort (this->name_components.begin (),
2444 this->name_components.end (),
2445 name_comp_compare);
2446 }
2447
2448 /* See read.h. */
2449
2450 bool
2451 dw2_expand_symtabs_matching_symbol
2452 (mapped_index_base &index,
2453 const lookup_name_info &lookup_name_in,
2454 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2455 gdb::function_view<bool (offset_type)> match_callback,
2456 dwarf2_per_objfile *per_objfile)
2457 {
2458 lookup_name_info lookup_name_without_params
2459 = lookup_name_in.make_ignore_params ();
2460
2461 /* Build the symbol name component sorted vector, if we haven't
2462 yet. */
2463 index.build_name_components (per_objfile);
2464
2465 /* The same symbol may appear more than once in the range though.
2466 E.g., if we're looking for symbols that complete "w", and we have
2467 a symbol named "w1::w2", we'll find the two name components for
2468 that same symbol in the range. To be sure we only call the
2469 callback once per symbol, we first collect the symbol name
2470 indexes that matched in a temporary vector and ignore
2471 duplicates. */
2472 std::vector<offset_type> matches;
2473
2474 struct name_and_matcher
2475 {
2476 symbol_name_matcher_ftype *matcher;
2477 const char *name;
2478
2479 bool operator== (const name_and_matcher &other) const
2480 {
2481 return matcher == other.matcher && strcmp (name, other.name) == 0;
2482 }
2483 };
2484
2485 /* A vector holding all the different symbol name matchers, for all
2486 languages. */
2487 std::vector<name_and_matcher> matchers;
2488
2489 for (int i = 0; i < nr_languages; i++)
2490 {
2491 enum language lang_e = (enum language) i;
2492
2493 const language_defn *lang = language_def (lang_e);
2494 symbol_name_matcher_ftype *name_matcher
2495 = lang->get_symbol_name_matcher (lookup_name_without_params);
2496
2497 name_and_matcher key {
2498 name_matcher,
2499 lookup_name_without_params.language_lookup_name (lang_e)
2500 };
2501
2502 /* Don't insert the same comparison routine more than once.
2503 Note that we do this linear walk. This is not a problem in
2504 practice because the number of supported languages is
2505 low. */
2506 if (std::find (matchers.begin (), matchers.end (), key)
2507 != matchers.end ())
2508 continue;
2509 matchers.push_back (std::move (key));
2510
2511 auto bounds
2512 = index.find_name_components_bounds (lookup_name_without_params,
2513 lang_e, per_objfile);
2514
2515 /* Now for each symbol name in range, check to see if we have a name
2516 match, and if so, call the MATCH_CALLBACK callback. */
2517
2518 for (; bounds.first != bounds.second; ++bounds.first)
2519 {
2520 const char *qualified
2521 = index.symbol_name_at (bounds.first->idx, per_objfile);
2522
2523 if (!name_matcher (qualified, lookup_name_without_params, NULL)
2524 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
2525 continue;
2526
2527 matches.push_back (bounds.first->idx);
2528 }
2529 }
2530
2531 std::sort (matches.begin (), matches.end ());
2532
2533 /* Finally call the callback, once per match. */
2534 ULONGEST prev = -1;
2535 bool result = true;
2536 for (offset_type idx : matches)
2537 {
2538 if (prev != idx)
2539 {
2540 if (!match_callback (idx))
2541 {
2542 result = false;
2543 break;
2544 }
2545 prev = idx;
2546 }
2547 }
2548
2549 /* Above we use a type wider than idx's for 'prev', since 0 and
2550 (offset_type)-1 are both possible values. */
2551 static_assert (sizeof (prev) > sizeof (offset_type), "");
2552
2553 return result;
2554 }
2555
2556 #if GDB_SELF_TEST
2557
2558 namespace selftests { namespace dw2_expand_symtabs_matching {
2559
2560 /* A mock .gdb_index/.debug_names-like name index table, enough to
2561 exercise dw2_expand_symtabs_matching_symbol, which works with the
2562 mapped_index_base interface. Builds an index from the symbol list
2563 passed as parameter to the constructor. */
2564 class mock_mapped_index : public mapped_index_base
2565 {
2566 public:
2567 mock_mapped_index (gdb::array_view<const char *> symbols)
2568 : m_symbol_table (symbols)
2569 {}
2570
2571 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
2572
2573 /* Return the number of names in the symbol table. */
2574 size_t symbol_name_count () const override
2575 {
2576 return m_symbol_table.size ();
2577 }
2578
2579 /* Get the name of the symbol at IDX in the symbol table. */
2580 const char *symbol_name_at
2581 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
2582 {
2583 return m_symbol_table[idx];
2584 }
2585
2586 quick_symbol_functions_up make_quick_functions () const override
2587 {
2588 return nullptr;
2589 }
2590
2591 private:
2592 gdb::array_view<const char *> m_symbol_table;
2593 };
2594
2595 /* Convenience function that converts a NULL pointer to a "<null>"
2596 string, to pass to print routines. */
2597
2598 static const char *
2599 string_or_null (const char *str)
2600 {
2601 return str != NULL ? str : "<null>";
2602 }
2603
2604 /* Check if a lookup_name_info built from
2605 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
2606 index. EXPECTED_LIST is the list of expected matches, in expected
2607 matching order. If no match expected, then an empty list is
2608 specified. Returns true on success. On failure prints a warning
2609 indicating the file:line that failed, and returns false. */
2610
2611 static bool
2612 check_match (const char *file, int line,
2613 mock_mapped_index &mock_index,
2614 const char *name, symbol_name_match_type match_type,
2615 bool completion_mode,
2616 std::initializer_list<const char *> expected_list,
2617 dwarf2_per_objfile *per_objfile)
2618 {
2619 lookup_name_info lookup_name (name, match_type, completion_mode);
2620
2621 bool matched = true;
2622
2623 auto mismatch = [&] (const char *expected_str,
2624 const char *got)
2625 {
2626 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
2627 "expected=\"%s\", got=\"%s\"\n"),
2628 file, line,
2629 (match_type == symbol_name_match_type::FULL
2630 ? "FULL" : "WILD"),
2631 name, string_or_null (expected_str), string_or_null (got));
2632 matched = false;
2633 };
2634
2635 auto expected_it = expected_list.begin ();
2636 auto expected_end = expected_list.end ();
2637
2638 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
2639 nullptr,
2640 [&] (offset_type idx)
2641 {
2642 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
2643 const char *expected_str
2644 = expected_it == expected_end ? NULL : *expected_it++;
2645
2646 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
2647 mismatch (expected_str, matched_name);
2648 return true;
2649 }, per_objfile);
2650
2651 const char *expected_str
2652 = expected_it == expected_end ? NULL : *expected_it++;
2653 if (expected_str != NULL)
2654 mismatch (expected_str, NULL);
2655
2656 return matched;
2657 }
2658
2659 /* The symbols added to the mock mapped_index for testing (in
2660 canonical form). */
2661 static const char *test_symbols[] = {
2662 "function",
2663 "std::bar",
2664 "std::zfunction",
2665 "std::zfunction2",
2666 "w1::w2",
2667 "ns::foo<char*>",
2668 "ns::foo<int>",
2669 "ns::foo<long>",
2670 "ns2::tmpl<int>::foo2",
2671 "(anonymous namespace)::A::B::C",
2672
2673 /* These are used to check that the increment-last-char in the
2674 matching algorithm for completion doesn't match "t1_fund" when
2675 completing "t1_func". */
2676 "t1_func",
2677 "t1_func1",
2678 "t1_fund",
2679 "t1_fund1",
2680
2681 /* A UTF-8 name with multi-byte sequences to make sure that
2682 cp-name-parser understands this as a single identifier ("função"
2683 is "function" in PT). */
2684 u8"u8função",
2685
2686 /* Test a symbol name that ends with a 0xff character, which is a
2687 valid character in non-UTF-8 source character sets (e.g. Latin1
2688 'ÿ'), and we can't rule out compilers allowing it in identifiers.
2689 We test this because the completion algorithm finds the upper
2690 bound of symbols by looking for the insertion point of
2691 "func"-with-last-character-incremented, i.e. "fund", and adding 1
2692 to 0xff should wraparound and carry to the previous character.
2693 See comments in make_sort_after_prefix_name. */
2694 "yfunc\377",
2695
2696 /* Some more symbols with \377 (0xff). See above. */
2697 "\377",
2698 "\377\377123",
2699
2700 /* A name with all sorts of complications. Starts with "z" to make
2701 it easier for the completion tests below. */
2702 #define Z_SYM_NAME \
2703 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
2704 "::tuple<(anonymous namespace)::ui*, " \
2705 "std::default_delete<(anonymous namespace)::ui>, void>"
2706
2707 Z_SYM_NAME
2708 };
2709
2710 /* Returns true if the mapped_index_base::find_name_component_bounds
2711 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
2712 in completion mode. */
2713
2714 static bool
2715 check_find_bounds_finds (mapped_index_base &index,
2716 const char *search_name,
2717 gdb::array_view<const char *> expected_syms,
2718 dwarf2_per_objfile *per_objfile)
2719 {
2720 lookup_name_info lookup_name (search_name,
2721 symbol_name_match_type::FULL, true);
2722
2723 auto bounds = index.find_name_components_bounds (lookup_name,
2724 language_cplus,
2725 per_objfile);
2726
2727 size_t distance = std::distance (bounds.first, bounds.second);
2728 if (distance != expected_syms.size ())
2729 return false;
2730
2731 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
2732 {
2733 auto nc_elem = bounds.first + exp_elem;
2734 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
2735 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
2736 return false;
2737 }
2738
2739 return true;
2740 }
2741
2742 /* Test the lower-level mapped_index::find_name_component_bounds
2743 method. */
2744
2745 static void
2746 test_mapped_index_find_name_component_bounds ()
2747 {
2748 mock_mapped_index mock_index (test_symbols);
2749
2750 mock_index.build_name_components (NULL /* per_objfile */);
2751
2752 /* Test the lower-level mapped_index::find_name_component_bounds
2753 method in completion mode. */
2754 {
2755 static const char *expected_syms[] = {
2756 "t1_func",
2757 "t1_func1",
2758 };
2759
2760 SELF_CHECK (check_find_bounds_finds
2761 (mock_index, "t1_func", expected_syms,
2762 NULL /* per_objfile */));
2763 }
2764
2765 /* Check that the increment-last-char in the name matching algorithm
2766 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
2767 make_sort_after_prefix_name. */
2768 {
2769 static const char *expected_syms1[] = {
2770 "\377",
2771 "\377\377123",
2772 };
2773 SELF_CHECK (check_find_bounds_finds
2774 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
2775
2776 static const char *expected_syms2[] = {
2777 "\377\377123",
2778 };
2779 SELF_CHECK (check_find_bounds_finds
2780 (mock_index, "\377\377", expected_syms2,
2781 NULL /* per_objfile */));
2782 }
2783 }
2784
2785 /* Test dw2_expand_symtabs_matching_symbol. */
2786
2787 static void
2788 test_dw2_expand_symtabs_matching_symbol ()
2789 {
2790 mock_mapped_index mock_index (test_symbols);
2791
2792 /* We let all tests run until the end even if some fails, for debug
2793 convenience. */
2794 bool any_mismatch = false;
2795
2796 /* Create the expected symbols list (an initializer_list). Needed
2797 because lists have commas, and we need to pass them to CHECK,
2798 which is a macro. */
2799 #define EXPECT(...) { __VA_ARGS__ }
2800
2801 /* Wrapper for check_match that passes down the current
2802 __FILE__/__LINE__. */
2803 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
2804 any_mismatch |= !check_match (__FILE__, __LINE__, \
2805 mock_index, \
2806 NAME, MATCH_TYPE, COMPLETION_MODE, \
2807 EXPECTED_LIST, NULL)
2808
2809 /* Identity checks. */
2810 for (const char *sym : test_symbols)
2811 {
2812 /* Should be able to match all existing symbols. */
2813 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
2814 EXPECT (sym));
2815
2816 /* Should be able to match all existing symbols with
2817 parameters. */
2818 std::string with_params = std::string (sym) + "(int)";
2819 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2820 EXPECT (sym));
2821
2822 /* Should be able to match all existing symbols with
2823 parameters and qualifiers. */
2824 with_params = std::string (sym) + " ( int ) const";
2825 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2826 EXPECT (sym));
2827
2828 /* This should really find sym, but cp-name-parser.y doesn't
2829 know about lvalue/rvalue qualifiers yet. */
2830 with_params = std::string (sym) + " ( int ) &&";
2831 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
2832 {});
2833 }
2834
2835 /* Check that the name matching algorithm for completion doesn't get
2836 confused with Latin1 'ÿ' / 0xff. See
2837 make_sort_after_prefix_name. */
2838 {
2839 static const char str[] = "\377";
2840 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
2841 EXPECT ("\377", "\377\377123"));
2842 }
2843
2844 /* Check that the increment-last-char in the matching algorithm for
2845 completion doesn't match "t1_fund" when completing "t1_func". */
2846 {
2847 static const char str[] = "t1_func";
2848 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
2849 EXPECT ("t1_func", "t1_func1"));
2850 }
2851
2852 /* Check that completion mode works at each prefix of the expected
2853 symbol name. */
2854 {
2855 static const char str[] = "function(int)";
2856 size_t len = strlen (str);
2857 std::string lookup;
2858
2859 for (size_t i = 1; i < len; i++)
2860 {
2861 lookup.assign (str, i);
2862 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
2863 EXPECT ("function"));
2864 }
2865 }
2866
2867 /* While "w" is a prefix of both components, the match function
2868 should still only be called once. */
2869 {
2870 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
2871 EXPECT ("w1::w2"));
2872 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
2873 EXPECT ("w1::w2"));
2874 }
2875
2876 /* Same, with a "complicated" symbol. */
2877 {
2878 static const char str[] = Z_SYM_NAME;
2879 size_t len = strlen (str);
2880 std::string lookup;
2881
2882 for (size_t i = 1; i < len; i++)
2883 {
2884 lookup.assign (str, i);
2885 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
2886 EXPECT (Z_SYM_NAME));
2887 }
2888 }
2889
2890 /* In FULL mode, an incomplete symbol doesn't match. */
2891 {
2892 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
2893 {});
2894 }
2895
2896 /* A complete symbol with parameters matches any overload, since the
2897 index has no overload info. */
2898 {
2899 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
2900 EXPECT ("std::zfunction", "std::zfunction2"));
2901 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
2902 EXPECT ("std::zfunction", "std::zfunction2"));
2903 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
2904 EXPECT ("std::zfunction", "std::zfunction2"));
2905 }
2906
2907 /* Check that whitespace is ignored appropriately. A symbol with a
2908 template argument list. */
2909 {
2910 static const char expected[] = "ns::foo<int>";
2911 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
2912 EXPECT (expected));
2913 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
2914 EXPECT (expected));
2915 }
2916
2917 /* Check that whitespace is ignored appropriately. A symbol with a
2918 template argument list that includes a pointer. */
2919 {
2920 static const char expected[] = "ns::foo<char*>";
2921 /* Try both completion and non-completion modes. */
2922 static const bool completion_mode[2] = {false, true};
2923 for (size_t i = 0; i < 2; i++)
2924 {
2925 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
2926 completion_mode[i], EXPECT (expected));
2927 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
2928 completion_mode[i], EXPECT (expected));
2929
2930 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
2931 completion_mode[i], EXPECT (expected));
2932 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
2933 completion_mode[i], EXPECT (expected));
2934 }
2935 }
2936
2937 {
2938 /* Check method qualifiers are ignored. */
2939 static const char expected[] = "ns::foo<char*>";
2940 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
2941 symbol_name_match_type::FULL, true, EXPECT (expected));
2942 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
2943 symbol_name_match_type::FULL, true, EXPECT (expected));
2944 CHECK_MATCH ("foo < char * > ( int ) const",
2945 symbol_name_match_type::WILD, true, EXPECT (expected));
2946 CHECK_MATCH ("foo < char * > ( int ) &&",
2947 symbol_name_match_type::WILD, true, EXPECT (expected));
2948 }
2949
2950 /* Test lookup names that don't match anything. */
2951 {
2952 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
2953 {});
2954
2955 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
2956 {});
2957 }
2958
2959 /* Some wild matching tests, exercising "(anonymous namespace)",
2960 which should not be confused with a parameter list. */
2961 {
2962 static const char *syms[] = {
2963 "A::B::C",
2964 "B::C",
2965 "C",
2966 "A :: B :: C ( int )",
2967 "B :: C ( int )",
2968 "C ( int )",
2969 };
2970
2971 for (const char *s : syms)
2972 {
2973 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
2974 EXPECT ("(anonymous namespace)::A::B::C"));
2975 }
2976 }
2977
2978 {
2979 static const char expected[] = "ns2::tmpl<int>::foo2";
2980 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
2981 EXPECT (expected));
2982 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
2983 EXPECT (expected));
2984 }
2985
2986 SELF_CHECK (!any_mismatch);
2987
2988 #undef EXPECT
2989 #undef CHECK_MATCH
2990 }
2991
2992 static void
2993 run_test ()
2994 {
2995 test_mapped_index_find_name_component_bounds ();
2996 test_dw2_expand_symtabs_matching_symbol ();
2997 }
2998
2999 }} // namespace selftests::dw2_expand_symtabs_matching
3000
3001 #endif /* GDB_SELF_TEST */
3002
3003 /* See read.h. */
3004
3005 bool
3006 dw2_expand_symtabs_matching_one
3007 (dwarf2_per_cu_data *per_cu,
3008 dwarf2_per_objfile *per_objfile,
3009 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3010 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
3011 {
3012 if (file_matcher == NULL || per_cu->mark)
3013 {
3014 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
3015
3016 compunit_symtab *symtab
3017 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3018 gdb_assert (symtab != nullptr);
3019
3020 if (expansion_notify != NULL && symtab_was_null)
3021 return expansion_notify (symtab);
3022 }
3023 return true;
3024 }
3025
3026 /* See read.h. */
3027
3028 void
3029 dw_expand_symtabs_matching_file_matcher
3030 (dwarf2_per_objfile *per_objfile,
3031 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
3032 {
3033 if (file_matcher == NULL)
3034 return;
3035
3036 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
3037 htab_eq_pointer,
3038 NULL, xcalloc, xfree));
3039 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
3040 htab_eq_pointer,
3041 NULL, xcalloc, xfree));
3042
3043 /* The rule is CUs specify all the files, including those used by
3044 any TU, so there's no need to scan TUs here. */
3045
3046 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3047 {
3048 QUIT;
3049
3050 if (per_cu->is_debug_types)
3051 continue;
3052 per_cu->mark = 0;
3053
3054 /* We only need to look at symtabs not already expanded. */
3055 if (per_objfile->symtab_set_p (per_cu.get ()))
3056 continue;
3057
3058 if (per_cu->fnd != nullptr)
3059 {
3060 file_and_directory *fnd = per_cu->fnd.get ();
3061
3062 if (file_matcher (fnd->get_name (), false))
3063 {
3064 per_cu->mark = 1;
3065 continue;
3066 }
3067
3068 /* Before we invoke realpath, which can get expensive when many
3069 files are involved, do a quick comparison of the basenames. */
3070 if ((basenames_may_differ
3071 || file_matcher (lbasename (fnd->get_name ()), true))
3072 && file_matcher (fnd->get_fullname (), false))
3073 {
3074 per_cu->mark = 1;
3075 continue;
3076 }
3077 }
3078
3079 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
3080 per_objfile);
3081 if (file_data == NULL)
3082 continue;
3083
3084 if (htab_find (visited_not_found.get (), file_data) != NULL)
3085 continue;
3086 else if (htab_find (visited_found.get (), file_data) != NULL)
3087 {
3088 per_cu->mark = 1;
3089 continue;
3090 }
3091
3092 for (int j = 0; j < file_data->num_file_names; ++j)
3093 {
3094 const char *this_real_name;
3095
3096 if (file_matcher (file_data->file_names[j], false))
3097 {
3098 per_cu->mark = 1;
3099 break;
3100 }
3101
3102 /* Before we invoke realpath, which can get expensive when many
3103 files are involved, do a quick comparison of the basenames. */
3104 if (!basenames_may_differ
3105 && !file_matcher (lbasename (file_data->file_names[j]),
3106 true))
3107 continue;
3108
3109 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3110 if (file_matcher (this_real_name, false))
3111 {
3112 per_cu->mark = 1;
3113 break;
3114 }
3115 }
3116
3117 void **slot = htab_find_slot (per_cu->mark
3118 ? visited_found.get ()
3119 : visited_not_found.get (),
3120 file_data, INSERT);
3121 *slot = file_data;
3122 }
3123 }
3124
3125
3126 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
3127 symtab. */
3128
3129 static struct compunit_symtab *
3130 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
3131 CORE_ADDR pc)
3132 {
3133 int i;
3134
3135 if (cust->blockvector () != nullptr
3136 && blockvector_contains_pc (cust->blockvector (), pc))
3137 return cust;
3138
3139 if (cust->includes == NULL)
3140 return NULL;
3141
3142 for (i = 0; cust->includes[i]; ++i)
3143 {
3144 struct compunit_symtab *s = cust->includes[i];
3145
3146 s = recursively_find_pc_sect_compunit_symtab (s, pc);
3147 if (s != NULL)
3148 return s;
3149 }
3150
3151 return NULL;
3152 }
3153
3154 dwarf2_per_cu_data *
3155 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
3156 CORE_ADDR adjusted_pc)
3157 {
3158 if (per_bfd->index_addrmap == nullptr)
3159 return nullptr;
3160
3161 void *obj = per_bfd->index_addrmap->find (adjusted_pc);
3162 return static_cast<dwarf2_per_cu_data *> (obj);
3163 }
3164
3165 struct compunit_symtab *
3166 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
3167 (struct objfile *objfile,
3168 struct bound_minimal_symbol msymbol,
3169 CORE_ADDR pc,
3170 struct obj_section *section,
3171 int warn_if_readin)
3172 {
3173 struct compunit_symtab *result;
3174
3175 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3176
3177 CORE_ADDR baseaddr = objfile->text_section_offset ();
3178 struct dwarf2_per_cu_data *data = find_per_cu (per_objfile->per_bfd,
3179 pc - baseaddr);
3180 if (data == nullptr)
3181 return nullptr;
3182
3183 if (warn_if_readin && per_objfile->symtab_set_p (data))
3184 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3185 paddress (objfile->arch (), pc));
3186
3187 result = recursively_find_pc_sect_compunit_symtab
3188 (dw2_instantiate_symtab (data, per_objfile, false), pc);
3189
3190 if (warn_if_readin && result == nullptr)
3191 warning (_("(Error: pc %s in address map, but not in symtab.)"),
3192 paddress (objfile->arch (), pc));
3193
3194 return result;
3195 }
3196
3197 void
3198 dwarf2_base_index_functions::map_symbol_filenames
3199 (struct objfile *objfile,
3200 gdb::function_view<symbol_filename_ftype> fun,
3201 bool need_fullname)
3202 {
3203 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3204
3205 /* Use caches to ensure we only call FUN once for each filename. */
3206 filename_seen_cache filenames_cache;
3207 std::unordered_set<quick_file_names *> qfn_cache;
3208
3209 /* The rule is CUs specify all the files, including those used by any TU,
3210 so there's no need to scan TUs here. We can ignore file names coming
3211 from already-expanded CUs. It is possible that an expanded CU might
3212 reuse the file names data from a currently unexpanded CU, in this
3213 case we don't want to report the files from the unexpanded CU. */
3214
3215 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3216 {
3217 if (!per_cu->is_debug_types
3218 && per_objfile->symtab_set_p (per_cu.get ()))
3219 {
3220 if (per_cu->file_names != nullptr)
3221 qfn_cache.insert (per_cu->file_names);
3222 }
3223 }
3224
3225 for (dwarf2_per_cu_data *per_cu
3226 : all_units_range (per_objfile->per_bfd))
3227 {
3228 /* We only need to look at symtabs not already expanded. */
3229 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
3230 continue;
3231
3232 if (per_cu->fnd != nullptr)
3233 {
3234 file_and_directory *fnd = per_cu->fnd.get ();
3235
3236 const char *filename = fnd->get_name ();
3237 const char *key = filename;
3238 const char *fullname = nullptr;
3239
3240 if (need_fullname)
3241 {
3242 fullname = fnd->get_fullname ();
3243 key = fullname;
3244 }
3245
3246 if (!filenames_cache.seen (key))
3247 fun (filename, fullname);
3248 }
3249
3250 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3251 if (file_data == nullptr
3252 || qfn_cache.find (file_data) != qfn_cache.end ())
3253 continue;
3254
3255 for (int j = 0; j < file_data->num_file_names; ++j)
3256 {
3257 const char *filename = file_data->file_names[j];
3258 const char *key = filename;
3259 const char *fullname = nullptr;
3260
3261 if (need_fullname)
3262 {
3263 fullname = dw2_get_real_path (per_objfile, file_data, j);
3264 key = fullname;
3265 }
3266
3267 if (!filenames_cache.seen (key))
3268 fun (filename, fullname);
3269 }
3270 }
3271 }
3272
3273 bool
3274 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
3275 {
3276 return true;
3277 }
3278
3279 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
3280
3281 bool
3282 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
3283 {
3284 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3285
3286 for (const auto &per_cu : per_objfile->per_bfd->all_units)
3287 {
3288 /* Is this already expanded? */
3289 if (per_objfile->symtab_set_p (per_cu.get ()))
3290 continue;
3291
3292 /* It has not yet been expanded. */
3293 return true;
3294 }
3295
3296 return false;
3297 }
3298
3299 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
3300 to either a dwarf2_per_bfd or dwz_file object. */
3301
3302 template <typename T>
3303 static gdb::array_view<const gdb_byte>
3304 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
3305 {
3306 dwarf2_section_info *section = &section_owner->gdb_index;
3307
3308 if (section->empty ())
3309 return {};
3310
3311 /* Older elfutils strip versions could keep the section in the main
3312 executable while splitting it for the separate debug info file. */
3313 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
3314 return {};
3315
3316 section->read (obj);
3317
3318 /* dwarf2_section_info::size is a bfd_size_type, while
3319 gdb::array_view works with size_t. On 32-bit hosts, with
3320 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
3321 is 32-bit. So we need an explicit narrowing conversion here.
3322 This is fine, because it's impossible to allocate or mmap an
3323 array/buffer larger than what size_t can represent. */
3324 return gdb::make_array_view (section->buffer, section->size);
3325 }
3326
3327 /* Lookup the index cache for the contents of the index associated to
3328 DWARF2_OBJ. */
3329
3330 static gdb::array_view<const gdb_byte>
3331 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
3332 {
3333 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
3334 if (build_id == nullptr)
3335 return {};
3336
3337 return global_index_cache.lookup_gdb_index (build_id,
3338 &dwarf2_per_bfd->index_cache_res);
3339 }
3340
3341 /* Same as the above, but for DWZ. */
3342
3343 static gdb::array_view<const gdb_byte>
3344 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
3345 {
3346 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
3347 if (build_id == nullptr)
3348 return {};
3349
3350 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
3351 }
3352
3353 static quick_symbol_functions_up make_cooked_index_funcs ();
3354
3355 /* See dwarf2/public.h. */
3356
3357 void
3358 dwarf2_initialize_objfile (struct objfile *objfile)
3359 {
3360 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3361 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3362
3363 dwarf_read_debug_printf ("called");
3364
3365 /* If we're about to read full symbols, don't bother with the
3366 indices. In this case we also don't care if some other debug
3367 format is making psymtabs, because they are all about to be
3368 expanded anyway. */
3369 if ((objfile->flags & OBJF_READNOW))
3370 {
3371 dwarf_read_debug_printf ("readnow requested");
3372
3373 create_all_units (per_objfile);
3374 per_bfd->quick_file_names_table
3375 = create_quick_file_names_table (per_bfd->all_units.size ());
3376
3377 objfile->qf.emplace_front (new readnow_functions);
3378 return;
3379 }
3380
3381 /* Was a GDB index already read when we processed an objfile sharing
3382 PER_BFD? */
3383 if (per_bfd->index_table != nullptr)
3384 {
3385 dwarf_read_debug_printf ("re-using symbols");
3386 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3387 return;
3388 }
3389
3390 if (dwarf2_read_debug_names (per_objfile))
3391 {
3392 dwarf_read_debug_printf ("found debug names");
3393 objfile->qf.push_front
3394 (per_bfd->index_table->make_quick_functions ());
3395 return;
3396 }
3397
3398 if (dwarf2_read_gdb_index (per_objfile,
3399 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
3400 get_gdb_index_contents_from_section<dwz_file>))
3401 {
3402 dwarf_read_debug_printf ("found gdb index from file");
3403 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3404 return;
3405 }
3406
3407 /* ... otherwise, try to find the index in the index cache. */
3408 if (dwarf2_read_gdb_index (per_objfile,
3409 get_gdb_index_contents_from_cache,
3410 get_gdb_index_contents_from_cache_dwz))
3411 {
3412 dwarf_read_debug_printf ("found gdb index from cache");
3413 global_index_cache.hit ();
3414 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
3415 return;
3416 }
3417
3418 global_index_cache.miss ();
3419 objfile->qf.push_front (make_cooked_index_funcs ());
3420 }
3421
3422 \f
3423
3424 /* Build a partial symbol table. */
3425
3426 static void
3427 dwarf2_build_psymtabs (struct objfile *objfile)
3428 {
3429 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3430
3431 if (per_objfile->per_bfd->index_table != nullptr)
3432 return;
3433
3434 try
3435 {
3436 dwarf2_build_psymtabs_hard (per_objfile);
3437 }
3438 catch (const gdb_exception_error &except)
3439 {
3440 exception_print (gdb_stderr, except);
3441 }
3442 }
3443
3444 /* Find the base address of the compilation unit for range lists and
3445 location lists. It will normally be specified by DW_AT_low_pc.
3446 In DWARF-3 draft 4, the base address could be overridden by
3447 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3448 compilation units with discontinuous ranges. */
3449
3450 static void
3451 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3452 {
3453 struct attribute *attr;
3454
3455 cu->base_address.reset ();
3456
3457 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3458 if (attr != nullptr)
3459 cu->base_address = attr->as_address ();
3460 else
3461 {
3462 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3463 if (attr != nullptr)
3464 cu->base_address = attr->as_address ();
3465 }
3466 }
3467
3468 /* Helper function that returns the proper abbrev section for
3469 THIS_CU. */
3470
3471 static struct dwarf2_section_info *
3472 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3473 {
3474 struct dwarf2_section_info *abbrev;
3475 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
3476
3477 if (this_cu->is_dwz)
3478 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
3479 else
3480 abbrev = &per_bfd->abbrev;
3481
3482 return abbrev;
3483 }
3484
3485 /* Fetch the abbreviation table offset from a comp or type unit header. */
3486
3487 static sect_offset
3488 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
3489 struct dwarf2_section_info *section,
3490 sect_offset sect_off)
3491 {
3492 bfd *abfd = section->get_bfd_owner ();
3493 const gdb_byte *info_ptr;
3494 unsigned int initial_length_size, offset_size;
3495 uint16_t version;
3496
3497 section->read (per_objfile->objfile);
3498 info_ptr = section->buffer + to_underlying (sect_off);
3499 read_initial_length (abfd, info_ptr, &initial_length_size);
3500 offset_size = initial_length_size == 4 ? 4 : 8;
3501 info_ptr += initial_length_size;
3502
3503 version = read_2_bytes (abfd, info_ptr);
3504 info_ptr += 2;
3505 if (version >= 5)
3506 {
3507 /* Skip unit type and address size. */
3508 info_ptr += 2;
3509 }
3510
3511 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
3512 }
3513
3514 static hashval_t
3515 hash_signatured_type (const void *item)
3516 {
3517 const struct signatured_type *sig_type
3518 = (const struct signatured_type *) item;
3519
3520 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3521 return sig_type->signature;
3522 }
3523
3524 static int
3525 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3526 {
3527 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
3528 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
3529
3530 return lhs->signature == rhs->signature;
3531 }
3532
3533 /* See read.h. */
3534
3535 htab_up
3536 allocate_signatured_type_table ()
3537 {
3538 return htab_up (htab_create_alloc (41,
3539 hash_signatured_type,
3540 eq_signatured_type,
3541 NULL, xcalloc, xfree));
3542 }
3543
3544 /* A helper for create_debug_types_hash_table. Read types from SECTION
3545 and fill them into TYPES_HTAB. It will process only type units,
3546 therefore DW_UT_type. */
3547
3548 static void
3549 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
3550 struct dwo_file *dwo_file,
3551 dwarf2_section_info *section, htab_up &types_htab,
3552 rcuh_kind section_kind)
3553 {
3554 struct objfile *objfile = per_objfile->objfile;
3555 struct dwarf2_section_info *abbrev_section;
3556 bfd *abfd;
3557 const gdb_byte *info_ptr, *end_ptr;
3558
3559 abbrev_section = &dwo_file->sections.abbrev;
3560
3561 dwarf_read_debug_printf ("Reading %s for %s",
3562 section->get_name (),
3563 abbrev_section->get_file_name ());
3564
3565 section->read (objfile);
3566 info_ptr = section->buffer;
3567
3568 if (info_ptr == NULL)
3569 return;
3570
3571 /* We can't set abfd until now because the section may be empty or
3572 not present, in which case the bfd is unknown. */
3573 abfd = section->get_bfd_owner ();
3574
3575 /* We don't use cutu_reader here because we don't need to read
3576 any dies: the signature is in the header. */
3577
3578 end_ptr = info_ptr + section->size;
3579 while (info_ptr < end_ptr)
3580 {
3581 signatured_type_up sig_type;
3582 struct dwo_unit *dwo_tu;
3583 void **slot;
3584 const gdb_byte *ptr = info_ptr;
3585 struct comp_unit_head header;
3586 unsigned int length;
3587
3588 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
3589
3590 /* Initialize it due to a false compiler warning. */
3591 header.signature = -1;
3592 header.type_cu_offset_in_tu = (cu_offset) -1;
3593
3594 /* We need to read the type's signature in order to build the hash
3595 table, but we don't need anything else just yet. */
3596
3597 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
3598 abbrev_section, ptr, section_kind);
3599
3600 length = header.get_length_with_initial ();
3601
3602 /* Skip dummy type units. */
3603 if (ptr >= info_ptr + length
3604 || peek_abbrev_code (abfd, ptr) == 0
3605 || (header.unit_type != DW_UT_type
3606 && header.unit_type != DW_UT_split_type))
3607 {
3608 info_ptr += length;
3609 continue;
3610 }
3611
3612 if (types_htab == NULL)
3613 types_htab = allocate_dwo_unit_table ();
3614
3615 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
3616 dwo_tu->dwo_file = dwo_file;
3617 dwo_tu->signature = header.signature;
3618 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
3619 dwo_tu->section = section;
3620 dwo_tu->sect_off = sect_off;
3621 dwo_tu->length = length;
3622
3623 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
3624 gdb_assert (slot != NULL);
3625 if (*slot != NULL)
3626 complaint (_("debug type entry at offset %s is duplicate to"
3627 " the entry at offset %s, signature %s"),
3628 sect_offset_str (sect_off),
3629 sect_offset_str (dwo_tu->sect_off),
3630 hex_string (header.signature));
3631 *slot = dwo_tu;
3632
3633 dwarf_read_debug_printf_v (" offset %s, signature %s",
3634 sect_offset_str (sect_off),
3635 hex_string (header.signature));
3636
3637 info_ptr += length;
3638 }
3639 }
3640
3641 /* Create the hash table of all entries in the .debug_types
3642 (or .debug_types.dwo) section(s).
3643 DWO_FILE is a pointer to the DWO file object.
3644
3645 The result is a pointer to the hash table or NULL if there are no types.
3646
3647 Note: This function processes DWO files only, not DWP files. */
3648
3649 static void
3650 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
3651 struct dwo_file *dwo_file,
3652 gdb::array_view<dwarf2_section_info> type_sections,
3653 htab_up &types_htab)
3654 {
3655 for (dwarf2_section_info &section : type_sections)
3656 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
3657 rcuh_kind::TYPE);
3658 }
3659
3660 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
3661 If SLOT is non-NULL, it is the entry to use in the hash table.
3662 Otherwise we find one. */
3663
3664 static struct signatured_type *
3665 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
3666 {
3667 if (per_objfile->per_bfd->all_units.size ()
3668 == per_objfile->per_bfd->all_units.capacity ())
3669 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
3670
3671 signatured_type_up sig_type_holder
3672 = per_objfile->per_bfd->allocate_signatured_type (sig);
3673 signatured_type *sig_type = sig_type_holder.get ();
3674
3675 per_objfile->per_bfd->all_units.emplace_back
3676 (sig_type_holder.release ());
3677
3678 if (slot == NULL)
3679 {
3680 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3681 sig_type, INSERT);
3682 }
3683 gdb_assert (*slot == NULL);
3684 *slot = sig_type;
3685 /* The rest of sig_type must be filled in by the caller. */
3686 return sig_type;
3687 }
3688
3689 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
3690 Fill in SIG_ENTRY with DWO_ENTRY. */
3691
3692 static void
3693 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
3694 struct signatured_type *sig_entry,
3695 struct dwo_unit *dwo_entry)
3696 {
3697 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3698
3699 /* Make sure we're not clobbering something we don't expect to. */
3700 gdb_assert (! sig_entry->queued);
3701 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
3702 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
3703 gdb_assert (sig_entry->signature == dwo_entry->signature);
3704 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
3705 || (to_underlying (sig_entry->type_offset_in_section)
3706 == to_underlying (dwo_entry->type_offset_in_tu)));
3707 gdb_assert (sig_entry->type_unit_group == NULL);
3708 gdb_assert (sig_entry->dwo_unit == NULL
3709 || sig_entry->dwo_unit == dwo_entry);
3710
3711 sig_entry->section = dwo_entry->section;
3712 sig_entry->sect_off = dwo_entry->sect_off;
3713 sig_entry->set_length (dwo_entry->length, false);
3714 sig_entry->reading_dwo_directly = 1;
3715 sig_entry->per_bfd = per_bfd;
3716 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
3717 sig_entry->dwo_unit = dwo_entry;
3718 }
3719
3720 /* Subroutine of lookup_signatured_type.
3721 If we haven't read the TU yet, create the signatured_type data structure
3722 for a TU to be read in directly from a DWO file, bypassing the stub.
3723 This is the "Stay in DWO Optimization": When there is no DWP file and we're
3724 using .gdb_index, then when reading a CU we want to stay in the DWO file
3725 containing that CU. Otherwise we could end up reading several other DWO
3726 files (due to comdat folding) to process the transitive closure of all the
3727 mentioned TUs, and that can be slow. The current DWO file will have every
3728 type signature that it needs.
3729 We only do this for .gdb_index because in the psymtab case we already have
3730 to read all the DWOs to build the type unit groups. */
3731
3732 static struct signatured_type *
3733 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3734 {
3735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3736 struct dwo_file *dwo_file;
3737 struct dwo_unit find_dwo_entry, *dwo_entry;
3738 void **slot;
3739
3740 gdb_assert (cu->dwo_unit);
3741
3742 /* If TU skeletons have been removed then we may not have read in any
3743 TUs yet. */
3744 if (per_objfile->per_bfd->signatured_types == NULL)
3745 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
3746
3747 /* We only ever need to read in one copy of a signatured type.
3748 Use the global signatured_types array to do our own comdat-folding
3749 of types. If this is the first time we're reading this TU, and
3750 the TU has an entry in .gdb_index, replace the recorded data from
3751 .gdb_index with this TU. */
3752
3753 signatured_type find_sig_entry (sig);
3754 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3755 &find_sig_entry, INSERT);
3756 signatured_type *sig_entry = (struct signatured_type *) *slot;
3757
3758 /* We can get here with the TU already read, *or* in the process of being
3759 read. Don't reassign the global entry to point to this DWO if that's
3760 the case. Also note that if the TU is already being read, it may not
3761 have come from a DWO, the program may be a mix of Fission-compiled
3762 code and non-Fission-compiled code. */
3763
3764 /* Have we already tried to read this TU?
3765 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3766 needn't exist in the global table yet). */
3767 if (sig_entry != NULL && sig_entry->tu_read)
3768 return sig_entry;
3769
3770 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
3771 dwo_unit of the TU itself. */
3772 dwo_file = cu->dwo_unit->dwo_file;
3773
3774 /* Ok, this is the first time we're reading this TU. */
3775 if (dwo_file->tus == NULL)
3776 return NULL;
3777 find_dwo_entry.signature = sig;
3778 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
3779 &find_dwo_entry);
3780 if (dwo_entry == NULL)
3781 return NULL;
3782
3783 /* If the global table doesn't have an entry for this TU, add one. */
3784 if (sig_entry == NULL)
3785 sig_entry = add_type_unit (per_objfile, sig, slot);
3786
3787 if (sig_entry->dwo_unit == nullptr)
3788 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
3789 sig_entry->tu_read = 1;
3790 return sig_entry;
3791 }
3792
3793 /* Subroutine of lookup_signatured_type.
3794 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
3795 then try the DWP file. If the TU stub (skeleton) has been removed then
3796 it won't be in .gdb_index. */
3797
3798 static struct signatured_type *
3799 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3800 {
3801 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3802 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
3803 struct dwo_unit *dwo_entry;
3804 void **slot;
3805
3806 gdb_assert (cu->dwo_unit);
3807 gdb_assert (dwp_file != NULL);
3808
3809 /* If TU skeletons have been removed then we may not have read in any
3810 TUs yet. */
3811 if (per_objfile->per_bfd->signatured_types == NULL)
3812 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
3813
3814 signatured_type find_sig_entry (sig);
3815 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
3816 &find_sig_entry, INSERT);
3817 signatured_type *sig_entry = (struct signatured_type *) *slot;
3818
3819 /* Have we already tried to read this TU?
3820 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3821 needn't exist in the global table yet). */
3822 if (sig_entry != NULL)
3823 return sig_entry;
3824
3825 if (dwp_file->tus == NULL)
3826 return NULL;
3827 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
3828 1 /* is_debug_types */);
3829 if (dwo_entry == NULL)
3830 return NULL;
3831
3832 sig_entry = add_type_unit (per_objfile, sig, slot);
3833 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
3834
3835 return sig_entry;
3836 }
3837
3838 /* Lookup a signature based type for DW_FORM_ref_sig8.
3839 Returns NULL if signature SIG is not present in the table.
3840 It is up to the caller to complain about this. */
3841
3842 static struct signatured_type *
3843 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
3844 {
3845 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3846
3847 if (cu->dwo_unit)
3848 {
3849 /* We're in a DWO/DWP file, and we're using .gdb_index.
3850 These cases require special processing. */
3851 if (get_dwp_file (per_objfile) == NULL)
3852 return lookup_dwo_signatured_type (cu, sig);
3853 else
3854 return lookup_dwp_signatured_type (cu, sig);
3855 }
3856 else
3857 {
3858 if (per_objfile->per_bfd->signatured_types == NULL)
3859 return NULL;
3860 signatured_type find_entry (sig);
3861 return ((struct signatured_type *)
3862 htab_find (per_objfile->per_bfd->signatured_types.get (),
3863 &find_entry));
3864 }
3865 }
3866
3867 /* Low level DIE reading support. */
3868
3869 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3870
3871 static void
3872 init_cu_die_reader (struct die_reader_specs *reader,
3873 struct dwarf2_cu *cu,
3874 struct dwarf2_section_info *section,
3875 struct dwo_file *dwo_file,
3876 struct abbrev_table *abbrev_table)
3877 {
3878 gdb_assert (section->readin && section->buffer != NULL);
3879 reader->abfd = section->get_bfd_owner ();
3880 reader->cu = cu;
3881 reader->dwo_file = dwo_file;
3882 reader->die_section = section;
3883 reader->buffer = section->buffer;
3884 reader->buffer_end = section->buffer + section->size;
3885 reader->abbrev_table = abbrev_table;
3886 }
3887
3888 /* Subroutine of cutu_reader to simplify it.
3889 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
3890 There's just a lot of work to do, and cutu_reader is big enough
3891 already.
3892
3893 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
3894 from it to the DIE in the DWO. If NULL we are skipping the stub.
3895 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
3896 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
3897 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
3898 STUB_COMP_DIR may be non-NULL.
3899 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
3900 are filled in with the info of the DIE from the DWO file.
3901 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
3902 from the dwo. Since *RESULT_READER references this abbrev table, it must be
3903 kept around for at least as long as *RESULT_READER.
3904
3905 The result is non-zero if a valid (non-dummy) DIE was found. */
3906
3907 static int
3908 read_cutu_die_from_dwo (dwarf2_cu *cu,
3909 struct dwo_unit *dwo_unit,
3910 struct die_info *stub_comp_unit_die,
3911 const char *stub_comp_dir,
3912 struct die_reader_specs *result_reader,
3913 const gdb_byte **result_info_ptr,
3914 struct die_info **result_comp_unit_die,
3915 abbrev_table_up *result_dwo_abbrev_table)
3916 {
3917 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3918 dwarf2_per_cu_data *per_cu = cu->per_cu;
3919 struct objfile *objfile = per_objfile->objfile;
3920 bfd *abfd;
3921 const gdb_byte *begin_info_ptr, *info_ptr;
3922 struct dwarf2_section_info *dwo_abbrev_section;
3923
3924 /* At most one of these may be provided. */
3925 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
3926
3927 /* These attributes aren't processed until later: DW_AT_stmt_list,
3928 DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_comp_dir.
3929 However, these attributes are found in the stub which we won't
3930 have later. In order to not impose this complication on the rest
3931 of the code, we read them here and copy them to the DWO CU/TU
3932 die. */
3933
3934 /* We store them all in an array. */
3935 struct attribute *attributes[5] {};
3936 /* Next available element of the attributes array. */
3937 int next_attr_idx = 0;
3938
3939 /* Push an element into ATTRIBUTES. */
3940 auto push_back = [&] (struct attribute *attr)
3941 {
3942 gdb_assert (next_attr_idx < ARRAY_SIZE (attributes));
3943 if (attr != nullptr)
3944 attributes[next_attr_idx++] = attr;
3945 };
3946
3947 if (stub_comp_unit_die != NULL)
3948 {
3949 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3950 DWO file. */
3951 if (!per_cu->is_debug_types)
3952 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu));
3953 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu));
3954 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu));
3955 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu));
3956 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu));
3957
3958 cu->addr_base = stub_comp_unit_die->addr_base ();
3959
3960 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
3961 We need the value before we can process DW_AT_ranges values from the
3962 DWO. */
3963 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
3964
3965 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
3966 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
3967 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
3968 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
3969 section. */
3970 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
3971 }
3972 else if (stub_comp_dir != NULL)
3973 {
3974 /* Reconstruct the comp_dir attribute to simplify the code below. */
3975 struct attribute *comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack,
3976 struct attribute);
3977 comp_dir->name = DW_AT_comp_dir;
3978 comp_dir->form = DW_FORM_string;
3979 comp_dir->set_string_noncanonical (stub_comp_dir);
3980 push_back (comp_dir);
3981 }
3982
3983 /* Set up for reading the DWO CU/TU. */
3984 cu->dwo_unit = dwo_unit;
3985 dwarf2_section_info *section = dwo_unit->section;
3986 section->read (objfile);
3987 abfd = section->get_bfd_owner ();
3988 begin_info_ptr = info_ptr = (section->buffer
3989 + to_underlying (dwo_unit->sect_off));
3990 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3991
3992 if (per_cu->is_debug_types)
3993 {
3994 signatured_type *sig_type = (struct signatured_type *) per_cu;
3995
3996 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3997 section, dwo_abbrev_section,
3998 info_ptr, rcuh_kind::TYPE);
3999 /* This is not an assert because it can be caused by bad debug info. */
4000 if (sig_type->signature != cu->header.signature)
4001 {
4002 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4003 " TU at offset %s [in module %s]"),
4004 hex_string (sig_type->signature),
4005 hex_string (cu->header.signature),
4006 sect_offset_str (dwo_unit->sect_off),
4007 bfd_get_filename (abfd));
4008 }
4009 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
4010 /* For DWOs coming from DWP files, we don't know the CU length
4011 nor the type's offset in the TU until now. */
4012 dwo_unit->length = cu->header.get_length_with_initial ();
4013 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
4014
4015 /* Establish the type offset that can be used to lookup the type.
4016 For DWO files, we don't know it until now. */
4017 sig_type->type_offset_in_section
4018 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
4019 }
4020 else
4021 {
4022 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4023 section, dwo_abbrev_section,
4024 info_ptr, rcuh_kind::COMPILE);
4025 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
4026 /* For DWOs coming from DWP files, we don't know the CU length
4027 until now. */
4028 dwo_unit->length = cu->header.get_length_with_initial ();
4029 }
4030
4031 dwo_abbrev_section->read (objfile);
4032 *result_dwo_abbrev_table
4033 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
4034 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
4035 result_dwo_abbrev_table->get ());
4036
4037 /* Read in the die, filling in the attributes from the stub. This
4038 has the benefit of simplifying the rest of the code - all the
4039 work to maintain the illusion of a single
4040 DW_TAG_{compile,type}_unit DIE is done here. */
4041 info_ptr = read_toplevel_die (result_reader, result_comp_unit_die, info_ptr,
4042 gdb::make_array_view (attributes,
4043 next_attr_idx));
4044
4045 /* Skip dummy compilation units. */
4046 if (info_ptr >= begin_info_ptr + dwo_unit->length
4047 || peek_abbrev_code (abfd, info_ptr) == 0)
4048 return 0;
4049
4050 *result_info_ptr = info_ptr;
4051 return 1;
4052 }
4053
4054 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
4055 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
4056 signature is part of the header. */
4057 static gdb::optional<ULONGEST>
4058 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
4059 {
4060 if (cu->header.version >= 5)
4061 return cu->header.signature;
4062 struct attribute *attr;
4063 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4064 if (attr == nullptr || !attr->form_is_unsigned ())
4065 return gdb::optional<ULONGEST> ();
4066 return attr->as_unsigned ();
4067 }
4068
4069 /* Subroutine of cutu_reader to simplify it.
4070 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4071 Returns NULL if the specified DWO unit cannot be found. */
4072
4073 static struct dwo_unit *
4074 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
4075 {
4076 #if CXX_STD_THREAD
4077 /* We need a lock here both to handle the DWO hash table, and BFD,
4078 which is not thread-safe. */
4079 static std::mutex dwo_lock;
4080
4081 std::lock_guard<std::mutex> guard (dwo_lock);
4082 #endif
4083
4084 dwarf2_per_cu_data *per_cu = cu->per_cu;
4085 struct dwo_unit *dwo_unit;
4086 const char *comp_dir;
4087
4088 gdb_assert (cu != NULL);
4089
4090 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4091 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
4092 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
4093
4094 if (per_cu->is_debug_types)
4095 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
4096 else
4097 {
4098 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4099
4100 if (!signature.has_value ())
4101 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4102 " [in module %s]"),
4103 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
4104
4105 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
4106 }
4107
4108 return dwo_unit;
4109 }
4110
4111 /* Subroutine of cutu_reader to simplify it.
4112 See it for a description of the parameters.
4113 Read a TU directly from a DWO file, bypassing the stub. */
4114
4115 void
4116 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
4117 dwarf2_per_objfile *per_objfile,
4118 dwarf2_cu *existing_cu)
4119 {
4120 struct signatured_type *sig_type;
4121
4122 /* Verify we can do the following downcast, and that we have the
4123 data we need. */
4124 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4125 sig_type = (struct signatured_type *) this_cu;
4126 gdb_assert (sig_type->dwo_unit != NULL);
4127
4128 dwarf2_cu *cu;
4129
4130 if (existing_cu != nullptr)
4131 {
4132 cu = existing_cu;
4133 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
4134 /* There's no need to do the rereading_dwo_cu handling that
4135 cutu_reader does since we don't read the stub. */
4136 }
4137 else
4138 {
4139 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
4140 in per_objfile yet. */
4141 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
4142 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4143 cu = m_new_cu.get ();
4144 }
4145
4146 /* A future optimization, if needed, would be to use an existing
4147 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
4148 could share abbrev tables. */
4149
4150 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
4151 NULL /* stub_comp_unit_die */,
4152 sig_type->dwo_unit->dwo_file->comp_dir,
4153 this, &info_ptr,
4154 &comp_unit_die,
4155 &m_dwo_abbrev_table) == 0)
4156 {
4157 /* Dummy die. */
4158 dummy_p = true;
4159 }
4160 }
4161
4162 /* Initialize a CU (or TU) and read its DIEs.
4163 If the CU defers to a DWO file, read the DWO file as well.
4164
4165 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4166 Otherwise the table specified in the comp unit header is read in and used.
4167 This is an optimization for when we already have the abbrev table.
4168
4169 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
4170 allocated. */
4171
4172 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
4173 dwarf2_per_objfile *per_objfile,
4174 struct abbrev_table *abbrev_table,
4175 dwarf2_cu *existing_cu,
4176 bool skip_partial,
4177 abbrev_cache *cache)
4178 : die_reader_specs {},
4179 m_this_cu (this_cu)
4180 {
4181 struct objfile *objfile = per_objfile->objfile;
4182 struct dwarf2_section_info *section = this_cu->section;
4183 bfd *abfd = section->get_bfd_owner ();
4184 const gdb_byte *begin_info_ptr;
4185 struct signatured_type *sig_type = NULL;
4186 struct dwarf2_section_info *abbrev_section;
4187 /* Non-zero if CU currently points to a DWO file and we need to
4188 reread it. When this happens we need to reread the skeleton die
4189 before we can reread the DWO file (this only applies to CUs, not TUs). */
4190 int rereading_dwo_cu = 0;
4191
4192 if (dwarf_die_debug)
4193 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
4194 this_cu->is_debug_types ? "type" : "comp",
4195 sect_offset_str (this_cu->sect_off));
4196
4197 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4198 file (instead of going through the stub), short-circuit all of this. */
4199 if (this_cu->reading_dwo_directly)
4200 {
4201 /* Narrow down the scope of possibilities to have to understand. */
4202 gdb_assert (this_cu->is_debug_types);
4203 gdb_assert (abbrev_table == NULL);
4204 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
4205 return;
4206 }
4207
4208 /* This is cheap if the section is already read in. */
4209 section->read (objfile);
4210
4211 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4212
4213 abbrev_section = get_abbrev_section_for_cu (this_cu);
4214
4215 dwarf2_cu *cu;
4216
4217 if (existing_cu != nullptr)
4218 {
4219 cu = existing_cu;
4220 /* If this CU is from a DWO file we need to start over, we need to
4221 refetch the attributes from the skeleton CU.
4222 This could be optimized by retrieving those attributes from when we
4223 were here the first time: the previous comp_unit_die was stored in
4224 comp_unit_obstack. But there's no data yet that we need this
4225 optimization. */
4226 if (cu->dwo_unit != NULL)
4227 rereading_dwo_cu = 1;
4228 }
4229 else
4230 {
4231 /* If an existing_cu is provided, a dwarf2_cu must not exist for
4232 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
4233 let us know that the CU is being scanned using the parallel
4234 indexer. This assert is avoided in this case because (1) it
4235 is irrelevant, and (2) the get_cu method is not
4236 thread-safe. */
4237 gdb_assert (cache != nullptr
4238 || per_objfile->get_cu (this_cu) == nullptr);
4239 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4240 cu = m_new_cu.get ();
4241 }
4242
4243 /* Get the header. */
4244 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
4245 {
4246 /* We already have the header, there's no need to read it in again. */
4247 info_ptr += to_underlying (cu->header.first_die_cu_offset);
4248 }
4249 else
4250 {
4251 if (this_cu->is_debug_types)
4252 {
4253 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4254 section, abbrev_section,
4255 info_ptr, rcuh_kind::TYPE);
4256
4257 /* Since per_cu is the first member of struct signatured_type,
4258 we can go from a pointer to one to a pointer to the other. */
4259 sig_type = (struct signatured_type *) this_cu;
4260 gdb_assert (sig_type->signature == cu->header.signature);
4261 gdb_assert (sig_type->type_offset_in_tu
4262 == cu->header.type_cu_offset_in_tu);
4263 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4264
4265 /* LENGTH has not been set yet for type units if we're
4266 using .gdb_index. */
4267 this_cu->set_length (cu->header.get_length_with_initial ());
4268
4269 /* Establish the type offset that can be used to lookup the type. */
4270 sig_type->type_offset_in_section =
4271 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
4272
4273 this_cu->set_version (cu->header.version);
4274 }
4275 else
4276 {
4277 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
4278 section, abbrev_section,
4279 info_ptr,
4280 rcuh_kind::COMPILE);
4281
4282 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4283 this_cu->set_length (cu->header.get_length_with_initial ());
4284 this_cu->set_version (cu->header.version);
4285 }
4286 }
4287
4288 /* Skip dummy compilation units. */
4289 if (info_ptr >= begin_info_ptr + this_cu->length ()
4290 || peek_abbrev_code (abfd, info_ptr) == 0)
4291 {
4292 dummy_p = true;
4293 return;
4294 }
4295
4296 /* If we don't have them yet, read the abbrevs for this compilation unit.
4297 And if we need to read them now, make sure they're freed when we're
4298 done. */
4299 if (abbrev_table != NULL)
4300 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
4301 else
4302 {
4303 if (cache != nullptr)
4304 abbrev_table = cache->find (abbrev_section,
4305 cu->header.abbrev_sect_off);
4306 if (abbrev_table == nullptr)
4307 {
4308 abbrev_section->read (objfile);
4309 m_abbrev_table_holder
4310 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
4311 abbrev_table = m_abbrev_table_holder.get ();
4312 }
4313 }
4314
4315 /* Read the top level CU/TU die. */
4316 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
4317 info_ptr = read_toplevel_die (this, &comp_unit_die, info_ptr);
4318
4319 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
4320 {
4321 dummy_p = true;
4322 return;
4323 }
4324
4325 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4326 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
4327 table from the DWO file and pass the ownership over to us. It will be
4328 referenced from READER, so we must make sure to free it after we're done
4329 with READER.
4330
4331 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4332 DWO CU, that this test will fail (the attribute will not be present). */
4333 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
4334 if (dwo_name != nullptr)
4335 {
4336 struct dwo_unit *dwo_unit;
4337 struct die_info *dwo_comp_unit_die;
4338
4339 if (comp_unit_die->has_children)
4340 {
4341 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
4342 " has children (offset %s) [in module %s]"),
4343 sect_offset_str (this_cu->sect_off),
4344 bfd_get_filename (abfd));
4345 }
4346 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
4347 if (dwo_unit != NULL)
4348 {
4349 if (read_cutu_die_from_dwo (cu, dwo_unit,
4350 comp_unit_die, NULL,
4351 this, &info_ptr,
4352 &dwo_comp_unit_die,
4353 &m_dwo_abbrev_table) == 0)
4354 {
4355 /* Dummy die. */
4356 dummy_p = true;
4357 return;
4358 }
4359 comp_unit_die = dwo_comp_unit_die;
4360 }
4361 else
4362 {
4363 /* Yikes, we couldn't find the rest of the DIE, we only have
4364 the stub. A complaint has already been logged. There's
4365 not much more we can do except pass on the stub DIE to
4366 die_reader_func. We don't want to throw an error on bad
4367 debug info. */
4368 }
4369 }
4370 }
4371
4372 void
4373 cutu_reader::keep ()
4374 {
4375 /* Done, clean up. */
4376 gdb_assert (!dummy_p);
4377 if (m_new_cu != NULL)
4378 {
4379 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
4380 now. */
4381 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
4382 per_objfile->set_cu (m_this_cu, std::move (m_new_cu));
4383 }
4384 }
4385
4386 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
4387 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
4388 assumed to have already done the lookup to find the DWO file).
4389
4390 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4391 THIS_CU->is_debug_types, but nothing else.
4392
4393 We fill in THIS_CU->length.
4394
4395 THIS_CU->cu is always freed when done.
4396 This is done in order to not leave THIS_CU->cu in a state where we have
4397 to care whether it refers to the "main" CU or the DWO CU.
4398
4399 When parent_cu is passed, it is used to provide a default value for
4400 str_offsets_base and addr_base from the parent. */
4401
4402 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
4403 dwarf2_per_objfile *per_objfile,
4404 struct dwarf2_cu *parent_cu,
4405 struct dwo_file *dwo_file)
4406 : die_reader_specs {},
4407 m_this_cu (this_cu)
4408 {
4409 struct objfile *objfile = per_objfile->objfile;
4410 struct dwarf2_section_info *section = this_cu->section;
4411 bfd *abfd = section->get_bfd_owner ();
4412 struct dwarf2_section_info *abbrev_section;
4413 const gdb_byte *begin_info_ptr, *info_ptr;
4414
4415 if (dwarf_die_debug)
4416 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
4417 this_cu->is_debug_types ? "type" : "comp",
4418 sect_offset_str (this_cu->sect_off));
4419
4420 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
4421
4422 abbrev_section = (dwo_file != NULL
4423 ? &dwo_file->sections.abbrev
4424 : get_abbrev_section_for_cu (this_cu));
4425
4426 /* This is cheap if the section is already read in. */
4427 section->read (objfile);
4428
4429 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
4430
4431 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
4432 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
4433 section, abbrev_section, info_ptr,
4434 (this_cu->is_debug_types
4435 ? rcuh_kind::TYPE
4436 : rcuh_kind::COMPILE));
4437
4438 if (parent_cu != nullptr)
4439 {
4440 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
4441 m_new_cu->addr_base = parent_cu->addr_base;
4442 }
4443 this_cu->set_length (m_new_cu->header.get_length_with_initial ());
4444
4445 /* Skip dummy compilation units. */
4446 if (info_ptr >= begin_info_ptr + this_cu->length ()
4447 || peek_abbrev_code (abfd, info_ptr) == 0)
4448 {
4449 dummy_p = true;
4450 return;
4451 }
4452
4453 abbrev_section->read (objfile);
4454 m_abbrev_table_holder
4455 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
4456
4457 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
4458 m_abbrev_table_holder.get ());
4459 info_ptr = read_toplevel_die (this, &comp_unit_die, info_ptr);
4460 }
4461
4462 \f
4463 /* Type Unit Groups.
4464
4465 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4466 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4467 so that all types coming from the same compilation (.o file) are grouped
4468 together. A future step could be to put the types in the same symtab as
4469 the CU the types ultimately came from. */
4470
4471 static hashval_t
4472 hash_type_unit_group (const void *item)
4473 {
4474 const struct type_unit_group *tu_group
4475 = (const struct type_unit_group *) item;
4476
4477 return hash_stmt_list_entry (&tu_group->hash);
4478 }
4479
4480 static int
4481 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4482 {
4483 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
4484 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
4485
4486 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4487 }
4488
4489 /* Allocate a hash table for type unit groups. */
4490
4491 static htab_up
4492 allocate_type_unit_groups_table ()
4493 {
4494 return htab_up (htab_create_alloc (3,
4495 hash_type_unit_group,
4496 eq_type_unit_group,
4497 htab_delete_entry<type_unit_group>,
4498 xcalloc, xfree));
4499 }
4500
4501 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4502 partial symtabs. We combine several TUs per psymtab to not let the size
4503 of any one psymtab grow too big. */
4504 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4505 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4506
4507 /* Helper routine for get_type_unit_group.
4508 Create the type_unit_group object used to hold one or more TUs. */
4509
4510 static std::unique_ptr<type_unit_group>
4511 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4512 {
4513 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
4514
4515 tu_group->hash.dwo_unit = cu->dwo_unit;
4516 tu_group->hash.line_sect_off = line_offset_struct;
4517
4518 return tu_group;
4519 }
4520
4521 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4522 STMT_LIST is a DW_AT_stmt_list attribute. */
4523
4524 static struct type_unit_group *
4525 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
4526 {
4527 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4528 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4529 struct type_unit_group *tu_group;
4530 void **slot;
4531 unsigned int line_offset;
4532 struct type_unit_group type_unit_group_for_lookup;
4533
4534 if (per_objfile->per_bfd->type_unit_groups == NULL)
4535 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
4536
4537 /* Do we need to create a new group, or can we use an existing one? */
4538
4539 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
4540 {
4541 line_offset = stmt_list->as_unsigned ();
4542 ++tu_stats->nr_symtab_sharers;
4543 }
4544 else
4545 {
4546 /* Ugh, no stmt_list. Rare, but we have to handle it.
4547 We can do various things here like create one group per TU or
4548 spread them over multiple groups to split up the expansion work.
4549 To avoid worst case scenarios (too many groups or too large groups)
4550 we, umm, group them in bunches. */
4551 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
4552 | (tu_stats->nr_stmt_less_type_units
4553 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
4554 ++tu_stats->nr_stmt_less_type_units;
4555 }
4556
4557 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
4558 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
4559 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
4560 &type_unit_group_for_lookup, INSERT);
4561 if (*slot == nullptr)
4562 {
4563 sect_offset line_offset_struct = (sect_offset) line_offset;
4564 std::unique_ptr<type_unit_group> grp
4565 = create_type_unit_group (cu, line_offset_struct);
4566 *slot = grp.release ();
4567 ++tu_stats->nr_symtabs;
4568 }
4569
4570 tu_group = (struct type_unit_group *) *slot;
4571 gdb_assert (tu_group != nullptr);
4572 return tu_group;
4573 }
4574 \f
4575
4576 /* An instance of this is created when scanning DWARF to create a
4577 cooked index. */
4578
4579 class cooked_index_storage
4580 {
4581 public:
4582
4583 cooked_index_storage ()
4584 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
4585 eq_cutu_reader,
4586 htab_delete_entry<cutu_reader>,
4587 xcalloc, xfree)),
4588 m_index (new cooked_index_shard)
4589 {
4590 }
4591
4592 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
4593
4594 /* Return the current abbrev cache. */
4595 abbrev_cache *get_abbrev_cache ()
4596 {
4597 return &m_abbrev_cache;
4598 }
4599
4600 /* Return the DIE reader corresponding to PER_CU. If no such reader
4601 has been registered, return NULL. */
4602 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
4603 {
4604 int index = per_cu->index;
4605 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
4606 &index, index);
4607 }
4608
4609 /* Preserve READER by storing it in the local hash table. */
4610 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
4611 {
4612 m_abbrev_cache.add (reader->release_abbrev_table ());
4613
4614 int index = reader->cu->per_cu->index;
4615 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
4616 index, INSERT);
4617 gdb_assert (*slot == nullptr);
4618 cutu_reader *result = reader.get ();
4619 *slot = reader.release ();
4620 return result;
4621 }
4622
4623 /* Add an entry to the index. The arguments describe the entry; see
4624 cooked-index.h. The new entry is returned. */
4625 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
4626 cooked_index_flag flags,
4627 const char *name,
4628 const cooked_index_entry *parent_entry,
4629 dwarf2_per_cu_data *per_cu)
4630 {
4631 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
4632 }
4633
4634 /* Install the current addrmap into the index shard being constructed,
4635 then transfer ownership of the index to the caller. */
4636 std::unique_ptr<cooked_index_shard> release ()
4637 {
4638 m_index->install_addrmap (&m_addrmap);
4639 return std::move (m_index);
4640 }
4641
4642 /* Return the mutable addrmap that is currently being created. */
4643 addrmap_mutable *get_addrmap ()
4644 {
4645 return &m_addrmap;
4646 }
4647
4648 private:
4649
4650 /* Hash function for a cutu_reader. */
4651 static hashval_t hash_cutu_reader (const void *a)
4652 {
4653 const cutu_reader *reader = (const cutu_reader *) a;
4654 return reader->cu->per_cu->index;
4655 }
4656
4657 /* Equality function for cutu_reader. */
4658 static int eq_cutu_reader (const void *a, const void *b)
4659 {
4660 const cutu_reader *ra = (const cutu_reader *) a;
4661 const int *rb = (const int *) b;
4662 return ra->cu->per_cu->index == *rb;
4663 }
4664
4665 /* The abbrev cache used by this indexer. */
4666 abbrev_cache m_abbrev_cache;
4667 /* A hash table of cutu_reader objects. */
4668 htab_up m_reader_hash;
4669 /* The index shard that is being constructed. */
4670 std::unique_ptr<cooked_index_shard> m_index;
4671
4672 /* A writeable addrmap being constructed by this scanner. */
4673 addrmap_mutable m_addrmap;
4674 };
4675
4676 /* An instance of this is created to index a CU. */
4677
4678 class cooked_indexer
4679 {
4680 public:
4681
4682 cooked_indexer (cooked_index_storage *storage,
4683 dwarf2_per_cu_data *per_cu,
4684 enum language language)
4685 : m_index_storage (storage),
4686 m_per_cu (per_cu),
4687 m_language (language)
4688 {
4689 }
4690
4691 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
4692
4693 /* Index the given CU. */
4694 void make_index (cutu_reader *reader);
4695
4696 private:
4697
4698 /* A helper function to turn a section offset into an address that
4699 can be used in an addrmap. */
4700 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
4701 {
4702 CORE_ADDR value = to_underlying (offset);
4703 if (is_dwz)
4704 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
4705 return value;
4706 }
4707
4708 /* A helper function to scan the PC bounds of READER and record them
4709 in the storage's addrmap. */
4710 void check_bounds (cutu_reader *reader);
4711
4712 /* Ensure that the indicated CU exists. The cutu_reader for it is
4713 returned. FOR_SCANNING is true if the caller intends to scan all
4714 the DIEs in the CU; when false, this use is assumed to be to look
4715 up just a single DIE. */
4716 cutu_reader *ensure_cu_exists (cutu_reader *reader,
4717 dwarf2_per_objfile *per_objfile,
4718 sect_offset sect_off,
4719 bool is_dwz,
4720 bool for_scanning);
4721
4722 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
4723 the entry for the enclosing scope (nullptr at top level). FULLY
4724 is true when a full scan must be done -- in some languages,
4725 function scopes must be fully explored in order to find nested
4726 functions. This returns a pointer to just after the spot where
4727 reading stopped. */
4728 const gdb_byte *index_dies (cutu_reader *reader,
4729 const gdb_byte *info_ptr,
4730 const cooked_index_entry *parent_entry,
4731 bool fully);
4732
4733 /* Scan the attributes for a given DIE and update the out
4734 parameters. Returns a pointer to the byte after the DIE. */
4735 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
4736 cutu_reader *reader,
4737 const gdb_byte *watermark_ptr,
4738 const gdb_byte *info_ptr,
4739 const abbrev_info *abbrev,
4740 const char **name,
4741 const char **linkage_name,
4742 cooked_index_flag *flags,
4743 sect_offset *sibling_offset,
4744 const cooked_index_entry **parent_entry,
4745 CORE_ADDR *maybe_defer,
4746 bool for_specification);
4747
4748 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
4749 DW_AT_import, and then scanning the referenced CU. Returns a
4750 pointer to the byte after the DIE. */
4751 const gdb_byte *index_imported_unit (cutu_reader *reader,
4752 const gdb_byte *info_ptr,
4753 const abbrev_info *abbrev);
4754
4755 /* Recursively read DIEs, recording the section offsets in
4756 m_die_range_map and then calling index_dies. */
4757 const gdb_byte *recurse (cutu_reader *reader,
4758 const gdb_byte *info_ptr,
4759 const cooked_index_entry *parent_entry,
4760 bool fully);
4761
4762 /* The storage object, where the results are kept. */
4763 cooked_index_storage *m_index_storage;
4764 /* The CU that we are reading on behalf of. This object might be
4765 asked to index one CU but to treat the results as if they come
4766 from some including CU; in this case the including CU would be
4767 recorded here. */
4768 dwarf2_per_cu_data *m_per_cu;
4769 /* The language that we're assuming when reading. */
4770 enum language m_language;
4771
4772 /* An addrmap that maps from section offsets (see the form_addr
4773 method) to newly-created entries. See m_deferred_entries to
4774 understand this. */
4775 addrmap_mutable m_die_range_map;
4776
4777 /* A single deferred entry. */
4778 struct deferred_entry
4779 {
4780 sect_offset die_offset;
4781 const char *name;
4782 CORE_ADDR spec_offset;
4783 dwarf_tag tag;
4784 cooked_index_flag flags;
4785 };
4786
4787 /* The generated DWARF can sometimes have the declaration for a
4788 method in a class (or perhaps namespace) scope, with the
4789 definition appearing outside this scope... just one of the many
4790 bad things about DWARF. In order to handle this situation, we
4791 defer certain entries until the end of scanning, at which point
4792 we'll know the containing context of all the DIEs that we might
4793 have scanned. This vector stores these deferred entries. */
4794 std::vector<deferred_entry> m_deferred_entries;
4795 };
4796
4797 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4798 Process compilation unit THIS_CU for a psymtab. */
4799
4800 static void
4801 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
4802 dwarf2_per_objfile *per_objfile,
4803 cooked_index_storage *storage)
4804 {
4805 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
4806 storage->get_abbrev_cache ());
4807
4808 if (reader.comp_unit_die == nullptr)
4809 return;
4810
4811 if (reader.dummy_p)
4812 {
4813 /* Nothing. */
4814 }
4815 else if (this_cu->is_debug_types)
4816 build_type_psymtabs_reader (&reader, storage);
4817 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
4818 {
4819 bool nope = false;
4820 if (this_cu->scanned.compare_exchange_strong (nope, true))
4821 {
4822 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
4823 language_minimal);
4824 gdb_assert (storage != nullptr);
4825 cooked_indexer indexer (storage, this_cu, reader.cu->lang ());
4826 indexer.make_index (&reader);
4827 }
4828 }
4829 }
4830
4831 /* Reader function for build_type_psymtabs. */
4832
4833 static void
4834 build_type_psymtabs_reader (cutu_reader *reader,
4835 cooked_index_storage *storage)
4836 {
4837 struct dwarf2_cu *cu = reader->cu;
4838 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4839 struct die_info *type_unit_die = reader->comp_unit_die;
4840
4841 gdb_assert (per_cu->is_debug_types);
4842
4843 if (! type_unit_die->has_children)
4844 return;
4845
4846 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
4847
4848 gdb_assert (storage != nullptr);
4849 cooked_indexer indexer (storage, per_cu, cu->lang ());
4850 indexer.make_index (reader);
4851 }
4852
4853 /* Struct used to sort TUs by their abbreviation table offset. */
4854
4855 struct tu_abbrev_offset
4856 {
4857 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
4858 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
4859 {}
4860
4861 /* This is used when sorting. */
4862 bool operator< (const tu_abbrev_offset &other) const
4863 {
4864 return abbrev_offset < other.abbrev_offset;
4865 }
4866
4867 signatured_type *sig_type;
4868 sect_offset abbrev_offset;
4869 };
4870
4871 /* Efficiently read all the type units.
4872
4873 The efficiency is because we sort TUs by the abbrev table they use and
4874 only read each abbrev table once. In one program there are 200K TUs
4875 sharing 8K abbrev tables.
4876
4877 The main purpose of this function is to support building the
4878 dwarf2_per_objfile->per_bfd->type_unit_groups table.
4879 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
4880 can collapse the search space by grouping them by stmt_list.
4881 The savings can be significant, in the same program from above the 200K TUs
4882 share 8K stmt_list tables.
4883
4884 FUNC is expected to call get_type_unit_group, which will create the
4885 struct type_unit_group if necessary and add it to
4886 dwarf2_per_objfile->per_bfd->type_unit_groups. */
4887
4888 static void
4889 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
4890 cooked_index_storage *storage)
4891 {
4892 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4893 abbrev_table_up abbrev_table;
4894 sect_offset abbrev_offset;
4895
4896 /* It's up to the caller to not call us multiple times. */
4897 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
4898
4899 if (per_objfile->per_bfd->all_type_units.size () == 0)
4900 return;
4901
4902 /* TUs typically share abbrev tables, and there can be way more TUs than
4903 abbrev tables. Sort by abbrev table to reduce the number of times we
4904 read each abbrev table in.
4905 Alternatives are to punt or to maintain a cache of abbrev tables.
4906 This is simpler and efficient enough for now.
4907
4908 Later we group TUs by their DW_AT_stmt_list value (as this defines the
4909 symtab to use). Typically TUs with the same abbrev offset have the same
4910 stmt_list value too so in practice this should work well.
4911
4912 The basic algorithm here is:
4913
4914 sort TUs by abbrev table
4915 for each TU with same abbrev table:
4916 read abbrev table if first user
4917 read TU top level DIE
4918 [IWBN if DWO skeletons had DW_AT_stmt_list]
4919 call FUNC */
4920
4921 dwarf_read_debug_printf ("Building type unit groups ...");
4922
4923 /* Sort in a separate table to maintain the order of all_units
4924 for .gdb_index: TU indices directly index all_type_units. */
4925 std::vector<tu_abbrev_offset> sorted_by_abbrev;
4926 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
4927
4928 for (const auto &cu : per_objfile->per_bfd->all_units)
4929 {
4930 if (cu->is_debug_types)
4931 {
4932 auto sig_type = static_cast<signatured_type *> (cu.get ());
4933 sorted_by_abbrev.emplace_back
4934 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
4935 sig_type->sect_off));
4936 }
4937 }
4938
4939 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
4940
4941 abbrev_offset = (sect_offset) ~(unsigned) 0;
4942
4943 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
4944 {
4945 /* Switch to the next abbrev table if necessary. */
4946 if (abbrev_table == NULL
4947 || tu.abbrev_offset != abbrev_offset)
4948 {
4949 abbrev_offset = tu.abbrev_offset;
4950 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
4951 abbrev_table =
4952 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
4953 ++tu_stats->nr_uniq_abbrev_tables;
4954 }
4955
4956 cutu_reader reader (tu.sig_type, per_objfile,
4957 abbrev_table.get (), nullptr, false);
4958 if (!reader.dummy_p)
4959 build_type_psymtabs_reader (&reader, storage);
4960 }
4961 }
4962
4963 /* Print collected type unit statistics. */
4964
4965 static void
4966 print_tu_stats (dwarf2_per_objfile *per_objfile)
4967 {
4968 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4969
4970 dwarf_read_debug_printf ("Type unit statistics:");
4971 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
4972 dwarf_read_debug_printf (" %d uniq abbrev tables",
4973 tu_stats->nr_uniq_abbrev_tables);
4974 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
4975 tu_stats->nr_symtabs);
4976 dwarf_read_debug_printf (" %d symtab sharers",
4977 tu_stats->nr_symtab_sharers);
4978 dwarf_read_debug_printf (" %d type units without a stmt_list",
4979 tu_stats->nr_stmt_less_type_units);
4980 dwarf_read_debug_printf (" %d all_type_units reallocs",
4981 tu_stats->nr_all_type_units_reallocs);
4982 }
4983
4984 struct skeleton_data
4985 {
4986 dwarf2_per_objfile *per_objfile;
4987 cooked_index_storage *storage;
4988 };
4989
4990 /* Traversal function for process_skeletonless_type_unit.
4991 Read a TU in a DWO file and build partial symbols for it. */
4992
4993 static int
4994 process_skeletonless_type_unit (void **slot, void *info)
4995 {
4996 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
4997 skeleton_data *data = (skeleton_data *) info;
4998
4999 /* If this TU doesn't exist in the global table, add it and read it in. */
5000
5001 if (data->per_objfile->per_bfd->signatured_types == NULL)
5002 data->per_objfile->per_bfd->signatured_types
5003 = allocate_signatured_type_table ();
5004
5005 signatured_type find_entry (dwo_unit->signature);
5006 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
5007 &find_entry, INSERT);
5008 /* If we've already seen this type there's nothing to do. What's happening
5009 is we're doing our own version of comdat-folding here. */
5010 if (*slot != NULL)
5011 return 1;
5012
5013 /* This does the job that create_all_units would have done for
5014 this TU. */
5015 signatured_type *entry
5016 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
5017 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
5018 *slot = entry;
5019
5020 /* This does the job that build_type_psymtabs would have done. */
5021 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
5022 if (!reader.dummy_p)
5023 build_type_psymtabs_reader (&reader, data->storage);
5024
5025 return 1;
5026 }
5027
5028 /* Traversal function for process_skeletonless_type_units. */
5029
5030 static int
5031 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
5032 {
5033 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
5034
5035 if (dwo_file->tus != NULL)
5036 htab_traverse_noresize (dwo_file->tus.get (),
5037 process_skeletonless_type_unit, info);
5038
5039 return 1;
5040 }
5041
5042 /* Scan all TUs of DWO files, verifying we've processed them.
5043 This is needed in case a TU was emitted without its skeleton.
5044 Note: This can't be done until we know what all the DWO files are. */
5045
5046 static void
5047 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
5048 cooked_index_storage *storage)
5049 {
5050 skeleton_data data { per_objfile, storage };
5051
5052 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
5053 if (get_dwp_file (per_objfile) == NULL
5054 && per_objfile->per_bfd->dwo_files != NULL)
5055 {
5056 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
5057 process_dwo_file_for_skeletonless_type_units,
5058 &data);
5059 }
5060 }
5061
5062 /* Build the partial symbol table by doing a quick pass through the
5063 .debug_info and .debug_abbrev sections. */
5064
5065 static void
5066 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
5067 {
5068 struct objfile *objfile = per_objfile->objfile;
5069 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5070
5071 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
5072 objfile_name (objfile));
5073
5074 per_bfd->map_info_sections (objfile);
5075
5076 cooked_index_storage index_storage;
5077 create_all_units (per_objfile);
5078 build_type_psymtabs (per_objfile, &index_storage);
5079 std::vector<std::unique_ptr<cooked_index_shard>> indexes;
5080
5081 per_bfd->quick_file_names_table
5082 = create_quick_file_names_table (per_bfd->all_units.size ());
5083 if (!per_bfd->debug_aranges.empty ())
5084 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
5085 index_storage.get_addrmap ());
5086
5087 {
5088 /* Ensure that complaints are handled correctly. */
5089 complaint_interceptor complaint_handler;
5090
5091 using iter_type = decltype (per_bfd->all_units.begin ());
5092
5093 auto task_size_ = [] (iter_type iter)
5094 {
5095 dwarf2_per_cu_data *per_cu = iter->get ();
5096 return (size_t)per_cu->length ();
5097 };
5098 auto task_size = gdb::make_function_view (task_size_);
5099
5100 /* Each thread returns a pair holding a cooked index, and a vector
5101 of errors that should be printed. The latter is done because
5102 GDB's I/O system is not thread-safe. run_on_main_thread could be
5103 used, but that would mean the messages are printed after the
5104 prompt, which looks weird. */
5105 using result_type = std::pair<std::unique_ptr<cooked_index_shard>,
5106 std::vector<gdb_exception>>;
5107 std::vector<result_type> results
5108 = gdb::parallel_for_each (1, per_bfd->all_units.begin (),
5109 per_bfd->all_units.end (),
5110 [=] (iter_type iter, iter_type end)
5111 {
5112 std::vector<gdb_exception> errors;
5113 cooked_index_storage thread_storage;
5114 for (; iter != end; ++iter)
5115 {
5116 dwarf2_per_cu_data *per_cu = iter->get ();
5117 try
5118 {
5119 process_psymtab_comp_unit (per_cu, per_objfile,
5120 &thread_storage);
5121 }
5122 catch (gdb_exception &except)
5123 {
5124 errors.push_back (std::move (except));
5125 }
5126 }
5127 return result_type (thread_storage.release (), std::move (errors));
5128 }, task_size);
5129
5130 /* Only show a given exception a single time. */
5131 std::unordered_set<gdb_exception> seen_exceptions;
5132 for (auto &one_result : results)
5133 {
5134 indexes.push_back (std::move (one_result.first));
5135 for (auto &one_exc : one_result.second)
5136 if (seen_exceptions.insert (one_exc).second)
5137 exception_print (gdb_stderr, one_exc);
5138 }
5139 }
5140
5141 /* This has to wait until we read the CUs, we need the list of DWOs. */
5142 process_skeletonless_type_units (per_objfile, &index_storage);
5143
5144 if (dwarf_read_debug > 0)
5145 print_tu_stats (per_objfile);
5146
5147 indexes.push_back (index_storage.release ());
5148 indexes.shrink_to_fit ();
5149
5150 cooked_index *vec = new cooked_index (std::move (indexes), per_bfd);
5151 per_bfd->index_table.reset (vec);
5152
5153 const cooked_index_entry *main_entry = vec->get_main ();
5154 if (main_entry != nullptr)
5155 {
5156 /* We only do this for names not requiring canonicalization. At
5157 this point in the process names have not been canonicalized.
5158 However, currently, languages that require this step also do
5159 not use DW_AT_main_subprogram. An assert is appropriate here
5160 because this filtering is done in get_main. */
5161 enum language lang = main_entry->per_cu->lang ();
5162 gdb_assert (!language_requires_canonicalization (lang));
5163 const char *full_name = main_entry->full_name (&per_bfd->obstack, true);
5164 set_objfile_main_name (objfile, full_name, lang);
5165 }
5166
5167 dwarf_read_debug_printf ("Done building psymtabs of %s",
5168 objfile_name (objfile));
5169 }
5170
5171 static void
5172 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
5173 struct dwarf2_section_info *section,
5174 struct dwarf2_section_info *abbrev_section,
5175 unsigned int is_dwz,
5176 htab_up &types_htab,
5177 rcuh_kind section_kind)
5178 {
5179 const gdb_byte *info_ptr;
5180 struct objfile *objfile = per_objfile->objfile;
5181
5182 dwarf_read_debug_printf ("Reading %s for %s",
5183 section->get_name (),
5184 section->get_file_name ());
5185
5186 section->read (objfile);
5187
5188 info_ptr = section->buffer;
5189
5190 while (info_ptr < section->buffer + section->size)
5191 {
5192 dwarf2_per_cu_data_up this_cu;
5193
5194 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
5195
5196 comp_unit_head cu_header;
5197 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
5198 abbrev_section, info_ptr,
5199 section_kind);
5200
5201 /* Save the compilation unit for later lookup. */
5202 if (cu_header.unit_type != DW_UT_type)
5203 this_cu = per_objfile->per_bfd->allocate_per_cu ();
5204 else
5205 {
5206 if (types_htab == nullptr)
5207 types_htab = allocate_signatured_type_table ();
5208
5209 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
5210 (cu_header.signature);
5211 signatured_type *sig_ptr = sig_type.get ();
5212 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
5213 this_cu.reset (sig_type.release ());
5214
5215 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
5216 gdb_assert (slot != nullptr);
5217 if (*slot != nullptr)
5218 complaint (_("debug type entry at offset %s is duplicate to"
5219 " the entry at offset %s, signature %s"),
5220 sect_offset_str (sect_off),
5221 sect_offset_str (sig_ptr->sect_off),
5222 hex_string (sig_ptr->signature));
5223 *slot = sig_ptr;
5224 }
5225 this_cu->sect_off = sect_off;
5226 this_cu->set_length (cu_header.get_length_with_initial ());
5227 this_cu->is_dwz = is_dwz;
5228 this_cu->section = section;
5229 /* Init this asap, to avoid a data race in the set_version in
5230 cutu_reader::cutu_reader (which may be run in parallel for the cooked
5231 index case). */
5232 this_cu->set_version (cu_header.version);
5233
5234 info_ptr = info_ptr + this_cu->length ();
5235 per_objfile->per_bfd->all_units.push_back (std::move (this_cu));
5236 }
5237 }
5238
5239 /* Initialize the views on all_units. */
5240
5241 void
5242 finalize_all_units (dwarf2_per_bfd *per_bfd)
5243 {
5244 size_t nr_tus = per_bfd->tu_stats.nr_tus;
5245 size_t nr_cus = per_bfd->all_units.size () - nr_tus;
5246 gdb::array_view<dwarf2_per_cu_data_up> tmp = per_bfd->all_units;
5247 per_bfd->all_comp_units = tmp.slice (0, nr_cus);
5248 per_bfd->all_type_units = tmp.slice (nr_cus, nr_tus);
5249 }
5250
5251 /* Create a list of all compilation units in OBJFILE.
5252 This is only done for -readnow and building partial symtabs. */
5253
5254 static void
5255 create_all_units (dwarf2_per_objfile *per_objfile)
5256 {
5257 htab_up types_htab;
5258 gdb_assert (per_objfile->per_bfd->all_units.empty ());
5259
5260 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
5261 &per_objfile->per_bfd->abbrev, 0,
5262 types_htab, rcuh_kind::COMPILE);
5263 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
5264 read_comp_units_from_section (per_objfile, &section,
5265 &per_objfile->per_bfd->abbrev, 0,
5266 types_htab, rcuh_kind::TYPE);
5267
5268 dwz_file *dwz;
5269 try
5270 {
5271 dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
5272 }
5273 catch (const gdb_exception_error &)
5274 {
5275 per_objfile->per_bfd->all_units.clear ();
5276 throw;
5277 }
5278 if (dwz != NULL)
5279 {
5280 /* Pre-read the sections we'll need to construct an index. */
5281 struct objfile *objfile = per_objfile->objfile;
5282 dwz->abbrev.read (objfile);
5283 dwz->info.read (objfile);
5284 dwz->str.read (objfile);
5285 dwz->line.read (objfile);
5286 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
5287 types_htab, rcuh_kind::COMPILE);
5288 }
5289
5290 per_objfile->per_bfd->signatured_types = std::move (types_htab);
5291
5292 finalize_all_units (per_objfile->per_bfd);
5293 }
5294
5295 /* Return the initial uleb128 in the die at INFO_PTR. */
5296
5297 static unsigned int
5298 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
5299 {
5300 unsigned int bytes_read;
5301
5302 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5303 }
5304
5305 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
5306 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
5307
5308 Return the corresponding abbrev, or NULL if the number is zero (indicating
5309 an empty DIE). In either case *BYTES_READ will be set to the length of
5310 the initial number. */
5311
5312 static const struct abbrev_info *
5313 peek_die_abbrev (const die_reader_specs &reader,
5314 const gdb_byte *info_ptr, unsigned int *bytes_read)
5315 {
5316 dwarf2_cu *cu = reader.cu;
5317 bfd *abfd = reader.abfd;
5318 unsigned int abbrev_number
5319 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5320
5321 if (abbrev_number == 0)
5322 return NULL;
5323
5324 const abbrev_info *abbrev
5325 = reader.abbrev_table->lookup_abbrev (abbrev_number);
5326 if (!abbrev)
5327 {
5328 error (_("Dwarf Error: Could not find abbrev number %d in %s"
5329 " at offset %s [in module %s]"),
5330 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
5331 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
5332 }
5333
5334 return abbrev;
5335 }
5336
5337 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5338 Returns a pointer to the end of a series of DIEs, terminated by an empty
5339 DIE. Any children of the skipped DIEs will also be skipped. */
5340
5341 static const gdb_byte *
5342 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
5343 {
5344 while (1)
5345 {
5346 unsigned int bytes_read;
5347 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
5348 &bytes_read);
5349
5350 if (abbrev == NULL)
5351 return info_ptr + bytes_read;
5352 else
5353 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
5354 }
5355 }
5356
5357 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5358 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5359 abbrev corresponding to that skipped uleb128 should be passed in
5360 ABBREV.
5361
5362 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
5363 returns a pointer to this DIE's sibling, skipping any children.
5364 Otherwise, returns a pointer to the DIE's first child. */
5365
5366 static const gdb_byte *
5367 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
5368 const struct abbrev_info *abbrev, bool do_skip_children)
5369 {
5370 unsigned int bytes_read;
5371 struct attribute attr;
5372 bfd *abfd = reader->abfd;
5373 struct dwarf2_cu *cu = reader->cu;
5374 const gdb_byte *buffer = reader->buffer;
5375 const gdb_byte *buffer_end = reader->buffer_end;
5376 unsigned int form, i;
5377
5378 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
5379 {
5380 /* We only handle DW_FORM_ref4 here. */
5381 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
5382 unsigned int offset = read_4_bytes (abfd, sibling_data);
5383 const gdb_byte *sibling_ptr
5384 = buffer + to_underlying (cu->header.sect_off) + offset;
5385 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
5386 return sibling_ptr;
5387 /* Fall through to the slow way. */
5388 }
5389 else if (abbrev->size_if_constant != 0)
5390 {
5391 info_ptr += abbrev->size_if_constant;
5392 if (do_skip_children && abbrev->has_children)
5393 return skip_children (reader, info_ptr);
5394 return info_ptr;
5395 }
5396
5397 for (i = 0; i < abbrev->num_attrs; i++)
5398 {
5399 /* The only abbrev we care about is DW_AT_sibling. */
5400 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
5401 {
5402 /* Note there is no need for the extra work of
5403 "reprocessing" here, so we pass false for that
5404 argument. */
5405 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr, false);
5406 if (attr.form == DW_FORM_ref_addr)
5407 complaint (_("ignoring absolute DW_AT_sibling"));
5408 else
5409 {
5410 sect_offset off = attr.get_ref_die_offset ();
5411 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
5412
5413 if (sibling_ptr < info_ptr)
5414 complaint (_("DW_AT_sibling points backwards"));
5415 else if (sibling_ptr > reader->buffer_end)
5416 reader->die_section->overflow_complaint ();
5417 else
5418 return sibling_ptr;
5419 }
5420 }
5421
5422 /* If it isn't DW_AT_sibling, skip this attribute. */
5423 form = abbrev->attrs[i].form;
5424 skip_attribute:
5425 switch (form)
5426 {
5427 case DW_FORM_ref_addr:
5428 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5429 and later it is offset sized. */
5430 if (cu->header.version == 2)
5431 info_ptr += cu->header.addr_size;
5432 else
5433 info_ptr += cu->header.offset_size;
5434 break;
5435 case DW_FORM_GNU_ref_alt:
5436 info_ptr += cu->header.offset_size;
5437 break;
5438 case DW_FORM_addr:
5439 info_ptr += cu->header.addr_size;
5440 break;
5441 case DW_FORM_data1:
5442 case DW_FORM_ref1:
5443 case DW_FORM_flag:
5444 case DW_FORM_strx1:
5445 info_ptr += 1;
5446 break;
5447 case DW_FORM_flag_present:
5448 case DW_FORM_implicit_const:
5449 break;
5450 case DW_FORM_data2:
5451 case DW_FORM_ref2:
5452 case DW_FORM_strx2:
5453 info_ptr += 2;
5454 break;
5455 case DW_FORM_strx3:
5456 info_ptr += 3;
5457 break;
5458 case DW_FORM_data4:
5459 case DW_FORM_ref4:
5460 case DW_FORM_strx4:
5461 info_ptr += 4;
5462 break;
5463 case DW_FORM_data8:
5464 case DW_FORM_ref8:
5465 case DW_FORM_ref_sig8:
5466 info_ptr += 8;
5467 break;
5468 case DW_FORM_data16:
5469 info_ptr += 16;
5470 break;
5471 case DW_FORM_string:
5472 read_direct_string (abfd, info_ptr, &bytes_read);
5473 info_ptr += bytes_read;
5474 break;
5475 case DW_FORM_sec_offset:
5476 case DW_FORM_strp:
5477 case DW_FORM_GNU_strp_alt:
5478 info_ptr += cu->header.offset_size;
5479 break;
5480 case DW_FORM_exprloc:
5481 case DW_FORM_block:
5482 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5483 info_ptr += bytes_read;
5484 break;
5485 case DW_FORM_block1:
5486 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5487 break;
5488 case DW_FORM_block2:
5489 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5490 break;
5491 case DW_FORM_block4:
5492 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5493 break;
5494 case DW_FORM_addrx:
5495 case DW_FORM_strx:
5496 case DW_FORM_sdata:
5497 case DW_FORM_udata:
5498 case DW_FORM_ref_udata:
5499 case DW_FORM_GNU_addr_index:
5500 case DW_FORM_GNU_str_index:
5501 case DW_FORM_rnglistx:
5502 case DW_FORM_loclistx:
5503 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
5504 break;
5505 case DW_FORM_indirect:
5506 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5507 info_ptr += bytes_read;
5508 /* We need to continue parsing from here, so just go back to
5509 the top. */
5510 goto skip_attribute;
5511
5512 default:
5513 error (_("Dwarf Error: Cannot handle %s "
5514 "in DWARF reader [in module %s]"),
5515 dwarf_form_name (form),
5516 bfd_get_filename (abfd));
5517 }
5518 }
5519
5520 if (do_skip_children && abbrev->has_children)
5521 return skip_children (reader, info_ptr);
5522 else
5523 return info_ptr;
5524 }
5525 \f
5526 /* Reading in full CUs. */
5527
5528 /* Add PER_CU to the queue. */
5529
5530 static void
5531 queue_comp_unit (dwarf2_per_cu_data *per_cu,
5532 dwarf2_per_objfile *per_objfile,
5533 enum language pretend_language)
5534 {
5535 per_cu->queued = 1;
5536
5537 gdb_assert (per_objfile->queue.has_value ());
5538 per_objfile->queue->emplace (per_cu, per_objfile, pretend_language);
5539 }
5540
5541 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
5542
5543 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
5544 dependency.
5545
5546 Return true if maybe_queue_comp_unit requires the caller to load the CU's
5547 DIEs, false otherwise.
5548
5549 Explanation: there is an invariant that if a CU is queued for expansion
5550 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
5551 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
5552 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
5553 are not yet loaded, the the caller must load the CU's DIEs to ensure the
5554 invariant is respected.
5555
5556 The caller is therefore not required to load the CU's DIEs (we return false)
5557 if:
5558
5559 - the CU is already expanded, and therefore does not get enqueued
5560 - the CU gets enqueued for expansion, but its DIEs are already loaded
5561
5562 Note that the caller should not use this function's return value as an
5563 indicator of whether the CU's DIEs are loaded right now, it should check
5564 that by calling `dwarf2_per_objfile::get_cu` instead. */
5565
5566 static int
5567 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
5568 dwarf2_per_cu_data *per_cu,
5569 dwarf2_per_objfile *per_objfile,
5570 enum language pretend_language)
5571 {
5572 /* Mark the dependence relation so that we don't flush PER_CU
5573 too early. */
5574 if (dependent_cu != NULL)
5575 dependent_cu->add_dependence (per_cu);
5576
5577 /* If it's already on the queue, we have nothing to do. */
5578 if (per_cu->queued)
5579 {
5580 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
5581 loaded. */
5582 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
5583
5584 /* If the CU is queued for expansion, it should not already be
5585 expanded. */
5586 gdb_assert (!per_objfile->symtab_set_p (per_cu));
5587
5588 /* The DIEs are already loaded, the caller doesn't need to do it. */
5589 return 0;
5590 }
5591
5592 bool queued = false;
5593 if (!per_objfile->symtab_set_p (per_cu))
5594 {
5595 /* Add it to the queue. */
5596 queue_comp_unit (per_cu, per_objfile, pretend_language);
5597 queued = true;
5598 }
5599
5600 /* If the compilation unit is already loaded, just mark it as
5601 used. */
5602 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
5603 if (cu != nullptr)
5604 cu->last_used = 0;
5605
5606 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
5607 and the DIEs are not already loaded. */
5608 return queued && cu == nullptr;
5609 }
5610
5611 /* Process the queue. */
5612
5613 static void
5614 process_queue (dwarf2_per_objfile *per_objfile)
5615 {
5616 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
5617 objfile_name (per_objfile->objfile));
5618
5619 /* The queue starts out with one item, but following a DIE reference
5620 may load a new CU, adding it to the end of the queue. */
5621 while (!per_objfile->queue->empty ())
5622 {
5623 dwarf2_queue_item &item = per_objfile->queue->front ();
5624 dwarf2_per_cu_data *per_cu = item.per_cu;
5625
5626 if (!per_objfile->symtab_set_p (per_cu))
5627 {
5628 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
5629
5630 /* Skip dummy CUs. */
5631 if (cu != nullptr)
5632 {
5633 unsigned int debug_print_threshold;
5634 char buf[100];
5635
5636 if (per_cu->is_debug_types)
5637 {
5638 struct signatured_type *sig_type =
5639 (struct signatured_type *) per_cu;
5640
5641 sprintf (buf, "TU %s at offset %s",
5642 hex_string (sig_type->signature),
5643 sect_offset_str (per_cu->sect_off));
5644 /* There can be 100s of TUs.
5645 Only print them in verbose mode. */
5646 debug_print_threshold = 2;
5647 }
5648 else
5649 {
5650 sprintf (buf, "CU at offset %s",
5651 sect_offset_str (per_cu->sect_off));
5652 debug_print_threshold = 1;
5653 }
5654
5655 if (dwarf_read_debug >= debug_print_threshold)
5656 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
5657
5658 if (per_cu->is_debug_types)
5659 process_full_type_unit (cu, item.pretend_language);
5660 else
5661 process_full_comp_unit (cu, item.pretend_language);
5662
5663 if (dwarf_read_debug >= debug_print_threshold)
5664 dwarf_read_debug_printf ("Done expanding %s", buf);
5665 }
5666 }
5667
5668 per_cu->queued = 0;
5669 per_objfile->queue->pop ();
5670 }
5671
5672 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
5673 objfile_name (per_objfile->objfile));
5674 }
5675
5676 /* Load the DIEs associated with PER_CU into memory.
5677
5678 In some cases, the caller, while reading partial symbols, will need to load
5679 the full symbols for the CU for some reason. It will already have a
5680 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
5681 rather than creating a new one. */
5682
5683 static void
5684 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
5685 dwarf2_per_objfile *per_objfile,
5686 dwarf2_cu *existing_cu,
5687 bool skip_partial,
5688 enum language pretend_language)
5689 {
5690 gdb_assert (! this_cu->is_debug_types);
5691
5692 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
5693 if (reader.dummy_p)
5694 return;
5695
5696 struct dwarf2_cu *cu = reader.cu;
5697 const gdb_byte *info_ptr = reader.info_ptr;
5698
5699 gdb_assert (cu->die_hash == NULL);
5700 cu->die_hash =
5701 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
5702 die_info::hash,
5703 die_info::eq,
5704 NULL,
5705 &cu->comp_unit_obstack,
5706 hashtab_obstack_allocate,
5707 dummy_obstack_deallocate);
5708
5709 if (reader.comp_unit_die->has_children)
5710 reader.comp_unit_die->child
5711 = read_die_and_siblings (&reader, reader.info_ptr,
5712 &info_ptr, reader.comp_unit_die);
5713 cu->dies = reader.comp_unit_die;
5714 /* comp_unit_die is not stored in die_hash, no need. */
5715
5716 /* We try not to read any attributes in this function, because not
5717 all CUs needed for references have been loaded yet, and symbol
5718 table processing isn't initialized. But we have to set the CU language,
5719 or we won't be able to build types correctly.
5720 Similarly, if we do not read the producer, we can not apply
5721 producer-specific interpretation. */
5722 prepare_one_comp_unit (cu, cu->dies, pretend_language);
5723
5724 reader.keep ();
5725 }
5726
5727 /* Add a DIE to the delayed physname list. */
5728
5729 static void
5730 add_to_method_list (struct type *type, int fnfield_index, int index,
5731 const char *name, struct die_info *die,
5732 struct dwarf2_cu *cu)
5733 {
5734 struct delayed_method_info mi;
5735 mi.type = type;
5736 mi.fnfield_index = fnfield_index;
5737 mi.index = index;
5738 mi.name = name;
5739 mi.die = die;
5740 cu->method_list.push_back (mi);
5741 }
5742
5743 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
5744 "const" / "volatile". If so, decrements LEN by the length of the
5745 modifier and return true. Otherwise return false. */
5746
5747 template<size_t N>
5748 static bool
5749 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
5750 {
5751 size_t mod_len = sizeof (mod) - 1;
5752 if (len > mod_len && startswith (physname + (len - mod_len), mod))
5753 {
5754 len -= mod_len;
5755 return true;
5756 }
5757 return false;
5758 }
5759
5760 /* Compute the physnames of any methods on the CU's method list.
5761
5762 The computation of method physnames is delayed in order to avoid the
5763 (bad) condition that one of the method's formal parameters is of an as yet
5764 incomplete type. */
5765
5766 static void
5767 compute_delayed_physnames (struct dwarf2_cu *cu)
5768 {
5769 /* Only C++ delays computing physnames. */
5770 if (cu->method_list.empty ())
5771 return;
5772 gdb_assert (cu->lang () == language_cplus);
5773
5774 for (const delayed_method_info &mi : cu->method_list)
5775 {
5776 const char *physname;
5777 struct fn_fieldlist *fn_flp
5778 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
5779 physname = dwarf2_physname (mi.name, mi.die, cu);
5780 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
5781 = physname ? physname : "";
5782
5783 /* Since there's no tag to indicate whether a method is a
5784 const/volatile overload, extract that information out of the
5785 demangled name. */
5786 if (physname != NULL)
5787 {
5788 size_t len = strlen (physname);
5789
5790 while (1)
5791 {
5792 if (physname[len] == ')') /* shortcut */
5793 break;
5794 else if (check_modifier (physname, len, " const"))
5795 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
5796 else if (check_modifier (physname, len, " volatile"))
5797 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
5798 else
5799 break;
5800 }
5801 }
5802 }
5803
5804 /* The list is no longer needed. */
5805 cu->method_list.clear ();
5806 }
5807
5808 /* Go objects should be embedded in a DW_TAG_module DIE,
5809 and it's not clear if/how imported objects will appear.
5810 To keep Go support simple until that's worked out,
5811 go back through what we've read and create something usable.
5812 We could do this while processing each DIE, and feels kinda cleaner,
5813 but that way is more invasive.
5814 This is to, for example, allow the user to type "p var" or "b main"
5815 without having to specify the package name, and allow lookups
5816 of module.object to work in contexts that use the expression
5817 parser. */
5818
5819 static void
5820 fixup_go_packaging (struct dwarf2_cu *cu)
5821 {
5822 gdb::unique_xmalloc_ptr<char> package_name;
5823 struct pending *list;
5824 int i;
5825
5826 for (list = *cu->get_builder ()->get_global_symbols ();
5827 list != NULL;
5828 list = list->next)
5829 {
5830 for (i = 0; i < list->nsyms; ++i)
5831 {
5832 struct symbol *sym = list->symbol[i];
5833
5834 if (sym->language () == language_go
5835 && sym->aclass () == LOC_BLOCK)
5836 {
5837 gdb::unique_xmalloc_ptr<char> this_package_name
5838 = go_symbol_package_name (sym);
5839
5840 if (this_package_name == NULL)
5841 continue;
5842 if (package_name == NULL)
5843 package_name = std::move (this_package_name);
5844 else
5845 {
5846 struct objfile *objfile = cu->per_objfile->objfile;
5847 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
5848 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
5849 (sym->symtab () != NULL
5850 ? symtab_to_filename_for_display
5851 (sym->symtab ())
5852 : objfile_name (objfile)),
5853 this_package_name.get (), package_name.get ());
5854 }
5855 }
5856 }
5857 }
5858
5859 if (package_name != NULL)
5860 {
5861 struct objfile *objfile = cu->per_objfile->objfile;
5862 const char *saved_package_name = objfile->intern (package_name.get ());
5863 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
5864 saved_package_name);
5865 struct symbol *sym;
5866
5867 sym = new (&objfile->objfile_obstack) symbol;
5868 sym->set_language (language_go, &objfile->objfile_obstack);
5869 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
5870 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5871 e.g., "main" finds the "main" module and not C's main(). */
5872 sym->set_domain (STRUCT_DOMAIN);
5873 sym->set_aclass_index (LOC_TYPEDEF);
5874 sym->set_type (type);
5875
5876 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
5877 }
5878 }
5879
5880 /* Allocate a fully-qualified name consisting of the two parts on the
5881 obstack. */
5882
5883 static const char *
5884 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
5885 {
5886 return obconcat (obstack, p1, "::", p2, (char *) NULL);
5887 }
5888
5889 /* A helper that allocates a variant part to attach to a Rust enum
5890 type. OBSTACK is where the results should be allocated. TYPE is
5891 the type we're processing. DISCRIMINANT_INDEX is the index of the
5892 discriminant. It must be the index of one of the fields of TYPE,
5893 or -1 to mean there is no discriminant (univariant enum).
5894 DEFAULT_INDEX is the index of the default field; or -1 if there is
5895 no default. RANGES is indexed by "effective" field number (the
5896 field index, but omitting the discriminant and default fields) and
5897 must hold the discriminant values used by the variants. Note that
5898 RANGES must have a lifetime at least as long as OBSTACK -- either
5899 already allocated on it, or static. */
5900
5901 static void
5902 alloc_rust_variant (struct obstack *obstack, struct type *type,
5903 int discriminant_index, int default_index,
5904 gdb::array_view<discriminant_range> ranges)
5905 {
5906 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
5907 gdb_assert (discriminant_index == -1
5908 || (discriminant_index >= 0
5909 && discriminant_index < type->num_fields ()));
5910 gdb_assert (default_index == -1
5911 || (default_index >= 0 && default_index < type->num_fields ()));
5912
5913 /* We have one variant for each non-discriminant field. */
5914 int n_variants = type->num_fields ();
5915 if (discriminant_index != -1)
5916 --n_variants;
5917
5918 variant *variants = new (obstack) variant[n_variants];
5919 int var_idx = 0;
5920 int range_idx = 0;
5921 for (int i = 0; i < type->num_fields (); ++i)
5922 {
5923 if (i == discriminant_index)
5924 continue;
5925
5926 variants[var_idx].first_field = i;
5927 variants[var_idx].last_field = i + 1;
5928
5929 /* The default field does not need a range, but other fields do.
5930 We skipped the discriminant above. */
5931 if (i != default_index)
5932 {
5933 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
5934 ++range_idx;
5935 }
5936
5937 ++var_idx;
5938 }
5939
5940 gdb_assert (range_idx == ranges.size ());
5941 gdb_assert (var_idx == n_variants);
5942
5943 variant_part *part = new (obstack) variant_part;
5944 part->discriminant_index = discriminant_index;
5945 /* If there is no discriminant, then whether it is signed is of no
5946 consequence. */
5947 part->is_unsigned
5948 = (discriminant_index == -1
5949 ? false
5950 : type->field (discriminant_index).type ()->is_unsigned ());
5951 part->variants = gdb::array_view<variant> (variants, n_variants);
5952
5953 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
5954 gdb::array_view<variant_part> *prop_value
5955 = new (storage) gdb::array_view<variant_part> (part, 1);
5956
5957 struct dynamic_prop prop;
5958 prop.set_variant_parts (prop_value);
5959
5960 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
5961 }
5962
5963 /* Some versions of rustc emitted enums in an unusual way.
5964
5965 Ordinary enums were emitted as unions. The first element of each
5966 structure in the union was named "RUST$ENUM$DISR". This element
5967 held the discriminant.
5968
5969 These versions of Rust also implemented the "non-zero"
5970 optimization. When the enum had two values, and one is empty and
5971 the other holds a pointer that cannot be zero, the pointer is used
5972 as the discriminant, with a zero value meaning the empty variant.
5973 Here, the union's first member is of the form
5974 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
5975 where the fieldnos are the indices of the fields that should be
5976 traversed in order to find the field (which may be several fields deep)
5977 and the variantname is the name of the variant of the case when the
5978 field is zero.
5979
5980 This function recognizes whether TYPE is of one of these forms,
5981 and, if so, smashes it to be a variant type. */
5982
5983 static void
5984 quirk_rust_enum (struct type *type, struct objfile *objfile)
5985 {
5986 gdb_assert (type->code () == TYPE_CODE_UNION);
5987
5988 /* We don't need to deal with empty enums. */
5989 if (type->num_fields () == 0)
5990 return;
5991
5992 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
5993 if (type->num_fields () == 1
5994 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
5995 {
5996 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
5997
5998 /* Decode the field name to find the offset of the
5999 discriminant. */
6000 ULONGEST bit_offset = 0;
6001 struct type *field_type = type->field (0).type ();
6002 while (name[0] >= '0' && name[0] <= '9')
6003 {
6004 char *tail;
6005 unsigned long index = strtoul (name, &tail, 10);
6006 name = tail;
6007 if (*name != '$'
6008 || index >= field_type->num_fields ()
6009 || (field_type->field (index).loc_kind ()
6010 != FIELD_LOC_KIND_BITPOS))
6011 {
6012 complaint (_("Could not parse Rust enum encoding string \"%s\""
6013 "[in module %s]"),
6014 type->field (0).name (),
6015 objfile_name (objfile));
6016 return;
6017 }
6018 ++name;
6019
6020 bit_offset += field_type->field (index).loc_bitpos ();
6021 field_type = field_type->field (index).type ();
6022 }
6023
6024 /* Smash this type to be a structure type. We have to do this
6025 because the type has already been recorded. */
6026 type->set_code (TYPE_CODE_STRUCT);
6027 type->set_num_fields (3);
6028 /* Save the field we care about. */
6029 struct field saved_field = type->field (0);
6030 type->set_fields
6031 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
6032
6033 /* Put the discriminant at index 0. */
6034 type->field (0).set_type (field_type);
6035 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
6036 type->field (0).set_name ("<<discriminant>>");
6037 type->field (0).set_loc_bitpos (bit_offset);
6038
6039 /* The order of fields doesn't really matter, so put the real
6040 field at index 1 and the data-less field at index 2. */
6041 type->field (1) = saved_field;
6042 type->field (1).set_name
6043 (rust_last_path_segment (type->field (1).type ()->name ()));
6044 type->field (1).type ()->set_name
6045 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
6046 type->field (1).name ()));
6047
6048 const char *dataless_name
6049 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
6050 name);
6051 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
6052 dataless_name);
6053 type->field (2).set_type (dataless_type);
6054 /* NAME points into the original discriminant name, which
6055 already has the correct lifetime. */
6056 type->field (2).set_name (name);
6057 type->field (2).set_loc_bitpos (0);
6058
6059 /* Indicate that this is a variant type. */
6060 static discriminant_range ranges[1] = { { 0, 0 } };
6061 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
6062 }
6063 /* A union with a single anonymous field is probably an old-style
6064 univariant enum. */
6065 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
6066 {
6067 /* Smash this type to be a structure type. We have to do this
6068 because the type has already been recorded. */
6069 type->set_code (TYPE_CODE_STRUCT);
6070
6071 struct type *field_type = type->field (0).type ();
6072 const char *variant_name
6073 = rust_last_path_segment (field_type->name ());
6074 type->field (0).set_name (variant_name);
6075 field_type->set_name
6076 (rust_fully_qualify (&objfile->objfile_obstack,
6077 type->name (), variant_name));
6078
6079 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
6080 }
6081 else
6082 {
6083 struct type *disr_type = nullptr;
6084 for (int i = 0; i < type->num_fields (); ++i)
6085 {
6086 disr_type = type->field (i).type ();
6087
6088 if (disr_type->code () != TYPE_CODE_STRUCT)
6089 {
6090 /* All fields of a true enum will be structs. */
6091 return;
6092 }
6093 else if (disr_type->num_fields () == 0)
6094 {
6095 /* Could be data-less variant, so keep going. */
6096 disr_type = nullptr;
6097 }
6098 else if (strcmp (disr_type->field (0).name (),
6099 "RUST$ENUM$DISR") != 0)
6100 {
6101 /* Not a Rust enum. */
6102 return;
6103 }
6104 else
6105 {
6106 /* Found one. */
6107 break;
6108 }
6109 }
6110
6111 /* If we got here without a discriminant, then it's probably
6112 just a union. */
6113 if (disr_type == nullptr)
6114 return;
6115
6116 /* Smash this type to be a structure type. We have to do this
6117 because the type has already been recorded. */
6118 type->set_code (TYPE_CODE_STRUCT);
6119
6120 /* Make space for the discriminant field. */
6121 struct field *disr_field = &disr_type->field (0);
6122 field *new_fields
6123 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
6124 * sizeof (struct field)));
6125 memcpy (new_fields + 1, type->fields (),
6126 type->num_fields () * sizeof (struct field));
6127 type->set_fields (new_fields);
6128 type->set_num_fields (type->num_fields () + 1);
6129
6130 /* Install the discriminant at index 0 in the union. */
6131 type->field (0) = *disr_field;
6132 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
6133 type->field (0).set_name ("<<discriminant>>");
6134
6135 /* We need a way to find the correct discriminant given a
6136 variant name. For convenience we build a map here. */
6137 struct type *enum_type = disr_field->type ();
6138 std::unordered_map<std::string, ULONGEST> discriminant_map;
6139 for (int i = 0; i < enum_type->num_fields (); ++i)
6140 {
6141 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
6142 {
6143 const char *name
6144 = rust_last_path_segment (enum_type->field (i).name ());
6145 discriminant_map[name] = enum_type->field (i).loc_enumval ();
6146 }
6147 }
6148
6149 int n_fields = type->num_fields ();
6150 /* We don't need a range entry for the discriminant, but we do
6151 need one for every other field, as there is no default
6152 variant. */
6153 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
6154 discriminant_range,
6155 n_fields - 1);
6156 /* Skip the discriminant here. */
6157 for (int i = 1; i < n_fields; ++i)
6158 {
6159 /* Find the final word in the name of this variant's type.
6160 That name can be used to look up the correct
6161 discriminant. */
6162 const char *variant_name
6163 = rust_last_path_segment (type->field (i).type ()->name ());
6164
6165 auto iter = discriminant_map.find (variant_name);
6166 if (iter != discriminant_map.end ())
6167 {
6168 ranges[i - 1].low = iter->second;
6169 ranges[i - 1].high = iter->second;
6170 }
6171
6172 /* In Rust, each element should have the size of the
6173 enclosing enum. */
6174 type->field (i).type ()->set_length (type->length ());
6175
6176 /* Remove the discriminant field, if it exists. */
6177 struct type *sub_type = type->field (i).type ();
6178 if (sub_type->num_fields () > 0)
6179 {
6180 sub_type->set_num_fields (sub_type->num_fields () - 1);
6181 sub_type->set_fields (sub_type->fields () + 1);
6182 }
6183 type->field (i).set_name (variant_name);
6184 sub_type->set_name
6185 (rust_fully_qualify (&objfile->objfile_obstack,
6186 type->name (), variant_name));
6187 }
6188
6189 /* Indicate that this is a variant type. */
6190 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
6191 gdb::array_view<discriminant_range> (ranges,
6192 n_fields - 1));
6193 }
6194 }
6195
6196 /* Rewrite some Rust unions to be structures with variants parts. */
6197
6198 static void
6199 rust_union_quirks (struct dwarf2_cu *cu)
6200 {
6201 gdb_assert (cu->lang () == language_rust);
6202 for (type *type_ : cu->rust_unions)
6203 quirk_rust_enum (type_, cu->per_objfile->objfile);
6204 /* We don't need this any more. */
6205 cu->rust_unions.clear ();
6206 }
6207
6208 /* See read.h. */
6209
6210 type_unit_group_unshareable *
6211 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
6212 {
6213 auto iter = this->m_type_units.find (tu_group);
6214 if (iter != this->m_type_units.end ())
6215 return iter->second.get ();
6216
6217 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
6218 type_unit_group_unshareable *result = uniq.get ();
6219 this->m_type_units[tu_group] = std::move (uniq);
6220 return result;
6221 }
6222
6223 struct type *
6224 dwarf2_per_objfile::get_type_for_signatured_type
6225 (signatured_type *sig_type) const
6226 {
6227 auto iter = this->m_type_map.find (sig_type);
6228 if (iter == this->m_type_map.end ())
6229 return nullptr;
6230
6231 return iter->second;
6232 }
6233
6234 void dwarf2_per_objfile::set_type_for_signatured_type
6235 (signatured_type *sig_type, struct type *type)
6236 {
6237 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
6238
6239 this->m_type_map[sig_type] = type;
6240 }
6241
6242 /* A helper function for computing the list of all symbol tables
6243 included by PER_CU. */
6244
6245 static void
6246 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
6247 htab_t all_children, htab_t all_type_symtabs,
6248 dwarf2_per_cu_data *per_cu,
6249 dwarf2_per_objfile *per_objfile,
6250 struct compunit_symtab *immediate_parent)
6251 {
6252 void **slot = htab_find_slot (all_children, per_cu, INSERT);
6253 if (*slot != NULL)
6254 {
6255 /* This inclusion and its children have been processed. */
6256 return;
6257 }
6258
6259 *slot = per_cu;
6260
6261 /* Only add a CU if it has a symbol table. */
6262 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6263 if (cust != NULL)
6264 {
6265 /* If this is a type unit only add its symbol table if we haven't
6266 seen it yet (type unit per_cu's can share symtabs). */
6267 if (per_cu->is_debug_types)
6268 {
6269 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
6270 if (*slot == NULL)
6271 {
6272 *slot = cust;
6273 result->push_back (cust);
6274 if (cust->user == NULL)
6275 cust->user = immediate_parent;
6276 }
6277 }
6278 else
6279 {
6280 result->push_back (cust);
6281 if (cust->user == NULL)
6282 cust->user = immediate_parent;
6283 }
6284 }
6285
6286 if (!per_cu->imported_symtabs_empty ())
6287 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
6288 {
6289 recursively_compute_inclusions (result, all_children,
6290 all_type_symtabs, ptr, per_objfile,
6291 cust);
6292 }
6293 }
6294
6295 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
6296 PER_CU. */
6297
6298 static void
6299 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
6300 dwarf2_per_objfile *per_objfile)
6301 {
6302 gdb_assert (! per_cu->is_debug_types);
6303
6304 if (!per_cu->imported_symtabs_empty ())
6305 {
6306 int len;
6307 std::vector<compunit_symtab *> result_symtabs;
6308 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6309
6310 /* If we don't have a symtab, we can just skip this case. */
6311 if (cust == NULL)
6312 return;
6313
6314 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
6315 htab_eq_pointer,
6316 NULL, xcalloc, xfree));
6317 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
6318 htab_eq_pointer,
6319 NULL, xcalloc, xfree));
6320
6321 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
6322 {
6323 recursively_compute_inclusions (&result_symtabs, all_children.get (),
6324 all_type_symtabs.get (), ptr,
6325 per_objfile, cust);
6326 }
6327
6328 /* Now we have a transitive closure of all the included symtabs. */
6329 len = result_symtabs.size ();
6330 cust->includes
6331 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
6332 struct compunit_symtab *, len + 1);
6333 memcpy (cust->includes, result_symtabs.data (),
6334 len * sizeof (compunit_symtab *));
6335 cust->includes[len] = NULL;
6336 }
6337 }
6338
6339 /* Compute the 'includes' field for the symtabs of all the CUs we just
6340 read. */
6341
6342 static void
6343 process_cu_includes (dwarf2_per_objfile *per_objfile)
6344 {
6345 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
6346 {
6347 if (! iter->is_debug_types)
6348 compute_compunit_symtab_includes (iter, per_objfile);
6349 }
6350
6351 per_objfile->per_bfd->just_read_cus.clear ();
6352 }
6353
6354 /* Generate full symbol information for CU, whose DIEs have
6355 already been loaded into memory. */
6356
6357 static void
6358 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
6359 {
6360 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6361 struct objfile *objfile = per_objfile->objfile;
6362 struct gdbarch *gdbarch = objfile->arch ();
6363 CORE_ADDR lowpc, highpc;
6364 struct compunit_symtab *cust;
6365 CORE_ADDR baseaddr;
6366 struct block *static_block;
6367 CORE_ADDR addr;
6368
6369 baseaddr = objfile->text_section_offset ();
6370
6371 /* Clear the list here in case something was left over. */
6372 cu->method_list.clear ();
6373
6374 dwarf2_find_base_address (cu->dies, cu);
6375
6376 /* Before we start reading the top-level DIE, ensure it has a valid tag
6377 type. */
6378 switch (cu->dies->tag)
6379 {
6380 case DW_TAG_compile_unit:
6381 case DW_TAG_partial_unit:
6382 case DW_TAG_type_unit:
6383 break;
6384 default:
6385 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6386 dwarf_tag_name (cu->dies->tag),
6387 sect_offset_str (cu->per_cu->sect_off),
6388 objfile_name (per_objfile->objfile));
6389 }
6390
6391 /* Do line number decoding in read_file_scope () */
6392 process_die (cu->dies, cu);
6393
6394 /* For now fudge the Go package. */
6395 if (cu->lang () == language_go)
6396 fixup_go_packaging (cu);
6397
6398 /* Now that we have processed all the DIEs in the CU, all the types
6399 should be complete, and it should now be safe to compute all of the
6400 physnames. */
6401 compute_delayed_physnames (cu);
6402
6403 if (cu->lang () == language_rust)
6404 rust_union_quirks (cu);
6405
6406 /* Some compilers don't define a DW_AT_high_pc attribute for the
6407 compilation unit. If the DW_AT_high_pc is missing, synthesize
6408 it, by scanning the DIE's below the compilation unit. */
6409 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6410
6411 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
6412 static_block
6413 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
6414
6415 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6416 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6417 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6418 addrmap to help ensure it has an accurate map of pc values belonging to
6419 this comp unit. */
6420 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6421
6422 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
6423 (static_block, 0);
6424
6425 if (cust != NULL)
6426 {
6427 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6428
6429 /* Set symtab language to language from DW_AT_language. If the
6430 compilation is from a C file generated by language preprocessors, do
6431 not set the language if it was already deduced by start_subfile. */
6432 if (!(cu->lang () == language_c
6433 && cust->primary_filetab ()->language () != language_unknown))
6434 cust->primary_filetab ()->set_language (cu->lang ());
6435
6436 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6437 produce DW_AT_location with location lists but it can be possibly
6438 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6439 there were bugs in prologue debug info, fixed later in GCC-4.5
6440 by "unwind info for epilogues" patch (which is not directly related).
6441
6442 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6443 needed, it would be wrong due to missing DW_AT_producer there.
6444
6445 Still one can confuse GDB by using non-standard GCC compilation
6446 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6447 */
6448 if (cu->has_loclist && gcc_4_minor >= 5)
6449 cust->set_locations_valid (true);
6450
6451 int major, minor;
6452 if (cu->producer != nullptr
6453 && producer_is_gcc (cu->producer, &major, &minor)
6454 && (major < 4 || (major == 4 && minor < 5)))
6455 /* Don't trust gcc < 4.5.x. */
6456 cust->set_epilogue_unwind_valid (false);
6457 else
6458 cust->set_epilogue_unwind_valid (true);
6459
6460 cust->set_call_site_htab (cu->call_site_htab);
6461 }
6462
6463 per_objfile->set_symtab (cu->per_cu, cust);
6464
6465 /* Push it for inclusion processing later. */
6466 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
6467
6468 /* Not needed any more. */
6469 cu->reset_builder ();
6470 }
6471
6472 /* Generate full symbol information for type unit CU, whose DIEs have
6473 already been loaded into memory. */
6474
6475 static void
6476 process_full_type_unit (dwarf2_cu *cu,
6477 enum language pretend_language)
6478 {
6479 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6480 struct compunit_symtab *cust;
6481 struct signatured_type *sig_type;
6482
6483 gdb_assert (cu->per_cu->is_debug_types);
6484 sig_type = (struct signatured_type *) cu->per_cu;
6485
6486 /* Clear the list here in case something was left over. */
6487 cu->method_list.clear ();
6488
6489 /* The symbol tables are set up in read_type_unit_scope. */
6490 process_die (cu->dies, cu);
6491
6492 /* For now fudge the Go package. */
6493 if (cu->lang () == language_go)
6494 fixup_go_packaging (cu);
6495
6496 /* Now that we have processed all the DIEs in the CU, all the types
6497 should be complete, and it should now be safe to compute all of the
6498 physnames. */
6499 compute_delayed_physnames (cu);
6500
6501 if (cu->lang () == language_rust)
6502 rust_union_quirks (cu);
6503
6504 /* TUs share symbol tables.
6505 If this is the first TU to use this symtab, complete the construction
6506 of it with end_expandable_symtab. Otherwise, complete the addition of
6507 this TU's symbols to the existing symtab. */
6508 type_unit_group_unshareable *tug_unshare =
6509 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
6510 if (tug_unshare->compunit_symtab == NULL)
6511 {
6512 buildsym_compunit *builder = cu->get_builder ();
6513 cust = builder->end_expandable_symtab (0);
6514 tug_unshare->compunit_symtab = cust;
6515
6516 if (cust != NULL)
6517 {
6518 /* Set symtab language to language from DW_AT_language. If the
6519 compilation is from a C file generated by language preprocessors,
6520 do not set the language if it was already deduced by
6521 start_subfile. */
6522 if (!(cu->lang () == language_c
6523 && cust->primary_filetab ()->language () != language_c))
6524 cust->primary_filetab ()->set_language (cu->lang ());
6525 }
6526 }
6527 else
6528 {
6529 cu->get_builder ()->augment_type_symtab ();
6530 cust = tug_unshare->compunit_symtab;
6531 }
6532
6533 per_objfile->set_symtab (cu->per_cu, cust);
6534
6535 /* Not needed any more. */
6536 cu->reset_builder ();
6537 }
6538
6539 /* Process an imported unit DIE. */
6540
6541 static void
6542 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6543 {
6544 struct attribute *attr;
6545
6546 /* For now we don't handle imported units in type units. */
6547 if (cu->per_cu->is_debug_types)
6548 {
6549 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6550 " supported in type units [in module %s]"),
6551 objfile_name (cu->per_objfile->objfile));
6552 }
6553
6554 attr = dwarf2_attr (die, DW_AT_import, cu);
6555 if (attr != NULL)
6556 {
6557 sect_offset sect_off = attr->get_ref_die_offset ();
6558 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
6559 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6560 dwarf2_per_cu_data *per_cu
6561 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
6562 per_objfile->per_bfd);
6563
6564 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
6565 into another compilation unit, at root level. Regard this as a hint,
6566 and ignore it. This is a best effort, it only works if unit_type and
6567 lang are already set. */
6568 if (die->parent && die->parent->parent == NULL
6569 && per_cu->unit_type (false) == DW_UT_compile
6570 && per_cu->lang (false) == language_cplus)
6571 return;
6572
6573 /* If necessary, add it to the queue and load its DIEs. */
6574 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
6575 cu->lang ()))
6576 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
6577 false, cu->lang ());
6578
6579 cu->per_cu->imported_symtabs_push (per_cu);
6580 }
6581 }
6582
6583 /* RAII object that represents a process_die scope: i.e.,
6584 starts/finishes processing a DIE. */
6585 class process_die_scope
6586 {
6587 public:
6588 process_die_scope (die_info *die, dwarf2_cu *cu)
6589 : m_die (die), m_cu (cu)
6590 {
6591 /* We should only be processing DIEs not already in process. */
6592 gdb_assert (!m_die->in_process);
6593 m_die->in_process = true;
6594 }
6595
6596 ~process_die_scope ()
6597 {
6598 m_die->in_process = false;
6599
6600 /* If we're done processing the DIE for the CU that owns the line
6601 header, we don't need the line header anymore. */
6602 if (m_cu->line_header_die_owner == m_die)
6603 {
6604 delete m_cu->line_header;
6605 m_cu->line_header = NULL;
6606 m_cu->line_header_die_owner = NULL;
6607 }
6608 }
6609
6610 private:
6611 die_info *m_die;
6612 dwarf2_cu *m_cu;
6613 };
6614
6615 /* Process a die and its children. */
6616
6617 static void
6618 process_die (struct die_info *die, struct dwarf2_cu *cu)
6619 {
6620 process_die_scope scope (die, cu);
6621
6622 switch (die->tag)
6623 {
6624 case DW_TAG_padding:
6625 break;
6626 case DW_TAG_compile_unit:
6627 case DW_TAG_partial_unit:
6628 read_file_scope (die, cu);
6629 break;
6630 case DW_TAG_type_unit:
6631 read_type_unit_scope (die, cu);
6632 break;
6633 case DW_TAG_subprogram:
6634 /* Nested subprograms in Fortran get a prefix. */
6635 if (cu->lang () == language_fortran
6636 && die->parent != NULL
6637 && die->parent->tag == DW_TAG_subprogram)
6638 cu->processing_has_namespace_info = true;
6639 /* Fall through. */
6640 case DW_TAG_inlined_subroutine:
6641 read_func_scope (die, cu);
6642 break;
6643 case DW_TAG_lexical_block:
6644 case DW_TAG_try_block:
6645 case DW_TAG_catch_block:
6646 read_lexical_block_scope (die, cu);
6647 break;
6648 case DW_TAG_call_site:
6649 case DW_TAG_GNU_call_site:
6650 read_call_site_scope (die, cu);
6651 break;
6652 case DW_TAG_class_type:
6653 case DW_TAG_interface_type:
6654 case DW_TAG_structure_type:
6655 case DW_TAG_union_type:
6656 case DW_TAG_namelist:
6657 process_structure_scope (die, cu);
6658 break;
6659 case DW_TAG_enumeration_type:
6660 process_enumeration_scope (die, cu);
6661 break;
6662
6663 /* These dies have a type, but processing them does not create
6664 a symbol or recurse to process the children. Therefore we can
6665 read them on-demand through read_type_die. */
6666 case DW_TAG_subroutine_type:
6667 case DW_TAG_set_type:
6668 case DW_TAG_pointer_type:
6669 case DW_TAG_ptr_to_member_type:
6670 case DW_TAG_reference_type:
6671 case DW_TAG_rvalue_reference_type:
6672 case DW_TAG_string_type:
6673 break;
6674
6675 case DW_TAG_array_type:
6676 /* We only need to handle this case for Ada -- in other
6677 languages, it's normal for the compiler to emit a typedef
6678 instead. */
6679 if (cu->lang () != language_ada)
6680 break;
6681 /* FALLTHROUGH */
6682 case DW_TAG_base_type:
6683 case DW_TAG_subrange_type:
6684 case DW_TAG_generic_subrange:
6685 case DW_TAG_typedef:
6686 /* Add a typedef symbol for the type definition, if it has a
6687 DW_AT_name. */
6688 new_symbol (die, read_type_die (die, cu), cu);
6689 break;
6690 case DW_TAG_common_block:
6691 read_common_block (die, cu);
6692 break;
6693 case DW_TAG_common_inclusion:
6694 break;
6695 case DW_TAG_namespace:
6696 cu->processing_has_namespace_info = true;
6697 read_namespace (die, cu);
6698 break;
6699 case DW_TAG_module:
6700 cu->processing_has_namespace_info = true;
6701 read_module (die, cu);
6702 break;
6703 case DW_TAG_imported_declaration:
6704 cu->processing_has_namespace_info = true;
6705 if (read_alias (die, cu))
6706 break;
6707 /* The declaration is neither a global namespace nor a variable
6708 alias. */
6709 /* Fall through. */
6710 case DW_TAG_imported_module:
6711 cu->processing_has_namespace_info = true;
6712 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6713 || cu->lang () != language_fortran))
6714 complaint (_("Tag '%s' has unexpected children"),
6715 dwarf_tag_name (die->tag));
6716 read_import_statement (die, cu);
6717 break;
6718
6719 case DW_TAG_imported_unit:
6720 process_imported_unit_die (die, cu);
6721 break;
6722
6723 case DW_TAG_variable:
6724 read_variable (die, cu);
6725 break;
6726
6727 default:
6728 new_symbol (die, NULL, cu);
6729 break;
6730 }
6731 }
6732 \f
6733 /* DWARF name computation. */
6734
6735 /* A helper function for dwarf2_compute_name which determines whether DIE
6736 needs to have the name of the scope prepended to the name listed in the
6737 die. */
6738
6739 static int
6740 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6741 {
6742 struct attribute *attr;
6743
6744 switch (die->tag)
6745 {
6746 case DW_TAG_namespace:
6747 case DW_TAG_typedef:
6748 case DW_TAG_class_type:
6749 case DW_TAG_interface_type:
6750 case DW_TAG_structure_type:
6751 case DW_TAG_union_type:
6752 case DW_TAG_enumeration_type:
6753 case DW_TAG_enumerator:
6754 case DW_TAG_subprogram:
6755 case DW_TAG_inlined_subroutine:
6756 case DW_TAG_member:
6757 case DW_TAG_imported_declaration:
6758 return 1;
6759
6760 case DW_TAG_variable:
6761 case DW_TAG_constant:
6762 /* We only need to prefix "globally" visible variables. These include
6763 any variable marked with DW_AT_external or any variable that
6764 lives in a namespace. [Variables in anonymous namespaces
6765 require prefixing, but they are not DW_AT_external.] */
6766
6767 if (dwarf2_attr (die, DW_AT_specification, cu))
6768 {
6769 struct dwarf2_cu *spec_cu = cu;
6770
6771 return die_needs_namespace (die_specification (die, &spec_cu),
6772 spec_cu);
6773 }
6774
6775 attr = dwarf2_attr (die, DW_AT_external, cu);
6776 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6777 && die->parent->tag != DW_TAG_module)
6778 return 0;
6779 /* A variable in a lexical block of some kind does not need a
6780 namespace, even though in C++ such variables may be external
6781 and have a mangled name. */
6782 if (die->parent->tag == DW_TAG_lexical_block
6783 || die->parent->tag == DW_TAG_try_block
6784 || die->parent->tag == DW_TAG_catch_block
6785 || die->parent->tag == DW_TAG_subprogram)
6786 return 0;
6787 return 1;
6788
6789 default:
6790 return 0;
6791 }
6792 }
6793
6794 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
6795 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6796 defined for the given DIE. */
6797
6798 static struct attribute *
6799 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
6800 {
6801 struct attribute *attr;
6802
6803 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6804 if (attr == NULL)
6805 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6806
6807 return attr;
6808 }
6809
6810 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
6811 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6812 defined for the given DIE. */
6813
6814 static const char *
6815 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
6816 {
6817 const char *linkage_name;
6818
6819 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
6820 if (linkage_name == NULL)
6821 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
6822
6823 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
6824 See https://github.com/rust-lang/rust/issues/32925. */
6825 if (cu->lang () == language_rust && linkage_name != NULL
6826 && strchr (linkage_name, '{') != NULL)
6827 linkage_name = NULL;
6828
6829 return linkage_name;
6830 }
6831
6832 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6833 compute the physname for the object, which include a method's:
6834 - formal parameters (C++),
6835 - receiver type (Go),
6836
6837 The term "physname" is a bit confusing.
6838 For C++, for example, it is the demangled name.
6839 For Go, for example, it's the mangled name.
6840
6841 For Ada, return the DIE's linkage name rather than the fully qualified
6842 name. PHYSNAME is ignored..
6843
6844 The result is allocated on the objfile->per_bfd's obstack and
6845 canonicalized. */
6846
6847 static const char *
6848 dwarf2_compute_name (const char *name,
6849 struct die_info *die, struct dwarf2_cu *cu,
6850 int physname)
6851 {
6852 struct objfile *objfile = cu->per_objfile->objfile;
6853
6854 if (name == NULL)
6855 name = dwarf2_name (die, cu);
6856
6857 enum language lang = cu->lang ();
6858
6859 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
6860 but otherwise compute it by typename_concat inside GDB.
6861 FIXME: Actually this is not really true, or at least not always true.
6862 It's all very confusing. compute_and_set_names doesn't try to demangle
6863 Fortran names because there is no mangling standard. So new_symbol
6864 will set the demangled name to the result of dwarf2_full_name, and it is
6865 the demangled name that GDB uses if it exists. */
6866 if (lang == language_ada
6867 || (lang == language_fortran && physname))
6868 {
6869 /* For Ada unit, we prefer the linkage name over the name, as
6870 the former contains the exported name, which the user expects
6871 to be able to reference. Ideally, we want the user to be able
6872 to reference this entity using either natural or linkage name,
6873 but we haven't started looking at this enhancement yet. */
6874 const char *linkage_name = dw2_linkage_name (die, cu);
6875
6876 if (linkage_name != NULL)
6877 return linkage_name;
6878 }
6879
6880 /* These are the only languages we know how to qualify names in. */
6881 if (name != NULL
6882 && (lang == language_cplus
6883 || lang == language_fortran || lang == language_d
6884 || lang == language_rust))
6885 {
6886 if (die_needs_namespace (die, cu))
6887 {
6888 const char *prefix;
6889
6890 string_file buf;
6891
6892 prefix = determine_prefix (die, cu);
6893 if (*prefix != '\0')
6894 {
6895 gdb::unique_xmalloc_ptr<char> prefixed_name
6896 (typename_concat (NULL, prefix, name, physname, cu));
6897
6898 buf.puts (prefixed_name.get ());
6899 }
6900 else
6901 buf.puts (name);
6902
6903 /* Template parameters may be specified in the DIE's DW_AT_name, or
6904 as children with DW_TAG_template_type_param or
6905 DW_TAG_value_type_param. If the latter, add them to the name
6906 here. If the name already has template parameters, then
6907 skip this step; some versions of GCC emit both, and
6908 it is more efficient to use the pre-computed name.
6909
6910 Something to keep in mind about this process: it is very
6911 unlikely, or in some cases downright impossible, to produce
6912 something that will match the mangled name of a function.
6913 If the definition of the function has the same debug info,
6914 we should be able to match up with it anyway. But fallbacks
6915 using the minimal symbol, for instance to find a method
6916 implemented in a stripped copy of libstdc++, will not work.
6917 If we do not have debug info for the definition, we will have to
6918 match them up some other way.
6919
6920 When we do name matching there is a related problem with function
6921 templates; two instantiated function templates are allowed to
6922 differ only by their return types, which we do not add here. */
6923
6924 if (lang == language_cplus && strchr (name, '<') == NULL)
6925 {
6926 struct attribute *attr;
6927 struct die_info *child;
6928 int first = 1;
6929
6930 die->building_fullname = 1;
6931
6932 for (child = die->child; child != NULL; child = child->sibling)
6933 {
6934 struct type *type;
6935 LONGEST value;
6936 const gdb_byte *bytes;
6937 struct dwarf2_locexpr_baton *baton;
6938 struct value *v;
6939
6940 if (child->tag != DW_TAG_template_type_param
6941 && child->tag != DW_TAG_template_value_param)
6942 continue;
6943
6944 if (first)
6945 {
6946 buf.puts ("<");
6947 first = 0;
6948 }
6949 else
6950 buf.puts (", ");
6951
6952 attr = dwarf2_attr (child, DW_AT_type, cu);
6953 if (attr == NULL)
6954 {
6955 complaint (_("template parameter missing DW_AT_type"));
6956 buf.puts ("UNKNOWN_TYPE");
6957 continue;
6958 }
6959 type = die_type (child, cu);
6960
6961 if (child->tag == DW_TAG_template_type_param)
6962 {
6963 cu->language_defn->print_type (type, "", &buf, -1, 0,
6964 &type_print_raw_options);
6965 continue;
6966 }
6967
6968 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6969 if (attr == NULL)
6970 {
6971 complaint (_("template parameter missing "
6972 "DW_AT_const_value"));
6973 buf.puts ("UNKNOWN_VALUE");
6974 continue;
6975 }
6976
6977 dwarf2_const_value_attr (attr, type, name,
6978 &cu->comp_unit_obstack, cu,
6979 &value, &bytes, &baton);
6980
6981 if (type->has_no_signedness ())
6982 /* GDB prints characters as NUMBER 'CHAR'. If that's
6983 changed, this can use value_print instead. */
6984 cu->language_defn->printchar (value, type, &buf);
6985 else
6986 {
6987 struct value_print_options opts;
6988
6989 if (baton != NULL)
6990 v = dwarf2_evaluate_loc_desc (type, NULL,
6991 baton->data,
6992 baton->size,
6993 baton->per_cu,
6994 baton->per_objfile);
6995 else if (bytes != NULL)
6996 {
6997 v = value::allocate (type);
6998 memcpy (v->contents_writeable ().data (), bytes,
6999 type->length ());
7000 }
7001 else
7002 v = value_from_longest (type, value);
7003
7004 /* Specify decimal so that we do not depend on
7005 the radix. */
7006 get_formatted_print_options (&opts, 'd');
7007 opts.raw = true;
7008 value_print (v, &buf, &opts);
7009 release_value (v);
7010 }
7011 }
7012
7013 die->building_fullname = 0;
7014
7015 if (!first)
7016 {
7017 /* Close the argument list, with a space if necessary
7018 (nested templates). */
7019 if (!buf.empty () && buf.string ().back () == '>')
7020 buf.puts (" >");
7021 else
7022 buf.puts (">");
7023 }
7024 }
7025
7026 /* For C++ methods, append formal parameter type
7027 information, if PHYSNAME. */
7028
7029 if (physname && die->tag == DW_TAG_subprogram
7030 && lang == language_cplus)
7031 {
7032 struct type *type = read_type_die (die, cu);
7033
7034 c_type_print_args (type, &buf, 1, lang,
7035 &type_print_raw_options);
7036
7037 if (lang == language_cplus)
7038 {
7039 /* Assume that an artificial first parameter is
7040 "this", but do not crash if it is not. RealView
7041 marks unnamed (and thus unused) parameters as
7042 artificial; there is no way to differentiate
7043 the two cases. */
7044 if (type->num_fields () > 0
7045 && TYPE_FIELD_ARTIFICIAL (type, 0)
7046 && type->field (0).type ()->code () == TYPE_CODE_PTR
7047 && TYPE_CONST (type->field (0).type ()->target_type ()))
7048 buf.puts (" const");
7049 }
7050 }
7051
7052 const std::string &intermediate_name = buf.string ();
7053
7054 const char *canonical_name
7055 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
7056 objfile);
7057
7058 /* If we only computed INTERMEDIATE_NAME, or if
7059 INTERMEDIATE_NAME is already canonical, then we need to
7060 intern it. */
7061 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
7062 name = objfile->intern (intermediate_name);
7063 else
7064 name = canonical_name;
7065 }
7066 }
7067
7068 return name;
7069 }
7070
7071 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7072 If scope qualifiers are appropriate they will be added. The result
7073 will be allocated on the storage_obstack, or NULL if the DIE does
7074 not have a name. NAME may either be from a previous call to
7075 dwarf2_name or NULL.
7076
7077 The output string will be canonicalized (if C++). */
7078
7079 static const char *
7080 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7081 {
7082 return dwarf2_compute_name (name, die, cu, 0);
7083 }
7084
7085 /* Construct a physname for the given DIE in CU. NAME may either be
7086 from a previous call to dwarf2_name or NULL. The result will be
7087 allocated on the objfile_objstack or NULL if the DIE does not have a
7088 name.
7089
7090 The output string will be canonicalized (if C++). */
7091
7092 static const char *
7093 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7094 {
7095 struct objfile *objfile = cu->per_objfile->objfile;
7096 const char *retval, *mangled = NULL, *canon = NULL;
7097 int need_copy = 1;
7098
7099 /* In this case dwarf2_compute_name is just a shortcut not building anything
7100 on its own. */
7101 if (!die_needs_namespace (die, cu))
7102 return dwarf2_compute_name (name, die, cu, 1);
7103
7104 if (cu->lang () != language_rust)
7105 mangled = dw2_linkage_name (die, cu);
7106
7107 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7108 has computed. */
7109 gdb::unique_xmalloc_ptr<char> demangled;
7110 if (mangled != NULL)
7111 {
7112 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
7113 {
7114 /* Do nothing (do not demangle the symbol name). */
7115 }
7116 else
7117 {
7118 /* Use DMGL_RET_DROP for C++ template functions to suppress
7119 their return type. It is easier for GDB users to search
7120 for such functions as `name(params)' than `long name(params)'.
7121 In such case the minimal symbol names do not match the full
7122 symbol names but for template functions there is never a need
7123 to look up their definition from their declaration so
7124 the only disadvantage remains the minimal symbol variant
7125 `long name(params)' does not have the proper inferior type. */
7126 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
7127 | DMGL_RET_DROP));
7128 }
7129 if (demangled)
7130 canon = demangled.get ();
7131 else
7132 {
7133 canon = mangled;
7134 need_copy = 0;
7135 }
7136 }
7137
7138 if (canon == NULL || check_physname)
7139 {
7140 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7141
7142 if (canon != NULL && strcmp (physname, canon) != 0)
7143 {
7144 /* It may not mean a bug in GDB. The compiler could also
7145 compute DW_AT_linkage_name incorrectly. But in such case
7146 GDB would need to be bug-to-bug compatible. */
7147
7148 complaint (_("Computed physname <%s> does not match demangled <%s> "
7149 "(from linkage <%s>) - DIE at %s [in module %s]"),
7150 physname, canon, mangled, sect_offset_str (die->sect_off),
7151 objfile_name (objfile));
7152
7153 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7154 is available here - over computed PHYSNAME. It is safer
7155 against both buggy GDB and buggy compilers. */
7156
7157 retval = canon;
7158 }
7159 else
7160 {
7161 retval = physname;
7162 need_copy = 0;
7163 }
7164 }
7165 else
7166 retval = canon;
7167
7168 if (need_copy)
7169 retval = objfile->intern (retval);
7170
7171 return retval;
7172 }
7173
7174 /* Inspect DIE in CU for a namespace alias or a variable with alias
7175 attribute. If one exists, record a new symbol for it.
7176
7177 Returns true if an alias was recorded, false otherwise. */
7178
7179 static bool
7180 read_alias (struct die_info *die, struct dwarf2_cu *cu)
7181 {
7182 struct attribute *attr;
7183
7184 /* If the die does not have a name, this is neither a namespace
7185 alias nor a variable alias. */
7186 attr = dwarf2_attr (die, DW_AT_name, cu);
7187 if (attr != NULL)
7188 {
7189 int num;
7190 struct die_info *d = die;
7191 struct dwarf2_cu *imported_cu = cu;
7192
7193 /* If the compiler has nested DW_AT_imported_declaration DIEs,
7194 keep inspecting DIEs until we hit the underlying import. */
7195 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
7196 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
7197 {
7198 attr = dwarf2_attr (d, DW_AT_import, cu);
7199 if (attr == NULL)
7200 break;
7201
7202 d = follow_die_ref (d, attr, &imported_cu);
7203 if (d->tag != DW_TAG_imported_declaration)
7204 break;
7205 }
7206
7207 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
7208 {
7209 complaint (_("DIE at %s has too many recursively imported "
7210 "declarations"), sect_offset_str (d->sect_off));
7211 return false;
7212 }
7213
7214 if (attr != NULL)
7215 {
7216 struct type *type;
7217 if (d->tag == DW_TAG_variable)
7218 {
7219 /* This declaration is a C/C++ global variable alias.
7220 Add a symbol for it whose type is the same as the
7221 aliased variable's. */
7222 type = die_type (d, imported_cu);
7223 struct symbol *sym = new_symbol (die, type, cu);
7224 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
7225 sym->set_aclass_index (LOC_UNRESOLVED);
7226 if (attr != nullptr)
7227 var_decode_location (attr, sym, cu);
7228 return true;
7229 }
7230 else
7231 {
7232 sect_offset sect_off = attr->get_ref_die_offset ();
7233 type = get_die_type_at_offset (sect_off, cu->per_cu,
7234 cu->per_objfile);
7235 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
7236 {
7237 /* This declaration is a global namespace alias. Add
7238 a symbol for it whose type is the aliased
7239 namespace. */
7240 new_symbol (die, type, cu);
7241 return true;
7242 }
7243 }
7244 }
7245 }
7246 return false;
7247 }
7248
7249 /* Return the using directives repository (global or local?) to use in the
7250 current context for CU.
7251
7252 For Ada, imported declarations can materialize renamings, which *may* be
7253 global. However it is impossible (for now?) in DWARF to distinguish
7254 "external" imported declarations and "static" ones. As all imported
7255 declarations seem to be static in all other languages, make them all CU-wide
7256 global only in Ada. */
7257
7258 static struct using_direct **
7259 using_directives (struct dwarf2_cu *cu)
7260 {
7261 if (cu->lang () == language_ada
7262 && cu->get_builder ()->outermost_context_p ())
7263 return cu->get_builder ()->get_global_using_directives ();
7264 else
7265 return cu->get_builder ()->get_local_using_directives ();
7266 }
7267
7268 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
7269 given CU. If the format is not recognized or the attribute is
7270 not present, set it to 0. */
7271
7272 static unsigned int
7273 read_decl_line (struct die_info *die, struct dwarf2_cu *cu)
7274 {
7275 struct attribute *decl_line = dwarf2_attr (die, DW_AT_decl_line, cu);
7276 if (decl_line == nullptr)
7277 return 0;
7278 if (decl_line->form_is_constant ())
7279 {
7280 LONGEST val = decl_line->constant_value (0);
7281 if (0 <= val && val <= UINT_MAX)
7282 return (unsigned int) val;
7283
7284 complaint (_("Declared line for using directive is too large"));
7285 return 0;
7286 }
7287
7288 complaint (_("Declared line for using directive is of incorrect format"));
7289 return 0;
7290 }
7291
7292 /* Read the import statement specified by the given die and record it. */
7293
7294 static void
7295 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7296 {
7297 struct objfile *objfile = cu->per_objfile->objfile;
7298 struct attribute *import_attr;
7299 struct die_info *imported_die, *child_die;
7300 struct dwarf2_cu *imported_cu;
7301 const char *imported_name;
7302 const char *imported_name_prefix;
7303 const char *canonical_name;
7304 const char *import_alias;
7305 const char *imported_declaration = NULL;
7306 const char *import_prefix;
7307 std::vector<const char *> excludes;
7308
7309 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7310 if (import_attr == NULL)
7311 {
7312 complaint (_("Tag '%s' has no DW_AT_import"),
7313 dwarf_tag_name (die->tag));
7314 return;
7315 }
7316
7317 imported_cu = cu;
7318 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7319 imported_name = dwarf2_name (imported_die, imported_cu);
7320 if (imported_name == NULL)
7321 {
7322 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7323
7324 The import in the following code:
7325 namespace A
7326 {
7327 typedef int B;
7328 }
7329
7330 int main ()
7331 {
7332 using A::B;
7333 B b;
7334 return b;
7335 }
7336
7337 ...
7338 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7339 <52> DW_AT_decl_file : 1
7340 <53> DW_AT_decl_line : 6
7341 <54> DW_AT_import : <0x75>
7342 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7343 <59> DW_AT_name : B
7344 <5b> DW_AT_decl_file : 1
7345 <5c> DW_AT_decl_line : 2
7346 <5d> DW_AT_type : <0x6e>
7347 ...
7348 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7349 <76> DW_AT_byte_size : 4
7350 <77> DW_AT_encoding : 5 (signed)
7351
7352 imports the wrong die ( 0x75 instead of 0x58 ).
7353 This case will be ignored until the gcc bug is fixed. */
7354 return;
7355 }
7356
7357 /* Figure out the local name after import. */
7358 import_alias = dwarf2_name (die, cu);
7359
7360 /* Figure out where the statement is being imported to. */
7361 import_prefix = determine_prefix (die, cu);
7362
7363 /* Figure out what the scope of the imported die is and prepend it
7364 to the name of the imported die. */
7365 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7366
7367 if (imported_die->tag != DW_TAG_namespace
7368 && imported_die->tag != DW_TAG_module)
7369 {
7370 imported_declaration = imported_name;
7371 canonical_name = imported_name_prefix;
7372 }
7373 else if (strlen (imported_name_prefix) > 0)
7374 canonical_name = obconcat (&objfile->objfile_obstack,
7375 imported_name_prefix,
7376 (cu->lang () == language_d
7377 ? "."
7378 : "::"),
7379 imported_name, (char *) NULL);
7380 else
7381 canonical_name = imported_name;
7382
7383 if (die->tag == DW_TAG_imported_module
7384 && cu->lang () == language_fortran)
7385 for (child_die = die->child; child_die && child_die->tag;
7386 child_die = child_die->sibling)
7387 {
7388 /* DWARF-4: A Fortran use statement with a “rename list” may be
7389 represented by an imported module entry with an import attribute
7390 referring to the module and owned entries corresponding to those
7391 entities that are renamed as part of being imported. */
7392
7393 if (child_die->tag != DW_TAG_imported_declaration)
7394 {
7395 complaint (_("child DW_TAG_imported_declaration expected "
7396 "- DIE at %s [in module %s]"),
7397 sect_offset_str (child_die->sect_off),
7398 objfile_name (objfile));
7399 continue;
7400 }
7401
7402 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7403 if (import_attr == NULL)
7404 {
7405 complaint (_("Tag '%s' has no DW_AT_import"),
7406 dwarf_tag_name (child_die->tag));
7407 continue;
7408 }
7409
7410 imported_cu = cu;
7411 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7412 &imported_cu);
7413 imported_name = dwarf2_name (imported_die, imported_cu);
7414 if (imported_name == NULL)
7415 {
7416 complaint (_("child DW_TAG_imported_declaration has unknown "
7417 "imported name - DIE at %s [in module %s]"),
7418 sect_offset_str (child_die->sect_off),
7419 objfile_name (objfile));
7420 continue;
7421 }
7422
7423 excludes.push_back (imported_name);
7424
7425 process_die (child_die, cu);
7426 }
7427
7428 add_using_directive (using_directives (cu),
7429 import_prefix,
7430 canonical_name,
7431 import_alias,
7432 imported_declaration,
7433 excludes,
7434 read_decl_line (die, cu),
7435 0,
7436 &objfile->objfile_obstack);
7437 }
7438
7439 /* ICC<14 does not output the required DW_AT_declaration on incomplete
7440 types, but gives them a size of zero. Starting with version 14,
7441 ICC is compatible with GCC. */
7442
7443 static bool
7444 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
7445 {
7446 if (!cu->checked_producer)
7447 check_producer (cu);
7448
7449 return cu->producer_is_icc_lt_14;
7450 }
7451
7452 /* ICC generates a DW_AT_type for C void functions. This was observed on
7453 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
7454 which says that void functions should not have a DW_AT_type. */
7455
7456 static bool
7457 producer_is_icc (struct dwarf2_cu *cu)
7458 {
7459 if (!cu->checked_producer)
7460 check_producer (cu);
7461
7462 return cu->producer_is_icc;
7463 }
7464
7465 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7466 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7467 this, it was first present in GCC release 4.3.0. */
7468
7469 static bool
7470 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7471 {
7472 if (!cu->checked_producer)
7473 check_producer (cu);
7474
7475 return cu->producer_is_gcc_lt_4_3;
7476 }
7477
7478 /* See dwarf2/read.h. */
7479 bool
7480 producer_is_clang (struct dwarf2_cu *cu)
7481 {
7482 if (!cu->checked_producer)
7483 check_producer (cu);
7484
7485 return cu->producer_is_clang;
7486 }
7487
7488 static file_and_directory &
7489 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
7490 {
7491 if (cu->per_cu->fnd != nullptr)
7492 return *cu->per_cu->fnd;
7493
7494 /* Find the filename. Do not use dwarf2_name here, since the filename
7495 is not a source language identifier. */
7496 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
7497 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
7498
7499 if (res.get_comp_dir () == nullptr
7500 && producer_is_gcc_lt_4_3 (cu)
7501 && res.get_name () != nullptr
7502 && IS_ABSOLUTE_PATH (res.get_name ()))
7503 {
7504 res.set_comp_dir (ldirname (res.get_name ()));
7505 res.set_name (make_unique_xstrdup (lbasename (res.get_name ())));
7506 }
7507
7508 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
7509 return *cu->per_cu->fnd;
7510 }
7511
7512 /* Handle DW_AT_stmt_list for a compilation unit.
7513 DIE is the DW_TAG_compile_unit die for CU.
7514 COMP_DIR is the compilation directory. LOWPC is passed to
7515 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
7516
7517 static void
7518 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7519 const file_and_directory &fnd, CORE_ADDR lowpc,
7520 bool have_code) /* ARI: editCase function */
7521 {
7522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7523 struct attribute *attr;
7524 hashval_t line_header_local_hash;
7525 void **slot;
7526 int decode_mapping;
7527
7528 gdb_assert (! cu->per_cu->is_debug_types);
7529
7530 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7531 if (attr == NULL || !attr->form_is_unsigned ())
7532 return;
7533
7534 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7535
7536 /* The line header hash table is only created if needed (it exists to
7537 prevent redundant reading of the line table for partial_units).
7538 If we're given a partial_unit, we'll need it. If we're given a
7539 compile_unit, then use the line header hash table if it's already
7540 created, but don't create one just yet. */
7541
7542 if (per_objfile->line_header_hash == NULL
7543 && die->tag == DW_TAG_partial_unit)
7544 {
7545 per_objfile->line_header_hash
7546 .reset (htab_create_alloc (127, line_header_hash_voidp,
7547 line_header_eq_voidp,
7548 htab_delete_entry<line_header>,
7549 xcalloc, xfree));
7550 }
7551
7552 line_header line_header_local (line_offset, cu->per_cu->is_dwz);
7553 line_header_local_hash = line_header_hash (&line_header_local);
7554 if (per_objfile->line_header_hash != NULL)
7555 {
7556 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7557 &line_header_local,
7558 line_header_local_hash, NO_INSERT);
7559
7560 /* For DW_TAG_compile_unit we need info like symtab::linetable which
7561 is not present in *SLOT (since if there is something in *SLOT then
7562 it will be for a partial_unit). */
7563 if (die->tag == DW_TAG_partial_unit && slot != NULL)
7564 {
7565 gdb_assert (*slot != NULL);
7566 cu->line_header = (struct line_header *) *slot;
7567 return;
7568 }
7569 }
7570
7571 /* dwarf_decode_line_header does not yet provide sufficient information.
7572 We always have to call also dwarf_decode_lines for it. */
7573 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
7574 fnd.get_comp_dir ());
7575 if (lh == NULL)
7576 return;
7577
7578 cu->line_header = lh.release ();
7579 cu->line_header_die_owner = die;
7580
7581 if (per_objfile->line_header_hash == NULL)
7582 slot = NULL;
7583 else
7584 {
7585 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7586 &line_header_local,
7587 line_header_local_hash, INSERT);
7588 gdb_assert (slot != NULL);
7589 }
7590 if (slot != NULL && *slot == NULL)
7591 {
7592 /* This newly decoded line number information unit will be owned
7593 by line_header_hash hash table. */
7594 *slot = cu->line_header;
7595 cu->line_header_die_owner = NULL;
7596 }
7597 else
7598 {
7599 /* We cannot free any current entry in (*slot) as that struct line_header
7600 may be already used by multiple CUs. Create only temporary decoded
7601 line_header for this CU - it may happen at most once for each line
7602 number information unit. And if we're not using line_header_hash
7603 then this is what we want as well. */
7604 gdb_assert (die->tag != DW_TAG_partial_unit);
7605 }
7606 decode_mapping = (die->tag != DW_TAG_partial_unit);
7607 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
7608 then there won't be any interesting code in the CU, but a check later on
7609 (in lnp_state_machine::check_line_address) will fail to properly exclude
7610 an entry that was removed via --gc-sections. */
7611 if (have_code)
7612 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
7613 }
7614
7615 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7616
7617 static void
7618 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7619 {
7620 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7621 struct objfile *objfile = per_objfile->objfile;
7622 struct gdbarch *gdbarch = objfile->arch ();
7623 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7624 CORE_ADDR highpc = ((CORE_ADDR) 0);
7625 struct attribute *attr;
7626 struct die_info *child_die;
7627 CORE_ADDR baseaddr;
7628
7629 prepare_one_comp_unit (cu, die, cu->lang ());
7630 baseaddr = objfile->text_section_offset ();
7631
7632 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7633
7634 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7635 from finish_block. */
7636 if (lowpc == ((CORE_ADDR) -1))
7637 lowpc = highpc;
7638 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
7639
7640 file_and_directory &fnd = find_file_and_directory (die, cu);
7641
7642 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
7643 lowpc);
7644
7645 gdb_assert (per_objfile->sym_cu == nullptr);
7646 scoped_restore restore_sym_cu
7647 = make_scoped_restore (&per_objfile->sym_cu, cu);
7648
7649 /* Decode line number information if present. We do this before
7650 processing child DIEs, so that the line header table is available
7651 for DW_AT_decl_file. */
7652 handle_DW_AT_stmt_list (die, cu, fnd, lowpc, lowpc != highpc);
7653
7654 /* Process all dies in compilation unit. */
7655 if (die->child != NULL)
7656 {
7657 child_die = die->child;
7658 while (child_die && child_die->tag)
7659 {
7660 process_die (child_die, cu);
7661 child_die = child_die->sibling;
7662 }
7663 }
7664 per_objfile->sym_cu = nullptr;
7665
7666 /* Decode macro information, if present. Dwarf 2 macro information
7667 refers to information in the line number info statement program
7668 header, so we can only read it if we've read the header
7669 successfully. */
7670 attr = dwarf2_attr (die, DW_AT_macros, cu);
7671 if (attr == NULL)
7672 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7673 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7674 {
7675 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7676 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
7677
7678 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
7679 }
7680 else
7681 {
7682 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7683 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7684 {
7685 unsigned int macro_offset = attr->as_unsigned ();
7686
7687 dwarf_decode_macros (cu, macro_offset, 0);
7688 }
7689 }
7690 }
7691
7692 void
7693 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
7694 {
7695 struct type_unit_group *tu_group;
7696 int first_time;
7697 struct attribute *attr;
7698 unsigned int i;
7699 struct signatured_type *sig_type;
7700
7701 gdb_assert (per_cu->is_debug_types);
7702 sig_type = (struct signatured_type *) per_cu;
7703
7704 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
7705
7706 /* If we're using .gdb_index (includes -readnow) then
7707 per_cu->type_unit_group may not have been set up yet. */
7708 if (sig_type->type_unit_group == NULL)
7709 sig_type->type_unit_group = get_type_unit_group (this, attr);
7710 tu_group = sig_type->type_unit_group;
7711
7712 /* If we've already processed this stmt_list there's no real need to
7713 do it again, we could fake it and just recreate the part we need
7714 (file name,index -> symtab mapping). If data shows this optimization
7715 is useful we can do it then. */
7716 type_unit_group_unshareable *tug_unshare
7717 = per_objfile->get_type_unit_group_unshareable (tu_group);
7718 first_time = tug_unshare->compunit_symtab == NULL;
7719
7720 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7721 debug info. */
7722 line_header_up lh;
7723 if (attr != NULL && attr->form_is_unsigned ())
7724 {
7725 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7726 lh = dwarf_decode_line_header (line_offset, this, nullptr);
7727 }
7728 if (lh == NULL)
7729 {
7730 if (first_time)
7731 start_compunit_symtab ("", NULL, 0);
7732 else
7733 {
7734 gdb_assert (tug_unshare->symtabs == NULL);
7735 gdb_assert (m_builder == nullptr);
7736 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7737 m_builder.reset (new struct buildsym_compunit
7738 (cust->objfile (), "",
7739 cust->dirname (),
7740 cust->language (),
7741 0, cust));
7742 list_in_scope = get_builder ()->get_file_symbols ();
7743 }
7744 return;
7745 }
7746
7747 line_header = lh.release ();
7748 line_header_die_owner = die;
7749
7750 if (first_time)
7751 {
7752 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
7753
7754 /* Note: We don't assign tu_group->compunit_symtab yet because we're
7755 still initializing it, and our caller (a few levels up)
7756 process_full_type_unit still needs to know if this is the first
7757 time. */
7758
7759 tug_unshare->symtabs
7760 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
7761 struct symtab *, line_header->file_names_size ());
7762
7763 auto &file_names = line_header->file_names ();
7764 for (i = 0; i < file_names.size (); ++i)
7765 {
7766 file_entry &fe = file_names[i];
7767 dwarf2_start_subfile (this, fe, *line_header);
7768 buildsym_compunit *b = get_builder ();
7769 subfile *sf = b->get_current_subfile ();
7770
7771 if (sf->symtab == nullptr)
7772 {
7773 /* NOTE: start_subfile will recognize when it's been
7774 passed a file it has already seen. So we can't
7775 assume there's a simple mapping from
7776 cu->line_header->file_names to subfiles, plus
7777 cu->line_header->file_names may contain dups. */
7778 const char *name = sf->name.c_str ();
7779 const char *name_for_id = sf->name_for_id.c_str ();
7780 sf->symtab = allocate_symtab (cust, name, name_for_id);
7781 }
7782
7783 fe.symtab = b->get_current_subfile ()->symtab;
7784 tug_unshare->symtabs[i] = fe.symtab;
7785 }
7786 }
7787 else
7788 {
7789 gdb_assert (m_builder == nullptr);
7790 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7791 m_builder.reset (new struct buildsym_compunit
7792 (cust->objfile (), "",
7793 cust->dirname (),
7794 cust->language (),
7795 0, cust));
7796 list_in_scope = get_builder ()->get_file_symbols ();
7797
7798 auto &file_names = line_header->file_names ();
7799 for (i = 0; i < file_names.size (); ++i)
7800 {
7801 file_entry &fe = file_names[i];
7802 fe.symtab = tug_unshare->symtabs[i];
7803 }
7804 }
7805
7806 /* The main symtab is allocated last. Type units don't have DW_AT_name
7807 so they don't have a "real" (so to speak) symtab anyway.
7808 There is later code that will assign the main symtab to all symbols
7809 that don't have one. We need to handle the case of a symbol with a
7810 missing symtab (DW_AT_decl_file) anyway. */
7811 }
7812
7813 /* Process DW_TAG_type_unit.
7814 For TUs we want to skip the first top level sibling if it's not the
7815 actual type being defined by this TU. In this case the first top
7816 level sibling is there to provide context only. */
7817
7818 static void
7819 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7820 {
7821 struct die_info *child_die;
7822
7823 prepare_one_comp_unit (cu, die, language_minimal);
7824
7825 /* Initialize (or reinitialize) the machinery for building symtabs.
7826 We do this before processing child DIEs, so that the line header table
7827 is available for DW_AT_decl_file. */
7828 cu->setup_type_unit_groups (die);
7829
7830 if (die->child != NULL)
7831 {
7832 child_die = die->child;
7833 while (child_die && child_die->tag)
7834 {
7835 process_die (child_die, cu);
7836 child_die = child_die->sibling;
7837 }
7838 }
7839 }
7840 \f
7841 /* DWO/DWP files.
7842
7843 http://gcc.gnu.org/wiki/DebugFission
7844 http://gcc.gnu.org/wiki/DebugFissionDWP
7845
7846 To simplify handling of both DWO files ("object" files with the DWARF info)
7847 and DWP files (a file with the DWOs packaged up into one file), we treat
7848 DWP files as having a collection of virtual DWO files. */
7849
7850 static hashval_t
7851 hash_dwo_file (const void *item)
7852 {
7853 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
7854 hashval_t hash;
7855
7856 hash = htab_hash_string (dwo_file->dwo_name);
7857 if (dwo_file->comp_dir != NULL)
7858 hash += htab_hash_string (dwo_file->comp_dir);
7859 return hash;
7860 }
7861
7862 static int
7863 eq_dwo_file (const void *item_lhs, const void *item_rhs)
7864 {
7865 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
7866 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
7867
7868 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
7869 return 0;
7870 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
7871 return lhs->comp_dir == rhs->comp_dir;
7872 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
7873 }
7874
7875 /* Allocate a hash table for DWO files. */
7876
7877 static htab_up
7878 allocate_dwo_file_hash_table ()
7879 {
7880 return htab_up (htab_create_alloc (41,
7881 hash_dwo_file,
7882 eq_dwo_file,
7883 htab_delete_entry<dwo_file>,
7884 xcalloc, xfree));
7885 }
7886
7887 /* Lookup DWO file DWO_NAME. */
7888
7889 static void **
7890 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
7891 const char *dwo_name,
7892 const char *comp_dir)
7893 {
7894 struct dwo_file find_entry;
7895 void **slot;
7896
7897 if (per_objfile->per_bfd->dwo_files == NULL)
7898 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
7899
7900 find_entry.dwo_name = dwo_name;
7901 find_entry.comp_dir = comp_dir;
7902 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
7903 INSERT);
7904
7905 return slot;
7906 }
7907
7908 static hashval_t
7909 hash_dwo_unit (const void *item)
7910 {
7911 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
7912
7913 /* This drops the top 32 bits of the id, but is ok for a hash. */
7914 return dwo_unit->signature;
7915 }
7916
7917 static int
7918 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7919 {
7920 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
7921 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
7922
7923 /* The signature is assumed to be unique within the DWO file.
7924 So while object file CU dwo_id's always have the value zero,
7925 that's OK, assuming each object file DWO file has only one CU,
7926 and that's the rule for now. */
7927 return lhs->signature == rhs->signature;
7928 }
7929
7930 /* Allocate a hash table for DWO CUs,TUs.
7931 There is one of these tables for each of CUs,TUs for each DWO file. */
7932
7933 static htab_up
7934 allocate_dwo_unit_table ()
7935 {
7936 /* Start out with a pretty small number.
7937 Generally DWO files contain only one CU and maybe some TUs. */
7938 return htab_up (htab_create_alloc (3,
7939 hash_dwo_unit,
7940 eq_dwo_unit,
7941 NULL, xcalloc, xfree));
7942 }
7943
7944 /* die_reader_func for create_dwo_cu. */
7945
7946 static void
7947 create_dwo_cu_reader (const struct die_reader_specs *reader,
7948 const gdb_byte *info_ptr,
7949 struct die_info *comp_unit_die,
7950 struct dwo_file *dwo_file,
7951 struct dwo_unit *dwo_unit)
7952 {
7953 struct dwarf2_cu *cu = reader->cu;
7954 sect_offset sect_off = cu->per_cu->sect_off;
7955 struct dwarf2_section_info *section = cu->per_cu->section;
7956
7957 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7958 if (!signature.has_value ())
7959 {
7960 complaint (_("Dwarf Error: debug entry at offset %s is missing"
7961 " its dwo_id [in module %s]"),
7962 sect_offset_str (sect_off), dwo_file->dwo_name);
7963 return;
7964 }
7965
7966 dwo_unit->dwo_file = dwo_file;
7967 dwo_unit->signature = *signature;
7968 dwo_unit->section = section;
7969 dwo_unit->sect_off = sect_off;
7970 dwo_unit->length = cu->per_cu->length ();
7971
7972 dwarf_read_debug_printf (" offset %s, dwo_id %s",
7973 sect_offset_str (sect_off),
7974 hex_string (dwo_unit->signature));
7975 }
7976
7977 /* Create the dwo_units for the CUs in a DWO_FILE.
7978 Note: This function processes DWO files only, not DWP files. */
7979
7980 static void
7981 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
7982 dwarf2_cu *cu, struct dwo_file &dwo_file,
7983 dwarf2_section_info &section, htab_up &cus_htab)
7984 {
7985 struct objfile *objfile = per_objfile->objfile;
7986 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7987 const gdb_byte *info_ptr, *end_ptr;
7988
7989 section.read (objfile);
7990 info_ptr = section.buffer;
7991
7992 if (info_ptr == NULL)
7993 return;
7994
7995 dwarf_read_debug_printf ("Reading %s for %s:",
7996 section.get_name (),
7997 section.get_file_name ());
7998
7999 end_ptr = info_ptr + section.size;
8000 while (info_ptr < end_ptr)
8001 {
8002 struct dwarf2_per_cu_data per_cu;
8003 struct dwo_unit read_unit {};
8004 struct dwo_unit *dwo_unit;
8005 void **slot;
8006 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
8007
8008 per_cu.per_bfd = per_bfd;
8009 per_cu.is_debug_types = 0;
8010 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
8011 per_cu.section = &section;
8012
8013 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
8014 if (!reader.dummy_p)
8015 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8016 &dwo_file, &read_unit);
8017 info_ptr += per_cu.length ();
8018
8019 // If the unit could not be parsed, skip it.
8020 if (read_unit.dwo_file == NULL)
8021 continue;
8022
8023 if (cus_htab == NULL)
8024 cus_htab = allocate_dwo_unit_table ();
8025
8026 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
8027 struct dwo_unit);
8028 *dwo_unit = read_unit;
8029 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
8030 gdb_assert (slot != NULL);
8031 if (*slot != NULL)
8032 {
8033 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
8034 sect_offset dup_sect_off = dup_cu->sect_off;
8035
8036 complaint (_("debug cu entry at offset %s is duplicate to"
8037 " the entry at offset %s, signature %s"),
8038 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
8039 hex_string (dwo_unit->signature));
8040 }
8041 *slot = (void *)dwo_unit;
8042 }
8043 }
8044
8045 /* DWP file .debug_{cu,tu}_index section format:
8046 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8047 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
8048
8049 DWP Versions 1 & 2 are older, pre-standard format versions. The first
8050 officially standard DWP format was published with DWARF v5 and is called
8051 Version 5. There are no versions 3 or 4.
8052
8053 DWP Version 1:
8054
8055 Both index sections have the same format, and serve to map a 64-bit
8056 signature to a set of section numbers. Each section begins with a header,
8057 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8058 indexes, and a pool of 32-bit section numbers. The index sections will be
8059 aligned at 8-byte boundaries in the file.
8060
8061 The index section header consists of:
8062
8063 V, 32 bit version number
8064 -, 32 bits unused
8065 N, 32 bit number of compilation units or type units in the index
8066 M, 32 bit number of slots in the hash table
8067
8068 Numbers are recorded using the byte order of the application binary.
8069
8070 The hash table begins at offset 16 in the section, and consists of an array
8071 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8072 order of the application binary). Unused slots in the hash table are 0.
8073 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8074
8075 The parallel table begins immediately after the hash table
8076 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8077 array of 32-bit indexes (using the byte order of the application binary),
8078 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8079 table contains a 32-bit index into the pool of section numbers. For unused
8080 hash table slots, the corresponding entry in the parallel table will be 0.
8081
8082 The pool of section numbers begins immediately following the hash table
8083 (at offset 16 + 12 * M from the beginning of the section). The pool of
8084 section numbers consists of an array of 32-bit words (using the byte order
8085 of the application binary). Each item in the array is indexed starting
8086 from 0. The hash table entry provides the index of the first section
8087 number in the set. Additional section numbers in the set follow, and the
8088 set is terminated by a 0 entry (section number 0 is not used in ELF).
8089
8090 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8091 section must be the first entry in the set, and the .debug_abbrev.dwo must
8092 be the second entry. Other members of the set may follow in any order.
8093
8094 ---
8095
8096 DWP Versions 2 and 5:
8097
8098 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
8099 and the entries in the index tables are now offsets into these sections.
8100 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
8101 section.
8102
8103 Index Section Contents:
8104 Header
8105 Hash Table of Signatures dwp_hash_table.hash_table
8106 Parallel Table of Indices dwp_hash_table.unit_table
8107 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
8108 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
8109
8110 The index section header consists of:
8111
8112 V, 32 bit version number
8113 L, 32 bit number of columns in the table of section offsets
8114 N, 32 bit number of compilation units or type units in the index
8115 M, 32 bit number of slots in the hash table
8116
8117 Numbers are recorded using the byte order of the application binary.
8118
8119 The hash table has the same format as version 1.
8120 The parallel table of indices has the same format as version 1,
8121 except that the entries are origin-1 indices into the table of sections
8122 offsets and the table of section sizes.
8123
8124 The table of offsets begins immediately following the parallel table
8125 (at offset 16 + 12 * M from the beginning of the section). The table is
8126 a two-dimensional array of 32-bit words (using the byte order of the
8127 application binary), with L columns and N+1 rows, in row-major order.
8128 Each row in the array is indexed starting from 0. The first row provides
8129 a key to the remaining rows: each column in this row provides an identifier
8130 for a debug section, and the offsets in the same column of subsequent rows
8131 refer to that section. The section identifiers for Version 2 are:
8132
8133 DW_SECT_INFO 1 .debug_info.dwo
8134 DW_SECT_TYPES 2 .debug_types.dwo
8135 DW_SECT_ABBREV 3 .debug_abbrev.dwo
8136 DW_SECT_LINE 4 .debug_line.dwo
8137 DW_SECT_LOC 5 .debug_loc.dwo
8138 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
8139 DW_SECT_MACINFO 7 .debug_macinfo.dwo
8140 DW_SECT_MACRO 8 .debug_macro.dwo
8141
8142 The section identifiers for Version 5 are:
8143
8144 DW_SECT_INFO_V5 1 .debug_info.dwo
8145 DW_SECT_RESERVED_V5 2 --
8146 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
8147 DW_SECT_LINE_V5 4 .debug_line.dwo
8148 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
8149 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
8150 DW_SECT_MACRO_V5 7 .debug_macro.dwo
8151 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
8152
8153 The offsets provided by the CU and TU index sections are the base offsets
8154 for the contributions made by each CU or TU to the corresponding section
8155 in the package file. Each CU and TU header contains an abbrev_offset
8156 field, used to find the abbreviations table for that CU or TU within the
8157 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
8158 be interpreted as relative to the base offset given in the index section.
8159 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
8160 should be interpreted as relative to the base offset for .debug_line.dwo,
8161 and offsets into other debug sections obtained from DWARF attributes should
8162 also be interpreted as relative to the corresponding base offset.
8163
8164 The table of sizes begins immediately following the table of offsets.
8165 Like the table of offsets, it is a two-dimensional array of 32-bit words,
8166 with L columns and N rows, in row-major order. Each row in the array is
8167 indexed starting from 1 (row 0 is shared by the two tables).
8168
8169 ---
8170
8171 Hash table lookup is handled the same in version 1 and 2:
8172
8173 We assume that N and M will not exceed 2^32 - 1.
8174 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8175
8176 Given a 64-bit compilation unit signature or a type signature S, an entry
8177 in the hash table is located as follows:
8178
8179 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8180 the low-order k bits all set to 1.
8181
8182 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8183
8184 3) If the hash table entry at index H matches the signature, use that
8185 entry. If the hash table entry at index H is unused (all zeroes),
8186 terminate the search: the signature is not present in the table.
8187
8188 4) Let H = (H + H') modulo M. Repeat at Step 3.
8189
8190 Because M > N and H' and M are relatively prime, the search is guaranteed
8191 to stop at an unused slot or find the match. */
8192
8193 /* Create a hash table to map DWO IDs to their CU/TU entry in
8194 .debug_{info,types}.dwo in DWP_FILE.
8195 Returns NULL if there isn't one.
8196 Note: This function processes DWP files only, not DWO files. */
8197
8198 static struct dwp_hash_table *
8199 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
8200 struct dwp_file *dwp_file, int is_debug_types)
8201 {
8202 struct objfile *objfile = per_objfile->objfile;
8203 bfd *dbfd = dwp_file->dbfd.get ();
8204 const gdb_byte *index_ptr, *index_end;
8205 struct dwarf2_section_info *index;
8206 uint32_t version, nr_columns, nr_units, nr_slots;
8207 struct dwp_hash_table *htab;
8208
8209 if (is_debug_types)
8210 index = &dwp_file->sections.tu_index;
8211 else
8212 index = &dwp_file->sections.cu_index;
8213
8214 if (index->empty ())
8215 return NULL;
8216 index->read (objfile);
8217
8218 index_ptr = index->buffer;
8219 index_end = index_ptr + index->size;
8220
8221 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
8222 For now it's safe to just read 4 bytes (particularly as it's difficult to
8223 tell if you're dealing with Version 5 before you've read the version). */
8224 version = read_4_bytes (dbfd, index_ptr);
8225 index_ptr += 4;
8226 if (version == 2 || version == 5)
8227 nr_columns = read_4_bytes (dbfd, index_ptr);
8228 else
8229 nr_columns = 0;
8230 index_ptr += 4;
8231 nr_units = read_4_bytes (dbfd, index_ptr);
8232 index_ptr += 4;
8233 nr_slots = read_4_bytes (dbfd, index_ptr);
8234 index_ptr += 4;
8235
8236 if (version != 1 && version != 2 && version != 5)
8237 {
8238 error (_("Dwarf Error: unsupported DWP file version (%s)"
8239 " [in module %s]"),
8240 pulongest (version), dwp_file->name);
8241 }
8242 if (nr_slots != (nr_slots & -nr_slots))
8243 {
8244 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
8245 " is not power of 2 [in module %s]"),
8246 pulongest (nr_slots), dwp_file->name);
8247 }
8248
8249 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
8250 htab->version = version;
8251 htab->nr_columns = nr_columns;
8252 htab->nr_units = nr_units;
8253 htab->nr_slots = nr_slots;
8254 htab->hash_table = index_ptr;
8255 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8256
8257 /* Exit early if the table is empty. */
8258 if (nr_slots == 0 || nr_units == 0
8259 || (version == 2 && nr_columns == 0)
8260 || (version == 5 && nr_columns == 0))
8261 {
8262 /* All must be zero. */
8263 if (nr_slots != 0 || nr_units != 0
8264 || (version == 2 && nr_columns != 0)
8265 || (version == 5 && nr_columns != 0))
8266 {
8267 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
8268 " all zero [in modules %s]"),
8269 dwp_file->name);
8270 }
8271 return htab;
8272 }
8273
8274 if (version == 1)
8275 {
8276 htab->section_pool.v1.indices =
8277 htab->unit_table + sizeof (uint32_t) * nr_slots;
8278 /* It's harder to decide whether the section is too small in v1.
8279 V1 is deprecated anyway so we punt. */
8280 }
8281 else if (version == 2)
8282 {
8283 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8284 int *ids = htab->section_pool.v2.section_ids;
8285 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
8286 /* Reverse map for error checking. */
8287 int ids_seen[DW_SECT_MAX + 1];
8288 int i;
8289
8290 if (nr_columns < 2)
8291 {
8292 error (_("Dwarf Error: bad DWP hash table, too few columns"
8293 " in section table [in module %s]"),
8294 dwp_file->name);
8295 }
8296 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
8297 {
8298 error (_("Dwarf Error: bad DWP hash table, too many columns"
8299 " in section table [in module %s]"),
8300 dwp_file->name);
8301 }
8302 memset (ids, 255, sizeof_ids);
8303 memset (ids_seen, 255, sizeof (ids_seen));
8304 for (i = 0; i < nr_columns; ++i)
8305 {
8306 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8307
8308 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
8309 {
8310 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8311 " in section table [in module %s]"),
8312 id, dwp_file->name);
8313 }
8314 if (ids_seen[id] != -1)
8315 {
8316 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8317 " id %d in section table [in module %s]"),
8318 id, dwp_file->name);
8319 }
8320 ids_seen[id] = i;
8321 ids[i] = id;
8322 }
8323 /* Must have exactly one info or types section. */
8324 if (((ids_seen[DW_SECT_INFO] != -1)
8325 + (ids_seen[DW_SECT_TYPES] != -1))
8326 != 1)
8327 {
8328 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8329 " DWO info/types section [in module %s]"),
8330 dwp_file->name);
8331 }
8332 /* Must have an abbrev section. */
8333 if (ids_seen[DW_SECT_ABBREV] == -1)
8334 {
8335 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8336 " section [in module %s]"),
8337 dwp_file->name);
8338 }
8339 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8340 htab->section_pool.v2.sizes =
8341 htab->section_pool.v2.offsets + (sizeof (uint32_t)
8342 * nr_units * nr_columns);
8343 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
8344 * nr_units * nr_columns))
8345 > index_end)
8346 {
8347 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8348 " [in module %s]"),
8349 dwp_file->name);
8350 }
8351 }
8352 else /* version == 5 */
8353 {
8354 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8355 int *ids = htab->section_pool.v5.section_ids;
8356 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
8357 /* Reverse map for error checking. */
8358 int ids_seen[DW_SECT_MAX_V5 + 1];
8359
8360 if (nr_columns < 2)
8361 {
8362 error (_("Dwarf Error: bad DWP hash table, too few columns"
8363 " in section table [in module %s]"),
8364 dwp_file->name);
8365 }
8366 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
8367 {
8368 error (_("Dwarf Error: bad DWP hash table, too many columns"
8369 " in section table [in module %s]"),
8370 dwp_file->name);
8371 }
8372 memset (ids, 255, sizeof_ids);
8373 memset (ids_seen, 255, sizeof (ids_seen));
8374 for (int i = 0; i < nr_columns; ++i)
8375 {
8376 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8377
8378 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
8379 {
8380 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8381 " in section table [in module %s]"),
8382 id, dwp_file->name);
8383 }
8384 if (ids_seen[id] != -1)
8385 {
8386 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8387 " id %d in section table [in module %s]"),
8388 id, dwp_file->name);
8389 }
8390 ids_seen[id] = i;
8391 ids[i] = id;
8392 }
8393 /* Must have seen an info section. */
8394 if (ids_seen[DW_SECT_INFO_V5] == -1)
8395 {
8396 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8397 " DWO info/types section [in module %s]"),
8398 dwp_file->name);
8399 }
8400 /* Must have an abbrev section. */
8401 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
8402 {
8403 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8404 " section [in module %s]"),
8405 dwp_file->name);
8406 }
8407 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8408 htab->section_pool.v5.sizes
8409 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
8410 * nr_units * nr_columns);
8411 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
8412 * nr_units * nr_columns))
8413 > index_end)
8414 {
8415 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8416 " [in module %s]"),
8417 dwp_file->name);
8418 }
8419 }
8420
8421 return htab;
8422 }
8423
8424 /* Update SECTIONS with the data from SECTP.
8425
8426 This function is like the other "locate" section routines, but in
8427 this context the sections to read comes from the DWP V1 hash table,
8428 not the full ELF section table.
8429
8430 The result is non-zero for success, or zero if an error was found. */
8431
8432 static int
8433 locate_v1_virtual_dwo_sections (asection *sectp,
8434 struct virtual_v1_dwo_sections *sections)
8435 {
8436 const struct dwop_section_names *names = &dwop_section_names;
8437
8438 if (names->abbrev_dwo.matches (sectp->name))
8439 {
8440 /* There can be only one. */
8441 if (sections->abbrev.s.section != NULL)
8442 return 0;
8443 sections->abbrev.s.section = sectp;
8444 sections->abbrev.size = bfd_section_size (sectp);
8445 }
8446 else if (names->info_dwo.matches (sectp->name)
8447 || names->types_dwo.matches (sectp->name))
8448 {
8449 /* There can be only one. */
8450 if (sections->info_or_types.s.section != NULL)
8451 return 0;
8452 sections->info_or_types.s.section = sectp;
8453 sections->info_or_types.size = bfd_section_size (sectp);
8454 }
8455 else if (names->line_dwo.matches (sectp->name))
8456 {
8457 /* There can be only one. */
8458 if (sections->line.s.section != NULL)
8459 return 0;
8460 sections->line.s.section = sectp;
8461 sections->line.size = bfd_section_size (sectp);
8462 }
8463 else if (names->loc_dwo.matches (sectp->name))
8464 {
8465 /* There can be only one. */
8466 if (sections->loc.s.section != NULL)
8467 return 0;
8468 sections->loc.s.section = sectp;
8469 sections->loc.size = bfd_section_size (sectp);
8470 }
8471 else if (names->macinfo_dwo.matches (sectp->name))
8472 {
8473 /* There can be only one. */
8474 if (sections->macinfo.s.section != NULL)
8475 return 0;
8476 sections->macinfo.s.section = sectp;
8477 sections->macinfo.size = bfd_section_size (sectp);
8478 }
8479 else if (names->macro_dwo.matches (sectp->name))
8480 {
8481 /* There can be only one. */
8482 if (sections->macro.s.section != NULL)
8483 return 0;
8484 sections->macro.s.section = sectp;
8485 sections->macro.size = bfd_section_size (sectp);
8486 }
8487 else if (names->str_offsets_dwo.matches (sectp->name))
8488 {
8489 /* There can be only one. */
8490 if (sections->str_offsets.s.section != NULL)
8491 return 0;
8492 sections->str_offsets.s.section = sectp;
8493 sections->str_offsets.size = bfd_section_size (sectp);
8494 }
8495 else
8496 {
8497 /* No other kind of section is valid. */
8498 return 0;
8499 }
8500
8501 return 1;
8502 }
8503
8504 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8505 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8506 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8507 This is for DWP version 1 files. */
8508
8509 static struct dwo_unit *
8510 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
8511 struct dwp_file *dwp_file,
8512 uint32_t unit_index,
8513 const char *comp_dir,
8514 ULONGEST signature, int is_debug_types)
8515 {
8516 const struct dwp_hash_table *dwp_htab =
8517 is_debug_types ? dwp_file->tus : dwp_file->cus;
8518 bfd *dbfd = dwp_file->dbfd.get ();
8519 const char *kind = is_debug_types ? "TU" : "CU";
8520 struct dwo_file *dwo_file;
8521 struct dwo_unit *dwo_unit;
8522 struct virtual_v1_dwo_sections sections;
8523 void **dwo_file_slot;
8524 int i;
8525
8526 gdb_assert (dwp_file->version == 1);
8527
8528 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
8529 kind, pulongest (unit_index), hex_string (signature),
8530 dwp_file->name);
8531
8532 /* Fetch the sections of this DWO unit.
8533 Put a limit on the number of sections we look for so that bad data
8534 doesn't cause us to loop forever. */
8535
8536 #define MAX_NR_V1_DWO_SECTIONS \
8537 (1 /* .debug_info or .debug_types */ \
8538 + 1 /* .debug_abbrev */ \
8539 + 1 /* .debug_line */ \
8540 + 1 /* .debug_loc */ \
8541 + 1 /* .debug_str_offsets */ \
8542 + 1 /* .debug_macro or .debug_macinfo */ \
8543 + 1 /* trailing zero */)
8544
8545 memset (&sections, 0, sizeof (sections));
8546
8547 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
8548 {
8549 asection *sectp;
8550 uint32_t section_nr =
8551 read_4_bytes (dbfd,
8552 dwp_htab->section_pool.v1.indices
8553 + (unit_index + i) * sizeof (uint32_t));
8554
8555 if (section_nr == 0)
8556 break;
8557 if (section_nr >= dwp_file->num_sections)
8558 {
8559 error (_("Dwarf Error: bad DWP hash table, section number too large"
8560 " [in module %s]"),
8561 dwp_file->name);
8562 }
8563
8564 sectp = dwp_file->elf_sections[section_nr];
8565 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
8566 {
8567 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8568 " [in module %s]"),
8569 dwp_file->name);
8570 }
8571 }
8572
8573 if (i < 2
8574 || sections.info_or_types.empty ()
8575 || sections.abbrev.empty ())
8576 {
8577 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8578 " [in module %s]"),
8579 dwp_file->name);
8580 }
8581 if (i == MAX_NR_V1_DWO_SECTIONS)
8582 {
8583 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8584 " [in module %s]"),
8585 dwp_file->name);
8586 }
8587
8588 /* It's easier for the rest of the code if we fake a struct dwo_file and
8589 have dwo_unit "live" in that. At least for now.
8590
8591 The DWP file can be made up of a random collection of CUs and TUs.
8592 However, for each CU + set of TUs that came from the same original DWO
8593 file, we can combine them back into a virtual DWO file to save space
8594 (fewer struct dwo_file objects to allocate). Remember that for really
8595 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8596
8597 std::string virtual_dwo_name =
8598 string_printf ("virtual-dwo/%d-%d-%d-%d",
8599 sections.abbrev.get_id (),
8600 sections.line.get_id (),
8601 sections.loc.get_id (),
8602 sections.str_offsets.get_id ());
8603 /* Can we use an existing virtual DWO file? */
8604 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8605 comp_dir);
8606 /* Create one if necessary. */
8607 if (*dwo_file_slot == NULL)
8608 {
8609 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8610 virtual_dwo_name.c_str ());
8611
8612 dwo_file = new struct dwo_file;
8613 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
8614 dwo_file->comp_dir = comp_dir;
8615 dwo_file->sections.abbrev = sections.abbrev;
8616 dwo_file->sections.line = sections.line;
8617 dwo_file->sections.loc = sections.loc;
8618 dwo_file->sections.macinfo = sections.macinfo;
8619 dwo_file->sections.macro = sections.macro;
8620 dwo_file->sections.str_offsets = sections.str_offsets;
8621 /* The "str" section is global to the entire DWP file. */
8622 dwo_file->sections.str = dwp_file->sections.str;
8623 /* The info or types section is assigned below to dwo_unit,
8624 there's no need to record it in dwo_file.
8625 Also, we can't simply record type sections in dwo_file because
8626 we record a pointer into the vector in dwo_unit. As we collect more
8627 types we'll grow the vector and eventually have to reallocate space
8628 for it, invalidating all copies of pointers into the previous
8629 contents. */
8630 *dwo_file_slot = dwo_file;
8631 }
8632 else
8633 {
8634 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8635 virtual_dwo_name.c_str ());
8636
8637 dwo_file = (struct dwo_file *) *dwo_file_slot;
8638 }
8639
8640 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8641 dwo_unit->dwo_file = dwo_file;
8642 dwo_unit->signature = signature;
8643 dwo_unit->section =
8644 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8645 *dwo_unit->section = sections.info_or_types;
8646 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8647
8648 return dwo_unit;
8649 }
8650
8651 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
8652 simplify them. Given a pointer to the containing section SECTION, and
8653 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
8654 virtual section of just that piece. */
8655
8656 static struct dwarf2_section_info
8657 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
8658 struct dwarf2_section_info *section,
8659 bfd_size_type offset, bfd_size_type size)
8660 {
8661 struct dwarf2_section_info result;
8662 asection *sectp;
8663
8664 gdb_assert (section != NULL);
8665 gdb_assert (!section->is_virtual);
8666
8667 memset (&result, 0, sizeof (result));
8668 result.s.containing_section = section;
8669 result.is_virtual = true;
8670
8671 if (size == 0)
8672 return result;
8673
8674 sectp = section->get_bfd_section ();
8675
8676 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
8677 bounds of the real section. This is a pretty-rare event, so just
8678 flag an error (easier) instead of a warning and trying to cope. */
8679 if (sectp == NULL
8680 || offset + size > bfd_section_size (sectp))
8681 {
8682 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
8683 " in section %s [in module %s]"),
8684 sectp ? bfd_section_name (sectp) : "<unknown>",
8685 objfile_name (per_objfile->objfile));
8686 }
8687
8688 result.virtual_offset = offset;
8689 result.size = size;
8690 return result;
8691 }
8692
8693 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8694 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8695 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8696 This is for DWP version 2 files. */
8697
8698 static struct dwo_unit *
8699 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
8700 struct dwp_file *dwp_file,
8701 uint32_t unit_index,
8702 const char *comp_dir,
8703 ULONGEST signature, int is_debug_types)
8704 {
8705 const struct dwp_hash_table *dwp_htab =
8706 is_debug_types ? dwp_file->tus : dwp_file->cus;
8707 bfd *dbfd = dwp_file->dbfd.get ();
8708 const char *kind = is_debug_types ? "TU" : "CU";
8709 struct dwo_file *dwo_file;
8710 struct dwo_unit *dwo_unit;
8711 struct virtual_v2_or_v5_dwo_sections sections;
8712 void **dwo_file_slot;
8713 int i;
8714
8715 gdb_assert (dwp_file->version == 2);
8716
8717 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
8718 kind, pulongest (unit_index), hex_string (signature),
8719 dwp_file->name);
8720
8721 /* Fetch the section offsets of this DWO unit. */
8722
8723 memset (&sections, 0, sizeof (sections));
8724
8725 for (i = 0; i < dwp_htab->nr_columns; ++i)
8726 {
8727 uint32_t offset = read_4_bytes (dbfd,
8728 dwp_htab->section_pool.v2.offsets
8729 + (((unit_index - 1) * dwp_htab->nr_columns
8730 + i)
8731 * sizeof (uint32_t)));
8732 uint32_t size = read_4_bytes (dbfd,
8733 dwp_htab->section_pool.v2.sizes
8734 + (((unit_index - 1) * dwp_htab->nr_columns
8735 + i)
8736 * sizeof (uint32_t)));
8737
8738 switch (dwp_htab->section_pool.v2.section_ids[i])
8739 {
8740 case DW_SECT_INFO:
8741 case DW_SECT_TYPES:
8742 sections.info_or_types_offset = offset;
8743 sections.info_or_types_size = size;
8744 break;
8745 case DW_SECT_ABBREV:
8746 sections.abbrev_offset = offset;
8747 sections.abbrev_size = size;
8748 break;
8749 case DW_SECT_LINE:
8750 sections.line_offset = offset;
8751 sections.line_size = size;
8752 break;
8753 case DW_SECT_LOC:
8754 sections.loc_offset = offset;
8755 sections.loc_size = size;
8756 break;
8757 case DW_SECT_STR_OFFSETS:
8758 sections.str_offsets_offset = offset;
8759 sections.str_offsets_size = size;
8760 break;
8761 case DW_SECT_MACINFO:
8762 sections.macinfo_offset = offset;
8763 sections.macinfo_size = size;
8764 break;
8765 case DW_SECT_MACRO:
8766 sections.macro_offset = offset;
8767 sections.macro_size = size;
8768 break;
8769 }
8770 }
8771
8772 /* It's easier for the rest of the code if we fake a struct dwo_file and
8773 have dwo_unit "live" in that. At least for now.
8774
8775 The DWP file can be made up of a random collection of CUs and TUs.
8776 However, for each CU + set of TUs that came from the same original DWO
8777 file, we can combine them back into a virtual DWO file to save space
8778 (fewer struct dwo_file objects to allocate). Remember that for really
8779 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8780
8781 std::string virtual_dwo_name =
8782 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
8783 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8784 (long) (sections.line_size ? sections.line_offset : 0),
8785 (long) (sections.loc_size ? sections.loc_offset : 0),
8786 (long) (sections.str_offsets_size
8787 ? sections.str_offsets_offset : 0));
8788 /* Can we use an existing virtual DWO file? */
8789 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8790 comp_dir);
8791 /* Create one if necessary. */
8792 if (*dwo_file_slot == NULL)
8793 {
8794 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8795 virtual_dwo_name.c_str ());
8796
8797 dwo_file = new struct dwo_file;
8798 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
8799 dwo_file->comp_dir = comp_dir;
8800 dwo_file->sections.abbrev =
8801 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
8802 sections.abbrev_offset,
8803 sections.abbrev_size);
8804 dwo_file->sections.line =
8805 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
8806 sections.line_offset,
8807 sections.line_size);
8808 dwo_file->sections.loc =
8809 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
8810 sections.loc_offset, sections.loc_size);
8811 dwo_file->sections.macinfo =
8812 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
8813 sections.macinfo_offset,
8814 sections.macinfo_size);
8815 dwo_file->sections.macro =
8816 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
8817 sections.macro_offset,
8818 sections.macro_size);
8819 dwo_file->sections.str_offsets =
8820 create_dwp_v2_or_v5_section (per_objfile,
8821 &dwp_file->sections.str_offsets,
8822 sections.str_offsets_offset,
8823 sections.str_offsets_size);
8824 /* The "str" section is global to the entire DWP file. */
8825 dwo_file->sections.str = dwp_file->sections.str;
8826 /* The info or types section is assigned below to dwo_unit,
8827 there's no need to record it in dwo_file.
8828 Also, we can't simply record type sections in dwo_file because
8829 we record a pointer into the vector in dwo_unit. As we collect more
8830 types we'll grow the vector and eventually have to reallocate space
8831 for it, invalidating all copies of pointers into the previous
8832 contents. */
8833 *dwo_file_slot = dwo_file;
8834 }
8835 else
8836 {
8837 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8838 virtual_dwo_name.c_str ());
8839
8840 dwo_file = (struct dwo_file *) *dwo_file_slot;
8841 }
8842
8843 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8844 dwo_unit->dwo_file = dwo_file;
8845 dwo_unit->signature = signature;
8846 dwo_unit->section =
8847 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8848 *dwo_unit->section = create_dwp_v2_or_v5_section
8849 (per_objfile,
8850 is_debug_types
8851 ? &dwp_file->sections.types
8852 : &dwp_file->sections.info,
8853 sections.info_or_types_offset,
8854 sections.info_or_types_size);
8855 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8856
8857 return dwo_unit;
8858 }
8859
8860 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8861 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8862 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8863 This is for DWP version 5 files. */
8864
8865 static struct dwo_unit *
8866 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
8867 struct dwp_file *dwp_file,
8868 uint32_t unit_index,
8869 const char *comp_dir,
8870 ULONGEST signature, int is_debug_types)
8871 {
8872 const struct dwp_hash_table *dwp_htab
8873 = is_debug_types ? dwp_file->tus : dwp_file->cus;
8874 bfd *dbfd = dwp_file->dbfd.get ();
8875 const char *kind = is_debug_types ? "TU" : "CU";
8876 struct dwo_file *dwo_file;
8877 struct dwo_unit *dwo_unit;
8878 struct virtual_v2_or_v5_dwo_sections sections {};
8879 void **dwo_file_slot;
8880
8881 gdb_assert (dwp_file->version == 5);
8882
8883 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
8884 kind, pulongest (unit_index), hex_string (signature),
8885 dwp_file->name);
8886
8887 /* Fetch the section offsets of this DWO unit. */
8888
8889 /* memset (&sections, 0, sizeof (sections)); */
8890
8891 for (int i = 0; i < dwp_htab->nr_columns; ++i)
8892 {
8893 uint32_t offset = read_4_bytes (dbfd,
8894 dwp_htab->section_pool.v5.offsets
8895 + (((unit_index - 1)
8896 * dwp_htab->nr_columns
8897 + i)
8898 * sizeof (uint32_t)));
8899 uint32_t size = read_4_bytes (dbfd,
8900 dwp_htab->section_pool.v5.sizes
8901 + (((unit_index - 1) * dwp_htab->nr_columns
8902 + i)
8903 * sizeof (uint32_t)));
8904
8905 switch (dwp_htab->section_pool.v5.section_ids[i])
8906 {
8907 case DW_SECT_ABBREV_V5:
8908 sections.abbrev_offset = offset;
8909 sections.abbrev_size = size;
8910 break;
8911 case DW_SECT_INFO_V5:
8912 sections.info_or_types_offset = offset;
8913 sections.info_or_types_size = size;
8914 break;
8915 case DW_SECT_LINE_V5:
8916 sections.line_offset = offset;
8917 sections.line_size = size;
8918 break;
8919 case DW_SECT_LOCLISTS_V5:
8920 sections.loclists_offset = offset;
8921 sections.loclists_size = size;
8922 break;
8923 case DW_SECT_MACRO_V5:
8924 sections.macro_offset = offset;
8925 sections.macro_size = size;
8926 break;
8927 case DW_SECT_RNGLISTS_V5:
8928 sections.rnglists_offset = offset;
8929 sections.rnglists_size = size;
8930 break;
8931 case DW_SECT_STR_OFFSETS_V5:
8932 sections.str_offsets_offset = offset;
8933 sections.str_offsets_size = size;
8934 break;
8935 case DW_SECT_RESERVED_V5:
8936 default:
8937 break;
8938 }
8939 }
8940
8941 /* It's easier for the rest of the code if we fake a struct dwo_file and
8942 have dwo_unit "live" in that. At least for now.
8943
8944 The DWP file can be made up of a random collection of CUs and TUs.
8945 However, for each CU + set of TUs that came from the same original DWO
8946 file, we can combine them back into a virtual DWO file to save space
8947 (fewer struct dwo_file objects to allocate). Remember that for really
8948 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8949
8950 std::string virtual_dwo_name =
8951 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
8952 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8953 (long) (sections.line_size ? sections.line_offset : 0),
8954 (long) (sections.loclists_size ? sections.loclists_offset : 0),
8955 (long) (sections.str_offsets_size
8956 ? sections.str_offsets_offset : 0),
8957 (long) (sections.macro_size ? sections.macro_offset : 0),
8958 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
8959 /* Can we use an existing virtual DWO file? */
8960 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
8961 virtual_dwo_name.c_str (),
8962 comp_dir);
8963 /* Create one if necessary. */
8964 if (*dwo_file_slot == NULL)
8965 {
8966 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8967 virtual_dwo_name.c_str ());
8968
8969 dwo_file = new struct dwo_file;
8970 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
8971 dwo_file->comp_dir = comp_dir;
8972 dwo_file->sections.abbrev =
8973 create_dwp_v2_or_v5_section (per_objfile,
8974 &dwp_file->sections.abbrev,
8975 sections.abbrev_offset,
8976 sections.abbrev_size);
8977 dwo_file->sections.line =
8978 create_dwp_v2_or_v5_section (per_objfile,
8979 &dwp_file->sections.line,
8980 sections.line_offset, sections.line_size);
8981 dwo_file->sections.macro =
8982 create_dwp_v2_or_v5_section (per_objfile,
8983 &dwp_file->sections.macro,
8984 sections.macro_offset,
8985 sections.macro_size);
8986 dwo_file->sections.loclists =
8987 create_dwp_v2_or_v5_section (per_objfile,
8988 &dwp_file->sections.loclists,
8989 sections.loclists_offset,
8990 sections.loclists_size);
8991 dwo_file->sections.rnglists =
8992 create_dwp_v2_or_v5_section (per_objfile,
8993 &dwp_file->sections.rnglists,
8994 sections.rnglists_offset,
8995 sections.rnglists_size);
8996 dwo_file->sections.str_offsets =
8997 create_dwp_v2_or_v5_section (per_objfile,
8998 &dwp_file->sections.str_offsets,
8999 sections.str_offsets_offset,
9000 sections.str_offsets_size);
9001 /* The "str" section is global to the entire DWP file. */
9002 dwo_file->sections.str = dwp_file->sections.str;
9003 /* The info or types section is assigned below to dwo_unit,
9004 there's no need to record it in dwo_file.
9005 Also, we can't simply record type sections in dwo_file because
9006 we record a pointer into the vector in dwo_unit. As we collect more
9007 types we'll grow the vector and eventually have to reallocate space
9008 for it, invalidating all copies of pointers into the previous
9009 contents. */
9010 *dwo_file_slot = dwo_file;
9011 }
9012 else
9013 {
9014 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
9015 virtual_dwo_name.c_str ());
9016
9017 dwo_file = (struct dwo_file *) *dwo_file_slot;
9018 }
9019
9020 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
9021 dwo_unit->dwo_file = dwo_file;
9022 dwo_unit->signature = signature;
9023 dwo_unit->section
9024 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
9025 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
9026 &dwp_file->sections.info,
9027 sections.info_or_types_offset,
9028 sections.info_or_types_size);
9029 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
9030
9031 return dwo_unit;
9032 }
9033
9034 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
9035 Returns NULL if the signature isn't found. */
9036
9037 static struct dwo_unit *
9038 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
9039 struct dwp_file *dwp_file, const char *comp_dir,
9040 ULONGEST signature, int is_debug_types)
9041 {
9042 const struct dwp_hash_table *dwp_htab =
9043 is_debug_types ? dwp_file->tus : dwp_file->cus;
9044 bfd *dbfd = dwp_file->dbfd.get ();
9045 uint32_t mask = dwp_htab->nr_slots - 1;
9046 uint32_t hash = signature & mask;
9047 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9048 unsigned int i;
9049 void **slot;
9050 struct dwo_unit find_dwo_cu;
9051
9052 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9053 find_dwo_cu.signature = signature;
9054 slot = htab_find_slot (is_debug_types
9055 ? dwp_file->loaded_tus.get ()
9056 : dwp_file->loaded_cus.get (),
9057 &find_dwo_cu, INSERT);
9058
9059 if (*slot != NULL)
9060 return (struct dwo_unit *) *slot;
9061
9062 /* Use a for loop so that we don't loop forever on bad debug info. */
9063 for (i = 0; i < dwp_htab->nr_slots; ++i)
9064 {
9065 ULONGEST signature_in_table;
9066
9067 signature_in_table =
9068 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
9069 if (signature_in_table == signature)
9070 {
9071 uint32_t unit_index =
9072 read_4_bytes (dbfd,
9073 dwp_htab->unit_table + hash * sizeof (uint32_t));
9074
9075 if (dwp_file->version == 1)
9076 {
9077 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
9078 unit_index, comp_dir,
9079 signature, is_debug_types);
9080 }
9081 else if (dwp_file->version == 2)
9082 {
9083 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
9084 unit_index, comp_dir,
9085 signature, is_debug_types);
9086 }
9087 else /* version == 5 */
9088 {
9089 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
9090 unit_index, comp_dir,
9091 signature, is_debug_types);
9092 }
9093 return (struct dwo_unit *) *slot;
9094 }
9095 if (signature_in_table == 0)
9096 return NULL;
9097 hash = (hash + hash2) & mask;
9098 }
9099
9100 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9101 " [in module %s]"),
9102 dwp_file->name);
9103 }
9104
9105 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9106 Open the file specified by FILE_NAME and hand it off to BFD for
9107 preliminary analysis. Return a newly initialized bfd *, which
9108 includes a canonicalized copy of FILE_NAME.
9109 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9110 SEARCH_CWD is true if the current directory is to be searched.
9111 It will be searched before debug-file-directory.
9112 If successful, the file is added to the bfd include table of the
9113 objfile's bfd (see gdb_bfd_record_inclusion).
9114 If unable to find/open the file, return NULL.
9115 NOTE: This function is derived from symfile_bfd_open. */
9116
9117 static gdb_bfd_ref_ptr
9118 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
9119 const char *file_name, int is_dwp, int search_cwd)
9120 {
9121 int desc;
9122 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9123 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9124 to debug_file_directory. */
9125 const char *search_path;
9126 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9127
9128 gdb::unique_xmalloc_ptr<char> search_path_holder;
9129 if (search_cwd)
9130 {
9131 if (!debug_file_directory.empty ())
9132 {
9133 search_path_holder.reset (concat (".", dirname_separator_string,
9134 debug_file_directory.c_str (),
9135 (char *) NULL));
9136 search_path = search_path_holder.get ();
9137 }
9138 else
9139 search_path = ".";
9140 }
9141 else
9142 search_path = debug_file_directory.c_str ();
9143
9144 /* Add the path for the executable binary to the list of search paths. */
9145 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
9146 search_path_holder.reset (concat (objfile_dir.c_str (),
9147 dirname_separator_string,
9148 search_path, nullptr));
9149 search_path = search_path_holder.get ();
9150
9151 openp_flags flags = OPF_RETURN_REALPATH;
9152 if (is_dwp)
9153 flags |= OPF_SEARCH_IN_PATH;
9154
9155 gdb::unique_xmalloc_ptr<char> absolute_name;
9156 desc = openp (search_path, flags, file_name,
9157 O_RDONLY | O_BINARY, &absolute_name);
9158 if (desc < 0)
9159 return NULL;
9160
9161 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
9162 gnutarget, desc));
9163 if (sym_bfd == NULL)
9164 return NULL;
9165 bfd_set_cacheable (sym_bfd.get (), 1);
9166
9167 if (!bfd_check_format (sym_bfd.get (), bfd_object))
9168 return NULL;
9169
9170 /* Success. Record the bfd as having been included by the objfile's bfd.
9171 This is important because things like demangled_names_hash lives in the
9172 objfile's per_bfd space and may have references to things like symbol
9173 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
9174 gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
9175
9176 return sym_bfd;
9177 }
9178
9179 /* Try to open DWO file FILE_NAME.
9180 COMP_DIR is the DW_AT_comp_dir attribute.
9181 The result is the bfd handle of the file.
9182 If there is a problem finding or opening the file, return NULL.
9183 Upon success, the canonicalized path of the file is stored in the bfd,
9184 same as symfile_bfd_open. */
9185
9186 static gdb_bfd_ref_ptr
9187 open_dwo_file (dwarf2_per_objfile *per_objfile,
9188 const char *file_name, const char *comp_dir)
9189 {
9190 if (IS_ABSOLUTE_PATH (file_name))
9191 return try_open_dwop_file (per_objfile, file_name,
9192 0 /*is_dwp*/, 0 /*search_cwd*/);
9193
9194 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9195
9196 if (comp_dir != NULL)
9197 {
9198 std::string path_to_try = path_join (comp_dir, file_name);
9199
9200 /* NOTE: If comp_dir is a relative path, this will also try the
9201 search path, which seems useful. */
9202 gdb_bfd_ref_ptr abfd (try_open_dwop_file
9203 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
9204
9205 if (abfd != NULL)
9206 return abfd;
9207 }
9208
9209 /* That didn't work, try debug-file-directory, which, despite its name,
9210 is a list of paths. */
9211
9212 if (debug_file_directory.empty ())
9213 return NULL;
9214
9215 return try_open_dwop_file (per_objfile, file_name,
9216 0 /*is_dwp*/, 1 /*search_cwd*/);
9217 }
9218
9219 /* This function is mapped across the sections and remembers the offset and
9220 size of each of the DWO debugging sections we are interested in. */
9221
9222 static void
9223 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
9224 asection *sectp, dwo_sections *dwo_sections)
9225 {
9226 const struct dwop_section_names *names = &dwop_section_names;
9227
9228 struct dwarf2_section_info *dw_sect = nullptr;
9229
9230 if (names->abbrev_dwo.matches (sectp->name))
9231 dw_sect = &dwo_sections->abbrev;
9232 else if (names->info_dwo.matches (sectp->name))
9233 dw_sect = &dwo_sections->info;
9234 else if (names->line_dwo.matches (sectp->name))
9235 dw_sect = &dwo_sections->line;
9236 else if (names->loc_dwo.matches (sectp->name))
9237 dw_sect = &dwo_sections->loc;
9238 else if (names->loclists_dwo.matches (sectp->name))
9239 dw_sect = &dwo_sections->loclists;
9240 else if (names->macinfo_dwo.matches (sectp->name))
9241 dw_sect = &dwo_sections->macinfo;
9242 else if (names->macro_dwo.matches (sectp->name))
9243 dw_sect = &dwo_sections->macro;
9244 else if (names->rnglists_dwo.matches (sectp->name))
9245 dw_sect = &dwo_sections->rnglists;
9246 else if (names->str_dwo.matches (sectp->name))
9247 dw_sect = &dwo_sections->str;
9248 else if (names->str_offsets_dwo.matches (sectp->name))
9249 dw_sect = &dwo_sections->str_offsets;
9250 else if (names->types_dwo.matches (sectp->name))
9251 {
9252 struct dwarf2_section_info type_section;
9253
9254 memset (&type_section, 0, sizeof (type_section));
9255 dwo_sections->types.push_back (type_section);
9256 dw_sect = &dwo_sections->types.back ();
9257 }
9258
9259 if (dw_sect != nullptr)
9260 {
9261 dw_sect->s.section = sectp;
9262 dw_sect->size = bfd_section_size (sectp);
9263 dw_sect->read (objfile);
9264 }
9265 }
9266
9267 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9268 by PER_CU. This is for the non-DWP case.
9269 The result is NULL if DWO_NAME can't be found. */
9270
9271 static struct dwo_file *
9272 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
9273 const char *comp_dir)
9274 {
9275 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9276
9277 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
9278 if (dbfd == NULL)
9279 {
9280 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
9281
9282 return NULL;
9283 }
9284
9285 dwo_file_up dwo_file (new struct dwo_file);
9286 dwo_file->dwo_name = dwo_name;
9287 dwo_file->comp_dir = comp_dir;
9288 dwo_file->dbfd = std::move (dbfd);
9289
9290 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
9291 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
9292 sec, &dwo_file->sections);
9293
9294 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
9295 dwo_file->cus);
9296
9297 if (cu->per_cu->version () < 5)
9298 {
9299 create_debug_types_hash_table (per_objfile, dwo_file.get (),
9300 dwo_file->sections.types, dwo_file->tus);
9301 }
9302 else
9303 {
9304 create_debug_type_hash_table (per_objfile, dwo_file.get (),
9305 &dwo_file->sections.info, dwo_file->tus,
9306 rcuh_kind::COMPILE);
9307 }
9308
9309 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
9310
9311 return dwo_file.release ();
9312 }
9313
9314 /* This function is mapped across the sections and remembers the offset and
9315 size of each of the DWP debugging sections common to version 1 and 2 that
9316 we are interested in. */
9317
9318 static void
9319 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
9320 asection *sectp, dwp_file *dwp_file)
9321 {
9322 const struct dwop_section_names *names = &dwop_section_names;
9323 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9324
9325 /* Record the ELF section number for later lookup: this is what the
9326 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9327 gdb_assert (elf_section_nr < dwp_file->num_sections);
9328 dwp_file->elf_sections[elf_section_nr] = sectp;
9329
9330 /* Look for specific sections that we need. */
9331 struct dwarf2_section_info *dw_sect = nullptr;
9332 if (names->str_dwo.matches (sectp->name))
9333 dw_sect = &dwp_file->sections.str;
9334 else if (names->cu_index.matches (sectp->name))
9335 dw_sect = &dwp_file->sections.cu_index;
9336 else if (names->tu_index.matches (sectp->name))
9337 dw_sect = &dwp_file->sections.tu_index;
9338
9339 if (dw_sect != nullptr)
9340 {
9341 dw_sect->s.section = sectp;
9342 dw_sect->size = bfd_section_size (sectp);
9343 dw_sect->read (objfile);
9344 }
9345 }
9346
9347 /* This function is mapped across the sections and remembers the offset and
9348 size of each of the DWP version 2 debugging sections that we are interested
9349 in. This is split into a separate function because we don't know if we
9350 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9351
9352 static void
9353 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
9354 asection *sectp, void *dwp_file_ptr)
9355 {
9356 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9357 const struct dwop_section_names *names = &dwop_section_names;
9358 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9359
9360 /* Record the ELF section number for later lookup: this is what the
9361 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9362 gdb_assert (elf_section_nr < dwp_file->num_sections);
9363 dwp_file->elf_sections[elf_section_nr] = sectp;
9364
9365 /* Look for specific sections that we need. */
9366 struct dwarf2_section_info *dw_sect = nullptr;
9367 if (names->abbrev_dwo.matches (sectp->name))
9368 dw_sect = &dwp_file->sections.abbrev;
9369 else if (names->info_dwo.matches (sectp->name))
9370 dw_sect = &dwp_file->sections.info;
9371 else if (names->line_dwo.matches (sectp->name))
9372 dw_sect = &dwp_file->sections.line;
9373 else if (names->loc_dwo.matches (sectp->name))
9374 dw_sect = &dwp_file->sections.loc;
9375 else if (names->macinfo_dwo.matches (sectp->name))
9376 dw_sect = &dwp_file->sections.macinfo;
9377 else if (names->macro_dwo.matches (sectp->name))
9378 dw_sect = &dwp_file->sections.macro;
9379 else if (names->str_offsets_dwo.matches (sectp->name))
9380 dw_sect = &dwp_file->sections.str_offsets;
9381 else if (names->types_dwo.matches (sectp->name))
9382 dw_sect = &dwp_file->sections.types;
9383
9384 if (dw_sect != nullptr)
9385 {
9386 dw_sect->s.section = sectp;
9387 dw_sect->size = bfd_section_size (sectp);
9388 dw_sect->read (objfile);
9389 }
9390 }
9391
9392 /* This function is mapped across the sections and remembers the offset and
9393 size of each of the DWP version 5 debugging sections that we are interested
9394 in. This is split into a separate function because we don't know if we
9395 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9396
9397 static void
9398 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
9399 asection *sectp, void *dwp_file_ptr)
9400 {
9401 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9402 const struct dwop_section_names *names = &dwop_section_names;
9403 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9404
9405 /* Record the ELF section number for later lookup: this is what the
9406 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9407 gdb_assert (elf_section_nr < dwp_file->num_sections);
9408 dwp_file->elf_sections[elf_section_nr] = sectp;
9409
9410 /* Look for specific sections that we need. */
9411 struct dwarf2_section_info *dw_sect = nullptr;
9412 if (names->abbrev_dwo.matches (sectp->name))
9413 dw_sect = &dwp_file->sections.abbrev;
9414 else if (names->info_dwo.matches (sectp->name))
9415 dw_sect = &dwp_file->sections.info;
9416 else if (names->line_dwo.matches (sectp->name))
9417 dw_sect = &dwp_file->sections.line;
9418 else if (names->loclists_dwo.matches (sectp->name))
9419 dw_sect = &dwp_file->sections.loclists;
9420 else if (names->macro_dwo.matches (sectp->name))
9421 dw_sect = &dwp_file->sections.macro;
9422 else if (names->rnglists_dwo.matches (sectp->name))
9423 dw_sect = &dwp_file->sections.rnglists;
9424 else if (names->str_offsets_dwo.matches (sectp->name))
9425 dw_sect = &dwp_file->sections.str_offsets;
9426
9427 if (dw_sect != nullptr)
9428 {
9429 dw_sect->s.section = sectp;
9430 dw_sect->size = bfd_section_size (sectp);
9431 dw_sect->read (objfile);
9432 }
9433 }
9434
9435 /* Hash function for dwp_file loaded CUs/TUs. */
9436
9437 static hashval_t
9438 hash_dwp_loaded_cutus (const void *item)
9439 {
9440 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9441
9442 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9443 return dwo_unit->signature;
9444 }
9445
9446 /* Equality function for dwp_file loaded CUs/TUs. */
9447
9448 static int
9449 eq_dwp_loaded_cutus (const void *a, const void *b)
9450 {
9451 const struct dwo_unit *dua = (const struct dwo_unit *) a;
9452 const struct dwo_unit *dub = (const struct dwo_unit *) b;
9453
9454 return dua->signature == dub->signature;
9455 }
9456
9457 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9458
9459 static htab_up
9460 allocate_dwp_loaded_cutus_table ()
9461 {
9462 return htab_up (htab_create_alloc (3,
9463 hash_dwp_loaded_cutus,
9464 eq_dwp_loaded_cutus,
9465 NULL, xcalloc, xfree));
9466 }
9467
9468 /* Try to open DWP file FILE_NAME.
9469 The result is the bfd handle of the file.
9470 If there is a problem finding or opening the file, return NULL.
9471 Upon success, the canonicalized path of the file is stored in the bfd,
9472 same as symfile_bfd_open. */
9473
9474 static gdb_bfd_ref_ptr
9475 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
9476 {
9477 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
9478 1 /*is_dwp*/,
9479 1 /*search_cwd*/));
9480 if (abfd != NULL)
9481 return abfd;
9482
9483 /* Work around upstream bug 15652.
9484 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9485 [Whether that's a "bug" is debatable, but it is getting in our way.]
9486 We have no real idea where the dwp file is, because gdb's realpath-ing
9487 of the executable's path may have discarded the needed info.
9488 [IWBN if the dwp file name was recorded in the executable, akin to
9489 .gnu_debuglink, but that doesn't exist yet.]
9490 Strip the directory from FILE_NAME and search again. */
9491 if (!debug_file_directory.empty ())
9492 {
9493 /* Don't implicitly search the current directory here.
9494 If the user wants to search "." to handle this case,
9495 it must be added to debug-file-directory. */
9496 return try_open_dwop_file (per_objfile, lbasename (file_name),
9497 1 /*is_dwp*/,
9498 0 /*search_cwd*/);
9499 }
9500
9501 return NULL;
9502 }
9503
9504 /* Initialize the use of the DWP file for the current objfile.
9505 By convention the name of the DWP file is ${objfile}.dwp.
9506 The result is NULL if it can't be found. */
9507
9508 static std::unique_ptr<struct dwp_file>
9509 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
9510 {
9511 struct objfile *objfile = per_objfile->objfile;
9512
9513 /* Try to find first .dwp for the binary file before any symbolic links
9514 resolving. */
9515
9516 /* If the objfile is a debug file, find the name of the real binary
9517 file and get the name of dwp file from there. */
9518 std::string dwp_name;
9519 if (objfile->separate_debug_objfile_backlink != NULL)
9520 {
9521 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
9522 const char *backlink_basename = lbasename (backlink->original_name);
9523
9524 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
9525 }
9526 else
9527 dwp_name = objfile->original_name;
9528
9529 dwp_name += ".dwp";
9530
9531 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
9532 if (dbfd == NULL
9533 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
9534 {
9535 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9536 dwp_name = objfile_name (objfile);
9537 dwp_name += ".dwp";
9538 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
9539 }
9540
9541 if (dbfd == NULL)
9542 {
9543 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
9544
9545 return std::unique_ptr<dwp_file> ();
9546 }
9547
9548 const char *name = bfd_get_filename (dbfd.get ());
9549 std::unique_ptr<struct dwp_file> dwp_file
9550 (new struct dwp_file (name, std::move (dbfd)));
9551
9552 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
9553 dwp_file->elf_sections =
9554 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
9555 dwp_file->num_sections, asection *);
9556
9557 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9558 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9559 dwp_file.get ());
9560
9561 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
9562
9563 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
9564
9565 /* The DWP file version is stored in the hash table. Oh well. */
9566 if (dwp_file->cus && dwp_file->tus
9567 && dwp_file->cus->version != dwp_file->tus->version)
9568 {
9569 /* Technically speaking, we should try to limp along, but this is
9570 pretty bizarre. We use pulongest here because that's the established
9571 portability solution (e.g, we cannot use %u for uint32_t). */
9572 error (_("Dwarf Error: DWP file CU version %s doesn't match"
9573 " TU version %s [in DWP file %s]"),
9574 pulongest (dwp_file->cus->version),
9575 pulongest (dwp_file->tus->version), dwp_name.c_str ());
9576 }
9577
9578 if (dwp_file->cus)
9579 dwp_file->version = dwp_file->cus->version;
9580 else if (dwp_file->tus)
9581 dwp_file->version = dwp_file->tus->version;
9582 else
9583 dwp_file->version = 2;
9584
9585 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9586 {
9587 if (dwp_file->version == 2)
9588 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9589 dwp_file.get ());
9590 else
9591 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9592 dwp_file.get ());
9593 }
9594
9595 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
9596 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
9597
9598 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
9599 dwarf_read_debug_printf (" %s CUs, %s TUs",
9600 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9601 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9602
9603 return dwp_file;
9604 }
9605
9606 /* Wrapper around open_and_init_dwp_file, only open it once. */
9607
9608 static struct dwp_file *
9609 get_dwp_file (dwarf2_per_objfile *per_objfile)
9610 {
9611 if (!per_objfile->per_bfd->dwp_checked)
9612 {
9613 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
9614 per_objfile->per_bfd->dwp_checked = 1;
9615 }
9616 return per_objfile->per_bfd->dwp_file.get ();
9617 }
9618
9619 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9620 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9621 or in the DWP file for the objfile, referenced by THIS_UNIT.
9622 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9623 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9624
9625 This is called, for example, when wanting to read a variable with a
9626 complex location. Therefore we don't want to do file i/o for every call.
9627 Therefore we don't want to look for a DWO file on every call.
9628 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9629 then we check if we've already seen DWO_NAME, and only THEN do we check
9630 for a DWO file.
9631
9632 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9633 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9634
9635 static struct dwo_unit *
9636 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9637 ULONGEST signature, int is_debug_types)
9638 {
9639 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9640 struct objfile *objfile = per_objfile->objfile;
9641 const char *kind = is_debug_types ? "TU" : "CU";
9642 void **dwo_file_slot;
9643 struct dwo_file *dwo_file;
9644 struct dwp_file *dwp_file;
9645
9646 /* First see if there's a DWP file.
9647 If we have a DWP file but didn't find the DWO inside it, don't
9648 look for the original DWO file. It makes gdb behave differently
9649 depending on whether one is debugging in the build tree. */
9650
9651 dwp_file = get_dwp_file (per_objfile);
9652 if (dwp_file != NULL)
9653 {
9654 const struct dwp_hash_table *dwp_htab =
9655 is_debug_types ? dwp_file->tus : dwp_file->cus;
9656
9657 if (dwp_htab != NULL)
9658 {
9659 struct dwo_unit *dwo_cutu =
9660 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
9661 is_debug_types);
9662
9663 if (dwo_cutu != NULL)
9664 {
9665 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
9666 kind, hex_string (signature),
9667 host_address_to_string (dwo_cutu));
9668
9669 return dwo_cutu;
9670 }
9671 }
9672 }
9673 else
9674 {
9675 /* No DWP file, look for the DWO file. */
9676
9677 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
9678 if (*dwo_file_slot == NULL)
9679 {
9680 /* Read in the file and build a table of the CUs/TUs it contains. */
9681 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
9682 }
9683 /* NOTE: This will be NULL if unable to open the file. */
9684 dwo_file = (struct dwo_file *) *dwo_file_slot;
9685
9686 if (dwo_file != NULL)
9687 {
9688 struct dwo_unit *dwo_cutu = NULL;
9689
9690 if (is_debug_types && dwo_file->tus)
9691 {
9692 struct dwo_unit find_dwo_cutu;
9693
9694 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9695 find_dwo_cutu.signature = signature;
9696 dwo_cutu
9697 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
9698 &find_dwo_cutu);
9699 }
9700 else if (!is_debug_types && dwo_file->cus)
9701 {
9702 struct dwo_unit find_dwo_cutu;
9703
9704 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9705 find_dwo_cutu.signature = signature;
9706 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
9707 &find_dwo_cutu);
9708 }
9709
9710 if (dwo_cutu != NULL)
9711 {
9712 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
9713 kind, dwo_name, hex_string (signature),
9714 host_address_to_string (dwo_cutu));
9715
9716 return dwo_cutu;
9717 }
9718 }
9719 }
9720
9721 /* We didn't find it. This could mean a dwo_id mismatch, or
9722 someone deleted the DWO/DWP file, or the search path isn't set up
9723 correctly to find the file. */
9724
9725 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
9726 kind, dwo_name, hex_string (signature));
9727
9728 /* This is a warning and not a complaint because it can be caused by
9729 pilot error (e.g., user accidentally deleting the DWO). */
9730 {
9731 /* Print the name of the DWP file if we looked there, helps the user
9732 better diagnose the problem. */
9733 std::string dwp_text;
9734
9735 if (dwp_file != NULL)
9736 dwp_text = string_printf (" [in DWP file %s]",
9737 lbasename (dwp_file->name));
9738
9739 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
9740 " [in module %s]"),
9741 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
9742 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
9743 }
9744 return NULL;
9745 }
9746
9747 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9748 See lookup_dwo_cutu_unit for details. */
9749
9750 static struct dwo_unit *
9751 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9752 ULONGEST signature)
9753 {
9754 gdb_assert (!cu->per_cu->is_debug_types);
9755
9756 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
9757 }
9758
9759 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9760 See lookup_dwo_cutu_unit for details. */
9761
9762 static struct dwo_unit *
9763 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
9764 {
9765 gdb_assert (cu->per_cu->is_debug_types);
9766
9767 signatured_type *sig_type = (signatured_type *) cu->per_cu;
9768
9769 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
9770 }
9771
9772 /* Traversal function for queue_and_load_all_dwo_tus. */
9773
9774 static int
9775 queue_and_load_dwo_tu (void **slot, void *info)
9776 {
9777 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9778 dwarf2_cu *cu = (dwarf2_cu *) info;
9779 ULONGEST signature = dwo_unit->signature;
9780 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
9781
9782 if (sig_type != NULL)
9783 {
9784 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9785 a real dependency of PER_CU on SIG_TYPE. That is detected later
9786 while processing PER_CU. */
9787 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
9788 cu->lang ()))
9789 load_full_type_unit (sig_type, cu->per_objfile);
9790 cu->per_cu->imported_symtabs_push (sig_type);
9791 }
9792
9793 return 1;
9794 }
9795
9796 /* Queue all TUs contained in the DWO of CU to be read in.
9797 The DWO may have the only definition of the type, though it may not be
9798 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9799 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9800
9801 static void
9802 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
9803 {
9804 struct dwo_unit *dwo_unit;
9805 struct dwo_file *dwo_file;
9806
9807 gdb_assert (cu != nullptr);
9808 gdb_assert (!cu->per_cu->is_debug_types);
9809 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
9810
9811 dwo_unit = cu->dwo_unit;
9812 gdb_assert (dwo_unit != NULL);
9813
9814 dwo_file = dwo_unit->dwo_file;
9815 if (dwo_file->tus != NULL)
9816 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
9817 }
9818
9819 /* Read in various DIEs. */
9820
9821 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9822 Inherit only the children of the DW_AT_abstract_origin DIE not being
9823 already referenced by DW_AT_abstract_origin from the children of the
9824 current DIE. */
9825
9826 static void
9827 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9828 {
9829 attribute *attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9830 if (attr == nullptr)
9831 return;
9832
9833 /* Note that following die references may follow to a die in a
9834 different CU. */
9835 dwarf2_cu *origin_cu = cu;
9836
9837 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9838 die_info *origin_die = follow_die_ref (die, attr, &origin_cu);
9839
9840 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9841 symbols in. */
9842 struct pending **origin_previous_list_in_scope = origin_cu->list_in_scope;
9843 origin_cu->list_in_scope = cu->list_in_scope;
9844
9845 if (die->tag != origin_die->tag
9846 && !(die->tag == DW_TAG_inlined_subroutine
9847 && origin_die->tag == DW_TAG_subprogram))
9848 complaint (_("DIE %s and its abstract origin %s have different tags"),
9849 sect_offset_str (die->sect_off),
9850 sect_offset_str (origin_die->sect_off));
9851
9852 /* Find if the concrete and abstract trees are structurally the
9853 same. This is a shallow traversal and it is not bullet-proof;
9854 the compiler can trick the debugger into believing that the trees
9855 are isomorphic, whereas they actually are not. However, the
9856 likelyhood of this happening is pretty low, and a full-fledged
9857 check would be an overkill. */
9858 bool are_isomorphic = true;
9859 die_info *concrete_child = die->child;
9860 die_info *abstract_child = origin_die->child;
9861 while (concrete_child != nullptr || abstract_child != nullptr)
9862 {
9863 if (concrete_child == nullptr
9864 || abstract_child == nullptr
9865 || concrete_child->tag != abstract_child->tag)
9866 {
9867 are_isomorphic = false;
9868 break;
9869 }
9870
9871 concrete_child = concrete_child->sibling;
9872 abstract_child = abstract_child->sibling;
9873 }
9874
9875 /* Walk the origin's children in parallel to the concrete children.
9876 This helps match an origin child in case the debug info misses
9877 DW_AT_abstract_origin attributes. Keep in mind that the abstract
9878 origin tree may not have the same tree structure as the concrete
9879 DIE, though. */
9880 die_info *corresponding_abstract_child
9881 = are_isomorphic ? origin_die->child : nullptr;
9882
9883 std::vector<sect_offset> offsets;
9884
9885 for (die_info *child_die = die->child;
9886 child_die && child_die->tag;
9887 child_die = child_die->sibling)
9888 {
9889 /* We are trying to process concrete instance entries:
9890 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
9891 it's not relevant to our analysis here. i.e. detecting DIEs that are
9892 present in the abstract instance but not referenced in the concrete
9893 one. */
9894 if (child_die->tag == DW_TAG_call_site
9895 || child_die->tag == DW_TAG_GNU_call_site)
9896 {
9897 if (are_isomorphic)
9898 corresponding_abstract_child
9899 = corresponding_abstract_child->sibling;
9900 continue;
9901 }
9902
9903 /* For each CHILD_DIE, find the corresponding child of
9904 ORIGIN_DIE. If there is more than one layer of
9905 DW_AT_abstract_origin, follow them all; there shouldn't be,
9906 but GCC versions at least through 4.4 generate this (GCC PR
9907 40573). */
9908 die_info *child_origin_die = child_die;
9909 dwarf2_cu *child_origin_cu = cu;
9910 while (true)
9911 {
9912 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9913 child_origin_cu);
9914 if (attr == nullptr)
9915 break;
9916
9917 child_origin_die = follow_die_ref (child_origin_die, attr,
9918 &child_origin_cu);
9919 }
9920
9921 /* If missing DW_AT_abstract_origin, try the corresponding child
9922 of the origin. Clang emits such lexical scopes. */
9923 if (child_origin_die == child_die
9924 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
9925 && are_isomorphic
9926 && child_die->tag == DW_TAG_lexical_block)
9927 child_origin_die = corresponding_abstract_child;
9928
9929 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9930 counterpart may exist. */
9931 if (child_origin_die != child_die)
9932 {
9933 if (child_die->tag != child_origin_die->tag
9934 && !(child_die->tag == DW_TAG_inlined_subroutine
9935 && child_origin_die->tag == DW_TAG_subprogram))
9936 complaint (_("Child DIE %s and its abstract origin %s have "
9937 "different tags"),
9938 sect_offset_str (child_die->sect_off),
9939 sect_offset_str (child_origin_die->sect_off));
9940 if (child_origin_die->parent != origin_die)
9941 complaint (_("Child DIE %s and its abstract origin %s have "
9942 "different parents"),
9943 sect_offset_str (child_die->sect_off),
9944 sect_offset_str (child_origin_die->sect_off));
9945 else
9946 offsets.push_back (child_origin_die->sect_off);
9947 }
9948
9949 if (are_isomorphic)
9950 corresponding_abstract_child = corresponding_abstract_child->sibling;
9951 }
9952
9953 if (!offsets.empty ())
9954 {
9955 std::sort (offsets.begin (), offsets.end ());
9956
9957 for (auto offsets_it = offsets.begin () + 1;
9958 offsets_it < offsets.end ();
9959 ++offsets_it)
9960 if (*(offsets_it - 1) == *offsets_it)
9961 complaint (_("Multiple children of DIE %s refer "
9962 "to DIE %s as their abstract origin"),
9963 sect_offset_str (die->sect_off),
9964 sect_offset_str (*offsets_it));
9965 }
9966
9967 auto offsets_it = offsets.begin ();
9968 die_info *origin_child_die = origin_die->child;
9969 while (origin_child_die != nullptr && origin_child_die->tag != 0)
9970 {
9971 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9972 while (offsets_it < offsets.end ()
9973 && *offsets_it < origin_child_die->sect_off)
9974 ++offsets_it;
9975
9976 if (offsets_it == offsets.end ()
9977 || *offsets_it > origin_child_die->sect_off)
9978 {
9979 /* Found that ORIGIN_CHILD_DIE is really not referenced.
9980 Check whether we're already processing ORIGIN_CHILD_DIE.
9981 This can happen with mutually referenced abstract_origins.
9982 PR 16581. */
9983 if (!origin_child_die->in_process)
9984 process_die (origin_child_die, origin_cu);
9985 }
9986
9987 origin_child_die = origin_child_die->sibling;
9988 }
9989
9990 origin_cu->list_in_scope = origin_previous_list_in_scope;
9991
9992 if (cu != origin_cu)
9993 compute_delayed_physnames (origin_cu);
9994 }
9995
9996 static void
9997 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9998 {
9999 struct objfile *objfile = cu->per_objfile->objfile;
10000 struct gdbarch *gdbarch = objfile->arch ();
10001 struct context_stack *newobj;
10002 CORE_ADDR lowpc;
10003 CORE_ADDR highpc;
10004 struct die_info *child_die;
10005 struct attribute *attr, *call_line, *call_file;
10006 const char *name;
10007 CORE_ADDR baseaddr;
10008 struct block *block;
10009 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10010 std::vector<struct symbol *> template_args;
10011 struct template_symbol *templ_func = NULL;
10012
10013 if (inlined_func)
10014 {
10015 /* If we do not have call site information, we can't show the
10016 caller of this inlined function. That's too confusing, so
10017 only use the scope for local variables. */
10018 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10019 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10020 if (call_line == NULL || call_file == NULL)
10021 {
10022 read_lexical_block_scope (die, cu);
10023 return;
10024 }
10025 }
10026
10027 baseaddr = objfile->text_section_offset ();
10028
10029 name = dwarf2_name (die, cu);
10030 if (name == nullptr)
10031 name = dw2_linkage_name (die, cu);
10032
10033 /* Ignore functions with missing or empty names. These are actually
10034 illegal according to the DWARF standard. */
10035 if (name == NULL)
10036 {
10037 complaint (_("missing name for subprogram DIE at %s"),
10038 sect_offset_str (die->sect_off));
10039 return;
10040 }
10041
10042 /* Ignore functions with missing or invalid low and high pc attributes. */
10043 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
10044 <= PC_BOUNDS_INVALID)
10045 {
10046 if (have_complaint ())
10047 {
10048 attr = dwarf2_attr (die, DW_AT_external, cu);
10049 bool external_p = attr != nullptr && attr->as_boolean ();
10050 attr = dwarf2_attr (die, DW_AT_inline, cu);
10051 bool inlined_p
10052 = (attr != nullptr
10053 && attr->is_nonnegative ()
10054 && (attr->as_nonnegative () == DW_INL_inlined
10055 || attr->as_nonnegative () == DW_INL_declared_inlined));
10056 attr = dwarf2_attr (die, DW_AT_declaration, cu);
10057 bool decl_p = attr != nullptr && attr->as_boolean ();
10058 if (!external_p && !inlined_p && !decl_p)
10059 complaint (_("cannot get low and high bounds "
10060 "for subprogram DIE at %s"),
10061 sect_offset_str (die->sect_off));
10062 }
10063 return;
10064 }
10065
10066 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10067 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10068
10069 /* If we have any template arguments, then we must allocate a
10070 different sort of symbol. */
10071 for (child_die = die->child; child_die; child_die = child_die->sibling)
10072 {
10073 if (child_die->tag == DW_TAG_template_type_param
10074 || child_die->tag == DW_TAG_template_value_param)
10075 {
10076 templ_func = new (&objfile->objfile_obstack) template_symbol;
10077 templ_func->subclass = SYMBOL_TEMPLATE;
10078 break;
10079 }
10080 }
10081
10082 gdb_assert (cu->get_builder () != nullptr);
10083 newobj = cu->get_builder ()->push_context (0, lowpc);
10084 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
10085 (struct symbol *) templ_func);
10086
10087 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
10088 set_objfile_main_name (objfile, newobj->name->linkage_name (),
10089 cu->lang ());
10090
10091 /* If there is a location expression for DW_AT_frame_base, record
10092 it. */
10093 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10094 if (attr != nullptr)
10095 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
10096
10097 /* If there is a location for the static link, record it. */
10098 newobj->static_link = NULL;
10099 attr = dwarf2_attr (die, DW_AT_static_link, cu);
10100 if (attr != nullptr)
10101 {
10102 newobj->static_link
10103 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
10104 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
10105 cu->addr_type ());
10106 }
10107
10108 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
10109
10110 if (die->child != NULL)
10111 {
10112 child_die = die->child;
10113 while (child_die && child_die->tag)
10114 {
10115 if (child_die->tag == DW_TAG_template_type_param
10116 || child_die->tag == DW_TAG_template_value_param)
10117 {
10118 struct symbol *arg = new_symbol (child_die, NULL, cu);
10119
10120 if (arg != NULL)
10121 template_args.push_back (arg);
10122 }
10123 else
10124 process_die (child_die, cu);
10125 child_die = child_die->sibling;
10126 }
10127 }
10128
10129 inherit_abstract_dies (die, cu);
10130
10131 /* If we have a DW_AT_specification, we might need to import using
10132 directives from the context of the specification DIE. See the
10133 comment in determine_prefix. */
10134 if (cu->lang () == language_cplus
10135 && dwarf2_attr (die, DW_AT_specification, cu))
10136 {
10137 struct dwarf2_cu *spec_cu = cu;
10138 struct die_info *spec_die = die_specification (die, &spec_cu);
10139
10140 while (spec_die)
10141 {
10142 child_die = spec_die->child;
10143 while (child_die && child_die->tag)
10144 {
10145 if (child_die->tag == DW_TAG_imported_module)
10146 process_die (child_die, spec_cu);
10147 child_die = child_die->sibling;
10148 }
10149
10150 /* In some cases, GCC generates specification DIEs that
10151 themselves contain DW_AT_specification attributes. */
10152 spec_die = die_specification (spec_die, &spec_cu);
10153 }
10154 }
10155
10156 struct context_stack cstk = cu->get_builder ()->pop_context ();
10157 /* Make a block for the local symbols within. */
10158 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
10159 cstk.static_link, lowpc, highpc);
10160
10161 /* For C++, set the block's scope. */
10162 if ((cu->lang () == language_cplus
10163 || cu->lang () == language_fortran
10164 || cu->lang () == language_d
10165 || cu->lang () == language_rust)
10166 && cu->processing_has_namespace_info)
10167 block->set_scope (determine_prefix (die, cu),
10168 &objfile->objfile_obstack);
10169
10170 /* If we have address ranges, record them. */
10171 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10172
10173 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
10174
10175 /* Attach template arguments to function. */
10176 if (!template_args.empty ())
10177 {
10178 gdb_assert (templ_func != NULL);
10179
10180 templ_func->n_template_arguments = template_args.size ();
10181 templ_func->template_arguments
10182 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
10183 templ_func->n_template_arguments);
10184 memcpy (templ_func->template_arguments,
10185 template_args.data (),
10186 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10187
10188 /* Make sure that the symtab is set on the new symbols. Even
10189 though they don't appear in this symtab directly, other parts
10190 of gdb assume that symbols do, and this is reasonably
10191 true. */
10192 for (symbol *sym : template_args)
10193 sym->set_symtab (templ_func->symtab ());
10194 }
10195
10196 /* In C++, we can have functions nested inside functions (e.g., when
10197 a function declares a class that has methods). This means that
10198 when we finish processing a function scope, we may need to go
10199 back to building a containing block's symbol lists. */
10200 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10201 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10202
10203 /* If we've finished processing a top-level function, subsequent
10204 symbols go in the file symbol list. */
10205 if (cu->get_builder ()->outermost_context_p ())
10206 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
10207 }
10208
10209 /* Process all the DIES contained within a lexical block scope. Start
10210 a new scope, process the dies, and then close the scope. */
10211
10212 static void
10213 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10214 {
10215 struct objfile *objfile = cu->per_objfile->objfile;
10216 struct gdbarch *gdbarch = objfile->arch ();
10217 CORE_ADDR lowpc, highpc;
10218 struct die_info *child_die;
10219 CORE_ADDR baseaddr;
10220
10221 baseaddr = objfile->text_section_offset ();
10222
10223 /* Ignore blocks with missing or invalid low and high pc attributes. */
10224 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10225 as multiple lexical blocks? Handling children in a sane way would
10226 be nasty. Might be easier to properly extend generic blocks to
10227 describe ranges. */
10228 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
10229 {
10230 case PC_BOUNDS_NOT_PRESENT:
10231 /* DW_TAG_lexical_block has no attributes, process its children as if
10232 there was no wrapping by that DW_TAG_lexical_block.
10233 GCC does no longer produces such DWARF since GCC r224161. */
10234 for (child_die = die->child;
10235 child_die != NULL && child_die->tag;
10236 child_die = child_die->sibling)
10237 {
10238 /* We might already be processing this DIE. This can happen
10239 in an unusual circumstance -- where a subroutine A
10240 appears lexically in another subroutine B, but A actually
10241 inlines B. The recursion is broken here, rather than in
10242 inherit_abstract_dies, because it seems better to simply
10243 drop concrete children here. */
10244 if (!child_die->in_process)
10245 process_die (child_die, cu);
10246 }
10247 return;
10248 case PC_BOUNDS_INVALID:
10249 return;
10250 }
10251 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10252 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10253
10254 cu->get_builder ()->push_context (0, lowpc);
10255 if (die->child != NULL)
10256 {
10257 child_die = die->child;
10258 while (child_die && child_die->tag)
10259 {
10260 process_die (child_die, cu);
10261 child_die = child_die->sibling;
10262 }
10263 }
10264 inherit_abstract_dies (die, cu);
10265 struct context_stack cstk = cu->get_builder ()->pop_context ();
10266
10267 if (*cu->get_builder ()->get_local_symbols () != NULL
10268 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
10269 {
10270 struct block *block
10271 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
10272 cstk.start_addr, highpc);
10273
10274 /* Note that recording ranges after traversing children, as we
10275 do here, means that recording a parent's ranges entails
10276 walking across all its children's ranges as they appear in
10277 the address map, which is quadratic behavior.
10278
10279 It would be nicer to record the parent's ranges before
10280 traversing its children, simply overriding whatever you find
10281 there. But since we don't even decide whether to create a
10282 block until after we've traversed its children, that's hard
10283 to do. */
10284 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10285 }
10286 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10287 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10288 }
10289
10290 static void dwarf2_ranges_read_low_addrs (unsigned offset,
10291 struct dwarf2_cu *cu,
10292 dwarf_tag tag,
10293 std::vector<CORE_ADDR> &result);
10294
10295 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
10296
10297 static void
10298 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10299 {
10300 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10301 struct objfile *objfile = per_objfile->objfile;
10302 struct gdbarch *gdbarch = objfile->arch ();
10303 CORE_ADDR pc, baseaddr;
10304 struct attribute *attr;
10305 void **slot;
10306 int nparams;
10307 struct die_info *child_die;
10308
10309 baseaddr = objfile->text_section_offset ();
10310
10311 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
10312 if (attr == NULL)
10313 {
10314 /* This was a pre-DWARF-5 GNU extension alias
10315 for DW_AT_call_return_pc. */
10316 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10317 }
10318 if (!attr)
10319 {
10320 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
10321 "DIE %s [in module %s]"),
10322 sect_offset_str (die->sect_off), objfile_name (objfile));
10323 return;
10324 }
10325 pc = attr->as_address () + baseaddr;
10326 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
10327 pc -= baseaddr;
10328
10329 if (cu->call_site_htab == NULL)
10330 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
10331 call_site::eq, NULL,
10332 &objfile->objfile_obstack,
10333 hashtab_obstack_allocate, NULL);
10334 struct call_site call_site_local (pc, nullptr, nullptr);
10335 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10336 if (*slot != NULL)
10337 {
10338 complaint (_("Duplicate PC %s for DW_TAG_call_site "
10339 "DIE %s [in module %s]"),
10340 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
10341 objfile_name (objfile));
10342 return;
10343 }
10344
10345 /* Count parameters at the caller. */
10346
10347 nparams = 0;
10348 for (child_die = die->child; child_die && child_die->tag;
10349 child_die = child_die->sibling)
10350 {
10351 if (child_die->tag != DW_TAG_call_site_parameter
10352 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10353 {
10354 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
10355 "DW_TAG_call_site child DIE %s [in module %s]"),
10356 child_die->tag, sect_offset_str (child_die->sect_off),
10357 objfile_name (objfile));
10358 continue;
10359 }
10360
10361 nparams++;
10362 }
10363
10364 struct call_site *call_site
10365 = new (XOBNEWVAR (&objfile->objfile_obstack,
10366 struct call_site,
10367 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
10368 struct call_site (pc, cu->per_cu, per_objfile);
10369 *slot = call_site;
10370
10371 /* We never call the destructor of call_site, so we must ensure it is
10372 trivially destructible. */
10373 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
10374
10375 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
10376 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10377 {
10378 struct die_info *func_die;
10379
10380 /* Skip also over DW_TAG_inlined_subroutine. */
10381 for (func_die = die->parent;
10382 func_die && func_die->tag != DW_TAG_subprogram
10383 && func_die->tag != DW_TAG_subroutine_type;
10384 func_die = func_die->parent);
10385
10386 /* DW_AT_call_all_calls is a superset
10387 of DW_AT_call_all_tail_calls. */
10388 if (func_die
10389 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
10390 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10391 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
10392 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10393 {
10394 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10395 not complete. But keep CALL_SITE for look ups via call_site_htab,
10396 both the initial caller containing the real return address PC and
10397 the final callee containing the current PC of a chain of tail
10398 calls do not need to have the tail call list complete. But any
10399 function candidate for a virtual tail call frame searched via
10400 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10401 determined unambiguously. */
10402 }
10403 else
10404 {
10405 struct type *func_type = NULL;
10406
10407 if (func_die)
10408 func_type = get_die_type (func_die, cu);
10409 if (func_type != NULL)
10410 {
10411 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
10412
10413 /* Enlist this call site to the function. */
10414 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10415 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10416 }
10417 else
10418 complaint (_("Cannot find function owning DW_TAG_call_site "
10419 "DIE %s [in module %s]"),
10420 sect_offset_str (die->sect_off), objfile_name (objfile));
10421 }
10422 }
10423
10424 attr = dwarf2_attr (die, DW_AT_call_target, cu);
10425 if (attr == NULL)
10426 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10427 if (attr == NULL)
10428 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
10429 if (attr == NULL)
10430 {
10431 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
10432 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10433 }
10434
10435 call_site->target.set_loc_dwarf_block (nullptr);
10436 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
10437 /* Keep NULL DWARF_BLOCK. */;
10438 else if (attr->form_is_block ())
10439 {
10440 struct dwarf2_locexpr_baton *dlbaton;
10441 struct dwarf_block *block = attr->as_block ();
10442
10443 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
10444 dlbaton->data = block->data;
10445 dlbaton->size = block->size;
10446 dlbaton->per_objfile = per_objfile;
10447 dlbaton->per_cu = cu->per_cu;
10448
10449 call_site->target.set_loc_dwarf_block (dlbaton);
10450 }
10451 else if (attr->form_is_ref ())
10452 {
10453 struct dwarf2_cu *target_cu = cu;
10454 struct die_info *target_die;
10455
10456 target_die = follow_die_ref (die, attr, &target_cu);
10457 gdb_assert (target_cu->per_objfile->objfile == objfile);
10458
10459 struct attribute *ranges_attr
10460 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
10461
10462 if (die_is_declaration (target_die, target_cu))
10463 {
10464 const char *target_physname;
10465
10466 /* Prefer the mangled name; otherwise compute the demangled one. */
10467 target_physname = dw2_linkage_name (target_die, target_cu);
10468 if (target_physname == NULL)
10469 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10470 if (target_physname == NULL)
10471 complaint (_("DW_AT_call_target target DIE has invalid "
10472 "physname, for referencing DIE %s [in module %s]"),
10473 sect_offset_str (die->sect_off), objfile_name (objfile));
10474 else
10475 call_site->target.set_loc_physname (target_physname);
10476 }
10477 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
10478 {
10479 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
10480 + target_cu->gnu_ranges_base);
10481 std::vector<CORE_ADDR> addresses;
10482 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
10483 target_die->tag, addresses);
10484 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
10485 addresses.size ());
10486 std::copy (addresses.begin (), addresses.end (), saved);
10487 call_site->target.set_loc_array (addresses.size (), saved);
10488 }
10489 else
10490 {
10491 CORE_ADDR lowpc;
10492
10493 /* DW_AT_entry_pc should be preferred. */
10494 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
10495 nullptr, nullptr)
10496 <= PC_BOUNDS_INVALID)
10497 complaint (_("DW_AT_call_target target DIE has invalid "
10498 "low pc, for referencing DIE %s [in module %s]"),
10499 sect_offset_str (die->sect_off), objfile_name (objfile));
10500 else
10501 {
10502 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
10503 - baseaddr);
10504 call_site->target.set_loc_physaddr (lowpc);
10505 }
10506 }
10507 }
10508 else
10509 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
10510 "block nor reference, for DIE %s [in module %s]"),
10511 sect_offset_str (die->sect_off), objfile_name (objfile));
10512
10513 for (child_die = die->child;
10514 child_die && child_die->tag;
10515 child_die = child_die->sibling)
10516 {
10517 struct call_site_parameter *parameter;
10518 struct attribute *loc, *origin;
10519
10520 if (child_die->tag != DW_TAG_call_site_parameter
10521 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10522 {
10523 /* Already printed the complaint above. */
10524 continue;
10525 }
10526
10527 gdb_assert (call_site->parameter_count < nparams);
10528 parameter = &call_site->parameter[call_site->parameter_count];
10529
10530 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10531 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10532 register is contained in DW_AT_call_value. */
10533
10534 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10535 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
10536 if (origin == NULL)
10537 {
10538 /* This was a pre-DWARF-5 GNU extension alias
10539 for DW_AT_call_parameter. */
10540 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10541 }
10542 if (loc == NULL && origin != NULL && origin->form_is_ref ())
10543 {
10544 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10545
10546 sect_offset sect_off = origin->get_ref_die_offset ();
10547 if (!cu->header.offset_in_cu_p (sect_off))
10548 {
10549 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10550 binding can be done only inside one CU. Such referenced DIE
10551 therefore cannot be even moved to DW_TAG_partial_unit. */
10552 complaint (_("DW_AT_call_parameter offset is not in CU for "
10553 "DW_TAG_call_site child DIE %s [in module %s]"),
10554 sect_offset_str (child_die->sect_off),
10555 objfile_name (objfile));
10556 continue;
10557 }
10558 parameter->u.param_cu_off
10559 = (cu_offset) (sect_off - cu->header.sect_off);
10560 }
10561 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
10562 {
10563 complaint (_("No DW_FORM_block* DW_AT_location for "
10564 "DW_TAG_call_site child DIE %s [in module %s]"),
10565 sect_offset_str (child_die->sect_off), objfile_name (objfile));
10566 continue;
10567 }
10568 else
10569 {
10570 struct dwarf_block *block = loc->as_block ();
10571
10572 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10573 (block->data, &block->data[block->size]);
10574 if (parameter->u.dwarf_reg != -1)
10575 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10576 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
10577 &block->data[block->size],
10578 &parameter->u.fb_offset))
10579 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10580 else
10581 {
10582 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
10583 "for DW_FORM_block* DW_AT_location is supported for "
10584 "DW_TAG_call_site child DIE %s "
10585 "[in module %s]"),
10586 sect_offset_str (child_die->sect_off),
10587 objfile_name (objfile));
10588 continue;
10589 }
10590 }
10591
10592 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
10593 if (attr == NULL)
10594 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10595 if (attr == NULL || !attr->form_is_block ())
10596 {
10597 complaint (_("No DW_FORM_block* DW_AT_call_value for "
10598 "DW_TAG_call_site child DIE %s [in module %s]"),
10599 sect_offset_str (child_die->sect_off),
10600 objfile_name (objfile));
10601 continue;
10602 }
10603
10604 struct dwarf_block *block = attr->as_block ();
10605 parameter->value = block->data;
10606 parameter->value_size = block->size;
10607
10608 /* Parameters are not pre-cleared by memset above. */
10609 parameter->data_value = NULL;
10610 parameter->data_value_size = 0;
10611 call_site->parameter_count++;
10612
10613 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
10614 if (attr == NULL)
10615 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10616 if (attr != nullptr)
10617 {
10618 if (!attr->form_is_block ())
10619 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
10620 "DW_TAG_call_site child DIE %s [in module %s]"),
10621 sect_offset_str (child_die->sect_off),
10622 objfile_name (objfile));
10623 else
10624 {
10625 block = attr->as_block ();
10626 parameter->data_value = block->data;
10627 parameter->data_value_size = block->size;
10628 }
10629 }
10630 }
10631 }
10632
10633 /* Helper function for read_variable. If DIE represents a virtual
10634 table, then return the type of the concrete object that is
10635 associated with the virtual table. Otherwise, return NULL. */
10636
10637 static struct type *
10638 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10639 {
10640 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
10641 if (attr == NULL)
10642 return NULL;
10643
10644 /* Find the type DIE. */
10645 struct die_info *type_die = NULL;
10646 struct dwarf2_cu *type_cu = cu;
10647
10648 if (attr->form_is_ref ())
10649 type_die = follow_die_ref (die, attr, &type_cu);
10650 if (type_die == NULL)
10651 return NULL;
10652
10653 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
10654 return NULL;
10655 return die_containing_type (type_die, type_cu);
10656 }
10657
10658 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
10659
10660 static void
10661 read_variable (struct die_info *die, struct dwarf2_cu *cu)
10662 {
10663 struct rust_vtable_symbol *storage = NULL;
10664
10665 if (cu->lang () == language_rust)
10666 {
10667 struct type *containing_type = rust_containing_type (die, cu);
10668
10669 if (containing_type != NULL)
10670 {
10671 struct objfile *objfile = cu->per_objfile->objfile;
10672
10673 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
10674 storage->concrete_type = containing_type;
10675 storage->subclass = SYMBOL_RUST_VTABLE;
10676 }
10677 }
10678
10679 struct symbol *res = new_symbol (die, NULL, cu, storage);
10680 struct attribute *abstract_origin
10681 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10682 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
10683 if (res == NULL && loc && abstract_origin)
10684 {
10685 /* We have a variable without a name, but with a location and an abstract
10686 origin. This may be a concrete instance of an abstract variable
10687 referenced from an DW_OP_GNU_variable_value, so save it to find it back
10688 later. */
10689 struct dwarf2_cu *origin_cu = cu;
10690 struct die_info *origin_die
10691 = follow_die_ref (die, abstract_origin, &origin_cu);
10692 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10693 per_objfile->per_bfd->abstract_to_concrete
10694 [origin_die->sect_off].push_back (die->sect_off);
10695 }
10696 }
10697
10698 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
10699 reading .debug_rnglists.
10700 Callback's type should be:
10701 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10702 Return true if the attributes are present and valid, otherwise,
10703 return false. */
10704
10705 template <typename Callback>
10706 static bool
10707 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
10708 dwarf_tag tag, Callback &&callback)
10709 {
10710 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10711 struct objfile *objfile = per_objfile->objfile;
10712 bfd *obfd = objfile->obfd.get ();
10713 /* Base address selection entry. */
10714 gdb::optional<CORE_ADDR> base;
10715 const gdb_byte *buffer;
10716 bool overflow = false;
10717 ULONGEST addr_index;
10718 struct dwarf2_section_info *rnglists_section;
10719
10720 base = cu->base_address;
10721 rnglists_section = cu_debug_rnglists_section (cu, tag);
10722 rnglists_section->read (objfile);
10723
10724 if (offset >= rnglists_section->size)
10725 {
10726 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10727 offset);
10728 return false;
10729 }
10730 buffer = rnglists_section->buffer + offset;
10731
10732 while (1)
10733 {
10734 /* Initialize it due to a false compiler warning. */
10735 CORE_ADDR range_beginning = 0, range_end = 0;
10736 const gdb_byte *buf_end = (rnglists_section->buffer
10737 + rnglists_section->size);
10738 unsigned int bytes_read;
10739
10740 if (buffer == buf_end)
10741 {
10742 overflow = true;
10743 break;
10744 }
10745 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
10746 switch (rlet)
10747 {
10748 case DW_RLE_end_of_list:
10749 break;
10750 case DW_RLE_base_address:
10751 if (buffer + cu->header.addr_size > buf_end)
10752 {
10753 overflow = true;
10754 break;
10755 }
10756 base = cu->header.read_address (obfd, buffer, &bytes_read);
10757 buffer += bytes_read;
10758 break;
10759 case DW_RLE_base_addressx:
10760 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10761 buffer += bytes_read;
10762 base = read_addr_index (cu, addr_index);
10763 break;
10764 case DW_RLE_start_length:
10765 if (buffer + cu->header.addr_size > buf_end)
10766 {
10767 overflow = true;
10768 break;
10769 }
10770 range_beginning = cu->header.read_address (obfd, buffer,
10771 &bytes_read);
10772 buffer += bytes_read;
10773 range_end = (range_beginning
10774 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
10775 buffer += bytes_read;
10776 if (buffer > buf_end)
10777 {
10778 overflow = true;
10779 break;
10780 }
10781 break;
10782 case DW_RLE_startx_length:
10783 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10784 buffer += bytes_read;
10785 range_beginning = read_addr_index (cu, addr_index);
10786 if (buffer > buf_end)
10787 {
10788 overflow = true;
10789 break;
10790 }
10791 range_end = (range_beginning
10792 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
10793 buffer += bytes_read;
10794 break;
10795 case DW_RLE_offset_pair:
10796 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10797 buffer += bytes_read;
10798 if (buffer > buf_end)
10799 {
10800 overflow = true;
10801 break;
10802 }
10803 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10804 buffer += bytes_read;
10805 if (buffer > buf_end)
10806 {
10807 overflow = true;
10808 break;
10809 }
10810 break;
10811 case DW_RLE_start_end:
10812 if (buffer + 2 * cu->header.addr_size > buf_end)
10813 {
10814 overflow = true;
10815 break;
10816 }
10817 range_beginning = cu->header.read_address (obfd, buffer,
10818 &bytes_read);
10819 buffer += bytes_read;
10820 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
10821 buffer += bytes_read;
10822 break;
10823 case DW_RLE_startx_endx:
10824 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10825 buffer += bytes_read;
10826 range_beginning = read_addr_index (cu, addr_index);
10827 if (buffer > buf_end)
10828 {
10829 overflow = true;
10830 break;
10831 }
10832 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10833 buffer += bytes_read;
10834 range_end = read_addr_index (cu, addr_index);
10835 break;
10836 default:
10837 complaint (_("Invalid .debug_rnglists data (no base address)"));
10838 return false;
10839 }
10840 if (rlet == DW_RLE_end_of_list || overflow)
10841 break;
10842 if (rlet == DW_RLE_base_address)
10843 continue;
10844
10845 if (range_beginning > range_end)
10846 {
10847 /* Inverted range entries are invalid. */
10848 complaint (_("Invalid .debug_rnglists data (inverted range)"));
10849 return false;
10850 }
10851
10852 /* Empty range entries have no effect. */
10853 if (range_beginning == range_end)
10854 continue;
10855
10856 /* Only DW_RLE_offset_pair needs the base address added. */
10857 if (rlet == DW_RLE_offset_pair)
10858 {
10859 if (!base.has_value ())
10860 {
10861 /* We have no valid base address for the DW_RLE_offset_pair. */
10862 complaint (_("Invalid .debug_rnglists data (no base address for "
10863 "DW_RLE_offset_pair)"));
10864 return false;
10865 }
10866
10867 range_beginning += *base;
10868 range_end += *base;
10869 }
10870
10871 /* A not-uncommon case of bad debug info.
10872 Don't pollute the addrmap with bad data. */
10873 if (range_beginning == 0
10874 && !per_objfile->per_bfd->has_section_at_zero)
10875 {
10876 complaint (_(".debug_rnglists entry has start address of zero"
10877 " [in module %s]"), objfile_name (objfile));
10878 continue;
10879 }
10880
10881 callback (range_beginning, range_end);
10882 }
10883
10884 if (overflow)
10885 {
10886 complaint (_("Offset %d is not terminated "
10887 "for DW_AT_ranges attribute"),
10888 offset);
10889 return false;
10890 }
10891
10892 return true;
10893 }
10894
10895 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
10896 Callback's type should be:
10897 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10898 Return 1 if the attributes are present and valid, otherwise, return 0. */
10899
10900 template <typename Callback>
10901 static int
10902 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
10903 Callback &&callback)
10904 {
10905 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10906 struct objfile *objfile = per_objfile->objfile;
10907 struct comp_unit_head *cu_header = &cu->header;
10908 bfd *obfd = objfile->obfd.get ();
10909 unsigned int addr_size = cu_header->addr_size;
10910 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10911 /* Base address selection entry. */
10912 gdb::optional<CORE_ADDR> base;
10913 unsigned int dummy;
10914 const gdb_byte *buffer;
10915
10916 if (cu_header->version >= 5)
10917 return dwarf2_rnglists_process (offset, cu, tag, callback);
10918
10919 base = cu->base_address;
10920
10921 per_objfile->per_bfd->ranges.read (objfile);
10922 if (offset >= per_objfile->per_bfd->ranges.size)
10923 {
10924 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10925 offset);
10926 return 0;
10927 }
10928 buffer = per_objfile->per_bfd->ranges.buffer + offset;
10929
10930 while (1)
10931 {
10932 CORE_ADDR range_beginning, range_end;
10933
10934 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
10935 buffer += addr_size;
10936 range_end = cu->header.read_address (obfd, buffer, &dummy);
10937 buffer += addr_size;
10938 offset += 2 * addr_size;
10939
10940 /* An end of list marker is a pair of zero addresses. */
10941 if (range_beginning == 0 && range_end == 0)
10942 /* Found the end of list entry. */
10943 break;
10944
10945 /* Each base address selection entry is a pair of 2 values.
10946 The first is the largest possible address, the second is
10947 the base address. Check for a base address here. */
10948 if ((range_beginning & mask) == mask)
10949 {
10950 /* If we found the largest possible address, then we already
10951 have the base address in range_end. */
10952 base = range_end;
10953 continue;
10954 }
10955
10956 if (!base.has_value ())
10957 {
10958 /* We have no valid base address for the ranges
10959 data. */
10960 complaint (_("Invalid .debug_ranges data (no base address)"));
10961 return 0;
10962 }
10963
10964 if (range_beginning > range_end)
10965 {
10966 /* Inverted range entries are invalid. */
10967 complaint (_("Invalid .debug_ranges data (inverted range)"));
10968 return 0;
10969 }
10970
10971 /* Empty range entries have no effect. */
10972 if (range_beginning == range_end)
10973 continue;
10974
10975 range_beginning += *base;
10976 range_end += *base;
10977
10978 /* A not-uncommon case of bad debug info.
10979 Don't pollute the addrmap with bad data. */
10980 if (range_beginning == 0
10981 && !per_objfile->per_bfd->has_section_at_zero)
10982 {
10983 complaint (_(".debug_ranges entry has start address of zero"
10984 " [in module %s]"), objfile_name (objfile));
10985 continue;
10986 }
10987
10988 callback (range_beginning, range_end);
10989 }
10990
10991 return 1;
10992 }
10993
10994 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10995 Return 1 if the attributes are present and valid, otherwise, return 0.
10996 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
10997 ranges in MAP are set, using DATUM as the value. */
10998
10999 static int
11000 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
11001 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11002 addrmap *map, void *datum, dwarf_tag tag)
11003 {
11004 struct objfile *objfile = cu->per_objfile->objfile;
11005 struct gdbarch *gdbarch = objfile->arch ();
11006 const CORE_ADDR baseaddr = objfile->text_section_offset ();
11007 int low_set = 0;
11008 CORE_ADDR low = 0;
11009 CORE_ADDR high = 0;
11010 int retval;
11011
11012 retval = dwarf2_ranges_process (offset, cu, tag,
11013 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
11014 {
11015 if (map != nullptr)
11016 {
11017 CORE_ADDR lowpc;
11018 CORE_ADDR highpc;
11019
11020 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
11021 range_beginning + baseaddr)
11022 - baseaddr);
11023 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
11024 range_end + baseaddr)
11025 - baseaddr);
11026 map->set_empty (lowpc, highpc - 1, datum);
11027 }
11028
11029 /* FIXME: This is recording everything as a low-high
11030 segment of consecutive addresses. We should have a
11031 data structure for discontiguous block ranges
11032 instead. */
11033 if (! low_set)
11034 {
11035 low = range_beginning;
11036 high = range_end;
11037 low_set = 1;
11038 }
11039 else
11040 {
11041 if (range_beginning < low)
11042 low = range_beginning;
11043 if (range_end > high)
11044 high = range_end;
11045 }
11046 });
11047 if (!retval)
11048 return 0;
11049
11050 if (! low_set)
11051 /* If the first entry is an end-of-list marker, the range
11052 describes an empty scope, i.e. no instructions. */
11053 return 0;
11054
11055 if (low_return)
11056 *low_return = low;
11057 if (high_return)
11058 *high_return = high;
11059 return 1;
11060 }
11061
11062 /* Process ranges and fill in a vector of the low PC values only. */
11063
11064 static void
11065 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
11066 dwarf_tag tag,
11067 std::vector<CORE_ADDR> &result)
11068 {
11069 dwarf2_ranges_process (offset, cu, tag,
11070 [&] (CORE_ADDR start, CORE_ADDR end)
11071 {
11072 result.push_back (start);
11073 });
11074 }
11075
11076 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
11077 definition for the return value. *LOWPC and *HIGHPC are set iff
11078 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
11079
11080 static enum pc_bounds_kind
11081 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
11082 CORE_ADDR *highpc, struct dwarf2_cu *cu,
11083 addrmap *map, void *datum)
11084 {
11085 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11086 struct attribute *attr;
11087 struct attribute *attr_high;
11088 CORE_ADDR low = 0;
11089 CORE_ADDR high = 0;
11090 enum pc_bounds_kind ret;
11091
11092 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11093 if (attr_high)
11094 {
11095 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11096 if (attr != nullptr)
11097 {
11098 low = attr->as_address ();
11099 high = attr_high->as_address ();
11100 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11101 high += low;
11102 }
11103 else
11104 /* Found high w/o low attribute. */
11105 return PC_BOUNDS_INVALID;
11106
11107 /* Found consecutive range of addresses. */
11108 ret = PC_BOUNDS_HIGH_LOW;
11109 }
11110 else
11111 {
11112 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11113 if (attr != nullptr && attr->form_is_unsigned ())
11114 {
11115 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11116 on DWARF version). */
11117 ULONGEST ranges_offset = attr->as_unsigned ();
11118
11119 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11120 this value. */
11121 if (die->tag != DW_TAG_compile_unit)
11122 ranges_offset += cu->gnu_ranges_base;
11123
11124 /* Value of the DW_AT_ranges attribute is the offset in the
11125 .debug_ranges section. */
11126 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
11127 map, datum, die->tag))
11128 return PC_BOUNDS_INVALID;
11129 /* Found discontinuous range of addresses. */
11130 ret = PC_BOUNDS_RANGES;
11131 }
11132 else
11133 return PC_BOUNDS_NOT_PRESENT;
11134 }
11135
11136 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
11137 if (high <= low)
11138 return PC_BOUNDS_INVALID;
11139
11140 /* When using the GNU linker, .gnu.linkonce. sections are used to
11141 eliminate duplicate copies of functions and vtables and such.
11142 The linker will arbitrarily choose one and discard the others.
11143 The AT_*_pc values for such functions refer to local labels in
11144 these sections. If the section from that file was discarded, the
11145 labels are not in the output, so the relocs get a value of 0.
11146 If this is a discarded function, mark the pc bounds as invalid,
11147 so that GDB will ignore it. */
11148 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
11149 return PC_BOUNDS_INVALID;
11150
11151 *lowpc = low;
11152 if (highpc)
11153 *highpc = high;
11154 return ret;
11155 }
11156
11157 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
11158 its low and high PC addresses. Do nothing if these addresses could not
11159 be determined. Otherwise, set LOWPC to the low address if it is smaller,
11160 and HIGHPC to the high address if greater than HIGHPC. */
11161
11162 static void
11163 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
11164 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11165 struct dwarf2_cu *cu)
11166 {
11167 CORE_ADDR low, high;
11168 struct die_info *child = die->child;
11169
11170 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
11171 >= PC_BOUNDS_RANGES)
11172 {
11173 *lowpc = std::min (*lowpc, low);
11174 *highpc = std::max (*highpc, high);
11175 }
11176
11177 /* If the language does not allow nested subprograms (either inside
11178 subprograms or lexical blocks), we're done. */
11179 if (cu->lang () != language_ada)
11180 return;
11181
11182 /* Check all the children of the given DIE. If it contains nested
11183 subprograms, then check their pc bounds. Likewise, we need to
11184 check lexical blocks as well, as they may also contain subprogram
11185 definitions. */
11186 while (child && child->tag)
11187 {
11188 if (child->tag == DW_TAG_subprogram
11189 || child->tag == DW_TAG_lexical_block)
11190 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
11191 child = child->sibling;
11192 }
11193 }
11194
11195 /* Get the low and high pc's represented by the scope DIE, and store
11196 them in *LOWPC and *HIGHPC. If the correct values can't be
11197 determined, set *LOWPC to -1 and *HIGHPC to 0. */
11198
11199 static void
11200 get_scope_pc_bounds (struct die_info *die,
11201 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11202 struct dwarf2_cu *cu)
11203 {
11204 CORE_ADDR best_low = (CORE_ADDR) -1;
11205 CORE_ADDR best_high = (CORE_ADDR) 0;
11206 CORE_ADDR current_low, current_high;
11207
11208 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
11209 nullptr, nullptr)
11210 >= PC_BOUNDS_RANGES)
11211 {
11212 best_low = current_low;
11213 best_high = current_high;
11214 }
11215 else
11216 {
11217 struct die_info *child = die->child;
11218
11219 while (child && child->tag)
11220 {
11221 switch (child->tag) {
11222 case DW_TAG_subprogram:
11223 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
11224 break;
11225 case DW_TAG_namespace:
11226 case DW_TAG_module:
11227 /* FIXME: carlton/2004-01-16: Should we do this for
11228 DW_TAG_class_type/DW_TAG_structure_type, too? I think
11229 that current GCC's always emit the DIEs corresponding
11230 to definitions of methods of classes as children of a
11231 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11232 the DIEs giving the declarations, which could be
11233 anywhere). But I don't see any reason why the
11234 standards says that they have to be there. */
11235 get_scope_pc_bounds (child, &current_low, &current_high, cu);
11236
11237 if (current_low != ((CORE_ADDR) -1))
11238 {
11239 best_low = std::min (best_low, current_low);
11240 best_high = std::max (best_high, current_high);
11241 }
11242 break;
11243 default:
11244 /* Ignore. */
11245 break;
11246 }
11247
11248 child = child->sibling;
11249 }
11250 }
11251
11252 *lowpc = best_low;
11253 *highpc = best_high;
11254 }
11255
11256 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
11257 in DIE. */
11258
11259 static void
11260 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
11261 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
11262 {
11263 struct objfile *objfile = cu->per_objfile->objfile;
11264 struct gdbarch *gdbarch = objfile->arch ();
11265 struct attribute *attr;
11266 struct attribute *attr_high;
11267
11268 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11269 if (attr_high)
11270 {
11271 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11272 if (attr != nullptr)
11273 {
11274 CORE_ADDR low = attr->as_address ();
11275 CORE_ADDR high = attr_high->as_address ();
11276
11277 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11278 high += low;
11279
11280 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
11281 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
11282 cu->get_builder ()->record_block_range (block, low, high - 1);
11283 }
11284 }
11285
11286 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11287 if (attr != nullptr && attr->form_is_unsigned ())
11288 {
11289 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11290 on DWARF version). */
11291 ULONGEST ranges_offset = attr->as_unsigned ();
11292
11293 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11294 this value. */
11295 if (die->tag != DW_TAG_compile_unit)
11296 ranges_offset += cu->gnu_ranges_base;
11297
11298 std::vector<blockrange> blockvec;
11299 dwarf2_ranges_process (ranges_offset, cu, die->tag,
11300 [&] (CORE_ADDR start, CORE_ADDR end)
11301 {
11302 start += baseaddr;
11303 end += baseaddr;
11304 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
11305 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
11306 cu->get_builder ()->record_block_range (block, start, end - 1);
11307 blockvec.emplace_back (start, end);
11308 });
11309
11310 block->set_ranges (make_blockranges (objfile, blockvec));
11311 }
11312 }
11313
11314 /* Check whether the producer field indicates either of GCC < 4.6, or the
11315 Intel C/C++ compiler, and cache the result in CU. */
11316
11317 static void
11318 check_producer (struct dwarf2_cu *cu)
11319 {
11320 int major, minor;
11321
11322 if (cu->producer == NULL)
11323 {
11324 /* For unknown compilers expect their behavior is DWARF version
11325 compliant.
11326
11327 GCC started to support .debug_types sections by -gdwarf-4 since
11328 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11329 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11330 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11331 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11332 }
11333 else if (producer_is_gcc (cu->producer, &major, &minor))
11334 {
11335 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11336 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11337 cu->producer_is_gcc_11 = major == 11;
11338 }
11339 else if (producer_is_icc (cu->producer, &major, &minor))
11340 {
11341 cu->producer_is_icc = true;
11342 cu->producer_is_icc_lt_14 = major < 14;
11343 }
11344 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
11345 cu->producer_is_codewarrior = true;
11346 else if (producer_is_clang (cu->producer, &major, &minor))
11347 cu->producer_is_clang = true;
11348 else
11349 {
11350 /* For other non-GCC compilers, expect their behavior is DWARF version
11351 compliant. */
11352 }
11353
11354 cu->checked_producer = true;
11355 }
11356
11357 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11358 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11359 during 4.6.0 experimental. */
11360
11361 static bool
11362 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11363 {
11364 if (!cu->checked_producer)
11365 check_producer (cu);
11366
11367 return cu->producer_is_gxx_lt_4_6;
11368 }
11369
11370
11371 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
11372 with incorrect is_stmt attributes. */
11373
11374 static bool
11375 producer_is_codewarrior (struct dwarf2_cu *cu)
11376 {
11377 if (!cu->checked_producer)
11378 check_producer (cu);
11379
11380 return cu->producer_is_codewarrior;
11381 }
11382
11383 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
11384 If that attribute is not available, return the appropriate
11385 default. */
11386
11387 static enum dwarf_access_attribute
11388 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11389 {
11390 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11391 if (attr != nullptr)
11392 {
11393 LONGEST value = attr->constant_value (-1);
11394 if (value == DW_ACCESS_public
11395 || value == DW_ACCESS_protected
11396 || value == DW_ACCESS_private)
11397 return (dwarf_access_attribute) value;
11398 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
11399 plongest (value));
11400 }
11401
11402 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11403 {
11404 /* The default DWARF 2 accessibility for members is public, the default
11405 accessibility for inheritance is private. */
11406
11407 if (die->tag != DW_TAG_inheritance)
11408 return DW_ACCESS_public;
11409 else
11410 return DW_ACCESS_private;
11411 }
11412 else
11413 {
11414 /* DWARF 3+ defines the default accessibility a different way. The same
11415 rules apply now for DW_TAG_inheritance as for the members and it only
11416 depends on the container kind. */
11417
11418 if (die->parent->tag == DW_TAG_class_type)
11419 return DW_ACCESS_private;
11420 else
11421 return DW_ACCESS_public;
11422 }
11423 }
11424
11425 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
11426 *OFFSET to the byte offset. If the attribute was not found return
11427 0, otherwise return 1. If it was found but could not properly be
11428 handled, set *OFFSET to 0. */
11429
11430 static int
11431 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11432 LONGEST *offset)
11433 {
11434 struct attribute *attr;
11435
11436 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11437 if (attr != NULL)
11438 {
11439 *offset = 0;
11440
11441 /* Note that we do not check for a section offset first here.
11442 This is because DW_AT_data_member_location is new in DWARF 4,
11443 so if we see it, we can assume that a constant form is really
11444 a constant and not a section offset. */
11445 if (attr->form_is_constant ())
11446 *offset = attr->constant_value (0);
11447 else if (attr->form_is_section_offset ())
11448 dwarf2_complex_location_expr_complaint ();
11449 else if (attr->form_is_block ())
11450 *offset = decode_locdesc (attr->as_block (), cu);
11451 else
11452 dwarf2_complex_location_expr_complaint ();
11453
11454 return 1;
11455 }
11456 else
11457 {
11458 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11459 if (attr != nullptr)
11460 {
11461 *offset = attr->constant_value (0);
11462 return 1;
11463 }
11464 }
11465
11466 return 0;
11467 }
11468
11469 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
11470 store the results in FIELD. */
11471
11472 static void
11473 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11474 struct field *field)
11475 {
11476 struct attribute *attr;
11477
11478 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11479 if (attr != NULL)
11480 {
11481 if (attr->form_is_constant ())
11482 {
11483 LONGEST offset = attr->constant_value (0);
11484
11485 /* Work around this GCC 11 bug, where it would erroneously use -1
11486 data member locations, instead of 0:
11487
11488 Negative DW_AT_data_member_location
11489 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
11490 */
11491 if (offset == -1 && cu->producer_is_gcc_11)
11492 {
11493 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
11494 offset = 0;
11495 }
11496
11497 field->set_loc_bitpos (offset * bits_per_byte);
11498 }
11499 else if (attr->form_is_section_offset ())
11500 dwarf2_complex_location_expr_complaint ();
11501 else if (attr->form_is_block ())
11502 {
11503 bool handled;
11504 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
11505 if (handled)
11506 field->set_loc_bitpos (offset * bits_per_byte);
11507 else
11508 {
11509 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11510 struct objfile *objfile = per_objfile->objfile;
11511 struct dwarf2_locexpr_baton *dlbaton
11512 = XOBNEW (&objfile->objfile_obstack,
11513 struct dwarf2_locexpr_baton);
11514 dlbaton->data = attr->as_block ()->data;
11515 dlbaton->size = attr->as_block ()->size;
11516 /* When using this baton, we want to compute the address
11517 of the field, not the value. This is why
11518 is_reference is set to false here. */
11519 dlbaton->is_reference = false;
11520 dlbaton->per_objfile = per_objfile;
11521 dlbaton->per_cu = cu->per_cu;
11522
11523 field->set_loc_dwarf_block (dlbaton);
11524 }
11525 }
11526 else
11527 dwarf2_complex_location_expr_complaint ();
11528 }
11529 else
11530 {
11531 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11532 if (attr != nullptr)
11533 field->set_loc_bitpos (attr->constant_value (0));
11534 }
11535 }
11536
11537 /* Add an aggregate field to the field list. */
11538
11539 static void
11540 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11541 struct dwarf2_cu *cu)
11542 {
11543 struct objfile *objfile = cu->per_objfile->objfile;
11544 struct gdbarch *gdbarch = objfile->arch ();
11545 struct nextfield *new_field;
11546 struct attribute *attr;
11547 struct field *fp;
11548 const char *fieldname = "";
11549
11550 if (die->tag == DW_TAG_inheritance)
11551 {
11552 fip->baseclasses.emplace_back ();
11553 new_field = &fip->baseclasses.back ();
11554 }
11555 else
11556 {
11557 fip->fields.emplace_back ();
11558 new_field = &fip->fields.back ();
11559 }
11560
11561 new_field->offset = die->sect_off;
11562
11563 new_field->accessibility = dwarf2_access_attribute (die, cu);
11564 if (new_field->accessibility != DW_ACCESS_public)
11565 fip->non_public_fields = true;
11566
11567 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11568 if (attr != nullptr)
11569 new_field->virtuality = attr->as_virtuality ();
11570 else
11571 new_field->virtuality = DW_VIRTUALITY_none;
11572
11573 fp = &new_field->field;
11574
11575 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
11576 && !die_is_declaration (die, cu))
11577 {
11578 if (die->tag == DW_TAG_namelist_item)
11579 {
11580 /* Typically, DW_TAG_namelist_item are references to namelist items.
11581 If so, follow that reference. */
11582 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
11583 struct die_info *item_die = nullptr;
11584 struct dwarf2_cu *item_cu = cu;
11585 if (attr1->form_is_ref ())
11586 item_die = follow_die_ref (die, attr1, &item_cu);
11587 if (item_die != nullptr)
11588 die = item_die;
11589 }
11590 /* Data member other than a C++ static data member. */
11591
11592 /* Get type of field. */
11593 fp->set_type (die_type (die, cu));
11594
11595 fp->set_loc_bitpos (0);
11596
11597 /* Get bit size of field (zero if none). */
11598 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11599 if (attr != nullptr)
11600 {
11601 FIELD_BITSIZE (*fp) = attr->constant_value (0);
11602 }
11603 else
11604 {
11605 FIELD_BITSIZE (*fp) = 0;
11606 }
11607
11608 /* Get bit offset of field. */
11609 handle_member_location (die, cu, fp);
11610 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11611 if (attr != nullptr && attr->form_is_constant ())
11612 {
11613 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
11614 {
11615 /* For big endian bits, the DW_AT_bit_offset gives the
11616 additional bit offset from the MSB of the containing
11617 anonymous object to the MSB of the field. We don't
11618 have to do anything special since we don't need to
11619 know the size of the anonymous object. */
11620 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
11621 }
11622 else
11623 {
11624 /* For little endian bits, compute the bit offset to the
11625 MSB of the anonymous object, subtract off the number of
11626 bits from the MSB of the field to the MSB of the
11627 object, and then subtract off the number of bits of
11628 the field itself. The result is the bit offset of
11629 the LSB of the field. */
11630 int anonymous_size;
11631 int bit_offset = attr->constant_value (0);
11632
11633 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11634 if (attr != nullptr && attr->form_is_constant ())
11635 {
11636 /* The size of the anonymous object containing
11637 the bit field is explicit, so use the
11638 indicated size (in bytes). */
11639 anonymous_size = attr->constant_value (0);
11640 }
11641 else
11642 {
11643 /* The size of the anonymous object containing
11644 the bit field must be inferred from the type
11645 attribute of the data member containing the
11646 bit field. */
11647 anonymous_size = fp->type ()->length ();
11648 }
11649 fp->set_loc_bitpos (fp->loc_bitpos ()
11650 + anonymous_size * bits_per_byte
11651 - bit_offset - FIELD_BITSIZE (*fp));
11652 }
11653 }
11654
11655 /* Get name of field. */
11656 fieldname = dwarf2_name (die, cu);
11657 if (fieldname == NULL)
11658 fieldname = "";
11659
11660 /* The name is already allocated along with this objfile, so we don't
11661 need to duplicate it for the type. */
11662 fp->set_name (fieldname);
11663
11664 /* Change accessibility for artificial fields (e.g. virtual table
11665 pointer or virtual base class pointer) to private. */
11666 if (dwarf2_attr (die, DW_AT_artificial, cu))
11667 {
11668 FIELD_ARTIFICIAL (*fp) = 1;
11669 new_field->accessibility = DW_ACCESS_private;
11670 fip->non_public_fields = true;
11671 }
11672 }
11673 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11674 {
11675 /* C++ static member. */
11676
11677 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11678 is a declaration, but all versions of G++ as of this writing
11679 (so through at least 3.2.1) incorrectly generate
11680 DW_TAG_variable tags. */
11681
11682 const char *physname;
11683
11684 /* Get name of field. */
11685 fieldname = dwarf2_name (die, cu);
11686 if (fieldname == NULL)
11687 return;
11688
11689 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11690 if (attr
11691 /* Only create a symbol if this is an external value.
11692 new_symbol checks this and puts the value in the global symbol
11693 table, which we want. If it is not external, new_symbol
11694 will try to put the value in cu->list_in_scope which is wrong. */
11695 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11696 {
11697 /* A static const member, not much different than an enum as far as
11698 we're concerned, except that we can support more types. */
11699 new_symbol (die, NULL, cu);
11700 }
11701
11702 /* Get physical name. */
11703 physname = dwarf2_physname (fieldname, die, cu);
11704
11705 /* The name is already allocated along with this objfile, so we don't
11706 need to duplicate it for the type. */
11707 fp->set_loc_physname (physname ? physname : "");
11708 fp->set_type (die_type (die, cu));
11709 fp->set_name (fieldname);
11710 }
11711 else if (die->tag == DW_TAG_inheritance)
11712 {
11713 /* C++ base class field. */
11714 handle_member_location (die, cu, fp);
11715 FIELD_BITSIZE (*fp) = 0;
11716 fp->set_type (die_type (die, cu));
11717 fp->set_name (fp->type ()->name ());
11718 }
11719 else
11720 gdb_assert_not_reached ("missing case in dwarf2_add_field");
11721 }
11722
11723 /* Can the type given by DIE define another type? */
11724
11725 static bool
11726 type_can_define_types (const struct die_info *die)
11727 {
11728 switch (die->tag)
11729 {
11730 case DW_TAG_typedef:
11731 case DW_TAG_class_type:
11732 case DW_TAG_structure_type:
11733 case DW_TAG_union_type:
11734 case DW_TAG_enumeration_type:
11735 return true;
11736
11737 default:
11738 return false;
11739 }
11740 }
11741
11742 /* Add a type definition defined in the scope of the FIP's class. */
11743
11744 static void
11745 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
11746 struct dwarf2_cu *cu)
11747 {
11748 struct decl_field fp;
11749 memset (&fp, 0, sizeof (fp));
11750
11751 gdb_assert (type_can_define_types (die));
11752
11753 /* Get name of field. NULL is okay here, meaning an anonymous type. */
11754 fp.name = dwarf2_name (die, cu);
11755 fp.type = read_type_die (die, cu);
11756
11757 /* Save accessibility. */
11758 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
11759 switch (accessibility)
11760 {
11761 case DW_ACCESS_public:
11762 /* The assumed value if neither private nor protected. */
11763 break;
11764 case DW_ACCESS_private:
11765 fp.is_private = 1;
11766 break;
11767 case DW_ACCESS_protected:
11768 fp.is_protected = 1;
11769 break;
11770 }
11771
11772 if (die->tag == DW_TAG_typedef)
11773 fip->typedef_field_list.push_back (fp);
11774 else
11775 fip->nested_types_list.push_back (fp);
11776 }
11777
11778 /* A convenience typedef that's used when finding the discriminant
11779 field for a variant part. */
11780 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
11781 offset_map_type;
11782
11783 /* Compute the discriminant range for a given variant. OBSTACK is
11784 where the results will be stored. VARIANT is the variant to
11785 process. IS_UNSIGNED indicates whether the discriminant is signed
11786 or unsigned. */
11787
11788 static const gdb::array_view<discriminant_range>
11789 convert_variant_range (struct obstack *obstack, const variant_field &variant,
11790 bool is_unsigned)
11791 {
11792 std::vector<discriminant_range> ranges;
11793
11794 if (variant.default_branch)
11795 return {};
11796
11797 if (variant.discr_list_data == nullptr)
11798 {
11799 discriminant_range r
11800 = {variant.discriminant_value, variant.discriminant_value};
11801 ranges.push_back (r);
11802 }
11803 else
11804 {
11805 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
11806 variant.discr_list_data->size);
11807 while (!data.empty ())
11808 {
11809 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
11810 {
11811 complaint (_("invalid discriminant marker: %d"), data[0]);
11812 break;
11813 }
11814 bool is_range = data[0] == DW_DSC_range;
11815 data = data.slice (1);
11816
11817 ULONGEST low, high;
11818 unsigned int bytes_read;
11819
11820 if (data.empty ())
11821 {
11822 complaint (_("DW_AT_discr_list missing low value"));
11823 break;
11824 }
11825 if (is_unsigned)
11826 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
11827 else
11828 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
11829 &bytes_read);
11830 data = data.slice (bytes_read);
11831
11832 if (is_range)
11833 {
11834 if (data.empty ())
11835 {
11836 complaint (_("DW_AT_discr_list missing high value"));
11837 break;
11838 }
11839 if (is_unsigned)
11840 high = read_unsigned_leb128 (nullptr, data.data (),
11841 &bytes_read);
11842 else
11843 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
11844 &bytes_read);
11845 data = data.slice (bytes_read);
11846 }
11847 else
11848 high = low;
11849
11850 ranges.push_back ({ low, high });
11851 }
11852 }
11853
11854 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
11855 ranges.size ());
11856 std::copy (ranges.begin (), ranges.end (), result);
11857 return gdb::array_view<discriminant_range> (result, ranges.size ());
11858 }
11859
11860 static const gdb::array_view<variant_part> create_variant_parts
11861 (struct obstack *obstack,
11862 const offset_map_type &offset_map,
11863 struct field_info *fi,
11864 const std::vector<variant_part_builder> &variant_parts);
11865
11866 /* Fill in a "struct variant" for a given variant field. RESULT is
11867 the variant to fill in. OBSTACK is where any needed allocations
11868 will be done. OFFSET_MAP holds the mapping from section offsets to
11869 fields for the type. FI describes the fields of the type we're
11870 processing. FIELD is the variant field we're converting. */
11871
11872 static void
11873 create_one_variant (variant &result, struct obstack *obstack,
11874 const offset_map_type &offset_map,
11875 struct field_info *fi, const variant_field &field)
11876 {
11877 result.discriminants = convert_variant_range (obstack, field, false);
11878 result.first_field = field.first_field + fi->baseclasses.size ();
11879 result.last_field = field.last_field + fi->baseclasses.size ();
11880 result.parts = create_variant_parts (obstack, offset_map, fi,
11881 field.variant_parts);
11882 }
11883
11884 /* Fill in a "struct variant_part" for a given variant part. RESULT
11885 is the variant part to fill in. OBSTACK is where any needed
11886 allocations will be done. OFFSET_MAP holds the mapping from
11887 section offsets to fields for the type. FI describes the fields of
11888 the type we're processing. BUILDER is the variant part to be
11889 converted. */
11890
11891 static void
11892 create_one_variant_part (variant_part &result,
11893 struct obstack *obstack,
11894 const offset_map_type &offset_map,
11895 struct field_info *fi,
11896 const variant_part_builder &builder)
11897 {
11898 auto iter = offset_map.find (builder.discriminant_offset);
11899 if (iter == offset_map.end ())
11900 {
11901 result.discriminant_index = -1;
11902 /* Doesn't matter. */
11903 result.is_unsigned = false;
11904 }
11905 else
11906 {
11907 result.discriminant_index = iter->second;
11908 result.is_unsigned
11909 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
11910 }
11911
11912 size_t n = builder.variants.size ();
11913 variant *output = new (obstack) variant[n];
11914 for (size_t i = 0; i < n; ++i)
11915 create_one_variant (output[i], obstack, offset_map, fi,
11916 builder.variants[i]);
11917
11918 result.variants = gdb::array_view<variant> (output, n);
11919 }
11920
11921 /* Create a vector of variant parts that can be attached to a type.
11922 OBSTACK is where any needed allocations will be done. OFFSET_MAP
11923 holds the mapping from section offsets to fields for the type. FI
11924 describes the fields of the type we're processing. VARIANT_PARTS
11925 is the vector to convert. */
11926
11927 static const gdb::array_view<variant_part>
11928 create_variant_parts (struct obstack *obstack,
11929 const offset_map_type &offset_map,
11930 struct field_info *fi,
11931 const std::vector<variant_part_builder> &variant_parts)
11932 {
11933 if (variant_parts.empty ())
11934 return {};
11935
11936 size_t n = variant_parts.size ();
11937 variant_part *result = new (obstack) variant_part[n];
11938 for (size_t i = 0; i < n; ++i)
11939 create_one_variant_part (result[i], obstack, offset_map, fi,
11940 variant_parts[i]);
11941
11942 return gdb::array_view<variant_part> (result, n);
11943 }
11944
11945 /* Compute the variant part vector for FIP, attaching it to TYPE when
11946 done. */
11947
11948 static void
11949 add_variant_property (struct field_info *fip, struct type *type,
11950 struct dwarf2_cu *cu)
11951 {
11952 /* Map section offsets of fields to their field index. Note the
11953 field index here does not take the number of baseclasses into
11954 account. */
11955 offset_map_type offset_map;
11956 for (int i = 0; i < fip->fields.size (); ++i)
11957 offset_map[fip->fields[i].offset] = i;
11958
11959 struct objfile *objfile = cu->per_objfile->objfile;
11960 gdb::array_view<const variant_part> parts
11961 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
11962 fip->variant_parts);
11963
11964 struct dynamic_prop prop;
11965 prop.set_variant_parts ((gdb::array_view<variant_part> *)
11966 obstack_copy (&objfile->objfile_obstack, &parts,
11967 sizeof (parts)));
11968
11969 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
11970 }
11971
11972 /* Create the vector of fields, and attach it to the type. */
11973
11974 static void
11975 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11976 struct dwarf2_cu *cu)
11977 {
11978 int nfields = fip->nfields ();
11979
11980 /* Record the field count, allocate space for the array of fields,
11981 and create blank accessibility bitfields if necessary. */
11982 type->set_num_fields (nfields);
11983 type->set_fields
11984 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
11985
11986 if (fip->non_public_fields && cu->lang () != language_ada)
11987 {
11988 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11989
11990 TYPE_FIELD_PRIVATE_BITS (type) =
11991 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11992 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11993
11994 TYPE_FIELD_PROTECTED_BITS (type) =
11995 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11996 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11997
11998 TYPE_FIELD_IGNORE_BITS (type) =
11999 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12000 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12001 }
12002
12003 /* If the type has baseclasses, allocate and clear a bit vector for
12004 TYPE_FIELD_VIRTUAL_BITS. */
12005 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
12006 {
12007 int num_bytes = B_BYTES (fip->baseclasses.size ());
12008 unsigned char *pointer;
12009
12010 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12011 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
12012 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12013 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
12014 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
12015 }
12016
12017 if (!fip->variant_parts.empty ())
12018 add_variant_property (fip, type, cu);
12019
12020 /* Copy the saved-up fields into the field vector. */
12021 for (int i = 0; i < nfields; ++i)
12022 {
12023 struct nextfield &field
12024 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
12025 : fip->fields[i - fip->baseclasses.size ()]);
12026
12027 type->field (i) = field.field;
12028 switch (field.accessibility)
12029 {
12030 case DW_ACCESS_private:
12031 if (cu->lang () != language_ada)
12032 SET_TYPE_FIELD_PRIVATE (type, i);
12033 break;
12034
12035 case DW_ACCESS_protected:
12036 if (cu->lang () != language_ada)
12037 SET_TYPE_FIELD_PROTECTED (type, i);
12038 break;
12039
12040 case DW_ACCESS_public:
12041 break;
12042
12043 default:
12044 /* Unknown accessibility. Complain and treat it as public. */
12045 {
12046 complaint (_("unsupported accessibility %d"),
12047 field.accessibility);
12048 }
12049 break;
12050 }
12051 if (i < fip->baseclasses.size ())
12052 {
12053 switch (field.virtuality)
12054 {
12055 case DW_VIRTUALITY_virtual:
12056 case DW_VIRTUALITY_pure_virtual:
12057 if (cu->lang () == language_ada)
12058 error (_("unexpected virtuality in component of Ada type"));
12059 SET_TYPE_FIELD_VIRTUAL (type, i);
12060 break;
12061 }
12062 }
12063 }
12064 }
12065
12066 /* Return true if this member function is a constructor, false
12067 otherwise. */
12068
12069 static int
12070 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12071 {
12072 const char *fieldname;
12073 const char *type_name;
12074 int len;
12075
12076 if (die->parent == NULL)
12077 return 0;
12078
12079 if (die->parent->tag != DW_TAG_structure_type
12080 && die->parent->tag != DW_TAG_union_type
12081 && die->parent->tag != DW_TAG_class_type)
12082 return 0;
12083
12084 fieldname = dwarf2_name (die, cu);
12085 type_name = dwarf2_name (die->parent, cu);
12086 if (fieldname == NULL || type_name == NULL)
12087 return 0;
12088
12089 len = strlen (fieldname);
12090 return (strncmp (fieldname, type_name, len) == 0
12091 && (type_name[len] == '\0' || type_name[len] == '<'));
12092 }
12093
12094 /* Add a member function to the proper fieldlist. */
12095
12096 static void
12097 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12098 struct type *type, struct dwarf2_cu *cu)
12099 {
12100 struct objfile *objfile = cu->per_objfile->objfile;
12101 struct attribute *attr;
12102 int i;
12103 struct fnfieldlist *flp = nullptr;
12104 struct fn_field *fnp;
12105 const char *fieldname;
12106 struct type *this_type;
12107
12108 if (cu->lang () == language_ada)
12109 error (_("unexpected member function in Ada type"));
12110
12111 /* Get name of member function. */
12112 fieldname = dwarf2_name (die, cu);
12113 if (fieldname == NULL)
12114 return;
12115
12116 /* Look up member function name in fieldlist. */
12117 for (i = 0; i < fip->fnfieldlists.size (); i++)
12118 {
12119 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12120 {
12121 flp = &fip->fnfieldlists[i];
12122 break;
12123 }
12124 }
12125
12126 /* Create a new fnfieldlist if necessary. */
12127 if (flp == nullptr)
12128 {
12129 fip->fnfieldlists.emplace_back ();
12130 flp = &fip->fnfieldlists.back ();
12131 flp->name = fieldname;
12132 i = fip->fnfieldlists.size () - 1;
12133 }
12134
12135 /* Create a new member function field and add it to the vector of
12136 fnfieldlists. */
12137 flp->fnfields.emplace_back ();
12138 fnp = &flp->fnfields.back ();
12139
12140 /* Delay processing of the physname until later. */
12141 if (cu->lang () == language_cplus)
12142 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
12143 die, cu);
12144 else
12145 {
12146 const char *physname = dwarf2_physname (fieldname, die, cu);
12147 fnp->physname = physname ? physname : "";
12148 }
12149
12150 fnp->type = alloc_type (objfile);
12151 this_type = read_type_die (die, cu);
12152 if (this_type && this_type->code () == TYPE_CODE_FUNC)
12153 {
12154 int nparams = this_type->num_fields ();
12155
12156 /* TYPE is the domain of this method, and THIS_TYPE is the type
12157 of the method itself (TYPE_CODE_METHOD). */
12158 smash_to_method_type (fnp->type, type,
12159 this_type->target_type (),
12160 this_type->fields (),
12161 this_type->num_fields (),
12162 this_type->has_varargs ());
12163
12164 /* Handle static member functions.
12165 Dwarf2 has no clean way to discern C++ static and non-static
12166 member functions. G++ helps GDB by marking the first
12167 parameter for non-static member functions (which is the this
12168 pointer) as artificial. We obtain this information from
12169 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12170 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
12171 fnp->voffset = VOFFSET_STATIC;
12172 }
12173 else
12174 complaint (_("member function type missing for '%s'"),
12175 dwarf2_full_name (fieldname, die, cu));
12176
12177 /* Get fcontext from DW_AT_containing_type if present. */
12178 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12179 fnp->fcontext = die_containing_type (die, cu);
12180
12181 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12182 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12183
12184 /* Get accessibility. */
12185 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
12186 switch (accessibility)
12187 {
12188 case DW_ACCESS_private:
12189 fnp->is_private = 1;
12190 break;
12191 case DW_ACCESS_protected:
12192 fnp->is_protected = 1;
12193 break;
12194 }
12195
12196 /* Check for artificial methods. */
12197 attr = dwarf2_attr (die, DW_AT_artificial, cu);
12198 if (attr && attr->as_boolean ())
12199 fnp->is_artificial = 1;
12200
12201 /* Check for defaulted methods. */
12202 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
12203 if (attr != nullptr)
12204 fnp->defaulted = attr->defaulted ();
12205
12206 /* Check for deleted methods. */
12207 attr = dwarf2_attr (die, DW_AT_deleted, cu);
12208 if (attr != nullptr && attr->as_boolean ())
12209 fnp->is_deleted = 1;
12210
12211 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12212
12213 /* Get index in virtual function table if it is a virtual member
12214 function. For older versions of GCC, this is an offset in the
12215 appropriate virtual table, as specified by DW_AT_containing_type.
12216 For everyone else, it is an expression to be evaluated relative
12217 to the object address. */
12218
12219 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12220 if (attr != nullptr)
12221 {
12222 if (attr->form_is_block () && attr->as_block ()->size > 0)
12223 {
12224 struct dwarf_block *block = attr->as_block ();
12225
12226 if (block->data[0] == DW_OP_constu)
12227 {
12228 /* Old-style GCC. */
12229 fnp->voffset = decode_locdesc (block, cu) + 2;
12230 }
12231 else if (block->data[0] == DW_OP_deref
12232 || (block->size > 1
12233 && block->data[0] == DW_OP_deref_size
12234 && block->data[1] == cu->header.addr_size))
12235 {
12236 fnp->voffset = decode_locdesc (block, cu);
12237 if ((fnp->voffset % cu->header.addr_size) != 0)
12238 dwarf2_complex_location_expr_complaint ();
12239 else
12240 fnp->voffset /= cu->header.addr_size;
12241 fnp->voffset += 2;
12242 }
12243 else
12244 dwarf2_complex_location_expr_complaint ();
12245
12246 if (!fnp->fcontext)
12247 {
12248 /* If there is no `this' field and no DW_AT_containing_type,
12249 we cannot actually find a base class context for the
12250 vtable! */
12251 if (this_type->num_fields () == 0
12252 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
12253 {
12254 complaint (_("cannot determine context for virtual member "
12255 "function \"%s\" (offset %s)"),
12256 fieldname, sect_offset_str (die->sect_off));
12257 }
12258 else
12259 {
12260 fnp->fcontext = this_type->field (0).type ()->target_type ();
12261 }
12262 }
12263 }
12264 else if (attr->form_is_section_offset ())
12265 {
12266 dwarf2_complex_location_expr_complaint ();
12267 }
12268 else
12269 {
12270 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12271 fieldname);
12272 }
12273 }
12274 else
12275 {
12276 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12277 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
12278 {
12279 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12280 complaint (_("Member function \"%s\" (offset %s) is virtual "
12281 "but the vtable offset is not specified"),
12282 fieldname, sect_offset_str (die->sect_off));
12283 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12284 TYPE_CPLUS_DYNAMIC (type) = 1;
12285 }
12286 }
12287 }
12288
12289 /* Create the vector of member function fields, and attach it to the type. */
12290
12291 static void
12292 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
12293 struct dwarf2_cu *cu)
12294 {
12295 if (cu->lang () == language_ada)
12296 error (_("unexpected member functions in Ada type"));
12297
12298 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12299 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
12300 TYPE_ALLOC (type,
12301 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
12302
12303 for (int i = 0; i < fip->fnfieldlists.size (); i++)
12304 {
12305 struct fnfieldlist &nf = fip->fnfieldlists[i];
12306 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
12307
12308 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
12309 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
12310 fn_flp->fn_fields = (struct fn_field *)
12311 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
12312
12313 for (int k = 0; k < nf.fnfields.size (); ++k)
12314 fn_flp->fn_fields[k] = nf.fnfields[k];
12315 }
12316
12317 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
12318 }
12319
12320 /* Returns non-zero if NAME is the name of a vtable member in CU's
12321 language, zero otherwise. */
12322 static int
12323 is_vtable_name (const char *name, struct dwarf2_cu *cu)
12324 {
12325 static const char vptr[] = "_vptr";
12326
12327 /* Look for the C++ form of the vtable. */
12328 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
12329 return 1;
12330
12331 return 0;
12332 }
12333
12334 /* GCC outputs unnamed structures that are really pointers to member
12335 functions, with the ABI-specified layout. If TYPE describes
12336 such a structure, smash it into a member function type.
12337
12338 GCC shouldn't do this; it should just output pointer to member DIEs.
12339 This is GCC PR debug/28767. */
12340
12341 static void
12342 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
12343 {
12344 struct type *pfn_type, *self_type, *new_type;
12345
12346 /* Check for a structure with no name and two children. */
12347 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12348 return;
12349
12350 /* Check for __pfn and __delta members. */
12351 if (type->field (0).name () == NULL
12352 || strcmp (type->field (0).name (), "__pfn") != 0
12353 || type->field (1).name () == NULL
12354 || strcmp (type->field (1).name (), "__delta") != 0)
12355 return;
12356
12357 /* Find the type of the method. */
12358 pfn_type = type->field (0).type ();
12359 if (pfn_type == NULL
12360 || pfn_type->code () != TYPE_CODE_PTR
12361 || pfn_type->target_type ()->code () != TYPE_CODE_FUNC)
12362 return;
12363
12364 /* Look for the "this" argument. */
12365 pfn_type = pfn_type->target_type ();
12366 if (pfn_type->num_fields () == 0
12367 /* || pfn_type->field (0).type () == NULL */
12368 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
12369 return;
12370
12371 self_type = pfn_type->field (0).type ()->target_type ();
12372 new_type = alloc_type (objfile);
12373 smash_to_method_type (new_type, self_type, pfn_type->target_type (),
12374 pfn_type->fields (), pfn_type->num_fields (),
12375 pfn_type->has_varargs ());
12376 smash_to_methodptr_type (type, new_type);
12377 }
12378
12379 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
12380 requires rewriting, then copy it and return the updated copy.
12381 Otherwise return nullptr. */
12382
12383 static struct type *
12384 rewrite_array_type (struct type *type)
12385 {
12386 if (type->code () != TYPE_CODE_ARRAY)
12387 return nullptr;
12388
12389 struct type *index_type = type->index_type ();
12390 range_bounds *current_bounds = index_type->bounds ();
12391
12392 /* Handle multi-dimensional arrays. */
12393 struct type *new_target = rewrite_array_type (type->target_type ());
12394 if (new_target == nullptr)
12395 {
12396 /* Maybe we don't need to rewrite this array. */
12397 if (current_bounds->low.kind () == PROP_CONST
12398 && current_bounds->high.kind () == PROP_CONST)
12399 return nullptr;
12400 }
12401
12402 /* Either the target type was rewritten, or the bounds have to be
12403 updated. Either way we want to copy the type and update
12404 everything. */
12405 struct type *copy = copy_type (type);
12406 int nfields = copy->num_fields ();
12407 field *new_fields
12408 = ((struct field *) TYPE_ZALLOC (copy,
12409 nfields * sizeof (struct field)));
12410 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
12411 copy->set_fields (new_fields);
12412 if (new_target != nullptr)
12413 copy->set_target_type (new_target);
12414
12415 struct type *index_copy = copy_type (index_type);
12416 range_bounds *bounds
12417 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
12418 sizeof (range_bounds));
12419 *bounds = *current_bounds;
12420 bounds->low.set_const_val (1);
12421 bounds->high.set_const_val (0);
12422 index_copy->set_bounds (bounds);
12423 copy->set_index_type (index_copy);
12424
12425 return copy;
12426 }
12427
12428 /* While some versions of GCC will generate complicated DWARF for an
12429 array (see quirk_ada_thick_pointer), more recent versions were
12430 modified to emit an explicit thick pointer structure. However, in
12431 this case, the array still has DWARF expressions for its ranges,
12432 and these must be ignored. */
12433
12434 static void
12435 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
12436 struct type *type)
12437 {
12438 gdb_assert (cu->lang () == language_ada);
12439
12440 /* Check for a structure with two children. */
12441 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12442 return;
12443
12444 /* Check for P_ARRAY and P_BOUNDS members. */
12445 if (type->field (0).name () == NULL
12446 || strcmp (type->field (0).name (), "P_ARRAY") != 0
12447 || type->field (1).name () == NULL
12448 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
12449 return;
12450
12451 /* Make sure we're looking at a pointer to an array. */
12452 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
12453 return;
12454
12455 /* The Ada code already knows how to handle these types, so all that
12456 we need to do is turn the bounds into static bounds. However, we
12457 don't want to rewrite existing array or index types in-place,
12458 because those may be referenced in other contexts where this
12459 rewriting is undesirable. */
12460 struct type *new_ary_type
12461 = rewrite_array_type (type->field (0).type ()->target_type ());
12462 if (new_ary_type != nullptr)
12463 type->field (0).set_type (lookup_pointer_type (new_ary_type));
12464 }
12465
12466 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
12467 appropriate error checking and issuing complaints if there is a
12468 problem. */
12469
12470 static ULONGEST
12471 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
12472 {
12473 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
12474
12475 if (attr == nullptr)
12476 return 0;
12477
12478 if (!attr->form_is_constant ())
12479 {
12480 complaint (_("DW_AT_alignment must have constant form"
12481 " - DIE at %s [in module %s]"),
12482 sect_offset_str (die->sect_off),
12483 objfile_name (cu->per_objfile->objfile));
12484 return 0;
12485 }
12486
12487 LONGEST val = attr->constant_value (0);
12488 if (val < 0)
12489 {
12490 complaint (_("DW_AT_alignment value must not be negative"
12491 " - DIE at %s [in module %s]"),
12492 sect_offset_str (die->sect_off),
12493 objfile_name (cu->per_objfile->objfile));
12494 return 0;
12495 }
12496 ULONGEST align = val;
12497
12498 if (align == 0)
12499 {
12500 complaint (_("DW_AT_alignment value must not be zero"
12501 " - DIE at %s [in module %s]"),
12502 sect_offset_str (die->sect_off),
12503 objfile_name (cu->per_objfile->objfile));
12504 return 0;
12505 }
12506 if ((align & (align - 1)) != 0)
12507 {
12508 complaint (_("DW_AT_alignment value must be a power of 2"
12509 " - DIE at %s [in module %s]"),
12510 sect_offset_str (die->sect_off),
12511 objfile_name (cu->per_objfile->objfile));
12512 return 0;
12513 }
12514
12515 return align;
12516 }
12517
12518 /* If the DIE has a DW_AT_alignment attribute, use its value to set
12519 the alignment for TYPE. */
12520
12521 static void
12522 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
12523 struct type *type)
12524 {
12525 if (!set_type_align (type, get_alignment (cu, die)))
12526 complaint (_("DW_AT_alignment value too large"
12527 " - DIE at %s [in module %s]"),
12528 sect_offset_str (die->sect_off),
12529 objfile_name (cu->per_objfile->objfile));
12530 }
12531
12532 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12533 constant for a type, according to DWARF5 spec, Table 5.5. */
12534
12535 static bool
12536 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
12537 {
12538 switch (value)
12539 {
12540 case DW_CC_normal:
12541 case DW_CC_pass_by_reference:
12542 case DW_CC_pass_by_value:
12543 return true;
12544
12545 default:
12546 complaint (_("unrecognized DW_AT_calling_convention value "
12547 "(%s) for a type"), pulongest (value));
12548 return false;
12549 }
12550 }
12551
12552 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12553 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
12554 also according to GNU-specific values (see include/dwarf2.h). */
12555
12556 static bool
12557 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
12558 {
12559 switch (value)
12560 {
12561 case DW_CC_normal:
12562 case DW_CC_program:
12563 case DW_CC_nocall:
12564 return true;
12565
12566 case DW_CC_GNU_renesas_sh:
12567 case DW_CC_GNU_borland_fastcall_i386:
12568 case DW_CC_GDB_IBM_OpenCL:
12569 return true;
12570
12571 default:
12572 complaint (_("unrecognized DW_AT_calling_convention value "
12573 "(%s) for a subroutine"), pulongest (value));
12574 return false;
12575 }
12576 }
12577
12578 /* Called when we find the DIE that starts a structure or union scope
12579 (definition) to create a type for the structure or union. Fill in
12580 the type's name and general properties; the members will not be
12581 processed until process_structure_scope. A symbol table entry for
12582 the type will also not be done until process_structure_scope (assuming
12583 the type has a name).
12584
12585 NOTE: we need to call these functions regardless of whether or not the
12586 DIE has a DW_AT_name attribute, since it might be an anonymous
12587 structure or union. This gets the type entered into our set of
12588 user defined types. */
12589
12590 static struct type *
12591 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
12592 {
12593 struct objfile *objfile = cu->per_objfile->objfile;
12594 struct type *type;
12595 struct attribute *attr;
12596 const char *name;
12597
12598 /* If the definition of this type lives in .debug_types, read that type.
12599 Don't follow DW_AT_specification though, that will take us back up
12600 the chain and we want to go down. */
12601 attr = die->attr (DW_AT_signature);
12602 if (attr != nullptr)
12603 {
12604 type = get_DW_AT_signature_type (die, attr, cu);
12605
12606 /* The type's CU may not be the same as CU.
12607 Ensure TYPE is recorded with CU in die_type_hash. */
12608 return set_die_type (die, type, cu);
12609 }
12610
12611 type = alloc_type (objfile);
12612 INIT_CPLUS_SPECIFIC (type);
12613
12614 name = dwarf2_name (die, cu);
12615 if (name != NULL)
12616 {
12617 if (cu->lang () == language_cplus
12618 || cu->lang () == language_d
12619 || cu->lang () == language_rust)
12620 {
12621 const char *full_name = dwarf2_full_name (name, die, cu);
12622
12623 /* dwarf2_full_name might have already finished building the DIE's
12624 type. If so, there is no need to continue. */
12625 if (get_die_type (die, cu) != NULL)
12626 return get_die_type (die, cu);
12627
12628 type->set_name (full_name);
12629 }
12630 else
12631 {
12632 /* The name is already allocated along with this objfile, so
12633 we don't need to duplicate it for the type. */
12634 type->set_name (name);
12635 }
12636 }
12637
12638 if (die->tag == DW_TAG_structure_type)
12639 {
12640 type->set_code (TYPE_CODE_STRUCT);
12641 }
12642 else if (die->tag == DW_TAG_union_type)
12643 {
12644 type->set_code (TYPE_CODE_UNION);
12645 }
12646 else if (die->tag == DW_TAG_namelist)
12647 {
12648 type->set_code (TYPE_CODE_NAMELIST);
12649 }
12650 else
12651 {
12652 type->set_code (TYPE_CODE_STRUCT);
12653 }
12654
12655 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
12656 type->set_is_declared_class (true);
12657
12658 /* Store the calling convention in the type if it's available in
12659 the die. Otherwise the calling convention remains set to
12660 the default value DW_CC_normal. */
12661 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12662 if (attr != nullptr
12663 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
12664 {
12665 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12666 TYPE_CPLUS_CALLING_CONVENTION (type)
12667 = (enum dwarf_calling_convention) (attr->constant_value (0));
12668 }
12669
12670 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12671 if (attr != nullptr)
12672 {
12673 if (attr->form_is_constant ())
12674 type->set_length (attr->constant_value (0));
12675 else
12676 {
12677 struct dynamic_prop prop;
12678 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
12679 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
12680
12681 type->set_length (0);
12682 }
12683 }
12684 else
12685 type->set_length (0);
12686
12687 maybe_set_alignment (cu, die, type);
12688
12689 if (producer_is_icc_lt_14 (cu) && (type->length () == 0))
12690 {
12691 /* ICC<14 does not output the required DW_AT_declaration on
12692 incomplete types, but gives them a size of zero. */
12693 type->set_is_stub (true);
12694 }
12695 else
12696 type->set_stub_is_supported (true);
12697
12698 if (die_is_declaration (die, cu))
12699 type->set_is_stub (true);
12700 else if (attr == NULL && die->child == NULL
12701 && producer_is_realview (cu->producer))
12702 /* RealView does not output the required DW_AT_declaration
12703 on incomplete types. */
12704 type->set_is_stub (true);
12705
12706 /* We need to add the type field to the die immediately so we don't
12707 infinitely recurse when dealing with pointers to the structure
12708 type within the structure itself. */
12709 set_die_type (die, type, cu);
12710
12711 /* set_die_type should be already done. */
12712 set_descriptive_type (type, die, cu);
12713
12714 return type;
12715 }
12716
12717 static void handle_struct_member_die
12718 (struct die_info *child_die,
12719 struct type *type,
12720 struct field_info *fi,
12721 std::vector<struct symbol *> *template_args,
12722 struct dwarf2_cu *cu);
12723
12724 /* A helper for handle_struct_member_die that handles
12725 DW_TAG_variant_part. */
12726
12727 static void
12728 handle_variant_part (struct die_info *die, struct type *type,
12729 struct field_info *fi,
12730 std::vector<struct symbol *> *template_args,
12731 struct dwarf2_cu *cu)
12732 {
12733 variant_part_builder *new_part;
12734 if (fi->current_variant_part == nullptr)
12735 {
12736 fi->variant_parts.emplace_back ();
12737 new_part = &fi->variant_parts.back ();
12738 }
12739 else if (!fi->current_variant_part->processing_variant)
12740 {
12741 complaint (_("nested DW_TAG_variant_part seen "
12742 "- DIE at %s [in module %s]"),
12743 sect_offset_str (die->sect_off),
12744 objfile_name (cu->per_objfile->objfile));
12745 return;
12746 }
12747 else
12748 {
12749 variant_field &current = fi->current_variant_part->variants.back ();
12750 current.variant_parts.emplace_back ();
12751 new_part = &current.variant_parts.back ();
12752 }
12753
12754 /* When we recurse, we want callees to add to this new variant
12755 part. */
12756 scoped_restore save_current_variant_part
12757 = make_scoped_restore (&fi->current_variant_part, new_part);
12758
12759 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
12760 if (discr == NULL)
12761 {
12762 /* It's a univariant form, an extension we support. */
12763 }
12764 else if (discr->form_is_ref ())
12765 {
12766 struct dwarf2_cu *target_cu = cu;
12767 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
12768
12769 new_part->discriminant_offset = target_die->sect_off;
12770 }
12771 else
12772 {
12773 complaint (_("DW_AT_discr does not have DIE reference form"
12774 " - DIE at %s [in module %s]"),
12775 sect_offset_str (die->sect_off),
12776 objfile_name (cu->per_objfile->objfile));
12777 }
12778
12779 for (die_info *child_die = die->child;
12780 child_die != NULL;
12781 child_die = child_die->sibling)
12782 handle_struct_member_die (child_die, type, fi, template_args, cu);
12783 }
12784
12785 /* A helper for handle_struct_member_die that handles
12786 DW_TAG_variant. */
12787
12788 static void
12789 handle_variant (struct die_info *die, struct type *type,
12790 struct field_info *fi,
12791 std::vector<struct symbol *> *template_args,
12792 struct dwarf2_cu *cu)
12793 {
12794 if (fi->current_variant_part == nullptr)
12795 {
12796 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
12797 "- DIE at %s [in module %s]"),
12798 sect_offset_str (die->sect_off),
12799 objfile_name (cu->per_objfile->objfile));
12800 return;
12801 }
12802 if (fi->current_variant_part->processing_variant)
12803 {
12804 complaint (_("nested DW_TAG_variant seen "
12805 "- DIE at %s [in module %s]"),
12806 sect_offset_str (die->sect_off),
12807 objfile_name (cu->per_objfile->objfile));
12808 return;
12809 }
12810
12811 scoped_restore save_processing_variant
12812 = make_scoped_restore (&fi->current_variant_part->processing_variant,
12813 true);
12814
12815 fi->current_variant_part->variants.emplace_back ();
12816 variant_field &variant = fi->current_variant_part->variants.back ();
12817 variant.first_field = fi->fields.size ();
12818
12819 /* In a variant we want to get the discriminant and also add a
12820 field for our sole member child. */
12821 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
12822 if (discr == nullptr || !discr->form_is_constant ())
12823 {
12824 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
12825 if (discr == nullptr || discr->as_block ()->size == 0)
12826 variant.default_branch = true;
12827 else
12828 variant.discr_list_data = discr->as_block ();
12829 }
12830 else
12831 variant.discriminant_value = discr->constant_value (0);
12832
12833 for (die_info *variant_child = die->child;
12834 variant_child != NULL;
12835 variant_child = variant_child->sibling)
12836 handle_struct_member_die (variant_child, type, fi, template_args, cu);
12837
12838 variant.last_field = fi->fields.size ();
12839 }
12840
12841 /* A helper for process_structure_scope that handles a single member
12842 DIE. */
12843
12844 static void
12845 handle_struct_member_die (struct die_info *child_die, struct type *type,
12846 struct field_info *fi,
12847 std::vector<struct symbol *> *template_args,
12848 struct dwarf2_cu *cu)
12849 {
12850 if (child_die->tag == DW_TAG_member
12851 || child_die->tag == DW_TAG_variable
12852 || child_die->tag == DW_TAG_namelist_item)
12853 {
12854 /* NOTE: carlton/2002-11-05: A C++ static data member
12855 should be a DW_TAG_member that is a declaration, but
12856 all versions of G++ as of this writing (so through at
12857 least 3.2.1) incorrectly generate DW_TAG_variable
12858 tags for them instead. */
12859 dwarf2_add_field (fi, child_die, cu);
12860 }
12861 else if (child_die->tag == DW_TAG_subprogram)
12862 {
12863 /* Rust doesn't have member functions in the C++ sense.
12864 However, it does emit ordinary functions as children
12865 of a struct DIE. */
12866 if (cu->lang () == language_rust)
12867 read_func_scope (child_die, cu);
12868 else
12869 {
12870 /* C++ member function. */
12871 dwarf2_add_member_fn (fi, child_die, type, cu);
12872 }
12873 }
12874 else if (child_die->tag == DW_TAG_inheritance)
12875 {
12876 /* C++ base class field. */
12877 dwarf2_add_field (fi, child_die, cu);
12878 }
12879 else if (type_can_define_types (child_die))
12880 dwarf2_add_type_defn (fi, child_die, cu);
12881 else if (child_die->tag == DW_TAG_template_type_param
12882 || child_die->tag == DW_TAG_template_value_param)
12883 {
12884 struct symbol *arg = new_symbol (child_die, NULL, cu);
12885
12886 if (arg != NULL)
12887 template_args->push_back (arg);
12888 }
12889 else if (child_die->tag == DW_TAG_variant_part)
12890 handle_variant_part (child_die, type, fi, template_args, cu);
12891 else if (child_die->tag == DW_TAG_variant)
12892 handle_variant (child_die, type, fi, template_args, cu);
12893 }
12894
12895 /* Finish creating a structure or union type, including filling in its
12896 members and creating a symbol for it. This function also handles Fortran
12897 namelist variables, their items or members and creating a symbol for
12898 them. */
12899
12900 static void
12901 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12902 {
12903 struct objfile *objfile = cu->per_objfile->objfile;
12904 struct die_info *child_die;
12905 struct type *type;
12906
12907 type = get_die_type (die, cu);
12908 if (type == NULL)
12909 type = read_structure_type (die, cu);
12910
12911 bool has_template_parameters = false;
12912 if (die->child != NULL && ! die_is_declaration (die, cu))
12913 {
12914 struct field_info fi;
12915 std::vector<struct symbol *> template_args;
12916
12917 child_die = die->child;
12918
12919 while (child_die && child_die->tag)
12920 {
12921 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
12922 child_die = child_die->sibling;
12923 }
12924
12925 /* Attach template arguments to type. */
12926 if (!template_args.empty ())
12927 {
12928 has_template_parameters = true;
12929 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12930 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
12931 TYPE_TEMPLATE_ARGUMENTS (type)
12932 = XOBNEWVEC (&objfile->objfile_obstack,
12933 struct symbol *,
12934 TYPE_N_TEMPLATE_ARGUMENTS (type));
12935 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12936 template_args.data (),
12937 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12938 * sizeof (struct symbol *)));
12939 }
12940
12941 /* Attach fields and member functions to the type. */
12942 if (fi.nfields () > 0)
12943 dwarf2_attach_fields_to_type (&fi, type, cu);
12944 if (!fi.fnfieldlists.empty ())
12945 {
12946 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12947
12948 /* Get the type which refers to the base class (possibly this
12949 class itself) which contains the vtable pointer for the current
12950 class from the DW_AT_containing_type attribute. This use of
12951 DW_AT_containing_type is a GNU extension. */
12952
12953 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12954 {
12955 struct type *t = die_containing_type (die, cu);
12956
12957 set_type_vptr_basetype (type, t);
12958 if (type == t)
12959 {
12960 int i;
12961
12962 /* Our own class provides vtbl ptr. */
12963 for (i = t->num_fields () - 1;
12964 i >= TYPE_N_BASECLASSES (t);
12965 --i)
12966 {
12967 const char *fieldname = t->field (i).name ();
12968
12969 if (is_vtable_name (fieldname, cu))
12970 {
12971 set_type_vptr_fieldno (type, i);
12972 break;
12973 }
12974 }
12975
12976 /* Complain if virtual function table field not found. */
12977 if (i < TYPE_N_BASECLASSES (t))
12978 complaint (_("virtual function table pointer "
12979 "not found when defining class '%s'"),
12980 type->name () ? type->name () : "");
12981 }
12982 else
12983 {
12984 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
12985 }
12986 }
12987 else if (cu->producer
12988 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
12989 {
12990 /* The IBM XLC compiler does not provide direct indication
12991 of the containing type, but the vtable pointer is
12992 always named __vfp. */
12993
12994 int i;
12995
12996 for (i = type->num_fields () - 1;
12997 i >= TYPE_N_BASECLASSES (type);
12998 --i)
12999 {
13000 if (strcmp (type->field (i).name (), "__vfp") == 0)
13001 {
13002 set_type_vptr_fieldno (type, i);
13003 set_type_vptr_basetype (type, type);
13004 break;
13005 }
13006 }
13007 }
13008 }
13009
13010 /* Copy fi.typedef_field_list linked list elements content into the
13011 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13012 if (!fi.typedef_field_list.empty ())
13013 {
13014 int count = fi.typedef_field_list.size ();
13015
13016 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13017 TYPE_TYPEDEF_FIELD_ARRAY (type)
13018 = ((struct decl_field *)
13019 TYPE_ALLOC (type,
13020 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
13021 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
13022
13023 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
13024 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
13025 }
13026
13027 /* Copy fi.nested_types_list linked list elements content into the
13028 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
13029 if (!fi.nested_types_list.empty ()
13030 && cu->lang () != language_ada)
13031 {
13032 int count = fi.nested_types_list.size ();
13033
13034 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13035 TYPE_NESTED_TYPES_ARRAY (type)
13036 = ((struct decl_field *)
13037 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
13038 TYPE_NESTED_TYPES_COUNT (type) = count;
13039
13040 for (int i = 0; i < fi.nested_types_list.size (); ++i)
13041 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
13042 }
13043 }
13044
13045 quirk_gcc_member_function_pointer (type, objfile);
13046 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
13047 cu->rust_unions.push_back (type);
13048 else if (cu->lang () == language_ada)
13049 quirk_ada_thick_pointer_struct (die, cu, type);
13050
13051 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13052 snapshots) has been known to create a die giving a declaration
13053 for a class that has, as a child, a die giving a definition for a
13054 nested class. So we have to process our children even if the
13055 current die is a declaration. Normally, of course, a declaration
13056 won't have any children at all. */
13057
13058 child_die = die->child;
13059
13060 while (child_die != NULL && child_die->tag)
13061 {
13062 if (child_die->tag == DW_TAG_member
13063 || child_die->tag == DW_TAG_variable
13064 || child_die->tag == DW_TAG_inheritance
13065 || child_die->tag == DW_TAG_template_value_param
13066 || child_die->tag == DW_TAG_template_type_param)
13067 {
13068 /* Do nothing. */
13069 }
13070 else
13071 process_die (child_die, cu);
13072
13073 child_die = child_die->sibling;
13074 }
13075
13076 /* Do not consider external references. According to the DWARF standard,
13077 these DIEs are identified by the fact that they have no byte_size
13078 attribute, and a declaration attribute. */
13079 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13080 || !die_is_declaration (die, cu)
13081 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
13082 {
13083 struct symbol *sym = new_symbol (die, type, cu);
13084
13085 if (has_template_parameters)
13086 {
13087 struct symtab *symtab;
13088 if (sym != nullptr)
13089 symtab = sym->symtab ();
13090 else if (cu->line_header != nullptr)
13091 {
13092 /* Any related symtab will do. */
13093 symtab
13094 = cu->line_header->file_names ()[0].symtab;
13095 }
13096 else
13097 {
13098 symtab = nullptr;
13099 complaint (_("could not find suitable "
13100 "symtab for template parameter"
13101 " - DIE at %s [in module %s]"),
13102 sect_offset_str (die->sect_off),
13103 objfile_name (objfile));
13104 }
13105
13106 if (symtab != nullptr)
13107 {
13108 /* Make sure that the symtab is set on the new symbols.
13109 Even though they don't appear in this symtab directly,
13110 other parts of gdb assume that symbols do, and this is
13111 reasonably true. */
13112 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
13113 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
13114 }
13115 }
13116 }
13117 }
13118
13119 /* Assuming DIE is an enumeration type, and TYPE is its associated
13120 type, update TYPE using some information only available in DIE's
13121 children. In particular, the fields are computed. */
13122
13123 static void
13124 update_enumeration_type_from_children (struct die_info *die,
13125 struct type *type,
13126 struct dwarf2_cu *cu)
13127 {
13128 struct die_info *child_die;
13129 int unsigned_enum = 1;
13130 int flag_enum = 1;
13131
13132 auto_obstack obstack;
13133 std::vector<struct field> fields;
13134
13135 for (child_die = die->child;
13136 child_die != NULL && child_die->tag;
13137 child_die = child_die->sibling)
13138 {
13139 struct attribute *attr;
13140 LONGEST value;
13141 const gdb_byte *bytes;
13142 struct dwarf2_locexpr_baton *baton;
13143 const char *name;
13144
13145 if (child_die->tag != DW_TAG_enumerator)
13146 continue;
13147
13148 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13149 if (attr == NULL)
13150 continue;
13151
13152 name = dwarf2_name (child_die, cu);
13153 if (name == NULL)
13154 name = "<anonymous enumerator>";
13155
13156 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13157 &value, &bytes, &baton);
13158 if (value < 0)
13159 {
13160 unsigned_enum = 0;
13161 flag_enum = 0;
13162 }
13163 else
13164 {
13165 if (count_one_bits_ll (value) >= 2)
13166 flag_enum = 0;
13167 }
13168
13169 fields.emplace_back ();
13170 struct field &field = fields.back ();
13171 field.set_name (dwarf2_physname (name, child_die, cu));
13172 field.set_loc_enumval (value);
13173 }
13174
13175 if (!fields.empty ())
13176 {
13177 type->set_num_fields (fields.size ());
13178 type->set_fields
13179 ((struct field *)
13180 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
13181 memcpy (type->fields (), fields.data (),
13182 sizeof (struct field) * fields.size ());
13183 }
13184 else
13185 flag_enum = 0;
13186
13187 if (unsigned_enum)
13188 type->set_is_unsigned (true);
13189
13190 if (flag_enum)
13191 type->set_is_flag_enum (true);
13192 }
13193
13194 /* Given a DW_AT_enumeration_type die, set its type. We do not
13195 complete the type's fields yet, or create any symbols. */
13196
13197 static struct type *
13198 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13199 {
13200 struct objfile *objfile = cu->per_objfile->objfile;
13201 struct type *type;
13202 struct attribute *attr;
13203 const char *name;
13204
13205 /* If the definition of this type lives in .debug_types, read that type.
13206 Don't follow DW_AT_specification though, that will take us back up
13207 the chain and we want to go down. */
13208 attr = die->attr (DW_AT_signature);
13209 if (attr != nullptr)
13210 {
13211 type = get_DW_AT_signature_type (die, attr, cu);
13212
13213 /* The type's CU may not be the same as CU.
13214 Ensure TYPE is recorded with CU in die_type_hash. */
13215 return set_die_type (die, type, cu);
13216 }
13217
13218 type = alloc_type (objfile);
13219
13220 type->set_code (TYPE_CODE_ENUM);
13221 name = dwarf2_full_name (NULL, die, cu);
13222 if (name != NULL)
13223 type->set_name (name);
13224
13225 attr = dwarf2_attr (die, DW_AT_type, cu);
13226 if (attr != NULL)
13227 {
13228 struct type *underlying_type = die_type (die, cu);
13229
13230 type->set_target_type (underlying_type);
13231 }
13232
13233 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13234 if (attr != nullptr)
13235 type->set_length (attr->constant_value (0));
13236 else
13237 type->set_length (0);
13238
13239 maybe_set_alignment (cu, die, type);
13240
13241 /* The enumeration DIE can be incomplete. In Ada, any type can be
13242 declared as private in the package spec, and then defined only
13243 inside the package body. Such types are known as Taft Amendment
13244 Types. When another package uses such a type, an incomplete DIE
13245 may be generated by the compiler. */
13246 if (die_is_declaration (die, cu))
13247 type->set_is_stub (true);
13248
13249 /* If this type has an underlying type that is not a stub, then we
13250 may use its attributes. We always use the "unsigned" attribute
13251 in this situation, because ordinarily we guess whether the type
13252 is unsigned -- but the guess can be wrong and the underlying type
13253 can tell us the reality. However, we defer to a local size
13254 attribute if one exists, because this lets the compiler override
13255 the underlying type if needed. */
13256 if (type->target_type () != NULL && !type->target_type ()->is_stub ())
13257 {
13258 struct type *underlying_type = type->target_type ();
13259 underlying_type = check_typedef (underlying_type);
13260
13261 type->set_is_unsigned (underlying_type->is_unsigned ());
13262
13263 if (type->length () == 0)
13264 type->set_length (underlying_type->length ());
13265
13266 if (TYPE_RAW_ALIGN (type) == 0
13267 && TYPE_RAW_ALIGN (underlying_type) != 0)
13268 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
13269 }
13270
13271 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
13272
13273 set_die_type (die, type, cu);
13274
13275 /* Finish the creation of this type by using the enum's children.
13276 Note that, as usual, this must come after set_die_type to avoid
13277 infinite recursion when trying to compute the names of the
13278 enumerators. */
13279 update_enumeration_type_from_children (die, type, cu);
13280
13281 return type;
13282 }
13283
13284 /* Given a pointer to a die which begins an enumeration, process all
13285 the dies that define the members of the enumeration, and create the
13286 symbol for the enumeration type.
13287
13288 NOTE: We reverse the order of the element list. */
13289
13290 static void
13291 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13292 {
13293 struct type *this_type;
13294
13295 this_type = get_die_type (die, cu);
13296 if (this_type == NULL)
13297 this_type = read_enumeration_type (die, cu);
13298
13299 if (die->child != NULL)
13300 {
13301 struct die_info *child_die;
13302 const char *name;
13303
13304 child_die = die->child;
13305 while (child_die && child_die->tag)
13306 {
13307 if (child_die->tag != DW_TAG_enumerator)
13308 {
13309 process_die (child_die, cu);
13310 }
13311 else
13312 {
13313 name = dwarf2_name (child_die, cu);
13314 if (name)
13315 new_symbol (child_die, this_type, cu);
13316 }
13317
13318 child_die = child_die->sibling;
13319 }
13320 }
13321
13322 /* If we are reading an enum from a .debug_types unit, and the enum
13323 is a declaration, and the enum is not the signatured type in the
13324 unit, then we do not want to add a symbol for it. Adding a
13325 symbol would in some cases obscure the true definition of the
13326 enum, giving users an incomplete type when the definition is
13327 actually available. Note that we do not want to do this for all
13328 enums which are just declarations, because C++0x allows forward
13329 enum declarations. */
13330 if (cu->per_cu->is_debug_types
13331 && die_is_declaration (die, cu))
13332 {
13333 struct signatured_type *sig_type;
13334
13335 sig_type = (struct signatured_type *) cu->per_cu;
13336 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
13337 if (sig_type->type_offset_in_section != die->sect_off)
13338 return;
13339 }
13340
13341 new_symbol (die, this_type, cu);
13342 }
13343
13344 /* Helper function for quirk_ada_thick_pointer that examines a bounds
13345 expression for an index type and finds the corresponding field
13346 offset in the hidden "P_BOUNDS" structure. Returns true on success
13347 and updates *FIELD, false if it fails to recognize an
13348 expression. */
13349
13350 static bool
13351 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
13352 int *bounds_offset, struct field *field,
13353 struct dwarf2_cu *cu)
13354 {
13355 struct attribute *attr = dwarf2_attr (die, name, cu);
13356 if (attr == nullptr || !attr->form_is_block ())
13357 return false;
13358
13359 const struct dwarf_block *block = attr->as_block ();
13360 const gdb_byte *start = block->data;
13361 const gdb_byte *end = block->data + block->size;
13362
13363 /* The expression to recognize generally looks like:
13364
13365 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13366 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13367
13368 However, the second "plus_uconst" may be missing:
13369
13370 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13371 DW_OP_deref_size: 4)
13372
13373 This happens when the field is at the start of the structure.
13374
13375 Also, the final deref may not be sized:
13376
13377 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
13378 DW_OP_deref)
13379
13380 This happens when the size of the index type happens to be the
13381 same as the architecture's word size. This can occur with or
13382 without the second plus_uconst. */
13383
13384 if (end - start < 2)
13385 return false;
13386 if (*start++ != DW_OP_push_object_address)
13387 return false;
13388 if (*start++ != DW_OP_plus_uconst)
13389 return false;
13390
13391 uint64_t this_bound_off;
13392 start = gdb_read_uleb128 (start, end, &this_bound_off);
13393 if (start == nullptr || (int) this_bound_off != this_bound_off)
13394 return false;
13395 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
13396 is consistent among all bounds. */
13397 if (*bounds_offset == -1)
13398 *bounds_offset = this_bound_off;
13399 else if (*bounds_offset != this_bound_off)
13400 return false;
13401
13402 if (start == end || *start++ != DW_OP_deref)
13403 return false;
13404
13405 int offset = 0;
13406 if (start ==end)
13407 return false;
13408 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
13409 {
13410 /* This means an offset of 0. */
13411 }
13412 else if (*start++ != DW_OP_plus_uconst)
13413 return false;
13414 else
13415 {
13416 /* The size is the parameter to DW_OP_plus_uconst. */
13417 uint64_t val;
13418 start = gdb_read_uleb128 (start, end, &val);
13419 if (start == nullptr)
13420 return false;
13421 if ((int) val != val)
13422 return false;
13423 offset = val;
13424 }
13425
13426 if (start == end)
13427 return false;
13428
13429 uint64_t size;
13430 if (*start == DW_OP_deref_size)
13431 {
13432 start = gdb_read_uleb128 (start + 1, end, &size);
13433 if (start == nullptr)
13434 return false;
13435 }
13436 else if (*start == DW_OP_deref)
13437 {
13438 size = cu->header.addr_size;
13439 ++start;
13440 }
13441 else
13442 return false;
13443
13444 field->set_loc_bitpos (8 * offset);
13445 if (size != field->type ()->length ())
13446 FIELD_BITSIZE (*field) = 8 * size;
13447
13448 return true;
13449 }
13450
13451 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
13452 some kinds of Ada arrays:
13453
13454 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
13455 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
13456 <11e0> DW_AT_data_location: 2 byte block: 97 6
13457 (DW_OP_push_object_address; DW_OP_deref)
13458 <11e3> DW_AT_type : <0x1173>
13459 <11e7> DW_AT_sibling : <0x1201>
13460 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
13461 <11ec> DW_AT_type : <0x1206>
13462 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
13463 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13464 DW_OP_deref_size: 4)
13465 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
13466 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13467 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13468
13469 This actually represents a "thick pointer", which is a structure
13470 with two elements: one that is a pointer to the array data, and one
13471 that is a pointer to another structure; this second structure holds
13472 the array bounds.
13473
13474 This returns a new type on success, or nullptr if this didn't
13475 recognize the type. */
13476
13477 static struct type *
13478 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
13479 struct type *type)
13480 {
13481 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
13482 /* So far we've only seen this with block form. */
13483 if (attr == nullptr || !attr->form_is_block ())
13484 return nullptr;
13485
13486 /* Note that this will fail if the structure layout is changed by
13487 the compiler. However, we have no good way to recognize some
13488 other layout, because we don't know what expression the compiler
13489 might choose to emit should this happen. */
13490 struct dwarf_block *blk = attr->as_block ();
13491 if (blk->size != 2
13492 || blk->data[0] != DW_OP_push_object_address
13493 || blk->data[1] != DW_OP_deref)
13494 return nullptr;
13495
13496 int bounds_offset = -1;
13497 int max_align = -1;
13498 std::vector<struct field> range_fields;
13499 for (struct die_info *child_die = die->child;
13500 child_die;
13501 child_die = child_die->sibling)
13502 {
13503 if (child_die->tag == DW_TAG_subrange_type)
13504 {
13505 struct type *underlying = read_subrange_index_type (child_die, cu);
13506
13507 int this_align = type_align (underlying);
13508 if (this_align > max_align)
13509 max_align = this_align;
13510
13511 range_fields.emplace_back ();
13512 range_fields.emplace_back ();
13513
13514 struct field &lower = range_fields[range_fields.size () - 2];
13515 struct field &upper = range_fields[range_fields.size () - 1];
13516
13517 lower.set_type (underlying);
13518 FIELD_ARTIFICIAL (lower) = 1;
13519
13520 upper.set_type (underlying);
13521 FIELD_ARTIFICIAL (upper) = 1;
13522
13523 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
13524 &bounds_offset, &lower, cu)
13525 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
13526 &bounds_offset, &upper, cu))
13527 return nullptr;
13528 }
13529 }
13530
13531 /* This shouldn't really happen, but double-check that we found
13532 where the bounds are stored. */
13533 if (bounds_offset == -1)
13534 return nullptr;
13535
13536 struct objfile *objfile = cu->per_objfile->objfile;
13537 for (int i = 0; i < range_fields.size (); i += 2)
13538 {
13539 char name[20];
13540
13541 /* Set the name of each field in the bounds. */
13542 xsnprintf (name, sizeof (name), "LB%d", i / 2);
13543 range_fields[i].set_name (objfile->intern (name));
13544 xsnprintf (name, sizeof (name), "UB%d", i / 2);
13545 range_fields[i + 1].set_name (objfile->intern (name));
13546 }
13547
13548 struct type *bounds = alloc_type (objfile);
13549 bounds->set_code (TYPE_CODE_STRUCT);
13550
13551 bounds->set_num_fields (range_fields.size ());
13552 bounds->set_fields
13553 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
13554 * sizeof (struct field))));
13555 memcpy (bounds->fields (), range_fields.data (),
13556 bounds->num_fields () * sizeof (struct field));
13557
13558 int last_fieldno = range_fields.size () - 1;
13559 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
13560 + bounds->field (last_fieldno).type ()->length ());
13561 bounds->set_length (align_up (bounds_size, max_align));
13562
13563 /* Rewrite the existing array type in place. Specifically, we
13564 remove any dynamic properties we might have read, and we replace
13565 the index types. */
13566 struct type *iter = type;
13567 for (int i = 0; i < range_fields.size (); i += 2)
13568 {
13569 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
13570 iter->main_type->dyn_prop_list = nullptr;
13571 iter->set_index_type
13572 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
13573 iter = iter->target_type ();
13574 }
13575
13576 struct type *result = alloc_type (objfile);
13577 result->set_code (TYPE_CODE_STRUCT);
13578
13579 result->set_num_fields (2);
13580 result->set_fields
13581 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
13582 * sizeof (struct field))));
13583
13584 /* The names are chosen to coincide with what the compiler does with
13585 -fgnat-encodings=all, which the Ada code in gdb already
13586 understands. */
13587 result->field (0).set_name ("P_ARRAY");
13588 result->field (0).set_type (lookup_pointer_type (type));
13589
13590 result->field (1).set_name ("P_BOUNDS");
13591 result->field (1).set_type (lookup_pointer_type (bounds));
13592 result->field (1).set_loc_bitpos (8 * bounds_offset);
13593
13594 result->set_name (type->name ());
13595 result->set_length (result->field (0).type ()->length ()
13596 + result->field (1).type ()->length ());
13597
13598 return result;
13599 }
13600
13601 /* Extract all information from a DW_TAG_array_type DIE and put it in
13602 the DIE's type field. For now, this only handles one dimensional
13603 arrays. */
13604
13605 static struct type *
13606 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13607 {
13608 struct objfile *objfile = cu->per_objfile->objfile;
13609 struct die_info *child_die;
13610 struct type *type;
13611 struct type *element_type, *range_type, *index_type;
13612 struct attribute *attr;
13613 const char *name;
13614 struct dynamic_prop *byte_stride_prop = NULL;
13615 unsigned int bit_stride = 0;
13616
13617 element_type = die_type (die, cu);
13618
13619 /* The die_type call above may have already set the type for this DIE. */
13620 type = get_die_type (die, cu);
13621 if (type)
13622 return type;
13623
13624 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13625 if (attr != NULL)
13626 {
13627 int stride_ok;
13628 struct type *prop_type = cu->addr_sized_int_type (false);
13629
13630 byte_stride_prop
13631 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
13632 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
13633 prop_type);
13634 if (!stride_ok)
13635 {
13636 complaint (_("unable to read array DW_AT_byte_stride "
13637 " - DIE at %s [in module %s]"),
13638 sect_offset_str (die->sect_off),
13639 objfile_name (cu->per_objfile->objfile));
13640 /* Ignore this attribute. We will likely not be able to print
13641 arrays of this type correctly, but there is little we can do
13642 to help if we cannot read the attribute's value. */
13643 byte_stride_prop = NULL;
13644 }
13645 }
13646
13647 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13648 if (attr != NULL)
13649 bit_stride = attr->constant_value (0);
13650
13651 /* Irix 6.2 native cc creates array types without children for
13652 arrays with unspecified length. */
13653 if (die->child == NULL)
13654 {
13655 index_type = objfile_type (objfile)->builtin_int;
13656 range_type = create_static_range_type (NULL, index_type, 0, -1);
13657 type = create_array_type_with_stride (NULL, element_type, range_type,
13658 byte_stride_prop, bit_stride);
13659 return set_die_type (die, type, cu);
13660 }
13661
13662 std::vector<struct type *> range_types;
13663 child_die = die->child;
13664 while (child_die && child_die->tag)
13665 {
13666 if (child_die->tag == DW_TAG_subrange_type
13667 || child_die->tag == DW_TAG_generic_subrange)
13668 {
13669 struct type *child_type = read_type_die (child_die, cu);
13670
13671 if (child_type != NULL)
13672 {
13673 /* The range type was succesfully read. Save it for the
13674 array type creation. */
13675 range_types.push_back (child_type);
13676 }
13677 }
13678 child_die = child_die->sibling;
13679 }
13680
13681 if (range_types.empty ())
13682 {
13683 complaint (_("unable to find array range - DIE at %s [in module %s]"),
13684 sect_offset_str (die->sect_off),
13685 objfile_name (cu->per_objfile->objfile));
13686 return NULL;
13687 }
13688
13689 /* Dwarf2 dimensions are output from left to right, create the
13690 necessary array types in backwards order. */
13691
13692 type = element_type;
13693
13694 if (read_array_order (die, cu) == DW_ORD_col_major)
13695 {
13696 int i = 0;
13697
13698 while (i < range_types.size ())
13699 {
13700 type = create_array_type_with_stride (NULL, type, range_types[i++],
13701 byte_stride_prop, bit_stride);
13702 type->set_is_multi_dimensional (true);
13703 bit_stride = 0;
13704 byte_stride_prop = nullptr;
13705 }
13706 }
13707 else
13708 {
13709 size_t ndim = range_types.size ();
13710 while (ndim-- > 0)
13711 {
13712 type = create_array_type_with_stride (NULL, type, range_types[ndim],
13713 byte_stride_prop, bit_stride);
13714 type->set_is_multi_dimensional (true);
13715 bit_stride = 0;
13716 byte_stride_prop = nullptr;
13717 }
13718 }
13719
13720 /* Clear the flag on the outermost array type. */
13721 type->set_is_multi_dimensional (false);
13722 gdb_assert (type != element_type);
13723
13724 /* Understand Dwarf2 support for vector types (like they occur on
13725 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13726 array type. This is not part of the Dwarf2/3 standard yet, but a
13727 custom vendor extension. The main difference between a regular
13728 array and the vector variant is that vectors are passed by value
13729 to functions. */
13730 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13731 if (attr != nullptr)
13732 make_vector_type (type);
13733
13734 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13735 implementation may choose to implement triple vectors using this
13736 attribute. */
13737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13738 if (attr != nullptr && attr->form_is_unsigned ())
13739 {
13740 if (attr->as_unsigned () >= type->length ())
13741 type->set_length (attr->as_unsigned ());
13742 else
13743 complaint (_("DW_AT_byte_size for array type smaller "
13744 "than the total size of elements"));
13745 }
13746
13747 name = dwarf2_name (die, cu);
13748 if (name)
13749 type->set_name (name);
13750
13751 maybe_set_alignment (cu, die, type);
13752
13753 struct type *replacement_type = nullptr;
13754 if (cu->lang () == language_ada)
13755 {
13756 replacement_type = quirk_ada_thick_pointer (die, cu, type);
13757 if (replacement_type != nullptr)
13758 type = replacement_type;
13759 }
13760
13761 /* Install the type in the die. */
13762 set_die_type (die, type, cu, replacement_type != nullptr);
13763
13764 /* set_die_type should be already done. */
13765 set_descriptive_type (type, die, cu);
13766
13767 return type;
13768 }
13769
13770 static enum dwarf_array_dim_ordering
13771 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13772 {
13773 struct attribute *attr;
13774
13775 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13776
13777 if (attr != nullptr)
13778 {
13779 LONGEST val = attr->constant_value (-1);
13780 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
13781 return (enum dwarf_array_dim_ordering) val;
13782 }
13783
13784 /* GNU F77 is a special case, as at 08/2004 array type info is the
13785 opposite order to the dwarf2 specification, but data is still
13786 laid out as per normal fortran.
13787
13788 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13789 version checking. */
13790
13791 if (cu->lang () == language_fortran
13792 && cu->producer && strstr (cu->producer, "GNU F77"))
13793 {
13794 return DW_ORD_row_major;
13795 }
13796
13797 switch (cu->language_defn->array_ordering ())
13798 {
13799 case array_column_major:
13800 return DW_ORD_col_major;
13801 case array_row_major:
13802 default:
13803 return DW_ORD_row_major;
13804 };
13805 }
13806
13807 /* Extract all information from a DW_TAG_set_type DIE and put it in
13808 the DIE's type field. */
13809
13810 static struct type *
13811 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13812 {
13813 struct type *domain_type, *set_type;
13814 struct attribute *attr;
13815
13816 domain_type = die_type (die, cu);
13817
13818 /* The die_type call above may have already set the type for this DIE. */
13819 set_type = get_die_type (die, cu);
13820 if (set_type)
13821 return set_type;
13822
13823 set_type = create_set_type (NULL, domain_type);
13824
13825 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13826 if (attr != nullptr && attr->form_is_unsigned ())
13827 set_type->set_length (attr->as_unsigned ());
13828
13829 maybe_set_alignment (cu, die, set_type);
13830
13831 return set_die_type (die, set_type, cu);
13832 }
13833
13834 /* A helper for read_common_block that creates a locexpr baton.
13835 SYM is the symbol which we are marking as computed.
13836 COMMON_DIE is the DIE for the common block.
13837 COMMON_LOC is the location expression attribute for the common
13838 block itself.
13839 MEMBER_LOC is the location expression attribute for the particular
13840 member of the common block that we are processing.
13841 CU is the CU from which the above come. */
13842
13843 static void
13844 mark_common_block_symbol_computed (struct symbol *sym,
13845 struct die_info *common_die,
13846 struct attribute *common_loc,
13847 struct attribute *member_loc,
13848 struct dwarf2_cu *cu)
13849 {
13850 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13851 struct objfile *objfile = per_objfile->objfile;
13852 struct dwarf2_locexpr_baton *baton;
13853 gdb_byte *ptr;
13854 unsigned int cu_off;
13855 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
13856 LONGEST offset = 0;
13857
13858 gdb_assert (common_loc && member_loc);
13859 gdb_assert (common_loc->form_is_block ());
13860 gdb_assert (member_loc->form_is_block ()
13861 || member_loc->form_is_constant ());
13862
13863 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13864 baton->per_objfile = per_objfile;
13865 baton->per_cu = cu->per_cu;
13866 gdb_assert (baton->per_cu);
13867
13868 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13869
13870 if (member_loc->form_is_constant ())
13871 {
13872 offset = member_loc->constant_value (0);
13873 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13874 }
13875 else
13876 baton->size += member_loc->as_block ()->size;
13877
13878 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
13879 baton->data = ptr;
13880
13881 *ptr++ = DW_OP_call4;
13882 cu_off = common_die->sect_off - cu->per_cu->sect_off;
13883 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13884 ptr += 4;
13885
13886 if (member_loc->form_is_constant ())
13887 {
13888 *ptr++ = DW_OP_addr;
13889 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13890 ptr += cu->header.addr_size;
13891 }
13892 else
13893 {
13894 /* We have to copy the data here, because DW_OP_call4 will only
13895 use a DW_AT_location attribute. */
13896 struct dwarf_block *block = member_loc->as_block ();
13897 memcpy (ptr, block->data, block->size);
13898 ptr += block->size;
13899 }
13900
13901 *ptr++ = DW_OP_plus;
13902 gdb_assert (ptr - baton->data == baton->size);
13903
13904 SYMBOL_LOCATION_BATON (sym) = baton;
13905 sym->set_aclass_index (dwarf2_locexpr_index);
13906 }
13907
13908 /* Create appropriate locally-scoped variables for all the
13909 DW_TAG_common_block entries. Also create a struct common_block
13910 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13911 is used to separate the common blocks name namespace from regular
13912 variable names. */
13913
13914 static void
13915 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13916 {
13917 struct attribute *attr;
13918
13919 attr = dwarf2_attr (die, DW_AT_location, cu);
13920 if (attr != nullptr)
13921 {
13922 /* Support the .debug_loc offsets. */
13923 if (attr->form_is_block ())
13924 {
13925 /* Ok. */
13926 }
13927 else if (attr->form_is_section_offset ())
13928 {
13929 dwarf2_complex_location_expr_complaint ();
13930 attr = NULL;
13931 }
13932 else
13933 {
13934 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13935 "common block member");
13936 attr = NULL;
13937 }
13938 }
13939
13940 if (die->child != NULL)
13941 {
13942 struct objfile *objfile = cu->per_objfile->objfile;
13943 struct die_info *child_die;
13944 size_t n_entries = 0, size;
13945 struct common_block *common_block;
13946 struct symbol *sym;
13947
13948 for (child_die = die->child;
13949 child_die && child_die->tag;
13950 child_die = child_die->sibling)
13951 ++n_entries;
13952
13953 size = (sizeof (struct common_block)
13954 + (n_entries - 1) * sizeof (struct symbol *));
13955 common_block
13956 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
13957 size);
13958 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13959 common_block->n_entries = 0;
13960
13961 for (child_die = die->child;
13962 child_die && child_die->tag;
13963 child_die = child_die->sibling)
13964 {
13965 /* Create the symbol in the DW_TAG_common_block block in the current
13966 symbol scope. */
13967 sym = new_symbol (child_die, NULL, cu);
13968 if (sym != NULL)
13969 {
13970 struct attribute *member_loc;
13971
13972 common_block->contents[common_block->n_entries++] = sym;
13973
13974 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13975 cu);
13976 if (member_loc)
13977 {
13978 /* GDB has handled this for a long time, but it is
13979 not specified by DWARF. It seems to have been
13980 emitted by gfortran at least as recently as:
13981 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
13982 complaint (_("Variable in common block has "
13983 "DW_AT_data_member_location "
13984 "- DIE at %s [in module %s]"),
13985 sect_offset_str (child_die->sect_off),
13986 objfile_name (objfile));
13987
13988 if (member_loc->form_is_section_offset ())
13989 dwarf2_complex_location_expr_complaint ();
13990 else if (member_loc->form_is_constant ()
13991 || member_loc->form_is_block ())
13992 {
13993 if (attr != nullptr)
13994 mark_common_block_symbol_computed (sym, die, attr,
13995 member_loc, cu);
13996 }
13997 else
13998 dwarf2_complex_location_expr_complaint ();
13999 }
14000 }
14001 }
14002
14003 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14004 sym->set_value_common_block (common_block);
14005 }
14006 }
14007
14008 /* Create a type for a C++ namespace. */
14009
14010 static struct type *
14011 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14012 {
14013 struct objfile *objfile = cu->per_objfile->objfile;
14014 const char *previous_prefix, *name;
14015 int is_anonymous;
14016 struct type *type;
14017
14018 /* For extensions, reuse the type of the original namespace. */
14019 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14020 {
14021 struct die_info *ext_die;
14022 struct dwarf2_cu *ext_cu = cu;
14023
14024 ext_die = dwarf2_extension (die, &ext_cu);
14025 type = read_type_die (ext_die, ext_cu);
14026
14027 /* EXT_CU may not be the same as CU.
14028 Ensure TYPE is recorded with CU in die_type_hash. */
14029 return set_die_type (die, type, cu);
14030 }
14031
14032 name = namespace_name (die, &is_anonymous, cu);
14033
14034 /* Now build the name of the current namespace. */
14035
14036 previous_prefix = determine_prefix (die, cu);
14037 if (previous_prefix[0] != '\0')
14038 name = typename_concat (&objfile->objfile_obstack,
14039 previous_prefix, name, 0, cu);
14040
14041 /* Create the type. */
14042 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14043
14044 return set_die_type (die, type, cu);
14045 }
14046
14047 /* Read a namespace scope. */
14048
14049 static void
14050 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14051 {
14052 struct objfile *objfile = cu->per_objfile->objfile;
14053 int is_anonymous;
14054
14055 /* Add a symbol associated to this if we haven't seen the namespace
14056 before. Also, add a using directive if it's an anonymous
14057 namespace. */
14058
14059 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14060 {
14061 struct type *type;
14062
14063 type = read_type_die (die, cu);
14064 new_symbol (die, type, cu);
14065
14066 namespace_name (die, &is_anonymous, cu);
14067 if (is_anonymous)
14068 {
14069 const char *previous_prefix = determine_prefix (die, cu);
14070
14071 std::vector<const char *> excludes;
14072 add_using_directive (using_directives (cu),
14073 previous_prefix, type->name (), NULL,
14074 NULL, excludes,
14075 read_decl_line (die, cu),
14076 0, &objfile->objfile_obstack);
14077 }
14078 }
14079
14080 if (die->child != NULL)
14081 {
14082 struct die_info *child_die = die->child;
14083
14084 while (child_die && child_die->tag)
14085 {
14086 process_die (child_die, cu);
14087 child_die = child_die->sibling;
14088 }
14089 }
14090 }
14091
14092 /* Read a Fortran module as type. This DIE can be only a declaration used for
14093 imported module. Still we need that type as local Fortran "use ... only"
14094 declaration imports depend on the created type in determine_prefix. */
14095
14096 static struct type *
14097 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14098 {
14099 struct objfile *objfile = cu->per_objfile->objfile;
14100 const char *module_name;
14101 struct type *type;
14102
14103 module_name = dwarf2_name (die, cu);
14104 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14105
14106 return set_die_type (die, type, cu);
14107 }
14108
14109 /* Read a Fortran module. */
14110
14111 static void
14112 read_module (struct die_info *die, struct dwarf2_cu *cu)
14113 {
14114 struct die_info *child_die = die->child;
14115 struct type *type;
14116
14117 type = read_type_die (die, cu);
14118 new_symbol (die, type, cu);
14119
14120 while (child_die && child_die->tag)
14121 {
14122 process_die (child_die, cu);
14123 child_die = child_die->sibling;
14124 }
14125 }
14126
14127 /* Return the name of the namespace represented by DIE. Set
14128 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14129 namespace. */
14130
14131 static const char *
14132 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14133 {
14134 struct die_info *current_die;
14135 const char *name = NULL;
14136
14137 /* Loop through the extensions until we find a name. */
14138
14139 for (current_die = die;
14140 current_die != NULL;
14141 current_die = dwarf2_extension (die, &cu))
14142 {
14143 /* We don't use dwarf2_name here so that we can detect the absence
14144 of a name -> anonymous namespace. */
14145 name = dwarf2_string_attr (die, DW_AT_name, cu);
14146
14147 if (name != NULL)
14148 break;
14149 }
14150
14151 /* Is it an anonymous namespace? */
14152
14153 *is_anonymous = (name == NULL);
14154 if (*is_anonymous)
14155 name = CP_ANONYMOUS_NAMESPACE_STR;
14156
14157 return name;
14158 }
14159
14160 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14161 the user defined type vector. */
14162
14163 static struct type *
14164 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14165 {
14166 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
14167 struct comp_unit_head *cu_header = &cu->header;
14168 struct type *type;
14169 struct attribute *attr_byte_size;
14170 struct attribute *attr_address_class;
14171 int byte_size, addr_class;
14172 struct type *target_type;
14173
14174 target_type = die_type (die, cu);
14175
14176 /* The die_type call above may have already set the type for this DIE. */
14177 type = get_die_type (die, cu);
14178 if (type)
14179 return type;
14180
14181 type = lookup_pointer_type (target_type);
14182
14183 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14184 if (attr_byte_size)
14185 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
14186 else
14187 byte_size = cu_header->addr_size;
14188
14189 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14190 if (attr_address_class)
14191 addr_class = attr_address_class->constant_value (DW_ADDR_none);
14192 else
14193 addr_class = DW_ADDR_none;
14194
14195 ULONGEST alignment = get_alignment (cu, die);
14196
14197 /* If the pointer size, alignment, or address class is different
14198 than the default, create a type variant marked as such and set
14199 the length accordingly. */
14200 if (type->length () != byte_size
14201 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
14202 && alignment != TYPE_RAW_ALIGN (type))
14203 || addr_class != DW_ADDR_none)
14204 {
14205 if (gdbarch_address_class_type_flags_p (gdbarch))
14206 {
14207 type_instance_flags type_flags
14208 = gdbarch_address_class_type_flags (gdbarch, byte_size,
14209 addr_class);
14210 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14211 == 0);
14212 type = make_type_with_address_space (type, type_flags);
14213 }
14214 else if (type->length () != byte_size)
14215 {
14216 complaint (_("invalid pointer size %d"), byte_size);
14217 }
14218 else if (TYPE_RAW_ALIGN (type) != alignment)
14219 {
14220 complaint (_("Invalid DW_AT_alignment"
14221 " - DIE at %s [in module %s]"),
14222 sect_offset_str (die->sect_off),
14223 objfile_name (cu->per_objfile->objfile));
14224 }
14225 else
14226 {
14227 /* Should we also complain about unhandled address classes? */
14228 }
14229 }
14230
14231 type->set_length (byte_size);
14232 set_type_align (type, alignment);
14233 return set_die_type (die, type, cu);
14234 }
14235
14236 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14237 the user defined type vector. */
14238
14239 static struct type *
14240 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14241 {
14242 struct type *type;
14243 struct type *to_type;
14244 struct type *domain;
14245
14246 to_type = die_type (die, cu);
14247 domain = die_containing_type (die, cu);
14248
14249 /* The calls above may have already set the type for this DIE. */
14250 type = get_die_type (die, cu);
14251 if (type)
14252 return type;
14253
14254 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
14255 type = lookup_methodptr_type (to_type);
14256 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
14257 {
14258 struct type *new_type = alloc_type (cu->per_objfile->objfile);
14259
14260 smash_to_method_type (new_type, domain, to_type->target_type (),
14261 to_type->fields (), to_type->num_fields (),
14262 to_type->has_varargs ());
14263 type = lookup_methodptr_type (new_type);
14264 }
14265 else
14266 type = lookup_memberptr_type (to_type, domain);
14267
14268 return set_die_type (die, type, cu);
14269 }
14270
14271 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14272 the user defined type vector. */
14273
14274 static struct type *
14275 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14276 enum type_code refcode)
14277 {
14278 struct comp_unit_head *cu_header = &cu->header;
14279 struct type *type, *target_type;
14280 struct attribute *attr;
14281
14282 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14283
14284 target_type = die_type (die, cu);
14285
14286 /* The die_type call above may have already set the type for this DIE. */
14287 type = get_die_type (die, cu);
14288 if (type)
14289 return type;
14290
14291 type = lookup_reference_type (target_type, refcode);
14292 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14293 if (attr != nullptr)
14294 type->set_length (attr->constant_value (cu_header->addr_size));
14295 else
14296 type->set_length (cu_header->addr_size);
14297
14298 maybe_set_alignment (cu, die, type);
14299 return set_die_type (die, type, cu);
14300 }
14301
14302 /* Add the given cv-qualifiers to the element type of the array. GCC
14303 outputs DWARF type qualifiers that apply to an array, not the
14304 element type. But GDB relies on the array element type to carry
14305 the cv-qualifiers. This mimics section 6.7.3 of the C99
14306 specification. */
14307
14308 static struct type *
14309 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14310 struct type *base_type, int cnst, int voltl)
14311 {
14312 struct type *el_type, *inner_array;
14313
14314 base_type = copy_type (base_type);
14315 inner_array = base_type;
14316
14317 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
14318 {
14319 inner_array->set_target_type (copy_type (inner_array->target_type ()));
14320 inner_array = inner_array->target_type ();
14321 }
14322
14323 el_type = inner_array->target_type ();
14324 cnst |= TYPE_CONST (el_type);
14325 voltl |= TYPE_VOLATILE (el_type);
14326 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
14327
14328 return set_die_type (die, base_type, cu);
14329 }
14330
14331 static struct type *
14332 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14333 {
14334 struct type *base_type, *cv_type;
14335
14336 base_type = die_type (die, cu);
14337
14338 /* The die_type call above may have already set the type for this DIE. */
14339 cv_type = get_die_type (die, cu);
14340 if (cv_type)
14341 return cv_type;
14342
14343 /* In case the const qualifier is applied to an array type, the element type
14344 is so qualified, not the array type (section 6.7.3 of C99). */
14345 if (base_type->code () == TYPE_CODE_ARRAY)
14346 return add_array_cv_type (die, cu, base_type, 1, 0);
14347
14348 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14349 return set_die_type (die, cv_type, cu);
14350 }
14351
14352 static struct type *
14353 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14354 {
14355 struct type *base_type, *cv_type;
14356
14357 base_type = die_type (die, cu);
14358
14359 /* The die_type call above may have already set the type for this DIE. */
14360 cv_type = get_die_type (die, cu);
14361 if (cv_type)
14362 return cv_type;
14363
14364 /* In case the volatile qualifier is applied to an array type, the
14365 element type is so qualified, not the array type (section 6.7.3
14366 of C99). */
14367 if (base_type->code () == TYPE_CODE_ARRAY)
14368 return add_array_cv_type (die, cu, base_type, 0, 1);
14369
14370 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14371 return set_die_type (die, cv_type, cu);
14372 }
14373
14374 /* Handle DW_TAG_restrict_type. */
14375
14376 static struct type *
14377 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14378 {
14379 struct type *base_type, *cv_type;
14380
14381 base_type = die_type (die, cu);
14382
14383 /* The die_type call above may have already set the type for this DIE. */
14384 cv_type = get_die_type (die, cu);
14385 if (cv_type)
14386 return cv_type;
14387
14388 cv_type = make_restrict_type (base_type);
14389 return set_die_type (die, cv_type, cu);
14390 }
14391
14392 /* Handle DW_TAG_atomic_type. */
14393
14394 static struct type *
14395 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14396 {
14397 struct type *base_type, *cv_type;
14398
14399 base_type = die_type (die, cu);
14400
14401 /* The die_type call above may have already set the type for this DIE. */
14402 cv_type = get_die_type (die, cu);
14403 if (cv_type)
14404 return cv_type;
14405
14406 cv_type = make_atomic_type (base_type);
14407 return set_die_type (die, cv_type, cu);
14408 }
14409
14410 /* Extract all information from a DW_TAG_string_type DIE and add to
14411 the user defined type vector. It isn't really a user defined type,
14412 but it behaves like one, with other DIE's using an AT_user_def_type
14413 attribute to reference it. */
14414
14415 static struct type *
14416 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14417 {
14418 struct objfile *objfile = cu->per_objfile->objfile;
14419 struct gdbarch *gdbarch = objfile->arch ();
14420 struct type *type, *range_type, *index_type, *char_type;
14421 struct attribute *attr;
14422 struct dynamic_prop prop;
14423 bool length_is_constant = true;
14424 LONGEST length;
14425
14426 /* There are a couple of places where bit sizes might be made use of
14427 when parsing a DW_TAG_string_type, however, no producer that we know
14428 of make use of these. Handling bit sizes that are a multiple of the
14429 byte size is easy enough, but what about other bit sizes? Lets deal
14430 with that problem when we have to. Warn about these attributes being
14431 unsupported, then parse the type and ignore them like we always
14432 have. */
14433 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
14434 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
14435 {
14436 static bool warning_printed = false;
14437 if (!warning_printed)
14438 {
14439 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
14440 "currently supported on DW_TAG_string_type."));
14441 warning_printed = true;
14442 }
14443 }
14444
14445 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14446 if (attr != nullptr && !attr->form_is_constant ())
14447 {
14448 /* The string length describes the location at which the length of
14449 the string can be found. The size of the length field can be
14450 specified with one of the attributes below. */
14451 struct type *prop_type;
14452 struct attribute *len
14453 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
14454 if (len == nullptr)
14455 len = dwarf2_attr (die, DW_AT_byte_size, cu);
14456 if (len != nullptr && len->form_is_constant ())
14457 {
14458 /* Pass 0 as the default as we know this attribute is constant
14459 and the default value will not be returned. */
14460 LONGEST sz = len->constant_value (0);
14461 prop_type = objfile_int_type (objfile, sz, true);
14462 }
14463 else
14464 {
14465 /* If the size is not specified then we assume it is the size of
14466 an address on this target. */
14467 prop_type = cu->addr_sized_int_type (true);
14468 }
14469
14470 /* Convert the attribute into a dynamic property. */
14471 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
14472 length = 1;
14473 else
14474 length_is_constant = false;
14475 }
14476 else if (attr != nullptr)
14477 {
14478 /* This DW_AT_string_length just contains the length with no
14479 indirection. There's no need to create a dynamic property in this
14480 case. Pass 0 for the default value as we know it will not be
14481 returned in this case. */
14482 length = attr->constant_value (0);
14483 }
14484 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
14485 {
14486 /* We don't currently support non-constant byte sizes for strings. */
14487 length = attr->constant_value (1);
14488 }
14489 else
14490 {
14491 /* Use 1 as a fallback length if we have nothing else. */
14492 length = 1;
14493 }
14494
14495 index_type = objfile_type (objfile)->builtin_int;
14496 if (length_is_constant)
14497 range_type = create_static_range_type (NULL, index_type, 1, length);
14498 else
14499 {
14500 struct dynamic_prop low_bound;
14501
14502 low_bound.set_const_val (1);
14503 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
14504 }
14505 char_type = language_string_char_type (cu->language_defn, gdbarch);
14506 type = create_string_type (NULL, char_type, range_type);
14507
14508 return set_die_type (die, type, cu);
14509 }
14510
14511 /* Assuming that DIE corresponds to a function, returns nonzero
14512 if the function is prototyped. */
14513
14514 static int
14515 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14516 {
14517 struct attribute *attr;
14518
14519 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14520 if (attr && attr->as_boolean ())
14521 return 1;
14522
14523 /* The DWARF standard implies that the DW_AT_prototyped attribute
14524 is only meaningful for C, but the concept also extends to other
14525 languages that allow unprototyped functions (Eg: Objective C).
14526 For all other languages, assume that functions are always
14527 prototyped. */
14528 if (cu->lang () != language_c
14529 && cu->lang () != language_objc
14530 && cu->lang () != language_opencl)
14531 return 1;
14532
14533 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14534 prototyped and unprototyped functions; default to prototyped,
14535 since that is more common in modern code (and RealView warns
14536 about unprototyped functions). */
14537 if (producer_is_realview (cu->producer))
14538 return 1;
14539
14540 return 0;
14541 }
14542
14543 /* Handle DIES due to C code like:
14544
14545 struct foo
14546 {
14547 int (*funcp)(int a, long l);
14548 int b;
14549 };
14550
14551 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14552
14553 static struct type *
14554 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14555 {
14556 struct objfile *objfile = cu->per_objfile->objfile;
14557 struct type *type; /* Type that this function returns. */
14558 struct type *ftype; /* Function that returns above type. */
14559 struct attribute *attr;
14560
14561 type = die_type (die, cu);
14562
14563 /* The die_type call above may have already set the type for this DIE. */
14564 ftype = get_die_type (die, cu);
14565 if (ftype)
14566 return ftype;
14567
14568 ftype = lookup_function_type (type);
14569
14570 if (prototyped_function_p (die, cu))
14571 ftype->set_is_prototyped (true);
14572
14573 /* Store the calling convention in the type if it's available in
14574 the subroutine die. Otherwise set the calling convention to
14575 the default value DW_CC_normal. */
14576 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14577 if (attr != nullptr
14578 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
14579 TYPE_CALLING_CONVENTION (ftype)
14580 = (enum dwarf_calling_convention) attr->constant_value (0);
14581 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14582 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14583 else
14584 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14585
14586 /* Record whether the function returns normally to its caller or not
14587 if the DWARF producer set that information. */
14588 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14589 if (attr && attr->as_boolean ())
14590 TYPE_NO_RETURN (ftype) = 1;
14591
14592 /* We need to add the subroutine type to the die immediately so
14593 we don't infinitely recurse when dealing with parameters
14594 declared as the same subroutine type. */
14595 set_die_type (die, ftype, cu);
14596
14597 if (die->child != NULL)
14598 {
14599 struct type *void_type = objfile_type (objfile)->builtin_void;
14600 struct die_info *child_die;
14601 int nparams, iparams;
14602
14603 /* Count the number of parameters.
14604 FIXME: GDB currently ignores vararg functions, but knows about
14605 vararg member functions. */
14606 nparams = 0;
14607 child_die = die->child;
14608 while (child_die && child_die->tag)
14609 {
14610 if (child_die->tag == DW_TAG_formal_parameter)
14611 nparams++;
14612 else if (child_die->tag == DW_TAG_unspecified_parameters)
14613 ftype->set_has_varargs (true);
14614
14615 child_die = child_die->sibling;
14616 }
14617
14618 /* Allocate storage for parameters and fill them in. */
14619 ftype->set_num_fields (nparams);
14620 ftype->set_fields
14621 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
14622
14623 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14624 even if we error out during the parameters reading below. */
14625 for (iparams = 0; iparams < nparams; iparams++)
14626 ftype->field (iparams).set_type (void_type);
14627
14628 iparams = 0;
14629 child_die = die->child;
14630 while (child_die && child_die->tag)
14631 {
14632 if (child_die->tag == DW_TAG_formal_parameter)
14633 {
14634 struct type *arg_type;
14635
14636 /* DWARF version 2 has no clean way to discern C++
14637 static and non-static member functions. G++ helps
14638 GDB by marking the first parameter for non-static
14639 member functions (which is the this pointer) as
14640 artificial. We pass this information to
14641 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14642
14643 DWARF version 3 added DW_AT_object_pointer, which GCC
14644 4.5 does not yet generate. */
14645 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14646 if (attr != nullptr)
14647 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
14648 else
14649 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14650 arg_type = die_type (child_die, cu);
14651
14652 /* RealView does not mark THIS as const, which the testsuite
14653 expects. GCC marks THIS as const in method definitions,
14654 but not in the class specifications (GCC PR 43053). */
14655 if (cu->lang () == language_cplus
14656 && !TYPE_CONST (arg_type)
14657 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14658 {
14659 int is_this = 0;
14660 struct dwarf2_cu *arg_cu = cu;
14661 const char *name = dwarf2_name (child_die, cu);
14662
14663 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14664 if (attr != nullptr)
14665 {
14666 /* If the compiler emits this, use it. */
14667 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14668 is_this = 1;
14669 }
14670 else if (name && strcmp (name, "this") == 0)
14671 /* Function definitions will have the argument names. */
14672 is_this = 1;
14673 else if (name == NULL && iparams == 0)
14674 /* Declarations may not have the names, so like
14675 elsewhere in GDB, assume an artificial first
14676 argument is "this". */
14677 is_this = 1;
14678
14679 if (is_this)
14680 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14681 arg_type, 0);
14682 }
14683
14684 ftype->field (iparams).set_type (arg_type);
14685 iparams++;
14686 }
14687 child_die = child_die->sibling;
14688 }
14689 }
14690
14691 return ftype;
14692 }
14693
14694 static struct type *
14695 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14696 {
14697 struct objfile *objfile = cu->per_objfile->objfile;
14698 const char *name = NULL;
14699 struct type *this_type, *target_type;
14700
14701 name = dwarf2_full_name (NULL, die, cu);
14702 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
14703 this_type->set_target_is_stub (true);
14704 set_die_type (die, this_type, cu);
14705 target_type = die_type (die, cu);
14706 if (target_type != this_type)
14707 this_type->set_target_type (target_type);
14708 else
14709 {
14710 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14711 spec and cause infinite loops in GDB. */
14712 complaint (_("Self-referential DW_TAG_typedef "
14713 "- DIE at %s [in module %s]"),
14714 sect_offset_str (die->sect_off), objfile_name (objfile));
14715 this_type->set_target_type (nullptr);
14716 }
14717 if (name == NULL)
14718 {
14719 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
14720 anonymous typedefs, which is, strictly speaking, invalid DWARF.
14721 Handle these by just returning the target type, rather than
14722 constructing an anonymous typedef type and trying to handle this
14723 elsewhere. */
14724 set_die_type (die, target_type, cu);
14725 return target_type;
14726 }
14727 return this_type;
14728 }
14729
14730 /* Helper for get_dwarf2_rational_constant that computes the value of
14731 a given gmp_mpz given an attribute. */
14732
14733 static void
14734 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
14735 {
14736 /* GCC will sometimes emit a 16-byte constant value as a DWARF
14737 location expression that pushes an implicit value. */
14738 if (attr->form == DW_FORM_exprloc)
14739 {
14740 dwarf_block *blk = attr->as_block ();
14741 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
14742 {
14743 uint64_t len;
14744 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
14745 blk->data + blk->size,
14746 &len);
14747 if (ptr - blk->data + len <= blk->size)
14748 {
14749 mpz_import (value->val, len,
14750 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14751 ? 1 : -1,
14752 1, 0, 0, ptr);
14753 return;
14754 }
14755 }
14756
14757 /* On failure set it to 1. */
14758 *value = gdb_mpz (1);
14759 }
14760 else if (attr->form_is_block ())
14761 {
14762 dwarf_block *blk = attr->as_block ();
14763 mpz_import (value->val, blk->size,
14764 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14765 ? 1 : -1,
14766 1, 0, 0, blk->data);
14767 }
14768 else
14769 *value = gdb_mpz (attr->constant_value (1));
14770 }
14771
14772 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
14773 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
14774
14775 If the numerator and/or numerator attribute is missing,
14776 a complaint is filed, and NUMERATOR and DENOMINATOR are left
14777 untouched. */
14778
14779 static void
14780 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
14781 gdb_mpz *numerator, gdb_mpz *denominator)
14782 {
14783 struct attribute *num_attr, *denom_attr;
14784
14785 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
14786 if (num_attr == nullptr)
14787 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
14788 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14789
14790 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
14791 if (denom_attr == nullptr)
14792 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
14793 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14794
14795 if (num_attr == nullptr || denom_attr == nullptr)
14796 return;
14797
14798 get_mpz (cu, numerator, num_attr);
14799 get_mpz (cu, denominator, denom_attr);
14800 }
14801
14802 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
14803 rational constant, rather than a signed one.
14804
14805 If the rational constant has a negative value, a complaint
14806 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
14807
14808 static void
14809 get_dwarf2_unsigned_rational_constant (struct die_info *die,
14810 struct dwarf2_cu *cu,
14811 gdb_mpz *numerator,
14812 gdb_mpz *denominator)
14813 {
14814 gdb_mpz num (1);
14815 gdb_mpz denom (1);
14816
14817 get_dwarf2_rational_constant (die, cu, &num, &denom);
14818 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
14819 {
14820 mpz_neg (num.val, num.val);
14821 mpz_neg (denom.val, denom.val);
14822 }
14823 else if (mpz_sgn (num.val) == -1)
14824 {
14825 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
14826 " in DIE at %s"),
14827 sect_offset_str (die->sect_off));
14828 return;
14829 }
14830 else if (mpz_sgn (denom.val) == -1)
14831 {
14832 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
14833 " in DIE at %s"),
14834 sect_offset_str (die->sect_off));
14835 return;
14836 }
14837
14838 *numerator = std::move (num);
14839 *denominator = std::move (denom);
14840 }
14841
14842 /* Assuming that ENCODING is a string whose contents starting at the
14843 K'th character is "_nn" where "nn" is a decimal number, scan that
14844 number and set RESULT to the value. K is updated to point to the
14845 character immediately following the number.
14846
14847 If the string does not conform to the format described above, false
14848 is returned, and K may or may not be changed. */
14849
14850 static bool
14851 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
14852 {
14853 /* The next character should be an underscore ('_') followed
14854 by a digit. */
14855 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
14856 return false;
14857
14858 /* Skip the underscore. */
14859 k++;
14860 int start = k;
14861
14862 /* Determine the number of digits for our number. */
14863 while (isdigit (encoding[k]))
14864 k++;
14865 if (k == start)
14866 return false;
14867
14868 std::string copy (&encoding[start], k - start);
14869 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
14870 return false;
14871
14872 return true;
14873 }
14874
14875 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
14876 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
14877 DENOM, update OFFSET, and return true on success. Return false on
14878 failure. */
14879
14880 static bool
14881 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
14882 gdb_mpz *num, gdb_mpz *denom)
14883 {
14884 if (!ada_get_gnat_encoded_number (encoding, offset, num))
14885 return false;
14886 return ada_get_gnat_encoded_number (encoding, offset, denom);
14887 }
14888
14889 /* Assuming DIE corresponds to a fixed point type, finish the creation
14890 of the corresponding TYPE by setting its type-specific data. CU is
14891 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
14892 encodings. It is nullptr if the GNAT encoding should be
14893 ignored. */
14894
14895 static void
14896 finish_fixed_point_type (struct type *type, const char *suffix,
14897 struct die_info *die, struct dwarf2_cu *cu)
14898 {
14899 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
14900 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
14901
14902 /* If GNAT encodings are preferred, don't examine the
14903 attributes. */
14904 struct attribute *attr = nullptr;
14905 if (suffix == nullptr)
14906 {
14907 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
14908 if (attr == nullptr)
14909 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
14910 if (attr == nullptr)
14911 attr = dwarf2_attr (die, DW_AT_small, cu);
14912 }
14913
14914 /* Numerator and denominator of our fixed-point type's scaling factor.
14915 The default is a scaling factor of 1, which we use as a fallback
14916 when we are not able to decode it (problem with the debugging info,
14917 unsupported forms, bug in GDB, etc...). Using that as the default
14918 allows us to at least print the unscaled value, which might still
14919 be useful to a user. */
14920 gdb_mpz scale_num (1);
14921 gdb_mpz scale_denom (1);
14922
14923 if (attr == nullptr)
14924 {
14925 int offset = 0;
14926 if (suffix != nullptr
14927 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14928 &scale_denom)
14929 /* The number might be encoded as _nn_dd_nn_dd, where the
14930 second ratio is the 'small value. In this situation, we
14931 want the second value. */
14932 && (suffix[offset] != '_'
14933 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14934 &scale_denom)))
14935 {
14936 /* Found it. */
14937 }
14938 else
14939 {
14940 /* Scaling factor not found. Assume a scaling factor of 1,
14941 and hope for the best. At least the user will be able to
14942 see the encoded value. */
14943 scale_num = 1;
14944 scale_denom = 1;
14945 complaint (_("no scale found for fixed-point type (DIE at %s)"),
14946 sect_offset_str (die->sect_off));
14947 }
14948 }
14949 else if (attr->name == DW_AT_binary_scale)
14950 {
14951 LONGEST scale_exp = attr->constant_value (0);
14952 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
14953
14954 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
14955 }
14956 else if (attr->name == DW_AT_decimal_scale)
14957 {
14958 LONGEST scale_exp = attr->constant_value (0);
14959 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
14960
14961 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
14962 }
14963 else if (attr->name == DW_AT_small)
14964 {
14965 struct die_info *scale_die;
14966 struct dwarf2_cu *scale_cu = cu;
14967
14968 scale_die = follow_die_ref (die, attr, &scale_cu);
14969 if (scale_die->tag == DW_TAG_constant)
14970 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
14971 &scale_num, &scale_denom);
14972 else
14973 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
14974 " (DIE at %s)"),
14975 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14976 }
14977 else
14978 {
14979 complaint (_("unsupported scale attribute %s for fixed-point type"
14980 " (DIE at %s)"),
14981 dwarf_attr_name (attr->name),
14982 sect_offset_str (die->sect_off));
14983 }
14984
14985 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
14986 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
14987 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
14988 mpq_canonicalize (scaling_factor.val);
14989 }
14990
14991 /* The gnat-encoding suffix for fixed point. */
14992
14993 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
14994
14995 /* If NAME encodes an Ada fixed-point type, return a pointer to the
14996 "XF" suffix of the name. The text after this is what encodes the
14997 'small and 'delta information. Otherwise, return nullptr. */
14998
14999 static const char *
15000 gnat_encoded_fixed_point_type_info (const char *name)
15001 {
15002 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
15003 }
15004
15005 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15006 (which may be different from NAME) to the architecture back-end to allow
15007 it to guess the correct format if necessary. */
15008
15009 static struct type *
15010 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15011 const char *name_hint, enum bfd_endian byte_order)
15012 {
15013 struct gdbarch *gdbarch = objfile->arch ();
15014 const struct floatformat **format;
15015 struct type *type;
15016
15017 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15018 if (format)
15019 type = init_float_type (objfile, bits, name, format, byte_order);
15020 else
15021 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15022
15023 return type;
15024 }
15025
15026 /* Allocate an integer type of size BITS and name NAME. */
15027
15028 static struct type *
15029 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
15030 int bits, int unsigned_p, const char *name)
15031 {
15032 struct type *type;
15033
15034 /* Versions of Intel's C Compiler generate an integer type called "void"
15035 instead of using DW_TAG_unspecified_type. This has been seen on
15036 at least versions 14, 17, and 18. */
15037 if (bits == 0 && producer_is_icc (cu) && name != nullptr
15038 && strcmp (name, "void") == 0)
15039 type = objfile_type (objfile)->builtin_void;
15040 else
15041 type = init_integer_type (objfile, bits, unsigned_p, name);
15042
15043 return type;
15044 }
15045
15046 /* Return true if DIE has a DW_AT_small attribute whose value is
15047 a constant rational, where both the numerator and denominator
15048 are equal to zero.
15049
15050 CU is the DIE's Compilation Unit. */
15051
15052 static bool
15053 has_zero_over_zero_small_attribute (struct die_info *die,
15054 struct dwarf2_cu *cu)
15055 {
15056 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
15057 if (attr == nullptr)
15058 return false;
15059
15060 struct dwarf2_cu *scale_cu = cu;
15061 struct die_info *scale_die
15062 = follow_die_ref (die, attr, &scale_cu);
15063
15064 if (scale_die->tag != DW_TAG_constant)
15065 return false;
15066
15067 gdb_mpz num (1), denom (1);
15068 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
15069 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
15070 }
15071
15072 /* Initialise and return a floating point type of size BITS suitable for
15073 use as a component of a complex number. The NAME_HINT is passed through
15074 when initialising the floating point type and is the name of the complex
15075 type.
15076
15077 As DWARF doesn't currently provide an explicit name for the components
15078 of a complex number, but it can be helpful to have these components
15079 named, we try to select a suitable name based on the size of the
15080 component. */
15081 static struct type *
15082 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
15083 struct objfile *objfile,
15084 int bits, const char *name_hint,
15085 enum bfd_endian byte_order)
15086 {
15087 gdbarch *gdbarch = objfile->arch ();
15088 struct type *tt = nullptr;
15089
15090 /* Try to find a suitable floating point builtin type of size BITS.
15091 We're going to use the name of this type as the name for the complex
15092 target type that we are about to create. */
15093 switch (cu->lang ())
15094 {
15095 case language_fortran:
15096 switch (bits)
15097 {
15098 case 32:
15099 tt = builtin_f_type (gdbarch)->builtin_real;
15100 break;
15101 case 64:
15102 tt = builtin_f_type (gdbarch)->builtin_real_s8;
15103 break;
15104 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15105 case 128:
15106 tt = builtin_f_type (gdbarch)->builtin_real_s16;
15107 break;
15108 }
15109 break;
15110 default:
15111 switch (bits)
15112 {
15113 case 32:
15114 tt = builtin_type (gdbarch)->builtin_float;
15115 break;
15116 case 64:
15117 tt = builtin_type (gdbarch)->builtin_double;
15118 break;
15119 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15120 case 128:
15121 tt = builtin_type (gdbarch)->builtin_long_double;
15122 break;
15123 }
15124 break;
15125 }
15126
15127 /* If the type we found doesn't match the size we were looking for, then
15128 pretend we didn't find a type at all, the complex target type we
15129 create will then be nameless. */
15130 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
15131 tt = nullptr;
15132
15133 const char *name = (tt == nullptr) ? nullptr : tt->name ();
15134 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
15135 }
15136
15137 /* Find a representation of a given base type and install
15138 it in the TYPE field of the die. */
15139
15140 static struct type *
15141 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15142 {
15143 struct objfile *objfile = cu->per_objfile->objfile;
15144 struct type *type;
15145 struct attribute *attr;
15146 int encoding = 0, bits = 0;
15147 const char *name;
15148 gdbarch *arch;
15149
15150 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15151 if (attr != nullptr && attr->form_is_constant ())
15152 encoding = attr->constant_value (0);
15153 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15154 if (attr != nullptr)
15155 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
15156 name = dwarf2_name (die, cu);
15157 if (!name)
15158 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
15159
15160 arch = objfile->arch ();
15161 enum bfd_endian byte_order = gdbarch_byte_order (arch);
15162
15163 attr = dwarf2_attr (die, DW_AT_endianity, cu);
15164 if (attr != nullptr && attr->form_is_constant ())
15165 {
15166 int endianity = attr->constant_value (0);
15167
15168 switch (endianity)
15169 {
15170 case DW_END_big:
15171 byte_order = BFD_ENDIAN_BIG;
15172 break;
15173 case DW_END_little:
15174 byte_order = BFD_ENDIAN_LITTLE;
15175 break;
15176 default:
15177 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
15178 break;
15179 }
15180 }
15181
15182 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
15183 && cu->lang () == language_ada
15184 && has_zero_over_zero_small_attribute (die, cu))
15185 {
15186 /* brobecker/2018-02-24: This is a fixed point type for which
15187 the scaling factor is represented as fraction whose value
15188 does not make sense (zero divided by zero), so we should
15189 normally never see these. However, there is a small category
15190 of fixed point types for which GNAT is unable to provide
15191 the scaling factor via the standard DWARF mechanisms, and
15192 for which the info is provided via the GNAT encodings instead.
15193 This is likely what this DIE is about. */
15194 encoding = (encoding == DW_ATE_signed_fixed
15195 ? DW_ATE_signed
15196 : DW_ATE_unsigned);
15197 }
15198
15199 /* With GNAT encodings, fixed-point information will be encoded in
15200 the type name. Note that this can also occur with the above
15201 zero-over-zero case, which is why this is a separate "if" rather
15202 than an "else if". */
15203 const char *gnat_encoding_suffix = nullptr;
15204 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
15205 && cu->lang () == language_ada
15206 && name != nullptr)
15207 {
15208 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
15209 if (gnat_encoding_suffix != nullptr)
15210 {
15211 gdb_assert (startswith (gnat_encoding_suffix,
15212 GNAT_FIXED_POINT_SUFFIX));
15213 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
15214 name, gnat_encoding_suffix - name);
15215 /* Use -1 here so that SUFFIX points at the "_" after the
15216 "XF". */
15217 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
15218
15219 encoding = (encoding == DW_ATE_signed
15220 ? DW_ATE_signed_fixed
15221 : DW_ATE_unsigned_fixed);
15222 }
15223 }
15224
15225 switch (encoding)
15226 {
15227 case DW_ATE_address:
15228 /* Turn DW_ATE_address into a void * pointer. */
15229 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
15230 type = init_pointer_type (objfile, bits, name, type);
15231 break;
15232 case DW_ATE_boolean:
15233 type = init_boolean_type (objfile, bits, 1, name);
15234 break;
15235 case DW_ATE_complex_float:
15236 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
15237 byte_order);
15238 if (type->code () == TYPE_CODE_ERROR)
15239 {
15240 if (name == nullptr)
15241 {
15242 struct obstack *obstack
15243 = &cu->per_objfile->objfile->objfile_obstack;
15244 name = obconcat (obstack, "_Complex ", type->name (),
15245 nullptr);
15246 }
15247 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15248 }
15249 else
15250 type = init_complex_type (name, type);
15251 break;
15252 case DW_ATE_decimal_float:
15253 type = init_decfloat_type (objfile, bits, name);
15254 break;
15255 case DW_ATE_float:
15256 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
15257 break;
15258 case DW_ATE_signed:
15259 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
15260 break;
15261 case DW_ATE_unsigned:
15262 if (cu->lang () == language_fortran
15263 && name
15264 && startswith (name, "character("))
15265 type = init_character_type (objfile, bits, 1, name);
15266 else
15267 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
15268 break;
15269 case DW_ATE_signed_char:
15270 if (cu->lang () == language_ada
15271 || cu->lang () == language_m2
15272 || cu->lang () == language_pascal
15273 || cu->lang () == language_fortran)
15274 type = init_character_type (objfile, bits, 0, name);
15275 else
15276 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
15277 break;
15278 case DW_ATE_unsigned_char:
15279 if (cu->lang () == language_ada
15280 || cu->lang () == language_m2
15281 || cu->lang () == language_pascal
15282 || cu->lang () == language_fortran
15283 || cu->lang () == language_rust)
15284 type = init_character_type (objfile, bits, 1, name);
15285 else
15286 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
15287 break;
15288 case DW_ATE_UTF:
15289 {
15290 type = init_character_type (objfile, bits, 1, name);
15291 return set_die_type (die, type, cu);
15292 }
15293 break;
15294 case DW_ATE_signed_fixed:
15295 type = init_fixed_point_type (objfile, bits, 0, name);
15296 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15297 break;
15298 case DW_ATE_unsigned_fixed:
15299 type = init_fixed_point_type (objfile, bits, 1, name);
15300 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15301 break;
15302
15303 default:
15304 complaint (_("unsupported DW_AT_encoding: '%s'"),
15305 dwarf_type_encoding_name (encoding));
15306 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
15307 break;
15308 }
15309
15310 if (type->code () == TYPE_CODE_INT
15311 && name != nullptr
15312 && strcmp (name, "char") == 0)
15313 type->set_has_no_signedness (true);
15314
15315 maybe_set_alignment (cu, die, type);
15316
15317 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
15318
15319 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
15320 {
15321 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15322 if (attr != nullptr && attr->as_unsigned () <= 8 * type->length ())
15323 {
15324 unsigned real_bit_size = attr->as_unsigned ();
15325 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15326 /* Only use the attributes if they make sense together. */
15327 if (attr == nullptr
15328 || (attr->as_unsigned () + real_bit_size
15329 <= 8 * type->length ()))
15330 {
15331 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
15332 = real_bit_size;
15333 if (attr != nullptr)
15334 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
15335 = attr->as_unsigned ();
15336 }
15337 }
15338 }
15339
15340 return set_die_type (die, type, cu);
15341 }
15342
15343 /* A helper function that returns the name of DIE, if it refers to a
15344 variable declaration. */
15345
15346 static const char *
15347 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
15348 {
15349 if (die->tag != DW_TAG_variable)
15350 return nullptr;
15351
15352 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
15353 if (attr == nullptr || !attr->as_boolean ())
15354 return nullptr;
15355
15356 attr = dwarf2_attr (die, DW_AT_name, cu);
15357 if (attr == nullptr)
15358 return nullptr;
15359 return attr->as_string ();
15360 }
15361
15362 /* Parse dwarf attribute if it's a block, reference or constant and put the
15363 resulting value of the attribute into struct bound_prop.
15364 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15365
15366 static int
15367 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15368 struct dwarf2_cu *cu, struct dynamic_prop *prop,
15369 struct type *default_type)
15370 {
15371 struct dwarf2_property_baton *baton;
15372 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15373 struct objfile *objfile = per_objfile->objfile;
15374 struct obstack *obstack = &objfile->objfile_obstack;
15375
15376 gdb_assert (default_type != NULL);
15377
15378 if (attr == NULL || prop == NULL)
15379 return 0;
15380
15381 if (attr->form_is_block ())
15382 {
15383 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15384 baton->property_type = default_type;
15385 baton->locexpr.per_cu = cu->per_cu;
15386 baton->locexpr.per_objfile = per_objfile;
15387
15388 struct dwarf_block *block;
15389 if (attr->form == DW_FORM_data16)
15390 {
15391 size_t data_size = 16;
15392 block = XOBNEW (obstack, struct dwarf_block);
15393 block->size = (data_size
15394 + 2 /* Extra bytes for DW_OP and arg. */);
15395 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
15396 data[0] = DW_OP_implicit_value;
15397 data[1] = data_size;
15398 memcpy (&data[2], attr->as_block ()->data, data_size);
15399 block->data = data;
15400 }
15401 else
15402 block = attr->as_block ();
15403
15404 baton->locexpr.size = block->size;
15405 baton->locexpr.data = block->data;
15406 switch (attr->name)
15407 {
15408 case DW_AT_string_length:
15409 baton->locexpr.is_reference = true;
15410 break;
15411 default:
15412 baton->locexpr.is_reference = false;
15413 break;
15414 }
15415
15416 prop->set_locexpr (baton);
15417 gdb_assert (prop->baton () != NULL);
15418 }
15419 else if (attr->form_is_ref ())
15420 {
15421 struct dwarf2_cu *target_cu = cu;
15422 struct die_info *target_die;
15423 struct attribute *target_attr;
15424
15425 target_die = follow_die_ref (die, attr, &target_cu);
15426 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15427 if (target_attr == NULL)
15428 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15429 target_cu);
15430 if (target_attr == nullptr)
15431 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
15432 target_cu);
15433 if (target_attr == NULL)
15434 {
15435 const char *name = var_decl_name (target_die, target_cu);
15436 if (name != nullptr)
15437 {
15438 prop->set_variable_name (name);
15439 return 1;
15440 }
15441 return 0;
15442 }
15443
15444 switch (target_attr->name)
15445 {
15446 case DW_AT_location:
15447 if (target_attr->form_is_section_offset ())
15448 {
15449 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15450 baton->property_type = die_type (target_die, target_cu);
15451 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15452 prop->set_loclist (baton);
15453 gdb_assert (prop->baton () != NULL);
15454 }
15455 else if (target_attr->form_is_block ())
15456 {
15457 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15458 baton->property_type = die_type (target_die, target_cu);
15459 baton->locexpr.per_cu = cu->per_cu;
15460 baton->locexpr.per_objfile = per_objfile;
15461 struct dwarf_block *block = target_attr->as_block ();
15462 baton->locexpr.size = block->size;
15463 baton->locexpr.data = block->data;
15464 baton->locexpr.is_reference = true;
15465 prop->set_locexpr (baton);
15466 gdb_assert (prop->baton () != NULL);
15467 }
15468 else
15469 {
15470 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15471 "dynamic property");
15472 return 0;
15473 }
15474 break;
15475 case DW_AT_data_member_location:
15476 case DW_AT_data_bit_offset:
15477 {
15478 LONGEST offset;
15479
15480 if (!handle_member_location (target_die, target_cu, &offset))
15481 return 0;
15482
15483 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15484 baton->property_type = read_type_die (target_die->parent,
15485 target_cu);
15486 baton->offset_info.offset = offset;
15487 baton->offset_info.type = die_type (target_die, target_cu);
15488 prop->set_addr_offset (baton);
15489 break;
15490 }
15491 }
15492 }
15493 else if (attr->form_is_constant ())
15494 prop->set_const_val (attr->constant_value (0));
15495 else if (attr->form_is_section_offset ())
15496 {
15497 switch (attr->name)
15498 {
15499 case DW_AT_string_length:
15500 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15501 baton->property_type = default_type;
15502 fill_in_loclist_baton (cu, &baton->loclist, attr);
15503 prop->set_loclist (baton);
15504 gdb_assert (prop->baton () != NULL);
15505 break;
15506 default:
15507 goto invalid;
15508 }
15509 }
15510 else
15511 goto invalid;
15512
15513 return 1;
15514
15515 invalid:
15516 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15517 dwarf2_name (die, cu));
15518 return 0;
15519 }
15520
15521 /* See read.h. */
15522
15523 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
15524 present (which is valid) then compute the default type based on the
15525 compilation units address size. */
15526
15527 static struct type *
15528 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
15529 {
15530 struct type *index_type = die_type (die, cu);
15531
15532 /* Dwarf-2 specifications explicitly allows to create subrange types
15533 without specifying a base type.
15534 In that case, the base type must be set to the type of
15535 the lower bound, upper bound or count, in that order, if any of these
15536 three attributes references an object that has a type.
15537 If no base type is found, the Dwarf-2 specifications say that
15538 a signed integer type of size equal to the size of an address should
15539 be used.
15540 For the following C code: `extern char gdb_int [];'
15541 GCC produces an empty range DIE.
15542 FIXME: muller/2010-05-28: Possible references to object for low bound,
15543 high bound or count are not yet handled by this code. */
15544 if (index_type->code () == TYPE_CODE_VOID)
15545 index_type = cu->addr_sized_int_type (false);
15546
15547 return index_type;
15548 }
15549
15550 /* Read the given DW_AT_subrange DIE. */
15551
15552 static struct type *
15553 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15554 {
15555 struct type *base_type, *orig_base_type;
15556 struct type *range_type;
15557 struct attribute *attr;
15558 struct dynamic_prop low, high;
15559 int low_default_is_valid;
15560 int high_bound_is_count = 0;
15561 const char *name;
15562
15563 orig_base_type = read_subrange_index_type (die, cu);
15564
15565 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15566 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15567 creating the range type, but we use the result of check_typedef
15568 when examining properties of the type. */
15569 base_type = check_typedef (orig_base_type);
15570
15571 /* The die_type call above may have already set the type for this DIE. */
15572 range_type = get_die_type (die, cu);
15573 if (range_type)
15574 return range_type;
15575
15576 high.set_const_val (0);
15577
15578 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15579 omitting DW_AT_lower_bound. */
15580 switch (cu->lang ())
15581 {
15582 case language_c:
15583 case language_cplus:
15584 low.set_const_val (0);
15585 low_default_is_valid = 1;
15586 break;
15587 case language_fortran:
15588 low.set_const_val (1);
15589 low_default_is_valid = 1;
15590 break;
15591 case language_d:
15592 case language_objc:
15593 case language_rust:
15594 low.set_const_val (0);
15595 low_default_is_valid = (cu->header.version >= 4);
15596 break;
15597 case language_ada:
15598 case language_m2:
15599 case language_pascal:
15600 low.set_const_val (1);
15601 low_default_is_valid = (cu->header.version >= 4);
15602 break;
15603 default:
15604 low.set_const_val (0);
15605 low_default_is_valid = 0;
15606 break;
15607 }
15608
15609 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15610 if (attr != nullptr)
15611 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
15612 else if (!low_default_is_valid)
15613 complaint (_("Missing DW_AT_lower_bound "
15614 "- DIE at %s [in module %s]"),
15615 sect_offset_str (die->sect_off),
15616 objfile_name (cu->per_objfile->objfile));
15617
15618 struct attribute *attr_ub, *attr_count;
15619 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
15620 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15621 {
15622 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
15623 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15624 {
15625 /* If bounds are constant do the final calculation here. */
15626 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
15627 high.set_const_val (low.const_val () + high.const_val () - 1);
15628 else
15629 high_bound_is_count = 1;
15630 }
15631 else
15632 {
15633 if (attr_ub != NULL)
15634 complaint (_("Unresolved DW_AT_upper_bound "
15635 "- DIE at %s [in module %s]"),
15636 sect_offset_str (die->sect_off),
15637 objfile_name (cu->per_objfile->objfile));
15638 if (attr_count != NULL)
15639 complaint (_("Unresolved DW_AT_count "
15640 "- DIE at %s [in module %s]"),
15641 sect_offset_str (die->sect_off),
15642 objfile_name (cu->per_objfile->objfile));
15643 }
15644 }
15645
15646 LONGEST bias = 0;
15647 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
15648 if (bias_attr != nullptr && bias_attr->form_is_constant ())
15649 bias = bias_attr->constant_value (0);
15650
15651 /* Normally, the DWARF producers are expected to use a signed
15652 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15653 But this is unfortunately not always the case, as witnessed
15654 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15655 is used instead. To work around that ambiguity, we treat
15656 the bounds as signed, and thus sign-extend their values, when
15657 the base type is signed.
15658
15659 Skip it if the base type's length is larger than ULONGEST, to avoid
15660 the undefined behavior of a too large left shift. We don't really handle
15661 constants larger than 8 bytes anyway, at the moment. */
15662
15663 if (base_type->length () <= sizeof (ULONGEST))
15664 {
15665 ULONGEST negative_mask
15666 = -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
15667
15668 if (low.kind () == PROP_CONST
15669 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
15670 low.set_const_val (low.const_val () | negative_mask);
15671
15672 if (high.kind () == PROP_CONST
15673 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
15674 high.set_const_val (high.const_val () | negative_mask);
15675 }
15676
15677 /* Check for bit and byte strides. */
15678 struct dynamic_prop byte_stride_prop;
15679 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
15680 if (attr_byte_stride != nullptr)
15681 {
15682 struct type *prop_type = cu->addr_sized_int_type (false);
15683 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
15684 prop_type);
15685 }
15686
15687 struct dynamic_prop bit_stride_prop;
15688 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
15689 if (attr_bit_stride != nullptr)
15690 {
15691 /* It only makes sense to have either a bit or byte stride. */
15692 if (attr_byte_stride != nullptr)
15693 {
15694 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
15695 "- DIE at %s [in module %s]"),
15696 sect_offset_str (die->sect_off),
15697 objfile_name (cu->per_objfile->objfile));
15698 attr_bit_stride = nullptr;
15699 }
15700 else
15701 {
15702 struct type *prop_type = cu->addr_sized_int_type (false);
15703 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
15704 prop_type);
15705 }
15706 }
15707
15708 if (attr_byte_stride != nullptr
15709 || attr_bit_stride != nullptr)
15710 {
15711 bool byte_stride_p = (attr_byte_stride != nullptr);
15712 struct dynamic_prop *stride
15713 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
15714
15715 range_type
15716 = create_range_type_with_stride (NULL, orig_base_type, &low,
15717 &high, bias, stride, byte_stride_p);
15718 }
15719 else
15720 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
15721
15722 if (high_bound_is_count)
15723 range_type->bounds ()->flag_upper_bound_is_count = 1;
15724
15725 /* Ada expects an empty array on no boundary attributes. */
15726 if (attr == NULL && cu->lang () != language_ada)
15727 range_type->bounds ()->high.set_undefined ();
15728
15729 name = dwarf2_name (die, cu);
15730 if (name)
15731 range_type->set_name (name);
15732
15733 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15734 if (attr != nullptr)
15735 range_type->set_length (attr->constant_value (0));
15736
15737 maybe_set_alignment (cu, die, range_type);
15738
15739 set_die_type (die, range_type, cu);
15740
15741 /* set_die_type should be already done. */
15742 set_descriptive_type (range_type, die, cu);
15743
15744 return range_type;
15745 }
15746
15747 static struct type *
15748 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15749 {
15750 struct type *type;
15751
15752 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
15753 type->set_name (dwarf2_name (die, cu));
15754
15755 /* In Ada, an unspecified type is typically used when the description
15756 of the type is deferred to a different unit. When encountering
15757 such a type, we treat it as a stub, and try to resolve it later on,
15758 when needed.
15759 Mark this as a stub type for all languages though. */
15760 type->set_is_stub (true);
15761
15762 return set_die_type (die, type, cu);
15763 }
15764
15765 /* Read a single die and all its descendents. Set the die's sibling
15766 field to NULL; set other fields in the die correctly, and set all
15767 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15768 location of the info_ptr after reading all of those dies. PARENT
15769 is the parent of the die in question. */
15770
15771 static struct die_info *
15772 read_die_and_children (const struct die_reader_specs *reader,
15773 const gdb_byte *info_ptr,
15774 const gdb_byte **new_info_ptr,
15775 struct die_info *parent)
15776 {
15777 struct die_info *die;
15778 const gdb_byte *cur_ptr;
15779
15780 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0, true);
15781 if (die == NULL)
15782 {
15783 *new_info_ptr = cur_ptr;
15784 return NULL;
15785 }
15786 store_in_ref_table (die, reader->cu);
15787
15788 if (die->has_children)
15789 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15790 else
15791 {
15792 die->child = NULL;
15793 *new_info_ptr = cur_ptr;
15794 }
15795
15796 die->sibling = NULL;
15797 die->parent = parent;
15798 return die;
15799 }
15800
15801 /* Read a die, all of its descendents, and all of its siblings; set
15802 all of the fields of all of the dies correctly. Arguments are as
15803 in read_die_and_children. */
15804
15805 static struct die_info *
15806 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15807 const gdb_byte *info_ptr,
15808 const gdb_byte **new_info_ptr,
15809 struct die_info *parent)
15810 {
15811 struct die_info *first_die, *last_sibling;
15812 const gdb_byte *cur_ptr;
15813
15814 cur_ptr = info_ptr;
15815 first_die = last_sibling = NULL;
15816
15817 while (1)
15818 {
15819 struct die_info *die
15820 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15821
15822 if (die == NULL)
15823 {
15824 *new_info_ptr = cur_ptr;
15825 return first_die;
15826 }
15827
15828 if (!first_die)
15829 first_die = die;
15830 else
15831 last_sibling->sibling = die;
15832
15833 last_sibling = die;
15834 }
15835 }
15836
15837 /* Read a die, all of its descendents, and all of its siblings; set
15838 all of the fields of all of the dies correctly. Arguments are as
15839 in read_die_and_children.
15840 This the main entry point for reading a DIE and all its children. */
15841
15842 static struct die_info *
15843 read_die_and_siblings (const struct die_reader_specs *reader,
15844 const gdb_byte *info_ptr,
15845 const gdb_byte **new_info_ptr,
15846 struct die_info *parent)
15847 {
15848 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15849 new_info_ptr, parent);
15850
15851 if (dwarf_die_debug)
15852 {
15853 gdb_printf (gdb_stdlog,
15854 "Read die from %s@0x%x of %s:\n",
15855 reader->die_section->get_name (),
15856 (unsigned) (info_ptr - reader->die_section->buffer),
15857 bfd_get_filename (reader->abfd));
15858 die->dump (dwarf_die_debug);
15859 }
15860
15861 return die;
15862 }
15863
15864 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15865 attributes.
15866 The caller is responsible for filling in the extra attributes
15867 and updating (*DIEP)->num_attrs.
15868 Set DIEP to point to a newly allocated die with its information,
15869 except for its child, sibling, and parent fields. */
15870
15871 static const gdb_byte *
15872 read_full_die_1 (const struct die_reader_specs *reader,
15873 struct die_info **diep, const gdb_byte *info_ptr,
15874 int num_extra_attrs, bool allow_reprocess)
15875 {
15876 unsigned int abbrev_number, bytes_read, i;
15877 const struct abbrev_info *abbrev;
15878 struct die_info *die;
15879 struct dwarf2_cu *cu = reader->cu;
15880 bfd *abfd = reader->abfd;
15881
15882 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15883 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15884 info_ptr += bytes_read;
15885 if (!abbrev_number)
15886 {
15887 *diep = NULL;
15888 return info_ptr;
15889 }
15890
15891 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
15892 if (!abbrev)
15893 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15894 abbrev_number,
15895 bfd_get_filename (abfd));
15896
15897 die = die_info::allocate (&cu->comp_unit_obstack,
15898 abbrev->num_attrs + num_extra_attrs);
15899 die->sect_off = sect_off;
15900 die->tag = abbrev->tag;
15901 die->abbrev = abbrev_number;
15902 die->has_children = abbrev->has_children;
15903
15904 /* Make the result usable.
15905 The caller needs to update num_attrs after adding the extra
15906 attributes. */
15907 die->num_attrs = abbrev->num_attrs;
15908
15909 for (i = 0; i < abbrev->num_attrs; ++i)
15910 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15911 info_ptr, allow_reprocess);
15912
15913 *diep = die;
15914 return info_ptr;
15915 }
15916
15917 /* Read a die and all its attributes.
15918 Set DIEP to point to a newly allocated die with its information,
15919 except for its child, sibling, and parent fields. */
15920
15921 static const gdb_byte *
15922 read_toplevel_die (const struct die_reader_specs *reader,
15923 struct die_info **diep, const gdb_byte *info_ptr,
15924 gdb::array_view<attribute *> extra_attrs)
15925 {
15926 const gdb_byte *result;
15927 struct dwarf2_cu *cu = reader->cu;
15928
15929 result = read_full_die_1 (reader, diep, info_ptr, extra_attrs.size (),
15930 false);
15931
15932 /* Copy in the extra attributes, if any. */
15933 attribute *next = &(*diep)->attrs[(*diep)->num_attrs];
15934 for (attribute *extra : extra_attrs)
15935 *next++ = *extra;
15936
15937 struct attribute *attr = (*diep)->attr (DW_AT_str_offsets_base);
15938 if (attr != nullptr && attr->form_is_unsigned ())
15939 cu->str_offsets_base = attr->as_unsigned ();
15940
15941 attr = (*diep)->attr (DW_AT_loclists_base);
15942 if (attr != nullptr)
15943 cu->loclist_base = attr->as_unsigned ();
15944
15945 auto maybe_addr_base = (*diep)->addr_base ();
15946 if (maybe_addr_base.has_value ())
15947 cu->addr_base = *maybe_addr_base;
15948
15949 attr = (*diep)->attr (DW_AT_rnglists_base);
15950 if (attr != nullptr)
15951 cu->rnglists_base = attr->as_unsigned ();
15952
15953 for (int i = 0; i < (*diep)->num_attrs; ++i)
15954 {
15955 if ((*diep)->attrs[i].form_requires_reprocessing ())
15956 read_attribute_reprocess (reader, &(*diep)->attrs[i], (*diep)->tag);
15957 }
15958
15959 (*diep)->num_attrs += extra_attrs.size ();
15960
15961 if (dwarf_die_debug)
15962 {
15963 gdb_printf (gdb_stdlog,
15964 "Read die from %s@0x%x of %s:\n",
15965 reader->die_section->get_name (),
15966 (unsigned) (info_ptr - reader->die_section->buffer),
15967 bfd_get_filename (reader->abfd));
15968 (*diep)->dump (dwarf_die_debug);
15969 }
15970
15971 return result;
15972 }
15973 \f
15974
15975 void
15976 cooked_indexer::check_bounds (cutu_reader *reader)
15977 {
15978 if (reader->cu->per_cu->addresses_seen)
15979 return;
15980
15981 dwarf2_cu *cu = reader->cu;
15982
15983 CORE_ADDR best_lowpc = 0, best_highpc = 0;
15984 /* Possibly set the default values of LOWPC and HIGHPC from
15985 `DW_AT_ranges'. */
15986 dwarf2_find_base_address (reader->comp_unit_die, cu);
15987 enum pc_bounds_kind cu_bounds_kind
15988 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
15989 cu, m_index_storage->get_addrmap (), cu->per_cu);
15990 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
15991 {
15992 struct objfile *objfile = cu->per_objfile->objfile;
15993 CORE_ADDR baseaddr = objfile->text_section_offset ();
15994 struct gdbarch *gdbarch = objfile->arch ();
15995 CORE_ADDR low
15996 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
15997 - baseaddr);
15998 CORE_ADDR high
15999 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
16000 - baseaddr - 1);
16001 /* Store the contiguous range if it is not empty; it can be
16002 empty for CUs with no code. */
16003 m_index_storage->get_addrmap ()->set_empty (low, high, cu->per_cu);
16004
16005 cu->per_cu->addresses_seen = true;
16006 }
16007 }
16008
16009 /* Helper function that returns true if TAG can have a linkage
16010 name. */
16011
16012 static bool
16013 tag_can_have_linkage_name (enum dwarf_tag tag)
16014 {
16015 switch (tag)
16016 {
16017 case DW_TAG_variable:
16018 case DW_TAG_subprogram:
16019 return true;
16020
16021 default:
16022 return false;
16023 }
16024 }
16025
16026 cutu_reader *
16027 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
16028 dwarf2_per_objfile *per_objfile,
16029 sect_offset sect_off, bool is_dwz,
16030 bool for_scanning)
16031 {
16032 /* Lookups for type unit references are always in the CU, and
16033 cross-CU references will crash. */
16034 if (reader->cu->per_cu->is_dwz == is_dwz
16035 && reader->cu->header.offset_in_cu_p (sect_off))
16036 return reader;
16037
16038 dwarf2_per_cu_data *per_cu
16039 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
16040 per_objfile->per_bfd);
16041
16042 /* When scanning, we only want to visit a given CU a single time.
16043 Doing this check here avoids self-imports as well. */
16044 if (for_scanning)
16045 {
16046 bool nope = false;
16047 if (!per_cu->scanned.compare_exchange_strong (nope, true))
16048 return nullptr;
16049 }
16050 if (per_cu == m_per_cu)
16051 return reader;
16052
16053 cutu_reader *result = m_index_storage->get_reader (per_cu);
16054 if (result == nullptr)
16055 {
16056 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
16057 m_index_storage->get_abbrev_cache ());
16058
16059 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
16060 language_minimal);
16061 std::unique_ptr<cutu_reader> copy
16062 (new cutu_reader (std::move (new_reader)));
16063 result = m_index_storage->preserve (std::move (copy));
16064 }
16065
16066 if (result->dummy_p || !result->comp_unit_die->has_children)
16067 return nullptr;
16068
16069 if (for_scanning)
16070 check_bounds (result);
16071
16072 return result;
16073 }
16074
16075 const gdb_byte *
16076 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
16077 cutu_reader *reader,
16078 const gdb_byte *watermark_ptr,
16079 const gdb_byte *info_ptr,
16080 const abbrev_info *abbrev,
16081 const char **name,
16082 const char **linkage_name,
16083 cooked_index_flag *flags,
16084 sect_offset *sibling_offset,
16085 const cooked_index_entry **parent_entry,
16086 CORE_ADDR *maybe_defer,
16087 bool for_specification)
16088 {
16089 bool origin_is_dwz = false;
16090 bool is_declaration = false;
16091 sect_offset origin_offset {};
16092
16093 gdb::optional<CORE_ADDR> low_pc;
16094 gdb::optional<CORE_ADDR> high_pc;
16095 bool high_pc_relative = false;
16096
16097 for (int i = 0; i < abbrev->num_attrs; ++i)
16098 {
16099 attribute attr;
16100 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16101
16102 /* Store the data if it is of an attribute we want to keep in a
16103 partial symbol table. */
16104 switch (attr.name)
16105 {
16106 case DW_AT_name:
16107 switch (abbrev->tag)
16108 {
16109 case DW_TAG_compile_unit:
16110 case DW_TAG_partial_unit:
16111 case DW_TAG_type_unit:
16112 /* Compilation units have a DW_AT_name that is a filename, not
16113 a source language identifier. */
16114 break;
16115
16116 default:
16117 if (*name == nullptr)
16118 *name = attr.as_string ();
16119 break;
16120 }
16121 break;
16122
16123 case DW_AT_linkage_name:
16124 case DW_AT_MIPS_linkage_name:
16125 /* Note that both forms of linkage name might appear. We
16126 assume they will be the same, and we only store the last
16127 one we see. */
16128 if (*linkage_name == nullptr)
16129 *linkage_name = attr.as_string ();
16130 break;
16131
16132 case DW_AT_main_subprogram:
16133 if (attr.as_boolean ())
16134 *flags |= IS_MAIN;
16135 break;
16136
16137 case DW_AT_declaration:
16138 is_declaration = attr.as_boolean ();
16139 break;
16140
16141 case DW_AT_sibling:
16142 if (sibling_offset != nullptr)
16143 *sibling_offset = attr.get_ref_die_offset ();
16144 break;
16145
16146 case DW_AT_specification:
16147 case DW_AT_abstract_origin:
16148 case DW_AT_extension:
16149 origin_offset = attr.get_ref_die_offset ();
16150 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
16151 break;
16152
16153 case DW_AT_external:
16154 if (attr.as_boolean ())
16155 *flags &= ~IS_STATIC;
16156 break;
16157
16158 case DW_AT_enum_class:
16159 if (attr.as_boolean ())
16160 *flags |= IS_ENUM_CLASS;
16161 break;
16162
16163 case DW_AT_low_pc:
16164 low_pc = attr.as_address ();
16165 break;
16166
16167 case DW_AT_high_pc:
16168 high_pc = attr.as_address ();
16169 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
16170 high_pc_relative = true;
16171 break;
16172
16173 case DW_AT_location:
16174 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
16175 {
16176 struct dwarf_block *locdesc = attr.as_block ();
16177 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
16178 if (addr != 0
16179 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
16180 {
16181 low_pc = addr;
16182 /* For variables, we don't want to try decoding the
16183 type just to find the size -- for gdb's purposes
16184 we only need the address of a variable. */
16185 high_pc = addr + 1;
16186 high_pc_relative = false;
16187 }
16188 }
16189 break;
16190
16191 case DW_AT_ranges:
16192 if (!scanning_per_cu->addresses_seen)
16193 {
16194 /* Offset in the .debug_ranges or .debug_rnglist section
16195 (depending on DWARF version). */
16196 ULONGEST ranges_offset = attr.as_unsigned ();
16197
16198 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
16199 want to add this value. */
16200 ranges_offset += reader->cu->gnu_ranges_base;
16201
16202 CORE_ADDR lowpc, highpc;
16203 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
16204 m_index_storage->get_addrmap (),
16205 scanning_per_cu, abbrev->tag);
16206 }
16207 break;
16208 }
16209 }
16210
16211 /* We don't want to examine declarations, but if we found a
16212 declaration when handling DW_AT_specification or the like, then
16213 that is ok. Similarly, we allow an external variable without a
16214 location; those are resolved via minimal symbols. */
16215 if (is_declaration && !for_specification
16216 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
16217 {
16218 /* We always want to recurse into some types, but we may not
16219 want to treat them as definitions. */
16220 if ((abbrev->tag == DW_TAG_class_type
16221 || abbrev->tag == DW_TAG_structure_type
16222 || abbrev->tag == DW_TAG_union_type)
16223 && abbrev->has_children)
16224 *flags |= IS_TYPE_DECLARATION;
16225 else
16226 {
16227 *linkage_name = nullptr;
16228 *name = nullptr;
16229 }
16230 }
16231 else if ((*name == nullptr
16232 || (*linkage_name == nullptr
16233 && tag_can_have_linkage_name (abbrev->tag))
16234 || (*parent_entry == nullptr && m_language != language_c))
16235 && origin_offset != sect_offset (0))
16236 {
16237 cutu_reader *new_reader
16238 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
16239 origin_is_dwz, false);
16240 if (new_reader != nullptr)
16241 {
16242 const gdb_byte *new_info_ptr = (new_reader->buffer
16243 + to_underlying (origin_offset));
16244
16245 if (new_reader->cu == reader->cu
16246 && new_info_ptr > watermark_ptr
16247 && *parent_entry == nullptr)
16248 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
16249 else if (*parent_entry == nullptr)
16250 {
16251 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
16252 void *obj = m_die_range_map.find (lookup);
16253 *parent_entry = static_cast <cooked_index_entry *> (obj);
16254 }
16255
16256 unsigned int bytes_read;
16257 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
16258 new_info_ptr,
16259 &bytes_read);
16260 new_info_ptr += bytes_read;
16261 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
16262 new_abbrev, name, linkage_name, flags, nullptr,
16263 parent_entry, maybe_defer, true);
16264 }
16265 }
16266
16267 if (!for_specification)
16268 {
16269 if (m_language == language_ada
16270 && *linkage_name == nullptr)
16271 *linkage_name = *name;
16272
16273 if (!scanning_per_cu->addresses_seen
16274 && low_pc.has_value ()
16275 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
16276 || *low_pc != 0)
16277 && high_pc.has_value ())
16278 {
16279 if (high_pc_relative)
16280 high_pc = *high_pc + *low_pc;
16281
16282 if (*high_pc > *low_pc)
16283 {
16284 struct objfile *objfile = reader->cu->per_objfile->objfile;
16285 CORE_ADDR baseaddr = objfile->text_section_offset ();
16286 struct gdbarch *gdbarch = objfile->arch ();
16287 CORE_ADDR lo
16288 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
16289 - baseaddr);
16290 CORE_ADDR hi
16291 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
16292 - baseaddr);
16293 m_index_storage->get_addrmap ()->set_empty (lo, hi - 1,
16294 scanning_per_cu);
16295 }
16296 }
16297
16298 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
16299 *flags &= ~IS_STATIC;
16300
16301 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
16302 *name = "(anonymous namespace)";
16303
16304 if (m_language == language_cplus
16305 && (abbrev->tag == DW_TAG_class_type
16306 || abbrev->tag == DW_TAG_interface_type
16307 || abbrev->tag == DW_TAG_structure_type
16308 || abbrev->tag == DW_TAG_union_type
16309 || abbrev->tag == DW_TAG_enumeration_type
16310 || abbrev->tag == DW_TAG_enumerator))
16311 *flags &= ~IS_STATIC;
16312 }
16313
16314 return info_ptr;
16315 }
16316
16317 const gdb_byte *
16318 cooked_indexer::index_imported_unit (cutu_reader *reader,
16319 const gdb_byte *info_ptr,
16320 const abbrev_info *abbrev)
16321 {
16322 sect_offset sect_off {};
16323 bool is_dwz = false;
16324
16325 for (int i = 0; i < abbrev->num_attrs; ++i)
16326 {
16327 /* Note that we never need to reprocess attributes here. */
16328 attribute attr;
16329 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16330
16331 if (attr.name == DW_AT_import)
16332 {
16333 sect_off = attr.get_ref_die_offset ();
16334 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16335 || reader->cu->per_cu->is_dwz);
16336 }
16337 }
16338
16339 /* Did not find DW_AT_import. */
16340 if (sect_off == sect_offset (0))
16341 return info_ptr;
16342
16343 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
16344 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
16345 is_dwz, true);
16346 if (new_reader != nullptr)
16347 {
16348 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
16349
16350 reader->cu->add_dependence (new_reader->cu->per_cu);
16351 }
16352
16353 return info_ptr;
16354 }
16355
16356 const gdb_byte *
16357 cooked_indexer::recurse (cutu_reader *reader,
16358 const gdb_byte *info_ptr,
16359 const cooked_index_entry *parent_entry,
16360 bool fully)
16361 {
16362 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16363
16364 if (parent_entry != nullptr)
16365 {
16366 CORE_ADDR start = form_addr (parent_entry->die_offset,
16367 reader->cu->per_cu->is_dwz);
16368 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
16369 reader->cu->per_cu->is_dwz);
16370 m_die_range_map.set_empty (start, end, (void *) parent_entry);
16371 }
16372
16373 return info_ptr;
16374 }
16375
16376 const gdb_byte *
16377 cooked_indexer::index_dies (cutu_reader *reader,
16378 const gdb_byte *info_ptr,
16379 const cooked_index_entry *parent_entry,
16380 bool fully)
16381 {
16382 const gdb_byte *end_ptr = (reader->buffer
16383 + to_underlying (reader->cu->header.sect_off)
16384 + reader->cu->header.get_length_with_initial ());
16385
16386 while (info_ptr < end_ptr)
16387 {
16388 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
16389 unsigned int bytes_read;
16390 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
16391 &bytes_read);
16392 info_ptr += bytes_read;
16393 if (abbrev == nullptr)
16394 break;
16395
16396 if (abbrev->tag == DW_TAG_imported_unit)
16397 {
16398 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
16399 continue;
16400 }
16401
16402 if (!abbrev->interesting)
16403 {
16404 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
16405 if (fully && abbrev->has_children)
16406 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16407 continue;
16408 }
16409
16410 const char *name = nullptr;
16411 const char *linkage_name = nullptr;
16412 CORE_ADDR defer = 0;
16413 cooked_index_flag flags = IS_STATIC;
16414 sect_offset sibling {};
16415 const cooked_index_entry *this_parent_entry = parent_entry;
16416 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
16417 info_ptr, abbrev, &name, &linkage_name,
16418 &flags, &sibling, &this_parent_entry,
16419 &defer, false);
16420
16421 if (abbrev->tag == DW_TAG_namespace
16422 && m_language == language_cplus
16423 && strcmp (name, "::") == 0)
16424 {
16425 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
16426 generated bogus DW_TAG_namespace DIEs with a name of "::"
16427 for the global namespace. Work around this problem
16428 here. */
16429 name = nullptr;
16430 }
16431
16432 const cooked_index_entry *this_entry = nullptr;
16433 if (name != nullptr)
16434 {
16435 if (defer != 0)
16436 m_deferred_entries.push_back ({
16437 this_die, name, defer, abbrev->tag, flags
16438 });
16439 else
16440 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
16441 name, this_parent_entry,
16442 m_per_cu);
16443 }
16444
16445 if (linkage_name != nullptr)
16446 {
16447 /* We only want this to be "main" if it has a linkage name
16448 but not an ordinary name. */
16449 if (name != nullptr)
16450 flags = flags & ~IS_MAIN;
16451 /* Set the IS_LINKAGE on for everything except when functions
16452 have linkage name present but name is absent. */
16453 if (name != nullptr
16454 || (abbrev->tag != DW_TAG_subprogram
16455 && abbrev->tag != DW_TAG_inlined_subroutine
16456 && abbrev->tag != DW_TAG_entry_point))
16457 flags = flags | IS_LINKAGE;
16458 m_index_storage->add (this_die, abbrev->tag, flags,
16459 linkage_name, nullptr, m_per_cu);
16460 }
16461
16462 if (abbrev->has_children)
16463 {
16464 switch (abbrev->tag)
16465 {
16466 case DW_TAG_class_type:
16467 case DW_TAG_interface_type:
16468 case DW_TAG_structure_type:
16469 case DW_TAG_union_type:
16470 if (m_language != language_c && this_entry != nullptr)
16471 {
16472 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16473 continue;
16474 }
16475 break;
16476
16477 case DW_TAG_enumeration_type:
16478 /* We need to recurse even for an anonymous enumeration.
16479 Which scope we record as the parent scope depends on
16480 whether we're reading an "enum class". If so, we use
16481 the enum itself as the parent, yielding names like
16482 "enum_class::enumerator"; otherwise we inject the
16483 names into our own parent scope. */
16484 info_ptr = recurse (reader, info_ptr,
16485 ((flags & IS_ENUM_CLASS) == 0)
16486 ? parent_entry
16487 : this_entry,
16488 fully);
16489 continue;
16490
16491 case DW_TAG_module:
16492 if (this_entry == nullptr)
16493 break;
16494 /* FALLTHROUGH */
16495 case DW_TAG_namespace:
16496 /* We don't check THIS_ENTRY for a namespace, to handle
16497 the ancient G++ workaround pointed out above. */
16498 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16499 continue;
16500
16501 case DW_TAG_subprogram:
16502 if ((m_language == language_fortran
16503 || m_language == language_ada)
16504 && this_entry != nullptr)
16505 {
16506 info_ptr = recurse (reader, info_ptr, this_entry, true);
16507 continue;
16508 }
16509 break;
16510 }
16511
16512 if (sibling != sect_offset (0))
16513 {
16514 const gdb_byte *sibling_ptr
16515 = reader->buffer + to_underlying (sibling);
16516
16517 if (sibling_ptr < info_ptr)
16518 complaint (_("DW_AT_sibling points backwards"));
16519 else if (sibling_ptr > reader->buffer_end)
16520 reader->die_section->overflow_complaint ();
16521 else
16522 info_ptr = sibling_ptr;
16523 }
16524 else
16525 info_ptr = skip_children (reader, info_ptr);
16526 }
16527 }
16528
16529 return info_ptr;
16530 }
16531
16532 void
16533 cooked_indexer::make_index (cutu_reader *reader)
16534 {
16535 check_bounds (reader);
16536 find_file_and_directory (reader->comp_unit_die, reader->cu);
16537 if (!reader->comp_unit_die->has_children)
16538 return;
16539 index_dies (reader, reader->info_ptr, nullptr, false);
16540
16541 for (const auto &entry : m_deferred_entries)
16542 {
16543 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
16544 void *obj = m_die_range_map.find (key);
16545 cooked_index_entry *parent = static_cast <cooked_index_entry *> (obj);
16546 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
16547 entry.name, parent, m_per_cu);
16548 }
16549 }
16550
16551 /* An implementation of quick_symbol_functions for the cooked DWARF
16552 index. */
16553
16554 struct cooked_index_functions : public dwarf2_base_index_functions
16555 {
16556 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
16557 CORE_ADDR adjusted_pc) override;
16558
16559 struct compunit_symtab *find_compunit_symtab_by_address
16560 (struct objfile *objfile, CORE_ADDR address) override;
16561
16562 void dump (struct objfile *objfile) override
16563 {
16564 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16565 cooked_index *index
16566 = (gdb::checked_static_cast<cooked_index *>
16567 (per_objfile->per_bfd->index_table.get ()));
16568 if (index == nullptr)
16569 return;
16570
16571 gdb_printf ("Cooked index in use:\n");
16572 gdb_printf ("\n");
16573 index->dump (objfile->arch ());
16574 }
16575
16576 void expand_matching_symbols
16577 (struct objfile *,
16578 const lookup_name_info &lookup_name,
16579 domain_enum domain,
16580 int global,
16581 symbol_compare_ftype *ordered_compare) override;
16582
16583 bool expand_symtabs_matching
16584 (struct objfile *objfile,
16585 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16586 const lookup_name_info *lookup_name,
16587 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16588 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16589 block_search_flags search_flags,
16590 domain_enum domain,
16591 enum search_domain kind) override;
16592
16593 bool can_lazily_read_symbols () override
16594 {
16595 return true;
16596 }
16597
16598 void read_partial_symbols (struct objfile *objfile) override
16599 {
16600 if (dwarf2_has_info (objfile, nullptr))
16601 dwarf2_build_psymtabs (objfile);
16602 }
16603 };
16604
16605 dwarf2_per_cu_data *
16606 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
16607 CORE_ADDR adjusted_pc)
16608 {
16609 cooked_index *table
16610 = (gdb::checked_static_cast<cooked_index *>
16611 (per_bfd->index_table.get ()));
16612 if (table == nullptr)
16613 return nullptr;
16614 return table->lookup (adjusted_pc);
16615 }
16616
16617 struct compunit_symtab *
16618 cooked_index_functions::find_compunit_symtab_by_address
16619 (struct objfile *objfile, CORE_ADDR address)
16620 {
16621 if (objfile->sect_index_data == -1)
16622 return nullptr;
16623
16624 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16625 cooked_index *table
16626 = (gdb::checked_static_cast<cooked_index *>
16627 (per_objfile->per_bfd->index_table.get ()));
16628 if (table == nullptr)
16629 return nullptr;
16630
16631 CORE_ADDR baseaddr = objfile->data_section_offset ();
16632 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
16633 if (per_cu == nullptr)
16634 return nullptr;
16635
16636 return dw2_instantiate_symtab (per_cu, per_objfile, false);
16637 }
16638
16639 void
16640 cooked_index_functions::expand_matching_symbols
16641 (struct objfile *objfile,
16642 const lookup_name_info &lookup_name,
16643 domain_enum domain,
16644 int global,
16645 symbol_compare_ftype *ordered_compare)
16646 {
16647 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16648 cooked_index *table
16649 = (gdb::checked_static_cast<cooked_index *>
16650 (per_objfile->per_bfd->index_table.get ()));
16651 if (table == nullptr)
16652 return;
16653
16654 const block_search_flags search_flags = (global
16655 ? SEARCH_GLOBAL_BLOCK
16656 : SEARCH_STATIC_BLOCK);
16657 const language_defn *lang = language_def (language_ada);
16658 symbol_name_matcher_ftype *name_match
16659 = lang->get_symbol_name_matcher (lookup_name);
16660
16661 for (const cooked_index_entry *entry : table->all_entries ())
16662 {
16663 QUIT;
16664
16665 if (entry->parent_entry != nullptr)
16666 continue;
16667
16668 if (!entry->matches (search_flags)
16669 || !entry->matches (domain))
16670 continue;
16671
16672 if (name_match (entry->canonical, lookup_name, nullptr))
16673 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
16674 }
16675 }
16676
16677 bool
16678 cooked_index_functions::expand_symtabs_matching
16679 (struct objfile *objfile,
16680 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16681 const lookup_name_info *lookup_name,
16682 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16683 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16684 block_search_flags search_flags,
16685 domain_enum domain,
16686 enum search_domain kind)
16687 {
16688 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16689
16690 cooked_index *table
16691 = (gdb::checked_static_cast<cooked_index *>
16692 (per_objfile->per_bfd->index_table.get ()));
16693 if (table == nullptr)
16694 return true;
16695
16696 table->wait ();
16697
16698 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
16699
16700 /* This invariant is documented in quick-functions.h. */
16701 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
16702 if (lookup_name == nullptr)
16703 {
16704 for (dwarf2_per_cu_data *per_cu
16705 : all_units_range (per_objfile->per_bfd))
16706 {
16707 QUIT;
16708
16709 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
16710 file_matcher,
16711 expansion_notify))
16712 return false;
16713 }
16714 return true;
16715 }
16716
16717 lookup_name_info lookup_name_without_params
16718 = lookup_name->make_ignore_params ();
16719 bool completing = lookup_name->completion_mode ();
16720
16721 /* Unique styles of language splitting. */
16722 static const enum language unique_styles[] =
16723 {
16724 /* No splitting is also a style. */
16725 language_c,
16726 /* This includes Rust. */
16727 language_cplus,
16728 /* This includes Go. */
16729 language_d,
16730 language_ada
16731 };
16732
16733 for (enum language lang : unique_styles)
16734 {
16735 std::vector<gdb::string_view> name_vec
16736 = lookup_name_without_params.split_name (lang);
16737 std::string last_name = gdb::to_string (name_vec.back ());
16738
16739 for (const cooked_index_entry *entry : table->find (last_name,
16740 completing))
16741 {
16742 QUIT;
16743
16744 /* No need to consider symbols from expanded CUs. */
16745 if (per_objfile->symtab_set_p (entry->per_cu))
16746 continue;
16747
16748 /* If file-matching was done, we don't need to consider
16749 symbols from unmarked CUs. */
16750 if (file_matcher != nullptr && !entry->per_cu->mark)
16751 continue;
16752
16753 /* See if the symbol matches the type filter. */
16754 if (!entry->matches (search_flags)
16755 || !entry->matches (domain)
16756 || !entry->matches (kind))
16757 continue;
16758
16759 /* We've found the base name of the symbol; now walk its
16760 parentage chain, ensuring that each component
16761 matches. */
16762 bool found = true;
16763
16764 const cooked_index_entry *parent = entry->parent_entry;
16765 for (int i = name_vec.size () - 1; i > 0; --i)
16766 {
16767 /* If we ran out of entries, or if this segment doesn't
16768 match, this did not match. */
16769 if (parent == nullptr
16770 || strncmp (parent->name, name_vec[i - 1].data (),
16771 name_vec[i - 1].length ()) != 0)
16772 {
16773 found = false;
16774 break;
16775 }
16776
16777 parent = parent->parent_entry;
16778 }
16779
16780 if (!found)
16781 continue;
16782
16783 /* Might have been looking for "a::b" and found
16784 "x::a::b". */
16785 if (symbol_matcher == nullptr)
16786 {
16787 symbol_name_match_type match_type
16788 = lookup_name_without_params.match_type ();
16789 if ((match_type == symbol_name_match_type::FULL
16790 || (lang != language_ada
16791 && match_type == symbol_name_match_type::EXPRESSION))
16792 && parent != nullptr)
16793 continue;
16794 }
16795 else
16796 {
16797 auto_obstack temp_storage;
16798 const char *full_name = entry->full_name (&temp_storage);
16799 if (!symbol_matcher (full_name))
16800 continue;
16801 }
16802
16803 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
16804 file_matcher,
16805 expansion_notify))
16806 return false;
16807 }
16808 }
16809
16810 return true;
16811 }
16812
16813 /* Return a new cooked_index_functions object. */
16814
16815 static quick_symbol_functions_up
16816 make_cooked_index_funcs ()
16817 {
16818 return quick_symbol_functions_up (new cooked_index_functions);
16819 }
16820
16821 quick_symbol_functions_up
16822 cooked_index::make_quick_functions () const
16823 {
16824 return make_cooked_index_funcs ();
16825 }
16826
16827 \f
16828
16829 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
16830 contents from the given SECTION in the HEADER.
16831
16832 HEADER_OFFSET is the offset of the header in the section. */
16833 static void
16834 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
16835 struct dwarf2_section_info *section,
16836 sect_offset header_offset)
16837 {
16838 unsigned int bytes_read;
16839 bfd *abfd = section->get_bfd_owner ();
16840 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
16841
16842 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
16843 info_ptr += bytes_read;
16844
16845 header->version = read_2_bytes (abfd, info_ptr);
16846 info_ptr += 2;
16847
16848 header->addr_size = read_1_byte (abfd, info_ptr);
16849 info_ptr += 1;
16850
16851 header->segment_collector_size = read_1_byte (abfd, info_ptr);
16852 info_ptr += 1;
16853
16854 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
16855 }
16856
16857 /* Return the DW_AT_loclists_base value for the CU. */
16858 static ULONGEST
16859 lookup_loclist_base (struct dwarf2_cu *cu)
16860 {
16861 /* For the .dwo unit, the loclist_base points to the first offset following
16862 the header. The header consists of the following entities-
16863 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
16864 bit format)
16865 2. version (2 bytes)
16866 3. address size (1 byte)
16867 4. segment selector size (1 byte)
16868 5. offset entry count (4 bytes)
16869 These sizes are derived as per the DWARFv5 standard. */
16870 if (cu->dwo_unit != nullptr)
16871 {
16872 if (cu->header.initial_length_size == 4)
16873 return LOCLIST_HEADER_SIZE32;
16874 return LOCLIST_HEADER_SIZE64;
16875 }
16876 return cu->loclist_base;
16877 }
16878
16879 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
16880 array of offsets in the .debug_loclists section. */
16881
16882 static sect_offset
16883 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
16884 {
16885 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16886 struct objfile *objfile = per_objfile->objfile;
16887 bfd *abfd = objfile->obfd.get ();
16888 ULONGEST loclist_header_size =
16889 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
16890 : LOCLIST_HEADER_SIZE64);
16891 ULONGEST loclist_base = lookup_loclist_base (cu);
16892
16893 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
16894 ULONGEST start_offset =
16895 loclist_base + loclist_index * cu->header.offset_size;
16896
16897 /* Get loclists section. */
16898 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16899
16900 /* Read the loclists section content. */
16901 section->read (objfile);
16902 if (section->buffer == NULL)
16903 error (_("DW_FORM_loclistx used without .debug_loclists "
16904 "section [in module %s]"), objfile_name (objfile));
16905
16906 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
16907 so if loclist_base is smaller than the header size, we have a problem. */
16908 if (loclist_base < loclist_header_size)
16909 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
16910 objfile_name (objfile));
16911
16912 /* Read the header of the loclists contribution. */
16913 struct loclists_rnglists_header header;
16914 read_loclists_rnglists_header (&header, section,
16915 (sect_offset) (loclist_base - loclist_header_size));
16916
16917 /* Verify the loclist index is valid. */
16918 if (loclist_index >= header.offset_entry_count)
16919 error (_("DW_FORM_loclistx pointing outside of "
16920 ".debug_loclists offset array [in module %s]"),
16921 objfile_name (objfile));
16922
16923 /* Validate that reading won't go beyond the end of the section. */
16924 if (start_offset + cu->header.offset_size > section->size)
16925 error (_("Reading DW_FORM_loclistx index beyond end of"
16926 ".debug_loclists section [in module %s]"),
16927 objfile_name (objfile));
16928
16929 const gdb_byte *info_ptr = section->buffer + start_offset;
16930
16931 if (cu->header.offset_size == 4)
16932 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
16933 else
16934 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
16935 }
16936
16937 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
16938 array of offsets in the .debug_rnglists section. */
16939
16940 static sect_offset
16941 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
16942 dwarf_tag tag)
16943 {
16944 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
16945 struct objfile *objfile = dwarf2_per_objfile->objfile;
16946 bfd *abfd = objfile->obfd.get ();
16947 ULONGEST rnglist_header_size =
16948 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
16949 : RNGLIST_HEADER_SIZE64);
16950
16951 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
16952 .debug_rnglists.dwo section. The rnglists base given in the skeleton
16953 doesn't apply. */
16954 ULONGEST rnglist_base =
16955 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
16956
16957 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
16958 ULONGEST start_offset =
16959 rnglist_base + rnglist_index * cu->header.offset_size;
16960
16961 /* Get rnglists section. */
16962 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
16963
16964 /* Read the rnglists section content. */
16965 section->read (objfile);
16966 if (section->buffer == nullptr)
16967 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
16968 "[in module %s]"),
16969 objfile_name (objfile));
16970
16971 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
16972 so if rnglist_base is smaller than the header size, we have a problem. */
16973 if (rnglist_base < rnglist_header_size)
16974 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
16975 objfile_name (objfile));
16976
16977 /* Read the header of the rnglists contribution. */
16978 struct loclists_rnglists_header header;
16979 read_loclists_rnglists_header (&header, section,
16980 (sect_offset) (rnglist_base - rnglist_header_size));
16981
16982 /* Verify the rnglist index is valid. */
16983 if (rnglist_index >= header.offset_entry_count)
16984 error (_("DW_FORM_rnglistx index pointing outside of "
16985 ".debug_rnglists offset array [in module %s]"),
16986 objfile_name (objfile));
16987
16988 /* Validate that reading won't go beyond the end of the section. */
16989 if (start_offset + cu->header.offset_size > section->size)
16990 error (_("Reading DW_FORM_rnglistx index beyond end of"
16991 ".debug_rnglists section [in module %s]"),
16992 objfile_name (objfile));
16993
16994 const gdb_byte *info_ptr = section->buffer + start_offset;
16995
16996 if (cu->header.offset_size == 4)
16997 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
16998 else
16999 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
17000 }
17001
17002 /* Process the attributes that had to be skipped in the first round. These
17003 attributes are the ones that need str_offsets_base or addr_base attributes.
17004 They could not have been processed in the first round, because at the time
17005 the values of str_offsets_base or addr_base may not have been known. */
17006 static void
17007 read_attribute_reprocess (const struct die_reader_specs *reader,
17008 struct attribute *attr, dwarf_tag tag)
17009 {
17010 struct dwarf2_cu *cu = reader->cu;
17011 switch (attr->form)
17012 {
17013 case DW_FORM_addrx:
17014 case DW_FORM_GNU_addr_index:
17015 attr->set_address (read_addr_index (cu,
17016 attr->as_unsigned_reprocess ()));
17017 break;
17018 case DW_FORM_loclistx:
17019 {
17020 sect_offset loclists_sect_off
17021 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
17022
17023 attr->set_unsigned (to_underlying (loclists_sect_off));
17024 }
17025 break;
17026 case DW_FORM_rnglistx:
17027 {
17028 sect_offset rnglists_sect_off
17029 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
17030
17031 attr->set_unsigned (to_underlying (rnglists_sect_off));
17032 }
17033 break;
17034 case DW_FORM_strx:
17035 case DW_FORM_strx1:
17036 case DW_FORM_strx2:
17037 case DW_FORM_strx3:
17038 case DW_FORM_strx4:
17039 case DW_FORM_GNU_str_index:
17040 {
17041 unsigned int str_index = attr->as_unsigned_reprocess ();
17042 gdb_assert (!attr->canonical_string_p ());
17043 if (reader->dwo_file != NULL)
17044 attr->set_string_noncanonical (read_dwo_str_index (reader,
17045 str_index));
17046 else
17047 attr->set_string_noncanonical (read_stub_str_index (cu,
17048 str_index));
17049 break;
17050 }
17051 default:
17052 gdb_assert_not_reached ("Unexpected DWARF form.");
17053 }
17054 }
17055
17056 /* Read an attribute value described by an attribute form. */
17057
17058 static const gdb_byte *
17059 read_attribute_value (const struct die_reader_specs *reader,
17060 struct attribute *attr, unsigned form,
17061 LONGEST implicit_const, const gdb_byte *info_ptr,
17062 bool allow_reprocess)
17063 {
17064 struct dwarf2_cu *cu = reader->cu;
17065 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17066 struct objfile *objfile = per_objfile->objfile;
17067 bfd *abfd = reader->abfd;
17068 struct comp_unit_head *cu_header = &cu->header;
17069 unsigned int bytes_read;
17070 struct dwarf_block *blk;
17071
17072 attr->form = (enum dwarf_form) form;
17073 switch (form)
17074 {
17075 case DW_FORM_ref_addr:
17076 if (cu_header->version == 2)
17077 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
17078 &bytes_read));
17079 else
17080 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17081 &bytes_read));
17082 info_ptr += bytes_read;
17083 break;
17084 case DW_FORM_GNU_ref_alt:
17085 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17086 &bytes_read));
17087 info_ptr += bytes_read;
17088 break;
17089 case DW_FORM_addr:
17090 {
17091 struct gdbarch *gdbarch = objfile->arch ();
17092 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
17093 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
17094 attr->set_address (addr);
17095 info_ptr += bytes_read;
17096 }
17097 break;
17098 case DW_FORM_block2:
17099 blk = dwarf_alloc_block (cu);
17100 blk->size = read_2_bytes (abfd, info_ptr);
17101 info_ptr += 2;
17102 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17103 info_ptr += blk->size;
17104 attr->set_block (blk);
17105 break;
17106 case DW_FORM_block4:
17107 blk = dwarf_alloc_block (cu);
17108 blk->size = read_4_bytes (abfd, info_ptr);
17109 info_ptr += 4;
17110 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17111 info_ptr += blk->size;
17112 attr->set_block (blk);
17113 break;
17114 case DW_FORM_data2:
17115 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
17116 info_ptr += 2;
17117 break;
17118 case DW_FORM_data4:
17119 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
17120 info_ptr += 4;
17121 break;
17122 case DW_FORM_data8:
17123 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
17124 info_ptr += 8;
17125 break;
17126 case DW_FORM_data16:
17127 blk = dwarf_alloc_block (cu);
17128 blk->size = 16;
17129 blk->data = read_n_bytes (abfd, info_ptr, 16);
17130 info_ptr += 16;
17131 attr->set_block (blk);
17132 break;
17133 case DW_FORM_sec_offset:
17134 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17135 &bytes_read));
17136 info_ptr += bytes_read;
17137 break;
17138 case DW_FORM_loclistx:
17139 {
17140 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17141 &bytes_read));
17142 info_ptr += bytes_read;
17143 if (allow_reprocess)
17144 read_attribute_reprocess (reader, attr);
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 if (allow_reprocess)
17219 read_attribute_reprocess (reader, attr);
17220 }
17221 break;
17222 case DW_FORM_udata:
17223 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17224 info_ptr += bytes_read;
17225 break;
17226 case DW_FORM_ref1:
17227 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17228 + read_1_byte (abfd, info_ptr)));
17229 info_ptr += 1;
17230 break;
17231 case DW_FORM_ref2:
17232 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17233 + read_2_bytes (abfd, info_ptr)));
17234 info_ptr += 2;
17235 break;
17236 case DW_FORM_ref4:
17237 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17238 + read_4_bytes (abfd, info_ptr)));
17239 info_ptr += 4;
17240 break;
17241 case DW_FORM_ref8:
17242 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17243 + read_8_bytes (abfd, info_ptr)));
17244 info_ptr += 8;
17245 break;
17246 case DW_FORM_ref_sig8:
17247 attr->set_signature (read_8_bytes (abfd, info_ptr));
17248 info_ptr += 8;
17249 break;
17250 case DW_FORM_ref_udata:
17251 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17252 + read_unsigned_leb128 (abfd, info_ptr,
17253 &bytes_read)));
17254 info_ptr += bytes_read;
17255 break;
17256 case DW_FORM_indirect:
17257 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17258 info_ptr += bytes_read;
17259 if (form == DW_FORM_implicit_const)
17260 {
17261 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17262 info_ptr += bytes_read;
17263 }
17264 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17265 info_ptr, allow_reprocess);
17266 break;
17267 case DW_FORM_implicit_const:
17268 attr->set_signed (implicit_const);
17269 break;
17270 case DW_FORM_addrx:
17271 case DW_FORM_GNU_addr_index:
17272 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17273 &bytes_read));
17274 info_ptr += bytes_read;
17275 if (allow_reprocess)
17276 read_attribute_reprocess (reader, attr);
17277 break;
17278 case DW_FORM_strx:
17279 case DW_FORM_strx1:
17280 case DW_FORM_strx2:
17281 case DW_FORM_strx3:
17282 case DW_FORM_strx4:
17283 case DW_FORM_GNU_str_index:
17284 {
17285 ULONGEST str_index;
17286 if (form == DW_FORM_strx1)
17287 {
17288 str_index = read_1_byte (abfd, info_ptr);
17289 info_ptr += 1;
17290 }
17291 else if (form == DW_FORM_strx2)
17292 {
17293 str_index = read_2_bytes (abfd, info_ptr);
17294 info_ptr += 2;
17295 }
17296 else if (form == DW_FORM_strx3)
17297 {
17298 str_index = read_3_bytes (abfd, info_ptr);
17299 info_ptr += 3;
17300 }
17301 else if (form == DW_FORM_strx4)
17302 {
17303 str_index = read_4_bytes (abfd, info_ptr);
17304 info_ptr += 4;
17305 }
17306 else
17307 {
17308 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17309 info_ptr += bytes_read;
17310 }
17311 attr->set_unsigned_reprocess (str_index);
17312 if (allow_reprocess)
17313 read_attribute_reprocess (reader, attr);
17314 }
17315 break;
17316 default:
17317 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17318 dwarf_form_name (form),
17319 bfd_get_filename (abfd));
17320 }
17321
17322 /* Super hack. */
17323 if (cu->per_cu->is_dwz && attr->form_is_ref ())
17324 attr->form = DW_FORM_GNU_ref_alt;
17325
17326 /* We have seen instances where the compiler tried to emit a byte
17327 size attribute of -1 which ended up being encoded as an unsigned
17328 0xffffffff. Although 0xffffffff is technically a valid size value,
17329 an object of this size seems pretty unlikely so we can relatively
17330 safely treat these cases as if the size attribute was invalid and
17331 treat them as zero by default. */
17332 if (attr->name == DW_AT_byte_size
17333 && form == DW_FORM_data4
17334 && attr->as_unsigned () >= 0xffffffff)
17335 {
17336 complaint
17337 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17338 hex_string (attr->as_unsigned ()));
17339 attr->set_unsigned (0);
17340 }
17341
17342 return info_ptr;
17343 }
17344
17345 /* Read an attribute described by an abbreviated attribute. */
17346
17347 static const gdb_byte *
17348 read_attribute (const struct die_reader_specs *reader,
17349 struct attribute *attr, const struct attr_abbrev *abbrev,
17350 const gdb_byte *info_ptr,
17351 bool allow_reprocess)
17352 {
17353 attr->name = abbrev->name;
17354 attr->string_is_canonical = 0;
17355 return read_attribute_value (reader, attr, abbrev->form,
17356 abbrev->implicit_const, info_ptr,
17357 allow_reprocess);
17358 }
17359
17360 /* See read.h. */
17361
17362 const char *
17363 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
17364 LONGEST str_offset)
17365 {
17366 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
17367 str_offset, "DW_FORM_strp");
17368 }
17369
17370 /* Return pointer to string at .debug_str offset as read from BUF.
17371 BUF is assumed to be in a compilation unit described by CU_HEADER.
17372 Return *BYTES_READ_PTR count of bytes read from BUF. */
17373
17374 static const char *
17375 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
17376 const gdb_byte *buf,
17377 const struct comp_unit_head *cu_header,
17378 unsigned int *bytes_read_ptr)
17379 {
17380 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17381
17382 return read_indirect_string_at_offset (per_objfile, str_offset);
17383 }
17384
17385 /* See read.h. */
17386
17387 const char *
17388 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17389 unsigned int offset_size)
17390 {
17391 bfd *abfd = objfile->obfd.get ();
17392 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
17393
17394 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17395 }
17396
17397 /* See read.h. */
17398
17399 const char *
17400 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17401 const struct comp_unit_head *cu_header,
17402 unsigned int *bytes_read_ptr)
17403 {
17404 bfd *abfd = objfile->obfd.get ();
17405 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17406
17407 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17408 }
17409
17410 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17411 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
17412 ADDR_SIZE is the size of addresses from the CU header. */
17413
17414 static CORE_ADDR
17415 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
17416 gdb::optional<ULONGEST> addr_base, int addr_size)
17417 {
17418 struct objfile *objfile = per_objfile->objfile;
17419 bfd *abfd = objfile->obfd.get ();
17420 const gdb_byte *info_ptr;
17421 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
17422
17423 per_objfile->per_bfd->addr.read (objfile);
17424 if (per_objfile->per_bfd->addr.buffer == NULL)
17425 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17426 objfile_name (objfile));
17427 if (addr_base_or_zero + addr_index * addr_size
17428 >= per_objfile->per_bfd->addr.size)
17429 error (_("DW_FORM_addr_index pointing outside of "
17430 ".debug_addr section [in module %s]"),
17431 objfile_name (objfile));
17432 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
17433 + addr_index * addr_size);
17434 if (addr_size == 4)
17435 return bfd_get_32 (abfd, info_ptr);
17436 else
17437 return bfd_get_64 (abfd, info_ptr);
17438 }
17439
17440 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17441
17442 static CORE_ADDR
17443 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17444 {
17445 return read_addr_index_1 (cu->per_objfile, addr_index,
17446 cu->addr_base, cu->header.addr_size);
17447 }
17448
17449 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17450
17451 static CORE_ADDR
17452 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17453 unsigned int *bytes_read)
17454 {
17455 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
17456 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17457
17458 return read_addr_index (cu, addr_index);
17459 }
17460
17461 /* See read.h. */
17462
17463 CORE_ADDR
17464 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
17465 dwarf2_per_objfile *per_objfile,
17466 unsigned int addr_index)
17467 {
17468 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
17469 gdb::optional<ULONGEST> addr_base;
17470 int addr_size;
17471
17472 /* We need addr_base and addr_size.
17473 If we don't have PER_CU->cu, we have to get it.
17474 Nasty, but the alternative is storing the needed info in PER_CU,
17475 which at this point doesn't seem justified: it's not clear how frequently
17476 it would get used and it would increase the size of every PER_CU.
17477 Entry points like dwarf2_per_cu_addr_size do a similar thing
17478 so we're not in uncharted territory here.
17479 Alas we need to be a bit more complicated as addr_base is contained
17480 in the DIE.
17481
17482 We don't need to read the entire CU(/TU).
17483 We just need the header and top level die.
17484
17485 IWBN to use the aging mechanism to let us lazily later discard the CU.
17486 For now we skip this optimization. */
17487
17488 if (cu != NULL)
17489 {
17490 addr_base = cu->addr_base;
17491 addr_size = cu->header.addr_size;
17492 }
17493 else
17494 {
17495 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
17496 addr_base = reader.cu->addr_base;
17497 addr_size = reader.cu->header.addr_size;
17498 }
17499
17500 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
17501 }
17502
17503 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
17504 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
17505 DWO file. */
17506
17507 static const char *
17508 read_str_index (struct dwarf2_cu *cu,
17509 struct dwarf2_section_info *str_section,
17510 struct dwarf2_section_info *str_offsets_section,
17511 ULONGEST str_offsets_base, ULONGEST str_index,
17512 unsigned offset_size)
17513 {
17514 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17515 struct objfile *objfile = per_objfile->objfile;
17516 const char *objf_name = objfile_name (objfile);
17517 bfd *abfd = objfile->obfd.get ();
17518 const gdb_byte *info_ptr;
17519 ULONGEST str_offset;
17520 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
17521
17522 str_section->read (objfile);
17523 str_offsets_section->read (objfile);
17524 if (str_section->buffer == NULL)
17525 error (_("%s used without %s section"
17526 " in CU at offset %s [in module %s]"),
17527 form_name, str_section->get_name (),
17528 sect_offset_str (cu->header.sect_off), objf_name);
17529 if (str_offsets_section->buffer == NULL)
17530 error (_("%s used without %s section"
17531 " in CU at offset %s [in module %s]"),
17532 form_name, str_section->get_name (),
17533 sect_offset_str (cu->header.sect_off), objf_name);
17534 info_ptr = (str_offsets_section->buffer
17535 + str_offsets_base
17536 + str_index * offset_size);
17537 if (offset_size == 4)
17538 str_offset = bfd_get_32 (abfd, info_ptr);
17539 else
17540 str_offset = bfd_get_64 (abfd, info_ptr);
17541 if (str_offset >= str_section->size)
17542 error (_("Offset from %s pointing outside of"
17543 " .debug_str.dwo section in CU at offset %s [in module %s]"),
17544 form_name, sect_offset_str (cu->header.sect_off), objf_name);
17545 return (const char *) (str_section->buffer + str_offset);
17546 }
17547
17548 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
17549
17550 static const char *
17551 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17552 {
17553 unsigned offset_size;
17554 ULONGEST str_offsets_base;
17555 if (reader->cu->header.version >= 5)
17556 {
17557 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
17558 so assume the .debug_str_offsets section is DWARF5 as well, and
17559 parse the header. FIXME: Parse the header only once. */
17560 unsigned int bytes_read = 0;
17561 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
17562 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
17563
17564 /* Header: Initial length. */
17565 read_initial_length (abfd, p + bytes_read, &bytes_read);
17566
17567 /* Determine offset_size based on the .debug_str_offsets header. */
17568 const bool dwarf5_is_dwarf64 = bytes_read != 4;
17569 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
17570
17571 /* Header: Version. */
17572 unsigned version = read_2_bytes (abfd, p + bytes_read);
17573 bytes_read += 2;
17574
17575 if (version <= 4)
17576 {
17577 /* We'd like one warning here about ignoring the section, but
17578 because we parse the header more than once (see FIXME above)
17579 we'd have many warnings, so use a complaint instead, which at
17580 least has a limit. */
17581 complaint (_("Section .debug_str_offsets in %s has unsupported"
17582 " version %d, use empty string."),
17583 reader->dwo_file->dwo_name, version);
17584 return "";
17585 }
17586
17587 /* Header: Padding. */
17588 bytes_read += 2;
17589
17590 str_offsets_base = bytes_read;
17591 }
17592 else
17593 {
17594 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
17595 section, assume the .debug_str_offsets section is pre-DWARF5 as
17596 well, which doesn't have a header. */
17597 str_offsets_base = 0;
17598
17599 /* Determine offset_size based on the .debug_info header. */
17600 offset_size = reader->cu->header.offset_size;
17601 }
17602
17603 return read_str_index (reader->cu,
17604 &reader->dwo_file->sections.str,
17605 &reader->dwo_file->sections.str_offsets,
17606 str_offsets_base, str_index, offset_size);
17607 }
17608
17609 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
17610
17611 static const char *
17612 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
17613 {
17614 struct objfile *objfile = cu->per_objfile->objfile;
17615 const char *objf_name = objfile_name (objfile);
17616 static const char form_name[] = "DW_FORM_GNU_str_index";
17617 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
17618
17619 if (!cu->str_offsets_base.has_value ())
17620 error (_("%s used in Fission stub without %s"
17621 " in CU at offset 0x%lx [in module %s]"),
17622 form_name, str_offsets_attr_name,
17623 (long) cu->header.offset_size, objf_name);
17624
17625 return read_str_index (cu,
17626 &cu->per_objfile->per_bfd->str,
17627 &cu->per_objfile->per_bfd->str_offsets,
17628 *cu->str_offsets_base, str_index,
17629 cu->header.offset_size);
17630 }
17631
17632 /* Return the length of an LEB128 number in BUF. */
17633
17634 static int
17635 leb128_size (const gdb_byte *buf)
17636 {
17637 const gdb_byte *begin = buf;
17638 gdb_byte byte;
17639
17640 while (1)
17641 {
17642 byte = *buf++;
17643 if ((byte & 128) == 0)
17644 return buf - begin;
17645 }
17646 }
17647
17648 static enum language
17649 dwarf_lang_to_enum_language (unsigned int lang)
17650 {
17651 enum language language;
17652
17653 switch (lang)
17654 {
17655 case DW_LANG_C89:
17656 case DW_LANG_C99:
17657 case DW_LANG_C11:
17658 case DW_LANG_C:
17659 case DW_LANG_UPC:
17660 language = language_c;
17661 break;
17662 case DW_LANG_Java:
17663 case DW_LANG_C_plus_plus:
17664 case DW_LANG_C_plus_plus_11:
17665 case DW_LANG_C_plus_plus_14:
17666 language = language_cplus;
17667 break;
17668 case DW_LANG_D:
17669 language = language_d;
17670 break;
17671 case DW_LANG_Fortran77:
17672 case DW_LANG_Fortran90:
17673 case DW_LANG_Fortran95:
17674 case DW_LANG_Fortran03:
17675 case DW_LANG_Fortran08:
17676 language = language_fortran;
17677 break;
17678 case DW_LANG_Go:
17679 language = language_go;
17680 break;
17681 case DW_LANG_Mips_Assembler:
17682 language = language_asm;
17683 break;
17684 case DW_LANG_Ada83:
17685 case DW_LANG_Ada95:
17686 language = language_ada;
17687 break;
17688 case DW_LANG_Modula2:
17689 language = language_m2;
17690 break;
17691 case DW_LANG_Pascal83:
17692 language = language_pascal;
17693 break;
17694 case DW_LANG_ObjC:
17695 language = language_objc;
17696 break;
17697 case DW_LANG_Rust:
17698 case DW_LANG_Rust_old:
17699 language = language_rust;
17700 break;
17701 case DW_LANG_OpenCL:
17702 language = language_opencl;
17703 break;
17704 case DW_LANG_Cobol74:
17705 case DW_LANG_Cobol85:
17706 default:
17707 language = language_minimal;
17708 break;
17709 }
17710
17711 return language;
17712 }
17713
17714 /* Return the named attribute or NULL if not there. */
17715
17716 static struct attribute *
17717 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17718 {
17719 for (;;)
17720 {
17721 unsigned int i;
17722 struct attribute *spec = NULL;
17723
17724 for (i = 0; i < die->num_attrs; ++i)
17725 {
17726 if (die->attrs[i].name == name)
17727 return &die->attrs[i];
17728 if (die->attrs[i].name == DW_AT_specification
17729 || die->attrs[i].name == DW_AT_abstract_origin)
17730 spec = &die->attrs[i];
17731 }
17732
17733 if (!spec)
17734 break;
17735
17736 die = follow_die_ref (die, spec, &cu);
17737 }
17738
17739 return NULL;
17740 }
17741
17742 /* Return the string associated with a string-typed attribute, or NULL if it
17743 is either not found or is of an incorrect type. */
17744
17745 static const char *
17746 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17747 {
17748 struct attribute *attr;
17749 const char *str = NULL;
17750
17751 attr = dwarf2_attr (die, name, cu);
17752
17753 if (attr != NULL)
17754 {
17755 str = attr->as_string ();
17756 if (str == nullptr)
17757 complaint (_("string type expected for attribute %s for "
17758 "DIE at %s in module %s"),
17759 dwarf_attr_name (name), sect_offset_str (die->sect_off),
17760 objfile_name (cu->per_objfile->objfile));
17761 }
17762
17763 return str;
17764 }
17765
17766 /* Return the dwo name or NULL if not present. If present, it is in either
17767 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
17768 static const char *
17769 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
17770 {
17771 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
17772 if (dwo_name == nullptr)
17773 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
17774 return dwo_name;
17775 }
17776
17777 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17778 and holds a non-zero value. This function should only be used for
17779 DW_FORM_flag or DW_FORM_flag_present attributes. */
17780
17781 static int
17782 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17783 {
17784 struct attribute *attr = dwarf2_attr (die, name, cu);
17785
17786 return attr != nullptr && attr->as_boolean ();
17787 }
17788
17789 static int
17790 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17791 {
17792 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17793 which value is non-zero. However, we have to be careful with
17794 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17795 (via dwarf2_flag_true_p) follows this attribute. So we may
17796 end up accidently finding a declaration attribute that belongs
17797 to a different DIE referenced by the specification attribute,
17798 even though the given DIE does not have a declaration attribute. */
17799 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17800 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17801 }
17802
17803 /* Return the die giving the specification for DIE, if there is
17804 one. *SPEC_CU is the CU containing DIE on input, and the CU
17805 containing the return value on output. If there is no
17806 specification, but there is an abstract origin, that is
17807 returned. */
17808
17809 static struct die_info *
17810 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17811 {
17812 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17813 *spec_cu);
17814
17815 if (spec_attr == NULL)
17816 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17817
17818 if (spec_attr == NULL)
17819 return NULL;
17820 else
17821 return follow_die_ref (die, spec_attr, spec_cu);
17822 }
17823
17824 /* A convenience function to find the proper .debug_line section for a CU. */
17825
17826 static struct dwarf2_section_info *
17827 get_debug_line_section (struct dwarf2_cu *cu)
17828 {
17829 struct dwarf2_section_info *section;
17830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17831
17832 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17833 DWO file. */
17834 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17835 section = &cu->dwo_unit->dwo_file->sections.line;
17836 else if (cu->per_cu->is_dwz)
17837 {
17838 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17839
17840 section = &dwz->line;
17841 }
17842 else
17843 section = &per_objfile->per_bfd->line;
17844
17845 return section;
17846 }
17847
17848 /* Read the statement program header starting at OFFSET in
17849 .debug_line, or .debug_line.dwo. Return a pointer
17850 to a struct line_header, allocated using xmalloc.
17851 Returns NULL if there is a problem reading the header, e.g., if it
17852 has a version we don't understand.
17853
17854 NOTE: the strings in the include directory and file name tables of
17855 the returned object point into the dwarf line section buffer,
17856 and must not be freed. */
17857
17858 static line_header_up
17859 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
17860 const char *comp_dir)
17861 {
17862 struct dwarf2_section_info *section;
17863 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17864
17865 section = get_debug_line_section (cu);
17866 section->read (per_objfile->objfile);
17867 if (section->buffer == NULL)
17868 {
17869 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17870 complaint (_("missing .debug_line.dwo section"));
17871 else
17872 complaint (_("missing .debug_line section"));
17873 return 0;
17874 }
17875
17876 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
17877 per_objfile, section, &cu->header,
17878 comp_dir);
17879 }
17880
17881 /* Subroutine of dwarf_decode_lines to simplify it.
17882 Return the file name for the given file_entry.
17883 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
17884 If space for the result is malloc'd, *NAME_HOLDER will be set.
17885 Returns NULL if FILE_INDEX should be ignored, i.e., it is
17886 equivalent to CU_INFO. */
17887
17888 static const char *
17889 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
17890 const file_and_directory &cu_info,
17891 std::string &name_holder)
17892 {
17893 const char *include_name = fe.name;
17894 const char *include_name_to_compare = include_name;
17895
17896 const char *dir_name = fe.include_dir (lh);
17897
17898 std::string hold_compare;
17899 if (!IS_ABSOLUTE_PATH (include_name)
17900 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
17901 {
17902 /* Avoid creating a duplicate name for CU_INFO.
17903 We do this by comparing INCLUDE_NAME and CU_INFO.
17904 Before we do the comparison, however, we need to account
17905 for DIR_NAME and COMP_DIR.
17906 First prepend dir_name (if non-NULL). If we still don't
17907 have an absolute path prepend comp_dir (if non-NULL).
17908 However, the directory we record in the include-file's
17909 psymtab does not contain COMP_DIR (to match the
17910 corresponding symtab(s)).
17911
17912 Example:
17913
17914 bash$ cd /tmp
17915 bash$ gcc -g ./hello.c
17916 include_name = "hello.c"
17917 dir_name = "."
17918 DW_AT_comp_dir = comp_dir = "/tmp"
17919 DW_AT_name = "./hello.c"
17920
17921 */
17922
17923 if (dir_name != NULL)
17924 {
17925 name_holder = path_join (dir_name, include_name);
17926 include_name = name_holder.c_str ();
17927 include_name_to_compare = include_name;
17928 }
17929 if (!IS_ABSOLUTE_PATH (include_name)
17930 && cu_info.get_comp_dir () != nullptr)
17931 {
17932 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
17933 include_name_to_compare = hold_compare.c_str ();
17934 }
17935 }
17936
17937 std::string copied_name;
17938 const char *cu_filename = cu_info.get_name ();
17939 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
17940 {
17941 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
17942 cu_filename = copied_name.c_str ();
17943 }
17944
17945 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
17946 return nullptr;
17947 return include_name;
17948 }
17949
17950 /* State machine to track the state of the line number program. */
17951
17952 class lnp_state_machine
17953 {
17954 public:
17955 /* Initialize a machine state for the start of a line number
17956 program. */
17957 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
17958
17959 file_entry *current_file ()
17960 {
17961 /* lh->file_names is 0-based, but the file name numbers in the
17962 statement program are 1-based. */
17963 return m_line_header->file_name_at (m_file);
17964 }
17965
17966 /* Record the line in the state machine. END_SEQUENCE is true if
17967 we're processing the end of a sequence. */
17968 void record_line (bool end_sequence);
17969
17970 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
17971 nop-out rest of the lines in this sequence. */
17972 void check_line_address (struct dwarf2_cu *cu,
17973 const gdb_byte *line_ptr,
17974 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
17975
17976 void handle_set_discriminator (unsigned int discriminator)
17977 {
17978 m_discriminator = discriminator;
17979 m_line_has_non_zero_discriminator |= discriminator != 0;
17980 }
17981
17982 /* Handle DW_LNE_set_address. */
17983 void handle_set_address (CORE_ADDR address)
17984 {
17985 m_op_index = 0;
17986 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
17987 }
17988
17989 /* Handle DW_LNS_advance_pc. */
17990 void handle_advance_pc (CORE_ADDR adjust);
17991
17992 /* Handle a special opcode. */
17993 void handle_special_opcode (unsigned char op_code);
17994
17995 /* Handle DW_LNS_advance_line. */
17996 void handle_advance_line (int line_delta)
17997 {
17998 advance_line (line_delta);
17999 }
18000
18001 /* Handle DW_LNS_set_file. */
18002 void handle_set_file (file_name_index file);
18003
18004 /* Handle DW_LNS_negate_stmt. */
18005 void handle_negate_stmt ()
18006 {
18007 m_flags ^= LEF_IS_STMT;
18008 }
18009
18010 /* Handle DW_LNS_const_add_pc. */
18011 void handle_const_add_pc ();
18012
18013 /* Handle DW_LNS_fixed_advance_pc. */
18014 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18015 {
18016 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18017 m_op_index = 0;
18018 }
18019
18020 /* Handle DW_LNS_copy. */
18021 void handle_copy ()
18022 {
18023 record_line (false);
18024 m_discriminator = 0;
18025 m_flags &= ~LEF_PROLOGUE_END;
18026 }
18027
18028 /* Handle DW_LNE_end_sequence. */
18029 void handle_end_sequence ()
18030 {
18031 m_currently_recording_lines = true;
18032 }
18033
18034 /* Handle DW_LNS_set_prologue_end. */
18035 void handle_set_prologue_end ()
18036 {
18037 m_flags |= LEF_PROLOGUE_END;
18038 }
18039
18040 private:
18041 /* Advance the line by LINE_DELTA. */
18042 void advance_line (int line_delta)
18043 {
18044 m_line += line_delta;
18045
18046 if (line_delta != 0)
18047 m_line_has_non_zero_discriminator = m_discriminator != 0;
18048 }
18049
18050 struct dwarf2_cu *m_cu;
18051
18052 gdbarch *m_gdbarch;
18053
18054 /* The line number header. */
18055 line_header *m_line_header;
18056
18057 /* These are part of the standard DWARF line number state machine,
18058 and initialized according to the DWARF spec. */
18059
18060 unsigned char m_op_index = 0;
18061 /* The line table index of the current file. */
18062 file_name_index m_file = 1;
18063 unsigned int m_line = 1;
18064
18065 /* These are initialized in the constructor. */
18066
18067 CORE_ADDR m_address;
18068 linetable_entry_flags m_flags;
18069 unsigned int m_discriminator = 0;
18070
18071 /* Additional bits of state we need to track. */
18072
18073 /* The last file that we called dwarf2_start_subfile for.
18074 This is only used for TLLs. */
18075 unsigned int m_last_file = 0;
18076 /* The last file a line number was recorded for. */
18077 struct subfile *m_last_subfile = NULL;
18078
18079 /* The address of the last line entry. */
18080 CORE_ADDR m_last_address;
18081
18082 /* Set to true when a previous line at the same address (using
18083 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
18084 when a line entry at a new address (m_address different to
18085 m_last_address) is processed. */
18086 bool m_stmt_at_address = false;
18087
18088 /* When true, record the lines we decode. */
18089 bool m_currently_recording_lines = true;
18090
18091 /* The last line number that was recorded, used to coalesce
18092 consecutive entries for the same line. This can happen, for
18093 example, when discriminators are present. PR 17276. */
18094 unsigned int m_last_line = 0;
18095 bool m_line_has_non_zero_discriminator = false;
18096 };
18097
18098 void
18099 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18100 {
18101 CORE_ADDR addr_adj = (((m_op_index + adjust)
18102 / m_line_header->maximum_ops_per_instruction)
18103 * m_line_header->minimum_instruction_length);
18104 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18105 m_op_index = ((m_op_index + adjust)
18106 % m_line_header->maximum_ops_per_instruction);
18107 }
18108
18109 void
18110 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18111 {
18112 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18113 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
18114 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
18115 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
18116 / m_line_header->maximum_ops_per_instruction)
18117 * m_line_header->minimum_instruction_length);
18118 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18119 m_op_index = ((m_op_index + adj_opcode_d)
18120 % m_line_header->maximum_ops_per_instruction);
18121
18122 int line_delta = m_line_header->line_base + adj_opcode_r;
18123 advance_line (line_delta);
18124 record_line (false);
18125 m_discriminator = 0;
18126 m_flags &= ~LEF_PROLOGUE_END;
18127 }
18128
18129 void
18130 lnp_state_machine::handle_set_file (file_name_index file)
18131 {
18132 m_file = file;
18133
18134 const file_entry *fe = current_file ();
18135 if (fe == NULL)
18136 dwarf2_debug_line_missing_file_complaint ();
18137 else
18138 {
18139 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18140 m_line_has_non_zero_discriminator = m_discriminator != 0;
18141 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
18142 }
18143 }
18144
18145 void
18146 lnp_state_machine::handle_const_add_pc ()
18147 {
18148 CORE_ADDR adjust
18149 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18150
18151 CORE_ADDR addr_adj
18152 = (((m_op_index + adjust)
18153 / m_line_header->maximum_ops_per_instruction)
18154 * m_line_header->minimum_instruction_length);
18155
18156 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18157 m_op_index = ((m_op_index + adjust)
18158 % m_line_header->maximum_ops_per_instruction);
18159 }
18160
18161 /* Return non-zero if we should add LINE to the line number table.
18162 LINE is the line to add, LAST_LINE is the last line that was added,
18163 LAST_SUBFILE is the subfile for LAST_LINE.
18164 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18165 had a non-zero discriminator.
18166
18167 We have to be careful in the presence of discriminators.
18168 E.g., for this line:
18169
18170 for (i = 0; i < 100000; i++);
18171
18172 clang can emit four line number entries for that one line,
18173 each with a different discriminator.
18174 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18175
18176 However, we want gdb to coalesce all four entries into one.
18177 Otherwise the user could stepi into the middle of the line and
18178 gdb would get confused about whether the pc really was in the
18179 middle of the line.
18180
18181 Things are further complicated by the fact that two consecutive
18182 line number entries for the same line is a heuristic used by gcc
18183 to denote the end of the prologue. So we can't just discard duplicate
18184 entries, we have to be selective about it. The heuristic we use is
18185 that we only collapse consecutive entries for the same line if at least
18186 one of those entries has a non-zero discriminator. PR 17276.
18187
18188 Note: Addresses in the line number state machine can never go backwards
18189 within one sequence, thus this coalescing is ok. */
18190
18191 static int
18192 dwarf_record_line_p (struct dwarf2_cu *cu,
18193 unsigned int line, unsigned int last_line,
18194 int line_has_non_zero_discriminator,
18195 struct subfile *last_subfile)
18196 {
18197 if (cu->get_builder ()->get_current_subfile () != last_subfile)
18198 return 1;
18199 if (line != last_line)
18200 return 1;
18201 /* Same line for the same file that we've seen already.
18202 As a last check, for pr 17276, only record the line if the line
18203 has never had a non-zero discriminator. */
18204 if (!line_has_non_zero_discriminator)
18205 return 1;
18206 return 0;
18207 }
18208
18209 /* Use the CU's builder to record line number LINE beginning at
18210 address ADDRESS in the line table of subfile SUBFILE. */
18211
18212 static void
18213 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18214 unsigned int line, CORE_ADDR address,
18215 linetable_entry_flags flags,
18216 struct dwarf2_cu *cu)
18217 {
18218 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18219
18220 if (dwarf_line_debug)
18221 {
18222 gdb_printf (gdb_stdlog,
18223 "Recording line %u, file %s, address %s\n",
18224 line, lbasename (subfile->name.c_str ()),
18225 paddress (gdbarch, address));
18226 }
18227
18228 if (cu != nullptr)
18229 cu->get_builder ()->record_line (subfile, line, addr, flags);
18230 }
18231
18232 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18233 Mark the end of a set of line number records.
18234 The arguments are the same as for dwarf_record_line_1.
18235 If SUBFILE is NULL the request is ignored. */
18236
18237 static void
18238 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18239 CORE_ADDR address, struct dwarf2_cu *cu)
18240 {
18241 if (subfile == NULL)
18242 return;
18243
18244 if (dwarf_line_debug)
18245 {
18246 gdb_printf (gdb_stdlog,
18247 "Finishing current line, file %s, address %s\n",
18248 lbasename (subfile->name.c_str ()),
18249 paddress (gdbarch, address));
18250 }
18251
18252 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
18253 }
18254
18255 void
18256 lnp_state_machine::record_line (bool end_sequence)
18257 {
18258 if (dwarf_line_debug)
18259 {
18260 gdb_printf (gdb_stdlog,
18261 "Processing actual line %u: file %u,"
18262 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
18263 m_line, m_file,
18264 paddress (m_gdbarch, m_address),
18265 (m_flags & LEF_IS_STMT) != 0,
18266 (m_flags & LEF_PROLOGUE_END) != 0,
18267 m_discriminator,
18268 (end_sequence ? "\t(end sequence)" : ""));
18269 }
18270
18271 file_entry *fe = current_file ();
18272
18273 if (fe == NULL)
18274 dwarf2_debug_line_missing_file_complaint ();
18275 /* For now we ignore lines not starting on an instruction boundary.
18276 But not when processing end_sequence for compatibility with the
18277 previous version of the code. */
18278 else if (m_op_index == 0 || end_sequence)
18279 {
18280 /* When we switch files we insert an end maker in the first file,
18281 switch to the second file and add a new line entry. The
18282 problem is that the end marker inserted in the first file will
18283 discard any previous line entries at the same address. If the
18284 line entries in the first file are marked as is-stmt, while
18285 the new line in the second file is non-stmt, then this means
18286 the end marker will discard is-stmt lines so we can have a
18287 non-stmt line. This means that there are less addresses at
18288 which the user can insert a breakpoint.
18289
18290 To improve this we track the last address in m_last_address,
18291 and whether we have seen an is-stmt at this address. Then
18292 when switching files, if we have seen a stmt at the current
18293 address, and we are switching to create a non-stmt line, then
18294 discard the new line. */
18295 bool file_changed
18296 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
18297 bool ignore_this_line
18298 = ((file_changed && !end_sequence && m_last_address == m_address
18299 && ((m_flags & LEF_IS_STMT) == 0)
18300 && m_stmt_at_address)
18301 || (!end_sequence && m_line == 0));
18302
18303 if ((file_changed && !ignore_this_line) || end_sequence)
18304 {
18305 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
18306 m_currently_recording_lines ? m_cu : nullptr);
18307 }
18308
18309 if (!end_sequence && !ignore_this_line)
18310 {
18311 linetable_entry_flags lte_flags = m_flags;
18312 if (producer_is_codewarrior (m_cu))
18313 lte_flags |= LEF_IS_STMT;
18314
18315 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
18316 m_line_has_non_zero_discriminator,
18317 m_last_subfile))
18318 {
18319 buildsym_compunit *builder = m_cu->get_builder ();
18320 dwarf_record_line_1 (m_gdbarch,
18321 builder->get_current_subfile (),
18322 m_line, m_address, lte_flags,
18323 m_currently_recording_lines ? m_cu : nullptr);
18324 }
18325 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18326 m_last_line = m_line;
18327 }
18328 }
18329
18330 /* Track whether we have seen any IS_STMT true at m_address in case we
18331 have multiple line table entries all at m_address. */
18332 if (m_last_address != m_address)
18333 {
18334 m_stmt_at_address = false;
18335 m_last_address = m_address;
18336 }
18337 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
18338 }
18339
18340 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
18341 line_header *lh)
18342 : m_cu (cu),
18343 m_gdbarch (arch),
18344 m_line_header (lh),
18345 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
18346 if there was a line entry for it so that the backend has a
18347 chance to adjust it and also record it in case it needs it.
18348 This is currently used by MIPS code,
18349 cf. `mips_adjust_dwarf2_line'. */
18350 m_address (gdbarch_adjust_dwarf2_line (arch, 0, 0)),
18351 m_flags (lh->default_is_stmt ? LEF_IS_STMT : (linetable_entry_flags) 0),
18352 m_last_address (m_address)
18353 {
18354 }
18355
18356 void
18357 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18358 const gdb_byte *line_ptr,
18359 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
18360 {
18361 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
18362 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
18363 located at 0x0. In this case, additionally check that if
18364 ADDRESS < UNRELOCATED_LOWPC. */
18365
18366 if ((address == 0 && address < unrelocated_lowpc)
18367 || address == (CORE_ADDR) -1)
18368 {
18369 /* This line table is for a function which has been
18370 GCd by the linker. Ignore it. PR gdb/12528 */
18371
18372 struct objfile *objfile = cu->per_objfile->objfile;
18373 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18374
18375 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18376 line_offset, objfile_name (objfile));
18377 m_currently_recording_lines = false;
18378 /* Note: m_currently_recording_lines is left as false until we see
18379 DW_LNE_end_sequence. */
18380 }
18381 }
18382
18383 /* Subroutine of dwarf_decode_lines to simplify it.
18384 Process the line number information in LH. */
18385
18386 static void
18387 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18388 CORE_ADDR lowpc)
18389 {
18390 const gdb_byte *line_ptr, *extended_end;
18391 const gdb_byte *line_end;
18392 unsigned int bytes_read, extended_len;
18393 unsigned char op_code, extended_op;
18394 CORE_ADDR baseaddr;
18395 struct objfile *objfile = cu->per_objfile->objfile;
18396 bfd *abfd = objfile->obfd.get ();
18397 struct gdbarch *gdbarch = objfile->arch ();
18398
18399 baseaddr = objfile->text_section_offset ();
18400
18401 line_ptr = lh->statement_program_start;
18402 line_end = lh->statement_program_end;
18403
18404 /* Read the statement sequences until there's nothing left. */
18405 while (line_ptr < line_end)
18406 {
18407 /* The DWARF line number program state machine. Reset the state
18408 machine at the start of each sequence. */
18409 lnp_state_machine state_machine (cu, gdbarch, lh);
18410 bool end_sequence = false;
18411
18412 /* Start a subfile for the current file of the state
18413 machine. */
18414 const file_entry *fe = state_machine.current_file ();
18415
18416 if (fe != NULL)
18417 dwarf2_start_subfile (cu, *fe, *lh);
18418
18419 /* Decode the table. */
18420 while (line_ptr < line_end && !end_sequence)
18421 {
18422 op_code = read_1_byte (abfd, line_ptr);
18423 line_ptr += 1;
18424
18425 if (op_code >= lh->opcode_base)
18426 {
18427 /* Special opcode. */
18428 state_machine.handle_special_opcode (op_code);
18429 }
18430 else switch (op_code)
18431 {
18432 case DW_LNS_extended_op:
18433 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18434 &bytes_read);
18435 line_ptr += bytes_read;
18436 extended_end = line_ptr + extended_len;
18437 extended_op = read_1_byte (abfd, line_ptr);
18438 line_ptr += 1;
18439 if (DW_LNE_lo_user <= extended_op
18440 && extended_op <= DW_LNE_hi_user)
18441 {
18442 /* Vendor extension, ignore. */
18443 line_ptr = extended_end;
18444 break;
18445 }
18446 switch (extended_op)
18447 {
18448 case DW_LNE_end_sequence:
18449 state_machine.handle_end_sequence ();
18450 end_sequence = true;
18451 break;
18452 case DW_LNE_set_address:
18453 {
18454 CORE_ADDR address
18455 = cu->header.read_address (abfd, line_ptr, &bytes_read);
18456 line_ptr += bytes_read;
18457
18458 state_machine.check_line_address (cu, line_ptr,
18459 lowpc - baseaddr, address);
18460 state_machine.handle_set_address (address);
18461 }
18462 break;
18463 case DW_LNE_define_file:
18464 {
18465 const char *cur_file;
18466 unsigned int mod_time, length;
18467 dir_index dindex;
18468
18469 cur_file = read_direct_string (abfd, line_ptr,
18470 &bytes_read);
18471 line_ptr += bytes_read;
18472 dindex = (dir_index)
18473 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18474 line_ptr += bytes_read;
18475 mod_time =
18476 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18477 line_ptr += bytes_read;
18478 length =
18479 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18480 line_ptr += bytes_read;
18481 lh->add_file_name (cur_file, dindex, mod_time, length);
18482 }
18483 break;
18484 case DW_LNE_set_discriminator:
18485 {
18486 /* The discriminator is not interesting to the
18487 debugger; just ignore it. We still need to
18488 check its value though:
18489 if there are consecutive entries for the same
18490 (non-prologue) line we want to coalesce them.
18491 PR 17276. */
18492 unsigned int discr
18493 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18494 line_ptr += bytes_read;
18495
18496 state_machine.handle_set_discriminator (discr);
18497 }
18498 break;
18499 default:
18500 complaint (_("mangled .debug_line section"));
18501 return;
18502 }
18503 /* Make sure that we parsed the extended op correctly. If e.g.
18504 we expected a different address size than the producer used,
18505 we may have read the wrong number of bytes. */
18506 if (line_ptr != extended_end)
18507 {
18508 complaint (_("mangled .debug_line section"));
18509 return;
18510 }
18511 break;
18512 case DW_LNS_copy:
18513 state_machine.handle_copy ();
18514 break;
18515 case DW_LNS_advance_pc:
18516 {
18517 CORE_ADDR adjust
18518 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18519 line_ptr += bytes_read;
18520
18521 state_machine.handle_advance_pc (adjust);
18522 }
18523 break;
18524 case DW_LNS_advance_line:
18525 {
18526 int line_delta
18527 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18528 line_ptr += bytes_read;
18529
18530 state_machine.handle_advance_line (line_delta);
18531 }
18532 break;
18533 case DW_LNS_set_file:
18534 {
18535 file_name_index file
18536 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18537 &bytes_read);
18538 line_ptr += bytes_read;
18539
18540 state_machine.handle_set_file (file);
18541 }
18542 break;
18543 case DW_LNS_set_column:
18544 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18545 line_ptr += bytes_read;
18546 break;
18547 case DW_LNS_negate_stmt:
18548 state_machine.handle_negate_stmt ();
18549 break;
18550 case DW_LNS_set_basic_block:
18551 break;
18552 /* Add to the address register of the state machine the
18553 address increment value corresponding to special opcode
18554 255. I.e., this value is scaled by the minimum
18555 instruction length since special opcode 255 would have
18556 scaled the increment. */
18557 case DW_LNS_const_add_pc:
18558 state_machine.handle_const_add_pc ();
18559 break;
18560 case DW_LNS_fixed_advance_pc:
18561 {
18562 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18563 line_ptr += 2;
18564
18565 state_machine.handle_fixed_advance_pc (addr_adj);
18566 }
18567 break;
18568 case DW_LNS_set_prologue_end:
18569 state_machine.handle_set_prologue_end ();
18570 break;
18571 default:
18572 {
18573 /* Unknown standard opcode, ignore it. */
18574 int i;
18575
18576 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18577 {
18578 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18579 line_ptr += bytes_read;
18580 }
18581 }
18582 }
18583 }
18584
18585 if (!end_sequence)
18586 dwarf2_debug_line_missing_end_sequence_complaint ();
18587
18588 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18589 in which case we still finish recording the last line). */
18590 state_machine.record_line (true);
18591 }
18592 }
18593
18594 /* Decode the Line Number Program (LNP) for the given line_header
18595 structure and CU. The actual information extracted and the type
18596 of structures created from the LNP depends on the value of PST.
18597
18598 FND holds the CU file name and directory, if known.
18599 It is used for relative paths in the line table.
18600
18601 NOTE: It is important that psymtabs have the same file name (via
18602 strcmp) as the corresponding symtab. Since the directory is not
18603 used in the name of the symtab we don't use it in the name of the
18604 psymtabs we create. E.g. expand_line_sal requires this when
18605 finding psymtabs to expand. A good testcase for this is
18606 mb-inline.exp.
18607
18608 LOWPC is the lowest address in CU (or 0 if not known).
18609
18610 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18611 for its PC<->lines mapping information. Otherwise only the filename
18612 table is read in. */
18613
18614 static void
18615 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
18616 CORE_ADDR lowpc, int decode_mapping)
18617 {
18618 if (decode_mapping)
18619 dwarf_decode_lines_1 (lh, cu, lowpc);
18620
18621 /* Make sure a symtab is created for every file, even files
18622 which contain only variables (i.e. no code with associated
18623 line numbers). */
18624 buildsym_compunit *builder = cu->get_builder ();
18625 struct compunit_symtab *cust = builder->get_compunit_symtab ();
18626
18627 for (auto &fe : lh->file_names ())
18628 {
18629 dwarf2_start_subfile (cu, fe, *lh);
18630 subfile *sf = builder->get_current_subfile ();
18631
18632 if (sf->symtab == nullptr)
18633 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
18634 sf->name_for_id.c_str ());
18635
18636 fe.symtab = sf->symtab;
18637 }
18638 }
18639
18640 /* Start a subfile for DWARF. FILENAME is the name of the file and
18641 DIRNAME the name of the source directory which contains FILENAME
18642 or NULL if not known.
18643 This routine tries to keep line numbers from identical absolute and
18644 relative file names in a common subfile.
18645
18646 Using the `list' example from the GDB testsuite, which resides in
18647 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18648 of /srcdir/list0.c yields the following debugging information for list0.c:
18649
18650 DW_AT_name: /srcdir/list0.c
18651 DW_AT_comp_dir: /compdir
18652 files.files[0].name: list0.h
18653 files.files[0].dir: /srcdir
18654 files.files[1].name: list0.c
18655 files.files[1].dir: /srcdir
18656
18657 The line number information for list0.c has to end up in a single
18658 subfile, so that `break /srcdir/list0.c:1' works as expected.
18659 start_subfile will ensure that this happens provided that we pass the
18660 concatenation of files.files[1].dir and files.files[1].name as the
18661 subfile's name. */
18662
18663 static void
18664 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
18665 const line_header &lh)
18666 {
18667 std::string filename_holder;
18668 const char *filename = fe.name;
18669 const char *dirname = lh.include_dir_at (fe.d_index);
18670
18671 /* In order not to lose the line information directory,
18672 we concatenate it to the filename when it makes sense.
18673 Note that the Dwarf3 standard says (speaking of filenames in line
18674 information): ``The directory index is ignored for file names
18675 that represent full path names''. Thus ignoring dirname in the
18676 `else' branch below isn't an issue. */
18677
18678 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18679 {
18680 filename_holder = path_join (dirname, filename);
18681 filename = filename_holder.c_str ();
18682 }
18683
18684 std::string filename_for_id = lh.file_file_name (fe);
18685 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
18686 }
18687
18688 static void
18689 var_decode_location (struct attribute *attr, struct symbol *sym,
18690 struct dwarf2_cu *cu)
18691 {
18692 struct objfile *objfile = cu->per_objfile->objfile;
18693 struct comp_unit_head *cu_header = &cu->header;
18694
18695 /* NOTE drow/2003-01-30: There used to be a comment and some special
18696 code here to turn a symbol with DW_AT_external and a
18697 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18698 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18699 with some versions of binutils) where shared libraries could have
18700 relocations against symbols in their debug information - the
18701 minimal symbol would have the right address, but the debug info
18702 would not. It's no longer necessary, because we will explicitly
18703 apply relocations when we read in the debug information now. */
18704
18705 /* A DW_AT_location attribute with no contents indicates that a
18706 variable has been optimized away. */
18707 if (attr->form_is_block () && attr->as_block ()->size == 0)
18708 {
18709 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18710 return;
18711 }
18712
18713 /* Handle one degenerate form of location expression specially, to
18714 preserve GDB's previous behavior when section offsets are
18715 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
18716 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
18717
18718 if (attr->form_is_block ())
18719 {
18720 struct dwarf_block *block = attr->as_block ();
18721
18722 if ((block->data[0] == DW_OP_addr
18723 && block->size == 1 + cu_header->addr_size)
18724 || ((block->data[0] == DW_OP_GNU_addr_index
18725 || block->data[0] == DW_OP_addrx)
18726 && (block->size
18727 == 1 + leb128_size (&block->data[1]))))
18728 {
18729 unsigned int dummy;
18730
18731 if (block->data[0] == DW_OP_addr)
18732 sym->set_value_address
18733 (cu->header.read_address (objfile->obfd.get (), block->data + 1,
18734 &dummy));
18735 else
18736 sym->set_value_address
18737 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
18738 sym->set_aclass_index (LOC_STATIC);
18739 fixup_symbol_section (sym, objfile);
18740 sym->set_value_address
18741 (sym->value_address ()
18742 + objfile->section_offsets[sym->section_index ()]);
18743 return;
18744 }
18745 }
18746
18747 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18748 expression evaluator, and use LOC_COMPUTED only when necessary
18749 (i.e. when the value of a register or memory location is
18750 referenced, or a thread-local block, etc.). Then again, it might
18751 not be worthwhile. I'm assuming that it isn't unless performance
18752 or memory numbers show me otherwise. */
18753
18754 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18755
18756 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18757 cu->has_loclist = true;
18758 }
18759
18760 /* Given a pointer to a DWARF information entry, figure out if we need
18761 to make a symbol table entry for it, and if so, create a new entry
18762 and return a pointer to it.
18763 If TYPE is NULL, determine symbol type from the die, otherwise
18764 used the passed type.
18765 If SPACE is not NULL, use it to hold the new symbol. If it is
18766 NULL, allocate a new symbol on the objfile's obstack. */
18767
18768 static struct symbol *
18769 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18770 struct symbol *space)
18771 {
18772 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18773 struct objfile *objfile = per_objfile->objfile;
18774 struct gdbarch *gdbarch = objfile->arch ();
18775 struct symbol *sym = NULL;
18776 const char *name;
18777 struct attribute *attr = NULL;
18778 struct attribute *attr2 = NULL;
18779 CORE_ADDR baseaddr;
18780 struct pending **list_to_add = NULL;
18781
18782 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18783
18784 baseaddr = objfile->text_section_offset ();
18785
18786 name = dwarf2_name (die, cu);
18787 if (name == nullptr && (die->tag == DW_TAG_subprogram
18788 || die->tag == DW_TAG_inlined_subroutine
18789 || die->tag == DW_TAG_entry_point))
18790 name = dw2_linkage_name (die, cu);
18791
18792 if (name)
18793 {
18794 int suppress_add = 0;
18795
18796 if (space)
18797 sym = space;
18798 else
18799 sym = new (&objfile->objfile_obstack) symbol;
18800 OBJSTAT (objfile, n_syms++);
18801
18802 /* Cache this symbol's name and the name's demangled form (if any). */
18803 sym->set_language (cu->lang (), &objfile->objfile_obstack);
18804 /* Fortran does not have mangling standard and the mangling does differ
18805 between gfortran, iFort etc. */
18806 const char *physname
18807 = (cu->lang () == language_fortran
18808 ? dwarf2_full_name (name, die, cu)
18809 : dwarf2_physname (name, die, cu));
18810 const char *linkagename = dw2_linkage_name (die, cu);
18811
18812 if (linkagename == nullptr || cu->lang () == language_ada)
18813 sym->set_linkage_name (physname);
18814 else
18815 {
18816 sym->set_demangled_name (physname, &objfile->objfile_obstack);
18817 sym->set_linkage_name (linkagename);
18818 }
18819
18820 /* Handle DW_AT_artificial. */
18821 attr = dwarf2_attr (die, DW_AT_artificial, cu);
18822 if (attr != nullptr)
18823 sym->set_is_artificial (attr->as_boolean ());
18824
18825 /* Default assumptions.
18826 Use the passed type or decode it from the die. */
18827 sym->set_domain (VAR_DOMAIN);
18828 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18829 if (type != NULL)
18830 sym->set_type (type);
18831 else
18832 sym->set_type (die_type (die, cu));
18833 attr = dwarf2_attr (die,
18834 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18835 cu);
18836 if (attr != nullptr)
18837 sym->set_line (attr->constant_value (0));
18838
18839 attr = dwarf2_attr (die,
18840 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18841 cu);
18842 if (attr != nullptr && attr->is_nonnegative ())
18843 {
18844 file_name_index file_index
18845 = (file_name_index) attr->as_nonnegative ();
18846 struct file_entry *fe;
18847
18848 if (cu->line_header != NULL)
18849 fe = cu->line_header->file_name_at (file_index);
18850 else
18851 fe = NULL;
18852
18853 if (fe == NULL)
18854 complaint (_("file index out of range"));
18855 else
18856 sym->set_symtab (fe->symtab);
18857 }
18858
18859 switch (die->tag)
18860 {
18861 case DW_TAG_label:
18862 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18863 if (attr != nullptr)
18864 {
18865 CORE_ADDR addr;
18866
18867 addr = attr->as_address ();
18868 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18869 sym->set_section_index (SECT_OFF_TEXT (objfile));
18870 sym->set_value_address (addr);
18871 sym->set_aclass_index (LOC_LABEL);
18872 }
18873 else
18874 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18875 sym->set_type (objfile_type (objfile)->builtin_core_addr);
18876 sym->set_domain (LABEL_DOMAIN);
18877 add_symbol_to_list (sym, cu->list_in_scope);
18878 break;
18879 case DW_TAG_subprogram:
18880 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18881 finish_block. */
18882 sym->set_aclass_index (LOC_BLOCK);
18883 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18884 if ((attr2 != nullptr && attr2->as_boolean ())
18885 || cu->lang () == language_ada
18886 || cu->lang () == language_fortran)
18887 {
18888 /* Subprograms marked external are stored as a global symbol.
18889 Ada and Fortran subprograms, whether marked external or
18890 not, are always stored as a global symbol, because we want
18891 to be able to access them globally. For instance, we want
18892 to be able to break on a nested subprogram without having
18893 to specify the context. */
18894 list_to_add = cu->get_builder ()->get_global_symbols ();
18895 }
18896 else
18897 {
18898 list_to_add = cu->list_in_scope;
18899 }
18900 break;
18901 case DW_TAG_inlined_subroutine:
18902 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18903 finish_block. */
18904 sym->set_aclass_index (LOC_BLOCK);
18905 sym->set_is_inlined (1);
18906 list_to_add = cu->list_in_scope;
18907 break;
18908 case DW_TAG_template_value_param:
18909 suppress_add = 1;
18910 /* Fall through. */
18911 case DW_TAG_constant:
18912 case DW_TAG_variable:
18913 case DW_TAG_member:
18914 /* Compilation with minimal debug info may result in
18915 variables with missing type entries. Change the
18916 misleading `void' type to something sensible. */
18917 if (sym->type ()->code () == TYPE_CODE_VOID)
18918 sym->set_type (objfile_type (objfile)->builtin_int);
18919
18920 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18921 /* In the case of DW_TAG_member, we should only be called for
18922 static const members. */
18923 if (die->tag == DW_TAG_member)
18924 {
18925 /* dwarf2_add_field uses die_is_declaration,
18926 so we do the same. */
18927 gdb_assert (die_is_declaration (die, cu));
18928 gdb_assert (attr);
18929 }
18930 if (attr != nullptr)
18931 {
18932 dwarf2_const_value (attr, sym, cu);
18933 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18934 if (!suppress_add)
18935 {
18936 if (attr2 != nullptr && attr2->as_boolean ())
18937 list_to_add = cu->get_builder ()->get_global_symbols ();
18938 else
18939 list_to_add = cu->list_in_scope;
18940 }
18941 break;
18942 }
18943 attr = dwarf2_attr (die, DW_AT_location, cu);
18944 if (attr != nullptr)
18945 {
18946 var_decode_location (attr, sym, cu);
18947 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18948
18949 /* Fortran explicitly imports any global symbols to the local
18950 scope by DW_TAG_common_block. */
18951 if (cu->lang () == language_fortran && die->parent
18952 && die->parent->tag == DW_TAG_common_block)
18953 attr2 = NULL;
18954
18955 if (sym->aclass () == LOC_STATIC
18956 && sym->value_address () == 0
18957 && !per_objfile->per_bfd->has_section_at_zero)
18958 {
18959 /* When a static variable is eliminated by the linker,
18960 the corresponding debug information is not stripped
18961 out, but the variable address is set to null;
18962 do not add such variables into symbol table. */
18963 }
18964 else if (attr2 != nullptr && attr2->as_boolean ())
18965 {
18966 if (sym->aclass () == LOC_STATIC
18967 && (objfile->flags & OBJF_MAINLINE) == 0
18968 && per_objfile->per_bfd->can_copy)
18969 {
18970 /* A global static variable might be subject to
18971 copy relocation. We first check for a local
18972 minsym, though, because maybe the symbol was
18973 marked hidden, in which case this would not
18974 apply. */
18975 bound_minimal_symbol found
18976 = (lookup_minimal_symbol_linkage
18977 (sym->linkage_name (), objfile));
18978 if (found.minsym != nullptr)
18979 sym->maybe_copied = 1;
18980 }
18981
18982 /* A variable with DW_AT_external is never static,
18983 but it may be block-scoped. */
18984 list_to_add
18985 = ((cu->list_in_scope
18986 == cu->get_builder ()->get_file_symbols ())
18987 ? cu->get_builder ()->get_global_symbols ()
18988 : cu->list_in_scope);
18989 }
18990 else
18991 list_to_add = cu->list_in_scope;
18992 }
18993 else
18994 {
18995 /* We do not know the address of this symbol.
18996 If it is an external symbol and we have type information
18997 for it, enter the symbol as a LOC_UNRESOLVED symbol.
18998 The address of the variable will then be determined from
18999 the minimal symbol table whenever the variable is
19000 referenced. */
19001 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19002
19003 /* Fortran explicitly imports any global symbols to the local
19004 scope by DW_TAG_common_block. */
19005 if (cu->lang () == language_fortran && die->parent
19006 && die->parent->tag == DW_TAG_common_block)
19007 {
19008 /* SYMBOL_CLASS doesn't matter here because
19009 read_common_block is going to reset it. */
19010 if (!suppress_add)
19011 list_to_add = cu->list_in_scope;
19012 }
19013 else if (attr2 != nullptr && attr2->as_boolean ()
19014 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19015 {
19016 /* A variable with DW_AT_external is never static, but it
19017 may be block-scoped. */
19018 list_to_add
19019 = ((cu->list_in_scope
19020 == cu->get_builder ()->get_file_symbols ())
19021 ? cu->get_builder ()->get_global_symbols ()
19022 : cu->list_in_scope);
19023
19024 sym->set_aclass_index (LOC_UNRESOLVED);
19025 }
19026 else if (!die_is_declaration (die, cu))
19027 {
19028 /* Use the default LOC_OPTIMIZED_OUT class. */
19029 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
19030 if (!suppress_add)
19031 list_to_add = cu->list_in_scope;
19032 }
19033 }
19034 break;
19035 case DW_TAG_formal_parameter:
19036 {
19037 /* If we are inside a function, mark this as an argument. If
19038 not, we might be looking at an argument to an inlined function
19039 when we do not have enough information to show inlined frames;
19040 pretend it's a local variable in that case so that the user can
19041 still see it. */
19042 struct context_stack *curr
19043 = cu->get_builder ()->get_current_context_stack ();
19044 if (curr != nullptr && curr->name != nullptr)
19045 sym->set_is_argument (1);
19046 attr = dwarf2_attr (die, DW_AT_location, cu);
19047 if (attr != nullptr)
19048 {
19049 var_decode_location (attr, sym, cu);
19050 }
19051 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19052 if (attr != nullptr)
19053 {
19054 dwarf2_const_value (attr, sym, cu);
19055 }
19056
19057 list_to_add = cu->list_in_scope;
19058 }
19059 break;
19060 case DW_TAG_unspecified_parameters:
19061 /* From varargs functions; gdb doesn't seem to have any
19062 interest in this information, so just ignore it for now.
19063 (FIXME?) */
19064 break;
19065 case DW_TAG_template_type_param:
19066 suppress_add = 1;
19067 /* Fall through. */
19068 case DW_TAG_class_type:
19069 case DW_TAG_interface_type:
19070 case DW_TAG_structure_type:
19071 case DW_TAG_union_type:
19072 case DW_TAG_set_type:
19073 case DW_TAG_enumeration_type:
19074 case DW_TAG_namelist:
19075 if (die->tag == DW_TAG_namelist)
19076 {
19077 sym->set_aclass_index (LOC_STATIC);
19078 sym->set_domain (VAR_DOMAIN);
19079 }
19080 else
19081 {
19082 sym->set_aclass_index (LOC_TYPEDEF);
19083 sym->set_domain (STRUCT_DOMAIN);
19084 }
19085 {
19086 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19087 really ever be static objects: otherwise, if you try
19088 to, say, break of a class's method and you're in a file
19089 which doesn't mention that class, it won't work unless
19090 the check for all static symbols in lookup_symbol_aux
19091 saves you. See the OtherFileClass tests in
19092 gdb.c++/namespace.exp. */
19093
19094 if (!suppress_add)
19095 {
19096 buildsym_compunit *builder = cu->get_builder ();
19097 list_to_add
19098 = (cu->list_in_scope == builder->get_file_symbols ()
19099 && cu->lang () == language_cplus
19100 ? builder->get_global_symbols ()
19101 : cu->list_in_scope);
19102
19103 /* The semantics of C++ state that "struct foo {
19104 ... }" also defines a typedef for "foo". */
19105 if (cu->lang () == language_cplus
19106 || cu->lang () == language_ada
19107 || cu->lang () == language_d
19108 || cu->lang () == language_rust)
19109 {
19110 /* The symbol's name is already allocated along
19111 with this objfile, so we don't need to
19112 duplicate it for the type. */
19113 if (sym->type ()->name () == 0)
19114 sym->type ()->set_name (sym->search_name ());
19115 }
19116 }
19117 }
19118 break;
19119 case DW_TAG_typedef:
19120 sym->set_aclass_index (LOC_TYPEDEF);
19121 sym->set_domain (VAR_DOMAIN);
19122 list_to_add = cu->list_in_scope;
19123 break;
19124 case DW_TAG_array_type:
19125 case DW_TAG_base_type:
19126 case DW_TAG_subrange_type:
19127 case DW_TAG_generic_subrange:
19128 case DW_TAG_unspecified_type:
19129 sym->set_aclass_index (LOC_TYPEDEF);
19130 sym->set_domain (VAR_DOMAIN);
19131 list_to_add = cu->list_in_scope;
19132 break;
19133 case DW_TAG_enumerator:
19134 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19135 if (attr != nullptr)
19136 {
19137 dwarf2_const_value (attr, sym, cu);
19138 }
19139 {
19140 /* NOTE: carlton/2003-11-10: See comment above in the
19141 DW_TAG_class_type, etc. block. */
19142
19143 list_to_add
19144 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
19145 && cu->lang () == language_cplus
19146 ? cu->get_builder ()->get_global_symbols ()
19147 : cu->list_in_scope);
19148 }
19149 break;
19150 case DW_TAG_imported_declaration:
19151 case DW_TAG_namespace:
19152 sym->set_aclass_index (LOC_TYPEDEF);
19153 list_to_add = cu->get_builder ()->get_global_symbols ();
19154 break;
19155 case DW_TAG_module:
19156 sym->set_aclass_index (LOC_TYPEDEF);
19157 sym->set_domain (MODULE_DOMAIN);
19158 list_to_add = cu->get_builder ()->get_global_symbols ();
19159 break;
19160 case DW_TAG_common_block:
19161 sym->set_aclass_index (LOC_COMMON_BLOCK);
19162 sym->set_domain (COMMON_BLOCK_DOMAIN);
19163 add_symbol_to_list (sym, cu->list_in_scope);
19164 break;
19165 default:
19166 /* Not a tag we recognize. Hopefully we aren't processing
19167 trash data, but since we must specifically ignore things
19168 we don't recognize, there is nothing else we should do at
19169 this point. */
19170 complaint (_("unsupported tag: '%s'"),
19171 dwarf_tag_name (die->tag));
19172 break;
19173 }
19174
19175 if (suppress_add)
19176 {
19177 sym->hash_next = objfile->template_symbols;
19178 objfile->template_symbols = sym;
19179 list_to_add = NULL;
19180 }
19181
19182 if (list_to_add != NULL)
19183 add_symbol_to_list (sym, list_to_add);
19184
19185 /* For the benefit of old versions of GCC, check for anonymous
19186 namespaces based on the demangled name. */
19187 if (!cu->processing_has_namespace_info
19188 && cu->lang () == language_cplus)
19189 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
19190 }
19191 return (sym);
19192 }
19193
19194 /* Given an attr with a DW_FORM_dataN value in host byte order,
19195 zero-extend it as appropriate for the symbol's type. The DWARF
19196 standard (v4) is not entirely clear about the meaning of using
19197 DW_FORM_dataN for a constant with a signed type, where the type is
19198 wider than the data. The conclusion of a discussion on the DWARF
19199 list was that this is unspecified. We choose to always zero-extend
19200 because that is the interpretation long in use by GCC. */
19201
19202 static gdb_byte *
19203 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19204 struct dwarf2_cu *cu, LONGEST *value, int bits)
19205 {
19206 struct objfile *objfile = cu->per_objfile->objfile;
19207 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
19208 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19209 LONGEST l = attr->constant_value (0);
19210
19211 if (bits < sizeof (*value) * 8)
19212 {
19213 l &= ((LONGEST) 1 << bits) - 1;
19214 *value = l;
19215 }
19216 else if (bits == sizeof (*value) * 8)
19217 *value = l;
19218 else
19219 {
19220 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19221 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19222 return bytes;
19223 }
19224
19225 return NULL;
19226 }
19227
19228 /* Read a constant value from an attribute. Either set *VALUE, or if
19229 the value does not fit in *VALUE, set *BYTES - either already
19230 allocated on the objfile obstack, or newly allocated on OBSTACK,
19231 or, set *BATON, if we translated the constant to a location
19232 expression. */
19233
19234 static void
19235 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19236 const char *name, struct obstack *obstack,
19237 struct dwarf2_cu *cu,
19238 LONGEST *value, const gdb_byte **bytes,
19239 struct dwarf2_locexpr_baton **baton)
19240 {
19241 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19242 struct objfile *objfile = per_objfile->objfile;
19243 struct comp_unit_head *cu_header = &cu->header;
19244 struct dwarf_block *blk;
19245 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
19246 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19247
19248 *value = 0;
19249 *bytes = NULL;
19250 *baton = NULL;
19251
19252 switch (attr->form)
19253 {
19254 case DW_FORM_addr:
19255 case DW_FORM_addrx:
19256 case DW_FORM_GNU_addr_index:
19257 {
19258 gdb_byte *data;
19259
19260 if (type->length () != cu_header->addr_size)
19261 dwarf2_const_value_length_mismatch_complaint (name,
19262 cu_header->addr_size,
19263 type->length ());
19264 /* Symbols of this form are reasonably rare, so we just
19265 piggyback on the existing location code rather than writing
19266 a new implementation of symbol_computed_ops. */
19267 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19268 (*baton)->per_objfile = per_objfile;
19269 (*baton)->per_cu = cu->per_cu;
19270 gdb_assert ((*baton)->per_cu);
19271
19272 (*baton)->size = 2 + cu_header->addr_size;
19273 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19274 (*baton)->data = data;
19275
19276 data[0] = DW_OP_addr;
19277 store_unsigned_integer (&data[1], cu_header->addr_size,
19278 byte_order, attr->as_address ());
19279 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19280 }
19281 break;
19282 case DW_FORM_string:
19283 case DW_FORM_strp:
19284 case DW_FORM_strx:
19285 case DW_FORM_GNU_str_index:
19286 case DW_FORM_GNU_strp_alt:
19287 /* The string is already allocated on the objfile obstack, point
19288 directly to it. */
19289 *bytes = (const gdb_byte *) attr->as_string ();
19290 break;
19291 case DW_FORM_block1:
19292 case DW_FORM_block2:
19293 case DW_FORM_block4:
19294 case DW_FORM_block:
19295 case DW_FORM_exprloc:
19296 case DW_FORM_data16:
19297 blk = attr->as_block ();
19298 if (type->length () != blk->size)
19299 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19300 type->length ());
19301 *bytes = blk->data;
19302 break;
19303
19304 /* The DW_AT_const_value attributes are supposed to carry the
19305 symbol's value "represented as it would be on the target
19306 architecture." By the time we get here, it's already been
19307 converted to host endianness, so we just need to sign- or
19308 zero-extend it as appropriate. */
19309 case DW_FORM_data1:
19310 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19311 break;
19312 case DW_FORM_data2:
19313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19314 break;
19315 case DW_FORM_data4:
19316 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19317 break;
19318 case DW_FORM_data8:
19319 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19320 break;
19321
19322 case DW_FORM_sdata:
19323 case DW_FORM_implicit_const:
19324 *value = attr->as_signed ();
19325 break;
19326
19327 case DW_FORM_udata:
19328 *value = attr->as_unsigned ();
19329 break;
19330
19331 default:
19332 complaint (_("unsupported const value attribute form: '%s'"),
19333 dwarf_form_name (attr->form));
19334 *value = 0;
19335 break;
19336 }
19337 }
19338
19339
19340 /* Copy constant value from an attribute to a symbol. */
19341
19342 static void
19343 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19344 struct dwarf2_cu *cu)
19345 {
19346 struct objfile *objfile = cu->per_objfile->objfile;
19347 LONGEST value;
19348 const gdb_byte *bytes;
19349 struct dwarf2_locexpr_baton *baton;
19350
19351 dwarf2_const_value_attr (attr, sym->type (),
19352 sym->print_name (),
19353 &objfile->objfile_obstack, cu,
19354 &value, &bytes, &baton);
19355
19356 if (baton != NULL)
19357 {
19358 SYMBOL_LOCATION_BATON (sym) = baton;
19359 sym->set_aclass_index (dwarf2_locexpr_index);
19360 }
19361 else if (bytes != NULL)
19362 {
19363 sym->set_value_bytes (bytes);
19364 sym->set_aclass_index (LOC_CONST_BYTES);
19365 }
19366 else
19367 {
19368 sym->set_value_longest (value);
19369 sym->set_aclass_index (LOC_CONST);
19370 }
19371 }
19372
19373 /* Return the type of the die in question using its DW_AT_type attribute. */
19374
19375 static struct type *
19376 die_type (struct die_info *die, struct dwarf2_cu *cu)
19377 {
19378 struct attribute *type_attr;
19379
19380 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19381 if (!type_attr)
19382 {
19383 struct objfile *objfile = cu->per_objfile->objfile;
19384 /* A missing DW_AT_type represents a void type. */
19385 return objfile_type (objfile)->builtin_void;
19386 }
19387
19388 return lookup_die_type (die, type_attr, cu);
19389 }
19390
19391 /* True iff CU's producer generates GNAT Ada auxiliary information
19392 that allows to find parallel types through that information instead
19393 of having to do expensive parallel lookups by type name. */
19394
19395 static int
19396 need_gnat_info (struct dwarf2_cu *cu)
19397 {
19398 /* Assume that the Ada compiler was GNAT, which always produces
19399 the auxiliary information. */
19400 return (cu->lang () == language_ada);
19401 }
19402
19403 /* Return the auxiliary type of the die in question using its
19404 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19405 attribute is not present. */
19406
19407 static struct type *
19408 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19409 {
19410 struct attribute *type_attr;
19411
19412 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19413 if (!type_attr)
19414 return NULL;
19415
19416 return lookup_die_type (die, type_attr, cu);
19417 }
19418
19419 /* If DIE has a descriptive_type attribute, then set the TYPE's
19420 descriptive type accordingly. */
19421
19422 static void
19423 set_descriptive_type (struct type *type, struct die_info *die,
19424 struct dwarf2_cu *cu)
19425 {
19426 struct type *descriptive_type = die_descriptive_type (die, cu);
19427
19428 if (descriptive_type)
19429 {
19430 ALLOCATE_GNAT_AUX_TYPE (type);
19431 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19432 }
19433 }
19434
19435 /* Return the containing type of the die in question using its
19436 DW_AT_containing_type attribute. */
19437
19438 static struct type *
19439 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19440 {
19441 struct attribute *type_attr;
19442 struct objfile *objfile = cu->per_objfile->objfile;
19443
19444 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19445 if (!type_attr)
19446 error (_("Dwarf Error: Problem turning containing type into gdb type "
19447 "[in module %s]"), objfile_name (objfile));
19448
19449 return lookup_die_type (die, type_attr, cu);
19450 }
19451
19452 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19453
19454 static struct type *
19455 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19456 {
19457 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19458 struct objfile *objfile = per_objfile->objfile;
19459 char *saved;
19460
19461 std::string message
19462 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
19463 objfile_name (objfile),
19464 sect_offset_str (cu->header.sect_off),
19465 sect_offset_str (die->sect_off));
19466 saved = obstack_strdup (&objfile->objfile_obstack, message);
19467
19468 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19469 }
19470
19471 /* Look up the type of DIE in CU using its type attribute ATTR.
19472 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19473 DW_AT_containing_type.
19474 If there is no type substitute an error marker. */
19475
19476 static struct type *
19477 lookup_die_type (struct die_info *die, const struct attribute *attr,
19478 struct dwarf2_cu *cu)
19479 {
19480 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19481 struct objfile *objfile = per_objfile->objfile;
19482 struct type *this_type;
19483
19484 gdb_assert (attr->name == DW_AT_type
19485 || attr->name == DW_AT_GNAT_descriptive_type
19486 || attr->name == DW_AT_containing_type);
19487
19488 /* First see if we have it cached. */
19489
19490 if (attr->form == DW_FORM_GNU_ref_alt)
19491 {
19492 struct dwarf2_per_cu_data *per_cu;
19493 sect_offset sect_off = attr->get_ref_die_offset ();
19494
19495 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
19496 per_objfile->per_bfd);
19497 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
19498 }
19499 else if (attr->form_is_ref ())
19500 {
19501 sect_offset sect_off = attr->get_ref_die_offset ();
19502
19503 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
19504 }
19505 else if (attr->form == DW_FORM_ref_sig8)
19506 {
19507 ULONGEST signature = attr->as_signature ();
19508
19509 return get_signatured_type (die, signature, cu);
19510 }
19511 else
19512 {
19513 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
19514 " at %s [in module %s]"),
19515 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
19516 objfile_name (objfile));
19517 return build_error_marker_type (cu, die);
19518 }
19519
19520 /* If not cached we need to read it in. */
19521
19522 if (this_type == NULL)
19523 {
19524 struct die_info *type_die = NULL;
19525 struct dwarf2_cu *type_cu = cu;
19526
19527 if (attr->form_is_ref ())
19528 type_die = follow_die_ref (die, attr, &type_cu);
19529 if (type_die == NULL)
19530 return build_error_marker_type (cu, die);
19531 /* If we find the type now, it's probably because the type came
19532 from an inter-CU reference and the type's CU got expanded before
19533 ours. */
19534 this_type = read_type_die (type_die, type_cu);
19535 }
19536
19537 /* If we still don't have a type use an error marker. */
19538
19539 if (this_type == NULL)
19540 return build_error_marker_type (cu, die);
19541
19542 return this_type;
19543 }
19544
19545 /* Return the type in DIE, CU.
19546 Returns NULL for invalid types.
19547
19548 This first does a lookup in die_type_hash,
19549 and only reads the die in if necessary.
19550
19551 NOTE: This can be called when reading in partial or full symbols. */
19552
19553 static struct type *
19554 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19555 {
19556 struct type *this_type;
19557
19558 this_type = get_die_type (die, cu);
19559 if (this_type)
19560 return this_type;
19561
19562 return read_type_die_1 (die, cu);
19563 }
19564
19565 /* Read the type in DIE, CU.
19566 Returns NULL for invalid types. */
19567
19568 static struct type *
19569 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19570 {
19571 struct type *this_type = NULL;
19572
19573 switch (die->tag)
19574 {
19575 case DW_TAG_class_type:
19576 case DW_TAG_interface_type:
19577 case DW_TAG_structure_type:
19578 case DW_TAG_union_type:
19579 this_type = read_structure_type (die, cu);
19580 break;
19581 case DW_TAG_enumeration_type:
19582 this_type = read_enumeration_type (die, cu);
19583 break;
19584 case DW_TAG_subprogram:
19585 case DW_TAG_subroutine_type:
19586 case DW_TAG_inlined_subroutine:
19587 this_type = read_subroutine_type (die, cu);
19588 break;
19589 case DW_TAG_array_type:
19590 this_type = read_array_type (die, cu);
19591 break;
19592 case DW_TAG_set_type:
19593 this_type = read_set_type (die, cu);
19594 break;
19595 case DW_TAG_pointer_type:
19596 this_type = read_tag_pointer_type (die, cu);
19597 break;
19598 case DW_TAG_ptr_to_member_type:
19599 this_type = read_tag_ptr_to_member_type (die, cu);
19600 break;
19601 case DW_TAG_reference_type:
19602 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19603 break;
19604 case DW_TAG_rvalue_reference_type:
19605 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19606 break;
19607 case DW_TAG_const_type:
19608 this_type = read_tag_const_type (die, cu);
19609 break;
19610 case DW_TAG_volatile_type:
19611 this_type = read_tag_volatile_type (die, cu);
19612 break;
19613 case DW_TAG_restrict_type:
19614 this_type = read_tag_restrict_type (die, cu);
19615 break;
19616 case DW_TAG_string_type:
19617 this_type = read_tag_string_type (die, cu);
19618 break;
19619 case DW_TAG_typedef:
19620 this_type = read_typedef (die, cu);
19621 break;
19622 case DW_TAG_generic_subrange:
19623 case DW_TAG_subrange_type:
19624 this_type = read_subrange_type (die, cu);
19625 break;
19626 case DW_TAG_base_type:
19627 this_type = read_base_type (die, cu);
19628 break;
19629 case DW_TAG_unspecified_type:
19630 this_type = read_unspecified_type (die, cu);
19631 break;
19632 case DW_TAG_namespace:
19633 this_type = read_namespace_type (die, cu);
19634 break;
19635 case DW_TAG_module:
19636 this_type = read_module_type (die, cu);
19637 break;
19638 case DW_TAG_atomic_type:
19639 this_type = read_tag_atomic_type (die, cu);
19640 break;
19641 default:
19642 complaint (_("unexpected tag in read_type_die: '%s'"),
19643 dwarf_tag_name (die->tag));
19644 break;
19645 }
19646
19647 return this_type;
19648 }
19649
19650 /* See if we can figure out if the class lives in a namespace. We do
19651 this by looking for a member function; its demangled name will
19652 contain namespace info, if there is any.
19653 Return the computed name or NULL.
19654 Space for the result is allocated on the objfile's obstack.
19655 This is the full-die version of guess_partial_die_structure_name.
19656 In this case we know DIE has no useful parent. */
19657
19658 static const char *
19659 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19660 {
19661 struct die_info *spec_die;
19662 struct dwarf2_cu *spec_cu;
19663 struct die_info *child;
19664 struct objfile *objfile = cu->per_objfile->objfile;
19665
19666 spec_cu = cu;
19667 spec_die = die_specification (die, &spec_cu);
19668 if (spec_die != NULL)
19669 {
19670 die = spec_die;
19671 cu = spec_cu;
19672 }
19673
19674 for (child = die->child;
19675 child != NULL;
19676 child = child->sibling)
19677 {
19678 if (child->tag == DW_TAG_subprogram)
19679 {
19680 const char *linkage_name = dw2_linkage_name (child, cu);
19681
19682 if (linkage_name != NULL)
19683 {
19684 gdb::unique_xmalloc_ptr<char> actual_name
19685 (cu->language_defn->class_name_from_physname (linkage_name));
19686 const char *name = NULL;
19687
19688 if (actual_name != NULL)
19689 {
19690 const char *die_name = dwarf2_name (die, cu);
19691
19692 if (die_name != NULL
19693 && strcmp (die_name, actual_name.get ()) != 0)
19694 {
19695 /* Strip off the class name from the full name.
19696 We want the prefix. */
19697 int die_name_len = strlen (die_name);
19698 int actual_name_len = strlen (actual_name.get ());
19699 const char *ptr = actual_name.get ();
19700
19701 /* Test for '::' as a sanity check. */
19702 if (actual_name_len > die_name_len + 2
19703 && ptr[actual_name_len - die_name_len - 1] == ':')
19704 name = obstack_strndup (
19705 &objfile->per_bfd->storage_obstack,
19706 ptr, actual_name_len - die_name_len - 2);
19707 }
19708 }
19709 return name;
19710 }
19711 }
19712 }
19713
19714 return NULL;
19715 }
19716
19717 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19718 prefix part in such case. See
19719 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19720
19721 static const char *
19722 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19723 {
19724 struct attribute *attr;
19725 const char *base;
19726
19727 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19728 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19729 return NULL;
19730
19731 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19732 return NULL;
19733
19734 attr = dw2_linkage_name_attr (die, cu);
19735 const char *attr_name = attr->as_string ();
19736 if (attr == NULL || attr_name == NULL)
19737 return NULL;
19738
19739 /* dwarf2_name had to be already called. */
19740 gdb_assert (attr->canonical_string_p ());
19741
19742 /* Strip the base name, keep any leading namespaces/classes. */
19743 base = strrchr (attr_name, ':');
19744 if (base == NULL || base == attr_name || base[-1] != ':')
19745 return "";
19746
19747 struct objfile *objfile = cu->per_objfile->objfile;
19748 return obstack_strndup (&objfile->per_bfd->storage_obstack,
19749 attr_name,
19750 &base[-1] - attr_name);
19751 }
19752
19753 /* Return the name of the namespace/class that DIE is defined within,
19754 or "" if we can't tell. The caller should not xfree the result.
19755
19756 For example, if we're within the method foo() in the following
19757 code:
19758
19759 namespace N {
19760 class C {
19761 void foo () {
19762 }
19763 };
19764 }
19765
19766 then determine_prefix on foo's die will return "N::C". */
19767
19768 static const char *
19769 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19770 {
19771 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19772 struct die_info *parent, *spec_die;
19773 struct dwarf2_cu *spec_cu;
19774 struct type *parent_type;
19775 const char *retval;
19776
19777 if (cu->lang () != language_cplus
19778 && cu->lang () != language_fortran
19779 && cu->lang () != language_d
19780 && cu->lang () != language_rust)
19781 return "";
19782
19783 retval = anonymous_struct_prefix (die, cu);
19784 if (retval)
19785 return retval;
19786
19787 /* We have to be careful in the presence of DW_AT_specification.
19788 For example, with GCC 3.4, given the code
19789
19790 namespace N {
19791 void foo() {
19792 // Definition of N::foo.
19793 }
19794 }
19795
19796 then we'll have a tree of DIEs like this:
19797
19798 1: DW_TAG_compile_unit
19799 2: DW_TAG_namespace // N
19800 3: DW_TAG_subprogram // declaration of N::foo
19801 4: DW_TAG_subprogram // definition of N::foo
19802 DW_AT_specification // refers to die #3
19803
19804 Thus, when processing die #4, we have to pretend that we're in
19805 the context of its DW_AT_specification, namely the contex of die
19806 #3. */
19807 spec_cu = cu;
19808 spec_die = die_specification (die, &spec_cu);
19809 if (spec_die == NULL)
19810 parent = die->parent;
19811 else
19812 {
19813 parent = spec_die->parent;
19814 cu = spec_cu;
19815 }
19816
19817 if (parent == NULL)
19818 return "";
19819 else if (parent->building_fullname)
19820 {
19821 const char *name;
19822 const char *parent_name;
19823
19824 /* It has been seen on RealView 2.2 built binaries,
19825 DW_TAG_template_type_param types actually _defined_ as
19826 children of the parent class:
19827
19828 enum E {};
19829 template class <class Enum> Class{};
19830 Class<enum E> class_e;
19831
19832 1: DW_TAG_class_type (Class)
19833 2: DW_TAG_enumeration_type (E)
19834 3: DW_TAG_enumerator (enum1:0)
19835 3: DW_TAG_enumerator (enum2:1)
19836 ...
19837 2: DW_TAG_template_type_param
19838 DW_AT_type DW_FORM_ref_udata (E)
19839
19840 Besides being broken debug info, it can put GDB into an
19841 infinite loop. Consider:
19842
19843 When we're building the full name for Class<E>, we'll start
19844 at Class, and go look over its template type parameters,
19845 finding E. We'll then try to build the full name of E, and
19846 reach here. We're now trying to build the full name of E,
19847 and look over the parent DIE for containing scope. In the
19848 broken case, if we followed the parent DIE of E, we'd again
19849 find Class, and once again go look at its template type
19850 arguments, etc., etc. Simply don't consider such parent die
19851 as source-level parent of this die (it can't be, the language
19852 doesn't allow it), and break the loop here. */
19853 name = dwarf2_name (die, cu);
19854 parent_name = dwarf2_name (parent, cu);
19855 complaint (_("template param type '%s' defined within parent '%s'"),
19856 name ? name : "<unknown>",
19857 parent_name ? parent_name : "<unknown>");
19858 return "";
19859 }
19860 else
19861 switch (parent->tag)
19862 {
19863 case DW_TAG_namespace:
19864 parent_type = read_type_die (parent, cu);
19865 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19866 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19867 Work around this problem here. */
19868 if (cu->lang () == language_cplus
19869 && strcmp (parent_type->name (), "::") == 0)
19870 return "";
19871 /* We give a name to even anonymous namespaces. */
19872 return parent_type->name ();
19873 case DW_TAG_class_type:
19874 case DW_TAG_interface_type:
19875 case DW_TAG_structure_type:
19876 case DW_TAG_union_type:
19877 case DW_TAG_module:
19878 parent_type = read_type_die (parent, cu);
19879 if (parent_type->name () != NULL)
19880 return parent_type->name ();
19881 else
19882 /* An anonymous structure is only allowed non-static data
19883 members; no typedefs, no member functions, et cetera.
19884 So it does not need a prefix. */
19885 return "";
19886 case DW_TAG_compile_unit:
19887 case DW_TAG_partial_unit:
19888 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19889 if (cu->lang () == language_cplus
19890 && !per_objfile->per_bfd->types.empty ()
19891 && die->child != NULL
19892 && (die->tag == DW_TAG_class_type
19893 || die->tag == DW_TAG_structure_type
19894 || die->tag == DW_TAG_union_type))
19895 {
19896 const char *name = guess_full_die_structure_name (die, cu);
19897 if (name != NULL)
19898 return name;
19899 }
19900 return "";
19901 case DW_TAG_subprogram:
19902 /* Nested subroutines in Fortran get a prefix with the name
19903 of the parent's subroutine. */
19904 if (cu->lang () == language_fortran)
19905 {
19906 if ((die->tag == DW_TAG_subprogram)
19907 && (dwarf2_name (parent, cu) != NULL))
19908 return dwarf2_name (parent, cu);
19909 }
19910 return "";
19911 case DW_TAG_enumeration_type:
19912 parent_type = read_type_die (parent, cu);
19913 if (parent_type->is_declared_class ())
19914 {
19915 if (parent_type->name () != NULL)
19916 return parent_type->name ();
19917 return "";
19918 }
19919 /* Fall through. */
19920 default:
19921 return determine_prefix (parent, cu);
19922 }
19923 }
19924
19925 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19926 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19927 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19928 an obconcat, otherwise allocate storage for the result. The CU argument is
19929 used to determine the language and hence, the appropriate separator. */
19930
19931 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19932
19933 static char *
19934 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19935 int physname, struct dwarf2_cu *cu)
19936 {
19937 const char *lead = "";
19938 const char *sep;
19939
19940 if (suffix == NULL || suffix[0] == '\0'
19941 || prefix == NULL || prefix[0] == '\0')
19942 sep = "";
19943 else if (cu->lang () == language_d)
19944 {
19945 /* For D, the 'main' function could be defined in any module, but it
19946 should never be prefixed. */
19947 if (strcmp (suffix, "D main") == 0)
19948 {
19949 prefix = "";
19950 sep = "";
19951 }
19952 else
19953 sep = ".";
19954 }
19955 else if (cu->lang () == language_fortran && physname)
19956 {
19957 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19958 DW_AT_MIPS_linkage_name is preferred and used instead. */
19959
19960 lead = "__";
19961 sep = "_MOD_";
19962 }
19963 else
19964 sep = "::";
19965
19966 if (prefix == NULL)
19967 prefix = "";
19968 if (suffix == NULL)
19969 suffix = "";
19970
19971 if (obs == NULL)
19972 {
19973 char *retval
19974 = ((char *)
19975 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19976
19977 strcpy (retval, lead);
19978 strcat (retval, prefix);
19979 strcat (retval, sep);
19980 strcat (retval, suffix);
19981 return retval;
19982 }
19983 else
19984 {
19985 /* We have an obstack. */
19986 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19987 }
19988 }
19989
19990 /* Return a generic name for a DW_TAG_template_type_param or
19991 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
19992 per parent, so each function/class/struct template will have their own set
19993 of template parameters named <unnnamed0>, <unnamed1>, ... where the
19994 enumeration starts at 0 and represents the position of the template tag in
19995 the list of unnamed template tags for this parent, counting both, type and
19996 value tags. */
19997
19998 static const char *
19999 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
20000 {
20001 if (die->parent == nullptr)
20002 return nullptr;
20003
20004 /* Count the parent types unnamed template type and value children until, we
20005 arrive at our entry. */
20006 size_t nth_unnamed = 0;
20007
20008 die_info *child = die->parent->child;
20009 while (child != die)
20010 {
20011 gdb_assert (child != nullptr);
20012 if (child->tag == DW_TAG_template_type_param
20013 || child->tag == DW_TAG_template_value_param)
20014 {
20015 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
20016 ++nth_unnamed;
20017 }
20018 child = child->sibling;
20019 }
20020
20021 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
20022 return cu->per_objfile->objfile->intern (name_str.c_str ());
20023 }
20024
20025 /* Get name of a die, return NULL if not found. */
20026
20027 static const char *
20028 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20029 struct objfile *objfile)
20030 {
20031 if (name == nullptr)
20032 return name;
20033
20034 if (cu->lang () == language_cplus)
20035 {
20036 gdb::unique_xmalloc_ptr<char> canon_name
20037 = cp_canonicalize_string (name);
20038
20039 if (canon_name != nullptr)
20040 name = objfile->intern (canon_name.get ());
20041 }
20042 else if (cu->lang () == language_c)
20043 {
20044 gdb::unique_xmalloc_ptr<char> canon_name
20045 = c_canonicalize_name (name);
20046
20047 if (canon_name != nullptr)
20048 name = objfile->intern (canon_name.get ());
20049 }
20050
20051 return name;
20052 }
20053
20054 /* Get name of a die, return NULL if not found.
20055 Anonymous namespaces are converted to their magic string. */
20056
20057 static const char *
20058 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20059 {
20060 struct attribute *attr;
20061 struct objfile *objfile = cu->per_objfile->objfile;
20062
20063 attr = dwarf2_attr (die, DW_AT_name, cu);
20064 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
20065 if (attr_name == nullptr
20066 && die->tag != DW_TAG_namespace
20067 && die->tag != DW_TAG_class_type
20068 && die->tag != DW_TAG_interface_type
20069 && die->tag != DW_TAG_structure_type
20070 && die->tag != DW_TAG_namelist
20071 && die->tag != DW_TAG_union_type
20072 && die->tag != DW_TAG_template_type_param
20073 && die->tag != DW_TAG_template_value_param)
20074 return NULL;
20075
20076 switch (die->tag)
20077 {
20078 /* A member's name should not be canonicalized. This is a bit
20079 of a hack, in that normally it should not be possible to run
20080 into this situation; however, the dw2-unusual-field-names.exp
20081 test creates custom DWARF that does. */
20082 case DW_TAG_member:
20083 case DW_TAG_compile_unit:
20084 case DW_TAG_partial_unit:
20085 /* Compilation units have a DW_AT_name that is a filename, not
20086 a source language identifier. */
20087 case DW_TAG_enumeration_type:
20088 case DW_TAG_enumerator:
20089 /* These tags always have simple identifiers already; no need
20090 to canonicalize them. */
20091 return attr_name;
20092
20093 case DW_TAG_namespace:
20094 if (attr_name != nullptr)
20095 return attr_name;
20096 return CP_ANONYMOUS_NAMESPACE_STR;
20097
20098 /* DWARF does not actually require template tags to have a name. */
20099 case DW_TAG_template_type_param:
20100 case DW_TAG_template_value_param:
20101 if (attr_name == nullptr)
20102 return unnamed_template_tag_name (die, cu);
20103 /* FALLTHROUGH. */
20104 case DW_TAG_class_type:
20105 case DW_TAG_interface_type:
20106 case DW_TAG_structure_type:
20107 case DW_TAG_union_type:
20108 case DW_TAG_namelist:
20109 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20110 structures or unions. These were of the form "._%d" in GCC 4.1,
20111 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20112 and GCC 4.4. We work around this problem by ignoring these. */
20113 if (attr_name != nullptr
20114 && (startswith (attr_name, "._")
20115 || startswith (attr_name, "<anonymous")))
20116 return NULL;
20117
20118 /* GCC might emit a nameless typedef that has a linkage name. See
20119 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20120 if (!attr || attr_name == NULL)
20121 {
20122 attr = dw2_linkage_name_attr (die, cu);
20123 attr_name = attr == nullptr ? nullptr : attr->as_string ();
20124 if (attr == NULL || attr_name == NULL)
20125 return NULL;
20126
20127 /* Avoid demangling attr_name the second time on a second
20128 call for the same DIE. */
20129 if (!attr->canonical_string_p ())
20130 {
20131 gdb::unique_xmalloc_ptr<char> demangled
20132 (gdb_demangle (attr_name, DMGL_TYPES));
20133 if (demangled == nullptr)
20134 return nullptr;
20135
20136 attr->set_string_canonical (objfile->intern (demangled.get ()));
20137 attr_name = attr->as_string ();
20138 }
20139
20140 /* Strip any leading namespaces/classes, keep only the
20141 base name. DW_AT_name for named DIEs does not
20142 contain the prefixes. */
20143 const char *base = strrchr (attr_name, ':');
20144 if (base && base > attr_name && base[-1] == ':')
20145 return &base[1];
20146 else
20147 return attr_name;
20148 }
20149 break;
20150
20151 default:
20152 break;
20153 }
20154
20155 if (!attr->canonical_string_p ())
20156 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
20157 objfile));
20158 return attr->as_string ();
20159 }
20160
20161 /* Return the die that this die in an extension of, or NULL if there
20162 is none. *EXT_CU is the CU containing DIE on input, and the CU
20163 containing the return value on output. */
20164
20165 static struct die_info *
20166 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20167 {
20168 struct attribute *attr;
20169
20170 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20171 if (attr == NULL)
20172 return NULL;
20173
20174 return follow_die_ref (die, attr, ext_cu);
20175 }
20176
20177 static void
20178 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20179 {
20180 void **slot;
20181
20182 slot = htab_find_slot_with_hash (cu->die_hash, die,
20183 to_underlying (die->sect_off),
20184 INSERT);
20185
20186 *slot = die;
20187 }
20188
20189 /* Follow reference or signature attribute ATTR of SRC_DIE.
20190 On entry *REF_CU is the CU of SRC_DIE.
20191 On exit *REF_CU is the CU of the result. */
20192
20193 static struct die_info *
20194 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20195 struct dwarf2_cu **ref_cu)
20196 {
20197 struct die_info *die;
20198
20199 if (attr->form_is_ref ())
20200 die = follow_die_ref (src_die, attr, ref_cu);
20201 else if (attr->form == DW_FORM_ref_sig8)
20202 die = follow_die_sig (src_die, attr, ref_cu);
20203 else
20204 {
20205 src_die->error_dump ();
20206 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20207 objfile_name ((*ref_cu)->per_objfile->objfile));
20208 }
20209
20210 return die;
20211 }
20212
20213 /* Follow reference OFFSET.
20214 On entry *REF_CU is the CU of the source die referencing OFFSET.
20215 On exit *REF_CU is the CU of the result.
20216 Returns NULL if OFFSET is invalid. */
20217
20218 static struct die_info *
20219 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20220 struct dwarf2_cu **ref_cu)
20221 {
20222 struct die_info temp_die;
20223 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20224 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20225
20226 gdb_assert (cu->per_cu != NULL);
20227
20228 target_cu = cu;
20229
20230 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
20231 "source CU contains target offset: %d",
20232 sect_offset_str (cu->per_cu->sect_off),
20233 sect_offset_str (sect_off),
20234 cu->header.offset_in_cu_p (sect_off));
20235
20236 if (cu->per_cu->is_debug_types)
20237 {
20238 /* .debug_types CUs cannot reference anything outside their CU.
20239 If they need to, they have to reference a signatured type via
20240 DW_FORM_ref_sig8. */
20241 if (!cu->header.offset_in_cu_p (sect_off))
20242 return NULL;
20243 }
20244 else if (offset_in_dwz != cu->per_cu->is_dwz
20245 || !cu->header.offset_in_cu_p (sect_off))
20246 {
20247 struct dwarf2_per_cu_data *per_cu;
20248
20249 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20250 per_objfile->per_bfd);
20251
20252 dwarf_read_debug_printf_v ("target CU offset: %s, "
20253 "target CU DIEs loaded: %d",
20254 sect_offset_str (per_cu->sect_off),
20255 per_objfile->get_cu (per_cu) != nullptr);
20256
20257 /* If necessary, add it to the queue and load its DIEs.
20258
20259 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20260 it doesn't mean they are currently loaded. Since we require them
20261 to be loaded, we must check for ourselves. */
20262 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
20263 || per_objfile->get_cu (per_cu) == nullptr)
20264 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
20265 false, cu->lang ());
20266
20267 target_cu = per_objfile->get_cu (per_cu);
20268 gdb_assert (target_cu != nullptr);
20269 }
20270 else if (cu->dies == NULL)
20271 {
20272 /* We're loading full DIEs during partial symbol reading. */
20273 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
20274 language_minimal);
20275 }
20276
20277 *ref_cu = target_cu;
20278 temp_die.sect_off = sect_off;
20279
20280 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20281 &temp_die,
20282 to_underlying (sect_off));
20283 }
20284
20285 /* Follow reference attribute ATTR of SRC_DIE.
20286 On entry *REF_CU is the CU of SRC_DIE.
20287 On exit *REF_CU is the CU of the result. */
20288
20289 static struct die_info *
20290 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20291 struct dwarf2_cu **ref_cu)
20292 {
20293 sect_offset sect_off = attr->get_ref_die_offset ();
20294 struct dwarf2_cu *cu = *ref_cu;
20295 struct die_info *die;
20296
20297 die = follow_die_offset (sect_off,
20298 (attr->form == DW_FORM_GNU_ref_alt
20299 || cu->per_cu->is_dwz),
20300 ref_cu);
20301 if (!die)
20302 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
20303 "at %s [in module %s]"),
20304 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
20305 objfile_name (cu->per_objfile->objfile));
20306
20307 return die;
20308 }
20309
20310 /* See read.h. */
20311
20312 struct dwarf2_locexpr_baton
20313 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20314 dwarf2_per_cu_data *per_cu,
20315 dwarf2_per_objfile *per_objfile,
20316 gdb::function_view<CORE_ADDR ()> get_frame_pc,
20317 bool resolve_abstract_p)
20318 {
20319 struct die_info *die;
20320 struct attribute *attr;
20321 struct dwarf2_locexpr_baton retval;
20322 struct objfile *objfile = per_objfile->objfile;
20323
20324 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20325 if (cu == nullptr)
20326 cu = load_cu (per_cu, per_objfile, false);
20327
20328 if (cu == nullptr)
20329 {
20330 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20331 Instead just throw an error, not much else we can do. */
20332 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20333 sect_offset_str (sect_off), objfile_name (objfile));
20334 }
20335
20336 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20337 if (!die)
20338 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20339 sect_offset_str (sect_off), objfile_name (objfile));
20340
20341 attr = dwarf2_attr (die, DW_AT_location, cu);
20342 if (!attr && resolve_abstract_p
20343 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
20344 != per_objfile->per_bfd->abstract_to_concrete.end ()))
20345 {
20346 CORE_ADDR pc = get_frame_pc ();
20347 CORE_ADDR baseaddr = objfile->text_section_offset ();
20348 struct gdbarch *gdbarch = objfile->arch ();
20349
20350 for (const auto &cand_off
20351 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
20352 {
20353 struct dwarf2_cu *cand_cu = cu;
20354 struct die_info *cand
20355 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
20356 if (!cand
20357 || !cand->parent
20358 || cand->parent->tag != DW_TAG_subprogram)
20359 continue;
20360
20361 CORE_ADDR pc_low, pc_high;
20362 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
20363 if (pc_low == ((CORE_ADDR) -1))
20364 continue;
20365 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
20366 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
20367 if (!(pc_low <= pc && pc < pc_high))
20368 continue;
20369
20370 die = cand;
20371 attr = dwarf2_attr (die, DW_AT_location, cu);
20372 break;
20373 }
20374 }
20375
20376 if (!attr)
20377 {
20378 /* DWARF: "If there is no such attribute, then there is no effect.".
20379 DATA is ignored if SIZE is 0. */
20380
20381 retval.data = NULL;
20382 retval.size = 0;
20383 }
20384 else if (attr->form_is_section_offset ())
20385 {
20386 struct dwarf2_loclist_baton loclist_baton;
20387 CORE_ADDR pc = get_frame_pc ();
20388 size_t size;
20389
20390 fill_in_loclist_baton (cu, &loclist_baton, attr);
20391
20392 retval.data = dwarf2_find_location_expression (&loclist_baton,
20393 &size, pc);
20394 retval.size = size;
20395 }
20396 else
20397 {
20398 if (!attr->form_is_block ())
20399 error (_("Dwarf Error: DIE at %s referenced in module %s "
20400 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20401 sect_offset_str (sect_off), objfile_name (objfile));
20402
20403 struct dwarf_block *block = attr->as_block ();
20404 retval.data = block->data;
20405 retval.size = block->size;
20406 }
20407 retval.per_objfile = per_objfile;
20408 retval.per_cu = cu->per_cu;
20409
20410 per_objfile->age_comp_units ();
20411
20412 return retval;
20413 }
20414
20415 /* See read.h. */
20416
20417 struct dwarf2_locexpr_baton
20418 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20419 dwarf2_per_cu_data *per_cu,
20420 dwarf2_per_objfile *per_objfile,
20421 gdb::function_view<CORE_ADDR ()> get_frame_pc)
20422 {
20423 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20424
20425 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
20426 get_frame_pc);
20427 }
20428
20429 /* Write a constant of a given type as target-ordered bytes into
20430 OBSTACK. */
20431
20432 static const gdb_byte *
20433 write_constant_as_bytes (struct obstack *obstack,
20434 enum bfd_endian byte_order,
20435 struct type *type,
20436 ULONGEST value,
20437 LONGEST *len)
20438 {
20439 gdb_byte *result;
20440
20441 *len = type->length ();
20442 result = (gdb_byte *) obstack_alloc (obstack, *len);
20443 store_unsigned_integer (result, *len, byte_order, value);
20444
20445 return result;
20446 }
20447
20448 /* See read.h. */
20449
20450 const gdb_byte *
20451 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20452 dwarf2_per_cu_data *per_cu,
20453 dwarf2_per_objfile *per_objfile,
20454 obstack *obstack,
20455 LONGEST *len)
20456 {
20457 struct die_info *die;
20458 struct attribute *attr;
20459 const gdb_byte *result = NULL;
20460 struct type *type;
20461 LONGEST value;
20462 enum bfd_endian byte_order;
20463 struct objfile *objfile = per_objfile->objfile;
20464
20465 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20466 if (cu == nullptr)
20467 cu = load_cu (per_cu, per_objfile, false);
20468
20469 if (cu == nullptr)
20470 {
20471 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20472 Instead just throw an error, not much else we can do. */
20473 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20474 sect_offset_str (sect_off), objfile_name (objfile));
20475 }
20476
20477 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20478 if (!die)
20479 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20480 sect_offset_str (sect_off), objfile_name (objfile));
20481
20482 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20483 if (attr == NULL)
20484 return NULL;
20485
20486 byte_order = (bfd_big_endian (objfile->obfd.get ())
20487 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20488
20489 switch (attr->form)
20490 {
20491 case DW_FORM_addr:
20492 case DW_FORM_addrx:
20493 case DW_FORM_GNU_addr_index:
20494 {
20495 gdb_byte *tem;
20496
20497 *len = cu->header.addr_size;
20498 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20499 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
20500 result = tem;
20501 }
20502 break;
20503 case DW_FORM_string:
20504 case DW_FORM_strp:
20505 case DW_FORM_strx:
20506 case DW_FORM_GNU_str_index:
20507 case DW_FORM_GNU_strp_alt:
20508 /* The string is already allocated on the objfile obstack, point
20509 directly to it. */
20510 {
20511 const char *attr_name = attr->as_string ();
20512 result = (const gdb_byte *) attr_name;
20513 *len = strlen (attr_name);
20514 }
20515 break;
20516 case DW_FORM_block1:
20517 case DW_FORM_block2:
20518 case DW_FORM_block4:
20519 case DW_FORM_block:
20520 case DW_FORM_exprloc:
20521 case DW_FORM_data16:
20522 {
20523 struct dwarf_block *block = attr->as_block ();
20524 result = block->data;
20525 *len = block->size;
20526 }
20527 break;
20528
20529 /* The DW_AT_const_value attributes are supposed to carry the
20530 symbol's value "represented as it would be on the target
20531 architecture." By the time we get here, it's already been
20532 converted to host endianness, so we just need to sign- or
20533 zero-extend it as appropriate. */
20534 case DW_FORM_data1:
20535 type = die_type (die, cu);
20536 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20537 if (result == NULL)
20538 result = write_constant_as_bytes (obstack, byte_order,
20539 type, value, len);
20540 break;
20541 case DW_FORM_data2:
20542 type = die_type (die, cu);
20543 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20544 if (result == NULL)
20545 result = write_constant_as_bytes (obstack, byte_order,
20546 type, value, len);
20547 break;
20548 case DW_FORM_data4:
20549 type = die_type (die, cu);
20550 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20551 if (result == NULL)
20552 result = write_constant_as_bytes (obstack, byte_order,
20553 type, value, len);
20554 break;
20555 case DW_FORM_data8:
20556 type = die_type (die, cu);
20557 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20558 if (result == NULL)
20559 result = write_constant_as_bytes (obstack, byte_order,
20560 type, value, len);
20561 break;
20562
20563 case DW_FORM_sdata:
20564 case DW_FORM_implicit_const:
20565 type = die_type (die, cu);
20566 result = write_constant_as_bytes (obstack, byte_order,
20567 type, attr->as_signed (), len);
20568 break;
20569
20570 case DW_FORM_udata:
20571 type = die_type (die, cu);
20572 result = write_constant_as_bytes (obstack, byte_order,
20573 type, attr->as_unsigned (), len);
20574 break;
20575
20576 default:
20577 complaint (_("unsupported const value attribute form: '%s'"),
20578 dwarf_form_name (attr->form));
20579 break;
20580 }
20581
20582 return result;
20583 }
20584
20585 /* See read.h. */
20586
20587 struct type *
20588 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20589 dwarf2_per_cu_data *per_cu,
20590 dwarf2_per_objfile *per_objfile,
20591 const char **var_name)
20592 {
20593 struct die_info *die;
20594
20595 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20596 if (cu == nullptr)
20597 cu = load_cu (per_cu, per_objfile, false);
20598
20599 if (cu == nullptr)
20600 return nullptr;
20601
20602 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20603 if (!die)
20604 return NULL;
20605
20606 if (var_name != nullptr)
20607 *var_name = var_decl_name (die, cu);
20608 return die_type (die, cu);
20609 }
20610
20611 /* See read.h. */
20612
20613 struct type *
20614 dwarf2_get_die_type (cu_offset die_offset,
20615 dwarf2_per_cu_data *per_cu,
20616 dwarf2_per_objfile *per_objfile)
20617 {
20618 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20619 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
20620 }
20621
20622 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20623 On entry *REF_CU is the CU of SRC_DIE.
20624 On exit *REF_CU is the CU of the result.
20625 Returns NULL if the referenced DIE isn't found. */
20626
20627 static struct die_info *
20628 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20629 struct dwarf2_cu **ref_cu)
20630 {
20631 struct die_info temp_die;
20632 struct dwarf2_cu *sig_cu;
20633 struct die_info *die;
20634 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
20635
20636
20637 /* While it might be nice to assert sig_type->type == NULL here,
20638 we can get here for DW_AT_imported_declaration where we need
20639 the DIE not the type. */
20640
20641 /* If necessary, add it to the queue and load its DIEs.
20642
20643 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20644 it doesn't mean they are currently loaded. Since we require them
20645 to be loaded, we must check for ourselves. */
20646 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
20647 language_minimal)
20648 || per_objfile->get_cu (sig_type) == nullptr)
20649 read_signatured_type (sig_type, per_objfile);
20650
20651 sig_cu = per_objfile->get_cu (sig_type);
20652 gdb_assert (sig_cu != NULL);
20653 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20654 temp_die.sect_off = sig_type->type_offset_in_section;
20655 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20656 to_underlying (temp_die.sect_off));
20657 if (die)
20658 {
20659 /* For .gdb_index version 7 keep track of included TUs.
20660 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20661 if (per_objfile->per_bfd->index_table != NULL
20662 && !per_objfile->per_bfd->index_table->version_check ())
20663 {
20664 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
20665 }
20666
20667 *ref_cu = sig_cu;
20668 return die;
20669 }
20670
20671 return NULL;
20672 }
20673
20674 /* Follow signatured type referenced by ATTR in SRC_DIE.
20675 On entry *REF_CU is the CU of SRC_DIE.
20676 On exit *REF_CU is the CU of the result.
20677 The result is the DIE of the type.
20678 If the referenced type cannot be found an error is thrown. */
20679
20680 static struct die_info *
20681 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20682 struct dwarf2_cu **ref_cu)
20683 {
20684 ULONGEST signature = attr->as_signature ();
20685 struct signatured_type *sig_type;
20686 struct die_info *die;
20687
20688 gdb_assert (attr->form == DW_FORM_ref_sig8);
20689
20690 sig_type = lookup_signatured_type (*ref_cu, signature);
20691 /* sig_type will be NULL if the signatured type is missing from
20692 the debug info. */
20693 if (sig_type == NULL)
20694 {
20695 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20696 " from DIE at %s [in module %s]"),
20697 hex_string (signature), sect_offset_str (src_die->sect_off),
20698 objfile_name ((*ref_cu)->per_objfile->objfile));
20699 }
20700
20701 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20702 if (die == NULL)
20703 {
20704 src_die->error_dump ();
20705 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20706 " from DIE at %s [in module %s]"),
20707 hex_string (signature), sect_offset_str (src_die->sect_off),
20708 objfile_name ((*ref_cu)->per_objfile->objfile));
20709 }
20710
20711 return die;
20712 }
20713
20714 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20715 reading in and processing the type unit if necessary. */
20716
20717 static struct type *
20718 get_signatured_type (struct die_info *die, ULONGEST signature,
20719 struct dwarf2_cu *cu)
20720 {
20721 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20722 struct signatured_type *sig_type;
20723 struct dwarf2_cu *type_cu;
20724 struct die_info *type_die;
20725 struct type *type;
20726
20727 sig_type = lookup_signatured_type (cu, signature);
20728 /* sig_type will be NULL if the signatured type is missing from
20729 the debug info. */
20730 if (sig_type == NULL)
20731 {
20732 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20733 " from DIE at %s [in module %s]"),
20734 hex_string (signature), sect_offset_str (die->sect_off),
20735 objfile_name (per_objfile->objfile));
20736 return build_error_marker_type (cu, die);
20737 }
20738
20739 /* If we already know the type we're done. */
20740 type = per_objfile->get_type_for_signatured_type (sig_type);
20741 if (type != nullptr)
20742 return type;
20743
20744 type_cu = cu;
20745 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20746 if (type_die != NULL)
20747 {
20748 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20749 is created. This is important, for example, because for c++ classes
20750 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20751 type = read_type_die (type_die, type_cu);
20752 if (type == NULL)
20753 {
20754 complaint (_("Dwarf Error: Cannot build signatured type %s"
20755 " referenced from DIE at %s [in module %s]"),
20756 hex_string (signature), sect_offset_str (die->sect_off),
20757 objfile_name (per_objfile->objfile));
20758 type = build_error_marker_type (cu, die);
20759 }
20760 }
20761 else
20762 {
20763 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20764 " from DIE at %s [in module %s]"),
20765 hex_string (signature), sect_offset_str (die->sect_off),
20766 objfile_name (per_objfile->objfile));
20767 type = build_error_marker_type (cu, die);
20768 }
20769
20770 per_objfile->set_type_for_signatured_type (sig_type, type);
20771
20772 return type;
20773 }
20774
20775 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20776 reading in and processing the type unit if necessary. */
20777
20778 static struct type *
20779 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20780 struct dwarf2_cu *cu) /* ARI: editCase function */
20781 {
20782 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20783 if (attr->form_is_ref ())
20784 {
20785 struct dwarf2_cu *type_cu = cu;
20786 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20787
20788 return read_type_die (type_die, type_cu);
20789 }
20790 else if (attr->form == DW_FORM_ref_sig8)
20791 {
20792 return get_signatured_type (die, attr->as_signature (), cu);
20793 }
20794 else
20795 {
20796 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20797
20798 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20799 " at %s [in module %s]"),
20800 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
20801 objfile_name (per_objfile->objfile));
20802 return build_error_marker_type (cu, die);
20803 }
20804 }
20805
20806 /* Load the DIEs associated with type unit PER_CU into memory. */
20807
20808 static void
20809 load_full_type_unit (dwarf2_per_cu_data *per_cu,
20810 dwarf2_per_objfile *per_objfile)
20811 {
20812 struct signatured_type *sig_type;
20813
20814 /* We have the per_cu, but we need the signatured_type.
20815 Fortunately this is an easy translation. */
20816 gdb_assert (per_cu->is_debug_types);
20817 sig_type = (struct signatured_type *) per_cu;
20818
20819 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
20820
20821 read_signatured_type (sig_type, per_objfile);
20822
20823 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
20824 }
20825
20826 /* Read in a signatured type and build its CU and DIEs.
20827 If the type is a stub for the real type in a DWO file,
20828 read in the real type from the DWO file as well. */
20829
20830 static void
20831 read_signatured_type (signatured_type *sig_type,
20832 dwarf2_per_objfile *per_objfile)
20833 {
20834 gdb_assert (sig_type->is_debug_types);
20835 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
20836
20837 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
20838
20839 if (!reader.dummy_p)
20840 {
20841 struct dwarf2_cu *cu = reader.cu;
20842 const gdb_byte *info_ptr = reader.info_ptr;
20843
20844 gdb_assert (cu->die_hash == NULL);
20845 cu->die_hash =
20846 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
20847 die_info::hash,
20848 die_info::eq,
20849 NULL,
20850 &cu->comp_unit_obstack,
20851 hashtab_obstack_allocate,
20852 dummy_obstack_deallocate);
20853
20854 if (reader.comp_unit_die->has_children)
20855 reader.comp_unit_die->child
20856 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
20857 reader.comp_unit_die);
20858 cu->dies = reader.comp_unit_die;
20859 /* comp_unit_die is not stored in die_hash, no need. */
20860
20861 /* We try not to read any attributes in this function, because
20862 not all CUs needed for references have been loaded yet, and
20863 symbol table processing isn't initialized. But we have to
20864 set the CU language, or we won't be able to build types
20865 correctly. Similarly, if we do not read the producer, we can
20866 not apply producer-specific interpretation. */
20867 prepare_one_comp_unit (cu, cu->dies, language_minimal);
20868
20869 reader.keep ();
20870 }
20871
20872 sig_type->tu_read = 1;
20873 }
20874
20875 /* Decode simple location descriptions.
20876 Given a pointer to a dwarf block that defines a location, compute
20877 the location and return the value. If COMPUTED is non-null, it is
20878 set to true to indicate that decoding was successful, and false
20879 otherwise. If COMPUTED is null, then this function may emit a
20880 complaint. */
20881
20882 static CORE_ADDR
20883 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
20884 {
20885 struct objfile *objfile = cu->per_objfile->objfile;
20886 size_t i;
20887 size_t size = blk->size;
20888 const gdb_byte *data = blk->data;
20889 CORE_ADDR stack[64];
20890 int stacki;
20891 unsigned int bytes_read, unsnd;
20892 gdb_byte op;
20893
20894 if (computed != nullptr)
20895 *computed = false;
20896
20897 i = 0;
20898 stacki = 0;
20899 stack[stacki] = 0;
20900 stack[++stacki] = 0;
20901
20902 while (i < size)
20903 {
20904 op = data[i++];
20905 switch (op)
20906 {
20907 case DW_OP_lit0:
20908 case DW_OP_lit1:
20909 case DW_OP_lit2:
20910 case DW_OP_lit3:
20911 case DW_OP_lit4:
20912 case DW_OP_lit5:
20913 case DW_OP_lit6:
20914 case DW_OP_lit7:
20915 case DW_OP_lit8:
20916 case DW_OP_lit9:
20917 case DW_OP_lit10:
20918 case DW_OP_lit11:
20919 case DW_OP_lit12:
20920 case DW_OP_lit13:
20921 case DW_OP_lit14:
20922 case DW_OP_lit15:
20923 case DW_OP_lit16:
20924 case DW_OP_lit17:
20925 case DW_OP_lit18:
20926 case DW_OP_lit19:
20927 case DW_OP_lit20:
20928 case DW_OP_lit21:
20929 case DW_OP_lit22:
20930 case DW_OP_lit23:
20931 case DW_OP_lit24:
20932 case DW_OP_lit25:
20933 case DW_OP_lit26:
20934 case DW_OP_lit27:
20935 case DW_OP_lit28:
20936 case DW_OP_lit29:
20937 case DW_OP_lit30:
20938 case DW_OP_lit31:
20939 stack[++stacki] = op - DW_OP_lit0;
20940 break;
20941
20942 case DW_OP_reg0:
20943 case DW_OP_reg1:
20944 case DW_OP_reg2:
20945 case DW_OP_reg3:
20946 case DW_OP_reg4:
20947 case DW_OP_reg5:
20948 case DW_OP_reg6:
20949 case DW_OP_reg7:
20950 case DW_OP_reg8:
20951 case DW_OP_reg9:
20952 case DW_OP_reg10:
20953 case DW_OP_reg11:
20954 case DW_OP_reg12:
20955 case DW_OP_reg13:
20956 case DW_OP_reg14:
20957 case DW_OP_reg15:
20958 case DW_OP_reg16:
20959 case DW_OP_reg17:
20960 case DW_OP_reg18:
20961 case DW_OP_reg19:
20962 case DW_OP_reg20:
20963 case DW_OP_reg21:
20964 case DW_OP_reg22:
20965 case DW_OP_reg23:
20966 case DW_OP_reg24:
20967 case DW_OP_reg25:
20968 case DW_OP_reg26:
20969 case DW_OP_reg27:
20970 case DW_OP_reg28:
20971 case DW_OP_reg29:
20972 case DW_OP_reg30:
20973 case DW_OP_reg31:
20974 stack[++stacki] = op - DW_OP_reg0;
20975 if (i < size)
20976 {
20977 if (computed == nullptr)
20978 dwarf2_complex_location_expr_complaint ();
20979 else
20980 return 0;
20981 }
20982 break;
20983
20984 case DW_OP_regx:
20985 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
20986 i += bytes_read;
20987 stack[++stacki] = unsnd;
20988 if (i < size)
20989 {
20990 if (computed == nullptr)
20991 dwarf2_complex_location_expr_complaint ();
20992 else
20993 return 0;
20994 }
20995 break;
20996
20997 case DW_OP_addr:
20998 stack[++stacki] = cu->header.read_address (objfile->obfd.get (),
20999 &data[i],
21000 &bytes_read);
21001 i += bytes_read;
21002 break;
21003
21004 case DW_OP_const1u:
21005 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
21006 i += 1;
21007 break;
21008
21009 case DW_OP_const1s:
21010 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
21011 i += 1;
21012 break;
21013
21014 case DW_OP_const2u:
21015 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
21016 i += 2;
21017 break;
21018
21019 case DW_OP_const2s:
21020 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
21021 i += 2;
21022 break;
21023
21024 case DW_OP_const4u:
21025 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
21026 i += 4;
21027 break;
21028
21029 case DW_OP_const4s:
21030 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
21031 i += 4;
21032 break;
21033
21034 case DW_OP_const8u:
21035 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
21036 i += 8;
21037 break;
21038
21039 case DW_OP_constu:
21040 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21041 &bytes_read);
21042 i += bytes_read;
21043 break;
21044
21045 case DW_OP_consts:
21046 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21047 i += bytes_read;
21048 break;
21049
21050 case DW_OP_dup:
21051 stack[stacki + 1] = stack[stacki];
21052 stacki++;
21053 break;
21054
21055 case DW_OP_plus:
21056 stack[stacki - 1] += stack[stacki];
21057 stacki--;
21058 break;
21059
21060 case DW_OP_plus_uconst:
21061 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21062 &bytes_read);
21063 i += bytes_read;
21064 break;
21065
21066 case DW_OP_minus:
21067 stack[stacki - 1] -= stack[stacki];
21068 stacki--;
21069 break;
21070
21071 case DW_OP_deref:
21072 /* If we're not the last op, then we definitely can't encode
21073 this using GDB's address_class enum. This is valid for partial
21074 global symbols, although the variable's address will be bogus
21075 in the psymtab. */
21076 if (i < size)
21077 {
21078 if (computed == nullptr)
21079 dwarf2_complex_location_expr_complaint ();
21080 else
21081 return 0;
21082 }
21083 break;
21084
21085 case DW_OP_GNU_push_tls_address:
21086 case DW_OP_form_tls_address:
21087 /* The top of the stack has the offset from the beginning
21088 of the thread control block at which the variable is located. */
21089 /* Nothing should follow this operator, so the top of stack would
21090 be returned. */
21091 /* This is valid for partial global symbols, but the variable's
21092 address will be bogus in the psymtab. Make it always at least
21093 non-zero to not look as a variable garbage collected by linker
21094 which have DW_OP_addr 0. */
21095 if (i < size)
21096 {
21097 if (computed == nullptr)
21098 dwarf2_complex_location_expr_complaint ();
21099 else
21100 return 0;
21101 }
21102 stack[stacki]++;
21103 break;
21104
21105 case DW_OP_GNU_uninit:
21106 if (computed != nullptr)
21107 return 0;
21108 break;
21109
21110 case DW_OP_addrx:
21111 case DW_OP_GNU_addr_index:
21112 case DW_OP_GNU_const_index:
21113 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21114 &bytes_read);
21115 i += bytes_read;
21116 break;
21117
21118 default:
21119 if (computed == nullptr)
21120 {
21121 const char *name = get_DW_OP_name (op);
21122
21123 if (name)
21124 complaint (_("unsupported stack op: '%s'"),
21125 name);
21126 else
21127 complaint (_("unsupported stack op: '%02x'"),
21128 op);
21129 }
21130
21131 return (stack[stacki]);
21132 }
21133
21134 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21135 outside of the allocated space. Also enforce minimum>0. */
21136 if (stacki >= ARRAY_SIZE (stack) - 1)
21137 {
21138 if (computed == nullptr)
21139 complaint (_("location description stack overflow"));
21140 return 0;
21141 }
21142
21143 if (stacki <= 0)
21144 {
21145 if (computed == nullptr)
21146 complaint (_("location description stack underflow"));
21147 return 0;
21148 }
21149 }
21150
21151 if (computed != nullptr)
21152 *computed = true;
21153 return (stack[stacki]);
21154 }
21155
21156 /* memory allocation interface */
21157
21158 static struct dwarf_block *
21159 dwarf_alloc_block (struct dwarf2_cu *cu)
21160 {
21161 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21162 }
21163
21164 \f
21165
21166 /* Macro support. */
21167
21168 /* An overload of dwarf_decode_macros that finds the correct section
21169 and ensures it is read in before calling the other overload. */
21170
21171 static void
21172 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
21173 int section_is_gnu)
21174 {
21175 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21176 struct objfile *objfile = per_objfile->objfile;
21177 const struct line_header *lh = cu->line_header;
21178 unsigned int offset_size = cu->header.offset_size;
21179 struct dwarf2_section_info *section;
21180 const char *section_name;
21181
21182 if (cu->dwo_unit != nullptr)
21183 {
21184 if (section_is_gnu)
21185 {
21186 section = &cu->dwo_unit->dwo_file->sections.macro;
21187 section_name = ".debug_macro.dwo";
21188 }
21189 else
21190 {
21191 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21192 section_name = ".debug_macinfo.dwo";
21193 }
21194 }
21195 else
21196 {
21197 if (section_is_gnu)
21198 {
21199 section = &per_objfile->per_bfd->macro;
21200 section_name = ".debug_macro";
21201 }
21202 else
21203 {
21204 section = &per_objfile->per_bfd->macinfo;
21205 section_name = ".debug_macinfo";
21206 }
21207 }
21208
21209 section->read (objfile);
21210 if (section->buffer == nullptr)
21211 {
21212 complaint (_("missing %s section"), section_name);
21213 return;
21214 }
21215
21216 buildsym_compunit *builder = cu->get_builder ();
21217
21218 struct dwarf2_section_info *str_offsets_section;
21219 struct dwarf2_section_info *str_section;
21220 gdb::optional<ULONGEST> str_offsets_base;
21221
21222 if (cu->dwo_unit != nullptr)
21223 {
21224 str_offsets_section = &cu->dwo_unit->dwo_file
21225 ->sections.str_offsets;
21226 str_section = &cu->dwo_unit->dwo_file->sections.str;
21227 str_offsets_base = cu->header.addr_size;
21228 }
21229 else
21230 {
21231 str_offsets_section = &per_objfile->per_bfd->str_offsets;
21232 str_section = &per_objfile->per_bfd->str;
21233 str_offsets_base = cu->str_offsets_base;
21234 }
21235
21236 dwarf_decode_macros (per_objfile, builder, section, lh,
21237 offset_size, offset, str_section, str_offsets_section,
21238 str_offsets_base, section_is_gnu, cu);
21239 }
21240
21241 /* Return the .debug_loc section to use for CU.
21242 For DWO files use .debug_loc.dwo. */
21243
21244 static struct dwarf2_section_info *
21245 cu_debug_loc_section (struct dwarf2_cu *cu)
21246 {
21247 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21248
21249 if (cu->dwo_unit)
21250 {
21251 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21252
21253 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
21254 }
21255 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
21256 : &per_objfile->per_bfd->loc);
21257 }
21258
21259 /* Return the .debug_rnglists section to use for CU. */
21260 static struct dwarf2_section_info *
21261 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
21262 {
21263 if (cu->header.version < 5)
21264 error (_(".debug_rnglists section cannot be used in DWARF %d"),
21265 cu->header.version);
21266 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21267
21268 /* Make sure we read the .debug_rnglists section from the file that
21269 contains the DW_AT_ranges attribute we are reading. Normally that
21270 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
21271 or DW_TAG_skeleton unit, we always want to read from objfile/linked
21272 program. */
21273 if (cu->dwo_unit != nullptr
21274 && tag != DW_TAG_compile_unit
21275 && tag != DW_TAG_skeleton_unit)
21276 {
21277 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21278
21279 if (sections->rnglists.size > 0)
21280 return &sections->rnglists;
21281 else
21282 error (_(".debug_rnglists section is missing from .dwo file."));
21283 }
21284 return &dwarf2_per_objfile->per_bfd->rnglists;
21285 }
21286
21287 /* A helper function that fills in a dwarf2_loclist_baton. */
21288
21289 static void
21290 fill_in_loclist_baton (struct dwarf2_cu *cu,
21291 struct dwarf2_loclist_baton *baton,
21292 const struct attribute *attr)
21293 {
21294 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21295 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21296
21297 section->read (per_objfile->objfile);
21298
21299 baton->per_objfile = per_objfile;
21300 baton->per_cu = cu->per_cu;
21301 gdb_assert (baton->per_cu);
21302 /* We don't know how long the location list is, but make sure we
21303 don't run off the edge of the section. */
21304 baton->size = section->size - attr->as_unsigned ();
21305 baton->data = section->buffer + attr->as_unsigned ();
21306 if (cu->base_address.has_value ())
21307 baton->base_address = *cu->base_address;
21308 else
21309 baton->base_address = 0;
21310 baton->from_dwo = cu->dwo_unit != NULL;
21311 }
21312
21313 static void
21314 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
21315 struct dwarf2_cu *cu, int is_block)
21316 {
21317 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21318 struct objfile *objfile = per_objfile->objfile;
21319 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21320
21321 if (attr->form_is_section_offset ()
21322 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21323 the section. If so, fall through to the complaint in the
21324 other branch. */
21325 && attr->as_unsigned () < section->get_size (objfile))
21326 {
21327 struct dwarf2_loclist_baton *baton;
21328
21329 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
21330
21331 fill_in_loclist_baton (cu, baton, attr);
21332
21333 if (!cu->base_address.has_value ())
21334 complaint (_("Location list used without "
21335 "specifying the CU base address."));
21336
21337 sym->set_aclass_index ((is_block
21338 ? dwarf2_loclist_block_index
21339 : dwarf2_loclist_index));
21340 SYMBOL_LOCATION_BATON (sym) = baton;
21341 }
21342 else
21343 {
21344 struct dwarf2_locexpr_baton *baton;
21345
21346 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
21347 baton->per_objfile = per_objfile;
21348 baton->per_cu = cu->per_cu;
21349 gdb_assert (baton->per_cu);
21350
21351 if (attr->form_is_block ())
21352 {
21353 /* Note that we're just copying the block's data pointer
21354 here, not the actual data. We're still pointing into the
21355 info_buffer for SYM's objfile; right now we never release
21356 that buffer, but when we do clean up properly this may
21357 need to change. */
21358 struct dwarf_block *block = attr->as_block ();
21359 baton->size = block->size;
21360 baton->data = block->data;
21361 }
21362 else
21363 {
21364 dwarf2_invalid_attrib_class_complaint ("location description",
21365 sym->natural_name ());
21366 baton->size = 0;
21367 }
21368
21369 sym->set_aclass_index ((is_block
21370 ? dwarf2_locexpr_block_index
21371 : dwarf2_locexpr_index));
21372 SYMBOL_LOCATION_BATON (sym) = baton;
21373 }
21374 }
21375
21376 /* See read.h. */
21377
21378 const comp_unit_head *
21379 dwarf2_per_cu_data::get_header () const
21380 {
21381 if (!m_header_read_in)
21382 {
21383 const gdb_byte *info_ptr
21384 = this->section->buffer + to_underlying (this->sect_off);
21385
21386 read_comp_unit_head (&m_header, info_ptr, this->section,
21387 rcuh_kind::COMPILE);
21388
21389 m_header_read_in = true;
21390 }
21391
21392 return &m_header;
21393 }
21394
21395 /* See read.h. */
21396
21397 int
21398 dwarf2_per_cu_data::addr_size () const
21399 {
21400 return this->get_header ()->addr_size;
21401 }
21402
21403 /* See read.h. */
21404
21405 int
21406 dwarf2_per_cu_data::offset_size () const
21407 {
21408 return this->get_header ()->offset_size;
21409 }
21410
21411 /* See read.h. */
21412
21413 int
21414 dwarf2_per_cu_data::ref_addr_size () const
21415 {
21416 const comp_unit_head *header = this->get_header ();
21417
21418 if (header->version == 2)
21419 return header->addr_size;
21420 else
21421 return header->offset_size;
21422 }
21423
21424 /* A helper function for dwarf2_find_containing_comp_unit that returns
21425 the index of the result, and that searches a vector. It will
21426 return a result even if the offset in question does not actually
21427 occur in any CU. This is separate so that it can be unit
21428 tested. */
21429
21430 static int
21431 dwarf2_find_containing_comp_unit
21432 (sect_offset sect_off,
21433 unsigned int offset_in_dwz,
21434 const std::vector<dwarf2_per_cu_data_up> &all_units)
21435 {
21436 int low, high;
21437
21438 low = 0;
21439 high = all_units.size () - 1;
21440 while (high > low)
21441 {
21442 struct dwarf2_per_cu_data *mid_cu;
21443 int mid = low + (high - low) / 2;
21444
21445 mid_cu = all_units[mid].get ();
21446 if (mid_cu->is_dwz > offset_in_dwz
21447 || (mid_cu->is_dwz == offset_in_dwz
21448 && mid_cu->sect_off + mid_cu->length () > sect_off))
21449 high = mid;
21450 else
21451 low = mid + 1;
21452 }
21453 gdb_assert (low == high);
21454 return low;
21455 }
21456
21457 /* Locate the .debug_info compilation unit from CU's objfile which contains
21458 the DIE at OFFSET. Raises an error on failure. */
21459
21460 static struct dwarf2_per_cu_data *
21461 dwarf2_find_containing_comp_unit (sect_offset sect_off,
21462 unsigned int offset_in_dwz,
21463 dwarf2_per_bfd *per_bfd)
21464 {
21465 int low = dwarf2_find_containing_comp_unit
21466 (sect_off, offset_in_dwz, per_bfd->all_units);
21467 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
21468
21469 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
21470 {
21471 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
21472 error (_("Dwarf Error: could not find partial DIE containing "
21473 "offset %s [in module %s]"),
21474 sect_offset_str (sect_off),
21475 bfd_get_filename (per_bfd->obfd));
21476
21477 gdb_assert (per_bfd->all_units[low-1]->sect_off
21478 <= sect_off);
21479 return per_bfd->all_units[low - 1].get ();
21480 }
21481 else
21482 {
21483 if (low == per_bfd->all_units.size () - 1
21484 && sect_off >= this_cu->sect_off + this_cu->length ())
21485 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
21486 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
21487 return this_cu;
21488 }
21489 }
21490
21491 #if GDB_SELF_TEST
21492
21493 namespace selftests {
21494 namespace find_containing_comp_unit {
21495
21496 static void
21497 run_test ()
21498 {
21499 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
21500 dwarf2_per_cu_data *one_ptr = one.get ();
21501 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
21502 dwarf2_per_cu_data *two_ptr = two.get ();
21503 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
21504 dwarf2_per_cu_data *three_ptr = three.get ();
21505 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
21506 dwarf2_per_cu_data *four_ptr = four.get ();
21507
21508 one->set_length (5);
21509 two->sect_off = sect_offset (one->length ());
21510 two->set_length (7);
21511
21512 three->set_length (5);
21513 three->is_dwz = 1;
21514 four->sect_off = sect_offset (three->length ());
21515 four->set_length (7);
21516 four->is_dwz = 1;
21517
21518 std::vector<dwarf2_per_cu_data_up> units;
21519 units.push_back (std::move (one));
21520 units.push_back (std::move (two));
21521 units.push_back (std::move (three));
21522 units.push_back (std::move (four));
21523
21524 int result;
21525
21526 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
21527 SELF_CHECK (units[result].get () == one_ptr);
21528 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
21529 SELF_CHECK (units[result].get () == one_ptr);
21530 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
21531 SELF_CHECK (units[result].get () == two_ptr);
21532
21533 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
21534 SELF_CHECK (units[result].get () == three_ptr);
21535 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
21536 SELF_CHECK (units[result].get () == three_ptr);
21537 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
21538 SELF_CHECK (units[result].get () == four_ptr);
21539 }
21540
21541 }
21542 }
21543
21544 #endif /* GDB_SELF_TEST */
21545
21546 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
21547
21548 static void
21549 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
21550 enum language pretend_language)
21551 {
21552 struct attribute *attr;
21553
21554 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
21555
21556 /* Set the language we're debugging. */
21557 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
21558 enum language lang;
21559 if (cu->producer != nullptr
21560 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
21561 {
21562 /* The XLCL doesn't generate DW_LANG_OpenCL because this
21563 attribute is not standardised yet. As a workaround for the
21564 language detection we fall back to the DW_AT_producer
21565 string. */
21566 lang = language_opencl;
21567 }
21568 else if (cu->producer != nullptr
21569 && strstr (cu->producer, "GNU Go ") != NULL)
21570 {
21571 /* Similar hack for Go. */
21572 lang = language_go;
21573 }
21574 else if (attr != nullptr)
21575 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
21576 else
21577 lang = pretend_language;
21578
21579 cu->language_defn = language_def (lang);
21580
21581 switch (comp_unit_die->tag)
21582 {
21583 case DW_TAG_compile_unit:
21584 cu->per_cu->set_unit_type (DW_UT_compile);
21585 break;
21586 case DW_TAG_partial_unit:
21587 cu->per_cu->set_unit_type (DW_UT_partial);
21588 break;
21589 case DW_TAG_type_unit:
21590 cu->per_cu->set_unit_type (DW_UT_type);
21591 break;
21592 default:
21593 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
21594 dwarf_tag_name (comp_unit_die->tag),
21595 sect_offset_str (cu->per_cu->sect_off));
21596 }
21597
21598 cu->per_cu->set_lang (lang);
21599 }
21600
21601 /* See read.h. */
21602
21603 dwarf2_cu *
21604 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
21605 {
21606 auto it = m_dwarf2_cus.find (per_cu);
21607 if (it == m_dwarf2_cus.end ())
21608 return nullptr;
21609
21610 return it->second.get ();
21611 }
21612
21613 /* See read.h. */
21614
21615 void
21616 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
21617 std::unique_ptr<dwarf2_cu> cu)
21618 {
21619 gdb_assert (this->get_cu (per_cu) == nullptr);
21620
21621 m_dwarf2_cus[per_cu] = std::move (cu);
21622 }
21623
21624 /* See read.h. */
21625
21626 void
21627 dwarf2_per_objfile::age_comp_units ()
21628 {
21629 dwarf_read_debug_printf_v ("running");
21630
21631 /* This is not expected to be called in the middle of CU expansion. There is
21632 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
21633 loaded in memory. Calling age_comp_units while the queue is in use could
21634 make us free the DIEs for a CU that is in the queue and therefore break
21635 that invariant. */
21636 gdb_assert (!queue.has_value ());
21637
21638 /* Start by clearing all marks. */
21639 for (const auto &pair : m_dwarf2_cus)
21640 pair.second->clear_mark ();
21641
21642 /* Traverse all CUs, mark them and their dependencies if used recently
21643 enough. */
21644 for (const auto &pair : m_dwarf2_cus)
21645 {
21646 dwarf2_cu *cu = pair.second.get ();
21647
21648 cu->last_used++;
21649 if (cu->last_used <= dwarf_max_cache_age)
21650 cu->mark ();
21651 }
21652
21653 /* Delete all CUs still not marked. */
21654 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
21655 {
21656 dwarf2_cu *cu = it->second.get ();
21657
21658 if (!cu->is_marked ())
21659 {
21660 dwarf_read_debug_printf_v ("deleting old CU %s",
21661 sect_offset_str (cu->per_cu->sect_off));
21662 it = m_dwarf2_cus.erase (it);
21663 }
21664 else
21665 it++;
21666 }
21667 }
21668
21669 /* See read.h. */
21670
21671 void
21672 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
21673 {
21674 auto it = m_dwarf2_cus.find (per_cu);
21675 if (it == m_dwarf2_cus.end ())
21676 return;
21677
21678 m_dwarf2_cus.erase (it);
21679 }
21680
21681 dwarf2_per_objfile::~dwarf2_per_objfile ()
21682 {
21683 remove_all_cus ();
21684 }
21685
21686 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21687 We store these in a hash table separate from the DIEs, and preserve them
21688 when the DIEs are flushed out of cache.
21689
21690 The CU "per_cu" pointer is needed because offset alone is not enough to
21691 uniquely identify the type. A file may have multiple .debug_types sections,
21692 or the type may come from a DWO file. Furthermore, while it's more logical
21693 to use per_cu->section+offset, with Fission the section with the data is in
21694 the DWO file but we don't know that section at the point we need it.
21695 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21696 because we can enter the lookup routine, get_die_type_at_offset, from
21697 outside this file, and thus won't necessarily have PER_CU->cu.
21698 Fortunately, PER_CU is stable for the life of the objfile. */
21699
21700 struct dwarf2_per_cu_offset_and_type
21701 {
21702 const struct dwarf2_per_cu_data *per_cu;
21703 sect_offset sect_off;
21704 struct type *type;
21705 };
21706
21707 /* Hash function for a dwarf2_per_cu_offset_and_type. */
21708
21709 static hashval_t
21710 per_cu_offset_and_type_hash (const void *item)
21711 {
21712 const struct dwarf2_per_cu_offset_and_type *ofs
21713 = (const struct dwarf2_per_cu_offset_and_type *) item;
21714
21715 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
21716 }
21717
21718 /* Equality function for a dwarf2_per_cu_offset_and_type. */
21719
21720 static int
21721 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
21722 {
21723 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
21724 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
21725 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
21726 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
21727
21728 return (ofs_lhs->per_cu == ofs_rhs->per_cu
21729 && ofs_lhs->sect_off == ofs_rhs->sect_off);
21730 }
21731
21732 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21733 table if necessary. For convenience, return TYPE.
21734
21735 The DIEs reading must have careful ordering to:
21736 * Not cause infinite loops trying to read in DIEs as a prerequisite for
21737 reading current DIE.
21738 * Not trying to dereference contents of still incompletely read in types
21739 while reading in other DIEs.
21740 * Enable referencing still incompletely read in types just by a pointer to
21741 the type without accessing its fields.
21742
21743 Therefore caller should follow these rules:
21744 * Try to fetch any prerequisite types we may need to build this DIE type
21745 before building the type and calling set_die_type.
21746 * After building type call set_die_type for current DIE as soon as
21747 possible before fetching more types to complete the current type.
21748 * Make the type as complete as possible before fetching more types. */
21749
21750 static struct type *
21751 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21752 bool skip_data_location)
21753 {
21754 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21755 struct dwarf2_per_cu_offset_and_type **slot, ofs;
21756 struct objfile *objfile = per_objfile->objfile;
21757 struct attribute *attr;
21758 struct dynamic_prop prop;
21759
21760 /* For Ada types, make sure that the gnat-specific data is always
21761 initialized (if not already set). There are a few types where
21762 we should not be doing so, because the type-specific area is
21763 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21764 where the type-specific area is used to store the floatformat).
21765 But this is not a problem, because the gnat-specific information
21766 is actually not needed for these types. */
21767 if (need_gnat_info (cu)
21768 && type->code () != TYPE_CODE_FUNC
21769 && type->code () != TYPE_CODE_FLT
21770 && type->code () != TYPE_CODE_METHODPTR
21771 && type->code () != TYPE_CODE_MEMBERPTR
21772 && type->code () != TYPE_CODE_METHOD
21773 && type->code () != TYPE_CODE_FIXED_POINT
21774 && !HAVE_GNAT_AUX_INFO (type))
21775 INIT_GNAT_SPECIFIC (type);
21776
21777 /* Read DW_AT_allocated and set in type. */
21778 attr = dwarf2_attr (die, DW_AT_allocated, cu);
21779 if (attr != NULL)
21780 {
21781 struct type *prop_type = cu->addr_sized_int_type (false);
21782 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21783 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
21784 }
21785
21786 /* Read DW_AT_associated and set in type. */
21787 attr = dwarf2_attr (die, DW_AT_associated, cu);
21788 if (attr != NULL)
21789 {
21790 struct type *prop_type = cu->addr_sized_int_type (false);
21791 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21792 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
21793 }
21794
21795 /* Read DW_AT_rank and set in type. */
21796 attr = dwarf2_attr (die, DW_AT_rank, cu);
21797 if (attr != NULL)
21798 {
21799 struct type *prop_type = cu->addr_sized_int_type (false);
21800 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21801 type->add_dyn_prop (DYN_PROP_RANK, prop);
21802 }
21803
21804 /* Read DW_AT_data_location and set in type. */
21805 if (!skip_data_location)
21806 {
21807 attr = dwarf2_attr (die, DW_AT_data_location, cu);
21808 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
21809 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
21810 }
21811
21812 if (per_objfile->die_type_hash == NULL)
21813 per_objfile->die_type_hash
21814 = htab_up (htab_create_alloc (127,
21815 per_cu_offset_and_type_hash,
21816 per_cu_offset_and_type_eq,
21817 NULL, xcalloc, xfree));
21818
21819 ofs.per_cu = cu->per_cu;
21820 ofs.sect_off = die->sect_off;
21821 ofs.type = type;
21822 slot = (struct dwarf2_per_cu_offset_and_type **)
21823 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
21824 if (*slot)
21825 complaint (_("A problem internal to GDB: DIE %s has type already set"),
21826 sect_offset_str (die->sect_off));
21827 *slot = XOBNEW (&objfile->objfile_obstack,
21828 struct dwarf2_per_cu_offset_and_type);
21829 **slot = ofs;
21830 return type;
21831 }
21832
21833 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
21834 or return NULL if the die does not have a saved type. */
21835
21836 static struct type *
21837 get_die_type_at_offset (sect_offset sect_off,
21838 dwarf2_per_cu_data *per_cu,
21839 dwarf2_per_objfile *per_objfile)
21840 {
21841 struct dwarf2_per_cu_offset_and_type *slot, ofs;
21842
21843 if (per_objfile->die_type_hash == NULL)
21844 return NULL;
21845
21846 ofs.per_cu = per_cu;
21847 ofs.sect_off = sect_off;
21848 slot = ((struct dwarf2_per_cu_offset_and_type *)
21849 htab_find (per_objfile->die_type_hash.get (), &ofs));
21850 if (slot)
21851 return slot->type;
21852 else
21853 return NULL;
21854 }
21855
21856 /* Look up the type for DIE in CU in die_type_hash,
21857 or return NULL if DIE does not have a saved type. */
21858
21859 static struct type *
21860 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
21861 {
21862 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
21863 }
21864
21865 struct cmd_list_element *set_dwarf_cmdlist;
21866 struct cmd_list_element *show_dwarf_cmdlist;
21867
21868 static void
21869 show_check_physname (struct ui_file *file, int from_tty,
21870 struct cmd_list_element *c, const char *value)
21871 {
21872 gdb_printf (file,
21873 _("Whether to check \"physname\" is %s.\n"),
21874 value);
21875 }
21876
21877 void _initialize_dwarf2_read ();
21878 void
21879 _initialize_dwarf2_read ()
21880 {
21881 add_setshow_prefix_cmd ("dwarf", class_maintenance,
21882 _("\
21883 Set DWARF specific variables.\n\
21884 Configure DWARF variables such as the cache size."),
21885 _("\
21886 Show DWARF specific variables.\n\
21887 Show DWARF variables such as the cache size."),
21888 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
21889 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
21890
21891 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21892 &dwarf_max_cache_age, _("\
21893 Set the upper bound on the age of cached DWARF compilation units."), _("\
21894 Show the upper bound on the age of cached DWARF compilation units."), _("\
21895 A higher limit means that cached compilation units will be stored\n\
21896 in memory longer, and more total memory will be used. Zero disables\n\
21897 caching, which can slow down startup."),
21898 NULL,
21899 show_dwarf_max_cache_age,
21900 &set_dwarf_cmdlist,
21901 &show_dwarf_cmdlist);
21902
21903 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
21904 Set debugging of the DWARF reader."), _("\
21905 Show debugging of the DWARF reader."), _("\
21906 When enabled (non-zero), debugging messages are printed during DWARF\n\
21907 reading and symtab expansion. A value of 1 (one) provides basic\n\
21908 information. A value greater than 1 provides more verbose information."),
21909 NULL,
21910 NULL,
21911 &setdebuglist, &showdebuglist);
21912
21913 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
21914 Set debugging of the DWARF DIE reader."), _("\
21915 Show debugging of the DWARF DIE reader."), _("\
21916 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21917 The value is the maximum depth to print."),
21918 NULL,
21919 NULL,
21920 &setdebuglist, &showdebuglist);
21921
21922 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
21923 Set debugging of the dwarf line reader."), _("\
21924 Show debugging of the dwarf line reader."), _("\
21925 When enabled (non-zero), line number entries are dumped as they are read in.\n\
21926 A value of 1 (one) provides basic information.\n\
21927 A value greater than 1 provides more verbose information."),
21928 NULL,
21929 NULL,
21930 &setdebuglist, &showdebuglist);
21931
21932 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21933 Set cross-checking of \"physname\" code against demangler."), _("\
21934 Show cross-checking of \"physname\" code against demangler."), _("\
21935 When enabled, GDB's internal \"physname\" code is checked against\n\
21936 the demangler."),
21937 NULL, show_check_physname,
21938 &setdebuglist, &showdebuglist);
21939
21940 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21941 &dwarf2_locexpr_funcs);
21942 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21943 &dwarf2_loclist_funcs);
21944
21945 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21946 &dwarf2_block_frame_base_locexpr_funcs);
21947 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21948 &dwarf2_block_frame_base_loclist_funcs);
21949
21950 #if GDB_SELF_TEST
21951 selftests::register_test ("dw2_expand_symtabs_matching",
21952 selftests::dw2_expand_symtabs_matching::run_test);
21953 selftests::register_test ("dwarf2_find_containing_comp_unit",
21954 selftests::find_containing_comp_unit::run_test);
21955 #endif
21956 }