]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
06aa4e36b4da249b2004370b681a40d1bb820cda
[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 struct type *bounds = type_allocator (objfile).new_type ();
13551 bounds->set_code (TYPE_CODE_STRUCT);
13552
13553 bounds->set_num_fields (range_fields.size ());
13554 bounds->set_fields
13555 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
13556 * sizeof (struct field))));
13557 memcpy (bounds->fields (), range_fields.data (),
13558 bounds->num_fields () * sizeof (struct field));
13559
13560 int last_fieldno = range_fields.size () - 1;
13561 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
13562 + bounds->field (last_fieldno).type ()->length ());
13563 bounds->set_length (align_up (bounds_size, max_align));
13564
13565 /* Rewrite the existing array type in place. Specifically, we
13566 remove any dynamic properties we might have read, and we replace
13567 the index types. */
13568 struct type *iter = type;
13569 for (int i = 0; i < range_fields.size (); i += 2)
13570 {
13571 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
13572 iter->main_type->dyn_prop_list = nullptr;
13573 iter->set_index_type
13574 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
13575 iter = iter->target_type ();
13576 }
13577
13578 struct type *result = type_allocator (objfile).new_type ();
13579 result->set_code (TYPE_CODE_STRUCT);
13580
13581 result->set_num_fields (2);
13582 result->set_fields
13583 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
13584 * sizeof (struct field))));
13585
13586 /* The names are chosen to coincide with what the compiler does with
13587 -fgnat-encodings=all, which the Ada code in gdb already
13588 understands. */
13589 result->field (0).set_name ("P_ARRAY");
13590 result->field (0).set_type (lookup_pointer_type (type));
13591
13592 result->field (1).set_name ("P_BOUNDS");
13593 result->field (1).set_type (lookup_pointer_type (bounds));
13594 result->field (1).set_loc_bitpos (8 * bounds_offset);
13595
13596 result->set_name (type->name ());
13597 result->set_length (result->field (0).type ()->length ()
13598 + result->field (1).type ()->length ());
13599
13600 return result;
13601 }
13602
13603 /* Extract all information from a DW_TAG_array_type DIE and put it in
13604 the DIE's type field. For now, this only handles one dimensional
13605 arrays. */
13606
13607 static struct type *
13608 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13609 {
13610 struct objfile *objfile = cu->per_objfile->objfile;
13611 struct die_info *child_die;
13612 struct type *type;
13613 struct type *element_type, *range_type, *index_type;
13614 struct attribute *attr;
13615 const char *name;
13616 struct dynamic_prop *byte_stride_prop = NULL;
13617 unsigned int bit_stride = 0;
13618
13619 element_type = die_type (die, cu);
13620
13621 /* The die_type call above may have already set the type for this DIE. */
13622 type = get_die_type (die, cu);
13623 if (type)
13624 return type;
13625
13626 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13627 if (attr != NULL)
13628 {
13629 int stride_ok;
13630 struct type *prop_type = cu->addr_sized_int_type (false);
13631
13632 byte_stride_prop
13633 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
13634 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
13635 prop_type);
13636 if (!stride_ok)
13637 {
13638 complaint (_("unable to read array DW_AT_byte_stride "
13639 " - DIE at %s [in module %s]"),
13640 sect_offset_str (die->sect_off),
13641 objfile_name (cu->per_objfile->objfile));
13642 /* Ignore this attribute. We will likely not be able to print
13643 arrays of this type correctly, but there is little we can do
13644 to help if we cannot read the attribute's value. */
13645 byte_stride_prop = NULL;
13646 }
13647 }
13648
13649 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13650 if (attr != NULL)
13651 bit_stride = attr->constant_value (0);
13652
13653 /* Irix 6.2 native cc creates array types without children for
13654 arrays with unspecified length. */
13655 if (die->child == NULL)
13656 {
13657 index_type = objfile_type (objfile)->builtin_int;
13658 range_type = create_static_range_type (NULL, index_type, 0, -1);
13659 type = create_array_type_with_stride (NULL, element_type, range_type,
13660 byte_stride_prop, bit_stride);
13661 return set_die_type (die, type, cu);
13662 }
13663
13664 std::vector<struct type *> range_types;
13665 child_die = die->child;
13666 while (child_die && child_die->tag)
13667 {
13668 if (child_die->tag == DW_TAG_subrange_type
13669 || child_die->tag == DW_TAG_generic_subrange)
13670 {
13671 struct type *child_type = read_type_die (child_die, cu);
13672
13673 if (child_type != NULL)
13674 {
13675 /* The range type was succesfully read. Save it for the
13676 array type creation. */
13677 range_types.push_back (child_type);
13678 }
13679 }
13680 child_die = child_die->sibling;
13681 }
13682
13683 if (range_types.empty ())
13684 {
13685 complaint (_("unable to find array range - DIE at %s [in module %s]"),
13686 sect_offset_str (die->sect_off),
13687 objfile_name (cu->per_objfile->objfile));
13688 return NULL;
13689 }
13690
13691 /* Dwarf2 dimensions are output from left to right, create the
13692 necessary array types in backwards order. */
13693
13694 type = element_type;
13695
13696 if (read_array_order (die, cu) == DW_ORD_col_major)
13697 {
13698 int i = 0;
13699
13700 while (i < range_types.size ())
13701 {
13702 type = create_array_type_with_stride (NULL, type, range_types[i++],
13703 byte_stride_prop, bit_stride);
13704 type->set_is_multi_dimensional (true);
13705 bit_stride = 0;
13706 byte_stride_prop = nullptr;
13707 }
13708 }
13709 else
13710 {
13711 size_t ndim = range_types.size ();
13712 while (ndim-- > 0)
13713 {
13714 type = create_array_type_with_stride (NULL, type, range_types[ndim],
13715 byte_stride_prop, bit_stride);
13716 type->set_is_multi_dimensional (true);
13717 bit_stride = 0;
13718 byte_stride_prop = nullptr;
13719 }
13720 }
13721
13722 /* Clear the flag on the outermost array type. */
13723 type->set_is_multi_dimensional (false);
13724 gdb_assert (type != element_type);
13725
13726 /* Understand Dwarf2 support for vector types (like they occur on
13727 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13728 array type. This is not part of the Dwarf2/3 standard yet, but a
13729 custom vendor extension. The main difference between a regular
13730 array and the vector variant is that vectors are passed by value
13731 to functions. */
13732 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13733 if (attr != nullptr)
13734 make_vector_type (type);
13735
13736 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13737 implementation may choose to implement triple vectors using this
13738 attribute. */
13739 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13740 if (attr != nullptr && attr->form_is_unsigned ())
13741 {
13742 if (attr->as_unsigned () >= type->length ())
13743 type->set_length (attr->as_unsigned ());
13744 else
13745 complaint (_("DW_AT_byte_size for array type smaller "
13746 "than the total size of elements"));
13747 }
13748
13749 name = dwarf2_name (die, cu);
13750 if (name)
13751 type->set_name (name);
13752
13753 maybe_set_alignment (cu, die, type);
13754
13755 struct type *replacement_type = nullptr;
13756 if (cu->lang () == language_ada)
13757 {
13758 replacement_type = quirk_ada_thick_pointer (die, cu, type);
13759 if (replacement_type != nullptr)
13760 type = replacement_type;
13761 }
13762
13763 /* Install the type in the die. */
13764 set_die_type (die, type, cu, replacement_type != nullptr);
13765
13766 /* set_die_type should be already done. */
13767 set_descriptive_type (type, die, cu);
13768
13769 return type;
13770 }
13771
13772 static enum dwarf_array_dim_ordering
13773 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13774 {
13775 struct attribute *attr;
13776
13777 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13778
13779 if (attr != nullptr)
13780 {
13781 LONGEST val = attr->constant_value (-1);
13782 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
13783 return (enum dwarf_array_dim_ordering) val;
13784 }
13785
13786 /* GNU F77 is a special case, as at 08/2004 array type info is the
13787 opposite order to the dwarf2 specification, but data is still
13788 laid out as per normal fortran.
13789
13790 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13791 version checking. */
13792
13793 if (cu->lang () == language_fortran
13794 && cu->producer && strstr (cu->producer, "GNU F77"))
13795 {
13796 return DW_ORD_row_major;
13797 }
13798
13799 switch (cu->language_defn->array_ordering ())
13800 {
13801 case array_column_major:
13802 return DW_ORD_col_major;
13803 case array_row_major:
13804 default:
13805 return DW_ORD_row_major;
13806 };
13807 }
13808
13809 /* Extract all information from a DW_TAG_set_type DIE and put it in
13810 the DIE's type field. */
13811
13812 static struct type *
13813 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13814 {
13815 struct type *domain_type, *set_type;
13816 struct attribute *attr;
13817
13818 domain_type = die_type (die, cu);
13819
13820 /* The die_type call above may have already set the type for this DIE. */
13821 set_type = get_die_type (die, cu);
13822 if (set_type)
13823 return set_type;
13824
13825 set_type = create_set_type (NULL, domain_type);
13826
13827 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13828 if (attr != nullptr && attr->form_is_unsigned ())
13829 set_type->set_length (attr->as_unsigned ());
13830
13831 maybe_set_alignment (cu, die, set_type);
13832
13833 return set_die_type (die, set_type, cu);
13834 }
13835
13836 /* A helper for read_common_block that creates a locexpr baton.
13837 SYM is the symbol which we are marking as computed.
13838 COMMON_DIE is the DIE for the common block.
13839 COMMON_LOC is the location expression attribute for the common
13840 block itself.
13841 MEMBER_LOC is the location expression attribute for the particular
13842 member of the common block that we are processing.
13843 CU is the CU from which the above come. */
13844
13845 static void
13846 mark_common_block_symbol_computed (struct symbol *sym,
13847 struct die_info *common_die,
13848 struct attribute *common_loc,
13849 struct attribute *member_loc,
13850 struct dwarf2_cu *cu)
13851 {
13852 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13853 struct objfile *objfile = per_objfile->objfile;
13854 struct dwarf2_locexpr_baton *baton;
13855 gdb_byte *ptr;
13856 unsigned int cu_off;
13857 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
13858 LONGEST offset = 0;
13859
13860 gdb_assert (common_loc && member_loc);
13861 gdb_assert (common_loc->form_is_block ());
13862 gdb_assert (member_loc->form_is_block ()
13863 || member_loc->form_is_constant ());
13864
13865 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13866 baton->per_objfile = per_objfile;
13867 baton->per_cu = cu->per_cu;
13868 gdb_assert (baton->per_cu);
13869
13870 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13871
13872 if (member_loc->form_is_constant ())
13873 {
13874 offset = member_loc->constant_value (0);
13875 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13876 }
13877 else
13878 baton->size += member_loc->as_block ()->size;
13879
13880 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
13881 baton->data = ptr;
13882
13883 *ptr++ = DW_OP_call4;
13884 cu_off = common_die->sect_off - cu->per_cu->sect_off;
13885 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13886 ptr += 4;
13887
13888 if (member_loc->form_is_constant ())
13889 {
13890 *ptr++ = DW_OP_addr;
13891 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13892 ptr += cu->header.addr_size;
13893 }
13894 else
13895 {
13896 /* We have to copy the data here, because DW_OP_call4 will only
13897 use a DW_AT_location attribute. */
13898 struct dwarf_block *block = member_loc->as_block ();
13899 memcpy (ptr, block->data, block->size);
13900 ptr += block->size;
13901 }
13902
13903 *ptr++ = DW_OP_plus;
13904 gdb_assert (ptr - baton->data == baton->size);
13905
13906 SYMBOL_LOCATION_BATON (sym) = baton;
13907 sym->set_aclass_index (dwarf2_locexpr_index);
13908 }
13909
13910 /* Create appropriate locally-scoped variables for all the
13911 DW_TAG_common_block entries. Also create a struct common_block
13912 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13913 is used to separate the common blocks name namespace from regular
13914 variable names. */
13915
13916 static void
13917 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13918 {
13919 struct attribute *attr;
13920
13921 attr = dwarf2_attr (die, DW_AT_location, cu);
13922 if (attr != nullptr)
13923 {
13924 /* Support the .debug_loc offsets. */
13925 if (attr->form_is_block ())
13926 {
13927 /* Ok. */
13928 }
13929 else if (attr->form_is_section_offset ())
13930 {
13931 dwarf2_complex_location_expr_complaint ();
13932 attr = NULL;
13933 }
13934 else
13935 {
13936 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13937 "common block member");
13938 attr = NULL;
13939 }
13940 }
13941
13942 if (die->child != NULL)
13943 {
13944 struct objfile *objfile = cu->per_objfile->objfile;
13945 struct die_info *child_die;
13946 size_t n_entries = 0, size;
13947 struct common_block *common_block;
13948 struct symbol *sym;
13949
13950 for (child_die = die->child;
13951 child_die && child_die->tag;
13952 child_die = child_die->sibling)
13953 ++n_entries;
13954
13955 size = (sizeof (struct common_block)
13956 + (n_entries - 1) * sizeof (struct symbol *));
13957 common_block
13958 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
13959 size);
13960 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13961 common_block->n_entries = 0;
13962
13963 for (child_die = die->child;
13964 child_die && child_die->tag;
13965 child_die = child_die->sibling)
13966 {
13967 /* Create the symbol in the DW_TAG_common_block block in the current
13968 symbol scope. */
13969 sym = new_symbol (child_die, NULL, cu);
13970 if (sym != NULL)
13971 {
13972 struct attribute *member_loc;
13973
13974 common_block->contents[common_block->n_entries++] = sym;
13975
13976 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13977 cu);
13978 if (member_loc)
13979 {
13980 /* GDB has handled this for a long time, but it is
13981 not specified by DWARF. It seems to have been
13982 emitted by gfortran at least as recently as:
13983 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
13984 complaint (_("Variable in common block has "
13985 "DW_AT_data_member_location "
13986 "- DIE at %s [in module %s]"),
13987 sect_offset_str (child_die->sect_off),
13988 objfile_name (objfile));
13989
13990 if (member_loc->form_is_section_offset ())
13991 dwarf2_complex_location_expr_complaint ();
13992 else if (member_loc->form_is_constant ()
13993 || member_loc->form_is_block ())
13994 {
13995 if (attr != nullptr)
13996 mark_common_block_symbol_computed (sym, die, attr,
13997 member_loc, cu);
13998 }
13999 else
14000 dwarf2_complex_location_expr_complaint ();
14001 }
14002 }
14003 }
14004
14005 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14006 sym->set_value_common_block (common_block);
14007 }
14008 }
14009
14010 /* Create a type for a C++ namespace. */
14011
14012 static struct type *
14013 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14014 {
14015 struct objfile *objfile = cu->per_objfile->objfile;
14016 const char *previous_prefix, *name;
14017 int is_anonymous;
14018 struct type *type;
14019
14020 /* For extensions, reuse the type of the original namespace. */
14021 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14022 {
14023 struct die_info *ext_die;
14024 struct dwarf2_cu *ext_cu = cu;
14025
14026 ext_die = dwarf2_extension (die, &ext_cu);
14027 type = read_type_die (ext_die, ext_cu);
14028
14029 /* EXT_CU may not be the same as CU.
14030 Ensure TYPE is recorded with CU in die_type_hash. */
14031 return set_die_type (die, type, cu);
14032 }
14033
14034 name = namespace_name (die, &is_anonymous, cu);
14035
14036 /* Now build the name of the current namespace. */
14037
14038 previous_prefix = determine_prefix (die, cu);
14039 if (previous_prefix[0] != '\0')
14040 name = typename_concat (&objfile->objfile_obstack,
14041 previous_prefix, name, 0, cu);
14042
14043 /* Create the type. */
14044 type = type_allocator (objfile).new_type (TYPE_CODE_NAMESPACE, 0, name);
14045
14046 return set_die_type (die, type, cu);
14047 }
14048
14049 /* Read a namespace scope. */
14050
14051 static void
14052 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14053 {
14054 struct objfile *objfile = cu->per_objfile->objfile;
14055 int is_anonymous;
14056
14057 /* Add a symbol associated to this if we haven't seen the namespace
14058 before. Also, add a using directive if it's an anonymous
14059 namespace. */
14060
14061 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14062 {
14063 struct type *type;
14064
14065 type = read_type_die (die, cu);
14066 new_symbol (die, type, cu);
14067
14068 namespace_name (die, &is_anonymous, cu);
14069 if (is_anonymous)
14070 {
14071 const char *previous_prefix = determine_prefix (die, cu);
14072
14073 std::vector<const char *> excludes;
14074 add_using_directive (using_directives (cu),
14075 previous_prefix, type->name (), NULL,
14076 NULL, excludes,
14077 read_decl_line (die, cu),
14078 0, &objfile->objfile_obstack);
14079 }
14080 }
14081
14082 if (die->child != NULL)
14083 {
14084 struct die_info *child_die = die->child;
14085
14086 while (child_die && child_die->tag)
14087 {
14088 process_die (child_die, cu);
14089 child_die = child_die->sibling;
14090 }
14091 }
14092 }
14093
14094 /* Read a Fortran module as type. This DIE can be only a declaration used for
14095 imported module. Still we need that type as local Fortran "use ... only"
14096 declaration imports depend on the created type in determine_prefix. */
14097
14098 static struct type *
14099 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14100 {
14101 struct objfile *objfile = cu->per_objfile->objfile;
14102 const char *module_name;
14103 struct type *type;
14104
14105 module_name = dwarf2_name (die, cu);
14106 type = type_allocator (objfile).new_type (TYPE_CODE_MODULE, 0, module_name);
14107
14108 return set_die_type (die, type, cu);
14109 }
14110
14111 /* Read a Fortran module. */
14112
14113 static void
14114 read_module (struct die_info *die, struct dwarf2_cu *cu)
14115 {
14116 struct die_info *child_die = die->child;
14117 struct type *type;
14118
14119 type = read_type_die (die, cu);
14120 new_symbol (die, type, cu);
14121
14122 while (child_die && child_die->tag)
14123 {
14124 process_die (child_die, cu);
14125 child_die = child_die->sibling;
14126 }
14127 }
14128
14129 /* Return the name of the namespace represented by DIE. Set
14130 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14131 namespace. */
14132
14133 static const char *
14134 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14135 {
14136 struct die_info *current_die;
14137 const char *name = NULL;
14138
14139 /* Loop through the extensions until we find a name. */
14140
14141 for (current_die = die;
14142 current_die != NULL;
14143 current_die = dwarf2_extension (die, &cu))
14144 {
14145 /* We don't use dwarf2_name here so that we can detect the absence
14146 of a name -> anonymous namespace. */
14147 name = dwarf2_string_attr (die, DW_AT_name, cu);
14148
14149 if (name != NULL)
14150 break;
14151 }
14152
14153 /* Is it an anonymous namespace? */
14154
14155 *is_anonymous = (name == NULL);
14156 if (*is_anonymous)
14157 name = CP_ANONYMOUS_NAMESPACE_STR;
14158
14159 return name;
14160 }
14161
14162 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14163 the user defined type vector. */
14164
14165 static struct type *
14166 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14167 {
14168 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
14169 struct comp_unit_head *cu_header = &cu->header;
14170 struct type *type;
14171 struct attribute *attr_byte_size;
14172 struct attribute *attr_address_class;
14173 int byte_size, addr_class;
14174 struct type *target_type;
14175
14176 target_type = die_type (die, cu);
14177
14178 /* The die_type call above may have already set the type for this DIE. */
14179 type = get_die_type (die, cu);
14180 if (type)
14181 return type;
14182
14183 type = lookup_pointer_type (target_type);
14184
14185 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14186 if (attr_byte_size)
14187 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
14188 else
14189 byte_size = cu_header->addr_size;
14190
14191 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14192 if (attr_address_class)
14193 addr_class = attr_address_class->constant_value (DW_ADDR_none);
14194 else
14195 addr_class = DW_ADDR_none;
14196
14197 ULONGEST alignment = get_alignment (cu, die);
14198
14199 /* If the pointer size, alignment, or address class is different
14200 than the default, create a type variant marked as such and set
14201 the length accordingly. */
14202 if (type->length () != byte_size
14203 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
14204 && alignment != TYPE_RAW_ALIGN (type))
14205 || addr_class != DW_ADDR_none)
14206 {
14207 if (gdbarch_address_class_type_flags_p (gdbarch))
14208 {
14209 type_instance_flags type_flags
14210 = gdbarch_address_class_type_flags (gdbarch, byte_size,
14211 addr_class);
14212 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14213 == 0);
14214 type = make_type_with_address_space (type, type_flags);
14215 }
14216 else if (type->length () != byte_size)
14217 {
14218 complaint (_("invalid pointer size %d"), byte_size);
14219 }
14220 else if (TYPE_RAW_ALIGN (type) != alignment)
14221 {
14222 complaint (_("Invalid DW_AT_alignment"
14223 " - DIE at %s [in module %s]"),
14224 sect_offset_str (die->sect_off),
14225 objfile_name (cu->per_objfile->objfile));
14226 }
14227 else
14228 {
14229 /* Should we also complain about unhandled address classes? */
14230 }
14231 }
14232
14233 type->set_length (byte_size);
14234 set_type_align (type, alignment);
14235 return set_die_type (die, type, cu);
14236 }
14237
14238 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14239 the user defined type vector. */
14240
14241 static struct type *
14242 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14243 {
14244 struct type *type;
14245 struct type *to_type;
14246 struct type *domain;
14247
14248 to_type = die_type (die, cu);
14249 domain = die_containing_type (die, cu);
14250
14251 /* The calls above may have already set the type for this DIE. */
14252 type = get_die_type (die, cu);
14253 if (type)
14254 return type;
14255
14256 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
14257 type = lookup_methodptr_type (to_type);
14258 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
14259 {
14260 struct type *new_type
14261 = type_allocator (cu->per_objfile->objfile).new_type ();
14262
14263 smash_to_method_type (new_type, domain, to_type->target_type (),
14264 to_type->fields (), to_type->num_fields (),
14265 to_type->has_varargs ());
14266 type = lookup_methodptr_type (new_type);
14267 }
14268 else
14269 type = lookup_memberptr_type (to_type, domain);
14270
14271 return set_die_type (die, type, cu);
14272 }
14273
14274 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14275 the user defined type vector. */
14276
14277 static struct type *
14278 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14279 enum type_code refcode)
14280 {
14281 struct comp_unit_head *cu_header = &cu->header;
14282 struct type *type, *target_type;
14283 struct attribute *attr;
14284
14285 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14286
14287 target_type = die_type (die, cu);
14288
14289 /* The die_type call above may have already set the type for this DIE. */
14290 type = get_die_type (die, cu);
14291 if (type)
14292 return type;
14293
14294 type = lookup_reference_type (target_type, refcode);
14295 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14296 if (attr != nullptr)
14297 type->set_length (attr->constant_value (cu_header->addr_size));
14298 else
14299 type->set_length (cu_header->addr_size);
14300
14301 maybe_set_alignment (cu, die, type);
14302 return set_die_type (die, type, cu);
14303 }
14304
14305 /* Add the given cv-qualifiers to the element type of the array. GCC
14306 outputs DWARF type qualifiers that apply to an array, not the
14307 element type. But GDB relies on the array element type to carry
14308 the cv-qualifiers. This mimics section 6.7.3 of the C99
14309 specification. */
14310
14311 static struct type *
14312 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14313 struct type *base_type, int cnst, int voltl)
14314 {
14315 struct type *el_type, *inner_array;
14316
14317 base_type = copy_type (base_type);
14318 inner_array = base_type;
14319
14320 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
14321 {
14322 inner_array->set_target_type (copy_type (inner_array->target_type ()));
14323 inner_array = inner_array->target_type ();
14324 }
14325
14326 el_type = inner_array->target_type ();
14327 cnst |= TYPE_CONST (el_type);
14328 voltl |= TYPE_VOLATILE (el_type);
14329 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
14330
14331 return set_die_type (die, base_type, cu);
14332 }
14333
14334 static struct type *
14335 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14336 {
14337 struct type *base_type, *cv_type;
14338
14339 base_type = die_type (die, cu);
14340
14341 /* The die_type call above may have already set the type for this DIE. */
14342 cv_type = get_die_type (die, cu);
14343 if (cv_type)
14344 return cv_type;
14345
14346 /* In case the const qualifier is applied to an array type, the element type
14347 is so qualified, not the array type (section 6.7.3 of C99). */
14348 if (base_type->code () == TYPE_CODE_ARRAY)
14349 return add_array_cv_type (die, cu, base_type, 1, 0);
14350
14351 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14352 return set_die_type (die, cv_type, cu);
14353 }
14354
14355 static struct type *
14356 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14357 {
14358 struct type *base_type, *cv_type;
14359
14360 base_type = die_type (die, cu);
14361
14362 /* The die_type call above may have already set the type for this DIE. */
14363 cv_type = get_die_type (die, cu);
14364 if (cv_type)
14365 return cv_type;
14366
14367 /* In case the volatile qualifier is applied to an array type, the
14368 element type is so qualified, not the array type (section 6.7.3
14369 of C99). */
14370 if (base_type->code () == TYPE_CODE_ARRAY)
14371 return add_array_cv_type (die, cu, base_type, 0, 1);
14372
14373 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14374 return set_die_type (die, cv_type, cu);
14375 }
14376
14377 /* Handle DW_TAG_restrict_type. */
14378
14379 static struct type *
14380 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14381 {
14382 struct type *base_type, *cv_type;
14383
14384 base_type = die_type (die, cu);
14385
14386 /* The die_type call above may have already set the type for this DIE. */
14387 cv_type = get_die_type (die, cu);
14388 if (cv_type)
14389 return cv_type;
14390
14391 cv_type = make_restrict_type (base_type);
14392 return set_die_type (die, cv_type, cu);
14393 }
14394
14395 /* Handle DW_TAG_atomic_type. */
14396
14397 static struct type *
14398 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14399 {
14400 struct type *base_type, *cv_type;
14401
14402 base_type = die_type (die, cu);
14403
14404 /* The die_type call above may have already set the type for this DIE. */
14405 cv_type = get_die_type (die, cu);
14406 if (cv_type)
14407 return cv_type;
14408
14409 cv_type = make_atomic_type (base_type);
14410 return set_die_type (die, cv_type, cu);
14411 }
14412
14413 /* Extract all information from a DW_TAG_string_type DIE and add to
14414 the user defined type vector. It isn't really a user defined type,
14415 but it behaves like one, with other DIE's using an AT_user_def_type
14416 attribute to reference it. */
14417
14418 static struct type *
14419 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14420 {
14421 struct objfile *objfile = cu->per_objfile->objfile;
14422 struct gdbarch *gdbarch = objfile->arch ();
14423 struct type *type, *range_type, *index_type, *char_type;
14424 struct attribute *attr;
14425 struct dynamic_prop prop;
14426 bool length_is_constant = true;
14427 LONGEST length;
14428
14429 /* There are a couple of places where bit sizes might be made use of
14430 when parsing a DW_TAG_string_type, however, no producer that we know
14431 of make use of these. Handling bit sizes that are a multiple of the
14432 byte size is easy enough, but what about other bit sizes? Lets deal
14433 with that problem when we have to. Warn about these attributes being
14434 unsupported, then parse the type and ignore them like we always
14435 have. */
14436 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
14437 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
14438 {
14439 static bool warning_printed = false;
14440 if (!warning_printed)
14441 {
14442 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
14443 "currently supported on DW_TAG_string_type."));
14444 warning_printed = true;
14445 }
14446 }
14447
14448 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14449 if (attr != nullptr && !attr->form_is_constant ())
14450 {
14451 /* The string length describes the location at which the length of
14452 the string can be found. The size of the length field can be
14453 specified with one of the attributes below. */
14454 struct type *prop_type;
14455 struct attribute *len
14456 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
14457 if (len == nullptr)
14458 len = dwarf2_attr (die, DW_AT_byte_size, cu);
14459 if (len != nullptr && len->form_is_constant ())
14460 {
14461 /* Pass 0 as the default as we know this attribute is constant
14462 and the default value will not be returned. */
14463 LONGEST sz = len->constant_value (0);
14464 prop_type = objfile_int_type (objfile, sz, true);
14465 }
14466 else
14467 {
14468 /* If the size is not specified then we assume it is the size of
14469 an address on this target. */
14470 prop_type = cu->addr_sized_int_type (true);
14471 }
14472
14473 /* Convert the attribute into a dynamic property. */
14474 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
14475 length = 1;
14476 else
14477 length_is_constant = false;
14478 }
14479 else if (attr != nullptr)
14480 {
14481 /* This DW_AT_string_length just contains the length with no
14482 indirection. There's no need to create a dynamic property in this
14483 case. Pass 0 for the default value as we know it will not be
14484 returned in this case. */
14485 length = attr->constant_value (0);
14486 }
14487 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
14488 {
14489 /* We don't currently support non-constant byte sizes for strings. */
14490 length = attr->constant_value (1);
14491 }
14492 else
14493 {
14494 /* Use 1 as a fallback length if we have nothing else. */
14495 length = 1;
14496 }
14497
14498 index_type = objfile_type (objfile)->builtin_int;
14499 if (length_is_constant)
14500 range_type = create_static_range_type (NULL, index_type, 1, length);
14501 else
14502 {
14503 struct dynamic_prop low_bound;
14504
14505 low_bound.set_const_val (1);
14506 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
14507 }
14508 char_type = language_string_char_type (cu->language_defn, gdbarch);
14509 type = create_string_type (NULL, char_type, range_type);
14510
14511 return set_die_type (die, type, cu);
14512 }
14513
14514 /* Assuming that DIE corresponds to a function, returns nonzero
14515 if the function is prototyped. */
14516
14517 static int
14518 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14519 {
14520 struct attribute *attr;
14521
14522 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14523 if (attr && attr->as_boolean ())
14524 return 1;
14525
14526 /* The DWARF standard implies that the DW_AT_prototyped attribute
14527 is only meaningful for C, but the concept also extends to other
14528 languages that allow unprototyped functions (Eg: Objective C).
14529 For all other languages, assume that functions are always
14530 prototyped. */
14531 if (cu->lang () != language_c
14532 && cu->lang () != language_objc
14533 && cu->lang () != language_opencl)
14534 return 1;
14535
14536 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14537 prototyped and unprototyped functions; default to prototyped,
14538 since that is more common in modern code (and RealView warns
14539 about unprototyped functions). */
14540 if (producer_is_realview (cu->producer))
14541 return 1;
14542
14543 return 0;
14544 }
14545
14546 /* Handle DIES due to C code like:
14547
14548 struct foo
14549 {
14550 int (*funcp)(int a, long l);
14551 int b;
14552 };
14553
14554 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14555
14556 static struct type *
14557 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14558 {
14559 struct objfile *objfile = cu->per_objfile->objfile;
14560 struct type *type; /* Type that this function returns. */
14561 struct type *ftype; /* Function that returns above type. */
14562 struct attribute *attr;
14563
14564 type = die_type (die, cu);
14565
14566 /* The die_type call above may have already set the type for this DIE. */
14567 ftype = get_die_type (die, cu);
14568 if (ftype)
14569 return ftype;
14570
14571 ftype = lookup_function_type (type);
14572
14573 if (prototyped_function_p (die, cu))
14574 ftype->set_is_prototyped (true);
14575
14576 /* Store the calling convention in the type if it's available in
14577 the subroutine die. Otherwise set the calling convention to
14578 the default value DW_CC_normal. */
14579 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14580 if (attr != nullptr
14581 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
14582 TYPE_CALLING_CONVENTION (ftype)
14583 = (enum dwarf_calling_convention) attr->constant_value (0);
14584 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14585 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14586 else
14587 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14588
14589 /* Record whether the function returns normally to its caller or not
14590 if the DWARF producer set that information. */
14591 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14592 if (attr && attr->as_boolean ())
14593 TYPE_NO_RETURN (ftype) = 1;
14594
14595 /* We need to add the subroutine type to the die immediately so
14596 we don't infinitely recurse when dealing with parameters
14597 declared as the same subroutine type. */
14598 set_die_type (die, ftype, cu);
14599
14600 if (die->child != NULL)
14601 {
14602 struct type *void_type = objfile_type (objfile)->builtin_void;
14603 struct die_info *child_die;
14604 int nparams, iparams;
14605
14606 /* Count the number of parameters.
14607 FIXME: GDB currently ignores vararg functions, but knows about
14608 vararg member functions. */
14609 nparams = 0;
14610 child_die = die->child;
14611 while (child_die && child_die->tag)
14612 {
14613 if (child_die->tag == DW_TAG_formal_parameter)
14614 nparams++;
14615 else if (child_die->tag == DW_TAG_unspecified_parameters)
14616 ftype->set_has_varargs (true);
14617
14618 child_die = child_die->sibling;
14619 }
14620
14621 /* Allocate storage for parameters and fill them in. */
14622 ftype->set_num_fields (nparams);
14623 ftype->set_fields
14624 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
14625
14626 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14627 even if we error out during the parameters reading below. */
14628 for (iparams = 0; iparams < nparams; iparams++)
14629 ftype->field (iparams).set_type (void_type);
14630
14631 iparams = 0;
14632 child_die = die->child;
14633 while (child_die && child_die->tag)
14634 {
14635 if (child_die->tag == DW_TAG_formal_parameter)
14636 {
14637 struct type *arg_type;
14638
14639 /* DWARF version 2 has no clean way to discern C++
14640 static and non-static member functions. G++ helps
14641 GDB by marking the first parameter for non-static
14642 member functions (which is the this pointer) as
14643 artificial. We pass this information to
14644 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14645
14646 DWARF version 3 added DW_AT_object_pointer, which GCC
14647 4.5 does not yet generate. */
14648 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14649 if (attr != nullptr)
14650 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
14651 else
14652 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14653 arg_type = die_type (child_die, cu);
14654
14655 /* RealView does not mark THIS as const, which the testsuite
14656 expects. GCC marks THIS as const in method definitions,
14657 but not in the class specifications (GCC PR 43053). */
14658 if (cu->lang () == language_cplus
14659 && !TYPE_CONST (arg_type)
14660 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14661 {
14662 int is_this = 0;
14663 struct dwarf2_cu *arg_cu = cu;
14664 const char *name = dwarf2_name (child_die, cu);
14665
14666 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14667 if (attr != nullptr)
14668 {
14669 /* If the compiler emits this, use it. */
14670 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14671 is_this = 1;
14672 }
14673 else if (name && strcmp (name, "this") == 0)
14674 /* Function definitions will have the argument names. */
14675 is_this = 1;
14676 else if (name == NULL && iparams == 0)
14677 /* Declarations may not have the names, so like
14678 elsewhere in GDB, assume an artificial first
14679 argument is "this". */
14680 is_this = 1;
14681
14682 if (is_this)
14683 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14684 arg_type, 0);
14685 }
14686
14687 ftype->field (iparams).set_type (arg_type);
14688 iparams++;
14689 }
14690 child_die = child_die->sibling;
14691 }
14692 }
14693
14694 return ftype;
14695 }
14696
14697 static struct type *
14698 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14699 {
14700 struct objfile *objfile = cu->per_objfile->objfile;
14701 const char *name = NULL;
14702 struct type *this_type, *target_type;
14703
14704 name = dwarf2_full_name (NULL, die, cu);
14705 this_type = type_allocator (objfile).new_type (TYPE_CODE_TYPEDEF, 0, name);
14706 this_type->set_target_is_stub (true);
14707 set_die_type (die, this_type, cu);
14708 target_type = die_type (die, cu);
14709 if (target_type != this_type)
14710 this_type->set_target_type (target_type);
14711 else
14712 {
14713 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14714 spec and cause infinite loops in GDB. */
14715 complaint (_("Self-referential DW_TAG_typedef "
14716 "- DIE at %s [in module %s]"),
14717 sect_offset_str (die->sect_off), objfile_name (objfile));
14718 this_type->set_target_type (nullptr);
14719 }
14720 if (name == NULL)
14721 {
14722 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
14723 anonymous typedefs, which is, strictly speaking, invalid DWARF.
14724 Handle these by just returning the target type, rather than
14725 constructing an anonymous typedef type and trying to handle this
14726 elsewhere. */
14727 set_die_type (die, target_type, cu);
14728 return target_type;
14729 }
14730 return this_type;
14731 }
14732
14733 /* Helper for get_dwarf2_rational_constant that computes the value of
14734 a given gmp_mpz given an attribute. */
14735
14736 static void
14737 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
14738 {
14739 /* GCC will sometimes emit a 16-byte constant value as a DWARF
14740 location expression that pushes an implicit value. */
14741 if (attr->form == DW_FORM_exprloc)
14742 {
14743 dwarf_block *blk = attr->as_block ();
14744 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
14745 {
14746 uint64_t len;
14747 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
14748 blk->data + blk->size,
14749 &len);
14750 if (ptr - blk->data + len <= blk->size)
14751 {
14752 value->read (gdb::make_array_view (ptr, len),
14753 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14754 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14755 true);
14756 return;
14757 }
14758 }
14759
14760 /* On failure set it to 1. */
14761 *value = gdb_mpz (1);
14762 }
14763 else if (attr->form_is_block ())
14764 {
14765 dwarf_block *blk = attr->as_block ();
14766 value->read (gdb::make_array_view (blk->data, blk->size),
14767 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14768 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14769 true);
14770 }
14771 else
14772 *value = gdb_mpz (attr->constant_value (1));
14773 }
14774
14775 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
14776 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
14777
14778 If the numerator and/or numerator attribute is missing,
14779 a complaint is filed, and NUMERATOR and DENOMINATOR are left
14780 untouched. */
14781
14782 static void
14783 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
14784 gdb_mpz *numerator, gdb_mpz *denominator)
14785 {
14786 struct attribute *num_attr, *denom_attr;
14787
14788 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
14789 if (num_attr == nullptr)
14790 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
14791 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14792
14793 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
14794 if (denom_attr == nullptr)
14795 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
14796 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14797
14798 if (num_attr == nullptr || denom_attr == nullptr)
14799 return;
14800
14801 get_mpz (cu, numerator, num_attr);
14802 get_mpz (cu, denominator, denom_attr);
14803 }
14804
14805 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
14806 rational constant, rather than a signed one.
14807
14808 If the rational constant has a negative value, a complaint
14809 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
14810
14811 static void
14812 get_dwarf2_unsigned_rational_constant (struct die_info *die,
14813 struct dwarf2_cu *cu,
14814 gdb_mpz *numerator,
14815 gdb_mpz *denominator)
14816 {
14817 gdb_mpz num (1);
14818 gdb_mpz denom (1);
14819
14820 get_dwarf2_rational_constant (die, cu, &num, &denom);
14821 if (num < 0 && denom < 0)
14822 {
14823 num.negate ();
14824 denom.negate ();
14825 }
14826 else if (num < 0)
14827 {
14828 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
14829 " in DIE at %s"),
14830 sect_offset_str (die->sect_off));
14831 return;
14832 }
14833 else if (denom < 0)
14834 {
14835 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
14836 " in DIE at %s"),
14837 sect_offset_str (die->sect_off));
14838 return;
14839 }
14840
14841 *numerator = std::move (num);
14842 *denominator = std::move (denom);
14843 }
14844
14845 /* Assuming that ENCODING is a string whose contents starting at the
14846 K'th character is "_nn" where "nn" is a decimal number, scan that
14847 number and set RESULT to the value. K is updated to point to the
14848 character immediately following the number.
14849
14850 If the string does not conform to the format described above, false
14851 is returned, and K may or may not be changed. */
14852
14853 static bool
14854 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
14855 {
14856 /* The next character should be an underscore ('_') followed
14857 by a digit. */
14858 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
14859 return false;
14860
14861 /* Skip the underscore. */
14862 k++;
14863 int start = k;
14864
14865 /* Determine the number of digits for our number. */
14866 while (isdigit (encoding[k]))
14867 k++;
14868 if (k == start)
14869 return false;
14870
14871 std::string copy (&encoding[start], k - start);
14872 return result->set (copy.c_str (), 10);
14873 }
14874
14875 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
14876 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
14877 DENOM, update OFFSET, and return true on success. Return false on
14878 failure. */
14879
14880 static bool
14881 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
14882 gdb_mpz *num, gdb_mpz *denom)
14883 {
14884 if (!ada_get_gnat_encoded_number (encoding, offset, num))
14885 return false;
14886 return ada_get_gnat_encoded_number (encoding, offset, denom);
14887 }
14888
14889 /* Assuming DIE corresponds to a fixed point type, finish the creation
14890 of the corresponding TYPE by setting its type-specific data. CU is
14891 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
14892 encodings. It is nullptr if the GNAT encoding should be
14893 ignored. */
14894
14895 static void
14896 finish_fixed_point_type (struct type *type, const char *suffix,
14897 struct die_info *die, struct dwarf2_cu *cu)
14898 {
14899 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
14900 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
14901
14902 /* If GNAT encodings are preferred, don't examine the
14903 attributes. */
14904 struct attribute *attr = nullptr;
14905 if (suffix == nullptr)
14906 {
14907 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
14908 if (attr == nullptr)
14909 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
14910 if (attr == nullptr)
14911 attr = dwarf2_attr (die, DW_AT_small, cu);
14912 }
14913
14914 /* Numerator and denominator of our fixed-point type's scaling factor.
14915 The default is a scaling factor of 1, which we use as a fallback
14916 when we are not able to decode it (problem with the debugging info,
14917 unsupported forms, bug in GDB, etc...). Using that as the default
14918 allows us to at least print the unscaled value, which might still
14919 be useful to a user. */
14920 gdb_mpz scale_num (1);
14921 gdb_mpz scale_denom (1);
14922
14923 if (attr == nullptr)
14924 {
14925 int offset = 0;
14926 if (suffix != nullptr
14927 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14928 &scale_denom)
14929 /* The number might be encoded as _nn_dd_nn_dd, where the
14930 second ratio is the 'small value. In this situation, we
14931 want the second value. */
14932 && (suffix[offset] != '_'
14933 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14934 &scale_denom)))
14935 {
14936 /* Found it. */
14937 }
14938 else
14939 {
14940 /* Scaling factor not found. Assume a scaling factor of 1,
14941 and hope for the best. At least the user will be able to
14942 see the encoded value. */
14943 scale_num = 1;
14944 scale_denom = 1;
14945 complaint (_("no scale found for fixed-point type (DIE at %s)"),
14946 sect_offset_str (die->sect_off));
14947 }
14948 }
14949 else if (attr->name == DW_AT_binary_scale)
14950 {
14951 LONGEST scale_exp = attr->constant_value (0);
14952 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
14953
14954 num_or_denom <<= std::abs (scale_exp);
14955 }
14956 else if (attr->name == DW_AT_decimal_scale)
14957 {
14958 LONGEST scale_exp = attr->constant_value (0);
14959 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
14960
14961 num_or_denom = gdb_mpz::pow (10, std::abs (scale_exp));
14962 }
14963 else if (attr->name == DW_AT_small)
14964 {
14965 struct die_info *scale_die;
14966 struct dwarf2_cu *scale_cu = cu;
14967
14968 scale_die = follow_die_ref (die, attr, &scale_cu);
14969 if (scale_die->tag == DW_TAG_constant)
14970 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
14971 &scale_num, &scale_denom);
14972 else
14973 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
14974 " (DIE at %s)"),
14975 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14976 }
14977 else
14978 {
14979 complaint (_("unsupported scale attribute %s for fixed-point type"
14980 " (DIE at %s)"),
14981 dwarf_attr_name (attr->name),
14982 sect_offset_str (die->sect_off));
14983 }
14984
14985 type->fixed_point_info ().scaling_factor = gdb_mpq (scale_num, scale_denom);
14986 }
14987
14988 /* The gnat-encoding suffix for fixed point. */
14989
14990 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
14991
14992 /* If NAME encodes an Ada fixed-point type, return a pointer to the
14993 "XF" suffix of the name. The text after this is what encodes the
14994 'small and 'delta information. Otherwise, return nullptr. */
14995
14996 static const char *
14997 gnat_encoded_fixed_point_type_info (const char *name)
14998 {
14999 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
15000 }
15001
15002 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15003 (which may be different from NAME) to the architecture back-end to allow
15004 it to guess the correct format if necessary. */
15005
15006 static struct type *
15007 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15008 const char *name_hint, enum bfd_endian byte_order)
15009 {
15010 struct gdbarch *gdbarch = objfile->arch ();
15011 const struct floatformat **format;
15012 struct type *type;
15013
15014 type_allocator alloc (objfile);
15015 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15016 if (format)
15017 type = init_float_type (alloc, bits, name, format, byte_order);
15018 else
15019 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15020
15021 return type;
15022 }
15023
15024 /* Allocate an integer type of size BITS and name NAME. */
15025
15026 static struct type *
15027 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
15028 int bits, int unsigned_p, const char *name)
15029 {
15030 struct type *type;
15031
15032 /* Versions of Intel's C Compiler generate an integer type called "void"
15033 instead of using DW_TAG_unspecified_type. This has been seen on
15034 at least versions 14, 17, and 18. */
15035 if (bits == 0 && producer_is_icc (cu) && name != nullptr
15036 && strcmp (name, "void") == 0)
15037 type = objfile_type (objfile)->builtin_void;
15038 else
15039 {
15040 type_allocator alloc (objfile);
15041 type = init_integer_type (alloc, bits, unsigned_p, name);
15042 }
15043
15044 return type;
15045 }
15046
15047 /* Return true if DIE has a DW_AT_small attribute whose value is
15048 a constant rational, where both the numerator and denominator
15049 are equal to zero.
15050
15051 CU is the DIE's Compilation Unit. */
15052
15053 static bool
15054 has_zero_over_zero_small_attribute (struct die_info *die,
15055 struct dwarf2_cu *cu)
15056 {
15057 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
15058 if (attr == nullptr)
15059 return false;
15060
15061 struct dwarf2_cu *scale_cu = cu;
15062 struct die_info *scale_die
15063 = follow_die_ref (die, attr, &scale_cu);
15064
15065 if (scale_die->tag != DW_TAG_constant)
15066 return false;
15067
15068 gdb_mpz num (1), denom (1);
15069 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
15070 return num == 0 && denom == 0;
15071 }
15072
15073 /* Initialise and return a floating point type of size BITS suitable for
15074 use as a component of a complex number. The NAME_HINT is passed through
15075 when initialising the floating point type and is the name of the complex
15076 type.
15077
15078 As DWARF doesn't currently provide an explicit name for the components
15079 of a complex number, but it can be helpful to have these components
15080 named, we try to select a suitable name based on the size of the
15081 component. */
15082 static struct type *
15083 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
15084 struct objfile *objfile,
15085 int bits, const char *name_hint,
15086 enum bfd_endian byte_order)
15087 {
15088 gdbarch *gdbarch = objfile->arch ();
15089 struct type *tt = nullptr;
15090
15091 /* Try to find a suitable floating point builtin type of size BITS.
15092 We're going to use the name of this type as the name for the complex
15093 target type that we are about to create. */
15094 switch (cu->lang ())
15095 {
15096 case language_fortran:
15097 switch (bits)
15098 {
15099 case 32:
15100 tt = builtin_f_type (gdbarch)->builtin_real;
15101 break;
15102 case 64:
15103 tt = builtin_f_type (gdbarch)->builtin_real_s8;
15104 break;
15105 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15106 case 128:
15107 tt = builtin_f_type (gdbarch)->builtin_real_s16;
15108 break;
15109 }
15110 break;
15111 default:
15112 switch (bits)
15113 {
15114 case 32:
15115 tt = builtin_type (gdbarch)->builtin_float;
15116 break;
15117 case 64:
15118 tt = builtin_type (gdbarch)->builtin_double;
15119 break;
15120 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15121 case 128:
15122 tt = builtin_type (gdbarch)->builtin_long_double;
15123 break;
15124 }
15125 break;
15126 }
15127
15128 /* If the type we found doesn't match the size we were looking for, then
15129 pretend we didn't find a type at all, the complex target type we
15130 create will then be nameless. */
15131 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
15132 tt = nullptr;
15133
15134 const char *name = (tt == nullptr) ? nullptr : tt->name ();
15135 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
15136 }
15137
15138 /* Find a representation of a given base type and install
15139 it in the TYPE field of the die. */
15140
15141 static struct type *
15142 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15143 {
15144 struct objfile *objfile = cu->per_objfile->objfile;
15145 struct type *type;
15146 struct attribute *attr;
15147 int encoding = 0, bits = 0;
15148 const char *name;
15149 gdbarch *arch;
15150
15151 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15152 if (attr != nullptr && attr->form_is_constant ())
15153 encoding = attr->constant_value (0);
15154 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15155 if (attr != nullptr)
15156 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
15157 name = dwarf2_name (die, cu);
15158 if (!name)
15159 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
15160
15161 arch = objfile->arch ();
15162 enum bfd_endian byte_order = gdbarch_byte_order (arch);
15163
15164 attr = dwarf2_attr (die, DW_AT_endianity, cu);
15165 if (attr != nullptr && attr->form_is_constant ())
15166 {
15167 int endianity = attr->constant_value (0);
15168
15169 switch (endianity)
15170 {
15171 case DW_END_big:
15172 byte_order = BFD_ENDIAN_BIG;
15173 break;
15174 case DW_END_little:
15175 byte_order = BFD_ENDIAN_LITTLE;
15176 break;
15177 default:
15178 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
15179 break;
15180 }
15181 }
15182
15183 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
15184 && cu->lang () == language_ada
15185 && has_zero_over_zero_small_attribute (die, cu))
15186 {
15187 /* brobecker/2018-02-24: This is a fixed point type for which
15188 the scaling factor is represented as fraction whose value
15189 does not make sense (zero divided by zero), so we should
15190 normally never see these. However, there is a small category
15191 of fixed point types for which GNAT is unable to provide
15192 the scaling factor via the standard DWARF mechanisms, and
15193 for which the info is provided via the GNAT encodings instead.
15194 This is likely what this DIE is about. */
15195 encoding = (encoding == DW_ATE_signed_fixed
15196 ? DW_ATE_signed
15197 : DW_ATE_unsigned);
15198 }
15199
15200 /* With GNAT encodings, fixed-point information will be encoded in
15201 the type name. Note that this can also occur with the above
15202 zero-over-zero case, which is why this is a separate "if" rather
15203 than an "else if". */
15204 const char *gnat_encoding_suffix = nullptr;
15205 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
15206 && cu->lang () == language_ada
15207 && name != nullptr)
15208 {
15209 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
15210 if (gnat_encoding_suffix != nullptr)
15211 {
15212 gdb_assert (startswith (gnat_encoding_suffix,
15213 GNAT_FIXED_POINT_SUFFIX));
15214 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
15215 name, gnat_encoding_suffix - name);
15216 /* Use -1 here so that SUFFIX points at the "_" after the
15217 "XF". */
15218 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
15219
15220 encoding = (encoding == DW_ATE_signed
15221 ? DW_ATE_signed_fixed
15222 : DW_ATE_unsigned_fixed);
15223 }
15224 }
15225
15226 type_allocator alloc (objfile);
15227 switch (encoding)
15228 {
15229 case DW_ATE_address:
15230 /* Turn DW_ATE_address into a void * pointer. */
15231 type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
15232 type = init_pointer_type (objfile, bits, name, type);
15233 break;
15234 case DW_ATE_boolean:
15235 type = init_boolean_type (alloc, bits, 1, name);
15236 break;
15237 case DW_ATE_complex_float:
15238 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
15239 byte_order);
15240 if (type->code () == TYPE_CODE_ERROR)
15241 {
15242 if (name == nullptr)
15243 {
15244 struct obstack *obstack
15245 = &cu->per_objfile->objfile->objfile_obstack;
15246 name = obconcat (obstack, "_Complex ", type->name (),
15247 nullptr);
15248 }
15249 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15250 }
15251 else
15252 type = init_complex_type (name, type);
15253 break;
15254 case DW_ATE_decimal_float:
15255 type = init_decfloat_type (objfile, bits, name);
15256 break;
15257 case DW_ATE_float:
15258 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
15259 break;
15260 case DW_ATE_signed:
15261 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
15262 break;
15263 case DW_ATE_unsigned:
15264 if (cu->lang () == language_fortran
15265 && name
15266 && startswith (name, "character("))
15267 type = init_character_type (alloc, bits, 1, name);
15268 else
15269 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
15270 break;
15271 case DW_ATE_signed_char:
15272 if (cu->lang () == language_ada
15273 || cu->lang () == language_m2
15274 || cu->lang () == language_pascal
15275 || cu->lang () == language_fortran)
15276 type = init_character_type (alloc, bits, 0, name);
15277 else
15278 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
15279 break;
15280 case DW_ATE_unsigned_char:
15281 if (cu->lang () == language_ada
15282 || cu->lang () == language_m2
15283 || cu->lang () == language_pascal
15284 || cu->lang () == language_fortran
15285 || cu->lang () == language_rust)
15286 type = init_character_type (alloc, bits, 1, name);
15287 else
15288 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
15289 break;
15290 case DW_ATE_UTF:
15291 {
15292 type = init_character_type (alloc, bits, 1, name);
15293 return set_die_type (die, type, cu);
15294 }
15295 break;
15296 case DW_ATE_signed_fixed:
15297 type = init_fixed_point_type (objfile, bits, 0, name);
15298 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15299 break;
15300 case DW_ATE_unsigned_fixed:
15301 type = init_fixed_point_type (objfile, bits, 1, name);
15302 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
15303 break;
15304
15305 default:
15306 complaint (_("unsupported DW_AT_encoding: '%s'"),
15307 dwarf_type_encoding_name (encoding));
15308 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
15309 break;
15310 }
15311
15312 if (type->code () == TYPE_CODE_INT
15313 && name != nullptr
15314 && strcmp (name, "char") == 0)
15315 type->set_has_no_signedness (true);
15316
15317 maybe_set_alignment (cu, die, type);
15318
15319 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
15320
15321 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
15322 {
15323 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15324 if (attr != nullptr && attr->as_unsigned () <= 8 * type->length ())
15325 {
15326 unsigned real_bit_size = attr->as_unsigned ();
15327 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15328 /* Only use the attributes if they make sense together. */
15329 if (attr == nullptr
15330 || (attr->as_unsigned () + real_bit_size
15331 <= 8 * type->length ()))
15332 {
15333 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
15334 = real_bit_size;
15335 if (attr != nullptr)
15336 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
15337 = attr->as_unsigned ();
15338 }
15339 }
15340 }
15341
15342 return set_die_type (die, type, cu);
15343 }
15344
15345 /* A helper function that returns the name of DIE, if it refers to a
15346 variable declaration. */
15347
15348 static const char *
15349 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
15350 {
15351 if (die->tag != DW_TAG_variable)
15352 return nullptr;
15353
15354 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
15355 if (attr == nullptr || !attr->as_boolean ())
15356 return nullptr;
15357
15358 attr = dwarf2_attr (die, DW_AT_name, cu);
15359 if (attr == nullptr)
15360 return nullptr;
15361 return attr->as_string ();
15362 }
15363
15364 /* Parse dwarf attribute if it's a block, reference or constant and put the
15365 resulting value of the attribute into struct bound_prop.
15366 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15367
15368 static int
15369 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15370 struct dwarf2_cu *cu, struct dynamic_prop *prop,
15371 struct type *default_type)
15372 {
15373 struct dwarf2_property_baton *baton;
15374 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15375 struct objfile *objfile = per_objfile->objfile;
15376 struct obstack *obstack = &objfile->objfile_obstack;
15377
15378 gdb_assert (default_type != NULL);
15379
15380 if (attr == NULL || prop == NULL)
15381 return 0;
15382
15383 if (attr->form_is_block ())
15384 {
15385 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15386 baton->property_type = default_type;
15387 baton->locexpr.per_cu = cu->per_cu;
15388 baton->locexpr.per_objfile = per_objfile;
15389
15390 struct dwarf_block *block;
15391 if (attr->form == DW_FORM_data16)
15392 {
15393 size_t data_size = 16;
15394 block = XOBNEW (obstack, struct dwarf_block);
15395 block->size = (data_size
15396 + 2 /* Extra bytes for DW_OP and arg. */);
15397 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
15398 data[0] = DW_OP_implicit_value;
15399 data[1] = data_size;
15400 memcpy (&data[2], attr->as_block ()->data, data_size);
15401 block->data = data;
15402 }
15403 else
15404 block = attr->as_block ();
15405
15406 baton->locexpr.size = block->size;
15407 baton->locexpr.data = block->data;
15408 switch (attr->name)
15409 {
15410 case DW_AT_string_length:
15411 baton->locexpr.is_reference = true;
15412 break;
15413 default:
15414 baton->locexpr.is_reference = false;
15415 break;
15416 }
15417
15418 prop->set_locexpr (baton);
15419 gdb_assert (prop->baton () != NULL);
15420 }
15421 else if (attr->form_is_ref ())
15422 {
15423 struct dwarf2_cu *target_cu = cu;
15424 struct die_info *target_die;
15425 struct attribute *target_attr;
15426
15427 target_die = follow_die_ref (die, attr, &target_cu);
15428 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15429 if (target_attr == NULL)
15430 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15431 target_cu);
15432 if (target_attr == nullptr)
15433 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
15434 target_cu);
15435 if (target_attr == NULL)
15436 {
15437 const char *name = var_decl_name (target_die, target_cu);
15438 if (name != nullptr)
15439 {
15440 prop->set_variable_name (name);
15441 return 1;
15442 }
15443 return 0;
15444 }
15445
15446 switch (target_attr->name)
15447 {
15448 case DW_AT_location:
15449 if (target_attr->form_is_section_offset ())
15450 {
15451 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15452 baton->property_type = die_type (target_die, target_cu);
15453 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15454 prop->set_loclist (baton);
15455 gdb_assert (prop->baton () != NULL);
15456 }
15457 else if (target_attr->form_is_block ())
15458 {
15459 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15460 baton->property_type = die_type (target_die, target_cu);
15461 baton->locexpr.per_cu = cu->per_cu;
15462 baton->locexpr.per_objfile = per_objfile;
15463 struct dwarf_block *block = target_attr->as_block ();
15464 baton->locexpr.size = block->size;
15465 baton->locexpr.data = block->data;
15466 baton->locexpr.is_reference = true;
15467 prop->set_locexpr (baton);
15468 gdb_assert (prop->baton () != NULL);
15469 }
15470 else
15471 {
15472 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15473 "dynamic property");
15474 return 0;
15475 }
15476 break;
15477 case DW_AT_data_member_location:
15478 case DW_AT_data_bit_offset:
15479 {
15480 LONGEST offset;
15481
15482 if (!handle_member_location (target_die, target_cu, &offset))
15483 return 0;
15484
15485 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15486 baton->property_type = read_type_die (target_die->parent,
15487 target_cu);
15488 baton->offset_info.offset = offset;
15489 baton->offset_info.type = die_type (target_die, target_cu);
15490 prop->set_addr_offset (baton);
15491 break;
15492 }
15493 }
15494 }
15495 else if (attr->form_is_constant ())
15496 prop->set_const_val (attr->constant_value (0));
15497 else if (attr->form_is_section_offset ())
15498 {
15499 switch (attr->name)
15500 {
15501 case DW_AT_string_length:
15502 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15503 baton->property_type = default_type;
15504 fill_in_loclist_baton (cu, &baton->loclist, attr);
15505 prop->set_loclist (baton);
15506 gdb_assert (prop->baton () != NULL);
15507 break;
15508 default:
15509 goto invalid;
15510 }
15511 }
15512 else
15513 goto invalid;
15514
15515 return 1;
15516
15517 invalid:
15518 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15519 dwarf2_name (die, cu));
15520 return 0;
15521 }
15522
15523 /* See read.h. */
15524
15525 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
15526 present (which is valid) then compute the default type based on the
15527 compilation units address size. */
15528
15529 static struct type *
15530 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
15531 {
15532 struct type *index_type = die_type (die, cu);
15533
15534 /* Dwarf-2 specifications explicitly allows to create subrange types
15535 without specifying a base type.
15536 In that case, the base type must be set to the type of
15537 the lower bound, upper bound or count, in that order, if any of these
15538 three attributes references an object that has a type.
15539 If no base type is found, the Dwarf-2 specifications say that
15540 a signed integer type of size equal to the size of an address should
15541 be used.
15542 For the following C code: `extern char gdb_int [];'
15543 GCC produces an empty range DIE.
15544 FIXME: muller/2010-05-28: Possible references to object for low bound,
15545 high bound or count are not yet handled by this code. */
15546 if (index_type->code () == TYPE_CODE_VOID)
15547 index_type = cu->addr_sized_int_type (false);
15548
15549 return index_type;
15550 }
15551
15552 /* Read the given DW_AT_subrange DIE. */
15553
15554 static struct type *
15555 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15556 {
15557 struct type *base_type, *orig_base_type;
15558 struct type *range_type;
15559 struct attribute *attr;
15560 struct dynamic_prop low, high;
15561 int low_default_is_valid;
15562 int high_bound_is_count = 0;
15563 const char *name;
15564
15565 orig_base_type = read_subrange_index_type (die, cu);
15566
15567 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15568 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15569 creating the range type, but we use the result of check_typedef
15570 when examining properties of the type. */
15571 base_type = check_typedef (orig_base_type);
15572
15573 /* The die_type call above may have already set the type for this DIE. */
15574 range_type = get_die_type (die, cu);
15575 if (range_type)
15576 return range_type;
15577
15578 high.set_const_val (0);
15579
15580 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15581 omitting DW_AT_lower_bound. */
15582 switch (cu->lang ())
15583 {
15584 case language_c:
15585 case language_cplus:
15586 low.set_const_val (0);
15587 low_default_is_valid = 1;
15588 break;
15589 case language_fortran:
15590 low.set_const_val (1);
15591 low_default_is_valid = 1;
15592 break;
15593 case language_d:
15594 case language_objc:
15595 case language_rust:
15596 low.set_const_val (0);
15597 low_default_is_valid = (cu->header.version >= 4);
15598 break;
15599 case language_ada:
15600 case language_m2:
15601 case language_pascal:
15602 low.set_const_val (1);
15603 low_default_is_valid = (cu->header.version >= 4);
15604 break;
15605 default:
15606 low.set_const_val (0);
15607 low_default_is_valid = 0;
15608 break;
15609 }
15610
15611 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15612 if (attr != nullptr)
15613 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
15614 else if (!low_default_is_valid)
15615 complaint (_("Missing DW_AT_lower_bound "
15616 "- DIE at %s [in module %s]"),
15617 sect_offset_str (die->sect_off),
15618 objfile_name (cu->per_objfile->objfile));
15619
15620 struct attribute *attr_ub, *attr_count;
15621 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
15622 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15623 {
15624 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
15625 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15626 {
15627 /* If bounds are constant do the final calculation here. */
15628 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
15629 high.set_const_val (low.const_val () + high.const_val () - 1);
15630 else
15631 high_bound_is_count = 1;
15632 }
15633 else
15634 {
15635 if (attr_ub != NULL)
15636 complaint (_("Unresolved DW_AT_upper_bound "
15637 "- DIE at %s [in module %s]"),
15638 sect_offset_str (die->sect_off),
15639 objfile_name (cu->per_objfile->objfile));
15640 if (attr_count != NULL)
15641 complaint (_("Unresolved DW_AT_count "
15642 "- DIE at %s [in module %s]"),
15643 sect_offset_str (die->sect_off),
15644 objfile_name (cu->per_objfile->objfile));
15645 }
15646 }
15647
15648 LONGEST bias = 0;
15649 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
15650 if (bias_attr != nullptr && bias_attr->form_is_constant ())
15651 bias = bias_attr->constant_value (0);
15652
15653 /* Normally, the DWARF producers are expected to use a signed
15654 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15655 But this is unfortunately not always the case, as witnessed
15656 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15657 is used instead. To work around that ambiguity, we treat
15658 the bounds as signed, and thus sign-extend their values, when
15659 the base type is signed.
15660
15661 Skip it if the base type's length is larger than ULONGEST, to avoid
15662 the undefined behavior of a too large left shift. We don't really handle
15663 constants larger than 8 bytes anyway, at the moment. */
15664
15665 if (base_type->length () <= sizeof (ULONGEST))
15666 {
15667 ULONGEST negative_mask
15668 = -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
15669
15670 if (low.kind () == PROP_CONST
15671 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
15672 low.set_const_val (low.const_val () | negative_mask);
15673
15674 if (high.kind () == PROP_CONST
15675 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
15676 high.set_const_val (high.const_val () | negative_mask);
15677 }
15678
15679 /* Check for bit and byte strides. */
15680 struct dynamic_prop byte_stride_prop;
15681 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
15682 if (attr_byte_stride != nullptr)
15683 {
15684 struct type *prop_type = cu->addr_sized_int_type (false);
15685 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
15686 prop_type);
15687 }
15688
15689 struct dynamic_prop bit_stride_prop;
15690 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
15691 if (attr_bit_stride != nullptr)
15692 {
15693 /* It only makes sense to have either a bit or byte stride. */
15694 if (attr_byte_stride != nullptr)
15695 {
15696 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
15697 "- DIE at %s [in module %s]"),
15698 sect_offset_str (die->sect_off),
15699 objfile_name (cu->per_objfile->objfile));
15700 attr_bit_stride = nullptr;
15701 }
15702 else
15703 {
15704 struct type *prop_type = cu->addr_sized_int_type (false);
15705 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
15706 prop_type);
15707 }
15708 }
15709
15710 if (attr_byte_stride != nullptr
15711 || attr_bit_stride != nullptr)
15712 {
15713 bool byte_stride_p = (attr_byte_stride != nullptr);
15714 struct dynamic_prop *stride
15715 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
15716
15717 range_type
15718 = create_range_type_with_stride (NULL, orig_base_type, &low,
15719 &high, bias, stride, byte_stride_p);
15720 }
15721 else
15722 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
15723
15724 if (high_bound_is_count)
15725 range_type->bounds ()->flag_upper_bound_is_count = 1;
15726
15727 /* Ada expects an empty array on no boundary attributes. */
15728 if (attr == NULL && cu->lang () != language_ada)
15729 range_type->bounds ()->high.set_undefined ();
15730
15731 name = dwarf2_name (die, cu);
15732 if (name)
15733 range_type->set_name (name);
15734
15735 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15736 if (attr != nullptr)
15737 range_type->set_length (attr->constant_value (0));
15738
15739 maybe_set_alignment (cu, die, range_type);
15740
15741 set_die_type (die, range_type, cu);
15742
15743 /* set_die_type should be already done. */
15744 set_descriptive_type (range_type, die, cu);
15745
15746 return range_type;
15747 }
15748
15749 static struct type *
15750 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15751 {
15752 struct type *type;
15753
15754 type = (type_allocator (cu->per_objfile->objfile)
15755 .new_type (TYPE_CODE_VOID, 0, nullptr));
15756 type->set_name (dwarf2_name (die, cu));
15757
15758 /* In Ada, an unspecified type is typically used when the description
15759 of the type is deferred to a different unit. When encountering
15760 such a type, we treat it as a stub, and try to resolve it later on,
15761 when needed.
15762 Mark this as a stub type for all languages though. */
15763 type->set_is_stub (true);
15764
15765 return set_die_type (die, type, cu);
15766 }
15767
15768 /* Read a single die and all its descendents. Set the die's sibling
15769 field to NULL; set other fields in the die correctly, and set all
15770 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15771 location of the info_ptr after reading all of those dies. PARENT
15772 is the parent of the die in question. */
15773
15774 static struct die_info *
15775 read_die_and_children (const struct die_reader_specs *reader,
15776 const gdb_byte *info_ptr,
15777 const gdb_byte **new_info_ptr,
15778 struct die_info *parent)
15779 {
15780 struct die_info *die;
15781 const gdb_byte *cur_ptr;
15782
15783 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0, true);
15784 if (die == NULL)
15785 {
15786 *new_info_ptr = cur_ptr;
15787 return NULL;
15788 }
15789 store_in_ref_table (die, reader->cu);
15790
15791 if (die->has_children)
15792 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15793 else
15794 {
15795 die->child = NULL;
15796 *new_info_ptr = cur_ptr;
15797 }
15798
15799 die->sibling = NULL;
15800 die->parent = parent;
15801 return die;
15802 }
15803
15804 /* Read a die, all of its descendents, and all of its siblings; set
15805 all of the fields of all of the dies correctly. Arguments are as
15806 in read_die_and_children. */
15807
15808 static struct die_info *
15809 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15810 const gdb_byte *info_ptr,
15811 const gdb_byte **new_info_ptr,
15812 struct die_info *parent)
15813 {
15814 struct die_info *first_die, *last_sibling;
15815 const gdb_byte *cur_ptr;
15816
15817 cur_ptr = info_ptr;
15818 first_die = last_sibling = NULL;
15819
15820 while (1)
15821 {
15822 struct die_info *die
15823 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15824
15825 if (die == NULL)
15826 {
15827 *new_info_ptr = cur_ptr;
15828 return first_die;
15829 }
15830
15831 if (!first_die)
15832 first_die = die;
15833 else
15834 last_sibling->sibling = die;
15835
15836 last_sibling = die;
15837 }
15838 }
15839
15840 /* Read a die, all of its descendents, and all of its siblings; set
15841 all of the fields of all of the dies correctly. Arguments are as
15842 in read_die_and_children.
15843 This the main entry point for reading a DIE and all its children. */
15844
15845 static struct die_info *
15846 read_die_and_siblings (const struct die_reader_specs *reader,
15847 const gdb_byte *info_ptr,
15848 const gdb_byte **new_info_ptr,
15849 struct die_info *parent)
15850 {
15851 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15852 new_info_ptr, parent);
15853
15854 if (dwarf_die_debug)
15855 {
15856 gdb_printf (gdb_stdlog,
15857 "Read die from %s@0x%x of %s:\n",
15858 reader->die_section->get_name (),
15859 (unsigned) (info_ptr - reader->die_section->buffer),
15860 bfd_get_filename (reader->abfd));
15861 die->dump (dwarf_die_debug);
15862 }
15863
15864 return die;
15865 }
15866
15867 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15868 attributes.
15869 The caller is responsible for filling in the extra attributes
15870 and updating (*DIEP)->num_attrs.
15871 Set DIEP to point to a newly allocated die with its information,
15872 except for its child, sibling, and parent fields. */
15873
15874 static const gdb_byte *
15875 read_full_die_1 (const struct die_reader_specs *reader,
15876 struct die_info **diep, const gdb_byte *info_ptr,
15877 int num_extra_attrs, bool allow_reprocess)
15878 {
15879 unsigned int abbrev_number, bytes_read, i;
15880 const struct abbrev_info *abbrev;
15881 struct die_info *die;
15882 struct dwarf2_cu *cu = reader->cu;
15883 bfd *abfd = reader->abfd;
15884
15885 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15886 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15887 info_ptr += bytes_read;
15888 if (!abbrev_number)
15889 {
15890 *diep = NULL;
15891 return info_ptr;
15892 }
15893
15894 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
15895 if (!abbrev)
15896 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15897 abbrev_number,
15898 bfd_get_filename (abfd));
15899
15900 die = die_info::allocate (&cu->comp_unit_obstack,
15901 abbrev->num_attrs + num_extra_attrs);
15902 die->sect_off = sect_off;
15903 die->tag = abbrev->tag;
15904 die->abbrev = abbrev_number;
15905 die->has_children = abbrev->has_children;
15906
15907 /* Make the result usable.
15908 The caller needs to update num_attrs after adding the extra
15909 attributes. */
15910 die->num_attrs = abbrev->num_attrs;
15911
15912 for (i = 0; i < abbrev->num_attrs; ++i)
15913 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15914 info_ptr, allow_reprocess);
15915
15916 *diep = die;
15917 return info_ptr;
15918 }
15919
15920 /* Read a die and all its attributes.
15921 Set DIEP to point to a newly allocated die with its information,
15922 except for its child, sibling, and parent fields. */
15923
15924 static const gdb_byte *
15925 read_toplevel_die (const struct die_reader_specs *reader,
15926 struct die_info **diep, const gdb_byte *info_ptr,
15927 gdb::array_view<attribute *> extra_attrs)
15928 {
15929 const gdb_byte *result;
15930 struct dwarf2_cu *cu = reader->cu;
15931
15932 result = read_full_die_1 (reader, diep, info_ptr, extra_attrs.size (),
15933 false);
15934
15935 /* Copy in the extra attributes, if any. */
15936 attribute *next = &(*diep)->attrs[(*diep)->num_attrs];
15937 for (attribute *extra : extra_attrs)
15938 *next++ = *extra;
15939
15940 struct attribute *attr = (*diep)->attr (DW_AT_str_offsets_base);
15941 if (attr != nullptr && attr->form_is_unsigned ())
15942 cu->str_offsets_base = attr->as_unsigned ();
15943
15944 attr = (*diep)->attr (DW_AT_loclists_base);
15945 if (attr != nullptr)
15946 cu->loclist_base = attr->as_unsigned ();
15947
15948 auto maybe_addr_base = (*diep)->addr_base ();
15949 if (maybe_addr_base.has_value ())
15950 cu->addr_base = *maybe_addr_base;
15951
15952 attr = (*diep)->attr (DW_AT_rnglists_base);
15953 if (attr != nullptr)
15954 cu->rnglists_base = attr->as_unsigned ();
15955
15956 for (int i = 0; i < (*diep)->num_attrs; ++i)
15957 {
15958 if ((*diep)->attrs[i].form_requires_reprocessing ())
15959 read_attribute_reprocess (reader, &(*diep)->attrs[i], (*diep)->tag);
15960 }
15961
15962 (*diep)->num_attrs += extra_attrs.size ();
15963
15964 if (dwarf_die_debug)
15965 {
15966 gdb_printf (gdb_stdlog,
15967 "Read die from %s@0x%x of %s:\n",
15968 reader->die_section->get_name (),
15969 (unsigned) (info_ptr - reader->die_section->buffer),
15970 bfd_get_filename (reader->abfd));
15971 (*diep)->dump (dwarf_die_debug);
15972 }
15973
15974 return result;
15975 }
15976 \f
15977
15978 void
15979 cooked_indexer::check_bounds (cutu_reader *reader)
15980 {
15981 if (reader->cu->per_cu->addresses_seen)
15982 return;
15983
15984 dwarf2_cu *cu = reader->cu;
15985
15986 CORE_ADDR best_lowpc = 0, best_highpc = 0;
15987 /* Possibly set the default values of LOWPC and HIGHPC from
15988 `DW_AT_ranges'. */
15989 dwarf2_find_base_address (reader->comp_unit_die, cu);
15990 enum pc_bounds_kind cu_bounds_kind
15991 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
15992 cu, m_index_storage->get_addrmap (), cu->per_cu);
15993 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
15994 {
15995 struct objfile *objfile = cu->per_objfile->objfile;
15996 CORE_ADDR baseaddr = objfile->text_section_offset ();
15997 struct gdbarch *gdbarch = objfile->arch ();
15998 CORE_ADDR low
15999 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
16000 - baseaddr);
16001 CORE_ADDR high
16002 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
16003 - baseaddr - 1);
16004 /* Store the contiguous range if it is not empty; it can be
16005 empty for CUs with no code. */
16006 m_index_storage->get_addrmap ()->set_empty (low, high, cu->per_cu);
16007
16008 cu->per_cu->addresses_seen = true;
16009 }
16010 }
16011
16012 /* Helper function that returns true if TAG can have a linkage
16013 name. */
16014
16015 static bool
16016 tag_can_have_linkage_name (enum dwarf_tag tag)
16017 {
16018 switch (tag)
16019 {
16020 case DW_TAG_variable:
16021 case DW_TAG_subprogram:
16022 return true;
16023
16024 default:
16025 return false;
16026 }
16027 }
16028
16029 cutu_reader *
16030 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
16031 dwarf2_per_objfile *per_objfile,
16032 sect_offset sect_off, bool is_dwz,
16033 bool for_scanning)
16034 {
16035 /* Lookups for type unit references are always in the CU, and
16036 cross-CU references will crash. */
16037 if (reader->cu->per_cu->is_dwz == is_dwz
16038 && reader->cu->header.offset_in_cu_p (sect_off))
16039 return reader;
16040
16041 dwarf2_per_cu_data *per_cu
16042 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
16043 per_objfile->per_bfd);
16044
16045 /* When scanning, we only want to visit a given CU a single time.
16046 Doing this check here avoids self-imports as well. */
16047 if (for_scanning)
16048 {
16049 bool nope = false;
16050 if (!per_cu->scanned.compare_exchange_strong (nope, true))
16051 return nullptr;
16052 }
16053 if (per_cu == m_per_cu)
16054 return reader;
16055
16056 cutu_reader *result = m_index_storage->get_reader (per_cu);
16057 if (result == nullptr)
16058 {
16059 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
16060 m_index_storage->get_abbrev_cache ());
16061
16062 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
16063 language_minimal);
16064 std::unique_ptr<cutu_reader> copy
16065 (new cutu_reader (std::move (new_reader)));
16066 result = m_index_storage->preserve (std::move (copy));
16067 }
16068
16069 if (result->dummy_p || !result->comp_unit_die->has_children)
16070 return nullptr;
16071
16072 if (for_scanning)
16073 check_bounds (result);
16074
16075 return result;
16076 }
16077
16078 const gdb_byte *
16079 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
16080 cutu_reader *reader,
16081 const gdb_byte *watermark_ptr,
16082 const gdb_byte *info_ptr,
16083 const abbrev_info *abbrev,
16084 const char **name,
16085 const char **linkage_name,
16086 cooked_index_flag *flags,
16087 sect_offset *sibling_offset,
16088 const cooked_index_entry **parent_entry,
16089 CORE_ADDR *maybe_defer,
16090 bool for_specification)
16091 {
16092 bool origin_is_dwz = false;
16093 bool is_declaration = false;
16094 sect_offset origin_offset {};
16095
16096 gdb::optional<CORE_ADDR> low_pc;
16097 gdb::optional<CORE_ADDR> high_pc;
16098 bool high_pc_relative = false;
16099
16100 for (int i = 0; i < abbrev->num_attrs; ++i)
16101 {
16102 attribute attr;
16103 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16104
16105 /* Store the data if it is of an attribute we want to keep in a
16106 partial symbol table. */
16107 switch (attr.name)
16108 {
16109 case DW_AT_name:
16110 switch (abbrev->tag)
16111 {
16112 case DW_TAG_compile_unit:
16113 case DW_TAG_partial_unit:
16114 case DW_TAG_type_unit:
16115 /* Compilation units have a DW_AT_name that is a filename, not
16116 a source language identifier. */
16117 break;
16118
16119 default:
16120 if (*name == nullptr)
16121 *name = attr.as_string ();
16122 break;
16123 }
16124 break;
16125
16126 case DW_AT_linkage_name:
16127 case DW_AT_MIPS_linkage_name:
16128 /* Note that both forms of linkage name might appear. We
16129 assume they will be the same, and we only store the last
16130 one we see. */
16131 if (*linkage_name == nullptr)
16132 *linkage_name = attr.as_string ();
16133 break;
16134
16135 case DW_AT_main_subprogram:
16136 if (attr.as_boolean ())
16137 *flags |= IS_MAIN;
16138 break;
16139
16140 case DW_AT_declaration:
16141 is_declaration = attr.as_boolean ();
16142 break;
16143
16144 case DW_AT_sibling:
16145 if (sibling_offset != nullptr)
16146 *sibling_offset = attr.get_ref_die_offset ();
16147 break;
16148
16149 case DW_AT_specification:
16150 case DW_AT_abstract_origin:
16151 case DW_AT_extension:
16152 origin_offset = attr.get_ref_die_offset ();
16153 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
16154 break;
16155
16156 case DW_AT_external:
16157 if (attr.as_boolean ())
16158 *flags &= ~IS_STATIC;
16159 break;
16160
16161 case DW_AT_enum_class:
16162 if (attr.as_boolean ())
16163 *flags |= IS_ENUM_CLASS;
16164 break;
16165
16166 case DW_AT_low_pc:
16167 low_pc = attr.as_address ();
16168 break;
16169
16170 case DW_AT_high_pc:
16171 high_pc = attr.as_address ();
16172 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
16173 high_pc_relative = true;
16174 break;
16175
16176 case DW_AT_location:
16177 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
16178 {
16179 struct dwarf_block *locdesc = attr.as_block ();
16180 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
16181 if (addr != 0
16182 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
16183 {
16184 low_pc = addr;
16185 /* For variables, we don't want to try decoding the
16186 type just to find the size -- for gdb's purposes
16187 we only need the address of a variable. */
16188 high_pc = addr + 1;
16189 high_pc_relative = false;
16190 }
16191 }
16192 break;
16193
16194 case DW_AT_ranges:
16195 if (!scanning_per_cu->addresses_seen)
16196 {
16197 /* Offset in the .debug_ranges or .debug_rnglist section
16198 (depending on DWARF version). */
16199 ULONGEST ranges_offset = attr.as_unsigned ();
16200
16201 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
16202 want to add this value. */
16203 ranges_offset += reader->cu->gnu_ranges_base;
16204
16205 CORE_ADDR lowpc, highpc;
16206 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
16207 m_index_storage->get_addrmap (),
16208 scanning_per_cu, abbrev->tag);
16209 }
16210 break;
16211 }
16212 }
16213
16214 /* We don't want to examine declarations, but if we found a
16215 declaration when handling DW_AT_specification or the like, then
16216 that is ok. Similarly, we allow an external variable without a
16217 location; those are resolved via minimal symbols. */
16218 if (is_declaration && !for_specification
16219 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
16220 {
16221 /* We always want to recurse into some types, but we may not
16222 want to treat them as definitions. */
16223 if ((abbrev->tag == DW_TAG_class_type
16224 || abbrev->tag == DW_TAG_structure_type
16225 || abbrev->tag == DW_TAG_union_type)
16226 && abbrev->has_children)
16227 *flags |= IS_TYPE_DECLARATION;
16228 else
16229 {
16230 *linkage_name = nullptr;
16231 *name = nullptr;
16232 }
16233 }
16234 else if ((*name == nullptr
16235 || (*linkage_name == nullptr
16236 && tag_can_have_linkage_name (abbrev->tag))
16237 || (*parent_entry == nullptr && m_language != language_c))
16238 && origin_offset != sect_offset (0))
16239 {
16240 cutu_reader *new_reader
16241 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
16242 origin_is_dwz, false);
16243 if (new_reader != nullptr)
16244 {
16245 const gdb_byte *new_info_ptr = (new_reader->buffer
16246 + to_underlying (origin_offset));
16247
16248 if (new_reader->cu == reader->cu
16249 && new_info_ptr > watermark_ptr
16250 && *parent_entry == nullptr)
16251 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
16252 else if (*parent_entry == nullptr)
16253 {
16254 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
16255 void *obj = m_die_range_map.find (lookup);
16256 *parent_entry = static_cast <cooked_index_entry *> (obj);
16257 }
16258
16259 unsigned int bytes_read;
16260 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
16261 new_info_ptr,
16262 &bytes_read);
16263 new_info_ptr += bytes_read;
16264 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
16265 new_abbrev, name, linkage_name, flags, nullptr,
16266 parent_entry, maybe_defer, true);
16267 }
16268 }
16269
16270 if (!for_specification)
16271 {
16272 if (m_language == language_ada
16273 && *linkage_name == nullptr)
16274 *linkage_name = *name;
16275
16276 if (!scanning_per_cu->addresses_seen
16277 && low_pc.has_value ()
16278 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
16279 || *low_pc != 0)
16280 && high_pc.has_value ())
16281 {
16282 if (high_pc_relative)
16283 high_pc = *high_pc + *low_pc;
16284
16285 if (*high_pc > *low_pc)
16286 {
16287 struct objfile *objfile = reader->cu->per_objfile->objfile;
16288 CORE_ADDR baseaddr = objfile->text_section_offset ();
16289 struct gdbarch *gdbarch = objfile->arch ();
16290 CORE_ADDR lo
16291 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
16292 - baseaddr);
16293 CORE_ADDR hi
16294 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
16295 - baseaddr);
16296 m_index_storage->get_addrmap ()->set_empty (lo, hi - 1,
16297 scanning_per_cu);
16298 }
16299 }
16300
16301 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
16302 *flags &= ~IS_STATIC;
16303
16304 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
16305 *name = "(anonymous namespace)";
16306
16307 if (m_language == language_cplus
16308 && (abbrev->tag == DW_TAG_class_type
16309 || abbrev->tag == DW_TAG_interface_type
16310 || abbrev->tag == DW_TAG_structure_type
16311 || abbrev->tag == DW_TAG_union_type
16312 || abbrev->tag == DW_TAG_enumeration_type
16313 || abbrev->tag == DW_TAG_enumerator))
16314 *flags &= ~IS_STATIC;
16315 }
16316
16317 return info_ptr;
16318 }
16319
16320 const gdb_byte *
16321 cooked_indexer::index_imported_unit (cutu_reader *reader,
16322 const gdb_byte *info_ptr,
16323 const abbrev_info *abbrev)
16324 {
16325 sect_offset sect_off {};
16326 bool is_dwz = false;
16327
16328 for (int i = 0; i < abbrev->num_attrs; ++i)
16329 {
16330 /* Note that we never need to reprocess attributes here. */
16331 attribute attr;
16332 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16333
16334 if (attr.name == DW_AT_import)
16335 {
16336 sect_off = attr.get_ref_die_offset ();
16337 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16338 || reader->cu->per_cu->is_dwz);
16339 }
16340 }
16341
16342 /* Did not find DW_AT_import. */
16343 if (sect_off == sect_offset (0))
16344 return info_ptr;
16345
16346 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
16347 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
16348 is_dwz, true);
16349 if (new_reader != nullptr)
16350 {
16351 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
16352
16353 reader->cu->add_dependence (new_reader->cu->per_cu);
16354 }
16355
16356 return info_ptr;
16357 }
16358
16359 const gdb_byte *
16360 cooked_indexer::recurse (cutu_reader *reader,
16361 const gdb_byte *info_ptr,
16362 const cooked_index_entry *parent_entry,
16363 bool fully)
16364 {
16365 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16366
16367 if (parent_entry != nullptr)
16368 {
16369 CORE_ADDR start = form_addr (parent_entry->die_offset,
16370 reader->cu->per_cu->is_dwz);
16371 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
16372 reader->cu->per_cu->is_dwz);
16373 m_die_range_map.set_empty (start, end, (void *) parent_entry);
16374 }
16375
16376 return info_ptr;
16377 }
16378
16379 const gdb_byte *
16380 cooked_indexer::index_dies (cutu_reader *reader,
16381 const gdb_byte *info_ptr,
16382 const cooked_index_entry *parent_entry,
16383 bool fully)
16384 {
16385 const gdb_byte *end_ptr = (reader->buffer
16386 + to_underlying (reader->cu->header.sect_off)
16387 + reader->cu->header.get_length_with_initial ());
16388
16389 while (info_ptr < end_ptr)
16390 {
16391 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
16392 unsigned int bytes_read;
16393 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
16394 &bytes_read);
16395 info_ptr += bytes_read;
16396 if (abbrev == nullptr)
16397 break;
16398
16399 if (abbrev->tag == DW_TAG_imported_unit)
16400 {
16401 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
16402 continue;
16403 }
16404
16405 if (!abbrev->interesting)
16406 {
16407 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
16408 if (fully && abbrev->has_children)
16409 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
16410 continue;
16411 }
16412
16413 const char *name = nullptr;
16414 const char *linkage_name = nullptr;
16415 CORE_ADDR defer = 0;
16416 cooked_index_flag flags = IS_STATIC;
16417 sect_offset sibling {};
16418 const cooked_index_entry *this_parent_entry = parent_entry;
16419 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
16420 info_ptr, abbrev, &name, &linkage_name,
16421 &flags, &sibling, &this_parent_entry,
16422 &defer, false);
16423
16424 if (abbrev->tag == DW_TAG_namespace
16425 && m_language == language_cplus
16426 && strcmp (name, "::") == 0)
16427 {
16428 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
16429 generated bogus DW_TAG_namespace DIEs with a name of "::"
16430 for the global namespace. Work around this problem
16431 here. */
16432 name = nullptr;
16433 }
16434
16435 const cooked_index_entry *this_entry = nullptr;
16436 if (name != nullptr)
16437 {
16438 if (defer != 0)
16439 m_deferred_entries.push_back ({
16440 this_die, name, defer, abbrev->tag, flags
16441 });
16442 else
16443 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
16444 name, this_parent_entry,
16445 m_per_cu);
16446 }
16447
16448 if (linkage_name != nullptr)
16449 {
16450 /* We only want this to be "main" if it has a linkage name
16451 but not an ordinary name. */
16452 if (name != nullptr)
16453 flags = flags & ~IS_MAIN;
16454 /* Set the IS_LINKAGE on for everything except when functions
16455 have linkage name present but name is absent. */
16456 if (name != nullptr
16457 || (abbrev->tag != DW_TAG_subprogram
16458 && abbrev->tag != DW_TAG_inlined_subroutine
16459 && abbrev->tag != DW_TAG_entry_point))
16460 flags = flags | IS_LINKAGE;
16461 m_index_storage->add (this_die, abbrev->tag, flags,
16462 linkage_name, nullptr, m_per_cu);
16463 }
16464
16465 if (abbrev->has_children)
16466 {
16467 switch (abbrev->tag)
16468 {
16469 case DW_TAG_class_type:
16470 case DW_TAG_interface_type:
16471 case DW_TAG_structure_type:
16472 case DW_TAG_union_type:
16473 if (m_language != language_c && this_entry != nullptr)
16474 {
16475 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16476 continue;
16477 }
16478 break;
16479
16480 case DW_TAG_enumeration_type:
16481 /* We need to recurse even for an anonymous enumeration.
16482 Which scope we record as the parent scope depends on
16483 whether we're reading an "enum class". If so, we use
16484 the enum itself as the parent, yielding names like
16485 "enum_class::enumerator"; otherwise we inject the
16486 names into our own parent scope. */
16487 info_ptr = recurse (reader, info_ptr,
16488 ((flags & IS_ENUM_CLASS) == 0)
16489 ? parent_entry
16490 : this_entry,
16491 fully);
16492 continue;
16493
16494 case DW_TAG_module:
16495 if (this_entry == nullptr)
16496 break;
16497 /* FALLTHROUGH */
16498 case DW_TAG_namespace:
16499 /* We don't check THIS_ENTRY for a namespace, to handle
16500 the ancient G++ workaround pointed out above. */
16501 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16502 continue;
16503
16504 case DW_TAG_subprogram:
16505 if ((m_language == language_fortran
16506 || m_language == language_ada)
16507 && this_entry != nullptr)
16508 {
16509 info_ptr = recurse (reader, info_ptr, this_entry, true);
16510 continue;
16511 }
16512 break;
16513 }
16514
16515 if (sibling != sect_offset (0))
16516 {
16517 const gdb_byte *sibling_ptr
16518 = reader->buffer + to_underlying (sibling);
16519
16520 if (sibling_ptr < info_ptr)
16521 complaint (_("DW_AT_sibling points backwards"));
16522 else if (sibling_ptr > reader->buffer_end)
16523 reader->die_section->overflow_complaint ();
16524 else
16525 info_ptr = sibling_ptr;
16526 }
16527 else
16528 info_ptr = skip_children (reader, info_ptr);
16529 }
16530 }
16531
16532 return info_ptr;
16533 }
16534
16535 void
16536 cooked_indexer::make_index (cutu_reader *reader)
16537 {
16538 check_bounds (reader);
16539 find_file_and_directory (reader->comp_unit_die, reader->cu);
16540 if (!reader->comp_unit_die->has_children)
16541 return;
16542 index_dies (reader, reader->info_ptr, nullptr, false);
16543
16544 for (const auto &entry : m_deferred_entries)
16545 {
16546 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
16547 void *obj = m_die_range_map.find (key);
16548 cooked_index_entry *parent = static_cast <cooked_index_entry *> (obj);
16549 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
16550 entry.name, parent, m_per_cu);
16551 }
16552 }
16553
16554 /* An implementation of quick_symbol_functions for the cooked DWARF
16555 index. */
16556
16557 struct cooked_index_functions : public dwarf2_base_index_functions
16558 {
16559 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
16560 CORE_ADDR adjusted_pc) override;
16561
16562 struct compunit_symtab *find_compunit_symtab_by_address
16563 (struct objfile *objfile, CORE_ADDR address) override;
16564
16565 void dump (struct objfile *objfile) override
16566 {
16567 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16568 cooked_index *index
16569 = (gdb::checked_static_cast<cooked_index *>
16570 (per_objfile->per_bfd->index_table.get ()));
16571 if (index == nullptr)
16572 return;
16573
16574 gdb_printf ("Cooked index in use:\n");
16575 gdb_printf ("\n");
16576 index->dump (objfile->arch ());
16577 }
16578
16579 void expand_matching_symbols
16580 (struct objfile *,
16581 const lookup_name_info &lookup_name,
16582 domain_enum domain,
16583 int global,
16584 symbol_compare_ftype *ordered_compare) override;
16585
16586 bool expand_symtabs_matching
16587 (struct objfile *objfile,
16588 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16589 const lookup_name_info *lookup_name,
16590 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16591 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16592 block_search_flags search_flags,
16593 domain_enum domain,
16594 enum search_domain kind) override;
16595
16596 bool can_lazily_read_symbols () override
16597 {
16598 return true;
16599 }
16600
16601 void read_partial_symbols (struct objfile *objfile) override
16602 {
16603 if (dwarf2_has_info (objfile, nullptr))
16604 dwarf2_build_psymtabs (objfile);
16605 }
16606 };
16607
16608 dwarf2_per_cu_data *
16609 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
16610 CORE_ADDR adjusted_pc)
16611 {
16612 cooked_index *table
16613 = (gdb::checked_static_cast<cooked_index *>
16614 (per_bfd->index_table.get ()));
16615 if (table == nullptr)
16616 return nullptr;
16617 return table->lookup (adjusted_pc);
16618 }
16619
16620 struct compunit_symtab *
16621 cooked_index_functions::find_compunit_symtab_by_address
16622 (struct objfile *objfile, CORE_ADDR address)
16623 {
16624 if (objfile->sect_index_data == -1)
16625 return nullptr;
16626
16627 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16628 cooked_index *table
16629 = (gdb::checked_static_cast<cooked_index *>
16630 (per_objfile->per_bfd->index_table.get ()));
16631 if (table == nullptr)
16632 return nullptr;
16633
16634 CORE_ADDR baseaddr = objfile->data_section_offset ();
16635 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
16636 if (per_cu == nullptr)
16637 return nullptr;
16638
16639 return dw2_instantiate_symtab (per_cu, per_objfile, false);
16640 }
16641
16642 void
16643 cooked_index_functions::expand_matching_symbols
16644 (struct objfile *objfile,
16645 const lookup_name_info &lookup_name,
16646 domain_enum domain,
16647 int global,
16648 symbol_compare_ftype *ordered_compare)
16649 {
16650 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16651 cooked_index *table
16652 = (gdb::checked_static_cast<cooked_index *>
16653 (per_objfile->per_bfd->index_table.get ()));
16654 if (table == nullptr)
16655 return;
16656
16657 const block_search_flags search_flags = (global
16658 ? SEARCH_GLOBAL_BLOCK
16659 : SEARCH_STATIC_BLOCK);
16660 const language_defn *lang = language_def (language_ada);
16661 symbol_name_matcher_ftype *name_match
16662 = lang->get_symbol_name_matcher (lookup_name);
16663
16664 for (const cooked_index_entry *entry : table->all_entries ())
16665 {
16666 QUIT;
16667
16668 if (entry->parent_entry != nullptr)
16669 continue;
16670
16671 if (!entry->matches (search_flags)
16672 || !entry->matches (domain))
16673 continue;
16674
16675 if (name_match (entry->canonical, lookup_name, nullptr))
16676 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
16677 }
16678 }
16679
16680 bool
16681 cooked_index_functions::expand_symtabs_matching
16682 (struct objfile *objfile,
16683 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16684 const lookup_name_info *lookup_name,
16685 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16686 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16687 block_search_flags search_flags,
16688 domain_enum domain,
16689 enum search_domain kind)
16690 {
16691 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16692
16693 cooked_index *table
16694 = (gdb::checked_static_cast<cooked_index *>
16695 (per_objfile->per_bfd->index_table.get ()));
16696 if (table == nullptr)
16697 return true;
16698
16699 table->wait ();
16700
16701 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
16702
16703 /* This invariant is documented in quick-functions.h. */
16704 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
16705 if (lookup_name == nullptr)
16706 {
16707 for (dwarf2_per_cu_data *per_cu
16708 : all_units_range (per_objfile->per_bfd))
16709 {
16710 QUIT;
16711
16712 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
16713 file_matcher,
16714 expansion_notify))
16715 return false;
16716 }
16717 return true;
16718 }
16719
16720 lookup_name_info lookup_name_without_params
16721 = lookup_name->make_ignore_params ();
16722 bool completing = lookup_name->completion_mode ();
16723
16724 /* Unique styles of language splitting. */
16725 static const enum language unique_styles[] =
16726 {
16727 /* No splitting is also a style. */
16728 language_c,
16729 /* This includes Rust. */
16730 language_cplus,
16731 /* This includes Go. */
16732 language_d,
16733 language_ada
16734 };
16735
16736 for (enum language lang : unique_styles)
16737 {
16738 std::vector<gdb::string_view> name_vec
16739 = lookup_name_without_params.split_name (lang);
16740 std::string last_name = gdb::to_string (name_vec.back ());
16741
16742 for (const cooked_index_entry *entry : table->find (last_name,
16743 completing))
16744 {
16745 QUIT;
16746
16747 /* No need to consider symbols from expanded CUs. */
16748 if (per_objfile->symtab_set_p (entry->per_cu))
16749 continue;
16750
16751 /* If file-matching was done, we don't need to consider
16752 symbols from unmarked CUs. */
16753 if (file_matcher != nullptr && !entry->per_cu->mark)
16754 continue;
16755
16756 /* See if the symbol matches the type filter. */
16757 if (!entry->matches (search_flags)
16758 || !entry->matches (domain)
16759 || !entry->matches (kind))
16760 continue;
16761
16762 /* We've found the base name of the symbol; now walk its
16763 parentage chain, ensuring that each component
16764 matches. */
16765 bool found = true;
16766
16767 const cooked_index_entry *parent = entry->parent_entry;
16768 for (int i = name_vec.size () - 1; i > 0; --i)
16769 {
16770 /* If we ran out of entries, or if this segment doesn't
16771 match, this did not match. */
16772 if (parent == nullptr
16773 || strncmp (parent->name, name_vec[i - 1].data (),
16774 name_vec[i - 1].length ()) != 0)
16775 {
16776 found = false;
16777 break;
16778 }
16779
16780 parent = parent->parent_entry;
16781 }
16782
16783 if (!found)
16784 continue;
16785
16786 /* Might have been looking for "a::b" and found
16787 "x::a::b". */
16788 if (symbol_matcher == nullptr)
16789 {
16790 symbol_name_match_type match_type
16791 = lookup_name_without_params.match_type ();
16792 if ((match_type == symbol_name_match_type::FULL
16793 || (lang != language_ada
16794 && match_type == symbol_name_match_type::EXPRESSION))
16795 && parent != nullptr)
16796 continue;
16797 }
16798 else
16799 {
16800 auto_obstack temp_storage;
16801 const char *full_name = entry->full_name (&temp_storage);
16802 if (!symbol_matcher (full_name))
16803 continue;
16804 }
16805
16806 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
16807 file_matcher,
16808 expansion_notify))
16809 return false;
16810 }
16811 }
16812
16813 return true;
16814 }
16815
16816 /* Return a new cooked_index_functions object. */
16817
16818 static quick_symbol_functions_up
16819 make_cooked_index_funcs ()
16820 {
16821 return quick_symbol_functions_up (new cooked_index_functions);
16822 }
16823
16824 quick_symbol_functions_up
16825 cooked_index::make_quick_functions () const
16826 {
16827 return make_cooked_index_funcs ();
16828 }
16829
16830 \f
16831
16832 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
16833 contents from the given SECTION in the HEADER.
16834
16835 HEADER_OFFSET is the offset of the header in the section. */
16836 static void
16837 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
16838 struct dwarf2_section_info *section,
16839 sect_offset header_offset)
16840 {
16841 unsigned int bytes_read;
16842 bfd *abfd = section->get_bfd_owner ();
16843 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
16844
16845 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
16846 info_ptr += bytes_read;
16847
16848 header->version = read_2_bytes (abfd, info_ptr);
16849 info_ptr += 2;
16850
16851 header->addr_size = read_1_byte (abfd, info_ptr);
16852 info_ptr += 1;
16853
16854 header->segment_collector_size = read_1_byte (abfd, info_ptr);
16855 info_ptr += 1;
16856
16857 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
16858 }
16859
16860 /* Return the DW_AT_loclists_base value for the CU. */
16861 static ULONGEST
16862 lookup_loclist_base (struct dwarf2_cu *cu)
16863 {
16864 /* For the .dwo unit, the loclist_base points to the first offset following
16865 the header. The header consists of the following entities-
16866 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
16867 bit format)
16868 2. version (2 bytes)
16869 3. address size (1 byte)
16870 4. segment selector size (1 byte)
16871 5. offset entry count (4 bytes)
16872 These sizes are derived as per the DWARFv5 standard. */
16873 if (cu->dwo_unit != nullptr)
16874 {
16875 if (cu->header.initial_length_size == 4)
16876 return LOCLIST_HEADER_SIZE32;
16877 return LOCLIST_HEADER_SIZE64;
16878 }
16879 return cu->loclist_base;
16880 }
16881
16882 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
16883 array of offsets in the .debug_loclists section. */
16884
16885 static sect_offset
16886 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
16887 {
16888 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16889 struct objfile *objfile = per_objfile->objfile;
16890 bfd *abfd = objfile->obfd.get ();
16891 ULONGEST loclist_header_size =
16892 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
16893 : LOCLIST_HEADER_SIZE64);
16894 ULONGEST loclist_base = lookup_loclist_base (cu);
16895
16896 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
16897 ULONGEST start_offset =
16898 loclist_base + loclist_index * cu->header.offset_size;
16899
16900 /* Get loclists section. */
16901 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16902
16903 /* Read the loclists section content. */
16904 section->read (objfile);
16905 if (section->buffer == NULL)
16906 error (_("DW_FORM_loclistx used without .debug_loclists "
16907 "section [in module %s]"), objfile_name (objfile));
16908
16909 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
16910 so if loclist_base is smaller than the header size, we have a problem. */
16911 if (loclist_base < loclist_header_size)
16912 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
16913 objfile_name (objfile));
16914
16915 /* Read the header of the loclists contribution. */
16916 struct loclists_rnglists_header header;
16917 read_loclists_rnglists_header (&header, section,
16918 (sect_offset) (loclist_base - loclist_header_size));
16919
16920 /* Verify the loclist index is valid. */
16921 if (loclist_index >= header.offset_entry_count)
16922 error (_("DW_FORM_loclistx pointing outside of "
16923 ".debug_loclists offset array [in module %s]"),
16924 objfile_name (objfile));
16925
16926 /* Validate that reading won't go beyond the end of the section. */
16927 if (start_offset + cu->header.offset_size > section->size)
16928 error (_("Reading DW_FORM_loclistx index beyond end of"
16929 ".debug_loclists section [in module %s]"),
16930 objfile_name (objfile));
16931
16932 const gdb_byte *info_ptr = section->buffer + start_offset;
16933
16934 if (cu->header.offset_size == 4)
16935 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
16936 else
16937 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
16938 }
16939
16940 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
16941 array of offsets in the .debug_rnglists section. */
16942
16943 static sect_offset
16944 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
16945 dwarf_tag tag)
16946 {
16947 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
16948 struct objfile *objfile = dwarf2_per_objfile->objfile;
16949 bfd *abfd = objfile->obfd.get ();
16950 ULONGEST rnglist_header_size =
16951 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
16952 : RNGLIST_HEADER_SIZE64);
16953
16954 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
16955 .debug_rnglists.dwo section. The rnglists base given in the skeleton
16956 doesn't apply. */
16957 ULONGEST rnglist_base =
16958 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
16959
16960 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
16961 ULONGEST start_offset =
16962 rnglist_base + rnglist_index * cu->header.offset_size;
16963
16964 /* Get rnglists section. */
16965 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
16966
16967 /* Read the rnglists section content. */
16968 section->read (objfile);
16969 if (section->buffer == nullptr)
16970 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
16971 "[in module %s]"),
16972 objfile_name (objfile));
16973
16974 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
16975 so if rnglist_base is smaller than the header size, we have a problem. */
16976 if (rnglist_base < rnglist_header_size)
16977 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
16978 objfile_name (objfile));
16979
16980 /* Read the header of the rnglists contribution. */
16981 struct loclists_rnglists_header header;
16982 read_loclists_rnglists_header (&header, section,
16983 (sect_offset) (rnglist_base - rnglist_header_size));
16984
16985 /* Verify the rnglist index is valid. */
16986 if (rnglist_index >= header.offset_entry_count)
16987 error (_("DW_FORM_rnglistx index pointing outside of "
16988 ".debug_rnglists offset array [in module %s]"),
16989 objfile_name (objfile));
16990
16991 /* Validate that reading won't go beyond the end of the section. */
16992 if (start_offset + cu->header.offset_size > section->size)
16993 error (_("Reading DW_FORM_rnglistx index beyond end of"
16994 ".debug_rnglists section [in module %s]"),
16995 objfile_name (objfile));
16996
16997 const gdb_byte *info_ptr = section->buffer + start_offset;
16998
16999 if (cu->header.offset_size == 4)
17000 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
17001 else
17002 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
17003 }
17004
17005 /* Process the attributes that had to be skipped in the first round. These
17006 attributes are the ones that need str_offsets_base or addr_base attributes.
17007 They could not have been processed in the first round, because at the time
17008 the values of str_offsets_base or addr_base may not have been known. */
17009 static void
17010 read_attribute_reprocess (const struct die_reader_specs *reader,
17011 struct attribute *attr, dwarf_tag tag)
17012 {
17013 struct dwarf2_cu *cu = reader->cu;
17014 switch (attr->form)
17015 {
17016 case DW_FORM_addrx:
17017 case DW_FORM_GNU_addr_index:
17018 attr->set_address (read_addr_index (cu,
17019 attr->as_unsigned_reprocess ()));
17020 break;
17021 case DW_FORM_loclistx:
17022 {
17023 sect_offset loclists_sect_off
17024 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
17025
17026 attr->set_unsigned (to_underlying (loclists_sect_off));
17027 }
17028 break;
17029 case DW_FORM_rnglistx:
17030 {
17031 sect_offset rnglists_sect_off
17032 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
17033
17034 attr->set_unsigned (to_underlying (rnglists_sect_off));
17035 }
17036 break;
17037 case DW_FORM_strx:
17038 case DW_FORM_strx1:
17039 case DW_FORM_strx2:
17040 case DW_FORM_strx3:
17041 case DW_FORM_strx4:
17042 case DW_FORM_GNU_str_index:
17043 {
17044 unsigned int str_index = attr->as_unsigned_reprocess ();
17045 gdb_assert (!attr->canonical_string_p ());
17046 if (reader->dwo_file != NULL)
17047 attr->set_string_noncanonical (read_dwo_str_index (reader,
17048 str_index));
17049 else
17050 attr->set_string_noncanonical (read_stub_str_index (cu,
17051 str_index));
17052 break;
17053 }
17054 default:
17055 gdb_assert_not_reached ("Unexpected DWARF form.");
17056 }
17057 }
17058
17059 /* Read an attribute value described by an attribute form. */
17060
17061 static const gdb_byte *
17062 read_attribute_value (const struct die_reader_specs *reader,
17063 struct attribute *attr, unsigned form,
17064 LONGEST implicit_const, const gdb_byte *info_ptr,
17065 bool allow_reprocess)
17066 {
17067 struct dwarf2_cu *cu = reader->cu;
17068 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17069 struct objfile *objfile = per_objfile->objfile;
17070 bfd *abfd = reader->abfd;
17071 struct comp_unit_head *cu_header = &cu->header;
17072 unsigned int bytes_read;
17073 struct dwarf_block *blk;
17074
17075 attr->form = (enum dwarf_form) form;
17076 switch (form)
17077 {
17078 case DW_FORM_ref_addr:
17079 if (cu_header->version == 2)
17080 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
17081 &bytes_read));
17082 else
17083 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17084 &bytes_read));
17085 info_ptr += bytes_read;
17086 break;
17087 case DW_FORM_GNU_ref_alt:
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_addr:
17093 {
17094 struct gdbarch *gdbarch = objfile->arch ();
17095 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
17096 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
17097 attr->set_address (addr);
17098 info_ptr += bytes_read;
17099 }
17100 break;
17101 case DW_FORM_block2:
17102 blk = dwarf_alloc_block (cu);
17103 blk->size = read_2_bytes (abfd, info_ptr);
17104 info_ptr += 2;
17105 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17106 info_ptr += blk->size;
17107 attr->set_block (blk);
17108 break;
17109 case DW_FORM_block4:
17110 blk = dwarf_alloc_block (cu);
17111 blk->size = read_4_bytes (abfd, info_ptr);
17112 info_ptr += 4;
17113 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17114 info_ptr += blk->size;
17115 attr->set_block (blk);
17116 break;
17117 case DW_FORM_data2:
17118 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
17119 info_ptr += 2;
17120 break;
17121 case DW_FORM_data4:
17122 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
17123 info_ptr += 4;
17124 break;
17125 case DW_FORM_data8:
17126 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
17127 info_ptr += 8;
17128 break;
17129 case DW_FORM_data16:
17130 blk = dwarf_alloc_block (cu);
17131 blk->size = 16;
17132 blk->data = read_n_bytes (abfd, info_ptr, 16);
17133 info_ptr += 16;
17134 attr->set_block (blk);
17135 break;
17136 case DW_FORM_sec_offset:
17137 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
17138 &bytes_read));
17139 info_ptr += bytes_read;
17140 break;
17141 case DW_FORM_loclistx:
17142 {
17143 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17144 &bytes_read));
17145 info_ptr += bytes_read;
17146 if (allow_reprocess)
17147 read_attribute_reprocess (reader, attr);
17148 }
17149 break;
17150 case DW_FORM_string:
17151 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
17152 &bytes_read));
17153 info_ptr += bytes_read;
17154 break;
17155 case DW_FORM_strp:
17156 if (!cu->per_cu->is_dwz)
17157 {
17158 attr->set_string_noncanonical
17159 (read_indirect_string (per_objfile,
17160 abfd, info_ptr, cu_header,
17161 &bytes_read));
17162 info_ptr += bytes_read;
17163 break;
17164 }
17165 /* FALLTHROUGH */
17166 case DW_FORM_line_strp:
17167 if (!cu->per_cu->is_dwz)
17168 {
17169 attr->set_string_noncanonical
17170 (per_objfile->read_line_string (info_ptr, cu_header,
17171 &bytes_read));
17172 info_ptr += bytes_read;
17173 break;
17174 }
17175 /* FALLTHROUGH */
17176 case DW_FORM_GNU_strp_alt:
17177 {
17178 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17179 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
17180 &bytes_read);
17181
17182 attr->set_string_noncanonical
17183 (dwz->read_string (objfile, str_offset));
17184 info_ptr += bytes_read;
17185 }
17186 break;
17187 case DW_FORM_exprloc:
17188 case DW_FORM_block:
17189 blk = dwarf_alloc_block (cu);
17190 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17191 info_ptr += bytes_read;
17192 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17193 info_ptr += blk->size;
17194 attr->set_block (blk);
17195 break;
17196 case DW_FORM_block1:
17197 blk = dwarf_alloc_block (cu);
17198 blk->size = read_1_byte (abfd, info_ptr);
17199 info_ptr += 1;
17200 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
17201 info_ptr += blk->size;
17202 attr->set_block (blk);
17203 break;
17204 case DW_FORM_data1:
17205 case DW_FORM_flag:
17206 attr->set_unsigned (read_1_byte (abfd, info_ptr));
17207 info_ptr += 1;
17208 break;
17209 case DW_FORM_flag_present:
17210 attr->set_unsigned (1);
17211 break;
17212 case DW_FORM_sdata:
17213 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
17214 info_ptr += bytes_read;
17215 break;
17216 case DW_FORM_rnglistx:
17217 {
17218 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17219 &bytes_read));
17220 info_ptr += bytes_read;
17221 if (allow_reprocess)
17222 read_attribute_reprocess (reader, attr);
17223 }
17224 break;
17225 case DW_FORM_udata:
17226 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
17227 info_ptr += bytes_read;
17228 break;
17229 case DW_FORM_ref1:
17230 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17231 + read_1_byte (abfd, info_ptr)));
17232 info_ptr += 1;
17233 break;
17234 case DW_FORM_ref2:
17235 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17236 + read_2_bytes (abfd, info_ptr)));
17237 info_ptr += 2;
17238 break;
17239 case DW_FORM_ref4:
17240 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17241 + read_4_bytes (abfd, info_ptr)));
17242 info_ptr += 4;
17243 break;
17244 case DW_FORM_ref8:
17245 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17246 + read_8_bytes (abfd, info_ptr)));
17247 info_ptr += 8;
17248 break;
17249 case DW_FORM_ref_sig8:
17250 attr->set_signature (read_8_bytes (abfd, info_ptr));
17251 info_ptr += 8;
17252 break;
17253 case DW_FORM_ref_udata:
17254 attr->set_unsigned ((to_underlying (cu_header->sect_off)
17255 + read_unsigned_leb128 (abfd, info_ptr,
17256 &bytes_read)));
17257 info_ptr += bytes_read;
17258 break;
17259 case DW_FORM_indirect:
17260 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17261 info_ptr += bytes_read;
17262 if (form == DW_FORM_implicit_const)
17263 {
17264 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
17265 info_ptr += bytes_read;
17266 }
17267 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
17268 info_ptr, allow_reprocess);
17269 break;
17270 case DW_FORM_implicit_const:
17271 attr->set_signed (implicit_const);
17272 break;
17273 case DW_FORM_addrx:
17274 case DW_FORM_GNU_addr_index:
17275 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
17276 &bytes_read));
17277 info_ptr += bytes_read;
17278 if (allow_reprocess)
17279 read_attribute_reprocess (reader, attr);
17280 break;
17281 case DW_FORM_strx:
17282 case DW_FORM_strx1:
17283 case DW_FORM_strx2:
17284 case DW_FORM_strx3:
17285 case DW_FORM_strx4:
17286 case DW_FORM_GNU_str_index:
17287 {
17288 ULONGEST str_index;
17289 if (form == DW_FORM_strx1)
17290 {
17291 str_index = read_1_byte (abfd, info_ptr);
17292 info_ptr += 1;
17293 }
17294 else if (form == DW_FORM_strx2)
17295 {
17296 str_index = read_2_bytes (abfd, info_ptr);
17297 info_ptr += 2;
17298 }
17299 else if (form == DW_FORM_strx3)
17300 {
17301 str_index = read_3_bytes (abfd, info_ptr);
17302 info_ptr += 3;
17303 }
17304 else if (form == DW_FORM_strx4)
17305 {
17306 str_index = read_4_bytes (abfd, info_ptr);
17307 info_ptr += 4;
17308 }
17309 else
17310 {
17311 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17312 info_ptr += bytes_read;
17313 }
17314 attr->set_unsigned_reprocess (str_index);
17315 if (allow_reprocess)
17316 read_attribute_reprocess (reader, attr);
17317 }
17318 break;
17319 default:
17320 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17321 dwarf_form_name (form),
17322 bfd_get_filename (abfd));
17323 }
17324
17325 /* Super hack. */
17326 if (cu->per_cu->is_dwz && attr->form_is_ref ())
17327 attr->form = DW_FORM_GNU_ref_alt;
17328
17329 /* We have seen instances where the compiler tried to emit a byte
17330 size attribute of -1 which ended up being encoded as an unsigned
17331 0xffffffff. Although 0xffffffff is technically a valid size value,
17332 an object of this size seems pretty unlikely so we can relatively
17333 safely treat these cases as if the size attribute was invalid and
17334 treat them as zero by default. */
17335 if (attr->name == DW_AT_byte_size
17336 && form == DW_FORM_data4
17337 && attr->as_unsigned () >= 0xffffffff)
17338 {
17339 complaint
17340 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17341 hex_string (attr->as_unsigned ()));
17342 attr->set_unsigned (0);
17343 }
17344
17345 return info_ptr;
17346 }
17347
17348 /* Read an attribute described by an abbreviated attribute. */
17349
17350 static const gdb_byte *
17351 read_attribute (const struct die_reader_specs *reader,
17352 struct attribute *attr, const struct attr_abbrev *abbrev,
17353 const gdb_byte *info_ptr,
17354 bool allow_reprocess)
17355 {
17356 attr->name = abbrev->name;
17357 attr->string_is_canonical = 0;
17358 return read_attribute_value (reader, attr, abbrev->form,
17359 abbrev->implicit_const, info_ptr,
17360 allow_reprocess);
17361 }
17362
17363 /* See read.h. */
17364
17365 const char *
17366 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
17367 LONGEST str_offset)
17368 {
17369 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
17370 str_offset, "DW_FORM_strp");
17371 }
17372
17373 /* Return pointer to string at .debug_str offset as read from BUF.
17374 BUF is assumed to be in a compilation unit described by CU_HEADER.
17375 Return *BYTES_READ_PTR count of bytes read from BUF. */
17376
17377 static const char *
17378 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
17379 const gdb_byte *buf,
17380 const struct comp_unit_head *cu_header,
17381 unsigned int *bytes_read_ptr)
17382 {
17383 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17384
17385 return read_indirect_string_at_offset (per_objfile, str_offset);
17386 }
17387
17388 /* See read.h. */
17389
17390 const char *
17391 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17392 unsigned int offset_size)
17393 {
17394 bfd *abfd = objfile->obfd.get ();
17395 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
17396
17397 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17398 }
17399
17400 /* See read.h. */
17401
17402 const char *
17403 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
17404 const struct comp_unit_head *cu_header,
17405 unsigned int *bytes_read_ptr)
17406 {
17407 bfd *abfd = objfile->obfd.get ();
17408 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
17409
17410 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
17411 }
17412
17413 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17414 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
17415 ADDR_SIZE is the size of addresses from the CU header. */
17416
17417 static CORE_ADDR
17418 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
17419 gdb::optional<ULONGEST> addr_base, int addr_size)
17420 {
17421 struct objfile *objfile = per_objfile->objfile;
17422 bfd *abfd = objfile->obfd.get ();
17423 const gdb_byte *info_ptr;
17424 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
17425
17426 per_objfile->per_bfd->addr.read (objfile);
17427 if (per_objfile->per_bfd->addr.buffer == NULL)
17428 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17429 objfile_name (objfile));
17430 if (addr_base_or_zero + addr_index * addr_size
17431 >= per_objfile->per_bfd->addr.size)
17432 error (_("DW_FORM_addr_index pointing outside of "
17433 ".debug_addr section [in module %s]"),
17434 objfile_name (objfile));
17435 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
17436 + addr_index * addr_size);
17437 if (addr_size == 4)
17438 return bfd_get_32 (abfd, info_ptr);
17439 else
17440 return bfd_get_64 (abfd, info_ptr);
17441 }
17442
17443 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17444
17445 static CORE_ADDR
17446 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17447 {
17448 return read_addr_index_1 (cu->per_objfile, addr_index,
17449 cu->addr_base, cu->header.addr_size);
17450 }
17451
17452 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17453
17454 static CORE_ADDR
17455 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17456 unsigned int *bytes_read)
17457 {
17458 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
17459 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17460
17461 return read_addr_index (cu, addr_index);
17462 }
17463
17464 /* See read.h. */
17465
17466 CORE_ADDR
17467 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
17468 dwarf2_per_objfile *per_objfile,
17469 unsigned int addr_index)
17470 {
17471 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
17472 gdb::optional<ULONGEST> addr_base;
17473 int addr_size;
17474
17475 /* We need addr_base and addr_size.
17476 If we don't have PER_CU->cu, we have to get it.
17477 Nasty, but the alternative is storing the needed info in PER_CU,
17478 which at this point doesn't seem justified: it's not clear how frequently
17479 it would get used and it would increase the size of every PER_CU.
17480 Entry points like dwarf2_per_cu_addr_size do a similar thing
17481 so we're not in uncharted territory here.
17482 Alas we need to be a bit more complicated as addr_base is contained
17483 in the DIE.
17484
17485 We don't need to read the entire CU(/TU).
17486 We just need the header and top level die.
17487
17488 IWBN to use the aging mechanism to let us lazily later discard the CU.
17489 For now we skip this optimization. */
17490
17491 if (cu != NULL)
17492 {
17493 addr_base = cu->addr_base;
17494 addr_size = cu->header.addr_size;
17495 }
17496 else
17497 {
17498 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
17499 addr_base = reader.cu->addr_base;
17500 addr_size = reader.cu->header.addr_size;
17501 }
17502
17503 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
17504 }
17505
17506 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
17507 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
17508 DWO file. */
17509
17510 static const char *
17511 read_str_index (struct dwarf2_cu *cu,
17512 struct dwarf2_section_info *str_section,
17513 struct dwarf2_section_info *str_offsets_section,
17514 ULONGEST str_offsets_base, ULONGEST str_index,
17515 unsigned offset_size)
17516 {
17517 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17518 struct objfile *objfile = per_objfile->objfile;
17519 const char *objf_name = objfile_name (objfile);
17520 bfd *abfd = objfile->obfd.get ();
17521 const gdb_byte *info_ptr;
17522 ULONGEST str_offset;
17523 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
17524
17525 str_section->read (objfile);
17526 str_offsets_section->read (objfile);
17527 if (str_section->buffer == NULL)
17528 error (_("%s used without %s section"
17529 " in CU at offset %s [in module %s]"),
17530 form_name, str_section->get_name (),
17531 sect_offset_str (cu->header.sect_off), objf_name);
17532 if (str_offsets_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 info_ptr = (str_offsets_section->buffer
17538 + str_offsets_base
17539 + str_index * offset_size);
17540 if (offset_size == 4)
17541 str_offset = bfd_get_32 (abfd, info_ptr);
17542 else
17543 str_offset = bfd_get_64 (abfd, info_ptr);
17544 if (str_offset >= str_section->size)
17545 error (_("Offset from %s pointing outside of"
17546 " .debug_str.dwo section in CU at offset %s [in module %s]"),
17547 form_name, sect_offset_str (cu->header.sect_off), objf_name);
17548 return (const char *) (str_section->buffer + str_offset);
17549 }
17550
17551 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
17552
17553 static const char *
17554 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17555 {
17556 unsigned offset_size;
17557 ULONGEST str_offsets_base;
17558 if (reader->cu->header.version >= 5)
17559 {
17560 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
17561 so assume the .debug_str_offsets section is DWARF5 as well, and
17562 parse the header. FIXME: Parse the header only once. */
17563 unsigned int bytes_read = 0;
17564 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
17565 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
17566
17567 /* Header: Initial length. */
17568 read_initial_length (abfd, p + bytes_read, &bytes_read);
17569
17570 /* Determine offset_size based on the .debug_str_offsets header. */
17571 const bool dwarf5_is_dwarf64 = bytes_read != 4;
17572 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
17573
17574 /* Header: Version. */
17575 unsigned version = read_2_bytes (abfd, p + bytes_read);
17576 bytes_read += 2;
17577
17578 if (version <= 4)
17579 {
17580 /* We'd like one warning here about ignoring the section, but
17581 because we parse the header more than once (see FIXME above)
17582 we'd have many warnings, so use a complaint instead, which at
17583 least has a limit. */
17584 complaint (_("Section .debug_str_offsets in %s has unsupported"
17585 " version %d, use empty string."),
17586 reader->dwo_file->dwo_name, version);
17587 return "";
17588 }
17589
17590 /* Header: Padding. */
17591 bytes_read += 2;
17592
17593 str_offsets_base = bytes_read;
17594 }
17595 else
17596 {
17597 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
17598 section, assume the .debug_str_offsets section is pre-DWARF5 as
17599 well, which doesn't have a header. */
17600 str_offsets_base = 0;
17601
17602 /* Determine offset_size based on the .debug_info header. */
17603 offset_size = reader->cu->header.offset_size;
17604 }
17605
17606 return read_str_index (reader->cu,
17607 &reader->dwo_file->sections.str,
17608 &reader->dwo_file->sections.str_offsets,
17609 str_offsets_base, str_index, offset_size);
17610 }
17611
17612 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
17613
17614 static const char *
17615 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
17616 {
17617 struct objfile *objfile = cu->per_objfile->objfile;
17618 const char *objf_name = objfile_name (objfile);
17619 static const char form_name[] = "DW_FORM_GNU_str_index";
17620 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
17621
17622 if (!cu->str_offsets_base.has_value ())
17623 error (_("%s used in Fission stub without %s"
17624 " in CU at offset 0x%lx [in module %s]"),
17625 form_name, str_offsets_attr_name,
17626 (long) cu->header.offset_size, objf_name);
17627
17628 return read_str_index (cu,
17629 &cu->per_objfile->per_bfd->str,
17630 &cu->per_objfile->per_bfd->str_offsets,
17631 *cu->str_offsets_base, str_index,
17632 cu->header.offset_size);
17633 }
17634
17635 /* Return the length of an LEB128 number in BUF. */
17636
17637 static int
17638 leb128_size (const gdb_byte *buf)
17639 {
17640 const gdb_byte *begin = buf;
17641 gdb_byte byte;
17642
17643 while (1)
17644 {
17645 byte = *buf++;
17646 if ((byte & 128) == 0)
17647 return buf - begin;
17648 }
17649 }
17650
17651 static enum language
17652 dwarf_lang_to_enum_language (unsigned int lang)
17653 {
17654 enum language language;
17655
17656 switch (lang)
17657 {
17658 case DW_LANG_C89:
17659 case DW_LANG_C99:
17660 case DW_LANG_C11:
17661 case DW_LANG_C:
17662 case DW_LANG_UPC:
17663 language = language_c;
17664 break;
17665 case DW_LANG_Java:
17666 case DW_LANG_C_plus_plus:
17667 case DW_LANG_C_plus_plus_11:
17668 case DW_LANG_C_plus_plus_14:
17669 language = language_cplus;
17670 break;
17671 case DW_LANG_D:
17672 language = language_d;
17673 break;
17674 case DW_LANG_Fortran77:
17675 case DW_LANG_Fortran90:
17676 case DW_LANG_Fortran95:
17677 case DW_LANG_Fortran03:
17678 case DW_LANG_Fortran08:
17679 language = language_fortran;
17680 break;
17681 case DW_LANG_Go:
17682 language = language_go;
17683 break;
17684 case DW_LANG_Mips_Assembler:
17685 language = language_asm;
17686 break;
17687 case DW_LANG_Ada83:
17688 case DW_LANG_Ada95:
17689 language = language_ada;
17690 break;
17691 case DW_LANG_Modula2:
17692 language = language_m2;
17693 break;
17694 case DW_LANG_Pascal83:
17695 language = language_pascal;
17696 break;
17697 case DW_LANG_ObjC:
17698 language = language_objc;
17699 break;
17700 case DW_LANG_Rust:
17701 case DW_LANG_Rust_old:
17702 language = language_rust;
17703 break;
17704 case DW_LANG_OpenCL:
17705 language = language_opencl;
17706 break;
17707 case DW_LANG_Cobol74:
17708 case DW_LANG_Cobol85:
17709 default:
17710 language = language_minimal;
17711 break;
17712 }
17713
17714 return language;
17715 }
17716
17717 /* Return the named attribute or NULL if not there. */
17718
17719 static struct attribute *
17720 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17721 {
17722 for (;;)
17723 {
17724 unsigned int i;
17725 struct attribute *spec = NULL;
17726
17727 for (i = 0; i < die->num_attrs; ++i)
17728 {
17729 if (die->attrs[i].name == name)
17730 return &die->attrs[i];
17731 if (die->attrs[i].name == DW_AT_specification
17732 || die->attrs[i].name == DW_AT_abstract_origin)
17733 spec = &die->attrs[i];
17734 }
17735
17736 if (!spec)
17737 break;
17738
17739 die = follow_die_ref (die, spec, &cu);
17740 }
17741
17742 return NULL;
17743 }
17744
17745 /* Return the string associated with a string-typed attribute, or NULL if it
17746 is either not found or is of an incorrect type. */
17747
17748 static const char *
17749 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17750 {
17751 struct attribute *attr;
17752 const char *str = NULL;
17753
17754 attr = dwarf2_attr (die, name, cu);
17755
17756 if (attr != NULL)
17757 {
17758 str = attr->as_string ();
17759 if (str == nullptr)
17760 complaint (_("string type expected for attribute %s for "
17761 "DIE at %s in module %s"),
17762 dwarf_attr_name (name), sect_offset_str (die->sect_off),
17763 objfile_name (cu->per_objfile->objfile));
17764 }
17765
17766 return str;
17767 }
17768
17769 /* Return the dwo name or NULL if not present. If present, it is in either
17770 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
17771 static const char *
17772 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
17773 {
17774 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
17775 if (dwo_name == nullptr)
17776 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
17777 return dwo_name;
17778 }
17779
17780 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17781 and holds a non-zero value. This function should only be used for
17782 DW_FORM_flag or DW_FORM_flag_present attributes. */
17783
17784 static int
17785 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17786 {
17787 struct attribute *attr = dwarf2_attr (die, name, cu);
17788
17789 return attr != nullptr && attr->as_boolean ();
17790 }
17791
17792 static int
17793 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17794 {
17795 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17796 which value is non-zero. However, we have to be careful with
17797 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17798 (via dwarf2_flag_true_p) follows this attribute. So we may
17799 end up accidently finding a declaration attribute that belongs
17800 to a different DIE referenced by the specification attribute,
17801 even though the given DIE does not have a declaration attribute. */
17802 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17803 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17804 }
17805
17806 /* Return the die giving the specification for DIE, if there is
17807 one. *SPEC_CU is the CU containing DIE on input, and the CU
17808 containing the return value on output. If there is no
17809 specification, but there is an abstract origin, that is
17810 returned. */
17811
17812 static struct die_info *
17813 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17814 {
17815 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17816 *spec_cu);
17817
17818 if (spec_attr == NULL)
17819 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17820
17821 if (spec_attr == NULL)
17822 return NULL;
17823 else
17824 return follow_die_ref (die, spec_attr, spec_cu);
17825 }
17826
17827 /* A convenience function to find the proper .debug_line section for a CU. */
17828
17829 static struct dwarf2_section_info *
17830 get_debug_line_section (struct dwarf2_cu *cu)
17831 {
17832 struct dwarf2_section_info *section;
17833 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17834
17835 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17836 DWO file. */
17837 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17838 section = &cu->dwo_unit->dwo_file->sections.line;
17839 else if (cu->per_cu->is_dwz)
17840 {
17841 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17842
17843 section = &dwz->line;
17844 }
17845 else
17846 section = &per_objfile->per_bfd->line;
17847
17848 return section;
17849 }
17850
17851 /* Read the statement program header starting at OFFSET in
17852 .debug_line, or .debug_line.dwo. Return a pointer
17853 to a struct line_header, allocated using xmalloc.
17854 Returns NULL if there is a problem reading the header, e.g., if it
17855 has a version we don't understand.
17856
17857 NOTE: the strings in the include directory and file name tables of
17858 the returned object point into the dwarf line section buffer,
17859 and must not be freed. */
17860
17861 static line_header_up
17862 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
17863 const char *comp_dir)
17864 {
17865 struct dwarf2_section_info *section;
17866 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17867
17868 section = get_debug_line_section (cu);
17869 section->read (per_objfile->objfile);
17870 if (section->buffer == NULL)
17871 {
17872 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17873 complaint (_("missing .debug_line.dwo section"));
17874 else
17875 complaint (_("missing .debug_line section"));
17876 return 0;
17877 }
17878
17879 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
17880 per_objfile, section, &cu->header,
17881 comp_dir);
17882 }
17883
17884 /* Subroutine of dwarf_decode_lines to simplify it.
17885 Return the file name for the given file_entry.
17886 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
17887 If space for the result is malloc'd, *NAME_HOLDER will be set.
17888 Returns NULL if FILE_INDEX should be ignored, i.e., it is
17889 equivalent to CU_INFO. */
17890
17891 static const char *
17892 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
17893 const file_and_directory &cu_info,
17894 std::string &name_holder)
17895 {
17896 const char *include_name = fe.name;
17897 const char *include_name_to_compare = include_name;
17898
17899 const char *dir_name = fe.include_dir (lh);
17900
17901 std::string hold_compare;
17902 if (!IS_ABSOLUTE_PATH (include_name)
17903 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
17904 {
17905 /* Avoid creating a duplicate name for CU_INFO.
17906 We do this by comparing INCLUDE_NAME and CU_INFO.
17907 Before we do the comparison, however, we need to account
17908 for DIR_NAME and COMP_DIR.
17909 First prepend dir_name (if non-NULL). If we still don't
17910 have an absolute path prepend comp_dir (if non-NULL).
17911 However, the directory we record in the include-file's
17912 psymtab does not contain COMP_DIR (to match the
17913 corresponding symtab(s)).
17914
17915 Example:
17916
17917 bash$ cd /tmp
17918 bash$ gcc -g ./hello.c
17919 include_name = "hello.c"
17920 dir_name = "."
17921 DW_AT_comp_dir = comp_dir = "/tmp"
17922 DW_AT_name = "./hello.c"
17923
17924 */
17925
17926 if (dir_name != NULL)
17927 {
17928 name_holder = path_join (dir_name, include_name);
17929 include_name = name_holder.c_str ();
17930 include_name_to_compare = include_name;
17931 }
17932 if (!IS_ABSOLUTE_PATH (include_name)
17933 && cu_info.get_comp_dir () != nullptr)
17934 {
17935 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
17936 include_name_to_compare = hold_compare.c_str ();
17937 }
17938 }
17939
17940 std::string copied_name;
17941 const char *cu_filename = cu_info.get_name ();
17942 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
17943 {
17944 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
17945 cu_filename = copied_name.c_str ();
17946 }
17947
17948 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
17949 return nullptr;
17950 return include_name;
17951 }
17952
17953 /* State machine to track the state of the line number program. */
17954
17955 class lnp_state_machine
17956 {
17957 public:
17958 /* Initialize a machine state for the start of a line number
17959 program. */
17960 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
17961
17962 file_entry *current_file ()
17963 {
17964 /* lh->file_names is 0-based, but the file name numbers in the
17965 statement program are 1-based. */
17966 return m_line_header->file_name_at (m_file);
17967 }
17968
17969 /* Record the line in the state machine. END_SEQUENCE is true if
17970 we're processing the end of a sequence. */
17971 void record_line (bool end_sequence);
17972
17973 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
17974 nop-out rest of the lines in this sequence. */
17975 void check_line_address (struct dwarf2_cu *cu,
17976 const gdb_byte *line_ptr,
17977 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
17978
17979 void handle_set_discriminator (unsigned int discriminator)
17980 {
17981 m_discriminator = discriminator;
17982 m_line_has_non_zero_discriminator |= discriminator != 0;
17983 }
17984
17985 /* Handle DW_LNE_set_address. */
17986 void handle_set_address (CORE_ADDR address)
17987 {
17988 m_op_index = 0;
17989 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
17990 }
17991
17992 /* Handle DW_LNS_advance_pc. */
17993 void handle_advance_pc (CORE_ADDR adjust);
17994
17995 /* Handle a special opcode. */
17996 void handle_special_opcode (unsigned char op_code);
17997
17998 /* Handle DW_LNS_advance_line. */
17999 void handle_advance_line (int line_delta)
18000 {
18001 advance_line (line_delta);
18002 }
18003
18004 /* Handle DW_LNS_set_file. */
18005 void handle_set_file (file_name_index file);
18006
18007 /* Handle DW_LNS_negate_stmt. */
18008 void handle_negate_stmt ()
18009 {
18010 m_flags ^= LEF_IS_STMT;
18011 }
18012
18013 /* Handle DW_LNS_const_add_pc. */
18014 void handle_const_add_pc ();
18015
18016 /* Handle DW_LNS_fixed_advance_pc. */
18017 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
18018 {
18019 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18020 m_op_index = 0;
18021 }
18022
18023 /* Handle DW_LNS_copy. */
18024 void handle_copy ()
18025 {
18026 record_line (false);
18027 m_discriminator = 0;
18028 m_flags &= ~LEF_PROLOGUE_END;
18029 }
18030
18031 /* Handle DW_LNE_end_sequence. */
18032 void handle_end_sequence ()
18033 {
18034 m_currently_recording_lines = true;
18035 }
18036
18037 /* Handle DW_LNS_set_prologue_end. */
18038 void handle_set_prologue_end ()
18039 {
18040 m_flags |= LEF_PROLOGUE_END;
18041 }
18042
18043 private:
18044 /* Advance the line by LINE_DELTA. */
18045 void advance_line (int line_delta)
18046 {
18047 m_line += line_delta;
18048
18049 if (line_delta != 0)
18050 m_line_has_non_zero_discriminator = m_discriminator != 0;
18051 }
18052
18053 struct dwarf2_cu *m_cu;
18054
18055 gdbarch *m_gdbarch;
18056
18057 /* The line number header. */
18058 line_header *m_line_header;
18059
18060 /* These are part of the standard DWARF line number state machine,
18061 and initialized according to the DWARF spec. */
18062
18063 unsigned char m_op_index = 0;
18064 /* The line table index of the current file. */
18065 file_name_index m_file = 1;
18066 unsigned int m_line = 1;
18067
18068 /* These are initialized in the constructor. */
18069
18070 CORE_ADDR m_address;
18071 linetable_entry_flags m_flags;
18072 unsigned int m_discriminator = 0;
18073
18074 /* Additional bits of state we need to track. */
18075
18076 /* The last file that we called dwarf2_start_subfile for.
18077 This is only used for TLLs. */
18078 unsigned int m_last_file = 0;
18079 /* The last file a line number was recorded for. */
18080 struct subfile *m_last_subfile = NULL;
18081
18082 /* The address of the last line entry. */
18083 CORE_ADDR m_last_address;
18084
18085 /* Set to true when a previous line at the same address (using
18086 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
18087 when a line entry at a new address (m_address different to
18088 m_last_address) is processed. */
18089 bool m_stmt_at_address = false;
18090
18091 /* When true, record the lines we decode. */
18092 bool m_currently_recording_lines = true;
18093
18094 /* The last line number that was recorded, used to coalesce
18095 consecutive entries for the same line. This can happen, for
18096 example, when discriminators are present. PR 17276. */
18097 unsigned int m_last_line = 0;
18098 bool m_line_has_non_zero_discriminator = false;
18099 };
18100
18101 void
18102 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
18103 {
18104 CORE_ADDR addr_adj = (((m_op_index + adjust)
18105 / m_line_header->maximum_ops_per_instruction)
18106 * m_line_header->minimum_instruction_length);
18107 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18108 m_op_index = ((m_op_index + adjust)
18109 % m_line_header->maximum_ops_per_instruction);
18110 }
18111
18112 void
18113 lnp_state_machine::handle_special_opcode (unsigned char op_code)
18114 {
18115 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
18116 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
18117 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
18118 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
18119 / m_line_header->maximum_ops_per_instruction)
18120 * m_line_header->minimum_instruction_length);
18121 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18122 m_op_index = ((m_op_index + adj_opcode_d)
18123 % m_line_header->maximum_ops_per_instruction);
18124
18125 int line_delta = m_line_header->line_base + adj_opcode_r;
18126 advance_line (line_delta);
18127 record_line (false);
18128 m_discriminator = 0;
18129 m_flags &= ~LEF_PROLOGUE_END;
18130 }
18131
18132 void
18133 lnp_state_machine::handle_set_file (file_name_index file)
18134 {
18135 m_file = file;
18136
18137 const file_entry *fe = current_file ();
18138 if (fe == NULL)
18139 dwarf2_debug_line_missing_file_complaint ();
18140 else
18141 {
18142 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18143 m_line_has_non_zero_discriminator = m_discriminator != 0;
18144 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
18145 }
18146 }
18147
18148 void
18149 lnp_state_machine::handle_const_add_pc ()
18150 {
18151 CORE_ADDR adjust
18152 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
18153
18154 CORE_ADDR addr_adj
18155 = (((m_op_index + adjust)
18156 / m_line_header->maximum_ops_per_instruction)
18157 * m_line_header->minimum_instruction_length);
18158
18159 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
18160 m_op_index = ((m_op_index + adjust)
18161 % m_line_header->maximum_ops_per_instruction);
18162 }
18163
18164 /* Return non-zero if we should add LINE to the line number table.
18165 LINE is the line to add, LAST_LINE is the last line that was added,
18166 LAST_SUBFILE is the subfile for LAST_LINE.
18167 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18168 had a non-zero discriminator.
18169
18170 We have to be careful in the presence of discriminators.
18171 E.g., for this line:
18172
18173 for (i = 0; i < 100000; i++);
18174
18175 clang can emit four line number entries for that one line,
18176 each with a different discriminator.
18177 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18178
18179 However, we want gdb to coalesce all four entries into one.
18180 Otherwise the user could stepi into the middle of the line and
18181 gdb would get confused about whether the pc really was in the
18182 middle of the line.
18183
18184 Things are further complicated by the fact that two consecutive
18185 line number entries for the same line is a heuristic used by gcc
18186 to denote the end of the prologue. So we can't just discard duplicate
18187 entries, we have to be selective about it. The heuristic we use is
18188 that we only collapse consecutive entries for the same line if at least
18189 one of those entries has a non-zero discriminator. PR 17276.
18190
18191 Note: Addresses in the line number state machine can never go backwards
18192 within one sequence, thus this coalescing is ok. */
18193
18194 static int
18195 dwarf_record_line_p (struct dwarf2_cu *cu,
18196 unsigned int line, unsigned int last_line,
18197 int line_has_non_zero_discriminator,
18198 struct subfile *last_subfile)
18199 {
18200 if (cu->get_builder ()->get_current_subfile () != last_subfile)
18201 return 1;
18202 if (line != last_line)
18203 return 1;
18204 /* Same line for the same file that we've seen already.
18205 As a last check, for pr 17276, only record the line if the line
18206 has never had a non-zero discriminator. */
18207 if (!line_has_non_zero_discriminator)
18208 return 1;
18209 return 0;
18210 }
18211
18212 /* Use the CU's builder to record line number LINE beginning at
18213 address ADDRESS in the line table of subfile SUBFILE. */
18214
18215 static void
18216 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18217 unsigned int line, CORE_ADDR address,
18218 linetable_entry_flags flags,
18219 struct dwarf2_cu *cu)
18220 {
18221 unrelocated_addr addr
18222 = unrelocated_addr (gdbarch_addr_bits_remove (gdbarch, address));
18223
18224 if (dwarf_line_debug)
18225 {
18226 gdb_printf (gdb_stdlog,
18227 "Recording line %u, file %s, address %s\n",
18228 line, lbasename (subfile->name.c_str ()),
18229 paddress (gdbarch, address));
18230 }
18231
18232 if (cu != nullptr)
18233 cu->get_builder ()->record_line (subfile, line, addr, flags);
18234 }
18235
18236 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18237 Mark the end of a set of line number records.
18238 The arguments are the same as for dwarf_record_line_1.
18239 If SUBFILE is NULL the request is ignored. */
18240
18241 static void
18242 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18243 CORE_ADDR address, struct dwarf2_cu *cu)
18244 {
18245 if (subfile == NULL)
18246 return;
18247
18248 if (dwarf_line_debug)
18249 {
18250 gdb_printf (gdb_stdlog,
18251 "Finishing current line, file %s, address %s\n",
18252 lbasename (subfile->name.c_str ()),
18253 paddress (gdbarch, address));
18254 }
18255
18256 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
18257 }
18258
18259 void
18260 lnp_state_machine::record_line (bool end_sequence)
18261 {
18262 if (dwarf_line_debug)
18263 {
18264 gdb_printf (gdb_stdlog,
18265 "Processing actual line %u: file %u,"
18266 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
18267 m_line, m_file,
18268 paddress (m_gdbarch, m_address),
18269 (m_flags & LEF_IS_STMT) != 0,
18270 (m_flags & LEF_PROLOGUE_END) != 0,
18271 m_discriminator,
18272 (end_sequence ? "\t(end sequence)" : ""));
18273 }
18274
18275 file_entry *fe = current_file ();
18276
18277 if (fe == NULL)
18278 dwarf2_debug_line_missing_file_complaint ();
18279 /* For now we ignore lines not starting on an instruction boundary.
18280 But not when processing end_sequence for compatibility with the
18281 previous version of the code. */
18282 else if (m_op_index == 0 || end_sequence)
18283 {
18284 /* When we switch files we insert an end maker in the first file,
18285 switch to the second file and add a new line entry. The
18286 problem is that the end marker inserted in the first file will
18287 discard any previous line entries at the same address. If the
18288 line entries in the first file are marked as is-stmt, while
18289 the new line in the second file is non-stmt, then this means
18290 the end marker will discard is-stmt lines so we can have a
18291 non-stmt line. This means that there are less addresses at
18292 which the user can insert a breakpoint.
18293
18294 To improve this we track the last address in m_last_address,
18295 and whether we have seen an is-stmt at this address. Then
18296 when switching files, if we have seen a stmt at the current
18297 address, and we are switching to create a non-stmt line, then
18298 discard the new line. */
18299 bool file_changed
18300 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
18301 bool ignore_this_line
18302 = ((file_changed && !end_sequence && m_last_address == m_address
18303 && ((m_flags & LEF_IS_STMT) == 0)
18304 && m_stmt_at_address)
18305 || (!end_sequence && m_line == 0));
18306
18307 if ((file_changed && !ignore_this_line) || end_sequence)
18308 {
18309 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
18310 m_currently_recording_lines ? m_cu : nullptr);
18311 }
18312
18313 if (!end_sequence && !ignore_this_line)
18314 {
18315 linetable_entry_flags lte_flags = m_flags;
18316 if (producer_is_codewarrior (m_cu))
18317 lte_flags |= LEF_IS_STMT;
18318
18319 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
18320 m_line_has_non_zero_discriminator,
18321 m_last_subfile))
18322 {
18323 buildsym_compunit *builder = m_cu->get_builder ();
18324 dwarf_record_line_1 (m_gdbarch,
18325 builder->get_current_subfile (),
18326 m_line, m_address, lte_flags,
18327 m_currently_recording_lines ? m_cu : nullptr);
18328 }
18329 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
18330 m_last_line = m_line;
18331 }
18332 }
18333
18334 /* Track whether we have seen any IS_STMT true at m_address in case we
18335 have multiple line table entries all at m_address. */
18336 if (m_last_address != m_address)
18337 {
18338 m_stmt_at_address = false;
18339 m_last_address = m_address;
18340 }
18341 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
18342 }
18343
18344 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
18345 line_header *lh)
18346 : m_cu (cu),
18347 m_gdbarch (arch),
18348 m_line_header (lh),
18349 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
18350 if there was a line entry for it so that the backend has a
18351 chance to adjust it and also record it in case it needs it.
18352 This is currently used by MIPS code,
18353 cf. `mips_adjust_dwarf2_line'. */
18354 m_address (gdbarch_adjust_dwarf2_line (arch, 0, 0)),
18355 m_flags (lh->default_is_stmt ? LEF_IS_STMT : (linetable_entry_flags) 0),
18356 m_last_address (m_address)
18357 {
18358 }
18359
18360 void
18361 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
18362 const gdb_byte *line_ptr,
18363 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
18364 {
18365 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
18366 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
18367 located at 0x0. In this case, additionally check that if
18368 ADDRESS < UNRELOCATED_LOWPC. */
18369
18370 if ((address == 0 && address < unrelocated_lowpc)
18371 || address == (CORE_ADDR) -1)
18372 {
18373 /* This line table is for a function which has been
18374 GCd by the linker. Ignore it. PR gdb/12528 */
18375
18376 struct objfile *objfile = cu->per_objfile->objfile;
18377 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18378
18379 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18380 line_offset, objfile_name (objfile));
18381 m_currently_recording_lines = false;
18382 /* Note: m_currently_recording_lines is left as false until we see
18383 DW_LNE_end_sequence. */
18384 }
18385 }
18386
18387 /* Subroutine of dwarf_decode_lines to simplify it.
18388 Process the line number information in LH. */
18389
18390 static void
18391 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18392 CORE_ADDR lowpc)
18393 {
18394 const gdb_byte *line_ptr, *extended_end;
18395 const gdb_byte *line_end;
18396 unsigned int bytes_read, extended_len;
18397 unsigned char op_code, extended_op;
18398 CORE_ADDR baseaddr;
18399 struct objfile *objfile = cu->per_objfile->objfile;
18400 bfd *abfd = objfile->obfd.get ();
18401 struct gdbarch *gdbarch = objfile->arch ();
18402
18403 baseaddr = objfile->text_section_offset ();
18404
18405 line_ptr = lh->statement_program_start;
18406 line_end = lh->statement_program_end;
18407
18408 /* Read the statement sequences until there's nothing left. */
18409 while (line_ptr < line_end)
18410 {
18411 /* The DWARF line number program state machine. Reset the state
18412 machine at the start of each sequence. */
18413 lnp_state_machine state_machine (cu, gdbarch, lh);
18414 bool end_sequence = false;
18415
18416 /* Start a subfile for the current file of the state
18417 machine. */
18418 const file_entry *fe = state_machine.current_file ();
18419
18420 if (fe != NULL)
18421 dwarf2_start_subfile (cu, *fe, *lh);
18422
18423 /* Decode the table. */
18424 while (line_ptr < line_end && !end_sequence)
18425 {
18426 op_code = read_1_byte (abfd, line_ptr);
18427 line_ptr += 1;
18428
18429 if (op_code >= lh->opcode_base)
18430 {
18431 /* Special opcode. */
18432 state_machine.handle_special_opcode (op_code);
18433 }
18434 else switch (op_code)
18435 {
18436 case DW_LNS_extended_op:
18437 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18438 &bytes_read);
18439 line_ptr += bytes_read;
18440 extended_end = line_ptr + extended_len;
18441 extended_op = read_1_byte (abfd, line_ptr);
18442 line_ptr += 1;
18443 if (DW_LNE_lo_user <= extended_op
18444 && extended_op <= DW_LNE_hi_user)
18445 {
18446 /* Vendor extension, ignore. */
18447 line_ptr = extended_end;
18448 break;
18449 }
18450 switch (extended_op)
18451 {
18452 case DW_LNE_end_sequence:
18453 state_machine.handle_end_sequence ();
18454 end_sequence = true;
18455 break;
18456 case DW_LNE_set_address:
18457 {
18458 CORE_ADDR address
18459 = cu->header.read_address (abfd, line_ptr, &bytes_read);
18460 line_ptr += bytes_read;
18461
18462 state_machine.check_line_address (cu, line_ptr,
18463 lowpc - baseaddr, address);
18464 state_machine.handle_set_address (address);
18465 }
18466 break;
18467 case DW_LNE_define_file:
18468 {
18469 const char *cur_file;
18470 unsigned int mod_time, length;
18471 dir_index dindex;
18472
18473 cur_file = read_direct_string (abfd, line_ptr,
18474 &bytes_read);
18475 line_ptr += bytes_read;
18476 dindex = (dir_index)
18477 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18478 line_ptr += bytes_read;
18479 mod_time =
18480 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18481 line_ptr += bytes_read;
18482 length =
18483 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18484 line_ptr += bytes_read;
18485 lh->add_file_name (cur_file, dindex, mod_time, length);
18486 }
18487 break;
18488 case DW_LNE_set_discriminator:
18489 {
18490 /* The discriminator is not interesting to the
18491 debugger; just ignore it. We still need to
18492 check its value though:
18493 if there are consecutive entries for the same
18494 (non-prologue) line we want to coalesce them.
18495 PR 17276. */
18496 unsigned int discr
18497 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18498 line_ptr += bytes_read;
18499
18500 state_machine.handle_set_discriminator (discr);
18501 }
18502 break;
18503 default:
18504 complaint (_("mangled .debug_line section"));
18505 return;
18506 }
18507 /* Make sure that we parsed the extended op correctly. If e.g.
18508 we expected a different address size than the producer used,
18509 we may have read the wrong number of bytes. */
18510 if (line_ptr != extended_end)
18511 {
18512 complaint (_("mangled .debug_line section"));
18513 return;
18514 }
18515 break;
18516 case DW_LNS_copy:
18517 state_machine.handle_copy ();
18518 break;
18519 case DW_LNS_advance_pc:
18520 {
18521 CORE_ADDR adjust
18522 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18523 line_ptr += bytes_read;
18524
18525 state_machine.handle_advance_pc (adjust);
18526 }
18527 break;
18528 case DW_LNS_advance_line:
18529 {
18530 int line_delta
18531 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18532 line_ptr += bytes_read;
18533
18534 state_machine.handle_advance_line (line_delta);
18535 }
18536 break;
18537 case DW_LNS_set_file:
18538 {
18539 file_name_index file
18540 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18541 &bytes_read);
18542 line_ptr += bytes_read;
18543
18544 state_machine.handle_set_file (file);
18545 }
18546 break;
18547 case DW_LNS_set_column:
18548 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18549 line_ptr += bytes_read;
18550 break;
18551 case DW_LNS_negate_stmt:
18552 state_machine.handle_negate_stmt ();
18553 break;
18554 case DW_LNS_set_basic_block:
18555 break;
18556 /* Add to the address register of the state machine the
18557 address increment value corresponding to special opcode
18558 255. I.e., this value is scaled by the minimum
18559 instruction length since special opcode 255 would have
18560 scaled the increment. */
18561 case DW_LNS_const_add_pc:
18562 state_machine.handle_const_add_pc ();
18563 break;
18564 case DW_LNS_fixed_advance_pc:
18565 {
18566 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18567 line_ptr += 2;
18568
18569 state_machine.handle_fixed_advance_pc (addr_adj);
18570 }
18571 break;
18572 case DW_LNS_set_prologue_end:
18573 state_machine.handle_set_prologue_end ();
18574 break;
18575 default:
18576 {
18577 /* Unknown standard opcode, ignore it. */
18578 int i;
18579
18580 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18581 {
18582 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18583 line_ptr += bytes_read;
18584 }
18585 }
18586 }
18587 }
18588
18589 if (!end_sequence)
18590 dwarf2_debug_line_missing_end_sequence_complaint ();
18591
18592 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18593 in which case we still finish recording the last line). */
18594 state_machine.record_line (true);
18595 }
18596 }
18597
18598 /* Decode the Line Number Program (LNP) for the given line_header
18599 structure and CU. The actual information extracted and the type
18600 of structures created from the LNP depends on the value of PST.
18601
18602 FND holds the CU file name and directory, if known.
18603 It is used for relative paths in the line table.
18604
18605 NOTE: It is important that psymtabs have the same file name (via
18606 strcmp) as the corresponding symtab. Since the directory is not
18607 used in the name of the symtab we don't use it in the name of the
18608 psymtabs we create. E.g. expand_line_sal requires this when
18609 finding psymtabs to expand. A good testcase for this is
18610 mb-inline.exp.
18611
18612 LOWPC is the lowest address in CU (or 0 if not known).
18613
18614 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18615 for its PC<->lines mapping information. Otherwise only the filename
18616 table is read in. */
18617
18618 static void
18619 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
18620 CORE_ADDR lowpc, int decode_mapping)
18621 {
18622 if (decode_mapping)
18623 dwarf_decode_lines_1 (lh, cu, lowpc);
18624
18625 /* Make sure a symtab is created for every file, even files
18626 which contain only variables (i.e. no code with associated
18627 line numbers). */
18628 buildsym_compunit *builder = cu->get_builder ();
18629 struct compunit_symtab *cust = builder->get_compunit_symtab ();
18630
18631 for (auto &fe : lh->file_names ())
18632 {
18633 dwarf2_start_subfile (cu, fe, *lh);
18634 subfile *sf = builder->get_current_subfile ();
18635
18636 if (sf->symtab == nullptr)
18637 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
18638 sf->name_for_id.c_str ());
18639
18640 fe.symtab = sf->symtab;
18641 }
18642 }
18643
18644 /* Start a subfile for DWARF. FILENAME is the name of the file and
18645 DIRNAME the name of the source directory which contains FILENAME
18646 or NULL if not known.
18647 This routine tries to keep line numbers from identical absolute and
18648 relative file names in a common subfile.
18649
18650 Using the `list' example from the GDB testsuite, which resides in
18651 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18652 of /srcdir/list0.c yields the following debugging information for list0.c:
18653
18654 DW_AT_name: /srcdir/list0.c
18655 DW_AT_comp_dir: /compdir
18656 files.files[0].name: list0.h
18657 files.files[0].dir: /srcdir
18658 files.files[1].name: list0.c
18659 files.files[1].dir: /srcdir
18660
18661 The line number information for list0.c has to end up in a single
18662 subfile, so that `break /srcdir/list0.c:1' works as expected.
18663 start_subfile will ensure that this happens provided that we pass the
18664 concatenation of files.files[1].dir and files.files[1].name as the
18665 subfile's name. */
18666
18667 static void
18668 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
18669 const line_header &lh)
18670 {
18671 std::string filename_holder;
18672 const char *filename = fe.name;
18673 const char *dirname = lh.include_dir_at (fe.d_index);
18674
18675 /* In order not to lose the line information directory,
18676 we concatenate it to the filename when it makes sense.
18677 Note that the Dwarf3 standard says (speaking of filenames in line
18678 information): ``The directory index is ignored for file names
18679 that represent full path names''. Thus ignoring dirname in the
18680 `else' branch below isn't an issue. */
18681
18682 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18683 {
18684 filename_holder = path_join (dirname, filename);
18685 filename = filename_holder.c_str ();
18686 }
18687
18688 std::string filename_for_id = lh.file_file_name (fe);
18689 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
18690 }
18691
18692 static void
18693 var_decode_location (struct attribute *attr, struct symbol *sym,
18694 struct dwarf2_cu *cu)
18695 {
18696 struct objfile *objfile = cu->per_objfile->objfile;
18697 struct comp_unit_head *cu_header = &cu->header;
18698
18699 /* NOTE drow/2003-01-30: There used to be a comment and some special
18700 code here to turn a symbol with DW_AT_external and a
18701 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18702 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18703 with some versions of binutils) where shared libraries could have
18704 relocations against symbols in their debug information - the
18705 minimal symbol would have the right address, but the debug info
18706 would not. It's no longer necessary, because we will explicitly
18707 apply relocations when we read in the debug information now. */
18708
18709 /* A DW_AT_location attribute with no contents indicates that a
18710 variable has been optimized away. */
18711 if (attr->form_is_block () && attr->as_block ()->size == 0)
18712 {
18713 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18714 return;
18715 }
18716
18717 /* Handle one degenerate form of location expression specially, to
18718 preserve GDB's previous behavior when section offsets are
18719 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
18720 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
18721
18722 if (attr->form_is_block ())
18723 {
18724 struct dwarf_block *block = attr->as_block ();
18725
18726 if ((block->data[0] == DW_OP_addr
18727 && block->size == 1 + cu_header->addr_size)
18728 || ((block->data[0] == DW_OP_GNU_addr_index
18729 || block->data[0] == DW_OP_addrx)
18730 && (block->size
18731 == 1 + leb128_size (&block->data[1]))))
18732 {
18733 unsigned int dummy;
18734
18735 if (block->data[0] == DW_OP_addr)
18736 sym->set_value_address
18737 (cu->header.read_address (objfile->obfd.get (), block->data + 1,
18738 &dummy));
18739 else
18740 sym->set_value_address
18741 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
18742 sym->set_aclass_index (LOC_STATIC);
18743 fixup_symbol_section (sym, objfile);
18744 sym->set_value_address
18745 (sym->value_address ()
18746 + objfile->section_offsets[sym->section_index ()]);
18747 return;
18748 }
18749 }
18750
18751 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18752 expression evaluator, and use LOC_COMPUTED only when necessary
18753 (i.e. when the value of a register or memory location is
18754 referenced, or a thread-local block, etc.). Then again, it might
18755 not be worthwhile. I'm assuming that it isn't unless performance
18756 or memory numbers show me otherwise. */
18757
18758 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18759
18760 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18761 cu->has_loclist = true;
18762 }
18763
18764 /* Given a pointer to a DWARF information entry, figure out if we need
18765 to make a symbol table entry for it, and if so, create a new entry
18766 and return a pointer to it.
18767 If TYPE is NULL, determine symbol type from the die, otherwise
18768 used the passed type.
18769 If SPACE is not NULL, use it to hold the new symbol. If it is
18770 NULL, allocate a new symbol on the objfile's obstack. */
18771
18772 static struct symbol *
18773 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18774 struct symbol *space)
18775 {
18776 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18777 struct objfile *objfile = per_objfile->objfile;
18778 struct gdbarch *gdbarch = objfile->arch ();
18779 struct symbol *sym = NULL;
18780 const char *name;
18781 struct attribute *attr = NULL;
18782 struct attribute *attr2 = NULL;
18783 CORE_ADDR baseaddr;
18784 struct pending **list_to_add = NULL;
18785
18786 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18787
18788 baseaddr = objfile->text_section_offset ();
18789
18790 name = dwarf2_name (die, cu);
18791 if (name == nullptr && (die->tag == DW_TAG_subprogram
18792 || die->tag == DW_TAG_inlined_subroutine
18793 || die->tag == DW_TAG_entry_point))
18794 name = dw2_linkage_name (die, cu);
18795
18796 if (name)
18797 {
18798 int suppress_add = 0;
18799
18800 if (space)
18801 sym = space;
18802 else
18803 sym = new (&objfile->objfile_obstack) symbol;
18804 OBJSTAT (objfile, n_syms++);
18805
18806 /* Cache this symbol's name and the name's demangled form (if any). */
18807 sym->set_language (cu->lang (), &objfile->objfile_obstack);
18808 /* Fortran does not have mangling standard and the mangling does differ
18809 between gfortran, iFort etc. */
18810 const char *physname
18811 = (cu->lang () == language_fortran
18812 ? dwarf2_full_name (name, die, cu)
18813 : dwarf2_physname (name, die, cu));
18814 const char *linkagename = dw2_linkage_name (die, cu);
18815
18816 if (linkagename == nullptr || cu->lang () == language_ada)
18817 sym->set_linkage_name (physname);
18818 else
18819 {
18820 sym->set_demangled_name (physname, &objfile->objfile_obstack);
18821 sym->set_linkage_name (linkagename);
18822 }
18823
18824 /* Handle DW_AT_artificial. */
18825 attr = dwarf2_attr (die, DW_AT_artificial, cu);
18826 if (attr != nullptr)
18827 sym->set_is_artificial (attr->as_boolean ());
18828
18829 /* Default assumptions.
18830 Use the passed type or decode it from the die. */
18831 sym->set_domain (VAR_DOMAIN);
18832 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18833 if (type != NULL)
18834 sym->set_type (type);
18835 else
18836 sym->set_type (die_type (die, cu));
18837 attr = dwarf2_attr (die,
18838 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18839 cu);
18840 if (attr != nullptr)
18841 sym->set_line (attr->constant_value (0));
18842
18843 attr = dwarf2_attr (die,
18844 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18845 cu);
18846 if (attr != nullptr && attr->is_nonnegative ())
18847 {
18848 file_name_index file_index
18849 = (file_name_index) attr->as_nonnegative ();
18850 struct file_entry *fe;
18851
18852 if (cu->line_header != NULL)
18853 fe = cu->line_header->file_name_at (file_index);
18854 else
18855 fe = NULL;
18856
18857 if (fe == NULL)
18858 complaint (_("file index out of range"));
18859 else
18860 sym->set_symtab (fe->symtab);
18861 }
18862
18863 switch (die->tag)
18864 {
18865 case DW_TAG_label:
18866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18867 if (attr != nullptr)
18868 {
18869 CORE_ADDR addr;
18870
18871 addr = attr->as_address ();
18872 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18873 sym->set_section_index (SECT_OFF_TEXT (objfile));
18874 sym->set_value_address (addr);
18875 sym->set_aclass_index (LOC_LABEL);
18876 }
18877 else
18878 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18879 sym->set_type (objfile_type (objfile)->builtin_core_addr);
18880 sym->set_domain (LABEL_DOMAIN);
18881 add_symbol_to_list (sym, cu->list_in_scope);
18882 break;
18883 case DW_TAG_subprogram:
18884 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18885 finish_block. */
18886 sym->set_aclass_index (LOC_BLOCK);
18887 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18888 if ((attr2 != nullptr && attr2->as_boolean ())
18889 || cu->lang () == language_ada
18890 || cu->lang () == language_fortran)
18891 {
18892 /* Subprograms marked external are stored as a global symbol.
18893 Ada and Fortran subprograms, whether marked external or
18894 not, are always stored as a global symbol, because we want
18895 to be able to access them globally. For instance, we want
18896 to be able to break on a nested subprogram without having
18897 to specify the context. */
18898 list_to_add = cu->get_builder ()->get_global_symbols ();
18899 }
18900 else
18901 {
18902 list_to_add = cu->list_in_scope;
18903 }
18904 break;
18905 case DW_TAG_inlined_subroutine:
18906 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18907 finish_block. */
18908 sym->set_aclass_index (LOC_BLOCK);
18909 sym->set_is_inlined (1);
18910 list_to_add = cu->list_in_scope;
18911 break;
18912 case DW_TAG_template_value_param:
18913 suppress_add = 1;
18914 /* Fall through. */
18915 case DW_TAG_constant:
18916 case DW_TAG_variable:
18917 case DW_TAG_member:
18918 /* Compilation with minimal debug info may result in
18919 variables with missing type entries. Change the
18920 misleading `void' type to something sensible. */
18921 if (sym->type ()->code () == TYPE_CODE_VOID)
18922 sym->set_type (objfile_type (objfile)->builtin_int);
18923
18924 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18925 /* In the case of DW_TAG_member, we should only be called for
18926 static const members. */
18927 if (die->tag == DW_TAG_member)
18928 {
18929 /* dwarf2_add_field uses die_is_declaration,
18930 so we do the same. */
18931 gdb_assert (die_is_declaration (die, cu));
18932 gdb_assert (attr);
18933 }
18934 if (attr != nullptr)
18935 {
18936 dwarf2_const_value (attr, sym, cu);
18937 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18938 if (!suppress_add)
18939 {
18940 if (attr2 != nullptr && attr2->as_boolean ())
18941 list_to_add = cu->get_builder ()->get_global_symbols ();
18942 else
18943 list_to_add = cu->list_in_scope;
18944 }
18945 break;
18946 }
18947 attr = dwarf2_attr (die, DW_AT_location, cu);
18948 if (attr != nullptr)
18949 {
18950 var_decode_location (attr, sym, cu);
18951 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18952
18953 /* Fortran explicitly imports any global symbols to the local
18954 scope by DW_TAG_common_block. */
18955 if (cu->lang () == language_fortran && die->parent
18956 && die->parent->tag == DW_TAG_common_block)
18957 attr2 = NULL;
18958
18959 if (sym->aclass () == LOC_STATIC
18960 && sym->value_address () == 0
18961 && !per_objfile->per_bfd->has_section_at_zero)
18962 {
18963 /* When a static variable is eliminated by the linker,
18964 the corresponding debug information is not stripped
18965 out, but the variable address is set to null;
18966 do not add such variables into symbol table. */
18967 }
18968 else if (attr2 != nullptr && attr2->as_boolean ())
18969 {
18970 if (sym->aclass () == LOC_STATIC
18971 && (objfile->flags & OBJF_MAINLINE) == 0
18972 && per_objfile->per_bfd->can_copy)
18973 {
18974 /* A global static variable might be subject to
18975 copy relocation. We first check for a local
18976 minsym, though, because maybe the symbol was
18977 marked hidden, in which case this would not
18978 apply. */
18979 bound_minimal_symbol found
18980 = (lookup_minimal_symbol_linkage
18981 (sym->linkage_name (), objfile));
18982 if (found.minsym != nullptr)
18983 sym->maybe_copied = 1;
18984 }
18985
18986 /* A variable with DW_AT_external is never static,
18987 but it may be block-scoped. */
18988 list_to_add
18989 = ((cu->list_in_scope
18990 == cu->get_builder ()->get_file_symbols ())
18991 ? cu->get_builder ()->get_global_symbols ()
18992 : cu->list_in_scope);
18993 }
18994 else
18995 list_to_add = cu->list_in_scope;
18996 }
18997 else
18998 {
18999 /* We do not know the address of this symbol.
19000 If it is an external symbol and we have type information
19001 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19002 The address of the variable will then be determined from
19003 the minimal symbol table whenever the variable is
19004 referenced. */
19005 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19006
19007 /* Fortran explicitly imports any global symbols to the local
19008 scope by DW_TAG_common_block. */
19009 if (cu->lang () == language_fortran && die->parent
19010 && die->parent->tag == DW_TAG_common_block)
19011 {
19012 /* SYMBOL_CLASS doesn't matter here because
19013 read_common_block is going to reset it. */
19014 if (!suppress_add)
19015 list_to_add = cu->list_in_scope;
19016 }
19017 else if (attr2 != nullptr && attr2->as_boolean ()
19018 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19019 {
19020 /* A variable with DW_AT_external is never static, but it
19021 may be block-scoped. */
19022 list_to_add
19023 = ((cu->list_in_scope
19024 == cu->get_builder ()->get_file_symbols ())
19025 ? cu->get_builder ()->get_global_symbols ()
19026 : cu->list_in_scope);
19027
19028 sym->set_aclass_index (LOC_UNRESOLVED);
19029 }
19030 else if (!die_is_declaration (die, cu))
19031 {
19032 /* Use the default LOC_OPTIMIZED_OUT class. */
19033 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
19034 if (!suppress_add)
19035 list_to_add = cu->list_in_scope;
19036 }
19037 }
19038 break;
19039 case DW_TAG_formal_parameter:
19040 {
19041 /* If we are inside a function, mark this as an argument. If
19042 not, we might be looking at an argument to an inlined function
19043 when we do not have enough information to show inlined frames;
19044 pretend it's a local variable in that case so that the user can
19045 still see it. */
19046 struct context_stack *curr
19047 = cu->get_builder ()->get_current_context_stack ();
19048 if (curr != nullptr && curr->name != nullptr)
19049 sym->set_is_argument (1);
19050 attr = dwarf2_attr (die, DW_AT_location, cu);
19051 if (attr != nullptr)
19052 {
19053 var_decode_location (attr, sym, cu);
19054 }
19055 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19056 if (attr != nullptr)
19057 {
19058 dwarf2_const_value (attr, sym, cu);
19059 }
19060
19061 list_to_add = cu->list_in_scope;
19062 }
19063 break;
19064 case DW_TAG_unspecified_parameters:
19065 /* From varargs functions; gdb doesn't seem to have any
19066 interest in this information, so just ignore it for now.
19067 (FIXME?) */
19068 break;
19069 case DW_TAG_template_type_param:
19070 suppress_add = 1;
19071 /* Fall through. */
19072 case DW_TAG_class_type:
19073 case DW_TAG_interface_type:
19074 case DW_TAG_structure_type:
19075 case DW_TAG_union_type:
19076 case DW_TAG_set_type:
19077 case DW_TAG_enumeration_type:
19078 case DW_TAG_namelist:
19079 if (die->tag == DW_TAG_namelist)
19080 {
19081 sym->set_aclass_index (LOC_STATIC);
19082 sym->set_domain (VAR_DOMAIN);
19083 }
19084 else
19085 {
19086 sym->set_aclass_index (LOC_TYPEDEF);
19087 sym->set_domain (STRUCT_DOMAIN);
19088 }
19089 {
19090 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19091 really ever be static objects: otherwise, if you try
19092 to, say, break of a class's method and you're in a file
19093 which doesn't mention that class, it won't work unless
19094 the check for all static symbols in lookup_symbol_aux
19095 saves you. See the OtherFileClass tests in
19096 gdb.c++/namespace.exp. */
19097
19098 if (!suppress_add)
19099 {
19100 buildsym_compunit *builder = cu->get_builder ();
19101 list_to_add
19102 = (cu->list_in_scope == builder->get_file_symbols ()
19103 && cu->lang () == language_cplus
19104 ? builder->get_global_symbols ()
19105 : cu->list_in_scope);
19106
19107 /* The semantics of C++ state that "struct foo {
19108 ... }" also defines a typedef for "foo". */
19109 if (cu->lang () == language_cplus
19110 || cu->lang () == language_ada
19111 || cu->lang () == language_d
19112 || cu->lang () == language_rust)
19113 {
19114 /* The symbol's name is already allocated along
19115 with this objfile, so we don't need to
19116 duplicate it for the type. */
19117 if (sym->type ()->name () == 0)
19118 sym->type ()->set_name (sym->search_name ());
19119 }
19120 }
19121 }
19122 break;
19123 case DW_TAG_typedef:
19124 sym->set_aclass_index (LOC_TYPEDEF);
19125 sym->set_domain (VAR_DOMAIN);
19126 list_to_add = cu->list_in_scope;
19127 break;
19128 case DW_TAG_array_type:
19129 case DW_TAG_base_type:
19130 case DW_TAG_subrange_type:
19131 case DW_TAG_generic_subrange:
19132 case DW_TAG_unspecified_type:
19133 sym->set_aclass_index (LOC_TYPEDEF);
19134 sym->set_domain (VAR_DOMAIN);
19135 list_to_add = cu->list_in_scope;
19136 break;
19137 case DW_TAG_enumerator:
19138 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19139 if (attr != nullptr)
19140 {
19141 dwarf2_const_value (attr, sym, cu);
19142 }
19143 {
19144 /* NOTE: carlton/2003-11-10: See comment above in the
19145 DW_TAG_class_type, etc. block. */
19146
19147 list_to_add
19148 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
19149 && cu->lang () == language_cplus
19150 ? cu->get_builder ()->get_global_symbols ()
19151 : cu->list_in_scope);
19152 }
19153 break;
19154 case DW_TAG_imported_declaration:
19155 case DW_TAG_namespace:
19156 sym->set_aclass_index (LOC_TYPEDEF);
19157 list_to_add = cu->get_builder ()->get_global_symbols ();
19158 break;
19159 case DW_TAG_module:
19160 sym->set_aclass_index (LOC_TYPEDEF);
19161 sym->set_domain (MODULE_DOMAIN);
19162 list_to_add = cu->get_builder ()->get_global_symbols ();
19163 break;
19164 case DW_TAG_common_block:
19165 sym->set_aclass_index (LOC_COMMON_BLOCK);
19166 sym->set_domain (COMMON_BLOCK_DOMAIN);
19167 add_symbol_to_list (sym, cu->list_in_scope);
19168 break;
19169 default:
19170 /* Not a tag we recognize. Hopefully we aren't processing
19171 trash data, but since we must specifically ignore things
19172 we don't recognize, there is nothing else we should do at
19173 this point. */
19174 complaint (_("unsupported tag: '%s'"),
19175 dwarf_tag_name (die->tag));
19176 break;
19177 }
19178
19179 if (suppress_add)
19180 {
19181 sym->hash_next = objfile->template_symbols;
19182 objfile->template_symbols = sym;
19183 list_to_add = NULL;
19184 }
19185
19186 if (list_to_add != NULL)
19187 add_symbol_to_list (sym, list_to_add);
19188
19189 /* For the benefit of old versions of GCC, check for anonymous
19190 namespaces based on the demangled name. */
19191 if (!cu->processing_has_namespace_info
19192 && cu->lang () == language_cplus)
19193 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
19194 }
19195 return (sym);
19196 }
19197
19198 /* Given an attr with a DW_FORM_dataN value in host byte order,
19199 zero-extend it as appropriate for the symbol's type. The DWARF
19200 standard (v4) is not entirely clear about the meaning of using
19201 DW_FORM_dataN for a constant with a signed type, where the type is
19202 wider than the data. The conclusion of a discussion on the DWARF
19203 list was that this is unspecified. We choose to always zero-extend
19204 because that is the interpretation long in use by GCC. */
19205
19206 static gdb_byte *
19207 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19208 struct dwarf2_cu *cu, LONGEST *value, int bits)
19209 {
19210 struct objfile *objfile = cu->per_objfile->objfile;
19211 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
19212 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19213 LONGEST l = attr->constant_value (0);
19214
19215 if (bits < sizeof (*value) * 8)
19216 {
19217 l &= ((LONGEST) 1 << bits) - 1;
19218 *value = l;
19219 }
19220 else if (bits == sizeof (*value) * 8)
19221 *value = l;
19222 else
19223 {
19224 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19225 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19226 return bytes;
19227 }
19228
19229 return NULL;
19230 }
19231
19232 /* Read a constant value from an attribute. Either set *VALUE, or if
19233 the value does not fit in *VALUE, set *BYTES - either already
19234 allocated on the objfile obstack, or newly allocated on OBSTACK,
19235 or, set *BATON, if we translated the constant to a location
19236 expression. */
19237
19238 static void
19239 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19240 const char *name, struct obstack *obstack,
19241 struct dwarf2_cu *cu,
19242 LONGEST *value, const gdb_byte **bytes,
19243 struct dwarf2_locexpr_baton **baton)
19244 {
19245 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19246 struct objfile *objfile = per_objfile->objfile;
19247 struct comp_unit_head *cu_header = &cu->header;
19248 struct dwarf_block *blk;
19249 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
19250 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19251
19252 *value = 0;
19253 *bytes = NULL;
19254 *baton = NULL;
19255
19256 switch (attr->form)
19257 {
19258 case DW_FORM_addr:
19259 case DW_FORM_addrx:
19260 case DW_FORM_GNU_addr_index:
19261 {
19262 gdb_byte *data;
19263
19264 if (type->length () != cu_header->addr_size)
19265 dwarf2_const_value_length_mismatch_complaint (name,
19266 cu_header->addr_size,
19267 type->length ());
19268 /* Symbols of this form are reasonably rare, so we just
19269 piggyback on the existing location code rather than writing
19270 a new implementation of symbol_computed_ops. */
19271 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19272 (*baton)->per_objfile = per_objfile;
19273 (*baton)->per_cu = cu->per_cu;
19274 gdb_assert ((*baton)->per_cu);
19275
19276 (*baton)->size = 2 + cu_header->addr_size;
19277 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19278 (*baton)->data = data;
19279
19280 data[0] = DW_OP_addr;
19281 store_unsigned_integer (&data[1], cu_header->addr_size,
19282 byte_order, attr->as_address ());
19283 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19284 }
19285 break;
19286 case DW_FORM_string:
19287 case DW_FORM_strp:
19288 case DW_FORM_strx:
19289 case DW_FORM_GNU_str_index:
19290 case DW_FORM_GNU_strp_alt:
19291 /* The string is already allocated on the objfile obstack, point
19292 directly to it. */
19293 *bytes = (const gdb_byte *) attr->as_string ();
19294 break;
19295 case DW_FORM_block1:
19296 case DW_FORM_block2:
19297 case DW_FORM_block4:
19298 case DW_FORM_block:
19299 case DW_FORM_exprloc:
19300 case DW_FORM_data16:
19301 blk = attr->as_block ();
19302 if (type->length () != blk->size)
19303 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19304 type->length ());
19305 *bytes = blk->data;
19306 break;
19307
19308 /* The DW_AT_const_value attributes are supposed to carry the
19309 symbol's value "represented as it would be on the target
19310 architecture." By the time we get here, it's already been
19311 converted to host endianness, so we just need to sign- or
19312 zero-extend it as appropriate. */
19313 case DW_FORM_data1:
19314 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19315 break;
19316 case DW_FORM_data2:
19317 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19318 break;
19319 case DW_FORM_data4:
19320 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19321 break;
19322 case DW_FORM_data8:
19323 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19324 break;
19325
19326 case DW_FORM_sdata:
19327 case DW_FORM_implicit_const:
19328 *value = attr->as_signed ();
19329 break;
19330
19331 case DW_FORM_udata:
19332 *value = attr->as_unsigned ();
19333 break;
19334
19335 default:
19336 complaint (_("unsupported const value attribute form: '%s'"),
19337 dwarf_form_name (attr->form));
19338 *value = 0;
19339 break;
19340 }
19341 }
19342
19343
19344 /* Copy constant value from an attribute to a symbol. */
19345
19346 static void
19347 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19348 struct dwarf2_cu *cu)
19349 {
19350 struct objfile *objfile = cu->per_objfile->objfile;
19351 LONGEST value;
19352 const gdb_byte *bytes;
19353 struct dwarf2_locexpr_baton *baton;
19354
19355 dwarf2_const_value_attr (attr, sym->type (),
19356 sym->print_name (),
19357 &objfile->objfile_obstack, cu,
19358 &value, &bytes, &baton);
19359
19360 if (baton != NULL)
19361 {
19362 SYMBOL_LOCATION_BATON (sym) = baton;
19363 sym->set_aclass_index (dwarf2_locexpr_index);
19364 }
19365 else if (bytes != NULL)
19366 {
19367 sym->set_value_bytes (bytes);
19368 sym->set_aclass_index (LOC_CONST_BYTES);
19369 }
19370 else
19371 {
19372 sym->set_value_longest (value);
19373 sym->set_aclass_index (LOC_CONST);
19374 }
19375 }
19376
19377 /* Return the type of the die in question using its DW_AT_type attribute. */
19378
19379 static struct type *
19380 die_type (struct die_info *die, struct dwarf2_cu *cu)
19381 {
19382 struct attribute *type_attr;
19383
19384 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19385 if (!type_attr)
19386 {
19387 struct objfile *objfile = cu->per_objfile->objfile;
19388 /* A missing DW_AT_type represents a void type. */
19389 return objfile_type (objfile)->builtin_void;
19390 }
19391
19392 return lookup_die_type (die, type_attr, cu);
19393 }
19394
19395 /* True iff CU's producer generates GNAT Ada auxiliary information
19396 that allows to find parallel types through that information instead
19397 of having to do expensive parallel lookups by type name. */
19398
19399 static int
19400 need_gnat_info (struct dwarf2_cu *cu)
19401 {
19402 /* Assume that the Ada compiler was GNAT, which always produces
19403 the auxiliary information. */
19404 return (cu->lang () == language_ada);
19405 }
19406
19407 /* Return the auxiliary type of the die in question using its
19408 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19409 attribute is not present. */
19410
19411 static struct type *
19412 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19413 {
19414 struct attribute *type_attr;
19415
19416 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19417 if (!type_attr)
19418 return NULL;
19419
19420 return lookup_die_type (die, type_attr, cu);
19421 }
19422
19423 /* If DIE has a descriptive_type attribute, then set the TYPE's
19424 descriptive type accordingly. */
19425
19426 static void
19427 set_descriptive_type (struct type *type, struct die_info *die,
19428 struct dwarf2_cu *cu)
19429 {
19430 struct type *descriptive_type = die_descriptive_type (die, cu);
19431
19432 if (descriptive_type)
19433 {
19434 ALLOCATE_GNAT_AUX_TYPE (type);
19435 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19436 }
19437 }
19438
19439 /* Return the containing type of the die in question using its
19440 DW_AT_containing_type attribute. */
19441
19442 static struct type *
19443 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19444 {
19445 struct attribute *type_attr;
19446 struct objfile *objfile = cu->per_objfile->objfile;
19447
19448 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19449 if (!type_attr)
19450 error (_("Dwarf Error: Problem turning containing type into gdb type "
19451 "[in module %s]"), objfile_name (objfile));
19452
19453 return lookup_die_type (die, type_attr, cu);
19454 }
19455
19456 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19457
19458 static struct type *
19459 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19460 {
19461 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19462 struct objfile *objfile = per_objfile->objfile;
19463 char *saved;
19464
19465 std::string message
19466 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
19467 objfile_name (objfile),
19468 sect_offset_str (cu->header.sect_off),
19469 sect_offset_str (die->sect_off));
19470 saved = obstack_strdup (&objfile->objfile_obstack, message);
19471
19472 return type_allocator (objfile).new_type (TYPE_CODE_ERROR, 0, saved);
19473 }
19474
19475 /* Look up the type of DIE in CU using its type attribute ATTR.
19476 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19477 DW_AT_containing_type.
19478 If there is no type substitute an error marker. */
19479
19480 static struct type *
19481 lookup_die_type (struct die_info *die, const struct attribute *attr,
19482 struct dwarf2_cu *cu)
19483 {
19484 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19485 struct objfile *objfile = per_objfile->objfile;
19486 struct type *this_type;
19487
19488 gdb_assert (attr->name == DW_AT_type
19489 || attr->name == DW_AT_GNAT_descriptive_type
19490 || attr->name == DW_AT_containing_type);
19491
19492 /* First see if we have it cached. */
19493
19494 if (attr->form == DW_FORM_GNU_ref_alt)
19495 {
19496 struct dwarf2_per_cu_data *per_cu;
19497 sect_offset sect_off = attr->get_ref_die_offset ();
19498
19499 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
19500 per_objfile->per_bfd);
19501 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
19502 }
19503 else if (attr->form_is_ref ())
19504 {
19505 sect_offset sect_off = attr->get_ref_die_offset ();
19506
19507 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
19508 }
19509 else if (attr->form == DW_FORM_ref_sig8)
19510 {
19511 ULONGEST signature = attr->as_signature ();
19512
19513 return get_signatured_type (die, signature, cu);
19514 }
19515 else
19516 {
19517 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
19518 " at %s [in module %s]"),
19519 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
19520 objfile_name (objfile));
19521 return build_error_marker_type (cu, die);
19522 }
19523
19524 /* If not cached we need to read it in. */
19525
19526 if (this_type == NULL)
19527 {
19528 struct die_info *type_die = NULL;
19529 struct dwarf2_cu *type_cu = cu;
19530
19531 if (attr->form_is_ref ())
19532 type_die = follow_die_ref (die, attr, &type_cu);
19533 if (type_die == NULL)
19534 return build_error_marker_type (cu, die);
19535 /* If we find the type now, it's probably because the type came
19536 from an inter-CU reference and the type's CU got expanded before
19537 ours. */
19538 this_type = read_type_die (type_die, type_cu);
19539 }
19540
19541 /* If we still don't have a type use an error marker. */
19542
19543 if (this_type == NULL)
19544 return build_error_marker_type (cu, die);
19545
19546 return this_type;
19547 }
19548
19549 /* Return the type in DIE, CU.
19550 Returns NULL for invalid types.
19551
19552 This first does a lookup in die_type_hash,
19553 and only reads the die in if necessary.
19554
19555 NOTE: This can be called when reading in partial or full symbols. */
19556
19557 static struct type *
19558 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19559 {
19560 struct type *this_type;
19561
19562 this_type = get_die_type (die, cu);
19563 if (this_type)
19564 return this_type;
19565
19566 return read_type_die_1 (die, cu);
19567 }
19568
19569 /* Read the type in DIE, CU.
19570 Returns NULL for invalid types. */
19571
19572 static struct type *
19573 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19574 {
19575 struct type *this_type = NULL;
19576
19577 switch (die->tag)
19578 {
19579 case DW_TAG_class_type:
19580 case DW_TAG_interface_type:
19581 case DW_TAG_structure_type:
19582 case DW_TAG_union_type:
19583 this_type = read_structure_type (die, cu);
19584 break;
19585 case DW_TAG_enumeration_type:
19586 this_type = read_enumeration_type (die, cu);
19587 break;
19588 case DW_TAG_subprogram:
19589 case DW_TAG_subroutine_type:
19590 case DW_TAG_inlined_subroutine:
19591 this_type = read_subroutine_type (die, cu);
19592 break;
19593 case DW_TAG_array_type:
19594 this_type = read_array_type (die, cu);
19595 break;
19596 case DW_TAG_set_type:
19597 this_type = read_set_type (die, cu);
19598 break;
19599 case DW_TAG_pointer_type:
19600 this_type = read_tag_pointer_type (die, cu);
19601 break;
19602 case DW_TAG_ptr_to_member_type:
19603 this_type = read_tag_ptr_to_member_type (die, cu);
19604 break;
19605 case DW_TAG_reference_type:
19606 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19607 break;
19608 case DW_TAG_rvalue_reference_type:
19609 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19610 break;
19611 case DW_TAG_const_type:
19612 this_type = read_tag_const_type (die, cu);
19613 break;
19614 case DW_TAG_volatile_type:
19615 this_type = read_tag_volatile_type (die, cu);
19616 break;
19617 case DW_TAG_restrict_type:
19618 this_type = read_tag_restrict_type (die, cu);
19619 break;
19620 case DW_TAG_string_type:
19621 this_type = read_tag_string_type (die, cu);
19622 break;
19623 case DW_TAG_typedef:
19624 this_type = read_typedef (die, cu);
19625 break;
19626 case DW_TAG_generic_subrange:
19627 case DW_TAG_subrange_type:
19628 this_type = read_subrange_type (die, cu);
19629 break;
19630 case DW_TAG_base_type:
19631 this_type = read_base_type (die, cu);
19632 break;
19633 case DW_TAG_unspecified_type:
19634 this_type = read_unspecified_type (die, cu);
19635 break;
19636 case DW_TAG_namespace:
19637 this_type = read_namespace_type (die, cu);
19638 break;
19639 case DW_TAG_module:
19640 this_type = read_module_type (die, cu);
19641 break;
19642 case DW_TAG_atomic_type:
19643 this_type = read_tag_atomic_type (die, cu);
19644 break;
19645 default:
19646 complaint (_("unexpected tag in read_type_die: '%s'"),
19647 dwarf_tag_name (die->tag));
19648 break;
19649 }
19650
19651 return this_type;
19652 }
19653
19654 /* See if we can figure out if the class lives in a namespace. We do
19655 this by looking for a member function; its demangled name will
19656 contain namespace info, if there is any.
19657 Return the computed name or NULL.
19658 Space for the result is allocated on the objfile's obstack.
19659 This is the full-die version of guess_partial_die_structure_name.
19660 In this case we know DIE has no useful parent. */
19661
19662 static const char *
19663 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19664 {
19665 struct die_info *spec_die;
19666 struct dwarf2_cu *spec_cu;
19667 struct die_info *child;
19668 struct objfile *objfile = cu->per_objfile->objfile;
19669
19670 spec_cu = cu;
19671 spec_die = die_specification (die, &spec_cu);
19672 if (spec_die != NULL)
19673 {
19674 die = spec_die;
19675 cu = spec_cu;
19676 }
19677
19678 for (child = die->child;
19679 child != NULL;
19680 child = child->sibling)
19681 {
19682 if (child->tag == DW_TAG_subprogram)
19683 {
19684 const char *linkage_name = dw2_linkage_name (child, cu);
19685
19686 if (linkage_name != NULL)
19687 {
19688 gdb::unique_xmalloc_ptr<char> actual_name
19689 (cu->language_defn->class_name_from_physname (linkage_name));
19690 const char *name = NULL;
19691
19692 if (actual_name != NULL)
19693 {
19694 const char *die_name = dwarf2_name (die, cu);
19695
19696 if (die_name != NULL
19697 && strcmp (die_name, actual_name.get ()) != 0)
19698 {
19699 /* Strip off the class name from the full name.
19700 We want the prefix. */
19701 int die_name_len = strlen (die_name);
19702 int actual_name_len = strlen (actual_name.get ());
19703 const char *ptr = actual_name.get ();
19704
19705 /* Test for '::' as a sanity check. */
19706 if (actual_name_len > die_name_len + 2
19707 && ptr[actual_name_len - die_name_len - 1] == ':')
19708 name = obstack_strndup (
19709 &objfile->per_bfd->storage_obstack,
19710 ptr, actual_name_len - die_name_len - 2);
19711 }
19712 }
19713 return name;
19714 }
19715 }
19716 }
19717
19718 return NULL;
19719 }
19720
19721 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19722 prefix part in such case. See
19723 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19724
19725 static const char *
19726 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19727 {
19728 struct attribute *attr;
19729 const char *base;
19730
19731 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19732 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19733 return NULL;
19734
19735 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19736 return NULL;
19737
19738 attr = dw2_linkage_name_attr (die, cu);
19739 const char *attr_name = attr->as_string ();
19740 if (attr == NULL || attr_name == NULL)
19741 return NULL;
19742
19743 /* dwarf2_name had to be already called. */
19744 gdb_assert (attr->canonical_string_p ());
19745
19746 /* Strip the base name, keep any leading namespaces/classes. */
19747 base = strrchr (attr_name, ':');
19748 if (base == NULL || base == attr_name || base[-1] != ':')
19749 return "";
19750
19751 struct objfile *objfile = cu->per_objfile->objfile;
19752 return obstack_strndup (&objfile->per_bfd->storage_obstack,
19753 attr_name,
19754 &base[-1] - attr_name);
19755 }
19756
19757 /* Return the name of the namespace/class that DIE is defined within,
19758 or "" if we can't tell. The caller should not xfree the result.
19759
19760 For example, if we're within the method foo() in the following
19761 code:
19762
19763 namespace N {
19764 class C {
19765 void foo () {
19766 }
19767 };
19768 }
19769
19770 then determine_prefix on foo's die will return "N::C". */
19771
19772 static const char *
19773 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19774 {
19775 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19776 struct die_info *parent, *spec_die;
19777 struct dwarf2_cu *spec_cu;
19778 struct type *parent_type;
19779 const char *retval;
19780
19781 if (cu->lang () != language_cplus
19782 && cu->lang () != language_fortran
19783 && cu->lang () != language_d
19784 && cu->lang () != language_rust)
19785 return "";
19786
19787 retval = anonymous_struct_prefix (die, cu);
19788 if (retval)
19789 return retval;
19790
19791 /* We have to be careful in the presence of DW_AT_specification.
19792 For example, with GCC 3.4, given the code
19793
19794 namespace N {
19795 void foo() {
19796 // Definition of N::foo.
19797 }
19798 }
19799
19800 then we'll have a tree of DIEs like this:
19801
19802 1: DW_TAG_compile_unit
19803 2: DW_TAG_namespace // N
19804 3: DW_TAG_subprogram // declaration of N::foo
19805 4: DW_TAG_subprogram // definition of N::foo
19806 DW_AT_specification // refers to die #3
19807
19808 Thus, when processing die #4, we have to pretend that we're in
19809 the context of its DW_AT_specification, namely the contex of die
19810 #3. */
19811 spec_cu = cu;
19812 spec_die = die_specification (die, &spec_cu);
19813 if (spec_die == NULL)
19814 parent = die->parent;
19815 else
19816 {
19817 parent = spec_die->parent;
19818 cu = spec_cu;
19819 }
19820
19821 if (parent == NULL)
19822 return "";
19823 else if (parent->building_fullname)
19824 {
19825 const char *name;
19826 const char *parent_name;
19827
19828 /* It has been seen on RealView 2.2 built binaries,
19829 DW_TAG_template_type_param types actually _defined_ as
19830 children of the parent class:
19831
19832 enum E {};
19833 template class <class Enum> Class{};
19834 Class<enum E> class_e;
19835
19836 1: DW_TAG_class_type (Class)
19837 2: DW_TAG_enumeration_type (E)
19838 3: DW_TAG_enumerator (enum1:0)
19839 3: DW_TAG_enumerator (enum2:1)
19840 ...
19841 2: DW_TAG_template_type_param
19842 DW_AT_type DW_FORM_ref_udata (E)
19843
19844 Besides being broken debug info, it can put GDB into an
19845 infinite loop. Consider:
19846
19847 When we're building the full name for Class<E>, we'll start
19848 at Class, and go look over its template type parameters,
19849 finding E. We'll then try to build the full name of E, and
19850 reach here. We're now trying to build the full name of E,
19851 and look over the parent DIE for containing scope. In the
19852 broken case, if we followed the parent DIE of E, we'd again
19853 find Class, and once again go look at its template type
19854 arguments, etc., etc. Simply don't consider such parent die
19855 as source-level parent of this die (it can't be, the language
19856 doesn't allow it), and break the loop here. */
19857 name = dwarf2_name (die, cu);
19858 parent_name = dwarf2_name (parent, cu);
19859 complaint (_("template param type '%s' defined within parent '%s'"),
19860 name ? name : "<unknown>",
19861 parent_name ? parent_name : "<unknown>");
19862 return "";
19863 }
19864 else
19865 switch (parent->tag)
19866 {
19867 case DW_TAG_namespace:
19868 parent_type = read_type_die (parent, cu);
19869 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19870 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19871 Work around this problem here. */
19872 if (cu->lang () == language_cplus
19873 && strcmp (parent_type->name (), "::") == 0)
19874 return "";
19875 /* We give a name to even anonymous namespaces. */
19876 return parent_type->name ();
19877 case DW_TAG_class_type:
19878 case DW_TAG_interface_type:
19879 case DW_TAG_structure_type:
19880 case DW_TAG_union_type:
19881 case DW_TAG_module:
19882 parent_type = read_type_die (parent, cu);
19883 if (parent_type->name () != NULL)
19884 return parent_type->name ();
19885 else
19886 /* An anonymous structure is only allowed non-static data
19887 members; no typedefs, no member functions, et cetera.
19888 So it does not need a prefix. */
19889 return "";
19890 case DW_TAG_compile_unit:
19891 case DW_TAG_partial_unit:
19892 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19893 if (cu->lang () == language_cplus
19894 && !per_objfile->per_bfd->types.empty ()
19895 && die->child != NULL
19896 && (die->tag == DW_TAG_class_type
19897 || die->tag == DW_TAG_structure_type
19898 || die->tag == DW_TAG_union_type))
19899 {
19900 const char *name = guess_full_die_structure_name (die, cu);
19901 if (name != NULL)
19902 return name;
19903 }
19904 return "";
19905 case DW_TAG_subprogram:
19906 /* Nested subroutines in Fortran get a prefix with the name
19907 of the parent's subroutine. */
19908 if (cu->lang () == language_fortran)
19909 {
19910 if ((die->tag == DW_TAG_subprogram)
19911 && (dwarf2_name (parent, cu) != NULL))
19912 return dwarf2_name (parent, cu);
19913 }
19914 return "";
19915 case DW_TAG_enumeration_type:
19916 parent_type = read_type_die (parent, cu);
19917 if (parent_type->is_declared_class ())
19918 {
19919 if (parent_type->name () != NULL)
19920 return parent_type->name ();
19921 return "";
19922 }
19923 /* Fall through. */
19924 default:
19925 return determine_prefix (parent, cu);
19926 }
19927 }
19928
19929 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19930 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19931 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19932 an obconcat, otherwise allocate storage for the result. The CU argument is
19933 used to determine the language and hence, the appropriate separator. */
19934
19935 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19936
19937 static char *
19938 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19939 int physname, struct dwarf2_cu *cu)
19940 {
19941 const char *lead = "";
19942 const char *sep;
19943
19944 if (suffix == NULL || suffix[0] == '\0'
19945 || prefix == NULL || prefix[0] == '\0')
19946 sep = "";
19947 else if (cu->lang () == language_d)
19948 {
19949 /* For D, the 'main' function could be defined in any module, but it
19950 should never be prefixed. */
19951 if (strcmp (suffix, "D main") == 0)
19952 {
19953 prefix = "";
19954 sep = "";
19955 }
19956 else
19957 sep = ".";
19958 }
19959 else if (cu->lang () == language_fortran && physname)
19960 {
19961 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19962 DW_AT_MIPS_linkage_name is preferred and used instead. */
19963
19964 lead = "__";
19965 sep = "_MOD_";
19966 }
19967 else
19968 sep = "::";
19969
19970 if (prefix == NULL)
19971 prefix = "";
19972 if (suffix == NULL)
19973 suffix = "";
19974
19975 if (obs == NULL)
19976 {
19977 char *retval
19978 = ((char *)
19979 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19980
19981 strcpy (retval, lead);
19982 strcat (retval, prefix);
19983 strcat (retval, sep);
19984 strcat (retval, suffix);
19985 return retval;
19986 }
19987 else
19988 {
19989 /* We have an obstack. */
19990 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
19991 }
19992 }
19993
19994 /* Return a generic name for a DW_TAG_template_type_param or
19995 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
19996 per parent, so each function/class/struct template will have their own set
19997 of template parameters named <unnnamed0>, <unnamed1>, ... where the
19998 enumeration starts at 0 and represents the position of the template tag in
19999 the list of unnamed template tags for this parent, counting both, type and
20000 value tags. */
20001
20002 static const char *
20003 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
20004 {
20005 if (die->parent == nullptr)
20006 return nullptr;
20007
20008 /* Count the parent types unnamed template type and value children until, we
20009 arrive at our entry. */
20010 size_t nth_unnamed = 0;
20011
20012 die_info *child = die->parent->child;
20013 while (child != die)
20014 {
20015 gdb_assert (child != nullptr);
20016 if (child->tag == DW_TAG_template_type_param
20017 || child->tag == DW_TAG_template_value_param)
20018 {
20019 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
20020 ++nth_unnamed;
20021 }
20022 child = child->sibling;
20023 }
20024
20025 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
20026 return cu->per_objfile->objfile->intern (name_str.c_str ());
20027 }
20028
20029 /* Get name of a die, return NULL if not found. */
20030
20031 static const char *
20032 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20033 struct objfile *objfile)
20034 {
20035 if (name == nullptr)
20036 return name;
20037
20038 if (cu->lang () == language_cplus)
20039 {
20040 gdb::unique_xmalloc_ptr<char> canon_name
20041 = cp_canonicalize_string (name);
20042
20043 if (canon_name != nullptr)
20044 name = objfile->intern (canon_name.get ());
20045 }
20046 else if (cu->lang () == language_c)
20047 {
20048 gdb::unique_xmalloc_ptr<char> canon_name
20049 = c_canonicalize_name (name);
20050
20051 if (canon_name != nullptr)
20052 name = objfile->intern (canon_name.get ());
20053 }
20054
20055 return name;
20056 }
20057
20058 /* Get name of a die, return NULL if not found.
20059 Anonymous namespaces are converted to their magic string. */
20060
20061 static const char *
20062 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20063 {
20064 struct attribute *attr;
20065 struct objfile *objfile = cu->per_objfile->objfile;
20066
20067 attr = dwarf2_attr (die, DW_AT_name, cu);
20068 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
20069 if (attr_name == nullptr
20070 && die->tag != DW_TAG_namespace
20071 && die->tag != DW_TAG_class_type
20072 && die->tag != DW_TAG_interface_type
20073 && die->tag != DW_TAG_structure_type
20074 && die->tag != DW_TAG_namelist
20075 && die->tag != DW_TAG_union_type
20076 && die->tag != DW_TAG_template_type_param
20077 && die->tag != DW_TAG_template_value_param)
20078 return NULL;
20079
20080 switch (die->tag)
20081 {
20082 /* A member's name should not be canonicalized. This is a bit
20083 of a hack, in that normally it should not be possible to run
20084 into this situation; however, the dw2-unusual-field-names.exp
20085 test creates custom DWARF that does. */
20086 case DW_TAG_member:
20087 case DW_TAG_compile_unit:
20088 case DW_TAG_partial_unit:
20089 /* Compilation units have a DW_AT_name that is a filename, not
20090 a source language identifier. */
20091 case DW_TAG_enumeration_type:
20092 case DW_TAG_enumerator:
20093 /* These tags always have simple identifiers already; no need
20094 to canonicalize them. */
20095 return attr_name;
20096
20097 case DW_TAG_namespace:
20098 if (attr_name != nullptr)
20099 return attr_name;
20100 return CP_ANONYMOUS_NAMESPACE_STR;
20101
20102 /* DWARF does not actually require template tags to have a name. */
20103 case DW_TAG_template_type_param:
20104 case DW_TAG_template_value_param:
20105 if (attr_name == nullptr)
20106 return unnamed_template_tag_name (die, cu);
20107 /* FALLTHROUGH. */
20108 case DW_TAG_class_type:
20109 case DW_TAG_interface_type:
20110 case DW_TAG_structure_type:
20111 case DW_TAG_union_type:
20112 case DW_TAG_namelist:
20113 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20114 structures or unions. These were of the form "._%d" in GCC 4.1,
20115 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20116 and GCC 4.4. We work around this problem by ignoring these. */
20117 if (attr_name != nullptr
20118 && (startswith (attr_name, "._")
20119 || startswith (attr_name, "<anonymous")))
20120 return NULL;
20121
20122 /* GCC might emit a nameless typedef that has a linkage name. See
20123 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20124 if (!attr || attr_name == NULL)
20125 {
20126 attr = dw2_linkage_name_attr (die, cu);
20127 attr_name = attr == nullptr ? nullptr : attr->as_string ();
20128 if (attr == NULL || attr_name == NULL)
20129 return NULL;
20130
20131 /* Avoid demangling attr_name the second time on a second
20132 call for the same DIE. */
20133 if (!attr->canonical_string_p ())
20134 {
20135 gdb::unique_xmalloc_ptr<char> demangled
20136 (gdb_demangle (attr_name, DMGL_TYPES));
20137 if (demangled == nullptr)
20138 return nullptr;
20139
20140 attr->set_string_canonical (objfile->intern (demangled.get ()));
20141 attr_name = attr->as_string ();
20142 }
20143
20144 /* Strip any leading namespaces/classes, keep only the
20145 base name. DW_AT_name for named DIEs does not
20146 contain the prefixes. */
20147 const char *base = strrchr (attr_name, ':');
20148 if (base && base > attr_name && base[-1] == ':')
20149 return &base[1];
20150 else
20151 return attr_name;
20152 }
20153 break;
20154
20155 default:
20156 break;
20157 }
20158
20159 if (!attr->canonical_string_p ())
20160 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
20161 objfile));
20162 return attr->as_string ();
20163 }
20164
20165 /* Return the die that this die in an extension of, or NULL if there
20166 is none. *EXT_CU is the CU containing DIE on input, and the CU
20167 containing the return value on output. */
20168
20169 static struct die_info *
20170 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20171 {
20172 struct attribute *attr;
20173
20174 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20175 if (attr == NULL)
20176 return NULL;
20177
20178 return follow_die_ref (die, attr, ext_cu);
20179 }
20180
20181 static void
20182 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20183 {
20184 void **slot;
20185
20186 slot = htab_find_slot_with_hash (cu->die_hash, die,
20187 to_underlying (die->sect_off),
20188 INSERT);
20189
20190 *slot = die;
20191 }
20192
20193 /* Follow reference or signature attribute ATTR of SRC_DIE.
20194 On entry *REF_CU is the CU of SRC_DIE.
20195 On exit *REF_CU is the CU of the result. */
20196
20197 static struct die_info *
20198 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20199 struct dwarf2_cu **ref_cu)
20200 {
20201 struct die_info *die;
20202
20203 if (attr->form_is_ref ())
20204 die = follow_die_ref (src_die, attr, ref_cu);
20205 else if (attr->form == DW_FORM_ref_sig8)
20206 die = follow_die_sig (src_die, attr, ref_cu);
20207 else
20208 {
20209 src_die->error_dump ();
20210 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20211 objfile_name ((*ref_cu)->per_objfile->objfile));
20212 }
20213
20214 return die;
20215 }
20216
20217 /* Follow reference OFFSET.
20218 On entry *REF_CU is the CU of the source die referencing OFFSET.
20219 On exit *REF_CU is the CU of the result.
20220 Returns NULL if OFFSET is invalid. */
20221
20222 static struct die_info *
20223 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
20224 struct dwarf2_cu **ref_cu)
20225 {
20226 struct die_info temp_die;
20227 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20228 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20229
20230 gdb_assert (cu->per_cu != NULL);
20231
20232 target_cu = cu;
20233
20234 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
20235 "source CU contains target offset: %d",
20236 sect_offset_str (cu->per_cu->sect_off),
20237 sect_offset_str (sect_off),
20238 cu->header.offset_in_cu_p (sect_off));
20239
20240 if (cu->per_cu->is_debug_types)
20241 {
20242 /* .debug_types CUs cannot reference anything outside their CU.
20243 If they need to, they have to reference a signatured type via
20244 DW_FORM_ref_sig8. */
20245 if (!cu->header.offset_in_cu_p (sect_off))
20246 return NULL;
20247 }
20248 else if (offset_in_dwz != cu->per_cu->is_dwz
20249 || !cu->header.offset_in_cu_p (sect_off))
20250 {
20251 struct dwarf2_per_cu_data *per_cu;
20252
20253 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20254 per_objfile->per_bfd);
20255
20256 dwarf_read_debug_printf_v ("target CU offset: %s, "
20257 "target CU DIEs loaded: %d",
20258 sect_offset_str (per_cu->sect_off),
20259 per_objfile->get_cu (per_cu) != nullptr);
20260
20261 /* If necessary, add it to the queue and load its DIEs.
20262
20263 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20264 it doesn't mean they are currently loaded. Since we require them
20265 to be loaded, we must check for ourselves. */
20266 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
20267 || per_objfile->get_cu (per_cu) == nullptr)
20268 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
20269 false, cu->lang ());
20270
20271 target_cu = per_objfile->get_cu (per_cu);
20272 gdb_assert (target_cu != nullptr);
20273 }
20274 else if (cu->dies == NULL)
20275 {
20276 /* We're loading full DIEs during partial symbol reading. */
20277 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
20278 language_minimal);
20279 }
20280
20281 *ref_cu = target_cu;
20282 temp_die.sect_off = sect_off;
20283
20284 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20285 &temp_die,
20286 to_underlying (sect_off));
20287 }
20288
20289 /* Follow reference attribute ATTR of SRC_DIE.
20290 On entry *REF_CU is the CU of SRC_DIE.
20291 On exit *REF_CU is the CU of the result. */
20292
20293 static struct die_info *
20294 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20295 struct dwarf2_cu **ref_cu)
20296 {
20297 sect_offset sect_off = attr->get_ref_die_offset ();
20298 struct dwarf2_cu *cu = *ref_cu;
20299 struct die_info *die;
20300
20301 die = follow_die_offset (sect_off,
20302 (attr->form == DW_FORM_GNU_ref_alt
20303 || cu->per_cu->is_dwz),
20304 ref_cu);
20305 if (!die)
20306 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
20307 "at %s [in module %s]"),
20308 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
20309 objfile_name (cu->per_objfile->objfile));
20310
20311 return die;
20312 }
20313
20314 /* See read.h. */
20315
20316 struct dwarf2_locexpr_baton
20317 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20318 dwarf2_per_cu_data *per_cu,
20319 dwarf2_per_objfile *per_objfile,
20320 gdb::function_view<CORE_ADDR ()> get_frame_pc,
20321 bool resolve_abstract_p)
20322 {
20323 struct die_info *die;
20324 struct attribute *attr;
20325 struct dwarf2_locexpr_baton retval;
20326 struct objfile *objfile = per_objfile->objfile;
20327
20328 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20329 if (cu == nullptr)
20330 cu = load_cu (per_cu, per_objfile, false);
20331
20332 if (cu == nullptr)
20333 {
20334 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20335 Instead just throw an error, not much else we can do. */
20336 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20337 sect_offset_str (sect_off), objfile_name (objfile));
20338 }
20339
20340 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20341 if (!die)
20342 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20343 sect_offset_str (sect_off), objfile_name (objfile));
20344
20345 attr = dwarf2_attr (die, DW_AT_location, cu);
20346 if (!attr && resolve_abstract_p
20347 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
20348 != per_objfile->per_bfd->abstract_to_concrete.end ()))
20349 {
20350 CORE_ADDR pc = get_frame_pc ();
20351 CORE_ADDR baseaddr = objfile->text_section_offset ();
20352 struct gdbarch *gdbarch = objfile->arch ();
20353
20354 for (const auto &cand_off
20355 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
20356 {
20357 struct dwarf2_cu *cand_cu = cu;
20358 struct die_info *cand
20359 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
20360 if (!cand
20361 || !cand->parent
20362 || cand->parent->tag != DW_TAG_subprogram)
20363 continue;
20364
20365 CORE_ADDR pc_low, pc_high;
20366 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
20367 if (pc_low == ((CORE_ADDR) -1))
20368 continue;
20369 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
20370 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
20371 if (!(pc_low <= pc && pc < pc_high))
20372 continue;
20373
20374 die = cand;
20375 attr = dwarf2_attr (die, DW_AT_location, cu);
20376 break;
20377 }
20378 }
20379
20380 if (!attr)
20381 {
20382 /* DWARF: "If there is no such attribute, then there is no effect.".
20383 DATA is ignored if SIZE is 0. */
20384
20385 retval.data = NULL;
20386 retval.size = 0;
20387 }
20388 else if (attr->form_is_section_offset ())
20389 {
20390 struct dwarf2_loclist_baton loclist_baton;
20391 CORE_ADDR pc = get_frame_pc ();
20392 size_t size;
20393
20394 fill_in_loclist_baton (cu, &loclist_baton, attr);
20395
20396 retval.data = dwarf2_find_location_expression (&loclist_baton,
20397 &size, pc);
20398 retval.size = size;
20399 }
20400 else
20401 {
20402 if (!attr->form_is_block ())
20403 error (_("Dwarf Error: DIE at %s referenced in module %s "
20404 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20405 sect_offset_str (sect_off), objfile_name (objfile));
20406
20407 struct dwarf_block *block = attr->as_block ();
20408 retval.data = block->data;
20409 retval.size = block->size;
20410 }
20411 retval.per_objfile = per_objfile;
20412 retval.per_cu = cu->per_cu;
20413
20414 per_objfile->age_comp_units ();
20415
20416 return retval;
20417 }
20418
20419 /* See read.h. */
20420
20421 struct dwarf2_locexpr_baton
20422 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20423 dwarf2_per_cu_data *per_cu,
20424 dwarf2_per_objfile *per_objfile,
20425 gdb::function_view<CORE_ADDR ()> get_frame_pc)
20426 {
20427 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20428
20429 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
20430 get_frame_pc);
20431 }
20432
20433 /* Write a constant of a given type as target-ordered bytes into
20434 OBSTACK. */
20435
20436 static const gdb_byte *
20437 write_constant_as_bytes (struct obstack *obstack,
20438 enum bfd_endian byte_order,
20439 struct type *type,
20440 ULONGEST value,
20441 LONGEST *len)
20442 {
20443 gdb_byte *result;
20444
20445 *len = type->length ();
20446 result = (gdb_byte *) obstack_alloc (obstack, *len);
20447 store_unsigned_integer (result, *len, byte_order, value);
20448
20449 return result;
20450 }
20451
20452 /* See read.h. */
20453
20454 const gdb_byte *
20455 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20456 dwarf2_per_cu_data *per_cu,
20457 dwarf2_per_objfile *per_objfile,
20458 obstack *obstack,
20459 LONGEST *len)
20460 {
20461 struct die_info *die;
20462 struct attribute *attr;
20463 const gdb_byte *result = NULL;
20464 struct type *type;
20465 LONGEST value;
20466 enum bfd_endian byte_order;
20467 struct objfile *objfile = per_objfile->objfile;
20468
20469 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20470 if (cu == nullptr)
20471 cu = load_cu (per_cu, per_objfile, false);
20472
20473 if (cu == nullptr)
20474 {
20475 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20476 Instead just throw an error, not much else we can do. */
20477 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20478 sect_offset_str (sect_off), objfile_name (objfile));
20479 }
20480
20481 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20482 if (!die)
20483 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20484 sect_offset_str (sect_off), objfile_name (objfile));
20485
20486 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20487 if (attr == NULL)
20488 return NULL;
20489
20490 byte_order = (bfd_big_endian (objfile->obfd.get ())
20491 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20492
20493 switch (attr->form)
20494 {
20495 case DW_FORM_addr:
20496 case DW_FORM_addrx:
20497 case DW_FORM_GNU_addr_index:
20498 {
20499 gdb_byte *tem;
20500
20501 *len = cu->header.addr_size;
20502 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20503 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
20504 result = tem;
20505 }
20506 break;
20507 case DW_FORM_string:
20508 case DW_FORM_strp:
20509 case DW_FORM_strx:
20510 case DW_FORM_GNU_str_index:
20511 case DW_FORM_GNU_strp_alt:
20512 /* The string is already allocated on the objfile obstack, point
20513 directly to it. */
20514 {
20515 const char *attr_name = attr->as_string ();
20516 result = (const gdb_byte *) attr_name;
20517 *len = strlen (attr_name);
20518 }
20519 break;
20520 case DW_FORM_block1:
20521 case DW_FORM_block2:
20522 case DW_FORM_block4:
20523 case DW_FORM_block:
20524 case DW_FORM_exprloc:
20525 case DW_FORM_data16:
20526 {
20527 struct dwarf_block *block = attr->as_block ();
20528 result = block->data;
20529 *len = block->size;
20530 }
20531 break;
20532
20533 /* The DW_AT_const_value attributes are supposed to carry the
20534 symbol's value "represented as it would be on the target
20535 architecture." By the time we get here, it's already been
20536 converted to host endianness, so we just need to sign- or
20537 zero-extend it as appropriate. */
20538 case DW_FORM_data1:
20539 type = die_type (die, cu);
20540 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20541 if (result == NULL)
20542 result = write_constant_as_bytes (obstack, byte_order,
20543 type, value, len);
20544 break;
20545 case DW_FORM_data2:
20546 type = die_type (die, cu);
20547 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20548 if (result == NULL)
20549 result = write_constant_as_bytes (obstack, byte_order,
20550 type, value, len);
20551 break;
20552 case DW_FORM_data4:
20553 type = die_type (die, cu);
20554 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20555 if (result == NULL)
20556 result = write_constant_as_bytes (obstack, byte_order,
20557 type, value, len);
20558 break;
20559 case DW_FORM_data8:
20560 type = die_type (die, cu);
20561 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20562 if (result == NULL)
20563 result = write_constant_as_bytes (obstack, byte_order,
20564 type, value, len);
20565 break;
20566
20567 case DW_FORM_sdata:
20568 case DW_FORM_implicit_const:
20569 type = die_type (die, cu);
20570 result = write_constant_as_bytes (obstack, byte_order,
20571 type, attr->as_signed (), len);
20572 break;
20573
20574 case DW_FORM_udata:
20575 type = die_type (die, cu);
20576 result = write_constant_as_bytes (obstack, byte_order,
20577 type, attr->as_unsigned (), len);
20578 break;
20579
20580 default:
20581 complaint (_("unsupported const value attribute form: '%s'"),
20582 dwarf_form_name (attr->form));
20583 break;
20584 }
20585
20586 return result;
20587 }
20588
20589 /* See read.h. */
20590
20591 struct type *
20592 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20593 dwarf2_per_cu_data *per_cu,
20594 dwarf2_per_objfile *per_objfile,
20595 const char **var_name)
20596 {
20597 struct die_info *die;
20598
20599 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20600 if (cu == nullptr)
20601 cu = load_cu (per_cu, per_objfile, false);
20602
20603 if (cu == nullptr)
20604 return nullptr;
20605
20606 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20607 if (!die)
20608 return NULL;
20609
20610 if (var_name != nullptr)
20611 *var_name = var_decl_name (die, cu);
20612 return die_type (die, cu);
20613 }
20614
20615 /* See read.h. */
20616
20617 struct type *
20618 dwarf2_get_die_type (cu_offset die_offset,
20619 dwarf2_per_cu_data *per_cu,
20620 dwarf2_per_objfile *per_objfile)
20621 {
20622 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20623 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
20624 }
20625
20626 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20627 On entry *REF_CU is the CU of SRC_DIE.
20628 On exit *REF_CU is the CU of the result.
20629 Returns NULL if the referenced DIE isn't found. */
20630
20631 static struct die_info *
20632 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20633 struct dwarf2_cu **ref_cu)
20634 {
20635 struct die_info temp_die;
20636 struct dwarf2_cu *sig_cu;
20637 struct die_info *die;
20638 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
20639
20640
20641 /* While it might be nice to assert sig_type->type == NULL here,
20642 we can get here for DW_AT_imported_declaration where we need
20643 the DIE not the type. */
20644
20645 /* If necessary, add it to the queue and load its DIEs.
20646
20647 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20648 it doesn't mean they are currently loaded. Since we require them
20649 to be loaded, we must check for ourselves. */
20650 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
20651 language_minimal)
20652 || per_objfile->get_cu (sig_type) == nullptr)
20653 read_signatured_type (sig_type, per_objfile);
20654
20655 sig_cu = per_objfile->get_cu (sig_type);
20656 gdb_assert (sig_cu != NULL);
20657 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20658 temp_die.sect_off = sig_type->type_offset_in_section;
20659 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20660 to_underlying (temp_die.sect_off));
20661 if (die)
20662 {
20663 /* For .gdb_index version 7 keep track of included TUs.
20664 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20665 if (per_objfile->per_bfd->index_table != NULL
20666 && !per_objfile->per_bfd->index_table->version_check ())
20667 {
20668 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
20669 }
20670
20671 *ref_cu = sig_cu;
20672 return die;
20673 }
20674
20675 return NULL;
20676 }
20677
20678 /* Follow signatured type referenced by ATTR in SRC_DIE.
20679 On entry *REF_CU is the CU of SRC_DIE.
20680 On exit *REF_CU is the CU of the result.
20681 The result is the DIE of the type.
20682 If the referenced type cannot be found an error is thrown. */
20683
20684 static struct die_info *
20685 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20686 struct dwarf2_cu **ref_cu)
20687 {
20688 ULONGEST signature = attr->as_signature ();
20689 struct signatured_type *sig_type;
20690 struct die_info *die;
20691
20692 gdb_assert (attr->form == DW_FORM_ref_sig8);
20693
20694 sig_type = lookup_signatured_type (*ref_cu, signature);
20695 /* sig_type will be NULL if the signatured type is missing from
20696 the debug info. */
20697 if (sig_type == NULL)
20698 {
20699 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20700 " from DIE at %s [in module %s]"),
20701 hex_string (signature), sect_offset_str (src_die->sect_off),
20702 objfile_name ((*ref_cu)->per_objfile->objfile));
20703 }
20704
20705 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20706 if (die == NULL)
20707 {
20708 src_die->error_dump ();
20709 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20710 " from DIE at %s [in module %s]"),
20711 hex_string (signature), sect_offset_str (src_die->sect_off),
20712 objfile_name ((*ref_cu)->per_objfile->objfile));
20713 }
20714
20715 return die;
20716 }
20717
20718 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20719 reading in and processing the type unit if necessary. */
20720
20721 static struct type *
20722 get_signatured_type (struct die_info *die, ULONGEST signature,
20723 struct dwarf2_cu *cu)
20724 {
20725 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20726 struct signatured_type *sig_type;
20727 struct dwarf2_cu *type_cu;
20728 struct die_info *type_die;
20729 struct type *type;
20730
20731 sig_type = lookup_signatured_type (cu, signature);
20732 /* sig_type will be NULL if the signatured type is missing from
20733 the debug info. */
20734 if (sig_type == NULL)
20735 {
20736 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20737 " from DIE at %s [in module %s]"),
20738 hex_string (signature), sect_offset_str (die->sect_off),
20739 objfile_name (per_objfile->objfile));
20740 return build_error_marker_type (cu, die);
20741 }
20742
20743 /* If we already know the type we're done. */
20744 type = per_objfile->get_type_for_signatured_type (sig_type);
20745 if (type != nullptr)
20746 return type;
20747
20748 type_cu = cu;
20749 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20750 if (type_die != NULL)
20751 {
20752 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20753 is created. This is important, for example, because for c++ classes
20754 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20755 type = read_type_die (type_die, type_cu);
20756 if (type == NULL)
20757 {
20758 complaint (_("Dwarf Error: Cannot build signatured type %s"
20759 " referenced from DIE at %s [in module %s]"),
20760 hex_string (signature), sect_offset_str (die->sect_off),
20761 objfile_name (per_objfile->objfile));
20762 type = build_error_marker_type (cu, die);
20763 }
20764 }
20765 else
20766 {
20767 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20768 " from DIE at %s [in module %s]"),
20769 hex_string (signature), sect_offset_str (die->sect_off),
20770 objfile_name (per_objfile->objfile));
20771 type = build_error_marker_type (cu, die);
20772 }
20773
20774 per_objfile->set_type_for_signatured_type (sig_type, type);
20775
20776 return type;
20777 }
20778
20779 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20780 reading in and processing the type unit if necessary. */
20781
20782 static struct type *
20783 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20784 struct dwarf2_cu *cu) /* ARI: editCase function */
20785 {
20786 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20787 if (attr->form_is_ref ())
20788 {
20789 struct dwarf2_cu *type_cu = cu;
20790 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20791
20792 return read_type_die (type_die, type_cu);
20793 }
20794 else if (attr->form == DW_FORM_ref_sig8)
20795 {
20796 return get_signatured_type (die, attr->as_signature (), cu);
20797 }
20798 else
20799 {
20800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20801
20802 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20803 " at %s [in module %s]"),
20804 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
20805 objfile_name (per_objfile->objfile));
20806 return build_error_marker_type (cu, die);
20807 }
20808 }
20809
20810 /* Load the DIEs associated with type unit PER_CU into memory. */
20811
20812 static void
20813 load_full_type_unit (dwarf2_per_cu_data *per_cu,
20814 dwarf2_per_objfile *per_objfile)
20815 {
20816 struct signatured_type *sig_type;
20817
20818 /* We have the per_cu, but we need the signatured_type.
20819 Fortunately this is an easy translation. */
20820 gdb_assert (per_cu->is_debug_types);
20821 sig_type = (struct signatured_type *) per_cu;
20822
20823 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
20824
20825 read_signatured_type (sig_type, per_objfile);
20826
20827 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
20828 }
20829
20830 /* Read in a signatured type and build its CU and DIEs.
20831 If the type is a stub for the real type in a DWO file,
20832 read in the real type from the DWO file as well. */
20833
20834 static void
20835 read_signatured_type (signatured_type *sig_type,
20836 dwarf2_per_objfile *per_objfile)
20837 {
20838 gdb_assert (sig_type->is_debug_types);
20839 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
20840
20841 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
20842
20843 if (!reader.dummy_p)
20844 {
20845 struct dwarf2_cu *cu = reader.cu;
20846 const gdb_byte *info_ptr = reader.info_ptr;
20847
20848 gdb_assert (cu->die_hash == NULL);
20849 cu->die_hash =
20850 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
20851 die_info::hash,
20852 die_info::eq,
20853 NULL,
20854 &cu->comp_unit_obstack,
20855 hashtab_obstack_allocate,
20856 dummy_obstack_deallocate);
20857
20858 if (reader.comp_unit_die->has_children)
20859 reader.comp_unit_die->child
20860 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
20861 reader.comp_unit_die);
20862 cu->dies = reader.comp_unit_die;
20863 /* comp_unit_die is not stored in die_hash, no need. */
20864
20865 /* We try not to read any attributes in this function, because
20866 not all CUs needed for references have been loaded yet, and
20867 symbol table processing isn't initialized. But we have to
20868 set the CU language, or we won't be able to build types
20869 correctly. Similarly, if we do not read the producer, we can
20870 not apply producer-specific interpretation. */
20871 prepare_one_comp_unit (cu, cu->dies, language_minimal);
20872
20873 reader.keep ();
20874 }
20875
20876 sig_type->tu_read = 1;
20877 }
20878
20879 /* Decode simple location descriptions.
20880 Given a pointer to a dwarf block that defines a location, compute
20881 the location and return the value. If COMPUTED is non-null, it is
20882 set to true to indicate that decoding was successful, and false
20883 otherwise. If COMPUTED is null, then this function may emit a
20884 complaint. */
20885
20886 static CORE_ADDR
20887 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
20888 {
20889 struct objfile *objfile = cu->per_objfile->objfile;
20890 size_t i;
20891 size_t size = blk->size;
20892 const gdb_byte *data = blk->data;
20893 CORE_ADDR stack[64];
20894 int stacki;
20895 unsigned int bytes_read, unsnd;
20896 gdb_byte op;
20897
20898 if (computed != nullptr)
20899 *computed = false;
20900
20901 i = 0;
20902 stacki = 0;
20903 stack[stacki] = 0;
20904 stack[++stacki] = 0;
20905
20906 while (i < size)
20907 {
20908 op = data[i++];
20909 switch (op)
20910 {
20911 case DW_OP_lit0:
20912 case DW_OP_lit1:
20913 case DW_OP_lit2:
20914 case DW_OP_lit3:
20915 case DW_OP_lit4:
20916 case DW_OP_lit5:
20917 case DW_OP_lit6:
20918 case DW_OP_lit7:
20919 case DW_OP_lit8:
20920 case DW_OP_lit9:
20921 case DW_OP_lit10:
20922 case DW_OP_lit11:
20923 case DW_OP_lit12:
20924 case DW_OP_lit13:
20925 case DW_OP_lit14:
20926 case DW_OP_lit15:
20927 case DW_OP_lit16:
20928 case DW_OP_lit17:
20929 case DW_OP_lit18:
20930 case DW_OP_lit19:
20931 case DW_OP_lit20:
20932 case DW_OP_lit21:
20933 case DW_OP_lit22:
20934 case DW_OP_lit23:
20935 case DW_OP_lit24:
20936 case DW_OP_lit25:
20937 case DW_OP_lit26:
20938 case DW_OP_lit27:
20939 case DW_OP_lit28:
20940 case DW_OP_lit29:
20941 case DW_OP_lit30:
20942 case DW_OP_lit31:
20943 stack[++stacki] = op - DW_OP_lit0;
20944 break;
20945
20946 case DW_OP_reg0:
20947 case DW_OP_reg1:
20948 case DW_OP_reg2:
20949 case DW_OP_reg3:
20950 case DW_OP_reg4:
20951 case DW_OP_reg5:
20952 case DW_OP_reg6:
20953 case DW_OP_reg7:
20954 case DW_OP_reg8:
20955 case DW_OP_reg9:
20956 case DW_OP_reg10:
20957 case DW_OP_reg11:
20958 case DW_OP_reg12:
20959 case DW_OP_reg13:
20960 case DW_OP_reg14:
20961 case DW_OP_reg15:
20962 case DW_OP_reg16:
20963 case DW_OP_reg17:
20964 case DW_OP_reg18:
20965 case DW_OP_reg19:
20966 case DW_OP_reg20:
20967 case DW_OP_reg21:
20968 case DW_OP_reg22:
20969 case DW_OP_reg23:
20970 case DW_OP_reg24:
20971 case DW_OP_reg25:
20972 case DW_OP_reg26:
20973 case DW_OP_reg27:
20974 case DW_OP_reg28:
20975 case DW_OP_reg29:
20976 case DW_OP_reg30:
20977 case DW_OP_reg31:
20978 stack[++stacki] = op - DW_OP_reg0;
20979 if (i < size)
20980 {
20981 if (computed == nullptr)
20982 dwarf2_complex_location_expr_complaint ();
20983 else
20984 return 0;
20985 }
20986 break;
20987
20988 case DW_OP_regx:
20989 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
20990 i += bytes_read;
20991 stack[++stacki] = unsnd;
20992 if (i < size)
20993 {
20994 if (computed == nullptr)
20995 dwarf2_complex_location_expr_complaint ();
20996 else
20997 return 0;
20998 }
20999 break;
21000
21001 case DW_OP_addr:
21002 stack[++stacki] = cu->header.read_address (objfile->obfd.get (),
21003 &data[i],
21004 &bytes_read);
21005 i += bytes_read;
21006 break;
21007
21008 case DW_OP_const1u:
21009 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
21010 i += 1;
21011 break;
21012
21013 case DW_OP_const1s:
21014 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
21015 i += 1;
21016 break;
21017
21018 case DW_OP_const2u:
21019 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
21020 i += 2;
21021 break;
21022
21023 case DW_OP_const2s:
21024 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
21025 i += 2;
21026 break;
21027
21028 case DW_OP_const4u:
21029 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
21030 i += 4;
21031 break;
21032
21033 case DW_OP_const4s:
21034 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
21035 i += 4;
21036 break;
21037
21038 case DW_OP_const8u:
21039 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
21040 i += 8;
21041 break;
21042
21043 case DW_OP_constu:
21044 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21045 &bytes_read);
21046 i += bytes_read;
21047 break;
21048
21049 case DW_OP_consts:
21050 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21051 i += bytes_read;
21052 break;
21053
21054 case DW_OP_dup:
21055 stack[stacki + 1] = stack[stacki];
21056 stacki++;
21057 break;
21058
21059 case DW_OP_plus:
21060 stack[stacki - 1] += stack[stacki];
21061 stacki--;
21062 break;
21063
21064 case DW_OP_plus_uconst:
21065 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21066 &bytes_read);
21067 i += bytes_read;
21068 break;
21069
21070 case DW_OP_minus:
21071 stack[stacki - 1] -= stack[stacki];
21072 stacki--;
21073 break;
21074
21075 case DW_OP_deref:
21076 /* If we're not the last op, then we definitely can't encode
21077 this using GDB's address_class enum. This is valid for partial
21078 global symbols, although the variable's address will be bogus
21079 in the psymtab. */
21080 if (i < size)
21081 {
21082 if (computed == nullptr)
21083 dwarf2_complex_location_expr_complaint ();
21084 else
21085 return 0;
21086 }
21087 break;
21088
21089 case DW_OP_GNU_push_tls_address:
21090 case DW_OP_form_tls_address:
21091 /* The top of the stack has the offset from the beginning
21092 of the thread control block at which the variable is located. */
21093 /* Nothing should follow this operator, so the top of stack would
21094 be returned. */
21095 /* This is valid for partial global symbols, but the variable's
21096 address will be bogus in the psymtab. Make it always at least
21097 non-zero to not look as a variable garbage collected by linker
21098 which have DW_OP_addr 0. */
21099 if (i < size)
21100 {
21101 if (computed == nullptr)
21102 dwarf2_complex_location_expr_complaint ();
21103 else
21104 return 0;
21105 }
21106 stack[stacki]++;
21107 break;
21108
21109 case DW_OP_GNU_uninit:
21110 if (computed != nullptr)
21111 return 0;
21112 break;
21113
21114 case DW_OP_addrx:
21115 case DW_OP_GNU_addr_index:
21116 case DW_OP_GNU_const_index:
21117 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21118 &bytes_read);
21119 i += bytes_read;
21120 break;
21121
21122 default:
21123 if (computed == nullptr)
21124 {
21125 const char *name = get_DW_OP_name (op);
21126
21127 if (name)
21128 complaint (_("unsupported stack op: '%s'"),
21129 name);
21130 else
21131 complaint (_("unsupported stack op: '%02x'"),
21132 op);
21133 }
21134
21135 return (stack[stacki]);
21136 }
21137
21138 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21139 outside of the allocated space. Also enforce minimum>0. */
21140 if (stacki >= ARRAY_SIZE (stack) - 1)
21141 {
21142 if (computed == nullptr)
21143 complaint (_("location description stack overflow"));
21144 return 0;
21145 }
21146
21147 if (stacki <= 0)
21148 {
21149 if (computed == nullptr)
21150 complaint (_("location description stack underflow"));
21151 return 0;
21152 }
21153 }
21154
21155 if (computed != nullptr)
21156 *computed = true;
21157 return (stack[stacki]);
21158 }
21159
21160 /* memory allocation interface */
21161
21162 static struct dwarf_block *
21163 dwarf_alloc_block (struct dwarf2_cu *cu)
21164 {
21165 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21166 }
21167
21168 \f
21169
21170 /* Macro support. */
21171
21172 /* An overload of dwarf_decode_macros that finds the correct section
21173 and ensures it is read in before calling the other overload. */
21174
21175 static void
21176 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
21177 int section_is_gnu)
21178 {
21179 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21180 struct objfile *objfile = per_objfile->objfile;
21181 const struct line_header *lh = cu->line_header;
21182 unsigned int offset_size = cu->header.offset_size;
21183 struct dwarf2_section_info *section;
21184 const char *section_name;
21185
21186 if (cu->dwo_unit != nullptr)
21187 {
21188 if (section_is_gnu)
21189 {
21190 section = &cu->dwo_unit->dwo_file->sections.macro;
21191 section_name = ".debug_macro.dwo";
21192 }
21193 else
21194 {
21195 section = &cu->dwo_unit->dwo_file->sections.macinfo;
21196 section_name = ".debug_macinfo.dwo";
21197 }
21198 }
21199 else
21200 {
21201 if (section_is_gnu)
21202 {
21203 section = &per_objfile->per_bfd->macro;
21204 section_name = ".debug_macro";
21205 }
21206 else
21207 {
21208 section = &per_objfile->per_bfd->macinfo;
21209 section_name = ".debug_macinfo";
21210 }
21211 }
21212
21213 section->read (objfile);
21214 if (section->buffer == nullptr)
21215 {
21216 complaint (_("missing %s section"), section_name);
21217 return;
21218 }
21219
21220 buildsym_compunit *builder = cu->get_builder ();
21221
21222 struct dwarf2_section_info *str_offsets_section;
21223 struct dwarf2_section_info *str_section;
21224 gdb::optional<ULONGEST> str_offsets_base;
21225
21226 if (cu->dwo_unit != nullptr)
21227 {
21228 str_offsets_section = &cu->dwo_unit->dwo_file
21229 ->sections.str_offsets;
21230 str_section = &cu->dwo_unit->dwo_file->sections.str;
21231 str_offsets_base = cu->header.addr_size;
21232 }
21233 else
21234 {
21235 str_offsets_section = &per_objfile->per_bfd->str_offsets;
21236 str_section = &per_objfile->per_bfd->str;
21237 str_offsets_base = cu->str_offsets_base;
21238 }
21239
21240 dwarf_decode_macros (per_objfile, builder, section, lh,
21241 offset_size, offset, str_section, str_offsets_section,
21242 str_offsets_base, section_is_gnu, cu);
21243 }
21244
21245 /* Return the .debug_loc section to use for CU.
21246 For DWO files use .debug_loc.dwo. */
21247
21248 static struct dwarf2_section_info *
21249 cu_debug_loc_section (struct dwarf2_cu *cu)
21250 {
21251 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21252
21253 if (cu->dwo_unit)
21254 {
21255 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21256
21257 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
21258 }
21259 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
21260 : &per_objfile->per_bfd->loc);
21261 }
21262
21263 /* Return the .debug_rnglists section to use for CU. */
21264 static struct dwarf2_section_info *
21265 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
21266 {
21267 if (cu->header.version < 5)
21268 error (_(".debug_rnglists section cannot be used in DWARF %d"),
21269 cu->header.version);
21270 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21271
21272 /* Make sure we read the .debug_rnglists section from the file that
21273 contains the DW_AT_ranges attribute we are reading. Normally that
21274 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
21275 or DW_TAG_skeleton unit, we always want to read from objfile/linked
21276 program. */
21277 if (cu->dwo_unit != nullptr
21278 && tag != DW_TAG_compile_unit
21279 && tag != DW_TAG_skeleton_unit)
21280 {
21281 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
21282
21283 if (sections->rnglists.size > 0)
21284 return &sections->rnglists;
21285 else
21286 error (_(".debug_rnglists section is missing from .dwo file."));
21287 }
21288 return &dwarf2_per_objfile->per_bfd->rnglists;
21289 }
21290
21291 /* A helper function that fills in a dwarf2_loclist_baton. */
21292
21293 static void
21294 fill_in_loclist_baton (struct dwarf2_cu *cu,
21295 struct dwarf2_loclist_baton *baton,
21296 const struct attribute *attr)
21297 {
21298 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21299 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21300
21301 section->read (per_objfile->objfile);
21302
21303 baton->per_objfile = per_objfile;
21304 baton->per_cu = cu->per_cu;
21305 gdb_assert (baton->per_cu);
21306 /* We don't know how long the location list is, but make sure we
21307 don't run off the edge of the section. */
21308 baton->size = section->size - attr->as_unsigned ();
21309 baton->data = section->buffer + attr->as_unsigned ();
21310 if (cu->base_address.has_value ())
21311 baton->base_address = *cu->base_address;
21312 else
21313 baton->base_address = 0;
21314 baton->from_dwo = cu->dwo_unit != NULL;
21315 }
21316
21317 static void
21318 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
21319 struct dwarf2_cu *cu, int is_block)
21320 {
21321 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21322 struct objfile *objfile = per_objfile->objfile;
21323 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
21324
21325 if (attr->form_is_section_offset ()
21326 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21327 the section. If so, fall through to the complaint in the
21328 other branch. */
21329 && attr->as_unsigned () < section->get_size (objfile))
21330 {
21331 struct dwarf2_loclist_baton *baton;
21332
21333 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
21334
21335 fill_in_loclist_baton (cu, baton, attr);
21336
21337 if (!cu->base_address.has_value ())
21338 complaint (_("Location list used without "
21339 "specifying the CU base address."));
21340
21341 sym->set_aclass_index ((is_block
21342 ? dwarf2_loclist_block_index
21343 : dwarf2_loclist_index));
21344 SYMBOL_LOCATION_BATON (sym) = baton;
21345 }
21346 else
21347 {
21348 struct dwarf2_locexpr_baton *baton;
21349
21350 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
21351 baton->per_objfile = per_objfile;
21352 baton->per_cu = cu->per_cu;
21353 gdb_assert (baton->per_cu);
21354
21355 if (attr->form_is_block ())
21356 {
21357 /* Note that we're just copying the block's data pointer
21358 here, not the actual data. We're still pointing into the
21359 info_buffer for SYM's objfile; right now we never release
21360 that buffer, but when we do clean up properly this may
21361 need to change. */
21362 struct dwarf_block *block = attr->as_block ();
21363 baton->size = block->size;
21364 baton->data = block->data;
21365 }
21366 else
21367 {
21368 dwarf2_invalid_attrib_class_complaint ("location description",
21369 sym->natural_name ());
21370 baton->size = 0;
21371 }
21372
21373 sym->set_aclass_index ((is_block
21374 ? dwarf2_locexpr_block_index
21375 : dwarf2_locexpr_index));
21376 SYMBOL_LOCATION_BATON (sym) = baton;
21377 }
21378 }
21379
21380 /* See read.h. */
21381
21382 const comp_unit_head *
21383 dwarf2_per_cu_data::get_header () const
21384 {
21385 if (!m_header_read_in)
21386 {
21387 const gdb_byte *info_ptr
21388 = this->section->buffer + to_underlying (this->sect_off);
21389
21390 read_comp_unit_head (&m_header, info_ptr, this->section,
21391 rcuh_kind::COMPILE);
21392
21393 m_header_read_in = true;
21394 }
21395
21396 return &m_header;
21397 }
21398
21399 /* See read.h. */
21400
21401 int
21402 dwarf2_per_cu_data::addr_size () const
21403 {
21404 return this->get_header ()->addr_size;
21405 }
21406
21407 /* See read.h. */
21408
21409 int
21410 dwarf2_per_cu_data::offset_size () const
21411 {
21412 return this->get_header ()->offset_size;
21413 }
21414
21415 /* See read.h. */
21416
21417 int
21418 dwarf2_per_cu_data::ref_addr_size () const
21419 {
21420 const comp_unit_head *header = this->get_header ();
21421
21422 if (header->version == 2)
21423 return header->addr_size;
21424 else
21425 return header->offset_size;
21426 }
21427
21428 /* A helper function for dwarf2_find_containing_comp_unit that returns
21429 the index of the result, and that searches a vector. It will
21430 return a result even if the offset in question does not actually
21431 occur in any CU. This is separate so that it can be unit
21432 tested. */
21433
21434 static int
21435 dwarf2_find_containing_comp_unit
21436 (sect_offset sect_off,
21437 unsigned int offset_in_dwz,
21438 const std::vector<dwarf2_per_cu_data_up> &all_units)
21439 {
21440 int low, high;
21441
21442 low = 0;
21443 high = all_units.size () - 1;
21444 while (high > low)
21445 {
21446 struct dwarf2_per_cu_data *mid_cu;
21447 int mid = low + (high - low) / 2;
21448
21449 mid_cu = all_units[mid].get ();
21450 if (mid_cu->is_dwz > offset_in_dwz
21451 || (mid_cu->is_dwz == offset_in_dwz
21452 && mid_cu->sect_off + mid_cu->length () > sect_off))
21453 high = mid;
21454 else
21455 low = mid + 1;
21456 }
21457 gdb_assert (low == high);
21458 return low;
21459 }
21460
21461 /* Locate the .debug_info compilation unit from CU's objfile which contains
21462 the DIE at OFFSET. Raises an error on failure. */
21463
21464 static struct dwarf2_per_cu_data *
21465 dwarf2_find_containing_comp_unit (sect_offset sect_off,
21466 unsigned int offset_in_dwz,
21467 dwarf2_per_bfd *per_bfd)
21468 {
21469 int low = dwarf2_find_containing_comp_unit
21470 (sect_off, offset_in_dwz, per_bfd->all_units);
21471 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
21472
21473 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
21474 {
21475 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
21476 error (_("Dwarf Error: could not find partial DIE containing "
21477 "offset %s [in module %s]"),
21478 sect_offset_str (sect_off),
21479 bfd_get_filename (per_bfd->obfd));
21480
21481 gdb_assert (per_bfd->all_units[low-1]->sect_off
21482 <= sect_off);
21483 return per_bfd->all_units[low - 1].get ();
21484 }
21485 else
21486 {
21487 if (low == per_bfd->all_units.size () - 1
21488 && sect_off >= this_cu->sect_off + this_cu->length ())
21489 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
21490 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
21491 return this_cu;
21492 }
21493 }
21494
21495 #if GDB_SELF_TEST
21496
21497 namespace selftests {
21498 namespace find_containing_comp_unit {
21499
21500 static void
21501 run_test ()
21502 {
21503 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
21504 dwarf2_per_cu_data *one_ptr = one.get ();
21505 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
21506 dwarf2_per_cu_data *two_ptr = two.get ();
21507 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
21508 dwarf2_per_cu_data *three_ptr = three.get ();
21509 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
21510 dwarf2_per_cu_data *four_ptr = four.get ();
21511
21512 one->set_length (5);
21513 two->sect_off = sect_offset (one->length ());
21514 two->set_length (7);
21515
21516 three->set_length (5);
21517 three->is_dwz = 1;
21518 four->sect_off = sect_offset (three->length ());
21519 four->set_length (7);
21520 four->is_dwz = 1;
21521
21522 std::vector<dwarf2_per_cu_data_up> units;
21523 units.push_back (std::move (one));
21524 units.push_back (std::move (two));
21525 units.push_back (std::move (three));
21526 units.push_back (std::move (four));
21527
21528 int result;
21529
21530 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
21531 SELF_CHECK (units[result].get () == one_ptr);
21532 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
21533 SELF_CHECK (units[result].get () == one_ptr);
21534 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
21535 SELF_CHECK (units[result].get () == two_ptr);
21536
21537 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
21538 SELF_CHECK (units[result].get () == three_ptr);
21539 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
21540 SELF_CHECK (units[result].get () == three_ptr);
21541 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
21542 SELF_CHECK (units[result].get () == four_ptr);
21543 }
21544
21545 }
21546 }
21547
21548 #endif /* GDB_SELF_TEST */
21549
21550 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
21551
21552 static void
21553 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
21554 enum language pretend_language)
21555 {
21556 struct attribute *attr;
21557
21558 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
21559
21560 /* Set the language we're debugging. */
21561 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
21562 enum language lang;
21563 if (cu->producer != nullptr
21564 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
21565 {
21566 /* The XLCL doesn't generate DW_LANG_OpenCL because this
21567 attribute is not standardised yet. As a workaround for the
21568 language detection we fall back to the DW_AT_producer
21569 string. */
21570 lang = language_opencl;
21571 }
21572 else if (cu->producer != nullptr
21573 && strstr (cu->producer, "GNU Go ") != NULL)
21574 {
21575 /* Similar hack for Go. */
21576 lang = language_go;
21577 }
21578 else if (attr != nullptr)
21579 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
21580 else
21581 lang = pretend_language;
21582
21583 cu->language_defn = language_def (lang);
21584
21585 switch (comp_unit_die->tag)
21586 {
21587 case DW_TAG_compile_unit:
21588 cu->per_cu->set_unit_type (DW_UT_compile);
21589 break;
21590 case DW_TAG_partial_unit:
21591 cu->per_cu->set_unit_type (DW_UT_partial);
21592 break;
21593 case DW_TAG_type_unit:
21594 cu->per_cu->set_unit_type (DW_UT_type);
21595 break;
21596 default:
21597 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
21598 dwarf_tag_name (comp_unit_die->tag),
21599 sect_offset_str (cu->per_cu->sect_off));
21600 }
21601
21602 cu->per_cu->set_lang (lang);
21603 }
21604
21605 /* See read.h. */
21606
21607 dwarf2_cu *
21608 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
21609 {
21610 auto it = m_dwarf2_cus.find (per_cu);
21611 if (it == m_dwarf2_cus.end ())
21612 return nullptr;
21613
21614 return it->second.get ();
21615 }
21616
21617 /* See read.h. */
21618
21619 void
21620 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
21621 std::unique_ptr<dwarf2_cu> cu)
21622 {
21623 gdb_assert (this->get_cu (per_cu) == nullptr);
21624
21625 m_dwarf2_cus[per_cu] = std::move (cu);
21626 }
21627
21628 /* See read.h. */
21629
21630 void
21631 dwarf2_per_objfile::age_comp_units ()
21632 {
21633 dwarf_read_debug_printf_v ("running");
21634
21635 /* This is not expected to be called in the middle of CU expansion. There is
21636 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
21637 loaded in memory. Calling age_comp_units while the queue is in use could
21638 make us free the DIEs for a CU that is in the queue and therefore break
21639 that invariant. */
21640 gdb_assert (!queue.has_value ());
21641
21642 /* Start by clearing all marks. */
21643 for (const auto &pair : m_dwarf2_cus)
21644 pair.second->clear_mark ();
21645
21646 /* Traverse all CUs, mark them and their dependencies if used recently
21647 enough. */
21648 for (const auto &pair : m_dwarf2_cus)
21649 {
21650 dwarf2_cu *cu = pair.second.get ();
21651
21652 cu->last_used++;
21653 if (cu->last_used <= dwarf_max_cache_age)
21654 cu->mark ();
21655 }
21656
21657 /* Delete all CUs still not marked. */
21658 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
21659 {
21660 dwarf2_cu *cu = it->second.get ();
21661
21662 if (!cu->is_marked ())
21663 {
21664 dwarf_read_debug_printf_v ("deleting old CU %s",
21665 sect_offset_str (cu->per_cu->sect_off));
21666 it = m_dwarf2_cus.erase (it);
21667 }
21668 else
21669 it++;
21670 }
21671 }
21672
21673 /* See read.h. */
21674
21675 void
21676 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
21677 {
21678 auto it = m_dwarf2_cus.find (per_cu);
21679 if (it == m_dwarf2_cus.end ())
21680 return;
21681
21682 m_dwarf2_cus.erase (it);
21683 }
21684
21685 dwarf2_per_objfile::~dwarf2_per_objfile ()
21686 {
21687 remove_all_cus ();
21688 }
21689
21690 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21691 We store these in a hash table separate from the DIEs, and preserve them
21692 when the DIEs are flushed out of cache.
21693
21694 The CU "per_cu" pointer is needed because offset alone is not enough to
21695 uniquely identify the type. A file may have multiple .debug_types sections,
21696 or the type may come from a DWO file. Furthermore, while it's more logical
21697 to use per_cu->section+offset, with Fission the section with the data is in
21698 the DWO file but we don't know that section at the point we need it.
21699 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21700 because we can enter the lookup routine, get_die_type_at_offset, from
21701 outside this file, and thus won't necessarily have PER_CU->cu.
21702 Fortunately, PER_CU is stable for the life of the objfile. */
21703
21704 struct dwarf2_per_cu_offset_and_type
21705 {
21706 const struct dwarf2_per_cu_data *per_cu;
21707 sect_offset sect_off;
21708 struct type *type;
21709 };
21710
21711 /* Hash function for a dwarf2_per_cu_offset_and_type. */
21712
21713 static hashval_t
21714 per_cu_offset_and_type_hash (const void *item)
21715 {
21716 const struct dwarf2_per_cu_offset_and_type *ofs
21717 = (const struct dwarf2_per_cu_offset_and_type *) item;
21718
21719 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
21720 }
21721
21722 /* Equality function for a dwarf2_per_cu_offset_and_type. */
21723
21724 static int
21725 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
21726 {
21727 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
21728 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
21729 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
21730 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
21731
21732 return (ofs_lhs->per_cu == ofs_rhs->per_cu
21733 && ofs_lhs->sect_off == ofs_rhs->sect_off);
21734 }
21735
21736 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21737 table if necessary. For convenience, return TYPE.
21738
21739 The DIEs reading must have careful ordering to:
21740 * Not cause infinite loops trying to read in DIEs as a prerequisite for
21741 reading current DIE.
21742 * Not trying to dereference contents of still incompletely read in types
21743 while reading in other DIEs.
21744 * Enable referencing still incompletely read in types just by a pointer to
21745 the type without accessing its fields.
21746
21747 Therefore caller should follow these rules:
21748 * Try to fetch any prerequisite types we may need to build this DIE type
21749 before building the type and calling set_die_type.
21750 * After building type call set_die_type for current DIE as soon as
21751 possible before fetching more types to complete the current type.
21752 * Make the type as complete as possible before fetching more types. */
21753
21754 static struct type *
21755 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21756 bool skip_data_location)
21757 {
21758 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21759 struct dwarf2_per_cu_offset_and_type **slot, ofs;
21760 struct objfile *objfile = per_objfile->objfile;
21761 struct attribute *attr;
21762 struct dynamic_prop prop;
21763
21764 /* For Ada types, make sure that the gnat-specific data is always
21765 initialized (if not already set). There are a few types where
21766 we should not be doing so, because the type-specific area is
21767 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21768 where the type-specific area is used to store the floatformat).
21769 But this is not a problem, because the gnat-specific information
21770 is actually not needed for these types. */
21771 if (need_gnat_info (cu)
21772 && type->code () != TYPE_CODE_FUNC
21773 && type->code () != TYPE_CODE_FLT
21774 && type->code () != TYPE_CODE_METHODPTR
21775 && type->code () != TYPE_CODE_MEMBERPTR
21776 && type->code () != TYPE_CODE_METHOD
21777 && type->code () != TYPE_CODE_FIXED_POINT
21778 && !HAVE_GNAT_AUX_INFO (type))
21779 INIT_GNAT_SPECIFIC (type);
21780
21781 /* Read DW_AT_allocated and set in type. */
21782 attr = dwarf2_attr (die, DW_AT_allocated, cu);
21783 if (attr != NULL)
21784 {
21785 struct type *prop_type = cu->addr_sized_int_type (false);
21786 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21787 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
21788 }
21789
21790 /* Read DW_AT_associated and set in type. */
21791 attr = dwarf2_attr (die, DW_AT_associated, cu);
21792 if (attr != NULL)
21793 {
21794 struct type *prop_type = cu->addr_sized_int_type (false);
21795 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21796 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
21797 }
21798
21799 /* Read DW_AT_rank and set in type. */
21800 attr = dwarf2_attr (die, DW_AT_rank, cu);
21801 if (attr != NULL)
21802 {
21803 struct type *prop_type = cu->addr_sized_int_type (false);
21804 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21805 type->add_dyn_prop (DYN_PROP_RANK, prop);
21806 }
21807
21808 /* Read DW_AT_data_location and set in type. */
21809 if (!skip_data_location)
21810 {
21811 attr = dwarf2_attr (die, DW_AT_data_location, cu);
21812 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
21813 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
21814 }
21815
21816 if (per_objfile->die_type_hash == NULL)
21817 per_objfile->die_type_hash
21818 = htab_up (htab_create_alloc (127,
21819 per_cu_offset_and_type_hash,
21820 per_cu_offset_and_type_eq,
21821 NULL, xcalloc, xfree));
21822
21823 ofs.per_cu = cu->per_cu;
21824 ofs.sect_off = die->sect_off;
21825 ofs.type = type;
21826 slot = (struct dwarf2_per_cu_offset_and_type **)
21827 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
21828 if (*slot)
21829 complaint (_("A problem internal to GDB: DIE %s has type already set"),
21830 sect_offset_str (die->sect_off));
21831 *slot = XOBNEW (&objfile->objfile_obstack,
21832 struct dwarf2_per_cu_offset_and_type);
21833 **slot = ofs;
21834 return type;
21835 }
21836
21837 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
21838 or return NULL if the die does not have a saved type. */
21839
21840 static struct type *
21841 get_die_type_at_offset (sect_offset sect_off,
21842 dwarf2_per_cu_data *per_cu,
21843 dwarf2_per_objfile *per_objfile)
21844 {
21845 struct dwarf2_per_cu_offset_and_type *slot, ofs;
21846
21847 if (per_objfile->die_type_hash == NULL)
21848 return NULL;
21849
21850 ofs.per_cu = per_cu;
21851 ofs.sect_off = sect_off;
21852 slot = ((struct dwarf2_per_cu_offset_and_type *)
21853 htab_find (per_objfile->die_type_hash.get (), &ofs));
21854 if (slot)
21855 return slot->type;
21856 else
21857 return NULL;
21858 }
21859
21860 /* Look up the type for DIE in CU in die_type_hash,
21861 or return NULL if DIE does not have a saved type. */
21862
21863 static struct type *
21864 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
21865 {
21866 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
21867 }
21868
21869 struct cmd_list_element *set_dwarf_cmdlist;
21870 struct cmd_list_element *show_dwarf_cmdlist;
21871
21872 static void
21873 show_check_physname (struct ui_file *file, int from_tty,
21874 struct cmd_list_element *c, const char *value)
21875 {
21876 gdb_printf (file,
21877 _("Whether to check \"physname\" is %s.\n"),
21878 value);
21879 }
21880
21881 void _initialize_dwarf2_read ();
21882 void
21883 _initialize_dwarf2_read ()
21884 {
21885 add_setshow_prefix_cmd ("dwarf", class_maintenance,
21886 _("\
21887 Set DWARF specific variables.\n\
21888 Configure DWARF variables such as the cache size."),
21889 _("\
21890 Show DWARF specific variables.\n\
21891 Show DWARF variables such as the cache size."),
21892 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
21893 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
21894
21895 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21896 &dwarf_max_cache_age, _("\
21897 Set the upper bound on the age of cached DWARF compilation units."), _("\
21898 Show the upper bound on the age of cached DWARF compilation units."), _("\
21899 A higher limit means that cached compilation units will be stored\n\
21900 in memory longer, and more total memory will be used. Zero disables\n\
21901 caching, which can slow down startup."),
21902 NULL,
21903 show_dwarf_max_cache_age,
21904 &set_dwarf_cmdlist,
21905 &show_dwarf_cmdlist);
21906
21907 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
21908 Set debugging of the DWARF reader."), _("\
21909 Show debugging of the DWARF reader."), _("\
21910 When enabled (non-zero), debugging messages are printed during DWARF\n\
21911 reading and symtab expansion. A value of 1 (one) provides basic\n\
21912 information. A value greater than 1 provides more verbose information."),
21913 NULL,
21914 NULL,
21915 &setdebuglist, &showdebuglist);
21916
21917 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
21918 Set debugging of the DWARF DIE reader."), _("\
21919 Show debugging of the DWARF DIE reader."), _("\
21920 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21921 The value is the maximum depth to print."),
21922 NULL,
21923 NULL,
21924 &setdebuglist, &showdebuglist);
21925
21926 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
21927 Set debugging of the dwarf line reader."), _("\
21928 Show debugging of the dwarf line reader."), _("\
21929 When enabled (non-zero), line number entries are dumped as they are read in.\n\
21930 A value of 1 (one) provides basic information.\n\
21931 A value greater than 1 provides more verbose information."),
21932 NULL,
21933 NULL,
21934 &setdebuglist, &showdebuglist);
21935
21936 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21937 Set cross-checking of \"physname\" code against demangler."), _("\
21938 Show cross-checking of \"physname\" code against demangler."), _("\
21939 When enabled, GDB's internal \"physname\" code is checked against\n\
21940 the demangler."),
21941 NULL, show_check_physname,
21942 &setdebuglist, &showdebuglist);
21943
21944 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21945 &dwarf2_locexpr_funcs);
21946 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21947 &dwarf2_loclist_funcs);
21948
21949 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21950 &dwarf2_block_frame_base_locexpr_funcs);
21951 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21952 &dwarf2_block_frame_base_loclist_funcs);
21953
21954 #if GDB_SELF_TEST
21955 selftests::register_test ("dw2_expand_symtabs_matching",
21956 selftests::dw2_expand_symtabs_matching::run_test);
21957 selftests::register_test ("dwarf2_find_containing_comp_unit",
21958 selftests::find_containing_comp_unit::run_test);
21959 #endif
21960 }