]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Cache the result of find_file_and_directory
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 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 "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91 #include <unordered_set>
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100 #define dwarf_read_debug_printf(fmt, ...) \
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106 #define dwarf_read_debug_printf_v(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* When non-zero, dump DIEs after they are read in. */
111 static unsigned int dwarf_die_debug = 0;
112
113 /* When non-zero, dump line number entries as they are read in. */
114 unsigned int dwarf_line_debug = 0;
115
116 /* When true, cross-check physname against demangler. */
117 static bool check_physname = false;
118
119 /* When true, do not reject deprecated .gdb_index sections. */
120 static bool use_deprecated_index_sections = false;
121
122 /* This is used to store the data that is always per objfile. */
123 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125 /* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
135
136 /* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138 static int dwarf2_locexpr_index;
139 static int dwarf2_loclist_index;
140 static int dwarf2_locexpr_block_index;
141 static int dwarf2_loclist_block_index;
142
143 /* Size of .debug_loclists section header for 32-bit DWARF format. */
144 #define LOCLIST_HEADER_SIZE32 12
145
146 /* Size of .debug_loclists section header for 64-bit DWARF format. */
147 #define LOCLIST_HEADER_SIZE64 20
148
149 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
150 #define RNGLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
153 #define RNGLIST_HEADER_SIZE64 20
154
155 /* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168 struct name_component
169 {
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178 };
179
180 /* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183 struct mapped_index_base
184 {
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
211 void build_name_components (dwarf2_per_objfile *per_objfile);
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
218 find_name_components_bounds (const lookup_name_info &ln_no_params,
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
221
222 /* Prevent deleting/destroying via a base class pointer. */
223 protected:
224 ~mapped_index_base() = default;
225 };
226
227 /* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231 class offset_view
232 {
233 public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262 private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265 };
266
267 /* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
269 struct mapped_index final : public mapped_index_base
270 {
271 /* Index data format version. */
272 int version = 0;
273
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
276
277 /* The symbol table, implemented as a hash table. */
278 offset_view symbol_table;
279
280 /* A pointer to the constant pool. */
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
296
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
301 }
302
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size () / 2; }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
363 };
364
365 /* See dwarf2read.h. */
366
367 dwarf2_per_objfile *
368 get_dwarf2_per_objfile (struct objfile *objfile)
369 {
370 return dwarf2_objfile_data_key.get (objfile);
371 }
372
373 /* Default names of the debugging sections. */
374
375 /* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
378 const struct dwarf2_debug_sections dwarf2_elf_names =
379 {
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
384 { ".debug_loclists", ".zdebug_loclists" },
385 { ".debug_macinfo", ".zdebug_macinfo" },
386 { ".debug_macro", ".zdebug_macro" },
387 { ".debug_str", ".zdebug_str" },
388 { ".debug_str_offsets", ".zdebug_str_offsets" },
389 { ".debug_line_str", ".zdebug_line_str" },
390 { ".debug_ranges", ".zdebug_ranges" },
391 { ".debug_rnglists", ".zdebug_rnglists" },
392 { ".debug_types", ".zdebug_types" },
393 { ".debug_addr", ".zdebug_addr" },
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
396 { ".gdb_index", ".zgdb_index" },
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
399 23
400 };
401
402 /* List of DWO/DWP sections. */
403
404 static const struct dwop_section_names
405 {
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
410 struct dwarf2_section_names loclists_dwo;
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
413 struct dwarf2_section_names rnglists_dwo;
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
419 }
420 dwop_section_names =
421 {
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
435 };
436
437 /* local data types */
438
439 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441 struct loclists_rnglists_header
442 {
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461 };
462
463 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466 struct stmt_list_hash
467 {
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
472 sect_offset line_sect_off;
473 };
474
475 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
479
480 struct type_unit_group : public dwarf2_per_cu_data
481 {
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
485 std::vector<signatured_type *> *tus = nullptr;
486
487 /* The data used to construct the hash key. */
488 struct stmt_list_hash hash {};
489 };
490
491 /* These sections are what may appear in a (real or virtual) DWO file. */
492
493 struct dwo_sections
494 {
495 struct dwarf2_section_info abbrev;
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
498 struct dwarf2_section_info loclists;
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
501 struct dwarf2_section_info rnglists;
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
506 std::vector<dwarf2_section_info> types;
507 };
508
509 /* CUs/TUs in DWP/DWO files. */
510
511 struct dwo_unit
512 {
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
522 struct dwarf2_section_info *section;
523
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530 };
531
532 /* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536 enum dwp_v2_section_ids
537 {
538 DW_SECT_MIN = 1
539 };
540
541 /* Data for one DWO file.
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
551
552 struct dwo_file
553 {
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
561 const char *dwo_name = nullptr;
562
563 /* The DW_AT_comp_dir attribute. */
564 const char *comp_dir = nullptr;
565
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
568 gdb_bfd_ref_ptr dbfd;
569
570 /* The sections that make up this DWO file.
571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
572 sections (for lack of a better name). */
573 struct dwo_sections sections {};
574
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
579 htab_up cus;
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
583 htab_up tus;
584 };
585
586 /* These sections are what may appear in a DWP file. */
587
588 struct dwp_sections
589 {
590 /* These are used by all DWP versions (1, 2 and 5). */
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
594
595 /* These are only used by DWP version 2 and version 5 files.
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
607 struct dwarf2_section_info loclists;
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
610 struct dwarf2_section_info rnglists;
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
613 };
614
615 /* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
617
618 struct virtual_v1_dwo_sections
619 {
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
627 That is recorded here, and copied to dwo_unit.section. */
628 struct dwarf2_section_info info_or_types;
629 };
630
631 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
636 struct virtual_v2_or_v5_dwo_sections
637 {
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666 };
667
668 /* Contents of DWP hash tables. */
669
670 struct dwp_hash_table
671 {
672 uint32_t version, nr_columns;
673 uint32_t nr_units, nr_slots;
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685 #define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700 #define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
712 } section_pool;
713 };
714
715 /* Data for one DWP file. */
716
717 struct dwp_file
718 {
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
725 /* Name of the file. */
726 const char *name;
727
728 /* File format version. */
729 int version = 0;
730
731 /* The bfd. */
732 gdb_bfd_ref_ptr dbfd;
733
734 /* Section info for this file. */
735 struct dwp_sections sections {};
736
737 /* Table of CUs in the file. */
738 const struct dwp_hash_table *cus = nullptr;
739
740 /* Table of TUs in the file. */
741 const struct dwp_hash_table *tus = nullptr;
742
743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
744 htab_up loaded_cus;
745 htab_up loaded_tus;
746
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
751 };
752
753 /* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
756 struct exists to abstract away the constant parameters of die reading. */
757
758 struct die_reader_specs
759 {
760 /* The bfd of die_section. */
761 bfd *abfd;
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
767 struct dwo_file *dwo_file;
768
769 /* The section the die comes from.
770 This is either .debug_info or .debug_types, or the .dwo variants. */
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
774 const gdb_byte *buffer;
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
778
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
781 };
782
783 /* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786 class cutu_reader : public die_reader_specs
787 {
788 public:
789
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
792 struct abbrev_table *abbrev_table,
793 dwarf2_cu *existing_cu,
794 bool skip_partial);
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 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
805 bool dummy_p = false;
806
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
811 private:
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
814 dwarf2_cu *existing_cu);
815
816 struct dwarf2_per_cu_data *m_this_cu;
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824 };
825
826 /* When we construct a partial symbol table entry we only
827 need this much information. */
828 struct partial_die_info : public allocate_on_obstack
829 {
830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
835 partial_die_info (const partial_die_info &) = default;
836
837 /* Adjust the partial die before generating a symbol for it. This
838 function may set the is_external flag or change the DIE's
839 name. */
840 void fixup (struct dwarf2_cu *cu);
841
842 /* Read a minimal amount of information into the minimal die
843 structure. */
844 const gdb_byte *read (const struct die_reader_specs *reader,
845 const struct abbrev_info &abbrev,
846 const gdb_byte *info_ptr);
847
848 /* Compute the name of this partial DIE. This memoizes the
849 result, so it is safe to call multiple times. */
850 const char *name (dwarf2_cu *cu);
851
852 /* Offset of this DIE. */
853 const sect_offset sect_off;
854
855 /* DWARF-2 tag for this DIE. */
856 const ENUM_BITFIELD(dwarf_tag) tag : 16;
857
858 /* Assorted flags describing the data found in this DIE. */
859 const unsigned int has_children : 1;
860
861 unsigned int is_external : 1;
862 unsigned int is_declaration : 1;
863 unsigned int has_type : 1;
864 unsigned int has_specification : 1;
865 unsigned int has_pc_info : 1;
866 unsigned int has_range_info : 1;
867 unsigned int may_be_inlined : 1;
868
869 /* This DIE has been marked DW_AT_main_subprogram. */
870 unsigned int main_subprogram : 1;
871
872 /* Flag set if the SCOPE field of this structure has been
873 computed. */
874 unsigned int scope_set : 1;
875
876 /* Flag set if the DIE has a byte_size attribute. */
877 unsigned int has_byte_size : 1;
878
879 /* Flag set if the DIE has a DW_AT_const_value attribute. */
880 unsigned int has_const_value : 1;
881
882 /* Flag set if any of the DIE's children are template arguments. */
883 unsigned int has_template_arguments : 1;
884
885 /* Flag set if fixup has been called on this die. */
886 unsigned int fixup_called : 1;
887
888 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
889 unsigned int is_dwz : 1;
890
891 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
892 unsigned int spec_is_dwz : 1;
893
894 unsigned int canonical_name : 1;
895
896 /* The name of this DIE. Normally the value of DW_AT_name, but
897 sometimes a default name for unnamed DIEs. */
898 const char *raw_name = nullptr;
899
900 /* The linkage name, if present. */
901 const char *linkage_name = nullptr;
902
903 /* The scope to prepend to our children. This is generally
904 allocated on the comp_unit_obstack, so will disappear
905 when this compilation unit leaves the cache. */
906 const char *scope = nullptr;
907
908 /* Some data associated with the partial DIE. The tag determines
909 which field is live. */
910 union
911 {
912 /* The location description associated with this DIE, if any. */
913 struct dwarf_block *locdesc;
914 /* The offset of an import, for DW_TAG_imported_unit. */
915 sect_offset sect_off;
916 } d {};
917
918 union
919 {
920 /* If HAS_PC_INFO, the PC range associated with this DIE. */
921 struct
922 {
923 CORE_ADDR lowpc;
924 CORE_ADDR highpc;
925 };
926 /* If HAS_RANGE_INFO, the ranges offset associated with this DIE. */
927 ULONGEST ranges_offset;
928 };
929
930 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
931 DW_AT_sibling, if any. */
932 /* NOTE: This member isn't strictly necessary, partial_die_info::read
933 could return DW_AT_sibling values to its caller load_partial_dies. */
934 const gdb_byte *sibling = nullptr;
935
936 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
937 DW_AT_specification (or DW_AT_abstract_origin or
938 DW_AT_extension). */
939 sect_offset spec_offset {};
940
941 /* Pointers to this DIE's parent, first child, and next sibling,
942 if any. */
943 struct partial_die_info *die_parent = nullptr;
944 struct partial_die_info *die_child = nullptr;
945 struct partial_die_info *die_sibling = nullptr;
946
947 friend struct partial_die_info *
948 dwarf2_cu::find_partial_die (sect_offset sect_off);
949
950 private:
951 /* Only need to do look up in dwarf2_cu::find_partial_die. */
952 partial_die_info (sect_offset sect_off)
953 : partial_die_info (sect_off, DW_TAG_padding, 0)
954 {
955 }
956
957 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
958 int has_children_)
959 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
960 {
961 is_external = 0;
962 is_declaration = 0;
963 has_type = 0;
964 has_specification = 0;
965 has_pc_info = 0;
966 has_range_info = 0;
967 may_be_inlined = 0;
968 main_subprogram = 0;
969 scope_set = 0;
970 has_byte_size = 0;
971 has_const_value = 0;
972 has_template_arguments = 0;
973 fixup_called = 0;
974 is_dwz = 0;
975 spec_is_dwz = 0;
976 canonical_name = 0;
977 /* Don't set these using NSDMI (Non-static data member initialisation),
978 because g++-4.8 will error out. */
979 lowpc = 0;
980 highpc = 0;
981 }
982 };
983
984 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
985 but this would require a corresponding change in unpack_field_as_long
986 and friends. */
987 static int bits_per_byte = 8;
988
989 struct variant_part_builder;
990
991 /* When reading a variant, we track a bit more information about the
992 field, and store it in an object of this type. */
993
994 struct variant_field
995 {
996 int first_field = -1;
997 int last_field = -1;
998
999 /* A variant can contain other variant parts. */
1000 std::vector<variant_part_builder> variant_parts;
1001
1002 /* If we see a DW_TAG_variant, then this will be set if this is the
1003 default branch. */
1004 bool default_branch = false;
1005 /* If we see a DW_AT_discr_value, then this will be the discriminant
1006 value. */
1007 ULONGEST discriminant_value = 0;
1008 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1009 data. */
1010 struct dwarf_block *discr_list_data = nullptr;
1011 };
1012
1013 /* This represents a DW_TAG_variant_part. */
1014
1015 struct variant_part_builder
1016 {
1017 /* The offset of the discriminant field. */
1018 sect_offset discriminant_offset {};
1019
1020 /* Variants that are direct children of this variant part. */
1021 std::vector<variant_field> variants;
1022
1023 /* True if we're currently reading a variant. */
1024 bool processing_variant = false;
1025 };
1026
1027 struct nextfield
1028 {
1029 int accessibility = 0;
1030 int virtuality = 0;
1031 /* Variant parts need to find the discriminant, which is a DIE
1032 reference. We track the section offset of each field to make
1033 this link. */
1034 sect_offset offset;
1035 struct field field {};
1036 };
1037
1038 struct fnfieldlist
1039 {
1040 const char *name = nullptr;
1041 std::vector<struct fn_field> fnfields;
1042 };
1043
1044 /* The routines that read and process dies for a C struct or C++ class
1045 pass lists of data member fields and lists of member function fields
1046 in an instance of a field_info structure, as defined below. */
1047 struct field_info
1048 {
1049 /* List of data member and baseclasses fields. */
1050 std::vector<struct nextfield> fields;
1051 std::vector<struct nextfield> baseclasses;
1052
1053 /* Set if the accessibility of one of the fields is not public. */
1054 bool non_public_fields = false;
1055
1056 /* Member function fieldlist array, contains name of possibly overloaded
1057 member function, number of overloaded member functions and a pointer
1058 to the head of the member function field chain. */
1059 std::vector<struct fnfieldlist> fnfieldlists;
1060
1061 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1062 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1063 std::vector<struct decl_field> typedef_field_list;
1064
1065 /* Nested types defined by this class and the number of elements in this
1066 list. */
1067 std::vector<struct decl_field> nested_types_list;
1068
1069 /* If non-null, this is the variant part we are currently
1070 reading. */
1071 variant_part_builder *current_variant_part = nullptr;
1072 /* This holds all the top-level variant parts attached to the type
1073 we're reading. */
1074 std::vector<variant_part_builder> variant_parts;
1075
1076 /* Return the total number of fields (including baseclasses). */
1077 int nfields () const
1078 {
1079 return fields.size () + baseclasses.size ();
1080 }
1081 };
1082
1083 /* Loaded secondary compilation units are kept in memory until they
1084 have not been referenced for the processing of this many
1085 compilation units. Set this to zero to disable caching. Cache
1086 sizes of up to at least twenty will improve startup time for
1087 typical inter-CU-reference binaries, at an obvious memory cost. */
1088 static int dwarf_max_cache_age = 5;
1089 static void
1090 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1091 struct cmd_list_element *c, const char *value)
1092 {
1093 fprintf_filtered (file, _("The upper bound on the age of cached "
1094 "DWARF compilation units is %s.\n"),
1095 value);
1096 }
1097 \f
1098 /* local function prototypes */
1099
1100 static void dwarf2_find_base_address (struct die_info *die,
1101 struct dwarf2_cu *cu);
1102
1103 static dwarf2_psymtab *create_partial_symtab
1104 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1105 const char *name);
1106
1107 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1108 const gdb_byte *info_ptr,
1109 struct die_info *type_unit_die);
1110
1111 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1112
1113 static void scan_partial_symbols (struct partial_die_info *,
1114 CORE_ADDR *, CORE_ADDR *,
1115 int, struct dwarf2_cu *);
1116
1117 static void add_partial_symbol (struct partial_die_info *,
1118 struct dwarf2_cu *);
1119
1120 static void add_partial_namespace (struct partial_die_info *pdi,
1121 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1122 int set_addrmap, struct dwarf2_cu *cu);
1123
1124 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1125 CORE_ADDR *highpc, int set_addrmap,
1126 struct dwarf2_cu *cu);
1127
1128 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1129 struct dwarf2_cu *cu);
1130
1131 static void add_partial_subprogram (struct partial_die_info *pdi,
1132 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1133 int need_pc, struct dwarf2_cu *cu);
1134
1135 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1136
1137 static struct partial_die_info *load_partial_dies
1138 (const struct die_reader_specs *, const gdb_byte *, int);
1139
1140 /* A pair of partial_die_info and compilation unit. */
1141 struct cu_partial_die_info
1142 {
1143 /* The compilation unit of the partial_die_info. */
1144 struct dwarf2_cu *cu;
1145 /* A partial_die_info. */
1146 struct partial_die_info *pdi;
1147
1148 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1149 : cu (cu),
1150 pdi (pdi)
1151 { /* Nothing. */ }
1152
1153 private:
1154 cu_partial_die_info () = delete;
1155 };
1156
1157 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1158 struct dwarf2_cu *);
1159
1160 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1161 struct attribute *,
1162 const struct attr_abbrev *,
1163 const gdb_byte *);
1164
1165 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1166 struct attribute *attr, dwarf_tag tag);
1167
1168 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1169
1170 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1171 dwarf2_section_info *, sect_offset);
1172
1173 static const char *read_indirect_string
1174 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1175 const struct comp_unit_head *, unsigned int *);
1176
1177 static const char *read_indirect_string_at_offset
1178 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1179
1180 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1181 const gdb_byte *,
1182 unsigned int *);
1183
1184 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1185 ULONGEST str_index);
1186
1187 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1188 ULONGEST str_index);
1189
1190 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1191 struct dwarf2_cu *);
1192
1193 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1194 struct dwarf2_cu *cu);
1195
1196 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1197
1198 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1199 struct dwarf2_cu *cu);
1200
1201 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1202
1203 static struct die_info *die_specification (struct die_info *die,
1204 struct dwarf2_cu **);
1205
1206 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1207 struct dwarf2_cu *cu);
1208
1209 static void dwarf_decode_lines (struct line_header *,
1210 const file_and_directory &,
1211 struct dwarf2_cu *, dwarf2_psymtab *,
1212 CORE_ADDR, int decode_mapping);
1213
1214 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1215 const char *);
1216
1217 static struct symbol *new_symbol (struct die_info *, struct type *,
1218 struct dwarf2_cu *, struct symbol * = NULL);
1219
1220 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1221 struct dwarf2_cu *);
1222
1223 static void dwarf2_const_value_attr (const struct attribute *attr,
1224 struct type *type,
1225 const char *name,
1226 struct obstack *obstack,
1227 struct dwarf2_cu *cu, LONGEST *value,
1228 const gdb_byte **bytes,
1229 struct dwarf2_locexpr_baton **baton);
1230
1231 static struct type *read_subrange_index_type (struct die_info *die,
1232 struct dwarf2_cu *cu);
1233
1234 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1235
1236 static int need_gnat_info (struct dwarf2_cu *);
1237
1238 static struct type *die_descriptive_type (struct die_info *,
1239 struct dwarf2_cu *);
1240
1241 static void set_descriptive_type (struct type *, struct die_info *,
1242 struct dwarf2_cu *);
1243
1244 static struct type *die_containing_type (struct die_info *,
1245 struct dwarf2_cu *);
1246
1247 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1248 struct dwarf2_cu *);
1249
1250 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1251
1252 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1253
1254 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1255
1256 static char *typename_concat (struct obstack *obs, const char *prefix,
1257 const char *suffix, int physname,
1258 struct dwarf2_cu *cu);
1259
1260 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1261
1262 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1263
1264 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1265
1266 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1267
1268 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1269
1270 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1271
1272 /* Return the .debug_loclists section to use for cu. */
1273 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1274
1275 /* Return the .debug_rnglists section to use for cu. */
1276 static struct dwarf2_section_info *cu_debug_rnglists_section
1277 (struct dwarf2_cu *cu, dwarf_tag tag);
1278
1279 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1280 values. Keep the items ordered with increasing constraints compliance. */
1281 enum pc_bounds_kind
1282 {
1283 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1284 PC_BOUNDS_NOT_PRESENT,
1285
1286 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1287 were present but they do not form a valid range of PC addresses. */
1288 PC_BOUNDS_INVALID,
1289
1290 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1291 PC_BOUNDS_RANGES,
1292
1293 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1294 PC_BOUNDS_HIGH_LOW,
1295 };
1296
1297 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1298 CORE_ADDR *, CORE_ADDR *,
1299 struct dwarf2_cu *,
1300 dwarf2_psymtab *);
1301
1302 static void get_scope_pc_bounds (struct die_info *,
1303 CORE_ADDR *, CORE_ADDR *,
1304 struct dwarf2_cu *);
1305
1306 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1307 CORE_ADDR, struct dwarf2_cu *);
1308
1309 static void dwarf2_add_field (struct field_info *, struct die_info *,
1310 struct dwarf2_cu *);
1311
1312 static void dwarf2_attach_fields_to_type (struct field_info *,
1313 struct type *, struct dwarf2_cu *);
1314
1315 static void dwarf2_add_member_fn (struct field_info *,
1316 struct die_info *, struct type *,
1317 struct dwarf2_cu *);
1318
1319 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1320 struct type *,
1321 struct dwarf2_cu *);
1322
1323 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1324
1325 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1326
1327 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1328
1329 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1330
1331 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1332
1333 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1334
1335 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1336
1337 static struct type *read_module_type (struct die_info *die,
1338 struct dwarf2_cu *cu);
1339
1340 static const char *namespace_name (struct die_info *die,
1341 int *is_anonymous, struct dwarf2_cu *);
1342
1343 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1344
1345 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1346 bool * = nullptr);
1347
1348 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1349 struct dwarf2_cu *);
1350
1351 static struct die_info *read_die_and_siblings_1
1352 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1353 struct die_info *);
1354
1355 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1356 const gdb_byte *info_ptr,
1357 const gdb_byte **new_info_ptr,
1358 struct die_info *parent);
1359
1360 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1361 struct die_info **, const gdb_byte *,
1362 int);
1363
1364 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1365 struct die_info **, const gdb_byte *);
1366
1367 static void process_die (struct die_info *, struct dwarf2_cu *);
1368
1369 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1370 struct objfile *);
1371
1372 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1373
1374 static const char *dwarf2_full_name (const char *name,
1375 struct die_info *die,
1376 struct dwarf2_cu *cu);
1377
1378 static const char *dwarf2_physname (const char *name, struct die_info *die,
1379 struct dwarf2_cu *cu);
1380
1381 static struct die_info *dwarf2_extension (struct die_info *die,
1382 struct dwarf2_cu **);
1383
1384 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1385
1386 static void dump_die_for_error (struct die_info *);
1387
1388 static void dump_die_1 (struct ui_file *, int level, int max_level,
1389 struct die_info *);
1390
1391 /*static*/ void dump_die (struct die_info *, int max_level);
1392
1393 static void store_in_ref_table (struct die_info *,
1394 struct dwarf2_cu *);
1395
1396 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1397 const struct attribute *,
1398 struct dwarf2_cu **);
1399
1400 static struct die_info *follow_die_ref (struct die_info *,
1401 const struct attribute *,
1402 struct dwarf2_cu **);
1403
1404 static struct die_info *follow_die_sig (struct die_info *,
1405 const struct attribute *,
1406 struct dwarf2_cu **);
1407
1408 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1409 struct dwarf2_cu *);
1410
1411 static struct type *get_DW_AT_signature_type (struct die_info *,
1412 const struct attribute *,
1413 struct dwarf2_cu *);
1414
1415 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1416 dwarf2_per_objfile *per_objfile);
1417
1418 static void read_signatured_type (signatured_type *sig_type,
1419 dwarf2_per_objfile *per_objfile);
1420
1421 static int attr_to_dynamic_prop (const struct attribute *attr,
1422 struct die_info *die, struct dwarf2_cu *cu,
1423 struct dynamic_prop *prop, struct type *type);
1424
1425 /* memory allocation interface */
1426
1427 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1428
1429 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1430
1431 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1432
1433 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1434 struct dwarf2_loclist_baton *baton,
1435 const struct attribute *attr);
1436
1437 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1438 struct symbol *sym,
1439 struct dwarf2_cu *cu,
1440 int is_block);
1441
1442 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1443 const gdb_byte *info_ptr,
1444 const struct abbrev_info *abbrev);
1445
1446 static hashval_t partial_die_hash (const void *item);
1447
1448 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1449
1450 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1451 (sect_offset sect_off, unsigned int offset_in_dwz,
1452 dwarf2_per_bfd *per_bfd);
1453
1454 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1455 struct die_info *comp_unit_die,
1456 enum language pretend_language);
1457
1458 static struct type *set_die_type (struct die_info *, struct type *,
1459 struct dwarf2_cu *, bool = false);
1460
1461 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1462
1463 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1464 dwarf2_per_objfile *per_objfile,
1465 dwarf2_cu *existing_cu,
1466 bool skip_partial,
1467 enum language pretend_language);
1468
1469 static void process_full_comp_unit (dwarf2_cu *cu,
1470 enum language pretend_language);
1471
1472 static void process_full_type_unit (dwarf2_cu *cu,
1473 enum language pretend_language);
1474
1475 static struct type *get_die_type_at_offset (sect_offset,
1476 dwarf2_per_cu_data *per_cu,
1477 dwarf2_per_objfile *per_objfile);
1478
1479 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1480
1481 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1482 dwarf2_per_objfile *per_objfile,
1483 enum language pretend_language);
1484
1485 static void process_queue (dwarf2_per_objfile *per_objfile);
1486
1487 /* Class, the destructor of which frees all allocated queue entries. This
1488 will only have work to do if an error was thrown while processing the
1489 dwarf. If no error was thrown then the queue entries should have all
1490 been processed, and freed, as we went along. */
1491
1492 class dwarf2_queue_guard
1493 {
1494 public:
1495 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1496 : m_per_objfile (per_objfile)
1497 {
1498 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1499
1500 m_per_objfile->per_bfd->queue.emplace ();
1501 }
1502
1503 /* Free any entries remaining on the queue. There should only be
1504 entries left if we hit an error while processing the dwarf. */
1505 ~dwarf2_queue_guard ()
1506 {
1507 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1508
1509 m_per_objfile->per_bfd->queue.reset ();
1510 }
1511
1512 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1513
1514 private:
1515 dwarf2_per_objfile *m_per_objfile;
1516 };
1517
1518 dwarf2_queue_item::~dwarf2_queue_item ()
1519 {
1520 /* Anything still marked queued is likely to be in an
1521 inconsistent state, so discard it. */
1522 if (per_cu->queued)
1523 {
1524 per_objfile->remove_cu (per_cu);
1525 per_cu->queued = 0;
1526 }
1527 }
1528
1529 /* See dwarf2/read.h. */
1530
1531 void
1532 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1533 {
1534 if (data->is_debug_types)
1535 delete static_cast<signatured_type *> (data);
1536 else
1537 delete data;
1538 }
1539
1540 static file_and_directory &find_file_and_directory
1541 (struct die_info *die, struct dwarf2_cu *cu);
1542
1543 static const char *compute_include_file_name
1544 (const struct line_header *lh,
1545 const file_entry &fe,
1546 const file_and_directory &cu_info,
1547 gdb::unique_xmalloc_ptr<char> *name_holder);
1548
1549 static htab_up allocate_signatured_type_table ();
1550
1551 static htab_up allocate_dwo_unit_table ();
1552
1553 static struct dwo_unit *lookup_dwo_unit_in_dwp
1554 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1555 const char *comp_dir, ULONGEST signature, int is_debug_types);
1556
1557 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1558
1559 static struct dwo_unit *lookup_dwo_comp_unit
1560 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1561 ULONGEST signature);
1562
1563 static struct dwo_unit *lookup_dwo_type_unit
1564 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1565
1566 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1567
1568 /* A unique pointer to a dwo_file. */
1569
1570 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1571
1572 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1573
1574 static void check_producer (struct dwarf2_cu *cu);
1575 \f
1576 /* Various complaints about symbol reading that don't abort the process. */
1577
1578 static void
1579 dwarf2_debug_line_missing_file_complaint (void)
1580 {
1581 complaint (_(".debug_line section has line data without a file"));
1582 }
1583
1584 static void
1585 dwarf2_debug_line_missing_end_sequence_complaint (void)
1586 {
1587 complaint (_(".debug_line section has line "
1588 "program sequence without an end"));
1589 }
1590
1591 static void
1592 dwarf2_complex_location_expr_complaint (void)
1593 {
1594 complaint (_("location expression too complex"));
1595 }
1596
1597 static void
1598 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1599 int arg3)
1600 {
1601 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1602 arg1, arg2, arg3);
1603 }
1604
1605 static void
1606 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1607 {
1608 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1609 arg1, arg2);
1610 }
1611
1612 /* Hash function for line_header_hash. */
1613
1614 static hashval_t
1615 line_header_hash (const struct line_header *ofs)
1616 {
1617 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1618 }
1619
1620 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1621
1622 static hashval_t
1623 line_header_hash_voidp (const void *item)
1624 {
1625 const struct line_header *ofs = (const struct line_header *) item;
1626
1627 return line_header_hash (ofs);
1628 }
1629
1630 /* Equality function for line_header_hash. */
1631
1632 static int
1633 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1634 {
1635 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1636 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1637
1638 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1639 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1640 }
1641
1642 \f
1643
1644 /* An iterator for all_comp_units that is based on index. This
1645 approach makes it possible to iterate over all_comp_units safely,
1646 when some caller in the loop may add new units. */
1647
1648 class all_comp_units_iterator
1649 {
1650 public:
1651
1652 all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1653 : m_per_bfd (per_bfd),
1654 m_index (start ? 0 : per_bfd->all_comp_units.size ())
1655 {
1656 }
1657
1658 all_comp_units_iterator &operator++ ()
1659 {
1660 ++m_index;
1661 return *this;
1662 }
1663
1664 dwarf2_per_cu_data *operator* () const
1665 {
1666 return m_per_bfd->get_cu (m_index);
1667 }
1668
1669 bool operator== (const all_comp_units_iterator &other) const
1670 {
1671 return m_index == other.m_index;
1672 }
1673
1674
1675 bool operator!= (const all_comp_units_iterator &other) const
1676 {
1677 return m_index != other.m_index;
1678 }
1679
1680 private:
1681
1682 dwarf2_per_bfd *m_per_bfd;
1683 size_t m_index;
1684 };
1685
1686 /* A range adapter for the all_comp_units_iterator. */
1687 class all_comp_units_range
1688 {
1689 public:
1690
1691 all_comp_units_range (dwarf2_per_bfd *per_bfd)
1692 : m_per_bfd (per_bfd)
1693 {
1694 }
1695
1696 all_comp_units_iterator begin ()
1697 {
1698 return all_comp_units_iterator (m_per_bfd, true);
1699 }
1700
1701 all_comp_units_iterator end ()
1702 {
1703 return all_comp_units_iterator (m_per_bfd, false);
1704 }
1705
1706 private:
1707
1708 dwarf2_per_bfd *m_per_bfd;
1709 };
1710
1711 /* See declaration. */
1712
1713 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1714 bool can_copy_)
1715 : obfd (obfd),
1716 can_copy (can_copy_)
1717 {
1718 if (names == NULL)
1719 names = &dwarf2_elf_names;
1720
1721 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1722 locate_sections (obfd, sec, *names);
1723 }
1724
1725 dwarf2_per_bfd::~dwarf2_per_bfd ()
1726 {
1727 for (auto &per_cu : all_comp_units)
1728 {
1729 per_cu->imported_symtabs_free ();
1730 per_cu->free_cached_file_names ();
1731 }
1732
1733 /* Everything else should be on this->obstack. */
1734 }
1735
1736 /* See read.h. */
1737
1738 void
1739 dwarf2_per_objfile::remove_all_cus ()
1740 {
1741 gdb_assert (!this->per_bfd->queue.has_value ());
1742
1743 for (auto pair : m_dwarf2_cus)
1744 delete pair.second;
1745
1746 m_dwarf2_cus.clear ();
1747 }
1748
1749 /* A helper class that calls free_cached_comp_units on
1750 destruction. */
1751
1752 class free_cached_comp_units
1753 {
1754 public:
1755
1756 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1757 : m_per_objfile (per_objfile)
1758 {
1759 }
1760
1761 ~free_cached_comp_units ()
1762 {
1763 m_per_objfile->remove_all_cus ();
1764 }
1765
1766 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1767
1768 private:
1769
1770 dwarf2_per_objfile *m_per_objfile;
1771 };
1772
1773 /* See read.h. */
1774
1775 bool
1776 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1777 {
1778 if (per_cu->index < this->m_symtabs.size ())
1779 return this->m_symtabs[per_cu->index] != nullptr;
1780 return false;
1781 }
1782
1783 /* See read.h. */
1784
1785 compunit_symtab *
1786 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1787 {
1788 if (per_cu->index < this->m_symtabs.size ())
1789 return this->m_symtabs[per_cu->index];
1790 return nullptr;
1791 }
1792
1793 /* See read.h. */
1794
1795 void
1796 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1797 compunit_symtab *symtab)
1798 {
1799 if (per_cu->index >= this->m_symtabs.size ())
1800 this->m_symtabs.resize (per_cu->index + 1);
1801 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1802 this->m_symtabs[per_cu->index] = symtab;
1803 }
1804
1805 /* Try to locate the sections we need for DWARF 2 debugging
1806 information and return true if we have enough to do something.
1807 NAMES points to the dwarf2 section names, or is NULL if the standard
1808 ELF names are used. CAN_COPY is true for formats where symbol
1809 interposition is possible and so symbol values must follow copy
1810 relocation rules. */
1811
1812 int
1813 dwarf2_has_info (struct objfile *objfile,
1814 const struct dwarf2_debug_sections *names,
1815 bool can_copy)
1816 {
1817 if (objfile->flags & OBJF_READNEVER)
1818 return 0;
1819
1820 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1821
1822 if (per_objfile == NULL)
1823 {
1824 dwarf2_per_bfd *per_bfd;
1825
1826 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1827 BFD doesn't require relocations.
1828
1829 We don't share with objfiles for which -readnow was requested,
1830 because it would complicate things when loading the same BFD with
1831 -readnow and then without -readnow. */
1832 if (!gdb_bfd_requires_relocations (objfile->obfd)
1833 && (objfile->flags & OBJF_READNOW) == 0)
1834 {
1835 /* See if one has been created for this BFD yet. */
1836 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1837
1838 if (per_bfd == nullptr)
1839 {
1840 /* No, create it now. */
1841 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1842 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1843 }
1844 }
1845 else
1846 {
1847 /* No sharing possible, create one specifically for this objfile. */
1848 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1849 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1850 }
1851
1852 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1853 }
1854
1855 return (!per_objfile->per_bfd->info.is_virtual
1856 && per_objfile->per_bfd->info.s.section != NULL
1857 && !per_objfile->per_bfd->abbrev.is_virtual
1858 && per_objfile->per_bfd->abbrev.s.section != NULL);
1859 }
1860
1861 /* See declaration. */
1862
1863 void
1864 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1865 const dwarf2_debug_sections &names)
1866 {
1867 flagword aflag = bfd_section_flags (sectp);
1868
1869 if ((aflag & SEC_HAS_CONTENTS) == 0)
1870 {
1871 }
1872 else if (elf_section_data (sectp)->this_hdr.sh_size
1873 > bfd_get_file_size (abfd))
1874 {
1875 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1876 warning (_("Discarding section %s which has a section size (%s"
1877 ") larger than the file size [in module %s]"),
1878 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1879 bfd_get_filename (abfd));
1880 }
1881 else if (names.info.matches (sectp->name))
1882 {
1883 this->info.s.section = sectp;
1884 this->info.size = bfd_section_size (sectp);
1885 }
1886 else if (names.abbrev.matches (sectp->name))
1887 {
1888 this->abbrev.s.section = sectp;
1889 this->abbrev.size = bfd_section_size (sectp);
1890 }
1891 else if (names.line.matches (sectp->name))
1892 {
1893 this->line.s.section = sectp;
1894 this->line.size = bfd_section_size (sectp);
1895 }
1896 else if (names.loc.matches (sectp->name))
1897 {
1898 this->loc.s.section = sectp;
1899 this->loc.size = bfd_section_size (sectp);
1900 }
1901 else if (names.loclists.matches (sectp->name))
1902 {
1903 this->loclists.s.section = sectp;
1904 this->loclists.size = bfd_section_size (sectp);
1905 }
1906 else if (names.macinfo.matches (sectp->name))
1907 {
1908 this->macinfo.s.section = sectp;
1909 this->macinfo.size = bfd_section_size (sectp);
1910 }
1911 else if (names.macro.matches (sectp->name))
1912 {
1913 this->macro.s.section = sectp;
1914 this->macro.size = bfd_section_size (sectp);
1915 }
1916 else if (names.str.matches (sectp->name))
1917 {
1918 this->str.s.section = sectp;
1919 this->str.size = bfd_section_size (sectp);
1920 }
1921 else if (names.str_offsets.matches (sectp->name))
1922 {
1923 this->str_offsets.s.section = sectp;
1924 this->str_offsets.size = bfd_section_size (sectp);
1925 }
1926 else if (names.line_str.matches (sectp->name))
1927 {
1928 this->line_str.s.section = sectp;
1929 this->line_str.size = bfd_section_size (sectp);
1930 }
1931 else if (names.addr.matches (sectp->name))
1932 {
1933 this->addr.s.section = sectp;
1934 this->addr.size = bfd_section_size (sectp);
1935 }
1936 else if (names.frame.matches (sectp->name))
1937 {
1938 this->frame.s.section = sectp;
1939 this->frame.size = bfd_section_size (sectp);
1940 }
1941 else if (names.eh_frame.matches (sectp->name))
1942 {
1943 this->eh_frame.s.section = sectp;
1944 this->eh_frame.size = bfd_section_size (sectp);
1945 }
1946 else if (names.ranges.matches (sectp->name))
1947 {
1948 this->ranges.s.section = sectp;
1949 this->ranges.size = bfd_section_size (sectp);
1950 }
1951 else if (names.rnglists.matches (sectp->name))
1952 {
1953 this->rnglists.s.section = sectp;
1954 this->rnglists.size = bfd_section_size (sectp);
1955 }
1956 else if (names.types.matches (sectp->name))
1957 {
1958 struct dwarf2_section_info type_section;
1959
1960 memset (&type_section, 0, sizeof (type_section));
1961 type_section.s.section = sectp;
1962 type_section.size = bfd_section_size (sectp);
1963
1964 this->types.push_back (type_section);
1965 }
1966 else if (names.gdb_index.matches (sectp->name))
1967 {
1968 this->gdb_index.s.section = sectp;
1969 this->gdb_index.size = bfd_section_size (sectp);
1970 }
1971 else if (names.debug_names.matches (sectp->name))
1972 {
1973 this->debug_names.s.section = sectp;
1974 this->debug_names.size = bfd_section_size (sectp);
1975 }
1976 else if (names.debug_aranges.matches (sectp->name))
1977 {
1978 this->debug_aranges.s.section = sectp;
1979 this->debug_aranges.size = bfd_section_size (sectp);
1980 }
1981
1982 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1983 && bfd_section_vma (sectp) == 0)
1984 this->has_section_at_zero = true;
1985 }
1986
1987 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1988 SECTION_NAME. */
1989
1990 void
1991 dwarf2_get_section_info (struct objfile *objfile,
1992 enum dwarf2_section_enum sect,
1993 asection **sectp, const gdb_byte **bufp,
1994 bfd_size_type *sizep)
1995 {
1996 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1997 struct dwarf2_section_info *info;
1998
1999 /* We may see an objfile without any DWARF, in which case we just
2000 return nothing. */
2001 if (per_objfile == NULL)
2002 {
2003 *sectp = NULL;
2004 *bufp = NULL;
2005 *sizep = 0;
2006 return;
2007 }
2008 switch (sect)
2009 {
2010 case DWARF2_DEBUG_FRAME:
2011 info = &per_objfile->per_bfd->frame;
2012 break;
2013 case DWARF2_EH_FRAME:
2014 info = &per_objfile->per_bfd->eh_frame;
2015 break;
2016 default:
2017 gdb_assert_not_reached ("unexpected section");
2018 }
2019
2020 info->read (objfile);
2021
2022 *sectp = info->get_bfd_section ();
2023 *bufp = info->buffer;
2024 *sizep = info->size;
2025 }
2026
2027 \f
2028 /* DWARF quick_symbol_functions support. */
2029
2030 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2031 unique line tables, so we maintain a separate table of all .debug_line
2032 derived entries to support the sharing.
2033 All the quick functions need is the list of file names. We discard the
2034 line_header when we're done and don't need to record it here. */
2035 struct quick_file_names
2036 {
2037 /* The data used to construct the hash key. */
2038 struct stmt_list_hash hash;
2039
2040 /* The number of entries in file_names, real_names. */
2041 unsigned int num_file_names;
2042
2043 /* The CU directory, as given by DW_AT_comp_dir. May be
2044 nullptr. */
2045 const char *comp_dir;
2046
2047 /* The file names from the line table, after being run through
2048 file_full_name. */
2049 const char **file_names;
2050
2051 /* The file names from the line table after being run through
2052 gdb_realpath. These are computed lazily. */
2053 const char **real_names;
2054 };
2055
2056 /* When using the index (and thus not using psymtabs), each CU has an
2057 object of this type. This is used to hold information needed by
2058 the various "quick" methods. */
2059 struct dwarf2_per_cu_quick_data
2060 {
2061 /* The file table. This can be NULL if there was no file table
2062 or it's currently not read in.
2063 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2064 struct quick_file_names *file_names;
2065
2066 /* A temporary mark bit used when iterating over all CUs in
2067 expand_symtabs_matching. */
2068 unsigned int mark : 1;
2069
2070 /* True if we've tried to read the file table. There will be no
2071 point in trying to read it again next time. */
2072 bool files_read : 1;
2073 };
2074
2075 /* A subclass of psymbol_functions that arranges to read the DWARF
2076 partial symbols when needed. */
2077 struct lazy_dwarf_reader : public psymbol_functions
2078 {
2079 using psymbol_functions::psymbol_functions;
2080
2081 bool can_lazily_read_symbols () override
2082 {
2083 return true;
2084 }
2085
2086 void read_partial_symbols (struct objfile *objfile) override
2087 {
2088 if (dwarf2_has_info (objfile, nullptr))
2089 dwarf2_build_psymtabs (objfile, this);
2090 }
2091 };
2092
2093 static quick_symbol_functions_up
2094 make_lazy_dwarf_reader ()
2095 {
2096 return quick_symbol_functions_up (new lazy_dwarf_reader);
2097 }
2098
2099 struct dwarf2_base_index_functions : public quick_symbol_functions
2100 {
2101 bool has_symbols (struct objfile *objfile) override;
2102
2103 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2104
2105 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2106
2107 void forget_cached_source_info (struct objfile *objfile) override;
2108
2109 enum language lookup_global_symbol_language (struct objfile *objfile,
2110 const char *name,
2111 domain_enum domain,
2112 bool *symbol_found_p) override
2113 {
2114 *symbol_found_p = false;
2115 return language_unknown;
2116 }
2117
2118 void print_stats (struct objfile *objfile, bool print_bcache) override;
2119
2120 void expand_all_symtabs (struct objfile *objfile) override;
2121
2122 struct compunit_symtab *find_pc_sect_compunit_symtab
2123 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2124 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2125
2126 struct compunit_symtab *find_compunit_symtab_by_address
2127 (struct objfile *objfile, CORE_ADDR address) override
2128 {
2129 return nullptr;
2130 }
2131
2132 void map_symbol_filenames (struct objfile *objfile,
2133 gdb::function_view<symbol_filename_ftype> fun,
2134 bool need_fullname) override;
2135 };
2136
2137 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2138 {
2139 void dump (struct objfile *objfile) override;
2140
2141 void expand_matching_symbols
2142 (struct objfile *,
2143 const lookup_name_info &lookup_name,
2144 domain_enum domain,
2145 int global,
2146 symbol_compare_ftype *ordered_compare) override;
2147
2148 bool expand_symtabs_matching
2149 (struct objfile *objfile,
2150 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2151 const lookup_name_info *lookup_name,
2152 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2153 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2154 block_search_flags search_flags,
2155 domain_enum domain,
2156 enum search_domain kind) override;
2157 };
2158
2159 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2160 {
2161 void dump (struct objfile *objfile) override;
2162
2163 void expand_matching_symbols
2164 (struct objfile *,
2165 const lookup_name_info &lookup_name,
2166 domain_enum domain,
2167 int global,
2168 symbol_compare_ftype *ordered_compare) override;
2169
2170 bool expand_symtabs_matching
2171 (struct objfile *objfile,
2172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2173 const lookup_name_info *lookup_name,
2174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2176 block_search_flags search_flags,
2177 domain_enum domain,
2178 enum search_domain kind) override;
2179 };
2180
2181 static quick_symbol_functions_up
2182 make_dwarf_gdb_index ()
2183 {
2184 return quick_symbol_functions_up (new dwarf2_gdb_index);
2185 }
2186
2187 static quick_symbol_functions_up
2188 make_dwarf_debug_names ()
2189 {
2190 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2191 }
2192
2193 /* Utility hash function for a stmt_list_hash. */
2194
2195 static hashval_t
2196 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2197 {
2198 hashval_t v = 0;
2199
2200 if (stmt_list_hash->dwo_unit != NULL)
2201 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2202 v += to_underlying (stmt_list_hash->line_sect_off);
2203 return v;
2204 }
2205
2206 /* Utility equality function for a stmt_list_hash. */
2207
2208 static int
2209 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2210 const struct stmt_list_hash *rhs)
2211 {
2212 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2213 return 0;
2214 if (lhs->dwo_unit != NULL
2215 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2216 return 0;
2217
2218 return lhs->line_sect_off == rhs->line_sect_off;
2219 }
2220
2221 /* Hash function for a quick_file_names. */
2222
2223 static hashval_t
2224 hash_file_name_entry (const void *e)
2225 {
2226 const struct quick_file_names *file_data
2227 = (const struct quick_file_names *) e;
2228
2229 return hash_stmt_list_entry (&file_data->hash);
2230 }
2231
2232 /* Equality function for a quick_file_names. */
2233
2234 static int
2235 eq_file_name_entry (const void *a, const void *b)
2236 {
2237 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2238 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2239
2240 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2241 }
2242
2243 /* Create a quick_file_names hash table. */
2244
2245 static htab_up
2246 create_quick_file_names_table (unsigned int nr_initial_entries)
2247 {
2248 return htab_up (htab_create_alloc (nr_initial_entries,
2249 hash_file_name_entry, eq_file_name_entry,
2250 nullptr, xcalloc, xfree));
2251 }
2252
2253 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2254 function is unrelated to symtabs, symtab would have to be created afterwards.
2255 You should call age_cached_comp_units after processing the CU. */
2256
2257 static dwarf2_cu *
2258 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2259 bool skip_partial)
2260 {
2261 if (per_cu->is_debug_types)
2262 load_full_type_unit (per_cu, per_objfile);
2263 else
2264 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2265 skip_partial, language_minimal);
2266
2267 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2268 if (cu == nullptr)
2269 return nullptr; /* Dummy CU. */
2270
2271 dwarf2_find_base_address (cu->dies, cu);
2272
2273 return cu;
2274 }
2275
2276 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2277
2278 static void
2279 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2280 dwarf2_per_objfile *per_objfile, bool skip_partial)
2281 {
2282 /* Skip type_unit_groups, reading the type units they contain
2283 is handled elsewhere. */
2284 if (per_cu->type_unit_group_p ())
2285 return;
2286
2287 {
2288 /* The destructor of dwarf2_queue_guard frees any entries left on
2289 the queue. After this point we're guaranteed to leave this function
2290 with the dwarf queue empty. */
2291 dwarf2_queue_guard q_guard (per_objfile);
2292
2293 if (!per_objfile->symtab_set_p (per_cu))
2294 {
2295 queue_comp_unit (per_cu, per_objfile, language_minimal);
2296 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2297
2298 /* If we just loaded a CU from a DWO, and we're working with an index
2299 that may badly handle TUs, load all the TUs in that DWO as well.
2300 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2301 if (!per_cu->is_debug_types
2302 && cu != NULL
2303 && cu->dwo_unit != NULL
2304 && per_objfile->per_bfd->index_table != NULL
2305 && per_objfile->per_bfd->index_table->version <= 7
2306 /* DWP files aren't supported yet. */
2307 && get_dwp_file (per_objfile) == NULL)
2308 queue_and_load_all_dwo_tus (cu);
2309 }
2310
2311 process_queue (per_objfile);
2312 }
2313
2314 /* Age the cache, releasing compilation units that have not
2315 been used recently. */
2316 per_objfile->age_comp_units ();
2317 }
2318
2319 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2320 the per-objfile for which this symtab is instantiated.
2321
2322 Returns the resulting symbol table. */
2323
2324 static struct compunit_symtab *
2325 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2326 dwarf2_per_objfile *per_objfile,
2327 bool skip_partial)
2328 {
2329 gdb_assert (per_objfile->per_bfd->using_index);
2330
2331 if (!per_objfile->symtab_set_p (per_cu))
2332 {
2333 free_cached_comp_units freer (per_objfile);
2334 scoped_restore decrementer = increment_reading_symtab ();
2335 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2336 process_cu_includes (per_objfile);
2337 }
2338
2339 return per_objfile->get_symtab (per_cu);
2340 }
2341
2342 /* See read.h. */
2343
2344 dwarf2_per_cu_data_up
2345 dwarf2_per_bfd::allocate_per_cu ()
2346 {
2347 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2348 result->per_bfd = this;
2349 result->index = all_comp_units.size ();
2350 return result;
2351 }
2352
2353 /* See read.h. */
2354
2355 signatured_type_up
2356 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2357 {
2358 signatured_type_up result (new signatured_type (signature));
2359 result->per_bfd = this;
2360 result->index = all_comp_units.size ();
2361 result->is_debug_types = true;
2362 tu_stats.nr_tus++;
2363 return result;
2364 }
2365
2366 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2367 obstack, and constructed with the specified field values. */
2368
2369 static dwarf2_per_cu_data_up
2370 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2371 struct dwarf2_section_info *section,
2372 int is_dwz,
2373 sect_offset sect_off, ULONGEST length)
2374 {
2375 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2376 the_cu->sect_off = sect_off;
2377 the_cu->length = length;
2378 the_cu->section = section;
2379 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2380 struct dwarf2_per_cu_quick_data);
2381 the_cu->is_dwz = is_dwz;
2382 return the_cu;
2383 }
2384
2385 /* A helper for create_cus_from_index that handles a given list of
2386 CUs. */
2387
2388 static void
2389 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2390 const gdb_byte *cu_list, offset_type n_elements,
2391 struct dwarf2_section_info *section,
2392 int is_dwz)
2393 {
2394 for (offset_type i = 0; i < n_elements; i += 2)
2395 {
2396 gdb_static_assert (sizeof (ULONGEST) >= 8);
2397
2398 sect_offset sect_off
2399 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2400 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2401 cu_list += 2 * 8;
2402
2403 dwarf2_per_cu_data_up per_cu
2404 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2405 length);
2406 per_bfd->all_comp_units.push_back (std::move (per_cu));
2407 }
2408 }
2409
2410 /* Read the CU list from the mapped index, and use it to create all
2411 the CU objects for PER_BFD. */
2412
2413 static void
2414 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2415 const gdb_byte *cu_list, offset_type cu_list_elements,
2416 const gdb_byte *dwz_list, offset_type dwz_elements)
2417 {
2418 gdb_assert (per_bfd->all_comp_units.empty ());
2419 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2420
2421 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2422 &per_bfd->info, 0);
2423
2424 if (dwz_elements == 0)
2425 return;
2426
2427 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2428 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2429 &dwz->info, 1);
2430 }
2431
2432 /* Create the signatured type hash table from the index. */
2433
2434 static void
2435 create_signatured_type_table_from_index
2436 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2437 const gdb_byte *bytes, offset_type elements)
2438 {
2439 htab_up sig_types_hash = allocate_signatured_type_table ();
2440
2441 for (offset_type i = 0; i < elements; i += 3)
2442 {
2443 signatured_type_up sig_type;
2444 ULONGEST signature;
2445 void **slot;
2446 cu_offset type_offset_in_tu;
2447
2448 gdb_static_assert (sizeof (ULONGEST) >= 8);
2449 sect_offset sect_off
2450 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2451 type_offset_in_tu
2452 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2453 BFD_ENDIAN_LITTLE);
2454 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2455 bytes += 3 * 8;
2456
2457 sig_type = per_bfd->allocate_signatured_type (signature);
2458 sig_type->type_offset_in_tu = type_offset_in_tu;
2459 sig_type->section = section;
2460 sig_type->sect_off = sect_off;
2461 sig_type->v.quick
2462 = OBSTACK_ZALLOC (&per_bfd->obstack,
2463 struct dwarf2_per_cu_quick_data);
2464
2465 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2466 *slot = sig_type.get ();
2467
2468 per_bfd->all_comp_units.emplace_back (sig_type.release ());
2469 }
2470
2471 per_bfd->signatured_types = std::move (sig_types_hash);
2472 }
2473
2474 /* Create the signatured type hash table from .debug_names. */
2475
2476 static void
2477 create_signatured_type_table_from_debug_names
2478 (dwarf2_per_objfile *per_objfile,
2479 const mapped_debug_names &map,
2480 struct dwarf2_section_info *section,
2481 struct dwarf2_section_info *abbrev_section)
2482 {
2483 struct objfile *objfile = per_objfile->objfile;
2484
2485 section->read (objfile);
2486 abbrev_section->read (objfile);
2487
2488 htab_up sig_types_hash = allocate_signatured_type_table ();
2489
2490 for (uint32_t i = 0; i < map.tu_count; ++i)
2491 {
2492 signatured_type_up sig_type;
2493 void **slot;
2494
2495 sect_offset sect_off
2496 = (sect_offset) (extract_unsigned_integer
2497 (map.tu_table_reordered + i * map.offset_size,
2498 map.offset_size,
2499 map.dwarf5_byte_order));
2500
2501 comp_unit_head cu_header;
2502 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2503 abbrev_section,
2504 section->buffer + to_underlying (sect_off),
2505 rcuh_kind::TYPE);
2506
2507 sig_type = per_objfile->per_bfd->allocate_signatured_type
2508 (cu_header.signature);
2509 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2510 sig_type->section = section;
2511 sig_type->sect_off = sect_off;
2512 sig_type->v.quick
2513 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2514 struct dwarf2_per_cu_quick_data);
2515
2516 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2517 *slot = sig_type.get ();
2518
2519 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
2520 }
2521
2522 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2523 }
2524
2525 /* Read the address map data from the mapped index, and use it to
2526 populate the psymtabs_addrmap. */
2527
2528 static void
2529 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2530 struct mapped_index *index)
2531 {
2532 struct objfile *objfile = per_objfile->objfile;
2533 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2534 struct gdbarch *gdbarch = objfile->arch ();
2535 const gdb_byte *iter, *end;
2536 struct addrmap *mutable_map;
2537 CORE_ADDR baseaddr;
2538
2539 auto_obstack temp_obstack;
2540
2541 mutable_map = addrmap_create_mutable (&temp_obstack);
2542
2543 iter = index->address_table.data ();
2544 end = iter + index->address_table.size ();
2545
2546 baseaddr = objfile->text_section_offset ();
2547
2548 while (iter < end)
2549 {
2550 ULONGEST hi, lo, cu_index;
2551 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2552 iter += 8;
2553 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2554 iter += 8;
2555 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2556 iter += 4;
2557
2558 if (lo > hi)
2559 {
2560 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2561 hex_string (lo), hex_string (hi));
2562 continue;
2563 }
2564
2565 if (cu_index >= per_bfd->all_comp_units.size ())
2566 {
2567 complaint (_(".gdb_index address table has invalid CU number %u"),
2568 (unsigned) cu_index);
2569 continue;
2570 }
2571
2572 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2573 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2574 addrmap_set_empty (mutable_map, lo, hi - 1,
2575 per_bfd->get_cu (cu_index));
2576 }
2577
2578 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2579 &per_bfd->obstack);
2580 }
2581
2582 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2583 populate the psymtabs_addrmap. */
2584
2585 static void
2586 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2587 struct dwarf2_section_info *section)
2588 {
2589 struct objfile *objfile = per_objfile->objfile;
2590 bfd *abfd = objfile->obfd;
2591 struct gdbarch *gdbarch = objfile->arch ();
2592 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2593 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2594
2595 auto_obstack temp_obstack;
2596 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2597
2598 std::unordered_map<sect_offset,
2599 dwarf2_per_cu_data *,
2600 gdb::hash_enum<sect_offset>>
2601 debug_info_offset_to_per_cu;
2602 for (const auto &per_cu : per_bfd->all_comp_units)
2603 {
2604 /* A TU will not need aranges, and skipping them here is an easy
2605 way of ignoring .debug_types -- and possibly seeing a
2606 duplicate section offset -- entirely. */
2607 if (per_cu->is_debug_types)
2608 continue;
2609
2610 const auto insertpair
2611 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2612 per_cu.get ());
2613 if (!insertpair.second)
2614 {
2615 warning (_("Section .debug_aranges in %s has duplicate "
2616 "debug_info_offset %s, ignoring .debug_aranges."),
2617 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2618 return;
2619 }
2620 }
2621
2622 section->read (objfile);
2623
2624 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2625
2626 const gdb_byte *addr = section->buffer;
2627
2628 while (addr < section->buffer + section->size)
2629 {
2630 const gdb_byte *const entry_addr = addr;
2631 unsigned int bytes_read;
2632
2633 const LONGEST entry_length = read_initial_length (abfd, addr,
2634 &bytes_read);
2635 addr += bytes_read;
2636
2637 const gdb_byte *const entry_end = addr + entry_length;
2638 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2639 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2640 if (addr + entry_length > section->buffer + section->size)
2641 {
2642 warning (_("Section .debug_aranges in %s entry at offset %s "
2643 "length %s exceeds section length %s, "
2644 "ignoring .debug_aranges."),
2645 objfile_name (objfile),
2646 plongest (entry_addr - section->buffer),
2647 plongest (bytes_read + entry_length),
2648 pulongest (section->size));
2649 return;
2650 }
2651
2652 /* The version number. */
2653 const uint16_t version = read_2_bytes (abfd, addr);
2654 addr += 2;
2655 if (version != 2)
2656 {
2657 warning (_("Section .debug_aranges in %s entry at offset %s "
2658 "has unsupported version %d, ignoring .debug_aranges."),
2659 objfile_name (objfile),
2660 plongest (entry_addr - section->buffer), version);
2661 return;
2662 }
2663
2664 const uint64_t debug_info_offset
2665 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2666 addr += offset_size;
2667 const auto per_cu_it
2668 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2669 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2670 {
2671 warning (_("Section .debug_aranges in %s entry at offset %s "
2672 "debug_info_offset %s does not exists, "
2673 "ignoring .debug_aranges."),
2674 objfile_name (objfile),
2675 plongest (entry_addr - section->buffer),
2676 pulongest (debug_info_offset));
2677 return;
2678 }
2679 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2680
2681 const uint8_t address_size = *addr++;
2682 if (address_size < 1 || address_size > 8)
2683 {
2684 warning (_("Section .debug_aranges in %s entry at offset %s "
2685 "address_size %u is invalid, ignoring .debug_aranges."),
2686 objfile_name (objfile),
2687 plongest (entry_addr - section->buffer), address_size);
2688 return;
2689 }
2690
2691 const uint8_t segment_selector_size = *addr++;
2692 if (segment_selector_size != 0)
2693 {
2694 warning (_("Section .debug_aranges in %s entry at offset %s "
2695 "segment_selector_size %u is not supported, "
2696 "ignoring .debug_aranges."),
2697 objfile_name (objfile),
2698 plongest (entry_addr - section->buffer),
2699 segment_selector_size);
2700 return;
2701 }
2702
2703 /* Must pad to an alignment boundary that is twice the address
2704 size. It is undocumented by the DWARF standard but GCC does
2705 use it. However, not every compiler does this. We can see
2706 whether it has happened by looking at the total length of the
2707 contents of the aranges for this CU -- it if isn't a multiple
2708 of twice the address size, then we skip any leftover
2709 bytes. */
2710 addr += (entry_end - addr) % (2 * address_size);
2711
2712 for (;;)
2713 {
2714 if (addr + 2 * address_size > entry_end)
2715 {
2716 warning (_("Section .debug_aranges in %s entry at offset %s "
2717 "address list is not properly terminated, "
2718 "ignoring .debug_aranges."),
2719 objfile_name (objfile),
2720 plongest (entry_addr - section->buffer));
2721 return;
2722 }
2723 ULONGEST start = extract_unsigned_integer (addr, address_size,
2724 dwarf5_byte_order);
2725 addr += address_size;
2726 ULONGEST length = extract_unsigned_integer (addr, address_size,
2727 dwarf5_byte_order);
2728 addr += address_size;
2729 if (start == 0 && length == 0)
2730 break;
2731 if (start == 0 && !per_bfd->has_section_at_zero)
2732 {
2733 /* Symbol was eliminated due to a COMDAT group. */
2734 continue;
2735 }
2736 ULONGEST end = start + length;
2737 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2738 - baseaddr);
2739 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2740 - baseaddr);
2741 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2742 }
2743 }
2744
2745 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2746 &per_bfd->obstack);
2747 }
2748
2749 /* A helper function that reads the .gdb_index from BUFFER and fills
2750 in MAP. FILENAME is the name of the file containing the data;
2751 it is used for error reporting. DEPRECATED_OK is true if it is
2752 ok to use deprecated sections.
2753
2754 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2755 out parameters that are filled in with information about the CU and
2756 TU lists in the section.
2757
2758 Returns true if all went well, false otherwise. */
2759
2760 static bool
2761 read_gdb_index_from_buffer (const char *filename,
2762 bool deprecated_ok,
2763 gdb::array_view<const gdb_byte> buffer,
2764 struct mapped_index *map,
2765 const gdb_byte **cu_list,
2766 offset_type *cu_list_elements,
2767 const gdb_byte **types_list,
2768 offset_type *types_list_elements)
2769 {
2770 const gdb_byte *addr = &buffer[0];
2771 offset_view metadata (buffer);
2772
2773 /* Version check. */
2774 offset_type version = metadata[0];
2775 /* Versions earlier than 3 emitted every copy of a psymbol. This
2776 causes the index to behave very poorly for certain requests. Version 3
2777 contained incomplete addrmap. So, it seems better to just ignore such
2778 indices. */
2779 if (version < 4)
2780 {
2781 static int warning_printed = 0;
2782 if (!warning_printed)
2783 {
2784 warning (_("Skipping obsolete .gdb_index section in %s."),
2785 filename);
2786 warning_printed = 1;
2787 }
2788 return 0;
2789 }
2790 /* Index version 4 uses a different hash function than index version
2791 5 and later.
2792
2793 Versions earlier than 6 did not emit psymbols for inlined
2794 functions. Using these files will cause GDB not to be able to
2795 set breakpoints on inlined functions by name, so we ignore these
2796 indices unless the user has done
2797 "set use-deprecated-index-sections on". */
2798 if (version < 6 && !deprecated_ok)
2799 {
2800 static int warning_printed = 0;
2801 if (!warning_printed)
2802 {
2803 warning (_("\
2804 Skipping deprecated .gdb_index section in %s.\n\
2805 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2806 to use the section anyway."),
2807 filename);
2808 warning_printed = 1;
2809 }
2810 return 0;
2811 }
2812 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2813 of the TU (for symbols coming from TUs),
2814 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2815 Plus gold-generated indices can have duplicate entries for global symbols,
2816 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2817 These are just performance bugs, and we can't distinguish gdb-generated
2818 indices from gold-generated ones, so issue no warning here. */
2819
2820 /* Indexes with higher version than the one supported by GDB may be no
2821 longer backward compatible. */
2822 if (version > 8)
2823 return 0;
2824
2825 map->version = version;
2826
2827 int i = 1;
2828 *cu_list = addr + metadata[i];
2829 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2830 ++i;
2831
2832 *types_list = addr + metadata[i];
2833 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2834 ++i;
2835
2836 const gdb_byte *address_table = addr + metadata[i];
2837 const gdb_byte *address_table_end = addr + metadata[i + 1];
2838 map->address_table
2839 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2840 ++i;
2841
2842 const gdb_byte *symbol_table = addr + metadata[i];
2843 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2844 map->symbol_table
2845 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2846 symbol_table_end));
2847
2848 ++i;
2849 map->constant_pool = buffer.slice (metadata[i]);
2850
2851 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2852 {
2853 /* An empty constant pool implies that all symbol table entries are
2854 empty. Make map->symbol_table.empty () == true. */
2855 map->symbol_table
2856 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2857 symbol_table));
2858 }
2859
2860 return 1;
2861 }
2862
2863 /* Callback types for dwarf2_read_gdb_index. */
2864
2865 typedef gdb::function_view
2866 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2867 get_gdb_index_contents_ftype;
2868 typedef gdb::function_view
2869 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2870 get_gdb_index_contents_dwz_ftype;
2871
2872 /* Read .gdb_index. If everything went ok, initialize the "quick"
2873 elements of all the CUs and return 1. Otherwise, return 0. */
2874
2875 static int
2876 dwarf2_read_gdb_index
2877 (dwarf2_per_objfile *per_objfile,
2878 get_gdb_index_contents_ftype get_gdb_index_contents,
2879 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2880 {
2881 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2882 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2883 struct dwz_file *dwz;
2884 struct objfile *objfile = per_objfile->objfile;
2885 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2886
2887 gdb::array_view<const gdb_byte> main_index_contents
2888 = get_gdb_index_contents (objfile, per_bfd);
2889
2890 if (main_index_contents.empty ())
2891 return 0;
2892
2893 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2894 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2895 use_deprecated_index_sections,
2896 main_index_contents, map.get (), &cu_list,
2897 &cu_list_elements, &types_list,
2898 &types_list_elements))
2899 return 0;
2900
2901 /* Don't use the index if it's empty. */
2902 if (map->symbol_table.empty ())
2903 return 0;
2904
2905 /* If there is a .dwz file, read it so we can get its CU list as
2906 well. */
2907 dwz = dwarf2_get_dwz_file (per_bfd);
2908 if (dwz != NULL)
2909 {
2910 struct mapped_index dwz_map;
2911 const gdb_byte *dwz_types_ignore;
2912 offset_type dwz_types_elements_ignore;
2913
2914 gdb::array_view<const gdb_byte> dwz_index_content
2915 = get_gdb_index_contents_dwz (objfile, dwz);
2916
2917 if (dwz_index_content.empty ())
2918 return 0;
2919
2920 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2921 1, dwz_index_content, &dwz_map,
2922 &dwz_list, &dwz_list_elements,
2923 &dwz_types_ignore,
2924 &dwz_types_elements_ignore))
2925 {
2926 warning (_("could not read '.gdb_index' section from %s; skipping"),
2927 bfd_get_filename (dwz->dwz_bfd.get ()));
2928 return 0;
2929 }
2930 }
2931
2932 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2933 dwz_list_elements);
2934
2935 if (types_list_elements)
2936 {
2937 /* We can only handle a single .debug_types when we have an
2938 index. */
2939 if (per_bfd->types.size () != 1)
2940 return 0;
2941
2942 dwarf2_section_info *section = &per_bfd->types[0];
2943
2944 create_signatured_type_table_from_index (per_bfd, section, types_list,
2945 types_list_elements);
2946 }
2947
2948 create_addrmap_from_index (per_objfile, map.get ());
2949
2950 per_bfd->index_table = std::move (map);
2951 per_bfd->using_index = 1;
2952 per_bfd->quick_file_names_table =
2953 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2954
2955 return 1;
2956 }
2957
2958 /* die_reader_func for dw2_get_file_names. */
2959
2960 static void
2961 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2962 struct die_info *comp_unit_die)
2963 {
2964 struct dwarf2_cu *cu = reader->cu;
2965 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2966 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2967 struct dwarf2_per_cu_data *lh_cu;
2968 struct attribute *attr;
2969 void **slot;
2970 struct quick_file_names *qfn;
2971
2972 gdb_assert (! this_cu->is_debug_types);
2973
2974 this_cu->v.quick->files_read = true;
2975 /* Our callers never want to match partial units -- instead they
2976 will match the enclosing full CU. */
2977 if (comp_unit_die->tag == DW_TAG_partial_unit)
2978 return;
2979
2980 lh_cu = this_cu;
2981 slot = NULL;
2982
2983 line_header_up lh;
2984 sect_offset line_offset {};
2985
2986 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2987 if (attr != nullptr && attr->form_is_unsigned ())
2988 {
2989 struct quick_file_names find_entry;
2990
2991 line_offset = (sect_offset) attr->as_unsigned ();
2992
2993 /* We may have already read in this line header (TU line header sharing).
2994 If we have we're done. */
2995 find_entry.hash.dwo_unit = cu->dwo_unit;
2996 find_entry.hash.line_sect_off = line_offset;
2997 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2998 &find_entry, INSERT);
2999 if (*slot != NULL)
3000 {
3001 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3002 return;
3003 }
3004
3005 lh = dwarf_decode_line_header (line_offset, cu);
3006 }
3007
3008 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
3009
3010 int offset = 0;
3011 if (fnd.is_unknown ())
3012 ++offset;
3013 else if (lh == nullptr)
3014 return;
3015
3016 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3017 qfn->hash.dwo_unit = cu->dwo_unit;
3018 qfn->hash.line_sect_off = line_offset;
3019 /* There may not be a DW_AT_stmt_list. */
3020 if (slot != nullptr)
3021 *slot = qfn;
3022
3023 std::vector<const char *> include_names;
3024 if (lh != nullptr)
3025 {
3026 for (const auto &entry : lh->file_names ())
3027 {
3028 gdb::unique_xmalloc_ptr<char> name_holder;
3029 const char *include_name =
3030 compute_include_file_name (lh.get (), entry, fnd, &name_holder);
3031 if (include_name != nullptr)
3032 {
3033 include_name = per_objfile->objfile->intern (include_name);
3034 include_names.push_back (include_name);
3035 }
3036 }
3037 }
3038
3039 qfn->num_file_names = offset + include_names.size ();
3040 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
3041 qfn->file_names =
3042 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3043 qfn->num_file_names);
3044 if (offset != 0)
3045 qfn->file_names[0] = xstrdup (fnd.get_name ());
3046
3047 if (!include_names.empty ())
3048 memcpy (&qfn->file_names[offset], include_names.data (),
3049 include_names.size () * sizeof (const char *));
3050
3051 qfn->real_names = NULL;
3052
3053 lh_cu->v.quick->file_names = qfn;
3054 }
3055
3056 /* A helper for the "quick" functions which attempts to read the line
3057 table for THIS_CU. */
3058
3059 static struct quick_file_names *
3060 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3061 dwarf2_per_objfile *per_objfile)
3062 {
3063 /* This should never be called for TUs. */
3064 gdb_assert (! this_cu->is_debug_types);
3065 /* Nor type unit groups. */
3066 gdb_assert (! this_cu->type_unit_group_p ());
3067
3068 if (this_cu->v.quick->files_read)
3069 return this_cu->v.quick->file_names;
3070
3071 cutu_reader reader (this_cu, per_objfile);
3072 if (!reader.dummy_p)
3073 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3074
3075 return this_cu->v.quick->file_names;
3076 }
3077
3078 /* A helper for the "quick" functions which computes and caches the
3079 real path for a given file name from the line table. */
3080
3081 static const char *
3082 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3083 struct quick_file_names *qfn, int index)
3084 {
3085 if (qfn->real_names == NULL)
3086 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3087 qfn->num_file_names, const char *);
3088
3089 if (qfn->real_names[index] == NULL)
3090 {
3091 const char *dirname = nullptr;
3092
3093 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
3094 dirname = qfn->comp_dir;
3095
3096 gdb::unique_xmalloc_ptr<char> fullname;
3097 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
3098
3099 qfn->real_names[index] = fullname.release ();
3100 }
3101
3102 return qfn->real_names[index];
3103 }
3104
3105 struct symtab *
3106 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3107 {
3108 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3109 dwarf2_per_cu_data *dwarf_cu
3110 = per_objfile->per_bfd->all_comp_units.back ().get ();
3111 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3112
3113 if (cust == NULL)
3114 return NULL;
3115
3116 return compunit_primary_filetab (cust);
3117 }
3118
3119 /* See read.h. */
3120
3121 void
3122 dwarf2_per_cu_data::free_cached_file_names ()
3123 {
3124 if (per_bfd == nullptr || !per_bfd->using_index || v.quick == nullptr)
3125 return;
3126
3127 struct quick_file_names *file_data = v.quick->file_names;
3128 if (file_data != nullptr && file_data->real_names != nullptr)
3129 {
3130 for (int i = 0; i < file_data->num_file_names; ++i)
3131 {
3132 xfree ((void *) file_data->real_names[i]);
3133 file_data->real_names[i] = nullptr;
3134 }
3135 }
3136 }
3137
3138 void
3139 dwarf2_base_index_functions::forget_cached_source_info
3140 (struct objfile *objfile)
3141 {
3142 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3143
3144 for (auto &per_cu : per_objfile->per_bfd->all_comp_units)
3145 per_cu->free_cached_file_names ();
3146 }
3147
3148 /* Struct used to manage iterating over all CUs looking for a symbol. */
3149
3150 struct dw2_symtab_iterator
3151 {
3152 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3153 dwarf2_per_objfile *per_objfile;
3154 /* If set, only look for symbols that match that block. Valid values are
3155 GLOBAL_BLOCK and STATIC_BLOCK. */
3156 gdb::optional<block_enum> block_index;
3157 /* The kind of symbol we're looking for. */
3158 domain_enum domain;
3159 /* The list of CUs from the index entry of the symbol,
3160 or NULL if not found. */
3161 offset_view vec;
3162 /* The next element in VEC to look at. */
3163 int next;
3164 /* The number of elements in VEC, or zero if there is no match. */
3165 int length;
3166 /* Have we seen a global version of the symbol?
3167 If so we can ignore all further global instances.
3168 This is to work around gold/15646, inefficient gold-generated
3169 indices. */
3170 int global_seen;
3171 };
3172
3173 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3174
3175 static void
3176 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3177 dwarf2_per_objfile *per_objfile,
3178 gdb::optional<block_enum> block_index,
3179 domain_enum domain, offset_type namei)
3180 {
3181 iter->per_objfile = per_objfile;
3182 iter->block_index = block_index;
3183 iter->domain = domain;
3184 iter->next = 0;
3185 iter->global_seen = 0;
3186 iter->vec = {};
3187 iter->length = 0;
3188
3189 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3190 /* index is NULL if OBJF_READNOW. */
3191 if (index == NULL)
3192 return;
3193
3194 gdb_assert (!index->symbol_name_slot_invalid (namei));
3195 offset_type vec_idx = index->symbol_vec_index (namei);
3196
3197 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3198 iter->length = iter->vec[0];
3199 }
3200
3201 /* Return the next matching CU or NULL if there are no more. */
3202
3203 static struct dwarf2_per_cu_data *
3204 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3205 {
3206 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3207
3208 for ( ; iter->next < iter->length; ++iter->next)
3209 {
3210 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3211 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3212 gdb_index_symbol_kind symbol_kind =
3213 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3214 /* Only check the symbol attributes if they're present.
3215 Indices prior to version 7 don't record them,
3216 and indices >= 7 may elide them for certain symbols
3217 (gold does this). */
3218 int attrs_valid =
3219 (per_objfile->per_bfd->index_table->version >= 7
3220 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3221
3222 /* Don't crash on bad data. */
3223 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3224 {
3225 complaint (_(".gdb_index entry has bad CU index"
3226 " [in module %s]"), objfile_name (per_objfile->objfile));
3227 continue;
3228 }
3229
3230 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3231
3232 /* Skip if already read in. */
3233 if (per_objfile->symtab_set_p (per_cu))
3234 continue;
3235
3236 /* Check static vs global. */
3237 if (attrs_valid)
3238 {
3239 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3240
3241 if (iter->block_index.has_value ())
3242 {
3243 bool want_static = *iter->block_index == STATIC_BLOCK;
3244
3245 if (is_static != want_static)
3246 continue;
3247 }
3248
3249 /* Work around gold/15646. */
3250 if (!is_static
3251 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3252 {
3253 if (iter->global_seen)
3254 continue;
3255
3256 iter->global_seen = 1;
3257 }
3258 }
3259
3260 /* Only check the symbol's kind if it has one. */
3261 if (attrs_valid)
3262 {
3263 switch (iter->domain)
3264 {
3265 case VAR_DOMAIN:
3266 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3267 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3268 /* Some types are also in VAR_DOMAIN. */
3269 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3270 continue;
3271 break;
3272 case STRUCT_DOMAIN:
3273 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3274 continue;
3275 break;
3276 case LABEL_DOMAIN:
3277 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3278 continue;
3279 break;
3280 case MODULE_DOMAIN:
3281 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3282 continue;
3283 break;
3284 default:
3285 break;
3286 }
3287 }
3288
3289 ++iter->next;
3290 return per_cu;
3291 }
3292
3293 return NULL;
3294 }
3295
3296 void
3297 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3298 bool print_bcache)
3299 {
3300 if (print_bcache)
3301 return;
3302
3303 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3304 int total = per_objfile->per_bfd->all_comp_units.size ();
3305 int count = 0;
3306
3307 for (int i = 0; i < total; ++i)
3308 {
3309 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3310
3311 if (!per_objfile->symtab_set_p (per_cu))
3312 ++count;
3313 }
3314 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3315 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3316 }
3317
3318 /* This dumps minimal information about the index.
3319 It is called via "mt print objfiles".
3320 One use is to verify .gdb_index has been loaded by the
3321 gdb.dwarf2/gdb-index.exp testcase. */
3322
3323 void
3324 dwarf2_gdb_index::dump (struct objfile *objfile)
3325 {
3326 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3327
3328 gdb_assert (per_objfile->per_bfd->using_index);
3329 printf_filtered (".gdb_index:");
3330 if (per_objfile->per_bfd->index_table != NULL)
3331 {
3332 printf_filtered (" version %d\n",
3333 per_objfile->per_bfd->index_table->version);
3334 }
3335 else
3336 printf_filtered (" faked for \"readnow\"\n");
3337 printf_filtered ("\n");
3338 }
3339
3340 void
3341 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3342 {
3343 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3344 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3345
3346 for (int i = 0; i < total_units; ++i)
3347 {
3348 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3349
3350 /* We don't want to directly expand a partial CU, because if we
3351 read it with the wrong language, then assertion failures can
3352 be triggered later on. See PR symtab/23010. So, tell
3353 dw2_instantiate_symtab to skip partial CUs -- any important
3354 partial CU will be read via DW_TAG_imported_unit anyway. */
3355 dw2_instantiate_symtab (per_cu, per_objfile, true);
3356 }
3357 }
3358
3359 static bool
3360 dw2_expand_symtabs_matching_symbol
3361 (mapped_index_base &index,
3362 const lookup_name_info &lookup_name_in,
3363 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3364 gdb::function_view<bool (offset_type)> match_callback,
3365 dwarf2_per_objfile *per_objfile);
3366
3367 static bool
3368 dw2_expand_symtabs_matching_one
3369 (dwarf2_per_cu_data *per_cu,
3370 dwarf2_per_objfile *per_objfile,
3371 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3372 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3373
3374 void
3375 dwarf2_gdb_index::expand_matching_symbols
3376 (struct objfile *objfile,
3377 const lookup_name_info &name, domain_enum domain,
3378 int global,
3379 symbol_compare_ftype *ordered_compare)
3380 {
3381 /* Used for Ada. */
3382 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3383
3384 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3385
3386 if (per_objfile->per_bfd->index_table != nullptr)
3387 {
3388 mapped_index &index = *per_objfile->per_bfd->index_table;
3389
3390 const char *match_name = name.ada ().lookup_name ().c_str ();
3391 auto matcher = [&] (const char *symname)
3392 {
3393 if (ordered_compare == nullptr)
3394 return true;
3395 return ordered_compare (symname, match_name) == 0;
3396 };
3397
3398 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3399 [&] (offset_type namei)
3400 {
3401 struct dw2_symtab_iterator iter;
3402 struct dwarf2_per_cu_data *per_cu;
3403
3404 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3405 namei);
3406 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3407 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3408 nullptr);
3409 return true;
3410 }, per_objfile);
3411 }
3412 else
3413 {
3414 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3415 proceed assuming all symtabs have been read in. */
3416 }
3417 }
3418
3419 /* Starting from a search name, return the string that finds the upper
3420 bound of all strings that start with SEARCH_NAME in a sorted name
3421 list. Returns the empty string to indicate that the upper bound is
3422 the end of the list. */
3423
3424 static std::string
3425 make_sort_after_prefix_name (const char *search_name)
3426 {
3427 /* When looking to complete "func", we find the upper bound of all
3428 symbols that start with "func" by looking for where we'd insert
3429 the closest string that would follow "func" in lexicographical
3430 order. Usually, that's "func"-with-last-character-incremented,
3431 i.e. "fund". Mind non-ASCII characters, though. Usually those
3432 will be UTF-8 multi-byte sequences, but we can't be certain.
3433 Especially mind the 0xff character, which is a valid character in
3434 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3435 rule out compilers allowing it in identifiers. Note that
3436 conveniently, strcmp/strcasecmp are specified to compare
3437 characters interpreted as unsigned char. So what we do is treat
3438 the whole string as a base 256 number composed of a sequence of
3439 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3440 to 0, and carries 1 to the following more-significant position.
3441 If the very first character in SEARCH_NAME ends up incremented
3442 and carries/overflows, then the upper bound is the end of the
3443 list. The string after the empty string is also the empty
3444 string.
3445
3446 Some examples of this operation:
3447
3448 SEARCH_NAME => "+1" RESULT
3449
3450 "abc" => "abd"
3451 "ab\xff" => "ac"
3452 "\xff" "a" "\xff" => "\xff" "b"
3453 "\xff" => ""
3454 "\xff\xff" => ""
3455 "" => ""
3456
3457 Then, with these symbols for example:
3458
3459 func
3460 func1
3461 fund
3462
3463 completing "func" looks for symbols between "func" and
3464 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3465 which finds "func" and "func1", but not "fund".
3466
3467 And with:
3468
3469 funcÿ (Latin1 'ÿ' [0xff])
3470 funcÿ1
3471 fund
3472
3473 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3474 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3475
3476 And with:
3477
3478 ÿÿ (Latin1 'ÿ' [0xff])
3479 ÿÿ1
3480
3481 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3482 the end of the list.
3483 */
3484 std::string after = search_name;
3485 while (!after.empty () && (unsigned char) after.back () == 0xff)
3486 after.pop_back ();
3487 if (!after.empty ())
3488 after.back () = (unsigned char) after.back () + 1;
3489 return after;
3490 }
3491
3492 /* See declaration. */
3493
3494 std::pair<std::vector<name_component>::const_iterator,
3495 std::vector<name_component>::const_iterator>
3496 mapped_index_base::find_name_components_bounds
3497 (const lookup_name_info &lookup_name_without_params, language lang,
3498 dwarf2_per_objfile *per_objfile) const
3499 {
3500 auto *name_cmp
3501 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3502
3503 const char *lang_name
3504 = lookup_name_without_params.language_lookup_name (lang);
3505
3506 /* Comparison function object for lower_bound that matches against a
3507 given symbol name. */
3508 auto lookup_compare_lower = [&] (const name_component &elem,
3509 const char *name)
3510 {
3511 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3512 const char *elem_name = elem_qualified + elem.name_offset;
3513 return name_cmp (elem_name, name) < 0;
3514 };
3515
3516 /* Comparison function object for upper_bound that matches against a
3517 given symbol name. */
3518 auto lookup_compare_upper = [&] (const char *name,
3519 const name_component &elem)
3520 {
3521 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3522 const char *elem_name = elem_qualified + elem.name_offset;
3523 return name_cmp (name, elem_name) < 0;
3524 };
3525
3526 auto begin = this->name_components.begin ();
3527 auto end = this->name_components.end ();
3528
3529 /* Find the lower bound. */
3530 auto lower = [&] ()
3531 {
3532 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3533 return begin;
3534 else
3535 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3536 } ();
3537
3538 /* Find the upper bound. */
3539 auto upper = [&] ()
3540 {
3541 if (lookup_name_without_params.completion_mode ())
3542 {
3543 /* In completion mode, we want UPPER to point past all
3544 symbols names that have the same prefix. I.e., with
3545 these symbols, and completing "func":
3546
3547 function << lower bound
3548 function1
3549 other_function << upper bound
3550
3551 We find the upper bound by looking for the insertion
3552 point of "func"-with-last-character-incremented,
3553 i.e. "fund". */
3554 std::string after = make_sort_after_prefix_name (lang_name);
3555 if (after.empty ())
3556 return end;
3557 return std::lower_bound (lower, end, after.c_str (),
3558 lookup_compare_lower);
3559 }
3560 else
3561 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3562 } ();
3563
3564 return {lower, upper};
3565 }
3566
3567 /* See declaration. */
3568
3569 void
3570 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3571 {
3572 if (!this->name_components.empty ())
3573 return;
3574
3575 this->name_components_casing = case_sensitivity;
3576 auto *name_cmp
3577 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3578
3579 /* The code below only knows how to break apart components of C++
3580 symbol names (and other languages that use '::' as
3581 namespace/module separator) and Ada symbol names. */
3582 auto count = this->symbol_name_count ();
3583 for (offset_type idx = 0; idx < count; idx++)
3584 {
3585 if (this->symbol_name_slot_invalid (idx))
3586 continue;
3587
3588 const char *name = this->symbol_name_at (idx, per_objfile);
3589
3590 /* Add each name component to the name component table. */
3591 unsigned int previous_len = 0;
3592
3593 if (strstr (name, "::") != nullptr)
3594 {
3595 for (unsigned int current_len = cp_find_first_component (name);
3596 name[current_len] != '\0';
3597 current_len += cp_find_first_component (name + current_len))
3598 {
3599 gdb_assert (name[current_len] == ':');
3600 this->name_components.push_back ({previous_len, idx});
3601 /* Skip the '::'. */
3602 current_len += 2;
3603 previous_len = current_len;
3604 }
3605 }
3606 else
3607 {
3608 /* Handle the Ada encoded (aka mangled) form here. */
3609 for (const char *iter = strstr (name, "__");
3610 iter != nullptr;
3611 iter = strstr (iter, "__"))
3612 {
3613 this->name_components.push_back ({previous_len, idx});
3614 iter += 2;
3615 previous_len = iter - name;
3616 }
3617 }
3618
3619 this->name_components.push_back ({previous_len, idx});
3620 }
3621
3622 /* Sort name_components elements by name. */
3623 auto name_comp_compare = [&] (const name_component &left,
3624 const name_component &right)
3625 {
3626 const char *left_qualified
3627 = this->symbol_name_at (left.idx, per_objfile);
3628 const char *right_qualified
3629 = this->symbol_name_at (right.idx, per_objfile);
3630
3631 const char *left_name = left_qualified + left.name_offset;
3632 const char *right_name = right_qualified + right.name_offset;
3633
3634 return name_cmp (left_name, right_name) < 0;
3635 };
3636
3637 std::sort (this->name_components.begin (),
3638 this->name_components.end (),
3639 name_comp_compare);
3640 }
3641
3642 /* Helper for dw2_expand_symtabs_matching that works with a
3643 mapped_index_base instead of the containing objfile. This is split
3644 to a separate function in order to be able to unit test the
3645 name_components matching using a mock mapped_index_base. For each
3646 symbol name that matches, calls MATCH_CALLBACK, passing it the
3647 symbol's index in the mapped_index_base symbol table. */
3648
3649 static bool
3650 dw2_expand_symtabs_matching_symbol
3651 (mapped_index_base &index,
3652 const lookup_name_info &lookup_name_in,
3653 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3654 gdb::function_view<bool (offset_type)> match_callback,
3655 dwarf2_per_objfile *per_objfile)
3656 {
3657 lookup_name_info lookup_name_without_params
3658 = lookup_name_in.make_ignore_params ();
3659
3660 /* Build the symbol name component sorted vector, if we haven't
3661 yet. */
3662 index.build_name_components (per_objfile);
3663
3664 /* The same symbol may appear more than once in the range though.
3665 E.g., if we're looking for symbols that complete "w", and we have
3666 a symbol named "w1::w2", we'll find the two name components for
3667 that same symbol in the range. To be sure we only call the
3668 callback once per symbol, we first collect the symbol name
3669 indexes that matched in a temporary vector and ignore
3670 duplicates. */
3671 std::vector<offset_type> matches;
3672
3673 struct name_and_matcher
3674 {
3675 symbol_name_matcher_ftype *matcher;
3676 const char *name;
3677
3678 bool operator== (const name_and_matcher &other) const
3679 {
3680 return matcher == other.matcher && strcmp (name, other.name) == 0;
3681 }
3682 };
3683
3684 /* A vector holding all the different symbol name matchers, for all
3685 languages. */
3686 std::vector<name_and_matcher> matchers;
3687
3688 for (int i = 0; i < nr_languages; i++)
3689 {
3690 enum language lang_e = (enum language) i;
3691
3692 const language_defn *lang = language_def (lang_e);
3693 symbol_name_matcher_ftype *name_matcher
3694 = lang->get_symbol_name_matcher (lookup_name_without_params);
3695
3696 name_and_matcher key {
3697 name_matcher,
3698 lookup_name_without_params.language_lookup_name (lang_e)
3699 };
3700
3701 /* Don't insert the same comparison routine more than once.
3702 Note that we do this linear walk. This is not a problem in
3703 practice because the number of supported languages is
3704 low. */
3705 if (std::find (matchers.begin (), matchers.end (), key)
3706 != matchers.end ())
3707 continue;
3708 matchers.push_back (std::move (key));
3709
3710 auto bounds
3711 = index.find_name_components_bounds (lookup_name_without_params,
3712 lang_e, per_objfile);
3713
3714 /* Now for each symbol name in range, check to see if we have a name
3715 match, and if so, call the MATCH_CALLBACK callback. */
3716
3717 for (; bounds.first != bounds.second; ++bounds.first)
3718 {
3719 const char *qualified
3720 = index.symbol_name_at (bounds.first->idx, per_objfile);
3721
3722 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3723 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3724 continue;
3725
3726 matches.push_back (bounds.first->idx);
3727 }
3728 }
3729
3730 std::sort (matches.begin (), matches.end ());
3731
3732 /* Finally call the callback, once per match. */
3733 ULONGEST prev = -1;
3734 bool result = true;
3735 for (offset_type idx : matches)
3736 {
3737 if (prev != idx)
3738 {
3739 if (!match_callback (idx))
3740 {
3741 result = false;
3742 break;
3743 }
3744 prev = idx;
3745 }
3746 }
3747
3748 /* Above we use a type wider than idx's for 'prev', since 0 and
3749 (offset_type)-1 are both possible values. */
3750 static_assert (sizeof (prev) > sizeof (offset_type), "");
3751
3752 return result;
3753 }
3754
3755 #if GDB_SELF_TEST
3756
3757 namespace selftests { namespace dw2_expand_symtabs_matching {
3758
3759 /* A mock .gdb_index/.debug_names-like name index table, enough to
3760 exercise dw2_expand_symtabs_matching_symbol, which works with the
3761 mapped_index_base interface. Builds an index from the symbol list
3762 passed as parameter to the constructor. */
3763 class mock_mapped_index : public mapped_index_base
3764 {
3765 public:
3766 mock_mapped_index (gdb::array_view<const char *> symbols)
3767 : m_symbol_table (symbols)
3768 {}
3769
3770 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3771
3772 /* Return the number of names in the symbol table. */
3773 size_t symbol_name_count () const override
3774 {
3775 return m_symbol_table.size ();
3776 }
3777
3778 /* Get the name of the symbol at IDX in the symbol table. */
3779 const char *symbol_name_at
3780 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3781 {
3782 return m_symbol_table[idx];
3783 }
3784
3785 private:
3786 gdb::array_view<const char *> m_symbol_table;
3787 };
3788
3789 /* Convenience function that converts a NULL pointer to a "<null>"
3790 string, to pass to print routines. */
3791
3792 static const char *
3793 string_or_null (const char *str)
3794 {
3795 return str != NULL ? str : "<null>";
3796 }
3797
3798 /* Check if a lookup_name_info built from
3799 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3800 index. EXPECTED_LIST is the list of expected matches, in expected
3801 matching order. If no match expected, then an empty list is
3802 specified. Returns true on success. On failure prints a warning
3803 indicating the file:line that failed, and returns false. */
3804
3805 static bool
3806 check_match (const char *file, int line,
3807 mock_mapped_index &mock_index,
3808 const char *name, symbol_name_match_type match_type,
3809 bool completion_mode,
3810 std::initializer_list<const char *> expected_list,
3811 dwarf2_per_objfile *per_objfile)
3812 {
3813 lookup_name_info lookup_name (name, match_type, completion_mode);
3814
3815 bool matched = true;
3816
3817 auto mismatch = [&] (const char *expected_str,
3818 const char *got)
3819 {
3820 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3821 "expected=\"%s\", got=\"%s\"\n"),
3822 file, line,
3823 (match_type == symbol_name_match_type::FULL
3824 ? "FULL" : "WILD"),
3825 name, string_or_null (expected_str), string_or_null (got));
3826 matched = false;
3827 };
3828
3829 auto expected_it = expected_list.begin ();
3830 auto expected_end = expected_list.end ();
3831
3832 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3833 nullptr,
3834 [&] (offset_type idx)
3835 {
3836 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3837 const char *expected_str
3838 = expected_it == expected_end ? NULL : *expected_it++;
3839
3840 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3841 mismatch (expected_str, matched_name);
3842 return true;
3843 }, per_objfile);
3844
3845 const char *expected_str
3846 = expected_it == expected_end ? NULL : *expected_it++;
3847 if (expected_str != NULL)
3848 mismatch (expected_str, NULL);
3849
3850 return matched;
3851 }
3852
3853 /* The symbols added to the mock mapped_index for testing (in
3854 canonical form). */
3855 static const char *test_symbols[] = {
3856 "function",
3857 "std::bar",
3858 "std::zfunction",
3859 "std::zfunction2",
3860 "w1::w2",
3861 "ns::foo<char*>",
3862 "ns::foo<int>",
3863 "ns::foo<long>",
3864 "ns2::tmpl<int>::foo2",
3865 "(anonymous namespace)::A::B::C",
3866
3867 /* These are used to check that the increment-last-char in the
3868 matching algorithm for completion doesn't match "t1_fund" when
3869 completing "t1_func". */
3870 "t1_func",
3871 "t1_func1",
3872 "t1_fund",
3873 "t1_fund1",
3874
3875 /* A UTF-8 name with multi-byte sequences to make sure that
3876 cp-name-parser understands this as a single identifier ("função"
3877 is "function" in PT). */
3878 u8"u8função",
3879
3880 /* \377 (0xff) is Latin1 'ÿ'. */
3881 "yfunc\377",
3882
3883 /* \377 (0xff) is Latin1 'ÿ'. */
3884 "\377",
3885 "\377\377123",
3886
3887 /* A name with all sorts of complications. Starts with "z" to make
3888 it easier for the completion tests below. */
3889 #define Z_SYM_NAME \
3890 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3891 "::tuple<(anonymous namespace)::ui*, " \
3892 "std::default_delete<(anonymous namespace)::ui>, void>"
3893
3894 Z_SYM_NAME
3895 };
3896
3897 /* Returns true if the mapped_index_base::find_name_component_bounds
3898 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3899 in completion mode. */
3900
3901 static bool
3902 check_find_bounds_finds (mapped_index_base &index,
3903 const char *search_name,
3904 gdb::array_view<const char *> expected_syms,
3905 dwarf2_per_objfile *per_objfile)
3906 {
3907 lookup_name_info lookup_name (search_name,
3908 symbol_name_match_type::FULL, true);
3909
3910 auto bounds = index.find_name_components_bounds (lookup_name,
3911 language_cplus,
3912 per_objfile);
3913
3914 size_t distance = std::distance (bounds.first, bounds.second);
3915 if (distance != expected_syms.size ())
3916 return false;
3917
3918 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3919 {
3920 auto nc_elem = bounds.first + exp_elem;
3921 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3922 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3923 return false;
3924 }
3925
3926 return true;
3927 }
3928
3929 /* Test the lower-level mapped_index::find_name_component_bounds
3930 method. */
3931
3932 static void
3933 test_mapped_index_find_name_component_bounds ()
3934 {
3935 mock_mapped_index mock_index (test_symbols);
3936
3937 mock_index.build_name_components (NULL /* per_objfile */);
3938
3939 /* Test the lower-level mapped_index::find_name_component_bounds
3940 method in completion mode. */
3941 {
3942 static const char *expected_syms[] = {
3943 "t1_func",
3944 "t1_func1",
3945 };
3946
3947 SELF_CHECK (check_find_bounds_finds
3948 (mock_index, "t1_func", expected_syms,
3949 NULL /* per_objfile */));
3950 }
3951
3952 /* Check that the increment-last-char in the name matching algorithm
3953 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3954 {
3955 static const char *expected_syms1[] = {
3956 "\377",
3957 "\377\377123",
3958 };
3959 SELF_CHECK (check_find_bounds_finds
3960 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3961
3962 static const char *expected_syms2[] = {
3963 "\377\377123",
3964 };
3965 SELF_CHECK (check_find_bounds_finds
3966 (mock_index, "\377\377", expected_syms2,
3967 NULL /* per_objfile */));
3968 }
3969 }
3970
3971 /* Test dw2_expand_symtabs_matching_symbol. */
3972
3973 static void
3974 test_dw2_expand_symtabs_matching_symbol ()
3975 {
3976 mock_mapped_index mock_index (test_symbols);
3977
3978 /* We let all tests run until the end even if some fails, for debug
3979 convenience. */
3980 bool any_mismatch = false;
3981
3982 /* Create the expected symbols list (an initializer_list). Needed
3983 because lists have commas, and we need to pass them to CHECK,
3984 which is a macro. */
3985 #define EXPECT(...) { __VA_ARGS__ }
3986
3987 /* Wrapper for check_match that passes down the current
3988 __FILE__/__LINE__. */
3989 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3990 any_mismatch |= !check_match (__FILE__, __LINE__, \
3991 mock_index, \
3992 NAME, MATCH_TYPE, COMPLETION_MODE, \
3993 EXPECTED_LIST, NULL)
3994
3995 /* Identity checks. */
3996 for (const char *sym : test_symbols)
3997 {
3998 /* Should be able to match all existing symbols. */
3999 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4000 EXPECT (sym));
4001
4002 /* Should be able to match all existing symbols with
4003 parameters. */
4004 std::string with_params = std::string (sym) + "(int)";
4005 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4006 EXPECT (sym));
4007
4008 /* Should be able to match all existing symbols with
4009 parameters and qualifiers. */
4010 with_params = std::string (sym) + " ( int ) const";
4011 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4012 EXPECT (sym));
4013
4014 /* This should really find sym, but cp-name-parser.y doesn't
4015 know about lvalue/rvalue qualifiers yet. */
4016 with_params = std::string (sym) + " ( int ) &&";
4017 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4018 {});
4019 }
4020
4021 /* Check that the name matching algorithm for completion doesn't get
4022 confused with Latin1 'ÿ' / 0xff. */
4023 {
4024 static const char str[] = "\377";
4025 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4026 EXPECT ("\377", "\377\377123"));
4027 }
4028
4029 /* Check that the increment-last-char in the matching algorithm for
4030 completion doesn't match "t1_fund" when completing "t1_func". */
4031 {
4032 static const char str[] = "t1_func";
4033 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4034 EXPECT ("t1_func", "t1_func1"));
4035 }
4036
4037 /* Check that completion mode works at each prefix of the expected
4038 symbol name. */
4039 {
4040 static const char str[] = "function(int)";
4041 size_t len = strlen (str);
4042 std::string lookup;
4043
4044 for (size_t i = 1; i < len; i++)
4045 {
4046 lookup.assign (str, i);
4047 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4048 EXPECT ("function"));
4049 }
4050 }
4051
4052 /* While "w" is a prefix of both components, the match function
4053 should still only be called once. */
4054 {
4055 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4056 EXPECT ("w1::w2"));
4057 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4058 EXPECT ("w1::w2"));
4059 }
4060
4061 /* Same, with a "complicated" symbol. */
4062 {
4063 static const char str[] = Z_SYM_NAME;
4064 size_t len = strlen (str);
4065 std::string lookup;
4066
4067 for (size_t i = 1; i < len; i++)
4068 {
4069 lookup.assign (str, i);
4070 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4071 EXPECT (Z_SYM_NAME));
4072 }
4073 }
4074
4075 /* In FULL mode, an incomplete symbol doesn't match. */
4076 {
4077 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4078 {});
4079 }
4080
4081 /* A complete symbol with parameters matches any overload, since the
4082 index has no overload info. */
4083 {
4084 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4085 EXPECT ("std::zfunction", "std::zfunction2"));
4086 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4087 EXPECT ("std::zfunction", "std::zfunction2"));
4088 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4089 EXPECT ("std::zfunction", "std::zfunction2"));
4090 }
4091
4092 /* Check that whitespace is ignored appropriately. A symbol with a
4093 template argument list. */
4094 {
4095 static const char expected[] = "ns::foo<int>";
4096 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4097 EXPECT (expected));
4098 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4099 EXPECT (expected));
4100 }
4101
4102 /* Check that whitespace is ignored appropriately. A symbol with a
4103 template argument list that includes a pointer. */
4104 {
4105 static const char expected[] = "ns::foo<char*>";
4106 /* Try both completion and non-completion modes. */
4107 static const bool completion_mode[2] = {false, true};
4108 for (size_t i = 0; i < 2; i++)
4109 {
4110 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4111 completion_mode[i], EXPECT (expected));
4112 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4113 completion_mode[i], EXPECT (expected));
4114
4115 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4116 completion_mode[i], EXPECT (expected));
4117 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4118 completion_mode[i], EXPECT (expected));
4119 }
4120 }
4121
4122 {
4123 /* Check method qualifiers are ignored. */
4124 static const char expected[] = "ns::foo<char*>";
4125 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4126 symbol_name_match_type::FULL, true, EXPECT (expected));
4127 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4128 symbol_name_match_type::FULL, true, EXPECT (expected));
4129 CHECK_MATCH ("foo < char * > ( int ) const",
4130 symbol_name_match_type::WILD, true, EXPECT (expected));
4131 CHECK_MATCH ("foo < char * > ( int ) &&",
4132 symbol_name_match_type::WILD, true, EXPECT (expected));
4133 }
4134
4135 /* Test lookup names that don't match anything. */
4136 {
4137 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4138 {});
4139
4140 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4141 {});
4142 }
4143
4144 /* Some wild matching tests, exercising "(anonymous namespace)",
4145 which should not be confused with a parameter list. */
4146 {
4147 static const char *syms[] = {
4148 "A::B::C",
4149 "B::C",
4150 "C",
4151 "A :: B :: C ( int )",
4152 "B :: C ( int )",
4153 "C ( int )",
4154 };
4155
4156 for (const char *s : syms)
4157 {
4158 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4159 EXPECT ("(anonymous namespace)::A::B::C"));
4160 }
4161 }
4162
4163 {
4164 static const char expected[] = "ns2::tmpl<int>::foo2";
4165 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4166 EXPECT (expected));
4167 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4168 EXPECT (expected));
4169 }
4170
4171 SELF_CHECK (!any_mismatch);
4172
4173 #undef EXPECT
4174 #undef CHECK_MATCH
4175 }
4176
4177 static void
4178 run_test ()
4179 {
4180 test_mapped_index_find_name_component_bounds ();
4181 test_dw2_expand_symtabs_matching_symbol ();
4182 }
4183
4184 }} // namespace selftests::dw2_expand_symtabs_matching
4185
4186 #endif /* GDB_SELF_TEST */
4187
4188 /* If FILE_MATCHER is NULL or if PER_CU has
4189 dwarf2_per_cu_quick_data::MARK set (see
4190 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4191 EXPANSION_NOTIFY on it. */
4192
4193 static bool
4194 dw2_expand_symtabs_matching_one
4195 (dwarf2_per_cu_data *per_cu,
4196 dwarf2_per_objfile *per_objfile,
4197 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4198 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4199 {
4200 if (file_matcher == NULL || per_cu->v.quick->mark)
4201 {
4202 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4203
4204 compunit_symtab *symtab
4205 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4206 gdb_assert (symtab != nullptr);
4207
4208 if (expansion_notify != NULL && symtab_was_null)
4209 return expansion_notify (symtab);
4210 }
4211 return true;
4212 }
4213
4214 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4215 matched, to expand corresponding CUs that were marked. IDX is the
4216 index of the symbol name that matched. */
4217
4218 static bool
4219 dw2_expand_marked_cus
4220 (dwarf2_per_objfile *per_objfile, offset_type idx,
4221 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4222 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4223 block_search_flags search_flags,
4224 search_domain kind)
4225 {
4226 offset_type vec_len, vec_idx;
4227 bool global_seen = false;
4228 mapped_index &index = *per_objfile->per_bfd->index_table;
4229
4230 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4231 vec_len = vec[0];
4232 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4233 {
4234 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4235 /* This value is only valid for index versions >= 7. */
4236 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4237 gdb_index_symbol_kind symbol_kind =
4238 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4239 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4240 /* Only check the symbol attributes if they're present.
4241 Indices prior to version 7 don't record them,
4242 and indices >= 7 may elide them for certain symbols
4243 (gold does this). */
4244 int attrs_valid =
4245 (index.version >= 7
4246 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4247
4248 /* Work around gold/15646. */
4249 if (attrs_valid
4250 && !is_static
4251 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4252 {
4253 if (global_seen)
4254 continue;
4255
4256 global_seen = true;
4257 }
4258
4259 /* Only check the symbol's kind if it has one. */
4260 if (attrs_valid)
4261 {
4262 if (is_static)
4263 {
4264 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4265 continue;
4266 }
4267 else
4268 {
4269 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4270 continue;
4271 }
4272
4273 switch (kind)
4274 {
4275 case VARIABLES_DOMAIN:
4276 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4277 continue;
4278 break;
4279 case FUNCTIONS_DOMAIN:
4280 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4281 continue;
4282 break;
4283 case TYPES_DOMAIN:
4284 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4285 continue;
4286 break;
4287 case MODULES_DOMAIN:
4288 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4289 continue;
4290 break;
4291 default:
4292 break;
4293 }
4294 }
4295
4296 /* Don't crash on bad data. */
4297 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4298 {
4299 complaint (_(".gdb_index entry has bad CU index"
4300 " [in module %s]"), objfile_name (per_objfile->objfile));
4301 continue;
4302 }
4303
4304 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4305 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4306 expansion_notify))
4307 return false;
4308 }
4309
4310 return true;
4311 }
4312
4313 /* If FILE_MATCHER is non-NULL, set all the
4314 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4315 that match FILE_MATCHER. */
4316
4317 static void
4318 dw_expand_symtabs_matching_file_matcher
4319 (dwarf2_per_objfile *per_objfile,
4320 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4321 {
4322 if (file_matcher == NULL)
4323 return;
4324
4325 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4326 htab_eq_pointer,
4327 NULL, xcalloc, xfree));
4328 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4329 htab_eq_pointer,
4330 NULL, xcalloc, xfree));
4331
4332 /* The rule is CUs specify all the files, including those used by
4333 any TU, so there's no need to scan TUs here. */
4334
4335 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4336 {
4337 QUIT;
4338
4339 if (per_cu->is_debug_types)
4340 continue;
4341 per_cu->v.quick->mark = 0;
4342
4343 /* We only need to look at symtabs not already expanded. */
4344 if (per_objfile->symtab_set_p (per_cu.get ()))
4345 continue;
4346
4347 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4348 per_objfile);
4349 if (file_data == NULL)
4350 continue;
4351
4352 if (htab_find (visited_not_found.get (), file_data) != NULL)
4353 continue;
4354 else if (htab_find (visited_found.get (), file_data) != NULL)
4355 {
4356 per_cu->v.quick->mark = 1;
4357 continue;
4358 }
4359
4360 for (int j = 0; j < file_data->num_file_names; ++j)
4361 {
4362 const char *this_real_name;
4363
4364 if (file_matcher (file_data->file_names[j], false))
4365 {
4366 per_cu->v.quick->mark = 1;
4367 break;
4368 }
4369
4370 /* Before we invoke realpath, which can get expensive when many
4371 files are involved, do a quick comparison of the basenames. */
4372 if (!basenames_may_differ
4373 && !file_matcher (lbasename (file_data->file_names[j]),
4374 true))
4375 continue;
4376
4377 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4378 if (file_matcher (this_real_name, false))
4379 {
4380 per_cu->v.quick->mark = 1;
4381 break;
4382 }
4383 }
4384
4385 void **slot = htab_find_slot (per_cu->v.quick->mark
4386 ? visited_found.get ()
4387 : visited_not_found.get (),
4388 file_data, INSERT);
4389 *slot = file_data;
4390 }
4391 }
4392
4393 bool
4394 dwarf2_gdb_index::expand_symtabs_matching
4395 (struct objfile *objfile,
4396 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4397 const lookup_name_info *lookup_name,
4398 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4399 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4400 block_search_flags search_flags,
4401 domain_enum domain,
4402 enum search_domain kind)
4403 {
4404 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4405
4406 /* index_table is NULL if OBJF_READNOW. */
4407 if (!per_objfile->per_bfd->index_table)
4408 return true;
4409
4410 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4411
4412 /* This invariant is documented in quick-functions.h. */
4413 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4414 if (lookup_name == nullptr)
4415 {
4416 for (dwarf2_per_cu_data *per_cu
4417 : all_comp_units_range (per_objfile->per_bfd))
4418 {
4419 QUIT;
4420
4421 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4422 file_matcher,
4423 expansion_notify))
4424 return false;
4425 }
4426 return true;
4427 }
4428
4429 mapped_index &index = *per_objfile->per_bfd->index_table;
4430
4431 bool result
4432 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4433 symbol_matcher,
4434 [&] (offset_type idx)
4435 {
4436 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4437 expansion_notify, search_flags, kind))
4438 return false;
4439 return true;
4440 }, per_objfile);
4441
4442 return result;
4443 }
4444
4445 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4446 symtab. */
4447
4448 static struct compunit_symtab *
4449 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4450 CORE_ADDR pc)
4451 {
4452 int i;
4453
4454 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4455 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4456 return cust;
4457
4458 if (cust->includes == NULL)
4459 return NULL;
4460
4461 for (i = 0; cust->includes[i]; ++i)
4462 {
4463 struct compunit_symtab *s = cust->includes[i];
4464
4465 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4466 if (s != NULL)
4467 return s;
4468 }
4469
4470 return NULL;
4471 }
4472
4473 struct compunit_symtab *
4474 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4475 (struct objfile *objfile,
4476 struct bound_minimal_symbol msymbol,
4477 CORE_ADDR pc,
4478 struct obj_section *section,
4479 int warn_if_readin)
4480 {
4481 struct dwarf2_per_cu_data *data;
4482 struct compunit_symtab *result;
4483
4484 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4485 if (per_objfile->per_bfd->index_addrmap == nullptr)
4486 return NULL;
4487
4488 CORE_ADDR baseaddr = objfile->text_section_offset ();
4489 data = ((struct dwarf2_per_cu_data *)
4490 addrmap_find (per_objfile->per_bfd->index_addrmap,
4491 pc - baseaddr));
4492 if (!data)
4493 return NULL;
4494
4495 if (warn_if_readin && per_objfile->symtab_set_p (data))
4496 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4497 paddress (objfile->arch (), pc));
4498
4499 result = recursively_find_pc_sect_compunit_symtab
4500 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4501
4502 gdb_assert (result != NULL);
4503 return result;
4504 }
4505
4506 void
4507 dwarf2_base_index_functions::map_symbol_filenames
4508 (struct objfile *objfile,
4509 gdb::function_view<symbol_filename_ftype> fun,
4510 bool need_fullname)
4511 {
4512 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4513
4514 /* Use caches to ensure we only call FUN once for each filename. */
4515 filename_seen_cache filenames_cache;
4516 std::unordered_set<quick_file_names *> qfn_cache;
4517
4518 /* The rule is CUs specify all the files, including those used by any TU,
4519 so there's no need to scan TUs here. We can ignore file names coming
4520 from already-expanded CUs. It is possible that an expanded CU might
4521 reuse the file names data from a currently unexpanded CU, in this
4522 case we don't want to report the files from the unexpanded CU. */
4523
4524 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4525 {
4526 if (!per_cu->is_debug_types
4527 && per_objfile->symtab_set_p (per_cu.get ()))
4528 {
4529 if (per_cu->v.quick->file_names != nullptr)
4530 qfn_cache.insert (per_cu->v.quick->file_names);
4531 }
4532 }
4533
4534 for (dwarf2_per_cu_data *per_cu
4535 : all_comp_units_range (per_objfile->per_bfd))
4536 {
4537 /* We only need to look at symtabs not already expanded. */
4538 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4539 continue;
4540
4541 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4542 if (file_data == nullptr
4543 || qfn_cache.find (file_data) != qfn_cache.end ())
4544 continue;
4545
4546 for (int j = 0; j < file_data->num_file_names; ++j)
4547 {
4548 const char *filename = file_data->file_names[j];
4549 const char *key = filename;
4550 const char *fullname = nullptr;
4551
4552 if (need_fullname)
4553 {
4554 fullname = dw2_get_real_path (per_objfile, file_data, j);
4555 key = fullname;
4556 }
4557
4558 if (!filenames_cache.seen (key))
4559 fun (filename, fullname);
4560 }
4561 }
4562 }
4563
4564 bool
4565 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4566 {
4567 return true;
4568 }
4569
4570 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4571
4572 bool
4573 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4574 {
4575 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4576
4577 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4578 {
4579 /* Is this already expanded? */
4580 if (per_objfile->symtab_set_p (per_cu.get ()))
4581 continue;
4582
4583 /* It has not yet been expanded. */
4584 return true;
4585 }
4586
4587 return false;
4588 }
4589
4590 /* DWARF-5 debug_names reader. */
4591
4592 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4593 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4594
4595 /* A helper function that reads the .debug_names section in SECTION
4596 and fills in MAP. FILENAME is the name of the file containing the
4597 section; it is used for error reporting.
4598
4599 Returns true if all went well, false otherwise. */
4600
4601 static bool
4602 read_debug_names_from_section (struct objfile *objfile,
4603 const char *filename,
4604 struct dwarf2_section_info *section,
4605 mapped_debug_names &map)
4606 {
4607 if (section->empty ())
4608 return false;
4609
4610 /* Older elfutils strip versions could keep the section in the main
4611 executable while splitting it for the separate debug info file. */
4612 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4613 return false;
4614
4615 section->read (objfile);
4616
4617 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4618
4619 const gdb_byte *addr = section->buffer;
4620
4621 bfd *const abfd = section->get_bfd_owner ();
4622
4623 unsigned int bytes_read;
4624 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4625 addr += bytes_read;
4626
4627 map.dwarf5_is_dwarf64 = bytes_read != 4;
4628 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4629 if (bytes_read + length != section->size)
4630 {
4631 /* There may be multiple per-CU indices. */
4632 warning (_("Section .debug_names in %s length %s does not match "
4633 "section length %s, ignoring .debug_names."),
4634 filename, plongest (bytes_read + length),
4635 pulongest (section->size));
4636 return false;
4637 }
4638
4639 /* The version number. */
4640 uint16_t version = read_2_bytes (abfd, addr);
4641 addr += 2;
4642 if (version != 5)
4643 {
4644 warning (_("Section .debug_names in %s has unsupported version %d, "
4645 "ignoring .debug_names."),
4646 filename, version);
4647 return false;
4648 }
4649
4650 /* Padding. */
4651 uint16_t padding = read_2_bytes (abfd, addr);
4652 addr += 2;
4653 if (padding != 0)
4654 {
4655 warning (_("Section .debug_names in %s has unsupported padding %d, "
4656 "ignoring .debug_names."),
4657 filename, padding);
4658 return false;
4659 }
4660
4661 /* comp_unit_count - The number of CUs in the CU list. */
4662 map.cu_count = read_4_bytes (abfd, addr);
4663 addr += 4;
4664
4665 /* local_type_unit_count - The number of TUs in the local TU
4666 list. */
4667 map.tu_count = read_4_bytes (abfd, addr);
4668 addr += 4;
4669
4670 /* foreign_type_unit_count - The number of TUs in the foreign TU
4671 list. */
4672 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4673 addr += 4;
4674 if (foreign_tu_count != 0)
4675 {
4676 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4677 "ignoring .debug_names."),
4678 filename, static_cast<unsigned long> (foreign_tu_count));
4679 return false;
4680 }
4681
4682 /* bucket_count - The number of hash buckets in the hash lookup
4683 table. */
4684 map.bucket_count = read_4_bytes (abfd, addr);
4685 addr += 4;
4686
4687 /* name_count - The number of unique names in the index. */
4688 map.name_count = read_4_bytes (abfd, addr);
4689 addr += 4;
4690
4691 /* abbrev_table_size - The size in bytes of the abbreviations
4692 table. */
4693 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4694 addr += 4;
4695
4696 /* augmentation_string_size - The size in bytes of the augmentation
4697 string. This value is rounded up to a multiple of 4. */
4698 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4699 addr += 4;
4700 map.augmentation_is_gdb = ((augmentation_string_size
4701 == sizeof (dwarf5_augmentation))
4702 && memcmp (addr, dwarf5_augmentation,
4703 sizeof (dwarf5_augmentation)) == 0);
4704 augmentation_string_size += (-augmentation_string_size) & 3;
4705 addr += augmentation_string_size;
4706
4707 /* List of CUs */
4708 map.cu_table_reordered = addr;
4709 addr += map.cu_count * map.offset_size;
4710
4711 /* List of Local TUs */
4712 map.tu_table_reordered = addr;
4713 addr += map.tu_count * map.offset_size;
4714
4715 /* Hash Lookup Table */
4716 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4717 addr += map.bucket_count * 4;
4718 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4719 addr += map.name_count * 4;
4720
4721 /* Name Table */
4722 map.name_table_string_offs_reordered = addr;
4723 addr += map.name_count * map.offset_size;
4724 map.name_table_entry_offs_reordered = addr;
4725 addr += map.name_count * map.offset_size;
4726
4727 const gdb_byte *abbrev_table_start = addr;
4728 for (;;)
4729 {
4730 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4731 addr += bytes_read;
4732 if (index_num == 0)
4733 break;
4734
4735 const auto insertpair
4736 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4737 if (!insertpair.second)
4738 {
4739 warning (_("Section .debug_names in %s has duplicate index %s, "
4740 "ignoring .debug_names."),
4741 filename, pulongest (index_num));
4742 return false;
4743 }
4744 mapped_debug_names::index_val &indexval = insertpair.first->second;
4745 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4746 addr += bytes_read;
4747
4748 for (;;)
4749 {
4750 mapped_debug_names::index_val::attr attr;
4751 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4752 addr += bytes_read;
4753 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4754 addr += bytes_read;
4755 if (attr.form == DW_FORM_implicit_const)
4756 {
4757 attr.implicit_const = read_signed_leb128 (abfd, addr,
4758 &bytes_read);
4759 addr += bytes_read;
4760 }
4761 if (attr.dw_idx == 0 && attr.form == 0)
4762 break;
4763 indexval.attr_vec.push_back (std::move (attr));
4764 }
4765 }
4766 if (addr != abbrev_table_start + abbrev_table_size)
4767 {
4768 warning (_("Section .debug_names in %s has abbreviation_table "
4769 "of size %s vs. written as %u, ignoring .debug_names."),
4770 filename, plongest (addr - abbrev_table_start),
4771 abbrev_table_size);
4772 return false;
4773 }
4774 map.entry_pool = addr;
4775
4776 return true;
4777 }
4778
4779 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4780 list. */
4781
4782 static void
4783 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4784 const mapped_debug_names &map,
4785 dwarf2_section_info &section,
4786 bool is_dwz)
4787 {
4788 if (!map.augmentation_is_gdb)
4789 {
4790 for (uint32_t i = 0; i < map.cu_count; ++i)
4791 {
4792 sect_offset sect_off
4793 = (sect_offset) (extract_unsigned_integer
4794 (map.cu_table_reordered + i * map.offset_size,
4795 map.offset_size,
4796 map.dwarf5_byte_order));
4797 /* We don't know the length of the CU, because the CU list in a
4798 .debug_names index can be incomplete, so we can't use the start
4799 of the next CU as end of this CU. We create the CUs here with
4800 length 0, and in cutu_reader::cutu_reader we'll fill in the
4801 actual length. */
4802 dwarf2_per_cu_data_up per_cu
4803 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4804 sect_off, 0);
4805 per_bfd->all_comp_units.push_back (std::move (per_cu));
4806 }
4807 return;
4808 }
4809
4810 sect_offset sect_off_prev;
4811 for (uint32_t i = 0; i <= map.cu_count; ++i)
4812 {
4813 sect_offset sect_off_next;
4814 if (i < map.cu_count)
4815 {
4816 sect_off_next
4817 = (sect_offset) (extract_unsigned_integer
4818 (map.cu_table_reordered + i * map.offset_size,
4819 map.offset_size,
4820 map.dwarf5_byte_order));
4821 }
4822 else
4823 sect_off_next = (sect_offset) section.size;
4824 if (i >= 1)
4825 {
4826 const ULONGEST length = sect_off_next - sect_off_prev;
4827 dwarf2_per_cu_data_up per_cu
4828 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4829 sect_off_prev, length);
4830 per_bfd->all_comp_units.push_back (std::move (per_cu));
4831 }
4832 sect_off_prev = sect_off_next;
4833 }
4834 }
4835
4836 /* Read the CU list from the mapped index, and use it to create all
4837 the CU objects for this dwarf2_per_objfile. */
4838
4839 static void
4840 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4841 const mapped_debug_names &map,
4842 const mapped_debug_names &dwz_map)
4843 {
4844 gdb_assert (per_bfd->all_comp_units.empty ());
4845 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4846
4847 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4848 false /* is_dwz */);
4849
4850 if (dwz_map.cu_count == 0)
4851 return;
4852
4853 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4854 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4855 true /* is_dwz */);
4856 }
4857
4858 /* Read .debug_names. If everything went ok, initialize the "quick"
4859 elements of all the CUs and return true. Otherwise, return false. */
4860
4861 static bool
4862 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4863 {
4864 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4865 mapped_debug_names dwz_map;
4866 struct objfile *objfile = per_objfile->objfile;
4867 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4868
4869 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4870 &per_bfd->debug_names, *map))
4871 return false;
4872
4873 /* Don't use the index if it's empty. */
4874 if (map->name_count == 0)
4875 return false;
4876
4877 /* If there is a .dwz file, read it so we can get its CU list as
4878 well. */
4879 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4880 if (dwz != NULL)
4881 {
4882 if (!read_debug_names_from_section (objfile,
4883 bfd_get_filename (dwz->dwz_bfd.get ()),
4884 &dwz->debug_names, dwz_map))
4885 {
4886 warning (_("could not read '.debug_names' section from %s; skipping"),
4887 bfd_get_filename (dwz->dwz_bfd.get ()));
4888 return false;
4889 }
4890 }
4891
4892 create_cus_from_debug_names (per_bfd, *map, dwz_map);
4893
4894 if (map->tu_count != 0)
4895 {
4896 /* We can only handle a single .debug_types when we have an
4897 index. */
4898 if (per_bfd->types.size () != 1)
4899 return false;
4900
4901 dwarf2_section_info *section = &per_bfd->types[0];
4902
4903 create_signatured_type_table_from_debug_names
4904 (per_objfile, *map, section, &per_bfd->abbrev);
4905 }
4906
4907 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4908
4909 per_bfd->debug_names_table = std::move (map);
4910 per_bfd->using_index = 1;
4911 per_bfd->quick_file_names_table =
4912 create_quick_file_names_table (per_bfd->all_comp_units.size ());
4913
4914 return true;
4915 }
4916
4917 /* Type used to manage iterating over all CUs looking for a symbol for
4918 .debug_names. */
4919
4920 class dw2_debug_names_iterator
4921 {
4922 public:
4923 dw2_debug_names_iterator (const mapped_debug_names &map,
4924 block_search_flags block_index,
4925 domain_enum domain,
4926 const char *name, dwarf2_per_objfile *per_objfile)
4927 : m_map (map), m_block_index (block_index), m_domain (domain),
4928 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4929 m_per_objfile (per_objfile)
4930 {}
4931
4932 dw2_debug_names_iterator (const mapped_debug_names &map,
4933 search_domain search, uint32_t namei,
4934 dwarf2_per_objfile *per_objfile,
4935 domain_enum domain = UNDEF_DOMAIN)
4936 : m_map (map),
4937 m_domain (domain),
4938 m_search (search),
4939 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4940 m_per_objfile (per_objfile)
4941 {}
4942
4943 dw2_debug_names_iterator (const mapped_debug_names &map,
4944 block_search_flags block_index, domain_enum domain,
4945 uint32_t namei, dwarf2_per_objfile *per_objfile)
4946 : m_map (map), m_block_index (block_index), m_domain (domain),
4947 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4948 m_per_objfile (per_objfile)
4949 {}
4950
4951 /* Return the next matching CU or NULL if there are no more. */
4952 dwarf2_per_cu_data *next ();
4953
4954 private:
4955 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4956 const char *name,
4957 dwarf2_per_objfile *per_objfile);
4958 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4959 uint32_t namei,
4960 dwarf2_per_objfile *per_objfile);
4961
4962 /* The internalized form of .debug_names. */
4963 const mapped_debug_names &m_map;
4964
4965 /* Restrict the search to these blocks. */
4966 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4967 | SEARCH_STATIC_BLOCK);
4968
4969 /* The kind of symbol we're looking for. */
4970 const domain_enum m_domain = UNDEF_DOMAIN;
4971 const search_domain m_search = ALL_DOMAIN;
4972
4973 /* The list of CUs from the index entry of the symbol, or NULL if
4974 not found. */
4975 const gdb_byte *m_addr;
4976
4977 dwarf2_per_objfile *m_per_objfile;
4978 };
4979
4980 const char *
4981 mapped_debug_names::namei_to_name
4982 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4983 {
4984 const ULONGEST namei_string_offs
4985 = extract_unsigned_integer ((name_table_string_offs_reordered
4986 + namei * offset_size),
4987 offset_size,
4988 dwarf5_byte_order);
4989 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4990 }
4991
4992 /* Find a slot in .debug_names for the object named NAME. If NAME is
4993 found, return pointer to its pool data. If NAME cannot be found,
4994 return NULL. */
4995
4996 const gdb_byte *
4997 dw2_debug_names_iterator::find_vec_in_debug_names
4998 (const mapped_debug_names &map, const char *name,
4999 dwarf2_per_objfile *per_objfile)
5000 {
5001 int (*cmp) (const char *, const char *);
5002
5003 gdb::unique_xmalloc_ptr<char> without_params;
5004 if (current_language->la_language == language_cplus
5005 || current_language->la_language == language_fortran
5006 || current_language->la_language == language_d)
5007 {
5008 /* NAME is already canonical. Drop any qualifiers as
5009 .debug_names does not contain any. */
5010
5011 if (strchr (name, '(') != NULL)
5012 {
5013 without_params = cp_remove_params (name);
5014 if (without_params != NULL)
5015 name = without_params.get ();
5016 }
5017 }
5018
5019 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5020
5021 const uint32_t full_hash = dwarf5_djb_hash (name);
5022 uint32_t namei
5023 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5024 (map.bucket_table_reordered
5025 + (full_hash % map.bucket_count)), 4,
5026 map.dwarf5_byte_order);
5027 if (namei == 0)
5028 return NULL;
5029 --namei;
5030 if (namei >= map.name_count)
5031 {
5032 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5033 "[in module %s]"),
5034 namei, map.name_count,
5035 objfile_name (per_objfile->objfile));
5036 return NULL;
5037 }
5038
5039 for (;;)
5040 {
5041 const uint32_t namei_full_hash
5042 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5043 (map.hash_table_reordered + namei), 4,
5044 map.dwarf5_byte_order);
5045 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5046 return NULL;
5047
5048 if (full_hash == namei_full_hash)
5049 {
5050 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5051
5052 #if 0 /* An expensive sanity check. */
5053 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5054 {
5055 complaint (_("Wrong .debug_names hash for string at index %u "
5056 "[in module %s]"),
5057 namei, objfile_name (dwarf2_per_objfile->objfile));
5058 return NULL;
5059 }
5060 #endif
5061
5062 if (cmp (namei_string, name) == 0)
5063 {
5064 const ULONGEST namei_entry_offs
5065 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5066 + namei * map.offset_size),
5067 map.offset_size, map.dwarf5_byte_order);
5068 return map.entry_pool + namei_entry_offs;
5069 }
5070 }
5071
5072 ++namei;
5073 if (namei >= map.name_count)
5074 return NULL;
5075 }
5076 }
5077
5078 const gdb_byte *
5079 dw2_debug_names_iterator::find_vec_in_debug_names
5080 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5081 {
5082 if (namei >= map.name_count)
5083 {
5084 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5085 "[in module %s]"),
5086 namei, map.name_count,
5087 objfile_name (per_objfile->objfile));
5088 return NULL;
5089 }
5090
5091 const ULONGEST namei_entry_offs
5092 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5093 + namei * map.offset_size),
5094 map.offset_size, map.dwarf5_byte_order);
5095 return map.entry_pool + namei_entry_offs;
5096 }
5097
5098 /* See dw2_debug_names_iterator. */
5099
5100 dwarf2_per_cu_data *
5101 dw2_debug_names_iterator::next ()
5102 {
5103 if (m_addr == NULL)
5104 return NULL;
5105
5106 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5107 struct objfile *objfile = m_per_objfile->objfile;
5108 bfd *const abfd = objfile->obfd;
5109
5110 again:
5111
5112 unsigned int bytes_read;
5113 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5114 m_addr += bytes_read;
5115 if (abbrev == 0)
5116 return NULL;
5117
5118 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5119 if (indexval_it == m_map.abbrev_map.cend ())
5120 {
5121 complaint (_("Wrong .debug_names undefined abbrev code %s "
5122 "[in module %s]"),
5123 pulongest (abbrev), objfile_name (objfile));
5124 return NULL;
5125 }
5126 const mapped_debug_names::index_val &indexval = indexval_it->second;
5127 enum class symbol_linkage {
5128 unknown,
5129 static_,
5130 extern_,
5131 } symbol_linkage_ = symbol_linkage::unknown;
5132 dwarf2_per_cu_data *per_cu = NULL;
5133 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5134 {
5135 ULONGEST ull;
5136 switch (attr.form)
5137 {
5138 case DW_FORM_implicit_const:
5139 ull = attr.implicit_const;
5140 break;
5141 case DW_FORM_flag_present:
5142 ull = 1;
5143 break;
5144 case DW_FORM_udata:
5145 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5146 m_addr += bytes_read;
5147 break;
5148 case DW_FORM_ref4:
5149 ull = read_4_bytes (abfd, m_addr);
5150 m_addr += 4;
5151 break;
5152 case DW_FORM_ref8:
5153 ull = read_8_bytes (abfd, m_addr);
5154 m_addr += 8;
5155 break;
5156 case DW_FORM_ref_sig8:
5157 ull = read_8_bytes (abfd, m_addr);
5158 m_addr += 8;
5159 break;
5160 default:
5161 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5162 dwarf_form_name (attr.form),
5163 objfile_name (objfile));
5164 return NULL;
5165 }
5166 switch (attr.dw_idx)
5167 {
5168 case DW_IDX_compile_unit:
5169 /* Don't crash on bad data. */
5170 if (ull >= per_bfd->all_comp_units.size ())
5171 {
5172 complaint (_(".debug_names entry has bad CU index %s"
5173 " [in module %s]"),
5174 pulongest (ull),
5175 objfile_name (objfile));
5176 continue;
5177 }
5178 per_cu = per_bfd->get_cu (ull);
5179 break;
5180 case DW_IDX_type_unit:
5181 /* Don't crash on bad data. */
5182 if (ull >= per_bfd->tu_stats.nr_tus)
5183 {
5184 complaint (_(".debug_names entry has bad TU index %s"
5185 " [in module %s]"),
5186 pulongest (ull),
5187 objfile_name (objfile));
5188 continue;
5189 }
5190 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5191 break;
5192 case DW_IDX_die_offset:
5193 /* In a per-CU index (as opposed to a per-module index), index
5194 entries without CU attribute implicitly refer to the single CU. */
5195 if (per_cu == NULL)
5196 per_cu = per_bfd->get_cu (0);
5197 break;
5198 case DW_IDX_GNU_internal:
5199 if (!m_map.augmentation_is_gdb)
5200 break;
5201 symbol_linkage_ = symbol_linkage::static_;
5202 break;
5203 case DW_IDX_GNU_external:
5204 if (!m_map.augmentation_is_gdb)
5205 break;
5206 symbol_linkage_ = symbol_linkage::extern_;
5207 break;
5208 }
5209 }
5210
5211 /* Skip if already read in. */
5212 if (m_per_objfile->symtab_set_p (per_cu))
5213 goto again;
5214
5215 /* Check static vs global. */
5216 if (symbol_linkage_ != symbol_linkage::unknown)
5217 {
5218 if (symbol_linkage_ == symbol_linkage::static_)
5219 {
5220 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5221 goto again;
5222 }
5223 else
5224 {
5225 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5226 goto again;
5227 }
5228 }
5229
5230 /* Match dw2_symtab_iter_next, symbol_kind
5231 and debug_names::psymbol_tag. */
5232 switch (m_domain)
5233 {
5234 case VAR_DOMAIN:
5235 switch (indexval.dwarf_tag)
5236 {
5237 case DW_TAG_variable:
5238 case DW_TAG_subprogram:
5239 /* Some types are also in VAR_DOMAIN. */
5240 case DW_TAG_typedef:
5241 case DW_TAG_structure_type:
5242 break;
5243 default:
5244 goto again;
5245 }
5246 break;
5247 case STRUCT_DOMAIN:
5248 switch (indexval.dwarf_tag)
5249 {
5250 case DW_TAG_typedef:
5251 case DW_TAG_structure_type:
5252 break;
5253 default:
5254 goto again;
5255 }
5256 break;
5257 case LABEL_DOMAIN:
5258 switch (indexval.dwarf_tag)
5259 {
5260 case 0:
5261 case DW_TAG_variable:
5262 break;
5263 default:
5264 goto again;
5265 }
5266 break;
5267 case MODULE_DOMAIN:
5268 switch (indexval.dwarf_tag)
5269 {
5270 case DW_TAG_module:
5271 break;
5272 default:
5273 goto again;
5274 }
5275 break;
5276 default:
5277 break;
5278 }
5279
5280 /* Match dw2_expand_symtabs_matching, symbol_kind and
5281 debug_names::psymbol_tag. */
5282 switch (m_search)
5283 {
5284 case VARIABLES_DOMAIN:
5285 switch (indexval.dwarf_tag)
5286 {
5287 case DW_TAG_variable:
5288 break;
5289 default:
5290 goto again;
5291 }
5292 break;
5293 case FUNCTIONS_DOMAIN:
5294 switch (indexval.dwarf_tag)
5295 {
5296 case DW_TAG_subprogram:
5297 break;
5298 default:
5299 goto again;
5300 }
5301 break;
5302 case TYPES_DOMAIN:
5303 switch (indexval.dwarf_tag)
5304 {
5305 case DW_TAG_typedef:
5306 case DW_TAG_structure_type:
5307 break;
5308 default:
5309 goto again;
5310 }
5311 break;
5312 case MODULES_DOMAIN:
5313 switch (indexval.dwarf_tag)
5314 {
5315 case DW_TAG_module:
5316 break;
5317 default:
5318 goto again;
5319 }
5320 default:
5321 break;
5322 }
5323
5324 return per_cu;
5325 }
5326
5327 /* This dumps minimal information about .debug_names. It is called
5328 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5329 uses this to verify that .debug_names has been loaded. */
5330
5331 void
5332 dwarf2_debug_names_index::dump (struct objfile *objfile)
5333 {
5334 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5335
5336 gdb_assert (per_objfile->per_bfd->using_index);
5337 printf_filtered (".debug_names:");
5338 if (per_objfile->per_bfd->debug_names_table)
5339 printf_filtered (" exists\n");
5340 else
5341 printf_filtered (" faked for \"readnow\"\n");
5342 printf_filtered ("\n");
5343 }
5344
5345 void
5346 dwarf2_debug_names_index::expand_matching_symbols
5347 (struct objfile *objfile,
5348 const lookup_name_info &name, domain_enum domain,
5349 int global,
5350 symbol_compare_ftype *ordered_compare)
5351 {
5352 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5353
5354 /* debug_names_table is NULL if OBJF_READNOW. */
5355 if (!per_objfile->per_bfd->debug_names_table)
5356 return;
5357
5358 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5359 const block_search_flags block_flags
5360 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5361
5362 const char *match_name = name.ada ().lookup_name ().c_str ();
5363 auto matcher = [&] (const char *symname)
5364 {
5365 if (ordered_compare == nullptr)
5366 return true;
5367 return ordered_compare (symname, match_name) == 0;
5368 };
5369
5370 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5371 [&] (offset_type namei)
5372 {
5373 /* The name was matched, now expand corresponding CUs that were
5374 marked. */
5375 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5376 per_objfile);
5377
5378 struct dwarf2_per_cu_data *per_cu;
5379 while ((per_cu = iter.next ()) != NULL)
5380 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5381 nullptr);
5382 return true;
5383 }, per_objfile);
5384 }
5385
5386 bool
5387 dwarf2_debug_names_index::expand_symtabs_matching
5388 (struct objfile *objfile,
5389 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5390 const lookup_name_info *lookup_name,
5391 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5392 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5393 block_search_flags search_flags,
5394 domain_enum domain,
5395 enum search_domain kind)
5396 {
5397 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5398
5399 /* debug_names_table is NULL if OBJF_READNOW. */
5400 if (!per_objfile->per_bfd->debug_names_table)
5401 return true;
5402
5403 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5404
5405 /* This invariant is documented in quick-functions.h. */
5406 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5407 if (lookup_name == nullptr)
5408 {
5409 for (dwarf2_per_cu_data *per_cu
5410 : all_comp_units_range (per_objfile->per_bfd))
5411 {
5412 QUIT;
5413
5414 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5415 file_matcher,
5416 expansion_notify))
5417 return false;
5418 }
5419 return true;
5420 }
5421
5422 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5423
5424 bool result
5425 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5426 symbol_matcher,
5427 [&] (offset_type namei)
5428 {
5429 /* The name was matched, now expand corresponding CUs that were
5430 marked. */
5431 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5432
5433 struct dwarf2_per_cu_data *per_cu;
5434 while ((per_cu = iter.next ()) != NULL)
5435 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5436 file_matcher,
5437 expansion_notify))
5438 return false;
5439 return true;
5440 }, per_objfile);
5441
5442 return result;
5443 }
5444
5445 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5446 to either a dwarf2_per_bfd or dwz_file object. */
5447
5448 template <typename T>
5449 static gdb::array_view<const gdb_byte>
5450 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5451 {
5452 dwarf2_section_info *section = &section_owner->gdb_index;
5453
5454 if (section->empty ())
5455 return {};
5456
5457 /* Older elfutils strip versions could keep the section in the main
5458 executable while splitting it for the separate debug info file. */
5459 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5460 return {};
5461
5462 section->read (obj);
5463
5464 /* dwarf2_section_info::size is a bfd_size_type, while
5465 gdb::array_view works with size_t. On 32-bit hosts, with
5466 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5467 is 32-bit. So we need an explicit narrowing conversion here.
5468 This is fine, because it's impossible to allocate or mmap an
5469 array/buffer larger than what size_t can represent. */
5470 return gdb::make_array_view (section->buffer, section->size);
5471 }
5472
5473 /* Lookup the index cache for the contents of the index associated to
5474 DWARF2_OBJ. */
5475
5476 static gdb::array_view<const gdb_byte>
5477 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5478 {
5479 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5480 if (build_id == nullptr)
5481 return {};
5482
5483 return global_index_cache.lookup_gdb_index (build_id,
5484 &dwarf2_per_bfd->index_cache_res);
5485 }
5486
5487 /* Same as the above, but for DWZ. */
5488
5489 static gdb::array_view<const gdb_byte>
5490 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5491 {
5492 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5493 if (build_id == nullptr)
5494 return {};
5495
5496 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5497 }
5498
5499 /* See dwarf2/public.h. */
5500
5501 void
5502 dwarf2_initialize_objfile (struct objfile *objfile)
5503 {
5504 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5505 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5506
5507 dwarf_read_debug_printf ("called");
5508
5509 /* If we're about to read full symbols, don't bother with the
5510 indices. In this case we also don't care if some other debug
5511 format is making psymtabs, because they are all about to be
5512 expanded anyway. */
5513 if ((objfile->flags & OBJF_READNOW))
5514 {
5515 dwarf_read_debug_printf ("readnow requested");
5516
5517 /* When using READNOW, the using_index flag (set below) indicates that
5518 PER_BFD was already initialized, when we loaded some other objfile. */
5519 if (per_bfd->using_index)
5520 {
5521 dwarf_read_debug_printf ("using_index already set");
5522 objfile->qf.push_front (make_dwarf_gdb_index ());
5523 return;
5524 }
5525
5526 per_bfd->using_index = 1;
5527 create_all_comp_units (per_objfile);
5528 per_bfd->quick_file_names_table
5529 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5530
5531 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5532 {
5533 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5534
5535 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5536 struct dwarf2_per_cu_quick_data);
5537 }
5538
5539 /* Arrange for gdb to see the "quick" functions. However, these
5540 functions will be no-ops because we will have expanded all
5541 symtabs. */
5542 objfile->qf.push_front (make_dwarf_gdb_index ());
5543 return;
5544 }
5545
5546 /* Was a debug names index already read when we processed an objfile sharing
5547 PER_BFD? */
5548 if (per_bfd->debug_names_table != nullptr)
5549 {
5550 dwarf_read_debug_printf ("re-using shared debug names table");
5551 objfile->qf.push_front (make_dwarf_debug_names ());
5552 return;
5553 }
5554
5555 /* Was a GDB index already read when we processed an objfile sharing
5556 PER_BFD? */
5557 if (per_bfd->index_table != nullptr)
5558 {
5559 dwarf_read_debug_printf ("re-using shared index table");
5560 objfile->qf.push_front (make_dwarf_gdb_index ());
5561 return;
5562 }
5563
5564 /* There might already be partial symtabs built for this BFD. This happens
5565 when loading the same binary twice with the index-cache enabled. If so,
5566 don't try to read an index. The objfile / per_objfile initialization will
5567 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5568 code path. */
5569 if (per_bfd->partial_symtabs != nullptr)
5570 {
5571 dwarf_read_debug_printf ("re-using shared partial symtabs");
5572 objfile->qf.push_front (make_lazy_dwarf_reader ());
5573 return;
5574 }
5575
5576 if (dwarf2_read_debug_names (per_objfile))
5577 {
5578 dwarf_read_debug_printf ("found debug names");
5579 objfile->qf.push_front (make_dwarf_debug_names ());
5580 return;
5581 }
5582
5583 if (dwarf2_read_gdb_index (per_objfile,
5584 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5585 get_gdb_index_contents_from_section<dwz_file>))
5586 {
5587 dwarf_read_debug_printf ("found gdb index from file");
5588 objfile->qf.push_front (make_dwarf_gdb_index ());
5589 return;
5590 }
5591
5592 /* ... otherwise, try to find the index in the index cache. */
5593 if (dwarf2_read_gdb_index (per_objfile,
5594 get_gdb_index_contents_from_cache,
5595 get_gdb_index_contents_from_cache_dwz))
5596 {
5597 dwarf_read_debug_printf ("found gdb index from cache");
5598 global_index_cache.hit ();
5599 objfile->qf.push_front (make_dwarf_gdb_index ());
5600 return;
5601 }
5602
5603 global_index_cache.miss ();
5604 objfile->qf.push_front (make_lazy_dwarf_reader ());
5605 }
5606
5607 \f
5608
5609 /* Build a partial symbol table. */
5610
5611 void
5612 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5613 {
5614 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5615 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5616
5617 if (per_bfd->partial_symtabs != nullptr)
5618 {
5619 /* Partial symbols were already read, so now we can simply
5620 attach them. */
5621 if (psf == nullptr)
5622 {
5623 psf = new psymbol_functions (per_bfd->partial_symtabs);
5624 objfile->qf.emplace_front (psf);
5625 }
5626 else
5627 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5628 return;
5629 }
5630
5631 if (psf == nullptr)
5632 {
5633 psf = new psymbol_functions;
5634 objfile->qf.emplace_front (psf);
5635 }
5636 const std::shared_ptr<psymtab_storage> &partial_symtabs
5637 = psf->get_partial_symtabs ();
5638
5639 /* Set the local reference to partial symtabs, so that we don't try
5640 to read them again if reading another objfile with the same BFD.
5641 If we can't in fact share, this won't make a difference anyway as
5642 the dwarf2_per_bfd object won't be shared. */
5643 per_bfd->partial_symtabs = partial_symtabs;
5644
5645 try
5646 {
5647 /* This isn't really ideal: all the data we allocate on the
5648 objfile's obstack is still uselessly kept around. However,
5649 freeing it seems unsafe. */
5650 psymtab_discarder psymtabs (partial_symtabs.get ());
5651 dwarf2_build_psymtabs_hard (per_objfile);
5652 psymtabs.keep ();
5653
5654 /* (maybe) store an index in the cache. */
5655 global_index_cache.store (per_objfile);
5656 }
5657 catch (const gdb_exception_error &except)
5658 {
5659 exception_print (gdb_stderr, except);
5660 }
5661 }
5662
5663 /* Find the base address of the compilation unit for range lists and
5664 location lists. It will normally be specified by DW_AT_low_pc.
5665 In DWARF-3 draft 4, the base address could be overridden by
5666 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5667 compilation units with discontinuous ranges. */
5668
5669 static void
5670 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5671 {
5672 struct attribute *attr;
5673
5674 cu->base_address.reset ();
5675
5676 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5677 if (attr != nullptr)
5678 cu->base_address = attr->as_address ();
5679 else
5680 {
5681 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5682 if (attr != nullptr)
5683 cu->base_address = attr->as_address ();
5684 }
5685 }
5686
5687 /* Helper function that returns the proper abbrev section for
5688 THIS_CU. */
5689
5690 static struct dwarf2_section_info *
5691 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5692 {
5693 struct dwarf2_section_info *abbrev;
5694 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5695
5696 if (this_cu->is_dwz)
5697 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5698 else
5699 abbrev = &per_bfd->abbrev;
5700
5701 return abbrev;
5702 }
5703
5704 /* Fetch the abbreviation table offset from a comp or type unit header. */
5705
5706 static sect_offset
5707 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5708 struct dwarf2_section_info *section,
5709 sect_offset sect_off)
5710 {
5711 bfd *abfd = section->get_bfd_owner ();
5712 const gdb_byte *info_ptr;
5713 unsigned int initial_length_size, offset_size;
5714 uint16_t version;
5715
5716 section->read (per_objfile->objfile);
5717 info_ptr = section->buffer + to_underlying (sect_off);
5718 read_initial_length (abfd, info_ptr, &initial_length_size);
5719 offset_size = initial_length_size == 4 ? 4 : 8;
5720 info_ptr += initial_length_size;
5721
5722 version = read_2_bytes (abfd, info_ptr);
5723 info_ptr += 2;
5724 if (version >= 5)
5725 {
5726 /* Skip unit type and address size. */
5727 info_ptr += 2;
5728 }
5729
5730 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5731 }
5732
5733 /* A partial symtab that is used only for include files. */
5734 struct dwarf2_include_psymtab : public partial_symtab
5735 {
5736 dwarf2_include_psymtab (const char *filename,
5737 psymtab_storage *partial_symtabs,
5738 objfile_per_bfd_storage *objfile_per_bfd)
5739 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5740 {
5741 }
5742
5743 void read_symtab (struct objfile *objfile) override
5744 {
5745 /* It's an include file, no symbols to read for it.
5746 Everything is in the includer symtab. */
5747
5748 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5749 expansion of the includer psymtab. We use the dependencies[0] field to
5750 model the includer. But if we go the regular route of calling
5751 expand_psymtab here, and having expand_psymtab call expand_dependencies
5752 to expand the includer, we'll only use expand_psymtab on the includer
5753 (making it a non-toplevel psymtab), while if we expand the includer via
5754 another path, we'll use read_symtab (making it a toplevel psymtab).
5755 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5756 psymtab, and trigger read_symtab on the includer here directly. */
5757 includer ()->read_symtab (objfile);
5758 }
5759
5760 void expand_psymtab (struct objfile *objfile) override
5761 {
5762 /* This is not called by read_symtab, and should not be called by any
5763 expand_dependencies. */
5764 gdb_assert (false);
5765 }
5766
5767 bool readin_p (struct objfile *objfile) const override
5768 {
5769 return includer ()->readin_p (objfile);
5770 }
5771
5772 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5773 {
5774 compunit_symtab *cust = includer ()->get_compunit_symtab (objfile);
5775 while (cust != nullptr && cust->user != nullptr)
5776 cust = cust->user;
5777 return cust;
5778 }
5779
5780 private:
5781 partial_symtab *includer () const
5782 {
5783 /* An include psymtab has exactly one dependency: the psymtab that
5784 includes it. */
5785 gdb_assert (this->number_of_dependencies == 1);
5786 return this->dependencies[0];
5787 }
5788 };
5789
5790 /* Allocate a new partial symtab for file named NAME and mark this new
5791 partial symtab as being an include of PST. */
5792
5793 static void
5794 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5795 const char *name,
5796 dwarf2_psymtab *pst,
5797 psymtab_storage *partial_symtabs,
5798 objfile_per_bfd_storage *objfile_per_bfd)
5799 {
5800 dwarf2_include_psymtab *subpst
5801 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5802
5803 if (!IS_ABSOLUTE_PATH (subpst->filename))
5804 subpst->dirname = pst->dirname;
5805
5806 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5807 subpst->dependencies[0] = pst;
5808 subpst->number_of_dependencies = 1;
5809 }
5810
5811 /* Read the Line Number Program data and extract the list of files
5812 included by the source file represented by PST. Build an include
5813 partial symtab for each of these included files. */
5814
5815 static void
5816 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5817 struct die_info *die,
5818 const file_and_directory &fnd,
5819 dwarf2_psymtab *pst)
5820 {
5821 line_header_up lh;
5822 struct attribute *attr;
5823
5824 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5825 if (attr != nullptr && attr->form_is_unsigned ())
5826 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5827 if (lh == NULL)
5828 return; /* No linetable, so no includes. */
5829
5830 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5831 that we pass in the raw text_low here; that is ok because we're
5832 only decoding the line table to make include partial symtabs, and
5833 so the addresses aren't really used. */
5834 dwarf_decode_lines (lh.get (), fnd, cu, pst,
5835 pst->raw_text_low (), 1);
5836 }
5837
5838 static hashval_t
5839 hash_signatured_type (const void *item)
5840 {
5841 const struct signatured_type *sig_type
5842 = (const struct signatured_type *) item;
5843
5844 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5845 return sig_type->signature;
5846 }
5847
5848 static int
5849 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5850 {
5851 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5852 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5853
5854 return lhs->signature == rhs->signature;
5855 }
5856
5857 /* Allocate a hash table for signatured types. */
5858
5859 static htab_up
5860 allocate_signatured_type_table ()
5861 {
5862 return htab_up (htab_create_alloc (41,
5863 hash_signatured_type,
5864 eq_signatured_type,
5865 NULL, xcalloc, xfree));
5866 }
5867
5868 /* A helper for create_debug_types_hash_table. Read types from SECTION
5869 and fill them into TYPES_HTAB. It will process only type units,
5870 therefore DW_UT_type. */
5871
5872 static void
5873 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5874 struct dwo_file *dwo_file,
5875 dwarf2_section_info *section, htab_up &types_htab,
5876 rcuh_kind section_kind)
5877 {
5878 struct objfile *objfile = per_objfile->objfile;
5879 struct dwarf2_section_info *abbrev_section;
5880 bfd *abfd;
5881 const gdb_byte *info_ptr, *end_ptr;
5882
5883 abbrev_section = &dwo_file->sections.abbrev;
5884
5885 dwarf_read_debug_printf ("Reading %s for %s",
5886 section->get_name (),
5887 abbrev_section->get_file_name ());
5888
5889 section->read (objfile);
5890 info_ptr = section->buffer;
5891
5892 if (info_ptr == NULL)
5893 return;
5894
5895 /* We can't set abfd until now because the section may be empty or
5896 not present, in which case the bfd is unknown. */
5897 abfd = section->get_bfd_owner ();
5898
5899 /* We don't use cutu_reader here because we don't need to read
5900 any dies: the signature is in the header. */
5901
5902 end_ptr = info_ptr + section->size;
5903 while (info_ptr < end_ptr)
5904 {
5905 signatured_type_up sig_type;
5906 struct dwo_unit *dwo_tu;
5907 void **slot;
5908 const gdb_byte *ptr = info_ptr;
5909 struct comp_unit_head header;
5910 unsigned int length;
5911
5912 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5913
5914 /* Initialize it due to a false compiler warning. */
5915 header.signature = -1;
5916 header.type_cu_offset_in_tu = (cu_offset) -1;
5917
5918 /* We need to read the type's signature in order to build the hash
5919 table, but we don't need anything else just yet. */
5920
5921 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5922 abbrev_section, ptr, section_kind);
5923
5924 length = header.get_length ();
5925
5926 /* Skip dummy type units. */
5927 if (ptr >= info_ptr + length
5928 || peek_abbrev_code (abfd, ptr) == 0
5929 || (header.unit_type != DW_UT_type
5930 && header.unit_type != DW_UT_split_type))
5931 {
5932 info_ptr += length;
5933 continue;
5934 }
5935
5936 if (types_htab == NULL)
5937 types_htab = allocate_dwo_unit_table ();
5938
5939 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5940 dwo_tu->dwo_file = dwo_file;
5941 dwo_tu->signature = header.signature;
5942 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5943 dwo_tu->section = section;
5944 dwo_tu->sect_off = sect_off;
5945 dwo_tu->length = length;
5946
5947 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5948 gdb_assert (slot != NULL);
5949 if (*slot != NULL)
5950 complaint (_("debug type entry at offset %s is duplicate to"
5951 " the entry at offset %s, signature %s"),
5952 sect_offset_str (sect_off),
5953 sect_offset_str (dwo_tu->sect_off),
5954 hex_string (header.signature));
5955 *slot = dwo_tu;
5956
5957 dwarf_read_debug_printf_v (" offset %s, signature %s",
5958 sect_offset_str (sect_off),
5959 hex_string (header.signature));
5960
5961 info_ptr += length;
5962 }
5963 }
5964
5965 /* Create the hash table of all entries in the .debug_types
5966 (or .debug_types.dwo) section(s).
5967 DWO_FILE is a pointer to the DWO file object.
5968
5969 The result is a pointer to the hash table or NULL if there are no types.
5970
5971 Note: This function processes DWO files only, not DWP files. */
5972
5973 static void
5974 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5975 struct dwo_file *dwo_file,
5976 gdb::array_view<dwarf2_section_info> type_sections,
5977 htab_up &types_htab)
5978 {
5979 for (dwarf2_section_info &section : type_sections)
5980 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5981 rcuh_kind::TYPE);
5982 }
5983
5984 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5985 If SLOT is non-NULL, it is the entry to use in the hash table.
5986 Otherwise we find one. */
5987
5988 static struct signatured_type *
5989 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5990 {
5991 if (per_objfile->per_bfd->all_comp_units.size ()
5992 == per_objfile->per_bfd->all_comp_units.capacity ())
5993 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5994
5995 signatured_type_up sig_type_holder
5996 = per_objfile->per_bfd->allocate_signatured_type (sig);
5997 signatured_type *sig_type = sig_type_holder.get ();
5998
5999 per_objfile->per_bfd->all_comp_units.emplace_back
6000 (sig_type_holder.release ());
6001 if (per_objfile->per_bfd->using_index)
6002 {
6003 sig_type->v.quick =
6004 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6005 struct dwarf2_per_cu_quick_data);
6006 }
6007
6008 if (slot == NULL)
6009 {
6010 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6011 sig_type, INSERT);
6012 }
6013 gdb_assert (*slot == NULL);
6014 *slot = sig_type;
6015 /* The rest of sig_type must be filled in by the caller. */
6016 return sig_type;
6017 }
6018
6019 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6020 Fill in SIG_ENTRY with DWO_ENTRY. */
6021
6022 static void
6023 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6024 struct signatured_type *sig_entry,
6025 struct dwo_unit *dwo_entry)
6026 {
6027 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6028
6029 /* Make sure we're not clobbering something we don't expect to. */
6030 gdb_assert (! sig_entry->queued);
6031 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6032 if (per_bfd->using_index)
6033 {
6034 gdb_assert (sig_entry->v.quick != NULL);
6035 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6036 }
6037 else
6038 gdb_assert (sig_entry->v.psymtab == NULL);
6039 gdb_assert (sig_entry->signature == dwo_entry->signature);
6040 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6041 gdb_assert (sig_entry->type_unit_group == NULL);
6042 gdb_assert (sig_entry->dwo_unit == NULL);
6043
6044 sig_entry->section = dwo_entry->section;
6045 sig_entry->sect_off = dwo_entry->sect_off;
6046 sig_entry->length = dwo_entry->length;
6047 sig_entry->reading_dwo_directly = 1;
6048 sig_entry->per_bfd = per_bfd;
6049 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6050 sig_entry->dwo_unit = dwo_entry;
6051 }
6052
6053 /* Subroutine of lookup_signatured_type.
6054 If we haven't read the TU yet, create the signatured_type data structure
6055 for a TU to be read in directly from a DWO file, bypassing the stub.
6056 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6057 using .gdb_index, then when reading a CU we want to stay in the DWO file
6058 containing that CU. Otherwise we could end up reading several other DWO
6059 files (due to comdat folding) to process the transitive closure of all the
6060 mentioned TUs, and that can be slow. The current DWO file will have every
6061 type signature that it needs.
6062 We only do this for .gdb_index because in the psymtab case we already have
6063 to read all the DWOs to build the type unit groups. */
6064
6065 static struct signatured_type *
6066 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6067 {
6068 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6069 struct dwo_file *dwo_file;
6070 struct dwo_unit find_dwo_entry, *dwo_entry;
6071 void **slot;
6072
6073 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6074
6075 /* If TU skeletons have been removed then we may not have read in any
6076 TUs yet. */
6077 if (per_objfile->per_bfd->signatured_types == NULL)
6078 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6079
6080 /* We only ever need to read in one copy of a signatured type.
6081 Use the global signatured_types array to do our own comdat-folding
6082 of types. If this is the first time we're reading this TU, and
6083 the TU has an entry in .gdb_index, replace the recorded data from
6084 .gdb_index with this TU. */
6085
6086 signatured_type find_sig_entry (sig);
6087 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6088 &find_sig_entry, INSERT);
6089 signatured_type *sig_entry = (struct signatured_type *) *slot;
6090
6091 /* We can get here with the TU already read, *or* in the process of being
6092 read. Don't reassign the global entry to point to this DWO if that's
6093 the case. Also note that if the TU is already being read, it may not
6094 have come from a DWO, the program may be a mix of Fission-compiled
6095 code and non-Fission-compiled code. */
6096
6097 /* Have we already tried to read this TU?
6098 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6099 needn't exist in the global table yet). */
6100 if (sig_entry != NULL && sig_entry->tu_read)
6101 return sig_entry;
6102
6103 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6104 dwo_unit of the TU itself. */
6105 dwo_file = cu->dwo_unit->dwo_file;
6106
6107 /* Ok, this is the first time we're reading this TU. */
6108 if (dwo_file->tus == NULL)
6109 return NULL;
6110 find_dwo_entry.signature = sig;
6111 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6112 &find_dwo_entry);
6113 if (dwo_entry == NULL)
6114 return NULL;
6115
6116 /* If the global table doesn't have an entry for this TU, add one. */
6117 if (sig_entry == NULL)
6118 sig_entry = add_type_unit (per_objfile, sig, slot);
6119
6120 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6121 sig_entry->tu_read = 1;
6122 return sig_entry;
6123 }
6124
6125 /* Subroutine of lookup_signatured_type.
6126 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6127 then try the DWP file. If the TU stub (skeleton) has been removed then
6128 it won't be in .gdb_index. */
6129
6130 static struct signatured_type *
6131 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6132 {
6133 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6134 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6135 struct dwo_unit *dwo_entry;
6136 void **slot;
6137
6138 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6139 gdb_assert (dwp_file != NULL);
6140
6141 /* If TU skeletons have been removed then we may not have read in any
6142 TUs yet. */
6143 if (per_objfile->per_bfd->signatured_types == NULL)
6144 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6145
6146 signatured_type find_sig_entry (sig);
6147 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6148 &find_sig_entry, INSERT);
6149 signatured_type *sig_entry = (struct signatured_type *) *slot;
6150
6151 /* Have we already tried to read this TU?
6152 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6153 needn't exist in the global table yet). */
6154 if (sig_entry != NULL)
6155 return sig_entry;
6156
6157 if (dwp_file->tus == NULL)
6158 return NULL;
6159 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6160 1 /* is_debug_types */);
6161 if (dwo_entry == NULL)
6162 return NULL;
6163
6164 sig_entry = add_type_unit (per_objfile, sig, slot);
6165 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6166
6167 return sig_entry;
6168 }
6169
6170 /* Lookup a signature based type for DW_FORM_ref_sig8.
6171 Returns NULL if signature SIG is not present in the table.
6172 It is up to the caller to complain about this. */
6173
6174 static struct signatured_type *
6175 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6176 {
6177 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6178
6179 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6180 {
6181 /* We're in a DWO/DWP file, and we're using .gdb_index.
6182 These cases require special processing. */
6183 if (get_dwp_file (per_objfile) == NULL)
6184 return lookup_dwo_signatured_type (cu, sig);
6185 else
6186 return lookup_dwp_signatured_type (cu, sig);
6187 }
6188 else
6189 {
6190 if (per_objfile->per_bfd->signatured_types == NULL)
6191 return NULL;
6192 signatured_type find_entry (sig);
6193 return ((struct signatured_type *)
6194 htab_find (per_objfile->per_bfd->signatured_types.get (),
6195 &find_entry));
6196 }
6197 }
6198
6199 /* Low level DIE reading support. */
6200
6201 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6202
6203 static void
6204 init_cu_die_reader (struct die_reader_specs *reader,
6205 struct dwarf2_cu *cu,
6206 struct dwarf2_section_info *section,
6207 struct dwo_file *dwo_file,
6208 struct abbrev_table *abbrev_table)
6209 {
6210 gdb_assert (section->readin && section->buffer != NULL);
6211 reader->abfd = section->get_bfd_owner ();
6212 reader->cu = cu;
6213 reader->dwo_file = dwo_file;
6214 reader->die_section = section;
6215 reader->buffer = section->buffer;
6216 reader->buffer_end = section->buffer + section->size;
6217 reader->abbrev_table = abbrev_table;
6218 }
6219
6220 /* Subroutine of cutu_reader to simplify it.
6221 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6222 There's just a lot of work to do, and cutu_reader is big enough
6223 already.
6224
6225 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6226 from it to the DIE in the DWO. If NULL we are skipping the stub.
6227 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6228 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6229 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6230 STUB_COMP_DIR may be non-NULL.
6231 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6232 are filled in with the info of the DIE from the DWO file.
6233 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6234 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6235 kept around for at least as long as *RESULT_READER.
6236
6237 The result is non-zero if a valid (non-dummy) DIE was found. */
6238
6239 static int
6240 read_cutu_die_from_dwo (dwarf2_cu *cu,
6241 struct dwo_unit *dwo_unit,
6242 struct die_info *stub_comp_unit_die,
6243 const char *stub_comp_dir,
6244 struct die_reader_specs *result_reader,
6245 const gdb_byte **result_info_ptr,
6246 struct die_info **result_comp_unit_die,
6247 abbrev_table_up *result_dwo_abbrev_table)
6248 {
6249 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6250 dwarf2_per_cu_data *per_cu = cu->per_cu;
6251 struct objfile *objfile = per_objfile->objfile;
6252 bfd *abfd;
6253 const gdb_byte *begin_info_ptr, *info_ptr;
6254 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6255 int i,num_extra_attrs;
6256 struct dwarf2_section_info *dwo_abbrev_section;
6257 struct die_info *comp_unit_die;
6258
6259 /* At most one of these may be provided. */
6260 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6261
6262 /* These attributes aren't processed until later:
6263 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6264 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6265 referenced later. However, these attributes are found in the stub
6266 which we won't have later. In order to not impose this complication
6267 on the rest of the code, we read them here and copy them to the
6268 DWO CU/TU die. */
6269
6270 stmt_list = NULL;
6271 low_pc = NULL;
6272 high_pc = NULL;
6273 ranges = NULL;
6274 comp_dir = NULL;
6275
6276 if (stub_comp_unit_die != NULL)
6277 {
6278 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6279 DWO file. */
6280 if (!per_cu->is_debug_types)
6281 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6282 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6283 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6284 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6285 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6286
6287 cu->addr_base = stub_comp_unit_die->addr_base ();
6288
6289 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6290 We need the value before we can process DW_AT_ranges values from the
6291 DWO. */
6292 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6293
6294 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6295 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6296 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6297 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6298 section. */
6299 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6300 }
6301 else if (stub_comp_dir != NULL)
6302 {
6303 /* Reconstruct the comp_dir attribute to simplify the code below. */
6304 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6305 comp_dir->name = DW_AT_comp_dir;
6306 comp_dir->form = DW_FORM_string;
6307 comp_dir->set_string_noncanonical (stub_comp_dir);
6308 }
6309
6310 /* Set up for reading the DWO CU/TU. */
6311 cu->dwo_unit = dwo_unit;
6312 dwarf2_section_info *section = dwo_unit->section;
6313 section->read (objfile);
6314 abfd = section->get_bfd_owner ();
6315 begin_info_ptr = info_ptr = (section->buffer
6316 + to_underlying (dwo_unit->sect_off));
6317 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6318
6319 if (per_cu->is_debug_types)
6320 {
6321 signatured_type *sig_type = (struct signatured_type *) per_cu;
6322
6323 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6324 section, dwo_abbrev_section,
6325 info_ptr, rcuh_kind::TYPE);
6326 /* This is not an assert because it can be caused by bad debug info. */
6327 if (sig_type->signature != cu->header.signature)
6328 {
6329 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6330 " TU at offset %s [in module %s]"),
6331 hex_string (sig_type->signature),
6332 hex_string (cu->header.signature),
6333 sect_offset_str (dwo_unit->sect_off),
6334 bfd_get_filename (abfd));
6335 }
6336 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6337 /* For DWOs coming from DWP files, we don't know the CU length
6338 nor the type's offset in the TU until now. */
6339 dwo_unit->length = cu->header.get_length ();
6340 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6341
6342 /* Establish the type offset that can be used to lookup the type.
6343 For DWO files, we don't know it until now. */
6344 sig_type->type_offset_in_section
6345 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6346 }
6347 else
6348 {
6349 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6350 section, dwo_abbrev_section,
6351 info_ptr, rcuh_kind::COMPILE);
6352 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6353 /* For DWOs coming from DWP files, we don't know the CU length
6354 until now. */
6355 dwo_unit->length = cu->header.get_length ();
6356 }
6357
6358 dwo_abbrev_section->read (objfile);
6359 *result_dwo_abbrev_table
6360 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6361 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6362 result_dwo_abbrev_table->get ());
6363
6364 /* Read in the die, but leave space to copy over the attributes
6365 from the stub. This has the benefit of simplifying the rest of
6366 the code - all the work to maintain the illusion of a single
6367 DW_TAG_{compile,type}_unit DIE is done here. */
6368 num_extra_attrs = ((stmt_list != NULL)
6369 + (low_pc != NULL)
6370 + (high_pc != NULL)
6371 + (ranges != NULL)
6372 + (comp_dir != NULL));
6373 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6374 num_extra_attrs);
6375
6376 /* Copy over the attributes from the stub to the DIE we just read in. */
6377 comp_unit_die = *result_comp_unit_die;
6378 i = comp_unit_die->num_attrs;
6379 if (stmt_list != NULL)
6380 comp_unit_die->attrs[i++] = *stmt_list;
6381 if (low_pc != NULL)
6382 comp_unit_die->attrs[i++] = *low_pc;
6383 if (high_pc != NULL)
6384 comp_unit_die->attrs[i++] = *high_pc;
6385 if (ranges != NULL)
6386 comp_unit_die->attrs[i++] = *ranges;
6387 if (comp_dir != NULL)
6388 comp_unit_die->attrs[i++] = *comp_dir;
6389 comp_unit_die->num_attrs += num_extra_attrs;
6390
6391 if (dwarf_die_debug)
6392 {
6393 fprintf_unfiltered (gdb_stdlog,
6394 "Read die from %s@0x%x of %s:\n",
6395 section->get_name (),
6396 (unsigned) (begin_info_ptr - section->buffer),
6397 bfd_get_filename (abfd));
6398 dump_die (comp_unit_die, dwarf_die_debug);
6399 }
6400
6401 /* Skip dummy compilation units. */
6402 if (info_ptr >= begin_info_ptr + dwo_unit->length
6403 || peek_abbrev_code (abfd, info_ptr) == 0)
6404 return 0;
6405
6406 *result_info_ptr = info_ptr;
6407 return 1;
6408 }
6409
6410 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6411 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6412 signature is part of the header. */
6413 static gdb::optional<ULONGEST>
6414 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6415 {
6416 if (cu->header.version >= 5)
6417 return cu->header.signature;
6418 struct attribute *attr;
6419 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6420 if (attr == nullptr || !attr->form_is_unsigned ())
6421 return gdb::optional<ULONGEST> ();
6422 return attr->as_unsigned ();
6423 }
6424
6425 /* Subroutine of cutu_reader to simplify it.
6426 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6427 Returns NULL if the specified DWO unit cannot be found. */
6428
6429 static struct dwo_unit *
6430 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6431 {
6432 dwarf2_per_cu_data *per_cu = cu->per_cu;
6433 struct dwo_unit *dwo_unit;
6434 const char *comp_dir;
6435
6436 gdb_assert (cu != NULL);
6437
6438 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6439 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6440 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6441
6442 if (per_cu->is_debug_types)
6443 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6444 else
6445 {
6446 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6447
6448 if (!signature.has_value ())
6449 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6450 " [in module %s]"),
6451 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6452
6453 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6454 }
6455
6456 return dwo_unit;
6457 }
6458
6459 /* Subroutine of cutu_reader to simplify it.
6460 See it for a description of the parameters.
6461 Read a TU directly from a DWO file, bypassing the stub. */
6462
6463 void
6464 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6465 dwarf2_per_objfile *per_objfile,
6466 dwarf2_cu *existing_cu)
6467 {
6468 struct signatured_type *sig_type;
6469
6470 /* Verify we can do the following downcast, and that we have the
6471 data we need. */
6472 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6473 sig_type = (struct signatured_type *) this_cu;
6474 gdb_assert (sig_type->dwo_unit != NULL);
6475
6476 dwarf2_cu *cu;
6477
6478 if (existing_cu != nullptr)
6479 {
6480 cu = existing_cu;
6481 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6482 /* There's no need to do the rereading_dwo_cu handling that
6483 cutu_reader does since we don't read the stub. */
6484 }
6485 else
6486 {
6487 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6488 in per_objfile yet. */
6489 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6490 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6491 cu = m_new_cu.get ();
6492 }
6493
6494 /* A future optimization, if needed, would be to use an existing
6495 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6496 could share abbrev tables. */
6497
6498 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6499 NULL /* stub_comp_unit_die */,
6500 sig_type->dwo_unit->dwo_file->comp_dir,
6501 this, &info_ptr,
6502 &comp_unit_die,
6503 &m_dwo_abbrev_table) == 0)
6504 {
6505 /* Dummy die. */
6506 dummy_p = true;
6507 }
6508 }
6509
6510 /* Initialize a CU (or TU) and read its DIEs.
6511 If the CU defers to a DWO file, read the DWO file as well.
6512
6513 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6514 Otherwise the table specified in the comp unit header is read in and used.
6515 This is an optimization for when we already have the abbrev table.
6516
6517 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6518 allocated. */
6519
6520 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6521 dwarf2_per_objfile *per_objfile,
6522 struct abbrev_table *abbrev_table,
6523 dwarf2_cu *existing_cu,
6524 bool skip_partial)
6525 : die_reader_specs {},
6526 m_this_cu (this_cu)
6527 {
6528 struct objfile *objfile = per_objfile->objfile;
6529 struct dwarf2_section_info *section = this_cu->section;
6530 bfd *abfd = section->get_bfd_owner ();
6531 const gdb_byte *begin_info_ptr;
6532 struct signatured_type *sig_type = NULL;
6533 struct dwarf2_section_info *abbrev_section;
6534 /* Non-zero if CU currently points to a DWO file and we need to
6535 reread it. When this happens we need to reread the skeleton die
6536 before we can reread the DWO file (this only applies to CUs, not TUs). */
6537 int rereading_dwo_cu = 0;
6538
6539 if (dwarf_die_debug)
6540 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6541 this_cu->is_debug_types ? "type" : "comp",
6542 sect_offset_str (this_cu->sect_off));
6543
6544 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6545 file (instead of going through the stub), short-circuit all of this. */
6546 if (this_cu->reading_dwo_directly)
6547 {
6548 /* Narrow down the scope of possibilities to have to understand. */
6549 gdb_assert (this_cu->is_debug_types);
6550 gdb_assert (abbrev_table == NULL);
6551 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6552 return;
6553 }
6554
6555 /* This is cheap if the section is already read in. */
6556 section->read (objfile);
6557
6558 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6559
6560 abbrev_section = get_abbrev_section_for_cu (this_cu);
6561
6562 dwarf2_cu *cu;
6563
6564 if (existing_cu != nullptr)
6565 {
6566 cu = existing_cu;
6567 /* If this CU is from a DWO file we need to start over, we need to
6568 refetch the attributes from the skeleton CU.
6569 This could be optimized by retrieving those attributes from when we
6570 were here the first time: the previous comp_unit_die was stored in
6571 comp_unit_obstack. But there's no data yet that we need this
6572 optimization. */
6573 if (cu->dwo_unit != NULL)
6574 rereading_dwo_cu = 1;
6575 }
6576 else
6577 {
6578 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6579 in per_objfile yet. */
6580 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6581 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6582 cu = m_new_cu.get ();
6583 }
6584
6585 /* Get the header. */
6586 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6587 {
6588 /* We already have the header, there's no need to read it in again. */
6589 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6590 }
6591 else
6592 {
6593 if (this_cu->is_debug_types)
6594 {
6595 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6596 section, abbrev_section,
6597 info_ptr, rcuh_kind::TYPE);
6598
6599 /* Since per_cu is the first member of struct signatured_type,
6600 we can go from a pointer to one to a pointer to the other. */
6601 sig_type = (struct signatured_type *) this_cu;
6602 gdb_assert (sig_type->signature == cu->header.signature);
6603 gdb_assert (sig_type->type_offset_in_tu
6604 == cu->header.type_cu_offset_in_tu);
6605 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6606
6607 /* LENGTH has not been set yet for type units if we're
6608 using .gdb_index. */
6609 this_cu->length = cu->header.get_length ();
6610
6611 /* Establish the type offset that can be used to lookup the type. */
6612 sig_type->type_offset_in_section =
6613 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6614
6615 this_cu->dwarf_version = cu->header.version;
6616 }
6617 else
6618 {
6619 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6620 section, abbrev_section,
6621 info_ptr,
6622 rcuh_kind::COMPILE);
6623
6624 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6625 if (this_cu->length == 0)
6626 this_cu->length = cu->header.get_length ();
6627 else
6628 gdb_assert (this_cu->length == cu->header.get_length ());
6629 this_cu->dwarf_version = cu->header.version;
6630 }
6631 }
6632
6633 /* Skip dummy compilation units. */
6634 if (info_ptr >= begin_info_ptr + this_cu->length
6635 || peek_abbrev_code (abfd, info_ptr) == 0)
6636 {
6637 dummy_p = true;
6638 return;
6639 }
6640
6641 /* If we don't have them yet, read the abbrevs for this compilation unit.
6642 And if we need to read them now, make sure they're freed when we're
6643 done. */
6644 if (abbrev_table != NULL)
6645 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6646 else
6647 {
6648 abbrev_section->read (objfile);
6649 m_abbrev_table_holder
6650 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6651 abbrev_table = m_abbrev_table_holder.get ();
6652 }
6653
6654 /* Read the top level CU/TU die. */
6655 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6656 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6657
6658 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6659 {
6660 dummy_p = true;
6661 return;
6662 }
6663
6664 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6665 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6666 table from the DWO file and pass the ownership over to us. It will be
6667 referenced from READER, so we must make sure to free it after we're done
6668 with READER.
6669
6670 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6671 DWO CU, that this test will fail (the attribute will not be present). */
6672 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6673 if (dwo_name != nullptr)
6674 {
6675 struct dwo_unit *dwo_unit;
6676 struct die_info *dwo_comp_unit_die;
6677
6678 if (comp_unit_die->has_children)
6679 {
6680 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6681 " has children (offset %s) [in module %s]"),
6682 sect_offset_str (this_cu->sect_off),
6683 bfd_get_filename (abfd));
6684 }
6685 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6686 if (dwo_unit != NULL)
6687 {
6688 if (read_cutu_die_from_dwo (cu, dwo_unit,
6689 comp_unit_die, NULL,
6690 this, &info_ptr,
6691 &dwo_comp_unit_die,
6692 &m_dwo_abbrev_table) == 0)
6693 {
6694 /* Dummy die. */
6695 dummy_p = true;
6696 return;
6697 }
6698 comp_unit_die = dwo_comp_unit_die;
6699 }
6700 else
6701 {
6702 /* Yikes, we couldn't find the rest of the DIE, we only have
6703 the stub. A complaint has already been logged. There's
6704 not much more we can do except pass on the stub DIE to
6705 die_reader_func. We don't want to throw an error on bad
6706 debug info. */
6707 }
6708 }
6709 }
6710
6711 void
6712 cutu_reader::keep ()
6713 {
6714 /* Done, clean up. */
6715 gdb_assert (!dummy_p);
6716 if (m_new_cu != NULL)
6717 {
6718 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6719 now. */
6720 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6721 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6722 }
6723 }
6724
6725 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6726 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6727 assumed to have already done the lookup to find the DWO file).
6728
6729 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6730 THIS_CU->is_debug_types, but nothing else.
6731
6732 We fill in THIS_CU->length.
6733
6734 THIS_CU->cu is always freed when done.
6735 This is done in order to not leave THIS_CU->cu in a state where we have
6736 to care whether it refers to the "main" CU or the DWO CU.
6737
6738 When parent_cu is passed, it is used to provide a default value for
6739 str_offsets_base and addr_base from the parent. */
6740
6741 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6742 dwarf2_per_objfile *per_objfile,
6743 struct dwarf2_cu *parent_cu,
6744 struct dwo_file *dwo_file)
6745 : die_reader_specs {},
6746 m_this_cu (this_cu)
6747 {
6748 struct objfile *objfile = per_objfile->objfile;
6749 struct dwarf2_section_info *section = this_cu->section;
6750 bfd *abfd = section->get_bfd_owner ();
6751 struct dwarf2_section_info *abbrev_section;
6752 const gdb_byte *begin_info_ptr, *info_ptr;
6753
6754 if (dwarf_die_debug)
6755 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6756 this_cu->is_debug_types ? "type" : "comp",
6757 sect_offset_str (this_cu->sect_off));
6758
6759 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6760
6761 abbrev_section = (dwo_file != NULL
6762 ? &dwo_file->sections.abbrev
6763 : get_abbrev_section_for_cu (this_cu));
6764
6765 /* This is cheap if the section is already read in. */
6766 section->read (objfile);
6767
6768 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6769
6770 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6771 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6772 section, abbrev_section, info_ptr,
6773 (this_cu->is_debug_types
6774 ? rcuh_kind::TYPE
6775 : rcuh_kind::COMPILE));
6776
6777 if (parent_cu != nullptr)
6778 {
6779 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6780 m_new_cu->addr_base = parent_cu->addr_base;
6781 }
6782 this_cu->length = m_new_cu->header.get_length ();
6783
6784 /* Skip dummy compilation units. */
6785 if (info_ptr >= begin_info_ptr + this_cu->length
6786 || peek_abbrev_code (abfd, info_ptr) == 0)
6787 {
6788 dummy_p = true;
6789 return;
6790 }
6791
6792 abbrev_section->read (objfile);
6793 m_abbrev_table_holder
6794 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6795
6796 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6797 m_abbrev_table_holder.get ());
6798 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6799 }
6800
6801 \f
6802 /* Type Unit Groups.
6803
6804 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6805 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6806 so that all types coming from the same compilation (.o file) are grouped
6807 together. A future step could be to put the types in the same symtab as
6808 the CU the types ultimately came from. */
6809
6810 static hashval_t
6811 hash_type_unit_group (const void *item)
6812 {
6813 const struct type_unit_group *tu_group
6814 = (const struct type_unit_group *) item;
6815
6816 return hash_stmt_list_entry (&tu_group->hash);
6817 }
6818
6819 static int
6820 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6821 {
6822 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6823 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6824
6825 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6826 }
6827
6828 /* Allocate a hash table for type unit groups. */
6829
6830 static htab_up
6831 allocate_type_unit_groups_table ()
6832 {
6833 return htab_up (htab_create_alloc (3,
6834 hash_type_unit_group,
6835 eq_type_unit_group,
6836 htab_delete_entry<type_unit_group>,
6837 xcalloc, xfree));
6838 }
6839
6840 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6841 partial symtabs. We combine several TUs per psymtab to not let the size
6842 of any one psymtab grow too big. */
6843 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6844 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6845
6846 /* Helper routine for get_type_unit_group.
6847 Create the type_unit_group object used to hold one or more TUs. */
6848
6849 static std::unique_ptr<type_unit_group>
6850 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6851 {
6852 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6853 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6854
6855 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6856 tu_group->per_bfd = per_bfd;
6857
6858 if (per_bfd->using_index)
6859 {
6860 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6861 struct dwarf2_per_cu_quick_data);
6862 }
6863 else
6864 {
6865 unsigned int line_offset = to_underlying (line_offset_struct);
6866 dwarf2_psymtab *pst;
6867 std::string name;
6868
6869 /* Give the symtab a useful name for debug purposes. */
6870 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
6871 name = string_printf ("<type_units_%d>",
6872 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
6873 else
6874 name = string_printf ("<type_units_at_0x%x>", line_offset);
6875
6876 pst = create_partial_symtab (tu_group.get (), per_objfile,
6877 name.c_str ());
6878 pst->anonymous = true;
6879 }
6880
6881 tu_group->hash.dwo_unit = cu->dwo_unit;
6882 tu_group->hash.line_sect_off = line_offset_struct;
6883
6884 return tu_group;
6885 }
6886
6887 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6888 STMT_LIST is a DW_AT_stmt_list attribute. */
6889
6890 static struct type_unit_group *
6891 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6892 {
6893 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6894 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6895 struct type_unit_group *tu_group;
6896 void **slot;
6897 unsigned int line_offset;
6898 struct type_unit_group type_unit_group_for_lookup;
6899
6900 if (per_objfile->per_bfd->type_unit_groups == NULL)
6901 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6902
6903 /* Do we need to create a new group, or can we use an existing one? */
6904
6905 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6906 {
6907 line_offset = stmt_list->as_unsigned ();
6908 ++tu_stats->nr_symtab_sharers;
6909 }
6910 else
6911 {
6912 /* Ugh, no stmt_list. Rare, but we have to handle it.
6913 We can do various things here like create one group per TU or
6914 spread them over multiple groups to split up the expansion work.
6915 To avoid worst case scenarios (too many groups or too large groups)
6916 we, umm, group them in bunches. */
6917 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6918 | (tu_stats->nr_stmt_less_type_units
6919 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6920 ++tu_stats->nr_stmt_less_type_units;
6921 }
6922
6923 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6924 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6925 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6926 &type_unit_group_for_lookup, INSERT);
6927 if (*slot == nullptr)
6928 {
6929 sect_offset line_offset_struct = (sect_offset) line_offset;
6930 std::unique_ptr<type_unit_group> grp
6931 = create_type_unit_group (cu, line_offset_struct);
6932 *slot = grp.release ();
6933 ++tu_stats->nr_symtabs;
6934 }
6935
6936 tu_group = (struct type_unit_group *) *slot;
6937 gdb_assert (tu_group != nullptr);
6938 return tu_group;
6939 }
6940 \f
6941 /* Partial symbol tables. */
6942
6943 /* Create a psymtab named NAME and assign it to PER_CU.
6944
6945 The caller must fill in the following details:
6946 dirname, textlow, texthigh. */
6947
6948 static dwarf2_psymtab *
6949 create_partial_symtab (dwarf2_per_cu_data *per_cu,
6950 dwarf2_per_objfile *per_objfile,
6951 const char *name)
6952 {
6953 dwarf2_psymtab *pst
6954 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6955 per_objfile->objfile->per_bfd, per_cu);
6956
6957 pst->psymtabs_addrmap_supported = true;
6958
6959 /* This is the glue that links PST into GDB's symbol API. */
6960 per_cu->v.psymtab = pst;
6961
6962 return pst;
6963 }
6964
6965 /* DIE reader function for process_psymtab_comp_unit. */
6966
6967 static void
6968 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6969 const gdb_byte *info_ptr,
6970 struct die_info *comp_unit_die,
6971 enum language pretend_language)
6972 {
6973 struct dwarf2_cu *cu = reader->cu;
6974 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6975 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6976 struct objfile *objfile = per_objfile->objfile;
6977 struct gdbarch *gdbarch = objfile->arch ();
6978 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6979 CORE_ADDR baseaddr;
6980 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6981 dwarf2_psymtab *pst;
6982 enum pc_bounds_kind cu_bounds_kind;
6983
6984 gdb_assert (! per_cu->is_debug_types);
6985
6986 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
6987
6988 /* Allocate a new partial symbol table structure. */
6989 gdb::unique_xmalloc_ptr<char> debug_filename;
6990 static const char artificial[] = "<artificial>";
6991 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
6992 if (strcmp (fnd.get_name (), artificial) == 0)
6993 {
6994 debug_filename.reset (concat (artificial, "@",
6995 sect_offset_str (per_cu->sect_off),
6996 (char *) NULL));
6997 fnd.set_name (debug_filename.get ());
6998 }
6999
7000 pst = create_partial_symtab (per_cu, per_objfile, fnd.get_name ());
7001
7002 /* This must be done before calling dwarf2_build_include_psymtabs. */
7003 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7004
7005 baseaddr = objfile->text_section_offset ();
7006
7007 dwarf2_find_base_address (comp_unit_die, cu);
7008
7009 /* Possibly set the default values of LOWPC and HIGHPC from
7010 `DW_AT_ranges'. */
7011 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7012 &best_highpc, cu, pst);
7013 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7014 {
7015 CORE_ADDR low
7016 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7017 - baseaddr);
7018 CORE_ADDR high
7019 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7020 - baseaddr - 1);
7021 /* Store the contiguous range if it is not empty; it can be
7022 empty for CUs with no code. */
7023 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7024 low, high, pst);
7025 }
7026
7027 /* Check if comp unit has_children.
7028 If so, read the rest of the partial symbols from this comp unit.
7029 If not, there's no more debug_info for this comp unit. */
7030 if (comp_unit_die->has_children)
7031 {
7032 struct partial_die_info *first_die;
7033 CORE_ADDR lowpc, highpc;
7034
7035 lowpc = ((CORE_ADDR) -1);
7036 highpc = ((CORE_ADDR) 0);
7037
7038 first_die = load_partial_dies (reader, info_ptr, 1);
7039
7040 scan_partial_symbols (first_die, &lowpc, &highpc,
7041 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7042
7043 /* If we didn't find a lowpc, set it to highpc to avoid
7044 complaints from `maint check'. */
7045 if (lowpc == ((CORE_ADDR) -1))
7046 lowpc = highpc;
7047
7048 /* If the compilation unit didn't have an explicit address range,
7049 then use the information extracted from its child dies. */
7050 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7051 {
7052 best_lowpc = lowpc;
7053 best_highpc = highpc;
7054 }
7055 }
7056 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7057 best_lowpc + baseaddr)
7058 - baseaddr);
7059 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7060 best_highpc + baseaddr)
7061 - baseaddr);
7062
7063 pst->end ();
7064
7065 if (!cu->per_cu->imported_symtabs_empty ())
7066 {
7067 int i;
7068 int len = cu->per_cu->imported_symtabs_size ();
7069
7070 /* Fill in 'dependencies' here; we fill in 'users' in a
7071 post-pass. */
7072 pst->number_of_dependencies = len;
7073 pst->dependencies
7074 = per_bfd->partial_symtabs->allocate_dependencies (len);
7075 for (i = 0; i < len; ++i)
7076 {
7077 pst->dependencies[i]
7078 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7079 }
7080
7081 cu->per_cu->imported_symtabs_free ();
7082 }
7083
7084 /* Get the list of files included in the current compilation unit,
7085 and build a psymtab for each of them. */
7086 dwarf2_build_include_psymtabs (cu, comp_unit_die, fnd, pst);
7087
7088 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7089 ", %d global, %d static syms",
7090 per_cu->is_debug_types ? "type" : "comp",
7091 sect_offset_str (per_cu->sect_off),
7092 paddress (gdbarch, pst->text_low (objfile)),
7093 paddress (gdbarch, pst->text_high (objfile)),
7094 (int) pst->global_psymbols.size (),
7095 (int) pst->static_psymbols.size ());
7096 }
7097
7098 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7099 Process compilation unit THIS_CU for a psymtab. */
7100
7101 static void
7102 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7103 dwarf2_per_objfile *per_objfile,
7104 bool want_partial_unit,
7105 enum language pretend_language)
7106 {
7107 /* If this compilation unit was already read in, free the
7108 cached copy in order to read it in again. This is
7109 necessary because we skipped some symbols when we first
7110 read in the compilation unit (see load_partial_dies).
7111 This problem could be avoided, but the benefit is unclear. */
7112 per_objfile->remove_cu (this_cu);
7113
7114 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7115
7116 if (reader.comp_unit_die == nullptr)
7117 return;
7118
7119 switch (reader.comp_unit_die->tag)
7120 {
7121 case DW_TAG_compile_unit:
7122 this_cu->unit_type = DW_UT_compile;
7123 break;
7124 case DW_TAG_partial_unit:
7125 this_cu->unit_type = DW_UT_partial;
7126 break;
7127 case DW_TAG_type_unit:
7128 this_cu->unit_type = DW_UT_type;
7129 break;
7130 default:
7131 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7132 dwarf_tag_name (reader.comp_unit_die->tag),
7133 sect_offset_str (reader.cu->per_cu->sect_off),
7134 objfile_name (per_objfile->objfile));
7135 }
7136
7137 if (reader.dummy_p)
7138 {
7139 /* Nothing. */
7140 }
7141 else if (this_cu->is_debug_types)
7142 build_type_psymtabs_reader (&reader, reader.info_ptr,
7143 reader.comp_unit_die);
7144 else if (want_partial_unit
7145 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7146 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7147 reader.comp_unit_die,
7148 pretend_language);
7149
7150 /* Age out any secondary CUs. */
7151 per_objfile->age_comp_units ();
7152 }
7153
7154 /* Reader function for build_type_psymtabs. */
7155
7156 static void
7157 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7158 const gdb_byte *info_ptr,
7159 struct die_info *type_unit_die)
7160 {
7161 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7162 struct dwarf2_cu *cu = reader->cu;
7163 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7164 struct signatured_type *sig_type;
7165 struct type_unit_group *tu_group;
7166 struct attribute *attr;
7167 struct partial_die_info *first_die;
7168 CORE_ADDR lowpc, highpc;
7169 dwarf2_psymtab *pst;
7170
7171 gdb_assert (per_cu->is_debug_types);
7172 sig_type = (struct signatured_type *) per_cu;
7173
7174 if (! type_unit_die->has_children)
7175 return;
7176
7177 attr = type_unit_die->attr (DW_AT_stmt_list);
7178 tu_group = get_type_unit_group (cu, attr);
7179
7180 if (tu_group->tus == nullptr)
7181 tu_group->tus = new std::vector<signatured_type *>;
7182 tu_group->tus->push_back (sig_type);
7183
7184 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7185 pst = create_partial_symtab (per_cu, per_objfile, "");
7186 pst->anonymous = true;
7187
7188 first_die = load_partial_dies (reader, info_ptr, 1);
7189
7190 lowpc = (CORE_ADDR) -1;
7191 highpc = (CORE_ADDR) 0;
7192 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7193
7194 pst->end ();
7195 }
7196
7197 /* Struct used to sort TUs by their abbreviation table offset. */
7198
7199 struct tu_abbrev_offset
7200 {
7201 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7202 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7203 {}
7204
7205 /* This is used when sorting. */
7206 bool operator< (const tu_abbrev_offset &other) const
7207 {
7208 return abbrev_offset < other.abbrev_offset;
7209 }
7210
7211 signatured_type *sig_type;
7212 sect_offset abbrev_offset;
7213 };
7214
7215 /* Efficiently read all the type units.
7216
7217 The efficiency is because we sort TUs by the abbrev table they use and
7218 only read each abbrev table once. In one program there are 200K TUs
7219 sharing 8K abbrev tables.
7220
7221 The main purpose of this function is to support building the
7222 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7223 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7224 can collapse the search space by grouping them by stmt_list.
7225 The savings can be significant, in the same program from above the 200K TUs
7226 share 8K stmt_list tables.
7227
7228 FUNC is expected to call get_type_unit_group, which will create the
7229 struct type_unit_group if necessary and add it to
7230 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7231
7232 static void
7233 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7234 {
7235 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7236 abbrev_table_up abbrev_table;
7237 sect_offset abbrev_offset;
7238
7239 /* It's up to the caller to not call us multiple times. */
7240 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7241
7242 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7243 return;
7244
7245 /* TUs typically share abbrev tables, and there can be way more TUs than
7246 abbrev tables. Sort by abbrev table to reduce the number of times we
7247 read each abbrev table in.
7248 Alternatives are to punt or to maintain a cache of abbrev tables.
7249 This is simpler and efficient enough for now.
7250
7251 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7252 symtab to use). Typically TUs with the same abbrev offset have the same
7253 stmt_list value too so in practice this should work well.
7254
7255 The basic algorithm here is:
7256
7257 sort TUs by abbrev table
7258 for each TU with same abbrev table:
7259 read abbrev table if first user
7260 read TU top level DIE
7261 [IWBN if DWO skeletons had DW_AT_stmt_list]
7262 call FUNC */
7263
7264 dwarf_read_debug_printf ("Building type unit groups ...");
7265
7266 /* Sort in a separate table to maintain the order of all_comp_units
7267 for .gdb_index: TU indices directly index all_type_units. */
7268 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7269 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7270
7271 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7272 {
7273 if (cu->is_debug_types)
7274 {
7275 auto sig_type = static_cast<signatured_type *> (cu.get ());
7276 sorted_by_abbrev.emplace_back
7277 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7278 sig_type->sect_off));
7279 }
7280 }
7281
7282 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
7283
7284 abbrev_offset = (sect_offset) ~(unsigned) 0;
7285
7286 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7287 {
7288 /* Switch to the next abbrev table if necessary. */
7289 if (abbrev_table == NULL
7290 || tu.abbrev_offset != abbrev_offset)
7291 {
7292 abbrev_offset = tu.abbrev_offset;
7293 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7294 abbrev_table =
7295 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7296 ++tu_stats->nr_uniq_abbrev_tables;
7297 }
7298
7299 cutu_reader reader (tu.sig_type, per_objfile,
7300 abbrev_table.get (), nullptr, false);
7301 if (!reader.dummy_p)
7302 build_type_psymtabs_reader (&reader, reader.info_ptr,
7303 reader.comp_unit_die);
7304 }
7305 }
7306
7307 /* Print collected type unit statistics. */
7308
7309 static void
7310 print_tu_stats (dwarf2_per_objfile *per_objfile)
7311 {
7312 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7313
7314 dwarf_read_debug_printf ("Type unit statistics:");
7315 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7316 dwarf_read_debug_printf (" %d uniq abbrev tables",
7317 tu_stats->nr_uniq_abbrev_tables);
7318 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7319 tu_stats->nr_symtabs);
7320 dwarf_read_debug_printf (" %d symtab sharers",
7321 tu_stats->nr_symtab_sharers);
7322 dwarf_read_debug_printf (" %d type units without a stmt_list",
7323 tu_stats->nr_stmt_less_type_units);
7324 dwarf_read_debug_printf (" %d all_type_units reallocs",
7325 tu_stats->nr_all_type_units_reallocs);
7326 }
7327
7328 /* Traversal function for build_type_psymtabs. */
7329
7330 static int
7331 build_type_psymtab_dependencies (void **slot, void *info)
7332 {
7333 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7334 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7335 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7336 dwarf2_psymtab *pst = tu_group->v.psymtab;
7337 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7338 int i;
7339
7340 gdb_assert (len > 0);
7341 gdb_assert (tu_group->type_unit_group_p ());
7342
7343 pst->number_of_dependencies = len;
7344 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7345 for (i = 0; i < len; ++i)
7346 {
7347 struct signatured_type *iter = tu_group->tus->at (i);
7348 gdb_assert (iter->is_debug_types);
7349 pst->dependencies[i] = iter->v.psymtab;
7350 iter->type_unit_group = tu_group;
7351 }
7352
7353 delete tu_group->tus;
7354 tu_group->tus = nullptr;
7355
7356 return 1;
7357 }
7358
7359 /* Traversal function for process_skeletonless_type_unit.
7360 Read a TU in a DWO file and build partial symbols for it. */
7361
7362 static int
7363 process_skeletonless_type_unit (void **slot, void *info)
7364 {
7365 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7366 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7367
7368 /* If this TU doesn't exist in the global table, add it and read it in. */
7369
7370 if (per_objfile->per_bfd->signatured_types == NULL)
7371 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7372
7373 signatured_type find_entry (dwo_unit->signature);
7374 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7375 &find_entry, INSERT);
7376 /* If we've already seen this type there's nothing to do. What's happening
7377 is we're doing our own version of comdat-folding here. */
7378 if (*slot != NULL)
7379 return 1;
7380
7381 /* This does the job that create_all_comp_units would have done for
7382 this TU. */
7383 signatured_type *entry
7384 = add_type_unit (per_objfile, dwo_unit->signature, slot);
7385 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7386 *slot = entry;
7387
7388 /* This does the job that build_type_psymtabs would have done. */
7389 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7390 if (!reader.dummy_p)
7391 build_type_psymtabs_reader (&reader, reader.info_ptr,
7392 reader.comp_unit_die);
7393
7394 return 1;
7395 }
7396
7397 /* Traversal function for process_skeletonless_type_units. */
7398
7399 static int
7400 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7401 {
7402 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7403
7404 if (dwo_file->tus != NULL)
7405 htab_traverse_noresize (dwo_file->tus.get (),
7406 process_skeletonless_type_unit, info);
7407
7408 return 1;
7409 }
7410
7411 /* Scan all TUs of DWO files, verifying we've processed them.
7412 This is needed in case a TU was emitted without its skeleton.
7413 Note: This can't be done until we know what all the DWO files are. */
7414
7415 static void
7416 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7417 {
7418 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7419 if (get_dwp_file (per_objfile) == NULL
7420 && per_objfile->per_bfd->dwo_files != NULL)
7421 {
7422 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7423 process_dwo_file_for_skeletonless_type_units,
7424 per_objfile);
7425 }
7426 }
7427
7428 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7429
7430 static void
7431 set_partial_user (dwarf2_per_objfile *per_objfile)
7432 {
7433 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7434 {
7435 dwarf2_psymtab *pst = per_cu->v.psymtab;
7436
7437 if (pst == NULL)
7438 continue;
7439
7440 for (int j = 0; j < pst->number_of_dependencies; ++j)
7441 {
7442 /* Set the 'user' field only if it is not already set. */
7443 if (pst->dependencies[j]->user == NULL)
7444 pst->dependencies[j]->user = pst;
7445 }
7446 }
7447 }
7448
7449 /* Build the partial symbol table by doing a quick pass through the
7450 .debug_info and .debug_abbrev sections. */
7451
7452 static void
7453 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7454 {
7455 struct objfile *objfile = per_objfile->objfile;
7456 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7457
7458 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7459 objfile_name (objfile));
7460
7461 scoped_restore restore_reading_psyms
7462 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7463
7464 per_bfd->info.read (objfile);
7465
7466 /* Any cached compilation units will be linked by the per-objfile
7467 read_in_chain. Make sure to free them when we're done. */
7468 free_cached_comp_units freer (per_objfile);
7469
7470 create_all_comp_units (per_objfile);
7471 build_type_psymtabs (per_objfile);
7472
7473 /* Create a temporary address map on a temporary obstack. We later
7474 copy this to the final obstack. */
7475 auto_obstack temp_obstack;
7476
7477 scoped_restore save_psymtabs_addrmap
7478 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7479 addrmap_create_mutable (&temp_obstack));
7480
7481 for (const auto &per_cu : per_bfd->all_comp_units)
7482 {
7483 if (per_cu->v.psymtab != NULL)
7484 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7485 continue;
7486 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7487 language_minimal);
7488 }
7489
7490 /* This has to wait until we read the CUs, we need the list of DWOs. */
7491 process_skeletonless_type_units (per_objfile);
7492
7493 /* Now that all TUs have been processed we can fill in the dependencies. */
7494 if (per_bfd->type_unit_groups != NULL)
7495 {
7496 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7497 build_type_psymtab_dependencies, per_objfile);
7498 }
7499
7500 if (dwarf_read_debug > 0)
7501 print_tu_stats (per_objfile);
7502
7503 set_partial_user (per_objfile);
7504
7505 per_bfd->partial_symtabs->psymtabs_addrmap
7506 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7507 per_bfd->partial_symtabs->obstack ());
7508 /* At this point we want to keep the address map. */
7509 save_psymtabs_addrmap.release ();
7510
7511 dwarf_read_debug_printf ("Done building psymtabs of %s",
7512 objfile_name (objfile));
7513 }
7514
7515 /* Load the partial DIEs for a secondary CU into memory.
7516 This is also used when rereading a primary CU with load_all_dies. */
7517
7518 static void
7519 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7520 dwarf2_per_objfile *per_objfile,
7521 dwarf2_cu *existing_cu)
7522 {
7523 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7524
7525 if (!reader.dummy_p)
7526 {
7527 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7528 language_minimal);
7529
7530 /* Check if comp unit has_children.
7531 If so, read the rest of the partial symbols from this comp unit.
7532 If not, there's no more debug_info for this comp unit. */
7533 if (reader.comp_unit_die->has_children)
7534 load_partial_dies (&reader, reader.info_ptr, 0);
7535
7536 reader.keep ();
7537 }
7538 }
7539
7540 static void
7541 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7542 struct dwarf2_section_info *section,
7543 struct dwarf2_section_info *abbrev_section,
7544 unsigned int is_dwz,
7545 htab_up &types_htab,
7546 rcuh_kind section_kind)
7547 {
7548 const gdb_byte *info_ptr;
7549 struct objfile *objfile = per_objfile->objfile;
7550
7551 dwarf_read_debug_printf ("Reading %s for %s",
7552 section->get_name (),
7553 section->get_file_name ());
7554
7555 section->read (objfile);
7556
7557 info_ptr = section->buffer;
7558
7559 while (info_ptr < section->buffer + section->size)
7560 {
7561 dwarf2_per_cu_data_up this_cu;
7562
7563 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7564
7565 comp_unit_head cu_header;
7566 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7567 abbrev_section, info_ptr,
7568 section_kind);
7569
7570 /* Save the compilation unit for later lookup. */
7571 if (cu_header.unit_type != DW_UT_type)
7572 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7573 else
7574 {
7575 if (types_htab == nullptr)
7576 types_htab = allocate_signatured_type_table ();
7577
7578 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7579 (cu_header.signature);
7580 signatured_type *sig_ptr = sig_type.get ();
7581 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7582 this_cu.reset (sig_type.release ());
7583
7584 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7585 gdb_assert (slot != nullptr);
7586 if (*slot != nullptr)
7587 complaint (_("debug type entry at offset %s is duplicate to"
7588 " the entry at offset %s, signature %s"),
7589 sect_offset_str (sect_off),
7590 sect_offset_str (sig_ptr->sect_off),
7591 hex_string (sig_ptr->signature));
7592 *slot = sig_ptr;
7593 }
7594 this_cu->sect_off = sect_off;
7595 this_cu->length = cu_header.length + cu_header.initial_length_size;
7596 this_cu->is_dwz = is_dwz;
7597 this_cu->section = section;
7598
7599 info_ptr = info_ptr + this_cu->length;
7600 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7601 }
7602 }
7603
7604 /* Create a list of all compilation units in OBJFILE.
7605 This is only done for -readnow and building partial symtabs. */
7606
7607 static void
7608 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7609 {
7610 htab_up types_htab;
7611
7612 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7613 &per_objfile->per_bfd->abbrev, 0,
7614 types_htab, rcuh_kind::COMPILE);
7615 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7616 read_comp_units_from_section (per_objfile, &section,
7617 &per_objfile->per_bfd->abbrev, 0,
7618 types_htab, rcuh_kind::TYPE);
7619
7620 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7621 if (dwz != NULL)
7622 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7623 types_htab, rcuh_kind::COMPILE);
7624
7625 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7626 }
7627
7628 /* Process all loaded DIEs for compilation unit CU, starting at
7629 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7630 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7631 DW_AT_ranges). See the comments of add_partial_subprogram on how
7632 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7633
7634 static void
7635 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7636 CORE_ADDR *highpc, int set_addrmap,
7637 struct dwarf2_cu *cu)
7638 {
7639 struct partial_die_info *pdi;
7640
7641 /* Now, march along the PDI's, descending into ones which have
7642 interesting children but skipping the children of the other ones,
7643 until we reach the end of the compilation unit. */
7644
7645 pdi = first_die;
7646
7647 while (pdi != NULL)
7648 {
7649 pdi->fixup (cu);
7650
7651 /* Anonymous namespaces or modules have no name but have interesting
7652 children, so we need to look at them. Ditto for anonymous
7653 enums. */
7654
7655 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7656 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7657 || pdi->tag == DW_TAG_imported_unit
7658 || pdi->tag == DW_TAG_inlined_subroutine)
7659 {
7660 switch (pdi->tag)
7661 {
7662 case DW_TAG_subprogram:
7663 case DW_TAG_inlined_subroutine:
7664 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7665 if (cu->per_cu->lang == language_cplus)
7666 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7667 set_addrmap, cu);
7668 break;
7669 case DW_TAG_constant:
7670 case DW_TAG_variable:
7671 case DW_TAG_typedef:
7672 case DW_TAG_union_type:
7673 if (!pdi->is_declaration
7674 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7675 {
7676 add_partial_symbol (pdi, cu);
7677 }
7678 break;
7679 case DW_TAG_class_type:
7680 case DW_TAG_interface_type:
7681 case DW_TAG_structure_type:
7682 if (!pdi->is_declaration)
7683 {
7684 add_partial_symbol (pdi, cu);
7685 }
7686 if ((cu->per_cu->lang == language_rust
7687 || cu->per_cu->lang == language_cplus)
7688 && pdi->has_children)
7689 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7690 set_addrmap, cu);
7691 break;
7692 case DW_TAG_enumeration_type:
7693 if (!pdi->is_declaration)
7694 add_partial_enumeration (pdi, cu);
7695 break;
7696 case DW_TAG_base_type:
7697 case DW_TAG_subrange_type:
7698 /* File scope base type definitions are added to the partial
7699 symbol table. */
7700 add_partial_symbol (pdi, cu);
7701 break;
7702 case DW_TAG_namespace:
7703 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7704 break;
7705 case DW_TAG_module:
7706 if (!pdi->is_declaration)
7707 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7708 break;
7709 case DW_TAG_imported_unit:
7710 {
7711 struct dwarf2_per_cu_data *per_cu;
7712
7713 /* For now we don't handle imported units in type units. */
7714 if (cu->per_cu->is_debug_types)
7715 {
7716 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7717 " supported in type units [in module %s]"),
7718 objfile_name (cu->per_objfile->objfile));
7719 }
7720
7721 per_cu = dwarf2_find_containing_comp_unit
7722 (pdi->d.sect_off, pdi->is_dwz,
7723 cu->per_objfile->per_bfd);
7724
7725 /* Go read the partial unit, if needed. */
7726 if (per_cu->v.psymtab == NULL)
7727 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7728 cu->per_cu->lang);
7729
7730 if (pdi->die_parent == nullptr
7731 && per_cu->unit_type == DW_UT_compile
7732 && per_cu->lang == language_cplus)
7733 /* Regard import as hint. See corresponding code in
7734 process_imported_unit_die. */
7735 break;
7736
7737 cu->per_cu->imported_symtabs_push (per_cu);
7738 }
7739 break;
7740 case DW_TAG_imported_declaration:
7741 add_partial_symbol (pdi, cu);
7742 break;
7743 default:
7744 break;
7745 }
7746 }
7747
7748 /* If the die has a sibling, skip to the sibling. */
7749
7750 pdi = pdi->die_sibling;
7751 }
7752 }
7753
7754 /* Functions used to compute the fully scoped name of a partial DIE.
7755
7756 Normally, this is simple. For C++, the parent DIE's fully scoped
7757 name is concatenated with "::" and the partial DIE's name.
7758 Enumerators are an exception; they use the scope of their parent
7759 enumeration type, i.e. the name of the enumeration type is not
7760 prepended to the enumerator.
7761
7762 There are two complexities. One is DW_AT_specification; in this
7763 case "parent" means the parent of the target of the specification,
7764 instead of the direct parent of the DIE. The other is compilers
7765 which do not emit DW_TAG_namespace; in this case we try to guess
7766 the fully qualified name of structure types from their members'
7767 linkage names. This must be done using the DIE's children rather
7768 than the children of any DW_AT_specification target. We only need
7769 to do this for structures at the top level, i.e. if the target of
7770 any DW_AT_specification (if any; otherwise the DIE itself) does not
7771 have a parent. */
7772
7773 /* Compute the scope prefix associated with PDI's parent, in
7774 compilation unit CU. The result will be allocated on CU's
7775 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7776 field. NULL is returned if no prefix is necessary. */
7777 static const char *
7778 partial_die_parent_scope (struct partial_die_info *pdi,
7779 struct dwarf2_cu *cu)
7780 {
7781 const char *grandparent_scope;
7782 struct partial_die_info *parent, *real_pdi;
7783
7784 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7785 then this means the parent of the specification DIE. */
7786
7787 real_pdi = pdi;
7788 while (real_pdi->has_specification)
7789 {
7790 auto res = find_partial_die (real_pdi->spec_offset,
7791 real_pdi->spec_is_dwz, cu);
7792 real_pdi = res.pdi;
7793 cu = res.cu;
7794 }
7795
7796 parent = real_pdi->die_parent;
7797 if (parent == NULL)
7798 return NULL;
7799
7800 if (parent->scope_set)
7801 return parent->scope;
7802
7803 parent->fixup (cu);
7804
7805 grandparent_scope = partial_die_parent_scope (parent, cu);
7806
7807 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7808 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7809 Work around this problem here. */
7810 if (cu->per_cu->lang == language_cplus
7811 && parent->tag == DW_TAG_namespace
7812 && strcmp (parent->name (cu), "::") == 0
7813 && grandparent_scope == NULL)
7814 {
7815 parent->scope = NULL;
7816 parent->scope_set = 1;
7817 return NULL;
7818 }
7819
7820 /* Nested subroutines in Fortran get a prefix. */
7821 if (pdi->tag == DW_TAG_enumerator)
7822 /* Enumerators should not get the name of the enumeration as a prefix. */
7823 parent->scope = grandparent_scope;
7824 else if (parent->tag == DW_TAG_namespace
7825 || parent->tag == DW_TAG_module
7826 || parent->tag == DW_TAG_structure_type
7827 || parent->tag == DW_TAG_class_type
7828 || parent->tag == DW_TAG_interface_type
7829 || parent->tag == DW_TAG_union_type
7830 || parent->tag == DW_TAG_enumeration_type
7831 || (cu->per_cu->lang == language_fortran
7832 && parent->tag == DW_TAG_subprogram
7833 && pdi->tag == DW_TAG_subprogram))
7834 {
7835 if (grandparent_scope == NULL)
7836 parent->scope = parent->name (cu);
7837 else
7838 parent->scope = typename_concat (&cu->comp_unit_obstack,
7839 grandparent_scope,
7840 parent->name (cu), 0, cu);
7841 }
7842 else
7843 {
7844 /* FIXME drow/2004-04-01: What should we be doing with
7845 function-local names? For partial symbols, we should probably be
7846 ignoring them. */
7847 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7848 dwarf_tag_name (parent->tag),
7849 sect_offset_str (pdi->sect_off));
7850 parent->scope = grandparent_scope;
7851 }
7852
7853 parent->scope_set = 1;
7854 return parent->scope;
7855 }
7856
7857 /* Return the fully scoped name associated with PDI, from compilation unit
7858 CU. The result will be allocated with malloc. */
7859
7860 static gdb::unique_xmalloc_ptr<char>
7861 partial_die_full_name (struct partial_die_info *pdi,
7862 struct dwarf2_cu *cu)
7863 {
7864 const char *parent_scope;
7865
7866 /* If this is a template instantiation, we can not work out the
7867 template arguments from partial DIEs. So, unfortunately, we have
7868 to go through the full DIEs. At least any work we do building
7869 types here will be reused if full symbols are loaded later. */
7870 if (pdi->has_template_arguments)
7871 {
7872 pdi->fixup (cu);
7873
7874 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
7875 {
7876 struct die_info *die;
7877 struct attribute attr;
7878 struct dwarf2_cu *ref_cu = cu;
7879
7880 /* DW_FORM_ref_addr is using section offset. */
7881 attr.name = (enum dwarf_attribute) 0;
7882 attr.form = DW_FORM_ref_addr;
7883 attr.u.unsnd = to_underlying (pdi->sect_off);
7884 die = follow_die_ref (NULL, &attr, &ref_cu);
7885
7886 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7887 }
7888 }
7889
7890 parent_scope = partial_die_parent_scope (pdi, cu);
7891 if (parent_scope == NULL)
7892 return NULL;
7893 else
7894 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7895 pdi->name (cu),
7896 0, cu));
7897 }
7898
7899 static void
7900 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7901 {
7902 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7903 struct objfile *objfile = per_objfile->objfile;
7904 struct gdbarch *gdbarch = objfile->arch ();
7905 CORE_ADDR addr = 0;
7906 const char *actual_name = NULL;
7907 CORE_ADDR baseaddr;
7908
7909 baseaddr = objfile->text_section_offset ();
7910
7911 gdb::unique_xmalloc_ptr<char> built_actual_name
7912 = partial_die_full_name (pdi, cu);
7913 if (built_actual_name != NULL)
7914 actual_name = built_actual_name.get ();
7915
7916 if (actual_name == NULL)
7917 actual_name = pdi->name (cu);
7918
7919 partial_symbol psymbol;
7920 memset (&psymbol, 0, sizeof (psymbol));
7921 psymbol.ginfo.set_language (cu->per_cu->lang,
7922 &objfile->objfile_obstack);
7923 psymbol.ginfo.set_section_index (-1);
7924
7925 /* The code below indicates that the psymbol should be installed by
7926 setting this. */
7927 gdb::optional<psymbol_placement> where;
7928
7929 switch (pdi->tag)
7930 {
7931 case DW_TAG_inlined_subroutine:
7932 case DW_TAG_subprogram:
7933 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7934 - baseaddr);
7935 if (pdi->is_external
7936 || cu->per_cu->lang == language_ada
7937 || (cu->per_cu->lang == language_fortran
7938 && pdi->die_parent != NULL
7939 && pdi->die_parent->tag == DW_TAG_subprogram))
7940 {
7941 /* Normally, only "external" DIEs are part of the global scope.
7942 But in Ada and Fortran, we want to be able to access nested
7943 procedures globally. So all Ada and Fortran subprograms are
7944 stored in the global scope. */
7945 where = psymbol_placement::GLOBAL;
7946 }
7947 else
7948 where = psymbol_placement::STATIC;
7949
7950 psymbol.domain = VAR_DOMAIN;
7951 psymbol.aclass = LOC_BLOCK;
7952 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7953 psymbol.ginfo.value.address = addr;
7954
7955 if (pdi->main_subprogram && actual_name != NULL)
7956 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
7957 break;
7958 case DW_TAG_constant:
7959 psymbol.domain = VAR_DOMAIN;
7960 psymbol.aclass = LOC_STATIC;
7961 where = (pdi->is_external
7962 ? psymbol_placement::GLOBAL
7963 : psymbol_placement::STATIC);
7964 break;
7965 case DW_TAG_variable:
7966 if (pdi->d.locdesc)
7967 addr = decode_locdesc (pdi->d.locdesc, cu);
7968
7969 if (pdi->d.locdesc
7970 && addr == 0
7971 && !per_objfile->per_bfd->has_section_at_zero)
7972 {
7973 /* A global or static variable may also have been stripped
7974 out by the linker if unused, in which case its address
7975 will be nullified; do not add such variables into partial
7976 symbol table then. */
7977 }
7978 else if (pdi->is_external)
7979 {
7980 /* Global Variable.
7981 Don't enter into the minimal symbol tables as there is
7982 a minimal symbol table entry from the ELF symbols already.
7983 Enter into partial symbol table if it has a location
7984 descriptor or a type.
7985 If the location descriptor is missing, new_symbol will create
7986 a LOC_UNRESOLVED symbol, the address of the variable will then
7987 be determined from the minimal symbol table whenever the variable
7988 is referenced.
7989 The address for the partial symbol table entry is not
7990 used by GDB, but it comes in handy for debugging partial symbol
7991 table building. */
7992
7993 if (pdi->d.locdesc || pdi->has_type)
7994 {
7995 psymbol.domain = VAR_DOMAIN;
7996 psymbol.aclass = LOC_STATIC;
7997 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
7998 psymbol.ginfo.value.address = addr;
7999 where = psymbol_placement::GLOBAL;
8000 }
8001 }
8002 else
8003 {
8004 int has_loc = pdi->d.locdesc != NULL;
8005
8006 /* Static Variable. Skip symbols whose value we cannot know (those
8007 without location descriptors or constant values). */
8008 if (!has_loc && !pdi->has_const_value)
8009 return;
8010
8011 psymbol.domain = VAR_DOMAIN;
8012 psymbol.aclass = LOC_STATIC;
8013 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8014 if (has_loc)
8015 psymbol.ginfo.value.address = addr;
8016 where = psymbol_placement::STATIC;
8017 }
8018 break;
8019 case DW_TAG_array_type:
8020 case DW_TAG_typedef:
8021 case DW_TAG_base_type:
8022 case DW_TAG_subrange_type:
8023 psymbol.domain = VAR_DOMAIN;
8024 psymbol.aclass = LOC_TYPEDEF;
8025 where = psymbol_placement::STATIC;
8026 break;
8027 case DW_TAG_imported_declaration:
8028 case DW_TAG_namespace:
8029 psymbol.domain = VAR_DOMAIN;
8030 psymbol.aclass = LOC_TYPEDEF;
8031 where = psymbol_placement::GLOBAL;
8032 break;
8033 case DW_TAG_module:
8034 /* With Fortran 77 there might be a "BLOCK DATA" module
8035 available without any name. If so, we skip the module as it
8036 doesn't bring any value. */
8037 if (actual_name != nullptr)
8038 {
8039 psymbol.domain = MODULE_DOMAIN;
8040 psymbol.aclass = LOC_TYPEDEF;
8041 where = psymbol_placement::GLOBAL;
8042 }
8043 break;
8044 case DW_TAG_class_type:
8045 case DW_TAG_interface_type:
8046 case DW_TAG_structure_type:
8047 case DW_TAG_union_type:
8048 case DW_TAG_enumeration_type:
8049 /* Skip external references. The DWARF standard says in the section
8050 about "Structure, Union, and Class Type Entries": "An incomplete
8051 structure, union or class type is represented by a structure,
8052 union or class entry that does not have a byte size attribute
8053 and that has a DW_AT_declaration attribute." */
8054 if (!pdi->has_byte_size && pdi->is_declaration)
8055 return;
8056
8057 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8058 static vs. global. */
8059 psymbol.domain = STRUCT_DOMAIN;
8060 psymbol.aclass = LOC_TYPEDEF;
8061 where = (cu->per_cu->lang == language_cplus
8062 ? psymbol_placement::GLOBAL
8063 : psymbol_placement::STATIC);
8064 break;
8065 case DW_TAG_enumerator:
8066 psymbol.domain = VAR_DOMAIN;
8067 psymbol.aclass = LOC_CONST;
8068 where = (cu->per_cu->lang == language_cplus
8069 ? psymbol_placement::GLOBAL
8070 : psymbol_placement::STATIC);
8071 break;
8072 default:
8073 break;
8074 }
8075
8076 if (where.has_value ())
8077 {
8078 if (built_actual_name != nullptr)
8079 actual_name = objfile->intern (actual_name);
8080 if (pdi->linkage_name == nullptr
8081 || cu->per_cu->lang == language_ada)
8082 psymbol.ginfo.set_linkage_name (actual_name);
8083 else
8084 {
8085 psymbol.ginfo.set_demangled_name (actual_name,
8086 &objfile->objfile_obstack);
8087 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8088 }
8089 cu->per_cu->v.psymtab->add_psymbol
8090 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8091 objfile);
8092 }
8093 }
8094
8095 /* Read a partial die corresponding to a namespace; also, add a symbol
8096 corresponding to that namespace to the symbol table. NAMESPACE is
8097 the name of the enclosing namespace. */
8098
8099 static void
8100 add_partial_namespace (struct partial_die_info *pdi,
8101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8102 int set_addrmap, struct dwarf2_cu *cu)
8103 {
8104 /* Add a symbol for the namespace. */
8105
8106 add_partial_symbol (pdi, cu);
8107
8108 /* Now scan partial symbols in that namespace. */
8109
8110 if (pdi->has_children)
8111 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8112 }
8113
8114 /* Read a partial die corresponding to a Fortran module. */
8115
8116 static void
8117 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8118 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8119 {
8120 /* Add a symbol for the namespace. */
8121
8122 add_partial_symbol (pdi, cu);
8123
8124 /* Now scan partial symbols in that module. */
8125
8126 if (pdi->has_children)
8127 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8128 }
8129
8130 static int
8131 dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
8132 dwarf2_psymtab *, dwarf_tag);
8133
8134 /* Read a partial die corresponding to a subprogram or an inlined
8135 subprogram and create a partial symbol for that subprogram.
8136 When the CU language allows it, this routine also defines a partial
8137 symbol for each nested subprogram that this subprogram contains.
8138 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8139 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8140
8141 PDI may also be a lexical block, in which case we simply search
8142 recursively for subprograms defined inside that lexical block.
8143 Again, this is only performed when the CU language allows this
8144 type of definitions. */
8145
8146 static void
8147 add_partial_subprogram (struct partial_die_info *pdi,
8148 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8149 int set_addrmap, struct dwarf2_cu *cu)
8150 {
8151 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8152 {
8153 if (pdi->has_pc_info)
8154 {
8155 if (pdi->lowpc < *lowpc)
8156 *lowpc = pdi->lowpc;
8157 if (pdi->highpc > *highpc)
8158 *highpc = pdi->highpc;
8159 if (set_addrmap)
8160 {
8161 struct objfile *objfile = cu->per_objfile->objfile;
8162 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8163 struct gdbarch *gdbarch = objfile->arch ();
8164 CORE_ADDR baseaddr;
8165 CORE_ADDR this_highpc;
8166 CORE_ADDR this_lowpc;
8167
8168 baseaddr = objfile->text_section_offset ();
8169 this_lowpc
8170 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8171 pdi->lowpc + baseaddr)
8172 - baseaddr);
8173 this_highpc
8174 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8175 pdi->highpc + baseaddr)
8176 - baseaddr);
8177 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8178 this_lowpc, this_highpc - 1,
8179 cu->per_cu->v.psymtab);
8180 }
8181 }
8182
8183 if (pdi->has_range_info
8184 && dwarf2_ranges_read (pdi->ranges_offset, &pdi->lowpc, &pdi->highpc,
8185 cu,
8186 set_addrmap ? cu->per_cu->v.psymtab : nullptr,
8187 pdi->tag))
8188 {
8189 if (pdi->lowpc < *lowpc)
8190 *lowpc = pdi->lowpc;
8191 if (pdi->highpc > *highpc)
8192 *highpc = pdi->highpc;
8193 }
8194
8195 if (pdi->has_pc_info || pdi->has_range_info
8196 || (!pdi->is_external && pdi->may_be_inlined))
8197 {
8198 if (!pdi->is_declaration)
8199 /* Ignore subprogram DIEs that do not have a name, they are
8200 illegal. Do not emit a complaint at this point, we will
8201 do so when we convert this psymtab into a symtab. */
8202 if (pdi->name (cu))
8203 add_partial_symbol (pdi, cu);
8204 }
8205 }
8206
8207 if (! pdi->has_children)
8208 return;
8209
8210 if (cu->per_cu->lang == language_ada
8211 || cu->per_cu->lang == language_fortran)
8212 {
8213 pdi = pdi->die_child;
8214 while (pdi != NULL)
8215 {
8216 pdi->fixup (cu);
8217 if (pdi->tag == DW_TAG_subprogram
8218 || pdi->tag == DW_TAG_inlined_subroutine
8219 || pdi->tag == DW_TAG_lexical_block)
8220 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8221 pdi = pdi->die_sibling;
8222 }
8223 }
8224 }
8225
8226 /* Read a partial die corresponding to an enumeration type. */
8227
8228 static void
8229 add_partial_enumeration (struct partial_die_info *enum_pdi,
8230 struct dwarf2_cu *cu)
8231 {
8232 struct partial_die_info *pdi;
8233
8234 if (enum_pdi->name (cu) != NULL)
8235 add_partial_symbol (enum_pdi, cu);
8236
8237 pdi = enum_pdi->die_child;
8238 while (pdi)
8239 {
8240 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8241 complaint (_("malformed enumerator DIE ignored"));
8242 else
8243 add_partial_symbol (pdi, cu);
8244 pdi = pdi->die_sibling;
8245 }
8246 }
8247
8248 /* Return the initial uleb128 in the die at INFO_PTR. */
8249
8250 static unsigned int
8251 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8252 {
8253 unsigned int bytes_read;
8254
8255 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8256 }
8257
8258 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8259 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8260
8261 Return the corresponding abbrev, or NULL if the number is zero (indicating
8262 an empty DIE). In either case *BYTES_READ will be set to the length of
8263 the initial number. */
8264
8265 static const struct abbrev_info *
8266 peek_die_abbrev (const die_reader_specs &reader,
8267 const gdb_byte *info_ptr, unsigned int *bytes_read)
8268 {
8269 dwarf2_cu *cu = reader.cu;
8270 bfd *abfd = reader.abfd;
8271 unsigned int abbrev_number
8272 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8273
8274 if (abbrev_number == 0)
8275 return NULL;
8276
8277 const abbrev_info *abbrev
8278 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8279 if (!abbrev)
8280 {
8281 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8282 " at offset %s [in module %s]"),
8283 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8284 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8285 }
8286
8287 return abbrev;
8288 }
8289
8290 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8291 Returns a pointer to the end of a series of DIEs, terminated by an empty
8292 DIE. Any children of the skipped DIEs will also be skipped. */
8293
8294 static const gdb_byte *
8295 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8296 {
8297 while (1)
8298 {
8299 unsigned int bytes_read;
8300 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8301 &bytes_read);
8302
8303 if (abbrev == NULL)
8304 return info_ptr + bytes_read;
8305 else
8306 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8307 }
8308 }
8309
8310 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8311 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8312 abbrev corresponding to that skipped uleb128 should be passed in
8313 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8314 children. */
8315
8316 static const gdb_byte *
8317 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8318 const struct abbrev_info *abbrev)
8319 {
8320 unsigned int bytes_read;
8321 struct attribute attr;
8322 bfd *abfd = reader->abfd;
8323 struct dwarf2_cu *cu = reader->cu;
8324 const gdb_byte *buffer = reader->buffer;
8325 const gdb_byte *buffer_end = reader->buffer_end;
8326 unsigned int form, i;
8327
8328 for (i = 0; i < abbrev->num_attrs; i++)
8329 {
8330 /* The only abbrev we care about is DW_AT_sibling. */
8331 if (abbrev->attrs[i].name == DW_AT_sibling)
8332 {
8333 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8334 if (attr.form == DW_FORM_ref_addr)
8335 complaint (_("ignoring absolute DW_AT_sibling"));
8336 else
8337 {
8338 sect_offset off = attr.get_ref_die_offset ();
8339 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8340
8341 if (sibling_ptr < info_ptr)
8342 complaint (_("DW_AT_sibling points backwards"));
8343 else if (sibling_ptr > reader->buffer_end)
8344 reader->die_section->overflow_complaint ();
8345 else
8346 return sibling_ptr;
8347 }
8348 }
8349
8350 /* If it isn't DW_AT_sibling, skip this attribute. */
8351 form = abbrev->attrs[i].form;
8352 skip_attribute:
8353 switch (form)
8354 {
8355 case DW_FORM_ref_addr:
8356 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8357 and later it is offset sized. */
8358 if (cu->header.version == 2)
8359 info_ptr += cu->header.addr_size;
8360 else
8361 info_ptr += cu->header.offset_size;
8362 break;
8363 case DW_FORM_GNU_ref_alt:
8364 info_ptr += cu->header.offset_size;
8365 break;
8366 case DW_FORM_addr:
8367 info_ptr += cu->header.addr_size;
8368 break;
8369 case DW_FORM_data1:
8370 case DW_FORM_ref1:
8371 case DW_FORM_flag:
8372 case DW_FORM_strx1:
8373 info_ptr += 1;
8374 break;
8375 case DW_FORM_flag_present:
8376 case DW_FORM_implicit_const:
8377 break;
8378 case DW_FORM_data2:
8379 case DW_FORM_ref2:
8380 case DW_FORM_strx2:
8381 info_ptr += 2;
8382 break;
8383 case DW_FORM_strx3:
8384 info_ptr += 3;
8385 break;
8386 case DW_FORM_data4:
8387 case DW_FORM_ref4:
8388 case DW_FORM_strx4:
8389 info_ptr += 4;
8390 break;
8391 case DW_FORM_data8:
8392 case DW_FORM_ref8:
8393 case DW_FORM_ref_sig8:
8394 info_ptr += 8;
8395 break;
8396 case DW_FORM_data16:
8397 info_ptr += 16;
8398 break;
8399 case DW_FORM_string:
8400 read_direct_string (abfd, info_ptr, &bytes_read);
8401 info_ptr += bytes_read;
8402 break;
8403 case DW_FORM_sec_offset:
8404 case DW_FORM_strp:
8405 case DW_FORM_GNU_strp_alt:
8406 info_ptr += cu->header.offset_size;
8407 break;
8408 case DW_FORM_exprloc:
8409 case DW_FORM_block:
8410 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8411 info_ptr += bytes_read;
8412 break;
8413 case DW_FORM_block1:
8414 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8415 break;
8416 case DW_FORM_block2:
8417 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8418 break;
8419 case DW_FORM_block4:
8420 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8421 break;
8422 case DW_FORM_addrx:
8423 case DW_FORM_strx:
8424 case DW_FORM_sdata:
8425 case DW_FORM_udata:
8426 case DW_FORM_ref_udata:
8427 case DW_FORM_GNU_addr_index:
8428 case DW_FORM_GNU_str_index:
8429 case DW_FORM_rnglistx:
8430 case DW_FORM_loclistx:
8431 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8432 break;
8433 case DW_FORM_indirect:
8434 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8435 info_ptr += bytes_read;
8436 /* We need to continue parsing from here, so just go back to
8437 the top. */
8438 goto skip_attribute;
8439
8440 default:
8441 error (_("Dwarf Error: Cannot handle %s "
8442 "in DWARF reader [in module %s]"),
8443 dwarf_form_name (form),
8444 bfd_get_filename (abfd));
8445 }
8446 }
8447
8448 if (abbrev->has_children)
8449 return skip_children (reader, info_ptr);
8450 else
8451 return info_ptr;
8452 }
8453
8454 /* Locate ORIG_PDI's sibling.
8455 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8456
8457 static const gdb_byte *
8458 locate_pdi_sibling (const struct die_reader_specs *reader,
8459 struct partial_die_info *orig_pdi,
8460 const gdb_byte *info_ptr)
8461 {
8462 /* Do we know the sibling already? */
8463
8464 if (orig_pdi->sibling)
8465 return orig_pdi->sibling;
8466
8467 /* Are there any children to deal with? */
8468
8469 if (!orig_pdi->has_children)
8470 return info_ptr;
8471
8472 /* Skip the children the long way. */
8473
8474 return skip_children (reader, info_ptr);
8475 }
8476
8477 /* Expand this partial symbol table into a full symbol table. SELF is
8478 not NULL. */
8479
8480 void
8481 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8482 {
8483 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8484
8485 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8486
8487 /* If this psymtab is constructed from a debug-only objfile, the
8488 has_section_at_zero flag will not necessarily be correct. We
8489 can get the correct value for this flag by looking at the data
8490 associated with the (presumably stripped) associated objfile. */
8491 if (objfile->separate_debug_objfile_backlink)
8492 {
8493 dwarf2_per_objfile *per_objfile_backlink
8494 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8495
8496 per_objfile->per_bfd->has_section_at_zero
8497 = per_objfile_backlink->per_bfd->has_section_at_zero;
8498 }
8499
8500 expand_psymtab (objfile);
8501
8502 process_cu_includes (per_objfile);
8503 }
8504 \f
8505 /* Reading in full CUs. */
8506
8507 /* Add PER_CU to the queue. */
8508
8509 static void
8510 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8511 dwarf2_per_objfile *per_objfile,
8512 enum language pretend_language)
8513 {
8514 per_cu->queued = 1;
8515
8516 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8517 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8518 }
8519
8520 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8521
8522 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8523 dependency.
8524
8525 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8526 DIEs, false otherwise.
8527
8528 Explanation: there is an invariant that if a CU is queued for expansion
8529 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8530 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8531 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8532 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8533 invariant is respected.
8534
8535 The caller is therefore not required to load the CU's DIEs (we return false)
8536 if:
8537
8538 - the CU is already expanded, and therefore does not get enqueued
8539 - the CU gets enqueued for expansion, but its DIEs are already loaded
8540
8541 Note that the caller should not use this function's return value as an
8542 indicator of whether the CU's DIEs are loaded right now, it should check
8543 that by calling `dwarf2_per_objfile::get_cu` instead. */
8544
8545 static int
8546 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8547 dwarf2_per_cu_data *per_cu,
8548 dwarf2_per_objfile *per_objfile,
8549 enum language pretend_language)
8550 {
8551 /* We may arrive here during partial symbol reading, if we need full
8552 DIEs to process an unusual case (e.g. template arguments). Do
8553 not queue PER_CU, just tell our caller to load its DIEs. */
8554 if (per_cu->per_bfd->reading_partial_symbols)
8555 {
8556 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8557
8558 if (cu == NULL || cu->dies == NULL)
8559 return 1;
8560 return 0;
8561 }
8562
8563 /* Mark the dependence relation so that we don't flush PER_CU
8564 too early. */
8565 if (dependent_cu != NULL)
8566 dependent_cu->add_dependence (per_cu);
8567
8568 /* If it's already on the queue, we have nothing to do. */
8569 if (per_cu->queued)
8570 {
8571 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8572 loaded. */
8573 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8574
8575 /* If the CU is queued for expansion, it should not already be
8576 expanded. */
8577 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8578
8579 /* The DIEs are already loaded, the caller doesn't need to do it. */
8580 return 0;
8581 }
8582
8583 bool queued = false;
8584 if (!per_objfile->symtab_set_p (per_cu))
8585 {
8586 /* Add it to the queue. */
8587 queue_comp_unit (per_cu, per_objfile, pretend_language);
8588 queued = true;
8589 }
8590
8591 /* If the compilation unit is already loaded, just mark it as
8592 used. */
8593 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8594 if (cu != nullptr)
8595 cu->last_used = 0;
8596
8597 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8598 and the DIEs are not already loaded. */
8599 return queued && cu == nullptr;
8600 }
8601
8602 /* Process the queue. */
8603
8604 static void
8605 process_queue (dwarf2_per_objfile *per_objfile)
8606 {
8607 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8608 objfile_name (per_objfile->objfile));
8609
8610 /* The queue starts out with one item, but following a DIE reference
8611 may load a new CU, adding it to the end of the queue. */
8612 while (!per_objfile->per_bfd->queue->empty ())
8613 {
8614 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8615 dwarf2_per_cu_data *per_cu = item.per_cu;
8616
8617 if (!per_objfile->symtab_set_p (per_cu))
8618 {
8619 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8620
8621 /* Skip dummy CUs. */
8622 if (cu != nullptr)
8623 {
8624 unsigned int debug_print_threshold;
8625 char buf[100];
8626
8627 if (per_cu->is_debug_types)
8628 {
8629 struct signatured_type *sig_type =
8630 (struct signatured_type *) per_cu;
8631
8632 sprintf (buf, "TU %s at offset %s",
8633 hex_string (sig_type->signature),
8634 sect_offset_str (per_cu->sect_off));
8635 /* There can be 100s of TUs.
8636 Only print them in verbose mode. */
8637 debug_print_threshold = 2;
8638 }
8639 else
8640 {
8641 sprintf (buf, "CU at offset %s",
8642 sect_offset_str (per_cu->sect_off));
8643 debug_print_threshold = 1;
8644 }
8645
8646 if (dwarf_read_debug >= debug_print_threshold)
8647 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8648
8649 if (per_cu->is_debug_types)
8650 process_full_type_unit (cu, item.pretend_language);
8651 else
8652 process_full_comp_unit (cu, item.pretend_language);
8653
8654 if (dwarf_read_debug >= debug_print_threshold)
8655 dwarf_read_debug_printf ("Done expanding %s", buf);
8656 }
8657 }
8658
8659 per_cu->queued = 0;
8660 per_objfile->per_bfd->queue->pop ();
8661 }
8662
8663 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8664 objfile_name (per_objfile->objfile));
8665 }
8666
8667 /* Read in full symbols for PST, and anything it depends on. */
8668
8669 void
8670 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8671 {
8672 gdb_assert (!readin_p (objfile));
8673
8674 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8675 free_cached_comp_units freer (per_objfile);
8676 expand_dependencies (objfile);
8677
8678 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8679 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8680 }
8681
8682 /* See psympriv.h. */
8683
8684 bool
8685 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8686 {
8687 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8688 return per_objfile->symtab_set_p (per_cu_data);
8689 }
8690
8691 /* See psympriv.h. */
8692
8693 compunit_symtab *
8694 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8695 {
8696 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8697 return per_objfile->get_symtab (per_cu_data);
8698 }
8699
8700 /* Trivial hash function for die_info: the hash value of a DIE
8701 is its offset in .debug_info for this objfile. */
8702
8703 static hashval_t
8704 die_hash (const void *item)
8705 {
8706 const struct die_info *die = (const struct die_info *) item;
8707
8708 return to_underlying (die->sect_off);
8709 }
8710
8711 /* Trivial comparison function for die_info structures: two DIEs
8712 are equal if they have the same offset. */
8713
8714 static int
8715 die_eq (const void *item_lhs, const void *item_rhs)
8716 {
8717 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8718 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8719
8720 return die_lhs->sect_off == die_rhs->sect_off;
8721 }
8722
8723 /* Load the DIEs associated with PER_CU into memory.
8724
8725 In some cases, the caller, while reading partial symbols, will need to load
8726 the full symbols for the CU for some reason. It will already have a
8727 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8728 rather than creating a new one. */
8729
8730 static void
8731 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8732 dwarf2_per_objfile *per_objfile,
8733 dwarf2_cu *existing_cu,
8734 bool skip_partial,
8735 enum language pretend_language)
8736 {
8737 gdb_assert (! this_cu->is_debug_types);
8738
8739 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8740 if (reader.dummy_p)
8741 return;
8742
8743 struct dwarf2_cu *cu = reader.cu;
8744 const gdb_byte *info_ptr = reader.info_ptr;
8745
8746 gdb_assert (cu->die_hash == NULL);
8747 cu->die_hash =
8748 htab_create_alloc_ex (cu->header.length / 12,
8749 die_hash,
8750 die_eq,
8751 NULL,
8752 &cu->comp_unit_obstack,
8753 hashtab_obstack_allocate,
8754 dummy_obstack_deallocate);
8755
8756 if (reader.comp_unit_die->has_children)
8757 reader.comp_unit_die->child
8758 = read_die_and_siblings (&reader, reader.info_ptr,
8759 &info_ptr, reader.comp_unit_die);
8760 cu->dies = reader.comp_unit_die;
8761 /* comp_unit_die is not stored in die_hash, no need. */
8762
8763 /* We try not to read any attributes in this function, because not
8764 all CUs needed for references have been loaded yet, and symbol
8765 table processing isn't initialized. But we have to set the CU language,
8766 or we won't be able to build types correctly.
8767 Similarly, if we do not read the producer, we can not apply
8768 producer-specific interpretation. */
8769 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8770
8771 reader.keep ();
8772 }
8773
8774 /* Add a DIE to the delayed physname list. */
8775
8776 static void
8777 add_to_method_list (struct type *type, int fnfield_index, int index,
8778 const char *name, struct die_info *die,
8779 struct dwarf2_cu *cu)
8780 {
8781 struct delayed_method_info mi;
8782 mi.type = type;
8783 mi.fnfield_index = fnfield_index;
8784 mi.index = index;
8785 mi.name = name;
8786 mi.die = die;
8787 cu->method_list.push_back (mi);
8788 }
8789
8790 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8791 "const" / "volatile". If so, decrements LEN by the length of the
8792 modifier and return true. Otherwise return false. */
8793
8794 template<size_t N>
8795 static bool
8796 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8797 {
8798 size_t mod_len = sizeof (mod) - 1;
8799 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8800 {
8801 len -= mod_len;
8802 return true;
8803 }
8804 return false;
8805 }
8806
8807 /* Compute the physnames of any methods on the CU's method list.
8808
8809 The computation of method physnames is delayed in order to avoid the
8810 (bad) condition that one of the method's formal parameters is of an as yet
8811 incomplete type. */
8812
8813 static void
8814 compute_delayed_physnames (struct dwarf2_cu *cu)
8815 {
8816 /* Only C++ delays computing physnames. */
8817 if (cu->method_list.empty ())
8818 return;
8819 gdb_assert (cu->per_cu->lang == language_cplus);
8820
8821 for (const delayed_method_info &mi : cu->method_list)
8822 {
8823 const char *physname;
8824 struct fn_fieldlist *fn_flp
8825 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8826 physname = dwarf2_physname (mi.name, mi.die, cu);
8827 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8828 = physname ? physname : "";
8829
8830 /* Since there's no tag to indicate whether a method is a
8831 const/volatile overload, extract that information out of the
8832 demangled name. */
8833 if (physname != NULL)
8834 {
8835 size_t len = strlen (physname);
8836
8837 while (1)
8838 {
8839 if (physname[len] == ')') /* shortcut */
8840 break;
8841 else if (check_modifier (physname, len, " const"))
8842 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8843 else if (check_modifier (physname, len, " volatile"))
8844 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8845 else
8846 break;
8847 }
8848 }
8849 }
8850
8851 /* The list is no longer needed. */
8852 cu->method_list.clear ();
8853 }
8854
8855 /* Go objects should be embedded in a DW_TAG_module DIE,
8856 and it's not clear if/how imported objects will appear.
8857 To keep Go support simple until that's worked out,
8858 go back through what we've read and create something usable.
8859 We could do this while processing each DIE, and feels kinda cleaner,
8860 but that way is more invasive.
8861 This is to, for example, allow the user to type "p var" or "b main"
8862 without having to specify the package name, and allow lookups
8863 of module.object to work in contexts that use the expression
8864 parser. */
8865
8866 static void
8867 fixup_go_packaging (struct dwarf2_cu *cu)
8868 {
8869 gdb::unique_xmalloc_ptr<char> package_name;
8870 struct pending *list;
8871 int i;
8872
8873 for (list = *cu->get_builder ()->get_global_symbols ();
8874 list != NULL;
8875 list = list->next)
8876 {
8877 for (i = 0; i < list->nsyms; ++i)
8878 {
8879 struct symbol *sym = list->symbol[i];
8880
8881 if (sym->language () == language_go
8882 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8883 {
8884 gdb::unique_xmalloc_ptr<char> this_package_name
8885 (go_symbol_package_name (sym));
8886
8887 if (this_package_name == NULL)
8888 continue;
8889 if (package_name == NULL)
8890 package_name = std::move (this_package_name);
8891 else
8892 {
8893 struct objfile *objfile = cu->per_objfile->objfile;
8894 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8895 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8896 (symbol_symtab (sym) != NULL
8897 ? symtab_to_filename_for_display
8898 (symbol_symtab (sym))
8899 : objfile_name (objfile)),
8900 this_package_name.get (), package_name.get ());
8901 }
8902 }
8903 }
8904 }
8905
8906 if (package_name != NULL)
8907 {
8908 struct objfile *objfile = cu->per_objfile->objfile;
8909 const char *saved_package_name = objfile->intern (package_name.get ());
8910 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8911 saved_package_name);
8912 struct symbol *sym;
8913
8914 sym = new (&objfile->objfile_obstack) symbol;
8915 sym->set_language (language_go, &objfile->objfile_obstack);
8916 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8917 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8918 e.g., "main" finds the "main" module and not C's main(). */
8919 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8920 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8921 SYMBOL_TYPE (sym) = type;
8922
8923 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
8924 }
8925 }
8926
8927 /* Allocate a fully-qualified name consisting of the two parts on the
8928 obstack. */
8929
8930 static const char *
8931 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8932 {
8933 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8934 }
8935
8936 /* A helper that allocates a variant part to attach to a Rust enum
8937 type. OBSTACK is where the results should be allocated. TYPE is
8938 the type we're processing. DISCRIMINANT_INDEX is the index of the
8939 discriminant. It must be the index of one of the fields of TYPE,
8940 or -1 to mean there is no discriminant (univariant enum).
8941 DEFAULT_INDEX is the index of the default field; or -1 if there is
8942 no default. RANGES is indexed by "effective" field number (the
8943 field index, but omitting the discriminant and default fields) and
8944 must hold the discriminant values used by the variants. Note that
8945 RANGES must have a lifetime at least as long as OBSTACK -- either
8946 already allocated on it, or static. */
8947
8948 static void
8949 alloc_rust_variant (struct obstack *obstack, struct type *type,
8950 int discriminant_index, int default_index,
8951 gdb::array_view<discriminant_range> ranges)
8952 {
8953 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8954 gdb_assert (discriminant_index == -1
8955 || (discriminant_index >= 0
8956 && discriminant_index < type->num_fields ()));
8957 gdb_assert (default_index == -1
8958 || (default_index >= 0 && default_index < type->num_fields ()));
8959
8960 /* We have one variant for each non-discriminant field. */
8961 int n_variants = type->num_fields ();
8962 if (discriminant_index != -1)
8963 --n_variants;
8964
8965 variant *variants = new (obstack) variant[n_variants];
8966 int var_idx = 0;
8967 int range_idx = 0;
8968 for (int i = 0; i < type->num_fields (); ++i)
8969 {
8970 if (i == discriminant_index)
8971 continue;
8972
8973 variants[var_idx].first_field = i;
8974 variants[var_idx].last_field = i + 1;
8975
8976 /* The default field does not need a range, but other fields do.
8977 We skipped the discriminant above. */
8978 if (i != default_index)
8979 {
8980 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8981 ++range_idx;
8982 }
8983
8984 ++var_idx;
8985 }
8986
8987 gdb_assert (range_idx == ranges.size ());
8988 gdb_assert (var_idx == n_variants);
8989
8990 variant_part *part = new (obstack) variant_part;
8991 part->discriminant_index = discriminant_index;
8992 /* If there is no discriminant, then whether it is signed is of no
8993 consequence. */
8994 part->is_unsigned
8995 = (discriminant_index == -1
8996 ? false
8997 : type->field (discriminant_index).type ()->is_unsigned ());
8998 part->variants = gdb::array_view<variant> (variants, n_variants);
8999
9000 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9001 gdb::array_view<variant_part> *prop_value
9002 = new (storage) gdb::array_view<variant_part> (part, 1);
9003
9004 struct dynamic_prop prop;
9005 prop.set_variant_parts (prop_value);
9006
9007 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9008 }
9009
9010 /* Some versions of rustc emitted enums in an unusual way.
9011
9012 Ordinary enums were emitted as unions. The first element of each
9013 structure in the union was named "RUST$ENUM$DISR". This element
9014 held the discriminant.
9015
9016 These versions of Rust also implemented the "non-zero"
9017 optimization. When the enum had two values, and one is empty and
9018 the other holds a pointer that cannot be zero, the pointer is used
9019 as the discriminant, with a zero value meaning the empty variant.
9020 Here, the union's first member is of the form
9021 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9022 where the fieldnos are the indices of the fields that should be
9023 traversed in order to find the field (which may be several fields deep)
9024 and the variantname is the name of the variant of the case when the
9025 field is zero.
9026
9027 This function recognizes whether TYPE is of one of these forms,
9028 and, if so, smashes it to be a variant type. */
9029
9030 static void
9031 quirk_rust_enum (struct type *type, struct objfile *objfile)
9032 {
9033 gdb_assert (type->code () == TYPE_CODE_UNION);
9034
9035 /* We don't need to deal with empty enums. */
9036 if (type->num_fields () == 0)
9037 return;
9038
9039 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9040 if (type->num_fields () == 1
9041 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
9042 {
9043 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
9044
9045 /* Decode the field name to find the offset of the
9046 discriminant. */
9047 ULONGEST bit_offset = 0;
9048 struct type *field_type = type->field (0).type ();
9049 while (name[0] >= '0' && name[0] <= '9')
9050 {
9051 char *tail;
9052 unsigned long index = strtoul (name, &tail, 10);
9053 name = tail;
9054 if (*name != '$'
9055 || index >= field_type->num_fields ()
9056 || (field_type->field (index).loc_kind ()
9057 != FIELD_LOC_KIND_BITPOS))
9058 {
9059 complaint (_("Could not parse Rust enum encoding string \"%s\""
9060 "[in module %s]"),
9061 type->field (0).name (),
9062 objfile_name (objfile));
9063 return;
9064 }
9065 ++name;
9066
9067 bit_offset += field_type->field (index).loc_bitpos ();
9068 field_type = field_type->field (index).type ();
9069 }
9070
9071 /* Smash this type to be a structure type. We have to do this
9072 because the type has already been recorded. */
9073 type->set_code (TYPE_CODE_STRUCT);
9074 type->set_num_fields (3);
9075 /* Save the field we care about. */
9076 struct field saved_field = type->field (0);
9077 type->set_fields
9078 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9079
9080 /* Put the discriminant at index 0. */
9081 type->field (0).set_type (field_type);
9082 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9083 type->field (0).set_name ("<<discriminant>>");
9084 type->field (0).set_loc_bitpos (bit_offset);
9085
9086 /* The order of fields doesn't really matter, so put the real
9087 field at index 1 and the data-less field at index 2. */
9088 type->field (1) = saved_field;
9089 type->field (1).set_name
9090 (rust_last_path_segment (type->field (1).type ()->name ()));
9091 type->field (1).type ()->set_name
9092 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9093 type->field (1).name ()));
9094
9095 const char *dataless_name
9096 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9097 name);
9098 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9099 dataless_name);
9100 type->field (2).set_type (dataless_type);
9101 /* NAME points into the original discriminant name, which
9102 already has the correct lifetime. */
9103 type->field (2).set_name (name);
9104 type->field (2).set_loc_bitpos (0);
9105
9106 /* Indicate that this is a variant type. */
9107 static discriminant_range ranges[1] = { { 0, 0 } };
9108 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9109 }
9110 /* A union with a single anonymous field is probably an old-style
9111 univariant enum. */
9112 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
9113 {
9114 /* Smash this type to be a structure type. We have to do this
9115 because the type has already been recorded. */
9116 type->set_code (TYPE_CODE_STRUCT);
9117
9118 struct type *field_type = type->field (0).type ();
9119 const char *variant_name
9120 = rust_last_path_segment (field_type->name ());
9121 type->field (0).set_name (variant_name);
9122 field_type->set_name
9123 (rust_fully_qualify (&objfile->objfile_obstack,
9124 type->name (), variant_name));
9125
9126 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9127 }
9128 else
9129 {
9130 struct type *disr_type = nullptr;
9131 for (int i = 0; i < type->num_fields (); ++i)
9132 {
9133 disr_type = type->field (i).type ();
9134
9135 if (disr_type->code () != TYPE_CODE_STRUCT)
9136 {
9137 /* All fields of a true enum will be structs. */
9138 return;
9139 }
9140 else if (disr_type->num_fields () == 0)
9141 {
9142 /* Could be data-less variant, so keep going. */
9143 disr_type = nullptr;
9144 }
9145 else if (strcmp (disr_type->field (0).name (),
9146 "RUST$ENUM$DISR") != 0)
9147 {
9148 /* Not a Rust enum. */
9149 return;
9150 }
9151 else
9152 {
9153 /* Found one. */
9154 break;
9155 }
9156 }
9157
9158 /* If we got here without a discriminant, then it's probably
9159 just a union. */
9160 if (disr_type == nullptr)
9161 return;
9162
9163 /* Smash this type to be a structure type. We have to do this
9164 because the type has already been recorded. */
9165 type->set_code (TYPE_CODE_STRUCT);
9166
9167 /* Make space for the discriminant field. */
9168 struct field *disr_field = &disr_type->field (0);
9169 field *new_fields
9170 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9171 * sizeof (struct field)));
9172 memcpy (new_fields + 1, type->fields (),
9173 type->num_fields () * sizeof (struct field));
9174 type->set_fields (new_fields);
9175 type->set_num_fields (type->num_fields () + 1);
9176
9177 /* Install the discriminant at index 0 in the union. */
9178 type->field (0) = *disr_field;
9179 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9180 type->field (0).set_name ("<<discriminant>>");
9181
9182 /* We need a way to find the correct discriminant given a
9183 variant name. For convenience we build a map here. */
9184 struct type *enum_type = disr_field->type ();
9185 std::unordered_map<std::string, ULONGEST> discriminant_map;
9186 for (int i = 0; i < enum_type->num_fields (); ++i)
9187 {
9188 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
9189 {
9190 const char *name
9191 = rust_last_path_segment (enum_type->field (i).name ());
9192 discriminant_map[name] = enum_type->field (i).loc_enumval ();
9193 }
9194 }
9195
9196 int n_fields = type->num_fields ();
9197 /* We don't need a range entry for the discriminant, but we do
9198 need one for every other field, as there is no default
9199 variant. */
9200 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9201 discriminant_range,
9202 n_fields - 1);
9203 /* Skip the discriminant here. */
9204 for (int i = 1; i < n_fields; ++i)
9205 {
9206 /* Find the final word in the name of this variant's type.
9207 That name can be used to look up the correct
9208 discriminant. */
9209 const char *variant_name
9210 = rust_last_path_segment (type->field (i).type ()->name ());
9211
9212 auto iter = discriminant_map.find (variant_name);
9213 if (iter != discriminant_map.end ())
9214 {
9215 ranges[i - 1].low = iter->second;
9216 ranges[i - 1].high = iter->second;
9217 }
9218
9219 /* In Rust, each element should have the size of the
9220 enclosing enum. */
9221 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9222
9223 /* Remove the discriminant field, if it exists. */
9224 struct type *sub_type = type->field (i).type ();
9225 if (sub_type->num_fields () > 0)
9226 {
9227 sub_type->set_num_fields (sub_type->num_fields () - 1);
9228 sub_type->set_fields (sub_type->fields () + 1);
9229 }
9230 type->field (i).set_name (variant_name);
9231 sub_type->set_name
9232 (rust_fully_qualify (&objfile->objfile_obstack,
9233 type->name (), variant_name));
9234 }
9235
9236 /* Indicate that this is a variant type. */
9237 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9238 gdb::array_view<discriminant_range> (ranges,
9239 n_fields - 1));
9240 }
9241 }
9242
9243 /* Rewrite some Rust unions to be structures with variants parts. */
9244
9245 static void
9246 rust_union_quirks (struct dwarf2_cu *cu)
9247 {
9248 gdb_assert (cu->per_cu->lang == language_rust);
9249 for (type *type_ : cu->rust_unions)
9250 quirk_rust_enum (type_, cu->per_objfile->objfile);
9251 /* We don't need this any more. */
9252 cu->rust_unions.clear ();
9253 }
9254
9255 /* See read.h. */
9256
9257 type_unit_group_unshareable *
9258 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9259 {
9260 auto iter = this->m_type_units.find (tu_group);
9261 if (iter != this->m_type_units.end ())
9262 return iter->second.get ();
9263
9264 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9265 type_unit_group_unshareable *result = uniq.get ();
9266 this->m_type_units[tu_group] = std::move (uniq);
9267 return result;
9268 }
9269
9270 struct type *
9271 dwarf2_per_objfile::get_type_for_signatured_type
9272 (signatured_type *sig_type) const
9273 {
9274 auto iter = this->m_type_map.find (sig_type);
9275 if (iter == this->m_type_map.end ())
9276 return nullptr;
9277
9278 return iter->second;
9279 }
9280
9281 void dwarf2_per_objfile::set_type_for_signatured_type
9282 (signatured_type *sig_type, struct type *type)
9283 {
9284 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9285
9286 this->m_type_map[sig_type] = type;
9287 }
9288
9289 /* A helper function for computing the list of all symbol tables
9290 included by PER_CU. */
9291
9292 static void
9293 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9294 htab_t all_children, htab_t all_type_symtabs,
9295 dwarf2_per_cu_data *per_cu,
9296 dwarf2_per_objfile *per_objfile,
9297 struct compunit_symtab *immediate_parent)
9298 {
9299 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9300 if (*slot != NULL)
9301 {
9302 /* This inclusion and its children have been processed. */
9303 return;
9304 }
9305
9306 *slot = per_cu;
9307
9308 /* Only add a CU if it has a symbol table. */
9309 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9310 if (cust != NULL)
9311 {
9312 /* If this is a type unit only add its symbol table if we haven't
9313 seen it yet (type unit per_cu's can share symtabs). */
9314 if (per_cu->is_debug_types)
9315 {
9316 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9317 if (*slot == NULL)
9318 {
9319 *slot = cust;
9320 result->push_back (cust);
9321 if (cust->user == NULL)
9322 cust->user = immediate_parent;
9323 }
9324 }
9325 else
9326 {
9327 result->push_back (cust);
9328 if (cust->user == NULL)
9329 cust->user = immediate_parent;
9330 }
9331 }
9332
9333 if (!per_cu->imported_symtabs_empty ())
9334 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9335 {
9336 recursively_compute_inclusions (result, all_children,
9337 all_type_symtabs, ptr, per_objfile,
9338 cust);
9339 }
9340 }
9341
9342 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9343 PER_CU. */
9344
9345 static void
9346 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9347 dwarf2_per_objfile *per_objfile)
9348 {
9349 gdb_assert (! per_cu->is_debug_types);
9350
9351 if (!per_cu->imported_symtabs_empty ())
9352 {
9353 int len;
9354 std::vector<compunit_symtab *> result_symtabs;
9355 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9356
9357 /* If we don't have a symtab, we can just skip this case. */
9358 if (cust == NULL)
9359 return;
9360
9361 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9362 htab_eq_pointer,
9363 NULL, xcalloc, xfree));
9364 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9365 htab_eq_pointer,
9366 NULL, xcalloc, xfree));
9367
9368 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9369 {
9370 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9371 all_type_symtabs.get (), ptr,
9372 per_objfile, cust);
9373 }
9374
9375 /* Now we have a transitive closure of all the included symtabs. */
9376 len = result_symtabs.size ();
9377 cust->includes
9378 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9379 struct compunit_symtab *, len + 1);
9380 memcpy (cust->includes, result_symtabs.data (),
9381 len * sizeof (compunit_symtab *));
9382 cust->includes[len] = NULL;
9383 }
9384 }
9385
9386 /* Compute the 'includes' field for the symtabs of all the CUs we just
9387 read. */
9388
9389 static void
9390 process_cu_includes (dwarf2_per_objfile *per_objfile)
9391 {
9392 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9393 {
9394 if (! iter->is_debug_types)
9395 compute_compunit_symtab_includes (iter, per_objfile);
9396 }
9397
9398 per_objfile->per_bfd->just_read_cus.clear ();
9399 }
9400
9401 /* Generate full symbol information for CU, whose DIEs have
9402 already been loaded into memory. */
9403
9404 static void
9405 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9406 {
9407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9408 struct objfile *objfile = per_objfile->objfile;
9409 struct gdbarch *gdbarch = objfile->arch ();
9410 CORE_ADDR lowpc, highpc;
9411 struct compunit_symtab *cust;
9412 CORE_ADDR baseaddr;
9413 struct block *static_block;
9414 CORE_ADDR addr;
9415
9416 baseaddr = objfile->text_section_offset ();
9417
9418 /* Clear the list here in case something was left over. */
9419 cu->method_list.clear ();
9420
9421 dwarf2_find_base_address (cu->dies, cu);
9422
9423 /* Before we start reading the top-level DIE, ensure it has a valid tag
9424 type. */
9425 switch (cu->dies->tag)
9426 {
9427 case DW_TAG_compile_unit:
9428 case DW_TAG_partial_unit:
9429 case DW_TAG_type_unit:
9430 break;
9431 default:
9432 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9433 dwarf_tag_name (cu->dies->tag),
9434 sect_offset_str (cu->per_cu->sect_off),
9435 objfile_name (per_objfile->objfile));
9436 }
9437
9438 /* Do line number decoding in read_file_scope () */
9439 process_die (cu->dies, cu);
9440
9441 /* For now fudge the Go package. */
9442 if (cu->per_cu->lang == language_go)
9443 fixup_go_packaging (cu);
9444
9445 /* Now that we have processed all the DIEs in the CU, all the types
9446 should be complete, and it should now be safe to compute all of the
9447 physnames. */
9448 compute_delayed_physnames (cu);
9449
9450 if (cu->per_cu->lang == language_rust)
9451 rust_union_quirks (cu);
9452
9453 /* Some compilers don't define a DW_AT_high_pc attribute for the
9454 compilation unit. If the DW_AT_high_pc is missing, synthesize
9455 it, by scanning the DIE's below the compilation unit. */
9456 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9457
9458 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9459 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9460
9461 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9462 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9463 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9464 addrmap to help ensure it has an accurate map of pc values belonging to
9465 this comp unit. */
9466 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9467
9468 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9469 SECT_OFF_TEXT (objfile),
9470 0);
9471
9472 if (cust != NULL)
9473 {
9474 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9475
9476 /* Set symtab language to language from DW_AT_language. If the
9477 compilation is from a C file generated by language preprocessors, do
9478 not set the language if it was already deduced by start_subfile. */
9479 if (!(cu->per_cu->lang == language_c
9480 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9481 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9482
9483 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9484 produce DW_AT_location with location lists but it can be possibly
9485 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9486 there were bugs in prologue debug info, fixed later in GCC-4.5
9487 by "unwind info for epilogues" patch (which is not directly related).
9488
9489 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9490 needed, it would be wrong due to missing DW_AT_producer there.
9491
9492 Still one can confuse GDB by using non-standard GCC compilation
9493 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9494 */
9495 if (cu->has_loclist && gcc_4_minor >= 5)
9496 cust->locations_valid = 1;
9497
9498 if (gcc_4_minor >= 5)
9499 cust->epilogue_unwind_valid = 1;
9500
9501 cust->set_call_site_htab (cu->call_site_htab);
9502 }
9503
9504 per_objfile->set_symtab (cu->per_cu, cust);
9505
9506 /* Push it for inclusion processing later. */
9507 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9508
9509 /* Not needed any more. */
9510 cu->reset_builder ();
9511 }
9512
9513 /* Generate full symbol information for type unit CU, whose DIEs have
9514 already been loaded into memory. */
9515
9516 static void
9517 process_full_type_unit (dwarf2_cu *cu,
9518 enum language pretend_language)
9519 {
9520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9521 struct objfile *objfile = per_objfile->objfile;
9522 struct compunit_symtab *cust;
9523 struct signatured_type *sig_type;
9524
9525 gdb_assert (cu->per_cu->is_debug_types);
9526 sig_type = (struct signatured_type *) cu->per_cu;
9527
9528 /* Clear the list here in case something was left over. */
9529 cu->method_list.clear ();
9530
9531 /* The symbol tables are set up in read_type_unit_scope. */
9532 process_die (cu->dies, cu);
9533
9534 /* For now fudge the Go package. */
9535 if (cu->per_cu->lang == language_go)
9536 fixup_go_packaging (cu);
9537
9538 /* Now that we have processed all the DIEs in the CU, all the types
9539 should be complete, and it should now be safe to compute all of the
9540 physnames. */
9541 compute_delayed_physnames (cu);
9542
9543 if (cu->per_cu->lang == language_rust)
9544 rust_union_quirks (cu);
9545
9546 /* TUs share symbol tables.
9547 If this is the first TU to use this symtab, complete the construction
9548 of it with end_expandable_symtab. Otherwise, complete the addition of
9549 this TU's symbols to the existing symtab. */
9550 type_unit_group_unshareable *tug_unshare =
9551 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9552 if (tug_unshare->compunit_symtab == NULL)
9553 {
9554 buildsym_compunit *builder = cu->get_builder ();
9555 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9556 tug_unshare->compunit_symtab = cust;
9557
9558 if (cust != NULL)
9559 {
9560 /* Set symtab language to language from DW_AT_language. If the
9561 compilation is from a C file generated by language preprocessors,
9562 do not set the language if it was already deduced by
9563 start_subfile. */
9564 if (!(cu->per_cu->lang == language_c
9565 && COMPUNIT_FILETABS (cust)->language != language_c))
9566 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
9567 }
9568 }
9569 else
9570 {
9571 cu->get_builder ()->augment_type_symtab ();
9572 cust = tug_unshare->compunit_symtab;
9573 }
9574
9575 per_objfile->set_symtab (cu->per_cu, cust);
9576
9577 /* Not needed any more. */
9578 cu->reset_builder ();
9579 }
9580
9581 /* Process an imported unit DIE. */
9582
9583 static void
9584 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9585 {
9586 struct attribute *attr;
9587
9588 /* For now we don't handle imported units in type units. */
9589 if (cu->per_cu->is_debug_types)
9590 {
9591 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9592 " supported in type units [in module %s]"),
9593 objfile_name (cu->per_objfile->objfile));
9594 }
9595
9596 attr = dwarf2_attr (die, DW_AT_import, cu);
9597 if (attr != NULL)
9598 {
9599 sect_offset sect_off = attr->get_ref_die_offset ();
9600 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9601 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9602 dwarf2_per_cu_data *per_cu
9603 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9604 per_objfile->per_bfd);
9605
9606 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9607 into another compilation unit, at root level. Regard this as a hint,
9608 and ignore it. */
9609 if (die->parent && die->parent->parent == NULL
9610 && per_cu->unit_type == DW_UT_compile
9611 && per_cu->lang == language_cplus)
9612 return;
9613
9614 /* If necessary, add it to the queue and load its DIEs. */
9615 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9616 cu->per_cu->lang))
9617 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9618 false, cu->per_cu->lang);
9619
9620 cu->per_cu->imported_symtabs_push (per_cu);
9621 }
9622 }
9623
9624 /* RAII object that represents a process_die scope: i.e.,
9625 starts/finishes processing a DIE. */
9626 class process_die_scope
9627 {
9628 public:
9629 process_die_scope (die_info *die, dwarf2_cu *cu)
9630 : m_die (die), m_cu (cu)
9631 {
9632 /* We should only be processing DIEs not already in process. */
9633 gdb_assert (!m_die->in_process);
9634 m_die->in_process = true;
9635 }
9636
9637 ~process_die_scope ()
9638 {
9639 m_die->in_process = false;
9640
9641 /* If we're done processing the DIE for the CU that owns the line
9642 header, we don't need the line header anymore. */
9643 if (m_cu->line_header_die_owner == m_die)
9644 {
9645 delete m_cu->line_header;
9646 m_cu->line_header = NULL;
9647 m_cu->line_header_die_owner = NULL;
9648 }
9649 }
9650
9651 private:
9652 die_info *m_die;
9653 dwarf2_cu *m_cu;
9654 };
9655
9656 /* Process a die and its children. */
9657
9658 static void
9659 process_die (struct die_info *die, struct dwarf2_cu *cu)
9660 {
9661 process_die_scope scope (die, cu);
9662
9663 switch (die->tag)
9664 {
9665 case DW_TAG_padding:
9666 break;
9667 case DW_TAG_compile_unit:
9668 case DW_TAG_partial_unit:
9669 read_file_scope (die, cu);
9670 break;
9671 case DW_TAG_type_unit:
9672 read_type_unit_scope (die, cu);
9673 break;
9674 case DW_TAG_subprogram:
9675 /* Nested subprograms in Fortran get a prefix. */
9676 if (cu->per_cu->lang == language_fortran
9677 && die->parent != NULL
9678 && die->parent->tag == DW_TAG_subprogram)
9679 cu->processing_has_namespace_info = true;
9680 /* Fall through. */
9681 case DW_TAG_inlined_subroutine:
9682 read_func_scope (die, cu);
9683 break;
9684 case DW_TAG_lexical_block:
9685 case DW_TAG_try_block:
9686 case DW_TAG_catch_block:
9687 read_lexical_block_scope (die, cu);
9688 break;
9689 case DW_TAG_call_site:
9690 case DW_TAG_GNU_call_site:
9691 read_call_site_scope (die, cu);
9692 break;
9693 case DW_TAG_class_type:
9694 case DW_TAG_interface_type:
9695 case DW_TAG_structure_type:
9696 case DW_TAG_union_type:
9697 process_structure_scope (die, cu);
9698 break;
9699 case DW_TAG_enumeration_type:
9700 process_enumeration_scope (die, cu);
9701 break;
9702
9703 /* These dies have a type, but processing them does not create
9704 a symbol or recurse to process the children. Therefore we can
9705 read them on-demand through read_type_die. */
9706 case DW_TAG_subroutine_type:
9707 case DW_TAG_set_type:
9708 case DW_TAG_pointer_type:
9709 case DW_TAG_ptr_to_member_type:
9710 case DW_TAG_reference_type:
9711 case DW_TAG_rvalue_reference_type:
9712 case DW_TAG_string_type:
9713 break;
9714
9715 case DW_TAG_array_type:
9716 /* We only need to handle this case for Ada -- in other
9717 languages, it's normal for the compiler to emit a typedef
9718 instead. */
9719 if (cu->per_cu->lang != language_ada)
9720 break;
9721 /* FALLTHROUGH */
9722 case DW_TAG_base_type:
9723 case DW_TAG_subrange_type:
9724 case DW_TAG_typedef:
9725 /* Add a typedef symbol for the type definition, if it has a
9726 DW_AT_name. */
9727 new_symbol (die, read_type_die (die, cu), cu);
9728 break;
9729 case DW_TAG_common_block:
9730 read_common_block (die, cu);
9731 break;
9732 case DW_TAG_common_inclusion:
9733 break;
9734 case DW_TAG_namespace:
9735 cu->processing_has_namespace_info = true;
9736 read_namespace (die, cu);
9737 break;
9738 case DW_TAG_module:
9739 cu->processing_has_namespace_info = true;
9740 read_module (die, cu);
9741 break;
9742 case DW_TAG_imported_declaration:
9743 cu->processing_has_namespace_info = true;
9744 if (read_namespace_alias (die, cu))
9745 break;
9746 /* The declaration is not a global namespace alias. */
9747 /* Fall through. */
9748 case DW_TAG_imported_module:
9749 cu->processing_has_namespace_info = true;
9750 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9751 || cu->per_cu->lang != language_fortran))
9752 complaint (_("Tag '%s' has unexpected children"),
9753 dwarf_tag_name (die->tag));
9754 read_import_statement (die, cu);
9755 break;
9756
9757 case DW_TAG_imported_unit:
9758 process_imported_unit_die (die, cu);
9759 break;
9760
9761 case DW_TAG_variable:
9762 read_variable (die, cu);
9763 break;
9764
9765 default:
9766 new_symbol (die, NULL, cu);
9767 break;
9768 }
9769 }
9770 \f
9771 /* DWARF name computation. */
9772
9773 /* A helper function for dwarf2_compute_name which determines whether DIE
9774 needs to have the name of the scope prepended to the name listed in the
9775 die. */
9776
9777 static int
9778 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9779 {
9780 struct attribute *attr;
9781
9782 switch (die->tag)
9783 {
9784 case DW_TAG_namespace:
9785 case DW_TAG_typedef:
9786 case DW_TAG_class_type:
9787 case DW_TAG_interface_type:
9788 case DW_TAG_structure_type:
9789 case DW_TAG_union_type:
9790 case DW_TAG_enumeration_type:
9791 case DW_TAG_enumerator:
9792 case DW_TAG_subprogram:
9793 case DW_TAG_inlined_subroutine:
9794 case DW_TAG_member:
9795 case DW_TAG_imported_declaration:
9796 return 1;
9797
9798 case DW_TAG_variable:
9799 case DW_TAG_constant:
9800 /* We only need to prefix "globally" visible variables. These include
9801 any variable marked with DW_AT_external or any variable that
9802 lives in a namespace. [Variables in anonymous namespaces
9803 require prefixing, but they are not DW_AT_external.] */
9804
9805 if (dwarf2_attr (die, DW_AT_specification, cu))
9806 {
9807 struct dwarf2_cu *spec_cu = cu;
9808
9809 return die_needs_namespace (die_specification (die, &spec_cu),
9810 spec_cu);
9811 }
9812
9813 attr = dwarf2_attr (die, DW_AT_external, cu);
9814 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9815 && die->parent->tag != DW_TAG_module)
9816 return 0;
9817 /* A variable in a lexical block of some kind does not need a
9818 namespace, even though in C++ such variables may be external
9819 and have a mangled name. */
9820 if (die->parent->tag == DW_TAG_lexical_block
9821 || die->parent->tag == DW_TAG_try_block
9822 || die->parent->tag == DW_TAG_catch_block
9823 || die->parent->tag == DW_TAG_subprogram)
9824 return 0;
9825 return 1;
9826
9827 default:
9828 return 0;
9829 }
9830 }
9831
9832 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9833 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9834 defined for the given DIE. */
9835
9836 static struct attribute *
9837 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9838 {
9839 struct attribute *attr;
9840
9841 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9842 if (attr == NULL)
9843 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9844
9845 return attr;
9846 }
9847
9848 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9849 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9850 defined for the given DIE. */
9851
9852 static const char *
9853 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9854 {
9855 const char *linkage_name;
9856
9857 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9858 if (linkage_name == NULL)
9859 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9860
9861 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9862 See https://github.com/rust-lang/rust/issues/32925. */
9863 if (cu->per_cu->lang == language_rust && linkage_name != NULL
9864 && strchr (linkage_name, '{') != NULL)
9865 linkage_name = NULL;
9866
9867 return linkage_name;
9868 }
9869
9870 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9871 compute the physname for the object, which include a method's:
9872 - formal parameters (C++),
9873 - receiver type (Go),
9874
9875 The term "physname" is a bit confusing.
9876 For C++, for example, it is the demangled name.
9877 For Go, for example, it's the mangled name.
9878
9879 For Ada, return the DIE's linkage name rather than the fully qualified
9880 name. PHYSNAME is ignored..
9881
9882 The result is allocated on the objfile->per_bfd's obstack and
9883 canonicalized. */
9884
9885 static const char *
9886 dwarf2_compute_name (const char *name,
9887 struct die_info *die, struct dwarf2_cu *cu,
9888 int physname)
9889 {
9890 struct objfile *objfile = cu->per_objfile->objfile;
9891
9892 if (name == NULL)
9893 name = dwarf2_name (die, cu);
9894
9895 enum language lang = cu->per_cu->lang;
9896
9897 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9898 but otherwise compute it by typename_concat inside GDB.
9899 FIXME: Actually this is not really true, or at least not always true.
9900 It's all very confusing. compute_and_set_names doesn't try to demangle
9901 Fortran names because there is no mangling standard. So new_symbol
9902 will set the demangled name to the result of dwarf2_full_name, and it is
9903 the demangled name that GDB uses if it exists. */
9904 if (lang == language_ada
9905 || (lang == language_fortran && physname))
9906 {
9907 /* For Ada unit, we prefer the linkage name over the name, as
9908 the former contains the exported name, which the user expects
9909 to be able to reference. Ideally, we want the user to be able
9910 to reference this entity using either natural or linkage name,
9911 but we haven't started looking at this enhancement yet. */
9912 const char *linkage_name = dw2_linkage_name (die, cu);
9913
9914 if (linkage_name != NULL)
9915 return linkage_name;
9916 }
9917
9918 /* These are the only languages we know how to qualify names in. */
9919 if (name != NULL
9920 && (lang == language_cplus
9921 || lang == language_fortran || lang == language_d
9922 || lang == language_rust))
9923 {
9924 if (die_needs_namespace (die, cu))
9925 {
9926 const char *prefix;
9927 const char *canonical_name = NULL;
9928
9929 string_file buf;
9930
9931 prefix = determine_prefix (die, cu);
9932 if (*prefix != '\0')
9933 {
9934 gdb::unique_xmalloc_ptr<char> prefixed_name
9935 (typename_concat (NULL, prefix, name, physname, cu));
9936
9937 buf.puts (prefixed_name.get ());
9938 }
9939 else
9940 buf.puts (name);
9941
9942 /* Template parameters may be specified in the DIE's DW_AT_name, or
9943 as children with DW_TAG_template_type_param or
9944 DW_TAG_value_type_param. If the latter, add them to the name
9945 here. If the name already has template parameters, then
9946 skip this step; some versions of GCC emit both, and
9947 it is more efficient to use the pre-computed name.
9948
9949 Something to keep in mind about this process: it is very
9950 unlikely, or in some cases downright impossible, to produce
9951 something that will match the mangled name of a function.
9952 If the definition of the function has the same debug info,
9953 we should be able to match up with it anyway. But fallbacks
9954 using the minimal symbol, for instance to find a method
9955 implemented in a stripped copy of libstdc++, will not work.
9956 If we do not have debug info for the definition, we will have to
9957 match them up some other way.
9958
9959 When we do name matching there is a related problem with function
9960 templates; two instantiated function templates are allowed to
9961 differ only by their return types, which we do not add here. */
9962
9963 if (lang == language_cplus && strchr (name, '<') == NULL)
9964 {
9965 struct attribute *attr;
9966 struct die_info *child;
9967 int first = 1;
9968
9969 die->building_fullname = 1;
9970
9971 for (child = die->child; child != NULL; child = child->sibling)
9972 {
9973 struct type *type;
9974 LONGEST value;
9975 const gdb_byte *bytes;
9976 struct dwarf2_locexpr_baton *baton;
9977 struct value *v;
9978
9979 if (child->tag != DW_TAG_template_type_param
9980 && child->tag != DW_TAG_template_value_param)
9981 continue;
9982
9983 if (first)
9984 {
9985 buf.puts ("<");
9986 first = 0;
9987 }
9988 else
9989 buf.puts (", ");
9990
9991 attr = dwarf2_attr (child, DW_AT_type, cu);
9992 if (attr == NULL)
9993 {
9994 complaint (_("template parameter missing DW_AT_type"));
9995 buf.puts ("UNKNOWN_TYPE");
9996 continue;
9997 }
9998 type = die_type (child, cu);
9999
10000 if (child->tag == DW_TAG_template_type_param)
10001 {
10002 cu->language_defn->print_type (type, "", &buf, -1, 0,
10003 &type_print_raw_options);
10004 continue;
10005 }
10006
10007 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10008 if (attr == NULL)
10009 {
10010 complaint (_("template parameter missing "
10011 "DW_AT_const_value"));
10012 buf.puts ("UNKNOWN_VALUE");
10013 continue;
10014 }
10015
10016 dwarf2_const_value_attr (attr, type, name,
10017 &cu->comp_unit_obstack, cu,
10018 &value, &bytes, &baton);
10019
10020 if (type->has_no_signedness ())
10021 /* GDB prints characters as NUMBER 'CHAR'. If that's
10022 changed, this can use value_print instead. */
10023 cu->language_defn->printchar (value, type, &buf);
10024 else
10025 {
10026 struct value_print_options opts;
10027
10028 if (baton != NULL)
10029 v = dwarf2_evaluate_loc_desc (type, NULL,
10030 baton->data,
10031 baton->size,
10032 baton->per_cu,
10033 baton->per_objfile);
10034 else if (bytes != NULL)
10035 {
10036 v = allocate_value (type);
10037 memcpy (value_contents_writeable (v).data (), bytes,
10038 TYPE_LENGTH (type));
10039 }
10040 else
10041 v = value_from_longest (type, value);
10042
10043 /* Specify decimal so that we do not depend on
10044 the radix. */
10045 get_formatted_print_options (&opts, 'd');
10046 opts.raw = 1;
10047 value_print (v, &buf, &opts);
10048 release_value (v);
10049 }
10050 }
10051
10052 die->building_fullname = 0;
10053
10054 if (!first)
10055 {
10056 /* Close the argument list, with a space if necessary
10057 (nested templates). */
10058 if (!buf.empty () && buf.string ().back () == '>')
10059 buf.puts (" >");
10060 else
10061 buf.puts (">");
10062 }
10063 }
10064
10065 /* For C++ methods, append formal parameter type
10066 information, if PHYSNAME. */
10067
10068 if (physname && die->tag == DW_TAG_subprogram
10069 && lang == language_cplus)
10070 {
10071 struct type *type = read_type_die (die, cu);
10072
10073 c_type_print_args (type, &buf, 1, lang,
10074 &type_print_raw_options);
10075
10076 if (lang == language_cplus)
10077 {
10078 /* Assume that an artificial first parameter is
10079 "this", but do not crash if it is not. RealView
10080 marks unnamed (and thus unused) parameters as
10081 artificial; there is no way to differentiate
10082 the two cases. */
10083 if (type->num_fields () > 0
10084 && TYPE_FIELD_ARTIFICIAL (type, 0)
10085 && type->field (0).type ()->code () == TYPE_CODE_PTR
10086 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10087 buf.puts (" const");
10088 }
10089 }
10090
10091 const std::string &intermediate_name = buf.string ();
10092
10093 if (lang == language_cplus)
10094 canonical_name
10095 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10096 objfile);
10097
10098 /* If we only computed INTERMEDIATE_NAME, or if
10099 INTERMEDIATE_NAME is already canonical, then we need to
10100 intern it. */
10101 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10102 name = objfile->intern (intermediate_name);
10103 else
10104 name = canonical_name;
10105 }
10106 }
10107
10108 return name;
10109 }
10110
10111 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10112 If scope qualifiers are appropriate they will be added. The result
10113 will be allocated on the storage_obstack, or NULL if the DIE does
10114 not have a name. NAME may either be from a previous call to
10115 dwarf2_name or NULL.
10116
10117 The output string will be canonicalized (if C++). */
10118
10119 static const char *
10120 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10121 {
10122 return dwarf2_compute_name (name, die, cu, 0);
10123 }
10124
10125 /* Construct a physname for the given DIE in CU. NAME may either be
10126 from a previous call to dwarf2_name or NULL. The result will be
10127 allocated on the objfile_objstack or NULL if the DIE does not have a
10128 name.
10129
10130 The output string will be canonicalized (if C++). */
10131
10132 static const char *
10133 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10134 {
10135 struct objfile *objfile = cu->per_objfile->objfile;
10136 const char *retval, *mangled = NULL, *canon = NULL;
10137 int need_copy = 1;
10138
10139 /* In this case dwarf2_compute_name is just a shortcut not building anything
10140 on its own. */
10141 if (!die_needs_namespace (die, cu))
10142 return dwarf2_compute_name (name, die, cu, 1);
10143
10144 if (cu->per_cu->lang != language_rust)
10145 mangled = dw2_linkage_name (die, cu);
10146
10147 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10148 has computed. */
10149 gdb::unique_xmalloc_ptr<char> demangled;
10150 if (mangled != NULL)
10151 {
10152 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
10153 {
10154 /* Do nothing (do not demangle the symbol name). */
10155 }
10156 else
10157 {
10158 /* Use DMGL_RET_DROP for C++ template functions to suppress
10159 their return type. It is easier for GDB users to search
10160 for such functions as `name(params)' than `long name(params)'.
10161 In such case the minimal symbol names do not match the full
10162 symbol names but for template functions there is never a need
10163 to look up their definition from their declaration so
10164 the only disadvantage remains the minimal symbol variant
10165 `long name(params)' does not have the proper inferior type. */
10166 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
10167 | DMGL_RET_DROP));
10168 }
10169 if (demangled)
10170 canon = demangled.get ();
10171 else
10172 {
10173 canon = mangled;
10174 need_copy = 0;
10175 }
10176 }
10177
10178 if (canon == NULL || check_physname)
10179 {
10180 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10181
10182 if (canon != NULL && strcmp (physname, canon) != 0)
10183 {
10184 /* It may not mean a bug in GDB. The compiler could also
10185 compute DW_AT_linkage_name incorrectly. But in such case
10186 GDB would need to be bug-to-bug compatible. */
10187
10188 complaint (_("Computed physname <%s> does not match demangled <%s> "
10189 "(from linkage <%s>) - DIE at %s [in module %s]"),
10190 physname, canon, mangled, sect_offset_str (die->sect_off),
10191 objfile_name (objfile));
10192
10193 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10194 is available here - over computed PHYSNAME. It is safer
10195 against both buggy GDB and buggy compilers. */
10196
10197 retval = canon;
10198 }
10199 else
10200 {
10201 retval = physname;
10202 need_copy = 0;
10203 }
10204 }
10205 else
10206 retval = canon;
10207
10208 if (need_copy)
10209 retval = objfile->intern (retval);
10210
10211 return retval;
10212 }
10213
10214 /* Inspect DIE in CU for a namespace alias. If one exists, record
10215 a new symbol for it.
10216
10217 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10218
10219 static int
10220 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10221 {
10222 struct attribute *attr;
10223
10224 /* If the die does not have a name, this is not a namespace
10225 alias. */
10226 attr = dwarf2_attr (die, DW_AT_name, cu);
10227 if (attr != NULL)
10228 {
10229 int num;
10230 struct die_info *d = die;
10231 struct dwarf2_cu *imported_cu = cu;
10232
10233 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10234 keep inspecting DIEs until we hit the underlying import. */
10235 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10236 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10237 {
10238 attr = dwarf2_attr (d, DW_AT_import, cu);
10239 if (attr == NULL)
10240 break;
10241
10242 d = follow_die_ref (d, attr, &imported_cu);
10243 if (d->tag != DW_TAG_imported_declaration)
10244 break;
10245 }
10246
10247 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10248 {
10249 complaint (_("DIE at %s has too many recursively imported "
10250 "declarations"), sect_offset_str (d->sect_off));
10251 return 0;
10252 }
10253
10254 if (attr != NULL)
10255 {
10256 struct type *type;
10257 sect_offset sect_off = attr->get_ref_die_offset ();
10258
10259 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10260 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10261 {
10262 /* This declaration is a global namespace alias. Add
10263 a symbol for it whose type is the aliased namespace. */
10264 new_symbol (die, type, cu);
10265 return 1;
10266 }
10267 }
10268 }
10269
10270 return 0;
10271 }
10272
10273 /* Return the using directives repository (global or local?) to use in the
10274 current context for CU.
10275
10276 For Ada, imported declarations can materialize renamings, which *may* be
10277 global. However it is impossible (for now?) in DWARF to distinguish
10278 "external" imported declarations and "static" ones. As all imported
10279 declarations seem to be static in all other languages, make them all CU-wide
10280 global only in Ada. */
10281
10282 static struct using_direct **
10283 using_directives (struct dwarf2_cu *cu)
10284 {
10285 if (cu->per_cu->lang == language_ada
10286 && cu->get_builder ()->outermost_context_p ())
10287 return cu->get_builder ()->get_global_using_directives ();
10288 else
10289 return cu->get_builder ()->get_local_using_directives ();
10290 }
10291
10292 /* Read the import statement specified by the given die and record it. */
10293
10294 static void
10295 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10296 {
10297 struct objfile *objfile = cu->per_objfile->objfile;
10298 struct attribute *import_attr;
10299 struct die_info *imported_die, *child_die;
10300 struct dwarf2_cu *imported_cu;
10301 const char *imported_name;
10302 const char *imported_name_prefix;
10303 const char *canonical_name;
10304 const char *import_alias;
10305 const char *imported_declaration = NULL;
10306 const char *import_prefix;
10307 std::vector<const char *> excludes;
10308
10309 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10310 if (import_attr == NULL)
10311 {
10312 complaint (_("Tag '%s' has no DW_AT_import"),
10313 dwarf_tag_name (die->tag));
10314 return;
10315 }
10316
10317 imported_cu = cu;
10318 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10319 imported_name = dwarf2_name (imported_die, imported_cu);
10320 if (imported_name == NULL)
10321 {
10322 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10323
10324 The import in the following code:
10325 namespace A
10326 {
10327 typedef int B;
10328 }
10329
10330 int main ()
10331 {
10332 using A::B;
10333 B b;
10334 return b;
10335 }
10336
10337 ...
10338 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10339 <52> DW_AT_decl_file : 1
10340 <53> DW_AT_decl_line : 6
10341 <54> DW_AT_import : <0x75>
10342 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10343 <59> DW_AT_name : B
10344 <5b> DW_AT_decl_file : 1
10345 <5c> DW_AT_decl_line : 2
10346 <5d> DW_AT_type : <0x6e>
10347 ...
10348 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10349 <76> DW_AT_byte_size : 4
10350 <77> DW_AT_encoding : 5 (signed)
10351
10352 imports the wrong die ( 0x75 instead of 0x58 ).
10353 This case will be ignored until the gcc bug is fixed. */
10354 return;
10355 }
10356
10357 /* Figure out the local name after import. */
10358 import_alias = dwarf2_name (die, cu);
10359
10360 /* Figure out where the statement is being imported to. */
10361 import_prefix = determine_prefix (die, cu);
10362
10363 /* Figure out what the scope of the imported die is and prepend it
10364 to the name of the imported die. */
10365 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10366
10367 if (imported_die->tag != DW_TAG_namespace
10368 && imported_die->tag != DW_TAG_module)
10369 {
10370 imported_declaration = imported_name;
10371 canonical_name = imported_name_prefix;
10372 }
10373 else if (strlen (imported_name_prefix) > 0)
10374 canonical_name = obconcat (&objfile->objfile_obstack,
10375 imported_name_prefix,
10376 (cu->per_cu->lang == language_d
10377 ? "."
10378 : "::"),
10379 imported_name, (char *) NULL);
10380 else
10381 canonical_name = imported_name;
10382
10383 if (die->tag == DW_TAG_imported_module
10384 && cu->per_cu->lang == language_fortran)
10385 for (child_die = die->child; child_die && child_die->tag;
10386 child_die = child_die->sibling)
10387 {
10388 /* DWARF-4: A Fortran use statement with a “rename list” may be
10389 represented by an imported module entry with an import attribute
10390 referring to the module and owned entries corresponding to those
10391 entities that are renamed as part of being imported. */
10392
10393 if (child_die->tag != DW_TAG_imported_declaration)
10394 {
10395 complaint (_("child DW_TAG_imported_declaration expected "
10396 "- DIE at %s [in module %s]"),
10397 sect_offset_str (child_die->sect_off),
10398 objfile_name (objfile));
10399 continue;
10400 }
10401
10402 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10403 if (import_attr == NULL)
10404 {
10405 complaint (_("Tag '%s' has no DW_AT_import"),
10406 dwarf_tag_name (child_die->tag));
10407 continue;
10408 }
10409
10410 imported_cu = cu;
10411 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10412 &imported_cu);
10413 imported_name = dwarf2_name (imported_die, imported_cu);
10414 if (imported_name == NULL)
10415 {
10416 complaint (_("child DW_TAG_imported_declaration has unknown "
10417 "imported name - DIE at %s [in module %s]"),
10418 sect_offset_str (child_die->sect_off),
10419 objfile_name (objfile));
10420 continue;
10421 }
10422
10423 excludes.push_back (imported_name);
10424
10425 process_die (child_die, cu);
10426 }
10427
10428 add_using_directive (using_directives (cu),
10429 import_prefix,
10430 canonical_name,
10431 import_alias,
10432 imported_declaration,
10433 excludes,
10434 0,
10435 &objfile->objfile_obstack);
10436 }
10437
10438 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10439 types, but gives them a size of zero. Starting with version 14,
10440 ICC is compatible with GCC. */
10441
10442 static bool
10443 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10444 {
10445 if (!cu->checked_producer)
10446 check_producer (cu);
10447
10448 return cu->producer_is_icc_lt_14;
10449 }
10450
10451 /* ICC generates a DW_AT_type for C void functions. This was observed on
10452 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10453 which says that void functions should not have a DW_AT_type. */
10454
10455 static bool
10456 producer_is_icc (struct dwarf2_cu *cu)
10457 {
10458 if (!cu->checked_producer)
10459 check_producer (cu);
10460
10461 return cu->producer_is_icc;
10462 }
10463
10464 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10465 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10466 this, it was first present in GCC release 4.3.0. */
10467
10468 static bool
10469 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10470 {
10471 if (!cu->checked_producer)
10472 check_producer (cu);
10473
10474 return cu->producer_is_gcc_lt_4_3;
10475 }
10476
10477 static file_and_directory &
10478 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10479 {
10480 if (cu->per_cu->fnd != nullptr)
10481 return *cu->per_cu->fnd;
10482
10483 /* Find the filename. Do not use dwarf2_name here, since the filename
10484 is not a source language identifier. */
10485 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
10486 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
10487
10488 if (res.get_comp_dir () == nullptr
10489 && producer_is_gcc_lt_4_3 (cu)
10490 && res.get_name () != nullptr
10491 && IS_ABSOLUTE_PATH (res.get_name ()))
10492 res.set_comp_dir (ldirname (res.get_name ()));
10493
10494 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
10495 return *cu->per_cu->fnd;
10496 }
10497
10498 /* Handle DW_AT_stmt_list for a compilation unit.
10499 DIE is the DW_TAG_compile_unit die for CU.
10500 COMP_DIR is the compilation directory. LOWPC is passed to
10501 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10502
10503 static void
10504 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10505 const file_and_directory &fnd, CORE_ADDR lowpc) /* ARI: editCase function */
10506 {
10507 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10508 struct attribute *attr;
10509 struct line_header line_header_local;
10510 hashval_t line_header_local_hash;
10511 void **slot;
10512 int decode_mapping;
10513
10514 gdb_assert (! cu->per_cu->is_debug_types);
10515
10516 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10517 if (attr == NULL || !attr->form_is_unsigned ())
10518 return;
10519
10520 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10521
10522 /* The line header hash table is only created if needed (it exists to
10523 prevent redundant reading of the line table for partial_units).
10524 If we're given a partial_unit, we'll need it. If we're given a
10525 compile_unit, then use the line header hash table if it's already
10526 created, but don't create one just yet. */
10527
10528 if (per_objfile->line_header_hash == NULL
10529 && die->tag == DW_TAG_partial_unit)
10530 {
10531 per_objfile->line_header_hash
10532 .reset (htab_create_alloc (127, line_header_hash_voidp,
10533 line_header_eq_voidp,
10534 htab_delete_entry<line_header>,
10535 xcalloc, xfree));
10536 }
10537
10538 line_header_local.sect_off = line_offset;
10539 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10540 line_header_local_hash = line_header_hash (&line_header_local);
10541 if (per_objfile->line_header_hash != NULL)
10542 {
10543 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10544 &line_header_local,
10545 line_header_local_hash, NO_INSERT);
10546
10547 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10548 is not present in *SLOT (since if there is something in *SLOT then
10549 it will be for a partial_unit). */
10550 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10551 {
10552 gdb_assert (*slot != NULL);
10553 cu->line_header = (struct line_header *) *slot;
10554 return;
10555 }
10556 }
10557
10558 /* dwarf_decode_line_header does not yet provide sufficient information.
10559 We always have to call also dwarf_decode_lines for it. */
10560 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10561 if (lh == NULL)
10562 return;
10563
10564 cu->line_header = lh.release ();
10565 cu->line_header_die_owner = die;
10566
10567 if (per_objfile->line_header_hash == NULL)
10568 slot = NULL;
10569 else
10570 {
10571 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10572 &line_header_local,
10573 line_header_local_hash, INSERT);
10574 gdb_assert (slot != NULL);
10575 }
10576 if (slot != NULL && *slot == NULL)
10577 {
10578 /* This newly decoded line number information unit will be owned
10579 by line_header_hash hash table. */
10580 *slot = cu->line_header;
10581 cu->line_header_die_owner = NULL;
10582 }
10583 else
10584 {
10585 /* We cannot free any current entry in (*slot) as that struct line_header
10586 may be already used by multiple CUs. Create only temporary decoded
10587 line_header for this CU - it may happen at most once for each line
10588 number information unit. And if we're not using line_header_hash
10589 then this is what we want as well. */
10590 gdb_assert (die->tag != DW_TAG_partial_unit);
10591 }
10592 decode_mapping = (die->tag != DW_TAG_partial_unit);
10593 dwarf_decode_lines (cu->line_header, fnd, cu, nullptr, lowpc,
10594 decode_mapping);
10595
10596 }
10597
10598 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10599
10600 static void
10601 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10602 {
10603 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10604 struct objfile *objfile = per_objfile->objfile;
10605 struct gdbarch *gdbarch = objfile->arch ();
10606 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10607 CORE_ADDR highpc = ((CORE_ADDR) 0);
10608 struct attribute *attr;
10609 struct die_info *child_die;
10610 CORE_ADDR baseaddr;
10611
10612 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
10613 baseaddr = objfile->text_section_offset ();
10614
10615 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10616
10617 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10618 from finish_block. */
10619 if (lowpc == ((CORE_ADDR) -1))
10620 lowpc = highpc;
10621 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10622
10623 file_and_directory &fnd = find_file_and_directory (die, cu);
10624
10625 cu->start_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile), lowpc);
10626
10627 gdb_assert (per_objfile->sym_cu == nullptr);
10628 scoped_restore restore_sym_cu
10629 = make_scoped_restore (&per_objfile->sym_cu, cu);
10630
10631 /* Decode line number information if present. We do this before
10632 processing child DIEs, so that the line header table is available
10633 for DW_AT_decl_file. */
10634 handle_DW_AT_stmt_list (die, cu, fnd, lowpc);
10635
10636 /* Process all dies in compilation unit. */
10637 if (die->child != NULL)
10638 {
10639 child_die = die->child;
10640 while (child_die && child_die->tag)
10641 {
10642 process_die (child_die, cu);
10643 child_die = child_die->sibling;
10644 }
10645 }
10646 per_objfile->sym_cu = nullptr;
10647
10648 /* Decode macro information, if present. Dwarf 2 macro information
10649 refers to information in the line number info statement program
10650 header, so we can only read it if we've read the header
10651 successfully. */
10652 attr = dwarf2_attr (die, DW_AT_macros, cu);
10653 if (attr == NULL)
10654 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10655 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10656 {
10657 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10658 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10659
10660 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10661 }
10662 else
10663 {
10664 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10665 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10666 {
10667 unsigned int macro_offset = attr->as_unsigned ();
10668
10669 dwarf_decode_macros (cu, macro_offset, 0);
10670 }
10671 }
10672 }
10673
10674 void
10675 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10676 {
10677 struct type_unit_group *tu_group;
10678 int first_time;
10679 struct attribute *attr;
10680 unsigned int i;
10681 struct signatured_type *sig_type;
10682
10683 gdb_assert (per_cu->is_debug_types);
10684 sig_type = (struct signatured_type *) per_cu;
10685
10686 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10687
10688 /* If we're using .gdb_index (includes -readnow) then
10689 per_cu->type_unit_group may not have been set up yet. */
10690 if (sig_type->type_unit_group == NULL)
10691 sig_type->type_unit_group = get_type_unit_group (this, attr);
10692 tu_group = sig_type->type_unit_group;
10693
10694 /* If we've already processed this stmt_list there's no real need to
10695 do it again, we could fake it and just recreate the part we need
10696 (file name,index -> symtab mapping). If data shows this optimization
10697 is useful we can do it then. */
10698 type_unit_group_unshareable *tug_unshare
10699 = per_objfile->get_type_unit_group_unshareable (tu_group);
10700 first_time = tug_unshare->compunit_symtab == NULL;
10701
10702 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10703 debug info. */
10704 line_header_up lh;
10705 if (attr != NULL && attr->form_is_unsigned ())
10706 {
10707 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10708 lh = dwarf_decode_line_header (line_offset, this);
10709 }
10710 if (lh == NULL)
10711 {
10712 if (first_time)
10713 start_symtab ("", NULL, 0);
10714 else
10715 {
10716 gdb_assert (tug_unshare->symtabs == NULL);
10717 gdb_assert (m_builder == nullptr);
10718 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10719 m_builder.reset (new struct buildsym_compunit
10720 (COMPUNIT_OBJFILE (cust), "",
10721 COMPUNIT_DIRNAME (cust),
10722 compunit_language (cust),
10723 0, cust));
10724 list_in_scope = get_builder ()->get_file_symbols ();
10725 }
10726 return;
10727 }
10728
10729 line_header = lh.release ();
10730 line_header_die_owner = die;
10731
10732 if (first_time)
10733 {
10734 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10735
10736 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10737 still initializing it, and our caller (a few levels up)
10738 process_full_type_unit still needs to know if this is the first
10739 time. */
10740
10741 tug_unshare->symtabs
10742 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10743 struct symtab *, line_header->file_names_size ());
10744
10745 auto &file_names = line_header->file_names ();
10746 for (i = 0; i < file_names.size (); ++i)
10747 {
10748 file_entry &fe = file_names[i];
10749 dwarf2_start_subfile (this, fe.name,
10750 fe.include_dir (line_header));
10751 buildsym_compunit *b = get_builder ();
10752 if (b->get_current_subfile ()->symtab == NULL)
10753 {
10754 /* NOTE: start_subfile will recognize when it's been
10755 passed a file it has already seen. So we can't
10756 assume there's a simple mapping from
10757 cu->line_header->file_names to subfiles, plus
10758 cu->line_header->file_names may contain dups. */
10759 b->get_current_subfile ()->symtab
10760 = allocate_symtab (cust, b->get_current_subfile ()->name);
10761 }
10762
10763 fe.symtab = b->get_current_subfile ()->symtab;
10764 tug_unshare->symtabs[i] = fe.symtab;
10765 }
10766 }
10767 else
10768 {
10769 gdb_assert (m_builder == nullptr);
10770 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10771 m_builder.reset (new struct buildsym_compunit
10772 (COMPUNIT_OBJFILE (cust), "",
10773 COMPUNIT_DIRNAME (cust),
10774 compunit_language (cust),
10775 0, cust));
10776 list_in_scope = get_builder ()->get_file_symbols ();
10777
10778 auto &file_names = line_header->file_names ();
10779 for (i = 0; i < file_names.size (); ++i)
10780 {
10781 file_entry &fe = file_names[i];
10782 fe.symtab = tug_unshare->symtabs[i];
10783 }
10784 }
10785
10786 /* The main symtab is allocated last. Type units don't have DW_AT_name
10787 so they don't have a "real" (so to speak) symtab anyway.
10788 There is later code that will assign the main symtab to all symbols
10789 that don't have one. We need to handle the case of a symbol with a
10790 missing symtab (DW_AT_decl_file) anyway. */
10791 }
10792
10793 /* Process DW_TAG_type_unit.
10794 For TUs we want to skip the first top level sibling if it's not the
10795 actual type being defined by this TU. In this case the first top
10796 level sibling is there to provide context only. */
10797
10798 static void
10799 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10800 {
10801 struct die_info *child_die;
10802
10803 prepare_one_comp_unit (cu, die, language_minimal);
10804
10805 /* Initialize (or reinitialize) the machinery for building symtabs.
10806 We do this before processing child DIEs, so that the line header table
10807 is available for DW_AT_decl_file. */
10808 cu->setup_type_unit_groups (die);
10809
10810 if (die->child != NULL)
10811 {
10812 child_die = die->child;
10813 while (child_die && child_die->tag)
10814 {
10815 process_die (child_die, cu);
10816 child_die = child_die->sibling;
10817 }
10818 }
10819 }
10820 \f
10821 /* DWO/DWP files.
10822
10823 http://gcc.gnu.org/wiki/DebugFission
10824 http://gcc.gnu.org/wiki/DebugFissionDWP
10825
10826 To simplify handling of both DWO files ("object" files with the DWARF info)
10827 and DWP files (a file with the DWOs packaged up into one file), we treat
10828 DWP files as having a collection of virtual DWO files. */
10829
10830 static hashval_t
10831 hash_dwo_file (const void *item)
10832 {
10833 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10834 hashval_t hash;
10835
10836 hash = htab_hash_string (dwo_file->dwo_name);
10837 if (dwo_file->comp_dir != NULL)
10838 hash += htab_hash_string (dwo_file->comp_dir);
10839 return hash;
10840 }
10841
10842 static int
10843 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10844 {
10845 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10846 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10847
10848 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10849 return 0;
10850 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10851 return lhs->comp_dir == rhs->comp_dir;
10852 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10853 }
10854
10855 /* Allocate a hash table for DWO files. */
10856
10857 static htab_up
10858 allocate_dwo_file_hash_table ()
10859 {
10860 return htab_up (htab_create_alloc (41,
10861 hash_dwo_file,
10862 eq_dwo_file,
10863 htab_delete_entry<dwo_file>,
10864 xcalloc, xfree));
10865 }
10866
10867 /* Lookup DWO file DWO_NAME. */
10868
10869 static void **
10870 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
10871 const char *dwo_name,
10872 const char *comp_dir)
10873 {
10874 struct dwo_file find_entry;
10875 void **slot;
10876
10877 if (per_objfile->per_bfd->dwo_files == NULL)
10878 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
10879
10880 find_entry.dwo_name = dwo_name;
10881 find_entry.comp_dir = comp_dir;
10882 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
10883 INSERT);
10884
10885 return slot;
10886 }
10887
10888 static hashval_t
10889 hash_dwo_unit (const void *item)
10890 {
10891 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10892
10893 /* This drops the top 32 bits of the id, but is ok for a hash. */
10894 return dwo_unit->signature;
10895 }
10896
10897 static int
10898 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10899 {
10900 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10901 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10902
10903 /* The signature is assumed to be unique within the DWO file.
10904 So while object file CU dwo_id's always have the value zero,
10905 that's OK, assuming each object file DWO file has only one CU,
10906 and that's the rule for now. */
10907 return lhs->signature == rhs->signature;
10908 }
10909
10910 /* Allocate a hash table for DWO CUs,TUs.
10911 There is one of these tables for each of CUs,TUs for each DWO file. */
10912
10913 static htab_up
10914 allocate_dwo_unit_table ()
10915 {
10916 /* Start out with a pretty small number.
10917 Generally DWO files contain only one CU and maybe some TUs. */
10918 return htab_up (htab_create_alloc (3,
10919 hash_dwo_unit,
10920 eq_dwo_unit,
10921 NULL, xcalloc, xfree));
10922 }
10923
10924 /* die_reader_func for create_dwo_cu. */
10925
10926 static void
10927 create_dwo_cu_reader (const struct die_reader_specs *reader,
10928 const gdb_byte *info_ptr,
10929 struct die_info *comp_unit_die,
10930 struct dwo_file *dwo_file,
10931 struct dwo_unit *dwo_unit)
10932 {
10933 struct dwarf2_cu *cu = reader->cu;
10934 sect_offset sect_off = cu->per_cu->sect_off;
10935 struct dwarf2_section_info *section = cu->per_cu->section;
10936
10937 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10938 if (!signature.has_value ())
10939 {
10940 complaint (_("Dwarf Error: debug entry at offset %s is missing"
10941 " its dwo_id [in module %s]"),
10942 sect_offset_str (sect_off), dwo_file->dwo_name);
10943 return;
10944 }
10945
10946 dwo_unit->dwo_file = dwo_file;
10947 dwo_unit->signature = *signature;
10948 dwo_unit->section = section;
10949 dwo_unit->sect_off = sect_off;
10950 dwo_unit->length = cu->per_cu->length;
10951
10952 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10953 sect_offset_str (sect_off),
10954 hex_string (dwo_unit->signature));
10955 }
10956
10957 /* Create the dwo_units for the CUs in a DWO_FILE.
10958 Note: This function processes DWO files only, not DWP files. */
10959
10960 static void
10961 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10962 dwarf2_cu *cu, struct dwo_file &dwo_file,
10963 dwarf2_section_info &section, htab_up &cus_htab)
10964 {
10965 struct objfile *objfile = per_objfile->objfile;
10966 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10967 const gdb_byte *info_ptr, *end_ptr;
10968
10969 section.read (objfile);
10970 info_ptr = section.buffer;
10971
10972 if (info_ptr == NULL)
10973 return;
10974
10975 dwarf_read_debug_printf ("Reading %s for %s:",
10976 section.get_name (),
10977 section.get_file_name ());
10978
10979 end_ptr = info_ptr + section.size;
10980 while (info_ptr < end_ptr)
10981 {
10982 struct dwarf2_per_cu_data per_cu;
10983 struct dwo_unit read_unit {};
10984 struct dwo_unit *dwo_unit;
10985 void **slot;
10986 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10987
10988 per_cu.per_bfd = per_bfd;
10989 per_cu.is_debug_types = 0;
10990 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10991 per_cu.section = &section;
10992
10993 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10994 if (!reader.dummy_p)
10995 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
10996 &dwo_file, &read_unit);
10997 info_ptr += per_cu.length;
10998
10999 // If the unit could not be parsed, skip it.
11000 if (read_unit.dwo_file == NULL)
11001 continue;
11002
11003 if (cus_htab == NULL)
11004 cus_htab = allocate_dwo_unit_table ();
11005
11006 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11007 struct dwo_unit);
11008 *dwo_unit = read_unit;
11009 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11010 gdb_assert (slot != NULL);
11011 if (*slot != NULL)
11012 {
11013 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11014 sect_offset dup_sect_off = dup_cu->sect_off;
11015
11016 complaint (_("debug cu entry at offset %s is duplicate to"
11017 " the entry at offset %s, signature %s"),
11018 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11019 hex_string (dwo_unit->signature));
11020 }
11021 *slot = (void *)dwo_unit;
11022 }
11023 }
11024
11025 /* DWP file .debug_{cu,tu}_index section format:
11026 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11027 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11028
11029 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11030 officially standard DWP format was published with DWARF v5 and is called
11031 Version 5. There are no versions 3 or 4.
11032
11033 DWP Version 1:
11034
11035 Both index sections have the same format, and serve to map a 64-bit
11036 signature to a set of section numbers. Each section begins with a header,
11037 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11038 indexes, and a pool of 32-bit section numbers. The index sections will be
11039 aligned at 8-byte boundaries in the file.
11040
11041 The index section header consists of:
11042
11043 V, 32 bit version number
11044 -, 32 bits unused
11045 N, 32 bit number of compilation units or type units in the index
11046 M, 32 bit number of slots in the hash table
11047
11048 Numbers are recorded using the byte order of the application binary.
11049
11050 The hash table begins at offset 16 in the section, and consists of an array
11051 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11052 order of the application binary). Unused slots in the hash table are 0.
11053 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11054
11055 The parallel table begins immediately after the hash table
11056 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11057 array of 32-bit indexes (using the byte order of the application binary),
11058 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11059 table contains a 32-bit index into the pool of section numbers. For unused
11060 hash table slots, the corresponding entry in the parallel table will be 0.
11061
11062 The pool of section numbers begins immediately following the hash table
11063 (at offset 16 + 12 * M from the beginning of the section). The pool of
11064 section numbers consists of an array of 32-bit words (using the byte order
11065 of the application binary). Each item in the array is indexed starting
11066 from 0. The hash table entry provides the index of the first section
11067 number in the set. Additional section numbers in the set follow, and the
11068 set is terminated by a 0 entry (section number 0 is not used in ELF).
11069
11070 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11071 section must be the first entry in the set, and the .debug_abbrev.dwo must
11072 be the second entry. Other members of the set may follow in any order.
11073
11074 ---
11075
11076 DWP Versions 2 and 5:
11077
11078 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11079 and the entries in the index tables are now offsets into these sections.
11080 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11081 section.
11082
11083 Index Section Contents:
11084 Header
11085 Hash Table of Signatures dwp_hash_table.hash_table
11086 Parallel Table of Indices dwp_hash_table.unit_table
11087 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11088 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11089
11090 The index section header consists of:
11091
11092 V, 32 bit version number
11093 L, 32 bit number of columns in the table of section offsets
11094 N, 32 bit number of compilation units or type units in the index
11095 M, 32 bit number of slots in the hash table
11096
11097 Numbers are recorded using the byte order of the application binary.
11098
11099 The hash table has the same format as version 1.
11100 The parallel table of indices has the same format as version 1,
11101 except that the entries are origin-1 indices into the table of sections
11102 offsets and the table of section sizes.
11103
11104 The table of offsets begins immediately following the parallel table
11105 (at offset 16 + 12 * M from the beginning of the section). The table is
11106 a two-dimensional array of 32-bit words (using the byte order of the
11107 application binary), with L columns and N+1 rows, in row-major order.
11108 Each row in the array is indexed starting from 0. The first row provides
11109 a key to the remaining rows: each column in this row provides an identifier
11110 for a debug section, and the offsets in the same column of subsequent rows
11111 refer to that section. The section identifiers for Version 2 are:
11112
11113 DW_SECT_INFO 1 .debug_info.dwo
11114 DW_SECT_TYPES 2 .debug_types.dwo
11115 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11116 DW_SECT_LINE 4 .debug_line.dwo
11117 DW_SECT_LOC 5 .debug_loc.dwo
11118 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11119 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11120 DW_SECT_MACRO 8 .debug_macro.dwo
11121
11122 The section identifiers for Version 5 are:
11123
11124 DW_SECT_INFO_V5 1 .debug_info.dwo
11125 DW_SECT_RESERVED_V5 2 --
11126 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11127 DW_SECT_LINE_V5 4 .debug_line.dwo
11128 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11129 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11130 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11131 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11132
11133 The offsets provided by the CU and TU index sections are the base offsets
11134 for the contributions made by each CU or TU to the corresponding section
11135 in the package file. Each CU and TU header contains an abbrev_offset
11136 field, used to find the abbreviations table for that CU or TU within the
11137 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11138 be interpreted as relative to the base offset given in the index section.
11139 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11140 should be interpreted as relative to the base offset for .debug_line.dwo,
11141 and offsets into other debug sections obtained from DWARF attributes should
11142 also be interpreted as relative to the corresponding base offset.
11143
11144 The table of sizes begins immediately following the table of offsets.
11145 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11146 with L columns and N rows, in row-major order. Each row in the array is
11147 indexed starting from 1 (row 0 is shared by the two tables).
11148
11149 ---
11150
11151 Hash table lookup is handled the same in version 1 and 2:
11152
11153 We assume that N and M will not exceed 2^32 - 1.
11154 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11155
11156 Given a 64-bit compilation unit signature or a type signature S, an entry
11157 in the hash table is located as follows:
11158
11159 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11160 the low-order k bits all set to 1.
11161
11162 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11163
11164 3) If the hash table entry at index H matches the signature, use that
11165 entry. If the hash table entry at index H is unused (all zeroes),
11166 terminate the search: the signature is not present in the table.
11167
11168 4) Let H = (H + H') modulo M. Repeat at Step 3.
11169
11170 Because M > N and H' and M are relatively prime, the search is guaranteed
11171 to stop at an unused slot or find the match. */
11172
11173 /* Create a hash table to map DWO IDs to their CU/TU entry in
11174 .debug_{info,types}.dwo in DWP_FILE.
11175 Returns NULL if there isn't one.
11176 Note: This function processes DWP files only, not DWO files. */
11177
11178 static struct dwp_hash_table *
11179 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11180 struct dwp_file *dwp_file, int is_debug_types)
11181 {
11182 struct objfile *objfile = per_objfile->objfile;
11183 bfd *dbfd = dwp_file->dbfd.get ();
11184 const gdb_byte *index_ptr, *index_end;
11185 struct dwarf2_section_info *index;
11186 uint32_t version, nr_columns, nr_units, nr_slots;
11187 struct dwp_hash_table *htab;
11188
11189 if (is_debug_types)
11190 index = &dwp_file->sections.tu_index;
11191 else
11192 index = &dwp_file->sections.cu_index;
11193
11194 if (index->empty ())
11195 return NULL;
11196 index->read (objfile);
11197
11198 index_ptr = index->buffer;
11199 index_end = index_ptr + index->size;
11200
11201 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11202 For now it's safe to just read 4 bytes (particularly as it's difficult to
11203 tell if you're dealing with Version 5 before you've read the version). */
11204 version = read_4_bytes (dbfd, index_ptr);
11205 index_ptr += 4;
11206 if (version == 2 || version == 5)
11207 nr_columns = read_4_bytes (dbfd, index_ptr);
11208 else
11209 nr_columns = 0;
11210 index_ptr += 4;
11211 nr_units = read_4_bytes (dbfd, index_ptr);
11212 index_ptr += 4;
11213 nr_slots = read_4_bytes (dbfd, index_ptr);
11214 index_ptr += 4;
11215
11216 if (version != 1 && version != 2 && version != 5)
11217 {
11218 error (_("Dwarf Error: unsupported DWP file version (%s)"
11219 " [in module %s]"),
11220 pulongest (version), dwp_file->name);
11221 }
11222 if (nr_slots != (nr_slots & -nr_slots))
11223 {
11224 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11225 " is not power of 2 [in module %s]"),
11226 pulongest (nr_slots), dwp_file->name);
11227 }
11228
11229 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11230 htab->version = version;
11231 htab->nr_columns = nr_columns;
11232 htab->nr_units = nr_units;
11233 htab->nr_slots = nr_slots;
11234 htab->hash_table = index_ptr;
11235 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11236
11237 /* Exit early if the table is empty. */
11238 if (nr_slots == 0 || nr_units == 0
11239 || (version == 2 && nr_columns == 0)
11240 || (version == 5 && nr_columns == 0))
11241 {
11242 /* All must be zero. */
11243 if (nr_slots != 0 || nr_units != 0
11244 || (version == 2 && nr_columns != 0)
11245 || (version == 5 && nr_columns != 0))
11246 {
11247 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11248 " all zero [in modules %s]"),
11249 dwp_file->name);
11250 }
11251 return htab;
11252 }
11253
11254 if (version == 1)
11255 {
11256 htab->section_pool.v1.indices =
11257 htab->unit_table + sizeof (uint32_t) * nr_slots;
11258 /* It's harder to decide whether the section is too small in v1.
11259 V1 is deprecated anyway so we punt. */
11260 }
11261 else if (version == 2)
11262 {
11263 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11264 int *ids = htab->section_pool.v2.section_ids;
11265 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11266 /* Reverse map for error checking. */
11267 int ids_seen[DW_SECT_MAX + 1];
11268 int i;
11269
11270 if (nr_columns < 2)
11271 {
11272 error (_("Dwarf Error: bad DWP hash table, too few columns"
11273 " in section table [in module %s]"),
11274 dwp_file->name);
11275 }
11276 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11277 {
11278 error (_("Dwarf Error: bad DWP hash table, too many columns"
11279 " in section table [in module %s]"),
11280 dwp_file->name);
11281 }
11282 memset (ids, 255, sizeof_ids);
11283 memset (ids_seen, 255, sizeof (ids_seen));
11284 for (i = 0; i < nr_columns; ++i)
11285 {
11286 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11287
11288 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11289 {
11290 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11291 " in section table [in module %s]"),
11292 id, dwp_file->name);
11293 }
11294 if (ids_seen[id] != -1)
11295 {
11296 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11297 " id %d in section table [in module %s]"),
11298 id, dwp_file->name);
11299 }
11300 ids_seen[id] = i;
11301 ids[i] = id;
11302 }
11303 /* Must have exactly one info or types section. */
11304 if (((ids_seen[DW_SECT_INFO] != -1)
11305 + (ids_seen[DW_SECT_TYPES] != -1))
11306 != 1)
11307 {
11308 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11309 " DWO info/types section [in module %s]"),
11310 dwp_file->name);
11311 }
11312 /* Must have an abbrev section. */
11313 if (ids_seen[DW_SECT_ABBREV] == -1)
11314 {
11315 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11316 " section [in module %s]"),
11317 dwp_file->name);
11318 }
11319 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11320 htab->section_pool.v2.sizes =
11321 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11322 * nr_units * nr_columns);
11323 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11324 * nr_units * nr_columns))
11325 > index_end)
11326 {
11327 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11328 " [in module %s]"),
11329 dwp_file->name);
11330 }
11331 }
11332 else /* version == 5 */
11333 {
11334 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11335 int *ids = htab->section_pool.v5.section_ids;
11336 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11337 /* Reverse map for error checking. */
11338 int ids_seen[DW_SECT_MAX_V5 + 1];
11339
11340 if (nr_columns < 2)
11341 {
11342 error (_("Dwarf Error: bad DWP hash table, too few columns"
11343 " in section table [in module %s]"),
11344 dwp_file->name);
11345 }
11346 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11347 {
11348 error (_("Dwarf Error: bad DWP hash table, too many columns"
11349 " in section table [in module %s]"),
11350 dwp_file->name);
11351 }
11352 memset (ids, 255, sizeof_ids);
11353 memset (ids_seen, 255, sizeof (ids_seen));
11354 for (int i = 0; i < nr_columns; ++i)
11355 {
11356 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11357
11358 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11359 {
11360 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11361 " in section table [in module %s]"),
11362 id, dwp_file->name);
11363 }
11364 if (ids_seen[id] != -1)
11365 {
11366 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11367 " id %d in section table [in module %s]"),
11368 id, dwp_file->name);
11369 }
11370 ids_seen[id] = i;
11371 ids[i] = id;
11372 }
11373 /* Must have seen an info section. */
11374 if (ids_seen[DW_SECT_INFO_V5] == -1)
11375 {
11376 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11377 " DWO info/types section [in module %s]"),
11378 dwp_file->name);
11379 }
11380 /* Must have an abbrev section. */
11381 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11382 {
11383 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11384 " section [in module %s]"),
11385 dwp_file->name);
11386 }
11387 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11388 htab->section_pool.v5.sizes
11389 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11390 * nr_units * nr_columns);
11391 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11392 * nr_units * nr_columns))
11393 > index_end)
11394 {
11395 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11396 " [in module %s]"),
11397 dwp_file->name);
11398 }
11399 }
11400
11401 return htab;
11402 }
11403
11404 /* Update SECTIONS with the data from SECTP.
11405
11406 This function is like the other "locate" section routines, but in
11407 this context the sections to read comes from the DWP V1 hash table,
11408 not the full ELF section table.
11409
11410 The result is non-zero for success, or zero if an error was found. */
11411
11412 static int
11413 locate_v1_virtual_dwo_sections (asection *sectp,
11414 struct virtual_v1_dwo_sections *sections)
11415 {
11416 const struct dwop_section_names *names = &dwop_section_names;
11417
11418 if (names->abbrev_dwo.matches (sectp->name))
11419 {
11420 /* There can be only one. */
11421 if (sections->abbrev.s.section != NULL)
11422 return 0;
11423 sections->abbrev.s.section = sectp;
11424 sections->abbrev.size = bfd_section_size (sectp);
11425 }
11426 else if (names->info_dwo.matches (sectp->name)
11427 || names->types_dwo.matches (sectp->name))
11428 {
11429 /* There can be only one. */
11430 if (sections->info_or_types.s.section != NULL)
11431 return 0;
11432 sections->info_or_types.s.section = sectp;
11433 sections->info_or_types.size = bfd_section_size (sectp);
11434 }
11435 else if (names->line_dwo.matches (sectp->name))
11436 {
11437 /* There can be only one. */
11438 if (sections->line.s.section != NULL)
11439 return 0;
11440 sections->line.s.section = sectp;
11441 sections->line.size = bfd_section_size (sectp);
11442 }
11443 else if (names->loc_dwo.matches (sectp->name))
11444 {
11445 /* There can be only one. */
11446 if (sections->loc.s.section != NULL)
11447 return 0;
11448 sections->loc.s.section = sectp;
11449 sections->loc.size = bfd_section_size (sectp);
11450 }
11451 else if (names->macinfo_dwo.matches (sectp->name))
11452 {
11453 /* There can be only one. */
11454 if (sections->macinfo.s.section != NULL)
11455 return 0;
11456 sections->macinfo.s.section = sectp;
11457 sections->macinfo.size = bfd_section_size (sectp);
11458 }
11459 else if (names->macro_dwo.matches (sectp->name))
11460 {
11461 /* There can be only one. */
11462 if (sections->macro.s.section != NULL)
11463 return 0;
11464 sections->macro.s.section = sectp;
11465 sections->macro.size = bfd_section_size (sectp);
11466 }
11467 else if (names->str_offsets_dwo.matches (sectp->name))
11468 {
11469 /* There can be only one. */
11470 if (sections->str_offsets.s.section != NULL)
11471 return 0;
11472 sections->str_offsets.s.section = sectp;
11473 sections->str_offsets.size = bfd_section_size (sectp);
11474 }
11475 else
11476 {
11477 /* No other kind of section is valid. */
11478 return 0;
11479 }
11480
11481 return 1;
11482 }
11483
11484 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11485 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11486 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11487 This is for DWP version 1 files. */
11488
11489 static struct dwo_unit *
11490 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11491 struct dwp_file *dwp_file,
11492 uint32_t unit_index,
11493 const char *comp_dir,
11494 ULONGEST signature, int is_debug_types)
11495 {
11496 const struct dwp_hash_table *dwp_htab =
11497 is_debug_types ? dwp_file->tus : dwp_file->cus;
11498 bfd *dbfd = dwp_file->dbfd.get ();
11499 const char *kind = is_debug_types ? "TU" : "CU";
11500 struct dwo_file *dwo_file;
11501 struct dwo_unit *dwo_unit;
11502 struct virtual_v1_dwo_sections sections;
11503 void **dwo_file_slot;
11504 int i;
11505
11506 gdb_assert (dwp_file->version == 1);
11507
11508 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11509 kind, pulongest (unit_index), hex_string (signature),
11510 dwp_file->name);
11511
11512 /* Fetch the sections of this DWO unit.
11513 Put a limit on the number of sections we look for so that bad data
11514 doesn't cause us to loop forever. */
11515
11516 #define MAX_NR_V1_DWO_SECTIONS \
11517 (1 /* .debug_info or .debug_types */ \
11518 + 1 /* .debug_abbrev */ \
11519 + 1 /* .debug_line */ \
11520 + 1 /* .debug_loc */ \
11521 + 1 /* .debug_str_offsets */ \
11522 + 1 /* .debug_macro or .debug_macinfo */ \
11523 + 1 /* trailing zero */)
11524
11525 memset (&sections, 0, sizeof (sections));
11526
11527 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11528 {
11529 asection *sectp;
11530 uint32_t section_nr =
11531 read_4_bytes (dbfd,
11532 dwp_htab->section_pool.v1.indices
11533 + (unit_index + i) * sizeof (uint32_t));
11534
11535 if (section_nr == 0)
11536 break;
11537 if (section_nr >= dwp_file->num_sections)
11538 {
11539 error (_("Dwarf Error: bad DWP hash table, section number too large"
11540 " [in module %s]"),
11541 dwp_file->name);
11542 }
11543
11544 sectp = dwp_file->elf_sections[section_nr];
11545 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11546 {
11547 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11548 " [in module %s]"),
11549 dwp_file->name);
11550 }
11551 }
11552
11553 if (i < 2
11554 || sections.info_or_types.empty ()
11555 || sections.abbrev.empty ())
11556 {
11557 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11558 " [in module %s]"),
11559 dwp_file->name);
11560 }
11561 if (i == MAX_NR_V1_DWO_SECTIONS)
11562 {
11563 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11564 " [in module %s]"),
11565 dwp_file->name);
11566 }
11567
11568 /* It's easier for the rest of the code if we fake a struct dwo_file and
11569 have dwo_unit "live" in that. At least for now.
11570
11571 The DWP file can be made up of a random collection of CUs and TUs.
11572 However, for each CU + set of TUs that came from the same original DWO
11573 file, we can combine them back into a virtual DWO file to save space
11574 (fewer struct dwo_file objects to allocate). Remember that for really
11575 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11576
11577 std::string virtual_dwo_name =
11578 string_printf ("virtual-dwo/%d-%d-%d-%d",
11579 sections.abbrev.get_id (),
11580 sections.line.get_id (),
11581 sections.loc.get_id (),
11582 sections.str_offsets.get_id ());
11583 /* Can we use an existing virtual DWO file? */
11584 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11585 comp_dir);
11586 /* Create one if necessary. */
11587 if (*dwo_file_slot == NULL)
11588 {
11589 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11590 virtual_dwo_name.c_str ());
11591
11592 dwo_file = new struct dwo_file;
11593 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11594 dwo_file->comp_dir = comp_dir;
11595 dwo_file->sections.abbrev = sections.abbrev;
11596 dwo_file->sections.line = sections.line;
11597 dwo_file->sections.loc = sections.loc;
11598 dwo_file->sections.macinfo = sections.macinfo;
11599 dwo_file->sections.macro = sections.macro;
11600 dwo_file->sections.str_offsets = sections.str_offsets;
11601 /* The "str" section is global to the entire DWP file. */
11602 dwo_file->sections.str = dwp_file->sections.str;
11603 /* The info or types section is assigned below to dwo_unit,
11604 there's no need to record it in dwo_file.
11605 Also, we can't simply record type sections in dwo_file because
11606 we record a pointer into the vector in dwo_unit. As we collect more
11607 types we'll grow the vector and eventually have to reallocate space
11608 for it, invalidating all copies of pointers into the previous
11609 contents. */
11610 *dwo_file_slot = dwo_file;
11611 }
11612 else
11613 {
11614 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11615 virtual_dwo_name.c_str ());
11616
11617 dwo_file = (struct dwo_file *) *dwo_file_slot;
11618 }
11619
11620 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11621 dwo_unit->dwo_file = dwo_file;
11622 dwo_unit->signature = signature;
11623 dwo_unit->section =
11624 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11625 *dwo_unit->section = sections.info_or_types;
11626 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11627
11628 return dwo_unit;
11629 }
11630
11631 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11632 simplify them. Given a pointer to the containing section SECTION, and
11633 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11634 virtual section of just that piece. */
11635
11636 static struct dwarf2_section_info
11637 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11638 struct dwarf2_section_info *section,
11639 bfd_size_type offset, bfd_size_type size)
11640 {
11641 struct dwarf2_section_info result;
11642 asection *sectp;
11643
11644 gdb_assert (section != NULL);
11645 gdb_assert (!section->is_virtual);
11646
11647 memset (&result, 0, sizeof (result));
11648 result.s.containing_section = section;
11649 result.is_virtual = true;
11650
11651 if (size == 0)
11652 return result;
11653
11654 sectp = section->get_bfd_section ();
11655
11656 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11657 bounds of the real section. This is a pretty-rare event, so just
11658 flag an error (easier) instead of a warning and trying to cope. */
11659 if (sectp == NULL
11660 || offset + size > bfd_section_size (sectp))
11661 {
11662 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11663 " in section %s [in module %s]"),
11664 sectp ? bfd_section_name (sectp) : "<unknown>",
11665 objfile_name (per_objfile->objfile));
11666 }
11667
11668 result.virtual_offset = offset;
11669 result.size = size;
11670 return result;
11671 }
11672
11673 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11674 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11675 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11676 This is for DWP version 2 files. */
11677
11678 static struct dwo_unit *
11679 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11680 struct dwp_file *dwp_file,
11681 uint32_t unit_index,
11682 const char *comp_dir,
11683 ULONGEST signature, int is_debug_types)
11684 {
11685 const struct dwp_hash_table *dwp_htab =
11686 is_debug_types ? dwp_file->tus : dwp_file->cus;
11687 bfd *dbfd = dwp_file->dbfd.get ();
11688 const char *kind = is_debug_types ? "TU" : "CU";
11689 struct dwo_file *dwo_file;
11690 struct dwo_unit *dwo_unit;
11691 struct virtual_v2_or_v5_dwo_sections sections;
11692 void **dwo_file_slot;
11693 int i;
11694
11695 gdb_assert (dwp_file->version == 2);
11696
11697 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11698 kind, pulongest (unit_index), hex_string (signature),
11699 dwp_file->name);
11700
11701 /* Fetch the section offsets of this DWO unit. */
11702
11703 memset (&sections, 0, sizeof (sections));
11704
11705 for (i = 0; i < dwp_htab->nr_columns; ++i)
11706 {
11707 uint32_t offset = read_4_bytes (dbfd,
11708 dwp_htab->section_pool.v2.offsets
11709 + (((unit_index - 1) * dwp_htab->nr_columns
11710 + i)
11711 * sizeof (uint32_t)));
11712 uint32_t size = read_4_bytes (dbfd,
11713 dwp_htab->section_pool.v2.sizes
11714 + (((unit_index - 1) * dwp_htab->nr_columns
11715 + i)
11716 * sizeof (uint32_t)));
11717
11718 switch (dwp_htab->section_pool.v2.section_ids[i])
11719 {
11720 case DW_SECT_INFO:
11721 case DW_SECT_TYPES:
11722 sections.info_or_types_offset = offset;
11723 sections.info_or_types_size = size;
11724 break;
11725 case DW_SECT_ABBREV:
11726 sections.abbrev_offset = offset;
11727 sections.abbrev_size = size;
11728 break;
11729 case DW_SECT_LINE:
11730 sections.line_offset = offset;
11731 sections.line_size = size;
11732 break;
11733 case DW_SECT_LOC:
11734 sections.loc_offset = offset;
11735 sections.loc_size = size;
11736 break;
11737 case DW_SECT_STR_OFFSETS:
11738 sections.str_offsets_offset = offset;
11739 sections.str_offsets_size = size;
11740 break;
11741 case DW_SECT_MACINFO:
11742 sections.macinfo_offset = offset;
11743 sections.macinfo_size = size;
11744 break;
11745 case DW_SECT_MACRO:
11746 sections.macro_offset = offset;
11747 sections.macro_size = size;
11748 break;
11749 }
11750 }
11751
11752 /* It's easier for the rest of the code if we fake a struct dwo_file and
11753 have dwo_unit "live" in that. At least for now.
11754
11755 The DWP file can be made up of a random collection of CUs and TUs.
11756 However, for each CU + set of TUs that came from the same original DWO
11757 file, we can combine them back into a virtual DWO file to save space
11758 (fewer struct dwo_file objects to allocate). Remember that for really
11759 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11760
11761 std::string virtual_dwo_name =
11762 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11763 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11764 (long) (sections.line_size ? sections.line_offset : 0),
11765 (long) (sections.loc_size ? sections.loc_offset : 0),
11766 (long) (sections.str_offsets_size
11767 ? sections.str_offsets_offset : 0));
11768 /* Can we use an existing virtual DWO file? */
11769 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11770 comp_dir);
11771 /* Create one if necessary. */
11772 if (*dwo_file_slot == NULL)
11773 {
11774 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11775 virtual_dwo_name.c_str ());
11776
11777 dwo_file = new struct dwo_file;
11778 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11779 dwo_file->comp_dir = comp_dir;
11780 dwo_file->sections.abbrev =
11781 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11782 sections.abbrev_offset,
11783 sections.abbrev_size);
11784 dwo_file->sections.line =
11785 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11786 sections.line_offset,
11787 sections.line_size);
11788 dwo_file->sections.loc =
11789 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11790 sections.loc_offset, sections.loc_size);
11791 dwo_file->sections.macinfo =
11792 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11793 sections.macinfo_offset,
11794 sections.macinfo_size);
11795 dwo_file->sections.macro =
11796 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11797 sections.macro_offset,
11798 sections.macro_size);
11799 dwo_file->sections.str_offsets =
11800 create_dwp_v2_or_v5_section (per_objfile,
11801 &dwp_file->sections.str_offsets,
11802 sections.str_offsets_offset,
11803 sections.str_offsets_size);
11804 /* The "str" section is global to the entire DWP file. */
11805 dwo_file->sections.str = dwp_file->sections.str;
11806 /* The info or types section is assigned below to dwo_unit,
11807 there's no need to record it in dwo_file.
11808 Also, we can't simply record type sections in dwo_file because
11809 we record a pointer into the vector in dwo_unit. As we collect more
11810 types we'll grow the vector and eventually have to reallocate space
11811 for it, invalidating all copies of pointers into the previous
11812 contents. */
11813 *dwo_file_slot = dwo_file;
11814 }
11815 else
11816 {
11817 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11818 virtual_dwo_name.c_str ());
11819
11820 dwo_file = (struct dwo_file *) *dwo_file_slot;
11821 }
11822
11823 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11824 dwo_unit->dwo_file = dwo_file;
11825 dwo_unit->signature = signature;
11826 dwo_unit->section =
11827 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11828 *dwo_unit->section = create_dwp_v2_or_v5_section
11829 (per_objfile,
11830 is_debug_types
11831 ? &dwp_file->sections.types
11832 : &dwp_file->sections.info,
11833 sections.info_or_types_offset,
11834 sections.info_or_types_size);
11835 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11836
11837 return dwo_unit;
11838 }
11839
11840 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11841 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11842 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11843 This is for DWP version 5 files. */
11844
11845 static struct dwo_unit *
11846 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11847 struct dwp_file *dwp_file,
11848 uint32_t unit_index,
11849 const char *comp_dir,
11850 ULONGEST signature, int is_debug_types)
11851 {
11852 const struct dwp_hash_table *dwp_htab
11853 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11854 bfd *dbfd = dwp_file->dbfd.get ();
11855 const char *kind = is_debug_types ? "TU" : "CU";
11856 struct dwo_file *dwo_file;
11857 struct dwo_unit *dwo_unit;
11858 struct virtual_v2_or_v5_dwo_sections sections {};
11859 void **dwo_file_slot;
11860
11861 gdb_assert (dwp_file->version == 5);
11862
11863 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11864 kind, pulongest (unit_index), hex_string (signature),
11865 dwp_file->name);
11866
11867 /* Fetch the section offsets of this DWO unit. */
11868
11869 /* memset (&sections, 0, sizeof (sections)); */
11870
11871 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11872 {
11873 uint32_t offset = read_4_bytes (dbfd,
11874 dwp_htab->section_pool.v5.offsets
11875 + (((unit_index - 1)
11876 * dwp_htab->nr_columns
11877 + i)
11878 * sizeof (uint32_t)));
11879 uint32_t size = read_4_bytes (dbfd,
11880 dwp_htab->section_pool.v5.sizes
11881 + (((unit_index - 1) * dwp_htab->nr_columns
11882 + i)
11883 * sizeof (uint32_t)));
11884
11885 switch (dwp_htab->section_pool.v5.section_ids[i])
11886 {
11887 case DW_SECT_ABBREV_V5:
11888 sections.abbrev_offset = offset;
11889 sections.abbrev_size = size;
11890 break;
11891 case DW_SECT_INFO_V5:
11892 sections.info_or_types_offset = offset;
11893 sections.info_or_types_size = size;
11894 break;
11895 case DW_SECT_LINE_V5:
11896 sections.line_offset = offset;
11897 sections.line_size = size;
11898 break;
11899 case DW_SECT_LOCLISTS_V5:
11900 sections.loclists_offset = offset;
11901 sections.loclists_size = size;
11902 break;
11903 case DW_SECT_MACRO_V5:
11904 sections.macro_offset = offset;
11905 sections.macro_size = size;
11906 break;
11907 case DW_SECT_RNGLISTS_V5:
11908 sections.rnglists_offset = offset;
11909 sections.rnglists_size = size;
11910 break;
11911 case DW_SECT_STR_OFFSETS_V5:
11912 sections.str_offsets_offset = offset;
11913 sections.str_offsets_size = size;
11914 break;
11915 case DW_SECT_RESERVED_V5:
11916 default:
11917 break;
11918 }
11919 }
11920
11921 /* It's easier for the rest of the code if we fake a struct dwo_file and
11922 have dwo_unit "live" in that. At least for now.
11923
11924 The DWP file can be made up of a random collection of CUs and TUs.
11925 However, for each CU + set of TUs that came from the same original DWO
11926 file, we can combine them back into a virtual DWO file to save space
11927 (fewer struct dwo_file objects to allocate). Remember that for really
11928 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11929
11930 std::string virtual_dwo_name =
11931 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
11932 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11933 (long) (sections.line_size ? sections.line_offset : 0),
11934 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11935 (long) (sections.str_offsets_size
11936 ? sections.str_offsets_offset : 0),
11937 (long) (sections.macro_size ? sections.macro_offset : 0),
11938 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
11939 /* Can we use an existing virtual DWO file? */
11940 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
11941 virtual_dwo_name.c_str (),
11942 comp_dir);
11943 /* Create one if necessary. */
11944 if (*dwo_file_slot == NULL)
11945 {
11946 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11947 virtual_dwo_name.c_str ());
11948
11949 dwo_file = new struct dwo_file;
11950 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11951 dwo_file->comp_dir = comp_dir;
11952 dwo_file->sections.abbrev =
11953 create_dwp_v2_or_v5_section (per_objfile,
11954 &dwp_file->sections.abbrev,
11955 sections.abbrev_offset,
11956 sections.abbrev_size);
11957 dwo_file->sections.line =
11958 create_dwp_v2_or_v5_section (per_objfile,
11959 &dwp_file->sections.line,
11960 sections.line_offset, sections.line_size);
11961 dwo_file->sections.macro =
11962 create_dwp_v2_or_v5_section (per_objfile,
11963 &dwp_file->sections.macro,
11964 sections.macro_offset,
11965 sections.macro_size);
11966 dwo_file->sections.loclists =
11967 create_dwp_v2_or_v5_section (per_objfile,
11968 &dwp_file->sections.loclists,
11969 sections.loclists_offset,
11970 sections.loclists_size);
11971 dwo_file->sections.rnglists =
11972 create_dwp_v2_or_v5_section (per_objfile,
11973 &dwp_file->sections.rnglists,
11974 sections.rnglists_offset,
11975 sections.rnglists_size);
11976 dwo_file->sections.str_offsets =
11977 create_dwp_v2_or_v5_section (per_objfile,
11978 &dwp_file->sections.str_offsets,
11979 sections.str_offsets_offset,
11980 sections.str_offsets_size);
11981 /* The "str" section is global to the entire DWP file. */
11982 dwo_file->sections.str = dwp_file->sections.str;
11983 /* The info or types section is assigned below to dwo_unit,
11984 there's no need to record it in dwo_file.
11985 Also, we can't simply record type sections in dwo_file because
11986 we record a pointer into the vector in dwo_unit. As we collect more
11987 types we'll grow the vector and eventually have to reallocate space
11988 for it, invalidating all copies of pointers into the previous
11989 contents. */
11990 *dwo_file_slot = dwo_file;
11991 }
11992 else
11993 {
11994 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11995 virtual_dwo_name.c_str ());
11996
11997 dwo_file = (struct dwo_file *) *dwo_file_slot;
11998 }
11999
12000 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12001 dwo_unit->dwo_file = dwo_file;
12002 dwo_unit->signature = signature;
12003 dwo_unit->section
12004 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12005 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12006 &dwp_file->sections.info,
12007 sections.info_or_types_offset,
12008 sections.info_or_types_size);
12009 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12010
12011 return dwo_unit;
12012 }
12013
12014 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12015 Returns NULL if the signature isn't found. */
12016
12017 static struct dwo_unit *
12018 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12019 struct dwp_file *dwp_file, const char *comp_dir,
12020 ULONGEST signature, int is_debug_types)
12021 {
12022 const struct dwp_hash_table *dwp_htab =
12023 is_debug_types ? dwp_file->tus : dwp_file->cus;
12024 bfd *dbfd = dwp_file->dbfd.get ();
12025 uint32_t mask = dwp_htab->nr_slots - 1;
12026 uint32_t hash = signature & mask;
12027 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12028 unsigned int i;
12029 void **slot;
12030 struct dwo_unit find_dwo_cu;
12031
12032 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12033 find_dwo_cu.signature = signature;
12034 slot = htab_find_slot (is_debug_types
12035 ? dwp_file->loaded_tus.get ()
12036 : dwp_file->loaded_cus.get (),
12037 &find_dwo_cu, INSERT);
12038
12039 if (*slot != NULL)
12040 return (struct dwo_unit *) *slot;
12041
12042 /* Use a for loop so that we don't loop forever on bad debug info. */
12043 for (i = 0; i < dwp_htab->nr_slots; ++i)
12044 {
12045 ULONGEST signature_in_table;
12046
12047 signature_in_table =
12048 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12049 if (signature_in_table == signature)
12050 {
12051 uint32_t unit_index =
12052 read_4_bytes (dbfd,
12053 dwp_htab->unit_table + hash * sizeof (uint32_t));
12054
12055 if (dwp_file->version == 1)
12056 {
12057 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12058 unit_index, comp_dir,
12059 signature, is_debug_types);
12060 }
12061 else if (dwp_file->version == 2)
12062 {
12063 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12064 unit_index, comp_dir,
12065 signature, is_debug_types);
12066 }
12067 else /* version == 5 */
12068 {
12069 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12070 unit_index, comp_dir,
12071 signature, is_debug_types);
12072 }
12073 return (struct dwo_unit *) *slot;
12074 }
12075 if (signature_in_table == 0)
12076 return NULL;
12077 hash = (hash + hash2) & mask;
12078 }
12079
12080 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12081 " [in module %s]"),
12082 dwp_file->name);
12083 }
12084
12085 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12086 Open the file specified by FILE_NAME and hand it off to BFD for
12087 preliminary analysis. Return a newly initialized bfd *, which
12088 includes a canonicalized copy of FILE_NAME.
12089 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12090 SEARCH_CWD is true if the current directory is to be searched.
12091 It will be searched before debug-file-directory.
12092 If successful, the file is added to the bfd include table of the
12093 objfile's bfd (see gdb_bfd_record_inclusion).
12094 If unable to find/open the file, return NULL.
12095 NOTE: This function is derived from symfile_bfd_open. */
12096
12097 static gdb_bfd_ref_ptr
12098 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12099 const char *file_name, int is_dwp, int search_cwd)
12100 {
12101 int desc;
12102 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12103 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12104 to debug_file_directory. */
12105 const char *search_path;
12106 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12107
12108 gdb::unique_xmalloc_ptr<char> search_path_holder;
12109 if (search_cwd)
12110 {
12111 if (!debug_file_directory.empty ())
12112 {
12113 search_path_holder.reset (concat (".", dirname_separator_string,
12114 debug_file_directory.c_str (),
12115 (char *) NULL));
12116 search_path = search_path_holder.get ();
12117 }
12118 else
12119 search_path = ".";
12120 }
12121 else
12122 search_path = debug_file_directory.c_str ();
12123
12124 /* Add the path for the executable binary to the list of search paths. */
12125 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12126 search_path_holder.reset (concat (objfile_dir.c_str (),
12127 dirname_separator_string,
12128 search_path, nullptr));
12129 search_path = search_path_holder.get ();
12130
12131 openp_flags flags = OPF_RETURN_REALPATH;
12132 if (is_dwp)
12133 flags |= OPF_SEARCH_IN_PATH;
12134
12135 gdb::unique_xmalloc_ptr<char> absolute_name;
12136 desc = openp (search_path, flags, file_name,
12137 O_RDONLY | O_BINARY, &absolute_name);
12138 if (desc < 0)
12139 return NULL;
12140
12141 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12142 gnutarget, desc));
12143 if (sym_bfd == NULL)
12144 return NULL;
12145 bfd_set_cacheable (sym_bfd.get (), 1);
12146
12147 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12148 return NULL;
12149
12150 /* Success. Record the bfd as having been included by the objfile's bfd.
12151 This is important because things like demangled_names_hash lives in the
12152 objfile's per_bfd space and may have references to things like symbol
12153 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12154 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12155
12156 return sym_bfd;
12157 }
12158
12159 /* Try to open DWO file FILE_NAME.
12160 COMP_DIR is the DW_AT_comp_dir attribute.
12161 The result is the bfd handle of the file.
12162 If there is a problem finding or opening the file, return NULL.
12163 Upon success, the canonicalized path of the file is stored in the bfd,
12164 same as symfile_bfd_open. */
12165
12166 static gdb_bfd_ref_ptr
12167 open_dwo_file (dwarf2_per_objfile *per_objfile,
12168 const char *file_name, const char *comp_dir)
12169 {
12170 if (IS_ABSOLUTE_PATH (file_name))
12171 return try_open_dwop_file (per_objfile, file_name,
12172 0 /*is_dwp*/, 0 /*search_cwd*/);
12173
12174 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12175
12176 if (comp_dir != NULL)
12177 {
12178 gdb::unique_xmalloc_ptr<char> path_to_try
12179 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12180
12181 /* NOTE: If comp_dir is a relative path, this will also try the
12182 search path, which seems useful. */
12183 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12184 0 /*is_dwp*/,
12185 1 /*search_cwd*/));
12186 if (abfd != NULL)
12187 return abfd;
12188 }
12189
12190 /* That didn't work, try debug-file-directory, which, despite its name,
12191 is a list of paths. */
12192
12193 if (debug_file_directory.empty ())
12194 return NULL;
12195
12196 return try_open_dwop_file (per_objfile, file_name,
12197 0 /*is_dwp*/, 1 /*search_cwd*/);
12198 }
12199
12200 /* This function is mapped across the sections and remembers the offset and
12201 size of each of the DWO debugging sections we are interested in. */
12202
12203 static void
12204 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12205 dwo_sections *dwo_sections)
12206 {
12207 const struct dwop_section_names *names = &dwop_section_names;
12208
12209 if (names->abbrev_dwo.matches (sectp->name))
12210 {
12211 dwo_sections->abbrev.s.section = sectp;
12212 dwo_sections->abbrev.size = bfd_section_size (sectp);
12213 }
12214 else if (names->info_dwo.matches (sectp->name))
12215 {
12216 dwo_sections->info.s.section = sectp;
12217 dwo_sections->info.size = bfd_section_size (sectp);
12218 }
12219 else if (names->line_dwo.matches (sectp->name))
12220 {
12221 dwo_sections->line.s.section = sectp;
12222 dwo_sections->line.size = bfd_section_size (sectp);
12223 }
12224 else if (names->loc_dwo.matches (sectp->name))
12225 {
12226 dwo_sections->loc.s.section = sectp;
12227 dwo_sections->loc.size = bfd_section_size (sectp);
12228 }
12229 else if (names->loclists_dwo.matches (sectp->name))
12230 {
12231 dwo_sections->loclists.s.section = sectp;
12232 dwo_sections->loclists.size = bfd_section_size (sectp);
12233 }
12234 else if (names->macinfo_dwo.matches (sectp->name))
12235 {
12236 dwo_sections->macinfo.s.section = sectp;
12237 dwo_sections->macinfo.size = bfd_section_size (sectp);
12238 }
12239 else if (names->macro_dwo.matches (sectp->name))
12240 {
12241 dwo_sections->macro.s.section = sectp;
12242 dwo_sections->macro.size = bfd_section_size (sectp);
12243 }
12244 else if (names->rnglists_dwo.matches (sectp->name))
12245 {
12246 dwo_sections->rnglists.s.section = sectp;
12247 dwo_sections->rnglists.size = bfd_section_size (sectp);
12248 }
12249 else if (names->str_dwo.matches (sectp->name))
12250 {
12251 dwo_sections->str.s.section = sectp;
12252 dwo_sections->str.size = bfd_section_size (sectp);
12253 }
12254 else if (names->str_offsets_dwo.matches (sectp->name))
12255 {
12256 dwo_sections->str_offsets.s.section = sectp;
12257 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12258 }
12259 else if (names->types_dwo.matches (sectp->name))
12260 {
12261 struct dwarf2_section_info type_section;
12262
12263 memset (&type_section, 0, sizeof (type_section));
12264 type_section.s.section = sectp;
12265 type_section.size = bfd_section_size (sectp);
12266 dwo_sections->types.push_back (type_section);
12267 }
12268 }
12269
12270 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12271 by PER_CU. This is for the non-DWP case.
12272 The result is NULL if DWO_NAME can't be found. */
12273
12274 static struct dwo_file *
12275 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12276 const char *comp_dir)
12277 {
12278 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12279
12280 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12281 if (dbfd == NULL)
12282 {
12283 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12284
12285 return NULL;
12286 }
12287
12288 dwo_file_up dwo_file (new struct dwo_file);
12289 dwo_file->dwo_name = dwo_name;
12290 dwo_file->comp_dir = comp_dir;
12291 dwo_file->dbfd = std::move (dbfd);
12292
12293 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12294 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12295 &dwo_file->sections);
12296
12297 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12298 dwo_file->cus);
12299
12300 if (cu->per_cu->dwarf_version < 5)
12301 {
12302 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12303 dwo_file->sections.types, dwo_file->tus);
12304 }
12305 else
12306 {
12307 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12308 &dwo_file->sections.info, dwo_file->tus,
12309 rcuh_kind::COMPILE);
12310 }
12311
12312 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12313
12314 return dwo_file.release ();
12315 }
12316
12317 /* This function is mapped across the sections and remembers the offset and
12318 size of each of the DWP debugging sections common to version 1 and 2 that
12319 we are interested in. */
12320
12321 static void
12322 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12323 dwp_file *dwp_file)
12324 {
12325 const struct dwop_section_names *names = &dwop_section_names;
12326 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12327
12328 /* Record the ELF section number for later lookup: this is what the
12329 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12330 gdb_assert (elf_section_nr < dwp_file->num_sections);
12331 dwp_file->elf_sections[elf_section_nr] = sectp;
12332
12333 /* Look for specific sections that we need. */
12334 if (names->str_dwo.matches (sectp->name))
12335 {
12336 dwp_file->sections.str.s.section = sectp;
12337 dwp_file->sections.str.size = bfd_section_size (sectp);
12338 }
12339 else if (names->cu_index.matches (sectp->name))
12340 {
12341 dwp_file->sections.cu_index.s.section = sectp;
12342 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12343 }
12344 else if (names->tu_index.matches (sectp->name))
12345 {
12346 dwp_file->sections.tu_index.s.section = sectp;
12347 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12348 }
12349 }
12350
12351 /* This function is mapped across the sections and remembers the offset and
12352 size of each of the DWP version 2 debugging sections that we are interested
12353 in. This is split into a separate function because we don't know if we
12354 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12355
12356 static void
12357 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12358 {
12359 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12360 const struct dwop_section_names *names = &dwop_section_names;
12361 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12362
12363 /* Record the ELF section number for later lookup: this is what the
12364 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12365 gdb_assert (elf_section_nr < dwp_file->num_sections);
12366 dwp_file->elf_sections[elf_section_nr] = sectp;
12367
12368 /* Look for specific sections that we need. */
12369 if (names->abbrev_dwo.matches (sectp->name))
12370 {
12371 dwp_file->sections.abbrev.s.section = sectp;
12372 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12373 }
12374 else if (names->info_dwo.matches (sectp->name))
12375 {
12376 dwp_file->sections.info.s.section = sectp;
12377 dwp_file->sections.info.size = bfd_section_size (sectp);
12378 }
12379 else if (names->line_dwo.matches (sectp->name))
12380 {
12381 dwp_file->sections.line.s.section = sectp;
12382 dwp_file->sections.line.size = bfd_section_size (sectp);
12383 }
12384 else if (names->loc_dwo.matches (sectp->name))
12385 {
12386 dwp_file->sections.loc.s.section = sectp;
12387 dwp_file->sections.loc.size = bfd_section_size (sectp);
12388 }
12389 else if (names->macinfo_dwo.matches (sectp->name))
12390 {
12391 dwp_file->sections.macinfo.s.section = sectp;
12392 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12393 }
12394 else if (names->macro_dwo.matches (sectp->name))
12395 {
12396 dwp_file->sections.macro.s.section = sectp;
12397 dwp_file->sections.macro.size = bfd_section_size (sectp);
12398 }
12399 else if (names->str_offsets_dwo.matches (sectp->name))
12400 {
12401 dwp_file->sections.str_offsets.s.section = sectp;
12402 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12403 }
12404 else if (names->types_dwo.matches (sectp->name))
12405 {
12406 dwp_file->sections.types.s.section = sectp;
12407 dwp_file->sections.types.size = bfd_section_size (sectp);
12408 }
12409 }
12410
12411 /* This function is mapped across the sections and remembers the offset and
12412 size of each of the DWP version 5 debugging sections that we are interested
12413 in. This is split into a separate function because we don't know if we
12414 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12415
12416 static void
12417 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12418 {
12419 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12420 const struct dwop_section_names *names = &dwop_section_names;
12421 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12422
12423 /* Record the ELF section number for later lookup: this is what the
12424 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12425 gdb_assert (elf_section_nr < dwp_file->num_sections);
12426 dwp_file->elf_sections[elf_section_nr] = sectp;
12427
12428 /* Look for specific sections that we need. */
12429 if (names->abbrev_dwo.matches (sectp->name))
12430 {
12431 dwp_file->sections.abbrev.s.section = sectp;
12432 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12433 }
12434 else if (names->info_dwo.matches (sectp->name))
12435 {
12436 dwp_file->sections.info.s.section = sectp;
12437 dwp_file->sections.info.size = bfd_section_size (sectp);
12438 }
12439 else if (names->line_dwo.matches (sectp->name))
12440 {
12441 dwp_file->sections.line.s.section = sectp;
12442 dwp_file->sections.line.size = bfd_section_size (sectp);
12443 }
12444 else if (names->loclists_dwo.matches (sectp->name))
12445 {
12446 dwp_file->sections.loclists.s.section = sectp;
12447 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12448 }
12449 else if (names->macro_dwo.matches (sectp->name))
12450 {
12451 dwp_file->sections.macro.s.section = sectp;
12452 dwp_file->sections.macro.size = bfd_section_size (sectp);
12453 }
12454 else if (names->rnglists_dwo.matches (sectp->name))
12455 {
12456 dwp_file->sections.rnglists.s.section = sectp;
12457 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12458 }
12459 else if (names->str_offsets_dwo.matches (sectp->name))
12460 {
12461 dwp_file->sections.str_offsets.s.section = sectp;
12462 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12463 }
12464 }
12465
12466 /* Hash function for dwp_file loaded CUs/TUs. */
12467
12468 static hashval_t
12469 hash_dwp_loaded_cutus (const void *item)
12470 {
12471 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12472
12473 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12474 return dwo_unit->signature;
12475 }
12476
12477 /* Equality function for dwp_file loaded CUs/TUs. */
12478
12479 static int
12480 eq_dwp_loaded_cutus (const void *a, const void *b)
12481 {
12482 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12483 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12484
12485 return dua->signature == dub->signature;
12486 }
12487
12488 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12489
12490 static htab_up
12491 allocate_dwp_loaded_cutus_table ()
12492 {
12493 return htab_up (htab_create_alloc (3,
12494 hash_dwp_loaded_cutus,
12495 eq_dwp_loaded_cutus,
12496 NULL, xcalloc, xfree));
12497 }
12498
12499 /* Try to open DWP file FILE_NAME.
12500 The result is the bfd handle of the file.
12501 If there is a problem finding or opening the file, return NULL.
12502 Upon success, the canonicalized path of the file is stored in the bfd,
12503 same as symfile_bfd_open. */
12504
12505 static gdb_bfd_ref_ptr
12506 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12507 {
12508 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12509 1 /*is_dwp*/,
12510 1 /*search_cwd*/));
12511 if (abfd != NULL)
12512 return abfd;
12513
12514 /* Work around upstream bug 15652.
12515 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12516 [Whether that's a "bug" is debatable, but it is getting in our way.]
12517 We have no real idea where the dwp file is, because gdb's realpath-ing
12518 of the executable's path may have discarded the needed info.
12519 [IWBN if the dwp file name was recorded in the executable, akin to
12520 .gnu_debuglink, but that doesn't exist yet.]
12521 Strip the directory from FILE_NAME and search again. */
12522 if (!debug_file_directory.empty ())
12523 {
12524 /* Don't implicitly search the current directory here.
12525 If the user wants to search "." to handle this case,
12526 it must be added to debug-file-directory. */
12527 return try_open_dwop_file (per_objfile, lbasename (file_name),
12528 1 /*is_dwp*/,
12529 0 /*search_cwd*/);
12530 }
12531
12532 return NULL;
12533 }
12534
12535 /* Initialize the use of the DWP file for the current objfile.
12536 By convention the name of the DWP file is ${objfile}.dwp.
12537 The result is NULL if it can't be found. */
12538
12539 static std::unique_ptr<struct dwp_file>
12540 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12541 {
12542 struct objfile *objfile = per_objfile->objfile;
12543
12544 /* Try to find first .dwp for the binary file before any symbolic links
12545 resolving. */
12546
12547 /* If the objfile is a debug file, find the name of the real binary
12548 file and get the name of dwp file from there. */
12549 std::string dwp_name;
12550 if (objfile->separate_debug_objfile_backlink != NULL)
12551 {
12552 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12553 const char *backlink_basename = lbasename (backlink->original_name);
12554
12555 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12556 }
12557 else
12558 dwp_name = objfile->original_name;
12559
12560 dwp_name += ".dwp";
12561
12562 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12563 if (dbfd == NULL
12564 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12565 {
12566 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12567 dwp_name = objfile_name (objfile);
12568 dwp_name += ".dwp";
12569 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12570 }
12571
12572 if (dbfd == NULL)
12573 {
12574 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12575
12576 return std::unique_ptr<dwp_file> ();
12577 }
12578
12579 const char *name = bfd_get_filename (dbfd.get ());
12580 std::unique_ptr<struct dwp_file> dwp_file
12581 (new struct dwp_file (name, std::move (dbfd)));
12582
12583 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12584 dwp_file->elf_sections =
12585 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12586 dwp_file->num_sections, asection *);
12587
12588 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12589 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12590 dwp_file.get ());
12591
12592 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12593
12594 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12595
12596 /* The DWP file version is stored in the hash table. Oh well. */
12597 if (dwp_file->cus && dwp_file->tus
12598 && dwp_file->cus->version != dwp_file->tus->version)
12599 {
12600 /* Technically speaking, we should try to limp along, but this is
12601 pretty bizarre. We use pulongest here because that's the established
12602 portability solution (e.g, we cannot use %u for uint32_t). */
12603 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12604 " TU version %s [in DWP file %s]"),
12605 pulongest (dwp_file->cus->version),
12606 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12607 }
12608
12609 if (dwp_file->cus)
12610 dwp_file->version = dwp_file->cus->version;
12611 else if (dwp_file->tus)
12612 dwp_file->version = dwp_file->tus->version;
12613 else
12614 dwp_file->version = 2;
12615
12616 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12617 {
12618 if (dwp_file->version == 2)
12619 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12620 dwp_file.get ());
12621 else
12622 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12623 dwp_file.get ());
12624 }
12625
12626 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12627 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12628
12629 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12630 dwarf_read_debug_printf (" %s CUs, %s TUs",
12631 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12632 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12633
12634 return dwp_file;
12635 }
12636
12637 /* Wrapper around open_and_init_dwp_file, only open it once. */
12638
12639 static struct dwp_file *
12640 get_dwp_file (dwarf2_per_objfile *per_objfile)
12641 {
12642 if (!per_objfile->per_bfd->dwp_checked)
12643 {
12644 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12645 per_objfile->per_bfd->dwp_checked = 1;
12646 }
12647 return per_objfile->per_bfd->dwp_file.get ();
12648 }
12649
12650 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12651 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12652 or in the DWP file for the objfile, referenced by THIS_UNIT.
12653 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12654 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12655
12656 This is called, for example, when wanting to read a variable with a
12657 complex location. Therefore we don't want to do file i/o for every call.
12658 Therefore we don't want to look for a DWO file on every call.
12659 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12660 then we check if we've already seen DWO_NAME, and only THEN do we check
12661 for a DWO file.
12662
12663 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12664 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12665
12666 static struct dwo_unit *
12667 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12668 ULONGEST signature, int is_debug_types)
12669 {
12670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12671 struct objfile *objfile = per_objfile->objfile;
12672 const char *kind = is_debug_types ? "TU" : "CU";
12673 void **dwo_file_slot;
12674 struct dwo_file *dwo_file;
12675 struct dwp_file *dwp_file;
12676
12677 /* First see if there's a DWP file.
12678 If we have a DWP file but didn't find the DWO inside it, don't
12679 look for the original DWO file. It makes gdb behave differently
12680 depending on whether one is debugging in the build tree. */
12681
12682 dwp_file = get_dwp_file (per_objfile);
12683 if (dwp_file != NULL)
12684 {
12685 const struct dwp_hash_table *dwp_htab =
12686 is_debug_types ? dwp_file->tus : dwp_file->cus;
12687
12688 if (dwp_htab != NULL)
12689 {
12690 struct dwo_unit *dwo_cutu =
12691 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12692 is_debug_types);
12693
12694 if (dwo_cutu != NULL)
12695 {
12696 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12697 kind, hex_string (signature),
12698 host_address_to_string (dwo_cutu));
12699
12700 return dwo_cutu;
12701 }
12702 }
12703 }
12704 else
12705 {
12706 /* No DWP file, look for the DWO file. */
12707
12708 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12709 if (*dwo_file_slot == NULL)
12710 {
12711 /* Read in the file and build a table of the CUs/TUs it contains. */
12712 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12713 }
12714 /* NOTE: This will be NULL if unable to open the file. */
12715 dwo_file = (struct dwo_file *) *dwo_file_slot;
12716
12717 if (dwo_file != NULL)
12718 {
12719 struct dwo_unit *dwo_cutu = NULL;
12720
12721 if (is_debug_types && dwo_file->tus)
12722 {
12723 struct dwo_unit find_dwo_cutu;
12724
12725 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12726 find_dwo_cutu.signature = signature;
12727 dwo_cutu
12728 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12729 &find_dwo_cutu);
12730 }
12731 else if (!is_debug_types && dwo_file->cus)
12732 {
12733 struct dwo_unit find_dwo_cutu;
12734
12735 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12736 find_dwo_cutu.signature = signature;
12737 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12738 &find_dwo_cutu);
12739 }
12740
12741 if (dwo_cutu != NULL)
12742 {
12743 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12744 kind, dwo_name, hex_string (signature),
12745 host_address_to_string (dwo_cutu));
12746
12747 return dwo_cutu;
12748 }
12749 }
12750 }
12751
12752 /* We didn't find it. This could mean a dwo_id mismatch, or
12753 someone deleted the DWO/DWP file, or the search path isn't set up
12754 correctly to find the file. */
12755
12756 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12757 kind, dwo_name, hex_string (signature));
12758
12759 /* This is a warning and not a complaint because it can be caused by
12760 pilot error (e.g., user accidentally deleting the DWO). */
12761 {
12762 /* Print the name of the DWP file if we looked there, helps the user
12763 better diagnose the problem. */
12764 std::string dwp_text;
12765
12766 if (dwp_file != NULL)
12767 dwp_text = string_printf (" [in DWP file %s]",
12768 lbasename (dwp_file->name));
12769
12770 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12771 " [in module %s]"),
12772 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12773 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12774 }
12775 return NULL;
12776 }
12777
12778 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12779 See lookup_dwo_cutu_unit for details. */
12780
12781 static struct dwo_unit *
12782 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12783 ULONGEST signature)
12784 {
12785 gdb_assert (!cu->per_cu->is_debug_types);
12786
12787 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12788 }
12789
12790 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12791 See lookup_dwo_cutu_unit for details. */
12792
12793 static struct dwo_unit *
12794 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12795 {
12796 gdb_assert (cu->per_cu->is_debug_types);
12797
12798 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12799
12800 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12801 }
12802
12803 /* Traversal function for queue_and_load_all_dwo_tus. */
12804
12805 static int
12806 queue_and_load_dwo_tu (void **slot, void *info)
12807 {
12808 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12809 dwarf2_cu *cu = (dwarf2_cu *) info;
12810 ULONGEST signature = dwo_unit->signature;
12811 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12812
12813 if (sig_type != NULL)
12814 {
12815 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12816 a real dependency of PER_CU on SIG_TYPE. That is detected later
12817 while processing PER_CU. */
12818 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12819 cu->per_cu->lang))
12820 load_full_type_unit (sig_type, cu->per_objfile);
12821 cu->per_cu->imported_symtabs_push (sig_type);
12822 }
12823
12824 return 1;
12825 }
12826
12827 /* Queue all TUs contained in the DWO of CU to be read in.
12828 The DWO may have the only definition of the type, though it may not be
12829 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12830 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12831
12832 static void
12833 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12834 {
12835 struct dwo_unit *dwo_unit;
12836 struct dwo_file *dwo_file;
12837
12838 gdb_assert (cu != nullptr);
12839 gdb_assert (!cu->per_cu->is_debug_types);
12840 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12841
12842 dwo_unit = cu->dwo_unit;
12843 gdb_assert (dwo_unit != NULL);
12844
12845 dwo_file = dwo_unit->dwo_file;
12846 if (dwo_file->tus != NULL)
12847 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12848 }
12849
12850 /* Read in various DIEs. */
12851
12852 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12853 Inherit only the children of the DW_AT_abstract_origin DIE not being
12854 already referenced by DW_AT_abstract_origin from the children of the
12855 current DIE. */
12856
12857 static void
12858 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12859 {
12860 struct die_info *child_die;
12861 sect_offset *offsetp;
12862 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12863 struct die_info *origin_die;
12864 /* Iterator of the ORIGIN_DIE children. */
12865 struct die_info *origin_child_die;
12866 struct attribute *attr;
12867 struct dwarf2_cu *origin_cu;
12868 struct pending **origin_previous_list_in_scope;
12869
12870 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12871 if (!attr)
12872 return;
12873
12874 /* Note that following die references may follow to a die in a
12875 different cu. */
12876
12877 origin_cu = cu;
12878 origin_die = follow_die_ref (die, attr, &origin_cu);
12879
12880 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12881 symbols in. */
12882 origin_previous_list_in_scope = origin_cu->list_in_scope;
12883 origin_cu->list_in_scope = cu->list_in_scope;
12884
12885 if (die->tag != origin_die->tag
12886 && !(die->tag == DW_TAG_inlined_subroutine
12887 && origin_die->tag == DW_TAG_subprogram))
12888 complaint (_("DIE %s and its abstract origin %s have different tags"),
12889 sect_offset_str (die->sect_off),
12890 sect_offset_str (origin_die->sect_off));
12891
12892 /* Find if the concrete and abstract trees are structurally the
12893 same. This is a shallow traversal and it is not bullet-proof;
12894 the compiler can trick the debugger into believing that the trees
12895 are isomorphic, whereas they actually are not. However, the
12896 likelyhood of this happening is pretty low, and a full-fledged
12897 check would be an overkill. */
12898 bool are_isomorphic = true;
12899 die_info *concrete_child = die->child;
12900 die_info *abstract_child = origin_die->child;
12901 while (concrete_child != nullptr || abstract_child != nullptr)
12902 {
12903 if (concrete_child == nullptr
12904 || abstract_child == nullptr
12905 || concrete_child->tag != abstract_child->tag)
12906 {
12907 are_isomorphic = false;
12908 break;
12909 }
12910
12911 concrete_child = concrete_child->sibling;
12912 abstract_child = abstract_child->sibling;
12913 }
12914
12915 /* Walk the origin's children in parallel to the concrete children.
12916 This helps match an origin child in case the debug info misses
12917 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12918 origin tree may not have the same tree structure as the concrete
12919 DIE, though. */
12920 die_info *corresponding_abstract_child
12921 = are_isomorphic ? origin_die->child : nullptr;
12922
12923 std::vector<sect_offset> offsets;
12924
12925 for (child_die = die->child;
12926 child_die && child_die->tag;
12927 child_die = child_die->sibling)
12928 {
12929 struct die_info *child_origin_die;
12930 struct dwarf2_cu *child_origin_cu;
12931
12932 /* We are trying to process concrete instance entries:
12933 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12934 it's not relevant to our analysis here. i.e. detecting DIEs that are
12935 present in the abstract instance but not referenced in the concrete
12936 one. */
12937 if (child_die->tag == DW_TAG_call_site
12938 || child_die->tag == DW_TAG_GNU_call_site)
12939 {
12940 if (are_isomorphic)
12941 corresponding_abstract_child
12942 = corresponding_abstract_child->sibling;
12943 continue;
12944 }
12945
12946 /* For each CHILD_DIE, find the corresponding child of
12947 ORIGIN_DIE. If there is more than one layer of
12948 DW_AT_abstract_origin, follow them all; there shouldn't be,
12949 but GCC versions at least through 4.4 generate this (GCC PR
12950 40573). */
12951 child_origin_die = child_die;
12952 child_origin_cu = cu;
12953 while (1)
12954 {
12955 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12956 child_origin_cu);
12957 if (attr == NULL)
12958 break;
12959 child_origin_die = follow_die_ref (child_origin_die, attr,
12960 &child_origin_cu);
12961 }
12962
12963 /* If missing DW_AT_abstract_origin, try the corresponding child
12964 of the origin. Clang emits such lexical scopes. */
12965 if (child_origin_die == child_die
12966 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12967 && are_isomorphic
12968 && child_die->tag == DW_TAG_lexical_block)
12969 child_origin_die = corresponding_abstract_child;
12970
12971 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12972 counterpart may exist. */
12973 if (child_origin_die != child_die)
12974 {
12975 if (child_die->tag != child_origin_die->tag
12976 && !(child_die->tag == DW_TAG_inlined_subroutine
12977 && child_origin_die->tag == DW_TAG_subprogram))
12978 complaint (_("Child DIE %s and its abstract origin %s have "
12979 "different tags"),
12980 sect_offset_str (child_die->sect_off),
12981 sect_offset_str (child_origin_die->sect_off));
12982 if (child_origin_die->parent != origin_die)
12983 complaint (_("Child DIE %s and its abstract origin %s have "
12984 "different parents"),
12985 sect_offset_str (child_die->sect_off),
12986 sect_offset_str (child_origin_die->sect_off));
12987 else
12988 offsets.push_back (child_origin_die->sect_off);
12989 }
12990
12991 if (are_isomorphic)
12992 corresponding_abstract_child = corresponding_abstract_child->sibling;
12993 }
12994 std::sort (offsets.begin (), offsets.end ());
12995 sect_offset *offsets_end = offsets.data () + offsets.size ();
12996 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12997 if (offsetp[-1] == *offsetp)
12998 complaint (_("Multiple children of DIE %s refer "
12999 "to DIE %s as their abstract origin"),
13000 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13001
13002 offsetp = offsets.data ();
13003 origin_child_die = origin_die->child;
13004 while (origin_child_die && origin_child_die->tag)
13005 {
13006 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13007 while (offsetp < offsets_end
13008 && *offsetp < origin_child_die->sect_off)
13009 offsetp++;
13010 if (offsetp >= offsets_end
13011 || *offsetp > origin_child_die->sect_off)
13012 {
13013 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13014 Check whether we're already processing ORIGIN_CHILD_DIE.
13015 This can happen with mutually referenced abstract_origins.
13016 PR 16581. */
13017 if (!origin_child_die->in_process)
13018 process_die (origin_child_die, origin_cu);
13019 }
13020 origin_child_die = origin_child_die->sibling;
13021 }
13022 origin_cu->list_in_scope = origin_previous_list_in_scope;
13023
13024 if (cu != origin_cu)
13025 compute_delayed_physnames (origin_cu);
13026 }
13027
13028 static void
13029 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13030 {
13031 struct objfile *objfile = cu->per_objfile->objfile;
13032 struct gdbarch *gdbarch = objfile->arch ();
13033 struct context_stack *newobj;
13034 CORE_ADDR lowpc;
13035 CORE_ADDR highpc;
13036 struct die_info *child_die;
13037 struct attribute *attr, *call_line, *call_file;
13038 const char *name;
13039 CORE_ADDR baseaddr;
13040 struct block *block;
13041 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13042 std::vector<struct symbol *> template_args;
13043 struct template_symbol *templ_func = NULL;
13044
13045 if (inlined_func)
13046 {
13047 /* If we do not have call site information, we can't show the
13048 caller of this inlined function. That's too confusing, so
13049 only use the scope for local variables. */
13050 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13051 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13052 if (call_line == NULL || call_file == NULL)
13053 {
13054 read_lexical_block_scope (die, cu);
13055 return;
13056 }
13057 }
13058
13059 baseaddr = objfile->text_section_offset ();
13060
13061 name = dwarf2_name (die, cu);
13062
13063 /* Ignore functions with missing or empty names. These are actually
13064 illegal according to the DWARF standard. */
13065 if (name == NULL)
13066 {
13067 complaint (_("missing name for subprogram DIE at %s"),
13068 sect_offset_str (die->sect_off));
13069 return;
13070 }
13071
13072 /* Ignore functions with missing or invalid low and high pc attributes. */
13073 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13074 <= PC_BOUNDS_INVALID)
13075 {
13076 attr = dwarf2_attr (die, DW_AT_external, cu);
13077 if (attr == nullptr || !attr->as_boolean ())
13078 complaint (_("cannot get low and high bounds "
13079 "for subprogram DIE at %s"),
13080 sect_offset_str (die->sect_off));
13081 return;
13082 }
13083
13084 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13085 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13086
13087 /* If we have any template arguments, then we must allocate a
13088 different sort of symbol. */
13089 for (child_die = die->child; child_die; child_die = child_die->sibling)
13090 {
13091 if (child_die->tag == DW_TAG_template_type_param
13092 || child_die->tag == DW_TAG_template_value_param)
13093 {
13094 templ_func = new (&objfile->objfile_obstack) template_symbol;
13095 templ_func->subclass = SYMBOL_TEMPLATE;
13096 break;
13097 }
13098 }
13099
13100 gdb_assert (cu->get_builder () != nullptr);
13101 newobj = cu->get_builder ()->push_context (0, lowpc);
13102 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13103 (struct symbol *) templ_func);
13104
13105 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13106 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13107 cu->per_cu->lang);
13108
13109 /* If there is a location expression for DW_AT_frame_base, record
13110 it. */
13111 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13112 if (attr != nullptr)
13113 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13114
13115 /* If there is a location for the static link, record it. */
13116 newobj->static_link = NULL;
13117 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13118 if (attr != nullptr)
13119 {
13120 newobj->static_link
13121 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13122 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13123 cu->addr_type ());
13124 }
13125
13126 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13127
13128 if (die->child != NULL)
13129 {
13130 child_die = die->child;
13131 while (child_die && child_die->tag)
13132 {
13133 if (child_die->tag == DW_TAG_template_type_param
13134 || child_die->tag == DW_TAG_template_value_param)
13135 {
13136 struct symbol *arg = new_symbol (child_die, NULL, cu);
13137
13138 if (arg != NULL)
13139 template_args.push_back (arg);
13140 }
13141 else
13142 process_die (child_die, cu);
13143 child_die = child_die->sibling;
13144 }
13145 }
13146
13147 inherit_abstract_dies (die, cu);
13148
13149 /* If we have a DW_AT_specification, we might need to import using
13150 directives from the context of the specification DIE. See the
13151 comment in determine_prefix. */
13152 if (cu->per_cu->lang == language_cplus
13153 && dwarf2_attr (die, DW_AT_specification, cu))
13154 {
13155 struct dwarf2_cu *spec_cu = cu;
13156 struct die_info *spec_die = die_specification (die, &spec_cu);
13157
13158 while (spec_die)
13159 {
13160 child_die = spec_die->child;
13161 while (child_die && child_die->tag)
13162 {
13163 if (child_die->tag == DW_TAG_imported_module)
13164 process_die (child_die, spec_cu);
13165 child_die = child_die->sibling;
13166 }
13167
13168 /* In some cases, GCC generates specification DIEs that
13169 themselves contain DW_AT_specification attributes. */
13170 spec_die = die_specification (spec_die, &spec_cu);
13171 }
13172 }
13173
13174 struct context_stack cstk = cu->get_builder ()->pop_context ();
13175 /* Make a block for the local symbols within. */
13176 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13177 cstk.static_link, lowpc, highpc);
13178
13179 /* For C++, set the block's scope. */
13180 if ((cu->per_cu->lang == language_cplus
13181 || cu->per_cu->lang == language_fortran
13182 || cu->per_cu->lang == language_d
13183 || cu->per_cu->lang == language_rust)
13184 && cu->processing_has_namespace_info)
13185 block_set_scope (block, determine_prefix (die, cu),
13186 &objfile->objfile_obstack);
13187
13188 /* If we have address ranges, record them. */
13189 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13190
13191 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13192
13193 /* Attach template arguments to function. */
13194 if (!template_args.empty ())
13195 {
13196 gdb_assert (templ_func != NULL);
13197
13198 templ_func->n_template_arguments = template_args.size ();
13199 templ_func->template_arguments
13200 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13201 templ_func->n_template_arguments);
13202 memcpy (templ_func->template_arguments,
13203 template_args.data (),
13204 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13205
13206 /* Make sure that the symtab is set on the new symbols. Even
13207 though they don't appear in this symtab directly, other parts
13208 of gdb assume that symbols do, and this is reasonably
13209 true. */
13210 for (symbol *sym : template_args)
13211 symbol_set_symtab (sym, symbol_symtab (templ_func));
13212 }
13213
13214 /* In C++, we can have functions nested inside functions (e.g., when
13215 a function declares a class that has methods). This means that
13216 when we finish processing a function scope, we may need to go
13217 back to building a containing block's symbol lists. */
13218 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13219 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13220
13221 /* If we've finished processing a top-level function, subsequent
13222 symbols go in the file symbol list. */
13223 if (cu->get_builder ()->outermost_context_p ())
13224 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13225 }
13226
13227 /* Process all the DIES contained within a lexical block scope. Start
13228 a new scope, process the dies, and then close the scope. */
13229
13230 static void
13231 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13232 {
13233 struct objfile *objfile = cu->per_objfile->objfile;
13234 struct gdbarch *gdbarch = objfile->arch ();
13235 CORE_ADDR lowpc, highpc;
13236 struct die_info *child_die;
13237 CORE_ADDR baseaddr;
13238
13239 baseaddr = objfile->text_section_offset ();
13240
13241 /* Ignore blocks with missing or invalid low and high pc attributes. */
13242 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13243 as multiple lexical blocks? Handling children in a sane way would
13244 be nasty. Might be easier to properly extend generic blocks to
13245 describe ranges. */
13246 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13247 {
13248 case PC_BOUNDS_NOT_PRESENT:
13249 /* DW_TAG_lexical_block has no attributes, process its children as if
13250 there was no wrapping by that DW_TAG_lexical_block.
13251 GCC does no longer produces such DWARF since GCC r224161. */
13252 for (child_die = die->child;
13253 child_die != NULL && child_die->tag;
13254 child_die = child_die->sibling)
13255 {
13256 /* We might already be processing this DIE. This can happen
13257 in an unusual circumstance -- where a subroutine A
13258 appears lexically in another subroutine B, but A actually
13259 inlines B. The recursion is broken here, rather than in
13260 inherit_abstract_dies, because it seems better to simply
13261 drop concrete children here. */
13262 if (!child_die->in_process)
13263 process_die (child_die, cu);
13264 }
13265 return;
13266 case PC_BOUNDS_INVALID:
13267 return;
13268 }
13269 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13270 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13271
13272 cu->get_builder ()->push_context (0, lowpc);
13273 if (die->child != NULL)
13274 {
13275 child_die = die->child;
13276 while (child_die && child_die->tag)
13277 {
13278 process_die (child_die, cu);
13279 child_die = child_die->sibling;
13280 }
13281 }
13282 inherit_abstract_dies (die, cu);
13283 struct context_stack cstk = cu->get_builder ()->pop_context ();
13284
13285 if (*cu->get_builder ()->get_local_symbols () != NULL
13286 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13287 {
13288 struct block *block
13289 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13290 cstk.start_addr, highpc);
13291
13292 /* Note that recording ranges after traversing children, as we
13293 do here, means that recording a parent's ranges entails
13294 walking across all its children's ranges as they appear in
13295 the address map, which is quadratic behavior.
13296
13297 It would be nicer to record the parent's ranges before
13298 traversing its children, simply overriding whatever you find
13299 there. But since we don't even decide whether to create a
13300 block until after we've traversed its children, that's hard
13301 to do. */
13302 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13303 }
13304 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13305 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13306 }
13307
13308 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13309
13310 static void
13311 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13312 {
13313 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13314 struct objfile *objfile = per_objfile->objfile;
13315 struct gdbarch *gdbarch = objfile->arch ();
13316 CORE_ADDR pc, baseaddr;
13317 struct attribute *attr;
13318 void **slot;
13319 int nparams;
13320 struct die_info *child_die;
13321
13322 baseaddr = objfile->text_section_offset ();
13323
13324 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13325 if (attr == NULL)
13326 {
13327 /* This was a pre-DWARF-5 GNU extension alias
13328 for DW_AT_call_return_pc. */
13329 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13330 }
13331 if (!attr)
13332 {
13333 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13334 "DIE %s [in module %s]"),
13335 sect_offset_str (die->sect_off), objfile_name (objfile));
13336 return;
13337 }
13338 pc = attr->as_address () + baseaddr;
13339 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13340 pc -= baseaddr;
13341
13342 if (cu->call_site_htab == NULL)
13343 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
13344 call_site::eq, NULL,
13345 &objfile->objfile_obstack,
13346 hashtab_obstack_allocate, NULL);
13347 struct call_site call_site_local (pc, nullptr, nullptr);
13348 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13349 if (*slot != NULL)
13350 {
13351 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13352 "DIE %s [in module %s]"),
13353 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13354 objfile_name (objfile));
13355 return;
13356 }
13357
13358 /* Count parameters at the caller. */
13359
13360 nparams = 0;
13361 for (child_die = die->child; child_die && child_die->tag;
13362 child_die = child_die->sibling)
13363 {
13364 if (child_die->tag != DW_TAG_call_site_parameter
13365 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13366 {
13367 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13368 "DW_TAG_call_site child DIE %s [in module %s]"),
13369 child_die->tag, sect_offset_str (child_die->sect_off),
13370 objfile_name (objfile));
13371 continue;
13372 }
13373
13374 nparams++;
13375 }
13376
13377 struct call_site *call_site
13378 = new (XOBNEWVAR (&objfile->objfile_obstack,
13379 struct call_site,
13380 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
13381 struct call_site (pc, cu->per_cu, per_objfile);
13382 *slot = call_site;
13383
13384 /* We never call the destructor of call_site, so we must ensure it is
13385 trivially destructible. */
13386 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
13387
13388 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13389 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13390 {
13391 struct die_info *func_die;
13392
13393 /* Skip also over DW_TAG_inlined_subroutine. */
13394 for (func_die = die->parent;
13395 func_die && func_die->tag != DW_TAG_subprogram
13396 && func_die->tag != DW_TAG_subroutine_type;
13397 func_die = func_die->parent);
13398
13399 /* DW_AT_call_all_calls is a superset
13400 of DW_AT_call_all_tail_calls. */
13401 if (func_die
13402 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13403 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13404 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13405 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13406 {
13407 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13408 not complete. But keep CALL_SITE for look ups via call_site_htab,
13409 both the initial caller containing the real return address PC and
13410 the final callee containing the current PC of a chain of tail
13411 calls do not need to have the tail call list complete. But any
13412 function candidate for a virtual tail call frame searched via
13413 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13414 determined unambiguously. */
13415 }
13416 else
13417 {
13418 struct type *func_type = NULL;
13419
13420 if (func_die)
13421 func_type = get_die_type (func_die, cu);
13422 if (func_type != NULL)
13423 {
13424 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13425
13426 /* Enlist this call site to the function. */
13427 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13428 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13429 }
13430 else
13431 complaint (_("Cannot find function owning DW_TAG_call_site "
13432 "DIE %s [in module %s]"),
13433 sect_offset_str (die->sect_off), objfile_name (objfile));
13434 }
13435 }
13436
13437 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13438 if (attr == NULL)
13439 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13440 if (attr == NULL)
13441 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13442 if (attr == NULL)
13443 {
13444 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13445 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13446 }
13447
13448 call_site->target.set_loc_dwarf_block (nullptr);
13449 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13450 /* Keep NULL DWARF_BLOCK. */;
13451 else if (attr->form_is_block ())
13452 {
13453 struct dwarf2_locexpr_baton *dlbaton;
13454 struct dwarf_block *block = attr->as_block ();
13455
13456 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13457 dlbaton->data = block->data;
13458 dlbaton->size = block->size;
13459 dlbaton->per_objfile = per_objfile;
13460 dlbaton->per_cu = cu->per_cu;
13461
13462 call_site->target.set_loc_dwarf_block (dlbaton);
13463 }
13464 else if (attr->form_is_ref ())
13465 {
13466 struct dwarf2_cu *target_cu = cu;
13467 struct die_info *target_die;
13468
13469 target_die = follow_die_ref (die, attr, &target_cu);
13470 gdb_assert (target_cu->per_objfile->objfile == objfile);
13471 if (die_is_declaration (target_die, target_cu))
13472 {
13473 const char *target_physname;
13474
13475 /* Prefer the mangled name; otherwise compute the demangled one. */
13476 target_physname = dw2_linkage_name (target_die, target_cu);
13477 if (target_physname == NULL)
13478 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13479 if (target_physname == NULL)
13480 complaint (_("DW_AT_call_target target DIE has invalid "
13481 "physname, for referencing DIE %s [in module %s]"),
13482 sect_offset_str (die->sect_off), objfile_name (objfile));
13483 else
13484 call_site->target.set_loc_physname (target_physname);
13485 }
13486 else
13487 {
13488 CORE_ADDR lowpc;
13489
13490 /* DW_AT_entry_pc should be preferred. */
13491 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13492 <= PC_BOUNDS_INVALID)
13493 complaint (_("DW_AT_call_target target DIE has invalid "
13494 "low pc, for referencing DIE %s [in module %s]"),
13495 sect_offset_str (die->sect_off), objfile_name (objfile));
13496 else
13497 {
13498 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
13499 - baseaddr);
13500 call_site->target.set_loc_physaddr (lowpc);
13501 }
13502 }
13503 }
13504 else
13505 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13506 "block nor reference, for DIE %s [in module %s]"),
13507 sect_offset_str (die->sect_off), objfile_name (objfile));
13508
13509 for (child_die = die->child;
13510 child_die && child_die->tag;
13511 child_die = child_die->sibling)
13512 {
13513 struct call_site_parameter *parameter;
13514 struct attribute *loc, *origin;
13515
13516 if (child_die->tag != DW_TAG_call_site_parameter
13517 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13518 {
13519 /* Already printed the complaint above. */
13520 continue;
13521 }
13522
13523 gdb_assert (call_site->parameter_count < nparams);
13524 parameter = &call_site->parameter[call_site->parameter_count];
13525
13526 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13527 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13528 register is contained in DW_AT_call_value. */
13529
13530 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13531 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13532 if (origin == NULL)
13533 {
13534 /* This was a pre-DWARF-5 GNU extension alias
13535 for DW_AT_call_parameter. */
13536 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13537 }
13538 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13539 {
13540 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13541
13542 sect_offset sect_off = origin->get_ref_die_offset ();
13543 if (!cu->header.offset_in_cu_p (sect_off))
13544 {
13545 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13546 binding can be done only inside one CU. Such referenced DIE
13547 therefore cannot be even moved to DW_TAG_partial_unit. */
13548 complaint (_("DW_AT_call_parameter offset is not in CU for "
13549 "DW_TAG_call_site child DIE %s [in module %s]"),
13550 sect_offset_str (child_die->sect_off),
13551 objfile_name (objfile));
13552 continue;
13553 }
13554 parameter->u.param_cu_off
13555 = (cu_offset) (sect_off - cu->header.sect_off);
13556 }
13557 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13558 {
13559 complaint (_("No DW_FORM_block* DW_AT_location for "
13560 "DW_TAG_call_site child DIE %s [in module %s]"),
13561 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13562 continue;
13563 }
13564 else
13565 {
13566 struct dwarf_block *block = loc->as_block ();
13567
13568 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13569 (block->data, &block->data[block->size]);
13570 if (parameter->u.dwarf_reg != -1)
13571 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13572 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13573 &block->data[block->size],
13574 &parameter->u.fb_offset))
13575 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13576 else
13577 {
13578 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13579 "for DW_FORM_block* DW_AT_location is supported for "
13580 "DW_TAG_call_site child DIE %s "
13581 "[in module %s]"),
13582 sect_offset_str (child_die->sect_off),
13583 objfile_name (objfile));
13584 continue;
13585 }
13586 }
13587
13588 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13589 if (attr == NULL)
13590 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13591 if (attr == NULL || !attr->form_is_block ())
13592 {
13593 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13594 "DW_TAG_call_site child DIE %s [in module %s]"),
13595 sect_offset_str (child_die->sect_off),
13596 objfile_name (objfile));
13597 continue;
13598 }
13599
13600 struct dwarf_block *block = attr->as_block ();
13601 parameter->value = block->data;
13602 parameter->value_size = block->size;
13603
13604 /* Parameters are not pre-cleared by memset above. */
13605 parameter->data_value = NULL;
13606 parameter->data_value_size = 0;
13607 call_site->parameter_count++;
13608
13609 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13610 if (attr == NULL)
13611 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13612 if (attr != nullptr)
13613 {
13614 if (!attr->form_is_block ())
13615 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13616 "DW_TAG_call_site child DIE %s [in module %s]"),
13617 sect_offset_str (child_die->sect_off),
13618 objfile_name (objfile));
13619 else
13620 {
13621 block = attr->as_block ();
13622 parameter->data_value = block->data;
13623 parameter->data_value_size = block->size;
13624 }
13625 }
13626 }
13627 }
13628
13629 /* Helper function for read_variable. If DIE represents a virtual
13630 table, then return the type of the concrete object that is
13631 associated with the virtual table. Otherwise, return NULL. */
13632
13633 static struct type *
13634 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13635 {
13636 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13637 if (attr == NULL)
13638 return NULL;
13639
13640 /* Find the type DIE. */
13641 struct die_info *type_die = NULL;
13642 struct dwarf2_cu *type_cu = cu;
13643
13644 if (attr->form_is_ref ())
13645 type_die = follow_die_ref (die, attr, &type_cu);
13646 if (type_die == NULL)
13647 return NULL;
13648
13649 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13650 return NULL;
13651 return die_containing_type (type_die, type_cu);
13652 }
13653
13654 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13655
13656 static void
13657 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13658 {
13659 struct rust_vtable_symbol *storage = NULL;
13660
13661 if (cu->per_cu->lang == language_rust)
13662 {
13663 struct type *containing_type = rust_containing_type (die, cu);
13664
13665 if (containing_type != NULL)
13666 {
13667 struct objfile *objfile = cu->per_objfile->objfile;
13668
13669 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13670 storage->concrete_type = containing_type;
13671 storage->subclass = SYMBOL_RUST_VTABLE;
13672 }
13673 }
13674
13675 struct symbol *res = new_symbol (die, NULL, cu, storage);
13676 struct attribute *abstract_origin
13677 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13678 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13679 if (res == NULL && loc && abstract_origin)
13680 {
13681 /* We have a variable without a name, but with a location and an abstract
13682 origin. This may be a concrete instance of an abstract variable
13683 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13684 later. */
13685 struct dwarf2_cu *origin_cu = cu;
13686 struct die_info *origin_die
13687 = follow_die_ref (die, abstract_origin, &origin_cu);
13688 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13689 per_objfile->per_bfd->abstract_to_concrete
13690 [origin_die->sect_off].push_back (die->sect_off);
13691 }
13692 }
13693
13694 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13695 reading .debug_rnglists.
13696 Callback's type should be:
13697 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13698 Return true if the attributes are present and valid, otherwise,
13699 return false. */
13700
13701 template <typename Callback>
13702 static bool
13703 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13704 dwarf_tag tag, Callback &&callback)
13705 {
13706 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13707 struct objfile *objfile = per_objfile->objfile;
13708 bfd *obfd = objfile->obfd;
13709 /* Base address selection entry. */
13710 gdb::optional<CORE_ADDR> base;
13711 const gdb_byte *buffer;
13712 bool overflow = false;
13713 ULONGEST addr_index;
13714 struct dwarf2_section_info *rnglists_section;
13715
13716 base = cu->base_address;
13717 rnglists_section = cu_debug_rnglists_section (cu, tag);
13718 rnglists_section->read (objfile);
13719
13720 if (offset >= rnglists_section->size)
13721 {
13722 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13723 offset);
13724 return false;
13725 }
13726 buffer = rnglists_section->buffer + offset;
13727
13728 while (1)
13729 {
13730 /* Initialize it due to a false compiler warning. */
13731 CORE_ADDR range_beginning = 0, range_end = 0;
13732 const gdb_byte *buf_end = (rnglists_section->buffer
13733 + rnglists_section->size);
13734 unsigned int bytes_read;
13735
13736 if (buffer == buf_end)
13737 {
13738 overflow = true;
13739 break;
13740 }
13741 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13742 switch (rlet)
13743 {
13744 case DW_RLE_end_of_list:
13745 break;
13746 case DW_RLE_base_address:
13747 if (buffer + cu->header.addr_size > buf_end)
13748 {
13749 overflow = true;
13750 break;
13751 }
13752 base = cu->header.read_address (obfd, buffer, &bytes_read);
13753 buffer += bytes_read;
13754 break;
13755 case DW_RLE_base_addressx:
13756 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13757 buffer += bytes_read;
13758 base = read_addr_index (cu, addr_index);
13759 break;
13760 case DW_RLE_start_length:
13761 if (buffer + cu->header.addr_size > buf_end)
13762 {
13763 overflow = true;
13764 break;
13765 }
13766 range_beginning = cu->header.read_address (obfd, buffer,
13767 &bytes_read);
13768 buffer += bytes_read;
13769 range_end = (range_beginning
13770 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13771 buffer += bytes_read;
13772 if (buffer > buf_end)
13773 {
13774 overflow = true;
13775 break;
13776 }
13777 break;
13778 case DW_RLE_startx_length:
13779 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13780 buffer += bytes_read;
13781 range_beginning = read_addr_index (cu, addr_index);
13782 if (buffer > buf_end)
13783 {
13784 overflow = true;
13785 break;
13786 }
13787 range_end = (range_beginning
13788 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13789 buffer += bytes_read;
13790 break;
13791 case DW_RLE_offset_pair:
13792 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13793 buffer += bytes_read;
13794 if (buffer > buf_end)
13795 {
13796 overflow = true;
13797 break;
13798 }
13799 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13800 buffer += bytes_read;
13801 if (buffer > buf_end)
13802 {
13803 overflow = true;
13804 break;
13805 }
13806 break;
13807 case DW_RLE_start_end:
13808 if (buffer + 2 * cu->header.addr_size > buf_end)
13809 {
13810 overflow = true;
13811 break;
13812 }
13813 range_beginning = cu->header.read_address (obfd, buffer,
13814 &bytes_read);
13815 buffer += bytes_read;
13816 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13817 buffer += bytes_read;
13818 break;
13819 case DW_RLE_startx_endx:
13820 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13821 buffer += bytes_read;
13822 range_beginning = read_addr_index (cu, addr_index);
13823 if (buffer > buf_end)
13824 {
13825 overflow = true;
13826 break;
13827 }
13828 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13829 buffer += bytes_read;
13830 range_end = read_addr_index (cu, addr_index);
13831 break;
13832 default:
13833 complaint (_("Invalid .debug_rnglists data (no base address)"));
13834 return false;
13835 }
13836 if (rlet == DW_RLE_end_of_list || overflow)
13837 break;
13838 if (rlet == DW_RLE_base_address)
13839 continue;
13840
13841 if (range_beginning > range_end)
13842 {
13843 /* Inverted range entries are invalid. */
13844 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13845 return false;
13846 }
13847
13848 /* Empty range entries have no effect. */
13849 if (range_beginning == range_end)
13850 continue;
13851
13852 /* Only DW_RLE_offset_pair needs the base address added. */
13853 if (rlet == DW_RLE_offset_pair)
13854 {
13855 if (!base.has_value ())
13856 {
13857 /* We have no valid base address for the DW_RLE_offset_pair. */
13858 complaint (_("Invalid .debug_rnglists data (no base address for "
13859 "DW_RLE_offset_pair)"));
13860 return false;
13861 }
13862
13863 range_beginning += *base;
13864 range_end += *base;
13865 }
13866
13867 /* A not-uncommon case of bad debug info.
13868 Don't pollute the addrmap with bad data. */
13869 if (range_beginning == 0
13870 && !per_objfile->per_bfd->has_section_at_zero)
13871 {
13872 complaint (_(".debug_rnglists entry has start address of zero"
13873 " [in module %s]"), objfile_name (objfile));
13874 continue;
13875 }
13876
13877 callback (range_beginning, range_end);
13878 }
13879
13880 if (overflow)
13881 {
13882 complaint (_("Offset %d is not terminated "
13883 "for DW_AT_ranges attribute"),
13884 offset);
13885 return false;
13886 }
13887
13888 return true;
13889 }
13890
13891 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13892 Callback's type should be:
13893 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13894 Return 1 if the attributes are present and valid, otherwise, return 0. */
13895
13896 template <typename Callback>
13897 static int
13898 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
13899 Callback &&callback)
13900 {
13901 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13902 struct objfile *objfile = per_objfile->objfile;
13903 struct comp_unit_head *cu_header = &cu->header;
13904 bfd *obfd = objfile->obfd;
13905 unsigned int addr_size = cu_header->addr_size;
13906 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13907 /* Base address selection entry. */
13908 gdb::optional<CORE_ADDR> base;
13909 unsigned int dummy;
13910 const gdb_byte *buffer;
13911
13912 if (cu_header->version >= 5)
13913 return dwarf2_rnglists_process (offset, cu, tag, callback);
13914
13915 base = cu->base_address;
13916
13917 per_objfile->per_bfd->ranges.read (objfile);
13918 if (offset >= per_objfile->per_bfd->ranges.size)
13919 {
13920 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13921 offset);
13922 return 0;
13923 }
13924 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13925
13926 while (1)
13927 {
13928 CORE_ADDR range_beginning, range_end;
13929
13930 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13931 buffer += addr_size;
13932 range_end = cu->header.read_address (obfd, buffer, &dummy);
13933 buffer += addr_size;
13934 offset += 2 * addr_size;
13935
13936 /* An end of list marker is a pair of zero addresses. */
13937 if (range_beginning == 0 && range_end == 0)
13938 /* Found the end of list entry. */
13939 break;
13940
13941 /* Each base address selection entry is a pair of 2 values.
13942 The first is the largest possible address, the second is
13943 the base address. Check for a base address here. */
13944 if ((range_beginning & mask) == mask)
13945 {
13946 /* If we found the largest possible address, then we already
13947 have the base address in range_end. */
13948 base = range_end;
13949 continue;
13950 }
13951
13952 if (!base.has_value ())
13953 {
13954 /* We have no valid base address for the ranges
13955 data. */
13956 complaint (_("Invalid .debug_ranges data (no base address)"));
13957 return 0;
13958 }
13959
13960 if (range_beginning > range_end)
13961 {
13962 /* Inverted range entries are invalid. */
13963 complaint (_("Invalid .debug_ranges data (inverted range)"));
13964 return 0;
13965 }
13966
13967 /* Empty range entries have no effect. */
13968 if (range_beginning == range_end)
13969 continue;
13970
13971 range_beginning += *base;
13972 range_end += *base;
13973
13974 /* A not-uncommon case of bad debug info.
13975 Don't pollute the addrmap with bad data. */
13976 if (range_beginning == 0
13977 && !per_objfile->per_bfd->has_section_at_zero)
13978 {
13979 complaint (_(".debug_ranges entry has start address of zero"
13980 " [in module %s]"), objfile_name (objfile));
13981 continue;
13982 }
13983
13984 callback (range_beginning, range_end);
13985 }
13986
13987 return 1;
13988 }
13989
13990 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13991 Return 1 if the attributes are present and valid, otherwise, return 0.
13992 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
13993
13994 static int
13995 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13996 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13997 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
13998 {
13999 struct objfile *objfile = cu->per_objfile->objfile;
14000 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14001 struct gdbarch *gdbarch = objfile->arch ();
14002 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14003 int low_set = 0;
14004 CORE_ADDR low = 0;
14005 CORE_ADDR high = 0;
14006 int retval;
14007
14008 retval = dwarf2_ranges_process (offset, cu, tag,
14009 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14010 {
14011 if (ranges_pst != NULL)
14012 {
14013 CORE_ADDR lowpc;
14014 CORE_ADDR highpc;
14015
14016 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14017 range_beginning + baseaddr)
14018 - baseaddr);
14019 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14020 range_end + baseaddr)
14021 - baseaddr);
14022 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14023 lowpc, highpc - 1, ranges_pst);
14024 }
14025
14026 /* FIXME: This is recording everything as a low-high
14027 segment of consecutive addresses. We should have a
14028 data structure for discontiguous block ranges
14029 instead. */
14030 if (! low_set)
14031 {
14032 low = range_beginning;
14033 high = range_end;
14034 low_set = 1;
14035 }
14036 else
14037 {
14038 if (range_beginning < low)
14039 low = range_beginning;
14040 if (range_end > high)
14041 high = range_end;
14042 }
14043 });
14044 if (!retval)
14045 return 0;
14046
14047 if (! low_set)
14048 /* If the first entry is an end-of-list marker, the range
14049 describes an empty scope, i.e. no instructions. */
14050 return 0;
14051
14052 if (low_return)
14053 *low_return = low;
14054 if (high_return)
14055 *high_return = high;
14056 return 1;
14057 }
14058
14059 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14060 definition for the return value. *LOWPC and *HIGHPC are set iff
14061 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14062
14063 static enum pc_bounds_kind
14064 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14065 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14066 dwarf2_psymtab *pst)
14067 {
14068 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14069 struct attribute *attr;
14070 struct attribute *attr_high;
14071 CORE_ADDR low = 0;
14072 CORE_ADDR high = 0;
14073 enum pc_bounds_kind ret;
14074
14075 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14076 if (attr_high)
14077 {
14078 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14079 if (attr != nullptr)
14080 {
14081 low = attr->as_address ();
14082 high = attr_high->as_address ();
14083 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14084 high += low;
14085 }
14086 else
14087 /* Found high w/o low attribute. */
14088 return PC_BOUNDS_INVALID;
14089
14090 /* Found consecutive range of addresses. */
14091 ret = PC_BOUNDS_HIGH_LOW;
14092 }
14093 else
14094 {
14095 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14096 if (attr != nullptr && attr->form_is_unsigned ())
14097 {
14098 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14099 on DWARF version). */
14100 ULONGEST ranges_offset = attr->as_unsigned ();
14101
14102 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14103 this value. */
14104 if (die->tag != DW_TAG_compile_unit)
14105 ranges_offset += cu->gnu_ranges_base;
14106
14107 /* Value of the DW_AT_ranges attribute is the offset in the
14108 .debug_ranges section. */
14109 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14110 die->tag))
14111 return PC_BOUNDS_INVALID;
14112 /* Found discontinuous range of addresses. */
14113 ret = PC_BOUNDS_RANGES;
14114 }
14115 else
14116 return PC_BOUNDS_NOT_PRESENT;
14117 }
14118
14119 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14120 if (high <= low)
14121 return PC_BOUNDS_INVALID;
14122
14123 /* When using the GNU linker, .gnu.linkonce. sections are used to
14124 eliminate duplicate copies of functions and vtables and such.
14125 The linker will arbitrarily choose one and discard the others.
14126 The AT_*_pc values for such functions refer to local labels in
14127 these sections. If the section from that file was discarded, the
14128 labels are not in the output, so the relocs get a value of 0.
14129 If this is a discarded function, mark the pc bounds as invalid,
14130 so that GDB will ignore it. */
14131 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14132 return PC_BOUNDS_INVALID;
14133
14134 *lowpc = low;
14135 if (highpc)
14136 *highpc = high;
14137 return ret;
14138 }
14139
14140 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14141 its low and high PC addresses. Do nothing if these addresses could not
14142 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14143 and HIGHPC to the high address if greater than HIGHPC. */
14144
14145 static void
14146 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14147 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14148 struct dwarf2_cu *cu)
14149 {
14150 CORE_ADDR low, high;
14151 struct die_info *child = die->child;
14152
14153 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14154 {
14155 *lowpc = std::min (*lowpc, low);
14156 *highpc = std::max (*highpc, high);
14157 }
14158
14159 /* If the language does not allow nested subprograms (either inside
14160 subprograms or lexical blocks), we're done. */
14161 if (cu->per_cu->lang != language_ada)
14162 return;
14163
14164 /* Check all the children of the given DIE. If it contains nested
14165 subprograms, then check their pc bounds. Likewise, we need to
14166 check lexical blocks as well, as they may also contain subprogram
14167 definitions. */
14168 while (child && child->tag)
14169 {
14170 if (child->tag == DW_TAG_subprogram
14171 || child->tag == DW_TAG_lexical_block)
14172 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14173 child = child->sibling;
14174 }
14175 }
14176
14177 /* Get the low and high pc's represented by the scope DIE, and store
14178 them in *LOWPC and *HIGHPC. If the correct values can't be
14179 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14180
14181 static void
14182 get_scope_pc_bounds (struct die_info *die,
14183 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14184 struct dwarf2_cu *cu)
14185 {
14186 CORE_ADDR best_low = (CORE_ADDR) -1;
14187 CORE_ADDR best_high = (CORE_ADDR) 0;
14188 CORE_ADDR current_low, current_high;
14189
14190 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14191 >= PC_BOUNDS_RANGES)
14192 {
14193 best_low = current_low;
14194 best_high = current_high;
14195 }
14196 else
14197 {
14198 struct die_info *child = die->child;
14199
14200 while (child && child->tag)
14201 {
14202 switch (child->tag) {
14203 case DW_TAG_subprogram:
14204 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14205 break;
14206 case DW_TAG_namespace:
14207 case DW_TAG_module:
14208 /* FIXME: carlton/2004-01-16: Should we do this for
14209 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14210 that current GCC's always emit the DIEs corresponding
14211 to definitions of methods of classes as children of a
14212 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14213 the DIEs giving the declarations, which could be
14214 anywhere). But I don't see any reason why the
14215 standards says that they have to be there. */
14216 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14217
14218 if (current_low != ((CORE_ADDR) -1))
14219 {
14220 best_low = std::min (best_low, current_low);
14221 best_high = std::max (best_high, current_high);
14222 }
14223 break;
14224 default:
14225 /* Ignore. */
14226 break;
14227 }
14228
14229 child = child->sibling;
14230 }
14231 }
14232
14233 *lowpc = best_low;
14234 *highpc = best_high;
14235 }
14236
14237 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14238 in DIE. */
14239
14240 static void
14241 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14242 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14243 {
14244 struct objfile *objfile = cu->per_objfile->objfile;
14245 struct gdbarch *gdbarch = objfile->arch ();
14246 struct attribute *attr;
14247 struct attribute *attr_high;
14248
14249 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14250 if (attr_high)
14251 {
14252 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14253 if (attr != nullptr)
14254 {
14255 CORE_ADDR low = attr->as_address ();
14256 CORE_ADDR high = attr_high->as_address ();
14257
14258 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14259 high += low;
14260
14261 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14262 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14263 cu->get_builder ()->record_block_range (block, low, high - 1);
14264 }
14265 }
14266
14267 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14268 if (attr != nullptr && attr->form_is_unsigned ())
14269 {
14270 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14271 on DWARF version). */
14272 ULONGEST ranges_offset = attr->as_unsigned ();
14273
14274 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14275 this value. */
14276 if (die->tag != DW_TAG_compile_unit)
14277 ranges_offset += cu->gnu_ranges_base;
14278
14279 std::vector<blockrange> blockvec;
14280 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14281 [&] (CORE_ADDR start, CORE_ADDR end)
14282 {
14283 start += baseaddr;
14284 end += baseaddr;
14285 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14286 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14287 cu->get_builder ()->record_block_range (block, start, end - 1);
14288 blockvec.emplace_back (start, end);
14289 });
14290
14291 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14292 }
14293 }
14294
14295 /* Check whether the producer field indicates either of GCC < 4.6, or the
14296 Intel C/C++ compiler, and cache the result in CU. */
14297
14298 static void
14299 check_producer (struct dwarf2_cu *cu)
14300 {
14301 int major, minor;
14302
14303 if (cu->producer == NULL)
14304 {
14305 /* For unknown compilers expect their behavior is DWARF version
14306 compliant.
14307
14308 GCC started to support .debug_types sections by -gdwarf-4 since
14309 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14310 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14311 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14312 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14313 }
14314 else if (producer_is_gcc (cu->producer, &major, &minor))
14315 {
14316 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14317 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14318 }
14319 else if (producer_is_icc (cu->producer, &major, &minor))
14320 {
14321 cu->producer_is_icc = true;
14322 cu->producer_is_icc_lt_14 = major < 14;
14323 }
14324 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14325 cu->producer_is_codewarrior = true;
14326 else
14327 {
14328 /* For other non-GCC compilers, expect their behavior is DWARF version
14329 compliant. */
14330 }
14331
14332 cu->checked_producer = true;
14333 }
14334
14335 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14336 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14337 during 4.6.0 experimental. */
14338
14339 static bool
14340 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14341 {
14342 if (!cu->checked_producer)
14343 check_producer (cu);
14344
14345 return cu->producer_is_gxx_lt_4_6;
14346 }
14347
14348
14349 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14350 with incorrect is_stmt attributes. */
14351
14352 static bool
14353 producer_is_codewarrior (struct dwarf2_cu *cu)
14354 {
14355 if (!cu->checked_producer)
14356 check_producer (cu);
14357
14358 return cu->producer_is_codewarrior;
14359 }
14360
14361 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14362 If that attribute is not available, return the appropriate
14363 default. */
14364
14365 static enum dwarf_access_attribute
14366 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14367 {
14368 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14369 if (attr != nullptr)
14370 {
14371 LONGEST value = attr->constant_value (-1);
14372 if (value == DW_ACCESS_public
14373 || value == DW_ACCESS_protected
14374 || value == DW_ACCESS_private)
14375 return (dwarf_access_attribute) value;
14376 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14377 plongest (value));
14378 }
14379
14380 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14381 {
14382 /* The default DWARF 2 accessibility for members is public, the default
14383 accessibility for inheritance is private. */
14384
14385 if (die->tag != DW_TAG_inheritance)
14386 return DW_ACCESS_public;
14387 else
14388 return DW_ACCESS_private;
14389 }
14390 else
14391 {
14392 /* DWARF 3+ defines the default accessibility a different way. The same
14393 rules apply now for DW_TAG_inheritance as for the members and it only
14394 depends on the container kind. */
14395
14396 if (die->parent->tag == DW_TAG_class_type)
14397 return DW_ACCESS_private;
14398 else
14399 return DW_ACCESS_public;
14400 }
14401 }
14402
14403 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
14404 *OFFSET to the byte offset. If the attribute was not found return
14405 0, otherwise return 1. If it was found but could not properly be
14406 handled, set *OFFSET to 0. */
14407
14408 static int
14409 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14410 LONGEST *offset)
14411 {
14412 struct attribute *attr;
14413
14414 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14415 if (attr != NULL)
14416 {
14417 *offset = 0;
14418
14419 /* Note that we do not check for a section offset first here.
14420 This is because DW_AT_data_member_location is new in DWARF 4,
14421 so if we see it, we can assume that a constant form is really
14422 a constant and not a section offset. */
14423 if (attr->form_is_constant ())
14424 *offset = attr->constant_value (0);
14425 else if (attr->form_is_section_offset ())
14426 dwarf2_complex_location_expr_complaint ();
14427 else if (attr->form_is_block ())
14428 *offset = decode_locdesc (attr->as_block (), cu);
14429 else
14430 dwarf2_complex_location_expr_complaint ();
14431
14432 return 1;
14433 }
14434 else
14435 {
14436 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14437 if (attr != nullptr)
14438 {
14439 *offset = attr->constant_value (0);
14440 return 1;
14441 }
14442 }
14443
14444 return 0;
14445 }
14446
14447 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
14448 store the results in FIELD. */
14449
14450 static void
14451 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
14452 struct field *field)
14453 {
14454 struct attribute *attr;
14455
14456 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14457 if (attr != NULL)
14458 {
14459 if (attr->form_is_constant ())
14460 {
14461 LONGEST offset = attr->constant_value (0);
14462 field->set_loc_bitpos (offset * bits_per_byte);
14463 }
14464 else if (attr->form_is_section_offset ())
14465 dwarf2_complex_location_expr_complaint ();
14466 else if (attr->form_is_block ())
14467 {
14468 bool handled;
14469 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14470 if (handled)
14471 field->set_loc_bitpos (offset * bits_per_byte);
14472 else
14473 {
14474 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14475 struct objfile *objfile = per_objfile->objfile;
14476 struct dwarf2_locexpr_baton *dlbaton
14477 = XOBNEW (&objfile->objfile_obstack,
14478 struct dwarf2_locexpr_baton);
14479 dlbaton->data = attr->as_block ()->data;
14480 dlbaton->size = attr->as_block ()->size;
14481 /* When using this baton, we want to compute the address
14482 of the field, not the value. This is why
14483 is_reference is set to false here. */
14484 dlbaton->is_reference = false;
14485 dlbaton->per_objfile = per_objfile;
14486 dlbaton->per_cu = cu->per_cu;
14487
14488 field->set_loc_dwarf_block (dlbaton);
14489 }
14490 }
14491 else
14492 dwarf2_complex_location_expr_complaint ();
14493 }
14494 else
14495 {
14496 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14497 if (attr != nullptr)
14498 field->set_loc_bitpos (attr->constant_value (0));
14499 }
14500 }
14501
14502 /* Add an aggregate field to the field list. */
14503
14504 static void
14505 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14506 struct dwarf2_cu *cu)
14507 {
14508 struct objfile *objfile = cu->per_objfile->objfile;
14509 struct gdbarch *gdbarch = objfile->arch ();
14510 struct nextfield *new_field;
14511 struct attribute *attr;
14512 struct field *fp;
14513 const char *fieldname = "";
14514
14515 if (die->tag == DW_TAG_inheritance)
14516 {
14517 fip->baseclasses.emplace_back ();
14518 new_field = &fip->baseclasses.back ();
14519 }
14520 else
14521 {
14522 fip->fields.emplace_back ();
14523 new_field = &fip->fields.back ();
14524 }
14525
14526 new_field->offset = die->sect_off;
14527
14528 new_field->accessibility = dwarf2_access_attribute (die, cu);
14529 if (new_field->accessibility != DW_ACCESS_public)
14530 fip->non_public_fields = true;
14531
14532 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14533 if (attr != nullptr)
14534 new_field->virtuality = attr->as_virtuality ();
14535 else
14536 new_field->virtuality = DW_VIRTUALITY_none;
14537
14538 fp = &new_field->field;
14539
14540 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14541 {
14542 /* Data member other than a C++ static data member. */
14543
14544 /* Get type of field. */
14545 fp->set_type (die_type (die, cu));
14546
14547 fp->set_loc_bitpos (0);
14548
14549 /* Get bit size of field (zero if none). */
14550 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14551 if (attr != nullptr)
14552 {
14553 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14554 }
14555 else
14556 {
14557 FIELD_BITSIZE (*fp) = 0;
14558 }
14559
14560 /* Get bit offset of field. */
14561 handle_member_location (die, cu, fp);
14562 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14563 if (attr != nullptr && attr->form_is_constant ())
14564 {
14565 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14566 {
14567 /* For big endian bits, the DW_AT_bit_offset gives the
14568 additional bit offset from the MSB of the containing
14569 anonymous object to the MSB of the field. We don't
14570 have to do anything special since we don't need to
14571 know the size of the anonymous object. */
14572 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
14573 }
14574 else
14575 {
14576 /* For little endian bits, compute the bit offset to the
14577 MSB of the anonymous object, subtract off the number of
14578 bits from the MSB of the field to the MSB of the
14579 object, and then subtract off the number of bits of
14580 the field itself. The result is the bit offset of
14581 the LSB of the field. */
14582 int anonymous_size;
14583 int bit_offset = attr->constant_value (0);
14584
14585 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14586 if (attr != nullptr && attr->form_is_constant ())
14587 {
14588 /* The size of the anonymous object containing
14589 the bit field is explicit, so use the
14590 indicated size (in bytes). */
14591 anonymous_size = attr->constant_value (0);
14592 }
14593 else
14594 {
14595 /* The size of the anonymous object containing
14596 the bit field must be inferred from the type
14597 attribute of the data member containing the
14598 bit field. */
14599 anonymous_size = TYPE_LENGTH (fp->type ());
14600 }
14601 fp->set_loc_bitpos (fp->loc_bitpos ()
14602 + anonymous_size * bits_per_byte
14603 - bit_offset - FIELD_BITSIZE (*fp));
14604 }
14605 }
14606
14607 /* Get name of field. */
14608 fieldname = dwarf2_name (die, cu);
14609 if (fieldname == NULL)
14610 fieldname = "";
14611
14612 /* The name is already allocated along with this objfile, so we don't
14613 need to duplicate it for the type. */
14614 fp->set_name (fieldname);
14615
14616 /* Change accessibility for artificial fields (e.g. virtual table
14617 pointer or virtual base class pointer) to private. */
14618 if (dwarf2_attr (die, DW_AT_artificial, cu))
14619 {
14620 FIELD_ARTIFICIAL (*fp) = 1;
14621 new_field->accessibility = DW_ACCESS_private;
14622 fip->non_public_fields = true;
14623 }
14624 }
14625 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14626 {
14627 /* C++ static member. */
14628
14629 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14630 is a declaration, but all versions of G++ as of this writing
14631 (so through at least 3.2.1) incorrectly generate
14632 DW_TAG_variable tags. */
14633
14634 const char *physname;
14635
14636 /* Get name of field. */
14637 fieldname = dwarf2_name (die, cu);
14638 if (fieldname == NULL)
14639 return;
14640
14641 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14642 if (attr
14643 /* Only create a symbol if this is an external value.
14644 new_symbol checks this and puts the value in the global symbol
14645 table, which we want. If it is not external, new_symbol
14646 will try to put the value in cu->list_in_scope which is wrong. */
14647 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14648 {
14649 /* A static const member, not much different than an enum as far as
14650 we're concerned, except that we can support more types. */
14651 new_symbol (die, NULL, cu);
14652 }
14653
14654 /* Get physical name. */
14655 physname = dwarf2_physname (fieldname, die, cu);
14656
14657 /* The name is already allocated along with this objfile, so we don't
14658 need to duplicate it for the type. */
14659 fp->set_loc_physname (physname ? physname : "");
14660 fp->set_type (die_type (die, cu));
14661 fp->set_name (fieldname);
14662 }
14663 else if (die->tag == DW_TAG_inheritance)
14664 {
14665 /* C++ base class field. */
14666 handle_member_location (die, cu, fp);
14667 FIELD_BITSIZE (*fp) = 0;
14668 fp->set_type (die_type (die, cu));
14669 fp->set_name (fp->type ()->name ());
14670 }
14671 else
14672 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14673 }
14674
14675 /* Can the type given by DIE define another type? */
14676
14677 static bool
14678 type_can_define_types (const struct die_info *die)
14679 {
14680 switch (die->tag)
14681 {
14682 case DW_TAG_typedef:
14683 case DW_TAG_class_type:
14684 case DW_TAG_structure_type:
14685 case DW_TAG_union_type:
14686 case DW_TAG_enumeration_type:
14687 return true;
14688
14689 default:
14690 return false;
14691 }
14692 }
14693
14694 /* Add a type definition defined in the scope of the FIP's class. */
14695
14696 static void
14697 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14698 struct dwarf2_cu *cu)
14699 {
14700 struct decl_field fp;
14701 memset (&fp, 0, sizeof (fp));
14702
14703 gdb_assert (type_can_define_types (die));
14704
14705 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14706 fp.name = dwarf2_name (die, cu);
14707 fp.type = read_type_die (die, cu);
14708
14709 /* Save accessibility. */
14710 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14711 switch (accessibility)
14712 {
14713 case DW_ACCESS_public:
14714 /* The assumed value if neither private nor protected. */
14715 break;
14716 case DW_ACCESS_private:
14717 fp.is_private = 1;
14718 break;
14719 case DW_ACCESS_protected:
14720 fp.is_protected = 1;
14721 break;
14722 }
14723
14724 if (die->tag == DW_TAG_typedef)
14725 fip->typedef_field_list.push_back (fp);
14726 else
14727 fip->nested_types_list.push_back (fp);
14728 }
14729
14730 /* A convenience typedef that's used when finding the discriminant
14731 field for a variant part. */
14732 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14733 offset_map_type;
14734
14735 /* Compute the discriminant range for a given variant. OBSTACK is
14736 where the results will be stored. VARIANT is the variant to
14737 process. IS_UNSIGNED indicates whether the discriminant is signed
14738 or unsigned. */
14739
14740 static const gdb::array_view<discriminant_range>
14741 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14742 bool is_unsigned)
14743 {
14744 std::vector<discriminant_range> ranges;
14745
14746 if (variant.default_branch)
14747 return {};
14748
14749 if (variant.discr_list_data == nullptr)
14750 {
14751 discriminant_range r
14752 = {variant.discriminant_value, variant.discriminant_value};
14753 ranges.push_back (r);
14754 }
14755 else
14756 {
14757 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14758 variant.discr_list_data->size);
14759 while (!data.empty ())
14760 {
14761 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14762 {
14763 complaint (_("invalid discriminant marker: %d"), data[0]);
14764 break;
14765 }
14766 bool is_range = data[0] == DW_DSC_range;
14767 data = data.slice (1);
14768
14769 ULONGEST low, high;
14770 unsigned int bytes_read;
14771
14772 if (data.empty ())
14773 {
14774 complaint (_("DW_AT_discr_list missing low value"));
14775 break;
14776 }
14777 if (is_unsigned)
14778 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14779 else
14780 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14781 &bytes_read);
14782 data = data.slice (bytes_read);
14783
14784 if (is_range)
14785 {
14786 if (data.empty ())
14787 {
14788 complaint (_("DW_AT_discr_list missing high value"));
14789 break;
14790 }
14791 if (is_unsigned)
14792 high = read_unsigned_leb128 (nullptr, data.data (),
14793 &bytes_read);
14794 else
14795 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14796 &bytes_read);
14797 data = data.slice (bytes_read);
14798 }
14799 else
14800 high = low;
14801
14802 ranges.push_back ({ low, high });
14803 }
14804 }
14805
14806 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14807 ranges.size ());
14808 std::copy (ranges.begin (), ranges.end (), result);
14809 return gdb::array_view<discriminant_range> (result, ranges.size ());
14810 }
14811
14812 static const gdb::array_view<variant_part> create_variant_parts
14813 (struct obstack *obstack,
14814 const offset_map_type &offset_map,
14815 struct field_info *fi,
14816 const std::vector<variant_part_builder> &variant_parts);
14817
14818 /* Fill in a "struct variant" for a given variant field. RESULT is
14819 the variant to fill in. OBSTACK is where any needed allocations
14820 will be done. OFFSET_MAP holds the mapping from section offsets to
14821 fields for the type. FI describes the fields of the type we're
14822 processing. FIELD is the variant field we're converting. */
14823
14824 static void
14825 create_one_variant (variant &result, struct obstack *obstack,
14826 const offset_map_type &offset_map,
14827 struct field_info *fi, const variant_field &field)
14828 {
14829 result.discriminants = convert_variant_range (obstack, field, false);
14830 result.first_field = field.first_field + fi->baseclasses.size ();
14831 result.last_field = field.last_field + fi->baseclasses.size ();
14832 result.parts = create_variant_parts (obstack, offset_map, fi,
14833 field.variant_parts);
14834 }
14835
14836 /* Fill in a "struct variant_part" for a given variant part. RESULT
14837 is the variant part to fill in. OBSTACK is where any needed
14838 allocations will be done. OFFSET_MAP holds the mapping from
14839 section offsets to fields for the type. FI describes the fields of
14840 the type we're processing. BUILDER is the variant part to be
14841 converted. */
14842
14843 static void
14844 create_one_variant_part (variant_part &result,
14845 struct obstack *obstack,
14846 const offset_map_type &offset_map,
14847 struct field_info *fi,
14848 const variant_part_builder &builder)
14849 {
14850 auto iter = offset_map.find (builder.discriminant_offset);
14851 if (iter == offset_map.end ())
14852 {
14853 result.discriminant_index = -1;
14854 /* Doesn't matter. */
14855 result.is_unsigned = false;
14856 }
14857 else
14858 {
14859 result.discriminant_index = iter->second;
14860 result.is_unsigned
14861 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
14862 }
14863
14864 size_t n = builder.variants.size ();
14865 variant *output = new (obstack) variant[n];
14866 for (size_t i = 0; i < n; ++i)
14867 create_one_variant (output[i], obstack, offset_map, fi,
14868 builder.variants[i]);
14869
14870 result.variants = gdb::array_view<variant> (output, n);
14871 }
14872
14873 /* Create a vector of variant parts that can be attached to a type.
14874 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14875 holds the mapping from section offsets to fields for the type. FI
14876 describes the fields of the type we're processing. VARIANT_PARTS
14877 is the vector to convert. */
14878
14879 static const gdb::array_view<variant_part>
14880 create_variant_parts (struct obstack *obstack,
14881 const offset_map_type &offset_map,
14882 struct field_info *fi,
14883 const std::vector<variant_part_builder> &variant_parts)
14884 {
14885 if (variant_parts.empty ())
14886 return {};
14887
14888 size_t n = variant_parts.size ();
14889 variant_part *result = new (obstack) variant_part[n];
14890 for (size_t i = 0; i < n; ++i)
14891 create_one_variant_part (result[i], obstack, offset_map, fi,
14892 variant_parts[i]);
14893
14894 return gdb::array_view<variant_part> (result, n);
14895 }
14896
14897 /* Compute the variant part vector for FIP, attaching it to TYPE when
14898 done. */
14899
14900 static void
14901 add_variant_property (struct field_info *fip, struct type *type,
14902 struct dwarf2_cu *cu)
14903 {
14904 /* Map section offsets of fields to their field index. Note the
14905 field index here does not take the number of baseclasses into
14906 account. */
14907 offset_map_type offset_map;
14908 for (int i = 0; i < fip->fields.size (); ++i)
14909 offset_map[fip->fields[i].offset] = i;
14910
14911 struct objfile *objfile = cu->per_objfile->objfile;
14912 gdb::array_view<const variant_part> parts
14913 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14914 fip->variant_parts);
14915
14916 struct dynamic_prop prop;
14917 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14918 obstack_copy (&objfile->objfile_obstack, &parts,
14919 sizeof (parts)));
14920
14921 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14922 }
14923
14924 /* Create the vector of fields, and attach it to the type. */
14925
14926 static void
14927 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14928 struct dwarf2_cu *cu)
14929 {
14930 int nfields = fip->nfields ();
14931
14932 /* Record the field count, allocate space for the array of fields,
14933 and create blank accessibility bitfields if necessary. */
14934 type->set_num_fields (nfields);
14935 type->set_fields
14936 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14937
14938 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
14939 {
14940 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14941
14942 TYPE_FIELD_PRIVATE_BITS (type) =
14943 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14944 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14945
14946 TYPE_FIELD_PROTECTED_BITS (type) =
14947 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14948 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14949
14950 TYPE_FIELD_IGNORE_BITS (type) =
14951 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14952 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14953 }
14954
14955 /* If the type has baseclasses, allocate and clear a bit vector for
14956 TYPE_FIELD_VIRTUAL_BITS. */
14957 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
14958 {
14959 int num_bytes = B_BYTES (fip->baseclasses.size ());
14960 unsigned char *pointer;
14961
14962 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14963 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14964 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14965 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14966 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14967 }
14968
14969 if (!fip->variant_parts.empty ())
14970 add_variant_property (fip, type, cu);
14971
14972 /* Copy the saved-up fields into the field vector. */
14973 for (int i = 0; i < nfields; ++i)
14974 {
14975 struct nextfield &field
14976 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14977 : fip->fields[i - fip->baseclasses.size ()]);
14978
14979 type->field (i) = field.field;
14980 switch (field.accessibility)
14981 {
14982 case DW_ACCESS_private:
14983 if (cu->per_cu->lang != language_ada)
14984 SET_TYPE_FIELD_PRIVATE (type, i);
14985 break;
14986
14987 case DW_ACCESS_protected:
14988 if (cu->per_cu->lang != language_ada)
14989 SET_TYPE_FIELD_PROTECTED (type, i);
14990 break;
14991
14992 case DW_ACCESS_public:
14993 break;
14994
14995 default:
14996 /* Unknown accessibility. Complain and treat it as public. */
14997 {
14998 complaint (_("unsupported accessibility %d"),
14999 field.accessibility);
15000 }
15001 break;
15002 }
15003 if (i < fip->baseclasses.size ())
15004 {
15005 switch (field.virtuality)
15006 {
15007 case DW_VIRTUALITY_virtual:
15008 case DW_VIRTUALITY_pure_virtual:
15009 if (cu->per_cu->lang == language_ada)
15010 error (_("unexpected virtuality in component of Ada type"));
15011 SET_TYPE_FIELD_VIRTUAL (type, i);
15012 break;
15013 }
15014 }
15015 }
15016 }
15017
15018 /* Return true if this member function is a constructor, false
15019 otherwise. */
15020
15021 static int
15022 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15023 {
15024 const char *fieldname;
15025 const char *type_name;
15026 int len;
15027
15028 if (die->parent == NULL)
15029 return 0;
15030
15031 if (die->parent->tag != DW_TAG_structure_type
15032 && die->parent->tag != DW_TAG_union_type
15033 && die->parent->tag != DW_TAG_class_type)
15034 return 0;
15035
15036 fieldname = dwarf2_name (die, cu);
15037 type_name = dwarf2_name (die->parent, cu);
15038 if (fieldname == NULL || type_name == NULL)
15039 return 0;
15040
15041 len = strlen (fieldname);
15042 return (strncmp (fieldname, type_name, len) == 0
15043 && (type_name[len] == '\0' || type_name[len] == '<'));
15044 }
15045
15046 /* Add a member function to the proper fieldlist. */
15047
15048 static void
15049 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15050 struct type *type, struct dwarf2_cu *cu)
15051 {
15052 struct objfile *objfile = cu->per_objfile->objfile;
15053 struct attribute *attr;
15054 int i;
15055 struct fnfieldlist *flp = nullptr;
15056 struct fn_field *fnp;
15057 const char *fieldname;
15058 struct type *this_type;
15059
15060 if (cu->per_cu->lang == language_ada)
15061 error (_("unexpected member function in Ada type"));
15062
15063 /* Get name of member function. */
15064 fieldname = dwarf2_name (die, cu);
15065 if (fieldname == NULL)
15066 return;
15067
15068 /* Look up member function name in fieldlist. */
15069 for (i = 0; i < fip->fnfieldlists.size (); i++)
15070 {
15071 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15072 {
15073 flp = &fip->fnfieldlists[i];
15074 break;
15075 }
15076 }
15077
15078 /* Create a new fnfieldlist if necessary. */
15079 if (flp == nullptr)
15080 {
15081 fip->fnfieldlists.emplace_back ();
15082 flp = &fip->fnfieldlists.back ();
15083 flp->name = fieldname;
15084 i = fip->fnfieldlists.size () - 1;
15085 }
15086
15087 /* Create a new member function field and add it to the vector of
15088 fnfieldlists. */
15089 flp->fnfields.emplace_back ();
15090 fnp = &flp->fnfields.back ();
15091
15092 /* Delay processing of the physname until later. */
15093 if (cu->per_cu->lang == language_cplus)
15094 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15095 die, cu);
15096 else
15097 {
15098 const char *physname = dwarf2_physname (fieldname, die, cu);
15099 fnp->physname = physname ? physname : "";
15100 }
15101
15102 fnp->type = alloc_type (objfile);
15103 this_type = read_type_die (die, cu);
15104 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15105 {
15106 int nparams = this_type->num_fields ();
15107
15108 /* TYPE is the domain of this method, and THIS_TYPE is the type
15109 of the method itself (TYPE_CODE_METHOD). */
15110 smash_to_method_type (fnp->type, type,
15111 TYPE_TARGET_TYPE (this_type),
15112 this_type->fields (),
15113 this_type->num_fields (),
15114 this_type->has_varargs ());
15115
15116 /* Handle static member functions.
15117 Dwarf2 has no clean way to discern C++ static and non-static
15118 member functions. G++ helps GDB by marking the first
15119 parameter for non-static member functions (which is the this
15120 pointer) as artificial. We obtain this information from
15121 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15122 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15123 fnp->voffset = VOFFSET_STATIC;
15124 }
15125 else
15126 complaint (_("member function type missing for '%s'"),
15127 dwarf2_full_name (fieldname, die, cu));
15128
15129 /* Get fcontext from DW_AT_containing_type if present. */
15130 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15131 fnp->fcontext = die_containing_type (die, cu);
15132
15133 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15134 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15135
15136 /* Get accessibility. */
15137 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15138 switch (accessibility)
15139 {
15140 case DW_ACCESS_private:
15141 fnp->is_private = 1;
15142 break;
15143 case DW_ACCESS_protected:
15144 fnp->is_protected = 1;
15145 break;
15146 }
15147
15148 /* Check for artificial methods. */
15149 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15150 if (attr && attr->as_boolean ())
15151 fnp->is_artificial = 1;
15152
15153 /* Check for defaulted methods. */
15154 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15155 if (attr != nullptr)
15156 fnp->defaulted = attr->defaulted ();
15157
15158 /* Check for deleted methods. */
15159 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15160 if (attr != nullptr && attr->as_boolean ())
15161 fnp->is_deleted = 1;
15162
15163 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15164
15165 /* Get index in virtual function table if it is a virtual member
15166 function. For older versions of GCC, this is an offset in the
15167 appropriate virtual table, as specified by DW_AT_containing_type.
15168 For everyone else, it is an expression to be evaluated relative
15169 to the object address. */
15170
15171 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15172 if (attr != nullptr)
15173 {
15174 if (attr->form_is_block () && attr->as_block ()->size > 0)
15175 {
15176 struct dwarf_block *block = attr->as_block ();
15177
15178 if (block->data[0] == DW_OP_constu)
15179 {
15180 /* Old-style GCC. */
15181 fnp->voffset = decode_locdesc (block, cu) + 2;
15182 }
15183 else if (block->data[0] == DW_OP_deref
15184 || (block->size > 1
15185 && block->data[0] == DW_OP_deref_size
15186 && block->data[1] == cu->header.addr_size))
15187 {
15188 fnp->voffset = decode_locdesc (block, cu);
15189 if ((fnp->voffset % cu->header.addr_size) != 0)
15190 dwarf2_complex_location_expr_complaint ();
15191 else
15192 fnp->voffset /= cu->header.addr_size;
15193 fnp->voffset += 2;
15194 }
15195 else
15196 dwarf2_complex_location_expr_complaint ();
15197
15198 if (!fnp->fcontext)
15199 {
15200 /* If there is no `this' field and no DW_AT_containing_type,
15201 we cannot actually find a base class context for the
15202 vtable! */
15203 if (this_type->num_fields () == 0
15204 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15205 {
15206 complaint (_("cannot determine context for virtual member "
15207 "function \"%s\" (offset %s)"),
15208 fieldname, sect_offset_str (die->sect_off));
15209 }
15210 else
15211 {
15212 fnp->fcontext
15213 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15214 }
15215 }
15216 }
15217 else if (attr->form_is_section_offset ())
15218 {
15219 dwarf2_complex_location_expr_complaint ();
15220 }
15221 else
15222 {
15223 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15224 fieldname);
15225 }
15226 }
15227 else
15228 {
15229 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15230 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15231 {
15232 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15233 complaint (_("Member function \"%s\" (offset %s) is virtual "
15234 "but the vtable offset is not specified"),
15235 fieldname, sect_offset_str (die->sect_off));
15236 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15237 TYPE_CPLUS_DYNAMIC (type) = 1;
15238 }
15239 }
15240 }
15241
15242 /* Create the vector of member function fields, and attach it to the type. */
15243
15244 static void
15245 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15246 struct dwarf2_cu *cu)
15247 {
15248 if (cu->per_cu->lang == language_ada)
15249 error (_("unexpected member functions in Ada type"));
15250
15251 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15252 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15253 TYPE_ALLOC (type,
15254 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15255
15256 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15257 {
15258 struct fnfieldlist &nf = fip->fnfieldlists[i];
15259 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15260
15261 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15262 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15263 fn_flp->fn_fields = (struct fn_field *)
15264 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15265
15266 for (int k = 0; k < nf.fnfields.size (); ++k)
15267 fn_flp->fn_fields[k] = nf.fnfields[k];
15268 }
15269
15270 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15271 }
15272
15273 /* Returns non-zero if NAME is the name of a vtable member in CU's
15274 language, zero otherwise. */
15275 static int
15276 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15277 {
15278 static const char vptr[] = "_vptr";
15279
15280 /* Look for the C++ form of the vtable. */
15281 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15282 return 1;
15283
15284 return 0;
15285 }
15286
15287 /* GCC outputs unnamed structures that are really pointers to member
15288 functions, with the ABI-specified layout. If TYPE describes
15289 such a structure, smash it into a member function type.
15290
15291 GCC shouldn't do this; it should just output pointer to member DIEs.
15292 This is GCC PR debug/28767. */
15293
15294 static void
15295 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15296 {
15297 struct type *pfn_type, *self_type, *new_type;
15298
15299 /* Check for a structure with no name and two children. */
15300 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15301 return;
15302
15303 /* Check for __pfn and __delta members. */
15304 if (type->field (0).name () == NULL
15305 || strcmp (type->field (0).name (), "__pfn") != 0
15306 || type->field (1).name () == NULL
15307 || strcmp (type->field (1).name (), "__delta") != 0)
15308 return;
15309
15310 /* Find the type of the method. */
15311 pfn_type = type->field (0).type ();
15312 if (pfn_type == NULL
15313 || pfn_type->code () != TYPE_CODE_PTR
15314 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15315 return;
15316
15317 /* Look for the "this" argument. */
15318 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15319 if (pfn_type->num_fields () == 0
15320 /* || pfn_type->field (0).type () == NULL */
15321 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15322 return;
15323
15324 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15325 new_type = alloc_type (objfile);
15326 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15327 pfn_type->fields (), pfn_type->num_fields (),
15328 pfn_type->has_varargs ());
15329 smash_to_methodptr_type (type, new_type);
15330 }
15331
15332 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15333 requires rewriting, then copy it and return the updated copy.
15334 Otherwise return nullptr. */
15335
15336 static struct type *
15337 rewrite_array_type (struct type *type)
15338 {
15339 if (type->code () != TYPE_CODE_ARRAY)
15340 return nullptr;
15341
15342 struct type *index_type = type->index_type ();
15343 range_bounds *current_bounds = index_type->bounds ();
15344
15345 /* Handle multi-dimensional arrays. */
15346 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15347 if (new_target == nullptr)
15348 {
15349 /* Maybe we don't need to rewrite this array. */
15350 if (current_bounds->low.kind () == PROP_CONST
15351 && current_bounds->high.kind () == PROP_CONST)
15352 return nullptr;
15353 }
15354
15355 /* Either the target type was rewritten, or the bounds have to be
15356 updated. Either way we want to copy the type and update
15357 everything. */
15358 struct type *copy = copy_type (type);
15359 int nfields = copy->num_fields ();
15360 field *new_fields
15361 = ((struct field *) TYPE_ZALLOC (copy,
15362 nfields * sizeof (struct field)));
15363 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15364 copy->set_fields (new_fields);
15365 if (new_target != nullptr)
15366 TYPE_TARGET_TYPE (copy) = new_target;
15367
15368 struct type *index_copy = copy_type (index_type);
15369 range_bounds *bounds
15370 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15371 sizeof (range_bounds));
15372 *bounds = *current_bounds;
15373 bounds->low.set_const_val (1);
15374 bounds->high.set_const_val (0);
15375 index_copy->set_bounds (bounds);
15376 copy->set_index_type (index_copy);
15377
15378 return copy;
15379 }
15380
15381 /* While some versions of GCC will generate complicated DWARF for an
15382 array (see quirk_ada_thick_pointer), more recent versions were
15383 modified to emit an explicit thick pointer structure. However, in
15384 this case, the array still has DWARF expressions for its ranges,
15385 and these must be ignored. */
15386
15387 static void
15388 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15389 struct type *type)
15390 {
15391 gdb_assert (cu->per_cu->lang == language_ada);
15392
15393 /* Check for a structure with two children. */
15394 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15395 return;
15396
15397 /* Check for P_ARRAY and P_BOUNDS members. */
15398 if (type->field (0).name () == NULL
15399 || strcmp (type->field (0).name (), "P_ARRAY") != 0
15400 || type->field (1).name () == NULL
15401 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
15402 return;
15403
15404 /* Make sure we're looking at a pointer to an array. */
15405 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15406 return;
15407
15408 /* The Ada code already knows how to handle these types, so all that
15409 we need to do is turn the bounds into static bounds. However, we
15410 don't want to rewrite existing array or index types in-place,
15411 because those may be referenced in other contexts where this
15412 rewriting is undesirable. */
15413 struct type *new_ary_type
15414 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15415 if (new_ary_type != nullptr)
15416 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15417 }
15418
15419 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15420 appropriate error checking and issuing complaints if there is a
15421 problem. */
15422
15423 static ULONGEST
15424 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15425 {
15426 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15427
15428 if (attr == nullptr)
15429 return 0;
15430
15431 if (!attr->form_is_constant ())
15432 {
15433 complaint (_("DW_AT_alignment must have constant form"
15434 " - DIE at %s [in module %s]"),
15435 sect_offset_str (die->sect_off),
15436 objfile_name (cu->per_objfile->objfile));
15437 return 0;
15438 }
15439
15440 LONGEST val = attr->constant_value (0);
15441 if (val < 0)
15442 {
15443 complaint (_("DW_AT_alignment value must not be negative"
15444 " - DIE at %s [in module %s]"),
15445 sect_offset_str (die->sect_off),
15446 objfile_name (cu->per_objfile->objfile));
15447 return 0;
15448 }
15449 ULONGEST align = val;
15450
15451 if (align == 0)
15452 {
15453 complaint (_("DW_AT_alignment value must not be zero"
15454 " - DIE at %s [in module %s]"),
15455 sect_offset_str (die->sect_off),
15456 objfile_name (cu->per_objfile->objfile));
15457 return 0;
15458 }
15459 if ((align & (align - 1)) != 0)
15460 {
15461 complaint (_("DW_AT_alignment value must be a power of 2"
15462 " - DIE at %s [in module %s]"),
15463 sect_offset_str (die->sect_off),
15464 objfile_name (cu->per_objfile->objfile));
15465 return 0;
15466 }
15467
15468 return align;
15469 }
15470
15471 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15472 the alignment for TYPE. */
15473
15474 static void
15475 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15476 struct type *type)
15477 {
15478 if (!set_type_align (type, get_alignment (cu, die)))
15479 complaint (_("DW_AT_alignment value too large"
15480 " - DIE at %s [in module %s]"),
15481 sect_offset_str (die->sect_off),
15482 objfile_name (cu->per_objfile->objfile));
15483 }
15484
15485 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15486 constant for a type, according to DWARF5 spec, Table 5.5. */
15487
15488 static bool
15489 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15490 {
15491 switch (value)
15492 {
15493 case DW_CC_normal:
15494 case DW_CC_pass_by_reference:
15495 case DW_CC_pass_by_value:
15496 return true;
15497
15498 default:
15499 complaint (_("unrecognized DW_AT_calling_convention value "
15500 "(%s) for a type"), pulongest (value));
15501 return false;
15502 }
15503 }
15504
15505 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15506 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15507 also according to GNU-specific values (see include/dwarf2.h). */
15508
15509 static bool
15510 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15511 {
15512 switch (value)
15513 {
15514 case DW_CC_normal:
15515 case DW_CC_program:
15516 case DW_CC_nocall:
15517 return true;
15518
15519 case DW_CC_GNU_renesas_sh:
15520 case DW_CC_GNU_borland_fastcall_i386:
15521 case DW_CC_GDB_IBM_OpenCL:
15522 return true;
15523
15524 default:
15525 complaint (_("unrecognized DW_AT_calling_convention value "
15526 "(%s) for a subroutine"), pulongest (value));
15527 return false;
15528 }
15529 }
15530
15531 /* Called when we find the DIE that starts a structure or union scope
15532 (definition) to create a type for the structure or union. Fill in
15533 the type's name and general properties; the members will not be
15534 processed until process_structure_scope. A symbol table entry for
15535 the type will also not be done until process_structure_scope (assuming
15536 the type has a name).
15537
15538 NOTE: we need to call these functions regardless of whether or not the
15539 DIE has a DW_AT_name attribute, since it might be an anonymous
15540 structure or union. This gets the type entered into our set of
15541 user defined types. */
15542
15543 static struct type *
15544 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15545 {
15546 struct objfile *objfile = cu->per_objfile->objfile;
15547 struct type *type;
15548 struct attribute *attr;
15549 const char *name;
15550
15551 /* If the definition of this type lives in .debug_types, read that type.
15552 Don't follow DW_AT_specification though, that will take us back up
15553 the chain and we want to go down. */
15554 attr = die->attr (DW_AT_signature);
15555 if (attr != nullptr)
15556 {
15557 type = get_DW_AT_signature_type (die, attr, cu);
15558
15559 /* The type's CU may not be the same as CU.
15560 Ensure TYPE is recorded with CU in die_type_hash. */
15561 return set_die_type (die, type, cu);
15562 }
15563
15564 type = alloc_type (objfile);
15565 INIT_CPLUS_SPECIFIC (type);
15566
15567 name = dwarf2_name (die, cu);
15568 if (name != NULL)
15569 {
15570 if (cu->per_cu->lang == language_cplus
15571 || cu->per_cu->lang == language_d
15572 || cu->per_cu->lang == language_rust)
15573 {
15574 const char *full_name = dwarf2_full_name (name, die, cu);
15575
15576 /* dwarf2_full_name might have already finished building the DIE's
15577 type. If so, there is no need to continue. */
15578 if (get_die_type (die, cu) != NULL)
15579 return get_die_type (die, cu);
15580
15581 type->set_name (full_name);
15582 }
15583 else
15584 {
15585 /* The name is already allocated along with this objfile, so
15586 we don't need to duplicate it for the type. */
15587 type->set_name (name);
15588 }
15589 }
15590
15591 if (die->tag == DW_TAG_structure_type)
15592 {
15593 type->set_code (TYPE_CODE_STRUCT);
15594 }
15595 else if (die->tag == DW_TAG_union_type)
15596 {
15597 type->set_code (TYPE_CODE_UNION);
15598 }
15599 else
15600 {
15601 type->set_code (TYPE_CODE_STRUCT);
15602 }
15603
15604 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
15605 type->set_is_declared_class (true);
15606
15607 /* Store the calling convention in the type if it's available in
15608 the die. Otherwise the calling convention remains set to
15609 the default value DW_CC_normal. */
15610 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15611 if (attr != nullptr
15612 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15613 {
15614 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15615 TYPE_CPLUS_CALLING_CONVENTION (type)
15616 = (enum dwarf_calling_convention) (attr->constant_value (0));
15617 }
15618
15619 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15620 if (attr != nullptr)
15621 {
15622 if (attr->form_is_constant ())
15623 TYPE_LENGTH (type) = attr->constant_value (0);
15624 else
15625 {
15626 struct dynamic_prop prop;
15627 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15628 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15629 TYPE_LENGTH (type) = 0;
15630 }
15631 }
15632 else
15633 {
15634 TYPE_LENGTH (type) = 0;
15635 }
15636
15637 maybe_set_alignment (cu, die, type);
15638
15639 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15640 {
15641 /* ICC<14 does not output the required DW_AT_declaration on
15642 incomplete types, but gives them a size of zero. */
15643 type->set_is_stub (true);
15644 }
15645 else
15646 type->set_stub_is_supported (true);
15647
15648 if (die_is_declaration (die, cu))
15649 type->set_is_stub (true);
15650 else if (attr == NULL && die->child == NULL
15651 && producer_is_realview (cu->producer))
15652 /* RealView does not output the required DW_AT_declaration
15653 on incomplete types. */
15654 type->set_is_stub (true);
15655
15656 /* We need to add the type field to the die immediately so we don't
15657 infinitely recurse when dealing with pointers to the structure
15658 type within the structure itself. */
15659 set_die_type (die, type, cu);
15660
15661 /* set_die_type should be already done. */
15662 set_descriptive_type (type, die, cu);
15663
15664 return type;
15665 }
15666
15667 static void handle_struct_member_die
15668 (struct die_info *child_die,
15669 struct type *type,
15670 struct field_info *fi,
15671 std::vector<struct symbol *> *template_args,
15672 struct dwarf2_cu *cu);
15673
15674 /* A helper for handle_struct_member_die that handles
15675 DW_TAG_variant_part. */
15676
15677 static void
15678 handle_variant_part (struct die_info *die, struct type *type,
15679 struct field_info *fi,
15680 std::vector<struct symbol *> *template_args,
15681 struct dwarf2_cu *cu)
15682 {
15683 variant_part_builder *new_part;
15684 if (fi->current_variant_part == nullptr)
15685 {
15686 fi->variant_parts.emplace_back ();
15687 new_part = &fi->variant_parts.back ();
15688 }
15689 else if (!fi->current_variant_part->processing_variant)
15690 {
15691 complaint (_("nested DW_TAG_variant_part seen "
15692 "- DIE at %s [in module %s]"),
15693 sect_offset_str (die->sect_off),
15694 objfile_name (cu->per_objfile->objfile));
15695 return;
15696 }
15697 else
15698 {
15699 variant_field &current = fi->current_variant_part->variants.back ();
15700 current.variant_parts.emplace_back ();
15701 new_part = &current.variant_parts.back ();
15702 }
15703
15704 /* When we recurse, we want callees to add to this new variant
15705 part. */
15706 scoped_restore save_current_variant_part
15707 = make_scoped_restore (&fi->current_variant_part, new_part);
15708
15709 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15710 if (discr == NULL)
15711 {
15712 /* It's a univariant form, an extension we support. */
15713 }
15714 else if (discr->form_is_ref ())
15715 {
15716 struct dwarf2_cu *target_cu = cu;
15717 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15718
15719 new_part->discriminant_offset = target_die->sect_off;
15720 }
15721 else
15722 {
15723 complaint (_("DW_AT_discr does not have DIE reference form"
15724 " - DIE at %s [in module %s]"),
15725 sect_offset_str (die->sect_off),
15726 objfile_name (cu->per_objfile->objfile));
15727 }
15728
15729 for (die_info *child_die = die->child;
15730 child_die != NULL;
15731 child_die = child_die->sibling)
15732 handle_struct_member_die (child_die, type, fi, template_args, cu);
15733 }
15734
15735 /* A helper for handle_struct_member_die that handles
15736 DW_TAG_variant. */
15737
15738 static void
15739 handle_variant (struct die_info *die, struct type *type,
15740 struct field_info *fi,
15741 std::vector<struct symbol *> *template_args,
15742 struct dwarf2_cu *cu)
15743 {
15744 if (fi->current_variant_part == nullptr)
15745 {
15746 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15747 "- DIE at %s [in module %s]"),
15748 sect_offset_str (die->sect_off),
15749 objfile_name (cu->per_objfile->objfile));
15750 return;
15751 }
15752 if (fi->current_variant_part->processing_variant)
15753 {
15754 complaint (_("nested DW_TAG_variant seen "
15755 "- DIE at %s [in module %s]"),
15756 sect_offset_str (die->sect_off),
15757 objfile_name (cu->per_objfile->objfile));
15758 return;
15759 }
15760
15761 scoped_restore save_processing_variant
15762 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15763 true);
15764
15765 fi->current_variant_part->variants.emplace_back ();
15766 variant_field &variant = fi->current_variant_part->variants.back ();
15767 variant.first_field = fi->fields.size ();
15768
15769 /* In a variant we want to get the discriminant and also add a
15770 field for our sole member child. */
15771 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15772 if (discr == nullptr || !discr->form_is_constant ())
15773 {
15774 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15775 if (discr == nullptr || discr->as_block ()->size == 0)
15776 variant.default_branch = true;
15777 else
15778 variant.discr_list_data = discr->as_block ();
15779 }
15780 else
15781 variant.discriminant_value = discr->constant_value (0);
15782
15783 for (die_info *variant_child = die->child;
15784 variant_child != NULL;
15785 variant_child = variant_child->sibling)
15786 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15787
15788 variant.last_field = fi->fields.size ();
15789 }
15790
15791 /* A helper for process_structure_scope that handles a single member
15792 DIE. */
15793
15794 static void
15795 handle_struct_member_die (struct die_info *child_die, struct type *type,
15796 struct field_info *fi,
15797 std::vector<struct symbol *> *template_args,
15798 struct dwarf2_cu *cu)
15799 {
15800 if (child_die->tag == DW_TAG_member
15801 || child_die->tag == DW_TAG_variable)
15802 {
15803 /* NOTE: carlton/2002-11-05: A C++ static data member
15804 should be a DW_TAG_member that is a declaration, but
15805 all versions of G++ as of this writing (so through at
15806 least 3.2.1) incorrectly generate DW_TAG_variable
15807 tags for them instead. */
15808 dwarf2_add_field (fi, child_die, cu);
15809 }
15810 else if (child_die->tag == DW_TAG_subprogram)
15811 {
15812 /* Rust doesn't have member functions in the C++ sense.
15813 However, it does emit ordinary functions as children
15814 of a struct DIE. */
15815 if (cu->per_cu->lang == language_rust)
15816 read_func_scope (child_die, cu);
15817 else
15818 {
15819 /* C++ member function. */
15820 dwarf2_add_member_fn (fi, child_die, type, cu);
15821 }
15822 }
15823 else if (child_die->tag == DW_TAG_inheritance)
15824 {
15825 /* C++ base class field. */
15826 dwarf2_add_field (fi, child_die, cu);
15827 }
15828 else if (type_can_define_types (child_die))
15829 dwarf2_add_type_defn (fi, child_die, cu);
15830 else if (child_die->tag == DW_TAG_template_type_param
15831 || child_die->tag == DW_TAG_template_value_param)
15832 {
15833 struct symbol *arg = new_symbol (child_die, NULL, cu);
15834
15835 if (arg != NULL)
15836 template_args->push_back (arg);
15837 }
15838 else if (child_die->tag == DW_TAG_variant_part)
15839 handle_variant_part (child_die, type, fi, template_args, cu);
15840 else if (child_die->tag == DW_TAG_variant)
15841 handle_variant (child_die, type, fi, template_args, cu);
15842 }
15843
15844 /* Finish creating a structure or union type, including filling in
15845 its members and creating a symbol for it. */
15846
15847 static void
15848 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15849 {
15850 struct objfile *objfile = cu->per_objfile->objfile;
15851 struct die_info *child_die;
15852 struct type *type;
15853
15854 type = get_die_type (die, cu);
15855 if (type == NULL)
15856 type = read_structure_type (die, cu);
15857
15858 bool has_template_parameters = false;
15859 if (die->child != NULL && ! die_is_declaration (die, cu))
15860 {
15861 struct field_info fi;
15862 std::vector<struct symbol *> template_args;
15863
15864 child_die = die->child;
15865
15866 while (child_die && child_die->tag)
15867 {
15868 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15869 child_die = child_die->sibling;
15870 }
15871
15872 /* Attach template arguments to type. */
15873 if (!template_args.empty ())
15874 {
15875 has_template_parameters = true;
15876 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15877 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15878 TYPE_TEMPLATE_ARGUMENTS (type)
15879 = XOBNEWVEC (&objfile->objfile_obstack,
15880 struct symbol *,
15881 TYPE_N_TEMPLATE_ARGUMENTS (type));
15882 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15883 template_args.data (),
15884 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15885 * sizeof (struct symbol *)));
15886 }
15887
15888 /* Attach fields and member functions to the type. */
15889 if (fi.nfields () > 0)
15890 dwarf2_attach_fields_to_type (&fi, type, cu);
15891 if (!fi.fnfieldlists.empty ())
15892 {
15893 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15894
15895 /* Get the type which refers to the base class (possibly this
15896 class itself) which contains the vtable pointer for the current
15897 class from the DW_AT_containing_type attribute. This use of
15898 DW_AT_containing_type is a GNU extension. */
15899
15900 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15901 {
15902 struct type *t = die_containing_type (die, cu);
15903
15904 set_type_vptr_basetype (type, t);
15905 if (type == t)
15906 {
15907 int i;
15908
15909 /* Our own class provides vtbl ptr. */
15910 for (i = t->num_fields () - 1;
15911 i >= TYPE_N_BASECLASSES (t);
15912 --i)
15913 {
15914 const char *fieldname = t->field (i).name ();
15915
15916 if (is_vtable_name (fieldname, cu))
15917 {
15918 set_type_vptr_fieldno (type, i);
15919 break;
15920 }
15921 }
15922
15923 /* Complain if virtual function table field not found. */
15924 if (i < TYPE_N_BASECLASSES (t))
15925 complaint (_("virtual function table pointer "
15926 "not found when defining class '%s'"),
15927 type->name () ? type->name () : "");
15928 }
15929 else
15930 {
15931 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15932 }
15933 }
15934 else if (cu->producer
15935 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15936 {
15937 /* The IBM XLC compiler does not provide direct indication
15938 of the containing type, but the vtable pointer is
15939 always named __vfp. */
15940
15941 int i;
15942
15943 for (i = type->num_fields () - 1;
15944 i >= TYPE_N_BASECLASSES (type);
15945 --i)
15946 {
15947 if (strcmp (type->field (i).name (), "__vfp") == 0)
15948 {
15949 set_type_vptr_fieldno (type, i);
15950 set_type_vptr_basetype (type, type);
15951 break;
15952 }
15953 }
15954 }
15955 }
15956
15957 /* Copy fi.typedef_field_list linked list elements content into the
15958 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15959 if (!fi.typedef_field_list.empty ())
15960 {
15961 int count = fi.typedef_field_list.size ();
15962
15963 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15964 TYPE_TYPEDEF_FIELD_ARRAY (type)
15965 = ((struct decl_field *)
15966 TYPE_ALLOC (type,
15967 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15968 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15969
15970 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15971 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15972 }
15973
15974 /* Copy fi.nested_types_list linked list elements content into the
15975 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15976 if (!fi.nested_types_list.empty ()
15977 && cu->per_cu->lang != language_ada)
15978 {
15979 int count = fi.nested_types_list.size ();
15980
15981 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15982 TYPE_NESTED_TYPES_ARRAY (type)
15983 = ((struct decl_field *)
15984 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15985 TYPE_NESTED_TYPES_COUNT (type) = count;
15986
15987 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15988 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15989 }
15990 }
15991
15992 quirk_gcc_member_function_pointer (type, objfile);
15993 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
15994 cu->rust_unions.push_back (type);
15995 else if (cu->per_cu->lang == language_ada)
15996 quirk_ada_thick_pointer_struct (die, cu, type);
15997
15998 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15999 snapshots) has been known to create a die giving a declaration
16000 for a class that has, as a child, a die giving a definition for a
16001 nested class. So we have to process our children even if the
16002 current die is a declaration. Normally, of course, a declaration
16003 won't have any children at all. */
16004
16005 child_die = die->child;
16006
16007 while (child_die != NULL && child_die->tag)
16008 {
16009 if (child_die->tag == DW_TAG_member
16010 || child_die->tag == DW_TAG_variable
16011 || child_die->tag == DW_TAG_inheritance
16012 || child_die->tag == DW_TAG_template_value_param
16013 || child_die->tag == DW_TAG_template_type_param)
16014 {
16015 /* Do nothing. */
16016 }
16017 else
16018 process_die (child_die, cu);
16019
16020 child_die = child_die->sibling;
16021 }
16022
16023 /* Do not consider external references. According to the DWARF standard,
16024 these DIEs are identified by the fact that they have no byte_size
16025 attribute, and a declaration attribute. */
16026 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16027 || !die_is_declaration (die, cu)
16028 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16029 {
16030 struct symbol *sym = new_symbol (die, type, cu);
16031
16032 if (has_template_parameters)
16033 {
16034 struct symtab *symtab;
16035 if (sym != nullptr)
16036 symtab = symbol_symtab (sym);
16037 else if (cu->line_header != nullptr)
16038 {
16039 /* Any related symtab will do. */
16040 symtab
16041 = cu->line_header->file_names ()[0].symtab;
16042 }
16043 else
16044 {
16045 symtab = nullptr;
16046 complaint (_("could not find suitable "
16047 "symtab for template parameter"
16048 " - DIE at %s [in module %s]"),
16049 sect_offset_str (die->sect_off),
16050 objfile_name (objfile));
16051 }
16052
16053 if (symtab != nullptr)
16054 {
16055 /* Make sure that the symtab is set on the new symbols.
16056 Even though they don't appear in this symtab directly,
16057 other parts of gdb assume that symbols do, and this is
16058 reasonably true. */
16059 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16060 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16061 }
16062 }
16063 }
16064 }
16065
16066 /* Assuming DIE is an enumeration type, and TYPE is its associated
16067 type, update TYPE using some information only available in DIE's
16068 children. In particular, the fields are computed. */
16069
16070 static void
16071 update_enumeration_type_from_children (struct die_info *die,
16072 struct type *type,
16073 struct dwarf2_cu *cu)
16074 {
16075 struct die_info *child_die;
16076 int unsigned_enum = 1;
16077 int flag_enum = 1;
16078
16079 auto_obstack obstack;
16080 std::vector<struct field> fields;
16081
16082 for (child_die = die->child;
16083 child_die != NULL && child_die->tag;
16084 child_die = child_die->sibling)
16085 {
16086 struct attribute *attr;
16087 LONGEST value;
16088 const gdb_byte *bytes;
16089 struct dwarf2_locexpr_baton *baton;
16090 const char *name;
16091
16092 if (child_die->tag != DW_TAG_enumerator)
16093 continue;
16094
16095 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16096 if (attr == NULL)
16097 continue;
16098
16099 name = dwarf2_name (child_die, cu);
16100 if (name == NULL)
16101 name = "<anonymous enumerator>";
16102
16103 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16104 &value, &bytes, &baton);
16105 if (value < 0)
16106 {
16107 unsigned_enum = 0;
16108 flag_enum = 0;
16109 }
16110 else
16111 {
16112 if (count_one_bits_ll (value) >= 2)
16113 flag_enum = 0;
16114 }
16115
16116 fields.emplace_back ();
16117 struct field &field = fields.back ();
16118 field.set_name (dwarf2_physname (name, child_die, cu));
16119 field.set_loc_enumval (value);
16120 }
16121
16122 if (!fields.empty ())
16123 {
16124 type->set_num_fields (fields.size ());
16125 type->set_fields
16126 ((struct field *)
16127 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16128 memcpy (type->fields (), fields.data (),
16129 sizeof (struct field) * fields.size ());
16130 }
16131
16132 if (unsigned_enum)
16133 type->set_is_unsigned (true);
16134
16135 if (flag_enum)
16136 type->set_is_flag_enum (true);
16137 }
16138
16139 /* Given a DW_AT_enumeration_type die, set its type. We do not
16140 complete the type's fields yet, or create any symbols. */
16141
16142 static struct type *
16143 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16144 {
16145 struct objfile *objfile = cu->per_objfile->objfile;
16146 struct type *type;
16147 struct attribute *attr;
16148 const char *name;
16149
16150 /* If the definition of this type lives in .debug_types, read that type.
16151 Don't follow DW_AT_specification though, that will take us back up
16152 the chain and we want to go down. */
16153 attr = die->attr (DW_AT_signature);
16154 if (attr != nullptr)
16155 {
16156 type = get_DW_AT_signature_type (die, attr, cu);
16157
16158 /* The type's CU may not be the same as CU.
16159 Ensure TYPE is recorded with CU in die_type_hash. */
16160 return set_die_type (die, type, cu);
16161 }
16162
16163 type = alloc_type (objfile);
16164
16165 type->set_code (TYPE_CODE_ENUM);
16166 name = dwarf2_full_name (NULL, die, cu);
16167 if (name != NULL)
16168 type->set_name (name);
16169
16170 attr = dwarf2_attr (die, DW_AT_type, cu);
16171 if (attr != NULL)
16172 {
16173 struct type *underlying_type = die_type (die, cu);
16174
16175 TYPE_TARGET_TYPE (type) = underlying_type;
16176 }
16177
16178 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16179 if (attr != nullptr)
16180 {
16181 TYPE_LENGTH (type) = attr->constant_value (0);
16182 }
16183 else
16184 {
16185 TYPE_LENGTH (type) = 0;
16186 }
16187
16188 maybe_set_alignment (cu, die, type);
16189
16190 /* The enumeration DIE can be incomplete. In Ada, any type can be
16191 declared as private in the package spec, and then defined only
16192 inside the package body. Such types are known as Taft Amendment
16193 Types. When another package uses such a type, an incomplete DIE
16194 may be generated by the compiler. */
16195 if (die_is_declaration (die, cu))
16196 type->set_is_stub (true);
16197
16198 /* If this type has an underlying type that is not a stub, then we
16199 may use its attributes. We always use the "unsigned" attribute
16200 in this situation, because ordinarily we guess whether the type
16201 is unsigned -- but the guess can be wrong and the underlying type
16202 can tell us the reality. However, we defer to a local size
16203 attribute if one exists, because this lets the compiler override
16204 the underlying type if needed. */
16205 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16206 {
16207 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16208 underlying_type = check_typedef (underlying_type);
16209
16210 type->set_is_unsigned (underlying_type->is_unsigned ());
16211
16212 if (TYPE_LENGTH (type) == 0)
16213 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16214
16215 if (TYPE_RAW_ALIGN (type) == 0
16216 && TYPE_RAW_ALIGN (underlying_type) != 0)
16217 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16218 }
16219
16220 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16221
16222 set_die_type (die, type, cu);
16223
16224 /* Finish the creation of this type by using the enum's children.
16225 Note that, as usual, this must come after set_die_type to avoid
16226 infinite recursion when trying to compute the names of the
16227 enumerators. */
16228 update_enumeration_type_from_children (die, type, cu);
16229
16230 return type;
16231 }
16232
16233 /* Given a pointer to a die which begins an enumeration, process all
16234 the dies that define the members of the enumeration, and create the
16235 symbol for the enumeration type.
16236
16237 NOTE: We reverse the order of the element list. */
16238
16239 static void
16240 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16241 {
16242 struct type *this_type;
16243
16244 this_type = get_die_type (die, cu);
16245 if (this_type == NULL)
16246 this_type = read_enumeration_type (die, cu);
16247
16248 if (die->child != NULL)
16249 {
16250 struct die_info *child_die;
16251 const char *name;
16252
16253 child_die = die->child;
16254 while (child_die && child_die->tag)
16255 {
16256 if (child_die->tag != DW_TAG_enumerator)
16257 {
16258 process_die (child_die, cu);
16259 }
16260 else
16261 {
16262 name = dwarf2_name (child_die, cu);
16263 if (name)
16264 new_symbol (child_die, this_type, cu);
16265 }
16266
16267 child_die = child_die->sibling;
16268 }
16269 }
16270
16271 /* If we are reading an enum from a .debug_types unit, and the enum
16272 is a declaration, and the enum is not the signatured type in the
16273 unit, then we do not want to add a symbol for it. Adding a
16274 symbol would in some cases obscure the true definition of the
16275 enum, giving users an incomplete type when the definition is
16276 actually available. Note that we do not want to do this for all
16277 enums which are just declarations, because C++0x allows forward
16278 enum declarations. */
16279 if (cu->per_cu->is_debug_types
16280 && die_is_declaration (die, cu))
16281 {
16282 struct signatured_type *sig_type;
16283
16284 sig_type = (struct signatured_type *) cu->per_cu;
16285 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16286 if (sig_type->type_offset_in_section != die->sect_off)
16287 return;
16288 }
16289
16290 new_symbol (die, this_type, cu);
16291 }
16292
16293 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16294 expression for an index type and finds the corresponding field
16295 offset in the hidden "P_BOUNDS" structure. Returns true on success
16296 and updates *FIELD, false if it fails to recognize an
16297 expression. */
16298
16299 static bool
16300 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16301 int *bounds_offset, struct field *field,
16302 struct dwarf2_cu *cu)
16303 {
16304 struct attribute *attr = dwarf2_attr (die, name, cu);
16305 if (attr == nullptr || !attr->form_is_block ())
16306 return false;
16307
16308 const struct dwarf_block *block = attr->as_block ();
16309 const gdb_byte *start = block->data;
16310 const gdb_byte *end = block->data + block->size;
16311
16312 /* The expression to recognize generally looks like:
16313
16314 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16315 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16316
16317 However, the second "plus_uconst" may be missing:
16318
16319 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16320 DW_OP_deref_size: 4)
16321
16322 This happens when the field is at the start of the structure.
16323
16324 Also, the final deref may not be sized:
16325
16326 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16327 DW_OP_deref)
16328
16329 This happens when the size of the index type happens to be the
16330 same as the architecture's word size. This can occur with or
16331 without the second plus_uconst. */
16332
16333 if (end - start < 2)
16334 return false;
16335 if (*start++ != DW_OP_push_object_address)
16336 return false;
16337 if (*start++ != DW_OP_plus_uconst)
16338 return false;
16339
16340 uint64_t this_bound_off;
16341 start = gdb_read_uleb128 (start, end, &this_bound_off);
16342 if (start == nullptr || (int) this_bound_off != this_bound_off)
16343 return false;
16344 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16345 is consistent among all bounds. */
16346 if (*bounds_offset == -1)
16347 *bounds_offset = this_bound_off;
16348 else if (*bounds_offset != this_bound_off)
16349 return false;
16350
16351 if (start == end || *start++ != DW_OP_deref)
16352 return false;
16353
16354 int offset = 0;
16355 if (start ==end)
16356 return false;
16357 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16358 {
16359 /* This means an offset of 0. */
16360 }
16361 else if (*start++ != DW_OP_plus_uconst)
16362 return false;
16363 else
16364 {
16365 /* The size is the parameter to DW_OP_plus_uconst. */
16366 uint64_t val;
16367 start = gdb_read_uleb128 (start, end, &val);
16368 if (start == nullptr)
16369 return false;
16370 if ((int) val != val)
16371 return false;
16372 offset = val;
16373 }
16374
16375 if (start == end)
16376 return false;
16377
16378 uint64_t size;
16379 if (*start == DW_OP_deref_size)
16380 {
16381 start = gdb_read_uleb128 (start + 1, end, &size);
16382 if (start == nullptr)
16383 return false;
16384 }
16385 else if (*start == DW_OP_deref)
16386 {
16387 size = cu->header.addr_size;
16388 ++start;
16389 }
16390 else
16391 return false;
16392
16393 field->set_loc_bitpos (8 * offset);
16394 if (size != TYPE_LENGTH (field->type ()))
16395 FIELD_BITSIZE (*field) = 8 * size;
16396
16397 return true;
16398 }
16399
16400 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16401 some kinds of Ada arrays:
16402
16403 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16404 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16405 <11e0> DW_AT_data_location: 2 byte block: 97 6
16406 (DW_OP_push_object_address; DW_OP_deref)
16407 <11e3> DW_AT_type : <0x1173>
16408 <11e7> DW_AT_sibling : <0x1201>
16409 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16410 <11ec> DW_AT_type : <0x1206>
16411 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16412 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16413 DW_OP_deref_size: 4)
16414 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16415 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16416 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16417
16418 This actually represents a "thick pointer", which is a structure
16419 with two elements: one that is a pointer to the array data, and one
16420 that is a pointer to another structure; this second structure holds
16421 the array bounds.
16422
16423 This returns a new type on success, or nullptr if this didn't
16424 recognize the type. */
16425
16426 static struct type *
16427 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16428 struct type *type)
16429 {
16430 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16431 /* So far we've only seen this with block form. */
16432 if (attr == nullptr || !attr->form_is_block ())
16433 return nullptr;
16434
16435 /* Note that this will fail if the structure layout is changed by
16436 the compiler. However, we have no good way to recognize some
16437 other layout, because we don't know what expression the compiler
16438 might choose to emit should this happen. */
16439 struct dwarf_block *blk = attr->as_block ();
16440 if (blk->size != 2
16441 || blk->data[0] != DW_OP_push_object_address
16442 || blk->data[1] != DW_OP_deref)
16443 return nullptr;
16444
16445 int bounds_offset = -1;
16446 int max_align = -1;
16447 std::vector<struct field> range_fields;
16448 for (struct die_info *child_die = die->child;
16449 child_die;
16450 child_die = child_die->sibling)
16451 {
16452 if (child_die->tag == DW_TAG_subrange_type)
16453 {
16454 struct type *underlying = read_subrange_index_type (child_die, cu);
16455
16456 int this_align = type_align (underlying);
16457 if (this_align > max_align)
16458 max_align = this_align;
16459
16460 range_fields.emplace_back ();
16461 range_fields.emplace_back ();
16462
16463 struct field &lower = range_fields[range_fields.size () - 2];
16464 struct field &upper = range_fields[range_fields.size () - 1];
16465
16466 lower.set_type (underlying);
16467 FIELD_ARTIFICIAL (lower) = 1;
16468
16469 upper.set_type (underlying);
16470 FIELD_ARTIFICIAL (upper) = 1;
16471
16472 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16473 &bounds_offset, &lower, cu)
16474 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16475 &bounds_offset, &upper, cu))
16476 return nullptr;
16477 }
16478 }
16479
16480 /* This shouldn't really happen, but double-check that we found
16481 where the bounds are stored. */
16482 if (bounds_offset == -1)
16483 return nullptr;
16484
16485 struct objfile *objfile = cu->per_objfile->objfile;
16486 for (int i = 0; i < range_fields.size (); i += 2)
16487 {
16488 char name[20];
16489
16490 /* Set the name of each field in the bounds. */
16491 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16492 range_fields[i].set_name (objfile->intern (name));
16493 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16494 range_fields[i + 1].set_name (objfile->intern (name));
16495 }
16496
16497 struct type *bounds = alloc_type (objfile);
16498 bounds->set_code (TYPE_CODE_STRUCT);
16499
16500 bounds->set_num_fields (range_fields.size ());
16501 bounds->set_fields
16502 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16503 * sizeof (struct field))));
16504 memcpy (bounds->fields (), range_fields.data (),
16505 bounds->num_fields () * sizeof (struct field));
16506
16507 int last_fieldno = range_fields.size () - 1;
16508 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
16509 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16510 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16511
16512 /* Rewrite the existing array type in place. Specifically, we
16513 remove any dynamic properties we might have read, and we replace
16514 the index types. */
16515 struct type *iter = type;
16516 for (int i = 0; i < range_fields.size (); i += 2)
16517 {
16518 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16519 iter->main_type->dyn_prop_list = nullptr;
16520 iter->set_index_type
16521 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16522 iter = TYPE_TARGET_TYPE (iter);
16523 }
16524
16525 struct type *result = alloc_type (objfile);
16526 result->set_code (TYPE_CODE_STRUCT);
16527
16528 result->set_num_fields (2);
16529 result->set_fields
16530 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16531 * sizeof (struct field))));
16532
16533 /* The names are chosen to coincide with what the compiler does with
16534 -fgnat-encodings=all, which the Ada code in gdb already
16535 understands. */
16536 result->field (0).set_name ("P_ARRAY");
16537 result->field (0).set_type (lookup_pointer_type (type));
16538
16539 result->field (1).set_name ("P_BOUNDS");
16540 result->field (1).set_type (lookup_pointer_type (bounds));
16541 result->field (1).set_loc_bitpos (8 * bounds_offset);
16542
16543 result->set_name (type->name ());
16544 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16545 + TYPE_LENGTH (result->field (1).type ()));
16546
16547 return result;
16548 }
16549
16550 /* Extract all information from a DW_TAG_array_type DIE and put it in
16551 the DIE's type field. For now, this only handles one dimensional
16552 arrays. */
16553
16554 static struct type *
16555 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16556 {
16557 struct objfile *objfile = cu->per_objfile->objfile;
16558 struct die_info *child_die;
16559 struct type *type;
16560 struct type *element_type, *range_type, *index_type;
16561 struct attribute *attr;
16562 const char *name;
16563 struct dynamic_prop *byte_stride_prop = NULL;
16564 unsigned int bit_stride = 0;
16565
16566 element_type = die_type (die, cu);
16567
16568 /* The die_type call above may have already set the type for this DIE. */
16569 type = get_die_type (die, cu);
16570 if (type)
16571 return type;
16572
16573 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16574 if (attr != NULL)
16575 {
16576 int stride_ok;
16577 struct type *prop_type = cu->addr_sized_int_type (false);
16578
16579 byte_stride_prop
16580 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16581 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16582 prop_type);
16583 if (!stride_ok)
16584 {
16585 complaint (_("unable to read array DW_AT_byte_stride "
16586 " - DIE at %s [in module %s]"),
16587 sect_offset_str (die->sect_off),
16588 objfile_name (cu->per_objfile->objfile));
16589 /* Ignore this attribute. We will likely not be able to print
16590 arrays of this type correctly, but there is little we can do
16591 to help if we cannot read the attribute's value. */
16592 byte_stride_prop = NULL;
16593 }
16594 }
16595
16596 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16597 if (attr != NULL)
16598 bit_stride = attr->constant_value (0);
16599
16600 /* Irix 6.2 native cc creates array types without children for
16601 arrays with unspecified length. */
16602 if (die->child == NULL)
16603 {
16604 index_type = objfile_type (objfile)->builtin_int;
16605 range_type = create_static_range_type (NULL, index_type, 0, -1);
16606 type = create_array_type_with_stride (NULL, element_type, range_type,
16607 byte_stride_prop, bit_stride);
16608 return set_die_type (die, type, cu);
16609 }
16610
16611 std::vector<struct type *> range_types;
16612 child_die = die->child;
16613 while (child_die && child_die->tag)
16614 {
16615 if (child_die->tag == DW_TAG_subrange_type)
16616 {
16617 struct type *child_type = read_type_die (child_die, cu);
16618
16619 if (child_type != NULL)
16620 {
16621 /* The range type was succesfully read. Save it for the
16622 array type creation. */
16623 range_types.push_back (child_type);
16624 }
16625 }
16626 child_die = child_die->sibling;
16627 }
16628
16629 if (range_types.empty ())
16630 {
16631 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16632 sect_offset_str (die->sect_off),
16633 objfile_name (cu->per_objfile->objfile));
16634 return NULL;
16635 }
16636
16637 /* Dwarf2 dimensions are output from left to right, create the
16638 necessary array types in backwards order. */
16639
16640 type = element_type;
16641
16642 if (read_array_order (die, cu) == DW_ORD_col_major)
16643 {
16644 int i = 0;
16645
16646 while (i < range_types.size ())
16647 {
16648 type = create_array_type_with_stride (NULL, type, range_types[i++],
16649 byte_stride_prop, bit_stride);
16650 bit_stride = 0;
16651 byte_stride_prop = nullptr;
16652 }
16653 }
16654 else
16655 {
16656 size_t ndim = range_types.size ();
16657 while (ndim-- > 0)
16658 {
16659 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16660 byte_stride_prop, bit_stride);
16661 bit_stride = 0;
16662 byte_stride_prop = nullptr;
16663 }
16664 }
16665
16666 gdb_assert (type != element_type);
16667
16668 /* Understand Dwarf2 support for vector types (like they occur on
16669 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16670 array type. This is not part of the Dwarf2/3 standard yet, but a
16671 custom vendor extension. The main difference between a regular
16672 array and the vector variant is that vectors are passed by value
16673 to functions. */
16674 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16675 if (attr != nullptr)
16676 make_vector_type (type);
16677
16678 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16679 implementation may choose to implement triple vectors using this
16680 attribute. */
16681 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16682 if (attr != nullptr && attr->form_is_unsigned ())
16683 {
16684 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16685 TYPE_LENGTH (type) = attr->as_unsigned ();
16686 else
16687 complaint (_("DW_AT_byte_size for array type smaller "
16688 "than the total size of elements"));
16689 }
16690
16691 name = dwarf2_name (die, cu);
16692 if (name)
16693 type->set_name (name);
16694
16695 maybe_set_alignment (cu, die, type);
16696
16697 struct type *replacement_type = nullptr;
16698 if (cu->per_cu->lang == language_ada)
16699 {
16700 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16701 if (replacement_type != nullptr)
16702 type = replacement_type;
16703 }
16704
16705 /* Install the type in the die. */
16706 set_die_type (die, type, cu, replacement_type != nullptr);
16707
16708 /* set_die_type should be already done. */
16709 set_descriptive_type (type, die, cu);
16710
16711 return type;
16712 }
16713
16714 static enum dwarf_array_dim_ordering
16715 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16716 {
16717 struct attribute *attr;
16718
16719 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16720
16721 if (attr != nullptr)
16722 {
16723 LONGEST val = attr->constant_value (-1);
16724 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16725 return (enum dwarf_array_dim_ordering) val;
16726 }
16727
16728 /* GNU F77 is a special case, as at 08/2004 array type info is the
16729 opposite order to the dwarf2 specification, but data is still
16730 laid out as per normal fortran.
16731
16732 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16733 version checking. */
16734
16735 if (cu->per_cu->lang == language_fortran
16736 && cu->producer && strstr (cu->producer, "GNU F77"))
16737 {
16738 return DW_ORD_row_major;
16739 }
16740
16741 switch (cu->language_defn->array_ordering ())
16742 {
16743 case array_column_major:
16744 return DW_ORD_col_major;
16745 case array_row_major:
16746 default:
16747 return DW_ORD_row_major;
16748 };
16749 }
16750
16751 /* Extract all information from a DW_TAG_set_type DIE and put it in
16752 the DIE's type field. */
16753
16754 static struct type *
16755 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16756 {
16757 struct type *domain_type, *set_type;
16758 struct attribute *attr;
16759
16760 domain_type = die_type (die, cu);
16761
16762 /* The die_type call above may have already set the type for this DIE. */
16763 set_type = get_die_type (die, cu);
16764 if (set_type)
16765 return set_type;
16766
16767 set_type = create_set_type (NULL, domain_type);
16768
16769 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16770 if (attr != nullptr && attr->form_is_unsigned ())
16771 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16772
16773 maybe_set_alignment (cu, die, set_type);
16774
16775 return set_die_type (die, set_type, cu);
16776 }
16777
16778 /* A helper for read_common_block that creates a locexpr baton.
16779 SYM is the symbol which we are marking as computed.
16780 COMMON_DIE is the DIE for the common block.
16781 COMMON_LOC is the location expression attribute for the common
16782 block itself.
16783 MEMBER_LOC is the location expression attribute for the particular
16784 member of the common block that we are processing.
16785 CU is the CU from which the above come. */
16786
16787 static void
16788 mark_common_block_symbol_computed (struct symbol *sym,
16789 struct die_info *common_die,
16790 struct attribute *common_loc,
16791 struct attribute *member_loc,
16792 struct dwarf2_cu *cu)
16793 {
16794 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16795 struct objfile *objfile = per_objfile->objfile;
16796 struct dwarf2_locexpr_baton *baton;
16797 gdb_byte *ptr;
16798 unsigned int cu_off;
16799 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16800 LONGEST offset = 0;
16801
16802 gdb_assert (common_loc && member_loc);
16803 gdb_assert (common_loc->form_is_block ());
16804 gdb_assert (member_loc->form_is_block ()
16805 || member_loc->form_is_constant ());
16806
16807 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16808 baton->per_objfile = per_objfile;
16809 baton->per_cu = cu->per_cu;
16810 gdb_assert (baton->per_cu);
16811
16812 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16813
16814 if (member_loc->form_is_constant ())
16815 {
16816 offset = member_loc->constant_value (0);
16817 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16818 }
16819 else
16820 baton->size += member_loc->as_block ()->size;
16821
16822 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16823 baton->data = ptr;
16824
16825 *ptr++ = DW_OP_call4;
16826 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16827 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16828 ptr += 4;
16829
16830 if (member_loc->form_is_constant ())
16831 {
16832 *ptr++ = DW_OP_addr;
16833 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16834 ptr += cu->header.addr_size;
16835 }
16836 else
16837 {
16838 /* We have to copy the data here, because DW_OP_call4 will only
16839 use a DW_AT_location attribute. */
16840 struct dwarf_block *block = member_loc->as_block ();
16841 memcpy (ptr, block->data, block->size);
16842 ptr += block->size;
16843 }
16844
16845 *ptr++ = DW_OP_plus;
16846 gdb_assert (ptr - baton->data == baton->size);
16847
16848 SYMBOL_LOCATION_BATON (sym) = baton;
16849 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16850 }
16851
16852 /* Create appropriate locally-scoped variables for all the
16853 DW_TAG_common_block entries. Also create a struct common_block
16854 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16855 is used to separate the common blocks name namespace from regular
16856 variable names. */
16857
16858 static void
16859 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16860 {
16861 struct attribute *attr;
16862
16863 attr = dwarf2_attr (die, DW_AT_location, cu);
16864 if (attr != nullptr)
16865 {
16866 /* Support the .debug_loc offsets. */
16867 if (attr->form_is_block ())
16868 {
16869 /* Ok. */
16870 }
16871 else if (attr->form_is_section_offset ())
16872 {
16873 dwarf2_complex_location_expr_complaint ();
16874 attr = NULL;
16875 }
16876 else
16877 {
16878 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16879 "common block member");
16880 attr = NULL;
16881 }
16882 }
16883
16884 if (die->child != NULL)
16885 {
16886 struct objfile *objfile = cu->per_objfile->objfile;
16887 struct die_info *child_die;
16888 size_t n_entries = 0, size;
16889 struct common_block *common_block;
16890 struct symbol *sym;
16891
16892 for (child_die = die->child;
16893 child_die && child_die->tag;
16894 child_die = child_die->sibling)
16895 ++n_entries;
16896
16897 size = (sizeof (struct common_block)
16898 + (n_entries - 1) * sizeof (struct symbol *));
16899 common_block
16900 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16901 size);
16902 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16903 common_block->n_entries = 0;
16904
16905 for (child_die = die->child;
16906 child_die && child_die->tag;
16907 child_die = child_die->sibling)
16908 {
16909 /* Create the symbol in the DW_TAG_common_block block in the current
16910 symbol scope. */
16911 sym = new_symbol (child_die, NULL, cu);
16912 if (sym != NULL)
16913 {
16914 struct attribute *member_loc;
16915
16916 common_block->contents[common_block->n_entries++] = sym;
16917
16918 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16919 cu);
16920 if (member_loc)
16921 {
16922 /* GDB has handled this for a long time, but it is
16923 not specified by DWARF. It seems to have been
16924 emitted by gfortran at least as recently as:
16925 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16926 complaint (_("Variable in common block has "
16927 "DW_AT_data_member_location "
16928 "- DIE at %s [in module %s]"),
16929 sect_offset_str (child_die->sect_off),
16930 objfile_name (objfile));
16931
16932 if (member_loc->form_is_section_offset ())
16933 dwarf2_complex_location_expr_complaint ();
16934 else if (member_loc->form_is_constant ()
16935 || member_loc->form_is_block ())
16936 {
16937 if (attr != nullptr)
16938 mark_common_block_symbol_computed (sym, die, attr,
16939 member_loc, cu);
16940 }
16941 else
16942 dwarf2_complex_location_expr_complaint ();
16943 }
16944 }
16945 }
16946
16947 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16948 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16949 }
16950 }
16951
16952 /* Create a type for a C++ namespace. */
16953
16954 static struct type *
16955 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16956 {
16957 struct objfile *objfile = cu->per_objfile->objfile;
16958 const char *previous_prefix, *name;
16959 int is_anonymous;
16960 struct type *type;
16961
16962 /* For extensions, reuse the type of the original namespace. */
16963 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16964 {
16965 struct die_info *ext_die;
16966 struct dwarf2_cu *ext_cu = cu;
16967
16968 ext_die = dwarf2_extension (die, &ext_cu);
16969 type = read_type_die (ext_die, ext_cu);
16970
16971 /* EXT_CU may not be the same as CU.
16972 Ensure TYPE is recorded with CU in die_type_hash. */
16973 return set_die_type (die, type, cu);
16974 }
16975
16976 name = namespace_name (die, &is_anonymous, cu);
16977
16978 /* Now build the name of the current namespace. */
16979
16980 previous_prefix = determine_prefix (die, cu);
16981 if (previous_prefix[0] != '\0')
16982 name = typename_concat (&objfile->objfile_obstack,
16983 previous_prefix, name, 0, cu);
16984
16985 /* Create the type. */
16986 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16987
16988 return set_die_type (die, type, cu);
16989 }
16990
16991 /* Read a namespace scope. */
16992
16993 static void
16994 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16995 {
16996 struct objfile *objfile = cu->per_objfile->objfile;
16997 int is_anonymous;
16998
16999 /* Add a symbol associated to this if we haven't seen the namespace
17000 before. Also, add a using directive if it's an anonymous
17001 namespace. */
17002
17003 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17004 {
17005 struct type *type;
17006
17007 type = read_type_die (die, cu);
17008 new_symbol (die, type, cu);
17009
17010 namespace_name (die, &is_anonymous, cu);
17011 if (is_anonymous)
17012 {
17013 const char *previous_prefix = determine_prefix (die, cu);
17014
17015 std::vector<const char *> excludes;
17016 add_using_directive (using_directives (cu),
17017 previous_prefix, type->name (), NULL,
17018 NULL, excludes, 0, &objfile->objfile_obstack);
17019 }
17020 }
17021
17022 if (die->child != NULL)
17023 {
17024 struct die_info *child_die = die->child;
17025
17026 while (child_die && child_die->tag)
17027 {
17028 process_die (child_die, cu);
17029 child_die = child_die->sibling;
17030 }
17031 }
17032 }
17033
17034 /* Read a Fortran module as type. This DIE can be only a declaration used for
17035 imported module. Still we need that type as local Fortran "use ... only"
17036 declaration imports depend on the created type in determine_prefix. */
17037
17038 static struct type *
17039 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17040 {
17041 struct objfile *objfile = cu->per_objfile->objfile;
17042 const char *module_name;
17043 struct type *type;
17044
17045 module_name = dwarf2_name (die, cu);
17046 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17047
17048 return set_die_type (die, type, cu);
17049 }
17050
17051 /* Read a Fortran module. */
17052
17053 static void
17054 read_module (struct die_info *die, struct dwarf2_cu *cu)
17055 {
17056 struct die_info *child_die = die->child;
17057 struct type *type;
17058
17059 type = read_type_die (die, cu);
17060 new_symbol (die, type, cu);
17061
17062 while (child_die && child_die->tag)
17063 {
17064 process_die (child_die, cu);
17065 child_die = child_die->sibling;
17066 }
17067 }
17068
17069 /* Return the name of the namespace represented by DIE. Set
17070 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17071 namespace. */
17072
17073 static const char *
17074 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17075 {
17076 struct die_info *current_die;
17077 const char *name = NULL;
17078
17079 /* Loop through the extensions until we find a name. */
17080
17081 for (current_die = die;
17082 current_die != NULL;
17083 current_die = dwarf2_extension (die, &cu))
17084 {
17085 /* We don't use dwarf2_name here so that we can detect the absence
17086 of a name -> anonymous namespace. */
17087 name = dwarf2_string_attr (die, DW_AT_name, cu);
17088
17089 if (name != NULL)
17090 break;
17091 }
17092
17093 /* Is it an anonymous namespace? */
17094
17095 *is_anonymous = (name == NULL);
17096 if (*is_anonymous)
17097 name = CP_ANONYMOUS_NAMESPACE_STR;
17098
17099 return name;
17100 }
17101
17102 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17103 the user defined type vector. */
17104
17105 static struct type *
17106 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17107 {
17108 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17109 struct comp_unit_head *cu_header = &cu->header;
17110 struct type *type;
17111 struct attribute *attr_byte_size;
17112 struct attribute *attr_address_class;
17113 int byte_size, addr_class;
17114 struct type *target_type;
17115
17116 target_type = die_type (die, cu);
17117
17118 /* The die_type call above may have already set the type for this DIE. */
17119 type = get_die_type (die, cu);
17120 if (type)
17121 return type;
17122
17123 type = lookup_pointer_type (target_type);
17124
17125 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17126 if (attr_byte_size)
17127 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17128 else
17129 byte_size = cu_header->addr_size;
17130
17131 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17132 if (attr_address_class)
17133 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17134 else
17135 addr_class = DW_ADDR_none;
17136
17137 ULONGEST alignment = get_alignment (cu, die);
17138
17139 /* If the pointer size, alignment, or address class is different
17140 than the default, create a type variant marked as such and set
17141 the length accordingly. */
17142 if (TYPE_LENGTH (type) != byte_size
17143 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17144 && alignment != TYPE_RAW_ALIGN (type))
17145 || addr_class != DW_ADDR_none)
17146 {
17147 if (gdbarch_address_class_type_flags_p (gdbarch))
17148 {
17149 type_instance_flags type_flags
17150 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17151 addr_class);
17152 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17153 == 0);
17154 type = make_type_with_address_space (type, type_flags);
17155 }
17156 else if (TYPE_LENGTH (type) != byte_size)
17157 {
17158 complaint (_("invalid pointer size %d"), byte_size);
17159 }
17160 else if (TYPE_RAW_ALIGN (type) != alignment)
17161 {
17162 complaint (_("Invalid DW_AT_alignment"
17163 " - DIE at %s [in module %s]"),
17164 sect_offset_str (die->sect_off),
17165 objfile_name (cu->per_objfile->objfile));
17166 }
17167 else
17168 {
17169 /* Should we also complain about unhandled address classes? */
17170 }
17171 }
17172
17173 TYPE_LENGTH (type) = byte_size;
17174 set_type_align (type, alignment);
17175 return set_die_type (die, type, cu);
17176 }
17177
17178 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17179 the user defined type vector. */
17180
17181 static struct type *
17182 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17183 {
17184 struct type *type;
17185 struct type *to_type;
17186 struct type *domain;
17187
17188 to_type = die_type (die, cu);
17189 domain = die_containing_type (die, cu);
17190
17191 /* The calls above may have already set the type for this DIE. */
17192 type = get_die_type (die, cu);
17193 if (type)
17194 return type;
17195
17196 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17197 type = lookup_methodptr_type (to_type);
17198 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17199 {
17200 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17201
17202 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17203 to_type->fields (), to_type->num_fields (),
17204 to_type->has_varargs ());
17205 type = lookup_methodptr_type (new_type);
17206 }
17207 else
17208 type = lookup_memberptr_type (to_type, domain);
17209
17210 return set_die_type (die, type, cu);
17211 }
17212
17213 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17214 the user defined type vector. */
17215
17216 static struct type *
17217 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17218 enum type_code refcode)
17219 {
17220 struct comp_unit_head *cu_header = &cu->header;
17221 struct type *type, *target_type;
17222 struct attribute *attr;
17223
17224 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17225
17226 target_type = die_type (die, cu);
17227
17228 /* The die_type call above may have already set the type for this DIE. */
17229 type = get_die_type (die, cu);
17230 if (type)
17231 return type;
17232
17233 type = lookup_reference_type (target_type, refcode);
17234 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17235 if (attr != nullptr)
17236 {
17237 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17238 }
17239 else
17240 {
17241 TYPE_LENGTH (type) = cu_header->addr_size;
17242 }
17243 maybe_set_alignment (cu, die, type);
17244 return set_die_type (die, type, cu);
17245 }
17246
17247 /* Add the given cv-qualifiers to the element type of the array. GCC
17248 outputs DWARF type qualifiers that apply to an array, not the
17249 element type. But GDB relies on the array element type to carry
17250 the cv-qualifiers. This mimics section 6.7.3 of the C99
17251 specification. */
17252
17253 static struct type *
17254 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17255 struct type *base_type, int cnst, int voltl)
17256 {
17257 struct type *el_type, *inner_array;
17258
17259 base_type = copy_type (base_type);
17260 inner_array = base_type;
17261
17262 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17263 {
17264 TYPE_TARGET_TYPE (inner_array) =
17265 copy_type (TYPE_TARGET_TYPE (inner_array));
17266 inner_array = TYPE_TARGET_TYPE (inner_array);
17267 }
17268
17269 el_type = TYPE_TARGET_TYPE (inner_array);
17270 cnst |= TYPE_CONST (el_type);
17271 voltl |= TYPE_VOLATILE (el_type);
17272 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17273
17274 return set_die_type (die, base_type, cu);
17275 }
17276
17277 static struct type *
17278 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17279 {
17280 struct type *base_type, *cv_type;
17281
17282 base_type = die_type (die, cu);
17283
17284 /* The die_type call above may have already set the type for this DIE. */
17285 cv_type = get_die_type (die, cu);
17286 if (cv_type)
17287 return cv_type;
17288
17289 /* In case the const qualifier is applied to an array type, the element type
17290 is so qualified, not the array type (section 6.7.3 of C99). */
17291 if (base_type->code () == TYPE_CODE_ARRAY)
17292 return add_array_cv_type (die, cu, base_type, 1, 0);
17293
17294 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17295 return set_die_type (die, cv_type, cu);
17296 }
17297
17298 static struct type *
17299 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17300 {
17301 struct type *base_type, *cv_type;
17302
17303 base_type = die_type (die, cu);
17304
17305 /* The die_type call above may have already set the type for this DIE. */
17306 cv_type = get_die_type (die, cu);
17307 if (cv_type)
17308 return cv_type;
17309
17310 /* In case the volatile qualifier is applied to an array type, the
17311 element type is so qualified, not the array type (section 6.7.3
17312 of C99). */
17313 if (base_type->code () == TYPE_CODE_ARRAY)
17314 return add_array_cv_type (die, cu, base_type, 0, 1);
17315
17316 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17317 return set_die_type (die, cv_type, cu);
17318 }
17319
17320 /* Handle DW_TAG_restrict_type. */
17321
17322 static struct type *
17323 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17324 {
17325 struct type *base_type, *cv_type;
17326
17327 base_type = die_type (die, cu);
17328
17329 /* The die_type call above may have already set the type for this DIE. */
17330 cv_type = get_die_type (die, cu);
17331 if (cv_type)
17332 return cv_type;
17333
17334 cv_type = make_restrict_type (base_type);
17335 return set_die_type (die, cv_type, cu);
17336 }
17337
17338 /* Handle DW_TAG_atomic_type. */
17339
17340 static struct type *
17341 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17342 {
17343 struct type *base_type, *cv_type;
17344
17345 base_type = die_type (die, cu);
17346
17347 /* The die_type call above may have already set the type for this DIE. */
17348 cv_type = get_die_type (die, cu);
17349 if (cv_type)
17350 return cv_type;
17351
17352 cv_type = make_atomic_type (base_type);
17353 return set_die_type (die, cv_type, cu);
17354 }
17355
17356 /* Extract all information from a DW_TAG_string_type DIE and add to
17357 the user defined type vector. It isn't really a user defined type,
17358 but it behaves like one, with other DIE's using an AT_user_def_type
17359 attribute to reference it. */
17360
17361 static struct type *
17362 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17363 {
17364 struct objfile *objfile = cu->per_objfile->objfile;
17365 struct gdbarch *gdbarch = objfile->arch ();
17366 struct type *type, *range_type, *index_type, *char_type;
17367 struct attribute *attr;
17368 struct dynamic_prop prop;
17369 bool length_is_constant = true;
17370 LONGEST length;
17371
17372 /* There are a couple of places where bit sizes might be made use of
17373 when parsing a DW_TAG_string_type, however, no producer that we know
17374 of make use of these. Handling bit sizes that are a multiple of the
17375 byte size is easy enough, but what about other bit sizes? Lets deal
17376 with that problem when we have to. Warn about these attributes being
17377 unsupported, then parse the type and ignore them like we always
17378 have. */
17379 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17380 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17381 {
17382 static bool warning_printed = false;
17383 if (!warning_printed)
17384 {
17385 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17386 "currently supported on DW_TAG_string_type."));
17387 warning_printed = true;
17388 }
17389 }
17390
17391 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17392 if (attr != nullptr && !attr->form_is_constant ())
17393 {
17394 /* The string length describes the location at which the length of
17395 the string can be found. The size of the length field can be
17396 specified with one of the attributes below. */
17397 struct type *prop_type;
17398 struct attribute *len
17399 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17400 if (len == nullptr)
17401 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17402 if (len != nullptr && len->form_is_constant ())
17403 {
17404 /* Pass 0 as the default as we know this attribute is constant
17405 and the default value will not be returned. */
17406 LONGEST sz = len->constant_value (0);
17407 prop_type = objfile_int_type (objfile, sz, true);
17408 }
17409 else
17410 {
17411 /* If the size is not specified then we assume it is the size of
17412 an address on this target. */
17413 prop_type = cu->addr_sized_int_type (true);
17414 }
17415
17416 /* Convert the attribute into a dynamic property. */
17417 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17418 length = 1;
17419 else
17420 length_is_constant = false;
17421 }
17422 else if (attr != nullptr)
17423 {
17424 /* This DW_AT_string_length just contains the length with no
17425 indirection. There's no need to create a dynamic property in this
17426 case. Pass 0 for the default value as we know it will not be
17427 returned in this case. */
17428 length = attr->constant_value (0);
17429 }
17430 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17431 {
17432 /* We don't currently support non-constant byte sizes for strings. */
17433 length = attr->constant_value (1);
17434 }
17435 else
17436 {
17437 /* Use 1 as a fallback length if we have nothing else. */
17438 length = 1;
17439 }
17440
17441 index_type = objfile_type (objfile)->builtin_int;
17442 if (length_is_constant)
17443 range_type = create_static_range_type (NULL, index_type, 1, length);
17444 else
17445 {
17446 struct dynamic_prop low_bound;
17447
17448 low_bound.set_const_val (1);
17449 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17450 }
17451 char_type = language_string_char_type (cu->language_defn, gdbarch);
17452 type = create_string_type (NULL, char_type, range_type);
17453
17454 return set_die_type (die, type, cu);
17455 }
17456
17457 /* Assuming that DIE corresponds to a function, returns nonzero
17458 if the function is prototyped. */
17459
17460 static int
17461 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17462 {
17463 struct attribute *attr;
17464
17465 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17466 if (attr && attr->as_boolean ())
17467 return 1;
17468
17469 /* The DWARF standard implies that the DW_AT_prototyped attribute
17470 is only meaningful for C, but the concept also extends to other
17471 languages that allow unprototyped functions (Eg: Objective C).
17472 For all other languages, assume that functions are always
17473 prototyped. */
17474 if (cu->per_cu->lang != language_c
17475 && cu->per_cu->lang != language_objc
17476 && cu->per_cu->lang != language_opencl)
17477 return 1;
17478
17479 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17480 prototyped and unprototyped functions; default to prototyped,
17481 since that is more common in modern code (and RealView warns
17482 about unprototyped functions). */
17483 if (producer_is_realview (cu->producer))
17484 return 1;
17485
17486 return 0;
17487 }
17488
17489 /* Handle DIES due to C code like:
17490
17491 struct foo
17492 {
17493 int (*funcp)(int a, long l);
17494 int b;
17495 };
17496
17497 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17498
17499 static struct type *
17500 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17501 {
17502 struct objfile *objfile = cu->per_objfile->objfile;
17503 struct type *type; /* Type that this function returns. */
17504 struct type *ftype; /* Function that returns above type. */
17505 struct attribute *attr;
17506
17507 type = die_type (die, cu);
17508
17509 /* The die_type call above may have already set the type for this DIE. */
17510 ftype = get_die_type (die, cu);
17511 if (ftype)
17512 return ftype;
17513
17514 ftype = lookup_function_type (type);
17515
17516 if (prototyped_function_p (die, cu))
17517 ftype->set_is_prototyped (true);
17518
17519 /* Store the calling convention in the type if it's available in
17520 the subroutine die. Otherwise set the calling convention to
17521 the default value DW_CC_normal. */
17522 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17523 if (attr != nullptr
17524 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17525 TYPE_CALLING_CONVENTION (ftype)
17526 = (enum dwarf_calling_convention) attr->constant_value (0);
17527 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17528 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17529 else
17530 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17531
17532 /* Record whether the function returns normally to its caller or not
17533 if the DWARF producer set that information. */
17534 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17535 if (attr && attr->as_boolean ())
17536 TYPE_NO_RETURN (ftype) = 1;
17537
17538 /* We need to add the subroutine type to the die immediately so
17539 we don't infinitely recurse when dealing with parameters
17540 declared as the same subroutine type. */
17541 set_die_type (die, ftype, cu);
17542
17543 if (die->child != NULL)
17544 {
17545 struct type *void_type = objfile_type (objfile)->builtin_void;
17546 struct die_info *child_die;
17547 int nparams, iparams;
17548
17549 /* Count the number of parameters.
17550 FIXME: GDB currently ignores vararg functions, but knows about
17551 vararg member functions. */
17552 nparams = 0;
17553 child_die = die->child;
17554 while (child_die && child_die->tag)
17555 {
17556 if (child_die->tag == DW_TAG_formal_parameter)
17557 nparams++;
17558 else if (child_die->tag == DW_TAG_unspecified_parameters)
17559 ftype->set_has_varargs (true);
17560
17561 child_die = child_die->sibling;
17562 }
17563
17564 /* Allocate storage for parameters and fill them in. */
17565 ftype->set_num_fields (nparams);
17566 ftype->set_fields
17567 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17568
17569 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17570 even if we error out during the parameters reading below. */
17571 for (iparams = 0; iparams < nparams; iparams++)
17572 ftype->field (iparams).set_type (void_type);
17573
17574 iparams = 0;
17575 child_die = die->child;
17576 while (child_die && child_die->tag)
17577 {
17578 if (child_die->tag == DW_TAG_formal_parameter)
17579 {
17580 struct type *arg_type;
17581
17582 /* DWARF version 2 has no clean way to discern C++
17583 static and non-static member functions. G++ helps
17584 GDB by marking the first parameter for non-static
17585 member functions (which is the this pointer) as
17586 artificial. We pass this information to
17587 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17588
17589 DWARF version 3 added DW_AT_object_pointer, which GCC
17590 4.5 does not yet generate. */
17591 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17592 if (attr != nullptr)
17593 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17594 else
17595 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17596 arg_type = die_type (child_die, cu);
17597
17598 /* RealView does not mark THIS as const, which the testsuite
17599 expects. GCC marks THIS as const in method definitions,
17600 but not in the class specifications (GCC PR 43053). */
17601 if (cu->per_cu->lang == language_cplus
17602 && !TYPE_CONST (arg_type)
17603 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17604 {
17605 int is_this = 0;
17606 struct dwarf2_cu *arg_cu = cu;
17607 const char *name = dwarf2_name (child_die, cu);
17608
17609 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17610 if (attr != nullptr)
17611 {
17612 /* If the compiler emits this, use it. */
17613 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17614 is_this = 1;
17615 }
17616 else if (name && strcmp (name, "this") == 0)
17617 /* Function definitions will have the argument names. */
17618 is_this = 1;
17619 else if (name == NULL && iparams == 0)
17620 /* Declarations may not have the names, so like
17621 elsewhere in GDB, assume an artificial first
17622 argument is "this". */
17623 is_this = 1;
17624
17625 if (is_this)
17626 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17627 arg_type, 0);
17628 }
17629
17630 ftype->field (iparams).set_type (arg_type);
17631 iparams++;
17632 }
17633 child_die = child_die->sibling;
17634 }
17635 }
17636
17637 return ftype;
17638 }
17639
17640 static struct type *
17641 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17642 {
17643 struct objfile *objfile = cu->per_objfile->objfile;
17644 const char *name = NULL;
17645 struct type *this_type, *target_type;
17646
17647 name = dwarf2_full_name (NULL, die, cu);
17648 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17649 this_type->set_target_is_stub (true);
17650 set_die_type (die, this_type, cu);
17651 target_type = die_type (die, cu);
17652 if (target_type != this_type)
17653 TYPE_TARGET_TYPE (this_type) = target_type;
17654 else
17655 {
17656 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17657 spec and cause infinite loops in GDB. */
17658 complaint (_("Self-referential DW_TAG_typedef "
17659 "- DIE at %s [in module %s]"),
17660 sect_offset_str (die->sect_off), objfile_name (objfile));
17661 TYPE_TARGET_TYPE (this_type) = NULL;
17662 }
17663 if (name == NULL)
17664 {
17665 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17666 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17667 Handle these by just returning the target type, rather than
17668 constructing an anonymous typedef type and trying to handle this
17669 elsewhere. */
17670 set_die_type (die, target_type, cu);
17671 return target_type;
17672 }
17673 return this_type;
17674 }
17675
17676 /* Helper for get_dwarf2_rational_constant that computes the value of
17677 a given gmp_mpz given an attribute. */
17678
17679 static void
17680 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17681 {
17682 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17683 location expression that pushes an implicit value. */
17684 if (attr->form == DW_FORM_exprloc)
17685 {
17686 dwarf_block *blk = attr->as_block ();
17687 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17688 {
17689 uint64_t len;
17690 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17691 blk->data + blk->size,
17692 &len);
17693 if (ptr - blk->data + len <= blk->size)
17694 {
17695 mpz_import (value->val, len,
17696 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17697 1, 0, 0, ptr);
17698 return;
17699 }
17700 }
17701
17702 /* On failure set it to 1. */
17703 *value = gdb_mpz (1);
17704 }
17705 else if (attr->form_is_block ())
17706 {
17707 dwarf_block *blk = attr->as_block ();
17708 mpz_import (value->val, blk->size,
17709 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17710 1, 0, 0, blk->data);
17711 }
17712 else
17713 *value = gdb_mpz (attr->constant_value (1));
17714 }
17715
17716 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17717 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17718
17719 If the numerator and/or numerator attribute is missing,
17720 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17721 untouched. */
17722
17723 static void
17724 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17725 gdb_mpz *numerator, gdb_mpz *denominator)
17726 {
17727 struct attribute *num_attr, *denom_attr;
17728
17729 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17730 if (num_attr == nullptr)
17731 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17732 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17733
17734 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17735 if (denom_attr == nullptr)
17736 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17737 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17738
17739 if (num_attr == nullptr || denom_attr == nullptr)
17740 return;
17741
17742 get_mpz (cu, numerator, num_attr);
17743 get_mpz (cu, denominator, denom_attr);
17744 }
17745
17746 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17747 rational constant, rather than a signed one.
17748
17749 If the rational constant has a negative value, a complaint
17750 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17751
17752 static void
17753 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17754 struct dwarf2_cu *cu,
17755 gdb_mpz *numerator,
17756 gdb_mpz *denominator)
17757 {
17758 gdb_mpz num (1);
17759 gdb_mpz denom (1);
17760
17761 get_dwarf2_rational_constant (die, cu, &num, &denom);
17762 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17763 {
17764 mpz_neg (num.val, num.val);
17765 mpz_neg (denom.val, denom.val);
17766 }
17767 else if (mpz_sgn (num.val) == -1)
17768 {
17769 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17770 " in DIE at %s"),
17771 sect_offset_str (die->sect_off));
17772 return;
17773 }
17774 else if (mpz_sgn (denom.val) == -1)
17775 {
17776 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17777 " in DIE at %s"),
17778 sect_offset_str (die->sect_off));
17779 return;
17780 }
17781
17782 *numerator = std::move (num);
17783 *denominator = std::move (denom);
17784 }
17785
17786 /* Assuming that ENCODING is a string whose contents starting at the
17787 K'th character is "_nn" where "nn" is a decimal number, scan that
17788 number and set RESULT to the value. K is updated to point to the
17789 character immediately following the number.
17790
17791 If the string does not conform to the format described above, false
17792 is returned, and K may or may not be changed. */
17793
17794 static bool
17795 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17796 {
17797 /* The next character should be an underscore ('_') followed
17798 by a digit. */
17799 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17800 return false;
17801
17802 /* Skip the underscore. */
17803 k++;
17804 int start = k;
17805
17806 /* Determine the number of digits for our number. */
17807 while (isdigit (encoding[k]))
17808 k++;
17809 if (k == start)
17810 return false;
17811
17812 std::string copy (&encoding[start], k - start);
17813 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17814 return false;
17815
17816 return true;
17817 }
17818
17819 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17820 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17821 DENOM, update OFFSET, and return true on success. Return false on
17822 failure. */
17823
17824 static bool
17825 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17826 gdb_mpz *num, gdb_mpz *denom)
17827 {
17828 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17829 return false;
17830 return ada_get_gnat_encoded_number (encoding, offset, denom);
17831 }
17832
17833 /* Assuming DIE corresponds to a fixed point type, finish the creation
17834 of the corresponding TYPE by setting its type-specific data. CU is
17835 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17836 encodings. It is nullptr if the GNAT encoding should be
17837 ignored. */
17838
17839 static void
17840 finish_fixed_point_type (struct type *type, const char *suffix,
17841 struct die_info *die, struct dwarf2_cu *cu)
17842 {
17843 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17844 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17845
17846 /* If GNAT encodings are preferred, don't examine the
17847 attributes. */
17848 struct attribute *attr = nullptr;
17849 if (suffix == nullptr)
17850 {
17851 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17852 if (attr == nullptr)
17853 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17854 if (attr == nullptr)
17855 attr = dwarf2_attr (die, DW_AT_small, cu);
17856 }
17857
17858 /* Numerator and denominator of our fixed-point type's scaling factor.
17859 The default is a scaling factor of 1, which we use as a fallback
17860 when we are not able to decode it (problem with the debugging info,
17861 unsupported forms, bug in GDB, etc...). Using that as the default
17862 allows us to at least print the unscaled value, which might still
17863 be useful to a user. */
17864 gdb_mpz scale_num (1);
17865 gdb_mpz scale_denom (1);
17866
17867 if (attr == nullptr)
17868 {
17869 int offset = 0;
17870 if (suffix != nullptr
17871 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17872 &scale_denom)
17873 /* The number might be encoded as _nn_dd_nn_dd, where the
17874 second ratio is the 'small value. In this situation, we
17875 want the second value. */
17876 && (suffix[offset] != '_'
17877 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17878 &scale_denom)))
17879 {
17880 /* Found it. */
17881 }
17882 else
17883 {
17884 /* Scaling factor not found. Assume a scaling factor of 1,
17885 and hope for the best. At least the user will be able to
17886 see the encoded value. */
17887 scale_num = 1;
17888 scale_denom = 1;
17889 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17890 sect_offset_str (die->sect_off));
17891 }
17892 }
17893 else if (attr->name == DW_AT_binary_scale)
17894 {
17895 LONGEST scale_exp = attr->constant_value (0);
17896 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17897
17898 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
17899 }
17900 else if (attr->name == DW_AT_decimal_scale)
17901 {
17902 LONGEST scale_exp = attr->constant_value (0);
17903 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
17904
17905 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
17906 }
17907 else if (attr->name == DW_AT_small)
17908 {
17909 struct die_info *scale_die;
17910 struct dwarf2_cu *scale_cu = cu;
17911
17912 scale_die = follow_die_ref (die, attr, &scale_cu);
17913 if (scale_die->tag == DW_TAG_constant)
17914 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17915 &scale_num, &scale_denom);
17916 else
17917 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17918 " (DIE at %s)"),
17919 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17920 }
17921 else
17922 {
17923 complaint (_("unsupported scale attribute %s for fixed-point type"
17924 " (DIE at %s)"),
17925 dwarf_attr_name (attr->name),
17926 sect_offset_str (die->sect_off));
17927 }
17928
17929 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17930 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17931 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17932 mpq_canonicalize (scaling_factor.val);
17933 }
17934
17935 /* The gnat-encoding suffix for fixed point. */
17936
17937 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17938
17939 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17940 "XF" suffix of the name. The text after this is what encodes the
17941 'small and 'delta information. Otherwise, return nullptr. */
17942
17943 static const char *
17944 gnat_encoded_fixed_point_type_info (const char *name)
17945 {
17946 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17947 }
17948
17949 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17950 (which may be different from NAME) to the architecture back-end to allow
17951 it to guess the correct format if necessary. */
17952
17953 static struct type *
17954 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17955 const char *name_hint, enum bfd_endian byte_order)
17956 {
17957 struct gdbarch *gdbarch = objfile->arch ();
17958 const struct floatformat **format;
17959 struct type *type;
17960
17961 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17962 if (format)
17963 type = init_float_type (objfile, bits, name, format, byte_order);
17964 else
17965 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17966
17967 return type;
17968 }
17969
17970 /* Allocate an integer type of size BITS and name NAME. */
17971
17972 static struct type *
17973 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17974 int bits, int unsigned_p, const char *name)
17975 {
17976 struct type *type;
17977
17978 /* Versions of Intel's C Compiler generate an integer type called "void"
17979 instead of using DW_TAG_unspecified_type. This has been seen on
17980 at least versions 14, 17, and 18. */
17981 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17982 && strcmp (name, "void") == 0)
17983 type = objfile_type (objfile)->builtin_void;
17984 else
17985 type = init_integer_type (objfile, bits, unsigned_p, name);
17986
17987 return type;
17988 }
17989
17990 /* Return true if DIE has a DW_AT_small attribute whose value is
17991 a constant rational, where both the numerator and denominator
17992 are equal to zero.
17993
17994 CU is the DIE's Compilation Unit. */
17995
17996 static bool
17997 has_zero_over_zero_small_attribute (struct die_info *die,
17998 struct dwarf2_cu *cu)
17999 {
18000 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18001 if (attr == nullptr)
18002 return false;
18003
18004 struct dwarf2_cu *scale_cu = cu;
18005 struct die_info *scale_die
18006 = follow_die_ref (die, attr, &scale_cu);
18007
18008 if (scale_die->tag != DW_TAG_constant)
18009 return false;
18010
18011 gdb_mpz num (1), denom (1);
18012 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18013 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18014 }
18015
18016 /* Initialise and return a floating point type of size BITS suitable for
18017 use as a component of a complex number. The NAME_HINT is passed through
18018 when initialising the floating point type and is the name of the complex
18019 type.
18020
18021 As DWARF doesn't currently provide an explicit name for the components
18022 of a complex number, but it can be helpful to have these components
18023 named, we try to select a suitable name based on the size of the
18024 component. */
18025 static struct type *
18026 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18027 struct objfile *objfile,
18028 int bits, const char *name_hint,
18029 enum bfd_endian byte_order)
18030 {
18031 gdbarch *gdbarch = objfile->arch ();
18032 struct type *tt = nullptr;
18033
18034 /* Try to find a suitable floating point builtin type of size BITS.
18035 We're going to use the name of this type as the name for the complex
18036 target type that we are about to create. */
18037 switch (cu->per_cu->lang)
18038 {
18039 case language_fortran:
18040 switch (bits)
18041 {
18042 case 32:
18043 tt = builtin_f_type (gdbarch)->builtin_real;
18044 break;
18045 case 64:
18046 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18047 break;
18048 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18049 case 128:
18050 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18051 break;
18052 }
18053 break;
18054 default:
18055 switch (bits)
18056 {
18057 case 32:
18058 tt = builtin_type (gdbarch)->builtin_float;
18059 break;
18060 case 64:
18061 tt = builtin_type (gdbarch)->builtin_double;
18062 break;
18063 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18064 case 128:
18065 tt = builtin_type (gdbarch)->builtin_long_double;
18066 break;
18067 }
18068 break;
18069 }
18070
18071 /* If the type we found doesn't match the size we were looking for, then
18072 pretend we didn't find a type at all, the complex target type we
18073 create will then be nameless. */
18074 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18075 tt = nullptr;
18076
18077 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18078 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18079 }
18080
18081 /* Find a representation of a given base type and install
18082 it in the TYPE field of the die. */
18083
18084 static struct type *
18085 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18086 {
18087 struct objfile *objfile = cu->per_objfile->objfile;
18088 struct type *type;
18089 struct attribute *attr;
18090 int encoding = 0, bits = 0;
18091 const char *name;
18092 gdbarch *arch;
18093
18094 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18095 if (attr != nullptr && attr->form_is_constant ())
18096 encoding = attr->constant_value (0);
18097 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18098 if (attr != nullptr)
18099 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18100 name = dwarf2_name (die, cu);
18101 if (!name)
18102 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18103
18104 arch = objfile->arch ();
18105 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18106
18107 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18108 if (attr != nullptr && attr->form_is_constant ())
18109 {
18110 int endianity = attr->constant_value (0);
18111
18112 switch (endianity)
18113 {
18114 case DW_END_big:
18115 byte_order = BFD_ENDIAN_BIG;
18116 break;
18117 case DW_END_little:
18118 byte_order = BFD_ENDIAN_LITTLE;
18119 break;
18120 default:
18121 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18122 break;
18123 }
18124 }
18125
18126 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18127 && cu->per_cu->lang == language_ada
18128 && has_zero_over_zero_small_attribute (die, cu))
18129 {
18130 /* brobecker/2018-02-24: This is a fixed point type for which
18131 the scaling factor is represented as fraction whose value
18132 does not make sense (zero divided by zero), so we should
18133 normally never see these. However, there is a small category
18134 of fixed point types for which GNAT is unable to provide
18135 the scaling factor via the standard DWARF mechanisms, and
18136 for which the info is provided via the GNAT encodings instead.
18137 This is likely what this DIE is about. */
18138 encoding = (encoding == DW_ATE_signed_fixed
18139 ? DW_ATE_signed
18140 : DW_ATE_unsigned);
18141 }
18142
18143 /* With GNAT encodings, fixed-point information will be encoded in
18144 the type name. Note that this can also occur with the above
18145 zero-over-zero case, which is why this is a separate "if" rather
18146 than an "else if". */
18147 const char *gnat_encoding_suffix = nullptr;
18148 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18149 && cu->per_cu->lang == language_ada
18150 && name != nullptr)
18151 {
18152 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18153 if (gnat_encoding_suffix != nullptr)
18154 {
18155 gdb_assert (startswith (gnat_encoding_suffix,
18156 GNAT_FIXED_POINT_SUFFIX));
18157 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18158 name, gnat_encoding_suffix - name);
18159 /* Use -1 here so that SUFFIX points at the "_" after the
18160 "XF". */
18161 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18162
18163 encoding = (encoding == DW_ATE_signed
18164 ? DW_ATE_signed_fixed
18165 : DW_ATE_unsigned_fixed);
18166 }
18167 }
18168
18169 switch (encoding)
18170 {
18171 case DW_ATE_address:
18172 /* Turn DW_ATE_address into a void * pointer. */
18173 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18174 type = init_pointer_type (objfile, bits, name, type);
18175 break;
18176 case DW_ATE_boolean:
18177 type = init_boolean_type (objfile, bits, 1, name);
18178 break;
18179 case DW_ATE_complex_float:
18180 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18181 byte_order);
18182 if (type->code () == TYPE_CODE_ERROR)
18183 {
18184 if (name == nullptr)
18185 {
18186 struct obstack *obstack
18187 = &cu->per_objfile->objfile->objfile_obstack;
18188 name = obconcat (obstack, "_Complex ", type->name (),
18189 nullptr);
18190 }
18191 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18192 }
18193 else
18194 type = init_complex_type (name, type);
18195 break;
18196 case DW_ATE_decimal_float:
18197 type = init_decfloat_type (objfile, bits, name);
18198 break;
18199 case DW_ATE_float:
18200 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18201 break;
18202 case DW_ATE_signed:
18203 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18204 break;
18205 case DW_ATE_unsigned:
18206 if (cu->per_cu->lang == language_fortran
18207 && name
18208 && startswith (name, "character("))
18209 type = init_character_type (objfile, bits, 1, name);
18210 else
18211 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18212 break;
18213 case DW_ATE_signed_char:
18214 if (cu->per_cu->lang == language_ada
18215 || cu->per_cu->lang == language_m2
18216 || cu->per_cu->lang == language_pascal
18217 || cu->per_cu->lang == language_fortran)
18218 type = init_character_type (objfile, bits, 0, name);
18219 else
18220 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18221 break;
18222 case DW_ATE_unsigned_char:
18223 if (cu->per_cu->lang == language_ada
18224 || cu->per_cu->lang == language_m2
18225 || cu->per_cu->lang == language_pascal
18226 || cu->per_cu->lang == language_fortran
18227 || cu->per_cu->lang == language_rust)
18228 type = init_character_type (objfile, bits, 1, name);
18229 else
18230 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18231 break;
18232 case DW_ATE_UTF:
18233 {
18234 type = init_character_type (objfile, bits, 1, name);
18235 return set_die_type (die, type, cu);
18236 }
18237 break;
18238 case DW_ATE_signed_fixed:
18239 type = init_fixed_point_type (objfile, bits, 0, name);
18240 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18241 break;
18242 case DW_ATE_unsigned_fixed:
18243 type = init_fixed_point_type (objfile, bits, 1, name);
18244 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18245 break;
18246
18247 default:
18248 complaint (_("unsupported DW_AT_encoding: '%s'"),
18249 dwarf_type_encoding_name (encoding));
18250 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18251 break;
18252 }
18253
18254 if (type->code () == TYPE_CODE_INT
18255 && name != nullptr
18256 && strcmp (name, "char") == 0)
18257 type->set_has_no_signedness (true);
18258
18259 maybe_set_alignment (cu, die, type);
18260
18261 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18262
18263 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18264 {
18265 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18266 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18267 {
18268 unsigned real_bit_size = attr->as_unsigned ();
18269 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18270 /* Only use the attributes if they make sense together. */
18271 if (attr == nullptr
18272 || (attr->as_unsigned () + real_bit_size
18273 <= 8 * TYPE_LENGTH (type)))
18274 {
18275 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18276 = real_bit_size;
18277 if (attr != nullptr)
18278 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18279 = attr->as_unsigned ();
18280 }
18281 }
18282 }
18283
18284 return set_die_type (die, type, cu);
18285 }
18286
18287 /* A helper function that returns the name of DIE, if it refers to a
18288 variable declaration. */
18289
18290 static const char *
18291 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18292 {
18293 if (die->tag != DW_TAG_variable)
18294 return nullptr;
18295
18296 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18297 if (attr == nullptr || !attr->as_boolean ())
18298 return nullptr;
18299
18300 attr = dwarf2_attr (die, DW_AT_name, cu);
18301 if (attr == nullptr)
18302 return nullptr;
18303 return attr->as_string ();
18304 }
18305
18306 /* Parse dwarf attribute if it's a block, reference or constant and put the
18307 resulting value of the attribute into struct bound_prop.
18308 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18309
18310 static int
18311 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18312 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18313 struct type *default_type)
18314 {
18315 struct dwarf2_property_baton *baton;
18316 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18317 struct objfile *objfile = per_objfile->objfile;
18318 struct obstack *obstack = &objfile->objfile_obstack;
18319
18320 gdb_assert (default_type != NULL);
18321
18322 if (attr == NULL || prop == NULL)
18323 return 0;
18324
18325 if (attr->form_is_block ())
18326 {
18327 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18328 baton->property_type = default_type;
18329 baton->locexpr.per_cu = cu->per_cu;
18330 baton->locexpr.per_objfile = per_objfile;
18331
18332 struct dwarf_block *block;
18333 if (attr->form == DW_FORM_data16)
18334 {
18335 size_t data_size = 16;
18336 block = XOBNEW (obstack, struct dwarf_block);
18337 block->size = (data_size
18338 + 2 /* Extra bytes for DW_OP and arg. */);
18339 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18340 data[0] = DW_OP_implicit_value;
18341 data[1] = data_size;
18342 memcpy (&data[2], attr->as_block ()->data, data_size);
18343 block->data = data;
18344 }
18345 else
18346 block = attr->as_block ();
18347
18348 baton->locexpr.size = block->size;
18349 baton->locexpr.data = block->data;
18350 switch (attr->name)
18351 {
18352 case DW_AT_string_length:
18353 baton->locexpr.is_reference = true;
18354 break;
18355 default:
18356 baton->locexpr.is_reference = false;
18357 break;
18358 }
18359
18360 prop->set_locexpr (baton);
18361 gdb_assert (prop->baton () != NULL);
18362 }
18363 else if (attr->form_is_ref ())
18364 {
18365 struct dwarf2_cu *target_cu = cu;
18366 struct die_info *target_die;
18367 struct attribute *target_attr;
18368
18369 target_die = follow_die_ref (die, attr, &target_cu);
18370 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18371 if (target_attr == NULL)
18372 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18373 target_cu);
18374 if (target_attr == nullptr)
18375 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
18376 target_cu);
18377 if (target_attr == NULL)
18378 {
18379 const char *name = var_decl_name (target_die, target_cu);
18380 if (name != nullptr)
18381 {
18382 prop->set_variable_name (name);
18383 return 1;
18384 }
18385 return 0;
18386 }
18387
18388 switch (target_attr->name)
18389 {
18390 case DW_AT_location:
18391 if (target_attr->form_is_section_offset ())
18392 {
18393 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18394 baton->property_type = die_type (target_die, target_cu);
18395 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18396 prop->set_loclist (baton);
18397 gdb_assert (prop->baton () != NULL);
18398 }
18399 else if (target_attr->form_is_block ())
18400 {
18401 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18402 baton->property_type = die_type (target_die, target_cu);
18403 baton->locexpr.per_cu = cu->per_cu;
18404 baton->locexpr.per_objfile = per_objfile;
18405 struct dwarf_block *block = target_attr->as_block ();
18406 baton->locexpr.size = block->size;
18407 baton->locexpr.data = block->data;
18408 baton->locexpr.is_reference = true;
18409 prop->set_locexpr (baton);
18410 gdb_assert (prop->baton () != NULL);
18411 }
18412 else
18413 {
18414 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18415 "dynamic property");
18416 return 0;
18417 }
18418 break;
18419 case DW_AT_data_member_location:
18420 case DW_AT_data_bit_offset:
18421 {
18422 LONGEST offset;
18423
18424 if (!handle_member_location (target_die, target_cu, &offset))
18425 return 0;
18426
18427 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18428 baton->property_type = read_type_die (target_die->parent,
18429 target_cu);
18430 baton->offset_info.offset = offset;
18431 baton->offset_info.type = die_type (target_die, target_cu);
18432 prop->set_addr_offset (baton);
18433 break;
18434 }
18435 }
18436 }
18437 else if (attr->form_is_constant ())
18438 prop->set_const_val (attr->constant_value (0));
18439 else if (attr->form_is_section_offset ())
18440 {
18441 switch (attr->name)
18442 {
18443 case DW_AT_string_length:
18444 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18445 baton->property_type = default_type;
18446 fill_in_loclist_baton (cu, &baton->loclist, attr);
18447 prop->set_loclist (baton);
18448 gdb_assert (prop->baton () != NULL);
18449 break;
18450 default:
18451 goto invalid;
18452 }
18453 }
18454 else
18455 goto invalid;
18456
18457 return 1;
18458
18459 invalid:
18460 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18461 dwarf2_name (die, cu));
18462 return 0;
18463 }
18464
18465 /* See read.h. */
18466
18467 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18468 present (which is valid) then compute the default type based on the
18469 compilation units address size. */
18470
18471 static struct type *
18472 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18473 {
18474 struct type *index_type = die_type (die, cu);
18475
18476 /* Dwarf-2 specifications explicitly allows to create subrange types
18477 without specifying a base type.
18478 In that case, the base type must be set to the type of
18479 the lower bound, upper bound or count, in that order, if any of these
18480 three attributes references an object that has a type.
18481 If no base type is found, the Dwarf-2 specifications say that
18482 a signed integer type of size equal to the size of an address should
18483 be used.
18484 For the following C code: `extern char gdb_int [];'
18485 GCC produces an empty range DIE.
18486 FIXME: muller/2010-05-28: Possible references to object for low bound,
18487 high bound or count are not yet handled by this code. */
18488 if (index_type->code () == TYPE_CODE_VOID)
18489 index_type = cu->addr_sized_int_type (false);
18490
18491 return index_type;
18492 }
18493
18494 /* Read the given DW_AT_subrange DIE. */
18495
18496 static struct type *
18497 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18498 {
18499 struct type *base_type, *orig_base_type;
18500 struct type *range_type;
18501 struct attribute *attr;
18502 struct dynamic_prop low, high;
18503 int low_default_is_valid;
18504 int high_bound_is_count = 0;
18505 const char *name;
18506 ULONGEST negative_mask;
18507
18508 orig_base_type = read_subrange_index_type (die, cu);
18509
18510 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18511 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18512 creating the range type, but we use the result of check_typedef
18513 when examining properties of the type. */
18514 base_type = check_typedef (orig_base_type);
18515
18516 /* The die_type call above may have already set the type for this DIE. */
18517 range_type = get_die_type (die, cu);
18518 if (range_type)
18519 return range_type;
18520
18521 high.set_const_val (0);
18522
18523 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18524 omitting DW_AT_lower_bound. */
18525 switch (cu->per_cu->lang)
18526 {
18527 case language_c:
18528 case language_cplus:
18529 low.set_const_val (0);
18530 low_default_is_valid = 1;
18531 break;
18532 case language_fortran:
18533 low.set_const_val (1);
18534 low_default_is_valid = 1;
18535 break;
18536 case language_d:
18537 case language_objc:
18538 case language_rust:
18539 low.set_const_val (0);
18540 low_default_is_valid = (cu->header.version >= 4);
18541 break;
18542 case language_ada:
18543 case language_m2:
18544 case language_pascal:
18545 low.set_const_val (1);
18546 low_default_is_valid = (cu->header.version >= 4);
18547 break;
18548 default:
18549 low.set_const_val (0);
18550 low_default_is_valid = 0;
18551 break;
18552 }
18553
18554 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18555 if (attr != nullptr)
18556 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18557 else if (!low_default_is_valid)
18558 complaint (_("Missing DW_AT_lower_bound "
18559 "- DIE at %s [in module %s]"),
18560 sect_offset_str (die->sect_off),
18561 objfile_name (cu->per_objfile->objfile));
18562
18563 struct attribute *attr_ub, *attr_count;
18564 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18565 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18566 {
18567 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18568 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18569 {
18570 /* If bounds are constant do the final calculation here. */
18571 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18572 high.set_const_val (low.const_val () + high.const_val () - 1);
18573 else
18574 high_bound_is_count = 1;
18575 }
18576 else
18577 {
18578 if (attr_ub != NULL)
18579 complaint (_("Unresolved DW_AT_upper_bound "
18580 "- DIE at %s [in module %s]"),
18581 sect_offset_str (die->sect_off),
18582 objfile_name (cu->per_objfile->objfile));
18583 if (attr_count != NULL)
18584 complaint (_("Unresolved DW_AT_count "
18585 "- DIE at %s [in module %s]"),
18586 sect_offset_str (die->sect_off),
18587 objfile_name (cu->per_objfile->objfile));
18588 }
18589 }
18590
18591 LONGEST bias = 0;
18592 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18593 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18594 bias = bias_attr->constant_value (0);
18595
18596 /* Normally, the DWARF producers are expected to use a signed
18597 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18598 But this is unfortunately not always the case, as witnessed
18599 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18600 is used instead. To work around that ambiguity, we treat
18601 the bounds as signed, and thus sign-extend their values, when
18602 the base type is signed. */
18603 negative_mask =
18604 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18605 if (low.kind () == PROP_CONST
18606 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18607 low.set_const_val (low.const_val () | negative_mask);
18608 if (high.kind () == PROP_CONST
18609 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18610 high.set_const_val (high.const_val () | negative_mask);
18611
18612 /* Check for bit and byte strides. */
18613 struct dynamic_prop byte_stride_prop;
18614 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18615 if (attr_byte_stride != nullptr)
18616 {
18617 struct type *prop_type = cu->addr_sized_int_type (false);
18618 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18619 prop_type);
18620 }
18621
18622 struct dynamic_prop bit_stride_prop;
18623 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18624 if (attr_bit_stride != nullptr)
18625 {
18626 /* It only makes sense to have either a bit or byte stride. */
18627 if (attr_byte_stride != nullptr)
18628 {
18629 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18630 "- DIE at %s [in module %s]"),
18631 sect_offset_str (die->sect_off),
18632 objfile_name (cu->per_objfile->objfile));
18633 attr_bit_stride = nullptr;
18634 }
18635 else
18636 {
18637 struct type *prop_type = cu->addr_sized_int_type (false);
18638 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18639 prop_type);
18640 }
18641 }
18642
18643 if (attr_byte_stride != nullptr
18644 || attr_bit_stride != nullptr)
18645 {
18646 bool byte_stride_p = (attr_byte_stride != nullptr);
18647 struct dynamic_prop *stride
18648 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18649
18650 range_type
18651 = create_range_type_with_stride (NULL, orig_base_type, &low,
18652 &high, bias, stride, byte_stride_p);
18653 }
18654 else
18655 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18656
18657 if (high_bound_is_count)
18658 range_type->bounds ()->flag_upper_bound_is_count = 1;
18659
18660 /* Ada expects an empty array on no boundary attributes. */
18661 if (attr == NULL && cu->per_cu->lang != language_ada)
18662 range_type->bounds ()->high.set_undefined ();
18663
18664 name = dwarf2_name (die, cu);
18665 if (name)
18666 range_type->set_name (name);
18667
18668 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18669 if (attr != nullptr)
18670 TYPE_LENGTH (range_type) = attr->constant_value (0);
18671
18672 maybe_set_alignment (cu, die, range_type);
18673
18674 set_die_type (die, range_type, cu);
18675
18676 /* set_die_type should be already done. */
18677 set_descriptive_type (range_type, die, cu);
18678
18679 return range_type;
18680 }
18681
18682 static struct type *
18683 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18684 {
18685 struct type *type;
18686
18687 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18688 type->set_name (dwarf2_name (die, cu));
18689
18690 /* In Ada, an unspecified type is typically used when the description
18691 of the type is deferred to a different unit. When encountering
18692 such a type, we treat it as a stub, and try to resolve it later on,
18693 when needed. */
18694 if (cu->per_cu->lang == language_ada)
18695 type->set_is_stub (true);
18696
18697 return set_die_type (die, type, cu);
18698 }
18699
18700 /* Read a single die and all its descendents. Set the die's sibling
18701 field to NULL; set other fields in the die correctly, and set all
18702 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18703 location of the info_ptr after reading all of those dies. PARENT
18704 is the parent of the die in question. */
18705
18706 static struct die_info *
18707 read_die_and_children (const struct die_reader_specs *reader,
18708 const gdb_byte *info_ptr,
18709 const gdb_byte **new_info_ptr,
18710 struct die_info *parent)
18711 {
18712 struct die_info *die;
18713 const gdb_byte *cur_ptr;
18714
18715 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18716 if (die == NULL)
18717 {
18718 *new_info_ptr = cur_ptr;
18719 return NULL;
18720 }
18721 store_in_ref_table (die, reader->cu);
18722
18723 if (die->has_children)
18724 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18725 else
18726 {
18727 die->child = NULL;
18728 *new_info_ptr = cur_ptr;
18729 }
18730
18731 die->sibling = NULL;
18732 die->parent = parent;
18733 return die;
18734 }
18735
18736 /* Read a die, all of its descendents, and all of its siblings; set
18737 all of the fields of all of the dies correctly. Arguments are as
18738 in read_die_and_children. */
18739
18740 static struct die_info *
18741 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18742 const gdb_byte *info_ptr,
18743 const gdb_byte **new_info_ptr,
18744 struct die_info *parent)
18745 {
18746 struct die_info *first_die, *last_sibling;
18747 const gdb_byte *cur_ptr;
18748
18749 cur_ptr = info_ptr;
18750 first_die = last_sibling = NULL;
18751
18752 while (1)
18753 {
18754 struct die_info *die
18755 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18756
18757 if (die == NULL)
18758 {
18759 *new_info_ptr = cur_ptr;
18760 return first_die;
18761 }
18762
18763 if (!first_die)
18764 first_die = die;
18765 else
18766 last_sibling->sibling = die;
18767
18768 last_sibling = die;
18769 }
18770 }
18771
18772 /* Read a die, all of its descendents, and all of its siblings; set
18773 all of the fields of all of the dies correctly. Arguments are as
18774 in read_die_and_children.
18775 This the main entry point for reading a DIE and all its children. */
18776
18777 static struct die_info *
18778 read_die_and_siblings (const struct die_reader_specs *reader,
18779 const gdb_byte *info_ptr,
18780 const gdb_byte **new_info_ptr,
18781 struct die_info *parent)
18782 {
18783 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18784 new_info_ptr, parent);
18785
18786 if (dwarf_die_debug)
18787 {
18788 fprintf_unfiltered (gdb_stdlog,
18789 "Read die from %s@0x%x of %s:\n",
18790 reader->die_section->get_name (),
18791 (unsigned) (info_ptr - reader->die_section->buffer),
18792 bfd_get_filename (reader->abfd));
18793 dump_die (die, dwarf_die_debug);
18794 }
18795
18796 return die;
18797 }
18798
18799 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18800 attributes.
18801 The caller is responsible for filling in the extra attributes
18802 and updating (*DIEP)->num_attrs.
18803 Set DIEP to point to a newly allocated die with its information,
18804 except for its child, sibling, and parent fields. */
18805
18806 static const gdb_byte *
18807 read_full_die_1 (const struct die_reader_specs *reader,
18808 struct die_info **diep, const gdb_byte *info_ptr,
18809 int num_extra_attrs)
18810 {
18811 unsigned int abbrev_number, bytes_read, i;
18812 const struct abbrev_info *abbrev;
18813 struct die_info *die;
18814 struct dwarf2_cu *cu = reader->cu;
18815 bfd *abfd = reader->abfd;
18816
18817 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18818 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18819 info_ptr += bytes_read;
18820 if (!abbrev_number)
18821 {
18822 *diep = NULL;
18823 return info_ptr;
18824 }
18825
18826 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18827 if (!abbrev)
18828 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18829 abbrev_number,
18830 bfd_get_filename (abfd));
18831
18832 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18833 die->sect_off = sect_off;
18834 die->tag = abbrev->tag;
18835 die->abbrev = abbrev_number;
18836 die->has_children = abbrev->has_children;
18837
18838 /* Make the result usable.
18839 The caller needs to update num_attrs after adding the extra
18840 attributes. */
18841 die->num_attrs = abbrev->num_attrs;
18842
18843 bool any_need_reprocess = false;
18844 for (i = 0; i < abbrev->num_attrs; ++i)
18845 {
18846 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18847 info_ptr);
18848 if (die->attrs[i].requires_reprocessing_p ())
18849 any_need_reprocess = true;
18850 }
18851
18852 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18853 if (attr != nullptr && attr->form_is_unsigned ())
18854 cu->str_offsets_base = attr->as_unsigned ();
18855
18856 attr = die->attr (DW_AT_loclists_base);
18857 if (attr != nullptr)
18858 cu->loclist_base = attr->as_unsigned ();
18859
18860 auto maybe_addr_base = die->addr_base ();
18861 if (maybe_addr_base.has_value ())
18862 cu->addr_base = *maybe_addr_base;
18863
18864 attr = die->attr (DW_AT_rnglists_base);
18865 if (attr != nullptr)
18866 cu->rnglists_base = attr->as_unsigned ();
18867
18868 if (any_need_reprocess)
18869 {
18870 for (i = 0; i < abbrev->num_attrs; ++i)
18871 {
18872 if (die->attrs[i].requires_reprocessing_p ())
18873 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18874 }
18875 }
18876 *diep = die;
18877 return info_ptr;
18878 }
18879
18880 /* Read a die and all its attributes.
18881 Set DIEP to point to a newly allocated die with its information,
18882 except for its child, sibling, and parent fields. */
18883
18884 static const gdb_byte *
18885 read_full_die (const struct die_reader_specs *reader,
18886 struct die_info **diep, const gdb_byte *info_ptr)
18887 {
18888 const gdb_byte *result;
18889
18890 result = read_full_die_1 (reader, diep, info_ptr, 0);
18891
18892 if (dwarf_die_debug)
18893 {
18894 fprintf_unfiltered (gdb_stdlog,
18895 "Read die from %s@0x%x of %s:\n",
18896 reader->die_section->get_name (),
18897 (unsigned) (info_ptr - reader->die_section->buffer),
18898 bfd_get_filename (reader->abfd));
18899 dump_die (*diep, dwarf_die_debug);
18900 }
18901
18902 return result;
18903 }
18904 \f
18905
18906 /* Returns nonzero if TAG represents a type that we might generate a partial
18907 symbol for. */
18908
18909 static int
18910 is_type_tag_for_partial (int tag, enum language lang)
18911 {
18912 switch (tag)
18913 {
18914 #if 0
18915 /* Some types that would be reasonable to generate partial symbols for,
18916 that we don't at present. Note that normally this does not
18917 matter, mainly because C compilers don't give names to these
18918 types, but instead emit DW_TAG_typedef. */
18919 case DW_TAG_file_type:
18920 case DW_TAG_ptr_to_member_type:
18921 case DW_TAG_set_type:
18922 case DW_TAG_string_type:
18923 case DW_TAG_subroutine_type:
18924 #endif
18925
18926 /* GNAT may emit an array with a name, but no typedef, so we
18927 need to make a symbol in this case. */
18928 case DW_TAG_array_type:
18929 return lang == language_ada;
18930
18931 case DW_TAG_base_type:
18932 case DW_TAG_class_type:
18933 case DW_TAG_interface_type:
18934 case DW_TAG_enumeration_type:
18935 case DW_TAG_structure_type:
18936 case DW_TAG_subrange_type:
18937 case DW_TAG_typedef:
18938 case DW_TAG_union_type:
18939 return 1;
18940 default:
18941 return 0;
18942 }
18943 }
18944
18945 /* Load all DIEs that are interesting for partial symbols into memory. */
18946
18947 static struct partial_die_info *
18948 load_partial_dies (const struct die_reader_specs *reader,
18949 const gdb_byte *info_ptr, int building_psymtab)
18950 {
18951 struct dwarf2_cu *cu = reader->cu;
18952 struct objfile *objfile = cu->per_objfile->objfile;
18953 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18954 unsigned int bytes_read;
18955 unsigned int load_all = 0;
18956 int nesting_level = 1;
18957
18958 parent_die = NULL;
18959 last_die = NULL;
18960
18961 gdb_assert (cu->per_cu != NULL);
18962 if (cu->load_all_dies)
18963 load_all = 1;
18964
18965 cu->partial_dies
18966 = htab_create_alloc_ex (cu->header.length / 12,
18967 partial_die_hash,
18968 partial_die_eq,
18969 NULL,
18970 &cu->comp_unit_obstack,
18971 hashtab_obstack_allocate,
18972 dummy_obstack_deallocate);
18973
18974 while (1)
18975 {
18976 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18977 &bytes_read);
18978
18979 /* A NULL abbrev means the end of a series of children. */
18980 if (abbrev == NULL)
18981 {
18982 if (--nesting_level == 0)
18983 return first_die;
18984
18985 info_ptr += bytes_read;
18986 last_die = parent_die;
18987 parent_die = parent_die->die_parent;
18988 continue;
18989 }
18990
18991 /* Check for template arguments. We never save these; if
18992 they're seen, we just mark the parent, and go on our way. */
18993 if (parent_die != NULL
18994 && cu->per_cu->lang == language_cplus
18995 && (abbrev->tag == DW_TAG_template_type_param
18996 || abbrev->tag == DW_TAG_template_value_param))
18997 {
18998 parent_die->has_template_arguments = 1;
18999
19000 if (!load_all)
19001 {
19002 /* We don't need a partial DIE for the template argument. */
19003 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19004 continue;
19005 }
19006 }
19007
19008 /* We only recurse into c++ subprograms looking for template arguments.
19009 Skip their other children. */
19010 if (!load_all
19011 && cu->per_cu->lang == language_cplus
19012 && parent_die != NULL
19013 && parent_die->tag == DW_TAG_subprogram
19014 && abbrev->tag != DW_TAG_inlined_subroutine)
19015 {
19016 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19017 continue;
19018 }
19019
19020 /* Check whether this DIE is interesting enough to save. Normally
19021 we would not be interested in members here, but there may be
19022 later variables referencing them via DW_AT_specification (for
19023 static members). */
19024 if (!load_all
19025 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
19026 && abbrev->tag != DW_TAG_constant
19027 && abbrev->tag != DW_TAG_enumerator
19028 && abbrev->tag != DW_TAG_subprogram
19029 && abbrev->tag != DW_TAG_inlined_subroutine
19030 && abbrev->tag != DW_TAG_lexical_block
19031 && abbrev->tag != DW_TAG_variable
19032 && abbrev->tag != DW_TAG_namespace
19033 && abbrev->tag != DW_TAG_module
19034 && abbrev->tag != DW_TAG_member
19035 && abbrev->tag != DW_TAG_imported_unit
19036 && abbrev->tag != DW_TAG_imported_declaration)
19037 {
19038 /* Otherwise we skip to the next sibling, if any. */
19039 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19040 continue;
19041 }
19042
19043 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19044 abbrev);
19045
19046 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19047
19048 /* This two-pass algorithm for processing partial symbols has a
19049 high cost in cache pressure. Thus, handle some simple cases
19050 here which cover the majority of C partial symbols. DIEs
19051 which neither have specification tags in them, nor could have
19052 specification tags elsewhere pointing at them, can simply be
19053 processed and discarded.
19054
19055 This segment is also optional; scan_partial_symbols and
19056 add_partial_symbol will handle these DIEs if we chain
19057 them in normally. When compilers which do not emit large
19058 quantities of duplicate debug information are more common,
19059 this code can probably be removed. */
19060
19061 /* Any complete simple types at the top level (pretty much all
19062 of them, for a language without namespaces), can be processed
19063 directly. */
19064 if (parent_die == NULL
19065 && pdi.has_specification == 0
19066 && pdi.is_declaration == 0
19067 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19068 || pdi.tag == DW_TAG_base_type
19069 || pdi.tag == DW_TAG_array_type
19070 || pdi.tag == DW_TAG_subrange_type))
19071 {
19072 if (building_psymtab && pdi.raw_name != NULL)
19073 add_partial_symbol (&pdi, cu);
19074
19075 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19076 continue;
19077 }
19078
19079 /* The exception for DW_TAG_typedef with has_children above is
19080 a workaround of GCC PR debug/47510. In the case of this complaint
19081 type_name_or_error will error on such types later.
19082
19083 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19084 it could not find the child DIEs referenced later, this is checked
19085 above. In correct DWARF DW_TAG_typedef should have no children. */
19086
19087 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19088 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19089 "- DIE at %s [in module %s]"),
19090 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19091
19092 /* If we're at the second level, and we're an enumerator, and
19093 our parent has no specification (meaning possibly lives in a
19094 namespace elsewhere), then we can add the partial symbol now
19095 instead of queueing it. */
19096 if (pdi.tag == DW_TAG_enumerator
19097 && parent_die != NULL
19098 && parent_die->die_parent == NULL
19099 && parent_die->tag == DW_TAG_enumeration_type
19100 && parent_die->has_specification == 0)
19101 {
19102 if (pdi.raw_name == NULL)
19103 complaint (_("malformed enumerator DIE ignored"));
19104 else if (building_psymtab)
19105 add_partial_symbol (&pdi, cu);
19106
19107 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19108 continue;
19109 }
19110
19111 struct partial_die_info *part_die
19112 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19113
19114 /* We'll save this DIE so link it in. */
19115 part_die->die_parent = parent_die;
19116 part_die->die_sibling = NULL;
19117 part_die->die_child = NULL;
19118
19119 if (last_die && last_die == parent_die)
19120 last_die->die_child = part_die;
19121 else if (last_die)
19122 last_die->die_sibling = part_die;
19123
19124 last_die = part_die;
19125
19126 if (first_die == NULL)
19127 first_die = part_die;
19128
19129 /* Maybe add the DIE to the hash table. Not all DIEs that we
19130 find interesting need to be in the hash table, because we
19131 also have the parent/sibling/child chains; only those that we
19132 might refer to by offset later during partial symbol reading.
19133
19134 For now this means things that might have be the target of a
19135 DW_AT_specification, DW_AT_abstract_origin, or
19136 DW_AT_extension. DW_AT_extension will refer only to
19137 namespaces; DW_AT_abstract_origin refers to functions (and
19138 many things under the function DIE, but we do not recurse
19139 into function DIEs during partial symbol reading) and
19140 possibly variables as well; DW_AT_specification refers to
19141 declarations. Declarations ought to have the DW_AT_declaration
19142 flag. It happens that GCC forgets to put it in sometimes, but
19143 only for functions, not for types.
19144
19145 Adding more things than necessary to the hash table is harmless
19146 except for the performance cost. Adding too few will result in
19147 wasted time in find_partial_die, when we reread the compilation
19148 unit with load_all_dies set. */
19149
19150 if (load_all
19151 || abbrev->tag == DW_TAG_constant
19152 || abbrev->tag == DW_TAG_subprogram
19153 || abbrev->tag == DW_TAG_variable
19154 || abbrev->tag == DW_TAG_namespace
19155 || part_die->is_declaration)
19156 {
19157 void **slot;
19158
19159 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19160 to_underlying (part_die->sect_off),
19161 INSERT);
19162 *slot = part_die;
19163 }
19164
19165 /* For some DIEs we want to follow their children (if any). For C
19166 we have no reason to follow the children of structures; for other
19167 languages we have to, so that we can get at method physnames
19168 to infer fully qualified class names, for DW_AT_specification,
19169 and for C++ template arguments. For C++, we also look one level
19170 inside functions to find template arguments (if the name of the
19171 function does not already contain the template arguments).
19172
19173 For Ada and Fortran, we need to scan the children of subprograms
19174 and lexical blocks as well because these languages allow the
19175 definition of nested entities that could be interesting for the
19176 debugger, such as nested subprograms for instance. */
19177 if (last_die->has_children
19178 && (load_all
19179 || last_die->tag == DW_TAG_namespace
19180 || last_die->tag == DW_TAG_module
19181 || last_die->tag == DW_TAG_enumeration_type
19182 || (cu->per_cu->lang == language_cplus
19183 && last_die->tag == DW_TAG_subprogram
19184 && (last_die->raw_name == NULL
19185 || strchr (last_die->raw_name, '<') == NULL))
19186 || (cu->per_cu->lang != language_c
19187 && (last_die->tag == DW_TAG_class_type
19188 || last_die->tag == DW_TAG_interface_type
19189 || last_die->tag == DW_TAG_structure_type
19190 || last_die->tag == DW_TAG_union_type))
19191 || ((cu->per_cu->lang == language_ada
19192 || cu->per_cu->lang == language_fortran)
19193 && (last_die->tag == DW_TAG_subprogram
19194 || last_die->tag == DW_TAG_lexical_block))))
19195 {
19196 nesting_level++;
19197 parent_die = last_die;
19198 continue;
19199 }
19200
19201 /* Otherwise we skip to the next sibling, if any. */
19202 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19203
19204 /* Back to the top, do it again. */
19205 }
19206 }
19207
19208 partial_die_info::partial_die_info (sect_offset sect_off_,
19209 const struct abbrev_info *abbrev)
19210 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19211 {
19212 }
19213
19214 /* See class definition. */
19215
19216 const char *
19217 partial_die_info::name (dwarf2_cu *cu)
19218 {
19219 if (!canonical_name && raw_name != nullptr)
19220 {
19221 struct objfile *objfile = cu->per_objfile->objfile;
19222 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19223 canonical_name = 1;
19224 }
19225
19226 return raw_name;
19227 }
19228
19229 /* Read a minimal amount of information into the minimal die structure.
19230 INFO_PTR should point just after the initial uleb128 of a DIE. */
19231
19232 const gdb_byte *
19233 partial_die_info::read (const struct die_reader_specs *reader,
19234 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19235 {
19236 struct dwarf2_cu *cu = reader->cu;
19237 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19238 unsigned int i;
19239 int has_low_pc_attr = 0;
19240 int has_high_pc_attr = 0;
19241 int high_pc_relative = 0;
19242
19243 for (i = 0; i < abbrev.num_attrs; ++i)
19244 {
19245 attribute attr;
19246 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19247 /* String and address offsets that need to do the reprocessing have
19248 already been read at this point, so there is no need to wait until
19249 the loop terminates to do the reprocessing. */
19250 if (attr.requires_reprocessing_p ())
19251 read_attribute_reprocess (reader, &attr, tag);
19252 /* Store the data if it is of an attribute we want to keep in a
19253 partial symbol table. */
19254 switch (attr.name)
19255 {
19256 case DW_AT_name:
19257 switch (tag)
19258 {
19259 case DW_TAG_compile_unit:
19260 case DW_TAG_partial_unit:
19261 case DW_TAG_type_unit:
19262 /* Compilation units have a DW_AT_name that is a filename, not
19263 a source language identifier. */
19264 case DW_TAG_enumeration_type:
19265 case DW_TAG_enumerator:
19266 /* These tags always have simple identifiers already; no need
19267 to canonicalize them. */
19268 canonical_name = 1;
19269 raw_name = attr.as_string ();
19270 break;
19271 default:
19272 canonical_name = 0;
19273 raw_name = attr.as_string ();
19274 break;
19275 }
19276 break;
19277 case DW_AT_linkage_name:
19278 case DW_AT_MIPS_linkage_name:
19279 /* Note that both forms of linkage name might appear. We
19280 assume they will be the same, and we only store the last
19281 one we see. */
19282 linkage_name = attr.as_string ();
19283 break;
19284 case DW_AT_low_pc:
19285 has_low_pc_attr = 1;
19286 lowpc = attr.as_address ();
19287 break;
19288 case DW_AT_high_pc:
19289 has_high_pc_attr = 1;
19290 highpc = attr.as_address ();
19291 if (cu->header.version >= 4 && attr.form_is_constant ())
19292 high_pc_relative = 1;
19293 break;
19294 case DW_AT_location:
19295 /* Support the .debug_loc offsets. */
19296 if (attr.form_is_block ())
19297 {
19298 d.locdesc = attr.as_block ();
19299 }
19300 else if (attr.form_is_section_offset ())
19301 {
19302 dwarf2_complex_location_expr_complaint ();
19303 }
19304 else
19305 {
19306 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19307 "partial symbol information");
19308 }
19309 break;
19310 case DW_AT_external:
19311 is_external = attr.as_boolean ();
19312 break;
19313 case DW_AT_declaration:
19314 is_declaration = attr.as_boolean ();
19315 break;
19316 case DW_AT_type:
19317 has_type = 1;
19318 break;
19319 case DW_AT_abstract_origin:
19320 case DW_AT_specification:
19321 case DW_AT_extension:
19322 has_specification = 1;
19323 spec_offset = attr.get_ref_die_offset ();
19324 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19325 || cu->per_cu->is_dwz);
19326 break;
19327 case DW_AT_sibling:
19328 /* Ignore absolute siblings, they might point outside of
19329 the current compile unit. */
19330 if (attr.form == DW_FORM_ref_addr)
19331 complaint (_("ignoring absolute DW_AT_sibling"));
19332 else
19333 {
19334 const gdb_byte *buffer = reader->buffer;
19335 sect_offset off = attr.get_ref_die_offset ();
19336 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19337
19338 if (sibling_ptr < info_ptr)
19339 complaint (_("DW_AT_sibling points backwards"));
19340 else if (sibling_ptr > reader->buffer_end)
19341 reader->die_section->overflow_complaint ();
19342 else
19343 sibling = sibling_ptr;
19344 }
19345 break;
19346 case DW_AT_byte_size:
19347 has_byte_size = 1;
19348 break;
19349 case DW_AT_const_value:
19350 has_const_value = 1;
19351 break;
19352 case DW_AT_calling_convention:
19353 /* DWARF doesn't provide a way to identify a program's source-level
19354 entry point. DW_AT_calling_convention attributes are only meant
19355 to describe functions' calling conventions.
19356
19357 However, because it's a necessary piece of information in
19358 Fortran, and before DWARF 4 DW_CC_program was the only
19359 piece of debugging information whose definition refers to
19360 a 'main program' at all, several compilers marked Fortran
19361 main programs with DW_CC_program --- even when those
19362 functions use the standard calling conventions.
19363
19364 Although DWARF now specifies a way to provide this
19365 information, we support this practice for backward
19366 compatibility. */
19367 if (attr.constant_value (0) == DW_CC_program
19368 && cu->per_cu->lang == language_fortran)
19369 main_subprogram = 1;
19370 break;
19371 case DW_AT_inline:
19372 {
19373 LONGEST value = attr.constant_value (-1);
19374 if (value == DW_INL_inlined
19375 || value == DW_INL_declared_inlined)
19376 may_be_inlined = 1;
19377 }
19378 break;
19379
19380 case DW_AT_import:
19381 if (tag == DW_TAG_imported_unit)
19382 {
19383 d.sect_off = attr.get_ref_die_offset ();
19384 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19385 || cu->per_cu->is_dwz);
19386 }
19387 break;
19388
19389 case DW_AT_main_subprogram:
19390 main_subprogram = attr.as_boolean ();
19391 break;
19392
19393 case DW_AT_ranges:
19394 {
19395 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19396 on DWARF version). */
19397 ranges_offset = attr.as_unsigned ();
19398
19399 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19400 this value. */
19401 if (tag != DW_TAG_compile_unit)
19402 ranges_offset += cu->gnu_ranges_base;
19403
19404 has_range_info = 1;
19405 }
19406 break;
19407
19408 default:
19409 break;
19410 }
19411 }
19412
19413 /* For Ada, if both the name and the linkage name appear, we prefer
19414 the latter. This lets "catch exception" work better, regardless
19415 of the order in which the name and linkage name were emitted.
19416 Really, though, this is just a workaround for the fact that gdb
19417 doesn't store both the name and the linkage name. */
19418 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
19419 raw_name = linkage_name;
19420
19421 if (high_pc_relative)
19422 highpc += lowpc;
19423
19424 if (has_low_pc_attr && has_high_pc_attr)
19425 {
19426 /* When using the GNU linker, .gnu.linkonce. sections are used to
19427 eliminate duplicate copies of functions and vtables and such.
19428 The linker will arbitrarily choose one and discard the others.
19429 The AT_*_pc values for such functions refer to local labels in
19430 these sections. If the section from that file was discarded, the
19431 labels are not in the output, so the relocs get a value of 0.
19432 If this is a discarded function, mark the pc bounds as invalid,
19433 so that GDB will ignore it. */
19434 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19435 {
19436 struct objfile *objfile = per_objfile->objfile;
19437 struct gdbarch *gdbarch = objfile->arch ();
19438
19439 complaint (_("DW_AT_low_pc %s is zero "
19440 "for DIE at %s [in module %s]"),
19441 paddress (gdbarch, lowpc),
19442 sect_offset_str (sect_off),
19443 objfile_name (objfile));
19444 }
19445 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19446 else if (lowpc >= highpc)
19447 {
19448 struct objfile *objfile = per_objfile->objfile;
19449 struct gdbarch *gdbarch = objfile->arch ();
19450
19451 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19452 "for DIE at %s [in module %s]"),
19453 paddress (gdbarch, lowpc),
19454 paddress (gdbarch, highpc),
19455 sect_offset_str (sect_off),
19456 objfile_name (objfile));
19457 }
19458 else
19459 has_pc_info = 1;
19460 }
19461
19462 return info_ptr;
19463 }
19464
19465 /* Find a cached partial DIE at OFFSET in CU. */
19466
19467 struct partial_die_info *
19468 dwarf2_cu::find_partial_die (sect_offset sect_off)
19469 {
19470 struct partial_die_info *lookup_die = NULL;
19471 struct partial_die_info part_die (sect_off);
19472
19473 lookup_die = ((struct partial_die_info *)
19474 htab_find_with_hash (partial_dies, &part_die,
19475 to_underlying (sect_off)));
19476
19477 return lookup_die;
19478 }
19479
19480 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19481 except in the case of .debug_types DIEs which do not reference
19482 outside their CU (they do however referencing other types via
19483 DW_FORM_ref_sig8). */
19484
19485 static const struct cu_partial_die_info
19486 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19487 {
19488 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19489 struct objfile *objfile = per_objfile->objfile;
19490 struct partial_die_info *pd = NULL;
19491
19492 if (offset_in_dwz == cu->per_cu->is_dwz
19493 && cu->header.offset_in_cu_p (sect_off))
19494 {
19495 pd = cu->find_partial_die (sect_off);
19496 if (pd != NULL)
19497 return { cu, pd };
19498 /* We missed recording what we needed.
19499 Load all dies and try again. */
19500 }
19501 else
19502 {
19503 /* TUs don't reference other CUs/TUs (except via type signatures). */
19504 if (cu->per_cu->is_debug_types)
19505 {
19506 error (_("Dwarf Error: Type Unit at offset %s contains"
19507 " external reference to offset %s [in module %s].\n"),
19508 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19509 bfd_get_filename (objfile->obfd));
19510 }
19511 dwarf2_per_cu_data *per_cu
19512 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19513 per_objfile->per_bfd);
19514
19515 cu = per_objfile->get_cu (per_cu);
19516 if (cu == NULL || cu->partial_dies == NULL)
19517 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19518
19519 cu = per_objfile->get_cu (per_cu);
19520
19521 cu->last_used = 0;
19522 pd = cu->find_partial_die (sect_off);
19523 }
19524
19525 /* If we didn't find it, and not all dies have been loaded,
19526 load them all and try again. */
19527
19528 if (pd == NULL && cu->load_all_dies == 0)
19529 {
19530 cu->load_all_dies = 1;
19531
19532 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19533 THIS_CU->cu may already be in use. So we can't just free it and
19534 replace its DIEs with the ones we read in. Instead, we leave those
19535 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19536 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19537 set. */
19538 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19539
19540 pd = cu->find_partial_die (sect_off);
19541 }
19542
19543 if (pd == NULL)
19544 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
19545 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19546 return { cu, pd };
19547 }
19548
19549 /* See if we can figure out if the class lives in a namespace. We do
19550 this by looking for a member function; its demangled name will
19551 contain namespace info, if there is any. */
19552
19553 static void
19554 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19555 struct dwarf2_cu *cu)
19556 {
19557 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19558 what template types look like, because the demangler
19559 frequently doesn't give the same name as the debug info. We
19560 could fix this by only using the demangled name to get the
19561 prefix (but see comment in read_structure_type). */
19562
19563 struct partial_die_info *real_pdi;
19564 struct partial_die_info *child_pdi;
19565
19566 /* If this DIE (this DIE's specification, if any) has a parent, then
19567 we should not do this. We'll prepend the parent's fully qualified
19568 name when we create the partial symbol. */
19569
19570 real_pdi = struct_pdi;
19571 while (real_pdi->has_specification)
19572 {
19573 auto res = find_partial_die (real_pdi->spec_offset,
19574 real_pdi->spec_is_dwz, cu);
19575 real_pdi = res.pdi;
19576 cu = res.cu;
19577 }
19578
19579 if (real_pdi->die_parent != NULL)
19580 return;
19581
19582 for (child_pdi = struct_pdi->die_child;
19583 child_pdi != NULL;
19584 child_pdi = child_pdi->die_sibling)
19585 {
19586 if (child_pdi->tag == DW_TAG_subprogram
19587 && child_pdi->linkage_name != NULL)
19588 {
19589 gdb::unique_xmalloc_ptr<char> actual_class_name
19590 (cu->language_defn->class_name_from_physname
19591 (child_pdi->linkage_name));
19592 if (actual_class_name != NULL)
19593 {
19594 struct objfile *objfile = cu->per_objfile->objfile;
19595 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19596 struct_pdi->canonical_name = 1;
19597 }
19598 break;
19599 }
19600 }
19601 }
19602
19603 /* Return true if a DIE with TAG may have the DW_AT_const_value
19604 attribute. */
19605
19606 static bool
19607 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19608 {
19609 switch (tag)
19610 {
19611 case DW_TAG_constant:
19612 case DW_TAG_enumerator:
19613 case DW_TAG_formal_parameter:
19614 case DW_TAG_template_value_param:
19615 case DW_TAG_variable:
19616 return true;
19617 }
19618
19619 return false;
19620 }
19621
19622 void
19623 partial_die_info::fixup (struct dwarf2_cu *cu)
19624 {
19625 /* Once we've fixed up a die, there's no point in doing so again.
19626 This also avoids a memory leak if we were to call
19627 guess_partial_die_structure_name multiple times. */
19628 if (fixup_called)
19629 return;
19630
19631 /* If we found a reference attribute and the DIE has no name, try
19632 to find a name in the referred to DIE. */
19633
19634 if (raw_name == NULL && has_specification)
19635 {
19636 struct partial_die_info *spec_die;
19637
19638 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19639 spec_die = res.pdi;
19640 cu = res.cu;
19641
19642 spec_die->fixup (cu);
19643
19644 if (spec_die->raw_name)
19645 {
19646 raw_name = spec_die->raw_name;
19647 canonical_name = spec_die->canonical_name;
19648
19649 /* Copy DW_AT_external attribute if it is set. */
19650 if (spec_die->is_external)
19651 is_external = spec_die->is_external;
19652 }
19653 }
19654
19655 if (!has_const_value && has_specification
19656 && can_have_DW_AT_const_value_p (tag))
19657 {
19658 struct partial_die_info *spec_die;
19659
19660 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19661 spec_die = res.pdi;
19662 cu = res.cu;
19663
19664 spec_die->fixup (cu);
19665
19666 if (spec_die->has_const_value)
19667 {
19668 /* Copy DW_AT_const_value attribute if it is set. */
19669 has_const_value = spec_die->has_const_value;
19670 }
19671 }
19672
19673 /* Set default names for some unnamed DIEs. */
19674
19675 if (raw_name == NULL && tag == DW_TAG_namespace)
19676 {
19677 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19678 canonical_name = 1;
19679 }
19680
19681 /* If there is no parent die to provide a namespace, and there are
19682 children, see if we can determine the namespace from their linkage
19683 name. */
19684 if (cu->per_cu->lang == language_cplus
19685 && !cu->per_objfile->per_bfd->types.empty ()
19686 && die_parent == NULL
19687 && has_children
19688 && (tag == DW_TAG_class_type
19689 || tag == DW_TAG_structure_type
19690 || tag == DW_TAG_union_type))
19691 guess_partial_die_structure_name (this, cu);
19692
19693 /* GCC might emit a nameless struct or union that has a linkage
19694 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19695 if (raw_name == NULL
19696 && (tag == DW_TAG_class_type
19697 || tag == DW_TAG_interface_type
19698 || tag == DW_TAG_structure_type
19699 || tag == DW_TAG_union_type)
19700 && linkage_name != NULL)
19701 {
19702 gdb::unique_xmalloc_ptr<char> demangled
19703 (gdb_demangle (linkage_name, DMGL_TYPES));
19704 if (demangled != nullptr)
19705 {
19706 const char *base;
19707
19708 /* Strip any leading namespaces/classes, keep only the base name.
19709 DW_AT_name for named DIEs does not contain the prefixes. */
19710 base = strrchr (demangled.get (), ':');
19711 if (base && base > demangled.get () && base[-1] == ':')
19712 base++;
19713 else
19714 base = demangled.get ();
19715
19716 struct objfile *objfile = cu->per_objfile->objfile;
19717 raw_name = objfile->intern (base);
19718 canonical_name = 1;
19719 }
19720 }
19721
19722 fixup_called = 1;
19723 }
19724
19725 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19726 contents from the given SECTION in the HEADER.
19727
19728 HEADER_OFFSET is the offset of the header in the section. */
19729 static void
19730 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19731 struct dwarf2_section_info *section,
19732 sect_offset header_offset)
19733 {
19734 unsigned int bytes_read;
19735 bfd *abfd = section->get_bfd_owner ();
19736 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19737
19738 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19739 info_ptr += bytes_read;
19740
19741 header->version = read_2_bytes (abfd, info_ptr);
19742 info_ptr += 2;
19743
19744 header->addr_size = read_1_byte (abfd, info_ptr);
19745 info_ptr += 1;
19746
19747 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19748 info_ptr += 1;
19749
19750 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19751 }
19752
19753 /* Return the DW_AT_loclists_base value for the CU. */
19754 static ULONGEST
19755 lookup_loclist_base (struct dwarf2_cu *cu)
19756 {
19757 /* For the .dwo unit, the loclist_base points to the first offset following
19758 the header. The header consists of the following entities-
19759 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19760 bit format)
19761 2. version (2 bytes)
19762 3. address size (1 byte)
19763 4. segment selector size (1 byte)
19764 5. offset entry count (4 bytes)
19765 These sizes are derived as per the DWARFv5 standard. */
19766 if (cu->dwo_unit != nullptr)
19767 {
19768 if (cu->header.initial_length_size == 4)
19769 return LOCLIST_HEADER_SIZE32;
19770 return LOCLIST_HEADER_SIZE64;
19771 }
19772 return cu->loclist_base;
19773 }
19774
19775 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19776 array of offsets in the .debug_loclists section. */
19777
19778 static sect_offset
19779 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19780 {
19781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19782 struct objfile *objfile = per_objfile->objfile;
19783 bfd *abfd = objfile->obfd;
19784 ULONGEST loclist_header_size =
19785 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19786 : LOCLIST_HEADER_SIZE64);
19787 ULONGEST loclist_base = lookup_loclist_base (cu);
19788
19789 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19790 ULONGEST start_offset =
19791 loclist_base + loclist_index * cu->header.offset_size;
19792
19793 /* Get loclists section. */
19794 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19795
19796 /* Read the loclists section content. */
19797 section->read (objfile);
19798 if (section->buffer == NULL)
19799 error (_("DW_FORM_loclistx used without .debug_loclists "
19800 "section [in module %s]"), objfile_name (objfile));
19801
19802 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19803 so if loclist_base is smaller than the header size, we have a problem. */
19804 if (loclist_base < loclist_header_size)
19805 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19806 objfile_name (objfile));
19807
19808 /* Read the header of the loclists contribution. */
19809 struct loclists_rnglists_header header;
19810 read_loclists_rnglists_header (&header, section,
19811 (sect_offset) (loclist_base - loclist_header_size));
19812
19813 /* Verify the loclist index is valid. */
19814 if (loclist_index >= header.offset_entry_count)
19815 error (_("DW_FORM_loclistx pointing outside of "
19816 ".debug_loclists offset array [in module %s]"),
19817 objfile_name (objfile));
19818
19819 /* Validate that reading won't go beyond the end of the section. */
19820 if (start_offset + cu->header.offset_size > section->size)
19821 error (_("Reading DW_FORM_loclistx index beyond end of"
19822 ".debug_loclists section [in module %s]"),
19823 objfile_name (objfile));
19824
19825 const gdb_byte *info_ptr = section->buffer + start_offset;
19826
19827 if (cu->header.offset_size == 4)
19828 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19829 else
19830 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19831 }
19832
19833 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19834 array of offsets in the .debug_rnglists section. */
19835
19836 static sect_offset
19837 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19838 dwarf_tag tag)
19839 {
19840 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19841 struct objfile *objfile = dwarf2_per_objfile->objfile;
19842 bfd *abfd = objfile->obfd;
19843 ULONGEST rnglist_header_size =
19844 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19845 : RNGLIST_HEADER_SIZE64);
19846
19847 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19848 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19849 doesn't apply. */
19850 ULONGEST rnglist_base =
19851 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19852
19853 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19854 ULONGEST start_offset =
19855 rnglist_base + rnglist_index * cu->header.offset_size;
19856
19857 /* Get rnglists section. */
19858 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19859
19860 /* Read the rnglists section content. */
19861 section->read (objfile);
19862 if (section->buffer == nullptr)
19863 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19864 "[in module %s]"),
19865 objfile_name (objfile));
19866
19867 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19868 so if rnglist_base is smaller than the header size, we have a problem. */
19869 if (rnglist_base < rnglist_header_size)
19870 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19871 objfile_name (objfile));
19872
19873 /* Read the header of the rnglists contribution. */
19874 struct loclists_rnglists_header header;
19875 read_loclists_rnglists_header (&header, section,
19876 (sect_offset) (rnglist_base - rnglist_header_size));
19877
19878 /* Verify the rnglist index is valid. */
19879 if (rnglist_index >= header.offset_entry_count)
19880 error (_("DW_FORM_rnglistx index pointing outside of "
19881 ".debug_rnglists offset array [in module %s]"),
19882 objfile_name (objfile));
19883
19884 /* Validate that reading won't go beyond the end of the section. */
19885 if (start_offset + cu->header.offset_size > section->size)
19886 error (_("Reading DW_FORM_rnglistx index beyond end of"
19887 ".debug_rnglists section [in module %s]"),
19888 objfile_name (objfile));
19889
19890 const gdb_byte *info_ptr = section->buffer + start_offset;
19891
19892 if (cu->header.offset_size == 4)
19893 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19894 else
19895 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19896 }
19897
19898 /* Process the attributes that had to be skipped in the first round. These
19899 attributes are the ones that need str_offsets_base or addr_base attributes.
19900 They could not have been processed in the first round, because at the time
19901 the values of str_offsets_base or addr_base may not have been known. */
19902 static void
19903 read_attribute_reprocess (const struct die_reader_specs *reader,
19904 struct attribute *attr, dwarf_tag tag)
19905 {
19906 struct dwarf2_cu *cu = reader->cu;
19907 switch (attr->form)
19908 {
19909 case DW_FORM_addrx:
19910 case DW_FORM_GNU_addr_index:
19911 attr->set_address (read_addr_index (cu,
19912 attr->as_unsigned_reprocess ()));
19913 break;
19914 case DW_FORM_loclistx:
19915 {
19916 sect_offset loclists_sect_off
19917 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19918
19919 attr->set_unsigned (to_underlying (loclists_sect_off));
19920 }
19921 break;
19922 case DW_FORM_rnglistx:
19923 {
19924 sect_offset rnglists_sect_off
19925 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19926
19927 attr->set_unsigned (to_underlying (rnglists_sect_off));
19928 }
19929 break;
19930 case DW_FORM_strx:
19931 case DW_FORM_strx1:
19932 case DW_FORM_strx2:
19933 case DW_FORM_strx3:
19934 case DW_FORM_strx4:
19935 case DW_FORM_GNU_str_index:
19936 {
19937 unsigned int str_index = attr->as_unsigned_reprocess ();
19938 gdb_assert (!attr->canonical_string_p ());
19939 if (reader->dwo_file != NULL)
19940 attr->set_string_noncanonical (read_dwo_str_index (reader,
19941 str_index));
19942 else
19943 attr->set_string_noncanonical (read_stub_str_index (cu,
19944 str_index));
19945 break;
19946 }
19947 default:
19948 gdb_assert_not_reached ("Unexpected DWARF form.");
19949 }
19950 }
19951
19952 /* Read an attribute value described by an attribute form. */
19953
19954 static const gdb_byte *
19955 read_attribute_value (const struct die_reader_specs *reader,
19956 struct attribute *attr, unsigned form,
19957 LONGEST implicit_const, const gdb_byte *info_ptr)
19958 {
19959 struct dwarf2_cu *cu = reader->cu;
19960 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19961 struct objfile *objfile = per_objfile->objfile;
19962 bfd *abfd = reader->abfd;
19963 struct comp_unit_head *cu_header = &cu->header;
19964 unsigned int bytes_read;
19965 struct dwarf_block *blk;
19966
19967 attr->form = (enum dwarf_form) form;
19968 switch (form)
19969 {
19970 case DW_FORM_ref_addr:
19971 if (cu_header->version == 2)
19972 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19973 &bytes_read));
19974 else
19975 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19976 &bytes_read));
19977 info_ptr += bytes_read;
19978 break;
19979 case DW_FORM_GNU_ref_alt:
19980 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19981 &bytes_read));
19982 info_ptr += bytes_read;
19983 break;
19984 case DW_FORM_addr:
19985 {
19986 struct gdbarch *gdbarch = objfile->arch ();
19987 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19988 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19989 attr->set_address (addr);
19990 info_ptr += bytes_read;
19991 }
19992 break;
19993 case DW_FORM_block2:
19994 blk = dwarf_alloc_block (cu);
19995 blk->size = read_2_bytes (abfd, info_ptr);
19996 info_ptr += 2;
19997 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19998 info_ptr += blk->size;
19999 attr->set_block (blk);
20000 break;
20001 case DW_FORM_block4:
20002 blk = dwarf_alloc_block (cu);
20003 blk->size = read_4_bytes (abfd, info_ptr);
20004 info_ptr += 4;
20005 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20006 info_ptr += blk->size;
20007 attr->set_block (blk);
20008 break;
20009 case DW_FORM_data2:
20010 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20011 info_ptr += 2;
20012 break;
20013 case DW_FORM_data4:
20014 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20015 info_ptr += 4;
20016 break;
20017 case DW_FORM_data8:
20018 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20019 info_ptr += 8;
20020 break;
20021 case DW_FORM_data16:
20022 blk = dwarf_alloc_block (cu);
20023 blk->size = 16;
20024 blk->data = read_n_bytes (abfd, info_ptr, 16);
20025 info_ptr += 16;
20026 attr->set_block (blk);
20027 break;
20028 case DW_FORM_sec_offset:
20029 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20030 &bytes_read));
20031 info_ptr += bytes_read;
20032 break;
20033 case DW_FORM_loclistx:
20034 {
20035 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20036 &bytes_read));
20037 info_ptr += bytes_read;
20038 }
20039 break;
20040 case DW_FORM_string:
20041 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20042 &bytes_read));
20043 info_ptr += bytes_read;
20044 break;
20045 case DW_FORM_strp:
20046 if (!cu->per_cu->is_dwz)
20047 {
20048 attr->set_string_noncanonical
20049 (read_indirect_string (per_objfile,
20050 abfd, info_ptr, cu_header,
20051 &bytes_read));
20052 info_ptr += bytes_read;
20053 break;
20054 }
20055 /* FALLTHROUGH */
20056 case DW_FORM_line_strp:
20057 if (!cu->per_cu->is_dwz)
20058 {
20059 attr->set_string_noncanonical
20060 (per_objfile->read_line_string (info_ptr, cu_header,
20061 &bytes_read));
20062 info_ptr += bytes_read;
20063 break;
20064 }
20065 /* FALLTHROUGH */
20066 case DW_FORM_GNU_strp_alt:
20067 {
20068 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20069 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20070 &bytes_read);
20071
20072 attr->set_string_noncanonical
20073 (dwz->read_string (objfile, str_offset));
20074 info_ptr += bytes_read;
20075 }
20076 break;
20077 case DW_FORM_exprloc:
20078 case DW_FORM_block:
20079 blk = dwarf_alloc_block (cu);
20080 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20081 info_ptr += bytes_read;
20082 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20083 info_ptr += blk->size;
20084 attr->set_block (blk);
20085 break;
20086 case DW_FORM_block1:
20087 blk = dwarf_alloc_block (cu);
20088 blk->size = read_1_byte (abfd, info_ptr);
20089 info_ptr += 1;
20090 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20091 info_ptr += blk->size;
20092 attr->set_block (blk);
20093 break;
20094 case DW_FORM_data1:
20095 case DW_FORM_flag:
20096 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20097 info_ptr += 1;
20098 break;
20099 case DW_FORM_flag_present:
20100 attr->set_unsigned (1);
20101 break;
20102 case DW_FORM_sdata:
20103 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20104 info_ptr += bytes_read;
20105 break;
20106 case DW_FORM_rnglistx:
20107 {
20108 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20109 &bytes_read));
20110 info_ptr += bytes_read;
20111 }
20112 break;
20113 case DW_FORM_udata:
20114 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20115 info_ptr += bytes_read;
20116 break;
20117 case DW_FORM_ref1:
20118 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20119 + read_1_byte (abfd, info_ptr)));
20120 info_ptr += 1;
20121 break;
20122 case DW_FORM_ref2:
20123 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20124 + read_2_bytes (abfd, info_ptr)));
20125 info_ptr += 2;
20126 break;
20127 case DW_FORM_ref4:
20128 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20129 + read_4_bytes (abfd, info_ptr)));
20130 info_ptr += 4;
20131 break;
20132 case DW_FORM_ref8:
20133 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20134 + read_8_bytes (abfd, info_ptr)));
20135 info_ptr += 8;
20136 break;
20137 case DW_FORM_ref_sig8:
20138 attr->set_signature (read_8_bytes (abfd, info_ptr));
20139 info_ptr += 8;
20140 break;
20141 case DW_FORM_ref_udata:
20142 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20143 + read_unsigned_leb128 (abfd, info_ptr,
20144 &bytes_read)));
20145 info_ptr += bytes_read;
20146 break;
20147 case DW_FORM_indirect:
20148 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20149 info_ptr += bytes_read;
20150 if (form == DW_FORM_implicit_const)
20151 {
20152 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20153 info_ptr += bytes_read;
20154 }
20155 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20156 info_ptr);
20157 break;
20158 case DW_FORM_implicit_const:
20159 attr->set_signed (implicit_const);
20160 break;
20161 case DW_FORM_addrx:
20162 case DW_FORM_GNU_addr_index:
20163 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20164 &bytes_read));
20165 info_ptr += bytes_read;
20166 break;
20167 case DW_FORM_strx:
20168 case DW_FORM_strx1:
20169 case DW_FORM_strx2:
20170 case DW_FORM_strx3:
20171 case DW_FORM_strx4:
20172 case DW_FORM_GNU_str_index:
20173 {
20174 ULONGEST str_index;
20175 if (form == DW_FORM_strx1)
20176 {
20177 str_index = read_1_byte (abfd, info_ptr);
20178 info_ptr += 1;
20179 }
20180 else if (form == DW_FORM_strx2)
20181 {
20182 str_index = read_2_bytes (abfd, info_ptr);
20183 info_ptr += 2;
20184 }
20185 else if (form == DW_FORM_strx3)
20186 {
20187 str_index = read_3_bytes (abfd, info_ptr);
20188 info_ptr += 3;
20189 }
20190 else if (form == DW_FORM_strx4)
20191 {
20192 str_index = read_4_bytes (abfd, info_ptr);
20193 info_ptr += 4;
20194 }
20195 else
20196 {
20197 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20198 info_ptr += bytes_read;
20199 }
20200 attr->set_unsigned_reprocess (str_index);
20201 }
20202 break;
20203 default:
20204 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20205 dwarf_form_name (form),
20206 bfd_get_filename (abfd));
20207 }
20208
20209 /* Super hack. */
20210 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20211 attr->form = DW_FORM_GNU_ref_alt;
20212
20213 /* We have seen instances where the compiler tried to emit a byte
20214 size attribute of -1 which ended up being encoded as an unsigned
20215 0xffffffff. Although 0xffffffff is technically a valid size value,
20216 an object of this size seems pretty unlikely so we can relatively
20217 safely treat these cases as if the size attribute was invalid and
20218 treat them as zero by default. */
20219 if (attr->name == DW_AT_byte_size
20220 && form == DW_FORM_data4
20221 && attr->as_unsigned () >= 0xffffffff)
20222 {
20223 complaint
20224 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20225 hex_string (attr->as_unsigned ()));
20226 attr->set_unsigned (0);
20227 }
20228
20229 return info_ptr;
20230 }
20231
20232 /* Read an attribute described by an abbreviated attribute. */
20233
20234 static const gdb_byte *
20235 read_attribute (const struct die_reader_specs *reader,
20236 struct attribute *attr, const struct attr_abbrev *abbrev,
20237 const gdb_byte *info_ptr)
20238 {
20239 attr->name = abbrev->name;
20240 attr->string_is_canonical = 0;
20241 attr->requires_reprocessing = 0;
20242 return read_attribute_value (reader, attr, abbrev->form,
20243 abbrev->implicit_const, info_ptr);
20244 }
20245
20246 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20247
20248 static const char *
20249 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20250 LONGEST str_offset)
20251 {
20252 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20253 str_offset, "DW_FORM_strp");
20254 }
20255
20256 /* Return pointer to string at .debug_str offset as read from BUF.
20257 BUF is assumed to be in a compilation unit described by CU_HEADER.
20258 Return *BYTES_READ_PTR count of bytes read from BUF. */
20259
20260 static const char *
20261 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20262 const gdb_byte *buf,
20263 const struct comp_unit_head *cu_header,
20264 unsigned int *bytes_read_ptr)
20265 {
20266 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20267
20268 return read_indirect_string_at_offset (per_objfile, str_offset);
20269 }
20270
20271 /* See read.h. */
20272
20273 const char *
20274 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20275 unsigned int offset_size)
20276 {
20277 bfd *abfd = objfile->obfd;
20278 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
20279
20280 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20281 }
20282
20283 /* See read.h. */
20284
20285 const char *
20286 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20287 const struct comp_unit_head *cu_header,
20288 unsigned int *bytes_read_ptr)
20289 {
20290 bfd *abfd = objfile->obfd;
20291 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20292
20293 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20294 }
20295
20296 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20297 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20298 ADDR_SIZE is the size of addresses from the CU header. */
20299
20300 static CORE_ADDR
20301 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20302 gdb::optional<ULONGEST> addr_base, int addr_size)
20303 {
20304 struct objfile *objfile = per_objfile->objfile;
20305 bfd *abfd = objfile->obfd;
20306 const gdb_byte *info_ptr;
20307 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20308
20309 per_objfile->per_bfd->addr.read (objfile);
20310 if (per_objfile->per_bfd->addr.buffer == NULL)
20311 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20312 objfile_name (objfile));
20313 if (addr_base_or_zero + addr_index * addr_size
20314 >= per_objfile->per_bfd->addr.size)
20315 error (_("DW_FORM_addr_index pointing outside of "
20316 ".debug_addr section [in module %s]"),
20317 objfile_name (objfile));
20318 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20319 + addr_index * addr_size);
20320 if (addr_size == 4)
20321 return bfd_get_32 (abfd, info_ptr);
20322 else
20323 return bfd_get_64 (abfd, info_ptr);
20324 }
20325
20326 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20327
20328 static CORE_ADDR
20329 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20330 {
20331 return read_addr_index_1 (cu->per_objfile, addr_index,
20332 cu->addr_base, cu->header.addr_size);
20333 }
20334
20335 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20336
20337 static CORE_ADDR
20338 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20339 unsigned int *bytes_read)
20340 {
20341 bfd *abfd = cu->per_objfile->objfile->obfd;
20342 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20343
20344 return read_addr_index (cu, addr_index);
20345 }
20346
20347 /* See read.h. */
20348
20349 CORE_ADDR
20350 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20351 dwarf2_per_objfile *per_objfile,
20352 unsigned int addr_index)
20353 {
20354 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20355 gdb::optional<ULONGEST> addr_base;
20356 int addr_size;
20357
20358 /* We need addr_base and addr_size.
20359 If we don't have PER_CU->cu, we have to get it.
20360 Nasty, but the alternative is storing the needed info in PER_CU,
20361 which at this point doesn't seem justified: it's not clear how frequently
20362 it would get used and it would increase the size of every PER_CU.
20363 Entry points like dwarf2_per_cu_addr_size do a similar thing
20364 so we're not in uncharted territory here.
20365 Alas we need to be a bit more complicated as addr_base is contained
20366 in the DIE.
20367
20368 We don't need to read the entire CU(/TU).
20369 We just need the header and top level die.
20370
20371 IWBN to use the aging mechanism to let us lazily later discard the CU.
20372 For now we skip this optimization. */
20373
20374 if (cu != NULL)
20375 {
20376 addr_base = cu->addr_base;
20377 addr_size = cu->header.addr_size;
20378 }
20379 else
20380 {
20381 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20382 addr_base = reader.cu->addr_base;
20383 addr_size = reader.cu->header.addr_size;
20384 }
20385
20386 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20387 }
20388
20389 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20390 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20391 DWO file. */
20392
20393 static const char *
20394 read_str_index (struct dwarf2_cu *cu,
20395 struct dwarf2_section_info *str_section,
20396 struct dwarf2_section_info *str_offsets_section,
20397 ULONGEST str_offsets_base, ULONGEST str_index)
20398 {
20399 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20400 struct objfile *objfile = per_objfile->objfile;
20401 const char *objf_name = objfile_name (objfile);
20402 bfd *abfd = objfile->obfd;
20403 const gdb_byte *info_ptr;
20404 ULONGEST str_offset;
20405 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20406
20407 str_section->read (objfile);
20408 str_offsets_section->read (objfile);
20409 if (str_section->buffer == NULL)
20410 error (_("%s used without %s section"
20411 " in CU at offset %s [in module %s]"),
20412 form_name, str_section->get_name (),
20413 sect_offset_str (cu->header.sect_off), objf_name);
20414 if (str_offsets_section->buffer == NULL)
20415 error (_("%s used without %s section"
20416 " in CU at offset %s [in module %s]"),
20417 form_name, str_section->get_name (),
20418 sect_offset_str (cu->header.sect_off), objf_name);
20419 info_ptr = (str_offsets_section->buffer
20420 + str_offsets_base
20421 + str_index * cu->header.offset_size);
20422 if (cu->header.offset_size == 4)
20423 str_offset = bfd_get_32 (abfd, info_ptr);
20424 else
20425 str_offset = bfd_get_64 (abfd, info_ptr);
20426 if (str_offset >= str_section->size)
20427 error (_("Offset from %s pointing outside of"
20428 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20429 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20430 return (const char *) (str_section->buffer + str_offset);
20431 }
20432
20433 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20434
20435 static const char *
20436 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20437 {
20438 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20439 ? reader->cu->header.addr_size : 0;
20440 return read_str_index (reader->cu,
20441 &reader->dwo_file->sections.str,
20442 &reader->dwo_file->sections.str_offsets,
20443 str_offsets_base, str_index);
20444 }
20445
20446 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20447
20448 static const char *
20449 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20450 {
20451 struct objfile *objfile = cu->per_objfile->objfile;
20452 const char *objf_name = objfile_name (objfile);
20453 static const char form_name[] = "DW_FORM_GNU_str_index";
20454 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20455
20456 if (!cu->str_offsets_base.has_value ())
20457 error (_("%s used in Fission stub without %s"
20458 " in CU at offset 0x%lx [in module %s]"),
20459 form_name, str_offsets_attr_name,
20460 (long) cu->header.offset_size, objf_name);
20461
20462 return read_str_index (cu,
20463 &cu->per_objfile->per_bfd->str,
20464 &cu->per_objfile->per_bfd->str_offsets,
20465 *cu->str_offsets_base, str_index);
20466 }
20467
20468 /* Return the length of an LEB128 number in BUF. */
20469
20470 static int
20471 leb128_size (const gdb_byte *buf)
20472 {
20473 const gdb_byte *begin = buf;
20474 gdb_byte byte;
20475
20476 while (1)
20477 {
20478 byte = *buf++;
20479 if ((byte & 128) == 0)
20480 return buf - begin;
20481 }
20482 }
20483
20484 static enum language
20485 dwarf_lang_to_enum_language (unsigned int lang)
20486 {
20487 enum language language;
20488
20489 switch (lang)
20490 {
20491 case DW_LANG_C89:
20492 case DW_LANG_C99:
20493 case DW_LANG_C11:
20494 case DW_LANG_C:
20495 case DW_LANG_UPC:
20496 language = language_c;
20497 break;
20498 case DW_LANG_Java:
20499 case DW_LANG_C_plus_plus:
20500 case DW_LANG_C_plus_plus_11:
20501 case DW_LANG_C_plus_plus_14:
20502 language = language_cplus;
20503 break;
20504 case DW_LANG_D:
20505 language = language_d;
20506 break;
20507 case DW_LANG_Fortran77:
20508 case DW_LANG_Fortran90:
20509 case DW_LANG_Fortran95:
20510 case DW_LANG_Fortran03:
20511 case DW_LANG_Fortran08:
20512 language = language_fortran;
20513 break;
20514 case DW_LANG_Go:
20515 language = language_go;
20516 break;
20517 case DW_LANG_Mips_Assembler:
20518 language = language_asm;
20519 break;
20520 case DW_LANG_Ada83:
20521 case DW_LANG_Ada95:
20522 language = language_ada;
20523 break;
20524 case DW_LANG_Modula2:
20525 language = language_m2;
20526 break;
20527 case DW_LANG_Pascal83:
20528 language = language_pascal;
20529 break;
20530 case DW_LANG_ObjC:
20531 language = language_objc;
20532 break;
20533 case DW_LANG_Rust:
20534 case DW_LANG_Rust_old:
20535 language = language_rust;
20536 break;
20537 case DW_LANG_OpenCL:
20538 language = language_opencl;
20539 break;
20540 case DW_LANG_Cobol74:
20541 case DW_LANG_Cobol85:
20542 default:
20543 language = language_minimal;
20544 break;
20545 }
20546
20547 return language;
20548 }
20549
20550 /* Return the named attribute or NULL if not there. */
20551
20552 static struct attribute *
20553 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20554 {
20555 for (;;)
20556 {
20557 unsigned int i;
20558 struct attribute *spec = NULL;
20559
20560 for (i = 0; i < die->num_attrs; ++i)
20561 {
20562 if (die->attrs[i].name == name)
20563 return &die->attrs[i];
20564 if (die->attrs[i].name == DW_AT_specification
20565 || die->attrs[i].name == DW_AT_abstract_origin)
20566 spec = &die->attrs[i];
20567 }
20568
20569 if (!spec)
20570 break;
20571
20572 die = follow_die_ref (die, spec, &cu);
20573 }
20574
20575 return NULL;
20576 }
20577
20578 /* Return the string associated with a string-typed attribute, or NULL if it
20579 is either not found or is of an incorrect type. */
20580
20581 static const char *
20582 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20583 {
20584 struct attribute *attr;
20585 const char *str = NULL;
20586
20587 attr = dwarf2_attr (die, name, cu);
20588
20589 if (attr != NULL)
20590 {
20591 str = attr->as_string ();
20592 if (str == nullptr)
20593 complaint (_("string type expected for attribute %s for "
20594 "DIE at %s in module %s"),
20595 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20596 objfile_name (cu->per_objfile->objfile));
20597 }
20598
20599 return str;
20600 }
20601
20602 /* Return the dwo name or NULL if not present. If present, it is in either
20603 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20604 static const char *
20605 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20606 {
20607 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20608 if (dwo_name == nullptr)
20609 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20610 return dwo_name;
20611 }
20612
20613 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20614 and holds a non-zero value. This function should only be used for
20615 DW_FORM_flag or DW_FORM_flag_present attributes. */
20616
20617 static int
20618 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20619 {
20620 struct attribute *attr = dwarf2_attr (die, name, cu);
20621
20622 return attr != nullptr && attr->as_boolean ();
20623 }
20624
20625 static int
20626 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20627 {
20628 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20629 which value is non-zero. However, we have to be careful with
20630 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20631 (via dwarf2_flag_true_p) follows this attribute. So we may
20632 end up accidently finding a declaration attribute that belongs
20633 to a different DIE referenced by the specification attribute,
20634 even though the given DIE does not have a declaration attribute. */
20635 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20636 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20637 }
20638
20639 /* Return the die giving the specification for DIE, if there is
20640 one. *SPEC_CU is the CU containing DIE on input, and the CU
20641 containing the return value on output. If there is no
20642 specification, but there is an abstract origin, that is
20643 returned. */
20644
20645 static struct die_info *
20646 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20647 {
20648 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20649 *spec_cu);
20650
20651 if (spec_attr == NULL)
20652 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20653
20654 if (spec_attr == NULL)
20655 return NULL;
20656 else
20657 return follow_die_ref (die, spec_attr, spec_cu);
20658 }
20659
20660 /* A convenience function to find the proper .debug_line section for a CU. */
20661
20662 static struct dwarf2_section_info *
20663 get_debug_line_section (struct dwarf2_cu *cu)
20664 {
20665 struct dwarf2_section_info *section;
20666 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20667
20668 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20669 DWO file. */
20670 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20671 section = &cu->dwo_unit->dwo_file->sections.line;
20672 else if (cu->per_cu->is_dwz)
20673 {
20674 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20675
20676 section = &dwz->line;
20677 }
20678 else
20679 section = &per_objfile->per_bfd->line;
20680
20681 return section;
20682 }
20683
20684 /* Read the statement program header starting at OFFSET in
20685 .debug_line, or .debug_line.dwo. Return a pointer
20686 to a struct line_header, allocated using xmalloc.
20687 Returns NULL if there is a problem reading the header, e.g., if it
20688 has a version we don't understand.
20689
20690 NOTE: the strings in the include directory and file name tables of
20691 the returned object point into the dwarf line section buffer,
20692 and must not be freed. */
20693
20694 static line_header_up
20695 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20696 {
20697 struct dwarf2_section_info *section;
20698 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20699
20700 section = get_debug_line_section (cu);
20701 section->read (per_objfile->objfile);
20702 if (section->buffer == NULL)
20703 {
20704 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20705 complaint (_("missing .debug_line.dwo section"));
20706 else
20707 complaint (_("missing .debug_line section"));
20708 return 0;
20709 }
20710
20711 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20712 per_objfile, section, &cu->header);
20713 }
20714
20715 /* Subroutine of dwarf_decode_lines to simplify it.
20716 Return the file name for the given file_entry.
20717 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
20718 If space for the result is malloc'd, *NAME_HOLDER will be set.
20719 Returns NULL if FILE_INDEX should be ignored, i.e., it is
20720 equivalent to CU_INFO. */
20721
20722 static const char *
20723 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
20724 const file_and_directory &cu_info,
20725 gdb::unique_xmalloc_ptr<char> *name_holder)
20726 {
20727 const char *include_name = fe.name;
20728 const char *include_name_to_compare = include_name;
20729
20730 const char *dir_name = fe.include_dir (lh);
20731
20732 gdb::unique_xmalloc_ptr<char> hold_compare;
20733 if (!IS_ABSOLUTE_PATH (include_name)
20734 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
20735 {
20736 /* Avoid creating a duplicate name for CU_INFO.
20737 We do this by comparing INCLUDE_NAME and CU_INFO.
20738 Before we do the comparison, however, we need to account
20739 for DIR_NAME and COMP_DIR.
20740 First prepend dir_name (if non-NULL). If we still don't
20741 have an absolute path prepend comp_dir (if non-NULL).
20742 However, the directory we record in the include-file's
20743 psymtab does not contain COMP_DIR (to match the
20744 corresponding symtab(s)).
20745
20746 Example:
20747
20748 bash$ cd /tmp
20749 bash$ gcc -g ./hello.c
20750 include_name = "hello.c"
20751 dir_name = "."
20752 DW_AT_comp_dir = comp_dir = "/tmp"
20753 DW_AT_name = "./hello.c"
20754
20755 */
20756
20757 if (dir_name != NULL)
20758 {
20759 name_holder->reset (concat (dir_name, SLASH_STRING,
20760 include_name, (char *) NULL));
20761 include_name = name_holder->get ();
20762 include_name_to_compare = include_name;
20763 }
20764 if (!IS_ABSOLUTE_PATH (include_name)
20765 && cu_info.get_comp_dir () != nullptr)
20766 {
20767 hold_compare.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20768 include_name, (char *) NULL));
20769 include_name_to_compare = hold_compare.get ();
20770 }
20771 }
20772
20773 gdb::unique_xmalloc_ptr<char> copied_name;
20774 const char *cu_filename = cu_info.get_name ();
20775 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
20776 {
20777 copied_name.reset (concat (cu_info.get_comp_dir (), SLASH_STRING,
20778 cu_filename, (char *) NULL));
20779 cu_filename = copied_name.get ();
20780 }
20781
20782 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
20783 return nullptr;
20784 return include_name;
20785 }
20786
20787 /* State machine to track the state of the line number program. */
20788
20789 class lnp_state_machine
20790 {
20791 public:
20792 /* Initialize a machine state for the start of a line number
20793 program. */
20794 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20795 bool record_lines_p);
20796
20797 file_entry *current_file ()
20798 {
20799 /* lh->file_names is 0-based, but the file name numbers in the
20800 statement program are 1-based. */
20801 return m_line_header->file_name_at (m_file);
20802 }
20803
20804 /* Record the line in the state machine. END_SEQUENCE is true if
20805 we're processing the end of a sequence. */
20806 void record_line (bool end_sequence);
20807
20808 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20809 nop-out rest of the lines in this sequence. */
20810 void check_line_address (struct dwarf2_cu *cu,
20811 const gdb_byte *line_ptr,
20812 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20813
20814 void handle_set_discriminator (unsigned int discriminator)
20815 {
20816 m_discriminator = discriminator;
20817 m_line_has_non_zero_discriminator |= discriminator != 0;
20818 }
20819
20820 /* Handle DW_LNE_set_address. */
20821 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20822 {
20823 m_op_index = 0;
20824 address += baseaddr;
20825 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20826 }
20827
20828 /* Handle DW_LNS_advance_pc. */
20829 void handle_advance_pc (CORE_ADDR adjust);
20830
20831 /* Handle a special opcode. */
20832 void handle_special_opcode (unsigned char op_code);
20833
20834 /* Handle DW_LNS_advance_line. */
20835 void handle_advance_line (int line_delta)
20836 {
20837 advance_line (line_delta);
20838 }
20839
20840 /* Handle DW_LNS_set_file. */
20841 void handle_set_file (file_name_index file);
20842
20843 /* Handle DW_LNS_negate_stmt. */
20844 void handle_negate_stmt ()
20845 {
20846 m_is_stmt = !m_is_stmt;
20847 }
20848
20849 /* Handle DW_LNS_const_add_pc. */
20850 void handle_const_add_pc ();
20851
20852 /* Handle DW_LNS_fixed_advance_pc. */
20853 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20854 {
20855 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20856 m_op_index = 0;
20857 }
20858
20859 /* Handle DW_LNS_copy. */
20860 void handle_copy ()
20861 {
20862 record_line (false);
20863 m_discriminator = 0;
20864 }
20865
20866 /* Handle DW_LNE_end_sequence. */
20867 void handle_end_sequence ()
20868 {
20869 m_currently_recording_lines = true;
20870 }
20871
20872 private:
20873 /* Advance the line by LINE_DELTA. */
20874 void advance_line (int line_delta)
20875 {
20876 m_line += line_delta;
20877
20878 if (line_delta != 0)
20879 m_line_has_non_zero_discriminator = m_discriminator != 0;
20880 }
20881
20882 struct dwarf2_cu *m_cu;
20883
20884 gdbarch *m_gdbarch;
20885
20886 /* True if we're recording lines.
20887 Otherwise we're building partial symtabs and are just interested in
20888 finding include files mentioned by the line number program. */
20889 bool m_record_lines_p;
20890
20891 /* The line number header. */
20892 line_header *m_line_header;
20893
20894 /* These are part of the standard DWARF line number state machine,
20895 and initialized according to the DWARF spec. */
20896
20897 unsigned char m_op_index = 0;
20898 /* The line table index of the current file. */
20899 file_name_index m_file = 1;
20900 unsigned int m_line = 1;
20901
20902 /* These are initialized in the constructor. */
20903
20904 CORE_ADDR m_address;
20905 bool m_is_stmt;
20906 unsigned int m_discriminator;
20907
20908 /* Additional bits of state we need to track. */
20909
20910 /* The last file that we called dwarf2_start_subfile for.
20911 This is only used for TLLs. */
20912 unsigned int m_last_file = 0;
20913 /* The last file a line number was recorded for. */
20914 struct subfile *m_last_subfile = NULL;
20915
20916 /* The address of the last line entry. */
20917 CORE_ADDR m_last_address;
20918
20919 /* Set to true when a previous line at the same address (using
20920 m_last_address) had m_is_stmt true. This is reset to false when a
20921 line entry at a new address (m_address different to m_last_address) is
20922 processed. */
20923 bool m_stmt_at_address = false;
20924
20925 /* When true, record the lines we decode. */
20926 bool m_currently_recording_lines = false;
20927
20928 /* The last line number that was recorded, used to coalesce
20929 consecutive entries for the same line. This can happen, for
20930 example, when discriminators are present. PR 17276. */
20931 unsigned int m_last_line = 0;
20932 bool m_line_has_non_zero_discriminator = false;
20933 };
20934
20935 void
20936 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20937 {
20938 CORE_ADDR addr_adj = (((m_op_index + adjust)
20939 / m_line_header->maximum_ops_per_instruction)
20940 * m_line_header->minimum_instruction_length);
20941 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20942 m_op_index = ((m_op_index + adjust)
20943 % m_line_header->maximum_ops_per_instruction);
20944 }
20945
20946 void
20947 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20948 {
20949 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20950 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20951 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20952 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20953 / m_line_header->maximum_ops_per_instruction)
20954 * m_line_header->minimum_instruction_length);
20955 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20956 m_op_index = ((m_op_index + adj_opcode_d)
20957 % m_line_header->maximum_ops_per_instruction);
20958
20959 int line_delta = m_line_header->line_base + adj_opcode_r;
20960 advance_line (line_delta);
20961 record_line (false);
20962 m_discriminator = 0;
20963 }
20964
20965 void
20966 lnp_state_machine::handle_set_file (file_name_index file)
20967 {
20968 m_file = file;
20969
20970 const file_entry *fe = current_file ();
20971 if (fe == NULL)
20972 dwarf2_debug_line_missing_file_complaint ();
20973 else if (m_record_lines_p)
20974 {
20975 const char *dir = fe->include_dir (m_line_header);
20976
20977 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20978 m_line_has_non_zero_discriminator = m_discriminator != 0;
20979 dwarf2_start_subfile (m_cu, fe->name, dir);
20980 }
20981 }
20982
20983 void
20984 lnp_state_machine::handle_const_add_pc ()
20985 {
20986 CORE_ADDR adjust
20987 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20988
20989 CORE_ADDR addr_adj
20990 = (((m_op_index + adjust)
20991 / m_line_header->maximum_ops_per_instruction)
20992 * m_line_header->minimum_instruction_length);
20993
20994 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20995 m_op_index = ((m_op_index + adjust)
20996 % m_line_header->maximum_ops_per_instruction);
20997 }
20998
20999 /* Return non-zero if we should add LINE to the line number table.
21000 LINE is the line to add, LAST_LINE is the last line that was added,
21001 LAST_SUBFILE is the subfile for LAST_LINE.
21002 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21003 had a non-zero discriminator.
21004
21005 We have to be careful in the presence of discriminators.
21006 E.g., for this line:
21007
21008 for (i = 0; i < 100000; i++);
21009
21010 clang can emit four line number entries for that one line,
21011 each with a different discriminator.
21012 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21013
21014 However, we want gdb to coalesce all four entries into one.
21015 Otherwise the user could stepi into the middle of the line and
21016 gdb would get confused about whether the pc really was in the
21017 middle of the line.
21018
21019 Things are further complicated by the fact that two consecutive
21020 line number entries for the same line is a heuristic used by gcc
21021 to denote the end of the prologue. So we can't just discard duplicate
21022 entries, we have to be selective about it. The heuristic we use is
21023 that we only collapse consecutive entries for the same line if at least
21024 one of those entries has a non-zero discriminator. PR 17276.
21025
21026 Note: Addresses in the line number state machine can never go backwards
21027 within one sequence, thus this coalescing is ok. */
21028
21029 static int
21030 dwarf_record_line_p (struct dwarf2_cu *cu,
21031 unsigned int line, unsigned int last_line,
21032 int line_has_non_zero_discriminator,
21033 struct subfile *last_subfile)
21034 {
21035 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21036 return 1;
21037 if (line != last_line)
21038 return 1;
21039 /* Same line for the same file that we've seen already.
21040 As a last check, for pr 17276, only record the line if the line
21041 has never had a non-zero discriminator. */
21042 if (!line_has_non_zero_discriminator)
21043 return 1;
21044 return 0;
21045 }
21046
21047 /* Use the CU's builder to record line number LINE beginning at
21048 address ADDRESS in the line table of subfile SUBFILE. */
21049
21050 static void
21051 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21052 unsigned int line, CORE_ADDR address, bool is_stmt,
21053 struct dwarf2_cu *cu)
21054 {
21055 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21056
21057 if (dwarf_line_debug)
21058 {
21059 fprintf_unfiltered (gdb_stdlog,
21060 "Recording line %u, file %s, address %s\n",
21061 line, lbasename (subfile->name),
21062 paddress (gdbarch, address));
21063 }
21064
21065 if (cu != nullptr)
21066 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21067 }
21068
21069 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21070 Mark the end of a set of line number records.
21071 The arguments are the same as for dwarf_record_line_1.
21072 If SUBFILE is NULL the request is ignored. */
21073
21074 static void
21075 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21076 CORE_ADDR address, struct dwarf2_cu *cu)
21077 {
21078 if (subfile == NULL)
21079 return;
21080
21081 if (dwarf_line_debug)
21082 {
21083 fprintf_unfiltered (gdb_stdlog,
21084 "Finishing current line, file %s, address %s\n",
21085 lbasename (subfile->name),
21086 paddress (gdbarch, address));
21087 }
21088
21089 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21090 }
21091
21092 void
21093 lnp_state_machine::record_line (bool end_sequence)
21094 {
21095 if (dwarf_line_debug)
21096 {
21097 fprintf_unfiltered (gdb_stdlog,
21098 "Processing actual line %u: file %u,"
21099 " address %s, is_stmt %u, discrim %u%s\n",
21100 m_line, m_file,
21101 paddress (m_gdbarch, m_address),
21102 m_is_stmt, m_discriminator,
21103 (end_sequence ? "\t(end sequence)" : ""));
21104 }
21105
21106 file_entry *fe = current_file ();
21107
21108 if (fe == NULL)
21109 dwarf2_debug_line_missing_file_complaint ();
21110 /* For now we ignore lines not starting on an instruction boundary.
21111 But not when processing end_sequence for compatibility with the
21112 previous version of the code. */
21113 else if (m_op_index == 0 || end_sequence)
21114 {
21115 fe->included_p = true;
21116 if (m_record_lines_p)
21117 {
21118 /* When we switch files we insert an end maker in the first file,
21119 switch to the second file and add a new line entry. The
21120 problem is that the end marker inserted in the first file will
21121 discard any previous line entries at the same address. If the
21122 line entries in the first file are marked as is-stmt, while
21123 the new line in the second file is non-stmt, then this means
21124 the end marker will discard is-stmt lines so we can have a
21125 non-stmt line. This means that there are less addresses at
21126 which the user can insert a breakpoint.
21127
21128 To improve this we track the last address in m_last_address,
21129 and whether we have seen an is-stmt at this address. Then
21130 when switching files, if we have seen a stmt at the current
21131 address, and we are switching to create a non-stmt line, then
21132 discard the new line. */
21133 bool file_changed
21134 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21135 bool ignore_this_line
21136 = ((file_changed && !end_sequence && m_last_address == m_address
21137 && !m_is_stmt && m_stmt_at_address)
21138 || (!end_sequence && m_line == 0));
21139
21140 if ((file_changed && !ignore_this_line) || end_sequence)
21141 {
21142 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21143 m_currently_recording_lines ? m_cu : nullptr);
21144 }
21145
21146 if (!end_sequence && !ignore_this_line)
21147 {
21148 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21149
21150 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21151 m_line_has_non_zero_discriminator,
21152 m_last_subfile))
21153 {
21154 buildsym_compunit *builder = m_cu->get_builder ();
21155 dwarf_record_line_1 (m_gdbarch,
21156 builder->get_current_subfile (),
21157 m_line, m_address, is_stmt,
21158 m_currently_recording_lines ? m_cu : nullptr);
21159 }
21160 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21161 m_last_line = m_line;
21162 }
21163 }
21164 }
21165
21166 /* Track whether we have seen any m_is_stmt true at m_address in case we
21167 have multiple line table entries all at m_address. */
21168 if (m_last_address != m_address)
21169 {
21170 m_stmt_at_address = false;
21171 m_last_address = m_address;
21172 }
21173 m_stmt_at_address |= m_is_stmt;
21174 }
21175
21176 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21177 line_header *lh, bool record_lines_p)
21178 {
21179 m_cu = cu;
21180 m_gdbarch = arch;
21181 m_record_lines_p = record_lines_p;
21182 m_line_header = lh;
21183
21184 m_currently_recording_lines = true;
21185
21186 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21187 was a line entry for it so that the backend has a chance to adjust it
21188 and also record it in case it needs it. This is currently used by MIPS
21189 code, cf. `mips_adjust_dwarf2_line'. */
21190 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21191 m_is_stmt = lh->default_is_stmt;
21192 m_discriminator = 0;
21193
21194 m_last_address = m_address;
21195 m_stmt_at_address = false;
21196 }
21197
21198 void
21199 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21200 const gdb_byte *line_ptr,
21201 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21202 {
21203 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21204 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21205 located at 0x0. In this case, additionally check that if
21206 ADDRESS < UNRELOCATED_LOWPC. */
21207
21208 if ((address == 0 && address < unrelocated_lowpc)
21209 || address == (CORE_ADDR) -1)
21210 {
21211 /* This line table is for a function which has been
21212 GCd by the linker. Ignore it. PR gdb/12528 */
21213
21214 struct objfile *objfile = cu->per_objfile->objfile;
21215 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21216
21217 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21218 line_offset, objfile_name (objfile));
21219 m_currently_recording_lines = false;
21220 /* Note: m_currently_recording_lines is left as false until we see
21221 DW_LNE_end_sequence. */
21222 }
21223 }
21224
21225 /* Subroutine of dwarf_decode_lines to simplify it.
21226 Process the line number information in LH.
21227 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21228 program in order to set included_p for every referenced header. */
21229
21230 static void
21231 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21232 const int decode_for_pst_p, CORE_ADDR lowpc)
21233 {
21234 const gdb_byte *line_ptr, *extended_end;
21235 const gdb_byte *line_end;
21236 unsigned int bytes_read, extended_len;
21237 unsigned char op_code, extended_op;
21238 CORE_ADDR baseaddr;
21239 struct objfile *objfile = cu->per_objfile->objfile;
21240 bfd *abfd = objfile->obfd;
21241 struct gdbarch *gdbarch = objfile->arch ();
21242 /* True if we're recording line info (as opposed to building partial
21243 symtabs and just interested in finding include files mentioned by
21244 the line number program). */
21245 bool record_lines_p = !decode_for_pst_p;
21246
21247 baseaddr = objfile->text_section_offset ();
21248
21249 line_ptr = lh->statement_program_start;
21250 line_end = lh->statement_program_end;
21251
21252 /* Read the statement sequences until there's nothing left. */
21253 while (line_ptr < line_end)
21254 {
21255 /* The DWARF line number program state machine. Reset the state
21256 machine at the start of each sequence. */
21257 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21258 bool end_sequence = false;
21259
21260 if (record_lines_p)
21261 {
21262 /* Start a subfile for the current file of the state
21263 machine. */
21264 const file_entry *fe = state_machine.current_file ();
21265
21266 if (fe != NULL)
21267 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21268 }
21269
21270 /* Decode the table. */
21271 while (line_ptr < line_end && !end_sequence)
21272 {
21273 op_code = read_1_byte (abfd, line_ptr);
21274 line_ptr += 1;
21275
21276 if (op_code >= lh->opcode_base)
21277 {
21278 /* Special opcode. */
21279 state_machine.handle_special_opcode (op_code);
21280 }
21281 else switch (op_code)
21282 {
21283 case DW_LNS_extended_op:
21284 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21285 &bytes_read);
21286 line_ptr += bytes_read;
21287 extended_end = line_ptr + extended_len;
21288 extended_op = read_1_byte (abfd, line_ptr);
21289 line_ptr += 1;
21290 if (DW_LNE_lo_user <= extended_op
21291 && extended_op <= DW_LNE_hi_user)
21292 {
21293 /* Vendor extension, ignore. */
21294 line_ptr = extended_end;
21295 break;
21296 }
21297 switch (extended_op)
21298 {
21299 case DW_LNE_end_sequence:
21300 state_machine.handle_end_sequence ();
21301 end_sequence = true;
21302 break;
21303 case DW_LNE_set_address:
21304 {
21305 CORE_ADDR address
21306 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21307 line_ptr += bytes_read;
21308
21309 state_machine.check_line_address (cu, line_ptr,
21310 lowpc - baseaddr, address);
21311 state_machine.handle_set_address (baseaddr, address);
21312 }
21313 break;
21314 case DW_LNE_define_file:
21315 {
21316 const char *cur_file;
21317 unsigned int mod_time, length;
21318 dir_index dindex;
21319
21320 cur_file = read_direct_string (abfd, line_ptr,
21321 &bytes_read);
21322 line_ptr += bytes_read;
21323 dindex = (dir_index)
21324 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21325 line_ptr += bytes_read;
21326 mod_time =
21327 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21328 line_ptr += bytes_read;
21329 length =
21330 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21331 line_ptr += bytes_read;
21332 lh->add_file_name (cur_file, dindex, mod_time, length);
21333 }
21334 break;
21335 case DW_LNE_set_discriminator:
21336 {
21337 /* The discriminator is not interesting to the
21338 debugger; just ignore it. We still need to
21339 check its value though:
21340 if there are consecutive entries for the same
21341 (non-prologue) line we want to coalesce them.
21342 PR 17276. */
21343 unsigned int discr
21344 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21345 line_ptr += bytes_read;
21346
21347 state_machine.handle_set_discriminator (discr);
21348 }
21349 break;
21350 default:
21351 complaint (_("mangled .debug_line section"));
21352 return;
21353 }
21354 /* Make sure that we parsed the extended op correctly. If e.g.
21355 we expected a different address size than the producer used,
21356 we may have read the wrong number of bytes. */
21357 if (line_ptr != extended_end)
21358 {
21359 complaint (_("mangled .debug_line section"));
21360 return;
21361 }
21362 break;
21363 case DW_LNS_copy:
21364 state_machine.handle_copy ();
21365 break;
21366 case DW_LNS_advance_pc:
21367 {
21368 CORE_ADDR adjust
21369 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21370 line_ptr += bytes_read;
21371
21372 state_machine.handle_advance_pc (adjust);
21373 }
21374 break;
21375 case DW_LNS_advance_line:
21376 {
21377 int line_delta
21378 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21379 line_ptr += bytes_read;
21380
21381 state_machine.handle_advance_line (line_delta);
21382 }
21383 break;
21384 case DW_LNS_set_file:
21385 {
21386 file_name_index file
21387 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21388 &bytes_read);
21389 line_ptr += bytes_read;
21390
21391 state_machine.handle_set_file (file);
21392 }
21393 break;
21394 case DW_LNS_set_column:
21395 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21396 line_ptr += bytes_read;
21397 break;
21398 case DW_LNS_negate_stmt:
21399 state_machine.handle_negate_stmt ();
21400 break;
21401 case DW_LNS_set_basic_block:
21402 break;
21403 /* Add to the address register of the state machine the
21404 address increment value corresponding to special opcode
21405 255. I.e., this value is scaled by the minimum
21406 instruction length since special opcode 255 would have
21407 scaled the increment. */
21408 case DW_LNS_const_add_pc:
21409 state_machine.handle_const_add_pc ();
21410 break;
21411 case DW_LNS_fixed_advance_pc:
21412 {
21413 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21414 line_ptr += 2;
21415
21416 state_machine.handle_fixed_advance_pc (addr_adj);
21417 }
21418 break;
21419 default:
21420 {
21421 /* Unknown standard opcode, ignore it. */
21422 int i;
21423
21424 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21425 {
21426 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21427 line_ptr += bytes_read;
21428 }
21429 }
21430 }
21431 }
21432
21433 if (!end_sequence)
21434 dwarf2_debug_line_missing_end_sequence_complaint ();
21435
21436 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21437 in which case we still finish recording the last line). */
21438 state_machine.record_line (true);
21439 }
21440 }
21441
21442 /* Decode the Line Number Program (LNP) for the given line_header
21443 structure and CU. The actual information extracted and the type
21444 of structures created from the LNP depends on the value of PST.
21445
21446 1. If PST is NULL, then this procedure uses the data from the program
21447 to create all necessary symbol tables, and their linetables.
21448
21449 2. If PST is not NULL, this procedure reads the program to determine
21450 the list of files included by the unit represented by PST, and
21451 builds all the associated partial symbol tables.
21452
21453 FND holds the CU file name and directory, if known.
21454 It is used for relative paths in the line table.
21455
21456 NOTE: It is important that psymtabs have the same file name (via
21457 strcmp) as the corresponding symtab. Since the directory is not
21458 used in the name of the symtab we don't use it in the name of the
21459 psymtabs we create. E.g. expand_line_sal requires this when
21460 finding psymtabs to expand. A good testcase for this is
21461 mb-inline.exp.
21462
21463 LOWPC is the lowest address in CU (or 0 if not known).
21464
21465 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21466 for its PC<->lines mapping information. Otherwise only the filename
21467 table is read in. */
21468
21469 static void
21470 dwarf_decode_lines (struct line_header *lh, const file_and_directory &fnd,
21471 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21472 CORE_ADDR lowpc, int decode_mapping)
21473 {
21474 struct objfile *objfile = cu->per_objfile->objfile;
21475 const int decode_for_pst_p = (pst != NULL);
21476
21477 if (decode_mapping)
21478 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21479
21480 if (decode_for_pst_p)
21481 {
21482 /* Now that we're done scanning the Line Header Program, we can
21483 create the psymtab of each included file. */
21484 for (auto &file_entry : lh->file_names ())
21485 if (file_entry.included_p)
21486 {
21487 gdb::unique_xmalloc_ptr<char> name_holder;
21488 const char *include_name =
21489 compute_include_file_name (lh, file_entry, fnd, &name_holder);
21490 if (include_name != NULL)
21491 dwarf2_create_include_psymtab
21492 (cu->per_objfile->per_bfd, include_name, pst,
21493 cu->per_objfile->per_bfd->partial_symtabs.get (),
21494 objfile->per_bfd);
21495 }
21496 }
21497 else
21498 {
21499 /* Make sure a symtab is created for every file, even files
21500 which contain only variables (i.e. no code with associated
21501 line numbers). */
21502 buildsym_compunit *builder = cu->get_builder ();
21503 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21504
21505 for (auto &fe : lh->file_names ())
21506 {
21507 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21508 if (builder->get_current_subfile ()->symtab == NULL)
21509 {
21510 builder->get_current_subfile ()->symtab
21511 = allocate_symtab (cust,
21512 builder->get_current_subfile ()->name);
21513 }
21514 fe.symtab = builder->get_current_subfile ()->symtab;
21515 }
21516 }
21517 }
21518
21519 /* Start a subfile for DWARF. FILENAME is the name of the file and
21520 DIRNAME the name of the source directory which contains FILENAME
21521 or NULL if not known.
21522 This routine tries to keep line numbers from identical absolute and
21523 relative file names in a common subfile.
21524
21525 Using the `list' example from the GDB testsuite, which resides in
21526 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21527 of /srcdir/list0.c yields the following debugging information for list0.c:
21528
21529 DW_AT_name: /srcdir/list0.c
21530 DW_AT_comp_dir: /compdir
21531 files.files[0].name: list0.h
21532 files.files[0].dir: /srcdir
21533 files.files[1].name: list0.c
21534 files.files[1].dir: /srcdir
21535
21536 The line number information for list0.c has to end up in a single
21537 subfile, so that `break /srcdir/list0.c:1' works as expected.
21538 start_subfile will ensure that this happens provided that we pass the
21539 concatenation of files.files[1].dir and files.files[1].name as the
21540 subfile's name. */
21541
21542 static void
21543 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21544 const char *dirname)
21545 {
21546 gdb::unique_xmalloc_ptr<char> copy;
21547
21548 /* In order not to lose the line information directory,
21549 we concatenate it to the filename when it makes sense.
21550 Note that the Dwarf3 standard says (speaking of filenames in line
21551 information): ``The directory index is ignored for file names
21552 that represent full path names''. Thus ignoring dirname in the
21553 `else' branch below isn't an issue. */
21554
21555 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21556 {
21557 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21558 filename = copy.get ();
21559 }
21560
21561 cu->get_builder ()->start_subfile (filename);
21562 }
21563
21564 static void
21565 var_decode_location (struct attribute *attr, struct symbol *sym,
21566 struct dwarf2_cu *cu)
21567 {
21568 struct objfile *objfile = cu->per_objfile->objfile;
21569 struct comp_unit_head *cu_header = &cu->header;
21570
21571 /* NOTE drow/2003-01-30: There used to be a comment and some special
21572 code here to turn a symbol with DW_AT_external and a
21573 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21574 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21575 with some versions of binutils) where shared libraries could have
21576 relocations against symbols in their debug information - the
21577 minimal symbol would have the right address, but the debug info
21578 would not. It's no longer necessary, because we will explicitly
21579 apply relocations when we read in the debug information now. */
21580
21581 /* A DW_AT_location attribute with no contents indicates that a
21582 variable has been optimized away. */
21583 if (attr->form_is_block () && attr->as_block ()->size == 0)
21584 {
21585 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21586 return;
21587 }
21588
21589 /* Handle one degenerate form of location expression specially, to
21590 preserve GDB's previous behavior when section offsets are
21591 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21592 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21593
21594 if (attr->form_is_block ())
21595 {
21596 struct dwarf_block *block = attr->as_block ();
21597
21598 if ((block->data[0] == DW_OP_addr
21599 && block->size == 1 + cu_header->addr_size)
21600 || ((block->data[0] == DW_OP_GNU_addr_index
21601 || block->data[0] == DW_OP_addrx)
21602 && (block->size
21603 == 1 + leb128_size (&block->data[1]))))
21604 {
21605 unsigned int dummy;
21606
21607 if (block->data[0] == DW_OP_addr)
21608 SET_SYMBOL_VALUE_ADDRESS
21609 (sym, cu->header.read_address (objfile->obfd,
21610 block->data + 1,
21611 &dummy));
21612 else
21613 SET_SYMBOL_VALUE_ADDRESS
21614 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21615 &dummy));
21616 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21617 fixup_symbol_section (sym, objfile);
21618 SET_SYMBOL_VALUE_ADDRESS
21619 (sym,
21620 SYMBOL_VALUE_ADDRESS (sym)
21621 + objfile->section_offsets[sym->section_index ()]);
21622 return;
21623 }
21624 }
21625
21626 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21627 expression evaluator, and use LOC_COMPUTED only when necessary
21628 (i.e. when the value of a register or memory location is
21629 referenced, or a thread-local block, etc.). Then again, it might
21630 not be worthwhile. I'm assuming that it isn't unless performance
21631 or memory numbers show me otherwise. */
21632
21633 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21634
21635 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21636 cu->has_loclist = true;
21637 }
21638
21639 /* Given a pointer to a DWARF information entry, figure out if we need
21640 to make a symbol table entry for it, and if so, create a new entry
21641 and return a pointer to it.
21642 If TYPE is NULL, determine symbol type from the die, otherwise
21643 used the passed type.
21644 If SPACE is not NULL, use it to hold the new symbol. If it is
21645 NULL, allocate a new symbol on the objfile's obstack. */
21646
21647 static struct symbol *
21648 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21649 struct symbol *space)
21650 {
21651 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21652 struct objfile *objfile = per_objfile->objfile;
21653 struct gdbarch *gdbarch = objfile->arch ();
21654 struct symbol *sym = NULL;
21655 const char *name;
21656 struct attribute *attr = NULL;
21657 struct attribute *attr2 = NULL;
21658 CORE_ADDR baseaddr;
21659 struct pending **list_to_add = NULL;
21660
21661 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21662
21663 baseaddr = objfile->text_section_offset ();
21664
21665 name = dwarf2_name (die, cu);
21666 if (name)
21667 {
21668 int suppress_add = 0;
21669
21670 if (space)
21671 sym = space;
21672 else
21673 sym = new (&objfile->objfile_obstack) symbol;
21674 OBJSTAT (objfile, n_syms++);
21675
21676 /* Cache this symbol's name and the name's demangled form (if any). */
21677 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
21678 /* Fortran does not have mangling standard and the mangling does differ
21679 between gfortran, iFort etc. */
21680 const char *physname
21681 = (cu->per_cu->lang == language_fortran
21682 ? dwarf2_full_name (name, die, cu)
21683 : dwarf2_physname (name, die, cu));
21684 const char *linkagename = dw2_linkage_name (die, cu);
21685
21686 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
21687 sym->set_linkage_name (physname);
21688 else
21689 {
21690 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21691 sym->set_linkage_name (linkagename);
21692 }
21693
21694 /* Handle DW_AT_artificial. */
21695 attr = dwarf2_attr (die, DW_AT_artificial, cu);
21696 if (attr != nullptr)
21697 sym->artificial = attr->as_boolean ();
21698
21699 /* Default assumptions.
21700 Use the passed type or decode it from the die. */
21701 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21702 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21703 if (type != NULL)
21704 SYMBOL_TYPE (sym) = type;
21705 else
21706 SYMBOL_TYPE (sym) = die_type (die, cu);
21707 attr = dwarf2_attr (die,
21708 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21709 cu);
21710 if (attr != nullptr)
21711 SYMBOL_LINE (sym) = attr->constant_value (0);
21712
21713 attr = dwarf2_attr (die,
21714 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21715 cu);
21716 if (attr != nullptr && attr->is_nonnegative ())
21717 {
21718 file_name_index file_index
21719 = (file_name_index) attr->as_nonnegative ();
21720 struct file_entry *fe;
21721
21722 if (cu->line_header != NULL)
21723 fe = cu->line_header->file_name_at (file_index);
21724 else
21725 fe = NULL;
21726
21727 if (fe == NULL)
21728 complaint (_("file index out of range"));
21729 else
21730 symbol_set_symtab (sym, fe->symtab);
21731 }
21732
21733 switch (die->tag)
21734 {
21735 case DW_TAG_label:
21736 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21737 if (attr != nullptr)
21738 {
21739 CORE_ADDR addr;
21740
21741 addr = attr->as_address ();
21742 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21743 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21744 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21745 }
21746 else
21747 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21748 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21749 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21750 add_symbol_to_list (sym, cu->list_in_scope);
21751 break;
21752 case DW_TAG_subprogram:
21753 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21754 finish_block. */
21755 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21756 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21757 if ((attr2 != nullptr && attr2->as_boolean ())
21758 || cu->per_cu->lang == language_ada
21759 || cu->per_cu->lang == language_fortran)
21760 {
21761 /* Subprograms marked external are stored as a global symbol.
21762 Ada and Fortran subprograms, whether marked external or
21763 not, are always stored as a global symbol, because we want
21764 to be able to access them globally. For instance, we want
21765 to be able to break on a nested subprogram without having
21766 to specify the context. */
21767 list_to_add = cu->get_builder ()->get_global_symbols ();
21768 }
21769 else
21770 {
21771 list_to_add = cu->list_in_scope;
21772 }
21773 break;
21774 case DW_TAG_inlined_subroutine:
21775 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21776 finish_block. */
21777 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21778 SYMBOL_INLINED (sym) = 1;
21779 list_to_add = cu->list_in_scope;
21780 break;
21781 case DW_TAG_template_value_param:
21782 suppress_add = 1;
21783 /* Fall through. */
21784 case DW_TAG_constant:
21785 case DW_TAG_variable:
21786 case DW_TAG_member:
21787 /* Compilation with minimal debug info may result in
21788 variables with missing type entries. Change the
21789 misleading `void' type to something sensible. */
21790 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21791 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21792
21793 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21794 /* In the case of DW_TAG_member, we should only be called for
21795 static const members. */
21796 if (die->tag == DW_TAG_member)
21797 {
21798 /* dwarf2_add_field uses die_is_declaration,
21799 so we do the same. */
21800 gdb_assert (die_is_declaration (die, cu));
21801 gdb_assert (attr);
21802 }
21803 if (attr != nullptr)
21804 {
21805 dwarf2_const_value (attr, sym, cu);
21806 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21807 if (!suppress_add)
21808 {
21809 if (attr2 != nullptr && attr2->as_boolean ())
21810 list_to_add = cu->get_builder ()->get_global_symbols ();
21811 else
21812 list_to_add = cu->list_in_scope;
21813 }
21814 break;
21815 }
21816 attr = dwarf2_attr (die, DW_AT_location, cu);
21817 if (attr != nullptr)
21818 {
21819 var_decode_location (attr, sym, cu);
21820 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21821
21822 /* Fortran explicitly imports any global symbols to the local
21823 scope by DW_TAG_common_block. */
21824 if (cu->per_cu->lang == language_fortran && die->parent
21825 && die->parent->tag == DW_TAG_common_block)
21826 attr2 = NULL;
21827
21828 if (SYMBOL_CLASS (sym) == LOC_STATIC
21829 && SYMBOL_VALUE_ADDRESS (sym) == 0
21830 && !per_objfile->per_bfd->has_section_at_zero)
21831 {
21832 /* When a static variable is eliminated by the linker,
21833 the corresponding debug information is not stripped
21834 out, but the variable address is set to null;
21835 do not add such variables into symbol table. */
21836 }
21837 else if (attr2 != nullptr && attr2->as_boolean ())
21838 {
21839 if (SYMBOL_CLASS (sym) == LOC_STATIC
21840 && (objfile->flags & OBJF_MAINLINE) == 0
21841 && per_objfile->per_bfd->can_copy)
21842 {
21843 /* A global static variable might be subject to
21844 copy relocation. We first check for a local
21845 minsym, though, because maybe the symbol was
21846 marked hidden, in which case this would not
21847 apply. */
21848 bound_minimal_symbol found
21849 = (lookup_minimal_symbol_linkage
21850 (sym->linkage_name (), objfile));
21851 if (found.minsym != nullptr)
21852 sym->maybe_copied = 1;
21853 }
21854
21855 /* A variable with DW_AT_external is never static,
21856 but it may be block-scoped. */
21857 list_to_add
21858 = ((cu->list_in_scope
21859 == cu->get_builder ()->get_file_symbols ())
21860 ? cu->get_builder ()->get_global_symbols ()
21861 : cu->list_in_scope);
21862 }
21863 else
21864 list_to_add = cu->list_in_scope;
21865 }
21866 else
21867 {
21868 /* We do not know the address of this symbol.
21869 If it is an external symbol and we have type information
21870 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21871 The address of the variable will then be determined from
21872 the minimal symbol table whenever the variable is
21873 referenced. */
21874 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21875
21876 /* Fortran explicitly imports any global symbols to the local
21877 scope by DW_TAG_common_block. */
21878 if (cu->per_cu->lang == language_fortran && die->parent
21879 && die->parent->tag == DW_TAG_common_block)
21880 {
21881 /* SYMBOL_CLASS doesn't matter here because
21882 read_common_block is going to reset it. */
21883 if (!suppress_add)
21884 list_to_add = cu->list_in_scope;
21885 }
21886 else if (attr2 != nullptr && attr2->as_boolean ()
21887 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21888 {
21889 /* A variable with DW_AT_external is never static, but it
21890 may be block-scoped. */
21891 list_to_add
21892 = ((cu->list_in_scope
21893 == cu->get_builder ()->get_file_symbols ())
21894 ? cu->get_builder ()->get_global_symbols ()
21895 : cu->list_in_scope);
21896
21897 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21898 }
21899 else if (!die_is_declaration (die, cu))
21900 {
21901 /* Use the default LOC_OPTIMIZED_OUT class. */
21902 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21903 if (!suppress_add)
21904 list_to_add = cu->list_in_scope;
21905 }
21906 }
21907 break;
21908 case DW_TAG_formal_parameter:
21909 {
21910 /* If we are inside a function, mark this as an argument. If
21911 not, we might be looking at an argument to an inlined function
21912 when we do not have enough information to show inlined frames;
21913 pretend it's a local variable in that case so that the user can
21914 still see it. */
21915 struct context_stack *curr
21916 = cu->get_builder ()->get_current_context_stack ();
21917 if (curr != nullptr && curr->name != nullptr)
21918 SYMBOL_IS_ARGUMENT (sym) = 1;
21919 attr = dwarf2_attr (die, DW_AT_location, cu);
21920 if (attr != nullptr)
21921 {
21922 var_decode_location (attr, sym, cu);
21923 }
21924 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21925 if (attr != nullptr)
21926 {
21927 dwarf2_const_value (attr, sym, cu);
21928 }
21929
21930 list_to_add = cu->list_in_scope;
21931 }
21932 break;
21933 case DW_TAG_unspecified_parameters:
21934 /* From varargs functions; gdb doesn't seem to have any
21935 interest in this information, so just ignore it for now.
21936 (FIXME?) */
21937 break;
21938 case DW_TAG_template_type_param:
21939 suppress_add = 1;
21940 /* Fall through. */
21941 case DW_TAG_class_type:
21942 case DW_TAG_interface_type:
21943 case DW_TAG_structure_type:
21944 case DW_TAG_union_type:
21945 case DW_TAG_set_type:
21946 case DW_TAG_enumeration_type:
21947 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21948 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21949
21950 {
21951 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21952 really ever be static objects: otherwise, if you try
21953 to, say, break of a class's method and you're in a file
21954 which doesn't mention that class, it won't work unless
21955 the check for all static symbols in lookup_symbol_aux
21956 saves you. See the OtherFileClass tests in
21957 gdb.c++/namespace.exp. */
21958
21959 if (!suppress_add)
21960 {
21961 buildsym_compunit *builder = cu->get_builder ();
21962 list_to_add
21963 = (cu->list_in_scope == builder->get_file_symbols ()
21964 && cu->per_cu->lang == language_cplus
21965 ? builder->get_global_symbols ()
21966 : cu->list_in_scope);
21967
21968 /* The semantics of C++ state that "struct foo {
21969 ... }" also defines a typedef for "foo". */
21970 if (cu->per_cu->lang == language_cplus
21971 || cu->per_cu->lang == language_ada
21972 || cu->per_cu->lang == language_d
21973 || cu->per_cu->lang == language_rust)
21974 {
21975 /* The symbol's name is already allocated along
21976 with this objfile, so we don't need to
21977 duplicate it for the type. */
21978 if (SYMBOL_TYPE (sym)->name () == 0)
21979 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21980 }
21981 }
21982 }
21983 break;
21984 case DW_TAG_typedef:
21985 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21986 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21987 list_to_add = cu->list_in_scope;
21988 break;
21989 case DW_TAG_array_type:
21990 case DW_TAG_base_type:
21991 case DW_TAG_subrange_type:
21992 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21993 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21994 list_to_add = cu->list_in_scope;
21995 break;
21996 case DW_TAG_enumerator:
21997 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21998 if (attr != nullptr)
21999 {
22000 dwarf2_const_value (attr, sym, cu);
22001 }
22002 {
22003 /* NOTE: carlton/2003-11-10: See comment above in the
22004 DW_TAG_class_type, etc. block. */
22005
22006 list_to_add
22007 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22008 && cu->per_cu->lang == language_cplus
22009 ? cu->get_builder ()->get_global_symbols ()
22010 : cu->list_in_scope);
22011 }
22012 break;
22013 case DW_TAG_imported_declaration:
22014 case DW_TAG_namespace:
22015 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22016 list_to_add = cu->get_builder ()->get_global_symbols ();
22017 break;
22018 case DW_TAG_module:
22019 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22020 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22021 list_to_add = cu->get_builder ()->get_global_symbols ();
22022 break;
22023 case DW_TAG_common_block:
22024 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22025 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22026 add_symbol_to_list (sym, cu->list_in_scope);
22027 break;
22028 default:
22029 /* Not a tag we recognize. Hopefully we aren't processing
22030 trash data, but since we must specifically ignore things
22031 we don't recognize, there is nothing else we should do at
22032 this point. */
22033 complaint (_("unsupported tag: '%s'"),
22034 dwarf_tag_name (die->tag));
22035 break;
22036 }
22037
22038 if (suppress_add)
22039 {
22040 sym->hash_next = objfile->template_symbols;
22041 objfile->template_symbols = sym;
22042 list_to_add = NULL;
22043 }
22044
22045 if (list_to_add != NULL)
22046 add_symbol_to_list (sym, list_to_add);
22047
22048 /* For the benefit of old versions of GCC, check for anonymous
22049 namespaces based on the demangled name. */
22050 if (!cu->processing_has_namespace_info
22051 && cu->per_cu->lang == language_cplus)
22052 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22053 }
22054 return (sym);
22055 }
22056
22057 /* Given an attr with a DW_FORM_dataN value in host byte order,
22058 zero-extend it as appropriate for the symbol's type. The DWARF
22059 standard (v4) is not entirely clear about the meaning of using
22060 DW_FORM_dataN for a constant with a signed type, where the type is
22061 wider than the data. The conclusion of a discussion on the DWARF
22062 list was that this is unspecified. We choose to always zero-extend
22063 because that is the interpretation long in use by GCC. */
22064
22065 static gdb_byte *
22066 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22067 struct dwarf2_cu *cu, LONGEST *value, int bits)
22068 {
22069 struct objfile *objfile = cu->per_objfile->objfile;
22070 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22071 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22072 LONGEST l = attr->constant_value (0);
22073
22074 if (bits < sizeof (*value) * 8)
22075 {
22076 l &= ((LONGEST) 1 << bits) - 1;
22077 *value = l;
22078 }
22079 else if (bits == sizeof (*value) * 8)
22080 *value = l;
22081 else
22082 {
22083 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22084 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22085 return bytes;
22086 }
22087
22088 return NULL;
22089 }
22090
22091 /* Read a constant value from an attribute. Either set *VALUE, or if
22092 the value does not fit in *VALUE, set *BYTES - either already
22093 allocated on the objfile obstack, or newly allocated on OBSTACK,
22094 or, set *BATON, if we translated the constant to a location
22095 expression. */
22096
22097 static void
22098 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22099 const char *name, struct obstack *obstack,
22100 struct dwarf2_cu *cu,
22101 LONGEST *value, const gdb_byte **bytes,
22102 struct dwarf2_locexpr_baton **baton)
22103 {
22104 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22105 struct objfile *objfile = per_objfile->objfile;
22106 struct comp_unit_head *cu_header = &cu->header;
22107 struct dwarf_block *blk;
22108 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22109 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22110
22111 *value = 0;
22112 *bytes = NULL;
22113 *baton = NULL;
22114
22115 switch (attr->form)
22116 {
22117 case DW_FORM_addr:
22118 case DW_FORM_addrx:
22119 case DW_FORM_GNU_addr_index:
22120 {
22121 gdb_byte *data;
22122
22123 if (TYPE_LENGTH (type) != cu_header->addr_size)
22124 dwarf2_const_value_length_mismatch_complaint (name,
22125 cu_header->addr_size,
22126 TYPE_LENGTH (type));
22127 /* Symbols of this form are reasonably rare, so we just
22128 piggyback on the existing location code rather than writing
22129 a new implementation of symbol_computed_ops. */
22130 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22131 (*baton)->per_objfile = per_objfile;
22132 (*baton)->per_cu = cu->per_cu;
22133 gdb_assert ((*baton)->per_cu);
22134
22135 (*baton)->size = 2 + cu_header->addr_size;
22136 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22137 (*baton)->data = data;
22138
22139 data[0] = DW_OP_addr;
22140 store_unsigned_integer (&data[1], cu_header->addr_size,
22141 byte_order, attr->as_address ());
22142 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22143 }
22144 break;
22145 case DW_FORM_string:
22146 case DW_FORM_strp:
22147 case DW_FORM_strx:
22148 case DW_FORM_GNU_str_index:
22149 case DW_FORM_GNU_strp_alt:
22150 /* The string is already allocated on the objfile obstack, point
22151 directly to it. */
22152 *bytes = (const gdb_byte *) attr->as_string ();
22153 break;
22154 case DW_FORM_block1:
22155 case DW_FORM_block2:
22156 case DW_FORM_block4:
22157 case DW_FORM_block:
22158 case DW_FORM_exprloc:
22159 case DW_FORM_data16:
22160 blk = attr->as_block ();
22161 if (TYPE_LENGTH (type) != blk->size)
22162 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22163 TYPE_LENGTH (type));
22164 *bytes = blk->data;
22165 break;
22166
22167 /* The DW_AT_const_value attributes are supposed to carry the
22168 symbol's value "represented as it would be on the target
22169 architecture." By the time we get here, it's already been
22170 converted to host endianness, so we just need to sign- or
22171 zero-extend it as appropriate. */
22172 case DW_FORM_data1:
22173 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22174 break;
22175 case DW_FORM_data2:
22176 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22177 break;
22178 case DW_FORM_data4:
22179 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22180 break;
22181 case DW_FORM_data8:
22182 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22183 break;
22184
22185 case DW_FORM_sdata:
22186 case DW_FORM_implicit_const:
22187 *value = attr->as_signed ();
22188 break;
22189
22190 case DW_FORM_udata:
22191 *value = attr->as_unsigned ();
22192 break;
22193
22194 default:
22195 complaint (_("unsupported const value attribute form: '%s'"),
22196 dwarf_form_name (attr->form));
22197 *value = 0;
22198 break;
22199 }
22200 }
22201
22202
22203 /* Copy constant value from an attribute to a symbol. */
22204
22205 static void
22206 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22207 struct dwarf2_cu *cu)
22208 {
22209 struct objfile *objfile = cu->per_objfile->objfile;
22210 LONGEST value;
22211 const gdb_byte *bytes;
22212 struct dwarf2_locexpr_baton *baton;
22213
22214 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22215 sym->print_name (),
22216 &objfile->objfile_obstack, cu,
22217 &value, &bytes, &baton);
22218
22219 if (baton != NULL)
22220 {
22221 SYMBOL_LOCATION_BATON (sym) = baton;
22222 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22223 }
22224 else if (bytes != NULL)
22225 {
22226 SYMBOL_VALUE_BYTES (sym) = bytes;
22227 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22228 }
22229 else
22230 {
22231 SYMBOL_VALUE (sym) = value;
22232 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22233 }
22234 }
22235
22236 /* Return the type of the die in question using its DW_AT_type attribute. */
22237
22238 static struct type *
22239 die_type (struct die_info *die, struct dwarf2_cu *cu)
22240 {
22241 struct attribute *type_attr;
22242
22243 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22244 if (!type_attr)
22245 {
22246 struct objfile *objfile = cu->per_objfile->objfile;
22247 /* A missing DW_AT_type represents a void type. */
22248 return objfile_type (objfile)->builtin_void;
22249 }
22250
22251 return lookup_die_type (die, type_attr, cu);
22252 }
22253
22254 /* True iff CU's producer generates GNAT Ada auxiliary information
22255 that allows to find parallel types through that information instead
22256 of having to do expensive parallel lookups by type name. */
22257
22258 static int
22259 need_gnat_info (struct dwarf2_cu *cu)
22260 {
22261 /* Assume that the Ada compiler was GNAT, which always produces
22262 the auxiliary information. */
22263 return (cu->per_cu->lang == language_ada);
22264 }
22265
22266 /* Return the auxiliary type of the die in question using its
22267 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22268 attribute is not present. */
22269
22270 static struct type *
22271 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22272 {
22273 struct attribute *type_attr;
22274
22275 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22276 if (!type_attr)
22277 return NULL;
22278
22279 return lookup_die_type (die, type_attr, cu);
22280 }
22281
22282 /* If DIE has a descriptive_type attribute, then set the TYPE's
22283 descriptive type accordingly. */
22284
22285 static void
22286 set_descriptive_type (struct type *type, struct die_info *die,
22287 struct dwarf2_cu *cu)
22288 {
22289 struct type *descriptive_type = die_descriptive_type (die, cu);
22290
22291 if (descriptive_type)
22292 {
22293 ALLOCATE_GNAT_AUX_TYPE (type);
22294 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22295 }
22296 }
22297
22298 /* Return the containing type of the die in question using its
22299 DW_AT_containing_type attribute. */
22300
22301 static struct type *
22302 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22303 {
22304 struct attribute *type_attr;
22305 struct objfile *objfile = cu->per_objfile->objfile;
22306
22307 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22308 if (!type_attr)
22309 error (_("Dwarf Error: Problem turning containing type into gdb type "
22310 "[in module %s]"), objfile_name (objfile));
22311
22312 return lookup_die_type (die, type_attr, cu);
22313 }
22314
22315 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22316
22317 static struct type *
22318 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22319 {
22320 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22321 struct objfile *objfile = per_objfile->objfile;
22322 char *saved;
22323
22324 std::string message
22325 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22326 objfile_name (objfile),
22327 sect_offset_str (cu->header.sect_off),
22328 sect_offset_str (die->sect_off));
22329 saved = obstack_strdup (&objfile->objfile_obstack, message);
22330
22331 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22332 }
22333
22334 /* Look up the type of DIE in CU using its type attribute ATTR.
22335 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22336 DW_AT_containing_type.
22337 If there is no type substitute an error marker. */
22338
22339 static struct type *
22340 lookup_die_type (struct die_info *die, const struct attribute *attr,
22341 struct dwarf2_cu *cu)
22342 {
22343 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22344 struct objfile *objfile = per_objfile->objfile;
22345 struct type *this_type;
22346
22347 gdb_assert (attr->name == DW_AT_type
22348 || attr->name == DW_AT_GNAT_descriptive_type
22349 || attr->name == DW_AT_containing_type);
22350
22351 /* First see if we have it cached. */
22352
22353 if (attr->form == DW_FORM_GNU_ref_alt)
22354 {
22355 struct dwarf2_per_cu_data *per_cu;
22356 sect_offset sect_off = attr->get_ref_die_offset ();
22357
22358 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22359 per_objfile->per_bfd);
22360 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22361 }
22362 else if (attr->form_is_ref ())
22363 {
22364 sect_offset sect_off = attr->get_ref_die_offset ();
22365
22366 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22367 }
22368 else if (attr->form == DW_FORM_ref_sig8)
22369 {
22370 ULONGEST signature = attr->as_signature ();
22371
22372 return get_signatured_type (die, signature, cu);
22373 }
22374 else
22375 {
22376 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22377 " at %s [in module %s]"),
22378 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22379 objfile_name (objfile));
22380 return build_error_marker_type (cu, die);
22381 }
22382
22383 /* If not cached we need to read it in. */
22384
22385 if (this_type == NULL)
22386 {
22387 struct die_info *type_die = NULL;
22388 struct dwarf2_cu *type_cu = cu;
22389
22390 if (attr->form_is_ref ())
22391 type_die = follow_die_ref (die, attr, &type_cu);
22392 if (type_die == NULL)
22393 return build_error_marker_type (cu, die);
22394 /* If we find the type now, it's probably because the type came
22395 from an inter-CU reference and the type's CU got expanded before
22396 ours. */
22397 this_type = read_type_die (type_die, type_cu);
22398 }
22399
22400 /* If we still don't have a type use an error marker. */
22401
22402 if (this_type == NULL)
22403 return build_error_marker_type (cu, die);
22404
22405 return this_type;
22406 }
22407
22408 /* Return the type in DIE, CU.
22409 Returns NULL for invalid types.
22410
22411 This first does a lookup in die_type_hash,
22412 and only reads the die in if necessary.
22413
22414 NOTE: This can be called when reading in partial or full symbols. */
22415
22416 static struct type *
22417 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22418 {
22419 struct type *this_type;
22420
22421 this_type = get_die_type (die, cu);
22422 if (this_type)
22423 return this_type;
22424
22425 return read_type_die_1 (die, cu);
22426 }
22427
22428 /* Read the type in DIE, CU.
22429 Returns NULL for invalid types. */
22430
22431 static struct type *
22432 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22433 {
22434 struct type *this_type = NULL;
22435
22436 switch (die->tag)
22437 {
22438 case DW_TAG_class_type:
22439 case DW_TAG_interface_type:
22440 case DW_TAG_structure_type:
22441 case DW_TAG_union_type:
22442 this_type = read_structure_type (die, cu);
22443 break;
22444 case DW_TAG_enumeration_type:
22445 this_type = read_enumeration_type (die, cu);
22446 break;
22447 case DW_TAG_subprogram:
22448 case DW_TAG_subroutine_type:
22449 case DW_TAG_inlined_subroutine:
22450 this_type = read_subroutine_type (die, cu);
22451 break;
22452 case DW_TAG_array_type:
22453 this_type = read_array_type (die, cu);
22454 break;
22455 case DW_TAG_set_type:
22456 this_type = read_set_type (die, cu);
22457 break;
22458 case DW_TAG_pointer_type:
22459 this_type = read_tag_pointer_type (die, cu);
22460 break;
22461 case DW_TAG_ptr_to_member_type:
22462 this_type = read_tag_ptr_to_member_type (die, cu);
22463 break;
22464 case DW_TAG_reference_type:
22465 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22466 break;
22467 case DW_TAG_rvalue_reference_type:
22468 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22469 break;
22470 case DW_TAG_const_type:
22471 this_type = read_tag_const_type (die, cu);
22472 break;
22473 case DW_TAG_volatile_type:
22474 this_type = read_tag_volatile_type (die, cu);
22475 break;
22476 case DW_TAG_restrict_type:
22477 this_type = read_tag_restrict_type (die, cu);
22478 break;
22479 case DW_TAG_string_type:
22480 this_type = read_tag_string_type (die, cu);
22481 break;
22482 case DW_TAG_typedef:
22483 this_type = read_typedef (die, cu);
22484 break;
22485 case DW_TAG_subrange_type:
22486 this_type = read_subrange_type (die, cu);
22487 break;
22488 case DW_TAG_base_type:
22489 this_type = read_base_type (die, cu);
22490 break;
22491 case DW_TAG_unspecified_type:
22492 this_type = read_unspecified_type (die, cu);
22493 break;
22494 case DW_TAG_namespace:
22495 this_type = read_namespace_type (die, cu);
22496 break;
22497 case DW_TAG_module:
22498 this_type = read_module_type (die, cu);
22499 break;
22500 case DW_TAG_atomic_type:
22501 this_type = read_tag_atomic_type (die, cu);
22502 break;
22503 default:
22504 complaint (_("unexpected tag in read_type_die: '%s'"),
22505 dwarf_tag_name (die->tag));
22506 break;
22507 }
22508
22509 return this_type;
22510 }
22511
22512 /* See if we can figure out if the class lives in a namespace. We do
22513 this by looking for a member function; its demangled name will
22514 contain namespace info, if there is any.
22515 Return the computed name or NULL.
22516 Space for the result is allocated on the objfile's obstack.
22517 This is the full-die version of guess_partial_die_structure_name.
22518 In this case we know DIE has no useful parent. */
22519
22520 static const char *
22521 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22522 {
22523 struct die_info *spec_die;
22524 struct dwarf2_cu *spec_cu;
22525 struct die_info *child;
22526 struct objfile *objfile = cu->per_objfile->objfile;
22527
22528 spec_cu = cu;
22529 spec_die = die_specification (die, &spec_cu);
22530 if (spec_die != NULL)
22531 {
22532 die = spec_die;
22533 cu = spec_cu;
22534 }
22535
22536 for (child = die->child;
22537 child != NULL;
22538 child = child->sibling)
22539 {
22540 if (child->tag == DW_TAG_subprogram)
22541 {
22542 const char *linkage_name = dw2_linkage_name (child, cu);
22543
22544 if (linkage_name != NULL)
22545 {
22546 gdb::unique_xmalloc_ptr<char> actual_name
22547 (cu->language_defn->class_name_from_physname (linkage_name));
22548 const char *name = NULL;
22549
22550 if (actual_name != NULL)
22551 {
22552 const char *die_name = dwarf2_name (die, cu);
22553
22554 if (die_name != NULL
22555 && strcmp (die_name, actual_name.get ()) != 0)
22556 {
22557 /* Strip off the class name from the full name.
22558 We want the prefix. */
22559 int die_name_len = strlen (die_name);
22560 int actual_name_len = strlen (actual_name.get ());
22561 const char *ptr = actual_name.get ();
22562
22563 /* Test for '::' as a sanity check. */
22564 if (actual_name_len > die_name_len + 2
22565 && ptr[actual_name_len - die_name_len - 1] == ':')
22566 name = obstack_strndup (
22567 &objfile->per_bfd->storage_obstack,
22568 ptr, actual_name_len - die_name_len - 2);
22569 }
22570 }
22571 return name;
22572 }
22573 }
22574 }
22575
22576 return NULL;
22577 }
22578
22579 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22580 prefix part in such case. See
22581 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22582
22583 static const char *
22584 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22585 {
22586 struct attribute *attr;
22587 const char *base;
22588
22589 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22590 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22591 return NULL;
22592
22593 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22594 return NULL;
22595
22596 attr = dw2_linkage_name_attr (die, cu);
22597 const char *attr_name = attr->as_string ();
22598 if (attr == NULL || attr_name == NULL)
22599 return NULL;
22600
22601 /* dwarf2_name had to be already called. */
22602 gdb_assert (attr->canonical_string_p ());
22603
22604 /* Strip the base name, keep any leading namespaces/classes. */
22605 base = strrchr (attr_name, ':');
22606 if (base == NULL || base == attr_name || base[-1] != ':')
22607 return "";
22608
22609 struct objfile *objfile = cu->per_objfile->objfile;
22610 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22611 attr_name,
22612 &base[-1] - attr_name);
22613 }
22614
22615 /* Return the name of the namespace/class that DIE is defined within,
22616 or "" if we can't tell. The caller should not xfree the result.
22617
22618 For example, if we're within the method foo() in the following
22619 code:
22620
22621 namespace N {
22622 class C {
22623 void foo () {
22624 }
22625 };
22626 }
22627
22628 then determine_prefix on foo's die will return "N::C". */
22629
22630 static const char *
22631 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22632 {
22633 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22634 struct die_info *parent, *spec_die;
22635 struct dwarf2_cu *spec_cu;
22636 struct type *parent_type;
22637 const char *retval;
22638
22639 if (cu->per_cu->lang != language_cplus
22640 && cu->per_cu->lang != language_fortran
22641 && cu->per_cu->lang != language_d
22642 && cu->per_cu->lang != language_rust)
22643 return "";
22644
22645 retval = anonymous_struct_prefix (die, cu);
22646 if (retval)
22647 return retval;
22648
22649 /* We have to be careful in the presence of DW_AT_specification.
22650 For example, with GCC 3.4, given the code
22651
22652 namespace N {
22653 void foo() {
22654 // Definition of N::foo.
22655 }
22656 }
22657
22658 then we'll have a tree of DIEs like this:
22659
22660 1: DW_TAG_compile_unit
22661 2: DW_TAG_namespace // N
22662 3: DW_TAG_subprogram // declaration of N::foo
22663 4: DW_TAG_subprogram // definition of N::foo
22664 DW_AT_specification // refers to die #3
22665
22666 Thus, when processing die #4, we have to pretend that we're in
22667 the context of its DW_AT_specification, namely the contex of die
22668 #3. */
22669 spec_cu = cu;
22670 spec_die = die_specification (die, &spec_cu);
22671 if (spec_die == NULL)
22672 parent = die->parent;
22673 else
22674 {
22675 parent = spec_die->parent;
22676 cu = spec_cu;
22677 }
22678
22679 if (parent == NULL)
22680 return "";
22681 else if (parent->building_fullname)
22682 {
22683 const char *name;
22684 const char *parent_name;
22685
22686 /* It has been seen on RealView 2.2 built binaries,
22687 DW_TAG_template_type_param types actually _defined_ as
22688 children of the parent class:
22689
22690 enum E {};
22691 template class <class Enum> Class{};
22692 Class<enum E> class_e;
22693
22694 1: DW_TAG_class_type (Class)
22695 2: DW_TAG_enumeration_type (E)
22696 3: DW_TAG_enumerator (enum1:0)
22697 3: DW_TAG_enumerator (enum2:1)
22698 ...
22699 2: DW_TAG_template_type_param
22700 DW_AT_type DW_FORM_ref_udata (E)
22701
22702 Besides being broken debug info, it can put GDB into an
22703 infinite loop. Consider:
22704
22705 When we're building the full name for Class<E>, we'll start
22706 at Class, and go look over its template type parameters,
22707 finding E. We'll then try to build the full name of E, and
22708 reach here. We're now trying to build the full name of E,
22709 and look over the parent DIE for containing scope. In the
22710 broken case, if we followed the parent DIE of E, we'd again
22711 find Class, and once again go look at its template type
22712 arguments, etc., etc. Simply don't consider such parent die
22713 as source-level parent of this die (it can't be, the language
22714 doesn't allow it), and break the loop here. */
22715 name = dwarf2_name (die, cu);
22716 parent_name = dwarf2_name (parent, cu);
22717 complaint (_("template param type '%s' defined within parent '%s'"),
22718 name ? name : "<unknown>",
22719 parent_name ? parent_name : "<unknown>");
22720 return "";
22721 }
22722 else
22723 switch (parent->tag)
22724 {
22725 case DW_TAG_namespace:
22726 parent_type = read_type_die (parent, cu);
22727 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22728 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22729 Work around this problem here. */
22730 if (cu->per_cu->lang == language_cplus
22731 && strcmp (parent_type->name (), "::") == 0)
22732 return "";
22733 /* We give a name to even anonymous namespaces. */
22734 return parent_type->name ();
22735 case DW_TAG_class_type:
22736 case DW_TAG_interface_type:
22737 case DW_TAG_structure_type:
22738 case DW_TAG_union_type:
22739 case DW_TAG_module:
22740 parent_type = read_type_die (parent, cu);
22741 if (parent_type->name () != NULL)
22742 return parent_type->name ();
22743 else
22744 /* An anonymous structure is only allowed non-static data
22745 members; no typedefs, no member functions, et cetera.
22746 So it does not need a prefix. */
22747 return "";
22748 case DW_TAG_compile_unit:
22749 case DW_TAG_partial_unit:
22750 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22751 if (cu->per_cu->lang == language_cplus
22752 && !per_objfile->per_bfd->types.empty ()
22753 && die->child != NULL
22754 && (die->tag == DW_TAG_class_type
22755 || die->tag == DW_TAG_structure_type
22756 || die->tag == DW_TAG_union_type))
22757 {
22758 const char *name = guess_full_die_structure_name (die, cu);
22759 if (name != NULL)
22760 return name;
22761 }
22762 return "";
22763 case DW_TAG_subprogram:
22764 /* Nested subroutines in Fortran get a prefix with the name
22765 of the parent's subroutine. */
22766 if (cu->per_cu->lang == language_fortran)
22767 {
22768 if ((die->tag == DW_TAG_subprogram)
22769 && (dwarf2_name (parent, cu) != NULL))
22770 return dwarf2_name (parent, cu);
22771 }
22772 return "";
22773 case DW_TAG_enumeration_type:
22774 parent_type = read_type_die (parent, cu);
22775 if (parent_type->is_declared_class ())
22776 {
22777 if (parent_type->name () != NULL)
22778 return parent_type->name ();
22779 return "";
22780 }
22781 /* Fall through. */
22782 default:
22783 return determine_prefix (parent, cu);
22784 }
22785 }
22786
22787 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22788 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22789 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22790 an obconcat, otherwise allocate storage for the result. The CU argument is
22791 used to determine the language and hence, the appropriate separator. */
22792
22793 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22794
22795 static char *
22796 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22797 int physname, struct dwarf2_cu *cu)
22798 {
22799 const char *lead = "";
22800 const char *sep;
22801
22802 if (suffix == NULL || suffix[0] == '\0'
22803 || prefix == NULL || prefix[0] == '\0')
22804 sep = "";
22805 else if (cu->per_cu->lang == language_d)
22806 {
22807 /* For D, the 'main' function could be defined in any module, but it
22808 should never be prefixed. */
22809 if (strcmp (suffix, "D main") == 0)
22810 {
22811 prefix = "";
22812 sep = "";
22813 }
22814 else
22815 sep = ".";
22816 }
22817 else if (cu->per_cu->lang == language_fortran && physname)
22818 {
22819 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22820 DW_AT_MIPS_linkage_name is preferred and used instead. */
22821
22822 lead = "__";
22823 sep = "_MOD_";
22824 }
22825 else
22826 sep = "::";
22827
22828 if (prefix == NULL)
22829 prefix = "";
22830 if (suffix == NULL)
22831 suffix = "";
22832
22833 if (obs == NULL)
22834 {
22835 char *retval
22836 = ((char *)
22837 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22838
22839 strcpy (retval, lead);
22840 strcat (retval, prefix);
22841 strcat (retval, sep);
22842 strcat (retval, suffix);
22843 return retval;
22844 }
22845 else
22846 {
22847 /* We have an obstack. */
22848 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22849 }
22850 }
22851
22852 /* Get name of a die, return NULL if not found. */
22853
22854 static const char *
22855 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22856 struct objfile *objfile)
22857 {
22858 if (name && cu->per_cu->lang == language_cplus)
22859 {
22860 gdb::unique_xmalloc_ptr<char> canon_name
22861 = cp_canonicalize_string (name);
22862
22863 if (canon_name != nullptr)
22864 name = objfile->intern (canon_name.get ());
22865 }
22866
22867 return name;
22868 }
22869
22870 /* Get name of a die, return NULL if not found.
22871 Anonymous namespaces are converted to their magic string. */
22872
22873 static const char *
22874 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22875 {
22876 struct attribute *attr;
22877 struct objfile *objfile = cu->per_objfile->objfile;
22878
22879 attr = dwarf2_attr (die, DW_AT_name, cu);
22880 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22881 if (attr_name == nullptr
22882 && die->tag != DW_TAG_namespace
22883 && die->tag != DW_TAG_class_type
22884 && die->tag != DW_TAG_interface_type
22885 && die->tag != DW_TAG_structure_type
22886 && die->tag != DW_TAG_union_type)
22887 return NULL;
22888
22889 switch (die->tag)
22890 {
22891 case DW_TAG_compile_unit:
22892 case DW_TAG_partial_unit:
22893 /* Compilation units have a DW_AT_name that is a filename, not
22894 a source language identifier. */
22895 case DW_TAG_enumeration_type:
22896 case DW_TAG_enumerator:
22897 /* These tags always have simple identifiers already; no need
22898 to canonicalize them. */
22899 return attr_name;
22900
22901 case DW_TAG_namespace:
22902 if (attr_name != nullptr)
22903 return attr_name;
22904 return CP_ANONYMOUS_NAMESPACE_STR;
22905
22906 case DW_TAG_class_type:
22907 case DW_TAG_interface_type:
22908 case DW_TAG_structure_type:
22909 case DW_TAG_union_type:
22910 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22911 structures or unions. These were of the form "._%d" in GCC 4.1,
22912 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22913 and GCC 4.4. We work around this problem by ignoring these. */
22914 if (attr_name != nullptr
22915 && (startswith (attr_name, "._")
22916 || startswith (attr_name, "<anonymous")))
22917 return NULL;
22918
22919 /* GCC might emit a nameless typedef that has a linkage name. See
22920 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22921 if (!attr || attr_name == NULL)
22922 {
22923 attr = dw2_linkage_name_attr (die, cu);
22924 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22925 if (attr == NULL || attr_name == NULL)
22926 return NULL;
22927
22928 /* Avoid demangling attr_name the second time on a second
22929 call for the same DIE. */
22930 if (!attr->canonical_string_p ())
22931 {
22932 gdb::unique_xmalloc_ptr<char> demangled
22933 (gdb_demangle (attr_name, DMGL_TYPES));
22934 if (demangled == nullptr)
22935 return nullptr;
22936
22937 attr->set_string_canonical (objfile->intern (demangled.get ()));
22938 attr_name = attr->as_string ();
22939 }
22940
22941 /* Strip any leading namespaces/classes, keep only the
22942 base name. DW_AT_name for named DIEs does not
22943 contain the prefixes. */
22944 const char *base = strrchr (attr_name, ':');
22945 if (base && base > attr_name && base[-1] == ':')
22946 return &base[1];
22947 else
22948 return attr_name;
22949 }
22950 break;
22951
22952 default:
22953 break;
22954 }
22955
22956 if (!attr->canonical_string_p ())
22957 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22958 objfile));
22959 return attr->as_string ();
22960 }
22961
22962 /* Return the die that this die in an extension of, or NULL if there
22963 is none. *EXT_CU is the CU containing DIE on input, and the CU
22964 containing the return value on output. */
22965
22966 static struct die_info *
22967 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22968 {
22969 struct attribute *attr;
22970
22971 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22972 if (attr == NULL)
22973 return NULL;
22974
22975 return follow_die_ref (die, attr, ext_cu);
22976 }
22977
22978 static void
22979 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22980 {
22981 unsigned int i;
22982
22983 print_spaces (indent, f);
22984 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22985 dwarf_tag_name (die->tag), die->abbrev,
22986 sect_offset_str (die->sect_off));
22987
22988 if (die->parent != NULL)
22989 {
22990 print_spaces (indent, f);
22991 fprintf_unfiltered (f, " parent at offset: %s\n",
22992 sect_offset_str (die->parent->sect_off));
22993 }
22994
22995 print_spaces (indent, f);
22996 fprintf_unfiltered (f, " has children: %s\n",
22997 dwarf_bool_name (die->child != NULL));
22998
22999 print_spaces (indent, f);
23000 fprintf_unfiltered (f, " attributes:\n");
23001
23002 for (i = 0; i < die->num_attrs; ++i)
23003 {
23004 print_spaces (indent, f);
23005 fprintf_unfiltered (f, " %s (%s) ",
23006 dwarf_attr_name (die->attrs[i].name),
23007 dwarf_form_name (die->attrs[i].form));
23008
23009 switch (die->attrs[i].form)
23010 {
23011 case DW_FORM_addr:
23012 case DW_FORM_addrx:
23013 case DW_FORM_GNU_addr_index:
23014 fprintf_unfiltered (f, "address: ");
23015 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23016 break;
23017 case DW_FORM_block2:
23018 case DW_FORM_block4:
23019 case DW_FORM_block:
23020 case DW_FORM_block1:
23021 fprintf_unfiltered (f, "block: size %s",
23022 pulongest (die->attrs[i].as_block ()->size));
23023 break;
23024 case DW_FORM_exprloc:
23025 fprintf_unfiltered (f, "expression: size %s",
23026 pulongest (die->attrs[i].as_block ()->size));
23027 break;
23028 case DW_FORM_data16:
23029 fprintf_unfiltered (f, "constant of 16 bytes");
23030 break;
23031 case DW_FORM_ref_addr:
23032 fprintf_unfiltered (f, "ref address: ");
23033 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23034 break;
23035 case DW_FORM_GNU_ref_alt:
23036 fprintf_unfiltered (f, "alt ref address: ");
23037 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23038 break;
23039 case DW_FORM_ref1:
23040 case DW_FORM_ref2:
23041 case DW_FORM_ref4:
23042 case DW_FORM_ref8:
23043 case DW_FORM_ref_udata:
23044 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23045 (long) (die->attrs[i].as_unsigned ()));
23046 break;
23047 case DW_FORM_data1:
23048 case DW_FORM_data2:
23049 case DW_FORM_data4:
23050 case DW_FORM_data8:
23051 case DW_FORM_udata:
23052 fprintf_unfiltered (f, "constant: %s",
23053 pulongest (die->attrs[i].as_unsigned ()));
23054 break;
23055 case DW_FORM_sec_offset:
23056 fprintf_unfiltered (f, "section offset: %s",
23057 pulongest (die->attrs[i].as_unsigned ()));
23058 break;
23059 case DW_FORM_ref_sig8:
23060 fprintf_unfiltered (f, "signature: %s",
23061 hex_string (die->attrs[i].as_signature ()));
23062 break;
23063 case DW_FORM_string:
23064 case DW_FORM_strp:
23065 case DW_FORM_line_strp:
23066 case DW_FORM_strx:
23067 case DW_FORM_GNU_str_index:
23068 case DW_FORM_GNU_strp_alt:
23069 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23070 die->attrs[i].as_string ()
23071 ? die->attrs[i].as_string () : "",
23072 die->attrs[i].canonical_string_p () ? "is" : "not");
23073 break;
23074 case DW_FORM_flag:
23075 if (die->attrs[i].as_boolean ())
23076 fprintf_unfiltered (f, "flag: TRUE");
23077 else
23078 fprintf_unfiltered (f, "flag: FALSE");
23079 break;
23080 case DW_FORM_flag_present:
23081 fprintf_unfiltered (f, "flag: TRUE");
23082 break;
23083 case DW_FORM_indirect:
23084 /* The reader will have reduced the indirect form to
23085 the "base form" so this form should not occur. */
23086 fprintf_unfiltered (f,
23087 "unexpected attribute form: DW_FORM_indirect");
23088 break;
23089 case DW_FORM_sdata:
23090 case DW_FORM_implicit_const:
23091 fprintf_unfiltered (f, "constant: %s",
23092 plongest (die->attrs[i].as_signed ()));
23093 break;
23094 default:
23095 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23096 die->attrs[i].form);
23097 break;
23098 }
23099 fprintf_unfiltered (f, "\n");
23100 }
23101 }
23102
23103 static void
23104 dump_die_for_error (struct die_info *die)
23105 {
23106 dump_die_shallow (gdb_stderr, 0, die);
23107 }
23108
23109 static void
23110 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23111 {
23112 int indent = level * 4;
23113
23114 gdb_assert (die != NULL);
23115
23116 if (level >= max_level)
23117 return;
23118
23119 dump_die_shallow (f, indent, die);
23120
23121 if (die->child != NULL)
23122 {
23123 print_spaces (indent, f);
23124 fprintf_unfiltered (f, " Children:");
23125 if (level + 1 < max_level)
23126 {
23127 fprintf_unfiltered (f, "\n");
23128 dump_die_1 (f, level + 1, max_level, die->child);
23129 }
23130 else
23131 {
23132 fprintf_unfiltered (f,
23133 " [not printed, max nesting level reached]\n");
23134 }
23135 }
23136
23137 if (die->sibling != NULL && level > 0)
23138 {
23139 dump_die_1 (f, level, max_level, die->sibling);
23140 }
23141 }
23142
23143 /* This is called from the pdie macro in gdbinit.in.
23144 It's not static so gcc will keep a copy callable from gdb. */
23145
23146 void
23147 dump_die (struct die_info *die, int max_level)
23148 {
23149 dump_die_1 (gdb_stdlog, 0, max_level, die);
23150 }
23151
23152 static void
23153 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23154 {
23155 void **slot;
23156
23157 slot = htab_find_slot_with_hash (cu->die_hash, die,
23158 to_underlying (die->sect_off),
23159 INSERT);
23160
23161 *slot = die;
23162 }
23163
23164 /* Follow reference or signature attribute ATTR of SRC_DIE.
23165 On entry *REF_CU is the CU of SRC_DIE.
23166 On exit *REF_CU is the CU of the result. */
23167
23168 static struct die_info *
23169 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23170 struct dwarf2_cu **ref_cu)
23171 {
23172 struct die_info *die;
23173
23174 if (attr->form_is_ref ())
23175 die = follow_die_ref (src_die, attr, ref_cu);
23176 else if (attr->form == DW_FORM_ref_sig8)
23177 die = follow_die_sig (src_die, attr, ref_cu);
23178 else
23179 {
23180 dump_die_for_error (src_die);
23181 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23182 objfile_name ((*ref_cu)->per_objfile->objfile));
23183 }
23184
23185 return die;
23186 }
23187
23188 /* Follow reference OFFSET.
23189 On entry *REF_CU is the CU of the source die referencing OFFSET.
23190 On exit *REF_CU is the CU of the result.
23191 Returns NULL if OFFSET is invalid. */
23192
23193 static struct die_info *
23194 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23195 struct dwarf2_cu **ref_cu)
23196 {
23197 struct die_info temp_die;
23198 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23199 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23200
23201 gdb_assert (cu->per_cu != NULL);
23202
23203 target_cu = cu;
23204
23205 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23206 "source CU contains target offset: %d",
23207 sect_offset_str (cu->per_cu->sect_off),
23208 sect_offset_str (sect_off),
23209 cu->header.offset_in_cu_p (sect_off));
23210
23211 if (cu->per_cu->is_debug_types)
23212 {
23213 /* .debug_types CUs cannot reference anything outside their CU.
23214 If they need to, they have to reference a signatured type via
23215 DW_FORM_ref_sig8. */
23216 if (!cu->header.offset_in_cu_p (sect_off))
23217 return NULL;
23218 }
23219 else if (offset_in_dwz != cu->per_cu->is_dwz
23220 || !cu->header.offset_in_cu_p (sect_off))
23221 {
23222 struct dwarf2_per_cu_data *per_cu;
23223
23224 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23225 per_objfile->per_bfd);
23226
23227 dwarf_read_debug_printf_v ("target CU offset: %s, "
23228 "target CU DIEs loaded: %d",
23229 sect_offset_str (per_cu->sect_off),
23230 per_objfile->get_cu (per_cu) != nullptr);
23231
23232 /* If necessary, add it to the queue and load its DIEs.
23233
23234 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23235 it doesn't mean they are currently loaded. Since we require them
23236 to be loaded, we must check for ourselves. */
23237 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
23238 || per_objfile->get_cu (per_cu) == nullptr)
23239 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23240 false, cu->per_cu->lang);
23241
23242 target_cu = per_objfile->get_cu (per_cu);
23243 gdb_assert (target_cu != nullptr);
23244 }
23245 else if (cu->dies == NULL)
23246 {
23247 /* We're loading full DIEs during partial symbol reading. */
23248 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23249 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23250 language_minimal);
23251 }
23252
23253 *ref_cu = target_cu;
23254 temp_die.sect_off = sect_off;
23255
23256 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23257 &temp_die,
23258 to_underlying (sect_off));
23259 }
23260
23261 /* Follow reference attribute ATTR of SRC_DIE.
23262 On entry *REF_CU is the CU of SRC_DIE.
23263 On exit *REF_CU is the CU of the result. */
23264
23265 static struct die_info *
23266 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23267 struct dwarf2_cu **ref_cu)
23268 {
23269 sect_offset sect_off = attr->get_ref_die_offset ();
23270 struct dwarf2_cu *cu = *ref_cu;
23271 struct die_info *die;
23272
23273 die = follow_die_offset (sect_off,
23274 (attr->form == DW_FORM_GNU_ref_alt
23275 || cu->per_cu->is_dwz),
23276 ref_cu);
23277 if (!die)
23278 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23279 "at %s [in module %s]"),
23280 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23281 objfile_name (cu->per_objfile->objfile));
23282
23283 return die;
23284 }
23285
23286 /* See read.h. */
23287
23288 struct dwarf2_locexpr_baton
23289 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23290 dwarf2_per_cu_data *per_cu,
23291 dwarf2_per_objfile *per_objfile,
23292 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23293 bool resolve_abstract_p)
23294 {
23295 struct die_info *die;
23296 struct attribute *attr;
23297 struct dwarf2_locexpr_baton retval;
23298 struct objfile *objfile = per_objfile->objfile;
23299
23300 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23301 if (cu == nullptr)
23302 cu = load_cu (per_cu, per_objfile, false);
23303
23304 if (cu == nullptr)
23305 {
23306 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23307 Instead just throw an error, not much else we can do. */
23308 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23309 sect_offset_str (sect_off), objfile_name (objfile));
23310 }
23311
23312 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23313 if (!die)
23314 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23315 sect_offset_str (sect_off), objfile_name (objfile));
23316
23317 attr = dwarf2_attr (die, DW_AT_location, cu);
23318 if (!attr && resolve_abstract_p
23319 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23320 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23321 {
23322 CORE_ADDR pc = get_frame_pc ();
23323 CORE_ADDR baseaddr = objfile->text_section_offset ();
23324 struct gdbarch *gdbarch = objfile->arch ();
23325
23326 for (const auto &cand_off
23327 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23328 {
23329 struct dwarf2_cu *cand_cu = cu;
23330 struct die_info *cand
23331 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23332 if (!cand
23333 || !cand->parent
23334 || cand->parent->tag != DW_TAG_subprogram)
23335 continue;
23336
23337 CORE_ADDR pc_low, pc_high;
23338 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23339 if (pc_low == ((CORE_ADDR) -1))
23340 continue;
23341 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23342 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23343 if (!(pc_low <= pc && pc < pc_high))
23344 continue;
23345
23346 die = cand;
23347 attr = dwarf2_attr (die, DW_AT_location, cu);
23348 break;
23349 }
23350 }
23351
23352 if (!attr)
23353 {
23354 /* DWARF: "If there is no such attribute, then there is no effect.".
23355 DATA is ignored if SIZE is 0. */
23356
23357 retval.data = NULL;
23358 retval.size = 0;
23359 }
23360 else if (attr->form_is_section_offset ())
23361 {
23362 struct dwarf2_loclist_baton loclist_baton;
23363 CORE_ADDR pc = get_frame_pc ();
23364 size_t size;
23365
23366 fill_in_loclist_baton (cu, &loclist_baton, attr);
23367
23368 retval.data = dwarf2_find_location_expression (&loclist_baton,
23369 &size, pc);
23370 retval.size = size;
23371 }
23372 else
23373 {
23374 if (!attr->form_is_block ())
23375 error (_("Dwarf Error: DIE at %s referenced in module %s "
23376 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23377 sect_offset_str (sect_off), objfile_name (objfile));
23378
23379 struct dwarf_block *block = attr->as_block ();
23380 retval.data = block->data;
23381 retval.size = block->size;
23382 }
23383 retval.per_objfile = per_objfile;
23384 retval.per_cu = cu->per_cu;
23385
23386 per_objfile->age_comp_units ();
23387
23388 return retval;
23389 }
23390
23391 /* See read.h. */
23392
23393 struct dwarf2_locexpr_baton
23394 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23395 dwarf2_per_cu_data *per_cu,
23396 dwarf2_per_objfile *per_objfile,
23397 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23398 {
23399 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23400
23401 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23402 get_frame_pc);
23403 }
23404
23405 /* Write a constant of a given type as target-ordered bytes into
23406 OBSTACK. */
23407
23408 static const gdb_byte *
23409 write_constant_as_bytes (struct obstack *obstack,
23410 enum bfd_endian byte_order,
23411 struct type *type,
23412 ULONGEST value,
23413 LONGEST *len)
23414 {
23415 gdb_byte *result;
23416
23417 *len = TYPE_LENGTH (type);
23418 result = (gdb_byte *) obstack_alloc (obstack, *len);
23419 store_unsigned_integer (result, *len, byte_order, value);
23420
23421 return result;
23422 }
23423
23424 /* See read.h. */
23425
23426 const gdb_byte *
23427 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23428 dwarf2_per_cu_data *per_cu,
23429 dwarf2_per_objfile *per_objfile,
23430 obstack *obstack,
23431 LONGEST *len)
23432 {
23433 struct die_info *die;
23434 struct attribute *attr;
23435 const gdb_byte *result = NULL;
23436 struct type *type;
23437 LONGEST value;
23438 enum bfd_endian byte_order;
23439 struct objfile *objfile = per_objfile->objfile;
23440
23441 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23442 if (cu == nullptr)
23443 cu = load_cu (per_cu, per_objfile, false);
23444
23445 if (cu == nullptr)
23446 {
23447 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23448 Instead just throw an error, not much else we can do. */
23449 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23450 sect_offset_str (sect_off), objfile_name (objfile));
23451 }
23452
23453 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23454 if (!die)
23455 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23456 sect_offset_str (sect_off), objfile_name (objfile));
23457
23458 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23459 if (attr == NULL)
23460 return NULL;
23461
23462 byte_order = (bfd_big_endian (objfile->obfd)
23463 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23464
23465 switch (attr->form)
23466 {
23467 case DW_FORM_addr:
23468 case DW_FORM_addrx:
23469 case DW_FORM_GNU_addr_index:
23470 {
23471 gdb_byte *tem;
23472
23473 *len = cu->header.addr_size;
23474 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23475 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23476 result = tem;
23477 }
23478 break;
23479 case DW_FORM_string:
23480 case DW_FORM_strp:
23481 case DW_FORM_strx:
23482 case DW_FORM_GNU_str_index:
23483 case DW_FORM_GNU_strp_alt:
23484 /* The string is already allocated on the objfile obstack, point
23485 directly to it. */
23486 {
23487 const char *attr_name = attr->as_string ();
23488 result = (const gdb_byte *) attr_name;
23489 *len = strlen (attr_name);
23490 }
23491 break;
23492 case DW_FORM_block1:
23493 case DW_FORM_block2:
23494 case DW_FORM_block4:
23495 case DW_FORM_block:
23496 case DW_FORM_exprloc:
23497 case DW_FORM_data16:
23498 {
23499 struct dwarf_block *block = attr->as_block ();
23500 result = block->data;
23501 *len = block->size;
23502 }
23503 break;
23504
23505 /* The DW_AT_const_value attributes are supposed to carry the
23506 symbol's value "represented as it would be on the target
23507 architecture." By the time we get here, it's already been
23508 converted to host endianness, so we just need to sign- or
23509 zero-extend it as appropriate. */
23510 case DW_FORM_data1:
23511 type = die_type (die, cu);
23512 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23513 if (result == NULL)
23514 result = write_constant_as_bytes (obstack, byte_order,
23515 type, value, len);
23516 break;
23517 case DW_FORM_data2:
23518 type = die_type (die, cu);
23519 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23520 if (result == NULL)
23521 result = write_constant_as_bytes (obstack, byte_order,
23522 type, value, len);
23523 break;
23524 case DW_FORM_data4:
23525 type = die_type (die, cu);
23526 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23527 if (result == NULL)
23528 result = write_constant_as_bytes (obstack, byte_order,
23529 type, value, len);
23530 break;
23531 case DW_FORM_data8:
23532 type = die_type (die, cu);
23533 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23534 if (result == NULL)
23535 result = write_constant_as_bytes (obstack, byte_order,
23536 type, value, len);
23537 break;
23538
23539 case DW_FORM_sdata:
23540 case DW_FORM_implicit_const:
23541 type = die_type (die, cu);
23542 result = write_constant_as_bytes (obstack, byte_order,
23543 type, attr->as_signed (), len);
23544 break;
23545
23546 case DW_FORM_udata:
23547 type = die_type (die, cu);
23548 result = write_constant_as_bytes (obstack, byte_order,
23549 type, attr->as_unsigned (), len);
23550 break;
23551
23552 default:
23553 complaint (_("unsupported const value attribute form: '%s'"),
23554 dwarf_form_name (attr->form));
23555 break;
23556 }
23557
23558 return result;
23559 }
23560
23561 /* See read.h. */
23562
23563 struct type *
23564 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23565 dwarf2_per_cu_data *per_cu,
23566 dwarf2_per_objfile *per_objfile,
23567 const char **var_name)
23568 {
23569 struct die_info *die;
23570
23571 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23572 if (cu == nullptr)
23573 cu = load_cu (per_cu, per_objfile, false);
23574
23575 if (cu == nullptr)
23576 return nullptr;
23577
23578 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23579 if (!die)
23580 return NULL;
23581
23582 if (var_name != nullptr)
23583 *var_name = var_decl_name (die, cu);
23584 return die_type (die, cu);
23585 }
23586
23587 /* See read.h. */
23588
23589 struct type *
23590 dwarf2_get_die_type (cu_offset die_offset,
23591 dwarf2_per_cu_data *per_cu,
23592 dwarf2_per_objfile *per_objfile)
23593 {
23594 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23595 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23596 }
23597
23598 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23599 On entry *REF_CU is the CU of SRC_DIE.
23600 On exit *REF_CU is the CU of the result.
23601 Returns NULL if the referenced DIE isn't found. */
23602
23603 static struct die_info *
23604 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23605 struct dwarf2_cu **ref_cu)
23606 {
23607 struct die_info temp_die;
23608 struct dwarf2_cu *sig_cu;
23609 struct die_info *die;
23610 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23611
23612
23613 /* While it might be nice to assert sig_type->type == NULL here,
23614 we can get here for DW_AT_imported_declaration where we need
23615 the DIE not the type. */
23616
23617 /* If necessary, add it to the queue and load its DIEs.
23618
23619 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23620 it doesn't mean they are currently loaded. Since we require them
23621 to be loaded, we must check for ourselves. */
23622 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23623 language_minimal)
23624 || per_objfile->get_cu (sig_type) == nullptr)
23625 read_signatured_type (sig_type, per_objfile);
23626
23627 sig_cu = per_objfile->get_cu (sig_type);
23628 gdb_assert (sig_cu != NULL);
23629 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23630 temp_die.sect_off = sig_type->type_offset_in_section;
23631 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23632 to_underlying (temp_die.sect_off));
23633 if (die)
23634 {
23635 /* For .gdb_index version 7 keep track of included TUs.
23636 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23637 if (per_objfile->per_bfd->index_table != NULL
23638 && per_objfile->per_bfd->index_table->version <= 7)
23639 {
23640 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23641 }
23642
23643 *ref_cu = sig_cu;
23644 return die;
23645 }
23646
23647 return NULL;
23648 }
23649
23650 /* Follow signatured type referenced by ATTR in SRC_DIE.
23651 On entry *REF_CU is the CU of SRC_DIE.
23652 On exit *REF_CU is the CU of the result.
23653 The result is the DIE of the type.
23654 If the referenced type cannot be found an error is thrown. */
23655
23656 static struct die_info *
23657 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23658 struct dwarf2_cu **ref_cu)
23659 {
23660 ULONGEST signature = attr->as_signature ();
23661 struct signatured_type *sig_type;
23662 struct die_info *die;
23663
23664 gdb_assert (attr->form == DW_FORM_ref_sig8);
23665
23666 sig_type = lookup_signatured_type (*ref_cu, signature);
23667 /* sig_type will be NULL if the signatured type is missing from
23668 the debug info. */
23669 if (sig_type == NULL)
23670 {
23671 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23672 " from DIE at %s [in module %s]"),
23673 hex_string (signature), sect_offset_str (src_die->sect_off),
23674 objfile_name ((*ref_cu)->per_objfile->objfile));
23675 }
23676
23677 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23678 if (die == NULL)
23679 {
23680 dump_die_for_error (src_die);
23681 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23682 " from DIE at %s [in module %s]"),
23683 hex_string (signature), sect_offset_str (src_die->sect_off),
23684 objfile_name ((*ref_cu)->per_objfile->objfile));
23685 }
23686
23687 return die;
23688 }
23689
23690 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23691 reading in and processing the type unit if necessary. */
23692
23693 static struct type *
23694 get_signatured_type (struct die_info *die, ULONGEST signature,
23695 struct dwarf2_cu *cu)
23696 {
23697 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23698 struct signatured_type *sig_type;
23699 struct dwarf2_cu *type_cu;
23700 struct die_info *type_die;
23701 struct type *type;
23702
23703 sig_type = lookup_signatured_type (cu, signature);
23704 /* sig_type will be NULL if the signatured type is missing from
23705 the debug info. */
23706 if (sig_type == NULL)
23707 {
23708 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23709 " from DIE at %s [in module %s]"),
23710 hex_string (signature), sect_offset_str (die->sect_off),
23711 objfile_name (per_objfile->objfile));
23712 return build_error_marker_type (cu, die);
23713 }
23714
23715 /* If we already know the type we're done. */
23716 type = per_objfile->get_type_for_signatured_type (sig_type);
23717 if (type != nullptr)
23718 return type;
23719
23720 type_cu = cu;
23721 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23722 if (type_die != NULL)
23723 {
23724 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23725 is created. This is important, for example, because for c++ classes
23726 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23727 type = read_type_die (type_die, type_cu);
23728 if (type == NULL)
23729 {
23730 complaint (_("Dwarf Error: Cannot build signatured type %s"
23731 " referenced from DIE at %s [in module %s]"),
23732 hex_string (signature), sect_offset_str (die->sect_off),
23733 objfile_name (per_objfile->objfile));
23734 type = build_error_marker_type (cu, die);
23735 }
23736 }
23737 else
23738 {
23739 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23740 " from DIE at %s [in module %s]"),
23741 hex_string (signature), sect_offset_str (die->sect_off),
23742 objfile_name (per_objfile->objfile));
23743 type = build_error_marker_type (cu, die);
23744 }
23745
23746 per_objfile->set_type_for_signatured_type (sig_type, type);
23747
23748 return type;
23749 }
23750
23751 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23752 reading in and processing the type unit if necessary. */
23753
23754 static struct type *
23755 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23756 struct dwarf2_cu *cu) /* ARI: editCase function */
23757 {
23758 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23759 if (attr->form_is_ref ())
23760 {
23761 struct dwarf2_cu *type_cu = cu;
23762 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23763
23764 return read_type_die (type_die, type_cu);
23765 }
23766 else if (attr->form == DW_FORM_ref_sig8)
23767 {
23768 return get_signatured_type (die, attr->as_signature (), cu);
23769 }
23770 else
23771 {
23772 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23773
23774 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23775 " at %s [in module %s]"),
23776 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23777 objfile_name (per_objfile->objfile));
23778 return build_error_marker_type (cu, die);
23779 }
23780 }
23781
23782 /* Load the DIEs associated with type unit PER_CU into memory. */
23783
23784 static void
23785 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23786 dwarf2_per_objfile *per_objfile)
23787 {
23788 struct signatured_type *sig_type;
23789
23790 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23791 gdb_assert (! per_cu->type_unit_group_p ());
23792
23793 /* We have the per_cu, but we need the signatured_type.
23794 Fortunately this is an easy translation. */
23795 gdb_assert (per_cu->is_debug_types);
23796 sig_type = (struct signatured_type *) per_cu;
23797
23798 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23799
23800 read_signatured_type (sig_type, per_objfile);
23801
23802 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23803 }
23804
23805 /* Read in a signatured type and build its CU and DIEs.
23806 If the type is a stub for the real type in a DWO file,
23807 read in the real type from the DWO file as well. */
23808
23809 static void
23810 read_signatured_type (signatured_type *sig_type,
23811 dwarf2_per_objfile *per_objfile)
23812 {
23813 gdb_assert (sig_type->is_debug_types);
23814 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23815
23816 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23817
23818 if (!reader.dummy_p)
23819 {
23820 struct dwarf2_cu *cu = reader.cu;
23821 const gdb_byte *info_ptr = reader.info_ptr;
23822
23823 gdb_assert (cu->die_hash == NULL);
23824 cu->die_hash =
23825 htab_create_alloc_ex (cu->header.length / 12,
23826 die_hash,
23827 die_eq,
23828 NULL,
23829 &cu->comp_unit_obstack,
23830 hashtab_obstack_allocate,
23831 dummy_obstack_deallocate);
23832
23833 if (reader.comp_unit_die->has_children)
23834 reader.comp_unit_die->child
23835 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23836 reader.comp_unit_die);
23837 cu->dies = reader.comp_unit_die;
23838 /* comp_unit_die is not stored in die_hash, no need. */
23839
23840 /* We try not to read any attributes in this function, because
23841 not all CUs needed for references have been loaded yet, and
23842 symbol table processing isn't initialized. But we have to
23843 set the CU language, or we won't be able to build types
23844 correctly. Similarly, if we do not read the producer, we can
23845 not apply producer-specific interpretation. */
23846 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23847
23848 reader.keep ();
23849 }
23850
23851 sig_type->tu_read = 1;
23852 }
23853
23854 /* Decode simple location descriptions.
23855 Given a pointer to a dwarf block that defines a location, compute
23856 the location and return the value. If COMPUTED is non-null, it is
23857 set to true to indicate that decoding was successful, and false
23858 otherwise. If COMPUTED is null, then this function may emit a
23859 complaint. */
23860
23861 static CORE_ADDR
23862 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23863 {
23864 struct objfile *objfile = cu->per_objfile->objfile;
23865 size_t i;
23866 size_t size = blk->size;
23867 const gdb_byte *data = blk->data;
23868 CORE_ADDR stack[64];
23869 int stacki;
23870 unsigned int bytes_read, unsnd;
23871 gdb_byte op;
23872
23873 if (computed != nullptr)
23874 *computed = false;
23875
23876 i = 0;
23877 stacki = 0;
23878 stack[stacki] = 0;
23879 stack[++stacki] = 0;
23880
23881 while (i < size)
23882 {
23883 op = data[i++];
23884 switch (op)
23885 {
23886 case DW_OP_lit0:
23887 case DW_OP_lit1:
23888 case DW_OP_lit2:
23889 case DW_OP_lit3:
23890 case DW_OP_lit4:
23891 case DW_OP_lit5:
23892 case DW_OP_lit6:
23893 case DW_OP_lit7:
23894 case DW_OP_lit8:
23895 case DW_OP_lit9:
23896 case DW_OP_lit10:
23897 case DW_OP_lit11:
23898 case DW_OP_lit12:
23899 case DW_OP_lit13:
23900 case DW_OP_lit14:
23901 case DW_OP_lit15:
23902 case DW_OP_lit16:
23903 case DW_OP_lit17:
23904 case DW_OP_lit18:
23905 case DW_OP_lit19:
23906 case DW_OP_lit20:
23907 case DW_OP_lit21:
23908 case DW_OP_lit22:
23909 case DW_OP_lit23:
23910 case DW_OP_lit24:
23911 case DW_OP_lit25:
23912 case DW_OP_lit26:
23913 case DW_OP_lit27:
23914 case DW_OP_lit28:
23915 case DW_OP_lit29:
23916 case DW_OP_lit30:
23917 case DW_OP_lit31:
23918 stack[++stacki] = op - DW_OP_lit0;
23919 break;
23920
23921 case DW_OP_reg0:
23922 case DW_OP_reg1:
23923 case DW_OP_reg2:
23924 case DW_OP_reg3:
23925 case DW_OP_reg4:
23926 case DW_OP_reg5:
23927 case DW_OP_reg6:
23928 case DW_OP_reg7:
23929 case DW_OP_reg8:
23930 case DW_OP_reg9:
23931 case DW_OP_reg10:
23932 case DW_OP_reg11:
23933 case DW_OP_reg12:
23934 case DW_OP_reg13:
23935 case DW_OP_reg14:
23936 case DW_OP_reg15:
23937 case DW_OP_reg16:
23938 case DW_OP_reg17:
23939 case DW_OP_reg18:
23940 case DW_OP_reg19:
23941 case DW_OP_reg20:
23942 case DW_OP_reg21:
23943 case DW_OP_reg22:
23944 case DW_OP_reg23:
23945 case DW_OP_reg24:
23946 case DW_OP_reg25:
23947 case DW_OP_reg26:
23948 case DW_OP_reg27:
23949 case DW_OP_reg28:
23950 case DW_OP_reg29:
23951 case DW_OP_reg30:
23952 case DW_OP_reg31:
23953 stack[++stacki] = op - DW_OP_reg0;
23954 if (i < size)
23955 {
23956 if (computed == nullptr)
23957 dwarf2_complex_location_expr_complaint ();
23958 else
23959 return 0;
23960 }
23961 break;
23962
23963 case DW_OP_regx:
23964 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23965 i += bytes_read;
23966 stack[++stacki] = unsnd;
23967 if (i < size)
23968 {
23969 if (computed == nullptr)
23970 dwarf2_complex_location_expr_complaint ();
23971 else
23972 return 0;
23973 }
23974 break;
23975
23976 case DW_OP_addr:
23977 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23978 &bytes_read);
23979 i += bytes_read;
23980 break;
23981
23982 case DW_OP_const1u:
23983 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23984 i += 1;
23985 break;
23986
23987 case DW_OP_const1s:
23988 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23989 i += 1;
23990 break;
23991
23992 case DW_OP_const2u:
23993 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23994 i += 2;
23995 break;
23996
23997 case DW_OP_const2s:
23998 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23999 i += 2;
24000 break;
24001
24002 case DW_OP_const4u:
24003 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24004 i += 4;
24005 break;
24006
24007 case DW_OP_const4s:
24008 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24009 i += 4;
24010 break;
24011
24012 case DW_OP_const8u:
24013 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24014 i += 8;
24015 break;
24016
24017 case DW_OP_constu:
24018 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24019 &bytes_read);
24020 i += bytes_read;
24021 break;
24022
24023 case DW_OP_consts:
24024 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24025 i += bytes_read;
24026 break;
24027
24028 case DW_OP_dup:
24029 stack[stacki + 1] = stack[stacki];
24030 stacki++;
24031 break;
24032
24033 case DW_OP_plus:
24034 stack[stacki - 1] += stack[stacki];
24035 stacki--;
24036 break;
24037
24038 case DW_OP_plus_uconst:
24039 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24040 &bytes_read);
24041 i += bytes_read;
24042 break;
24043
24044 case DW_OP_minus:
24045 stack[stacki - 1] -= stack[stacki];
24046 stacki--;
24047 break;
24048
24049 case DW_OP_deref:
24050 /* If we're not the last op, then we definitely can't encode
24051 this using GDB's address_class enum. This is valid for partial
24052 global symbols, although the variable's address will be bogus
24053 in the psymtab. */
24054 if (i < size)
24055 {
24056 if (computed == nullptr)
24057 dwarf2_complex_location_expr_complaint ();
24058 else
24059 return 0;
24060 }
24061 break;
24062
24063 case DW_OP_GNU_push_tls_address:
24064 case DW_OP_form_tls_address:
24065 /* The top of the stack has the offset from the beginning
24066 of the thread control block at which the variable is located. */
24067 /* Nothing should follow this operator, so the top of stack would
24068 be returned. */
24069 /* This is valid for partial global symbols, but the variable's
24070 address will be bogus in the psymtab. Make it always at least
24071 non-zero to not look as a variable garbage collected by linker
24072 which have DW_OP_addr 0. */
24073 if (i < size)
24074 {
24075 if (computed == nullptr)
24076 dwarf2_complex_location_expr_complaint ();
24077 else
24078 return 0;
24079 }
24080 stack[stacki]++;
24081 break;
24082
24083 case DW_OP_GNU_uninit:
24084 if (computed != nullptr)
24085 return 0;
24086 break;
24087
24088 case DW_OP_addrx:
24089 case DW_OP_GNU_addr_index:
24090 case DW_OP_GNU_const_index:
24091 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24092 &bytes_read);
24093 i += bytes_read;
24094 break;
24095
24096 default:
24097 if (computed == nullptr)
24098 {
24099 const char *name = get_DW_OP_name (op);
24100
24101 if (name)
24102 complaint (_("unsupported stack op: '%s'"),
24103 name);
24104 else
24105 complaint (_("unsupported stack op: '%02x'"),
24106 op);
24107 }
24108
24109 return (stack[stacki]);
24110 }
24111
24112 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24113 outside of the allocated space. Also enforce minimum>0. */
24114 if (stacki >= ARRAY_SIZE (stack) - 1)
24115 {
24116 if (computed == nullptr)
24117 complaint (_("location description stack overflow"));
24118 return 0;
24119 }
24120
24121 if (stacki <= 0)
24122 {
24123 if (computed == nullptr)
24124 complaint (_("location description stack underflow"));
24125 return 0;
24126 }
24127 }
24128
24129 if (computed != nullptr)
24130 *computed = true;
24131 return (stack[stacki]);
24132 }
24133
24134 /* memory allocation interface */
24135
24136 static struct dwarf_block *
24137 dwarf_alloc_block (struct dwarf2_cu *cu)
24138 {
24139 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24140 }
24141
24142 static struct die_info *
24143 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24144 {
24145 struct die_info *die;
24146 size_t size = sizeof (struct die_info);
24147
24148 if (num_attrs > 1)
24149 size += (num_attrs - 1) * sizeof (struct attribute);
24150
24151 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24152 memset (die, 0, sizeof (struct die_info));
24153 return (die);
24154 }
24155
24156 \f
24157
24158 /* Macro support. */
24159
24160 /* An overload of dwarf_decode_macros that finds the correct section
24161 and ensures it is read in before calling the other overload. */
24162
24163 static void
24164 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24165 int section_is_gnu)
24166 {
24167 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24168 struct objfile *objfile = per_objfile->objfile;
24169 const struct line_header *lh = cu->line_header;
24170 unsigned int offset_size = cu->header.offset_size;
24171 struct dwarf2_section_info *section;
24172 const char *section_name;
24173
24174 if (cu->dwo_unit != nullptr)
24175 {
24176 if (section_is_gnu)
24177 {
24178 section = &cu->dwo_unit->dwo_file->sections.macro;
24179 section_name = ".debug_macro.dwo";
24180 }
24181 else
24182 {
24183 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24184 section_name = ".debug_macinfo.dwo";
24185 }
24186 }
24187 else
24188 {
24189 if (section_is_gnu)
24190 {
24191 section = &per_objfile->per_bfd->macro;
24192 section_name = ".debug_macro";
24193 }
24194 else
24195 {
24196 section = &per_objfile->per_bfd->macinfo;
24197 section_name = ".debug_macinfo";
24198 }
24199 }
24200
24201 section->read (objfile);
24202 if (section->buffer == nullptr)
24203 {
24204 complaint (_("missing %s section"), section_name);
24205 return;
24206 }
24207
24208 buildsym_compunit *builder = cu->get_builder ();
24209
24210 struct dwarf2_section_info *str_offsets_section;
24211 struct dwarf2_section_info *str_section;
24212 gdb::optional<ULONGEST> str_offsets_base;
24213
24214 if (cu->dwo_unit != nullptr)
24215 {
24216 str_offsets_section = &cu->dwo_unit->dwo_file
24217 ->sections.str_offsets;
24218 str_section = &cu->dwo_unit->dwo_file->sections.str;
24219 str_offsets_base = cu->header.addr_size;
24220 }
24221 else
24222 {
24223 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24224 str_section = &per_objfile->per_bfd->str;
24225 str_offsets_base = cu->str_offsets_base;
24226 }
24227
24228 dwarf_decode_macros (per_objfile, builder, section, lh,
24229 offset_size, offset, str_section, str_offsets_section,
24230 str_offsets_base, section_is_gnu);
24231 }
24232
24233 /* Return the .debug_loc section to use for CU.
24234 For DWO files use .debug_loc.dwo. */
24235
24236 static struct dwarf2_section_info *
24237 cu_debug_loc_section (struct dwarf2_cu *cu)
24238 {
24239 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24240
24241 if (cu->dwo_unit)
24242 {
24243 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24244
24245 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24246 }
24247 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24248 : &per_objfile->per_bfd->loc);
24249 }
24250
24251 /* Return the .debug_rnglists section to use for CU. */
24252 static struct dwarf2_section_info *
24253 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24254 {
24255 if (cu->header.version < 5)
24256 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24257 cu->header.version);
24258 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24259
24260 /* Make sure we read the .debug_rnglists section from the file that
24261 contains the DW_AT_ranges attribute we are reading. Normally that
24262 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24263 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24264 program. */
24265 if (cu->dwo_unit != nullptr
24266 && tag != DW_TAG_compile_unit
24267 && tag != DW_TAG_skeleton_unit)
24268 {
24269 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24270
24271 if (sections->rnglists.size > 0)
24272 return &sections->rnglists;
24273 else
24274 error (_(".debug_rnglists section is missing from .dwo file."));
24275 }
24276 return &dwarf2_per_objfile->per_bfd->rnglists;
24277 }
24278
24279 /* A helper function that fills in a dwarf2_loclist_baton. */
24280
24281 static void
24282 fill_in_loclist_baton (struct dwarf2_cu *cu,
24283 struct dwarf2_loclist_baton *baton,
24284 const struct attribute *attr)
24285 {
24286 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24287 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24288
24289 section->read (per_objfile->objfile);
24290
24291 baton->per_objfile = per_objfile;
24292 baton->per_cu = cu->per_cu;
24293 gdb_assert (baton->per_cu);
24294 /* We don't know how long the location list is, but make sure we
24295 don't run off the edge of the section. */
24296 baton->size = section->size - attr->as_unsigned ();
24297 baton->data = section->buffer + attr->as_unsigned ();
24298 if (cu->base_address.has_value ())
24299 baton->base_address = *cu->base_address;
24300 else
24301 baton->base_address = 0;
24302 baton->from_dwo = cu->dwo_unit != NULL;
24303 }
24304
24305 static void
24306 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24307 struct dwarf2_cu *cu, int is_block)
24308 {
24309 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24310 struct objfile *objfile = per_objfile->objfile;
24311 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24312
24313 if (attr->form_is_section_offset ()
24314 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24315 the section. If so, fall through to the complaint in the
24316 other branch. */
24317 && attr->as_unsigned () < section->get_size (objfile))
24318 {
24319 struct dwarf2_loclist_baton *baton;
24320
24321 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24322
24323 fill_in_loclist_baton (cu, baton, attr);
24324
24325 if (!cu->base_address.has_value ())
24326 complaint (_("Location list used without "
24327 "specifying the CU base address."));
24328
24329 SYMBOL_ACLASS_INDEX (sym) = (is_block
24330 ? dwarf2_loclist_block_index
24331 : dwarf2_loclist_index);
24332 SYMBOL_LOCATION_BATON (sym) = baton;
24333 }
24334 else
24335 {
24336 struct dwarf2_locexpr_baton *baton;
24337
24338 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24339 baton->per_objfile = per_objfile;
24340 baton->per_cu = cu->per_cu;
24341 gdb_assert (baton->per_cu);
24342
24343 if (attr->form_is_block ())
24344 {
24345 /* Note that we're just copying the block's data pointer
24346 here, not the actual data. We're still pointing into the
24347 info_buffer for SYM's objfile; right now we never release
24348 that buffer, but when we do clean up properly this may
24349 need to change. */
24350 struct dwarf_block *block = attr->as_block ();
24351 baton->size = block->size;
24352 baton->data = block->data;
24353 }
24354 else
24355 {
24356 dwarf2_invalid_attrib_class_complaint ("location description",
24357 sym->natural_name ());
24358 baton->size = 0;
24359 }
24360
24361 SYMBOL_ACLASS_INDEX (sym) = (is_block
24362 ? dwarf2_locexpr_block_index
24363 : dwarf2_locexpr_index);
24364 SYMBOL_LOCATION_BATON (sym) = baton;
24365 }
24366 }
24367
24368 /* See read.h. */
24369
24370 const comp_unit_head *
24371 dwarf2_per_cu_data::get_header () const
24372 {
24373 if (!m_header_read_in)
24374 {
24375 const gdb_byte *info_ptr
24376 = this->section->buffer + to_underlying (this->sect_off);
24377
24378 memset (&m_header, 0, sizeof (m_header));
24379
24380 read_comp_unit_head (&m_header, info_ptr, this->section,
24381 rcuh_kind::COMPILE);
24382
24383 m_header_read_in = true;
24384 }
24385
24386 return &m_header;
24387 }
24388
24389 /* See read.h. */
24390
24391 int
24392 dwarf2_per_cu_data::addr_size () const
24393 {
24394 return this->get_header ()->addr_size;
24395 }
24396
24397 /* See read.h. */
24398
24399 int
24400 dwarf2_per_cu_data::offset_size () const
24401 {
24402 return this->get_header ()->offset_size;
24403 }
24404
24405 /* See read.h. */
24406
24407 int
24408 dwarf2_per_cu_data::ref_addr_size () const
24409 {
24410 const comp_unit_head *header = this->get_header ();
24411
24412 if (header->version == 2)
24413 return header->addr_size;
24414 else
24415 return header->offset_size;
24416 }
24417
24418 /* A helper function for dwarf2_find_containing_comp_unit that returns
24419 the index of the result, and that searches a vector. It will
24420 return a result even if the offset in question does not actually
24421 occur in any CU. This is separate so that it can be unit
24422 tested. */
24423
24424 static int
24425 dwarf2_find_containing_comp_unit
24426 (sect_offset sect_off,
24427 unsigned int offset_in_dwz,
24428 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
24429 {
24430 int low, high;
24431
24432 low = 0;
24433 high = all_comp_units.size () - 1;
24434 while (high > low)
24435 {
24436 struct dwarf2_per_cu_data *mid_cu;
24437 int mid = low + (high - low) / 2;
24438
24439 mid_cu = all_comp_units[mid].get ();
24440 if (mid_cu->is_dwz > offset_in_dwz
24441 || (mid_cu->is_dwz == offset_in_dwz
24442 && mid_cu->sect_off + mid_cu->length > sect_off))
24443 high = mid;
24444 else
24445 low = mid + 1;
24446 }
24447 gdb_assert (low == high);
24448 return low;
24449 }
24450
24451 /* Locate the .debug_info compilation unit from CU's objfile which contains
24452 the DIE at OFFSET. Raises an error on failure. */
24453
24454 static struct dwarf2_per_cu_data *
24455 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24456 unsigned int offset_in_dwz,
24457 dwarf2_per_bfd *per_bfd)
24458 {
24459 int low = dwarf2_find_containing_comp_unit
24460 (sect_off, offset_in_dwz, per_bfd->all_comp_units);
24461 dwarf2_per_cu_data *this_cu = per_bfd->all_comp_units[low].get ();
24462
24463 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24464 {
24465 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24466 error (_("Dwarf Error: could not find partial DIE containing "
24467 "offset %s [in module %s]"),
24468 sect_offset_str (sect_off),
24469 bfd_get_filename (per_bfd->obfd));
24470
24471 gdb_assert (per_bfd->all_comp_units[low-1]->sect_off
24472 <= sect_off);
24473 return per_bfd->all_comp_units[low - 1].get ();
24474 }
24475 else
24476 {
24477 if (low == per_bfd->all_comp_units.size () - 1
24478 && sect_off >= this_cu->sect_off + this_cu->length)
24479 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24480 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24481 return this_cu;
24482 }
24483 }
24484
24485 #if GDB_SELF_TEST
24486
24487 namespace selftests {
24488 namespace find_containing_comp_unit {
24489
24490 static void
24491 run_test ()
24492 {
24493 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
24494 dwarf2_per_cu_data *one_ptr = one.get ();
24495 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
24496 dwarf2_per_cu_data *two_ptr = two.get ();
24497 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
24498 dwarf2_per_cu_data *three_ptr = three.get ();
24499 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
24500 dwarf2_per_cu_data *four_ptr = four.get ();
24501
24502 one->length = 5;
24503 two->sect_off = sect_offset (one->length);
24504 two->length = 7;
24505
24506 three->length = 5;
24507 three->is_dwz = 1;
24508 four->sect_off = sect_offset (three->length);
24509 four->length = 7;
24510 four->is_dwz = 1;
24511
24512 std::vector<dwarf2_per_cu_data_up> units;
24513 units.push_back (std::move (one));
24514 units.push_back (std::move (two));
24515 units.push_back (std::move (three));
24516 units.push_back (std::move (four));
24517
24518 int result;
24519
24520 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24521 SELF_CHECK (units[result].get () == one_ptr);
24522 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24523 SELF_CHECK (units[result].get () == one_ptr);
24524 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24525 SELF_CHECK (units[result].get () == two_ptr);
24526
24527 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24528 SELF_CHECK (units[result].get () == three_ptr);
24529 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24530 SELF_CHECK (units[result].get () == three_ptr);
24531 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24532 SELF_CHECK (units[result].get () == four_ptr);
24533 }
24534
24535 }
24536 }
24537
24538 #endif /* GDB_SELF_TEST */
24539
24540 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24541
24542 static void
24543 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24544 enum language pretend_language)
24545 {
24546 struct attribute *attr;
24547
24548 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24549
24550 /* Set the language we're debugging. */
24551 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24552 if (cu->producer != nullptr
24553 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
24554 {
24555 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24556 attribute is not standardised yet. As a workaround for the
24557 language detection we fall back to the DW_AT_producer
24558 string. */
24559 cu->per_cu->lang = language_opencl;
24560 }
24561 else if (cu->producer != nullptr
24562 && strstr (cu->producer, "GNU Go ") != NULL)
24563 {
24564 /* Similar hack for Go. */
24565 cu->per_cu->lang = language_go;
24566 }
24567 else if (attr != nullptr)
24568 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
24569 else
24570 cu->per_cu->lang = pretend_language;
24571 cu->language_defn = language_def (cu->per_cu->lang);
24572 }
24573
24574 /* See read.h. */
24575
24576 dwarf2_cu *
24577 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24578 {
24579 auto it = m_dwarf2_cus.find (per_cu);
24580 if (it == m_dwarf2_cus.end ())
24581 return nullptr;
24582
24583 return it->second;
24584 }
24585
24586 /* See read.h. */
24587
24588 void
24589 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24590 {
24591 gdb_assert (this->get_cu (per_cu) == nullptr);
24592
24593 m_dwarf2_cus[per_cu] = cu;
24594 }
24595
24596 /* See read.h. */
24597
24598 void
24599 dwarf2_per_objfile::age_comp_units ()
24600 {
24601 dwarf_read_debug_printf_v ("running");
24602
24603 /* This is not expected to be called in the middle of CU expansion. There is
24604 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24605 loaded in memory. Calling age_comp_units while the queue is in use could
24606 make us free the DIEs for a CU that is in the queue and therefore break
24607 that invariant. */
24608 gdb_assert (!this->per_bfd->queue.has_value ());
24609
24610 /* Start by clearing all marks. */
24611 for (auto pair : m_dwarf2_cus)
24612 pair.second->clear_mark ();
24613
24614 /* Traverse all CUs, mark them and their dependencies if used recently
24615 enough. */
24616 for (auto pair : m_dwarf2_cus)
24617 {
24618 dwarf2_cu *cu = pair.second;
24619
24620 cu->last_used++;
24621 if (cu->last_used <= dwarf_max_cache_age)
24622 cu->mark ();
24623 }
24624
24625 /* Delete all CUs still not marked. */
24626 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24627 {
24628 dwarf2_cu *cu = it->second;
24629
24630 if (!cu->is_marked ())
24631 {
24632 dwarf_read_debug_printf_v ("deleting old CU %s",
24633 sect_offset_str (cu->per_cu->sect_off));
24634 delete cu;
24635 it = m_dwarf2_cus.erase (it);
24636 }
24637 else
24638 it++;
24639 }
24640 }
24641
24642 /* See read.h. */
24643
24644 void
24645 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24646 {
24647 auto it = m_dwarf2_cus.find (per_cu);
24648 if (it == m_dwarf2_cus.end ())
24649 return;
24650
24651 delete it->second;
24652
24653 m_dwarf2_cus.erase (it);
24654 }
24655
24656 dwarf2_per_objfile::~dwarf2_per_objfile ()
24657 {
24658 remove_all_cus ();
24659 }
24660
24661 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24662 We store these in a hash table separate from the DIEs, and preserve them
24663 when the DIEs are flushed out of cache.
24664
24665 The CU "per_cu" pointer is needed because offset alone is not enough to
24666 uniquely identify the type. A file may have multiple .debug_types sections,
24667 or the type may come from a DWO file. Furthermore, while it's more logical
24668 to use per_cu->section+offset, with Fission the section with the data is in
24669 the DWO file but we don't know that section at the point we need it.
24670 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24671 because we can enter the lookup routine, get_die_type_at_offset, from
24672 outside this file, and thus won't necessarily have PER_CU->cu.
24673 Fortunately, PER_CU is stable for the life of the objfile. */
24674
24675 struct dwarf2_per_cu_offset_and_type
24676 {
24677 const struct dwarf2_per_cu_data *per_cu;
24678 sect_offset sect_off;
24679 struct type *type;
24680 };
24681
24682 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24683
24684 static hashval_t
24685 per_cu_offset_and_type_hash (const void *item)
24686 {
24687 const struct dwarf2_per_cu_offset_and_type *ofs
24688 = (const struct dwarf2_per_cu_offset_and_type *) item;
24689
24690 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24691 }
24692
24693 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24694
24695 static int
24696 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24697 {
24698 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24699 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24700 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24701 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24702
24703 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24704 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24705 }
24706
24707 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24708 table if necessary. For convenience, return TYPE.
24709
24710 The DIEs reading must have careful ordering to:
24711 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24712 reading current DIE.
24713 * Not trying to dereference contents of still incompletely read in types
24714 while reading in other DIEs.
24715 * Enable referencing still incompletely read in types just by a pointer to
24716 the type without accessing its fields.
24717
24718 Therefore caller should follow these rules:
24719 * Try to fetch any prerequisite types we may need to build this DIE type
24720 before building the type and calling set_die_type.
24721 * After building type call set_die_type for current DIE as soon as
24722 possible before fetching more types to complete the current type.
24723 * Make the type as complete as possible before fetching more types. */
24724
24725 static struct type *
24726 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24727 bool skip_data_location)
24728 {
24729 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24730 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24731 struct objfile *objfile = per_objfile->objfile;
24732 struct attribute *attr;
24733 struct dynamic_prop prop;
24734
24735 /* For Ada types, make sure that the gnat-specific data is always
24736 initialized (if not already set). There are a few types where
24737 we should not be doing so, because the type-specific area is
24738 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24739 where the type-specific area is used to store the floatformat).
24740 But this is not a problem, because the gnat-specific information
24741 is actually not needed for these types. */
24742 if (need_gnat_info (cu)
24743 && type->code () != TYPE_CODE_FUNC
24744 && type->code () != TYPE_CODE_FLT
24745 && type->code () != TYPE_CODE_METHODPTR
24746 && type->code () != TYPE_CODE_MEMBERPTR
24747 && type->code () != TYPE_CODE_METHOD
24748 && type->code () != TYPE_CODE_FIXED_POINT
24749 && !HAVE_GNAT_AUX_INFO (type))
24750 INIT_GNAT_SPECIFIC (type);
24751
24752 /* Read DW_AT_allocated and set in type. */
24753 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24754 if (attr != NULL)
24755 {
24756 struct type *prop_type = cu->addr_sized_int_type (false);
24757 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24758 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24759 }
24760
24761 /* Read DW_AT_associated and set in type. */
24762 attr = dwarf2_attr (die, DW_AT_associated, cu);
24763 if (attr != NULL)
24764 {
24765 struct type *prop_type = cu->addr_sized_int_type (false);
24766 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24767 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24768 }
24769
24770 /* Read DW_AT_data_location and set in type. */
24771 if (!skip_data_location)
24772 {
24773 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24774 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24775 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24776 }
24777
24778 if (per_objfile->die_type_hash == NULL)
24779 per_objfile->die_type_hash
24780 = htab_up (htab_create_alloc (127,
24781 per_cu_offset_and_type_hash,
24782 per_cu_offset_and_type_eq,
24783 NULL, xcalloc, xfree));
24784
24785 ofs.per_cu = cu->per_cu;
24786 ofs.sect_off = die->sect_off;
24787 ofs.type = type;
24788 slot = (struct dwarf2_per_cu_offset_and_type **)
24789 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24790 if (*slot)
24791 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24792 sect_offset_str (die->sect_off));
24793 *slot = XOBNEW (&objfile->objfile_obstack,
24794 struct dwarf2_per_cu_offset_and_type);
24795 **slot = ofs;
24796 return type;
24797 }
24798
24799 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24800 or return NULL if the die does not have a saved type. */
24801
24802 static struct type *
24803 get_die_type_at_offset (sect_offset sect_off,
24804 dwarf2_per_cu_data *per_cu,
24805 dwarf2_per_objfile *per_objfile)
24806 {
24807 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24808
24809 if (per_objfile->die_type_hash == NULL)
24810 return NULL;
24811
24812 ofs.per_cu = per_cu;
24813 ofs.sect_off = sect_off;
24814 slot = ((struct dwarf2_per_cu_offset_and_type *)
24815 htab_find (per_objfile->die_type_hash.get (), &ofs));
24816 if (slot)
24817 return slot->type;
24818 else
24819 return NULL;
24820 }
24821
24822 /* Look up the type for DIE in CU in die_type_hash,
24823 or return NULL if DIE does not have a saved type. */
24824
24825 static struct type *
24826 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24827 {
24828 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24829 }
24830
24831 /* Trivial hash function for partial_die_info: the hash value of a DIE
24832 is its offset in .debug_info for this objfile. */
24833
24834 static hashval_t
24835 partial_die_hash (const void *item)
24836 {
24837 const struct partial_die_info *part_die
24838 = (const struct partial_die_info *) item;
24839
24840 return to_underlying (part_die->sect_off);
24841 }
24842
24843 /* Trivial comparison function for partial_die_info structures: two DIEs
24844 are equal if they have the same offset. */
24845
24846 static int
24847 partial_die_eq (const void *item_lhs, const void *item_rhs)
24848 {
24849 const struct partial_die_info *part_die_lhs
24850 = (const struct partial_die_info *) item_lhs;
24851 const struct partial_die_info *part_die_rhs
24852 = (const struct partial_die_info *) item_rhs;
24853
24854 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24855 }
24856
24857 struct cmd_list_element *set_dwarf_cmdlist;
24858 struct cmd_list_element *show_dwarf_cmdlist;
24859
24860 static void
24861 show_check_physname (struct ui_file *file, int from_tty,
24862 struct cmd_list_element *c, const char *value)
24863 {
24864 fprintf_filtered (file,
24865 _("Whether to check \"physname\" is %s.\n"),
24866 value);
24867 }
24868
24869 void _initialize_dwarf2_read ();
24870 void
24871 _initialize_dwarf2_read ()
24872 {
24873 add_setshow_prefix_cmd ("dwarf", class_maintenance,
24874 _("\
24875 Set DWARF specific variables.\n\
24876 Configure DWARF variables such as the cache size."),
24877 _("\
24878 Show DWARF specific variables.\n\
24879 Show DWARF variables such as the cache size."),
24880 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
24881 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
24882
24883 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24884 &dwarf_max_cache_age, _("\
24885 Set the upper bound on the age of cached DWARF compilation units."), _("\
24886 Show the upper bound on the age of cached DWARF compilation units."), _("\
24887 A higher limit means that cached compilation units will be stored\n\
24888 in memory longer, and more total memory will be used. Zero disables\n\
24889 caching, which can slow down startup."),
24890 NULL,
24891 show_dwarf_max_cache_age,
24892 &set_dwarf_cmdlist,
24893 &show_dwarf_cmdlist);
24894
24895 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24896 Set debugging of the DWARF reader."), _("\
24897 Show debugging of the DWARF reader."), _("\
24898 When enabled (non-zero), debugging messages are printed during DWARF\n\
24899 reading and symtab expansion. A value of 1 (one) provides basic\n\
24900 information. A value greater than 1 provides more verbose information."),
24901 NULL,
24902 NULL,
24903 &setdebuglist, &showdebuglist);
24904
24905 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24906 Set debugging of the DWARF DIE reader."), _("\
24907 Show debugging of the DWARF DIE reader."), _("\
24908 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24909 The value is the maximum depth to print."),
24910 NULL,
24911 NULL,
24912 &setdebuglist, &showdebuglist);
24913
24914 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24915 Set debugging of the dwarf line reader."), _("\
24916 Show debugging of the dwarf line reader."), _("\
24917 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24918 A value of 1 (one) provides basic information.\n\
24919 A value greater than 1 provides more verbose information."),
24920 NULL,
24921 NULL,
24922 &setdebuglist, &showdebuglist);
24923
24924 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24925 Set cross-checking of \"physname\" code against demangler."), _("\
24926 Show cross-checking of \"physname\" code against demangler."), _("\
24927 When enabled, GDB's internal \"physname\" code is checked against\n\
24928 the demangler."),
24929 NULL, show_check_physname,
24930 &setdebuglist, &showdebuglist);
24931
24932 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24933 no_class, &use_deprecated_index_sections, _("\
24934 Set whether to use deprecated gdb_index sections."), _("\
24935 Show whether to use deprecated gdb_index sections."), _("\
24936 When enabled, deprecated .gdb_index sections are used anyway.\n\
24937 Normally they are ignored either because of a missing feature or\n\
24938 performance issue.\n\
24939 Warning: This option must be enabled before gdb reads the file."),
24940 NULL,
24941 NULL,
24942 &setlist, &showlist);
24943
24944 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24945 &dwarf2_locexpr_funcs);
24946 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24947 &dwarf2_loclist_funcs);
24948
24949 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24950 &dwarf2_block_frame_base_locexpr_funcs);
24951 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24952 &dwarf2_block_frame_base_loclist_funcs);
24953
24954 #if GDB_SELF_TEST
24955 selftests::register_test ("dw2_expand_symtabs_matching",
24956 selftests::dw2_expand_symtabs_matching::run_test);
24957 selftests::register_test ("dwarf2_find_containing_comp_unit",
24958 selftests::find_containing_comp_unit::run_test);
24959 #endif
24960 }