]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Use type allocator for array types
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2023 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/read-debug-names.h"
45 #include "dwarf2/read-gdb-index.h"
46 #include "dwarf2/sect-names.h"
47 #include "dwarf2/stringify.h"
48 #include "dwarf2/public.h"
49 #include "bfd.h"
50 #include "elf-bfd.h"
51 #include "symtab.h"
52 #include "gdbtypes.h"
53 #include "objfiles.h"
54 #include "dwarf2.h"
55 #include "demangle.h"
56 #include "gdb-demangle.h"
57 #include "filenames.h" /* for DOSish file names */
58 #include "language.h"
59 #include "complaints.h"
60 #include "dwarf2/expr.h"
61 #include "dwarf2/loc.h"
62 #include "cp-support.h"
63 #include "hashtab.h"
64 #include "command.h"
65 #include "gdbcmd.h"
66 #include "block.h"
67 #include "addrmap.h"
68 #include "typeprint.h"
69 #include "c-lang.h"
70 #include "go-lang.h"
71 #include "valprint.h"
72 #include "gdbcore.h" /* for gnutarget */
73 #include "gdb/gdb-index.h"
74 #include "gdb_bfd.h"
75 #include "f-lang.h"
76 #include "source.h"
77 #include "build-id.h"
78 #include "namespace.h"
79 #include "gdbsupport/function-view.h"
80 #include "gdbsupport/gdb_optional.h"
81 #include "gdbsupport/underlying.h"
82 #include "gdbsupport/hash_enum.h"
83 #include "filename-seen-cache.h"
84 #include "producer.h"
85 #include <fcntl.h>
86 #include <algorithm>
87 #include <unordered_map>
88 #include "gdbsupport/selftest.h"
89 #include "rust-lang.h"
90 #include "gdbsupport/pathstuff.h"
91 #include "count-one-bits.h"
92 #include <unordered_set>
93 #include "dwarf2/abbrev-cache.h"
94 #include "cooked-index.h"
95 #include "split-name.h"
96 #include "gdbsupport/parallel-for.h"
97 #include "gdbsupport/thread-pool.h"
98
99 /* When == 1, print basic high level tracing messages.
100 When > 1, be more verbose.
101 This is in contrast to the low level DIE reading of dwarf_die_debug. */
102 static unsigned int dwarf_read_debug = 0;
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
105
106 #define dwarf_read_debug_printf(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
111
112 #define dwarf_read_debug_printf_v(fmt, ...) \
113 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
114 ##__VA_ARGS__)
115
116 /* When non-zero, dump DIEs after they are read in. */
117 static unsigned int dwarf_die_debug = 0;
118
119 /* When non-zero, dump line number entries as they are read in. */
120 unsigned int dwarf_line_debug = 0;
121
122 /* When true, cross-check physname against demangler. */
123 static bool check_physname = false;
124
125 /* This is used to store the data that is always per objfile. */
126 static const registry<objfile>::key<dwarf2_per_objfile>
127 dwarf2_objfile_data_key;
128
129 /* These are used to store the dwarf2_per_bfd objects.
130
131 objfiles having the same BFD, which doesn't require relocations, are going to
132 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
133
134 Other objfiles are not going to share a dwarf2_per_bfd with any other
135 objfiles, so they'll have their own version kept in the _objfile_data_key
136 version. */
137 static const registry<bfd>::key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
138 static const registry<objfile>::key<dwarf2_per_bfd>
139 dwarf2_per_bfd_objfile_data_key;
140
141 /* The "aclass" indices for various kinds of computed DWARF symbols. */
142
143 static int dwarf2_locexpr_index;
144 static int dwarf2_loclist_index;
145 static int dwarf2_locexpr_block_index;
146 static int dwarf2_loclist_block_index;
147
148 /* Size of .debug_loclists section header for 32-bit DWARF format. */
149 #define LOCLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_loclists section header for 64-bit DWARF format. */
152 #define LOCLIST_HEADER_SIZE64 20
153
154 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
155 #define RNGLIST_HEADER_SIZE32 12
156
157 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
158 #define RNGLIST_HEADER_SIZE64 20
159
160 /* See dwarf2/read.h. */
161
162 dwarf2_per_objfile *
163 get_dwarf2_per_objfile (struct objfile *objfile)
164 {
165 return dwarf2_objfile_data_key.get (objfile);
166 }
167
168 /* Default names of the debugging sections. */
169
170 /* Note that if the debugging section has been compressed, it might
171 have a name like .zdebug_info. */
172
173 const struct dwarf2_debug_sections dwarf2_elf_names =
174 {
175 { ".debug_info", ".zdebug_info" },
176 { ".debug_abbrev", ".zdebug_abbrev" },
177 { ".debug_line", ".zdebug_line" },
178 { ".debug_loc", ".zdebug_loc" },
179 { ".debug_loclists", ".zdebug_loclists" },
180 { ".debug_macinfo", ".zdebug_macinfo" },
181 { ".debug_macro", ".zdebug_macro" },
182 { ".debug_str", ".zdebug_str" },
183 { ".debug_str_offsets", ".zdebug_str_offsets" },
184 { ".debug_line_str", ".zdebug_line_str" },
185 { ".debug_ranges", ".zdebug_ranges" },
186 { ".debug_rnglists", ".zdebug_rnglists" },
187 { ".debug_types", ".zdebug_types" },
188 { ".debug_addr", ".zdebug_addr" },
189 { ".debug_frame", ".zdebug_frame" },
190 { ".eh_frame", NULL },
191 { ".gdb_index", ".zgdb_index" },
192 { ".debug_names", ".zdebug_names" },
193 { ".debug_aranges", ".zdebug_aranges" },
194 23
195 };
196
197 /* List of DWO/DWP sections. */
198
199 static const struct dwop_section_names
200 {
201 struct dwarf2_section_names abbrev_dwo;
202 struct dwarf2_section_names info_dwo;
203 struct dwarf2_section_names line_dwo;
204 struct dwarf2_section_names loc_dwo;
205 struct dwarf2_section_names loclists_dwo;
206 struct dwarf2_section_names macinfo_dwo;
207 struct dwarf2_section_names macro_dwo;
208 struct dwarf2_section_names rnglists_dwo;
209 struct dwarf2_section_names str_dwo;
210 struct dwarf2_section_names str_offsets_dwo;
211 struct dwarf2_section_names types_dwo;
212 struct dwarf2_section_names cu_index;
213 struct dwarf2_section_names tu_index;
214 }
215 dwop_section_names =
216 {
217 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
218 { ".debug_info.dwo", ".zdebug_info.dwo" },
219 { ".debug_line.dwo", ".zdebug_line.dwo" },
220 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
221 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
222 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
223 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
224 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
225 { ".debug_str.dwo", ".zdebug_str.dwo" },
226 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
227 { ".debug_types.dwo", ".zdebug_types.dwo" },
228 { ".debug_cu_index", ".zdebug_cu_index" },
229 { ".debug_tu_index", ".zdebug_tu_index" },
230 };
231
232 /* local data types */
233
234 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
235 begin with a header, which contains the following information. */
236 struct loclists_rnglists_header
237 {
238 /* A 4-byte or 12-byte length containing the length of the
239 set of entries for this compilation unit, not including the
240 length field itself. */
241 unsigned int length;
242
243 /* A 2-byte version identifier. */
244 short version;
245
246 /* A 1-byte unsigned integer containing the size in bytes of an address on
247 the target system. */
248 unsigned char addr_size;
249
250 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
251 on the target system. */
252 unsigned char segment_collector_size;
253
254 /* A 4-byte count of the number of offsets that follow the header. */
255 unsigned int offset_entry_count;
256 };
257
258 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
259 This includes type_unit_group and quick_file_names. */
260
261 struct stmt_list_hash
262 {
263 /* The DWO unit this table is from or NULL if there is none. */
264 struct dwo_unit *dwo_unit;
265
266 /* Offset in .debug_line or .debug_line.dwo. */
267 sect_offset line_sect_off;
268 };
269
270 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
271 an object of this type. This contains elements of type unit groups
272 that can be shared across objfiles. The non-shareable parts are in
273 type_unit_group_unshareable. */
274
275 struct type_unit_group
276 {
277 /* The data used to construct the hash key. */
278 struct stmt_list_hash hash {};
279 };
280
281 /* These sections are what may appear in a (real or virtual) DWO file. */
282
283 struct dwo_sections
284 {
285 struct dwarf2_section_info abbrev;
286 struct dwarf2_section_info line;
287 struct dwarf2_section_info loc;
288 struct dwarf2_section_info loclists;
289 struct dwarf2_section_info macinfo;
290 struct dwarf2_section_info macro;
291 struct dwarf2_section_info rnglists;
292 struct dwarf2_section_info str;
293 struct dwarf2_section_info str_offsets;
294 /* In the case of a virtual DWO file, these two are unused. */
295 struct dwarf2_section_info info;
296 std::vector<dwarf2_section_info> types;
297 };
298
299 /* CUs/TUs in DWP/DWO files. */
300
301 struct dwo_unit
302 {
303 /* Backlink to the containing struct dwo_file. */
304 struct dwo_file *dwo_file;
305
306 /* The "id" that distinguishes this CU/TU.
307 .debug_info calls this "dwo_id", .debug_types calls this "signature".
308 Since signatures came first, we stick with it for consistency. */
309 ULONGEST signature;
310
311 /* The section this CU/TU lives in, in the DWO file. */
312 struct dwarf2_section_info *section;
313
314 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
315 sect_offset sect_off;
316 unsigned int length;
317
318 /* For types, offset in the type's DIE of the type defined by this TU. */
319 cu_offset type_offset_in_tu;
320 };
321
322 /* include/dwarf2.h defines the DWP section codes.
323 It defines a max value but it doesn't define a min value, which we
324 use for error checking, so provide one. */
325
326 enum dwp_v2_section_ids
327 {
328 DW_SECT_MIN = 1
329 };
330
331 /* Data for one DWO file.
332
333 This includes virtual DWO files (a virtual DWO file is a DWO file as it
334 appears in a DWP file). DWP files don't really have DWO files per se -
335 comdat folding of types "loses" the DWO file they came from, and from
336 a high level view DWP files appear to contain a mass of random types.
337 However, to maintain consistency with the non-DWP case we pretend DWP
338 files contain virtual DWO files, and we assign each TU with one virtual
339 DWO file (generally based on the line and abbrev section offsets -
340 a heuristic that seems to work in practice). */
341
342 struct dwo_file
343 {
344 dwo_file () = default;
345 DISABLE_COPY_AND_ASSIGN (dwo_file);
346
347 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
348 For virtual DWO files the name is constructed from the section offsets
349 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
350 from related CU+TUs. */
351 const char *dwo_name = nullptr;
352
353 /* The DW_AT_comp_dir attribute. */
354 const char *comp_dir = nullptr;
355
356 /* The bfd, when the file is open. Otherwise this is NULL.
357 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
358 gdb_bfd_ref_ptr dbfd;
359
360 /* The sections that make up this DWO file.
361 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
362 sections (for lack of a better name). */
363 struct dwo_sections sections {};
364
365 /* The CUs in the file.
366 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
367 an extension to handle LLVM's Link Time Optimization output (where
368 multiple source files may be compiled into a single object/dwo pair). */
369 htab_up cus;
370
371 /* Table of TUs in the file.
372 Each element is a struct dwo_unit. */
373 htab_up tus;
374 };
375
376 /* These sections are what may appear in a DWP file. */
377
378 struct dwp_sections
379 {
380 /* These are used by all DWP versions (1, 2 and 5). */
381 struct dwarf2_section_info str;
382 struct dwarf2_section_info cu_index;
383 struct dwarf2_section_info tu_index;
384
385 /* These are only used by DWP version 2 and version 5 files.
386 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
387 sections are referenced by section number, and are not recorded here.
388 In DWP version 2 or 5 there is at most one copy of all these sections,
389 each section being (effectively) comprised of the concatenation of all of
390 the individual sections that exist in the version 1 format.
391 To keep the code simple we treat each of these concatenated pieces as a
392 section itself (a virtual section?). */
393 struct dwarf2_section_info abbrev;
394 struct dwarf2_section_info info;
395 struct dwarf2_section_info line;
396 struct dwarf2_section_info loc;
397 struct dwarf2_section_info loclists;
398 struct dwarf2_section_info macinfo;
399 struct dwarf2_section_info macro;
400 struct dwarf2_section_info rnglists;
401 struct dwarf2_section_info str_offsets;
402 struct dwarf2_section_info types;
403 };
404
405 /* These sections are what may appear in a virtual DWO file in DWP version 1.
406 A virtual DWO file is a DWO file as it appears in a DWP file. */
407
408 struct virtual_v1_dwo_sections
409 {
410 struct dwarf2_section_info abbrev;
411 struct dwarf2_section_info line;
412 struct dwarf2_section_info loc;
413 struct dwarf2_section_info macinfo;
414 struct dwarf2_section_info macro;
415 struct dwarf2_section_info str_offsets;
416 /* Each DWP hash table entry records one CU or one TU.
417 That is recorded here, and copied to dwo_unit.section. */
418 struct dwarf2_section_info info_or_types;
419 };
420
421 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
422 In version 2, the sections of the DWO files are concatenated together
423 and stored in one section of that name. Thus each ELF section contains
424 several "virtual" sections. */
425
426 struct virtual_v2_or_v5_dwo_sections
427 {
428 bfd_size_type abbrev_offset;
429 bfd_size_type abbrev_size;
430
431 bfd_size_type line_offset;
432 bfd_size_type line_size;
433
434 bfd_size_type loc_offset;
435 bfd_size_type loc_size;
436
437 bfd_size_type loclists_offset;
438 bfd_size_type loclists_size;
439
440 bfd_size_type macinfo_offset;
441 bfd_size_type macinfo_size;
442
443 bfd_size_type macro_offset;
444 bfd_size_type macro_size;
445
446 bfd_size_type rnglists_offset;
447 bfd_size_type rnglists_size;
448
449 bfd_size_type str_offsets_offset;
450 bfd_size_type str_offsets_size;
451
452 /* Each DWP hash table entry records one CU or one TU.
453 That is recorded here, and copied to dwo_unit.section. */
454 bfd_size_type info_or_types_offset;
455 bfd_size_type info_or_types_size;
456 };
457
458 /* Contents of DWP hash tables. */
459
460 struct dwp_hash_table
461 {
462 uint32_t version, nr_columns;
463 uint32_t nr_units, nr_slots;
464 const gdb_byte *hash_table, *unit_table;
465 union
466 {
467 struct
468 {
469 const gdb_byte *indices;
470 } v1;
471 struct
472 {
473 /* This is indexed by column number and gives the id of the section
474 in that column. */
475 #define MAX_NR_V2_DWO_SECTIONS \
476 (1 /* .debug_info or .debug_types */ \
477 + 1 /* .debug_abbrev */ \
478 + 1 /* .debug_line */ \
479 + 1 /* .debug_loc */ \
480 + 1 /* .debug_str_offsets */ \
481 + 1 /* .debug_macro or .debug_macinfo */)
482 int section_ids[MAX_NR_V2_DWO_SECTIONS];
483 const gdb_byte *offsets;
484 const gdb_byte *sizes;
485 } v2;
486 struct
487 {
488 /* This is indexed by column number and gives the id of the section
489 in that column. */
490 #define MAX_NR_V5_DWO_SECTIONS \
491 (1 /* .debug_info */ \
492 + 1 /* .debug_abbrev */ \
493 + 1 /* .debug_line */ \
494 + 1 /* .debug_loclists */ \
495 + 1 /* .debug_str_offsets */ \
496 + 1 /* .debug_macro */ \
497 + 1 /* .debug_rnglists */)
498 int section_ids[MAX_NR_V5_DWO_SECTIONS];
499 const gdb_byte *offsets;
500 const gdb_byte *sizes;
501 } v5;
502 } section_pool;
503 };
504
505 /* Data for one DWP file. */
506
507 struct dwp_file
508 {
509 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
510 : name (name_),
511 dbfd (std::move (abfd))
512 {
513 }
514
515 /* Name of the file. */
516 const char *name;
517
518 /* File format version. */
519 int version = 0;
520
521 /* The bfd. */
522 gdb_bfd_ref_ptr dbfd;
523
524 /* Section info for this file. */
525 struct dwp_sections sections {};
526
527 /* Table of CUs in the file. */
528 const struct dwp_hash_table *cus = nullptr;
529
530 /* Table of TUs in the file. */
531 const struct dwp_hash_table *tus = nullptr;
532
533 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
534 htab_up loaded_cus;
535 htab_up loaded_tus;
536
537 /* Table to map ELF section numbers to their sections.
538 This is only needed for the DWP V1 file format. */
539 unsigned int num_sections = 0;
540 asection **elf_sections = nullptr;
541 };
542
543 /* Struct used to pass misc. parameters to read_die_and_children, et
544 al. which are used for both .debug_info and .debug_types dies.
545 All parameters here are unchanging for the life of the call. This
546 struct exists to abstract away the constant parameters of die reading. */
547
548 struct die_reader_specs
549 {
550 /* The bfd of die_section. */
551 bfd *abfd;
552
553 /* The CU of the DIE we are parsing. */
554 struct dwarf2_cu *cu;
555
556 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
557 struct dwo_file *dwo_file;
558
559 /* The section the die comes from.
560 This is either .debug_info or .debug_types, or the .dwo variants. */
561 struct dwarf2_section_info *die_section;
562
563 /* die_section->buffer. */
564 const gdb_byte *buffer;
565
566 /* The end of the buffer. */
567 const gdb_byte *buffer_end;
568
569 /* The abbreviation table to use when reading the DIEs. */
570 struct abbrev_table *abbrev_table;
571 };
572
573 /* A subclass of die_reader_specs that holds storage and has complex
574 constructor and destructor behavior. */
575
576 class cutu_reader : public die_reader_specs
577 {
578 public:
579
580 cutu_reader (dwarf2_per_cu_data *this_cu,
581 dwarf2_per_objfile *per_objfile,
582 struct abbrev_table *abbrev_table,
583 dwarf2_cu *existing_cu,
584 bool skip_partial,
585 abbrev_cache *cache = nullptr);
586
587 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
588 dwarf2_per_objfile *per_objfile,
589 struct dwarf2_cu *parent_cu = nullptr,
590 struct dwo_file *dwo_file = nullptr);
591
592 DISABLE_COPY_AND_ASSIGN (cutu_reader);
593
594 cutu_reader (cutu_reader &&) = default;
595
596 const gdb_byte *info_ptr = nullptr;
597 struct die_info *comp_unit_die = nullptr;
598 bool dummy_p = false;
599
600 /* Release the new CU, putting it on the chain. This cannot be done
601 for dummy CUs. */
602 void keep ();
603
604 /* Release the abbrev table, transferring ownership to the
605 caller. */
606 abbrev_table_up release_abbrev_table ()
607 {
608 return std::move (m_abbrev_table_holder);
609 }
610
611 private:
612 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
613 dwarf2_per_objfile *per_objfile,
614 dwarf2_cu *existing_cu);
615
616 struct dwarf2_per_cu_data *m_this_cu;
617 std::unique_ptr<dwarf2_cu> m_new_cu;
618
619 /* The ordinary abbreviation table. */
620 abbrev_table_up m_abbrev_table_holder;
621
622 /* The DWO abbreviation table. */
623 abbrev_table_up m_dwo_abbrev_table;
624 };
625
626 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
627 but this would require a corresponding change in unpack_field_as_long
628 and friends. */
629 static int bits_per_byte = 8;
630
631 struct variant_part_builder;
632
633 /* When reading a variant, we track a bit more information about the
634 field, and store it in an object of this type. */
635
636 struct variant_field
637 {
638 int first_field = -1;
639 int last_field = -1;
640
641 /* A variant can contain other variant parts. */
642 std::vector<variant_part_builder> variant_parts;
643
644 /* If we see a DW_TAG_variant, then this will be set if this is the
645 default branch. */
646 bool default_branch = false;
647 /* If we see a DW_AT_discr_value, then this will be the discriminant
648 value. */
649 ULONGEST discriminant_value = 0;
650 /* If we see a DW_AT_discr_list, then this is a pointer to the list
651 data. */
652 struct dwarf_block *discr_list_data = nullptr;
653 };
654
655 /* This represents a DW_TAG_variant_part. */
656
657 struct variant_part_builder
658 {
659 /* The offset of the discriminant field. */
660 sect_offset discriminant_offset {};
661
662 /* Variants that are direct children of this variant part. */
663 std::vector<variant_field> variants;
664
665 /* True if we're currently reading a variant. */
666 bool processing_variant = false;
667 };
668
669 struct nextfield
670 {
671 int accessibility = 0;
672 int virtuality = 0;
673 /* Variant parts need to find the discriminant, which is a DIE
674 reference. We track the section offset of each field to make
675 this link. */
676 sect_offset offset;
677 struct field field {};
678 };
679
680 struct fnfieldlist
681 {
682 const char *name = nullptr;
683 std::vector<struct fn_field> fnfields;
684 };
685
686 /* The routines that read and process dies for a C struct or C++ class
687 pass lists of data member fields and lists of member function fields
688 in an instance of a field_info structure, as defined below. */
689 struct field_info
690 {
691 /* List of data member and baseclasses fields. */
692 std::vector<struct nextfield> fields;
693 std::vector<struct nextfield> baseclasses;
694
695 /* Set if the accessibility of one of the fields is not public. */
696 bool non_public_fields = false;
697
698 /* Member function fieldlist array, contains name of possibly overloaded
699 member function, number of overloaded member functions and a pointer
700 to the head of the member function field chain. */
701 std::vector<struct fnfieldlist> fnfieldlists;
702
703 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
704 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
705 std::vector<struct decl_field> typedef_field_list;
706
707 /* Nested types defined by this class and the number of elements in this
708 list. */
709 std::vector<struct decl_field> nested_types_list;
710
711 /* If non-null, this is the variant part we are currently
712 reading. */
713 variant_part_builder *current_variant_part = nullptr;
714 /* This holds all the top-level variant parts attached to the type
715 we're reading. */
716 std::vector<variant_part_builder> variant_parts;
717
718 /* Return the total number of fields (including baseclasses). */
719 int nfields () const
720 {
721 return fields.size () + baseclasses.size ();
722 }
723 };
724
725 /* Loaded secondary compilation units are kept in memory until they
726 have not been referenced for the processing of this many
727 compilation units. Set this to zero to disable caching. Cache
728 sizes of up to at least twenty will improve startup time for
729 typical inter-CU-reference binaries, at an obvious memory cost. */
730 static int dwarf_max_cache_age = 5;
731 static void
732 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
733 struct cmd_list_element *c, const char *value)
734 {
735 gdb_printf (file, _("The upper bound on the age of cached "
736 "DWARF compilation units is %s.\n"),
737 value);
738 }
739 \f
740 /* local function prototypes */
741
742 static void dwarf2_find_base_address (struct die_info *die,
743 struct dwarf2_cu *cu);
744
745 class cooked_index_storage;
746 static void build_type_psymtabs_reader (cutu_reader *reader,
747 cooked_index_storage *storage);
748
749 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
750
751 static void var_decode_location (struct attribute *attr,
752 struct symbol *sym,
753 struct dwarf2_cu *cu);
754
755 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
756
757 static const gdb_byte *read_attribute (const struct die_reader_specs *,
758 struct attribute *,
759 const struct attr_abbrev *,
760 const gdb_byte *,
761 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
5864 = type_allocator (objfile).new_type (TYPE_CODE_MODULE, 0,
5865 saved_package_name);
5866 struct symbol *sym;
5867
5868 sym = new (&objfile->objfile_obstack) symbol;
5869 sym->set_language (language_go, &objfile->objfile_obstack);
5870 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
5871 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5872 e.g., "main" finds the "main" module and not C's main(). */
5873 sym->set_domain (STRUCT_DOMAIN);
5874 sym->set_aclass_index (LOC_TYPEDEF);
5875 sym->set_type (type);
5876
5877 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
5878 }
5879 }
5880
5881 /* Allocate a fully-qualified name consisting of the two parts on the
5882 obstack. */
5883
5884 static const char *
5885 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
5886 {
5887 return obconcat (obstack, p1, "::", p2, (char *) NULL);
5888 }
5889
5890 /* A helper that allocates a variant part to attach to a Rust enum
5891 type. OBSTACK is where the results should be allocated. TYPE is
5892 the type we're processing. DISCRIMINANT_INDEX is the index of the
5893 discriminant. It must be the index of one of the fields of TYPE,
5894 or -1 to mean there is no discriminant (univariant enum).
5895 DEFAULT_INDEX is the index of the default field; or -1 if there is
5896 no default. RANGES is indexed by "effective" field number (the
5897 field index, but omitting the discriminant and default fields) and
5898 must hold the discriminant values used by the variants. Note that
5899 RANGES must have a lifetime at least as long as OBSTACK -- either
5900 already allocated on it, or static. */
5901
5902 static void
5903 alloc_rust_variant (struct obstack *obstack, struct type *type,
5904 int discriminant_index, int default_index,
5905 gdb::array_view<discriminant_range> ranges)
5906 {
5907 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
5908 gdb_assert (discriminant_index == -1
5909 || (discriminant_index >= 0
5910 && discriminant_index < type->num_fields ()));
5911 gdb_assert (default_index == -1
5912 || (default_index >= 0 && default_index < type->num_fields ()));
5913
5914 /* We have one variant for each non-discriminant field. */
5915 int n_variants = type->num_fields ();
5916 if (discriminant_index != -1)
5917 --n_variants;
5918
5919 variant *variants = new (obstack) variant[n_variants];
5920 int var_idx = 0;
5921 int range_idx = 0;
5922 for (int i = 0; i < type->num_fields (); ++i)
5923 {
5924 if (i == discriminant_index)
5925 continue;
5926
5927 variants[var_idx].first_field = i;
5928 variants[var_idx].last_field = i + 1;
5929
5930 /* The default field does not need a range, but other fields do.
5931 We skipped the discriminant above. */
5932 if (i != default_index)
5933 {
5934 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
5935 ++range_idx;
5936 }
5937
5938 ++var_idx;
5939 }
5940
5941 gdb_assert (range_idx == ranges.size ());
5942 gdb_assert (var_idx == n_variants);
5943
5944 variant_part *part = new (obstack) variant_part;
5945 part->discriminant_index = discriminant_index;
5946 /* If there is no discriminant, then whether it is signed is of no
5947 consequence. */
5948 part->is_unsigned
5949 = (discriminant_index == -1
5950 ? false
5951 : type->field (discriminant_index).type ()->is_unsigned ());
5952 part->variants = gdb::array_view<variant> (variants, n_variants);
5953
5954 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
5955 gdb::array_view<variant_part> *prop_value
5956 = new (storage) gdb::array_view<variant_part> (part, 1);
5957
5958 struct dynamic_prop prop;
5959 prop.set_variant_parts (prop_value);
5960
5961 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
5962 }
5963
5964 /* Some versions of rustc emitted enums in an unusual way.
5965
5966 Ordinary enums were emitted as unions. The first element of each
5967 structure in the union was named "RUST$ENUM$DISR". This element
5968 held the discriminant.
5969
5970 These versions of Rust also implemented the "non-zero"
5971 optimization. When the enum had two values, and one is empty and
5972 the other holds a pointer that cannot be zero, the pointer is used
5973 as the discriminant, with a zero value meaning the empty variant.
5974 Here, the union's first member is of the form
5975 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
5976 where the fieldnos are the indices of the fields that should be
5977 traversed in order to find the field (which may be several fields deep)
5978 and the variantname is the name of the variant of the case when the
5979 field is zero.
5980
5981 This function recognizes whether TYPE is of one of these forms,
5982 and, if so, smashes it to be a variant type. */
5983
5984 static void
5985 quirk_rust_enum (struct type *type, struct objfile *objfile)
5986 {
5987 gdb_assert (type->code () == TYPE_CODE_UNION);
5988
5989 /* We don't need to deal with empty enums. */
5990 if (type->num_fields () == 0)
5991 return;
5992
5993 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
5994 if (type->num_fields () == 1
5995 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
5996 {
5997 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
5998
5999 /* Decode the field name to find the offset of the
6000 discriminant. */
6001 ULONGEST bit_offset = 0;
6002 struct type *field_type = type->field (0).type ();
6003 while (name[0] >= '0' && name[0] <= '9')
6004 {
6005 char *tail;
6006 unsigned long index = strtoul (name, &tail, 10);
6007 name = tail;
6008 if (*name != '$'
6009 || index >= field_type->num_fields ()
6010 || (field_type->field (index).loc_kind ()
6011 != FIELD_LOC_KIND_BITPOS))
6012 {
6013 complaint (_("Could not parse Rust enum encoding string \"%s\""
6014 "[in module %s]"),
6015 type->field (0).name (),
6016 objfile_name (objfile));
6017 return;
6018 }
6019 ++name;
6020
6021 bit_offset += field_type->field (index).loc_bitpos ();
6022 field_type = field_type->field (index).type ();
6023 }
6024
6025 /* Smash this type to be a structure type. We have to do this
6026 because the type has already been recorded. */
6027 type->set_code (TYPE_CODE_STRUCT);
6028 type->set_num_fields (3);
6029 /* Save the field we care about. */
6030 struct field saved_field = type->field (0);
6031 type->set_fields
6032 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
6033
6034 /* Put the discriminant at index 0. */
6035 type->field (0).set_type (field_type);
6036 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
6037 type->field (0).set_name ("<<discriminant>>");
6038 type->field (0).set_loc_bitpos (bit_offset);
6039
6040 /* The order of fields doesn't really matter, so put the real
6041 field at index 1 and the data-less field at index 2. */
6042 type->field (1) = saved_field;
6043 type->field (1).set_name
6044 (rust_last_path_segment (type->field (1).type ()->name ()));
6045 type->field (1).type ()->set_name
6046 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
6047 type->field (1).name ()));
6048
6049 const char *dataless_name
6050 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
6051 name);
6052 struct type *dataless_type
6053 = type_allocator (objfile).new_type (TYPE_CODE_VOID, 0,
6054 dataless_name);
6055 type->field (2).set_type (dataless_type);
6056 /* NAME points into the original discriminant name, which
6057 already has the correct lifetime. */
6058 type->field (2).set_name (name);
6059 type->field (2).set_loc_bitpos (0);
6060
6061 /* Indicate that this is a variant type. */
6062 static discriminant_range ranges[1] = { { 0, 0 } };
6063 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
6064 }
6065 /* A union with a single anonymous field is probably an old-style
6066 univariant enum. */
6067 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
6068 {
6069 /* Smash this type to be a structure type. We have to do this
6070 because the type has already been recorded. */
6071 type->set_code (TYPE_CODE_STRUCT);
6072
6073 struct type *field_type = type->field (0).type ();
6074 const char *variant_name
6075 = rust_last_path_segment (field_type->name ());
6076 type->field (0).set_name (variant_name);
6077 field_type->set_name
6078 (rust_fully_qualify (&objfile->objfile_obstack,
6079 type->name (), variant_name));
6080
6081 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
6082 }
6083 else
6084 {
6085 struct type *disr_type = nullptr;
6086 for (int i = 0; i < type->num_fields (); ++i)
6087 {
6088 disr_type = type->field (i).type ();
6089
6090 if (disr_type->code () != TYPE_CODE_STRUCT)
6091 {
6092 /* All fields of a true enum will be structs. */
6093 return;
6094 }
6095 else if (disr_type->num_fields () == 0)
6096 {
6097 /* Could be data-less variant, so keep going. */
6098 disr_type = nullptr;
6099 }
6100 else if (strcmp (disr_type->field (0).name (),
6101 "RUST$ENUM$DISR") != 0)
6102 {
6103 /* Not a Rust enum. */
6104 return;
6105 }
6106 else
6107 {
6108 /* Found one. */
6109 break;
6110 }
6111 }
6112
6113 /* If we got here without a discriminant, then it's probably
6114 just a union. */
6115 if (disr_type == nullptr)
6116 return;
6117
6118 /* Smash this type to be a structure type. We have to do this
6119 because the type has already been recorded. */
6120 type->set_code (TYPE_CODE_STRUCT);
6121
6122 /* Make space for the discriminant field. */
6123 struct field *disr_field = &disr_type->field (0);
6124 field *new_fields
6125 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
6126 * sizeof (struct field)));
6127 memcpy (new_fields + 1, type->fields (),
6128 type->num_fields () * sizeof (struct field));
6129 type->set_fields (new_fields);
6130 type->set_num_fields (type->num_fields () + 1);
6131
6132 /* Install the discriminant at index 0 in the union. */
6133 type->field (0) = *disr_field;
6134 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
6135 type->field (0).set_name ("<<discriminant>>");
6136
6137 /* We need a way to find the correct discriminant given a
6138 variant name. For convenience we build a map here. */
6139 struct type *enum_type = disr_field->type ();
6140 std::unordered_map<std::string, ULONGEST> discriminant_map;
6141 for (int i = 0; i < enum_type->num_fields (); ++i)
6142 {
6143 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
6144 {
6145 const char *name
6146 = rust_last_path_segment (enum_type->field (i).name ());
6147 discriminant_map[name] = enum_type->field (i).loc_enumval ();
6148 }
6149 }
6150
6151 int n_fields = type->num_fields ();
6152 /* We don't need a range entry for the discriminant, but we do
6153 need one for every other field, as there is no default
6154 variant. */
6155 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
6156 discriminant_range,
6157 n_fields - 1);
6158 /* Skip the discriminant here. */
6159 for (int i = 1; i < n_fields; ++i)
6160 {
6161 /* Find the final word in the name of this variant's type.
6162 That name can be used to look up the correct
6163 discriminant. */
6164 const char *variant_name
6165 = rust_last_path_segment (type->field (i).type ()->name ());
6166
6167 auto iter = discriminant_map.find (variant_name);
6168 if (iter != discriminant_map.end ())
6169 {
6170 ranges[i - 1].low = iter->second;
6171 ranges[i - 1].high = iter->second;
6172 }
6173
6174 /* In Rust, each element should have the size of the
6175 enclosing enum. */
6176 type->field (i).type ()->set_length (type->length ());
6177
6178 /* Remove the discriminant field, if it exists. */
6179 struct type *sub_type = type->field (i).type ();
6180 if (sub_type->num_fields () > 0)
6181 {
6182 sub_type->set_num_fields (sub_type->num_fields () - 1);
6183 sub_type->set_fields (sub_type->fields () + 1);
6184 }
6185 type->field (i).set_name (variant_name);
6186 sub_type->set_name
6187 (rust_fully_qualify (&objfile->objfile_obstack,
6188 type->name (), variant_name));
6189 }
6190
6191 /* Indicate that this is a variant type. */
6192 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
6193 gdb::array_view<discriminant_range> (ranges,
6194 n_fields - 1));
6195 }
6196 }
6197
6198 /* Rewrite some Rust unions to be structures with variants parts. */
6199
6200 static void
6201 rust_union_quirks (struct dwarf2_cu *cu)
6202 {
6203 gdb_assert (cu->lang () == language_rust);
6204 for (type *type_ : cu->rust_unions)
6205 quirk_rust_enum (type_, cu->per_objfile->objfile);
6206 /* We don't need this any more. */
6207 cu->rust_unions.clear ();
6208 }
6209
6210 /* See read.h. */
6211
6212 type_unit_group_unshareable *
6213 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
6214 {
6215 auto iter = this->m_type_units.find (tu_group);
6216 if (iter != this->m_type_units.end ())
6217 return iter->second.get ();
6218
6219 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
6220 type_unit_group_unshareable *result = uniq.get ();
6221 this->m_type_units[tu_group] = std::move (uniq);
6222 return result;
6223 }
6224
6225 struct type *
6226 dwarf2_per_objfile::get_type_for_signatured_type
6227 (signatured_type *sig_type) const
6228 {
6229 auto iter = this->m_type_map.find (sig_type);
6230 if (iter == this->m_type_map.end ())
6231 return nullptr;
6232
6233 return iter->second;
6234 }
6235
6236 void dwarf2_per_objfile::set_type_for_signatured_type
6237 (signatured_type *sig_type, struct type *type)
6238 {
6239 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
6240
6241 this->m_type_map[sig_type] = type;
6242 }
6243
6244 /* A helper function for computing the list of all symbol tables
6245 included by PER_CU. */
6246
6247 static void
6248 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
6249 htab_t all_children, htab_t all_type_symtabs,
6250 dwarf2_per_cu_data *per_cu,
6251 dwarf2_per_objfile *per_objfile,
6252 struct compunit_symtab *immediate_parent)
6253 {
6254 void **slot = htab_find_slot (all_children, per_cu, INSERT);
6255 if (*slot != NULL)
6256 {
6257 /* This inclusion and its children have been processed. */
6258 return;
6259 }
6260
6261 *slot = per_cu;
6262
6263 /* Only add a CU if it has a symbol table. */
6264 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6265 if (cust != NULL)
6266 {
6267 /* If this is a type unit only add its symbol table if we haven't
6268 seen it yet (type unit per_cu's can share symtabs). */
6269 if (per_cu->is_debug_types)
6270 {
6271 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
6272 if (*slot == NULL)
6273 {
6274 *slot = cust;
6275 result->push_back (cust);
6276 if (cust->user == NULL)
6277 cust->user = immediate_parent;
6278 }
6279 }
6280 else
6281 {
6282 result->push_back (cust);
6283 if (cust->user == NULL)
6284 cust->user = immediate_parent;
6285 }
6286 }
6287
6288 if (!per_cu->imported_symtabs_empty ())
6289 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
6290 {
6291 recursively_compute_inclusions (result, all_children,
6292 all_type_symtabs, ptr, per_objfile,
6293 cust);
6294 }
6295 }
6296
6297 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
6298 PER_CU. */
6299
6300 static void
6301 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
6302 dwarf2_per_objfile *per_objfile)
6303 {
6304 gdb_assert (! per_cu->is_debug_types);
6305
6306 if (!per_cu->imported_symtabs_empty ())
6307 {
6308 int len;
6309 std::vector<compunit_symtab *> result_symtabs;
6310 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
6311
6312 /* If we don't have a symtab, we can just skip this case. */
6313 if (cust == NULL)
6314 return;
6315
6316 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
6317 htab_eq_pointer,
6318 NULL, xcalloc, xfree));
6319 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
6320 htab_eq_pointer,
6321 NULL, xcalloc, xfree));
6322
6323 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
6324 {
6325 recursively_compute_inclusions (&result_symtabs, all_children.get (),
6326 all_type_symtabs.get (), ptr,
6327 per_objfile, cust);
6328 }
6329
6330 /* Now we have a transitive closure of all the included symtabs. */
6331 len = result_symtabs.size ();
6332 cust->includes
6333 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
6334 struct compunit_symtab *, len + 1);
6335 memcpy (cust->includes, result_symtabs.data (),
6336 len * sizeof (compunit_symtab *));
6337 cust->includes[len] = NULL;
6338 }
6339 }
6340
6341 /* Compute the 'includes' field for the symtabs of all the CUs we just
6342 read. */
6343
6344 static void
6345 process_cu_includes (dwarf2_per_objfile *per_objfile)
6346 {
6347 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
6348 {
6349 if (! iter->is_debug_types)
6350 compute_compunit_symtab_includes (iter, per_objfile);
6351 }
6352
6353 per_objfile->per_bfd->just_read_cus.clear ();
6354 }
6355
6356 /* Generate full symbol information for CU, whose DIEs have
6357 already been loaded into memory. */
6358
6359 static void
6360 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
6361 {
6362 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6363 struct objfile *objfile = per_objfile->objfile;
6364 struct gdbarch *gdbarch = objfile->arch ();
6365 CORE_ADDR lowpc, highpc;
6366 struct compunit_symtab *cust;
6367 CORE_ADDR baseaddr;
6368 struct block *static_block;
6369 CORE_ADDR addr;
6370
6371 baseaddr = objfile->text_section_offset ();
6372
6373 /* Clear the list here in case something was left over. */
6374 cu->method_list.clear ();
6375
6376 dwarf2_find_base_address (cu->dies, cu);
6377
6378 /* Before we start reading the top-level DIE, ensure it has a valid tag
6379 type. */
6380 switch (cu->dies->tag)
6381 {
6382 case DW_TAG_compile_unit:
6383 case DW_TAG_partial_unit:
6384 case DW_TAG_type_unit:
6385 break;
6386 default:
6387 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6388 dwarf_tag_name (cu->dies->tag),
6389 sect_offset_str (cu->per_cu->sect_off),
6390 objfile_name (per_objfile->objfile));
6391 }
6392
6393 /* Do line number decoding in read_file_scope () */
6394 process_die (cu->dies, cu);
6395
6396 /* For now fudge the Go package. */
6397 if (cu->lang () == language_go)
6398 fixup_go_packaging (cu);
6399
6400 /* Now that we have processed all the DIEs in the CU, all the types
6401 should be complete, and it should now be safe to compute all of the
6402 physnames. */
6403 compute_delayed_physnames (cu);
6404
6405 if (cu->lang () == language_rust)
6406 rust_union_quirks (cu);
6407
6408 /* Some compilers don't define a DW_AT_high_pc attribute for the
6409 compilation unit. If the DW_AT_high_pc is missing, synthesize
6410 it, by scanning the DIE's below the compilation unit. */
6411 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6412
6413 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
6414 static_block
6415 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
6416
6417 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6418 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6419 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6420 addrmap to help ensure it has an accurate map of pc values belonging to
6421 this comp unit. */
6422 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6423
6424 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
6425 (static_block, 0);
6426
6427 if (cust != NULL)
6428 {
6429 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6430
6431 /* Set symtab language to language from DW_AT_language. If the
6432 compilation is from a C file generated by language preprocessors, do
6433 not set the language if it was already deduced by start_subfile. */
6434 if (!(cu->lang () == language_c
6435 && cust->primary_filetab ()->language () != language_unknown))
6436 cust->primary_filetab ()->set_language (cu->lang ());
6437
6438 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6439 produce DW_AT_location with location lists but it can be possibly
6440 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6441 there were bugs in prologue debug info, fixed later in GCC-4.5
6442 by "unwind info for epilogues" patch (which is not directly related).
6443
6444 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6445 needed, it would be wrong due to missing DW_AT_producer there.
6446
6447 Still one can confuse GDB by using non-standard GCC compilation
6448 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6449 */
6450 if (cu->has_loclist && gcc_4_minor >= 5)
6451 cust->set_locations_valid (true);
6452
6453 int major, minor;
6454 if (cu->producer != nullptr
6455 && producer_is_gcc (cu->producer, &major, &minor)
6456 && (major < 4 || (major == 4 && minor < 5)))
6457 /* Don't trust gcc < 4.5.x. */
6458 cust->set_epilogue_unwind_valid (false);
6459 else
6460 cust->set_epilogue_unwind_valid (true);
6461
6462 cust->set_call_site_htab (cu->call_site_htab);
6463 }
6464
6465 per_objfile->set_symtab (cu->per_cu, cust);
6466
6467 /* Push it for inclusion processing later. */
6468 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
6469
6470 /* Not needed any more. */
6471 cu->reset_builder ();
6472 }
6473
6474 /* Generate full symbol information for type unit CU, whose DIEs have
6475 already been loaded into memory. */
6476
6477 static void
6478 process_full_type_unit (dwarf2_cu *cu,
6479 enum language pretend_language)
6480 {
6481 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6482 struct compunit_symtab *cust;
6483 struct signatured_type *sig_type;
6484
6485 gdb_assert (cu->per_cu->is_debug_types);
6486 sig_type = (struct signatured_type *) cu->per_cu;
6487
6488 /* Clear the list here in case something was left over. */
6489 cu->method_list.clear ();
6490
6491 /* The symbol tables are set up in read_type_unit_scope. */
6492 process_die (cu->dies, cu);
6493
6494 /* For now fudge the Go package. */
6495 if (cu->lang () == language_go)
6496 fixup_go_packaging (cu);
6497
6498 /* Now that we have processed all the DIEs in the CU, all the types
6499 should be complete, and it should now be safe to compute all of the
6500 physnames. */
6501 compute_delayed_physnames (cu);
6502
6503 if (cu->lang () == language_rust)
6504 rust_union_quirks (cu);
6505
6506 /* TUs share symbol tables.
6507 If this is the first TU to use this symtab, complete the construction
6508 of it with end_expandable_symtab. Otherwise, complete the addition of
6509 this TU's symbols to the existing symtab. */
6510 type_unit_group_unshareable *tug_unshare =
6511 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
6512 if (tug_unshare->compunit_symtab == NULL)
6513 {
6514 buildsym_compunit *builder = cu->get_builder ();
6515 cust = builder->end_expandable_symtab (0);
6516 tug_unshare->compunit_symtab = cust;
6517
6518 if (cust != NULL)
6519 {
6520 /* Set symtab language to language from DW_AT_language. If the
6521 compilation is from a C file generated by language preprocessors,
6522 do not set the language if it was already deduced by
6523 start_subfile. */
6524 if (!(cu->lang () == language_c
6525 && cust->primary_filetab ()->language () != language_c))
6526 cust->primary_filetab ()->set_language (cu->lang ());
6527 }
6528 }
6529 else
6530 {
6531 cu->get_builder ()->augment_type_symtab ();
6532 cust = tug_unshare->compunit_symtab;
6533 }
6534
6535 per_objfile->set_symtab (cu->per_cu, cust);
6536
6537 /* Not needed any more. */
6538 cu->reset_builder ();
6539 }
6540
6541 /* Process an imported unit DIE. */
6542
6543 static void
6544 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6545 {
6546 struct attribute *attr;
6547
6548 /* For now we don't handle imported units in type units. */
6549 if (cu->per_cu->is_debug_types)
6550 {
6551 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6552 " supported in type units [in module %s]"),
6553 objfile_name (cu->per_objfile->objfile));
6554 }
6555
6556 attr = dwarf2_attr (die, DW_AT_import, cu);
6557 if (attr != NULL)
6558 {
6559 sect_offset sect_off = attr->get_ref_die_offset ();
6560 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
6561 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6562 dwarf2_per_cu_data *per_cu
6563 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
6564 per_objfile->per_bfd);
6565
6566 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
6567 into another compilation unit, at root level. Regard this as a hint,
6568 and ignore it. This is a best effort, it only works if unit_type and
6569 lang are already set. */
6570 if (die->parent && die->parent->parent == NULL
6571 && per_cu->unit_type (false) == DW_UT_compile
6572 && per_cu->lang (false) == language_cplus)
6573 return;
6574
6575 /* If necessary, add it to the queue and load its DIEs. */
6576 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
6577 cu->lang ()))
6578 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
6579 false, cu->lang ());
6580
6581 cu->per_cu->imported_symtabs_push (per_cu);
6582 }
6583 }
6584
6585 /* RAII object that represents a process_die scope: i.e.,
6586 starts/finishes processing a DIE. */
6587 class process_die_scope
6588 {
6589 public:
6590 process_die_scope (die_info *die, dwarf2_cu *cu)
6591 : m_die (die), m_cu (cu)
6592 {
6593 /* We should only be processing DIEs not already in process. */
6594 gdb_assert (!m_die->in_process);
6595 m_die->in_process = true;
6596 }
6597
6598 ~process_die_scope ()
6599 {
6600 m_die->in_process = false;
6601
6602 /* If we're done processing the DIE for the CU that owns the line
6603 header, we don't need the line header anymore. */
6604 if (m_cu->line_header_die_owner == m_die)
6605 {
6606 delete m_cu->line_header;
6607 m_cu->line_header = NULL;
6608 m_cu->line_header_die_owner = NULL;
6609 }
6610 }
6611
6612 private:
6613 die_info *m_die;
6614 dwarf2_cu *m_cu;
6615 };
6616
6617 /* Process a die and its children. */
6618
6619 static void
6620 process_die (struct die_info *die, struct dwarf2_cu *cu)
6621 {
6622 process_die_scope scope (die, cu);
6623
6624 switch (die->tag)
6625 {
6626 case DW_TAG_padding:
6627 break;
6628 case DW_TAG_compile_unit:
6629 case DW_TAG_partial_unit:
6630 read_file_scope (die, cu);
6631 break;
6632 case DW_TAG_type_unit:
6633 read_type_unit_scope (die, cu);
6634 break;
6635 case DW_TAG_subprogram:
6636 /* Nested subprograms in Fortran get a prefix. */
6637 if (cu->lang () == language_fortran
6638 && die->parent != NULL
6639 && die->parent->tag == DW_TAG_subprogram)
6640 cu->processing_has_namespace_info = true;
6641 /* Fall through. */
6642 case DW_TAG_inlined_subroutine:
6643 read_func_scope (die, cu);
6644 break;
6645 case DW_TAG_lexical_block:
6646 case DW_TAG_try_block:
6647 case DW_TAG_catch_block:
6648 read_lexical_block_scope (die, cu);
6649 break;
6650 case DW_TAG_call_site:
6651 case DW_TAG_GNU_call_site:
6652 read_call_site_scope (die, cu);
6653 break;
6654 case DW_TAG_class_type:
6655 case DW_TAG_interface_type:
6656 case DW_TAG_structure_type:
6657 case DW_TAG_union_type:
6658 case DW_TAG_namelist:
6659 process_structure_scope (die, cu);
6660 break;
6661 case DW_TAG_enumeration_type:
6662 process_enumeration_scope (die, cu);
6663 break;
6664
6665 /* These dies have a type, but processing them does not create
6666 a symbol or recurse to process the children. Therefore we can
6667 read them on-demand through read_type_die. */
6668 case DW_TAG_subroutine_type:
6669 case DW_TAG_set_type:
6670 case DW_TAG_pointer_type:
6671 case DW_TAG_ptr_to_member_type:
6672 case DW_TAG_reference_type:
6673 case DW_TAG_rvalue_reference_type:
6674 case DW_TAG_string_type:
6675 break;
6676
6677 case DW_TAG_array_type:
6678 /* We only need to handle this case for Ada -- in other
6679 languages, it's normal for the compiler to emit a typedef
6680 instead. */
6681 if (cu->lang () != language_ada)
6682 break;
6683 /* FALLTHROUGH */
6684 case DW_TAG_base_type:
6685 case DW_TAG_subrange_type:
6686 case DW_TAG_generic_subrange:
6687 case DW_TAG_typedef:
6688 /* Add a typedef symbol for the type definition, if it has a
6689 DW_AT_name. */
6690 new_symbol (die, read_type_die (die, cu), cu);
6691 break;
6692 case DW_TAG_common_block:
6693 read_common_block (die, cu);
6694 break;
6695 case DW_TAG_common_inclusion:
6696 break;
6697 case DW_TAG_namespace:
6698 cu->processing_has_namespace_info = true;
6699 read_namespace (die, cu);
6700 break;
6701 case DW_TAG_module:
6702 cu->processing_has_namespace_info = true;
6703 read_module (die, cu);
6704 break;
6705 case DW_TAG_imported_declaration:
6706 cu->processing_has_namespace_info = true;
6707 if (read_alias (die, cu))
6708 break;
6709 /* The declaration is neither a global namespace nor a variable
6710 alias. */
6711 /* Fall through. */
6712 case DW_TAG_imported_module:
6713 cu->processing_has_namespace_info = true;
6714 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6715 || cu->lang () != language_fortran))
6716 complaint (_("Tag '%s' has unexpected children"),
6717 dwarf_tag_name (die->tag));
6718 read_import_statement (die, cu);
6719 break;
6720
6721 case DW_TAG_imported_unit:
6722 process_imported_unit_die (die, cu);
6723 break;
6724
6725 case DW_TAG_variable:
6726 read_variable (die, cu);
6727 break;
6728
6729 default:
6730 new_symbol (die, NULL, cu);
6731 break;
6732 }
6733 }
6734 \f
6735 /* DWARF name computation. */
6736
6737 /* A helper function for dwarf2_compute_name which determines whether DIE
6738 needs to have the name of the scope prepended to the name listed in the
6739 die. */
6740
6741 static int
6742 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6743 {
6744 struct attribute *attr;
6745
6746 switch (die->tag)
6747 {
6748 case DW_TAG_namespace:
6749 case DW_TAG_typedef:
6750 case DW_TAG_class_type:
6751 case DW_TAG_interface_type:
6752 case DW_TAG_structure_type:
6753 case DW_TAG_union_type:
6754 case DW_TAG_enumeration_type:
6755 case DW_TAG_enumerator:
6756 case DW_TAG_subprogram:
6757 case DW_TAG_inlined_subroutine:
6758 case DW_TAG_member:
6759 case DW_TAG_imported_declaration:
6760 return 1;
6761
6762 case DW_TAG_variable:
6763 case DW_TAG_constant:
6764 /* We only need to prefix "globally" visible variables. These include
6765 any variable marked with DW_AT_external or any variable that
6766 lives in a namespace. [Variables in anonymous namespaces
6767 require prefixing, but they are not DW_AT_external.] */
6768
6769 if (dwarf2_attr (die, DW_AT_specification, cu))
6770 {
6771 struct dwarf2_cu *spec_cu = cu;
6772
6773 return die_needs_namespace (die_specification (die, &spec_cu),
6774 spec_cu);
6775 }
6776
6777 attr = dwarf2_attr (die, DW_AT_external, cu);
6778 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6779 && die->parent->tag != DW_TAG_module)
6780 return 0;
6781 /* A variable in a lexical block of some kind does not need a
6782 namespace, even though in C++ such variables may be external
6783 and have a mangled name. */
6784 if (die->parent->tag == DW_TAG_lexical_block
6785 || die->parent->tag == DW_TAG_try_block
6786 || die->parent->tag == DW_TAG_catch_block
6787 || die->parent->tag == DW_TAG_subprogram)
6788 return 0;
6789 return 1;
6790
6791 default:
6792 return 0;
6793 }
6794 }
6795
6796 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
6797 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6798 defined for the given DIE. */
6799
6800 static struct attribute *
6801 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
6802 {
6803 struct attribute *attr;
6804
6805 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6806 if (attr == NULL)
6807 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6808
6809 return attr;
6810 }
6811
6812 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
6813 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6814 defined for the given DIE. */
6815
6816 static const char *
6817 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
6818 {
6819 const char *linkage_name;
6820
6821 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
6822 if (linkage_name == NULL)
6823 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
6824
6825 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
6826 See https://github.com/rust-lang/rust/issues/32925. */
6827 if (cu->lang () == language_rust && linkage_name != NULL
6828 && strchr (linkage_name, '{') != NULL)
6829 linkage_name = NULL;
6830
6831 return linkage_name;
6832 }
6833
6834 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6835 compute the physname for the object, which include a method's:
6836 - formal parameters (C++),
6837 - receiver type (Go),
6838
6839 The term "physname" is a bit confusing.
6840 For C++, for example, it is the demangled name.
6841 For Go, for example, it's the mangled name.
6842
6843 For Ada, return the DIE's linkage name rather than the fully qualified
6844 name. PHYSNAME is ignored..
6845
6846 The result is allocated on the objfile->per_bfd's obstack and
6847 canonicalized. */
6848
6849 static const char *
6850 dwarf2_compute_name (const char *name,
6851 struct die_info *die, struct dwarf2_cu *cu,
6852 int physname)
6853 {
6854 struct objfile *objfile = cu->per_objfile->objfile;
6855
6856 if (name == NULL)
6857 name = dwarf2_name (die, cu);
6858
6859 enum language lang = cu->lang ();
6860
6861 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
6862 but otherwise compute it by typename_concat inside GDB.
6863 FIXME: Actually this is not really true, or at least not always true.
6864 It's all very confusing. compute_and_set_names doesn't try to demangle
6865 Fortran names because there is no mangling standard. So new_symbol
6866 will set the demangled name to the result of dwarf2_full_name, and it is
6867 the demangled name that GDB uses if it exists. */
6868 if (lang == language_ada
6869 || (lang == language_fortran && physname))
6870 {
6871 /* For Ada unit, we prefer the linkage name over the name, as
6872 the former contains the exported name, which the user expects
6873 to be able to reference. Ideally, we want the user to be able
6874 to reference this entity using either natural or linkage name,
6875 but we haven't started looking at this enhancement yet. */
6876 const char *linkage_name = dw2_linkage_name (die, cu);
6877
6878 if (linkage_name != NULL)
6879 return linkage_name;
6880 }
6881
6882 /* These are the only languages we know how to qualify names in. */
6883 if (name != NULL
6884 && (lang == language_cplus
6885 || lang == language_fortran || lang == language_d
6886 || lang == language_rust))
6887 {
6888 if (die_needs_namespace (die, cu))
6889 {
6890 const char *prefix;
6891
6892 string_file buf;
6893
6894 prefix = determine_prefix (die, cu);
6895 if (*prefix != '\0')
6896 {
6897 gdb::unique_xmalloc_ptr<char> prefixed_name
6898 (typename_concat (NULL, prefix, name, physname, cu));
6899
6900 buf.puts (prefixed_name.get ());
6901 }
6902 else
6903 buf.puts (name);
6904
6905 /* Template parameters may be specified in the DIE's DW_AT_name, or
6906 as children with DW_TAG_template_type_param or
6907 DW_TAG_value_type_param. If the latter, add them to the name
6908 here. If the name already has template parameters, then
6909 skip this step; some versions of GCC emit both, and
6910 it is more efficient to use the pre-computed name.
6911
6912 Something to keep in mind about this process: it is very
6913 unlikely, or in some cases downright impossible, to produce
6914 something that will match the mangled name of a function.
6915 If the definition of the function has the same debug info,
6916 we should be able to match up with it anyway. But fallbacks
6917 using the minimal symbol, for instance to find a method
6918 implemented in a stripped copy of libstdc++, will not work.
6919 If we do not have debug info for the definition, we will have to
6920 match them up some other way.
6921
6922 When we do name matching there is a related problem with function
6923 templates; two instantiated function templates are allowed to
6924 differ only by their return types, which we do not add here. */
6925
6926 if (lang == language_cplus && strchr (name, '<') == NULL)
6927 {
6928 struct attribute *attr;
6929 struct die_info *child;
6930 int first = 1;
6931
6932 die->building_fullname = 1;
6933
6934 for (child = die->child; child != NULL; child = child->sibling)
6935 {
6936 struct type *type;
6937 LONGEST value;
6938 const gdb_byte *bytes;
6939 struct dwarf2_locexpr_baton *baton;
6940 struct value *v;
6941
6942 if (child->tag != DW_TAG_template_type_param
6943 && child->tag != DW_TAG_template_value_param)
6944 continue;
6945
6946 if (first)
6947 {
6948 buf.puts ("<");
6949 first = 0;
6950 }
6951 else
6952 buf.puts (", ");
6953
6954 attr = dwarf2_attr (child, DW_AT_type, cu);
6955 if (attr == NULL)
6956 {
6957 complaint (_("template parameter missing DW_AT_type"));
6958 buf.puts ("UNKNOWN_TYPE");
6959 continue;
6960 }
6961 type = die_type (child, cu);
6962
6963 if (child->tag == DW_TAG_template_type_param)
6964 {
6965 cu->language_defn->print_type (type, "", &buf, -1, 0,
6966 &type_print_raw_options);
6967 continue;
6968 }
6969
6970 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6971 if (attr == NULL)
6972 {
6973 complaint (_("template parameter missing "
6974 "DW_AT_const_value"));
6975 buf.puts ("UNKNOWN_VALUE");
6976 continue;
6977 }
6978
6979 dwarf2_const_value_attr (attr, type, name,
6980 &cu->comp_unit_obstack, cu,
6981 &value, &bytes, &baton);
6982
6983 if (type->has_no_signedness ())
6984 /* GDB prints characters as NUMBER 'CHAR'. If that's
6985 changed, this can use value_print instead. */
6986 cu->language_defn->printchar (value, type, &buf);
6987 else
6988 {
6989 struct value_print_options opts;
6990
6991 if (baton != NULL)
6992 v = dwarf2_evaluate_loc_desc (type, NULL,
6993 baton->data,
6994 baton->size,
6995 baton->per_cu,
6996 baton->per_objfile);
6997 else if (bytes != NULL)
6998 {
6999 v = value::allocate (type);
7000 memcpy (v->contents_writeable ().data (), bytes,
7001 type->length ());
7002 }
7003 else
7004 v = value_from_longest (type, value);
7005
7006 /* Specify decimal so that we do not depend on
7007 the radix. */
7008 get_formatted_print_options (&opts, 'd');
7009 opts.raw = true;
7010 value_print (v, &buf, &opts);
7011 release_value (v);
7012 }
7013 }
7014
7015 die->building_fullname = 0;
7016
7017 if (!first)
7018 {
7019 /* Close the argument list, with a space if necessary
7020 (nested templates). */
7021 if (!buf.empty () && buf.string ().back () == '>')
7022 buf.puts (" >");
7023 else
7024 buf.puts (">");
7025 }
7026 }
7027
7028 /* For C++ methods, append formal parameter type
7029 information, if PHYSNAME. */
7030
7031 if (physname && die->tag == DW_TAG_subprogram
7032 && lang == language_cplus)
7033 {
7034 struct type *type = read_type_die (die, cu);
7035
7036 c_type_print_args (type, &buf, 1, lang,
7037 &type_print_raw_options);
7038
7039 if (lang == language_cplus)
7040 {
7041 /* Assume that an artificial first parameter is
7042 "this", but do not crash if it is not. RealView
7043 marks unnamed (and thus unused) parameters as
7044 artificial; there is no way to differentiate
7045 the two cases. */
7046 if (type->num_fields () > 0
7047 && TYPE_FIELD_ARTIFICIAL (type, 0)
7048 && type->field (0).type ()->code () == TYPE_CODE_PTR
7049 && TYPE_CONST (type->field (0).type ()->target_type ()))
7050 buf.puts (" const");
7051 }
7052 }
7053
7054 const std::string &intermediate_name = buf.string ();
7055
7056 const char *canonical_name
7057 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
7058 objfile);
7059
7060 /* If we only computed INTERMEDIATE_NAME, or if
7061 INTERMEDIATE_NAME is already canonical, then we need to
7062 intern it. */
7063 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
7064 name = objfile->intern (intermediate_name);
7065 else
7066 name = canonical_name;
7067 }
7068 }
7069
7070 return name;
7071 }
7072
7073 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7074 If scope qualifiers are appropriate they will be added. The result
7075 will be allocated on the storage_obstack, or NULL if the DIE does
7076 not have a name. NAME may either be from a previous call to
7077 dwarf2_name or NULL.
7078
7079 The output string will be canonicalized (if C++). */
7080
7081 static const char *
7082 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7083 {
7084 return dwarf2_compute_name (name, die, cu, 0);
7085 }
7086
7087 /* Construct a physname for the given DIE in CU. NAME may either be
7088 from a previous call to dwarf2_name or NULL. The result will be
7089 allocated on the objfile_objstack or NULL if the DIE does not have a
7090 name.
7091
7092 The output string will be canonicalized (if C++). */
7093
7094 static const char *
7095 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7096 {
7097 struct objfile *objfile = cu->per_objfile->objfile;
7098 const char *retval, *mangled = NULL, *canon = NULL;
7099 int need_copy = 1;
7100
7101 /* In this case dwarf2_compute_name is just a shortcut not building anything
7102 on its own. */
7103 if (!die_needs_namespace (die, cu))
7104 return dwarf2_compute_name (name, die, cu, 1);
7105
7106 if (cu->lang () != language_rust)
7107 mangled = dw2_linkage_name (die, cu);
7108
7109 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7110 has computed. */
7111 gdb::unique_xmalloc_ptr<char> demangled;
7112 if (mangled != NULL)
7113 {
7114 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
7115 {
7116 /* Do nothing (do not demangle the symbol name). */
7117 }
7118 else
7119 {
7120 /* Use DMGL_RET_DROP for C++ template functions to suppress
7121 their return type. It is easier for GDB users to search
7122 for such functions as `name(params)' than `long name(params)'.
7123 In such case the minimal symbol names do not match the full
7124 symbol names but for template functions there is never a need
7125 to look up their definition from their declaration so
7126 the only disadvantage remains the minimal symbol variant
7127 `long name(params)' does not have the proper inferior type. */
7128 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
7129 | DMGL_RET_DROP));
7130 }
7131 if (demangled)
7132 canon = demangled.get ();
7133 else
7134 {
7135 canon = mangled;
7136 need_copy = 0;
7137 }
7138 }
7139
7140 if (canon == NULL || check_physname)
7141 {
7142 const char *physname = dwarf2_compute_name (name, die, cu, 1);
7143
7144 if (canon != NULL && strcmp (physname, canon) != 0)
7145 {
7146 /* It may not mean a bug in GDB. The compiler could also
7147 compute DW_AT_linkage_name incorrectly. But in such case
7148 GDB would need to be bug-to-bug compatible. */
7149
7150 complaint (_("Computed physname <%s> does not match demangled <%s> "
7151 "(from linkage <%s>) - DIE at %s [in module %s]"),
7152 physname, canon, mangled, sect_offset_str (die->sect_off),
7153 objfile_name (objfile));
7154
7155 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7156 is available here - over computed PHYSNAME. It is safer
7157 against both buggy GDB and buggy compilers. */
7158
7159 retval = canon;
7160 }
7161 else
7162 {
7163 retval = physname;
7164 need_copy = 0;
7165 }
7166 }
7167 else
7168 retval = canon;
7169
7170 if (need_copy)
7171 retval = objfile->intern (retval);
7172
7173 return retval;
7174 }
7175
7176 /* Inspect DIE in CU for a namespace alias or a variable with alias
7177 attribute. If one exists, record a new symbol for it.
7178
7179 Returns true if an alias was recorded, false otherwise. */
7180
7181 static bool
7182 read_alias (struct die_info *die, struct dwarf2_cu *cu)
7183 {
7184 struct attribute *attr;
7185
7186 /* If the die does not have a name, this is neither a namespace
7187 alias nor a variable alias. */
7188 attr = dwarf2_attr (die, DW_AT_name, cu);
7189 if (attr != NULL)
7190 {
7191 int num;
7192 struct die_info *d = die;
7193 struct dwarf2_cu *imported_cu = cu;
7194
7195 /* If the compiler has nested DW_AT_imported_declaration DIEs,
7196 keep inspecting DIEs until we hit the underlying import. */
7197 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
7198 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
7199 {
7200 attr = dwarf2_attr (d, DW_AT_import, cu);
7201 if (attr == NULL)
7202 break;
7203
7204 d = follow_die_ref (d, attr, &imported_cu);
7205 if (d->tag != DW_TAG_imported_declaration)
7206 break;
7207 }
7208
7209 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
7210 {
7211 complaint (_("DIE at %s has too many recursively imported "
7212 "declarations"), sect_offset_str (d->sect_off));
7213 return false;
7214 }
7215
7216 if (attr != NULL)
7217 {
7218 struct type *type;
7219 if (d->tag == DW_TAG_variable)
7220 {
7221 /* This declaration is a C/C++ global variable alias.
7222 Add a symbol for it whose type is the same as the
7223 aliased variable's. */
7224 type = die_type (d, imported_cu);
7225 struct symbol *sym = new_symbol (die, type, cu);
7226 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
7227 sym->set_aclass_index (LOC_UNRESOLVED);
7228 if (attr != nullptr)
7229 var_decode_location (attr, sym, cu);
7230 return true;
7231 }
7232 else
7233 {
7234 sect_offset sect_off = attr->get_ref_die_offset ();
7235 type = get_die_type_at_offset (sect_off, cu->per_cu,
7236 cu->per_objfile);
7237 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
7238 {
7239 /* This declaration is a global namespace alias. Add
7240 a symbol for it whose type is the aliased
7241 namespace. */
7242 new_symbol (die, type, cu);
7243 return true;
7244 }
7245 }
7246 }
7247 }
7248 return false;
7249 }
7250
7251 /* Return the using directives repository (global or local?) to use in the
7252 current context for CU.
7253
7254 For Ada, imported declarations can materialize renamings, which *may* be
7255 global. However it is impossible (for now?) in DWARF to distinguish
7256 "external" imported declarations and "static" ones. As all imported
7257 declarations seem to be static in all other languages, make them all CU-wide
7258 global only in Ada. */
7259
7260 static struct using_direct **
7261 using_directives (struct dwarf2_cu *cu)
7262 {
7263 if (cu->lang () == language_ada
7264 && cu->get_builder ()->outermost_context_p ())
7265 return cu->get_builder ()->get_global_using_directives ();
7266 else
7267 return cu->get_builder ()->get_local_using_directives ();
7268 }
7269
7270 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
7271 given CU. If the format is not recognized or the attribute is
7272 not present, set it to 0. */
7273
7274 static unsigned int
7275 read_decl_line (struct die_info *die, struct dwarf2_cu *cu)
7276 {
7277 struct attribute *decl_line = dwarf2_attr (die, DW_AT_decl_line, cu);
7278 if (decl_line == nullptr)
7279 return 0;
7280 if (decl_line->form_is_constant ())
7281 {
7282 LONGEST val = decl_line->constant_value (0);
7283 if (0 <= val && val <= UINT_MAX)
7284 return (unsigned int) val;
7285
7286 complaint (_("Declared line for using directive is too large"));
7287 return 0;
7288 }
7289
7290 complaint (_("Declared line for using directive is of incorrect format"));
7291 return 0;
7292 }
7293
7294 /* Read the import statement specified by the given die and record it. */
7295
7296 static void
7297 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7298 {
7299 struct objfile *objfile = cu->per_objfile->objfile;
7300 struct attribute *import_attr;
7301 struct die_info *imported_die, *child_die;
7302 struct dwarf2_cu *imported_cu;
7303 const char *imported_name;
7304 const char *imported_name_prefix;
7305 const char *canonical_name;
7306 const char *import_alias;
7307 const char *imported_declaration = NULL;
7308 const char *import_prefix;
7309 std::vector<const char *> excludes;
7310
7311 import_attr = dwarf2_attr (die, DW_AT_import, cu);
7312 if (import_attr == NULL)
7313 {
7314 complaint (_("Tag '%s' has no DW_AT_import"),
7315 dwarf_tag_name (die->tag));
7316 return;
7317 }
7318
7319 imported_cu = cu;
7320 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7321 imported_name = dwarf2_name (imported_die, imported_cu);
7322 if (imported_name == NULL)
7323 {
7324 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7325
7326 The import in the following code:
7327 namespace A
7328 {
7329 typedef int B;
7330 }
7331
7332 int main ()
7333 {
7334 using A::B;
7335 B b;
7336 return b;
7337 }
7338
7339 ...
7340 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7341 <52> DW_AT_decl_file : 1
7342 <53> DW_AT_decl_line : 6
7343 <54> DW_AT_import : <0x75>
7344 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7345 <59> DW_AT_name : B
7346 <5b> DW_AT_decl_file : 1
7347 <5c> DW_AT_decl_line : 2
7348 <5d> DW_AT_type : <0x6e>
7349 ...
7350 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7351 <76> DW_AT_byte_size : 4
7352 <77> DW_AT_encoding : 5 (signed)
7353
7354 imports the wrong die ( 0x75 instead of 0x58 ).
7355 This case will be ignored until the gcc bug is fixed. */
7356 return;
7357 }
7358
7359 /* Figure out the local name after import. */
7360 import_alias = dwarf2_name (die, cu);
7361
7362 /* Figure out where the statement is being imported to. */
7363 import_prefix = determine_prefix (die, cu);
7364
7365 /* Figure out what the scope of the imported die is and prepend it
7366 to the name of the imported die. */
7367 imported_name_prefix = determine_prefix (imported_die, imported_cu);
7368
7369 if (imported_die->tag != DW_TAG_namespace
7370 && imported_die->tag != DW_TAG_module)
7371 {
7372 imported_declaration = imported_name;
7373 canonical_name = imported_name_prefix;
7374 }
7375 else if (strlen (imported_name_prefix) > 0)
7376 canonical_name = obconcat (&objfile->objfile_obstack,
7377 imported_name_prefix,
7378 (cu->lang () == language_d
7379 ? "."
7380 : "::"),
7381 imported_name, (char *) NULL);
7382 else
7383 canonical_name = imported_name;
7384
7385 if (die->tag == DW_TAG_imported_module
7386 && cu->lang () == language_fortran)
7387 for (child_die = die->child; child_die && child_die->tag;
7388 child_die = child_die->sibling)
7389 {
7390 /* DWARF-4: A Fortran use statement with a “rename list” may be
7391 represented by an imported module entry with an import attribute
7392 referring to the module and owned entries corresponding to those
7393 entities that are renamed as part of being imported. */
7394
7395 if (child_die->tag != DW_TAG_imported_declaration)
7396 {
7397 complaint (_("child DW_TAG_imported_declaration expected "
7398 "- DIE at %s [in module %s]"),
7399 sect_offset_str (child_die->sect_off),
7400 objfile_name (objfile));
7401 continue;
7402 }
7403
7404 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7405 if (import_attr == NULL)
7406 {
7407 complaint (_("Tag '%s' has no DW_AT_import"),
7408 dwarf_tag_name (child_die->tag));
7409 continue;
7410 }
7411
7412 imported_cu = cu;
7413 imported_die = follow_die_ref_or_sig (child_die, import_attr,
7414 &imported_cu);
7415 imported_name = dwarf2_name (imported_die, imported_cu);
7416 if (imported_name == NULL)
7417 {
7418 complaint (_("child DW_TAG_imported_declaration has unknown "
7419 "imported name - DIE at %s [in module %s]"),
7420 sect_offset_str (child_die->sect_off),
7421 objfile_name (objfile));
7422 continue;
7423 }
7424
7425 excludes.push_back (imported_name);
7426
7427 process_die (child_die, cu);
7428 }
7429
7430 add_using_directive (using_directives (cu),
7431 import_prefix,
7432 canonical_name,
7433 import_alias,
7434 imported_declaration,
7435 excludes,
7436 read_decl_line (die, cu),
7437 0,
7438 &objfile->objfile_obstack);
7439 }
7440
7441 /* ICC<14 does not output the required DW_AT_declaration on incomplete
7442 types, but gives them a size of zero. Starting with version 14,
7443 ICC is compatible with GCC. */
7444
7445 static bool
7446 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
7447 {
7448 if (!cu->checked_producer)
7449 check_producer (cu);
7450
7451 return cu->producer_is_icc_lt_14;
7452 }
7453
7454 /* ICC generates a DW_AT_type for C void functions. This was observed on
7455 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
7456 which says that void functions should not have a DW_AT_type. */
7457
7458 static bool
7459 producer_is_icc (struct dwarf2_cu *cu)
7460 {
7461 if (!cu->checked_producer)
7462 check_producer (cu);
7463
7464 return cu->producer_is_icc;
7465 }
7466
7467 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7468 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7469 this, it was first present in GCC release 4.3.0. */
7470
7471 static bool
7472 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7473 {
7474 if (!cu->checked_producer)
7475 check_producer (cu);
7476
7477 return cu->producer_is_gcc_lt_4_3;
7478 }
7479
7480 /* See dwarf2/read.h. */
7481 bool
7482 producer_is_clang (struct dwarf2_cu *cu)
7483 {
7484 if (!cu->checked_producer)
7485 check_producer (cu);
7486
7487 return cu->producer_is_clang;
7488 }
7489
7490 static file_and_directory &
7491 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
7492 {
7493 if (cu->per_cu->fnd != nullptr)
7494 return *cu->per_cu->fnd;
7495
7496 /* Find the filename. Do not use dwarf2_name here, since the filename
7497 is not a source language identifier. */
7498 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
7499 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
7500
7501 if (res.get_comp_dir () == nullptr
7502 && producer_is_gcc_lt_4_3 (cu)
7503 && res.get_name () != nullptr
7504 && IS_ABSOLUTE_PATH (res.get_name ()))
7505 {
7506 res.set_comp_dir (ldirname (res.get_name ()));
7507 res.set_name (make_unique_xstrdup (lbasename (res.get_name ())));
7508 }
7509
7510 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
7511 return *cu->per_cu->fnd;
7512 }
7513
7514 /* Handle DW_AT_stmt_list for a compilation unit.
7515 DIE is the DW_TAG_compile_unit die for CU.
7516 COMP_DIR is the compilation directory. LOWPC is passed to
7517 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
7518
7519 static void
7520 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7521 const file_and_directory &fnd, CORE_ADDR lowpc,
7522 bool have_code) /* ARI: editCase function */
7523 {
7524 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7525 struct attribute *attr;
7526 hashval_t line_header_local_hash;
7527 void **slot;
7528 int decode_mapping;
7529
7530 gdb_assert (! cu->per_cu->is_debug_types);
7531
7532 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7533 if (attr == NULL || !attr->form_is_unsigned ())
7534 return;
7535
7536 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7537
7538 /* The line header hash table is only created if needed (it exists to
7539 prevent redundant reading of the line table for partial_units).
7540 If we're given a partial_unit, we'll need it. If we're given a
7541 compile_unit, then use the line header hash table if it's already
7542 created, but don't create one just yet. */
7543
7544 if (per_objfile->line_header_hash == NULL
7545 && die->tag == DW_TAG_partial_unit)
7546 {
7547 per_objfile->line_header_hash
7548 .reset (htab_create_alloc (127, line_header_hash_voidp,
7549 line_header_eq_voidp,
7550 htab_delete_entry<line_header>,
7551 xcalloc, xfree));
7552 }
7553
7554 line_header line_header_local (line_offset, cu->per_cu->is_dwz);
7555 line_header_local_hash = line_header_hash (&line_header_local);
7556 if (per_objfile->line_header_hash != NULL)
7557 {
7558 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7559 &line_header_local,
7560 line_header_local_hash, NO_INSERT);
7561
7562 /* For DW_TAG_compile_unit we need info like symtab::linetable which
7563 is not present in *SLOT (since if there is something in *SLOT then
7564 it will be for a partial_unit). */
7565 if (die->tag == DW_TAG_partial_unit && slot != NULL)
7566 {
7567 gdb_assert (*slot != NULL);
7568 cu->line_header = (struct line_header *) *slot;
7569 return;
7570 }
7571 }
7572
7573 /* dwarf_decode_line_header does not yet provide sufficient information.
7574 We always have to call also dwarf_decode_lines for it. */
7575 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
7576 fnd.get_comp_dir ());
7577 if (lh == NULL)
7578 return;
7579
7580 cu->line_header = lh.release ();
7581 cu->line_header_die_owner = die;
7582
7583 if (per_objfile->line_header_hash == NULL)
7584 slot = NULL;
7585 else
7586 {
7587 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
7588 &line_header_local,
7589 line_header_local_hash, INSERT);
7590 gdb_assert (slot != NULL);
7591 }
7592 if (slot != NULL && *slot == NULL)
7593 {
7594 /* This newly decoded line number information unit will be owned
7595 by line_header_hash hash table. */
7596 *slot = cu->line_header;
7597 cu->line_header_die_owner = NULL;
7598 }
7599 else
7600 {
7601 /* We cannot free any current entry in (*slot) as that struct line_header
7602 may be already used by multiple CUs. Create only temporary decoded
7603 line_header for this CU - it may happen at most once for each line
7604 number information unit. And if we're not using line_header_hash
7605 then this is what we want as well. */
7606 gdb_assert (die->tag != DW_TAG_partial_unit);
7607 }
7608 decode_mapping = (die->tag != DW_TAG_partial_unit);
7609 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
7610 then there won't be any interesting code in the CU, but a check later on
7611 (in lnp_state_machine::check_line_address) will fail to properly exclude
7612 an entry that was removed via --gc-sections. */
7613 if (have_code)
7614 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
7615 }
7616
7617 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7618
7619 static void
7620 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7621 {
7622 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7623 struct objfile *objfile = per_objfile->objfile;
7624 struct gdbarch *gdbarch = objfile->arch ();
7625 CORE_ADDR lowpc = ((CORE_ADDR) -1);
7626 CORE_ADDR highpc = ((CORE_ADDR) 0);
7627 struct attribute *attr;
7628 struct die_info *child_die;
7629 CORE_ADDR baseaddr;
7630
7631 prepare_one_comp_unit (cu, die, cu->lang ());
7632 baseaddr = objfile->text_section_offset ();
7633
7634 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7635
7636 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7637 from finish_block. */
7638 if (lowpc == ((CORE_ADDR) -1))
7639 lowpc = highpc;
7640 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
7641
7642 file_and_directory &fnd = find_file_and_directory (die, cu);
7643
7644 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
7645 lowpc);
7646
7647 gdb_assert (per_objfile->sym_cu == nullptr);
7648 scoped_restore restore_sym_cu
7649 = make_scoped_restore (&per_objfile->sym_cu, cu);
7650
7651 /* Decode line number information if present. We do this before
7652 processing child DIEs, so that the line header table is available
7653 for DW_AT_decl_file. */
7654 handle_DW_AT_stmt_list (die, cu, fnd, lowpc, lowpc != highpc);
7655
7656 /* Process all dies in compilation unit. */
7657 if (die->child != NULL)
7658 {
7659 child_die = die->child;
7660 while (child_die && child_die->tag)
7661 {
7662 process_die (child_die, cu);
7663 child_die = child_die->sibling;
7664 }
7665 }
7666 per_objfile->sym_cu = nullptr;
7667
7668 /* Decode macro information, if present. Dwarf 2 macro information
7669 refers to information in the line number info statement program
7670 header, so we can only read it if we've read the header
7671 successfully. */
7672 attr = dwarf2_attr (die, DW_AT_macros, cu);
7673 if (attr == NULL)
7674 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7675 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7676 {
7677 if (dwarf2_attr (die, DW_AT_macro_info, cu))
7678 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
7679
7680 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
7681 }
7682 else
7683 {
7684 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7685 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
7686 {
7687 unsigned int macro_offset = attr->as_unsigned ();
7688
7689 dwarf_decode_macros (cu, macro_offset, 0);
7690 }
7691 }
7692 }
7693
7694 void
7695 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
7696 {
7697 struct type_unit_group *tu_group;
7698 int first_time;
7699 struct attribute *attr;
7700 unsigned int i;
7701 struct signatured_type *sig_type;
7702
7703 gdb_assert (per_cu->is_debug_types);
7704 sig_type = (struct signatured_type *) per_cu;
7705
7706 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
7707
7708 /* If we're using .gdb_index (includes -readnow) then
7709 per_cu->type_unit_group may not have been set up yet. */
7710 if (sig_type->type_unit_group == NULL)
7711 sig_type->type_unit_group = get_type_unit_group (this, attr);
7712 tu_group = sig_type->type_unit_group;
7713
7714 /* If we've already processed this stmt_list there's no real need to
7715 do it again, we could fake it and just recreate the part we need
7716 (file name,index -> symtab mapping). If data shows this optimization
7717 is useful we can do it then. */
7718 type_unit_group_unshareable *tug_unshare
7719 = per_objfile->get_type_unit_group_unshareable (tu_group);
7720 first_time = tug_unshare->compunit_symtab == NULL;
7721
7722 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7723 debug info. */
7724 line_header_up lh;
7725 if (attr != NULL && attr->form_is_unsigned ())
7726 {
7727 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
7728 lh = dwarf_decode_line_header (line_offset, this, nullptr);
7729 }
7730 if (lh == NULL)
7731 {
7732 if (first_time)
7733 start_compunit_symtab ("", NULL, 0);
7734 else
7735 {
7736 gdb_assert (tug_unshare->symtabs == NULL);
7737 gdb_assert (m_builder == nullptr);
7738 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7739 m_builder.reset (new struct buildsym_compunit
7740 (cust->objfile (), "",
7741 cust->dirname (),
7742 cust->language (),
7743 0, cust));
7744 list_in_scope = get_builder ()->get_file_symbols ();
7745 }
7746 return;
7747 }
7748
7749 line_header = lh.release ();
7750 line_header_die_owner = die;
7751
7752 if (first_time)
7753 {
7754 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
7755
7756 /* Note: We don't assign tu_group->compunit_symtab yet because we're
7757 still initializing it, and our caller (a few levels up)
7758 process_full_type_unit still needs to know if this is the first
7759 time. */
7760
7761 tug_unshare->symtabs
7762 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
7763 struct symtab *, line_header->file_names_size ());
7764
7765 auto &file_names = line_header->file_names ();
7766 for (i = 0; i < file_names.size (); ++i)
7767 {
7768 file_entry &fe = file_names[i];
7769 dwarf2_start_subfile (this, fe, *line_header);
7770 buildsym_compunit *b = get_builder ();
7771 subfile *sf = b->get_current_subfile ();
7772
7773 if (sf->symtab == nullptr)
7774 {
7775 /* NOTE: start_subfile will recognize when it's been
7776 passed a file it has already seen. So we can't
7777 assume there's a simple mapping from
7778 cu->line_header->file_names to subfiles, plus
7779 cu->line_header->file_names may contain dups. */
7780 const char *name = sf->name.c_str ();
7781 const char *name_for_id = sf->name_for_id.c_str ();
7782 sf->symtab = allocate_symtab (cust, name, name_for_id);
7783 }
7784
7785 fe.symtab = b->get_current_subfile ()->symtab;
7786 tug_unshare->symtabs[i] = fe.symtab;
7787 }
7788 }
7789 else
7790 {
7791 gdb_assert (m_builder == nullptr);
7792 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
7793 m_builder.reset (new struct buildsym_compunit
7794 (cust->objfile (), "",
7795 cust->dirname (),
7796 cust->language (),
7797 0, cust));
7798 list_in_scope = get_builder ()->get_file_symbols ();
7799
7800 auto &file_names = line_header->file_names ();
7801 for (i = 0; i < file_names.size (); ++i)
7802 {
7803 file_entry &fe = file_names[i];
7804 fe.symtab = tug_unshare->symtabs[i];
7805 }
7806 }
7807
7808 /* The main symtab is allocated last. Type units don't have DW_AT_name
7809 so they don't have a "real" (so to speak) symtab anyway.
7810 There is later code that will assign the main symtab to all symbols
7811 that don't have one. We need to handle the case of a symbol with a
7812 missing symtab (DW_AT_decl_file) anyway. */
7813 }
7814
7815 /* Process DW_TAG_type_unit.
7816 For TUs we want to skip the first top level sibling if it's not the
7817 actual type being defined by this TU. In this case the first top
7818 level sibling is there to provide context only. */
7819
7820 static void
7821 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7822 {
7823 struct die_info *child_die;
7824
7825 prepare_one_comp_unit (cu, die, language_minimal);
7826
7827 /* Initialize (or reinitialize) the machinery for building symtabs.
7828 We do this before processing child DIEs, so that the line header table
7829 is available for DW_AT_decl_file. */
7830 cu->setup_type_unit_groups (die);
7831
7832 if (die->child != NULL)
7833 {
7834 child_die = die->child;
7835 while (child_die && child_die->tag)
7836 {
7837 process_die (child_die, cu);
7838 child_die = child_die->sibling;
7839 }
7840 }
7841 }
7842 \f
7843 /* DWO/DWP files.
7844
7845 http://gcc.gnu.org/wiki/DebugFission
7846 http://gcc.gnu.org/wiki/DebugFissionDWP
7847
7848 To simplify handling of both DWO files ("object" files with the DWARF info)
7849 and DWP files (a file with the DWOs packaged up into one file), we treat
7850 DWP files as having a collection of virtual DWO files. */
7851
7852 static hashval_t
7853 hash_dwo_file (const void *item)
7854 {
7855 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
7856 hashval_t hash;
7857
7858 hash = htab_hash_string (dwo_file->dwo_name);
7859 if (dwo_file->comp_dir != NULL)
7860 hash += htab_hash_string (dwo_file->comp_dir);
7861 return hash;
7862 }
7863
7864 static int
7865 eq_dwo_file (const void *item_lhs, const void *item_rhs)
7866 {
7867 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
7868 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
7869
7870 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
7871 return 0;
7872 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
7873 return lhs->comp_dir == rhs->comp_dir;
7874 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
7875 }
7876
7877 /* Allocate a hash table for DWO files. */
7878
7879 static htab_up
7880 allocate_dwo_file_hash_table ()
7881 {
7882 return htab_up (htab_create_alloc (41,
7883 hash_dwo_file,
7884 eq_dwo_file,
7885 htab_delete_entry<dwo_file>,
7886 xcalloc, xfree));
7887 }
7888
7889 /* Lookup DWO file DWO_NAME. */
7890
7891 static void **
7892 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
7893 const char *dwo_name,
7894 const char *comp_dir)
7895 {
7896 struct dwo_file find_entry;
7897 void **slot;
7898
7899 if (per_objfile->per_bfd->dwo_files == NULL)
7900 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
7901
7902 find_entry.dwo_name = dwo_name;
7903 find_entry.comp_dir = comp_dir;
7904 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
7905 INSERT);
7906
7907 return slot;
7908 }
7909
7910 static hashval_t
7911 hash_dwo_unit (const void *item)
7912 {
7913 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
7914
7915 /* This drops the top 32 bits of the id, but is ok for a hash. */
7916 return dwo_unit->signature;
7917 }
7918
7919 static int
7920 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7921 {
7922 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
7923 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
7924
7925 /* The signature is assumed to be unique within the DWO file.
7926 So while object file CU dwo_id's always have the value zero,
7927 that's OK, assuming each object file DWO file has only one CU,
7928 and that's the rule for now. */
7929 return lhs->signature == rhs->signature;
7930 }
7931
7932 /* Allocate a hash table for DWO CUs,TUs.
7933 There is one of these tables for each of CUs,TUs for each DWO file. */
7934
7935 static htab_up
7936 allocate_dwo_unit_table ()
7937 {
7938 /* Start out with a pretty small number.
7939 Generally DWO files contain only one CU and maybe some TUs. */
7940 return htab_up (htab_create_alloc (3,
7941 hash_dwo_unit,
7942 eq_dwo_unit,
7943 NULL, xcalloc, xfree));
7944 }
7945
7946 /* die_reader_func for create_dwo_cu. */
7947
7948 static void
7949 create_dwo_cu_reader (const struct die_reader_specs *reader,
7950 const gdb_byte *info_ptr,
7951 struct die_info *comp_unit_die,
7952 struct dwo_file *dwo_file,
7953 struct dwo_unit *dwo_unit)
7954 {
7955 struct dwarf2_cu *cu = reader->cu;
7956 sect_offset sect_off = cu->per_cu->sect_off;
7957 struct dwarf2_section_info *section = cu->per_cu->section;
7958
7959 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7960 if (!signature.has_value ())
7961 {
7962 complaint (_("Dwarf Error: debug entry at offset %s is missing"
7963 " its dwo_id [in module %s]"),
7964 sect_offset_str (sect_off), dwo_file->dwo_name);
7965 return;
7966 }
7967
7968 dwo_unit->dwo_file = dwo_file;
7969 dwo_unit->signature = *signature;
7970 dwo_unit->section = section;
7971 dwo_unit->sect_off = sect_off;
7972 dwo_unit->length = cu->per_cu->length ();
7973
7974 dwarf_read_debug_printf (" offset %s, dwo_id %s",
7975 sect_offset_str (sect_off),
7976 hex_string (dwo_unit->signature));
7977 }
7978
7979 /* Create the dwo_units for the CUs in a DWO_FILE.
7980 Note: This function processes DWO files only, not DWP files. */
7981
7982 static void
7983 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
7984 dwarf2_cu *cu, struct dwo_file &dwo_file,
7985 dwarf2_section_info &section, htab_up &cus_htab)
7986 {
7987 struct objfile *objfile = per_objfile->objfile;
7988 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7989 const gdb_byte *info_ptr, *end_ptr;
7990
7991 section.read (objfile);
7992 info_ptr = section.buffer;
7993
7994 if (info_ptr == NULL)
7995 return;
7996
7997 dwarf_read_debug_printf ("Reading %s for %s:",
7998 section.get_name (),
7999 section.get_file_name ());
8000
8001 end_ptr = info_ptr + section.size;
8002 while (info_ptr < end_ptr)
8003 {
8004 struct dwarf2_per_cu_data per_cu;
8005 struct dwo_unit read_unit {};
8006 struct dwo_unit *dwo_unit;
8007 void **slot;
8008 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
8009
8010 per_cu.per_bfd = per_bfd;
8011 per_cu.is_debug_types = 0;
8012 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
8013 per_cu.section = &section;
8014
8015 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
8016 if (!reader.dummy_p)
8017 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8018 &dwo_file, &read_unit);
8019 info_ptr += per_cu.length ();
8020
8021 // If the unit could not be parsed, skip it.
8022 if (read_unit.dwo_file == NULL)
8023 continue;
8024
8025 if (cus_htab == NULL)
8026 cus_htab = allocate_dwo_unit_table ();
8027
8028 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
8029 struct dwo_unit);
8030 *dwo_unit = read_unit;
8031 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
8032 gdb_assert (slot != NULL);
8033 if (*slot != NULL)
8034 {
8035 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
8036 sect_offset dup_sect_off = dup_cu->sect_off;
8037
8038 complaint (_("debug cu entry at offset %s is duplicate to"
8039 " the entry at offset %s, signature %s"),
8040 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
8041 hex_string (dwo_unit->signature));
8042 }
8043 *slot = (void *)dwo_unit;
8044 }
8045 }
8046
8047 /* DWP file .debug_{cu,tu}_index section format:
8048 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8049 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
8050
8051 DWP Versions 1 & 2 are older, pre-standard format versions. The first
8052 officially standard DWP format was published with DWARF v5 and is called
8053 Version 5. There are no versions 3 or 4.
8054
8055 DWP Version 1:
8056
8057 Both index sections have the same format, and serve to map a 64-bit
8058 signature to a set of section numbers. Each section begins with a header,
8059 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8060 indexes, and a pool of 32-bit section numbers. The index sections will be
8061 aligned at 8-byte boundaries in the file.
8062
8063 The index section header consists of:
8064
8065 V, 32 bit version number
8066 -, 32 bits unused
8067 N, 32 bit number of compilation units or type units in the index
8068 M, 32 bit number of slots in the hash table
8069
8070 Numbers are recorded using the byte order of the application binary.
8071
8072 The hash table begins at offset 16 in the section, and consists of an array
8073 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8074 order of the application binary). Unused slots in the hash table are 0.
8075 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8076
8077 The parallel table begins immediately after the hash table
8078 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8079 array of 32-bit indexes (using the byte order of the application binary),
8080 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8081 table contains a 32-bit index into the pool of section numbers. For unused
8082 hash table slots, the corresponding entry in the parallel table will be 0.
8083
8084 The pool of section numbers begins immediately following the hash table
8085 (at offset 16 + 12 * M from the beginning of the section). The pool of
8086 section numbers consists of an array of 32-bit words (using the byte order
8087 of the application binary). Each item in the array is indexed starting
8088 from 0. The hash table entry provides the index of the first section
8089 number in the set. Additional section numbers in the set follow, and the
8090 set is terminated by a 0 entry (section number 0 is not used in ELF).
8091
8092 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8093 section must be the first entry in the set, and the .debug_abbrev.dwo must
8094 be the second entry. Other members of the set may follow in any order.
8095
8096 ---
8097
8098 DWP Versions 2 and 5:
8099
8100 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
8101 and the entries in the index tables are now offsets into these sections.
8102 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
8103 section.
8104
8105 Index Section Contents:
8106 Header
8107 Hash Table of Signatures dwp_hash_table.hash_table
8108 Parallel Table of Indices dwp_hash_table.unit_table
8109 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
8110 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
8111
8112 The index section header consists of:
8113
8114 V, 32 bit version number
8115 L, 32 bit number of columns in the table of section offsets
8116 N, 32 bit number of compilation units or type units in the index
8117 M, 32 bit number of slots in the hash table
8118
8119 Numbers are recorded using the byte order of the application binary.
8120
8121 The hash table has the same format as version 1.
8122 The parallel table of indices has the same format as version 1,
8123 except that the entries are origin-1 indices into the table of sections
8124 offsets and the table of section sizes.
8125
8126 The table of offsets begins immediately following the parallel table
8127 (at offset 16 + 12 * M from the beginning of the section). The table is
8128 a two-dimensional array of 32-bit words (using the byte order of the
8129 application binary), with L columns and N+1 rows, in row-major order.
8130 Each row in the array is indexed starting from 0. The first row provides
8131 a key to the remaining rows: each column in this row provides an identifier
8132 for a debug section, and the offsets in the same column of subsequent rows
8133 refer to that section. The section identifiers for Version 2 are:
8134
8135 DW_SECT_INFO 1 .debug_info.dwo
8136 DW_SECT_TYPES 2 .debug_types.dwo
8137 DW_SECT_ABBREV 3 .debug_abbrev.dwo
8138 DW_SECT_LINE 4 .debug_line.dwo
8139 DW_SECT_LOC 5 .debug_loc.dwo
8140 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
8141 DW_SECT_MACINFO 7 .debug_macinfo.dwo
8142 DW_SECT_MACRO 8 .debug_macro.dwo
8143
8144 The section identifiers for Version 5 are:
8145
8146 DW_SECT_INFO_V5 1 .debug_info.dwo
8147 DW_SECT_RESERVED_V5 2 --
8148 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
8149 DW_SECT_LINE_V5 4 .debug_line.dwo
8150 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
8151 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
8152 DW_SECT_MACRO_V5 7 .debug_macro.dwo
8153 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
8154
8155 The offsets provided by the CU and TU index sections are the base offsets
8156 for the contributions made by each CU or TU to the corresponding section
8157 in the package file. Each CU and TU header contains an abbrev_offset
8158 field, used to find the abbreviations table for that CU or TU within the
8159 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
8160 be interpreted as relative to the base offset given in the index section.
8161 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
8162 should be interpreted as relative to the base offset for .debug_line.dwo,
8163 and offsets into other debug sections obtained from DWARF attributes should
8164 also be interpreted as relative to the corresponding base offset.
8165
8166 The table of sizes begins immediately following the table of offsets.
8167 Like the table of offsets, it is a two-dimensional array of 32-bit words,
8168 with L columns and N rows, in row-major order. Each row in the array is
8169 indexed starting from 1 (row 0 is shared by the two tables).
8170
8171 ---
8172
8173 Hash table lookup is handled the same in version 1 and 2:
8174
8175 We assume that N and M will not exceed 2^32 - 1.
8176 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8177
8178 Given a 64-bit compilation unit signature or a type signature S, an entry
8179 in the hash table is located as follows:
8180
8181 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8182 the low-order k bits all set to 1.
8183
8184 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8185
8186 3) If the hash table entry at index H matches the signature, use that
8187 entry. If the hash table entry at index H is unused (all zeroes),
8188 terminate the search: the signature is not present in the table.
8189
8190 4) Let H = (H + H') modulo M. Repeat at Step 3.
8191
8192 Because M > N and H' and M are relatively prime, the search is guaranteed
8193 to stop at an unused slot or find the match. */
8194
8195 /* Create a hash table to map DWO IDs to their CU/TU entry in
8196 .debug_{info,types}.dwo in DWP_FILE.
8197 Returns NULL if there isn't one.
8198 Note: This function processes DWP files only, not DWO files. */
8199
8200 static struct dwp_hash_table *
8201 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
8202 struct dwp_file *dwp_file, int is_debug_types)
8203 {
8204 struct objfile *objfile = per_objfile->objfile;
8205 bfd *dbfd = dwp_file->dbfd.get ();
8206 const gdb_byte *index_ptr, *index_end;
8207 struct dwarf2_section_info *index;
8208 uint32_t version, nr_columns, nr_units, nr_slots;
8209 struct dwp_hash_table *htab;
8210
8211 if (is_debug_types)
8212 index = &dwp_file->sections.tu_index;
8213 else
8214 index = &dwp_file->sections.cu_index;
8215
8216 if (index->empty ())
8217 return NULL;
8218 index->read (objfile);
8219
8220 index_ptr = index->buffer;
8221 index_end = index_ptr + index->size;
8222
8223 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
8224 For now it's safe to just read 4 bytes (particularly as it's difficult to
8225 tell if you're dealing with Version 5 before you've read the version). */
8226 version = read_4_bytes (dbfd, index_ptr);
8227 index_ptr += 4;
8228 if (version == 2 || version == 5)
8229 nr_columns = read_4_bytes (dbfd, index_ptr);
8230 else
8231 nr_columns = 0;
8232 index_ptr += 4;
8233 nr_units = read_4_bytes (dbfd, index_ptr);
8234 index_ptr += 4;
8235 nr_slots = read_4_bytes (dbfd, index_ptr);
8236 index_ptr += 4;
8237
8238 if (version != 1 && version != 2 && version != 5)
8239 {
8240 error (_("Dwarf Error: unsupported DWP file version (%s)"
8241 " [in module %s]"),
8242 pulongest (version), dwp_file->name);
8243 }
8244 if (nr_slots != (nr_slots & -nr_slots))
8245 {
8246 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
8247 " is not power of 2 [in module %s]"),
8248 pulongest (nr_slots), dwp_file->name);
8249 }
8250
8251 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
8252 htab->version = version;
8253 htab->nr_columns = nr_columns;
8254 htab->nr_units = nr_units;
8255 htab->nr_slots = nr_slots;
8256 htab->hash_table = index_ptr;
8257 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8258
8259 /* Exit early if the table is empty. */
8260 if (nr_slots == 0 || nr_units == 0
8261 || (version == 2 && nr_columns == 0)
8262 || (version == 5 && nr_columns == 0))
8263 {
8264 /* All must be zero. */
8265 if (nr_slots != 0 || nr_units != 0
8266 || (version == 2 && nr_columns != 0)
8267 || (version == 5 && nr_columns != 0))
8268 {
8269 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
8270 " all zero [in modules %s]"),
8271 dwp_file->name);
8272 }
8273 return htab;
8274 }
8275
8276 if (version == 1)
8277 {
8278 htab->section_pool.v1.indices =
8279 htab->unit_table + sizeof (uint32_t) * nr_slots;
8280 /* It's harder to decide whether the section is too small in v1.
8281 V1 is deprecated anyway so we punt. */
8282 }
8283 else if (version == 2)
8284 {
8285 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8286 int *ids = htab->section_pool.v2.section_ids;
8287 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
8288 /* Reverse map for error checking. */
8289 int ids_seen[DW_SECT_MAX + 1];
8290 int i;
8291
8292 if (nr_columns < 2)
8293 {
8294 error (_("Dwarf Error: bad DWP hash table, too few columns"
8295 " in section table [in module %s]"),
8296 dwp_file->name);
8297 }
8298 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
8299 {
8300 error (_("Dwarf Error: bad DWP hash table, too many columns"
8301 " in section table [in module %s]"),
8302 dwp_file->name);
8303 }
8304 memset (ids, 255, sizeof_ids);
8305 memset (ids_seen, 255, sizeof (ids_seen));
8306 for (i = 0; i < nr_columns; ++i)
8307 {
8308 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8309
8310 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
8311 {
8312 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8313 " in section table [in module %s]"),
8314 id, dwp_file->name);
8315 }
8316 if (ids_seen[id] != -1)
8317 {
8318 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8319 " id %d in section table [in module %s]"),
8320 id, dwp_file->name);
8321 }
8322 ids_seen[id] = i;
8323 ids[i] = id;
8324 }
8325 /* Must have exactly one info or types section. */
8326 if (((ids_seen[DW_SECT_INFO] != -1)
8327 + (ids_seen[DW_SECT_TYPES] != -1))
8328 != 1)
8329 {
8330 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8331 " DWO info/types section [in module %s]"),
8332 dwp_file->name);
8333 }
8334 /* Must have an abbrev section. */
8335 if (ids_seen[DW_SECT_ABBREV] == -1)
8336 {
8337 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8338 " section [in module %s]"),
8339 dwp_file->name);
8340 }
8341 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8342 htab->section_pool.v2.sizes =
8343 htab->section_pool.v2.offsets + (sizeof (uint32_t)
8344 * nr_units * nr_columns);
8345 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
8346 * nr_units * nr_columns))
8347 > index_end)
8348 {
8349 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8350 " [in module %s]"),
8351 dwp_file->name);
8352 }
8353 }
8354 else /* version == 5 */
8355 {
8356 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
8357 int *ids = htab->section_pool.v5.section_ids;
8358 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
8359 /* Reverse map for error checking. */
8360 int ids_seen[DW_SECT_MAX_V5 + 1];
8361
8362 if (nr_columns < 2)
8363 {
8364 error (_("Dwarf Error: bad DWP hash table, too few columns"
8365 " in section table [in module %s]"),
8366 dwp_file->name);
8367 }
8368 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
8369 {
8370 error (_("Dwarf Error: bad DWP hash table, too many columns"
8371 " in section table [in module %s]"),
8372 dwp_file->name);
8373 }
8374 memset (ids, 255, sizeof_ids);
8375 memset (ids_seen, 255, sizeof (ids_seen));
8376 for (int i = 0; i < nr_columns; ++i)
8377 {
8378 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
8379
8380 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
8381 {
8382 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8383 " in section table [in module %s]"),
8384 id, dwp_file->name);
8385 }
8386 if (ids_seen[id] != -1)
8387 {
8388 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8389 " id %d in section table [in module %s]"),
8390 id, dwp_file->name);
8391 }
8392 ids_seen[id] = i;
8393 ids[i] = id;
8394 }
8395 /* Must have seen an info section. */
8396 if (ids_seen[DW_SECT_INFO_V5] == -1)
8397 {
8398 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8399 " DWO info/types section [in module %s]"),
8400 dwp_file->name);
8401 }
8402 /* Must have an abbrev section. */
8403 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
8404 {
8405 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8406 " section [in module %s]"),
8407 dwp_file->name);
8408 }
8409 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
8410 htab->section_pool.v5.sizes
8411 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
8412 * nr_units * nr_columns);
8413 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
8414 * nr_units * nr_columns))
8415 > index_end)
8416 {
8417 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8418 " [in module %s]"),
8419 dwp_file->name);
8420 }
8421 }
8422
8423 return htab;
8424 }
8425
8426 /* Update SECTIONS with the data from SECTP.
8427
8428 This function is like the other "locate" section routines, but in
8429 this context the sections to read comes from the DWP V1 hash table,
8430 not the full ELF section table.
8431
8432 The result is non-zero for success, or zero if an error was found. */
8433
8434 static int
8435 locate_v1_virtual_dwo_sections (asection *sectp,
8436 struct virtual_v1_dwo_sections *sections)
8437 {
8438 const struct dwop_section_names *names = &dwop_section_names;
8439
8440 if (names->abbrev_dwo.matches (sectp->name))
8441 {
8442 /* There can be only one. */
8443 if (sections->abbrev.s.section != NULL)
8444 return 0;
8445 sections->abbrev.s.section = sectp;
8446 sections->abbrev.size = bfd_section_size (sectp);
8447 }
8448 else if (names->info_dwo.matches (sectp->name)
8449 || names->types_dwo.matches (sectp->name))
8450 {
8451 /* There can be only one. */
8452 if (sections->info_or_types.s.section != NULL)
8453 return 0;
8454 sections->info_or_types.s.section = sectp;
8455 sections->info_or_types.size = bfd_section_size (sectp);
8456 }
8457 else if (names->line_dwo.matches (sectp->name))
8458 {
8459 /* There can be only one. */
8460 if (sections->line.s.section != NULL)
8461 return 0;
8462 sections->line.s.section = sectp;
8463 sections->line.size = bfd_section_size (sectp);
8464 }
8465 else if (names->loc_dwo.matches (sectp->name))
8466 {
8467 /* There can be only one. */
8468 if (sections->loc.s.section != NULL)
8469 return 0;
8470 sections->loc.s.section = sectp;
8471 sections->loc.size = bfd_section_size (sectp);
8472 }
8473 else if (names->macinfo_dwo.matches (sectp->name))
8474 {
8475 /* There can be only one. */
8476 if (sections->macinfo.s.section != NULL)
8477 return 0;
8478 sections->macinfo.s.section = sectp;
8479 sections->macinfo.size = bfd_section_size (sectp);
8480 }
8481 else if (names->macro_dwo.matches (sectp->name))
8482 {
8483 /* There can be only one. */
8484 if (sections->macro.s.section != NULL)
8485 return 0;
8486 sections->macro.s.section = sectp;
8487 sections->macro.size = bfd_section_size (sectp);
8488 }
8489 else if (names->str_offsets_dwo.matches (sectp->name))
8490 {
8491 /* There can be only one. */
8492 if (sections->str_offsets.s.section != NULL)
8493 return 0;
8494 sections->str_offsets.s.section = sectp;
8495 sections->str_offsets.size = bfd_section_size (sectp);
8496 }
8497 else
8498 {
8499 /* No other kind of section is valid. */
8500 return 0;
8501 }
8502
8503 return 1;
8504 }
8505
8506 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8507 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8508 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8509 This is for DWP version 1 files. */
8510
8511 static struct dwo_unit *
8512 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
8513 struct dwp_file *dwp_file,
8514 uint32_t unit_index,
8515 const char *comp_dir,
8516 ULONGEST signature, int is_debug_types)
8517 {
8518 const struct dwp_hash_table *dwp_htab =
8519 is_debug_types ? dwp_file->tus : dwp_file->cus;
8520 bfd *dbfd = dwp_file->dbfd.get ();
8521 const char *kind = is_debug_types ? "TU" : "CU";
8522 struct dwo_file *dwo_file;
8523 struct dwo_unit *dwo_unit;
8524 struct virtual_v1_dwo_sections sections;
8525 void **dwo_file_slot;
8526 int i;
8527
8528 gdb_assert (dwp_file->version == 1);
8529
8530 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
8531 kind, pulongest (unit_index), hex_string (signature),
8532 dwp_file->name);
8533
8534 /* Fetch the sections of this DWO unit.
8535 Put a limit on the number of sections we look for so that bad data
8536 doesn't cause us to loop forever. */
8537
8538 #define MAX_NR_V1_DWO_SECTIONS \
8539 (1 /* .debug_info or .debug_types */ \
8540 + 1 /* .debug_abbrev */ \
8541 + 1 /* .debug_line */ \
8542 + 1 /* .debug_loc */ \
8543 + 1 /* .debug_str_offsets */ \
8544 + 1 /* .debug_macro or .debug_macinfo */ \
8545 + 1 /* trailing zero */)
8546
8547 memset (&sections, 0, sizeof (sections));
8548
8549 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
8550 {
8551 asection *sectp;
8552 uint32_t section_nr =
8553 read_4_bytes (dbfd,
8554 dwp_htab->section_pool.v1.indices
8555 + (unit_index + i) * sizeof (uint32_t));
8556
8557 if (section_nr == 0)
8558 break;
8559 if (section_nr >= dwp_file->num_sections)
8560 {
8561 error (_("Dwarf Error: bad DWP hash table, section number too large"
8562 " [in module %s]"),
8563 dwp_file->name);
8564 }
8565
8566 sectp = dwp_file->elf_sections[section_nr];
8567 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
8568 {
8569 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8570 " [in module %s]"),
8571 dwp_file->name);
8572 }
8573 }
8574
8575 if (i < 2
8576 || sections.info_or_types.empty ()
8577 || sections.abbrev.empty ())
8578 {
8579 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8580 " [in module %s]"),
8581 dwp_file->name);
8582 }
8583 if (i == MAX_NR_V1_DWO_SECTIONS)
8584 {
8585 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8586 " [in module %s]"),
8587 dwp_file->name);
8588 }
8589
8590 /* It's easier for the rest of the code if we fake a struct dwo_file and
8591 have dwo_unit "live" in that. At least for now.
8592
8593 The DWP file can be made up of a random collection of CUs and TUs.
8594 However, for each CU + set of TUs that came from the same original DWO
8595 file, we can combine them back into a virtual DWO file to save space
8596 (fewer struct dwo_file objects to allocate). Remember that for really
8597 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8598
8599 std::string virtual_dwo_name =
8600 string_printf ("virtual-dwo/%d-%d-%d-%d",
8601 sections.abbrev.get_id (),
8602 sections.line.get_id (),
8603 sections.loc.get_id (),
8604 sections.str_offsets.get_id ());
8605 /* Can we use an existing virtual DWO file? */
8606 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8607 comp_dir);
8608 /* Create one if necessary. */
8609 if (*dwo_file_slot == NULL)
8610 {
8611 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8612 virtual_dwo_name.c_str ());
8613
8614 dwo_file = new struct dwo_file;
8615 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
8616 dwo_file->comp_dir = comp_dir;
8617 dwo_file->sections.abbrev = sections.abbrev;
8618 dwo_file->sections.line = sections.line;
8619 dwo_file->sections.loc = sections.loc;
8620 dwo_file->sections.macinfo = sections.macinfo;
8621 dwo_file->sections.macro = sections.macro;
8622 dwo_file->sections.str_offsets = sections.str_offsets;
8623 /* The "str" section is global to the entire DWP file. */
8624 dwo_file->sections.str = dwp_file->sections.str;
8625 /* The info or types section is assigned below to dwo_unit,
8626 there's no need to record it in dwo_file.
8627 Also, we can't simply record type sections in dwo_file because
8628 we record a pointer into the vector in dwo_unit. As we collect more
8629 types we'll grow the vector and eventually have to reallocate space
8630 for it, invalidating all copies of pointers into the previous
8631 contents. */
8632 *dwo_file_slot = dwo_file;
8633 }
8634 else
8635 {
8636 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8637 virtual_dwo_name.c_str ());
8638
8639 dwo_file = (struct dwo_file *) *dwo_file_slot;
8640 }
8641
8642 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8643 dwo_unit->dwo_file = dwo_file;
8644 dwo_unit->signature = signature;
8645 dwo_unit->section =
8646 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8647 *dwo_unit->section = sections.info_or_types;
8648 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8649
8650 return dwo_unit;
8651 }
8652
8653 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
8654 simplify them. Given a pointer to the containing section SECTION, and
8655 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
8656 virtual section of just that piece. */
8657
8658 static struct dwarf2_section_info
8659 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
8660 struct dwarf2_section_info *section,
8661 bfd_size_type offset, bfd_size_type size)
8662 {
8663 struct dwarf2_section_info result;
8664 asection *sectp;
8665
8666 gdb_assert (section != NULL);
8667 gdb_assert (!section->is_virtual);
8668
8669 memset (&result, 0, sizeof (result));
8670 result.s.containing_section = section;
8671 result.is_virtual = true;
8672
8673 if (size == 0)
8674 return result;
8675
8676 sectp = section->get_bfd_section ();
8677
8678 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
8679 bounds of the real section. This is a pretty-rare event, so just
8680 flag an error (easier) instead of a warning and trying to cope. */
8681 if (sectp == NULL
8682 || offset + size > bfd_section_size (sectp))
8683 {
8684 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
8685 " in section %s [in module %s]"),
8686 sectp ? bfd_section_name (sectp) : "<unknown>",
8687 objfile_name (per_objfile->objfile));
8688 }
8689
8690 result.virtual_offset = offset;
8691 result.size = size;
8692 return result;
8693 }
8694
8695 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8696 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8697 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8698 This is for DWP version 2 files. */
8699
8700 static struct dwo_unit *
8701 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
8702 struct dwp_file *dwp_file,
8703 uint32_t unit_index,
8704 const char *comp_dir,
8705 ULONGEST signature, int is_debug_types)
8706 {
8707 const struct dwp_hash_table *dwp_htab =
8708 is_debug_types ? dwp_file->tus : dwp_file->cus;
8709 bfd *dbfd = dwp_file->dbfd.get ();
8710 const char *kind = is_debug_types ? "TU" : "CU";
8711 struct dwo_file *dwo_file;
8712 struct dwo_unit *dwo_unit;
8713 struct virtual_v2_or_v5_dwo_sections sections;
8714 void **dwo_file_slot;
8715 int i;
8716
8717 gdb_assert (dwp_file->version == 2);
8718
8719 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
8720 kind, pulongest (unit_index), hex_string (signature),
8721 dwp_file->name);
8722
8723 /* Fetch the section offsets of this DWO unit. */
8724
8725 memset (&sections, 0, sizeof (sections));
8726
8727 for (i = 0; i < dwp_htab->nr_columns; ++i)
8728 {
8729 uint32_t offset = read_4_bytes (dbfd,
8730 dwp_htab->section_pool.v2.offsets
8731 + (((unit_index - 1) * dwp_htab->nr_columns
8732 + i)
8733 * sizeof (uint32_t)));
8734 uint32_t size = read_4_bytes (dbfd,
8735 dwp_htab->section_pool.v2.sizes
8736 + (((unit_index - 1) * dwp_htab->nr_columns
8737 + i)
8738 * sizeof (uint32_t)));
8739
8740 switch (dwp_htab->section_pool.v2.section_ids[i])
8741 {
8742 case DW_SECT_INFO:
8743 case DW_SECT_TYPES:
8744 sections.info_or_types_offset = offset;
8745 sections.info_or_types_size = size;
8746 break;
8747 case DW_SECT_ABBREV:
8748 sections.abbrev_offset = offset;
8749 sections.abbrev_size = size;
8750 break;
8751 case DW_SECT_LINE:
8752 sections.line_offset = offset;
8753 sections.line_size = size;
8754 break;
8755 case DW_SECT_LOC:
8756 sections.loc_offset = offset;
8757 sections.loc_size = size;
8758 break;
8759 case DW_SECT_STR_OFFSETS:
8760 sections.str_offsets_offset = offset;
8761 sections.str_offsets_size = size;
8762 break;
8763 case DW_SECT_MACINFO:
8764 sections.macinfo_offset = offset;
8765 sections.macinfo_size = size;
8766 break;
8767 case DW_SECT_MACRO:
8768 sections.macro_offset = offset;
8769 sections.macro_size = size;
8770 break;
8771 }
8772 }
8773
8774 /* It's easier for the rest of the code if we fake a struct dwo_file and
8775 have dwo_unit "live" in that. At least for now.
8776
8777 The DWP file can be made up of a random collection of CUs and TUs.
8778 However, for each CU + set of TUs that came from the same original DWO
8779 file, we can combine them back into a virtual DWO file to save space
8780 (fewer struct dwo_file objects to allocate). Remember that for really
8781 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8782
8783 std::string virtual_dwo_name =
8784 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
8785 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8786 (long) (sections.line_size ? sections.line_offset : 0),
8787 (long) (sections.loc_size ? sections.loc_offset : 0),
8788 (long) (sections.str_offsets_size
8789 ? sections.str_offsets_offset : 0));
8790 /* Can we use an existing virtual DWO file? */
8791 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
8792 comp_dir);
8793 /* Create one if necessary. */
8794 if (*dwo_file_slot == NULL)
8795 {
8796 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8797 virtual_dwo_name.c_str ());
8798
8799 dwo_file = new struct dwo_file;
8800 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
8801 dwo_file->comp_dir = comp_dir;
8802 dwo_file->sections.abbrev =
8803 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
8804 sections.abbrev_offset,
8805 sections.abbrev_size);
8806 dwo_file->sections.line =
8807 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
8808 sections.line_offset,
8809 sections.line_size);
8810 dwo_file->sections.loc =
8811 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
8812 sections.loc_offset, sections.loc_size);
8813 dwo_file->sections.macinfo =
8814 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
8815 sections.macinfo_offset,
8816 sections.macinfo_size);
8817 dwo_file->sections.macro =
8818 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
8819 sections.macro_offset,
8820 sections.macro_size);
8821 dwo_file->sections.str_offsets =
8822 create_dwp_v2_or_v5_section (per_objfile,
8823 &dwp_file->sections.str_offsets,
8824 sections.str_offsets_offset,
8825 sections.str_offsets_size);
8826 /* The "str" section is global to the entire DWP file. */
8827 dwo_file->sections.str = dwp_file->sections.str;
8828 /* The info or types section is assigned below to dwo_unit,
8829 there's no need to record it in dwo_file.
8830 Also, we can't simply record type sections in dwo_file because
8831 we record a pointer into the vector in dwo_unit. As we collect more
8832 types we'll grow the vector and eventually have to reallocate space
8833 for it, invalidating all copies of pointers into the previous
8834 contents. */
8835 *dwo_file_slot = dwo_file;
8836 }
8837 else
8838 {
8839 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8840 virtual_dwo_name.c_str ());
8841
8842 dwo_file = (struct dwo_file *) *dwo_file_slot;
8843 }
8844
8845 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8846 dwo_unit->dwo_file = dwo_file;
8847 dwo_unit->signature = signature;
8848 dwo_unit->section =
8849 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8850 *dwo_unit->section = create_dwp_v2_or_v5_section
8851 (per_objfile,
8852 is_debug_types
8853 ? &dwp_file->sections.types
8854 : &dwp_file->sections.info,
8855 sections.info_or_types_offset,
8856 sections.info_or_types_size);
8857 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8858
8859 return dwo_unit;
8860 }
8861
8862 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8863 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8864 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8865 This is for DWP version 5 files. */
8866
8867 static struct dwo_unit *
8868 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
8869 struct dwp_file *dwp_file,
8870 uint32_t unit_index,
8871 const char *comp_dir,
8872 ULONGEST signature, int is_debug_types)
8873 {
8874 const struct dwp_hash_table *dwp_htab
8875 = is_debug_types ? dwp_file->tus : dwp_file->cus;
8876 bfd *dbfd = dwp_file->dbfd.get ();
8877 const char *kind = is_debug_types ? "TU" : "CU";
8878 struct dwo_file *dwo_file;
8879 struct dwo_unit *dwo_unit;
8880 struct virtual_v2_or_v5_dwo_sections sections {};
8881 void **dwo_file_slot;
8882
8883 gdb_assert (dwp_file->version == 5);
8884
8885 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
8886 kind, pulongest (unit_index), hex_string (signature),
8887 dwp_file->name);
8888
8889 /* Fetch the section offsets of this DWO unit. */
8890
8891 /* memset (&sections, 0, sizeof (sections)); */
8892
8893 for (int i = 0; i < dwp_htab->nr_columns; ++i)
8894 {
8895 uint32_t offset = read_4_bytes (dbfd,
8896 dwp_htab->section_pool.v5.offsets
8897 + (((unit_index - 1)
8898 * dwp_htab->nr_columns
8899 + i)
8900 * sizeof (uint32_t)));
8901 uint32_t size = read_4_bytes (dbfd,
8902 dwp_htab->section_pool.v5.sizes
8903 + (((unit_index - 1) * dwp_htab->nr_columns
8904 + i)
8905 * sizeof (uint32_t)));
8906
8907 switch (dwp_htab->section_pool.v5.section_ids[i])
8908 {
8909 case DW_SECT_ABBREV_V5:
8910 sections.abbrev_offset = offset;
8911 sections.abbrev_size = size;
8912 break;
8913 case DW_SECT_INFO_V5:
8914 sections.info_or_types_offset = offset;
8915 sections.info_or_types_size = size;
8916 break;
8917 case DW_SECT_LINE_V5:
8918 sections.line_offset = offset;
8919 sections.line_size = size;
8920 break;
8921 case DW_SECT_LOCLISTS_V5:
8922 sections.loclists_offset = offset;
8923 sections.loclists_size = size;
8924 break;
8925 case DW_SECT_MACRO_V5:
8926 sections.macro_offset = offset;
8927 sections.macro_size = size;
8928 break;
8929 case DW_SECT_RNGLISTS_V5:
8930 sections.rnglists_offset = offset;
8931 sections.rnglists_size = size;
8932 break;
8933 case DW_SECT_STR_OFFSETS_V5:
8934 sections.str_offsets_offset = offset;
8935 sections.str_offsets_size = size;
8936 break;
8937 case DW_SECT_RESERVED_V5:
8938 default:
8939 break;
8940 }
8941 }
8942
8943 /* It's easier for the rest of the code if we fake a struct dwo_file and
8944 have dwo_unit "live" in that. At least for now.
8945
8946 The DWP file can be made up of a random collection of CUs and TUs.
8947 However, for each CU + set of TUs that came from the same original DWO
8948 file, we can combine them back into a virtual DWO file to save space
8949 (fewer struct dwo_file objects to allocate). Remember that for really
8950 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8951
8952 std::string virtual_dwo_name =
8953 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
8954 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8955 (long) (sections.line_size ? sections.line_offset : 0),
8956 (long) (sections.loclists_size ? sections.loclists_offset : 0),
8957 (long) (sections.str_offsets_size
8958 ? sections.str_offsets_offset : 0),
8959 (long) (sections.macro_size ? sections.macro_offset : 0),
8960 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
8961 /* Can we use an existing virtual DWO file? */
8962 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
8963 virtual_dwo_name.c_str (),
8964 comp_dir);
8965 /* Create one if necessary. */
8966 if (*dwo_file_slot == NULL)
8967 {
8968 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8969 virtual_dwo_name.c_str ());
8970
8971 dwo_file = new struct dwo_file;
8972 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
8973 dwo_file->comp_dir = comp_dir;
8974 dwo_file->sections.abbrev =
8975 create_dwp_v2_or_v5_section (per_objfile,
8976 &dwp_file->sections.abbrev,
8977 sections.abbrev_offset,
8978 sections.abbrev_size);
8979 dwo_file->sections.line =
8980 create_dwp_v2_or_v5_section (per_objfile,
8981 &dwp_file->sections.line,
8982 sections.line_offset, sections.line_size);
8983 dwo_file->sections.macro =
8984 create_dwp_v2_or_v5_section (per_objfile,
8985 &dwp_file->sections.macro,
8986 sections.macro_offset,
8987 sections.macro_size);
8988 dwo_file->sections.loclists =
8989 create_dwp_v2_or_v5_section (per_objfile,
8990 &dwp_file->sections.loclists,
8991 sections.loclists_offset,
8992 sections.loclists_size);
8993 dwo_file->sections.rnglists =
8994 create_dwp_v2_or_v5_section (per_objfile,
8995 &dwp_file->sections.rnglists,
8996 sections.rnglists_offset,
8997 sections.rnglists_size);
8998 dwo_file->sections.str_offsets =
8999 create_dwp_v2_or_v5_section (per_objfile,
9000 &dwp_file->sections.str_offsets,
9001 sections.str_offsets_offset,
9002 sections.str_offsets_size);
9003 /* The "str" section is global to the entire DWP file. */
9004 dwo_file->sections.str = dwp_file->sections.str;
9005 /* The info or types section is assigned below to dwo_unit,
9006 there's no need to record it in dwo_file.
9007 Also, we can't simply record type sections in dwo_file because
9008 we record a pointer into the vector in dwo_unit. As we collect more
9009 types we'll grow the vector and eventually have to reallocate space
9010 for it, invalidating all copies of pointers into the previous
9011 contents. */
9012 *dwo_file_slot = dwo_file;
9013 }
9014 else
9015 {
9016 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
9017 virtual_dwo_name.c_str ());
9018
9019 dwo_file = (struct dwo_file *) *dwo_file_slot;
9020 }
9021
9022 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
9023 dwo_unit->dwo_file = dwo_file;
9024 dwo_unit->signature = signature;
9025 dwo_unit->section
9026 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
9027 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
9028 &dwp_file->sections.info,
9029 sections.info_or_types_offset,
9030 sections.info_or_types_size);
9031 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
9032
9033 return dwo_unit;
9034 }
9035
9036 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
9037 Returns NULL if the signature isn't found. */
9038
9039 static struct dwo_unit *
9040 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
9041 struct dwp_file *dwp_file, const char *comp_dir,
9042 ULONGEST signature, int is_debug_types)
9043 {
9044 const struct dwp_hash_table *dwp_htab =
9045 is_debug_types ? dwp_file->tus : dwp_file->cus;
9046 bfd *dbfd = dwp_file->dbfd.get ();
9047 uint32_t mask = dwp_htab->nr_slots - 1;
9048 uint32_t hash = signature & mask;
9049 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9050 unsigned int i;
9051 void **slot;
9052 struct dwo_unit find_dwo_cu;
9053
9054 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9055 find_dwo_cu.signature = signature;
9056 slot = htab_find_slot (is_debug_types
9057 ? dwp_file->loaded_tus.get ()
9058 : dwp_file->loaded_cus.get (),
9059 &find_dwo_cu, INSERT);
9060
9061 if (*slot != NULL)
9062 return (struct dwo_unit *) *slot;
9063
9064 /* Use a for loop so that we don't loop forever on bad debug info. */
9065 for (i = 0; i < dwp_htab->nr_slots; ++i)
9066 {
9067 ULONGEST signature_in_table;
9068
9069 signature_in_table =
9070 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
9071 if (signature_in_table == signature)
9072 {
9073 uint32_t unit_index =
9074 read_4_bytes (dbfd,
9075 dwp_htab->unit_table + hash * sizeof (uint32_t));
9076
9077 if (dwp_file->version == 1)
9078 {
9079 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
9080 unit_index, comp_dir,
9081 signature, is_debug_types);
9082 }
9083 else if (dwp_file->version == 2)
9084 {
9085 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
9086 unit_index, comp_dir,
9087 signature, is_debug_types);
9088 }
9089 else /* version == 5 */
9090 {
9091 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
9092 unit_index, comp_dir,
9093 signature, is_debug_types);
9094 }
9095 return (struct dwo_unit *) *slot;
9096 }
9097 if (signature_in_table == 0)
9098 return NULL;
9099 hash = (hash + hash2) & mask;
9100 }
9101
9102 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9103 " [in module %s]"),
9104 dwp_file->name);
9105 }
9106
9107 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9108 Open the file specified by FILE_NAME and hand it off to BFD for
9109 preliminary analysis. Return a newly initialized bfd *, which
9110 includes a canonicalized copy of FILE_NAME.
9111 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9112 SEARCH_CWD is true if the current directory is to be searched.
9113 It will be searched before debug-file-directory.
9114 If successful, the file is added to the bfd include table of the
9115 objfile's bfd (see gdb_bfd_record_inclusion).
9116 If unable to find/open the file, return NULL.
9117 NOTE: This function is derived from symfile_bfd_open. */
9118
9119 static gdb_bfd_ref_ptr
9120 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
9121 const char *file_name, int is_dwp, int search_cwd)
9122 {
9123 int desc;
9124 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9125 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9126 to debug_file_directory. */
9127 const char *search_path;
9128 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9129
9130 gdb::unique_xmalloc_ptr<char> search_path_holder;
9131 if (search_cwd)
9132 {
9133 if (!debug_file_directory.empty ())
9134 {
9135 search_path_holder.reset (concat (".", dirname_separator_string,
9136 debug_file_directory.c_str (),
9137 (char *) NULL));
9138 search_path = search_path_holder.get ();
9139 }
9140 else
9141 search_path = ".";
9142 }
9143 else
9144 search_path = debug_file_directory.c_str ();
9145
9146 /* Add the path for the executable binary to the list of search paths. */
9147 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
9148 search_path_holder.reset (concat (objfile_dir.c_str (),
9149 dirname_separator_string,
9150 search_path, nullptr));
9151 search_path = search_path_holder.get ();
9152
9153 openp_flags flags = OPF_RETURN_REALPATH;
9154 if (is_dwp)
9155 flags |= OPF_SEARCH_IN_PATH;
9156
9157 gdb::unique_xmalloc_ptr<char> absolute_name;
9158 desc = openp (search_path, flags, file_name,
9159 O_RDONLY | O_BINARY, &absolute_name);
9160 if (desc < 0)
9161 return NULL;
9162
9163 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
9164 gnutarget, desc));
9165 if (sym_bfd == NULL)
9166 return NULL;
9167 bfd_set_cacheable (sym_bfd.get (), 1);
9168
9169 if (!bfd_check_format (sym_bfd.get (), bfd_object))
9170 return NULL;
9171
9172 /* Success. Record the bfd as having been included by the objfile's bfd.
9173 This is important because things like demangled_names_hash lives in the
9174 objfile's per_bfd space and may have references to things like symbol
9175 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
9176 gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
9177
9178 return sym_bfd;
9179 }
9180
9181 /* Try to open DWO file FILE_NAME.
9182 COMP_DIR is the DW_AT_comp_dir attribute.
9183 The result is the bfd handle of the file.
9184 If there is a problem finding or opening the file, return NULL.
9185 Upon success, the canonicalized path of the file is stored in the bfd,
9186 same as symfile_bfd_open. */
9187
9188 static gdb_bfd_ref_ptr
9189 open_dwo_file (dwarf2_per_objfile *per_objfile,
9190 const char *file_name, const char *comp_dir)
9191 {
9192 if (IS_ABSOLUTE_PATH (file_name))
9193 return try_open_dwop_file (per_objfile, file_name,
9194 0 /*is_dwp*/, 0 /*search_cwd*/);
9195
9196 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9197
9198 if (comp_dir != NULL)
9199 {
9200 std::string path_to_try = path_join (comp_dir, file_name);
9201
9202 /* NOTE: If comp_dir is a relative path, this will also try the
9203 search path, which seems useful. */
9204 gdb_bfd_ref_ptr abfd (try_open_dwop_file
9205 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
9206
9207 if (abfd != NULL)
9208 return abfd;
9209 }
9210
9211 /* That didn't work, try debug-file-directory, which, despite its name,
9212 is a list of paths. */
9213
9214 if (debug_file_directory.empty ())
9215 return NULL;
9216
9217 return try_open_dwop_file (per_objfile, file_name,
9218 0 /*is_dwp*/, 1 /*search_cwd*/);
9219 }
9220
9221 /* This function is mapped across the sections and remembers the offset and
9222 size of each of the DWO debugging sections we are interested in. */
9223
9224 static void
9225 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
9226 asection *sectp, dwo_sections *dwo_sections)
9227 {
9228 const struct dwop_section_names *names = &dwop_section_names;
9229
9230 struct dwarf2_section_info *dw_sect = nullptr;
9231
9232 if (names->abbrev_dwo.matches (sectp->name))
9233 dw_sect = &dwo_sections->abbrev;
9234 else if (names->info_dwo.matches (sectp->name))
9235 dw_sect = &dwo_sections->info;
9236 else if (names->line_dwo.matches (sectp->name))
9237 dw_sect = &dwo_sections->line;
9238 else if (names->loc_dwo.matches (sectp->name))
9239 dw_sect = &dwo_sections->loc;
9240 else if (names->loclists_dwo.matches (sectp->name))
9241 dw_sect = &dwo_sections->loclists;
9242 else if (names->macinfo_dwo.matches (sectp->name))
9243 dw_sect = &dwo_sections->macinfo;
9244 else if (names->macro_dwo.matches (sectp->name))
9245 dw_sect = &dwo_sections->macro;
9246 else if (names->rnglists_dwo.matches (sectp->name))
9247 dw_sect = &dwo_sections->rnglists;
9248 else if (names->str_dwo.matches (sectp->name))
9249 dw_sect = &dwo_sections->str;
9250 else if (names->str_offsets_dwo.matches (sectp->name))
9251 dw_sect = &dwo_sections->str_offsets;
9252 else if (names->types_dwo.matches (sectp->name))
9253 {
9254 struct dwarf2_section_info type_section;
9255
9256 memset (&type_section, 0, sizeof (type_section));
9257 dwo_sections->types.push_back (type_section);
9258 dw_sect = &dwo_sections->types.back ();
9259 }
9260
9261 if (dw_sect != nullptr)
9262 {
9263 dw_sect->s.section = sectp;
9264 dw_sect->size = bfd_section_size (sectp);
9265 dw_sect->read (objfile);
9266 }
9267 }
9268
9269 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9270 by PER_CU. This is for the non-DWP case.
9271 The result is NULL if DWO_NAME can't be found. */
9272
9273 static struct dwo_file *
9274 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
9275 const char *comp_dir)
9276 {
9277 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9278
9279 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
9280 if (dbfd == NULL)
9281 {
9282 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
9283
9284 return NULL;
9285 }
9286
9287 dwo_file_up dwo_file (new struct dwo_file);
9288 dwo_file->dwo_name = dwo_name;
9289 dwo_file->comp_dir = comp_dir;
9290 dwo_file->dbfd = std::move (dbfd);
9291
9292 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
9293 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
9294 sec, &dwo_file->sections);
9295
9296 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
9297 dwo_file->cus);
9298
9299 if (cu->per_cu->version () < 5)
9300 {
9301 create_debug_types_hash_table (per_objfile, dwo_file.get (),
9302 dwo_file->sections.types, dwo_file->tus);
9303 }
9304 else
9305 {
9306 create_debug_type_hash_table (per_objfile, dwo_file.get (),
9307 &dwo_file->sections.info, dwo_file->tus,
9308 rcuh_kind::COMPILE);
9309 }
9310
9311 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
9312
9313 return dwo_file.release ();
9314 }
9315
9316 /* This function is mapped across the sections and remembers the offset and
9317 size of each of the DWP debugging sections common to version 1 and 2 that
9318 we are interested in. */
9319
9320 static void
9321 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
9322 asection *sectp, dwp_file *dwp_file)
9323 {
9324 const struct dwop_section_names *names = &dwop_section_names;
9325 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9326
9327 /* Record the ELF section number for later lookup: this is what the
9328 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9329 gdb_assert (elf_section_nr < dwp_file->num_sections);
9330 dwp_file->elf_sections[elf_section_nr] = sectp;
9331
9332 /* Look for specific sections that we need. */
9333 struct dwarf2_section_info *dw_sect = nullptr;
9334 if (names->str_dwo.matches (sectp->name))
9335 dw_sect = &dwp_file->sections.str;
9336 else if (names->cu_index.matches (sectp->name))
9337 dw_sect = &dwp_file->sections.cu_index;
9338 else if (names->tu_index.matches (sectp->name))
9339 dw_sect = &dwp_file->sections.tu_index;
9340
9341 if (dw_sect != nullptr)
9342 {
9343 dw_sect->s.section = sectp;
9344 dw_sect->size = bfd_section_size (sectp);
9345 dw_sect->read (objfile);
9346 }
9347 }
9348
9349 /* This function is mapped across the sections and remembers the offset and
9350 size of each of the DWP version 2 debugging sections that we are interested
9351 in. This is split into a separate function because we don't know if we
9352 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9353
9354 static void
9355 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
9356 asection *sectp, void *dwp_file_ptr)
9357 {
9358 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9359 const struct dwop_section_names *names = &dwop_section_names;
9360 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9361
9362 /* Record the ELF section number for later lookup: this is what the
9363 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9364 gdb_assert (elf_section_nr < dwp_file->num_sections);
9365 dwp_file->elf_sections[elf_section_nr] = sectp;
9366
9367 /* Look for specific sections that we need. */
9368 struct dwarf2_section_info *dw_sect = nullptr;
9369 if (names->abbrev_dwo.matches (sectp->name))
9370 dw_sect = &dwp_file->sections.abbrev;
9371 else if (names->info_dwo.matches (sectp->name))
9372 dw_sect = &dwp_file->sections.info;
9373 else if (names->line_dwo.matches (sectp->name))
9374 dw_sect = &dwp_file->sections.line;
9375 else if (names->loc_dwo.matches (sectp->name))
9376 dw_sect = &dwp_file->sections.loc;
9377 else if (names->macinfo_dwo.matches (sectp->name))
9378 dw_sect = &dwp_file->sections.macinfo;
9379 else if (names->macro_dwo.matches (sectp->name))
9380 dw_sect = &dwp_file->sections.macro;
9381 else if (names->str_offsets_dwo.matches (sectp->name))
9382 dw_sect = &dwp_file->sections.str_offsets;
9383 else if (names->types_dwo.matches (sectp->name))
9384 dw_sect = &dwp_file->sections.types;
9385
9386 if (dw_sect != nullptr)
9387 {
9388 dw_sect->s.section = sectp;
9389 dw_sect->size = bfd_section_size (sectp);
9390 dw_sect->read (objfile);
9391 }
9392 }
9393
9394 /* This function is mapped across the sections and remembers the offset and
9395 size of each of the DWP version 5 debugging sections that we are interested
9396 in. This is split into a separate function because we don't know if we
9397 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9398
9399 static void
9400 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
9401 asection *sectp, void *dwp_file_ptr)
9402 {
9403 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
9404 const struct dwop_section_names *names = &dwop_section_names;
9405 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9406
9407 /* Record the ELF section number for later lookup: this is what the
9408 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9409 gdb_assert (elf_section_nr < dwp_file->num_sections);
9410 dwp_file->elf_sections[elf_section_nr] = sectp;
9411
9412 /* Look for specific sections that we need. */
9413 struct dwarf2_section_info *dw_sect = nullptr;
9414 if (names->abbrev_dwo.matches (sectp->name))
9415 dw_sect = &dwp_file->sections.abbrev;
9416 else if (names->info_dwo.matches (sectp->name))
9417 dw_sect = &dwp_file->sections.info;
9418 else if (names->line_dwo.matches (sectp->name))
9419 dw_sect = &dwp_file->sections.line;
9420 else if (names->loclists_dwo.matches (sectp->name))
9421 dw_sect = &dwp_file->sections.loclists;
9422 else if (names->macro_dwo.matches (sectp->name))
9423 dw_sect = &dwp_file->sections.macro;
9424 else if (names->rnglists_dwo.matches (sectp->name))
9425 dw_sect = &dwp_file->sections.rnglists;
9426 else if (names->str_offsets_dwo.matches (sectp->name))
9427 dw_sect = &dwp_file->sections.str_offsets;
9428
9429 if (dw_sect != nullptr)
9430 {
9431 dw_sect->s.section = sectp;
9432 dw_sect->size = bfd_section_size (sectp);
9433 dw_sect->read (objfile);
9434 }
9435 }
9436
9437 /* Hash function for dwp_file loaded CUs/TUs. */
9438
9439 static hashval_t
9440 hash_dwp_loaded_cutus (const void *item)
9441 {
9442 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9443
9444 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9445 return dwo_unit->signature;
9446 }
9447
9448 /* Equality function for dwp_file loaded CUs/TUs. */
9449
9450 static int
9451 eq_dwp_loaded_cutus (const void *a, const void *b)
9452 {
9453 const struct dwo_unit *dua = (const struct dwo_unit *) a;
9454 const struct dwo_unit *dub = (const struct dwo_unit *) b;
9455
9456 return dua->signature == dub->signature;
9457 }
9458
9459 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9460
9461 static htab_up
9462 allocate_dwp_loaded_cutus_table ()
9463 {
9464 return htab_up (htab_create_alloc (3,
9465 hash_dwp_loaded_cutus,
9466 eq_dwp_loaded_cutus,
9467 NULL, xcalloc, xfree));
9468 }
9469
9470 /* Try to open DWP file FILE_NAME.
9471 The result is the bfd handle of the file.
9472 If there is a problem finding or opening the file, return NULL.
9473 Upon success, the canonicalized path of the file is stored in the bfd,
9474 same as symfile_bfd_open. */
9475
9476 static gdb_bfd_ref_ptr
9477 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
9478 {
9479 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
9480 1 /*is_dwp*/,
9481 1 /*search_cwd*/));
9482 if (abfd != NULL)
9483 return abfd;
9484
9485 /* Work around upstream bug 15652.
9486 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9487 [Whether that's a "bug" is debatable, but it is getting in our way.]
9488 We have no real idea where the dwp file is, because gdb's realpath-ing
9489 of the executable's path may have discarded the needed info.
9490 [IWBN if the dwp file name was recorded in the executable, akin to
9491 .gnu_debuglink, but that doesn't exist yet.]
9492 Strip the directory from FILE_NAME and search again. */
9493 if (!debug_file_directory.empty ())
9494 {
9495 /* Don't implicitly search the current directory here.
9496 If the user wants to search "." to handle this case,
9497 it must be added to debug-file-directory. */
9498 return try_open_dwop_file (per_objfile, lbasename (file_name),
9499 1 /*is_dwp*/,
9500 0 /*search_cwd*/);
9501 }
9502
9503 return NULL;
9504 }
9505
9506 /* Initialize the use of the DWP file for the current objfile.
9507 By convention the name of the DWP file is ${objfile}.dwp.
9508 The result is NULL if it can't be found. */
9509
9510 static std::unique_ptr<struct dwp_file>
9511 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
9512 {
9513 struct objfile *objfile = per_objfile->objfile;
9514
9515 /* Try to find first .dwp for the binary file before any symbolic links
9516 resolving. */
9517
9518 /* If the objfile is a debug file, find the name of the real binary
9519 file and get the name of dwp file from there. */
9520 std::string dwp_name;
9521 if (objfile->separate_debug_objfile_backlink != NULL)
9522 {
9523 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
9524 const char *backlink_basename = lbasename (backlink->original_name);
9525
9526 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
9527 }
9528 else
9529 dwp_name = objfile->original_name;
9530
9531 dwp_name += ".dwp";
9532
9533 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
9534 if (dbfd == NULL
9535 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
9536 {
9537 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9538 dwp_name = objfile_name (objfile);
9539 dwp_name += ".dwp";
9540 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
9541 }
9542
9543 if (dbfd == NULL)
9544 {
9545 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
9546
9547 return std::unique_ptr<dwp_file> ();
9548 }
9549
9550 const char *name = bfd_get_filename (dbfd.get ());
9551 std::unique_ptr<struct dwp_file> dwp_file
9552 (new struct dwp_file (name, std::move (dbfd)));
9553
9554 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
9555 dwp_file->elf_sections =
9556 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
9557 dwp_file->num_sections, asection *);
9558
9559 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9560 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9561 dwp_file.get ());
9562
9563 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
9564
9565 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
9566
9567 /* The DWP file version is stored in the hash table. Oh well. */
9568 if (dwp_file->cus && dwp_file->tus
9569 && dwp_file->cus->version != dwp_file->tus->version)
9570 {
9571 /* Technically speaking, we should try to limp along, but this is
9572 pretty bizarre. We use pulongest here because that's the established
9573 portability solution (e.g, we cannot use %u for uint32_t). */
9574 error (_("Dwarf Error: DWP file CU version %s doesn't match"
9575 " TU version %s [in DWP file %s]"),
9576 pulongest (dwp_file->cus->version),
9577 pulongest (dwp_file->tus->version), dwp_name.c_str ());
9578 }
9579
9580 if (dwp_file->cus)
9581 dwp_file->version = dwp_file->cus->version;
9582 else if (dwp_file->tus)
9583 dwp_file->version = dwp_file->tus->version;
9584 else
9585 dwp_file->version = 2;
9586
9587 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
9588 {
9589 if (dwp_file->version == 2)
9590 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9591 dwp_file.get ());
9592 else
9593 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
9594 dwp_file.get ());
9595 }
9596
9597 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
9598 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
9599
9600 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
9601 dwarf_read_debug_printf (" %s CUs, %s TUs",
9602 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9603 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9604
9605 return dwp_file;
9606 }
9607
9608 /* Wrapper around open_and_init_dwp_file, only open it once. */
9609
9610 static struct dwp_file *
9611 get_dwp_file (dwarf2_per_objfile *per_objfile)
9612 {
9613 if (!per_objfile->per_bfd->dwp_checked)
9614 {
9615 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
9616 per_objfile->per_bfd->dwp_checked = 1;
9617 }
9618 return per_objfile->per_bfd->dwp_file.get ();
9619 }
9620
9621 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9622 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9623 or in the DWP file for the objfile, referenced by THIS_UNIT.
9624 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9625 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9626
9627 This is called, for example, when wanting to read a variable with a
9628 complex location. Therefore we don't want to do file i/o for every call.
9629 Therefore we don't want to look for a DWO file on every call.
9630 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9631 then we check if we've already seen DWO_NAME, and only THEN do we check
9632 for a DWO file.
9633
9634 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9635 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9636
9637 static struct dwo_unit *
9638 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9639 ULONGEST signature, int is_debug_types)
9640 {
9641 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9642 struct objfile *objfile = per_objfile->objfile;
9643 const char *kind = is_debug_types ? "TU" : "CU";
9644 void **dwo_file_slot;
9645 struct dwo_file *dwo_file;
9646 struct dwp_file *dwp_file;
9647
9648 /* First see if there's a DWP file.
9649 If we have a DWP file but didn't find the DWO inside it, don't
9650 look for the original DWO file. It makes gdb behave differently
9651 depending on whether one is debugging in the build tree. */
9652
9653 dwp_file = get_dwp_file (per_objfile);
9654 if (dwp_file != NULL)
9655 {
9656 const struct dwp_hash_table *dwp_htab =
9657 is_debug_types ? dwp_file->tus : dwp_file->cus;
9658
9659 if (dwp_htab != NULL)
9660 {
9661 struct dwo_unit *dwo_cutu =
9662 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
9663 is_debug_types);
9664
9665 if (dwo_cutu != NULL)
9666 {
9667 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
9668 kind, hex_string (signature),
9669 host_address_to_string (dwo_cutu));
9670
9671 return dwo_cutu;
9672 }
9673 }
9674 }
9675 else
9676 {
9677 /* No DWP file, look for the DWO file. */
9678
9679 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
9680 if (*dwo_file_slot == NULL)
9681 {
9682 /* Read in the file and build a table of the CUs/TUs it contains. */
9683 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
9684 }
9685 /* NOTE: This will be NULL if unable to open the file. */
9686 dwo_file = (struct dwo_file *) *dwo_file_slot;
9687
9688 if (dwo_file != NULL)
9689 {
9690 struct dwo_unit *dwo_cutu = NULL;
9691
9692 if (is_debug_types && dwo_file->tus)
9693 {
9694 struct dwo_unit find_dwo_cutu;
9695
9696 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9697 find_dwo_cutu.signature = signature;
9698 dwo_cutu
9699 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
9700 &find_dwo_cutu);
9701 }
9702 else if (!is_debug_types && dwo_file->cus)
9703 {
9704 struct dwo_unit find_dwo_cutu;
9705
9706 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9707 find_dwo_cutu.signature = signature;
9708 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
9709 &find_dwo_cutu);
9710 }
9711
9712 if (dwo_cutu != NULL)
9713 {
9714 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
9715 kind, dwo_name, hex_string (signature),
9716 host_address_to_string (dwo_cutu));
9717
9718 return dwo_cutu;
9719 }
9720 }
9721 }
9722
9723 /* We didn't find it. This could mean a dwo_id mismatch, or
9724 someone deleted the DWO/DWP file, or the search path isn't set up
9725 correctly to find the file. */
9726
9727 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
9728 kind, dwo_name, hex_string (signature));
9729
9730 /* This is a warning and not a complaint because it can be caused by
9731 pilot error (e.g., user accidentally deleting the DWO). */
9732 {
9733 /* Print the name of the DWP file if we looked there, helps the user
9734 better diagnose the problem. */
9735 std::string dwp_text;
9736
9737 if (dwp_file != NULL)
9738 dwp_text = string_printf (" [in DWP file %s]",
9739 lbasename (dwp_file->name));
9740
9741 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
9742 " [in module %s]"),
9743 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
9744 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
9745 }
9746 return NULL;
9747 }
9748
9749 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9750 See lookup_dwo_cutu_unit for details. */
9751
9752 static struct dwo_unit *
9753 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
9754 ULONGEST signature)
9755 {
9756 gdb_assert (!cu->per_cu->is_debug_types);
9757
9758 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
9759 }
9760
9761 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9762 See lookup_dwo_cutu_unit for details. */
9763
9764 static struct dwo_unit *
9765 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
9766 {
9767 gdb_assert (cu->per_cu->is_debug_types);
9768
9769 signatured_type *sig_type = (signatured_type *) cu->per_cu;
9770
9771 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
9772 }
9773
9774 /* Traversal function for queue_and_load_all_dwo_tus. */
9775
9776 static int
9777 queue_and_load_dwo_tu (void **slot, void *info)
9778 {
9779 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9780 dwarf2_cu *cu = (dwarf2_cu *) info;
9781 ULONGEST signature = dwo_unit->signature;
9782 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
9783
9784 if (sig_type != NULL)
9785 {
9786 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9787 a real dependency of PER_CU on SIG_TYPE. That is detected later
9788 while processing PER_CU. */
9789 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
9790 cu->lang ()))
9791 load_full_type_unit (sig_type, cu->per_objfile);
9792 cu->per_cu->imported_symtabs_push (sig_type);
9793 }
9794
9795 return 1;
9796 }
9797
9798 /* Queue all TUs contained in the DWO of CU to be read in.
9799 The DWO may have the only definition of the type, though it may not be
9800 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9801 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9802
9803 static void
9804 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
9805 {
9806 struct dwo_unit *dwo_unit;
9807 struct dwo_file *dwo_file;
9808
9809 gdb_assert (cu != nullptr);
9810 gdb_assert (!cu->per_cu->is_debug_types);
9811 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
9812
9813 dwo_unit = cu->dwo_unit;
9814 gdb_assert (dwo_unit != NULL);
9815
9816 dwo_file = dwo_unit->dwo_file;
9817 if (dwo_file->tus != NULL)
9818 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
9819 }
9820
9821 /* Read in various DIEs. */
9822
9823 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9824 Inherit only the children of the DW_AT_abstract_origin DIE not being
9825 already referenced by DW_AT_abstract_origin from the children of the
9826 current DIE. */
9827
9828 static void
9829 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9830 {
9831 attribute *attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9832 if (attr == nullptr)
9833 return;
9834
9835 /* Note that following die references may follow to a die in a
9836 different CU. */
9837 dwarf2_cu *origin_cu = cu;
9838
9839 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9840 die_info *origin_die = follow_die_ref (die, attr, &origin_cu);
9841
9842 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9843 symbols in. */
9844 struct pending **origin_previous_list_in_scope = origin_cu->list_in_scope;
9845 origin_cu->list_in_scope = cu->list_in_scope;
9846
9847 if (die->tag != origin_die->tag
9848 && !(die->tag == DW_TAG_inlined_subroutine
9849 && origin_die->tag == DW_TAG_subprogram))
9850 complaint (_("DIE %s and its abstract origin %s have different tags"),
9851 sect_offset_str (die->sect_off),
9852 sect_offset_str (origin_die->sect_off));
9853
9854 /* Find if the concrete and abstract trees are structurally the
9855 same. This is a shallow traversal and it is not bullet-proof;
9856 the compiler can trick the debugger into believing that the trees
9857 are isomorphic, whereas they actually are not. However, the
9858 likelyhood of this happening is pretty low, and a full-fledged
9859 check would be an overkill. */
9860 bool are_isomorphic = true;
9861 die_info *concrete_child = die->child;
9862 die_info *abstract_child = origin_die->child;
9863 while (concrete_child != nullptr || abstract_child != nullptr)
9864 {
9865 if (concrete_child == nullptr
9866 || abstract_child == nullptr
9867 || concrete_child->tag != abstract_child->tag)
9868 {
9869 are_isomorphic = false;
9870 break;
9871 }
9872
9873 concrete_child = concrete_child->sibling;
9874 abstract_child = abstract_child->sibling;
9875 }
9876
9877 /* Walk the origin's children in parallel to the concrete children.
9878 This helps match an origin child in case the debug info misses
9879 DW_AT_abstract_origin attributes. Keep in mind that the abstract
9880 origin tree may not have the same tree structure as the concrete
9881 DIE, though. */
9882 die_info *corresponding_abstract_child
9883 = are_isomorphic ? origin_die->child : nullptr;
9884
9885 std::vector<sect_offset> offsets;
9886
9887 for (die_info *child_die = die->child;
9888 child_die && child_die->tag;
9889 child_die = child_die->sibling)
9890 {
9891 /* We are trying to process concrete instance entries:
9892 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
9893 it's not relevant to our analysis here. i.e. detecting DIEs that are
9894 present in the abstract instance but not referenced in the concrete
9895 one. */
9896 if (child_die->tag == DW_TAG_call_site
9897 || child_die->tag == DW_TAG_GNU_call_site)
9898 {
9899 if (are_isomorphic)
9900 corresponding_abstract_child
9901 = corresponding_abstract_child->sibling;
9902 continue;
9903 }
9904
9905 /* For each CHILD_DIE, find the corresponding child of
9906 ORIGIN_DIE. If there is more than one layer of
9907 DW_AT_abstract_origin, follow them all; there shouldn't be,
9908 but GCC versions at least through 4.4 generate this (GCC PR
9909 40573). */
9910 die_info *child_origin_die = child_die;
9911 dwarf2_cu *child_origin_cu = cu;
9912 while (true)
9913 {
9914 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9915 child_origin_cu);
9916 if (attr == nullptr)
9917 break;
9918
9919 child_origin_die = follow_die_ref (child_origin_die, attr,
9920 &child_origin_cu);
9921 }
9922
9923 /* If missing DW_AT_abstract_origin, try the corresponding child
9924 of the origin. Clang emits such lexical scopes. */
9925 if (child_origin_die == child_die
9926 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
9927 && are_isomorphic
9928 && child_die->tag == DW_TAG_lexical_block)
9929 child_origin_die = corresponding_abstract_child;
9930
9931 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9932 counterpart may exist. */
9933 if (child_origin_die != child_die)
9934 {
9935 if (child_die->tag != child_origin_die->tag
9936 && !(child_die->tag == DW_TAG_inlined_subroutine
9937 && child_origin_die->tag == DW_TAG_subprogram))
9938 complaint (_("Child DIE %s and its abstract origin %s have "
9939 "different tags"),
9940 sect_offset_str (child_die->sect_off),
9941 sect_offset_str (child_origin_die->sect_off));
9942 if (child_origin_die->parent != origin_die)
9943 complaint (_("Child DIE %s and its abstract origin %s have "
9944 "different parents"),
9945 sect_offset_str (child_die->sect_off),
9946 sect_offset_str (child_origin_die->sect_off));
9947 else
9948 offsets.push_back (child_origin_die->sect_off);
9949 }
9950
9951 if (are_isomorphic)
9952 corresponding_abstract_child = corresponding_abstract_child->sibling;
9953 }
9954
9955 if (!offsets.empty ())
9956 {
9957 std::sort (offsets.begin (), offsets.end ());
9958
9959 for (auto offsets_it = offsets.begin () + 1;
9960 offsets_it < offsets.end ();
9961 ++offsets_it)
9962 if (*(offsets_it - 1) == *offsets_it)
9963 complaint (_("Multiple children of DIE %s refer "
9964 "to DIE %s as their abstract origin"),
9965 sect_offset_str (die->sect_off),
9966 sect_offset_str (*offsets_it));
9967 }
9968
9969 auto offsets_it = offsets.begin ();
9970 die_info *origin_child_die = origin_die->child;
9971 while (origin_child_die != nullptr && origin_child_die->tag != 0)
9972 {
9973 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9974 while (offsets_it < offsets.end ()
9975 && *offsets_it < origin_child_die->sect_off)
9976 ++offsets_it;
9977
9978 if (offsets_it == offsets.end ()
9979 || *offsets_it > origin_child_die->sect_off)
9980 {
9981 /* Found that ORIGIN_CHILD_DIE is really not referenced.
9982 Check whether we're already processing ORIGIN_CHILD_DIE.
9983 This can happen with mutually referenced abstract_origins.
9984 PR 16581. */
9985 if (!origin_child_die->in_process)
9986 process_die (origin_child_die, origin_cu);
9987 }
9988
9989 origin_child_die = origin_child_die->sibling;
9990 }
9991
9992 origin_cu->list_in_scope = origin_previous_list_in_scope;
9993
9994 if (cu != origin_cu)
9995 compute_delayed_physnames (origin_cu);
9996 }
9997
9998 static void
9999 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
10000 {
10001 struct objfile *objfile = cu->per_objfile->objfile;
10002 struct gdbarch *gdbarch = objfile->arch ();
10003 struct context_stack *newobj;
10004 CORE_ADDR lowpc;
10005 CORE_ADDR highpc;
10006 struct die_info *child_die;
10007 struct attribute *attr, *call_line, *call_file;
10008 const char *name;
10009 CORE_ADDR baseaddr;
10010 struct block *block;
10011 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10012 std::vector<struct symbol *> template_args;
10013 struct template_symbol *templ_func = NULL;
10014
10015 if (inlined_func)
10016 {
10017 /* If we do not have call site information, we can't show the
10018 caller of this inlined function. That's too confusing, so
10019 only use the scope for local variables. */
10020 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10021 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10022 if (call_line == NULL || call_file == NULL)
10023 {
10024 read_lexical_block_scope (die, cu);
10025 return;
10026 }
10027 }
10028
10029 baseaddr = objfile->text_section_offset ();
10030
10031 name = dwarf2_name (die, cu);
10032 if (name == nullptr)
10033 name = dw2_linkage_name (die, cu);
10034
10035 /* Ignore functions with missing or empty names. These are actually
10036 illegal according to the DWARF standard. */
10037 if (name == NULL)
10038 {
10039 complaint (_("missing name for subprogram DIE at %s"),
10040 sect_offset_str (die->sect_off));
10041 return;
10042 }
10043
10044 /* Ignore functions with missing or invalid low and high pc attributes. */
10045 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
10046 <= PC_BOUNDS_INVALID)
10047 {
10048 if (have_complaint ())
10049 {
10050 attr = dwarf2_attr (die, DW_AT_external, cu);
10051 bool external_p = attr != nullptr && attr->as_boolean ();
10052 attr = dwarf2_attr (die, DW_AT_inline, cu);
10053 bool inlined_p
10054 = (attr != nullptr
10055 && attr->is_nonnegative ()
10056 && (attr->as_nonnegative () == DW_INL_inlined
10057 || attr->as_nonnegative () == DW_INL_declared_inlined));
10058 attr = dwarf2_attr (die, DW_AT_declaration, cu);
10059 bool decl_p = attr != nullptr && attr->as_boolean ();
10060 if (!external_p && !inlined_p && !decl_p)
10061 complaint (_("cannot get low and high bounds "
10062 "for subprogram DIE at %s"),
10063 sect_offset_str (die->sect_off));
10064 }
10065 return;
10066 }
10067
10068 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10069 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10070
10071 /* If we have any template arguments, then we must allocate a
10072 different sort of symbol. */
10073 for (child_die = die->child; child_die; child_die = child_die->sibling)
10074 {
10075 if (child_die->tag == DW_TAG_template_type_param
10076 || child_die->tag == DW_TAG_template_value_param)
10077 {
10078 templ_func = new (&objfile->objfile_obstack) template_symbol;
10079 templ_func->subclass = SYMBOL_TEMPLATE;
10080 break;
10081 }
10082 }
10083
10084 gdb_assert (cu->get_builder () != nullptr);
10085 newobj = cu->get_builder ()->push_context (0, lowpc);
10086 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
10087 (struct symbol *) templ_func);
10088
10089 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
10090 set_objfile_main_name (objfile, newobj->name->linkage_name (),
10091 cu->lang ());
10092
10093 /* If there is a location expression for DW_AT_frame_base, record
10094 it. */
10095 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10096 if (attr != nullptr)
10097 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
10098
10099 /* If there is a location for the static link, record it. */
10100 newobj->static_link = NULL;
10101 attr = dwarf2_attr (die, DW_AT_static_link, cu);
10102 if (attr != nullptr)
10103 {
10104 newobj->static_link
10105 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
10106 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
10107 cu->addr_type ());
10108 }
10109
10110 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
10111
10112 if (die->child != NULL)
10113 {
10114 child_die = die->child;
10115 while (child_die && child_die->tag)
10116 {
10117 if (child_die->tag == DW_TAG_template_type_param
10118 || child_die->tag == DW_TAG_template_value_param)
10119 {
10120 struct symbol *arg = new_symbol (child_die, NULL, cu);
10121
10122 if (arg != NULL)
10123 template_args.push_back (arg);
10124 }
10125 else
10126 process_die (child_die, cu);
10127 child_die = child_die->sibling;
10128 }
10129 }
10130
10131 inherit_abstract_dies (die, cu);
10132
10133 /* If we have a DW_AT_specification, we might need to import using
10134 directives from the context of the specification DIE. See the
10135 comment in determine_prefix. */
10136 if (cu->lang () == language_cplus
10137 && dwarf2_attr (die, DW_AT_specification, cu))
10138 {
10139 struct dwarf2_cu *spec_cu = cu;
10140 struct die_info *spec_die = die_specification (die, &spec_cu);
10141
10142 while (spec_die)
10143 {
10144 child_die = spec_die->child;
10145 while (child_die && child_die->tag)
10146 {
10147 if (child_die->tag == DW_TAG_imported_module)
10148 process_die (child_die, spec_cu);
10149 child_die = child_die->sibling;
10150 }
10151
10152 /* In some cases, GCC generates specification DIEs that
10153 themselves contain DW_AT_specification attributes. */
10154 spec_die = die_specification (spec_die, &spec_cu);
10155 }
10156 }
10157
10158 struct context_stack cstk = cu->get_builder ()->pop_context ();
10159 /* Make a block for the local symbols within. */
10160 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
10161 cstk.static_link, lowpc, highpc);
10162
10163 /* For C++, set the block's scope. */
10164 if ((cu->lang () == language_cplus
10165 || cu->lang () == language_fortran
10166 || cu->lang () == language_d
10167 || cu->lang () == language_rust)
10168 && cu->processing_has_namespace_info)
10169 block->set_scope (determine_prefix (die, cu),
10170 &objfile->objfile_obstack);
10171
10172 /* If we have address ranges, record them. */
10173 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10174
10175 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
10176
10177 /* Attach template arguments to function. */
10178 if (!template_args.empty ())
10179 {
10180 gdb_assert (templ_func != NULL);
10181
10182 templ_func->n_template_arguments = template_args.size ();
10183 templ_func->template_arguments
10184 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
10185 templ_func->n_template_arguments);
10186 memcpy (templ_func->template_arguments,
10187 template_args.data (),
10188 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10189
10190 /* Make sure that the symtab is set on the new symbols. Even
10191 though they don't appear in this symtab directly, other parts
10192 of gdb assume that symbols do, and this is reasonably
10193 true. */
10194 for (symbol *sym : template_args)
10195 sym->set_symtab (templ_func->symtab ());
10196 }
10197
10198 /* In C++, we can have functions nested inside functions (e.g., when
10199 a function declares a class that has methods). This means that
10200 when we finish processing a function scope, we may need to go
10201 back to building a containing block's symbol lists. */
10202 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10203 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10204
10205 /* If we've finished processing a top-level function, subsequent
10206 symbols go in the file symbol list. */
10207 if (cu->get_builder ()->outermost_context_p ())
10208 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
10209 }
10210
10211 /* Process all the DIES contained within a lexical block scope. Start
10212 a new scope, process the dies, and then close the scope. */
10213
10214 static void
10215 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10216 {
10217 struct objfile *objfile = cu->per_objfile->objfile;
10218 struct gdbarch *gdbarch = objfile->arch ();
10219 CORE_ADDR lowpc, highpc;
10220 struct die_info *child_die;
10221 CORE_ADDR baseaddr;
10222
10223 baseaddr = objfile->text_section_offset ();
10224
10225 /* Ignore blocks with missing or invalid low and high pc attributes. */
10226 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10227 as multiple lexical blocks? Handling children in a sane way would
10228 be nasty. Might be easier to properly extend generic blocks to
10229 describe ranges. */
10230 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
10231 {
10232 case PC_BOUNDS_NOT_PRESENT:
10233 /* DW_TAG_lexical_block has no attributes, process its children as if
10234 there was no wrapping by that DW_TAG_lexical_block.
10235 GCC does no longer produces such DWARF since GCC r224161. */
10236 for (child_die = die->child;
10237 child_die != NULL && child_die->tag;
10238 child_die = child_die->sibling)
10239 {
10240 /* We might already be processing this DIE. This can happen
10241 in an unusual circumstance -- where a subroutine A
10242 appears lexically in another subroutine B, but A actually
10243 inlines B. The recursion is broken here, rather than in
10244 inherit_abstract_dies, because it seems better to simply
10245 drop concrete children here. */
10246 if (!child_die->in_process)
10247 process_die (child_die, cu);
10248 }
10249 return;
10250 case PC_BOUNDS_INVALID:
10251 return;
10252 }
10253 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10254 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10255
10256 cu->get_builder ()->push_context (0, lowpc);
10257 if (die->child != NULL)
10258 {
10259 child_die = die->child;
10260 while (child_die && child_die->tag)
10261 {
10262 process_die (child_die, cu);
10263 child_die = child_die->sibling;
10264 }
10265 }
10266 inherit_abstract_dies (die, cu);
10267 struct context_stack cstk = cu->get_builder ()->pop_context ();
10268
10269 if (*cu->get_builder ()->get_local_symbols () != NULL
10270 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
10271 {
10272 struct block *block
10273 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
10274 cstk.start_addr, highpc);
10275
10276 /* Note that recording ranges after traversing children, as we
10277 do here, means that recording a parent's ranges entails
10278 walking across all its children's ranges as they appear in
10279 the address map, which is quadratic behavior.
10280
10281 It would be nicer to record the parent's ranges before
10282 traversing its children, simply overriding whatever you find
10283 there. But since we don't even decide whether to create a
10284 block until after we've traversed its children, that's hard
10285 to do. */
10286 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10287 }
10288 *cu->get_builder ()->get_local_symbols () = cstk.locals;
10289 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
10290 }
10291
10292 static void dwarf2_ranges_read_low_addrs (unsigned offset,
10293 struct dwarf2_cu *cu,
10294 dwarf_tag tag,
10295 std::vector<CORE_ADDR> &result);
10296
10297 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
10298
10299 static void
10300 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10301 {
10302 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10303 struct objfile *objfile = per_objfile->objfile;
10304 struct gdbarch *gdbarch = objfile->arch ();
10305 CORE_ADDR pc, baseaddr;
10306 struct attribute *attr;
10307 void **slot;
10308 int nparams;
10309 struct die_info *child_die;
10310
10311 baseaddr = objfile->text_section_offset ();
10312
10313 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
10314 if (attr == NULL)
10315 {
10316 /* This was a pre-DWARF-5 GNU extension alias
10317 for DW_AT_call_return_pc. */
10318 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10319 }
10320 if (!attr)
10321 {
10322 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
10323 "DIE %s [in module %s]"),
10324 sect_offset_str (die->sect_off), objfile_name (objfile));
10325 return;
10326 }
10327 pc = attr->as_address () + baseaddr;
10328 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
10329 pc -= baseaddr;
10330
10331 if (cu->call_site_htab == NULL)
10332 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
10333 call_site::eq, NULL,
10334 &objfile->objfile_obstack,
10335 hashtab_obstack_allocate, NULL);
10336 struct call_site call_site_local (pc, nullptr, nullptr);
10337 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10338 if (*slot != NULL)
10339 {
10340 complaint (_("Duplicate PC %s for DW_TAG_call_site "
10341 "DIE %s [in module %s]"),
10342 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
10343 objfile_name (objfile));
10344 return;
10345 }
10346
10347 /* Count parameters at the caller. */
10348
10349 nparams = 0;
10350 for (child_die = die->child; child_die && child_die->tag;
10351 child_die = child_die->sibling)
10352 {
10353 if (child_die->tag != DW_TAG_call_site_parameter
10354 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10355 {
10356 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
10357 "DW_TAG_call_site child DIE %s [in module %s]"),
10358 child_die->tag, sect_offset_str (child_die->sect_off),
10359 objfile_name (objfile));
10360 continue;
10361 }
10362
10363 nparams++;
10364 }
10365
10366 struct call_site *call_site
10367 = new (XOBNEWVAR (&objfile->objfile_obstack,
10368 struct call_site,
10369 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
10370 struct call_site (pc, cu->per_cu, per_objfile);
10371 *slot = call_site;
10372
10373 /* We never call the destructor of call_site, so we must ensure it is
10374 trivially destructible. */
10375 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
10376
10377 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
10378 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10379 {
10380 struct die_info *func_die;
10381
10382 /* Skip also over DW_TAG_inlined_subroutine. */
10383 for (func_die = die->parent;
10384 func_die && func_die->tag != DW_TAG_subprogram
10385 && func_die->tag != DW_TAG_subroutine_type;
10386 func_die = func_die->parent);
10387
10388 /* DW_AT_call_all_calls is a superset
10389 of DW_AT_call_all_tail_calls. */
10390 if (func_die
10391 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
10392 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10393 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
10394 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10395 {
10396 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10397 not complete. But keep CALL_SITE for look ups via call_site_htab,
10398 both the initial caller containing the real return address PC and
10399 the final callee containing the current PC of a chain of tail
10400 calls do not need to have the tail call list complete. But any
10401 function candidate for a virtual tail call frame searched via
10402 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10403 determined unambiguously. */
10404 }
10405 else
10406 {
10407 struct type *func_type = NULL;
10408
10409 if (func_die)
10410 func_type = get_die_type (func_die, cu);
10411 if (func_type != NULL)
10412 {
10413 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
10414
10415 /* Enlist this call site to the function. */
10416 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10417 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10418 }
10419 else
10420 complaint (_("Cannot find function owning DW_TAG_call_site "
10421 "DIE %s [in module %s]"),
10422 sect_offset_str (die->sect_off), objfile_name (objfile));
10423 }
10424 }
10425
10426 attr = dwarf2_attr (die, DW_AT_call_target, cu);
10427 if (attr == NULL)
10428 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10429 if (attr == NULL)
10430 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
10431 if (attr == NULL)
10432 {
10433 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
10434 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10435 }
10436
10437 call_site->target.set_loc_dwarf_block (nullptr);
10438 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
10439 /* Keep NULL DWARF_BLOCK. */;
10440 else if (attr->form_is_block ())
10441 {
10442 struct dwarf2_locexpr_baton *dlbaton;
10443 struct dwarf_block *block = attr->as_block ();
10444
10445 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
10446 dlbaton->data = block->data;
10447 dlbaton->size = block->size;
10448 dlbaton->per_objfile = per_objfile;
10449 dlbaton->per_cu = cu->per_cu;
10450
10451 call_site->target.set_loc_dwarf_block (dlbaton);
10452 }
10453 else if (attr->form_is_ref ())
10454 {
10455 struct dwarf2_cu *target_cu = cu;
10456 struct die_info *target_die;
10457
10458 target_die = follow_die_ref (die, attr, &target_cu);
10459 gdb_assert (target_cu->per_objfile->objfile == objfile);
10460
10461 struct attribute *ranges_attr
10462 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
10463
10464 if (die_is_declaration (target_die, target_cu))
10465 {
10466 const char *target_physname;
10467
10468 /* Prefer the mangled name; otherwise compute the demangled one. */
10469 target_physname = dw2_linkage_name (target_die, target_cu);
10470 if (target_physname == NULL)
10471 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10472 if (target_physname == NULL)
10473 complaint (_("DW_AT_call_target target DIE has invalid "
10474 "physname, for referencing DIE %s [in module %s]"),
10475 sect_offset_str (die->sect_off), objfile_name (objfile));
10476 else
10477 call_site->target.set_loc_physname (target_physname);
10478 }
10479 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
10480 {
10481 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
10482 + target_cu->gnu_ranges_base);
10483 std::vector<CORE_ADDR> addresses;
10484 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
10485 target_die->tag, addresses);
10486 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
10487 addresses.size ());
10488 std::copy (addresses.begin (), addresses.end (), saved);
10489 call_site->target.set_loc_array (addresses.size (), saved);
10490 }
10491 else
10492 {
10493 CORE_ADDR lowpc;
10494
10495 /* DW_AT_entry_pc should be preferred. */
10496 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
10497 nullptr, nullptr)
10498 <= PC_BOUNDS_INVALID)
10499 complaint (_("DW_AT_call_target target DIE has invalid "
10500 "low pc, for referencing DIE %s [in module %s]"),
10501 sect_offset_str (die->sect_off), objfile_name (objfile));
10502 else
10503 {
10504 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
10505 - baseaddr);
10506 call_site->target.set_loc_physaddr (lowpc);
10507 }
10508 }
10509 }
10510 else
10511 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
10512 "block nor reference, for DIE %s [in module %s]"),
10513 sect_offset_str (die->sect_off), objfile_name (objfile));
10514
10515 for (child_die = die->child;
10516 child_die && child_die->tag;
10517 child_die = child_die->sibling)
10518 {
10519 struct call_site_parameter *parameter;
10520 struct attribute *loc, *origin;
10521
10522 if (child_die->tag != DW_TAG_call_site_parameter
10523 && child_die->tag != DW_TAG_GNU_call_site_parameter)
10524 {
10525 /* Already printed the complaint above. */
10526 continue;
10527 }
10528
10529 gdb_assert (call_site->parameter_count < nparams);
10530 parameter = &call_site->parameter[call_site->parameter_count];
10531
10532 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10533 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10534 register is contained in DW_AT_call_value. */
10535
10536 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10537 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
10538 if (origin == NULL)
10539 {
10540 /* This was a pre-DWARF-5 GNU extension alias
10541 for DW_AT_call_parameter. */
10542 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10543 }
10544 if (loc == NULL && origin != NULL && origin->form_is_ref ())
10545 {
10546 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10547
10548 sect_offset sect_off = origin->get_ref_die_offset ();
10549 if (!cu->header.offset_in_cu_p (sect_off))
10550 {
10551 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10552 binding can be done only inside one CU. Such referenced DIE
10553 therefore cannot be even moved to DW_TAG_partial_unit. */
10554 complaint (_("DW_AT_call_parameter offset is not in CU for "
10555 "DW_TAG_call_site child DIE %s [in module %s]"),
10556 sect_offset_str (child_die->sect_off),
10557 objfile_name (objfile));
10558 continue;
10559 }
10560 parameter->u.param_cu_off
10561 = (cu_offset) (sect_off - cu->header.sect_off);
10562 }
10563 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
10564 {
10565 complaint (_("No DW_FORM_block* DW_AT_location for "
10566 "DW_TAG_call_site child DIE %s [in module %s]"),
10567 sect_offset_str (child_die->sect_off), objfile_name (objfile));
10568 continue;
10569 }
10570 else
10571 {
10572 struct dwarf_block *block = loc->as_block ();
10573
10574 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10575 (block->data, &block->data[block->size]);
10576 if (parameter->u.dwarf_reg != -1)
10577 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10578 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
10579 &block->data[block->size],
10580 &parameter->u.fb_offset))
10581 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10582 else
10583 {
10584 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
10585 "for DW_FORM_block* DW_AT_location is supported for "
10586 "DW_TAG_call_site child DIE %s "
10587 "[in module %s]"),
10588 sect_offset_str (child_die->sect_off),
10589 objfile_name (objfile));
10590 continue;
10591 }
10592 }
10593
10594 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
10595 if (attr == NULL)
10596 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10597 if (attr == NULL || !attr->form_is_block ())
10598 {
10599 complaint (_("No DW_FORM_block* DW_AT_call_value for "
10600 "DW_TAG_call_site child DIE %s [in module %s]"),
10601 sect_offset_str (child_die->sect_off),
10602 objfile_name (objfile));
10603 continue;
10604 }
10605
10606 struct dwarf_block *block = attr->as_block ();
10607 parameter->value = block->data;
10608 parameter->value_size = block->size;
10609
10610 /* Parameters are not pre-cleared by memset above. */
10611 parameter->data_value = NULL;
10612 parameter->data_value_size = 0;
10613 call_site->parameter_count++;
10614
10615 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
10616 if (attr == NULL)
10617 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10618 if (attr != nullptr)
10619 {
10620 if (!attr->form_is_block ())
10621 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
10622 "DW_TAG_call_site child DIE %s [in module %s]"),
10623 sect_offset_str (child_die->sect_off),
10624 objfile_name (objfile));
10625 else
10626 {
10627 block = attr->as_block ();
10628 parameter->data_value = block->data;
10629 parameter->data_value_size = block->size;
10630 }
10631 }
10632 }
10633 }
10634
10635 /* Helper function for read_variable. If DIE represents a virtual
10636 table, then return the type of the concrete object that is
10637 associated with the virtual table. Otherwise, return NULL. */
10638
10639 static struct type *
10640 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10641 {
10642 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
10643 if (attr == NULL)
10644 return NULL;
10645
10646 /* Find the type DIE. */
10647 struct die_info *type_die = NULL;
10648 struct dwarf2_cu *type_cu = cu;
10649
10650 if (attr->form_is_ref ())
10651 type_die = follow_die_ref (die, attr, &type_cu);
10652 if (type_die == NULL)
10653 return NULL;
10654
10655 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
10656 return NULL;
10657 return die_containing_type (type_die, type_cu);
10658 }
10659
10660 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
10661
10662 static void
10663 read_variable (struct die_info *die, struct dwarf2_cu *cu)
10664 {
10665 struct rust_vtable_symbol *storage = NULL;
10666
10667 if (cu->lang () == language_rust)
10668 {
10669 struct type *containing_type = rust_containing_type (die, cu);
10670
10671 if (containing_type != NULL)
10672 {
10673 struct objfile *objfile = cu->per_objfile->objfile;
10674
10675 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
10676 storage->concrete_type = containing_type;
10677 storage->subclass = SYMBOL_RUST_VTABLE;
10678 }
10679 }
10680
10681 struct symbol *res = new_symbol (die, NULL, cu, storage);
10682 struct attribute *abstract_origin
10683 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10684 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
10685 if (res == NULL && loc && abstract_origin)
10686 {
10687 /* We have a variable without a name, but with a location and an abstract
10688 origin. This may be a concrete instance of an abstract variable
10689 referenced from an DW_OP_GNU_variable_value, so save it to find it back
10690 later. */
10691 struct dwarf2_cu *origin_cu = cu;
10692 struct die_info *origin_die
10693 = follow_die_ref (die, abstract_origin, &origin_cu);
10694 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10695 per_objfile->per_bfd->abstract_to_concrete
10696 [origin_die->sect_off].push_back (die->sect_off);
10697 }
10698 }
10699
10700 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
10701 reading .debug_rnglists.
10702 Callback's type should be:
10703 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10704 Return true if the attributes are present and valid, otherwise,
10705 return false. */
10706
10707 template <typename Callback>
10708 static bool
10709 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
10710 dwarf_tag tag, Callback &&callback)
10711 {
10712 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10713 struct objfile *objfile = per_objfile->objfile;
10714 bfd *obfd = objfile->obfd.get ();
10715 /* Base address selection entry. */
10716 gdb::optional<CORE_ADDR> base;
10717 const gdb_byte *buffer;
10718 bool overflow = false;
10719 ULONGEST addr_index;
10720 struct dwarf2_section_info *rnglists_section;
10721
10722 base = cu->base_address;
10723 rnglists_section = cu_debug_rnglists_section (cu, tag);
10724 rnglists_section->read (objfile);
10725
10726 if (offset >= rnglists_section->size)
10727 {
10728 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10729 offset);
10730 return false;
10731 }
10732 buffer = rnglists_section->buffer + offset;
10733
10734 while (1)
10735 {
10736 /* Initialize it due to a false compiler warning. */
10737 CORE_ADDR range_beginning = 0, range_end = 0;
10738 const gdb_byte *buf_end = (rnglists_section->buffer
10739 + rnglists_section->size);
10740 unsigned int bytes_read;
10741
10742 if (buffer == buf_end)
10743 {
10744 overflow = true;
10745 break;
10746 }
10747 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
10748 switch (rlet)
10749 {
10750 case DW_RLE_end_of_list:
10751 break;
10752 case DW_RLE_base_address:
10753 if (buffer + cu->header.addr_size > buf_end)
10754 {
10755 overflow = true;
10756 break;
10757 }
10758 base = cu->header.read_address (obfd, buffer, &bytes_read);
10759 buffer += bytes_read;
10760 break;
10761 case DW_RLE_base_addressx:
10762 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10763 buffer += bytes_read;
10764 base = read_addr_index (cu, addr_index);
10765 break;
10766 case DW_RLE_start_length:
10767 if (buffer + cu->header.addr_size > buf_end)
10768 {
10769 overflow = true;
10770 break;
10771 }
10772 range_beginning = cu->header.read_address (obfd, buffer,
10773 &bytes_read);
10774 buffer += bytes_read;
10775 range_end = (range_beginning
10776 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
10777 buffer += bytes_read;
10778 if (buffer > buf_end)
10779 {
10780 overflow = true;
10781 break;
10782 }
10783 break;
10784 case DW_RLE_startx_length:
10785 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10786 buffer += bytes_read;
10787 range_beginning = read_addr_index (cu, addr_index);
10788 if (buffer > buf_end)
10789 {
10790 overflow = true;
10791 break;
10792 }
10793 range_end = (range_beginning
10794 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
10795 buffer += bytes_read;
10796 break;
10797 case DW_RLE_offset_pair:
10798 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10799 buffer += bytes_read;
10800 if (buffer > buf_end)
10801 {
10802 overflow = true;
10803 break;
10804 }
10805 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10806 buffer += bytes_read;
10807 if (buffer > buf_end)
10808 {
10809 overflow = true;
10810 break;
10811 }
10812 break;
10813 case DW_RLE_start_end:
10814 if (buffer + 2 * cu->header.addr_size > buf_end)
10815 {
10816 overflow = true;
10817 break;
10818 }
10819 range_beginning = cu->header.read_address (obfd, buffer,
10820 &bytes_read);
10821 buffer += bytes_read;
10822 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
10823 buffer += bytes_read;
10824 break;
10825 case DW_RLE_startx_endx:
10826 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10827 buffer += bytes_read;
10828 range_beginning = read_addr_index (cu, addr_index);
10829 if (buffer > buf_end)
10830 {
10831 overflow = true;
10832 break;
10833 }
10834 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10835 buffer += bytes_read;
10836 range_end = read_addr_index (cu, addr_index);
10837 break;
10838 default:
10839 complaint (_("Invalid .debug_rnglists data (no base address)"));
10840 return false;
10841 }
10842 if (rlet == DW_RLE_end_of_list || overflow)
10843 break;
10844 if (rlet == DW_RLE_base_address)
10845 continue;
10846
10847 if (range_beginning > range_end)
10848 {
10849 /* Inverted range entries are invalid. */
10850 complaint (_("Invalid .debug_rnglists data (inverted range)"));
10851 return false;
10852 }
10853
10854 /* Empty range entries have no effect. */
10855 if (range_beginning == range_end)
10856 continue;
10857
10858 /* Only DW_RLE_offset_pair needs the base address added. */
10859 if (rlet == DW_RLE_offset_pair)
10860 {
10861 if (!base.has_value ())
10862 {
10863 /* We have no valid base address for the DW_RLE_offset_pair. */
10864 complaint (_("Invalid .debug_rnglists data (no base address for "
10865 "DW_RLE_offset_pair)"));
10866 return false;
10867 }
10868
10869 range_beginning += *base;
10870 range_end += *base;
10871 }
10872
10873 /* A not-uncommon case of bad debug info.
10874 Don't pollute the addrmap with bad data. */
10875 if (range_beginning == 0
10876 && !per_objfile->per_bfd->has_section_at_zero)
10877 {
10878 complaint (_(".debug_rnglists entry has start address of zero"
10879 " [in module %s]"), objfile_name (objfile));
10880 continue;
10881 }
10882
10883 callback (range_beginning, range_end);
10884 }
10885
10886 if (overflow)
10887 {
10888 complaint (_("Offset %d is not terminated "
10889 "for DW_AT_ranges attribute"),
10890 offset);
10891 return false;
10892 }
10893
10894 return true;
10895 }
10896
10897 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
10898 Callback's type should be:
10899 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10900 Return 1 if the attributes are present and valid, otherwise, return 0. */
10901
10902 template <typename Callback>
10903 static int
10904 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
10905 Callback &&callback)
10906 {
10907 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10908 struct objfile *objfile = per_objfile->objfile;
10909 struct comp_unit_head *cu_header = &cu->header;
10910 bfd *obfd = objfile->obfd.get ();
10911 unsigned int addr_size = cu_header->addr_size;
10912 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10913 /* Base address selection entry. */
10914 gdb::optional<CORE_ADDR> base;
10915 unsigned int dummy;
10916 const gdb_byte *buffer;
10917
10918 if (cu_header->version >= 5)
10919 return dwarf2_rnglists_process (offset, cu, tag, callback);
10920
10921 base = cu->base_address;
10922
10923 per_objfile->per_bfd->ranges.read (objfile);
10924 if (offset >= per_objfile->per_bfd->ranges.size)
10925 {
10926 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10927 offset);
10928 return 0;
10929 }
10930 buffer = per_objfile->per_bfd->ranges.buffer + offset;
10931
10932 while (1)
10933 {
10934 CORE_ADDR range_beginning, range_end;
10935
10936 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
10937 buffer += addr_size;
10938 range_end = cu->header.read_address (obfd, buffer, &dummy);
10939 buffer += addr_size;
10940 offset += 2 * addr_size;
10941
10942 /* An end of list marker is a pair of zero addresses. */
10943 if (range_beginning == 0 && range_end == 0)
10944 /* Found the end of list entry. */
10945 break;
10946
10947 /* Each base address selection entry is a pair of 2 values.
10948 The first is the largest possible address, the second is
10949 the base address. Check for a base address here. */
10950 if ((range_beginning & mask) == mask)
10951 {
10952 /* If we found the largest possible address, then we already
10953 have the base address in range_end. */
10954 base = range_end;
10955 continue;
10956 }
10957
10958 if (!base.has_value ())
10959 {
10960 /* We have no valid base address for the ranges
10961 data. */
10962 complaint (_("Invalid .debug_ranges data (no base address)"));
10963 return 0;
10964 }
10965
10966 if (range_beginning > range_end)
10967 {
10968 /* Inverted range entries are invalid. */
10969 complaint (_("Invalid .debug_ranges data (inverted range)"));
10970 return 0;
10971 }
10972
10973 /* Empty range entries have no effect. */
10974 if (range_beginning == range_end)
10975 continue;
10976
10977 range_beginning += *base;
10978 range_end += *base;
10979
10980 /* A not-uncommon case of bad debug info.
10981 Don't pollute the addrmap with bad data. */
10982 if (range_beginning == 0
10983 && !per_objfile->per_bfd->has_section_at_zero)
10984 {
10985 complaint (_(".debug_ranges entry has start address of zero"
10986 " [in module %s]"), objfile_name (objfile));
10987 continue;
10988 }
10989
10990 callback (range_beginning, range_end);
10991 }
10992
10993 return 1;
10994 }
10995
10996 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10997 Return 1 if the attributes are present and valid, otherwise, return 0.
10998 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
10999 ranges in MAP are set, using DATUM as the value. */
11000
11001 static int
11002 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
11003 CORE_ADDR *high_return, struct dwarf2_cu *cu,
11004 addrmap *map, void *datum, dwarf_tag tag)
11005 {
11006 struct objfile *objfile = cu->per_objfile->objfile;
11007 struct gdbarch *gdbarch = objfile->arch ();
11008 const CORE_ADDR baseaddr = objfile->text_section_offset ();
11009 int low_set = 0;
11010 CORE_ADDR low = 0;
11011 CORE_ADDR high = 0;
11012 int retval;
11013
11014 retval = dwarf2_ranges_process (offset, cu, tag,
11015 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
11016 {
11017 if (map != nullptr)
11018 {
11019 CORE_ADDR lowpc;
11020 CORE_ADDR highpc;
11021
11022 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
11023 range_beginning + baseaddr)
11024 - baseaddr);
11025 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
11026 range_end + baseaddr)
11027 - baseaddr);
11028 map->set_empty (lowpc, highpc - 1, datum);
11029 }
11030
11031 /* FIXME: This is recording everything as a low-high
11032 segment of consecutive addresses. We should have a
11033 data structure for discontiguous block ranges
11034 instead. */
11035 if (! low_set)
11036 {
11037 low = range_beginning;
11038 high = range_end;
11039 low_set = 1;
11040 }
11041 else
11042 {
11043 if (range_beginning < low)
11044 low = range_beginning;
11045 if (range_end > high)
11046 high = range_end;
11047 }
11048 });
11049 if (!retval)
11050 return 0;
11051
11052 if (! low_set)
11053 /* If the first entry is an end-of-list marker, the range
11054 describes an empty scope, i.e. no instructions. */
11055 return 0;
11056
11057 if (low_return)
11058 *low_return = low;
11059 if (high_return)
11060 *high_return = high;
11061 return 1;
11062 }
11063
11064 /* Process ranges and fill in a vector of the low PC values only. */
11065
11066 static void
11067 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
11068 dwarf_tag tag,
11069 std::vector<CORE_ADDR> &result)
11070 {
11071 dwarf2_ranges_process (offset, cu, tag,
11072 [&] (CORE_ADDR start, CORE_ADDR end)
11073 {
11074 result.push_back (start);
11075 });
11076 }
11077
11078 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
11079 definition for the return value. *LOWPC and *HIGHPC are set iff
11080 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
11081
11082 static enum pc_bounds_kind
11083 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
11084 CORE_ADDR *highpc, struct dwarf2_cu *cu,
11085 addrmap *map, void *datum)
11086 {
11087 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11088 struct attribute *attr;
11089 struct attribute *attr_high;
11090 CORE_ADDR low = 0;
11091 CORE_ADDR high = 0;
11092 enum pc_bounds_kind ret;
11093
11094 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11095 if (attr_high)
11096 {
11097 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11098 if (attr != nullptr)
11099 {
11100 low = attr->as_address ();
11101 high = attr_high->as_address ();
11102 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11103 high += low;
11104 }
11105 else
11106 /* Found high w/o low attribute. */
11107 return PC_BOUNDS_INVALID;
11108
11109 /* Found consecutive range of addresses. */
11110 ret = PC_BOUNDS_HIGH_LOW;
11111 }
11112 else
11113 {
11114 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11115 if (attr != nullptr && attr->form_is_unsigned ())
11116 {
11117 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11118 on DWARF version). */
11119 ULONGEST ranges_offset = attr->as_unsigned ();
11120
11121 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11122 this value. */
11123 if (die->tag != DW_TAG_compile_unit)
11124 ranges_offset += cu->gnu_ranges_base;
11125
11126 /* Value of the DW_AT_ranges attribute is the offset in the
11127 .debug_ranges section. */
11128 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
11129 map, datum, die->tag))
11130 return PC_BOUNDS_INVALID;
11131 /* Found discontinuous range of addresses. */
11132 ret = PC_BOUNDS_RANGES;
11133 }
11134 else
11135 return PC_BOUNDS_NOT_PRESENT;
11136 }
11137
11138 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
11139 if (high <= low)
11140 return PC_BOUNDS_INVALID;
11141
11142 /* When using the GNU linker, .gnu.linkonce. sections are used to
11143 eliminate duplicate copies of functions and vtables and such.
11144 The linker will arbitrarily choose one and discard the others.
11145 The AT_*_pc values for such functions refer to local labels in
11146 these sections. If the section from that file was discarded, the
11147 labels are not in the output, so the relocs get a value of 0.
11148 If this is a discarded function, mark the pc bounds as invalid,
11149 so that GDB will ignore it. */
11150 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
11151 return PC_BOUNDS_INVALID;
11152
11153 *lowpc = low;
11154 if (highpc)
11155 *highpc = high;
11156 return ret;
11157 }
11158
11159 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
11160 its low and high PC addresses. Do nothing if these addresses could not
11161 be determined. Otherwise, set LOWPC to the low address if it is smaller,
11162 and HIGHPC to the high address if greater than HIGHPC. */
11163
11164 static void
11165 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
11166 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11167 struct dwarf2_cu *cu)
11168 {
11169 CORE_ADDR low, high;
11170 struct die_info *child = die->child;
11171
11172 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
11173 >= PC_BOUNDS_RANGES)
11174 {
11175 *lowpc = std::min (*lowpc, low);
11176 *highpc = std::max (*highpc, high);
11177 }
11178
11179 /* If the language does not allow nested subprograms (either inside
11180 subprograms or lexical blocks), we're done. */
11181 if (cu->lang () != language_ada)
11182 return;
11183
11184 /* Check all the children of the given DIE. If it contains nested
11185 subprograms, then check their pc bounds. Likewise, we need to
11186 check lexical blocks as well, as they may also contain subprogram
11187 definitions. */
11188 while (child && child->tag)
11189 {
11190 if (child->tag == DW_TAG_subprogram
11191 || child->tag == DW_TAG_lexical_block)
11192 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
11193 child = child->sibling;
11194 }
11195 }
11196
11197 /* Get the low and high pc's represented by the scope DIE, and store
11198 them in *LOWPC and *HIGHPC. If the correct values can't be
11199 determined, set *LOWPC to -1 and *HIGHPC to 0. */
11200
11201 static void
11202 get_scope_pc_bounds (struct die_info *die,
11203 CORE_ADDR *lowpc, CORE_ADDR *highpc,
11204 struct dwarf2_cu *cu)
11205 {
11206 CORE_ADDR best_low = (CORE_ADDR) -1;
11207 CORE_ADDR best_high = (CORE_ADDR) 0;
11208 CORE_ADDR current_low, current_high;
11209
11210 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
11211 nullptr, nullptr)
11212 >= PC_BOUNDS_RANGES)
11213 {
11214 best_low = current_low;
11215 best_high = current_high;
11216 }
11217 else
11218 {
11219 struct die_info *child = die->child;
11220
11221 while (child && child->tag)
11222 {
11223 switch (child->tag) {
11224 case DW_TAG_subprogram:
11225 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
11226 break;
11227 case DW_TAG_namespace:
11228 case DW_TAG_module:
11229 /* FIXME: carlton/2004-01-16: Should we do this for
11230 DW_TAG_class_type/DW_TAG_structure_type, too? I think
11231 that current GCC's always emit the DIEs corresponding
11232 to definitions of methods of classes as children of a
11233 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11234 the DIEs giving the declarations, which could be
11235 anywhere). But I don't see any reason why the
11236 standards says that they have to be there. */
11237 get_scope_pc_bounds (child, &current_low, &current_high, cu);
11238
11239 if (current_low != ((CORE_ADDR) -1))
11240 {
11241 best_low = std::min (best_low, current_low);
11242 best_high = std::max (best_high, current_high);
11243 }
11244 break;
11245 default:
11246 /* Ignore. */
11247 break;
11248 }
11249
11250 child = child->sibling;
11251 }
11252 }
11253
11254 *lowpc = best_low;
11255 *highpc = best_high;
11256 }
11257
11258 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
11259 in DIE. */
11260
11261 static void
11262 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
11263 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
11264 {
11265 struct objfile *objfile = cu->per_objfile->objfile;
11266 struct gdbarch *gdbarch = objfile->arch ();
11267 struct attribute *attr;
11268 struct attribute *attr_high;
11269
11270 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11271 if (attr_high)
11272 {
11273 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11274 if (attr != nullptr)
11275 {
11276 CORE_ADDR low = attr->as_address ();
11277 CORE_ADDR high = attr_high->as_address ();
11278
11279 if (cu->header.version >= 4 && attr_high->form_is_constant ())
11280 high += low;
11281
11282 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
11283 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
11284 cu->get_builder ()->record_block_range (block, low, high - 1);
11285 }
11286 }
11287
11288 attr = dwarf2_attr (die, DW_AT_ranges, cu);
11289 if (attr != nullptr && attr->form_is_unsigned ())
11290 {
11291 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11292 on DWARF version). */
11293 ULONGEST ranges_offset = attr->as_unsigned ();
11294
11295 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11296 this value. */
11297 if (die->tag != DW_TAG_compile_unit)
11298 ranges_offset += cu->gnu_ranges_base;
11299
11300 std::vector<blockrange> blockvec;
11301 dwarf2_ranges_process (ranges_offset, cu, die->tag,
11302 [&] (CORE_ADDR start, CORE_ADDR end)
11303 {
11304 start += baseaddr;
11305 end += baseaddr;
11306 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
11307 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
11308 cu->get_builder ()->record_block_range (block, start, end - 1);
11309 blockvec.emplace_back (start, end);
11310 });
11311
11312 block->set_ranges (make_blockranges (objfile, blockvec));
11313 }
11314 }
11315
11316 /* Check whether the producer field indicates either of GCC < 4.6, or the
11317 Intel C/C++ compiler, and cache the result in CU. */
11318
11319 static void
11320 check_producer (struct dwarf2_cu *cu)
11321 {
11322 int major, minor;
11323
11324 if (cu->producer == NULL)
11325 {
11326 /* For unknown compilers expect their behavior is DWARF version
11327 compliant.
11328
11329 GCC started to support .debug_types sections by -gdwarf-4 since
11330 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11331 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11332 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11333 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11334 }
11335 else if (producer_is_gcc (cu->producer, &major, &minor))
11336 {
11337 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11338 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11339 cu->producer_is_gcc_11 = major == 11;
11340 }
11341 else if (producer_is_icc (cu->producer, &major, &minor))
11342 {
11343 cu->producer_is_icc = true;
11344 cu->producer_is_icc_lt_14 = major < 14;
11345 }
11346 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
11347 cu->producer_is_codewarrior = true;
11348 else if (producer_is_clang (cu->producer, &major, &minor))
11349 cu->producer_is_clang = true;
11350 else
11351 {
11352 /* For other non-GCC compilers, expect their behavior is DWARF version
11353 compliant. */
11354 }
11355
11356 cu->checked_producer = true;
11357 }
11358
11359 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11360 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11361 during 4.6.0 experimental. */
11362
11363 static bool
11364 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11365 {
11366 if (!cu->checked_producer)
11367 check_producer (cu);
11368
11369 return cu->producer_is_gxx_lt_4_6;
11370 }
11371
11372
11373 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
11374 with incorrect is_stmt attributes. */
11375
11376 static bool
11377 producer_is_codewarrior (struct dwarf2_cu *cu)
11378 {
11379 if (!cu->checked_producer)
11380 check_producer (cu);
11381
11382 return cu->producer_is_codewarrior;
11383 }
11384
11385 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
11386 If that attribute is not available, return the appropriate
11387 default. */
11388
11389 static enum dwarf_access_attribute
11390 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11391 {
11392 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11393 if (attr != nullptr)
11394 {
11395 LONGEST value = attr->constant_value (-1);
11396 if (value == DW_ACCESS_public
11397 || value == DW_ACCESS_protected
11398 || value == DW_ACCESS_private)
11399 return (dwarf_access_attribute) value;
11400 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
11401 plongest (value));
11402 }
11403
11404 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11405 {
11406 /* The default DWARF 2 accessibility for members is public, the default
11407 accessibility for inheritance is private. */
11408
11409 if (die->tag != DW_TAG_inheritance)
11410 return DW_ACCESS_public;
11411 else
11412 return DW_ACCESS_private;
11413 }
11414 else
11415 {
11416 /* DWARF 3+ defines the default accessibility a different way. The same
11417 rules apply now for DW_TAG_inheritance as for the members and it only
11418 depends on the container kind. */
11419
11420 if (die->parent->tag == DW_TAG_class_type)
11421 return DW_ACCESS_private;
11422 else
11423 return DW_ACCESS_public;
11424 }
11425 }
11426
11427 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
11428 *OFFSET to the byte offset. If the attribute was not found return
11429 0, otherwise return 1. If it was found but could not properly be
11430 handled, set *OFFSET to 0. */
11431
11432 static int
11433 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11434 LONGEST *offset)
11435 {
11436 struct attribute *attr;
11437
11438 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11439 if (attr != NULL)
11440 {
11441 *offset = 0;
11442
11443 /* Note that we do not check for a section offset first here.
11444 This is because DW_AT_data_member_location is new in DWARF 4,
11445 so if we see it, we can assume that a constant form is really
11446 a constant and not a section offset. */
11447 if (attr->form_is_constant ())
11448 *offset = attr->constant_value (0);
11449 else if (attr->form_is_section_offset ())
11450 dwarf2_complex_location_expr_complaint ();
11451 else if (attr->form_is_block ())
11452 *offset = decode_locdesc (attr->as_block (), cu);
11453 else
11454 dwarf2_complex_location_expr_complaint ();
11455
11456 return 1;
11457 }
11458 else
11459 {
11460 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11461 if (attr != nullptr)
11462 {
11463 *offset = attr->constant_value (0);
11464 return 1;
11465 }
11466 }
11467
11468 return 0;
11469 }
11470
11471 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
11472 store the results in FIELD. */
11473
11474 static void
11475 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
11476 struct field *field)
11477 {
11478 struct attribute *attr;
11479
11480 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11481 if (attr != NULL)
11482 {
11483 if (attr->form_is_constant ())
11484 {
11485 LONGEST offset = attr->constant_value (0);
11486
11487 /* Work around this GCC 11 bug, where it would erroneously use -1
11488 data member locations, instead of 0:
11489
11490 Negative DW_AT_data_member_location
11491 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
11492 */
11493 if (offset == -1 && cu->producer_is_gcc_11)
11494 {
11495 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
11496 offset = 0;
11497 }
11498
11499 field->set_loc_bitpos (offset * bits_per_byte);
11500 }
11501 else if (attr->form_is_section_offset ())
11502 dwarf2_complex_location_expr_complaint ();
11503 else if (attr->form_is_block ())
11504 {
11505 bool handled;
11506 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
11507 if (handled)
11508 field->set_loc_bitpos (offset * bits_per_byte);
11509 else
11510 {
11511 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11512 struct objfile *objfile = per_objfile->objfile;
11513 struct dwarf2_locexpr_baton *dlbaton
11514 = XOBNEW (&objfile->objfile_obstack,
11515 struct dwarf2_locexpr_baton);
11516 dlbaton->data = attr->as_block ()->data;
11517 dlbaton->size = attr->as_block ()->size;
11518 /* When using this baton, we want to compute the address
11519 of the field, not the value. This is why
11520 is_reference is set to false here. */
11521 dlbaton->is_reference = false;
11522 dlbaton->per_objfile = per_objfile;
11523 dlbaton->per_cu = cu->per_cu;
11524
11525 field->set_loc_dwarf_block (dlbaton);
11526 }
11527 }
11528 else
11529 dwarf2_complex_location_expr_complaint ();
11530 }
11531 else
11532 {
11533 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11534 if (attr != nullptr)
11535 field->set_loc_bitpos (attr->constant_value (0));
11536 }
11537 }
11538
11539 /* Add an aggregate field to the field list. */
11540
11541 static void
11542 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11543 struct dwarf2_cu *cu)
11544 {
11545 struct objfile *objfile = cu->per_objfile->objfile;
11546 struct gdbarch *gdbarch = objfile->arch ();
11547 struct nextfield *new_field;
11548 struct attribute *attr;
11549 struct field *fp;
11550 const char *fieldname = "";
11551
11552 if (die->tag == DW_TAG_inheritance)
11553 {
11554 fip->baseclasses.emplace_back ();
11555 new_field = &fip->baseclasses.back ();
11556 }
11557 else
11558 {
11559 fip->fields.emplace_back ();
11560 new_field = &fip->fields.back ();
11561 }
11562
11563 new_field->offset = die->sect_off;
11564
11565 new_field->accessibility = dwarf2_access_attribute (die, cu);
11566 if (new_field->accessibility != DW_ACCESS_public)
11567 fip->non_public_fields = true;
11568
11569 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11570 if (attr != nullptr)
11571 new_field->virtuality = attr->as_virtuality ();
11572 else
11573 new_field->virtuality = DW_VIRTUALITY_none;
11574
11575 fp = &new_field->field;
11576
11577 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
11578 && !die_is_declaration (die, cu))
11579 {
11580 if (die->tag == DW_TAG_namelist_item)
11581 {
11582 /* Typically, DW_TAG_namelist_item are references to namelist items.
11583 If so, follow that reference. */
11584 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
11585 struct die_info *item_die = nullptr;
11586 struct dwarf2_cu *item_cu = cu;
11587 if (attr1->form_is_ref ())
11588 item_die = follow_die_ref (die, attr1, &item_cu);
11589 if (item_die != nullptr)
11590 die = item_die;
11591 }
11592 /* Data member other than a C++ static data member. */
11593
11594 /* Get type of field. */
11595 fp->set_type (die_type (die, cu));
11596
11597 fp->set_loc_bitpos (0);
11598
11599 /* Get bit size of field (zero if none). */
11600 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11601 if (attr != nullptr)
11602 {
11603 FIELD_BITSIZE (*fp) = attr->constant_value (0);
11604 }
11605 else
11606 {
11607 FIELD_BITSIZE (*fp) = 0;
11608 }
11609
11610 /* Get bit offset of field. */
11611 handle_member_location (die, cu, fp);
11612 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11613 if (attr != nullptr && attr->form_is_constant ())
11614 {
11615 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
11616 {
11617 /* For big endian bits, the DW_AT_bit_offset gives the
11618 additional bit offset from the MSB of the containing
11619 anonymous object to the MSB of the field. We don't
11620 have to do anything special since we don't need to
11621 know the size of the anonymous object. */
11622 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
11623 }
11624 else
11625 {
11626 /* For little endian bits, compute the bit offset to the
11627 MSB of the anonymous object, subtract off the number of
11628 bits from the MSB of the field to the MSB of the
11629 object, and then subtract off the number of bits of
11630 the field itself. The result is the bit offset of
11631 the LSB of the field. */
11632 int anonymous_size;
11633 int bit_offset = attr->constant_value (0);
11634
11635 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11636 if (attr != nullptr && attr->form_is_constant ())
11637 {
11638 /* The size of the anonymous object containing
11639 the bit field is explicit, so use the
11640 indicated size (in bytes). */
11641 anonymous_size = attr->constant_value (0);
11642 }
11643 else
11644 {
11645 /* The size of the anonymous object containing
11646 the bit field must be inferred from the type
11647 attribute of the data member containing the
11648 bit field. */
11649 anonymous_size = fp->type ()->length ();
11650 }
11651 fp->set_loc_bitpos (fp->loc_bitpos ()
11652 + anonymous_size * bits_per_byte
11653 - bit_offset - FIELD_BITSIZE (*fp));
11654 }
11655 }
11656
11657 /* Get name of field. */
11658 fieldname = dwarf2_name (die, cu);
11659 if (fieldname == NULL)
11660 fieldname = "";
11661
11662 /* The name is already allocated along with this objfile, so we don't
11663 need to duplicate it for the type. */
11664 fp->set_name (fieldname);
11665
11666 /* Change accessibility for artificial fields (e.g. virtual table
11667 pointer or virtual base class pointer) to private. */
11668 if (dwarf2_attr (die, DW_AT_artificial, cu))
11669 {
11670 FIELD_ARTIFICIAL (*fp) = 1;
11671 new_field->accessibility = DW_ACCESS_private;
11672 fip->non_public_fields = true;
11673 }
11674 }
11675 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11676 {
11677 /* C++ static member. */
11678
11679 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11680 is a declaration, but all versions of G++ as of this writing
11681 (so through at least 3.2.1) incorrectly generate
11682 DW_TAG_variable tags. */
11683
11684 const char *physname;
11685
11686 /* Get name of field. */
11687 fieldname = dwarf2_name (die, cu);
11688 if (fieldname == NULL)
11689 return;
11690
11691 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11692 if (attr
11693 /* Only create a symbol if this is an external value.
11694 new_symbol checks this and puts the value in the global symbol
11695 table, which we want. If it is not external, new_symbol
11696 will try to put the value in cu->list_in_scope which is wrong. */
11697 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11698 {
11699 /* A static const member, not much different than an enum as far as
11700 we're concerned, except that we can support more types. */
11701 new_symbol (die, NULL, cu);
11702 }
11703
11704 /* Get physical name. */
11705 physname = dwarf2_physname (fieldname, die, cu);
11706
11707 /* The name is already allocated along with this objfile, so we don't
11708 need to duplicate it for the type. */
11709 fp->set_loc_physname (physname ? physname : "");
11710 fp->set_type (die_type (die, cu));
11711 fp->set_name (fieldname);
11712 }
11713 else if (die->tag == DW_TAG_inheritance)
11714 {
11715 /* C++ base class field. */
11716 handle_member_location (die, cu, fp);
11717 FIELD_BITSIZE (*fp) = 0;
11718 fp->set_type (die_type (die, cu));
11719 fp->set_name (fp->type ()->name ());
11720 }
11721 else
11722 gdb_assert_not_reached ("missing case in dwarf2_add_field");
11723 }
11724
11725 /* Can the type given by DIE define another type? */
11726
11727 static bool
11728 type_can_define_types (const struct die_info *die)
11729 {
11730 switch (die->tag)
11731 {
11732 case DW_TAG_typedef:
11733 case DW_TAG_class_type:
11734 case DW_TAG_structure_type:
11735 case DW_TAG_union_type:
11736 case DW_TAG_enumeration_type:
11737 return true;
11738
11739 default:
11740 return false;
11741 }
11742 }
11743
11744 /* Add a type definition defined in the scope of the FIP's class. */
11745
11746 static void
11747 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
11748 struct dwarf2_cu *cu)
11749 {
11750 struct decl_field fp;
11751 memset (&fp, 0, sizeof (fp));
11752
11753 gdb_assert (type_can_define_types (die));
11754
11755 /* Get name of field. NULL is okay here, meaning an anonymous type. */
11756 fp.name = dwarf2_name (die, cu);
11757 fp.type = read_type_die (die, cu);
11758
11759 /* Save accessibility. */
11760 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
11761 switch (accessibility)
11762 {
11763 case DW_ACCESS_public:
11764 /* The assumed value if neither private nor protected. */
11765 break;
11766 case DW_ACCESS_private:
11767 fp.is_private = 1;
11768 break;
11769 case DW_ACCESS_protected:
11770 fp.is_protected = 1;
11771 break;
11772 }
11773
11774 if (die->tag == DW_TAG_typedef)
11775 fip->typedef_field_list.push_back (fp);
11776 else
11777 fip->nested_types_list.push_back (fp);
11778 }
11779
11780 /* A convenience typedef that's used when finding the discriminant
11781 field for a variant part. */
11782 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
11783 offset_map_type;
11784
11785 /* Compute the discriminant range for a given variant. OBSTACK is
11786 where the results will be stored. VARIANT is the variant to
11787 process. IS_UNSIGNED indicates whether the discriminant is signed
11788 or unsigned. */
11789
11790 static const gdb::array_view<discriminant_range>
11791 convert_variant_range (struct obstack *obstack, const variant_field &variant,
11792 bool is_unsigned)
11793 {
11794 std::vector<discriminant_range> ranges;
11795
11796 if (variant.default_branch)
11797 return {};
11798
11799 if (variant.discr_list_data == nullptr)
11800 {
11801 discriminant_range r
11802 = {variant.discriminant_value, variant.discriminant_value};
11803 ranges.push_back (r);
11804 }
11805 else
11806 {
11807 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
11808 variant.discr_list_data->size);
11809 while (!data.empty ())
11810 {
11811 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
11812 {
11813 complaint (_("invalid discriminant marker: %d"), data[0]);
11814 break;
11815 }
11816 bool is_range = data[0] == DW_DSC_range;
11817 data = data.slice (1);
11818
11819 ULONGEST low, high;
11820 unsigned int bytes_read;
11821
11822 if (data.empty ())
11823 {
11824 complaint (_("DW_AT_discr_list missing low value"));
11825 break;
11826 }
11827 if (is_unsigned)
11828 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
11829 else
11830 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
11831 &bytes_read);
11832 data = data.slice (bytes_read);
11833
11834 if (is_range)
11835 {
11836 if (data.empty ())
11837 {
11838 complaint (_("DW_AT_discr_list missing high value"));
11839 break;
11840 }
11841 if (is_unsigned)
11842 high = read_unsigned_leb128 (nullptr, data.data (),
11843 &bytes_read);
11844 else
11845 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
11846 &bytes_read);
11847 data = data.slice (bytes_read);
11848 }
11849 else
11850 high = low;
11851
11852 ranges.push_back ({ low, high });
11853 }
11854 }
11855
11856 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
11857 ranges.size ());
11858 std::copy (ranges.begin (), ranges.end (), result);
11859 return gdb::array_view<discriminant_range> (result, ranges.size ());
11860 }
11861
11862 static const gdb::array_view<variant_part> create_variant_parts
11863 (struct obstack *obstack,
11864 const offset_map_type &offset_map,
11865 struct field_info *fi,
11866 const std::vector<variant_part_builder> &variant_parts);
11867
11868 /* Fill in a "struct variant" for a given variant field. RESULT is
11869 the variant to fill in. OBSTACK is where any needed allocations
11870 will be done. OFFSET_MAP holds the mapping from section offsets to
11871 fields for the type. FI describes the fields of the type we're
11872 processing. FIELD is the variant field we're converting. */
11873
11874 static void
11875 create_one_variant (variant &result, struct obstack *obstack,
11876 const offset_map_type &offset_map,
11877 struct field_info *fi, const variant_field &field)
11878 {
11879 result.discriminants = convert_variant_range (obstack, field, false);
11880 result.first_field = field.first_field + fi->baseclasses.size ();
11881 result.last_field = field.last_field + fi->baseclasses.size ();
11882 result.parts = create_variant_parts (obstack, offset_map, fi,
11883 field.variant_parts);
11884 }
11885
11886 /* Fill in a "struct variant_part" for a given variant part. RESULT
11887 is the variant part to fill in. OBSTACK is where any needed
11888 allocations will be done. OFFSET_MAP holds the mapping from
11889 section offsets to fields for the type. FI describes the fields of
11890 the type we're processing. BUILDER is the variant part to be
11891 converted. */
11892
11893 static void
11894 create_one_variant_part (variant_part &result,
11895 struct obstack *obstack,
11896 const offset_map_type &offset_map,
11897 struct field_info *fi,
11898 const variant_part_builder &builder)
11899 {
11900 auto iter = offset_map.find (builder.discriminant_offset);
11901 if (iter == offset_map.end ())
11902 {
11903 result.discriminant_index = -1;
11904 /* Doesn't matter. */
11905 result.is_unsigned = false;
11906 }
11907 else
11908 {
11909 result.discriminant_index = iter->second;
11910 result.is_unsigned
11911 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
11912 }
11913
11914 size_t n = builder.variants.size ();
11915 variant *output = new (obstack) variant[n];
11916 for (size_t i = 0; i < n; ++i)
11917 create_one_variant (output[i], obstack, offset_map, fi,
11918 builder.variants[i]);
11919
11920 result.variants = gdb::array_view<variant> (output, n);
11921 }
11922
11923 /* Create a vector of variant parts that can be attached to a type.
11924 OBSTACK is where any needed allocations will be done. OFFSET_MAP
11925 holds the mapping from section offsets to fields for the type. FI
11926 describes the fields of the type we're processing. VARIANT_PARTS
11927 is the vector to convert. */
11928
11929 static const gdb::array_view<variant_part>
11930 create_variant_parts (struct obstack *obstack,
11931 const offset_map_type &offset_map,
11932 struct field_info *fi,
11933 const std::vector<variant_part_builder> &variant_parts)
11934 {
11935 if (variant_parts.empty ())
11936 return {};
11937
11938 size_t n = variant_parts.size ();
11939 variant_part *result = new (obstack) variant_part[n];
11940 for (size_t i = 0; i < n; ++i)
11941 create_one_variant_part (result[i], obstack, offset_map, fi,
11942 variant_parts[i]);
11943
11944 return gdb::array_view<variant_part> (result, n);
11945 }
11946
11947 /* Compute the variant part vector for FIP, attaching it to TYPE when
11948 done. */
11949
11950 static void
11951 add_variant_property (struct field_info *fip, struct type *type,
11952 struct dwarf2_cu *cu)
11953 {
11954 /* Map section offsets of fields to their field index. Note the
11955 field index here does not take the number of baseclasses into
11956 account. */
11957 offset_map_type offset_map;
11958 for (int i = 0; i < fip->fields.size (); ++i)
11959 offset_map[fip->fields[i].offset] = i;
11960
11961 struct objfile *objfile = cu->per_objfile->objfile;
11962 gdb::array_view<const variant_part> parts
11963 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
11964 fip->variant_parts);
11965
11966 struct dynamic_prop prop;
11967 prop.set_variant_parts ((gdb::array_view<variant_part> *)
11968 obstack_copy (&objfile->objfile_obstack, &parts,
11969 sizeof (parts)));
11970
11971 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
11972 }
11973
11974 /* Create the vector of fields, and attach it to the type. */
11975
11976 static void
11977 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11978 struct dwarf2_cu *cu)
11979 {
11980 int nfields = fip->nfields ();
11981
11982 /* Record the field count, allocate space for the array of fields,
11983 and create blank accessibility bitfields if necessary. */
11984 type->set_num_fields (nfields);
11985 type->set_fields
11986 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
11987
11988 if (fip->non_public_fields && cu->lang () != language_ada)
11989 {
11990 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11991
11992 TYPE_FIELD_PRIVATE_BITS (type) =
11993 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11994 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11995
11996 TYPE_FIELD_PROTECTED_BITS (type) =
11997 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11998 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11999
12000 TYPE_FIELD_IGNORE_BITS (type) =
12001 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12002 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12003 }
12004
12005 /* If the type has baseclasses, allocate and clear a bit vector for
12006 TYPE_FIELD_VIRTUAL_BITS. */
12007 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
12008 {
12009 int num_bytes = B_BYTES (fip->baseclasses.size ());
12010 unsigned char *pointer;
12011
12012 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12013 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
12014 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12015 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
12016 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
12017 }
12018
12019 if (!fip->variant_parts.empty ())
12020 add_variant_property (fip, type, cu);
12021
12022 /* Copy the saved-up fields into the field vector. */
12023 for (int i = 0; i < nfields; ++i)
12024 {
12025 struct nextfield &field
12026 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
12027 : fip->fields[i - fip->baseclasses.size ()]);
12028
12029 type->field (i) = field.field;
12030 switch (field.accessibility)
12031 {
12032 case DW_ACCESS_private:
12033 if (cu->lang () != language_ada)
12034 SET_TYPE_FIELD_PRIVATE (type, i);
12035 break;
12036
12037 case DW_ACCESS_protected:
12038 if (cu->lang () != language_ada)
12039 SET_TYPE_FIELD_PROTECTED (type, i);
12040 break;
12041
12042 case DW_ACCESS_public:
12043 break;
12044
12045 default:
12046 /* Unknown accessibility. Complain and treat it as public. */
12047 {
12048 complaint (_("unsupported accessibility %d"),
12049 field.accessibility);
12050 }
12051 break;
12052 }
12053 if (i < fip->baseclasses.size ())
12054 {
12055 switch (field.virtuality)
12056 {
12057 case DW_VIRTUALITY_virtual:
12058 case DW_VIRTUALITY_pure_virtual:
12059 if (cu->lang () == language_ada)
12060 error (_("unexpected virtuality in component of Ada type"));
12061 SET_TYPE_FIELD_VIRTUAL (type, i);
12062 break;
12063 }
12064 }
12065 }
12066 }
12067
12068 /* Return true if this member function is a constructor, false
12069 otherwise. */
12070
12071 static int
12072 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12073 {
12074 const char *fieldname;
12075 const char *type_name;
12076 int len;
12077
12078 if (die->parent == NULL)
12079 return 0;
12080
12081 if (die->parent->tag != DW_TAG_structure_type
12082 && die->parent->tag != DW_TAG_union_type
12083 && die->parent->tag != DW_TAG_class_type)
12084 return 0;
12085
12086 fieldname = dwarf2_name (die, cu);
12087 type_name = dwarf2_name (die->parent, cu);
12088 if (fieldname == NULL || type_name == NULL)
12089 return 0;
12090
12091 len = strlen (fieldname);
12092 return (strncmp (fieldname, type_name, len) == 0
12093 && (type_name[len] == '\0' || type_name[len] == '<'));
12094 }
12095
12096 /* Add a member function to the proper fieldlist. */
12097
12098 static void
12099 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12100 struct type *type, struct dwarf2_cu *cu)
12101 {
12102 struct objfile *objfile = cu->per_objfile->objfile;
12103 struct attribute *attr;
12104 int i;
12105 struct fnfieldlist *flp = nullptr;
12106 struct fn_field *fnp;
12107 const char *fieldname;
12108 struct type *this_type;
12109
12110 if (cu->lang () == language_ada)
12111 error (_("unexpected member function in Ada type"));
12112
12113 /* Get name of member function. */
12114 fieldname = dwarf2_name (die, cu);
12115 if (fieldname == NULL)
12116 return;
12117
12118 /* Look up member function name in fieldlist. */
12119 for (i = 0; i < fip->fnfieldlists.size (); i++)
12120 {
12121 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12122 {
12123 flp = &fip->fnfieldlists[i];
12124 break;
12125 }
12126 }
12127
12128 /* Create a new fnfieldlist if necessary. */
12129 if (flp == nullptr)
12130 {
12131 fip->fnfieldlists.emplace_back ();
12132 flp = &fip->fnfieldlists.back ();
12133 flp->name = fieldname;
12134 i = fip->fnfieldlists.size () - 1;
12135 }
12136
12137 /* Create a new member function field and add it to the vector of
12138 fnfieldlists. */
12139 flp->fnfields.emplace_back ();
12140 fnp = &flp->fnfields.back ();
12141
12142 /* Delay processing of the physname until later. */
12143 if (cu->lang () == language_cplus)
12144 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
12145 die, cu);
12146 else
12147 {
12148 const char *physname = dwarf2_physname (fieldname, die, cu);
12149 fnp->physname = physname ? physname : "";
12150 }
12151
12152 fnp->type = type_allocator (objfile).new_type ();
12153 this_type = read_type_die (die, cu);
12154 if (this_type && this_type->code () == TYPE_CODE_FUNC)
12155 {
12156 int nparams = this_type->num_fields ();
12157
12158 /* TYPE is the domain of this method, and THIS_TYPE is the type
12159 of the method itself (TYPE_CODE_METHOD). */
12160 smash_to_method_type (fnp->type, type,
12161 this_type->target_type (),
12162 this_type->fields (),
12163 this_type->num_fields (),
12164 this_type->has_varargs ());
12165
12166 /* Handle static member functions.
12167 Dwarf2 has no clean way to discern C++ static and non-static
12168 member functions. G++ helps GDB by marking the first
12169 parameter for non-static member functions (which is the this
12170 pointer) as artificial. We obtain this information from
12171 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12172 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
12173 fnp->voffset = VOFFSET_STATIC;
12174 }
12175 else
12176 complaint (_("member function type missing for '%s'"),
12177 dwarf2_full_name (fieldname, die, cu));
12178
12179 /* Get fcontext from DW_AT_containing_type if present. */
12180 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12181 fnp->fcontext = die_containing_type (die, cu);
12182
12183 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12184 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12185
12186 /* Get accessibility. */
12187 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
12188 switch (accessibility)
12189 {
12190 case DW_ACCESS_private:
12191 fnp->is_private = 1;
12192 break;
12193 case DW_ACCESS_protected:
12194 fnp->is_protected = 1;
12195 break;
12196 }
12197
12198 /* Check for artificial methods. */
12199 attr = dwarf2_attr (die, DW_AT_artificial, cu);
12200 if (attr && attr->as_boolean ())
12201 fnp->is_artificial = 1;
12202
12203 /* Check for defaulted methods. */
12204 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
12205 if (attr != nullptr)
12206 fnp->defaulted = attr->defaulted ();
12207
12208 /* Check for deleted methods. */
12209 attr = dwarf2_attr (die, DW_AT_deleted, cu);
12210 if (attr != nullptr && attr->as_boolean ())
12211 fnp->is_deleted = 1;
12212
12213 fnp->is_constructor = dwarf2_is_constructor (die, cu);
12214
12215 /* Get index in virtual function table if it is a virtual member
12216 function. For older versions of GCC, this is an offset in the
12217 appropriate virtual table, as specified by DW_AT_containing_type.
12218 For everyone else, it is an expression to be evaluated relative
12219 to the object address. */
12220
12221 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12222 if (attr != nullptr)
12223 {
12224 if (attr->form_is_block () && attr->as_block ()->size > 0)
12225 {
12226 struct dwarf_block *block = attr->as_block ();
12227
12228 if (block->data[0] == DW_OP_constu)
12229 {
12230 /* Old-style GCC. */
12231 fnp->voffset = decode_locdesc (block, cu) + 2;
12232 }
12233 else if (block->data[0] == DW_OP_deref
12234 || (block->size > 1
12235 && block->data[0] == DW_OP_deref_size
12236 && block->data[1] == cu->header.addr_size))
12237 {
12238 fnp->voffset = decode_locdesc (block, cu);
12239 if ((fnp->voffset % cu->header.addr_size) != 0)
12240 dwarf2_complex_location_expr_complaint ();
12241 else
12242 fnp->voffset /= cu->header.addr_size;
12243 fnp->voffset += 2;
12244 }
12245 else
12246 dwarf2_complex_location_expr_complaint ();
12247
12248 if (!fnp->fcontext)
12249 {
12250 /* If there is no `this' field and no DW_AT_containing_type,
12251 we cannot actually find a base class context for the
12252 vtable! */
12253 if (this_type->num_fields () == 0
12254 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
12255 {
12256 complaint (_("cannot determine context for virtual member "
12257 "function \"%s\" (offset %s)"),
12258 fieldname, sect_offset_str (die->sect_off));
12259 }
12260 else
12261 {
12262 fnp->fcontext = this_type->field (0).type ()->target_type ();
12263 }
12264 }
12265 }
12266 else if (attr->form_is_section_offset ())
12267 {
12268 dwarf2_complex_location_expr_complaint ();
12269 }
12270 else
12271 {
12272 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12273 fieldname);
12274 }
12275 }
12276 else
12277 {
12278 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12279 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
12280 {
12281 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12282 complaint (_("Member function \"%s\" (offset %s) is virtual "
12283 "but the vtable offset is not specified"),
12284 fieldname, sect_offset_str (die->sect_off));
12285 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12286 TYPE_CPLUS_DYNAMIC (type) = 1;
12287 }
12288 }
12289 }
12290
12291 /* Create the vector of member function fields, and attach it to the type. */
12292
12293 static void
12294 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
12295 struct dwarf2_cu *cu)
12296 {
12297 if (cu->lang () == language_ada)
12298 error (_("unexpected member functions in Ada type"));
12299
12300 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12301 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
12302 TYPE_ALLOC (type,
12303 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
12304
12305 for (int i = 0; i < fip->fnfieldlists.size (); i++)
12306 {
12307 struct fnfieldlist &nf = fip->fnfieldlists[i];
12308 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
12309
12310 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
12311 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
12312 fn_flp->fn_fields = (struct fn_field *)
12313 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
12314
12315 for (int k = 0; k < nf.fnfields.size (); ++k)
12316 fn_flp->fn_fields[k] = nf.fnfields[k];
12317 }
12318
12319 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
12320 }
12321
12322 /* Returns non-zero if NAME is the name of a vtable member in CU's
12323 language, zero otherwise. */
12324 static int
12325 is_vtable_name (const char *name, struct dwarf2_cu *cu)
12326 {
12327 static const char vptr[] = "_vptr";
12328
12329 /* Look for the C++ form of the vtable. */
12330 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
12331 return 1;
12332
12333 return 0;
12334 }
12335
12336 /* GCC outputs unnamed structures that are really pointers to member
12337 functions, with the ABI-specified layout. If TYPE describes
12338 such a structure, smash it into a member function type.
12339
12340 GCC shouldn't do this; it should just output pointer to member DIEs.
12341 This is GCC PR debug/28767. */
12342
12343 static void
12344 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
12345 {
12346 struct type *pfn_type, *self_type, *new_type;
12347
12348 /* Check for a structure with no name and two children. */
12349 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12350 return;
12351
12352 /* Check for __pfn and __delta members. */
12353 if (type->field (0).name () == NULL
12354 || strcmp (type->field (0).name (), "__pfn") != 0
12355 || type->field (1).name () == NULL
12356 || strcmp (type->field (1).name (), "__delta") != 0)
12357 return;
12358
12359 /* Find the type of the method. */
12360 pfn_type = type->field (0).type ();
12361 if (pfn_type == NULL
12362 || pfn_type->code () != TYPE_CODE_PTR
12363 || pfn_type->target_type ()->code () != TYPE_CODE_FUNC)
12364 return;
12365
12366 /* Look for the "this" argument. */
12367 pfn_type = pfn_type->target_type ();
12368 if (pfn_type->num_fields () == 0
12369 /* || pfn_type->field (0).type () == NULL */
12370 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
12371 return;
12372
12373 self_type = pfn_type->field (0).type ()->target_type ();
12374 new_type = type_allocator (objfile).new_type ();
12375 smash_to_method_type (new_type, self_type, pfn_type->target_type (),
12376 pfn_type->fields (), pfn_type->num_fields (),
12377 pfn_type->has_varargs ());
12378 smash_to_methodptr_type (type, new_type);
12379 }
12380
12381 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
12382 requires rewriting, then copy it and return the updated copy.
12383 Otherwise return nullptr. */
12384
12385 static struct type *
12386 rewrite_array_type (struct type *type)
12387 {
12388 if (type->code () != TYPE_CODE_ARRAY)
12389 return nullptr;
12390
12391 struct type *index_type = type->index_type ();
12392 range_bounds *current_bounds = index_type->bounds ();
12393
12394 /* Handle multi-dimensional arrays. */
12395 struct type *new_target = rewrite_array_type (type->target_type ());
12396 if (new_target == nullptr)
12397 {
12398 /* Maybe we don't need to rewrite this array. */
12399 if (current_bounds->low.kind () == PROP_CONST
12400 && current_bounds->high.kind () == PROP_CONST)
12401 return nullptr;
12402 }
12403
12404 /* Either the target type was rewritten, or the bounds have to be
12405 updated. Either way we want to copy the type and update
12406 everything. */
12407 struct type *copy = copy_type (type);
12408 int nfields = copy->num_fields ();
12409 field *new_fields
12410 = ((struct field *) TYPE_ZALLOC (copy,
12411 nfields * sizeof (struct field)));
12412 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
12413 copy->set_fields (new_fields);
12414 if (new_target != nullptr)
12415 copy->set_target_type (new_target);
12416
12417 struct type *index_copy = copy_type (index_type);
12418 range_bounds *bounds
12419 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
12420 sizeof (range_bounds));
12421 *bounds = *current_bounds;
12422 bounds->low.set_const_val (1);
12423 bounds->high.set_const_val (0);
12424 index_copy->set_bounds (bounds);
12425 copy->set_index_type (index_copy);
12426
12427 return copy;
12428 }
12429
12430 /* While some versions of GCC will generate complicated DWARF for an
12431 array (see quirk_ada_thick_pointer), more recent versions were
12432 modified to emit an explicit thick pointer structure. However, in
12433 this case, the array still has DWARF expressions for its ranges,
12434 and these must be ignored. */
12435
12436 static void
12437 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
12438 struct type *type)
12439 {
12440 gdb_assert (cu->lang () == language_ada);
12441
12442 /* Check for a structure with two children. */
12443 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
12444 return;
12445
12446 /* Check for P_ARRAY and P_BOUNDS members. */
12447 if (type->field (0).name () == NULL
12448 || strcmp (type->field (0).name (), "P_ARRAY") != 0
12449 || type->field (1).name () == NULL
12450 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
12451 return;
12452
12453 /* Make sure we're looking at a pointer to an array. */
12454 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
12455 return;
12456
12457 /* The Ada code already knows how to handle these types, so all that
12458 we need to do is turn the bounds into static bounds. However, we
12459 don't want to rewrite existing array or index types in-place,
12460 because those may be referenced in other contexts where this
12461 rewriting is undesirable. */
12462 struct type *new_ary_type
12463 = rewrite_array_type (type->field (0).type ()->target_type ());
12464 if (new_ary_type != nullptr)
12465 type->field (0).set_type (lookup_pointer_type (new_ary_type));
12466 }
12467
12468 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
12469 appropriate error checking and issuing complaints if there is a
12470 problem. */
12471
12472 static ULONGEST
12473 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
12474 {
12475 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
12476
12477 if (attr == nullptr)
12478 return 0;
12479
12480 if (!attr->form_is_constant ())
12481 {
12482 complaint (_("DW_AT_alignment must have constant form"
12483 " - DIE at %s [in module %s]"),
12484 sect_offset_str (die->sect_off),
12485 objfile_name (cu->per_objfile->objfile));
12486 return 0;
12487 }
12488
12489 LONGEST val = attr->constant_value (0);
12490 if (val < 0)
12491 {
12492 complaint (_("DW_AT_alignment value must not be negative"
12493 " - DIE at %s [in module %s]"),
12494 sect_offset_str (die->sect_off),
12495 objfile_name (cu->per_objfile->objfile));
12496 return 0;
12497 }
12498 ULONGEST align = val;
12499
12500 if (align == 0)
12501 {
12502 complaint (_("DW_AT_alignment value must not be zero"
12503 " - DIE at %s [in module %s]"),
12504 sect_offset_str (die->sect_off),
12505 objfile_name (cu->per_objfile->objfile));
12506 return 0;
12507 }
12508 if ((align & (align - 1)) != 0)
12509 {
12510 complaint (_("DW_AT_alignment value must be a power of 2"
12511 " - DIE at %s [in module %s]"),
12512 sect_offset_str (die->sect_off),
12513 objfile_name (cu->per_objfile->objfile));
12514 return 0;
12515 }
12516
12517 return align;
12518 }
12519
12520 /* If the DIE has a DW_AT_alignment attribute, use its value to set
12521 the alignment for TYPE. */
12522
12523 static void
12524 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
12525 struct type *type)
12526 {
12527 if (!set_type_align (type, get_alignment (cu, die)))
12528 complaint (_("DW_AT_alignment value too large"
12529 " - DIE at %s [in module %s]"),
12530 sect_offset_str (die->sect_off),
12531 objfile_name (cu->per_objfile->objfile));
12532 }
12533
12534 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12535 constant for a type, according to DWARF5 spec, Table 5.5. */
12536
12537 static bool
12538 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
12539 {
12540 switch (value)
12541 {
12542 case DW_CC_normal:
12543 case DW_CC_pass_by_reference:
12544 case DW_CC_pass_by_value:
12545 return true;
12546
12547 default:
12548 complaint (_("unrecognized DW_AT_calling_convention value "
12549 "(%s) for a type"), pulongest (value));
12550 return false;
12551 }
12552 }
12553
12554 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12555 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
12556 also according to GNU-specific values (see include/dwarf2.h). */
12557
12558 static bool
12559 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
12560 {
12561 switch (value)
12562 {
12563 case DW_CC_normal:
12564 case DW_CC_program:
12565 case DW_CC_nocall:
12566 return true;
12567
12568 case DW_CC_GNU_renesas_sh:
12569 case DW_CC_GNU_borland_fastcall_i386:
12570 case DW_CC_GDB_IBM_OpenCL:
12571 return true;
12572
12573 default:
12574 complaint (_("unrecognized DW_AT_calling_convention value "
12575 "(%s) for a subroutine"), pulongest (value));
12576 return false;
12577 }
12578 }
12579
12580 /* Called when we find the DIE that starts a structure or union scope
12581 (definition) to create a type for the structure or union. Fill in
12582 the type's name and general properties; the members will not be
12583 processed until process_structure_scope. A symbol table entry for
12584 the type will also not be done until process_structure_scope (assuming
12585 the type has a name).
12586
12587 NOTE: we need to call these functions regardless of whether or not the
12588 DIE has a DW_AT_name attribute, since it might be an anonymous
12589 structure or union. This gets the type entered into our set of
12590 user defined types. */
12591
12592 static struct type *
12593 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
12594 {
12595 struct objfile *objfile = cu->per_objfile->objfile;
12596 struct type *type;
12597 struct attribute *attr;
12598 const char *name;
12599
12600 /* If the definition of this type lives in .debug_types, read that type.
12601 Don't follow DW_AT_specification though, that will take us back up
12602 the chain and we want to go down. */
12603 attr = die->attr (DW_AT_signature);
12604 if (attr != nullptr)
12605 {
12606 type = get_DW_AT_signature_type (die, attr, cu);
12607
12608 /* The type's CU may not be the same as CU.
12609 Ensure TYPE is recorded with CU in die_type_hash. */
12610 return set_die_type (die, type, cu);
12611 }
12612
12613 type = type_allocator (objfile).new_type ();
12614 INIT_CPLUS_SPECIFIC (type);
12615
12616 name = dwarf2_name (die, cu);
12617 if (name != NULL)
12618 {
12619 if (cu->lang () == language_cplus
12620 || cu->lang () == language_d
12621 || cu->lang () == language_rust)
12622 {
12623 const char *full_name = dwarf2_full_name (name, die, cu);
12624
12625 /* dwarf2_full_name might have already finished building the DIE's
12626 type. If so, there is no need to continue. */
12627 if (get_die_type (die, cu) != NULL)
12628 return get_die_type (die, cu);
12629
12630 type->set_name (full_name);
12631 }
12632 else
12633 {
12634 /* The name is already allocated along with this objfile, so
12635 we don't need to duplicate it for the type. */
12636 type->set_name (name);
12637 }
12638 }
12639
12640 if (die->tag == DW_TAG_structure_type)
12641 {
12642 type->set_code (TYPE_CODE_STRUCT);
12643 }
12644 else if (die->tag == DW_TAG_union_type)
12645 {
12646 type->set_code (TYPE_CODE_UNION);
12647 }
12648 else if (die->tag == DW_TAG_namelist)
12649 {
12650 type->set_code (TYPE_CODE_NAMELIST);
12651 }
12652 else
12653 {
12654 type->set_code (TYPE_CODE_STRUCT);
12655 }
12656
12657 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
12658 type->set_is_declared_class (true);
12659
12660 /* Store the calling convention in the type if it's available in
12661 the die. Otherwise the calling convention remains set to
12662 the default value DW_CC_normal. */
12663 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12664 if (attr != nullptr
12665 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
12666 {
12667 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12668 TYPE_CPLUS_CALLING_CONVENTION (type)
12669 = (enum dwarf_calling_convention) (attr->constant_value (0));
12670 }
12671
12672 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12673 if (attr != nullptr)
12674 {
12675 if (attr->form_is_constant ())
12676 type->set_length (attr->constant_value (0));
12677 else
12678 {
12679 struct dynamic_prop prop;
12680 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
12681 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
12682
12683 type->set_length (0);
12684 }
12685 }
12686 else
12687 type->set_length (0);
12688
12689 maybe_set_alignment (cu, die, type);
12690
12691 if (producer_is_icc_lt_14 (cu) && (type->length () == 0))
12692 {
12693 /* ICC<14 does not output the required DW_AT_declaration on
12694 incomplete types, but gives them a size of zero. */
12695 type->set_is_stub (true);
12696 }
12697 else
12698 type->set_stub_is_supported (true);
12699
12700 if (die_is_declaration (die, cu))
12701 type->set_is_stub (true);
12702 else if (attr == NULL && die->child == NULL
12703 && producer_is_realview (cu->producer))
12704 /* RealView does not output the required DW_AT_declaration
12705 on incomplete types. */
12706 type->set_is_stub (true);
12707
12708 /* We need to add the type field to the die immediately so we don't
12709 infinitely recurse when dealing with pointers to the structure
12710 type within the structure itself. */
12711 set_die_type (die, type, cu);
12712
12713 /* set_die_type should be already done. */
12714 set_descriptive_type (type, die, cu);
12715
12716 return type;
12717 }
12718
12719 static void handle_struct_member_die
12720 (struct die_info *child_die,
12721 struct type *type,
12722 struct field_info *fi,
12723 std::vector<struct symbol *> *template_args,
12724 struct dwarf2_cu *cu);
12725
12726 /* A helper for handle_struct_member_die that handles
12727 DW_TAG_variant_part. */
12728
12729 static void
12730 handle_variant_part (struct die_info *die, struct type *type,
12731 struct field_info *fi,
12732 std::vector<struct symbol *> *template_args,
12733 struct dwarf2_cu *cu)
12734 {
12735 variant_part_builder *new_part;
12736 if (fi->current_variant_part == nullptr)
12737 {
12738 fi->variant_parts.emplace_back ();
12739 new_part = &fi->variant_parts.back ();
12740 }
12741 else if (!fi->current_variant_part->processing_variant)
12742 {
12743 complaint (_("nested DW_TAG_variant_part seen "
12744 "- DIE at %s [in module %s]"),
12745 sect_offset_str (die->sect_off),
12746 objfile_name (cu->per_objfile->objfile));
12747 return;
12748 }
12749 else
12750 {
12751 variant_field &current = fi->current_variant_part->variants.back ();
12752 current.variant_parts.emplace_back ();
12753 new_part = &current.variant_parts.back ();
12754 }
12755
12756 /* When we recurse, we want callees to add to this new variant
12757 part. */
12758 scoped_restore save_current_variant_part
12759 = make_scoped_restore (&fi->current_variant_part, new_part);
12760
12761 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
12762 if (discr == NULL)
12763 {
12764 /* It's a univariant form, an extension we support. */
12765 }
12766 else if (discr->form_is_ref ())
12767 {
12768 struct dwarf2_cu *target_cu = cu;
12769 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
12770
12771 new_part->discriminant_offset = target_die->sect_off;
12772 }
12773 else
12774 {
12775 complaint (_("DW_AT_discr does not have DIE reference form"
12776 " - DIE at %s [in module %s]"),
12777 sect_offset_str (die->sect_off),
12778 objfile_name (cu->per_objfile->objfile));
12779 }
12780
12781 for (die_info *child_die = die->child;
12782 child_die != NULL;
12783 child_die = child_die->sibling)
12784 handle_struct_member_die (child_die, type, fi, template_args, cu);
12785 }
12786
12787 /* A helper for handle_struct_member_die that handles
12788 DW_TAG_variant. */
12789
12790 static void
12791 handle_variant (struct die_info *die, struct type *type,
12792 struct field_info *fi,
12793 std::vector<struct symbol *> *template_args,
12794 struct dwarf2_cu *cu)
12795 {
12796 if (fi->current_variant_part == nullptr)
12797 {
12798 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
12799 "- DIE at %s [in module %s]"),
12800 sect_offset_str (die->sect_off),
12801 objfile_name (cu->per_objfile->objfile));
12802 return;
12803 }
12804 if (fi->current_variant_part->processing_variant)
12805 {
12806 complaint (_("nested DW_TAG_variant seen "
12807 "- DIE at %s [in module %s]"),
12808 sect_offset_str (die->sect_off),
12809 objfile_name (cu->per_objfile->objfile));
12810 return;
12811 }
12812
12813 scoped_restore save_processing_variant
12814 = make_scoped_restore (&fi->current_variant_part->processing_variant,
12815 true);
12816
12817 fi->current_variant_part->variants.emplace_back ();
12818 variant_field &variant = fi->current_variant_part->variants.back ();
12819 variant.first_field = fi->fields.size ();
12820
12821 /* In a variant we want to get the discriminant and also add a
12822 field for our sole member child. */
12823 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
12824 if (discr == nullptr || !discr->form_is_constant ())
12825 {
12826 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
12827 if (discr == nullptr || discr->as_block ()->size == 0)
12828 variant.default_branch = true;
12829 else
12830 variant.discr_list_data = discr->as_block ();
12831 }
12832 else
12833 variant.discriminant_value = discr->constant_value (0);
12834
12835 for (die_info *variant_child = die->child;
12836 variant_child != NULL;
12837 variant_child = variant_child->sibling)
12838 handle_struct_member_die (variant_child, type, fi, template_args, cu);
12839
12840 variant.last_field = fi->fields.size ();
12841 }
12842
12843 /* A helper for process_structure_scope that handles a single member
12844 DIE. */
12845
12846 static void
12847 handle_struct_member_die (struct die_info *child_die, struct type *type,
12848 struct field_info *fi,
12849 std::vector<struct symbol *> *template_args,
12850 struct dwarf2_cu *cu)
12851 {
12852 if (child_die->tag == DW_TAG_member
12853 || child_die->tag == DW_TAG_variable
12854 || child_die->tag == DW_TAG_namelist_item)
12855 {
12856 /* NOTE: carlton/2002-11-05: A C++ static data member
12857 should be a DW_TAG_member that is a declaration, but
12858 all versions of G++ as of this writing (so through at
12859 least 3.2.1) incorrectly generate DW_TAG_variable
12860 tags for them instead. */
12861 dwarf2_add_field (fi, child_die, cu);
12862 }
12863 else if (child_die->tag == DW_TAG_subprogram)
12864 {
12865 /* Rust doesn't have member functions in the C++ sense.
12866 However, it does emit ordinary functions as children
12867 of a struct DIE. */
12868 if (cu->lang () == language_rust)
12869 read_func_scope (child_die, cu);
12870 else
12871 {
12872 /* C++ member function. */
12873 dwarf2_add_member_fn (fi, child_die, type, cu);
12874 }
12875 }
12876 else if (child_die->tag == DW_TAG_inheritance)
12877 {
12878 /* C++ base class field. */
12879 dwarf2_add_field (fi, child_die, cu);
12880 }
12881 else if (type_can_define_types (child_die))
12882 dwarf2_add_type_defn (fi, child_die, cu);
12883 else if (child_die->tag == DW_TAG_template_type_param
12884 || child_die->tag == DW_TAG_template_value_param)
12885 {
12886 struct symbol *arg = new_symbol (child_die, NULL, cu);
12887
12888 if (arg != NULL)
12889 template_args->push_back (arg);
12890 }
12891 else if (child_die->tag == DW_TAG_variant_part)
12892 handle_variant_part (child_die, type, fi, template_args, cu);
12893 else if (child_die->tag == DW_TAG_variant)
12894 handle_variant (child_die, type, fi, template_args, cu);
12895 }
12896
12897 /* Finish creating a structure or union type, including filling in its
12898 members and creating a symbol for it. This function also handles Fortran
12899 namelist variables, their items or members and creating a symbol for
12900 them. */
12901
12902 static void
12903 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12904 {
12905 struct objfile *objfile = cu->per_objfile->objfile;
12906 struct die_info *child_die;
12907 struct type *type;
12908
12909 type = get_die_type (die, cu);
12910 if (type == NULL)
12911 type = read_structure_type (die, cu);
12912
12913 bool has_template_parameters = false;
12914 if (die->child != NULL && ! die_is_declaration (die, cu))
12915 {
12916 struct field_info fi;
12917 std::vector<struct symbol *> template_args;
12918
12919 child_die = die->child;
12920
12921 while (child_die && child_die->tag)
12922 {
12923 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
12924 child_die = child_die->sibling;
12925 }
12926
12927 /* Attach template arguments to type. */
12928 if (!template_args.empty ())
12929 {
12930 has_template_parameters = true;
12931 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12932 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
12933 TYPE_TEMPLATE_ARGUMENTS (type)
12934 = XOBNEWVEC (&objfile->objfile_obstack,
12935 struct symbol *,
12936 TYPE_N_TEMPLATE_ARGUMENTS (type));
12937 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12938 template_args.data (),
12939 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12940 * sizeof (struct symbol *)));
12941 }
12942
12943 /* Attach fields and member functions to the type. */
12944 if (fi.nfields () > 0)
12945 dwarf2_attach_fields_to_type (&fi, type, cu);
12946 if (!fi.fnfieldlists.empty ())
12947 {
12948 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12949
12950 /* Get the type which refers to the base class (possibly this
12951 class itself) which contains the vtable pointer for the current
12952 class from the DW_AT_containing_type attribute. This use of
12953 DW_AT_containing_type is a GNU extension. */
12954
12955 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12956 {
12957 struct type *t = die_containing_type (die, cu);
12958
12959 set_type_vptr_basetype (type, t);
12960 if (type == t)
12961 {
12962 int i;
12963
12964 /* Our own class provides vtbl ptr. */
12965 for (i = t->num_fields () - 1;
12966 i >= TYPE_N_BASECLASSES (t);
12967 --i)
12968 {
12969 const char *fieldname = t->field (i).name ();
12970
12971 if (is_vtable_name (fieldname, cu))
12972 {
12973 set_type_vptr_fieldno (type, i);
12974 break;
12975 }
12976 }
12977
12978 /* Complain if virtual function table field not found. */
12979 if (i < TYPE_N_BASECLASSES (t))
12980 complaint (_("virtual function table pointer "
12981 "not found when defining class '%s'"),
12982 type->name () ? type->name () : "");
12983 }
12984 else
12985 {
12986 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
12987 }
12988 }
12989 else if (cu->producer
12990 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
12991 {
12992 /* The IBM XLC compiler does not provide direct indication
12993 of the containing type, but the vtable pointer is
12994 always named __vfp. */
12995
12996 int i;
12997
12998 for (i = type->num_fields () - 1;
12999 i >= TYPE_N_BASECLASSES (type);
13000 --i)
13001 {
13002 if (strcmp (type->field (i).name (), "__vfp") == 0)
13003 {
13004 set_type_vptr_fieldno (type, i);
13005 set_type_vptr_basetype (type, type);
13006 break;
13007 }
13008 }
13009 }
13010 }
13011
13012 /* Copy fi.typedef_field_list linked list elements content into the
13013 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13014 if (!fi.typedef_field_list.empty ())
13015 {
13016 int count = fi.typedef_field_list.size ();
13017
13018 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13019 TYPE_TYPEDEF_FIELD_ARRAY (type)
13020 = ((struct decl_field *)
13021 TYPE_ALLOC (type,
13022 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
13023 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
13024
13025 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
13026 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
13027 }
13028
13029 /* Copy fi.nested_types_list linked list elements content into the
13030 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
13031 if (!fi.nested_types_list.empty ()
13032 && cu->lang () != language_ada)
13033 {
13034 int count = fi.nested_types_list.size ();
13035
13036 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13037 TYPE_NESTED_TYPES_ARRAY (type)
13038 = ((struct decl_field *)
13039 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
13040 TYPE_NESTED_TYPES_COUNT (type) = count;
13041
13042 for (int i = 0; i < fi.nested_types_list.size (); ++i)
13043 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
13044 }
13045 }
13046
13047 quirk_gcc_member_function_pointer (type, objfile);
13048 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
13049 cu->rust_unions.push_back (type);
13050 else if (cu->lang () == language_ada)
13051 quirk_ada_thick_pointer_struct (die, cu, type);
13052
13053 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13054 snapshots) has been known to create a die giving a declaration
13055 for a class that has, as a child, a die giving a definition for a
13056 nested class. So we have to process our children even if the
13057 current die is a declaration. Normally, of course, a declaration
13058 won't have any children at all. */
13059
13060 child_die = die->child;
13061
13062 while (child_die != NULL && child_die->tag)
13063 {
13064 if (child_die->tag == DW_TAG_member
13065 || child_die->tag == DW_TAG_variable
13066 || child_die->tag == DW_TAG_inheritance
13067 || child_die->tag == DW_TAG_template_value_param
13068 || child_die->tag == DW_TAG_template_type_param)
13069 {
13070 /* Do nothing. */
13071 }
13072 else
13073 process_die (child_die, cu);
13074
13075 child_die = child_die->sibling;
13076 }
13077
13078 /* Do not consider external references. According to the DWARF standard,
13079 these DIEs are identified by the fact that they have no byte_size
13080 attribute, and a declaration attribute. */
13081 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13082 || !die_is_declaration (die, cu)
13083 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
13084 {
13085 struct symbol *sym = new_symbol (die, type, cu);
13086
13087 if (has_template_parameters)
13088 {
13089 struct symtab *symtab;
13090 if (sym != nullptr)
13091 symtab = sym->symtab ();
13092 else if (cu->line_header != nullptr)
13093 {
13094 /* Any related symtab will do. */
13095 symtab
13096 = cu->line_header->file_names ()[0].symtab;
13097 }
13098 else
13099 {
13100 symtab = nullptr;
13101 complaint (_("could not find suitable "
13102 "symtab for template parameter"
13103 " - DIE at %s [in module %s]"),
13104 sect_offset_str (die->sect_off),
13105 objfile_name (objfile));
13106 }
13107
13108 if (symtab != nullptr)
13109 {
13110 /* Make sure that the symtab is set on the new symbols.
13111 Even though they don't appear in this symtab directly,
13112 other parts of gdb assume that symbols do, and this is
13113 reasonably true. */
13114 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
13115 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
13116 }
13117 }
13118 }
13119 }
13120
13121 /* Assuming DIE is an enumeration type, and TYPE is its associated
13122 type, update TYPE using some information only available in DIE's
13123 children. In particular, the fields are computed. */
13124
13125 static void
13126 update_enumeration_type_from_children (struct die_info *die,
13127 struct type *type,
13128 struct dwarf2_cu *cu)
13129 {
13130 struct die_info *child_die;
13131 int unsigned_enum = 1;
13132 int flag_enum = 1;
13133
13134 auto_obstack obstack;
13135 std::vector<struct field> fields;
13136
13137 for (child_die = die->child;
13138 child_die != NULL && child_die->tag;
13139 child_die = child_die->sibling)
13140 {
13141 struct attribute *attr;
13142 LONGEST value;
13143 const gdb_byte *bytes;
13144 struct dwarf2_locexpr_baton *baton;
13145 const char *name;
13146
13147 if (child_die->tag != DW_TAG_enumerator)
13148 continue;
13149
13150 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13151 if (attr == NULL)
13152 continue;
13153
13154 name = dwarf2_name (child_die, cu);
13155 if (name == NULL)
13156 name = "<anonymous enumerator>";
13157
13158 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13159 &value, &bytes, &baton);
13160 if (value < 0)
13161 {
13162 unsigned_enum = 0;
13163 flag_enum = 0;
13164 }
13165 else
13166 {
13167 if (count_one_bits_ll (value) >= 2)
13168 flag_enum = 0;
13169 }
13170
13171 fields.emplace_back ();
13172 struct field &field = fields.back ();
13173 field.set_name (dwarf2_physname (name, child_die, cu));
13174 field.set_loc_enumval (value);
13175 }
13176
13177 if (!fields.empty ())
13178 {
13179 type->set_num_fields (fields.size ());
13180 type->set_fields
13181 ((struct field *)
13182 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
13183 memcpy (type->fields (), fields.data (),
13184 sizeof (struct field) * fields.size ());
13185 }
13186 else
13187 flag_enum = 0;
13188
13189 if (unsigned_enum)
13190 type->set_is_unsigned (true);
13191
13192 if (flag_enum)
13193 type->set_is_flag_enum (true);
13194 }
13195
13196 /* Given a DW_AT_enumeration_type die, set its type. We do not
13197 complete the type's fields yet, or create any symbols. */
13198
13199 static struct type *
13200 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13201 {
13202 struct objfile *objfile = cu->per_objfile->objfile;
13203 struct type *type;
13204 struct attribute *attr;
13205 const char *name;
13206
13207 /* If the definition of this type lives in .debug_types, read that type.
13208 Don't follow DW_AT_specification though, that will take us back up
13209 the chain and we want to go down. */
13210 attr = die->attr (DW_AT_signature);
13211 if (attr != nullptr)
13212 {
13213 type = get_DW_AT_signature_type (die, attr, cu);
13214
13215 /* The type's CU may not be the same as CU.
13216 Ensure TYPE is recorded with CU in die_type_hash. */
13217 return set_die_type (die, type, cu);
13218 }
13219
13220 type = type_allocator (objfile).new_type ();
13221
13222 type->set_code (TYPE_CODE_ENUM);
13223 name = dwarf2_full_name (NULL, die, cu);
13224 if (name != NULL)
13225 type->set_name (name);
13226
13227 attr = dwarf2_attr (die, DW_AT_type, cu);
13228 if (attr != NULL)
13229 {
13230 struct type *underlying_type = die_type (die, cu);
13231
13232 type->set_target_type (underlying_type);
13233 }
13234
13235 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13236 if (attr != nullptr)
13237 type->set_length (attr->constant_value (0));
13238 else
13239 type->set_length (0);
13240
13241 maybe_set_alignment (cu, die, type);
13242
13243 /* The enumeration DIE can be incomplete. In Ada, any type can be
13244 declared as private in the package spec, and then defined only
13245 inside the package body. Such types are known as Taft Amendment
13246 Types. When another package uses such a type, an incomplete DIE
13247 may be generated by the compiler. */
13248 if (die_is_declaration (die, cu))
13249 type->set_is_stub (true);
13250
13251 /* If this type has an underlying type that is not a stub, then we
13252 may use its attributes. We always use the "unsigned" attribute
13253 in this situation, because ordinarily we guess whether the type
13254 is unsigned -- but the guess can be wrong and the underlying type
13255 can tell us the reality. However, we defer to a local size
13256 attribute if one exists, because this lets the compiler override
13257 the underlying type if needed. */
13258 if (type->target_type () != NULL && !type->target_type ()->is_stub ())
13259 {
13260 struct type *underlying_type = type->target_type ();
13261 underlying_type = check_typedef (underlying_type);
13262
13263 type->set_is_unsigned (underlying_type->is_unsigned ());
13264
13265 if (type->length () == 0)
13266 type->set_length (underlying_type->length ());
13267
13268 if (TYPE_RAW_ALIGN (type) == 0
13269 && TYPE_RAW_ALIGN (underlying_type) != 0)
13270 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
13271 }
13272
13273 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
13274
13275 set_die_type (die, type, cu);
13276
13277 /* Finish the creation of this type by using the enum's children.
13278 Note that, as usual, this must come after set_die_type to avoid
13279 infinite recursion when trying to compute the names of the
13280 enumerators. */
13281 update_enumeration_type_from_children (die, type, cu);
13282
13283 return type;
13284 }
13285
13286 /* Given a pointer to a die which begins an enumeration, process all
13287 the dies that define the members of the enumeration, and create the
13288 symbol for the enumeration type.
13289
13290 NOTE: We reverse the order of the element list. */
13291
13292 static void
13293 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13294 {
13295 struct type *this_type;
13296
13297 this_type = get_die_type (die, cu);
13298 if (this_type == NULL)
13299 this_type = read_enumeration_type (die, cu);
13300
13301 if (die->child != NULL)
13302 {
13303 struct die_info *child_die;
13304 const char *name;
13305
13306 child_die = die->child;
13307 while (child_die && child_die->tag)
13308 {
13309 if (child_die->tag != DW_TAG_enumerator)
13310 {
13311 process_die (child_die, cu);
13312 }
13313 else
13314 {
13315 name = dwarf2_name (child_die, cu);
13316 if (name)
13317 new_symbol (child_die, this_type, cu);
13318 }
13319
13320 child_die = child_die->sibling;
13321 }
13322 }
13323
13324 /* If we are reading an enum from a .debug_types unit, and the enum
13325 is a declaration, and the enum is not the signatured type in the
13326 unit, then we do not want to add a symbol for it. Adding a
13327 symbol would in some cases obscure the true definition of the
13328 enum, giving users an incomplete type when the definition is
13329 actually available. Note that we do not want to do this for all
13330 enums which are just declarations, because C++0x allows forward
13331 enum declarations. */
13332 if (cu->per_cu->is_debug_types
13333 && die_is_declaration (die, cu))
13334 {
13335 struct signatured_type *sig_type;
13336
13337 sig_type = (struct signatured_type *) cu->per_cu;
13338 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
13339 if (sig_type->type_offset_in_section != die->sect_off)
13340 return;
13341 }
13342
13343 new_symbol (die, this_type, cu);
13344 }
13345
13346 /* Helper function for quirk_ada_thick_pointer that examines a bounds
13347 expression for an index type and finds the corresponding field
13348 offset in the hidden "P_BOUNDS" structure. Returns true on success
13349 and updates *FIELD, false if it fails to recognize an
13350 expression. */
13351
13352 static bool
13353 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
13354 int *bounds_offset, struct field *field,
13355 struct dwarf2_cu *cu)
13356 {
13357 struct attribute *attr = dwarf2_attr (die, name, cu);
13358 if (attr == nullptr || !attr->form_is_block ())
13359 return false;
13360
13361 const struct dwarf_block *block = attr->as_block ();
13362 const gdb_byte *start = block->data;
13363 const gdb_byte *end = block->data + block->size;
13364
13365 /* The expression to recognize generally looks like:
13366
13367 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13368 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13369
13370 However, the second "plus_uconst" may be missing:
13371
13372 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13373 DW_OP_deref_size: 4)
13374
13375 This happens when the field is at the start of the structure.
13376
13377 Also, the final deref may not be sized:
13378
13379 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
13380 DW_OP_deref)
13381
13382 This happens when the size of the index type happens to be the
13383 same as the architecture's word size. This can occur with or
13384 without the second plus_uconst. */
13385
13386 if (end - start < 2)
13387 return false;
13388 if (*start++ != DW_OP_push_object_address)
13389 return false;
13390 if (*start++ != DW_OP_plus_uconst)
13391 return false;
13392
13393 uint64_t this_bound_off;
13394 start = gdb_read_uleb128 (start, end, &this_bound_off);
13395 if (start == nullptr || (int) this_bound_off != this_bound_off)
13396 return false;
13397 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
13398 is consistent among all bounds. */
13399 if (*bounds_offset == -1)
13400 *bounds_offset = this_bound_off;
13401 else if (*bounds_offset != this_bound_off)
13402 return false;
13403
13404 if (start == end || *start++ != DW_OP_deref)
13405 return false;
13406
13407 int offset = 0;
13408 if (start ==end)
13409 return false;
13410 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
13411 {
13412 /* This means an offset of 0. */
13413 }
13414 else if (*start++ != DW_OP_plus_uconst)
13415 return false;
13416 else
13417 {
13418 /* The size is the parameter to DW_OP_plus_uconst. */
13419 uint64_t val;
13420 start = gdb_read_uleb128 (start, end, &val);
13421 if (start == nullptr)
13422 return false;
13423 if ((int) val != val)
13424 return false;
13425 offset = val;
13426 }
13427
13428 if (start == end)
13429 return false;
13430
13431 uint64_t size;
13432 if (*start == DW_OP_deref_size)
13433 {
13434 start = gdb_read_uleb128 (start + 1, end, &size);
13435 if (start == nullptr)
13436 return false;
13437 }
13438 else if (*start == DW_OP_deref)
13439 {
13440 size = cu->header.addr_size;
13441 ++start;
13442 }
13443 else
13444 return false;
13445
13446 field->set_loc_bitpos (8 * offset);
13447 if (size != field->type ()->length ())
13448 FIELD_BITSIZE (*field) = 8 * size;
13449
13450 return true;
13451 }
13452
13453 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
13454 some kinds of Ada arrays:
13455
13456 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
13457 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
13458 <11e0> DW_AT_data_location: 2 byte block: 97 6
13459 (DW_OP_push_object_address; DW_OP_deref)
13460 <11e3> DW_AT_type : <0x1173>
13461 <11e7> DW_AT_sibling : <0x1201>
13462 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
13463 <11ec> DW_AT_type : <0x1206>
13464 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
13465 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13466 DW_OP_deref_size: 4)
13467 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
13468 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13469 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13470
13471 This actually represents a "thick pointer", which is a structure
13472 with two elements: one that is a pointer to the array data, and one
13473 that is a pointer to another structure; this second structure holds
13474 the array bounds.
13475
13476 This returns a new type on success, or nullptr if this didn't
13477 recognize the type. */
13478
13479 static struct type *
13480 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
13481 struct type *type)
13482 {
13483 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
13484 /* So far we've only seen this with block form. */
13485 if (attr == nullptr || !attr->form_is_block ())
13486 return nullptr;
13487
13488 /* Note that this will fail if the structure layout is changed by
13489 the compiler. However, we have no good way to recognize some
13490 other layout, because we don't know what expression the compiler
13491 might choose to emit should this happen. */
13492 struct dwarf_block *blk = attr->as_block ();
13493 if (blk->size != 2
13494 || blk->data[0] != DW_OP_push_object_address
13495 || blk->data[1] != DW_OP_deref)
13496 return nullptr;
13497
13498 int bounds_offset = -1;
13499 int max_align = -1;
13500 std::vector<struct field> range_fields;
13501 for (struct die_info *child_die = die->child;
13502 child_die;
13503 child_die = child_die->sibling)
13504 {
13505 if (child_die->tag == DW_TAG_subrange_type)
13506 {
13507 struct type *underlying = read_subrange_index_type (child_die, cu);
13508
13509 int this_align = type_align (underlying);
13510 if (this_align > max_align)
13511 max_align = this_align;
13512
13513 range_fields.emplace_back ();
13514 range_fields.emplace_back ();
13515
13516 struct field &lower = range_fields[range_fields.size () - 2];
13517 struct field &upper = range_fields[range_fields.size () - 1];
13518
13519 lower.set_type (underlying);
13520 FIELD_ARTIFICIAL (lower) = 1;
13521
13522 upper.set_type (underlying);
13523 FIELD_ARTIFICIAL (upper) = 1;
13524
13525 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
13526 &bounds_offset, &lower, cu)
13527 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
13528 &bounds_offset, &upper, cu))
13529 return nullptr;
13530 }
13531 }
13532
13533 /* This shouldn't really happen, but double-check that we found
13534 where the bounds are stored. */
13535 if (bounds_offset == -1)
13536 return nullptr;
13537
13538 struct objfile *objfile = cu->per_objfile->objfile;
13539 for (int i = 0; i < range_fields.size (); i += 2)
13540 {
13541 char name[20];
13542
13543 /* Set the name of each field in the bounds. */
13544 xsnprintf (name, sizeof (name), "LB%d", i / 2);
13545 range_fields[i].set_name (objfile->intern (name));
13546 xsnprintf (name, sizeof (name), "UB%d", i / 2);
13547 range_fields[i + 1].set_name (objfile->intern (name));
13548 }
13549
13550 type_allocator alloc (objfile);
13551 struct type *bounds = alloc.new_type ();
13552 bounds->set_code (TYPE_CODE_STRUCT);
13553
13554 bounds->set_num_fields (range_fields.size ());
13555 bounds->set_fields
13556 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
13557 * sizeof (struct field))));
13558 memcpy (bounds->fields (), range_fields.data (),
13559 bounds->num_fields () * sizeof (struct field));
13560
13561 int last_fieldno = range_fields.size () - 1;
13562 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
13563 + bounds->field (last_fieldno).type ()->length ());
13564 bounds->set_length (align_up (bounds_size, max_align));
13565
13566 /* Rewrite the existing array type in place. Specifically, we
13567 remove any dynamic properties we might have read, and we replace
13568 the index types. */
13569 struct type *iter = type;
13570 for (int i = 0; i < range_fields.size (); i += 2)
13571 {
13572 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
13573 iter->main_type->dyn_prop_list = nullptr;
13574 iter->set_index_type
13575 (create_static_range_type (alloc, bounds->field (i).type (), 1, 0));
13576 iter = iter->target_type ();
13577 }
13578
13579 struct type *result = type_allocator (objfile).new_type ();
13580 result->set_code (TYPE_CODE_STRUCT);
13581
13582 result->set_num_fields (2);
13583 result->set_fields
13584 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
13585 * sizeof (struct field))));
13586
13587 /* The names are chosen to coincide with what the compiler does with
13588 -fgnat-encodings=all, which the Ada code in gdb already
13589 understands. */
13590 result->field (0).set_name ("P_ARRAY");
13591 result->field (0).set_type (lookup_pointer_type (type));
13592
13593 result->field (1).set_name ("P_BOUNDS");
13594 result->field (1).set_type (lookup_pointer_type (bounds));
13595 result->field (1).set_loc_bitpos (8 * bounds_offset);
13596
13597 result->set_name (type->name ());
13598 result->set_length (result->field (0).type ()->length ()
13599 + result->field (1).type ()->length ());
13600
13601 return result;
13602 }
13603
13604 /* Extract all information from a DW_TAG_array_type DIE and put it in
13605 the DIE's type field. For now, this only handles one dimensional
13606 arrays. */
13607
13608 static struct type *
13609 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13610 {
13611 struct objfile *objfile = cu->per_objfile->objfile;
13612 struct die_info *child_die;
13613 struct type *type;
13614 struct type *element_type, *range_type, *index_type;
13615 struct attribute *attr;
13616 const char *name;
13617 struct dynamic_prop *byte_stride_prop = NULL;
13618 unsigned int bit_stride = 0;
13619
13620 element_type = die_type (die, cu);
13621
13622 /* The die_type call above may have already set the type for this DIE. */
13623 type = get_die_type (die, cu);
13624 if (type)
13625 return type;
13626
13627 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13628 if (attr != NULL)
13629 {
13630 int stride_ok;
13631 struct type *prop_type = cu->addr_sized_int_type (false);
13632
13633 byte_stride_prop
13634 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
13635 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
13636 prop_type);
13637 if (!stride_ok)
13638 {
13639 complaint (_("unable to read array DW_AT_byte_stride "
13640 " - DIE at %s [in module %s]"),
13641 sect_offset_str (die->sect_off),
13642 objfile_name (cu->per_objfile->objfile));
13643 /* Ignore this attribute. We will likely not be able to print
13644 arrays of this type correctly, but there is little we can do
13645 to help if we cannot read the attribute's value. */
13646 byte_stride_prop = NULL;
13647 }
13648 }
13649
13650 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13651 if (attr != NULL)
13652 bit_stride = attr->constant_value (0);
13653
13654 /* Irix 6.2 native cc creates array types without children for
13655 arrays with unspecified length. */
13656 if (die->child == NULL)
13657 {
13658 index_type = objfile_type (objfile)->builtin_int;
13659 type_allocator alloc (objfile);
13660 range_type = create_static_range_type (alloc, index_type, 0, -1);
13661 type = create_array_type_with_stride (alloc, element_type, range_type,
13662 byte_stride_prop, bit_stride);
13663 return set_die_type (die, type, cu);
13664 }
13665
13666 std::vector<struct type *> range_types;
13667 child_die = die->child;
13668 while (child_die && child_die->tag)
13669 {
13670 if (child_die->tag == DW_TAG_subrange_type
13671 || child_die->tag == DW_TAG_generic_subrange)
13672 {
13673 struct type *child_type = read_type_die (child_die, cu);
13674
13675 if (child_type != NULL)
13676 {
13677 /* The range type was succesfully read. Save it for the
13678 array type creation. */
13679 range_types.push_back (child_type);
13680 }
13681 }
13682 child_die = child_die->sibling;
13683 }
13684
13685 if (range_types.empty ())
13686 {
13687 complaint (_("unable to find array range - DIE at %s [in module %s]"),
13688 sect_offset_str (die->sect_off),
13689 objfile_name (cu->per_objfile->objfile));
13690 return NULL;
13691 }
13692
13693 /* Dwarf2 dimensions are output from left to right, create the
13694 necessary array types in backwards order. */
13695
13696 type = element_type;
13697
13698 type_allocator alloc (cu->per_objfile->objfile);
13699 if (read_array_order (die, cu) == DW_ORD_col_major)
13700 {
13701 int i = 0;
13702
13703 while (i < range_types.size ())
13704 {
13705 type = create_array_type_with_stride (alloc, type, range_types[i++],
13706 byte_stride_prop, bit_stride);
13707 type->set_is_multi_dimensional (true);
13708 bit_stride = 0;
13709 byte_stride_prop = nullptr;
13710 }
13711 }
13712 else
13713 {
13714 size_t ndim = range_types.size ();
13715 while (ndim-- > 0)
13716 {
13717 type = create_array_type_with_stride (alloc, type, range_types[ndim],
13718 byte_stride_prop, bit_stride);
13719 type->set_is_multi_dimensional (true);
13720 bit_stride = 0;
13721 byte_stride_prop = nullptr;
13722 }
13723 }
13724
13725 /* Clear the flag on the outermost array type. */
13726 type->set_is_multi_dimensional (false);
13727 gdb_assert (type != element_type);
13728
13729 /* Understand Dwarf2 support for vector types (like they occur on
13730 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13731 array type. This is not part of the Dwarf2/3 standard yet, but a
13732 custom vendor extension. The main difference between a regular
13733 array and the vector variant is that vectors are passed by value
13734 to functions. */
13735 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13736 if (attr != nullptr)
13737 make_vector_type (type);
13738
13739 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13740 implementation may choose to implement triple vectors using this
13741 attribute. */
13742 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13743 if (attr != nullptr && attr->form_is_unsigned ())
13744 {
13745 if (attr->as_unsigned () >= type->length ())
13746 type->set_length (attr->as_unsigned ());
13747 else
13748 complaint (_("DW_AT_byte_size for array type smaller "
13749 "than the total size of elements"));
13750 }
13751
13752 name = dwarf2_name (die, cu);
13753 if (name)
13754 type->set_name (name);
13755
13756 maybe_set_alignment (cu, die, type);
13757
13758 struct type *replacement_type = nullptr;
13759 if (cu->lang () == language_ada)
13760 {
13761 replacement_type = quirk_ada_thick_pointer (die, cu, type);
13762 if (replacement_type != nullptr)
13763 type = replacement_type;
13764 }
13765
13766 /* Install the type in the die. */
13767 set_die_type (die, type, cu, replacement_type != nullptr);
13768
13769 /* set_die_type should be already done. */
13770 set_descriptive_type (type, die, cu);
13771
13772 return type;
13773 }
13774
13775 static enum dwarf_array_dim_ordering
13776 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13777 {
13778 struct attribute *attr;
13779
13780 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13781
13782 if (attr != nullptr)
13783 {
13784 LONGEST val = attr->constant_value (-1);
13785 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
13786 return (enum dwarf_array_dim_ordering) val;
13787 }
13788
13789 /* GNU F77 is a special case, as at 08/2004 array type info is the
13790 opposite order to the dwarf2 specification, but data is still
13791 laid out as per normal fortran.
13792
13793 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13794 version checking. */
13795
13796 if (cu->lang () == language_fortran
13797 && cu->producer && strstr (cu->producer, "GNU F77"))
13798 {
13799 return DW_ORD_row_major;
13800 }
13801
13802 switch (cu->language_defn->array_ordering ())
13803 {
13804 case array_column_major:
13805 return DW_ORD_col_major;
13806 case array_row_major:
13807 default:
13808 return DW_ORD_row_major;
13809 };
13810 }
13811
13812 /* Extract all information from a DW_TAG_set_type DIE and put it in
13813 the DIE's type field. */
13814
13815 static struct type *
13816 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13817 {
13818 struct type *domain_type, *set_type;
13819 struct attribute *attr;
13820
13821 domain_type = die_type (die, cu);
13822
13823 /* The die_type call above may have already set the type for this DIE. */
13824 set_type = get_die_type (die, cu);
13825 if (set_type)
13826 return set_type;
13827
13828 set_type = create_set_type (NULL, domain_type);
13829
13830 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13831 if (attr != nullptr && attr->form_is_unsigned ())
13832 set_type->set_length (attr->as_unsigned ());
13833
13834 maybe_set_alignment (cu, die, set_type);
13835
13836 return set_die_type (die, set_type, cu);
13837 }
13838
13839 /* A helper for read_common_block that creates a locexpr baton.
13840 SYM is the symbol which we are marking as computed.
13841 COMMON_DIE is the DIE for the common block.
13842 COMMON_LOC is the location expression attribute for the common
13843 block itself.
13844 MEMBER_LOC is the location expression attribute for the particular
13845 member of the common block that we are processing.
13846 CU is the CU from which the above come. */
13847
13848 static void
13849 mark_common_block_symbol_computed (struct symbol *sym,
13850 struct die_info *common_die,
13851 struct attribute *common_loc,
13852 struct attribute *member_loc,
13853 struct dwarf2_cu *cu)
13854 {
13855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13856 struct objfile *objfile = per_objfile->objfile;
13857 struct dwarf2_locexpr_baton *baton;
13858 gdb_byte *ptr;
13859 unsigned int cu_off;
13860 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
13861 LONGEST offset = 0;
13862
13863 gdb_assert (common_loc && member_loc);
13864 gdb_assert (common_loc->form_is_block ());
13865 gdb_assert (member_loc->form_is_block ()
13866 || member_loc->form_is_constant ());
13867
13868 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13869 baton->per_objfile = per_objfile;
13870 baton->per_cu = cu->per_cu;
13871 gdb_assert (baton->per_cu);
13872
13873 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13874
13875 if (member_loc->form_is_constant ())
13876 {
13877 offset = member_loc->constant_value (0);
13878 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13879 }
13880 else
13881 baton->size += member_loc->as_block ()->size;
13882
13883 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
13884 baton->data = ptr;
13885
13886 *ptr++ = DW_OP_call4;
13887 cu_off = common_die->sect_off - cu->per_cu->sect_off;
13888 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13889 ptr += 4;
13890
13891 if (member_loc->form_is_constant ())
13892 {
13893 *ptr++ = DW_OP_addr;
13894 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13895 ptr += cu->header.addr_size;
13896 }
13897 else
13898 {
13899 /* We have to copy the data here, because DW_OP_call4 will only
13900 use a DW_AT_location attribute. */
13901 struct dwarf_block *block = member_loc->as_block ();
13902 memcpy (ptr, block->data, block->size);
13903 ptr += block->size;
13904 }
13905
13906 *ptr++ = DW_OP_plus;
13907 gdb_assert (ptr - baton->data == baton->size);
13908
13909 SYMBOL_LOCATION_BATON (sym) = baton;
13910 sym->set_aclass_index (dwarf2_locexpr_index);
13911 }
13912
13913 /* Create appropriate locally-scoped variables for all the
13914 DW_TAG_common_block entries. Also create a struct common_block
13915 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13916 is used to separate the common blocks name namespace from regular
13917 variable names. */
13918
13919 static void
13920 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13921 {
13922 struct attribute *attr;
13923
13924 attr = dwarf2_attr (die, DW_AT_location, cu);
13925 if (attr != nullptr)
13926 {
13927 /* Support the .debug_loc offsets. */
13928 if (attr->form_is_block ())
13929 {
13930 /* Ok. */
13931 }
13932 else if (attr->form_is_section_offset ())
13933 {
13934 dwarf2_complex_location_expr_complaint ();
13935 attr = NULL;
13936 }
13937 else
13938 {
13939 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13940 "common block member");
13941 attr = NULL;
13942 }
13943 }
13944
13945 if (die->child != NULL)
13946 {
13947 struct objfile *objfile = cu->per_objfile->objfile;
13948 struct die_info *child_die;
13949 size_t n_entries = 0, size;
13950 struct common_block *common_block;
13951 struct symbol *sym;
13952
13953 for (child_die = die->child;
13954 child_die && child_die->tag;
13955 child_die = child_die->sibling)
13956 ++n_entries;
13957
13958 size = (sizeof (struct common_block)
13959 + (n_entries - 1) * sizeof (struct symbol *));
13960 common_block
13961 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
13962 size);
13963 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13964 common_block->n_entries = 0;
13965
13966 for (child_die = die->child;
13967 child_die && child_die->tag;
13968 child_die = child_die->sibling)
13969 {
13970 /* Create the symbol in the DW_TAG_common_block block in the current
13971 symbol scope. */
13972 sym = new_symbol (child_die, NULL, cu);
13973 if (sym != NULL)
13974 {
13975 struct attribute *member_loc;
13976
13977 common_block->contents[common_block->n_entries++] = sym;
13978
13979 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13980 cu);
13981 if (member_loc)
13982 {
13983 /* GDB has handled this for a long time, but it is
13984 not specified by DWARF. It seems to have been
13985 emitted by gfortran at least as recently as:
13986 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
13987 complaint (_("Variable in common block has "
13988 "DW_AT_data_member_location "
13989 "- DIE at %s [in module %s]"),
13990 sect_offset_str (child_die->sect_off),
13991 objfile_name (objfile));
13992
13993 if (member_loc->form_is_section_offset ())
13994 dwarf2_complex_location_expr_complaint ();
13995 else if (member_loc->form_is_constant ()
13996 || member_loc->form_is_block ())
13997 {
13998 if (attr != nullptr)
13999 mark_common_block_symbol_computed (sym, die, attr,
14000 member_loc, cu);
14001 }
14002 else
14003 dwarf2_complex_location_expr_complaint ();
14004 }
14005 }
14006 }
14007
14008 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14009 sym->set_value_common_block (common_block);
14010 }
14011 }
14012
14013 /* Create a type for a C++ namespace. */
14014
14015 static struct type *
14016 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14017 {
14018 struct objfile *objfile = cu->per_objfile->objfile;
14019 const char *previous_prefix, *name;
14020 int is_anonymous;
14021 struct type *type;
14022
14023 /* For extensions, reuse the type of the original namespace. */
14024 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14025 {
14026 struct die_info *ext_die;
14027 struct dwarf2_cu *ext_cu = cu;
14028
14029 ext_die = dwarf2_extension (die, &ext_cu);
14030 type = read_type_die (ext_die, ext_cu);
14031
14032 /* EXT_CU may not be the same as CU.
14033 Ensure TYPE is recorded with CU in die_type_hash. */
14034 return set_die_type (die, type, cu);
14035 }
14036
14037 name = namespace_name (die, &is_anonymous, cu);
14038
14039 /* Now build the name of the current namespace. */
14040
14041 previous_prefix = determine_prefix (die, cu);
14042 if (previous_prefix[0] != '\0')
14043 name = typename_concat (&objfile->objfile_obstack,
14044 previous_prefix, name, 0, cu);
14045
14046 /* Create the type. */
14047 type = type_allocator (objfile).new_type (TYPE_CODE_NAMESPACE, 0, name);
14048
14049 return set_die_type (die, type, cu);
14050 }
14051
14052 /* Read a namespace scope. */
14053
14054 static void
14055 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14056 {
14057 struct objfile *objfile = cu->per_objfile->objfile;
14058 int is_anonymous;
14059
14060 /* Add a symbol associated to this if we haven't seen the namespace
14061 before. Also, add a using directive if it's an anonymous
14062 namespace. */
14063
14064 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14065 {
14066 struct type *type;
14067
14068 type = read_type_die (die, cu);
14069 new_symbol (die, type, cu);
14070
14071 namespace_name (die, &is_anonymous, cu);
14072 if (is_anonymous)
14073 {
14074 const char *previous_prefix = determine_prefix (die, cu);
14075
14076 std::vector<const char *> excludes;
14077 add_using_directive (using_directives (cu),
14078 previous_prefix, type->name (), NULL,
14079 NULL, excludes,
14080 read_decl_line (die, cu),
14081 0, &objfile->objfile_obstack);
14082 }
14083 }
14084
14085 if (die->child != NULL)
14086 {
14087 struct die_info *child_die = die->child;
14088
14089 while (child_die && child_die->tag)
14090 {
14091 process_die (child_die, cu);
14092 child_die = child_die->sibling;
14093 }
14094 }
14095 }
14096
14097 /* Read a Fortran module as type. This DIE can be only a declaration used for
14098 imported module. Still we need that type as local Fortran "use ... only"
14099 declaration imports depend on the created type in determine_prefix. */
14100
14101 static struct type *
14102 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14103 {
14104 struct objfile *objfile = cu->per_objfile->objfile;
14105 const char *module_name;
14106 struct type *type;
14107
14108 module_name = dwarf2_name (die, cu);
14109 type = type_allocator (objfile).new_type (TYPE_CODE_MODULE, 0, module_name);
14110
14111 return set_die_type (die, type, cu);
14112 }
14113
14114 /* Read a Fortran module. */
14115
14116 static void
14117 read_module (struct die_info *die, struct dwarf2_cu *cu)
14118 {
14119 struct die_info *child_die = die->child;
14120 struct type *type;
14121
14122 type = read_type_die (die, cu);
14123 new_symbol (die, type, cu);
14124
14125 while (child_die && child_die->tag)
14126 {
14127 process_die (child_die, cu);
14128 child_die = child_die->sibling;
14129 }
14130 }
14131
14132 /* Return the name of the namespace represented by DIE. Set
14133 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14134 namespace. */
14135
14136 static const char *
14137 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14138 {
14139 struct die_info *current_die;
14140 const char *name = NULL;
14141
14142 /* Loop through the extensions until we find a name. */
14143
14144 for (current_die = die;
14145 current_die != NULL;
14146 current_die = dwarf2_extension (die, &cu))
14147 {
14148 /* We don't use dwarf2_name here so that we can detect the absence
14149 of a name -> anonymous namespace. */
14150 name = dwarf2_string_attr (die, DW_AT_name, cu);
14151
14152 if (name != NULL)
14153 break;
14154 }
14155
14156 /* Is it an anonymous namespace? */
14157
14158 *is_anonymous = (name == NULL);
14159 if (*is_anonymous)
14160 name = CP_ANONYMOUS_NAMESPACE_STR;
14161
14162 return name;
14163 }
14164
14165 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14166 the user defined type vector. */
14167
14168 static struct type *
14169 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14170 {
14171 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
14172 struct comp_unit_head *cu_header = &cu->header;
14173 struct type *type;
14174 struct attribute *attr_byte_size;
14175 struct attribute *attr_address_class;
14176 int byte_size, addr_class;
14177 struct type *target_type;
14178
14179 target_type = die_type (die, cu);
14180
14181 /* The die_type call above may have already set the type for this DIE. */
14182 type = get_die_type (die, cu);
14183 if (type)
14184 return type;
14185
14186 type = lookup_pointer_type (target_type);
14187
14188 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14189 if (attr_byte_size)
14190 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
14191 else
14192 byte_size = cu_header->addr_size;
14193
14194 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14195 if (attr_address_class)
14196 addr_class = attr_address_class->constant_value (DW_ADDR_none);
14197 else
14198 addr_class = DW_ADDR_none;
14199
14200 ULONGEST alignment = get_alignment (cu, die);
14201
14202 /* If the pointer size, alignment, or address class is different
14203 than the default, create a type variant marked as such and set
14204 the length accordingly. */
14205 if (type->length () != byte_size
14206 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
14207 && alignment != TYPE_RAW_ALIGN (type))
14208 || addr_class != DW_ADDR_none)
14209 {
14210 if (gdbarch_address_class_type_flags_p (gdbarch))
14211 {
14212 type_instance_flags type_flags
14213 = gdbarch_address_class_type_flags (gdbarch, byte_size,
14214 addr_class);
14215 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14216 == 0);
14217 type = make_type_with_address_space (type, type_flags);
14218 }
14219 else if (type->length () != byte_size)
14220 {
14221 complaint (_("invalid pointer size %d"), byte_size);
14222 }
14223 else if (TYPE_RAW_ALIGN (type) != alignment)
14224 {
14225 complaint (_("Invalid DW_AT_alignment"
14226 " - DIE at %s [in module %s]"),
14227 sect_offset_str (die->sect_off),
14228 objfile_name (cu->per_objfile->objfile));
14229 }
14230 else
14231 {
14232 /* Should we also complain about unhandled address classes? */
14233 }
14234 }
14235
14236 type->set_length (byte_size);
14237 set_type_align (type, alignment);
14238 return set_die_type (die, type, cu);
14239 }
14240
14241 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14242 the user defined type vector. */
14243
14244 static struct type *
14245 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14246 {
14247 struct type *type;
14248 struct type *to_type;
14249 struct type *domain;
14250
14251 to_type = die_type (die, cu);
14252 domain = die_containing_type (die, cu);
14253
14254 /* The calls above may have already set the type for this DIE. */
14255 type = get_die_type (die, cu);
14256 if (type)
14257 return type;
14258
14259 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
14260 type = lookup_methodptr_type (to_type);
14261 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
14262 {
14263 struct type *new_type
14264 = type_allocator (cu->per_objfile->objfile).new_type ();
14265
14266 smash_to_method_type (new_type, domain, to_type->target_type (),
14267 to_type->fields (), to_type->num_fields (),
14268 to_type->has_varargs ());
14269 type = lookup_methodptr_type (new_type);
14270 }
14271 else
14272 type = lookup_memberptr_type (to_type, domain);
14273
14274 return set_die_type (die, type, cu);
14275 }
14276
14277 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14278 the user defined type vector. */
14279
14280 static struct type *
14281 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14282 enum type_code refcode)
14283 {
14284 struct comp_unit_head *cu_header = &cu->header;
14285 struct type *type, *target_type;
14286 struct attribute *attr;
14287
14288 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14289
14290 target_type = die_type (die, cu);
14291
14292 /* The die_type call above may have already set the type for this DIE. */
14293 type = get_die_type (die, cu);
14294 if (type)
14295 return type;
14296
14297 type = lookup_reference_type (target_type, refcode);
14298 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14299 if (attr != nullptr)
14300 type->set_length (attr->constant_value (cu_header->addr_size));
14301 else
14302 type->set_length (cu_header->addr_size);
14303
14304 maybe_set_alignment (cu, die, type);
14305 return set_die_type (die, type, cu);
14306 }
14307
14308 /* Add the given cv-qualifiers to the element type of the array. GCC
14309 outputs DWARF type qualifiers that apply to an array, not the
14310 element type. But GDB relies on the array element type to carry
14311 the cv-qualifiers. This mimics section 6.7.3 of the C99
14312 specification. */
14313
14314 static struct type *
14315 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14316 struct type *base_type, int cnst, int voltl)
14317 {
14318 struct type *el_type, *inner_array;
14319
14320 base_type = copy_type (base_type);
14321 inner_array = base_type;
14322
14323 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
14324 {
14325 inner_array->set_target_type (copy_type (inner_array->target_type ()));
14326 inner_array = inner_array->target_type ();
14327 }
14328
14329 el_type = inner_array->target_type ();
14330 cnst |= TYPE_CONST (el_type);
14331 voltl |= TYPE_VOLATILE (el_type);
14332 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
14333
14334 return set_die_type (die, base_type, cu);
14335 }
14336
14337 static struct type *
14338 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14339 {
14340 struct type *base_type, *cv_type;
14341
14342 base_type = die_type (die, cu);
14343
14344 /* The die_type call above may have already set the type for this DIE. */
14345 cv_type = get_die_type (die, cu);
14346 if (cv_type)
14347 return cv_type;
14348
14349 /* In case the const qualifier is applied to an array type, the element type
14350 is so qualified, not the array type (section 6.7.3 of C99). */
14351 if (base_type->code () == TYPE_CODE_ARRAY)
14352 return add_array_cv_type (die, cu, base_type, 1, 0);
14353
14354 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14355 return set_die_type (die, cv_type, cu);
14356 }
14357
14358 static struct type *
14359 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14360 {
14361 struct type *base_type, *cv_type;
14362
14363 base_type = die_type (die, cu);
14364
14365 /* The die_type call above may have already set the type for this DIE. */
14366 cv_type = get_die_type (die, cu);
14367 if (cv_type)
14368 return cv_type;
14369
14370 /* In case the volatile qualifier is applied to an array type, the
14371 element type is so qualified, not the array type (section 6.7.3
14372 of C99). */
14373 if (base_type->code () == TYPE_CODE_ARRAY)
14374 return add_array_cv_type (die, cu, base_type, 0, 1);
14375
14376 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14377 return set_die_type (die, cv_type, cu);
14378 }
14379
14380 /* Handle DW_TAG_restrict_type. */
14381
14382 static struct type *
14383 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14384 {
14385 struct type *base_type, *cv_type;
14386
14387 base_type = die_type (die, cu);
14388
14389 /* The die_type call above may have already set the type for this DIE. */
14390 cv_type = get_die_type (die, cu);
14391 if (cv_type)
14392 return cv_type;
14393
14394 cv_type = make_restrict_type (base_type);
14395 return set_die_type (die, cv_type, cu);
14396 }
14397
14398 /* Handle DW_TAG_atomic_type. */
14399
14400 static struct type *
14401 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14402 {
14403 struct type *base_type, *cv_type;
14404
14405 base_type = die_type (die, cu);
14406
14407 /* The die_type call above may have already set the type for this DIE. */
14408 cv_type = get_die_type (die, cu);
14409 if (cv_type)
14410 return cv_type;
14411
14412 cv_type = make_atomic_type (base_type);
14413 return set_die_type (die, cv_type, cu);
14414 }
14415
14416 /* Extract all information from a DW_TAG_string_type DIE and add to
14417 the user defined type vector. It isn't really a user defined type,
14418 but it behaves like one, with other DIE's using an AT_user_def_type
14419 attribute to reference it. */
14420
14421 static struct type *
14422 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14423 {
14424 struct objfile *objfile = cu->per_objfile->objfile;
14425 struct gdbarch *gdbarch = objfile->arch ();
14426 struct type *type, *range_type, *index_type, *char_type;
14427 struct attribute *attr;
14428 struct dynamic_prop prop;
14429 bool length_is_constant = true;
14430 LONGEST length;
14431
14432 /* There are a couple of places where bit sizes might be made use of
14433 when parsing a DW_TAG_string_type, however, no producer that we know
14434 of make use of these. Handling bit sizes that are a multiple of the
14435 byte size is easy enough, but what about other bit sizes? Lets deal
14436 with that problem when we have to. Warn about these attributes being
14437 unsupported, then parse the type and ignore them like we always
14438 have. */
14439 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
14440 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
14441 {
14442 static bool warning_printed = false;
14443 if (!warning_printed)
14444 {
14445 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
14446 "currently supported on DW_TAG_string_type."));
14447 warning_printed = true;
14448 }
14449 }
14450
14451 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14452 if (attr != nullptr && !attr->form_is_constant ())
14453 {
14454 /* The string length describes the location at which the length of
14455 the string can be found. The size of the length field can be
14456 specified with one of the attributes below. */
14457 struct type *prop_type;
14458 struct attribute *len
14459 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
14460 if (len == nullptr)
14461 len = dwarf2_attr (die, DW_AT_byte_size, cu);
14462 if (len != nullptr && len->form_is_constant ())
14463 {
14464 /* Pass 0 as the default as we know this attribute is constant
14465 and the default value will not be returned. */
14466 LONGEST sz = len->constant_value (0);
14467 prop_type = objfile_int_type (objfile, sz, true);
14468 }
14469 else
14470 {
14471 /* If the size is not specified then we assume it is the size of
14472 an address on this target. */
14473 prop_type = cu->addr_sized_int_type (true);
14474 }
14475
14476 /* Convert the attribute into a dynamic property. */
14477 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
14478 length = 1;
14479 else
14480 length_is_constant = false;
14481 }
14482 else if (attr != nullptr)
14483 {
14484 /* This DW_AT_string_length just contains the length with no
14485 indirection. There's no need to create a dynamic property in this
14486 case. Pass 0 for the default value as we know it will not be
14487 returned in this case. */
14488 length = attr->constant_value (0);
14489 }
14490 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
14491 {
14492 /* We don't currently support non-constant byte sizes for strings. */
14493 length = attr->constant_value (1);
14494 }
14495 else
14496 {
14497 /* Use 1 as a fallback length if we have nothing else. */
14498 length = 1;
14499 }
14500
14501 index_type = objfile_type (objfile)->builtin_int;
14502 type_allocator alloc (objfile);
14503 if (length_is_constant)
14504 range_type = create_static_range_type (alloc, index_type, 1, length);
14505 else
14506 {
14507 struct dynamic_prop low_bound;
14508
14509 low_bound.set_const_val (1);
14510 range_type = create_range_type (alloc, index_type, &low_bound, &prop, 0);
14511 }
14512 char_type = language_string_char_type (cu->language_defn, gdbarch);
14513 type = create_string_type (alloc, char_type, range_type);
14514
14515 return set_die_type (die, type, cu);
14516 }
14517
14518 /* Assuming that DIE corresponds to a function, returns nonzero
14519 if the function is prototyped. */
14520
14521 static int
14522 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14523 {
14524 struct attribute *attr;
14525
14526 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14527 if (attr && attr->as_boolean ())
14528 return 1;
14529
14530 /* The DWARF standard implies that the DW_AT_prototyped attribute
14531 is only meaningful for C, but the concept also extends to other
14532 languages that allow unprototyped functions (Eg: Objective C).
14533 For all other languages, assume that functions are always
14534 prototyped. */
14535 if (cu->lang () != language_c
14536 && cu->lang () != language_objc
14537 && cu->lang () != language_opencl)
14538 return 1;
14539
14540 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14541 prototyped and unprototyped functions; default to prototyped,
14542 since that is more common in modern code (and RealView warns
14543 about unprototyped functions). */
14544 if (producer_is_realview (cu->producer))
14545 return 1;
14546
14547 return 0;
14548 }
14549
14550 /* Handle DIES due to C code like:
14551
14552 struct foo
14553 {
14554 int (*funcp)(int a, long l);
14555 int b;
14556 };
14557
14558 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14559
14560 static struct type *
14561 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14562 {
14563 struct objfile *objfile = cu->per_objfile->objfile;
14564 struct type *type; /* Type that this function returns. */
14565 struct type *ftype; /* Function that returns above type. */
14566 struct attribute *attr;
14567
14568 type = die_type (die, cu);
14569
14570 /* The die_type call above may have already set the type for this DIE. */
14571 ftype = get_die_type (die, cu);
14572 if (ftype)
14573 return ftype;
14574
14575 ftype = lookup_function_type (type);
14576
14577 if (prototyped_function_p (die, cu))
14578 ftype->set_is_prototyped (true);
14579
14580 /* Store the calling convention in the type if it's available in
14581 the subroutine die. Otherwise set the calling convention to
14582 the default value DW_CC_normal. */
14583 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14584 if (attr != nullptr
14585 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
14586 TYPE_CALLING_CONVENTION (ftype)
14587 = (enum dwarf_calling_convention) attr->constant_value (0);
14588 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14589 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14590 else
14591 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14592
14593 /* Record whether the function returns normally to its caller or not
14594 if the DWARF producer set that information. */
14595 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14596 if (attr && attr->as_boolean ())
14597 TYPE_NO_RETURN (ftype) = 1;
14598
14599 /* We need to add the subroutine type to the die immediately so
14600 we don't infinitely recurse when dealing with parameters
14601 declared as the same subroutine type. */
14602 set_die_type (die, ftype, cu);
14603
14604 if (die->child != NULL)
14605 {
14606 struct type *void_type = objfile_type (objfile)->builtin_void;
14607 struct die_info *child_die;
14608 int nparams, iparams;
14609
14610 /* Count the number of parameters.
14611 FIXME: GDB currently ignores vararg functions, but knows about
14612 vararg member functions. */
14613 nparams = 0;
14614 child_die = die->child;
14615 while (child_die && child_die->tag)
14616 {
14617 if (child_die->tag == DW_TAG_formal_parameter)
14618 nparams++;
14619 else if (child_die->tag == DW_TAG_unspecified_parameters)
14620 ftype->set_has_varargs (true);
14621
14622 child_die = child_die->sibling;
14623 }
14624
14625 /* Allocate storage for parameters and fill them in. */
14626 ftype->set_num_fields (nparams);
14627 ftype->set_fields
14628 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
14629
14630 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14631 even if we error out during the parameters reading below. */
14632 for (iparams = 0; iparams < nparams; iparams++)
14633 ftype->field (iparams).set_type (void_type);
14634
14635 iparams = 0;
14636 child_die = die->child;
14637 while (child_die && child_die->tag)
14638 {
14639 if (child_die->tag == DW_TAG_formal_parameter)
14640 {
14641 struct type *arg_type;
14642
14643 /* DWARF version 2 has no clean way to discern C++
14644 static and non-static member functions. G++ helps
14645 GDB by marking the first parameter for non-static
14646 member functions (which is the this pointer) as
14647 artificial. We pass this information to
14648 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14649
14650 DWARF version 3 added DW_AT_object_pointer, which GCC
14651 4.5 does not yet generate. */
14652 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14653 if (attr != nullptr)
14654 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
14655 else
14656 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14657 arg_type = die_type (child_die, cu);
14658
14659 /* RealView does not mark THIS as const, which the testsuite
14660 expects. GCC marks THIS as const in method definitions,
14661 but not in the class specifications (GCC PR 43053). */
14662 if (cu->lang () == language_cplus
14663 && !TYPE_CONST (arg_type)
14664 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14665 {
14666 int is_this = 0;
14667 struct dwarf2_cu *arg_cu = cu;
14668 const char *name = dwarf2_name (child_die, cu);
14669
14670 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14671 if (attr != nullptr)
14672 {
14673 /* If the compiler emits this, use it. */
14674 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14675 is_this = 1;
14676 }
14677 else if (name && strcmp (name, "this") == 0)
14678 /* Function definitions will have the argument names. */
14679 is_this = 1;
14680 else if (name == NULL && iparams == 0)
14681 /* Declarations may not have the names, so like
14682 elsewhere in GDB, assume an artificial first
14683 argument is "this". */
14684 is_this = 1;
14685
14686 if (is_this)
14687 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14688 arg_type, 0);
14689 }
14690
14691 ftype->field (iparams).set_type (arg_type);
14692 iparams++;
14693 }
14694 child_die = child_die->sibling;
14695 }
14696 }
14697
14698 return ftype;
14699 }
14700
14701 static struct type *
14702 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14703 {
14704 struct objfile *objfile = cu->per_objfile->objfile;
14705 const char *name = NULL;
14706 struct type *this_type, *target_type;
14707
14708 name = dwarf2_full_name (NULL, die, cu);
14709 this_type = type_allocator (objfile).new_type (TYPE_CODE_TYPEDEF, 0, name);
14710 this_type->set_target_is_stub (true);
14711 set_die_type (die, this_type, cu);
14712 target_type = die_type (die, cu);
14713 if (target_type != this_type)
14714 this_type->set_target_type (target_type);
14715 else
14716 {
14717 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14718 spec and cause infinite loops in GDB. */
14719 complaint (_("Self-referential DW_TAG_typedef "
14720 "- DIE at %s [in module %s]"),
14721 sect_offset_str (die->sect_off), objfile_name (objfile));
14722 this_type->set_target_type (nullptr);
14723 }
14724 if (name == NULL)
14725 {
14726 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
14727 anonymous typedefs, which is, strictly speaking, invalid DWARF.
14728 Handle these by just returning the target type, rather than
14729 constructing an anonymous typedef type and trying to handle this
14730 elsewhere. */
14731 set_die_type (die, target_type, cu);
14732 return target_type;
14733 }
14734 return this_type;
14735 }
14736
14737 /* Helper for get_dwarf2_rational_constant that computes the value of
14738 a given gmp_mpz given an attribute. */
14739
14740 static void
14741 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
14742 {
14743 /* GCC will sometimes emit a 16-byte constant value as a DWARF
14744 location expression that pushes an implicit value. */
14745 if (attr->form == DW_FORM_exprloc)
14746 {
14747 dwarf_block *blk = attr->as_block ();
14748 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
14749 {
14750 uint64_t len;
14751 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
14752 blk->data + blk->size,
14753 &len);
14754 if (ptr - blk->data + len <= blk->size)
14755 {
14756 value->read (gdb::make_array_view (ptr, len),
14757 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14758 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14759 true);
14760 return;
14761 }
14762 }
14763
14764 /* On failure set it to 1. */
14765 *value = gdb_mpz (1);
14766 }
14767 else if (attr->form_is_block ())
14768 {
14769 dwarf_block *blk = attr->as_block ();
14770 value->read (gdb::make_array_view (blk->data, blk->size),
14771 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14772 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14773 true);
14774 }
14775 else
14776 *value = gdb_mpz (attr->constant_value (1));
14777 }
14778
14779 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
14780 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
14781
14782 If the numerator and/or numerator attribute is missing,
14783 a complaint is filed, and NUMERATOR and DENOMINATOR are left
14784 untouched. */
14785
14786 static void
14787 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
14788 gdb_mpz *numerator, gdb_mpz *denominator)
14789 {
14790 struct attribute *num_attr, *denom_attr;
14791
14792 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
14793 if (num_attr == nullptr)
14794 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
14795 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14796
14797 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
14798 if (denom_attr == nullptr)
14799 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
14800 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14801
14802 if (num_attr == nullptr || denom_attr == nullptr)
14803 return;
14804
14805 get_mpz (cu, numerator, num_attr);
14806 get_mpz (cu, denominator, denom_attr);
14807 }
14808
14809 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
14810 rational constant, rather than a signed one.
14811
14812 If the rational constant has a negative value, a complaint
14813 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
14814
14815 static void
14816 get_dwarf2_unsigned_rational_constant (struct die_info *die,
14817 struct dwarf2_cu *cu,
14818 gdb_mpz *numerator,
14819 gdb_mpz *denominator)
14820 {
14821 gdb_mpz num (1);
14822 gdb_mpz denom (1);
14823
14824 get_dwarf2_rational_constant (die, cu, &num, &denom);
14825 if (num < 0 && denom < 0)
14826 {
14827 num.negate ();
14828 denom.negate ();
14829 }
14830 else if (num < 0)
14831 {
14832 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
14833 " in DIE at %s"),
14834 sect_offset_str (die->sect_off));
14835 return;
14836 }
14837 else if (denom < 0)
14838 {
14839 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
14840 " in DIE at %s"),
14841 sect_offset_str (die->sect_off));
14842 return;
14843 }
14844
14845 *numerator = std::move (num);
14846 *denominator = std::move (denom);
14847 }
14848
14849 /* Assuming that ENCODING is a string whose contents starting at the
14850 K'th character is "_nn" where "nn" is a decimal number, scan that
14851 number and set RESULT to the value. K is updated to point to the
14852 character immediately following the number.
14853
14854 If the string does not conform to the format described above, false
14855 is returned, and K may or may not be changed. */
14856
14857 static bool
14858 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
14859 {
14860 /* The next character should be an underscore ('_') followed
14861 by a digit. */
14862 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
14863 return false;
14864
14865 /* Skip the underscore. */
14866 k++;
14867 int start = k;
14868
14869 /* Determine the number of digits for our number. */
14870 while (isdigit (encoding[k]))
14871 k++;
14872 if (k == start)
14873 return false;
14874
14875 std::string copy (&encoding[start], k - start);
14876 return result->set (copy.c_str (), 10);
14877 }
14878
14879 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
14880 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
14881 DENOM, update OFFSET, and return true on success. Return false on
14882 failure. */
14883
14884 static bool
14885 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
14886 gdb_mpz *num, gdb_mpz *denom)
14887 {
14888 if (!ada_get_gnat_encoded_number (encoding, offset, num))
14889 return false;
14890 return ada_get_gnat_encoded_number (encoding, offset, denom);
14891 }
14892
14893 /* Assuming DIE corresponds to a fixed point type, finish the creation
14894 of the corresponding TYPE by setting its type-specific data. CU is
14895 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
14896 encodings. It is nullptr if the GNAT encoding should be
14897 ignored. */
14898
14899 static void
14900 finish_fixed_point_type (struct type *type, const char *suffix,
14901 struct die_info *die, struct dwarf2_cu *cu)
14902 {
14903 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
14904 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
14905
14906 /* If GNAT encodings are preferred, don't examine the
14907 attributes. */
14908 struct attribute *attr = nullptr;
14909 if (suffix == nullptr)
14910 {
14911 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
14912 if (attr == nullptr)
14913 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
14914 if (attr == nullptr)
14915 attr = dwarf2_attr (die, DW_AT_small, cu);
14916 }
14917
14918 /* Numerator and denominator of our fixed-point type's scaling factor.
14919 The default is a scaling factor of 1, which we use as a fallback
14920 when we are not able to decode it (problem with the debugging info,
14921 unsupported forms, bug in GDB, etc...). Using that as the default
14922 allows us to at least print the unscaled value, which might still
14923 be useful to a user. */
14924 gdb_mpz scale_num (1);
14925 gdb_mpz scale_denom (1);
14926
14927 if (attr == nullptr)
14928 {
14929 int offset = 0;
14930 if (suffix != nullptr
14931 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14932 &scale_denom)
14933 /* The number might be encoded as _nn_dd_nn_dd, where the
14934 second ratio is the 'small value. In this situation, we
14935 want the second value. */
14936 && (suffix[offset] != '_'
14937 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14938 &scale_denom)))
14939 {
14940 /* Found it. */
14941 }
14942 else
14943 {
14944 /* Scaling factor not found. Assume a scaling factor of 1,
14945 and hope for the best. At least the user will be able to
14946 see the encoded value. */
14947 scale_num = 1;
14948 scale_denom = 1;
14949 complaint (_("no scale found for fixed-point type (DIE at %s)"),
14950 sect_offset_str (die->sect_off));
14951 }
14952 }
14953 else if (attr->name == DW_AT_binary_scale)
14954 {
14955 LONGEST scale_exp = attr->constant_value (0);
14956 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
14957
14958 num_or_denom <<= std::abs (scale_exp);
14959 }
14960 else if (attr->name == DW_AT_decimal_scale)
14961 {
14962 LONGEST scale_exp = attr->constant_value (0);
14963 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
14964
14965 num_or_denom = gdb_mpz::pow (10, std::abs (scale_exp));
14966 }
14967 else if (attr->name == DW_AT_small)
14968 {
14969 struct die_info *scale_die;
14970 struct dwarf2_cu *scale_cu = cu;
14971
14972 scale_die = follow_die_ref (die, attr, &scale_cu);
14973 if (scale_die->tag == DW_TAG_constant)
14974 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
14975 &scale_num, &scale_denom);
14976 else
14977 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
14978 " (DIE at %s)"),
14979 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14980 }
14981 else
14982 {
14983 complaint (_("unsupported scale attribute %s for fixed-point type"
14984 " (DIE at %s)"),
14985 dwarf_attr_name (attr->name),
14986 sect_offset_str (die->sect_off));
14987 }
14988
14989 type->fixed_point_info ().scaling_factor = gdb_mpq (scale_num, scale_denom);
14990 }
14991
14992 /* The gnat-encoding suffix for fixed point. */
14993
14994 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
14995
14996 /* If NAME encodes an Ada fixed-point type, return a pointer to the
14997 "XF" suffix of the name. The text after this is what encodes the
14998 'small and 'delta information. Otherwise, return nullptr. */
14999
15000 static const char *
15001 gnat_encoded_fixed_point_type_info (const char *name)
15002 {
15003 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
15004 }
15005
15006 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15007 (which may be different from NAME) to the architecture back-end to allow
15008 it to guess the correct format if necessary. */
15009
15010 static struct type *
15011 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15012 const char *name_hint, enum bfd_endian byte_order)
15013 {
15014 struct gdbarch *gdbarch = objfile->arch ();
15015 const struct floatformat **format;
15016 struct type *type;
15017
15018 type_allocator alloc (objfile);
15019 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15020 if (format)
15021 type = init_float_type (alloc, bits, name, format, byte_order);
15022 else
15023 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15024
15025 return type;
15026 }
15027
15028 /* Allocate an integer type of size BITS and name NAME. */
15029
15030 static struct type *
15031 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
15032 int bits, int unsigned_p, const char *name)
15033 {
15034 struct type *type;
15035
15036 /* Versions of Intel's C Compiler generate an integer type called "void"
15037 instead of using DW_TAG_unspecified_type. This has been seen on
15038 at least versions 14, 17, and 18. */
15039 if (bits == 0 && producer_is_icc (cu) && name != nullptr
15040 && strcmp (name, "void") == 0)
15041 type = objfile_type (objfile)->builtin_void;
15042 else
15043 {
15044 type_allocator alloc (objfile);
15045 type = init_integer_type (alloc, bits, unsigned_p, name);
15046 }
15047
15048 return type;
15049 }
15050
15051 /* Return true if DIE has a DW_AT_small attribute whose value is
15052 a constant rational, where both the numerator and denominator
15053 are equal to zero.
15054
15055 CU is the DIE's Compilation Unit. */
15056
15057 static bool
15058 has_zero_over_zero_small_attribute (struct die_info *die,
15059 struct dwarf2_cu *cu)
15060 {
15061 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
15062 if (attr == nullptr)
15063 return false;
15064
15065 struct dwarf2_cu *scale_cu = cu;
15066 struct die_info *scale_die
15067 = follow_die_ref (die, attr, &scale_cu);
15068
15069 if (scale_die->tag != DW_TAG_constant)
15070 return false;
15071
15072 gdb_mpz num (1), denom (1);
15073 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
15074 return num == 0 && denom == 0;
15075 }
15076
15077 /* Initialise and return a floating point type of size BITS suitable for
15078 use as a component of a complex number. The NAME_HINT is passed through
15079 when initialising the floating point type and is the name of the complex
15080 type.
15081
15082 As DWARF doesn't currently provide an explicit name for the components
15083 of a complex number, but it can be helpful to have these components
15084 named, we try to select a suitable name based on the size of the
15085 component. */
15086 static struct type *
15087 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
15088 struct objfile *objfile,
15089 int bits, const char *name_hint,
15090 enum bfd_endian byte_order)
15091 {
15092 gdbarch *gdbarch = objfile->arch ();
15093 struct type *tt = nullptr;
15094
15095 /* Try to find a suitable floating point builtin type of size BITS.
15096 We're going to use the name of this type as the name for the complex
15097 target type that we are about to create. */
15098 switch (cu->lang ())
15099 {
15100 case language_fortran:
15101 switch (bits)
15102 {
15103 case 32:
15104 tt = builtin_f_type (gdbarch)->builtin_real;
15105 break;
15106 case 64:
15107 tt = builtin_f_type (gdbarch)->builtin_real_s8;
15108 break;
15109 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15110 case 128:
15111 tt = builtin_f_type (gdbarch)->builtin_real_s16;
15112 break;
15113 }
15114 break;
15115 default:
15116 switch (bits)
15117 {
15118 case 32:
15119 tt = builtin_type (gdbarch)->builtin_float;
15120 break;
15121 case 64:
15122 tt = builtin_type (gdbarch)->builtin_double;
15123 break;
15124 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15125 case 128:
15126 tt = builtin_type (gdbarch)->builtin_long_double;
15127 break;
15128 }
15129 break;
15130 }
15131
15132 /* If the type we found doesn't match the size we were looking for, then
15133 pretend we didn't find a type at all, the complex target type we
15134 create will then be nameless. */
15135 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
15136 tt = nullptr;
15137
15138 const char *name = (tt == nullptr) ? nullptr : tt->name ();
15139 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
15140 }
15141
15142 /* Find a representation of a given base type and install
15143 it in the TYPE field of the die. */
15144
15145 static struct type *
15146 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15147 {
15148 struct objfile *objfile = cu->per_objfile->objfile;
15149 struct type *type;
15150 struct attribute *attr;
15151 int encoding = 0, bits = 0;
15152 const char *name;
15153 gdbarch *arch;
15154
15155 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15156 if (attr != nullptr && attr->form_is_constant ())
15157 encoding = attr->constant_value (0);
15158 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15159 if (attr != nullptr)
15160 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
15161 name = dwarf2_name (die, cu);
15162 if (!name)
15163 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
15164
15165 arch = objfile->arch ();
15166 enum bfd_endian byte_order = gdbarch_byte_order (arch);
15167
15168 attr = dwarf2_attr (die, DW_AT_endianity, cu);
15169 if (attr != nullptr && attr->form_is_constant ())
15170 {
15171 int endianity = attr->constant_value (0);
15172
15173 switch (endianity)
15174 {
15175 case DW_END_big:
15176 byte_order = BFD_ENDIAN_BIG;
15177 break;
15178 case DW_END_little:
15179 byte_order = BFD_ENDIAN_LITTLE;
15180 break;
15181 default:
15182 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
15183 break;
15184 }
15185 }
15186
15187 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
15188 && cu->lang () == language_ada
15189 && has_zero_over_zero_small_attribute (die, cu))
15190 {
15191 /* brobecker/2018-02-24: This is a fixed point type for which
15192 the scaling factor is represented as fraction whose value
15193 does not make sense (zero divided by zero), so we should
15194 normally never see these. However, there is a small category
15195 of fixed point types for which GNAT is unable to provide
15196 the scaling factor via the standard DWARF mechanisms, and
15197 for which the info is provided via the GNAT encodings instead.
15198 This is likely what this DIE is about. */
15199 encoding = (encoding == DW_ATE_signed_fixed
15200 ? DW_ATE_signed
15201 : DW_ATE_unsigned);
15202 }
15203
15204 /* With GNAT encodings, fixed-point information will be encoded in
15205 the type name. Note that this can also occur with the above
15206 zero-over-zero case, which is why this is a separate "if" rather
15207 than an "else if". */
15208 const char *gnat_encoding_suffix = nullptr;
15209 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
15210 && cu->lang () == language_ada
15211 && name != nullptr)
15212 {
15213 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
15214 if (gnat_encoding_suffix != nullptr)
15215 {
15216 gdb_assert (startswith (gnat_encoding_suffix,
15217 GNAT_FIXED_POINT_SUFFIX));
15218 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
15219 name, gnat_encoding_suffix - name);
15220 /* Use -1 here so that SUFFIX points at the "_" after the
15221 "XF". */
15222 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
15223
15224 encoding = (encoding == DW_ATE_signed
15225 ? DW_ATE_signed_fixed
15226 : DW_ATE_unsigned_fixed);
15227 }
15228 }
15229
15230 type_allocator alloc (objfile);
15231 switch (encoding)
15232 {
15233 case DW_ATE_address:
15234 /* Turn DW_ATE_address into a void * pointer. */
15235 type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
15236 type = init_pointer_type (alloc, bits, name, type);
15237 break;
15238 case DW_ATE_boolean:
15239 type = init_boolean_type (alloc, bits, 1, name);
15240 break;
15241 case DW_ATE_complex_float:
15242 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
15243 byte_order);
15244 if (type->code () == TYPE_CODE_ERROR)
15245 {
15246 if (name == nullptr)
15247 {
15248 struct obstack *obstack
15249 = &cu->per_objfile->objfile->objfile_obstack;
15250 name = obconcat (obstack, "_Complex ", type->name (),
15251 nullptr);
15252 }
15253 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15254 }
15255 else
15256 type = init_complex_type (name, type);
15257 break;
15258 case DW_ATE_decimal_float:
15259 type = init_decfloat_type (alloc, bits, name);
15260 break;
15261 case DW_ATE_float:
15262 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
15263 break;
15264 case DW_ATE_signed:
15265 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
15266 break;
15267 case DW_ATE_unsigned:
15268 if (cu->lang () == language_fortran
15269 && name
15270 && startswith (name, "character("))
15271 type = init_character_type (alloc, bits, 1, name);
15272 else
15273 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
15274 break;
15275 case DW_ATE_signed_char:
15276 if (cu->lang () == language_ada
15277 || cu->lang () == language_m2
15278 || cu->lang () == language_pascal
15279 || cu->lang () == language_fortran)
15280 type = init_character_type (alloc, bits, 0, name);
15281 else
15282 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
15283 break;
15284 case DW_ATE_unsigned_char:
15285 if (cu->lang () == language_ada
15286 || cu->lang () == language_m2
15287 || cu->lang () == language_pascal
15288 || cu->lang () == language_fortran
15289 || cu->lang () == language_rust)
15290 type = init_character_type (alloc, bits, 1, name);
15291 else
15292 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
15293 break;
15294 case DW_ATE_UTF:
15295 {
15296 type = init_character_type (alloc, bits, 1, name);
15297 return set_die_type (die, type, cu);
15298 }
15299 break;
15300 case DW_ATE_signed_fixed:
15301 type = init_fixed_point_type (objfile, bits, 0, name);
15302 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15303 break;
15304 case DW_ATE_unsigned_fixed:
15305 type = init_fixed_point_type (objfile, bits, 1, name);
15306 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15307 break;
15308
15309 default:
15310 complaint (_("unsupported DW_AT_encoding: '%s'"),
15311 dwarf_type_encoding_name (encoding));
15312 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15313 break;
15314 }
15315
15316 if (type->code () == TYPE_CODE_INT
15317 && name != nullptr
15318 && strcmp (name, "char") == 0)
15319 type->set_has_no_signedness (true);
15320
15321 maybe_set_alignment (cu, die, type);
15322
15323 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
15324
15325 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
15326 {
15327 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15328 if (attr != nullptr && attr->as_unsigned () <= 8 * type->length ())
15329 {
15330 unsigned real_bit_size = attr->as_unsigned ();
15331 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15332 /* Only use the attributes if they make sense together. */
15333 if (attr == nullptr
15334 || (attr->as_unsigned () + real_bit_size
15335 <= 8 * type->length ()))
15336 {
15337 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
15338 = real_bit_size;
15339 if (attr != nullptr)
15340 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
15341 = attr->as_unsigned ();
15342 }
15343 }
15344 }
15345
15346 return set_die_type (die, type, cu);
15347 }
15348
15349 /* A helper function that returns the name of DIE, if it refers to a
15350 variable declaration. */
15351
15352 static const char *
15353 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
15354 {
15355 if (die->tag != DW_TAG_variable)
15356 return nullptr;
15357
15358 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
15359 if (attr == nullptr || !attr->as_boolean ())
15360 return nullptr;
15361
15362 attr = dwarf2_attr (die, DW_AT_name, cu);
15363 if (attr == nullptr)
15364 return nullptr;
15365 return attr->as_string ();
15366 }
15367
15368 /* Parse dwarf attribute if it's a block, reference or constant and put the
15369 resulting value of the attribute into struct bound_prop.
15370 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15371
15372 static int
15373 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15374 struct dwarf2_cu *cu, struct dynamic_prop *prop,
15375 struct type *default_type)
15376 {
15377 struct dwarf2_property_baton *baton;
15378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15379 struct objfile *objfile = per_objfile->objfile;
15380 struct obstack *obstack = &objfile->objfile_obstack;
15381
15382 gdb_assert (default_type != NULL);
15383
15384 if (attr == NULL || prop == NULL)
15385 return 0;
15386
15387 if (attr->form_is_block ())
15388 {
15389 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15390 baton->property_type = default_type;
15391 baton->locexpr.per_cu = cu->per_cu;
15392 baton->locexpr.per_objfile = per_objfile;
15393
15394 struct dwarf_block *block;
15395 if (attr->form == DW_FORM_data16)
15396 {
15397 size_t data_size = 16;
15398 block = XOBNEW (obstack, struct dwarf_block);
15399 block->size = (data_size
15400 + 2 /* Extra bytes for DW_OP and arg. */);
15401 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
15402 data[0] = DW_OP_implicit_value;
15403 data[1] = data_size;
15404 memcpy (&data[2], attr->as_block ()->data, data_size);
15405 block->data = data;
15406 }
15407 else
15408 block = attr->as_block ();
15409
15410 baton->locexpr.size = block->size;
15411 baton->locexpr.data = block->data;
15412 switch (attr->name)
15413 {
15414 case DW_AT_string_length:
15415 baton->locexpr.is_reference = true;
15416 break;
15417 default:
15418 baton->locexpr.is_reference = false;
15419 break;
15420 }
15421
15422 prop->set_locexpr (baton);
15423 gdb_assert (prop->baton () != NULL);
15424 }
15425 else if (attr->form_is_ref ())
15426 {
15427 struct dwarf2_cu *target_cu = cu;
15428 struct die_info *target_die;
15429 struct attribute *target_attr;
15430
15431 target_die = follow_die_ref (die, attr, &target_cu);
15432 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15433 if (target_attr == NULL)
15434 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15435 target_cu);
15436 if (target_attr == nullptr)
15437 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
15438 target_cu);
15439 if (target_attr == NULL)
15440 {
15441 const char *name = var_decl_name (target_die, target_cu);
15442 if (name != nullptr)
15443 {
15444 prop->set_variable_name (name);
15445 return 1;
15446 }
15447 return 0;
15448 }
15449
15450 switch (target_attr->name)
15451 {
15452 case DW_AT_location:
15453 if (target_attr->form_is_section_offset ())
15454 {
15455 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15456 baton->property_type = die_type (target_die, target_cu);
15457 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15458 prop->set_loclist (baton);
15459 gdb_assert (prop->baton () != NULL);
15460 }
15461 else if (target_attr->form_is_block ())
15462 {
15463 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15464 baton->property_type = die_type (target_die, target_cu);
15465 baton->locexpr.per_cu = cu->per_cu;
15466 baton->locexpr.per_objfile = per_objfile;
15467 struct dwarf_block *block = target_attr->as_block ();
15468 baton->locexpr.size = block->size;
15469 baton->locexpr.data = block->data;
15470 baton->locexpr.is_reference = true;
15471 prop->set_locexpr (baton);
15472 gdb_assert (prop->baton () != NULL);
15473 }
15474 else
15475 {
15476 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15477 "dynamic property");
15478 return 0;
15479 }
15480 break;
15481 case DW_AT_data_member_location:
15482 case DW_AT_data_bit_offset:
15483 {
15484 LONGEST offset;
15485
15486 if (!handle_member_location (target_die, target_cu, &offset))
15487 return 0;
15488
15489 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15490 baton->property_type = read_type_die (target_die->parent,
15491 target_cu);
15492 baton->offset_info.offset = offset;
15493 baton->offset_info.type = die_type (target_die, target_cu);
15494 prop->set_addr_offset (baton);
15495 break;
15496 }
15497 }
15498 }
15499 else if (attr->form_is_constant ())
15500 prop->set_const_val (attr->constant_value (0));
15501 else if (attr->form_is_section_offset ())
15502 {
15503 switch (attr->name)
15504 {
15505 case DW_AT_string_length:
15506 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15507 baton->property_type = default_type;
15508 fill_in_loclist_baton (cu, &baton->loclist, attr);
15509 prop->set_loclist (baton);
15510 gdb_assert (prop->baton () != NULL);
15511 break;
15512 default:
15513 goto invalid;
15514 }
15515 }
15516 else
15517 goto invalid;
15518
15519 return 1;
15520
15521 invalid:
15522 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15523 dwarf2_name (die, cu));
15524 return 0;
15525 }
15526
15527 /* See read.h. */
15528
15529 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
15530 present (which is valid) then compute the default type based on the
15531 compilation units address size. */
15532
15533 static struct type *
15534 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
15535 {
15536 struct type *index_type = die_type (die, cu);
15537
15538 /* Dwarf-2 specifications explicitly allows to create subrange types
15539 without specifying a base type.
15540 In that case, the base type must be set to the type of
15541 the lower bound, upper bound or count, in that order, if any of these
15542 three attributes references an object that has a type.
15543 If no base type is found, the Dwarf-2 specifications say that
15544 a signed integer type of size equal to the size of an address should
15545 be used.
15546 For the following C code: `extern char gdb_int [];'
15547 GCC produces an empty range DIE.
15548 FIXME: muller/2010-05-28: Possible references to object for low bound,
15549 high bound or count are not yet handled by this code. */
15550 if (index_type->code () == TYPE_CODE_VOID)
15551 index_type = cu->addr_sized_int_type (false);
15552
15553 return index_type;
15554 }
15555
15556 /* Read the given DW_AT_subrange DIE. */
15557
15558 static struct type *
15559 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15560 {
15561 struct type *base_type, *orig_base_type;
15562 struct type *range_type;
15563 struct attribute *attr;
15564 struct dynamic_prop low, high;
15565 int low_default_is_valid;
15566 int high_bound_is_count = 0;
15567 const char *name;
15568
15569 orig_base_type = read_subrange_index_type (die, cu);
15570
15571 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15572 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15573 creating the range type, but we use the result of check_typedef
15574 when examining properties of the type. */
15575 base_type = check_typedef (orig_base_type);
15576
15577 /* The die_type call above may have already set the type for this DIE. */
15578 range_type = get_die_type (die, cu);
15579 if (range_type)
15580 return range_type;
15581
15582 high.set_const_val (0);
15583
15584 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15585 omitting DW_AT_lower_bound. */
15586 switch (cu->lang ())
15587 {
15588 case language_c:
15589 case language_cplus:
15590 low.set_const_val (0);
15591 low_default_is_valid = 1;
15592 break;
15593 case language_fortran:
15594 low.set_const_val (1);
15595 low_default_is_valid = 1;
15596 break;
15597 case language_d:
15598 case language_objc:
15599 case language_rust:
15600 low.set_const_val (0);
15601 low_default_is_valid = (cu->header.version >= 4);
15602 break;
15603 case language_ada:
15604 case language_m2:
15605 case language_pascal:
15606 low.set_const_val (1);
15607 low_default_is_valid = (cu->header.version >= 4);
15608 break;
15609 default:
15610 low.set_const_val (0);
15611 low_default_is_valid = 0;
15612 break;
15613 }
15614
15615 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15616 if (attr != nullptr)
15617 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
15618 else if (!low_default_is_valid)
15619 complaint (_("Missing DW_AT_lower_bound "
15620 "- DIE at %s [in module %s]"),
15621 sect_offset_str (die->sect_off),
15622 objfile_name (cu->per_objfile->objfile));
15623
15624 struct attribute *attr_ub, *attr_count;
15625 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
15626 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15627 {
15628 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
15629 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15630 {
15631 /* If bounds are constant do the final calculation here. */
15632 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
15633 high.set_const_val (low.const_val () + high.const_val () - 1);
15634 else
15635 high_bound_is_count = 1;
15636 }
15637 else
15638 {
15639 if (attr_ub != NULL)
15640 complaint (_("Unresolved DW_AT_upper_bound "
15641 "- DIE at %s [in module %s]"),
15642 sect_offset_str (die->sect_off),
15643 objfile_name (cu->per_objfile->objfile));
15644 if (attr_count != NULL)
15645 complaint (_("Unresolved DW_AT_count "
15646 "- DIE at %s [in module %s]"),
15647 sect_offset_str (die->sect_off),
15648 objfile_name (cu->per_objfile->objfile));
15649 }
15650 }
15651
15652 LONGEST bias = 0;
15653 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
15654 if (bias_attr != nullptr && bias_attr->form_is_constant ())
15655 bias = bias_attr->constant_value (0);
15656
15657 /* Normally, the DWARF producers are expected to use a signed
15658 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15659 But this is unfortunately not always the case, as witnessed
15660 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15661 is used instead. To work around that ambiguity, we treat
15662 the bounds as signed, and thus sign-extend their values, when
15663 the base type is signed.
15664
15665 Skip it if the base type's length is larger than ULONGEST, to avoid
15666 the undefined behavior of a too large left shift. We don't really handle
15667 constants larger than 8 bytes anyway, at the moment. */
15668
15669 if (base_type->length () <= sizeof (ULONGEST))
15670 {
15671 ULONGEST negative_mask
15672 = -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
15673
15674 if (low.kind () == PROP_CONST
15675 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
15676 low.set_const_val (low.const_val () | negative_mask);
15677
15678 if (high.kind () == PROP_CONST
15679 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
15680 high.set_const_val (high.const_val () | negative_mask);
15681 }
15682
15683 /* Check for bit and byte strides. */
15684 struct dynamic_prop byte_stride_prop;
15685 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
15686 if (attr_byte_stride != nullptr)
15687 {
15688 struct type *prop_type = cu->addr_sized_int_type (false);
15689 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
15690 prop_type);
15691 }
15692
15693 struct dynamic_prop bit_stride_prop;
15694 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
15695 if (attr_bit_stride != nullptr)
15696 {
15697 /* It only makes sense to have either a bit or byte stride. */
15698 if (attr_byte_stride != nullptr)
15699 {
15700 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
15701 "- DIE at %s [in module %s]"),
15702 sect_offset_str (die->sect_off),
15703 objfile_name (cu->per_objfile->objfile));
15704 attr_bit_stride = nullptr;
15705 }
15706 else
15707 {
15708 struct type *prop_type = cu->addr_sized_int_type (false);
15709 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
15710 prop_type);
15711 }
15712 }
15713
15714 type_allocator alloc (cu->per_objfile->objfile);
15715 if (attr_byte_stride != nullptr
15716 || attr_bit_stride != nullptr)
15717 {
15718 bool byte_stride_p = (attr_byte_stride != nullptr);
15719 struct dynamic_prop *stride
15720 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
15721
15722 range_type
15723 = create_range_type_with_stride (alloc, orig_base_type, &low,
15724 &high, bias, stride, byte_stride_p);
15725 }
15726 else
15727 range_type = create_range_type (alloc, orig_base_type, &low, &high, bias);
15728
15729 if (high_bound_is_count)
15730 range_type->bounds ()->flag_upper_bound_is_count = 1;
15731
15732 /* Ada expects an empty array on no boundary attributes. */
15733 if (attr == NULL && cu->lang () != language_ada)
15734 range_type->bounds ()->high.set_undefined ();
15735
15736 name = dwarf2_name (die, cu);
15737 if (name)
15738 range_type->set_name (name);
15739
15740 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15741 if (attr != nullptr)
15742 range_type->set_length (attr->constant_value (0));
15743
15744 maybe_set_alignment (cu, die, range_type);
15745
15746 set_die_type (die, range_type, cu);
15747
15748 /* set_die_type should be already done. */
15749 set_descriptive_type (range_type, die, cu);
15750
15751 return range_type;
15752 }
15753
15754 static struct type *
15755 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15756 {
15757 struct type *type;
15758
15759 type = (type_allocator (cu->per_objfile->objfile)
15760 .new_type (TYPE_CODE_VOID, 0, nullptr));
15761 type->set_name (dwarf2_name (die, cu));
15762
15763 /* In Ada, an unspecified type is typically used when the description
15764 of the type is deferred to a different unit. When encountering
15765 such a type, we treat it as a stub, and try to resolve it later on,
15766 when needed.
15767 Mark this as a stub type for all languages though. */
15768 type->set_is_stub (true);
15769
15770 return set_die_type (die, type, cu);
15771 }
15772
15773 /* Read a single die and all its descendents. Set the die's sibling
15774 field to NULL; set other fields in the die correctly, and set all
15775 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15776 location of the info_ptr after reading all of those dies. PARENT
15777 is the parent of the die in question. */
15778
15779 static struct die_info *
15780 read_die_and_children (const struct die_reader_specs *reader,
15781 const gdb_byte *info_ptr,
15782 const gdb_byte **new_info_ptr,
15783 struct die_info *parent)
15784 {
15785 struct die_info *die;
15786 const gdb_byte *cur_ptr;
15787
15788 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0, true);
15789 if (die == NULL)
15790 {
15791 *new_info_ptr = cur_ptr;
15792 return NULL;
15793 }
15794 store_in_ref_table (die, reader->cu);
15795
15796 if (die->has_children)
15797 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15798 else
15799 {
15800 die->child = NULL;
15801 *new_info_ptr = cur_ptr;
15802 }
15803
15804 die->sibling = NULL;
15805 die->parent = parent;
15806 return die;
15807 }
15808
15809 /* Read a die, all of its descendents, and all of its siblings; set
15810 all of the fields of all of the dies correctly. Arguments are as
15811 in read_die_and_children. */
15812
15813 static struct die_info *
15814 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15815 const gdb_byte *info_ptr,
15816 const gdb_byte **new_info_ptr,
15817 struct die_info *parent)
15818 {
15819 struct die_info *first_die, *last_sibling;
15820 const gdb_byte *cur_ptr;
15821
15822 cur_ptr = info_ptr;
15823 first_die = last_sibling = NULL;
15824
15825 while (1)
15826 {
15827 struct die_info *die
15828 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15829
15830 if (die == NULL)
15831 {
15832 *new_info_ptr = cur_ptr;
15833 return first_die;
15834 }
15835
15836 if (!first_die)
15837 first_die = die;
15838 else
15839 last_sibling->sibling = die;
15840
15841 last_sibling = die;
15842 }
15843 }
15844
15845 /* Read a die, all of its descendents, and all of its siblings; set
15846 all of the fields of all of the dies correctly. Arguments are as
15847 in read_die_and_children.
15848 This the main entry point for reading a DIE and all its children. */
15849
15850 static struct die_info *
15851 read_die_and_siblings (const struct die_reader_specs *reader,
15852 const gdb_byte *info_ptr,
15853 const gdb_byte **new_info_ptr,
15854 struct die_info *parent)
15855 {
15856 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15857 new_info_ptr, parent);
15858
15859 if (dwarf_die_debug)
15860 {
15861 gdb_printf (gdb_stdlog,
15862 "Read die from %s@0x%x of %s:\n",
15863 reader->die_section->get_name (),
15864 (unsigned) (info_ptr - reader->die_section->buffer),
15865 bfd_get_filename (reader->abfd));
15866 die->dump (dwarf_die_debug);
15867 }
15868
15869 return die;
15870 }
15871
15872 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15873 attributes.
15874 The caller is responsible for filling in the extra attributes
15875 and updating (*DIEP)->num_attrs.
15876 Set DIEP to point to a newly allocated die with its information,
15877 except for its child, sibling, and parent fields. */
15878
15879 static const gdb_byte *
15880 read_full_die_1 (const struct die_reader_specs *reader,
15881 struct die_info **diep, const gdb_byte *info_ptr,
15882 int num_extra_attrs, bool allow_reprocess)
15883 {
15884 unsigned int abbrev_number, bytes_read, i;
15885 const struct abbrev_info *abbrev;
15886 struct die_info *die;
15887 struct dwarf2_cu *cu = reader->cu;
15888 bfd *abfd = reader->abfd;
15889
15890 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15891 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15892 info_ptr += bytes_read;
15893 if (!abbrev_number)
15894 {
15895 *diep = NULL;
15896 return info_ptr;
15897 }
15898
15899 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
15900 if (!abbrev)
15901 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15902 abbrev_number,
15903 bfd_get_filename (abfd));
15904
15905 die = die_info::allocate (&cu->comp_unit_obstack,
15906 abbrev->num_attrs + num_extra_attrs);
15907 die->sect_off = sect_off;
15908 die->tag = abbrev->tag;
15909 die->abbrev = abbrev_number;
15910 die->has_children = abbrev->has_children;
15911
15912 /* Make the result usable.
15913 The caller needs to update num_attrs after adding the extra
15914 attributes. */
15915 die->num_attrs = abbrev->num_attrs;
15916
15917 for (i = 0; i < abbrev->num_attrs; ++i)
15918 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15919 info_ptr, allow_reprocess);
15920
15921 *diep = die;
15922 return info_ptr;
15923 }
15924
15925 /* Read a die and all its attributes.
15926 Set DIEP to point to a newly allocated die with its information,
15927 except for its child, sibling, and parent fields. */
15928
15929 static const gdb_byte *
15930 read_toplevel_die (const struct die_reader_specs *reader,
15931 struct die_info **diep, const gdb_byte *info_ptr,
15932 gdb::array_view<attribute *> extra_attrs)
15933 {
15934 const gdb_byte *result;
15935 struct dwarf2_cu *cu = reader->cu;
15936
15937 result = read_full_die_1 (reader, diep, info_ptr, extra_attrs.size (),
15938 false);
15939
15940 /* Copy in the extra attributes, if any. */
15941 attribute *next = &(*diep)->attrs[(*diep)->num_attrs];
15942 for (attribute *extra : extra_attrs)
15943 *next++ = *extra;
15944
15945 struct attribute *attr = (*diep)->attr (DW_AT_str_offsets_base);
15946 if (attr != nullptr && attr->form_is_unsigned ())
15947 cu->str_offsets_base = attr->as_unsigned ();
15948
15949 attr = (*diep)->attr (DW_AT_loclists_base);
15950 if (attr != nullptr)
15951 cu->loclist_base = attr->as_unsigned ();
15952
15953 auto maybe_addr_base = (*diep)->addr_base ();
15954 if (maybe_addr_base.has_value ())
15955 cu->addr_base = *maybe_addr_base;
15956
15957 attr = (*diep)->attr (DW_AT_rnglists_base);
15958 if (attr != nullptr)
15959 cu->rnglists_base = attr->as_unsigned ();
15960
15961 for (int i = 0; i < (*diep)->num_attrs; ++i)
15962 {
15963 if ((*diep)->attrs[i].form_requires_reprocessing ())
15964 read_attribute_reprocess (reader, &(*diep)->attrs[i], (*diep)->tag);
15965 }
15966
15967 (*diep)->num_attrs += extra_attrs.size ();
15968
15969 if (dwarf_die_debug)
15970 {
15971 gdb_printf (gdb_stdlog,
15972 "Read die from %s@0x%x of %s:\n",
15973 reader->die_section->get_name (),
15974 (unsigned) (info_ptr - reader->die_section->buffer),
15975 bfd_get_filename (reader->abfd));
15976 (*diep)->dump (dwarf_die_debug);
15977 }
15978
15979 return result;
15980 }
15981 \f
15982
15983 void
15984 cooked_indexer::check_bounds (cutu_reader *reader)
15985 {
15986 if (reader->cu->per_cu->addresses_seen)
15987 return;
15988
15989 dwarf2_cu *cu = reader->cu;
15990
15991 CORE_ADDR best_lowpc = 0, best_highpc = 0;
15992 /* Possibly set the default values of LOWPC and HIGHPC from
15993 `DW_AT_ranges'. */
15994 dwarf2_find_base_address (reader->comp_unit_die, cu);
15995 enum pc_bounds_kind cu_bounds_kind
15996 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
15997 cu, m_index_storage->get_addrmap (), cu->per_cu);
15998 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
15999 {
16000 struct objfile *objfile = cu->per_objfile->objfile;
16001 CORE_ADDR baseaddr = objfile->text_section_offset ();
16002 struct gdbarch *gdbarch = objfile->arch ();
16003 CORE_ADDR low
16004 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
16005 - baseaddr);
16006 CORE_ADDR high
16007 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
16008 - baseaddr - 1);
16009 /* Store the contiguous range if it is not empty; it can be
16010 empty for CUs with no code. */
16011 m_index_storage->get_addrmap ()->set_empty (low, high, cu->per_cu);
16012
16013 cu->per_cu->addresses_seen = true;
16014 }
16015 }
16016
16017 /* Helper function that returns true if TAG can have a linkage
16018 name. */
16019
16020 static bool
16021 tag_can_have_linkage_name (enum dwarf_tag tag)
16022 {
16023 switch (tag)
16024 {
16025 case DW_TAG_variable:
16026 case DW_TAG_subprogram:
16027 return true;
16028
16029 default:
16030 return false;
16031 }
16032 }
16033
16034 cutu_reader *
16035 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
16036 dwarf2_per_objfile *per_objfile,
16037 sect_offset sect_off, bool is_dwz,
16038 bool for_scanning)
16039 {
16040 /* Lookups for type unit references are always in the CU, and
16041 cross-CU references will crash. */
16042 if (reader->cu->per_cu->is_dwz == is_dwz
16043 && reader->cu->header.offset_in_cu_p (sect_off))
16044 return reader;
16045
16046 dwarf2_per_cu_data *per_cu
16047 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
16048 per_objfile->per_bfd);
16049
16050 /* When scanning, we only want to visit a given CU a single time.
16051 Doing this check here avoids self-imports as well. */
16052 if (for_scanning)
16053 {
16054 bool nope = false;
16055 if (!per_cu->scanned.compare_exchange_strong (nope, true))
16056 return nullptr;
16057 }
16058 if (per_cu == m_per_cu)
16059 return reader;
16060
16061 cutu_reader *result = m_index_storage->get_reader (per_cu);
16062 if (result == nullptr)
16063 {
16064 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
16065 m_index_storage->get_abbrev_cache ());
16066
16067 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
16068 language_minimal);
16069 std::unique_ptr<cutu_reader> copy
16070 (new cutu_reader (std::move (new_reader)));
16071 result = m_index_storage->preserve (std::move (copy));
16072 }
16073
16074 if (result->dummy_p || !result->comp_unit_die->has_children)
16075 return nullptr;
16076
16077 if (for_scanning)
16078 check_bounds (result);
16079
16080 return result;
16081 }
16082
16083 const gdb_byte *
16084 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
16085 cutu_reader *reader,
16086 const gdb_byte *watermark_ptr,
16087 const gdb_byte *info_ptr,
16088 const abbrev_info *abbrev,
16089 const char **name,
16090 const char **linkage_name,
16091 cooked_index_flag *flags,
16092 sect_offset *sibling_offset,
16093 const cooked_index_entry **parent_entry,
16094 CORE_ADDR *maybe_defer,
16095 bool for_specification)
16096 {
16097 bool origin_is_dwz = false;
16098 bool is_declaration = false;
16099 sect_offset origin_offset {};
16100
16101 gdb::optional<CORE_ADDR> low_pc;
16102 gdb::optional<CORE_ADDR> high_pc;
16103 bool high_pc_relative = false;
16104
16105 for (int i = 0; i < abbrev->num_attrs; ++i)
16106 {
16107 attribute attr;
16108 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16109
16110 /* Store the data if it is of an attribute we want to keep in a
16111 partial symbol table. */
16112 switch (attr.name)
16113 {
16114 case DW_AT_name:
16115 switch (abbrev->tag)
16116 {
16117 case DW_TAG_compile_unit:
16118 case DW_TAG_partial_unit:
16119 case DW_TAG_type_unit:
16120 /* Compilation units have a DW_AT_name that is a filename, not
16121 a source language identifier. */
16122 break;
16123
16124 default:
16125 if (*name == nullptr)
16126 *name = attr.as_string ();
16127 break;
16128 }
16129 break;
16130
16131 case DW_AT_linkage_name:
16132 case DW_AT_MIPS_linkage_name:
16133 /* Note that both forms of linkage name might appear. We
16134 assume they will be the same, and we only store the last
16135 one we see. */
16136 if (*linkage_name == nullptr)
16137 *linkage_name = attr.as_string ();
16138 break;
16139
16140 case DW_AT_main_subprogram:
16141 if (attr.as_boolean ())
16142 *flags |= IS_MAIN;
16143 break;
16144
16145 case DW_AT_declaration:
16146 is_declaration = attr.as_boolean ();
16147 break;
16148
16149 case DW_AT_sibling:
16150 if (sibling_offset != nullptr)
16151 *sibling_offset = attr.get_ref_die_offset ();
16152 break;
16153
16154 case DW_AT_specification:
16155 case DW_AT_abstract_origin:
16156 case DW_AT_extension:
16157 origin_offset = attr.get_ref_die_offset ();
16158 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
16159 break;
16160
16161 case DW_AT_external:
16162 if (attr.as_boolean ())
16163 *flags &= ~IS_STATIC;
16164 break;
16165
16166 case DW_AT_enum_class:
16167 if (attr.as_boolean ())
16168 *flags |= IS_ENUM_CLASS;
16169 break;
16170
16171 case DW_AT_low_pc:
16172 low_pc = attr.as_address ();
16173 break;
16174
16175 case DW_AT_high_pc:
16176 high_pc = attr.as_address ();
16177 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
16178 high_pc_relative = true;
16179 break;
16180
16181 case DW_AT_location:
16182 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
16183 {
16184 struct dwarf_block *locdesc = attr.as_block ();
16185 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
16186 if (addr != 0
16187 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
16188 {
16189 low_pc = addr;
16190 /* For variables, we don't want to try decoding the
16191 type just to find the size -- for gdb's purposes
16192 we only need the address of a variable. */
16193 high_pc = addr + 1;
16194 high_pc_relative = false;
16195 }
16196 }
16197 break;
16198
16199 case DW_AT_ranges:
16200 if (!scanning_per_cu->addresses_seen)
16201 {
16202 /* Offset in the .debug_ranges or .debug_rnglist section
16203 (depending on DWARF version). */
16204 ULONGEST ranges_offset = attr.as_unsigned ();
16205
16206 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
16207 want to add this value. */
16208 ranges_offset += reader->cu->gnu_ranges_base;
16209
16210 CORE_ADDR lowpc, highpc;
16211 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
16212 m_index_storage->get_addrmap (),
16213 scanning_per_cu, abbrev->tag);
16214 }
16215 break;
16216 }
16217 }
16218
16219 /* We don't want to examine declarations, but if we found a
16220 declaration when handling DW_AT_specification or the like, then
16221 that is ok. Similarly, we allow an external variable without a
16222 location; those are resolved via minimal symbols. */
16223 if (is_declaration && !for_specification
16224 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
16225 {
16226 /* We always want to recurse into some types, but we may not
16227 want to treat them as definitions. */
16228 if ((abbrev->tag == DW_TAG_class_type
16229 || abbrev->tag == DW_TAG_structure_type
16230 || abbrev->tag == DW_TAG_union_type)
16231 && abbrev->has_children)
16232 *flags |= IS_TYPE_DECLARATION;
16233 else
16234 {
16235 *linkage_name = nullptr;
16236 *name = nullptr;
16237 }
16238 }
16239 else if ((*name == nullptr
16240 || (*linkage_name == nullptr
16241 && tag_can_have_linkage_name (abbrev->tag))
16242 || (*parent_entry == nullptr && m_language != language_c))
16243 && origin_offset != sect_offset (0))
16244 {
16245 cutu_reader *new_reader
16246 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
16247 origin_is_dwz, false);
16248 if (new_reader != nullptr)
16249 {
16250 const gdb_byte *new_info_ptr = (new_reader->buffer
16251 + to_underlying (origin_offset));
16252
16253 if (new_reader->cu == reader->cu
16254 && new_info_ptr > watermark_ptr
16255 && *parent_entry == nullptr)
16256 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
16257 else if (*parent_entry == nullptr)
16258 {
16259 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
16260 void *obj = m_die_range_map.find (lookup);
16261 *parent_entry = static_cast <cooked_index_entry *> (obj);
16262 }
16263
16264 unsigned int bytes_read;
16265 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
16266 new_info_ptr,
16267 &bytes_read);
16268 new_info_ptr += bytes_read;
16269 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
16270 new_abbrev, name, linkage_name, flags, nullptr,
16271 parent_entry, maybe_defer, true);
16272 }
16273 }
16274
16275 if (!for_specification)
16276 {
16277 if (m_language == language_ada
16278 && *linkage_name == nullptr)
16279 *linkage_name = *name;
16280
16281 if (!scanning_per_cu->addresses_seen
16282 && low_pc.has_value ()
16283 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
16284 || *low_pc != 0)
16285 && high_pc.has_value ())
16286 {
16287 if (high_pc_relative)
16288 high_pc = *high_pc + *low_pc;
16289
16290 if (*high_pc > *low_pc)
16291 {
16292 struct objfile *objfile = reader->cu->per_objfile->objfile;
16293 CORE_ADDR baseaddr = objfile->text_section_offset ();
16294 struct gdbarch *gdbarch = objfile->arch ();
16295 CORE_ADDR lo
16296 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
16297 - baseaddr);
16298 CORE_ADDR hi
16299 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
16300 - baseaddr);
16301 m_index_storage->get_addrmap ()->set_empty (lo, hi - 1,
16302 scanning_per_cu);
16303 }
16304 }
16305
16306 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
16307 *flags &= ~IS_STATIC;
16308
16309 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
16310 *name = "(anonymous namespace)";
16311
16312 if (m_language == language_cplus
16313 && (abbrev->tag == DW_TAG_class_type
16314 || abbrev->tag == DW_TAG_interface_type
16315 || abbrev->tag == DW_TAG_structure_type
16316 || abbrev->tag == DW_TAG_union_type
16317 || abbrev->tag == DW_TAG_enumeration_type
16318 || abbrev->tag == DW_TAG_enumerator))
16319 *flags &= ~IS_STATIC;
16320 }
16321
16322 return info_ptr;
16323 }
16324
16325 const gdb_byte *
16326 cooked_indexer::index_imported_unit (cutu_reader *reader,
16327 const gdb_byte *info_ptr,
16328 const abbrev_info *abbrev)
16329 {
16330 sect_offset sect_off {};
16331 bool is_dwz = false;
16332
16333 for (int i = 0; i < abbrev->num_attrs; ++i)
16334 {
16335 /* Note that we never need to reprocess attributes here. */
16336 attribute attr;
16337 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16338
16339 if (attr.name == DW_AT_import)
16340 {
16341 sect_off = attr.get_ref_die_offset ();
16342 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16343 || reader->cu->per_cu->is_dwz);
16344 }
16345 }
16346
16347 /* Did not find DW_AT_import. */
16348 if (sect_off == sect_offset (0))
16349 return info_ptr;
16350
16351 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
16352 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
16353 is_dwz, true);
16354 if (new_reader != nullptr)
16355 {
16356 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
16357
16358 reader->cu->add_dependence (new_reader->cu->per_cu);
16359 }
16360
16361 return info_ptr;
16362 }
16363
16364 const gdb_byte *
16365 cooked_indexer::recurse (cutu_reader *reader,
16366 const gdb_byte *info_ptr,
16367 const cooked_index_entry *parent_entry,
16368 bool fully)
16369 {
16370 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16371
16372 if (parent_entry != nullptr)
16373 {
16374 CORE_ADDR start = form_addr (parent_entry->die_offset,
16375 reader->cu->per_cu->is_dwz);
16376 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
16377 reader->cu->per_cu->is_dwz);
16378 m_die_range_map.set_empty (start, end, (void *) parent_entry);
16379 }
16380
16381 return info_ptr;
16382 }
16383
16384 const gdb_byte *
16385 cooked_indexer::index_dies (cutu_reader *reader,
16386 const gdb_byte *info_ptr,
16387 const cooked_index_entry *parent_entry,
16388 bool fully)
16389 {
16390 const gdb_byte *end_ptr = (reader->buffer
16391 + to_underlying (reader->cu->header.sect_off)
16392 + reader->cu->header.get_length_with_initial ());
16393
16394 while (info_ptr < end_ptr)
16395 {
16396 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
16397 unsigned int bytes_read;
16398 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
16399 &bytes_read);
16400 info_ptr += bytes_read;
16401 if (abbrev == nullptr)
16402 break;
16403
16404 if (abbrev->tag == DW_TAG_imported_unit)
16405 {
16406 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
16407 continue;
16408 }
16409
16410 if (!abbrev->interesting)
16411 {
16412 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
16413 if (fully && abbrev->has_children)
16414 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16415 continue;
16416 }
16417
16418 const char *name = nullptr;
16419 const char *linkage_name = nullptr;
16420 CORE_ADDR defer = 0;
16421 cooked_index_flag flags = IS_STATIC;
16422 sect_offset sibling {};
16423 const cooked_index_entry *this_parent_entry = parent_entry;
16424 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
16425 info_ptr, abbrev, &name, &linkage_name,
16426 &flags, &sibling, &this_parent_entry,
16427 &defer, false);
16428
16429 if (abbrev->tag == DW_TAG_namespace
16430 && m_language == language_cplus
16431 && strcmp (name, "::") == 0)
16432 {
16433 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
16434 generated bogus DW_TAG_namespace DIEs with a name of "::"
16435 for the global namespace. Work around this problem
16436 here. */
16437 name = nullptr;
16438 }
16439
16440 const cooked_index_entry *this_entry = nullptr;
16441 if (name != nullptr)
16442 {
16443 if (defer != 0)
16444 m_deferred_entries.push_back ({
16445 this_die, name, defer, abbrev->tag, flags
16446 });
16447 else
16448 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
16449 name, this_parent_entry,
16450 m_per_cu);
16451 }
16452
16453 if (linkage_name != nullptr)
16454 {
16455 /* We only want this to be "main" if it has a linkage name
16456 but not an ordinary name. */
16457 if (name != nullptr)
16458 flags = flags & ~IS_MAIN;
16459 /* Set the IS_LINKAGE on for everything except when functions
16460 have linkage name present but name is absent. */
16461 if (name != nullptr
16462 || (abbrev->tag != DW_TAG_subprogram
16463 && abbrev->tag != DW_TAG_inlined_subroutine
16464 && abbrev->tag != DW_TAG_entry_point))
16465 flags = flags | IS_LINKAGE;
16466 m_index_storage->add (this_die, abbrev->tag, flags,
16467 linkage_name, nullptr, m_per_cu);
16468 }
16469
16470 if (abbrev->has_children)
16471 {
16472 switch (abbrev->tag)
16473 {
16474 case DW_TAG_class_type:
16475 case DW_TAG_interface_type:
16476 case DW_TAG_structure_type:
16477 case DW_TAG_union_type:
16478 if (m_language != language_c && this_entry != nullptr)
16479 {
16480 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16481 continue;
16482 }
16483 break;
16484
16485 case DW_TAG_enumeration_type:
16486 /* We need to recurse even for an anonymous enumeration.
16487 Which scope we record as the parent scope depends on
16488 whether we're reading an "enum class". If so, we use
16489 the enum itself as the parent, yielding names like
16490 "enum_class::enumerator"; otherwise we inject the
16491 names into our own parent scope. */
16492 info_ptr = recurse (reader, info_ptr,
16493 ((flags & IS_ENUM_CLASS) == 0)
16494 ? parent_entry
16495 : this_entry,
16496 fully);
16497 continue;
16498
16499 case DW_TAG_module:
16500 if (this_entry == nullptr)
16501 break;
16502 /* FALLTHROUGH */
16503 case DW_TAG_namespace:
16504 /* We don't check THIS_ENTRY for a namespace, to handle
16505 the ancient G++ workaround pointed out above. */
16506 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16507 continue;
16508
16509 case DW_TAG_subprogram:
16510 if ((m_language == language_fortran
16511 || m_language == language_ada)
16512 && this_entry != nullptr)
16513 {
16514 info_ptr = recurse (reader, info_ptr, this_entry, true);
16515 continue;
16516 }
16517 break;
16518 }
16519
16520 if (sibling != sect_offset (0))
16521 {
16522 const gdb_byte *sibling_ptr
16523 = reader->buffer + to_underlying (sibling);
16524
16525 if (sibling_ptr < info_ptr)
16526 complaint (_("DW_AT_sibling points backwards"));
16527 else if (sibling_ptr > reader->buffer_end)
16528 reader->die_section->overflow_complaint ();
16529 else
16530 info_ptr = sibling_ptr;
16531 }
16532 else
16533 info_ptr = skip_children (reader, info_ptr);
16534 }
16535 }
16536
16537 return info_ptr;
16538 }
16539
16540 void
16541 cooked_indexer::make_index (cutu_reader *reader)
16542 {
16543 check_bounds (reader);
16544 find_file_and_directory (reader->comp_unit_die, reader->cu);
16545 if (!reader->comp_unit_die->has_children)
16546 return;
16547 index_dies (reader, reader->info_ptr, nullptr, false);
16548
16549 for (const auto &entry : m_deferred_entries)
16550 {
16551 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
16552 void *obj = m_die_range_map.find (key);
16553 cooked_index_entry *parent = static_cast <cooked_index_entry *> (obj);
16554 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
16555 entry.name, parent, m_per_cu);
16556 }
16557 }
16558
16559 /* An implementation of quick_symbol_functions for the cooked DWARF
16560 index. */
16561
16562 struct cooked_index_functions : public dwarf2_base_index_functions
16563 {
16564 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
16565 CORE_ADDR adjusted_pc) override;
16566
16567 struct compunit_symtab *find_compunit_symtab_by_address
16568 (struct objfile *objfile, CORE_ADDR address) override;
16569
16570 void dump (struct objfile *objfile) override
16571 {
16572 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16573 cooked_index *index
16574 = (gdb::checked_static_cast<cooked_index *>
16575 (per_objfile->per_bfd->index_table.get ()));
16576 if (index == nullptr)
16577 return;
16578
16579 gdb_printf ("Cooked index in use:\n");
16580 gdb_printf ("\n");
16581 index->dump (objfile->arch ());
16582 }
16583
16584 void expand_matching_symbols
16585 (struct objfile *,
16586 const lookup_name_info &lookup_name,
16587 domain_enum domain,
16588 int global,
16589 symbol_compare_ftype *ordered_compare) override;
16590
16591 bool expand_symtabs_matching
16592 (struct objfile *objfile,
16593 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16594 const lookup_name_info *lookup_name,
16595 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16596 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16597 block_search_flags search_flags,
16598 domain_enum domain,
16599 enum search_domain kind) override;
16600
16601 bool can_lazily_read_symbols () override
16602 {
16603 return true;
16604 }
16605
16606 void read_partial_symbols (struct objfile *objfile) override
16607 {
16608 if (dwarf2_has_info (objfile, nullptr))
16609 dwarf2_build_psymtabs (objfile);
16610 }
16611 };
16612
16613 dwarf2_per_cu_data *
16614 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
16615 CORE_ADDR adjusted_pc)
16616 {
16617 cooked_index *table
16618 = (gdb::checked_static_cast<cooked_index *>
16619 (per_bfd->index_table.get ()));
16620 if (table == nullptr)
16621 return nullptr;
16622 return table->lookup (adjusted_pc);
16623 }
16624
16625 struct compunit_symtab *
16626 cooked_index_functions::find_compunit_symtab_by_address
16627 (struct objfile *objfile, CORE_ADDR address)
16628 {
16629 if (objfile->sect_index_data == -1)
16630 return nullptr;
16631
16632 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16633 cooked_index *table
16634 = (gdb::checked_static_cast<cooked_index *>
16635 (per_objfile->per_bfd->index_table.get ()));
16636 if (table == nullptr)
16637 return nullptr;
16638
16639 CORE_ADDR baseaddr = objfile->data_section_offset ();
16640 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
16641 if (per_cu == nullptr)
16642 return nullptr;
16643
16644 return dw2_instantiate_symtab (per_cu, per_objfile, false);
16645 }
16646
16647 void
16648 cooked_index_functions::expand_matching_symbols
16649 (struct objfile *objfile,
16650 const lookup_name_info &lookup_name,
16651 domain_enum domain,
16652 int global,
16653 symbol_compare_ftype *ordered_compare)
16654 {
16655 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16656 cooked_index *table
16657 = (gdb::checked_static_cast<cooked_index *>
16658 (per_objfile->per_bfd->index_table.get ()));
16659 if (table == nullptr)
16660 return;
16661
16662 const block_search_flags search_flags = (global
16663 ? SEARCH_GLOBAL_BLOCK
16664 : SEARCH_STATIC_BLOCK);
16665 const language_defn *lang = language_def (language_ada);
16666 symbol_name_matcher_ftype *name_match
16667 = lang->get_symbol_name_matcher (lookup_name);
16668
16669 for (const cooked_index_entry *entry : table->all_entries ())
16670 {
16671 QUIT;
16672
16673 if (entry->parent_entry != nullptr)
16674 continue;
16675
16676 if (!entry->matches (search_flags)
16677 || !entry->matches (domain))
16678 continue;
16679
16680 if (name_match (entry->canonical, lookup_name, nullptr))
16681 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
16682 }
16683 }
16684
16685 bool
16686 cooked_index_functions::expand_symtabs_matching
16687 (struct objfile *objfile,
16688 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16689 const lookup_name_info *lookup_name,
16690 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16691 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16692 block_search_flags search_flags,
16693 domain_enum domain,
16694 enum search_domain kind)
16695 {
16696 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16697
16698 cooked_index *table
16699 = (gdb::checked_static_cast<cooked_index *>
16700 (per_objfile->per_bfd->index_table.get ()));
16701 if (table == nullptr)
16702 return true;
16703
16704 table->wait ();
16705
16706 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
16707
16708 /* This invariant is documented in quick-functions.h. */
16709 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
16710 if (lookup_name == nullptr)
16711 {
16712 for (dwarf2_per_cu_data *per_cu
16713 : all_units_range (per_objfile->per_bfd))
16714 {
16715 QUIT;
16716
16717 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
16718 file_matcher,
16719 expansion_notify))
16720 return false;
16721 }
16722 return true;
16723 }
16724
16725 lookup_name_info lookup_name_without_params
16726 = lookup_name->make_ignore_params ();
16727 bool completing = lookup_name->completion_mode ();
16728
16729 /* Unique styles of language splitting. */
16730 static const enum language unique_styles[] =
16731 {
16732 /* No splitting is also a style. */
16733 language_c,
16734 /* This includes Rust. */
16735 language_cplus,
16736 /* This includes Go. */
16737 language_d,
16738 language_ada
16739 };
16740
16741 for (enum language lang : unique_styles)
16742 {
16743 std::vector<gdb::string_view> name_vec
16744 = lookup_name_without_params.split_name (lang);
16745 std::string last_name = gdb::to_string (name_vec.back ());
16746
16747 for (const cooked_index_entry *entry : table->find (last_name,
16748 completing))
16749 {
16750 QUIT;
16751
16752 /* No need to consider symbols from expanded CUs. */
16753 if (per_objfile->symtab_set_p (entry->per_cu))
16754 continue;
16755
16756 /* If file-matching was done, we don't need to consider
16757 symbols from unmarked CUs. */
16758 if (file_matcher != nullptr && !entry->per_cu->mark)
16759 continue;
16760
16761 /* See if the symbol matches the type filter. */
16762 if (!entry->matches (search_flags)
16763 || !entry->matches (domain)
16764 || !entry->matches (kind))
16765 continue;
16766
16767 /* We've found the base name of the symbol; now walk its
16768 parentage chain, ensuring that each component
16769 matches. */
16770 bool found = true;
16771
16772 const cooked_index_entry *parent = entry->parent_entry;
16773 for (int i = name_vec.size () - 1; i > 0; --i)
16774 {
16775 /* If we ran out of entries, or if this segment doesn't
16776 match, this did not match. */
16777 if (parent == nullptr
16778 || strncmp (parent->name, name_vec[i - 1].data (),
16779 name_vec[i - 1].length ()) != 0)
16780 {
16781 found = false;
16782 break;
16783 }
16784
16785 parent = parent->parent_entry;
16786 }
16787
16788 if (!found)
16789 continue;
16790
16791 /* Might have been looking for "a::b" and found
16792 "x::a::b". */
16793 if (symbol_matcher == nullptr)
16794 {
16795 symbol_name_match_type match_type
16796 = lookup_name_without_params.match_type ();
16797 if ((match_type == symbol_name_match_type::FULL
16798 || (lang != language_ada
16799 && match_type == symbol_name_match_type::EXPRESSION))
16800 && parent != nullptr)
16801 continue;
16802 }
16803 else
16804 {
16805 auto_obstack temp_storage;
16806 const char *full_name = entry->full_name (&temp_storage);
16807 if (!symbol_matcher (full_name))
16808 continue;
16809 }
16810
16811 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
16812 file_matcher,
16813 expansion_notify))
16814 return false;
16815 }
16816 }
16817
16818 return true;
16819 }
16820
16821 /* Return a new cooked_index_functions object. */
16822
16823 static quick_symbol_functions_up
16824 make_cooked_index_funcs ()
16825 {
16826 return quick_symbol_functions_up (new cooked_index_functions);
16827 }
16828
16829 quick_symbol_functions_up
16830 cooked_index::make_quick_functions () const
16831 {
16832 return make_cooked_index_funcs ();
16833 }
16834
16835 \f
16836
16837 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
16838 contents from the given SECTION in the HEADER.
16839
16840 HEADER_OFFSET is the offset of the header in the section. */
16841 static void
16842 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
16843 struct dwarf2_section_info *section,
16844 sect_offset header_offset)
16845 {
16846 unsigned int bytes_read;
16847 bfd *abfd = section->get_bfd_owner ();
16848 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
16849
16850 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
16851 info_ptr += bytes_read;
16852
16853 header->version = read_2_bytes (abfd, info_ptr);
16854 info_ptr += 2;
16855
16856 header->addr_size = read_1_byte (abfd, info_ptr);
16857 info_ptr += 1;
16858
16859 header->segment_collector_size = read_1_byte (abfd, info_ptr);
16860 info_ptr += 1;
16861
16862 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
16863 }
16864
16865 /* Return the DW_AT_loclists_base value for the CU. */
16866 static ULONGEST
16867 lookup_loclist_base (struct dwarf2_cu *cu)
16868 {
16869 /* For the .dwo unit, the loclist_base points to the first offset following
16870 the header. The header consists of the following entities-
16871 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
16872 bit format)
16873 2. version (2 bytes)
16874 3. address size (1 byte)
16875 4. segment selector size (1 byte)
16876 5. offset entry count (4 bytes)
16877 These sizes are derived as per the DWARFv5 standard. */
16878 if (cu->dwo_unit != nullptr)
16879 {
16880 if (cu->header.initial_length_size == 4)
16881 return LOCLIST_HEADER_SIZE32;
16882 return LOCLIST_HEADER_SIZE64;
16883 }
16884 return cu->loclist_base;
16885 }
16886
16887 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
16888 array of offsets in the .debug_loclists section. */
16889
16890 static sect_offset
16891 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
16892 {
16893 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16894 struct objfile *objfile = per_objfile->objfile;
16895 bfd *abfd = objfile->obfd.get ();
16896 ULONGEST loclist_header_size =
16897 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
16898 : LOCLIST_HEADER_SIZE64);
16899 ULONGEST loclist_base = lookup_loclist_base (cu);
16900
16901 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
16902 ULONGEST start_offset =
16903 loclist_base + loclist_index * cu->header.offset_size;
16904
16905 /* Get loclists section. */
16906 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16907
16908 /* Read the loclists section content. */
16909 section->read (objfile);
16910 if (section->buffer == NULL)
16911 error (_("DW_FORM_loclistx used without .debug_loclists "
16912 "section [in module %s]"), objfile_name (objfile));
16913
16914 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
16915 so if loclist_base is smaller than the header size, we have a problem. */
16916 if (loclist_base < loclist_header_size)
16917 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
16918 objfile_name (objfile));
16919
16920 /* Read the header of the loclists contribution. */
16921 struct loclists_rnglists_header header;
16922 read_loclists_rnglists_header (&header, section,
16923 (sect_offset) (loclist_base - loclist_header_size));
16924
16925 /* Verify the loclist index is valid. */
16926 if (loclist_index >= header.offset_entry_count)
16927 error (_("DW_FORM_loclistx pointing outside of "
16928 ".debug_loclists offset array [in module %s]"),
16929 objfile_name (objfile));
16930
16931 /* Validate that reading won't go beyond the end of the section. */
16932 if (start_offset + cu->header.offset_size > section->size)
16933 error (_("Reading DW_FORM_loclistx index beyond end of"
16934 ".debug_loclists section [in module %s]"),
16935 objfile_name (objfile));
16936
16937 const gdb_byte *info_ptr = section->buffer + start_offset;
16938
16939 if (cu->header.offset_size == 4)
16940 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
16941 else
16942 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
16943 }
16944
16945 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
16946 array of offsets in the .debug_rnglists section. */
16947
16948 static sect_offset
16949 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
16950 dwarf_tag tag)
16951 {
16952 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
16953 struct objfile *objfile = dwarf2_per_objfile->objfile;
16954 bfd *abfd = objfile->obfd.get ();
16955 ULONGEST rnglist_header_size =
16956 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
16957 : RNGLIST_HEADER_SIZE64);
16958
16959 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
16960 .debug_rnglists.dwo section. The rnglists base given in the skeleton
16961 doesn't apply. */
16962 ULONGEST rnglist_base =
16963 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
16964
16965 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
16966 ULONGEST start_offset =
16967 rnglist_base + rnglist_index * cu->header.offset_size;
16968
16969 /* Get rnglists section. */
16970 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
16971
16972 /* Read the rnglists section content. */
16973 section->read (objfile);
16974 if (section->buffer == nullptr)
16975 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
16976 "[in module %s]"),
16977 objfile_name (objfile));
16978
16979 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
16980 so if rnglist_base is smaller than the header size, we have a problem. */
16981 if (rnglist_base < rnglist_header_size)
16982 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
16983 objfile_name (objfile));
16984
16985 /* Read the header of the rnglists contribution. */
16986 struct loclists_rnglists_header header;
16987 read_loclists_rnglists_header (&header, section,
16988 (sect_offset) (rnglist_base - rnglist_header_size));
16989
16990 /* Verify the rnglist index is valid. */
16991 if (rnglist_index >= header.offset_entry_count)
16992 error (_("DW_FORM_rnglistx index pointing outside of "
16993 ".debug_rnglists offset array [in module %s]"),
16994 objfile_name (objfile));
16995
16996 /* Validate that reading won't go beyond the end of the section. */
16997 if (start_offset + cu->header.offset_size > section->size)
16998 error (_("Reading DW_FORM_rnglistx index beyond end of"
16999 ".debug_rnglists section [in module %s]"),
17000 objfile_name (objfile));
17001
17002 const gdb_byte *info_ptr = section->buffer + start_offset;
17003
17004 if (cu->header.offset_size == 4)
17005 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
17006 else
17007 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
17008 }
17009
17010 /* Process the attributes that had to be skipped in the first round. These
17011 attributes are the ones that need str_offsets_base or addr_base attributes.
17012 They could not have been processed in the first round, because at the time
17013 the values of str_offsets_base or addr_base may not have been known. */
17014 static void
17015 read_attribute_reprocess (const struct die_reader_specs *reader,
17016 struct attribute *attr, dwarf_tag tag)
17017 {
17018 struct dwarf2_cu *cu = reader->cu;
17019 switch (attr->form)
17020 {
17021 case DW_FORM_addrx:
17022 case DW_FORM_GNU_addr_index:
17023 attr->set_address (read_addr_index (cu,
17024 attr->as_unsigned_reprocess ()));
17025 break;
17026 case DW_FORM_loclistx:
17027 {
17028 sect_offset loclists_sect_off
17029 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
17030
17031 attr->set_unsigned (to_underlying (loclists_sect_off));
17032 }
17033 break;
17034 case DW_FORM_rnglistx:
17035 {
17036 sect_offset rnglists_sect_off
17037 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
17038
17039 attr->set_unsigned (to_underlying (rnglists_sect_off));
17040 }
17041 break;
17042 case DW_FORM_strx:
17043 case DW_FORM_strx1:
17044 case DW_FORM_strx2:
17045 case DW_FORM_strx3:
17046 case DW_FORM_strx4:
17047 case DW_FORM_GNU_str_index:
17048 {
17049 unsigned int str_index = attr->as_unsigned_reprocess ();
17050 gdb_assert (!attr->canonical_string_p ());
17051 if (reader->dwo_file != NULL)
17052 attr->set_string_noncanonical (read_dwo_str_index (reader,
17053 str_index));
17054 else
17055 attr->set_string_noncanonical (read_stub_str_index (cu,
17056 str_index));
17057 break;
17058 }
17059 default:
17060 gdb_assert_not_reached ("Unexpected DWARF form.");
17061 }
17062 }
17063
17064 /* Read an attribute value described by an attribute form. */
17065
17066 static const gdb_byte *
17067 read_attribute_value (const struct die_reader_specs *reader,
17068 struct attribute *attr, unsigned form,
17069 LONGEST implicit_const, const gdb_byte *info_ptr,
17070 bool allow_reprocess)
17071 {
17072 struct dwarf2_cu *cu = reader->cu;
17073 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17074 struct objfile *objfile = per_objfile->objfile;
17075 bfd *abfd = reader->abfd;
17076 struct comp_unit_head *cu_header = &cu->header;
17077 unsigned int bytes_read;
17078 struct dwarf_block *blk;
17079
17080 attr->form = (enum dwarf_form) form;
17081 switch (form)
17082 {
17083 case DW_FORM_ref_addr:
17084 if (cu_header->version == 2)
17085 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
17086 &bytes_read));
17087 else
17088 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17089 &bytes_read));
17090 info_ptr += bytes_read;
17091 break;
17092 case DW_FORM_GNU_ref_alt:
17093 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17094 &bytes_read));
17095 info_ptr += bytes_read;
17096 break;
17097 case DW_FORM_addr:
17098 {
17099 struct gdbarch *gdbarch = objfile->arch ();
17100 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
17101 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
17102 attr->set_address (addr);
17103 info_ptr += bytes_read;
17104 }
17105 break;
17106 case DW_FORM_block2:
17107 blk = dwarf_alloc_block (cu);
17108 blk->size = read_2_bytes (abfd, info_ptr);
17109 info_ptr += 2;
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_block4:
17115 blk = dwarf_alloc_block (cu);
17116 blk->size = read_4_bytes (abfd, info_ptr);
17117 info_ptr += 4;
17118 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17119 info_ptr += blk->size;
17120 attr->set_block (blk);
17121 break;
17122 case DW_FORM_data2:
17123 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
17124 info_ptr += 2;
17125 break;
17126 case DW_FORM_data4:
17127 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
17128 info_ptr += 4;
17129 break;
17130 case DW_FORM_data8:
17131 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
17132 info_ptr += 8;
17133 break;
17134 case DW_FORM_data16:
17135 blk = dwarf_alloc_block (cu);
17136 blk->size = 16;
17137 blk->data = read_n_bytes (abfd, info_ptr, 16);
17138 info_ptr += 16;
17139 attr->set_block (blk);
17140 break;
17141 case DW_FORM_sec_offset:
17142 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17143 &bytes_read));
17144 info_ptr += bytes_read;
17145 break;
17146 case DW_FORM_loclistx:
17147 {
17148 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17149 &bytes_read));
17150 info_ptr += bytes_read;
17151 if (allow_reprocess)
17152 read_attribute_reprocess (reader, attr);
17153 }
17154 break;
17155 case DW_FORM_string:
17156 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
17157 &bytes_read));
17158 info_ptr += bytes_read;
17159 break;
17160 case DW_FORM_strp:
17161 if (!cu->per_cu->is_dwz)
17162 {
17163 attr->set_string_noncanonical
17164 (read_indirect_string (per_objfile,
17165 abfd, info_ptr, cu_header,
17166 &bytes_read));
17167 info_ptr += bytes_read;
17168 break;
17169 }
17170 /* FALLTHROUGH */
17171 case DW_FORM_line_strp:
17172 if (!cu->per_cu->is_dwz)
17173 {
17174 attr->set_string_noncanonical
17175 (per_objfile->read_line_string (info_ptr, cu_header,
17176 &bytes_read));
17177 info_ptr += bytes_read;
17178 break;
17179 }
17180 /* FALLTHROUGH */
17181 case DW_FORM_GNU_strp_alt:
17182 {
17183 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17184 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
17185 &bytes_read);
17186
17187 attr->set_string_noncanonical
17188 (dwz->read_string (objfile, str_offset));
17189 info_ptr += bytes_read;
17190 }
17191 break;
17192 case DW_FORM_exprloc:
17193 case DW_FORM_block:
17194 blk = dwarf_alloc_block (cu);
17195 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17196 info_ptr += bytes_read;
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_block1:
17202 blk = dwarf_alloc_block (cu);
17203 blk->size = read_1_byte (abfd, info_ptr);
17204 info_ptr += 1;
17205 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17206 info_ptr += blk->size;
17207 attr->set_block (blk);
17208 break;
17209 case DW_FORM_data1:
17210 case DW_FORM_flag:
17211 attr->set_unsigned (read_1_byte (abfd, info_ptr));
17212 info_ptr += 1;
17213 break;
17214 case DW_FORM_flag_present:
17215 attr->set_unsigned (1);
17216 break;
17217 case DW_FORM_sdata:
17218 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
17219 info_ptr += bytes_read;
17220 break;
17221 case DW_FORM_rnglistx:
17222 {
17223 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17224 &bytes_read));
17225 info_ptr += bytes_read;
17226 if (allow_reprocess)
17227 read_attribute_reprocess (reader, attr);
17228 }
17229 break;
17230 case DW_FORM_udata:
17231 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17232 info_ptr += bytes_read;
17233 break;
17234 case DW_FORM_ref1:
17235 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17236 + read_1_byte (abfd, info_ptr)));
17237 info_ptr += 1;
17238 break;
17239 case DW_FORM_ref2:
17240 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17241 + read_2_bytes (abfd, info_ptr)));
17242 info_ptr += 2;
17243 break;
17244 case DW_FORM_ref4:
17245 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17246 + read_4_bytes (abfd, info_ptr)));
17247 info_ptr += 4;
17248 break;
17249 case DW_FORM_ref8:
17250 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17251 + read_8_bytes (abfd, info_ptr)));
17252 info_ptr += 8;
17253 break;
17254 case DW_FORM_ref_sig8:
17255 attr->set_signature (read_8_bytes (abfd, info_ptr));
17256 info_ptr += 8;
17257 break;
17258 case DW_FORM_ref_udata:
17259 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17260 + read_unsigned_leb128 (abfd, info_ptr,
17261 &bytes_read)));
17262 info_ptr += bytes_read;
17263 break;
17264 case DW_FORM_indirect:
17265 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17266 info_ptr += bytes_read;
17267 if (form == DW_FORM_implicit_const)
17268 {
17269 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17270 info_ptr += bytes_read;
17271 }
17272 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17273 info_ptr, allow_reprocess);
17274 break;
17275 case DW_FORM_implicit_const:
17276 attr->set_signed (implicit_const);
17277 break;
17278 case DW_FORM_addrx:
17279 case DW_FORM_GNU_addr_index:
17280 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17281 &bytes_read));
17282 info_ptr += bytes_read;
17283 if (allow_reprocess)
17284 read_attribute_reprocess (reader, attr);
17285 break;
17286 case DW_FORM_strx:
17287 case DW_FORM_strx1:
17288 case DW_FORM_strx2:
17289 case DW_FORM_strx3:
17290 case DW_FORM_strx4:
17291 case DW_FORM_GNU_str_index:
17292 {
17293 ULONGEST str_index;
17294 if (form == DW_FORM_strx1)
17295 {
17296 str_index = read_1_byte (abfd, info_ptr);
17297 info_ptr += 1;
17298 }
17299 else if (form == DW_FORM_strx2)
17300 {
17301 str_index = read_2_bytes (abfd, info_ptr);
17302 info_ptr += 2;
17303 }
17304 else if (form == DW_FORM_strx3)
17305 {
17306 str_index = read_3_bytes (abfd, info_ptr);
17307 info_ptr += 3;
17308 }
17309 else if (form == DW_FORM_strx4)
17310 {
17311 str_index = read_4_bytes (abfd, info_ptr);
17312 info_ptr += 4;
17313 }
17314 else
17315 {
17316 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17317 info_ptr += bytes_read;
17318 }
17319 attr->set_unsigned_reprocess (str_index);
17320 if (allow_reprocess)
17321 read_attribute_reprocess (reader, attr);
17322 }
17323 break;
17324 default:
17325 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17326 dwarf_form_name (form),
17327 bfd_get_filename (abfd));
17328 }
17329
17330 /* Super hack. */
17331 if (cu->per_cu->is_dwz && attr->form_is_ref ())
17332 attr->form = DW_FORM_GNU_ref_alt;
17333
17334 /* We have seen instances where the compiler tried to emit a byte
17335 size attribute of -1 which ended up being encoded as an unsigned
17336 0xffffffff. Although 0xffffffff is technically a valid size value,
17337 an object of this size seems pretty unlikely so we can relatively
17338 safely treat these cases as if the size attribute was invalid and
17339 treat them as zero by default. */
17340 if (attr->name == DW_AT_byte_size
17341 && form == DW_FORM_data4
17342 && attr->as_unsigned () >= 0xffffffff)
17343 {
17344 complaint
17345 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17346 hex_string (attr->as_unsigned ()));
17347 attr->set_unsigned (0);
17348 }
17349
17350 return info_ptr;
17351 }
17352
17353 /* Read an attribute described by an abbreviated attribute. */
17354
17355 static const gdb_byte *
17356 read_attribute (const struct die_reader_specs *reader,
17357 struct attribute *attr, const struct attr_abbrev *abbrev,
17358 const gdb_byte *info_ptr,
17359 bool allow_reprocess)
17360 {
17361 attr->name = abbrev->name;
17362 attr->string_is_canonical = 0;
17363 return read_attribute_value (reader, attr, abbrev->form,
17364 abbrev->implicit_const, info_ptr,
17365 allow_reprocess);
17366 }
17367
17368 /* See read.h. */
17369
17370 const char *
17371 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
17372 LONGEST str_offset)
17373 {
17374 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
17375 str_offset, "DW_FORM_strp");
17376 }
17377
17378 /* Return pointer to string at .debug_str offset as read from BUF.
17379 BUF is assumed to be in a compilation unit described by CU_HEADER.
17380 Return *BYTES_READ_PTR count of bytes read from BUF. */
17381
17382 static const char *
17383 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
17384 const gdb_byte *buf,
17385 const struct comp_unit_head *cu_header,
17386 unsigned int *bytes_read_ptr)
17387 {
17388 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17389
17390 return read_indirect_string_at_offset (per_objfile, str_offset);
17391 }
17392
17393 /* See read.h. */
17394
17395 const char *
17396 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17397 unsigned int offset_size)
17398 {
17399 bfd *abfd = objfile->obfd.get ();
17400 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
17401
17402 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17403 }
17404
17405 /* See read.h. */
17406
17407 const char *
17408 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17409 const struct comp_unit_head *cu_header,
17410 unsigned int *bytes_read_ptr)
17411 {
17412 bfd *abfd = objfile->obfd.get ();
17413 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17414
17415 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17416 }
17417
17418 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17419 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
17420 ADDR_SIZE is the size of addresses from the CU header. */
17421
17422 static CORE_ADDR
17423 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
17424 gdb::optional<ULONGEST> addr_base, int addr_size)
17425 {
17426 struct objfile *objfile = per_objfile->objfile;
17427 bfd *abfd = objfile->obfd.get ();
17428 const gdb_byte *info_ptr;
17429 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
17430
17431 per_objfile->per_bfd->addr.read (objfile);
17432 if (per_objfile->per_bfd->addr.buffer == NULL)
17433 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17434 objfile_name (objfile));
17435 if (addr_base_or_zero + addr_index * addr_size
17436 >= per_objfile->per_bfd->addr.size)
17437 error (_("DW_FORM_addr_index pointing outside of "
17438 ".debug_addr section [in module %s]"),
17439 objfile_name (objfile));
17440 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
17441 + addr_index * addr_size);
17442 if (addr_size == 4)
17443 return bfd_get_32 (abfd, info_ptr);
17444 else
17445 return bfd_get_64 (abfd, info_ptr);
17446 }
17447
17448 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17449
17450 static CORE_ADDR
17451 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17452 {
17453 return read_addr_index_1 (cu->per_objfile, addr_index,
17454 cu->addr_base, cu->header.addr_size);
17455 }
17456
17457 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17458
17459 static CORE_ADDR
17460 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17461 unsigned int *bytes_read)
17462 {
17463 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
17464 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17465
17466 return read_addr_index (cu, addr_index);
17467 }
17468
17469 /* See read.h. */
17470
17471 CORE_ADDR
17472 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
17473 dwarf2_per_objfile *per_objfile,
17474 unsigned int addr_index)
17475 {
17476 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
17477 gdb::optional<ULONGEST> addr_base;
17478 int addr_size;
17479
17480 /* We need addr_base and addr_size.
17481 If we don't have PER_CU->cu, we have to get it.
17482 Nasty, but the alternative is storing the needed info in PER_CU,
17483 which at this point doesn't seem justified: it's not clear how frequently
17484 it would get used and it would increase the size of every PER_CU.
17485 Entry points like dwarf2_per_cu_addr_size do a similar thing
17486 so we're not in uncharted territory here.
17487 Alas we need to be a bit more complicated as addr_base is contained
17488 in the DIE.
17489
17490 We don't need to read the entire CU(/TU).
17491 We just need the header and top level die.
17492
17493 IWBN to use the aging mechanism to let us lazily later discard the CU.
17494 For now we skip this optimization. */
17495
17496 if (cu != NULL)
17497 {
17498 addr_base = cu->addr_base;
17499 addr_size = cu->header.addr_size;
17500 }
17501 else
17502 {
17503 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
17504 addr_base = reader.cu->addr_base;
17505 addr_size = reader.cu->header.addr_size;
17506 }
17507
17508 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
17509 }
17510
17511 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
17512 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
17513 DWO file. */
17514
17515 static const char *
17516 read_str_index (struct dwarf2_cu *cu,
17517 struct dwarf2_section_info *str_section,
17518 struct dwarf2_section_info *str_offsets_section,
17519 ULONGEST str_offsets_base, ULONGEST str_index,
17520 unsigned offset_size)
17521 {
17522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17523 struct objfile *objfile = per_objfile->objfile;
17524 const char *objf_name = objfile_name (objfile);
17525 bfd *abfd = objfile->obfd.get ();
17526 const gdb_byte *info_ptr;
17527 ULONGEST str_offset;
17528 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
17529
17530 str_section->read (objfile);
17531 str_offsets_section->read (objfile);
17532 if (str_section->buffer == NULL)
17533 error (_("%s used without %s section"
17534 " in CU at offset %s [in module %s]"),
17535 form_name, str_section->get_name (),
17536 sect_offset_str (cu->header.sect_off), objf_name);
17537 if (str_offsets_section->buffer == NULL)
17538 error (_("%s used without %s section"
17539 " in CU at offset %s [in module %s]"),
17540 form_name, str_section->get_name (),
17541 sect_offset_str (cu->header.sect_off), objf_name);
17542 info_ptr = (str_offsets_section->buffer
17543 + str_offsets_base
17544 + str_index * offset_size);
17545 if (offset_size == 4)
17546 str_offset = bfd_get_32 (abfd, info_ptr);
17547 else
17548 str_offset = bfd_get_64 (abfd, info_ptr);
17549 if (str_offset >= str_section->size)
17550 error (_("Offset from %s pointing outside of"
17551 " .debug_str.dwo section in CU at offset %s [in module %s]"),
17552 form_name, sect_offset_str (cu->header.sect_off), objf_name);
17553 return (const char *) (str_section->buffer + str_offset);
17554 }
17555
17556 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
17557
17558 static const char *
17559 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17560 {
17561 unsigned offset_size;
17562 ULONGEST str_offsets_base;
17563 if (reader->cu->header.version >= 5)
17564 {
17565 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
17566 so assume the .debug_str_offsets section is DWARF5 as well, and
17567 parse the header. FIXME: Parse the header only once. */
17568 unsigned int bytes_read = 0;
17569 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
17570 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
17571
17572 /* Header: Initial length. */
17573 read_initial_length (abfd, p + bytes_read, &bytes_read);
17574
17575 /* Determine offset_size based on the .debug_str_offsets header. */
17576 const bool dwarf5_is_dwarf64 = bytes_read != 4;
17577 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
17578
17579 /* Header: Version. */
17580 unsigned version = read_2_bytes (abfd, p + bytes_read);
17581 bytes_read += 2;
17582
17583 if (version <= 4)
17584 {
17585 /* We'd like one warning here about ignoring the section, but
17586 because we parse the header more than once (see FIXME above)
17587 we'd have many warnings, so use a complaint instead, which at
17588 least has a limit. */
17589 complaint (_("Section .debug_str_offsets in %s has unsupported"
17590 " version %d, use empty string."),
17591 reader->dwo_file->dwo_name, version);
17592 return "";
17593 }
17594
17595 /* Header: Padding. */
17596 bytes_read += 2;
17597
17598 str_offsets_base = bytes_read;
17599 }
17600 else
17601 {
17602 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
17603 section, assume the .debug_str_offsets section is pre-DWARF5 as
17604 well, which doesn't have a header. */
17605 str_offsets_base = 0;
17606
17607 /* Determine offset_size based on the .debug_info header. */
17608 offset_size = reader->cu->header.offset_size;
17609 }
17610
17611 return read_str_index (reader->cu,
17612 &reader->dwo_file->sections.str,
17613 &reader->dwo_file->sections.str_offsets,
17614 str_offsets_base, str_index, offset_size);
17615 }
17616
17617 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
17618
17619 static const char *
17620 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
17621 {
17622 struct objfile *objfile = cu->per_objfile->objfile;
17623 const char *objf_name = objfile_name (objfile);
17624 static const char form_name[] = "DW_FORM_GNU_str_index";
17625 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
17626
17627 if (!cu->str_offsets_base.has_value ())
17628 error (_("%s used in Fission stub without %s"
17629 " in CU at offset 0x%lx [in module %s]"),
17630 form_name, str_offsets_attr_name,
17631 (long) cu->header.offset_size, objf_name);
17632
17633 return read_str_index (cu,
17634 &cu->per_objfile->per_bfd->str,
17635 &cu->per_objfile->per_bfd->str_offsets,
17636 *cu->str_offsets_base, str_index,
17637 cu->header.offset_size);
17638 }
17639
17640 /* Return the length of an LEB128 number in BUF. */
17641
17642 static int
17643 leb128_size (const gdb_byte *buf)
17644 {
17645 const gdb_byte *begin = buf;
17646 gdb_byte byte;
17647
17648 while (1)
17649 {
17650 byte = *buf++;
17651 if ((byte & 128) == 0)
17652 return buf - begin;
17653 }
17654 }
17655
17656 static enum language
17657 dwarf_lang_to_enum_language (unsigned int lang)
17658 {
17659 enum language language;
17660
17661 switch (lang)
17662 {
17663 case DW_LANG_C89:
17664 case DW_LANG_C99:
17665 case DW_LANG_C11:
17666 case DW_LANG_C:
17667 case DW_LANG_UPC:
17668 language = language_c;
17669 break;
17670 case DW_LANG_Java:
17671 case DW_LANG_C_plus_plus:
17672 case DW_LANG_C_plus_plus_11:
17673 case DW_LANG_C_plus_plus_14:
17674 language = language_cplus;
17675 break;
17676 case DW_LANG_D:
17677 language = language_d;
17678 break;
17679 case DW_LANG_Fortran77:
17680 case DW_LANG_Fortran90:
17681 case DW_LANG_Fortran95:
17682 case DW_LANG_Fortran03:
17683 case DW_LANG_Fortran08:
17684 language = language_fortran;
17685 break;
17686 case DW_LANG_Go:
17687 language = language_go;
17688 break;
17689 case DW_LANG_Mips_Assembler:
17690 language = language_asm;
17691 break;
17692 case DW_LANG_Ada83:
17693 case DW_LANG_Ada95:
17694 language = language_ada;
17695 break;
17696 case DW_LANG_Modula2:
17697 language = language_m2;
17698 break;
17699 case DW_LANG_Pascal83:
17700 language = language_pascal;
17701 break;
17702 case DW_LANG_ObjC:
17703 language = language_objc;
17704 break;
17705 case DW_LANG_Rust:
17706 case DW_LANG_Rust_old:
17707 language = language_rust;
17708 break;
17709 case DW_LANG_OpenCL:
17710 language = language_opencl;
17711 break;
17712 case DW_LANG_Cobol74:
17713 case DW_LANG_Cobol85:
17714 default:
17715 language = language_minimal;
17716 break;
17717 }
17718
17719 return language;
17720 }
17721
17722 /* Return the named attribute or NULL if not there. */
17723
17724 static struct attribute *
17725 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17726 {
17727 for (;;)
17728 {
17729 unsigned int i;
17730 struct attribute *spec = NULL;
17731
17732 for (i = 0; i < die->num_attrs; ++i)
17733 {
17734 if (die->attrs[i].name == name)
17735 return &die->attrs[i];
17736 if (die->attrs[i].name == DW_AT_specification
17737 || die->attrs[i].name == DW_AT_abstract_origin)
17738 spec = &die->attrs[i];
17739 }
17740
17741 if (!spec)
17742 break;
17743
17744 die = follow_die_ref (die, spec, &cu);
17745 }
17746
17747 return NULL;
17748 }
17749
17750 /* Return the string associated with a string-typed attribute, or NULL if it
17751 is either not found or is of an incorrect type. */
17752
17753 static const char *
17754 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17755 {
17756 struct attribute *attr;
17757 const char *str = NULL;
17758
17759 attr = dwarf2_attr (die, name, cu);
17760
17761 if (attr != NULL)
17762 {
17763 str = attr->as_string ();
17764 if (str == nullptr)
17765 complaint (_("string type expected for attribute %s for "
17766 "DIE at %s in module %s"),
17767 dwarf_attr_name (name), sect_offset_str (die->sect_off),
17768 objfile_name (cu->per_objfile->objfile));
17769 }
17770
17771 return str;
17772 }
17773
17774 /* Return the dwo name or NULL if not present. If present, it is in either
17775 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
17776 static const char *
17777 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
17778 {
17779 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
17780 if (dwo_name == nullptr)
17781 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
17782 return dwo_name;
17783 }
17784
17785 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17786 and holds a non-zero value. This function should only be used for
17787 DW_FORM_flag or DW_FORM_flag_present attributes. */
17788
17789 static int
17790 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17791 {
17792 struct attribute *attr = dwarf2_attr (die, name, cu);
17793
17794 return attr != nullptr && attr->as_boolean ();
17795 }
17796
17797 static int
17798 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17799 {
17800 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17801 which value is non-zero. However, we have to be careful with
17802 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17803 (via dwarf2_flag_true_p) follows this attribute. So we may
17804 end up accidently finding a declaration attribute that belongs
17805 to a different DIE referenced by the specification attribute,
17806 even though the given DIE does not have a declaration attribute. */
17807 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17808 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17809 }
17810
17811 /* Return the die giving the specification for DIE, if there is
17812 one. *SPEC_CU is the CU containing DIE on input, and the CU
17813 containing the return value on output. If there is no
17814 specification, but there is an abstract origin, that is
17815 returned. */
17816
17817 static struct die_info *
17818 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17819 {
17820 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17821 *spec_cu);
17822
17823 if (spec_attr == NULL)
17824 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17825
17826 if (spec_attr == NULL)
17827 return NULL;
17828 else
17829 return follow_die_ref (die, spec_attr, spec_cu);
17830 }
17831
17832 /* A convenience function to find the proper .debug_line section for a CU. */
17833
17834 static struct dwarf2_section_info *
17835 get_debug_line_section (struct dwarf2_cu *cu)
17836 {
17837 struct dwarf2_section_info *section;
17838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17839
17840 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17841 DWO file. */
17842 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17843 section = &cu->dwo_unit->dwo_file->sections.line;
17844 else if (cu->per_cu->is_dwz)
17845 {
17846 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17847
17848 section = &dwz->line;
17849 }
17850 else
17851 section = &per_objfile->per_bfd->line;
17852
17853 return section;
17854 }
17855
17856 /* Read the statement program header starting at OFFSET in
17857 .debug_line, or .debug_line.dwo. Return a pointer
17858 to a struct line_header, allocated using xmalloc.
17859 Returns NULL if there is a problem reading the header, e.g., if it
17860 has a version we don't understand.
17861
17862 NOTE: the strings in the include directory and file name tables of
17863 the returned object point into the dwarf line section buffer,
17864 and must not be freed. */
17865
17866 static line_header_up
17867 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
17868 const char *comp_dir)
17869 {
17870 struct dwarf2_section_info *section;
17871 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17872
17873 section = get_debug_line_section (cu);
17874 section->read (per_objfile->objfile);
17875 if (section->buffer == NULL)
17876 {
17877 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17878 complaint (_("missing .debug_line.dwo section"));
17879 else
17880 complaint (_("missing .debug_line section"));
17881 return 0;
17882 }
17883
17884 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
17885 per_objfile, section, &cu->header,
17886 comp_dir);
17887 }
17888
17889 /* Subroutine of dwarf_decode_lines to simplify it.
17890 Return the file name for the given file_entry.
17891 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
17892 If space for the result is malloc'd, *NAME_HOLDER will be set.
17893 Returns NULL if FILE_INDEX should be ignored, i.e., it is
17894 equivalent to CU_INFO. */
17895
17896 static const char *
17897 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
17898 const file_and_directory &cu_info,
17899 std::string &name_holder)
17900 {
17901 const char *include_name = fe.name;
17902 const char *include_name_to_compare = include_name;
17903
17904 const char *dir_name = fe.include_dir (lh);
17905
17906 std::string hold_compare;
17907 if (!IS_ABSOLUTE_PATH (include_name)
17908 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
17909 {
17910 /* Avoid creating a duplicate name for CU_INFO.
17911 We do this by comparing INCLUDE_NAME and CU_INFO.
17912 Before we do the comparison, however, we need to account
17913 for DIR_NAME and COMP_DIR.
17914 First prepend dir_name (if non-NULL). If we still don't
17915 have an absolute path prepend comp_dir (if non-NULL).
17916 However, the directory we record in the include-file's
17917 psymtab does not contain COMP_DIR (to match the
17918 corresponding symtab(s)).
17919
17920 Example:
17921
17922 bash$ cd /tmp
17923 bash$ gcc -g ./hello.c
17924 include_name = "hello.c"
17925 dir_name = "."
17926 DW_AT_comp_dir = comp_dir = "/tmp"
17927 DW_AT_name = "./hello.c"
17928
17929 */
17930
17931 if (dir_name != NULL)
17932 {
17933 name_holder = path_join (dir_name, include_name);
17934 include_name = name_holder.c_str ();
17935 include_name_to_compare = include_name;
17936 }
17937 if (!IS_ABSOLUTE_PATH (include_name)
17938 && cu_info.get_comp_dir () != nullptr)
17939 {
17940 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
17941 include_name_to_compare = hold_compare.c_str ();
17942 }
17943 }
17944
17945 std::string copied_name;
17946 const char *cu_filename = cu_info.get_name ();
17947 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
17948 {
17949 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
17950 cu_filename = copied_name.c_str ();
17951 }
17952
17953 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
17954 return nullptr;
17955 return include_name;
17956 }
17957
17958 /* State machine to track the state of the line number program. */
17959
17960 class lnp_state_machine
17961 {
17962 public:
17963 /* Initialize a machine state for the start of a line number
17964 program. */
17965 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
17966
17967 file_entry *current_file ()
17968 {
17969 /* lh->file_names is 0-based, but the file name numbers in the
17970 statement program are 1-based. */
17971 return m_line_header->file_name_at (m_file);
17972 }
17973
17974 /* Record the line in the state machine. END_SEQUENCE is true if
17975 we're processing the end of a sequence. */
17976 void record_line (bool end_sequence);
17977
17978 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
17979 nop-out rest of the lines in this sequence. */
17980 void check_line_address (struct dwarf2_cu *cu,
17981 const gdb_byte *line_ptr,
17982 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
17983
17984 void handle_set_discriminator (unsigned int discriminator)
17985 {
17986 m_discriminator = discriminator;
17987 m_line_has_non_zero_discriminator |= discriminator != 0;
17988 }
17989
17990 /* Handle DW_LNE_set_address. */
17991 void handle_set_address (CORE_ADDR address)
17992 {
17993 m_op_index = 0;
17994 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
17995 }
17996
17997 /* Handle DW_LNS_advance_pc. */
17998 void handle_advance_pc (CORE_ADDR adjust);
17999
18000 /* Handle a special opcode. */
18001 void handle_special_opcode (unsigned char op_code);
18002
18003 /* Handle DW_LNS_advance_line. */
18004 void handle_advance_line (int line_delta)
18005 {
18006 advance_line (line_delta);
18007 }
18008
18009 /* Handle DW_LNS_set_file. */
18010 void handle_set_file (file_name_index file);
18011
18012 /* Handle DW_LNS_negate_stmt. */
18013 void handle_negate_stmt ()
18014 {
18015 m_flags ^= LEF_IS_STMT;
18016 }
18017
18018 /* Handle DW_LNS_const_add_pc. */
18019 void handle_const_add_pc ();
18020
18021 /* Handle DW_LNS_fixed_advance_pc. */
18022 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18023 {
18024 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18025 m_op_index = 0;
18026 }
18027
18028 /* Handle DW_LNS_copy. */
18029 void handle_copy ()
18030 {
18031 record_line (false);
18032 m_discriminator = 0;
18033 m_flags &= ~LEF_PROLOGUE_END;
18034 }
18035
18036 /* Handle DW_LNE_end_sequence. */
18037 void handle_end_sequence ()
18038 {
18039 m_currently_recording_lines = true;
18040 }
18041
18042 /* Handle DW_LNS_set_prologue_end. */
18043 void handle_set_prologue_end ()
18044 {
18045 m_flags |= LEF_PROLOGUE_END;
18046 }
18047
18048 private:
18049 /* Advance the line by LINE_DELTA. */
18050 void advance_line (int line_delta)
18051 {
18052 m_line += line_delta;
18053
18054 if (line_delta != 0)
18055 m_line_has_non_zero_discriminator = m_discriminator != 0;
18056 }
18057
18058 struct dwarf2_cu *m_cu;
18059
18060 gdbarch *m_gdbarch;
18061
18062 /* The line number header. */
18063 line_header *m_line_header;
18064
18065 /* These are part of the standard DWARF line number state machine,
18066 and initialized according to the DWARF spec. */
18067
18068 unsigned char m_op_index = 0;
18069 /* The line table index of the current file. */
18070 file_name_index m_file = 1;
18071 unsigned int m_line = 1;
18072
18073 /* These are initialized in the constructor. */
18074
18075 CORE_ADDR m_address;
18076 linetable_entry_flags m_flags;
18077 unsigned int m_discriminator = 0;
18078
18079 /* Additional bits of state we need to track. */
18080
18081 /* The last file that we called dwarf2_start_subfile for.
18082 This is only used for TLLs. */
18083 unsigned int m_last_file = 0;
18084 /* The last file a line number was recorded for. */
18085 struct subfile *m_last_subfile = NULL;
18086
18087 /* The address of the last line entry. */
18088 CORE_ADDR m_last_address;
18089
18090 /* Set to true when a previous line at the same address (using
18091 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
18092 when a line entry at a new address (m_address different to
18093 m_last_address) is processed. */
18094 bool m_stmt_at_address = false;
18095
18096 /* When true, record the lines we decode. */
18097 bool m_currently_recording_lines = true;
18098
18099 /* The last line number that was recorded, used to coalesce
18100 consecutive entries for the same line. This can happen, for
18101 example, when discriminators are present. PR 17276. */
18102 unsigned int m_last_line = 0;
18103 bool m_line_has_non_zero_discriminator = false;
18104 };
18105
18106 void
18107 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18108 {
18109 CORE_ADDR addr_adj = (((m_op_index + adjust)
18110 / m_line_header->maximum_ops_per_instruction)
18111 * m_line_header->minimum_instruction_length);
18112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18113 m_op_index = ((m_op_index + adjust)
18114 % m_line_header->maximum_ops_per_instruction);
18115 }
18116
18117 void
18118 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18119 {
18120 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18121 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
18122 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
18123 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
18124 / m_line_header->maximum_ops_per_instruction)
18125 * m_line_header->minimum_instruction_length);
18126 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18127 m_op_index = ((m_op_index + adj_opcode_d)
18128 % m_line_header->maximum_ops_per_instruction);
18129
18130 int line_delta = m_line_header->line_base + adj_opcode_r;
18131 advance_line (line_delta);
18132 record_line (false);
18133 m_discriminator = 0;
18134 m_flags &= ~LEF_PROLOGUE_END;
18135 }
18136
18137 void
18138 lnp_state_machine::handle_set_file (file_name_index file)
18139 {
18140 m_file = file;
18141
18142 const file_entry *fe = current_file ();
18143 if (fe == NULL)
18144 dwarf2_debug_line_missing_file_complaint ();
18145 else
18146 {
18147 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18148 m_line_has_non_zero_discriminator = m_discriminator != 0;
18149 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
18150 }
18151 }
18152
18153 void
18154 lnp_state_machine::handle_const_add_pc ()
18155 {
18156 CORE_ADDR adjust
18157 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18158
18159 CORE_ADDR addr_adj
18160 = (((m_op_index + adjust)
18161 / m_line_header->maximum_ops_per_instruction)
18162 * m_line_header->minimum_instruction_length);
18163
18164 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18165 m_op_index = ((m_op_index + adjust)
18166 % m_line_header->maximum_ops_per_instruction);
18167 }
18168
18169 /* Return non-zero if we should add LINE to the line number table.
18170 LINE is the line to add, LAST_LINE is the last line that was added,
18171 LAST_SUBFILE is the subfile for LAST_LINE.
18172 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18173 had a non-zero discriminator.
18174
18175 We have to be careful in the presence of discriminators.
18176 E.g., for this line:
18177
18178 for (i = 0; i < 100000; i++);
18179
18180 clang can emit four line number entries for that one line,
18181 each with a different discriminator.
18182 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18183
18184 However, we want gdb to coalesce all four entries into one.
18185 Otherwise the user could stepi into the middle of the line and
18186 gdb would get confused about whether the pc really was in the
18187 middle of the line.
18188
18189 Things are further complicated by the fact that two consecutive
18190 line number entries for the same line is a heuristic used by gcc
18191 to denote the end of the prologue. So we can't just discard duplicate
18192 entries, we have to be selective about it. The heuristic we use is
18193 that we only collapse consecutive entries for the same line if at least
18194 one of those entries has a non-zero discriminator. PR 17276.
18195
18196 Note: Addresses in the line number state machine can never go backwards
18197 within one sequence, thus this coalescing is ok. */
18198
18199 static int
18200 dwarf_record_line_p (struct dwarf2_cu *cu,
18201 unsigned int line, unsigned int last_line,
18202 int line_has_non_zero_discriminator,
18203 struct subfile *last_subfile)
18204 {
18205 if (cu->get_builder ()->get_current_subfile () != last_subfile)
18206 return 1;
18207 if (line != last_line)
18208 return 1;
18209 /* Same line for the same file that we've seen already.
18210 As a last check, for pr 17276, only record the line if the line
18211 has never had a non-zero discriminator. */
18212 if (!line_has_non_zero_discriminator)
18213 return 1;
18214 return 0;
18215 }
18216
18217 /* Use the CU's builder to record line number LINE beginning at
18218 address ADDRESS in the line table of subfile SUBFILE. */
18219
18220 static void
18221 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18222 unsigned int line, CORE_ADDR address,
18223 linetable_entry_flags flags,
18224 struct dwarf2_cu *cu)
18225 {
18226 unrelocated_addr addr
18227 = unrelocated_addr (gdbarch_addr_bits_remove (gdbarch, address));
18228
18229 if (dwarf_line_debug)
18230 {
18231 gdb_printf (gdb_stdlog,
18232 "Recording line %u, file %s, address %s\n",
18233 line, lbasename (subfile->name.c_str ()),
18234 paddress (gdbarch, address));
18235 }
18236
18237 if (cu != nullptr)
18238 cu->get_builder ()->record_line (subfile, line, addr, flags);
18239 }
18240
18241 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18242 Mark the end of a set of line number records.
18243 The arguments are the same as for dwarf_record_line_1.
18244 If SUBFILE is NULL the request is ignored. */
18245
18246 static void
18247 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18248 CORE_ADDR address, struct dwarf2_cu *cu)
18249 {
18250 if (subfile == NULL)
18251 return;
18252
18253 if (dwarf_line_debug)
18254 {
18255 gdb_printf (gdb_stdlog,
18256 "Finishing current line, file %s, address %s\n",
18257 lbasename (subfile->name.c_str ()),
18258 paddress (gdbarch, address));
18259 }
18260
18261 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
18262 }
18263
18264 void
18265 lnp_state_machine::record_line (bool end_sequence)
18266 {
18267 if (dwarf_line_debug)
18268 {
18269 gdb_printf (gdb_stdlog,
18270 "Processing actual line %u: file %u,"
18271 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
18272 m_line, m_file,
18273 paddress (m_gdbarch, m_address),
18274 (m_flags & LEF_IS_STMT) != 0,
18275 (m_flags & LEF_PROLOGUE_END) != 0,
18276 m_discriminator,
18277 (end_sequence ? "\t(end sequence)" : ""));
18278 }
18279
18280 file_entry *fe = current_file ();
18281
18282 if (fe == NULL)
18283 dwarf2_debug_line_missing_file_complaint ();
18284 /* For now we ignore lines not starting on an instruction boundary.
18285 But not when processing end_sequence for compatibility with the
18286 previous version of the code. */
18287 else if (m_op_index == 0 || end_sequence)
18288 {
18289 /* When we switch files we insert an end maker in the first file,
18290 switch to the second file and add a new line entry. The
18291 problem is that the end marker inserted in the first file will
18292 discard any previous line entries at the same address. If the
18293 line entries in the first file are marked as is-stmt, while
18294 the new line in the second file is non-stmt, then this means
18295 the end marker will discard is-stmt lines so we can have a
18296 non-stmt line. This means that there are less addresses at
18297 which the user can insert a breakpoint.
18298
18299 To improve this we track the last address in m_last_address,
18300 and whether we have seen an is-stmt at this address. Then
18301 when switching files, if we have seen a stmt at the current
18302 address, and we are switching to create a non-stmt line, then
18303 discard the new line. */
18304 bool file_changed
18305 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
18306 bool ignore_this_line
18307 = ((file_changed && !end_sequence && m_last_address == m_address
18308 && ((m_flags & LEF_IS_STMT) == 0)
18309 && m_stmt_at_address)
18310 || (!end_sequence && m_line == 0));
18311
18312 if ((file_changed && !ignore_this_line) || end_sequence)
18313 {
18314 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
18315 m_currently_recording_lines ? m_cu : nullptr);
18316 }
18317
18318 if (!end_sequence && !ignore_this_line)
18319 {
18320 linetable_entry_flags lte_flags = m_flags;
18321 if (producer_is_codewarrior (m_cu))
18322 lte_flags |= LEF_IS_STMT;
18323
18324 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
18325 m_line_has_non_zero_discriminator,
18326 m_last_subfile))
18327 {
18328 buildsym_compunit *builder = m_cu->get_builder ();
18329 dwarf_record_line_1 (m_gdbarch,
18330 builder->get_current_subfile (),
18331 m_line, m_address, lte_flags,
18332 m_currently_recording_lines ? m_cu : nullptr);
18333 }
18334 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18335 m_last_line = m_line;
18336 }
18337 }
18338
18339 /* Track whether we have seen any IS_STMT true at m_address in case we
18340 have multiple line table entries all at m_address. */
18341 if (m_last_address != m_address)
18342 {
18343 m_stmt_at_address = false;
18344 m_last_address = m_address;
18345 }
18346 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
18347 }
18348
18349 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
18350 line_header *lh)
18351 : m_cu (cu),
18352 m_gdbarch (arch),
18353 m_line_header (lh),
18354 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
18355 if there was a line entry for it so that the backend has a
18356 chance to adjust it and also record it in case it needs it.
18357 This is currently used by MIPS code,
18358 cf. `mips_adjust_dwarf2_line'. */
18359 m_address (gdbarch_adjust_dwarf2_line (arch, 0, 0)),
18360 m_flags (lh->default_is_stmt ? LEF_IS_STMT : (linetable_entry_flags) 0),
18361 m_last_address (m_address)
18362 {
18363 }
18364
18365 void
18366 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18367 const gdb_byte *line_ptr,
18368 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
18369 {
18370 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
18371 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
18372 located at 0x0. In this case, additionally check that if
18373 ADDRESS < UNRELOCATED_LOWPC. */
18374
18375 if ((address == 0 && address < unrelocated_lowpc)
18376 || address == (CORE_ADDR) -1)
18377 {
18378 /* This line table is for a function which has been
18379 GCd by the linker. Ignore it. PR gdb/12528 */
18380
18381 struct objfile *objfile = cu->per_objfile->objfile;
18382 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18383
18384 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18385 line_offset, objfile_name (objfile));
18386 m_currently_recording_lines = false;
18387 /* Note: m_currently_recording_lines is left as false until we see
18388 DW_LNE_end_sequence. */
18389 }
18390 }
18391
18392 /* Subroutine of dwarf_decode_lines to simplify it.
18393 Process the line number information in LH. */
18394
18395 static void
18396 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18397 CORE_ADDR lowpc)
18398 {
18399 const gdb_byte *line_ptr, *extended_end;
18400 const gdb_byte *line_end;
18401 unsigned int bytes_read, extended_len;
18402 unsigned char op_code, extended_op;
18403 CORE_ADDR baseaddr;
18404 struct objfile *objfile = cu->per_objfile->objfile;
18405 bfd *abfd = objfile->obfd.get ();
18406 struct gdbarch *gdbarch = objfile->arch ();
18407
18408 baseaddr = objfile->text_section_offset ();
18409
18410 line_ptr = lh->statement_program_start;
18411 line_end = lh->statement_program_end;
18412
18413 /* Read the statement sequences until there's nothing left. */
18414 while (line_ptr < line_end)
18415 {
18416 /* The DWARF line number program state machine. Reset the state
18417 machine at the start of each sequence. */
18418 lnp_state_machine state_machine (cu, gdbarch, lh);
18419 bool end_sequence = false;
18420
18421 /* Start a subfile for the current file of the state
18422 machine. */
18423 const file_entry *fe = state_machine.current_file ();
18424
18425 if (fe != NULL)
18426 dwarf2_start_subfile (cu, *fe, *lh);
18427
18428 /* Decode the table. */
18429 while (line_ptr < line_end && !end_sequence)
18430 {
18431 op_code = read_1_byte (abfd, line_ptr);
18432 line_ptr += 1;
18433
18434 if (op_code >= lh->opcode_base)
18435 {
18436 /* Special opcode. */
18437 state_machine.handle_special_opcode (op_code);
18438 }
18439 else switch (op_code)
18440 {
18441 case DW_LNS_extended_op:
18442 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18443 &bytes_read);
18444 line_ptr += bytes_read;
18445 extended_end = line_ptr + extended_len;
18446 extended_op = read_1_byte (abfd, line_ptr);
18447 line_ptr += 1;
18448 if (DW_LNE_lo_user <= extended_op
18449 && extended_op <= DW_LNE_hi_user)
18450 {
18451 /* Vendor extension, ignore. */
18452 line_ptr = extended_end;
18453 break;
18454 }
18455 switch (extended_op)
18456 {
18457 case DW_LNE_end_sequence:
18458 state_machine.handle_end_sequence ();
18459 end_sequence = true;
18460 break;
18461 case DW_LNE_set_address:
18462 {
18463 CORE_ADDR address
18464 = cu->header.read_address (abfd, line_ptr, &bytes_read);
18465 line_ptr += bytes_read;
18466
18467 state_machine.check_line_address (cu, line_ptr,
18468 lowpc - baseaddr, address);
18469 state_machine.handle_set_address (address);
18470 }
18471 break;
18472 case DW_LNE_define_file:
18473 {
18474 const char *cur_file;
18475 unsigned int mod_time, length;
18476 dir_index dindex;
18477
18478 cur_file = read_direct_string (abfd, line_ptr,
18479 &bytes_read);
18480 line_ptr += bytes_read;
18481 dindex = (dir_index)
18482 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18483 line_ptr += bytes_read;
18484 mod_time =
18485 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18486 line_ptr += bytes_read;
18487 length =
18488 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18489 line_ptr += bytes_read;
18490 lh->add_file_name (cur_file, dindex, mod_time, length);
18491 }
18492 break;
18493 case DW_LNE_set_discriminator:
18494 {
18495 /* The discriminator is not interesting to the
18496 debugger; just ignore it. We still need to
18497 check its value though:
18498 if there are consecutive entries for the same
18499 (non-prologue) line we want to coalesce them.
18500 PR 17276. */
18501 unsigned int discr
18502 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18503 line_ptr += bytes_read;
18504
18505 state_machine.handle_set_discriminator (discr);
18506 }
18507 break;
18508 default:
18509 complaint (_("mangled .debug_line section"));
18510 return;
18511 }
18512 /* Make sure that we parsed the extended op correctly. If e.g.
18513 we expected a different address size than the producer used,
18514 we may have read the wrong number of bytes. */
18515 if (line_ptr != extended_end)
18516 {
18517 complaint (_("mangled .debug_line section"));
18518 return;
18519 }
18520 break;
18521 case DW_LNS_copy:
18522 state_machine.handle_copy ();
18523 break;
18524 case DW_LNS_advance_pc:
18525 {
18526 CORE_ADDR adjust
18527 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18528 line_ptr += bytes_read;
18529
18530 state_machine.handle_advance_pc (adjust);
18531 }
18532 break;
18533 case DW_LNS_advance_line:
18534 {
18535 int line_delta
18536 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18537 line_ptr += bytes_read;
18538
18539 state_machine.handle_advance_line (line_delta);
18540 }
18541 break;
18542 case DW_LNS_set_file:
18543 {
18544 file_name_index file
18545 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18546 &bytes_read);
18547 line_ptr += bytes_read;
18548
18549 state_machine.handle_set_file (file);
18550 }
18551 break;
18552 case DW_LNS_set_column:
18553 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18554 line_ptr += bytes_read;
18555 break;
18556 case DW_LNS_negate_stmt:
18557 state_machine.handle_negate_stmt ();
18558 break;
18559 case DW_LNS_set_basic_block:
18560 break;
18561 /* Add to the address register of the state machine the
18562 address increment value corresponding to special opcode
18563 255. I.e., this value is scaled by the minimum
18564 instruction length since special opcode 255 would have
18565 scaled the increment. */
18566 case DW_LNS_const_add_pc:
18567 state_machine.handle_const_add_pc ();
18568 break;
18569 case DW_LNS_fixed_advance_pc:
18570 {
18571 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18572 line_ptr += 2;
18573
18574 state_machine.handle_fixed_advance_pc (addr_adj);
18575 }
18576 break;
18577 case DW_LNS_set_prologue_end:
18578 state_machine.handle_set_prologue_end ();
18579 break;
18580 default:
18581 {
18582 /* Unknown standard opcode, ignore it. */
18583 int i;
18584
18585 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18586 {
18587 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18588 line_ptr += bytes_read;
18589 }
18590 }
18591 }
18592 }
18593
18594 if (!end_sequence)
18595 dwarf2_debug_line_missing_end_sequence_complaint ();
18596
18597 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18598 in which case we still finish recording the last line). */
18599 state_machine.record_line (true);
18600 }
18601 }
18602
18603 /* Decode the Line Number Program (LNP) for the given line_header
18604 structure and CU. The actual information extracted and the type
18605 of structures created from the LNP depends on the value of PST.
18606
18607 FND holds the CU file name and directory, if known.
18608 It is used for relative paths in the line table.
18609
18610 NOTE: It is important that psymtabs have the same file name (via
18611 strcmp) as the corresponding symtab. Since the directory is not
18612 used in the name of the symtab we don't use it in the name of the
18613 psymtabs we create. E.g. expand_line_sal requires this when
18614 finding psymtabs to expand. A good testcase for this is
18615 mb-inline.exp.
18616
18617 LOWPC is the lowest address in CU (or 0 if not known).
18618
18619 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18620 for its PC<->lines mapping information. Otherwise only the filename
18621 table is read in. */
18622
18623 static void
18624 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
18625 CORE_ADDR lowpc, int decode_mapping)
18626 {
18627 if (decode_mapping)
18628 dwarf_decode_lines_1 (lh, cu, lowpc);
18629
18630 /* Make sure a symtab is created for every file, even files
18631 which contain only variables (i.e. no code with associated
18632 line numbers). */
18633 buildsym_compunit *builder = cu->get_builder ();
18634 struct compunit_symtab *cust = builder->get_compunit_symtab ();
18635
18636 for (auto &fe : lh->file_names ())
18637 {
18638 dwarf2_start_subfile (cu, fe, *lh);
18639 subfile *sf = builder->get_current_subfile ();
18640
18641 if (sf->symtab == nullptr)
18642 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
18643 sf->name_for_id.c_str ());
18644
18645 fe.symtab = sf->symtab;
18646 }
18647 }
18648
18649 /* Start a subfile for DWARF. FILENAME is the name of the file and
18650 DIRNAME the name of the source directory which contains FILENAME
18651 or NULL if not known.
18652 This routine tries to keep line numbers from identical absolute and
18653 relative file names in a common subfile.
18654
18655 Using the `list' example from the GDB testsuite, which resides in
18656 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18657 of /srcdir/list0.c yields the following debugging information for list0.c:
18658
18659 DW_AT_name: /srcdir/list0.c
18660 DW_AT_comp_dir: /compdir
18661 files.files[0].name: list0.h
18662 files.files[0].dir: /srcdir
18663 files.files[1].name: list0.c
18664 files.files[1].dir: /srcdir
18665
18666 The line number information for list0.c has to end up in a single
18667 subfile, so that `break /srcdir/list0.c:1' works as expected.
18668 start_subfile will ensure that this happens provided that we pass the
18669 concatenation of files.files[1].dir and files.files[1].name as the
18670 subfile's name. */
18671
18672 static void
18673 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
18674 const line_header &lh)
18675 {
18676 std::string filename_holder;
18677 const char *filename = fe.name;
18678 const char *dirname = lh.include_dir_at (fe.d_index);
18679
18680 /* In order not to lose the line information directory,
18681 we concatenate it to the filename when it makes sense.
18682 Note that the Dwarf3 standard says (speaking of filenames in line
18683 information): ``The directory index is ignored for file names
18684 that represent full path names''. Thus ignoring dirname in the
18685 `else' branch below isn't an issue. */
18686
18687 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18688 {
18689 filename_holder = path_join (dirname, filename);
18690 filename = filename_holder.c_str ();
18691 }
18692
18693 std::string filename_for_id = lh.file_file_name (fe);
18694 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
18695 }
18696
18697 static void
18698 var_decode_location (struct attribute *attr, struct symbol *sym,
18699 struct dwarf2_cu *cu)
18700 {
18701 struct objfile *objfile = cu->per_objfile->objfile;
18702 struct comp_unit_head *cu_header = &cu->header;
18703
18704 /* NOTE drow/2003-01-30: There used to be a comment and some special
18705 code here to turn a symbol with DW_AT_external and a
18706 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18707 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18708 with some versions of binutils) where shared libraries could have
18709 relocations against symbols in their debug information - the
18710 minimal symbol would have the right address, but the debug info
18711 would not. It's no longer necessary, because we will explicitly
18712 apply relocations when we read in the debug information now. */
18713
18714 /* A DW_AT_location attribute with no contents indicates that a
18715 variable has been optimized away. */
18716 if (attr->form_is_block () && attr->as_block ()->size == 0)
18717 {
18718 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18719 return;
18720 }
18721
18722 /* Handle one degenerate form of location expression specially, to
18723 preserve GDB's previous behavior when section offsets are
18724 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
18725 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
18726
18727 if (attr->form_is_block ())
18728 {
18729 struct dwarf_block *block = attr->as_block ();
18730
18731 if ((block->data[0] == DW_OP_addr
18732 && block->size == 1 + cu_header->addr_size)
18733 || ((block->data[0] == DW_OP_GNU_addr_index
18734 || block->data[0] == DW_OP_addrx)
18735 && (block->size
18736 == 1 + leb128_size (&block->data[1]))))
18737 {
18738 unsigned int dummy;
18739
18740 if (block->data[0] == DW_OP_addr)
18741 sym->set_value_address
18742 (cu->header.read_address (objfile->obfd.get (), block->data + 1,
18743 &dummy));
18744 else
18745 sym->set_value_address
18746 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
18747 sym->set_aclass_index (LOC_STATIC);
18748 fixup_symbol_section (sym, objfile);
18749 sym->set_value_address
18750 (sym->value_address ()
18751 + objfile->section_offsets[sym->section_index ()]);
18752 return;
18753 }
18754 }
18755
18756 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18757 expression evaluator, and use LOC_COMPUTED only when necessary
18758 (i.e. when the value of a register or memory location is
18759 referenced, or a thread-local block, etc.). Then again, it might
18760 not be worthwhile. I'm assuming that it isn't unless performance
18761 or memory numbers show me otherwise. */
18762
18763 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18764
18765 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18766 cu->has_loclist = true;
18767 }
18768
18769 /* Given a pointer to a DWARF information entry, figure out if we need
18770 to make a symbol table entry for it, and if so, create a new entry
18771 and return a pointer to it.
18772 If TYPE is NULL, determine symbol type from the die, otherwise
18773 used the passed type.
18774 If SPACE is not NULL, use it to hold the new symbol. If it is
18775 NULL, allocate a new symbol on the objfile's obstack. */
18776
18777 static struct symbol *
18778 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18779 struct symbol *space)
18780 {
18781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18782 struct objfile *objfile = per_objfile->objfile;
18783 struct gdbarch *gdbarch = objfile->arch ();
18784 struct symbol *sym = NULL;
18785 const char *name;
18786 struct attribute *attr = NULL;
18787 struct attribute *attr2 = NULL;
18788 CORE_ADDR baseaddr;
18789 struct pending **list_to_add = NULL;
18790
18791 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18792
18793 baseaddr = objfile->text_section_offset ();
18794
18795 name = dwarf2_name (die, cu);
18796 if (name == nullptr && (die->tag == DW_TAG_subprogram
18797 || die->tag == DW_TAG_inlined_subroutine
18798 || die->tag == DW_TAG_entry_point))
18799 name = dw2_linkage_name (die, cu);
18800
18801 if (name)
18802 {
18803 int suppress_add = 0;
18804
18805 if (space)
18806 sym = space;
18807 else
18808 sym = new (&objfile->objfile_obstack) symbol;
18809 OBJSTAT (objfile, n_syms++);
18810
18811 /* Cache this symbol's name and the name's demangled form (if any). */
18812 sym->set_language (cu->lang (), &objfile->objfile_obstack);
18813 /* Fortran does not have mangling standard and the mangling does differ
18814 between gfortran, iFort etc. */
18815 const char *physname
18816 = (cu->lang () == language_fortran
18817 ? dwarf2_full_name (name, die, cu)
18818 : dwarf2_physname (name, die, cu));
18819 const char *linkagename = dw2_linkage_name (die, cu);
18820
18821 if (linkagename == nullptr || cu->lang () == language_ada)
18822 sym->set_linkage_name (physname);
18823 else
18824 {
18825 sym->set_demangled_name (physname, &objfile->objfile_obstack);
18826 sym->set_linkage_name (linkagename);
18827 }
18828
18829 /* Handle DW_AT_artificial. */
18830 attr = dwarf2_attr (die, DW_AT_artificial, cu);
18831 if (attr != nullptr)
18832 sym->set_is_artificial (attr->as_boolean ());
18833
18834 /* Default assumptions.
18835 Use the passed type or decode it from the die. */
18836 sym->set_domain (VAR_DOMAIN);
18837 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18838 if (type != NULL)
18839 sym->set_type (type);
18840 else
18841 sym->set_type (die_type (die, cu));
18842 attr = dwarf2_attr (die,
18843 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18844 cu);
18845 if (attr != nullptr)
18846 sym->set_line (attr->constant_value (0));
18847
18848 attr = dwarf2_attr (die,
18849 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18850 cu);
18851 if (attr != nullptr && attr->is_nonnegative ())
18852 {
18853 file_name_index file_index
18854 = (file_name_index) attr->as_nonnegative ();
18855 struct file_entry *fe;
18856
18857 if (cu->line_header != NULL)
18858 fe = cu->line_header->file_name_at (file_index);
18859 else
18860 fe = NULL;
18861
18862 if (fe == NULL)
18863 complaint (_("file index out of range"));
18864 else
18865 sym->set_symtab (fe->symtab);
18866 }
18867
18868 switch (die->tag)
18869 {
18870 case DW_TAG_label:
18871 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18872 if (attr != nullptr)
18873 {
18874 CORE_ADDR addr;
18875
18876 addr = attr->as_address ();
18877 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18878 sym->set_section_index (SECT_OFF_TEXT (objfile));
18879 sym->set_value_address (addr);
18880 sym->set_aclass_index (LOC_LABEL);
18881 }
18882 else
18883 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18884 sym->set_type (objfile_type (objfile)->builtin_core_addr);
18885 sym->set_domain (LABEL_DOMAIN);
18886 add_symbol_to_list (sym, cu->list_in_scope);
18887 break;
18888 case DW_TAG_subprogram:
18889 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18890 finish_block. */
18891 sym->set_aclass_index (LOC_BLOCK);
18892 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18893 if ((attr2 != nullptr && attr2->as_boolean ())
18894 || cu->lang () == language_ada
18895 || cu->lang () == language_fortran)
18896 {
18897 /* Subprograms marked external are stored as a global symbol.
18898 Ada and Fortran subprograms, whether marked external or
18899 not, are always stored as a global symbol, because we want
18900 to be able to access them globally. For instance, we want
18901 to be able to break on a nested subprogram without having
18902 to specify the context. */
18903 list_to_add = cu->get_builder ()->get_global_symbols ();
18904 }
18905 else
18906 {
18907 list_to_add = cu->list_in_scope;
18908 }
18909 break;
18910 case DW_TAG_inlined_subroutine:
18911 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18912 finish_block. */
18913 sym->set_aclass_index (LOC_BLOCK);
18914 sym->set_is_inlined (1);
18915 list_to_add = cu->list_in_scope;
18916 break;
18917 case DW_TAG_template_value_param:
18918 suppress_add = 1;
18919 /* Fall through. */
18920 case DW_TAG_constant:
18921 case DW_TAG_variable:
18922 case DW_TAG_member:
18923 /* Compilation with minimal debug info may result in
18924 variables with missing type entries. Change the
18925 misleading `void' type to something sensible. */
18926 if (sym->type ()->code () == TYPE_CODE_VOID)
18927 sym->set_type (objfile_type (objfile)->builtin_int);
18928
18929 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18930 /* In the case of DW_TAG_member, we should only be called for
18931 static const members. */
18932 if (die->tag == DW_TAG_member)
18933 {
18934 /* dwarf2_add_field uses die_is_declaration,
18935 so we do the same. */
18936 gdb_assert (die_is_declaration (die, cu));
18937 gdb_assert (attr);
18938 }
18939 if (attr != nullptr)
18940 {
18941 dwarf2_const_value (attr, sym, cu);
18942 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18943 if (!suppress_add)
18944 {
18945 if (attr2 != nullptr && attr2->as_boolean ())
18946 list_to_add = cu->get_builder ()->get_global_symbols ();
18947 else
18948 list_to_add = cu->list_in_scope;
18949 }
18950 break;
18951 }
18952 attr = dwarf2_attr (die, DW_AT_location, cu);
18953 if (attr != nullptr)
18954 {
18955 var_decode_location (attr, sym, cu);
18956 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18957
18958 /* Fortran explicitly imports any global symbols to the local
18959 scope by DW_TAG_common_block. */
18960 if (cu->lang () == language_fortran && die->parent
18961 && die->parent->tag == DW_TAG_common_block)
18962 attr2 = NULL;
18963
18964 if (sym->aclass () == LOC_STATIC
18965 && sym->value_address () == 0
18966 && !per_objfile->per_bfd->has_section_at_zero)
18967 {
18968 /* When a static variable is eliminated by the linker,
18969 the corresponding debug information is not stripped
18970 out, but the variable address is set to null;
18971 do not add such variables into symbol table. */
18972 }
18973 else if (attr2 != nullptr && attr2->as_boolean ())
18974 {
18975 if (sym->aclass () == LOC_STATIC
18976 && (objfile->flags & OBJF_MAINLINE) == 0
18977 && per_objfile->per_bfd->can_copy)
18978 {
18979 /* A global static variable might be subject to
18980 copy relocation. We first check for a local
18981 minsym, though, because maybe the symbol was
18982 marked hidden, in which case this would not
18983 apply. */
18984 bound_minimal_symbol found
18985 = (lookup_minimal_symbol_linkage
18986 (sym->linkage_name (), objfile));
18987 if (found.minsym != nullptr)
18988 sym->maybe_copied = 1;
18989 }
18990
18991 /* A variable with DW_AT_external is never static,
18992 but it may be block-scoped. */
18993 list_to_add
18994 = ((cu->list_in_scope
18995 == cu->get_builder ()->get_file_symbols ())
18996 ? cu->get_builder ()->get_global_symbols ()
18997 : cu->list_in_scope);
18998 }
18999 else
19000 list_to_add = cu->list_in_scope;
19001 }
19002 else
19003 {
19004 /* We do not know the address of this symbol.
19005 If it is an external symbol and we have type information
19006 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19007 The address of the variable will then be determined from
19008 the minimal symbol table whenever the variable is
19009 referenced. */
19010 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19011
19012 /* Fortran explicitly imports any global symbols to the local
19013 scope by DW_TAG_common_block. */
19014 if (cu->lang () == language_fortran && die->parent
19015 && die->parent->tag == DW_TAG_common_block)
19016 {
19017 /* SYMBOL_CLASS doesn't matter here because
19018 read_common_block is going to reset it. */
19019 if (!suppress_add)
19020 list_to_add = cu->list_in_scope;
19021 }
19022 else if (attr2 != nullptr && attr2->as_boolean ()
19023 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19024 {
19025 /* A variable with DW_AT_external is never static, but it
19026 may be block-scoped. */
19027 list_to_add
19028 = ((cu->list_in_scope
19029 == cu->get_builder ()->get_file_symbols ())
19030 ? cu->get_builder ()->get_global_symbols ()
19031 : cu->list_in_scope);
19032
19033 sym->set_aclass_index (LOC_UNRESOLVED);
19034 }
19035 else if (!die_is_declaration (die, cu))
19036 {
19037 /* Use the default LOC_OPTIMIZED_OUT class. */
19038 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
19039 if (!suppress_add)
19040 list_to_add = cu->list_in_scope;
19041 }
19042 }
19043 break;
19044 case DW_TAG_formal_parameter:
19045 {
19046 /* If we are inside a function, mark this as an argument. If
19047 not, we might be looking at an argument to an inlined function
19048 when we do not have enough information to show inlined frames;
19049 pretend it's a local variable in that case so that the user can
19050 still see it. */
19051 struct context_stack *curr
19052 = cu->get_builder ()->get_current_context_stack ();
19053 if (curr != nullptr && curr->name != nullptr)
19054 sym->set_is_argument (1);
19055 attr = dwarf2_attr (die, DW_AT_location, cu);
19056 if (attr != nullptr)
19057 {
19058 var_decode_location (attr, sym, cu);
19059 }
19060 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19061 if (attr != nullptr)
19062 {
19063 dwarf2_const_value (attr, sym, cu);
19064 }
19065
19066 list_to_add = cu->list_in_scope;
19067 }
19068 break;
19069 case DW_TAG_unspecified_parameters:
19070 /* From varargs functions; gdb doesn't seem to have any
19071 interest in this information, so just ignore it for now.
19072 (FIXME?) */
19073 break;
19074 case DW_TAG_template_type_param:
19075 suppress_add = 1;
19076 /* Fall through. */
19077 case DW_TAG_class_type:
19078 case DW_TAG_interface_type:
19079 case DW_TAG_structure_type:
19080 case DW_TAG_union_type:
19081 case DW_TAG_set_type:
19082 case DW_TAG_enumeration_type:
19083 case DW_TAG_namelist:
19084 if (die->tag == DW_TAG_namelist)
19085 {
19086 sym->set_aclass_index (LOC_STATIC);
19087 sym->set_domain (VAR_DOMAIN);
19088 }
19089 else
19090 {
19091 sym->set_aclass_index (LOC_TYPEDEF);
19092 sym->set_domain (STRUCT_DOMAIN);
19093 }
19094 {
19095 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19096 really ever be static objects: otherwise, if you try
19097 to, say, break of a class's method and you're in a file
19098 which doesn't mention that class, it won't work unless
19099 the check for all static symbols in lookup_symbol_aux
19100 saves you. See the OtherFileClass tests in
19101 gdb.c++/namespace.exp. */
19102
19103 if (!suppress_add)
19104 {
19105 buildsym_compunit *builder = cu->get_builder ();
19106 list_to_add
19107 = (cu->list_in_scope == builder->get_file_symbols ()
19108 && cu->lang () == language_cplus
19109 ? builder->get_global_symbols ()
19110 : cu->list_in_scope);
19111
19112 /* The semantics of C++ state that "struct foo {
19113 ... }" also defines a typedef for "foo". */
19114 if (cu->lang () == language_cplus
19115 || cu->lang () == language_ada
19116 || cu->lang () == language_d
19117 || cu->lang () == language_rust)
19118 {
19119 /* The symbol's name is already allocated along
19120 with this objfile, so we don't need to
19121 duplicate it for the type. */
19122 if (sym->type ()->name () == 0)
19123 sym->type ()->set_name (sym->search_name ());
19124 }
19125 }
19126 }
19127 break;
19128 case DW_TAG_typedef:
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_array_type:
19134 case DW_TAG_base_type:
19135 case DW_TAG_subrange_type:
19136 case DW_TAG_generic_subrange:
19137 case DW_TAG_unspecified_type:
19138 sym->set_aclass_index (LOC_TYPEDEF);
19139 sym->set_domain (VAR_DOMAIN);
19140 list_to_add = cu->list_in_scope;
19141 break;
19142 case DW_TAG_enumerator:
19143 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19144 if (attr != nullptr)
19145 {
19146 dwarf2_const_value (attr, sym, cu);
19147 }
19148 {
19149 /* NOTE: carlton/2003-11-10: See comment above in the
19150 DW_TAG_class_type, etc. block. */
19151
19152 list_to_add
19153 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
19154 && cu->lang () == language_cplus
19155 ? cu->get_builder ()->get_global_symbols ()
19156 : cu->list_in_scope);
19157 }
19158 break;
19159 case DW_TAG_imported_declaration:
19160 case DW_TAG_namespace:
19161 sym->set_aclass_index (LOC_TYPEDEF);
19162 list_to_add = cu->get_builder ()->get_global_symbols ();
19163 break;
19164 case DW_TAG_module:
19165 sym->set_aclass_index (LOC_TYPEDEF);
19166 sym->set_domain (MODULE_DOMAIN);
19167 list_to_add = cu->get_builder ()->get_global_symbols ();
19168 break;
19169 case DW_TAG_common_block:
19170 sym->set_aclass_index (LOC_COMMON_BLOCK);
19171 sym->set_domain (COMMON_BLOCK_DOMAIN);
19172 add_symbol_to_list (sym, cu->list_in_scope);
19173 break;
19174 default:
19175 /* Not a tag we recognize. Hopefully we aren't processing
19176 trash data, but since we must specifically ignore things
19177 we don't recognize, there is nothing else we should do at
19178 this point. */
19179 complaint (_("unsupported tag: '%s'"),
19180 dwarf_tag_name (die->tag));
19181 break;
19182 }
19183
19184 if (suppress_add)
19185 {
19186 sym->hash_next = objfile->template_symbols;
19187 objfile->template_symbols = sym;
19188 list_to_add = NULL;
19189 }
19190
19191 if (list_to_add != NULL)
19192 add_symbol_to_list (sym, list_to_add);
19193
19194 /* For the benefit of old versions of GCC, check for anonymous
19195 namespaces based on the demangled name. */
19196 if (!cu->processing_has_namespace_info
19197 && cu->lang () == language_cplus)
19198 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
19199 }
19200 return (sym);
19201 }
19202
19203 /* Given an attr with a DW_FORM_dataN value in host byte order,
19204 zero-extend it as appropriate for the symbol's type. The DWARF
19205 standard (v4) is not entirely clear about the meaning of using
19206 DW_FORM_dataN for a constant with a signed type, where the type is
19207 wider than the data. The conclusion of a discussion on the DWARF
19208 list was that this is unspecified. We choose to always zero-extend
19209 because that is the interpretation long in use by GCC. */
19210
19211 static gdb_byte *
19212 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19213 struct dwarf2_cu *cu, LONGEST *value, int bits)
19214 {
19215 struct objfile *objfile = cu->per_objfile->objfile;
19216 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
19217 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19218 LONGEST l = attr->constant_value (0);
19219
19220 if (bits < sizeof (*value) * 8)
19221 {
19222 l &= ((LONGEST) 1 << bits) - 1;
19223 *value = l;
19224 }
19225 else if (bits == sizeof (*value) * 8)
19226 *value = l;
19227 else
19228 {
19229 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19230 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19231 return bytes;
19232 }
19233
19234 return NULL;
19235 }
19236
19237 /* Read a constant value from an attribute. Either set *VALUE, or if
19238 the value does not fit in *VALUE, set *BYTES - either already
19239 allocated on the objfile obstack, or newly allocated on OBSTACK,
19240 or, set *BATON, if we translated the constant to a location
19241 expression. */
19242
19243 static void
19244 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19245 const char *name, struct obstack *obstack,
19246 struct dwarf2_cu *cu,
19247 LONGEST *value, const gdb_byte **bytes,
19248 struct dwarf2_locexpr_baton **baton)
19249 {
19250 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19251 struct objfile *objfile = per_objfile->objfile;
19252 struct comp_unit_head *cu_header = &cu->header;
19253 struct dwarf_block *blk;
19254 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
19255 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19256
19257 *value = 0;
19258 *bytes = NULL;
19259 *baton = NULL;
19260
19261 switch (attr->form)
19262 {
19263 case DW_FORM_addr:
19264 case DW_FORM_addrx:
19265 case DW_FORM_GNU_addr_index:
19266 {
19267 gdb_byte *data;
19268
19269 if (type->length () != cu_header->addr_size)
19270 dwarf2_const_value_length_mismatch_complaint (name,
19271 cu_header->addr_size,
19272 type->length ());
19273 /* Symbols of this form are reasonably rare, so we just
19274 piggyback on the existing location code rather than writing
19275 a new implementation of symbol_computed_ops. */
19276 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19277 (*baton)->per_objfile = per_objfile;
19278 (*baton)->per_cu = cu->per_cu;
19279 gdb_assert ((*baton)->per_cu);
19280
19281 (*baton)->size = 2 + cu_header->addr_size;
19282 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19283 (*baton)->data = data;
19284
19285 data[0] = DW_OP_addr;
19286 store_unsigned_integer (&data[1], cu_header->addr_size,
19287 byte_order, attr->as_address ());
19288 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19289 }
19290 break;
19291 case DW_FORM_string:
19292 case DW_FORM_strp:
19293 case DW_FORM_strx:
19294 case DW_FORM_GNU_str_index:
19295 case DW_FORM_GNU_strp_alt:
19296 /* The string is already allocated on the objfile obstack, point
19297 directly to it. */
19298 *bytes = (const gdb_byte *) attr->as_string ();
19299 break;
19300 case DW_FORM_block1:
19301 case DW_FORM_block2:
19302 case DW_FORM_block4:
19303 case DW_FORM_block:
19304 case DW_FORM_exprloc:
19305 case DW_FORM_data16:
19306 blk = attr->as_block ();
19307 if (type->length () != blk->size)
19308 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19309 type->length ());
19310 *bytes = blk->data;
19311 break;
19312
19313 /* The DW_AT_const_value attributes are supposed to carry the
19314 symbol's value "represented as it would be on the target
19315 architecture." By the time we get here, it's already been
19316 converted to host endianness, so we just need to sign- or
19317 zero-extend it as appropriate. */
19318 case DW_FORM_data1:
19319 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19320 break;
19321 case DW_FORM_data2:
19322 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19323 break;
19324 case DW_FORM_data4:
19325 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19326 break;
19327 case DW_FORM_data8:
19328 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19329 break;
19330
19331 case DW_FORM_sdata:
19332 case DW_FORM_implicit_const:
19333 *value = attr->as_signed ();
19334 break;
19335
19336 case DW_FORM_udata:
19337 *value = attr->as_unsigned ();
19338 break;
19339
19340 default:
19341 complaint (_("unsupported const value attribute form: '%s'"),
19342 dwarf_form_name (attr->form));
19343 *value = 0;
19344 break;
19345 }
19346 }
19347
19348
19349 /* Copy constant value from an attribute to a symbol. */
19350
19351 static void
19352 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19353 struct dwarf2_cu *cu)
19354 {
19355 struct objfile *objfile = cu->per_objfile->objfile;
19356 LONGEST value;
19357 const gdb_byte *bytes;
19358 struct dwarf2_locexpr_baton *baton;
19359
19360 dwarf2_const_value_attr (attr, sym->type (),
19361 sym->print_name (),
19362 &objfile->objfile_obstack, cu,
19363 &value, &bytes, &baton);
19364
19365 if (baton != NULL)
19366 {
19367 SYMBOL_LOCATION_BATON (sym) = baton;
19368 sym->set_aclass_index (dwarf2_locexpr_index);
19369 }
19370 else if (bytes != NULL)
19371 {
19372 sym->set_value_bytes (bytes);
19373 sym->set_aclass_index (LOC_CONST_BYTES);
19374 }
19375 else
19376 {
19377 sym->set_value_longest (value);
19378 sym->set_aclass_index (LOC_CONST);
19379 }
19380 }
19381
19382 /* Return the type of the die in question using its DW_AT_type attribute. */
19383
19384 static struct type *
19385 die_type (struct die_info *die, struct dwarf2_cu *cu)
19386 {
19387 struct attribute *type_attr;
19388
19389 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19390 if (!type_attr)
19391 {
19392 struct objfile *objfile = cu->per_objfile->objfile;
19393 /* A missing DW_AT_type represents a void type. */
19394 return objfile_type (objfile)->builtin_void;
19395 }
19396
19397 return lookup_die_type (die, type_attr, cu);
19398 }
19399
19400 /* True iff CU's producer generates GNAT Ada auxiliary information
19401 that allows to find parallel types through that information instead
19402 of having to do expensive parallel lookups by type name. */
19403
19404 static int
19405 need_gnat_info (struct dwarf2_cu *cu)
19406 {
19407 /* Assume that the Ada compiler was GNAT, which always produces
19408 the auxiliary information. */
19409 return (cu->lang () == language_ada);
19410 }
19411
19412 /* Return the auxiliary type of the die in question using its
19413 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19414 attribute is not present. */
19415
19416 static struct type *
19417 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19418 {
19419 struct attribute *type_attr;
19420
19421 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19422 if (!type_attr)
19423 return NULL;
19424
19425 return lookup_die_type (die, type_attr, cu);
19426 }
19427
19428 /* If DIE has a descriptive_type attribute, then set the TYPE's
19429 descriptive type accordingly. */
19430
19431 static void
19432 set_descriptive_type (struct type *type, struct die_info *die,
19433 struct dwarf2_cu *cu)
19434 {
19435 struct type *descriptive_type = die_descriptive_type (die, cu);
19436
19437 if (descriptive_type)
19438 {
19439 ALLOCATE_GNAT_AUX_TYPE (type);
19440 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19441 }
19442 }
19443
19444 /* Return the containing type of the die in question using its
19445 DW_AT_containing_type attribute. */
19446
19447 static struct type *
19448 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19449 {
19450 struct attribute *type_attr;
19451 struct objfile *objfile = cu->per_objfile->objfile;
19452
19453 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19454 if (!type_attr)
19455 error (_("Dwarf Error: Problem turning containing type into gdb type "
19456 "[in module %s]"), objfile_name (objfile));
19457
19458 return lookup_die_type (die, type_attr, cu);
19459 }
19460
19461 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19462
19463 static struct type *
19464 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19465 {
19466 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19467 struct objfile *objfile = per_objfile->objfile;
19468 char *saved;
19469
19470 std::string message
19471 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
19472 objfile_name (objfile),
19473 sect_offset_str (cu->header.sect_off),
19474 sect_offset_str (die->sect_off));
19475 saved = obstack_strdup (&objfile->objfile_obstack, message);
19476
19477 return type_allocator (objfile).new_type (TYPE_CODE_ERROR, 0, saved);
19478 }
19479
19480 /* Look up the type of DIE in CU using its type attribute ATTR.
19481 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19482 DW_AT_containing_type.
19483 If there is no type substitute an error marker. */
19484
19485 static struct type *
19486 lookup_die_type (struct die_info *die, const struct attribute *attr,
19487 struct dwarf2_cu *cu)
19488 {
19489 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19490 struct objfile *objfile = per_objfile->objfile;
19491 struct type *this_type;
19492
19493 gdb_assert (attr->name == DW_AT_type
19494 || attr->name == DW_AT_GNAT_descriptive_type
19495 || attr->name == DW_AT_containing_type);
19496
19497 /* First see if we have it cached. */
19498
19499 if (attr->form == DW_FORM_GNU_ref_alt)
19500 {
19501 struct dwarf2_per_cu_data *per_cu;
19502 sect_offset sect_off = attr->get_ref_die_offset ();
19503
19504 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
19505 per_objfile->per_bfd);
19506 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
19507 }
19508 else if (attr->form_is_ref ())
19509 {
19510 sect_offset sect_off = attr->get_ref_die_offset ();
19511
19512 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
19513 }
19514 else if (attr->form == DW_FORM_ref_sig8)
19515 {
19516 ULONGEST signature = attr->as_signature ();
19517
19518 return get_signatured_type (die, signature, cu);
19519 }
19520 else
19521 {
19522 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
19523 " at %s [in module %s]"),
19524 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
19525 objfile_name (objfile));
19526 return build_error_marker_type (cu, die);
19527 }
19528
19529 /* If not cached we need to read it in. */
19530
19531 if (this_type == NULL)
19532 {
19533 struct die_info *type_die = NULL;
19534 struct dwarf2_cu *type_cu = cu;
19535
19536 if (attr->form_is_ref ())
19537 type_die = follow_die_ref (die, attr, &type_cu);
19538 if (type_die == NULL)
19539 return build_error_marker_type (cu, die);
19540 /* If we find the type now, it's probably because the type came
19541 from an inter-CU reference and the type's CU got expanded before
19542 ours. */
19543 this_type = read_type_die (type_die, type_cu);
19544 }
19545
19546 /* If we still don't have a type use an error marker. */
19547
19548 if (this_type == NULL)
19549 return build_error_marker_type (cu, die);
19550
19551 return this_type;
19552 }
19553
19554 /* Return the type in DIE, CU.
19555 Returns NULL for invalid types.
19556
19557 This first does a lookup in die_type_hash,
19558 and only reads the die in if necessary.
19559
19560 NOTE: This can be called when reading in partial or full symbols. */
19561
19562 static struct type *
19563 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19564 {
19565 struct type *this_type;
19566
19567 this_type = get_die_type (die, cu);
19568 if (this_type)
19569 return this_type;
19570
19571 return read_type_die_1 (die, cu);
19572 }
19573
19574 /* Read the type in DIE, CU.
19575 Returns NULL for invalid types. */
19576
19577 static struct type *
19578 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19579 {
19580 struct type *this_type = NULL;
19581
19582 switch (die->tag)
19583 {
19584 case DW_TAG_class_type:
19585 case DW_TAG_interface_type:
19586 case DW_TAG_structure_type:
19587 case DW_TAG_union_type:
19588 this_type = read_structure_type (die, cu);
19589 break;
19590 case DW_TAG_enumeration_type:
19591 this_type = read_enumeration_type (die, cu);
19592 break;
19593 case DW_TAG_subprogram:
19594 case DW_TAG_subroutine_type:
19595 case DW_TAG_inlined_subroutine:
19596 this_type = read_subroutine_type (die, cu);
19597 break;
19598 case DW_TAG_array_type:
19599 this_type = read_array_type (die, cu);
19600 break;
19601 case DW_TAG_set_type:
19602 this_type = read_set_type (die, cu);
19603 break;
19604 case DW_TAG_pointer_type:
19605 this_type = read_tag_pointer_type (die, cu);
19606 break;
19607 case DW_TAG_ptr_to_member_type:
19608 this_type = read_tag_ptr_to_member_type (die, cu);
19609 break;
19610 case DW_TAG_reference_type:
19611 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19612 break;
19613 case DW_TAG_rvalue_reference_type:
19614 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19615 break;
19616 case DW_TAG_const_type:
19617 this_type = read_tag_const_type (die, cu);
19618 break;
19619 case DW_TAG_volatile_type:
19620 this_type = read_tag_volatile_type (die, cu);
19621 break;
19622 case DW_TAG_restrict_type:
19623 this_type = read_tag_restrict_type (die, cu);
19624 break;
19625 case DW_TAG_string_type:
19626 this_type = read_tag_string_type (die, cu);
19627 break;
19628 case DW_TAG_typedef:
19629 this_type = read_typedef (die, cu);
19630 break;
19631 case DW_TAG_generic_subrange:
19632 case DW_TAG_subrange_type:
19633 this_type = read_subrange_type (die, cu);
19634 break;
19635 case DW_TAG_base_type:
19636 this_type = read_base_type (die, cu);
19637 break;
19638 case DW_TAG_unspecified_type:
19639 this_type = read_unspecified_type (die, cu);
19640 break;
19641 case DW_TAG_namespace:
19642 this_type = read_namespace_type (die, cu);
19643 break;
19644 case DW_TAG_module:
19645 this_type = read_module_type (die, cu);
19646 break;
19647 case DW_TAG_atomic_type:
19648 this_type = read_tag_atomic_type (die, cu);
19649 break;
19650 default:
19651 complaint (_("unexpected tag in read_type_die: '%s'"),
19652 dwarf_tag_name (die->tag));
19653 break;
19654 }
19655
19656 return this_type;
19657 }
19658
19659 /* See if we can figure out if the class lives in a namespace. We do
19660 this by looking for a member function; its demangled name will
19661 contain namespace info, if there is any.
19662 Return the computed name or NULL.
19663 Space for the result is allocated on the objfile's obstack.
19664 This is the full-die version of guess_partial_die_structure_name.
19665 In this case we know DIE has no useful parent. */
19666
19667 static const char *
19668 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19669 {
19670 struct die_info *spec_die;
19671 struct dwarf2_cu *spec_cu;
19672 struct die_info *child;
19673 struct objfile *objfile = cu->per_objfile->objfile;
19674
19675 spec_cu = cu;
19676 spec_die = die_specification (die, &spec_cu);
19677 if (spec_die != NULL)
19678 {
19679 die = spec_die;
19680 cu = spec_cu;
19681 }
19682
19683 for (child = die->child;
19684 child != NULL;
19685 child = child->sibling)
19686 {
19687 if (child->tag == DW_TAG_subprogram)
19688 {
19689 const char *linkage_name = dw2_linkage_name (child, cu);
19690
19691 if (linkage_name != NULL)
19692 {
19693 gdb::unique_xmalloc_ptr<char> actual_name
19694 (cu->language_defn->class_name_from_physname (linkage_name));
19695 const char *name = NULL;
19696
19697 if (actual_name != NULL)
19698 {
19699 const char *die_name = dwarf2_name (die, cu);
19700
19701 if (die_name != NULL
19702 && strcmp (die_name, actual_name.get ()) != 0)
19703 {
19704 /* Strip off the class name from the full name.
19705 We want the prefix. */
19706 int die_name_len = strlen (die_name);
19707 int actual_name_len = strlen (actual_name.get ());
19708 const char *ptr = actual_name.get ();
19709
19710 /* Test for '::' as a sanity check. */
19711 if (actual_name_len > die_name_len + 2
19712 && ptr[actual_name_len - die_name_len - 1] == ':')
19713 name = obstack_strndup (
19714 &objfile->per_bfd->storage_obstack,
19715 ptr, actual_name_len - die_name_len - 2);
19716 }
19717 }
19718 return name;
19719 }
19720 }
19721 }
19722
19723 return NULL;
19724 }
19725
19726 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19727 prefix part in such case. See
19728 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19729
19730 static const char *
19731 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19732 {
19733 struct attribute *attr;
19734 const char *base;
19735
19736 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19737 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19738 return NULL;
19739
19740 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19741 return NULL;
19742
19743 attr = dw2_linkage_name_attr (die, cu);
19744 const char *attr_name = attr->as_string ();
19745 if (attr == NULL || attr_name == NULL)
19746 return NULL;
19747
19748 /* dwarf2_name had to be already called. */
19749 gdb_assert (attr->canonical_string_p ());
19750
19751 /* Strip the base name, keep any leading namespaces/classes. */
19752 base = strrchr (attr_name, ':');
19753 if (base == NULL || base == attr_name || base[-1] != ':')
19754 return "";
19755
19756 struct objfile *objfile = cu->per_objfile->objfile;
19757 return obstack_strndup (&objfile->per_bfd->storage_obstack,
19758 attr_name,
19759 &base[-1] - attr_name);
19760 }
19761
19762 /* Return the name of the namespace/class that DIE is defined within,
19763 or "" if we can't tell. The caller should not xfree the result.
19764
19765 For example, if we're within the method foo() in the following
19766 code:
19767
19768 namespace N {
19769 class C {
19770 void foo () {
19771 }
19772 };
19773 }
19774
19775 then determine_prefix on foo's die will return "N::C". */
19776
19777 static const char *
19778 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19779 {
19780 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19781 struct die_info *parent, *spec_die;
19782 struct dwarf2_cu *spec_cu;
19783 struct type *parent_type;
19784 const char *retval;
19785
19786 if (cu->lang () != language_cplus
19787 && cu->lang () != language_fortran
19788 && cu->lang () != language_d
19789 && cu->lang () != language_rust)
19790 return "";
19791
19792 retval = anonymous_struct_prefix (die, cu);
19793 if (retval)
19794 return retval;
19795
19796 /* We have to be careful in the presence of DW_AT_specification.
19797 For example, with GCC 3.4, given the code
19798
19799 namespace N {
19800 void foo() {
19801 // Definition of N::foo.
19802 }
19803 }
19804
19805 then we'll have a tree of DIEs like this:
19806
19807 1: DW_TAG_compile_unit
19808 2: DW_TAG_namespace // N
19809 3: DW_TAG_subprogram // declaration of N::foo
19810 4: DW_TAG_subprogram // definition of N::foo
19811 DW_AT_specification // refers to die #3
19812
19813 Thus, when processing die #4, we have to pretend that we're in
19814 the context of its DW_AT_specification, namely the contex of die
19815 #3. */
19816 spec_cu = cu;
19817 spec_die = die_specification (die, &spec_cu);
19818 if (spec_die == NULL)
19819 parent = die->parent;
19820 else
19821 {
19822 parent = spec_die->parent;
19823 cu = spec_cu;
19824 }
19825
19826 if (parent == NULL)
19827 return "";
19828 else if (parent->building_fullname)
19829 {
19830 const char *name;
19831 const char *parent_name;
19832
19833 /* It has been seen on RealView 2.2 built binaries,
19834 DW_TAG_template_type_param types actually _defined_ as
19835 children of the parent class:
19836
19837 enum E {};
19838 template class <class Enum> Class{};
19839 Class<enum E> class_e;
19840
19841 1: DW_TAG_class_type (Class)
19842 2: DW_TAG_enumeration_type (E)
19843 3: DW_TAG_enumerator (enum1:0)
19844 3: DW_TAG_enumerator (enum2:1)
19845 ...
19846 2: DW_TAG_template_type_param
19847 DW_AT_type DW_FORM_ref_udata (E)
19848
19849 Besides being broken debug info, it can put GDB into an
19850 infinite loop. Consider:
19851
19852 When we're building the full name for Class<E>, we'll start
19853 at Class, and go look over its template type parameters,
19854 finding E. We'll then try to build the full name of E, and
19855 reach here. We're now trying to build the full name of E,
19856 and look over the parent DIE for containing scope. In the
19857 broken case, if we followed the parent DIE of E, we'd again
19858 find Class, and once again go look at its template type
19859 arguments, etc., etc. Simply don't consider such parent die
19860 as source-level parent of this die (it can't be, the language
19861 doesn't allow it), and break the loop here. */
19862 name = dwarf2_name (die, cu);
19863 parent_name = dwarf2_name (parent, cu);
19864 complaint (_("template param type '%s' defined within parent '%s'"),
19865 name ? name : "<unknown>",
19866 parent_name ? parent_name : "<unknown>");
19867 return "";
19868 }
19869 else
19870 switch (parent->tag)
19871 {
19872 case DW_TAG_namespace:
19873 parent_type = read_type_die (parent, cu);
19874 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19875 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19876 Work around this problem here. */
19877 if (cu->lang () == language_cplus
19878 && strcmp (parent_type->name (), "::") == 0)
19879 return "";
19880 /* We give a name to even anonymous namespaces. */
19881 return parent_type->name ();
19882 case DW_TAG_class_type:
19883 case DW_TAG_interface_type:
19884 case DW_TAG_structure_type:
19885 case DW_TAG_union_type:
19886 case DW_TAG_module:
19887 parent_type = read_type_die (parent, cu);
19888 if (parent_type->name () != NULL)
19889 return parent_type->name ();
19890 else
19891 /* An anonymous structure is only allowed non-static data
19892 members; no typedefs, no member functions, et cetera.
19893 So it does not need a prefix. */
19894 return "";
19895 case DW_TAG_compile_unit:
19896 case DW_TAG_partial_unit:
19897 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19898 if (cu->lang () == language_cplus
19899 && !per_objfile->per_bfd->types.empty ()
19900 && die->child != NULL
19901 && (die->tag == DW_TAG_class_type
19902 || die->tag == DW_TAG_structure_type
19903 || die->tag == DW_TAG_union_type))
19904 {
19905 const char *name = guess_full_die_structure_name (die, cu);
19906 if (name != NULL)
19907 return name;
19908 }
19909 return "";
19910 case DW_TAG_subprogram:
19911 /* Nested subroutines in Fortran get a prefix with the name
19912 of the parent's subroutine. */
19913 if (cu->lang () == language_fortran)
19914 {
19915 if ((die->tag == DW_TAG_subprogram)
19916 && (dwarf2_name (parent, cu) != NULL))
19917 return dwarf2_name (parent, cu);
19918 }
19919 return "";
19920 case DW_TAG_enumeration_type:
19921 parent_type = read_type_die (parent, cu);
19922 if (parent_type->is_declared_class ())
19923 {
19924 if (parent_type->name () != NULL)
19925 return parent_type->name ();
19926 return "";
19927 }
19928 /* Fall through. */
19929 default:
19930 return determine_prefix (parent, cu);
19931 }
19932 }
19933
19934 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19935 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19936 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19937 an obconcat, otherwise allocate storage for the result. The CU argument is
19938 used to determine the language and hence, the appropriate separator. */
19939
19940 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19941
19942 static char *
19943 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19944 int physname, struct dwarf2_cu *cu)
19945 {
19946 const char *lead = "";
19947 const char *sep;
19948
19949 if (suffix == NULL || suffix[0] == '\0'
19950 || prefix == NULL || prefix[0] == '\0')
19951 sep = "";
19952 else if (cu->lang () == language_d)
19953 {
19954 /* For D, the 'main' function could be defined in any module, but it
19955 should never be prefixed. */
19956 if (strcmp (suffix, "D main") == 0)
19957 {
19958 prefix = "";
19959 sep = "";
19960 }
19961 else
19962 sep = ".";
19963 }
19964 else if (cu->lang () == language_fortran && physname)
19965 {
19966 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19967 DW_AT_MIPS_linkage_name is preferred and used instead. */
19968
19969 lead = "__";
19970 sep = "_MOD_";
19971 }
19972 else
19973 sep = "::";
19974
19975 if (prefix == NULL)
19976 prefix = "";
19977 if (suffix == NULL)
19978 suffix = "";
19979
19980 if (obs == NULL)
19981 {
19982 char *retval
19983 = ((char *)
19984 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19985
19986 strcpy (retval, lead);
19987 strcat (retval, prefix);
19988 strcat (retval, sep);
19989 strcat (retval, suffix);
19990 return retval;
19991 }
19992 else
19993 {
19994 /* We have an obstack. */
19995 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19996 }
19997 }
19998
19999 /* Return a generic name for a DW_TAG_template_type_param or
20000 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
20001 per parent, so each function/class/struct template will have their own set
20002 of template parameters named <unnnamed0>, <unnamed1>, ... where the
20003 enumeration starts at 0 and represents the position of the template tag in
20004 the list of unnamed template tags for this parent, counting both, type and
20005 value tags. */
20006
20007 static const char *
20008 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
20009 {
20010 if (die->parent == nullptr)
20011 return nullptr;
20012
20013 /* Count the parent types unnamed template type and value children until, we
20014 arrive at our entry. */
20015 size_t nth_unnamed = 0;
20016
20017 die_info *child = die->parent->child;
20018 while (child != die)
20019 {
20020 gdb_assert (child != nullptr);
20021 if (child->tag == DW_TAG_template_type_param
20022 || child->tag == DW_TAG_template_value_param)
20023 {
20024 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
20025 ++nth_unnamed;
20026 }
20027 child = child->sibling;
20028 }
20029
20030 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
20031 return cu->per_objfile->objfile->intern (name_str.c_str ());
20032 }
20033
20034 /* Get name of a die, return NULL if not found. */
20035
20036 static const char *
20037 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20038 struct objfile *objfile)
20039 {
20040 if (name == nullptr)
20041 return name;
20042
20043 if (cu->lang () == language_cplus)
20044 {
20045 gdb::unique_xmalloc_ptr<char> canon_name
20046 = cp_canonicalize_string (name);
20047
20048 if (canon_name != nullptr)
20049 name = objfile->intern (canon_name.get ());
20050 }
20051 else if (cu->lang () == language_c)
20052 {
20053 gdb::unique_xmalloc_ptr<char> canon_name
20054 = c_canonicalize_name (name);
20055
20056 if (canon_name != nullptr)
20057 name = objfile->intern (canon_name.get ());
20058 }
20059
20060 return name;
20061 }
20062
20063 /* Get name of a die, return NULL if not found.
20064 Anonymous namespaces are converted to their magic string. */
20065
20066 static const char *
20067 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20068 {
20069 struct attribute *attr;
20070 struct objfile *objfile = cu->per_objfile->objfile;
20071
20072 attr = dwarf2_attr (die, DW_AT_name, cu);
20073 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
20074 if (attr_name == nullptr
20075 && die->tag != DW_TAG_namespace
20076 && die->tag != DW_TAG_class_type
20077 && die->tag != DW_TAG_interface_type
20078 && die->tag != DW_TAG_structure_type
20079 && die->tag != DW_TAG_namelist
20080 && die->tag != DW_TAG_union_type
20081 && die->tag != DW_TAG_template_type_param
20082 && die->tag != DW_TAG_template_value_param)
20083 return NULL;
20084
20085 switch (die->tag)
20086 {
20087 /* A member's name should not be canonicalized. This is a bit
20088 of a hack, in that normally it should not be possible to run
20089 into this situation; however, the dw2-unusual-field-names.exp
20090 test creates custom DWARF that does. */
20091 case DW_TAG_member:
20092 case DW_TAG_compile_unit:
20093 case DW_TAG_partial_unit:
20094 /* Compilation units have a DW_AT_name that is a filename, not
20095 a source language identifier. */
20096 case DW_TAG_enumeration_type:
20097 case DW_TAG_enumerator:
20098 /* These tags always have simple identifiers already; no need
20099 to canonicalize them. */
20100 return attr_name;
20101
20102 case DW_TAG_namespace:
20103 if (attr_name != nullptr)
20104 return attr_name;
20105 return CP_ANONYMOUS_NAMESPACE_STR;
20106
20107 /* DWARF does not actually require template tags to have a name. */
20108 case DW_TAG_template_type_param:
20109 case DW_TAG_template_value_param:
20110 if (attr_name == nullptr)
20111 return unnamed_template_tag_name (die, cu);
20112 /* FALLTHROUGH. */
20113 case DW_TAG_class_type:
20114 case DW_TAG_interface_type:
20115 case DW_TAG_structure_type:
20116 case DW_TAG_union_type:
20117 case DW_TAG_namelist:
20118 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20119 structures or unions. These were of the form "._%d" in GCC 4.1,
20120 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20121 and GCC 4.4. We work around this problem by ignoring these. */
20122 if (attr_name != nullptr
20123 && (startswith (attr_name, "._")
20124 || startswith (attr_name, "<anonymous")))
20125 return NULL;
20126
20127 /* GCC might emit a nameless typedef that has a linkage name. See
20128 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20129 if (!attr || attr_name == NULL)
20130 {
20131 attr = dw2_linkage_name_attr (die, cu);
20132 attr_name = attr == nullptr ? nullptr : attr->as_string ();
20133 if (attr == NULL || attr_name == NULL)
20134 return NULL;
20135
20136 /* Avoid demangling attr_name the second time on a second
20137 call for the same DIE. */
20138 if (!attr->canonical_string_p ())
20139 {
20140 gdb::unique_xmalloc_ptr<char> demangled
20141 (gdb_demangle (attr_name, DMGL_TYPES));
20142 if (demangled == nullptr)
20143 return nullptr;
20144
20145 attr->set_string_canonical (objfile->intern (demangled.get ()));
20146 attr_name = attr->as_string ();
20147 }
20148
20149 /* Strip any leading namespaces/classes, keep only the
20150 base name. DW_AT_name for named DIEs does not
20151 contain the prefixes. */
20152 const char *base = strrchr (attr_name, ':');
20153 if (base && base > attr_name && base[-1] == ':')
20154 return &base[1];
20155 else
20156 return attr_name;
20157 }
20158 break;
20159
20160 default:
20161 break;
20162 }
20163
20164 if (!attr->canonical_string_p ())
20165 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
20166 objfile));
20167 return attr->as_string ();
20168 }
20169
20170 /* Return the die that this die in an extension of, or NULL if there
20171 is none. *EXT_CU is the CU containing DIE on input, and the CU
20172 containing the return value on output. */
20173
20174 static struct die_info *
20175 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20176 {
20177 struct attribute *attr;
20178
20179 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20180 if (attr == NULL)
20181 return NULL;
20182
20183 return follow_die_ref (die, attr, ext_cu);
20184 }
20185
20186 static void
20187 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20188 {
20189 void **slot;
20190
20191 slot = htab_find_slot_with_hash (cu->die_hash, die,
20192 to_underlying (die->sect_off),
20193 INSERT);
20194
20195 *slot = die;
20196 }
20197
20198 /* Follow reference or signature attribute ATTR of SRC_DIE.
20199 On entry *REF_CU is the CU of SRC_DIE.
20200 On exit *REF_CU is the CU of the result. */
20201
20202 static struct die_info *
20203 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20204 struct dwarf2_cu **ref_cu)
20205 {
20206 struct die_info *die;
20207
20208 if (attr->form_is_ref ())
20209 die = follow_die_ref (src_die, attr, ref_cu);
20210 else if (attr->form == DW_FORM_ref_sig8)
20211 die = follow_die_sig (src_die, attr, ref_cu);
20212 else
20213 {
20214 src_die->error_dump ();
20215 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20216 objfile_name ((*ref_cu)->per_objfile->objfile));
20217 }
20218
20219 return die;
20220 }
20221
20222 /* Follow reference OFFSET.
20223 On entry *REF_CU is the CU of the source die referencing OFFSET.
20224 On exit *REF_CU is the CU of the result.
20225 Returns NULL if OFFSET is invalid. */
20226
20227 static struct die_info *
20228 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20229 struct dwarf2_cu **ref_cu)
20230 {
20231 struct die_info temp_die;
20232 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20233 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20234
20235 gdb_assert (cu->per_cu != NULL);
20236
20237 target_cu = cu;
20238
20239 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
20240 "source CU contains target offset: %d",
20241 sect_offset_str (cu->per_cu->sect_off),
20242 sect_offset_str (sect_off),
20243 cu->header.offset_in_cu_p (sect_off));
20244
20245 if (cu->per_cu->is_debug_types)
20246 {
20247 /* .debug_types CUs cannot reference anything outside their CU.
20248 If they need to, they have to reference a signatured type via
20249 DW_FORM_ref_sig8. */
20250 if (!cu->header.offset_in_cu_p (sect_off))
20251 return NULL;
20252 }
20253 else if (offset_in_dwz != cu->per_cu->is_dwz
20254 || !cu->header.offset_in_cu_p (sect_off))
20255 {
20256 struct dwarf2_per_cu_data *per_cu;
20257
20258 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20259 per_objfile->per_bfd);
20260
20261 dwarf_read_debug_printf_v ("target CU offset: %s, "
20262 "target CU DIEs loaded: %d",
20263 sect_offset_str (per_cu->sect_off),
20264 per_objfile->get_cu (per_cu) != nullptr);
20265
20266 /* If necessary, add it to the queue and load its DIEs.
20267
20268 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20269 it doesn't mean they are currently loaded. Since we require them
20270 to be loaded, we must check for ourselves. */
20271 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
20272 || per_objfile->get_cu (per_cu) == nullptr)
20273 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
20274 false, cu->lang ());
20275
20276 target_cu = per_objfile->get_cu (per_cu);
20277 gdb_assert (target_cu != nullptr);
20278 }
20279 else if (cu->dies == NULL)
20280 {
20281 /* We're loading full DIEs during partial symbol reading. */
20282 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
20283 language_minimal);
20284 }
20285
20286 *ref_cu = target_cu;
20287 temp_die.sect_off = sect_off;
20288
20289 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20290 &temp_die,
20291 to_underlying (sect_off));
20292 }
20293
20294 /* Follow reference attribute ATTR of SRC_DIE.
20295 On entry *REF_CU is the CU of SRC_DIE.
20296 On exit *REF_CU is the CU of the result. */
20297
20298 static struct die_info *
20299 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20300 struct dwarf2_cu **ref_cu)
20301 {
20302 sect_offset sect_off = attr->get_ref_die_offset ();
20303 struct dwarf2_cu *cu = *ref_cu;
20304 struct die_info *die;
20305
20306 die = follow_die_offset (sect_off,
20307 (attr->form == DW_FORM_GNU_ref_alt
20308 || cu->per_cu->is_dwz),
20309 ref_cu);
20310 if (!die)
20311 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
20312 "at %s [in module %s]"),
20313 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
20314 objfile_name (cu->per_objfile->objfile));
20315
20316 return die;
20317 }
20318
20319 /* See read.h. */
20320
20321 struct dwarf2_locexpr_baton
20322 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20323 dwarf2_per_cu_data *per_cu,
20324 dwarf2_per_objfile *per_objfile,
20325 gdb::function_view<CORE_ADDR ()> get_frame_pc,
20326 bool resolve_abstract_p)
20327 {
20328 struct die_info *die;
20329 struct attribute *attr;
20330 struct dwarf2_locexpr_baton retval;
20331 struct objfile *objfile = per_objfile->objfile;
20332
20333 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20334 if (cu == nullptr)
20335 cu = load_cu (per_cu, per_objfile, false);
20336
20337 if (cu == nullptr)
20338 {
20339 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20340 Instead just throw an error, not much else we can do. */
20341 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20342 sect_offset_str (sect_off), objfile_name (objfile));
20343 }
20344
20345 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20346 if (!die)
20347 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20348 sect_offset_str (sect_off), objfile_name (objfile));
20349
20350 attr = dwarf2_attr (die, DW_AT_location, cu);
20351 if (!attr && resolve_abstract_p
20352 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
20353 != per_objfile->per_bfd->abstract_to_concrete.end ()))
20354 {
20355 CORE_ADDR pc = get_frame_pc ();
20356 CORE_ADDR baseaddr = objfile->text_section_offset ();
20357 struct gdbarch *gdbarch = objfile->arch ();
20358
20359 for (const auto &cand_off
20360 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
20361 {
20362 struct dwarf2_cu *cand_cu = cu;
20363 struct die_info *cand
20364 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
20365 if (!cand
20366 || !cand->parent
20367 || cand->parent->tag != DW_TAG_subprogram)
20368 continue;
20369
20370 CORE_ADDR pc_low, pc_high;
20371 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
20372 if (pc_low == ((CORE_ADDR) -1))
20373 continue;
20374 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
20375 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
20376 if (!(pc_low <= pc && pc < pc_high))
20377 continue;
20378
20379 die = cand;
20380 attr = dwarf2_attr (die, DW_AT_location, cu);
20381 break;
20382 }
20383 }
20384
20385 if (!attr)
20386 {
20387 /* DWARF: "If there is no such attribute, then there is no effect.".
20388 DATA is ignored if SIZE is 0. */
20389
20390 retval.data = NULL;
20391 retval.size = 0;
20392 }
20393 else if (attr->form_is_section_offset ())
20394 {
20395 struct dwarf2_loclist_baton loclist_baton;
20396 CORE_ADDR pc = get_frame_pc ();
20397 size_t size;
20398
20399 fill_in_loclist_baton (cu, &loclist_baton, attr);
20400
20401 retval.data = dwarf2_find_location_expression (&loclist_baton,
20402 &size, pc);
20403 retval.size = size;
20404 }
20405 else
20406 {
20407 if (!attr->form_is_block ())
20408 error (_("Dwarf Error: DIE at %s referenced in module %s "
20409 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20410 sect_offset_str (sect_off), objfile_name (objfile));
20411
20412 struct dwarf_block *block = attr->as_block ();
20413 retval.data = block->data;
20414 retval.size = block->size;
20415 }
20416 retval.per_objfile = per_objfile;
20417 retval.per_cu = cu->per_cu;
20418
20419 per_objfile->age_comp_units ();
20420
20421 return retval;
20422 }
20423
20424 /* See read.h. */
20425
20426 struct dwarf2_locexpr_baton
20427 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20428 dwarf2_per_cu_data *per_cu,
20429 dwarf2_per_objfile *per_objfile,
20430 gdb::function_view<CORE_ADDR ()> get_frame_pc)
20431 {
20432 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20433
20434 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
20435 get_frame_pc);
20436 }
20437
20438 /* Write a constant of a given type as target-ordered bytes into
20439 OBSTACK. */
20440
20441 static const gdb_byte *
20442 write_constant_as_bytes (struct obstack *obstack,
20443 enum bfd_endian byte_order,
20444 struct type *type,
20445 ULONGEST value,
20446 LONGEST *len)
20447 {
20448 gdb_byte *result;
20449
20450 *len = type->length ();
20451 result = (gdb_byte *) obstack_alloc (obstack, *len);
20452 store_unsigned_integer (result, *len, byte_order, value);
20453
20454 return result;
20455 }
20456
20457 /* See read.h. */
20458
20459 const gdb_byte *
20460 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20461 dwarf2_per_cu_data *per_cu,
20462 dwarf2_per_objfile *per_objfile,
20463 obstack *obstack,
20464 LONGEST *len)
20465 {
20466 struct die_info *die;
20467 struct attribute *attr;
20468 const gdb_byte *result = NULL;
20469 struct type *type;
20470 LONGEST value;
20471 enum bfd_endian byte_order;
20472 struct objfile *objfile = per_objfile->objfile;
20473
20474 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20475 if (cu == nullptr)
20476 cu = load_cu (per_cu, per_objfile, false);
20477
20478 if (cu == nullptr)
20479 {
20480 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20481 Instead just throw an error, not much else we can do. */
20482 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20483 sect_offset_str (sect_off), objfile_name (objfile));
20484 }
20485
20486 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20487 if (!die)
20488 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20489 sect_offset_str (sect_off), objfile_name (objfile));
20490
20491 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20492 if (attr == NULL)
20493 return NULL;
20494
20495 byte_order = (bfd_big_endian (objfile->obfd.get ())
20496 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20497
20498 switch (attr->form)
20499 {
20500 case DW_FORM_addr:
20501 case DW_FORM_addrx:
20502 case DW_FORM_GNU_addr_index:
20503 {
20504 gdb_byte *tem;
20505
20506 *len = cu->header.addr_size;
20507 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20508 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
20509 result = tem;
20510 }
20511 break;
20512 case DW_FORM_string:
20513 case DW_FORM_strp:
20514 case DW_FORM_strx:
20515 case DW_FORM_GNU_str_index:
20516 case DW_FORM_GNU_strp_alt:
20517 /* The string is already allocated on the objfile obstack, point
20518 directly to it. */
20519 {
20520 const char *attr_name = attr->as_string ();
20521 result = (const gdb_byte *) attr_name;
20522 *len = strlen (attr_name);
20523 }
20524 break;
20525 case DW_FORM_block1:
20526 case DW_FORM_block2:
20527 case DW_FORM_block4:
20528 case DW_FORM_block:
20529 case DW_FORM_exprloc:
20530 case DW_FORM_data16:
20531 {
20532 struct dwarf_block *block = attr->as_block ();
20533 result = block->data;
20534 *len = block->size;
20535 }
20536 break;
20537
20538 /* The DW_AT_const_value attributes are supposed to carry the
20539 symbol's value "represented as it would be on the target
20540 architecture." By the time we get here, it's already been
20541 converted to host endianness, so we just need to sign- or
20542 zero-extend it as appropriate. */
20543 case DW_FORM_data1:
20544 type = die_type (die, cu);
20545 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20546 if (result == NULL)
20547 result = write_constant_as_bytes (obstack, byte_order,
20548 type, value, len);
20549 break;
20550 case DW_FORM_data2:
20551 type = die_type (die, cu);
20552 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20553 if (result == NULL)
20554 result = write_constant_as_bytes (obstack, byte_order,
20555 type, value, len);
20556 break;
20557 case DW_FORM_data4:
20558 type = die_type (die, cu);
20559 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20560 if (result == NULL)
20561 result = write_constant_as_bytes (obstack, byte_order,
20562 type, value, len);
20563 break;
20564 case DW_FORM_data8:
20565 type = die_type (die, cu);
20566 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20567 if (result == NULL)
20568 result = write_constant_as_bytes (obstack, byte_order,
20569 type, value, len);
20570 break;
20571
20572 case DW_FORM_sdata:
20573 case DW_FORM_implicit_const:
20574 type = die_type (die, cu);
20575 result = write_constant_as_bytes (obstack, byte_order,
20576 type, attr->as_signed (), len);
20577 break;
20578
20579 case DW_FORM_udata:
20580 type = die_type (die, cu);
20581 result = write_constant_as_bytes (obstack, byte_order,
20582 type, attr->as_unsigned (), len);
20583 break;
20584
20585 default:
20586 complaint (_("unsupported const value attribute form: '%s'"),
20587 dwarf_form_name (attr->form));
20588 break;
20589 }
20590
20591 return result;
20592 }
20593
20594 /* See read.h. */
20595
20596 struct type *
20597 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20598 dwarf2_per_cu_data *per_cu,
20599 dwarf2_per_objfile *per_objfile,
20600 const char **var_name)
20601 {
20602 struct die_info *die;
20603
20604 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20605 if (cu == nullptr)
20606 cu = load_cu (per_cu, per_objfile, false);
20607
20608 if (cu == nullptr)
20609 return nullptr;
20610
20611 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20612 if (!die)
20613 return NULL;
20614
20615 if (var_name != nullptr)
20616 *var_name = var_decl_name (die, cu);
20617 return die_type (die, cu);
20618 }
20619
20620 /* See read.h. */
20621
20622 struct type *
20623 dwarf2_get_die_type (cu_offset die_offset,
20624 dwarf2_per_cu_data *per_cu,
20625 dwarf2_per_objfile *per_objfile)
20626 {
20627 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20628 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
20629 }
20630
20631 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20632 On entry *REF_CU is the CU of SRC_DIE.
20633 On exit *REF_CU is the CU of the result.
20634 Returns NULL if the referenced DIE isn't found. */
20635
20636 static struct die_info *
20637 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20638 struct dwarf2_cu **ref_cu)
20639 {
20640 struct die_info temp_die;
20641 struct dwarf2_cu *sig_cu;
20642 struct die_info *die;
20643 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
20644
20645
20646 /* While it might be nice to assert sig_type->type == NULL here,
20647 we can get here for DW_AT_imported_declaration where we need
20648 the DIE not the type. */
20649
20650 /* If necessary, add it to the queue and load its DIEs.
20651
20652 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20653 it doesn't mean they are currently loaded. Since we require them
20654 to be loaded, we must check for ourselves. */
20655 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
20656 language_minimal)
20657 || per_objfile->get_cu (sig_type) == nullptr)
20658 read_signatured_type (sig_type, per_objfile);
20659
20660 sig_cu = per_objfile->get_cu (sig_type);
20661 gdb_assert (sig_cu != NULL);
20662 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20663 temp_die.sect_off = sig_type->type_offset_in_section;
20664 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20665 to_underlying (temp_die.sect_off));
20666 if (die)
20667 {
20668 /* For .gdb_index version 7 keep track of included TUs.
20669 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20670 if (per_objfile->per_bfd->index_table != NULL
20671 && !per_objfile->per_bfd->index_table->version_check ())
20672 {
20673 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
20674 }
20675
20676 *ref_cu = sig_cu;
20677 return die;
20678 }
20679
20680 return NULL;
20681 }
20682
20683 /* Follow signatured type referenced by ATTR in SRC_DIE.
20684 On entry *REF_CU is the CU of SRC_DIE.
20685 On exit *REF_CU is the CU of the result.
20686 The result is the DIE of the type.
20687 If the referenced type cannot be found an error is thrown. */
20688
20689 static struct die_info *
20690 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20691 struct dwarf2_cu **ref_cu)
20692 {
20693 ULONGEST signature = attr->as_signature ();
20694 struct signatured_type *sig_type;
20695 struct die_info *die;
20696
20697 gdb_assert (attr->form == DW_FORM_ref_sig8);
20698
20699 sig_type = lookup_signatured_type (*ref_cu, signature);
20700 /* sig_type will be NULL if the signatured type is missing from
20701 the debug info. */
20702 if (sig_type == NULL)
20703 {
20704 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20705 " from DIE at %s [in module %s]"),
20706 hex_string (signature), sect_offset_str (src_die->sect_off),
20707 objfile_name ((*ref_cu)->per_objfile->objfile));
20708 }
20709
20710 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20711 if (die == NULL)
20712 {
20713 src_die->error_dump ();
20714 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20715 " from DIE at %s [in module %s]"),
20716 hex_string (signature), sect_offset_str (src_die->sect_off),
20717 objfile_name ((*ref_cu)->per_objfile->objfile));
20718 }
20719
20720 return die;
20721 }
20722
20723 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20724 reading in and processing the type unit if necessary. */
20725
20726 static struct type *
20727 get_signatured_type (struct die_info *die, ULONGEST signature,
20728 struct dwarf2_cu *cu)
20729 {
20730 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20731 struct signatured_type *sig_type;
20732 struct dwarf2_cu *type_cu;
20733 struct die_info *type_die;
20734 struct type *type;
20735
20736 sig_type = lookup_signatured_type (cu, signature);
20737 /* sig_type will be NULL if the signatured type is missing from
20738 the debug info. */
20739 if (sig_type == NULL)
20740 {
20741 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20742 " from DIE at %s [in module %s]"),
20743 hex_string (signature), sect_offset_str (die->sect_off),
20744 objfile_name (per_objfile->objfile));
20745 return build_error_marker_type (cu, die);
20746 }
20747
20748 /* If we already know the type we're done. */
20749 type = per_objfile->get_type_for_signatured_type (sig_type);
20750 if (type != nullptr)
20751 return type;
20752
20753 type_cu = cu;
20754 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20755 if (type_die != NULL)
20756 {
20757 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20758 is created. This is important, for example, because for c++ classes
20759 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20760 type = read_type_die (type_die, type_cu);
20761 if (type == NULL)
20762 {
20763 complaint (_("Dwarf Error: Cannot build signatured type %s"
20764 " referenced 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 else
20771 {
20772 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20773 " from DIE at %s [in module %s]"),
20774 hex_string (signature), sect_offset_str (die->sect_off),
20775 objfile_name (per_objfile->objfile));
20776 type = build_error_marker_type (cu, die);
20777 }
20778
20779 per_objfile->set_type_for_signatured_type (sig_type, type);
20780
20781 return type;
20782 }
20783
20784 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20785 reading in and processing the type unit if necessary. */
20786
20787 static struct type *
20788 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20789 struct dwarf2_cu *cu) /* ARI: editCase function */
20790 {
20791 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20792 if (attr->form_is_ref ())
20793 {
20794 struct dwarf2_cu *type_cu = cu;
20795 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20796
20797 return read_type_die (type_die, type_cu);
20798 }
20799 else if (attr->form == DW_FORM_ref_sig8)
20800 {
20801 return get_signatured_type (die, attr->as_signature (), cu);
20802 }
20803 else
20804 {
20805 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20806
20807 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20808 " at %s [in module %s]"),
20809 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
20810 objfile_name (per_objfile->objfile));
20811 return build_error_marker_type (cu, die);
20812 }
20813 }
20814
20815 /* Load the DIEs associated with type unit PER_CU into memory. */
20816
20817 static void
20818 load_full_type_unit (dwarf2_per_cu_data *per_cu,
20819 dwarf2_per_objfile *per_objfile)
20820 {
20821 struct signatured_type *sig_type;
20822
20823 /* We have the per_cu, but we need the signatured_type.
20824 Fortunately this is an easy translation. */
20825 gdb_assert (per_cu->is_debug_types);
20826 sig_type = (struct signatured_type *) per_cu;
20827
20828 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
20829
20830 read_signatured_type (sig_type, per_objfile);
20831
20832 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
20833 }
20834
20835 /* Read in a signatured type and build its CU and DIEs.
20836 If the type is a stub for the real type in a DWO file,
20837 read in the real type from the DWO file as well. */
20838
20839 static void
20840 read_signatured_type (signatured_type *sig_type,
20841 dwarf2_per_objfile *per_objfile)
20842 {
20843 gdb_assert (sig_type->is_debug_types);
20844 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
20845
20846 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
20847
20848 if (!reader.dummy_p)
20849 {
20850 struct dwarf2_cu *cu = reader.cu;
20851 const gdb_byte *info_ptr = reader.info_ptr;
20852
20853 gdb_assert (cu->die_hash == NULL);
20854 cu->die_hash =
20855 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
20856 die_info::hash,
20857 die_info::eq,
20858 NULL,
20859 &cu->comp_unit_obstack,
20860 hashtab_obstack_allocate,
20861 dummy_obstack_deallocate);
20862
20863 if (reader.comp_unit_die->has_children)
20864 reader.comp_unit_die->child
20865 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
20866 reader.comp_unit_die);
20867 cu->dies = reader.comp_unit_die;
20868 /* comp_unit_die is not stored in die_hash, no need. */
20869
20870 /* We try not to read any attributes in this function, because
20871 not all CUs needed for references have been loaded yet, and
20872 symbol table processing isn't initialized. But we have to
20873 set the CU language, or we won't be able to build types
20874 correctly. Similarly, if we do not read the producer, we can
20875 not apply producer-specific interpretation. */
20876 prepare_one_comp_unit (cu, cu->dies, language_minimal);
20877
20878 reader.keep ();
20879 }
20880
20881 sig_type->tu_read = 1;
20882 }
20883
20884 /* Decode simple location descriptions.
20885 Given a pointer to a dwarf block that defines a location, compute
20886 the location and return the value. If COMPUTED is non-null, it is
20887 set to true to indicate that decoding was successful, and false
20888 otherwise. If COMPUTED is null, then this function may emit a
20889 complaint. */
20890
20891 static CORE_ADDR
20892 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
20893 {
20894 struct objfile *objfile = cu->per_objfile->objfile;
20895 size_t i;
20896 size_t size = blk->size;
20897 const gdb_byte *data = blk->data;
20898 CORE_ADDR stack[64];
20899 int stacki;
20900 unsigned int bytes_read, unsnd;
20901 gdb_byte op;
20902
20903 if (computed != nullptr)
20904 *computed = false;
20905
20906 i = 0;
20907 stacki = 0;
20908 stack[stacki] = 0;
20909 stack[++stacki] = 0;
20910
20911 while (i < size)
20912 {
20913 op = data[i++];
20914 switch (op)
20915 {
20916 case DW_OP_lit0:
20917 case DW_OP_lit1:
20918 case DW_OP_lit2:
20919 case DW_OP_lit3:
20920 case DW_OP_lit4:
20921 case DW_OP_lit5:
20922 case DW_OP_lit6:
20923 case DW_OP_lit7:
20924 case DW_OP_lit8:
20925 case DW_OP_lit9:
20926 case DW_OP_lit10:
20927 case DW_OP_lit11:
20928 case DW_OP_lit12:
20929 case DW_OP_lit13:
20930 case DW_OP_lit14:
20931 case DW_OP_lit15:
20932 case DW_OP_lit16:
20933 case DW_OP_lit17:
20934 case DW_OP_lit18:
20935 case DW_OP_lit19:
20936 case DW_OP_lit20:
20937 case DW_OP_lit21:
20938 case DW_OP_lit22:
20939 case DW_OP_lit23:
20940 case DW_OP_lit24:
20941 case DW_OP_lit25:
20942 case DW_OP_lit26:
20943 case DW_OP_lit27:
20944 case DW_OP_lit28:
20945 case DW_OP_lit29:
20946 case DW_OP_lit30:
20947 case DW_OP_lit31:
20948 stack[++stacki] = op - DW_OP_lit0;
20949 break;
20950
20951 case DW_OP_reg0:
20952 case DW_OP_reg1:
20953 case DW_OP_reg2:
20954 case DW_OP_reg3:
20955 case DW_OP_reg4:
20956 case DW_OP_reg5:
20957 case DW_OP_reg6:
20958 case DW_OP_reg7:
20959 case DW_OP_reg8:
20960 case DW_OP_reg9:
20961 case DW_OP_reg10:
20962 case DW_OP_reg11:
20963 case DW_OP_reg12:
20964 case DW_OP_reg13:
20965 case DW_OP_reg14:
20966 case DW_OP_reg15:
20967 case DW_OP_reg16:
20968 case DW_OP_reg17:
20969 case DW_OP_reg18:
20970 case DW_OP_reg19:
20971 case DW_OP_reg20:
20972 case DW_OP_reg21:
20973 case DW_OP_reg22:
20974 case DW_OP_reg23:
20975 case DW_OP_reg24:
20976 case DW_OP_reg25:
20977 case DW_OP_reg26:
20978 case DW_OP_reg27:
20979 case DW_OP_reg28:
20980 case DW_OP_reg29:
20981 case DW_OP_reg30:
20982 case DW_OP_reg31:
20983 stack[++stacki] = op - DW_OP_reg0;
20984 if (i < size)
20985 {
20986 if (computed == nullptr)
20987 dwarf2_complex_location_expr_complaint ();
20988 else
20989 return 0;
20990 }
20991 break;
20992
20993 case DW_OP_regx:
20994 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
20995 i += bytes_read;
20996 stack[++stacki] = unsnd;
20997 if (i < size)
20998 {
20999 if (computed == nullptr)
21000 dwarf2_complex_location_expr_complaint ();
21001 else
21002 return 0;
21003 }
21004 break;
21005
21006 case DW_OP_addr:
21007 stack[++stacki] = cu->header.read_address (objfile->obfd.get (),
21008 &data[i],
21009 &bytes_read);
21010 i += bytes_read;
21011 break;
21012
21013 case DW_OP_const1u:
21014 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
21015 i += 1;
21016 break;
21017
21018 case DW_OP_const1s:
21019 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
21020 i += 1;
21021 break;
21022
21023 case DW_OP_const2u:
21024 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
21025 i += 2;
21026 break;
21027
21028 case DW_OP_const2s:
21029 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
21030 i += 2;
21031 break;
21032
21033 case DW_OP_const4u:
21034 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
21035 i += 4;
21036 break;
21037
21038 case DW_OP_const4s:
21039 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
21040 i += 4;
21041 break;
21042
21043 case DW_OP_const8u:
21044 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
21045 i += 8;
21046 break;
21047
21048 case DW_OP_constu:
21049 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21050 &bytes_read);
21051 i += bytes_read;
21052 break;
21053
21054 case DW_OP_consts:
21055 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21056 i += bytes_read;
21057 break;
21058
21059 case DW_OP_dup:
21060 stack[stacki + 1] = stack[stacki];
21061 stacki++;
21062 break;
21063
21064 case DW_OP_plus:
21065 stack[stacki - 1] += stack[stacki];
21066 stacki--;
21067 break;
21068
21069 case DW_OP_plus_uconst:
21070 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21071 &bytes_read);
21072 i += bytes_read;
21073 break;
21074
21075 case DW_OP_minus:
21076 stack[stacki - 1] -= stack[stacki];
21077 stacki--;
21078 break;
21079
21080 case DW_OP_deref:
21081 /* If we're not the last op, then we definitely can't encode
21082 this using GDB's address_class enum. This is valid for partial
21083 global symbols, although the variable's address will be bogus
21084 in the psymtab. */
21085 if (i < size)
21086 {
21087 if (computed == nullptr)
21088 dwarf2_complex_location_expr_complaint ();
21089 else
21090 return 0;
21091 }
21092 break;
21093
21094 case DW_OP_GNU_push_tls_address:
21095 case DW_OP_form_tls_address:
21096 /* The top of the stack has the offset from the beginning
21097 of the thread control block at which the variable is located. */
21098 /* Nothing should follow this operator, so the top of stack would
21099 be returned. */
21100 /* This is valid for partial global symbols, but the variable's
21101 address will be bogus in the psymtab. Make it always at least
21102 non-zero to not look as a variable garbage collected by linker
21103 which have DW_OP_addr 0. */
21104 if (i < size)
21105 {
21106 if (computed == nullptr)
21107 dwarf2_complex_location_expr_complaint ();
21108 else
21109 return 0;
21110 }
21111 stack[stacki]++;
21112 break;
21113
21114 case DW_OP_GNU_uninit:
21115 if (computed != nullptr)
21116 return 0;
21117 break;
21118
21119 case DW_OP_addrx:
21120 case DW_OP_GNU_addr_index:
21121 case DW_OP_GNU_const_index:
21122 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21123 &bytes_read);
21124 i += bytes_read;
21125 break;
21126
21127 default:
21128 if (computed == nullptr)
21129 {
21130 const char *name = get_DW_OP_name (op);
21131
21132 if (name)
21133 complaint (_("unsupported stack op: '%s'"),
21134 name);
21135 else
21136 complaint (_("unsupported stack op: '%02x'"),
21137 op);
21138 }
21139
21140 return (stack[stacki]);
21141 }
21142
21143 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21144 outside of the allocated space. Also enforce minimum>0. */
21145 if (stacki >= ARRAY_SIZE (stack) - 1)
21146 {
21147 if (computed == nullptr)
21148 complaint (_("location description stack overflow"));
21149 return 0;
21150 }
21151
21152 if (stacki <= 0)
21153 {
21154 if (computed == nullptr)
21155 complaint (_("location description stack underflow"));
21156 return 0;
21157 }
21158 }
21159
21160 if (computed != nullptr)
21161 *computed = true;
21162 return (stack[stacki]);
21163 }
21164
21165 /* memory allocation interface */
21166
21167 static struct dwarf_block *
21168 dwarf_alloc_block (struct dwarf2_cu *cu)
21169 {
21170 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21171 }
21172
21173 \f
21174
21175 /* Macro support. */
21176
21177 /* An overload of dwarf_decode_macros that finds the correct section
21178 and ensures it is read in before calling the other overload. */
21179
21180 static void
21181 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
21182 int section_is_gnu)
21183 {
21184 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21185 struct objfile *objfile = per_objfile->objfile;
21186 const struct line_header *lh = cu->line_header;
21187 unsigned int offset_size = cu->header.offset_size;
21188 struct dwarf2_section_info *section;
21189 const char *section_name;
21190
21191 if (cu->dwo_unit != nullptr)
21192 {
21193 if (section_is_gnu)
21194 {
21195 section = &cu->dwo_unit->dwo_file->sections.macro;
21196 section_name = ".debug_macro.dwo";
21197 }
21198 else
21199 {
21200 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21201 section_name = ".debug_macinfo.dwo";
21202 }
21203 }
21204 else
21205 {
21206 if (section_is_gnu)
21207 {
21208 section = &per_objfile->per_bfd->macro;
21209 section_name = ".debug_macro";
21210 }
21211 else
21212 {
21213 section = &per_objfile->per_bfd->macinfo;
21214 section_name = ".debug_macinfo";
21215 }
21216 }
21217
21218 section->read (objfile);
21219 if (section->buffer == nullptr)
21220 {
21221 complaint (_("missing %s section"), section_name);
21222 return;
21223 }
21224
21225 buildsym_compunit *builder = cu->get_builder ();
21226
21227 struct dwarf2_section_info *str_offsets_section;
21228 struct dwarf2_section_info *str_section;
21229 gdb::optional<ULONGEST> str_offsets_base;
21230
21231 if (cu->dwo_unit != nullptr)
21232 {
21233 str_offsets_section = &cu->dwo_unit->dwo_file
21234 ->sections.str_offsets;
21235 str_section = &cu->dwo_unit->dwo_file->sections.str;
21236 str_offsets_base = cu->header.addr_size;
21237 }
21238 else
21239 {
21240 str_offsets_section = &per_objfile->per_bfd->str_offsets;
21241 str_section = &per_objfile->per_bfd->str;
21242 str_offsets_base = cu->str_offsets_base;
21243 }
21244
21245 dwarf_decode_macros (per_objfile, builder, section, lh,
21246 offset_size, offset, str_section, str_offsets_section,
21247 str_offsets_base, section_is_gnu, cu);
21248 }
21249
21250 /* Return the .debug_loc section to use for CU.
21251 For DWO files use .debug_loc.dwo. */
21252
21253 static struct dwarf2_section_info *
21254 cu_debug_loc_section (struct dwarf2_cu *cu)
21255 {
21256 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21257
21258 if (cu->dwo_unit)
21259 {
21260 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21261
21262 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
21263 }
21264 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
21265 : &per_objfile->per_bfd->loc);
21266 }
21267
21268 /* Return the .debug_rnglists section to use for CU. */
21269 static struct dwarf2_section_info *
21270 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
21271 {
21272 if (cu->header.version < 5)
21273 error (_(".debug_rnglists section cannot be used in DWARF %d"),
21274 cu->header.version);
21275 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21276
21277 /* Make sure we read the .debug_rnglists section from the file that
21278 contains the DW_AT_ranges attribute we are reading. Normally that
21279 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
21280 or DW_TAG_skeleton unit, we always want to read from objfile/linked
21281 program. */
21282 if (cu->dwo_unit != nullptr
21283 && tag != DW_TAG_compile_unit
21284 && tag != DW_TAG_skeleton_unit)
21285 {
21286 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21287
21288 if (sections->rnglists.size > 0)
21289 return &sections->rnglists;
21290 else
21291 error (_(".debug_rnglists section is missing from .dwo file."));
21292 }
21293 return &dwarf2_per_objfile->per_bfd->rnglists;
21294 }
21295
21296 /* A helper function that fills in a dwarf2_loclist_baton. */
21297
21298 static void
21299 fill_in_loclist_baton (struct dwarf2_cu *cu,
21300 struct dwarf2_loclist_baton *baton,
21301 const struct attribute *attr)
21302 {
21303 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21304 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21305
21306 section->read (per_objfile->objfile);
21307
21308 baton->per_objfile = per_objfile;
21309 baton->per_cu = cu->per_cu;
21310 gdb_assert (baton->per_cu);
21311 /* We don't know how long the location list is, but make sure we
21312 don't run off the edge of the section. */
21313 baton->size = section->size - attr->as_unsigned ();
21314 baton->data = section->buffer + attr->as_unsigned ();
21315 if (cu->base_address.has_value ())
21316 baton->base_address = *cu->base_address;
21317 else
21318 baton->base_address = 0;
21319 baton->from_dwo = cu->dwo_unit != NULL;
21320 }
21321
21322 static void
21323 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
21324 struct dwarf2_cu *cu, int is_block)
21325 {
21326 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21327 struct objfile *objfile = per_objfile->objfile;
21328 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21329
21330 if (attr->form_is_section_offset ()
21331 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21332 the section. If so, fall through to the complaint in the
21333 other branch. */
21334 && attr->as_unsigned () < section->get_size (objfile))
21335 {
21336 struct dwarf2_loclist_baton *baton;
21337
21338 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
21339
21340 fill_in_loclist_baton (cu, baton, attr);
21341
21342 if (!cu->base_address.has_value ())
21343 complaint (_("Location list used without "
21344 "specifying the CU base address."));
21345
21346 sym->set_aclass_index ((is_block
21347 ? dwarf2_loclist_block_index
21348 : dwarf2_loclist_index));
21349 SYMBOL_LOCATION_BATON (sym) = baton;
21350 }
21351 else
21352 {
21353 struct dwarf2_locexpr_baton *baton;
21354
21355 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
21356 baton->per_objfile = per_objfile;
21357 baton->per_cu = cu->per_cu;
21358 gdb_assert (baton->per_cu);
21359
21360 if (attr->form_is_block ())
21361 {
21362 /* Note that we're just copying the block's data pointer
21363 here, not the actual data. We're still pointing into the
21364 info_buffer for SYM's objfile; right now we never release
21365 that buffer, but when we do clean up properly this may
21366 need to change. */
21367 struct dwarf_block *block = attr->as_block ();
21368 baton->size = block->size;
21369 baton->data = block->data;
21370 }
21371 else
21372 {
21373 dwarf2_invalid_attrib_class_complaint ("location description",
21374 sym->natural_name ());
21375 baton->size = 0;
21376 }
21377
21378 sym->set_aclass_index ((is_block
21379 ? dwarf2_locexpr_block_index
21380 : dwarf2_locexpr_index));
21381 SYMBOL_LOCATION_BATON (sym) = baton;
21382 }
21383 }
21384
21385 /* See read.h. */
21386
21387 const comp_unit_head *
21388 dwarf2_per_cu_data::get_header () const
21389 {
21390 if (!m_header_read_in)
21391 {
21392 const gdb_byte *info_ptr
21393 = this->section->buffer + to_underlying (this->sect_off);
21394
21395 read_comp_unit_head (&m_header, info_ptr, this->section,
21396 rcuh_kind::COMPILE);
21397
21398 m_header_read_in = true;
21399 }
21400
21401 return &m_header;
21402 }
21403
21404 /* See read.h. */
21405
21406 int
21407 dwarf2_per_cu_data::addr_size () const
21408 {
21409 return this->get_header ()->addr_size;
21410 }
21411
21412 /* See read.h. */
21413
21414 int
21415 dwarf2_per_cu_data::offset_size () const
21416 {
21417 return this->get_header ()->offset_size;
21418 }
21419
21420 /* See read.h. */
21421
21422 int
21423 dwarf2_per_cu_data::ref_addr_size () const
21424 {
21425 const comp_unit_head *header = this->get_header ();
21426
21427 if (header->version == 2)
21428 return header->addr_size;
21429 else
21430 return header->offset_size;
21431 }
21432
21433 /* A helper function for dwarf2_find_containing_comp_unit that returns
21434 the index of the result, and that searches a vector. It will
21435 return a result even if the offset in question does not actually
21436 occur in any CU. This is separate so that it can be unit
21437 tested. */
21438
21439 static int
21440 dwarf2_find_containing_comp_unit
21441 (sect_offset sect_off,
21442 unsigned int offset_in_dwz,
21443 const std::vector<dwarf2_per_cu_data_up> &all_units)
21444 {
21445 int low, high;
21446
21447 low = 0;
21448 high = all_units.size () - 1;
21449 while (high > low)
21450 {
21451 struct dwarf2_per_cu_data *mid_cu;
21452 int mid = low + (high - low) / 2;
21453
21454 mid_cu = all_units[mid].get ();
21455 if (mid_cu->is_dwz > offset_in_dwz
21456 || (mid_cu->is_dwz == offset_in_dwz
21457 && mid_cu->sect_off + mid_cu->length () > sect_off))
21458 high = mid;
21459 else
21460 low = mid + 1;
21461 }
21462 gdb_assert (low == high);
21463 return low;
21464 }
21465
21466 /* Locate the .debug_info compilation unit from CU's objfile which contains
21467 the DIE at OFFSET. Raises an error on failure. */
21468
21469 static struct dwarf2_per_cu_data *
21470 dwarf2_find_containing_comp_unit (sect_offset sect_off,
21471 unsigned int offset_in_dwz,
21472 dwarf2_per_bfd *per_bfd)
21473 {
21474 int low = dwarf2_find_containing_comp_unit
21475 (sect_off, offset_in_dwz, per_bfd->all_units);
21476 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
21477
21478 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
21479 {
21480 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
21481 error (_("Dwarf Error: could not find partial DIE containing "
21482 "offset %s [in module %s]"),
21483 sect_offset_str (sect_off),
21484 bfd_get_filename (per_bfd->obfd));
21485
21486 gdb_assert (per_bfd->all_units[low-1]->sect_off
21487 <= sect_off);
21488 return per_bfd->all_units[low - 1].get ();
21489 }
21490 else
21491 {
21492 if (low == per_bfd->all_units.size () - 1
21493 && sect_off >= this_cu->sect_off + this_cu->length ())
21494 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
21495 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
21496 return this_cu;
21497 }
21498 }
21499
21500 #if GDB_SELF_TEST
21501
21502 namespace selftests {
21503 namespace find_containing_comp_unit {
21504
21505 static void
21506 run_test ()
21507 {
21508 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
21509 dwarf2_per_cu_data *one_ptr = one.get ();
21510 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
21511 dwarf2_per_cu_data *two_ptr = two.get ();
21512 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
21513 dwarf2_per_cu_data *three_ptr = three.get ();
21514 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
21515 dwarf2_per_cu_data *four_ptr = four.get ();
21516
21517 one->set_length (5);
21518 two->sect_off = sect_offset (one->length ());
21519 two->set_length (7);
21520
21521 three->set_length (5);
21522 three->is_dwz = 1;
21523 four->sect_off = sect_offset (three->length ());
21524 four->set_length (7);
21525 four->is_dwz = 1;
21526
21527 std::vector<dwarf2_per_cu_data_up> units;
21528 units.push_back (std::move (one));
21529 units.push_back (std::move (two));
21530 units.push_back (std::move (three));
21531 units.push_back (std::move (four));
21532
21533 int result;
21534
21535 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
21536 SELF_CHECK (units[result].get () == one_ptr);
21537 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
21538 SELF_CHECK (units[result].get () == one_ptr);
21539 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
21540 SELF_CHECK (units[result].get () == two_ptr);
21541
21542 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
21543 SELF_CHECK (units[result].get () == three_ptr);
21544 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
21545 SELF_CHECK (units[result].get () == three_ptr);
21546 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
21547 SELF_CHECK (units[result].get () == four_ptr);
21548 }
21549
21550 }
21551 }
21552
21553 #endif /* GDB_SELF_TEST */
21554
21555 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
21556
21557 static void
21558 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
21559 enum language pretend_language)
21560 {
21561 struct attribute *attr;
21562
21563 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
21564
21565 /* Set the language we're debugging. */
21566 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
21567 enum language lang;
21568 if (cu->producer != nullptr
21569 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
21570 {
21571 /* The XLCL doesn't generate DW_LANG_OpenCL because this
21572 attribute is not standardised yet. As a workaround for the
21573 language detection we fall back to the DW_AT_producer
21574 string. */
21575 lang = language_opencl;
21576 }
21577 else if (cu->producer != nullptr
21578 && strstr (cu->producer, "GNU Go ") != NULL)
21579 {
21580 /* Similar hack for Go. */
21581 lang = language_go;
21582 }
21583 else if (attr != nullptr)
21584 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
21585 else
21586 lang = pretend_language;
21587
21588 cu->language_defn = language_def (lang);
21589
21590 switch (comp_unit_die->tag)
21591 {
21592 case DW_TAG_compile_unit:
21593 cu->per_cu->set_unit_type (DW_UT_compile);
21594 break;
21595 case DW_TAG_partial_unit:
21596 cu->per_cu->set_unit_type (DW_UT_partial);
21597 break;
21598 case DW_TAG_type_unit:
21599 cu->per_cu->set_unit_type (DW_UT_type);
21600 break;
21601 default:
21602 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
21603 dwarf_tag_name (comp_unit_die->tag),
21604 sect_offset_str (cu->per_cu->sect_off));
21605 }
21606
21607 cu->per_cu->set_lang (lang);
21608 }
21609
21610 /* See read.h. */
21611
21612 dwarf2_cu *
21613 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
21614 {
21615 auto it = m_dwarf2_cus.find (per_cu);
21616 if (it == m_dwarf2_cus.end ())
21617 return nullptr;
21618
21619 return it->second.get ();
21620 }
21621
21622 /* See read.h. */
21623
21624 void
21625 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
21626 std::unique_ptr<dwarf2_cu> cu)
21627 {
21628 gdb_assert (this->get_cu (per_cu) == nullptr);
21629
21630 m_dwarf2_cus[per_cu] = std::move (cu);
21631 }
21632
21633 /* See read.h. */
21634
21635 void
21636 dwarf2_per_objfile::age_comp_units ()
21637 {
21638 dwarf_read_debug_printf_v ("running");
21639
21640 /* This is not expected to be called in the middle of CU expansion. There is
21641 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
21642 loaded in memory. Calling age_comp_units while the queue is in use could
21643 make us free the DIEs for a CU that is in the queue and therefore break
21644 that invariant. */
21645 gdb_assert (!queue.has_value ());
21646
21647 /* Start by clearing all marks. */
21648 for (const auto &pair : m_dwarf2_cus)
21649 pair.second->clear_mark ();
21650
21651 /* Traverse all CUs, mark them and their dependencies if used recently
21652 enough. */
21653 for (const auto &pair : m_dwarf2_cus)
21654 {
21655 dwarf2_cu *cu = pair.second.get ();
21656
21657 cu->last_used++;
21658 if (cu->last_used <= dwarf_max_cache_age)
21659 cu->mark ();
21660 }
21661
21662 /* Delete all CUs still not marked. */
21663 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
21664 {
21665 dwarf2_cu *cu = it->second.get ();
21666
21667 if (!cu->is_marked ())
21668 {
21669 dwarf_read_debug_printf_v ("deleting old CU %s",
21670 sect_offset_str (cu->per_cu->sect_off));
21671 it = m_dwarf2_cus.erase (it);
21672 }
21673 else
21674 it++;
21675 }
21676 }
21677
21678 /* See read.h. */
21679
21680 void
21681 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
21682 {
21683 auto it = m_dwarf2_cus.find (per_cu);
21684 if (it == m_dwarf2_cus.end ())
21685 return;
21686
21687 m_dwarf2_cus.erase (it);
21688 }
21689
21690 dwarf2_per_objfile::~dwarf2_per_objfile ()
21691 {
21692 remove_all_cus ();
21693 }
21694
21695 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21696 We store these in a hash table separate from the DIEs, and preserve them
21697 when the DIEs are flushed out of cache.
21698
21699 The CU "per_cu" pointer is needed because offset alone is not enough to
21700 uniquely identify the type. A file may have multiple .debug_types sections,
21701 or the type may come from a DWO file. Furthermore, while it's more logical
21702 to use per_cu->section+offset, with Fission the section with the data is in
21703 the DWO file but we don't know that section at the point we need it.
21704 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21705 because we can enter the lookup routine, get_die_type_at_offset, from
21706 outside this file, and thus won't necessarily have PER_CU->cu.
21707 Fortunately, PER_CU is stable for the life of the objfile. */
21708
21709 struct dwarf2_per_cu_offset_and_type
21710 {
21711 const struct dwarf2_per_cu_data *per_cu;
21712 sect_offset sect_off;
21713 struct type *type;
21714 };
21715
21716 /* Hash function for a dwarf2_per_cu_offset_and_type. */
21717
21718 static hashval_t
21719 per_cu_offset_and_type_hash (const void *item)
21720 {
21721 const struct dwarf2_per_cu_offset_and_type *ofs
21722 = (const struct dwarf2_per_cu_offset_and_type *) item;
21723
21724 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
21725 }
21726
21727 /* Equality function for a dwarf2_per_cu_offset_and_type. */
21728
21729 static int
21730 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
21731 {
21732 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
21733 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
21734 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
21735 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
21736
21737 return (ofs_lhs->per_cu == ofs_rhs->per_cu
21738 && ofs_lhs->sect_off == ofs_rhs->sect_off);
21739 }
21740
21741 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21742 table if necessary. For convenience, return TYPE.
21743
21744 The DIEs reading must have careful ordering to:
21745 * Not cause infinite loops trying to read in DIEs as a prerequisite for
21746 reading current DIE.
21747 * Not trying to dereference contents of still incompletely read in types
21748 while reading in other DIEs.
21749 * Enable referencing still incompletely read in types just by a pointer to
21750 the type without accessing its fields.
21751
21752 Therefore caller should follow these rules:
21753 * Try to fetch any prerequisite types we may need to build this DIE type
21754 before building the type and calling set_die_type.
21755 * After building type call set_die_type for current DIE as soon as
21756 possible before fetching more types to complete the current type.
21757 * Make the type as complete as possible before fetching more types. */
21758
21759 static struct type *
21760 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21761 bool skip_data_location)
21762 {
21763 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21764 struct dwarf2_per_cu_offset_and_type **slot, ofs;
21765 struct objfile *objfile = per_objfile->objfile;
21766 struct attribute *attr;
21767 struct dynamic_prop prop;
21768
21769 /* For Ada types, make sure that the gnat-specific data is always
21770 initialized (if not already set). There are a few types where
21771 we should not be doing so, because the type-specific area is
21772 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21773 where the type-specific area is used to store the floatformat).
21774 But this is not a problem, because the gnat-specific information
21775 is actually not needed for these types. */
21776 if (need_gnat_info (cu)
21777 && type->code () != TYPE_CODE_FUNC
21778 && type->code () != TYPE_CODE_FLT
21779 && type->code () != TYPE_CODE_METHODPTR
21780 && type->code () != TYPE_CODE_MEMBERPTR
21781 && type->code () != TYPE_CODE_METHOD
21782 && type->code () != TYPE_CODE_FIXED_POINT
21783 && !HAVE_GNAT_AUX_INFO (type))
21784 INIT_GNAT_SPECIFIC (type);
21785
21786 /* Read DW_AT_allocated and set in type. */
21787 attr = dwarf2_attr (die, DW_AT_allocated, 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_ALLOCATED, prop);
21793 }
21794
21795 /* Read DW_AT_associated and set in type. */
21796 attr = dwarf2_attr (die, DW_AT_associated, 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_ASSOCIATED, prop);
21802 }
21803
21804 /* Read DW_AT_rank and set in type. */
21805 attr = dwarf2_attr (die, DW_AT_rank, cu);
21806 if (attr != NULL)
21807 {
21808 struct type *prop_type = cu->addr_sized_int_type (false);
21809 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21810 type->add_dyn_prop (DYN_PROP_RANK, prop);
21811 }
21812
21813 /* Read DW_AT_data_location and set in type. */
21814 if (!skip_data_location)
21815 {
21816 attr = dwarf2_attr (die, DW_AT_data_location, cu);
21817 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
21818 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
21819 }
21820
21821 if (per_objfile->die_type_hash == NULL)
21822 per_objfile->die_type_hash
21823 = htab_up (htab_create_alloc (127,
21824 per_cu_offset_and_type_hash,
21825 per_cu_offset_and_type_eq,
21826 NULL, xcalloc, xfree));
21827
21828 ofs.per_cu = cu->per_cu;
21829 ofs.sect_off = die->sect_off;
21830 ofs.type = type;
21831 slot = (struct dwarf2_per_cu_offset_and_type **)
21832 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
21833 if (*slot)
21834 complaint (_("A problem internal to GDB: DIE %s has type already set"),
21835 sect_offset_str (die->sect_off));
21836 *slot = XOBNEW (&objfile->objfile_obstack,
21837 struct dwarf2_per_cu_offset_and_type);
21838 **slot = ofs;
21839 return type;
21840 }
21841
21842 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
21843 or return NULL if the die does not have a saved type. */
21844
21845 static struct type *
21846 get_die_type_at_offset (sect_offset sect_off,
21847 dwarf2_per_cu_data *per_cu,
21848 dwarf2_per_objfile *per_objfile)
21849 {
21850 struct dwarf2_per_cu_offset_and_type *slot, ofs;
21851
21852 if (per_objfile->die_type_hash == NULL)
21853 return NULL;
21854
21855 ofs.per_cu = per_cu;
21856 ofs.sect_off = sect_off;
21857 slot = ((struct dwarf2_per_cu_offset_and_type *)
21858 htab_find (per_objfile->die_type_hash.get (), &ofs));
21859 if (slot)
21860 return slot->type;
21861 else
21862 return NULL;
21863 }
21864
21865 /* Look up the type for DIE in CU in die_type_hash,
21866 or return NULL if DIE does not have a saved type. */
21867
21868 static struct type *
21869 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
21870 {
21871 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
21872 }
21873
21874 struct cmd_list_element *set_dwarf_cmdlist;
21875 struct cmd_list_element *show_dwarf_cmdlist;
21876
21877 static void
21878 show_check_physname (struct ui_file *file, int from_tty,
21879 struct cmd_list_element *c, const char *value)
21880 {
21881 gdb_printf (file,
21882 _("Whether to check \"physname\" is %s.\n"),
21883 value);
21884 }
21885
21886 void _initialize_dwarf2_read ();
21887 void
21888 _initialize_dwarf2_read ()
21889 {
21890 add_setshow_prefix_cmd ("dwarf", class_maintenance,
21891 _("\
21892 Set DWARF specific variables.\n\
21893 Configure DWARF variables such as the cache size."),
21894 _("\
21895 Show DWARF specific variables.\n\
21896 Show DWARF variables such as the cache size."),
21897 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
21898 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
21899
21900 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21901 &dwarf_max_cache_age, _("\
21902 Set the upper bound on the age of cached DWARF compilation units."), _("\
21903 Show the upper bound on the age of cached DWARF compilation units."), _("\
21904 A higher limit means that cached compilation units will be stored\n\
21905 in memory longer, and more total memory will be used. Zero disables\n\
21906 caching, which can slow down startup."),
21907 NULL,
21908 show_dwarf_max_cache_age,
21909 &set_dwarf_cmdlist,
21910 &show_dwarf_cmdlist);
21911
21912 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
21913 Set debugging of the DWARF reader."), _("\
21914 Show debugging of the DWARF reader."), _("\
21915 When enabled (non-zero), debugging messages are printed during DWARF\n\
21916 reading and symtab expansion. A value of 1 (one) provides basic\n\
21917 information. A value greater than 1 provides more verbose information."),
21918 NULL,
21919 NULL,
21920 &setdebuglist, &showdebuglist);
21921
21922 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
21923 Set debugging of the DWARF DIE reader."), _("\
21924 Show debugging of the DWARF DIE reader."), _("\
21925 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21926 The value is the maximum depth to print."),
21927 NULL,
21928 NULL,
21929 &setdebuglist, &showdebuglist);
21930
21931 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
21932 Set debugging of the dwarf line reader."), _("\
21933 Show debugging of the dwarf line reader."), _("\
21934 When enabled (non-zero), line number entries are dumped as they are read in.\n\
21935 A value of 1 (one) provides basic information.\n\
21936 A value greater than 1 provides more verbose information."),
21937 NULL,
21938 NULL,
21939 &setdebuglist, &showdebuglist);
21940
21941 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21942 Set cross-checking of \"physname\" code against demangler."), _("\
21943 Show cross-checking of \"physname\" code against demangler."), _("\
21944 When enabled, GDB's internal \"physname\" code is checked against\n\
21945 the demangler."),
21946 NULL, show_check_physname,
21947 &setdebuglist, &showdebuglist);
21948
21949 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21950 &dwarf2_locexpr_funcs);
21951 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21952 &dwarf2_loclist_funcs);
21953
21954 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21955 &dwarf2_block_frame_base_locexpr_funcs);
21956 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21957 &dwarf2_block_frame_base_loclist_funcs);
21958
21959 #if GDB_SELF_TEST
21960 selftests::register_test ("dw2_expand_symtabs_matching",
21961 selftests::dw2_expand_symtabs_matching::run_test);
21962 selftests::register_test ("dwarf2_find_containing_comp_unit",
21963 selftests::find_containing_comp_unit::run_test);
21964 #endif
21965 }