]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
gdb: use decltype instead of typeof in dwarf2/read.c
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2022 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/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "c-lang.h"
68 #include "go-lang.h"
69 #include "valprint.h"
70 #include "gdbcore.h" /* for gnutarget */
71 #include "gdb/gdb-index.h"
72 #include "gdb_bfd.h"
73 #include "f-lang.h"
74 #include "source.h"
75 #include "build-id.h"
76 #include "namespace.h"
77 #include "gdbsupport/function-view.h"
78 #include "gdbsupport/gdb_optional.h"
79 #include "gdbsupport/underlying.h"
80 #include "gdbsupport/hash_enum.h"
81 #include "filename-seen-cache.h"
82 #include "producer.h"
83 #include <fcntl.h>
84 #include <algorithm>
85 #include <unordered_map>
86 #include "gdbsupport/selftest.h"
87 #include "rust-lang.h"
88 #include "gdbsupport/pathstuff.h"
89 #include "count-one-bits.h"
90 #include <unordered_set>
91 #include "dwarf2/abbrev-cache.h"
92 #include "cooked-index.h"
93 #include "split-name.h"
94 #include "gdbsupport/parallel-for.h"
95 #include "gdbsupport/thread-pool.h"
96
97 /* When == 1, print basic high level tracing messages.
98 When > 1, be more verbose.
99 This is in contrast to the low level DIE reading of dwarf_die_debug. */
100 static unsigned int dwarf_read_debug = 0;
101
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
103
104 #define dwarf_read_debug_printf(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
107
108 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
109
110 #define dwarf_read_debug_printf_v(fmt, ...) \
111 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
112 ##__VA_ARGS__)
113
114 /* When non-zero, dump DIEs after they are read in. */
115 static unsigned int dwarf_die_debug = 0;
116
117 /* When non-zero, dump line number entries as they are read in. */
118 unsigned int dwarf_line_debug = 0;
119
120 /* When true, cross-check physname against demangler. */
121 static bool check_physname = false;
122
123 /* When true, do not reject deprecated .gdb_index sections. */
124 static bool use_deprecated_index_sections = false;
125
126 /* This is used to store the data that is always per objfile. */
127 static const objfile_key<dwarf2_per_objfile> 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 struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
138 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
139
140 /* The "aclass" indices for various kinds of computed DWARF symbols. */
141
142 static int dwarf2_locexpr_index;
143 static int dwarf2_loclist_index;
144 static int dwarf2_locexpr_block_index;
145 static int dwarf2_loclist_block_index;
146
147 /* Size of .debug_loclists section header for 32-bit DWARF format. */
148 #define LOCLIST_HEADER_SIZE32 12
149
150 /* Size of .debug_loclists section header for 64-bit DWARF format. */
151 #define LOCLIST_HEADER_SIZE64 20
152
153 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
154 #define RNGLIST_HEADER_SIZE32 12
155
156 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
157 #define RNGLIST_HEADER_SIZE64 20
158
159 /* An index into a (C++) symbol name component in a symbol name as
160 recorded in the mapped_index's symbol table. For each C++ symbol
161 in the symbol table, we record one entry for the start of each
162 component in the symbol in a table of name components, and then
163 sort the table, in order to be able to binary search symbol names,
164 ignoring leading namespaces, both completion and regular look up.
165 For example, for symbol "A::B::C", we'll have an entry that points
166 to "A::B::C", another that points to "B::C", and another for "C".
167 Note that function symbols in GDB index have no parameter
168 information, just the function/method names. You can convert a
169 name_component to a "const char *" using the
170 'mapped_index::symbol_name_at(offset_type)' method. */
171
172 struct name_component
173 {
174 /* Offset in the symbol name where the component starts. Stored as
175 a (32-bit) offset instead of a pointer to save memory and improve
176 locality on 64-bit architectures. */
177 offset_type name_offset;
178
179 /* The symbol's index in the symbol and constant pool tables of a
180 mapped_index. */
181 offset_type idx;
182 };
183
184 /* Base class containing bits shared by both .gdb_index and
185 .debug_name indexes. */
186
187 struct mapped_index_base
188 {
189 mapped_index_base () = default;
190 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
191
192 /* The name_component table (a sorted vector). See name_component's
193 description above. */
194 std::vector<name_component> name_components;
195
196 /* How NAME_COMPONENTS is sorted. */
197 enum case_sensitivity name_components_casing;
198
199 /* Return the number of names in the symbol table. */
200 virtual size_t symbol_name_count () const = 0;
201
202 /* Get the name of the symbol at IDX in the symbol table. */
203 virtual const char *symbol_name_at
204 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
205
206 /* Return whether the name at IDX in the symbol table should be
207 ignored. */
208 virtual bool symbol_name_slot_invalid (offset_type idx) const
209 {
210 return false;
211 }
212
213 /* Build the symbol name component sorted vector, if we haven't
214 yet. */
215 void build_name_components (dwarf2_per_objfile *per_objfile);
216
217 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
218 possible matches for LN_NO_PARAMS in the name component
219 vector. */
220 std::pair<std::vector<name_component>::const_iterator,
221 std::vector<name_component>::const_iterator>
222 find_name_components_bounds (const lookup_name_info &ln_no_params,
223 enum language lang,
224 dwarf2_per_objfile *per_objfile) const;
225
226 /* Prevent deleting/destroying via a base class pointer. */
227 protected:
228 ~mapped_index_base() = default;
229 };
230
231 /* This is a view into the index that converts from bytes to an
232 offset_type, and allows indexing. Unaligned bytes are specifically
233 allowed here, and handled via unpacking. */
234
235 class offset_view
236 {
237 public:
238 offset_view () = default;
239
240 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
241 : m_bytes (bytes)
242 {
243 }
244
245 /* Extract the INDEXth offset_type from the array. */
246 offset_type operator[] (size_t index) const
247 {
248 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
249 return (offset_type) extract_unsigned_integer (bytes,
250 sizeof (offset_type),
251 BFD_ENDIAN_LITTLE);
252 }
253
254 /* Return the number of offset_types in this array. */
255 size_t size () const
256 {
257 return m_bytes.size () / sizeof (offset_type);
258 }
259
260 /* Return true if this view is empty. */
261 bool empty () const
262 {
263 return m_bytes.empty ();
264 }
265
266 private:
267 /* The underlying bytes. */
268 gdb::array_view<const gdb_byte> m_bytes;
269 };
270
271 /* A description of the mapped index. The file format is described in
272 a comment by the code that writes the index. */
273 struct mapped_index final : public mapped_index_base
274 {
275 /* Index data format version. */
276 int version = 0;
277
278 /* The address table data. */
279 gdb::array_view<const gdb_byte> address_table;
280
281 /* The symbol table, implemented as a hash table. */
282 offset_view symbol_table;
283
284 /* A pointer to the constant pool. */
285 gdb::array_view<const gdb_byte> constant_pool;
286
287 /* Return the index into the constant pool of the name of the IDXth
288 symbol in the symbol table. */
289 offset_type symbol_name_index (offset_type idx) const
290 {
291 return symbol_table[2 * idx];
292 }
293
294 /* Return the index into the constant pool of the CU vector of the
295 IDXth symbol in the symbol table. */
296 offset_type symbol_vec_index (offset_type idx) const
297 {
298 return symbol_table[2 * idx + 1];
299 }
300
301 bool symbol_name_slot_invalid (offset_type idx) const override
302 {
303 return (symbol_name_index (idx) == 0
304 && symbol_vec_index (idx) == 0);
305 }
306
307 /* Convenience method to get at the name of the symbol at IDX in the
308 symbol table. */
309 const char *symbol_name_at
310 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
311 {
312 return (const char *) (this->constant_pool.data ()
313 + symbol_name_index (idx));
314 }
315
316 size_t symbol_name_count () const override
317 { return this->symbol_table.size () / 2; }
318 };
319
320 /* A description of the mapped .debug_names.
321 Uninitialized map has CU_COUNT 0. */
322 struct mapped_debug_names final : public mapped_index_base
323 {
324 bfd_endian dwarf5_byte_order;
325 bool dwarf5_is_dwarf64;
326 bool augmentation_is_gdb;
327 uint8_t offset_size;
328 uint32_t cu_count = 0;
329 uint32_t tu_count, bucket_count, name_count;
330 const gdb_byte *cu_table_reordered, *tu_table_reordered;
331 const uint32_t *bucket_table_reordered, *hash_table_reordered;
332 const gdb_byte *name_table_string_offs_reordered;
333 const gdb_byte *name_table_entry_offs_reordered;
334 const gdb_byte *entry_pool;
335
336 struct index_val
337 {
338 ULONGEST dwarf_tag;
339 struct attr
340 {
341 /* Attribute name DW_IDX_*. */
342 ULONGEST dw_idx;
343
344 /* Attribute form DW_FORM_*. */
345 ULONGEST form;
346
347 /* Value if FORM is DW_FORM_implicit_const. */
348 LONGEST implicit_const;
349 };
350 std::vector<attr> attr_vec;
351 };
352
353 std::unordered_map<ULONGEST, index_val> abbrev_map;
354
355 const char *namei_to_name
356 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
357
358 /* Implementation of the mapped_index_base virtual interface, for
359 the name_components cache. */
360
361 const char *symbol_name_at
362 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
363 { return namei_to_name (idx, per_objfile); }
364
365 size_t symbol_name_count () const override
366 { return this->name_count; }
367 };
368
369 /* See dwarf2/read.h. */
370
371 dwarf2_per_objfile *
372 get_dwarf2_per_objfile (struct objfile *objfile)
373 {
374 return dwarf2_objfile_data_key.get (objfile);
375 }
376
377 /* Default names of the debugging sections. */
378
379 /* Note that if the debugging section has been compressed, it might
380 have a name like .zdebug_info. */
381
382 const struct dwarf2_debug_sections dwarf2_elf_names =
383 {
384 { ".debug_info", ".zdebug_info" },
385 { ".debug_abbrev", ".zdebug_abbrev" },
386 { ".debug_line", ".zdebug_line" },
387 { ".debug_loc", ".zdebug_loc" },
388 { ".debug_loclists", ".zdebug_loclists" },
389 { ".debug_macinfo", ".zdebug_macinfo" },
390 { ".debug_macro", ".zdebug_macro" },
391 { ".debug_str", ".zdebug_str" },
392 { ".debug_str_offsets", ".zdebug_str_offsets" },
393 { ".debug_line_str", ".zdebug_line_str" },
394 { ".debug_ranges", ".zdebug_ranges" },
395 { ".debug_rnglists", ".zdebug_rnglists" },
396 { ".debug_types", ".zdebug_types" },
397 { ".debug_addr", ".zdebug_addr" },
398 { ".debug_frame", ".zdebug_frame" },
399 { ".eh_frame", NULL },
400 { ".gdb_index", ".zgdb_index" },
401 { ".debug_names", ".zdebug_names" },
402 { ".debug_aranges", ".zdebug_aranges" },
403 23
404 };
405
406 /* List of DWO/DWP sections. */
407
408 static const struct dwop_section_names
409 {
410 struct dwarf2_section_names abbrev_dwo;
411 struct dwarf2_section_names info_dwo;
412 struct dwarf2_section_names line_dwo;
413 struct dwarf2_section_names loc_dwo;
414 struct dwarf2_section_names loclists_dwo;
415 struct dwarf2_section_names macinfo_dwo;
416 struct dwarf2_section_names macro_dwo;
417 struct dwarf2_section_names rnglists_dwo;
418 struct dwarf2_section_names str_dwo;
419 struct dwarf2_section_names str_offsets_dwo;
420 struct dwarf2_section_names types_dwo;
421 struct dwarf2_section_names cu_index;
422 struct dwarf2_section_names tu_index;
423 }
424 dwop_section_names =
425 {
426 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
427 { ".debug_info.dwo", ".zdebug_info.dwo" },
428 { ".debug_line.dwo", ".zdebug_line.dwo" },
429 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
430 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
431 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
432 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
433 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
434 { ".debug_str.dwo", ".zdebug_str.dwo" },
435 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
436 { ".debug_types.dwo", ".zdebug_types.dwo" },
437 { ".debug_cu_index", ".zdebug_cu_index" },
438 { ".debug_tu_index", ".zdebug_tu_index" },
439 };
440
441 /* local data types */
442
443 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
444 begin with a header, which contains the following information. */
445 struct loclists_rnglists_header
446 {
447 /* A 4-byte or 12-byte length containing the length of the
448 set of entries for this compilation unit, not including the
449 length field itself. */
450 unsigned int length;
451
452 /* A 2-byte version identifier. */
453 short version;
454
455 /* A 1-byte unsigned integer containing the size in bytes of an address on
456 the target system. */
457 unsigned char addr_size;
458
459 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
460 on the target system. */
461 unsigned char segment_collector_size;
462
463 /* A 4-byte count of the number of offsets that follow the header. */
464 unsigned int offset_entry_count;
465 };
466
467 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
468 This includes type_unit_group and quick_file_names. */
469
470 struct stmt_list_hash
471 {
472 /* The DWO unit this table is from or NULL if there is none. */
473 struct dwo_unit *dwo_unit;
474
475 /* Offset in .debug_line or .debug_line.dwo. */
476 sect_offset line_sect_off;
477 };
478
479 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
480 an object of this type. This contains elements of type unit groups
481 that can be shared across objfiles. The non-shareable parts are in
482 type_unit_group_unshareable. */
483
484 struct type_unit_group
485 {
486 /* The data used to construct the hash key. */
487 struct stmt_list_hash hash {};
488 };
489
490 /* These sections are what may appear in a (real or virtual) DWO file. */
491
492 struct dwo_sections
493 {
494 struct dwarf2_section_info abbrev;
495 struct dwarf2_section_info line;
496 struct dwarf2_section_info loc;
497 struct dwarf2_section_info loclists;
498 struct dwarf2_section_info macinfo;
499 struct dwarf2_section_info macro;
500 struct dwarf2_section_info rnglists;
501 struct dwarf2_section_info str;
502 struct dwarf2_section_info str_offsets;
503 /* In the case of a virtual DWO file, these two are unused. */
504 struct dwarf2_section_info info;
505 std::vector<dwarf2_section_info> types;
506 };
507
508 /* CUs/TUs in DWP/DWO files. */
509
510 struct dwo_unit
511 {
512 /* Backlink to the containing struct dwo_file. */
513 struct dwo_file *dwo_file;
514
515 /* The "id" that distinguishes this CU/TU.
516 .debug_info calls this "dwo_id", .debug_types calls this "signature".
517 Since signatures came first, we stick with it for consistency. */
518 ULONGEST signature;
519
520 /* The section this CU/TU lives in, in the DWO file. */
521 struct dwarf2_section_info *section;
522
523 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
524 sect_offset sect_off;
525 unsigned int length;
526
527 /* For types, offset in the type's DIE of the type defined by this TU. */
528 cu_offset type_offset_in_tu;
529 };
530
531 /* include/dwarf2.h defines the DWP section codes.
532 It defines a max value but it doesn't define a min value, which we
533 use for error checking, so provide one. */
534
535 enum dwp_v2_section_ids
536 {
537 DW_SECT_MIN = 1
538 };
539
540 /* Data for one DWO file.
541
542 This includes virtual DWO files (a virtual DWO file is a DWO file as it
543 appears in a DWP file). DWP files don't really have DWO files per se -
544 comdat folding of types "loses" the DWO file they came from, and from
545 a high level view DWP files appear to contain a mass of random types.
546 However, to maintain consistency with the non-DWP case we pretend DWP
547 files contain virtual DWO files, and we assign each TU with one virtual
548 DWO file (generally based on the line and abbrev section offsets -
549 a heuristic that seems to work in practice). */
550
551 struct dwo_file
552 {
553 dwo_file () = default;
554 DISABLE_COPY_AND_ASSIGN (dwo_file);
555
556 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
557 For virtual DWO files the name is constructed from the section offsets
558 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
559 from related CU+TUs. */
560 const char *dwo_name = nullptr;
561
562 /* The DW_AT_comp_dir attribute. */
563 const char *comp_dir = nullptr;
564
565 /* The bfd, when the file is open. Otherwise this is NULL.
566 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
567 gdb_bfd_ref_ptr dbfd;
568
569 /* The sections that make up this DWO file.
570 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
571 sections (for lack of a better name). */
572 struct dwo_sections sections {};
573
574 /* The CUs in the file.
575 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
576 an extension to handle LLVM's Link Time Optimization output (where
577 multiple source files may be compiled into a single object/dwo pair). */
578 htab_up cus;
579
580 /* Table of TUs in the file.
581 Each element is a struct dwo_unit. */
582 htab_up tus;
583 };
584
585 /* These sections are what may appear in a DWP file. */
586
587 struct dwp_sections
588 {
589 /* These are used by all DWP versions (1, 2 and 5). */
590 struct dwarf2_section_info str;
591 struct dwarf2_section_info cu_index;
592 struct dwarf2_section_info tu_index;
593
594 /* These are only used by DWP version 2 and version 5 files.
595 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
596 sections are referenced by section number, and are not recorded here.
597 In DWP version 2 or 5 there is at most one copy of all these sections,
598 each section being (effectively) comprised of the concatenation of all of
599 the individual sections that exist in the version 1 format.
600 To keep the code simple we treat each of these concatenated pieces as a
601 section itself (a virtual section?). */
602 struct dwarf2_section_info abbrev;
603 struct dwarf2_section_info info;
604 struct dwarf2_section_info line;
605 struct dwarf2_section_info loc;
606 struct dwarf2_section_info loclists;
607 struct dwarf2_section_info macinfo;
608 struct dwarf2_section_info macro;
609 struct dwarf2_section_info rnglists;
610 struct dwarf2_section_info str_offsets;
611 struct dwarf2_section_info types;
612 };
613
614 /* These sections are what may appear in a virtual DWO file in DWP version 1.
615 A virtual DWO file is a DWO file as it appears in a DWP file. */
616
617 struct virtual_v1_dwo_sections
618 {
619 struct dwarf2_section_info abbrev;
620 struct dwarf2_section_info line;
621 struct dwarf2_section_info loc;
622 struct dwarf2_section_info macinfo;
623 struct dwarf2_section_info macro;
624 struct dwarf2_section_info str_offsets;
625 /* Each DWP hash table entry records one CU or one TU.
626 That is recorded here, and copied to dwo_unit.section. */
627 struct dwarf2_section_info info_or_types;
628 };
629
630 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
631 In version 2, the sections of the DWO files are concatenated together
632 and stored in one section of that name. Thus each ELF section contains
633 several "virtual" sections. */
634
635 struct virtual_v2_or_v5_dwo_sections
636 {
637 bfd_size_type abbrev_offset;
638 bfd_size_type abbrev_size;
639
640 bfd_size_type line_offset;
641 bfd_size_type line_size;
642
643 bfd_size_type loc_offset;
644 bfd_size_type loc_size;
645
646 bfd_size_type loclists_offset;
647 bfd_size_type loclists_size;
648
649 bfd_size_type macinfo_offset;
650 bfd_size_type macinfo_size;
651
652 bfd_size_type macro_offset;
653 bfd_size_type macro_size;
654
655 bfd_size_type rnglists_offset;
656 bfd_size_type rnglists_size;
657
658 bfd_size_type str_offsets_offset;
659 bfd_size_type str_offsets_size;
660
661 /* Each DWP hash table entry records one CU or one TU.
662 That is recorded here, and copied to dwo_unit.section. */
663 bfd_size_type info_or_types_offset;
664 bfd_size_type info_or_types_size;
665 };
666
667 /* Contents of DWP hash tables. */
668
669 struct dwp_hash_table
670 {
671 uint32_t version, nr_columns;
672 uint32_t nr_units, nr_slots;
673 const gdb_byte *hash_table, *unit_table;
674 union
675 {
676 struct
677 {
678 const gdb_byte *indices;
679 } v1;
680 struct
681 {
682 /* This is indexed by column number and gives the id of the section
683 in that column. */
684 #define MAX_NR_V2_DWO_SECTIONS \
685 (1 /* .debug_info or .debug_types */ \
686 + 1 /* .debug_abbrev */ \
687 + 1 /* .debug_line */ \
688 + 1 /* .debug_loc */ \
689 + 1 /* .debug_str_offsets */ \
690 + 1 /* .debug_macro or .debug_macinfo */)
691 int section_ids[MAX_NR_V2_DWO_SECTIONS];
692 const gdb_byte *offsets;
693 const gdb_byte *sizes;
694 } v2;
695 struct
696 {
697 /* This is indexed by column number and gives the id of the section
698 in that column. */
699 #define MAX_NR_V5_DWO_SECTIONS \
700 (1 /* .debug_info */ \
701 + 1 /* .debug_abbrev */ \
702 + 1 /* .debug_line */ \
703 + 1 /* .debug_loclists */ \
704 + 1 /* .debug_str_offsets */ \
705 + 1 /* .debug_macro */ \
706 + 1 /* .debug_rnglists */)
707 int section_ids[MAX_NR_V5_DWO_SECTIONS];
708 const gdb_byte *offsets;
709 const gdb_byte *sizes;
710 } v5;
711 } section_pool;
712 };
713
714 /* Data for one DWP file. */
715
716 struct dwp_file
717 {
718 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
719 : name (name_),
720 dbfd (std::move (abfd))
721 {
722 }
723
724 /* Name of the file. */
725 const char *name;
726
727 /* File format version. */
728 int version = 0;
729
730 /* The bfd. */
731 gdb_bfd_ref_ptr dbfd;
732
733 /* Section info for this file. */
734 struct dwp_sections sections {};
735
736 /* Table of CUs in the file. */
737 const struct dwp_hash_table *cus = nullptr;
738
739 /* Table of TUs in the file. */
740 const struct dwp_hash_table *tus = nullptr;
741
742 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
743 htab_up loaded_cus;
744 htab_up loaded_tus;
745
746 /* Table to map ELF section numbers to their sections.
747 This is only needed for the DWP V1 file format. */
748 unsigned int num_sections = 0;
749 asection **elf_sections = nullptr;
750 };
751
752 /* Struct used to pass misc. parameters to read_die_and_children, et
753 al. which are used for both .debug_info and .debug_types dies.
754 All parameters here are unchanging for the life of the call. This
755 struct exists to abstract away the constant parameters of die reading. */
756
757 struct die_reader_specs
758 {
759 /* The bfd of die_section. */
760 bfd *abfd;
761
762 /* The CU of the DIE we are parsing. */
763 struct dwarf2_cu *cu;
764
765 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
766 struct dwo_file *dwo_file;
767
768 /* The section the die comes from.
769 This is either .debug_info or .debug_types, or the .dwo variants. */
770 struct dwarf2_section_info *die_section;
771
772 /* die_section->buffer. */
773 const gdb_byte *buffer;
774
775 /* The end of the buffer. */
776 const gdb_byte *buffer_end;
777
778 /* The abbreviation table to use when reading the DIEs. */
779 struct abbrev_table *abbrev_table;
780 };
781
782 /* A subclass of die_reader_specs that holds storage and has complex
783 constructor and destructor behavior. */
784
785 class cutu_reader : public die_reader_specs
786 {
787 public:
788
789 cutu_reader (dwarf2_per_cu_data *this_cu,
790 dwarf2_per_objfile *per_objfile,
791 struct abbrev_table *abbrev_table,
792 dwarf2_cu *existing_cu,
793 bool skip_partial,
794 abbrev_cache *cache = nullptr);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
797 dwarf2_per_objfile *per_objfile,
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 cutu_reader (cutu_reader &&) = default;
804
805 const gdb_byte *info_ptr = nullptr;
806 struct die_info *comp_unit_die = nullptr;
807 bool dummy_p = false;
808
809 /* Release the new CU, putting it on the chain. This cannot be done
810 for dummy CUs. */
811 void keep ();
812
813 /* Release the abbrev table, transferring ownership to the
814 caller. */
815 abbrev_table_up release_abbrev_table ()
816 {
817 return std::move (m_abbrev_table_holder);
818 }
819
820 private:
821 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
822 dwarf2_per_objfile *per_objfile,
823 dwarf2_cu *existing_cu);
824
825 struct dwarf2_per_cu_data *m_this_cu;
826 std::unique_ptr<dwarf2_cu> m_new_cu;
827
828 /* The ordinary abbreviation table. */
829 abbrev_table_up m_abbrev_table_holder;
830
831 /* The DWO abbreviation table. */
832 abbrev_table_up m_dwo_abbrev_table;
833 };
834
835 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
836 but this would require a corresponding change in unpack_field_as_long
837 and friends. */
838 static int bits_per_byte = 8;
839
840 struct variant_part_builder;
841
842 /* When reading a variant, we track a bit more information about the
843 field, and store it in an object of this type. */
844
845 struct variant_field
846 {
847 int first_field = -1;
848 int last_field = -1;
849
850 /* A variant can contain other variant parts. */
851 std::vector<variant_part_builder> variant_parts;
852
853 /* If we see a DW_TAG_variant, then this will be set if this is the
854 default branch. */
855 bool default_branch = false;
856 /* If we see a DW_AT_discr_value, then this will be the discriminant
857 value. */
858 ULONGEST discriminant_value = 0;
859 /* If we see a DW_AT_discr_list, then this is a pointer to the list
860 data. */
861 struct dwarf_block *discr_list_data = nullptr;
862 };
863
864 /* This represents a DW_TAG_variant_part. */
865
866 struct variant_part_builder
867 {
868 /* The offset of the discriminant field. */
869 sect_offset discriminant_offset {};
870
871 /* Variants that are direct children of this variant part. */
872 std::vector<variant_field> variants;
873
874 /* True if we're currently reading a variant. */
875 bool processing_variant = false;
876 };
877
878 struct nextfield
879 {
880 int accessibility = 0;
881 int virtuality = 0;
882 /* Variant parts need to find the discriminant, which is a DIE
883 reference. We track the section offset of each field to make
884 this link. */
885 sect_offset offset;
886 struct field field {};
887 };
888
889 struct fnfieldlist
890 {
891 const char *name = nullptr;
892 std::vector<struct fn_field> fnfields;
893 };
894
895 /* The routines that read and process dies for a C struct or C++ class
896 pass lists of data member fields and lists of member function fields
897 in an instance of a field_info structure, as defined below. */
898 struct field_info
899 {
900 /* List of data member and baseclasses fields. */
901 std::vector<struct nextfield> fields;
902 std::vector<struct nextfield> baseclasses;
903
904 /* Set if the accessibility of one of the fields is not public. */
905 bool non_public_fields = false;
906
907 /* Member function fieldlist array, contains name of possibly overloaded
908 member function, number of overloaded member functions and a pointer
909 to the head of the member function field chain. */
910 std::vector<struct fnfieldlist> fnfieldlists;
911
912 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
913 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
914 std::vector<struct decl_field> typedef_field_list;
915
916 /* Nested types defined by this class and the number of elements in this
917 list. */
918 std::vector<struct decl_field> nested_types_list;
919
920 /* If non-null, this is the variant part we are currently
921 reading. */
922 variant_part_builder *current_variant_part = nullptr;
923 /* This holds all the top-level variant parts attached to the type
924 we're reading. */
925 std::vector<variant_part_builder> variant_parts;
926
927 /* Return the total number of fields (including baseclasses). */
928 int nfields () const
929 {
930 return fields.size () + baseclasses.size ();
931 }
932 };
933
934 /* Loaded secondary compilation units are kept in memory until they
935 have not been referenced for the processing of this many
936 compilation units. Set this to zero to disable caching. Cache
937 sizes of up to at least twenty will improve startup time for
938 typical inter-CU-reference binaries, at an obvious memory cost. */
939 static int dwarf_max_cache_age = 5;
940 static void
941 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
942 struct cmd_list_element *c, const char *value)
943 {
944 gdb_printf (file, _("The upper bound on the age of cached "
945 "DWARF compilation units is %s.\n"),
946 value);
947 }
948 \f
949 /* local function prototypes */
950
951 static void dwarf2_find_base_address (struct die_info *die,
952 struct dwarf2_cu *cu);
953
954 class cooked_index_storage;
955 static void build_type_psymtabs_reader (cutu_reader *reader,
956 cooked_index_storage *storage);
957
958 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
959
960 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
961
962 static const gdb_byte *read_attribute (const struct die_reader_specs *,
963 struct attribute *,
964 const struct attr_abbrev *,
965 const gdb_byte *);
966
967 static void read_attribute_reprocess (const struct die_reader_specs *reader,
968 struct attribute *attr, dwarf_tag tag);
969
970 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
971
972 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
973 dwarf2_section_info *, sect_offset);
974
975 static const char *read_indirect_string
976 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
977 const struct comp_unit_head *, unsigned int *);
978
979 static const char *read_indirect_string_at_offset
980 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
981
982 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
983 const gdb_byte *,
984 unsigned int *);
985
986 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
987 ULONGEST str_index);
988
989 static const char *read_stub_str_index (struct dwarf2_cu *cu,
990 ULONGEST str_index);
991
992 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
993 struct dwarf2_cu *);
994
995 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
996 struct dwarf2_cu *cu);
997
998 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
999
1000 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1001 struct dwarf2_cu *cu);
1002
1003 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1004
1005 static struct die_info *die_specification (struct die_info *die,
1006 struct dwarf2_cu **);
1007
1008 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1009 struct dwarf2_cu *cu);
1010
1011 static void dwarf_decode_lines (struct line_header *,
1012 struct dwarf2_cu *,
1013 CORE_ADDR, int decode_mapping);
1014
1015 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1016 const char *);
1017
1018 static struct symbol *new_symbol (struct die_info *, struct type *,
1019 struct dwarf2_cu *, struct symbol * = NULL);
1020
1021 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1022 struct dwarf2_cu *);
1023
1024 static void dwarf2_const_value_attr (const struct attribute *attr,
1025 struct type *type,
1026 const char *name,
1027 struct obstack *obstack,
1028 struct dwarf2_cu *cu, LONGEST *value,
1029 const gdb_byte **bytes,
1030 struct dwarf2_locexpr_baton **baton);
1031
1032 static struct type *read_subrange_index_type (struct die_info *die,
1033 struct dwarf2_cu *cu);
1034
1035 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1036
1037 static int need_gnat_info (struct dwarf2_cu *);
1038
1039 static struct type *die_descriptive_type (struct die_info *,
1040 struct dwarf2_cu *);
1041
1042 static void set_descriptive_type (struct type *, struct die_info *,
1043 struct dwarf2_cu *);
1044
1045 static struct type *die_containing_type (struct die_info *,
1046 struct dwarf2_cu *);
1047
1048 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1049 struct dwarf2_cu *);
1050
1051 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1052
1053 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1054
1055 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1056
1057 static char *typename_concat (struct obstack *obs, const char *prefix,
1058 const char *suffix, int physname,
1059 struct dwarf2_cu *cu);
1060
1061 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1062
1063 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1064
1065 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1066
1067 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1068
1069 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1070
1071 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1072
1073 /* Return the .debug_loclists section to use for cu. */
1074 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1075
1076 /* Return the .debug_rnglists section to use for cu. */
1077 static struct dwarf2_section_info *cu_debug_rnglists_section
1078 (struct dwarf2_cu *cu, dwarf_tag tag);
1079
1080 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1081 values. Keep the items ordered with increasing constraints compliance. */
1082 enum pc_bounds_kind
1083 {
1084 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1085 PC_BOUNDS_NOT_PRESENT,
1086
1087 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1088 were present but they do not form a valid range of PC addresses. */
1089 PC_BOUNDS_INVALID,
1090
1091 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1092 PC_BOUNDS_RANGES,
1093
1094 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1095 PC_BOUNDS_HIGH_LOW,
1096 };
1097
1098 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1099 CORE_ADDR *, CORE_ADDR *,
1100 struct dwarf2_cu *,
1101 addrmap *,
1102 void *);
1103
1104 static void get_scope_pc_bounds (struct die_info *,
1105 CORE_ADDR *, CORE_ADDR *,
1106 struct dwarf2_cu *);
1107
1108 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1109 CORE_ADDR, struct dwarf2_cu *);
1110
1111 static void dwarf2_add_field (struct field_info *, struct die_info *,
1112 struct dwarf2_cu *);
1113
1114 static void dwarf2_attach_fields_to_type (struct field_info *,
1115 struct type *, struct dwarf2_cu *);
1116
1117 static void dwarf2_add_member_fn (struct field_info *,
1118 struct die_info *, struct type *,
1119 struct dwarf2_cu *);
1120
1121 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1122 struct type *,
1123 struct dwarf2_cu *);
1124
1125 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1126
1127 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1128
1129 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1130
1131 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1132
1133 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1134
1135 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1136
1137 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1138
1139 static struct type *read_module_type (struct die_info *die,
1140 struct dwarf2_cu *cu);
1141
1142 static const char *namespace_name (struct die_info *die,
1143 int *is_anonymous, struct dwarf2_cu *);
1144
1145 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1146
1147 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1148 bool * = nullptr);
1149
1150 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1151 struct dwarf2_cu *);
1152
1153 static struct die_info *read_die_and_siblings_1
1154 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1155 struct die_info *);
1156
1157 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1158 const gdb_byte *info_ptr,
1159 const gdb_byte **new_info_ptr,
1160 struct die_info *parent);
1161
1162 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1163 struct die_info **, const gdb_byte *,
1164 int);
1165
1166 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1167 struct die_info **, const gdb_byte *);
1168
1169 static void process_die (struct die_info *, struct dwarf2_cu *);
1170
1171 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1172 struct objfile *);
1173
1174 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1175
1176 static const char *dwarf2_full_name (const char *name,
1177 struct die_info *die,
1178 struct dwarf2_cu *cu);
1179
1180 static const char *dwarf2_physname (const char *name, struct die_info *die,
1181 struct dwarf2_cu *cu);
1182
1183 static struct die_info *dwarf2_extension (struct die_info *die,
1184 struct dwarf2_cu **);
1185
1186 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1187
1188 static void dump_die_for_error (struct die_info *);
1189
1190 static void dump_die_1 (struct ui_file *, int level, int max_level,
1191 struct die_info *);
1192
1193 /*static*/ void dump_die (struct die_info *, int max_level);
1194
1195 static void store_in_ref_table (struct die_info *,
1196 struct dwarf2_cu *);
1197
1198 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1199 const struct attribute *,
1200 struct dwarf2_cu **);
1201
1202 static struct die_info *follow_die_ref (struct die_info *,
1203 const struct attribute *,
1204 struct dwarf2_cu **);
1205
1206 static struct die_info *follow_die_sig (struct die_info *,
1207 const struct attribute *,
1208 struct dwarf2_cu **);
1209
1210 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1211 struct dwarf2_cu *);
1212
1213 static struct type *get_DW_AT_signature_type (struct die_info *,
1214 const struct attribute *,
1215 struct dwarf2_cu *);
1216
1217 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1218 dwarf2_per_objfile *per_objfile);
1219
1220 static void read_signatured_type (signatured_type *sig_type,
1221 dwarf2_per_objfile *per_objfile);
1222
1223 static int attr_to_dynamic_prop (const struct attribute *attr,
1224 struct die_info *die, struct dwarf2_cu *cu,
1225 struct dynamic_prop *prop, struct type *type);
1226
1227 /* memory allocation interface */
1228
1229 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1230
1231 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1232
1233 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1234
1235 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1236 struct dwarf2_loclist_baton *baton,
1237 const struct attribute *attr);
1238
1239 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1240 struct symbol *sym,
1241 struct dwarf2_cu *cu,
1242 int is_block);
1243
1244 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1245 const gdb_byte *info_ptr,
1246 const struct abbrev_info *abbrev,
1247 bool do_skip_children = true);
1248
1249 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1250 (sect_offset sect_off, unsigned int offset_in_dwz,
1251 dwarf2_per_bfd *per_bfd);
1252
1253 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1254 struct die_info *comp_unit_die,
1255 enum language pretend_language);
1256
1257 static struct type *set_die_type (struct die_info *, struct type *,
1258 struct dwarf2_cu *, bool = false);
1259
1260 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1261
1262 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1263 dwarf2_per_objfile *per_objfile,
1264 dwarf2_cu *existing_cu,
1265 bool skip_partial,
1266 enum language pretend_language);
1267
1268 static void process_full_comp_unit (dwarf2_cu *cu,
1269 enum language pretend_language);
1270
1271 static void process_full_type_unit (dwarf2_cu *cu,
1272 enum language pretend_language);
1273
1274 static struct type *get_die_type_at_offset (sect_offset,
1275 dwarf2_per_cu_data *per_cu,
1276 dwarf2_per_objfile *per_objfile);
1277
1278 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1279
1280 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1281 dwarf2_per_objfile *per_objfile,
1282 enum language pretend_language);
1283
1284 static void process_queue (dwarf2_per_objfile *per_objfile);
1285
1286 /* Class, the destructor of which frees all allocated queue entries. This
1287 will only have work to do if an error was thrown while processing the
1288 dwarf. If no error was thrown then the queue entries should have all
1289 been processed, and freed, as we went along. */
1290
1291 class dwarf2_queue_guard
1292 {
1293 public:
1294 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1295 : m_per_objfile (per_objfile)
1296 {
1297 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1298
1299 m_per_objfile->per_bfd->queue.emplace ();
1300 }
1301
1302 /* Free any entries remaining on the queue. There should only be
1303 entries left if we hit an error while processing the dwarf. */
1304 ~dwarf2_queue_guard ()
1305 {
1306 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1307
1308 m_per_objfile->per_bfd->queue.reset ();
1309 }
1310
1311 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1312
1313 private:
1314 dwarf2_per_objfile *m_per_objfile;
1315 };
1316
1317 dwarf2_queue_item::~dwarf2_queue_item ()
1318 {
1319 /* Anything still marked queued is likely to be in an
1320 inconsistent state, so discard it. */
1321 if (per_cu->queued)
1322 {
1323 per_objfile->remove_cu (per_cu);
1324 per_cu->queued = 0;
1325 }
1326 }
1327
1328 /* See dwarf2/read.h. */
1329
1330 void
1331 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1332 {
1333 if (data->is_debug_types)
1334 delete static_cast<signatured_type *> (data);
1335 else
1336 delete data;
1337 }
1338
1339 static file_and_directory &find_file_and_directory
1340 (struct die_info *die, struct dwarf2_cu *cu);
1341
1342 static const char *compute_include_file_name
1343 (const struct line_header *lh,
1344 const file_entry &fe,
1345 const file_and_directory &cu_info,
1346 gdb::unique_xmalloc_ptr<char> *name_holder);
1347
1348 static htab_up allocate_signatured_type_table ();
1349
1350 static htab_up allocate_dwo_unit_table ();
1351
1352 static struct dwo_unit *lookup_dwo_unit_in_dwp
1353 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1354 const char *comp_dir, ULONGEST signature, int is_debug_types);
1355
1356 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1357
1358 static struct dwo_unit *lookup_dwo_comp_unit
1359 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1360 ULONGEST signature);
1361
1362 static struct dwo_unit *lookup_dwo_type_unit
1363 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1364
1365 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1366
1367 /* A unique pointer to a dwo_file. */
1368
1369 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1370
1371 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1372
1373 static void check_producer (struct dwarf2_cu *cu);
1374 \f
1375 /* Various complaints about symbol reading that don't abort the process. */
1376
1377 static void
1378 dwarf2_debug_line_missing_file_complaint (void)
1379 {
1380 complaint (_(".debug_line section has line data without a file"));
1381 }
1382
1383 static void
1384 dwarf2_debug_line_missing_end_sequence_complaint (void)
1385 {
1386 complaint (_(".debug_line section has line "
1387 "program sequence without an end"));
1388 }
1389
1390 static void
1391 dwarf2_complex_location_expr_complaint (void)
1392 {
1393 complaint (_("location expression too complex"));
1394 }
1395
1396 static void
1397 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1398 int arg3)
1399 {
1400 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1401 arg1, arg2, arg3);
1402 }
1403
1404 static void
1405 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1406 {
1407 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1408 arg1, arg2);
1409 }
1410
1411 /* Hash function for line_header_hash. */
1412
1413 static hashval_t
1414 line_header_hash (const struct line_header *ofs)
1415 {
1416 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1417 }
1418
1419 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1420
1421 static hashval_t
1422 line_header_hash_voidp (const void *item)
1423 {
1424 const struct line_header *ofs = (const struct line_header *) item;
1425
1426 return line_header_hash (ofs);
1427 }
1428
1429 /* Equality function for line_header_hash. */
1430
1431 static int
1432 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1433 {
1434 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1435 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1436
1437 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1438 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1439 }
1440
1441 \f
1442
1443 /* An iterator for all_comp_units that is based on index. This
1444 approach makes it possible to iterate over all_comp_units safely,
1445 when some caller in the loop may add new units. */
1446
1447 class all_comp_units_iterator
1448 {
1449 public:
1450
1451 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1452 : m_per_bfd (per_bfd),
1453 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1454 {
1455 }
1456
1457 all_comp_units_iterator &operator++ ()
1458 {
1459 ++m_index;
1460 return *this;
1461 }
1462
1463 dwarf2_per_cu_data *operator* () const
1464 {
1465 return m_per_bfd->get_cu (m_index);
1466 }
1467
1468 bool operator== (const all_comp_units_iterator &other) const
1469 {
1470 return m_index == other.m_index;
1471 }
1472
1473
1474 bool operator!= (const all_comp_units_iterator &other) const
1475 {
1476 return m_index != other.m_index;
1477 }
1478
1479 private:
1480
1481 dwarf2_per_bfd *m_per_bfd;
1482 size_t m_index;
1483 };
1484
1485 /* A range adapter for the all_comp_units_iterator. */
1486 class all_comp_units_range
1487 {
1488 public:
1489
1490 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1491 : m_per_bfd (per_bfd)
1492 {
1493 }
1494
1495 all_comp_units_iterator begin ()
1496 {
1497 return all_comp_units_iterator (m_per_bfd, true);
1498 }
1499
1500 all_comp_units_iterator end ()
1501 {
1502 return all_comp_units_iterator (m_per_bfd, false);
1503 }
1504
1505 private:
1506
1507 dwarf2_per_bfd *m_per_bfd;
1508 };
1509
1510 /* See declaration. */
1511
1512 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1513 bool can_copy_)
1514 : obfd (obfd),
1515 can_copy (can_copy_)
1516 {
1517 if (names == NULL)
1518 names = &dwarf2_elf_names;
1519
1520 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1521 locate_sections (obfd, sec, *names);
1522 }
1523
1524 dwarf2_per_bfd::~dwarf2_per_bfd ()
1525 {
1526 for (auto &per_cu : all_comp_units)
1527 {
1528 per_cu->imported_symtabs_free ();
1529 per_cu->free_cached_file_names ();
1530 }
1531
1532 /* Everything else should be on this->obstack. */
1533 }
1534
1535 /* See read.h. */
1536
1537 void
1538 dwarf2_per_objfile::remove_all_cus ()
1539 {
1540 gdb_assert (!this->per_bfd->queue.has_value ());
1541
1542 for (auto pair : m_dwarf2_cus)
1543 delete pair.second;
1544
1545 m_dwarf2_cus.clear ();
1546 }
1547
1548 /* A helper class that calls free_cached_comp_units on
1549 destruction. */
1550
1551 class free_cached_comp_units
1552 {
1553 public:
1554
1555 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1556 : m_per_objfile (per_objfile)
1557 {
1558 }
1559
1560 ~free_cached_comp_units ()
1561 {
1562 m_per_objfile->remove_all_cus ();
1563 }
1564
1565 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1566
1567 private:
1568
1569 dwarf2_per_objfile *m_per_objfile;
1570 };
1571
1572 /* See read.h. */
1573
1574 bool
1575 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1576 {
1577 if (per_cu->index < this->m_symtabs.size ())
1578 return this->m_symtabs[per_cu->index] != nullptr;
1579 return false;
1580 }
1581
1582 /* See read.h. */
1583
1584 compunit_symtab *
1585 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1586 {
1587 if (per_cu->index < this->m_symtabs.size ())
1588 return this->m_symtabs[per_cu->index];
1589 return nullptr;
1590 }
1591
1592 /* See read.h. */
1593
1594 void
1595 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1596 compunit_symtab *symtab)
1597 {
1598 if (per_cu->index >= this->m_symtabs.size ())
1599 this->m_symtabs.resize (per_cu->index + 1);
1600 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1601 this->m_symtabs[per_cu->index] = symtab;
1602 }
1603
1604 /* Try to locate the sections we need for DWARF 2 debugging
1605 information and return true if we have enough to do something.
1606 NAMES points to the dwarf2 section names, or is NULL if the standard
1607 ELF names are used. CAN_COPY is true for formats where symbol
1608 interposition is possible and so symbol values must follow copy
1609 relocation rules. */
1610
1611 int
1612 dwarf2_has_info (struct objfile *objfile,
1613 const struct dwarf2_debug_sections *names,
1614 bool can_copy)
1615 {
1616 if (objfile->flags & OBJF_READNEVER)
1617 return 0;
1618
1619 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1620
1621 if (per_objfile == NULL)
1622 {
1623 dwarf2_per_bfd *per_bfd;
1624
1625 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1626 BFD doesn't require relocations.
1627
1628 We don't share with objfiles for which -readnow was requested,
1629 because it would complicate things when loading the same BFD with
1630 -readnow and then without -readnow. */
1631 if (!gdb_bfd_requires_relocations (objfile->obfd)
1632 && (objfile->flags & OBJF_READNOW) == 0)
1633 {
1634 /* See if one has been created for this BFD yet. */
1635 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1636
1637 if (per_bfd == nullptr)
1638 {
1639 /* No, create it now. */
1640 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1641 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1642 }
1643 }
1644 else
1645 {
1646 /* No sharing possible, create one specifically for this objfile. */
1647 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1648 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1649 }
1650
1651 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1652 }
1653
1654 return (!per_objfile->per_bfd->info.is_virtual
1655 && per_objfile->per_bfd->info.s.section != NULL
1656 && !per_objfile->per_bfd->abbrev.is_virtual
1657 && per_objfile->per_bfd->abbrev.s.section != NULL);
1658 }
1659
1660 /* See declaration. */
1661
1662 void
1663 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1664 const dwarf2_debug_sections &names)
1665 {
1666 flagword aflag = bfd_section_flags (sectp);
1667
1668 if ((aflag & SEC_HAS_CONTENTS) == 0)
1669 {
1670 }
1671 else if (elf_section_data (sectp)->this_hdr.sh_size
1672 > bfd_get_file_size (abfd))
1673 {
1674 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1675 warning (_("Discarding section %s which has a section size (%s"
1676 ") larger than the file size [in module %s]"),
1677 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1678 bfd_get_filename (abfd));
1679 }
1680 else if (names.info.matches (sectp->name))
1681 {
1682 this->info.s.section = sectp;
1683 this->info.size = bfd_section_size (sectp);
1684 }
1685 else if (names.abbrev.matches (sectp->name))
1686 {
1687 this->abbrev.s.section = sectp;
1688 this->abbrev.size = bfd_section_size (sectp);
1689 }
1690 else if (names.line.matches (sectp->name))
1691 {
1692 this->line.s.section = sectp;
1693 this->line.size = bfd_section_size (sectp);
1694 }
1695 else if (names.loc.matches (sectp->name))
1696 {
1697 this->loc.s.section = sectp;
1698 this->loc.size = bfd_section_size (sectp);
1699 }
1700 else if (names.loclists.matches (sectp->name))
1701 {
1702 this->loclists.s.section = sectp;
1703 this->loclists.size = bfd_section_size (sectp);
1704 }
1705 else if (names.macinfo.matches (sectp->name))
1706 {
1707 this->macinfo.s.section = sectp;
1708 this->macinfo.size = bfd_section_size (sectp);
1709 }
1710 else if (names.macro.matches (sectp->name))
1711 {
1712 this->macro.s.section = sectp;
1713 this->macro.size = bfd_section_size (sectp);
1714 }
1715 else if (names.str.matches (sectp->name))
1716 {
1717 this->str.s.section = sectp;
1718 this->str.size = bfd_section_size (sectp);
1719 }
1720 else if (names.str_offsets.matches (sectp->name))
1721 {
1722 this->str_offsets.s.section = sectp;
1723 this->str_offsets.size = bfd_section_size (sectp);
1724 }
1725 else if (names.line_str.matches (sectp->name))
1726 {
1727 this->line_str.s.section = sectp;
1728 this->line_str.size = bfd_section_size (sectp);
1729 }
1730 else if (names.addr.matches (sectp->name))
1731 {
1732 this->addr.s.section = sectp;
1733 this->addr.size = bfd_section_size (sectp);
1734 }
1735 else if (names.frame.matches (sectp->name))
1736 {
1737 this->frame.s.section = sectp;
1738 this->frame.size = bfd_section_size (sectp);
1739 }
1740 else if (names.eh_frame.matches (sectp->name))
1741 {
1742 this->eh_frame.s.section = sectp;
1743 this->eh_frame.size = bfd_section_size (sectp);
1744 }
1745 else if (names.ranges.matches (sectp->name))
1746 {
1747 this->ranges.s.section = sectp;
1748 this->ranges.size = bfd_section_size (sectp);
1749 }
1750 else if (names.rnglists.matches (sectp->name))
1751 {
1752 this->rnglists.s.section = sectp;
1753 this->rnglists.size = bfd_section_size (sectp);
1754 }
1755 else if (names.types.matches (sectp->name))
1756 {
1757 struct dwarf2_section_info type_section;
1758
1759 memset (&type_section, 0, sizeof (type_section));
1760 type_section.s.section = sectp;
1761 type_section.size = bfd_section_size (sectp);
1762
1763 this->types.push_back (type_section);
1764 }
1765 else if (names.gdb_index.matches (sectp->name))
1766 {
1767 this->gdb_index.s.section = sectp;
1768 this->gdb_index.size = bfd_section_size (sectp);
1769 }
1770 else if (names.debug_names.matches (sectp->name))
1771 {
1772 this->debug_names.s.section = sectp;
1773 this->debug_names.size = bfd_section_size (sectp);
1774 }
1775 else if (names.debug_aranges.matches (sectp->name))
1776 {
1777 this->debug_aranges.s.section = sectp;
1778 this->debug_aranges.size = bfd_section_size (sectp);
1779 }
1780
1781 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1782 && bfd_section_vma (sectp) == 0)
1783 this->has_section_at_zero = true;
1784 }
1785
1786 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1787 SECTION_NAME. */
1788
1789 void
1790 dwarf2_get_section_info (struct objfile *objfile,
1791 enum dwarf2_section_enum sect,
1792 asection **sectp, const gdb_byte **bufp,
1793 bfd_size_type *sizep)
1794 {
1795 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1796 struct dwarf2_section_info *info;
1797
1798 /* We may see an objfile without any DWARF, in which case we just
1799 return nothing. */
1800 if (per_objfile == NULL)
1801 {
1802 *sectp = NULL;
1803 *bufp = NULL;
1804 *sizep = 0;
1805 return;
1806 }
1807 switch (sect)
1808 {
1809 case DWARF2_DEBUG_FRAME:
1810 info = &per_objfile->per_bfd->frame;
1811 break;
1812 case DWARF2_EH_FRAME:
1813 info = &per_objfile->per_bfd->eh_frame;
1814 break;
1815 default:
1816 gdb_assert_not_reached ("unexpected section");
1817 }
1818
1819 info->read (objfile);
1820
1821 *sectp = info->get_bfd_section ();
1822 *bufp = info->buffer;
1823 *sizep = info->size;
1824 }
1825
1826 /* See dwarf2/read.h. */
1827
1828 void
1829 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1830 {
1831 info.read (objfile);
1832 abbrev.read (objfile);
1833 line.read (objfile);
1834 str.read (objfile);
1835 str_offsets.read (objfile);
1836 line_str.read (objfile);
1837 ranges.read (objfile);
1838 rnglists.read (objfile);
1839 addr.read (objfile);
1840
1841 for (auto &section : types)
1842 section.read (objfile);
1843 }
1844
1845 \f
1846 /* DWARF quick_symbol_functions support. */
1847
1848 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1849 unique line tables, so we maintain a separate table of all .debug_line
1850 derived entries to support the sharing.
1851 All the quick functions need is the list of file names. We discard the
1852 line_header when we're done and don't need to record it here. */
1853 struct quick_file_names
1854 {
1855 /* The data used to construct the hash key. */
1856 struct stmt_list_hash hash;
1857
1858 /* The number of entries in file_names, real_names. */
1859 unsigned int num_file_names;
1860
1861 /* The CU directory, as given by DW_AT_comp_dir. May be
1862 nullptr. */
1863 const char *comp_dir;
1864
1865 /* The file names from the line table, after being run through
1866 file_full_name. */
1867 const char **file_names;
1868
1869 /* The file names from the line table after being run through
1870 gdb_realpath. These are computed lazily. */
1871 const char **real_names;
1872 };
1873
1874 struct dwarf2_base_index_functions : public quick_symbol_functions
1875 {
1876 bool has_symbols (struct objfile *objfile) override;
1877
1878 bool has_unexpanded_symtabs (struct objfile *objfile) override;
1879
1880 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
1881
1882 void forget_cached_source_info (struct objfile *objfile) override;
1883
1884 enum language lookup_global_symbol_language (struct objfile *objfile,
1885 const char *name,
1886 domain_enum domain,
1887 bool *symbol_found_p) override
1888 {
1889 *symbol_found_p = false;
1890 return language_unknown;
1891 }
1892
1893 void print_stats (struct objfile *objfile, bool print_bcache) override;
1894
1895 void expand_all_symtabs (struct objfile *objfile) override;
1896
1897 struct compunit_symtab *find_pc_sect_compunit_symtab
1898 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
1899 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
1900
1901 struct compunit_symtab *find_compunit_symtab_by_address
1902 (struct objfile *objfile, CORE_ADDR address) override
1903 {
1904 return nullptr;
1905 }
1906
1907 void map_symbol_filenames (struct objfile *objfile,
1908 gdb::function_view<symbol_filename_ftype> fun,
1909 bool need_fullname) override;
1910 };
1911
1912 struct dwarf2_gdb_index : public dwarf2_base_index_functions
1913 {
1914 void dump (struct objfile *objfile) override;
1915
1916 void expand_matching_symbols
1917 (struct objfile *,
1918 const lookup_name_info &lookup_name,
1919 domain_enum domain,
1920 int global,
1921 symbol_compare_ftype *ordered_compare) override;
1922
1923 bool expand_symtabs_matching
1924 (struct objfile *objfile,
1925 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1926 const lookup_name_info *lookup_name,
1927 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1928 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1929 block_search_flags search_flags,
1930 domain_enum domain,
1931 enum search_domain kind) override;
1932 };
1933
1934 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
1935 {
1936 void dump (struct objfile *objfile) override;
1937
1938 void expand_matching_symbols
1939 (struct objfile *,
1940 const lookup_name_info &lookup_name,
1941 domain_enum domain,
1942 int global,
1943 symbol_compare_ftype *ordered_compare) override;
1944
1945 bool expand_symtabs_matching
1946 (struct objfile *objfile,
1947 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1948 const lookup_name_info *lookup_name,
1949 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1950 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1951 block_search_flags search_flags,
1952 domain_enum domain,
1953 enum search_domain kind) override;
1954 };
1955
1956 static quick_symbol_functions_up
1957 make_dwarf_gdb_index ()
1958 {
1959 return quick_symbol_functions_up (new dwarf2_gdb_index);
1960 }
1961
1962 static quick_symbol_functions_up
1963 make_dwarf_debug_names ()
1964 {
1965 return quick_symbol_functions_up (new dwarf2_debug_names_index);
1966 }
1967
1968 /* Utility hash function for a stmt_list_hash. */
1969
1970 static hashval_t
1971 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1972 {
1973 hashval_t v = 0;
1974
1975 if (stmt_list_hash->dwo_unit != NULL)
1976 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1977 v += to_underlying (stmt_list_hash->line_sect_off);
1978 return v;
1979 }
1980
1981 /* Utility equality function for a stmt_list_hash. */
1982
1983 static int
1984 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1985 const struct stmt_list_hash *rhs)
1986 {
1987 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1988 return 0;
1989 if (lhs->dwo_unit != NULL
1990 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1991 return 0;
1992
1993 return lhs->line_sect_off == rhs->line_sect_off;
1994 }
1995
1996 /* Hash function for a quick_file_names. */
1997
1998 static hashval_t
1999 hash_file_name_entry (const void *e)
2000 {
2001 const struct quick_file_names *file_data
2002 = (const struct quick_file_names *) e;
2003
2004 return hash_stmt_list_entry (&file_data->hash);
2005 }
2006
2007 /* Equality function for a quick_file_names. */
2008
2009 static int
2010 eq_file_name_entry (const void *a, const void *b)
2011 {
2012 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2013 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2014
2015 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2016 }
2017
2018 /* Create a quick_file_names hash table. */
2019
2020 static htab_up
2021 create_quick_file_names_table (unsigned int nr_initial_entries)
2022 {
2023 return htab_up (htab_create_alloc (nr_initial_entries,
2024 hash_file_name_entry, eq_file_name_entry,
2025 nullptr, xcalloc, xfree));
2026 }
2027
2028 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2029 function is unrelated to symtabs, symtab would have to be created afterwards.
2030 You should call age_cached_comp_units after processing the CU. */
2031
2032 static dwarf2_cu *
2033 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2034 bool skip_partial)
2035 {
2036 if (per_cu->is_debug_types)
2037 load_full_type_unit (per_cu, per_objfile);
2038 else
2039 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2040 skip_partial, language_minimal);
2041
2042 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2043 if (cu == nullptr)
2044 return nullptr; /* Dummy CU. */
2045
2046 dwarf2_find_base_address (cu->dies, cu);
2047
2048 return cu;
2049 }
2050
2051 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2052
2053 static void
2054 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2055 dwarf2_per_objfile *per_objfile, bool skip_partial)
2056 {
2057 {
2058 /* The destructor of dwarf2_queue_guard frees any entries left on
2059 the queue. After this point we're guaranteed to leave this function
2060 with the dwarf queue empty. */
2061 dwarf2_queue_guard q_guard (per_objfile);
2062
2063 if (!per_objfile->symtab_set_p (per_cu))
2064 {
2065 queue_comp_unit (per_cu, per_objfile, language_minimal);
2066 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2067
2068 /* If we just loaded a CU from a DWO, and we're working with an index
2069 that may badly handle TUs, load all the TUs in that DWO as well.
2070 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2071 if (!per_cu->is_debug_types
2072 && cu != NULL
2073 && cu->dwo_unit != NULL
2074 && per_objfile->per_bfd->index_table != NULL
2075 && per_objfile->per_bfd->index_table->version <= 7
2076 /* DWP files aren't supported yet. */
2077 && get_dwp_file (per_objfile) == NULL)
2078 queue_and_load_all_dwo_tus (cu);
2079 }
2080
2081 process_queue (per_objfile);
2082 }
2083
2084 /* Age the cache, releasing compilation units that have not
2085 been used recently. */
2086 per_objfile->age_comp_units ();
2087 }
2088
2089 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2090 the per-objfile for which this symtab is instantiated.
2091
2092 Returns the resulting symbol table. */
2093
2094 static struct compunit_symtab *
2095 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2096 dwarf2_per_objfile *per_objfile,
2097 bool skip_partial)
2098 {
2099 if (!per_objfile->symtab_set_p (per_cu))
2100 {
2101 free_cached_comp_units freer (per_objfile);
2102 scoped_restore decrementer = increment_reading_symtab ();
2103 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2104 process_cu_includes (per_objfile);
2105 }
2106
2107 return per_objfile->get_symtab (per_cu);
2108 }
2109
2110 /* See read.h. */
2111
2112 dwarf2_per_cu_data_up
2113 dwarf2_per_bfd::allocate_per_cu ()
2114 {
2115 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2116 result->per_bfd = this;
2117 result->index = all_comp_units.size ();
2118 return result;
2119 }
2120
2121 /* See read.h. */
2122
2123 signatured_type_up
2124 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2125 {
2126 signatured_type_up result (new signatured_type (signature));
2127 result->per_bfd = this;
2128 result->index = all_comp_units.size ();
2129 result->is_debug_types = true;
2130 tu_stats.nr_tus++;
2131 return result;
2132 }
2133
2134 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2135 obstack, and constructed with the specified field values. */
2136
2137 static dwarf2_per_cu_data_up
2138 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2139 struct dwarf2_section_info *section,
2140 int is_dwz,
2141 sect_offset sect_off, ULONGEST length)
2142 {
2143 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2144 the_cu->sect_off = sect_off;
2145 the_cu->length = length;
2146 the_cu->section = section;
2147 the_cu->is_dwz = is_dwz;
2148 return the_cu;
2149 }
2150
2151 /* A helper for create_cus_from_index that handles a given list of
2152 CUs. */
2153
2154 static void
2155 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2156 const gdb_byte *cu_list, offset_type n_elements,
2157 struct dwarf2_section_info *section,
2158 int is_dwz)
2159 {
2160 for (offset_type i = 0; i < n_elements; i += 2)
2161 {
2162 gdb_static_assert (sizeof (ULONGEST) >= 8);
2163
2164 sect_offset sect_off
2165 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2166 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2167 cu_list += 2 * 8;
2168
2169 dwarf2_per_cu_data_up per_cu
2170 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2171 length);
2172 per_bfd->all_comp_units.push_back (std::move (per_cu));
2173 }
2174 }
2175
2176 /* Read the CU list from the mapped index, and use it to create all
2177 the CU objects for PER_BFD. */
2178
2179 static void
2180 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2181 const gdb_byte *cu_list, offset_type cu_list_elements,
2182 const gdb_byte *dwz_list, offset_type dwz_elements)
2183 {
2184 gdb_assert (per_bfd->all_comp_units.empty ());
2185 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2186
2187 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2188 &per_bfd->info, 0);
2189
2190 if (dwz_elements == 0)
2191 return;
2192
2193 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2194 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2195 &dwz->info, 1);
2196 }
2197
2198 /* Create the signatured type hash table from the index. */
2199
2200 static void
2201 create_signatured_type_table_from_index
2202 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2203 const gdb_byte *bytes, offset_type elements)
2204 {
2205 htab_up sig_types_hash = allocate_signatured_type_table ();
2206
2207 for (offset_type i = 0; i < elements; i += 3)
2208 {
2209 signatured_type_up sig_type;
2210 ULONGEST signature;
2211 void **slot;
2212 cu_offset type_offset_in_tu;
2213
2214 gdb_static_assert (sizeof (ULONGEST) >= 8);
2215 sect_offset sect_off
2216 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2217 type_offset_in_tu
2218 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2219 BFD_ENDIAN_LITTLE);
2220 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2221 bytes += 3 * 8;
2222
2223 sig_type = per_bfd->allocate_signatured_type (signature);
2224 sig_type->type_offset_in_tu = type_offset_in_tu;
2225 sig_type->section = section;
2226 sig_type->sect_off = sect_off;
2227
2228 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2229 *slot = sig_type.get ();
2230
2231 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2232 }
2233
2234 per_bfd->signatured_types = std::move (sig_types_hash);
2235 }
2236
2237 /* Create the signatured type hash table from .debug_names. */
2238
2239 static void
2240 create_signatured_type_table_from_debug_names
2241 (dwarf2_per_objfile *per_objfile,
2242 const mapped_debug_names &map,
2243 struct dwarf2_section_info *section,
2244 struct dwarf2_section_info *abbrev_section)
2245 {
2246 struct objfile *objfile = per_objfile->objfile;
2247
2248 section->read (objfile);
2249 abbrev_section->read (objfile);
2250
2251 htab_up sig_types_hash = allocate_signatured_type_table ();
2252
2253 for (uint32_t i = 0; i < map.tu_count; ++i)
2254 {
2255 signatured_type_up sig_type;
2256 void **slot;
2257
2258 sect_offset sect_off
2259 = (sect_offset) (extract_unsigned_integer
2260 (map.tu_table_reordered + i * map.offset_size,
2261 map.offset_size,
2262 map.dwarf5_byte_order));
2263
2264 comp_unit_head cu_header;
2265 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2266 abbrev_section,
2267 section->buffer + to_underlying (sect_off),
2268 rcuh_kind::TYPE);
2269
2270 sig_type = per_objfile->per_bfd->allocate_signatured_type
2271 (cu_header.signature);
2272 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2273 sig_type->section = section;
2274 sig_type->sect_off = sect_off;
2275
2276 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2277 *slot = sig_type.get ();
2278
2279 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2280 }
2281
2282 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2283 }
2284
2285 /* Read the address map data from the mapped index, and use it to
2286 populate the psymtabs_addrmap. */
2287
2288 static void
2289 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2290 struct mapped_index *index)
2291 {
2292 struct objfile *objfile = per_objfile->objfile;
2293 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2294 struct gdbarch *gdbarch = objfile->arch ();
2295 const gdb_byte *iter, *end;
2296 struct addrmap *mutable_map;
2297 CORE_ADDR baseaddr;
2298
2299 auto_obstack temp_obstack;
2300
2301 mutable_map = addrmap_create_mutable (&temp_obstack);
2302
2303 iter = index->address_table.data ();
2304 end = iter + index->address_table.size ();
2305
2306 baseaddr = objfile->text_section_offset ();
2307
2308 while (iter < end)
2309 {
2310 ULONGEST hi, lo, cu_index;
2311 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2312 iter += 8;
2313 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2314 iter += 8;
2315 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2316 iter += 4;
2317
2318 if (lo > hi)
2319 {
2320 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2321 hex_string (lo), hex_string (hi));
2322 continue;
2323 }
2324
2325 if (cu_index >= per_bfd->all_comp_units.size ())
2326 {
2327 complaint (_(".gdb_index address table has invalid CU number %u"),
2328 (unsigned) cu_index);
2329 continue;
2330 }
2331
2332 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2333 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2334 addrmap_set_empty (mutable_map, lo, hi - 1,
2335 per_bfd->get_cu (cu_index));
2336 }
2337
2338 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2339 &per_bfd->obstack);
2340 }
2341
2342 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2343 to populate given addrmap. Returns true on success, false on
2344 failure. */
2345
2346 static bool
2347 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2348 struct dwarf2_section_info *section,
2349 addrmap *mutable_map)
2350 {
2351 struct objfile *objfile = per_objfile->objfile;
2352 bfd *abfd = objfile->obfd;
2353 struct gdbarch *gdbarch = objfile->arch ();
2354 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2355 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2356
2357 std::unordered_map<sect_offset,
2358 dwarf2_per_cu_data *,
2359 gdb::hash_enum<sect_offset>>
2360 debug_info_offset_to_per_cu;
2361 for (const auto &per_cu : per_bfd->all_comp_units)
2362 {
2363 /* A TU will not need aranges, and skipping them here is an easy
2364 way of ignoring .debug_types -- and possibly seeing a
2365 duplicate section offset -- entirely. The same applies to
2366 units coming from a dwz file. */
2367 if (per_cu->is_debug_types || per_cu->is_dwz)
2368 continue;
2369
2370 const auto insertpair
2371 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2372 per_cu.get ());
2373 if (!insertpair.second)
2374 {
2375 warning (_("Section .debug_aranges in %s has duplicate "
2376 "debug_info_offset %s, ignoring .debug_aranges."),
2377 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2378 return false;
2379 }
2380 }
2381
2382 section->read (objfile);
2383
2384 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2385
2386 const gdb_byte *addr = section->buffer;
2387
2388 while (addr < section->buffer + section->size)
2389 {
2390 const gdb_byte *const entry_addr = addr;
2391 unsigned int bytes_read;
2392
2393 const LONGEST entry_length = read_initial_length (abfd, addr,
2394 &bytes_read);
2395 addr += bytes_read;
2396
2397 const gdb_byte *const entry_end = addr + entry_length;
2398 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2399 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2400 if (addr + entry_length > section->buffer + section->size)
2401 {
2402 warning (_("Section .debug_aranges in %s entry at offset %s "
2403 "length %s exceeds section length %s, "
2404 "ignoring .debug_aranges."),
2405 objfile_name (objfile),
2406 plongest (entry_addr - section->buffer),
2407 plongest (bytes_read + entry_length),
2408 pulongest (section->size));
2409 return false;
2410 }
2411
2412 /* The version number. */
2413 const uint16_t version = read_2_bytes (abfd, addr);
2414 addr += 2;
2415 if (version != 2)
2416 {
2417 warning (_("Section .debug_aranges in %s entry at offset %s "
2418 "has unsupported version %d, ignoring .debug_aranges."),
2419 objfile_name (objfile),
2420 plongest (entry_addr - section->buffer), version);
2421 return false;
2422 }
2423
2424 const uint64_t debug_info_offset
2425 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2426 addr += offset_size;
2427 const auto per_cu_it
2428 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2429 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2430 {
2431 warning (_("Section .debug_aranges in %s entry at offset %s "
2432 "debug_info_offset %s does not exists, "
2433 "ignoring .debug_aranges."),
2434 objfile_name (objfile),
2435 plongest (entry_addr - section->buffer),
2436 pulongest (debug_info_offset));
2437 return false;
2438 }
2439 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2440
2441 const uint8_t address_size = *addr++;
2442 if (address_size < 1 || address_size > 8)
2443 {
2444 warning (_("Section .debug_aranges in %s entry at offset %s "
2445 "address_size %u is invalid, ignoring .debug_aranges."),
2446 objfile_name (objfile),
2447 plongest (entry_addr - section->buffer), address_size);
2448 return false;
2449 }
2450
2451 const uint8_t segment_selector_size = *addr++;
2452 if (segment_selector_size != 0)
2453 {
2454 warning (_("Section .debug_aranges in %s entry at offset %s "
2455 "segment_selector_size %u is not supported, "
2456 "ignoring .debug_aranges."),
2457 objfile_name (objfile),
2458 plongest (entry_addr - section->buffer),
2459 segment_selector_size);
2460 return false;
2461 }
2462
2463 /* Must pad to an alignment boundary that is twice the address
2464 size. It is undocumented by the DWARF standard but GCC does
2465 use it. However, not every compiler does this. We can see
2466 whether it has happened by looking at the total length of the
2467 contents of the aranges for this CU -- it if isn't a multiple
2468 of twice the address size, then we skip any leftover
2469 bytes. */
2470 addr += (entry_end - addr) % (2 * address_size);
2471
2472 for (;;)
2473 {
2474 if (addr + 2 * address_size > entry_end)
2475 {
2476 warning (_("Section .debug_aranges in %s entry at offset %s "
2477 "address list is not properly terminated, "
2478 "ignoring .debug_aranges."),
2479 objfile_name (objfile),
2480 plongest (entry_addr - section->buffer));
2481 return false;
2482 }
2483 ULONGEST start = extract_unsigned_integer (addr, address_size,
2484 dwarf5_byte_order);
2485 addr += address_size;
2486 ULONGEST length = extract_unsigned_integer (addr, address_size,
2487 dwarf5_byte_order);
2488 addr += address_size;
2489 if (start == 0 && length == 0)
2490 break;
2491 if (start == 0 && !per_bfd->has_section_at_zero)
2492 {
2493 /* Symbol was eliminated due to a COMDAT group. */
2494 continue;
2495 }
2496 ULONGEST end = start + length;
2497 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2498 - baseaddr);
2499 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2500 - baseaddr);
2501 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2502 }
2503
2504 per_cu->addresses_seen = true;
2505 }
2506
2507 return true;
2508 }
2509
2510 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2511 populate the psymtabs_addrmap. */
2512
2513 static void
2514 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2515 struct dwarf2_section_info *section)
2516 {
2517 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2518
2519 auto_obstack temp_obstack;
2520 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2521
2522 if (read_addrmap_from_aranges (per_objfile, section, mutable_map))
2523 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2524 &per_bfd->obstack);
2525 }
2526
2527 /* A helper function that reads the .gdb_index from BUFFER and fills
2528 in MAP. FILENAME is the name of the file containing the data;
2529 it is used for error reporting. DEPRECATED_OK is true if it is
2530 ok to use deprecated sections.
2531
2532 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2533 out parameters that are filled in with information about the CU and
2534 TU lists in the section.
2535
2536 Returns true if all went well, false otherwise. */
2537
2538 static bool
2539 read_gdb_index_from_buffer (const char *filename,
2540 bool deprecated_ok,
2541 gdb::array_view<const gdb_byte> buffer,
2542 struct mapped_index *map,
2543 const gdb_byte **cu_list,
2544 offset_type *cu_list_elements,
2545 const gdb_byte **types_list,
2546 offset_type *types_list_elements)
2547 {
2548 const gdb_byte *addr = &buffer[0];
2549 offset_view metadata (buffer);
2550
2551 /* Version check. */
2552 offset_type version = metadata[0];
2553 /* Versions earlier than 3 emitted every copy of a psymbol. This
2554 causes the index to behave very poorly for certain requests. Version 3
2555 contained incomplete addrmap. So, it seems better to just ignore such
2556 indices. */
2557 if (version < 4)
2558 {
2559 static int warning_printed = 0;
2560 if (!warning_printed)
2561 {
2562 warning (_("Skipping obsolete .gdb_index section in %s."),
2563 filename);
2564 warning_printed = 1;
2565 }
2566 return 0;
2567 }
2568 /* Index version 4 uses a different hash function than index version
2569 5 and later.
2570
2571 Versions earlier than 6 did not emit psymbols for inlined
2572 functions. Using these files will cause GDB not to be able to
2573 set breakpoints on inlined functions by name, so we ignore these
2574 indices unless the user has done
2575 "set use-deprecated-index-sections on". */
2576 if (version < 6 && !deprecated_ok)
2577 {
2578 static int warning_printed = 0;
2579 if (!warning_printed)
2580 {
2581 warning (_("\
2582 Skipping deprecated .gdb_index section in %s.\n\
2583 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2584 to use the section anyway."),
2585 filename);
2586 warning_printed = 1;
2587 }
2588 return 0;
2589 }
2590 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2591 of the TU (for symbols coming from TUs),
2592 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2593 Plus gold-generated indices can have duplicate entries for global symbols,
2594 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2595 These are just performance bugs, and we can't distinguish gdb-generated
2596 indices from gold-generated ones, so issue no warning here. */
2597
2598 /* Indexes with higher version than the one supported by GDB may be no
2599 longer backward compatible. */
2600 if (version > 8)
2601 return 0;
2602
2603 map->version = version;
2604
2605 int i = 1;
2606 *cu_list = addr + metadata[i];
2607 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2608 ++i;
2609
2610 *types_list = addr + metadata[i];
2611 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2612 ++i;
2613
2614 const gdb_byte *address_table = addr + metadata[i];
2615 const gdb_byte *address_table_end = addr + metadata[i + 1];
2616 map->address_table
2617 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2618 ++i;
2619
2620 const gdb_byte *symbol_table = addr + metadata[i];
2621 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2622 map->symbol_table
2623 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2624 symbol_table_end));
2625
2626 ++i;
2627 map->constant_pool = buffer.slice (metadata[i]);
2628
2629 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2630 {
2631 /* An empty constant pool implies that all symbol table entries are
2632 empty. Make map->symbol_table.empty () == true. */
2633 map->symbol_table
2634 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2635 symbol_table));
2636 }
2637
2638 return 1;
2639 }
2640
2641 /* Callback types for dwarf2_read_gdb_index. */
2642
2643 typedef gdb::function_view
2644 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2645 get_gdb_index_contents_ftype;
2646 typedef gdb::function_view
2647 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2648 get_gdb_index_contents_dwz_ftype;
2649
2650 /* Read .gdb_index. If everything went ok, initialize the "quick"
2651 elements of all the CUs and return 1. Otherwise, return 0. */
2652
2653 static int
2654 dwarf2_read_gdb_index
2655 (dwarf2_per_objfile *per_objfile,
2656 get_gdb_index_contents_ftype get_gdb_index_contents,
2657 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2658 {
2659 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2660 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2661 struct dwz_file *dwz;
2662 struct objfile *objfile = per_objfile->objfile;
2663 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2664
2665 gdb::array_view<const gdb_byte> main_index_contents
2666 = get_gdb_index_contents (objfile, per_bfd);
2667
2668 if (main_index_contents.empty ())
2669 return 0;
2670
2671 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2672 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2673 use_deprecated_index_sections,
2674 main_index_contents, map.get (), &cu_list,
2675 &cu_list_elements, &types_list,
2676 &types_list_elements))
2677 return 0;
2678
2679 /* Don't use the index if it's empty. */
2680 if (map->symbol_table.empty ())
2681 return 0;
2682
2683 /* If there is a .dwz file, read it so we can get its CU list as
2684 well. */
2685 dwz = dwarf2_get_dwz_file (per_bfd);
2686 if (dwz != NULL)
2687 {
2688 struct mapped_index dwz_map;
2689 const gdb_byte *dwz_types_ignore;
2690 offset_type dwz_types_elements_ignore;
2691
2692 gdb::array_view<const gdb_byte> dwz_index_content
2693 = get_gdb_index_contents_dwz (objfile, dwz);
2694
2695 if (dwz_index_content.empty ())
2696 return 0;
2697
2698 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2699 1, dwz_index_content, &dwz_map,
2700 &dwz_list, &dwz_list_elements,
2701 &dwz_types_ignore,
2702 &dwz_types_elements_ignore))
2703 {
2704 warning (_("could not read '.gdb_index' section from %s; skipping"),
2705 bfd_get_filename (dwz->dwz_bfd.get ()));
2706 return 0;
2707 }
2708 }
2709
2710 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2711 dwz_list_elements);
2712
2713 if (types_list_elements)
2714 {
2715 /* We can only handle a single .debug_types when we have an
2716 index. */
2717 if (per_bfd->types.size () != 1)
2718 return 0;
2719
2720 dwarf2_section_info *section = &per_bfd->types[0];
2721
2722 create_signatured_type_table_from_index (per_bfd, section, types_list,
2723 types_list_elements);
2724 }
2725
2726 create_addrmap_from_index (per_objfile, map.get ());
2727
2728 per_bfd->index_table = std::move (map);
2729 per_bfd->quick_file_names_table =
2730 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2731
2732 return 1;
2733 }
2734
2735 /* die_reader_func for dw2_get_file_names. */
2736
2737 static void
2738 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2739 struct die_info *comp_unit_die)
2740 {
2741 struct dwarf2_cu *cu = reader->cu;
2742 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2743 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2744 struct dwarf2_per_cu_data *lh_cu;
2745 struct attribute *attr;
2746 void **slot;
2747 struct quick_file_names *qfn;
2748
2749 gdb_assert (! this_cu->is_debug_types);
2750
2751 this_cu->files_read = true;
2752 /* Our callers never want to match partial units -- instead they
2753 will match the enclosing full CU. */
2754 if (comp_unit_die->tag == DW_TAG_partial_unit)
2755 return;
2756
2757 lh_cu = this_cu;
2758 slot = NULL;
2759
2760 line_header_up lh;
2761 sect_offset line_offset {};
2762
2763 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2764 if (attr != nullptr && attr->form_is_unsigned ())
2765 {
2766 struct quick_file_names find_entry;
2767
2768 line_offset = (sect_offset) attr->as_unsigned ();
2769
2770 /* We may have already read in this line header (TU line header sharing).
2771 If we have we're done. */
2772 find_entry.hash.dwo_unit = cu->dwo_unit;
2773 find_entry.hash.line_sect_off = line_offset;
2774 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2775 &find_entry, INSERT);
2776 if (*slot != NULL)
2777 {
2778 lh_cu->file_names = (struct quick_file_names *) *slot;
2779 return;
2780 }
2781
2782 lh = dwarf_decode_line_header (line_offset, cu);
2783 }
2784
2785 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
2786
2787 int offset = 0;
2788 if (!fnd.is_unknown ())
2789 ++offset;
2790 else if (lh == nullptr)
2791 return;
2792
2793 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2794 qfn->hash.dwo_unit = cu->dwo_unit;
2795 qfn->hash.line_sect_off = line_offset;
2796 /* There may not be a DW_AT_stmt_list. */
2797 if (slot != nullptr)
2798 *slot = qfn;
2799
2800 std::vector<const char *> include_names;
2801 if (lh != nullptr)
2802 {
2803 for (const auto &entry : lh->file_names ())
2804 {
2805 gdb::unique_xmalloc_ptr<char> name_holder;
2806 const char *include_name =
2807 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
2808 if (include_name != nullptr)
2809 {
2810 include_name = per_objfile->objfile->intern (include_name);
2811 include_names.push_back (include_name);
2812 }
2813 }
2814 }
2815
2816 qfn->num_file_names = offset + include_names.size ();
2817 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
2818 qfn->file_names =
2819 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2820 qfn->num_file_names);
2821 if (offset != 0)
2822 qfn->file_names[0] = xstrdup (fnd.get_name ());
2823
2824 if (!include_names.empty ())
2825 memcpy (&qfn->file_names[offset], include_names.data (),
2826 include_names.size () * sizeof (const char *));
2827
2828 qfn->real_names = NULL;
2829
2830 lh_cu->file_names = qfn;
2831 }
2832
2833 /* A helper for the "quick" functions which attempts to read the line
2834 table for THIS_CU. */
2835
2836 static struct quick_file_names *
2837 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2838 dwarf2_per_objfile *per_objfile)
2839 {
2840 /* This should never be called for TUs. */
2841 gdb_assert (! this_cu->is_debug_types);
2842
2843 if (this_cu->files_read)
2844 return this_cu->file_names;
2845
2846 cutu_reader reader (this_cu, per_objfile);
2847 if (!reader.dummy_p)
2848 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2849
2850 return this_cu->file_names;
2851 }
2852
2853 /* A helper for the "quick" functions which computes and caches the
2854 real path for a given file name from the line table. */
2855
2856 static const char *
2857 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2858 struct quick_file_names *qfn, int index)
2859 {
2860 if (qfn->real_names == NULL)
2861 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
2862 qfn->num_file_names, const char *);
2863
2864 if (qfn->real_names[index] == NULL)
2865 {
2866 const char *dirname = nullptr;
2867
2868 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
2869 dirname = qfn->comp_dir;
2870
2871 gdb::unique_xmalloc_ptr<char> fullname;
2872 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
2873
2874 qfn->real_names[index] = fullname.release ();
2875 }
2876
2877 return qfn->real_names[index];
2878 }
2879
2880 struct symtab *
2881 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
2882 {
2883 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2884 dwarf2_per_cu_data *dwarf_cu
2885 = per_objfile->per_bfd->all_comp_units.back ().get ();
2886 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2887
2888 if (cust == NULL)
2889 return NULL;
2890
2891 return cust->primary_filetab ();
2892 }
2893
2894 /* See read.h. */
2895
2896 void
2897 dwarf2_per_cu_data::free_cached_file_names ()
2898 {
2899 if (fnd != nullptr)
2900 fnd->forget_fullname ();
2901
2902 if (per_bfd == nullptr)
2903 return;
2904
2905 struct quick_file_names *file_data = file_names;
2906 if (file_data != nullptr && file_data->real_names != nullptr)
2907 {
2908 for (int i = 0; i < file_data->num_file_names; ++i)
2909 {
2910 xfree ((void *) file_data->real_names[i]);
2911 file_data->real_names[i] = nullptr;
2912 }
2913 }
2914 }
2915
2916 void
2917 dwarf2_base_index_functions::forget_cached_source_info
2918 (struct objfile *objfile)
2919 {
2920 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2921
2922 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
2923 per_cu->free_cached_file_names ();
2924 }
2925
2926 /* Struct used to manage iterating over all CUs looking for a symbol. */
2927
2928 struct dw2_symtab_iterator
2929 {
2930 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2931 dwarf2_per_objfile *per_objfile;
2932 /* If set, only look for symbols that match that block. Valid values are
2933 GLOBAL_BLOCK and STATIC_BLOCK. */
2934 gdb::optional<block_enum> block_index;
2935 /* The kind of symbol we're looking for. */
2936 domain_enum domain;
2937 /* The list of CUs from the index entry of the symbol,
2938 or NULL if not found. */
2939 offset_view vec;
2940 /* The next element in VEC to look at. */
2941 int next;
2942 /* The number of elements in VEC, or zero if there is no match. */
2943 int length;
2944 /* Have we seen a global version of the symbol?
2945 If so we can ignore all further global instances.
2946 This is to work around gold/15646, inefficient gold-generated
2947 indices. */
2948 int global_seen;
2949 };
2950
2951 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2952
2953 static void
2954 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
2955 dwarf2_per_objfile *per_objfile,
2956 gdb::optional<block_enum> block_index,
2957 domain_enum domain, offset_type namei)
2958 {
2959 iter->per_objfile = per_objfile;
2960 iter->block_index = block_index;
2961 iter->domain = domain;
2962 iter->next = 0;
2963 iter->global_seen = 0;
2964 iter->vec = {};
2965 iter->length = 0;
2966
2967 mapped_index *index = per_objfile->per_bfd->index_table.get ();
2968 /* index is NULL if OBJF_READNOW. */
2969 if (index == NULL)
2970 return;
2971
2972 gdb_assert (!index->symbol_name_slot_invalid (namei));
2973 offset_type vec_idx = index->symbol_vec_index (namei);
2974
2975 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
2976 iter->length = iter->vec[0];
2977 }
2978
2979 /* Return the next matching CU or NULL if there are no more. */
2980
2981 static struct dwarf2_per_cu_data *
2982 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
2983 {
2984 dwarf2_per_objfile *per_objfile = iter->per_objfile;
2985
2986 for ( ; iter->next < iter->length; ++iter->next)
2987 {
2988 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
2989 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2990 gdb_index_symbol_kind symbol_kind =
2991 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2992 /* Only check the symbol attributes if they're present.
2993 Indices prior to version 7 don't record them,
2994 and indices >= 7 may elide them for certain symbols
2995 (gold does this). */
2996 int attrs_valid =
2997 (per_objfile->per_bfd->index_table->version >= 7
2998 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
2999
3000 /* Don't crash on bad data. */
3001 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3002 {
3003 complaint (_(".gdb_index entry has bad CU index"
3004 " [in module %s]"), objfile_name (per_objfile->objfile));
3005 continue;
3006 }
3007
3008 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3009
3010 /* Skip if already read in. */
3011 if (per_objfile->symtab_set_p (per_cu))
3012 continue;
3013
3014 /* Check static vs global. */
3015 if (attrs_valid)
3016 {
3017 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3018
3019 if (iter->block_index.has_value ())
3020 {
3021 bool want_static = *iter->block_index == STATIC_BLOCK;
3022
3023 if (is_static != want_static)
3024 continue;
3025 }
3026
3027 /* Work around gold/15646. */
3028 if (!is_static
3029 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3030 {
3031 if (iter->global_seen)
3032 continue;
3033
3034 iter->global_seen = 1;
3035 }
3036 }
3037
3038 /* Only check the symbol's kind if it has one. */
3039 if (attrs_valid)
3040 {
3041 switch (iter->domain)
3042 {
3043 case VAR_DOMAIN:
3044 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3045 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3046 /* Some types are also in VAR_DOMAIN. */
3047 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3048 continue;
3049 break;
3050 case STRUCT_DOMAIN:
3051 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3052 continue;
3053 break;
3054 case LABEL_DOMAIN:
3055 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3056 continue;
3057 break;
3058 case MODULE_DOMAIN:
3059 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3060 continue;
3061 break;
3062 default:
3063 break;
3064 }
3065 }
3066
3067 ++iter->next;
3068 return per_cu;
3069 }
3070
3071 return NULL;
3072 }
3073
3074 void
3075 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3076 bool print_bcache)
3077 {
3078 if (print_bcache)
3079 return;
3080
3081 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3082 int total = per_objfile->per_bfd->all_comp_units.size ();
3083 int count = 0;
3084
3085 for (int i = 0; i < total; ++i)
3086 {
3087 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3088
3089 if (!per_objfile->symtab_set_p (per_cu))
3090 ++count;
3091 }
3092 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3093 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3094 }
3095
3096 /* This dumps minimal information about the index.
3097 It is called via "mt print objfiles".
3098 One use is to verify .gdb_index has been loaded by the
3099 gdb.dwarf2/gdb-index.exp testcase. */
3100
3101 void
3102 dwarf2_gdb_index::dump (struct objfile *objfile)
3103 {
3104 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3105
3106 gdb_printf (".gdb_index:");
3107 if (per_objfile->per_bfd->index_table != NULL)
3108 {
3109 gdb_printf (" version %d\n",
3110 per_objfile->per_bfd->index_table->version);
3111 }
3112 else
3113 gdb_printf (" faked for \"readnow\"\n");
3114 gdb_printf ("\n");
3115 }
3116
3117 void
3118 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3119 {
3120 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3121 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3122
3123 for (int i = 0; i < total_units; ++i)
3124 {
3125 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3126
3127 /* We don't want to directly expand a partial CU, because if we
3128 read it with the wrong language, then assertion failures can
3129 be triggered later on. See PR symtab/23010. So, tell
3130 dw2_instantiate_symtab to skip partial CUs -- any important
3131 partial CU will be read via DW_TAG_imported_unit anyway. */
3132 dw2_instantiate_symtab (per_cu, per_objfile, true);
3133 }
3134 }
3135
3136 static bool
3137 dw2_expand_symtabs_matching_symbol
3138 (mapped_index_base &index,
3139 const lookup_name_info &lookup_name_in,
3140 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3141 gdb::function_view<bool (offset_type)> match_callback,
3142 dwarf2_per_objfile *per_objfile);
3143
3144 static bool
3145 dw2_expand_symtabs_matching_one
3146 (dwarf2_per_cu_data *per_cu,
3147 dwarf2_per_objfile *per_objfile,
3148 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3149 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3150
3151 void
3152 dwarf2_gdb_index::expand_matching_symbols
3153 (struct objfile *objfile,
3154 const lookup_name_info &name, domain_enum domain,
3155 int global,
3156 symbol_compare_ftype *ordered_compare)
3157 {
3158 /* Used for Ada. */
3159 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3160
3161 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3162
3163 if (per_objfile->per_bfd->index_table != nullptr)
3164 {
3165 mapped_index &index = *per_objfile->per_bfd->index_table;
3166
3167 const char *match_name = name.ada ().lookup_name ().c_str ();
3168 auto matcher = [&] (const char *symname)
3169 {
3170 if (ordered_compare == nullptr)
3171 return true;
3172 return ordered_compare (symname, match_name) == 0;
3173 };
3174
3175 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3176 [&] (offset_type namei)
3177 {
3178 struct dw2_symtab_iterator iter;
3179 struct dwarf2_per_cu_data *per_cu;
3180
3181 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3182 namei);
3183 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3184 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3185 nullptr);
3186 return true;
3187 }, per_objfile);
3188 }
3189 else
3190 {
3191 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3192 proceed assuming all symtabs have been read in. */
3193 }
3194 }
3195
3196 /* Starting from a search name, return the string that finds the upper
3197 bound of all strings that start with SEARCH_NAME in a sorted name
3198 list. Returns the empty string to indicate that the upper bound is
3199 the end of the list. */
3200
3201 static std::string
3202 make_sort_after_prefix_name (const char *search_name)
3203 {
3204 /* When looking to complete "func", we find the upper bound of all
3205 symbols that start with "func" by looking for where we'd insert
3206 the closest string that would follow "func" in lexicographical
3207 order. Usually, that's "func"-with-last-character-incremented,
3208 i.e. "fund". Mind non-ASCII characters, though. Usually those
3209 will be UTF-8 multi-byte sequences, but we can't be certain.
3210 Especially mind the 0xff character, which is a valid character in
3211 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3212 rule out compilers allowing it in identifiers. Note that
3213 conveniently, strcmp/strcasecmp are specified to compare
3214 characters interpreted as unsigned char. So what we do is treat
3215 the whole string as a base 256 number composed of a sequence of
3216 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3217 to 0, and carries 1 to the following more-significant position.
3218 If the very first character in SEARCH_NAME ends up incremented
3219 and carries/overflows, then the upper bound is the end of the
3220 list. The string after the empty string is also the empty
3221 string.
3222
3223 Some examples of this operation:
3224
3225 SEARCH_NAME => "+1" RESULT
3226
3227 "abc" => "abd"
3228 "ab\xff" => "ac"
3229 "\xff" "a" "\xff" => "\xff" "b"
3230 "\xff" => ""
3231 "\xff\xff" => ""
3232 "" => ""
3233
3234 Then, with these symbols for example:
3235
3236 func
3237 func1
3238 fund
3239
3240 completing "func" looks for symbols between "func" and
3241 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3242 which finds "func" and "func1", but not "fund".
3243
3244 And with:
3245
3246 funcÿ (Latin1 'ÿ' [0xff])
3247 funcÿ1
3248 fund
3249
3250 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3251 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3252
3253 And with:
3254
3255 ÿÿ (Latin1 'ÿ' [0xff])
3256 ÿÿ1
3257
3258 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3259 the end of the list.
3260 */
3261 std::string after = search_name;
3262 while (!after.empty () && (unsigned char) after.back () == 0xff)
3263 after.pop_back ();
3264 if (!after.empty ())
3265 after.back () = (unsigned char) after.back () + 1;
3266 return after;
3267 }
3268
3269 /* See declaration. */
3270
3271 std::pair<std::vector<name_component>::const_iterator,
3272 std::vector<name_component>::const_iterator>
3273 mapped_index_base::find_name_components_bounds
3274 (const lookup_name_info &lookup_name_without_params, language lang,
3275 dwarf2_per_objfile *per_objfile) const
3276 {
3277 auto *name_cmp
3278 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3279
3280 const char *lang_name
3281 = lookup_name_without_params.language_lookup_name (lang);
3282
3283 /* Comparison function object for lower_bound that matches against a
3284 given symbol name. */
3285 auto lookup_compare_lower = [&] (const name_component &elem,
3286 const char *name)
3287 {
3288 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3289 const char *elem_name = elem_qualified + elem.name_offset;
3290 return name_cmp (elem_name, name) < 0;
3291 };
3292
3293 /* Comparison function object for upper_bound that matches against a
3294 given symbol name. */
3295 auto lookup_compare_upper = [&] (const char *name,
3296 const name_component &elem)
3297 {
3298 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3299 const char *elem_name = elem_qualified + elem.name_offset;
3300 return name_cmp (name, elem_name) < 0;
3301 };
3302
3303 auto begin = this->name_components.begin ();
3304 auto end = this->name_components.end ();
3305
3306 /* Find the lower bound. */
3307 auto lower = [&] ()
3308 {
3309 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3310 return begin;
3311 else
3312 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3313 } ();
3314
3315 /* Find the upper bound. */
3316 auto upper = [&] ()
3317 {
3318 if (lookup_name_without_params.completion_mode ())
3319 {
3320 /* In completion mode, we want UPPER to point past all
3321 symbols names that have the same prefix. I.e., with
3322 these symbols, and completing "func":
3323
3324 function << lower bound
3325 function1
3326 other_function << upper bound
3327
3328 We find the upper bound by looking for the insertion
3329 point of "func"-with-last-character-incremented,
3330 i.e. "fund". */
3331 std::string after = make_sort_after_prefix_name (lang_name);
3332 if (after.empty ())
3333 return end;
3334 return std::lower_bound (lower, end, after.c_str (),
3335 lookup_compare_lower);
3336 }
3337 else
3338 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3339 } ();
3340
3341 return {lower, upper};
3342 }
3343
3344 /* See declaration. */
3345
3346 void
3347 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3348 {
3349 if (!this->name_components.empty ())
3350 return;
3351
3352 this->name_components_casing = case_sensitivity;
3353 auto *name_cmp
3354 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3355
3356 /* The code below only knows how to break apart components of C++
3357 symbol names (and other languages that use '::' as
3358 namespace/module separator) and Ada symbol names. */
3359 auto count = this->symbol_name_count ();
3360 for (offset_type idx = 0; idx < count; idx++)
3361 {
3362 if (this->symbol_name_slot_invalid (idx))
3363 continue;
3364
3365 const char *name = this->symbol_name_at (idx, per_objfile);
3366
3367 /* Add each name component to the name component table. */
3368 unsigned int previous_len = 0;
3369
3370 if (strstr (name, "::") != nullptr)
3371 {
3372 for (unsigned int current_len = cp_find_first_component (name);
3373 name[current_len] != '\0';
3374 current_len += cp_find_first_component (name + current_len))
3375 {
3376 gdb_assert (name[current_len] == ':');
3377 this->name_components.push_back ({previous_len, idx});
3378 /* Skip the '::'. */
3379 current_len += 2;
3380 previous_len = current_len;
3381 }
3382 }
3383 else
3384 {
3385 /* Handle the Ada encoded (aka mangled) form here. */
3386 for (const char *iter = strstr (name, "__");
3387 iter != nullptr;
3388 iter = strstr (iter, "__"))
3389 {
3390 this->name_components.push_back ({previous_len, idx});
3391 iter += 2;
3392 previous_len = iter - name;
3393 }
3394 }
3395
3396 this->name_components.push_back ({previous_len, idx});
3397 }
3398
3399 /* Sort name_components elements by name. */
3400 auto name_comp_compare = [&] (const name_component &left,
3401 const name_component &right)
3402 {
3403 const char *left_qualified
3404 = this->symbol_name_at (left.idx, per_objfile);
3405 const char *right_qualified
3406 = this->symbol_name_at (right.idx, per_objfile);
3407
3408 const char *left_name = left_qualified + left.name_offset;
3409 const char *right_name = right_qualified + right.name_offset;
3410
3411 return name_cmp (left_name, right_name) < 0;
3412 };
3413
3414 std::sort (this->name_components.begin (),
3415 this->name_components.end (),
3416 name_comp_compare);
3417 }
3418
3419 /* Helper for dw2_expand_symtabs_matching that works with a
3420 mapped_index_base instead of the containing objfile. This is split
3421 to a separate function in order to be able to unit test the
3422 name_components matching using a mock mapped_index_base. For each
3423 symbol name that matches, calls MATCH_CALLBACK, passing it the
3424 symbol's index in the mapped_index_base symbol table. */
3425
3426 static bool
3427 dw2_expand_symtabs_matching_symbol
3428 (mapped_index_base &index,
3429 const lookup_name_info &lookup_name_in,
3430 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3431 gdb::function_view<bool (offset_type)> match_callback,
3432 dwarf2_per_objfile *per_objfile)
3433 {
3434 lookup_name_info lookup_name_without_params
3435 = lookup_name_in.make_ignore_params ();
3436
3437 /* Build the symbol name component sorted vector, if we haven't
3438 yet. */
3439 index.build_name_components (per_objfile);
3440
3441 /* The same symbol may appear more than once in the range though.
3442 E.g., if we're looking for symbols that complete "w", and we have
3443 a symbol named "w1::w2", we'll find the two name components for
3444 that same symbol in the range. To be sure we only call the
3445 callback once per symbol, we first collect the symbol name
3446 indexes that matched in a temporary vector and ignore
3447 duplicates. */
3448 std::vector<offset_type> matches;
3449
3450 struct name_and_matcher
3451 {
3452 symbol_name_matcher_ftype *matcher;
3453 const char *name;
3454
3455 bool operator== (const name_and_matcher &other) const
3456 {
3457 return matcher == other.matcher && strcmp (name, other.name) == 0;
3458 }
3459 };
3460
3461 /* A vector holding all the different symbol name matchers, for all
3462 languages. */
3463 std::vector<name_and_matcher> matchers;
3464
3465 for (int i = 0; i < nr_languages; i++)
3466 {
3467 enum language lang_e = (enum language) i;
3468
3469 const language_defn *lang = language_def (lang_e);
3470 symbol_name_matcher_ftype *name_matcher
3471 = lang->get_symbol_name_matcher (lookup_name_without_params);
3472
3473 name_and_matcher key {
3474 name_matcher,
3475 lookup_name_without_params.language_lookup_name (lang_e)
3476 };
3477
3478 /* Don't insert the same comparison routine more than once.
3479 Note that we do this linear walk. This is not a problem in
3480 practice because the number of supported languages is
3481 low. */
3482 if (std::find (matchers.begin (), matchers.end (), key)
3483 != matchers.end ())
3484 continue;
3485 matchers.push_back (std::move (key));
3486
3487 auto bounds
3488 = index.find_name_components_bounds (lookup_name_without_params,
3489 lang_e, per_objfile);
3490
3491 /* Now for each symbol name in range, check to see if we have a name
3492 match, and if so, call the MATCH_CALLBACK callback. */
3493
3494 for (; bounds.first != bounds.second; ++bounds.first)
3495 {
3496 const char *qualified
3497 = index.symbol_name_at (bounds.first->idx, per_objfile);
3498
3499 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3500 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3501 continue;
3502
3503 matches.push_back (bounds.first->idx);
3504 }
3505 }
3506
3507 std::sort (matches.begin (), matches.end ());
3508
3509 /* Finally call the callback, once per match. */
3510 ULONGEST prev = -1;
3511 bool result = true;
3512 for (offset_type idx : matches)
3513 {
3514 if (prev != idx)
3515 {
3516 if (!match_callback (idx))
3517 {
3518 result = false;
3519 break;
3520 }
3521 prev = idx;
3522 }
3523 }
3524
3525 /* Above we use a type wider than idx's for 'prev', since 0 and
3526 (offset_type)-1 are both possible values. */
3527 static_assert (sizeof (prev) > sizeof (offset_type), "");
3528
3529 return result;
3530 }
3531
3532 #if GDB_SELF_TEST
3533
3534 namespace selftests { namespace dw2_expand_symtabs_matching {
3535
3536 /* A mock .gdb_index/.debug_names-like name index table, enough to
3537 exercise dw2_expand_symtabs_matching_symbol, which works with the
3538 mapped_index_base interface. Builds an index from the symbol list
3539 passed as parameter to the constructor. */
3540 class mock_mapped_index : public mapped_index_base
3541 {
3542 public:
3543 mock_mapped_index (gdb::array_view<const char *> symbols)
3544 : m_symbol_table (symbols)
3545 {}
3546
3547 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3548
3549 /* Return the number of names in the symbol table. */
3550 size_t symbol_name_count () const override
3551 {
3552 return m_symbol_table.size ();
3553 }
3554
3555 /* Get the name of the symbol at IDX in the symbol table. */
3556 const char *symbol_name_at
3557 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3558 {
3559 return m_symbol_table[idx];
3560 }
3561
3562 private:
3563 gdb::array_view<const char *> m_symbol_table;
3564 };
3565
3566 /* Convenience function that converts a NULL pointer to a "<null>"
3567 string, to pass to print routines. */
3568
3569 static const char *
3570 string_or_null (const char *str)
3571 {
3572 return str != NULL ? str : "<null>";
3573 }
3574
3575 /* Check if a lookup_name_info built from
3576 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3577 index. EXPECTED_LIST is the list of expected matches, in expected
3578 matching order. If no match expected, then an empty list is
3579 specified. Returns true on success. On failure prints a warning
3580 indicating the file:line that failed, and returns false. */
3581
3582 static bool
3583 check_match (const char *file, int line,
3584 mock_mapped_index &mock_index,
3585 const char *name, symbol_name_match_type match_type,
3586 bool completion_mode,
3587 std::initializer_list<const char *> expected_list,
3588 dwarf2_per_objfile *per_objfile)
3589 {
3590 lookup_name_info lookup_name (name, match_type, completion_mode);
3591
3592 bool matched = true;
3593
3594 auto mismatch = [&] (const char *expected_str,
3595 const char *got)
3596 {
3597 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3598 "expected=\"%s\", got=\"%s\"\n"),
3599 file, line,
3600 (match_type == symbol_name_match_type::FULL
3601 ? "FULL" : "WILD"),
3602 name, string_or_null (expected_str), string_or_null (got));
3603 matched = false;
3604 };
3605
3606 auto expected_it = expected_list.begin ();
3607 auto expected_end = expected_list.end ();
3608
3609 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3610 nullptr,
3611 [&] (offset_type idx)
3612 {
3613 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3614 const char *expected_str
3615 = expected_it == expected_end ? NULL : *expected_it++;
3616
3617 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3618 mismatch (expected_str, matched_name);
3619 return true;
3620 }, per_objfile);
3621
3622 const char *expected_str
3623 = expected_it == expected_end ? NULL : *expected_it++;
3624 if (expected_str != NULL)
3625 mismatch (expected_str, NULL);
3626
3627 return matched;
3628 }
3629
3630 /* The symbols added to the mock mapped_index for testing (in
3631 canonical form). */
3632 static const char *test_symbols[] = {
3633 "function",
3634 "std::bar",
3635 "std::zfunction",
3636 "std::zfunction2",
3637 "w1::w2",
3638 "ns::foo<char*>",
3639 "ns::foo<int>",
3640 "ns::foo<long>",
3641 "ns2::tmpl<int>::foo2",
3642 "(anonymous namespace)::A::B::C",
3643
3644 /* These are used to check that the increment-last-char in the
3645 matching algorithm for completion doesn't match "t1_fund" when
3646 completing "t1_func". */
3647 "t1_func",
3648 "t1_func1",
3649 "t1_fund",
3650 "t1_fund1",
3651
3652 /* A UTF-8 name with multi-byte sequences to make sure that
3653 cp-name-parser understands this as a single identifier ("função"
3654 is "function" in PT). */
3655 u8"u8função",
3656
3657 /* \377 (0xff) is Latin1 'ÿ'. */
3658 "yfunc\377",
3659
3660 /* \377 (0xff) is Latin1 'ÿ'. */
3661 "\377",
3662 "\377\377123",
3663
3664 /* A name with all sorts of complications. Starts with "z" to make
3665 it easier for the completion tests below. */
3666 #define Z_SYM_NAME \
3667 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3668 "::tuple<(anonymous namespace)::ui*, " \
3669 "std::default_delete<(anonymous namespace)::ui>, void>"
3670
3671 Z_SYM_NAME
3672 };
3673
3674 /* Returns true if the mapped_index_base::find_name_component_bounds
3675 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3676 in completion mode. */
3677
3678 static bool
3679 check_find_bounds_finds (mapped_index_base &index,
3680 const char *search_name,
3681 gdb::array_view<const char *> expected_syms,
3682 dwarf2_per_objfile *per_objfile)
3683 {
3684 lookup_name_info lookup_name (search_name,
3685 symbol_name_match_type::FULL, true);
3686
3687 auto bounds = index.find_name_components_bounds (lookup_name,
3688 language_cplus,
3689 per_objfile);
3690
3691 size_t distance = std::distance (bounds.first, bounds.second);
3692 if (distance != expected_syms.size ())
3693 return false;
3694
3695 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3696 {
3697 auto nc_elem = bounds.first + exp_elem;
3698 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3699 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3700 return false;
3701 }
3702
3703 return true;
3704 }
3705
3706 /* Test the lower-level mapped_index::find_name_component_bounds
3707 method. */
3708
3709 static void
3710 test_mapped_index_find_name_component_bounds ()
3711 {
3712 mock_mapped_index mock_index (test_symbols);
3713
3714 mock_index.build_name_components (NULL /* per_objfile */);
3715
3716 /* Test the lower-level mapped_index::find_name_component_bounds
3717 method in completion mode. */
3718 {
3719 static const char *expected_syms[] = {
3720 "t1_func",
3721 "t1_func1",
3722 };
3723
3724 SELF_CHECK (check_find_bounds_finds
3725 (mock_index, "t1_func", expected_syms,
3726 NULL /* per_objfile */));
3727 }
3728
3729 /* Check that the increment-last-char in the name matching algorithm
3730 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3731 {
3732 static const char *expected_syms1[] = {
3733 "\377",
3734 "\377\377123",
3735 };
3736 SELF_CHECK (check_find_bounds_finds
3737 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3738
3739 static const char *expected_syms2[] = {
3740 "\377\377123",
3741 };
3742 SELF_CHECK (check_find_bounds_finds
3743 (mock_index, "\377\377", expected_syms2,
3744 NULL /* per_objfile */));
3745 }
3746 }
3747
3748 /* Test dw2_expand_symtabs_matching_symbol. */
3749
3750 static void
3751 test_dw2_expand_symtabs_matching_symbol ()
3752 {
3753 mock_mapped_index mock_index (test_symbols);
3754
3755 /* We let all tests run until the end even if some fails, for debug
3756 convenience. */
3757 bool any_mismatch = false;
3758
3759 /* Create the expected symbols list (an initializer_list). Needed
3760 because lists have commas, and we need to pass them to CHECK,
3761 which is a macro. */
3762 #define EXPECT(...) { __VA_ARGS__ }
3763
3764 /* Wrapper for check_match that passes down the current
3765 __FILE__/__LINE__. */
3766 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3767 any_mismatch |= !check_match (__FILE__, __LINE__, \
3768 mock_index, \
3769 NAME, MATCH_TYPE, COMPLETION_MODE, \
3770 EXPECTED_LIST, NULL)
3771
3772 /* Identity checks. */
3773 for (const char *sym : test_symbols)
3774 {
3775 /* Should be able to match all existing symbols. */
3776 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3777 EXPECT (sym));
3778
3779 /* Should be able to match all existing symbols with
3780 parameters. */
3781 std::string with_params = std::string (sym) + "(int)";
3782 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3783 EXPECT (sym));
3784
3785 /* Should be able to match all existing symbols with
3786 parameters and qualifiers. */
3787 with_params = std::string (sym) + " ( int ) const";
3788 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3789 EXPECT (sym));
3790
3791 /* This should really find sym, but cp-name-parser.y doesn't
3792 know about lvalue/rvalue qualifiers yet. */
3793 with_params = std::string (sym) + " ( int ) &&";
3794 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3795 {});
3796 }
3797
3798 /* Check that the name matching algorithm for completion doesn't get
3799 confused with Latin1 'ÿ' / 0xff. */
3800 {
3801 static const char str[] = "\377";
3802 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3803 EXPECT ("\377", "\377\377123"));
3804 }
3805
3806 /* Check that the increment-last-char in the matching algorithm for
3807 completion doesn't match "t1_fund" when completing "t1_func". */
3808 {
3809 static const char str[] = "t1_func";
3810 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3811 EXPECT ("t1_func", "t1_func1"));
3812 }
3813
3814 /* Check that completion mode works at each prefix of the expected
3815 symbol name. */
3816 {
3817 static const char str[] = "function(int)";
3818 size_t len = strlen (str);
3819 std::string lookup;
3820
3821 for (size_t i = 1; i < len; i++)
3822 {
3823 lookup.assign (str, i);
3824 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3825 EXPECT ("function"));
3826 }
3827 }
3828
3829 /* While "w" is a prefix of both components, the match function
3830 should still only be called once. */
3831 {
3832 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3833 EXPECT ("w1::w2"));
3834 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3835 EXPECT ("w1::w2"));
3836 }
3837
3838 /* Same, with a "complicated" symbol. */
3839 {
3840 static const char str[] = Z_SYM_NAME;
3841 size_t len = strlen (str);
3842 std::string lookup;
3843
3844 for (size_t i = 1; i < len; i++)
3845 {
3846 lookup.assign (str, i);
3847 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3848 EXPECT (Z_SYM_NAME));
3849 }
3850 }
3851
3852 /* In FULL mode, an incomplete symbol doesn't match. */
3853 {
3854 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3855 {});
3856 }
3857
3858 /* A complete symbol with parameters matches any overload, since the
3859 index has no overload info. */
3860 {
3861 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3862 EXPECT ("std::zfunction", "std::zfunction2"));
3863 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
3864 EXPECT ("std::zfunction", "std::zfunction2"));
3865 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
3866 EXPECT ("std::zfunction", "std::zfunction2"));
3867 }
3868
3869 /* Check that whitespace is ignored appropriately. A symbol with a
3870 template argument list. */
3871 {
3872 static const char expected[] = "ns::foo<int>";
3873 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
3874 EXPECT (expected));
3875 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
3876 EXPECT (expected));
3877 }
3878
3879 /* Check that whitespace is ignored appropriately. A symbol with a
3880 template argument list that includes a pointer. */
3881 {
3882 static const char expected[] = "ns::foo<char*>";
3883 /* Try both completion and non-completion modes. */
3884 static const bool completion_mode[2] = {false, true};
3885 for (size_t i = 0; i < 2; i++)
3886 {
3887 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
3888 completion_mode[i], EXPECT (expected));
3889 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
3890 completion_mode[i], EXPECT (expected));
3891
3892 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
3893 completion_mode[i], EXPECT (expected));
3894 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
3895 completion_mode[i], EXPECT (expected));
3896 }
3897 }
3898
3899 {
3900 /* Check method qualifiers are ignored. */
3901 static const char expected[] = "ns::foo<char*>";
3902 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3903 symbol_name_match_type::FULL, true, EXPECT (expected));
3904 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3905 symbol_name_match_type::FULL, true, EXPECT (expected));
3906 CHECK_MATCH ("foo < char * > ( int ) const",
3907 symbol_name_match_type::WILD, true, EXPECT (expected));
3908 CHECK_MATCH ("foo < char * > ( int ) &&",
3909 symbol_name_match_type::WILD, true, EXPECT (expected));
3910 }
3911
3912 /* Test lookup names that don't match anything. */
3913 {
3914 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
3915 {});
3916
3917 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
3918 {});
3919 }
3920
3921 /* Some wild matching tests, exercising "(anonymous namespace)",
3922 which should not be confused with a parameter list. */
3923 {
3924 static const char *syms[] = {
3925 "A::B::C",
3926 "B::C",
3927 "C",
3928 "A :: B :: C ( int )",
3929 "B :: C ( int )",
3930 "C ( int )",
3931 };
3932
3933 for (const char *s : syms)
3934 {
3935 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
3936 EXPECT ("(anonymous namespace)::A::B::C"));
3937 }
3938 }
3939
3940 {
3941 static const char expected[] = "ns2::tmpl<int>::foo2";
3942 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
3943 EXPECT (expected));
3944 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
3945 EXPECT (expected));
3946 }
3947
3948 SELF_CHECK (!any_mismatch);
3949
3950 #undef EXPECT
3951 #undef CHECK_MATCH
3952 }
3953
3954 static void
3955 run_test ()
3956 {
3957 test_mapped_index_find_name_component_bounds ();
3958 test_dw2_expand_symtabs_matching_symbol ();
3959 }
3960
3961 }} // namespace selftests::dw2_expand_symtabs_matching
3962
3963 #endif /* GDB_SELF_TEST */
3964
3965 /* If FILE_MATCHER is NULL or if PER_CU has
3966 dwarf2_per_cu_quick_data::MARK set (see
3967 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3968 EXPANSION_NOTIFY on it. */
3969
3970 static bool
3971 dw2_expand_symtabs_matching_one
3972 (dwarf2_per_cu_data *per_cu,
3973 dwarf2_per_objfile *per_objfile,
3974 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3975 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
3976 {
3977 if (file_matcher == NULL || per_cu->mark)
3978 {
3979 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
3980
3981 compunit_symtab *symtab
3982 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3983 gdb_assert (symtab != nullptr);
3984
3985 if (expansion_notify != NULL && symtab_was_null)
3986 return expansion_notify (symtab);
3987 }
3988 return true;
3989 }
3990
3991 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3992 matched, to expand corresponding CUs that were marked. IDX is the
3993 index of the symbol name that matched. */
3994
3995 static bool
3996 dw2_expand_marked_cus
3997 (dwarf2_per_objfile *per_objfile, offset_type idx,
3998 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3999 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4000 block_search_flags search_flags,
4001 search_domain kind)
4002 {
4003 offset_type vec_len, vec_idx;
4004 bool global_seen = false;
4005 mapped_index &index = *per_objfile->per_bfd->index_table;
4006
4007 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4008 vec_len = vec[0];
4009 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4010 {
4011 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4012 /* This value is only valid for index versions >= 7. */
4013 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4014 gdb_index_symbol_kind symbol_kind =
4015 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4016 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4017 /* Only check the symbol attributes if they're present.
4018 Indices prior to version 7 don't record them,
4019 and indices >= 7 may elide them for certain symbols
4020 (gold does this). */
4021 int attrs_valid =
4022 (index.version >= 7
4023 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4024
4025 /* Work around gold/15646. */
4026 if (attrs_valid
4027 && !is_static
4028 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4029 {
4030 if (global_seen)
4031 continue;
4032
4033 global_seen = true;
4034 }
4035
4036 /* Only check the symbol's kind if it has one. */
4037 if (attrs_valid)
4038 {
4039 if (is_static)
4040 {
4041 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4042 continue;
4043 }
4044 else
4045 {
4046 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4047 continue;
4048 }
4049
4050 switch (kind)
4051 {
4052 case VARIABLES_DOMAIN:
4053 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4054 continue;
4055 break;
4056 case FUNCTIONS_DOMAIN:
4057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4058 continue;
4059 break;
4060 case TYPES_DOMAIN:
4061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4062 continue;
4063 break;
4064 case MODULES_DOMAIN:
4065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4066 continue;
4067 break;
4068 default:
4069 break;
4070 }
4071 }
4072
4073 /* Don't crash on bad data. */
4074 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4075 {
4076 complaint (_(".gdb_index entry has bad CU index"
4077 " [in module %s]"), objfile_name (per_objfile->objfile));
4078 continue;
4079 }
4080
4081 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4082 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4083 expansion_notify))
4084 return false;
4085 }
4086
4087 return true;
4088 }
4089
4090 /* If FILE_MATCHER is non-NULL, set all the
4091 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4092 that match FILE_MATCHER. */
4093
4094 static void
4095 dw_expand_symtabs_matching_file_matcher
4096 (dwarf2_per_objfile *per_objfile,
4097 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4098 {
4099 if (file_matcher == NULL)
4100 return;
4101
4102 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4103 htab_eq_pointer,
4104 NULL, xcalloc, xfree));
4105 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4106 htab_eq_pointer,
4107 NULL, xcalloc, xfree));
4108
4109 /* The rule is CUs specify all the files, including those used by
4110 any TU, so there's no need to scan TUs here. */
4111
4112 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4113 {
4114 QUIT;
4115
4116 if (per_cu->is_debug_types)
4117 continue;
4118 per_cu->mark = 0;
4119
4120 /* We only need to look at symtabs not already expanded. */
4121 if (per_objfile->symtab_set_p (per_cu.get ()))
4122 continue;
4123
4124 if (per_cu->fnd != nullptr)
4125 {
4126 file_and_directory *fnd = per_cu->fnd.get ();
4127
4128 if (file_matcher (fnd->get_name (), false))
4129 {
4130 per_cu->mark = 1;
4131 continue;
4132 }
4133
4134 /* Before we invoke realpath, which can get expensive when many
4135 files are involved, do a quick comparison of the basenames. */
4136 if ((basenames_may_differ
4137 || file_matcher (lbasename (fnd->get_name ()), true))
4138 && file_matcher (fnd->get_fullname (), false))
4139 {
4140 per_cu->mark = 1;
4141 continue;
4142 }
4143 }
4144
4145 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4146 per_objfile);
4147 if (file_data == NULL)
4148 continue;
4149
4150 if (htab_find (visited_not_found.get (), file_data) != NULL)
4151 continue;
4152 else if (htab_find (visited_found.get (), file_data) != NULL)
4153 {
4154 per_cu->mark = 1;
4155 continue;
4156 }
4157
4158 for (int j = 0; j < file_data->num_file_names; ++j)
4159 {
4160 const char *this_real_name;
4161
4162 if (file_matcher (file_data->file_names[j], false))
4163 {
4164 per_cu->mark = 1;
4165 break;
4166 }
4167
4168 /* Before we invoke realpath, which can get expensive when many
4169 files are involved, do a quick comparison of the basenames. */
4170 if (!basenames_may_differ
4171 && !file_matcher (lbasename (file_data->file_names[j]),
4172 true))
4173 continue;
4174
4175 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4176 if (file_matcher (this_real_name, false))
4177 {
4178 per_cu->mark = 1;
4179 break;
4180 }
4181 }
4182
4183 void **slot = htab_find_slot (per_cu->mark
4184 ? visited_found.get ()
4185 : visited_not_found.get (),
4186 file_data, INSERT);
4187 *slot = file_data;
4188 }
4189 }
4190
4191 bool
4192 dwarf2_gdb_index::expand_symtabs_matching
4193 (struct objfile *objfile,
4194 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4195 const lookup_name_info *lookup_name,
4196 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4197 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4198 block_search_flags search_flags,
4199 domain_enum domain,
4200 enum search_domain kind)
4201 {
4202 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4203
4204 /* index_table is NULL if OBJF_READNOW. */
4205 if (!per_objfile->per_bfd->index_table)
4206 return true;
4207
4208 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4209
4210 /* This invariant is documented in quick-functions.h. */
4211 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4212 if (lookup_name == nullptr)
4213 {
4214 for (dwarf2_per_cu_data *per_cu
4215 : all_comp_units_range (per_objfile->per_bfd))
4216 {
4217 QUIT;
4218
4219 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4220 file_matcher,
4221 expansion_notify))
4222 return false;
4223 }
4224 return true;
4225 }
4226
4227 mapped_index &index = *per_objfile->per_bfd->index_table;
4228
4229 bool result
4230 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4231 symbol_matcher,
4232 [&] (offset_type idx)
4233 {
4234 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4235 expansion_notify, search_flags, kind))
4236 return false;
4237 return true;
4238 }, per_objfile);
4239
4240 return result;
4241 }
4242
4243 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4244 symtab. */
4245
4246 static struct compunit_symtab *
4247 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4248 CORE_ADDR pc)
4249 {
4250 int i;
4251
4252 if (cust->blockvector () != nullptr
4253 && blockvector_contains_pc (cust->blockvector (), pc))
4254 return cust;
4255
4256 if (cust->includes == NULL)
4257 return NULL;
4258
4259 for (i = 0; cust->includes[i]; ++i)
4260 {
4261 struct compunit_symtab *s = cust->includes[i];
4262
4263 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4264 if (s != NULL)
4265 return s;
4266 }
4267
4268 return NULL;
4269 }
4270
4271 struct compunit_symtab *
4272 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4273 (struct objfile *objfile,
4274 struct bound_minimal_symbol msymbol,
4275 CORE_ADDR pc,
4276 struct obj_section *section,
4277 int warn_if_readin)
4278 {
4279 struct dwarf2_per_cu_data *data;
4280 struct compunit_symtab *result;
4281
4282 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4283 if (per_objfile->per_bfd->index_addrmap == nullptr)
4284 return NULL;
4285
4286 CORE_ADDR baseaddr = objfile->text_section_offset ();
4287 data = ((struct dwarf2_per_cu_data *)
4288 addrmap_find (per_objfile->per_bfd->index_addrmap,
4289 pc - baseaddr));
4290 if (!data)
4291 return NULL;
4292
4293 if (warn_if_readin && per_objfile->symtab_set_p (data))
4294 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4295 paddress (objfile->arch (), pc));
4296
4297 result = recursively_find_pc_sect_compunit_symtab
4298 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4299
4300 gdb_assert (result != NULL);
4301 return result;
4302 }
4303
4304 void
4305 dwarf2_base_index_functions::map_symbol_filenames
4306 (struct objfile *objfile,
4307 gdb::function_view<symbol_filename_ftype> fun,
4308 bool need_fullname)
4309 {
4310 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4311
4312 /* Use caches to ensure we only call FUN once for each filename. */
4313 filename_seen_cache filenames_cache;
4314 std::unordered_set<quick_file_names *> qfn_cache;
4315
4316 /* The rule is CUs specify all the files, including those used by any TU,
4317 so there's no need to scan TUs here. We can ignore file names coming
4318 from already-expanded CUs. It is possible that an expanded CU might
4319 reuse the file names data from a currently unexpanded CU, in this
4320 case we don't want to report the files from the unexpanded CU. */
4321
4322 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4323 {
4324 if (!per_cu->is_debug_types
4325 && per_objfile->symtab_set_p (per_cu.get ()))
4326 {
4327 if (per_cu->file_names != nullptr)
4328 qfn_cache.insert (per_cu->file_names);
4329 }
4330 }
4331
4332 for (dwarf2_per_cu_data *per_cu
4333 : all_comp_units_range (per_objfile->per_bfd))
4334 {
4335 /* We only need to look at symtabs not already expanded. */
4336 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4337 continue;
4338
4339 if (per_cu->fnd != nullptr)
4340 {
4341 file_and_directory *fnd = per_cu->fnd.get ();
4342
4343 const char *filename = fnd->get_name ();
4344 const char *key = filename;
4345 const char *fullname = nullptr;
4346
4347 if (need_fullname)
4348 {
4349 fullname = fnd->get_fullname ();
4350 key = fullname;
4351 }
4352
4353 if (!filenames_cache.seen (key))
4354 fun (filename, fullname);
4355 }
4356
4357 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4358 if (file_data == nullptr
4359 || qfn_cache.find (file_data) != qfn_cache.end ())
4360 continue;
4361
4362 for (int j = 0; j < file_data->num_file_names; ++j)
4363 {
4364 const char *filename = file_data->file_names[j];
4365 const char *key = filename;
4366 const char *fullname = nullptr;
4367
4368 if (need_fullname)
4369 {
4370 fullname = dw2_get_real_path (per_objfile, file_data, j);
4371 key = fullname;
4372 }
4373
4374 if (!filenames_cache.seen (key))
4375 fun (filename, fullname);
4376 }
4377 }
4378 }
4379
4380 bool
4381 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4382 {
4383 return true;
4384 }
4385
4386 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4387
4388 bool
4389 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4390 {
4391 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4392
4393 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4394 {
4395 /* Is this already expanded? */
4396 if (per_objfile->symtab_set_p (per_cu.get ()))
4397 continue;
4398
4399 /* It has not yet been expanded. */
4400 return true;
4401 }
4402
4403 return false;
4404 }
4405
4406 /* DWARF-5 debug_names reader. */
4407
4408 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4409 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4410
4411 /* A helper function that reads the .debug_names section in SECTION
4412 and fills in MAP. FILENAME is the name of the file containing the
4413 section; it is used for error reporting.
4414
4415 Returns true if all went well, false otherwise. */
4416
4417 static bool
4418 read_debug_names_from_section (struct objfile *objfile,
4419 const char *filename,
4420 struct dwarf2_section_info *section,
4421 mapped_debug_names &map)
4422 {
4423 if (section->empty ())
4424 return false;
4425
4426 /* Older elfutils strip versions could keep the section in the main
4427 executable while splitting it for the separate debug info file. */
4428 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4429 return false;
4430
4431 section->read (objfile);
4432
4433 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4434
4435 const gdb_byte *addr = section->buffer;
4436
4437 bfd *const abfd = section->get_bfd_owner ();
4438
4439 unsigned int bytes_read;
4440 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4441 addr += bytes_read;
4442
4443 map.dwarf5_is_dwarf64 = bytes_read != 4;
4444 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4445 if (bytes_read + length != section->size)
4446 {
4447 /* There may be multiple per-CU indices. */
4448 warning (_("Section .debug_names in %s length %s does not match "
4449 "section length %s, ignoring .debug_names."),
4450 filename, plongest (bytes_read + length),
4451 pulongest (section->size));
4452 return false;
4453 }
4454
4455 /* The version number. */
4456 uint16_t version = read_2_bytes (abfd, addr);
4457 addr += 2;
4458 if (version != 5)
4459 {
4460 warning (_("Section .debug_names in %s has unsupported version %d, "
4461 "ignoring .debug_names."),
4462 filename, version);
4463 return false;
4464 }
4465
4466 /* Padding. */
4467 uint16_t padding = read_2_bytes (abfd, addr);
4468 addr += 2;
4469 if (padding != 0)
4470 {
4471 warning (_("Section .debug_names in %s has unsupported padding %d, "
4472 "ignoring .debug_names."),
4473 filename, padding);
4474 return false;
4475 }
4476
4477 /* comp_unit_count - The number of CUs in the CU list. */
4478 map.cu_count = read_4_bytes (abfd, addr);
4479 addr += 4;
4480
4481 /* local_type_unit_count - The number of TUs in the local TU
4482 list. */
4483 map.tu_count = read_4_bytes (abfd, addr);
4484 addr += 4;
4485
4486 /* foreign_type_unit_count - The number of TUs in the foreign TU
4487 list. */
4488 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4489 addr += 4;
4490 if (foreign_tu_count != 0)
4491 {
4492 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4493 "ignoring .debug_names."),
4494 filename, static_cast<unsigned long> (foreign_tu_count));
4495 return false;
4496 }
4497
4498 /* bucket_count - The number of hash buckets in the hash lookup
4499 table. */
4500 map.bucket_count = read_4_bytes (abfd, addr);
4501 addr += 4;
4502
4503 /* name_count - The number of unique names in the index. */
4504 map.name_count = read_4_bytes (abfd, addr);
4505 addr += 4;
4506
4507 /* abbrev_table_size - The size in bytes of the abbreviations
4508 table. */
4509 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4510 addr += 4;
4511
4512 /* augmentation_string_size - The size in bytes of the augmentation
4513 string. This value is rounded up to a multiple of 4. */
4514 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4515 addr += 4;
4516 map.augmentation_is_gdb = ((augmentation_string_size
4517 == sizeof (dwarf5_augmentation))
4518 && memcmp (addr, dwarf5_augmentation,
4519 sizeof (dwarf5_augmentation)) == 0);
4520 augmentation_string_size += (-augmentation_string_size) & 3;
4521 addr += augmentation_string_size;
4522
4523 /* List of CUs */
4524 map.cu_table_reordered = addr;
4525 addr += map.cu_count * map.offset_size;
4526
4527 /* List of Local TUs */
4528 map.tu_table_reordered = addr;
4529 addr += map.tu_count * map.offset_size;
4530
4531 /* Hash Lookup Table */
4532 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4533 addr += map.bucket_count * 4;
4534 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4535 addr += map.name_count * 4;
4536
4537 /* Name Table */
4538 map.name_table_string_offs_reordered = addr;
4539 addr += map.name_count * map.offset_size;
4540 map.name_table_entry_offs_reordered = addr;
4541 addr += map.name_count * map.offset_size;
4542
4543 const gdb_byte *abbrev_table_start = addr;
4544 for (;;)
4545 {
4546 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4547 addr += bytes_read;
4548 if (index_num == 0)
4549 break;
4550
4551 const auto insertpair
4552 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4553 if (!insertpair.second)
4554 {
4555 warning (_("Section .debug_names in %s has duplicate index %s, "
4556 "ignoring .debug_names."),
4557 filename, pulongest (index_num));
4558 return false;
4559 }
4560 mapped_debug_names::index_val &indexval = insertpair.first->second;
4561 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4562 addr += bytes_read;
4563
4564 for (;;)
4565 {
4566 mapped_debug_names::index_val::attr attr;
4567 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4568 addr += bytes_read;
4569 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4570 addr += bytes_read;
4571 if (attr.form == DW_FORM_implicit_const)
4572 {
4573 attr.implicit_const = read_signed_leb128 (abfd, addr,
4574 &bytes_read);
4575 addr += bytes_read;
4576 }
4577 if (attr.dw_idx == 0 && attr.form == 0)
4578 break;
4579 indexval.attr_vec.push_back (std::move (attr));
4580 }
4581 }
4582 if (addr != abbrev_table_start + abbrev_table_size)
4583 {
4584 warning (_("Section .debug_names in %s has abbreviation_table "
4585 "of size %s vs. written as %u, ignoring .debug_names."),
4586 filename, plongest (addr - abbrev_table_start),
4587 abbrev_table_size);
4588 return false;
4589 }
4590 map.entry_pool = addr;
4591
4592 return true;
4593 }
4594
4595 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4596 list. */
4597
4598 static void
4599 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4600 const mapped_debug_names &map,
4601 dwarf2_section_info &section,
4602 bool is_dwz)
4603 {
4604 if (!map.augmentation_is_gdb)
4605 {
4606 for (uint32_t i = 0; i < map.cu_count; ++i)
4607 {
4608 sect_offset sect_off
4609 = (sect_offset) (extract_unsigned_integer
4610 (map.cu_table_reordered + i * map.offset_size,
4611 map.offset_size,
4612 map.dwarf5_byte_order));
4613 /* We don't know the length of the CU, because the CU list in a
4614 .debug_names index can be incomplete, so we can't use the start
4615 of the next CU as end of this CU. We create the CUs here with
4616 length 0, and in cutu_reader::cutu_reader we'll fill in the
4617 actual length. */
4618 dwarf2_per_cu_data_up per_cu
4619 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4620 sect_off, 0);
4621 per_bfd->all_comp_units.push_back (std::move (per_cu));
4622 }
4623 return;
4624 }
4625
4626 sect_offset sect_off_prev;
4627 for (uint32_t i = 0; i <= map.cu_count; ++i)
4628 {
4629 sect_offset sect_off_next;
4630 if (i < map.cu_count)
4631 {
4632 sect_off_next
4633 = (sect_offset) (extract_unsigned_integer
4634 (map.cu_table_reordered + i * map.offset_size,
4635 map.offset_size,
4636 map.dwarf5_byte_order));
4637 }
4638 else
4639 sect_off_next = (sect_offset) section.size;
4640 if (i >= 1)
4641 {
4642 const ULONGEST length = sect_off_next - sect_off_prev;
4643 dwarf2_per_cu_data_up per_cu
4644 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4645 sect_off_prev, length);
4646 per_bfd->all_comp_units.push_back (std::move (per_cu));
4647 }
4648 sect_off_prev = sect_off_next;
4649 }
4650 }
4651
4652 /* Read the CU list from the mapped index, and use it to create all
4653 the CU objects for this dwarf2_per_objfile. */
4654
4655 static void
4656 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4657 const mapped_debug_names &map,
4658 const mapped_debug_names &dwz_map)
4659 {
4660 gdb_assert (per_bfd->all_comp_units.empty ());
4661 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4662
4663 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4664 false /* is_dwz */);
4665
4666 if (dwz_map.cu_count == 0)
4667 return;
4668
4669 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4670 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4671 true /* is_dwz */);
4672 }
4673
4674 /* Read .debug_names. If everything went ok, initialize the "quick"
4675 elements of all the CUs and return true. Otherwise, return false. */
4676
4677 static bool
4678 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4679 {
4680 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4681 mapped_debug_names dwz_map;
4682 struct objfile *objfile = per_objfile->objfile;
4683 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4684
4685 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4686 &per_bfd->debug_names, *map))
4687 return false;
4688
4689 /* Don't use the index if it's empty. */
4690 if (map->name_count == 0)
4691 return false;
4692
4693 /* If there is a .dwz file, read it so we can get its CU list as
4694 well. */
4695 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4696 if (dwz != NULL)
4697 {
4698 if (!read_debug_names_from_section (objfile,
4699 bfd_get_filename (dwz->dwz_bfd.get ()),
4700 &dwz->debug_names, dwz_map))
4701 {
4702 warning (_("could not read '.debug_names' section from %s; skipping"),
4703 bfd_get_filename (dwz->dwz_bfd.get ()));
4704 return false;
4705 }
4706 }
4707
4708 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4709
4710 if (map->tu_count != 0)
4711 {
4712 /* We can only handle a single .debug_types when we have an
4713 index. */
4714 if (per_bfd->types.size () != 1)
4715 return false;
4716
4717 dwarf2_section_info *section = &per_bfd->types[0];
4718
4719 create_signatured_type_table_from_debug_names
4720 (per_objfile, *map, section, &per_bfd->abbrev);
4721 }
4722
4723 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4724
4725 per_bfd->debug_names_table = std::move (map);
4726 per_bfd->quick_file_names_table =
4727 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4728
4729 return true;
4730 }
4731
4732 /* Type used to manage iterating over all CUs looking for a symbol for
4733 .debug_names. */
4734
4735 class dw2_debug_names_iterator
4736 {
4737 public:
4738 dw2_debug_names_iterator (const mapped_debug_names &map,
4739 block_search_flags block_index,
4740 domain_enum domain,
4741 const char *name, dwarf2_per_objfile *per_objfile)
4742 : m_map (map), m_block_index (block_index), m_domain (domain),
4743 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4744 m_per_objfile (per_objfile)
4745 {}
4746
4747 dw2_debug_names_iterator (const mapped_debug_names &map,
4748 search_domain search, uint32_t namei,
4749 dwarf2_per_objfile *per_objfile,
4750 domain_enum domain = UNDEF_DOMAIN)
4751 : m_map (map),
4752 m_domain (domain),
4753 m_search (search),
4754 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4755 m_per_objfile (per_objfile)
4756 {}
4757
4758 dw2_debug_names_iterator (const mapped_debug_names &map,
4759 block_search_flags block_index, domain_enum domain,
4760 uint32_t namei, dwarf2_per_objfile *per_objfile)
4761 : m_map (map), m_block_index (block_index), m_domain (domain),
4762 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4763 m_per_objfile (per_objfile)
4764 {}
4765
4766 /* Return the next matching CU or NULL if there are no more. */
4767 dwarf2_per_cu_data *next ();
4768
4769 private:
4770 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4771 const char *name,
4772 dwarf2_per_objfile *per_objfile);
4773 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4774 uint32_t namei,
4775 dwarf2_per_objfile *per_objfile);
4776
4777 /* The internalized form of .debug_names. */
4778 const mapped_debug_names &m_map;
4779
4780 /* Restrict the search to these blocks. */
4781 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4782 | SEARCH_STATIC_BLOCK);
4783
4784 /* The kind of symbol we're looking for. */
4785 const domain_enum m_domain = UNDEF_DOMAIN;
4786 const search_domain m_search = ALL_DOMAIN;
4787
4788 /* The list of CUs from the index entry of the symbol, or NULL if
4789 not found. */
4790 const gdb_byte *m_addr;
4791
4792 dwarf2_per_objfile *m_per_objfile;
4793 };
4794
4795 const char *
4796 mapped_debug_names::namei_to_name
4797 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4798 {
4799 const ULONGEST namei_string_offs
4800 = extract_unsigned_integer ((name_table_string_offs_reordered
4801 + namei * offset_size),
4802 offset_size,
4803 dwarf5_byte_order);
4804 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4805 }
4806
4807 /* Find a slot in .debug_names for the object named NAME. If NAME is
4808 found, return pointer to its pool data. If NAME cannot be found,
4809 return NULL. */
4810
4811 const gdb_byte *
4812 dw2_debug_names_iterator::find_vec_in_debug_names
4813 (const mapped_debug_names &map, const char *name,
4814 dwarf2_per_objfile *per_objfile)
4815 {
4816 int (*cmp) (const char *, const char *);
4817
4818 gdb::unique_xmalloc_ptr<char> without_params;
4819 if (current_language->la_language == language_cplus
4820 || current_language->la_language == language_fortran
4821 || current_language->la_language == language_d)
4822 {
4823 /* NAME is already canonical. Drop any qualifiers as
4824 .debug_names does not contain any. */
4825
4826 if (strchr (name, '(') != NULL)
4827 {
4828 without_params = cp_remove_params (name);
4829 if (without_params != NULL)
4830 name = without_params.get ();
4831 }
4832 }
4833
4834 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4835
4836 const uint32_t full_hash = dwarf5_djb_hash (name);
4837 uint32_t namei
4838 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4839 (map.bucket_table_reordered
4840 + (full_hash % map.bucket_count)), 4,
4841 map.dwarf5_byte_order);
4842 if (namei == 0)
4843 return NULL;
4844 --namei;
4845 if (namei >= map.name_count)
4846 {
4847 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4848 "[in module %s]"),
4849 namei, map.name_count,
4850 objfile_name (per_objfile->objfile));
4851 return NULL;
4852 }
4853
4854 for (;;)
4855 {
4856 const uint32_t namei_full_hash
4857 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4858 (map.hash_table_reordered + namei), 4,
4859 map.dwarf5_byte_order);
4860 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4861 return NULL;
4862
4863 if (full_hash == namei_full_hash)
4864 {
4865 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4866
4867 #if 0 /* An expensive sanity check. */
4868 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4869 {
4870 complaint (_("Wrong .debug_names hash for string at index %u "
4871 "[in module %s]"),
4872 namei, objfile_name (dwarf2_per_objfile->objfile));
4873 return NULL;
4874 }
4875 #endif
4876
4877 if (cmp (namei_string, name) == 0)
4878 {
4879 const ULONGEST namei_entry_offs
4880 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4881 + namei * map.offset_size),
4882 map.offset_size, map.dwarf5_byte_order);
4883 return map.entry_pool + namei_entry_offs;
4884 }
4885 }
4886
4887 ++namei;
4888 if (namei >= map.name_count)
4889 return NULL;
4890 }
4891 }
4892
4893 const gdb_byte *
4894 dw2_debug_names_iterator::find_vec_in_debug_names
4895 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4896 {
4897 if (namei >= map.name_count)
4898 {
4899 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4900 "[in module %s]"),
4901 namei, map.name_count,
4902 objfile_name (per_objfile->objfile));
4903 return NULL;
4904 }
4905
4906 const ULONGEST namei_entry_offs
4907 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4908 + namei * map.offset_size),
4909 map.offset_size, map.dwarf5_byte_order);
4910 return map.entry_pool + namei_entry_offs;
4911 }
4912
4913 /* See dw2_debug_names_iterator. */
4914
4915 dwarf2_per_cu_data *
4916 dw2_debug_names_iterator::next ()
4917 {
4918 if (m_addr == NULL)
4919 return NULL;
4920
4921 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4922 struct objfile *objfile = m_per_objfile->objfile;
4923 bfd *const abfd = objfile->obfd;
4924
4925 again:
4926
4927 unsigned int bytes_read;
4928 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4929 m_addr += bytes_read;
4930 if (abbrev == 0)
4931 return NULL;
4932
4933 const auto indexval_it = m_map.abbrev_map.find (abbrev);
4934 if (indexval_it == m_map.abbrev_map.cend ())
4935 {
4936 complaint (_("Wrong .debug_names undefined abbrev code %s "
4937 "[in module %s]"),
4938 pulongest (abbrev), objfile_name (objfile));
4939 return NULL;
4940 }
4941 const mapped_debug_names::index_val &indexval = indexval_it->second;
4942 enum class symbol_linkage {
4943 unknown,
4944 static_,
4945 extern_,
4946 } symbol_linkage_ = symbol_linkage::unknown;
4947 dwarf2_per_cu_data *per_cu = NULL;
4948 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
4949 {
4950 ULONGEST ull;
4951 switch (attr.form)
4952 {
4953 case DW_FORM_implicit_const:
4954 ull = attr.implicit_const;
4955 break;
4956 case DW_FORM_flag_present:
4957 ull = 1;
4958 break;
4959 case DW_FORM_udata:
4960 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4961 m_addr += bytes_read;
4962 break;
4963 case DW_FORM_ref4:
4964 ull = read_4_bytes (abfd, m_addr);
4965 m_addr += 4;
4966 break;
4967 case DW_FORM_ref8:
4968 ull = read_8_bytes (abfd, m_addr);
4969 m_addr += 8;
4970 break;
4971 case DW_FORM_ref_sig8:
4972 ull = read_8_bytes (abfd, m_addr);
4973 m_addr += 8;
4974 break;
4975 default:
4976 complaint (_("Unsupported .debug_names form %s [in module %s]"),
4977 dwarf_form_name (attr.form),
4978 objfile_name (objfile));
4979 return NULL;
4980 }
4981 switch (attr.dw_idx)
4982 {
4983 case DW_IDX_compile_unit:
4984 /* Don't crash on bad data. */
4985 if (ull >= per_bfd->all_comp_units.size ())
4986 {
4987 complaint (_(".debug_names entry has bad CU index %s"
4988 " [in module %s]"),
4989 pulongest (ull),
4990 objfile_name (objfile));
4991 continue;
4992 }
4993 per_cu = per_bfd->get_cu (ull);
4994 break;
4995 case DW_IDX_type_unit:
4996 /* Don't crash on bad data. */
4997 if (ull >= per_bfd->tu_stats.nr_tus)
4998 {
4999 complaint (_(".debug_names entry has bad TU index %s"
5000 " [in module %s]"),
5001 pulongest (ull),
5002 objfile_name (objfile));
5003 continue;
5004 }
5005 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5006 break;
5007 case DW_IDX_die_offset:
5008 /* In a per-CU index (as opposed to a per-module index), index
5009 entries without CU attribute implicitly refer to the single CU. */
5010 if (per_cu == NULL)
5011 per_cu = per_bfd->get_cu (0);
5012 break;
5013 case DW_IDX_GNU_internal:
5014 if (!m_map.augmentation_is_gdb)
5015 break;
5016 symbol_linkage_ = symbol_linkage::static_;
5017 break;
5018 case DW_IDX_GNU_external:
5019 if (!m_map.augmentation_is_gdb)
5020 break;
5021 symbol_linkage_ = symbol_linkage::extern_;
5022 break;
5023 }
5024 }
5025
5026 /* Skip if already read in. */
5027 if (m_per_objfile->symtab_set_p (per_cu))
5028 goto again;
5029
5030 /* Check static vs global. */
5031 if (symbol_linkage_ != symbol_linkage::unknown)
5032 {
5033 if (symbol_linkage_ == symbol_linkage::static_)
5034 {
5035 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5036 goto again;
5037 }
5038 else
5039 {
5040 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5041 goto again;
5042 }
5043 }
5044
5045 /* Match dw2_symtab_iter_next, symbol_kind
5046 and debug_names::psymbol_tag. */
5047 switch (m_domain)
5048 {
5049 case VAR_DOMAIN:
5050 switch (indexval.dwarf_tag)
5051 {
5052 case DW_TAG_variable:
5053 case DW_TAG_subprogram:
5054 /* Some types are also in VAR_DOMAIN. */
5055 case DW_TAG_typedef:
5056 case DW_TAG_structure_type:
5057 break;
5058 default:
5059 goto again;
5060 }
5061 break;
5062 case STRUCT_DOMAIN:
5063 switch (indexval.dwarf_tag)
5064 {
5065 case DW_TAG_typedef:
5066 case DW_TAG_structure_type:
5067 break;
5068 default:
5069 goto again;
5070 }
5071 break;
5072 case LABEL_DOMAIN:
5073 switch (indexval.dwarf_tag)
5074 {
5075 case 0:
5076 case DW_TAG_variable:
5077 break;
5078 default:
5079 goto again;
5080 }
5081 break;
5082 case MODULE_DOMAIN:
5083 switch (indexval.dwarf_tag)
5084 {
5085 case DW_TAG_module:
5086 break;
5087 default:
5088 goto again;
5089 }
5090 break;
5091 default:
5092 break;
5093 }
5094
5095 /* Match dw2_expand_symtabs_matching, symbol_kind and
5096 debug_names::psymbol_tag. */
5097 switch (m_search)
5098 {
5099 case VARIABLES_DOMAIN:
5100 switch (indexval.dwarf_tag)
5101 {
5102 case DW_TAG_variable:
5103 break;
5104 default:
5105 goto again;
5106 }
5107 break;
5108 case FUNCTIONS_DOMAIN:
5109 switch (indexval.dwarf_tag)
5110 {
5111 case DW_TAG_subprogram:
5112 break;
5113 default:
5114 goto again;
5115 }
5116 break;
5117 case TYPES_DOMAIN:
5118 switch (indexval.dwarf_tag)
5119 {
5120 case DW_TAG_typedef:
5121 case DW_TAG_structure_type:
5122 break;
5123 default:
5124 goto again;
5125 }
5126 break;
5127 case MODULES_DOMAIN:
5128 switch (indexval.dwarf_tag)
5129 {
5130 case DW_TAG_module:
5131 break;
5132 default:
5133 goto again;
5134 }
5135 default:
5136 break;
5137 }
5138
5139 return per_cu;
5140 }
5141
5142 /* This dumps minimal information about .debug_names. It is called
5143 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5144 uses this to verify that .debug_names has been loaded. */
5145
5146 void
5147 dwarf2_debug_names_index::dump (struct objfile *objfile)
5148 {
5149 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5150
5151 gdb_printf (".debug_names:");
5152 if (per_objfile->per_bfd->debug_names_table)
5153 gdb_printf (" exists\n");
5154 else
5155 gdb_printf (" faked for \"readnow\"\n");
5156 gdb_printf ("\n");
5157 }
5158
5159 void
5160 dwarf2_debug_names_index::expand_matching_symbols
5161 (struct objfile *objfile,
5162 const lookup_name_info &name, domain_enum domain,
5163 int global,
5164 symbol_compare_ftype *ordered_compare)
5165 {
5166 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5167
5168 /* debug_names_table is NULL if OBJF_READNOW. */
5169 if (!per_objfile->per_bfd->debug_names_table)
5170 return;
5171
5172 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5173 const block_search_flags block_flags
5174 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5175
5176 const char *match_name = name.ada ().lookup_name ().c_str ();
5177 auto matcher = [&] (const char *symname)
5178 {
5179 if (ordered_compare == nullptr)
5180 return true;
5181 return ordered_compare (symname, match_name) == 0;
5182 };
5183
5184 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5185 [&] (offset_type namei)
5186 {
5187 /* The name was matched, now expand corresponding CUs that were
5188 marked. */
5189 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5190 per_objfile);
5191
5192 struct dwarf2_per_cu_data *per_cu;
5193 while ((per_cu = iter.next ()) != NULL)
5194 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5195 nullptr);
5196 return true;
5197 }, per_objfile);
5198 }
5199
5200 bool
5201 dwarf2_debug_names_index::expand_symtabs_matching
5202 (struct objfile *objfile,
5203 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5204 const lookup_name_info *lookup_name,
5205 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5206 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5207 block_search_flags search_flags,
5208 domain_enum domain,
5209 enum search_domain kind)
5210 {
5211 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5212
5213 /* debug_names_table is NULL if OBJF_READNOW. */
5214 if (!per_objfile->per_bfd->debug_names_table)
5215 return true;
5216
5217 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5218
5219 /* This invariant is documented in quick-functions.h. */
5220 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5221 if (lookup_name == nullptr)
5222 {
5223 for (dwarf2_per_cu_data *per_cu
5224 : all_comp_units_range (per_objfile->per_bfd))
5225 {
5226 QUIT;
5227
5228 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5229 file_matcher,
5230 expansion_notify))
5231 return false;
5232 }
5233 return true;
5234 }
5235
5236 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5237
5238 bool result
5239 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5240 symbol_matcher,
5241 [&] (offset_type namei)
5242 {
5243 /* The name was matched, now expand corresponding CUs that were
5244 marked. */
5245 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5246
5247 struct dwarf2_per_cu_data *per_cu;
5248 while ((per_cu = iter.next ()) != NULL)
5249 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5250 file_matcher,
5251 expansion_notify))
5252 return false;
5253 return true;
5254 }, per_objfile);
5255
5256 return result;
5257 }
5258
5259 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5260 to either a dwarf2_per_bfd or dwz_file object. */
5261
5262 template <typename T>
5263 static gdb::array_view<const gdb_byte>
5264 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5265 {
5266 dwarf2_section_info *section = &section_owner->gdb_index;
5267
5268 if (section->empty ())
5269 return {};
5270
5271 /* Older elfutils strip versions could keep the section in the main
5272 executable while splitting it for the separate debug info file. */
5273 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5274 return {};
5275
5276 section->read (obj);
5277
5278 /* dwarf2_section_info::size is a bfd_size_type, while
5279 gdb::array_view works with size_t. On 32-bit hosts, with
5280 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5281 is 32-bit. So we need an explicit narrowing conversion here.
5282 This is fine, because it's impossible to allocate or mmap an
5283 array/buffer larger than what size_t can represent. */
5284 return gdb::make_array_view (section->buffer, section->size);
5285 }
5286
5287 /* Lookup the index cache for the contents of the index associated to
5288 DWARF2_OBJ. */
5289
5290 static gdb::array_view<const gdb_byte>
5291 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5292 {
5293 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5294 if (build_id == nullptr)
5295 return {};
5296
5297 return global_index_cache.lookup_gdb_index (build_id,
5298 &dwarf2_per_bfd->index_cache_res);
5299 }
5300
5301 /* Same as the above, but for DWZ. */
5302
5303 static gdb::array_view<const gdb_byte>
5304 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5305 {
5306 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5307 if (build_id == nullptr)
5308 return {};
5309
5310 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5311 }
5312
5313 static quick_symbol_functions_up make_cooked_index_funcs ();
5314
5315 /* See dwarf2/public.h. */
5316
5317 void
5318 dwarf2_initialize_objfile (struct objfile *objfile)
5319 {
5320 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5321 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5322
5323 dwarf_read_debug_printf ("called");
5324
5325 /* If we're about to read full symbols, don't bother with the
5326 indices. In this case we also don't care if some other debug
5327 format is making psymtabs, because they are all about to be
5328 expanded anyway. */
5329 if ((objfile->flags & OBJF_READNOW))
5330 {
5331 dwarf_read_debug_printf ("readnow requested");
5332
5333 create_all_comp_units (per_objfile);
5334 per_bfd->quick_file_names_table
5335 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5336
5337 /* Arrange for gdb to see the "quick" functions. However, these
5338 functions will be no-ops because we will have expanded all
5339 symtabs. */
5340 objfile->qf.push_front (make_dwarf_gdb_index ());
5341 return;
5342 }
5343
5344 /* Was a debug names index already read when we processed an objfile sharing
5345 PER_BFD? */
5346 if (per_bfd->debug_names_table != nullptr)
5347 {
5348 dwarf_read_debug_printf ("re-using shared debug names table");
5349 objfile->qf.push_front (make_dwarf_debug_names ());
5350 return;
5351 }
5352
5353 /* Was a GDB index already read when we processed an objfile sharing
5354 PER_BFD? */
5355 if (per_bfd->index_table != nullptr)
5356 {
5357 dwarf_read_debug_printf ("re-using shared index table");
5358 objfile->qf.push_front (make_dwarf_gdb_index ());
5359 return;
5360 }
5361
5362 if (per_bfd->cooked_index_table != nullptr)
5363 {
5364 dwarf_read_debug_printf ("re-using cooked index table");
5365 objfile->qf.push_front (make_cooked_index_funcs ());
5366 return;
5367 }
5368
5369 if (dwarf2_read_debug_names (per_objfile))
5370 {
5371 dwarf_read_debug_printf ("found debug names");
5372 objfile->qf.push_front (make_dwarf_debug_names ());
5373 return;
5374 }
5375
5376 if (dwarf2_read_gdb_index (per_objfile,
5377 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5378 get_gdb_index_contents_from_section<dwz_file>))
5379 {
5380 dwarf_read_debug_printf ("found gdb index from file");
5381 objfile->qf.push_front (make_dwarf_gdb_index ());
5382 return;
5383 }
5384
5385 /* ... otherwise, try to find the index in the index cache. */
5386 if (dwarf2_read_gdb_index (per_objfile,
5387 get_gdb_index_contents_from_cache,
5388 get_gdb_index_contents_from_cache_dwz))
5389 {
5390 dwarf_read_debug_printf ("found gdb index from cache");
5391 global_index_cache.hit ();
5392 objfile->qf.push_front (make_dwarf_gdb_index ());
5393 return;
5394 }
5395
5396 global_index_cache.miss ();
5397 objfile->qf.push_front (make_cooked_index_funcs ());
5398 }
5399
5400 \f
5401
5402 /* Build a partial symbol table. */
5403
5404 void
5405 dwarf2_build_psymtabs (struct objfile *objfile, bool already_attached)
5406 {
5407 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5408
5409 if (already_attached)
5410 {
5411 if (per_objfile->per_bfd->cooked_index_table != nullptr)
5412 return;
5413 }
5414 else
5415 objfile->qf.push_front (make_cooked_index_funcs ());
5416
5417 try
5418 {
5419 dwarf2_build_psymtabs_hard (per_objfile);
5420
5421 /* (maybe) store an index in the cache. */
5422 global_index_cache.store (per_objfile);
5423 }
5424 catch (const gdb_exception_error &except)
5425 {
5426 exception_print (gdb_stderr, except);
5427 }
5428 }
5429
5430 /* Find the base address of the compilation unit for range lists and
5431 location lists. It will normally be specified by DW_AT_low_pc.
5432 In DWARF-3 draft 4, the base address could be overridden by
5433 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5434 compilation units with discontinuous ranges. */
5435
5436 static void
5437 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5438 {
5439 struct attribute *attr;
5440
5441 cu->base_address.reset ();
5442
5443 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5444 if (attr != nullptr)
5445 cu->base_address = attr->as_address ();
5446 else
5447 {
5448 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5449 if (attr != nullptr)
5450 cu->base_address = attr->as_address ();
5451 }
5452 }
5453
5454 /* Helper function that returns the proper abbrev section for
5455 THIS_CU. */
5456
5457 static struct dwarf2_section_info *
5458 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5459 {
5460 struct dwarf2_section_info *abbrev;
5461 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5462
5463 if (this_cu->is_dwz)
5464 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5465 else
5466 abbrev = &per_bfd->abbrev;
5467
5468 return abbrev;
5469 }
5470
5471 /* Fetch the abbreviation table offset from a comp or type unit header. */
5472
5473 static sect_offset
5474 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5475 struct dwarf2_section_info *section,
5476 sect_offset sect_off)
5477 {
5478 bfd *abfd = section->get_bfd_owner ();
5479 const gdb_byte *info_ptr;
5480 unsigned int initial_length_size, offset_size;
5481 uint16_t version;
5482
5483 section->read (per_objfile->objfile);
5484 info_ptr = section->buffer + to_underlying (sect_off);
5485 read_initial_length (abfd, info_ptr, &initial_length_size);
5486 offset_size = initial_length_size == 4 ? 4 : 8;
5487 info_ptr += initial_length_size;
5488
5489 version = read_2_bytes (abfd, info_ptr);
5490 info_ptr += 2;
5491 if (version >= 5)
5492 {
5493 /* Skip unit type and address size. */
5494 info_ptr += 2;
5495 }
5496
5497 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5498 }
5499
5500 static hashval_t
5501 hash_signatured_type (const void *item)
5502 {
5503 const struct signatured_type *sig_type
5504 = (const struct signatured_type *) item;
5505
5506 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5507 return sig_type->signature;
5508 }
5509
5510 static int
5511 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5512 {
5513 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5514 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5515
5516 return lhs->signature == rhs->signature;
5517 }
5518
5519 /* Allocate a hash table for signatured types. */
5520
5521 static htab_up
5522 allocate_signatured_type_table ()
5523 {
5524 return htab_up (htab_create_alloc (41,
5525 hash_signatured_type,
5526 eq_signatured_type,
5527 NULL, xcalloc, xfree));
5528 }
5529
5530 /* A helper for create_debug_types_hash_table. Read types from SECTION
5531 and fill them into TYPES_HTAB. It will process only type units,
5532 therefore DW_UT_type. */
5533
5534 static void
5535 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5536 struct dwo_file *dwo_file,
5537 dwarf2_section_info *section, htab_up &types_htab,
5538 rcuh_kind section_kind)
5539 {
5540 struct objfile *objfile = per_objfile->objfile;
5541 struct dwarf2_section_info *abbrev_section;
5542 bfd *abfd;
5543 const gdb_byte *info_ptr, *end_ptr;
5544
5545 abbrev_section = &dwo_file->sections.abbrev;
5546
5547 dwarf_read_debug_printf ("Reading %s for %s",
5548 section->get_name (),
5549 abbrev_section->get_file_name ());
5550
5551 section->read (objfile);
5552 info_ptr = section->buffer;
5553
5554 if (info_ptr == NULL)
5555 return;
5556
5557 /* We can't set abfd until now because the section may be empty or
5558 not present, in which case the bfd is unknown. */
5559 abfd = section->get_bfd_owner ();
5560
5561 /* We don't use cutu_reader here because we don't need to read
5562 any dies: the signature is in the header. */
5563
5564 end_ptr = info_ptr + section->size;
5565 while (info_ptr < end_ptr)
5566 {
5567 signatured_type_up sig_type;
5568 struct dwo_unit *dwo_tu;
5569 void **slot;
5570 const gdb_byte *ptr = info_ptr;
5571 struct comp_unit_head header;
5572 unsigned int length;
5573
5574 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5575
5576 /* Initialize it due to a false compiler warning. */
5577 header.signature = -1;
5578 header.type_cu_offset_in_tu = (cu_offset) -1;
5579
5580 /* We need to read the type's signature in order to build the hash
5581 table, but we don't need anything else just yet. */
5582
5583 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5584 abbrev_section, ptr, section_kind);
5585
5586 length = header.get_length ();
5587
5588 /* Skip dummy type units. */
5589 if (ptr >= info_ptr + length
5590 || peek_abbrev_code (abfd, ptr) == 0
5591 || (header.unit_type != DW_UT_type
5592 && header.unit_type != DW_UT_split_type))
5593 {
5594 info_ptr += length;
5595 continue;
5596 }
5597
5598 if (types_htab == NULL)
5599 types_htab = allocate_dwo_unit_table ();
5600
5601 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5602 dwo_tu->dwo_file = dwo_file;
5603 dwo_tu->signature = header.signature;
5604 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5605 dwo_tu->section = section;
5606 dwo_tu->sect_off = sect_off;
5607 dwo_tu->length = length;
5608
5609 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5610 gdb_assert (slot != NULL);
5611 if (*slot != NULL)
5612 complaint (_("debug type entry at offset %s is duplicate to"
5613 " the entry at offset %s, signature %s"),
5614 sect_offset_str (sect_off),
5615 sect_offset_str (dwo_tu->sect_off),
5616 hex_string (header.signature));
5617 *slot = dwo_tu;
5618
5619 dwarf_read_debug_printf_v (" offset %s, signature %s",
5620 sect_offset_str (sect_off),
5621 hex_string (header.signature));
5622
5623 info_ptr += length;
5624 }
5625 }
5626
5627 /* Create the hash table of all entries in the .debug_types
5628 (or .debug_types.dwo) section(s).
5629 DWO_FILE is a pointer to the DWO file object.
5630
5631 The result is a pointer to the hash table or NULL if there are no types.
5632
5633 Note: This function processes DWO files only, not DWP files. */
5634
5635 static void
5636 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5637 struct dwo_file *dwo_file,
5638 gdb::array_view<dwarf2_section_info> type_sections,
5639 htab_up &types_htab)
5640 {
5641 for (dwarf2_section_info &section : type_sections)
5642 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5643 rcuh_kind::TYPE);
5644 }
5645
5646 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5647 If SLOT is non-NULL, it is the entry to use in the hash table.
5648 Otherwise we find one. */
5649
5650 static struct signatured_type *
5651 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5652 {
5653 if (per_objfile->per_bfd->all_comp_units.size ()
5654 == per_objfile->per_bfd->all_comp_units.capacity ())
5655 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5656
5657 signatured_type_up sig_type_holder
5658 = per_objfile->per_bfd->allocate_signatured_type (sig);
5659 signatured_type *sig_type = sig_type_holder.get ();
5660
5661 per_objfile->per_bfd->all_comp_units.emplace_back
5662 (sig_type_holder.release ());
5663
5664 if (slot == NULL)
5665 {
5666 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5667 sig_type, INSERT);
5668 }
5669 gdb_assert (*slot == NULL);
5670 *slot = sig_type;
5671 /* The rest of sig_type must be filled in by the caller. */
5672 return sig_type;
5673 }
5674
5675 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5676 Fill in SIG_ENTRY with DWO_ENTRY. */
5677
5678 static void
5679 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5680 struct signatured_type *sig_entry,
5681 struct dwo_unit *dwo_entry)
5682 {
5683 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5684
5685 /* Make sure we're not clobbering something we don't expect to. */
5686 gdb_assert (! sig_entry->queued);
5687 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5688 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5689 gdb_assert (sig_entry->signature == dwo_entry->signature);
5690 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
5691 || (to_underlying (sig_entry->type_offset_in_section)
5692 == to_underlying (dwo_entry->type_offset_in_tu)));
5693 gdb_assert (sig_entry->type_unit_group == NULL);
5694 gdb_assert (sig_entry->dwo_unit == NULL
5695 || sig_entry->dwo_unit == dwo_entry);
5696
5697 sig_entry->section = dwo_entry->section;
5698 sig_entry->sect_off = dwo_entry->sect_off;
5699 sig_entry->length = dwo_entry->length;
5700 sig_entry->reading_dwo_directly = 1;
5701 sig_entry->per_bfd = per_bfd;
5702 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5703 sig_entry->dwo_unit = dwo_entry;
5704 }
5705
5706 /* Subroutine of lookup_signatured_type.
5707 If we haven't read the TU yet, create the signatured_type data structure
5708 for a TU to be read in directly from a DWO file, bypassing the stub.
5709 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5710 using .gdb_index, then when reading a CU we want to stay in the DWO file
5711 containing that CU. Otherwise we could end up reading several other DWO
5712 files (due to comdat folding) to process the transitive closure of all the
5713 mentioned TUs, and that can be slow. The current DWO file will have every
5714 type signature that it needs.
5715 We only do this for .gdb_index because in the psymtab case we already have
5716 to read all the DWOs to build the type unit groups. */
5717
5718 static struct signatured_type *
5719 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5720 {
5721 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5722 struct dwo_file *dwo_file;
5723 struct dwo_unit find_dwo_entry, *dwo_entry;
5724 void **slot;
5725
5726 gdb_assert (cu->dwo_unit);
5727
5728 /* If TU skeletons have been removed then we may not have read in any
5729 TUs yet. */
5730 if (per_objfile->per_bfd->signatured_types == NULL)
5731 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5732
5733 /* We only ever need to read in one copy of a signatured type.
5734 Use the global signatured_types array to do our own comdat-folding
5735 of types. If this is the first time we're reading this TU, and
5736 the TU has an entry in .gdb_index, replace the recorded data from
5737 .gdb_index with this TU. */
5738
5739 signatured_type find_sig_entry (sig);
5740 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5741 &find_sig_entry, INSERT);
5742 signatured_type *sig_entry = (struct signatured_type *) *slot;
5743
5744 /* We can get here with the TU already read, *or* in the process of being
5745 read. Don't reassign the global entry to point to this DWO if that's
5746 the case. Also note that if the TU is already being read, it may not
5747 have come from a DWO, the program may be a mix of Fission-compiled
5748 code and non-Fission-compiled code. */
5749
5750 /* Have we already tried to read this TU?
5751 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5752 needn't exist in the global table yet). */
5753 if (sig_entry != NULL && sig_entry->tu_read)
5754 return sig_entry;
5755
5756 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5757 dwo_unit of the TU itself. */
5758 dwo_file = cu->dwo_unit->dwo_file;
5759
5760 /* Ok, this is the first time we're reading this TU. */
5761 if (dwo_file->tus == NULL)
5762 return NULL;
5763 find_dwo_entry.signature = sig;
5764 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
5765 &find_dwo_entry);
5766 if (dwo_entry == NULL)
5767 return NULL;
5768
5769 /* If the global table doesn't have an entry for this TU, add one. */
5770 if (sig_entry == NULL)
5771 sig_entry = add_type_unit (per_objfile, sig, slot);
5772
5773 if (sig_entry->dwo_unit == nullptr)
5774 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5775 sig_entry->tu_read = 1;
5776 return sig_entry;
5777 }
5778
5779 /* Subroutine of lookup_signatured_type.
5780 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5781 then try the DWP file. If the TU stub (skeleton) has been removed then
5782 it won't be in .gdb_index. */
5783
5784 static struct signatured_type *
5785 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5786 {
5787 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5788 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
5789 struct dwo_unit *dwo_entry;
5790 void **slot;
5791
5792 gdb_assert (cu->dwo_unit);
5793 gdb_assert (dwp_file != NULL);
5794
5795 /* If TU skeletons have been removed then we may not have read in any
5796 TUs yet. */
5797 if (per_objfile->per_bfd->signatured_types == NULL)
5798 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5799
5800 signatured_type find_sig_entry (sig);
5801 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5802 &find_sig_entry, INSERT);
5803 signatured_type *sig_entry = (struct signatured_type *) *slot;
5804
5805 /* Have we already tried to read this TU?
5806 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5807 needn't exist in the global table yet). */
5808 if (sig_entry != NULL)
5809 return sig_entry;
5810
5811 if (dwp_file->tus == NULL)
5812 return NULL;
5813 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
5814 1 /* is_debug_types */);
5815 if (dwo_entry == NULL)
5816 return NULL;
5817
5818 sig_entry = add_type_unit (per_objfile, sig, slot);
5819 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5820
5821 return sig_entry;
5822 }
5823
5824 /* Lookup a signature based type for DW_FORM_ref_sig8.
5825 Returns NULL if signature SIG is not present in the table.
5826 It is up to the caller to complain about this. */
5827
5828 static struct signatured_type *
5829 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5830 {
5831 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5832
5833 if (cu->dwo_unit)
5834 {
5835 /* We're in a DWO/DWP file, and we're using .gdb_index.
5836 These cases require special processing. */
5837 if (get_dwp_file (per_objfile) == NULL)
5838 return lookup_dwo_signatured_type (cu, sig);
5839 else
5840 return lookup_dwp_signatured_type (cu, sig);
5841 }
5842 else
5843 {
5844 if (per_objfile->per_bfd->signatured_types == NULL)
5845 return NULL;
5846 signatured_type find_entry (sig);
5847 return ((struct signatured_type *)
5848 htab_find (per_objfile->per_bfd->signatured_types.get (),
5849 &find_entry));
5850 }
5851 }
5852
5853 /* Low level DIE reading support. */
5854
5855 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5856
5857 static void
5858 init_cu_die_reader (struct die_reader_specs *reader,
5859 struct dwarf2_cu *cu,
5860 struct dwarf2_section_info *section,
5861 struct dwo_file *dwo_file,
5862 struct abbrev_table *abbrev_table)
5863 {
5864 gdb_assert (section->readin && section->buffer != NULL);
5865 reader->abfd = section->get_bfd_owner ();
5866 reader->cu = cu;
5867 reader->dwo_file = dwo_file;
5868 reader->die_section = section;
5869 reader->buffer = section->buffer;
5870 reader->buffer_end = section->buffer + section->size;
5871 reader->abbrev_table = abbrev_table;
5872 }
5873
5874 /* Subroutine of cutu_reader to simplify it.
5875 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5876 There's just a lot of work to do, and cutu_reader is big enough
5877 already.
5878
5879 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5880 from it to the DIE in the DWO. If NULL we are skipping the stub.
5881 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5882 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5883 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5884 STUB_COMP_DIR may be non-NULL.
5885 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5886 are filled in with the info of the DIE from the DWO file.
5887 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5888 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5889 kept around for at least as long as *RESULT_READER.
5890
5891 The result is non-zero if a valid (non-dummy) DIE was found. */
5892
5893 static int
5894 read_cutu_die_from_dwo (dwarf2_cu *cu,
5895 struct dwo_unit *dwo_unit,
5896 struct die_info *stub_comp_unit_die,
5897 const char *stub_comp_dir,
5898 struct die_reader_specs *result_reader,
5899 const gdb_byte **result_info_ptr,
5900 struct die_info **result_comp_unit_die,
5901 abbrev_table_up *result_dwo_abbrev_table)
5902 {
5903 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5904 dwarf2_per_cu_data *per_cu = cu->per_cu;
5905 struct objfile *objfile = per_objfile->objfile;
5906 bfd *abfd;
5907 const gdb_byte *begin_info_ptr, *info_ptr;
5908 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5909 int i,num_extra_attrs;
5910 struct dwarf2_section_info *dwo_abbrev_section;
5911 struct die_info *comp_unit_die;
5912
5913 /* At most one of these may be provided. */
5914 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5915
5916 /* These attributes aren't processed until later:
5917 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5918 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5919 referenced later. However, these attributes are found in the stub
5920 which we won't have later. In order to not impose this complication
5921 on the rest of the code, we read them here and copy them to the
5922 DWO CU/TU die. */
5923
5924 stmt_list = NULL;
5925 low_pc = NULL;
5926 high_pc = NULL;
5927 ranges = NULL;
5928 comp_dir = NULL;
5929
5930 if (stub_comp_unit_die != NULL)
5931 {
5932 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5933 DWO file. */
5934 if (!per_cu->is_debug_types)
5935 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5936 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5937 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5938 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5939 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5940
5941 cu->addr_base = stub_comp_unit_die->addr_base ();
5942
5943 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5944 We need the value before we can process DW_AT_ranges values from the
5945 DWO. */
5946 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
5947
5948 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5949 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5950 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5951 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5952 section. */
5953 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
5954 }
5955 else if (stub_comp_dir != NULL)
5956 {
5957 /* Reconstruct the comp_dir attribute to simplify the code below. */
5958 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
5959 comp_dir->name = DW_AT_comp_dir;
5960 comp_dir->form = DW_FORM_string;
5961 comp_dir->set_string_noncanonical (stub_comp_dir);
5962 }
5963
5964 /* Set up for reading the DWO CU/TU. */
5965 cu->dwo_unit = dwo_unit;
5966 dwarf2_section_info *section = dwo_unit->section;
5967 section->read (objfile);
5968 abfd = section->get_bfd_owner ();
5969 begin_info_ptr = info_ptr = (section->buffer
5970 + to_underlying (dwo_unit->sect_off));
5971 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5972
5973 if (per_cu->is_debug_types)
5974 {
5975 signatured_type *sig_type = (struct signatured_type *) per_cu;
5976
5977 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5978 section, dwo_abbrev_section,
5979 info_ptr, rcuh_kind::TYPE);
5980 /* This is not an assert because it can be caused by bad debug info. */
5981 if (sig_type->signature != cu->header.signature)
5982 {
5983 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5984 " TU at offset %s [in module %s]"),
5985 hex_string (sig_type->signature),
5986 hex_string (cu->header.signature),
5987 sect_offset_str (dwo_unit->sect_off),
5988 bfd_get_filename (abfd));
5989 }
5990 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
5991 /* For DWOs coming from DWP files, we don't know the CU length
5992 nor the type's offset in the TU until now. */
5993 dwo_unit->length = cu->header.get_length ();
5994 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
5995
5996 /* Establish the type offset that can be used to lookup the type.
5997 For DWO files, we don't know it until now. */
5998 sig_type->type_offset_in_section
5999 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6000 }
6001 else
6002 {
6003 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6004 section, dwo_abbrev_section,
6005 info_ptr, rcuh_kind::COMPILE);
6006 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6007 /* For DWOs coming from DWP files, we don't know the CU length
6008 until now. */
6009 dwo_unit->length = cu->header.get_length ();
6010 }
6011
6012 dwo_abbrev_section->read (objfile);
6013 *result_dwo_abbrev_table
6014 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6015 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6016 result_dwo_abbrev_table->get ());
6017
6018 /* Read in the die, but leave space to copy over the attributes
6019 from the stub. This has the benefit of simplifying the rest of
6020 the code - all the work to maintain the illusion of a single
6021 DW_TAG_{compile,type}_unit DIE is done here. */
6022 num_extra_attrs = ((stmt_list != NULL)
6023 + (low_pc != NULL)
6024 + (high_pc != NULL)
6025 + (ranges != NULL)
6026 + (comp_dir != NULL));
6027 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6028 num_extra_attrs);
6029
6030 /* Copy over the attributes from the stub to the DIE we just read in. */
6031 comp_unit_die = *result_comp_unit_die;
6032 i = comp_unit_die->num_attrs;
6033 if (stmt_list != NULL)
6034 comp_unit_die->attrs[i++] = *stmt_list;
6035 if (low_pc != NULL)
6036 comp_unit_die->attrs[i++] = *low_pc;
6037 if (high_pc != NULL)
6038 comp_unit_die->attrs[i++] = *high_pc;
6039 if (ranges != NULL)
6040 comp_unit_die->attrs[i++] = *ranges;
6041 if (comp_dir != NULL)
6042 comp_unit_die->attrs[i++] = *comp_dir;
6043 comp_unit_die->num_attrs += num_extra_attrs;
6044
6045 if (dwarf_die_debug)
6046 {
6047 gdb_printf (gdb_stdlog,
6048 "Read die from %s@0x%x of %s:\n",
6049 section->get_name (),
6050 (unsigned) (begin_info_ptr - section->buffer),
6051 bfd_get_filename (abfd));
6052 dump_die (comp_unit_die, dwarf_die_debug);
6053 }
6054
6055 /* Skip dummy compilation units. */
6056 if (info_ptr >= begin_info_ptr + dwo_unit->length
6057 || peek_abbrev_code (abfd, info_ptr) == 0)
6058 return 0;
6059
6060 *result_info_ptr = info_ptr;
6061 return 1;
6062 }
6063
6064 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6065 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6066 signature is part of the header. */
6067 static gdb::optional<ULONGEST>
6068 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6069 {
6070 if (cu->header.version >= 5)
6071 return cu->header.signature;
6072 struct attribute *attr;
6073 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6074 if (attr == nullptr || !attr->form_is_unsigned ())
6075 return gdb::optional<ULONGEST> ();
6076 return attr->as_unsigned ();
6077 }
6078
6079 /* Subroutine of cutu_reader to simplify it.
6080 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6081 Returns NULL if the specified DWO unit cannot be found. */
6082
6083 static struct dwo_unit *
6084 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6085 {
6086 #if CXX_STD_THREAD
6087 /* We need a lock here both to handle the DWO hash table, and BFD,
6088 which is not thread-safe. */
6089 static std::mutex dwo_lock;
6090
6091 std::lock_guard<std::mutex> guard (dwo_lock);
6092 #endif
6093
6094 dwarf2_per_cu_data *per_cu = cu->per_cu;
6095 struct dwo_unit *dwo_unit;
6096 const char *comp_dir;
6097
6098 gdb_assert (cu != NULL);
6099
6100 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6101 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6102 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6103
6104 if (per_cu->is_debug_types)
6105 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6106 else
6107 {
6108 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6109
6110 if (!signature.has_value ())
6111 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6112 " [in module %s]"),
6113 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6114
6115 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6116 }
6117
6118 return dwo_unit;
6119 }
6120
6121 /* Subroutine of cutu_reader to simplify it.
6122 See it for a description of the parameters.
6123 Read a TU directly from a DWO file, bypassing the stub. */
6124
6125 void
6126 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6127 dwarf2_per_objfile *per_objfile,
6128 dwarf2_cu *existing_cu)
6129 {
6130 struct signatured_type *sig_type;
6131
6132 /* Verify we can do the following downcast, and that we have the
6133 data we need. */
6134 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6135 sig_type = (struct signatured_type *) this_cu;
6136 gdb_assert (sig_type->dwo_unit != NULL);
6137
6138 dwarf2_cu *cu;
6139
6140 if (existing_cu != nullptr)
6141 {
6142 cu = existing_cu;
6143 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6144 /* There's no need to do the rereading_dwo_cu handling that
6145 cutu_reader does since we don't read the stub. */
6146 }
6147 else
6148 {
6149 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6150 in per_objfile yet. */
6151 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6152 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6153 cu = m_new_cu.get ();
6154 }
6155
6156 /* A future optimization, if needed, would be to use an existing
6157 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6158 could share abbrev tables. */
6159
6160 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6161 NULL /* stub_comp_unit_die */,
6162 sig_type->dwo_unit->dwo_file->comp_dir,
6163 this, &info_ptr,
6164 &comp_unit_die,
6165 &m_dwo_abbrev_table) == 0)
6166 {
6167 /* Dummy die. */
6168 dummy_p = true;
6169 }
6170 }
6171
6172 /* Initialize a CU (or TU) and read its DIEs.
6173 If the CU defers to a DWO file, read the DWO file as well.
6174
6175 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6176 Otherwise the table specified in the comp unit header is read in and used.
6177 This is an optimization for when we already have the abbrev table.
6178
6179 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6180 allocated. */
6181
6182 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6183 dwarf2_per_objfile *per_objfile,
6184 struct abbrev_table *abbrev_table,
6185 dwarf2_cu *existing_cu,
6186 bool skip_partial,
6187 abbrev_cache *cache)
6188 : die_reader_specs {},
6189 m_this_cu (this_cu)
6190 {
6191 struct objfile *objfile = per_objfile->objfile;
6192 struct dwarf2_section_info *section = this_cu->section;
6193 bfd *abfd = section->get_bfd_owner ();
6194 const gdb_byte *begin_info_ptr;
6195 struct signatured_type *sig_type = NULL;
6196 struct dwarf2_section_info *abbrev_section;
6197 /* Non-zero if CU currently points to a DWO file and we need to
6198 reread it. When this happens we need to reread the skeleton die
6199 before we can reread the DWO file (this only applies to CUs, not TUs). */
6200 int rereading_dwo_cu = 0;
6201
6202 if (dwarf_die_debug)
6203 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6204 this_cu->is_debug_types ? "type" : "comp",
6205 sect_offset_str (this_cu->sect_off));
6206
6207 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6208 file (instead of going through the stub), short-circuit all of this. */
6209 if (this_cu->reading_dwo_directly)
6210 {
6211 /* Narrow down the scope of possibilities to have to understand. */
6212 gdb_assert (this_cu->is_debug_types);
6213 gdb_assert (abbrev_table == NULL);
6214 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6215 return;
6216 }
6217
6218 /* This is cheap if the section is already read in. */
6219 section->read (objfile);
6220
6221 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6222
6223 abbrev_section = get_abbrev_section_for_cu (this_cu);
6224
6225 dwarf2_cu *cu;
6226
6227 if (existing_cu != nullptr)
6228 {
6229 cu = existing_cu;
6230 /* If this CU is from a DWO file we need to start over, we need to
6231 refetch the attributes from the skeleton CU.
6232 This could be optimized by retrieving those attributes from when we
6233 were here the first time: the previous comp_unit_die was stored in
6234 comp_unit_obstack. But there's no data yet that we need this
6235 optimization. */
6236 if (cu->dwo_unit != NULL)
6237 rereading_dwo_cu = 1;
6238 }
6239 else
6240 {
6241 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6242 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6243 let us know that the CU is being scanned using the parallel
6244 indexer. This assert is avoided in this case because (1) it
6245 is irrelevant, and (2) the get_cu method is not
6246 thread-safe. */
6247 gdb_assert (cache != nullptr
6248 || per_objfile->get_cu (this_cu) == nullptr);
6249 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6250 cu = m_new_cu.get ();
6251 }
6252
6253 /* Get the header. */
6254 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6255 {
6256 /* We already have the header, there's no need to read it in again. */
6257 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6258 }
6259 else
6260 {
6261 if (this_cu->is_debug_types)
6262 {
6263 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6264 section, abbrev_section,
6265 info_ptr, rcuh_kind::TYPE);
6266
6267 /* Since per_cu is the first member of struct signatured_type,
6268 we can go from a pointer to one to a pointer to the other. */
6269 sig_type = (struct signatured_type *) this_cu;
6270 gdb_assert (sig_type->signature == cu->header.signature);
6271 gdb_assert (sig_type->type_offset_in_tu
6272 == cu->header.type_cu_offset_in_tu);
6273 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6274
6275 /* LENGTH has not been set yet for type units if we're
6276 using .gdb_index. */
6277 this_cu->length = cu->header.get_length ();
6278
6279 /* Establish the type offset that can be used to lookup the type. */
6280 sig_type->type_offset_in_section =
6281 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6282
6283 this_cu->dwarf_version = cu->header.version;
6284 }
6285 else
6286 {
6287 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6288 section, abbrev_section,
6289 info_ptr,
6290 rcuh_kind::COMPILE);
6291
6292 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6293 if (this_cu->length == 0)
6294 this_cu->length = cu->header.get_length ();
6295 else
6296 gdb_assert (this_cu->length == cu->header.get_length ());
6297 this_cu->dwarf_version = cu->header.version;
6298 }
6299 }
6300
6301 /* Skip dummy compilation units. */
6302 if (info_ptr >= begin_info_ptr + this_cu->length
6303 || peek_abbrev_code (abfd, info_ptr) == 0)
6304 {
6305 dummy_p = true;
6306 return;
6307 }
6308
6309 /* If we don't have them yet, read the abbrevs for this compilation unit.
6310 And if we need to read them now, make sure they're freed when we're
6311 done. */
6312 if (abbrev_table != NULL)
6313 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6314 else
6315 {
6316 if (cache != nullptr)
6317 abbrev_table = cache->find (abbrev_section,
6318 cu->header.abbrev_sect_off);
6319 if (abbrev_table == nullptr)
6320 {
6321 abbrev_section->read (objfile);
6322 m_abbrev_table_holder
6323 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6324 abbrev_table = m_abbrev_table_holder.get ();
6325 }
6326 }
6327
6328 /* Read the top level CU/TU die. */
6329 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6330 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6331
6332 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6333 {
6334 dummy_p = true;
6335 return;
6336 }
6337
6338 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6339 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6340 table from the DWO file and pass the ownership over to us. It will be
6341 referenced from READER, so we must make sure to free it after we're done
6342 with READER.
6343
6344 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6345 DWO CU, that this test will fail (the attribute will not be present). */
6346 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6347 if (dwo_name != nullptr)
6348 {
6349 struct dwo_unit *dwo_unit;
6350 struct die_info *dwo_comp_unit_die;
6351
6352 if (comp_unit_die->has_children)
6353 {
6354 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6355 " has children (offset %s) [in module %s]"),
6356 sect_offset_str (this_cu->sect_off),
6357 bfd_get_filename (abfd));
6358 }
6359 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6360 if (dwo_unit != NULL)
6361 {
6362 if (read_cutu_die_from_dwo (cu, dwo_unit,
6363 comp_unit_die, NULL,
6364 this, &info_ptr,
6365 &dwo_comp_unit_die,
6366 &m_dwo_abbrev_table) == 0)
6367 {
6368 /* Dummy die. */
6369 dummy_p = true;
6370 return;
6371 }
6372 comp_unit_die = dwo_comp_unit_die;
6373 }
6374 else
6375 {
6376 /* Yikes, we couldn't find the rest of the DIE, we only have
6377 the stub. A complaint has already been logged. There's
6378 not much more we can do except pass on the stub DIE to
6379 die_reader_func. We don't want to throw an error on bad
6380 debug info. */
6381 }
6382 }
6383 }
6384
6385 void
6386 cutu_reader::keep ()
6387 {
6388 /* Done, clean up. */
6389 gdb_assert (!dummy_p);
6390 if (m_new_cu != NULL)
6391 {
6392 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6393 now. */
6394 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6395 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6396 }
6397 }
6398
6399 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6400 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6401 assumed to have already done the lookup to find the DWO file).
6402
6403 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6404 THIS_CU->is_debug_types, but nothing else.
6405
6406 We fill in THIS_CU->length.
6407
6408 THIS_CU->cu is always freed when done.
6409 This is done in order to not leave THIS_CU->cu in a state where we have
6410 to care whether it refers to the "main" CU or the DWO CU.
6411
6412 When parent_cu is passed, it is used to provide a default value for
6413 str_offsets_base and addr_base from the parent. */
6414
6415 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6416 dwarf2_per_objfile *per_objfile,
6417 struct dwarf2_cu *parent_cu,
6418 struct dwo_file *dwo_file)
6419 : die_reader_specs {},
6420 m_this_cu (this_cu)
6421 {
6422 struct objfile *objfile = per_objfile->objfile;
6423 struct dwarf2_section_info *section = this_cu->section;
6424 bfd *abfd = section->get_bfd_owner ();
6425 struct dwarf2_section_info *abbrev_section;
6426 const gdb_byte *begin_info_ptr, *info_ptr;
6427
6428 if (dwarf_die_debug)
6429 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6430 this_cu->is_debug_types ? "type" : "comp",
6431 sect_offset_str (this_cu->sect_off));
6432
6433 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6434
6435 abbrev_section = (dwo_file != NULL
6436 ? &dwo_file->sections.abbrev
6437 : get_abbrev_section_for_cu (this_cu));
6438
6439 /* This is cheap if the section is already read in. */
6440 section->read (objfile);
6441
6442 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6443
6444 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6445 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6446 section, abbrev_section, info_ptr,
6447 (this_cu->is_debug_types
6448 ? rcuh_kind::TYPE
6449 : rcuh_kind::COMPILE));
6450
6451 if (parent_cu != nullptr)
6452 {
6453 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6454 m_new_cu->addr_base = parent_cu->addr_base;
6455 }
6456 this_cu->length = m_new_cu->header.get_length ();
6457
6458 /* Skip dummy compilation units. */
6459 if (info_ptr >= begin_info_ptr + this_cu->length
6460 || peek_abbrev_code (abfd, info_ptr) == 0)
6461 {
6462 dummy_p = true;
6463 return;
6464 }
6465
6466 abbrev_section->read (objfile);
6467 m_abbrev_table_holder
6468 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6469
6470 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6471 m_abbrev_table_holder.get ());
6472 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6473 }
6474
6475 \f
6476 /* Type Unit Groups.
6477
6478 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6479 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6480 so that all types coming from the same compilation (.o file) are grouped
6481 together. A future step could be to put the types in the same symtab as
6482 the CU the types ultimately came from. */
6483
6484 static hashval_t
6485 hash_type_unit_group (const void *item)
6486 {
6487 const struct type_unit_group *tu_group
6488 = (const struct type_unit_group *) item;
6489
6490 return hash_stmt_list_entry (&tu_group->hash);
6491 }
6492
6493 static int
6494 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6495 {
6496 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6497 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6498
6499 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6500 }
6501
6502 /* Allocate a hash table for type unit groups. */
6503
6504 static htab_up
6505 allocate_type_unit_groups_table ()
6506 {
6507 return htab_up (htab_create_alloc (3,
6508 hash_type_unit_group,
6509 eq_type_unit_group,
6510 htab_delete_entry<type_unit_group>,
6511 xcalloc, xfree));
6512 }
6513
6514 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6515 partial symtabs. We combine several TUs per psymtab to not let the size
6516 of any one psymtab grow too big. */
6517 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6518 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6519
6520 /* Helper routine for get_type_unit_group.
6521 Create the type_unit_group object used to hold one or more TUs. */
6522
6523 static std::unique_ptr<type_unit_group>
6524 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6525 {
6526 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6527
6528 tu_group->hash.dwo_unit = cu->dwo_unit;
6529 tu_group->hash.line_sect_off = line_offset_struct;
6530
6531 return tu_group;
6532 }
6533
6534 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6535 STMT_LIST is a DW_AT_stmt_list attribute. */
6536
6537 static struct type_unit_group *
6538 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6539 {
6540 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6541 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6542 struct type_unit_group *tu_group;
6543 void **slot;
6544 unsigned int line_offset;
6545 struct type_unit_group type_unit_group_for_lookup;
6546
6547 if (per_objfile->per_bfd->type_unit_groups == NULL)
6548 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6549
6550 /* Do we need to create a new group, or can we use an existing one? */
6551
6552 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6553 {
6554 line_offset = stmt_list->as_unsigned ();
6555 ++tu_stats->nr_symtab_sharers;
6556 }
6557 else
6558 {
6559 /* Ugh, no stmt_list. Rare, but we have to handle it.
6560 We can do various things here like create one group per TU or
6561 spread them over multiple groups to split up the expansion work.
6562 To avoid worst case scenarios (too many groups or too large groups)
6563 we, umm, group them in bunches. */
6564 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6565 | (tu_stats->nr_stmt_less_type_units
6566 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6567 ++tu_stats->nr_stmt_less_type_units;
6568 }
6569
6570 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6571 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6572 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6573 &type_unit_group_for_lookup, INSERT);
6574 if (*slot == nullptr)
6575 {
6576 sect_offset line_offset_struct = (sect_offset) line_offset;
6577 std::unique_ptr<type_unit_group> grp
6578 = create_type_unit_group (cu, line_offset_struct);
6579 *slot = grp.release ();
6580 ++tu_stats->nr_symtabs;
6581 }
6582
6583 tu_group = (struct type_unit_group *) *slot;
6584 gdb_assert (tu_group != nullptr);
6585 return tu_group;
6586 }
6587 \f
6588
6589 /* An instance of this is created when scanning DWARF to create a
6590 cooked index. */
6591
6592 class cooked_index_storage
6593 {
6594 public:
6595
6596 cooked_index_storage ()
6597 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
6598 eq_cutu_reader,
6599 htab_delete_entry<cutu_reader>,
6600 xcalloc, xfree)),
6601 m_index (new cooked_index),
6602 m_addrmap_storage (),
6603 m_addrmap (addrmap_create_mutable (&m_addrmap_storage))
6604 {
6605 }
6606
6607 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
6608
6609 /* Return the current abbrev cache. */
6610 abbrev_cache *get_abbrev_cache ()
6611 {
6612 return &m_abbrev_cache;
6613 }
6614
6615 /* Return the DIE reader corresponding to PER_CU. If no such reader
6616 has been registered, return NULL. */
6617 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
6618 {
6619 int index = per_cu->index;
6620 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
6621 &index, index);
6622 }
6623
6624 /* Preserve READER by storing it in the local hash table. */
6625 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
6626 {
6627 m_abbrev_cache.add (reader->release_abbrev_table ());
6628
6629 int index = reader->cu->per_cu->index;
6630 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
6631 index, INSERT);
6632 gdb_assert (*slot == nullptr);
6633 cutu_reader *result = reader.get ();
6634 *slot = reader.release ();
6635 return result;
6636 }
6637
6638 /* Add an entry to the index. The arguments describe the entry; see
6639 cooked-index.h. The new entry is returned. */
6640 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
6641 cooked_index_flag flags,
6642 const char *name,
6643 const cooked_index_entry *parent_entry,
6644 dwarf2_per_cu_data *per_cu)
6645 {
6646 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
6647 }
6648
6649 /* Install the current addrmap into the index being constructed,
6650 then transfer ownership of the index to the caller. */
6651 std::unique_ptr<cooked_index> release ()
6652 {
6653 m_index->install_addrmap (m_addrmap);
6654 return std::move (m_index);
6655 }
6656
6657 /* Return the mutable addrmap that is currently being created. */
6658 addrmap *get_addrmap ()
6659 {
6660 return m_addrmap;
6661 }
6662
6663 private:
6664
6665 /* Hash function for a cutu_reader. */
6666 static hashval_t hash_cutu_reader (const void *a)
6667 {
6668 const cutu_reader *reader = (const cutu_reader *) a;
6669 return reader->cu->per_cu->index;
6670 }
6671
6672 /* Equality function for cutu_reader. */
6673 static int eq_cutu_reader (const void *a, const void *b)
6674 {
6675 const cutu_reader *ra = (const cutu_reader *) a;
6676 const int *rb = (const int *) b;
6677 return ra->cu->per_cu->index == *rb;
6678 }
6679
6680 /* The abbrev cache used by this indexer. */
6681 abbrev_cache m_abbrev_cache;
6682 /* A hash table of cutu_reader objects. */
6683 htab_up m_reader_hash;
6684 /* The index that is being constructed. */
6685 std::unique_ptr<cooked_index> m_index;
6686
6687 /* Storage for the writeable addrmap. */
6688 auto_obstack m_addrmap_storage;
6689 /* A writeable addrmap being constructed by this scanner. */
6690 addrmap *m_addrmap;
6691 };
6692
6693 /* An instance of this is created to index a CU. */
6694
6695 class cooked_indexer
6696 {
6697 public:
6698
6699 cooked_indexer (cooked_index_storage *storage,
6700 dwarf2_per_cu_data *per_cu,
6701 enum language language)
6702 : m_index_storage (storage),
6703 m_per_cu (per_cu),
6704 m_language (language),
6705 m_obstack (),
6706 m_die_range_map (addrmap_create_mutable (&m_obstack))
6707 {
6708 }
6709
6710 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
6711
6712 /* Index the given CU. */
6713 void make_index (cutu_reader *reader);
6714
6715 private:
6716
6717 /* A helper function to turn a section offset into an address that
6718 can be used in an addrmap. */
6719 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
6720 {
6721 CORE_ADDR value = to_underlying (offset);
6722 if (is_dwz)
6723 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
6724 return value;
6725 }
6726
6727 /* A helper function to scan the PC bounds of READER and record them
6728 in the storage's addrmap. */
6729 void check_bounds (cutu_reader *reader);
6730
6731 /* Ensure that the indicated CU exists. The cutu_reader for it is
6732 returned. FOR_SCANNING is true if the caller intends to scan all
6733 the DIEs in the CU; when false, this use is assumed to be to look
6734 up just a single DIE. */
6735 cutu_reader *ensure_cu_exists (cutu_reader *reader,
6736 dwarf2_per_objfile *per_objfile,
6737 sect_offset sect_off,
6738 bool is_dwz,
6739 bool for_scanning);
6740
6741 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6742 the entry for the enclosing scope (nullptr at top level). FULLY
6743 is true when a full scan must be done -- in some languages,
6744 function scopes must be fully explored in order to find nested
6745 functions. This returns a pointer to just after the spot where
6746 reading stopped. */
6747 const gdb_byte *index_dies (cutu_reader *reader,
6748 const gdb_byte *info_ptr,
6749 const cooked_index_entry *parent_entry,
6750 bool fully);
6751
6752 /* Scan the attributes for a given DIE and update the out
6753 parameters. Returns a pointer to the byte after the DIE. */
6754 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
6755 cutu_reader *reader,
6756 const gdb_byte *watermark_ptr,
6757 const gdb_byte *info_ptr,
6758 const abbrev_info *abbrev,
6759 const char **name,
6760 const char **linkage_name,
6761 cooked_index_flag *flags,
6762 sect_offset *sibling_offset,
6763 const cooked_index_entry **parent_entry,
6764 CORE_ADDR *maybe_defer,
6765 bool for_specification);
6766
6767 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6768 DW_AT_import, and then scanning the referenced CU. Returns a
6769 pointer to the byte after the DIE. */
6770 const gdb_byte *index_imported_unit (cutu_reader *reader,
6771 const gdb_byte *info_ptr,
6772 const abbrev_info *abbrev);
6773
6774 /* Recursively read DIEs, recording the section offsets in
6775 m_die_range_map and then calling index_dies. */
6776 const gdb_byte *recurse (cutu_reader *reader,
6777 const gdb_byte *info_ptr,
6778 const cooked_index_entry *parent_entry,
6779 bool fully);
6780
6781 /* The storage object, where the results are kept. */
6782 cooked_index_storage *m_index_storage;
6783 /* The CU that we are reading on behalf of. This object might be
6784 asked to index one CU but to treat the results as if they come
6785 from some including CU; in this case the including CU would be
6786 recorded here. */
6787 dwarf2_per_cu_data *m_per_cu;
6788 /* The language that we're assuming when reading. */
6789 enum language m_language;
6790
6791 /* Temporary storage. */
6792 auto_obstack m_obstack;
6793 /* An addrmap that maps from section offsets (see the form_addr
6794 method) to newly-created entries. See m_deferred_entries to
6795 understand this. */
6796 addrmap *m_die_range_map;
6797
6798 /* A single deferred entry. */
6799 struct deferred_entry
6800 {
6801 sect_offset die_offset;
6802 const char *name;
6803 CORE_ADDR spec_offset;
6804 dwarf_tag tag;
6805 cooked_index_flag flags;
6806 };
6807
6808 /* The generated DWARF can sometimes have the declaration for a
6809 method in a class (or perhaps namespace) scope, with the
6810 definition appearing outside this scope... just one of the many
6811 bad things about DWARF. In order to handle this situation, we
6812 defer certain entries until the end of scanning, at which point
6813 we'll know the containing context of all the DIEs that we might
6814 have scanned. This vector stores these deferred entries. */
6815 std::vector<deferred_entry> m_deferred_entries;
6816 };
6817
6818 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6819 Process compilation unit THIS_CU for a psymtab. */
6820
6821 static void
6822 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
6823 dwarf2_per_objfile *per_objfile,
6824 cooked_index_storage *storage)
6825 {
6826 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
6827 storage->get_abbrev_cache ());
6828
6829 if (reader.comp_unit_die == nullptr)
6830 return;
6831
6832 switch (reader.comp_unit_die->tag)
6833 {
6834 case DW_TAG_compile_unit:
6835 this_cu->unit_type = DW_UT_compile;
6836 break;
6837 case DW_TAG_partial_unit:
6838 this_cu->unit_type = DW_UT_partial;
6839 break;
6840 case DW_TAG_type_unit:
6841 this_cu->unit_type = DW_UT_type;
6842 break;
6843 default:
6844 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6845 dwarf_tag_name (reader.comp_unit_die->tag),
6846 sect_offset_str (reader.cu->per_cu->sect_off),
6847 objfile_name (per_objfile->objfile));
6848 }
6849
6850 if (reader.dummy_p)
6851 {
6852 /* Nothing. */
6853 }
6854 else if (this_cu->is_debug_types)
6855 build_type_psymtabs_reader (&reader, storage);
6856 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
6857 {
6858 bool nope = false;
6859 if (this_cu->scanned.compare_exchange_strong (nope, true))
6860 {
6861 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
6862 language_minimal);
6863 gdb_assert (storage != nullptr);
6864 cooked_indexer indexer (storage, this_cu, reader.cu->per_cu->lang);
6865 indexer.make_index (&reader);
6866 }
6867 }
6868 }
6869
6870 /* Reader function for build_type_psymtabs. */
6871
6872 static void
6873 build_type_psymtabs_reader (cutu_reader *reader,
6874 cooked_index_storage *storage)
6875 {
6876 struct dwarf2_cu *cu = reader->cu;
6877 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6878 struct die_info *type_unit_die = reader->comp_unit_die;
6879
6880 gdb_assert (per_cu->is_debug_types);
6881
6882 if (! type_unit_die->has_children)
6883 return;
6884
6885 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6886
6887 gdb_assert (storage != nullptr);
6888 cooked_indexer indexer (storage, per_cu, cu->per_cu->lang);
6889 indexer.make_index (reader);
6890 }
6891
6892 /* Struct used to sort TUs by their abbreviation table offset. */
6893
6894 struct tu_abbrev_offset
6895 {
6896 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
6897 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
6898 {}
6899
6900 /* This is used when sorting. */
6901 bool operator< (const tu_abbrev_offset &other) const
6902 {
6903 return abbrev_offset < other.abbrev_offset;
6904 }
6905
6906 signatured_type *sig_type;
6907 sect_offset abbrev_offset;
6908 };
6909
6910 /* Efficiently read all the type units.
6911
6912 The efficiency is because we sort TUs by the abbrev table they use and
6913 only read each abbrev table once. In one program there are 200K TUs
6914 sharing 8K abbrev tables.
6915
6916 The main purpose of this function is to support building the
6917 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6918 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6919 can collapse the search space by grouping them by stmt_list.
6920 The savings can be significant, in the same program from above the 200K TUs
6921 share 8K stmt_list tables.
6922
6923 FUNC is expected to call get_type_unit_group, which will create the
6924 struct type_unit_group if necessary and add it to
6925 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6926
6927 static void
6928 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
6929 cooked_index_storage *storage)
6930 {
6931 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6932 abbrev_table_up abbrev_table;
6933 sect_offset abbrev_offset;
6934
6935 /* It's up to the caller to not call us multiple times. */
6936 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
6937
6938 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
6939 return;
6940
6941 /* TUs typically share abbrev tables, and there can be way more TUs than
6942 abbrev tables. Sort by abbrev table to reduce the number of times we
6943 read each abbrev table in.
6944 Alternatives are to punt or to maintain a cache of abbrev tables.
6945 This is simpler and efficient enough for now.
6946
6947 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6948 symtab to use). Typically TUs with the same abbrev offset have the same
6949 stmt_list value too so in practice this should work well.
6950
6951 The basic algorithm here is:
6952
6953 sort TUs by abbrev table
6954 for each TU with same abbrev table:
6955 read abbrev table if first user
6956 read TU top level DIE
6957 [IWBN if DWO skeletons had DW_AT_stmt_list]
6958 call FUNC */
6959
6960 dwarf_read_debug_printf ("Building type unit groups ...");
6961
6962 /* Sort in a separate table to maintain the order of all_comp_units
6963 for .gdb_index: TU indices directly index all_type_units. */
6964 std::vector<tu_abbrev_offset> sorted_by_abbrev;
6965 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
6966
6967 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
6968 {
6969 if (cu->is_debug_types)
6970 {
6971 auto sig_type = static_cast<signatured_type *> (cu.get ());
6972 sorted_by_abbrev.emplace_back
6973 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
6974 sig_type->sect_off));
6975 }
6976 }
6977
6978 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
6979
6980 abbrev_offset = (sect_offset) ~(unsigned) 0;
6981
6982 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
6983 {
6984 /* Switch to the next abbrev table if necessary. */
6985 if (abbrev_table == NULL
6986 || tu.abbrev_offset != abbrev_offset)
6987 {
6988 abbrev_offset = tu.abbrev_offset;
6989 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
6990 abbrev_table =
6991 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
6992 ++tu_stats->nr_uniq_abbrev_tables;
6993 }
6994
6995 cutu_reader reader (tu.sig_type, per_objfile,
6996 abbrev_table.get (), nullptr, false);
6997 if (!reader.dummy_p)
6998 build_type_psymtabs_reader (&reader, storage);
6999 }
7000 }
7001
7002 /* Print collected type unit statistics. */
7003
7004 static void
7005 print_tu_stats (dwarf2_per_objfile *per_objfile)
7006 {
7007 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7008
7009 dwarf_read_debug_printf ("Type unit statistics:");
7010 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7011 dwarf_read_debug_printf (" %d uniq abbrev tables",
7012 tu_stats->nr_uniq_abbrev_tables);
7013 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7014 tu_stats->nr_symtabs);
7015 dwarf_read_debug_printf (" %d symtab sharers",
7016 tu_stats->nr_symtab_sharers);
7017 dwarf_read_debug_printf (" %d type units without a stmt_list",
7018 tu_stats->nr_stmt_less_type_units);
7019 dwarf_read_debug_printf (" %d all_type_units reallocs",
7020 tu_stats->nr_all_type_units_reallocs);
7021 }
7022
7023 struct skeleton_data
7024 {
7025 dwarf2_per_objfile *per_objfile;
7026 cooked_index_storage *storage;
7027 };
7028
7029 /* Traversal function for process_skeletonless_type_unit.
7030 Read a TU in a DWO file and build partial symbols for it. */
7031
7032 static int
7033 process_skeletonless_type_unit (void **slot, void *info)
7034 {
7035 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7036 skeleton_data *data = (skeleton_data *) info;
7037
7038 /* If this TU doesn't exist in the global table, add it and read it in. */
7039
7040 if (data->per_objfile->per_bfd->signatured_types == NULL)
7041 data->per_objfile->per_bfd->signatured_types
7042 = allocate_signatured_type_table ();
7043
7044 signatured_type find_entry (dwo_unit->signature);
7045 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
7046 &find_entry, INSERT);
7047 /* If we've already seen this type there's nothing to do. What's happening
7048 is we're doing our own version of comdat-folding here. */
7049 if (*slot != NULL)
7050 return 1;
7051
7052 /* This does the job that create_all_comp_units would have done for
7053 this TU. */
7054 signatured_type *entry
7055 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
7056 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
7057 *slot = entry;
7058
7059 /* This does the job that build_type_psymtabs would have done. */
7060 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
7061 if (!reader.dummy_p)
7062 build_type_psymtabs_reader (&reader, data->storage);
7063
7064 return 1;
7065 }
7066
7067 /* Traversal function for process_skeletonless_type_units. */
7068
7069 static int
7070 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7071 {
7072 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7073
7074 if (dwo_file->tus != NULL)
7075 htab_traverse_noresize (dwo_file->tus.get (),
7076 process_skeletonless_type_unit, info);
7077
7078 return 1;
7079 }
7080
7081 /* Scan all TUs of DWO files, verifying we've processed them.
7082 This is needed in case a TU was emitted without its skeleton.
7083 Note: This can't be done until we know what all the DWO files are. */
7084
7085 static void
7086 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
7087 cooked_index_storage *storage)
7088 {
7089 skeleton_data data { per_objfile, storage };
7090
7091 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7092 if (get_dwp_file (per_objfile) == NULL
7093 && per_objfile->per_bfd->dwo_files != NULL)
7094 {
7095 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7096 process_dwo_file_for_skeletonless_type_units,
7097 &data);
7098 }
7099 }
7100
7101 /* Build the partial symbol table by doing a quick pass through the
7102 .debug_info and .debug_abbrev sections. */
7103
7104 static void
7105 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7106 {
7107 struct objfile *objfile = per_objfile->objfile;
7108 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7109
7110 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7111 objfile_name (objfile));
7112
7113 per_bfd->map_info_sections (objfile);
7114
7115 cooked_index_storage index_storage;
7116 create_all_comp_units (per_objfile);
7117 build_type_psymtabs (per_objfile, &index_storage);
7118 std::vector<std::unique_ptr<cooked_index>> indexes;
7119
7120 per_bfd->quick_file_names_table
7121 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
7122 if (!per_bfd->debug_aranges.empty ())
7123 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
7124 index_storage.get_addrmap ());
7125
7126 {
7127 /* Ensure that complaints are handled correctly. */
7128 complaint_interceptor complaint_handler;
7129
7130 using iter_type = decltype (per_bfd->all_comp_units.begin ());
7131
7132 /* Each thread returns a pair holding a cooked index, and a vector
7133 of errors that should be printed. The latter is done because
7134 GDB's I/O system is not thread-safe. run_on_main_thread could be
7135 used, but that would mean the messages are printed after the
7136 prompt, which looks weird. */
7137 using result_type = std::pair<std::unique_ptr<cooked_index>,
7138 std::vector<gdb_exception>>;
7139 std::vector<result_type> results
7140 = gdb::parallel_for_each (1, per_bfd->all_comp_units.begin (),
7141 per_bfd->all_comp_units.end (),
7142 [=] (iter_type iter, iter_type end)
7143 {
7144 std::vector<gdb_exception> errors;
7145 cooked_index_storage thread_storage;
7146 for (; iter != end; ++iter)
7147 {
7148 dwarf2_per_cu_data *per_cu = iter->get ();
7149 try
7150 {
7151 process_psymtab_comp_unit (per_cu, per_objfile,
7152 &thread_storage);
7153 }
7154 catch (gdb_exception &except)
7155 {
7156 errors.push_back (std::move (except));
7157 }
7158 }
7159 return result_type (thread_storage.release (), std::move (errors));
7160 });
7161
7162 /* Only show a given exception a single time. */
7163 std::unordered_set<gdb_exception> seen_exceptions;
7164 for (auto &one_result : results)
7165 {
7166 indexes.push_back (std::move (one_result.first));
7167 for (auto &one_exc : one_result.second)
7168 if (seen_exceptions.insert (one_exc).second)
7169 exception_print (gdb_stderr, one_exc);
7170 }
7171 }
7172
7173 /* This has to wait until we read the CUs, we need the list of DWOs. */
7174 process_skeletonless_type_units (per_objfile, &index_storage);
7175
7176 if (dwarf_read_debug > 0)
7177 print_tu_stats (per_objfile);
7178
7179 indexes.push_back (index_storage.release ());
7180 /* Remove any NULL entries. This might happen if parallel-for
7181 decides to throttle the number of threads that were used. */
7182 indexes.erase
7183 (std::remove_if (indexes.begin (),
7184 indexes.end (),
7185 [] (const std::unique_ptr<cooked_index> &entry)
7186 {
7187 return entry == nullptr;
7188 }),
7189 indexes.end ());
7190 indexes.shrink_to_fit ();
7191 per_bfd->cooked_index_table.reset
7192 (new cooked_index_vector (std::move (indexes)));
7193
7194 const cooked_index_entry *main_entry
7195 = per_bfd->cooked_index_table->get_main ();
7196 if (main_entry != nullptr)
7197 set_objfile_main_name (objfile, main_entry->name,
7198 main_entry->per_cu->lang);
7199
7200 dwarf_read_debug_printf ("Done building psymtabs of %s",
7201 objfile_name (objfile));
7202 }
7203
7204 static void
7205 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7206 struct dwarf2_section_info *section,
7207 struct dwarf2_section_info *abbrev_section,
7208 unsigned int is_dwz,
7209 htab_up &types_htab,
7210 rcuh_kind section_kind)
7211 {
7212 const gdb_byte *info_ptr;
7213 struct objfile *objfile = per_objfile->objfile;
7214
7215 dwarf_read_debug_printf ("Reading %s for %s",
7216 section->get_name (),
7217 section->get_file_name ());
7218
7219 section->read (objfile);
7220
7221 info_ptr = section->buffer;
7222
7223 while (info_ptr < section->buffer + section->size)
7224 {
7225 dwarf2_per_cu_data_up this_cu;
7226
7227 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7228
7229 comp_unit_head cu_header;
7230 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7231 abbrev_section, info_ptr,
7232 section_kind);
7233
7234 /* Save the compilation unit for later lookup. */
7235 if (cu_header.unit_type != DW_UT_type)
7236 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7237 else
7238 {
7239 if (types_htab == nullptr)
7240 types_htab = allocate_signatured_type_table ();
7241
7242 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7243 (cu_header.signature);
7244 signatured_type *sig_ptr = sig_type.get ();
7245 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7246 this_cu.reset (sig_type.release ());
7247
7248 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7249 gdb_assert (slot != nullptr);
7250 if (*slot != nullptr)
7251 complaint (_("debug type entry at offset %s is duplicate to"
7252 " the entry at offset %s, signature %s"),
7253 sect_offset_str (sect_off),
7254 sect_offset_str (sig_ptr->sect_off),
7255 hex_string (sig_ptr->signature));
7256 *slot = sig_ptr;
7257 }
7258 this_cu->sect_off = sect_off;
7259 this_cu->length = cu_header.length + cu_header.initial_length_size;
7260 this_cu->is_dwz = is_dwz;
7261 this_cu->section = section;
7262
7263 info_ptr = info_ptr + this_cu->length;
7264 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7265 }
7266 }
7267
7268 /* Create a list of all compilation units in OBJFILE.
7269 This is only done for -readnow and building partial symtabs. */
7270
7271 static void
7272 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7273 {
7274 htab_up types_htab;
7275
7276 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7277 &per_objfile->per_bfd->abbrev, 0,
7278 types_htab, rcuh_kind::COMPILE);
7279 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7280 read_comp_units_from_section (per_objfile, &section,
7281 &per_objfile->per_bfd->abbrev, 0,
7282 types_htab, rcuh_kind::TYPE);
7283
7284 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7285 if (dwz != NULL)
7286 {
7287 /* Pre-read the sections we'll need to construct an index. */
7288 struct objfile *objfile = per_objfile->objfile;
7289 dwz->abbrev.read (objfile);
7290 dwz->info.read (objfile);
7291 dwz->str.read (objfile);
7292 dwz->line.read (objfile);
7293 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7294 types_htab, rcuh_kind::COMPILE);
7295 }
7296
7297 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7298 }
7299
7300 /* Return the initial uleb128 in the die at INFO_PTR. */
7301
7302 static unsigned int
7303 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7304 {
7305 unsigned int bytes_read;
7306
7307 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7308 }
7309
7310 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7311 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7312
7313 Return the corresponding abbrev, or NULL if the number is zero (indicating
7314 an empty DIE). In either case *BYTES_READ will be set to the length of
7315 the initial number. */
7316
7317 static const struct abbrev_info *
7318 peek_die_abbrev (const die_reader_specs &reader,
7319 const gdb_byte *info_ptr, unsigned int *bytes_read)
7320 {
7321 dwarf2_cu *cu = reader.cu;
7322 bfd *abfd = reader.abfd;
7323 unsigned int abbrev_number
7324 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7325
7326 if (abbrev_number == 0)
7327 return NULL;
7328
7329 const abbrev_info *abbrev
7330 = reader.abbrev_table->lookup_abbrev (abbrev_number);
7331 if (!abbrev)
7332 {
7333 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7334 " at offset %s [in module %s]"),
7335 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7336 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
7337 }
7338
7339 return abbrev;
7340 }
7341
7342 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7343 Returns a pointer to the end of a series of DIEs, terminated by an empty
7344 DIE. Any children of the skipped DIEs will also be skipped. */
7345
7346 static const gdb_byte *
7347 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7348 {
7349 while (1)
7350 {
7351 unsigned int bytes_read;
7352 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
7353 &bytes_read);
7354
7355 if (abbrev == NULL)
7356 return info_ptr + bytes_read;
7357 else
7358 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7359 }
7360 }
7361
7362 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7363 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7364 abbrev corresponding to that skipped uleb128 should be passed in
7365 ABBREV.
7366
7367 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7368 returns a pointer to this DIE's sibling, skipping any children.
7369 Otherwise, returns a pointer to the DIE's first child. */
7370
7371 static const gdb_byte *
7372 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7373 const struct abbrev_info *abbrev, bool do_skip_children)
7374 {
7375 unsigned int bytes_read;
7376 struct attribute attr;
7377 bfd *abfd = reader->abfd;
7378 struct dwarf2_cu *cu = reader->cu;
7379 const gdb_byte *buffer = reader->buffer;
7380 const gdb_byte *buffer_end = reader->buffer_end;
7381 unsigned int form, i;
7382
7383 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
7384 {
7385 /* We only handle DW_FORM_ref4 here. */
7386 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
7387 unsigned int offset = read_4_bytes (abfd, sibling_data);
7388 const gdb_byte *sibling_ptr
7389 = buffer + to_underlying (cu->header.sect_off) + offset;
7390 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
7391 return sibling_ptr;
7392 /* Fall through to the slow way. */
7393 }
7394 else if (abbrev->size_if_constant != 0)
7395 {
7396 info_ptr += abbrev->size_if_constant;
7397 if (do_skip_children && abbrev->has_children)
7398 return skip_children (reader, info_ptr);
7399 return info_ptr;
7400 }
7401
7402 for (i = 0; i < abbrev->num_attrs; i++)
7403 {
7404 /* The only abbrev we care about is DW_AT_sibling. */
7405 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
7406 {
7407 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7408 if (attr.form == DW_FORM_ref_addr)
7409 complaint (_("ignoring absolute DW_AT_sibling"));
7410 else
7411 {
7412 sect_offset off = attr.get_ref_die_offset ();
7413 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7414
7415 if (sibling_ptr < info_ptr)
7416 complaint (_("DW_AT_sibling points backwards"));
7417 else if (sibling_ptr > reader->buffer_end)
7418 reader->die_section->overflow_complaint ();
7419 else
7420 return sibling_ptr;
7421 }
7422 }
7423
7424 /* If it isn't DW_AT_sibling, skip this attribute. */
7425 form = abbrev->attrs[i].form;
7426 skip_attribute:
7427 switch (form)
7428 {
7429 case DW_FORM_ref_addr:
7430 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7431 and later it is offset sized. */
7432 if (cu->header.version == 2)
7433 info_ptr += cu->header.addr_size;
7434 else
7435 info_ptr += cu->header.offset_size;
7436 break;
7437 case DW_FORM_GNU_ref_alt:
7438 info_ptr += cu->header.offset_size;
7439 break;
7440 case DW_FORM_addr:
7441 info_ptr += cu->header.addr_size;
7442 break;
7443 case DW_FORM_data1:
7444 case DW_FORM_ref1:
7445 case DW_FORM_flag:
7446 case DW_FORM_strx1:
7447 info_ptr += 1;
7448 break;
7449 case DW_FORM_flag_present:
7450 case DW_FORM_implicit_const:
7451 break;
7452 case DW_FORM_data2:
7453 case DW_FORM_ref2:
7454 case DW_FORM_strx2:
7455 info_ptr += 2;
7456 break;
7457 case DW_FORM_strx3:
7458 info_ptr += 3;
7459 break;
7460 case DW_FORM_data4:
7461 case DW_FORM_ref4:
7462 case DW_FORM_strx4:
7463 info_ptr += 4;
7464 break;
7465 case DW_FORM_data8:
7466 case DW_FORM_ref8:
7467 case DW_FORM_ref_sig8:
7468 info_ptr += 8;
7469 break;
7470 case DW_FORM_data16:
7471 info_ptr += 16;
7472 break;
7473 case DW_FORM_string:
7474 read_direct_string (abfd, info_ptr, &bytes_read);
7475 info_ptr += bytes_read;
7476 break;
7477 case DW_FORM_sec_offset:
7478 case DW_FORM_strp:
7479 case DW_FORM_GNU_strp_alt:
7480 info_ptr += cu->header.offset_size;
7481 break;
7482 case DW_FORM_exprloc:
7483 case DW_FORM_block:
7484 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7485 info_ptr += bytes_read;
7486 break;
7487 case DW_FORM_block1:
7488 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7489 break;
7490 case DW_FORM_block2:
7491 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7492 break;
7493 case DW_FORM_block4:
7494 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7495 break;
7496 case DW_FORM_addrx:
7497 case DW_FORM_strx:
7498 case DW_FORM_sdata:
7499 case DW_FORM_udata:
7500 case DW_FORM_ref_udata:
7501 case DW_FORM_GNU_addr_index:
7502 case DW_FORM_GNU_str_index:
7503 case DW_FORM_rnglistx:
7504 case DW_FORM_loclistx:
7505 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7506 break;
7507 case DW_FORM_indirect:
7508 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7509 info_ptr += bytes_read;
7510 /* We need to continue parsing from here, so just go back to
7511 the top. */
7512 goto skip_attribute;
7513
7514 default:
7515 error (_("Dwarf Error: Cannot handle %s "
7516 "in DWARF reader [in module %s]"),
7517 dwarf_form_name (form),
7518 bfd_get_filename (abfd));
7519 }
7520 }
7521
7522 if (do_skip_children && abbrev->has_children)
7523 return skip_children (reader, info_ptr);
7524 else
7525 return info_ptr;
7526 }
7527 \f
7528 /* Reading in full CUs. */
7529
7530 /* Add PER_CU to the queue. */
7531
7532 static void
7533 queue_comp_unit (dwarf2_per_cu_data *per_cu,
7534 dwarf2_per_objfile *per_objfile,
7535 enum language pretend_language)
7536 {
7537 per_cu->queued = 1;
7538
7539 gdb_assert (per_objfile->per_bfd->queue.has_value ());
7540 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
7541 }
7542
7543 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7544
7545 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7546 dependency.
7547
7548 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7549 DIEs, false otherwise.
7550
7551 Explanation: there is an invariant that if a CU is queued for expansion
7552 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7553 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7554 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7555 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7556 invariant is respected.
7557
7558 The caller is therefore not required to load the CU's DIEs (we return false)
7559 if:
7560
7561 - the CU is already expanded, and therefore does not get enqueued
7562 - the CU gets enqueued for expansion, but its DIEs are already loaded
7563
7564 Note that the caller should not use this function's return value as an
7565 indicator of whether the CU's DIEs are loaded right now, it should check
7566 that by calling `dwarf2_per_objfile::get_cu` instead. */
7567
7568 static int
7569 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7570 dwarf2_per_cu_data *per_cu,
7571 dwarf2_per_objfile *per_objfile,
7572 enum language pretend_language)
7573 {
7574 /* Mark the dependence relation so that we don't flush PER_CU
7575 too early. */
7576 if (dependent_cu != NULL)
7577 dependent_cu->add_dependence (per_cu);
7578
7579 /* If it's already on the queue, we have nothing to do. */
7580 if (per_cu->queued)
7581 {
7582 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7583 loaded. */
7584 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
7585
7586 /* If the CU is queued for expansion, it should not already be
7587 expanded. */
7588 gdb_assert (!per_objfile->symtab_set_p (per_cu));
7589
7590 /* The DIEs are already loaded, the caller doesn't need to do it. */
7591 return 0;
7592 }
7593
7594 bool queued = false;
7595 if (!per_objfile->symtab_set_p (per_cu))
7596 {
7597 /* Add it to the queue. */
7598 queue_comp_unit (per_cu, per_objfile, pretend_language);
7599 queued = true;
7600 }
7601
7602 /* If the compilation unit is already loaded, just mark it as
7603 used. */
7604 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7605 if (cu != nullptr)
7606 cu->last_used = 0;
7607
7608 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7609 and the DIEs are not already loaded. */
7610 return queued && cu == nullptr;
7611 }
7612
7613 /* Process the queue. */
7614
7615 static void
7616 process_queue (dwarf2_per_objfile *per_objfile)
7617 {
7618 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7619 objfile_name (per_objfile->objfile));
7620
7621 /* The queue starts out with one item, but following a DIE reference
7622 may load a new CU, adding it to the end of the queue. */
7623 while (!per_objfile->per_bfd->queue->empty ())
7624 {
7625 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7626 dwarf2_per_cu_data *per_cu = item.per_cu;
7627
7628 if (!per_objfile->symtab_set_p (per_cu))
7629 {
7630 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7631
7632 /* Skip dummy CUs. */
7633 if (cu != nullptr)
7634 {
7635 unsigned int debug_print_threshold;
7636 char buf[100];
7637
7638 if (per_cu->is_debug_types)
7639 {
7640 struct signatured_type *sig_type =
7641 (struct signatured_type *) per_cu;
7642
7643 sprintf (buf, "TU %s at offset %s",
7644 hex_string (sig_type->signature),
7645 sect_offset_str (per_cu->sect_off));
7646 /* There can be 100s of TUs.
7647 Only print them in verbose mode. */
7648 debug_print_threshold = 2;
7649 }
7650 else
7651 {
7652 sprintf (buf, "CU at offset %s",
7653 sect_offset_str (per_cu->sect_off));
7654 debug_print_threshold = 1;
7655 }
7656
7657 if (dwarf_read_debug >= debug_print_threshold)
7658 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
7659
7660 if (per_cu->is_debug_types)
7661 process_full_type_unit (cu, item.pretend_language);
7662 else
7663 process_full_comp_unit (cu, item.pretend_language);
7664
7665 if (dwarf_read_debug >= debug_print_threshold)
7666 dwarf_read_debug_printf ("Done expanding %s", buf);
7667 }
7668 }
7669
7670 per_cu->queued = 0;
7671 per_objfile->per_bfd->queue->pop ();
7672 }
7673
7674 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7675 objfile_name (per_objfile->objfile));
7676 }
7677
7678 /* Trivial hash function for die_info: the hash value of a DIE
7679 is its offset in .debug_info for this objfile. */
7680
7681 static hashval_t
7682 die_hash (const void *item)
7683 {
7684 const struct die_info *die = (const struct die_info *) item;
7685
7686 return to_underlying (die->sect_off);
7687 }
7688
7689 /* Trivial comparison function for die_info structures: two DIEs
7690 are equal if they have the same offset. */
7691
7692 static int
7693 die_eq (const void *item_lhs, const void *item_rhs)
7694 {
7695 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7696 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7697
7698 return die_lhs->sect_off == die_rhs->sect_off;
7699 }
7700
7701 /* Load the DIEs associated with PER_CU into memory.
7702
7703 In some cases, the caller, while reading partial symbols, will need to load
7704 the full symbols for the CU for some reason. It will already have a
7705 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7706 rather than creating a new one. */
7707
7708 static void
7709 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
7710 dwarf2_per_objfile *per_objfile,
7711 dwarf2_cu *existing_cu,
7712 bool skip_partial,
7713 enum language pretend_language)
7714 {
7715 gdb_assert (! this_cu->is_debug_types);
7716
7717 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
7718 if (reader.dummy_p)
7719 return;
7720
7721 struct dwarf2_cu *cu = reader.cu;
7722 const gdb_byte *info_ptr = reader.info_ptr;
7723
7724 gdb_assert (cu->die_hash == NULL);
7725 cu->die_hash =
7726 htab_create_alloc_ex (cu->header.length / 12,
7727 die_hash,
7728 die_eq,
7729 NULL,
7730 &cu->comp_unit_obstack,
7731 hashtab_obstack_allocate,
7732 dummy_obstack_deallocate);
7733
7734 if (reader.comp_unit_die->has_children)
7735 reader.comp_unit_die->child
7736 = read_die_and_siblings (&reader, reader.info_ptr,
7737 &info_ptr, reader.comp_unit_die);
7738 cu->dies = reader.comp_unit_die;
7739 /* comp_unit_die is not stored in die_hash, no need. */
7740
7741 /* We try not to read any attributes in this function, because not
7742 all CUs needed for references have been loaded yet, and symbol
7743 table processing isn't initialized. But we have to set the CU language,
7744 or we won't be able to build types correctly.
7745 Similarly, if we do not read the producer, we can not apply
7746 producer-specific interpretation. */
7747 prepare_one_comp_unit (cu, cu->dies, pretend_language);
7748
7749 reader.keep ();
7750 }
7751
7752 /* Add a DIE to the delayed physname list. */
7753
7754 static void
7755 add_to_method_list (struct type *type, int fnfield_index, int index,
7756 const char *name, struct die_info *die,
7757 struct dwarf2_cu *cu)
7758 {
7759 struct delayed_method_info mi;
7760 mi.type = type;
7761 mi.fnfield_index = fnfield_index;
7762 mi.index = index;
7763 mi.name = name;
7764 mi.die = die;
7765 cu->method_list.push_back (mi);
7766 }
7767
7768 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7769 "const" / "volatile". If so, decrements LEN by the length of the
7770 modifier and return true. Otherwise return false. */
7771
7772 template<size_t N>
7773 static bool
7774 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
7775 {
7776 size_t mod_len = sizeof (mod) - 1;
7777 if (len > mod_len && startswith (physname + (len - mod_len), mod))
7778 {
7779 len -= mod_len;
7780 return true;
7781 }
7782 return false;
7783 }
7784
7785 /* Compute the physnames of any methods on the CU's method list.
7786
7787 The computation of method physnames is delayed in order to avoid the
7788 (bad) condition that one of the method's formal parameters is of an as yet
7789 incomplete type. */
7790
7791 static void
7792 compute_delayed_physnames (struct dwarf2_cu *cu)
7793 {
7794 /* Only C++ delays computing physnames. */
7795 if (cu->method_list.empty ())
7796 return;
7797 gdb_assert (cu->per_cu->lang == language_cplus);
7798
7799 for (const delayed_method_info &mi : cu->method_list)
7800 {
7801 const char *physname;
7802 struct fn_fieldlist *fn_flp
7803 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
7804 physname = dwarf2_physname (mi.name, mi.die, cu);
7805 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
7806 = physname ? physname : "";
7807
7808 /* Since there's no tag to indicate whether a method is a
7809 const/volatile overload, extract that information out of the
7810 demangled name. */
7811 if (physname != NULL)
7812 {
7813 size_t len = strlen (physname);
7814
7815 while (1)
7816 {
7817 if (physname[len] == ')') /* shortcut */
7818 break;
7819 else if (check_modifier (physname, len, " const"))
7820 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
7821 else if (check_modifier (physname, len, " volatile"))
7822 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
7823 else
7824 break;
7825 }
7826 }
7827 }
7828
7829 /* The list is no longer needed. */
7830 cu->method_list.clear ();
7831 }
7832
7833 /* Go objects should be embedded in a DW_TAG_module DIE,
7834 and it's not clear if/how imported objects will appear.
7835 To keep Go support simple until that's worked out,
7836 go back through what we've read and create something usable.
7837 We could do this while processing each DIE, and feels kinda cleaner,
7838 but that way is more invasive.
7839 This is to, for example, allow the user to type "p var" or "b main"
7840 without having to specify the package name, and allow lookups
7841 of module.object to work in contexts that use the expression
7842 parser. */
7843
7844 static void
7845 fixup_go_packaging (struct dwarf2_cu *cu)
7846 {
7847 gdb::unique_xmalloc_ptr<char> package_name;
7848 struct pending *list;
7849 int i;
7850
7851 for (list = *cu->get_builder ()->get_global_symbols ();
7852 list != NULL;
7853 list = list->next)
7854 {
7855 for (i = 0; i < list->nsyms; ++i)
7856 {
7857 struct symbol *sym = list->symbol[i];
7858
7859 if (sym->language () == language_go
7860 && sym->aclass () == LOC_BLOCK)
7861 {
7862 gdb::unique_xmalloc_ptr<char> this_package_name
7863 (go_symbol_package_name (sym));
7864
7865 if (this_package_name == NULL)
7866 continue;
7867 if (package_name == NULL)
7868 package_name = std::move (this_package_name);
7869 else
7870 {
7871 struct objfile *objfile = cu->per_objfile->objfile;
7872 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
7873 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7874 (symbol_symtab (sym) != NULL
7875 ? symtab_to_filename_for_display
7876 (symbol_symtab (sym))
7877 : objfile_name (objfile)),
7878 this_package_name.get (), package_name.get ());
7879 }
7880 }
7881 }
7882 }
7883
7884 if (package_name != NULL)
7885 {
7886 struct objfile *objfile = cu->per_objfile->objfile;
7887 const char *saved_package_name = objfile->intern (package_name.get ());
7888 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7889 saved_package_name);
7890 struct symbol *sym;
7891
7892 sym = new (&objfile->objfile_obstack) symbol;
7893 sym->set_language (language_go, &objfile->objfile_obstack);
7894 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
7895 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7896 e.g., "main" finds the "main" module and not C's main(). */
7897 sym->set_domain (STRUCT_DOMAIN);
7898 sym->set_aclass_index (LOC_TYPEDEF);
7899 sym->set_type (type);
7900
7901 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
7902 }
7903 }
7904
7905 /* Allocate a fully-qualified name consisting of the two parts on the
7906 obstack. */
7907
7908 static const char *
7909 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
7910 {
7911 return obconcat (obstack, p1, "::", p2, (char *) NULL);
7912 }
7913
7914 /* A helper that allocates a variant part to attach to a Rust enum
7915 type. OBSTACK is where the results should be allocated. TYPE is
7916 the type we're processing. DISCRIMINANT_INDEX is the index of the
7917 discriminant. It must be the index of one of the fields of TYPE,
7918 or -1 to mean there is no discriminant (univariant enum).
7919 DEFAULT_INDEX is the index of the default field; or -1 if there is
7920 no default. RANGES is indexed by "effective" field number (the
7921 field index, but omitting the discriminant and default fields) and
7922 must hold the discriminant values used by the variants. Note that
7923 RANGES must have a lifetime at least as long as OBSTACK -- either
7924 already allocated on it, or static. */
7925
7926 static void
7927 alloc_rust_variant (struct obstack *obstack, struct type *type,
7928 int discriminant_index, int default_index,
7929 gdb::array_view<discriminant_range> ranges)
7930 {
7931 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7932 gdb_assert (discriminant_index == -1
7933 || (discriminant_index >= 0
7934 && discriminant_index < type->num_fields ()));
7935 gdb_assert (default_index == -1
7936 || (default_index >= 0 && default_index < type->num_fields ()));
7937
7938 /* We have one variant for each non-discriminant field. */
7939 int n_variants = type->num_fields ();
7940 if (discriminant_index != -1)
7941 --n_variants;
7942
7943 variant *variants = new (obstack) variant[n_variants];
7944 int var_idx = 0;
7945 int range_idx = 0;
7946 for (int i = 0; i < type->num_fields (); ++i)
7947 {
7948 if (i == discriminant_index)
7949 continue;
7950
7951 variants[var_idx].first_field = i;
7952 variants[var_idx].last_field = i + 1;
7953
7954 /* The default field does not need a range, but other fields do.
7955 We skipped the discriminant above. */
7956 if (i != default_index)
7957 {
7958 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
7959 ++range_idx;
7960 }
7961
7962 ++var_idx;
7963 }
7964
7965 gdb_assert (range_idx == ranges.size ());
7966 gdb_assert (var_idx == n_variants);
7967
7968 variant_part *part = new (obstack) variant_part;
7969 part->discriminant_index = discriminant_index;
7970 /* If there is no discriminant, then whether it is signed is of no
7971 consequence. */
7972 part->is_unsigned
7973 = (discriminant_index == -1
7974 ? false
7975 : type->field (discriminant_index).type ()->is_unsigned ());
7976 part->variants = gdb::array_view<variant> (variants, n_variants);
7977
7978 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
7979 gdb::array_view<variant_part> *prop_value
7980 = new (storage) gdb::array_view<variant_part> (part, 1);
7981
7982 struct dynamic_prop prop;
7983 prop.set_variant_parts (prop_value);
7984
7985 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
7986 }
7987
7988 /* Some versions of rustc emitted enums in an unusual way.
7989
7990 Ordinary enums were emitted as unions. The first element of each
7991 structure in the union was named "RUST$ENUM$DISR". This element
7992 held the discriminant.
7993
7994 These versions of Rust also implemented the "non-zero"
7995 optimization. When the enum had two values, and one is empty and
7996 the other holds a pointer that cannot be zero, the pointer is used
7997 as the discriminant, with a zero value meaning the empty variant.
7998 Here, the union's first member is of the form
7999 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8000 where the fieldnos are the indices of the fields that should be
8001 traversed in order to find the field (which may be several fields deep)
8002 and the variantname is the name of the variant of the case when the
8003 field is zero.
8004
8005 This function recognizes whether TYPE is of one of these forms,
8006 and, if so, smashes it to be a variant type. */
8007
8008 static void
8009 quirk_rust_enum (struct type *type, struct objfile *objfile)
8010 {
8011 gdb_assert (type->code () == TYPE_CODE_UNION);
8012
8013 /* We don't need to deal with empty enums. */
8014 if (type->num_fields () == 0)
8015 return;
8016
8017 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
8018 if (type->num_fields () == 1
8019 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
8020 {
8021 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
8022
8023 /* Decode the field name to find the offset of the
8024 discriminant. */
8025 ULONGEST bit_offset = 0;
8026 struct type *field_type = type->field (0).type ();
8027 while (name[0] >= '0' && name[0] <= '9')
8028 {
8029 char *tail;
8030 unsigned long index = strtoul (name, &tail, 10);
8031 name = tail;
8032 if (*name != '$'
8033 || index >= field_type->num_fields ()
8034 || (field_type->field (index).loc_kind ()
8035 != FIELD_LOC_KIND_BITPOS))
8036 {
8037 complaint (_("Could not parse Rust enum encoding string \"%s\""
8038 "[in module %s]"),
8039 type->field (0).name (),
8040 objfile_name (objfile));
8041 return;
8042 }
8043 ++name;
8044
8045 bit_offset += field_type->field (index).loc_bitpos ();
8046 field_type = field_type->field (index).type ();
8047 }
8048
8049 /* Smash this type to be a structure type. We have to do this
8050 because the type has already been recorded. */
8051 type->set_code (TYPE_CODE_STRUCT);
8052 type->set_num_fields (3);
8053 /* Save the field we care about. */
8054 struct field saved_field = type->field (0);
8055 type->set_fields
8056 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8057
8058 /* Put the discriminant at index 0. */
8059 type->field (0).set_type (field_type);
8060 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8061 type->field (0).set_name ("<<discriminant>>");
8062 type->field (0).set_loc_bitpos (bit_offset);
8063
8064 /* The order of fields doesn't really matter, so put the real
8065 field at index 1 and the data-less field at index 2. */
8066 type->field (1) = saved_field;
8067 type->field (1).set_name
8068 (rust_last_path_segment (type->field (1).type ()->name ()));
8069 type->field (1).type ()->set_name
8070 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8071 type->field (1).name ()));
8072
8073 const char *dataless_name
8074 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8075 name);
8076 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8077 dataless_name);
8078 type->field (2).set_type (dataless_type);
8079 /* NAME points into the original discriminant name, which
8080 already has the correct lifetime. */
8081 type->field (2).set_name (name);
8082 type->field (2).set_loc_bitpos (0);
8083
8084 /* Indicate that this is a variant type. */
8085 static discriminant_range ranges[1] = { { 0, 0 } };
8086 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8087 }
8088 /* A union with a single anonymous field is probably an old-style
8089 univariant enum. */
8090 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
8091 {
8092 /* Smash this type to be a structure type. We have to do this
8093 because the type has already been recorded. */
8094 type->set_code (TYPE_CODE_STRUCT);
8095
8096 struct type *field_type = type->field (0).type ();
8097 const char *variant_name
8098 = rust_last_path_segment (field_type->name ());
8099 type->field (0).set_name (variant_name);
8100 field_type->set_name
8101 (rust_fully_qualify (&objfile->objfile_obstack,
8102 type->name (), variant_name));
8103
8104 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
8105 }
8106 else
8107 {
8108 struct type *disr_type = nullptr;
8109 for (int i = 0; i < type->num_fields (); ++i)
8110 {
8111 disr_type = type->field (i).type ();
8112
8113 if (disr_type->code () != TYPE_CODE_STRUCT)
8114 {
8115 /* All fields of a true enum will be structs. */
8116 return;
8117 }
8118 else if (disr_type->num_fields () == 0)
8119 {
8120 /* Could be data-less variant, so keep going. */
8121 disr_type = nullptr;
8122 }
8123 else if (strcmp (disr_type->field (0).name (),
8124 "RUST$ENUM$DISR") != 0)
8125 {
8126 /* Not a Rust enum. */
8127 return;
8128 }
8129 else
8130 {
8131 /* Found one. */
8132 break;
8133 }
8134 }
8135
8136 /* If we got here without a discriminant, then it's probably
8137 just a union. */
8138 if (disr_type == nullptr)
8139 return;
8140
8141 /* Smash this type to be a structure type. We have to do this
8142 because the type has already been recorded. */
8143 type->set_code (TYPE_CODE_STRUCT);
8144
8145 /* Make space for the discriminant field. */
8146 struct field *disr_field = &disr_type->field (0);
8147 field *new_fields
8148 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
8149 * sizeof (struct field)));
8150 memcpy (new_fields + 1, type->fields (),
8151 type->num_fields () * sizeof (struct field));
8152 type->set_fields (new_fields);
8153 type->set_num_fields (type->num_fields () + 1);
8154
8155 /* Install the discriminant at index 0 in the union. */
8156 type->field (0) = *disr_field;
8157 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8158 type->field (0).set_name ("<<discriminant>>");
8159
8160 /* We need a way to find the correct discriminant given a
8161 variant name. For convenience we build a map here. */
8162 struct type *enum_type = disr_field->type ();
8163 std::unordered_map<std::string, ULONGEST> discriminant_map;
8164 for (int i = 0; i < enum_type->num_fields (); ++i)
8165 {
8166 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
8167 {
8168 const char *name
8169 = rust_last_path_segment (enum_type->field (i).name ());
8170 discriminant_map[name] = enum_type->field (i).loc_enumval ();
8171 }
8172 }
8173
8174 int n_fields = type->num_fields ();
8175 /* We don't need a range entry for the discriminant, but we do
8176 need one for every other field, as there is no default
8177 variant. */
8178 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
8179 discriminant_range,
8180 n_fields - 1);
8181 /* Skip the discriminant here. */
8182 for (int i = 1; i < n_fields; ++i)
8183 {
8184 /* Find the final word in the name of this variant's type.
8185 That name can be used to look up the correct
8186 discriminant. */
8187 const char *variant_name
8188 = rust_last_path_segment (type->field (i).type ()->name ());
8189
8190 auto iter = discriminant_map.find (variant_name);
8191 if (iter != discriminant_map.end ())
8192 {
8193 ranges[i - 1].low = iter->second;
8194 ranges[i - 1].high = iter->second;
8195 }
8196
8197 /* In Rust, each element should have the size of the
8198 enclosing enum. */
8199 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
8200
8201 /* Remove the discriminant field, if it exists. */
8202 struct type *sub_type = type->field (i).type ();
8203 if (sub_type->num_fields () > 0)
8204 {
8205 sub_type->set_num_fields (sub_type->num_fields () - 1);
8206 sub_type->set_fields (sub_type->fields () + 1);
8207 }
8208 type->field (i).set_name (variant_name);
8209 sub_type->set_name
8210 (rust_fully_qualify (&objfile->objfile_obstack,
8211 type->name (), variant_name));
8212 }
8213
8214 /* Indicate that this is a variant type. */
8215 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
8216 gdb::array_view<discriminant_range> (ranges,
8217 n_fields - 1));
8218 }
8219 }
8220
8221 /* Rewrite some Rust unions to be structures with variants parts. */
8222
8223 static void
8224 rust_union_quirks (struct dwarf2_cu *cu)
8225 {
8226 gdb_assert (cu->per_cu->lang == language_rust);
8227 for (type *type_ : cu->rust_unions)
8228 quirk_rust_enum (type_, cu->per_objfile->objfile);
8229 /* We don't need this any more. */
8230 cu->rust_unions.clear ();
8231 }
8232
8233 /* See read.h. */
8234
8235 type_unit_group_unshareable *
8236 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
8237 {
8238 auto iter = this->m_type_units.find (tu_group);
8239 if (iter != this->m_type_units.end ())
8240 return iter->second.get ();
8241
8242 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
8243 type_unit_group_unshareable *result = uniq.get ();
8244 this->m_type_units[tu_group] = std::move (uniq);
8245 return result;
8246 }
8247
8248 struct type *
8249 dwarf2_per_objfile::get_type_for_signatured_type
8250 (signatured_type *sig_type) const
8251 {
8252 auto iter = this->m_type_map.find (sig_type);
8253 if (iter == this->m_type_map.end ())
8254 return nullptr;
8255
8256 return iter->second;
8257 }
8258
8259 void dwarf2_per_objfile::set_type_for_signatured_type
8260 (signatured_type *sig_type, struct type *type)
8261 {
8262 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
8263
8264 this->m_type_map[sig_type] = type;
8265 }
8266
8267 /* A helper function for computing the list of all symbol tables
8268 included by PER_CU. */
8269
8270 static void
8271 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
8272 htab_t all_children, htab_t all_type_symtabs,
8273 dwarf2_per_cu_data *per_cu,
8274 dwarf2_per_objfile *per_objfile,
8275 struct compunit_symtab *immediate_parent)
8276 {
8277 void **slot = htab_find_slot (all_children, per_cu, INSERT);
8278 if (*slot != NULL)
8279 {
8280 /* This inclusion and its children have been processed. */
8281 return;
8282 }
8283
8284 *slot = per_cu;
8285
8286 /* Only add a CU if it has a symbol table. */
8287 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8288 if (cust != NULL)
8289 {
8290 /* If this is a type unit only add its symbol table if we haven't
8291 seen it yet (type unit per_cu's can share symtabs). */
8292 if (per_cu->is_debug_types)
8293 {
8294 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8295 if (*slot == NULL)
8296 {
8297 *slot = cust;
8298 result->push_back (cust);
8299 if (cust->user == NULL)
8300 cust->user = immediate_parent;
8301 }
8302 }
8303 else
8304 {
8305 result->push_back (cust);
8306 if (cust->user == NULL)
8307 cust->user = immediate_parent;
8308 }
8309 }
8310
8311 if (!per_cu->imported_symtabs_empty ())
8312 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8313 {
8314 recursively_compute_inclusions (result, all_children,
8315 all_type_symtabs, ptr, per_objfile,
8316 cust);
8317 }
8318 }
8319
8320 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8321 PER_CU. */
8322
8323 static void
8324 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
8325 dwarf2_per_objfile *per_objfile)
8326 {
8327 gdb_assert (! per_cu->is_debug_types);
8328
8329 if (!per_cu->imported_symtabs_empty ())
8330 {
8331 int len;
8332 std::vector<compunit_symtab *> result_symtabs;
8333 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8334
8335 /* If we don't have a symtab, we can just skip this case. */
8336 if (cust == NULL)
8337 return;
8338
8339 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
8340 htab_eq_pointer,
8341 NULL, xcalloc, xfree));
8342 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
8343 htab_eq_pointer,
8344 NULL, xcalloc, xfree));
8345
8346 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8347 {
8348 recursively_compute_inclusions (&result_symtabs, all_children.get (),
8349 all_type_symtabs.get (), ptr,
8350 per_objfile, cust);
8351 }
8352
8353 /* Now we have a transitive closure of all the included symtabs. */
8354 len = result_symtabs.size ();
8355 cust->includes
8356 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8357 struct compunit_symtab *, len + 1);
8358 memcpy (cust->includes, result_symtabs.data (),
8359 len * sizeof (compunit_symtab *));
8360 cust->includes[len] = NULL;
8361 }
8362 }
8363
8364 /* Compute the 'includes' field for the symtabs of all the CUs we just
8365 read. */
8366
8367 static void
8368 process_cu_includes (dwarf2_per_objfile *per_objfile)
8369 {
8370 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
8371 {
8372 if (! iter->is_debug_types)
8373 compute_compunit_symtab_includes (iter, per_objfile);
8374 }
8375
8376 per_objfile->per_bfd->just_read_cus.clear ();
8377 }
8378
8379 /* Generate full symbol information for CU, whose DIEs have
8380 already been loaded into memory. */
8381
8382 static void
8383 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
8384 {
8385 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8386 struct objfile *objfile = per_objfile->objfile;
8387 struct gdbarch *gdbarch = objfile->arch ();
8388 CORE_ADDR lowpc, highpc;
8389 struct compunit_symtab *cust;
8390 CORE_ADDR baseaddr;
8391 struct block *static_block;
8392 CORE_ADDR addr;
8393
8394 baseaddr = objfile->text_section_offset ();
8395
8396 /* Clear the list here in case something was left over. */
8397 cu->method_list.clear ();
8398
8399 dwarf2_find_base_address (cu->dies, cu);
8400
8401 /* Before we start reading the top-level DIE, ensure it has a valid tag
8402 type. */
8403 switch (cu->dies->tag)
8404 {
8405 case DW_TAG_compile_unit:
8406 case DW_TAG_partial_unit:
8407 case DW_TAG_type_unit:
8408 break;
8409 default:
8410 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8411 dwarf_tag_name (cu->dies->tag),
8412 sect_offset_str (cu->per_cu->sect_off),
8413 objfile_name (per_objfile->objfile));
8414 }
8415
8416 /* Do line number decoding in read_file_scope () */
8417 process_die (cu->dies, cu);
8418
8419 /* For now fudge the Go package. */
8420 if (cu->per_cu->lang == language_go)
8421 fixup_go_packaging (cu);
8422
8423 /* Now that we have processed all the DIEs in the CU, all the types
8424 should be complete, and it should now be safe to compute all of the
8425 physnames. */
8426 compute_delayed_physnames (cu);
8427
8428 if (cu->per_cu->lang == language_rust)
8429 rust_union_quirks (cu);
8430
8431 /* Some compilers don't define a DW_AT_high_pc attribute for the
8432 compilation unit. If the DW_AT_high_pc is missing, synthesize
8433 it, by scanning the DIE's below the compilation unit. */
8434 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8435
8436 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8437 static_block
8438 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
8439
8440 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8441 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8442 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8443 addrmap to help ensure it has an accurate map of pc values belonging to
8444 this comp unit. */
8445 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8446
8447 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
8448 (static_block, SECT_OFF_TEXT (objfile), 0);
8449
8450 if (cust != NULL)
8451 {
8452 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8453
8454 /* Set symtab language to language from DW_AT_language. If the
8455 compilation is from a C file generated by language preprocessors, do
8456 not set the language if it was already deduced by start_subfile. */
8457 if (!(cu->per_cu->lang == language_c
8458 && cust->primary_filetab ()->language () != language_unknown))
8459 cust->primary_filetab ()->set_language (cu->per_cu->lang);
8460
8461 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8462 produce DW_AT_location with location lists but it can be possibly
8463 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8464 there were bugs in prologue debug info, fixed later in GCC-4.5
8465 by "unwind info for epilogues" patch (which is not directly related).
8466
8467 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8468 needed, it would be wrong due to missing DW_AT_producer there.
8469
8470 Still one can confuse GDB by using non-standard GCC compilation
8471 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8472 */
8473 if (cu->has_loclist && gcc_4_minor >= 5)
8474 cust->set_locations_valid (true);
8475
8476 if (gcc_4_minor >= 5)
8477 cust->set_epilogue_unwind_valid (true);
8478
8479 cust->set_call_site_htab (cu->call_site_htab);
8480 }
8481
8482 per_objfile->set_symtab (cu->per_cu, cust);
8483
8484 /* Push it for inclusion processing later. */
8485 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
8486
8487 /* Not needed any more. */
8488 cu->reset_builder ();
8489 }
8490
8491 /* Generate full symbol information for type unit CU, whose DIEs have
8492 already been loaded into memory. */
8493
8494 static void
8495 process_full_type_unit (dwarf2_cu *cu,
8496 enum language pretend_language)
8497 {
8498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8499 struct objfile *objfile = per_objfile->objfile;
8500 struct compunit_symtab *cust;
8501 struct signatured_type *sig_type;
8502
8503 gdb_assert (cu->per_cu->is_debug_types);
8504 sig_type = (struct signatured_type *) cu->per_cu;
8505
8506 /* Clear the list here in case something was left over. */
8507 cu->method_list.clear ();
8508
8509 /* The symbol tables are set up in read_type_unit_scope. */
8510 process_die (cu->dies, cu);
8511
8512 /* For now fudge the Go package. */
8513 if (cu->per_cu->lang == language_go)
8514 fixup_go_packaging (cu);
8515
8516 /* Now that we have processed all the DIEs in the CU, all the types
8517 should be complete, and it should now be safe to compute all of the
8518 physnames. */
8519 compute_delayed_physnames (cu);
8520
8521 if (cu->per_cu->lang == language_rust)
8522 rust_union_quirks (cu);
8523
8524 /* TUs share symbol tables.
8525 If this is the first TU to use this symtab, complete the construction
8526 of it with end_expandable_symtab. Otherwise, complete the addition of
8527 this TU's symbols to the existing symtab. */
8528 type_unit_group_unshareable *tug_unshare =
8529 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8530 if (tug_unshare->compunit_symtab == NULL)
8531 {
8532 buildsym_compunit *builder = cu->get_builder ();
8533 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8534 tug_unshare->compunit_symtab = cust;
8535
8536 if (cust != NULL)
8537 {
8538 /* Set symtab language to language from DW_AT_language. If the
8539 compilation is from a C file generated by language preprocessors,
8540 do not set the language if it was already deduced by
8541 start_subfile. */
8542 if (!(cu->per_cu->lang == language_c
8543 && cust->primary_filetab ()->language () != language_c))
8544 cust->primary_filetab ()->set_language (cu->per_cu->lang);
8545 }
8546 }
8547 else
8548 {
8549 cu->get_builder ()->augment_type_symtab ();
8550 cust = tug_unshare->compunit_symtab;
8551 }
8552
8553 per_objfile->set_symtab (cu->per_cu, cust);
8554
8555 /* Not needed any more. */
8556 cu->reset_builder ();
8557 }
8558
8559 /* Process an imported unit DIE. */
8560
8561 static void
8562 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8563 {
8564 struct attribute *attr;
8565
8566 /* For now we don't handle imported units in type units. */
8567 if (cu->per_cu->is_debug_types)
8568 {
8569 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8570 " supported in type units [in module %s]"),
8571 objfile_name (cu->per_objfile->objfile));
8572 }
8573
8574 attr = dwarf2_attr (die, DW_AT_import, cu);
8575 if (attr != NULL)
8576 {
8577 sect_offset sect_off = attr->get_ref_die_offset ();
8578 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8579 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8580 dwarf2_per_cu_data *per_cu
8581 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
8582 per_objfile->per_bfd);
8583
8584 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8585 into another compilation unit, at root level. Regard this as a hint,
8586 and ignore it. */
8587 if (die->parent && die->parent->parent == NULL
8588 && per_cu->unit_type == DW_UT_compile
8589 && per_cu->lang == language_cplus)
8590 return;
8591
8592 /* If necessary, add it to the queue and load its DIEs. */
8593 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
8594 cu->per_cu->lang))
8595 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
8596 false, cu->per_cu->lang);
8597
8598 cu->per_cu->imported_symtabs_push (per_cu);
8599 }
8600 }
8601
8602 /* RAII object that represents a process_die scope: i.e.,
8603 starts/finishes processing a DIE. */
8604 class process_die_scope
8605 {
8606 public:
8607 process_die_scope (die_info *die, dwarf2_cu *cu)
8608 : m_die (die), m_cu (cu)
8609 {
8610 /* We should only be processing DIEs not already in process. */
8611 gdb_assert (!m_die->in_process);
8612 m_die->in_process = true;
8613 }
8614
8615 ~process_die_scope ()
8616 {
8617 m_die->in_process = false;
8618
8619 /* If we're done processing the DIE for the CU that owns the line
8620 header, we don't need the line header anymore. */
8621 if (m_cu->line_header_die_owner == m_die)
8622 {
8623 delete m_cu->line_header;
8624 m_cu->line_header = NULL;
8625 m_cu->line_header_die_owner = NULL;
8626 }
8627 }
8628
8629 private:
8630 die_info *m_die;
8631 dwarf2_cu *m_cu;
8632 };
8633
8634 /* Process a die and its children. */
8635
8636 static void
8637 process_die (struct die_info *die, struct dwarf2_cu *cu)
8638 {
8639 process_die_scope scope (die, cu);
8640
8641 switch (die->tag)
8642 {
8643 case DW_TAG_padding:
8644 break;
8645 case DW_TAG_compile_unit:
8646 case DW_TAG_partial_unit:
8647 read_file_scope (die, cu);
8648 break;
8649 case DW_TAG_type_unit:
8650 read_type_unit_scope (die, cu);
8651 break;
8652 case DW_TAG_subprogram:
8653 /* Nested subprograms in Fortran get a prefix. */
8654 if (cu->per_cu->lang == language_fortran
8655 && die->parent != NULL
8656 && die->parent->tag == DW_TAG_subprogram)
8657 cu->processing_has_namespace_info = true;
8658 /* Fall through. */
8659 case DW_TAG_inlined_subroutine:
8660 read_func_scope (die, cu);
8661 break;
8662 case DW_TAG_lexical_block:
8663 case DW_TAG_try_block:
8664 case DW_TAG_catch_block:
8665 read_lexical_block_scope (die, cu);
8666 break;
8667 case DW_TAG_call_site:
8668 case DW_TAG_GNU_call_site:
8669 read_call_site_scope (die, cu);
8670 break;
8671 case DW_TAG_class_type:
8672 case DW_TAG_interface_type:
8673 case DW_TAG_structure_type:
8674 case DW_TAG_union_type:
8675 case DW_TAG_namelist:
8676 process_structure_scope (die, cu);
8677 break;
8678 case DW_TAG_enumeration_type:
8679 process_enumeration_scope (die, cu);
8680 break;
8681
8682 /* These dies have a type, but processing them does not create
8683 a symbol or recurse to process the children. Therefore we can
8684 read them on-demand through read_type_die. */
8685 case DW_TAG_subroutine_type:
8686 case DW_TAG_set_type:
8687 case DW_TAG_pointer_type:
8688 case DW_TAG_ptr_to_member_type:
8689 case DW_TAG_reference_type:
8690 case DW_TAG_rvalue_reference_type:
8691 case DW_TAG_string_type:
8692 break;
8693
8694 case DW_TAG_array_type:
8695 /* We only need to handle this case for Ada -- in other
8696 languages, it's normal for the compiler to emit a typedef
8697 instead. */
8698 if (cu->per_cu->lang != language_ada)
8699 break;
8700 /* FALLTHROUGH */
8701 case DW_TAG_base_type:
8702 case DW_TAG_subrange_type:
8703 case DW_TAG_generic_subrange:
8704 case DW_TAG_typedef:
8705 /* Add a typedef symbol for the type definition, if it has a
8706 DW_AT_name. */
8707 new_symbol (die, read_type_die (die, cu), cu);
8708 break;
8709 case DW_TAG_common_block:
8710 read_common_block (die, cu);
8711 break;
8712 case DW_TAG_common_inclusion:
8713 break;
8714 case DW_TAG_namespace:
8715 cu->processing_has_namespace_info = true;
8716 read_namespace (die, cu);
8717 break;
8718 case DW_TAG_module:
8719 cu->processing_has_namespace_info = true;
8720 read_module (die, cu);
8721 break;
8722 case DW_TAG_imported_declaration:
8723 cu->processing_has_namespace_info = true;
8724 if (read_namespace_alias (die, cu))
8725 break;
8726 /* The declaration is not a global namespace alias. */
8727 /* Fall through. */
8728 case DW_TAG_imported_module:
8729 cu->processing_has_namespace_info = true;
8730 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8731 || cu->per_cu->lang != language_fortran))
8732 complaint (_("Tag '%s' has unexpected children"),
8733 dwarf_tag_name (die->tag));
8734 read_import_statement (die, cu);
8735 break;
8736
8737 case DW_TAG_imported_unit:
8738 process_imported_unit_die (die, cu);
8739 break;
8740
8741 case DW_TAG_variable:
8742 read_variable (die, cu);
8743 break;
8744
8745 default:
8746 new_symbol (die, NULL, cu);
8747 break;
8748 }
8749 }
8750 \f
8751 /* DWARF name computation. */
8752
8753 /* A helper function for dwarf2_compute_name which determines whether DIE
8754 needs to have the name of the scope prepended to the name listed in the
8755 die. */
8756
8757 static int
8758 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8759 {
8760 struct attribute *attr;
8761
8762 switch (die->tag)
8763 {
8764 case DW_TAG_namespace:
8765 case DW_TAG_typedef:
8766 case DW_TAG_class_type:
8767 case DW_TAG_interface_type:
8768 case DW_TAG_structure_type:
8769 case DW_TAG_union_type:
8770 case DW_TAG_enumeration_type:
8771 case DW_TAG_enumerator:
8772 case DW_TAG_subprogram:
8773 case DW_TAG_inlined_subroutine:
8774 case DW_TAG_member:
8775 case DW_TAG_imported_declaration:
8776 return 1;
8777
8778 case DW_TAG_variable:
8779 case DW_TAG_constant:
8780 /* We only need to prefix "globally" visible variables. These include
8781 any variable marked with DW_AT_external or any variable that
8782 lives in a namespace. [Variables in anonymous namespaces
8783 require prefixing, but they are not DW_AT_external.] */
8784
8785 if (dwarf2_attr (die, DW_AT_specification, cu))
8786 {
8787 struct dwarf2_cu *spec_cu = cu;
8788
8789 return die_needs_namespace (die_specification (die, &spec_cu),
8790 spec_cu);
8791 }
8792
8793 attr = dwarf2_attr (die, DW_AT_external, cu);
8794 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8795 && die->parent->tag != DW_TAG_module)
8796 return 0;
8797 /* A variable in a lexical block of some kind does not need a
8798 namespace, even though in C++ such variables may be external
8799 and have a mangled name. */
8800 if (die->parent->tag == DW_TAG_lexical_block
8801 || die->parent->tag == DW_TAG_try_block
8802 || die->parent->tag == DW_TAG_catch_block
8803 || die->parent->tag == DW_TAG_subprogram)
8804 return 0;
8805 return 1;
8806
8807 default:
8808 return 0;
8809 }
8810 }
8811
8812 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8813 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8814 defined for the given DIE. */
8815
8816 static struct attribute *
8817 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8818 {
8819 struct attribute *attr;
8820
8821 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8822 if (attr == NULL)
8823 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8824
8825 return attr;
8826 }
8827
8828 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8829 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8830 defined for the given DIE. */
8831
8832 static const char *
8833 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8834 {
8835 const char *linkage_name;
8836
8837 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8838 if (linkage_name == NULL)
8839 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8840
8841 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8842 See https://github.com/rust-lang/rust/issues/32925. */
8843 if (cu->per_cu->lang == language_rust && linkage_name != NULL
8844 && strchr (linkage_name, '{') != NULL)
8845 linkage_name = NULL;
8846
8847 return linkage_name;
8848 }
8849
8850 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8851 compute the physname for the object, which include a method's:
8852 - formal parameters (C++),
8853 - receiver type (Go),
8854
8855 The term "physname" is a bit confusing.
8856 For C++, for example, it is the demangled name.
8857 For Go, for example, it's the mangled name.
8858
8859 For Ada, return the DIE's linkage name rather than the fully qualified
8860 name. PHYSNAME is ignored..
8861
8862 The result is allocated on the objfile->per_bfd's obstack and
8863 canonicalized. */
8864
8865 static const char *
8866 dwarf2_compute_name (const char *name,
8867 struct die_info *die, struct dwarf2_cu *cu,
8868 int physname)
8869 {
8870 struct objfile *objfile = cu->per_objfile->objfile;
8871
8872 if (name == NULL)
8873 name = dwarf2_name (die, cu);
8874
8875 enum language lang = cu->per_cu->lang;
8876
8877 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8878 but otherwise compute it by typename_concat inside GDB.
8879 FIXME: Actually this is not really true, or at least not always true.
8880 It's all very confusing. compute_and_set_names doesn't try to demangle
8881 Fortran names because there is no mangling standard. So new_symbol
8882 will set the demangled name to the result of dwarf2_full_name, and it is
8883 the demangled name that GDB uses if it exists. */
8884 if (lang == language_ada
8885 || (lang == language_fortran && physname))
8886 {
8887 /* For Ada unit, we prefer the linkage name over the name, as
8888 the former contains the exported name, which the user expects
8889 to be able to reference. Ideally, we want the user to be able
8890 to reference this entity using either natural or linkage name,
8891 but we haven't started looking at this enhancement yet. */
8892 const char *linkage_name = dw2_linkage_name (die, cu);
8893
8894 if (linkage_name != NULL)
8895 return linkage_name;
8896 }
8897
8898 /* These are the only languages we know how to qualify names in. */
8899 if (name != NULL
8900 && (lang == language_cplus
8901 || lang == language_fortran || lang == language_d
8902 || lang == language_rust))
8903 {
8904 if (die_needs_namespace (die, cu))
8905 {
8906 const char *prefix;
8907 const char *canonical_name = NULL;
8908
8909 string_file buf;
8910
8911 prefix = determine_prefix (die, cu);
8912 if (*prefix != '\0')
8913 {
8914 gdb::unique_xmalloc_ptr<char> prefixed_name
8915 (typename_concat (NULL, prefix, name, physname, cu));
8916
8917 buf.puts (prefixed_name.get ());
8918 }
8919 else
8920 buf.puts (name);
8921
8922 /* Template parameters may be specified in the DIE's DW_AT_name, or
8923 as children with DW_TAG_template_type_param or
8924 DW_TAG_value_type_param. If the latter, add them to the name
8925 here. If the name already has template parameters, then
8926 skip this step; some versions of GCC emit both, and
8927 it is more efficient to use the pre-computed name.
8928
8929 Something to keep in mind about this process: it is very
8930 unlikely, or in some cases downright impossible, to produce
8931 something that will match the mangled name of a function.
8932 If the definition of the function has the same debug info,
8933 we should be able to match up with it anyway. But fallbacks
8934 using the minimal symbol, for instance to find a method
8935 implemented in a stripped copy of libstdc++, will not work.
8936 If we do not have debug info for the definition, we will have to
8937 match them up some other way.
8938
8939 When we do name matching there is a related problem with function
8940 templates; two instantiated function templates are allowed to
8941 differ only by their return types, which we do not add here. */
8942
8943 if (lang == language_cplus && strchr (name, '<') == NULL)
8944 {
8945 struct attribute *attr;
8946 struct die_info *child;
8947 int first = 1;
8948
8949 die->building_fullname = 1;
8950
8951 for (child = die->child; child != NULL; child = child->sibling)
8952 {
8953 struct type *type;
8954 LONGEST value;
8955 const gdb_byte *bytes;
8956 struct dwarf2_locexpr_baton *baton;
8957 struct value *v;
8958
8959 if (child->tag != DW_TAG_template_type_param
8960 && child->tag != DW_TAG_template_value_param)
8961 continue;
8962
8963 if (first)
8964 {
8965 buf.puts ("<");
8966 first = 0;
8967 }
8968 else
8969 buf.puts (", ");
8970
8971 attr = dwarf2_attr (child, DW_AT_type, cu);
8972 if (attr == NULL)
8973 {
8974 complaint (_("template parameter missing DW_AT_type"));
8975 buf.puts ("UNKNOWN_TYPE");
8976 continue;
8977 }
8978 type = die_type (child, cu);
8979
8980 if (child->tag == DW_TAG_template_type_param)
8981 {
8982 cu->language_defn->print_type (type, "", &buf, -1, 0,
8983 &type_print_raw_options);
8984 continue;
8985 }
8986
8987 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8988 if (attr == NULL)
8989 {
8990 complaint (_("template parameter missing "
8991 "DW_AT_const_value"));
8992 buf.puts ("UNKNOWN_VALUE");
8993 continue;
8994 }
8995
8996 dwarf2_const_value_attr (attr, type, name,
8997 &cu->comp_unit_obstack, cu,
8998 &value, &bytes, &baton);
8999
9000 if (type->has_no_signedness ())
9001 /* GDB prints characters as NUMBER 'CHAR'. If that's
9002 changed, this can use value_print instead. */
9003 cu->language_defn->printchar (value, type, &buf);
9004 else
9005 {
9006 struct value_print_options opts;
9007
9008 if (baton != NULL)
9009 v = dwarf2_evaluate_loc_desc (type, NULL,
9010 baton->data,
9011 baton->size,
9012 baton->per_cu,
9013 baton->per_objfile);
9014 else if (bytes != NULL)
9015 {
9016 v = allocate_value (type);
9017 memcpy (value_contents_writeable (v).data (), bytes,
9018 TYPE_LENGTH (type));
9019 }
9020 else
9021 v = value_from_longest (type, value);
9022
9023 /* Specify decimal so that we do not depend on
9024 the radix. */
9025 get_formatted_print_options (&opts, 'd');
9026 opts.raw = 1;
9027 value_print (v, &buf, &opts);
9028 release_value (v);
9029 }
9030 }
9031
9032 die->building_fullname = 0;
9033
9034 if (!first)
9035 {
9036 /* Close the argument list, with a space if necessary
9037 (nested templates). */
9038 if (!buf.empty () && buf.string ().back () == '>')
9039 buf.puts (" >");
9040 else
9041 buf.puts (">");
9042 }
9043 }
9044
9045 /* For C++ methods, append formal parameter type
9046 information, if PHYSNAME. */
9047
9048 if (physname && die->tag == DW_TAG_subprogram
9049 && lang == language_cplus)
9050 {
9051 struct type *type = read_type_die (die, cu);
9052
9053 c_type_print_args (type, &buf, 1, lang,
9054 &type_print_raw_options);
9055
9056 if (lang == language_cplus)
9057 {
9058 /* Assume that an artificial first parameter is
9059 "this", but do not crash if it is not. RealView
9060 marks unnamed (and thus unused) parameters as
9061 artificial; there is no way to differentiate
9062 the two cases. */
9063 if (type->num_fields () > 0
9064 && TYPE_FIELD_ARTIFICIAL (type, 0)
9065 && type->field (0).type ()->code () == TYPE_CODE_PTR
9066 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
9067 buf.puts (" const");
9068 }
9069 }
9070
9071 const std::string &intermediate_name = buf.string ();
9072
9073 if (lang == language_cplus)
9074 canonical_name
9075 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9076 objfile);
9077
9078 /* If we only computed INTERMEDIATE_NAME, or if
9079 INTERMEDIATE_NAME is already canonical, then we need to
9080 intern it. */
9081 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9082 name = objfile->intern (intermediate_name);
9083 else
9084 name = canonical_name;
9085 }
9086 }
9087
9088 return name;
9089 }
9090
9091 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9092 If scope qualifiers are appropriate they will be added. The result
9093 will be allocated on the storage_obstack, or NULL if the DIE does
9094 not have a name. NAME may either be from a previous call to
9095 dwarf2_name or NULL.
9096
9097 The output string will be canonicalized (if C++). */
9098
9099 static const char *
9100 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9101 {
9102 return dwarf2_compute_name (name, die, cu, 0);
9103 }
9104
9105 /* Construct a physname for the given DIE in CU. NAME may either be
9106 from a previous call to dwarf2_name or NULL. The result will be
9107 allocated on the objfile_objstack or NULL if the DIE does not have a
9108 name.
9109
9110 The output string will be canonicalized (if C++). */
9111
9112 static const char *
9113 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9114 {
9115 struct objfile *objfile = cu->per_objfile->objfile;
9116 const char *retval, *mangled = NULL, *canon = NULL;
9117 int need_copy = 1;
9118
9119 /* In this case dwarf2_compute_name is just a shortcut not building anything
9120 on its own. */
9121 if (!die_needs_namespace (die, cu))
9122 return dwarf2_compute_name (name, die, cu, 1);
9123
9124 if (cu->per_cu->lang != language_rust)
9125 mangled = dw2_linkage_name (die, cu);
9126
9127 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9128 has computed. */
9129 gdb::unique_xmalloc_ptr<char> demangled;
9130 if (mangled != NULL)
9131 {
9132 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
9133 {
9134 /* Do nothing (do not demangle the symbol name). */
9135 }
9136 else
9137 {
9138 /* Use DMGL_RET_DROP for C++ template functions to suppress
9139 their return type. It is easier for GDB users to search
9140 for such functions as `name(params)' than `long name(params)'.
9141 In such case the minimal symbol names do not match the full
9142 symbol names but for template functions there is never a need
9143 to look up their definition from their declaration so
9144 the only disadvantage remains the minimal symbol variant
9145 `long name(params)' does not have the proper inferior type. */
9146 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
9147 | DMGL_RET_DROP));
9148 }
9149 if (demangled)
9150 canon = demangled.get ();
9151 else
9152 {
9153 canon = mangled;
9154 need_copy = 0;
9155 }
9156 }
9157
9158 if (canon == NULL || check_physname)
9159 {
9160 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9161
9162 if (canon != NULL && strcmp (physname, canon) != 0)
9163 {
9164 /* It may not mean a bug in GDB. The compiler could also
9165 compute DW_AT_linkage_name incorrectly. But in such case
9166 GDB would need to be bug-to-bug compatible. */
9167
9168 complaint (_("Computed physname <%s> does not match demangled <%s> "
9169 "(from linkage <%s>) - DIE at %s [in module %s]"),
9170 physname, canon, mangled, sect_offset_str (die->sect_off),
9171 objfile_name (objfile));
9172
9173 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9174 is available here - over computed PHYSNAME. It is safer
9175 against both buggy GDB and buggy compilers. */
9176
9177 retval = canon;
9178 }
9179 else
9180 {
9181 retval = physname;
9182 need_copy = 0;
9183 }
9184 }
9185 else
9186 retval = canon;
9187
9188 if (need_copy)
9189 retval = objfile->intern (retval);
9190
9191 return retval;
9192 }
9193
9194 /* Inspect DIE in CU for a namespace alias. If one exists, record
9195 a new symbol for it.
9196
9197 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9198
9199 static int
9200 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
9201 {
9202 struct attribute *attr;
9203
9204 /* If the die does not have a name, this is not a namespace
9205 alias. */
9206 attr = dwarf2_attr (die, DW_AT_name, cu);
9207 if (attr != NULL)
9208 {
9209 int num;
9210 struct die_info *d = die;
9211 struct dwarf2_cu *imported_cu = cu;
9212
9213 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9214 keep inspecting DIEs until we hit the underlying import. */
9215 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9216 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9217 {
9218 attr = dwarf2_attr (d, DW_AT_import, cu);
9219 if (attr == NULL)
9220 break;
9221
9222 d = follow_die_ref (d, attr, &imported_cu);
9223 if (d->tag != DW_TAG_imported_declaration)
9224 break;
9225 }
9226
9227 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9228 {
9229 complaint (_("DIE at %s has too many recursively imported "
9230 "declarations"), sect_offset_str (d->sect_off));
9231 return 0;
9232 }
9233
9234 if (attr != NULL)
9235 {
9236 struct type *type;
9237 sect_offset sect_off = attr->get_ref_die_offset ();
9238
9239 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
9240 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
9241 {
9242 /* This declaration is a global namespace alias. Add
9243 a symbol for it whose type is the aliased namespace. */
9244 new_symbol (die, type, cu);
9245 return 1;
9246 }
9247 }
9248 }
9249
9250 return 0;
9251 }
9252
9253 /* Return the using directives repository (global or local?) to use in the
9254 current context for CU.
9255
9256 For Ada, imported declarations can materialize renamings, which *may* be
9257 global. However it is impossible (for now?) in DWARF to distinguish
9258 "external" imported declarations and "static" ones. As all imported
9259 declarations seem to be static in all other languages, make them all CU-wide
9260 global only in Ada. */
9261
9262 static struct using_direct **
9263 using_directives (struct dwarf2_cu *cu)
9264 {
9265 if (cu->per_cu->lang == language_ada
9266 && cu->get_builder ()->outermost_context_p ())
9267 return cu->get_builder ()->get_global_using_directives ();
9268 else
9269 return cu->get_builder ()->get_local_using_directives ();
9270 }
9271
9272 /* Read the import statement specified by the given die and record it. */
9273
9274 static void
9275 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9276 {
9277 struct objfile *objfile = cu->per_objfile->objfile;
9278 struct attribute *import_attr;
9279 struct die_info *imported_die, *child_die;
9280 struct dwarf2_cu *imported_cu;
9281 const char *imported_name;
9282 const char *imported_name_prefix;
9283 const char *canonical_name;
9284 const char *import_alias;
9285 const char *imported_declaration = NULL;
9286 const char *import_prefix;
9287 std::vector<const char *> excludes;
9288
9289 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9290 if (import_attr == NULL)
9291 {
9292 complaint (_("Tag '%s' has no DW_AT_import"),
9293 dwarf_tag_name (die->tag));
9294 return;
9295 }
9296
9297 imported_cu = cu;
9298 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9299 imported_name = dwarf2_name (imported_die, imported_cu);
9300 if (imported_name == NULL)
9301 {
9302 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9303
9304 The import in the following code:
9305 namespace A
9306 {
9307 typedef int B;
9308 }
9309
9310 int main ()
9311 {
9312 using A::B;
9313 B b;
9314 return b;
9315 }
9316
9317 ...
9318 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9319 <52> DW_AT_decl_file : 1
9320 <53> DW_AT_decl_line : 6
9321 <54> DW_AT_import : <0x75>
9322 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9323 <59> DW_AT_name : B
9324 <5b> DW_AT_decl_file : 1
9325 <5c> DW_AT_decl_line : 2
9326 <5d> DW_AT_type : <0x6e>
9327 ...
9328 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9329 <76> DW_AT_byte_size : 4
9330 <77> DW_AT_encoding : 5 (signed)
9331
9332 imports the wrong die ( 0x75 instead of 0x58 ).
9333 This case will be ignored until the gcc bug is fixed. */
9334 return;
9335 }
9336
9337 /* Figure out the local name after import. */
9338 import_alias = dwarf2_name (die, cu);
9339
9340 /* Figure out where the statement is being imported to. */
9341 import_prefix = determine_prefix (die, cu);
9342
9343 /* Figure out what the scope of the imported die is and prepend it
9344 to the name of the imported die. */
9345 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9346
9347 if (imported_die->tag != DW_TAG_namespace
9348 && imported_die->tag != DW_TAG_module)
9349 {
9350 imported_declaration = imported_name;
9351 canonical_name = imported_name_prefix;
9352 }
9353 else if (strlen (imported_name_prefix) > 0)
9354 canonical_name = obconcat (&objfile->objfile_obstack,
9355 imported_name_prefix,
9356 (cu->per_cu->lang == language_d
9357 ? "."
9358 : "::"),
9359 imported_name, (char *) NULL);
9360 else
9361 canonical_name = imported_name;
9362
9363 if (die->tag == DW_TAG_imported_module
9364 && cu->per_cu->lang == language_fortran)
9365 for (child_die = die->child; child_die && child_die->tag;
9366 child_die = child_die->sibling)
9367 {
9368 /* DWARF-4: A Fortran use statement with a “rename list” may be
9369 represented by an imported module entry with an import attribute
9370 referring to the module and owned entries corresponding to those
9371 entities that are renamed as part of being imported. */
9372
9373 if (child_die->tag != DW_TAG_imported_declaration)
9374 {
9375 complaint (_("child DW_TAG_imported_declaration expected "
9376 "- DIE at %s [in module %s]"),
9377 sect_offset_str (child_die->sect_off),
9378 objfile_name (objfile));
9379 continue;
9380 }
9381
9382 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9383 if (import_attr == NULL)
9384 {
9385 complaint (_("Tag '%s' has no DW_AT_import"),
9386 dwarf_tag_name (child_die->tag));
9387 continue;
9388 }
9389
9390 imported_cu = cu;
9391 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9392 &imported_cu);
9393 imported_name = dwarf2_name (imported_die, imported_cu);
9394 if (imported_name == NULL)
9395 {
9396 complaint (_("child DW_TAG_imported_declaration has unknown "
9397 "imported name - DIE at %s [in module %s]"),
9398 sect_offset_str (child_die->sect_off),
9399 objfile_name (objfile));
9400 continue;
9401 }
9402
9403 excludes.push_back (imported_name);
9404
9405 process_die (child_die, cu);
9406 }
9407
9408 add_using_directive (using_directives (cu),
9409 import_prefix,
9410 canonical_name,
9411 import_alias,
9412 imported_declaration,
9413 excludes,
9414 0,
9415 &objfile->objfile_obstack);
9416 }
9417
9418 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9419 types, but gives them a size of zero. Starting with version 14,
9420 ICC is compatible with GCC. */
9421
9422 static bool
9423 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9424 {
9425 if (!cu->checked_producer)
9426 check_producer (cu);
9427
9428 return cu->producer_is_icc_lt_14;
9429 }
9430
9431 /* ICC generates a DW_AT_type for C void functions. This was observed on
9432 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9433 which says that void functions should not have a DW_AT_type. */
9434
9435 static bool
9436 producer_is_icc (struct dwarf2_cu *cu)
9437 {
9438 if (!cu->checked_producer)
9439 check_producer (cu);
9440
9441 return cu->producer_is_icc;
9442 }
9443
9444 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9445 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9446 this, it was first present in GCC release 4.3.0. */
9447
9448 static bool
9449 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9450 {
9451 if (!cu->checked_producer)
9452 check_producer (cu);
9453
9454 return cu->producer_is_gcc_lt_4_3;
9455 }
9456
9457 static file_and_directory &
9458 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9459 {
9460 if (cu->per_cu->fnd != nullptr)
9461 return *cu->per_cu->fnd;
9462
9463 /* Find the filename. Do not use dwarf2_name here, since the filename
9464 is not a source language identifier. */
9465 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
9466 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
9467
9468 if (res.get_comp_dir () == nullptr
9469 && producer_is_gcc_lt_4_3 (cu)
9470 && res.get_name () != nullptr
9471 && IS_ABSOLUTE_PATH (res.get_name ()))
9472 res.set_comp_dir (ldirname (res.get_name ()));
9473
9474 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
9475 return *cu->per_cu->fnd;
9476 }
9477
9478 /* Handle DW_AT_stmt_list for a compilation unit.
9479 DIE is the DW_TAG_compile_unit die for CU.
9480 COMP_DIR is the compilation directory. LOWPC is passed to
9481 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9482
9483 static void
9484 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9485 CORE_ADDR lowpc) /* ARI: editCase function */
9486 {
9487 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9488 struct attribute *attr;
9489 struct line_header line_header_local;
9490 hashval_t line_header_local_hash;
9491 void **slot;
9492 int decode_mapping;
9493
9494 gdb_assert (! cu->per_cu->is_debug_types);
9495
9496 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9497 if (attr == NULL || !attr->form_is_unsigned ())
9498 return;
9499
9500 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9501
9502 /* The line header hash table is only created if needed (it exists to
9503 prevent redundant reading of the line table for partial_units).
9504 If we're given a partial_unit, we'll need it. If we're given a
9505 compile_unit, then use the line header hash table if it's already
9506 created, but don't create one just yet. */
9507
9508 if (per_objfile->line_header_hash == NULL
9509 && die->tag == DW_TAG_partial_unit)
9510 {
9511 per_objfile->line_header_hash
9512 .reset (htab_create_alloc (127, line_header_hash_voidp,
9513 line_header_eq_voidp,
9514 htab_delete_entry<line_header>,
9515 xcalloc, xfree));
9516 }
9517
9518 line_header_local.sect_off = line_offset;
9519 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9520 line_header_local_hash = line_header_hash (&line_header_local);
9521 if (per_objfile->line_header_hash != NULL)
9522 {
9523 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9524 &line_header_local,
9525 line_header_local_hash, NO_INSERT);
9526
9527 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9528 is not present in *SLOT (since if there is something in *SLOT then
9529 it will be for a partial_unit). */
9530 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9531 {
9532 gdb_assert (*slot != NULL);
9533 cu->line_header = (struct line_header *) *slot;
9534 return;
9535 }
9536 }
9537
9538 /* dwarf_decode_line_header does not yet provide sufficient information.
9539 We always have to call also dwarf_decode_lines for it. */
9540 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9541 if (lh == NULL)
9542 return;
9543
9544 cu->line_header = lh.release ();
9545 cu->line_header_die_owner = die;
9546
9547 if (per_objfile->line_header_hash == NULL)
9548 slot = NULL;
9549 else
9550 {
9551 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9552 &line_header_local,
9553 line_header_local_hash, INSERT);
9554 gdb_assert (slot != NULL);
9555 }
9556 if (slot != NULL && *slot == NULL)
9557 {
9558 /* This newly decoded line number information unit will be owned
9559 by line_header_hash hash table. */
9560 *slot = cu->line_header;
9561 cu->line_header_die_owner = NULL;
9562 }
9563 else
9564 {
9565 /* We cannot free any current entry in (*slot) as that struct line_header
9566 may be already used by multiple CUs. Create only temporary decoded
9567 line_header for this CU - it may happen at most once for each line
9568 number information unit. And if we're not using line_header_hash
9569 then this is what we want as well. */
9570 gdb_assert (die->tag != DW_TAG_partial_unit);
9571 }
9572 decode_mapping = (die->tag != DW_TAG_partial_unit);
9573 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
9574 }
9575
9576 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9577
9578 static void
9579 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9580 {
9581 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9582 struct objfile *objfile = per_objfile->objfile;
9583 struct gdbarch *gdbarch = objfile->arch ();
9584 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9585 CORE_ADDR highpc = ((CORE_ADDR) 0);
9586 struct attribute *attr;
9587 struct die_info *child_die;
9588 CORE_ADDR baseaddr;
9589
9590 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
9591 baseaddr = objfile->text_section_offset ();
9592
9593 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9594
9595 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9596 from finish_block. */
9597 if (lowpc == ((CORE_ADDR) -1))
9598 lowpc = highpc;
9599 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9600
9601 file_and_directory &fnd = find_file_and_directory (die, cu);
9602
9603 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
9604 lowpc);
9605
9606 gdb_assert (per_objfile->sym_cu == nullptr);
9607 scoped_restore restore_sym_cu
9608 = make_scoped_restore (&per_objfile->sym_cu, cu);
9609
9610 /* Decode line number information if present. We do this before
9611 processing child DIEs, so that the line header table is available
9612 for DW_AT_decl_file. The PC check is here because, if LOWPC and
9613 HIGHPC are both 0x0, then there won't be any interesting code in
9614 the CU, but a check later on (in
9615 lnp_state_machine::check_line_address) will fail to properly
9616 exclude an entry that was removed via --gc-sections. */
9617 if (lowpc != highpc)
9618 handle_DW_AT_stmt_list (die, cu, lowpc);
9619
9620 /* Process all dies in compilation unit. */
9621 if (die->child != NULL)
9622 {
9623 child_die = die->child;
9624 while (child_die && child_die->tag)
9625 {
9626 process_die (child_die, cu);
9627 child_die = child_die->sibling;
9628 }
9629 }
9630 per_objfile->sym_cu = nullptr;
9631
9632 /* Decode macro information, if present. Dwarf 2 macro information
9633 refers to information in the line number info statement program
9634 header, so we can only read it if we've read the header
9635 successfully. */
9636 attr = dwarf2_attr (die, DW_AT_macros, cu);
9637 if (attr == NULL)
9638 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9639 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9640 {
9641 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9642 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9643
9644 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
9645 }
9646 else
9647 {
9648 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9649 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9650 {
9651 unsigned int macro_offset = attr->as_unsigned ();
9652
9653 dwarf_decode_macros (cu, macro_offset, 0);
9654 }
9655 }
9656 }
9657
9658 void
9659 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
9660 {
9661 struct type_unit_group *tu_group;
9662 int first_time;
9663 struct attribute *attr;
9664 unsigned int i;
9665 struct signatured_type *sig_type;
9666
9667 gdb_assert (per_cu->is_debug_types);
9668 sig_type = (struct signatured_type *) per_cu;
9669
9670 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
9671
9672 /* If we're using .gdb_index (includes -readnow) then
9673 per_cu->type_unit_group may not have been set up yet. */
9674 if (sig_type->type_unit_group == NULL)
9675 sig_type->type_unit_group = get_type_unit_group (this, attr);
9676 tu_group = sig_type->type_unit_group;
9677
9678 /* If we've already processed this stmt_list there's no real need to
9679 do it again, we could fake it and just recreate the part we need
9680 (file name,index -> symtab mapping). If data shows this optimization
9681 is useful we can do it then. */
9682 type_unit_group_unshareable *tug_unshare
9683 = per_objfile->get_type_unit_group_unshareable (tu_group);
9684 first_time = tug_unshare->compunit_symtab == NULL;
9685
9686 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9687 debug info. */
9688 line_header_up lh;
9689 if (attr != NULL && attr->form_is_unsigned ())
9690 {
9691 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9692 lh = dwarf_decode_line_header (line_offset, this);
9693 }
9694 if (lh == NULL)
9695 {
9696 if (first_time)
9697 start_compunit_symtab ("", NULL, 0);
9698 else
9699 {
9700 gdb_assert (tug_unshare->symtabs == NULL);
9701 gdb_assert (m_builder == nullptr);
9702 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9703 m_builder.reset (new struct buildsym_compunit
9704 (cust->objfile (), "",
9705 cust->dirname (),
9706 compunit_language (cust),
9707 0, cust));
9708 list_in_scope = get_builder ()->get_file_symbols ();
9709 }
9710 return;
9711 }
9712
9713 line_header = lh.release ();
9714 line_header_die_owner = die;
9715
9716 if (first_time)
9717 {
9718 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
9719
9720 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9721 still initializing it, and our caller (a few levels up)
9722 process_full_type_unit still needs to know if this is the first
9723 time. */
9724
9725 tug_unshare->symtabs
9726 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
9727 struct symtab *, line_header->file_names_size ());
9728
9729 auto &file_names = line_header->file_names ();
9730 for (i = 0; i < file_names.size (); ++i)
9731 {
9732 file_entry &fe = file_names[i];
9733 dwarf2_start_subfile (this, fe.name,
9734 fe.include_dir (line_header));
9735 buildsym_compunit *b = get_builder ();
9736 if (b->get_current_subfile ()->symtab == NULL)
9737 {
9738 /* NOTE: start_subfile will recognize when it's been
9739 passed a file it has already seen. So we can't
9740 assume there's a simple mapping from
9741 cu->line_header->file_names to subfiles, plus
9742 cu->line_header->file_names may contain dups. */
9743 b->get_current_subfile ()->symtab
9744 = allocate_symtab (cust, b->get_current_subfile ()->name);
9745 }
9746
9747 fe.symtab = b->get_current_subfile ()->symtab;
9748 tug_unshare->symtabs[i] = fe.symtab;
9749 }
9750 }
9751 else
9752 {
9753 gdb_assert (m_builder == nullptr);
9754 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9755 m_builder.reset (new struct buildsym_compunit
9756 (cust->objfile (), "",
9757 cust->dirname (),
9758 compunit_language (cust),
9759 0, cust));
9760 list_in_scope = get_builder ()->get_file_symbols ();
9761
9762 auto &file_names = line_header->file_names ();
9763 for (i = 0; i < file_names.size (); ++i)
9764 {
9765 file_entry &fe = file_names[i];
9766 fe.symtab = tug_unshare->symtabs[i];
9767 }
9768 }
9769
9770 /* The main symtab is allocated last. Type units don't have DW_AT_name
9771 so they don't have a "real" (so to speak) symtab anyway.
9772 There is later code that will assign the main symtab to all symbols
9773 that don't have one. We need to handle the case of a symbol with a
9774 missing symtab (DW_AT_decl_file) anyway. */
9775 }
9776
9777 /* Process DW_TAG_type_unit.
9778 For TUs we want to skip the first top level sibling if it's not the
9779 actual type being defined by this TU. In this case the first top
9780 level sibling is there to provide context only. */
9781
9782 static void
9783 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9784 {
9785 struct die_info *child_die;
9786
9787 prepare_one_comp_unit (cu, die, language_minimal);
9788
9789 /* Initialize (or reinitialize) the machinery for building symtabs.
9790 We do this before processing child DIEs, so that the line header table
9791 is available for DW_AT_decl_file. */
9792 cu->setup_type_unit_groups (die);
9793
9794 if (die->child != NULL)
9795 {
9796 child_die = die->child;
9797 while (child_die && child_die->tag)
9798 {
9799 process_die (child_die, cu);
9800 child_die = child_die->sibling;
9801 }
9802 }
9803 }
9804 \f
9805 /* DWO/DWP files.
9806
9807 http://gcc.gnu.org/wiki/DebugFission
9808 http://gcc.gnu.org/wiki/DebugFissionDWP
9809
9810 To simplify handling of both DWO files ("object" files with the DWARF info)
9811 and DWP files (a file with the DWOs packaged up into one file), we treat
9812 DWP files as having a collection of virtual DWO files. */
9813
9814 static hashval_t
9815 hash_dwo_file (const void *item)
9816 {
9817 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9818 hashval_t hash;
9819
9820 hash = htab_hash_string (dwo_file->dwo_name);
9821 if (dwo_file->comp_dir != NULL)
9822 hash += htab_hash_string (dwo_file->comp_dir);
9823 return hash;
9824 }
9825
9826 static int
9827 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9828 {
9829 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9830 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9831
9832 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9833 return 0;
9834 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9835 return lhs->comp_dir == rhs->comp_dir;
9836 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9837 }
9838
9839 /* Allocate a hash table for DWO files. */
9840
9841 static htab_up
9842 allocate_dwo_file_hash_table ()
9843 {
9844 return htab_up (htab_create_alloc (41,
9845 hash_dwo_file,
9846 eq_dwo_file,
9847 htab_delete_entry<dwo_file>,
9848 xcalloc, xfree));
9849 }
9850
9851 /* Lookup DWO file DWO_NAME. */
9852
9853 static void **
9854 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
9855 const char *dwo_name,
9856 const char *comp_dir)
9857 {
9858 struct dwo_file find_entry;
9859 void **slot;
9860
9861 if (per_objfile->per_bfd->dwo_files == NULL)
9862 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
9863
9864 find_entry.dwo_name = dwo_name;
9865 find_entry.comp_dir = comp_dir;
9866 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
9867 INSERT);
9868
9869 return slot;
9870 }
9871
9872 static hashval_t
9873 hash_dwo_unit (const void *item)
9874 {
9875 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9876
9877 /* This drops the top 32 bits of the id, but is ok for a hash. */
9878 return dwo_unit->signature;
9879 }
9880
9881 static int
9882 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9883 {
9884 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9885 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9886
9887 /* The signature is assumed to be unique within the DWO file.
9888 So while object file CU dwo_id's always have the value zero,
9889 that's OK, assuming each object file DWO file has only one CU,
9890 and that's the rule for now. */
9891 return lhs->signature == rhs->signature;
9892 }
9893
9894 /* Allocate a hash table for DWO CUs,TUs.
9895 There is one of these tables for each of CUs,TUs for each DWO file. */
9896
9897 static htab_up
9898 allocate_dwo_unit_table ()
9899 {
9900 /* Start out with a pretty small number.
9901 Generally DWO files contain only one CU and maybe some TUs. */
9902 return htab_up (htab_create_alloc (3,
9903 hash_dwo_unit,
9904 eq_dwo_unit,
9905 NULL, xcalloc, xfree));
9906 }
9907
9908 /* die_reader_func for create_dwo_cu. */
9909
9910 static void
9911 create_dwo_cu_reader (const struct die_reader_specs *reader,
9912 const gdb_byte *info_ptr,
9913 struct die_info *comp_unit_die,
9914 struct dwo_file *dwo_file,
9915 struct dwo_unit *dwo_unit)
9916 {
9917 struct dwarf2_cu *cu = reader->cu;
9918 sect_offset sect_off = cu->per_cu->sect_off;
9919 struct dwarf2_section_info *section = cu->per_cu->section;
9920
9921 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
9922 if (!signature.has_value ())
9923 {
9924 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9925 " its dwo_id [in module %s]"),
9926 sect_offset_str (sect_off), dwo_file->dwo_name);
9927 return;
9928 }
9929
9930 dwo_unit->dwo_file = dwo_file;
9931 dwo_unit->signature = *signature;
9932 dwo_unit->section = section;
9933 dwo_unit->sect_off = sect_off;
9934 dwo_unit->length = cu->per_cu->length;
9935
9936 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9937 sect_offset_str (sect_off),
9938 hex_string (dwo_unit->signature));
9939 }
9940
9941 /* Create the dwo_units for the CUs in a DWO_FILE.
9942 Note: This function processes DWO files only, not DWP files. */
9943
9944 static void
9945 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
9946 dwarf2_cu *cu, struct dwo_file &dwo_file,
9947 dwarf2_section_info &section, htab_up &cus_htab)
9948 {
9949 struct objfile *objfile = per_objfile->objfile;
9950 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
9951 const gdb_byte *info_ptr, *end_ptr;
9952
9953 section.read (objfile);
9954 info_ptr = section.buffer;
9955
9956 if (info_ptr == NULL)
9957 return;
9958
9959 dwarf_read_debug_printf ("Reading %s for %s:",
9960 section.get_name (),
9961 section.get_file_name ());
9962
9963 end_ptr = info_ptr + section.size;
9964 while (info_ptr < end_ptr)
9965 {
9966 struct dwarf2_per_cu_data per_cu;
9967 struct dwo_unit read_unit {};
9968 struct dwo_unit *dwo_unit;
9969 void **slot;
9970 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
9971
9972 per_cu.per_bfd = per_bfd;
9973 per_cu.is_debug_types = 0;
9974 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
9975 per_cu.section = &section;
9976
9977 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
9978 if (!reader.dummy_p)
9979 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
9980 &dwo_file, &read_unit);
9981 info_ptr += per_cu.length;
9982
9983 // If the unit could not be parsed, skip it.
9984 if (read_unit.dwo_file == NULL)
9985 continue;
9986
9987 if (cus_htab == NULL)
9988 cus_htab = allocate_dwo_unit_table ();
9989
9990 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
9991 struct dwo_unit);
9992 *dwo_unit = read_unit;
9993 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
9994 gdb_assert (slot != NULL);
9995 if (*slot != NULL)
9996 {
9997 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
9998 sect_offset dup_sect_off = dup_cu->sect_off;
9999
10000 complaint (_("debug cu entry at offset %s is duplicate to"
10001 " the entry at offset %s, signature %s"),
10002 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
10003 hex_string (dwo_unit->signature));
10004 }
10005 *slot = (void *)dwo_unit;
10006 }
10007 }
10008
10009 /* DWP file .debug_{cu,tu}_index section format:
10010 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10011 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10012
10013 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10014 officially standard DWP format was published with DWARF v5 and is called
10015 Version 5. There are no versions 3 or 4.
10016
10017 DWP Version 1:
10018
10019 Both index sections have the same format, and serve to map a 64-bit
10020 signature to a set of section numbers. Each section begins with a header,
10021 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10022 indexes, and a pool of 32-bit section numbers. The index sections will be
10023 aligned at 8-byte boundaries in the file.
10024
10025 The index section header consists of:
10026
10027 V, 32 bit version number
10028 -, 32 bits unused
10029 N, 32 bit number of compilation units or type units in the index
10030 M, 32 bit number of slots in the hash table
10031
10032 Numbers are recorded using the byte order of the application binary.
10033
10034 The hash table begins at offset 16 in the section, and consists of an array
10035 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10036 order of the application binary). Unused slots in the hash table are 0.
10037 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10038
10039 The parallel table begins immediately after the hash table
10040 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10041 array of 32-bit indexes (using the byte order of the application binary),
10042 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10043 table contains a 32-bit index into the pool of section numbers. For unused
10044 hash table slots, the corresponding entry in the parallel table will be 0.
10045
10046 The pool of section numbers begins immediately following the hash table
10047 (at offset 16 + 12 * M from the beginning of the section). The pool of
10048 section numbers consists of an array of 32-bit words (using the byte order
10049 of the application binary). Each item in the array is indexed starting
10050 from 0. The hash table entry provides the index of the first section
10051 number in the set. Additional section numbers in the set follow, and the
10052 set is terminated by a 0 entry (section number 0 is not used in ELF).
10053
10054 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10055 section must be the first entry in the set, and the .debug_abbrev.dwo must
10056 be the second entry. Other members of the set may follow in any order.
10057
10058 ---
10059
10060 DWP Versions 2 and 5:
10061
10062 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10063 and the entries in the index tables are now offsets into these sections.
10064 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10065 section.
10066
10067 Index Section Contents:
10068 Header
10069 Hash Table of Signatures dwp_hash_table.hash_table
10070 Parallel Table of Indices dwp_hash_table.unit_table
10071 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10072 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10073
10074 The index section header consists of:
10075
10076 V, 32 bit version number
10077 L, 32 bit number of columns in the table of section offsets
10078 N, 32 bit number of compilation units or type units in the index
10079 M, 32 bit number of slots in the hash table
10080
10081 Numbers are recorded using the byte order of the application binary.
10082
10083 The hash table has the same format as version 1.
10084 The parallel table of indices has the same format as version 1,
10085 except that the entries are origin-1 indices into the table of sections
10086 offsets and the table of section sizes.
10087
10088 The table of offsets begins immediately following the parallel table
10089 (at offset 16 + 12 * M from the beginning of the section). The table is
10090 a two-dimensional array of 32-bit words (using the byte order of the
10091 application binary), with L columns and N+1 rows, in row-major order.
10092 Each row in the array is indexed starting from 0. The first row provides
10093 a key to the remaining rows: each column in this row provides an identifier
10094 for a debug section, and the offsets in the same column of subsequent rows
10095 refer to that section. The section identifiers for Version 2 are:
10096
10097 DW_SECT_INFO 1 .debug_info.dwo
10098 DW_SECT_TYPES 2 .debug_types.dwo
10099 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10100 DW_SECT_LINE 4 .debug_line.dwo
10101 DW_SECT_LOC 5 .debug_loc.dwo
10102 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10103 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10104 DW_SECT_MACRO 8 .debug_macro.dwo
10105
10106 The section identifiers for Version 5 are:
10107
10108 DW_SECT_INFO_V5 1 .debug_info.dwo
10109 DW_SECT_RESERVED_V5 2 --
10110 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10111 DW_SECT_LINE_V5 4 .debug_line.dwo
10112 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10113 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10114 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10115 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10116
10117 The offsets provided by the CU and TU index sections are the base offsets
10118 for the contributions made by each CU or TU to the corresponding section
10119 in the package file. Each CU and TU header contains an abbrev_offset
10120 field, used to find the abbreviations table for that CU or TU within the
10121 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10122 be interpreted as relative to the base offset given in the index section.
10123 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10124 should be interpreted as relative to the base offset for .debug_line.dwo,
10125 and offsets into other debug sections obtained from DWARF attributes should
10126 also be interpreted as relative to the corresponding base offset.
10127
10128 The table of sizes begins immediately following the table of offsets.
10129 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10130 with L columns and N rows, in row-major order. Each row in the array is
10131 indexed starting from 1 (row 0 is shared by the two tables).
10132
10133 ---
10134
10135 Hash table lookup is handled the same in version 1 and 2:
10136
10137 We assume that N and M will not exceed 2^32 - 1.
10138 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10139
10140 Given a 64-bit compilation unit signature or a type signature S, an entry
10141 in the hash table is located as follows:
10142
10143 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10144 the low-order k bits all set to 1.
10145
10146 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10147
10148 3) If the hash table entry at index H matches the signature, use that
10149 entry. If the hash table entry at index H is unused (all zeroes),
10150 terminate the search: the signature is not present in the table.
10151
10152 4) Let H = (H + H') modulo M. Repeat at Step 3.
10153
10154 Because M > N and H' and M are relatively prime, the search is guaranteed
10155 to stop at an unused slot or find the match. */
10156
10157 /* Create a hash table to map DWO IDs to their CU/TU entry in
10158 .debug_{info,types}.dwo in DWP_FILE.
10159 Returns NULL if there isn't one.
10160 Note: This function processes DWP files only, not DWO files. */
10161
10162 static struct dwp_hash_table *
10163 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
10164 struct dwp_file *dwp_file, int is_debug_types)
10165 {
10166 struct objfile *objfile = per_objfile->objfile;
10167 bfd *dbfd = dwp_file->dbfd.get ();
10168 const gdb_byte *index_ptr, *index_end;
10169 struct dwarf2_section_info *index;
10170 uint32_t version, nr_columns, nr_units, nr_slots;
10171 struct dwp_hash_table *htab;
10172
10173 if (is_debug_types)
10174 index = &dwp_file->sections.tu_index;
10175 else
10176 index = &dwp_file->sections.cu_index;
10177
10178 if (index->empty ())
10179 return NULL;
10180 index->read (objfile);
10181
10182 index_ptr = index->buffer;
10183 index_end = index_ptr + index->size;
10184
10185 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10186 For now it's safe to just read 4 bytes (particularly as it's difficult to
10187 tell if you're dealing with Version 5 before you've read the version). */
10188 version = read_4_bytes (dbfd, index_ptr);
10189 index_ptr += 4;
10190 if (version == 2 || version == 5)
10191 nr_columns = read_4_bytes (dbfd, index_ptr);
10192 else
10193 nr_columns = 0;
10194 index_ptr += 4;
10195 nr_units = read_4_bytes (dbfd, index_ptr);
10196 index_ptr += 4;
10197 nr_slots = read_4_bytes (dbfd, index_ptr);
10198 index_ptr += 4;
10199
10200 if (version != 1 && version != 2 && version != 5)
10201 {
10202 error (_("Dwarf Error: unsupported DWP file version (%s)"
10203 " [in module %s]"),
10204 pulongest (version), dwp_file->name);
10205 }
10206 if (nr_slots != (nr_slots & -nr_slots))
10207 {
10208 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10209 " is not power of 2 [in module %s]"),
10210 pulongest (nr_slots), dwp_file->name);
10211 }
10212
10213 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
10214 htab->version = version;
10215 htab->nr_columns = nr_columns;
10216 htab->nr_units = nr_units;
10217 htab->nr_slots = nr_slots;
10218 htab->hash_table = index_ptr;
10219 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10220
10221 /* Exit early if the table is empty. */
10222 if (nr_slots == 0 || nr_units == 0
10223 || (version == 2 && nr_columns == 0)
10224 || (version == 5 && nr_columns == 0))
10225 {
10226 /* All must be zero. */
10227 if (nr_slots != 0 || nr_units != 0
10228 || (version == 2 && nr_columns != 0)
10229 || (version == 5 && nr_columns != 0))
10230 {
10231 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10232 " all zero [in modules %s]"),
10233 dwp_file->name);
10234 }
10235 return htab;
10236 }
10237
10238 if (version == 1)
10239 {
10240 htab->section_pool.v1.indices =
10241 htab->unit_table + sizeof (uint32_t) * nr_slots;
10242 /* It's harder to decide whether the section is too small in v1.
10243 V1 is deprecated anyway so we punt. */
10244 }
10245 else if (version == 2)
10246 {
10247 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10248 int *ids = htab->section_pool.v2.section_ids;
10249 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
10250 /* Reverse map for error checking. */
10251 int ids_seen[DW_SECT_MAX + 1];
10252 int i;
10253
10254 if (nr_columns < 2)
10255 {
10256 error (_("Dwarf Error: bad DWP hash table, too few columns"
10257 " in section table [in module %s]"),
10258 dwp_file->name);
10259 }
10260 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10261 {
10262 error (_("Dwarf Error: bad DWP hash table, too many columns"
10263 " in section table [in module %s]"),
10264 dwp_file->name);
10265 }
10266 memset (ids, 255, sizeof_ids);
10267 memset (ids_seen, 255, sizeof (ids_seen));
10268 for (i = 0; i < nr_columns; ++i)
10269 {
10270 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10271
10272 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10273 {
10274 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10275 " in section table [in module %s]"),
10276 id, dwp_file->name);
10277 }
10278 if (ids_seen[id] != -1)
10279 {
10280 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10281 " id %d in section table [in module %s]"),
10282 id, dwp_file->name);
10283 }
10284 ids_seen[id] = i;
10285 ids[i] = id;
10286 }
10287 /* Must have exactly one info or types section. */
10288 if (((ids_seen[DW_SECT_INFO] != -1)
10289 + (ids_seen[DW_SECT_TYPES] != -1))
10290 != 1)
10291 {
10292 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10293 " DWO info/types section [in module %s]"),
10294 dwp_file->name);
10295 }
10296 /* Must have an abbrev section. */
10297 if (ids_seen[DW_SECT_ABBREV] == -1)
10298 {
10299 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10300 " section [in module %s]"),
10301 dwp_file->name);
10302 }
10303 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10304 htab->section_pool.v2.sizes =
10305 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10306 * nr_units * nr_columns);
10307 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10308 * nr_units * nr_columns))
10309 > index_end)
10310 {
10311 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10312 " [in module %s]"),
10313 dwp_file->name);
10314 }
10315 }
10316 else /* version == 5 */
10317 {
10318 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10319 int *ids = htab->section_pool.v5.section_ids;
10320 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
10321 /* Reverse map for error checking. */
10322 int ids_seen[DW_SECT_MAX_V5 + 1];
10323
10324 if (nr_columns < 2)
10325 {
10326 error (_("Dwarf Error: bad DWP hash table, too few columns"
10327 " in section table [in module %s]"),
10328 dwp_file->name);
10329 }
10330 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
10331 {
10332 error (_("Dwarf Error: bad DWP hash table, too many columns"
10333 " in section table [in module %s]"),
10334 dwp_file->name);
10335 }
10336 memset (ids, 255, sizeof_ids);
10337 memset (ids_seen, 255, sizeof (ids_seen));
10338 for (int i = 0; i < nr_columns; ++i)
10339 {
10340 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10341
10342 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
10343 {
10344 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10345 " in section table [in module %s]"),
10346 id, dwp_file->name);
10347 }
10348 if (ids_seen[id] != -1)
10349 {
10350 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10351 " id %d in section table [in module %s]"),
10352 id, dwp_file->name);
10353 }
10354 ids_seen[id] = i;
10355 ids[i] = id;
10356 }
10357 /* Must have seen an info section. */
10358 if (ids_seen[DW_SECT_INFO_V5] == -1)
10359 {
10360 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10361 " DWO info/types section [in module %s]"),
10362 dwp_file->name);
10363 }
10364 /* Must have an abbrev section. */
10365 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
10366 {
10367 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10368 " section [in module %s]"),
10369 dwp_file->name);
10370 }
10371 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10372 htab->section_pool.v5.sizes
10373 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
10374 * nr_units * nr_columns);
10375 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
10376 * nr_units * nr_columns))
10377 > index_end)
10378 {
10379 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10380 " [in module %s]"),
10381 dwp_file->name);
10382 }
10383 }
10384
10385 return htab;
10386 }
10387
10388 /* Update SECTIONS with the data from SECTP.
10389
10390 This function is like the other "locate" section routines, but in
10391 this context the sections to read comes from the DWP V1 hash table,
10392 not the full ELF section table.
10393
10394 The result is non-zero for success, or zero if an error was found. */
10395
10396 static int
10397 locate_v1_virtual_dwo_sections (asection *sectp,
10398 struct virtual_v1_dwo_sections *sections)
10399 {
10400 const struct dwop_section_names *names = &dwop_section_names;
10401
10402 if (names->abbrev_dwo.matches (sectp->name))
10403 {
10404 /* There can be only one. */
10405 if (sections->abbrev.s.section != NULL)
10406 return 0;
10407 sections->abbrev.s.section = sectp;
10408 sections->abbrev.size = bfd_section_size (sectp);
10409 }
10410 else if (names->info_dwo.matches (sectp->name)
10411 || names->types_dwo.matches (sectp->name))
10412 {
10413 /* There can be only one. */
10414 if (sections->info_or_types.s.section != NULL)
10415 return 0;
10416 sections->info_or_types.s.section = sectp;
10417 sections->info_or_types.size = bfd_section_size (sectp);
10418 }
10419 else if (names->line_dwo.matches (sectp->name))
10420 {
10421 /* There can be only one. */
10422 if (sections->line.s.section != NULL)
10423 return 0;
10424 sections->line.s.section = sectp;
10425 sections->line.size = bfd_section_size (sectp);
10426 }
10427 else if (names->loc_dwo.matches (sectp->name))
10428 {
10429 /* There can be only one. */
10430 if (sections->loc.s.section != NULL)
10431 return 0;
10432 sections->loc.s.section = sectp;
10433 sections->loc.size = bfd_section_size (sectp);
10434 }
10435 else if (names->macinfo_dwo.matches (sectp->name))
10436 {
10437 /* There can be only one. */
10438 if (sections->macinfo.s.section != NULL)
10439 return 0;
10440 sections->macinfo.s.section = sectp;
10441 sections->macinfo.size = bfd_section_size (sectp);
10442 }
10443 else if (names->macro_dwo.matches (sectp->name))
10444 {
10445 /* There can be only one. */
10446 if (sections->macro.s.section != NULL)
10447 return 0;
10448 sections->macro.s.section = sectp;
10449 sections->macro.size = bfd_section_size (sectp);
10450 }
10451 else if (names->str_offsets_dwo.matches (sectp->name))
10452 {
10453 /* There can be only one. */
10454 if (sections->str_offsets.s.section != NULL)
10455 return 0;
10456 sections->str_offsets.s.section = sectp;
10457 sections->str_offsets.size = bfd_section_size (sectp);
10458 }
10459 else
10460 {
10461 /* No other kind of section is valid. */
10462 return 0;
10463 }
10464
10465 return 1;
10466 }
10467
10468 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10469 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10470 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10471 This is for DWP version 1 files. */
10472
10473 static struct dwo_unit *
10474 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
10475 struct dwp_file *dwp_file,
10476 uint32_t unit_index,
10477 const char *comp_dir,
10478 ULONGEST signature, int is_debug_types)
10479 {
10480 const struct dwp_hash_table *dwp_htab =
10481 is_debug_types ? dwp_file->tus : dwp_file->cus;
10482 bfd *dbfd = dwp_file->dbfd.get ();
10483 const char *kind = is_debug_types ? "TU" : "CU";
10484 struct dwo_file *dwo_file;
10485 struct dwo_unit *dwo_unit;
10486 struct virtual_v1_dwo_sections sections;
10487 void **dwo_file_slot;
10488 int i;
10489
10490 gdb_assert (dwp_file->version == 1);
10491
10492 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10493 kind, pulongest (unit_index), hex_string (signature),
10494 dwp_file->name);
10495
10496 /* Fetch the sections of this DWO unit.
10497 Put a limit on the number of sections we look for so that bad data
10498 doesn't cause us to loop forever. */
10499
10500 #define MAX_NR_V1_DWO_SECTIONS \
10501 (1 /* .debug_info or .debug_types */ \
10502 + 1 /* .debug_abbrev */ \
10503 + 1 /* .debug_line */ \
10504 + 1 /* .debug_loc */ \
10505 + 1 /* .debug_str_offsets */ \
10506 + 1 /* .debug_macro or .debug_macinfo */ \
10507 + 1 /* trailing zero */)
10508
10509 memset (&sections, 0, sizeof (sections));
10510
10511 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10512 {
10513 asection *sectp;
10514 uint32_t section_nr =
10515 read_4_bytes (dbfd,
10516 dwp_htab->section_pool.v1.indices
10517 + (unit_index + i) * sizeof (uint32_t));
10518
10519 if (section_nr == 0)
10520 break;
10521 if (section_nr >= dwp_file->num_sections)
10522 {
10523 error (_("Dwarf Error: bad DWP hash table, section number too large"
10524 " [in module %s]"),
10525 dwp_file->name);
10526 }
10527
10528 sectp = dwp_file->elf_sections[section_nr];
10529 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10530 {
10531 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10532 " [in module %s]"),
10533 dwp_file->name);
10534 }
10535 }
10536
10537 if (i < 2
10538 || sections.info_or_types.empty ()
10539 || sections.abbrev.empty ())
10540 {
10541 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10542 " [in module %s]"),
10543 dwp_file->name);
10544 }
10545 if (i == MAX_NR_V1_DWO_SECTIONS)
10546 {
10547 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10548 " [in module %s]"),
10549 dwp_file->name);
10550 }
10551
10552 /* It's easier for the rest of the code if we fake a struct dwo_file and
10553 have dwo_unit "live" in that. At least for now.
10554
10555 The DWP file can be made up of a random collection of CUs and TUs.
10556 However, for each CU + set of TUs that came from the same original DWO
10557 file, we can combine them back into a virtual DWO file to save space
10558 (fewer struct dwo_file objects to allocate). Remember that for really
10559 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10560
10561 std::string virtual_dwo_name =
10562 string_printf ("virtual-dwo/%d-%d-%d-%d",
10563 sections.abbrev.get_id (),
10564 sections.line.get_id (),
10565 sections.loc.get_id (),
10566 sections.str_offsets.get_id ());
10567 /* Can we use an existing virtual DWO file? */
10568 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10569 comp_dir);
10570 /* Create one if necessary. */
10571 if (*dwo_file_slot == NULL)
10572 {
10573 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10574 virtual_dwo_name.c_str ());
10575
10576 dwo_file = new struct dwo_file;
10577 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10578 dwo_file->comp_dir = comp_dir;
10579 dwo_file->sections.abbrev = sections.abbrev;
10580 dwo_file->sections.line = sections.line;
10581 dwo_file->sections.loc = sections.loc;
10582 dwo_file->sections.macinfo = sections.macinfo;
10583 dwo_file->sections.macro = sections.macro;
10584 dwo_file->sections.str_offsets = sections.str_offsets;
10585 /* The "str" section is global to the entire DWP file. */
10586 dwo_file->sections.str = dwp_file->sections.str;
10587 /* The info or types section is assigned below to dwo_unit,
10588 there's no need to record it in dwo_file.
10589 Also, we can't simply record type sections in dwo_file because
10590 we record a pointer into the vector in dwo_unit. As we collect more
10591 types we'll grow the vector and eventually have to reallocate space
10592 for it, invalidating all copies of pointers into the previous
10593 contents. */
10594 *dwo_file_slot = dwo_file;
10595 }
10596 else
10597 {
10598 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10599 virtual_dwo_name.c_str ());
10600
10601 dwo_file = (struct dwo_file *) *dwo_file_slot;
10602 }
10603
10604 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10605 dwo_unit->dwo_file = dwo_file;
10606 dwo_unit->signature = signature;
10607 dwo_unit->section =
10608 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10609 *dwo_unit->section = sections.info_or_types;
10610 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10611
10612 return dwo_unit;
10613 }
10614
10615 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10616 simplify them. Given a pointer to the containing section SECTION, and
10617 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10618 virtual section of just that piece. */
10619
10620 static struct dwarf2_section_info
10621 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
10622 struct dwarf2_section_info *section,
10623 bfd_size_type offset, bfd_size_type size)
10624 {
10625 struct dwarf2_section_info result;
10626 asection *sectp;
10627
10628 gdb_assert (section != NULL);
10629 gdb_assert (!section->is_virtual);
10630
10631 memset (&result, 0, sizeof (result));
10632 result.s.containing_section = section;
10633 result.is_virtual = true;
10634
10635 if (size == 0)
10636 return result;
10637
10638 sectp = section->get_bfd_section ();
10639
10640 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10641 bounds of the real section. This is a pretty-rare event, so just
10642 flag an error (easier) instead of a warning and trying to cope. */
10643 if (sectp == NULL
10644 || offset + size > bfd_section_size (sectp))
10645 {
10646 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10647 " in section %s [in module %s]"),
10648 sectp ? bfd_section_name (sectp) : "<unknown>",
10649 objfile_name (per_objfile->objfile));
10650 }
10651
10652 result.virtual_offset = offset;
10653 result.size = size;
10654 return result;
10655 }
10656
10657 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10658 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10659 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10660 This is for DWP version 2 files. */
10661
10662 static struct dwo_unit *
10663 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
10664 struct dwp_file *dwp_file,
10665 uint32_t unit_index,
10666 const char *comp_dir,
10667 ULONGEST signature, int is_debug_types)
10668 {
10669 const struct dwp_hash_table *dwp_htab =
10670 is_debug_types ? dwp_file->tus : dwp_file->cus;
10671 bfd *dbfd = dwp_file->dbfd.get ();
10672 const char *kind = is_debug_types ? "TU" : "CU";
10673 struct dwo_file *dwo_file;
10674 struct dwo_unit *dwo_unit;
10675 struct virtual_v2_or_v5_dwo_sections sections;
10676 void **dwo_file_slot;
10677 int i;
10678
10679 gdb_assert (dwp_file->version == 2);
10680
10681 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10682 kind, pulongest (unit_index), hex_string (signature),
10683 dwp_file->name);
10684
10685 /* Fetch the section offsets of this DWO unit. */
10686
10687 memset (&sections, 0, sizeof (sections));
10688
10689 for (i = 0; i < dwp_htab->nr_columns; ++i)
10690 {
10691 uint32_t offset = read_4_bytes (dbfd,
10692 dwp_htab->section_pool.v2.offsets
10693 + (((unit_index - 1) * dwp_htab->nr_columns
10694 + i)
10695 * sizeof (uint32_t)));
10696 uint32_t size = read_4_bytes (dbfd,
10697 dwp_htab->section_pool.v2.sizes
10698 + (((unit_index - 1) * dwp_htab->nr_columns
10699 + i)
10700 * sizeof (uint32_t)));
10701
10702 switch (dwp_htab->section_pool.v2.section_ids[i])
10703 {
10704 case DW_SECT_INFO:
10705 case DW_SECT_TYPES:
10706 sections.info_or_types_offset = offset;
10707 sections.info_or_types_size = size;
10708 break;
10709 case DW_SECT_ABBREV:
10710 sections.abbrev_offset = offset;
10711 sections.abbrev_size = size;
10712 break;
10713 case DW_SECT_LINE:
10714 sections.line_offset = offset;
10715 sections.line_size = size;
10716 break;
10717 case DW_SECT_LOC:
10718 sections.loc_offset = offset;
10719 sections.loc_size = size;
10720 break;
10721 case DW_SECT_STR_OFFSETS:
10722 sections.str_offsets_offset = offset;
10723 sections.str_offsets_size = size;
10724 break;
10725 case DW_SECT_MACINFO:
10726 sections.macinfo_offset = offset;
10727 sections.macinfo_size = size;
10728 break;
10729 case DW_SECT_MACRO:
10730 sections.macro_offset = offset;
10731 sections.macro_size = size;
10732 break;
10733 }
10734 }
10735
10736 /* It's easier for the rest of the code if we fake a struct dwo_file and
10737 have dwo_unit "live" in that. At least for now.
10738
10739 The DWP file can be made up of a random collection of CUs and TUs.
10740 However, for each CU + set of TUs that came from the same original DWO
10741 file, we can combine them back into a virtual DWO file to save space
10742 (fewer struct dwo_file objects to allocate). Remember that for really
10743 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10744
10745 std::string virtual_dwo_name =
10746 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10747 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10748 (long) (sections.line_size ? sections.line_offset : 0),
10749 (long) (sections.loc_size ? sections.loc_offset : 0),
10750 (long) (sections.str_offsets_size
10751 ? sections.str_offsets_offset : 0));
10752 /* Can we use an existing virtual DWO file? */
10753 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10754 comp_dir);
10755 /* Create one if necessary. */
10756 if (*dwo_file_slot == NULL)
10757 {
10758 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10759 virtual_dwo_name.c_str ());
10760
10761 dwo_file = new struct dwo_file;
10762 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10763 dwo_file->comp_dir = comp_dir;
10764 dwo_file->sections.abbrev =
10765 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
10766 sections.abbrev_offset,
10767 sections.abbrev_size);
10768 dwo_file->sections.line =
10769 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
10770 sections.line_offset,
10771 sections.line_size);
10772 dwo_file->sections.loc =
10773 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
10774 sections.loc_offset, sections.loc_size);
10775 dwo_file->sections.macinfo =
10776 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
10777 sections.macinfo_offset,
10778 sections.macinfo_size);
10779 dwo_file->sections.macro =
10780 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
10781 sections.macro_offset,
10782 sections.macro_size);
10783 dwo_file->sections.str_offsets =
10784 create_dwp_v2_or_v5_section (per_objfile,
10785 &dwp_file->sections.str_offsets,
10786 sections.str_offsets_offset,
10787 sections.str_offsets_size);
10788 /* The "str" section is global to the entire DWP file. */
10789 dwo_file->sections.str = dwp_file->sections.str;
10790 /* The info or types section is assigned below to dwo_unit,
10791 there's no need to record it in dwo_file.
10792 Also, we can't simply record type sections in dwo_file because
10793 we record a pointer into the vector in dwo_unit. As we collect more
10794 types we'll grow the vector and eventually have to reallocate space
10795 for it, invalidating all copies of pointers into the previous
10796 contents. */
10797 *dwo_file_slot = dwo_file;
10798 }
10799 else
10800 {
10801 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10802 virtual_dwo_name.c_str ());
10803
10804 dwo_file = (struct dwo_file *) *dwo_file_slot;
10805 }
10806
10807 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10808 dwo_unit->dwo_file = dwo_file;
10809 dwo_unit->signature = signature;
10810 dwo_unit->section =
10811 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10812 *dwo_unit->section = create_dwp_v2_or_v5_section
10813 (per_objfile,
10814 is_debug_types
10815 ? &dwp_file->sections.types
10816 : &dwp_file->sections.info,
10817 sections.info_or_types_offset,
10818 sections.info_or_types_size);
10819 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10820
10821 return dwo_unit;
10822 }
10823
10824 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10825 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10826 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10827 This is for DWP version 5 files. */
10828
10829 static struct dwo_unit *
10830 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
10831 struct dwp_file *dwp_file,
10832 uint32_t unit_index,
10833 const char *comp_dir,
10834 ULONGEST signature, int is_debug_types)
10835 {
10836 const struct dwp_hash_table *dwp_htab
10837 = is_debug_types ? dwp_file->tus : dwp_file->cus;
10838 bfd *dbfd = dwp_file->dbfd.get ();
10839 const char *kind = is_debug_types ? "TU" : "CU";
10840 struct dwo_file *dwo_file;
10841 struct dwo_unit *dwo_unit;
10842 struct virtual_v2_or_v5_dwo_sections sections {};
10843 void **dwo_file_slot;
10844
10845 gdb_assert (dwp_file->version == 5);
10846
10847 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10848 kind, pulongest (unit_index), hex_string (signature),
10849 dwp_file->name);
10850
10851 /* Fetch the section offsets of this DWO unit. */
10852
10853 /* memset (&sections, 0, sizeof (sections)); */
10854
10855 for (int i = 0; i < dwp_htab->nr_columns; ++i)
10856 {
10857 uint32_t offset = read_4_bytes (dbfd,
10858 dwp_htab->section_pool.v5.offsets
10859 + (((unit_index - 1)
10860 * dwp_htab->nr_columns
10861 + i)
10862 * sizeof (uint32_t)));
10863 uint32_t size = read_4_bytes (dbfd,
10864 dwp_htab->section_pool.v5.sizes
10865 + (((unit_index - 1) * dwp_htab->nr_columns
10866 + i)
10867 * sizeof (uint32_t)));
10868
10869 switch (dwp_htab->section_pool.v5.section_ids[i])
10870 {
10871 case DW_SECT_ABBREV_V5:
10872 sections.abbrev_offset = offset;
10873 sections.abbrev_size = size;
10874 break;
10875 case DW_SECT_INFO_V5:
10876 sections.info_or_types_offset = offset;
10877 sections.info_or_types_size = size;
10878 break;
10879 case DW_SECT_LINE_V5:
10880 sections.line_offset = offset;
10881 sections.line_size = size;
10882 break;
10883 case DW_SECT_LOCLISTS_V5:
10884 sections.loclists_offset = offset;
10885 sections.loclists_size = size;
10886 break;
10887 case DW_SECT_MACRO_V5:
10888 sections.macro_offset = offset;
10889 sections.macro_size = size;
10890 break;
10891 case DW_SECT_RNGLISTS_V5:
10892 sections.rnglists_offset = offset;
10893 sections.rnglists_size = size;
10894 break;
10895 case DW_SECT_STR_OFFSETS_V5:
10896 sections.str_offsets_offset = offset;
10897 sections.str_offsets_size = size;
10898 break;
10899 case DW_SECT_RESERVED_V5:
10900 default:
10901 break;
10902 }
10903 }
10904
10905 /* It's easier for the rest of the code if we fake a struct dwo_file and
10906 have dwo_unit "live" in that. At least for now.
10907
10908 The DWP file can be made up of a random collection of CUs and TUs.
10909 However, for each CU + set of TUs that came from the same original DWO
10910 file, we can combine them back into a virtual DWO file to save space
10911 (fewer struct dwo_file objects to allocate). Remember that for really
10912 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10913
10914 std::string virtual_dwo_name =
10915 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10916 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10917 (long) (sections.line_size ? sections.line_offset : 0),
10918 (long) (sections.loclists_size ? sections.loclists_offset : 0),
10919 (long) (sections.str_offsets_size
10920 ? sections.str_offsets_offset : 0),
10921 (long) (sections.macro_size ? sections.macro_offset : 0),
10922 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
10923 /* Can we use an existing virtual DWO file? */
10924 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
10925 virtual_dwo_name.c_str (),
10926 comp_dir);
10927 /* Create one if necessary. */
10928 if (*dwo_file_slot == NULL)
10929 {
10930 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10931 virtual_dwo_name.c_str ());
10932
10933 dwo_file = new struct dwo_file;
10934 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10935 dwo_file->comp_dir = comp_dir;
10936 dwo_file->sections.abbrev =
10937 create_dwp_v2_or_v5_section (per_objfile,
10938 &dwp_file->sections.abbrev,
10939 sections.abbrev_offset,
10940 sections.abbrev_size);
10941 dwo_file->sections.line =
10942 create_dwp_v2_or_v5_section (per_objfile,
10943 &dwp_file->sections.line,
10944 sections.line_offset, sections.line_size);
10945 dwo_file->sections.macro =
10946 create_dwp_v2_or_v5_section (per_objfile,
10947 &dwp_file->sections.macro,
10948 sections.macro_offset,
10949 sections.macro_size);
10950 dwo_file->sections.loclists =
10951 create_dwp_v2_or_v5_section (per_objfile,
10952 &dwp_file->sections.loclists,
10953 sections.loclists_offset,
10954 sections.loclists_size);
10955 dwo_file->sections.rnglists =
10956 create_dwp_v2_or_v5_section (per_objfile,
10957 &dwp_file->sections.rnglists,
10958 sections.rnglists_offset,
10959 sections.rnglists_size);
10960 dwo_file->sections.str_offsets =
10961 create_dwp_v2_or_v5_section (per_objfile,
10962 &dwp_file->sections.str_offsets,
10963 sections.str_offsets_offset,
10964 sections.str_offsets_size);
10965 /* The "str" section is global to the entire DWP file. */
10966 dwo_file->sections.str = dwp_file->sections.str;
10967 /* The info or types section is assigned below to dwo_unit,
10968 there's no need to record it in dwo_file.
10969 Also, we can't simply record type sections in dwo_file because
10970 we record a pointer into the vector in dwo_unit. As we collect more
10971 types we'll grow the vector and eventually have to reallocate space
10972 for it, invalidating all copies of pointers into the previous
10973 contents. */
10974 *dwo_file_slot = dwo_file;
10975 }
10976 else
10977 {
10978 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10979 virtual_dwo_name.c_str ());
10980
10981 dwo_file = (struct dwo_file *) *dwo_file_slot;
10982 }
10983
10984 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10985 dwo_unit->dwo_file = dwo_file;
10986 dwo_unit->signature = signature;
10987 dwo_unit->section
10988 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10989 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
10990 &dwp_file->sections.info,
10991 sections.info_or_types_offset,
10992 sections.info_or_types_size);
10993 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10994
10995 return dwo_unit;
10996 }
10997
10998 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10999 Returns NULL if the signature isn't found. */
11000
11001 static struct dwo_unit *
11002 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
11003 struct dwp_file *dwp_file, const char *comp_dir,
11004 ULONGEST signature, int is_debug_types)
11005 {
11006 const struct dwp_hash_table *dwp_htab =
11007 is_debug_types ? dwp_file->tus : dwp_file->cus;
11008 bfd *dbfd = dwp_file->dbfd.get ();
11009 uint32_t mask = dwp_htab->nr_slots - 1;
11010 uint32_t hash = signature & mask;
11011 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11012 unsigned int i;
11013 void **slot;
11014 struct dwo_unit find_dwo_cu;
11015
11016 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11017 find_dwo_cu.signature = signature;
11018 slot = htab_find_slot (is_debug_types
11019 ? dwp_file->loaded_tus.get ()
11020 : dwp_file->loaded_cus.get (),
11021 &find_dwo_cu, INSERT);
11022
11023 if (*slot != NULL)
11024 return (struct dwo_unit *) *slot;
11025
11026 /* Use a for loop so that we don't loop forever on bad debug info. */
11027 for (i = 0; i < dwp_htab->nr_slots; ++i)
11028 {
11029 ULONGEST signature_in_table;
11030
11031 signature_in_table =
11032 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11033 if (signature_in_table == signature)
11034 {
11035 uint32_t unit_index =
11036 read_4_bytes (dbfd,
11037 dwp_htab->unit_table + hash * sizeof (uint32_t));
11038
11039 if (dwp_file->version == 1)
11040 {
11041 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11042 unit_index, comp_dir,
11043 signature, is_debug_types);
11044 }
11045 else if (dwp_file->version == 2)
11046 {
11047 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11048 unit_index, comp_dir,
11049 signature, is_debug_types);
11050 }
11051 else /* version == 5 */
11052 {
11053 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11054 unit_index, comp_dir,
11055 signature, is_debug_types);
11056 }
11057 return (struct dwo_unit *) *slot;
11058 }
11059 if (signature_in_table == 0)
11060 return NULL;
11061 hash = (hash + hash2) & mask;
11062 }
11063
11064 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11065 " [in module %s]"),
11066 dwp_file->name);
11067 }
11068
11069 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11070 Open the file specified by FILE_NAME and hand it off to BFD for
11071 preliminary analysis. Return a newly initialized bfd *, which
11072 includes a canonicalized copy of FILE_NAME.
11073 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11074 SEARCH_CWD is true if the current directory is to be searched.
11075 It will be searched before debug-file-directory.
11076 If successful, the file is added to the bfd include table of the
11077 objfile's bfd (see gdb_bfd_record_inclusion).
11078 If unable to find/open the file, return NULL.
11079 NOTE: This function is derived from symfile_bfd_open. */
11080
11081 static gdb_bfd_ref_ptr
11082 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11083 const char *file_name, int is_dwp, int search_cwd)
11084 {
11085 int desc;
11086 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11087 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11088 to debug_file_directory. */
11089 const char *search_path;
11090 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11091
11092 gdb::unique_xmalloc_ptr<char> search_path_holder;
11093 if (search_cwd)
11094 {
11095 if (!debug_file_directory.empty ())
11096 {
11097 search_path_holder.reset (concat (".", dirname_separator_string,
11098 debug_file_directory.c_str (),
11099 (char *) NULL));
11100 search_path = search_path_holder.get ();
11101 }
11102 else
11103 search_path = ".";
11104 }
11105 else
11106 search_path = debug_file_directory.c_str ();
11107
11108 /* Add the path for the executable binary to the list of search paths. */
11109 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
11110 search_path_holder.reset (concat (objfile_dir.c_str (),
11111 dirname_separator_string,
11112 search_path, nullptr));
11113 search_path = search_path_holder.get ();
11114
11115 openp_flags flags = OPF_RETURN_REALPATH;
11116 if (is_dwp)
11117 flags |= OPF_SEARCH_IN_PATH;
11118
11119 gdb::unique_xmalloc_ptr<char> absolute_name;
11120 desc = openp (search_path, flags, file_name,
11121 O_RDONLY | O_BINARY, &absolute_name);
11122 if (desc < 0)
11123 return NULL;
11124
11125 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11126 gnutarget, desc));
11127 if (sym_bfd == NULL)
11128 return NULL;
11129 bfd_set_cacheable (sym_bfd.get (), 1);
11130
11131 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11132 return NULL;
11133
11134 /* Success. Record the bfd as having been included by the objfile's bfd.
11135 This is important because things like demangled_names_hash lives in the
11136 objfile's per_bfd space and may have references to things like symbol
11137 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11138 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
11139
11140 return sym_bfd;
11141 }
11142
11143 /* Try to open DWO file FILE_NAME.
11144 COMP_DIR is the DW_AT_comp_dir attribute.
11145 The result is the bfd handle of the file.
11146 If there is a problem finding or opening the file, return NULL.
11147 Upon success, the canonicalized path of the file is stored in the bfd,
11148 same as symfile_bfd_open. */
11149
11150 static gdb_bfd_ref_ptr
11151 open_dwo_file (dwarf2_per_objfile *per_objfile,
11152 const char *file_name, const char *comp_dir)
11153 {
11154 if (IS_ABSOLUTE_PATH (file_name))
11155 return try_open_dwop_file (per_objfile, file_name,
11156 0 /*is_dwp*/, 0 /*search_cwd*/);
11157
11158 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11159
11160 if (comp_dir != NULL)
11161 {
11162 gdb::unique_xmalloc_ptr<char> path_to_try
11163 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
11164
11165 /* NOTE: If comp_dir is a relative path, this will also try the
11166 search path, which seems useful. */
11167 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
11168 0 /*is_dwp*/,
11169 1 /*search_cwd*/));
11170 if (abfd != NULL)
11171 return abfd;
11172 }
11173
11174 /* That didn't work, try debug-file-directory, which, despite its name,
11175 is a list of paths. */
11176
11177 if (debug_file_directory.empty ())
11178 return NULL;
11179
11180 return try_open_dwop_file (per_objfile, file_name,
11181 0 /*is_dwp*/, 1 /*search_cwd*/);
11182 }
11183
11184 /* This function is mapped across the sections and remembers the offset and
11185 size of each of the DWO debugging sections we are interested in. */
11186
11187 static void
11188 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
11189 asection *sectp, dwo_sections *dwo_sections)
11190 {
11191 const struct dwop_section_names *names = &dwop_section_names;
11192
11193 struct dwarf2_section_info *dw_sect = nullptr;
11194
11195 if (names->abbrev_dwo.matches (sectp->name))
11196 dw_sect = &dwo_sections->abbrev;
11197 else if (names->info_dwo.matches (sectp->name))
11198 dw_sect = &dwo_sections->info;
11199 else if (names->line_dwo.matches (sectp->name))
11200 dw_sect = &dwo_sections->line;
11201 else if (names->loc_dwo.matches (sectp->name))
11202 dw_sect = &dwo_sections->loc;
11203 else if (names->loclists_dwo.matches (sectp->name))
11204 dw_sect = &dwo_sections->loclists;
11205 else if (names->macinfo_dwo.matches (sectp->name))
11206 dw_sect = &dwo_sections->macinfo;
11207 else if (names->macro_dwo.matches (sectp->name))
11208 dw_sect = &dwo_sections->macro;
11209 else if (names->rnglists_dwo.matches (sectp->name))
11210 dw_sect = &dwo_sections->rnglists;
11211 else if (names->str_dwo.matches (sectp->name))
11212 dw_sect = &dwo_sections->str;
11213 else if (names->str_offsets_dwo.matches (sectp->name))
11214 dw_sect = &dwo_sections->str_offsets;
11215 else if (names->types_dwo.matches (sectp->name))
11216 {
11217 struct dwarf2_section_info type_section;
11218
11219 memset (&type_section, 0, sizeof (type_section));
11220 dwo_sections->types.push_back (type_section);
11221 dw_sect = &dwo_sections->types.back ();
11222 }
11223
11224 if (dw_sect != nullptr)
11225 {
11226 dw_sect->s.section = sectp;
11227 dw_sect->size = bfd_section_size (sectp);
11228 dw_sect->read (objfile);
11229 }
11230 }
11231
11232 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11233 by PER_CU. This is for the non-DWP case.
11234 The result is NULL if DWO_NAME can't be found. */
11235
11236 static struct dwo_file *
11237 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
11238 const char *comp_dir)
11239 {
11240 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11241
11242 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
11243 if (dbfd == NULL)
11244 {
11245 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
11246
11247 return NULL;
11248 }
11249
11250 dwo_file_up dwo_file (new struct dwo_file);
11251 dwo_file->dwo_name = dwo_name;
11252 dwo_file->comp_dir = comp_dir;
11253 dwo_file->dbfd = std::move (dbfd);
11254
11255 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
11256 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
11257 sec, &dwo_file->sections);
11258
11259 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
11260 dwo_file->cus);
11261
11262 if (cu->per_cu->dwarf_version < 5)
11263 {
11264 create_debug_types_hash_table (per_objfile, dwo_file.get (),
11265 dwo_file->sections.types, dwo_file->tus);
11266 }
11267 else
11268 {
11269 create_debug_type_hash_table (per_objfile, dwo_file.get (),
11270 &dwo_file->sections.info, dwo_file->tus,
11271 rcuh_kind::COMPILE);
11272 }
11273
11274 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
11275
11276 return dwo_file.release ();
11277 }
11278
11279 /* This function is mapped across the sections and remembers the offset and
11280 size of each of the DWP debugging sections common to version 1 and 2 that
11281 we are interested in. */
11282
11283 static void
11284 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
11285 asection *sectp, dwp_file *dwp_file)
11286 {
11287 const struct dwop_section_names *names = &dwop_section_names;
11288 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11289
11290 /* Record the ELF section number for later lookup: this is what the
11291 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11292 gdb_assert (elf_section_nr < dwp_file->num_sections);
11293 dwp_file->elf_sections[elf_section_nr] = sectp;
11294
11295 /* Look for specific sections that we need. */
11296 struct dwarf2_section_info *dw_sect = nullptr;
11297 if (names->str_dwo.matches (sectp->name))
11298 dw_sect = &dwp_file->sections.str;
11299 else if (names->cu_index.matches (sectp->name))
11300 dw_sect = &dwp_file->sections.cu_index;
11301 else if (names->tu_index.matches (sectp->name))
11302 dw_sect = &dwp_file->sections.tu_index;
11303
11304 if (dw_sect != nullptr)
11305 {
11306 dw_sect->s.section = sectp;
11307 dw_sect->size = bfd_section_size (sectp);
11308 dw_sect->read (objfile);
11309 }
11310 }
11311
11312 /* This function is mapped across the sections and remembers the offset and
11313 size of each of the DWP version 2 debugging sections that we are interested
11314 in. This is split into a separate function because we don't know if we
11315 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11316
11317 static void
11318 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
11319 asection *sectp, void *dwp_file_ptr)
11320 {
11321 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11322 const struct dwop_section_names *names = &dwop_section_names;
11323 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11324
11325 /* Record the ELF section number for later lookup: this is what the
11326 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11327 gdb_assert (elf_section_nr < dwp_file->num_sections);
11328 dwp_file->elf_sections[elf_section_nr] = sectp;
11329
11330 /* Look for specific sections that we need. */
11331 struct dwarf2_section_info *dw_sect = nullptr;
11332 if (names->abbrev_dwo.matches (sectp->name))
11333 dw_sect = &dwp_file->sections.abbrev;
11334 else if (names->info_dwo.matches (sectp->name))
11335 dw_sect = &dwp_file->sections.info;
11336 else if (names->line_dwo.matches (sectp->name))
11337 dw_sect = &dwp_file->sections.line;
11338 else if (names->loc_dwo.matches (sectp->name))
11339 dw_sect = &dwp_file->sections.loc;
11340 else if (names->macinfo_dwo.matches (sectp->name))
11341 dw_sect = &dwp_file->sections.macinfo;
11342 else if (names->macro_dwo.matches (sectp->name))
11343 dw_sect = &dwp_file->sections.macro;
11344 else if (names->str_offsets_dwo.matches (sectp->name))
11345 dw_sect = &dwp_file->sections.str_offsets;
11346 else if (names->types_dwo.matches (sectp->name))
11347 dw_sect = &dwp_file->sections.types;
11348
11349 if (dw_sect != nullptr)
11350 {
11351 dw_sect->s.section = sectp;
11352 dw_sect->size = bfd_section_size (sectp);
11353 dw_sect->read (objfile);
11354 }
11355 }
11356
11357 /* This function is mapped across the sections and remembers the offset and
11358 size of each of the DWP version 5 debugging sections that we are interested
11359 in. This is split into a separate function because we don't know if we
11360 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11361
11362 static void
11363 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
11364 asection *sectp, void *dwp_file_ptr)
11365 {
11366 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11367 const struct dwop_section_names *names = &dwop_section_names;
11368 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11369
11370 /* Record the ELF section number for later lookup: this is what the
11371 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11372 gdb_assert (elf_section_nr < dwp_file->num_sections);
11373 dwp_file->elf_sections[elf_section_nr] = sectp;
11374
11375 /* Look for specific sections that we need. */
11376 struct dwarf2_section_info *dw_sect = nullptr;
11377 if (names->abbrev_dwo.matches (sectp->name))
11378 dw_sect = &dwp_file->sections.abbrev;
11379 else if (names->info_dwo.matches (sectp->name))
11380 dw_sect = &dwp_file->sections.info;
11381 else if (names->line_dwo.matches (sectp->name))
11382 dw_sect = &dwp_file->sections.line;
11383 else if (names->loclists_dwo.matches (sectp->name))
11384 dw_sect = &dwp_file->sections.loclists;
11385 else if (names->macro_dwo.matches (sectp->name))
11386 dw_sect = &dwp_file->sections.macro;
11387 else if (names->rnglists_dwo.matches (sectp->name))
11388 dw_sect = &dwp_file->sections.rnglists;
11389 else if (names->str_offsets_dwo.matches (sectp->name))
11390 dw_sect = &dwp_file->sections.str_offsets;
11391
11392 if (dw_sect != nullptr)
11393 {
11394 dw_sect->s.section = sectp;
11395 dw_sect->size = bfd_section_size (sectp);
11396 dw_sect->read (objfile);
11397 }
11398 }
11399
11400 /* Hash function for dwp_file loaded CUs/TUs. */
11401
11402 static hashval_t
11403 hash_dwp_loaded_cutus (const void *item)
11404 {
11405 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11406
11407 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11408 return dwo_unit->signature;
11409 }
11410
11411 /* Equality function for dwp_file loaded CUs/TUs. */
11412
11413 static int
11414 eq_dwp_loaded_cutus (const void *a, const void *b)
11415 {
11416 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11417 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11418
11419 return dua->signature == dub->signature;
11420 }
11421
11422 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11423
11424 static htab_up
11425 allocate_dwp_loaded_cutus_table ()
11426 {
11427 return htab_up (htab_create_alloc (3,
11428 hash_dwp_loaded_cutus,
11429 eq_dwp_loaded_cutus,
11430 NULL, xcalloc, xfree));
11431 }
11432
11433 /* Try to open DWP file FILE_NAME.
11434 The result is the bfd handle of the file.
11435 If there is a problem finding or opening the file, return NULL.
11436 Upon success, the canonicalized path of the file is stored in the bfd,
11437 same as symfile_bfd_open. */
11438
11439 static gdb_bfd_ref_ptr
11440 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
11441 {
11442 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
11443 1 /*is_dwp*/,
11444 1 /*search_cwd*/));
11445 if (abfd != NULL)
11446 return abfd;
11447
11448 /* Work around upstream bug 15652.
11449 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11450 [Whether that's a "bug" is debatable, but it is getting in our way.]
11451 We have no real idea where the dwp file is, because gdb's realpath-ing
11452 of the executable's path may have discarded the needed info.
11453 [IWBN if the dwp file name was recorded in the executable, akin to
11454 .gnu_debuglink, but that doesn't exist yet.]
11455 Strip the directory from FILE_NAME and search again. */
11456 if (!debug_file_directory.empty ())
11457 {
11458 /* Don't implicitly search the current directory here.
11459 If the user wants to search "." to handle this case,
11460 it must be added to debug-file-directory. */
11461 return try_open_dwop_file (per_objfile, lbasename (file_name),
11462 1 /*is_dwp*/,
11463 0 /*search_cwd*/);
11464 }
11465
11466 return NULL;
11467 }
11468
11469 /* Initialize the use of the DWP file for the current objfile.
11470 By convention the name of the DWP file is ${objfile}.dwp.
11471 The result is NULL if it can't be found. */
11472
11473 static std::unique_ptr<struct dwp_file>
11474 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
11475 {
11476 struct objfile *objfile = per_objfile->objfile;
11477
11478 /* Try to find first .dwp for the binary file before any symbolic links
11479 resolving. */
11480
11481 /* If the objfile is a debug file, find the name of the real binary
11482 file and get the name of dwp file from there. */
11483 std::string dwp_name;
11484 if (objfile->separate_debug_objfile_backlink != NULL)
11485 {
11486 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11487 const char *backlink_basename = lbasename (backlink->original_name);
11488
11489 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11490 }
11491 else
11492 dwp_name = objfile->original_name;
11493
11494 dwp_name += ".dwp";
11495
11496 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
11497 if (dbfd == NULL
11498 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11499 {
11500 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11501 dwp_name = objfile_name (objfile);
11502 dwp_name += ".dwp";
11503 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
11504 }
11505
11506 if (dbfd == NULL)
11507 {
11508 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
11509
11510 return std::unique_ptr<dwp_file> ();
11511 }
11512
11513 const char *name = bfd_get_filename (dbfd.get ());
11514 std::unique_ptr<struct dwp_file> dwp_file
11515 (new struct dwp_file (name, std::move (dbfd)));
11516
11517 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
11518 dwp_file->elf_sections =
11519 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
11520 dwp_file->num_sections, asection *);
11521
11522 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11523 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11524 dwp_file.get ());
11525
11526 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
11527
11528 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
11529
11530 /* The DWP file version is stored in the hash table. Oh well. */
11531 if (dwp_file->cus && dwp_file->tus
11532 && dwp_file->cus->version != dwp_file->tus->version)
11533 {
11534 /* Technically speaking, we should try to limp along, but this is
11535 pretty bizarre. We use pulongest here because that's the established
11536 portability solution (e.g, we cannot use %u for uint32_t). */
11537 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11538 " TU version %s [in DWP file %s]"),
11539 pulongest (dwp_file->cus->version),
11540 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11541 }
11542
11543 if (dwp_file->cus)
11544 dwp_file->version = dwp_file->cus->version;
11545 else if (dwp_file->tus)
11546 dwp_file->version = dwp_file->tus->version;
11547 else
11548 dwp_file->version = 2;
11549
11550 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11551 {
11552 if (dwp_file->version == 2)
11553 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11554 dwp_file.get ());
11555 else
11556 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11557 dwp_file.get ());
11558 }
11559
11560 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
11561 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
11562
11563 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
11564 dwarf_read_debug_printf (" %s CUs, %s TUs",
11565 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11566 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11567
11568 return dwp_file;
11569 }
11570
11571 /* Wrapper around open_and_init_dwp_file, only open it once. */
11572
11573 static struct dwp_file *
11574 get_dwp_file (dwarf2_per_objfile *per_objfile)
11575 {
11576 if (!per_objfile->per_bfd->dwp_checked)
11577 {
11578 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
11579 per_objfile->per_bfd->dwp_checked = 1;
11580 }
11581 return per_objfile->per_bfd->dwp_file.get ();
11582 }
11583
11584 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11585 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11586 or in the DWP file for the objfile, referenced by THIS_UNIT.
11587 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11588 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11589
11590 This is called, for example, when wanting to read a variable with a
11591 complex location. Therefore we don't want to do file i/o for every call.
11592 Therefore we don't want to look for a DWO file on every call.
11593 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11594 then we check if we've already seen DWO_NAME, and only THEN do we check
11595 for a DWO file.
11596
11597 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11598 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11599
11600 static struct dwo_unit *
11601 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11602 ULONGEST signature, int is_debug_types)
11603 {
11604 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11605 struct objfile *objfile = per_objfile->objfile;
11606 const char *kind = is_debug_types ? "TU" : "CU";
11607 void **dwo_file_slot;
11608 struct dwo_file *dwo_file;
11609 struct dwp_file *dwp_file;
11610
11611 /* First see if there's a DWP file.
11612 If we have a DWP file but didn't find the DWO inside it, don't
11613 look for the original DWO file. It makes gdb behave differently
11614 depending on whether one is debugging in the build tree. */
11615
11616 dwp_file = get_dwp_file (per_objfile);
11617 if (dwp_file != NULL)
11618 {
11619 const struct dwp_hash_table *dwp_htab =
11620 is_debug_types ? dwp_file->tus : dwp_file->cus;
11621
11622 if (dwp_htab != NULL)
11623 {
11624 struct dwo_unit *dwo_cutu =
11625 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
11626 is_debug_types);
11627
11628 if (dwo_cutu != NULL)
11629 {
11630 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11631 kind, hex_string (signature),
11632 host_address_to_string (dwo_cutu));
11633
11634 return dwo_cutu;
11635 }
11636 }
11637 }
11638 else
11639 {
11640 /* No DWP file, look for the DWO file. */
11641
11642 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
11643 if (*dwo_file_slot == NULL)
11644 {
11645 /* Read in the file and build a table of the CUs/TUs it contains. */
11646 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
11647 }
11648 /* NOTE: This will be NULL if unable to open the file. */
11649 dwo_file = (struct dwo_file *) *dwo_file_slot;
11650
11651 if (dwo_file != NULL)
11652 {
11653 struct dwo_unit *dwo_cutu = NULL;
11654
11655 if (is_debug_types && dwo_file->tus)
11656 {
11657 struct dwo_unit find_dwo_cutu;
11658
11659 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11660 find_dwo_cutu.signature = signature;
11661 dwo_cutu
11662 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
11663 &find_dwo_cutu);
11664 }
11665 else if (!is_debug_types && dwo_file->cus)
11666 {
11667 struct dwo_unit find_dwo_cutu;
11668
11669 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11670 find_dwo_cutu.signature = signature;
11671 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
11672 &find_dwo_cutu);
11673 }
11674
11675 if (dwo_cutu != NULL)
11676 {
11677 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11678 kind, dwo_name, hex_string (signature),
11679 host_address_to_string (dwo_cutu));
11680
11681 return dwo_cutu;
11682 }
11683 }
11684 }
11685
11686 /* We didn't find it. This could mean a dwo_id mismatch, or
11687 someone deleted the DWO/DWP file, or the search path isn't set up
11688 correctly to find the file. */
11689
11690 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11691 kind, dwo_name, hex_string (signature));
11692
11693 /* This is a warning and not a complaint because it can be caused by
11694 pilot error (e.g., user accidentally deleting the DWO). */
11695 {
11696 /* Print the name of the DWP file if we looked there, helps the user
11697 better diagnose the problem. */
11698 std::string dwp_text;
11699
11700 if (dwp_file != NULL)
11701 dwp_text = string_printf (" [in DWP file %s]",
11702 lbasename (dwp_file->name));
11703
11704 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11705 " [in module %s]"),
11706 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
11707 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
11708 }
11709 return NULL;
11710 }
11711
11712 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11713 See lookup_dwo_cutu_unit for details. */
11714
11715 static struct dwo_unit *
11716 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11717 ULONGEST signature)
11718 {
11719 gdb_assert (!cu->per_cu->is_debug_types);
11720
11721 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
11722 }
11723
11724 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11725 See lookup_dwo_cutu_unit for details. */
11726
11727 static struct dwo_unit *
11728 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
11729 {
11730 gdb_assert (cu->per_cu->is_debug_types);
11731
11732 signatured_type *sig_type = (signatured_type *) cu->per_cu;
11733
11734 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
11735 }
11736
11737 /* Traversal function for queue_and_load_all_dwo_tus. */
11738
11739 static int
11740 queue_and_load_dwo_tu (void **slot, void *info)
11741 {
11742 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11743 dwarf2_cu *cu = (dwarf2_cu *) info;
11744 ULONGEST signature = dwo_unit->signature;
11745 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
11746
11747 if (sig_type != NULL)
11748 {
11749 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11750 a real dependency of PER_CU on SIG_TYPE. That is detected later
11751 while processing PER_CU. */
11752 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
11753 cu->per_cu->lang))
11754 load_full_type_unit (sig_type, cu->per_objfile);
11755 cu->per_cu->imported_symtabs_push (sig_type);
11756 }
11757
11758 return 1;
11759 }
11760
11761 /* Queue all TUs contained in the DWO of CU to be read in.
11762 The DWO may have the only definition of the type, though it may not be
11763 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11764 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11765
11766 static void
11767 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
11768 {
11769 struct dwo_unit *dwo_unit;
11770 struct dwo_file *dwo_file;
11771
11772 gdb_assert (cu != nullptr);
11773 gdb_assert (!cu->per_cu->is_debug_types);
11774 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
11775
11776 dwo_unit = cu->dwo_unit;
11777 gdb_assert (dwo_unit != NULL);
11778
11779 dwo_file = dwo_unit->dwo_file;
11780 if (dwo_file->tus != NULL)
11781 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
11782 }
11783
11784 /* Read in various DIEs. */
11785
11786 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11787 Inherit only the children of the DW_AT_abstract_origin DIE not being
11788 already referenced by DW_AT_abstract_origin from the children of the
11789 current DIE. */
11790
11791 static void
11792 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11793 {
11794 struct die_info *child_die;
11795 sect_offset *offsetp;
11796 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11797 struct die_info *origin_die;
11798 /* Iterator of the ORIGIN_DIE children. */
11799 struct die_info *origin_child_die;
11800 struct attribute *attr;
11801 struct dwarf2_cu *origin_cu;
11802 struct pending **origin_previous_list_in_scope;
11803
11804 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11805 if (!attr)
11806 return;
11807
11808 /* Note that following die references may follow to a die in a
11809 different cu. */
11810
11811 origin_cu = cu;
11812 origin_die = follow_die_ref (die, attr, &origin_cu);
11813
11814 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11815 symbols in. */
11816 origin_previous_list_in_scope = origin_cu->list_in_scope;
11817 origin_cu->list_in_scope = cu->list_in_scope;
11818
11819 if (die->tag != origin_die->tag
11820 && !(die->tag == DW_TAG_inlined_subroutine
11821 && origin_die->tag == DW_TAG_subprogram))
11822 complaint (_("DIE %s and its abstract origin %s have different tags"),
11823 sect_offset_str (die->sect_off),
11824 sect_offset_str (origin_die->sect_off));
11825
11826 /* Find if the concrete and abstract trees are structurally the
11827 same. This is a shallow traversal and it is not bullet-proof;
11828 the compiler can trick the debugger into believing that the trees
11829 are isomorphic, whereas they actually are not. However, the
11830 likelyhood of this happening is pretty low, and a full-fledged
11831 check would be an overkill. */
11832 bool are_isomorphic = true;
11833 die_info *concrete_child = die->child;
11834 die_info *abstract_child = origin_die->child;
11835 while (concrete_child != nullptr || abstract_child != nullptr)
11836 {
11837 if (concrete_child == nullptr
11838 || abstract_child == nullptr
11839 || concrete_child->tag != abstract_child->tag)
11840 {
11841 are_isomorphic = false;
11842 break;
11843 }
11844
11845 concrete_child = concrete_child->sibling;
11846 abstract_child = abstract_child->sibling;
11847 }
11848
11849 /* Walk the origin's children in parallel to the concrete children.
11850 This helps match an origin child in case the debug info misses
11851 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11852 origin tree may not have the same tree structure as the concrete
11853 DIE, though. */
11854 die_info *corresponding_abstract_child
11855 = are_isomorphic ? origin_die->child : nullptr;
11856
11857 std::vector<sect_offset> offsets;
11858
11859 for (child_die = die->child;
11860 child_die && child_die->tag;
11861 child_die = child_die->sibling)
11862 {
11863 struct die_info *child_origin_die;
11864 struct dwarf2_cu *child_origin_cu;
11865
11866 /* We are trying to process concrete instance entries:
11867 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11868 it's not relevant to our analysis here. i.e. detecting DIEs that are
11869 present in the abstract instance but not referenced in the concrete
11870 one. */
11871 if (child_die->tag == DW_TAG_call_site
11872 || child_die->tag == DW_TAG_GNU_call_site)
11873 {
11874 if (are_isomorphic)
11875 corresponding_abstract_child
11876 = corresponding_abstract_child->sibling;
11877 continue;
11878 }
11879
11880 /* For each CHILD_DIE, find the corresponding child of
11881 ORIGIN_DIE. If there is more than one layer of
11882 DW_AT_abstract_origin, follow them all; there shouldn't be,
11883 but GCC versions at least through 4.4 generate this (GCC PR
11884 40573). */
11885 child_origin_die = child_die;
11886 child_origin_cu = cu;
11887 while (1)
11888 {
11889 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11890 child_origin_cu);
11891 if (attr == NULL)
11892 break;
11893 child_origin_die = follow_die_ref (child_origin_die, attr,
11894 &child_origin_cu);
11895 }
11896
11897 /* If missing DW_AT_abstract_origin, try the corresponding child
11898 of the origin. Clang emits such lexical scopes. */
11899 if (child_origin_die == child_die
11900 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
11901 && are_isomorphic
11902 && child_die->tag == DW_TAG_lexical_block)
11903 child_origin_die = corresponding_abstract_child;
11904
11905 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11906 counterpart may exist. */
11907 if (child_origin_die != child_die)
11908 {
11909 if (child_die->tag != child_origin_die->tag
11910 && !(child_die->tag == DW_TAG_inlined_subroutine
11911 && child_origin_die->tag == DW_TAG_subprogram))
11912 complaint (_("Child DIE %s and its abstract origin %s have "
11913 "different tags"),
11914 sect_offset_str (child_die->sect_off),
11915 sect_offset_str (child_origin_die->sect_off));
11916 if (child_origin_die->parent != origin_die)
11917 complaint (_("Child DIE %s and its abstract origin %s have "
11918 "different parents"),
11919 sect_offset_str (child_die->sect_off),
11920 sect_offset_str (child_origin_die->sect_off));
11921 else
11922 offsets.push_back (child_origin_die->sect_off);
11923 }
11924
11925 if (are_isomorphic)
11926 corresponding_abstract_child = corresponding_abstract_child->sibling;
11927 }
11928 std::sort (offsets.begin (), offsets.end ());
11929 sect_offset *offsets_end = offsets.data () + offsets.size ();
11930 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
11931 if (offsetp[-1] == *offsetp)
11932 complaint (_("Multiple children of DIE %s refer "
11933 "to DIE %s as their abstract origin"),
11934 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
11935
11936 offsetp = offsets.data ();
11937 origin_child_die = origin_die->child;
11938 while (origin_child_die && origin_child_die->tag)
11939 {
11940 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11941 while (offsetp < offsets_end
11942 && *offsetp < origin_child_die->sect_off)
11943 offsetp++;
11944 if (offsetp >= offsets_end
11945 || *offsetp > origin_child_die->sect_off)
11946 {
11947 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11948 Check whether we're already processing ORIGIN_CHILD_DIE.
11949 This can happen with mutually referenced abstract_origins.
11950 PR 16581. */
11951 if (!origin_child_die->in_process)
11952 process_die (origin_child_die, origin_cu);
11953 }
11954 origin_child_die = origin_child_die->sibling;
11955 }
11956 origin_cu->list_in_scope = origin_previous_list_in_scope;
11957
11958 if (cu != origin_cu)
11959 compute_delayed_physnames (origin_cu);
11960 }
11961
11962 static void
11963 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11964 {
11965 struct objfile *objfile = cu->per_objfile->objfile;
11966 struct gdbarch *gdbarch = objfile->arch ();
11967 struct context_stack *newobj;
11968 CORE_ADDR lowpc;
11969 CORE_ADDR highpc;
11970 struct die_info *child_die;
11971 struct attribute *attr, *call_line, *call_file;
11972 const char *name;
11973 CORE_ADDR baseaddr;
11974 struct block *block;
11975 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11976 std::vector<struct symbol *> template_args;
11977 struct template_symbol *templ_func = NULL;
11978
11979 if (inlined_func)
11980 {
11981 /* If we do not have call site information, we can't show the
11982 caller of this inlined function. That's too confusing, so
11983 only use the scope for local variables. */
11984 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11985 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11986 if (call_line == NULL || call_file == NULL)
11987 {
11988 read_lexical_block_scope (die, cu);
11989 return;
11990 }
11991 }
11992
11993 baseaddr = objfile->text_section_offset ();
11994
11995 name = dwarf2_name (die, cu);
11996
11997 /* Ignore functions with missing or empty names. These are actually
11998 illegal according to the DWARF standard. */
11999 if (name == NULL)
12000 {
12001 complaint (_("missing name for subprogram DIE at %s"),
12002 sect_offset_str (die->sect_off));
12003 return;
12004 }
12005
12006 /* Ignore functions with missing or invalid low and high pc attributes. */
12007 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
12008 <= PC_BOUNDS_INVALID)
12009 {
12010 attr = dwarf2_attr (die, DW_AT_external, cu);
12011 if (attr == nullptr || !attr->as_boolean ())
12012 complaint (_("cannot get low and high bounds "
12013 "for subprogram DIE at %s"),
12014 sect_offset_str (die->sect_off));
12015 return;
12016 }
12017
12018 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12019 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12020
12021 /* If we have any template arguments, then we must allocate a
12022 different sort of symbol. */
12023 for (child_die = die->child; child_die; child_die = child_die->sibling)
12024 {
12025 if (child_die->tag == DW_TAG_template_type_param
12026 || child_die->tag == DW_TAG_template_value_param)
12027 {
12028 templ_func = new (&objfile->objfile_obstack) template_symbol;
12029 templ_func->subclass = SYMBOL_TEMPLATE;
12030 break;
12031 }
12032 }
12033
12034 gdb_assert (cu->get_builder () != nullptr);
12035 newobj = cu->get_builder ()->push_context (0, lowpc);
12036 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12037 (struct symbol *) templ_func);
12038
12039 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12040 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12041 cu->per_cu->lang);
12042
12043 /* If there is a location expression for DW_AT_frame_base, record
12044 it. */
12045 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12046 if (attr != nullptr)
12047 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12048
12049 /* If there is a location for the static link, record it. */
12050 newobj->static_link = NULL;
12051 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12052 if (attr != nullptr)
12053 {
12054 newobj->static_link
12055 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12056 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12057 cu->addr_type ());
12058 }
12059
12060 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12061
12062 if (die->child != NULL)
12063 {
12064 child_die = die->child;
12065 while (child_die && child_die->tag)
12066 {
12067 if (child_die->tag == DW_TAG_template_type_param
12068 || child_die->tag == DW_TAG_template_value_param)
12069 {
12070 struct symbol *arg = new_symbol (child_die, NULL, cu);
12071
12072 if (arg != NULL)
12073 template_args.push_back (arg);
12074 }
12075 else
12076 process_die (child_die, cu);
12077 child_die = child_die->sibling;
12078 }
12079 }
12080
12081 inherit_abstract_dies (die, cu);
12082
12083 /* If we have a DW_AT_specification, we might need to import using
12084 directives from the context of the specification DIE. See the
12085 comment in determine_prefix. */
12086 if (cu->per_cu->lang == language_cplus
12087 && dwarf2_attr (die, DW_AT_specification, cu))
12088 {
12089 struct dwarf2_cu *spec_cu = cu;
12090 struct die_info *spec_die = die_specification (die, &spec_cu);
12091
12092 while (spec_die)
12093 {
12094 child_die = spec_die->child;
12095 while (child_die && child_die->tag)
12096 {
12097 if (child_die->tag == DW_TAG_imported_module)
12098 process_die (child_die, spec_cu);
12099 child_die = child_die->sibling;
12100 }
12101
12102 /* In some cases, GCC generates specification DIEs that
12103 themselves contain DW_AT_specification attributes. */
12104 spec_die = die_specification (spec_die, &spec_cu);
12105 }
12106 }
12107
12108 struct context_stack cstk = cu->get_builder ()->pop_context ();
12109 /* Make a block for the local symbols within. */
12110 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12111 cstk.static_link, lowpc, highpc);
12112
12113 /* For C++, set the block's scope. */
12114 if ((cu->per_cu->lang == language_cplus
12115 || cu->per_cu->lang == language_fortran
12116 || cu->per_cu->lang == language_d
12117 || cu->per_cu->lang == language_rust)
12118 && cu->processing_has_namespace_info)
12119 block_set_scope (block, determine_prefix (die, cu),
12120 &objfile->objfile_obstack);
12121
12122 /* If we have address ranges, record them. */
12123 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12124
12125 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12126
12127 /* Attach template arguments to function. */
12128 if (!template_args.empty ())
12129 {
12130 gdb_assert (templ_func != NULL);
12131
12132 templ_func->n_template_arguments = template_args.size ();
12133 templ_func->template_arguments
12134 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12135 templ_func->n_template_arguments);
12136 memcpy (templ_func->template_arguments,
12137 template_args.data (),
12138 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12139
12140 /* Make sure that the symtab is set on the new symbols. Even
12141 though they don't appear in this symtab directly, other parts
12142 of gdb assume that symbols do, and this is reasonably
12143 true. */
12144 for (symbol *sym : template_args)
12145 symbol_set_symtab (sym, symbol_symtab (templ_func));
12146 }
12147
12148 /* In C++, we can have functions nested inside functions (e.g., when
12149 a function declares a class that has methods). This means that
12150 when we finish processing a function scope, we may need to go
12151 back to building a containing block's symbol lists. */
12152 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12153 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12154
12155 /* If we've finished processing a top-level function, subsequent
12156 symbols go in the file symbol list. */
12157 if (cu->get_builder ()->outermost_context_p ())
12158 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12159 }
12160
12161 /* Process all the DIES contained within a lexical block scope. Start
12162 a new scope, process the dies, and then close the scope. */
12163
12164 static void
12165 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12166 {
12167 struct objfile *objfile = cu->per_objfile->objfile;
12168 struct gdbarch *gdbarch = objfile->arch ();
12169 CORE_ADDR lowpc, highpc;
12170 struct die_info *child_die;
12171 CORE_ADDR baseaddr;
12172
12173 baseaddr = objfile->text_section_offset ();
12174
12175 /* Ignore blocks with missing or invalid low and high pc attributes. */
12176 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12177 as multiple lexical blocks? Handling children in a sane way would
12178 be nasty. Might be easier to properly extend generic blocks to
12179 describe ranges. */
12180 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
12181 {
12182 case PC_BOUNDS_NOT_PRESENT:
12183 /* DW_TAG_lexical_block has no attributes, process its children as if
12184 there was no wrapping by that DW_TAG_lexical_block.
12185 GCC does no longer produces such DWARF since GCC r224161. */
12186 for (child_die = die->child;
12187 child_die != NULL && child_die->tag;
12188 child_die = child_die->sibling)
12189 {
12190 /* We might already be processing this DIE. This can happen
12191 in an unusual circumstance -- where a subroutine A
12192 appears lexically in another subroutine B, but A actually
12193 inlines B. The recursion is broken here, rather than in
12194 inherit_abstract_dies, because it seems better to simply
12195 drop concrete children here. */
12196 if (!child_die->in_process)
12197 process_die (child_die, cu);
12198 }
12199 return;
12200 case PC_BOUNDS_INVALID:
12201 return;
12202 }
12203 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12204 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12205
12206 cu->get_builder ()->push_context (0, lowpc);
12207 if (die->child != NULL)
12208 {
12209 child_die = die->child;
12210 while (child_die && child_die->tag)
12211 {
12212 process_die (child_die, cu);
12213 child_die = child_die->sibling;
12214 }
12215 }
12216 inherit_abstract_dies (die, cu);
12217 struct context_stack cstk = cu->get_builder ()->pop_context ();
12218
12219 if (*cu->get_builder ()->get_local_symbols () != NULL
12220 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12221 {
12222 struct block *block
12223 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12224 cstk.start_addr, highpc);
12225
12226 /* Note that recording ranges after traversing children, as we
12227 do here, means that recording a parent's ranges entails
12228 walking across all its children's ranges as they appear in
12229 the address map, which is quadratic behavior.
12230
12231 It would be nicer to record the parent's ranges before
12232 traversing its children, simply overriding whatever you find
12233 there. But since we don't even decide whether to create a
12234 block until after we've traversed its children, that's hard
12235 to do. */
12236 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12237 }
12238 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12239 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12240 }
12241
12242 static void dwarf2_ranges_read_low_addrs (unsigned offset,
12243 struct dwarf2_cu *cu,
12244 dwarf_tag tag,
12245 std::vector<CORE_ADDR> &result);
12246
12247 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12248
12249 static void
12250 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12251 {
12252 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12253 struct objfile *objfile = per_objfile->objfile;
12254 struct gdbarch *gdbarch = objfile->arch ();
12255 CORE_ADDR pc, baseaddr;
12256 struct attribute *attr;
12257 void **slot;
12258 int nparams;
12259 struct die_info *child_die;
12260
12261 baseaddr = objfile->text_section_offset ();
12262
12263 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12264 if (attr == NULL)
12265 {
12266 /* This was a pre-DWARF-5 GNU extension alias
12267 for DW_AT_call_return_pc. */
12268 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12269 }
12270 if (!attr)
12271 {
12272 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12273 "DIE %s [in module %s]"),
12274 sect_offset_str (die->sect_off), objfile_name (objfile));
12275 return;
12276 }
12277 pc = attr->as_address () + baseaddr;
12278 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12279 pc -= baseaddr;
12280
12281 if (cu->call_site_htab == NULL)
12282 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
12283 call_site::eq, NULL,
12284 &objfile->objfile_obstack,
12285 hashtab_obstack_allocate, NULL);
12286 struct call_site call_site_local (pc, nullptr, nullptr);
12287 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12288 if (*slot != NULL)
12289 {
12290 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12291 "DIE %s [in module %s]"),
12292 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
12293 objfile_name (objfile));
12294 return;
12295 }
12296
12297 /* Count parameters at the caller. */
12298
12299 nparams = 0;
12300 for (child_die = die->child; child_die && child_die->tag;
12301 child_die = child_die->sibling)
12302 {
12303 if (child_die->tag != DW_TAG_call_site_parameter
12304 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12305 {
12306 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12307 "DW_TAG_call_site child DIE %s [in module %s]"),
12308 child_die->tag, sect_offset_str (child_die->sect_off),
12309 objfile_name (objfile));
12310 continue;
12311 }
12312
12313 nparams++;
12314 }
12315
12316 struct call_site *call_site
12317 = new (XOBNEWVAR (&objfile->objfile_obstack,
12318 struct call_site,
12319 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
12320 struct call_site (pc, cu->per_cu, per_objfile);
12321 *slot = call_site;
12322
12323 /* We never call the destructor of call_site, so we must ensure it is
12324 trivially destructible. */
12325 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
12326
12327 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12328 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12329 {
12330 struct die_info *func_die;
12331
12332 /* Skip also over DW_TAG_inlined_subroutine. */
12333 for (func_die = die->parent;
12334 func_die && func_die->tag != DW_TAG_subprogram
12335 && func_die->tag != DW_TAG_subroutine_type;
12336 func_die = func_die->parent);
12337
12338 /* DW_AT_call_all_calls is a superset
12339 of DW_AT_call_all_tail_calls. */
12340 if (func_die
12341 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12342 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12343 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12344 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12345 {
12346 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12347 not complete. But keep CALL_SITE for look ups via call_site_htab,
12348 both the initial caller containing the real return address PC and
12349 the final callee containing the current PC of a chain of tail
12350 calls do not need to have the tail call list complete. But any
12351 function candidate for a virtual tail call frame searched via
12352 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12353 determined unambiguously. */
12354 }
12355 else
12356 {
12357 struct type *func_type = NULL;
12358
12359 if (func_die)
12360 func_type = get_die_type (func_die, cu);
12361 if (func_type != NULL)
12362 {
12363 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
12364
12365 /* Enlist this call site to the function. */
12366 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12367 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12368 }
12369 else
12370 complaint (_("Cannot find function owning DW_TAG_call_site "
12371 "DIE %s [in module %s]"),
12372 sect_offset_str (die->sect_off), objfile_name (objfile));
12373 }
12374 }
12375
12376 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12377 if (attr == NULL)
12378 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12379 if (attr == NULL)
12380 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12381 if (attr == NULL)
12382 {
12383 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12384 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12385 }
12386
12387 call_site->target.set_loc_dwarf_block (nullptr);
12388 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
12389 /* Keep NULL DWARF_BLOCK. */;
12390 else if (attr->form_is_block ())
12391 {
12392 struct dwarf2_locexpr_baton *dlbaton;
12393 struct dwarf_block *block = attr->as_block ();
12394
12395 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12396 dlbaton->data = block->data;
12397 dlbaton->size = block->size;
12398 dlbaton->per_objfile = per_objfile;
12399 dlbaton->per_cu = cu->per_cu;
12400
12401 call_site->target.set_loc_dwarf_block (dlbaton);
12402 }
12403 else if (attr->form_is_ref ())
12404 {
12405 struct dwarf2_cu *target_cu = cu;
12406 struct die_info *target_die;
12407
12408 target_die = follow_die_ref (die, attr, &target_cu);
12409 gdb_assert (target_cu->per_objfile->objfile == objfile);
12410
12411 struct attribute *ranges_attr
12412 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
12413
12414 if (die_is_declaration (target_die, target_cu))
12415 {
12416 const char *target_physname;
12417
12418 /* Prefer the mangled name; otherwise compute the demangled one. */
12419 target_physname = dw2_linkage_name (target_die, target_cu);
12420 if (target_physname == NULL)
12421 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12422 if (target_physname == NULL)
12423 complaint (_("DW_AT_call_target target DIE has invalid "
12424 "physname, for referencing DIE %s [in module %s]"),
12425 sect_offset_str (die->sect_off), objfile_name (objfile));
12426 else
12427 call_site->target.set_loc_physname (target_physname);
12428 }
12429 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
12430 {
12431 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
12432 + target_cu->gnu_ranges_base);
12433 std::vector<CORE_ADDR> addresses;
12434 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
12435 target_die->tag, addresses);
12436 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
12437 addresses.size ());
12438 std::copy (addresses.begin (), addresses.end (), saved);
12439 call_site->target.set_loc_array (addresses.size (), saved);
12440 }
12441 else
12442 {
12443 CORE_ADDR lowpc;
12444
12445 /* DW_AT_entry_pc should be preferred. */
12446 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
12447 nullptr, nullptr)
12448 <= PC_BOUNDS_INVALID)
12449 complaint (_("DW_AT_call_target target DIE has invalid "
12450 "low pc, for referencing DIE %s [in module %s]"),
12451 sect_offset_str (die->sect_off), objfile_name (objfile));
12452 else
12453 {
12454 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
12455 - baseaddr);
12456 call_site->target.set_loc_physaddr (lowpc);
12457 }
12458 }
12459 }
12460 else
12461 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12462 "block nor reference, for DIE %s [in module %s]"),
12463 sect_offset_str (die->sect_off), objfile_name (objfile));
12464
12465 for (child_die = die->child;
12466 child_die && child_die->tag;
12467 child_die = child_die->sibling)
12468 {
12469 struct call_site_parameter *parameter;
12470 struct attribute *loc, *origin;
12471
12472 if (child_die->tag != DW_TAG_call_site_parameter
12473 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12474 {
12475 /* Already printed the complaint above. */
12476 continue;
12477 }
12478
12479 gdb_assert (call_site->parameter_count < nparams);
12480 parameter = &call_site->parameter[call_site->parameter_count];
12481
12482 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12483 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12484 register is contained in DW_AT_call_value. */
12485
12486 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12487 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12488 if (origin == NULL)
12489 {
12490 /* This was a pre-DWARF-5 GNU extension alias
12491 for DW_AT_call_parameter. */
12492 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12493 }
12494 if (loc == NULL && origin != NULL && origin->form_is_ref ())
12495 {
12496 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12497
12498 sect_offset sect_off = origin->get_ref_die_offset ();
12499 if (!cu->header.offset_in_cu_p (sect_off))
12500 {
12501 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12502 binding can be done only inside one CU. Such referenced DIE
12503 therefore cannot be even moved to DW_TAG_partial_unit. */
12504 complaint (_("DW_AT_call_parameter offset is not in CU for "
12505 "DW_TAG_call_site child DIE %s [in module %s]"),
12506 sect_offset_str (child_die->sect_off),
12507 objfile_name (objfile));
12508 continue;
12509 }
12510 parameter->u.param_cu_off
12511 = (cu_offset) (sect_off - cu->header.sect_off);
12512 }
12513 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
12514 {
12515 complaint (_("No DW_FORM_block* DW_AT_location for "
12516 "DW_TAG_call_site child DIE %s [in module %s]"),
12517 sect_offset_str (child_die->sect_off), objfile_name (objfile));
12518 continue;
12519 }
12520 else
12521 {
12522 struct dwarf_block *block = loc->as_block ();
12523
12524 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12525 (block->data, &block->data[block->size]);
12526 if (parameter->u.dwarf_reg != -1)
12527 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12528 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
12529 &block->data[block->size],
12530 &parameter->u.fb_offset))
12531 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12532 else
12533 {
12534 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12535 "for DW_FORM_block* DW_AT_location is supported for "
12536 "DW_TAG_call_site child DIE %s "
12537 "[in module %s]"),
12538 sect_offset_str (child_die->sect_off),
12539 objfile_name (objfile));
12540 continue;
12541 }
12542 }
12543
12544 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12545 if (attr == NULL)
12546 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12547 if (attr == NULL || !attr->form_is_block ())
12548 {
12549 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12550 "DW_TAG_call_site child DIE %s [in module %s]"),
12551 sect_offset_str (child_die->sect_off),
12552 objfile_name (objfile));
12553 continue;
12554 }
12555
12556 struct dwarf_block *block = attr->as_block ();
12557 parameter->value = block->data;
12558 parameter->value_size = block->size;
12559
12560 /* Parameters are not pre-cleared by memset above. */
12561 parameter->data_value = NULL;
12562 parameter->data_value_size = 0;
12563 call_site->parameter_count++;
12564
12565 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12566 if (attr == NULL)
12567 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12568 if (attr != nullptr)
12569 {
12570 if (!attr->form_is_block ())
12571 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12572 "DW_TAG_call_site child DIE %s [in module %s]"),
12573 sect_offset_str (child_die->sect_off),
12574 objfile_name (objfile));
12575 else
12576 {
12577 block = attr->as_block ();
12578 parameter->data_value = block->data;
12579 parameter->data_value_size = block->size;
12580 }
12581 }
12582 }
12583 }
12584
12585 /* Helper function for read_variable. If DIE represents a virtual
12586 table, then return the type of the concrete object that is
12587 associated with the virtual table. Otherwise, return NULL. */
12588
12589 static struct type *
12590 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12591 {
12592 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12593 if (attr == NULL)
12594 return NULL;
12595
12596 /* Find the type DIE. */
12597 struct die_info *type_die = NULL;
12598 struct dwarf2_cu *type_cu = cu;
12599
12600 if (attr->form_is_ref ())
12601 type_die = follow_die_ref (die, attr, &type_cu);
12602 if (type_die == NULL)
12603 return NULL;
12604
12605 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12606 return NULL;
12607 return die_containing_type (type_die, type_cu);
12608 }
12609
12610 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12611
12612 static void
12613 read_variable (struct die_info *die, struct dwarf2_cu *cu)
12614 {
12615 struct rust_vtable_symbol *storage = NULL;
12616
12617 if (cu->per_cu->lang == language_rust)
12618 {
12619 struct type *containing_type = rust_containing_type (die, cu);
12620
12621 if (containing_type != NULL)
12622 {
12623 struct objfile *objfile = cu->per_objfile->objfile;
12624
12625 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
12626 storage->concrete_type = containing_type;
12627 storage->subclass = SYMBOL_RUST_VTABLE;
12628 }
12629 }
12630
12631 struct symbol *res = new_symbol (die, NULL, cu, storage);
12632 struct attribute *abstract_origin
12633 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12634 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
12635 if (res == NULL && loc && abstract_origin)
12636 {
12637 /* We have a variable without a name, but with a location and an abstract
12638 origin. This may be a concrete instance of an abstract variable
12639 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12640 later. */
12641 struct dwarf2_cu *origin_cu = cu;
12642 struct die_info *origin_die
12643 = follow_die_ref (die, abstract_origin, &origin_cu);
12644 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12645 per_objfile->per_bfd->abstract_to_concrete
12646 [origin_die->sect_off].push_back (die->sect_off);
12647 }
12648 }
12649
12650 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12651 reading .debug_rnglists.
12652 Callback's type should be:
12653 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12654 Return true if the attributes are present and valid, otherwise,
12655 return false. */
12656
12657 template <typename Callback>
12658 static bool
12659 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12660 dwarf_tag tag, Callback &&callback)
12661 {
12662 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12663 struct objfile *objfile = per_objfile->objfile;
12664 bfd *obfd = objfile->obfd;
12665 /* Base address selection entry. */
12666 gdb::optional<CORE_ADDR> base;
12667 const gdb_byte *buffer;
12668 bool overflow = false;
12669 ULONGEST addr_index;
12670 struct dwarf2_section_info *rnglists_section;
12671
12672 base = cu->base_address;
12673 rnglists_section = cu_debug_rnglists_section (cu, tag);
12674 rnglists_section->read (objfile);
12675
12676 if (offset >= rnglists_section->size)
12677 {
12678 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12679 offset);
12680 return false;
12681 }
12682 buffer = rnglists_section->buffer + offset;
12683
12684 while (1)
12685 {
12686 /* Initialize it due to a false compiler warning. */
12687 CORE_ADDR range_beginning = 0, range_end = 0;
12688 const gdb_byte *buf_end = (rnglists_section->buffer
12689 + rnglists_section->size);
12690 unsigned int bytes_read;
12691
12692 if (buffer == buf_end)
12693 {
12694 overflow = true;
12695 break;
12696 }
12697 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12698 switch (rlet)
12699 {
12700 case DW_RLE_end_of_list:
12701 break;
12702 case DW_RLE_base_address:
12703 if (buffer + cu->header.addr_size > buf_end)
12704 {
12705 overflow = true;
12706 break;
12707 }
12708 base = cu->header.read_address (obfd, buffer, &bytes_read);
12709 buffer += bytes_read;
12710 break;
12711 case DW_RLE_base_addressx:
12712 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12713 buffer += bytes_read;
12714 base = read_addr_index (cu, addr_index);
12715 break;
12716 case DW_RLE_start_length:
12717 if (buffer + cu->header.addr_size > buf_end)
12718 {
12719 overflow = true;
12720 break;
12721 }
12722 range_beginning = cu->header.read_address (obfd, buffer,
12723 &bytes_read);
12724 buffer += bytes_read;
12725 range_end = (range_beginning
12726 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12727 buffer += bytes_read;
12728 if (buffer > buf_end)
12729 {
12730 overflow = true;
12731 break;
12732 }
12733 break;
12734 case DW_RLE_startx_length:
12735 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12736 buffer += bytes_read;
12737 range_beginning = read_addr_index (cu, addr_index);
12738 if (buffer > buf_end)
12739 {
12740 overflow = true;
12741 break;
12742 }
12743 range_end = (range_beginning
12744 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12745 buffer += bytes_read;
12746 break;
12747 case DW_RLE_offset_pair:
12748 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12749 buffer += bytes_read;
12750 if (buffer > buf_end)
12751 {
12752 overflow = true;
12753 break;
12754 }
12755 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12756 buffer += bytes_read;
12757 if (buffer > buf_end)
12758 {
12759 overflow = true;
12760 break;
12761 }
12762 break;
12763 case DW_RLE_start_end:
12764 if (buffer + 2 * cu->header.addr_size > buf_end)
12765 {
12766 overflow = true;
12767 break;
12768 }
12769 range_beginning = cu->header.read_address (obfd, buffer,
12770 &bytes_read);
12771 buffer += bytes_read;
12772 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
12773 buffer += bytes_read;
12774 break;
12775 case DW_RLE_startx_endx:
12776 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12777 buffer += bytes_read;
12778 range_beginning = read_addr_index (cu, addr_index);
12779 if (buffer > buf_end)
12780 {
12781 overflow = true;
12782 break;
12783 }
12784 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12785 buffer += bytes_read;
12786 range_end = read_addr_index (cu, addr_index);
12787 break;
12788 default:
12789 complaint (_("Invalid .debug_rnglists data (no base address)"));
12790 return false;
12791 }
12792 if (rlet == DW_RLE_end_of_list || overflow)
12793 break;
12794 if (rlet == DW_RLE_base_address)
12795 continue;
12796
12797 if (range_beginning > range_end)
12798 {
12799 /* Inverted range entries are invalid. */
12800 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12801 return false;
12802 }
12803
12804 /* Empty range entries have no effect. */
12805 if (range_beginning == range_end)
12806 continue;
12807
12808 /* Only DW_RLE_offset_pair needs the base address added. */
12809 if (rlet == DW_RLE_offset_pair)
12810 {
12811 if (!base.has_value ())
12812 {
12813 /* We have no valid base address for the DW_RLE_offset_pair. */
12814 complaint (_("Invalid .debug_rnglists data (no base address for "
12815 "DW_RLE_offset_pair)"));
12816 return false;
12817 }
12818
12819 range_beginning += *base;
12820 range_end += *base;
12821 }
12822
12823 /* A not-uncommon case of bad debug info.
12824 Don't pollute the addrmap with bad data. */
12825 if (range_beginning == 0
12826 && !per_objfile->per_bfd->has_section_at_zero)
12827 {
12828 complaint (_(".debug_rnglists entry has start address of zero"
12829 " [in module %s]"), objfile_name (objfile));
12830 continue;
12831 }
12832
12833 callback (range_beginning, range_end);
12834 }
12835
12836 if (overflow)
12837 {
12838 complaint (_("Offset %d is not terminated "
12839 "for DW_AT_ranges attribute"),
12840 offset);
12841 return false;
12842 }
12843
12844 return true;
12845 }
12846
12847 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12848 Callback's type should be:
12849 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12850 Return 1 if the attributes are present and valid, otherwise, return 0. */
12851
12852 template <typename Callback>
12853 static int
12854 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
12855 Callback &&callback)
12856 {
12857 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12858 struct objfile *objfile = per_objfile->objfile;
12859 struct comp_unit_head *cu_header = &cu->header;
12860 bfd *obfd = objfile->obfd;
12861 unsigned int addr_size = cu_header->addr_size;
12862 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12863 /* Base address selection entry. */
12864 gdb::optional<CORE_ADDR> base;
12865 unsigned int dummy;
12866 const gdb_byte *buffer;
12867
12868 if (cu_header->version >= 5)
12869 return dwarf2_rnglists_process (offset, cu, tag, callback);
12870
12871 base = cu->base_address;
12872
12873 per_objfile->per_bfd->ranges.read (objfile);
12874 if (offset >= per_objfile->per_bfd->ranges.size)
12875 {
12876 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12877 offset);
12878 return 0;
12879 }
12880 buffer = per_objfile->per_bfd->ranges.buffer + offset;
12881
12882 while (1)
12883 {
12884 CORE_ADDR range_beginning, range_end;
12885
12886 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
12887 buffer += addr_size;
12888 range_end = cu->header.read_address (obfd, buffer, &dummy);
12889 buffer += addr_size;
12890 offset += 2 * addr_size;
12891
12892 /* An end of list marker is a pair of zero addresses. */
12893 if (range_beginning == 0 && range_end == 0)
12894 /* Found the end of list entry. */
12895 break;
12896
12897 /* Each base address selection entry is a pair of 2 values.
12898 The first is the largest possible address, the second is
12899 the base address. Check for a base address here. */
12900 if ((range_beginning & mask) == mask)
12901 {
12902 /* If we found the largest possible address, then we already
12903 have the base address in range_end. */
12904 base = range_end;
12905 continue;
12906 }
12907
12908 if (!base.has_value ())
12909 {
12910 /* We have no valid base address for the ranges
12911 data. */
12912 complaint (_("Invalid .debug_ranges data (no base address)"));
12913 return 0;
12914 }
12915
12916 if (range_beginning > range_end)
12917 {
12918 /* Inverted range entries are invalid. */
12919 complaint (_("Invalid .debug_ranges data (inverted range)"));
12920 return 0;
12921 }
12922
12923 /* Empty range entries have no effect. */
12924 if (range_beginning == range_end)
12925 continue;
12926
12927 range_beginning += *base;
12928 range_end += *base;
12929
12930 /* A not-uncommon case of bad debug info.
12931 Don't pollute the addrmap with bad data. */
12932 if (range_beginning == 0
12933 && !per_objfile->per_bfd->has_section_at_zero)
12934 {
12935 complaint (_(".debug_ranges entry has start address of zero"
12936 " [in module %s]"), objfile_name (objfile));
12937 continue;
12938 }
12939
12940 callback (range_beginning, range_end);
12941 }
12942
12943 return 1;
12944 }
12945
12946 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12947 Return 1 if the attributes are present and valid, otherwise, return 0.
12948 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12949 ranges in MAP are set, using DATUM as the value. */
12950
12951 static int
12952 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12953 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12954 addrmap *map, void *datum, dwarf_tag tag)
12955 {
12956 struct objfile *objfile = cu->per_objfile->objfile;
12957 struct gdbarch *gdbarch = objfile->arch ();
12958 const CORE_ADDR baseaddr = objfile->text_section_offset ();
12959 int low_set = 0;
12960 CORE_ADDR low = 0;
12961 CORE_ADDR high = 0;
12962 int retval;
12963
12964 retval = dwarf2_ranges_process (offset, cu, tag,
12965 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12966 {
12967 if (map != nullptr)
12968 {
12969 CORE_ADDR lowpc;
12970 CORE_ADDR highpc;
12971
12972 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
12973 range_beginning + baseaddr)
12974 - baseaddr);
12975 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
12976 range_end + baseaddr)
12977 - baseaddr);
12978 addrmap_set_empty (map, lowpc, highpc - 1, datum);
12979 }
12980
12981 /* FIXME: This is recording everything as a low-high
12982 segment of consecutive addresses. We should have a
12983 data structure for discontiguous block ranges
12984 instead. */
12985 if (! low_set)
12986 {
12987 low = range_beginning;
12988 high = range_end;
12989 low_set = 1;
12990 }
12991 else
12992 {
12993 if (range_beginning < low)
12994 low = range_beginning;
12995 if (range_end > high)
12996 high = range_end;
12997 }
12998 });
12999 if (!retval)
13000 return 0;
13001
13002 if (! low_set)
13003 /* If the first entry is an end-of-list marker, the range
13004 describes an empty scope, i.e. no instructions. */
13005 return 0;
13006
13007 if (low_return)
13008 *low_return = low;
13009 if (high_return)
13010 *high_return = high;
13011 return 1;
13012 }
13013
13014 /* Process ranges and fill in a vector of the low PC values only. */
13015
13016 static void
13017 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
13018 dwarf_tag tag,
13019 std::vector<CORE_ADDR> &result)
13020 {
13021 dwarf2_ranges_process (offset, cu, tag,
13022 [&] (CORE_ADDR start, CORE_ADDR end)
13023 {
13024 result.push_back (start);
13025 });
13026 }
13027
13028 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13029 definition for the return value. *LOWPC and *HIGHPC are set iff
13030 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13031
13032 static enum pc_bounds_kind
13033 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13034 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13035 addrmap *map, void *datum)
13036 {
13037 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13038 struct attribute *attr;
13039 struct attribute *attr_high;
13040 CORE_ADDR low = 0;
13041 CORE_ADDR high = 0;
13042 enum pc_bounds_kind ret;
13043
13044 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13045 if (attr_high)
13046 {
13047 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13048 if (attr != nullptr)
13049 {
13050 low = attr->as_address ();
13051 high = attr_high->as_address ();
13052 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13053 high += low;
13054 }
13055 else
13056 /* Found high w/o low attribute. */
13057 return PC_BOUNDS_INVALID;
13058
13059 /* Found consecutive range of addresses. */
13060 ret = PC_BOUNDS_HIGH_LOW;
13061 }
13062 else
13063 {
13064 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13065 if (attr != nullptr && attr->form_is_unsigned ())
13066 {
13067 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13068 on DWARF version). */
13069 ULONGEST ranges_offset = attr->as_unsigned ();
13070
13071 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13072 this value. */
13073 if (die->tag != DW_TAG_compile_unit)
13074 ranges_offset += cu->gnu_ranges_base;
13075
13076 /* Value of the DW_AT_ranges attribute is the offset in the
13077 .debug_ranges section. */
13078 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
13079 map, datum, die->tag))
13080 return PC_BOUNDS_INVALID;
13081 /* Found discontinuous range of addresses. */
13082 ret = PC_BOUNDS_RANGES;
13083 }
13084 else
13085 return PC_BOUNDS_NOT_PRESENT;
13086 }
13087
13088 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13089 if (high <= low)
13090 return PC_BOUNDS_INVALID;
13091
13092 /* When using the GNU linker, .gnu.linkonce. sections are used to
13093 eliminate duplicate copies of functions and vtables and such.
13094 The linker will arbitrarily choose one and discard the others.
13095 The AT_*_pc values for such functions refer to local labels in
13096 these sections. If the section from that file was discarded, the
13097 labels are not in the output, so the relocs get a value of 0.
13098 If this is a discarded function, mark the pc bounds as invalid,
13099 so that GDB will ignore it. */
13100 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
13101 return PC_BOUNDS_INVALID;
13102
13103 *lowpc = low;
13104 if (highpc)
13105 *highpc = high;
13106 return ret;
13107 }
13108
13109 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13110 its low and high PC addresses. Do nothing if these addresses could not
13111 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13112 and HIGHPC to the high address if greater than HIGHPC. */
13113
13114 static void
13115 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13116 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13117 struct dwarf2_cu *cu)
13118 {
13119 CORE_ADDR low, high;
13120 struct die_info *child = die->child;
13121
13122 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
13123 >= PC_BOUNDS_RANGES)
13124 {
13125 *lowpc = std::min (*lowpc, low);
13126 *highpc = std::max (*highpc, high);
13127 }
13128
13129 /* If the language does not allow nested subprograms (either inside
13130 subprograms or lexical blocks), we're done. */
13131 if (cu->per_cu->lang != language_ada)
13132 return;
13133
13134 /* Check all the children of the given DIE. If it contains nested
13135 subprograms, then check their pc bounds. Likewise, we need to
13136 check lexical blocks as well, as they may also contain subprogram
13137 definitions. */
13138 while (child && child->tag)
13139 {
13140 if (child->tag == DW_TAG_subprogram
13141 || child->tag == DW_TAG_lexical_block)
13142 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13143 child = child->sibling;
13144 }
13145 }
13146
13147 /* Get the low and high pc's represented by the scope DIE, and store
13148 them in *LOWPC and *HIGHPC. If the correct values can't be
13149 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13150
13151 static void
13152 get_scope_pc_bounds (struct die_info *die,
13153 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13154 struct dwarf2_cu *cu)
13155 {
13156 CORE_ADDR best_low = (CORE_ADDR) -1;
13157 CORE_ADDR best_high = (CORE_ADDR) 0;
13158 CORE_ADDR current_low, current_high;
13159
13160 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
13161 nullptr, nullptr)
13162 >= PC_BOUNDS_RANGES)
13163 {
13164 best_low = current_low;
13165 best_high = current_high;
13166 }
13167 else
13168 {
13169 struct die_info *child = die->child;
13170
13171 while (child && child->tag)
13172 {
13173 switch (child->tag) {
13174 case DW_TAG_subprogram:
13175 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13176 break;
13177 case DW_TAG_namespace:
13178 case DW_TAG_module:
13179 /* FIXME: carlton/2004-01-16: Should we do this for
13180 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13181 that current GCC's always emit the DIEs corresponding
13182 to definitions of methods of classes as children of a
13183 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13184 the DIEs giving the declarations, which could be
13185 anywhere). But I don't see any reason why the
13186 standards says that they have to be there. */
13187 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13188
13189 if (current_low != ((CORE_ADDR) -1))
13190 {
13191 best_low = std::min (best_low, current_low);
13192 best_high = std::max (best_high, current_high);
13193 }
13194 break;
13195 default:
13196 /* Ignore. */
13197 break;
13198 }
13199
13200 child = child->sibling;
13201 }
13202 }
13203
13204 *lowpc = best_low;
13205 *highpc = best_high;
13206 }
13207
13208 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13209 in DIE. */
13210
13211 static void
13212 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13213 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13214 {
13215 struct objfile *objfile = cu->per_objfile->objfile;
13216 struct gdbarch *gdbarch = objfile->arch ();
13217 struct attribute *attr;
13218 struct attribute *attr_high;
13219
13220 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13221 if (attr_high)
13222 {
13223 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13224 if (attr != nullptr)
13225 {
13226 CORE_ADDR low = attr->as_address ();
13227 CORE_ADDR high = attr_high->as_address ();
13228
13229 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13230 high += low;
13231
13232 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13233 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13234 cu->get_builder ()->record_block_range (block, low, high - 1);
13235 }
13236 }
13237
13238 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13239 if (attr != nullptr && attr->form_is_unsigned ())
13240 {
13241 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13242 on DWARF version). */
13243 ULONGEST ranges_offset = attr->as_unsigned ();
13244
13245 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13246 this value. */
13247 if (die->tag != DW_TAG_compile_unit)
13248 ranges_offset += cu->gnu_ranges_base;
13249
13250 std::vector<blockrange> blockvec;
13251 dwarf2_ranges_process (ranges_offset, cu, die->tag,
13252 [&] (CORE_ADDR start, CORE_ADDR end)
13253 {
13254 start += baseaddr;
13255 end += baseaddr;
13256 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13257 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13258 cu->get_builder ()->record_block_range (block, start, end - 1);
13259 blockvec.emplace_back (start, end);
13260 });
13261
13262 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13263 }
13264 }
13265
13266 /* Check whether the producer field indicates either of GCC < 4.6, or the
13267 Intel C/C++ compiler, and cache the result in CU. */
13268
13269 static void
13270 check_producer (struct dwarf2_cu *cu)
13271 {
13272 int major, minor;
13273
13274 if (cu->producer == NULL)
13275 {
13276 /* For unknown compilers expect their behavior is DWARF version
13277 compliant.
13278
13279 GCC started to support .debug_types sections by -gdwarf-4 since
13280 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13281 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13282 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13283 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13284 }
13285 else if (producer_is_gcc (cu->producer, &major, &minor))
13286 {
13287 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13288 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13289 cu->producer_is_gcc_11 = major == 11;
13290 }
13291 else if (producer_is_icc (cu->producer, &major, &minor))
13292 {
13293 cu->producer_is_icc = true;
13294 cu->producer_is_icc_lt_14 = major < 14;
13295 }
13296 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13297 cu->producer_is_codewarrior = true;
13298 else
13299 {
13300 /* For other non-GCC compilers, expect their behavior is DWARF version
13301 compliant. */
13302 }
13303
13304 cu->checked_producer = true;
13305 }
13306
13307 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13308 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13309 during 4.6.0 experimental. */
13310
13311 static bool
13312 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13313 {
13314 if (!cu->checked_producer)
13315 check_producer (cu);
13316
13317 return cu->producer_is_gxx_lt_4_6;
13318 }
13319
13320
13321 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13322 with incorrect is_stmt attributes. */
13323
13324 static bool
13325 producer_is_codewarrior (struct dwarf2_cu *cu)
13326 {
13327 if (!cu->checked_producer)
13328 check_producer (cu);
13329
13330 return cu->producer_is_codewarrior;
13331 }
13332
13333 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13334 If that attribute is not available, return the appropriate
13335 default. */
13336
13337 static enum dwarf_access_attribute
13338 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13339 {
13340 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13341 if (attr != nullptr)
13342 {
13343 LONGEST value = attr->constant_value (-1);
13344 if (value == DW_ACCESS_public
13345 || value == DW_ACCESS_protected
13346 || value == DW_ACCESS_private)
13347 return (dwarf_access_attribute) value;
13348 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13349 plongest (value));
13350 }
13351
13352 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13353 {
13354 /* The default DWARF 2 accessibility for members is public, the default
13355 accessibility for inheritance is private. */
13356
13357 if (die->tag != DW_TAG_inheritance)
13358 return DW_ACCESS_public;
13359 else
13360 return DW_ACCESS_private;
13361 }
13362 else
13363 {
13364 /* DWARF 3+ defines the default accessibility a different way. The same
13365 rules apply now for DW_TAG_inheritance as for the members and it only
13366 depends on the container kind. */
13367
13368 if (die->parent->tag == DW_TAG_class_type)
13369 return DW_ACCESS_private;
13370 else
13371 return DW_ACCESS_public;
13372 }
13373 }
13374
13375 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13376 *OFFSET to the byte offset. If the attribute was not found return
13377 0, otherwise return 1. If it was found but could not properly be
13378 handled, set *OFFSET to 0. */
13379
13380 static int
13381 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13382 LONGEST *offset)
13383 {
13384 struct attribute *attr;
13385
13386 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13387 if (attr != NULL)
13388 {
13389 *offset = 0;
13390
13391 /* Note that we do not check for a section offset first here.
13392 This is because DW_AT_data_member_location is new in DWARF 4,
13393 so if we see it, we can assume that a constant form is really
13394 a constant and not a section offset. */
13395 if (attr->form_is_constant ())
13396 *offset = attr->constant_value (0);
13397 else if (attr->form_is_section_offset ())
13398 dwarf2_complex_location_expr_complaint ();
13399 else if (attr->form_is_block ())
13400 *offset = decode_locdesc (attr->as_block (), cu);
13401 else
13402 dwarf2_complex_location_expr_complaint ();
13403
13404 return 1;
13405 }
13406 else
13407 {
13408 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13409 if (attr != nullptr)
13410 {
13411 *offset = attr->constant_value (0);
13412 return 1;
13413 }
13414 }
13415
13416 return 0;
13417 }
13418
13419 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13420 store the results in FIELD. */
13421
13422 static void
13423 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13424 struct field *field)
13425 {
13426 struct attribute *attr;
13427
13428 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13429 if (attr != NULL)
13430 {
13431 if (attr->form_is_constant ())
13432 {
13433 LONGEST offset = attr->constant_value (0);
13434
13435 /* Work around this GCC 11 bug, where it would erroneously use -1
13436 data member locations, instead of 0:
13437
13438 Negative DW_AT_data_member_location
13439 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13440 */
13441 if (offset == -1 && cu->producer_is_gcc_11)
13442 {
13443 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13444 offset = 0;
13445 }
13446
13447 field->set_loc_bitpos (offset * bits_per_byte);
13448 }
13449 else if (attr->form_is_section_offset ())
13450 dwarf2_complex_location_expr_complaint ();
13451 else if (attr->form_is_block ())
13452 {
13453 bool handled;
13454 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
13455 if (handled)
13456 field->set_loc_bitpos (offset * bits_per_byte);
13457 else
13458 {
13459 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13460 struct objfile *objfile = per_objfile->objfile;
13461 struct dwarf2_locexpr_baton *dlbaton
13462 = XOBNEW (&objfile->objfile_obstack,
13463 struct dwarf2_locexpr_baton);
13464 dlbaton->data = attr->as_block ()->data;
13465 dlbaton->size = attr->as_block ()->size;
13466 /* When using this baton, we want to compute the address
13467 of the field, not the value. This is why
13468 is_reference is set to false here. */
13469 dlbaton->is_reference = false;
13470 dlbaton->per_objfile = per_objfile;
13471 dlbaton->per_cu = cu->per_cu;
13472
13473 field->set_loc_dwarf_block (dlbaton);
13474 }
13475 }
13476 else
13477 dwarf2_complex_location_expr_complaint ();
13478 }
13479 else
13480 {
13481 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13482 if (attr != nullptr)
13483 field->set_loc_bitpos (attr->constant_value (0));
13484 }
13485 }
13486
13487 /* Add an aggregate field to the field list. */
13488
13489 static void
13490 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13491 struct dwarf2_cu *cu)
13492 {
13493 struct objfile *objfile = cu->per_objfile->objfile;
13494 struct gdbarch *gdbarch = objfile->arch ();
13495 struct nextfield *new_field;
13496 struct attribute *attr;
13497 struct field *fp;
13498 const char *fieldname = "";
13499
13500 if (die->tag == DW_TAG_inheritance)
13501 {
13502 fip->baseclasses.emplace_back ();
13503 new_field = &fip->baseclasses.back ();
13504 }
13505 else
13506 {
13507 fip->fields.emplace_back ();
13508 new_field = &fip->fields.back ();
13509 }
13510
13511 new_field->offset = die->sect_off;
13512
13513 new_field->accessibility = dwarf2_access_attribute (die, cu);
13514 if (new_field->accessibility != DW_ACCESS_public)
13515 fip->non_public_fields = true;
13516
13517 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13518 if (attr != nullptr)
13519 new_field->virtuality = attr->as_virtuality ();
13520 else
13521 new_field->virtuality = DW_VIRTUALITY_none;
13522
13523 fp = &new_field->field;
13524
13525 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
13526 && !die_is_declaration (die, cu))
13527 {
13528 if (die->tag == DW_TAG_namelist_item)
13529 {
13530 /* Typically, DW_TAG_namelist_item are references to namelist items.
13531 If so, follow that reference. */
13532 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
13533 struct die_info *item_die = nullptr;
13534 struct dwarf2_cu *item_cu = cu;
13535 if (attr1->form_is_ref ())
13536 item_die = follow_die_ref (die, attr1, &item_cu);
13537 if (item_die != nullptr)
13538 die = item_die;
13539 }
13540 /* Data member other than a C++ static data member. */
13541
13542 /* Get type of field. */
13543 fp->set_type (die_type (die, cu));
13544
13545 fp->set_loc_bitpos (0);
13546
13547 /* Get bit size of field (zero if none). */
13548 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13549 if (attr != nullptr)
13550 {
13551 FIELD_BITSIZE (*fp) = attr->constant_value (0);
13552 }
13553 else
13554 {
13555 FIELD_BITSIZE (*fp) = 0;
13556 }
13557
13558 /* Get bit offset of field. */
13559 handle_member_location (die, cu, fp);
13560 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13561 if (attr != nullptr && attr->form_is_constant ())
13562 {
13563 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
13564 {
13565 /* For big endian bits, the DW_AT_bit_offset gives the
13566 additional bit offset from the MSB of the containing
13567 anonymous object to the MSB of the field. We don't
13568 have to do anything special since we don't need to
13569 know the size of the anonymous object. */
13570 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
13571 }
13572 else
13573 {
13574 /* For little endian bits, compute the bit offset to the
13575 MSB of the anonymous object, subtract off the number of
13576 bits from the MSB of the field to the MSB of the
13577 object, and then subtract off the number of bits of
13578 the field itself. The result is the bit offset of
13579 the LSB of the field. */
13580 int anonymous_size;
13581 int bit_offset = attr->constant_value (0);
13582
13583 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13584 if (attr != nullptr && attr->form_is_constant ())
13585 {
13586 /* The size of the anonymous object containing
13587 the bit field is explicit, so use the
13588 indicated size (in bytes). */
13589 anonymous_size = attr->constant_value (0);
13590 }
13591 else
13592 {
13593 /* The size of the anonymous object containing
13594 the bit field must be inferred from the type
13595 attribute of the data member containing the
13596 bit field. */
13597 anonymous_size = TYPE_LENGTH (fp->type ());
13598 }
13599 fp->set_loc_bitpos (fp->loc_bitpos ()
13600 + anonymous_size * bits_per_byte
13601 - bit_offset - FIELD_BITSIZE (*fp));
13602 }
13603 }
13604
13605 /* Get name of field. */
13606 fieldname = dwarf2_name (die, cu);
13607 if (fieldname == NULL)
13608 fieldname = "";
13609
13610 /* The name is already allocated along with this objfile, so we don't
13611 need to duplicate it for the type. */
13612 fp->set_name (fieldname);
13613
13614 /* Change accessibility for artificial fields (e.g. virtual table
13615 pointer or virtual base class pointer) to private. */
13616 if (dwarf2_attr (die, DW_AT_artificial, cu))
13617 {
13618 FIELD_ARTIFICIAL (*fp) = 1;
13619 new_field->accessibility = DW_ACCESS_private;
13620 fip->non_public_fields = true;
13621 }
13622 }
13623 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13624 {
13625 /* C++ static member. */
13626
13627 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13628 is a declaration, but all versions of G++ as of this writing
13629 (so through at least 3.2.1) incorrectly generate
13630 DW_TAG_variable tags. */
13631
13632 const char *physname;
13633
13634 /* Get name of field. */
13635 fieldname = dwarf2_name (die, cu);
13636 if (fieldname == NULL)
13637 return;
13638
13639 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13640 if (attr
13641 /* Only create a symbol if this is an external value.
13642 new_symbol checks this and puts the value in the global symbol
13643 table, which we want. If it is not external, new_symbol
13644 will try to put the value in cu->list_in_scope which is wrong. */
13645 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13646 {
13647 /* A static const member, not much different than an enum as far as
13648 we're concerned, except that we can support more types. */
13649 new_symbol (die, NULL, cu);
13650 }
13651
13652 /* Get physical name. */
13653 physname = dwarf2_physname (fieldname, die, cu);
13654
13655 /* The name is already allocated along with this objfile, so we don't
13656 need to duplicate it for the type. */
13657 fp->set_loc_physname (physname ? physname : "");
13658 fp->set_type (die_type (die, cu));
13659 fp->set_name (fieldname);
13660 }
13661 else if (die->tag == DW_TAG_inheritance)
13662 {
13663 /* C++ base class field. */
13664 handle_member_location (die, cu, fp);
13665 FIELD_BITSIZE (*fp) = 0;
13666 fp->set_type (die_type (die, cu));
13667 fp->set_name (fp->type ()->name ());
13668 }
13669 else
13670 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13671 }
13672
13673 /* Can the type given by DIE define another type? */
13674
13675 static bool
13676 type_can_define_types (const struct die_info *die)
13677 {
13678 switch (die->tag)
13679 {
13680 case DW_TAG_typedef:
13681 case DW_TAG_class_type:
13682 case DW_TAG_structure_type:
13683 case DW_TAG_union_type:
13684 case DW_TAG_enumeration_type:
13685 return true;
13686
13687 default:
13688 return false;
13689 }
13690 }
13691
13692 /* Add a type definition defined in the scope of the FIP's class. */
13693
13694 static void
13695 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
13696 struct dwarf2_cu *cu)
13697 {
13698 struct decl_field fp;
13699 memset (&fp, 0, sizeof (fp));
13700
13701 gdb_assert (type_can_define_types (die));
13702
13703 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13704 fp.name = dwarf2_name (die, cu);
13705 fp.type = read_type_die (die, cu);
13706
13707 /* Save accessibility. */
13708 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
13709 switch (accessibility)
13710 {
13711 case DW_ACCESS_public:
13712 /* The assumed value if neither private nor protected. */
13713 break;
13714 case DW_ACCESS_private:
13715 fp.is_private = 1;
13716 break;
13717 case DW_ACCESS_protected:
13718 fp.is_protected = 1;
13719 break;
13720 }
13721
13722 if (die->tag == DW_TAG_typedef)
13723 fip->typedef_field_list.push_back (fp);
13724 else
13725 fip->nested_types_list.push_back (fp);
13726 }
13727
13728 /* A convenience typedef that's used when finding the discriminant
13729 field for a variant part. */
13730 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
13731 offset_map_type;
13732
13733 /* Compute the discriminant range for a given variant. OBSTACK is
13734 where the results will be stored. VARIANT is the variant to
13735 process. IS_UNSIGNED indicates whether the discriminant is signed
13736 or unsigned. */
13737
13738 static const gdb::array_view<discriminant_range>
13739 convert_variant_range (struct obstack *obstack, const variant_field &variant,
13740 bool is_unsigned)
13741 {
13742 std::vector<discriminant_range> ranges;
13743
13744 if (variant.default_branch)
13745 return {};
13746
13747 if (variant.discr_list_data == nullptr)
13748 {
13749 discriminant_range r
13750 = {variant.discriminant_value, variant.discriminant_value};
13751 ranges.push_back (r);
13752 }
13753 else
13754 {
13755 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
13756 variant.discr_list_data->size);
13757 while (!data.empty ())
13758 {
13759 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
13760 {
13761 complaint (_("invalid discriminant marker: %d"), data[0]);
13762 break;
13763 }
13764 bool is_range = data[0] == DW_DSC_range;
13765 data = data.slice (1);
13766
13767 ULONGEST low, high;
13768 unsigned int bytes_read;
13769
13770 if (data.empty ())
13771 {
13772 complaint (_("DW_AT_discr_list missing low value"));
13773 break;
13774 }
13775 if (is_unsigned)
13776 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
13777 else
13778 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
13779 &bytes_read);
13780 data = data.slice (bytes_read);
13781
13782 if (is_range)
13783 {
13784 if (data.empty ())
13785 {
13786 complaint (_("DW_AT_discr_list missing high value"));
13787 break;
13788 }
13789 if (is_unsigned)
13790 high = read_unsigned_leb128 (nullptr, data.data (),
13791 &bytes_read);
13792 else
13793 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
13794 &bytes_read);
13795 data = data.slice (bytes_read);
13796 }
13797 else
13798 high = low;
13799
13800 ranges.push_back ({ low, high });
13801 }
13802 }
13803
13804 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
13805 ranges.size ());
13806 std::copy (ranges.begin (), ranges.end (), result);
13807 return gdb::array_view<discriminant_range> (result, ranges.size ());
13808 }
13809
13810 static const gdb::array_view<variant_part> create_variant_parts
13811 (struct obstack *obstack,
13812 const offset_map_type &offset_map,
13813 struct field_info *fi,
13814 const std::vector<variant_part_builder> &variant_parts);
13815
13816 /* Fill in a "struct variant" for a given variant field. RESULT is
13817 the variant to fill in. OBSTACK is where any needed allocations
13818 will be done. OFFSET_MAP holds the mapping from section offsets to
13819 fields for the type. FI describes the fields of the type we're
13820 processing. FIELD is the variant field we're converting. */
13821
13822 static void
13823 create_one_variant (variant &result, struct obstack *obstack,
13824 const offset_map_type &offset_map,
13825 struct field_info *fi, const variant_field &field)
13826 {
13827 result.discriminants = convert_variant_range (obstack, field, false);
13828 result.first_field = field.first_field + fi->baseclasses.size ();
13829 result.last_field = field.last_field + fi->baseclasses.size ();
13830 result.parts = create_variant_parts (obstack, offset_map, fi,
13831 field.variant_parts);
13832 }
13833
13834 /* Fill in a "struct variant_part" for a given variant part. RESULT
13835 is the variant part to fill in. OBSTACK is where any needed
13836 allocations will be done. OFFSET_MAP holds the mapping from
13837 section offsets to fields for the type. FI describes the fields of
13838 the type we're processing. BUILDER is the variant part to be
13839 converted. */
13840
13841 static void
13842 create_one_variant_part (variant_part &result,
13843 struct obstack *obstack,
13844 const offset_map_type &offset_map,
13845 struct field_info *fi,
13846 const variant_part_builder &builder)
13847 {
13848 auto iter = offset_map.find (builder.discriminant_offset);
13849 if (iter == offset_map.end ())
13850 {
13851 result.discriminant_index = -1;
13852 /* Doesn't matter. */
13853 result.is_unsigned = false;
13854 }
13855 else
13856 {
13857 result.discriminant_index = iter->second;
13858 result.is_unsigned
13859 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
13860 }
13861
13862 size_t n = builder.variants.size ();
13863 variant *output = new (obstack) variant[n];
13864 for (size_t i = 0; i < n; ++i)
13865 create_one_variant (output[i], obstack, offset_map, fi,
13866 builder.variants[i]);
13867
13868 result.variants = gdb::array_view<variant> (output, n);
13869 }
13870
13871 /* Create a vector of variant parts that can be attached to a type.
13872 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13873 holds the mapping from section offsets to fields for the type. FI
13874 describes the fields of the type we're processing. VARIANT_PARTS
13875 is the vector to convert. */
13876
13877 static const gdb::array_view<variant_part>
13878 create_variant_parts (struct obstack *obstack,
13879 const offset_map_type &offset_map,
13880 struct field_info *fi,
13881 const std::vector<variant_part_builder> &variant_parts)
13882 {
13883 if (variant_parts.empty ())
13884 return {};
13885
13886 size_t n = variant_parts.size ();
13887 variant_part *result = new (obstack) variant_part[n];
13888 for (size_t i = 0; i < n; ++i)
13889 create_one_variant_part (result[i], obstack, offset_map, fi,
13890 variant_parts[i]);
13891
13892 return gdb::array_view<variant_part> (result, n);
13893 }
13894
13895 /* Compute the variant part vector for FIP, attaching it to TYPE when
13896 done. */
13897
13898 static void
13899 add_variant_property (struct field_info *fip, struct type *type,
13900 struct dwarf2_cu *cu)
13901 {
13902 /* Map section offsets of fields to their field index. Note the
13903 field index here does not take the number of baseclasses into
13904 account. */
13905 offset_map_type offset_map;
13906 for (int i = 0; i < fip->fields.size (); ++i)
13907 offset_map[fip->fields[i].offset] = i;
13908
13909 struct objfile *objfile = cu->per_objfile->objfile;
13910 gdb::array_view<const variant_part> parts
13911 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
13912 fip->variant_parts);
13913
13914 struct dynamic_prop prop;
13915 prop.set_variant_parts ((gdb::array_view<variant_part> *)
13916 obstack_copy (&objfile->objfile_obstack, &parts,
13917 sizeof (parts)));
13918
13919 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
13920 }
13921
13922 /* Create the vector of fields, and attach it to the type. */
13923
13924 static void
13925 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
13926 struct dwarf2_cu *cu)
13927 {
13928 int nfields = fip->nfields ();
13929
13930 /* Record the field count, allocate space for the array of fields,
13931 and create blank accessibility bitfields if necessary. */
13932 type->set_num_fields (nfields);
13933 type->set_fields
13934 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
13935
13936 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
13937 {
13938 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13939
13940 TYPE_FIELD_PRIVATE_BITS (type) =
13941 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13942 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13943
13944 TYPE_FIELD_PROTECTED_BITS (type) =
13945 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13946 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13947
13948 TYPE_FIELD_IGNORE_BITS (type) =
13949 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13950 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13951 }
13952
13953 /* If the type has baseclasses, allocate and clear a bit vector for
13954 TYPE_FIELD_VIRTUAL_BITS. */
13955 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
13956 {
13957 int num_bytes = B_BYTES (fip->baseclasses.size ());
13958 unsigned char *pointer;
13959
13960 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13961 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13962 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13963 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
13964 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
13965 }
13966
13967 if (!fip->variant_parts.empty ())
13968 add_variant_property (fip, type, cu);
13969
13970 /* Copy the saved-up fields into the field vector. */
13971 for (int i = 0; i < nfields; ++i)
13972 {
13973 struct nextfield &field
13974 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
13975 : fip->fields[i - fip->baseclasses.size ()]);
13976
13977 type->field (i) = field.field;
13978 switch (field.accessibility)
13979 {
13980 case DW_ACCESS_private:
13981 if (cu->per_cu->lang != language_ada)
13982 SET_TYPE_FIELD_PRIVATE (type, i);
13983 break;
13984
13985 case DW_ACCESS_protected:
13986 if (cu->per_cu->lang != language_ada)
13987 SET_TYPE_FIELD_PROTECTED (type, i);
13988 break;
13989
13990 case DW_ACCESS_public:
13991 break;
13992
13993 default:
13994 /* Unknown accessibility. Complain and treat it as public. */
13995 {
13996 complaint (_("unsupported accessibility %d"),
13997 field.accessibility);
13998 }
13999 break;
14000 }
14001 if (i < fip->baseclasses.size ())
14002 {
14003 switch (field.virtuality)
14004 {
14005 case DW_VIRTUALITY_virtual:
14006 case DW_VIRTUALITY_pure_virtual:
14007 if (cu->per_cu->lang == language_ada)
14008 error (_("unexpected virtuality in component of Ada type"));
14009 SET_TYPE_FIELD_VIRTUAL (type, i);
14010 break;
14011 }
14012 }
14013 }
14014 }
14015
14016 /* Return true if this member function is a constructor, false
14017 otherwise. */
14018
14019 static int
14020 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14021 {
14022 const char *fieldname;
14023 const char *type_name;
14024 int len;
14025
14026 if (die->parent == NULL)
14027 return 0;
14028
14029 if (die->parent->tag != DW_TAG_structure_type
14030 && die->parent->tag != DW_TAG_union_type
14031 && die->parent->tag != DW_TAG_class_type)
14032 return 0;
14033
14034 fieldname = dwarf2_name (die, cu);
14035 type_name = dwarf2_name (die->parent, cu);
14036 if (fieldname == NULL || type_name == NULL)
14037 return 0;
14038
14039 len = strlen (fieldname);
14040 return (strncmp (fieldname, type_name, len) == 0
14041 && (type_name[len] == '\0' || type_name[len] == '<'));
14042 }
14043
14044 /* Add a member function to the proper fieldlist. */
14045
14046 static void
14047 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14048 struct type *type, struct dwarf2_cu *cu)
14049 {
14050 struct objfile *objfile = cu->per_objfile->objfile;
14051 struct attribute *attr;
14052 int i;
14053 struct fnfieldlist *flp = nullptr;
14054 struct fn_field *fnp;
14055 const char *fieldname;
14056 struct type *this_type;
14057
14058 if (cu->per_cu->lang == language_ada)
14059 error (_("unexpected member function in Ada type"));
14060
14061 /* Get name of member function. */
14062 fieldname = dwarf2_name (die, cu);
14063 if (fieldname == NULL)
14064 return;
14065
14066 /* Look up member function name in fieldlist. */
14067 for (i = 0; i < fip->fnfieldlists.size (); i++)
14068 {
14069 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14070 {
14071 flp = &fip->fnfieldlists[i];
14072 break;
14073 }
14074 }
14075
14076 /* Create a new fnfieldlist if necessary. */
14077 if (flp == nullptr)
14078 {
14079 fip->fnfieldlists.emplace_back ();
14080 flp = &fip->fnfieldlists.back ();
14081 flp->name = fieldname;
14082 i = fip->fnfieldlists.size () - 1;
14083 }
14084
14085 /* Create a new member function field and add it to the vector of
14086 fnfieldlists. */
14087 flp->fnfields.emplace_back ();
14088 fnp = &flp->fnfields.back ();
14089
14090 /* Delay processing of the physname until later. */
14091 if (cu->per_cu->lang == language_cplus)
14092 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14093 die, cu);
14094 else
14095 {
14096 const char *physname = dwarf2_physname (fieldname, die, cu);
14097 fnp->physname = physname ? physname : "";
14098 }
14099
14100 fnp->type = alloc_type (objfile);
14101 this_type = read_type_die (die, cu);
14102 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14103 {
14104 int nparams = this_type->num_fields ();
14105
14106 /* TYPE is the domain of this method, and THIS_TYPE is the type
14107 of the method itself (TYPE_CODE_METHOD). */
14108 smash_to_method_type (fnp->type, type,
14109 TYPE_TARGET_TYPE (this_type),
14110 this_type->fields (),
14111 this_type->num_fields (),
14112 this_type->has_varargs ());
14113
14114 /* Handle static member functions.
14115 Dwarf2 has no clean way to discern C++ static and non-static
14116 member functions. G++ helps GDB by marking the first
14117 parameter for non-static member functions (which is the this
14118 pointer) as artificial. We obtain this information from
14119 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14120 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14121 fnp->voffset = VOFFSET_STATIC;
14122 }
14123 else
14124 complaint (_("member function type missing for '%s'"),
14125 dwarf2_full_name (fieldname, die, cu));
14126
14127 /* Get fcontext from DW_AT_containing_type if present. */
14128 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14129 fnp->fcontext = die_containing_type (die, cu);
14130
14131 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14132 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14133
14134 /* Get accessibility. */
14135 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14136 switch (accessibility)
14137 {
14138 case DW_ACCESS_private:
14139 fnp->is_private = 1;
14140 break;
14141 case DW_ACCESS_protected:
14142 fnp->is_protected = 1;
14143 break;
14144 }
14145
14146 /* Check for artificial methods. */
14147 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14148 if (attr && attr->as_boolean ())
14149 fnp->is_artificial = 1;
14150
14151 /* Check for defaulted methods. */
14152 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14153 if (attr != nullptr)
14154 fnp->defaulted = attr->defaulted ();
14155
14156 /* Check for deleted methods. */
14157 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14158 if (attr != nullptr && attr->as_boolean ())
14159 fnp->is_deleted = 1;
14160
14161 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14162
14163 /* Get index in virtual function table if it is a virtual member
14164 function. For older versions of GCC, this is an offset in the
14165 appropriate virtual table, as specified by DW_AT_containing_type.
14166 For everyone else, it is an expression to be evaluated relative
14167 to the object address. */
14168
14169 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14170 if (attr != nullptr)
14171 {
14172 if (attr->form_is_block () && attr->as_block ()->size > 0)
14173 {
14174 struct dwarf_block *block = attr->as_block ();
14175
14176 if (block->data[0] == DW_OP_constu)
14177 {
14178 /* Old-style GCC. */
14179 fnp->voffset = decode_locdesc (block, cu) + 2;
14180 }
14181 else if (block->data[0] == DW_OP_deref
14182 || (block->size > 1
14183 && block->data[0] == DW_OP_deref_size
14184 && block->data[1] == cu->header.addr_size))
14185 {
14186 fnp->voffset = decode_locdesc (block, cu);
14187 if ((fnp->voffset % cu->header.addr_size) != 0)
14188 dwarf2_complex_location_expr_complaint ();
14189 else
14190 fnp->voffset /= cu->header.addr_size;
14191 fnp->voffset += 2;
14192 }
14193 else
14194 dwarf2_complex_location_expr_complaint ();
14195
14196 if (!fnp->fcontext)
14197 {
14198 /* If there is no `this' field and no DW_AT_containing_type,
14199 we cannot actually find a base class context for the
14200 vtable! */
14201 if (this_type->num_fields () == 0
14202 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14203 {
14204 complaint (_("cannot determine context for virtual member "
14205 "function \"%s\" (offset %s)"),
14206 fieldname, sect_offset_str (die->sect_off));
14207 }
14208 else
14209 {
14210 fnp->fcontext
14211 = TYPE_TARGET_TYPE (this_type->field (0).type ());
14212 }
14213 }
14214 }
14215 else if (attr->form_is_section_offset ())
14216 {
14217 dwarf2_complex_location_expr_complaint ();
14218 }
14219 else
14220 {
14221 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14222 fieldname);
14223 }
14224 }
14225 else
14226 {
14227 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14228 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
14229 {
14230 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14231 complaint (_("Member function \"%s\" (offset %s) is virtual "
14232 "but the vtable offset is not specified"),
14233 fieldname, sect_offset_str (die->sect_off));
14234 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14235 TYPE_CPLUS_DYNAMIC (type) = 1;
14236 }
14237 }
14238 }
14239
14240 /* Create the vector of member function fields, and attach it to the type. */
14241
14242 static void
14243 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14244 struct dwarf2_cu *cu)
14245 {
14246 if (cu->per_cu->lang == language_ada)
14247 error (_("unexpected member functions in Ada type"));
14248
14249 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14250 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14251 TYPE_ALLOC (type,
14252 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14253
14254 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14255 {
14256 struct fnfieldlist &nf = fip->fnfieldlists[i];
14257 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14258
14259 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14260 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14261 fn_flp->fn_fields = (struct fn_field *)
14262 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14263
14264 for (int k = 0; k < nf.fnfields.size (); ++k)
14265 fn_flp->fn_fields[k] = nf.fnfields[k];
14266 }
14267
14268 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14269 }
14270
14271 /* Returns non-zero if NAME is the name of a vtable member in CU's
14272 language, zero otherwise. */
14273 static int
14274 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14275 {
14276 static const char vptr[] = "_vptr";
14277
14278 /* Look for the C++ form of the vtable. */
14279 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14280 return 1;
14281
14282 return 0;
14283 }
14284
14285 /* GCC outputs unnamed structures that are really pointers to member
14286 functions, with the ABI-specified layout. If TYPE describes
14287 such a structure, smash it into a member function type.
14288
14289 GCC shouldn't do this; it should just output pointer to member DIEs.
14290 This is GCC PR debug/28767. */
14291
14292 static void
14293 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14294 {
14295 struct type *pfn_type, *self_type, *new_type;
14296
14297 /* Check for a structure with no name and two children. */
14298 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14299 return;
14300
14301 /* Check for __pfn and __delta members. */
14302 if (type->field (0).name () == NULL
14303 || strcmp (type->field (0).name (), "__pfn") != 0
14304 || type->field (1).name () == NULL
14305 || strcmp (type->field (1).name (), "__delta") != 0)
14306 return;
14307
14308 /* Find the type of the method. */
14309 pfn_type = type->field (0).type ();
14310 if (pfn_type == NULL
14311 || pfn_type->code () != TYPE_CODE_PTR
14312 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
14313 return;
14314
14315 /* Look for the "this" argument. */
14316 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14317 if (pfn_type->num_fields () == 0
14318 /* || pfn_type->field (0).type () == NULL */
14319 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
14320 return;
14321
14322 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
14323 new_type = alloc_type (objfile);
14324 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14325 pfn_type->fields (), pfn_type->num_fields (),
14326 pfn_type->has_varargs ());
14327 smash_to_methodptr_type (type, new_type);
14328 }
14329
14330 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14331 requires rewriting, then copy it and return the updated copy.
14332 Otherwise return nullptr. */
14333
14334 static struct type *
14335 rewrite_array_type (struct type *type)
14336 {
14337 if (type->code () != TYPE_CODE_ARRAY)
14338 return nullptr;
14339
14340 struct type *index_type = type->index_type ();
14341 range_bounds *current_bounds = index_type->bounds ();
14342
14343 /* Handle multi-dimensional arrays. */
14344 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
14345 if (new_target == nullptr)
14346 {
14347 /* Maybe we don't need to rewrite this array. */
14348 if (current_bounds->low.kind () == PROP_CONST
14349 && current_bounds->high.kind () == PROP_CONST)
14350 return nullptr;
14351 }
14352
14353 /* Either the target type was rewritten, or the bounds have to be
14354 updated. Either way we want to copy the type and update
14355 everything. */
14356 struct type *copy = copy_type (type);
14357 int nfields = copy->num_fields ();
14358 field *new_fields
14359 = ((struct field *) TYPE_ZALLOC (copy,
14360 nfields * sizeof (struct field)));
14361 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
14362 copy->set_fields (new_fields);
14363 if (new_target != nullptr)
14364 TYPE_TARGET_TYPE (copy) = new_target;
14365
14366 struct type *index_copy = copy_type (index_type);
14367 range_bounds *bounds
14368 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
14369 sizeof (range_bounds));
14370 *bounds = *current_bounds;
14371 bounds->low.set_const_val (1);
14372 bounds->high.set_const_val (0);
14373 index_copy->set_bounds (bounds);
14374 copy->set_index_type (index_copy);
14375
14376 return copy;
14377 }
14378
14379 /* While some versions of GCC will generate complicated DWARF for an
14380 array (see quirk_ada_thick_pointer), more recent versions were
14381 modified to emit an explicit thick pointer structure. However, in
14382 this case, the array still has DWARF expressions for its ranges,
14383 and these must be ignored. */
14384
14385 static void
14386 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
14387 struct type *type)
14388 {
14389 gdb_assert (cu->per_cu->lang == language_ada);
14390
14391 /* Check for a structure with two children. */
14392 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14393 return;
14394
14395 /* Check for P_ARRAY and P_BOUNDS members. */
14396 if (type->field (0).name () == NULL
14397 || strcmp (type->field (0).name (), "P_ARRAY") != 0
14398 || type->field (1).name () == NULL
14399 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
14400 return;
14401
14402 /* Make sure we're looking at a pointer to an array. */
14403 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
14404 return;
14405
14406 /* The Ada code already knows how to handle these types, so all that
14407 we need to do is turn the bounds into static bounds. However, we
14408 don't want to rewrite existing array or index types in-place,
14409 because those may be referenced in other contexts where this
14410 rewriting is undesirable. */
14411 struct type *new_ary_type
14412 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
14413 if (new_ary_type != nullptr)
14414 type->field (0).set_type (lookup_pointer_type (new_ary_type));
14415 }
14416
14417 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14418 appropriate error checking and issuing complaints if there is a
14419 problem. */
14420
14421 static ULONGEST
14422 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14423 {
14424 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14425
14426 if (attr == nullptr)
14427 return 0;
14428
14429 if (!attr->form_is_constant ())
14430 {
14431 complaint (_("DW_AT_alignment must have constant form"
14432 " - DIE at %s [in module %s]"),
14433 sect_offset_str (die->sect_off),
14434 objfile_name (cu->per_objfile->objfile));
14435 return 0;
14436 }
14437
14438 LONGEST val = attr->constant_value (0);
14439 if (val < 0)
14440 {
14441 complaint (_("DW_AT_alignment value must not be negative"
14442 " - DIE at %s [in module %s]"),
14443 sect_offset_str (die->sect_off),
14444 objfile_name (cu->per_objfile->objfile));
14445 return 0;
14446 }
14447 ULONGEST align = val;
14448
14449 if (align == 0)
14450 {
14451 complaint (_("DW_AT_alignment value must not be zero"
14452 " - DIE at %s [in module %s]"),
14453 sect_offset_str (die->sect_off),
14454 objfile_name (cu->per_objfile->objfile));
14455 return 0;
14456 }
14457 if ((align & (align - 1)) != 0)
14458 {
14459 complaint (_("DW_AT_alignment value must be a power of 2"
14460 " - DIE at %s [in module %s]"),
14461 sect_offset_str (die->sect_off),
14462 objfile_name (cu->per_objfile->objfile));
14463 return 0;
14464 }
14465
14466 return align;
14467 }
14468
14469 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14470 the alignment for TYPE. */
14471
14472 static void
14473 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14474 struct type *type)
14475 {
14476 if (!set_type_align (type, get_alignment (cu, die)))
14477 complaint (_("DW_AT_alignment value too large"
14478 " - DIE at %s [in module %s]"),
14479 sect_offset_str (die->sect_off),
14480 objfile_name (cu->per_objfile->objfile));
14481 }
14482
14483 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14484 constant for a type, according to DWARF5 spec, Table 5.5. */
14485
14486 static bool
14487 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14488 {
14489 switch (value)
14490 {
14491 case DW_CC_normal:
14492 case DW_CC_pass_by_reference:
14493 case DW_CC_pass_by_value:
14494 return true;
14495
14496 default:
14497 complaint (_("unrecognized DW_AT_calling_convention value "
14498 "(%s) for a type"), pulongest (value));
14499 return false;
14500 }
14501 }
14502
14503 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14504 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14505 also according to GNU-specific values (see include/dwarf2.h). */
14506
14507 static bool
14508 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14509 {
14510 switch (value)
14511 {
14512 case DW_CC_normal:
14513 case DW_CC_program:
14514 case DW_CC_nocall:
14515 return true;
14516
14517 case DW_CC_GNU_renesas_sh:
14518 case DW_CC_GNU_borland_fastcall_i386:
14519 case DW_CC_GDB_IBM_OpenCL:
14520 return true;
14521
14522 default:
14523 complaint (_("unrecognized DW_AT_calling_convention value "
14524 "(%s) for a subroutine"), pulongest (value));
14525 return false;
14526 }
14527 }
14528
14529 /* Called when we find the DIE that starts a structure or union scope
14530 (definition) to create a type for the structure or union. Fill in
14531 the type's name and general properties; the members will not be
14532 processed until process_structure_scope. A symbol table entry for
14533 the type will also not be done until process_structure_scope (assuming
14534 the type has a name).
14535
14536 NOTE: we need to call these functions regardless of whether or not the
14537 DIE has a DW_AT_name attribute, since it might be an anonymous
14538 structure or union. This gets the type entered into our set of
14539 user defined types. */
14540
14541 static struct type *
14542 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14543 {
14544 struct objfile *objfile = cu->per_objfile->objfile;
14545 struct type *type;
14546 struct attribute *attr;
14547 const char *name;
14548
14549 /* If the definition of this type lives in .debug_types, read that type.
14550 Don't follow DW_AT_specification though, that will take us back up
14551 the chain and we want to go down. */
14552 attr = die->attr (DW_AT_signature);
14553 if (attr != nullptr)
14554 {
14555 type = get_DW_AT_signature_type (die, attr, cu);
14556
14557 /* The type's CU may not be the same as CU.
14558 Ensure TYPE is recorded with CU in die_type_hash. */
14559 return set_die_type (die, type, cu);
14560 }
14561
14562 type = alloc_type (objfile);
14563 INIT_CPLUS_SPECIFIC (type);
14564
14565 name = dwarf2_name (die, cu);
14566 if (name != NULL)
14567 {
14568 if (cu->per_cu->lang == language_cplus
14569 || cu->per_cu->lang == language_d
14570 || cu->per_cu->lang == language_rust)
14571 {
14572 const char *full_name = dwarf2_full_name (name, die, cu);
14573
14574 /* dwarf2_full_name might have already finished building the DIE's
14575 type. If so, there is no need to continue. */
14576 if (get_die_type (die, cu) != NULL)
14577 return get_die_type (die, cu);
14578
14579 type->set_name (full_name);
14580 }
14581 else
14582 {
14583 /* The name is already allocated along with this objfile, so
14584 we don't need to duplicate it for the type. */
14585 type->set_name (name);
14586 }
14587 }
14588
14589 if (die->tag == DW_TAG_structure_type)
14590 {
14591 type->set_code (TYPE_CODE_STRUCT);
14592 }
14593 else if (die->tag == DW_TAG_union_type)
14594 {
14595 type->set_code (TYPE_CODE_UNION);
14596 }
14597 else if (die->tag == DW_TAG_namelist)
14598 {
14599 type->set_code (TYPE_CODE_NAMELIST);
14600 }
14601 else
14602 {
14603 type->set_code (TYPE_CODE_STRUCT);
14604 }
14605
14606 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
14607 type->set_is_declared_class (true);
14608
14609 /* Store the calling convention in the type if it's available in
14610 the die. Otherwise the calling convention remains set to
14611 the default value DW_CC_normal. */
14612 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14613 if (attr != nullptr
14614 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
14615 {
14616 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14617 TYPE_CPLUS_CALLING_CONVENTION (type)
14618 = (enum dwarf_calling_convention) (attr->constant_value (0));
14619 }
14620
14621 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14622 if (attr != nullptr)
14623 {
14624 if (attr->form_is_constant ())
14625 TYPE_LENGTH (type) = attr->constant_value (0);
14626 else
14627 {
14628 struct dynamic_prop prop;
14629 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
14630 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
14631 TYPE_LENGTH (type) = 0;
14632 }
14633 }
14634 else
14635 {
14636 TYPE_LENGTH (type) = 0;
14637 }
14638
14639 maybe_set_alignment (cu, die, type);
14640
14641 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
14642 {
14643 /* ICC<14 does not output the required DW_AT_declaration on
14644 incomplete types, but gives them a size of zero. */
14645 type->set_is_stub (true);
14646 }
14647 else
14648 type->set_stub_is_supported (true);
14649
14650 if (die_is_declaration (die, cu))
14651 type->set_is_stub (true);
14652 else if (attr == NULL && die->child == NULL
14653 && producer_is_realview (cu->producer))
14654 /* RealView does not output the required DW_AT_declaration
14655 on incomplete types. */
14656 type->set_is_stub (true);
14657
14658 /* We need to add the type field to the die immediately so we don't
14659 infinitely recurse when dealing with pointers to the structure
14660 type within the structure itself. */
14661 set_die_type (die, type, cu);
14662
14663 /* set_die_type should be already done. */
14664 set_descriptive_type (type, die, cu);
14665
14666 return type;
14667 }
14668
14669 static void handle_struct_member_die
14670 (struct die_info *child_die,
14671 struct type *type,
14672 struct field_info *fi,
14673 std::vector<struct symbol *> *template_args,
14674 struct dwarf2_cu *cu);
14675
14676 /* A helper for handle_struct_member_die that handles
14677 DW_TAG_variant_part. */
14678
14679 static void
14680 handle_variant_part (struct die_info *die, struct type *type,
14681 struct field_info *fi,
14682 std::vector<struct symbol *> *template_args,
14683 struct dwarf2_cu *cu)
14684 {
14685 variant_part_builder *new_part;
14686 if (fi->current_variant_part == nullptr)
14687 {
14688 fi->variant_parts.emplace_back ();
14689 new_part = &fi->variant_parts.back ();
14690 }
14691 else if (!fi->current_variant_part->processing_variant)
14692 {
14693 complaint (_("nested DW_TAG_variant_part seen "
14694 "- DIE at %s [in module %s]"),
14695 sect_offset_str (die->sect_off),
14696 objfile_name (cu->per_objfile->objfile));
14697 return;
14698 }
14699 else
14700 {
14701 variant_field &current = fi->current_variant_part->variants.back ();
14702 current.variant_parts.emplace_back ();
14703 new_part = &current.variant_parts.back ();
14704 }
14705
14706 /* When we recurse, we want callees to add to this new variant
14707 part. */
14708 scoped_restore save_current_variant_part
14709 = make_scoped_restore (&fi->current_variant_part, new_part);
14710
14711 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
14712 if (discr == NULL)
14713 {
14714 /* It's a univariant form, an extension we support. */
14715 }
14716 else if (discr->form_is_ref ())
14717 {
14718 struct dwarf2_cu *target_cu = cu;
14719 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
14720
14721 new_part->discriminant_offset = target_die->sect_off;
14722 }
14723 else
14724 {
14725 complaint (_("DW_AT_discr does not have DIE reference form"
14726 " - DIE at %s [in module %s]"),
14727 sect_offset_str (die->sect_off),
14728 objfile_name (cu->per_objfile->objfile));
14729 }
14730
14731 for (die_info *child_die = die->child;
14732 child_die != NULL;
14733 child_die = child_die->sibling)
14734 handle_struct_member_die (child_die, type, fi, template_args, cu);
14735 }
14736
14737 /* A helper for handle_struct_member_die that handles
14738 DW_TAG_variant. */
14739
14740 static void
14741 handle_variant (struct die_info *die, struct type *type,
14742 struct field_info *fi,
14743 std::vector<struct symbol *> *template_args,
14744 struct dwarf2_cu *cu)
14745 {
14746 if (fi->current_variant_part == nullptr)
14747 {
14748 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14749 "- DIE at %s [in module %s]"),
14750 sect_offset_str (die->sect_off),
14751 objfile_name (cu->per_objfile->objfile));
14752 return;
14753 }
14754 if (fi->current_variant_part->processing_variant)
14755 {
14756 complaint (_("nested DW_TAG_variant seen "
14757 "- DIE at %s [in module %s]"),
14758 sect_offset_str (die->sect_off),
14759 objfile_name (cu->per_objfile->objfile));
14760 return;
14761 }
14762
14763 scoped_restore save_processing_variant
14764 = make_scoped_restore (&fi->current_variant_part->processing_variant,
14765 true);
14766
14767 fi->current_variant_part->variants.emplace_back ();
14768 variant_field &variant = fi->current_variant_part->variants.back ();
14769 variant.first_field = fi->fields.size ();
14770
14771 /* In a variant we want to get the discriminant and also add a
14772 field for our sole member child. */
14773 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
14774 if (discr == nullptr || !discr->form_is_constant ())
14775 {
14776 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
14777 if (discr == nullptr || discr->as_block ()->size == 0)
14778 variant.default_branch = true;
14779 else
14780 variant.discr_list_data = discr->as_block ();
14781 }
14782 else
14783 variant.discriminant_value = discr->constant_value (0);
14784
14785 for (die_info *variant_child = die->child;
14786 variant_child != NULL;
14787 variant_child = variant_child->sibling)
14788 handle_struct_member_die (variant_child, type, fi, template_args, cu);
14789
14790 variant.last_field = fi->fields.size ();
14791 }
14792
14793 /* A helper for process_structure_scope that handles a single member
14794 DIE. */
14795
14796 static void
14797 handle_struct_member_die (struct die_info *child_die, struct type *type,
14798 struct field_info *fi,
14799 std::vector<struct symbol *> *template_args,
14800 struct dwarf2_cu *cu)
14801 {
14802 if (child_die->tag == DW_TAG_member
14803 || child_die->tag == DW_TAG_variable
14804 || child_die->tag == DW_TAG_namelist_item)
14805 {
14806 /* NOTE: carlton/2002-11-05: A C++ static data member
14807 should be a DW_TAG_member that is a declaration, but
14808 all versions of G++ as of this writing (so through at
14809 least 3.2.1) incorrectly generate DW_TAG_variable
14810 tags for them instead. */
14811 dwarf2_add_field (fi, child_die, cu);
14812 }
14813 else if (child_die->tag == DW_TAG_subprogram)
14814 {
14815 /* Rust doesn't have member functions in the C++ sense.
14816 However, it does emit ordinary functions as children
14817 of a struct DIE. */
14818 if (cu->per_cu->lang == language_rust)
14819 read_func_scope (child_die, cu);
14820 else
14821 {
14822 /* C++ member function. */
14823 dwarf2_add_member_fn (fi, child_die, type, cu);
14824 }
14825 }
14826 else if (child_die->tag == DW_TAG_inheritance)
14827 {
14828 /* C++ base class field. */
14829 dwarf2_add_field (fi, child_die, cu);
14830 }
14831 else if (type_can_define_types (child_die))
14832 dwarf2_add_type_defn (fi, child_die, cu);
14833 else if (child_die->tag == DW_TAG_template_type_param
14834 || child_die->tag == DW_TAG_template_value_param)
14835 {
14836 struct symbol *arg = new_symbol (child_die, NULL, cu);
14837
14838 if (arg != NULL)
14839 template_args->push_back (arg);
14840 }
14841 else if (child_die->tag == DW_TAG_variant_part)
14842 handle_variant_part (child_die, type, fi, template_args, cu);
14843 else if (child_die->tag == DW_TAG_variant)
14844 handle_variant (child_die, type, fi, template_args, cu);
14845 }
14846
14847 /* Finish creating a structure or union type, including filling in its
14848 members and creating a symbol for it. This function also handles Fortran
14849 namelist variables, their items or members and creating a symbol for
14850 them. */
14851
14852 static void
14853 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14854 {
14855 struct objfile *objfile = cu->per_objfile->objfile;
14856 struct die_info *child_die;
14857 struct type *type;
14858
14859 type = get_die_type (die, cu);
14860 if (type == NULL)
14861 type = read_structure_type (die, cu);
14862
14863 bool has_template_parameters = false;
14864 if (die->child != NULL && ! die_is_declaration (die, cu))
14865 {
14866 struct field_info fi;
14867 std::vector<struct symbol *> template_args;
14868
14869 child_die = die->child;
14870
14871 while (child_die && child_die->tag)
14872 {
14873 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
14874 child_die = child_die->sibling;
14875 }
14876
14877 /* Attach template arguments to type. */
14878 if (!template_args.empty ())
14879 {
14880 has_template_parameters = true;
14881 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14882 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14883 TYPE_TEMPLATE_ARGUMENTS (type)
14884 = XOBNEWVEC (&objfile->objfile_obstack,
14885 struct symbol *,
14886 TYPE_N_TEMPLATE_ARGUMENTS (type));
14887 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14888 template_args.data (),
14889 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14890 * sizeof (struct symbol *)));
14891 }
14892
14893 /* Attach fields and member functions to the type. */
14894 if (fi.nfields () > 0)
14895 dwarf2_attach_fields_to_type (&fi, type, cu);
14896 if (!fi.fnfieldlists.empty ())
14897 {
14898 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14899
14900 /* Get the type which refers to the base class (possibly this
14901 class itself) which contains the vtable pointer for the current
14902 class from the DW_AT_containing_type attribute. This use of
14903 DW_AT_containing_type is a GNU extension. */
14904
14905 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14906 {
14907 struct type *t = die_containing_type (die, cu);
14908
14909 set_type_vptr_basetype (type, t);
14910 if (type == t)
14911 {
14912 int i;
14913
14914 /* Our own class provides vtbl ptr. */
14915 for (i = t->num_fields () - 1;
14916 i >= TYPE_N_BASECLASSES (t);
14917 --i)
14918 {
14919 const char *fieldname = t->field (i).name ();
14920
14921 if (is_vtable_name (fieldname, cu))
14922 {
14923 set_type_vptr_fieldno (type, i);
14924 break;
14925 }
14926 }
14927
14928 /* Complain if virtual function table field not found. */
14929 if (i < TYPE_N_BASECLASSES (t))
14930 complaint (_("virtual function table pointer "
14931 "not found when defining class '%s'"),
14932 type->name () ? type->name () : "");
14933 }
14934 else
14935 {
14936 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
14937 }
14938 }
14939 else if (cu->producer
14940 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
14941 {
14942 /* The IBM XLC compiler does not provide direct indication
14943 of the containing type, but the vtable pointer is
14944 always named __vfp. */
14945
14946 int i;
14947
14948 for (i = type->num_fields () - 1;
14949 i >= TYPE_N_BASECLASSES (type);
14950 --i)
14951 {
14952 if (strcmp (type->field (i).name (), "__vfp") == 0)
14953 {
14954 set_type_vptr_fieldno (type, i);
14955 set_type_vptr_basetype (type, type);
14956 break;
14957 }
14958 }
14959 }
14960 }
14961
14962 /* Copy fi.typedef_field_list linked list elements content into the
14963 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14964 if (!fi.typedef_field_list.empty ())
14965 {
14966 int count = fi.typedef_field_list.size ();
14967
14968 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14969 TYPE_TYPEDEF_FIELD_ARRAY (type)
14970 = ((struct decl_field *)
14971 TYPE_ALLOC (type,
14972 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
14973 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
14974
14975 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
14976 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
14977 }
14978
14979 /* Copy fi.nested_types_list linked list elements content into the
14980 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
14981 if (!fi.nested_types_list.empty ()
14982 && cu->per_cu->lang != language_ada)
14983 {
14984 int count = fi.nested_types_list.size ();
14985
14986 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14987 TYPE_NESTED_TYPES_ARRAY (type)
14988 = ((struct decl_field *)
14989 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
14990 TYPE_NESTED_TYPES_COUNT (type) = count;
14991
14992 for (int i = 0; i < fi.nested_types_list.size (); ++i)
14993 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
14994 }
14995 }
14996
14997 quirk_gcc_member_function_pointer (type, objfile);
14998 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
14999 cu->rust_unions.push_back (type);
15000 else if (cu->per_cu->lang == language_ada)
15001 quirk_ada_thick_pointer_struct (die, cu, type);
15002
15003 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15004 snapshots) has been known to create a die giving a declaration
15005 for a class that has, as a child, a die giving a definition for a
15006 nested class. So we have to process our children even if the
15007 current die is a declaration. Normally, of course, a declaration
15008 won't have any children at all. */
15009
15010 child_die = die->child;
15011
15012 while (child_die != NULL && child_die->tag)
15013 {
15014 if (child_die->tag == DW_TAG_member
15015 || child_die->tag == DW_TAG_variable
15016 || child_die->tag == DW_TAG_inheritance
15017 || child_die->tag == DW_TAG_template_value_param
15018 || child_die->tag == DW_TAG_template_type_param)
15019 {
15020 /* Do nothing. */
15021 }
15022 else
15023 process_die (child_die, cu);
15024
15025 child_die = child_die->sibling;
15026 }
15027
15028 /* Do not consider external references. According to the DWARF standard,
15029 these DIEs are identified by the fact that they have no byte_size
15030 attribute, and a declaration attribute. */
15031 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15032 || !die_is_declaration (die, cu)
15033 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15034 {
15035 struct symbol *sym = new_symbol (die, type, cu);
15036
15037 if (has_template_parameters)
15038 {
15039 struct symtab *symtab;
15040 if (sym != nullptr)
15041 symtab = symbol_symtab (sym);
15042 else if (cu->line_header != nullptr)
15043 {
15044 /* Any related symtab will do. */
15045 symtab
15046 = cu->line_header->file_names ()[0].symtab;
15047 }
15048 else
15049 {
15050 symtab = nullptr;
15051 complaint (_("could not find suitable "
15052 "symtab for template parameter"
15053 " - DIE at %s [in module %s]"),
15054 sect_offset_str (die->sect_off),
15055 objfile_name (objfile));
15056 }
15057
15058 if (symtab != nullptr)
15059 {
15060 /* Make sure that the symtab is set on the new symbols.
15061 Even though they don't appear in this symtab directly,
15062 other parts of gdb assume that symbols do, and this is
15063 reasonably true. */
15064 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15065 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15066 }
15067 }
15068 }
15069 }
15070
15071 /* Assuming DIE is an enumeration type, and TYPE is its associated
15072 type, update TYPE using some information only available in DIE's
15073 children. In particular, the fields are computed. */
15074
15075 static void
15076 update_enumeration_type_from_children (struct die_info *die,
15077 struct type *type,
15078 struct dwarf2_cu *cu)
15079 {
15080 struct die_info *child_die;
15081 int unsigned_enum = 1;
15082 int flag_enum = 1;
15083
15084 auto_obstack obstack;
15085 std::vector<struct field> fields;
15086
15087 for (child_die = die->child;
15088 child_die != NULL && child_die->tag;
15089 child_die = child_die->sibling)
15090 {
15091 struct attribute *attr;
15092 LONGEST value;
15093 const gdb_byte *bytes;
15094 struct dwarf2_locexpr_baton *baton;
15095 const char *name;
15096
15097 if (child_die->tag != DW_TAG_enumerator)
15098 continue;
15099
15100 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15101 if (attr == NULL)
15102 continue;
15103
15104 name = dwarf2_name (child_die, cu);
15105 if (name == NULL)
15106 name = "<anonymous enumerator>";
15107
15108 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15109 &value, &bytes, &baton);
15110 if (value < 0)
15111 {
15112 unsigned_enum = 0;
15113 flag_enum = 0;
15114 }
15115 else
15116 {
15117 if (count_one_bits_ll (value) >= 2)
15118 flag_enum = 0;
15119 }
15120
15121 fields.emplace_back ();
15122 struct field &field = fields.back ();
15123 field.set_name (dwarf2_physname (name, child_die, cu));
15124 field.set_loc_enumval (value);
15125 }
15126
15127 if (!fields.empty ())
15128 {
15129 type->set_num_fields (fields.size ());
15130 type->set_fields
15131 ((struct field *)
15132 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15133 memcpy (type->fields (), fields.data (),
15134 sizeof (struct field) * fields.size ());
15135 }
15136
15137 if (unsigned_enum)
15138 type->set_is_unsigned (true);
15139
15140 if (flag_enum)
15141 type->set_is_flag_enum (true);
15142 }
15143
15144 /* Given a DW_AT_enumeration_type die, set its type. We do not
15145 complete the type's fields yet, or create any symbols. */
15146
15147 static struct type *
15148 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15149 {
15150 struct objfile *objfile = cu->per_objfile->objfile;
15151 struct type *type;
15152 struct attribute *attr;
15153 const char *name;
15154
15155 /* If the definition of this type lives in .debug_types, read that type.
15156 Don't follow DW_AT_specification though, that will take us back up
15157 the chain and we want to go down. */
15158 attr = die->attr (DW_AT_signature);
15159 if (attr != nullptr)
15160 {
15161 type = get_DW_AT_signature_type (die, attr, cu);
15162
15163 /* The type's CU may not be the same as CU.
15164 Ensure TYPE is recorded with CU in die_type_hash. */
15165 return set_die_type (die, type, cu);
15166 }
15167
15168 type = alloc_type (objfile);
15169
15170 type->set_code (TYPE_CODE_ENUM);
15171 name = dwarf2_full_name (NULL, die, cu);
15172 if (name != NULL)
15173 type->set_name (name);
15174
15175 attr = dwarf2_attr (die, DW_AT_type, cu);
15176 if (attr != NULL)
15177 {
15178 struct type *underlying_type = die_type (die, cu);
15179
15180 TYPE_TARGET_TYPE (type) = underlying_type;
15181 }
15182
15183 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15184 if (attr != nullptr)
15185 {
15186 TYPE_LENGTH (type) = attr->constant_value (0);
15187 }
15188 else
15189 {
15190 TYPE_LENGTH (type) = 0;
15191 }
15192
15193 maybe_set_alignment (cu, die, type);
15194
15195 /* The enumeration DIE can be incomplete. In Ada, any type can be
15196 declared as private in the package spec, and then defined only
15197 inside the package body. Such types are known as Taft Amendment
15198 Types. When another package uses such a type, an incomplete DIE
15199 may be generated by the compiler. */
15200 if (die_is_declaration (die, cu))
15201 type->set_is_stub (true);
15202
15203 /* If this type has an underlying type that is not a stub, then we
15204 may use its attributes. We always use the "unsigned" attribute
15205 in this situation, because ordinarily we guess whether the type
15206 is unsigned -- but the guess can be wrong and the underlying type
15207 can tell us the reality. However, we defer to a local size
15208 attribute if one exists, because this lets the compiler override
15209 the underlying type if needed. */
15210 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
15211 {
15212 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15213 underlying_type = check_typedef (underlying_type);
15214
15215 type->set_is_unsigned (underlying_type->is_unsigned ());
15216
15217 if (TYPE_LENGTH (type) == 0)
15218 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15219
15220 if (TYPE_RAW_ALIGN (type) == 0
15221 && TYPE_RAW_ALIGN (underlying_type) != 0)
15222 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15223 }
15224
15225 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
15226
15227 set_die_type (die, type, cu);
15228
15229 /* Finish the creation of this type by using the enum's children.
15230 Note that, as usual, this must come after set_die_type to avoid
15231 infinite recursion when trying to compute the names of the
15232 enumerators. */
15233 update_enumeration_type_from_children (die, type, cu);
15234
15235 return type;
15236 }
15237
15238 /* Given a pointer to a die which begins an enumeration, process all
15239 the dies that define the members of the enumeration, and create the
15240 symbol for the enumeration type.
15241
15242 NOTE: We reverse the order of the element list. */
15243
15244 static void
15245 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15246 {
15247 struct type *this_type;
15248
15249 this_type = get_die_type (die, cu);
15250 if (this_type == NULL)
15251 this_type = read_enumeration_type (die, cu);
15252
15253 if (die->child != NULL)
15254 {
15255 struct die_info *child_die;
15256 const char *name;
15257
15258 child_die = die->child;
15259 while (child_die && child_die->tag)
15260 {
15261 if (child_die->tag != DW_TAG_enumerator)
15262 {
15263 process_die (child_die, cu);
15264 }
15265 else
15266 {
15267 name = dwarf2_name (child_die, cu);
15268 if (name)
15269 new_symbol (child_die, this_type, cu);
15270 }
15271
15272 child_die = child_die->sibling;
15273 }
15274 }
15275
15276 /* If we are reading an enum from a .debug_types unit, and the enum
15277 is a declaration, and the enum is not the signatured type in the
15278 unit, then we do not want to add a symbol for it. Adding a
15279 symbol would in some cases obscure the true definition of the
15280 enum, giving users an incomplete type when the definition is
15281 actually available. Note that we do not want to do this for all
15282 enums which are just declarations, because C++0x allows forward
15283 enum declarations. */
15284 if (cu->per_cu->is_debug_types
15285 && die_is_declaration (die, cu))
15286 {
15287 struct signatured_type *sig_type;
15288
15289 sig_type = (struct signatured_type *) cu->per_cu;
15290 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15291 if (sig_type->type_offset_in_section != die->sect_off)
15292 return;
15293 }
15294
15295 new_symbol (die, this_type, cu);
15296 }
15297
15298 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15299 expression for an index type and finds the corresponding field
15300 offset in the hidden "P_BOUNDS" structure. Returns true on success
15301 and updates *FIELD, false if it fails to recognize an
15302 expression. */
15303
15304 static bool
15305 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
15306 int *bounds_offset, struct field *field,
15307 struct dwarf2_cu *cu)
15308 {
15309 struct attribute *attr = dwarf2_attr (die, name, cu);
15310 if (attr == nullptr || !attr->form_is_block ())
15311 return false;
15312
15313 const struct dwarf_block *block = attr->as_block ();
15314 const gdb_byte *start = block->data;
15315 const gdb_byte *end = block->data + block->size;
15316
15317 /* The expression to recognize generally looks like:
15318
15319 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15320 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15321
15322 However, the second "plus_uconst" may be missing:
15323
15324 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15325 DW_OP_deref_size: 4)
15326
15327 This happens when the field is at the start of the structure.
15328
15329 Also, the final deref may not be sized:
15330
15331 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15332 DW_OP_deref)
15333
15334 This happens when the size of the index type happens to be the
15335 same as the architecture's word size. This can occur with or
15336 without the second plus_uconst. */
15337
15338 if (end - start < 2)
15339 return false;
15340 if (*start++ != DW_OP_push_object_address)
15341 return false;
15342 if (*start++ != DW_OP_plus_uconst)
15343 return false;
15344
15345 uint64_t this_bound_off;
15346 start = gdb_read_uleb128 (start, end, &this_bound_off);
15347 if (start == nullptr || (int) this_bound_off != this_bound_off)
15348 return false;
15349 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15350 is consistent among all bounds. */
15351 if (*bounds_offset == -1)
15352 *bounds_offset = this_bound_off;
15353 else if (*bounds_offset != this_bound_off)
15354 return false;
15355
15356 if (start == end || *start++ != DW_OP_deref)
15357 return false;
15358
15359 int offset = 0;
15360 if (start ==end)
15361 return false;
15362 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
15363 {
15364 /* This means an offset of 0. */
15365 }
15366 else if (*start++ != DW_OP_plus_uconst)
15367 return false;
15368 else
15369 {
15370 /* The size is the parameter to DW_OP_plus_uconst. */
15371 uint64_t val;
15372 start = gdb_read_uleb128 (start, end, &val);
15373 if (start == nullptr)
15374 return false;
15375 if ((int) val != val)
15376 return false;
15377 offset = val;
15378 }
15379
15380 if (start == end)
15381 return false;
15382
15383 uint64_t size;
15384 if (*start == DW_OP_deref_size)
15385 {
15386 start = gdb_read_uleb128 (start + 1, end, &size);
15387 if (start == nullptr)
15388 return false;
15389 }
15390 else if (*start == DW_OP_deref)
15391 {
15392 size = cu->header.addr_size;
15393 ++start;
15394 }
15395 else
15396 return false;
15397
15398 field->set_loc_bitpos (8 * offset);
15399 if (size != TYPE_LENGTH (field->type ()))
15400 FIELD_BITSIZE (*field) = 8 * size;
15401
15402 return true;
15403 }
15404
15405 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15406 some kinds of Ada arrays:
15407
15408 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15409 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15410 <11e0> DW_AT_data_location: 2 byte block: 97 6
15411 (DW_OP_push_object_address; DW_OP_deref)
15412 <11e3> DW_AT_type : <0x1173>
15413 <11e7> DW_AT_sibling : <0x1201>
15414 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15415 <11ec> DW_AT_type : <0x1206>
15416 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15417 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15418 DW_OP_deref_size: 4)
15419 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15420 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15421 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15422
15423 This actually represents a "thick pointer", which is a structure
15424 with two elements: one that is a pointer to the array data, and one
15425 that is a pointer to another structure; this second structure holds
15426 the array bounds.
15427
15428 This returns a new type on success, or nullptr if this didn't
15429 recognize the type. */
15430
15431 static struct type *
15432 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
15433 struct type *type)
15434 {
15435 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
15436 /* So far we've only seen this with block form. */
15437 if (attr == nullptr || !attr->form_is_block ())
15438 return nullptr;
15439
15440 /* Note that this will fail if the structure layout is changed by
15441 the compiler. However, we have no good way to recognize some
15442 other layout, because we don't know what expression the compiler
15443 might choose to emit should this happen. */
15444 struct dwarf_block *blk = attr->as_block ();
15445 if (blk->size != 2
15446 || blk->data[0] != DW_OP_push_object_address
15447 || blk->data[1] != DW_OP_deref)
15448 return nullptr;
15449
15450 int bounds_offset = -1;
15451 int max_align = -1;
15452 std::vector<struct field> range_fields;
15453 for (struct die_info *child_die = die->child;
15454 child_die;
15455 child_die = child_die->sibling)
15456 {
15457 if (child_die->tag == DW_TAG_subrange_type)
15458 {
15459 struct type *underlying = read_subrange_index_type (child_die, cu);
15460
15461 int this_align = type_align (underlying);
15462 if (this_align > max_align)
15463 max_align = this_align;
15464
15465 range_fields.emplace_back ();
15466 range_fields.emplace_back ();
15467
15468 struct field &lower = range_fields[range_fields.size () - 2];
15469 struct field &upper = range_fields[range_fields.size () - 1];
15470
15471 lower.set_type (underlying);
15472 FIELD_ARTIFICIAL (lower) = 1;
15473
15474 upper.set_type (underlying);
15475 FIELD_ARTIFICIAL (upper) = 1;
15476
15477 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
15478 &bounds_offset, &lower, cu)
15479 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
15480 &bounds_offset, &upper, cu))
15481 return nullptr;
15482 }
15483 }
15484
15485 /* This shouldn't really happen, but double-check that we found
15486 where the bounds are stored. */
15487 if (bounds_offset == -1)
15488 return nullptr;
15489
15490 struct objfile *objfile = cu->per_objfile->objfile;
15491 for (int i = 0; i < range_fields.size (); i += 2)
15492 {
15493 char name[20];
15494
15495 /* Set the name of each field in the bounds. */
15496 xsnprintf (name, sizeof (name), "LB%d", i / 2);
15497 range_fields[i].set_name (objfile->intern (name));
15498 xsnprintf (name, sizeof (name), "UB%d", i / 2);
15499 range_fields[i + 1].set_name (objfile->intern (name));
15500 }
15501
15502 struct type *bounds = alloc_type (objfile);
15503 bounds->set_code (TYPE_CODE_STRUCT);
15504
15505 bounds->set_num_fields (range_fields.size ());
15506 bounds->set_fields
15507 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
15508 * sizeof (struct field))));
15509 memcpy (bounds->fields (), range_fields.data (),
15510 bounds->num_fields () * sizeof (struct field));
15511
15512 int last_fieldno = range_fields.size () - 1;
15513 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
15514 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
15515 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
15516
15517 /* Rewrite the existing array type in place. Specifically, we
15518 remove any dynamic properties we might have read, and we replace
15519 the index types. */
15520 struct type *iter = type;
15521 for (int i = 0; i < range_fields.size (); i += 2)
15522 {
15523 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
15524 iter->main_type->dyn_prop_list = nullptr;
15525 iter->set_index_type
15526 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
15527 iter = TYPE_TARGET_TYPE (iter);
15528 }
15529
15530 struct type *result = alloc_type (objfile);
15531 result->set_code (TYPE_CODE_STRUCT);
15532
15533 result->set_num_fields (2);
15534 result->set_fields
15535 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
15536 * sizeof (struct field))));
15537
15538 /* The names are chosen to coincide with what the compiler does with
15539 -fgnat-encodings=all, which the Ada code in gdb already
15540 understands. */
15541 result->field (0).set_name ("P_ARRAY");
15542 result->field (0).set_type (lookup_pointer_type (type));
15543
15544 result->field (1).set_name ("P_BOUNDS");
15545 result->field (1).set_type (lookup_pointer_type (bounds));
15546 result->field (1).set_loc_bitpos (8 * bounds_offset);
15547
15548 result->set_name (type->name ());
15549 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
15550 + TYPE_LENGTH (result->field (1).type ()));
15551
15552 return result;
15553 }
15554
15555 /* Extract all information from a DW_TAG_array_type DIE and put it in
15556 the DIE's type field. For now, this only handles one dimensional
15557 arrays. */
15558
15559 static struct type *
15560 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15561 {
15562 struct objfile *objfile = cu->per_objfile->objfile;
15563 struct die_info *child_die;
15564 struct type *type;
15565 struct type *element_type, *range_type, *index_type;
15566 struct attribute *attr;
15567 const char *name;
15568 struct dynamic_prop *byte_stride_prop = NULL;
15569 unsigned int bit_stride = 0;
15570
15571 element_type = die_type (die, cu);
15572
15573 /* The die_type call above may have already set the type for this DIE. */
15574 type = get_die_type (die, cu);
15575 if (type)
15576 return type;
15577
15578 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15579 if (attr != NULL)
15580 {
15581 int stride_ok;
15582 struct type *prop_type = cu->addr_sized_int_type (false);
15583
15584 byte_stride_prop
15585 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15586 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15587 prop_type);
15588 if (!stride_ok)
15589 {
15590 complaint (_("unable to read array DW_AT_byte_stride "
15591 " - DIE at %s [in module %s]"),
15592 sect_offset_str (die->sect_off),
15593 objfile_name (cu->per_objfile->objfile));
15594 /* Ignore this attribute. We will likely not be able to print
15595 arrays of this type correctly, but there is little we can do
15596 to help if we cannot read the attribute's value. */
15597 byte_stride_prop = NULL;
15598 }
15599 }
15600
15601 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15602 if (attr != NULL)
15603 bit_stride = attr->constant_value (0);
15604
15605 /* Irix 6.2 native cc creates array types without children for
15606 arrays with unspecified length. */
15607 if (die->child == NULL)
15608 {
15609 index_type = objfile_type (objfile)->builtin_int;
15610 range_type = create_static_range_type (NULL, index_type, 0, -1);
15611 type = create_array_type_with_stride (NULL, element_type, range_type,
15612 byte_stride_prop, bit_stride);
15613 return set_die_type (die, type, cu);
15614 }
15615
15616 std::vector<struct type *> range_types;
15617 child_die = die->child;
15618 while (child_die && child_die->tag)
15619 {
15620 if (child_die->tag == DW_TAG_subrange_type
15621 || child_die->tag == DW_TAG_generic_subrange)
15622 {
15623 struct type *child_type = read_type_die (child_die, cu);
15624
15625 if (child_type != NULL)
15626 {
15627 /* The range type was succesfully read. Save it for the
15628 array type creation. */
15629 range_types.push_back (child_type);
15630 }
15631 }
15632 child_die = child_die->sibling;
15633 }
15634
15635 if (range_types.empty ())
15636 {
15637 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15638 sect_offset_str (die->sect_off),
15639 objfile_name (cu->per_objfile->objfile));
15640 return NULL;
15641 }
15642
15643 /* Dwarf2 dimensions are output from left to right, create the
15644 necessary array types in backwards order. */
15645
15646 type = element_type;
15647
15648 if (read_array_order (die, cu) == DW_ORD_col_major)
15649 {
15650 int i = 0;
15651
15652 while (i < range_types.size ())
15653 {
15654 type = create_array_type_with_stride (NULL, type, range_types[i++],
15655 byte_stride_prop, bit_stride);
15656 bit_stride = 0;
15657 byte_stride_prop = nullptr;
15658 }
15659 }
15660 else
15661 {
15662 size_t ndim = range_types.size ();
15663 while (ndim-- > 0)
15664 {
15665 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15666 byte_stride_prop, bit_stride);
15667 bit_stride = 0;
15668 byte_stride_prop = nullptr;
15669 }
15670 }
15671
15672 gdb_assert (type != element_type);
15673
15674 /* Understand Dwarf2 support for vector types (like they occur on
15675 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15676 array type. This is not part of the Dwarf2/3 standard yet, but a
15677 custom vendor extension. The main difference between a regular
15678 array and the vector variant is that vectors are passed by value
15679 to functions. */
15680 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15681 if (attr != nullptr)
15682 make_vector_type (type);
15683
15684 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15685 implementation may choose to implement triple vectors using this
15686 attribute. */
15687 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15688 if (attr != nullptr && attr->form_is_unsigned ())
15689 {
15690 if (attr->as_unsigned () >= TYPE_LENGTH (type))
15691 TYPE_LENGTH (type) = attr->as_unsigned ();
15692 else
15693 complaint (_("DW_AT_byte_size for array type smaller "
15694 "than the total size of elements"));
15695 }
15696
15697 name = dwarf2_name (die, cu);
15698 if (name)
15699 type->set_name (name);
15700
15701 maybe_set_alignment (cu, die, type);
15702
15703 struct type *replacement_type = nullptr;
15704 if (cu->per_cu->lang == language_ada)
15705 {
15706 replacement_type = quirk_ada_thick_pointer (die, cu, type);
15707 if (replacement_type != nullptr)
15708 type = replacement_type;
15709 }
15710
15711 /* Install the type in the die. */
15712 set_die_type (die, type, cu, replacement_type != nullptr);
15713
15714 /* set_die_type should be already done. */
15715 set_descriptive_type (type, die, cu);
15716
15717 return type;
15718 }
15719
15720 static enum dwarf_array_dim_ordering
15721 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15722 {
15723 struct attribute *attr;
15724
15725 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15726
15727 if (attr != nullptr)
15728 {
15729 LONGEST val = attr->constant_value (-1);
15730 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
15731 return (enum dwarf_array_dim_ordering) val;
15732 }
15733
15734 /* GNU F77 is a special case, as at 08/2004 array type info is the
15735 opposite order to the dwarf2 specification, but data is still
15736 laid out as per normal fortran.
15737
15738 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15739 version checking. */
15740
15741 if (cu->per_cu->lang == language_fortran
15742 && cu->producer && strstr (cu->producer, "GNU F77"))
15743 {
15744 return DW_ORD_row_major;
15745 }
15746
15747 switch (cu->language_defn->array_ordering ())
15748 {
15749 case array_column_major:
15750 return DW_ORD_col_major;
15751 case array_row_major:
15752 default:
15753 return DW_ORD_row_major;
15754 };
15755 }
15756
15757 /* Extract all information from a DW_TAG_set_type DIE and put it in
15758 the DIE's type field. */
15759
15760 static struct type *
15761 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15762 {
15763 struct type *domain_type, *set_type;
15764 struct attribute *attr;
15765
15766 domain_type = die_type (die, cu);
15767
15768 /* The die_type call above may have already set the type for this DIE. */
15769 set_type = get_die_type (die, cu);
15770 if (set_type)
15771 return set_type;
15772
15773 set_type = create_set_type (NULL, domain_type);
15774
15775 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15776 if (attr != nullptr && attr->form_is_unsigned ())
15777 TYPE_LENGTH (set_type) = attr->as_unsigned ();
15778
15779 maybe_set_alignment (cu, die, set_type);
15780
15781 return set_die_type (die, set_type, cu);
15782 }
15783
15784 /* A helper for read_common_block that creates a locexpr baton.
15785 SYM is the symbol which we are marking as computed.
15786 COMMON_DIE is the DIE for the common block.
15787 COMMON_LOC is the location expression attribute for the common
15788 block itself.
15789 MEMBER_LOC is the location expression attribute for the particular
15790 member of the common block that we are processing.
15791 CU is the CU from which the above come. */
15792
15793 static void
15794 mark_common_block_symbol_computed (struct symbol *sym,
15795 struct die_info *common_die,
15796 struct attribute *common_loc,
15797 struct attribute *member_loc,
15798 struct dwarf2_cu *cu)
15799 {
15800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15801 struct objfile *objfile = per_objfile->objfile;
15802 struct dwarf2_locexpr_baton *baton;
15803 gdb_byte *ptr;
15804 unsigned int cu_off;
15805 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
15806 LONGEST offset = 0;
15807
15808 gdb_assert (common_loc && member_loc);
15809 gdb_assert (common_loc->form_is_block ());
15810 gdb_assert (member_loc->form_is_block ()
15811 || member_loc->form_is_constant ());
15812
15813 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15814 baton->per_objfile = per_objfile;
15815 baton->per_cu = cu->per_cu;
15816 gdb_assert (baton->per_cu);
15817
15818 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15819
15820 if (member_loc->form_is_constant ())
15821 {
15822 offset = member_loc->constant_value (0);
15823 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15824 }
15825 else
15826 baton->size += member_loc->as_block ()->size;
15827
15828 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15829 baton->data = ptr;
15830
15831 *ptr++ = DW_OP_call4;
15832 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15833 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15834 ptr += 4;
15835
15836 if (member_loc->form_is_constant ())
15837 {
15838 *ptr++ = DW_OP_addr;
15839 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15840 ptr += cu->header.addr_size;
15841 }
15842 else
15843 {
15844 /* We have to copy the data here, because DW_OP_call4 will only
15845 use a DW_AT_location attribute. */
15846 struct dwarf_block *block = member_loc->as_block ();
15847 memcpy (ptr, block->data, block->size);
15848 ptr += block->size;
15849 }
15850
15851 *ptr++ = DW_OP_plus;
15852 gdb_assert (ptr - baton->data == baton->size);
15853
15854 SYMBOL_LOCATION_BATON (sym) = baton;
15855 sym->set_aclass_index (dwarf2_locexpr_index);
15856 }
15857
15858 /* Create appropriate locally-scoped variables for all the
15859 DW_TAG_common_block entries. Also create a struct common_block
15860 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15861 is used to separate the common blocks name namespace from regular
15862 variable names. */
15863
15864 static void
15865 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15866 {
15867 struct attribute *attr;
15868
15869 attr = dwarf2_attr (die, DW_AT_location, cu);
15870 if (attr != nullptr)
15871 {
15872 /* Support the .debug_loc offsets. */
15873 if (attr->form_is_block ())
15874 {
15875 /* Ok. */
15876 }
15877 else if (attr->form_is_section_offset ())
15878 {
15879 dwarf2_complex_location_expr_complaint ();
15880 attr = NULL;
15881 }
15882 else
15883 {
15884 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15885 "common block member");
15886 attr = NULL;
15887 }
15888 }
15889
15890 if (die->child != NULL)
15891 {
15892 struct objfile *objfile = cu->per_objfile->objfile;
15893 struct die_info *child_die;
15894 size_t n_entries = 0, size;
15895 struct common_block *common_block;
15896 struct symbol *sym;
15897
15898 for (child_die = die->child;
15899 child_die && child_die->tag;
15900 child_die = child_die->sibling)
15901 ++n_entries;
15902
15903 size = (sizeof (struct common_block)
15904 + (n_entries - 1) * sizeof (struct symbol *));
15905 common_block
15906 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15907 size);
15908 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15909 common_block->n_entries = 0;
15910
15911 for (child_die = die->child;
15912 child_die && child_die->tag;
15913 child_die = child_die->sibling)
15914 {
15915 /* Create the symbol in the DW_TAG_common_block block in the current
15916 symbol scope. */
15917 sym = new_symbol (child_die, NULL, cu);
15918 if (sym != NULL)
15919 {
15920 struct attribute *member_loc;
15921
15922 common_block->contents[common_block->n_entries++] = sym;
15923
15924 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15925 cu);
15926 if (member_loc)
15927 {
15928 /* GDB has handled this for a long time, but it is
15929 not specified by DWARF. It seems to have been
15930 emitted by gfortran at least as recently as:
15931 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15932 complaint (_("Variable in common block has "
15933 "DW_AT_data_member_location "
15934 "- DIE at %s [in module %s]"),
15935 sect_offset_str (child_die->sect_off),
15936 objfile_name (objfile));
15937
15938 if (member_loc->form_is_section_offset ())
15939 dwarf2_complex_location_expr_complaint ();
15940 else if (member_loc->form_is_constant ()
15941 || member_loc->form_is_block ())
15942 {
15943 if (attr != nullptr)
15944 mark_common_block_symbol_computed (sym, die, attr,
15945 member_loc, cu);
15946 }
15947 else
15948 dwarf2_complex_location_expr_complaint ();
15949 }
15950 }
15951 }
15952
15953 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15954 sym->set_value_common_block (common_block);
15955 }
15956 }
15957
15958 /* Create a type for a C++ namespace. */
15959
15960 static struct type *
15961 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15962 {
15963 struct objfile *objfile = cu->per_objfile->objfile;
15964 const char *previous_prefix, *name;
15965 int is_anonymous;
15966 struct type *type;
15967
15968 /* For extensions, reuse the type of the original namespace. */
15969 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
15970 {
15971 struct die_info *ext_die;
15972 struct dwarf2_cu *ext_cu = cu;
15973
15974 ext_die = dwarf2_extension (die, &ext_cu);
15975 type = read_type_die (ext_die, ext_cu);
15976
15977 /* EXT_CU may not be the same as CU.
15978 Ensure TYPE is recorded with CU in die_type_hash. */
15979 return set_die_type (die, type, cu);
15980 }
15981
15982 name = namespace_name (die, &is_anonymous, cu);
15983
15984 /* Now build the name of the current namespace. */
15985
15986 previous_prefix = determine_prefix (die, cu);
15987 if (previous_prefix[0] != '\0')
15988 name = typename_concat (&objfile->objfile_obstack,
15989 previous_prefix, name, 0, cu);
15990
15991 /* Create the type. */
15992 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
15993
15994 return set_die_type (die, type, cu);
15995 }
15996
15997 /* Read a namespace scope. */
15998
15999 static void
16000 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16001 {
16002 struct objfile *objfile = cu->per_objfile->objfile;
16003 int is_anonymous;
16004
16005 /* Add a symbol associated to this if we haven't seen the namespace
16006 before. Also, add a using directive if it's an anonymous
16007 namespace. */
16008
16009 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16010 {
16011 struct type *type;
16012
16013 type = read_type_die (die, cu);
16014 new_symbol (die, type, cu);
16015
16016 namespace_name (die, &is_anonymous, cu);
16017 if (is_anonymous)
16018 {
16019 const char *previous_prefix = determine_prefix (die, cu);
16020
16021 std::vector<const char *> excludes;
16022 add_using_directive (using_directives (cu),
16023 previous_prefix, type->name (), NULL,
16024 NULL, excludes, 0, &objfile->objfile_obstack);
16025 }
16026 }
16027
16028 if (die->child != NULL)
16029 {
16030 struct die_info *child_die = die->child;
16031
16032 while (child_die && child_die->tag)
16033 {
16034 process_die (child_die, cu);
16035 child_die = child_die->sibling;
16036 }
16037 }
16038 }
16039
16040 /* Read a Fortran module as type. This DIE can be only a declaration used for
16041 imported module. Still we need that type as local Fortran "use ... only"
16042 declaration imports depend on the created type in determine_prefix. */
16043
16044 static struct type *
16045 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16046 {
16047 struct objfile *objfile = cu->per_objfile->objfile;
16048 const char *module_name;
16049 struct type *type;
16050
16051 module_name = dwarf2_name (die, cu);
16052 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16053
16054 return set_die_type (die, type, cu);
16055 }
16056
16057 /* Read a Fortran module. */
16058
16059 static void
16060 read_module (struct die_info *die, struct dwarf2_cu *cu)
16061 {
16062 struct die_info *child_die = die->child;
16063 struct type *type;
16064
16065 type = read_type_die (die, cu);
16066 new_symbol (die, type, cu);
16067
16068 while (child_die && child_die->tag)
16069 {
16070 process_die (child_die, cu);
16071 child_die = child_die->sibling;
16072 }
16073 }
16074
16075 /* Return the name of the namespace represented by DIE. Set
16076 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16077 namespace. */
16078
16079 static const char *
16080 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16081 {
16082 struct die_info *current_die;
16083 const char *name = NULL;
16084
16085 /* Loop through the extensions until we find a name. */
16086
16087 for (current_die = die;
16088 current_die != NULL;
16089 current_die = dwarf2_extension (die, &cu))
16090 {
16091 /* We don't use dwarf2_name here so that we can detect the absence
16092 of a name -> anonymous namespace. */
16093 name = dwarf2_string_attr (die, DW_AT_name, cu);
16094
16095 if (name != NULL)
16096 break;
16097 }
16098
16099 /* Is it an anonymous namespace? */
16100
16101 *is_anonymous = (name == NULL);
16102 if (*is_anonymous)
16103 name = CP_ANONYMOUS_NAMESPACE_STR;
16104
16105 return name;
16106 }
16107
16108 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16109 the user defined type vector. */
16110
16111 static struct type *
16112 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16113 {
16114 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16115 struct comp_unit_head *cu_header = &cu->header;
16116 struct type *type;
16117 struct attribute *attr_byte_size;
16118 struct attribute *attr_address_class;
16119 int byte_size, addr_class;
16120 struct type *target_type;
16121
16122 target_type = die_type (die, cu);
16123
16124 /* The die_type call above may have already set the type for this DIE. */
16125 type = get_die_type (die, cu);
16126 if (type)
16127 return type;
16128
16129 type = lookup_pointer_type (target_type);
16130
16131 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16132 if (attr_byte_size)
16133 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
16134 else
16135 byte_size = cu_header->addr_size;
16136
16137 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16138 if (attr_address_class)
16139 addr_class = attr_address_class->constant_value (DW_ADDR_none);
16140 else
16141 addr_class = DW_ADDR_none;
16142
16143 ULONGEST alignment = get_alignment (cu, die);
16144
16145 /* If the pointer size, alignment, or address class is different
16146 than the default, create a type variant marked as such and set
16147 the length accordingly. */
16148 if (TYPE_LENGTH (type) != byte_size
16149 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16150 && alignment != TYPE_RAW_ALIGN (type))
16151 || addr_class != DW_ADDR_none)
16152 {
16153 if (gdbarch_address_class_type_flags_p (gdbarch))
16154 {
16155 type_instance_flags type_flags
16156 = gdbarch_address_class_type_flags (gdbarch, byte_size,
16157 addr_class);
16158 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16159 == 0);
16160 type = make_type_with_address_space (type, type_flags);
16161 }
16162 else if (TYPE_LENGTH (type) != byte_size)
16163 {
16164 complaint (_("invalid pointer size %d"), byte_size);
16165 }
16166 else if (TYPE_RAW_ALIGN (type) != alignment)
16167 {
16168 complaint (_("Invalid DW_AT_alignment"
16169 " - DIE at %s [in module %s]"),
16170 sect_offset_str (die->sect_off),
16171 objfile_name (cu->per_objfile->objfile));
16172 }
16173 else
16174 {
16175 /* Should we also complain about unhandled address classes? */
16176 }
16177 }
16178
16179 TYPE_LENGTH (type) = byte_size;
16180 set_type_align (type, alignment);
16181 return set_die_type (die, type, cu);
16182 }
16183
16184 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16185 the user defined type vector. */
16186
16187 static struct type *
16188 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16189 {
16190 struct type *type;
16191 struct type *to_type;
16192 struct type *domain;
16193
16194 to_type = die_type (die, cu);
16195 domain = die_containing_type (die, cu);
16196
16197 /* The calls above may have already set the type for this DIE. */
16198 type = get_die_type (die, cu);
16199 if (type)
16200 return type;
16201
16202 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16203 type = lookup_methodptr_type (to_type);
16204 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16205 {
16206 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16207
16208 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16209 to_type->fields (), to_type->num_fields (),
16210 to_type->has_varargs ());
16211 type = lookup_methodptr_type (new_type);
16212 }
16213 else
16214 type = lookup_memberptr_type (to_type, domain);
16215
16216 return set_die_type (die, type, cu);
16217 }
16218
16219 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16220 the user defined type vector. */
16221
16222 static struct type *
16223 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16224 enum type_code refcode)
16225 {
16226 struct comp_unit_head *cu_header = &cu->header;
16227 struct type *type, *target_type;
16228 struct attribute *attr;
16229
16230 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16231
16232 target_type = die_type (die, cu);
16233
16234 /* The die_type call above may have already set the type for this DIE. */
16235 type = get_die_type (die, cu);
16236 if (type)
16237 return type;
16238
16239 type = lookup_reference_type (target_type, refcode);
16240 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16241 if (attr != nullptr)
16242 {
16243 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
16244 }
16245 else
16246 {
16247 TYPE_LENGTH (type) = cu_header->addr_size;
16248 }
16249 maybe_set_alignment (cu, die, type);
16250 return set_die_type (die, type, cu);
16251 }
16252
16253 /* Add the given cv-qualifiers to the element type of the array. GCC
16254 outputs DWARF type qualifiers that apply to an array, not the
16255 element type. But GDB relies on the array element type to carry
16256 the cv-qualifiers. This mimics section 6.7.3 of the C99
16257 specification. */
16258
16259 static struct type *
16260 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16261 struct type *base_type, int cnst, int voltl)
16262 {
16263 struct type *el_type, *inner_array;
16264
16265 base_type = copy_type (base_type);
16266 inner_array = base_type;
16267
16268 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16269 {
16270 TYPE_TARGET_TYPE (inner_array) =
16271 copy_type (TYPE_TARGET_TYPE (inner_array));
16272 inner_array = TYPE_TARGET_TYPE (inner_array);
16273 }
16274
16275 el_type = TYPE_TARGET_TYPE (inner_array);
16276 cnst |= TYPE_CONST (el_type);
16277 voltl |= TYPE_VOLATILE (el_type);
16278 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16279
16280 return set_die_type (die, base_type, cu);
16281 }
16282
16283 static struct type *
16284 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16285 {
16286 struct type *base_type, *cv_type;
16287
16288 base_type = die_type (die, cu);
16289
16290 /* The die_type call above may have already set the type for this DIE. */
16291 cv_type = get_die_type (die, cu);
16292 if (cv_type)
16293 return cv_type;
16294
16295 /* In case the const qualifier is applied to an array type, the element type
16296 is so qualified, not the array type (section 6.7.3 of C99). */
16297 if (base_type->code () == TYPE_CODE_ARRAY)
16298 return add_array_cv_type (die, cu, base_type, 1, 0);
16299
16300 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16301 return set_die_type (die, cv_type, cu);
16302 }
16303
16304 static struct type *
16305 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16306 {
16307 struct type *base_type, *cv_type;
16308
16309 base_type = die_type (die, cu);
16310
16311 /* The die_type call above may have already set the type for this DIE. */
16312 cv_type = get_die_type (die, cu);
16313 if (cv_type)
16314 return cv_type;
16315
16316 /* In case the volatile qualifier is applied to an array type, the
16317 element type is so qualified, not the array type (section 6.7.3
16318 of C99). */
16319 if (base_type->code () == TYPE_CODE_ARRAY)
16320 return add_array_cv_type (die, cu, base_type, 0, 1);
16321
16322 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16323 return set_die_type (die, cv_type, cu);
16324 }
16325
16326 /* Handle DW_TAG_restrict_type. */
16327
16328 static struct type *
16329 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16330 {
16331 struct type *base_type, *cv_type;
16332
16333 base_type = die_type (die, cu);
16334
16335 /* The die_type call above may have already set the type for this DIE. */
16336 cv_type = get_die_type (die, cu);
16337 if (cv_type)
16338 return cv_type;
16339
16340 cv_type = make_restrict_type (base_type);
16341 return set_die_type (die, cv_type, cu);
16342 }
16343
16344 /* Handle DW_TAG_atomic_type. */
16345
16346 static struct type *
16347 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16348 {
16349 struct type *base_type, *cv_type;
16350
16351 base_type = die_type (die, cu);
16352
16353 /* The die_type call above may have already set the type for this DIE. */
16354 cv_type = get_die_type (die, cu);
16355 if (cv_type)
16356 return cv_type;
16357
16358 cv_type = make_atomic_type (base_type);
16359 return set_die_type (die, cv_type, cu);
16360 }
16361
16362 /* Extract all information from a DW_TAG_string_type DIE and add to
16363 the user defined type vector. It isn't really a user defined type,
16364 but it behaves like one, with other DIE's using an AT_user_def_type
16365 attribute to reference it. */
16366
16367 static struct type *
16368 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16369 {
16370 struct objfile *objfile = cu->per_objfile->objfile;
16371 struct gdbarch *gdbarch = objfile->arch ();
16372 struct type *type, *range_type, *index_type, *char_type;
16373 struct attribute *attr;
16374 struct dynamic_prop prop;
16375 bool length_is_constant = true;
16376 LONGEST length;
16377
16378 /* There are a couple of places where bit sizes might be made use of
16379 when parsing a DW_TAG_string_type, however, no producer that we know
16380 of make use of these. Handling bit sizes that are a multiple of the
16381 byte size is easy enough, but what about other bit sizes? Lets deal
16382 with that problem when we have to. Warn about these attributes being
16383 unsupported, then parse the type and ignore them like we always
16384 have. */
16385 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16386 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16387 {
16388 static bool warning_printed = false;
16389 if (!warning_printed)
16390 {
16391 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16392 "currently supported on DW_TAG_string_type."));
16393 warning_printed = true;
16394 }
16395 }
16396
16397 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16398 if (attr != nullptr && !attr->form_is_constant ())
16399 {
16400 /* The string length describes the location at which the length of
16401 the string can be found. The size of the length field can be
16402 specified with one of the attributes below. */
16403 struct type *prop_type;
16404 struct attribute *len
16405 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16406 if (len == nullptr)
16407 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16408 if (len != nullptr && len->form_is_constant ())
16409 {
16410 /* Pass 0 as the default as we know this attribute is constant
16411 and the default value will not be returned. */
16412 LONGEST sz = len->constant_value (0);
16413 prop_type = objfile_int_type (objfile, sz, true);
16414 }
16415 else
16416 {
16417 /* If the size is not specified then we assume it is the size of
16418 an address on this target. */
16419 prop_type = cu->addr_sized_int_type (true);
16420 }
16421
16422 /* Convert the attribute into a dynamic property. */
16423 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16424 length = 1;
16425 else
16426 length_is_constant = false;
16427 }
16428 else if (attr != nullptr)
16429 {
16430 /* This DW_AT_string_length just contains the length with no
16431 indirection. There's no need to create a dynamic property in this
16432 case. Pass 0 for the default value as we know it will not be
16433 returned in this case. */
16434 length = attr->constant_value (0);
16435 }
16436 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16437 {
16438 /* We don't currently support non-constant byte sizes for strings. */
16439 length = attr->constant_value (1);
16440 }
16441 else
16442 {
16443 /* Use 1 as a fallback length if we have nothing else. */
16444 length = 1;
16445 }
16446
16447 index_type = objfile_type (objfile)->builtin_int;
16448 if (length_is_constant)
16449 range_type = create_static_range_type (NULL, index_type, 1, length);
16450 else
16451 {
16452 struct dynamic_prop low_bound;
16453
16454 low_bound.set_const_val (1);
16455 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16456 }
16457 char_type = language_string_char_type (cu->language_defn, gdbarch);
16458 type = create_string_type (NULL, char_type, range_type);
16459
16460 return set_die_type (die, type, cu);
16461 }
16462
16463 /* Assuming that DIE corresponds to a function, returns nonzero
16464 if the function is prototyped. */
16465
16466 static int
16467 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16468 {
16469 struct attribute *attr;
16470
16471 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16472 if (attr && attr->as_boolean ())
16473 return 1;
16474
16475 /* The DWARF standard implies that the DW_AT_prototyped attribute
16476 is only meaningful for C, but the concept also extends to other
16477 languages that allow unprototyped functions (Eg: Objective C).
16478 For all other languages, assume that functions are always
16479 prototyped. */
16480 if (cu->per_cu->lang != language_c
16481 && cu->per_cu->lang != language_objc
16482 && cu->per_cu->lang != language_opencl)
16483 return 1;
16484
16485 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16486 prototyped and unprototyped functions; default to prototyped,
16487 since that is more common in modern code (and RealView warns
16488 about unprototyped functions). */
16489 if (producer_is_realview (cu->producer))
16490 return 1;
16491
16492 return 0;
16493 }
16494
16495 /* Handle DIES due to C code like:
16496
16497 struct foo
16498 {
16499 int (*funcp)(int a, long l);
16500 int b;
16501 };
16502
16503 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16504
16505 static struct type *
16506 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16507 {
16508 struct objfile *objfile = cu->per_objfile->objfile;
16509 struct type *type; /* Type that this function returns. */
16510 struct type *ftype; /* Function that returns above type. */
16511 struct attribute *attr;
16512
16513 type = die_type (die, cu);
16514
16515 /* The die_type call above may have already set the type for this DIE. */
16516 ftype = get_die_type (die, cu);
16517 if (ftype)
16518 return ftype;
16519
16520 ftype = lookup_function_type (type);
16521
16522 if (prototyped_function_p (die, cu))
16523 ftype->set_is_prototyped (true);
16524
16525 /* Store the calling convention in the type if it's available in
16526 the subroutine die. Otherwise set the calling convention to
16527 the default value DW_CC_normal. */
16528 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16529 if (attr != nullptr
16530 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
16531 TYPE_CALLING_CONVENTION (ftype)
16532 = (enum dwarf_calling_convention) attr->constant_value (0);
16533 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16534 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16535 else
16536 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16537
16538 /* Record whether the function returns normally to its caller or not
16539 if the DWARF producer set that information. */
16540 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16541 if (attr && attr->as_boolean ())
16542 TYPE_NO_RETURN (ftype) = 1;
16543
16544 /* We need to add the subroutine type to the die immediately so
16545 we don't infinitely recurse when dealing with parameters
16546 declared as the same subroutine type. */
16547 set_die_type (die, ftype, cu);
16548
16549 if (die->child != NULL)
16550 {
16551 struct type *void_type = objfile_type (objfile)->builtin_void;
16552 struct die_info *child_die;
16553 int nparams, iparams;
16554
16555 /* Count the number of parameters.
16556 FIXME: GDB currently ignores vararg functions, but knows about
16557 vararg member functions. */
16558 nparams = 0;
16559 child_die = die->child;
16560 while (child_die && child_die->tag)
16561 {
16562 if (child_die->tag == DW_TAG_formal_parameter)
16563 nparams++;
16564 else if (child_die->tag == DW_TAG_unspecified_parameters)
16565 ftype->set_has_varargs (true);
16566
16567 child_die = child_die->sibling;
16568 }
16569
16570 /* Allocate storage for parameters and fill them in. */
16571 ftype->set_num_fields (nparams);
16572 ftype->set_fields
16573 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
16574
16575 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16576 even if we error out during the parameters reading below. */
16577 for (iparams = 0; iparams < nparams; iparams++)
16578 ftype->field (iparams).set_type (void_type);
16579
16580 iparams = 0;
16581 child_die = die->child;
16582 while (child_die && child_die->tag)
16583 {
16584 if (child_die->tag == DW_TAG_formal_parameter)
16585 {
16586 struct type *arg_type;
16587
16588 /* DWARF version 2 has no clean way to discern C++
16589 static and non-static member functions. G++ helps
16590 GDB by marking the first parameter for non-static
16591 member functions (which is the this pointer) as
16592 artificial. We pass this information to
16593 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16594
16595 DWARF version 3 added DW_AT_object_pointer, which GCC
16596 4.5 does not yet generate. */
16597 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16598 if (attr != nullptr)
16599 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
16600 else
16601 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16602 arg_type = die_type (child_die, cu);
16603
16604 /* RealView does not mark THIS as const, which the testsuite
16605 expects. GCC marks THIS as const in method definitions,
16606 but not in the class specifications (GCC PR 43053). */
16607 if (cu->per_cu->lang == language_cplus
16608 && !TYPE_CONST (arg_type)
16609 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16610 {
16611 int is_this = 0;
16612 struct dwarf2_cu *arg_cu = cu;
16613 const char *name = dwarf2_name (child_die, cu);
16614
16615 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16616 if (attr != nullptr)
16617 {
16618 /* If the compiler emits this, use it. */
16619 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16620 is_this = 1;
16621 }
16622 else if (name && strcmp (name, "this") == 0)
16623 /* Function definitions will have the argument names. */
16624 is_this = 1;
16625 else if (name == NULL && iparams == 0)
16626 /* Declarations may not have the names, so like
16627 elsewhere in GDB, assume an artificial first
16628 argument is "this". */
16629 is_this = 1;
16630
16631 if (is_this)
16632 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16633 arg_type, 0);
16634 }
16635
16636 ftype->field (iparams).set_type (arg_type);
16637 iparams++;
16638 }
16639 child_die = child_die->sibling;
16640 }
16641 }
16642
16643 return ftype;
16644 }
16645
16646 static struct type *
16647 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16648 {
16649 struct objfile *objfile = cu->per_objfile->objfile;
16650 const char *name = NULL;
16651 struct type *this_type, *target_type;
16652
16653 name = dwarf2_full_name (NULL, die, cu);
16654 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16655 this_type->set_target_is_stub (true);
16656 set_die_type (die, this_type, cu);
16657 target_type = die_type (die, cu);
16658 if (target_type != this_type)
16659 TYPE_TARGET_TYPE (this_type) = target_type;
16660 else
16661 {
16662 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16663 spec and cause infinite loops in GDB. */
16664 complaint (_("Self-referential DW_TAG_typedef "
16665 "- DIE at %s [in module %s]"),
16666 sect_offset_str (die->sect_off), objfile_name (objfile));
16667 TYPE_TARGET_TYPE (this_type) = NULL;
16668 }
16669 if (name == NULL)
16670 {
16671 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16672 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16673 Handle these by just returning the target type, rather than
16674 constructing an anonymous typedef type and trying to handle this
16675 elsewhere. */
16676 set_die_type (die, target_type, cu);
16677 return target_type;
16678 }
16679 return this_type;
16680 }
16681
16682 /* Helper for get_dwarf2_rational_constant that computes the value of
16683 a given gmp_mpz given an attribute. */
16684
16685 static void
16686 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
16687 {
16688 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16689 location expression that pushes an implicit value. */
16690 if (attr->form == DW_FORM_exprloc)
16691 {
16692 dwarf_block *blk = attr->as_block ();
16693 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
16694 {
16695 uint64_t len;
16696 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
16697 blk->data + blk->size,
16698 &len);
16699 if (ptr - blk->data + len <= blk->size)
16700 {
16701 mpz_import (value->val, len,
16702 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
16703 1, 0, 0, ptr);
16704 return;
16705 }
16706 }
16707
16708 /* On failure set it to 1. */
16709 *value = gdb_mpz (1);
16710 }
16711 else if (attr->form_is_block ())
16712 {
16713 dwarf_block *blk = attr->as_block ();
16714 mpz_import (value->val, blk->size,
16715 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
16716 1, 0, 0, blk->data);
16717 }
16718 else
16719 *value = gdb_mpz (attr->constant_value (1));
16720 }
16721
16722 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16723 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16724
16725 If the numerator and/or numerator attribute is missing,
16726 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16727 untouched. */
16728
16729 static void
16730 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
16731 gdb_mpz *numerator, gdb_mpz *denominator)
16732 {
16733 struct attribute *num_attr, *denom_attr;
16734
16735 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
16736 if (num_attr == nullptr)
16737 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16738 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16739
16740 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
16741 if (denom_attr == nullptr)
16742 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16743 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16744
16745 if (num_attr == nullptr || denom_attr == nullptr)
16746 return;
16747
16748 get_mpz (cu, numerator, num_attr);
16749 get_mpz (cu, denominator, denom_attr);
16750 }
16751
16752 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16753 rational constant, rather than a signed one.
16754
16755 If the rational constant has a negative value, a complaint
16756 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16757
16758 static void
16759 get_dwarf2_unsigned_rational_constant (struct die_info *die,
16760 struct dwarf2_cu *cu,
16761 gdb_mpz *numerator,
16762 gdb_mpz *denominator)
16763 {
16764 gdb_mpz num (1);
16765 gdb_mpz denom (1);
16766
16767 get_dwarf2_rational_constant (die, cu, &num, &denom);
16768 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
16769 {
16770 mpz_neg (num.val, num.val);
16771 mpz_neg (denom.val, denom.val);
16772 }
16773 else if (mpz_sgn (num.val) == -1)
16774 {
16775 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16776 " in DIE at %s"),
16777 sect_offset_str (die->sect_off));
16778 return;
16779 }
16780 else if (mpz_sgn (denom.val) == -1)
16781 {
16782 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16783 " in DIE at %s"),
16784 sect_offset_str (die->sect_off));
16785 return;
16786 }
16787
16788 *numerator = std::move (num);
16789 *denominator = std::move (denom);
16790 }
16791
16792 /* Assuming that ENCODING is a string whose contents starting at the
16793 K'th character is "_nn" where "nn" is a decimal number, scan that
16794 number and set RESULT to the value. K is updated to point to the
16795 character immediately following the number.
16796
16797 If the string does not conform to the format described above, false
16798 is returned, and K may or may not be changed. */
16799
16800 static bool
16801 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
16802 {
16803 /* The next character should be an underscore ('_') followed
16804 by a digit. */
16805 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
16806 return false;
16807
16808 /* Skip the underscore. */
16809 k++;
16810 int start = k;
16811
16812 /* Determine the number of digits for our number. */
16813 while (isdigit (encoding[k]))
16814 k++;
16815 if (k == start)
16816 return false;
16817
16818 std::string copy (&encoding[start], k - start);
16819 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
16820 return false;
16821
16822 return true;
16823 }
16824
16825 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16826 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16827 DENOM, update OFFSET, and return true on success. Return false on
16828 failure. */
16829
16830 static bool
16831 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
16832 gdb_mpz *num, gdb_mpz *denom)
16833 {
16834 if (!ada_get_gnat_encoded_number (encoding, offset, num))
16835 return false;
16836 return ada_get_gnat_encoded_number (encoding, offset, denom);
16837 }
16838
16839 /* Assuming DIE corresponds to a fixed point type, finish the creation
16840 of the corresponding TYPE by setting its type-specific data. CU is
16841 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16842 encodings. It is nullptr if the GNAT encoding should be
16843 ignored. */
16844
16845 static void
16846 finish_fixed_point_type (struct type *type, const char *suffix,
16847 struct die_info *die, struct dwarf2_cu *cu)
16848 {
16849 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
16850 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
16851
16852 /* If GNAT encodings are preferred, don't examine the
16853 attributes. */
16854 struct attribute *attr = nullptr;
16855 if (suffix == nullptr)
16856 {
16857 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
16858 if (attr == nullptr)
16859 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
16860 if (attr == nullptr)
16861 attr = dwarf2_attr (die, DW_AT_small, cu);
16862 }
16863
16864 /* Numerator and denominator of our fixed-point type's scaling factor.
16865 The default is a scaling factor of 1, which we use as a fallback
16866 when we are not able to decode it (problem with the debugging info,
16867 unsupported forms, bug in GDB, etc...). Using that as the default
16868 allows us to at least print the unscaled value, which might still
16869 be useful to a user. */
16870 gdb_mpz scale_num (1);
16871 gdb_mpz scale_denom (1);
16872
16873 if (attr == nullptr)
16874 {
16875 int offset = 0;
16876 if (suffix != nullptr
16877 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16878 &scale_denom)
16879 /* The number might be encoded as _nn_dd_nn_dd, where the
16880 second ratio is the 'small value. In this situation, we
16881 want the second value. */
16882 && (suffix[offset] != '_'
16883 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16884 &scale_denom)))
16885 {
16886 /* Found it. */
16887 }
16888 else
16889 {
16890 /* Scaling factor not found. Assume a scaling factor of 1,
16891 and hope for the best. At least the user will be able to
16892 see the encoded value. */
16893 scale_num = 1;
16894 scale_denom = 1;
16895 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16896 sect_offset_str (die->sect_off));
16897 }
16898 }
16899 else if (attr->name == DW_AT_binary_scale)
16900 {
16901 LONGEST scale_exp = attr->constant_value (0);
16902 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16903
16904 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
16905 }
16906 else if (attr->name == DW_AT_decimal_scale)
16907 {
16908 LONGEST scale_exp = attr->constant_value (0);
16909 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16910
16911 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
16912 }
16913 else if (attr->name == DW_AT_small)
16914 {
16915 struct die_info *scale_die;
16916 struct dwarf2_cu *scale_cu = cu;
16917
16918 scale_die = follow_die_ref (die, attr, &scale_cu);
16919 if (scale_die->tag == DW_TAG_constant)
16920 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
16921 &scale_num, &scale_denom);
16922 else
16923 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16924 " (DIE at %s)"),
16925 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16926 }
16927 else
16928 {
16929 complaint (_("unsupported scale attribute %s for fixed-point type"
16930 " (DIE at %s)"),
16931 dwarf_attr_name (attr->name),
16932 sect_offset_str (die->sect_off));
16933 }
16934
16935 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
16936 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
16937 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
16938 mpq_canonicalize (scaling_factor.val);
16939 }
16940
16941 /* The gnat-encoding suffix for fixed point. */
16942
16943 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16944
16945 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16946 "XF" suffix of the name. The text after this is what encodes the
16947 'small and 'delta information. Otherwise, return nullptr. */
16948
16949 static const char *
16950 gnat_encoded_fixed_point_type_info (const char *name)
16951 {
16952 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
16953 }
16954
16955 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16956 (which may be different from NAME) to the architecture back-end to allow
16957 it to guess the correct format if necessary. */
16958
16959 static struct type *
16960 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16961 const char *name_hint, enum bfd_endian byte_order)
16962 {
16963 struct gdbarch *gdbarch = objfile->arch ();
16964 const struct floatformat **format;
16965 struct type *type;
16966
16967 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16968 if (format)
16969 type = init_float_type (objfile, bits, name, format, byte_order);
16970 else
16971 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16972
16973 return type;
16974 }
16975
16976 /* Allocate an integer type of size BITS and name NAME. */
16977
16978 static struct type *
16979 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16980 int bits, int unsigned_p, const char *name)
16981 {
16982 struct type *type;
16983
16984 /* Versions of Intel's C Compiler generate an integer type called "void"
16985 instead of using DW_TAG_unspecified_type. This has been seen on
16986 at least versions 14, 17, and 18. */
16987 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16988 && strcmp (name, "void") == 0)
16989 type = objfile_type (objfile)->builtin_void;
16990 else
16991 type = init_integer_type (objfile, bits, unsigned_p, name);
16992
16993 return type;
16994 }
16995
16996 /* Return true if DIE has a DW_AT_small attribute whose value is
16997 a constant rational, where both the numerator and denominator
16998 are equal to zero.
16999
17000 CU is the DIE's Compilation Unit. */
17001
17002 static bool
17003 has_zero_over_zero_small_attribute (struct die_info *die,
17004 struct dwarf2_cu *cu)
17005 {
17006 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17007 if (attr == nullptr)
17008 return false;
17009
17010 struct dwarf2_cu *scale_cu = cu;
17011 struct die_info *scale_die
17012 = follow_die_ref (die, attr, &scale_cu);
17013
17014 if (scale_die->tag != DW_TAG_constant)
17015 return false;
17016
17017 gdb_mpz num (1), denom (1);
17018 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
17019 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
17020 }
17021
17022 /* Initialise and return a floating point type of size BITS suitable for
17023 use as a component of a complex number. The NAME_HINT is passed through
17024 when initialising the floating point type and is the name of the complex
17025 type.
17026
17027 As DWARF doesn't currently provide an explicit name for the components
17028 of a complex number, but it can be helpful to have these components
17029 named, we try to select a suitable name based on the size of the
17030 component. */
17031 static struct type *
17032 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17033 struct objfile *objfile,
17034 int bits, const char *name_hint,
17035 enum bfd_endian byte_order)
17036 {
17037 gdbarch *gdbarch = objfile->arch ();
17038 struct type *tt = nullptr;
17039
17040 /* Try to find a suitable floating point builtin type of size BITS.
17041 We're going to use the name of this type as the name for the complex
17042 target type that we are about to create. */
17043 switch (cu->per_cu->lang)
17044 {
17045 case language_fortran:
17046 switch (bits)
17047 {
17048 case 32:
17049 tt = builtin_f_type (gdbarch)->builtin_real;
17050 break;
17051 case 64:
17052 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17053 break;
17054 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17055 case 128:
17056 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17057 break;
17058 }
17059 break;
17060 default:
17061 switch (bits)
17062 {
17063 case 32:
17064 tt = builtin_type (gdbarch)->builtin_float;
17065 break;
17066 case 64:
17067 tt = builtin_type (gdbarch)->builtin_double;
17068 break;
17069 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17070 case 128:
17071 tt = builtin_type (gdbarch)->builtin_long_double;
17072 break;
17073 }
17074 break;
17075 }
17076
17077 /* If the type we found doesn't match the size we were looking for, then
17078 pretend we didn't find a type at all, the complex target type we
17079 create will then be nameless. */
17080 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17081 tt = nullptr;
17082
17083 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17084 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17085 }
17086
17087 /* Find a representation of a given base type and install
17088 it in the TYPE field of the die. */
17089
17090 static struct type *
17091 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17092 {
17093 struct objfile *objfile = cu->per_objfile->objfile;
17094 struct type *type;
17095 struct attribute *attr;
17096 int encoding = 0, bits = 0;
17097 const char *name;
17098 gdbarch *arch;
17099
17100 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17101 if (attr != nullptr && attr->form_is_constant ())
17102 encoding = attr->constant_value (0);
17103 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17104 if (attr != nullptr)
17105 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17106 name = dwarf2_name (die, cu);
17107 if (!name)
17108 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17109
17110 arch = objfile->arch ();
17111 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17112
17113 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17114 if (attr != nullptr && attr->form_is_constant ())
17115 {
17116 int endianity = attr->constant_value (0);
17117
17118 switch (endianity)
17119 {
17120 case DW_END_big:
17121 byte_order = BFD_ENDIAN_BIG;
17122 break;
17123 case DW_END_little:
17124 byte_order = BFD_ENDIAN_LITTLE;
17125 break;
17126 default:
17127 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17128 break;
17129 }
17130 }
17131
17132 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
17133 && cu->per_cu->lang == language_ada
17134 && has_zero_over_zero_small_attribute (die, cu))
17135 {
17136 /* brobecker/2018-02-24: This is a fixed point type for which
17137 the scaling factor is represented as fraction whose value
17138 does not make sense (zero divided by zero), so we should
17139 normally never see these. However, there is a small category
17140 of fixed point types for which GNAT is unable to provide
17141 the scaling factor via the standard DWARF mechanisms, and
17142 for which the info is provided via the GNAT encodings instead.
17143 This is likely what this DIE is about. */
17144 encoding = (encoding == DW_ATE_signed_fixed
17145 ? DW_ATE_signed
17146 : DW_ATE_unsigned);
17147 }
17148
17149 /* With GNAT encodings, fixed-point information will be encoded in
17150 the type name. Note that this can also occur with the above
17151 zero-over-zero case, which is why this is a separate "if" rather
17152 than an "else if". */
17153 const char *gnat_encoding_suffix = nullptr;
17154 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
17155 && cu->per_cu->lang == language_ada
17156 && name != nullptr)
17157 {
17158 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
17159 if (gnat_encoding_suffix != nullptr)
17160 {
17161 gdb_assert (startswith (gnat_encoding_suffix,
17162 GNAT_FIXED_POINT_SUFFIX));
17163 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
17164 name, gnat_encoding_suffix - name);
17165 /* Use -1 here so that SUFFIX points at the "_" after the
17166 "XF". */
17167 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
17168
17169 encoding = (encoding == DW_ATE_signed
17170 ? DW_ATE_signed_fixed
17171 : DW_ATE_unsigned_fixed);
17172 }
17173 }
17174
17175 switch (encoding)
17176 {
17177 case DW_ATE_address:
17178 /* Turn DW_ATE_address into a void * pointer. */
17179 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17180 type = init_pointer_type (objfile, bits, name, type);
17181 break;
17182 case DW_ATE_boolean:
17183 type = init_boolean_type (objfile, bits, 1, name);
17184 break;
17185 case DW_ATE_complex_float:
17186 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17187 byte_order);
17188 if (type->code () == TYPE_CODE_ERROR)
17189 {
17190 if (name == nullptr)
17191 {
17192 struct obstack *obstack
17193 = &cu->per_objfile->objfile->objfile_obstack;
17194 name = obconcat (obstack, "_Complex ", type->name (),
17195 nullptr);
17196 }
17197 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17198 }
17199 else
17200 type = init_complex_type (name, type);
17201 break;
17202 case DW_ATE_decimal_float:
17203 type = init_decfloat_type (objfile, bits, name);
17204 break;
17205 case DW_ATE_float:
17206 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17207 break;
17208 case DW_ATE_signed:
17209 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17210 break;
17211 case DW_ATE_unsigned:
17212 if (cu->per_cu->lang == language_fortran
17213 && name
17214 && startswith (name, "character("))
17215 type = init_character_type (objfile, bits, 1, name);
17216 else
17217 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17218 break;
17219 case DW_ATE_signed_char:
17220 if (cu->per_cu->lang == language_ada
17221 || cu->per_cu->lang == language_m2
17222 || cu->per_cu->lang == language_pascal
17223 || cu->per_cu->lang == language_fortran)
17224 type = init_character_type (objfile, bits, 0, name);
17225 else
17226 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17227 break;
17228 case DW_ATE_unsigned_char:
17229 if (cu->per_cu->lang == language_ada
17230 || cu->per_cu->lang == language_m2
17231 || cu->per_cu->lang == language_pascal
17232 || cu->per_cu->lang == language_fortran
17233 || cu->per_cu->lang == language_rust)
17234 type = init_character_type (objfile, bits, 1, name);
17235 else
17236 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17237 break;
17238 case DW_ATE_UTF:
17239 {
17240 type = init_character_type (objfile, bits, 1, name);
17241 return set_die_type (die, type, cu);
17242 }
17243 break;
17244 case DW_ATE_signed_fixed:
17245 type = init_fixed_point_type (objfile, bits, 0, name);
17246 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17247 break;
17248 case DW_ATE_unsigned_fixed:
17249 type = init_fixed_point_type (objfile, bits, 1, name);
17250 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17251 break;
17252
17253 default:
17254 complaint (_("unsupported DW_AT_encoding: '%s'"),
17255 dwarf_type_encoding_name (encoding));
17256 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17257 break;
17258 }
17259
17260 if (type->code () == TYPE_CODE_INT
17261 && name != nullptr
17262 && strcmp (name, "char") == 0)
17263 type->set_has_no_signedness (true);
17264
17265 maybe_set_alignment (cu, die, type);
17266
17267 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
17268
17269 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
17270 {
17271 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
17272 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
17273 {
17274 unsigned real_bit_size = attr->as_unsigned ();
17275 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
17276 /* Only use the attributes if they make sense together. */
17277 if (attr == nullptr
17278 || (attr->as_unsigned () + real_bit_size
17279 <= 8 * TYPE_LENGTH (type)))
17280 {
17281 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
17282 = real_bit_size;
17283 if (attr != nullptr)
17284 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
17285 = attr->as_unsigned ();
17286 }
17287 }
17288 }
17289
17290 return set_die_type (die, type, cu);
17291 }
17292
17293 /* A helper function that returns the name of DIE, if it refers to a
17294 variable declaration. */
17295
17296 static const char *
17297 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
17298 {
17299 if (die->tag != DW_TAG_variable)
17300 return nullptr;
17301
17302 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
17303 if (attr == nullptr || !attr->as_boolean ())
17304 return nullptr;
17305
17306 attr = dwarf2_attr (die, DW_AT_name, cu);
17307 if (attr == nullptr)
17308 return nullptr;
17309 return attr->as_string ();
17310 }
17311
17312 /* Parse dwarf attribute if it's a block, reference or constant and put the
17313 resulting value of the attribute into struct bound_prop.
17314 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17315
17316 static int
17317 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17318 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17319 struct type *default_type)
17320 {
17321 struct dwarf2_property_baton *baton;
17322 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17323 struct objfile *objfile = per_objfile->objfile;
17324 struct obstack *obstack = &objfile->objfile_obstack;
17325
17326 gdb_assert (default_type != NULL);
17327
17328 if (attr == NULL || prop == NULL)
17329 return 0;
17330
17331 if (attr->form_is_block ())
17332 {
17333 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17334 baton->property_type = default_type;
17335 baton->locexpr.per_cu = cu->per_cu;
17336 baton->locexpr.per_objfile = per_objfile;
17337
17338 struct dwarf_block *block;
17339 if (attr->form == DW_FORM_data16)
17340 {
17341 size_t data_size = 16;
17342 block = XOBNEW (obstack, struct dwarf_block);
17343 block->size = (data_size
17344 + 2 /* Extra bytes for DW_OP and arg. */);
17345 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
17346 data[0] = DW_OP_implicit_value;
17347 data[1] = data_size;
17348 memcpy (&data[2], attr->as_block ()->data, data_size);
17349 block->data = data;
17350 }
17351 else
17352 block = attr->as_block ();
17353
17354 baton->locexpr.size = block->size;
17355 baton->locexpr.data = block->data;
17356 switch (attr->name)
17357 {
17358 case DW_AT_string_length:
17359 baton->locexpr.is_reference = true;
17360 break;
17361 default:
17362 baton->locexpr.is_reference = false;
17363 break;
17364 }
17365
17366 prop->set_locexpr (baton);
17367 gdb_assert (prop->baton () != NULL);
17368 }
17369 else if (attr->form_is_ref ())
17370 {
17371 struct dwarf2_cu *target_cu = cu;
17372 struct die_info *target_die;
17373 struct attribute *target_attr;
17374
17375 target_die = follow_die_ref (die, attr, &target_cu);
17376 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17377 if (target_attr == NULL)
17378 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17379 target_cu);
17380 if (target_attr == nullptr)
17381 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
17382 target_cu);
17383 if (target_attr == NULL)
17384 {
17385 const char *name = var_decl_name (target_die, target_cu);
17386 if (name != nullptr)
17387 {
17388 prop->set_variable_name (name);
17389 return 1;
17390 }
17391 return 0;
17392 }
17393
17394 switch (target_attr->name)
17395 {
17396 case DW_AT_location:
17397 if (target_attr->form_is_section_offset ())
17398 {
17399 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17400 baton->property_type = die_type (target_die, target_cu);
17401 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17402 prop->set_loclist (baton);
17403 gdb_assert (prop->baton () != NULL);
17404 }
17405 else if (target_attr->form_is_block ())
17406 {
17407 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17408 baton->property_type = die_type (target_die, target_cu);
17409 baton->locexpr.per_cu = cu->per_cu;
17410 baton->locexpr.per_objfile = per_objfile;
17411 struct dwarf_block *block = target_attr->as_block ();
17412 baton->locexpr.size = block->size;
17413 baton->locexpr.data = block->data;
17414 baton->locexpr.is_reference = true;
17415 prop->set_locexpr (baton);
17416 gdb_assert (prop->baton () != NULL);
17417 }
17418 else
17419 {
17420 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17421 "dynamic property");
17422 return 0;
17423 }
17424 break;
17425 case DW_AT_data_member_location:
17426 case DW_AT_data_bit_offset:
17427 {
17428 LONGEST offset;
17429
17430 if (!handle_member_location (target_die, target_cu, &offset))
17431 return 0;
17432
17433 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17434 baton->property_type = read_type_die (target_die->parent,
17435 target_cu);
17436 baton->offset_info.offset = offset;
17437 baton->offset_info.type = die_type (target_die, target_cu);
17438 prop->set_addr_offset (baton);
17439 break;
17440 }
17441 }
17442 }
17443 else if (attr->form_is_constant ())
17444 prop->set_const_val (attr->constant_value (0));
17445 else if (attr->form_is_section_offset ())
17446 {
17447 switch (attr->name)
17448 {
17449 case DW_AT_string_length:
17450 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17451 baton->property_type = default_type;
17452 fill_in_loclist_baton (cu, &baton->loclist, attr);
17453 prop->set_loclist (baton);
17454 gdb_assert (prop->baton () != NULL);
17455 break;
17456 default:
17457 goto invalid;
17458 }
17459 }
17460 else
17461 goto invalid;
17462
17463 return 1;
17464
17465 invalid:
17466 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17467 dwarf2_name (die, cu));
17468 return 0;
17469 }
17470
17471 /* See read.h. */
17472
17473 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17474 present (which is valid) then compute the default type based on the
17475 compilation units address size. */
17476
17477 static struct type *
17478 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17479 {
17480 struct type *index_type = die_type (die, cu);
17481
17482 /* Dwarf-2 specifications explicitly allows to create subrange types
17483 without specifying a base type.
17484 In that case, the base type must be set to the type of
17485 the lower bound, upper bound or count, in that order, if any of these
17486 three attributes references an object that has a type.
17487 If no base type is found, the Dwarf-2 specifications say that
17488 a signed integer type of size equal to the size of an address should
17489 be used.
17490 For the following C code: `extern char gdb_int [];'
17491 GCC produces an empty range DIE.
17492 FIXME: muller/2010-05-28: Possible references to object for low bound,
17493 high bound or count are not yet handled by this code. */
17494 if (index_type->code () == TYPE_CODE_VOID)
17495 index_type = cu->addr_sized_int_type (false);
17496
17497 return index_type;
17498 }
17499
17500 /* Read the given DW_AT_subrange DIE. */
17501
17502 static struct type *
17503 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17504 {
17505 struct type *base_type, *orig_base_type;
17506 struct type *range_type;
17507 struct attribute *attr;
17508 struct dynamic_prop low, high;
17509 int low_default_is_valid;
17510 int high_bound_is_count = 0;
17511 const char *name;
17512 ULONGEST negative_mask;
17513
17514 orig_base_type = read_subrange_index_type (die, cu);
17515
17516 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17517 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17518 creating the range type, but we use the result of check_typedef
17519 when examining properties of the type. */
17520 base_type = check_typedef (orig_base_type);
17521
17522 /* The die_type call above may have already set the type for this DIE. */
17523 range_type = get_die_type (die, cu);
17524 if (range_type)
17525 return range_type;
17526
17527 high.set_const_val (0);
17528
17529 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17530 omitting DW_AT_lower_bound. */
17531 switch (cu->per_cu->lang)
17532 {
17533 case language_c:
17534 case language_cplus:
17535 low.set_const_val (0);
17536 low_default_is_valid = 1;
17537 break;
17538 case language_fortran:
17539 low.set_const_val (1);
17540 low_default_is_valid = 1;
17541 break;
17542 case language_d:
17543 case language_objc:
17544 case language_rust:
17545 low.set_const_val (0);
17546 low_default_is_valid = (cu->header.version >= 4);
17547 break;
17548 case language_ada:
17549 case language_m2:
17550 case language_pascal:
17551 low.set_const_val (1);
17552 low_default_is_valid = (cu->header.version >= 4);
17553 break;
17554 default:
17555 low.set_const_val (0);
17556 low_default_is_valid = 0;
17557 break;
17558 }
17559
17560 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17561 if (attr != nullptr)
17562 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17563 else if (!low_default_is_valid)
17564 complaint (_("Missing DW_AT_lower_bound "
17565 "- DIE at %s [in module %s]"),
17566 sect_offset_str (die->sect_off),
17567 objfile_name (cu->per_objfile->objfile));
17568
17569 struct attribute *attr_ub, *attr_count;
17570 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17571 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17572 {
17573 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17574 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17575 {
17576 /* If bounds are constant do the final calculation here. */
17577 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
17578 high.set_const_val (low.const_val () + high.const_val () - 1);
17579 else
17580 high_bound_is_count = 1;
17581 }
17582 else
17583 {
17584 if (attr_ub != NULL)
17585 complaint (_("Unresolved DW_AT_upper_bound "
17586 "- DIE at %s [in module %s]"),
17587 sect_offset_str (die->sect_off),
17588 objfile_name (cu->per_objfile->objfile));
17589 if (attr_count != NULL)
17590 complaint (_("Unresolved DW_AT_count "
17591 "- DIE at %s [in module %s]"),
17592 sect_offset_str (die->sect_off),
17593 objfile_name (cu->per_objfile->objfile));
17594 }
17595 }
17596
17597 LONGEST bias = 0;
17598 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17599 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17600 bias = bias_attr->constant_value (0);
17601
17602 /* Normally, the DWARF producers are expected to use a signed
17603 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17604 But this is unfortunately not always the case, as witnessed
17605 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17606 is used instead. To work around that ambiguity, we treat
17607 the bounds as signed, and thus sign-extend their values, when
17608 the base type is signed. */
17609 negative_mask =
17610 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17611 if (low.kind () == PROP_CONST
17612 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
17613 low.set_const_val (low.const_val () | negative_mask);
17614 if (high.kind () == PROP_CONST
17615 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
17616 high.set_const_val (high.const_val () | negative_mask);
17617
17618 /* Check for bit and byte strides. */
17619 struct dynamic_prop byte_stride_prop;
17620 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17621 if (attr_byte_stride != nullptr)
17622 {
17623 struct type *prop_type = cu->addr_sized_int_type (false);
17624 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17625 prop_type);
17626 }
17627
17628 struct dynamic_prop bit_stride_prop;
17629 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17630 if (attr_bit_stride != nullptr)
17631 {
17632 /* It only makes sense to have either a bit or byte stride. */
17633 if (attr_byte_stride != nullptr)
17634 {
17635 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17636 "- DIE at %s [in module %s]"),
17637 sect_offset_str (die->sect_off),
17638 objfile_name (cu->per_objfile->objfile));
17639 attr_bit_stride = nullptr;
17640 }
17641 else
17642 {
17643 struct type *prop_type = cu->addr_sized_int_type (false);
17644 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17645 prop_type);
17646 }
17647 }
17648
17649 if (attr_byte_stride != nullptr
17650 || attr_bit_stride != nullptr)
17651 {
17652 bool byte_stride_p = (attr_byte_stride != nullptr);
17653 struct dynamic_prop *stride
17654 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17655
17656 range_type
17657 = create_range_type_with_stride (NULL, orig_base_type, &low,
17658 &high, bias, stride, byte_stride_p);
17659 }
17660 else
17661 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17662
17663 if (high_bound_is_count)
17664 range_type->bounds ()->flag_upper_bound_is_count = 1;
17665
17666 /* Ada expects an empty array on no boundary attributes. */
17667 if (attr == NULL && cu->per_cu->lang != language_ada)
17668 range_type->bounds ()->high.set_undefined ();
17669
17670 name = dwarf2_name (die, cu);
17671 if (name)
17672 range_type->set_name (name);
17673
17674 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17675 if (attr != nullptr)
17676 TYPE_LENGTH (range_type) = attr->constant_value (0);
17677
17678 maybe_set_alignment (cu, die, range_type);
17679
17680 set_die_type (die, range_type, cu);
17681
17682 /* set_die_type should be already done. */
17683 set_descriptive_type (range_type, die, cu);
17684
17685 return range_type;
17686 }
17687
17688 static struct type *
17689 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17690 {
17691 struct type *type;
17692
17693 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17694 type->set_name (dwarf2_name (die, cu));
17695
17696 /* In Ada, an unspecified type is typically used when the description
17697 of the type is deferred to a different unit. When encountering
17698 such a type, we treat it as a stub, and try to resolve it later on,
17699 when needed. */
17700 if (cu->per_cu->lang == language_ada)
17701 type->set_is_stub (true);
17702
17703 return set_die_type (die, type, cu);
17704 }
17705
17706 /* Read a single die and all its descendents. Set the die's sibling
17707 field to NULL; set other fields in the die correctly, and set all
17708 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17709 location of the info_ptr after reading all of those dies. PARENT
17710 is the parent of the die in question. */
17711
17712 static struct die_info *
17713 read_die_and_children (const struct die_reader_specs *reader,
17714 const gdb_byte *info_ptr,
17715 const gdb_byte **new_info_ptr,
17716 struct die_info *parent)
17717 {
17718 struct die_info *die;
17719 const gdb_byte *cur_ptr;
17720
17721 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17722 if (die == NULL)
17723 {
17724 *new_info_ptr = cur_ptr;
17725 return NULL;
17726 }
17727 store_in_ref_table (die, reader->cu);
17728
17729 if (die->has_children)
17730 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17731 else
17732 {
17733 die->child = NULL;
17734 *new_info_ptr = cur_ptr;
17735 }
17736
17737 die->sibling = NULL;
17738 die->parent = parent;
17739 return die;
17740 }
17741
17742 /* Read a die, all of its descendents, and all of its siblings; set
17743 all of the fields of all of the dies correctly. Arguments are as
17744 in read_die_and_children. */
17745
17746 static struct die_info *
17747 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17748 const gdb_byte *info_ptr,
17749 const gdb_byte **new_info_ptr,
17750 struct die_info *parent)
17751 {
17752 struct die_info *first_die, *last_sibling;
17753 const gdb_byte *cur_ptr;
17754
17755 cur_ptr = info_ptr;
17756 first_die = last_sibling = NULL;
17757
17758 while (1)
17759 {
17760 struct die_info *die
17761 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17762
17763 if (die == NULL)
17764 {
17765 *new_info_ptr = cur_ptr;
17766 return first_die;
17767 }
17768
17769 if (!first_die)
17770 first_die = die;
17771 else
17772 last_sibling->sibling = die;
17773
17774 last_sibling = die;
17775 }
17776 }
17777
17778 /* Read a die, all of its descendents, and all of its siblings; set
17779 all of the fields of all of the dies correctly. Arguments are as
17780 in read_die_and_children.
17781 This the main entry point for reading a DIE and all its children. */
17782
17783 static struct die_info *
17784 read_die_and_siblings (const struct die_reader_specs *reader,
17785 const gdb_byte *info_ptr,
17786 const gdb_byte **new_info_ptr,
17787 struct die_info *parent)
17788 {
17789 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17790 new_info_ptr, parent);
17791
17792 if (dwarf_die_debug)
17793 {
17794 gdb_printf (gdb_stdlog,
17795 "Read die from %s@0x%x of %s:\n",
17796 reader->die_section->get_name (),
17797 (unsigned) (info_ptr - reader->die_section->buffer),
17798 bfd_get_filename (reader->abfd));
17799 dump_die (die, dwarf_die_debug);
17800 }
17801
17802 return die;
17803 }
17804
17805 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17806 attributes.
17807 The caller is responsible for filling in the extra attributes
17808 and updating (*DIEP)->num_attrs.
17809 Set DIEP to point to a newly allocated die with its information,
17810 except for its child, sibling, and parent fields. */
17811
17812 static const gdb_byte *
17813 read_full_die_1 (const struct die_reader_specs *reader,
17814 struct die_info **diep, const gdb_byte *info_ptr,
17815 int num_extra_attrs)
17816 {
17817 unsigned int abbrev_number, bytes_read, i;
17818 const struct abbrev_info *abbrev;
17819 struct die_info *die;
17820 struct dwarf2_cu *cu = reader->cu;
17821 bfd *abfd = reader->abfd;
17822
17823 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17824 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17825 info_ptr += bytes_read;
17826 if (!abbrev_number)
17827 {
17828 *diep = NULL;
17829 return info_ptr;
17830 }
17831
17832 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17833 if (!abbrev)
17834 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17835 abbrev_number,
17836 bfd_get_filename (abfd));
17837
17838 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17839 die->sect_off = sect_off;
17840 die->tag = abbrev->tag;
17841 die->abbrev = abbrev_number;
17842 die->has_children = abbrev->has_children;
17843
17844 /* Make the result usable.
17845 The caller needs to update num_attrs after adding the extra
17846 attributes. */
17847 die->num_attrs = abbrev->num_attrs;
17848
17849 bool any_need_reprocess = false;
17850 for (i = 0; i < abbrev->num_attrs; ++i)
17851 {
17852 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17853 info_ptr);
17854 if (die->attrs[i].requires_reprocessing_p ())
17855 any_need_reprocess = true;
17856 }
17857
17858 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17859 if (attr != nullptr && attr->form_is_unsigned ())
17860 cu->str_offsets_base = attr->as_unsigned ();
17861
17862 attr = die->attr (DW_AT_loclists_base);
17863 if (attr != nullptr)
17864 cu->loclist_base = attr->as_unsigned ();
17865
17866 auto maybe_addr_base = die->addr_base ();
17867 if (maybe_addr_base.has_value ())
17868 cu->addr_base = *maybe_addr_base;
17869
17870 attr = die->attr (DW_AT_rnglists_base);
17871 if (attr != nullptr)
17872 cu->rnglists_base = attr->as_unsigned ();
17873
17874 if (any_need_reprocess)
17875 {
17876 for (i = 0; i < abbrev->num_attrs; ++i)
17877 {
17878 if (die->attrs[i].requires_reprocessing_p ())
17879 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
17880 }
17881 }
17882 *diep = die;
17883 return info_ptr;
17884 }
17885
17886 /* Read a die and all its attributes.
17887 Set DIEP to point to a newly allocated die with its information,
17888 except for its child, sibling, and parent fields. */
17889
17890 static const gdb_byte *
17891 read_full_die (const struct die_reader_specs *reader,
17892 struct die_info **diep, const gdb_byte *info_ptr)
17893 {
17894 const gdb_byte *result;
17895
17896 result = read_full_die_1 (reader, diep, info_ptr, 0);
17897
17898 if (dwarf_die_debug)
17899 {
17900 gdb_printf (gdb_stdlog,
17901 "Read die from %s@0x%x of %s:\n",
17902 reader->die_section->get_name (),
17903 (unsigned) (info_ptr - reader->die_section->buffer),
17904 bfd_get_filename (reader->abfd));
17905 dump_die (*diep, dwarf_die_debug);
17906 }
17907
17908 return result;
17909 }
17910 \f
17911
17912 void
17913 cooked_indexer::check_bounds (cutu_reader *reader)
17914 {
17915 if (reader->cu->per_cu->addresses_seen)
17916 return;
17917
17918 dwarf2_cu *cu = reader->cu;
17919
17920 CORE_ADDR best_lowpc = 0, best_highpc = 0;
17921 /* Possibly set the default values of LOWPC and HIGHPC from
17922 `DW_AT_ranges'. */
17923 dwarf2_find_base_address (reader->comp_unit_die, cu);
17924 enum pc_bounds_kind cu_bounds_kind
17925 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
17926 cu, m_index_storage->get_addrmap (), cu->per_cu);
17927 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
17928 {
17929 struct objfile *objfile = cu->per_objfile->objfile;
17930 CORE_ADDR baseaddr = objfile->text_section_offset ();
17931 struct gdbarch *gdbarch = objfile->arch ();
17932 CORE_ADDR low
17933 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
17934 - baseaddr);
17935 CORE_ADDR high
17936 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
17937 - baseaddr - 1);
17938 /* Store the contiguous range if it is not empty; it can be
17939 empty for CUs with no code. */
17940 addrmap_set_empty (m_index_storage->get_addrmap (), low, high,
17941 cu->per_cu);
17942
17943 cu->per_cu->addresses_seen = true;
17944 }
17945 }
17946
17947 /* Helper function that returns true if TAG can have a linkage
17948 name. */
17949
17950 static bool
17951 tag_can_have_linkage_name (enum dwarf_tag tag)
17952 {
17953 switch (tag)
17954 {
17955 /* We include types here because an anonymous C++ type might
17956 have a name for linkage purposes. */
17957 case DW_TAG_class_type:
17958 case DW_TAG_structure_type:
17959 case DW_TAG_union_type:
17960 case DW_TAG_variable:
17961 case DW_TAG_subprogram:
17962 return true;
17963
17964 default:
17965 return false;
17966 }
17967 }
17968
17969 cutu_reader *
17970 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
17971 dwarf2_per_objfile *per_objfile,
17972 sect_offset sect_off, bool is_dwz,
17973 bool for_scanning)
17974 {
17975 /* Lookups for type unit references are always in the CU, and
17976 cross-CU references will crash. */
17977 if (reader->cu->per_cu->is_dwz == is_dwz
17978 && reader->cu->header.offset_in_cu_p (sect_off))
17979 return reader;
17980
17981 dwarf2_per_cu_data *per_cu
17982 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
17983 per_objfile->per_bfd);
17984
17985 /* When scanning, we only want to visit a given CU a single time.
17986 Doing this check here avoids self-imports as well. */
17987 if (for_scanning)
17988 {
17989 bool nope = false;
17990 if (!per_cu->scanned.compare_exchange_strong (nope, true))
17991 return nullptr;
17992 }
17993 if (per_cu == m_per_cu)
17994 return reader;
17995
17996 cutu_reader *result = m_index_storage->get_reader (per_cu);
17997 if (result == nullptr)
17998 {
17999 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
18000 m_index_storage->get_abbrev_cache ());
18001
18002 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
18003 language_minimal);
18004 std::unique_ptr<cutu_reader> copy
18005 (new cutu_reader (std::move (new_reader)));
18006 result = m_index_storage->preserve (std::move (copy));
18007 }
18008
18009 if (result->dummy_p || !result->comp_unit_die->has_children)
18010 return nullptr;
18011
18012 if (for_scanning)
18013 check_bounds (result);
18014
18015 return result;
18016 }
18017
18018 const gdb_byte *
18019 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
18020 cutu_reader *reader,
18021 const gdb_byte *watermark_ptr,
18022 const gdb_byte *info_ptr,
18023 const abbrev_info *abbrev,
18024 const char **name,
18025 const char **linkage_name,
18026 cooked_index_flag *flags,
18027 sect_offset *sibling_offset,
18028 const cooked_index_entry **parent_entry,
18029 CORE_ADDR *maybe_defer,
18030 bool for_specification)
18031 {
18032 bool origin_is_dwz = false;
18033 bool is_declaration = false;
18034 sect_offset origin_offset {};
18035
18036 gdb::optional<CORE_ADDR> low_pc;
18037 gdb::optional<CORE_ADDR> high_pc;
18038 bool high_pc_relative = false;
18039
18040 for (int i = 0; i < abbrev->num_attrs; ++i)
18041 {
18042 attribute attr;
18043 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18044 if (attr.requires_reprocessing_p ())
18045 read_attribute_reprocess (reader, &attr, abbrev->tag);
18046
18047 /* Store the data if it is of an attribute we want to keep in a
18048 partial symbol table. */
18049 switch (attr.name)
18050 {
18051 case DW_AT_name:
18052 switch (abbrev->tag)
18053 {
18054 case DW_TAG_compile_unit:
18055 case DW_TAG_partial_unit:
18056 case DW_TAG_type_unit:
18057 /* Compilation units have a DW_AT_name that is a filename, not
18058 a source language identifier. */
18059 break;
18060
18061 default:
18062 if (*name == nullptr)
18063 *name = attr.as_string ();
18064 break;
18065 }
18066 break;
18067
18068 case DW_AT_linkage_name:
18069 case DW_AT_MIPS_linkage_name:
18070 /* Note that both forms of linkage name might appear. We
18071 assume they will be the same, and we only store the last
18072 one we see. */
18073 if (*linkage_name == nullptr)
18074 *linkage_name = attr.as_string ();
18075 break;
18076
18077 case DW_AT_main_subprogram:
18078 if (attr.as_boolean ())
18079 *flags |= IS_MAIN;
18080 break;
18081
18082 case DW_AT_declaration:
18083 is_declaration = attr.as_boolean ();
18084 break;
18085
18086 case DW_AT_sibling:
18087 if (sibling_offset != nullptr)
18088 *sibling_offset = attr.get_ref_die_offset ();
18089 break;
18090
18091 case DW_AT_specification:
18092 case DW_AT_abstract_origin:
18093 case DW_AT_extension:
18094 origin_offset = attr.get_ref_die_offset ();
18095 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
18096 break;
18097
18098 case DW_AT_external:
18099 if (attr.as_boolean ())
18100 *flags &= ~IS_STATIC;
18101 break;
18102
18103 case DW_AT_enum_class:
18104 if (attr.as_boolean ())
18105 *flags |= IS_ENUM_CLASS;
18106 break;
18107
18108 case DW_AT_low_pc:
18109 low_pc = attr.as_address ();
18110 break;
18111
18112 case DW_AT_high_pc:
18113 high_pc = attr.as_address ();
18114 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
18115 high_pc_relative = true;
18116 break;
18117
18118 case DW_AT_location:
18119 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
18120 {
18121 struct dwarf_block *locdesc = attr.as_block ();
18122 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
18123 if (addr != 0
18124 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
18125 {
18126 low_pc = addr;
18127 /* For variables, we don't want to try decoding the
18128 type just to find the size -- for gdb's purposes
18129 we only need the address of a variable. */
18130 high_pc = addr + 1;
18131 high_pc_relative = false;
18132 }
18133 }
18134 break;
18135
18136 case DW_AT_ranges:
18137 if (!scanning_per_cu->addresses_seen)
18138 {
18139 /* Offset in the .debug_ranges or .debug_rnglist section
18140 (depending on DWARF version). */
18141 ULONGEST ranges_offset = attr.as_unsigned ();
18142
18143 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18144 want to add this value. */
18145 ranges_offset += reader->cu->gnu_ranges_base;
18146
18147 CORE_ADDR lowpc, highpc;
18148 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
18149 m_index_storage->get_addrmap (),
18150 scanning_per_cu, abbrev->tag);
18151 }
18152 break;
18153 }
18154 }
18155
18156 /* We don't want to examine declarations, but if we found a
18157 declaration when handling DW_AT_specification or the like, then
18158 that is ok. Similarly, we allow an external variable without a
18159 location; those are resolved via minimal symbols. */
18160 if (is_declaration && !for_specification
18161 && (abbrev->tag != DW_TAG_variable
18162 || (*flags & IS_STATIC) != 0))
18163 {
18164 *linkage_name = nullptr;
18165 *name = nullptr;
18166 }
18167 else if ((*name == nullptr
18168 || (*linkage_name == nullptr
18169 && tag_can_have_linkage_name (abbrev->tag))
18170 || (*parent_entry == nullptr && m_language != language_c))
18171 && origin_offset != sect_offset (0))
18172 {
18173 cutu_reader *new_reader
18174 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
18175 origin_is_dwz, false);
18176 if (new_reader != nullptr)
18177 {
18178 const gdb_byte *new_info_ptr = (new_reader->buffer
18179 + to_underlying (origin_offset));
18180
18181 if (new_reader->cu == reader->cu
18182 && new_info_ptr > watermark_ptr
18183 && maybe_defer != nullptr
18184 && *parent_entry == nullptr)
18185 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
18186 else if (*parent_entry == nullptr)
18187 {
18188 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
18189 *parent_entry
18190 = (cooked_index_entry *) addrmap_find (m_die_range_map,
18191 lookup);
18192 }
18193
18194 unsigned int bytes_read;
18195 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
18196 new_info_ptr,
18197 &bytes_read);
18198 new_info_ptr += bytes_read;
18199 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
18200 new_abbrev, name, linkage_name, flags, nullptr,
18201 parent_entry, maybe_defer, true);
18202 }
18203 }
18204
18205 if (!for_specification)
18206 {
18207 if (m_language == language_ada
18208 && *linkage_name == nullptr)
18209 *linkage_name = *name;
18210
18211 if (!scanning_per_cu->addresses_seen
18212 && low_pc.has_value ()
18213 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
18214 || *low_pc != 0)
18215 && high_pc.has_value ())
18216 {
18217 if (high_pc_relative)
18218 high_pc = *high_pc + *low_pc;
18219
18220 if (*high_pc > *low_pc)
18221 {
18222 struct objfile *objfile = reader->cu->per_objfile->objfile;
18223 CORE_ADDR baseaddr = objfile->text_section_offset ();
18224 struct gdbarch *gdbarch = objfile->arch ();
18225 CORE_ADDR lo
18226 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
18227 - baseaddr);
18228 CORE_ADDR hi
18229 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
18230 - baseaddr);
18231 addrmap_set_empty (m_index_storage->get_addrmap (), lo, hi - 1,
18232 scanning_per_cu);
18233 }
18234 }
18235
18236 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
18237 *flags &= ~IS_STATIC;
18238
18239 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
18240 *name = "(anonymous namespace)";
18241
18242 if (m_language == language_cplus
18243 && (abbrev->tag == DW_TAG_class_type
18244 || abbrev->tag == DW_TAG_interface_type
18245 || abbrev->tag == DW_TAG_structure_type
18246 || abbrev->tag == DW_TAG_union_type
18247 || abbrev->tag == DW_TAG_enumeration_type
18248 || abbrev->tag == DW_TAG_enumerator))
18249 *flags &= ~IS_STATIC;
18250 }
18251
18252 return info_ptr;
18253 }
18254
18255 const gdb_byte *
18256 cooked_indexer::index_imported_unit (cutu_reader *reader,
18257 const gdb_byte *info_ptr,
18258 const abbrev_info *abbrev)
18259 {
18260 sect_offset sect_off {};
18261 bool is_dwz = false;
18262
18263 for (int i = 0; i < abbrev->num_attrs; ++i)
18264 {
18265 /* Note that we never need to reprocess attributes here. */
18266 attribute attr;
18267 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18268
18269 if (attr.name == DW_AT_import)
18270 {
18271 sect_off = attr.get_ref_die_offset ();
18272 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18273 || reader->cu->per_cu->is_dwz);
18274 }
18275 }
18276
18277 /* Did not find DW_AT_import. */
18278 if (sect_off == sect_offset (0))
18279 return info_ptr;
18280
18281 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
18282 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
18283 is_dwz, true);
18284 if (new_reader != nullptr)
18285 {
18286 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
18287
18288 reader->cu->add_dependence (new_reader->cu->per_cu);
18289 }
18290
18291 return info_ptr;
18292 }
18293
18294 const gdb_byte *
18295 cooked_indexer::recurse (cutu_reader *reader,
18296 const gdb_byte *info_ptr,
18297 const cooked_index_entry *parent_entry,
18298 bool fully)
18299 {
18300 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18301
18302 if (parent_entry != nullptr)
18303 {
18304 CORE_ADDR start = form_addr (parent_entry->die_offset,
18305 reader->cu->per_cu->is_dwz);
18306 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
18307 reader->cu->per_cu->is_dwz);
18308 addrmap_set_empty (m_die_range_map, start, end, (void *) parent_entry);
18309 }
18310
18311 return info_ptr;
18312 }
18313
18314 const gdb_byte *
18315 cooked_indexer::index_dies (cutu_reader *reader,
18316 const gdb_byte *info_ptr,
18317 const cooked_index_entry *parent_entry,
18318 bool fully)
18319 {
18320 const gdb_byte *end_ptr = info_ptr + reader->cu->header.get_length ();
18321
18322 while (info_ptr < end_ptr)
18323 {
18324 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
18325 unsigned int bytes_read;
18326 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18327 &bytes_read);
18328 info_ptr += bytes_read;
18329 if (abbrev == nullptr)
18330 break;
18331
18332 if (abbrev->tag == DW_TAG_imported_unit)
18333 {
18334 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
18335 continue;
18336 }
18337
18338 if (!abbrev->interesting)
18339 {
18340 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
18341 if (fully && abbrev->has_children)
18342 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18343 continue;
18344 }
18345
18346 const char *name = nullptr;
18347 const char *linkage_name = nullptr;
18348 CORE_ADDR defer = 0;
18349 cooked_index_flag flags = IS_STATIC;
18350 sect_offset sibling {};
18351 const cooked_index_entry *this_parent_entry = parent_entry;
18352 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
18353 info_ptr, abbrev, &name, &linkage_name,
18354 &flags, &sibling, &this_parent_entry,
18355 &defer, false);
18356
18357 if (abbrev->tag == DW_TAG_namespace
18358 && m_language == language_cplus
18359 && strcmp (name, "::") == 0)
18360 {
18361 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18362 generated bogus DW_TAG_namespace DIEs with a name of "::"
18363 for the global namespace. Work around this problem
18364 here. */
18365 name = nullptr;
18366 }
18367
18368 const cooked_index_entry *this_entry = nullptr;
18369 if (name != nullptr)
18370 {
18371 if (defer != 0)
18372 m_deferred_entries.push_back ({
18373 this_die, name, defer, abbrev->tag, flags
18374 });
18375 else
18376 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
18377 name, this_parent_entry,
18378 m_per_cu);
18379 }
18380
18381 if (linkage_name != nullptr)
18382 {
18383 /* We only want this to be "main" if it has a linkage name
18384 but not an ordinary name. */
18385 if (name != nullptr)
18386 flags = flags & ~IS_MAIN;
18387 m_index_storage->add (this_die, abbrev->tag, flags | IS_LINKAGE,
18388 linkage_name, nullptr, m_per_cu);
18389 }
18390
18391 if (abbrev->has_children)
18392 {
18393 switch (abbrev->tag)
18394 {
18395 case DW_TAG_class_type:
18396 case DW_TAG_interface_type:
18397 case DW_TAG_structure_type:
18398 case DW_TAG_union_type:
18399 if (m_language != language_c && this_entry != nullptr)
18400 {
18401 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18402 continue;
18403 }
18404 break;
18405
18406 case DW_TAG_enumeration_type:
18407 /* We need to recurse even for an anonymous enumeration.
18408 Which scope we record as the parent scope depends on
18409 whether we're reading an "enum class". If so, we use
18410 the enum itself as the parent, yielding names like
18411 "enum_class::enumerator"; otherwise we inject the
18412 names into our own parent scope. */
18413 info_ptr = recurse (reader, info_ptr,
18414 ((flags & IS_ENUM_CLASS) == 0)
18415 ? parent_entry
18416 : this_entry,
18417 fully);
18418 continue;
18419
18420 case DW_TAG_module:
18421 if (this_entry == nullptr)
18422 break;
18423 /* FALLTHROUGH */
18424 case DW_TAG_namespace:
18425 /* We don't check THIS_ENTRY for a namespace, to handle
18426 the ancient G++ workaround pointed out above. */
18427 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18428 continue;
18429
18430 case DW_TAG_subprogram:
18431 if ((m_language == language_fortran
18432 || m_language == language_ada)
18433 && this_entry != nullptr)
18434 {
18435 info_ptr = recurse (reader, info_ptr, this_entry, true);
18436 continue;
18437 }
18438 break;
18439 }
18440
18441 if (sibling != sect_offset (0))
18442 {
18443 const gdb_byte *sibling_ptr
18444 = reader->buffer + to_underlying (sibling);
18445
18446 if (sibling_ptr < info_ptr)
18447 complaint (_("DW_AT_sibling points backwards"));
18448 else if (sibling_ptr > reader->buffer_end)
18449 reader->die_section->overflow_complaint ();
18450 else
18451 info_ptr = sibling_ptr;
18452 }
18453 else
18454 info_ptr = skip_children (reader, info_ptr);
18455 }
18456 }
18457
18458 return info_ptr;
18459 }
18460
18461 void
18462 cooked_indexer::make_index (cutu_reader *reader)
18463 {
18464 check_bounds (reader);
18465 find_file_and_directory (reader->comp_unit_die, reader->cu);
18466 if (!reader->comp_unit_die->has_children)
18467 return;
18468 index_dies (reader, reader->info_ptr, nullptr, false);
18469
18470 for (const auto &entry : m_deferred_entries)
18471 {
18472 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
18473 cooked_index_entry *parent
18474 = (cooked_index_entry *) addrmap_find (m_die_range_map, key);
18475 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
18476 entry.name, parent, m_per_cu);
18477 }
18478 }
18479
18480 /* An implementation of quick_symbol_functions for the cooked DWARF
18481 index. */
18482
18483 struct cooked_index_functions : public dwarf2_base_index_functions
18484 {
18485 struct compunit_symtab *find_pc_sect_compunit_symtab
18486 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
18487 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
18488
18489 struct compunit_symtab *find_compunit_symtab_by_address
18490 (struct objfile *objfile, CORE_ADDR address) override;
18491
18492 void dump (struct objfile *objfile) override
18493 {
18494 gdb_printf ("Cooked index in use\n");
18495 }
18496
18497 void expand_matching_symbols
18498 (struct objfile *,
18499 const lookup_name_info &lookup_name,
18500 domain_enum domain,
18501 int global,
18502 symbol_compare_ftype *ordered_compare) override;
18503
18504 bool expand_symtabs_matching
18505 (struct objfile *objfile,
18506 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18507 const lookup_name_info *lookup_name,
18508 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18509 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18510 block_search_flags search_flags,
18511 domain_enum domain,
18512 enum search_domain kind) override;
18513
18514 bool can_lazily_read_symbols () override
18515 {
18516 return true;
18517 }
18518
18519 void read_partial_symbols (struct objfile *objfile) override
18520 {
18521 if (dwarf2_has_info (objfile, nullptr))
18522 dwarf2_build_psymtabs (objfile, true);
18523 }
18524 };
18525
18526 struct compunit_symtab *
18527 cooked_index_functions::find_pc_sect_compunit_symtab
18528 (struct objfile *objfile,
18529 struct bound_minimal_symbol msymbol,
18530 CORE_ADDR pc,
18531 struct obj_section *section,
18532 int warn_if_readin)
18533 {
18534 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18535 if (per_objfile->per_bfd->cooked_index_table == nullptr)
18536 return nullptr;
18537
18538 CORE_ADDR baseaddr = objfile->text_section_offset ();
18539 dwarf2_per_cu_data *per_cu
18540 = per_objfile->per_bfd->cooked_index_table->lookup (pc - baseaddr);
18541 if (per_cu == nullptr)
18542 return nullptr;
18543
18544 if (warn_if_readin && per_objfile->symtab_set_p (per_cu))
18545 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
18546 paddress (objfile->arch (), pc));
18547
18548 compunit_symtab *result = (recursively_find_pc_sect_compunit_symtab
18549 (dw2_instantiate_symtab (per_cu, per_objfile,
18550 false),
18551 pc));
18552 gdb_assert (result != nullptr);
18553 return result;
18554 }
18555
18556 struct compunit_symtab *
18557 cooked_index_functions::find_compunit_symtab_by_address
18558 (struct objfile *objfile, CORE_ADDR address)
18559 {
18560 if (objfile->sect_index_data == -1)
18561 return nullptr;
18562
18563 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18564 if (per_objfile->per_bfd->cooked_index_table == nullptr)
18565 return nullptr;
18566
18567 CORE_ADDR baseaddr = objfile->data_section_offset ();
18568 dwarf2_per_cu_data *per_cu
18569 = per_objfile->per_bfd->cooked_index_table->lookup (address - baseaddr);
18570 if (per_cu == nullptr)
18571 return nullptr;
18572
18573 return dw2_instantiate_symtab (per_cu, per_objfile, false);
18574 }
18575
18576 void
18577 cooked_index_functions::expand_matching_symbols
18578 (struct objfile *objfile,
18579 const lookup_name_info &lookup_name,
18580 domain_enum domain,
18581 int global,
18582 symbol_compare_ftype *ordered_compare)
18583 {
18584 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18585 if (per_objfile->per_bfd->cooked_index_table == nullptr)
18586 return;
18587 const block_search_flags search_flags = (global
18588 ? SEARCH_GLOBAL_BLOCK
18589 : SEARCH_STATIC_BLOCK);
18590 const language_defn *lang = language_def (language_ada);
18591 symbol_name_matcher_ftype *name_match
18592 = lang->get_symbol_name_matcher (lookup_name);
18593
18594 for (const cooked_index_entry *entry
18595 : per_objfile->per_bfd->cooked_index_table->all_entries ())
18596 {
18597 if (entry->parent_entry != nullptr)
18598 continue;
18599
18600 if (!entry->matches (search_flags)
18601 || !entry->matches (domain))
18602 continue;
18603
18604 if (name_match (entry->canonical, lookup_name, nullptr))
18605 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
18606 }
18607 }
18608
18609 bool
18610 cooked_index_functions::expand_symtabs_matching
18611 (struct objfile *objfile,
18612 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18613 const lookup_name_info *lookup_name,
18614 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18615 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18616 block_search_flags search_flags,
18617 domain_enum domain,
18618 enum search_domain kind)
18619 {
18620 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18621 if (per_objfile->per_bfd->cooked_index_table == nullptr)
18622 return true;
18623
18624 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
18625
18626 /* This invariant is documented in quick-functions.h. */
18627 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
18628 if (lookup_name == nullptr)
18629 {
18630 for (dwarf2_per_cu_data *per_cu
18631 : all_comp_units_range (per_objfile->per_bfd))
18632 {
18633 QUIT;
18634
18635 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
18636 file_matcher,
18637 expansion_notify))
18638 return false;
18639 }
18640 return true;
18641 }
18642
18643 lookup_name_info lookup_name_without_params
18644 = lookup_name->make_ignore_params ();
18645 bool completing = lookup_name->completion_mode ();
18646
18647 /* Unique styles of language splitting. */
18648 static const enum language unique_styles[] =
18649 {
18650 /* No splitting is also a style. */
18651 language_c,
18652 /* This includes Rust. */
18653 language_cplus,
18654 /* This includes Go. */
18655 language_d,
18656 language_ada
18657 };
18658
18659 for (enum language lang : unique_styles)
18660 {
18661 std::vector<gdb::string_view> name_vec
18662 = lookup_name_without_params.split_name (lang);
18663
18664 for (const cooked_index_entry *entry
18665 : per_objfile->per_bfd->cooked_index_table->find (name_vec.back (),
18666 completing))
18667 {
18668 /* No need to consider symbols from expanded CUs. */
18669 if (per_objfile->symtab_set_p (entry->per_cu))
18670 continue;
18671
18672 /* If file-matching was done, we don't need to consider
18673 symbols from unmarked CUs. */
18674 if (file_matcher != nullptr && !entry->per_cu->mark)
18675 continue;
18676
18677 /* See if the symbol matches the type filter. */
18678 if (!entry->matches (search_flags)
18679 || !entry->matches (domain)
18680 || !entry->matches (kind))
18681 continue;
18682
18683 /* We've found the base name of the symbol; now walk its
18684 parentage chain, ensuring that each component
18685 matches. */
18686 bool found = true;
18687
18688 const cooked_index_entry *parent = entry->parent_entry;
18689 for (int i = name_vec.size () - 1; i > 0; --i)
18690 {
18691 /* If we ran out of entries, or if this segment doesn't
18692 match, this did not match. */
18693 if (parent == nullptr
18694 || strncmp (parent->name, name_vec[i - 1].data (),
18695 name_vec[i - 1].length ()) != 0)
18696 {
18697 found = false;
18698 break;
18699 }
18700
18701 parent = parent->parent_entry;
18702 }
18703
18704 if (!found)
18705 continue;
18706
18707 /* Might have been looking for "a::b" and found
18708 "x::a::b". */
18709 if (symbol_matcher == nullptr)
18710 {
18711 symbol_name_match_type match_type
18712 = lookup_name_without_params.match_type ();
18713 if ((match_type == symbol_name_match_type::FULL
18714 || (lang != language_ada
18715 && match_type == symbol_name_match_type::EXPRESSION))
18716 && parent != nullptr)
18717 continue;
18718 }
18719 else
18720 {
18721 auto_obstack temp_storage;
18722 const char *full_name = entry->full_name (&temp_storage);
18723 if (!symbol_matcher (full_name))
18724 continue;
18725 }
18726
18727 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
18728 file_matcher,
18729 expansion_notify))
18730 return false;
18731 }
18732 }
18733
18734 return true;
18735 }
18736
18737 /* Return a new cooked_index_functions object. */
18738
18739 static quick_symbol_functions_up
18740 make_cooked_index_funcs ()
18741 {
18742 return quick_symbol_functions_up (new cooked_index_functions);
18743 }
18744
18745 \f
18746
18747 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18748 contents from the given SECTION in the HEADER.
18749
18750 HEADER_OFFSET is the offset of the header in the section. */
18751 static void
18752 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
18753 struct dwarf2_section_info *section,
18754 sect_offset header_offset)
18755 {
18756 unsigned int bytes_read;
18757 bfd *abfd = section->get_bfd_owner ();
18758 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
18759
18760 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18761 info_ptr += bytes_read;
18762
18763 header->version = read_2_bytes (abfd, info_ptr);
18764 info_ptr += 2;
18765
18766 header->addr_size = read_1_byte (abfd, info_ptr);
18767 info_ptr += 1;
18768
18769 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18770 info_ptr += 1;
18771
18772 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18773 }
18774
18775 /* Return the DW_AT_loclists_base value for the CU. */
18776 static ULONGEST
18777 lookup_loclist_base (struct dwarf2_cu *cu)
18778 {
18779 /* For the .dwo unit, the loclist_base points to the first offset following
18780 the header. The header consists of the following entities-
18781 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18782 bit format)
18783 2. version (2 bytes)
18784 3. address size (1 byte)
18785 4. segment selector size (1 byte)
18786 5. offset entry count (4 bytes)
18787 These sizes are derived as per the DWARFv5 standard. */
18788 if (cu->dwo_unit != nullptr)
18789 {
18790 if (cu->header.initial_length_size == 4)
18791 return LOCLIST_HEADER_SIZE32;
18792 return LOCLIST_HEADER_SIZE64;
18793 }
18794 return cu->loclist_base;
18795 }
18796
18797 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18798 array of offsets in the .debug_loclists section. */
18799
18800 static sect_offset
18801 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18802 {
18803 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18804 struct objfile *objfile = per_objfile->objfile;
18805 bfd *abfd = objfile->obfd;
18806 ULONGEST loclist_header_size =
18807 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
18808 : LOCLIST_HEADER_SIZE64);
18809 ULONGEST loclist_base = lookup_loclist_base (cu);
18810
18811 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18812 ULONGEST start_offset =
18813 loclist_base + loclist_index * cu->header.offset_size;
18814
18815 /* Get loclists section. */
18816 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18817
18818 /* Read the loclists section content. */
18819 section->read (objfile);
18820 if (section->buffer == NULL)
18821 error (_("DW_FORM_loclistx used without .debug_loclists "
18822 "section [in module %s]"), objfile_name (objfile));
18823
18824 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18825 so if loclist_base is smaller than the header size, we have a problem. */
18826 if (loclist_base < loclist_header_size)
18827 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18828 objfile_name (objfile));
18829
18830 /* Read the header of the loclists contribution. */
18831 struct loclists_rnglists_header header;
18832 read_loclists_rnglists_header (&header, section,
18833 (sect_offset) (loclist_base - loclist_header_size));
18834
18835 /* Verify the loclist index is valid. */
18836 if (loclist_index >= header.offset_entry_count)
18837 error (_("DW_FORM_loclistx pointing outside of "
18838 ".debug_loclists offset array [in module %s]"),
18839 objfile_name (objfile));
18840
18841 /* Validate that reading won't go beyond the end of the section. */
18842 if (start_offset + cu->header.offset_size > section->size)
18843 error (_("Reading DW_FORM_loclistx index beyond end of"
18844 ".debug_loclists section [in module %s]"),
18845 objfile_name (objfile));
18846
18847 const gdb_byte *info_ptr = section->buffer + start_offset;
18848
18849 if (cu->header.offset_size == 4)
18850 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
18851 else
18852 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
18853 }
18854
18855 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18856 array of offsets in the .debug_rnglists section. */
18857
18858 static sect_offset
18859 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
18860 dwarf_tag tag)
18861 {
18862 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18863 struct objfile *objfile = dwarf2_per_objfile->objfile;
18864 bfd *abfd = objfile->obfd;
18865 ULONGEST rnglist_header_size =
18866 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
18867 : RNGLIST_HEADER_SIZE64);
18868
18869 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18870 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18871 doesn't apply. */
18872 ULONGEST rnglist_base =
18873 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
18874
18875 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18876 ULONGEST start_offset =
18877 rnglist_base + rnglist_index * cu->header.offset_size;
18878
18879 /* Get rnglists section. */
18880 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
18881
18882 /* Read the rnglists section content. */
18883 section->read (objfile);
18884 if (section->buffer == nullptr)
18885 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18886 "[in module %s]"),
18887 objfile_name (objfile));
18888
18889 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18890 so if rnglist_base is smaller than the header size, we have a problem. */
18891 if (rnglist_base < rnglist_header_size)
18892 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18893 objfile_name (objfile));
18894
18895 /* Read the header of the rnglists contribution. */
18896 struct loclists_rnglists_header header;
18897 read_loclists_rnglists_header (&header, section,
18898 (sect_offset) (rnglist_base - rnglist_header_size));
18899
18900 /* Verify the rnglist index is valid. */
18901 if (rnglist_index >= header.offset_entry_count)
18902 error (_("DW_FORM_rnglistx index pointing outside of "
18903 ".debug_rnglists offset array [in module %s]"),
18904 objfile_name (objfile));
18905
18906 /* Validate that reading won't go beyond the end of the section. */
18907 if (start_offset + cu->header.offset_size > section->size)
18908 error (_("Reading DW_FORM_rnglistx index beyond end of"
18909 ".debug_rnglists section [in module %s]"),
18910 objfile_name (objfile));
18911
18912 const gdb_byte *info_ptr = section->buffer + start_offset;
18913
18914 if (cu->header.offset_size == 4)
18915 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
18916 else
18917 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
18918 }
18919
18920 /* Process the attributes that had to be skipped in the first round. These
18921 attributes are the ones that need str_offsets_base or addr_base attributes.
18922 They could not have been processed in the first round, because at the time
18923 the values of str_offsets_base or addr_base may not have been known. */
18924 static void
18925 read_attribute_reprocess (const struct die_reader_specs *reader,
18926 struct attribute *attr, dwarf_tag tag)
18927 {
18928 struct dwarf2_cu *cu = reader->cu;
18929 switch (attr->form)
18930 {
18931 case DW_FORM_addrx:
18932 case DW_FORM_GNU_addr_index:
18933 attr->set_address (read_addr_index (cu,
18934 attr->as_unsigned_reprocess ()));
18935 break;
18936 case DW_FORM_loclistx:
18937 {
18938 sect_offset loclists_sect_off
18939 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
18940
18941 attr->set_unsigned (to_underlying (loclists_sect_off));
18942 }
18943 break;
18944 case DW_FORM_rnglistx:
18945 {
18946 sect_offset rnglists_sect_off
18947 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
18948
18949 attr->set_unsigned (to_underlying (rnglists_sect_off));
18950 }
18951 break;
18952 case DW_FORM_strx:
18953 case DW_FORM_strx1:
18954 case DW_FORM_strx2:
18955 case DW_FORM_strx3:
18956 case DW_FORM_strx4:
18957 case DW_FORM_GNU_str_index:
18958 {
18959 unsigned int str_index = attr->as_unsigned_reprocess ();
18960 gdb_assert (!attr->canonical_string_p ());
18961 if (reader->dwo_file != NULL)
18962 attr->set_string_noncanonical (read_dwo_str_index (reader,
18963 str_index));
18964 else
18965 attr->set_string_noncanonical (read_stub_str_index (cu,
18966 str_index));
18967 break;
18968 }
18969 default:
18970 gdb_assert_not_reached ("Unexpected DWARF form.");
18971 }
18972 }
18973
18974 /* Read an attribute value described by an attribute form. */
18975
18976 static const gdb_byte *
18977 read_attribute_value (const struct die_reader_specs *reader,
18978 struct attribute *attr, unsigned form,
18979 LONGEST implicit_const, const gdb_byte *info_ptr)
18980 {
18981 struct dwarf2_cu *cu = reader->cu;
18982 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18983 struct objfile *objfile = per_objfile->objfile;
18984 bfd *abfd = reader->abfd;
18985 struct comp_unit_head *cu_header = &cu->header;
18986 unsigned int bytes_read;
18987 struct dwarf_block *blk;
18988
18989 attr->form = (enum dwarf_form) form;
18990 switch (form)
18991 {
18992 case DW_FORM_ref_addr:
18993 if (cu_header->version == 2)
18994 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
18995 &bytes_read));
18996 else
18997 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
18998 &bytes_read));
18999 info_ptr += bytes_read;
19000 break;
19001 case DW_FORM_GNU_ref_alt:
19002 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19003 &bytes_read));
19004 info_ptr += bytes_read;
19005 break;
19006 case DW_FORM_addr:
19007 {
19008 struct gdbarch *gdbarch = objfile->arch ();
19009 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19010 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19011 attr->set_address (addr);
19012 info_ptr += bytes_read;
19013 }
19014 break;
19015 case DW_FORM_block2:
19016 blk = dwarf_alloc_block (cu);
19017 blk->size = read_2_bytes (abfd, info_ptr);
19018 info_ptr += 2;
19019 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19020 info_ptr += blk->size;
19021 attr->set_block (blk);
19022 break;
19023 case DW_FORM_block4:
19024 blk = dwarf_alloc_block (cu);
19025 blk->size = read_4_bytes (abfd, info_ptr);
19026 info_ptr += 4;
19027 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19028 info_ptr += blk->size;
19029 attr->set_block (blk);
19030 break;
19031 case DW_FORM_data2:
19032 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19033 info_ptr += 2;
19034 break;
19035 case DW_FORM_data4:
19036 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19037 info_ptr += 4;
19038 break;
19039 case DW_FORM_data8:
19040 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19041 info_ptr += 8;
19042 break;
19043 case DW_FORM_data16:
19044 blk = dwarf_alloc_block (cu);
19045 blk->size = 16;
19046 blk->data = read_n_bytes (abfd, info_ptr, 16);
19047 info_ptr += 16;
19048 attr->set_block (blk);
19049 break;
19050 case DW_FORM_sec_offset:
19051 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19052 &bytes_read));
19053 info_ptr += bytes_read;
19054 break;
19055 case DW_FORM_loclistx:
19056 {
19057 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19058 &bytes_read));
19059 info_ptr += bytes_read;
19060 }
19061 break;
19062 case DW_FORM_string:
19063 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19064 &bytes_read));
19065 info_ptr += bytes_read;
19066 break;
19067 case DW_FORM_strp:
19068 if (!cu->per_cu->is_dwz)
19069 {
19070 attr->set_string_noncanonical
19071 (read_indirect_string (per_objfile,
19072 abfd, info_ptr, cu_header,
19073 &bytes_read));
19074 info_ptr += bytes_read;
19075 break;
19076 }
19077 /* FALLTHROUGH */
19078 case DW_FORM_line_strp:
19079 if (!cu->per_cu->is_dwz)
19080 {
19081 attr->set_string_noncanonical
19082 (per_objfile->read_line_string (info_ptr, cu_header,
19083 &bytes_read));
19084 info_ptr += bytes_read;
19085 break;
19086 }
19087 /* FALLTHROUGH */
19088 case DW_FORM_GNU_strp_alt:
19089 {
19090 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19091 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19092 &bytes_read);
19093
19094 attr->set_string_noncanonical
19095 (dwz->read_string (objfile, str_offset));
19096 info_ptr += bytes_read;
19097 }
19098 break;
19099 case DW_FORM_exprloc:
19100 case DW_FORM_block:
19101 blk = dwarf_alloc_block (cu);
19102 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19103 info_ptr += bytes_read;
19104 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19105 info_ptr += blk->size;
19106 attr->set_block (blk);
19107 break;
19108 case DW_FORM_block1:
19109 blk = dwarf_alloc_block (cu);
19110 blk->size = read_1_byte (abfd, info_ptr);
19111 info_ptr += 1;
19112 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19113 info_ptr += blk->size;
19114 attr->set_block (blk);
19115 break;
19116 case DW_FORM_data1:
19117 case DW_FORM_flag:
19118 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19119 info_ptr += 1;
19120 break;
19121 case DW_FORM_flag_present:
19122 attr->set_unsigned (1);
19123 break;
19124 case DW_FORM_sdata:
19125 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19126 info_ptr += bytes_read;
19127 break;
19128 case DW_FORM_rnglistx:
19129 {
19130 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19131 &bytes_read));
19132 info_ptr += bytes_read;
19133 }
19134 break;
19135 case DW_FORM_udata:
19136 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19137 info_ptr += bytes_read;
19138 break;
19139 case DW_FORM_ref1:
19140 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19141 + read_1_byte (abfd, info_ptr)));
19142 info_ptr += 1;
19143 break;
19144 case DW_FORM_ref2:
19145 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19146 + read_2_bytes (abfd, info_ptr)));
19147 info_ptr += 2;
19148 break;
19149 case DW_FORM_ref4:
19150 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19151 + read_4_bytes (abfd, info_ptr)));
19152 info_ptr += 4;
19153 break;
19154 case DW_FORM_ref8:
19155 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19156 + read_8_bytes (abfd, info_ptr)));
19157 info_ptr += 8;
19158 break;
19159 case DW_FORM_ref_sig8:
19160 attr->set_signature (read_8_bytes (abfd, info_ptr));
19161 info_ptr += 8;
19162 break;
19163 case DW_FORM_ref_udata:
19164 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19165 + read_unsigned_leb128 (abfd, info_ptr,
19166 &bytes_read)));
19167 info_ptr += bytes_read;
19168 break;
19169 case DW_FORM_indirect:
19170 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19171 info_ptr += bytes_read;
19172 if (form == DW_FORM_implicit_const)
19173 {
19174 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19175 info_ptr += bytes_read;
19176 }
19177 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19178 info_ptr);
19179 break;
19180 case DW_FORM_implicit_const:
19181 attr->set_signed (implicit_const);
19182 break;
19183 case DW_FORM_addrx:
19184 case DW_FORM_GNU_addr_index:
19185 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19186 &bytes_read));
19187 info_ptr += bytes_read;
19188 break;
19189 case DW_FORM_strx:
19190 case DW_FORM_strx1:
19191 case DW_FORM_strx2:
19192 case DW_FORM_strx3:
19193 case DW_FORM_strx4:
19194 case DW_FORM_GNU_str_index:
19195 {
19196 ULONGEST str_index;
19197 if (form == DW_FORM_strx1)
19198 {
19199 str_index = read_1_byte (abfd, info_ptr);
19200 info_ptr += 1;
19201 }
19202 else if (form == DW_FORM_strx2)
19203 {
19204 str_index = read_2_bytes (abfd, info_ptr);
19205 info_ptr += 2;
19206 }
19207 else if (form == DW_FORM_strx3)
19208 {
19209 str_index = read_3_bytes (abfd, info_ptr);
19210 info_ptr += 3;
19211 }
19212 else if (form == DW_FORM_strx4)
19213 {
19214 str_index = read_4_bytes (abfd, info_ptr);
19215 info_ptr += 4;
19216 }
19217 else
19218 {
19219 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19220 info_ptr += bytes_read;
19221 }
19222 attr->set_unsigned_reprocess (str_index);
19223 }
19224 break;
19225 default:
19226 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19227 dwarf_form_name (form),
19228 bfd_get_filename (abfd));
19229 }
19230
19231 /* Super hack. */
19232 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19233 attr->form = DW_FORM_GNU_ref_alt;
19234
19235 /* We have seen instances where the compiler tried to emit a byte
19236 size attribute of -1 which ended up being encoded as an unsigned
19237 0xffffffff. Although 0xffffffff is technically a valid size value,
19238 an object of this size seems pretty unlikely so we can relatively
19239 safely treat these cases as if the size attribute was invalid and
19240 treat them as zero by default. */
19241 if (attr->name == DW_AT_byte_size
19242 && form == DW_FORM_data4
19243 && attr->as_unsigned () >= 0xffffffff)
19244 {
19245 complaint
19246 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19247 hex_string (attr->as_unsigned ()));
19248 attr->set_unsigned (0);
19249 }
19250
19251 return info_ptr;
19252 }
19253
19254 /* Read an attribute described by an abbreviated attribute. */
19255
19256 static const gdb_byte *
19257 read_attribute (const struct die_reader_specs *reader,
19258 struct attribute *attr, const struct attr_abbrev *abbrev,
19259 const gdb_byte *info_ptr)
19260 {
19261 attr->name = abbrev->name;
19262 attr->string_is_canonical = 0;
19263 attr->requires_reprocessing = 0;
19264 return read_attribute_value (reader, attr, abbrev->form,
19265 abbrev->implicit_const, info_ptr);
19266 }
19267
19268 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19269
19270 static const char *
19271 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19272 LONGEST str_offset)
19273 {
19274 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19275 str_offset, "DW_FORM_strp");
19276 }
19277
19278 /* Return pointer to string at .debug_str offset as read from BUF.
19279 BUF is assumed to be in a compilation unit described by CU_HEADER.
19280 Return *BYTES_READ_PTR count of bytes read from BUF. */
19281
19282 static const char *
19283 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19284 const gdb_byte *buf,
19285 const struct comp_unit_head *cu_header,
19286 unsigned int *bytes_read_ptr)
19287 {
19288 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19289
19290 return read_indirect_string_at_offset (per_objfile, str_offset);
19291 }
19292
19293 /* See read.h. */
19294
19295 const char *
19296 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19297 unsigned int offset_size)
19298 {
19299 bfd *abfd = objfile->obfd;
19300 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
19301
19302 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19303 }
19304
19305 /* See read.h. */
19306
19307 const char *
19308 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19309 const struct comp_unit_head *cu_header,
19310 unsigned int *bytes_read_ptr)
19311 {
19312 bfd *abfd = objfile->obfd;
19313 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19314
19315 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19316 }
19317
19318 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19319 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19320 ADDR_SIZE is the size of addresses from the CU header. */
19321
19322 static CORE_ADDR
19323 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19324 gdb::optional<ULONGEST> addr_base, int addr_size)
19325 {
19326 struct objfile *objfile = per_objfile->objfile;
19327 bfd *abfd = objfile->obfd;
19328 const gdb_byte *info_ptr;
19329 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19330
19331 per_objfile->per_bfd->addr.read (objfile);
19332 if (per_objfile->per_bfd->addr.buffer == NULL)
19333 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19334 objfile_name (objfile));
19335 if (addr_base_or_zero + addr_index * addr_size
19336 >= per_objfile->per_bfd->addr.size)
19337 error (_("DW_FORM_addr_index pointing outside of "
19338 ".debug_addr section [in module %s]"),
19339 objfile_name (objfile));
19340 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19341 + addr_index * addr_size);
19342 if (addr_size == 4)
19343 return bfd_get_32 (abfd, info_ptr);
19344 else
19345 return bfd_get_64 (abfd, info_ptr);
19346 }
19347
19348 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19349
19350 static CORE_ADDR
19351 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19352 {
19353 return read_addr_index_1 (cu->per_objfile, addr_index,
19354 cu->addr_base, cu->header.addr_size);
19355 }
19356
19357 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19358
19359 static CORE_ADDR
19360 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19361 unsigned int *bytes_read)
19362 {
19363 bfd *abfd = cu->per_objfile->objfile->obfd;
19364 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19365
19366 return read_addr_index (cu, addr_index);
19367 }
19368
19369 /* See read.h. */
19370
19371 CORE_ADDR
19372 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19373 dwarf2_per_objfile *per_objfile,
19374 unsigned int addr_index)
19375 {
19376 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19377 gdb::optional<ULONGEST> addr_base;
19378 int addr_size;
19379
19380 /* We need addr_base and addr_size.
19381 If we don't have PER_CU->cu, we have to get it.
19382 Nasty, but the alternative is storing the needed info in PER_CU,
19383 which at this point doesn't seem justified: it's not clear how frequently
19384 it would get used and it would increase the size of every PER_CU.
19385 Entry points like dwarf2_per_cu_addr_size do a similar thing
19386 so we're not in uncharted territory here.
19387 Alas we need to be a bit more complicated as addr_base is contained
19388 in the DIE.
19389
19390 We don't need to read the entire CU(/TU).
19391 We just need the header and top level die.
19392
19393 IWBN to use the aging mechanism to let us lazily later discard the CU.
19394 For now we skip this optimization. */
19395
19396 if (cu != NULL)
19397 {
19398 addr_base = cu->addr_base;
19399 addr_size = cu->header.addr_size;
19400 }
19401 else
19402 {
19403 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19404 addr_base = reader.cu->addr_base;
19405 addr_size = reader.cu->header.addr_size;
19406 }
19407
19408 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19409 }
19410
19411 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19412 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19413 DWO file. */
19414
19415 static const char *
19416 read_str_index (struct dwarf2_cu *cu,
19417 struct dwarf2_section_info *str_section,
19418 struct dwarf2_section_info *str_offsets_section,
19419 ULONGEST str_offsets_base, ULONGEST str_index)
19420 {
19421 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19422 struct objfile *objfile = per_objfile->objfile;
19423 const char *objf_name = objfile_name (objfile);
19424 bfd *abfd = objfile->obfd;
19425 const gdb_byte *info_ptr;
19426 ULONGEST str_offset;
19427 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19428
19429 str_section->read (objfile);
19430 str_offsets_section->read (objfile);
19431 if (str_section->buffer == NULL)
19432 error (_("%s used without %s section"
19433 " in CU at offset %s [in module %s]"),
19434 form_name, str_section->get_name (),
19435 sect_offset_str (cu->header.sect_off), objf_name);
19436 if (str_offsets_section->buffer == NULL)
19437 error (_("%s used without %s section"
19438 " in CU at offset %s [in module %s]"),
19439 form_name, str_section->get_name (),
19440 sect_offset_str (cu->header.sect_off), objf_name);
19441 info_ptr = (str_offsets_section->buffer
19442 + str_offsets_base
19443 + str_index * cu->header.offset_size);
19444 if (cu->header.offset_size == 4)
19445 str_offset = bfd_get_32 (abfd, info_ptr);
19446 else
19447 str_offset = bfd_get_64 (abfd, info_ptr);
19448 if (str_offset >= str_section->size)
19449 error (_("Offset from %s pointing outside of"
19450 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19451 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19452 return (const char *) (str_section->buffer + str_offset);
19453 }
19454
19455 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19456
19457 static const char *
19458 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19459 {
19460 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19461 ? reader->cu->header.addr_size : 0;
19462 return read_str_index (reader->cu,
19463 &reader->dwo_file->sections.str,
19464 &reader->dwo_file->sections.str_offsets,
19465 str_offsets_base, str_index);
19466 }
19467
19468 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19469
19470 static const char *
19471 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19472 {
19473 struct objfile *objfile = cu->per_objfile->objfile;
19474 const char *objf_name = objfile_name (objfile);
19475 static const char form_name[] = "DW_FORM_GNU_str_index";
19476 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19477
19478 if (!cu->str_offsets_base.has_value ())
19479 error (_("%s used in Fission stub without %s"
19480 " in CU at offset 0x%lx [in module %s]"),
19481 form_name, str_offsets_attr_name,
19482 (long) cu->header.offset_size, objf_name);
19483
19484 return read_str_index (cu,
19485 &cu->per_objfile->per_bfd->str,
19486 &cu->per_objfile->per_bfd->str_offsets,
19487 *cu->str_offsets_base, str_index);
19488 }
19489
19490 /* Return the length of an LEB128 number in BUF. */
19491
19492 static int
19493 leb128_size (const gdb_byte *buf)
19494 {
19495 const gdb_byte *begin = buf;
19496 gdb_byte byte;
19497
19498 while (1)
19499 {
19500 byte = *buf++;
19501 if ((byte & 128) == 0)
19502 return buf - begin;
19503 }
19504 }
19505
19506 static enum language
19507 dwarf_lang_to_enum_language (unsigned int lang)
19508 {
19509 enum language language;
19510
19511 switch (lang)
19512 {
19513 case DW_LANG_C89:
19514 case DW_LANG_C99:
19515 case DW_LANG_C11:
19516 case DW_LANG_C:
19517 case DW_LANG_UPC:
19518 language = language_c;
19519 break;
19520 case DW_LANG_Java:
19521 case DW_LANG_C_plus_plus:
19522 case DW_LANG_C_plus_plus_11:
19523 case DW_LANG_C_plus_plus_14:
19524 language = language_cplus;
19525 break;
19526 case DW_LANG_D:
19527 language = language_d;
19528 break;
19529 case DW_LANG_Fortran77:
19530 case DW_LANG_Fortran90:
19531 case DW_LANG_Fortran95:
19532 case DW_LANG_Fortran03:
19533 case DW_LANG_Fortran08:
19534 language = language_fortran;
19535 break;
19536 case DW_LANG_Go:
19537 language = language_go;
19538 break;
19539 case DW_LANG_Mips_Assembler:
19540 language = language_asm;
19541 break;
19542 case DW_LANG_Ada83:
19543 case DW_LANG_Ada95:
19544 language = language_ada;
19545 break;
19546 case DW_LANG_Modula2:
19547 language = language_m2;
19548 break;
19549 case DW_LANG_Pascal83:
19550 language = language_pascal;
19551 break;
19552 case DW_LANG_ObjC:
19553 language = language_objc;
19554 break;
19555 case DW_LANG_Rust:
19556 case DW_LANG_Rust_old:
19557 language = language_rust;
19558 break;
19559 case DW_LANG_OpenCL:
19560 language = language_opencl;
19561 break;
19562 case DW_LANG_Cobol74:
19563 case DW_LANG_Cobol85:
19564 default:
19565 language = language_minimal;
19566 break;
19567 }
19568
19569 return language;
19570 }
19571
19572 /* Return the named attribute or NULL if not there. */
19573
19574 static struct attribute *
19575 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19576 {
19577 for (;;)
19578 {
19579 unsigned int i;
19580 struct attribute *spec = NULL;
19581
19582 for (i = 0; i < die->num_attrs; ++i)
19583 {
19584 if (die->attrs[i].name == name)
19585 return &die->attrs[i];
19586 if (die->attrs[i].name == DW_AT_specification
19587 || die->attrs[i].name == DW_AT_abstract_origin)
19588 spec = &die->attrs[i];
19589 }
19590
19591 if (!spec)
19592 break;
19593
19594 die = follow_die_ref (die, spec, &cu);
19595 }
19596
19597 return NULL;
19598 }
19599
19600 /* Return the string associated with a string-typed attribute, or NULL if it
19601 is either not found or is of an incorrect type. */
19602
19603 static const char *
19604 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19605 {
19606 struct attribute *attr;
19607 const char *str = NULL;
19608
19609 attr = dwarf2_attr (die, name, cu);
19610
19611 if (attr != NULL)
19612 {
19613 str = attr->as_string ();
19614 if (str == nullptr)
19615 complaint (_("string type expected for attribute %s for "
19616 "DIE at %s in module %s"),
19617 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19618 objfile_name (cu->per_objfile->objfile));
19619 }
19620
19621 return str;
19622 }
19623
19624 /* Return the dwo name or NULL if not present. If present, it is in either
19625 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19626 static const char *
19627 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19628 {
19629 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19630 if (dwo_name == nullptr)
19631 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19632 return dwo_name;
19633 }
19634
19635 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19636 and holds a non-zero value. This function should only be used for
19637 DW_FORM_flag or DW_FORM_flag_present attributes. */
19638
19639 static int
19640 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19641 {
19642 struct attribute *attr = dwarf2_attr (die, name, cu);
19643
19644 return attr != nullptr && attr->as_boolean ();
19645 }
19646
19647 static int
19648 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19649 {
19650 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19651 which value is non-zero. However, we have to be careful with
19652 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19653 (via dwarf2_flag_true_p) follows this attribute. So we may
19654 end up accidently finding a declaration attribute that belongs
19655 to a different DIE referenced by the specification attribute,
19656 even though the given DIE does not have a declaration attribute. */
19657 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19658 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19659 }
19660
19661 /* Return the die giving the specification for DIE, if there is
19662 one. *SPEC_CU is the CU containing DIE on input, and the CU
19663 containing the return value on output. If there is no
19664 specification, but there is an abstract origin, that is
19665 returned. */
19666
19667 static struct die_info *
19668 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19669 {
19670 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19671 *spec_cu);
19672
19673 if (spec_attr == NULL)
19674 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19675
19676 if (spec_attr == NULL)
19677 return NULL;
19678 else
19679 return follow_die_ref (die, spec_attr, spec_cu);
19680 }
19681
19682 /* A convenience function to find the proper .debug_line section for a CU. */
19683
19684 static struct dwarf2_section_info *
19685 get_debug_line_section (struct dwarf2_cu *cu)
19686 {
19687 struct dwarf2_section_info *section;
19688 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19689
19690 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19691 DWO file. */
19692 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19693 section = &cu->dwo_unit->dwo_file->sections.line;
19694 else if (cu->per_cu->is_dwz)
19695 {
19696 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19697
19698 section = &dwz->line;
19699 }
19700 else
19701 section = &per_objfile->per_bfd->line;
19702
19703 return section;
19704 }
19705
19706 /* Read the statement program header starting at OFFSET in
19707 .debug_line, or .debug_line.dwo. Return a pointer
19708 to a struct line_header, allocated using xmalloc.
19709 Returns NULL if there is a problem reading the header, e.g., if it
19710 has a version we don't understand.
19711
19712 NOTE: the strings in the include directory and file name tables of
19713 the returned object point into the dwarf line section buffer,
19714 and must not be freed. */
19715
19716 static line_header_up
19717 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19718 {
19719 struct dwarf2_section_info *section;
19720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19721
19722 section = get_debug_line_section (cu);
19723 section->read (per_objfile->objfile);
19724 if (section->buffer == NULL)
19725 {
19726 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19727 complaint (_("missing .debug_line.dwo section"));
19728 else
19729 complaint (_("missing .debug_line section"));
19730 return 0;
19731 }
19732
19733 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19734 per_objfile, section, &cu->header);
19735 }
19736
19737 /* Subroutine of dwarf_decode_lines to simplify it.
19738 Return the file name for the given file_entry.
19739 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19740 If space for the result is malloc'd, *NAME_HOLDER will be set.
19741 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19742 equivalent to CU_INFO. */
19743
19744 static const char *
19745 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
19746 const file_and_directory &cu_info,
19747 gdb::unique_xmalloc_ptr<char> *name_holder)
19748 {
19749 const char *include_name = fe.name;
19750 const char *include_name_to_compare = include_name;
19751
19752 const char *dir_name = fe.include_dir (lh);
19753
19754 gdb::unique_xmalloc_ptr<char> hold_compare;
19755 if (!IS_ABSOLUTE_PATH (include_name)
19756 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
19757 {
19758 /* Avoid creating a duplicate name for CU_INFO.
19759 We do this by comparing INCLUDE_NAME and CU_INFO.
19760 Before we do the comparison, however, we need to account
19761 for DIR_NAME and COMP_DIR.
19762 First prepend dir_name (if non-NULL). If we still don't
19763 have an absolute path prepend comp_dir (if non-NULL).
19764 However, the directory we record in the include-file's
19765 psymtab does not contain COMP_DIR (to match the
19766 corresponding symtab(s)).
19767
19768 Example:
19769
19770 bash$ cd /tmp
19771 bash$ gcc -g ./hello.c
19772 include_name = "hello.c"
19773 dir_name = "."
19774 DW_AT_comp_dir = comp_dir = "/tmp"
19775 DW_AT_name = "./hello.c"
19776
19777 */
19778
19779 if (dir_name != NULL)
19780 {
19781 name_holder->reset (concat (dir_name, SLASH_STRING,
19782 include_name, (char *) NULL));
19783 include_name = name_holder->get ();
19784 include_name_to_compare = include_name;
19785 }
19786 if (!IS_ABSOLUTE_PATH (include_name)
19787 && cu_info.get_comp_dir () != nullptr)
19788 {
19789 hold_compare.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
19790 include_name, (char *) NULL));
19791 include_name_to_compare = hold_compare.get ();
19792 }
19793 }
19794
19795 gdb::unique_xmalloc_ptr<char> copied_name;
19796 const char *cu_filename = cu_info.get_name ();
19797 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
19798 {
19799 copied_name.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
19800 cu_filename, (char *) NULL));
19801 cu_filename = copied_name.get ();
19802 }
19803
19804 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
19805 return nullptr;
19806 return include_name;
19807 }
19808
19809 /* State machine to track the state of the line number program. */
19810
19811 class lnp_state_machine
19812 {
19813 public:
19814 /* Initialize a machine state for the start of a line number
19815 program. */
19816 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
19817
19818 file_entry *current_file ()
19819 {
19820 /* lh->file_names is 0-based, but the file name numbers in the
19821 statement program are 1-based. */
19822 return m_line_header->file_name_at (m_file);
19823 }
19824
19825 /* Record the line in the state machine. END_SEQUENCE is true if
19826 we're processing the end of a sequence. */
19827 void record_line (bool end_sequence);
19828
19829 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19830 nop-out rest of the lines in this sequence. */
19831 void check_line_address (struct dwarf2_cu *cu,
19832 const gdb_byte *line_ptr,
19833 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19834
19835 void handle_set_discriminator (unsigned int discriminator)
19836 {
19837 m_discriminator = discriminator;
19838 m_line_has_non_zero_discriminator |= discriminator != 0;
19839 }
19840
19841 /* Handle DW_LNE_set_address. */
19842 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19843 {
19844 m_op_index = 0;
19845 address += baseaddr;
19846 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19847 }
19848
19849 /* Handle DW_LNS_advance_pc. */
19850 void handle_advance_pc (CORE_ADDR adjust);
19851
19852 /* Handle a special opcode. */
19853 void handle_special_opcode (unsigned char op_code);
19854
19855 /* Handle DW_LNS_advance_line. */
19856 void handle_advance_line (int line_delta)
19857 {
19858 advance_line (line_delta);
19859 }
19860
19861 /* Handle DW_LNS_set_file. */
19862 void handle_set_file (file_name_index file);
19863
19864 /* Handle DW_LNS_negate_stmt. */
19865 void handle_negate_stmt ()
19866 {
19867 m_flags ^= LEF_IS_STMT;
19868 }
19869
19870 /* Handle DW_LNS_const_add_pc. */
19871 void handle_const_add_pc ();
19872
19873 /* Handle DW_LNS_fixed_advance_pc. */
19874 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19875 {
19876 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19877 m_op_index = 0;
19878 }
19879
19880 /* Handle DW_LNS_copy. */
19881 void handle_copy ()
19882 {
19883 record_line (false);
19884 m_discriminator = 0;
19885 m_flags &= ~LEF_PROLOGUE_END;
19886 }
19887
19888 /* Handle DW_LNE_end_sequence. */
19889 void handle_end_sequence ()
19890 {
19891 m_currently_recording_lines = true;
19892 }
19893
19894 /* Handle DW_LNS_set_prologue_end. */
19895 void handle_set_prologue_end ()
19896 {
19897 m_flags |= LEF_PROLOGUE_END;
19898 }
19899
19900 private:
19901 /* Advance the line by LINE_DELTA. */
19902 void advance_line (int line_delta)
19903 {
19904 m_line += line_delta;
19905
19906 if (line_delta != 0)
19907 m_line_has_non_zero_discriminator = m_discriminator != 0;
19908 }
19909
19910 struct dwarf2_cu *m_cu;
19911
19912 gdbarch *m_gdbarch;
19913
19914 /* The line number header. */
19915 line_header *m_line_header;
19916
19917 /* These are part of the standard DWARF line number state machine,
19918 and initialized according to the DWARF spec. */
19919
19920 unsigned char m_op_index = 0;
19921 /* The line table index of the current file. */
19922 file_name_index m_file = 1;
19923 unsigned int m_line = 1;
19924
19925 /* These are initialized in the constructor. */
19926
19927 CORE_ADDR m_address;
19928 linetable_entry_flags m_flags;
19929 unsigned int m_discriminator;
19930
19931 /* Additional bits of state we need to track. */
19932
19933 /* The last file that we called dwarf2_start_subfile for.
19934 This is only used for TLLs. */
19935 unsigned int m_last_file = 0;
19936 /* The last file a line number was recorded for. */
19937 struct subfile *m_last_subfile = NULL;
19938
19939 /* The address of the last line entry. */
19940 CORE_ADDR m_last_address;
19941
19942 /* Set to true when a previous line at the same address (using
19943 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
19944 when a line entry at a new address (m_address different to
19945 m_last_address) is processed. */
19946 bool m_stmt_at_address = false;
19947
19948 /* When true, record the lines we decode. */
19949 bool m_currently_recording_lines = false;
19950
19951 /* The last line number that was recorded, used to coalesce
19952 consecutive entries for the same line. This can happen, for
19953 example, when discriminators are present. PR 17276. */
19954 unsigned int m_last_line = 0;
19955 bool m_line_has_non_zero_discriminator = false;
19956 };
19957
19958 void
19959 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19960 {
19961 CORE_ADDR addr_adj = (((m_op_index + adjust)
19962 / m_line_header->maximum_ops_per_instruction)
19963 * m_line_header->minimum_instruction_length);
19964 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19965 m_op_index = ((m_op_index + adjust)
19966 % m_line_header->maximum_ops_per_instruction);
19967 }
19968
19969 void
19970 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19971 {
19972 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19973 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19974 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19975 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19976 / m_line_header->maximum_ops_per_instruction)
19977 * m_line_header->minimum_instruction_length);
19978 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19979 m_op_index = ((m_op_index + adj_opcode_d)
19980 % m_line_header->maximum_ops_per_instruction);
19981
19982 int line_delta = m_line_header->line_base + adj_opcode_r;
19983 advance_line (line_delta);
19984 record_line (false);
19985 m_discriminator = 0;
19986 m_flags &= ~LEF_PROLOGUE_END;
19987 }
19988
19989 void
19990 lnp_state_machine::handle_set_file (file_name_index file)
19991 {
19992 m_file = file;
19993
19994 const file_entry *fe = current_file ();
19995 if (fe == NULL)
19996 dwarf2_debug_line_missing_file_complaint ();
19997 else
19998 {
19999 const char *dir = fe->include_dir (m_line_header);
20000
20001 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20002 m_line_has_non_zero_discriminator = m_discriminator != 0;
20003 dwarf2_start_subfile (m_cu, fe->name, dir);
20004 }
20005 }
20006
20007 void
20008 lnp_state_machine::handle_const_add_pc ()
20009 {
20010 CORE_ADDR adjust
20011 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20012
20013 CORE_ADDR addr_adj
20014 = (((m_op_index + adjust)
20015 / m_line_header->maximum_ops_per_instruction)
20016 * m_line_header->minimum_instruction_length);
20017
20018 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20019 m_op_index = ((m_op_index + adjust)
20020 % m_line_header->maximum_ops_per_instruction);
20021 }
20022
20023 /* Return non-zero if we should add LINE to the line number table.
20024 LINE is the line to add, LAST_LINE is the last line that was added,
20025 LAST_SUBFILE is the subfile for LAST_LINE.
20026 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20027 had a non-zero discriminator.
20028
20029 We have to be careful in the presence of discriminators.
20030 E.g., for this line:
20031
20032 for (i = 0; i < 100000; i++);
20033
20034 clang can emit four line number entries for that one line,
20035 each with a different discriminator.
20036 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20037
20038 However, we want gdb to coalesce all four entries into one.
20039 Otherwise the user could stepi into the middle of the line and
20040 gdb would get confused about whether the pc really was in the
20041 middle of the line.
20042
20043 Things are further complicated by the fact that two consecutive
20044 line number entries for the same line is a heuristic used by gcc
20045 to denote the end of the prologue. So we can't just discard duplicate
20046 entries, we have to be selective about it. The heuristic we use is
20047 that we only collapse consecutive entries for the same line if at least
20048 one of those entries has a non-zero discriminator. PR 17276.
20049
20050 Note: Addresses in the line number state machine can never go backwards
20051 within one sequence, thus this coalescing is ok. */
20052
20053 static int
20054 dwarf_record_line_p (struct dwarf2_cu *cu,
20055 unsigned int line, unsigned int last_line,
20056 int line_has_non_zero_discriminator,
20057 struct subfile *last_subfile)
20058 {
20059 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20060 return 1;
20061 if (line != last_line)
20062 return 1;
20063 /* Same line for the same file that we've seen already.
20064 As a last check, for pr 17276, only record the line if the line
20065 has never had a non-zero discriminator. */
20066 if (!line_has_non_zero_discriminator)
20067 return 1;
20068 return 0;
20069 }
20070
20071 /* Use the CU's builder to record line number LINE beginning at
20072 address ADDRESS in the line table of subfile SUBFILE. */
20073
20074 static void
20075 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20076 unsigned int line, CORE_ADDR address,
20077 linetable_entry_flags flags,
20078 struct dwarf2_cu *cu)
20079 {
20080 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20081
20082 if (dwarf_line_debug)
20083 {
20084 gdb_printf (gdb_stdlog,
20085 "Recording line %u, file %s, address %s\n",
20086 line, lbasename (subfile->name),
20087 paddress (gdbarch, address));
20088 }
20089
20090 if (cu != nullptr)
20091 cu->get_builder ()->record_line (subfile, line, addr, flags);
20092 }
20093
20094 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20095 Mark the end of a set of line number records.
20096 The arguments are the same as for dwarf_record_line_1.
20097 If SUBFILE is NULL the request is ignored. */
20098
20099 static void
20100 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20101 CORE_ADDR address, struct dwarf2_cu *cu)
20102 {
20103 if (subfile == NULL)
20104 return;
20105
20106 if (dwarf_line_debug)
20107 {
20108 gdb_printf (gdb_stdlog,
20109 "Finishing current line, file %s, address %s\n",
20110 lbasename (subfile->name),
20111 paddress (gdbarch, address));
20112 }
20113
20114 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
20115 }
20116
20117 void
20118 lnp_state_machine::record_line (bool end_sequence)
20119 {
20120 if (dwarf_line_debug)
20121 {
20122 gdb_printf (gdb_stdlog,
20123 "Processing actual line %u: file %u,"
20124 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20125 m_line, m_file,
20126 paddress (m_gdbarch, m_address),
20127 (m_flags & LEF_IS_STMT) != 0,
20128 (m_flags & LEF_PROLOGUE_END) != 0,
20129 m_discriminator,
20130 (end_sequence ? "\t(end sequence)" : ""));
20131 }
20132
20133 file_entry *fe = current_file ();
20134
20135 if (fe == NULL)
20136 dwarf2_debug_line_missing_file_complaint ();
20137 /* For now we ignore lines not starting on an instruction boundary.
20138 But not when processing end_sequence for compatibility with the
20139 previous version of the code. */
20140 else if (m_op_index == 0 || end_sequence)
20141 {
20142 /* When we switch files we insert an end maker in the first file,
20143 switch to the second file and add a new line entry. The
20144 problem is that the end marker inserted in the first file will
20145 discard any previous line entries at the same address. If the
20146 line entries in the first file are marked as is-stmt, while
20147 the new line in the second file is non-stmt, then this means
20148 the end marker will discard is-stmt lines so we can have a
20149 non-stmt line. This means that there are less addresses at
20150 which the user can insert a breakpoint.
20151
20152 To improve this we track the last address in m_last_address,
20153 and whether we have seen an is-stmt at this address. Then
20154 when switching files, if we have seen a stmt at the current
20155 address, and we are switching to create a non-stmt line, then
20156 discard the new line. */
20157 bool file_changed
20158 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20159 bool ignore_this_line
20160 = ((file_changed && !end_sequence && m_last_address == m_address
20161 && ((m_flags & LEF_IS_STMT) == 0)
20162 && m_stmt_at_address)
20163 || (!end_sequence && m_line == 0));
20164
20165 if ((file_changed && !ignore_this_line) || end_sequence)
20166 {
20167 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20168 m_currently_recording_lines ? m_cu : nullptr);
20169 }
20170
20171 if (!end_sequence && !ignore_this_line)
20172 {
20173 linetable_entry_flags lte_flags = m_flags;
20174 if (producer_is_codewarrior (m_cu))
20175 lte_flags |= LEF_IS_STMT;
20176
20177 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20178 m_line_has_non_zero_discriminator,
20179 m_last_subfile))
20180 {
20181 buildsym_compunit *builder = m_cu->get_builder ();
20182 dwarf_record_line_1 (m_gdbarch,
20183 builder->get_current_subfile (),
20184 m_line, m_address, lte_flags,
20185 m_currently_recording_lines ? m_cu : nullptr);
20186 }
20187 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20188 m_last_line = m_line;
20189 }
20190 }
20191
20192 /* Track whether we have seen any IS_STMT true at m_address in case we
20193 have multiple line table entries all at m_address. */
20194 if (m_last_address != m_address)
20195 {
20196 m_stmt_at_address = false;
20197 m_last_address = m_address;
20198 }
20199 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
20200 }
20201
20202 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20203 line_header *lh)
20204 {
20205 m_cu = cu;
20206 m_gdbarch = arch;
20207 m_line_header = lh;
20208
20209 m_currently_recording_lines = true;
20210
20211 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20212 was a line entry for it so that the backend has a chance to adjust it
20213 and also record it in case it needs it. This is currently used by MIPS
20214 code, cf. `mips_adjust_dwarf2_line'. */
20215 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20216 m_flags = 0;
20217 if (lh->default_is_stmt)
20218 m_flags |= LEF_IS_STMT;
20219 m_discriminator = 0;
20220
20221 m_last_address = m_address;
20222 m_stmt_at_address = false;
20223 }
20224
20225 void
20226 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20227 const gdb_byte *line_ptr,
20228 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20229 {
20230 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20231 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20232 located at 0x0. In this case, additionally check that if
20233 ADDRESS < UNRELOCATED_LOWPC. */
20234
20235 if ((address == 0 && address < unrelocated_lowpc)
20236 || address == (CORE_ADDR) -1)
20237 {
20238 /* This line table is for a function which has been
20239 GCd by the linker. Ignore it. PR gdb/12528 */
20240
20241 struct objfile *objfile = cu->per_objfile->objfile;
20242 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20243
20244 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20245 line_offset, objfile_name (objfile));
20246 m_currently_recording_lines = false;
20247 /* Note: m_currently_recording_lines is left as false until we see
20248 DW_LNE_end_sequence. */
20249 }
20250 }
20251
20252 /* Subroutine of dwarf_decode_lines to simplify it.
20253 Process the line number information in LH. */
20254
20255 static void
20256 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20257 CORE_ADDR lowpc)
20258 {
20259 const gdb_byte *line_ptr, *extended_end;
20260 const gdb_byte *line_end;
20261 unsigned int bytes_read, extended_len;
20262 unsigned char op_code, extended_op;
20263 CORE_ADDR baseaddr;
20264 struct objfile *objfile = cu->per_objfile->objfile;
20265 bfd *abfd = objfile->obfd;
20266 struct gdbarch *gdbarch = objfile->arch ();
20267
20268 baseaddr = objfile->text_section_offset ();
20269
20270 line_ptr = lh->statement_program_start;
20271 line_end = lh->statement_program_end;
20272
20273 /* Read the statement sequences until there's nothing left. */
20274 while (line_ptr < line_end)
20275 {
20276 /* The DWARF line number program state machine. Reset the state
20277 machine at the start of each sequence. */
20278 lnp_state_machine state_machine (cu, gdbarch, lh);
20279 bool end_sequence = false;
20280
20281 /* Start a subfile for the current file of the state
20282 machine. */
20283 const file_entry *fe = state_machine.current_file ();
20284
20285 if (fe != NULL)
20286 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20287
20288 /* Decode the table. */
20289 while (line_ptr < line_end && !end_sequence)
20290 {
20291 op_code = read_1_byte (abfd, line_ptr);
20292 line_ptr += 1;
20293
20294 if (op_code >= lh->opcode_base)
20295 {
20296 /* Special opcode. */
20297 state_machine.handle_special_opcode (op_code);
20298 }
20299 else switch (op_code)
20300 {
20301 case DW_LNS_extended_op:
20302 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20303 &bytes_read);
20304 line_ptr += bytes_read;
20305 extended_end = line_ptr + extended_len;
20306 extended_op = read_1_byte (abfd, line_ptr);
20307 line_ptr += 1;
20308 if (DW_LNE_lo_user <= extended_op
20309 && extended_op <= DW_LNE_hi_user)
20310 {
20311 /* Vendor extension, ignore. */
20312 line_ptr = extended_end;
20313 break;
20314 }
20315 switch (extended_op)
20316 {
20317 case DW_LNE_end_sequence:
20318 state_machine.handle_end_sequence ();
20319 end_sequence = true;
20320 break;
20321 case DW_LNE_set_address:
20322 {
20323 CORE_ADDR address
20324 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20325 line_ptr += bytes_read;
20326
20327 state_machine.check_line_address (cu, line_ptr,
20328 lowpc - baseaddr, address);
20329 state_machine.handle_set_address (baseaddr, address);
20330 }
20331 break;
20332 case DW_LNE_define_file:
20333 {
20334 const char *cur_file;
20335 unsigned int mod_time, length;
20336 dir_index dindex;
20337
20338 cur_file = read_direct_string (abfd, line_ptr,
20339 &bytes_read);
20340 line_ptr += bytes_read;
20341 dindex = (dir_index)
20342 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20343 line_ptr += bytes_read;
20344 mod_time =
20345 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20346 line_ptr += bytes_read;
20347 length =
20348 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20349 line_ptr += bytes_read;
20350 lh->add_file_name (cur_file, dindex, mod_time, length);
20351 }
20352 break;
20353 case DW_LNE_set_discriminator:
20354 {
20355 /* The discriminator is not interesting to the
20356 debugger; just ignore it. We still need to
20357 check its value though:
20358 if there are consecutive entries for the same
20359 (non-prologue) line we want to coalesce them.
20360 PR 17276. */
20361 unsigned int discr
20362 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20363 line_ptr += bytes_read;
20364
20365 state_machine.handle_set_discriminator (discr);
20366 }
20367 break;
20368 default:
20369 complaint (_("mangled .debug_line section"));
20370 return;
20371 }
20372 /* Make sure that we parsed the extended op correctly. If e.g.
20373 we expected a different address size than the producer used,
20374 we may have read the wrong number of bytes. */
20375 if (line_ptr != extended_end)
20376 {
20377 complaint (_("mangled .debug_line section"));
20378 return;
20379 }
20380 break;
20381 case DW_LNS_copy:
20382 state_machine.handle_copy ();
20383 break;
20384 case DW_LNS_advance_pc:
20385 {
20386 CORE_ADDR adjust
20387 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20388 line_ptr += bytes_read;
20389
20390 state_machine.handle_advance_pc (adjust);
20391 }
20392 break;
20393 case DW_LNS_advance_line:
20394 {
20395 int line_delta
20396 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20397 line_ptr += bytes_read;
20398
20399 state_machine.handle_advance_line (line_delta);
20400 }
20401 break;
20402 case DW_LNS_set_file:
20403 {
20404 file_name_index file
20405 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20406 &bytes_read);
20407 line_ptr += bytes_read;
20408
20409 state_machine.handle_set_file (file);
20410 }
20411 break;
20412 case DW_LNS_set_column:
20413 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20414 line_ptr += bytes_read;
20415 break;
20416 case DW_LNS_negate_stmt:
20417 state_machine.handle_negate_stmt ();
20418 break;
20419 case DW_LNS_set_basic_block:
20420 break;
20421 /* Add to the address register of the state machine the
20422 address increment value corresponding to special opcode
20423 255. I.e., this value is scaled by the minimum
20424 instruction length since special opcode 255 would have
20425 scaled the increment. */
20426 case DW_LNS_const_add_pc:
20427 state_machine.handle_const_add_pc ();
20428 break;
20429 case DW_LNS_fixed_advance_pc:
20430 {
20431 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20432 line_ptr += 2;
20433
20434 state_machine.handle_fixed_advance_pc (addr_adj);
20435 }
20436 break;
20437 case DW_LNS_set_prologue_end:
20438 state_machine.handle_set_prologue_end ();
20439 break;
20440 default:
20441 {
20442 /* Unknown standard opcode, ignore it. */
20443 int i;
20444
20445 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20446 {
20447 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20448 line_ptr += bytes_read;
20449 }
20450 }
20451 }
20452 }
20453
20454 if (!end_sequence)
20455 dwarf2_debug_line_missing_end_sequence_complaint ();
20456
20457 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20458 in which case we still finish recording the last line). */
20459 state_machine.record_line (true);
20460 }
20461 }
20462
20463 /* Decode the Line Number Program (LNP) for the given line_header
20464 structure and CU. The actual information extracted and the type
20465 of structures created from the LNP depends on the value of PST.
20466
20467 FND holds the CU file name and directory, if known.
20468 It is used for relative paths in the line table.
20469
20470 NOTE: It is important that psymtabs have the same file name (via
20471 strcmp) as the corresponding symtab. Since the directory is not
20472 used in the name of the symtab we don't use it in the name of the
20473 psymtabs we create. E.g. expand_line_sal requires this when
20474 finding psymtabs to expand. A good testcase for this is
20475 mb-inline.exp.
20476
20477 LOWPC is the lowest address in CU (or 0 if not known).
20478
20479 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20480 for its PC<->lines mapping information. Otherwise only the filename
20481 table is read in. */
20482
20483 static void
20484 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
20485 CORE_ADDR lowpc, int decode_mapping)
20486 {
20487 if (decode_mapping)
20488 dwarf_decode_lines_1 (lh, cu, lowpc);
20489
20490 /* Make sure a symtab is created for every file, even files
20491 which contain only variables (i.e. no code with associated
20492 line numbers). */
20493 buildsym_compunit *builder = cu->get_builder ();
20494 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20495
20496 for (auto &fe : lh->file_names ())
20497 {
20498 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20499 if (builder->get_current_subfile ()->symtab == NULL)
20500 {
20501 builder->get_current_subfile ()->symtab
20502 = allocate_symtab (cust,
20503 builder->get_current_subfile ()->name);
20504 }
20505 fe.symtab = builder->get_current_subfile ()->symtab;
20506 }
20507 }
20508
20509 /* Start a subfile for DWARF. FILENAME is the name of the file and
20510 DIRNAME the name of the source directory which contains FILENAME
20511 or NULL if not known.
20512 This routine tries to keep line numbers from identical absolute and
20513 relative file names in a common subfile.
20514
20515 Using the `list' example from the GDB testsuite, which resides in
20516 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20517 of /srcdir/list0.c yields the following debugging information for list0.c:
20518
20519 DW_AT_name: /srcdir/list0.c
20520 DW_AT_comp_dir: /compdir
20521 files.files[0].name: list0.h
20522 files.files[0].dir: /srcdir
20523 files.files[1].name: list0.c
20524 files.files[1].dir: /srcdir
20525
20526 The line number information for list0.c has to end up in a single
20527 subfile, so that `break /srcdir/list0.c:1' works as expected.
20528 start_subfile will ensure that this happens provided that we pass the
20529 concatenation of files.files[1].dir and files.files[1].name as the
20530 subfile's name. */
20531
20532 static void
20533 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20534 const char *dirname)
20535 {
20536 gdb::unique_xmalloc_ptr<char> copy;
20537
20538 /* In order not to lose the line information directory,
20539 we concatenate it to the filename when it makes sense.
20540 Note that the Dwarf3 standard says (speaking of filenames in line
20541 information): ``The directory index is ignored for file names
20542 that represent full path names''. Thus ignoring dirname in the
20543 `else' branch below isn't an issue. */
20544
20545 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20546 {
20547 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20548 filename = copy.get ();
20549 }
20550
20551 cu->get_builder ()->start_subfile (filename);
20552 }
20553
20554 static void
20555 var_decode_location (struct attribute *attr, struct symbol *sym,
20556 struct dwarf2_cu *cu)
20557 {
20558 struct objfile *objfile = cu->per_objfile->objfile;
20559 struct comp_unit_head *cu_header = &cu->header;
20560
20561 /* NOTE drow/2003-01-30: There used to be a comment and some special
20562 code here to turn a symbol with DW_AT_external and a
20563 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20564 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20565 with some versions of binutils) where shared libraries could have
20566 relocations against symbols in their debug information - the
20567 minimal symbol would have the right address, but the debug info
20568 would not. It's no longer necessary, because we will explicitly
20569 apply relocations when we read in the debug information now. */
20570
20571 /* A DW_AT_location attribute with no contents indicates that a
20572 variable has been optimized away. */
20573 if (attr->form_is_block () && attr->as_block ()->size == 0)
20574 {
20575 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20576 return;
20577 }
20578
20579 /* Handle one degenerate form of location expression specially, to
20580 preserve GDB's previous behavior when section offsets are
20581 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20582 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20583
20584 if (attr->form_is_block ())
20585 {
20586 struct dwarf_block *block = attr->as_block ();
20587
20588 if ((block->data[0] == DW_OP_addr
20589 && block->size == 1 + cu_header->addr_size)
20590 || ((block->data[0] == DW_OP_GNU_addr_index
20591 || block->data[0] == DW_OP_addrx)
20592 && (block->size
20593 == 1 + leb128_size (&block->data[1]))))
20594 {
20595 unsigned int dummy;
20596
20597 if (block->data[0] == DW_OP_addr)
20598 sym->set_value_address
20599 (cu->header.read_address (objfile->obfd, block->data + 1,
20600 &dummy));
20601 else
20602 sym->set_value_address
20603 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
20604 sym->set_aclass_index (LOC_STATIC);
20605 fixup_symbol_section (sym, objfile);
20606 sym->set_value_address
20607 (sym->value_address ()
20608 + objfile->section_offsets[sym->section_index ()]);
20609 return;
20610 }
20611 }
20612
20613 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20614 expression evaluator, and use LOC_COMPUTED only when necessary
20615 (i.e. when the value of a register or memory location is
20616 referenced, or a thread-local block, etc.). Then again, it might
20617 not be worthwhile. I'm assuming that it isn't unless performance
20618 or memory numbers show me otherwise. */
20619
20620 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20621
20622 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20623 cu->has_loclist = true;
20624 }
20625
20626 /* Given a pointer to a DWARF information entry, figure out if we need
20627 to make a symbol table entry for it, and if so, create a new entry
20628 and return a pointer to it.
20629 If TYPE is NULL, determine symbol type from the die, otherwise
20630 used the passed type.
20631 If SPACE is not NULL, use it to hold the new symbol. If it is
20632 NULL, allocate a new symbol on the objfile's obstack. */
20633
20634 static struct symbol *
20635 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20636 struct symbol *space)
20637 {
20638 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20639 struct objfile *objfile = per_objfile->objfile;
20640 struct gdbarch *gdbarch = objfile->arch ();
20641 struct symbol *sym = NULL;
20642 const char *name;
20643 struct attribute *attr = NULL;
20644 struct attribute *attr2 = NULL;
20645 CORE_ADDR baseaddr;
20646 struct pending **list_to_add = NULL;
20647
20648 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20649
20650 baseaddr = objfile->text_section_offset ();
20651
20652 name = dwarf2_name (die, cu);
20653 if (name)
20654 {
20655 int suppress_add = 0;
20656
20657 if (space)
20658 sym = space;
20659 else
20660 sym = new (&objfile->objfile_obstack) symbol;
20661 OBJSTAT (objfile, n_syms++);
20662
20663 /* Cache this symbol's name and the name's demangled form (if any). */
20664 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
20665 /* Fortran does not have mangling standard and the mangling does differ
20666 between gfortran, iFort etc. */
20667 const char *physname
20668 = (cu->per_cu->lang == language_fortran
20669 ? dwarf2_full_name (name, die, cu)
20670 : dwarf2_physname (name, die, cu));
20671 const char *linkagename = dw2_linkage_name (die, cu);
20672
20673 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
20674 sym->set_linkage_name (physname);
20675 else
20676 {
20677 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20678 sym->set_linkage_name (linkagename);
20679 }
20680
20681 /* Handle DW_AT_artificial. */
20682 attr = dwarf2_attr (die, DW_AT_artificial, cu);
20683 if (attr != nullptr)
20684 sym->artificial = attr->as_boolean ();
20685
20686 /* Default assumptions.
20687 Use the passed type or decode it from the die. */
20688 sym->set_domain (VAR_DOMAIN);
20689 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20690 if (type != NULL)
20691 sym->set_type (type);
20692 else
20693 sym->set_type (die_type (die, cu));
20694 attr = dwarf2_attr (die,
20695 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20696 cu);
20697 if (attr != nullptr)
20698 sym->set_line (attr->constant_value (0));
20699
20700 attr = dwarf2_attr (die,
20701 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20702 cu);
20703 if (attr != nullptr && attr->is_nonnegative ())
20704 {
20705 file_name_index file_index
20706 = (file_name_index) attr->as_nonnegative ();
20707 struct file_entry *fe;
20708
20709 if (cu->line_header != NULL)
20710 fe = cu->line_header->file_name_at (file_index);
20711 else
20712 fe = NULL;
20713
20714 if (fe == NULL)
20715 complaint (_("file index out of range"));
20716 else
20717 symbol_set_symtab (sym, fe->symtab);
20718 }
20719
20720 switch (die->tag)
20721 {
20722 case DW_TAG_label:
20723 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20724 if (attr != nullptr)
20725 {
20726 CORE_ADDR addr;
20727
20728 addr = attr->as_address ();
20729 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20730 sym->set_value_address (addr);
20731 sym->set_aclass_index (LOC_LABEL);
20732 }
20733 else
20734 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20735 sym->set_type (objfile_type (objfile)->builtin_core_addr);
20736 sym->set_domain (LABEL_DOMAIN);
20737 add_symbol_to_list (sym, cu->list_in_scope);
20738 break;
20739 case DW_TAG_subprogram:
20740 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20741 finish_block. */
20742 sym->set_aclass_index (LOC_BLOCK);
20743 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20744 if ((attr2 != nullptr && attr2->as_boolean ())
20745 || cu->per_cu->lang == language_ada
20746 || cu->per_cu->lang == language_fortran)
20747 {
20748 /* Subprograms marked external are stored as a global symbol.
20749 Ada and Fortran subprograms, whether marked external or
20750 not, are always stored as a global symbol, because we want
20751 to be able to access them globally. For instance, we want
20752 to be able to break on a nested subprogram without having
20753 to specify the context. */
20754 list_to_add = cu->get_builder ()->get_global_symbols ();
20755 }
20756 else
20757 {
20758 list_to_add = cu->list_in_scope;
20759 }
20760 break;
20761 case DW_TAG_inlined_subroutine:
20762 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20763 finish_block. */
20764 sym->set_aclass_index (LOC_BLOCK);
20765 sym->set_is_inlined (1);
20766 list_to_add = cu->list_in_scope;
20767 break;
20768 case DW_TAG_template_value_param:
20769 suppress_add = 1;
20770 /* Fall through. */
20771 case DW_TAG_constant:
20772 case DW_TAG_variable:
20773 case DW_TAG_member:
20774 /* Compilation with minimal debug info may result in
20775 variables with missing type entries. Change the
20776 misleading `void' type to something sensible. */
20777 if (sym->type ()->code () == TYPE_CODE_VOID)
20778 sym->set_type (objfile_type (objfile)->builtin_int);
20779
20780 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20781 /* In the case of DW_TAG_member, we should only be called for
20782 static const members. */
20783 if (die->tag == DW_TAG_member)
20784 {
20785 /* dwarf2_add_field uses die_is_declaration,
20786 so we do the same. */
20787 gdb_assert (die_is_declaration (die, cu));
20788 gdb_assert (attr);
20789 }
20790 if (attr != nullptr)
20791 {
20792 dwarf2_const_value (attr, sym, cu);
20793 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20794 if (!suppress_add)
20795 {
20796 if (attr2 != nullptr && attr2->as_boolean ())
20797 list_to_add = cu->get_builder ()->get_global_symbols ();
20798 else
20799 list_to_add = cu->list_in_scope;
20800 }
20801 break;
20802 }
20803 attr = dwarf2_attr (die, DW_AT_location, cu);
20804 if (attr != nullptr)
20805 {
20806 var_decode_location (attr, sym, cu);
20807 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20808
20809 /* Fortran explicitly imports any global symbols to the local
20810 scope by DW_TAG_common_block. */
20811 if (cu->per_cu->lang == language_fortran && die->parent
20812 && die->parent->tag == DW_TAG_common_block)
20813 attr2 = NULL;
20814
20815 if (sym->aclass () == LOC_STATIC
20816 && sym->value_address () == 0
20817 && !per_objfile->per_bfd->has_section_at_zero)
20818 {
20819 /* When a static variable is eliminated by the linker,
20820 the corresponding debug information is not stripped
20821 out, but the variable address is set to null;
20822 do not add such variables into symbol table. */
20823 }
20824 else if (attr2 != nullptr && attr2->as_boolean ())
20825 {
20826 if (sym->aclass () == LOC_STATIC
20827 && (objfile->flags & OBJF_MAINLINE) == 0
20828 && per_objfile->per_bfd->can_copy)
20829 {
20830 /* A global static variable might be subject to
20831 copy relocation. We first check for a local
20832 minsym, though, because maybe the symbol was
20833 marked hidden, in which case this would not
20834 apply. */
20835 bound_minimal_symbol found
20836 = (lookup_minimal_symbol_linkage
20837 (sym->linkage_name (), objfile));
20838 if (found.minsym != nullptr)
20839 sym->maybe_copied = 1;
20840 }
20841
20842 /* A variable with DW_AT_external is never static,
20843 but it may be block-scoped. */
20844 list_to_add
20845 = ((cu->list_in_scope
20846 == cu->get_builder ()->get_file_symbols ())
20847 ? cu->get_builder ()->get_global_symbols ()
20848 : cu->list_in_scope);
20849 }
20850 else
20851 list_to_add = cu->list_in_scope;
20852 }
20853 else
20854 {
20855 /* We do not know the address of this symbol.
20856 If it is an external symbol and we have type information
20857 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20858 The address of the variable will then be determined from
20859 the minimal symbol table whenever the variable is
20860 referenced. */
20861 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20862
20863 /* Fortran explicitly imports any global symbols to the local
20864 scope by DW_TAG_common_block. */
20865 if (cu->per_cu->lang == language_fortran && die->parent
20866 && die->parent->tag == DW_TAG_common_block)
20867 {
20868 /* SYMBOL_CLASS doesn't matter here because
20869 read_common_block is going to reset it. */
20870 if (!suppress_add)
20871 list_to_add = cu->list_in_scope;
20872 }
20873 else if (attr2 != nullptr && attr2->as_boolean ()
20874 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20875 {
20876 /* A variable with DW_AT_external is never static, but it
20877 may be block-scoped. */
20878 list_to_add
20879 = ((cu->list_in_scope
20880 == cu->get_builder ()->get_file_symbols ())
20881 ? cu->get_builder ()->get_global_symbols ()
20882 : cu->list_in_scope);
20883
20884 sym->set_aclass_index (LOC_UNRESOLVED);
20885 }
20886 else if (!die_is_declaration (die, cu))
20887 {
20888 /* Use the default LOC_OPTIMIZED_OUT class. */
20889 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
20890 if (!suppress_add)
20891 list_to_add = cu->list_in_scope;
20892 }
20893 }
20894 break;
20895 case DW_TAG_formal_parameter:
20896 {
20897 /* If we are inside a function, mark this as an argument. If
20898 not, we might be looking at an argument to an inlined function
20899 when we do not have enough information to show inlined frames;
20900 pretend it's a local variable in that case so that the user can
20901 still see it. */
20902 struct context_stack *curr
20903 = cu->get_builder ()->get_current_context_stack ();
20904 if (curr != nullptr && curr->name != nullptr)
20905 sym->set_is_argument (1);
20906 attr = dwarf2_attr (die, DW_AT_location, cu);
20907 if (attr != nullptr)
20908 {
20909 var_decode_location (attr, sym, cu);
20910 }
20911 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20912 if (attr != nullptr)
20913 {
20914 dwarf2_const_value (attr, sym, cu);
20915 }
20916
20917 list_to_add = cu->list_in_scope;
20918 }
20919 break;
20920 case DW_TAG_unspecified_parameters:
20921 /* From varargs functions; gdb doesn't seem to have any
20922 interest in this information, so just ignore it for now.
20923 (FIXME?) */
20924 break;
20925 case DW_TAG_template_type_param:
20926 suppress_add = 1;
20927 /* Fall through. */
20928 case DW_TAG_class_type:
20929 case DW_TAG_interface_type:
20930 case DW_TAG_structure_type:
20931 case DW_TAG_union_type:
20932 case DW_TAG_set_type:
20933 case DW_TAG_enumeration_type:
20934 case DW_TAG_namelist:
20935 if (die->tag == DW_TAG_namelist)
20936 {
20937 sym->set_aclass_index (LOC_STATIC);
20938 sym->set_domain (VAR_DOMAIN);
20939 }
20940 else
20941 {
20942 sym->set_aclass_index (LOC_TYPEDEF);
20943 sym->set_domain (STRUCT_DOMAIN);
20944 }
20945 {
20946 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20947 really ever be static objects: otherwise, if you try
20948 to, say, break of a class's method and you're in a file
20949 which doesn't mention that class, it won't work unless
20950 the check for all static symbols in lookup_symbol_aux
20951 saves you. See the OtherFileClass tests in
20952 gdb.c++/namespace.exp. */
20953
20954 if (!suppress_add)
20955 {
20956 buildsym_compunit *builder = cu->get_builder ();
20957 list_to_add
20958 = (cu->list_in_scope == builder->get_file_symbols ()
20959 && cu->per_cu->lang == language_cplus
20960 ? builder->get_global_symbols ()
20961 : cu->list_in_scope);
20962
20963 /* The semantics of C++ state that "struct foo {
20964 ... }" also defines a typedef for "foo". */
20965 if (cu->per_cu->lang == language_cplus
20966 || cu->per_cu->lang == language_ada
20967 || cu->per_cu->lang == language_d
20968 || cu->per_cu->lang == language_rust)
20969 {
20970 /* The symbol's name is already allocated along
20971 with this objfile, so we don't need to
20972 duplicate it for the type. */
20973 if (sym->type ()->name () == 0)
20974 sym->type ()->set_name (sym->search_name ());
20975 }
20976 }
20977 }
20978 break;
20979 case DW_TAG_typedef:
20980 sym->set_aclass_index (LOC_TYPEDEF);
20981 sym->set_domain (VAR_DOMAIN);
20982 list_to_add = cu->list_in_scope;
20983 break;
20984 case DW_TAG_array_type:
20985 case DW_TAG_base_type:
20986 case DW_TAG_subrange_type:
20987 case DW_TAG_generic_subrange:
20988 sym->set_aclass_index (LOC_TYPEDEF);
20989 sym->set_domain (VAR_DOMAIN);
20990 list_to_add = cu->list_in_scope;
20991 break;
20992 case DW_TAG_enumerator:
20993 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20994 if (attr != nullptr)
20995 {
20996 dwarf2_const_value (attr, sym, cu);
20997 }
20998 {
20999 /* NOTE: carlton/2003-11-10: See comment above in the
21000 DW_TAG_class_type, etc. block. */
21001
21002 list_to_add
21003 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21004 && cu->per_cu->lang == language_cplus
21005 ? cu->get_builder ()->get_global_symbols ()
21006 : cu->list_in_scope);
21007 }
21008 break;
21009 case DW_TAG_imported_declaration:
21010 case DW_TAG_namespace:
21011 sym->set_aclass_index (LOC_TYPEDEF);
21012 list_to_add = cu->get_builder ()->get_global_symbols ();
21013 break;
21014 case DW_TAG_module:
21015 sym->set_aclass_index (LOC_TYPEDEF);
21016 sym->set_domain (MODULE_DOMAIN);
21017 list_to_add = cu->get_builder ()->get_global_symbols ();
21018 break;
21019 case DW_TAG_common_block:
21020 sym->set_aclass_index (LOC_COMMON_BLOCK);
21021 sym->set_domain (COMMON_BLOCK_DOMAIN);
21022 add_symbol_to_list (sym, cu->list_in_scope);
21023 break;
21024 default:
21025 /* Not a tag we recognize. Hopefully we aren't processing
21026 trash data, but since we must specifically ignore things
21027 we don't recognize, there is nothing else we should do at
21028 this point. */
21029 complaint (_("unsupported tag: '%s'"),
21030 dwarf_tag_name (die->tag));
21031 break;
21032 }
21033
21034 if (suppress_add)
21035 {
21036 sym->hash_next = objfile->template_symbols;
21037 objfile->template_symbols = sym;
21038 list_to_add = NULL;
21039 }
21040
21041 if (list_to_add != NULL)
21042 add_symbol_to_list (sym, list_to_add);
21043
21044 /* For the benefit of old versions of GCC, check for anonymous
21045 namespaces based on the demangled name. */
21046 if (!cu->processing_has_namespace_info
21047 && cu->per_cu->lang == language_cplus)
21048 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21049 }
21050 return (sym);
21051 }
21052
21053 /* Given an attr with a DW_FORM_dataN value in host byte order,
21054 zero-extend it as appropriate for the symbol's type. The DWARF
21055 standard (v4) is not entirely clear about the meaning of using
21056 DW_FORM_dataN for a constant with a signed type, where the type is
21057 wider than the data. The conclusion of a discussion on the DWARF
21058 list was that this is unspecified. We choose to always zero-extend
21059 because that is the interpretation long in use by GCC. */
21060
21061 static gdb_byte *
21062 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21063 struct dwarf2_cu *cu, LONGEST *value, int bits)
21064 {
21065 struct objfile *objfile = cu->per_objfile->objfile;
21066 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21067 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21068 LONGEST l = attr->constant_value (0);
21069
21070 if (bits < sizeof (*value) * 8)
21071 {
21072 l &= ((LONGEST) 1 << bits) - 1;
21073 *value = l;
21074 }
21075 else if (bits == sizeof (*value) * 8)
21076 *value = l;
21077 else
21078 {
21079 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21080 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21081 return bytes;
21082 }
21083
21084 return NULL;
21085 }
21086
21087 /* Read a constant value from an attribute. Either set *VALUE, or if
21088 the value does not fit in *VALUE, set *BYTES - either already
21089 allocated on the objfile obstack, or newly allocated on OBSTACK,
21090 or, set *BATON, if we translated the constant to a location
21091 expression. */
21092
21093 static void
21094 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21095 const char *name, struct obstack *obstack,
21096 struct dwarf2_cu *cu,
21097 LONGEST *value, const gdb_byte **bytes,
21098 struct dwarf2_locexpr_baton **baton)
21099 {
21100 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21101 struct objfile *objfile = per_objfile->objfile;
21102 struct comp_unit_head *cu_header = &cu->header;
21103 struct dwarf_block *blk;
21104 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21105 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21106
21107 *value = 0;
21108 *bytes = NULL;
21109 *baton = NULL;
21110
21111 switch (attr->form)
21112 {
21113 case DW_FORM_addr:
21114 case DW_FORM_addrx:
21115 case DW_FORM_GNU_addr_index:
21116 {
21117 gdb_byte *data;
21118
21119 if (TYPE_LENGTH (type) != cu_header->addr_size)
21120 dwarf2_const_value_length_mismatch_complaint (name,
21121 cu_header->addr_size,
21122 TYPE_LENGTH (type));
21123 /* Symbols of this form are reasonably rare, so we just
21124 piggyback on the existing location code rather than writing
21125 a new implementation of symbol_computed_ops. */
21126 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21127 (*baton)->per_objfile = per_objfile;
21128 (*baton)->per_cu = cu->per_cu;
21129 gdb_assert ((*baton)->per_cu);
21130
21131 (*baton)->size = 2 + cu_header->addr_size;
21132 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21133 (*baton)->data = data;
21134
21135 data[0] = DW_OP_addr;
21136 store_unsigned_integer (&data[1], cu_header->addr_size,
21137 byte_order, attr->as_address ());
21138 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21139 }
21140 break;
21141 case DW_FORM_string:
21142 case DW_FORM_strp:
21143 case DW_FORM_strx:
21144 case DW_FORM_GNU_str_index:
21145 case DW_FORM_GNU_strp_alt:
21146 /* The string is already allocated on the objfile obstack, point
21147 directly to it. */
21148 *bytes = (const gdb_byte *) attr->as_string ();
21149 break;
21150 case DW_FORM_block1:
21151 case DW_FORM_block2:
21152 case DW_FORM_block4:
21153 case DW_FORM_block:
21154 case DW_FORM_exprloc:
21155 case DW_FORM_data16:
21156 blk = attr->as_block ();
21157 if (TYPE_LENGTH (type) != blk->size)
21158 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21159 TYPE_LENGTH (type));
21160 *bytes = blk->data;
21161 break;
21162
21163 /* The DW_AT_const_value attributes are supposed to carry the
21164 symbol's value "represented as it would be on the target
21165 architecture." By the time we get here, it's already been
21166 converted to host endianness, so we just need to sign- or
21167 zero-extend it as appropriate. */
21168 case DW_FORM_data1:
21169 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21170 break;
21171 case DW_FORM_data2:
21172 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21173 break;
21174 case DW_FORM_data4:
21175 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21176 break;
21177 case DW_FORM_data8:
21178 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21179 break;
21180
21181 case DW_FORM_sdata:
21182 case DW_FORM_implicit_const:
21183 *value = attr->as_signed ();
21184 break;
21185
21186 case DW_FORM_udata:
21187 *value = attr->as_unsigned ();
21188 break;
21189
21190 default:
21191 complaint (_("unsupported const value attribute form: '%s'"),
21192 dwarf_form_name (attr->form));
21193 *value = 0;
21194 break;
21195 }
21196 }
21197
21198
21199 /* Copy constant value from an attribute to a symbol. */
21200
21201 static void
21202 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21203 struct dwarf2_cu *cu)
21204 {
21205 struct objfile *objfile = cu->per_objfile->objfile;
21206 LONGEST value;
21207 const gdb_byte *bytes;
21208 struct dwarf2_locexpr_baton *baton;
21209
21210 dwarf2_const_value_attr (attr, sym->type (),
21211 sym->print_name (),
21212 &objfile->objfile_obstack, cu,
21213 &value, &bytes, &baton);
21214
21215 if (baton != NULL)
21216 {
21217 SYMBOL_LOCATION_BATON (sym) = baton;
21218 sym->set_aclass_index (dwarf2_locexpr_index);
21219 }
21220 else if (bytes != NULL)
21221 {
21222 sym->set_value_bytes (bytes);
21223 sym->set_aclass_index (LOC_CONST_BYTES);
21224 }
21225 else
21226 {
21227 sym->set_value_longest (value);
21228 sym->set_aclass_index (LOC_CONST);
21229 }
21230 }
21231
21232 /* Return the type of the die in question using its DW_AT_type attribute. */
21233
21234 static struct type *
21235 die_type (struct die_info *die, struct dwarf2_cu *cu)
21236 {
21237 struct attribute *type_attr;
21238
21239 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21240 if (!type_attr)
21241 {
21242 struct objfile *objfile = cu->per_objfile->objfile;
21243 /* A missing DW_AT_type represents a void type. */
21244 return objfile_type (objfile)->builtin_void;
21245 }
21246
21247 return lookup_die_type (die, type_attr, cu);
21248 }
21249
21250 /* True iff CU's producer generates GNAT Ada auxiliary information
21251 that allows to find parallel types through that information instead
21252 of having to do expensive parallel lookups by type name. */
21253
21254 static int
21255 need_gnat_info (struct dwarf2_cu *cu)
21256 {
21257 /* Assume that the Ada compiler was GNAT, which always produces
21258 the auxiliary information. */
21259 return (cu->per_cu->lang == language_ada);
21260 }
21261
21262 /* Return the auxiliary type of the die in question using its
21263 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21264 attribute is not present. */
21265
21266 static struct type *
21267 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21268 {
21269 struct attribute *type_attr;
21270
21271 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21272 if (!type_attr)
21273 return NULL;
21274
21275 return lookup_die_type (die, type_attr, cu);
21276 }
21277
21278 /* If DIE has a descriptive_type attribute, then set the TYPE's
21279 descriptive type accordingly. */
21280
21281 static void
21282 set_descriptive_type (struct type *type, struct die_info *die,
21283 struct dwarf2_cu *cu)
21284 {
21285 struct type *descriptive_type = die_descriptive_type (die, cu);
21286
21287 if (descriptive_type)
21288 {
21289 ALLOCATE_GNAT_AUX_TYPE (type);
21290 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21291 }
21292 }
21293
21294 /* Return the containing type of the die in question using its
21295 DW_AT_containing_type attribute. */
21296
21297 static struct type *
21298 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21299 {
21300 struct attribute *type_attr;
21301 struct objfile *objfile = cu->per_objfile->objfile;
21302
21303 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21304 if (!type_attr)
21305 error (_("Dwarf Error: Problem turning containing type into gdb type "
21306 "[in module %s]"), objfile_name (objfile));
21307
21308 return lookup_die_type (die, type_attr, cu);
21309 }
21310
21311 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21312
21313 static struct type *
21314 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21315 {
21316 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21317 struct objfile *objfile = per_objfile->objfile;
21318 char *saved;
21319
21320 std::string message
21321 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21322 objfile_name (objfile),
21323 sect_offset_str (cu->header.sect_off),
21324 sect_offset_str (die->sect_off));
21325 saved = obstack_strdup (&objfile->objfile_obstack, message);
21326
21327 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21328 }
21329
21330 /* Look up the type of DIE in CU using its type attribute ATTR.
21331 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21332 DW_AT_containing_type.
21333 If there is no type substitute an error marker. */
21334
21335 static struct type *
21336 lookup_die_type (struct die_info *die, const struct attribute *attr,
21337 struct dwarf2_cu *cu)
21338 {
21339 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21340 struct objfile *objfile = per_objfile->objfile;
21341 struct type *this_type;
21342
21343 gdb_assert (attr->name == DW_AT_type
21344 || attr->name == DW_AT_GNAT_descriptive_type
21345 || attr->name == DW_AT_containing_type);
21346
21347 /* First see if we have it cached. */
21348
21349 if (attr->form == DW_FORM_GNU_ref_alt)
21350 {
21351 struct dwarf2_per_cu_data *per_cu;
21352 sect_offset sect_off = attr->get_ref_die_offset ();
21353
21354 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21355 per_objfile->per_bfd);
21356 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21357 }
21358 else if (attr->form_is_ref ())
21359 {
21360 sect_offset sect_off = attr->get_ref_die_offset ();
21361
21362 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21363 }
21364 else if (attr->form == DW_FORM_ref_sig8)
21365 {
21366 ULONGEST signature = attr->as_signature ();
21367
21368 return get_signatured_type (die, signature, cu);
21369 }
21370 else
21371 {
21372 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21373 " at %s [in module %s]"),
21374 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21375 objfile_name (objfile));
21376 return build_error_marker_type (cu, die);
21377 }
21378
21379 /* If not cached we need to read it in. */
21380
21381 if (this_type == NULL)
21382 {
21383 struct die_info *type_die = NULL;
21384 struct dwarf2_cu *type_cu = cu;
21385
21386 if (attr->form_is_ref ())
21387 type_die = follow_die_ref (die, attr, &type_cu);
21388 if (type_die == NULL)
21389 return build_error_marker_type (cu, die);
21390 /* If we find the type now, it's probably because the type came
21391 from an inter-CU reference and the type's CU got expanded before
21392 ours. */
21393 this_type = read_type_die (type_die, type_cu);
21394 }
21395
21396 /* If we still don't have a type use an error marker. */
21397
21398 if (this_type == NULL)
21399 return build_error_marker_type (cu, die);
21400
21401 return this_type;
21402 }
21403
21404 /* Return the type in DIE, CU.
21405 Returns NULL for invalid types.
21406
21407 This first does a lookup in die_type_hash,
21408 and only reads the die in if necessary.
21409
21410 NOTE: This can be called when reading in partial or full symbols. */
21411
21412 static struct type *
21413 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21414 {
21415 struct type *this_type;
21416
21417 this_type = get_die_type (die, cu);
21418 if (this_type)
21419 return this_type;
21420
21421 return read_type_die_1 (die, cu);
21422 }
21423
21424 /* Read the type in DIE, CU.
21425 Returns NULL for invalid types. */
21426
21427 static struct type *
21428 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21429 {
21430 struct type *this_type = NULL;
21431
21432 switch (die->tag)
21433 {
21434 case DW_TAG_class_type:
21435 case DW_TAG_interface_type:
21436 case DW_TAG_structure_type:
21437 case DW_TAG_union_type:
21438 this_type = read_structure_type (die, cu);
21439 break;
21440 case DW_TAG_enumeration_type:
21441 this_type = read_enumeration_type (die, cu);
21442 break;
21443 case DW_TAG_subprogram:
21444 case DW_TAG_subroutine_type:
21445 case DW_TAG_inlined_subroutine:
21446 this_type = read_subroutine_type (die, cu);
21447 break;
21448 case DW_TAG_array_type:
21449 this_type = read_array_type (die, cu);
21450 break;
21451 case DW_TAG_set_type:
21452 this_type = read_set_type (die, cu);
21453 break;
21454 case DW_TAG_pointer_type:
21455 this_type = read_tag_pointer_type (die, cu);
21456 break;
21457 case DW_TAG_ptr_to_member_type:
21458 this_type = read_tag_ptr_to_member_type (die, cu);
21459 break;
21460 case DW_TAG_reference_type:
21461 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21462 break;
21463 case DW_TAG_rvalue_reference_type:
21464 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21465 break;
21466 case DW_TAG_const_type:
21467 this_type = read_tag_const_type (die, cu);
21468 break;
21469 case DW_TAG_volatile_type:
21470 this_type = read_tag_volatile_type (die, cu);
21471 break;
21472 case DW_TAG_restrict_type:
21473 this_type = read_tag_restrict_type (die, cu);
21474 break;
21475 case DW_TAG_string_type:
21476 this_type = read_tag_string_type (die, cu);
21477 break;
21478 case DW_TAG_typedef:
21479 this_type = read_typedef (die, cu);
21480 break;
21481 case DW_TAG_generic_subrange:
21482 case DW_TAG_subrange_type:
21483 this_type = read_subrange_type (die, cu);
21484 break;
21485 case DW_TAG_base_type:
21486 this_type = read_base_type (die, cu);
21487 break;
21488 case DW_TAG_unspecified_type:
21489 this_type = read_unspecified_type (die, cu);
21490 break;
21491 case DW_TAG_namespace:
21492 this_type = read_namespace_type (die, cu);
21493 break;
21494 case DW_TAG_module:
21495 this_type = read_module_type (die, cu);
21496 break;
21497 case DW_TAG_atomic_type:
21498 this_type = read_tag_atomic_type (die, cu);
21499 break;
21500 default:
21501 complaint (_("unexpected tag in read_type_die: '%s'"),
21502 dwarf_tag_name (die->tag));
21503 break;
21504 }
21505
21506 return this_type;
21507 }
21508
21509 /* See if we can figure out if the class lives in a namespace. We do
21510 this by looking for a member function; its demangled name will
21511 contain namespace info, if there is any.
21512 Return the computed name or NULL.
21513 Space for the result is allocated on the objfile's obstack.
21514 This is the full-die version of guess_partial_die_structure_name.
21515 In this case we know DIE has no useful parent. */
21516
21517 static const char *
21518 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21519 {
21520 struct die_info *spec_die;
21521 struct dwarf2_cu *spec_cu;
21522 struct die_info *child;
21523 struct objfile *objfile = cu->per_objfile->objfile;
21524
21525 spec_cu = cu;
21526 spec_die = die_specification (die, &spec_cu);
21527 if (spec_die != NULL)
21528 {
21529 die = spec_die;
21530 cu = spec_cu;
21531 }
21532
21533 for (child = die->child;
21534 child != NULL;
21535 child = child->sibling)
21536 {
21537 if (child->tag == DW_TAG_subprogram)
21538 {
21539 const char *linkage_name = dw2_linkage_name (child, cu);
21540
21541 if (linkage_name != NULL)
21542 {
21543 gdb::unique_xmalloc_ptr<char> actual_name
21544 (cu->language_defn->class_name_from_physname (linkage_name));
21545 const char *name = NULL;
21546
21547 if (actual_name != NULL)
21548 {
21549 const char *die_name = dwarf2_name (die, cu);
21550
21551 if (die_name != NULL
21552 && strcmp (die_name, actual_name.get ()) != 0)
21553 {
21554 /* Strip off the class name from the full name.
21555 We want the prefix. */
21556 int die_name_len = strlen (die_name);
21557 int actual_name_len = strlen (actual_name.get ());
21558 const char *ptr = actual_name.get ();
21559
21560 /* Test for '::' as a sanity check. */
21561 if (actual_name_len > die_name_len + 2
21562 && ptr[actual_name_len - die_name_len - 1] == ':')
21563 name = obstack_strndup (
21564 &objfile->per_bfd->storage_obstack,
21565 ptr, actual_name_len - die_name_len - 2);
21566 }
21567 }
21568 return name;
21569 }
21570 }
21571 }
21572
21573 return NULL;
21574 }
21575
21576 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21577 prefix part in such case. See
21578 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21579
21580 static const char *
21581 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21582 {
21583 struct attribute *attr;
21584 const char *base;
21585
21586 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21587 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21588 return NULL;
21589
21590 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21591 return NULL;
21592
21593 attr = dw2_linkage_name_attr (die, cu);
21594 const char *attr_name = attr->as_string ();
21595 if (attr == NULL || attr_name == NULL)
21596 return NULL;
21597
21598 /* dwarf2_name had to be already called. */
21599 gdb_assert (attr->canonical_string_p ());
21600
21601 /* Strip the base name, keep any leading namespaces/classes. */
21602 base = strrchr (attr_name, ':');
21603 if (base == NULL || base == attr_name || base[-1] != ':')
21604 return "";
21605
21606 struct objfile *objfile = cu->per_objfile->objfile;
21607 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21608 attr_name,
21609 &base[-1] - attr_name);
21610 }
21611
21612 /* Return the name of the namespace/class that DIE is defined within,
21613 or "" if we can't tell. The caller should not xfree the result.
21614
21615 For example, if we're within the method foo() in the following
21616 code:
21617
21618 namespace N {
21619 class C {
21620 void foo () {
21621 }
21622 };
21623 }
21624
21625 then determine_prefix on foo's die will return "N::C". */
21626
21627 static const char *
21628 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21629 {
21630 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21631 struct die_info *parent, *spec_die;
21632 struct dwarf2_cu *spec_cu;
21633 struct type *parent_type;
21634 const char *retval;
21635
21636 if (cu->per_cu->lang != language_cplus
21637 && cu->per_cu->lang != language_fortran
21638 && cu->per_cu->lang != language_d
21639 && cu->per_cu->lang != language_rust)
21640 return "";
21641
21642 retval = anonymous_struct_prefix (die, cu);
21643 if (retval)
21644 return retval;
21645
21646 /* We have to be careful in the presence of DW_AT_specification.
21647 For example, with GCC 3.4, given the code
21648
21649 namespace N {
21650 void foo() {
21651 // Definition of N::foo.
21652 }
21653 }
21654
21655 then we'll have a tree of DIEs like this:
21656
21657 1: DW_TAG_compile_unit
21658 2: DW_TAG_namespace // N
21659 3: DW_TAG_subprogram // declaration of N::foo
21660 4: DW_TAG_subprogram // definition of N::foo
21661 DW_AT_specification // refers to die #3
21662
21663 Thus, when processing die #4, we have to pretend that we're in
21664 the context of its DW_AT_specification, namely the contex of die
21665 #3. */
21666 spec_cu = cu;
21667 spec_die = die_specification (die, &spec_cu);
21668 if (spec_die == NULL)
21669 parent = die->parent;
21670 else
21671 {
21672 parent = spec_die->parent;
21673 cu = spec_cu;
21674 }
21675
21676 if (parent == NULL)
21677 return "";
21678 else if (parent->building_fullname)
21679 {
21680 const char *name;
21681 const char *parent_name;
21682
21683 /* It has been seen on RealView 2.2 built binaries,
21684 DW_TAG_template_type_param types actually _defined_ as
21685 children of the parent class:
21686
21687 enum E {};
21688 template class <class Enum> Class{};
21689 Class<enum E> class_e;
21690
21691 1: DW_TAG_class_type (Class)
21692 2: DW_TAG_enumeration_type (E)
21693 3: DW_TAG_enumerator (enum1:0)
21694 3: DW_TAG_enumerator (enum2:1)
21695 ...
21696 2: DW_TAG_template_type_param
21697 DW_AT_type DW_FORM_ref_udata (E)
21698
21699 Besides being broken debug info, it can put GDB into an
21700 infinite loop. Consider:
21701
21702 When we're building the full name for Class<E>, we'll start
21703 at Class, and go look over its template type parameters,
21704 finding E. We'll then try to build the full name of E, and
21705 reach here. We're now trying to build the full name of E,
21706 and look over the parent DIE for containing scope. In the
21707 broken case, if we followed the parent DIE of E, we'd again
21708 find Class, and once again go look at its template type
21709 arguments, etc., etc. Simply don't consider such parent die
21710 as source-level parent of this die (it can't be, the language
21711 doesn't allow it), and break the loop here. */
21712 name = dwarf2_name (die, cu);
21713 parent_name = dwarf2_name (parent, cu);
21714 complaint (_("template param type '%s' defined within parent '%s'"),
21715 name ? name : "<unknown>",
21716 parent_name ? parent_name : "<unknown>");
21717 return "";
21718 }
21719 else
21720 switch (parent->tag)
21721 {
21722 case DW_TAG_namespace:
21723 parent_type = read_type_die (parent, cu);
21724 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21725 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21726 Work around this problem here. */
21727 if (cu->per_cu->lang == language_cplus
21728 && strcmp (parent_type->name (), "::") == 0)
21729 return "";
21730 /* We give a name to even anonymous namespaces. */
21731 return parent_type->name ();
21732 case DW_TAG_class_type:
21733 case DW_TAG_interface_type:
21734 case DW_TAG_structure_type:
21735 case DW_TAG_union_type:
21736 case DW_TAG_module:
21737 parent_type = read_type_die (parent, cu);
21738 if (parent_type->name () != NULL)
21739 return parent_type->name ();
21740 else
21741 /* An anonymous structure is only allowed non-static data
21742 members; no typedefs, no member functions, et cetera.
21743 So it does not need a prefix. */
21744 return "";
21745 case DW_TAG_compile_unit:
21746 case DW_TAG_partial_unit:
21747 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21748 if (cu->per_cu->lang == language_cplus
21749 && !per_objfile->per_bfd->types.empty ()
21750 && die->child != NULL
21751 && (die->tag == DW_TAG_class_type
21752 || die->tag == DW_TAG_structure_type
21753 || die->tag == DW_TAG_union_type))
21754 {
21755 const char *name = guess_full_die_structure_name (die, cu);
21756 if (name != NULL)
21757 return name;
21758 }
21759 return "";
21760 case DW_TAG_subprogram:
21761 /* Nested subroutines in Fortran get a prefix with the name
21762 of the parent's subroutine. */
21763 if (cu->per_cu->lang == language_fortran)
21764 {
21765 if ((die->tag == DW_TAG_subprogram)
21766 && (dwarf2_name (parent, cu) != NULL))
21767 return dwarf2_name (parent, cu);
21768 }
21769 return "";
21770 case DW_TAG_enumeration_type:
21771 parent_type = read_type_die (parent, cu);
21772 if (parent_type->is_declared_class ())
21773 {
21774 if (parent_type->name () != NULL)
21775 return parent_type->name ();
21776 return "";
21777 }
21778 /* Fall through. */
21779 default:
21780 return determine_prefix (parent, cu);
21781 }
21782 }
21783
21784 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21785 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21786 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21787 an obconcat, otherwise allocate storage for the result. The CU argument is
21788 used to determine the language and hence, the appropriate separator. */
21789
21790 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21791
21792 static char *
21793 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21794 int physname, struct dwarf2_cu *cu)
21795 {
21796 const char *lead = "";
21797 const char *sep;
21798
21799 if (suffix == NULL || suffix[0] == '\0'
21800 || prefix == NULL || prefix[0] == '\0')
21801 sep = "";
21802 else if (cu->per_cu->lang == language_d)
21803 {
21804 /* For D, the 'main' function could be defined in any module, but it
21805 should never be prefixed. */
21806 if (strcmp (suffix, "D main") == 0)
21807 {
21808 prefix = "";
21809 sep = "";
21810 }
21811 else
21812 sep = ".";
21813 }
21814 else if (cu->per_cu->lang == language_fortran && physname)
21815 {
21816 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21817 DW_AT_MIPS_linkage_name is preferred and used instead. */
21818
21819 lead = "__";
21820 sep = "_MOD_";
21821 }
21822 else
21823 sep = "::";
21824
21825 if (prefix == NULL)
21826 prefix = "";
21827 if (suffix == NULL)
21828 suffix = "";
21829
21830 if (obs == NULL)
21831 {
21832 char *retval
21833 = ((char *)
21834 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21835
21836 strcpy (retval, lead);
21837 strcat (retval, prefix);
21838 strcat (retval, sep);
21839 strcat (retval, suffix);
21840 return retval;
21841 }
21842 else
21843 {
21844 /* We have an obstack. */
21845 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21846 }
21847 }
21848
21849 /* Get name of a die, return NULL if not found. */
21850
21851 static const char *
21852 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21853 struct objfile *objfile)
21854 {
21855 if (name && cu->per_cu->lang == language_cplus)
21856 {
21857 gdb::unique_xmalloc_ptr<char> canon_name
21858 = cp_canonicalize_string (name);
21859
21860 if (canon_name != nullptr)
21861 name = objfile->intern (canon_name.get ());
21862 }
21863
21864 return name;
21865 }
21866
21867 /* Get name of a die, return NULL if not found.
21868 Anonymous namespaces are converted to their magic string. */
21869
21870 static const char *
21871 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21872 {
21873 struct attribute *attr;
21874 struct objfile *objfile = cu->per_objfile->objfile;
21875
21876 attr = dwarf2_attr (die, DW_AT_name, cu);
21877 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
21878 if (attr_name == nullptr
21879 && die->tag != DW_TAG_namespace
21880 && die->tag != DW_TAG_class_type
21881 && die->tag != DW_TAG_interface_type
21882 && die->tag != DW_TAG_structure_type
21883 && die->tag != DW_TAG_namelist
21884 && die->tag != DW_TAG_union_type)
21885 return NULL;
21886
21887 switch (die->tag)
21888 {
21889 case DW_TAG_compile_unit:
21890 case DW_TAG_partial_unit:
21891 /* Compilation units have a DW_AT_name that is a filename, not
21892 a source language identifier. */
21893 case DW_TAG_enumeration_type:
21894 case DW_TAG_enumerator:
21895 /* These tags always have simple identifiers already; no need
21896 to canonicalize them. */
21897 return attr_name;
21898
21899 case DW_TAG_namespace:
21900 if (attr_name != nullptr)
21901 return attr_name;
21902 return CP_ANONYMOUS_NAMESPACE_STR;
21903
21904 case DW_TAG_class_type:
21905 case DW_TAG_interface_type:
21906 case DW_TAG_structure_type:
21907 case DW_TAG_union_type:
21908 case DW_TAG_namelist:
21909 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21910 structures or unions. These were of the form "._%d" in GCC 4.1,
21911 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21912 and GCC 4.4. We work around this problem by ignoring these. */
21913 if (attr_name != nullptr
21914 && (startswith (attr_name, "._")
21915 || startswith (attr_name, "<anonymous")))
21916 return NULL;
21917
21918 /* GCC might emit a nameless typedef that has a linkage name. See
21919 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21920 if (!attr || attr_name == NULL)
21921 {
21922 attr = dw2_linkage_name_attr (die, cu);
21923 attr_name = attr == nullptr ? nullptr : attr->as_string ();
21924 if (attr == NULL || attr_name == NULL)
21925 return NULL;
21926
21927 /* Avoid demangling attr_name the second time on a second
21928 call for the same DIE. */
21929 if (!attr->canonical_string_p ())
21930 {
21931 gdb::unique_xmalloc_ptr<char> demangled
21932 (gdb_demangle (attr_name, DMGL_TYPES));
21933 if (demangled == nullptr)
21934 return nullptr;
21935
21936 attr->set_string_canonical (objfile->intern (demangled.get ()));
21937 attr_name = attr->as_string ();
21938 }
21939
21940 /* Strip any leading namespaces/classes, keep only the
21941 base name. DW_AT_name for named DIEs does not
21942 contain the prefixes. */
21943 const char *base = strrchr (attr_name, ':');
21944 if (base && base > attr_name && base[-1] == ':')
21945 return &base[1];
21946 else
21947 return attr_name;
21948 }
21949 break;
21950
21951 default:
21952 break;
21953 }
21954
21955 if (!attr->canonical_string_p ())
21956 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
21957 objfile));
21958 return attr->as_string ();
21959 }
21960
21961 /* Return the die that this die in an extension of, or NULL if there
21962 is none. *EXT_CU is the CU containing DIE on input, and the CU
21963 containing the return value on output. */
21964
21965 static struct die_info *
21966 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21967 {
21968 struct attribute *attr;
21969
21970 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21971 if (attr == NULL)
21972 return NULL;
21973
21974 return follow_die_ref (die, attr, ext_cu);
21975 }
21976
21977 static void
21978 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21979 {
21980 unsigned int i;
21981
21982 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
21983 indent, "",
21984 dwarf_tag_name (die->tag), die->abbrev,
21985 sect_offset_str (die->sect_off));
21986
21987 if (die->parent != NULL)
21988 gdb_printf (f, "%*s parent at offset: %s\n",
21989 indent, "",
21990 sect_offset_str (die->parent->sect_off));
21991
21992 gdb_printf (f, "%*s has children: %s\n",
21993 indent, "",
21994 dwarf_bool_name (die->child != NULL));
21995
21996 gdb_printf (f, "%*s attributes:\n", indent, "");
21997
21998 for (i = 0; i < die->num_attrs; ++i)
21999 {
22000 gdb_printf (f, "%*s %s (%s) ",
22001 indent, "",
22002 dwarf_attr_name (die->attrs[i].name),
22003 dwarf_form_name (die->attrs[i].form));
22004
22005 switch (die->attrs[i].form)
22006 {
22007 case DW_FORM_addr:
22008 case DW_FORM_addrx:
22009 case DW_FORM_GNU_addr_index:
22010 gdb_printf (f, "address: ");
22011 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
22012 break;
22013 case DW_FORM_block2:
22014 case DW_FORM_block4:
22015 case DW_FORM_block:
22016 case DW_FORM_block1:
22017 gdb_printf (f, "block: size %s",
22018 pulongest (die->attrs[i].as_block ()->size));
22019 break;
22020 case DW_FORM_exprloc:
22021 gdb_printf (f, "expression: size %s",
22022 pulongest (die->attrs[i].as_block ()->size));
22023 break;
22024 case DW_FORM_data16:
22025 gdb_printf (f, "constant of 16 bytes");
22026 break;
22027 case DW_FORM_ref_addr:
22028 gdb_printf (f, "ref address: ");
22029 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
22030 break;
22031 case DW_FORM_GNU_ref_alt:
22032 gdb_printf (f, "alt ref address: ");
22033 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
22034 break;
22035 case DW_FORM_ref1:
22036 case DW_FORM_ref2:
22037 case DW_FORM_ref4:
22038 case DW_FORM_ref8:
22039 case DW_FORM_ref_udata:
22040 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
22041 (long) (die->attrs[i].as_unsigned ()));
22042 break;
22043 case DW_FORM_data1:
22044 case DW_FORM_data2:
22045 case DW_FORM_data4:
22046 case DW_FORM_data8:
22047 case DW_FORM_udata:
22048 gdb_printf (f, "constant: %s",
22049 pulongest (die->attrs[i].as_unsigned ()));
22050 break;
22051 case DW_FORM_sec_offset:
22052 gdb_printf (f, "section offset: %s",
22053 pulongest (die->attrs[i].as_unsigned ()));
22054 break;
22055 case DW_FORM_ref_sig8:
22056 gdb_printf (f, "signature: %s",
22057 hex_string (die->attrs[i].as_signature ()));
22058 break;
22059 case DW_FORM_string:
22060 case DW_FORM_strp:
22061 case DW_FORM_line_strp:
22062 case DW_FORM_strx:
22063 case DW_FORM_GNU_str_index:
22064 case DW_FORM_GNU_strp_alt:
22065 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
22066 die->attrs[i].as_string ()
22067 ? die->attrs[i].as_string () : "",
22068 die->attrs[i].canonical_string_p () ? "is" : "not");
22069 break;
22070 case DW_FORM_flag:
22071 if (die->attrs[i].as_boolean ())
22072 gdb_printf (f, "flag: TRUE");
22073 else
22074 gdb_printf (f, "flag: FALSE");
22075 break;
22076 case DW_FORM_flag_present:
22077 gdb_printf (f, "flag: TRUE");
22078 break;
22079 case DW_FORM_indirect:
22080 /* The reader will have reduced the indirect form to
22081 the "base form" so this form should not occur. */
22082 gdb_printf (f,
22083 "unexpected attribute form: DW_FORM_indirect");
22084 break;
22085 case DW_FORM_sdata:
22086 case DW_FORM_implicit_const:
22087 gdb_printf (f, "constant: %s",
22088 plongest (die->attrs[i].as_signed ()));
22089 break;
22090 default:
22091 gdb_printf (f, "unsupported attribute form: %d.",
22092 die->attrs[i].form);
22093 break;
22094 }
22095 gdb_printf (f, "\n");
22096 }
22097 }
22098
22099 static void
22100 dump_die_for_error (struct die_info *die)
22101 {
22102 dump_die_shallow (gdb_stderr, 0, die);
22103 }
22104
22105 static void
22106 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22107 {
22108 int indent = level * 4;
22109
22110 gdb_assert (die != NULL);
22111
22112 if (level >= max_level)
22113 return;
22114
22115 dump_die_shallow (f, indent, die);
22116
22117 if (die->child != NULL)
22118 {
22119 gdb_printf (f, "%*s Children:", indent, "");
22120 if (level + 1 < max_level)
22121 {
22122 gdb_printf (f, "\n");
22123 dump_die_1 (f, level + 1, max_level, die->child);
22124 }
22125 else
22126 {
22127 gdb_printf (f,
22128 " [not printed, max nesting level reached]\n");
22129 }
22130 }
22131
22132 if (die->sibling != NULL && level > 0)
22133 {
22134 dump_die_1 (f, level, max_level, die->sibling);
22135 }
22136 }
22137
22138 /* This is called from the pdie macro in gdbinit.in.
22139 It's not static so gcc will keep a copy callable from gdb. */
22140
22141 void
22142 dump_die (struct die_info *die, int max_level)
22143 {
22144 dump_die_1 (gdb_stdlog, 0, max_level, die);
22145 }
22146
22147 static void
22148 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22149 {
22150 void **slot;
22151
22152 slot = htab_find_slot_with_hash (cu->die_hash, die,
22153 to_underlying (die->sect_off),
22154 INSERT);
22155
22156 *slot = die;
22157 }
22158
22159 /* Follow reference or signature attribute ATTR of SRC_DIE.
22160 On entry *REF_CU is the CU of SRC_DIE.
22161 On exit *REF_CU is the CU of the result. */
22162
22163 static struct die_info *
22164 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22165 struct dwarf2_cu **ref_cu)
22166 {
22167 struct die_info *die;
22168
22169 if (attr->form_is_ref ())
22170 die = follow_die_ref (src_die, attr, ref_cu);
22171 else if (attr->form == DW_FORM_ref_sig8)
22172 die = follow_die_sig (src_die, attr, ref_cu);
22173 else
22174 {
22175 dump_die_for_error (src_die);
22176 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22177 objfile_name ((*ref_cu)->per_objfile->objfile));
22178 }
22179
22180 return die;
22181 }
22182
22183 /* Follow reference OFFSET.
22184 On entry *REF_CU is the CU of the source die referencing OFFSET.
22185 On exit *REF_CU is the CU of the result.
22186 Returns NULL if OFFSET is invalid. */
22187
22188 static struct die_info *
22189 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22190 struct dwarf2_cu **ref_cu)
22191 {
22192 struct die_info temp_die;
22193 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22194 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22195
22196 gdb_assert (cu->per_cu != NULL);
22197
22198 target_cu = cu;
22199
22200 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22201 "source CU contains target offset: %d",
22202 sect_offset_str (cu->per_cu->sect_off),
22203 sect_offset_str (sect_off),
22204 cu->header.offset_in_cu_p (sect_off));
22205
22206 if (cu->per_cu->is_debug_types)
22207 {
22208 /* .debug_types CUs cannot reference anything outside their CU.
22209 If they need to, they have to reference a signatured type via
22210 DW_FORM_ref_sig8. */
22211 if (!cu->header.offset_in_cu_p (sect_off))
22212 return NULL;
22213 }
22214 else if (offset_in_dwz != cu->per_cu->is_dwz
22215 || !cu->header.offset_in_cu_p (sect_off))
22216 {
22217 struct dwarf2_per_cu_data *per_cu;
22218
22219 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22220 per_objfile->per_bfd);
22221
22222 dwarf_read_debug_printf_v ("target CU offset: %s, "
22223 "target CU DIEs loaded: %d",
22224 sect_offset_str (per_cu->sect_off),
22225 per_objfile->get_cu (per_cu) != nullptr);
22226
22227 /* If necessary, add it to the queue and load its DIEs.
22228
22229 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22230 it doesn't mean they are currently loaded. Since we require them
22231 to be loaded, we must check for ourselves. */
22232 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
22233 || per_objfile->get_cu (per_cu) == nullptr)
22234 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
22235 false, cu->per_cu->lang);
22236
22237 target_cu = per_objfile->get_cu (per_cu);
22238 gdb_assert (target_cu != nullptr);
22239 }
22240 else if (cu->dies == NULL)
22241 {
22242 /* We're loading full DIEs during partial symbol reading. */
22243 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
22244 language_minimal);
22245 }
22246
22247 *ref_cu = target_cu;
22248 temp_die.sect_off = sect_off;
22249
22250 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22251 &temp_die,
22252 to_underlying (sect_off));
22253 }
22254
22255 /* Follow reference attribute ATTR of SRC_DIE.
22256 On entry *REF_CU is the CU of SRC_DIE.
22257 On exit *REF_CU is the CU of the result. */
22258
22259 static struct die_info *
22260 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22261 struct dwarf2_cu **ref_cu)
22262 {
22263 sect_offset sect_off = attr->get_ref_die_offset ();
22264 struct dwarf2_cu *cu = *ref_cu;
22265 struct die_info *die;
22266
22267 die = follow_die_offset (sect_off,
22268 (attr->form == DW_FORM_GNU_ref_alt
22269 || cu->per_cu->is_dwz),
22270 ref_cu);
22271 if (!die)
22272 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22273 "at %s [in module %s]"),
22274 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22275 objfile_name (cu->per_objfile->objfile));
22276
22277 return die;
22278 }
22279
22280 /* See read.h. */
22281
22282 struct dwarf2_locexpr_baton
22283 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22284 dwarf2_per_cu_data *per_cu,
22285 dwarf2_per_objfile *per_objfile,
22286 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22287 bool resolve_abstract_p)
22288 {
22289 struct die_info *die;
22290 struct attribute *attr;
22291 struct dwarf2_locexpr_baton retval;
22292 struct objfile *objfile = per_objfile->objfile;
22293
22294 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22295 if (cu == nullptr)
22296 cu = load_cu (per_cu, per_objfile, false);
22297
22298 if (cu == nullptr)
22299 {
22300 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22301 Instead just throw an error, not much else we can do. */
22302 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22303 sect_offset_str (sect_off), objfile_name (objfile));
22304 }
22305
22306 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22307 if (!die)
22308 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22309 sect_offset_str (sect_off), objfile_name (objfile));
22310
22311 attr = dwarf2_attr (die, DW_AT_location, cu);
22312 if (!attr && resolve_abstract_p
22313 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22314 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22315 {
22316 CORE_ADDR pc = get_frame_pc ();
22317 CORE_ADDR baseaddr = objfile->text_section_offset ();
22318 struct gdbarch *gdbarch = objfile->arch ();
22319
22320 for (const auto &cand_off
22321 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22322 {
22323 struct dwarf2_cu *cand_cu = cu;
22324 struct die_info *cand
22325 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22326 if (!cand
22327 || !cand->parent
22328 || cand->parent->tag != DW_TAG_subprogram)
22329 continue;
22330
22331 CORE_ADDR pc_low, pc_high;
22332 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22333 if (pc_low == ((CORE_ADDR) -1))
22334 continue;
22335 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22336 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22337 if (!(pc_low <= pc && pc < pc_high))
22338 continue;
22339
22340 die = cand;
22341 attr = dwarf2_attr (die, DW_AT_location, cu);
22342 break;
22343 }
22344 }
22345
22346 if (!attr)
22347 {
22348 /* DWARF: "If there is no such attribute, then there is no effect.".
22349 DATA is ignored if SIZE is 0. */
22350
22351 retval.data = NULL;
22352 retval.size = 0;
22353 }
22354 else if (attr->form_is_section_offset ())
22355 {
22356 struct dwarf2_loclist_baton loclist_baton;
22357 CORE_ADDR pc = get_frame_pc ();
22358 size_t size;
22359
22360 fill_in_loclist_baton (cu, &loclist_baton, attr);
22361
22362 retval.data = dwarf2_find_location_expression (&loclist_baton,
22363 &size, pc);
22364 retval.size = size;
22365 }
22366 else
22367 {
22368 if (!attr->form_is_block ())
22369 error (_("Dwarf Error: DIE at %s referenced in module %s "
22370 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22371 sect_offset_str (sect_off), objfile_name (objfile));
22372
22373 struct dwarf_block *block = attr->as_block ();
22374 retval.data = block->data;
22375 retval.size = block->size;
22376 }
22377 retval.per_objfile = per_objfile;
22378 retval.per_cu = cu->per_cu;
22379
22380 per_objfile->age_comp_units ();
22381
22382 return retval;
22383 }
22384
22385 /* See read.h. */
22386
22387 struct dwarf2_locexpr_baton
22388 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22389 dwarf2_per_cu_data *per_cu,
22390 dwarf2_per_objfile *per_objfile,
22391 gdb::function_view<CORE_ADDR ()> get_frame_pc)
22392 {
22393 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22394
22395 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22396 get_frame_pc);
22397 }
22398
22399 /* Write a constant of a given type as target-ordered bytes into
22400 OBSTACK. */
22401
22402 static const gdb_byte *
22403 write_constant_as_bytes (struct obstack *obstack,
22404 enum bfd_endian byte_order,
22405 struct type *type,
22406 ULONGEST value,
22407 LONGEST *len)
22408 {
22409 gdb_byte *result;
22410
22411 *len = TYPE_LENGTH (type);
22412 result = (gdb_byte *) obstack_alloc (obstack, *len);
22413 store_unsigned_integer (result, *len, byte_order, value);
22414
22415 return result;
22416 }
22417
22418 /* See read.h. */
22419
22420 const gdb_byte *
22421 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22422 dwarf2_per_cu_data *per_cu,
22423 dwarf2_per_objfile *per_objfile,
22424 obstack *obstack,
22425 LONGEST *len)
22426 {
22427 struct die_info *die;
22428 struct attribute *attr;
22429 const gdb_byte *result = NULL;
22430 struct type *type;
22431 LONGEST value;
22432 enum bfd_endian byte_order;
22433 struct objfile *objfile = per_objfile->objfile;
22434
22435 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22436 if (cu == nullptr)
22437 cu = load_cu (per_cu, per_objfile, false);
22438
22439 if (cu == nullptr)
22440 {
22441 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22442 Instead just throw an error, not much else we can do. */
22443 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22444 sect_offset_str (sect_off), objfile_name (objfile));
22445 }
22446
22447 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22448 if (!die)
22449 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22450 sect_offset_str (sect_off), objfile_name (objfile));
22451
22452 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22453 if (attr == NULL)
22454 return NULL;
22455
22456 byte_order = (bfd_big_endian (objfile->obfd)
22457 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22458
22459 switch (attr->form)
22460 {
22461 case DW_FORM_addr:
22462 case DW_FORM_addrx:
22463 case DW_FORM_GNU_addr_index:
22464 {
22465 gdb_byte *tem;
22466
22467 *len = cu->header.addr_size;
22468 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22469 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
22470 result = tem;
22471 }
22472 break;
22473 case DW_FORM_string:
22474 case DW_FORM_strp:
22475 case DW_FORM_strx:
22476 case DW_FORM_GNU_str_index:
22477 case DW_FORM_GNU_strp_alt:
22478 /* The string is already allocated on the objfile obstack, point
22479 directly to it. */
22480 {
22481 const char *attr_name = attr->as_string ();
22482 result = (const gdb_byte *) attr_name;
22483 *len = strlen (attr_name);
22484 }
22485 break;
22486 case DW_FORM_block1:
22487 case DW_FORM_block2:
22488 case DW_FORM_block4:
22489 case DW_FORM_block:
22490 case DW_FORM_exprloc:
22491 case DW_FORM_data16:
22492 {
22493 struct dwarf_block *block = attr->as_block ();
22494 result = block->data;
22495 *len = block->size;
22496 }
22497 break;
22498
22499 /* The DW_AT_const_value attributes are supposed to carry the
22500 symbol's value "represented as it would be on the target
22501 architecture." By the time we get here, it's already been
22502 converted to host endianness, so we just need to sign- or
22503 zero-extend it as appropriate. */
22504 case DW_FORM_data1:
22505 type = die_type (die, cu);
22506 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22507 if (result == NULL)
22508 result = write_constant_as_bytes (obstack, byte_order,
22509 type, value, len);
22510 break;
22511 case DW_FORM_data2:
22512 type = die_type (die, cu);
22513 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22514 if (result == NULL)
22515 result = write_constant_as_bytes (obstack, byte_order,
22516 type, value, len);
22517 break;
22518 case DW_FORM_data4:
22519 type = die_type (die, cu);
22520 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22521 if (result == NULL)
22522 result = write_constant_as_bytes (obstack, byte_order,
22523 type, value, len);
22524 break;
22525 case DW_FORM_data8:
22526 type = die_type (die, cu);
22527 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22528 if (result == NULL)
22529 result = write_constant_as_bytes (obstack, byte_order,
22530 type, value, len);
22531 break;
22532
22533 case DW_FORM_sdata:
22534 case DW_FORM_implicit_const:
22535 type = die_type (die, cu);
22536 result = write_constant_as_bytes (obstack, byte_order,
22537 type, attr->as_signed (), len);
22538 break;
22539
22540 case DW_FORM_udata:
22541 type = die_type (die, cu);
22542 result = write_constant_as_bytes (obstack, byte_order,
22543 type, attr->as_unsigned (), len);
22544 break;
22545
22546 default:
22547 complaint (_("unsupported const value attribute form: '%s'"),
22548 dwarf_form_name (attr->form));
22549 break;
22550 }
22551
22552 return result;
22553 }
22554
22555 /* See read.h. */
22556
22557 struct type *
22558 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22559 dwarf2_per_cu_data *per_cu,
22560 dwarf2_per_objfile *per_objfile,
22561 const char **var_name)
22562 {
22563 struct die_info *die;
22564
22565 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22566 if (cu == nullptr)
22567 cu = load_cu (per_cu, per_objfile, false);
22568
22569 if (cu == nullptr)
22570 return nullptr;
22571
22572 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22573 if (!die)
22574 return NULL;
22575
22576 if (var_name != nullptr)
22577 *var_name = var_decl_name (die, cu);
22578 return die_type (die, cu);
22579 }
22580
22581 /* See read.h. */
22582
22583 struct type *
22584 dwarf2_get_die_type (cu_offset die_offset,
22585 dwarf2_per_cu_data *per_cu,
22586 dwarf2_per_objfile *per_objfile)
22587 {
22588 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22589 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22590 }
22591
22592 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22593 On entry *REF_CU is the CU of SRC_DIE.
22594 On exit *REF_CU is the CU of the result.
22595 Returns NULL if the referenced DIE isn't found. */
22596
22597 static struct die_info *
22598 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22599 struct dwarf2_cu **ref_cu)
22600 {
22601 struct die_info temp_die;
22602 struct dwarf2_cu *sig_cu;
22603 struct die_info *die;
22604 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22605
22606
22607 /* While it might be nice to assert sig_type->type == NULL here,
22608 we can get here for DW_AT_imported_declaration where we need
22609 the DIE not the type. */
22610
22611 /* If necessary, add it to the queue and load its DIEs.
22612
22613 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22614 it doesn't mean they are currently loaded. Since we require them
22615 to be loaded, we must check for ourselves. */
22616 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
22617 language_minimal)
22618 || per_objfile->get_cu (sig_type) == nullptr)
22619 read_signatured_type (sig_type, per_objfile);
22620
22621 sig_cu = per_objfile->get_cu (sig_type);
22622 gdb_assert (sig_cu != NULL);
22623 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22624 temp_die.sect_off = sig_type->type_offset_in_section;
22625 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22626 to_underlying (temp_die.sect_off));
22627 if (die)
22628 {
22629 /* For .gdb_index version 7 keep track of included TUs.
22630 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22631 if (per_objfile->per_bfd->index_table != NULL
22632 && per_objfile->per_bfd->index_table->version <= 7)
22633 {
22634 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22635 }
22636
22637 *ref_cu = sig_cu;
22638 return die;
22639 }
22640
22641 return NULL;
22642 }
22643
22644 /* Follow signatured type referenced by ATTR in SRC_DIE.
22645 On entry *REF_CU is the CU of SRC_DIE.
22646 On exit *REF_CU is the CU of the result.
22647 The result is the DIE of the type.
22648 If the referenced type cannot be found an error is thrown. */
22649
22650 static struct die_info *
22651 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22652 struct dwarf2_cu **ref_cu)
22653 {
22654 ULONGEST signature = attr->as_signature ();
22655 struct signatured_type *sig_type;
22656 struct die_info *die;
22657
22658 gdb_assert (attr->form == DW_FORM_ref_sig8);
22659
22660 sig_type = lookup_signatured_type (*ref_cu, signature);
22661 /* sig_type will be NULL if the signatured type is missing from
22662 the debug info. */
22663 if (sig_type == NULL)
22664 {
22665 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22666 " from DIE at %s [in module %s]"),
22667 hex_string (signature), sect_offset_str (src_die->sect_off),
22668 objfile_name ((*ref_cu)->per_objfile->objfile));
22669 }
22670
22671 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22672 if (die == NULL)
22673 {
22674 dump_die_for_error (src_die);
22675 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22676 " from DIE at %s [in module %s]"),
22677 hex_string (signature), sect_offset_str (src_die->sect_off),
22678 objfile_name ((*ref_cu)->per_objfile->objfile));
22679 }
22680
22681 return die;
22682 }
22683
22684 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22685 reading in and processing the type unit if necessary. */
22686
22687 static struct type *
22688 get_signatured_type (struct die_info *die, ULONGEST signature,
22689 struct dwarf2_cu *cu)
22690 {
22691 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22692 struct signatured_type *sig_type;
22693 struct dwarf2_cu *type_cu;
22694 struct die_info *type_die;
22695 struct type *type;
22696
22697 sig_type = lookup_signatured_type (cu, signature);
22698 /* sig_type will be NULL if the signatured type is missing from
22699 the debug info. */
22700 if (sig_type == NULL)
22701 {
22702 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22703 " from DIE at %s [in module %s]"),
22704 hex_string (signature), sect_offset_str (die->sect_off),
22705 objfile_name (per_objfile->objfile));
22706 return build_error_marker_type (cu, die);
22707 }
22708
22709 /* If we already know the type we're done. */
22710 type = per_objfile->get_type_for_signatured_type (sig_type);
22711 if (type != nullptr)
22712 return type;
22713
22714 type_cu = cu;
22715 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22716 if (type_die != NULL)
22717 {
22718 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22719 is created. This is important, for example, because for c++ classes
22720 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22721 type = read_type_die (type_die, type_cu);
22722 if (type == NULL)
22723 {
22724 complaint (_("Dwarf Error: Cannot build signatured type %s"
22725 " referenced from DIE at %s [in module %s]"),
22726 hex_string (signature), sect_offset_str (die->sect_off),
22727 objfile_name (per_objfile->objfile));
22728 type = build_error_marker_type (cu, die);
22729 }
22730 }
22731 else
22732 {
22733 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22734 " from DIE at %s [in module %s]"),
22735 hex_string (signature), sect_offset_str (die->sect_off),
22736 objfile_name (per_objfile->objfile));
22737 type = build_error_marker_type (cu, die);
22738 }
22739
22740 per_objfile->set_type_for_signatured_type (sig_type, type);
22741
22742 return type;
22743 }
22744
22745 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22746 reading in and processing the type unit if necessary. */
22747
22748 static struct type *
22749 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22750 struct dwarf2_cu *cu) /* ARI: editCase function */
22751 {
22752 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22753 if (attr->form_is_ref ())
22754 {
22755 struct dwarf2_cu *type_cu = cu;
22756 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22757
22758 return read_type_die (type_die, type_cu);
22759 }
22760 else if (attr->form == DW_FORM_ref_sig8)
22761 {
22762 return get_signatured_type (die, attr->as_signature (), cu);
22763 }
22764 else
22765 {
22766 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22767
22768 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22769 " at %s [in module %s]"),
22770 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22771 objfile_name (per_objfile->objfile));
22772 return build_error_marker_type (cu, die);
22773 }
22774 }
22775
22776 /* Load the DIEs associated with type unit PER_CU into memory. */
22777
22778 static void
22779 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22780 dwarf2_per_objfile *per_objfile)
22781 {
22782 struct signatured_type *sig_type;
22783
22784 /* We have the per_cu, but we need the signatured_type.
22785 Fortunately this is an easy translation. */
22786 gdb_assert (per_cu->is_debug_types);
22787 sig_type = (struct signatured_type *) per_cu;
22788
22789 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22790
22791 read_signatured_type (sig_type, per_objfile);
22792
22793 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22794 }
22795
22796 /* Read in a signatured type and build its CU and DIEs.
22797 If the type is a stub for the real type in a DWO file,
22798 read in the real type from the DWO file as well. */
22799
22800 static void
22801 read_signatured_type (signatured_type *sig_type,
22802 dwarf2_per_objfile *per_objfile)
22803 {
22804 gdb_assert (sig_type->is_debug_types);
22805 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
22806
22807 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
22808
22809 if (!reader.dummy_p)
22810 {
22811 struct dwarf2_cu *cu = reader.cu;
22812 const gdb_byte *info_ptr = reader.info_ptr;
22813
22814 gdb_assert (cu->die_hash == NULL);
22815 cu->die_hash =
22816 htab_create_alloc_ex (cu->header.length / 12,
22817 die_hash,
22818 die_eq,
22819 NULL,
22820 &cu->comp_unit_obstack,
22821 hashtab_obstack_allocate,
22822 dummy_obstack_deallocate);
22823
22824 if (reader.comp_unit_die->has_children)
22825 reader.comp_unit_die->child
22826 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22827 reader.comp_unit_die);
22828 cu->dies = reader.comp_unit_die;
22829 /* comp_unit_die is not stored in die_hash, no need. */
22830
22831 /* We try not to read any attributes in this function, because
22832 not all CUs needed for references have been loaded yet, and
22833 symbol table processing isn't initialized. But we have to
22834 set the CU language, or we won't be able to build types
22835 correctly. Similarly, if we do not read the producer, we can
22836 not apply producer-specific interpretation. */
22837 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22838
22839 reader.keep ();
22840 }
22841
22842 sig_type->tu_read = 1;
22843 }
22844
22845 /* Decode simple location descriptions.
22846 Given a pointer to a dwarf block that defines a location, compute
22847 the location and return the value. If COMPUTED is non-null, it is
22848 set to true to indicate that decoding was successful, and false
22849 otherwise. If COMPUTED is null, then this function may emit a
22850 complaint. */
22851
22852 static CORE_ADDR
22853 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22854 {
22855 struct objfile *objfile = cu->per_objfile->objfile;
22856 size_t i;
22857 size_t size = blk->size;
22858 const gdb_byte *data = blk->data;
22859 CORE_ADDR stack[64];
22860 int stacki;
22861 unsigned int bytes_read, unsnd;
22862 gdb_byte op;
22863
22864 if (computed != nullptr)
22865 *computed = false;
22866
22867 i = 0;
22868 stacki = 0;
22869 stack[stacki] = 0;
22870 stack[++stacki] = 0;
22871
22872 while (i < size)
22873 {
22874 op = data[i++];
22875 switch (op)
22876 {
22877 case DW_OP_lit0:
22878 case DW_OP_lit1:
22879 case DW_OP_lit2:
22880 case DW_OP_lit3:
22881 case DW_OP_lit4:
22882 case DW_OP_lit5:
22883 case DW_OP_lit6:
22884 case DW_OP_lit7:
22885 case DW_OP_lit8:
22886 case DW_OP_lit9:
22887 case DW_OP_lit10:
22888 case DW_OP_lit11:
22889 case DW_OP_lit12:
22890 case DW_OP_lit13:
22891 case DW_OP_lit14:
22892 case DW_OP_lit15:
22893 case DW_OP_lit16:
22894 case DW_OP_lit17:
22895 case DW_OP_lit18:
22896 case DW_OP_lit19:
22897 case DW_OP_lit20:
22898 case DW_OP_lit21:
22899 case DW_OP_lit22:
22900 case DW_OP_lit23:
22901 case DW_OP_lit24:
22902 case DW_OP_lit25:
22903 case DW_OP_lit26:
22904 case DW_OP_lit27:
22905 case DW_OP_lit28:
22906 case DW_OP_lit29:
22907 case DW_OP_lit30:
22908 case DW_OP_lit31:
22909 stack[++stacki] = op - DW_OP_lit0;
22910 break;
22911
22912 case DW_OP_reg0:
22913 case DW_OP_reg1:
22914 case DW_OP_reg2:
22915 case DW_OP_reg3:
22916 case DW_OP_reg4:
22917 case DW_OP_reg5:
22918 case DW_OP_reg6:
22919 case DW_OP_reg7:
22920 case DW_OP_reg8:
22921 case DW_OP_reg9:
22922 case DW_OP_reg10:
22923 case DW_OP_reg11:
22924 case DW_OP_reg12:
22925 case DW_OP_reg13:
22926 case DW_OP_reg14:
22927 case DW_OP_reg15:
22928 case DW_OP_reg16:
22929 case DW_OP_reg17:
22930 case DW_OP_reg18:
22931 case DW_OP_reg19:
22932 case DW_OP_reg20:
22933 case DW_OP_reg21:
22934 case DW_OP_reg22:
22935 case DW_OP_reg23:
22936 case DW_OP_reg24:
22937 case DW_OP_reg25:
22938 case DW_OP_reg26:
22939 case DW_OP_reg27:
22940 case DW_OP_reg28:
22941 case DW_OP_reg29:
22942 case DW_OP_reg30:
22943 case DW_OP_reg31:
22944 stack[++stacki] = op - DW_OP_reg0;
22945 if (i < size)
22946 {
22947 if (computed == nullptr)
22948 dwarf2_complex_location_expr_complaint ();
22949 else
22950 return 0;
22951 }
22952 break;
22953
22954 case DW_OP_regx:
22955 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22956 i += bytes_read;
22957 stack[++stacki] = unsnd;
22958 if (i < size)
22959 {
22960 if (computed == nullptr)
22961 dwarf2_complex_location_expr_complaint ();
22962 else
22963 return 0;
22964 }
22965 break;
22966
22967 case DW_OP_addr:
22968 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22969 &bytes_read);
22970 i += bytes_read;
22971 break;
22972
22973 case DW_OP_const1u:
22974 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22975 i += 1;
22976 break;
22977
22978 case DW_OP_const1s:
22979 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22980 i += 1;
22981 break;
22982
22983 case DW_OP_const2u:
22984 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22985 i += 2;
22986 break;
22987
22988 case DW_OP_const2s:
22989 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22990 i += 2;
22991 break;
22992
22993 case DW_OP_const4u:
22994 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22995 i += 4;
22996 break;
22997
22998 case DW_OP_const4s:
22999 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23000 i += 4;
23001 break;
23002
23003 case DW_OP_const8u:
23004 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23005 i += 8;
23006 break;
23007
23008 case DW_OP_constu:
23009 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23010 &bytes_read);
23011 i += bytes_read;
23012 break;
23013
23014 case DW_OP_consts:
23015 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23016 i += bytes_read;
23017 break;
23018
23019 case DW_OP_dup:
23020 stack[stacki + 1] = stack[stacki];
23021 stacki++;
23022 break;
23023
23024 case DW_OP_plus:
23025 stack[stacki - 1] += stack[stacki];
23026 stacki--;
23027 break;
23028
23029 case DW_OP_plus_uconst:
23030 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23031 &bytes_read);
23032 i += bytes_read;
23033 break;
23034
23035 case DW_OP_minus:
23036 stack[stacki - 1] -= stack[stacki];
23037 stacki--;
23038 break;
23039
23040 case DW_OP_deref:
23041 /* If we're not the last op, then we definitely can't encode
23042 this using GDB's address_class enum. This is valid for partial
23043 global symbols, although the variable's address will be bogus
23044 in the psymtab. */
23045 if (i < size)
23046 {
23047 if (computed == nullptr)
23048 dwarf2_complex_location_expr_complaint ();
23049 else
23050 return 0;
23051 }
23052 break;
23053
23054 case DW_OP_GNU_push_tls_address:
23055 case DW_OP_form_tls_address:
23056 /* The top of the stack has the offset from the beginning
23057 of the thread control block at which the variable is located. */
23058 /* Nothing should follow this operator, so the top of stack would
23059 be returned. */
23060 /* This is valid for partial global symbols, but the variable's
23061 address will be bogus in the psymtab. Make it always at least
23062 non-zero to not look as a variable garbage collected by linker
23063 which have DW_OP_addr 0. */
23064 if (i < size)
23065 {
23066 if (computed == nullptr)
23067 dwarf2_complex_location_expr_complaint ();
23068 else
23069 return 0;
23070 }
23071 stack[stacki]++;
23072 break;
23073
23074 case DW_OP_GNU_uninit:
23075 if (computed != nullptr)
23076 return 0;
23077 break;
23078
23079 case DW_OP_addrx:
23080 case DW_OP_GNU_addr_index:
23081 case DW_OP_GNU_const_index:
23082 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23083 &bytes_read);
23084 i += bytes_read;
23085 break;
23086
23087 default:
23088 if (computed == nullptr)
23089 {
23090 const char *name = get_DW_OP_name (op);
23091
23092 if (name)
23093 complaint (_("unsupported stack op: '%s'"),
23094 name);
23095 else
23096 complaint (_("unsupported stack op: '%02x'"),
23097 op);
23098 }
23099
23100 return (stack[stacki]);
23101 }
23102
23103 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23104 outside of the allocated space. Also enforce minimum>0. */
23105 if (stacki >= ARRAY_SIZE (stack) - 1)
23106 {
23107 if (computed == nullptr)
23108 complaint (_("location description stack overflow"));
23109 return 0;
23110 }
23111
23112 if (stacki <= 0)
23113 {
23114 if (computed == nullptr)
23115 complaint (_("location description stack underflow"));
23116 return 0;
23117 }
23118 }
23119
23120 if (computed != nullptr)
23121 *computed = true;
23122 return (stack[stacki]);
23123 }
23124
23125 /* memory allocation interface */
23126
23127 static struct dwarf_block *
23128 dwarf_alloc_block (struct dwarf2_cu *cu)
23129 {
23130 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23131 }
23132
23133 static struct die_info *
23134 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23135 {
23136 struct die_info *die;
23137 size_t size = sizeof (struct die_info);
23138
23139 if (num_attrs > 1)
23140 size += (num_attrs - 1) * sizeof (struct attribute);
23141
23142 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23143 memset (die, 0, sizeof (struct die_info));
23144 return (die);
23145 }
23146
23147 \f
23148
23149 /* Macro support. */
23150
23151 /* An overload of dwarf_decode_macros that finds the correct section
23152 and ensures it is read in before calling the other overload. */
23153
23154 static void
23155 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23156 int section_is_gnu)
23157 {
23158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23159 struct objfile *objfile = per_objfile->objfile;
23160 const struct line_header *lh = cu->line_header;
23161 unsigned int offset_size = cu->header.offset_size;
23162 struct dwarf2_section_info *section;
23163 const char *section_name;
23164
23165 if (cu->dwo_unit != nullptr)
23166 {
23167 if (section_is_gnu)
23168 {
23169 section = &cu->dwo_unit->dwo_file->sections.macro;
23170 section_name = ".debug_macro.dwo";
23171 }
23172 else
23173 {
23174 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23175 section_name = ".debug_macinfo.dwo";
23176 }
23177 }
23178 else
23179 {
23180 if (section_is_gnu)
23181 {
23182 section = &per_objfile->per_bfd->macro;
23183 section_name = ".debug_macro";
23184 }
23185 else
23186 {
23187 section = &per_objfile->per_bfd->macinfo;
23188 section_name = ".debug_macinfo";
23189 }
23190 }
23191
23192 section->read (objfile);
23193 if (section->buffer == nullptr)
23194 {
23195 complaint (_("missing %s section"), section_name);
23196 return;
23197 }
23198
23199 buildsym_compunit *builder = cu->get_builder ();
23200
23201 struct dwarf2_section_info *str_offsets_section;
23202 struct dwarf2_section_info *str_section;
23203 gdb::optional<ULONGEST> str_offsets_base;
23204
23205 if (cu->dwo_unit != nullptr)
23206 {
23207 str_offsets_section = &cu->dwo_unit->dwo_file
23208 ->sections.str_offsets;
23209 str_section = &cu->dwo_unit->dwo_file->sections.str;
23210 str_offsets_base = cu->header.addr_size;
23211 }
23212 else
23213 {
23214 str_offsets_section = &per_objfile->per_bfd->str_offsets;
23215 str_section = &per_objfile->per_bfd->str;
23216 str_offsets_base = cu->str_offsets_base;
23217 }
23218
23219 dwarf_decode_macros (per_objfile, builder, section, lh,
23220 offset_size, offset, str_section, str_offsets_section,
23221 str_offsets_base, section_is_gnu);
23222 }
23223
23224 /* Return the .debug_loc section to use for CU.
23225 For DWO files use .debug_loc.dwo. */
23226
23227 static struct dwarf2_section_info *
23228 cu_debug_loc_section (struct dwarf2_cu *cu)
23229 {
23230 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23231
23232 if (cu->dwo_unit)
23233 {
23234 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23235
23236 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23237 }
23238 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23239 : &per_objfile->per_bfd->loc);
23240 }
23241
23242 /* Return the .debug_rnglists section to use for CU. */
23243 static struct dwarf2_section_info *
23244 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23245 {
23246 if (cu->header.version < 5)
23247 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23248 cu->header.version);
23249 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23250
23251 /* Make sure we read the .debug_rnglists section from the file that
23252 contains the DW_AT_ranges attribute we are reading. Normally that
23253 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23254 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23255 program. */
23256 if (cu->dwo_unit != nullptr
23257 && tag != DW_TAG_compile_unit
23258 && tag != DW_TAG_skeleton_unit)
23259 {
23260 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23261
23262 if (sections->rnglists.size > 0)
23263 return &sections->rnglists;
23264 else
23265 error (_(".debug_rnglists section is missing from .dwo file."));
23266 }
23267 return &dwarf2_per_objfile->per_bfd->rnglists;
23268 }
23269
23270 /* A helper function that fills in a dwarf2_loclist_baton. */
23271
23272 static void
23273 fill_in_loclist_baton (struct dwarf2_cu *cu,
23274 struct dwarf2_loclist_baton *baton,
23275 const struct attribute *attr)
23276 {
23277 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23278 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23279
23280 section->read (per_objfile->objfile);
23281
23282 baton->per_objfile = per_objfile;
23283 baton->per_cu = cu->per_cu;
23284 gdb_assert (baton->per_cu);
23285 /* We don't know how long the location list is, but make sure we
23286 don't run off the edge of the section. */
23287 baton->size = section->size - attr->as_unsigned ();
23288 baton->data = section->buffer + attr->as_unsigned ();
23289 if (cu->base_address.has_value ())
23290 baton->base_address = *cu->base_address;
23291 else
23292 baton->base_address = 0;
23293 baton->from_dwo = cu->dwo_unit != NULL;
23294 }
23295
23296 static void
23297 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23298 struct dwarf2_cu *cu, int is_block)
23299 {
23300 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23301 struct objfile *objfile = per_objfile->objfile;
23302 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23303
23304 if (attr->form_is_section_offset ()
23305 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23306 the section. If so, fall through to the complaint in the
23307 other branch. */
23308 && attr->as_unsigned () < section->get_size (objfile))
23309 {
23310 struct dwarf2_loclist_baton *baton;
23311
23312 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23313
23314 fill_in_loclist_baton (cu, baton, attr);
23315
23316 if (!cu->base_address.has_value ())
23317 complaint (_("Location list used without "
23318 "specifying the CU base address."));
23319
23320 sym->set_aclass_index ((is_block
23321 ? dwarf2_loclist_block_index
23322 : dwarf2_loclist_index));
23323 SYMBOL_LOCATION_BATON (sym) = baton;
23324 }
23325 else
23326 {
23327 struct dwarf2_locexpr_baton *baton;
23328
23329 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23330 baton->per_objfile = per_objfile;
23331 baton->per_cu = cu->per_cu;
23332 gdb_assert (baton->per_cu);
23333
23334 if (attr->form_is_block ())
23335 {
23336 /* Note that we're just copying the block's data pointer
23337 here, not the actual data. We're still pointing into the
23338 info_buffer for SYM's objfile; right now we never release
23339 that buffer, but when we do clean up properly this may
23340 need to change. */
23341 struct dwarf_block *block = attr->as_block ();
23342 baton->size = block->size;
23343 baton->data = block->data;
23344 }
23345 else
23346 {
23347 dwarf2_invalid_attrib_class_complaint ("location description",
23348 sym->natural_name ());
23349 baton->size = 0;
23350 }
23351
23352 sym->set_aclass_index ((is_block
23353 ? dwarf2_locexpr_block_index
23354 : dwarf2_locexpr_index));
23355 SYMBOL_LOCATION_BATON (sym) = baton;
23356 }
23357 }
23358
23359 /* See read.h. */
23360
23361 const comp_unit_head *
23362 dwarf2_per_cu_data::get_header () const
23363 {
23364 if (!m_header_read_in)
23365 {
23366 const gdb_byte *info_ptr
23367 = this->section->buffer + to_underlying (this->sect_off);
23368
23369 memset (&m_header, 0, sizeof (m_header));
23370
23371 read_comp_unit_head (&m_header, info_ptr, this->section,
23372 rcuh_kind::COMPILE);
23373
23374 m_header_read_in = true;
23375 }
23376
23377 return &m_header;
23378 }
23379
23380 /* See read.h. */
23381
23382 int
23383 dwarf2_per_cu_data::addr_size () const
23384 {
23385 return this->get_header ()->addr_size;
23386 }
23387
23388 /* See read.h. */
23389
23390 int
23391 dwarf2_per_cu_data::offset_size () const
23392 {
23393 return this->get_header ()->offset_size;
23394 }
23395
23396 /* See read.h. */
23397
23398 int
23399 dwarf2_per_cu_data::ref_addr_size () const
23400 {
23401 const comp_unit_head *header = this->get_header ();
23402
23403 if (header->version == 2)
23404 return header->addr_size;
23405 else
23406 return header->offset_size;
23407 }
23408
23409 /* A helper function for dwarf2_find_containing_comp_unit that returns
23410 the index of the result, and that searches a vector. It will
23411 return a result even if the offset in question does not actually
23412 occur in any CU. This is separate so that it can be unit
23413 tested. */
23414
23415 static int
23416 dwarf2_find_containing_comp_unit
23417 (sect_offset sect_off,
23418 unsigned int offset_in_dwz,
23419 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
23420 {
23421 int low, high;
23422
23423 low = 0;
23424 high = all_comp_units.size () - 1;
23425 while (high > low)
23426 {
23427 struct dwarf2_per_cu_data *mid_cu;
23428 int mid = low + (high - low) / 2;
23429
23430 mid_cu = all_comp_units[mid].get ();
23431 if (mid_cu->is_dwz > offset_in_dwz
23432 || (mid_cu->is_dwz == offset_in_dwz
23433 && mid_cu->sect_off + mid_cu->length > sect_off))
23434 high = mid;
23435 else
23436 low = mid + 1;
23437 }
23438 gdb_assert (low == high);
23439 return low;
23440 }
23441
23442 /* Locate the .debug_info compilation unit from CU's objfile which contains
23443 the DIE at OFFSET. Raises an error on failure. */
23444
23445 static struct dwarf2_per_cu_data *
23446 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23447 unsigned int offset_in_dwz,
23448 dwarf2_per_bfd *per_bfd)
23449 {
23450 int low = dwarf2_find_containing_comp_unit
23451 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
23452 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
23453
23454 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23455 {
23456 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23457 error (_("Dwarf Error: could not find partial DIE containing "
23458 "offset %s [in module %s]"),
23459 sect_offset_str (sect_off),
23460 bfd_get_filename (per_bfd->obfd));
23461
23462 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
23463 <= sect_off);
23464 return per_bfd->all_comp_units[low - 1].get ();
23465 }
23466 else
23467 {
23468 if (low == per_bfd->all_comp_units.size () - 1
23469 && sect_off >= this_cu->sect_off + this_cu->length)
23470 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23471 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23472 return this_cu;
23473 }
23474 }
23475
23476 #if GDB_SELF_TEST
23477
23478 namespace selftests {
23479 namespace find_containing_comp_unit {
23480
23481 static void
23482 run_test ()
23483 {
23484 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
23485 dwarf2_per_cu_data *one_ptr = one.get ();
23486 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
23487 dwarf2_per_cu_data *two_ptr = two.get ();
23488 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
23489 dwarf2_per_cu_data *three_ptr = three.get ();
23490 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
23491 dwarf2_per_cu_data *four_ptr = four.get ();
23492
23493 one->length = 5;
23494 two->sect_off = sect_offset (one->length);
23495 two->length = 7;
23496
23497 three->length = 5;
23498 three->is_dwz = 1;
23499 four->sect_off = sect_offset (three->length);
23500 four->length = 7;
23501 four->is_dwz = 1;
23502
23503 std::vector<dwarf2_per_cu_data_up> units;
23504 units.push_back (std::move (one));
23505 units.push_back (std::move (two));
23506 units.push_back (std::move (three));
23507 units.push_back (std::move (four));
23508
23509 int result;
23510
23511 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23512 SELF_CHECK (units[result].get () == one_ptr);
23513 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23514 SELF_CHECK (units[result].get () == one_ptr);
23515 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23516 SELF_CHECK (units[result].get () == two_ptr);
23517
23518 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23519 SELF_CHECK (units[result].get () == three_ptr);
23520 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23521 SELF_CHECK (units[result].get () == three_ptr);
23522 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23523 SELF_CHECK (units[result].get () == four_ptr);
23524 }
23525
23526 }
23527 }
23528
23529 #endif /* GDB_SELF_TEST */
23530
23531 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23532
23533 static void
23534 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23535 enum language pretend_language)
23536 {
23537 struct attribute *attr;
23538
23539 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23540
23541 /* Set the language we're debugging. */
23542 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23543 if (cu->producer != nullptr
23544 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
23545 {
23546 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23547 attribute is not standardised yet. As a workaround for the
23548 language detection we fall back to the DW_AT_producer
23549 string. */
23550 cu->per_cu->lang = language_opencl;
23551 }
23552 else if (cu->producer != nullptr
23553 && strstr (cu->producer, "GNU Go ") != NULL)
23554 {
23555 /* Similar hack for Go. */
23556 cu->per_cu->lang = language_go;
23557 }
23558 else if (attr != nullptr)
23559 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
23560 else
23561 cu->per_cu->lang = pretend_language;
23562 cu->language_defn = language_def (cu->per_cu->lang);
23563 }
23564
23565 /* See read.h. */
23566
23567 dwarf2_cu *
23568 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23569 {
23570 auto it = m_dwarf2_cus.find (per_cu);
23571 if (it == m_dwarf2_cus.end ())
23572 return nullptr;
23573
23574 return it->second;
23575 }
23576
23577 /* See read.h. */
23578
23579 void
23580 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23581 {
23582 gdb_assert (this->get_cu (per_cu) == nullptr);
23583
23584 m_dwarf2_cus[per_cu] = cu;
23585 }
23586
23587 /* See read.h. */
23588
23589 void
23590 dwarf2_per_objfile::age_comp_units ()
23591 {
23592 dwarf_read_debug_printf_v ("running");
23593
23594 /* This is not expected to be called in the middle of CU expansion. There is
23595 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23596 loaded in memory. Calling age_comp_units while the queue is in use could
23597 make us free the DIEs for a CU that is in the queue and therefore break
23598 that invariant. */
23599 gdb_assert (!this->per_bfd->queue.has_value ());
23600
23601 /* Start by clearing all marks. */
23602 for (auto pair : m_dwarf2_cus)
23603 pair.second->clear_mark ();
23604
23605 /* Traverse all CUs, mark them and their dependencies if used recently
23606 enough. */
23607 for (auto pair : m_dwarf2_cus)
23608 {
23609 dwarf2_cu *cu = pair.second;
23610
23611 cu->last_used++;
23612 if (cu->last_used <= dwarf_max_cache_age)
23613 cu->mark ();
23614 }
23615
23616 /* Delete all CUs still not marked. */
23617 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23618 {
23619 dwarf2_cu *cu = it->second;
23620
23621 if (!cu->is_marked ())
23622 {
23623 dwarf_read_debug_printf_v ("deleting old CU %s",
23624 sect_offset_str (cu->per_cu->sect_off));
23625 delete cu;
23626 it = m_dwarf2_cus.erase (it);
23627 }
23628 else
23629 it++;
23630 }
23631 }
23632
23633 /* See read.h. */
23634
23635 void
23636 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23637 {
23638 auto it = m_dwarf2_cus.find (per_cu);
23639 if (it == m_dwarf2_cus.end ())
23640 return;
23641
23642 delete it->second;
23643
23644 m_dwarf2_cus.erase (it);
23645 }
23646
23647 dwarf2_per_objfile::~dwarf2_per_objfile ()
23648 {
23649 remove_all_cus ();
23650 }
23651
23652 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23653 We store these in a hash table separate from the DIEs, and preserve them
23654 when the DIEs are flushed out of cache.
23655
23656 The CU "per_cu" pointer is needed because offset alone is not enough to
23657 uniquely identify the type. A file may have multiple .debug_types sections,
23658 or the type may come from a DWO file. Furthermore, while it's more logical
23659 to use per_cu->section+offset, with Fission the section with the data is in
23660 the DWO file but we don't know that section at the point we need it.
23661 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23662 because we can enter the lookup routine, get_die_type_at_offset, from
23663 outside this file, and thus won't necessarily have PER_CU->cu.
23664 Fortunately, PER_CU is stable for the life of the objfile. */
23665
23666 struct dwarf2_per_cu_offset_and_type
23667 {
23668 const struct dwarf2_per_cu_data *per_cu;
23669 sect_offset sect_off;
23670 struct type *type;
23671 };
23672
23673 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23674
23675 static hashval_t
23676 per_cu_offset_and_type_hash (const void *item)
23677 {
23678 const struct dwarf2_per_cu_offset_and_type *ofs
23679 = (const struct dwarf2_per_cu_offset_and_type *) item;
23680
23681 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23682 }
23683
23684 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23685
23686 static int
23687 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23688 {
23689 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23690 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23691 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23692 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23693
23694 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23695 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23696 }
23697
23698 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23699 table if necessary. For convenience, return TYPE.
23700
23701 The DIEs reading must have careful ordering to:
23702 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23703 reading current DIE.
23704 * Not trying to dereference contents of still incompletely read in types
23705 while reading in other DIEs.
23706 * Enable referencing still incompletely read in types just by a pointer to
23707 the type without accessing its fields.
23708
23709 Therefore caller should follow these rules:
23710 * Try to fetch any prerequisite types we may need to build this DIE type
23711 before building the type and calling set_die_type.
23712 * After building type call set_die_type for current DIE as soon as
23713 possible before fetching more types to complete the current type.
23714 * Make the type as complete as possible before fetching more types. */
23715
23716 static struct type *
23717 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
23718 bool skip_data_location)
23719 {
23720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23721 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23722 struct objfile *objfile = per_objfile->objfile;
23723 struct attribute *attr;
23724 struct dynamic_prop prop;
23725
23726 /* For Ada types, make sure that the gnat-specific data is always
23727 initialized (if not already set). There are a few types where
23728 we should not be doing so, because the type-specific area is
23729 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23730 where the type-specific area is used to store the floatformat).
23731 But this is not a problem, because the gnat-specific information
23732 is actually not needed for these types. */
23733 if (need_gnat_info (cu)
23734 && type->code () != TYPE_CODE_FUNC
23735 && type->code () != TYPE_CODE_FLT
23736 && type->code () != TYPE_CODE_METHODPTR
23737 && type->code () != TYPE_CODE_MEMBERPTR
23738 && type->code () != TYPE_CODE_METHOD
23739 && type->code () != TYPE_CODE_FIXED_POINT
23740 && !HAVE_GNAT_AUX_INFO (type))
23741 INIT_GNAT_SPECIFIC (type);
23742
23743 /* Read DW_AT_allocated and set in type. */
23744 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23745 if (attr != NULL)
23746 {
23747 struct type *prop_type = cu->addr_sized_int_type (false);
23748 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23749 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23750 }
23751
23752 /* Read DW_AT_associated and set in type. */
23753 attr = dwarf2_attr (die, DW_AT_associated, cu);
23754 if (attr != NULL)
23755 {
23756 struct type *prop_type = cu->addr_sized_int_type (false);
23757 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23758 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23759 }
23760
23761 /* Read DW_AT_rank and set in type. */
23762 attr = dwarf2_attr (die, DW_AT_rank, cu);
23763 if (attr != NULL)
23764 {
23765 struct type *prop_type = cu->addr_sized_int_type (false);
23766 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23767 type->add_dyn_prop (DYN_PROP_RANK, prop);
23768 }
23769
23770 /* Read DW_AT_data_location and set in type. */
23771 if (!skip_data_location)
23772 {
23773 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23774 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23775 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23776 }
23777
23778 if (per_objfile->die_type_hash == NULL)
23779 per_objfile->die_type_hash
23780 = htab_up (htab_create_alloc (127,
23781 per_cu_offset_and_type_hash,
23782 per_cu_offset_and_type_eq,
23783 NULL, xcalloc, xfree));
23784
23785 ofs.per_cu = cu->per_cu;
23786 ofs.sect_off = die->sect_off;
23787 ofs.type = type;
23788 slot = (struct dwarf2_per_cu_offset_and_type **)
23789 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
23790 if (*slot)
23791 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23792 sect_offset_str (die->sect_off));
23793 *slot = XOBNEW (&objfile->objfile_obstack,
23794 struct dwarf2_per_cu_offset_and_type);
23795 **slot = ofs;
23796 return type;
23797 }
23798
23799 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23800 or return NULL if the die does not have a saved type. */
23801
23802 static struct type *
23803 get_die_type_at_offset (sect_offset sect_off,
23804 dwarf2_per_cu_data *per_cu,
23805 dwarf2_per_objfile *per_objfile)
23806 {
23807 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23808
23809 if (per_objfile->die_type_hash == NULL)
23810 return NULL;
23811
23812 ofs.per_cu = per_cu;
23813 ofs.sect_off = sect_off;
23814 slot = ((struct dwarf2_per_cu_offset_and_type *)
23815 htab_find (per_objfile->die_type_hash.get (), &ofs));
23816 if (slot)
23817 return slot->type;
23818 else
23819 return NULL;
23820 }
23821
23822 /* Look up the type for DIE in CU in die_type_hash,
23823 or return NULL if DIE does not have a saved type. */
23824
23825 static struct type *
23826 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23827 {
23828 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23829 }
23830
23831 struct cmd_list_element *set_dwarf_cmdlist;
23832 struct cmd_list_element *show_dwarf_cmdlist;
23833
23834 static void
23835 show_check_physname (struct ui_file *file, int from_tty,
23836 struct cmd_list_element *c, const char *value)
23837 {
23838 gdb_printf (file,
23839 _("Whether to check \"physname\" is %s.\n"),
23840 value);
23841 }
23842
23843 void _initialize_dwarf2_read ();
23844 void
23845 _initialize_dwarf2_read ()
23846 {
23847 add_setshow_prefix_cmd ("dwarf", class_maintenance,
23848 _("\
23849 Set DWARF specific variables.\n\
23850 Configure DWARF variables such as the cache size."),
23851 _("\
23852 Show DWARF specific variables.\n\
23853 Show DWARF variables such as the cache size."),
23854 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
23855 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
23856
23857 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23858 &dwarf_max_cache_age, _("\
23859 Set the upper bound on the age of cached DWARF compilation units."), _("\
23860 Show the upper bound on the age of cached DWARF compilation units."), _("\
23861 A higher limit means that cached compilation units will be stored\n\
23862 in memory longer, and more total memory will be used. Zero disables\n\
23863 caching, which can slow down startup."),
23864 NULL,
23865 show_dwarf_max_cache_age,
23866 &set_dwarf_cmdlist,
23867 &show_dwarf_cmdlist);
23868
23869 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23870 Set debugging of the DWARF reader."), _("\
23871 Show debugging of the DWARF reader."), _("\
23872 When enabled (non-zero), debugging messages are printed during DWARF\n\
23873 reading and symtab expansion. A value of 1 (one) provides basic\n\
23874 information. A value greater than 1 provides more verbose information."),
23875 NULL,
23876 NULL,
23877 &setdebuglist, &showdebuglist);
23878
23879 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23880 Set debugging of the DWARF DIE reader."), _("\
23881 Show debugging of the DWARF DIE reader."), _("\
23882 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23883 The value is the maximum depth to print."),
23884 NULL,
23885 NULL,
23886 &setdebuglist, &showdebuglist);
23887
23888 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23889 Set debugging of the dwarf line reader."), _("\
23890 Show debugging of the dwarf line reader."), _("\
23891 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23892 A value of 1 (one) provides basic information.\n\
23893 A value greater than 1 provides more verbose information."),
23894 NULL,
23895 NULL,
23896 &setdebuglist, &showdebuglist);
23897
23898 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23899 Set cross-checking of \"physname\" code against demangler."), _("\
23900 Show cross-checking of \"physname\" code against demangler."), _("\
23901 When enabled, GDB's internal \"physname\" code is checked against\n\
23902 the demangler."),
23903 NULL, show_check_physname,
23904 &setdebuglist, &showdebuglist);
23905
23906 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23907 no_class, &use_deprecated_index_sections, _("\
23908 Set whether to use deprecated gdb_index sections."), _("\
23909 Show whether to use deprecated gdb_index sections."), _("\
23910 When enabled, deprecated .gdb_index sections are used anyway.\n\
23911 Normally they are ignored either because of a missing feature or\n\
23912 performance issue.\n\
23913 Warning: This option must be enabled before gdb reads the file."),
23914 NULL,
23915 NULL,
23916 &setlist, &showlist);
23917
23918 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23919 &dwarf2_locexpr_funcs);
23920 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23921 &dwarf2_loclist_funcs);
23922
23923 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23924 &dwarf2_block_frame_base_locexpr_funcs);
23925 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23926 &dwarf2_block_frame_base_loclist_funcs);
23927
23928 #if GDB_SELF_TEST
23929 selftests::register_test ("dw2_expand_symtabs_matching",
23930 selftests::dw2_expand_symtabs_matching::run_test);
23931 selftests::register_test ("dwarf2_find_containing_comp_unit",
23932 selftests::find_containing_comp_unit::run_test);
23933 #endif
23934 }